lib/smbios: Improve Type9
[coreboot2.git] / src / device / device_util.c
blobf33c9250dc4ab024c5996e6f3ebd525ed7217c3f
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 <stdio.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->upstream->segment_group << 16 | dev->upstream->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_id;
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:%02x.%01x",
172 dev->upstream->segment_group,
173 dev->upstream->secondary,
174 PCI_SLOT(dev->path.pci.devfn),
175 PCI_FUNC(dev->path.pci.devfn));
176 break;
177 case DEVICE_PATH_PNP:
178 snprintf(buffer, sizeof(buffer), "PNP: %04x.%01x",
179 dev->path.pnp.port, dev->path.pnp.device);
180 break;
181 case DEVICE_PATH_I2C:
182 snprintf(buffer, sizeof(buffer), "I2C: %02x:%02x",
183 dev->upstream->secondary,
184 dev->path.i2c.device);
185 break;
186 case DEVICE_PATH_APIC:
187 snprintf(buffer, sizeof(buffer), "APIC: %02x",
188 dev->path.apic.apic_id);
189 break;
190 case DEVICE_PATH_IOAPIC:
191 snprintf(buffer, sizeof(buffer), "IOAPIC: %02x",
192 dev->path.ioapic.ioapic_id);
193 break;
194 case DEVICE_PATH_DOMAIN:
195 snprintf(buffer, sizeof(buffer), "DOMAIN: %08x",
196 dev->path.domain.domain_id);
197 break;
198 case DEVICE_PATH_CPU_CLUSTER:
199 snprintf(buffer, sizeof(buffer), "CPU_CLUSTER: %01x",
200 dev->path.cpu_cluster.cluster);
201 break;
202 case DEVICE_PATH_CPU:
203 snprintf(buffer, sizeof(buffer),
204 "CPU: %02x", dev->path.cpu.id);
205 break;
206 case DEVICE_PATH_CPU_BUS:
207 snprintf(buffer, sizeof(buffer),
208 "CPU_BUS: %02x", dev->path.cpu_bus.id);
209 break;
210 case DEVICE_PATH_GENERIC:
211 snprintf(buffer, sizeof(buffer),
212 "GENERIC: %d.%d", dev->path.generic.id,
213 dev->path.generic.subid);
214 break;
215 case DEVICE_PATH_SPI:
216 snprintf(buffer, sizeof(buffer), "SPI: %02x",
217 dev->path.spi.cs);
218 break;
219 case DEVICE_PATH_USB:
220 snprintf(buffer, sizeof(buffer), "USB%u port %u",
221 dev->path.usb.port_type, dev->path.usb.port_id);
222 break;
223 case DEVICE_PATH_MMIO:
224 snprintf(buffer, sizeof(buffer), "MMIO: %08lx",
225 dev->path.mmio.addr);
226 break;
227 case DEVICE_PATH_GPIO:
228 snprintf(buffer, sizeof(buffer), "GPIO: %d", dev->path.gpio.id);
229 break;
230 case DEVICE_PATH_MDIO:
231 snprintf(buffer, sizeof(buffer), "MDIO: %02x", dev->path.mdio.addr);
232 break;
233 case DEVICE_PATH_GICC_V3:
234 snprintf(buffer, sizeof(buffer), "GICV3: %02x", dev->path.gicc_v3.mpidr);
235 break;
236 default:
237 printk(BIOS_ERR, "Unknown device path type: %d\n",
238 dev->path.type);
239 break;
242 return buffer;
245 const char *dev_name(const struct device *dev)
247 if (dev->name)
248 return dev->name;
249 else if (dev->chip_ops && dev->chip_ops->name)
250 return dev->chip_ops->name;
251 else
252 return "unknown";
255 /* Returns the domain for the given device */
256 const struct device *dev_get_domain(const struct device *dev)
258 /* Walk up the tree up to the domain */
259 while (dev && dev->upstream && !is_root_device(dev)) {
260 if (dev->path.type == DEVICE_PATH_DOMAIN)
261 return dev;
262 dev = dev->upstream->dev;
265 return NULL;
268 unsigned int dev_get_domain_id(const struct device *dev)
270 const struct device *domain_dev = dev_get_domain(dev);
272 assert(domain_dev);
274 if (!domain_dev) {
275 printk(BIOS_ERR, "%s: doesn't have a domain device\n", dev_path(dev));
276 return 0;
279 return domain_dev->path.domain.domain_id;
282 bool is_domain0(const struct device *dev)
284 return dev && dev->path.type == DEVICE_PATH_DOMAIN && dev->path.domain.domain_id == 0;
287 bool is_dev_on_domain0(const struct device *dev)
289 return is_domain0(dev_get_domain(dev));
293 * Allocate 64 more resources to the free list.
295 * @return TODO.
297 static int allocate_more_resources(void)
299 int i;
300 struct resource *new_res_list;
302 new_res_list = malloc(64 * sizeof(*new_res_list));
304 if (new_res_list == NULL)
305 return 0;
307 memset(new_res_list, 0, 64 * sizeof(*new_res_list));
309 for (i = 0; i < 64 - 1; i++)
310 new_res_list[i].next = &new_res_list[i+1];
312 free_resources = new_res_list;
313 return 1;
317 * Remove resource res from the device's list and add it to the free list.
319 * @param dev TODO
320 * @param res TODO
321 * @param prev TODO
322 * @return TODO.
324 static void free_resource(struct device *dev, struct resource *res,
325 struct resource *prev)
327 if (prev)
328 prev->next = res->next;
329 else
330 dev->resource_list = res->next;
332 res->next = free_resources;
333 free_resources = res;
337 * See if we have unused but allocated resource structures.
339 * If so remove the allocation.
341 * @param dev The device to find the resource on.
343 void compact_resources(struct device *dev)
345 struct resource *res, *next, *prev = NULL;
347 /* Move all of the free resources to the end */
348 for (res = dev->resource_list; res; res = next) {
349 next = res->next;
350 if (!res->flags)
351 free_resource(dev, res, prev);
352 else
353 prev = res;
358 * See if a resource structure already exists for a given index.
360 * @param dev The device to find the resource on.
361 * @param index The index of the resource on the device.
362 * @return The resource, if it already exists.
364 struct resource *probe_resource(const struct device *dev, unsigned int index)
366 struct resource *res;
368 /* See if there is a resource with the appropriate index */
369 for (res = dev->resource_list; res; res = res->next) {
370 if (res->index == index)
371 break;
374 return res;
378 * See if a resource structure already exists for a given index and if not
379 * allocate one.
381 * Then initialize the resource to default values.
383 * @param dev The device to find the resource on.
384 * @param index The index of the resource on the device.
385 * @return TODO.
387 struct resource *new_resource(struct device *dev, unsigned int index)
389 struct resource *resource, *tail;
391 /* First move all of the free resources to the end. */
392 compact_resources(dev);
394 /* See if there is a resource with the appropriate index. */
395 resource = probe_resource(dev, index);
396 if (!resource) {
397 if (free_resources == NULL && !allocate_more_resources())
398 die("Couldn't allocate more resources.");
400 resource = free_resources;
401 free_resources = free_resources->next;
402 memset(resource, 0, sizeof(*resource));
403 resource->next = NULL;
404 tail = dev->resource_list;
405 if (tail) {
406 while (tail->next)
407 tail = tail->next;
408 tail->next = resource;
409 } else {
410 dev->resource_list = resource;
414 /* Initialize the resource values. */
415 if (!(resource->flags & IORESOURCE_FIXED)) {
416 resource->flags = 0;
417 resource->base = 0;
419 resource->size = 0;
420 resource->limit = 0;
421 resource->index = index;
422 resource->align = 0;
423 resource->gran = 0;
425 return resource;
429 * Return an existing resource structure for a given index.
431 * @param dev The device to find the resource on.
432 * @param index The index of the resource on the device.
433 * return TODO.
435 struct resource *find_resource(const struct device *dev, unsigned int index)
437 struct resource *resource;
439 /* See if there is a resource with the appropriate index. */
440 resource = probe_resource(dev, index);
441 if (!resource)
442 die("%s missing resource: %02x\n", dev_path(dev), index);
443 return resource;
447 * Round a number up to the next multiple of gran.
449 * @param val The starting value.
450 * @param gran Granularity we are aligning the number to.
451 * @return The aligned value.
453 static resource_t align_up(resource_t val, unsigned long gran)
455 resource_t mask;
456 mask = (1ULL << gran) - 1ULL;
457 val += mask;
458 val &= ~mask;
459 return val;
463 * Round a number up to the previous multiple of gran.
465 * @param val The starting value.
466 * @param gran Granularity we are aligning the number to.
467 * @return The aligned value.
469 static resource_t align_down(resource_t val, unsigned long gran)
471 resource_t mask;
472 mask = (1ULL << gran) - 1ULL;
473 val &= ~mask;
474 return val;
478 * Compute the maximum address that is part of a resource.
480 * @param resource The resource whose limit is desired.
481 * @return The end.
483 resource_t resource_end(const struct resource *resource)
485 resource_t base, end;
487 /* Get the base address. */
488 base = resource->base;
491 * For a non bridge resource granularity and alignment are the same.
492 * For a bridge resource align is the largest needed alignment below
493 * the bridge. While the granularity is simply how many low bits of
494 * the address cannot be set.
497 /* Get the end (rounded up). */
498 end = base + align_up(resource->size, resource->gran) - 1;
500 return end;
504 * Compute the maximum legal value for resource->base.
506 * @param resource The resource whose maximum is desired.
507 * @return The maximum.
509 resource_t resource_max(const struct resource *resource)
511 resource_t max;
513 max = align_down(resource->limit - resource->size + 1, resource->align);
515 return max;
519 * Return the resource type of a resource.
521 * @param resource The resource type to decode.
522 * @return TODO.
524 const char *resource_type(const struct resource *resource)
526 static char buffer[RESOURCE_TYPE_MAX];
527 snprintf(buffer, sizeof(buffer), "%s%s%s%s",
528 ((resource->flags & IORESOURCE_READONLY) ? "ro" : ""),
529 ((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""),
530 ((resource->flags == 0) ? "unused" :
531 (resource->flags & IORESOURCE_IO) ? "io" :
532 (resource->flags & IORESOURCE_DRQ) ? "drq" :
533 (resource->flags & IORESOURCE_IRQ) ? "irq" :
534 (resource->flags & IORESOURCE_MEM) ? "mem" : "??????"),
535 ((resource->flags & IORESOURCE_PCI64) ? "64" : ""));
536 return buffer;
540 * Print the resource that was just stored.
542 * @param dev The device the stored resource lives on.
543 * @param resource The resource that was just stored.
544 * @param comment TODO
546 void report_resource_stored(struct device *dev, const struct resource *resource,
547 const char *comment)
549 char buf[10];
550 unsigned long long base, end;
552 if (!(resource->flags & IORESOURCE_STORED))
553 return;
555 base = resource->base;
556 end = resource_end(resource);
557 buf[0] = '\0';
559 if (dev->downstream && (resource->flags & IORESOURCE_PCI_BRIDGE)) {
560 snprintf(buf, sizeof(buf),
561 "seg %02x bus %02x ", dev->downstream->segment_group,
562 dev->downstream->secondary);
564 printk(BIOS_DEBUG, "%s %02lx <- [0x%016llx - 0x%016llx] size 0x%08llx "
565 "gran 0x%02x %s%s%s\n", dev_path(dev), resource->index,
566 base, end, resource->size, resource->gran, buf,
567 resource_type(resource), comment);
570 void search_bus_resources(struct bus *bus, unsigned long type_mask,
571 unsigned long type, resource_search_t search,
572 void *gp)
574 struct device *curdev;
576 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
577 struct resource *res;
579 /* Ignore disabled devices. */
580 if (!curdev->enabled)
581 continue;
583 for (res = curdev->resource_list; res; res = res->next) {
584 /* If it isn't the right kind of resource ignore it. */
585 if ((res->flags & type_mask) != type)
586 continue;
588 /* If it is a subtractive resource recurse. */
589 if (res->flags & IORESOURCE_SUBTRACTIVE) {
590 if (curdev->downstream)
591 search_bus_resources(curdev->downstream, type_mask, type,
592 search, gp);
593 continue;
595 search(gp, curdev, res);
600 void search_global_resources(unsigned long type_mask, unsigned long type,
601 resource_search_t search, void *gp)
603 struct device *curdev;
605 for (curdev = all_devices; curdev; curdev = curdev->next) {
606 struct resource *res;
608 /* Ignore disabled devices. */
609 if (!curdev->enabled)
610 continue;
612 for (res = curdev->resource_list; res; res = res->next) {
613 /* If it isn't the right kind of resource ignore it. */
614 if ((res->flags & type_mask) != type)
615 continue;
617 /* If it is a subtractive resource ignore it. */
618 if (res->flags & IORESOURCE_SUBTRACTIVE)
619 continue;
621 /* If the resource is not assigned ignore it. */
622 if (!(res->flags & IORESOURCE_ASSIGNED))
623 continue;
625 search(gp, curdev, res);
630 void dev_set_enabled(struct device *dev, int enable)
632 if (dev->enabled == enable)
633 return;
635 dev->enabled = enable;
636 if (dev->ops && dev->ops->enable)
637 dev->ops->enable(dev);
638 else if (dev->chip_ops && dev->chip_ops->enable_dev)
639 dev->chip_ops->enable_dev(dev);
642 void disable_children(struct bus *bus)
644 struct device *child;
646 for (child = bus->children; child; child = child->sibling) {
647 if (child->downstream)
648 disable_children(child->downstream);
649 dev_set_enabled(child, 0);
654 * Returns true if the device is an enabled bridge that has at least
655 * one enabled device on its secondary bus that is not of type NONE.
657 bool dev_is_active_bridge(struct device *dev)
659 struct device *child;
661 if (!dev || !dev->enabled)
662 return 0;
664 if (!dev->downstream || !dev->downstream->children)
665 return 0;
667 for (child = dev->downstream->children; child; child = child->sibling) {
668 if (child->path.type == DEVICE_PATH_NONE)
669 continue;
670 if (child->enabled)
671 return 1;
674 return 0;
677 static void resource_tree(const struct device *root, int debug_level, int depth)
679 int i = 0;
680 struct device *child;
681 struct resource *res;
682 char indent[30]; /* If your tree has more levels, it's wrong. */
684 for (i = 0; i < depth + 1 && i < 29; i++)
685 indent[i] = ' ';
686 indent[i] = '\0';
688 printk(BIOS_DEBUG, "%s%s", indent, dev_path(root));
689 if (root->downstream && root->downstream->children)
690 printk(BIOS_DEBUG, " child on link 0 %s",
691 dev_path(root->downstream->children));
692 printk(BIOS_DEBUG, "\n");
694 for (res = root->resource_list; res; res = res->next) {
695 printk(debug_level, "%s%s resource base %llx size %llx "
696 "align %d gran %d limit %llx flags %lx index %lx\n",
697 indent, dev_path(root), res->base, res->size,
698 res->align, res->gran, res->limit, res->flags,
699 res->index);
702 if (!root->downstream)
703 return;
705 for (child = root->downstream->children; child; child = child->sibling)
706 resource_tree(child, debug_level, depth + 1);
709 void print_resource_tree(const struct device *root, int debug_level,
710 const char *msg)
712 /* Bail if root is null. */
713 if (!root) {
714 printk(debug_level, "%s passed NULL for root!\n", __func__);
715 return;
718 /* Bail if not printing to screen. */
719 if (!printk(debug_level, "Show resources in subtree (%s)...%s\n",
720 dev_path(root), msg))
721 return;
723 resource_tree(root, debug_level, 0);
726 void show_devs_tree(const struct device *dev, int debug_level, int depth)
728 char depth_str[20];
729 int i;
730 struct device *sibling;
732 for (i = 0; i < depth; i++)
733 depth_str[i] = ' ';
734 depth_str[i] = '\0';
736 printk(debug_level, "%s%s: enabled %d\n",
737 depth_str, dev_path(dev), dev->enabled);
739 if (!dev->downstream)
740 return;
742 for (sibling = dev->downstream->children; sibling; sibling = sibling->sibling)
743 show_devs_tree(sibling, debug_level, depth + 1);
746 void show_all_devs_tree(int debug_level, const char *msg)
748 /* Bail if not printing to screen. */
749 if (!printk(debug_level, "Show all devs in tree form... %s\n", msg))
750 return;
751 show_devs_tree(all_devices, debug_level, 0);
754 void show_devs_subtree(struct device *root, int debug_level, const char *msg)
756 /* Bail if not printing to screen. */
757 if (!printk(debug_level, "Show all devs in subtree %s... %s\n",
758 dev_path(root), msg))
759 return;
760 printk(debug_level, "%s\n", msg);
761 show_devs_tree(root, debug_level, 0);
764 void show_all_devs(int debug_level, const char *msg)
766 struct device *dev;
768 /* Bail if not printing to screen. */
769 if (!printk(debug_level, "Show all devs... %s\n", msg))
770 return;
771 for (dev = all_devices; dev; dev = dev->next) {
772 printk(debug_level, "%s: enabled %d\n",
773 dev_path(dev), dev->enabled);
777 void show_one_resource(int debug_level, struct device *dev,
778 struct resource *resource, const char *comment)
780 char buf[10];
781 unsigned long long base, end;
782 base = resource->base;
783 end = resource_end(resource);
784 buf[0] = '\0';
786 printk(debug_level, "%s %02lx <- [0x%016llx - 0x%016llx] "
787 "size 0x%08llx gran 0x%02x %s%s%s\n", dev_path(dev),
788 resource->index, base, end, resource->size, resource->gran,
789 buf, resource_type(resource), comment);
792 void show_all_devs_resources(int debug_level, const char *msg)
794 struct device *dev;
796 if (!printk(debug_level, "Show all devs with resources... %s\n", msg))
797 return;
799 for (dev = all_devices; dev; dev = dev->next) {
800 struct resource *res;
801 printk(debug_level, "%s: enabled %d\n",
802 dev_path(dev), dev->enabled);
803 for (res = dev->resource_list; res; res = res->next)
804 show_one_resource(debug_level, dev, res, "");
808 const struct resource *resource_range_idx(struct device *dev, unsigned long index,
809 uint64_t base, uint64_t size, unsigned long flags)
811 struct resource *resource;
812 if (!size)
813 return NULL;
815 resource = new_resource(dev, index);
816 resource->base = base;
818 if (flags & IORESOURCE_FIXED)
819 resource->size = size;
820 if (flags & IORESOURCE_BRIDGE)
821 resource->limit = base + size - 1;
823 resource->flags = IORESOURCE_ASSIGNED;
824 resource->flags |= flags;
826 printk(BIOS_SPEW, "dev: %s, index: 0x%lx, base: 0x%llx, size: 0x%llx\n",
827 dev_path(dev), resource->index, resource->base, resource->size);
829 return resource;
832 const struct resource *lower_ram_end(struct device *dev, unsigned long index, uint64_t end)
834 return ram_from_to(dev, index, 0, end);
837 const struct resource *upper_ram_end(struct device *dev, unsigned long index, uint64_t end)
839 if (end <= 4ull * GiB)
840 return NULL;
842 printk(BIOS_INFO, "Available memory above 4GB: %lluM\n", (end - 4ull * GiB) / MiB);
844 return ram_from_to(dev, index, 4ull * GiB, end);
847 void mmconf_resource(struct device *dev, unsigned long index)
849 struct resource *resource = new_resource(dev, index);
850 resource->base = CONFIG_ECAM_MMCONF_BASE_ADDRESS;
851 resource->size = CONFIG_ECAM_MMCONF_LENGTH;
852 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
853 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
855 printk(BIOS_DEBUG, "Adding PCIe enhanced config space BAR 0x%08lx-0x%08lx.\n",
856 (unsigned long)(resource->base),
857 (unsigned long)(resource->base + resource->size));
860 void tolm_test(void *gp, struct device *dev, struct resource *new)
862 struct resource **best_p = gp;
863 struct resource *best;
865 best = *best_p;
868 * If resource is not allocated any space i.e. size is zero,
869 * then do not consider this resource in tolm calculations.
871 if (new->size == 0)
872 return;
874 if (!best || (best->base > new->base))
875 best = new;
877 *best_p = best;
880 u32 find_pci_tolm(struct bus *bus)
882 struct resource *min = NULL;
883 u32 tolm;
884 unsigned long mask_match = IORESOURCE_MEM | IORESOURCE_ASSIGNED;
886 search_bus_resources(bus, mask_match, mask_match, tolm_test, &min);
888 tolm = 0xffffffffUL;
890 if (min && tolm > min->base)
891 tolm = min->base;
893 return tolm;
896 /* Count of enabled CPUs */
897 int dev_count_cpu(void)
899 struct device *cpu;
900 int count = 0;
902 for (cpu = all_devices; cpu; cpu = cpu->next) {
903 if (!is_enabled_cpu(cpu))
904 continue;
905 count++;
908 return count;
911 /* Get device path name */
912 const char *dev_path_name(enum device_path_type type)
914 static const char *const type_names[] = DEVICE_PATH_NAMES;
915 const char *type_name = "Unknown";
917 /* Translate the type value into a string */
918 if (type < ARRAY_SIZE(type_names))
919 type_name = type_names[type];
920 return type_name;
923 bool dev_path_hotplug(const struct device *dev)
925 for (dev = dev->upstream->dev; dev != dev->upstream->dev; dev = dev->upstream->dev) {
926 if (dev->hotplug_port)
927 return true;
929 return false;
932 void log_resource(const char *type, const struct device *dev, const struct resource *res,
933 const char *srcfile, const int line)
935 printk(BIOS_SPEW, "%s:%d res: %s, dev: %s, index: 0x%lx, base: 0x%llx, "
936 "end: 0x%llx, size_kb: 0x%llx\n",
937 srcfile, line, type, dev_path(dev), res->index, res->base,
938 resource_end(res), res->size / KiB);
941 bool is_cpu(const struct device *cpu)
943 return cpu->path.type == DEVICE_PATH_APIC &&
944 cpu->upstream->dev->path.type == DEVICE_PATH_CPU_CLUSTER;
947 bool is_enabled_cpu(const struct device *cpu)
949 return is_cpu(cpu) && cpu->enabled;
952 bool is_pci(const struct device *pci)
954 return pci->path.type == DEVICE_PATH_PCI;
957 bool is_enabled_pci(const struct device *pci)
959 return is_pci(pci) && pci->enabled;
962 bool is_pci_dev_on_bus(const struct device *pci, unsigned int bus)
964 return is_pci(pci) && pci->upstream->segment_group == 0
965 && pci->upstream->secondary == bus;
968 bool is_pci_bridge(const struct device *pci)
970 return is_pci(pci) && ((pci->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE);