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 /* Verify the first 16 busses. We assume that systems with more busses
18 #define PCI_MMCFG_MAX_CHECK_BUS 16
20 /* Static virtual mapping of the MMCONFIG aperture */
22 struct acpi_mcfg_allocation *cfg;
25 static struct mmcfg_virt *pci_mmcfg_virt;
27 static char __iomem *get_virt(unsigned int seg, unsigned bus)
30 struct acpi_mcfg_allocation *cfg;
34 if (cfg_num >= pci_mmcfg_config_num)
36 cfg = pci_mmcfg_virt[cfg_num].cfg;
37 if (cfg->pci_segment != seg)
39 if ((cfg->start_bus_number <= bus) &&
40 (cfg->end_bus_number >= bus))
41 return pci_mmcfg_virt[cfg_num].virt;
44 /* Fall back to type 0 */
48 static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
51 if (seg == 0 && bus < PCI_MMCFG_MAX_CHECK_BUS &&
52 test_bit(32*bus + PCI_SLOT(devfn), pci_mmcfg_fallback_slots))
54 addr = get_virt(seg, bus);
57 return addr + ((bus << 20) | (devfn << 12));
60 static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
61 unsigned int devfn, int reg, int len, u32 *value)
65 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
66 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
71 addr = pci_dev_base(seg, bus, devfn);
73 return pci_conf1_read(seg,bus,devfn,reg,len,value);
77 *value = readb(addr + reg);
80 *value = readw(addr + reg);
83 *value = readl(addr + reg);
90 static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
91 unsigned int devfn, int reg, int len, u32 value)
95 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
96 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
99 addr = pci_dev_base(seg, bus, devfn);
101 return pci_conf1_write(seg,bus,devfn,reg,len,value);
105 writeb(value, addr + reg);
108 writew(value, addr + reg);
111 writel(value, addr + reg);
118 static struct pci_raw_ops pci_mmcfg = {
119 .read = pci_mmcfg_read,
120 .write = pci_mmcfg_write,
123 static void __iomem * __init mcfg_ioremap(struct acpi_mcfg_allocation *cfg)
128 size = (cfg->end_bus_number + 1) << 20;
129 addr = ioremap_nocache(cfg->address, size);
131 printk(KERN_INFO "PCI: Using MMCONFIG at %Lx - %Lx\n",
132 cfg->address, cfg->address + size - 1);
137 int __init pci_mmcfg_arch_init(void)
140 pci_mmcfg_virt = kmalloc(sizeof(*pci_mmcfg_virt) *
141 pci_mmcfg_config_num, GFP_KERNEL);
142 if (pci_mmcfg_virt == NULL) {
143 printk(KERN_ERR "PCI: Can not allocate memory for mmconfig structures\n");
147 for (i = 0; i < pci_mmcfg_config_num; ++i) {
148 pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i];
149 pci_mmcfg_virt[i].virt = mcfg_ioremap(&pci_mmcfg_config[i]);
150 if (!pci_mmcfg_virt[i].virt) {
151 printk(KERN_ERR "PCI: Cannot map mmconfig aperture for "
153 pci_mmcfg_config[i].pci_segment);
157 raw_pci_ops = &pci_mmcfg;