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 * @brief See if a device structure exists for path
35 * @param bus 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 * @brief See if a device structure already exists and if not allocate it
54 * @param bus 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 * @brief 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
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 * @brief Given a 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
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)) {
114 /** Find a device of a given vendor and type
115 * @param vendor Vendor ID (e.g. 0x8086 for Intel)
116 * @param device Device ID
117 * @param from Pointer to the device structure, used as a starting point
118 * in 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(unsigned int vendor, unsigned int device, struct device *from)
128 while (from && (from->vendor != vendor || from->device != device)) {
134 /** Find a device of a given class
135 * @param class Class of the device
136 * @param from Pointer to the device structure, used as a starting point
137 * in the linked list of all_devices, which can be 0 to start at the
138 * head of the list (i.e. all_devices)
139 * @return Pointer to the device struct
141 struct device *dev_find_class(unsigned int class, struct device *from)
147 while (from && (from->class & 0xffffff00) != class)
152 /* Warning: This function uses a static buffer. Don't call it more than once
153 * from the same print statement! */
155 const char *dev_path(device_t dev)
157 static char buffer[DEVICE_PATH_MAX];
160 memcpy(buffer, "<null>", 7);
163 switch(dev->path.type) {
164 case DEVICE_PATH_ROOT:
165 memcpy(buffer, "Root Device", 12);
167 case DEVICE_PATH_PCI:
168 #if CONFIG_PCI_BUS_SEGN_BITS
169 sprintf(buffer, "PCI: %04x:%02x:%02x.%01x",
170 dev->bus->secondary>>8, dev->bus->secondary & 0xff,
171 PCI_SLOT(dev->path.pci.devfn), PCI_FUNC(dev->path.pci.devfn));
173 sprintf(buffer, "PCI: %02x:%02x.%01x",
175 PCI_SLOT(dev->path.pci.devfn), PCI_FUNC(dev->path.pci.devfn));
178 case DEVICE_PATH_PNP:
179 sprintf(buffer, "PNP: %04x.%01x",
180 dev->path.pnp.port, dev->path.pnp.device);
182 case DEVICE_PATH_I2C:
183 sprintf(buffer, "I2C: %02x:%02x",
185 dev->path.i2c.device);
187 case DEVICE_PATH_APIC:
188 sprintf(buffer, "APIC: %02x",
189 dev->path.apic.apic_id);
191 case DEVICE_PATH_PCI_DOMAIN:
192 sprintf(buffer, "PCI_DOMAIN: %04x",
193 dev->path.pci_domain.domain);
195 case DEVICE_PATH_APIC_CLUSTER:
196 sprintf(buffer, "APIC_CLUSTER: %01x",
197 dev->path.apic_cluster.cluster);
199 case DEVICE_PATH_CPU:
200 sprintf(buffer, "CPU: %02x", dev->path.cpu.id);
202 case DEVICE_PATH_CPU_BUS:
203 sprintf(buffer, "CPU_BUS: %02x", dev->path.cpu_bus.id);
206 printk(BIOS_ERR, "Unknown device path type: %d\n", dev->path.type);
213 const char *bus_path(struct bus *bus)
215 static char buffer[BUS_PATH_MAX];
216 sprintf(buffer, "%s,%d", dev_path(bus->dev), bus->link);
220 int path_eq(struct device_path *path1, struct device_path *path2)
223 if (path1->type == path2->type) {
224 switch(path1->type) {
225 case DEVICE_PATH_NONE:
227 case DEVICE_PATH_ROOT:
230 case DEVICE_PATH_PCI:
231 equal = (path1->pci.devfn == path2->pci.devfn);
233 case DEVICE_PATH_PNP:
234 equal = (path1->pnp.port == path2->pnp.port) &&
235 (path1->pnp.device == path2->pnp.device);
237 case DEVICE_PATH_I2C:
238 equal = (path1->i2c.device == path2->i2c.device);
240 case DEVICE_PATH_APIC:
241 equal = (path1->apic.apic_id == path2->apic.apic_id);
243 case DEVICE_PATH_PCI_DOMAIN:
244 equal = (path1->pci_domain.domain == path2->pci_domain.domain);
246 case DEVICE_PATH_APIC_CLUSTER:
247 equal = (path1->apic_cluster.cluster == path2->apic_cluster.cluster);
249 case DEVICE_PATH_CPU:
250 equal = (path1->cpu.id == path2->cpu.id);
252 case DEVICE_PATH_CPU_BUS:
253 equal = (path1->cpu_bus.id == path2->cpu_bus.id);
256 printk(BIOS_ERR, "Uknown device type: %d\n", path1->type);
264 * Allocate 64 more resources to the free list.
266 static int allocate_more_resources(void)
269 struct resource *new_res_list;
270 new_res_list = malloc(64 * sizeof(*new_res_list));
272 if (new_res_list == NULL)
275 memset(new_res_list, 0, 64 * sizeof(*new_res_list));
277 for (i = 0; i < 64-1; i++)
278 new_res_list[i].next = &new_res_list[i+1];
280 free_resources = new_res_list;
285 * Remove resource res from the device's list and add it to the free list.
287 static void free_resource(device_t dev, struct resource *res, struct resource *prev)
290 prev->next = res->next;
292 dev->resource_list = res->next;
293 res->next = free_resources;
294 free_resources = res;
298 * See if we have unused but allocated resource structures.
299 * If so remove the allocation.
300 * @param dev The device to find the resource on
302 void compact_resources(device_t dev)
304 struct resource *res, *next, *prev = NULL;
305 /* Move all of the free resources to the end */
306 for(res = dev->resource_list; res; res = next) {
309 free_resource(dev, res, prev);
317 * See if a resource structure already exists for a given index
318 * @param dev The device to find the resource on
319 * @param index The index of the resource on the device.
320 * @return the resource if it already exists
322 struct resource *probe_resource(device_t dev, unsigned index)
324 struct resource *res;
325 /* See if there is a resource with the appropriate index */
326 for(res = dev->resource_list; res; res = res->next) {
327 if (res->index == index)
334 * See if a resource structure already exists for a given index and if
335 * not allocate one. Then initialize the initialize the resource
337 * @param dev The device to find the resource on
338 * @param index The index of the resource on the device.
340 struct resource *new_resource(device_t dev, unsigned index)
342 struct resource *resource, *tail;
344 /* First move all of the free resources to the end */
345 compact_resources(dev);
347 /* See if there is a resource with the appropriate index */
348 resource = probe_resource(dev, index);
350 if (free_resources == NULL && !allocate_more_resources())
351 die("Couldn't allocate more resources.");
353 resource = free_resources;
354 free_resources = free_resources->next;
355 memset(resource, 0, sizeof(*resource));
356 resource->next = NULL;
357 tail = dev->resource_list;
359 while (tail->next) tail = tail->next;
360 tail->next = resource;
363 dev->resource_list = resource;
365 /* Initialize the resource values */
366 if (!(resource->flags & IORESOURCE_FIXED)) {
372 resource->index = index;
380 * Return an existing resource structure for a given index.
381 * @param dev The device to find the resource on
382 * @param index The index of the resource on the device.
384 struct resource *find_resource(device_t dev, unsigned index)
386 struct resource *resource;
388 /* See if there is a resource with the appropriate index */
389 resource = probe_resource(dev, index);
391 printk(BIOS_EMERG, "%s missing resource: %02x\n",
392 dev_path(dev), index);
400 * @brief round a number up to the next multiple of gran
401 * @param val the starting value
402 * @param gran granularity we are aligning the number to.
403 * @returns aligned value
405 static resource_t align_up(resource_t val, unsigned long gran)
408 mask = (1ULL << gran) - 1ULL;
415 * @brief round a number up to the previous multiple of gran
416 * @param val the starting value
417 * @param gran granularity we are aligning the number to.
418 * @returns aligned value
420 static resource_t align_down(resource_t val, unsigned long gran)
423 mask = (1ULL << gran) - 1ULL;
429 * @brief Compute the maximum address that is part of a resource
430 * @param resource the resource whose limit is desired
433 resource_t resource_end(struct resource *resource)
435 resource_t base, end;
436 /* get the base address */
437 base = resource->base;
439 /* For a non bridge resource granularity and alignment are the same.
440 * For a bridge resource align is the largest needed alignment below
441 * the bridge. While the granularity is simply how many low bits of the
442 * address cannot be set.
445 /* Get the end (rounded up) */
446 end = base + align_up(resource->size, resource->gran) - 1;
452 * @brief Compute the maximum legal value for resource->base
453 * @param resource the resource whose maximum is desired
454 * @returns the maximum
456 resource_t resource_max(struct resource *resource)
460 max = align_down(resource->limit - resource->size + 1, resource->align);
466 * @brief return the resource type of a resource
467 * @param resource the resource type to decode.
469 const char *resource_type(struct resource *resource)
471 static char buffer[RESOURCE_TYPE_MAX];
472 sprintf(buffer, "%s%s%s%s",
473 ((resource->flags & IORESOURCE_READONLY)? "ro": ""),
474 ((resource->flags & IORESOURCE_PREFETCH)? "pref":""),
475 ((resource->flags == 0)? "unused":
476 (resource->flags & IORESOURCE_IO)? "io":
477 (resource->flags & IORESOURCE_DRQ)? "drq":
478 (resource->flags & IORESOURCE_IRQ)? "irq":
479 (resource->flags & IORESOURCE_MEM)? "mem":"??????"),
480 ((resource->flags & IORESOURCE_PCI64)?"64":""));
485 * @brief print the resource that was just stored.
486 * @param dev the device the stored resorce lives on
487 * @param resource the resource that was just stored.
489 void report_resource_stored(device_t dev, struct resource *resource, const char *comment)
491 if (resource->flags & IORESOURCE_STORED) {
493 unsigned long long base, end;
494 base = resource->base;
495 end = resource_end(resource);
497 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
498 #if CONFIG_PCI_BUS_SEGN_BITS
499 sprintf(buf, "bus %04x:%02x ", dev->bus->secondary>>8, dev->link[0].secondary & 0xff);
501 sprintf(buf, "bus %02x ", dev->link[0].secondary);
505 "%s %02lx <- [0x%010Lx - 0x%010Lx] size 0x%08Lx gran 0x%02x %s%s%s\n",
509 resource->size, resource->gran,
511 resource_type(resource),
516 void search_bus_resources(struct bus *bus,
517 unsigned long type_mask, unsigned long type,
518 resource_search_t search, void *gp)
520 struct device *curdev;
521 for(curdev = bus->children; curdev; curdev = curdev->sibling) {
522 struct resource *res;
523 /* Ignore disabled devices */
524 if (!curdev->enabled) continue;
525 for(res = curdev->resource_list; res; res = res->next) {
526 /* If it isn't the right kind of resource ignore it */
527 if ((res->flags & type_mask) != type) {
530 /* If it is a subtractive resource recurse */
531 if (res->flags & IORESOURCE_SUBTRACTIVE) {
533 subbus = &curdev->link[IOINDEX_SUBTRACTIVE_LINK(res->index)];
534 search_bus_resources(subbus, type_mask, type, search, gp);
537 search(gp, curdev, res);
542 void search_global_resources(
543 unsigned long type_mask, unsigned long type,
544 resource_search_t search, void *gp)
546 struct device *curdev;
547 for(curdev = all_devices; curdev; curdev = curdev->next) {
548 struct resource *res;
549 /* Ignore disabled devices */
550 if (!curdev->enabled) continue;
551 for(res = curdev->resource_list; res; res = res->next) {
552 /* If it isn't the right kind of resource ignore it */
553 if ((res->flags & type_mask) != type) {
556 /* If it is a subtractive resource ignore it */
557 if (res->flags & IORESOURCE_SUBTRACTIVE) {
560 search(gp, curdev, res);
565 void dev_set_enabled(device_t dev, int enable)
567 if (dev->enabled == enable) {
570 dev->enabled = enable;
571 if (dev->ops && dev->ops->enable) {
572 dev->ops->enable(dev);
574 else if (dev->chip_ops && dev->chip_ops->enable_dev) {
575 dev->chip_ops->enable_dev(dev);
579 void disable_children(struct bus *bus)
582 for(child = bus->children; child; child = child->sibling) {
584 for(link = 0; link < child->links; link++) {
585 disable_children(&child->link[link]);
587 dev_set_enabled(child, 0);
591 static void resource_tree(struct device *root, int debug_level, int depth)
594 struct device *child;
595 struct resource *res;
596 char indent[30]; /* If your tree has more levels, it's wrong. */
598 for (i = 0; i < depth + 1 && i < 29; i++)
602 do_printk(debug_level, "%s%s links %x child on link 0", indent,
603 dev_path(root), root->links);
604 do_printk(debug_level, " %s\n", root->link[0].children ?
605 dev_path(root->link[0].children) : "NULL");
606 for (res = root->resource_list; res; res = res->next) {
607 do_printk(debug_level,
608 "%s%s resource base %llx size %llx align %d gran %d limit %llx flags %lx index %lx\n",
609 indent, dev_path(root), res->base,
610 res->size, res->align,
611 res->gran, res->limit,
612 res->flags, res->index);
615 for (link = 0; link < root->links; link++) {
616 for (child = root->link[link].children; child;
617 child = child->sibling)
618 resource_tree(child, debug_level, depth + 1);
622 void print_resource_tree(struct device * root, int debug_level,
625 /* Bail if root is null. */
627 do_printk(debug_level, "%s passed NULL for root!\n", __func__);
631 /* Bail if not printing to screen. */
632 if (!do_printk(debug_level, "Show resources in subtree (%s)...%s\n",
633 dev_path(root), msg))
635 resource_tree(root, debug_level, 0);
638 void show_devs_tree(struct device *dev, int debug_level, int depth, int linknum)
640 char depth_str[20] = "";
642 struct device *sibling;
643 for (i = 0; i < depth; i++)
646 do_printk(debug_level, "%s%s: enabled %d\n",
647 depth_str, dev_path(dev), dev->enabled);
648 for (i = 0; i < dev->links; i++) {
649 for (sibling = dev->link[i].children; sibling;
650 sibling = sibling->sibling)
651 show_devs_tree(sibling, debug_level, depth + 1, i);
655 void show_all_devs_tree(int debug_level, const char *msg)
657 /* Bail if not printing to screen. */
658 if (!do_printk(debug_level, "Show all devs in tree form...%s\n", msg))
660 show_devs_tree(all_devices, debug_level, 0, -1);
663 void show_devs_subtree(struct device *root, int debug_level, const char *msg)
665 /* Bail if not printing to screen. */
666 if (!do_printk(debug_level, "Show all devs in subtree %s...%s\n",
667 dev_path(root), msg))
669 do_printk(debug_level, "%s\n", msg);
670 show_devs_tree(root, debug_level, 0, -1);
673 void show_all_devs(int debug_level, const char *msg)
677 /* Bail if not printing to screen. */
678 if (!do_printk(debug_level, "Show all devs...%s\n", msg))
680 for (dev = all_devices; dev; dev = dev->next) {
681 do_printk(debug_level, "%s: enabled %d\n",
682 dev_path(dev), dev->enabled);
686 void show_one_resource(int debug_level, struct device *dev,
687 struct resource *resource, const char *comment)
690 unsigned long long base, end;
691 base = resource->base;
692 end = resource_end(resource);
695 if (resource->flags & IORESOURCE_BRIDGE) {
696 #if CONFIG_PCI_BUS_SEGN_BITS
697 sprintf(buf, "bus %04x:%02x ", dev->bus->secondary >> 8,
698 dev->link[0].secondary & 0xff);
700 sprintf(buf, "bus %02x ", dev->link[0].secondary);
704 do_printk(debug_level, "%s %02lx <- [0x%010llx - 0x%010llx] "
705 "size 0x%08Lx gran 0x%02x %s%s%s\n",
706 dev_path(dev), resource->index, base, end,
707 resource->size, resource->gran, buf,
708 resource_type(resource), comment);
712 void show_all_devs_resources(int debug_level, const char* msg)
716 if(!do_printk(debug_level, "Show all devs with resources...%s\n", msg))
719 for (dev = all_devices; dev; dev = dev->next) {
720 struct resource *res;
721 do_printk(debug_level, "%s: enabled %d\n",
722 dev_path(dev), dev->enabled);
723 for (res = dev->resource_list; res; res = res->next)
724 show_one_resource(debug_level, dev, res, "");