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/sfi_acpi.h>
17 #include <linux/bitmap.h>
18 #include <linux/dmi.h>
19 #include <linux/sort.h>
21 #include <asm/pci_x86.h>
24 #define PREFIX "PCI: "
26 /* aperture is up to 256MB but BIOS may reserve less */
27 #define MMCONFIG_APER_MIN (2 * 1024*1024)
28 #define MMCONFIG_APER_MAX (256 * 1024*1024)
30 /* Indicate if the mmcfg resources have been placed into the resource table. */
31 static int __initdata pci_mmcfg_resources_inserted;
33 static __init int extend_mmcfg(int num)
35 struct acpi_mcfg_allocation *new;
36 int new_num = pci_mmcfg_config_num + num;
38 new = kzalloc(sizeof(pci_mmcfg_config[0]) * new_num, GFP_KERNEL);
42 if (pci_mmcfg_config) {
43 memcpy(new, pci_mmcfg_config,
44 sizeof(pci_mmcfg_config[0]) * new_num);
45 kfree(pci_mmcfg_config);
47 pci_mmcfg_config = new;
52 static __init void fill_one_mmcfg(u64 addr, int segment, int start, int end)
54 int i = pci_mmcfg_config_num;
56 pci_mmcfg_config_num++;
57 pci_mmcfg_config[i].address = addr;
58 pci_mmcfg_config[i].pci_segment = segment;
59 pci_mmcfg_config[i].start_bus_number = start;
60 pci_mmcfg_config[i].end_bus_number = end;
63 static const char __init *pci_mmcfg_e7520(void)
66 raw_pci_ops->read(0, 0, PCI_DEVFN(0, 0), 0xce, 2, &win);
69 if (win == 0x0000 || win == 0xf000)
72 if (extend_mmcfg(1) == -1)
75 fill_one_mmcfg(win << 16, 0, 0, 255);
77 return "Intel Corporation E7520 Memory Controller Hub";
80 static const char __init *pci_mmcfg_intel_945(void)
82 u32 pciexbar, mask = 0, len = 0;
84 raw_pci_ops->read(0, 0, PCI_DEVFN(0, 0), 0x48, 4, &pciexbar);
91 switch ((pciexbar >> 1) & 3) {
108 /* Errata #2, things break when not aligned on a 256Mb boundary */
109 /* Can only happen in 64M/128M mode */
111 if ((pciexbar & mask) & 0x0fffffffU)
114 /* Don't hit the APIC registers and their friends */
115 if ((pciexbar & mask) >= 0xf0000000U)
118 if (extend_mmcfg(1) == -1)
121 fill_one_mmcfg(pciexbar & mask, 0, 0, (len >> 20) - 1);
123 return "Intel Corporation 945G/GZ/P/PL Express Memory Controller Hub";
126 static const char __init *pci_mmcfg_amd_fam10h(void)
128 u32 low, high, address;
131 unsigned segnbits = 0, busnbits;
133 if (!(pci_probe & PCI_CHECK_ENABLE_AMD_MMCONF))
136 address = MSR_FAM10H_MMIO_CONF_BASE;
137 if (rdmsr_safe(address, &low, &high))
144 /* mmconfig is not enable */
145 if (!(msr & FAM10H_MMIO_CONF_ENABLE))
148 base = msr & (FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT);
150 busnbits = (msr >> FAM10H_MMIO_CONF_BUSRANGE_SHIFT) &
151 FAM10H_MMIO_CONF_BUSRANGE_MASK;
154 * only handle bus 0 ?
161 segnbits = busnbits - 8;
165 if (extend_mmcfg(1 << segnbits) == -1)
168 for (i = 0; i < (1 << segnbits); i++)
169 fill_one_mmcfg(base + (1<<28) * i, i, 0, (1 << busnbits) - 1);
171 return "AMD Family 10h NB";
174 static bool __initdata mcp55_checked;
175 static const char __init *pci_mmcfg_nvidia_mcp55(void)
178 int mcp55_mmconf_found = 0;
180 static const u32 extcfg_regnum = 0x90;
181 static const u32 extcfg_regsize = 4;
182 static const u32 extcfg_enable_mask = 1<<31;
183 static const u32 extcfg_start_mask = 0xff<<16;
184 static const int extcfg_start_shift = 16;
185 static const u32 extcfg_size_mask = 0x3<<28;
186 static const int extcfg_size_shift = 28;
187 static const int extcfg_sizebus[] = {0x100, 0x80, 0x40, 0x20};
188 static const u32 extcfg_base_mask[] = {0x7ff8, 0x7ffc, 0x7ffe, 0x7fff};
189 static const int extcfg_base_lshift = 25;
192 * do check if amd fam10h already took over
194 if (!acpi_disabled || pci_mmcfg_config_num || mcp55_checked)
197 mcp55_checked = true;
198 for (bus = 0; bus < 256; bus++) {
202 int start, size_index, end;
204 raw_pci_ops->read(0, bus, PCI_DEVFN(0, 0), 0, 4, &l);
206 device = (l >> 16) & 0xffff;
208 if (PCI_VENDOR_ID_NVIDIA != vendor || 0x0369 != device)
211 raw_pci_ops->read(0, bus, PCI_DEVFN(0, 0), extcfg_regnum,
212 extcfg_regsize, &extcfg);
214 if (!(extcfg & extcfg_enable_mask))
217 if (extend_mmcfg(1) == -1)
220 size_index = (extcfg & extcfg_size_mask) >> extcfg_size_shift;
221 base = extcfg & extcfg_base_mask[size_index];
222 /* base could > 4G */
223 base <<= extcfg_base_lshift;
224 start = (extcfg & extcfg_start_mask) >> extcfg_start_shift;
225 end = start + extcfg_sizebus[size_index] - 1;
226 fill_one_mmcfg(base, 0, start, end);
227 mcp55_mmconf_found++;
230 if (!mcp55_mmconf_found)
233 return "nVidia MCP55";
236 struct pci_mmcfg_hostbridge_probe {
241 const char *(*probe)(void);
244 static struct pci_mmcfg_hostbridge_probe pci_mmcfg_probes[] __initdata = {
245 { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
246 PCI_DEVICE_ID_INTEL_E7520_MCH, pci_mmcfg_e7520 },
247 { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
248 PCI_DEVICE_ID_INTEL_82945G_HB, pci_mmcfg_intel_945 },
249 { 0, PCI_DEVFN(0x18, 0), PCI_VENDOR_ID_AMD,
250 0x1200, pci_mmcfg_amd_fam10h },
251 { 0xff, PCI_DEVFN(0, 0), PCI_VENDOR_ID_AMD,
252 0x1200, pci_mmcfg_amd_fam10h },
253 { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_NVIDIA,
254 0x0369, pci_mmcfg_nvidia_mcp55 },
257 static int __init cmp_mmcfg(const void *x1, const void *x2)
259 const typeof(pci_mmcfg_config[0]) *m1 = x1;
260 const typeof(pci_mmcfg_config[0]) *m2 = x2;
263 start1 = m1->start_bus_number;
264 start2 = m2->start_bus_number;
266 return start1 - start2;
269 static void __init pci_mmcfg_check_end_bus_number(void)
272 typeof(pci_mmcfg_config[0]) *cfg, *cfgx;
274 /* sort them at first */
275 sort(pci_mmcfg_config, pci_mmcfg_config_num,
276 sizeof(pci_mmcfg_config[0]), cmp_mmcfg, NULL);
279 if (pci_mmcfg_config_num > 0) {
280 i = pci_mmcfg_config_num - 1;
281 cfg = &pci_mmcfg_config[i];
282 if (cfg->end_bus_number < cfg->start_bus_number)
283 cfg->end_bus_number = 255;
286 /* don't overlap please */
287 for (i = 0; i < pci_mmcfg_config_num - 1; i++) {
288 cfg = &pci_mmcfg_config[i];
289 cfgx = &pci_mmcfg_config[i+1];
291 if (cfg->end_bus_number < cfg->start_bus_number)
292 cfg->end_bus_number = 255;
294 if (cfg->end_bus_number >= cfgx->start_bus_number)
295 cfg->end_bus_number = cfgx->start_bus_number - 1;
299 static int __init pci_mmcfg_check_hostbridge(void)
310 pci_mmcfg_config_num = 0;
311 pci_mmcfg_config = NULL;
313 for (i = 0; i < ARRAY_SIZE(pci_mmcfg_probes); i++) {
314 bus = pci_mmcfg_probes[i].bus;
315 devfn = pci_mmcfg_probes[i].devfn;
316 raw_pci_ops->read(0, bus, devfn, 0, 4, &l);
318 device = (l >> 16) & 0xffff;
321 if (pci_mmcfg_probes[i].vendor == vendor &&
322 pci_mmcfg_probes[i].device == device)
323 name = pci_mmcfg_probes[i].probe();
326 printk(KERN_INFO "PCI: Found %s with MMCONFIG support.\n",
330 /* some end_bus_number is crazy, fix it */
331 pci_mmcfg_check_end_bus_number();
333 return pci_mmcfg_config_num != 0;
336 static void __init pci_mmcfg_insert_resources(void)
338 #define PCI_MMCFG_RESOURCE_NAME_LEN 24
340 struct resource *res;
344 res = kcalloc(PCI_MMCFG_RESOURCE_NAME_LEN + sizeof(*res),
345 pci_mmcfg_config_num, GFP_KERNEL);
347 printk(KERN_ERR "PCI: Unable to allocate MMCONFIG resources\n");
351 names = (void *)&res[pci_mmcfg_config_num];
352 for (i = 0; i < pci_mmcfg_config_num; i++, res++) {
353 struct acpi_mcfg_allocation *cfg = &pci_mmcfg_config[i];
354 num_buses = cfg->end_bus_number - cfg->start_bus_number + 1;
356 snprintf(names, PCI_MMCFG_RESOURCE_NAME_LEN,
357 "PCI MMCONFIG %u [%02x-%02x]", cfg->pci_segment,
358 cfg->start_bus_number, cfg->end_bus_number);
359 res->start = cfg->address + (cfg->start_bus_number << 20);
360 res->end = res->start + (num_buses << 20) - 1;
361 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
362 insert_resource(&iomem_resource, res);
363 names += PCI_MMCFG_RESOURCE_NAME_LEN;
366 /* Mark that the resources have been inserted. */
367 pci_mmcfg_resources_inserted = 1;
370 static acpi_status __init check_mcfg_resource(struct acpi_resource *res,
373 struct resource *mcfg_res = data;
374 struct acpi_resource_address64 address;
377 if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
378 struct acpi_resource_fixed_memory32 *fixmem32 =
379 &res->data.fixed_memory32;
382 if ((mcfg_res->start >= fixmem32->address) &&
383 (mcfg_res->end < (fixmem32->address +
384 fixmem32->address_length))) {
386 return AE_CTRL_TERMINATE;
389 if ((res->type != ACPI_RESOURCE_TYPE_ADDRESS32) &&
390 (res->type != ACPI_RESOURCE_TYPE_ADDRESS64))
393 status = acpi_resource_to_address64(res, &address);
394 if (ACPI_FAILURE(status) ||
395 (address.address_length <= 0) ||
396 (address.resource_type != ACPI_MEMORY_RANGE))
399 if ((mcfg_res->start >= address.minimum) &&
400 (mcfg_res->end < (address.minimum + address.address_length))) {
402 return AE_CTRL_TERMINATE;
407 static acpi_status __init find_mboard_resource(acpi_handle handle, u32 lvl,
408 void *context, void **rv)
410 struct resource *mcfg_res = context;
412 acpi_walk_resources(handle, METHOD_NAME__CRS,
413 check_mcfg_resource, context);
416 return AE_CTRL_TERMINATE;
421 static int __init is_acpi_reserved(u64 start, u64 end, unsigned not_used)
423 struct resource mcfg_res;
425 mcfg_res.start = start;
426 mcfg_res.end = end - 1;
429 acpi_get_devices("PNP0C01", find_mboard_resource, &mcfg_res, NULL);
432 acpi_get_devices("PNP0C02", find_mboard_resource, &mcfg_res,
435 return mcfg_res.flags;
438 typedef int (*check_reserved_t)(u64 start, u64 end, unsigned type);
440 static int __init is_mmconf_reserved(check_reserved_t is_reserved,
441 u64 addr, u64 size, int i,
442 typeof(pci_mmcfg_config[0]) *cfg, int with_e820)
447 while (!is_reserved(addr, addr + size, E820_RESERVED)) {
449 if (size < (16UL<<20))
453 if (size >= (16UL<<20) || size == old_size) {
455 "PCI: MCFG area at %Lx reserved in %s\n",
456 addr, with_e820?"E820":"ACPI motherboard resources");
459 if (old_size != size) {
460 /* update end_bus_number */
461 cfg->end_bus_number = cfg->start_bus_number + ((size>>20) - 1);
462 printk(KERN_NOTICE "PCI: updated MCFG configuration %d: base %lx "
463 "segment %hu buses %u - %u\n",
464 i, (unsigned long)cfg->address, cfg->pci_segment,
465 (unsigned int)cfg->start_bus_number,
466 (unsigned int)cfg->end_bus_number);
473 static void __init pci_mmcfg_reject_broken(int early)
475 typeof(pci_mmcfg_config[0]) *cfg;
478 if ((pci_mmcfg_config_num == 0) ||
479 (pci_mmcfg_config == NULL) ||
480 (pci_mmcfg_config[0].address == 0))
483 for (i = 0; i < pci_mmcfg_config_num; i++) {
487 cfg = &pci_mmcfg_config[i];
488 addr = cfg->start_bus_number;
490 addr += cfg->address;
491 size = cfg->end_bus_number + 1 - cfg->start_bus_number;
493 printk(KERN_NOTICE "PCI: MCFG configuration %d: base %lx "
494 "segment %hu buses %u - %u\n",
495 i, (unsigned long)cfg->address, cfg->pci_segment,
496 (unsigned int)cfg->start_bus_number,
497 (unsigned int)cfg->end_bus_number);
499 if (!early && !acpi_disabled)
500 valid = is_mmconf_reserved(is_acpi_reserved, addr, size, i, cfg, 0);
506 printk(KERN_ERR "PCI: BIOS Bug: MCFG area at %Lx is not"
507 " reserved in ACPI motherboard resources\n",
510 /* Don't try to do this check unless configuration
511 type 1 is available. how about type 2 ?*/
513 valid = is_mmconf_reserved(e820_all_mapped, addr, size, i, cfg, 1);
522 printk(KERN_INFO "PCI: Not using MMCONFIG.\n");
523 pci_mmcfg_arch_free();
524 kfree(pci_mmcfg_config);
525 pci_mmcfg_config = NULL;
526 pci_mmcfg_config_num = 0;
529 static int __initdata known_bridge;
531 /* The physical address of the MMCONFIG aperture. Set from ACPI tables. */
532 struct acpi_mcfg_allocation *pci_mmcfg_config;
533 int pci_mmcfg_config_num;
535 static int __init acpi_mcfg_check_entry(struct acpi_table_mcfg *mcfg,
536 struct acpi_mcfg_allocation *cfg)
540 if (cfg->address < 0xFFFFFFFF)
543 if (!strcmp(mcfg->header.oem_id, "SGI"))
546 if (mcfg->header.revision >= 1) {
547 if (dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL) &&
552 printk(KERN_ERR PREFIX "MCFG region for %04x:%02x-%02x at %#llx "
553 "is above 4GB, ignored\n", cfg->pci_segment,
554 cfg->start_bus_number, cfg->end_bus_number, cfg->address);
558 static int __init pci_parse_mcfg(struct acpi_table_header *header)
560 struct acpi_table_mcfg *mcfg;
567 mcfg = (struct acpi_table_mcfg *)header;
569 /* how many config structures do we have */
570 pci_mmcfg_config_num = 0;
571 i = header->length - sizeof(struct acpi_table_mcfg);
572 while (i >= sizeof(struct acpi_mcfg_allocation)) {
573 ++pci_mmcfg_config_num;
574 i -= sizeof(struct acpi_mcfg_allocation);
576 if (pci_mmcfg_config_num == 0) {
577 printk(KERN_ERR PREFIX "MMCONFIG has no entries\n");
581 config_size = pci_mmcfg_config_num * sizeof(*pci_mmcfg_config);
582 pci_mmcfg_config = kmalloc(config_size, GFP_KERNEL);
583 if (!pci_mmcfg_config) {
584 printk(KERN_WARNING PREFIX
585 "No memory for MCFG config tables\n");
589 memcpy(pci_mmcfg_config, &mcfg[1], config_size);
591 for (i = 0; i < pci_mmcfg_config_num; ++i) {
592 if (acpi_mcfg_check_entry(mcfg, &pci_mmcfg_config[i])) {
593 kfree(pci_mmcfg_config);
594 pci_mmcfg_config_num = 0;
602 static void __init __pci_mmcfg_init(int early)
604 /* MMCONFIG disabled */
605 if ((pci_probe & PCI_PROBE_MMCONF) == 0)
608 /* MMCONFIG already enabled */
609 if (!early && !(pci_probe & PCI_PROBE_MASK & ~PCI_PROBE_MMCONF))
612 /* for late to exit */
617 if (pci_mmcfg_check_hostbridge())
622 acpi_sfi_table_parse(ACPI_SIG_MCFG, pci_parse_mcfg);
624 pci_mmcfg_reject_broken(early);
626 if ((pci_mmcfg_config_num == 0) ||
627 (pci_mmcfg_config == NULL) ||
628 (pci_mmcfg_config[0].address == 0))
631 if (pci_mmcfg_arch_init())
632 pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
635 * Signal not to attempt to insert mmcfg resources because
636 * the architecture mmcfg setup could not initialize.
638 pci_mmcfg_resources_inserted = 1;
642 void __init pci_mmcfg_early_init(void)
647 void __init pci_mmcfg_late_init(void)
652 static int __init pci_mmcfg_late_insert_resources(void)
655 * If resources are already inserted or we are not using MMCONFIG,
656 * don't insert the resources.
658 if ((pci_mmcfg_resources_inserted == 1) ||
659 (pci_probe & PCI_PROBE_MMCONF) == 0 ||
660 (pci_mmcfg_config_num == 0) ||
661 (pci_mmcfg_config == NULL) ||
662 (pci_mmcfg_config[0].address == 0))
666 * Attempt to insert the mmcfg resources but not with the busy flag
667 * marked so it won't cause request errors when __request_region is
670 pci_mmcfg_insert_resources();
676 * Perform MMCONFIG resource insertion after PCI initialization to allow for
677 * misprogrammed MCFG tables that state larger sizes but actually conflict
678 * with other system resources.
680 late_initcall(pci_mmcfg_late_insert_resources);