x86/PCI: MMCONFIG: manage pci_mmcfg_region as a list, not a table
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / pci / mmconfig_64.c
blobed1f479b4d0ecdd77a9ecc7921317b530db15c9b
1 /*
2 * mmconfig.c - Low-level direct PCI config space access via MMCONFIG
4 * This is an 64bit optimized version that always keeps the full mmconfig
5 * space mapped. This allows lockless config space operation.
6 */
8 #include <linux/pci.h>
9 #include <linux/init.h>
10 #include <linux/acpi.h>
11 #include <linux/bitmap.h>
12 #include <asm/e820.h>
13 #include <asm/pci_x86.h>
15 static char __iomem *get_virt(unsigned int seg, unsigned bus)
17 struct pci_mmcfg_region *cfg;
19 list_for_each_entry(cfg, &pci_mmcfg_list, list)
20 if (cfg->segment == seg &&
21 (cfg->start_bus <= bus) &&
22 (cfg->end_bus >= bus))
23 return cfg->virt;
25 /* Fall back to type 0 */
26 return NULL;
29 static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
31 char __iomem *addr;
33 addr = get_virt(seg, bus);
34 if (!addr)
35 return NULL;
36 return addr + (PCI_MMCFG_BUS_OFFSET(bus) | (devfn << 12));
39 static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
40 unsigned int devfn, int reg, int len, u32 *value)
42 char __iomem *addr;
44 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
45 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
46 err: *value = -1;
47 return -EINVAL;
50 addr = pci_dev_base(seg, bus, devfn);
51 if (!addr)
52 goto err;
54 switch (len) {
55 case 1:
56 *value = mmio_config_readb(addr + reg);
57 break;
58 case 2:
59 *value = mmio_config_readw(addr + reg);
60 break;
61 case 4:
62 *value = mmio_config_readl(addr + reg);
63 break;
66 return 0;
69 static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
70 unsigned int devfn, int reg, int len, u32 value)
72 char __iomem *addr;
74 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
75 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
76 return -EINVAL;
78 addr = pci_dev_base(seg, bus, devfn);
79 if (!addr)
80 return -EINVAL;
82 switch (len) {
83 case 1:
84 mmio_config_writeb(addr + reg, value);
85 break;
86 case 2:
87 mmio_config_writew(addr + reg, value);
88 break;
89 case 4:
90 mmio_config_writel(addr + reg, value);
91 break;
94 return 0;
97 static struct pci_raw_ops pci_mmcfg = {
98 .read = pci_mmcfg_read,
99 .write = pci_mmcfg_write,
102 static void __iomem * __init mcfg_ioremap(struct pci_mmcfg_region *cfg)
104 void __iomem *addr;
105 u64 start, size;
106 int num_buses;
108 start = cfg->address + PCI_MMCFG_BUS_OFFSET(cfg->start_bus);
109 num_buses = cfg->end_bus - cfg->start_bus + 1;
110 size = PCI_MMCFG_BUS_OFFSET(num_buses);
111 addr = ioremap_nocache(start, size);
112 if (addr) {
113 printk(KERN_INFO "PCI: Using MMCONFIG at %Lx - %Lx\n",
114 start, start + size - 1);
115 addr -= PCI_MMCFG_BUS_OFFSET(cfg->start_bus);
117 return addr;
120 int __init pci_mmcfg_arch_init(void)
122 struct pci_mmcfg_region *cfg;
124 list_for_each_entry(cfg, &pci_mmcfg_list, list) {
125 cfg->virt = mcfg_ioremap(cfg);
126 if (!cfg->virt) {
127 printk(KERN_ERR "PCI: Cannot map mmconfig aperture for "
128 "segment %d\n",
129 cfg->segment);
130 pci_mmcfg_arch_free();
131 return 0;
134 raw_pci_ext_ops = &pci_mmcfg;
135 return 1;
138 void __init pci_mmcfg_arch_free(void)
140 struct pci_mmcfg_region *cfg;
142 list_for_each_entry(cfg, &pci_mmcfg_list, list) {
143 if (cfg->virt) {
144 iounmap(cfg->virt + PCI_MMCFG_BUS_OFFSET(cfg->start_bus));
145 cfg->virt = NULL;