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 * Find a device of a given vendor and type.
115 * @param vendor A PCI vendor ID (e.g. 0x8086 for Intel).
116 * @param device A PCI device ID.
117 * @param from Pointer to the device structure, used as a starting point in
118 * the linked list of all_devices, which can be 0 to start at the
119 * head of the list (i.e. all_devices).
120 * @return Pointer to the device struct.
122 struct device *dev_find_device(u16 vendor, u16 device, struct device *from)
129 while (from && (from->vendor != vendor || from->device != device))
136 * Find a device of a given class.
138 * @param class Class of the device.
139 * @param from Pointer to the device structure, used as a starting point in
140 * the linked list of all_devices, which can be 0 to start at the
141 * head of the list (i.e. all_devices).
142 * @return Pointer to the device struct.
144 struct device *dev_find_class(unsigned int class, struct device *from)
151 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);
169 switch(dev->path.type) {
170 case DEVICE_PATH_ROOT:
171 memcpy(buffer, "Root Device", 12);
173 case DEVICE_PATH_PCI:
174 #if CONFIG_PCI_BUS_SEGN_BITS
175 sprintf(buffer, "PCI: %04x:%02x:%02x.%01x",
176 dev->bus->secondary >> 8,
177 dev->bus->secondary & 0xff,
178 PCI_SLOT(dev->path.pci.devfn),
179 PCI_FUNC(dev->path.pci.devfn));
181 sprintf(buffer, "PCI: %02x:%02x.%01x",
183 PCI_SLOT(dev->path.pci.devfn),
184 PCI_FUNC(dev->path.pci.devfn));
187 case DEVICE_PATH_PNP:
188 sprintf(buffer, "PNP: %04x.%01x",
189 dev->path.pnp.port, dev->path.pnp.device);
191 case DEVICE_PATH_I2C:
192 sprintf(buffer, "I2C: %02x:%02x",
194 dev->path.i2c.device);
196 case DEVICE_PATH_APIC:
197 sprintf(buffer, "APIC: %02x",
198 dev->path.apic.apic_id);
200 case DEVICE_PATH_PCI_DOMAIN:
201 sprintf(buffer, "PCI_DOMAIN: %04x",
202 dev->path.pci_domain.domain);
204 case DEVICE_PATH_APIC_CLUSTER:
205 sprintf(buffer, "APIC_CLUSTER: %01x",
206 dev->path.apic_cluster.cluster);
208 case DEVICE_PATH_CPU:
209 sprintf(buffer, "CPU: %02x", dev->path.cpu.id);
211 case DEVICE_PATH_CPU_BUS:
212 sprintf(buffer, "CPU_BUS: %02x", dev->path.cpu_bus.id);
215 printk(BIOS_ERR, "Unknown device path type: %d\n",
223 const char *bus_path(struct bus *bus)
225 static char buffer[BUS_PATH_MAX];
226 sprintf(buffer, "%s,%d", dev_path(bus->dev), bus->link_num);
230 int path_eq(struct device_path *path1, struct device_path *path2)
234 if (path1->type != path2->type)
237 switch (path1->type) {
238 case DEVICE_PATH_NONE:
240 case DEVICE_PATH_ROOT:
243 case DEVICE_PATH_PCI:
244 equal = (path1->pci.devfn == path2->pci.devfn);
246 case DEVICE_PATH_PNP:
247 equal = (path1->pnp.port == path2->pnp.port) &&
248 (path1->pnp.device == path2->pnp.device);
250 case DEVICE_PATH_I2C:
251 equal = (path1->i2c.device == path2->i2c.device);
253 case DEVICE_PATH_APIC:
254 equal = (path1->apic.apic_id == path2->apic.apic_id);
256 case DEVICE_PATH_PCI_DOMAIN:
257 equal = (path1->pci_domain.domain == path2->pci_domain.domain);
259 case DEVICE_PATH_APIC_CLUSTER:
260 equal = (path1->apic_cluster.cluster
261 == path2->apic_cluster.cluster);
263 case DEVICE_PATH_CPU:
264 equal = (path1->cpu.id == path2->cpu.id);
266 case DEVICE_PATH_CPU_BUS:
267 equal = (path1->cpu_bus.id == path2->cpu_bus.id);
270 printk(BIOS_ERR, "Uknown device type: %d\n", path1->type);
278 * Allocate 64 more resources to the free list.
282 static int allocate_more_resources(void)
285 struct resource *new_res_list;
287 new_res_list = malloc(64 * sizeof(*new_res_list));
289 if (new_res_list == NULL)
292 memset(new_res_list, 0, 64 * sizeof(*new_res_list));
294 for (i = 0; i < 64 - 1; i++)
295 new_res_list[i].next = &new_res_list[i+1];
297 free_resources = new_res_list;
302 * Remove resource res from the device's list and add it to the free list.
309 static void free_resource(device_t dev, struct resource *res,
310 struct resource *prev)
313 prev->next = res->next;
315 dev->resource_list = res->next;
317 res->next = free_resources;
318 free_resources = res;
322 * See if we have unused but allocated resource structures.
324 * If so remove the allocation.
326 * @param dev The device to find the resource on.
328 void compact_resources(device_t dev)
330 struct resource *res, *next, *prev = NULL;
332 /* Move all of the free resources to the end */
333 for (res = dev->resource_list; res; res = next) {
336 free_resource(dev, res, prev);
343 * See if a resource structure already exists for a given index.
345 * @param dev The device to find the resource on.
346 * @param index The index of the resource on the device.
347 * @return The resource, if it already exists.
349 struct resource *probe_resource(device_t dev, unsigned index)
351 struct resource *res;
353 /* See if there is a resource with the appropriate index */
354 for (res = dev->resource_list; res; res = res->next) {
355 if (res->index == index)
363 * See if a resource structure already exists for a given index and if not
366 * Then initialize the initialize the resource to default values.
368 * @param dev The device to find the resource on.
369 * @param index The index of the resource on the device.
372 struct resource *new_resource(device_t dev, unsigned index)
374 struct resource *resource, *tail;
376 /* First move all of the free resources to the end. */
377 compact_resources(dev);
379 /* See if there is a resource with the appropriate index. */
380 resource = probe_resource(dev, index);
382 if (free_resources == NULL && !allocate_more_resources())
383 die("Couldn't allocate more resources.");
385 resource = free_resources;
386 free_resources = free_resources->next;
387 memset(resource, 0, sizeof(*resource));
388 resource->next = NULL;
389 tail = dev->resource_list;
391 while (tail->next) tail = tail->next;
392 tail->next = resource;
394 dev->resource_list = resource;
398 /* Initialize the resource values. */
399 if (!(resource->flags & IORESOURCE_FIXED)) {
405 resource->index = index;
413 * Return an existing resource structure for a given index.
415 * @param dev The device to find the resource on.
416 * @param index The index of the resource on the device.
419 struct resource *find_resource(device_t dev, unsigned index)
421 struct resource *resource;
423 /* See if there is a resource with the appropriate index. */
424 resource = probe_resource(dev, index);
426 printk(BIOS_EMERG, "%s missing resource: %02x\n",
427 dev_path(dev), index);
434 * Round a number up to the next multiple of gran.
436 * @param val The starting value.
437 * @param gran Granularity we are aligning the number to.
438 * @return The aligned value.
440 static resource_t align_up(resource_t val, unsigned long gran)
443 mask = (1ULL << gran) - 1ULL;
450 * Round a number up to the previous multiple of gran.
452 * @param val The starting value.
453 * @param gran Granularity we are aligning the number to.
454 * @return The aligned value.
456 static resource_t align_down(resource_t val, unsigned long gran)
459 mask = (1ULL << gran) - 1ULL;
465 * Compute the maximum address that is part of a resource.
467 * @param resource The resource whose limit is desired.
470 resource_t resource_end(struct resource *resource)
472 resource_t base, end;
474 /* Get the base address. */
475 base = resource->base;
478 * For a non bridge resource granularity and alignment are the same.
479 * For a bridge resource align is the largest needed alignment below
480 * the bridge. While the granularity is simply how many low bits of
481 * the address cannot be set.
484 /* Get the end (rounded up). */
485 end = base + align_up(resource->size, resource->gran) - 1;
491 * Compute the maximum legal value for resource->base.
493 * @param resource The resource whose maximum is desired.
494 * @return The maximum.
496 resource_t resource_max(struct resource *resource)
500 max = align_down(resource->limit - resource->size + 1, resource->align);
506 * Return the resource type of a resource.
508 * @param resource The resource type to decode.
511 const char *resource_type(struct resource *resource)
513 static char buffer[RESOURCE_TYPE_MAX];
514 sprintf(buffer, "%s%s%s%s",
515 ((resource->flags & IORESOURCE_READONLY) ? "ro" : ""),
516 ((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""),
517 ((resource->flags == 0) ? "unused" :
518 (resource->flags & IORESOURCE_IO) ? "io" :
519 (resource->flags & IORESOURCE_DRQ) ? "drq" :
520 (resource->flags & IORESOURCE_IRQ) ? "irq" :
521 (resource->flags & IORESOURCE_MEM) ? "mem" : "??????"),
522 ((resource->flags & IORESOURCE_PCI64) ? "64" : ""));
527 * Print the resource that was just stored.
529 * @param dev The device the stored resorce lives on.
530 * @param resource The resource that was just stored.
531 * @param comment TODO
533 void report_resource_stored(device_t dev, struct resource *resource,
537 unsigned long long base, end;
539 if (!(resource->flags & IORESOURCE_STORED))
542 base = resource->base;
543 end = resource_end(resource);
546 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
547 #if CONFIG_PCI_BUS_SEGN_BITS
548 sprintf(buf, "bus %04x:%02x ", dev->bus->secondary >> 8,
549 dev->link_list->secondary & 0xff);
551 sprintf(buf, "bus %02x ", dev->link_list->secondary);
554 printk(BIOS_DEBUG, "%s %02lx <- [0x%010Lx - 0x%010Lx] size 0x%08Lx "
555 "gran 0x%02x %s%s%s\n", dev_path(dev), resource->index,
556 base, end, resource->size, resource->gran, buf,
557 resource_type(resource), comment);
560 void search_bus_resources(struct bus *bus, unsigned long type_mask,
561 unsigned long type, resource_search_t search,
564 struct device *curdev;
566 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
567 struct resource *res;
569 /* Ignore disabled devices. */
570 if (!curdev->enabled)
573 for (res = curdev->resource_list; res; res = res->next) {
574 /* If it isn't the right kind of resource ignore it. */
575 if ((res->flags & type_mask) != type)
578 /* If it is a subtractive resource recurse. */
579 if (res->flags & IORESOURCE_SUBTRACTIVE) {
581 for (subbus = curdev->link_list; subbus;
582 subbus = subbus->next)
584 == IOINDEX_SUBTRACTIVE_LINK(res->index))
586 if (!subbus) /* Why can subbus be NULL? */
588 search_bus_resources(subbus, type_mask, type,
592 search(gp, curdev, res);
597 void search_global_resources(unsigned long type_mask, unsigned long type,
598 resource_search_t search, void *gp)
600 struct device *curdev;
602 for (curdev = all_devices; curdev; curdev = curdev->next) {
603 struct resource *res;
605 /* Ignore disabled devices. */
606 if (!curdev->enabled)
609 for (res = curdev->resource_list; res; res = res->next) {
610 /* If it isn't the right kind of resource ignore it. */
611 if ((res->flags & type_mask) != type)
614 /* If it is a subtractive resource ignore it. */
615 if (res->flags & IORESOURCE_SUBTRACTIVE)
618 search(gp, curdev, res);
623 void dev_set_enabled(device_t dev, int enable)
625 if (dev->enabled == enable)
628 dev->enabled = enable;
629 if (dev->ops && dev->ops->enable) {
630 dev->ops->enable(dev);
631 } else if (dev->chip_ops && dev->chip_ops->enable_dev) {
632 dev->chip_ops->enable_dev(dev);
636 void disable_children(struct bus *bus)
640 for (child = bus->children; child; child = child->sibling) {
642 for (link = child->link_list; link; link = link->next)
643 disable_children(link);
644 dev_set_enabled(child, 0);
648 static void resource_tree(struct device *root, int debug_level, int depth)
651 struct device *child;
653 struct resource *res;
654 char indent[30]; /* If your tree has more levels, it's wrong. */
656 for (i = 0; i < depth + 1 && i < 29; i++)
660 do_printk(BIOS_DEBUG, "%s%s", indent, dev_path(root));
661 if (root->link_list && root->link_list->children)
662 do_printk(BIOS_DEBUG, " child on link 0 %s",
663 dev_path(root->link_list->children));
664 do_printk(BIOS_DEBUG, "\n");
666 for (res = root->resource_list; res; res = res->next) {
667 do_printk(debug_level, "%s%s resource base %llx size %llx "
668 "align %d gran %d limit %llx flags %lx index %lx\n",
669 indent, dev_path(root), res->base, res->size,
670 res->align, res->gran, res->limit, res->flags,
674 for (link = root->link_list; link; link = link->next) {
675 for (child = link->children; child; child = child->sibling)
676 resource_tree(child, debug_level, depth + 1);
680 void print_resource_tree(struct device *root, int debug_level, const char *msg)
682 /* Bail if root is null. */
684 do_printk(debug_level, "%s passed NULL for root!\n", __func__);
688 /* Bail if not printing to screen. */
689 if (!do_printk(debug_level, "Show resources in subtree (%s)...%s\n",
690 dev_path(root), msg))
693 resource_tree(root, debug_level, 0);
696 void show_devs_tree(struct device *dev, int debug_level, int depth, int linknum)
698 char depth_str[20] = "";
700 struct device *sibling;
703 for (i = 0; i < depth; i++)
707 do_printk(debug_level, "%s%s: enabled %d\n",
708 depth_str, dev_path(dev), dev->enabled);
710 for (link = dev->link_list; link; link = link->next) {
711 for (sibling = link->children; sibling;
712 sibling = sibling->sibling)
713 show_devs_tree(sibling, debug_level, depth + 1, i);
717 void show_all_devs_tree(int debug_level, const char *msg)
719 /* Bail if not printing to screen. */
720 if (!do_printk(debug_level, "Show all devs in tree form...%s\n", msg))
722 show_devs_tree(all_devices, debug_level, 0, -1);
725 void show_devs_subtree(struct device *root, int debug_level, const char *msg)
727 /* Bail if not printing to screen. */
728 if (!do_printk(debug_level, "Show all devs in subtree %s...%s\n",
729 dev_path(root), msg))
731 do_printk(debug_level, "%s\n", msg);
732 show_devs_tree(root, debug_level, 0, -1);
735 void show_all_devs(int debug_level, const char *msg)
739 /* Bail if not printing to screen. */
740 if (!do_printk(debug_level, "Show all devs...%s\n", msg))
742 for (dev = all_devices; dev; dev = dev->next) {
743 do_printk(debug_level, "%s: enabled %d\n",
744 dev_path(dev), dev->enabled);
748 void show_one_resource(int debug_level, struct device *dev,
749 struct resource *resource, const char *comment)
752 unsigned long long base, end;
753 base = resource->base;
754 end = resource_end(resource);
758 if (resource->flags & IORESOURCE_BRIDGE) {
759 #if CONFIG_PCI_BUS_SEGN_BITS
760 sprintf(buf, "bus %04x:%02x ", dev->bus->secondary >> 8,
761 dev->link[0].secondary & 0xff);
763 sprintf(buf, "bus %02x ", dev->link[0].secondary);
768 do_printk(debug_level, "%s %02lx <- [0x%010llx - 0x%010llx] "
769 "size 0x%08Lx gran 0x%02x %s%s%s\n", dev_path(dev),
770 resource->index, base, end, resource->size, resource->gran,
771 buf, resource_type(resource), comment);
774 void show_all_devs_resources(int debug_level, const char* msg)
778 if (!do_printk(debug_level, "Show all devs with resources...%s\n", msg))
781 for (dev = all_devices; dev; dev = dev->next) {
782 struct resource *res;
783 do_printk(debug_level, "%s: enabled %d\n",
784 dev_path(dev), dev->enabled);
785 for (res = dev->resource_list; res; res = res->next)
786 show_one_resource(debug_level, dev, res, "");
790 void ram_resource(device_t dev, unsigned long index,
791 unsigned long basek, unsigned long sizek)
793 struct resource *resource;
798 resource = new_resource(dev, index);
799 resource->base = ((resource_t)basek) << 10;
800 resource->size = ((resource_t)sizek) << 10;
801 resource->flags = IORESOURCE_MEM | IORESOURCE_CACHEABLE | \
802 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
805 void tolm_test(void *gp, struct device *dev, struct resource *new)
807 struct resource **best_p = gp;
808 struct resource *best;
812 if (!best || (best->base > new->base))
818 u32 find_pci_tolm(struct bus *bus)
820 struct resource *min = NULL;
823 search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM,
828 if (min && tolm > min->base)