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)) {
52 * See if a device structure already exists and if not allocate it.
54 * @param parent The bus to find the device on.
55 * @param path The relative path from the bus to the appropriate device.
56 * @return Pointer to a device structure for the device on bus at path.
58 device_t alloc_find_dev(struct bus *parent, struct device_path *path)
61 child = find_dev_path(parent, path);
63 child = alloc_dev(parent, path);
69 * Given a PCI bus and a devfn number, find the device structure.
71 * @param bus The bus number.
72 * @param devfn A device/function number.
73 * @return Pointer to the device structure (if found), 0 otherwise.
75 struct device *dev_find_slot(unsigned int bus, unsigned int devfn)
77 struct device *dev, *result;
80 for (dev = all_devices; dev; dev = dev->next) {
81 if ((dev->path.type == DEVICE_PATH_PCI) &&
82 (dev->bus->secondary == bus) &&
83 (dev->path.pci.devfn == devfn)) {
92 * Given an SMBus bus and a device number, find the device structure.
94 * @param bus The bus number.
95 * @param addr A device number.
96 * @return Pointer to the device structure (if found), 0 otherwise.
98 struct device *dev_find_slot_on_smbus(unsigned int bus, unsigned int addr)
100 struct device *dev, *result;
103 for (dev = all_devices; dev; dev = dev->next) {
104 if ((dev->path.type == DEVICE_PATH_I2C) &&
105 (dev->bus->secondary == bus) &&
106 (dev->path.i2c.device == addr)) {
115 * Find a device of a given vendor and type.
117 * @param vendor A PCI vendor ID (e.g. 0x8086 for Intel).
118 * @param device A PCI device ID.
119 * @param from Pointer to the device structure, used as a starting point
120 * in the linked list of all_devices, which can be 0 to start at the
121 * head of the list (i.e. all_devices).
122 * @return Pointer to the device struct.
124 struct device *dev_find_device(unsigned int vendor, unsigned int device,
131 while (from && (from->vendor != vendor || from->device != device)) {
138 * Find a device of a given class.
140 * @param class Class of the device.
141 * @param from Pointer to the device structure, used as a starting point
142 * in the linked list of all_devices, which can be 0 to start at the
143 * head of the list (i.e. all_devices).
144 * @return Pointer to the device struct.
146 struct device *dev_find_class(unsigned int class, struct device *from)
152 while (from && (from->class & 0xffffff00) != class)
158 * Warning: This function uses a static buffer. Don't call it more than once
159 * from the same print statement!
161 const char *dev_path(device_t dev)
163 static char buffer[DEVICE_PATH_MAX];
167 memcpy(buffer, "<null>", 7);
170 switch(dev->path.type) {
171 case DEVICE_PATH_ROOT:
172 memcpy(buffer, "Root Device", 12);
174 case DEVICE_PATH_PCI:
175 #if CONFIG_PCI_BUS_SEGN_BITS
176 sprintf(buffer, "PCI: %04x:%02x:%02x.%01x",
177 dev->bus->secondary>>8, dev->bus->secondary & 0xff,
178 PCI_SLOT(dev->path.pci.devfn), PCI_FUNC(dev->path.pci.devfn));
180 sprintf(buffer, "PCI: %02x:%02x.%01x",
182 PCI_SLOT(dev->path.pci.devfn), PCI_FUNC(dev->path.pci.devfn));
185 case DEVICE_PATH_PNP:
186 sprintf(buffer, "PNP: %04x.%01x",
187 dev->path.pnp.port, dev->path.pnp.device);
189 case DEVICE_PATH_I2C:
190 sprintf(buffer, "I2C: %02x:%02x",
192 dev->path.i2c.device);
194 case DEVICE_PATH_APIC:
195 sprintf(buffer, "APIC: %02x",
196 dev->path.apic.apic_id);
198 case DEVICE_PATH_PCI_DOMAIN:
199 sprintf(buffer, "PCI_DOMAIN: %04x",
200 dev->path.pci_domain.domain);
202 case DEVICE_PATH_APIC_CLUSTER:
203 sprintf(buffer, "APIC_CLUSTER: %01x",
204 dev->path.apic_cluster.cluster);
206 case DEVICE_PATH_CPU:
207 sprintf(buffer, "CPU: %02x", dev->path.cpu.id);
209 case DEVICE_PATH_CPU_BUS:
210 sprintf(buffer, "CPU_BUS: %02x", dev->path.cpu_bus.id);
213 printk(BIOS_ERR, "Unknown device path type: %d\n", dev->path.type);
220 const char *bus_path(struct bus *bus)
222 static char buffer[BUS_PATH_MAX];
223 sprintf(buffer, "%s,%d", dev_path(bus->dev), bus->link_num);
227 int path_eq(struct device_path *path1, struct device_path *path2)
230 if (path1->type == path2->type) {
231 switch(path1->type) {
232 case DEVICE_PATH_NONE:
234 case DEVICE_PATH_ROOT:
237 case DEVICE_PATH_PCI:
238 equal = (path1->pci.devfn == path2->pci.devfn);
240 case DEVICE_PATH_PNP:
241 equal = (path1->pnp.port == path2->pnp.port) &&
242 (path1->pnp.device == path2->pnp.device);
244 case DEVICE_PATH_I2C:
245 equal = (path1->i2c.device == path2->i2c.device);
247 case DEVICE_PATH_APIC:
248 equal = (path1->apic.apic_id == path2->apic.apic_id);
250 case DEVICE_PATH_PCI_DOMAIN:
251 equal = (path1->pci_domain.domain == path2->pci_domain.domain);
253 case DEVICE_PATH_APIC_CLUSTER:
254 equal = (path1->apic_cluster.cluster == path2->apic_cluster.cluster);
256 case DEVICE_PATH_CPU:
257 equal = (path1->cpu.id == path2->cpu.id);
259 case DEVICE_PATH_CPU_BUS:
260 equal = (path1->cpu_bus.id == path2->cpu_bus.id);
263 printk(BIOS_ERR, "Uknown device type: %d\n", path1->type);
271 * Allocate 64 more resources to the free list.
275 static int allocate_more_resources(void)
278 struct resource *new_res_list;
279 new_res_list = malloc(64 * sizeof(*new_res_list));
281 if (new_res_list == NULL)
284 memset(new_res_list, 0, 64 * sizeof(*new_res_list));
286 for (i = 0; i < 64-1; i++)
287 new_res_list[i].next = &new_res_list[i+1];
289 free_resources = new_res_list;
294 * Remove resource res from the device's list and add it to the free list.
301 static void free_resource(device_t dev, struct resource *res,
302 struct resource *prev)
305 prev->next = res->next;
307 dev->resource_list = res->next;
308 res->next = free_resources;
309 free_resources = res;
313 * See if we have unused but allocated resource structures.
314 * If so remove the allocation.
316 * @param dev The device to find the resource on.
318 void compact_resources(device_t dev)
320 struct resource *res, *next, *prev = NULL;
321 /* Move all of the free resources to the end */
322 for (res = dev->resource_list; res; res = next) {
325 free_resource(dev, res, prev);
332 * See if a resource structure already exists for a given index.
334 * @param dev The device to find the resource on.
335 * @param index The index of the resource on the device.
336 * @return The resource, if it already exists.
338 struct resource *probe_resource(device_t dev, unsigned index)
340 struct resource *res;
342 /* See if there is a resource with the appropriate index */
343 for (res = dev->resource_list; res; res = res->next) {
344 if (res->index == index)
351 * See if a resource structure already exists for a given index and if not
354 * Then initialize the initialize the resource to default values.
356 * @param dev The device to find the resource on.
357 * @param index The index of the resource on the device.
360 struct resource *new_resource(device_t dev, unsigned index)
362 struct resource *resource, *tail;
364 /* First move all of the free resources to the end */
365 compact_resources(dev);
367 /* See if there is a resource with the appropriate index */
368 resource = probe_resource(dev, index);
370 if (free_resources == NULL && !allocate_more_resources())
371 die("Couldn't allocate more resources.");
373 resource = free_resources;
374 free_resources = free_resources->next;
375 memset(resource, 0, sizeof(*resource));
376 resource->next = NULL;
377 tail = dev->resource_list;
379 while (tail->next) tail = tail->next;
380 tail->next = resource;
383 dev->resource_list = resource;
385 /* Initialize the resource values */
386 if (!(resource->flags & IORESOURCE_FIXED)) {
392 resource->index = index;
400 * Return an existing resource structure for a given index.
402 * @param dev The device to find the resource on.
403 * @param index The index of the resource on the device.
406 struct resource *find_resource(device_t dev, unsigned index)
408 struct resource *resource;
410 /* See if there is a resource with the appropriate index */
411 resource = probe_resource(dev, index);
413 printk(BIOS_EMERG, "%s missing resource: %02x\n",
414 dev_path(dev), index);
422 * Round a number up to the next multiple of gran.
424 * @param val The starting value.
425 * @param gran Granularity we are aligning the number to.
426 * @return The aligned value.
428 static resource_t align_up(resource_t val, unsigned long gran)
431 mask = (1ULL << gran) - 1ULL;
438 * Round a number up to the previous multiple of gran.
440 * @param val The starting value.
441 * @param gran Granularity we are aligning the number to.
442 * @return The aligned value.
444 static resource_t align_down(resource_t val, unsigned long gran)
447 mask = (1ULL << gran) - 1ULL;
453 * Compute the maximum address that is part of a resource.
455 * @param resource The resource whose limit is desired.
458 resource_t resource_end(struct resource *resource)
460 resource_t base, end;
461 /* get the base address */
462 base = resource->base;
464 /* For a non bridge resource granularity and alignment are the same.
465 * For a bridge resource align is the largest needed alignment below
466 * the bridge. While the granularity is simply how many low bits of the
467 * address cannot be set.
470 /* Get the end (rounded up) */
471 end = base + align_up(resource->size, resource->gran) - 1;
477 * Compute the maximum legal value for resource->base.
479 * @param resource The resource whose maximum is desired.
480 * @return The maximum.
482 resource_t resource_max(struct resource *resource)
486 max = align_down(resource->limit - resource->size + 1, resource->align);
492 * Return the resource type of a resource.
494 * @param resource The resource type to decode.
497 const char *resource_type(struct resource *resource)
499 static char buffer[RESOURCE_TYPE_MAX];
500 sprintf(buffer, "%s%s%s%s",
501 ((resource->flags & IORESOURCE_READONLY)? "ro": ""),
502 ((resource->flags & IORESOURCE_PREFETCH)? "pref":""),
503 ((resource->flags == 0)? "unused":
504 (resource->flags & IORESOURCE_IO)? "io":
505 (resource->flags & IORESOURCE_DRQ)? "drq":
506 (resource->flags & IORESOURCE_IRQ)? "irq":
507 (resource->flags & IORESOURCE_MEM)? "mem":"??????"),
508 ((resource->flags & IORESOURCE_PCI64)?"64":""));
513 * Print the resource that was just stored.
515 * @param dev The device the stored resorce lives on.
516 * @param resource The resource that was just stored.
517 * @param comment TODO
519 void report_resource_stored(device_t dev, struct resource *resource,
522 if (resource->flags & IORESOURCE_STORED) {
524 unsigned long long base, end;
525 base = resource->base;
526 end = resource_end(resource);
528 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
529 #if CONFIG_PCI_BUS_SEGN_BITS
530 sprintf(buf, "bus %04x:%02x ", dev->bus->secondary>>8, dev->link_list->secondary & 0xff);
532 sprintf(buf, "bus %02x ", dev->link_list->secondary);
536 "%s %02lx <- [0x%010Lx - 0x%010Lx] size 0x%08Lx gran 0x%02x %s%s%s\n",
540 resource->size, resource->gran,
542 resource_type(resource),
547 void search_bus_resources(struct bus *bus,
548 unsigned long type_mask, unsigned long type,
549 resource_search_t search, void *gp)
551 struct device *curdev;
552 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
553 struct resource *res;
554 /* Ignore disabled devices */
555 if (!curdev->enabled) continue;
556 for (res = curdev->resource_list; res; res = res->next) {
557 /* If it isn't the right kind of resource ignore it */
558 if ((res->flags & type_mask) != type) {
561 /* If it is a subtractive resource recurse */
562 if (res->flags & IORESOURCE_SUBTRACTIVE) {
564 for (subbus = curdev->link_list; subbus; subbus = subbus->next)
565 if (subbus->link_num == IOINDEX_SUBTRACTIVE_LINK(res->index))
567 search_bus_resources(subbus, type_mask, type, search, gp);
570 search(gp, curdev, res);
575 void search_global_resources(
576 unsigned long type_mask, unsigned long type,
577 resource_search_t search, void *gp)
579 struct device *curdev;
580 for (curdev = all_devices; curdev; curdev = curdev->next) {
581 struct resource *res;
582 /* Ignore disabled devices */
583 if (!curdev->enabled) 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) {
589 /* If it is a subtractive resource ignore it */
590 if (res->flags & IORESOURCE_SUBTRACTIVE) {
593 search(gp, curdev, res);
598 void dev_set_enabled(device_t dev, int enable)
600 if (dev->enabled == enable) {
603 dev->enabled = enable;
604 if (dev->ops && dev->ops->enable) {
605 dev->ops->enable(dev);
607 else if (dev->chip_ops && dev->chip_ops->enable_dev) {
608 dev->chip_ops->enable_dev(dev);
612 void disable_children(struct bus *bus)
615 for (child = bus->children; child; child = child->sibling) {
617 for (link = child->link_list; link; link = link->next) {
618 disable_children(link);
620 dev_set_enabled(child, 0);
624 static void resource_tree(struct device *root, int debug_level, int depth)
627 struct device *child;
629 struct resource *res;
630 char indent[30]; /* If your tree has more levels, it's wrong. */
632 for (i = 0; i < depth + 1 && i < 29; i++)
636 do_printk(BIOS_DEBUG, "%s%s", indent, dev_path(root));
637 if (root->link_list && root->link_list->children)
638 do_printk(BIOS_DEBUG, " child on link 0 %s",
639 dev_path(root->link_list->children));
640 do_printk(BIOS_DEBUG, "\n");
642 for (res = root->resource_list; res; res = res->next) {
643 do_printk(debug_level,
644 "%s%s resource base %llx size %llx align %d gran %d limit %llx flags %lx index %lx\n",
645 indent, dev_path(root), res->base,
646 res->size, res->align,
647 res->gran, res->limit,
648 res->flags, res->index);
651 for (link = root->link_list; link; link = link->next) {
652 for (child = link->children; child; child = child->sibling)
653 resource_tree(child, debug_level, depth + 1);
657 void print_resource_tree(struct device * root, int debug_level,
660 /* Bail if root is null. */
662 do_printk(debug_level, "%s passed NULL for root!\n", __func__);
666 /* Bail if not printing to screen. */
667 if (!do_printk(debug_level, "Show resources in subtree (%s)...%s\n",
668 dev_path(root), msg))
670 resource_tree(root, debug_level, 0);
673 void show_devs_tree(struct device *dev, int debug_level, int depth, int linknum)
675 char depth_str[20] = "";
677 struct device *sibling;
680 for (i = 0; i < depth; i++)
683 do_printk(debug_level, "%s%s: enabled %d\n",
684 depth_str, dev_path(dev), dev->enabled);
685 for (link = dev->link_list; link; link = link->next) {
686 for (sibling = link->children; sibling;
687 sibling = sibling->sibling)
688 show_devs_tree(sibling, debug_level, depth + 1, i);
692 void show_all_devs_tree(int debug_level, const char *msg)
694 /* Bail if not printing to screen. */
695 if (!do_printk(debug_level, "Show all devs in tree form...%s\n", msg))
697 show_devs_tree(all_devices, debug_level, 0, -1);
700 void show_devs_subtree(struct device *root, int debug_level, const char *msg)
702 /* Bail if not printing to screen. */
703 if (!do_printk(debug_level, "Show all devs in subtree %s...%s\n",
704 dev_path(root), msg))
706 do_printk(debug_level, "%s\n", msg);
707 show_devs_tree(root, debug_level, 0, -1);
710 void show_all_devs(int debug_level, const char *msg)
714 /* Bail if not printing to screen. */
715 if (!do_printk(debug_level, "Show all devs...%s\n", msg))
717 for (dev = all_devices; dev; dev = dev->next) {
718 do_printk(debug_level, "%s: enabled %d\n",
719 dev_path(dev), dev->enabled);
723 void show_one_resource(int debug_level, struct device *dev,
724 struct resource *resource, const char *comment)
727 unsigned long long base, end;
728 base = resource->base;
729 end = resource_end(resource);
732 if (resource->flags & IORESOURCE_BRIDGE) {
733 #if CONFIG_PCI_BUS_SEGN_BITS
734 sprintf(buf, "bus %04x:%02x ", dev->bus->secondary >> 8,
735 dev->link[0].secondary & 0xff);
737 sprintf(buf, "bus %02x ", dev->link[0].secondary);
741 do_printk(debug_level, "%s %02lx <- [0x%010llx - 0x%010llx] "
742 "size 0x%08Lx gran 0x%02x %s%s%s\n",
743 dev_path(dev), resource->index, base, end,
744 resource->size, resource->gran, buf,
745 resource_type(resource), comment);
749 void show_all_devs_resources(int debug_level, const char* msg)
753 if(!do_printk(debug_level, "Show all devs with resources...%s\n", msg))
756 for (dev = all_devices; dev; dev = dev->next) {
757 struct resource *res;
758 do_printk(debug_level, "%s: enabled %d\n",
759 dev_path(dev), dev->enabled);
760 for (res = dev->resource_list; res; res = res->next)
761 show_one_resource(debug_level, dev, res, "");
765 void ram_resource(device_t dev, unsigned long index,
766 unsigned long basek, unsigned long sizek)
768 struct resource *resource;
773 resource = new_resource(dev, index);
774 resource->base = ((resource_t)basek) << 10;
775 resource->size = ((resource_t)sizek) << 10;
776 resource->flags = IORESOURCE_MEM | IORESOURCE_CACHEABLE | \
777 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
780 void tolm_test(void *gp, struct device *dev, struct resource *new)
782 struct resource **best_p = gp;
783 struct resource *best;
787 if (!best || (best->base > new->base))
793 u32 find_pci_tolm(struct bus *bus)
795 struct resource *min = NULL;
798 search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM,
803 if (min && tolm > min->base)