xref: /OK3568_Linux_fs/kernel/arch/x86/pci/mmconfig-shared.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * mmconfig-shared.c - Low-level direct PCI config space access via
4*4882a593Smuzhiyun  *                     MMCONFIG - common code between i386 and x86-64.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * This code does:
7*4882a593Smuzhiyun  * - known chipset handling
8*4882a593Smuzhiyun  * - ACPI decoding and validation
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * Per-architecture code takes care of the mappings and accesses
11*4882a593Smuzhiyun  * themselves.
12*4882a593Smuzhiyun  */
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <linux/pci.h>
15*4882a593Smuzhiyun #include <linux/init.h>
16*4882a593Smuzhiyun #include <linux/sfi_acpi.h>
17*4882a593Smuzhiyun #include <linux/bitmap.h>
18*4882a593Smuzhiyun #include <linux/dmi.h>
19*4882a593Smuzhiyun #include <linux/slab.h>
20*4882a593Smuzhiyun #include <linux/mutex.h>
21*4882a593Smuzhiyun #include <linux/rculist.h>
22*4882a593Smuzhiyun #include <asm/e820/api.h>
23*4882a593Smuzhiyun #include <asm/pci_x86.h>
24*4882a593Smuzhiyun #include <asm/acpi.h>
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #define PREFIX "PCI: "
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /* Indicate if the mmcfg resources have been placed into the resource table. */
29*4882a593Smuzhiyun static bool pci_mmcfg_running_state;
30*4882a593Smuzhiyun static bool pci_mmcfg_arch_init_failed;
31*4882a593Smuzhiyun static DEFINE_MUTEX(pci_mmcfg_lock);
32*4882a593Smuzhiyun #define pci_mmcfg_lock_held() lock_is_held(&(pci_mmcfg_lock).dep_map)
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun LIST_HEAD(pci_mmcfg_list);
35*4882a593Smuzhiyun 
pci_mmconfig_remove(struct pci_mmcfg_region * cfg)36*4882a593Smuzhiyun static void __init pci_mmconfig_remove(struct pci_mmcfg_region *cfg)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun 	if (cfg->res.parent)
39*4882a593Smuzhiyun 		release_resource(&cfg->res);
40*4882a593Smuzhiyun 	list_del(&cfg->list);
41*4882a593Smuzhiyun 	kfree(cfg);
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun 
free_all_mmcfg(void)44*4882a593Smuzhiyun static void __init free_all_mmcfg(void)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun 	struct pci_mmcfg_region *cfg, *tmp;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	pci_mmcfg_arch_free();
49*4882a593Smuzhiyun 	list_for_each_entry_safe(cfg, tmp, &pci_mmcfg_list, list)
50*4882a593Smuzhiyun 		pci_mmconfig_remove(cfg);
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun 
list_add_sorted(struct pci_mmcfg_region * new)53*4882a593Smuzhiyun static void list_add_sorted(struct pci_mmcfg_region *new)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun 	struct pci_mmcfg_region *cfg;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	/* keep list sorted by segment and starting bus number */
58*4882a593Smuzhiyun 	list_for_each_entry_rcu(cfg, &pci_mmcfg_list, list, pci_mmcfg_lock_held()) {
59*4882a593Smuzhiyun 		if (cfg->segment > new->segment ||
60*4882a593Smuzhiyun 		    (cfg->segment == new->segment &&
61*4882a593Smuzhiyun 		     cfg->start_bus >= new->start_bus)) {
62*4882a593Smuzhiyun 			list_add_tail_rcu(&new->list, &cfg->list);
63*4882a593Smuzhiyun 			return;
64*4882a593Smuzhiyun 		}
65*4882a593Smuzhiyun 	}
66*4882a593Smuzhiyun 	list_add_tail_rcu(&new->list, &pci_mmcfg_list);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun 
pci_mmconfig_alloc(int segment,int start,int end,u64 addr)69*4882a593Smuzhiyun static struct pci_mmcfg_region *pci_mmconfig_alloc(int segment, int start,
70*4882a593Smuzhiyun 						   int end, u64 addr)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	struct pci_mmcfg_region *new;
73*4882a593Smuzhiyun 	struct resource *res;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	if (addr == 0)
76*4882a593Smuzhiyun 		return NULL;
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	new = kzalloc(sizeof(*new), GFP_KERNEL);
79*4882a593Smuzhiyun 	if (!new)
80*4882a593Smuzhiyun 		return NULL;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	new->address = addr;
83*4882a593Smuzhiyun 	new->segment = segment;
84*4882a593Smuzhiyun 	new->start_bus = start;
85*4882a593Smuzhiyun 	new->end_bus = end;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	res = &new->res;
88*4882a593Smuzhiyun 	res->start = addr + PCI_MMCFG_BUS_OFFSET(start);
89*4882a593Smuzhiyun 	res->end = addr + PCI_MMCFG_BUS_OFFSET(end + 1) - 1;
90*4882a593Smuzhiyun 	res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
91*4882a593Smuzhiyun 	snprintf(new->name, PCI_MMCFG_RESOURCE_NAME_LEN,
92*4882a593Smuzhiyun 		 "PCI MMCONFIG %04x [bus %02x-%02x]", segment, start, end);
93*4882a593Smuzhiyun 	res->name = new->name;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	return new;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun 
pci_mmconfig_add(int segment,int start,int end,u64 addr)98*4882a593Smuzhiyun struct pci_mmcfg_region *__init pci_mmconfig_add(int segment, int start,
99*4882a593Smuzhiyun 						 int end, u64 addr)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun 	struct pci_mmcfg_region *new;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	new = pci_mmconfig_alloc(segment, start, end, addr);
104*4882a593Smuzhiyun 	if (new) {
105*4882a593Smuzhiyun 		mutex_lock(&pci_mmcfg_lock);
106*4882a593Smuzhiyun 		list_add_sorted(new);
107*4882a593Smuzhiyun 		mutex_unlock(&pci_mmcfg_lock);
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 		pr_info(PREFIX
110*4882a593Smuzhiyun 		       "MMCONFIG for domain %04x [bus %02x-%02x] at %pR "
111*4882a593Smuzhiyun 		       "(base %#lx)\n",
112*4882a593Smuzhiyun 		       segment, start, end, &new->res, (unsigned long)addr);
113*4882a593Smuzhiyun 	}
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	return new;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun 
pci_mmconfig_lookup(int segment,int bus)118*4882a593Smuzhiyun struct pci_mmcfg_region *pci_mmconfig_lookup(int segment, int bus)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun 	struct pci_mmcfg_region *cfg;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	list_for_each_entry_rcu(cfg, &pci_mmcfg_list, list, pci_mmcfg_lock_held())
123*4882a593Smuzhiyun 		if (cfg->segment == segment &&
124*4882a593Smuzhiyun 		    cfg->start_bus <= bus && bus <= cfg->end_bus)
125*4882a593Smuzhiyun 			return cfg;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	return NULL;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun 
pci_mmcfg_e7520(void)130*4882a593Smuzhiyun static const char *__init pci_mmcfg_e7520(void)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun 	u32 win;
133*4882a593Smuzhiyun 	raw_pci_ops->read(0, 0, PCI_DEVFN(0, 0), 0xce, 2, &win);
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	win = win & 0xf000;
136*4882a593Smuzhiyun 	if (win == 0x0000 || win == 0xf000)
137*4882a593Smuzhiyun 		return NULL;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	if (pci_mmconfig_add(0, 0, 255, win << 16) == NULL)
140*4882a593Smuzhiyun 		return NULL;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	return "Intel Corporation E7520 Memory Controller Hub";
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun 
pci_mmcfg_intel_945(void)145*4882a593Smuzhiyun static const char *__init pci_mmcfg_intel_945(void)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun 	u32 pciexbar, mask = 0, len = 0;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	raw_pci_ops->read(0, 0, PCI_DEVFN(0, 0), 0x48, 4, &pciexbar);
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	/* Enable bit */
152*4882a593Smuzhiyun 	if (!(pciexbar & 1))
153*4882a593Smuzhiyun 		return NULL;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	/* Size bits */
156*4882a593Smuzhiyun 	switch ((pciexbar >> 1) & 3) {
157*4882a593Smuzhiyun 	case 0:
158*4882a593Smuzhiyun 		mask = 0xf0000000U;
159*4882a593Smuzhiyun 		len  = 0x10000000U;
160*4882a593Smuzhiyun 		break;
161*4882a593Smuzhiyun 	case 1:
162*4882a593Smuzhiyun 		mask = 0xf8000000U;
163*4882a593Smuzhiyun 		len  = 0x08000000U;
164*4882a593Smuzhiyun 		break;
165*4882a593Smuzhiyun 	case 2:
166*4882a593Smuzhiyun 		mask = 0xfc000000U;
167*4882a593Smuzhiyun 		len  = 0x04000000U;
168*4882a593Smuzhiyun 		break;
169*4882a593Smuzhiyun 	default:
170*4882a593Smuzhiyun 		return NULL;
171*4882a593Smuzhiyun 	}
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	/* Errata #2, things break when not aligned on a 256Mb boundary */
174*4882a593Smuzhiyun 	/* Can only happen in 64M/128M mode */
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	if ((pciexbar & mask) & 0x0fffffffU)
177*4882a593Smuzhiyun 		return NULL;
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	/* Don't hit the APIC registers and their friends */
180*4882a593Smuzhiyun 	if ((pciexbar & mask) >= 0xf0000000U)
181*4882a593Smuzhiyun 		return NULL;
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	if (pci_mmconfig_add(0, 0, (len >> 20) - 1, pciexbar & mask) == NULL)
184*4882a593Smuzhiyun 		return NULL;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	return "Intel Corporation 945G/GZ/P/PL Express Memory Controller Hub";
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun 
pci_mmcfg_amd_fam10h(void)189*4882a593Smuzhiyun static const char *__init pci_mmcfg_amd_fam10h(void)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun 	u32 low, high, address;
192*4882a593Smuzhiyun 	u64 base, msr;
193*4882a593Smuzhiyun 	int i;
194*4882a593Smuzhiyun 	unsigned segnbits = 0, busnbits, end_bus;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	if (!(pci_probe & PCI_CHECK_ENABLE_AMD_MMCONF))
197*4882a593Smuzhiyun 		return NULL;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	address = MSR_FAM10H_MMIO_CONF_BASE;
200*4882a593Smuzhiyun 	if (rdmsr_safe(address, &low, &high))
201*4882a593Smuzhiyun 		return NULL;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	msr = high;
204*4882a593Smuzhiyun 	msr <<= 32;
205*4882a593Smuzhiyun 	msr |= low;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	/* mmconfig is not enable */
208*4882a593Smuzhiyun 	if (!(msr & FAM10H_MMIO_CONF_ENABLE))
209*4882a593Smuzhiyun 		return NULL;
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	base = msr & (FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT);
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	busnbits = (msr >> FAM10H_MMIO_CONF_BUSRANGE_SHIFT) &
214*4882a593Smuzhiyun 			 FAM10H_MMIO_CONF_BUSRANGE_MASK;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	/*
217*4882a593Smuzhiyun 	 * only handle bus 0 ?
218*4882a593Smuzhiyun 	 * need to skip it
219*4882a593Smuzhiyun 	 */
220*4882a593Smuzhiyun 	if (!busnbits)
221*4882a593Smuzhiyun 		return NULL;
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	if (busnbits > 8) {
224*4882a593Smuzhiyun 		segnbits = busnbits - 8;
225*4882a593Smuzhiyun 		busnbits = 8;
226*4882a593Smuzhiyun 	}
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	end_bus = (1 << busnbits) - 1;
229*4882a593Smuzhiyun 	for (i = 0; i < (1 << segnbits); i++)
230*4882a593Smuzhiyun 		if (pci_mmconfig_add(i, 0, end_bus,
231*4882a593Smuzhiyun 				     base + (1<<28) * i) == NULL) {
232*4882a593Smuzhiyun 			free_all_mmcfg();
233*4882a593Smuzhiyun 			return NULL;
234*4882a593Smuzhiyun 		}
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	return "AMD Family 10h NB";
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun static bool __initdata mcp55_checked;
pci_mmcfg_nvidia_mcp55(void)240*4882a593Smuzhiyun static const char *__init pci_mmcfg_nvidia_mcp55(void)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun 	int bus;
243*4882a593Smuzhiyun 	int mcp55_mmconf_found = 0;
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	static const u32 extcfg_regnum __initconst	= 0x90;
246*4882a593Smuzhiyun 	static const u32 extcfg_regsize __initconst	= 4;
247*4882a593Smuzhiyun 	static const u32 extcfg_enable_mask __initconst	= 1 << 31;
248*4882a593Smuzhiyun 	static const u32 extcfg_start_mask __initconst	= 0xff << 16;
249*4882a593Smuzhiyun 	static const int extcfg_start_shift __initconst	= 16;
250*4882a593Smuzhiyun 	static const u32 extcfg_size_mask __initconst	= 0x3 << 28;
251*4882a593Smuzhiyun 	static const int extcfg_size_shift __initconst	= 28;
252*4882a593Smuzhiyun 	static const int extcfg_sizebus[] __initconst	= {
253*4882a593Smuzhiyun 		0x100, 0x80, 0x40, 0x20
254*4882a593Smuzhiyun 	};
255*4882a593Smuzhiyun 	static const u32 extcfg_base_mask[] __initconst	= {
256*4882a593Smuzhiyun 		0x7ff8, 0x7ffc, 0x7ffe, 0x7fff
257*4882a593Smuzhiyun 	};
258*4882a593Smuzhiyun 	static const int extcfg_base_lshift __initconst	= 25;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	/*
261*4882a593Smuzhiyun 	 * do check if amd fam10h already took over
262*4882a593Smuzhiyun 	 */
263*4882a593Smuzhiyun 	if (!acpi_disabled || !list_empty(&pci_mmcfg_list) || mcp55_checked)
264*4882a593Smuzhiyun 		return NULL;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	mcp55_checked = true;
267*4882a593Smuzhiyun 	for (bus = 0; bus < 256; bus++) {
268*4882a593Smuzhiyun 		u64 base;
269*4882a593Smuzhiyun 		u32 l, extcfg;
270*4882a593Smuzhiyun 		u16 vendor, device;
271*4882a593Smuzhiyun 		int start, size_index, end;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 		raw_pci_ops->read(0, bus, PCI_DEVFN(0, 0), 0, 4, &l);
274*4882a593Smuzhiyun 		vendor = l & 0xffff;
275*4882a593Smuzhiyun 		device = (l >> 16) & 0xffff;
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 		if (PCI_VENDOR_ID_NVIDIA != vendor || 0x0369 != device)
278*4882a593Smuzhiyun 			continue;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 		raw_pci_ops->read(0, bus, PCI_DEVFN(0, 0), extcfg_regnum,
281*4882a593Smuzhiyun 				  extcfg_regsize, &extcfg);
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 		if (!(extcfg & extcfg_enable_mask))
284*4882a593Smuzhiyun 			continue;
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 		size_index = (extcfg & extcfg_size_mask) >> extcfg_size_shift;
287*4882a593Smuzhiyun 		base = extcfg & extcfg_base_mask[size_index];
288*4882a593Smuzhiyun 		/* base could > 4G */
289*4882a593Smuzhiyun 		base <<= extcfg_base_lshift;
290*4882a593Smuzhiyun 		start = (extcfg & extcfg_start_mask) >> extcfg_start_shift;
291*4882a593Smuzhiyun 		end = start + extcfg_sizebus[size_index] - 1;
292*4882a593Smuzhiyun 		if (pci_mmconfig_add(0, start, end, base) == NULL)
293*4882a593Smuzhiyun 			continue;
294*4882a593Smuzhiyun 		mcp55_mmconf_found++;
295*4882a593Smuzhiyun 	}
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	if (!mcp55_mmconf_found)
298*4882a593Smuzhiyun 		return NULL;
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	return "nVidia MCP55";
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun struct pci_mmcfg_hostbridge_probe {
304*4882a593Smuzhiyun 	u32 bus;
305*4882a593Smuzhiyun 	u32 devfn;
306*4882a593Smuzhiyun 	u32 vendor;
307*4882a593Smuzhiyun 	u32 device;
308*4882a593Smuzhiyun 	const char *(*probe)(void);
309*4882a593Smuzhiyun };
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun static const struct pci_mmcfg_hostbridge_probe pci_mmcfg_probes[] __initconst = {
312*4882a593Smuzhiyun 	{ 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
313*4882a593Smuzhiyun 	  PCI_DEVICE_ID_INTEL_E7520_MCH, pci_mmcfg_e7520 },
314*4882a593Smuzhiyun 	{ 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
315*4882a593Smuzhiyun 	  PCI_DEVICE_ID_INTEL_82945G_HB, pci_mmcfg_intel_945 },
316*4882a593Smuzhiyun 	{ 0, PCI_DEVFN(0x18, 0), PCI_VENDOR_ID_AMD,
317*4882a593Smuzhiyun 	  0x1200, pci_mmcfg_amd_fam10h },
318*4882a593Smuzhiyun 	{ 0xff, PCI_DEVFN(0, 0), PCI_VENDOR_ID_AMD,
319*4882a593Smuzhiyun 	  0x1200, pci_mmcfg_amd_fam10h },
320*4882a593Smuzhiyun 	{ 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_NVIDIA,
321*4882a593Smuzhiyun 	  0x0369, pci_mmcfg_nvidia_mcp55 },
322*4882a593Smuzhiyun };
323*4882a593Smuzhiyun 
pci_mmcfg_check_end_bus_number(void)324*4882a593Smuzhiyun static void __init pci_mmcfg_check_end_bus_number(void)
325*4882a593Smuzhiyun {
326*4882a593Smuzhiyun 	struct pci_mmcfg_region *cfg, *cfgx;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	/* Fixup overlaps */
329*4882a593Smuzhiyun 	list_for_each_entry(cfg, &pci_mmcfg_list, list) {
330*4882a593Smuzhiyun 		if (cfg->end_bus < cfg->start_bus)
331*4882a593Smuzhiyun 			cfg->end_bus = 255;
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 		/* Don't access the list head ! */
334*4882a593Smuzhiyun 		if (cfg->list.next == &pci_mmcfg_list)
335*4882a593Smuzhiyun 			break;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 		cfgx = list_entry(cfg->list.next, typeof(*cfg), list);
338*4882a593Smuzhiyun 		if (cfg->end_bus >= cfgx->start_bus)
339*4882a593Smuzhiyun 			cfg->end_bus = cfgx->start_bus - 1;
340*4882a593Smuzhiyun 	}
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun 
pci_mmcfg_check_hostbridge(void)343*4882a593Smuzhiyun static int __init pci_mmcfg_check_hostbridge(void)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun 	u32 l;
346*4882a593Smuzhiyun 	u32 bus, devfn;
347*4882a593Smuzhiyun 	u16 vendor, device;
348*4882a593Smuzhiyun 	int i;
349*4882a593Smuzhiyun 	const char *name;
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	if (!raw_pci_ops)
352*4882a593Smuzhiyun 		return 0;
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	free_all_mmcfg();
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(pci_mmcfg_probes); i++) {
357*4882a593Smuzhiyun 		bus =  pci_mmcfg_probes[i].bus;
358*4882a593Smuzhiyun 		devfn = pci_mmcfg_probes[i].devfn;
359*4882a593Smuzhiyun 		raw_pci_ops->read(0, bus, devfn, 0, 4, &l);
360*4882a593Smuzhiyun 		vendor = l & 0xffff;
361*4882a593Smuzhiyun 		device = (l >> 16) & 0xffff;
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 		name = NULL;
364*4882a593Smuzhiyun 		if (pci_mmcfg_probes[i].vendor == vendor &&
365*4882a593Smuzhiyun 		    pci_mmcfg_probes[i].device == device)
366*4882a593Smuzhiyun 			name = pci_mmcfg_probes[i].probe();
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 		if (name)
369*4882a593Smuzhiyun 			pr_info(PREFIX "%s with MMCONFIG support\n", name);
370*4882a593Smuzhiyun 	}
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	/* some end_bus_number is crazy, fix it */
373*4882a593Smuzhiyun 	pci_mmcfg_check_end_bus_number();
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	return !list_empty(&pci_mmcfg_list);
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun 
check_mcfg_resource(struct acpi_resource * res,void * data)378*4882a593Smuzhiyun static acpi_status check_mcfg_resource(struct acpi_resource *res, void *data)
379*4882a593Smuzhiyun {
380*4882a593Smuzhiyun 	struct resource *mcfg_res = data;
381*4882a593Smuzhiyun 	struct acpi_resource_address64 address;
382*4882a593Smuzhiyun 	acpi_status status;
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
385*4882a593Smuzhiyun 		struct acpi_resource_fixed_memory32 *fixmem32 =
386*4882a593Smuzhiyun 			&res->data.fixed_memory32;
387*4882a593Smuzhiyun 		if (!fixmem32)
388*4882a593Smuzhiyun 			return AE_OK;
389*4882a593Smuzhiyun 		if ((mcfg_res->start >= fixmem32->address) &&
390*4882a593Smuzhiyun 		    (mcfg_res->end < (fixmem32->address +
391*4882a593Smuzhiyun 				      fixmem32->address_length))) {
392*4882a593Smuzhiyun 			mcfg_res->flags = 1;
393*4882a593Smuzhiyun 			return AE_CTRL_TERMINATE;
394*4882a593Smuzhiyun 		}
395*4882a593Smuzhiyun 	}
396*4882a593Smuzhiyun 	if ((res->type != ACPI_RESOURCE_TYPE_ADDRESS32) &&
397*4882a593Smuzhiyun 	    (res->type != ACPI_RESOURCE_TYPE_ADDRESS64))
398*4882a593Smuzhiyun 		return AE_OK;
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	status = acpi_resource_to_address64(res, &address);
401*4882a593Smuzhiyun 	if (ACPI_FAILURE(status) ||
402*4882a593Smuzhiyun 	   (address.address.address_length <= 0) ||
403*4882a593Smuzhiyun 	   (address.resource_type != ACPI_MEMORY_RANGE))
404*4882a593Smuzhiyun 		return AE_OK;
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	if ((mcfg_res->start >= address.address.minimum) &&
407*4882a593Smuzhiyun 	    (mcfg_res->end < (address.address.minimum + address.address.address_length))) {
408*4882a593Smuzhiyun 		mcfg_res->flags = 1;
409*4882a593Smuzhiyun 		return AE_CTRL_TERMINATE;
410*4882a593Smuzhiyun 	}
411*4882a593Smuzhiyun 	return AE_OK;
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun 
find_mboard_resource(acpi_handle handle,u32 lvl,void * context,void ** rv)414*4882a593Smuzhiyun static acpi_status find_mboard_resource(acpi_handle handle, u32 lvl,
415*4882a593Smuzhiyun 					void *context, void **rv)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun 	struct resource *mcfg_res = context;
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	acpi_walk_resources(handle, METHOD_NAME__CRS,
420*4882a593Smuzhiyun 			    check_mcfg_resource, context);
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	if (mcfg_res->flags)
423*4882a593Smuzhiyun 		return AE_CTRL_TERMINATE;
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	return AE_OK;
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun 
is_acpi_reserved(u64 start,u64 end,enum e820_type not_used)428*4882a593Smuzhiyun static bool is_acpi_reserved(u64 start, u64 end, enum e820_type not_used)
429*4882a593Smuzhiyun {
430*4882a593Smuzhiyun 	struct resource mcfg_res;
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	mcfg_res.start = start;
433*4882a593Smuzhiyun 	mcfg_res.end = end - 1;
434*4882a593Smuzhiyun 	mcfg_res.flags = 0;
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	acpi_get_devices("PNP0C01", find_mboard_resource, &mcfg_res, NULL);
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	if (!mcfg_res.flags)
439*4882a593Smuzhiyun 		acpi_get_devices("PNP0C02", find_mboard_resource, &mcfg_res,
440*4882a593Smuzhiyun 				 NULL);
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	return mcfg_res.flags;
443*4882a593Smuzhiyun }
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun typedef bool (*check_reserved_t)(u64 start, u64 end, enum e820_type type);
446*4882a593Smuzhiyun 
is_mmconf_reserved(check_reserved_t is_reserved,struct pci_mmcfg_region * cfg,struct device * dev,int with_e820)447*4882a593Smuzhiyun static bool __ref is_mmconf_reserved(check_reserved_t is_reserved,
448*4882a593Smuzhiyun 				     struct pci_mmcfg_region *cfg,
449*4882a593Smuzhiyun 				     struct device *dev, int with_e820)
450*4882a593Smuzhiyun {
451*4882a593Smuzhiyun 	u64 addr = cfg->res.start;
452*4882a593Smuzhiyun 	u64 size = resource_size(&cfg->res);
453*4882a593Smuzhiyun 	u64 old_size = size;
454*4882a593Smuzhiyun 	int num_buses;
455*4882a593Smuzhiyun 	char *method = with_e820 ? "E820" : "ACPI motherboard resources";
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	while (!is_reserved(addr, addr + size, E820_TYPE_RESERVED)) {
458*4882a593Smuzhiyun 		size >>= 1;
459*4882a593Smuzhiyun 		if (size < (16UL<<20))
460*4882a593Smuzhiyun 			break;
461*4882a593Smuzhiyun 	}
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 	if (size < (16UL<<20) && size != old_size)
464*4882a593Smuzhiyun 		return 0;
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	if (dev)
467*4882a593Smuzhiyun 		dev_info(dev, "MMCONFIG at %pR reserved in %s\n",
468*4882a593Smuzhiyun 			 &cfg->res, method);
469*4882a593Smuzhiyun 	else
470*4882a593Smuzhiyun 		pr_info(PREFIX "MMCONFIG at %pR reserved in %s\n",
471*4882a593Smuzhiyun 		       &cfg->res, method);
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	if (old_size != size) {
474*4882a593Smuzhiyun 		/* update end_bus */
475*4882a593Smuzhiyun 		cfg->end_bus = cfg->start_bus + ((size>>20) - 1);
476*4882a593Smuzhiyun 		num_buses = cfg->end_bus - cfg->start_bus + 1;
477*4882a593Smuzhiyun 		cfg->res.end = cfg->res.start +
478*4882a593Smuzhiyun 		    PCI_MMCFG_BUS_OFFSET(num_buses) - 1;
479*4882a593Smuzhiyun 		snprintf(cfg->name, PCI_MMCFG_RESOURCE_NAME_LEN,
480*4882a593Smuzhiyun 			 "PCI MMCONFIG %04x [bus %02x-%02x]",
481*4882a593Smuzhiyun 			 cfg->segment, cfg->start_bus, cfg->end_bus);
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 		if (dev)
484*4882a593Smuzhiyun 			dev_info(dev,
485*4882a593Smuzhiyun 				"MMCONFIG "
486*4882a593Smuzhiyun 				"at %pR (base %#lx) (size reduced!)\n",
487*4882a593Smuzhiyun 				&cfg->res, (unsigned long) cfg->address);
488*4882a593Smuzhiyun 		else
489*4882a593Smuzhiyun 			pr_info(PREFIX
490*4882a593Smuzhiyun 				"MMCONFIG for %04x [bus%02x-%02x] "
491*4882a593Smuzhiyun 				"at %pR (base %#lx) (size reduced!)\n",
492*4882a593Smuzhiyun 				cfg->segment, cfg->start_bus, cfg->end_bus,
493*4882a593Smuzhiyun 				&cfg->res, (unsigned long) cfg->address);
494*4882a593Smuzhiyun 	}
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun 	return 1;
497*4882a593Smuzhiyun }
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun static bool __ref
pci_mmcfg_check_reserved(struct device * dev,struct pci_mmcfg_region * cfg,int early)500*4882a593Smuzhiyun pci_mmcfg_check_reserved(struct device *dev, struct pci_mmcfg_region *cfg, int early)
501*4882a593Smuzhiyun {
502*4882a593Smuzhiyun 	if (!early && !acpi_disabled) {
503*4882a593Smuzhiyun 		if (is_mmconf_reserved(is_acpi_reserved, cfg, dev, 0))
504*4882a593Smuzhiyun 			return 1;
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 		if (dev)
507*4882a593Smuzhiyun 			dev_info(dev, FW_INFO
508*4882a593Smuzhiyun 				 "MMCONFIG at %pR not reserved in "
509*4882a593Smuzhiyun 				 "ACPI motherboard resources\n",
510*4882a593Smuzhiyun 				 &cfg->res);
511*4882a593Smuzhiyun 		else
512*4882a593Smuzhiyun 			pr_info(FW_INFO PREFIX
513*4882a593Smuzhiyun 			       "MMCONFIG at %pR not reserved in "
514*4882a593Smuzhiyun 			       "ACPI motherboard resources\n",
515*4882a593Smuzhiyun 			       &cfg->res);
516*4882a593Smuzhiyun 	}
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	/*
519*4882a593Smuzhiyun 	 * e820__mapped_all() is marked as __init.
520*4882a593Smuzhiyun 	 * All entries from ACPI MCFG table have been checked at boot time.
521*4882a593Smuzhiyun 	 * For MCFG information constructed from hotpluggable host bridge's
522*4882a593Smuzhiyun 	 * _CBA method, just assume it's reserved.
523*4882a593Smuzhiyun 	 */
524*4882a593Smuzhiyun 	if (pci_mmcfg_running_state)
525*4882a593Smuzhiyun 		return 1;
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	/* Don't try to do this check unless configuration
528*4882a593Smuzhiyun 	   type 1 is available. how about type 2 ?*/
529*4882a593Smuzhiyun 	if (raw_pci_ops)
530*4882a593Smuzhiyun 		return is_mmconf_reserved(e820__mapped_all, cfg, dev, 1);
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	return 0;
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun 
pci_mmcfg_reject_broken(int early)535*4882a593Smuzhiyun static void __init pci_mmcfg_reject_broken(int early)
536*4882a593Smuzhiyun {
537*4882a593Smuzhiyun 	struct pci_mmcfg_region *cfg;
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	list_for_each_entry(cfg, &pci_mmcfg_list, list) {
540*4882a593Smuzhiyun 		if (pci_mmcfg_check_reserved(NULL, cfg, early) == 0) {
541*4882a593Smuzhiyun 			pr_info(PREFIX "not using MMCONFIG\n");
542*4882a593Smuzhiyun 			free_all_mmcfg();
543*4882a593Smuzhiyun 			return;
544*4882a593Smuzhiyun 		}
545*4882a593Smuzhiyun 	}
546*4882a593Smuzhiyun }
547*4882a593Smuzhiyun 
acpi_mcfg_check_entry(struct acpi_table_mcfg * mcfg,struct acpi_mcfg_allocation * cfg)548*4882a593Smuzhiyun static int __init acpi_mcfg_check_entry(struct acpi_table_mcfg *mcfg,
549*4882a593Smuzhiyun 					struct acpi_mcfg_allocation *cfg)
550*4882a593Smuzhiyun {
551*4882a593Smuzhiyun 	if (cfg->address < 0xFFFFFFFF)
552*4882a593Smuzhiyun 		return 0;
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 	if (!strncmp(mcfg->header.oem_id, "SGI", 3))
555*4882a593Smuzhiyun 		return 0;
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 	if ((mcfg->header.revision >= 1) && (dmi_get_bios_year() >= 2010))
558*4882a593Smuzhiyun 		return 0;
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 	pr_err(PREFIX "MCFG region for %04x [bus %02x-%02x] at %#llx "
561*4882a593Smuzhiyun 	       "is above 4GB, ignored\n", cfg->pci_segment,
562*4882a593Smuzhiyun 	       cfg->start_bus_number, cfg->end_bus_number, cfg->address);
563*4882a593Smuzhiyun 	return -EINVAL;
564*4882a593Smuzhiyun }
565*4882a593Smuzhiyun 
pci_parse_mcfg(struct acpi_table_header * header)566*4882a593Smuzhiyun static int __init pci_parse_mcfg(struct acpi_table_header *header)
567*4882a593Smuzhiyun {
568*4882a593Smuzhiyun 	struct acpi_table_mcfg *mcfg;
569*4882a593Smuzhiyun 	struct acpi_mcfg_allocation *cfg_table, *cfg;
570*4882a593Smuzhiyun 	unsigned long i;
571*4882a593Smuzhiyun 	int entries;
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 	if (!header)
574*4882a593Smuzhiyun 		return -EINVAL;
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 	mcfg = (struct acpi_table_mcfg *)header;
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun 	/* how many config structures do we have */
579*4882a593Smuzhiyun 	free_all_mmcfg();
580*4882a593Smuzhiyun 	entries = 0;
581*4882a593Smuzhiyun 	i = header->length - sizeof(struct acpi_table_mcfg);
582*4882a593Smuzhiyun 	while (i >= sizeof(struct acpi_mcfg_allocation)) {
583*4882a593Smuzhiyun 		entries++;
584*4882a593Smuzhiyun 		i -= sizeof(struct acpi_mcfg_allocation);
585*4882a593Smuzhiyun 	}
586*4882a593Smuzhiyun 	if (entries == 0) {
587*4882a593Smuzhiyun 		pr_err(PREFIX "MMCONFIG has no entries\n");
588*4882a593Smuzhiyun 		return -ENODEV;
589*4882a593Smuzhiyun 	}
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 	cfg_table = (struct acpi_mcfg_allocation *) &mcfg[1];
592*4882a593Smuzhiyun 	for (i = 0; i < entries; i++) {
593*4882a593Smuzhiyun 		cfg = &cfg_table[i];
594*4882a593Smuzhiyun 		if (acpi_mcfg_check_entry(mcfg, cfg)) {
595*4882a593Smuzhiyun 			free_all_mmcfg();
596*4882a593Smuzhiyun 			return -ENODEV;
597*4882a593Smuzhiyun 		}
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 		if (pci_mmconfig_add(cfg->pci_segment, cfg->start_bus_number,
600*4882a593Smuzhiyun 				   cfg->end_bus_number, cfg->address) == NULL) {
601*4882a593Smuzhiyun 			pr_warn(PREFIX "no memory for MCFG entries\n");
602*4882a593Smuzhiyun 			free_all_mmcfg();
603*4882a593Smuzhiyun 			return -ENOMEM;
604*4882a593Smuzhiyun 		}
605*4882a593Smuzhiyun 	}
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	return 0;
608*4882a593Smuzhiyun }
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun #ifdef CONFIG_ACPI_APEI
611*4882a593Smuzhiyun extern int (*arch_apei_filter_addr)(int (*func)(__u64 start, __u64 size,
612*4882a593Smuzhiyun 				     void *data), void *data);
613*4882a593Smuzhiyun 
pci_mmcfg_for_each_region(int (* func)(__u64 start,__u64 size,void * data),void * data)614*4882a593Smuzhiyun static int pci_mmcfg_for_each_region(int (*func)(__u64 start, __u64 size,
615*4882a593Smuzhiyun 				     void *data), void *data)
616*4882a593Smuzhiyun {
617*4882a593Smuzhiyun 	struct pci_mmcfg_region *cfg;
618*4882a593Smuzhiyun 	int rc;
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 	if (list_empty(&pci_mmcfg_list))
621*4882a593Smuzhiyun 		return 0;
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun 	list_for_each_entry(cfg, &pci_mmcfg_list, list) {
624*4882a593Smuzhiyun 		rc = func(cfg->res.start, resource_size(&cfg->res), data);
625*4882a593Smuzhiyun 		if (rc)
626*4882a593Smuzhiyun 			return rc;
627*4882a593Smuzhiyun 	}
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	return 0;
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun #define set_apei_filter() (arch_apei_filter_addr = pci_mmcfg_for_each_region)
632*4882a593Smuzhiyun #else
633*4882a593Smuzhiyun #define set_apei_filter()
634*4882a593Smuzhiyun #endif
635*4882a593Smuzhiyun 
__pci_mmcfg_init(int early)636*4882a593Smuzhiyun static void __init __pci_mmcfg_init(int early)
637*4882a593Smuzhiyun {
638*4882a593Smuzhiyun 	pci_mmcfg_reject_broken(early);
639*4882a593Smuzhiyun 	if (list_empty(&pci_mmcfg_list))
640*4882a593Smuzhiyun 		return;
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 	if (pcibios_last_bus < 0) {
643*4882a593Smuzhiyun 		const struct pci_mmcfg_region *cfg;
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 		list_for_each_entry(cfg, &pci_mmcfg_list, list) {
646*4882a593Smuzhiyun 			if (cfg->segment)
647*4882a593Smuzhiyun 				break;
648*4882a593Smuzhiyun 			pcibios_last_bus = cfg->end_bus;
649*4882a593Smuzhiyun 		}
650*4882a593Smuzhiyun 	}
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun 	if (pci_mmcfg_arch_init())
653*4882a593Smuzhiyun 		pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
654*4882a593Smuzhiyun 	else {
655*4882a593Smuzhiyun 		free_all_mmcfg();
656*4882a593Smuzhiyun 		pci_mmcfg_arch_init_failed = true;
657*4882a593Smuzhiyun 	}
658*4882a593Smuzhiyun }
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun static int __initdata known_bridge;
661*4882a593Smuzhiyun 
pci_mmcfg_early_init(void)662*4882a593Smuzhiyun void __init pci_mmcfg_early_init(void)
663*4882a593Smuzhiyun {
664*4882a593Smuzhiyun 	if (pci_probe & PCI_PROBE_MMCONF) {
665*4882a593Smuzhiyun 		if (pci_mmcfg_check_hostbridge())
666*4882a593Smuzhiyun 			known_bridge = 1;
667*4882a593Smuzhiyun 		else
668*4882a593Smuzhiyun 			acpi_sfi_table_parse(ACPI_SIG_MCFG, pci_parse_mcfg);
669*4882a593Smuzhiyun 		__pci_mmcfg_init(1);
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun 		set_apei_filter();
672*4882a593Smuzhiyun 	}
673*4882a593Smuzhiyun }
674*4882a593Smuzhiyun 
pci_mmcfg_late_init(void)675*4882a593Smuzhiyun void __init pci_mmcfg_late_init(void)
676*4882a593Smuzhiyun {
677*4882a593Smuzhiyun 	/* MMCONFIG disabled */
678*4882a593Smuzhiyun 	if ((pci_probe & PCI_PROBE_MMCONF) == 0)
679*4882a593Smuzhiyun 		return;
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun 	if (known_bridge)
682*4882a593Smuzhiyun 		return;
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun 	/* MMCONFIG hasn't been enabled yet, try again */
685*4882a593Smuzhiyun 	if (pci_probe & PCI_PROBE_MASK & ~PCI_PROBE_MMCONF) {
686*4882a593Smuzhiyun 		acpi_sfi_table_parse(ACPI_SIG_MCFG, pci_parse_mcfg);
687*4882a593Smuzhiyun 		__pci_mmcfg_init(0);
688*4882a593Smuzhiyun 	}
689*4882a593Smuzhiyun }
690*4882a593Smuzhiyun 
pci_mmcfg_late_insert_resources(void)691*4882a593Smuzhiyun static int __init pci_mmcfg_late_insert_resources(void)
692*4882a593Smuzhiyun {
693*4882a593Smuzhiyun 	struct pci_mmcfg_region *cfg;
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	pci_mmcfg_running_state = true;
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 	/* If we are not using MMCONFIG, don't insert the resources. */
698*4882a593Smuzhiyun 	if ((pci_probe & PCI_PROBE_MMCONF) == 0)
699*4882a593Smuzhiyun 		return 1;
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 	/*
702*4882a593Smuzhiyun 	 * Attempt to insert the mmcfg resources but not with the busy flag
703*4882a593Smuzhiyun 	 * marked so it won't cause request errors when __request_region is
704*4882a593Smuzhiyun 	 * called.
705*4882a593Smuzhiyun 	 */
706*4882a593Smuzhiyun 	list_for_each_entry(cfg, &pci_mmcfg_list, list)
707*4882a593Smuzhiyun 		if (!cfg->res.parent)
708*4882a593Smuzhiyun 			insert_resource(&iomem_resource, &cfg->res);
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 	return 0;
711*4882a593Smuzhiyun }
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun /*
714*4882a593Smuzhiyun  * Perform MMCONFIG resource insertion after PCI initialization to allow for
715*4882a593Smuzhiyun  * misprogrammed MCFG tables that state larger sizes but actually conflict
716*4882a593Smuzhiyun  * with other system resources.
717*4882a593Smuzhiyun  */
718*4882a593Smuzhiyun late_initcall(pci_mmcfg_late_insert_resources);
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun /* Add MMCFG information for host bridges */
pci_mmconfig_insert(struct device * dev,u16 seg,u8 start,u8 end,phys_addr_t addr)721*4882a593Smuzhiyun int pci_mmconfig_insert(struct device *dev, u16 seg, u8 start, u8 end,
722*4882a593Smuzhiyun 			phys_addr_t addr)
723*4882a593Smuzhiyun {
724*4882a593Smuzhiyun 	int rc;
725*4882a593Smuzhiyun 	struct resource *tmp = NULL;
726*4882a593Smuzhiyun 	struct pci_mmcfg_region *cfg;
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 	if (!(pci_probe & PCI_PROBE_MMCONF) || pci_mmcfg_arch_init_failed)
729*4882a593Smuzhiyun 		return -ENODEV;
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	if (start > end)
732*4882a593Smuzhiyun 		return -EINVAL;
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	mutex_lock(&pci_mmcfg_lock);
735*4882a593Smuzhiyun 	cfg = pci_mmconfig_lookup(seg, start);
736*4882a593Smuzhiyun 	if (cfg) {
737*4882a593Smuzhiyun 		if (cfg->end_bus < end)
738*4882a593Smuzhiyun 			dev_info(dev, FW_INFO
739*4882a593Smuzhiyun 				 "MMCONFIG for "
740*4882a593Smuzhiyun 				 "domain %04x [bus %02x-%02x] "
741*4882a593Smuzhiyun 				 "only partially covers this bridge\n",
742*4882a593Smuzhiyun 				  cfg->segment, cfg->start_bus, cfg->end_bus);
743*4882a593Smuzhiyun 		mutex_unlock(&pci_mmcfg_lock);
744*4882a593Smuzhiyun 		return -EEXIST;
745*4882a593Smuzhiyun 	}
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun 	if (!addr) {
748*4882a593Smuzhiyun 		mutex_unlock(&pci_mmcfg_lock);
749*4882a593Smuzhiyun 		return -EINVAL;
750*4882a593Smuzhiyun 	}
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 	rc = -EBUSY;
753*4882a593Smuzhiyun 	cfg = pci_mmconfig_alloc(seg, start, end, addr);
754*4882a593Smuzhiyun 	if (cfg == NULL) {
755*4882a593Smuzhiyun 		dev_warn(dev, "fail to add MMCONFIG (out of memory)\n");
756*4882a593Smuzhiyun 		rc = -ENOMEM;
757*4882a593Smuzhiyun 	} else if (!pci_mmcfg_check_reserved(dev, cfg, 0)) {
758*4882a593Smuzhiyun 		dev_warn(dev, FW_BUG "MMCONFIG %pR isn't reserved\n",
759*4882a593Smuzhiyun 			 &cfg->res);
760*4882a593Smuzhiyun 	} else {
761*4882a593Smuzhiyun 		/* Insert resource if it's not in boot stage */
762*4882a593Smuzhiyun 		if (pci_mmcfg_running_state)
763*4882a593Smuzhiyun 			tmp = insert_resource_conflict(&iomem_resource,
764*4882a593Smuzhiyun 						       &cfg->res);
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun 		if (tmp) {
767*4882a593Smuzhiyun 			dev_warn(dev,
768*4882a593Smuzhiyun 				 "MMCONFIG %pR conflicts with "
769*4882a593Smuzhiyun 				 "%s %pR\n",
770*4882a593Smuzhiyun 				 &cfg->res, tmp->name, tmp);
771*4882a593Smuzhiyun 		} else if (pci_mmcfg_arch_map(cfg)) {
772*4882a593Smuzhiyun 			dev_warn(dev, "fail to map MMCONFIG %pR.\n",
773*4882a593Smuzhiyun 				 &cfg->res);
774*4882a593Smuzhiyun 		} else {
775*4882a593Smuzhiyun 			list_add_sorted(cfg);
776*4882a593Smuzhiyun 			dev_info(dev, "MMCONFIG at %pR (base %#lx)\n",
777*4882a593Smuzhiyun 				 &cfg->res, (unsigned long)addr);
778*4882a593Smuzhiyun 			cfg = NULL;
779*4882a593Smuzhiyun 			rc = 0;
780*4882a593Smuzhiyun 		}
781*4882a593Smuzhiyun 	}
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	if (cfg) {
784*4882a593Smuzhiyun 		if (cfg->res.parent)
785*4882a593Smuzhiyun 			release_resource(&cfg->res);
786*4882a593Smuzhiyun 		kfree(cfg);
787*4882a593Smuzhiyun 	}
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 	mutex_unlock(&pci_mmcfg_lock);
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun 	return rc;
792*4882a593Smuzhiyun }
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun /* Delete MMCFG information for host bridges */
pci_mmconfig_delete(u16 seg,u8 start,u8 end)795*4882a593Smuzhiyun int pci_mmconfig_delete(u16 seg, u8 start, u8 end)
796*4882a593Smuzhiyun {
797*4882a593Smuzhiyun 	struct pci_mmcfg_region *cfg;
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun 	mutex_lock(&pci_mmcfg_lock);
800*4882a593Smuzhiyun 	list_for_each_entry_rcu(cfg, &pci_mmcfg_list, list)
801*4882a593Smuzhiyun 		if (cfg->segment == seg && cfg->start_bus == start &&
802*4882a593Smuzhiyun 		    cfg->end_bus == end) {
803*4882a593Smuzhiyun 			list_del_rcu(&cfg->list);
804*4882a593Smuzhiyun 			synchronize_rcu();
805*4882a593Smuzhiyun 			pci_mmcfg_arch_unmap(cfg);
806*4882a593Smuzhiyun 			if (cfg->res.parent)
807*4882a593Smuzhiyun 				release_resource(&cfg->res);
808*4882a593Smuzhiyun 			mutex_unlock(&pci_mmcfg_lock);
809*4882a593Smuzhiyun 			kfree(cfg);
810*4882a593Smuzhiyun 			return 0;
811*4882a593Smuzhiyun 		}
812*4882a593Smuzhiyun 	mutex_unlock(&pci_mmcfg_lock);
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 	return -ENOENT;
815*4882a593Smuzhiyun }
816