2 * Copyright (C) 2004 Matthew Wilcox <matthew@wil.cx>
3 * Copyright (C) 2004 Intel Corp.
5 * This code is released under the GNU General Public License version 2.
9 * mmconfig.c - Low-level direct PCI config space access via MMCONFIG
12 #include <linux/pci.h>
13 #include <linux/init.h>
14 #include <linux/acpi.h>
17 #define mmcfg_virt_addr ((void __iomem *) fix_to_virt(FIX_PCIE_MCFG))
19 /* The base address of the last MMCONFIG device accessed */
20 static u32 mmcfg_last_accessed_device;
23 * Functions for accessing PCI configuration space with MMCONFIG accesses
26 static inline void pci_exp_set_dev_base(int bus, int devfn)
28 u32 dev_base = pci_mmcfg_config[0].base_address | (bus << 20) | (devfn << 12);
29 if (dev_base != mmcfg_last_accessed_device) {
30 mmcfg_last_accessed_device = dev_base;
31 set_fixmap_nocache(FIX_PCIE_MCFG, dev_base);
35 static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
36 unsigned int devfn, int reg, int len, u32 *value)
40 if (!value || (bus > 255) || (devfn > 255) || (reg > 4095))
43 spin_lock_irqsave(&pci_config_lock, flags);
45 pci_exp_set_dev_base(bus, devfn);
49 *value = readb(mmcfg_virt_addr + reg);
52 *value = readw(mmcfg_virt_addr + reg);
55 *value = readl(mmcfg_virt_addr + reg);
59 spin_unlock_irqrestore(&pci_config_lock, flags);
64 static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
65 unsigned int devfn, int reg, int len, u32 value)
69 if ((bus > 255) || (devfn > 255) || (reg > 4095))
72 spin_lock_irqsave(&pci_config_lock, flags);
74 pci_exp_set_dev_base(bus, devfn);
78 writeb(value, mmcfg_virt_addr + reg);
81 writew(value, mmcfg_virt_addr + reg);
84 writel(value, mmcfg_virt_addr + reg);
88 spin_unlock_irqrestore(&pci_config_lock, flags);
93 static struct pci_raw_ops pci_mmcfg = {
94 .read = pci_mmcfg_read,
95 .write = pci_mmcfg_write,
98 static int __init pci_mmcfg_init(void)
100 if ((pci_probe & PCI_PROBE_MMCONF) == 0)
103 acpi_table_parse(ACPI_MCFG, acpi_parse_mcfg);
104 if ((pci_mmcfg_config_num == 0) ||
105 (pci_mmcfg_config == NULL) ||
106 (pci_mmcfg_config[0].base_address == 0))
109 /* Kludge for now. Don't use mmconfig on AMD systems because
110 those have some busses where mmconfig doesn't work,
111 and we don't parse ACPI MCFG well enough to handle that.
112 Remove when proper handling is added. */
113 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
116 printk(KERN_INFO "PCI: Using MMCONFIG\n");
117 raw_pci_ops = &pci_mmcfg;
118 pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
124 arch_initcall(pci_mmcfg_init);