x86/pci: AMD one chain system to use pci read out res
[linux-flexiantxendom0.git] / arch / x86 / pci / amd_bus.c
index dbf5323..ae50b8f 100644 (file)
 #include <linux/init.h>
 #include <linux/pci.h>
 #include <linux/topology.h>
-#include "pci.h"
+#include <linux/cpu.h>
+#include <linux/range.h>
+
+#include <asm/pci_x86.h>
 
 #ifdef CONFIG_X86_64
 #include <asm/pci-direct.h>
-#include <asm/mpspec.h>
-#include <linux/cpumask.h>
 #endif
 
+#include "bus_numa.h"
+
 /*
  * This discovers the pcibus <-> node mapping on AMD K8.
  * also get peer root bus resource for io,mmio
  */
 
-#ifdef CONFIG_NUMA
-
-#define BUS_NR 256
-
-#ifdef CONFIG_X86_64
-
-static int mp_bus_to_node[BUS_NR];
-
-void set_mp_bus_to_node(int busnum, int node)
-{
-       if (busnum >= 0 &&  busnum < BUS_NR)
-               mp_bus_to_node[busnum] = node;
-}
-
-int get_mp_bus_to_node(int busnum)
-{
-       int node = -1;
-
-       if (busnum < 0 || busnum > (BUS_NR - 1))
-               return node;
-
-       node = mp_bus_to_node[busnum];
-
-       /*
-        * let numa_node_id to decide it later in dma_alloc_pages
-        * if there is no ram on that node
-        */
-       if (node != -1 && !node_online(node))
-               node = -1;
-
-       return node;
-}
-
-#else /* CONFIG_X86_32 */
-
-static unsigned char mp_bus_to_node[BUS_NR];
-
-void set_mp_bus_to_node(int busnum, int node)
-{
-       if (busnum >= 0 &&  busnum < BUS_NR)
-       mp_bus_to_node[busnum] = (unsigned char) node;
-}
-
-int get_mp_bus_to_node(int busnum)
-{
-       int node;
-
-       if (busnum < 0 || busnum > (BUS_NR - 1))
-               return 0;
-       node = mp_bus_to_node[busnum];
-       return node;
-}
-
-#endif /* CONFIG_X86_32 */
-
-#endif /* CONFIG_NUMA */
-
 #ifdef CONFIG_X86_64
 
-/*
- * sub bus (transparent) will use entres from 3 to store extra from root,
- * so need to make sure have enought slot there, increase PCI_BUS_NUM_RESOURCES?
- */
-#define RES_NUM 16
-struct pci_root_info {
-       char name[12];
-       unsigned int res_num;
-       struct resource res[RES_NUM];
-       int bus_min;
-       int bus_max;
-       int node;
-       int link;
-};
-
-/* 4 at this time, it may become to 32 */
-#define PCI_ROOT_NR 4
-static int pci_root_num;
-static struct pci_root_info pci_root_info[PCI_ROOT_NR];
-
-void set_pci_bus_resources_arch_default(struct pci_bus *b)
-{
-       int i;
-       int j;
-       struct pci_root_info *info;
-
-       /* if only one root bus, don't need to anything */
-       if (pci_root_num < 2)
-               return;
-
-       for (i = 0; i < pci_root_num; i++) {
-               if (pci_root_info[i].bus_min == b->number)
-                       break;
-       }
-
-       if (i == pci_root_num)
-               return;
-
-       info = &pci_root_info[i];
-       for (j = 0; j < info->res_num; j++) {
-               struct resource *res;
-               struct resource *root;
-
-               res = &info->res[j];
-               b->resource[j] = res;
-               if (res->flags & IORESOURCE_IO)
-                       root = &ioport_resource;
-               else
-                       root = &iomem_resource;
-               insert_resource(root, res);
-       }
-}
-
-#define RANGE_NUM 16
-
-struct res_range {
-       size_t start;
-       size_t end;
-};
-
-static void __init update_range(struct res_range *range, size_t start,
-                               size_t end)
-{
-       int i;
-       int j;
-
-       for (j = 0; j < RANGE_NUM; j++) {
-               if (!range[j].end)
-                       continue;
-
-               if (start <= range[j].start && end >= range[j].end) {
-                       range[j].start = 0;
-                       range[j].end = 0;
-                       continue;
-               }
-
-               if (start <= range[j].start && end < range[j].end && range[j].start < end + 1) {
-                       range[j].start = end + 1;
-                       continue;
-               }
-
-
-               if (start > range[j].start && end >= range[j].end && range[j].end > start - 1) {
-                       range[j].end = start - 1;
-                       continue;
-               }
-
-               if (start > range[j].start && end < range[j].end) {
-                       /* find the new spare */
-                       for (i = 0; i < RANGE_NUM; i++) {
-                               if (range[i].end == 0)
-                                       break;
-                       }
-                       if (i < RANGE_NUM) {
-                               range[i].end = range[j].end;
-                               range[i].start = end + 1;
-                       } else {
-                               printk(KERN_ERR "run of slot in ranges\n");
-                       }
-                       range[j].end = start - 1;
-                       continue;
-               }
-       }
-}
-
-static void __init update_res(struct pci_root_info *info, size_t start,
-                             size_t end, unsigned long flags, int merge)
-{
-       int i;
-       struct resource *res;
-
-       if (!merge)
-               goto addit;
-
-       /* try to merge it with old one */
-       for (i = 0; i < info->res_num; i++) {
-               size_t final_start, final_end;
-               size_t common_start, common_end;
-
-               res = &info->res[i];
-               if (res->flags != flags)
-                       continue;
-
-               common_start = max((size_t)res->start, start);
-               common_end = min((size_t)res->end, end);
-               if (common_start > common_end + 1)
-                       continue;
-
-               final_start = min((size_t)res->start, start);
-               final_end = max((size_t)res->end, end);
-
-               res->start = final_start;
-               res->end = final_end;
-               return;
-       }
-
-addit:
-
-       /* need to add that */
-       if (info->res_num >= RES_NUM)
-               return;
-
-       res = &info->res[info->res_num];
-       res->name = info->name;
-       res->flags = flags;
-       res->start = start;
-       res->end = end;
-       res->child = NULL;
-       info->res_num++;
-}
-
 struct pci_hostbridge_probe {
        u32 bus;
        u32 slot;
@@ -266,6 +61,8 @@ static void __init get_pci_mmcfg_amd_fam10h_range(void)
        fam10h_mmconf_end = base + (1ULL<<(segn_busn_bits + 20)) - 1;
 }
 
+#define RANGE_NUM 16
+
 /**
  * early_fill_mp_bus_to_node()
  * called before pcibios_scan_root and pci_scan_bus
@@ -278,7 +75,6 @@ static int __init early_fill_mp_bus_info(void)
        int j;
        unsigned bus;
        unsigned slot;
-       int found;
        int node;
        int link;
        int def_node;
@@ -288,19 +84,15 @@ static int __init early_fill_mp_bus_info(void)
        struct resource *res;
        size_t start;
        size_t end;
-       struct res_range range[RANGE_NUM];
+       struct range range[RANGE_NUM];
        u64 val;
        u32 address;
-
-#ifdef CONFIG_NUMA
-       for (i = 0; i < BUS_NR; i++)
-               mp_bus_to_node[i] = -1;
-#endif
+       bool found;
 
        if (!early_pci_allowed())
                return -1;
 
-       found = 0;
+       found = false;
        for (i = 0; i < ARRAY_SIZE(pci_probes); i++) {
                u32 id;
                u16 device;
@@ -314,7 +106,7 @@ static int __init early_fill_mp_bus_info(void)
                device = (id>>16) & 0xffff;
                if (pci_probes[i].vendor == vendor &&
                    pci_probes[i].device == device) {
-                       found = 1;
+                       found = true;
                        break;
                }
        }
@@ -337,7 +129,7 @@ static int __init early_fill_mp_bus_info(void)
                node = (reg >> 4) & 0x07;
 #ifdef CONFIG_NUMA
                for (j = min_bus; j <= max_bus; j++)
-                       mp_bus_to_node[j] = (unsigned char) node;
+                       set_mp_bus_to_node(j, node);
 #endif
                link = (reg >> 8) & 0x03;
 
@@ -387,7 +179,7 @@ static int __init early_fill_mp_bus_info(void)
                if (end > 0xffff)
                        end = 0xffff;
                update_res(info, start, end, IORESOURCE_IO, 1);
-               update_range(range, start, end);
+               subtract_range(range, RANGE_NUM, start, end);
        }
        /* add left over io port range to def node/link, [0, 0xffff] */
        /* find the position */
@@ -417,14 +209,14 @@ static int __init early_fill_mp_bus_info(void)
        end = (val & 0xffffff800000ULL);
        printk(KERN_INFO "TOM: %016lx aka %ldM\n", end, end>>20);
        if (end < (1ULL<<32))
-               update_range(range, 0, end - 1);
+               subtract_range(range, RANGE_NUM, 0, end - 1);
 
        /* get mmconfig */
        get_pci_mmcfg_amd_fam10h_range();
        /* need to take out mmconf range */
        if (fam10h_mmconf_end) {
                printk(KERN_DEBUG "Fam 10h mmconf [%llx, %llx]\n", fam10h_mmconf_start, fam10h_mmconf_end);
-               update_range(range, fam10h_mmconf_start, fam10h_mmconf_end);
+               subtract_range(range, RANGE_NUM, fam10h_mmconf_start, fam10h_mmconf_end);
        }
 
        /* mmio resource */
@@ -479,7 +271,7 @@ static int __init early_fill_mp_bus_info(void)
                                /* we got a hole */
                                endx = fam10h_mmconf_start - 1;
                                update_res(info, start, endx, IORESOURCE_MEM, 0);
-                               update_range(range, start, endx);
+                               subtract_range(range, RANGE_NUM, start, endx);
                                printk(KERN_CONT " ==> [%llx, %llx]", (u64)start, endx);
                                start = fam10h_mmconf_end + 1;
                                changed = 1;
@@ -495,7 +287,7 @@ static int __init early_fill_mp_bus_info(void)
                }
 
                update_res(info, start, end, IORESOURCE_MEM, 1);
-               update_range(range, start, end);
+               subtract_range(range, RANGE_NUM, start, end);
                printk(KERN_CONT "\n");
        }
 
@@ -510,7 +302,7 @@ static int __init early_fill_mp_bus_info(void)
                rdmsrl(address, val);
                end = (val & 0xffffff800000ULL);
                printk(KERN_INFO "TOM2: %016lx aka %ldM\n", end, end>>20);
-               update_range(range, 1ULL<<32, end - 1);
+               subtract_range(range, RANGE_NUM, 1ULL<<32, end - 1);
        }
 
        /*
@@ -541,7 +333,7 @@ static int __init early_fill_mp_bus_info(void)
                info = &pci_root_info[i];
                res_num = info->res_num;
                busnum = info->bus_min;
-               printk(KERN_DEBUG "bus: [%02x,%02x] on node %x link %x\n",
+               printk(KERN_DEBUG "bus: [%02x, %02x] on node %x link %x\n",
                       info->bus_min, info->bus_max, info->node, info->link);
                for (j = 0; j < res_num; j++) {
                        res = &info->res[j];
@@ -555,15 +347,17 @@ static int __init early_fill_mp_bus_info(void)
        return 0;
 }
 
-postcore_initcall(early_fill_mp_bus_info);
+#else  /* !CONFIG_X86_64 */
 
-#endif
+static int __init early_fill_mp_bus_info(void) { return 0; }
+
+#endif /* !CONFIG_X86_64 */
 
 /* common 32/64 bit code */
 
 #define ENABLE_CF8_EXT_CFG      (1ULL << 46)
 
-static void enable_pci_io_ecs_per_cpu(void *unused)
+static void enable_pci_io_ecs(void *unused)
 {
        u64 reg;
        rdmsrl(MSR_AMD64_NB_CFG, reg);
@@ -573,14 +367,51 @@ static void enable_pci_io_ecs_per_cpu(void *unused)
        }
 }
 
-static int __init enable_pci_io_ecs(void)
+static int __cpuinit amd_cpu_notify(struct notifier_block *self,
+                                   unsigned long action, void *hcpu)
 {
+       int cpu = (long)hcpu;
+       switch (action) {
+       case CPU_ONLINE:
+       case CPU_ONLINE_FROZEN:
+               smp_call_function_single(cpu, enable_pci_io_ecs, NULL, 0);
+               break;
+       default:
+               break;
+       }
+       return NOTIFY_OK;
+}
+
+static struct notifier_block __cpuinitdata amd_cpu_notifier = {
+       .notifier_call  = amd_cpu_notify,
+};
+
+static int __init pci_io_ecs_init(void)
+{
+       int cpu;
+
        /* assume all cpus from fam10h have IO ECS */
         if (boot_cpu_data.x86 < 0x10)
                return 0;
-       on_each_cpu(enable_pci_io_ecs_per_cpu, NULL, 1);
+
+       register_cpu_notifier(&amd_cpu_notifier);
+       for_each_online_cpu(cpu)
+               amd_cpu_notify(&amd_cpu_notifier, (unsigned long)CPU_ONLINE,
+                              (void *)(long)cpu);
        pci_probe |= PCI_HAS_IO_ECS;
+
+       return 0;
+}
+
+static int __init amd_postcore_init(void)
+{
+       if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
+               return 0;
+
+       early_fill_mp_bus_info();
+       pci_io_ecs_init();
+
        return 0;
 }
 
-postcore_initcall(enable_pci_io_ecs);
+postcore_initcall(amd_postcore_init);