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.
9 #include <linux/init.h>
10 #include <linux/acpi.h>
11 #include <linux/bitmap.h>
13 #include <asm/pci_x86.h>
15 /* Static virtual mapping of the MMCONFIG aperture */
17 struct acpi_mcfg_allocation *cfg;
20 static struct mmcfg_virt *pci_mmcfg_virt;
22 static char __iomem *get_virt(unsigned int seg, unsigned bus)
24 struct acpi_mcfg_allocation *cfg;
27 for (cfg_num = 0; cfg_num < pci_mmcfg_config_num; cfg_num++) {
28 cfg = pci_mmcfg_virt[cfg_num].cfg;
29 if (cfg->pci_segment == seg &&
30 (cfg->start_bus_number <= bus) &&
31 (cfg->end_bus_number >= bus))
32 return pci_mmcfg_virt[cfg_num].virt;
35 /* Fall back to type 0 */
39 static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
43 addr = get_virt(seg, bus);
46 return addr + ((bus << 20) | (devfn << 12));
49 static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
50 unsigned int devfn, int reg, int len, u32 *value)
54 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
55 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
60 addr = pci_dev_base(seg, bus, devfn);
66 *value = mmio_config_readb(addr + reg);
69 *value = mmio_config_readw(addr + reg);
72 *value = mmio_config_readl(addr + reg);
79 static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
80 unsigned int devfn, int reg, int len, u32 value)
84 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
85 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
88 addr = pci_dev_base(seg, bus, devfn);
94 mmio_config_writeb(addr + reg, value);
97 mmio_config_writew(addr + reg, value);
100 mmio_config_writel(addr + reg, value);
107 static struct pci_raw_ops pci_mmcfg = {
108 .read = pci_mmcfg_read,
109 .write = pci_mmcfg_write,
112 static void __iomem * __init mcfg_ioremap(struct acpi_mcfg_allocation *cfg)
117 start = cfg->start_bus_number;
119 start += cfg->address;
120 size = cfg->end_bus_number + 1 - cfg->start_bus_number;
122 addr = ioremap_nocache(start, size);
124 printk(KERN_INFO "PCI: Using MMCONFIG at %Lx - %Lx\n",
125 start, start + size - 1);
126 addr -= cfg->start_bus_number << 20;
131 int __init pci_mmcfg_arch_init(void)
134 pci_mmcfg_virt = kzalloc(sizeof(*pci_mmcfg_virt) *
135 pci_mmcfg_config_num, GFP_KERNEL);
136 if (pci_mmcfg_virt == NULL) {
137 printk(KERN_ERR "PCI: Can not allocate memory for mmconfig structures\n");
141 for (i = 0; i < pci_mmcfg_config_num; ++i) {
142 pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i];
143 pci_mmcfg_virt[i].virt = mcfg_ioremap(&pci_mmcfg_config[i]);
144 if (!pci_mmcfg_virt[i].virt) {
145 printk(KERN_ERR "PCI: Cannot map mmconfig aperture for "
147 pci_mmcfg_config[i].pci_segment);
148 pci_mmcfg_arch_free();
152 raw_pci_ext_ops = &pci_mmcfg;
156 void __init pci_mmcfg_arch_free(void)
160 if (pci_mmcfg_virt == NULL)
163 for (i = 0; i < pci_mmcfg_config_num; ++i) {
164 if (pci_mmcfg_virt[i].virt) {
165 iounmap(pci_mmcfg_virt[i].virt + (pci_mmcfg_virt[i].cfg->start_bus_number << 20));
166 pci_mmcfg_virt[i].virt = NULL;
167 pci_mmcfg_virt[i].cfg = NULL;
171 kfree(pci_mmcfg_virt);
172 pci_mmcfg_virt = NULL;