+/**
+ * Return an existing resource structure for a given index.
+ *
+ * @param dev The device to find the resource on.
+ * @param index The index of the resource on the device.
+ * return TODO.
+ */
+struct resource *find_resource(device_t dev, unsigned index)
+{
+ struct resource *resource;
+
+ /* See if there is a resource with the appropriate index. */
+ resource = probe_resource(dev, index);
+ if (!resource) {
+ printk(BIOS_EMERG, "%s missing resource: %02x\n",
+ dev_path(dev), index);
+ die("");
+ }
+ return resource;
+}
+
+/**
+ * Round a number up to the next multiple of gran.
+ *
+ * @param val The starting value.
+ * @param gran Granularity we are aligning the number to.
+ * @return The aligned value.
+ */
+static resource_t align_up(resource_t val, unsigned long gran)
+{
+ resource_t mask;
+ mask = (1ULL << gran) - 1ULL;
+ val += mask;
+ val &= ~mask;
+ return val;
+}
+
+/**
+ * Round a number up to the previous multiple of gran.
+ *
+ * @param val The starting value.
+ * @param gran Granularity we are aligning the number to.
+ * @return The aligned value.
+ */
+static resource_t align_down(resource_t val, unsigned long gran)
+{
+ resource_t mask;
+ mask = (1ULL << gran) - 1ULL;
+ val &= ~mask;
+ return val;
+}
+
+/**
+ * Compute the maximum address that is part of a resource.
+ *
+ * @param resource The resource whose limit is desired.
+ * @return The end.
+ */
+resource_t resource_end(struct resource *resource)
+{
+ resource_t base, end;
+
+ /* Get the base address. */
+ base = resource->base;
+
+ /*
+ * For a non bridge resource granularity and alignment are the same.
+ * For a bridge resource align is the largest needed alignment below
+ * 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;
+
+ return end;
+}
+
+/**
+ * Compute the maximum legal value for resource->base.
+ *
+ * @param resource The resource whose maximum is desired.
+ * @return The maximum.
+ */
+resource_t resource_max(struct resource *resource)
+{
+ resource_t max;
+
+ max = align_down(resource->limit - resource->size + 1, resource->align);
+
+ return max;
+}
+
+/**
+ * Return the resource type of a resource.
+ *
+ * @param resource The resource type to decode.
+ * @return TODO.
+ */
+const char *resource_type(struct resource *resource)
+{
+ static char buffer[RESOURCE_TYPE_MAX];
+ sprintf(buffer, "%s%s%s%s",
+ ((resource->flags & IORESOURCE_READONLY) ? "ro" : ""),
+ ((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""),
+ ((resource->flags == 0) ? "unused" :
+ (resource->flags & IORESOURCE_IO) ? "io" :
+ (resource->flags & IORESOURCE_DRQ) ? "drq" :
+ (resource->flags & IORESOURCE_IRQ) ? "irq" :
+ (resource->flags & IORESOURCE_MEM) ? "mem" : "??????"),
+ ((resource->flags & IORESOURCE_PCI64) ? "64" : ""));
+ return buffer;
+}
+
+/**
+ * Print the resource that was just stored.
+ *
+ * @param dev The device the stored resorce lives on.
+ * @param resource The resource that was just stored.
+ * @param comment TODO
+ */
+void report_resource_stored(device_t dev, struct resource *resource,
+ const char *comment)
+{
+ char buf[10];
+ unsigned long long base, end;
+
+ if (!(resource->flags & IORESOURCE_STORED))
+ return;
+
+ base = resource->base;
+ end = resource_end(resource);
+ buf[0] = '\0';
+
+ if (resource->flags & IORESOURCE_PCI_BRIDGE) {
+#if CONFIG_PCI_BUS_SEGN_BITS
+ sprintf(buf, "bus %04x:%02x ", dev->bus->secondary >> 8,
+ dev->link_list->secondary & 0xff);
+#else
+ sprintf(buf, "bus %02x ", dev->link_list->secondary);
+#endif
+ }
+ 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, resource->size, resource->gran, buf,
+ resource_type(resource), comment);
+}
+
+void search_bus_resources(struct bus *bus, unsigned long type_mask,
+ unsigned long type, resource_search_t search,
+ void *gp)
+{
+ struct device *curdev;
+
+ for (curdev = bus->children; curdev; curdev = curdev->sibling) {
+ struct resource *res;
+
+ /* Ignore disabled devices. */
+ if (!curdev->enabled)
+ continue;
+
+ for (res = curdev->resource_list; res; res = res->next) {
+ /* If it isn't the right kind of resource ignore it. */
+ if ((res->flags & type_mask) != type)
+ continue;
+
+ /* If it is a subtractive resource recurse. */
+ if (res->flags & IORESOURCE_SUBTRACTIVE) {
+ struct bus * subbus;
+ for (subbus = curdev->link_list; subbus;
+ subbus = subbus->next)
+ if (subbus->link_num
+ == IOINDEX_SUBTRACTIVE_LINK(res->index))
+ break;
+ search_bus_resources(subbus, type_mask, type,
+ search, gp);
+ continue;
+ }
+ search(gp, curdev, res);
+ }
+ }
+}
+
+void search_global_resources(unsigned long type_mask, unsigned long type,
+ resource_search_t search, void *gp)
+{
+ struct device *curdev;
+
+ for (curdev = all_devices; curdev; curdev = curdev->next) {
+ struct resource *res;
+
+ /* Ignore disabled devices. */
+ if (!curdev->enabled)
+ continue;
+
+ for (res = curdev->resource_list; res; res = res->next) {
+ /* If it isn't the right kind of resource ignore it. */
+ if ((res->flags & type_mask) != type)
+ continue;
+
+ /* If it is a subtractive resource ignore it. */
+ if (res->flags & IORESOURCE_SUBTRACTIVE)
+ continue;
+
+ search(gp, curdev, res);
+ }
+ }
+}
+
+void dev_set_enabled(device_t dev, int enable)
+{
+ if (dev->enabled == enable)
+ return;
+
+ dev->enabled = enable;
+ if (dev->ops && dev->ops->enable) {
+ dev->ops->enable(dev);
+ } else if (dev->chip_ops && dev->chip_ops->enable_dev) {
+ dev->chip_ops->enable_dev(dev);
+ }
+}
+
+void disable_children(struct bus *bus)
+{
+ device_t child;
+
+ for (child = bus->children; child; child = child->sibling) {
+ struct bus *link;
+ for (link = child->link_list; link; link = link->next)
+ disable_children(link);
+ dev_set_enabled(child, 0);
+ }
+}
+
+static void resource_tree(struct device *root, int debug_level, int depth)
+{
+ int i = 0;
+ struct device *child;
+ struct bus *link;
+ struct resource *res;
+ 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", indent, dev_path(root));
+ if (root->link_list && root->link_list->children)
+ do_printk(BIOS_DEBUG, " child on link 0 %s",
+ dev_path(root->link_list->children));
+ do_printk(BIOS_DEBUG, "\n");
+
+ for (res = root->resource_list; res; res = res->next) {
+ do_printk(debug_level, "%s%s resource base %llx size %llx "
+ "align %d gran %d limit %llx flags %lx index %lx\n",
+ indent, dev_path(root), res->base, res->size,
+ res->align, res->gran, res->limit, res->flags,
+ res->index);
+ }
+
+ for (link = root->link_list; link; link = link->next) {
+ for (child = 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;
+ struct bus *link;
+
+ for (i = 0; i < depth; i++)
+ depth_str[i] = ' ';
+ depth_str[i] = '\0';
+
+ do_printk(debug_level, "%s%s: enabled %d\n",
+ depth_str, dev_path(dev), dev->enabled);
+
+ for (link = dev->link_list; link; link = link->next) {
+ for (sibling = link->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\n",
+ dev_path(dev), dev->enabled);
+ }
+}
+
+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) {
+ struct resource *res;
+ do_printk(debug_level, "%s: enabled %d\n",
+ dev_path(dev), dev->enabled);
+ for (res = dev->resource_list; res; res = res->next)
+ show_one_resource(debug_level, dev, res, "");
+ }
+}
+
+void ram_resource(device_t dev, unsigned long index,
+ unsigned long basek, unsigned long sizek)
+{
+ struct resource *resource;
+
+ if (!sizek)
+ return;
+
+ resource = new_resource(dev, index);
+ resource->base = ((resource_t)basek) << 10;
+ resource->size = ((resource_t)sizek) << 10;
+ resource->flags = IORESOURCE_MEM | IORESOURCE_CACHEABLE | \
+ IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
+}
+
+void tolm_test(void *gp, struct device *dev, struct resource *new)
+{
+ struct resource **best_p = gp;
+ struct resource *best;
+
+ best = *best_p;
+
+ if (!best || (best->base > new->base))
+ best = new;
+
+ *best_p = best;
+}
+
+u32 find_pci_tolm(struct bus *bus)
+{
+ struct resource *min = NULL;
+ u32 tolm;
+
+ search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM,
+ tolm_test, &min);
+
+ tolm = 0xffffffffUL;
+
+ if (min && tolm > min->base)
+ tolm = min->base;
+
+ return tolm;
+}