1 // SPDX-License-Identifier: GPL-2.0
3 * mmconfig.c - Low-level direct PCI config space access via MMCONFIG
5 * This is an 64bit optimized version that always keeps the full mmconfig
6 * space mapped. This allows lockless config space operation.
9 #define pr_fmt(fmt) "PCI: " fmt
11 #include <linux/pci.h>
12 #include <linux/init.h>
13 #include <linux/acpi.h>
14 #include <linux/bitmap.h>
15 #include <linux/rcupdate.h>
16 #include <asm/e820/api.h>
17 #include <asm/pci_x86.h>
19 static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
21 struct pci_mmcfg_region *cfg = pci_mmconfig_lookup(seg, bus);
24 return cfg->virt + (PCI_MMCFG_BUS_OFFSET(bus) | (devfn << 12));
28 static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
29 unsigned int devfn, int reg, int len, u32 *value)
33 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
34 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
40 addr = pci_dev_base(seg, bus, devfn);
48 *value = mmio_config_readb(addr + reg);
51 *value = mmio_config_readw(addr + reg);
54 *value = mmio_config_readl(addr + reg);
62 static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
63 unsigned int devfn, int reg, int len, u32 value)
67 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
68 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
72 addr = pci_dev_base(seg, bus, devfn);
80 mmio_config_writeb(addr + reg, value);
83 mmio_config_writew(addr + reg, value);
86 mmio_config_writel(addr + reg, value);
94 const struct pci_raw_ops pci_mmcfg = {
95 .read = pci_mmcfg_read,
96 .write = pci_mmcfg_write,
99 static void __iomem *mcfg_ioremap(struct pci_mmcfg_region *cfg)
105 start = cfg->address + PCI_MMCFG_BUS_OFFSET(cfg->start_bus);
106 num_buses = cfg->end_bus - cfg->start_bus + 1;
107 size = PCI_MMCFG_BUS_OFFSET(num_buses);
108 addr = ioremap(start, size);
110 addr -= PCI_MMCFG_BUS_OFFSET(cfg->start_bus);
114 int pci_mmcfg_arch_map(struct pci_mmcfg_region *cfg)
116 cfg->virt = mcfg_ioremap(cfg);
118 pr_err("can't map ECAM at %pR\n", &cfg->res);
125 void pci_mmcfg_arch_unmap(struct pci_mmcfg_region *cfg)
127 if (cfg && cfg->virt) {
128 iounmap(cfg->virt + PCI_MMCFG_BUS_OFFSET(cfg->start_bus));
133 int __init pci_mmcfg_arch_init(void)
135 struct pci_mmcfg_region *cfg;
137 list_for_each_entry(cfg, &pci_mmcfg_list, list)
138 if (pci_mmcfg_arch_map(cfg)) {
139 pci_mmcfg_arch_free();
143 raw_pci_ext_ops = &pci_mmcfg;
148 void __init pci_mmcfg_arch_free(void)
150 struct pci_mmcfg_region *cfg;
152 list_for_each_entry(cfg, &pci_mmcfg_list, list)
153 pci_mmcfg_arch_unmap(cfg);