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 /* aperture is up to 256MB but BIOS may reserve less */
17 #define MMCONFIG_APER_MIN (2 * 1024*1024)
18 #define MMCONFIG_APER_MAX (256 * 1024*1024)
20 /* Verify the first 16 busses. We assume that systems with more busses
22 #define PCI_MMCFG_MAX_CHECK_BUS 16
24 /* Static virtual mapping of the MMCONFIG aperture */
26 struct acpi_mcfg_allocation *cfg;
29 static struct mmcfg_virt *pci_mmcfg_virt;
31 static inline int mcfg_broken(void)
33 struct acpi_mcfg_allocation *cfg = &pci_mmcfg_config[0];
35 /* Handle more broken MCFG tables on Asus etc.
36 They only contain a single entry for bus 0-0. Assume
37 this applies to all busses. */
38 if (pci_mmcfg_config_num == 1 &&
39 cfg->pci_segment_group_number == 0 &&
40 (cfg->start_bus_number | cfg->end_bus_number) == 0)
45 static void __iomem *mcfg_ioremap(struct acpi_mcfg_allocation *cfg)
53 size = (cfg->end_bus_number + 1) << 20;
55 addr = ioremap_nocache(cfg->base_address, size);
57 printk(KERN_INFO "PCI: Using MMCONFIG at %x - %x\n",
59 cfg->base_address + size - 1);
64 static char __iomem *get_virt(unsigned int seg, unsigned bus)
67 struct acpi_mcfg_allocation *cfg;
71 if (cfg_num >= pci_mmcfg_config_num)
73 cfg = pci_mmcfg_virt[cfg_num].cfg;
74 if (cfg->pci_segment != seg)
76 if ((cfg->start_bus_number <= bus) &&
77 (cfg->end_bus_number >= bus))
78 return pci_mmcfg_virt[cfg_num].virt;
82 return pci_mmcfg_virt[0].virt;
84 /* Fall back to type 0 */
88 static char __iomem *pci_dev_base(unsigned int seg, unsigned int bus, unsigned int devfn)
91 if (seg == 0 && bus < PCI_MMCFG_MAX_CHECK_BUS &&
92 test_bit(32*bus + PCI_SLOT(devfn), pci_mmcfg_fallback_slots))
94 addr = get_virt(seg, bus);
97 return addr + ((bus << 20) | (devfn << 12));
100 static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
101 unsigned int devfn, int reg, int len, u32 *value)
105 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
106 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095))) {
111 addr = pci_dev_base(seg, bus, devfn);
113 return pci_conf1_read(seg,bus,devfn,reg,len,value);
117 *value = readb(addr + reg);
120 *value = readw(addr + reg);
123 *value = readl(addr + reg);
130 static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
131 unsigned int devfn, int reg, int len, u32 value)
135 /* Why do we have this when nobody checks it. How about a BUG()!? -AK */
136 if (unlikely((bus > 255) || (devfn > 255) || (reg > 4095)))
139 addr = pci_dev_base(seg, bus, devfn);
141 return pci_conf1_write(seg,bus,devfn,reg,len,value);
145 writeb(value, addr + reg);
148 writew(value, addr + reg);
151 writel(value, addr + reg);
158 static struct pci_raw_ops pci_mmcfg = {
159 .read = pci_mmcfg_read,
160 .write = pci_mmcfg_write,
163 int __init pci_mmcfg_arch_init(void)
166 pci_mmcfg_virt = kmalloc(sizeof(*pci_mmcfg_virt) *
167 pci_mmcfg_config_num, GFP_KERNEL);
168 if (pci_mmcfg_virt == NULL) {
169 printk(KERN_ERR "PCI: Can not allocate memory for mmconfig structures\n");
173 for (i = 0; i < pci_mmcfg_config_num; ++i) {
174 pci_mmcfg_virt[i].cfg = &pci_mmcfg_config[i];
175 pci_mmcfg_virt[i].virt = mcfg_ioremap(&pci_mmcfg_config[i]);
176 if (!pci_mmcfg_virt[i].virt) {
177 printk(KERN_ERR "PCI: Cannot map mmconfig aperture for "
179 pci_mmcfg_config[i].pci_segment);
183 raw_pci_ops = &pci_mmcfg;