soc/intel/alderlake: Add ADL-P 4+4 with 28W TDP
[coreboot.git] / src / device / device_util.c
blobc5e03f20795ffae208ad32585ce496a03d7097c9
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <commonlib/bsd/helpers.h>
4 #include <console/console.h>
5 #include <device/device.h>
6 #include <device/path.h>
7 #include <device/pci_def.h>
8 #include <device/resource.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <types.h>
13 /**
14 * Given a Local APIC ID, find the device structure.
16 * @param apic_id The Local APIC ID number.
17 * @return Pointer to the device structure (if found), 0 otherwise.
19 struct device *dev_find_lapic(unsigned int apic_id)
21 struct device *dev;
22 struct device *result = NULL;
24 for (dev = all_devices; dev; dev = dev->next) {
25 if (dev->path.type == DEVICE_PATH_APIC &&
26 dev->path.apic.apic_id == apic_id) {
27 result = dev;
28 break;
31 return result;
34 /**
35 * Find a device of a given vendor and type.
37 * @param vendor A PCI vendor ID (e.g. 0x8086 for Intel).
38 * @param device A PCI device ID.
39 * @param from Pointer to the device structure, used as a starting point in
40 * the linked list of all_devices, which can be 0 to start at the
41 * head of the list (i.e. all_devices).
42 * @return Pointer to the device struct.
44 struct device *dev_find_device(u16 vendor, u16 device, struct device *from)
46 if (!from)
47 from = all_devices;
48 else
49 from = from->next;
51 while (from && (from->vendor != vendor || from->device != device))
52 from = from->next;
54 return from;
57 /**
58 * Find a device of a given class.
60 * @param class Class of the device.
61 * @param from Pointer to the device structure, used as a starting point in
62 * the linked list of all_devices, which can be 0 to start at the
63 * head of the list (i.e. all_devices).
64 * @return Pointer to the device struct.
66 struct device *dev_find_class(unsigned int class, struct device *from)
68 if (!from)
69 from = all_devices;
70 else
71 from = from->next;
73 while (from && (from->class & 0xffffff00) != class)
74 from = from->next;
76 return from;
79 /**
80 * Encode the device path into 3 bytes for logging to CMOS.
82 * @param dev The device path to encode.
83 * @return Device path encoded into lower 3 bytes of dword.
85 u32 dev_path_encode(const struct device *dev)
87 u32 ret;
89 if (!dev)
90 return 0;
92 /* Store the device type in 3rd byte. */
93 ret = dev->path.type << 16;
95 /* Encode the device specific path in the low word. */
96 switch (dev->path.type) {
97 case DEVICE_PATH_ROOT:
98 break;
99 case DEVICE_PATH_PCI:
100 ret |= dev->bus->secondary << 8 | dev->path.pci.devfn;
101 break;
102 case DEVICE_PATH_PNP:
103 ret |= dev->path.pnp.port << 8 | dev->path.pnp.device;
104 break;
105 case DEVICE_PATH_I2C:
106 ret |= dev->path.i2c.mode_10bit << 8 | dev->path.i2c.device;
107 break;
108 case DEVICE_PATH_APIC:
109 ret |= dev->path.apic.apic_id;
110 break;
111 case DEVICE_PATH_DOMAIN:
112 ret |= dev->path.domain.domain;
113 break;
114 case DEVICE_PATH_CPU_CLUSTER:
115 ret |= dev->path.cpu_cluster.cluster;
116 break;
117 case DEVICE_PATH_CPU:
118 ret |= dev->path.cpu.id;
119 break;
120 case DEVICE_PATH_CPU_BUS:
121 ret |= dev->path.cpu_bus.id;
122 break;
123 case DEVICE_PATH_IOAPIC:
124 ret |= dev->path.ioapic.ioapic_id;
125 break;
126 case DEVICE_PATH_GENERIC:
127 ret |= dev->path.generic.subid << 8 | dev->path.generic.id;
128 break;
129 case DEVICE_PATH_SPI:
130 ret |= dev->path.spi.cs;
131 break;
132 case DEVICE_PATH_USB:
133 ret |= dev->path.usb.port_type << 8 | dev->path.usb.port_id;
134 break;
135 case DEVICE_PATH_GPIO:
136 ret |= dev->path.gpio.id;
137 break;
138 case DEVICE_PATH_MDIO:
139 ret |= dev->path.mdio.addr;
140 break;
141 case DEVICE_PATH_NONE:
142 case DEVICE_PATH_MMIO: /* don't care */
143 default:
144 break;
147 return ret;
151 * Warning: This function uses a static buffer. Don't call it more than once
152 * from the same print statement!
154 const char *dev_path(const struct device *dev)
156 static char buffer[DEVICE_PATH_MAX];
158 buffer[0] = '\0';
159 if (!dev) {
160 strcpy(buffer, "<null>");
161 } else {
162 switch (dev->path.type) {
163 case DEVICE_PATH_NONE:
164 strcpy(buffer, "NONE");
165 break;
166 case DEVICE_PATH_ROOT:
167 strcpy(buffer, "Root Device");
168 break;
169 case DEVICE_PATH_PCI:
170 snprintf(buffer, sizeof(buffer),
171 "PCI: %02x:%02x.%01x",
172 dev->bus->secondary,
173 PCI_SLOT(dev->path.pci.devfn),
174 PCI_FUNC(dev->path.pci.devfn));
175 break;
176 case DEVICE_PATH_PNP:
177 snprintf(buffer, sizeof(buffer), "PNP: %04x.%01x",
178 dev->path.pnp.port, dev->path.pnp.device);
179 break;
180 case DEVICE_PATH_I2C:
181 snprintf(buffer, sizeof(buffer), "I2C: %02x:%02x",
182 dev->bus->secondary,
183 dev->path.i2c.device);
184 break;
185 case DEVICE_PATH_APIC:
186 snprintf(buffer, sizeof(buffer), "APIC: %02x",
187 dev->path.apic.apic_id);
188 break;
189 case DEVICE_PATH_IOAPIC:
190 snprintf(buffer, sizeof(buffer), "IOAPIC: %02x",
191 dev->path.ioapic.ioapic_id);
192 break;
193 case DEVICE_PATH_DOMAIN:
194 snprintf(buffer, sizeof(buffer), "DOMAIN: %04x",
195 dev->path.domain.domain);
196 break;
197 case DEVICE_PATH_CPU_CLUSTER:
198 snprintf(buffer, sizeof(buffer), "CPU_CLUSTER: %01x",
199 dev->path.cpu_cluster.cluster);
200 break;
201 case DEVICE_PATH_CPU:
202 snprintf(buffer, sizeof(buffer),
203 "CPU: %02x", dev->path.cpu.id);
204 break;
205 case DEVICE_PATH_CPU_BUS:
206 snprintf(buffer, sizeof(buffer),
207 "CPU_BUS: %02x", dev->path.cpu_bus.id);
208 break;
209 case DEVICE_PATH_GENERIC:
210 snprintf(buffer, sizeof(buffer),
211 "GENERIC: %d.%d", dev->path.generic.id,
212 dev->path.generic.subid);
213 break;
214 case DEVICE_PATH_SPI:
215 snprintf(buffer, sizeof(buffer), "SPI: %02x",
216 dev->path.spi.cs);
217 break;
218 case DEVICE_PATH_USB:
219 snprintf(buffer, sizeof(buffer), "USB%u port %u",
220 dev->path.usb.port_type, dev->path.usb.port_id);
221 break;
222 case DEVICE_PATH_MMIO:
223 snprintf(buffer, sizeof(buffer), "MMIO: %08lx",
224 dev->path.mmio.addr);
225 break;
226 case DEVICE_PATH_GPIO:
227 snprintf(buffer, sizeof(buffer), "GPIO: %d", dev->path.gpio.id);
228 break;
229 case DEVICE_PATH_MDIO:
230 snprintf(buffer, sizeof(buffer), "MDIO: %02x", dev->path.mdio.addr);
231 break;
232 default:
233 printk(BIOS_ERR, "Unknown device path type: %d\n",
234 dev->path.type);
235 break;
238 return buffer;
241 const char *dev_name(const struct device *dev)
243 if (dev->name)
244 return dev->name;
245 else if (dev->chip_ops && dev->chip_ops->name)
246 return dev->chip_ops->name;
247 else
248 return "unknown";
251 const char *bus_path(struct bus *bus)
253 static char buffer[BUS_PATH_MAX];
254 snprintf(buffer, sizeof(buffer),
255 "%s,%d", dev_path(bus->dev), bus->link_num);
256 return buffer;
260 * Allocate 64 more resources to the free list.
262 * @return TODO.
264 static int allocate_more_resources(void)
266 int i;
267 struct resource *new_res_list;
269 new_res_list = malloc(64 * sizeof(*new_res_list));
271 if (new_res_list == NULL)
272 return 0;
274 memset(new_res_list, 0, 64 * sizeof(*new_res_list));
276 for (i = 0; i < 64 - 1; i++)
277 new_res_list[i].next = &new_res_list[i+1];
279 free_resources = new_res_list;
280 return 1;
284 * Remove resource res from the device's list and add it to the free list.
286 * @param dev TODO
287 * @param res TODO
288 * @param prev TODO
289 * @return TODO.
291 static void free_resource(struct device *dev, struct resource *res,
292 struct resource *prev)
294 if (prev)
295 prev->next = res->next;
296 else
297 dev->resource_list = res->next;
299 res->next = free_resources;
300 free_resources = res;
304 * See if we have unused but allocated resource structures.
306 * If so remove the allocation.
308 * @param dev The device to find the resource on.
310 void compact_resources(struct device *dev)
312 struct resource *res, *next, *prev = NULL;
314 /* Move all of the free resources to the end */
315 for (res = dev->resource_list; res; res = next) {
316 next = res->next;
317 if (!res->flags)
318 free_resource(dev, res, prev);
319 else
320 prev = res;
325 * See if a resource structure already exists for a given index.
327 * @param dev The device to find the resource on.
328 * @param index The index of the resource on the device.
329 * @return The resource, if it already exists.
331 struct resource *probe_resource(const struct device *dev, unsigned int index)
333 struct resource *res;
335 /* See if there is a resource with the appropriate index */
336 for (res = dev->resource_list; res; res = res->next) {
337 if (res->index == index)
338 break;
341 return res;
345 * See if a resource structure already exists for a given index and if not
346 * allocate one.
348 * Then initialize the resource to default values.
350 * @param dev The device to find the resource on.
351 * @param index The index of the resource on the device.
352 * @return TODO.
354 struct resource *new_resource(struct device *dev, unsigned int index)
356 struct resource *resource, *tail;
358 /* First move all of the free resources to the end. */
359 compact_resources(dev);
361 /* See if there is a resource with the appropriate index. */
362 resource = probe_resource(dev, index);
363 if (!resource) {
364 if (free_resources == NULL && !allocate_more_resources())
365 die("Couldn't allocate more resources.");
367 resource = free_resources;
368 free_resources = free_resources->next;
369 memset(resource, 0, sizeof(*resource));
370 resource->next = NULL;
371 tail = dev->resource_list;
372 if (tail) {
373 while (tail->next)
374 tail = tail->next;
375 tail->next = resource;
376 } else {
377 dev->resource_list = resource;
381 /* Initialize the resource values. */
382 if (!(resource->flags & IORESOURCE_FIXED)) {
383 resource->flags = 0;
384 resource->base = 0;
386 resource->size = 0;
387 resource->limit = 0;
388 resource->index = index;
389 resource->align = 0;
390 resource->gran = 0;
392 return resource;
396 * Return an existing resource structure for a given index.
398 * @param dev The device to find the resource on.
399 * @param index The index of the resource on the device.
400 * return TODO.
402 struct resource *find_resource(const struct device *dev, unsigned int index)
404 struct resource *resource;
406 /* See if there is a resource with the appropriate index. */
407 resource = probe_resource(dev, index);
408 if (!resource) {
409 printk(BIOS_EMERG, "%s missing resource: %02x\n",
410 dev_path(dev), index);
411 die("");
413 return resource;
417 * Round a number up to the next multiple of gran.
419 * @param val The starting value.
420 * @param gran Granularity we are aligning the number to.
421 * @return The aligned value.
423 static resource_t align_up(resource_t val, unsigned long gran)
425 resource_t mask;
426 mask = (1ULL << gran) - 1ULL;
427 val += mask;
428 val &= ~mask;
429 return val;
433 * Round a number up to the previous multiple of gran.
435 * @param val The starting value.
436 * @param gran Granularity we are aligning the number to.
437 * @return The aligned value.
439 static resource_t align_down(resource_t val, unsigned long gran)
441 resource_t mask;
442 mask = (1ULL << gran) - 1ULL;
443 val &= ~mask;
444 return val;
448 * Compute the maximum address that is part of a resource.
450 * @param resource The resource whose limit is desired.
451 * @return The end.
453 resource_t resource_end(const struct resource *resource)
455 resource_t base, end;
457 /* Get the base address. */
458 base = resource->base;
461 * For a non bridge resource granularity and alignment are the same.
462 * For a bridge resource align is the largest needed alignment below
463 * the bridge. While the granularity is simply how many low bits of
464 * the address cannot be set.
467 /* Get the end (rounded up). */
468 end = base + align_up(resource->size, resource->gran) - 1;
470 return end;
474 * Compute the maximum legal value for resource->base.
476 * @param resource The resource whose maximum is desired.
477 * @return The maximum.
479 resource_t resource_max(const struct resource *resource)
481 resource_t max;
483 max = align_down(resource->limit - resource->size + 1, resource->align);
485 return max;
489 * Return the resource type of a resource.
491 * @param resource The resource type to decode.
492 * @return TODO.
494 const char *resource_type(const struct resource *resource)
496 static char buffer[RESOURCE_TYPE_MAX];
497 snprintf(buffer, sizeof(buffer), "%s%s%s%s",
498 ((resource->flags & IORESOURCE_READONLY) ? "ro" : ""),
499 ((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""),
500 ((resource->flags == 0) ? "unused" :
501 (resource->flags & IORESOURCE_IO) ? "io" :
502 (resource->flags & IORESOURCE_DRQ) ? "drq" :
503 (resource->flags & IORESOURCE_IRQ) ? "irq" :
504 (resource->flags & IORESOURCE_MEM) ? "mem" : "??????"),
505 ((resource->flags & IORESOURCE_PCI64) ? "64" : ""));
506 return buffer;
510 * Print the resource that was just stored.
512 * @param dev The device the stored resource lives on.
513 * @param resource The resource that was just stored.
514 * @param comment TODO
516 void report_resource_stored(struct device *dev, const struct resource *resource,
517 const char *comment)
519 char buf[10];
520 unsigned long long base, end;
522 if (!(resource->flags & IORESOURCE_STORED))
523 return;
525 base = resource->base;
526 end = resource_end(resource);
527 buf[0] = '\0';
529 if (dev->link_list && (resource->flags & IORESOURCE_PCI_BRIDGE)) {
530 snprintf(buf, sizeof(buf),
531 "bus %02x ", dev->link_list->secondary);
533 printk(BIOS_DEBUG, "%s %02lx <- [0x%016llx - 0x%016llx] size 0x%08llx "
534 "gran 0x%02x %s%s%s\n", dev_path(dev), resource->index,
535 base, end, resource->size, resource->gran, buf,
536 resource_type(resource), comment);
539 void search_bus_resources(struct bus *bus, unsigned long type_mask,
540 unsigned long type, resource_search_t search,
541 void *gp)
543 struct device *curdev;
545 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
546 struct resource *res;
548 /* Ignore disabled devices. */
549 if (!curdev->enabled)
550 continue;
552 for (res = curdev->resource_list; res; res = res->next) {
553 /* If it isn't the right kind of resource ignore it. */
554 if ((res->flags & type_mask) != type)
555 continue;
557 /* If it is a subtractive resource recurse. */
558 if (res->flags & IORESOURCE_SUBTRACTIVE) {
559 struct bus *subbus;
560 for (subbus = curdev->link_list; subbus;
561 subbus = subbus->next)
562 if (subbus->link_num
563 == IOINDEX_SUBTRACTIVE_LINK(res->index))
564 break;
565 if (!subbus) /* Why can subbus be NULL? */
566 break;
567 search_bus_resources(subbus, type_mask, type,
568 search, gp);
569 continue;
571 search(gp, curdev, res);
576 void search_global_resources(unsigned long type_mask, unsigned long type,
577 resource_search_t search, void *gp)
579 struct device *curdev;
581 for (curdev = all_devices; curdev; curdev = curdev->next) {
582 struct resource *res;
584 /* Ignore disabled devices. */
585 if (!curdev->enabled)
586 continue;
588 for (res = curdev->resource_list; res; res = res->next) {
589 /* If it isn't the right kind of resource ignore it. */
590 if ((res->flags & type_mask) != type)
591 continue;
593 /* If it is a subtractive resource ignore it. */
594 if (res->flags & IORESOURCE_SUBTRACTIVE)
595 continue;
597 /* If the resource is not assigned ignore it. */
598 if (!(res->flags & IORESOURCE_ASSIGNED))
599 continue;
601 search(gp, curdev, res);
606 void dev_set_enabled(struct device *dev, int enable)
608 if (dev->enabled == enable)
609 return;
611 dev->enabled = enable;
612 if (dev->ops && dev->ops->enable)
613 dev->ops->enable(dev);
614 else if (dev->chip_ops && dev->chip_ops->enable_dev)
615 dev->chip_ops->enable_dev(dev);
618 void disable_children(struct bus *bus)
620 struct device *child;
622 for (child = bus->children; child; child = child->sibling) {
623 struct bus *link;
624 for (link = child->link_list; link; link = link->next)
625 disable_children(link);
626 dev_set_enabled(child, 0);
631 * Returns true if the device is an enabled bridge that has at least
632 * one enabled device on its secondary bus that is not of type NONE.
634 bool dev_is_active_bridge(struct device *dev)
636 struct bus *link;
637 struct device *child;
639 if (!dev || !dev->enabled)
640 return 0;
642 if (!dev->link_list || !dev->link_list->children)
643 return 0;
645 for (link = dev->link_list; link; link = link->next) {
646 for (child = link->children; child; child = child->sibling) {
647 if (child->path.type == DEVICE_PATH_NONE)
648 continue;
650 if (child->enabled)
651 return 1;
655 return 0;
659 * Ensure the device has a minimum number of bus links.
661 * @param dev The device to add links to.
662 * @param total_links The minimum number of links to have.
664 void add_more_links(struct device *dev, unsigned int total_links)
666 struct bus *link, *last = NULL;
667 int link_num = -1;
669 for (link = dev->link_list; link; link = link->next) {
670 if (link_num < link->link_num)
671 link_num = link->link_num;
672 last = link;
675 if (last) {
676 int links = total_links - (link_num + 1);
677 if (links > 0) {
678 link = malloc(links * sizeof(*link));
679 if (!link)
680 die("Couldn't allocate more links!\n");
681 memset(link, 0, links * sizeof(*link));
682 last->next = link;
683 } else {
684 /* No more links to add */
685 return;
687 } else {
688 link = malloc(total_links * sizeof(*link));
689 if (!link)
690 die("Couldn't allocate more links!\n");
691 memset(link, 0, total_links * sizeof(*link));
692 dev->link_list = link;
695 for (link_num = link_num + 1; link_num < total_links; link_num++) {
696 link->link_num = link_num;
697 link->dev = dev;
698 link->next = link + 1;
699 last = link;
700 link = link->next;
702 last->next = NULL;
705 static void resource_tree(const struct device *root, int debug_level, int depth)
707 int i = 0;
708 struct device *child;
709 struct bus *link;
710 struct resource *res;
711 char indent[30]; /* If your tree has more levels, it's wrong. */
713 for (i = 0; i < depth + 1 && i < 29; i++)
714 indent[i] = ' ';
715 indent[i] = '\0';
717 printk(BIOS_DEBUG, "%s%s", indent, dev_path(root));
718 if (root->link_list && root->link_list->children)
719 printk(BIOS_DEBUG, " child on link 0 %s",
720 dev_path(root->link_list->children));
721 printk(BIOS_DEBUG, "\n");
723 for (res = root->resource_list; res; res = res->next) {
724 printk(debug_level, "%s%s resource base %llx size %llx "
725 "align %d gran %d limit %llx flags %lx index %lx\n",
726 indent, dev_path(root), res->base, res->size,
727 res->align, res->gran, res->limit, res->flags,
728 res->index);
731 for (link = root->link_list; link; link = link->next) {
732 for (child = link->children; child; child = child->sibling)
733 resource_tree(child, debug_level, depth + 1);
737 void print_resource_tree(const struct device *root, int debug_level,
738 const char *msg)
740 /* Bail if root is null. */
741 if (!root) {
742 printk(debug_level, "%s passed NULL for root!\n", __func__);
743 return;
746 /* Bail if not printing to screen. */
747 if (!printk(debug_level, "Show resources in subtree (%s)...%s\n",
748 dev_path(root), msg))
749 return;
751 resource_tree(root, debug_level, 0);
754 void show_devs_tree(const struct device *dev, int debug_level, int depth)
756 char depth_str[20];
757 int i;
758 struct device *sibling;
759 struct bus *link;
761 for (i = 0; i < depth; i++)
762 depth_str[i] = ' ';
763 depth_str[i] = '\0';
765 printk(debug_level, "%s%s: enabled %d\n",
766 depth_str, dev_path(dev), dev->enabled);
768 for (link = dev->link_list; link; link = link->next) {
769 for (sibling = link->children; sibling;
770 sibling = sibling->sibling)
771 show_devs_tree(sibling, debug_level, depth + 1);
775 void show_all_devs_tree(int debug_level, const char *msg)
777 /* Bail if not printing to screen. */
778 if (!printk(debug_level, "Show all devs in tree form... %s\n", msg))
779 return;
780 show_devs_tree(all_devices, debug_level, 0);
783 void show_devs_subtree(struct device *root, int debug_level, const char *msg)
785 /* Bail if not printing to screen. */
786 if (!printk(debug_level, "Show all devs in subtree %s... %s\n",
787 dev_path(root), msg))
788 return;
789 printk(debug_level, "%s\n", msg);
790 show_devs_tree(root, debug_level, 0);
793 void show_all_devs(int debug_level, const char *msg)
795 struct device *dev;
797 /* Bail if not printing to screen. */
798 if (!printk(debug_level, "Show all devs... %s\n", msg))
799 return;
800 for (dev = all_devices; dev; dev = dev->next) {
801 printk(debug_level, "%s: enabled %d\n",
802 dev_path(dev), dev->enabled);
806 void show_one_resource(int debug_level, struct device *dev,
807 struct resource *resource, const char *comment)
809 char buf[10];
810 unsigned long long base, end;
811 base = resource->base;
812 end = resource_end(resource);
813 buf[0] = '\0';
815 printk(debug_level, "%s %02lx <- [0x%016llx - 0x%016llx] "
816 "size 0x%08llx gran 0x%02x %s%s%s\n", dev_path(dev),
817 resource->index, base, end, resource->size, resource->gran,
818 buf, resource_type(resource), comment);
821 void show_all_devs_resources(int debug_level, const char *msg)
823 struct device *dev;
825 if (!printk(debug_level, "Show all devs with resources... %s\n", msg))
826 return;
828 for (dev = all_devices; dev; dev = dev->next) {
829 struct resource *res;
830 printk(debug_level, "%s: enabled %d\n",
831 dev_path(dev), dev->enabled);
832 for (res = dev->resource_list; res; res = res->next)
833 show_one_resource(debug_level, dev, res, "");
837 const struct resource *fixed_resource_range_idx(struct device *dev, unsigned long index,
838 uint64_t base, uint64_t size, unsigned long flags)
840 struct resource *resource;
841 if (!size)
842 return NULL;
844 resource = new_resource(dev, index);
845 resource->base = base;
846 resource->size = size;
847 resource->flags = IORESOURCE_FIXED | IORESOURCE_ASSIGNED;
848 resource->flags |= flags;
850 printk(BIOS_SPEW, "dev: %s, index: 0x%lx, base: 0x%llx, size: 0x%llx\n",
851 dev_path(dev), resource->index, resource->base, resource->size);
853 return resource;
856 const struct resource *lower_ram_end(struct device *dev, unsigned long index, uint64_t end)
858 return ram_from_to(dev, index, 0, end);
861 const struct resource *upper_ram_end(struct device *dev, unsigned long index, uint64_t end)
863 if (end <= 4ull * GiB)
864 return NULL;
866 printk(BIOS_INFO, "Available memory above 4GB: %lluM\n", (end - 4ull * GiB) / MiB);
868 return ram_from_to(dev, index, 4ull * GiB, end);
871 void mmconf_resource(struct device *dev, unsigned long index)
873 struct resource *resource = new_resource(dev, index);
874 resource->base = CONFIG_ECAM_MMCONF_BASE_ADDRESS;
875 resource->size = CONFIG_ECAM_MMCONF_LENGTH;
876 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
877 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
879 printk(BIOS_DEBUG, "Adding PCIe enhanced config space BAR 0x%08lx-0x%08lx.\n",
880 (unsigned long)(resource->base),
881 (unsigned long)(resource->base + resource->size));
884 void tolm_test(void *gp, struct device *dev, struct resource *new)
886 struct resource **best_p = gp;
887 struct resource *best;
889 best = *best_p;
892 * If resource is not allocated any space i.e. size is zero,
893 * then do not consider this resource in tolm calculations.
895 if (new->size == 0)
896 return;
898 if (!best || (best->base > new->base))
899 best = new;
901 *best_p = best;
904 u32 find_pci_tolm(struct bus *bus)
906 struct resource *min = NULL;
907 u32 tolm;
908 unsigned long mask_match = IORESOURCE_MEM | IORESOURCE_ASSIGNED;
910 search_bus_resources(bus, mask_match, mask_match, tolm_test, &min);
912 tolm = 0xffffffffUL;
914 if (min && tolm > min->base)
915 tolm = min->base;
917 return tolm;
920 /* Count of enabled CPUs */
921 int dev_count_cpu(void)
923 struct device *cpu;
924 int count = 0;
926 for (cpu = all_devices; cpu; cpu = cpu->next) {
927 if (!is_enabled_cpu(cpu))
928 continue;
929 count++;
932 return count;
935 /* Get device path name */
936 const char *dev_path_name(enum device_path_type type)
938 static const char *const type_names[] = DEVICE_PATH_NAMES;
939 const char *type_name = "Unknown";
941 /* Translate the type value into a string */
942 if (type < ARRAY_SIZE(type_names))
943 type_name = type_names[type];
944 return type_name;
947 bool dev_path_hotplug(const struct device *dev)
949 for (dev = dev->bus->dev; dev != dev->bus->dev; dev = dev->bus->dev) {
950 if (dev->hotplug_port)
951 return true;
953 return false;
956 void log_resource(const char *type, const struct device *dev, const struct resource *res,
957 const char *srcfile, const int line)
959 printk(BIOS_SPEW, "%s:%d res: %s, dev: %s, index: 0x%lx, base: 0x%llx, "
960 "end: 0x%llx, size_kb: 0x%llx\n",
961 srcfile, line, type, dev_path(dev), res->index, res->base,
962 resource_end(res), res->size / KiB);
965 bool is_cpu(const struct device *cpu)
967 return cpu->path.type == DEVICE_PATH_APIC &&
968 cpu->bus->dev->path.type == DEVICE_PATH_CPU_CLUSTER;
971 bool is_enabled_cpu(const struct device *cpu)
973 return is_cpu(cpu) && cpu->enabled;
976 bool is_pci(const struct device *pci)
978 return pci->path.type == DEVICE_PATH_PCI;
981 bool is_enabled_pci(const struct device *pci)
983 return is_pci(pci) && pci->enabled;
986 bool is_pci_dev_on_bus(const struct device *pci, unsigned int bus)
988 return is_pci(pci) && pci->bus->secondary == bus;