xref: /openbmc/linux/arch/x86/pci/mmconfig-shared.c (revision 574a59414083df3911e5a1514742959b412b6947)
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>
165f0db7a2SFeng Tang #include <linux/sfi_acpi.h>
17fb9aa6f1SThomas Gleixner #include <linux/bitmap.h>
189a08f7d3SBjorn Helgaas #include <linux/dmi.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
20376f70acSJiang Liu #include <linux/mutex.h>
21376f70acSJiang Liu #include <linux/rculist.h>
22fb9aa6f1SThomas Gleixner #include <asm/e820.h>
2382487711SJaswinder Singh Rajput #include <asm/pci_x86.h>
245f0db7a2SFeng Tang #include <asm/acpi.h>
25fb9aa6f1SThomas Gleixner 
26f4a2d584SLen Brown #define PREFIX "PCI: "
27a192a958SLen Brown 
28fb9aa6f1SThomas Gleixner /* Indicate if the mmcfg resources have been placed into the resource table. */
2995c5e92fSJiang Liu static bool pci_mmcfg_running_state;
309c95111bSJiang Liu static bool pci_mmcfg_arch_init_failed;
31376f70acSJiang Liu static DEFINE_MUTEX(pci_mmcfg_lock);
32fb9aa6f1SThomas Gleixner 
33ff097dddSBjorn Helgaas LIST_HEAD(pci_mmcfg_list);
34ff097dddSBjorn Helgaas 
35ba2afbabSBjorn Helgaas static __init void pci_mmconfig_remove(struct pci_mmcfg_region *cfg)
367da7d360SBjorn Helgaas {
3756ddf4d3SBjorn Helgaas 	if (cfg->res.parent)
3856ddf4d3SBjorn Helgaas 		release_resource(&cfg->res);
39ff097dddSBjorn Helgaas 	list_del(&cfg->list);
40ff097dddSBjorn Helgaas 	kfree(cfg);
4156ddf4d3SBjorn Helgaas }
42ba2afbabSBjorn Helgaas 
43ba2afbabSBjorn Helgaas static __init void free_all_mmcfg(void)
44ba2afbabSBjorn Helgaas {
45ba2afbabSBjorn Helgaas 	struct pci_mmcfg_region *cfg, *tmp;
46ba2afbabSBjorn Helgaas 
47ba2afbabSBjorn Helgaas 	pci_mmcfg_arch_free();
48ba2afbabSBjorn Helgaas 	list_for_each_entry_safe(cfg, tmp, &pci_mmcfg_list, list)
49ba2afbabSBjorn Helgaas 		pci_mmconfig_remove(cfg);
50ff097dddSBjorn Helgaas }
51ff097dddSBjorn Helgaas 
52376f70acSJiang Liu static __devinit void list_add_sorted(struct pci_mmcfg_region *new)
53ff097dddSBjorn Helgaas {
54ff097dddSBjorn Helgaas 	struct pci_mmcfg_region *cfg;
55ff097dddSBjorn Helgaas 
56ff097dddSBjorn Helgaas 	/* keep list sorted by segment and starting bus number */
57376f70acSJiang Liu 	list_for_each_entry_rcu(cfg, &pci_mmcfg_list, list) {
58ff097dddSBjorn Helgaas 		if (cfg->segment > new->segment ||
59ff097dddSBjorn Helgaas 		    (cfg->segment == new->segment &&
60ff097dddSBjorn Helgaas 		     cfg->start_bus >= new->start_bus)) {
61376f70acSJiang Liu 			list_add_tail_rcu(&new->list, &cfg->list);
62ff097dddSBjorn Helgaas 			return;
63ff097dddSBjorn Helgaas 		}
64ff097dddSBjorn Helgaas 	}
65376f70acSJiang Liu 	list_add_tail_rcu(&new->list, &pci_mmcfg_list);
667da7d360SBjorn Helgaas }
677da7d360SBjorn Helgaas 
68846e4023SJiang Liu static __devinit struct pci_mmcfg_region *pci_mmconfig_alloc(int segment,
69846e4023SJiang Liu 							     int start,
70d215a9c8SBjorn Helgaas 							     int end, u64 addr)
71068258bcSYinghai Lu {
72d215a9c8SBjorn Helgaas 	struct pci_mmcfg_region *new;
7356ddf4d3SBjorn Helgaas 	struct resource *res;
74068258bcSYinghai Lu 
75f7ca6984SBjorn Helgaas 	if (addr == 0)
76f7ca6984SBjorn Helgaas 		return NULL;
77f7ca6984SBjorn Helgaas 
78ff097dddSBjorn Helgaas 	new = kzalloc(sizeof(*new), GFP_KERNEL);
79068258bcSYinghai Lu 	if (!new)
807da7d360SBjorn Helgaas 		return NULL;
81068258bcSYinghai Lu 
8295cf1cf0SBjorn Helgaas 	new->address = addr;
8395cf1cf0SBjorn Helgaas 	new->segment = segment;
8495cf1cf0SBjorn Helgaas 	new->start_bus = start;
8595cf1cf0SBjorn Helgaas 	new->end_bus = end;
867da7d360SBjorn Helgaas 
8756ddf4d3SBjorn Helgaas 	res = &new->res;
8856ddf4d3SBjorn Helgaas 	res->start = addr + PCI_MMCFG_BUS_OFFSET(start);
891ca98fa6SBjorn Helgaas 	res->end = addr + PCI_MMCFG_BUS_OFFSET(end + 1) - 1;
9056ddf4d3SBjorn Helgaas 	res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
9156ddf4d3SBjorn Helgaas 	snprintf(new->name, PCI_MMCFG_RESOURCE_NAME_LEN,
9256ddf4d3SBjorn Helgaas 		 "PCI MMCONFIG %04x [bus %02x-%02x]", segment, start, end);
9356ddf4d3SBjorn Helgaas 	res->name = new->name;
9456ddf4d3SBjorn Helgaas 
95ff097dddSBjorn Helgaas 	return new;
96068258bcSYinghai Lu }
97068258bcSYinghai Lu 
98846e4023SJiang Liu static __init struct pci_mmcfg_region *pci_mmconfig_add(int segment, int start,
99846e4023SJiang Liu 							int end, u64 addr)
100846e4023SJiang Liu {
101846e4023SJiang Liu 	struct pci_mmcfg_region *new;
102846e4023SJiang Liu 
103846e4023SJiang Liu 	new = pci_mmconfig_alloc(segment, start, end, addr);
104376f70acSJiang Liu 	if (new) {
105376f70acSJiang Liu 		mutex_lock(&pci_mmcfg_lock);
106846e4023SJiang Liu 		list_add_sorted(new);
107376f70acSJiang Liu 		mutex_unlock(&pci_mmcfg_lock);
1089c95111bSJiang Liu 
1099c95111bSJiang Liu 		printk(KERN_INFO PREFIX
1109c95111bSJiang Liu 		       "MMCONFIG for domain %04x [bus %02x-%02x] at %pR "
1119c95111bSJiang Liu 		       "(base %#lx)\n",
1129c95111bSJiang Liu 		       segment, start, end, &new->res, (unsigned long)addr);
113376f70acSJiang Liu 	}
114846e4023SJiang Liu 
115846e4023SJiang Liu 	return new;
116846e4023SJiang Liu }
117846e4023SJiang Liu 
118f6e1d8ccSBjorn Helgaas struct pci_mmcfg_region *pci_mmconfig_lookup(int segment, int bus)
119f6e1d8ccSBjorn Helgaas {
120f6e1d8ccSBjorn Helgaas 	struct pci_mmcfg_region *cfg;
121f6e1d8ccSBjorn Helgaas 
122376f70acSJiang Liu 	list_for_each_entry_rcu(cfg, &pci_mmcfg_list, list)
123f6e1d8ccSBjorn Helgaas 		if (cfg->segment == segment &&
124f6e1d8ccSBjorn Helgaas 		    cfg->start_bus <= bus && bus <= cfg->end_bus)
125f6e1d8ccSBjorn Helgaas 			return cfg;
126f6e1d8ccSBjorn Helgaas 
127f6e1d8ccSBjorn Helgaas 	return NULL;
128f6e1d8ccSBjorn Helgaas }
129f6e1d8ccSBjorn Helgaas 
130fb9aa6f1SThomas Gleixner static const char __init *pci_mmcfg_e7520(void)
131fb9aa6f1SThomas Gleixner {
132fb9aa6f1SThomas Gleixner 	u32 win;
133bb63b421SYinghai Lu 	raw_pci_ops->read(0, 0, PCI_DEVFN(0, 0), 0xce, 2, &win);
134fb9aa6f1SThomas Gleixner 
135fb9aa6f1SThomas Gleixner 	win = win & 0xf000;
136fb9aa6f1SThomas Gleixner 	if (win == 0x0000 || win == 0xf000)
137fb9aa6f1SThomas Gleixner 		return NULL;
138068258bcSYinghai Lu 
1397da7d360SBjorn Helgaas 	if (pci_mmconfig_add(0, 0, 255, win << 16) == NULL)
140068258bcSYinghai Lu 		return NULL;
141068258bcSYinghai Lu 
142fb9aa6f1SThomas Gleixner 	return "Intel Corporation E7520 Memory Controller Hub";
143fb9aa6f1SThomas Gleixner }
144fb9aa6f1SThomas Gleixner 
145fb9aa6f1SThomas Gleixner static const char __init *pci_mmcfg_intel_945(void)
146fb9aa6f1SThomas Gleixner {
147fb9aa6f1SThomas Gleixner 	u32 pciexbar, mask = 0, len = 0;
148fb9aa6f1SThomas Gleixner 
149bb63b421SYinghai Lu 	raw_pci_ops->read(0, 0, PCI_DEVFN(0, 0), 0x48, 4, &pciexbar);
150fb9aa6f1SThomas Gleixner 
151fb9aa6f1SThomas Gleixner 	/* Enable bit */
152fb9aa6f1SThomas Gleixner 	if (!(pciexbar & 1))
153068258bcSYinghai Lu 		return NULL;
154fb9aa6f1SThomas Gleixner 
155fb9aa6f1SThomas Gleixner 	/* Size bits */
156fb9aa6f1SThomas Gleixner 	switch ((pciexbar >> 1) & 3) {
157fb9aa6f1SThomas Gleixner 	case 0:
158fb9aa6f1SThomas Gleixner 		mask = 0xf0000000U;
159fb9aa6f1SThomas Gleixner 		len  = 0x10000000U;
160fb9aa6f1SThomas Gleixner 		break;
161fb9aa6f1SThomas Gleixner 	case 1:
162fb9aa6f1SThomas Gleixner 		mask = 0xf8000000U;
163fb9aa6f1SThomas Gleixner 		len  = 0x08000000U;
164fb9aa6f1SThomas Gleixner 		break;
165fb9aa6f1SThomas Gleixner 	case 2:
166fb9aa6f1SThomas Gleixner 		mask = 0xfc000000U;
167fb9aa6f1SThomas Gleixner 		len  = 0x04000000U;
168fb9aa6f1SThomas Gleixner 		break;
169fb9aa6f1SThomas Gleixner 	default:
170068258bcSYinghai Lu 		return NULL;
171fb9aa6f1SThomas Gleixner 	}
172fb9aa6f1SThomas Gleixner 
173fb9aa6f1SThomas Gleixner 	/* Errata #2, things break when not aligned on a 256Mb boundary */
174fb9aa6f1SThomas Gleixner 	/* Can only happen in 64M/128M mode */
175fb9aa6f1SThomas Gleixner 
176fb9aa6f1SThomas Gleixner 	if ((pciexbar & mask) & 0x0fffffffU)
177068258bcSYinghai Lu 		return NULL;
178fb9aa6f1SThomas Gleixner 
179fb9aa6f1SThomas Gleixner 	/* Don't hit the APIC registers and their friends */
180fb9aa6f1SThomas Gleixner 	if ((pciexbar & mask) >= 0xf0000000U)
181fb9aa6f1SThomas Gleixner 		return NULL;
182068258bcSYinghai Lu 
1837da7d360SBjorn Helgaas 	if (pci_mmconfig_add(0, 0, (len >> 20) - 1, pciexbar & mask) == NULL)
184068258bcSYinghai Lu 		return NULL;
185068258bcSYinghai Lu 
186fb9aa6f1SThomas Gleixner 	return "Intel Corporation 945G/GZ/P/PL Express Memory Controller Hub";
187fb9aa6f1SThomas Gleixner }
188fb9aa6f1SThomas Gleixner 
1897fd0da40SYinghai Lu static const char __init *pci_mmcfg_amd_fam10h(void)
1907fd0da40SYinghai Lu {
1917fd0da40SYinghai Lu 	u32 low, high, address;
1927fd0da40SYinghai Lu 	u64 base, msr;
1937fd0da40SYinghai Lu 	int i;
1947da7d360SBjorn Helgaas 	unsigned segnbits = 0, busnbits, end_bus;
1957fd0da40SYinghai Lu 
1965f0b2976SYinghai Lu 	if (!(pci_probe & PCI_CHECK_ENABLE_AMD_MMCONF))
1975f0b2976SYinghai Lu 		return NULL;
1985f0b2976SYinghai Lu 
1997fd0da40SYinghai Lu 	address = MSR_FAM10H_MMIO_CONF_BASE;
2007fd0da40SYinghai Lu 	if (rdmsr_safe(address, &low, &high))
2017fd0da40SYinghai Lu 		return NULL;
2027fd0da40SYinghai Lu 
2037fd0da40SYinghai Lu 	msr = high;
2047fd0da40SYinghai Lu 	msr <<= 32;
2057fd0da40SYinghai Lu 	msr |= low;
2067fd0da40SYinghai Lu 
2077fd0da40SYinghai Lu 	/* mmconfig is not enable */
2087fd0da40SYinghai Lu 	if (!(msr & FAM10H_MMIO_CONF_ENABLE))
2097fd0da40SYinghai Lu 		return NULL;
2107fd0da40SYinghai Lu 
2117fd0da40SYinghai Lu 	base = msr & (FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT);
2127fd0da40SYinghai Lu 
2137fd0da40SYinghai Lu 	busnbits = (msr >> FAM10H_MMIO_CONF_BUSRANGE_SHIFT) &
2147fd0da40SYinghai Lu 			 FAM10H_MMIO_CONF_BUSRANGE_MASK;
2157fd0da40SYinghai Lu 
2167fd0da40SYinghai Lu 	/*
2177fd0da40SYinghai Lu 	 * only handle bus 0 ?
2187fd0da40SYinghai Lu 	 * need to skip it
2197fd0da40SYinghai Lu 	 */
2207fd0da40SYinghai Lu 	if (!busnbits)
2217fd0da40SYinghai Lu 		return NULL;
2227fd0da40SYinghai Lu 
2237fd0da40SYinghai Lu 	if (busnbits > 8) {
2247fd0da40SYinghai Lu 		segnbits = busnbits - 8;
2257fd0da40SYinghai Lu 		busnbits = 8;
2267fd0da40SYinghai Lu 	}
2277fd0da40SYinghai Lu 
2287da7d360SBjorn Helgaas 	end_bus = (1 << busnbits) - 1;
229068258bcSYinghai Lu 	for (i = 0; i < (1 << segnbits); i++)
2307da7d360SBjorn Helgaas 		if (pci_mmconfig_add(i, 0, end_bus,
2317da7d360SBjorn Helgaas 				     base + (1<<28) * i) == NULL) {
2327da7d360SBjorn Helgaas 			free_all_mmcfg();
2337da7d360SBjorn Helgaas 			return NULL;
2347da7d360SBjorn Helgaas 		}
2357fd0da40SYinghai Lu 
2367fd0da40SYinghai Lu 	return "AMD Family 10h NB";
2377fd0da40SYinghai Lu }
2387fd0da40SYinghai Lu 
2395546d6f5SEd Swierk static bool __initdata mcp55_checked;
2405546d6f5SEd Swierk static const char __init *pci_mmcfg_nvidia_mcp55(void)
2415546d6f5SEd Swierk {
2425546d6f5SEd Swierk 	int bus;
2435546d6f5SEd Swierk 	int mcp55_mmconf_found = 0;
2445546d6f5SEd Swierk 
2455546d6f5SEd Swierk 	static const u32 extcfg_regnum		= 0x90;
2465546d6f5SEd Swierk 	static const u32 extcfg_regsize		= 4;
2475546d6f5SEd Swierk 	static const u32 extcfg_enable_mask	= 1<<31;
2485546d6f5SEd Swierk 	static const u32 extcfg_start_mask	= 0xff<<16;
2495546d6f5SEd Swierk 	static const int extcfg_start_shift	= 16;
2505546d6f5SEd Swierk 	static const u32 extcfg_size_mask	= 0x3<<28;
2515546d6f5SEd Swierk 	static const int extcfg_size_shift	= 28;
2525546d6f5SEd Swierk 	static const int extcfg_sizebus[]	= {0x100, 0x80, 0x40, 0x20};
2535546d6f5SEd Swierk 	static const u32 extcfg_base_mask[]	= {0x7ff8, 0x7ffc, 0x7ffe, 0x7fff};
2545546d6f5SEd Swierk 	static const int extcfg_base_lshift	= 25;
2555546d6f5SEd Swierk 
2565546d6f5SEd Swierk 	/*
2575546d6f5SEd Swierk 	 * do check if amd fam10h already took over
2585546d6f5SEd Swierk 	 */
259ff097dddSBjorn Helgaas 	if (!acpi_disabled || !list_empty(&pci_mmcfg_list) || mcp55_checked)
2605546d6f5SEd Swierk 		return NULL;
2615546d6f5SEd Swierk 
2625546d6f5SEd Swierk 	mcp55_checked = true;
2635546d6f5SEd Swierk 	for (bus = 0; bus < 256; bus++) {
2645546d6f5SEd Swierk 		u64 base;
2655546d6f5SEd Swierk 		u32 l, extcfg;
2665546d6f5SEd Swierk 		u16 vendor, device;
2675546d6f5SEd Swierk 		int start, size_index, end;
2685546d6f5SEd Swierk 
2695546d6f5SEd Swierk 		raw_pci_ops->read(0, bus, PCI_DEVFN(0, 0), 0, 4, &l);
2705546d6f5SEd Swierk 		vendor = l & 0xffff;
2715546d6f5SEd Swierk 		device = (l >> 16) & 0xffff;
2725546d6f5SEd Swierk 
2735546d6f5SEd Swierk 		if (PCI_VENDOR_ID_NVIDIA != vendor || 0x0369 != device)
2745546d6f5SEd Swierk 			continue;
2755546d6f5SEd Swierk 
2765546d6f5SEd Swierk 		raw_pci_ops->read(0, bus, PCI_DEVFN(0, 0), extcfg_regnum,
2775546d6f5SEd Swierk 				  extcfg_regsize, &extcfg);
2785546d6f5SEd Swierk 
2795546d6f5SEd Swierk 		if (!(extcfg & extcfg_enable_mask))
2805546d6f5SEd Swierk 			continue;
2815546d6f5SEd Swierk 
2825546d6f5SEd Swierk 		size_index = (extcfg & extcfg_size_mask) >> extcfg_size_shift;
2835546d6f5SEd Swierk 		base = extcfg & extcfg_base_mask[size_index];
2845546d6f5SEd Swierk 		/* base could > 4G */
2855546d6f5SEd Swierk 		base <<= extcfg_base_lshift;
2865546d6f5SEd Swierk 		start = (extcfg & extcfg_start_mask) >> extcfg_start_shift;
2875546d6f5SEd Swierk 		end = start + extcfg_sizebus[size_index] - 1;
2887da7d360SBjorn Helgaas 		if (pci_mmconfig_add(0, start, end, base) == NULL)
2897da7d360SBjorn Helgaas 			continue;
2905546d6f5SEd Swierk 		mcp55_mmconf_found++;
2915546d6f5SEd Swierk 	}
2925546d6f5SEd Swierk 
2935546d6f5SEd Swierk 	if (!mcp55_mmconf_found)
2945546d6f5SEd Swierk 		return NULL;
2955546d6f5SEd Swierk 
2965546d6f5SEd Swierk 	return "nVidia MCP55";
2975546d6f5SEd Swierk }
2985546d6f5SEd Swierk 
299fb9aa6f1SThomas Gleixner struct pci_mmcfg_hostbridge_probe {
3007fd0da40SYinghai Lu 	u32 bus;
3017fd0da40SYinghai Lu 	u32 devfn;
302fb9aa6f1SThomas Gleixner 	u32 vendor;
303fb9aa6f1SThomas Gleixner 	u32 device;
304fb9aa6f1SThomas Gleixner 	const char *(*probe)(void);
305fb9aa6f1SThomas Gleixner };
306fb9aa6f1SThomas Gleixner 
307fb9aa6f1SThomas Gleixner static struct pci_mmcfg_hostbridge_probe pci_mmcfg_probes[] __initdata = {
3087fd0da40SYinghai Lu 	{ 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
3097fd0da40SYinghai Lu 	  PCI_DEVICE_ID_INTEL_E7520_MCH, pci_mmcfg_e7520 },
3107fd0da40SYinghai Lu 	{ 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
3117fd0da40SYinghai Lu 	  PCI_DEVICE_ID_INTEL_82945G_HB, pci_mmcfg_intel_945 },
3127fd0da40SYinghai Lu 	{ 0, PCI_DEVFN(0x18, 0), PCI_VENDOR_ID_AMD,
3137fd0da40SYinghai Lu 	  0x1200, pci_mmcfg_amd_fam10h },
3147fd0da40SYinghai Lu 	{ 0xff, PCI_DEVFN(0, 0), PCI_VENDOR_ID_AMD,
3157fd0da40SYinghai Lu 	  0x1200, pci_mmcfg_amd_fam10h },
3165546d6f5SEd Swierk 	{ 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_NVIDIA,
3175546d6f5SEd Swierk 	  0x0369, pci_mmcfg_nvidia_mcp55 },
318fb9aa6f1SThomas Gleixner };
319fb9aa6f1SThomas Gleixner 
320068258bcSYinghai Lu static void __init pci_mmcfg_check_end_bus_number(void)
321068258bcSYinghai Lu {
322987c367bSBjorn Helgaas 	struct pci_mmcfg_region *cfg, *cfgx;
323068258bcSYinghai Lu 
324bb8d4133SThomas Gleixner 	/* Fixup overlaps */
325ff097dddSBjorn Helgaas 	list_for_each_entry(cfg, &pci_mmcfg_list, list) {
326d7e6b66fSBjorn Helgaas 		if (cfg->end_bus < cfg->start_bus)
327d7e6b66fSBjorn Helgaas 			cfg->end_bus = 255;
328068258bcSYinghai Lu 
329bb8d4133SThomas Gleixner 		/* Don't access the list head ! */
330bb8d4133SThomas Gleixner 		if (cfg->list.next == &pci_mmcfg_list)
331bb8d4133SThomas Gleixner 			break;
332bb8d4133SThomas Gleixner 
333ff097dddSBjorn Helgaas 		cfgx = list_entry(cfg->list.next, typeof(*cfg), list);
334bb8d4133SThomas Gleixner 		if (cfg->end_bus >= cfgx->start_bus)
335d7e6b66fSBjorn Helgaas 			cfg->end_bus = cfgx->start_bus - 1;
336068258bcSYinghai Lu 	}
337068258bcSYinghai Lu }
338068258bcSYinghai Lu 
339fb9aa6f1SThomas Gleixner static int __init pci_mmcfg_check_hostbridge(void)
340fb9aa6f1SThomas Gleixner {
341fb9aa6f1SThomas Gleixner 	u32 l;
3427fd0da40SYinghai Lu 	u32 bus, devfn;
343fb9aa6f1SThomas Gleixner 	u16 vendor, device;
344fb9aa6f1SThomas Gleixner 	int i;
345fb9aa6f1SThomas Gleixner 	const char *name;
346fb9aa6f1SThomas Gleixner 
347bb63b421SYinghai Lu 	if (!raw_pci_ops)
348bb63b421SYinghai Lu 		return 0;
349bb63b421SYinghai Lu 
3507da7d360SBjorn Helgaas 	free_all_mmcfg();
351fb9aa6f1SThomas Gleixner 
352068258bcSYinghai Lu 	for (i = 0; i < ARRAY_SIZE(pci_mmcfg_probes); i++) {
3537fd0da40SYinghai Lu 		bus =  pci_mmcfg_probes[i].bus;
3547fd0da40SYinghai Lu 		devfn = pci_mmcfg_probes[i].devfn;
355bb63b421SYinghai Lu 		raw_pci_ops->read(0, bus, devfn, 0, 4, &l);
3567fd0da40SYinghai Lu 		vendor = l & 0xffff;
3577fd0da40SYinghai Lu 		device = (l >> 16) & 0xffff;
3587fd0da40SYinghai Lu 
359068258bcSYinghai Lu 		name = NULL;
360fb9aa6f1SThomas Gleixner 		if (pci_mmcfg_probes[i].vendor == vendor &&
361fb9aa6f1SThomas Gleixner 		    pci_mmcfg_probes[i].device == device)
362fb9aa6f1SThomas Gleixner 			name = pci_mmcfg_probes[i].probe();
363068258bcSYinghai Lu 
364068258bcSYinghai Lu 		if (name)
3658c57786aSBjorn Helgaas 			printk(KERN_INFO PREFIX "%s with MMCONFIG support\n",
366068258bcSYinghai Lu 			       name);
367fb9aa6f1SThomas Gleixner 	}
368fb9aa6f1SThomas Gleixner 
369068258bcSYinghai Lu 	/* some end_bus_number is crazy, fix it */
370068258bcSYinghai Lu 	pci_mmcfg_check_end_bus_number();
371fb9aa6f1SThomas Gleixner 
372ff097dddSBjorn Helgaas 	return !list_empty(&pci_mmcfg_list);
373fb9aa6f1SThomas Gleixner }
374fb9aa6f1SThomas Gleixner 
37595c5e92fSJiang Liu static acpi_status __devinit check_mcfg_resource(struct acpi_resource *res,
3767752d5cfSRobert Hancock 						 void *data)
3777752d5cfSRobert Hancock {
3787752d5cfSRobert Hancock 	struct resource *mcfg_res = data;
3797752d5cfSRobert Hancock 	struct acpi_resource_address64 address;
3807752d5cfSRobert Hancock 	acpi_status status;
3817752d5cfSRobert Hancock 
3827752d5cfSRobert Hancock 	if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
3837752d5cfSRobert Hancock 		struct acpi_resource_fixed_memory32 *fixmem32 =
3847752d5cfSRobert Hancock 			&res->data.fixed_memory32;
3857752d5cfSRobert Hancock 		if (!fixmem32)
3867752d5cfSRobert Hancock 			return AE_OK;
3877752d5cfSRobert Hancock 		if ((mcfg_res->start >= fixmem32->address) &&
38875e613cdSYinghai Lu 		    (mcfg_res->end < (fixmem32->address +
3897752d5cfSRobert Hancock 				      fixmem32->address_length))) {
3907752d5cfSRobert Hancock 			mcfg_res->flags = 1;
3917752d5cfSRobert Hancock 			return AE_CTRL_TERMINATE;
3927752d5cfSRobert Hancock 		}
3937752d5cfSRobert Hancock 	}
3947752d5cfSRobert Hancock 	if ((res->type != ACPI_RESOURCE_TYPE_ADDRESS32) &&
3957752d5cfSRobert Hancock 	    (res->type != ACPI_RESOURCE_TYPE_ADDRESS64))
3967752d5cfSRobert Hancock 		return AE_OK;
3977752d5cfSRobert Hancock 
3987752d5cfSRobert Hancock 	status = acpi_resource_to_address64(res, &address);
3997752d5cfSRobert Hancock 	if (ACPI_FAILURE(status) ||
4007752d5cfSRobert Hancock 	   (address.address_length <= 0) ||
4017752d5cfSRobert Hancock 	   (address.resource_type != ACPI_MEMORY_RANGE))
4027752d5cfSRobert Hancock 		return AE_OK;
4037752d5cfSRobert Hancock 
4047752d5cfSRobert Hancock 	if ((mcfg_res->start >= address.minimum) &&
40575e613cdSYinghai Lu 	    (mcfg_res->end < (address.minimum + address.address_length))) {
4067752d5cfSRobert Hancock 		mcfg_res->flags = 1;
4077752d5cfSRobert Hancock 		return AE_CTRL_TERMINATE;
4087752d5cfSRobert Hancock 	}
4097752d5cfSRobert Hancock 	return AE_OK;
4107752d5cfSRobert Hancock }
4117752d5cfSRobert Hancock 
41295c5e92fSJiang Liu static acpi_status __devinit find_mboard_resource(acpi_handle handle, u32 lvl,
4137752d5cfSRobert Hancock 						  void *context, void **rv)
4147752d5cfSRobert Hancock {
4157752d5cfSRobert Hancock 	struct resource *mcfg_res = context;
4167752d5cfSRobert Hancock 
4177752d5cfSRobert Hancock 	acpi_walk_resources(handle, METHOD_NAME__CRS,
4187752d5cfSRobert Hancock 			    check_mcfg_resource, context);
4197752d5cfSRobert Hancock 
4207752d5cfSRobert Hancock 	if (mcfg_res->flags)
4217752d5cfSRobert Hancock 		return AE_CTRL_TERMINATE;
4227752d5cfSRobert Hancock 
4237752d5cfSRobert Hancock 	return AE_OK;
4247752d5cfSRobert Hancock }
4257752d5cfSRobert Hancock 
42695c5e92fSJiang Liu static int __devinit is_acpi_reserved(u64 start, u64 end, unsigned not_used)
4277752d5cfSRobert Hancock {
4287752d5cfSRobert Hancock 	struct resource mcfg_res;
4297752d5cfSRobert Hancock 
4307752d5cfSRobert Hancock 	mcfg_res.start = start;
43175e613cdSYinghai Lu 	mcfg_res.end = end - 1;
4327752d5cfSRobert Hancock 	mcfg_res.flags = 0;
4337752d5cfSRobert Hancock 
4347752d5cfSRobert Hancock 	acpi_get_devices("PNP0C01", find_mboard_resource, &mcfg_res, NULL);
4357752d5cfSRobert Hancock 
4367752d5cfSRobert Hancock 	if (!mcfg_res.flags)
4377752d5cfSRobert Hancock 		acpi_get_devices("PNP0C02", find_mboard_resource, &mcfg_res,
4387752d5cfSRobert Hancock 				 NULL);
4397752d5cfSRobert Hancock 
4407752d5cfSRobert Hancock 	return mcfg_res.flags;
4417752d5cfSRobert Hancock }
4427752d5cfSRobert Hancock 
443a83fe32fSYinghai Lu typedef int (*check_reserved_t)(u64 start, u64 end, unsigned type);
444a83fe32fSYinghai Lu 
44595c5e92fSJiang Liu static int __ref is_mmconf_reserved(check_reserved_t is_reserved,
44695c5e92fSJiang Liu 				    struct pci_mmcfg_region *cfg,
44795c5e92fSJiang Liu 				    struct device *dev, int with_e820)
448a83fe32fSYinghai Lu {
4492f2a8b9cSBjorn Helgaas 	u64 addr = cfg->res.start;
4502f2a8b9cSBjorn Helgaas 	u64 size = resource_size(&cfg->res);
451a83fe32fSYinghai Lu 	u64 old_size = size;
45295c5e92fSJiang Liu 	int num_buses;
45395c5e92fSJiang Liu 	char *method = with_e820 ? "E820" : "ACPI motherboard resources";
454a83fe32fSYinghai Lu 
455044cd809SYinghai Lu 	while (!is_reserved(addr, addr + size, E820_RESERVED)) {
456a83fe32fSYinghai Lu 		size >>= 1;
457a83fe32fSYinghai Lu 		if (size < (16UL<<20))
458a83fe32fSYinghai Lu 			break;
459a83fe32fSYinghai Lu 	}
460a83fe32fSYinghai Lu 
46195c5e92fSJiang Liu 	if (size < (16UL<<20) && size != old_size)
46295c5e92fSJiang Liu 		return 0;
46395c5e92fSJiang Liu 
46495c5e92fSJiang Liu 	if (dev)
46595c5e92fSJiang Liu 		dev_info(dev, "MMCONFIG at %pR reserved in %s\n",
46695c5e92fSJiang Liu 			 &cfg->res, method);
46795c5e92fSJiang Liu 	else
46895c5e92fSJiang Liu 		printk(KERN_INFO PREFIX
46995c5e92fSJiang Liu 		       "MMCONFIG at %pR reserved in %s\n",
47095c5e92fSJiang Liu 		       &cfg->res, method);
471a83fe32fSYinghai Lu 
472a83fe32fSYinghai Lu 	if (old_size != size) {
473d7e6b66fSBjorn Helgaas 		/* update end_bus */
474d7e6b66fSBjorn Helgaas 		cfg->end_bus = cfg->start_bus + ((size>>20) - 1);
47556ddf4d3SBjorn Helgaas 		num_buses = cfg->end_bus - cfg->start_bus + 1;
47656ddf4d3SBjorn Helgaas 		cfg->res.end = cfg->res.start +
47756ddf4d3SBjorn Helgaas 		    PCI_MMCFG_BUS_OFFSET(num_buses) - 1;
47856ddf4d3SBjorn Helgaas 		snprintf(cfg->name, PCI_MMCFG_RESOURCE_NAME_LEN,
47956ddf4d3SBjorn Helgaas 			 "PCI MMCONFIG %04x [bus %02x-%02x]",
48056ddf4d3SBjorn Helgaas 			 cfg->segment, cfg->start_bus, cfg->end_bus);
48195c5e92fSJiang Liu 
48295c5e92fSJiang Liu 		if (dev)
48395c5e92fSJiang Liu 			dev_info(dev,
48495c5e92fSJiang Liu 				"MMCONFIG "
48595c5e92fSJiang Liu 				"at %pR (base %#lx) (size reduced!)\n",
48695c5e92fSJiang Liu 				&cfg->res, (unsigned long) cfg->address);
48795c5e92fSJiang Liu 		else
4888c57786aSBjorn Helgaas 			printk(KERN_INFO PREFIX
4898c57786aSBjorn Helgaas 				"MMCONFIG for %04x [bus%02x-%02x] "
4908c57786aSBjorn Helgaas 				"at %pR (base %#lx) (size reduced!)\n",
4918c57786aSBjorn Helgaas 				cfg->segment, cfg->start_bus, cfg->end_bus,
4928c57786aSBjorn Helgaas 				&cfg->res, (unsigned long) cfg->address);
493a83fe32fSYinghai Lu 	}
49495c5e92fSJiang Liu 
49595c5e92fSJiang Liu 	return 1;
496a83fe32fSYinghai Lu }
497a83fe32fSYinghai Lu 
49895c5e92fSJiang Liu static int __ref pci_mmcfg_check_reserved(struct device *dev,
49995c5e92fSJiang Liu 		  struct pci_mmcfg_region *cfg, int early)
500fb9aa6f1SThomas Gleixner {
501a02ce953SFeng Tang 	if (!early && !acpi_disabled) {
50295c5e92fSJiang Liu 		if (is_mmconf_reserved(is_acpi_reserved, cfg, dev, 0))
5032a76c450SJiang Liu 			return 1;
50495c5e92fSJiang Liu 
50595c5e92fSJiang Liu 		if (dev)
50695c5e92fSJiang Liu 			dev_info(dev, FW_INFO
50795c5e92fSJiang Liu 				 "MMCONFIG at %pR not reserved in "
50895c5e92fSJiang Liu 				 "ACPI motherboard resources\n",
50995c5e92fSJiang Liu 				 &cfg->res);
510a02ce953SFeng Tang 		else
51195c5e92fSJiang Liu 			printk(KERN_INFO FW_INFO PREFIX
5128c57786aSBjorn Helgaas 			       "MMCONFIG at %pR not reserved in "
513a02ce953SFeng Tang 			       "ACPI motherboard resources\n",
514a02ce953SFeng Tang 			       &cfg->res);
515a02ce953SFeng Tang 	}
516a83fe32fSYinghai Lu 
51795c5e92fSJiang Liu 	/*
51895c5e92fSJiang Liu 	 * e820_all_mapped() is marked as __init.
51995c5e92fSJiang Liu 	 * All entries from ACPI MCFG table have been checked at boot time.
52095c5e92fSJiang Liu 	 * For MCFG information constructed from hotpluggable host bridge's
52195c5e92fSJiang Liu 	 * _CBA method, just assume it's reserved.
52295c5e92fSJiang Liu 	 */
52395c5e92fSJiang Liu 	if (pci_mmcfg_running_state)
52495c5e92fSJiang Liu 		return 1;
52595c5e92fSJiang Liu 
5267752d5cfSRobert Hancock 	/* Don't try to do this check unless configuration
527bb63b421SYinghai Lu 	   type 1 is available. how about type 2 ?*/
528a83fe32fSYinghai Lu 	if (raw_pci_ops)
52995c5e92fSJiang Liu 		return is_mmconf_reserved(e820_all_mapped, cfg, dev, 1);
53005c58b8aSYinghai Lu 
5312a76c450SJiang Liu 	return 0;
5327752d5cfSRobert Hancock }
5337752d5cfSRobert Hancock 
5342a76c450SJiang Liu static void __init pci_mmcfg_reject_broken(int early)
5352a76c450SJiang Liu {
5362a76c450SJiang Liu 	struct pci_mmcfg_region *cfg;
537fb9aa6f1SThomas Gleixner 
5382a76c450SJiang Liu 	list_for_each_entry(cfg, &pci_mmcfg_list, list) {
53995c5e92fSJiang Liu 		if (pci_mmcfg_check_reserved(NULL, cfg, early) == 0) {
5408c57786aSBjorn Helgaas 			printk(KERN_INFO PREFIX "not using MMCONFIG\n");
5417da7d360SBjorn Helgaas 			free_all_mmcfg();
5422a76c450SJiang Liu 			return;
5432a76c450SJiang Liu 		}
5442a76c450SJiang Liu 	}
545fb9aa6f1SThomas Gleixner }
546fb9aa6f1SThomas Gleixner 
5479a08f7d3SBjorn Helgaas static int __init acpi_mcfg_check_entry(struct acpi_table_mcfg *mcfg,
5489a08f7d3SBjorn Helgaas 					struct acpi_mcfg_allocation *cfg)
549c4bf2f37SLen Brown {
5509a08f7d3SBjorn Helgaas 	int year;
551c4bf2f37SLen Brown 
5529a08f7d3SBjorn Helgaas 	if (cfg->address < 0xFFFFFFFF)
553c4bf2f37SLen Brown 		return 0;
5549a08f7d3SBjorn Helgaas 
55568856859SJack Steiner 	if (!strcmp(mcfg->header.oem_id, "SGI") ||
55668856859SJack Steiner 			!strcmp(mcfg->header.oem_id, "SGI2"))
5579a08f7d3SBjorn Helgaas 		return 0;
5589a08f7d3SBjorn Helgaas 
5599a08f7d3SBjorn Helgaas 	if (mcfg->header.revision >= 1) {
5609a08f7d3SBjorn Helgaas 		if (dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL) &&
5619a08f7d3SBjorn Helgaas 		    year >= 2010)
5629a08f7d3SBjorn Helgaas 			return 0;
5639a08f7d3SBjorn Helgaas 	}
5649a08f7d3SBjorn Helgaas 
5658c57786aSBjorn Helgaas 	printk(KERN_ERR PREFIX "MCFG region for %04x [bus %02x-%02x] at %#llx "
5669a08f7d3SBjorn Helgaas 	       "is above 4GB, ignored\n", cfg->pci_segment,
5679a08f7d3SBjorn Helgaas 	       cfg->start_bus_number, cfg->end_bus_number, cfg->address);
5689a08f7d3SBjorn Helgaas 	return -EINVAL;
569c4bf2f37SLen Brown }
570c4bf2f37SLen Brown 
571c4bf2f37SLen Brown static int __init pci_parse_mcfg(struct acpi_table_header *header)
572c4bf2f37SLen Brown {
573c4bf2f37SLen Brown 	struct acpi_table_mcfg *mcfg;
574d3578ef7SBjorn Helgaas 	struct acpi_mcfg_allocation *cfg_table, *cfg;
575c4bf2f37SLen Brown 	unsigned long i;
5767da7d360SBjorn Helgaas 	int entries;
577c4bf2f37SLen Brown 
578c4bf2f37SLen Brown 	if (!header)
579c4bf2f37SLen Brown 		return -EINVAL;
580c4bf2f37SLen Brown 
581c4bf2f37SLen Brown 	mcfg = (struct acpi_table_mcfg *)header;
582c4bf2f37SLen Brown 
583c4bf2f37SLen Brown 	/* how many config structures do we have */
5847da7d360SBjorn Helgaas 	free_all_mmcfg();
585e823d6ffSBjorn Helgaas 	entries = 0;
586c4bf2f37SLen Brown 	i = header->length - sizeof(struct acpi_table_mcfg);
587c4bf2f37SLen Brown 	while (i >= sizeof(struct acpi_mcfg_allocation)) {
588e823d6ffSBjorn Helgaas 		entries++;
589c4bf2f37SLen Brown 		i -= sizeof(struct acpi_mcfg_allocation);
590c4bf2f37SLen Brown 	};
591e823d6ffSBjorn Helgaas 	if (entries == 0) {
592c4bf2f37SLen Brown 		printk(KERN_ERR PREFIX "MMCONFIG has no entries\n");
593c4bf2f37SLen Brown 		return -ENODEV;
594c4bf2f37SLen Brown 	}
595c4bf2f37SLen Brown 
596d3578ef7SBjorn Helgaas 	cfg_table = (struct acpi_mcfg_allocation *) &mcfg[1];
597e823d6ffSBjorn Helgaas 	for (i = 0; i < entries; i++) {
598d3578ef7SBjorn Helgaas 		cfg = &cfg_table[i];
599d3578ef7SBjorn Helgaas 		if (acpi_mcfg_check_entry(mcfg, cfg)) {
6007da7d360SBjorn Helgaas 			free_all_mmcfg();
601c4bf2f37SLen Brown 			return -ENODEV;
602c4bf2f37SLen Brown 		}
6037da7d360SBjorn Helgaas 
6047da7d360SBjorn Helgaas 		if (pci_mmconfig_add(cfg->pci_segment, cfg->start_bus_number,
6057da7d360SBjorn Helgaas 				   cfg->end_bus_number, cfg->address) == NULL) {
6067da7d360SBjorn Helgaas 			printk(KERN_WARNING PREFIX
6077da7d360SBjorn Helgaas 			       "no memory for MCFG entries\n");
6087da7d360SBjorn Helgaas 			free_all_mmcfg();
6097da7d360SBjorn Helgaas 			return -ENOMEM;
6107da7d360SBjorn Helgaas 		}
611c4bf2f37SLen Brown 	}
612c4bf2f37SLen Brown 
613c4bf2f37SLen Brown 	return 0;
614c4bf2f37SLen Brown }
615c4bf2f37SLen Brown 
616968cbfadSThomas Gleixner static void __init __pci_mmcfg_init(int early)
617fb9aa6f1SThomas Gleixner {
618bb63b421SYinghai Lu 	pci_mmcfg_reject_broken(early);
619ff097dddSBjorn Helgaas 	if (list_empty(&pci_mmcfg_list))
620fb9aa6f1SThomas Gleixner 		return;
621fb9aa6f1SThomas Gleixner 
622a3170c1fSJan Beulich 	if (pcibios_last_bus < 0) {
623a3170c1fSJan Beulich 		const struct pci_mmcfg_region *cfg;
624a3170c1fSJan Beulich 
625a3170c1fSJan Beulich 		list_for_each_entry(cfg, &pci_mmcfg_list, list) {
626a3170c1fSJan Beulich 			if (cfg->segment)
627a3170c1fSJan Beulich 				break;
628a3170c1fSJan Beulich 			pcibios_last_bus = cfg->end_bus;
629a3170c1fSJan Beulich 		}
630a3170c1fSJan Beulich 	}
631a3170c1fSJan Beulich 
632ebd60cd6SYinghai Lu 	if (pci_mmcfg_arch_init())
633fb9aa6f1SThomas Gleixner 		pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
634ebd60cd6SYinghai Lu 	else {
63566e8850aSJiang Liu 		free_all_mmcfg();
6369c95111bSJiang Liu 		pci_mmcfg_arch_init_failed = true;
637fb9aa6f1SThomas Gleixner 	}
638fb9aa6f1SThomas Gleixner }
639fb9aa6f1SThomas Gleixner 
640*574a5941SJiang Liu static int __initdata known_bridge;
641*574a5941SJiang Liu 
642bb63b421SYinghai Lu void __init pci_mmcfg_early_init(void)
64305c58b8aSYinghai Lu {
644*574a5941SJiang Liu 	if (pci_probe & PCI_PROBE_MMCONF) {
645*574a5941SJiang Liu 		if (pci_mmcfg_check_hostbridge())
646*574a5941SJiang Liu 			known_bridge = 1;
647*574a5941SJiang Liu 		else
648*574a5941SJiang Liu 			acpi_sfi_table_parse(ACPI_SIG_MCFG, pci_parse_mcfg);
649bb63b421SYinghai Lu 		__pci_mmcfg_init(1);
65005c58b8aSYinghai Lu 	}
651*574a5941SJiang Liu }
65205c58b8aSYinghai Lu 
65305c58b8aSYinghai Lu void __init pci_mmcfg_late_init(void)
65405c58b8aSYinghai Lu {
655*574a5941SJiang Liu 	/* MMCONFIG disabled */
656*574a5941SJiang Liu 	if ((pci_probe & PCI_PROBE_MMCONF) == 0)
657*574a5941SJiang Liu 		return;
658*574a5941SJiang Liu 
659*574a5941SJiang Liu 	if (known_bridge)
660*574a5941SJiang Liu 		return;
661*574a5941SJiang Liu 
662*574a5941SJiang Liu 	/* MMCONFIG hasn't been enabled yet, try again */
663*574a5941SJiang Liu 	if (pci_probe & PCI_PROBE_MASK & ~PCI_PROBE_MMCONF) {
664*574a5941SJiang Liu 		acpi_sfi_table_parse(ACPI_SIG_MCFG, pci_parse_mcfg);
665bb63b421SYinghai Lu 		__pci_mmcfg_init(0);
66605c58b8aSYinghai Lu 	}
667*574a5941SJiang Liu }
66805c58b8aSYinghai Lu 
669fb9aa6f1SThomas Gleixner static int __init pci_mmcfg_late_insert_resources(void)
670fb9aa6f1SThomas Gleixner {
67166e8850aSJiang Liu 	struct pci_mmcfg_region *cfg;
67266e8850aSJiang Liu 
67395c5e92fSJiang Liu 	pci_mmcfg_running_state = true;
67495c5e92fSJiang Liu 
67566e8850aSJiang Liu 	/* If we are not using MMCONFIG, don't insert the resources. */
67666e8850aSJiang Liu 	if ((pci_probe & PCI_PROBE_MMCONF) == 0)
677fb9aa6f1SThomas Gleixner 		return 1;
678fb9aa6f1SThomas Gleixner 
679fb9aa6f1SThomas Gleixner 	/*
680fb9aa6f1SThomas Gleixner 	 * Attempt to insert the mmcfg resources but not with the busy flag
681fb9aa6f1SThomas Gleixner 	 * marked so it won't cause request errors when __request_region is
682fb9aa6f1SThomas Gleixner 	 * called.
683fb9aa6f1SThomas Gleixner 	 */
68466e8850aSJiang Liu 	list_for_each_entry(cfg, &pci_mmcfg_list, list)
68566e8850aSJiang Liu 		if (!cfg->res.parent)
68666e8850aSJiang Liu 			insert_resource(&iomem_resource, &cfg->res);
687fb9aa6f1SThomas Gleixner 
688fb9aa6f1SThomas Gleixner 	return 0;
689fb9aa6f1SThomas Gleixner }
690fb9aa6f1SThomas Gleixner 
691fb9aa6f1SThomas Gleixner /*
692fb9aa6f1SThomas Gleixner  * Perform MMCONFIG resource insertion after PCI initialization to allow for
693fb9aa6f1SThomas Gleixner  * misprogrammed MCFG tables that state larger sizes but actually conflict
694fb9aa6f1SThomas Gleixner  * with other system resources.
695fb9aa6f1SThomas Gleixner  */
696fb9aa6f1SThomas Gleixner late_initcall(pci_mmcfg_late_insert_resources);
6979c95111bSJiang Liu 
6989c95111bSJiang Liu /* Add MMCFG information for host bridges */
6999c95111bSJiang Liu int __devinit pci_mmconfig_insert(struct device *dev,
7009c95111bSJiang Liu 				  u16 seg, u8 start, u8 end,
7019c95111bSJiang Liu 				  phys_addr_t addr)
7029c95111bSJiang Liu {
7039c95111bSJiang Liu 	int rc;
7049c95111bSJiang Liu 	struct resource *tmp = NULL;
7059c95111bSJiang Liu 	struct pci_mmcfg_region *cfg;
7069c95111bSJiang Liu 
7079c95111bSJiang Liu 	if (!(pci_probe & PCI_PROBE_MMCONF) || pci_mmcfg_arch_init_failed)
7089c95111bSJiang Liu 		return -ENODEV;
7099c95111bSJiang Liu 
7109c95111bSJiang Liu 	if (start > end)
7119c95111bSJiang Liu 		return -EINVAL;
7129c95111bSJiang Liu 
7139c95111bSJiang Liu 	mutex_lock(&pci_mmcfg_lock);
7149c95111bSJiang Liu 	cfg = pci_mmconfig_lookup(seg, start);
7159c95111bSJiang Liu 	if (cfg) {
7169c95111bSJiang Liu 		if (cfg->end_bus < end)
7179c95111bSJiang Liu 			dev_info(dev, FW_INFO
7189c95111bSJiang Liu 				 "MMCONFIG for "
7199c95111bSJiang Liu 				 "domain %04x [bus %02x-%02x] "
7209c95111bSJiang Liu 				 "only partially covers this bridge\n",
7219c95111bSJiang Liu 				  cfg->segment, cfg->start_bus, cfg->end_bus);
7229c95111bSJiang Liu 		mutex_unlock(&pci_mmcfg_lock);
7239c95111bSJiang Liu 		return -EEXIST;
7249c95111bSJiang Liu 	}
7259c95111bSJiang Liu 
7269c95111bSJiang Liu 	if (!addr) {
7279c95111bSJiang Liu 		mutex_unlock(&pci_mmcfg_lock);
7289c95111bSJiang Liu 		return -EINVAL;
7299c95111bSJiang Liu 	}
7309c95111bSJiang Liu 
7319c95111bSJiang Liu 	rc = -EBUSY;
7329c95111bSJiang Liu 	cfg = pci_mmconfig_alloc(seg, start, end, addr);
7339c95111bSJiang Liu 	if (cfg == NULL) {
7349c95111bSJiang Liu 		dev_warn(dev, "fail to add MMCONFIG (out of memory)\n");
7359c95111bSJiang Liu 		rc = -ENOMEM;
7369c95111bSJiang Liu 	} else if (!pci_mmcfg_check_reserved(dev, cfg, 0)) {
7379c95111bSJiang Liu 		dev_warn(dev, FW_BUG "MMCONFIG %pR isn't reserved\n",
7389c95111bSJiang Liu 			 &cfg->res);
7399c95111bSJiang Liu 	} else {
7409c95111bSJiang Liu 		/* Insert resource if it's not in boot stage */
7419c95111bSJiang Liu 		if (pci_mmcfg_running_state)
7429c95111bSJiang Liu 			tmp = insert_resource_conflict(&iomem_resource,
7439c95111bSJiang Liu 						       &cfg->res);
7449c95111bSJiang Liu 
7459c95111bSJiang Liu 		if (tmp) {
7469c95111bSJiang Liu 			dev_warn(dev,
7479c95111bSJiang Liu 				 "MMCONFIG %pR conflicts with "
7489c95111bSJiang Liu 				 "%s %pR\n",
7499c95111bSJiang Liu 				 &cfg->res, tmp->name, tmp);
7509c95111bSJiang Liu 		} else if (pci_mmcfg_arch_map(cfg)) {
7519c95111bSJiang Liu 			dev_warn(dev, "fail to map MMCONFIG %pR.\n",
7529c95111bSJiang Liu 				 &cfg->res);
7539c95111bSJiang Liu 		} else {
7549c95111bSJiang Liu 			list_add_sorted(cfg);
7559c95111bSJiang Liu 			dev_info(dev, "MMCONFIG at %pR (base %#lx)\n",
7569c95111bSJiang Liu 				 &cfg->res, (unsigned long)addr);
7579c95111bSJiang Liu 			cfg = NULL;
7589c95111bSJiang Liu 			rc = 0;
7599c95111bSJiang Liu 		}
7609c95111bSJiang Liu 	}
7619c95111bSJiang Liu 
7629c95111bSJiang Liu 	if (cfg) {
7639c95111bSJiang Liu 		if (cfg->res.parent)
7649c95111bSJiang Liu 			release_resource(&cfg->res);
7659c95111bSJiang Liu 		kfree(cfg);
7669c95111bSJiang Liu 	}
7679c95111bSJiang Liu 
7689c95111bSJiang Liu 	mutex_unlock(&pci_mmcfg_lock);
7699c95111bSJiang Liu 
7709c95111bSJiang Liu 	return rc;
7719c95111bSJiang Liu }
7729c95111bSJiang Liu 
7739c95111bSJiang Liu /* Delete MMCFG information for host bridges */
7749c95111bSJiang Liu int pci_mmconfig_delete(u16 seg, u8 start, u8 end)
7759c95111bSJiang Liu {
7769c95111bSJiang Liu 	struct pci_mmcfg_region *cfg;
7779c95111bSJiang Liu 
7789c95111bSJiang Liu 	mutex_lock(&pci_mmcfg_lock);
7799c95111bSJiang Liu 	list_for_each_entry_rcu(cfg, &pci_mmcfg_list, list)
7809c95111bSJiang Liu 		if (cfg->segment == seg && cfg->start_bus == start &&
7819c95111bSJiang Liu 		    cfg->end_bus == end) {
7829c95111bSJiang Liu 			list_del_rcu(&cfg->list);
7839c95111bSJiang Liu 			synchronize_rcu();
7849c95111bSJiang Liu 			pci_mmcfg_arch_unmap(cfg);
7859c95111bSJiang Liu 			if (cfg->res.parent)
7869c95111bSJiang Liu 				release_resource(&cfg->res);
7879c95111bSJiang Liu 			mutex_unlock(&pci_mmcfg_lock);
7889c95111bSJiang Liu 			kfree(cfg);
7899c95111bSJiang Liu 			return 0;
7909c95111bSJiang Liu 		}
7919c95111bSJiang Liu 	mutex_unlock(&pci_mmcfg_lock);
7929c95111bSJiang Liu 
7939c95111bSJiang Liu 	return -ENOENT;
7949c95111bSJiang Liu }
795