]> bbs.cooldavid.org Git - net-next-2.6.git/blob - arch/x86/pci/mmconfig_64.c
ed1f479b4d0ecdd77a9ecc7921317b530db15c9b
[net-next-2.6.git] / arch / x86 / pci / mmconfig_64.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 #include <asm/pci_x86.h>
14
15 static char __iomem *get_virt(unsigned int seg, unsigned bus)
16 {
17         struct pci_mmcfg_region *cfg;
18
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;
24
25         /* Fall back to type 0 */
26         return NULL;
27 }
28
29 static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
30 {
31         char __iomem *addr;
32
33         addr = get_virt(seg, bus);
34         if (!addr)
35                 return NULL;
36         return addr + (PCI_MMCFG_BUS_OFFSET(bus) | (devfn << 12));
37 }
38
39 static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
40                           unsigned int devfn, int reg, int len, u32 *value)
41 {
42         char __iomem *addr;
43
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;
48         }
49
50         addr = pci_dev_base(seg, bus, devfn);
51         if (!addr)
52                 goto err;
53
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;
64         }
65
66         return 0;
67 }
68
69 static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
70                            unsigned int devfn, int reg, int len, u32 value)
71 {
72         char __iomem *addr;
73
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;
77
78         addr = pci_dev_base(seg, bus, devfn);
79         if (!addr)
80                 return -EINVAL;
81
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;
92         }
93
94         return 0;
95 }
96
97 static struct pci_raw_ops pci_mmcfg = {
98         .read =         pci_mmcfg_read,
99         .write =        pci_mmcfg_write,
100 };
101
102 static void __iomem * __init mcfg_ioremap(struct pci_mmcfg_region *cfg)
103 {
104         void __iomem *addr;
105         u64 start, size;
106         int num_buses;
107
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);
116         }
117         return addr;
118 }
119
120 int __init pci_mmcfg_arch_init(void)
121 {
122         struct pci_mmcfg_region *cfg;
123
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;
132                 }
133         }
134         raw_pci_ext_ops = &pci_mmcfg;
135         return 1;
136 }
137
138 void __init pci_mmcfg_arch_free(void)
139 {
140         struct pci_mmcfg_region *cfg;
141
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;
146                 }
147         }
148 }