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>
18 #define MMCONFIG_APER_SIZE (256*1024*1024)
20 /* Assume systems with more busses have correct MCFG */
21 #define MAX_CHECK_BUS 16
23 #define mmcfg_virt_addr ((void __iomem *) fix_to_virt(FIX_PCIE_MCFG))
25 /* The base address of the last MMCONFIG device accessed */
26 static u32 mmcfg_last_accessed_device;
28 static DECLARE_BITMAP(fallback_slots, MAX_CHECK_BUS*32);
31 * Functions for accessing PCI configuration space with MMCONFIG accesses
33 static u32 get_base_addr(unsigned int seg, int bus, unsigned devfn)
36 struct acpi_table_mcfg_config *cfg;
38 if (seg == 0 && bus < MAX_CHECK_BUS &&
39 test_bit(PCI_SLOT(devfn) + 32*bus, fallback_slots))
44 if (cfg_num >= pci_mmcfg_config_num) {
47 cfg = &pci_mmcfg_config[cfg_num];
48 if (cfg->pci_segment_group_number != seg)
50 if ((cfg->start_bus_number <= bus) &&
51 (cfg->end_bus_number >= bus))
52 return cfg->base_address;
55 /* Handle more broken MCFG tables on Asus etc.
56 They only contain a single entry for bus 0-0. Assume
57 this applies to all busses. */
58 cfg = &pci_mmcfg_config[0];
59 if (pci_mmcfg_config_num == 1 &&
60 cfg->pci_segment_group_number == 0 &&
61 (cfg->start_bus_number | cfg->end_bus_number) == 0)
62 return cfg->base_address;
64 /* Fall back to type 0 */
68 static inline void pci_exp_set_dev_base(unsigned int base, int bus, int devfn)
70 u32 dev_base = base | (bus << 20) | (devfn << 12);
71 if (dev_base != mmcfg_last_accessed_device) {
72 mmcfg_last_accessed_device = dev_base;
73 set_fixmap_nocache(FIX_PCIE_MCFG, dev_base);
77 static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
78 unsigned int devfn, int reg, int len, u32 *value)
83 if ((bus > 255) || (devfn > 255) || (reg > 4095)) {
88 base = get_base_addr(seg, bus, devfn);
90 return pci_conf1_read(seg,bus,devfn,reg,len,value);
92 spin_lock_irqsave(&pci_config_lock, flags);
94 pci_exp_set_dev_base(base, bus, devfn);
98 *value = readb(mmcfg_virt_addr + reg);
101 *value = readw(mmcfg_virt_addr + reg);
104 *value = readl(mmcfg_virt_addr + reg);
108 spin_unlock_irqrestore(&pci_config_lock, flags);
113 static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
114 unsigned int devfn, int reg, int len, u32 value)
119 if ((bus > 255) || (devfn > 255) || (reg > 4095))
122 base = get_base_addr(seg, bus, devfn);
124 return pci_conf1_write(seg,bus,devfn,reg,len,value);
126 spin_lock_irqsave(&pci_config_lock, flags);
128 pci_exp_set_dev_base(base, bus, devfn);
132 writeb(value, mmcfg_virt_addr + reg);
135 writew(value, mmcfg_virt_addr + reg);
138 writel(value, mmcfg_virt_addr + reg);
142 spin_unlock_irqrestore(&pci_config_lock, flags);
147 static struct pci_raw_ops pci_mmcfg = {
148 .read = pci_mmcfg_read,
149 .write = pci_mmcfg_write,
152 /* K8 systems have some devices (typically in the builtin northbridge)
153 that are only accessible using type1
154 Normally this can be expressed in the MCFG by not listing them
155 and assigning suitable _SEGs, but this isn't implemented in some BIOS.
156 Instead try to discover all devices on bus 0 that are unreachable using MM
157 and fallback for them. */
158 static __init void unreachable_devices(void)
163 for (k = 0; k < MAX_CHECK_BUS; k++) {
164 for (i = 0; i < 32; i++) {
168 pci_conf1_read(0, k, PCI_DEVFN(i, 0), 0, 4, &val1);
169 if (val1 == 0xffffffff)
172 /* Locking probably not needed, but safer */
173 spin_lock_irqsave(&pci_config_lock, flags);
174 addr = get_base_addr(0, k, PCI_DEVFN(i, 0));
176 pci_exp_set_dev_base(addr, k, PCI_DEVFN(i, 0));
178 readl((u32 __iomem *)mmcfg_virt_addr) != val1) {
179 set_bit(i, fallback_slots);
181 "PCI: No mmconfig possible on %x:%x\n", k, i);
183 spin_unlock_irqrestore(&pci_config_lock, flags);
188 void __init pci_mmcfg_init(void)
190 if ((pci_probe & PCI_PROBE_MMCONF) == 0)
193 acpi_table_parse(ACPI_MCFG, acpi_parse_mcfg);
194 if ((pci_mmcfg_config_num == 0) ||
195 (pci_mmcfg_config == NULL) ||
196 (pci_mmcfg_config[0].base_address == 0))
199 if (!e820_all_mapped(pci_mmcfg_config[0].base_address,
200 pci_mmcfg_config[0].base_address + MMCONFIG_APER_SIZE,
202 printk(KERN_ERR "PCI: BIOS Bug: MCFG area is not E820-reserved\n");
203 printk(KERN_ERR "PCI: Not using MMCONFIG.\n");
207 printk(KERN_INFO "PCI: Using MMCONFIG\n");
208 raw_pci_ops = &pci_mmcfg;
209 pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
211 unreachable_devices();