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 char __iomem *get_virt(unsigned int seg, unsigned bus)
18 struct pci_mmcfg_region *cfg;
20 for (i = 0; i < pci_mmcfg_config_num; ++i) {
21 cfg = &pci_mmcfg_config[i];
22 if (cfg->segment == seg &&
23 (cfg->start_bus <= bus) &&
24 (cfg->end_bus >= bus))
28 /* Fall back to type 0 */
32 static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
36 addr = get_virt(seg, bus);
39 return addr + (PCI_MMCFG_BUS_OFFSET(bus) | (devfn << 12));
42 static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
43 unsigned int devfn, int reg, int len, u32 *value)
47 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
48 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
53 addr = pci_dev_base(seg, bus, devfn);
59 *value = mmio_config_readb(addr + reg);
62 *value = mmio_config_readw(addr + reg);
65 *value = mmio_config_readl(addr + reg);
72 static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
73 unsigned int devfn, int reg, int len, u32 value)
77 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
78 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
81 addr = pci_dev_base(seg, bus, devfn);
87 mmio_config_writeb(addr + reg, value);
90 mmio_config_writew(addr + reg, value);
93 mmio_config_writel(addr + reg, value);
100 static struct pci_raw_ops pci_mmcfg = {
101 .read = pci_mmcfg_read,
102 .write = pci_mmcfg_write,
105 static void __iomem * __init mcfg_ioremap(struct pci_mmcfg_region *cfg)
111 start = cfg->address + PCI_MMCFG_BUS_OFFSET(cfg->start_bus);
112 num_buses = cfg->end_bus - cfg->start_bus + 1;
113 size = PCI_MMCFG_BUS_OFFSET(num_buses);
114 addr = ioremap_nocache(start, size);
116 printk(KERN_INFO "PCI: Using MMCONFIG at %Lx - %Lx\n",
117 start, start + size - 1);
118 addr -= PCI_MMCFG_BUS_OFFSET(cfg->start_bus);
123 int __init pci_mmcfg_arch_init(void)
126 struct pci_mmcfg_region *cfg;
128 for (i = 0; i < pci_mmcfg_config_num; ++i) {
129 cfg = &pci_mmcfg_config[i];
130 cfg->virt = mcfg_ioremap(cfg);
132 printk(KERN_ERR "PCI: Cannot map mmconfig aperture for "
135 pci_mmcfg_arch_free();
139 raw_pci_ext_ops = &pci_mmcfg;
143 void __init pci_mmcfg_arch_free(void)
146 struct pci_mmcfg_region *cfg;
148 for (i = 0; i < pci_mmcfg_config_num; ++i) {
149 cfg = &pci_mmcfg_config[i];
151 iounmap(cfg->virt + PCI_MMCFG_BUS_OFFSET(cfg->start_bus));