2 * mmconfig-shared.c - Low-level direct PCI config space access via
3 * MMCONFIG - common code between i386 and x86-64.
6 * - known chipset handling
7 * - ACPI decoding and validation
9 * Per-architecture code takes care of the mappings and accesses
13 #include <linux/pci.h>
14 #include <linux/init.h>
15 #include <linux/acpi.h>
16 #include <linux/bitmap.h>
21 /* aperture is up to 256MB but BIOS may reserve less */
22 #define MMCONFIG_APER_MIN (2 * 1024*1024)
23 #define MMCONFIG_APER_MAX (256 * 1024*1024)
25 /* Indicate if the mmcfg resources have been placed into the resource table. */
26 static int __initdata pci_mmcfg_resources_inserted;
28 static const char __init *pci_mmcfg_e7520(void)
31 raw_pci_ops->read(0, 0, PCI_DEVFN(0, 0), 0xce, 2, &win);
34 if(win == 0x0000 || win == 0xf000)
35 pci_mmcfg_config_num = 0;
37 pci_mmcfg_config_num = 1;
38 pci_mmcfg_config = kzalloc(sizeof(pci_mmcfg_config[0]), GFP_KERNEL);
39 if (!pci_mmcfg_config)
41 pci_mmcfg_config[0].address = win << 16;
42 pci_mmcfg_config[0].pci_segment = 0;
43 pci_mmcfg_config[0].start_bus_number = 0;
44 pci_mmcfg_config[0].end_bus_number = 255;
47 return "Intel Corporation E7520 Memory Controller Hub";
50 static const char __init *pci_mmcfg_intel_945(void)
52 u32 pciexbar, mask = 0, len = 0;
54 pci_mmcfg_config_num = 1;
56 raw_pci_ops->read(0, 0, PCI_DEVFN(0, 0), 0x48, 4, &pciexbar);
60 pci_mmcfg_config_num = 0;
63 switch ((pciexbar >> 1) & 3) {
77 pci_mmcfg_config_num = 0;
80 /* Errata #2, things break when not aligned on a 256Mb boundary */
81 /* Can only happen in 64M/128M mode */
83 if ((pciexbar & mask) & 0x0fffffffU)
84 pci_mmcfg_config_num = 0;
86 /* Don't hit the APIC registers and their friends */
87 if ((pciexbar & mask) >= 0xf0000000U)
88 pci_mmcfg_config_num = 0;
90 if (pci_mmcfg_config_num) {
91 pci_mmcfg_config = kzalloc(sizeof(pci_mmcfg_config[0]), GFP_KERNEL);
92 if (!pci_mmcfg_config)
94 pci_mmcfg_config[0].address = pciexbar & mask;
95 pci_mmcfg_config[0].pci_segment = 0;
96 pci_mmcfg_config[0].start_bus_number = 0;
97 pci_mmcfg_config[0].end_bus_number = (len >> 20) - 1;
100 return "Intel Corporation 945G/GZ/P/PL Express Memory Controller Hub";
103 static const char __init *pci_mmcfg_amd_fam10h(void)
105 u32 low, high, address;
108 unsigned segnbits = 0, busnbits;
110 if (!(pci_probe & PCI_CHECK_ENABLE_AMD_MMCONF))
113 address = MSR_FAM10H_MMIO_CONF_BASE;
114 if (rdmsr_safe(address, &low, &high))
121 /* mmconfig is not enable */
122 if (!(msr & FAM10H_MMIO_CONF_ENABLE))
125 base = msr & (FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT);
127 busnbits = (msr >> FAM10H_MMIO_CONF_BUSRANGE_SHIFT) &
128 FAM10H_MMIO_CONF_BUSRANGE_MASK;
131 * only handle bus 0 ?
138 segnbits = busnbits - 8;
142 pci_mmcfg_config_num = (1 << segnbits);
143 pci_mmcfg_config = kzalloc(sizeof(pci_mmcfg_config[0]) *
144 pci_mmcfg_config_num, GFP_KERNEL);
145 if (!pci_mmcfg_config)
148 for (i = 0; i < (1 << segnbits); i++) {
149 pci_mmcfg_config[i].address = base + (1<<28) * i;
150 pci_mmcfg_config[i].pci_segment = i;
151 pci_mmcfg_config[i].start_bus_number = 0;
152 pci_mmcfg_config[i].end_bus_number = (1 << busnbits) - 1;
155 return "AMD Family 10h NB";
158 struct pci_mmcfg_hostbridge_probe {
163 const char *(*probe)(void);
166 static struct pci_mmcfg_hostbridge_probe pci_mmcfg_probes[] __initdata = {
167 { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
168 PCI_DEVICE_ID_INTEL_E7520_MCH, pci_mmcfg_e7520 },
169 { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
170 PCI_DEVICE_ID_INTEL_82945G_HB, pci_mmcfg_intel_945 },
171 { 0, PCI_DEVFN(0x18, 0), PCI_VENDOR_ID_AMD,
172 0x1200, pci_mmcfg_amd_fam10h },
173 { 0xff, PCI_DEVFN(0, 0), PCI_VENDOR_ID_AMD,
174 0x1200, pci_mmcfg_amd_fam10h },
177 static int __init pci_mmcfg_check_hostbridge(void)
188 pci_mmcfg_config_num = 0;
189 pci_mmcfg_config = NULL;
192 for (i = 0; !name && i < ARRAY_SIZE(pci_mmcfg_probes); i++) {
193 bus = pci_mmcfg_probes[i].bus;
194 devfn = pci_mmcfg_probes[i].devfn;
195 raw_pci_ops->read(0, bus, devfn, 0, 4, &l);
197 device = (l >> 16) & 0xffff;
199 if (pci_mmcfg_probes[i].vendor == vendor &&
200 pci_mmcfg_probes[i].device == device)
201 name = pci_mmcfg_probes[i].probe();
205 printk(KERN_INFO "PCI: Found %s %s MMCONFIG support.\n",
206 name, pci_mmcfg_config_num ? "with" : "without");
212 static void __init pci_mmcfg_insert_resources(unsigned long resource_flags)
214 #define PCI_MMCFG_RESOURCE_NAME_LEN 19
216 struct resource *res;
220 res = kcalloc(PCI_MMCFG_RESOURCE_NAME_LEN + sizeof(*res),
221 pci_mmcfg_config_num, GFP_KERNEL);
223 printk(KERN_ERR "PCI: Unable to allocate MMCONFIG resources\n");
227 names = (void *)&res[pci_mmcfg_config_num];
228 for (i = 0; i < pci_mmcfg_config_num; i++, res++) {
229 struct acpi_mcfg_allocation *cfg = &pci_mmcfg_config[i];
230 num_buses = cfg->end_bus_number - cfg->start_bus_number + 1;
232 snprintf(names, PCI_MMCFG_RESOURCE_NAME_LEN, "PCI MMCONFIG %u",
234 res->start = cfg->address;
235 res->end = res->start + (num_buses << 20) - 1;
236 res->flags = IORESOURCE_MEM | resource_flags;
237 insert_resource(&iomem_resource, res);
238 names += PCI_MMCFG_RESOURCE_NAME_LEN;
241 /* Mark that the resources have been inserted. */
242 pci_mmcfg_resources_inserted = 1;
245 static acpi_status __init check_mcfg_resource(struct acpi_resource *res,
248 struct resource *mcfg_res = data;
249 struct acpi_resource_address64 address;
252 if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
253 struct acpi_resource_fixed_memory32 *fixmem32 =
254 &res->data.fixed_memory32;
257 if ((mcfg_res->start >= fixmem32->address) &&
258 (mcfg_res->end < (fixmem32->address +
259 fixmem32->address_length))) {
261 return AE_CTRL_TERMINATE;
264 if ((res->type != ACPI_RESOURCE_TYPE_ADDRESS32) &&
265 (res->type != ACPI_RESOURCE_TYPE_ADDRESS64))
268 status = acpi_resource_to_address64(res, &address);
269 if (ACPI_FAILURE(status) ||
270 (address.address_length <= 0) ||
271 (address.resource_type != ACPI_MEMORY_RANGE))
274 if ((mcfg_res->start >= address.minimum) &&
275 (mcfg_res->end < (address.minimum + address.address_length))) {
277 return AE_CTRL_TERMINATE;
282 static acpi_status __init find_mboard_resource(acpi_handle handle, u32 lvl,
283 void *context, void **rv)
285 struct resource *mcfg_res = context;
287 acpi_walk_resources(handle, METHOD_NAME__CRS,
288 check_mcfg_resource, context);
291 return AE_CTRL_TERMINATE;
296 static int __init is_acpi_reserved(u64 start, u64 end, unsigned not_used)
298 struct resource mcfg_res;
300 mcfg_res.start = start;
304 acpi_get_devices("PNP0C01", find_mboard_resource, &mcfg_res, NULL);
307 acpi_get_devices("PNP0C02", find_mboard_resource, &mcfg_res,
310 return mcfg_res.flags;
313 typedef int (*check_reserved_t)(u64 start, u64 end, unsigned type);
315 static int __init is_mmconf_reserved(check_reserved_t is_reserved,
316 u64 addr, u64 size, int i,
317 typeof(pci_mmcfg_config[0]) *cfg, int with_e820)
322 while (!is_reserved(addr, addr + size - 1, E820_RESERVED)) {
324 if (size < (16UL<<20))
328 if (size >= (16UL<<20) || size == old_size) {
330 "PCI: MCFG area at %Lx reserved in %s\n",
331 addr, with_e820?"E820":"ACPI motherboard resources");
334 if (old_size != size) {
335 /* update end_bus_number */
336 cfg->end_bus_number = cfg->start_bus_number + ((size>>20) - 1);
337 printk(KERN_NOTICE "PCI: updated MCFG configuration %d: base %lx "
338 "segment %hu buses %u - %u\n",
339 i, (unsigned long)cfg->address, cfg->pci_segment,
340 (unsigned int)cfg->start_bus_number,
341 (unsigned int)cfg->end_bus_number);
348 static void __init pci_mmcfg_reject_broken(int early)
350 typeof(pci_mmcfg_config[0]) *cfg;
353 if ((pci_mmcfg_config_num == 0) ||
354 (pci_mmcfg_config == NULL) ||
355 (pci_mmcfg_config[0].address == 0))
358 cfg = &pci_mmcfg_config[0];
360 for (i = 0; i < pci_mmcfg_config_num; i++) {
364 cfg = &pci_mmcfg_config[i];
365 addr = cfg->start_bus_number;
367 addr += cfg->address;
368 size = cfg->end_bus_number + 1 - cfg->start_bus_number;
370 printk(KERN_NOTICE "PCI: MCFG configuration %d: base %lx "
371 "segment %hu buses %u - %u\n",
372 i, (unsigned long)cfg->address, cfg->pci_segment,
373 (unsigned int)cfg->start_bus_number,
374 (unsigned int)cfg->end_bus_number);
377 valid = is_mmconf_reserved(is_acpi_reserved, addr, size, i, cfg, 0);
383 printk(KERN_ERR "PCI: BIOS Bug: MCFG area at %Lx is not"
384 " reserved in ACPI motherboard resources\n",
387 /* Don't try to do this check unless configuration
388 type 1 is available. how about type 2 ?*/
390 valid = is_mmconf_reserved(e820_all_mapped, addr, size, i, cfg, 1);
399 printk(KERN_INFO "PCI: Not using MMCONFIG.\n");
400 pci_mmcfg_arch_free();
401 kfree(pci_mmcfg_config);
402 pci_mmcfg_config = NULL;
403 pci_mmcfg_config_num = 0;
406 static int __initdata known_bridge;
408 static void __init __pci_mmcfg_init(int early)
410 /* MMCONFIG disabled */
411 if ((pci_probe & PCI_PROBE_MMCONF) == 0)
414 /* MMCONFIG already enabled */
415 if (!early && !(pci_probe & PCI_PROBE_MASK & ~PCI_PROBE_MMCONF))
418 /* for late to exit */
423 if (pci_mmcfg_check_hostbridge())
428 acpi_table_parse(ACPI_SIG_MCFG, acpi_parse_mcfg);
429 pci_mmcfg_reject_broken(early);
432 if ((pci_mmcfg_config_num == 0) ||
433 (pci_mmcfg_config == NULL) ||
434 (pci_mmcfg_config[0].address == 0))
437 if (pci_mmcfg_arch_init()) {
439 pci_mmcfg_insert_resources(IORESOURCE_BUSY);
440 pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
443 * Signal not to attempt to insert mmcfg resources because
444 * the architecture mmcfg setup could not initialize.
446 pci_mmcfg_resources_inserted = 1;
450 void __init pci_mmcfg_early_init(void)
455 void __init pci_mmcfg_late_init(void)
460 static int __init pci_mmcfg_late_insert_resources(void)
463 * If resources are already inserted or we are not using MMCONFIG,
464 * don't insert the resources.
466 if ((pci_mmcfg_resources_inserted == 1) ||
467 (pci_probe & PCI_PROBE_MMCONF) == 0 ||
468 (pci_mmcfg_config_num == 0) ||
469 (pci_mmcfg_config == NULL) ||
470 (pci_mmcfg_config[0].address == 0))
474 * Attempt to insert the mmcfg resources but not with the busy flag
475 * marked so it won't cause request errors when __request_region is
478 pci_mmcfg_insert_resources(0);
484 * Perform MMCONFIG resource insertion after PCI initialization to allow for
485 * misprogrammed MCFG tables that state larger sizes but actually conflict
486 * with other system resources.
488 late_initcall(pci_mmcfg_late_insert_resources);