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