]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blobdiff - drivers/pci/setup-bus.c
PCI: add failed_list to pci_bus_assign_resources
[mirror_ubuntu-bionic-kernel.git] / drivers / pci / setup-bus.c
index c48cd377b3f56451731ed72d6953caaea349f8bd..7e87ea8f320006cd39e8be30c8fc71aa5ccc3728 100644 (file)
 #include <linux/slab.h>
 #include "pci.h"
 
-static void pbus_assign_resources_sorted(const struct pci_bus *bus)
+struct resource_list_x {
+       struct resource_list_x *next;
+       struct resource *res;
+       struct pci_dev *dev;
+       resource_size_t start;
+       resource_size_t end;
+       unsigned long flags;
+};
+
+static void add_to_failed_list(struct resource_list_x *head,
+                                struct pci_dev *dev, struct resource *res)
+{
+       struct resource_list_x *list = head;
+       struct resource_list_x *ln = list->next;
+       struct resource_list_x *tmp;
+
+       tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
+       if (!tmp) {
+               pr_warning("add_to_failed_list: kmalloc() failed!\n");
+               return;
+       }
+
+       tmp->next = ln;
+       tmp->res = res;
+       tmp->dev = dev;
+       tmp->start = res->start;
+       tmp->end = res->end;
+       tmp->flags = res->flags;
+       list->next = tmp;
+}
+
+static void free_failed_list(struct resource_list_x *head)
+{
+       struct resource_list_x *list, *tmp;
+
+       for (list = head->next; list;) {
+               tmp = list;
+               list = list->next;
+               kfree(tmp);
+       }
+
+       head->next = NULL;
+}
+
+static void pbus_assign_resources_sorted(const struct pci_bus *bus,
+                                        struct resource_list_x *fail_head)
 {
        struct pci_dev *dev;
        struct resource *res;
@@ -58,6 +103,8 @@ static void pbus_assign_resources_sorted(const struct pci_bus *bus)
                res = list->res;
                idx = res - &list->dev->resource[0];
                if (pci_assign_resource(list->dev, idx)) {
+                       if (fail_head && !pci_is_root_bus(list->dev->bus))
+                               add_to_failed_list(fail_head, list->dev, res);
                        res->start = 0;
                        res->end = 0;
                        res->flags = 0;
@@ -134,18 +181,12 @@ EXPORT_SYMBOL(pci_setup_cardbus);
    config space writes, so it's quite possible that an I/O window of
    the bridge will have some undesirable address (e.g. 0) after the
    first write. Ditto 64-bit prefetchable MMIO.  */
-static void pci_setup_bridge(struct pci_bus *bus)
+static void pci_setup_bridge_io(struct pci_bus *bus)
 {
        struct pci_dev *bridge = bus->self;
        struct resource *res;
        struct pci_bus_region region;
-       u32 l, bu, lu, io_upper16;
-
-       if (pci_is_enabled(bridge))
-               return;
-
-       dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n",
-                bus->secondary, bus->subordinate);
+       u32 l, io_upper16;
 
        /* Set up the top and bottom of the PCI I/O segment for this bus. */
        res = bus->resource[0];
@@ -158,8 +199,7 @@ static void pci_setup_bridge(struct pci_bus *bus)
                /* Set up upper 16 bits of I/O base/limit. */
                io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
                dev_info(&bridge->dev, "  bridge window %pR\n", res);
-       }
-       else {
+       } else {
                /* Clear upper 16 bits of I/O base/limit. */
                io_upper16 = 0;
                l = 0x00f0;
@@ -171,21 +211,35 @@ static void pci_setup_bridge(struct pci_bus *bus)
        pci_write_config_dword(bridge, PCI_IO_BASE, l);
        /* Update upper 16 bits of I/O base/limit. */
        pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
+}
+
+static void pci_setup_bridge_mmio(struct pci_bus *bus)
+{
+       struct pci_dev *bridge = bus->self;
+       struct resource *res;
+       struct pci_bus_region region;
+       u32 l;
 
-       /* Set up the top and bottom of the PCI Memory segment
-          for this bus. */
+       /* Set up the top and bottom of the PCI Memory segment for this bus. */
        res = bus->resource[1];
        pcibios_resource_to_bus(bridge, &region, res);
        if (res->flags & IORESOURCE_MEM) {
                l = (region.start >> 16) & 0xfff0;
                l |= region.end & 0xfff00000;
                dev_info(&bridge->dev, "  bridge window %pR\n", res);
-       }
-       else {
+       } else {
                l = 0x0000fff0;
                dev_info(&bridge->dev, "  bridge window [mem disabled]\n");
        }
        pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
+}
+
+static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
+{
+       struct pci_dev *bridge = bus->self;
+       struct resource *res;
+       struct pci_bus_region region;
+       u32 l, bu, lu;
 
        /* Clear out the upper 32 bits of PREF limit.
           If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
@@ -204,8 +258,7 @@ static void pci_setup_bridge(struct pci_bus *bus)
                        lu = upper_32_bits(region.end);
                }
                dev_info(&bridge->dev, "  bridge window %pR\n", res);
-       }
-       else {
+       } else {
                l = 0x0000fff0;
                dev_info(&bridge->dev, "  bridge window [mem pref disabled]\n");
        }
@@ -214,10 +267,38 @@ static void pci_setup_bridge(struct pci_bus *bus)
        /* Set the upper 32 bits of PREF base & limit. */
        pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
        pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
+}
+
+static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
+{
+       struct pci_dev *bridge = bus->self;
+
+       if (pci_is_enabled(bridge))
+               return;
+
+       dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n",
+                bus->secondary, bus->subordinate);
+
+       if (type & IORESOURCE_IO)
+               pci_setup_bridge_io(bus);
+
+       if (type & IORESOURCE_MEM)
+               pci_setup_bridge_mmio(bus);
+
+       if (type & IORESOURCE_PREFETCH)
+               pci_setup_bridge_mmio_pref(bus);
 
        pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
 }
 
+static void pci_setup_bridge(struct pci_bus *bus)
+{
+       unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
+                                 IORESOURCE_PREFETCH;
+
+       __pci_setup_bridge(bus, type);
+}
+
 /* Check whether the bridge supports optional I/O and
    prefetchable memory ranges. If not, the respective
    base/limit registers must be read-only and read as 0. */
@@ -538,19 +619,20 @@ void __ref pci_bus_size_bridges(struct pci_bus *bus)
 }
 EXPORT_SYMBOL(pci_bus_size_bridges);
 
-void __ref pci_bus_assign_resources(const struct pci_bus *bus)
+static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
+                                        struct resource_list_x *fail_head)
 {
        struct pci_bus *b;
        struct pci_dev *dev;
 
-       pbus_assign_resources_sorted(bus);
+       pbus_assign_resources_sorted(bus, fail_head);
 
        list_for_each_entry(dev, &bus->devices, bus_list) {
                b = dev->subordinate;
                if (!b)
                        continue;
 
-               pci_bus_assign_resources(b);
+               __pci_bus_assign_resources(b, fail_head);
 
                switch (dev->class >> 8) {
                case PCI_CLASS_BRIDGE_PCI:
@@ -568,15 +650,103 @@ void __ref pci_bus_assign_resources(const struct pci_bus *bus)
                }
        }
 }
+
+void __ref pci_bus_assign_resources(const struct pci_bus *bus)
+{
+       __pci_bus_assign_resources(bus, NULL);
+}
 EXPORT_SYMBOL(pci_bus_assign_resources);
 
+static void pci_bridge_release_resources(struct pci_bus *bus,
+                                         unsigned long type)
+{
+       int idx;
+       bool changed = false;
+       struct pci_dev *dev;
+       struct resource *r;
+       unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
+                                 IORESOURCE_PREFETCH;
+
+       dev = bus->self;
+       for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
+            idx++) {
+               r = &dev->resource[idx];
+               if ((r->flags & type_mask) != type)
+                       continue;
+               if (!r->parent)
+                       continue;
+               /*
+                * if there are children under that, we should release them
+                *  all
+                */
+               release_child_resources(r);
+               if (!release_resource(r)) {
+                       dev_printk(KERN_DEBUG, &dev->dev,
+                                "resource %d %pR released\n", idx, r);
+                       /* keep the old size */
+                       r->end = resource_size(r) - 1;
+                       r->start = 0;
+                       r->flags = 0;
+                       changed = true;
+               }
+       }
+
+       if (changed) {
+               /* avoiding touch the one without PREF */
+               if (type & IORESOURCE_PREFETCH)
+                       type = IORESOURCE_PREFETCH;
+               __pci_setup_bridge(bus, type);
+       }
+}
+
+enum release_type {
+       leaf_only,
+       whole_subtree,
+};
+/*
+ * try to release pci bridge resources that is from leaf bridge,
+ * so we can allocate big new one later
+ */
+static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
+                                                  unsigned long type,
+                                                  enum release_type rel_type)
+{
+       struct pci_dev *dev;
+       bool is_leaf_bridge = true;
+
+       list_for_each_entry(dev, &bus->devices, bus_list) {
+               struct pci_bus *b = dev->subordinate;
+               if (!b)
+                       continue;
+
+               is_leaf_bridge = false;
+
+               if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
+                       continue;
+
+               if (rel_type == whole_subtree)
+                       pci_bus_release_bridge_resources(b, type,
+                                                whole_subtree);
+       }
+
+       if (pci_is_root_bus(bus))
+               return;
+
+       if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
+               return;
+
+       if ((rel_type == whole_subtree) || is_leaf_bridge)
+               pci_bridge_release_resources(bus, type);
+}
+
 static void pci_bus_dump_res(struct pci_bus *bus)
 {
         int i;
 
         for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
                 struct resource *res = bus->resource[i];
-                if (!res || !res->end)
+
+               if (!res || !res->end || !res->flags)
                         continue;
 
                dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);