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 /* aperture is up to 256MB but BIOS may reserve less */
19 #define MMCONFIG_APER_MIN (2 * 1024*1024)
20 #define MMCONFIG_APER_MAX (256 * 1024*1024)
22 /* Assume systems with more busses have correct MCFG */
23 #define MAX_CHECK_BUS 16
25 #define mmcfg_virt_addr ((void __iomem *) fix_to_virt(FIX_PCIE_MCFG))
27 /* The base address of the last MMCONFIG device accessed */
28 static u32 mmcfg_last_accessed_device;
29 static int mmcfg_last_accessed_cpu;
31 static DECLARE_BITMAP(fallback_slots, MAX_CHECK_BUS*32);
34 * Functions for accessing PCI configuration space with MMCONFIG accesses
36 static u32 get_base_addr(unsigned int seg, int bus, unsigned devfn)
39 struct acpi_table_mcfg_config *cfg;
41 if (seg == 0 && bus < MAX_CHECK_BUS &&
42 test_bit(PCI_SLOT(devfn) + 32*bus, fallback_slots))
47 if (cfg_num >= pci_mmcfg_config_num) {
50 cfg = &pci_mmcfg_config[cfg_num];
51 if (cfg->pci_segment_group_number != seg)
53 if ((cfg->start_bus_number <= bus) &&
54 (cfg->end_bus_number >= bus))
55 return cfg->base_address;
58 /* Handle more broken MCFG tables on Asus etc.
59 They only contain a single entry for bus 0-0. Assume
60 this applies to all busses. */
61 cfg = &pci_mmcfg_config[0];
62 if (pci_mmcfg_config_num == 1 &&
63 cfg->pci_segment_group_number == 0 &&
64 (cfg->start_bus_number | cfg->end_bus_number) == 0)
65 return cfg->base_address;
67 /* Fall back to type 0 */
72 * This is always called under pci_config_lock
74 static void pci_exp_set_dev_base(unsigned int base, int bus, int devfn)
76 u32 dev_base = base | (bus << 20) | (devfn << 12);
77 int cpu = smp_processor_id();
78 if (dev_base != mmcfg_last_accessed_device ||
79 cpu != mmcfg_last_accessed_cpu) {
80 mmcfg_last_accessed_device = dev_base;
81 mmcfg_last_accessed_cpu = cpu;
82 set_fixmap_nocache(FIX_PCIE_MCFG, dev_base);
86 static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
87 unsigned int devfn, int reg, int len, u32 *value)
92 if ((bus > 255) || (devfn > 255) || (reg > 4095)) {
97 base = get_base_addr(seg, bus, devfn);
99 return pci_conf1_read(seg,bus,devfn,reg,len,value);
101 spin_lock_irqsave(&pci_config_lock, flags);
103 pci_exp_set_dev_base(base, bus, devfn);
107 *value = readb(mmcfg_virt_addr + reg);
110 *value = readw(mmcfg_virt_addr + reg);
113 *value = readl(mmcfg_virt_addr + reg);
117 spin_unlock_irqrestore(&pci_config_lock, flags);
122 static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
123 unsigned int devfn, int reg, int len, u32 value)
128 if ((bus > 255) || (devfn > 255) || (reg > 4095))
131 base = get_base_addr(seg, bus, devfn);
133 return pci_conf1_write(seg,bus,devfn,reg,len,value);
135 spin_lock_irqsave(&pci_config_lock, flags);
137 pci_exp_set_dev_base(base, bus, devfn);
141 writeb(value, mmcfg_virt_addr + reg);
144 writew(value, mmcfg_virt_addr + reg);
147 writel(value, mmcfg_virt_addr + reg);
151 spin_unlock_irqrestore(&pci_config_lock, flags);
156 static struct pci_raw_ops pci_mmcfg = {
157 .read = pci_mmcfg_read,
158 .write = pci_mmcfg_write,
161 /* K8 systems have some devices (typically in the builtin northbridge)
162 that are only accessible using type1
163 Normally this can be expressed in the MCFG by not listing them
164 and assigning suitable _SEGs, but this isn't implemented in some BIOS.
165 Instead try to discover all devices on bus 0 that are unreachable using MM
166 and fallback for them. */
167 static __init void unreachable_devices(void)
172 for (k = 0; k < MAX_CHECK_BUS; k++) {
173 for (i = 0; i < 32; i++) {
177 pci_conf1_read(0, k, PCI_DEVFN(i, 0), 0, 4, &val1);
178 if (val1 == 0xffffffff)
181 /* Locking probably not needed, but safer */
182 spin_lock_irqsave(&pci_config_lock, flags);
183 addr = get_base_addr(0, k, PCI_DEVFN(i, 0));
185 pci_exp_set_dev_base(addr, k, PCI_DEVFN(i, 0));
187 readl((u32 __iomem *)mmcfg_virt_addr) != val1) {
188 set_bit(i + 32*k, fallback_slots);
190 "PCI: No mmconfig possible on %x:%x\n", k, i);
192 spin_unlock_irqrestore(&pci_config_lock, flags);
197 void __init pci_mmcfg_init(int type)
199 if ((pci_probe & PCI_PROBE_MMCONF) == 0)
202 acpi_table_parse(ACPI_MCFG, acpi_parse_mcfg);
203 if ((pci_mmcfg_config_num == 0) ||
204 (pci_mmcfg_config == NULL) ||
205 (pci_mmcfg_config[0].base_address == 0))
208 /* Only do this check when type 1 works. If it doesn't work
209 assume we run on a Mac and always use MCFG */
210 if (type == 1 && !e820_all_mapped(pci_mmcfg_config[0].base_address,
211 pci_mmcfg_config[0].base_address + MMCONFIG_APER_MIN,
213 printk(KERN_ERR "PCI: BIOS Bug: MCFG area at %x is not E820-reserved\n",
214 pci_mmcfg_config[0].base_address);
215 printk(KERN_ERR "PCI: Not using MMCONFIG.\n");
219 printk(KERN_INFO "PCI: Using MMCONFIG\n");
220 raw_pci_ops = &pci_mmcfg;
221 pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
223 unreachable_devices();