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 #define PREFIX "PCI: "
17 static char __iomem *get_virt(unsigned int seg, unsigned bus)
19 struct pci_mmcfg_region *cfg;
21 list_for_each_entry(cfg, &pci_mmcfg_list, list)
22 if (cfg->segment == seg &&
23 (cfg->start_bus <= bus) &&
24 (cfg->end_bus >= bus))
27 /* Fall back to type 0 */
31 static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
35 addr = get_virt(seg, bus);
38 return addr + (PCI_MMCFG_BUS_OFFSET(bus) | (devfn << 12));
41 static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
42 unsigned int devfn, int reg, int len, u32 *value)
46 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
47 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
52 addr = pci_dev_base(seg, bus, devfn);
58 *value = mmio_config_readb(addr + reg);
61 *value = mmio_config_readw(addr + reg);
64 *value = mmio_config_readl(addr + reg);
71 static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
72 unsigned int devfn, int reg, int len, u32 value)
76 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
77 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
80 addr = pci_dev_base(seg, bus, devfn);
86 mmio_config_writeb(addr + reg, value);
89 mmio_config_writew(addr + reg, value);
92 mmio_config_writel(addr + reg, value);
99 static struct pci_raw_ops pci_mmcfg = {
100 .read = pci_mmcfg_read,
101 .write = pci_mmcfg_write,
104 static void __iomem * __init mcfg_ioremap(struct pci_mmcfg_region *cfg)
110 start = cfg->address + PCI_MMCFG_BUS_OFFSET(cfg->start_bus);
111 num_buses = cfg->end_bus - cfg->start_bus + 1;
112 size = PCI_MMCFG_BUS_OFFSET(num_buses);
113 addr = ioremap_nocache(start, size);
115 addr -= PCI_MMCFG_BUS_OFFSET(cfg->start_bus);
119 int __init pci_mmcfg_arch_init(void)
121 struct pci_mmcfg_region *cfg;
123 list_for_each_entry(cfg, &pci_mmcfg_list, list) {
124 cfg->virt = mcfg_ioremap(cfg);
126 printk(KERN_ERR PREFIX "can't map MMCONFIG at %pR\n",
128 pci_mmcfg_arch_free();
132 raw_pci_ext_ops = &pci_mmcfg;
136 void __init pci_mmcfg_arch_free(void)
138 struct pci_mmcfg_region *cfg;
140 list_for_each_entry(cfg, &pci_mmcfg_list, list) {
142 iounmap(cfg->virt + PCI_MMCFG_BUS_OFFSET(cfg->start_bus));