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>
16 /* Static virtual mapping of the MMCONFIG aperture */
18 struct acpi_mcfg_allocation *cfg;
21 static struct mmcfg_virt *pci_mmcfg_virt;
23 static char __iomem *get_virt(unsigned int seg, unsigned bus)
25 struct acpi_mcfg_allocation *cfg;
28 for (cfg_num = 0; cfg_num < pci_mmcfg_config_num; cfg_num++) {
29 cfg = pci_mmcfg_virt[cfg_num].cfg;
30 if (cfg->pci_segment == seg &&
31 (cfg->start_bus_number <= bus) &&
32 (cfg->end_bus_number >= bus))
33 return pci_mmcfg_virt[cfg_num].virt;
36 /* Fall back to type 0 */
40 static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
44 addr = get_virt(seg, bus);
47 return addr + ((bus << 20) | (devfn << 12));
50 static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
51 unsigned int devfn, int reg, int len, u32 *value)
55 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
56 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
62 return pci_conf1_read(seg,bus,devfn,reg,len,value);
64 addr = pci_dev_base(seg, bus, devfn);
70 *value = mmio_config_readb(addr + reg);
73 *value = mmio_config_readw(addr + reg);
76 *value = mmio_config_readl(addr + reg);
83 static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
84 unsigned int devfn, int reg, int len, u32 value)
88 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
89 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
93 return pci_conf1_write(seg,bus,devfn,reg,len,value);
95 addr = pci_dev_base(seg, bus, devfn);
101 mmio_config_writeb(addr + reg, value);
104 mmio_config_writew(addr + reg, value);
107 mmio_config_writel(addr + reg, value);
114 static struct pci_raw_ops pci_mmcfg = {
115 .read = pci_mmcfg_read,
116 .write = pci_mmcfg_write,
119 static void __iomem * __init mcfg_ioremap(struct acpi_mcfg_allocation *cfg)
124 size = (cfg->end_bus_number + 1) << 20;
125 addr = ioremap_nocache(cfg->address, size);
127 printk(KERN_INFO "PCI: Using MMCONFIG at %Lx - %Lx\n",
128 cfg->address, cfg->address + size - 1);
133 int __init pci_mmcfg_arch_init(void)
136 pci_mmcfg_virt = kmalloc(sizeof(*pci_mmcfg_virt) *
137 pci_mmcfg_config_num, GFP_KERNEL);
138 if (pci_mmcfg_virt == NULL) {
139 printk(KERN_ERR "PCI: Can not allocate memory for mmconfig structures\n");
143 for (i = 0; i < pci_mmcfg_config_num; ++i) {
144 pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i];
145 pci_mmcfg_virt[i].virt = mcfg_ioremap(&pci_mmcfg_config[i]);
146 if (!pci_mmcfg_virt[i].virt) {
147 printk(KERN_ERR "PCI: Cannot map mmconfig aperture for "
149 pci_mmcfg_config[i].pci_segment);
153 raw_pci_ops = &pci_mmcfg;