2 * This file is part of the coreboot project.
4 * Copyright (C) 2003-2004 Linux Networx
5 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
6 * Copyright (C) 2003 Greg Watson <jarrah@users.sourceforge.net>
7 * Copyright (C) 2004 Li-Ta Lo <ollie@lanl.gov>
8 * Copyright (C) 2005-2006 Tyan
9 * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <console/console.h>
26 #include <device/device.h>
27 #include <device/path.h>
28 #include <device/pci.h>
29 #include <device/resource.h>
33 * See if a device structure exists for path.
35 * @param parent The bus to find the device on.
36 * @param path The relative path from the bus to the appropriate device.
37 * @return Pointer to a device structure for the device on bus at path
38 * or 0/NULL if no device is found.
40 device_t find_dev_path(struct bus *parent, struct device_path *path)
43 for (child = parent->children; child; child = child->sibling) {
44 if (path_eq(path, &child->path))
51 * See if a device structure already exists and if not allocate it.
53 * @param parent The bus to find the device on.
54 * @param path The relative path from the bus to the appropriate device.
55 * @return Pointer to a device structure for the device on bus at path.
57 device_t alloc_find_dev(struct bus *parent, struct device_path *path)
60 child = find_dev_path(parent, path);
62 child = alloc_dev(parent, path);
67 * Given a PCI bus and a devfn number, find the device structure.
69 * @param bus The bus number.
70 * @param devfn A device/function number.
71 * @return Pointer to the device structure (if found), 0 otherwise.
73 struct device *dev_find_slot(unsigned int bus, unsigned int devfn)
75 struct device *dev, *result;
78 for (dev = all_devices; dev; dev = dev->next) {
79 if ((dev->path.type == DEVICE_PATH_PCI) &&
80 (dev->bus->secondary == bus) &&
81 (dev->path.pci.devfn == devfn)) {
90 * Given an SMBus bus and a device number, find the device structure.
92 * @param bus The bus number.
93 * @param addr A device number.
94 * @return Pointer to the device structure (if found), 0 otherwise.
96 struct device *dev_find_slot_on_smbus(unsigned int bus, unsigned int addr)
98 struct device *dev, *result;
101 for (dev = all_devices; dev; dev = dev->next) {
102 if ((dev->path.type == DEVICE_PATH_I2C) &&
103 (dev->bus->secondary == bus) &&
104 (dev->path.i2c.device == addr)) {
113 * Given a Local APIC ID, find the device structure.
115 * @param apic_id The Local APIC ID number.
116 * @return Pointer to the device structure (if found), 0 otherwise.
118 device_t dev_find_lapic(unsigned apic_id)
120 device_t dev, result = NULL;
122 for (dev = all_devices; dev; dev = dev->next) {
123 if (dev->path.type == DEVICE_PATH_APIC &&
124 dev->path.apic.apic_id == apic_id) {
133 * Find a device of a given vendor and type.
135 * @param vendor A PCI vendor ID (e.g. 0x8086 for Intel).
136 * @param device A PCI device ID.
137 * @param from Pointer to the device structure, used as a starting point in
138 * the linked list of all_devices, which can be 0 to start at the
139 * head of the list (i.e. all_devices).
140 * @return Pointer to the device struct.
142 struct device *dev_find_device(u16 vendor, u16 device, struct device *from)
149 while (from && (from->vendor != vendor || from->device != device))
156 * Find a device of a given class.
158 * @param class Class of the device.
159 * @param from Pointer to the device structure, used as a starting point in
160 * the linked list of all_devices, which can be 0 to start at the
161 * head of the list (i.e. all_devices).
162 * @return Pointer to the device struct.
164 struct device *dev_find_class(unsigned int class, struct device *from)
171 while (from && (from->class & 0xffffff00) != class)
178 * Warning: This function uses a static buffer. Don't call it more than once
179 * from the same print statement!
181 const char *dev_path(device_t dev)
183 static char buffer[DEVICE_PATH_MAX];
187 memcpy(buffer, "<null>", 7);
189 switch(dev->path.type) {
190 case DEVICE_PATH_ROOT:
191 memcpy(buffer, "Root Device", 12);
193 case DEVICE_PATH_PCI:
194 #if CONFIG_PCI_BUS_SEGN_BITS
195 sprintf(buffer, "PCI: %04x:%02x:%02x.%01x",
196 dev->bus->secondary >> 8,
197 dev->bus->secondary & 0xff,
198 PCI_SLOT(dev->path.pci.devfn),
199 PCI_FUNC(dev->path.pci.devfn));
201 sprintf(buffer, "PCI: %02x:%02x.%01x",
203 PCI_SLOT(dev->path.pci.devfn),
204 PCI_FUNC(dev->path.pci.devfn));
207 case DEVICE_PATH_PNP:
208 sprintf(buffer, "PNP: %04x.%01x",
209 dev->path.pnp.port, dev->path.pnp.device);
211 case DEVICE_PATH_I2C:
212 sprintf(buffer, "I2C: %02x:%02x",
214 dev->path.i2c.device);
216 case DEVICE_PATH_APIC:
217 sprintf(buffer, "APIC: %02x",
218 dev->path.apic.apic_id);
220 case DEVICE_PATH_PCI_DOMAIN:
221 sprintf(buffer, "PCI_DOMAIN: %04x",
222 dev->path.pci_domain.domain);
224 case DEVICE_PATH_APIC_CLUSTER:
225 sprintf(buffer, "APIC_CLUSTER: %01x",
226 dev->path.apic_cluster.cluster);
228 case DEVICE_PATH_CPU:
229 sprintf(buffer, "CPU: %02x", dev->path.cpu.id);
231 case DEVICE_PATH_CPU_BUS:
232 sprintf(buffer, "CPU_BUS: %02x", dev->path.cpu_bus.id);
235 printk(BIOS_ERR, "Unknown device path type: %d\n",
243 const char *bus_path(struct bus *bus)
245 static char buffer[BUS_PATH_MAX];
246 sprintf(buffer, "%s,%d", dev_path(bus->dev), bus->link_num);
250 int path_eq(struct device_path *path1, struct device_path *path2)
254 if (path1->type != path2->type)
257 switch (path1->type) {
258 case DEVICE_PATH_NONE:
260 case DEVICE_PATH_ROOT:
263 case DEVICE_PATH_PCI:
264 equal = (path1->pci.devfn == path2->pci.devfn);
266 case DEVICE_PATH_PNP:
267 equal = (path1->pnp.port == path2->pnp.port) &&
268 (path1->pnp.device == path2->pnp.device);
270 case DEVICE_PATH_I2C:
271 equal = (path1->i2c.device == path2->i2c.device);
273 case DEVICE_PATH_APIC:
274 equal = (path1->apic.apic_id == path2->apic.apic_id);
276 case DEVICE_PATH_PCI_DOMAIN:
277 equal = (path1->pci_domain.domain == path2->pci_domain.domain);
279 case DEVICE_PATH_APIC_CLUSTER:
280 equal = (path1->apic_cluster.cluster
281 == path2->apic_cluster.cluster);
283 case DEVICE_PATH_CPU:
284 equal = (path1->cpu.id == path2->cpu.id);
286 case DEVICE_PATH_CPU_BUS:
287 equal = (path1->cpu_bus.id == path2->cpu_bus.id);
290 printk(BIOS_ERR, "Uknown device type: %d\n", path1->type);
298 * Allocate 64 more resources to the free list.
302 static int allocate_more_resources(void)
305 struct resource *new_res_list;
307 new_res_list = malloc(64 * sizeof(*new_res_list));
309 if (new_res_list == NULL)
312 memset(new_res_list, 0, 64 * sizeof(*new_res_list));
314 for (i = 0; i < 64 - 1; i++)
315 new_res_list[i].next = &new_res_list[i+1];
317 free_resources = new_res_list;
322 * Remove resource res from the device's list and add it to the free list.
329 static void free_resource(device_t dev, struct resource *res,
330 struct resource *prev)
333 prev->next = res->next;
335 dev->resource_list = res->next;
337 res->next = free_resources;
338 free_resources = res;
342 * See if we have unused but allocated resource structures.
344 * If so remove the allocation.
346 * @param dev The device to find the resource on.
348 void compact_resources(device_t dev)
350 struct resource *res, *next, *prev = NULL;
352 /* Move all of the free resources to the end */
353 for (res = dev->resource_list; res; res = next) {
356 free_resource(dev, res, prev);
363 * See if a resource structure already exists for a given index.
365 * @param dev The device to find the resource on.
366 * @param index The index of the resource on the device.
367 * @return The resource, if it already exists.
369 struct resource *probe_resource(device_t dev, unsigned index)
371 struct resource *res;
373 /* See if there is a resource with the appropriate index */
374 for (res = dev->resource_list; res; res = res->next) {
375 if (res->index == index)
383 * See if a resource structure already exists for a given index and if not
386 * Then initialize the initialize the resource to default values.
388 * @param dev The device to find the resource on.
389 * @param index The index of the resource on the device.
392 struct resource *new_resource(device_t dev, unsigned index)
394 struct resource *resource, *tail;
396 /* First move all of the free resources to the end. */
397 compact_resources(dev);
399 /* See if there is a resource with the appropriate index. */
400 resource = probe_resource(dev, index);
402 if (free_resources == NULL && !allocate_more_resources())
403 die("Couldn't allocate more resources.");
405 resource = free_resources;
406 free_resources = free_resources->next;
407 memset(resource, 0, sizeof(*resource));
408 resource->next = NULL;
409 tail = dev->resource_list;
411 while (tail->next) tail = tail->next;
412 tail->next = resource;
414 dev->resource_list = resource;
418 /* Initialize the resource values. */
419 if (!(resource->flags & IORESOURCE_FIXED)) {
425 resource->index = index;
433 * Return an existing resource structure for a given index.
435 * @param dev The device to find the resource on.
436 * @param index The index of the resource on the device.
439 struct resource *find_resource(device_t dev, unsigned index)
441 struct resource *resource;
443 /* See if there is a resource with the appropriate index. */
444 resource = probe_resource(dev, index);
446 printk(BIOS_EMERG, "%s missing resource: %02x\n",
447 dev_path(dev), index);
454 * Round a number up to the next multiple of gran.
456 * @param val The starting value.
457 * @param gran Granularity we are aligning the number to.
458 * @return The aligned value.
460 static resource_t align_up(resource_t val, unsigned long gran)
463 mask = (1ULL << gran) - 1ULL;
470 * Round a number up to the previous multiple of gran.
472 * @param val The starting value.
473 * @param gran Granularity we are aligning the number to.
474 * @return The aligned value.
476 static resource_t align_down(resource_t val, unsigned long gran)
479 mask = (1ULL << gran) - 1ULL;
485 * Compute the maximum address that is part of a resource.
487 * @param resource The resource whose limit is desired.
490 resource_t resource_end(struct resource *resource)
492 resource_t base, end;
494 /* Get the base address. */
495 base = resource->base;
498 * For a non bridge resource granularity and alignment are the same.
499 * For a bridge resource align is the largest needed alignment below
500 * the bridge. While the granularity is simply how many low bits of
501 * the address cannot be set.
504 /* Get the end (rounded up). */
505 end = base + align_up(resource->size, resource->gran) - 1;
511 * Compute the maximum legal value for resource->base.
513 * @param resource The resource whose maximum is desired.
514 * @return The maximum.
516 resource_t resource_max(struct resource *resource)
520 max = align_down(resource->limit - resource->size + 1, resource->align);
526 * Return the resource type of a resource.
528 * @param resource The resource type to decode.
531 const char *resource_type(struct resource *resource)
533 static char buffer[RESOURCE_TYPE_MAX];
534 sprintf(buffer, "%s%s%s%s",
535 ((resource->flags & IORESOURCE_READONLY) ? "ro" : ""),
536 ((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""),
537 ((resource->flags == 0) ? "unused" :
538 (resource->flags & IORESOURCE_IO) ? "io" :
539 (resource->flags & IORESOURCE_DRQ) ? "drq" :
540 (resource->flags & IORESOURCE_IRQ) ? "irq" :
541 (resource->flags & IORESOURCE_MEM) ? "mem" : "??????"),
542 ((resource->flags & IORESOURCE_PCI64) ? "64" : ""));
547 * Print the resource that was just stored.
549 * @param dev The device the stored resorce lives on.
550 * @param resource The resource that was just stored.
551 * @param comment TODO
553 void report_resource_stored(device_t dev, struct resource *resource,
557 unsigned long long base, end;
559 if (!(resource->flags & IORESOURCE_STORED))
562 base = resource->base;
563 end = resource_end(resource);
566 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
567 #if CONFIG_PCI_BUS_SEGN_BITS
568 sprintf(buf, "bus %04x:%02x ", dev->bus->secondary >> 8,
569 dev->link_list->secondary & 0xff);
571 sprintf(buf, "bus %02x ", dev->link_list->secondary);
574 printk(BIOS_DEBUG, "%s %02lx <- [0x%010llx - 0x%010llx] size 0x%08llx "
575 "gran 0x%02x %s%s%s\n", dev_path(dev), resource->index,
576 base, end, resource->size, resource->gran, buf,
577 resource_type(resource), comment);
580 void search_bus_resources(struct bus *bus, unsigned long type_mask,
581 unsigned long type, resource_search_t search,
584 struct device *curdev;
586 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
587 struct resource *res;
589 /* Ignore disabled devices. */
590 if (!curdev->enabled)
593 for (res = curdev->resource_list; res; res = res->next) {
594 /* If it isn't the right kind of resource ignore it. */
595 if ((res->flags & type_mask) != type)
598 /* If it is a subtractive resource recurse. */
599 if (res->flags & IORESOURCE_SUBTRACTIVE) {
601 for (subbus = curdev->link_list; subbus;
602 subbus = subbus->next)
604 == IOINDEX_SUBTRACTIVE_LINK(res->index))
606 if (!subbus) /* Why can subbus be NULL? */
608 search_bus_resources(subbus, type_mask, type,
612 search(gp, curdev, res);
617 void search_global_resources(unsigned long type_mask, unsigned long type,
618 resource_search_t search, void *gp)
620 struct device *curdev;
622 for (curdev = all_devices; curdev; curdev = curdev->next) {
623 struct resource *res;
625 /* Ignore disabled devices. */
626 if (!curdev->enabled)
629 for (res = curdev->resource_list; res; res = res->next) {
630 /* If it isn't the right kind of resource ignore it. */
631 if ((res->flags & type_mask) != type)
634 /* If it is a subtractive resource ignore it. */
635 if (res->flags & IORESOURCE_SUBTRACTIVE)
638 search(gp, curdev, res);
643 void dev_set_enabled(device_t dev, int enable)
645 if (dev->enabled == enable)
648 dev->enabled = enable;
649 if (dev->ops && dev->ops->enable) {
650 dev->ops->enable(dev);
651 } else if (dev->chip_ops && dev->chip_ops->enable_dev) {
652 dev->chip_ops->enable_dev(dev);
656 void disable_children(struct bus *bus)
660 for (child = bus->children; child; child = child->sibling) {
662 for (link = child->link_list; link; link = link->next)
663 disable_children(link);
664 dev_set_enabled(child, 0);
668 static void resource_tree(struct device *root, int debug_level, int depth)
671 struct device *child;
673 struct resource *res;
674 char indent[30]; /* If your tree has more levels, it's wrong. */
676 for (i = 0; i < depth + 1 && i < 29; i++)
680 do_printk(BIOS_DEBUG, "%s%s", indent, dev_path(root));
681 if (root->link_list && root->link_list->children)
682 do_printk(BIOS_DEBUG, " child on link 0 %s",
683 dev_path(root->link_list->children));
684 do_printk(BIOS_DEBUG, "\n");
686 for (res = root->resource_list; res; res = res->next) {
687 do_printk(debug_level, "%s%s resource base %llx size %llx "
688 "align %d gran %d limit %llx flags %lx index %lx\n",
689 indent, dev_path(root), res->base, res->size,
690 res->align, res->gran, res->limit, res->flags,
694 for (link = root->link_list; link; link = link->next) {
695 for (child = link->children; child; child = child->sibling)
696 resource_tree(child, debug_level, depth + 1);
700 void print_resource_tree(struct device *root, int debug_level, const char *msg)
702 /* Bail if root is null. */
704 do_printk(debug_level, "%s passed NULL for root!\n", __func__);
708 /* Bail if not printing to screen. */
709 if (!do_printk(debug_level, "Show resources in subtree (%s)...%s\n",
710 dev_path(root), msg))
713 resource_tree(root, debug_level, 0);
716 void show_devs_tree(struct device *dev, int debug_level, int depth, int linknum)
718 char depth_str[20] = "";
720 struct device *sibling;
723 for (i = 0; i < depth; i++)
727 do_printk(debug_level, "%s%s: enabled %d\n",
728 depth_str, dev_path(dev), dev->enabled);
730 for (link = dev->link_list; link; link = link->next) {
731 for (sibling = link->children; sibling;
732 sibling = sibling->sibling)
733 show_devs_tree(sibling, debug_level, depth + 1, i);
737 void show_all_devs_tree(int debug_level, const char *msg)
739 /* Bail if not printing to screen. */
740 if (!do_printk(debug_level, "Show all devs in tree form...%s\n", msg))
742 show_devs_tree(all_devices, debug_level, 0, -1);
745 void show_devs_subtree(struct device *root, int debug_level, const char *msg)
747 /* Bail if not printing to screen. */
748 if (!do_printk(debug_level, "Show all devs in subtree %s...%s\n",
749 dev_path(root), msg))
751 do_printk(debug_level, "%s\n", msg);
752 show_devs_tree(root, debug_level, 0, -1);
755 void show_all_devs(int debug_level, const char *msg)
759 /* Bail if not printing to screen. */
760 if (!do_printk(debug_level, "Show all devs...%s\n", msg))
762 for (dev = all_devices; dev; dev = dev->next) {
763 do_printk(debug_level, "%s: enabled %d\n",
764 dev_path(dev), dev->enabled);
768 void show_one_resource(int debug_level, struct device *dev,
769 struct resource *resource, const char *comment)
772 unsigned long long base, end;
773 base = resource->base;
774 end = resource_end(resource);
778 if (resource->flags & IORESOURCE_BRIDGE) {
779 #if CONFIG_PCI_BUS_SEGN_BITS
780 sprintf(buf, "bus %04x:%02x ", dev->bus->secondary >> 8,
781 dev->link[0].secondary & 0xff);
783 sprintf(buf, "bus %02x ", dev->link[0].secondary);
788 do_printk(debug_level, "%s %02lx <- [0x%010llx - 0x%010llx] "
789 "size 0x%08llx gran 0x%02x %s%s%s\n", dev_path(dev),
790 resource->index, base, end, resource->size, resource->gran,
791 buf, resource_type(resource), comment);
794 void show_all_devs_resources(int debug_level, const char* msg)
798 if (!do_printk(debug_level, "Show all devs with resources...%s\n", msg))
801 for (dev = all_devices; dev; dev = dev->next) {
802 struct resource *res;
803 do_printk(debug_level, "%s: enabled %d\n",
804 dev_path(dev), dev->enabled);
805 for (res = dev->resource_list; res; res = res->next)
806 show_one_resource(debug_level, dev, res, "");
810 void ram_resource(device_t dev, unsigned long index,
811 unsigned long basek, unsigned long sizek)
813 struct resource *resource;
818 resource = new_resource(dev, index);
819 resource->base = ((resource_t)basek) << 10;
820 resource->size = ((resource_t)sizek) << 10;
821 resource->flags = IORESOURCE_MEM | IORESOURCE_CACHEABLE | \
822 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
825 void tolm_test(void *gp, struct device *dev, struct resource *new)
827 struct resource **best_p = gp;
828 struct resource *best;
832 if (!best || (best->base > new->base))
838 u32 find_pci_tolm(struct bus *bus)
840 struct resource *min = NULL;
843 search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM,
848 if (min && tolm > min->base)
854 /* Count of enabled CPUs */
855 int dev_count_cpu(void)
860 for (cpu = all_devices; cpu; cpu = cpu->next) {
861 if ((cpu->path.type != DEVICE_PATH_APIC) ||
862 (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER))