[PATCH] mmconfig: Share parts of mmconfig code between i386 and x86-64
[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         /* Handle more broken MCFG tables on Asus etc.
49            They only contain a single entry for bus 0-0. Assume
50            this applies to all busses. */
51         cfg = &pci_mmcfg_config[0];
52         if (pci_mmcfg_config_num == 1 &&
53                 cfg->pci_segment == 0 &&
54                 (cfg->start_bus_number | cfg->end_bus_number) == 0)
55                 return pci_mmcfg_virt[0].virt;
56
57         /* Fall back to type 0 */
58         return NULL;
59 }
60
61 static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
62 {
63         char __iomem *addr;
64         if (seg == 0 && bus < PCI_MMCFG_MAX_CHECK_BUS &&
65                 test_bit(32*bus + PCI_SLOT(devfn), pci_mmcfg_fallback_slots))
66                 return NULL;
67         addr = get_virt(seg, bus);
68         if (!addr)
69                 return NULL;
70         return addr + ((bus << 20) | (devfn << 12));
71 }
72
73 static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
74                           unsigned int devfn, int reg, int len, u32 *value)
75 {
76         char __iomem *addr;
77
78         /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
79         if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
80                 *value = -1;
81                 return -EINVAL;
82         }
83
84         addr = pci_dev_base(seg, bus, devfn);
85         if (!addr)
86                 return pci_conf1_read(seg,bus,devfn,reg,len,value);
87
88         switch (len) {
89         case 1:
90                 *value = readb(addr + reg);
91                 break;
92         case 2:
93                 *value = readw(addr + reg);
94                 break;
95         case 4:
96                 *value = readl(addr + reg);
97                 break;
98         }
99
100         return 0;
101 }
102
103 static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
104                            unsigned int devfn, int reg, int len, u32 value)
105 {
106         char __iomem *addr;
107
108         /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
109         if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
110                 return -EINVAL;
111
112         addr = pci_dev_base(seg, bus, devfn);
113         if (!addr)
114                 return pci_conf1_write(seg,bus,devfn,reg,len,value);
115
116         switch (len) {
117         case 1:
118                 writeb(value, addr + reg);
119                 break;
120         case 2:
121                 writew(value, addr + reg);
122                 break;
123         case 4:
124                 writel(value, addr + reg);
125                 break;
126         }
127
128         return 0;
129 }
130
131 static struct pci_raw_ops pci_mmcfg = {
132         .read =         pci_mmcfg_read,
133         .write =        pci_mmcfg_write,
134 };
135
136 int __init pci_mmcfg_arch_init(void)
137 {
138         int i;
139         pci_mmcfg_virt = kmalloc(sizeof(*pci_mmcfg_virt) *
140                                  pci_mmcfg_config_num, GFP_KERNEL);
141         if (pci_mmcfg_virt == NULL) {
142                 printk(KERN_ERR "PCI: Can not allocate memory for mmconfig structures\n");
143                 return 0;
144         }
145
146         for (i = 0; i < pci_mmcfg_config_num; ++i) {
147                 pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i];
148                 pci_mmcfg_virt[i].virt = ioremap_nocache(pci_mmcfg_config[i].address,
149                                                          MMCONFIG_APER_MAX);
150                 if (!pci_mmcfg_virt[i].virt) {
151                         printk(KERN_ERR "PCI: Cannot map mmconfig aperture for "
152                                         "segment %d\n",
153                                 pci_mmcfg_config[i].pci_segment);
154                         return 0;
155                 }
156                 printk(KERN_INFO "PCI: Using MMCONFIG at %Lx\n",
157                                 pci_mmcfg_config[i].address);
158         }
159         raw_pci_ops = &pci_mmcfg;
160         return 1;
161 }