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>
12 #include <linux/rcupdate.h>
14 #include <asm/pci_x86.h>
16 #define PREFIX "PCI: "
18 static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
20 struct pci_mmcfg_region *cfg = pci_mmconfig_lookup(seg, bus);
23 return cfg->virt + (PCI_MMCFG_BUS_OFFSET(bus) | (devfn << 12));
27 static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
28 unsigned int devfn, int reg, int len, u32 *value)
32 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
33 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
39 addr = pci_dev_base(seg, bus, devfn);
47 *value = mmio_config_readb(addr + reg);
50 *value = mmio_config_readw(addr + reg);
53 *value = mmio_config_readl(addr + reg);
61 static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
62 unsigned int devfn, int reg, int len, u32 value)
66 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
67 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
71 addr = pci_dev_base(seg, bus, devfn);
79 mmio_config_writeb(addr + reg, value);
82 mmio_config_writew(addr + reg, value);
85 mmio_config_writel(addr + reg, value);
93 const struct pci_raw_ops pci_mmcfg = {
94 .read = pci_mmcfg_read,
95 .write = pci_mmcfg_write,
98 static void __iomem *mcfg_ioremap(struct pci_mmcfg_region *cfg)
104 start = cfg->address + PCI_MMCFG_BUS_OFFSET(cfg->start_bus);
105 num_buses = cfg->end_bus - cfg->start_bus + 1;
106 size = PCI_MMCFG_BUS_OFFSET(num_buses);
107 addr = ioremap_nocache(start, size);
109 addr -= PCI_MMCFG_BUS_OFFSET(cfg->start_bus);
113 int __init pci_mmcfg_arch_init(void)
115 struct pci_mmcfg_region *cfg;
117 list_for_each_entry(cfg, &pci_mmcfg_list, list)
118 if (pci_mmcfg_arch_map(cfg)) {
119 pci_mmcfg_arch_free();
123 raw_pci_ext_ops = &pci_mmcfg;
128 void __init pci_mmcfg_arch_free(void)
130 struct pci_mmcfg_region *cfg;
132 list_for_each_entry(cfg, &pci_mmcfg_list, list)
133 pci_mmcfg_arch_unmap(cfg);
136 int pci_mmcfg_arch_map(struct pci_mmcfg_region *cfg)
138 cfg->virt = mcfg_ioremap(cfg);
140 pr_err(PREFIX "can't map MMCONFIG at %pR\n", &cfg->res);
147 void pci_mmcfg_arch_unmap(struct pci_mmcfg_region *cfg)
149 if (cfg && cfg->virt) {
150 iounmap(cfg->virt + PCI_MMCFG_BUS_OFFSET(cfg->start_bus));