xref: /openbmc/linux/arch/x86/pci/mmconfig-shared.c (revision 824877111cd7f2b4fd2fe6947c5c5cbbb3ac5bd8)
1fb9aa6f1SThomas Gleixner /*
2fb9aa6f1SThomas Gleixner  * mmconfig-shared.c - Low-level direct PCI config space access via
3fb9aa6f1SThomas Gleixner  *                     MMCONFIG - common code between i386 and x86-64.
4fb9aa6f1SThomas Gleixner  *
5fb9aa6f1SThomas Gleixner  * This code does:
6fb9aa6f1SThomas Gleixner  * - known chipset handling
7fb9aa6f1SThomas Gleixner  * - ACPI decoding and validation
8fb9aa6f1SThomas Gleixner  *
9fb9aa6f1SThomas Gleixner  * Per-architecture code takes care of the mappings and accesses
10fb9aa6f1SThomas Gleixner  * themselves.
11fb9aa6f1SThomas Gleixner  */
12fb9aa6f1SThomas Gleixner 
13fb9aa6f1SThomas Gleixner #include <linux/pci.h>
14fb9aa6f1SThomas Gleixner #include <linux/init.h>
15fb9aa6f1SThomas Gleixner #include <linux/acpi.h>
16fb9aa6f1SThomas Gleixner #include <linux/bitmap.h>
17fb9aa6f1SThomas Gleixner #include <asm/e820.h>
18*82487711SJaswinder Singh Rajput #include <asm/pci_x86.h>
19fb9aa6f1SThomas Gleixner 
20fb9aa6f1SThomas Gleixner /* aperture is up to 256MB but BIOS may reserve less */
21fb9aa6f1SThomas Gleixner #define MMCONFIG_APER_MIN	(2 * 1024*1024)
22fb9aa6f1SThomas Gleixner #define MMCONFIG_APER_MAX	(256 * 1024*1024)
23fb9aa6f1SThomas Gleixner 
24fb9aa6f1SThomas Gleixner /* Indicate if the mmcfg resources have been placed into the resource table. */
25fb9aa6f1SThomas Gleixner static int __initdata pci_mmcfg_resources_inserted;
26fb9aa6f1SThomas Gleixner 
27fb9aa6f1SThomas Gleixner static const char __init *pci_mmcfg_e7520(void)
28fb9aa6f1SThomas Gleixner {
29fb9aa6f1SThomas Gleixner 	u32 win;
30bb63b421SYinghai Lu 	raw_pci_ops->read(0, 0, PCI_DEVFN(0, 0), 0xce, 2, &win);
31fb9aa6f1SThomas Gleixner 
32fb9aa6f1SThomas Gleixner 	win = win & 0xf000;
33fb9aa6f1SThomas Gleixner 	if(win == 0x0000 || win == 0xf000)
34fb9aa6f1SThomas Gleixner 		pci_mmcfg_config_num = 0;
35fb9aa6f1SThomas Gleixner 	else {
36fb9aa6f1SThomas Gleixner 		pci_mmcfg_config_num = 1;
37fb9aa6f1SThomas Gleixner 		pci_mmcfg_config = kzalloc(sizeof(pci_mmcfg_config[0]), GFP_KERNEL);
38fb9aa6f1SThomas Gleixner 		if (!pci_mmcfg_config)
39fb9aa6f1SThomas Gleixner 			return NULL;
40fb9aa6f1SThomas Gleixner 		pci_mmcfg_config[0].address = win << 16;
41fb9aa6f1SThomas Gleixner 		pci_mmcfg_config[0].pci_segment = 0;
42fb9aa6f1SThomas Gleixner 		pci_mmcfg_config[0].start_bus_number = 0;
43fb9aa6f1SThomas Gleixner 		pci_mmcfg_config[0].end_bus_number = 255;
44fb9aa6f1SThomas Gleixner 	}
45fb9aa6f1SThomas Gleixner 
46fb9aa6f1SThomas Gleixner 	return "Intel Corporation E7520 Memory Controller Hub";
47fb9aa6f1SThomas Gleixner }
48fb9aa6f1SThomas Gleixner 
49fb9aa6f1SThomas Gleixner static const char __init *pci_mmcfg_intel_945(void)
50fb9aa6f1SThomas Gleixner {
51fb9aa6f1SThomas Gleixner 	u32 pciexbar, mask = 0, len = 0;
52fb9aa6f1SThomas Gleixner 
53fb9aa6f1SThomas Gleixner 	pci_mmcfg_config_num = 1;
54fb9aa6f1SThomas Gleixner 
55bb63b421SYinghai Lu 	raw_pci_ops->read(0, 0, PCI_DEVFN(0, 0), 0x48, 4, &pciexbar);
56fb9aa6f1SThomas Gleixner 
57fb9aa6f1SThomas Gleixner 	/* Enable bit */
58fb9aa6f1SThomas Gleixner 	if (!(pciexbar & 1))
59fb9aa6f1SThomas Gleixner 		pci_mmcfg_config_num = 0;
60fb9aa6f1SThomas Gleixner 
61fb9aa6f1SThomas Gleixner 	/* Size bits */
62fb9aa6f1SThomas Gleixner 	switch ((pciexbar >> 1) & 3) {
63fb9aa6f1SThomas Gleixner 	case 0:
64fb9aa6f1SThomas Gleixner 		mask = 0xf0000000U;
65fb9aa6f1SThomas Gleixner 		len  = 0x10000000U;
66fb9aa6f1SThomas Gleixner 		break;
67fb9aa6f1SThomas Gleixner 	case 1:
68fb9aa6f1SThomas Gleixner 		mask = 0xf8000000U;
69fb9aa6f1SThomas Gleixner 		len  = 0x08000000U;
70fb9aa6f1SThomas Gleixner 		break;
71fb9aa6f1SThomas Gleixner 	case 2:
72fb9aa6f1SThomas Gleixner 		mask = 0xfc000000U;
73fb9aa6f1SThomas Gleixner 		len  = 0x04000000U;
74fb9aa6f1SThomas Gleixner 		break;
75fb9aa6f1SThomas Gleixner 	default:
76fb9aa6f1SThomas Gleixner 		pci_mmcfg_config_num = 0;
77fb9aa6f1SThomas Gleixner 	}
78fb9aa6f1SThomas Gleixner 
79fb9aa6f1SThomas Gleixner 	/* Errata #2, things break when not aligned on a 256Mb boundary */
80fb9aa6f1SThomas Gleixner 	/* Can only happen in 64M/128M mode */
81fb9aa6f1SThomas Gleixner 
82fb9aa6f1SThomas Gleixner 	if ((pciexbar & mask) & 0x0fffffffU)
83fb9aa6f1SThomas Gleixner 		pci_mmcfg_config_num = 0;
84fb9aa6f1SThomas Gleixner 
85fb9aa6f1SThomas Gleixner 	/* Don't hit the APIC registers and their friends */
86fb9aa6f1SThomas Gleixner 	if ((pciexbar & mask) >= 0xf0000000U)
87fb9aa6f1SThomas Gleixner 		pci_mmcfg_config_num = 0;
88fb9aa6f1SThomas Gleixner 
89fb9aa6f1SThomas Gleixner 	if (pci_mmcfg_config_num) {
90fb9aa6f1SThomas Gleixner 		pci_mmcfg_config = kzalloc(sizeof(pci_mmcfg_config[0]), GFP_KERNEL);
91fb9aa6f1SThomas Gleixner 		if (!pci_mmcfg_config)
92fb9aa6f1SThomas Gleixner 			return NULL;
93fb9aa6f1SThomas Gleixner 		pci_mmcfg_config[0].address = pciexbar & mask;
94fb9aa6f1SThomas Gleixner 		pci_mmcfg_config[0].pci_segment = 0;
95fb9aa6f1SThomas Gleixner 		pci_mmcfg_config[0].start_bus_number = 0;
96fb9aa6f1SThomas Gleixner 		pci_mmcfg_config[0].end_bus_number = (len >> 20) - 1;
97fb9aa6f1SThomas Gleixner 	}
98fb9aa6f1SThomas Gleixner 
99fb9aa6f1SThomas Gleixner 	return "Intel Corporation 945G/GZ/P/PL Express Memory Controller Hub";
100fb9aa6f1SThomas Gleixner }
101fb9aa6f1SThomas Gleixner 
1027fd0da40SYinghai Lu static const char __init *pci_mmcfg_amd_fam10h(void)
1037fd0da40SYinghai Lu {
1047fd0da40SYinghai Lu 	u32 low, high, address;
1057fd0da40SYinghai Lu 	u64 base, msr;
1067fd0da40SYinghai Lu 	int i;
1077fd0da40SYinghai Lu 	unsigned segnbits = 0, busnbits;
1087fd0da40SYinghai Lu 
1095f0b2976SYinghai Lu 	if (!(pci_probe & PCI_CHECK_ENABLE_AMD_MMCONF))
1105f0b2976SYinghai Lu 		return NULL;
1115f0b2976SYinghai Lu 
1127fd0da40SYinghai Lu 	address = MSR_FAM10H_MMIO_CONF_BASE;
1137fd0da40SYinghai Lu 	if (rdmsr_safe(address, &low, &high))
1147fd0da40SYinghai Lu 		return NULL;
1157fd0da40SYinghai Lu 
1167fd0da40SYinghai Lu 	msr = high;
1177fd0da40SYinghai Lu 	msr <<= 32;
1187fd0da40SYinghai Lu 	msr |= low;
1197fd0da40SYinghai Lu 
1207fd0da40SYinghai Lu 	/* mmconfig is not enable */
1217fd0da40SYinghai Lu 	if (!(msr & FAM10H_MMIO_CONF_ENABLE))
1227fd0da40SYinghai Lu 		return NULL;
1237fd0da40SYinghai Lu 
1247fd0da40SYinghai Lu 	base = msr & (FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT);
1257fd0da40SYinghai Lu 
1267fd0da40SYinghai Lu 	busnbits = (msr >> FAM10H_MMIO_CONF_BUSRANGE_SHIFT) &
1277fd0da40SYinghai Lu 			 FAM10H_MMIO_CONF_BUSRANGE_MASK;
1287fd0da40SYinghai Lu 
1297fd0da40SYinghai Lu 	/*
1307fd0da40SYinghai Lu 	 * only handle bus 0 ?
1317fd0da40SYinghai Lu 	 * need to skip it
1327fd0da40SYinghai Lu 	 */
1337fd0da40SYinghai Lu 	if (!busnbits)
1347fd0da40SYinghai Lu 		return NULL;
1357fd0da40SYinghai Lu 
1367fd0da40SYinghai Lu 	if (busnbits > 8) {
1377fd0da40SYinghai Lu 		segnbits = busnbits - 8;
1387fd0da40SYinghai Lu 		busnbits = 8;
1397fd0da40SYinghai Lu 	}
1407fd0da40SYinghai Lu 
1417fd0da40SYinghai Lu 	pci_mmcfg_config_num = (1 << segnbits);
1427fd0da40SYinghai Lu 	pci_mmcfg_config = kzalloc(sizeof(pci_mmcfg_config[0]) *
1437fd0da40SYinghai Lu 				   pci_mmcfg_config_num, GFP_KERNEL);
1447fd0da40SYinghai Lu 	if (!pci_mmcfg_config)
1457fd0da40SYinghai Lu 		return NULL;
1467fd0da40SYinghai Lu 
1477fd0da40SYinghai Lu 	for (i = 0; i < (1 << segnbits); i++) {
1487fd0da40SYinghai Lu 		pci_mmcfg_config[i].address = base + (1<<28) * i;
1497fd0da40SYinghai Lu 		pci_mmcfg_config[i].pci_segment = i;
1507fd0da40SYinghai Lu 		pci_mmcfg_config[i].start_bus_number = 0;
1517fd0da40SYinghai Lu 		pci_mmcfg_config[i].end_bus_number = (1 << busnbits) - 1;
1527fd0da40SYinghai Lu 	}
1537fd0da40SYinghai Lu 
1547fd0da40SYinghai Lu 	return "AMD Family 10h NB";
1557fd0da40SYinghai Lu }
1567fd0da40SYinghai Lu 
157fb9aa6f1SThomas Gleixner struct pci_mmcfg_hostbridge_probe {
1587fd0da40SYinghai Lu 	u32 bus;
1597fd0da40SYinghai Lu 	u32 devfn;
160fb9aa6f1SThomas Gleixner 	u32 vendor;
161fb9aa6f1SThomas Gleixner 	u32 device;
162fb9aa6f1SThomas Gleixner 	const char *(*probe)(void);
163fb9aa6f1SThomas Gleixner };
164fb9aa6f1SThomas Gleixner 
165fb9aa6f1SThomas Gleixner static struct pci_mmcfg_hostbridge_probe pci_mmcfg_probes[] __initdata = {
1667fd0da40SYinghai Lu 	{ 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
1677fd0da40SYinghai Lu 	  PCI_DEVICE_ID_INTEL_E7520_MCH, pci_mmcfg_e7520 },
1687fd0da40SYinghai Lu 	{ 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
1697fd0da40SYinghai Lu 	  PCI_DEVICE_ID_INTEL_82945G_HB, pci_mmcfg_intel_945 },
1707fd0da40SYinghai Lu 	{ 0, PCI_DEVFN(0x18, 0), PCI_VENDOR_ID_AMD,
1717fd0da40SYinghai Lu 	  0x1200, pci_mmcfg_amd_fam10h },
1727fd0da40SYinghai Lu 	{ 0xff, PCI_DEVFN(0, 0), PCI_VENDOR_ID_AMD,
1737fd0da40SYinghai Lu 	  0x1200, pci_mmcfg_amd_fam10h },
174fb9aa6f1SThomas Gleixner };
175fb9aa6f1SThomas Gleixner 
176fb9aa6f1SThomas Gleixner static int __init pci_mmcfg_check_hostbridge(void)
177fb9aa6f1SThomas Gleixner {
178fb9aa6f1SThomas Gleixner 	u32 l;
1797fd0da40SYinghai Lu 	u32 bus, devfn;
180fb9aa6f1SThomas Gleixner 	u16 vendor, device;
181fb9aa6f1SThomas Gleixner 	int i;
182fb9aa6f1SThomas Gleixner 	const char *name;
183fb9aa6f1SThomas Gleixner 
184bb63b421SYinghai Lu 	if (!raw_pci_ops)
185bb63b421SYinghai Lu 		return 0;
186bb63b421SYinghai Lu 
187fb9aa6f1SThomas Gleixner 	pci_mmcfg_config_num = 0;
188fb9aa6f1SThomas Gleixner 	pci_mmcfg_config = NULL;
189fb9aa6f1SThomas Gleixner 	name = NULL;
190fb9aa6f1SThomas Gleixner 
191fb9aa6f1SThomas Gleixner 	for (i = 0; !name && i < ARRAY_SIZE(pci_mmcfg_probes); i++) {
1927fd0da40SYinghai Lu 		bus =  pci_mmcfg_probes[i].bus;
1937fd0da40SYinghai Lu 		devfn = pci_mmcfg_probes[i].devfn;
194bb63b421SYinghai Lu 		raw_pci_ops->read(0, bus, devfn, 0, 4, &l);
1957fd0da40SYinghai Lu 		vendor = l & 0xffff;
1967fd0da40SYinghai Lu 		device = (l >> 16) & 0xffff;
1977fd0da40SYinghai Lu 
198fb9aa6f1SThomas Gleixner 		if (pci_mmcfg_probes[i].vendor == vendor &&
199fb9aa6f1SThomas Gleixner 		    pci_mmcfg_probes[i].device == device)
200fb9aa6f1SThomas Gleixner 			name = pci_mmcfg_probes[i].probe();
201fb9aa6f1SThomas Gleixner 	}
202fb9aa6f1SThomas Gleixner 
203fb9aa6f1SThomas Gleixner 	if (name) {
204fb9aa6f1SThomas Gleixner 		printk(KERN_INFO "PCI: Found %s %s MMCONFIG support.\n",
205fb9aa6f1SThomas Gleixner 		       name, pci_mmcfg_config_num ? "with" : "without");
206fb9aa6f1SThomas Gleixner 	}
207fb9aa6f1SThomas Gleixner 
208fb9aa6f1SThomas Gleixner 	return name != NULL;
209fb9aa6f1SThomas Gleixner }
210fb9aa6f1SThomas Gleixner 
211ebd60cd6SYinghai Lu static void __init pci_mmcfg_insert_resources(void)
212fb9aa6f1SThomas Gleixner {
213fb9aa6f1SThomas Gleixner #define PCI_MMCFG_RESOURCE_NAME_LEN 19
214fb9aa6f1SThomas Gleixner 	int i;
215fb9aa6f1SThomas Gleixner 	struct resource *res;
216fb9aa6f1SThomas Gleixner 	char *names;
217fb9aa6f1SThomas Gleixner 	unsigned num_buses;
218fb9aa6f1SThomas Gleixner 
219fb9aa6f1SThomas Gleixner 	res = kcalloc(PCI_MMCFG_RESOURCE_NAME_LEN + sizeof(*res),
220fb9aa6f1SThomas Gleixner 			pci_mmcfg_config_num, GFP_KERNEL);
221fb9aa6f1SThomas Gleixner 	if (!res) {
222fb9aa6f1SThomas Gleixner 		printk(KERN_ERR "PCI: Unable to allocate MMCONFIG resources\n");
223fb9aa6f1SThomas Gleixner 		return;
224fb9aa6f1SThomas Gleixner 	}
225fb9aa6f1SThomas Gleixner 
226fb9aa6f1SThomas Gleixner 	names = (void *)&res[pci_mmcfg_config_num];
227fb9aa6f1SThomas Gleixner 	for (i = 0; i < pci_mmcfg_config_num; i++, res++) {
228fb9aa6f1SThomas Gleixner 		struct acpi_mcfg_allocation *cfg = &pci_mmcfg_config[i];
229fb9aa6f1SThomas Gleixner 		num_buses = cfg->end_bus_number - cfg->start_bus_number + 1;
230fb9aa6f1SThomas Gleixner 		res->name = names;
231fb9aa6f1SThomas Gleixner 		snprintf(names, PCI_MMCFG_RESOURCE_NAME_LEN, "PCI MMCONFIG %u",
232fb9aa6f1SThomas Gleixner 			 cfg->pci_segment);
233fb9aa6f1SThomas Gleixner 		res->start = cfg->address;
234fb9aa6f1SThomas Gleixner 		res->end = res->start + (num_buses << 20) - 1;
235ebd60cd6SYinghai Lu 		res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
236fb9aa6f1SThomas Gleixner 		insert_resource(&iomem_resource, res);
237fb9aa6f1SThomas Gleixner 		names += PCI_MMCFG_RESOURCE_NAME_LEN;
238fb9aa6f1SThomas Gleixner 	}
239fb9aa6f1SThomas Gleixner 
240fb9aa6f1SThomas Gleixner 	/* Mark that the resources have been inserted. */
241fb9aa6f1SThomas Gleixner 	pci_mmcfg_resources_inserted = 1;
242fb9aa6f1SThomas Gleixner }
243fb9aa6f1SThomas Gleixner 
2447752d5cfSRobert Hancock static acpi_status __init check_mcfg_resource(struct acpi_resource *res,
2457752d5cfSRobert Hancock 					      void *data)
2467752d5cfSRobert Hancock {
2477752d5cfSRobert Hancock 	struct resource *mcfg_res = data;
2487752d5cfSRobert Hancock 	struct acpi_resource_address64 address;
2497752d5cfSRobert Hancock 	acpi_status status;
2507752d5cfSRobert Hancock 
2517752d5cfSRobert Hancock 	if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
2527752d5cfSRobert Hancock 		struct acpi_resource_fixed_memory32 *fixmem32 =
2537752d5cfSRobert Hancock 			&res->data.fixed_memory32;
2547752d5cfSRobert Hancock 		if (!fixmem32)
2557752d5cfSRobert Hancock 			return AE_OK;
2567752d5cfSRobert Hancock 		if ((mcfg_res->start >= fixmem32->address) &&
2577752d5cfSRobert Hancock 		    (mcfg_res->end < (fixmem32->address +
2587752d5cfSRobert Hancock 				      fixmem32->address_length))) {
2597752d5cfSRobert Hancock 			mcfg_res->flags = 1;
2607752d5cfSRobert Hancock 			return AE_CTRL_TERMINATE;
2617752d5cfSRobert Hancock 		}
2627752d5cfSRobert Hancock 	}
2637752d5cfSRobert Hancock 	if ((res->type != ACPI_RESOURCE_TYPE_ADDRESS32) &&
2647752d5cfSRobert Hancock 	    (res->type != ACPI_RESOURCE_TYPE_ADDRESS64))
2657752d5cfSRobert Hancock 		return AE_OK;
2667752d5cfSRobert Hancock 
2677752d5cfSRobert Hancock 	status = acpi_resource_to_address64(res, &address);
2687752d5cfSRobert Hancock 	if (ACPI_FAILURE(status) ||
2697752d5cfSRobert Hancock 	   (address.address_length <= 0) ||
2707752d5cfSRobert Hancock 	   (address.resource_type != ACPI_MEMORY_RANGE))
2717752d5cfSRobert Hancock 		return AE_OK;
2727752d5cfSRobert Hancock 
2737752d5cfSRobert Hancock 	if ((mcfg_res->start >= address.minimum) &&
2747752d5cfSRobert Hancock 	    (mcfg_res->end < (address.minimum + address.address_length))) {
2757752d5cfSRobert Hancock 		mcfg_res->flags = 1;
2767752d5cfSRobert Hancock 		return AE_CTRL_TERMINATE;
2777752d5cfSRobert Hancock 	}
2787752d5cfSRobert Hancock 	return AE_OK;
2797752d5cfSRobert Hancock }
2807752d5cfSRobert Hancock 
2817752d5cfSRobert Hancock static acpi_status __init find_mboard_resource(acpi_handle handle, u32 lvl,
2827752d5cfSRobert Hancock 		void *context, void **rv)
2837752d5cfSRobert Hancock {
2847752d5cfSRobert Hancock 	struct resource *mcfg_res = context;
2857752d5cfSRobert Hancock 
2867752d5cfSRobert Hancock 	acpi_walk_resources(handle, METHOD_NAME__CRS,
2877752d5cfSRobert Hancock 			    check_mcfg_resource, context);
2887752d5cfSRobert Hancock 
2897752d5cfSRobert Hancock 	if (mcfg_res->flags)
2907752d5cfSRobert Hancock 		return AE_CTRL_TERMINATE;
2917752d5cfSRobert Hancock 
2927752d5cfSRobert Hancock 	return AE_OK;
2937752d5cfSRobert Hancock }
2947752d5cfSRobert Hancock 
295a83fe32fSYinghai Lu static int __init is_acpi_reserved(u64 start, u64 end, unsigned not_used)
2967752d5cfSRobert Hancock {
2977752d5cfSRobert Hancock 	struct resource mcfg_res;
2987752d5cfSRobert Hancock 
2997752d5cfSRobert Hancock 	mcfg_res.start = start;
3007752d5cfSRobert Hancock 	mcfg_res.end = end;
3017752d5cfSRobert Hancock 	mcfg_res.flags = 0;
3027752d5cfSRobert Hancock 
3037752d5cfSRobert Hancock 	acpi_get_devices("PNP0C01", find_mboard_resource, &mcfg_res, NULL);
3047752d5cfSRobert Hancock 
3057752d5cfSRobert Hancock 	if (!mcfg_res.flags)
3067752d5cfSRobert Hancock 		acpi_get_devices("PNP0C02", find_mboard_resource, &mcfg_res,
3077752d5cfSRobert Hancock 				 NULL);
3087752d5cfSRobert Hancock 
3097752d5cfSRobert Hancock 	return mcfg_res.flags;
3107752d5cfSRobert Hancock }
3117752d5cfSRobert Hancock 
312a83fe32fSYinghai Lu typedef int (*check_reserved_t)(u64 start, u64 end, unsigned type);
313a83fe32fSYinghai Lu 
314a83fe32fSYinghai Lu static int __init is_mmconf_reserved(check_reserved_t is_reserved,
315a83fe32fSYinghai Lu 		u64 addr, u64 size, int i,
316a83fe32fSYinghai Lu 		typeof(pci_mmcfg_config[0]) *cfg, int with_e820)
317a83fe32fSYinghai Lu {
318a83fe32fSYinghai Lu 	u64 old_size = size;
319a83fe32fSYinghai Lu 	int valid = 0;
320a83fe32fSYinghai Lu 
321a83fe32fSYinghai Lu 	while (!is_reserved(addr, addr + size - 1, E820_RESERVED)) {
322a83fe32fSYinghai Lu 		size >>= 1;
323a83fe32fSYinghai Lu 		if (size < (16UL<<20))
324a83fe32fSYinghai Lu 			break;
325a83fe32fSYinghai Lu 	}
326a83fe32fSYinghai Lu 
327a83fe32fSYinghai Lu 	if (size >= (16UL<<20) || size == old_size) {
328a83fe32fSYinghai Lu 		printk(KERN_NOTICE
329a83fe32fSYinghai Lu 		       "PCI: MCFG area at %Lx reserved in %s\n",
330a83fe32fSYinghai Lu 			addr, with_e820?"E820":"ACPI motherboard resources");
331a83fe32fSYinghai Lu 		valid = 1;
332a83fe32fSYinghai Lu 
333a83fe32fSYinghai Lu 		if (old_size != size) {
334a83fe32fSYinghai Lu 			/* update end_bus_number */
335a83fe32fSYinghai Lu 			cfg->end_bus_number = cfg->start_bus_number + ((size>>20) - 1);
336a83fe32fSYinghai Lu 			printk(KERN_NOTICE "PCI: updated MCFG configuration %d: base %lx "
337a83fe32fSYinghai Lu 			       "segment %hu buses %u - %u\n",
338a83fe32fSYinghai Lu 			       i, (unsigned long)cfg->address, cfg->pci_segment,
339a83fe32fSYinghai Lu 			       (unsigned int)cfg->start_bus_number,
340a83fe32fSYinghai Lu 			       (unsigned int)cfg->end_bus_number);
341a83fe32fSYinghai Lu 		}
342a83fe32fSYinghai Lu 	}
343a83fe32fSYinghai Lu 
344a83fe32fSYinghai Lu 	return valid;
345a83fe32fSYinghai Lu }
346a83fe32fSYinghai Lu 
347bb63b421SYinghai Lu static void __init pci_mmcfg_reject_broken(int early)
348fb9aa6f1SThomas Gleixner {
349fb9aa6f1SThomas Gleixner 	typeof(pci_mmcfg_config[0]) *cfg;
3507752d5cfSRobert Hancock 	int i;
351fb9aa6f1SThomas Gleixner 
352fb9aa6f1SThomas Gleixner 	if ((pci_mmcfg_config_num == 0) ||
353fb9aa6f1SThomas Gleixner 	    (pci_mmcfg_config == NULL) ||
354fb9aa6f1SThomas Gleixner 	    (pci_mmcfg_config[0].address == 0))
355fb9aa6f1SThomas Gleixner 		return;
356fb9aa6f1SThomas Gleixner 
357fb9aa6f1SThomas Gleixner 	cfg = &pci_mmcfg_config[0];
358fb9aa6f1SThomas Gleixner 
3597752d5cfSRobert Hancock 	for (i = 0; i < pci_mmcfg_config_num; i++) {
36005c58b8aSYinghai Lu 		int valid = 0;
361a83fe32fSYinghai Lu 		u64 addr, size;
362a83fe32fSYinghai Lu 
3637752d5cfSRobert Hancock 		cfg = &pci_mmcfg_config[i];
364a83fe32fSYinghai Lu 		addr = cfg->start_bus_number;
365a83fe32fSYinghai Lu 		addr <<= 20;
366a83fe32fSYinghai Lu 		addr += cfg->address;
367a83fe32fSYinghai Lu 		size = cfg->end_bus_number + 1 - cfg->start_bus_number;
368a83fe32fSYinghai Lu 		size <<= 20;
36905c58b8aSYinghai Lu 		printk(KERN_NOTICE "PCI: MCFG configuration %d: base %lx "
3707752d5cfSRobert Hancock 		       "segment %hu buses %u - %u\n",
3717752d5cfSRobert Hancock 		       i, (unsigned long)cfg->address, cfg->pci_segment,
3727752d5cfSRobert Hancock 		       (unsigned int)cfg->start_bus_number,
3737752d5cfSRobert Hancock 		       (unsigned int)cfg->end_bus_number);
37405c58b8aSYinghai Lu 
375a83fe32fSYinghai Lu 		if (!early)
376a83fe32fSYinghai Lu 			valid = is_mmconf_reserved(is_acpi_reserved, addr, size, i, cfg, 0);
37705c58b8aSYinghai Lu 
37805c58b8aSYinghai Lu 		if (valid)
37905c58b8aSYinghai Lu 			continue;
38005c58b8aSYinghai Lu 
38105c58b8aSYinghai Lu 		if (!early)
382fb9aa6f1SThomas Gleixner 			printk(KERN_ERR "PCI: BIOS Bug: MCFG area at %Lx is not"
3837752d5cfSRobert Hancock 			       " reserved in ACPI motherboard resources\n",
3847752d5cfSRobert Hancock 			       cfg->address);
385a83fe32fSYinghai Lu 
3867752d5cfSRobert Hancock 		/* Don't try to do this check unless configuration
387bb63b421SYinghai Lu 		   type 1 is available. how about type 2 ?*/
388a83fe32fSYinghai Lu 		if (raw_pci_ops)
389a83fe32fSYinghai Lu 			valid = is_mmconf_reserved(e820_all_mapped, addr, size, i, cfg, 1);
39005c58b8aSYinghai Lu 
39105c58b8aSYinghai Lu 		if (!valid)
39205c58b8aSYinghai Lu 			goto reject;
3937752d5cfSRobert Hancock 	}
3947752d5cfSRobert Hancock 
395fb9aa6f1SThomas Gleixner 	return;
396fb9aa6f1SThomas Gleixner 
397fb9aa6f1SThomas Gleixner reject:
398ef310237SDave Jones 	printk(KERN_INFO "PCI: Not using MMCONFIG.\n");
3990b64ad71SYinghai Lu 	pci_mmcfg_arch_free();
400fb9aa6f1SThomas Gleixner 	kfree(pci_mmcfg_config);
401fb9aa6f1SThomas Gleixner 	pci_mmcfg_config = NULL;
402fb9aa6f1SThomas Gleixner 	pci_mmcfg_config_num = 0;
403fb9aa6f1SThomas Gleixner }
404fb9aa6f1SThomas Gleixner 
40505c58b8aSYinghai Lu static int __initdata known_bridge;
40605c58b8aSYinghai Lu 
407968cbfadSThomas Gleixner static void __init __pci_mmcfg_init(int early)
408fb9aa6f1SThomas Gleixner {
4097752d5cfSRobert Hancock 	/* MMCONFIG disabled */
4107752d5cfSRobert Hancock 	if ((pci_probe & PCI_PROBE_MMCONF) == 0)
4117752d5cfSRobert Hancock 		return;
4127752d5cfSRobert Hancock 
4137752d5cfSRobert Hancock 	/* MMCONFIG already enabled */
41405c58b8aSYinghai Lu 	if (!early && !(pci_probe & PCI_PROBE_MASK & ~PCI_PROBE_MMCONF))
4157752d5cfSRobert Hancock 		return;
4167752d5cfSRobert Hancock 
41705c58b8aSYinghai Lu 	/* for late to exit */
41805c58b8aSYinghai Lu 	if (known_bridge)
41905c58b8aSYinghai Lu 		return;
4207752d5cfSRobert Hancock 
421bb63b421SYinghai Lu 	if (early) {
42205c58b8aSYinghai Lu 		if (pci_mmcfg_check_hostbridge())
42305c58b8aSYinghai Lu 			known_bridge = 1;
42405c58b8aSYinghai Lu 	}
42505c58b8aSYinghai Lu 
42605c58b8aSYinghai Lu 	if (!known_bridge) {
42705c58b8aSYinghai Lu 		acpi_table_parse(ACPI_SIG_MCFG, acpi_parse_mcfg);
428bb63b421SYinghai Lu 		pci_mmcfg_reject_broken(early);
42905c58b8aSYinghai Lu 	}
4307752d5cfSRobert Hancock 
431fb9aa6f1SThomas Gleixner 	if ((pci_mmcfg_config_num == 0) ||
432fb9aa6f1SThomas Gleixner 	    (pci_mmcfg_config == NULL) ||
433fb9aa6f1SThomas Gleixner 	    (pci_mmcfg_config[0].address == 0))
434fb9aa6f1SThomas Gleixner 		return;
435fb9aa6f1SThomas Gleixner 
436ebd60cd6SYinghai Lu 	if (pci_mmcfg_arch_init())
437fb9aa6f1SThomas Gleixner 		pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
438ebd60cd6SYinghai Lu 	else {
439fb9aa6f1SThomas Gleixner 		/*
440fb9aa6f1SThomas Gleixner 		 * Signal not to attempt to insert mmcfg resources because
441fb9aa6f1SThomas Gleixner 		 * the architecture mmcfg setup could not initialize.
442fb9aa6f1SThomas Gleixner 		 */
443fb9aa6f1SThomas Gleixner 		pci_mmcfg_resources_inserted = 1;
444fb9aa6f1SThomas Gleixner 	}
445fb9aa6f1SThomas Gleixner }
446fb9aa6f1SThomas Gleixner 
447bb63b421SYinghai Lu void __init pci_mmcfg_early_init(void)
44805c58b8aSYinghai Lu {
449bb63b421SYinghai Lu 	__pci_mmcfg_init(1);
45005c58b8aSYinghai Lu }
45105c58b8aSYinghai Lu 
45205c58b8aSYinghai Lu void __init pci_mmcfg_late_init(void)
45305c58b8aSYinghai Lu {
454bb63b421SYinghai Lu 	__pci_mmcfg_init(0);
45505c58b8aSYinghai Lu }
45605c58b8aSYinghai Lu 
457fb9aa6f1SThomas Gleixner static int __init pci_mmcfg_late_insert_resources(void)
458fb9aa6f1SThomas Gleixner {
459fb9aa6f1SThomas Gleixner 	/*
460fb9aa6f1SThomas Gleixner 	 * If resources are already inserted or we are not using MMCONFIG,
461fb9aa6f1SThomas Gleixner 	 * don't insert the resources.
462fb9aa6f1SThomas Gleixner 	 */
463fb9aa6f1SThomas Gleixner 	if ((pci_mmcfg_resources_inserted == 1) ||
464fb9aa6f1SThomas Gleixner 	    (pci_probe & PCI_PROBE_MMCONF) == 0 ||
465fb9aa6f1SThomas Gleixner 	    (pci_mmcfg_config_num == 0) ||
466fb9aa6f1SThomas Gleixner 	    (pci_mmcfg_config == NULL) ||
467fb9aa6f1SThomas Gleixner 	    (pci_mmcfg_config[0].address == 0))
468fb9aa6f1SThomas Gleixner 		return 1;
469fb9aa6f1SThomas Gleixner 
470fb9aa6f1SThomas Gleixner 	/*
471fb9aa6f1SThomas Gleixner 	 * Attempt to insert the mmcfg resources but not with the busy flag
472fb9aa6f1SThomas Gleixner 	 * marked so it won't cause request errors when __request_region is
473fb9aa6f1SThomas Gleixner 	 * called.
474fb9aa6f1SThomas Gleixner 	 */
475ebd60cd6SYinghai Lu 	pci_mmcfg_insert_resources();
476fb9aa6f1SThomas Gleixner 
477fb9aa6f1SThomas Gleixner 	return 0;
478fb9aa6f1SThomas Gleixner }
479fb9aa6f1SThomas Gleixner 
480fb9aa6f1SThomas Gleixner /*
481fb9aa6f1SThomas Gleixner  * Perform MMCONFIG resource insertion after PCI initialization to allow for
482fb9aa6f1SThomas Gleixner  * misprogrammed MCFG tables that state larger sizes but actually conflict
483fb9aa6f1SThomas Gleixner  * with other system resources.
484fb9aa6f1SThomas Gleixner  */
485fb9aa6f1SThomas Gleixner late_initcall(pci_mmcfg_late_insert_resources);
486