[PATCH] mmconfig: Reject a broken MCFG tables on Asus etc
[pandora-kernel.git] / arch / x86_64 / pci / mmconfig.c
1 /*
2  * mmconfig.c - Low-level direct PCI config space access via MMCONFIG
3  *
4  * This is an 64bit optimized version that always keeps the full mmconfig
5  * space mapped. This allows lockless config space operation.
6  */
7
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
14 #include "pci.h"
15
16 /* aperture is up to 256MB but BIOS may reserve less */
17 #define MMCONFIG_APER_MIN       (2 * 1024*1024)
18 #define MMCONFIG_APER_MAX       (256 * 1024*1024)
19
20 /* Verify the first 16 busses. We assume that systems with more busses
21    get MCFG right. */
22 #define PCI_MMCFG_MAX_CHECK_BUS 16
23
24 /* Static virtual mapping of the MMCONFIG aperture */
25 struct mmcfg_virt {
26         struct acpi_mcfg_allocation *cfg;
27         char __iomem *virt;
28 };
29 static struct mmcfg_virt *pci_mmcfg_virt;
30
31 static char __iomem *get_virt(unsigned int seg, unsigned bus)
32 {
33         int cfg_num = -1;
34         struct acpi_mcfg_allocation *cfg;
35
36         while (1) {
37                 ++cfg_num;
38                 if (cfg_num >= pci_mmcfg_config_num)
39                         break;
40                 cfg = pci_mmcfg_virt[cfg_num].cfg;
41                 if (cfg->pci_segment != seg)
42                         continue;
43                 if ((cfg->start_bus_number <= bus) &&
44                     (cfg->end_bus_number >= bus))
45                         return pci_mmcfg_virt[cfg_num].virt;
46         }
47
48         /* Fall back to type 0 */
49         return NULL;
50 }
51
52 static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
53 {
54         char __iomem *addr;
55         if (seg == 0 && bus < PCI_MMCFG_MAX_CHECK_BUS &&
56                 test_bit(32*bus + PCI_SLOT(devfn), pci_mmcfg_fallback_slots))
57                 return NULL;
58         addr = get_virt(seg, bus);
59         if (!addr)
60                 return NULL;
61         return addr + ((bus << 20) | (devfn << 12));
62 }
63
64 static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
65                           unsigned int devfn, int reg, int len, u32 *value)
66 {
67         char __iomem *addr;
68
69         /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
70         if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
71                 *value = -1;
72                 return -EINVAL;
73         }
74
75         addr = pci_dev_base(seg, bus, devfn);
76         if (!addr)
77                 return pci_conf1_read(seg,bus,devfn,reg,len,value);
78
79         switch (len) {
80         case 1:
81                 *value = readb(addr + reg);
82                 break;
83         case 2:
84                 *value = readw(addr + reg);
85                 break;
86         case 4:
87                 *value = readl(addr + reg);
88                 break;
89         }
90
91         return 0;
92 }
93
94 static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
95                            unsigned int devfn, int reg, int len, u32 value)
96 {
97         char __iomem *addr;
98
99         /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
100         if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
101                 return -EINVAL;
102
103         addr = pci_dev_base(seg, bus, devfn);
104         if (!addr)
105                 return pci_conf1_write(seg,bus,devfn,reg,len,value);
106
107         switch (len) {
108         case 1:
109                 writeb(value, addr + reg);
110                 break;
111         case 2:
112                 writew(value, addr + reg);
113                 break;
114         case 4:
115                 writel(value, addr + reg);
116                 break;
117         }
118
119         return 0;
120 }
121
122 static struct pci_raw_ops pci_mmcfg = {
123         .read =         pci_mmcfg_read,
124         .write =        pci_mmcfg_write,
125 };
126
127 static void __iomem * __init mcfg_ioremap(struct acpi_mcfg_allocation *cfg)
128 {
129         void __iomem *addr;
130         u32 size;
131
132         size = (cfg->end_bus_number + 1) << 20;
133         addr = ioremap_nocache(cfg->address, size);
134         if (addr) {
135                 printk(KERN_INFO "PCI: Using MMCONFIG at %Lx - %Lx\n",
136                        cfg->address, cfg->address + size - 1);
137         }
138         return addr;
139 }
140
141 int __init pci_mmcfg_arch_init(void)
142 {
143         int i;
144         pci_mmcfg_virt = kmalloc(sizeof(*pci_mmcfg_virt) *
145                                  pci_mmcfg_config_num, GFP_KERNEL);
146         if (pci_mmcfg_virt == NULL) {
147                 printk(KERN_ERR "PCI: Can not allocate memory for mmconfig structures\n");
148                 return 0;
149         }
150
151         for (i = 0; i < pci_mmcfg_config_num; ++i) {
152                 pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i];
153                 pci_mmcfg_virt[i].virt = mcfg_ioremap(&pci_mmcfg_config[i]);
154                 if (!pci_mmcfg_virt[i].virt) {
155                         printk(KERN_ERR "PCI: Cannot map mmconfig aperture for "
156                                         "segment %d\n",
157                                 pci_mmcfg_config[i].pci_segment);
158                         return 0;
159                 }
160         }
161         raw_pci_ops = &pci_mmcfg;
162         return 1;
163 }