Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / src / devices / device_util.c
index efb1f0f2574ddc06a6ef16f5cbdd71316918bda0..3e8cba2f9815d1fe14cca136492edaf5d127c263 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * This file is part of the LinuxBIOS project.
+ * This file is part of the coreboot project.
  *
  * Copyright (C) 2003-2004 Linux Networx
  * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
@@ -79,8 +79,8 @@ struct device *dev_find_slot(unsigned int bus, unsigned int devfn)
        result = 0;
        for (dev = all_devices; dev; dev = dev->next) {
                if ((dev->path.type == DEVICE_PATH_PCI) &&
-                       (dev->bus->secondary == bus) && 
-                       (dev->path.u.pci.devfn == devfn)) {
+                       (dev->bus->secondary == bus) &&
+                       (dev->path.pci.devfn == devfn)) {
                        result = dev;
                        break;
                }
@@ -92,32 +92,32 @@ struct device *dev_find_slot(unsigned int bus, unsigned int devfn)
  * @brief Given a smbus bus and a device number, find the device structure
  *
  * @param bus The bus number
- * @param addr a device number 
+ * @param addr a device number
  * @return pointer to the device structure
  */
 struct device *dev_find_slot_on_smbus(unsigned int bus, unsigned int addr)
 {
         struct device *dev, *result;
-        
+
         result = 0;
         for (dev = all_devices; dev; dev = dev->next) {
                 if ((dev->path.type == DEVICE_PATH_I2C) &&
-                        (dev->bus->secondary == bus) && 
-                        (dev->path.u.i2c.device == addr)) {
+                        (dev->bus->secondary == bus) &&
+                        (dev->path.i2c.device == addr)) {
                         result = dev;
-                        break; 
-                }       
-        }       
+                        break;
+                }
+        }
         return result;
-}    
+}
 
 /** Find a device of a given vendor and type
  * @param vendor Vendor ID (e.g. 0x8086 for Intel)
  * @param device Device ID
  * @param from Pointer to the device structure, used as a starting point
- *        in the linked list of all_devices, which can be 0 to start at the 
+ *        in the linked list of all_devices, which can be 0 to start at the
  *        head of the list (i.e. all_devices)
- * @return Pointer to the device struct 
+ * @return Pointer to the device struct
  */
 struct device *dev_find_device(unsigned int vendor, unsigned int device, struct device *from)
 {
@@ -134,9 +134,9 @@ struct device *dev_find_device(unsigned int vendor, unsigned int device, struct
 /** Find a device of a given class
  * @param class Class of the device
  * @param from Pointer to the device structure, used as a starting point
- *        in the linked list of all_devices, which can be 0 to start at the 
+ *        in the linked list of all_devices, which can be 0 to start at the
  *        head of the list (i.e. all_devices)
- * @return Pointer to the device struct 
+ * @return Pointer to the device struct
  */
 struct device *dev_find_class(unsigned int class, struct device *from)
 {
@@ -149,6 +149,8 @@ struct device *dev_find_class(unsigned int class, struct device *from)
        return from;
 }
 
+/* Warning: This function uses a static buffer.  Don't call it more than once
+ * from the same print statement! */
 
 const char *dev_path(device_t dev)
 {
@@ -163,45 +165,45 @@ const char *dev_path(device_t dev)
                        memcpy(buffer, "Root Device", 12);
                        break;
                case DEVICE_PATH_PCI:
-#if PCI_BUS_SEGN_BITS
+#if CONFIG_PCI_BUS_SEGN_BITS
                        sprintf(buffer, "PCI: %04x:%02x:%02x.%01x",
-                               dev->bus->secondary>>8, dev->bus->secondary & 0xff, 
-                               PCI_SLOT(dev->path.u.pci.devfn), PCI_FUNC(dev->path.u.pci.devfn));
+                               dev->bus->secondary>>8, dev->bus->secondary & 0xff,
+                               PCI_SLOT(dev->path.pci.devfn), PCI_FUNC(dev->path.pci.devfn));
 #else
                        sprintf(buffer, "PCI: %02x:%02x.%01x",
-                               dev->bus->secondary, 
-                               PCI_SLOT(dev->path.u.pci.devfn), PCI_FUNC(dev->path.u.pci.devfn));
+                               dev->bus->secondary,
+                               PCI_SLOT(dev->path.pci.devfn), PCI_FUNC(dev->path.pci.devfn));
 #endif
                        break;
                case DEVICE_PATH_PNP:
                        sprintf(buffer, "PNP: %04x.%01x",
-                               dev->path.u.pnp.port, dev->path.u.pnp.device);
+                               dev->path.pnp.port, dev->path.pnp.device);
                        break;
                case DEVICE_PATH_I2C:
                        sprintf(buffer, "I2C: %02x:%02x",
                                dev->bus->secondary,
-                               dev->path.u.i2c.device);
+                               dev->path.i2c.device);
                        break;
                case DEVICE_PATH_APIC:
                        sprintf(buffer, "APIC: %02x",
-                               dev->path.u.apic.apic_id);
+                               dev->path.apic.apic_id);
                        break;
                case DEVICE_PATH_PCI_DOMAIN:
                        sprintf(buffer, "PCI_DOMAIN: %04x",
-                               dev->path.u.pci_domain.domain);
+                               dev->path.pci_domain.domain);
                        break;
                case DEVICE_PATH_APIC_CLUSTER:
                        sprintf(buffer, "APIC_CLUSTER: %01x",
-                               dev->path.u.apic_cluster.cluster);
+                               dev->path.apic_cluster.cluster);
                        break;
                case DEVICE_PATH_CPU:
-                       sprintf(buffer, "CPU: %02x", dev->path.u.cpu.id);
+                       sprintf(buffer, "CPU: %02x", dev->path.cpu.id);
                        break;
                case DEVICE_PATH_CPU_BUS:
-                       sprintf(buffer, "CPU_BUS: %02x", dev->path.u.cpu_bus.id);
+                       sprintf(buffer, "CPU_BUS: %02x", dev->path.cpu_bus.id);
                        break;
                default:
-                       printk_err("Unknown device path type: %d\n", dev->path.type);
+                       printk(BIOS_ERR, "Unknown device path type: %d\n", dev->path.type);
                        break;
                }
        }
@@ -211,8 +213,7 @@ const char *dev_path(device_t dev)
 const char *bus_path(struct bus *bus)
 {
        static char buffer[BUS_PATH_MAX];
-       sprintf(buffer, "%s,%d",
-               dev_path(bus->dev), bus->link);
+       sprintf(buffer, "%s,%d", dev_path(bus->dev), bus->link);
        return buffer;
 }
 
@@ -227,32 +228,32 @@ int path_eq(struct device_path *path1, struct device_path *path2)
                        equal = 1;
                        break;
                case DEVICE_PATH_PCI:
-                       equal = (path1->u.pci.devfn == path2->u.pci.devfn);
+                       equal = (path1->pci.devfn == path2->pci.devfn);
                        break;
                case DEVICE_PATH_PNP:
-                       equal = (path1->u.pnp.port == path2->u.pnp.port) &&
-                               (path1->u.pnp.device == path2->u.pnp.device);
+                       equal = (path1->pnp.port == path2->pnp.port) &&
+                               (path1->pnp.device == path2->pnp.device);
                        break;
                case DEVICE_PATH_I2C:
-                       equal = (path1->u.i2c.device == path2->u.i2c.device);
+                       equal = (path1->i2c.device == path2->i2c.device);
                        break;
                case DEVICE_PATH_APIC:
-                       equal = (path1->u.apic.apic_id == path2->u.apic.apic_id);
+                       equal = (path1->apic.apic_id == path2->apic.apic_id);
                        break;
                case DEVICE_PATH_PCI_DOMAIN:
-                       equal = (path1->u.pci_domain.domain == path2->u.pci_domain.domain);
+                       equal = (path1->pci_domain.domain == path2->pci_domain.domain);
                        break;
                case DEVICE_PATH_APIC_CLUSTER:
-                       equal = (path1->u.apic_cluster.cluster == path2->u.apic_cluster.cluster);
+                       equal = (path1->apic_cluster.cluster == path2->apic_cluster.cluster);
                        break;
                case DEVICE_PATH_CPU:
-                       equal = (path1->u.cpu.id == path2->u.cpu.id);
+                       equal = (path1->cpu.id == path2->cpu.id);
                        break;
                case DEVICE_PATH_CPU_BUS:
-                       equal = (path1->u.cpu_bus.id == path2->u.cpu_bus.id);
+                       equal = (path1->cpu_bus.id == path2->cpu_bus.id);
                        break;
                default:
-                       printk_err("Uknown device type: %d\n", path1->type);
+                       printk(BIOS_ERR, "Uknown device type: %d\n", path1->type);
                        break;
                }
        }
@@ -272,7 +273,8 @@ void compact_resources(device_t dev)
        for(i = 0; i < dev->resources;) {
                resource = &dev->resource[i];
                if (!resource->flags) {
-                       memmove(resource, resource + 1, dev->resources - i);
+                       memmove(resource, resource + 1, (dev->resources - i) *
+                               sizeof(*resource));
                        dev->resources -= 1;
                        memset(&dev->resource[dev->resources], 0, sizeof(*resource));
                } else {
@@ -353,7 +355,7 @@ struct resource *find_resource(device_t dev, unsigned index)
        /* See if there is a resource with the appropriate index */
        resource = probe_resource(dev, index);
        if (!resource) {
-               printk_emerg("%s missing resource: %02x\n",
+               printk(BIOS_EMERG, "%s missing resource: %02x\n",
                        dev_path(dev), index);
                die("");
        }
@@ -406,7 +408,7 @@ resource_t resource_end(struct resource *resource)
         * the bridge.  While the granularity is simply how many low bits of the
         * address cannot be set.
         */
-       
+
        /* Get the end (rounded up) */
        end = base + align_up(resource->size, resource->gran) - 1;
 
@@ -454,20 +456,20 @@ const char *resource_type(struct resource *resource)
 void report_resource_stored(device_t dev, struct resource *resource, const char *comment)
 {
        if (resource->flags & IORESOURCE_STORED) {
-               unsigned char buf[10];
+               char buf[10];
                unsigned long long base, end;
                base = resource->base;
                end = resource_end(resource);
                buf[0] = '\0';
                if (resource->flags & IORESOURCE_PCI_BRIDGE) {
-#if PCI_BUS_SEGN_BITS
+#if CONFIG_PCI_BUS_SEGN_BITS
                        sprintf(buf, "bus %04x:%02x ", dev->bus->secondary>>8, dev->link[0].secondary & 0xff);
 #else
                        sprintf(buf, "bus %02x ", dev->link[0].secondary);
 #endif
                }
-               printk_debug(
-                       "%s %02x <- [0x%010Lx - 0x%010Lx] size 0x%08Lx gran 0x%02x %s%s%s\n",
+               printk(BIOS_DEBUG,
+                       "%s %02lx <- [0x%010Lx - 0x%010Lx] size 0x%08Lx gran 0x%02x %s%s%s\n",
                        dev_path(dev),
                        resource->index,
                        base, end,
@@ -486,7 +488,7 @@ void search_bus_resources(struct bus *bus,
        for(curdev = bus->children; curdev; curdev = curdev->sibling) {
                int i;
                /* Ignore disabled devices */
-               if (!curdev->have_resources) continue;
+               if (!curdev->enabled) continue;
                for(i = 0; i < curdev->resources; i++) {
                        struct resource *resource = &curdev->resource[i];
                        /* If it isn't the right kind of resource ignore it */
@@ -513,7 +515,7 @@ void search_global_resources(
        for(curdev = all_devices; curdev; curdev = curdev->next) {
                int i;
                /* Ignore disabled devices */
-               if (!curdev->have_resources) continue;
+               if (!curdev->enabled) continue;
                for(i = 0; i < curdev->resources; i++) {
                        struct resource *resource = &curdev->resource[i];
                        /* If it isn't the right kind of resource ignore it */
@@ -554,3 +556,143 @@ void disable_children(struct bus *bus)
                dev_set_enabled(child, 0);
        }
 }
+
+static void resource_tree(struct device *root, int debug_level, int depth)
+{
+       int i = 0, link = 0;
+       struct device *child;
+       char indent[30];        /* If your tree has more levels, it's wrong. */
+
+       for (i = 0; i < depth + 1 && i < 29; i++)
+               indent[i] = ' ';
+       indent[i] = '\0';
+
+       do_printk(BIOS_DEBUG, "%s%s links %x child on link 0", indent,
+                 dev_path(root), root->links);
+       do_printk(BIOS_DEBUG, " %s\n", root->link[0].children ?
+                 dev_path(root->link[0].children) : "NULL");
+       for (i = 0; i < root->resources; i++) {
+               do_printk(BIOS_DEBUG,
+                         "%s%s resource base %llx size %llx align %d gran %d limit %llx flags %lx index %lx\n",
+                         indent, dev_path(root), root->resource[i].base,
+                         root->resource[i].size, root->resource[i].align,
+                         root->resource[i].gran, root->resource[i].limit,
+                         root->resource[i].flags, root->resource[i].index);
+       }
+
+       for (link = 0; link < root->links; link++) {
+               for (child = root->link[link].children; child;
+                    child = child->sibling)
+                       resource_tree(child, debug_level, depth + 1);
+       }
+}
+
+void print_resource_tree(struct device * root, int debug_level,
+                        const char *msg)
+{
+       /* Bail if root is null. */
+       if (!root) {
+               do_printk(debug_level, "%s passed NULL for root!\n", __func__);
+               return;
+       }
+
+       /* Bail if not printing to screen. */
+       if (!do_printk(debug_level, "Show resources in subtree (%s)...%s\n",
+                   dev_path(root), msg))
+               return;
+       resource_tree(root, debug_level, 0);
+}
+
+void show_devs_tree(struct device *dev, int debug_level, int depth, int linknum)
+{
+       char depth_str[20] = "";
+       int i;
+       struct device *sibling;
+       for (i = 0; i < depth; i++)
+               depth_str[i] = ' ';
+       depth_str[i] = '\0';
+       do_printk(debug_level, "%s%s: enabled %d, %d resources\n",
+                 depth_str, dev_path(dev), dev->enabled, dev->resources);
+       for (i = 0; i < dev->links; i++) {
+               for (sibling = dev->link[i].children; sibling;
+                    sibling = sibling->sibling)
+                       show_devs_tree(sibling, debug_level, depth + 1, i);
+       }
+}
+
+void show_all_devs_tree(int debug_level, const char *msg)
+{
+       /* Bail if not printing to screen. */
+       if (!do_printk(debug_level, "Show all devs in tree form...%s\n", msg))
+               return;
+       show_devs_tree(all_devices, debug_level, 0, -1);
+}
+
+void show_devs_subtree(struct device *root, int debug_level, const char *msg)
+{
+       /* Bail if not printing to screen. */
+       if (!do_printk(debug_level, "Show all devs in subtree %s...%s\n",
+                   dev_path(root), msg))
+               return;
+       do_printk(debug_level, "%s\n", msg);
+       show_devs_tree(root, debug_level, 0, -1);
+}
+
+void show_all_devs(int debug_level, const char *msg)
+{
+       struct device *dev;
+
+       /* Bail if not printing to screen. */
+       if (!do_printk(debug_level, "Show all devs...%s\n", msg))
+               return;
+       for (dev = all_devices; dev; dev = dev->next) {
+               do_printk(debug_level,
+                         "%s: enabled %d, %d resources\n",
+                         dev_path(dev), dev->enabled,
+                         dev->resources);
+       }
+}
+
+void show_one_resource(int debug_level, struct device *dev,
+                      struct resource *resource, const char *comment)
+{
+       char buf[10];
+       unsigned long long base, end;
+       base = resource->base;
+       end = resource_end(resource);
+       buf[0] = '\0';
+/*
+       if (resource->flags & IORESOURCE_BRIDGE) {
+#if CONFIG_PCI_BUS_SEGN_BITS
+               sprintf(buf, "bus %04x:%02x ", dev->bus->secondary >> 8,
+                       dev->link[0].secondary & 0xff);
+#else
+               sprintf(buf, "bus %02x ", dev->link[0].secondary);
+#endif
+       }
+*/
+       do_printk(debug_level, "%s %02lx <- [0x%010llx - 0x%010llx] "
+                 "size 0x%08Lx gran 0x%02x %s%s%s\n",
+                 dev_path(dev), resource->index, base, end,
+                 resource->size, resource->gran, buf,
+                 resource_type(resource), comment);
+
+}
+
+void show_all_devs_resources(int debug_level, const char* msg)
+{
+       struct device *dev;
+
+       if(!do_printk(debug_level, "Show all devs with resources...%s\n", msg))
+               return;
+
+       for (dev = all_devices; dev; dev = dev->next) {
+               int i;
+               do_printk(debug_level,
+                         "%s: enabled %d, %d resources\n",
+                         dev_path(dev), dev->enabled,
+                         dev->resources);
+               for (i = 0; i < dev->resources; i++)
+                       show_one_resource(debug_level, dev, &dev->resource[i], "");
+       }
+}