mb/google/brya/var/orisa: Update Type C DisplayPort HPD Configuration
[coreboot2.git] / src / device / device_util.c
blobec2d1591abdc144501cad217eac11f8aa1736938
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <assert.h>
4 #include <commonlib/bsd/helpers.h>
5 #include <console/console.h>
6 #include <device/device.h>
7 #include <device/pci_def.h>
8 #include <device/pci_ids.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <types.h>
14 /**
15 * Given a Local APIC ID, find the device structure.
17 * @param apic_id The Local APIC ID number.
18 * @return Pointer to the device structure (if found), 0 otherwise.
20 struct device *dev_find_lapic(unsigned int apic_id)
22 struct device *dev;
23 struct device *result = NULL;
25 for (dev = all_devices; dev; dev = dev->next) {
26 if (dev->path.type == DEVICE_PATH_APIC &&
27 dev->path.apic.apic_id == apic_id) {
28 result = dev;
29 break;
32 return result;
35 /**
36 * Find a device of a given vendor and type.
38 * @param vendor A PCI vendor ID (e.g. 0x8086 for Intel).
39 * @param device A PCI device ID.
40 * @param from Pointer to the device structure, used as a starting point in
41 * the linked list of all_devices, which can be 0 to start at the
42 * head of the list (i.e. all_devices).
43 * @return Pointer to the device struct.
45 struct device *dev_find_device(u16 vendor, u16 device, struct device *from)
47 if (!from)
48 from = all_devices;
49 else
50 from = from->next;
52 while (from && (from->vendor != vendor || from->device != device))
53 from = from->next;
55 return from;
58 /**
59 * Find a device of a given class.
61 * @param class Class of the device.
62 * @param from Pointer to the device structure, used as a starting point in
63 * the linked list of all_devices, which can be 0 to start at the
64 * head of the list (i.e. all_devices).
65 * @return Pointer to the device struct.
67 struct device *dev_find_class(unsigned int class, struct device *from)
69 if (!from)
70 from = all_devices;
71 else
72 from = from->next;
74 while (from && (from->class & 0xffffff00) != class)
75 from = from->next;
77 return from;
80 /**
81 * Encode the device path into 3 bytes for logging to CMOS.
83 * @param dev The device path to encode.
84 * @return Device path encoded into lower 3 bytes of dword.
86 u32 dev_path_encode(const struct device *dev)
88 u32 ret;
90 if (!dev)
91 return 0;
93 /* Store the device type in 3rd byte. */
94 ret = dev->path.type << 16;
96 /* Encode the device specific path in the low word. */
97 switch (dev->path.type) {
98 case DEVICE_PATH_ROOT:
99 break;
100 case DEVICE_PATH_PCI:
101 ret |= dev->upstream->segment_group << 16 | dev->upstream->secondary << 8 | dev->path.pci.devfn;
102 break;
103 case DEVICE_PATH_PNP:
104 ret |= dev->path.pnp.port << 8 | dev->path.pnp.device;
105 break;
106 case DEVICE_PATH_I2C:
107 ret |= dev->path.i2c.mode_10bit << 8 | dev->path.i2c.device;
108 break;
109 case DEVICE_PATH_APIC:
110 ret |= dev->path.apic.apic_id;
111 break;
112 case DEVICE_PATH_DOMAIN:
113 ret |= dev->path.domain.domain_id;
114 break;
115 case DEVICE_PATH_CPU_CLUSTER:
116 ret |= dev->path.cpu_cluster.cluster;
117 break;
118 case DEVICE_PATH_CPU:
119 ret |= dev->path.cpu.id;
120 break;
121 case DEVICE_PATH_CPU_BUS:
122 ret |= dev->path.cpu_bus.id;
123 break;
124 case DEVICE_PATH_IOAPIC:
125 ret |= dev->path.ioapic.ioapic_id;
126 break;
127 case DEVICE_PATH_GENERIC:
128 ret |= dev->path.generic.subid << 8 | dev->path.generic.id;
129 break;
130 case DEVICE_PATH_SPI:
131 ret |= dev->path.spi.cs;
132 break;
133 case DEVICE_PATH_USB:
134 ret |= dev->path.usb.port_type << 8 | dev->path.usb.port_id;
135 break;
136 case DEVICE_PATH_GPIO:
137 ret |= dev->path.gpio.id;
138 break;
139 case DEVICE_PATH_MDIO:
140 ret |= dev->path.mdio.addr;
141 break;
142 case DEVICE_PATH_NONE:
143 case DEVICE_PATH_MMIO: /* don't care */
144 default:
145 break;
148 return ret;
152 * Warning: This function uses a static buffer. Don't call it more than once
153 * from the same print statement!
155 const char *dev_path(const struct device *dev)
157 static char buffer[DEVICE_PATH_MAX];
159 buffer[0] = '\0';
160 if (!dev) {
161 strcpy(buffer, "<null>");
162 } else {
163 switch (dev->path.type) {
164 case DEVICE_PATH_NONE:
165 strcpy(buffer, "NONE");
166 break;
167 case DEVICE_PATH_ROOT:
168 strcpy(buffer, "Root Device");
169 break;
170 case DEVICE_PATH_PCI:
171 snprintf(buffer, sizeof(buffer),
172 "PCI: %02x:%02x:%02x.%01x",
173 dev->upstream->segment_group,
174 dev->upstream->secondary,
175 PCI_SLOT(dev->path.pci.devfn),
176 PCI_FUNC(dev->path.pci.devfn));
177 break;
178 case DEVICE_PATH_PNP:
179 snprintf(buffer, sizeof(buffer), "PNP: %04x.%01x",
180 dev->path.pnp.port, dev->path.pnp.device);
181 break;
182 case DEVICE_PATH_I2C:
183 snprintf(buffer, sizeof(buffer), "I2C: %02x:%02x",
184 dev->upstream->secondary,
185 dev->path.i2c.device);
186 break;
187 case DEVICE_PATH_APIC:
188 snprintf(buffer, sizeof(buffer), "APIC: %02x",
189 dev->path.apic.apic_id);
190 break;
191 case DEVICE_PATH_IOAPIC:
192 snprintf(buffer, sizeof(buffer), "IOAPIC: %02x",
193 dev->path.ioapic.ioapic_id);
194 break;
195 case DEVICE_PATH_DOMAIN:
196 snprintf(buffer, sizeof(buffer), "DOMAIN: %08x",
197 dev->path.domain.domain_id);
198 break;
199 case DEVICE_PATH_CPU_CLUSTER:
200 snprintf(buffer, sizeof(buffer), "CPU_CLUSTER: %01x",
201 dev->path.cpu_cluster.cluster);
202 break;
203 case DEVICE_PATH_CPU:
204 snprintf(buffer, sizeof(buffer),
205 "CPU: %02x", dev->path.cpu.id);
206 break;
207 case DEVICE_PATH_CPU_BUS:
208 snprintf(buffer, sizeof(buffer),
209 "CPU_BUS: %02x", dev->path.cpu_bus.id);
210 break;
211 case DEVICE_PATH_GENERIC:
212 snprintf(buffer, sizeof(buffer),
213 "GENERIC: %d.%d", dev->path.generic.id,
214 dev->path.generic.subid);
215 break;
216 case DEVICE_PATH_SPI:
217 snprintf(buffer, sizeof(buffer), "SPI: %02x",
218 dev->path.spi.cs);
219 break;
220 case DEVICE_PATH_USB:
221 snprintf(buffer, sizeof(buffer), "USB%u port %u",
222 dev->path.usb.port_type, dev->path.usb.port_id);
223 break;
224 case DEVICE_PATH_MMIO:
225 snprintf(buffer, sizeof(buffer), "MMIO: %08lx",
226 dev->path.mmio.addr);
227 break;
228 case DEVICE_PATH_GPIO:
229 snprintf(buffer, sizeof(buffer), "GPIO: %d", dev->path.gpio.id);
230 break;
231 case DEVICE_PATH_MDIO:
232 snprintf(buffer, sizeof(buffer), "MDIO: %02x", dev->path.mdio.addr);
233 break;
234 case DEVICE_PATH_GICC_V3:
235 snprintf(buffer, sizeof(buffer), "GICV3: %02x", dev->path.gicc_v3.mpidr);
236 break;
237 default:
238 printk(BIOS_ERR, "Unknown device path type: %d\n",
239 dev->path.type);
240 break;
243 return buffer;
246 const char *dev_name(const struct device *dev)
248 if (dev->name)
249 return dev->name;
250 else if (dev->chip_ops && dev->chip_ops->name)
251 return dev->chip_ops->name;
252 else
253 return "unknown";
256 /* Returns the domain for the given device */
257 const struct device *dev_get_domain(const struct device *dev)
259 /* Walk up the tree up to the domain */
260 while (dev && dev->upstream && !is_root_device(dev)) {
261 if (dev->path.type == DEVICE_PATH_DOMAIN)
262 return dev;
263 dev = dev->upstream->dev;
266 return NULL;
269 unsigned int dev_get_domain_id(const struct device *dev)
271 const struct device *domain_dev = dev_get_domain(dev);
273 assert(domain_dev);
275 if (!domain_dev) {
276 printk(BIOS_ERR, "%s: doesn't have a domain device\n", dev_path(dev));
277 return 0;
280 return domain_dev->path.domain.domain_id;
283 bool is_domain0(const struct device *dev)
285 return dev && dev->path.type == DEVICE_PATH_DOMAIN && dev->path.domain.domain_id == 0;
288 bool is_dev_on_domain0(const struct device *dev)
290 return is_domain0(dev_get_domain(dev));
294 * Allocate 64 more resources to the free list.
296 * @return TODO.
298 static int allocate_more_resources(void)
300 int i;
301 struct resource *new_res_list;
303 new_res_list = malloc(64 * sizeof(*new_res_list));
305 if (new_res_list == NULL)
306 return 0;
308 memset(new_res_list, 0, 64 * sizeof(*new_res_list));
310 for (i = 0; i < 64 - 1; i++)
311 new_res_list[i].next = &new_res_list[i+1];
313 free_resources = new_res_list;
314 return 1;
318 * Remove resource res from the device's list and add it to the free list.
320 * @param dev TODO
321 * @param res TODO
322 * @param prev TODO
323 * @return TODO.
325 static void free_resource(struct device *dev, struct resource *res,
326 struct resource *prev)
328 if (prev)
329 prev->next = res->next;
330 else
331 dev->resource_list = res->next;
333 res->next = free_resources;
334 free_resources = res;
338 * See if we have unused but allocated resource structures.
340 * If so remove the allocation.
342 * @param dev The device to find the resource on.
344 void compact_resources(struct device *dev)
346 struct resource *res, *next, *prev = NULL;
348 /* Move all of the free resources to the end */
349 for (res = dev->resource_list; res; res = next) {
350 next = res->next;
351 if (!res->flags)
352 free_resource(dev, res, prev);
353 else
354 prev = res;
359 * See if a resource structure already exists for a given index.
361 * @param dev The device to find the resource on.
362 * @param index The index of the resource on the device.
363 * @return The resource, if it already exists.
365 struct resource *probe_resource(const struct device *dev, unsigned int index)
367 struct resource *res;
369 /* See if there is a resource with the appropriate index */
370 for (res = dev->resource_list; res; res = res->next) {
371 if (res->index == index)
372 break;
375 return res;
379 * See if a resource structure already exists for a given index and if not
380 * allocate one.
382 * Then initialize the resource to default values.
384 * @param dev The device to find the resource on.
385 * @param index The index of the resource on the device.
386 * @return TODO.
388 struct resource *new_resource(struct device *dev, unsigned int index)
390 struct resource *resource, *tail;
392 /* First move all of the free resources to the end. */
393 compact_resources(dev);
395 /* See if there is a resource with the appropriate index. */
396 resource = probe_resource(dev, index);
397 if (!resource) {
398 if (free_resources == NULL && !allocate_more_resources())
399 die("Couldn't allocate more resources.");
401 resource = free_resources;
402 free_resources = free_resources->next;
403 memset(resource, 0, sizeof(*resource));
404 resource->next = NULL;
405 tail = dev->resource_list;
406 if (tail) {
407 while (tail->next)
408 tail = tail->next;
409 tail->next = resource;
410 } else {
411 dev->resource_list = resource;
415 /* Initialize the resource values. */
416 if (!(resource->flags & IORESOURCE_FIXED)) {
417 resource->flags = 0;
418 resource->base = 0;
420 resource->size = 0;
421 resource->limit = 0;
422 resource->index = index;
423 resource->align = 0;
424 resource->gran = 0;
426 return resource;
430 * Return an existing resource structure for a given index.
432 * @param dev The device to find the resource on.
433 * @param index The index of the resource on the device.
434 * return TODO.
436 struct resource *find_resource(const struct device *dev, unsigned int index)
438 struct resource *resource;
440 /* See if there is a resource with the appropriate index. */
441 resource = probe_resource(dev, index);
442 if (!resource)
443 die("%s missing resource: %02x\n", dev_path(dev), index);
444 return resource;
448 * Round a number up to the next multiple of gran.
450 * @param val The starting value.
451 * @param gran Granularity we are aligning the number to.
452 * @return The aligned value.
454 static resource_t align_up(resource_t val, unsigned long gran)
456 resource_t mask;
457 mask = (1ULL << gran) - 1ULL;
458 val += mask;
459 val &= ~mask;
460 return val;
464 * Round a number up to the previous multiple of gran.
466 * @param val The starting value.
467 * @param gran Granularity we are aligning the number to.
468 * @return The aligned value.
470 static resource_t align_down(resource_t val, unsigned long gran)
472 resource_t mask;
473 mask = (1ULL << gran) - 1ULL;
474 val &= ~mask;
475 return val;
479 * Compute the maximum address that is part of a resource.
481 * @param resource The resource whose limit is desired.
482 * @return The end.
484 resource_t resource_end(const struct resource *resource)
486 resource_t base, end;
488 /* Get the base address. */
489 base = resource->base;
492 * For a non bridge resource granularity and alignment are the same.
493 * For a bridge resource align is the largest needed alignment below
494 * the bridge. While the granularity is simply how many low bits of
495 * the address cannot be set.
498 /* Get the end (rounded up). */
499 end = base + align_up(resource->size, resource->gran) - 1;
501 return end;
505 * Compute the maximum legal value for resource->base.
507 * @param resource The resource whose maximum is desired.
508 * @return The maximum.
510 resource_t resource_max(const struct resource *resource)
512 resource_t max;
514 max = align_down(resource->limit - resource->size + 1, resource->align);
516 return max;
520 * Return the resource type of a resource.
522 * @param resource The resource type to decode.
523 * @return TODO.
525 const char *resource_type(const struct resource *resource)
527 static char buffer[RESOURCE_TYPE_MAX];
528 snprintf(buffer, sizeof(buffer), "%s%s%s%s",
529 ((resource->flags & IORESOURCE_READONLY) ? "ro" : ""),
530 ((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""),
531 ((resource->flags == 0) ? "unused" :
532 (resource->flags & IORESOURCE_IO) ? "io" :
533 (resource->flags & IORESOURCE_DRQ) ? "drq" :
534 (resource->flags & IORESOURCE_IRQ) ? "irq" :
535 (resource->flags & IORESOURCE_MEM) ? "mem" : "??????"),
536 ((resource->flags & IORESOURCE_PCI64) ? "64" : ""));
537 return buffer;
541 * Print the resource that was just stored.
543 * @param dev The device the stored resource lives on.
544 * @param resource The resource that was just stored.
545 * @param comment TODO
547 void report_resource_stored(struct device *dev, const struct resource *resource,
548 const char *comment)
550 char buf[10];
551 unsigned long long base, end;
553 if (!(resource->flags & IORESOURCE_STORED))
554 return;
556 base = resource->base;
557 end = resource_end(resource);
558 buf[0] = '\0';
560 if (dev->downstream && (resource->flags & IORESOURCE_PCI_BRIDGE)) {
561 snprintf(buf, sizeof(buf),
562 "seg %02x bus %02x ", dev->downstream->segment_group,
563 dev->downstream->secondary);
565 printk(BIOS_DEBUG, "%s %02lx <- [0x%016llx - 0x%016llx] size 0x%08llx "
566 "gran 0x%02x %s%s%s\n", dev_path(dev), resource->index,
567 base, end, resource->size, resource->gran, buf,
568 resource_type(resource), comment);
571 void search_bus_resources(struct bus *bus, unsigned long type_mask,
572 unsigned long type, resource_search_t search,
573 void *gp)
575 struct device *curdev;
577 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
578 struct resource *res;
580 /* Ignore disabled devices. */
581 if (!curdev->enabled)
582 continue;
584 for (res = curdev->resource_list; res; res = res->next) {
585 /* If it isn't the right kind of resource ignore it. */
586 if ((res->flags & type_mask) != type)
587 continue;
589 /* If it is a subtractive resource recurse. */
590 if (res->flags & IORESOURCE_SUBTRACTIVE) {
591 if (curdev->downstream)
592 search_bus_resources(curdev->downstream, type_mask, type,
593 search, gp);
594 continue;
596 search(gp, curdev, res);
601 void search_global_resources(unsigned long type_mask, unsigned long type,
602 resource_search_t search, void *gp)
604 struct device *curdev;
606 for (curdev = all_devices; curdev; curdev = curdev->next) {
607 struct resource *res;
609 /* Ignore disabled devices. */
610 if (!curdev->enabled)
611 continue;
613 for (res = curdev->resource_list; res; res = res->next) {
614 /* If it isn't the right kind of resource ignore it. */
615 if ((res->flags & type_mask) != type)
616 continue;
618 /* If it is a subtractive resource ignore it. */
619 if (res->flags & IORESOURCE_SUBTRACTIVE)
620 continue;
622 /* If the resource is not assigned ignore it. */
623 if (!(res->flags & IORESOURCE_ASSIGNED))
624 continue;
626 search(gp, curdev, res);
631 void dev_set_enabled(struct device *dev, int enable)
633 if (dev->enabled == enable)
634 return;
636 dev->enabled = enable;
637 if (dev->ops && dev->ops->enable)
638 dev->ops->enable(dev);
639 else if (dev->chip_ops && dev->chip_ops->enable_dev)
640 dev->chip_ops->enable_dev(dev);
643 void disable_children(struct bus *bus)
645 struct device *child;
647 for (child = bus->children; child; child = child->sibling) {
648 if (child->downstream)
649 disable_children(child->downstream);
650 dev_set_enabled(child, 0);
655 * Returns true if the device is an enabled bridge that has at least
656 * one enabled device on its secondary bus that is not of type NONE.
658 bool dev_is_active_bridge(struct device *dev)
660 struct device *child;
662 if (!dev || !dev->enabled)
663 return 0;
665 if (!dev->downstream || !dev->downstream->children)
666 return 0;
668 for (child = dev->downstream->children; child; child = child->sibling) {
669 if (child->path.type == DEVICE_PATH_NONE)
670 continue;
671 if (child->enabled)
672 return 1;
675 return 0;
678 static void resource_tree(const struct device *root, int debug_level, int depth)
680 int i = 0;
681 struct device *child;
682 struct resource *res;
683 char indent[30]; /* If your tree has more levels, it's wrong. */
685 for (i = 0; i < depth + 1 && i < 29; i++)
686 indent[i] = ' ';
687 indent[i] = '\0';
689 printk(BIOS_DEBUG, "%s%s", indent, dev_path(root));
690 if (root->downstream && root->downstream->children)
691 printk(BIOS_DEBUG, " child on link 0 %s",
692 dev_path(root->downstream->children));
693 printk(BIOS_DEBUG, "\n");
695 for (res = root->resource_list; res; res = res->next) {
696 printk(debug_level, "%s%s resource base %llx size %llx "
697 "align %d gran %d limit %llx flags %lx index %lx\n",
698 indent, dev_path(root), res->base, res->size,
699 res->align, res->gran, res->limit, res->flags,
700 res->index);
703 if (!root->downstream)
704 return;
706 for (child = root->downstream->children; child; child = child->sibling)
707 resource_tree(child, debug_level, depth + 1);
710 void print_resource_tree(const struct device *root, int debug_level,
711 const char *msg)
713 /* Bail if root is null. */
714 if (!root) {
715 printk(debug_level, "%s passed NULL for root!\n", __func__);
716 return;
719 /* Bail if not printing to screen. */
720 if (!printk(debug_level, "Show resources in subtree (%s)...%s\n",
721 dev_path(root), msg))
722 return;
724 resource_tree(root, debug_level, 0);
727 void show_devs_tree(const struct device *dev, int debug_level, int depth)
729 char depth_str[20];
730 int i;
731 struct device *sibling;
733 for (i = 0; i < depth; i++)
734 depth_str[i] = ' ';
735 depth_str[i] = '\0';
737 printk(debug_level, "%s%s: enabled %d\n",
738 depth_str, dev_path(dev), dev->enabled);
740 if (!dev->downstream)
741 return;
743 for (sibling = dev->downstream->children; sibling; sibling = sibling->sibling)
744 show_devs_tree(sibling, debug_level, depth + 1);
747 void show_all_devs_tree(int debug_level, const char *msg)
749 /* Bail if not printing to screen. */
750 if (!printk(debug_level, "Show all devs in tree form... %s\n", msg))
751 return;
752 show_devs_tree(all_devices, debug_level, 0);
755 void show_devs_subtree(struct device *root, int debug_level, const char *msg)
757 /* Bail if not printing to screen. */
758 if (!printk(debug_level, "Show all devs in subtree %s... %s\n",
759 dev_path(root), msg))
760 return;
761 printk(debug_level, "%s\n", msg);
762 show_devs_tree(root, debug_level, 0);
765 void show_all_devs(int debug_level, const char *msg)
767 struct device *dev;
769 /* Bail if not printing to screen. */
770 if (!printk(debug_level, "Show all devs... %s\n", msg))
771 return;
772 for (dev = all_devices; dev; dev = dev->next) {
773 printk(debug_level, "%s: enabled %d\n",
774 dev_path(dev), dev->enabled);
778 void show_one_resource(int debug_level, struct device *dev,
779 struct resource *resource, const char *comment)
781 char buf[10];
782 unsigned long long base, end;
783 base = resource->base;
784 end = resource_end(resource);
785 buf[0] = '\0';
787 printk(debug_level, "%s %02lx <- [0x%016llx - 0x%016llx] "
788 "size 0x%08llx gran 0x%02x %s%s%s\n", dev_path(dev),
789 resource->index, base, end, resource->size, resource->gran,
790 buf, resource_type(resource), comment);
793 void show_all_devs_resources(int debug_level, const char *msg)
795 struct device *dev;
797 if (!printk(debug_level, "Show all devs with resources... %s\n", msg))
798 return;
800 for (dev = all_devices; dev; dev = dev->next) {
801 struct resource *res;
802 printk(debug_level, "%s: enabled %d\n",
803 dev_path(dev), dev->enabled);
804 for (res = dev->resource_list; res; res = res->next)
805 show_one_resource(debug_level, dev, res, "");
809 const struct resource *resource_range_idx(struct device *dev, unsigned long index,
810 uint64_t base, uint64_t size, unsigned long flags)
812 struct resource *resource;
813 if (!size)
814 return NULL;
816 resource = new_resource(dev, index);
817 resource->base = base;
819 if (flags & IORESOURCE_FIXED)
820 resource->size = size;
821 if (flags & IORESOURCE_BRIDGE)
822 resource->limit = base + size - 1;
824 resource->flags = IORESOURCE_ASSIGNED;
825 resource->flags |= flags;
827 printk(BIOS_SPEW, "dev: %s, index: 0x%lx, base: 0x%llx, size: 0x%llx\n",
828 dev_path(dev), resource->index, resource->base, resource->size);
830 return resource;
833 const struct resource *lower_ram_end(struct device *dev, unsigned long index, uint64_t end)
835 return ram_from_to(dev, index, 0, end);
838 const struct resource *upper_ram_end(struct device *dev, unsigned long index, uint64_t end)
840 if (end <= 4ull * GiB)
841 return NULL;
843 printk(BIOS_INFO, "Available memory above 4GB: %lluM\n", (end - 4ull * GiB) / MiB);
845 return ram_from_to(dev, index, 4ull * GiB, end);
848 void mmconf_resource(struct device *dev, unsigned long index)
850 struct resource *resource = new_resource(dev, index);
851 resource->base = CONFIG_ECAM_MMCONF_BASE_ADDRESS;
852 resource->size = CONFIG_ECAM_MMCONF_LENGTH;
853 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
854 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
856 printk(BIOS_DEBUG, "Adding PCIe enhanced config space BAR 0x%08lx-0x%08lx.\n",
857 (unsigned long)(resource->base),
858 (unsigned long)(resource->base + resource->size));
861 void tolm_test(void *gp, struct device *dev, struct resource *new)
863 struct resource **best_p = gp;
864 struct resource *best;
866 best = *best_p;
869 * If resource is not allocated any space i.e. size is zero,
870 * then do not consider this resource in tolm calculations.
872 if (new->size == 0)
873 return;
875 if (!best || (best->base > new->base))
876 best = new;
878 *best_p = best;
881 u32 find_pci_tolm(struct bus *bus)
883 struct resource *min = NULL;
884 u32 tolm;
885 unsigned long mask_match = IORESOURCE_MEM | IORESOURCE_ASSIGNED;
887 search_bus_resources(bus, mask_match, mask_match, tolm_test, &min);
889 tolm = 0xffffffffUL;
891 if (min && tolm > min->base)
892 tolm = min->base;
894 return tolm;
897 /* Count of enabled CPUs */
898 int dev_count_cpu(void)
900 struct device *cpu;
901 int count = 0;
903 for (cpu = all_devices; cpu; cpu = cpu->next) {
904 if (!is_enabled_cpu(cpu))
905 continue;
906 count++;
909 return count;
912 /* Get device path name */
913 const char *dev_path_name(enum device_path_type type)
915 static const char *const type_names[] = DEVICE_PATH_NAMES;
916 const char *type_name = "Unknown";
918 /* Translate the type value into a string */
919 if (type < ARRAY_SIZE(type_names))
920 type_name = type_names[type];
921 return type_name;
924 bool dev_path_hotplug(const struct device *dev)
926 for (dev = dev->upstream->dev; dev != dev->upstream->dev; dev = dev->upstream->dev) {
927 if (dev->hotplug_port)
928 return true;
930 return false;
933 void log_resource(const char *type, const struct device *dev, const struct resource *res,
934 const char *srcfile, const int line)
936 printk(BIOS_SPEW, "%s:%d res: %s, dev: %s, index: 0x%lx, base: 0x%llx, "
937 "end: 0x%llx, size_kb: 0x%llx\n",
938 srcfile, line, type, dev_path(dev), res->index, res->base,
939 resource_end(res), res->size / KiB);
942 bool is_cpu(const struct device *cpu)
944 return cpu->path.type == DEVICE_PATH_APIC &&
945 cpu->upstream->dev->path.type == DEVICE_PATH_CPU_CLUSTER;
948 bool is_enabled_cpu(const struct device *cpu)
950 return is_cpu(cpu) && cpu->enabled;
953 bool is_pci(const struct device *pci)
955 return pci->path.type == DEVICE_PATH_PCI;
958 bool is_enabled_pci(const struct device *pci)
960 return is_pci(pci) && pci->enabled;
963 bool is_pci_dev_on_bus(const struct device *pci, unsigned int bus)
965 return is_pci(pci) && pci->upstream->segment_group == 0
966 && pci->upstream->secondary == bus;
969 bool is_pci_bridge(const struct device *pci)
971 return is_pci(pci) && ((pci->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE);
974 bool is_pci_ioapic(const struct device *pci)
976 return is_pci(pci) && ((pci->class >> 16) == PCI_BASE_CLASS_SYSTEM) &&
977 ((pci->class >> 8) == PCI_CLASS_SYSTEM_PIC) &&
978 ((pci->class & 0xff) >= 0x10);