include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / arch / x86 / pci / mmconfig-shared.c
index 9bf04bc..39b9ebe 100644 (file)
@@ -16,7 +16,7 @@
 #include <linux/sfi_acpi.h>
 #include <linux/bitmap.h>
 #include <linux/dmi.h>
-#include <linux/sort.h>
+#include <linux/slab.h>
 #include <asm/e820.h>
 #include <asm/pci_x86.h>
 #include <asm/acpi.h>
 /* Indicate if the mmcfg resources have been placed into the resource table. */
 static int __initdata pci_mmcfg_resources_inserted;
 
-static __init int extend_mmcfg(int num)
+LIST_HEAD(pci_mmcfg_list);
+
+static __init void pci_mmconfig_remove(struct pci_mmcfg_region *cfg)
 {
-       struct acpi_mcfg_allocation *new;
-       int new_num = pci_mmcfg_config_num + num;
+       if (cfg->res.parent)
+               release_resource(&cfg->res);
+       list_del(&cfg->list);
+       kfree(cfg);
+}
 
-       new = kzalloc(sizeof(pci_mmcfg_config[0]) * new_num, GFP_KERNEL);
-       if (!new)
-               return -1;
+static __init void free_all_mmcfg(void)
+{
+       struct pci_mmcfg_region *cfg, *tmp;
+
+       pci_mmcfg_arch_free();
+       list_for_each_entry_safe(cfg, tmp, &pci_mmcfg_list, list)
+               pci_mmconfig_remove(cfg);
+}
 
-       if (pci_mmcfg_config) {
-               memcpy(new, pci_mmcfg_config,
-                        sizeof(pci_mmcfg_config[0]) * new_num);
-               kfree(pci_mmcfg_config);
+static __init void list_add_sorted(struct pci_mmcfg_region *new)
+{
+       struct pci_mmcfg_region *cfg;
+
+       /* keep list sorted by segment and starting bus number */
+       list_for_each_entry(cfg, &pci_mmcfg_list, list) {
+               if (cfg->segment > new->segment ||
+                   (cfg->segment == new->segment &&
+                    cfg->start_bus >= new->start_bus)) {
+                       list_add_tail(&new->list, &cfg->list);
+                       return;
+               }
        }
-       pci_mmcfg_config = new;
+       list_add_tail(&new->list, &pci_mmcfg_list);
+}
 
-       return 0;
+static __init struct pci_mmcfg_region *pci_mmconfig_add(int segment, int start,
+                                                       int end, u64 addr)
+{
+       struct pci_mmcfg_region *new;
+       int num_buses;
+       struct resource *res;
+
+       if (addr == 0)
+               return NULL;
+
+       new = kzalloc(sizeof(*new), GFP_KERNEL);
+       if (!new)
+               return NULL;
+
+       new->address = addr;
+       new->segment = segment;
+       new->start_bus = start;
+       new->end_bus = end;
+
+       list_add_sorted(new);
+
+       num_buses = end - start + 1;
+       res = &new->res;
+       res->start = addr + PCI_MMCFG_BUS_OFFSET(start);
+       res->end = addr + PCI_MMCFG_BUS_OFFSET(num_buses) - 1;
+       res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
+       snprintf(new->name, PCI_MMCFG_RESOURCE_NAME_LEN,
+                "PCI MMCONFIG %04x [bus %02x-%02x]", segment, start, end);
+       res->name = new->name;
+
+       printk(KERN_INFO PREFIX "MMCONFIG for domain %04x [bus %02x-%02x] at "
+              "%pR (base %#lx)\n", segment, start, end, &new->res,
+              (unsigned long) addr);
+
+       return new;
 }
 
-static __init void fill_one_mmcfg(u64 addr, int segment, int start, int end)
+struct pci_mmcfg_region *pci_mmconfig_lookup(int segment, int bus)
 {
-       int i = pci_mmcfg_config_num;
+       struct pci_mmcfg_region *cfg;
 
-       pci_mmcfg_config_num++;
-       pci_mmcfg_config[i].address = addr;
-       pci_mmcfg_config[i].pci_segment = segment;
-       pci_mmcfg_config[i].start_bus_number = start;
-       pci_mmcfg_config[i].end_bus_number = end;
+       list_for_each_entry(cfg, &pci_mmcfg_list, list)
+               if (cfg->segment == segment &&
+                   cfg->start_bus <= bus && bus <= cfg->end_bus)
+                       return cfg;
+
+       return NULL;
 }
 
 static const char __init *pci_mmcfg_e7520(void)
@@ -65,11 +119,9 @@ static const char __init *pci_mmcfg_e7520(void)
        if (win == 0x0000 || win == 0xf000)
                return NULL;
 
-       if (extend_mmcfg(1) == -1)
+       if (pci_mmconfig_add(0, 0, 255, win << 16) == NULL)
                return NULL;
 
-       fill_one_mmcfg(win << 16, 0, 0, 255);
-
        return "Intel Corporation E7520 Memory Controller Hub";
 }
 
@@ -111,11 +163,9 @@ static const char __init *pci_mmcfg_intel_945(void)
        if ((pciexbar & mask) >= 0xf0000000U)
                return NULL;
 
-       if (extend_mmcfg(1) == -1)
+       if (pci_mmconfig_add(0, 0, (len >> 20) - 1, pciexbar & mask) == NULL)
                return NULL;
 
-       fill_one_mmcfg(pciexbar & mask, 0, 0, (len >> 20) - 1);
-
        return "Intel Corporation 945G/GZ/P/PL Express Memory Controller Hub";
 }
 
@@ -124,7 +174,7 @@ static const char __init *pci_mmcfg_amd_fam10h(void)
        u32 low, high, address;
        u64 base, msr;
        int i;
-       unsigned segnbits = 0, busnbits;
+       unsigned segnbits = 0, busnbits, end_bus;
 
        if (!(pci_probe & PCI_CHECK_ENABLE_AMD_MMCONF))
                return NULL;
@@ -158,11 +208,13 @@ static const char __init *pci_mmcfg_amd_fam10h(void)
                busnbits = 8;
        }
 
-       if (extend_mmcfg(1 << segnbits) == -1)
-               return NULL;
-
+       end_bus = (1 << busnbits) - 1;
        for (i = 0; i < (1 << segnbits); i++)
-               fill_one_mmcfg(base + (1<<28) * i, i, 0, (1 << busnbits) - 1);
+               if (pci_mmconfig_add(i, 0, end_bus,
+                                    base + (1<<28) * i) == NULL) {
+                       free_all_mmcfg();
+                       return NULL;
+               }
 
        return "AMD Family 10h NB";
 }
@@ -187,7 +239,7 @@ static const char __init *pci_mmcfg_nvidia_mcp55(void)
        /*
         * do check if amd fam10h already took over
         */
-       if (!acpi_disabled || pci_mmcfg_config_num || mcp55_checked)
+       if (!acpi_disabled || !list_empty(&pci_mmcfg_list) || mcp55_checked)
                return NULL;
 
        mcp55_checked = true;
@@ -210,16 +262,14 @@ static const char __init *pci_mmcfg_nvidia_mcp55(void)
                if (!(extcfg & extcfg_enable_mask))
                        continue;
 
-               if (extend_mmcfg(1) == -1)
-                       continue;
-
                size_index = (extcfg & extcfg_size_mask) >> extcfg_size_shift;
                base = extcfg & extcfg_base_mask[size_index];
                /* base could > 4G */
                base <<= extcfg_base_lshift;
                start = (extcfg & extcfg_start_mask) >> extcfg_start_shift;
                end = start + extcfg_sizebus[size_index] - 1;
-               fill_one_mmcfg(base, 0, start, end);
+               if (pci_mmconfig_add(0, start, end, base) == NULL)
+                       continue;
                mcp55_mmconf_found++;
        }
 
@@ -250,45 +300,22 @@ static struct pci_mmcfg_hostbridge_probe pci_mmcfg_probes[] __initdata = {
          0x0369, pci_mmcfg_nvidia_mcp55 },
 };
 
-static int __init cmp_mmcfg(const void *x1, const void *x2)
-{
-       const typeof(pci_mmcfg_config[0]) *m1 = x1;
-       const typeof(pci_mmcfg_config[0]) *m2 = x2;
-       int start1, start2;
-
-       start1 = m1->start_bus_number;
-       start2 = m2->start_bus_number;
-
-       return start1 - start2;
-}
-
 static void __init pci_mmcfg_check_end_bus_number(void)
 {
-       int i;
-       typeof(pci_mmcfg_config[0]) *cfg, *cfgx;
-
-       /* sort them at first */
-       sort(pci_mmcfg_config, pci_mmcfg_config_num,
-                sizeof(pci_mmcfg_config[0]), cmp_mmcfg, NULL);
-
-       /* last one*/
-       if (pci_mmcfg_config_num > 0) {
-               i = pci_mmcfg_config_num - 1;
-               cfg = &pci_mmcfg_config[i];
-               if (cfg->end_bus_number < cfg->start_bus_number)
-                       cfg->end_bus_number = 255;
-       }
+       struct pci_mmcfg_region *cfg, *cfgx;
 
-       /* don't overlap please */
-       for (i = 0; i < pci_mmcfg_config_num - 1; i++) {
-               cfg = &pci_mmcfg_config[i];
-               cfgx = &pci_mmcfg_config[i+1];
+       /* Fixup overlaps */
+       list_for_each_entry(cfg, &pci_mmcfg_list, list) {
+               if (cfg->end_bus < cfg->start_bus)
+                       cfg->end_bus = 255;
 
-               if (cfg->end_bus_number < cfg->start_bus_number)
-                       cfg->end_bus_number = 255;
+               /* Don't access the list head ! */
+               if (cfg->list.next == &pci_mmcfg_list)
+                       break;
 
-               if (cfg->end_bus_number >= cfgx->start_bus_number)
-                       cfg->end_bus_number = cfgx->start_bus_number - 1;
+               cfgx = list_entry(cfg->list.next, typeof(*cfg), list);
+               if (cfg->end_bus >= cfgx->start_bus)
+                       cfg->end_bus = cfgx->start_bus - 1;
        }
 }
 
@@ -303,8 +330,7 @@ static int __init pci_mmcfg_check_hostbridge(void)
        if (!raw_pci_ops)
                return 0;
 
-       pci_mmcfg_config_num = 0;
-       pci_mmcfg_config = NULL;
+       free_all_mmcfg();
 
        for (i = 0; i < ARRAY_SIZE(pci_mmcfg_probes); i++) {
                bus =  pci_mmcfg_probes[i].bus;
@@ -319,45 +345,22 @@ static int __init pci_mmcfg_check_hostbridge(void)
                        name = pci_mmcfg_probes[i].probe();
 
                if (name)
-                       printk(KERN_INFO "PCI: Found %s with MMCONFIG support.\n",
+                       printk(KERN_INFO PREFIX "%s with MMCONFIG support\n",
                               name);
        }
 
        /* some end_bus_number is crazy, fix it */
        pci_mmcfg_check_end_bus_number();
 
-       return pci_mmcfg_config_num != 0;
+       return !list_empty(&pci_mmcfg_list);
 }
 
 static void __init pci_mmcfg_insert_resources(void)
 {
-#define PCI_MMCFG_RESOURCE_NAME_LEN 24
-       int i;
-       struct resource *res;
-       char *names;
-       unsigned num_buses;
+       struct pci_mmcfg_region *cfg;
 
-       res = kcalloc(PCI_MMCFG_RESOURCE_NAME_LEN + sizeof(*res),
-                       pci_mmcfg_config_num, GFP_KERNEL);
-       if (!res) {
-               printk(KERN_ERR "PCI: Unable to allocate MMCONFIG resources\n");
-               return;
-       }
-
-       names = (void *)&res[pci_mmcfg_config_num];
-       for (i = 0; i < pci_mmcfg_config_num; i++, res++) {
-               struct acpi_mcfg_allocation *cfg = &pci_mmcfg_config[i];
-               num_buses = cfg->end_bus_number - cfg->start_bus_number + 1;
-               res->name = names;
-               snprintf(names, PCI_MMCFG_RESOURCE_NAME_LEN,
-                        "PCI MMCONFIG %u [%02x-%02x]", cfg->pci_segment,
-                        cfg->start_bus_number, cfg->end_bus_number);
-               res->start = cfg->address + (cfg->start_bus_number << 20);
-               res->end = res->start + (num_buses << 20) - 1;
-               res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
-               insert_resource(&iomem_resource, res);
-               names += PCI_MMCFG_RESOURCE_NAME_LEN;
-       }
+       list_for_each_entry(cfg, &pci_mmcfg_list, list)
+               insert_resource(&iomem_resource, &cfg->res);
 
        /* Mark that the resources have been inserted. */
        pci_mmcfg_resources_inserted = 1;
@@ -434,11 +437,12 @@ static int __init is_acpi_reserved(u64 start, u64 end, unsigned not_used)
 typedef int (*check_reserved_t)(u64 start, u64 end, unsigned type);
 
 static int __init is_mmconf_reserved(check_reserved_t is_reserved,
-               u64 addr, u64 size, int i,
-               typeof(pci_mmcfg_config[0]) *cfg, int with_e820)
+                                   struct pci_mmcfg_region *cfg, int with_e820)
 {
+       u64 addr = cfg->res.start;
+       u64 size = resource_size(&cfg->res);
        u64 old_size = size;
-       int valid = 0;
+       int valid = 0, num_buses;
 
        while (!is_reserved(addr, addr + size, E820_RESERVED)) {
                size >>= 1;
@@ -447,19 +451,25 @@ static int __init is_mmconf_reserved(check_reserved_t is_reserved,
        }
 
        if (size >= (16UL<<20) || size == old_size) {
-               printk(KERN_NOTICE
-                      "PCI: MCFG area at %Lx reserved in %s\n",
-                       addr, with_e820?"E820":"ACPI motherboard resources");
+               printk(KERN_INFO PREFIX "MMCONFIG at %pR reserved in %s\n",
+                      &cfg->res,
+                      with_e820 ? "E820" : "ACPI motherboard resources");
                valid = 1;
 
                if (old_size != size) {
-                       /* update end_bus_number */
-                       cfg->end_bus_number = cfg->start_bus_number + ((size>>20) - 1);
-                       printk(KERN_NOTICE "PCI: updated MCFG configuration %d: base %lx "
-                              "segment %hu buses %u - %u\n",
-                              i, (unsigned long)cfg->address, cfg->pci_segment,
-                              (unsigned int)cfg->start_bus_number,
-                              (unsigned int)cfg->end_bus_number);
+                       /* update end_bus */
+                       cfg->end_bus = cfg->start_bus + ((size>>20) - 1);
+                       num_buses = cfg->end_bus - cfg->start_bus + 1;
+                       cfg->res.end = cfg->res.start +
+                           PCI_MMCFG_BUS_OFFSET(num_buses) - 1;
+                       snprintf(cfg->name, PCI_MMCFG_RESOURCE_NAME_LEN,
+                                "PCI MMCONFIG %04x [bus %02x-%02x]",
+                                cfg->segment, cfg->start_bus, cfg->end_bus);
+                       printk(KERN_INFO PREFIX
+                              "MMCONFIG for %04x [bus%02x-%02x] "
+                              "at %pR (base %#lx) (size reduced!)\n",
+                              cfg->segment, cfg->start_bus, cfg->end_bus,
+                              &cfg->res, (unsigned long) cfg->address);
                }
        }
 
@@ -468,45 +478,26 @@ static int __init is_mmconf_reserved(check_reserved_t is_reserved,
 
 static void __init pci_mmcfg_reject_broken(int early)
 {
-       typeof(pci_mmcfg_config[0]) *cfg;
-       int i;
-
-       if ((pci_mmcfg_config_num == 0) ||
-           (pci_mmcfg_config == NULL) ||
-           (pci_mmcfg_config[0].address == 0))
-               return;
+       struct pci_mmcfg_region *cfg;
 
-       for (i = 0; i < pci_mmcfg_config_num; i++) {
+       list_for_each_entry(cfg, &pci_mmcfg_list, list) {
                int valid = 0;
-               u64 addr, size;
-
-               cfg = &pci_mmcfg_config[i];
-               addr = cfg->start_bus_number;
-               addr <<= 20;
-               addr += cfg->address;
-               size = cfg->end_bus_number + 1 - cfg->start_bus_number;
-               size <<= 20;
-               printk(KERN_NOTICE "PCI: MCFG configuration %d: base %lx "
-                      "segment %hu buses %u - %u\n",
-                      i, (unsigned long)cfg->address, cfg->pci_segment,
-                      (unsigned int)cfg->start_bus_number,
-                      (unsigned int)cfg->end_bus_number);
 
                if (!early && !acpi_disabled)
-                       valid = is_mmconf_reserved(is_acpi_reserved, addr, size, i, cfg, 0);
+                       valid = is_mmconf_reserved(is_acpi_reserved, cfg, 0);
 
                if (valid)
                        continue;
 
                if (!early)
-                       printk(KERN_ERR "PCI: BIOS Bug: MCFG area at %Lx is not"
-                              " reserved in ACPI motherboard resources\n",
-                              cfg->address);
+                       printk(KERN_ERR FW_BUG PREFIX
+                              "MMCONFIG at %pR not reserved in "
+                              "ACPI motherboard resources\n", &cfg->res);
 
                /* Don't try to do this check unless configuration
                   type 1 is available. how about type 2 ?*/
                if (raw_pci_ops)
-                       valid = is_mmconf_reserved(e820_all_mapped, addr, size, i, cfg, 1);
+                       valid = is_mmconf_reserved(e820_all_mapped, cfg, 1);
 
                if (!valid)
                        goto reject;
@@ -515,19 +506,12 @@ static void __init pci_mmcfg_reject_broken(int early)
        return;
 
 reject:
-       printk(KERN_INFO "PCI: Not using MMCONFIG.\n");
-       pci_mmcfg_arch_free();
-       kfree(pci_mmcfg_config);
-       pci_mmcfg_config = NULL;
-       pci_mmcfg_config_num = 0;
+       printk(KERN_INFO PREFIX "not using MMCONFIG\n");
+       free_all_mmcfg();
 }
 
 static int __initdata known_bridge;
 
-/* The physical address of the MMCONFIG aperture.  Set from ACPI tables. */
-struct acpi_mcfg_allocation *pci_mmcfg_config;
-int pci_mmcfg_config_num;
-
 static int __init acpi_mcfg_check_entry(struct acpi_table_mcfg *mcfg,
                                        struct acpi_mcfg_allocation *cfg)
 {
@@ -545,7 +529,7 @@ static int __init acpi_mcfg_check_entry(struct acpi_table_mcfg *mcfg,
                        return 0;
        }
 
-       printk(KERN_ERR PREFIX "MCFG region for %04x:%02x-%02x at %#llx "
+       printk(KERN_ERR PREFIX "MCFG region for %04x [bus %02x-%02x] at %#llx "
               "is above 4GB, ignored\n", cfg->pci_segment,
               cfg->start_bus_number, cfg->end_bus_number, cfg->address);
        return -EINVAL;
@@ -554,8 +538,9 @@ static int __init acpi_mcfg_check_entry(struct acpi_table_mcfg *mcfg,
 static int __init pci_parse_mcfg(struct acpi_table_header *header)
 {
        struct acpi_table_mcfg *mcfg;
+       struct acpi_mcfg_allocation *cfg_table, *cfg;
        unsigned long i;
-       int config_size;
+       int entries;
 
        if (!header)
                return -EINVAL;
@@ -563,33 +548,33 @@ static int __init pci_parse_mcfg(struct acpi_table_header *header)
        mcfg = (struct acpi_table_mcfg *)header;
 
        /* how many config structures do we have */
-       pci_mmcfg_config_num = 0;
+       free_all_mmcfg();
+       entries = 0;
        i = header->length - sizeof(struct acpi_table_mcfg);
        while (i >= sizeof(struct acpi_mcfg_allocation)) {
-               ++pci_mmcfg_config_num;
+               entries++;
                i -= sizeof(struct acpi_mcfg_allocation);
        };
-       if (pci_mmcfg_config_num == 0) {
+       if (entries == 0) {
                printk(KERN_ERR PREFIX "MMCONFIG has no entries\n");
                return -ENODEV;
        }
 
-       config_size = pci_mmcfg_config_num * sizeof(*pci_mmcfg_config);
-       pci_mmcfg_config = kmalloc(config_size, GFP_KERNEL);
-       if (!pci_mmcfg_config) {
-               printk(KERN_WARNING PREFIX
-                      "No memory for MCFG config tables\n");
-               return -ENOMEM;
-       }
-
-       memcpy(pci_mmcfg_config, &mcfg[1], config_size);
-
-       for (i = 0; i < pci_mmcfg_config_num; ++i) {
-               if (acpi_mcfg_check_entry(mcfg, &pci_mmcfg_config[i])) {
-                       kfree(pci_mmcfg_config);
-                       pci_mmcfg_config_num = 0;
+       cfg_table = (struct acpi_mcfg_allocation *) &mcfg[1];
+       for (i = 0; i < entries; i++) {
+               cfg = &cfg_table[i];
+               if (acpi_mcfg_check_entry(mcfg, cfg)) {
+                       free_all_mmcfg();
                        return -ENODEV;
                }
+
+               if (pci_mmconfig_add(cfg->pci_segment, cfg->start_bus_number,
+                                  cfg->end_bus_number, cfg->address) == NULL) {
+                       printk(KERN_WARNING PREFIX
+                              "no memory for MCFG entries\n");
+                       free_all_mmcfg();
+                       return -ENOMEM;
+               }
        }
 
        return 0;
@@ -619,9 +604,7 @@ static void __init __pci_mmcfg_init(int early)
 
        pci_mmcfg_reject_broken(early);
 
-       if ((pci_mmcfg_config_num == 0) ||
-           (pci_mmcfg_config == NULL) ||
-           (pci_mmcfg_config[0].address == 0))
+       if (list_empty(&pci_mmcfg_list))
                return;
 
        if (pci_mmcfg_arch_init())
@@ -653,9 +636,7 @@ static int __init pci_mmcfg_late_insert_resources(void)
         */
        if ((pci_mmcfg_resources_inserted == 1) ||
            (pci_probe & PCI_PROBE_MMCONF) == 0 ||
-           (pci_mmcfg_config_num == 0) ||
-           (pci_mmcfg_config == NULL) ||
-           (pci_mmcfg_config[0].address == 0))
+           list_empty(&pci_mmcfg_list))
                return 1;
 
        /*