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 /* Indicate if the mmcfg resources have been placed into the resource table. */
27 static int __initdata pci_mmcfg_resources_inserted
;
29 static __init
void free_all_mmcfg(void)
32 struct pci_mmcfg_region
*cfg
;
34 pci_mmcfg_arch_free();
35 for (i
= 0; i
< pci_mmcfg_config_num
; i
++) {
36 cfg
= &pci_mmcfg_config
[i
];
38 release_resource(&cfg
->res
);
40 pci_mmcfg_config_num
= 0;
41 kfree(pci_mmcfg_config
);
42 pci_mmcfg_config
= NULL
;
45 static __init
struct pci_mmcfg_region
*pci_mmconfig_add(int segment
, int start
,
48 struct pci_mmcfg_region
*new;
49 int new_num
= pci_mmcfg_config_num
+ 1;
50 int i
= pci_mmcfg_config_num
;
57 new = kzalloc(sizeof(pci_mmcfg_config
[0]) * new_num
, GFP_KERNEL
);
61 if (pci_mmcfg_config
) {
62 memcpy(new, pci_mmcfg_config
,
63 sizeof(pci_mmcfg_config
[0]) * new_num
);
64 kfree(pci_mmcfg_config
);
66 pci_mmcfg_config
= new;
67 pci_mmcfg_config_num
++;
69 new = &pci_mmcfg_config
[i
];
72 new->segment
= segment
;
73 new->start_bus
= start
;
76 num_buses
= end
- start
+ 1;
78 res
->start
= addr
+ PCI_MMCFG_BUS_OFFSET(start
);
79 res
->end
= addr
+ PCI_MMCFG_BUS_OFFSET(num_buses
) - 1;
80 res
->flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
;
81 snprintf(new->name
, PCI_MMCFG_RESOURCE_NAME_LEN
,
82 "PCI MMCONFIG %04x [bus %02x-%02x]", segment
, start
, end
);
83 res
->name
= new->name
;
85 return &pci_mmcfg_config
[i
];
88 static const char __init
*pci_mmcfg_e7520(void)
91 raw_pci_ops
->read(0, 0, PCI_DEVFN(0, 0), 0xce, 2, &win
);
94 if (win
== 0x0000 || win
== 0xf000)
97 if (pci_mmconfig_add(0, 0, 255, win
<< 16) == NULL
)
100 return "Intel Corporation E7520 Memory Controller Hub";
103 static const char __init
*pci_mmcfg_intel_945(void)
105 u32 pciexbar
, mask
= 0, len
= 0;
107 raw_pci_ops
->read(0, 0, PCI_DEVFN(0, 0), 0x48, 4, &pciexbar
);
114 switch ((pciexbar
>> 1) & 3) {
131 /* Errata #2, things break when not aligned on a 256Mb boundary */
132 /* Can only happen in 64M/128M mode */
134 if ((pciexbar
& mask
) & 0x0fffffffU
)
137 /* Don't hit the APIC registers and their friends */
138 if ((pciexbar
& mask
) >= 0xf0000000U
)
141 if (pci_mmconfig_add(0, 0, (len
>> 20) - 1, pciexbar
& mask
) == NULL
)
144 return "Intel Corporation 945G/GZ/P/PL Express Memory Controller Hub";
147 static const char __init
*pci_mmcfg_amd_fam10h(void)
149 u32 low
, high
, address
;
152 unsigned segnbits
= 0, busnbits
, end_bus
;
154 if (!(pci_probe
& PCI_CHECK_ENABLE_AMD_MMCONF
))
157 address
= MSR_FAM10H_MMIO_CONF_BASE
;
158 if (rdmsr_safe(address
, &low
, &high
))
165 /* mmconfig is not enable */
166 if (!(msr
& FAM10H_MMIO_CONF_ENABLE
))
169 base
= msr
& (FAM10H_MMIO_CONF_BASE_MASK
<<FAM10H_MMIO_CONF_BASE_SHIFT
);
171 busnbits
= (msr
>> FAM10H_MMIO_CONF_BUSRANGE_SHIFT
) &
172 FAM10H_MMIO_CONF_BUSRANGE_MASK
;
175 * only handle bus 0 ?
182 segnbits
= busnbits
- 8;
186 end_bus
= (1 << busnbits
) - 1;
187 for (i
= 0; i
< (1 << segnbits
); i
++)
188 if (pci_mmconfig_add(i
, 0, end_bus
,
189 base
+ (1<<28) * i
) == NULL
) {
194 return "AMD Family 10h NB";
197 static bool __initdata mcp55_checked
;
198 static const char __init
*pci_mmcfg_nvidia_mcp55(void)
201 int mcp55_mmconf_found
= 0;
203 static const u32 extcfg_regnum
= 0x90;
204 static const u32 extcfg_regsize
= 4;
205 static const u32 extcfg_enable_mask
= 1<<31;
206 static const u32 extcfg_start_mask
= 0xff<<16;
207 static const int extcfg_start_shift
= 16;
208 static const u32 extcfg_size_mask
= 0x3<<28;
209 static const int extcfg_size_shift
= 28;
210 static const int extcfg_sizebus
[] = {0x100, 0x80, 0x40, 0x20};
211 static const u32 extcfg_base_mask
[] = {0x7ff8, 0x7ffc, 0x7ffe, 0x7fff};
212 static const int extcfg_base_lshift
= 25;
215 * do check if amd fam10h already took over
217 if (!acpi_disabled
|| pci_mmcfg_config_num
|| mcp55_checked
)
220 mcp55_checked
= true;
221 for (bus
= 0; bus
< 256; bus
++) {
225 int start
, size_index
, end
;
227 raw_pci_ops
->read(0, bus
, PCI_DEVFN(0, 0), 0, 4, &l
);
229 device
= (l
>> 16) & 0xffff;
231 if (PCI_VENDOR_ID_NVIDIA
!= vendor
|| 0x0369 != device
)
234 raw_pci_ops
->read(0, bus
, PCI_DEVFN(0, 0), extcfg_regnum
,
235 extcfg_regsize
, &extcfg
);
237 if (!(extcfg
& extcfg_enable_mask
))
240 size_index
= (extcfg
& extcfg_size_mask
) >> extcfg_size_shift
;
241 base
= extcfg
& extcfg_base_mask
[size_index
];
242 /* base could > 4G */
243 base
<<= extcfg_base_lshift
;
244 start
= (extcfg
& extcfg_start_mask
) >> extcfg_start_shift
;
245 end
= start
+ extcfg_sizebus
[size_index
] - 1;
246 if (pci_mmconfig_add(0, start
, end
, base
) == NULL
)
248 mcp55_mmconf_found
++;
251 if (!mcp55_mmconf_found
)
254 return "nVidia MCP55";
257 struct pci_mmcfg_hostbridge_probe
{
262 const char *(*probe
)(void);
265 static struct pci_mmcfg_hostbridge_probe pci_mmcfg_probes
[] __initdata
= {
266 { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL
,
267 PCI_DEVICE_ID_INTEL_E7520_MCH
, pci_mmcfg_e7520
},
268 { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL
,
269 PCI_DEVICE_ID_INTEL_82945G_HB
, pci_mmcfg_intel_945
},
270 { 0, PCI_DEVFN(0x18, 0), PCI_VENDOR_ID_AMD
,
271 0x1200, pci_mmcfg_amd_fam10h
},
272 { 0xff, PCI_DEVFN(0, 0), PCI_VENDOR_ID_AMD
,
273 0x1200, pci_mmcfg_amd_fam10h
},
274 { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_NVIDIA
,
275 0x0369, pci_mmcfg_nvidia_mcp55
},
278 static int __init
cmp_mmcfg(const void *x1
, const void *x2
)
280 const struct pci_mmcfg_region
*m1
= x1
;
281 const struct pci_mmcfg_region
*m2
= x2
;
284 start1
= m1
->start_bus
;
285 start2
= m2
->start_bus
;
287 return start1
- start2
;
290 static void __init
pci_mmcfg_check_end_bus_number(void)
293 struct pci_mmcfg_region
*cfg
, *cfgx
;
295 /* sort them at first */
296 sort(pci_mmcfg_config
, pci_mmcfg_config_num
,
297 sizeof(pci_mmcfg_config
[0]), cmp_mmcfg
, NULL
);
300 if (pci_mmcfg_config_num
> 0) {
301 i
= pci_mmcfg_config_num
- 1;
302 cfg
= &pci_mmcfg_config
[i
];
303 if (cfg
->end_bus
< cfg
->start_bus
)
307 /* don't overlap please */
308 for (i
= 0; i
< pci_mmcfg_config_num
- 1; i
++) {
309 cfg
= &pci_mmcfg_config
[i
];
310 cfgx
= &pci_mmcfg_config
[i
+1];
312 if (cfg
->end_bus
< cfg
->start_bus
)
315 if (cfg
->end_bus
>= cfgx
->start_bus
)
316 cfg
->end_bus
= cfgx
->start_bus
- 1;
320 static int __init
pci_mmcfg_check_hostbridge(void)
333 for (i
= 0; i
< ARRAY_SIZE(pci_mmcfg_probes
); i
++) {
334 bus
= pci_mmcfg_probes
[i
].bus
;
335 devfn
= pci_mmcfg_probes
[i
].devfn
;
336 raw_pci_ops
->read(0, bus
, devfn
, 0, 4, &l
);
338 device
= (l
>> 16) & 0xffff;
341 if (pci_mmcfg_probes
[i
].vendor
== vendor
&&
342 pci_mmcfg_probes
[i
].device
== device
)
343 name
= pci_mmcfg_probes
[i
].probe();
346 printk(KERN_INFO
"PCI: Found %s with MMCONFIG support.\n",
350 /* some end_bus_number is crazy, fix it */
351 pci_mmcfg_check_end_bus_number();
353 return pci_mmcfg_config_num
!= 0;
356 static void __init
pci_mmcfg_insert_resources(void)
359 struct pci_mmcfg_region
*cfg
;
361 for (i
= 0; i
< pci_mmcfg_config_num
; i
++) {
362 cfg
= &pci_mmcfg_config
[i
];
363 insert_resource(&iomem_resource
, &cfg
->res
);
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 int i
, struct pci_mmcfg_region
*cfg
, int with_e820
)
443 u64 addr
= cfg
->res
.start
;
444 u64 size
= resource_size(&cfg
->res
);
446 int valid
= 0, num_buses
;
448 while (!is_reserved(addr
, addr
+ size
, E820_RESERVED
)) {
450 if (size
< (16UL<<20))
454 if (size
>= (16UL<<20) || size
== old_size
) {
456 "PCI: MCFG area at %Lx reserved in %s\n",
457 addr
, with_e820
?"E820":"ACPI motherboard resources");
460 if (old_size
!= size
) {
462 cfg
->end_bus
= cfg
->start_bus
+ ((size
>>20) - 1);
463 num_buses
= cfg
->end_bus
- cfg
->start_bus
+ 1;
464 cfg
->res
.end
= cfg
->res
.start
+
465 PCI_MMCFG_BUS_OFFSET(num_buses
) - 1;
466 snprintf(cfg
->name
, PCI_MMCFG_RESOURCE_NAME_LEN
,
467 "PCI MMCONFIG %04x [bus %02x-%02x]",
468 cfg
->segment
, cfg
->start_bus
, cfg
->end_bus
);
469 printk(KERN_NOTICE
"PCI: updated MCFG configuration %d: base %lx "
470 "segment %hu buses %u - %u\n",
471 i
, (unsigned long)cfg
->address
, cfg
->segment
,
472 (unsigned int)cfg
->start_bus
,
473 (unsigned int)cfg
->end_bus
);
480 static void __init
pci_mmcfg_reject_broken(int early
)
482 struct pci_mmcfg_region
*cfg
;
485 if (pci_mmcfg_config_num
== 0)
488 for (i
= 0; i
< pci_mmcfg_config_num
; i
++) {
491 cfg
= &pci_mmcfg_config
[i
];
492 printk(KERN_NOTICE
"PCI: MCFG configuration %d: base %lx "
493 "segment %hu buses %u - %u\n",
494 i
, (unsigned long)cfg
->address
, cfg
->segment
,
495 (unsigned int)cfg
->start_bus
,
496 (unsigned int)cfg
->end_bus
);
498 if (!early
&& !acpi_disabled
)
499 valid
= is_mmconf_reserved(is_acpi_reserved
, i
, cfg
, 0);
505 printk(KERN_ERR
"PCI: BIOS Bug: MCFG area at %Lx is not"
506 " reserved in ACPI motherboard resources\n",
509 /* Don't try to do this check unless configuration
510 type 1 is available. how about type 2 ?*/
512 valid
= is_mmconf_reserved(e820_all_mapped
, i
, cfg
, 1);
521 printk(KERN_INFO
"PCI: Not using MMCONFIG.\n");
525 static int __initdata known_bridge
;
527 /* The physical address of the MMCONFIG aperture. Set from ACPI tables. */
528 struct pci_mmcfg_region
*pci_mmcfg_config
;
529 int pci_mmcfg_config_num
;
531 static int __init
acpi_mcfg_check_entry(struct acpi_table_mcfg
*mcfg
,
532 struct acpi_mcfg_allocation
*cfg
)
536 if (cfg
->address
< 0xFFFFFFFF)
539 if (!strcmp(mcfg
->header
.oem_id
, "SGI"))
542 if (mcfg
->header
.revision
>= 1) {
543 if (dmi_get_date(DMI_BIOS_DATE
, &year
, NULL
, NULL
) &&
548 printk(KERN_ERR PREFIX
"MCFG region for %04x:%02x-%02x at %#llx "
549 "is above 4GB, ignored\n", cfg
->pci_segment
,
550 cfg
->start_bus_number
, cfg
->end_bus_number
, cfg
->address
);
554 static int __init
pci_parse_mcfg(struct acpi_table_header
*header
)
556 struct acpi_table_mcfg
*mcfg
;
557 struct acpi_mcfg_allocation
*cfg_table
, *cfg
;
564 mcfg
= (struct acpi_table_mcfg
*)header
;
566 /* how many config structures do we have */
569 i
= header
->length
- sizeof(struct acpi_table_mcfg
);
570 while (i
>= sizeof(struct acpi_mcfg_allocation
)) {
572 i
-= sizeof(struct acpi_mcfg_allocation
);
575 printk(KERN_ERR PREFIX
"MMCONFIG has no entries\n");
579 cfg_table
= (struct acpi_mcfg_allocation
*) &mcfg
[1];
580 for (i
= 0; i
< entries
; i
++) {
582 if (acpi_mcfg_check_entry(mcfg
, cfg
)) {
587 if (pci_mmconfig_add(cfg
->pci_segment
, cfg
->start_bus_number
,
588 cfg
->end_bus_number
, cfg
->address
) == NULL
) {
589 printk(KERN_WARNING PREFIX
590 "no memory for MCFG entries\n");
599 static void __init
__pci_mmcfg_init(int early
)
601 /* MMCONFIG disabled */
602 if ((pci_probe
& PCI_PROBE_MMCONF
) == 0)
605 /* MMCONFIG already enabled */
606 if (!early
&& !(pci_probe
& PCI_PROBE_MASK
& ~PCI_PROBE_MMCONF
))
609 /* for late to exit */
614 if (pci_mmcfg_check_hostbridge())
619 acpi_sfi_table_parse(ACPI_SIG_MCFG
, pci_parse_mcfg
);
621 pci_mmcfg_reject_broken(early
);
623 if (pci_mmcfg_config_num
== 0)
626 if (pci_mmcfg_arch_init())
627 pci_probe
= (pci_probe
& ~PCI_PROBE_MASK
) | PCI_PROBE_MMCONF
;
630 * Signal not to attempt to insert mmcfg resources because
631 * the architecture mmcfg setup could not initialize.
633 pci_mmcfg_resources_inserted
= 1;
637 void __init
pci_mmcfg_early_init(void)
642 void __init
pci_mmcfg_late_init(void)
647 static int __init
pci_mmcfg_late_insert_resources(void)
650 * If resources are already inserted or we are not using MMCONFIG,
651 * don't insert the resources.
653 if ((pci_mmcfg_resources_inserted
== 1) ||
654 (pci_probe
& PCI_PROBE_MMCONF
) == 0 ||
655 (pci_mmcfg_config_num
== 0))
659 * Attempt to insert the mmcfg resources but not with the busy flag
660 * marked so it won't cause request errors when __request_region is
663 pci_mmcfg_insert_resources();
669 * Perform MMCONFIG resource insertion after PCI initialization to allow for
670 * misprogrammed MCFG tables that state larger sizes but actually conflict
671 * with other system resources.
673 late_initcall(pci_mmcfg_late_insert_resources
);