blob: 067a2cfed15c09d60063b1cdbd10d0039f723968 [file] [log] [blame]
Olivier Galibertb7867392007-02-13 13:26:20 +01001/*
2 * mmconfig-shared.c - Low-level direct PCI config space access via
3 * MMCONFIG - common code between i386 and x86-64.
4 *
5 * This code does:
Olivier Galibert9358c692007-02-13 13:26:20 +01006 * - known chipset handling
Olivier Galibertb7867392007-02-13 13:26:20 +01007 * - ACPI decoding and validation
8 *
9 * Per-architecture code takes care of the mappings and accesses
10 * themselves.
11 */
12
13#include <linux/pci.h>
14#include <linux/init.h>
15#include <linux/acpi.h>
Feng Tang5f0db7a2009-08-14 15:37:50 -040016#include <linux/sfi_acpi.h>
Olivier Galibertb7867392007-02-13 13:26:20 +010017#include <linux/bitmap.h>
Bjorn Helgaas9a08f7d2009-10-23 15:20:33 -060018#include <linux/dmi.h>
Yinghai Lu068258b2009-03-19 20:55:35 -070019#include <linux/sort.h>
Olivier Galibertb7867392007-02-13 13:26:20 +010020#include <asm/e820.h>
Jaswinder Singh Rajput82487712008-12-27 18:32:28 +053021#include <asm/pci_x86.h>
Feng Tang5f0db7a2009-08-14 15:37:50 -040022#include <asm/acpi.h>
Olivier Galibertb7867392007-02-13 13:26:20 +010023
Len Brownf4a2d582009-07-28 16:48:02 -040024#define PREFIX "PCI: "
Len Browna192a952009-07-28 16:45:54 -040025
Aaron Durbina5ba7972007-07-21 17:10:34 +020026/* Indicate if the mmcfg resources have been placed into the resource table. */
27static int __initdata pci_mmcfg_resources_inserted;
28
Bjorn Helgaas7da7d362009-11-13 17:33:53 -070029static __init void free_all_mmcfg(void)
30{
31 pci_mmcfg_arch_free();
32 pci_mmcfg_config_num = 0;
33 kfree(pci_mmcfg_config);
34 pci_mmcfg_config = NULL;
35}
36
37static __init struct acpi_mcfg_allocation *pci_mmconfig_add(int segment,
38 int start, int end, u64 addr)
Yinghai Lu068258b2009-03-19 20:55:35 -070039{
40 struct acpi_mcfg_allocation *new;
Bjorn Helgaas7da7d362009-11-13 17:33:53 -070041 int new_num = pci_mmcfg_config_num + 1;
42 int i = pci_mmcfg_config_num;
Yinghai Lu068258b2009-03-19 20:55:35 -070043
Bjorn Helgaasf7ca6982009-11-13 17:34:03 -070044 if (addr == 0)
45 return NULL;
46
Yinghai Lu068258b2009-03-19 20:55:35 -070047 new = kzalloc(sizeof(pci_mmcfg_config[0]) * new_num, GFP_KERNEL);
48 if (!new)
Bjorn Helgaas7da7d362009-11-13 17:33:53 -070049 return NULL;
Yinghai Lu068258b2009-03-19 20:55:35 -070050
51 if (pci_mmcfg_config) {
52 memcpy(new, pci_mmcfg_config,
53 sizeof(pci_mmcfg_config[0]) * new_num);
54 kfree(pci_mmcfg_config);
55 }
56 pci_mmcfg_config = new;
57
Yinghai Lu068258b2009-03-19 20:55:35 -070058 pci_mmcfg_config_num++;
59 pci_mmcfg_config[i].address = addr;
60 pci_mmcfg_config[i].pci_segment = segment;
61 pci_mmcfg_config[i].start_bus_number = start;
62 pci_mmcfg_config[i].end_bus_number = end;
Bjorn Helgaas7da7d362009-11-13 17:33:53 -070063
64 return &pci_mmcfg_config[i];
Yinghai Lu068258b2009-03-19 20:55:35 -070065}
66
OGAWA Hirofumi429d5122007-02-13 13:26:20 +010067static const char __init *pci_mmcfg_e7520(void)
Olivier Galibert9358c692007-02-13 13:26:20 +010068{
69 u32 win;
Yinghai Lubb63b422008-02-28 23:56:50 -080070 raw_pci_ops->read(0, 0, PCI_DEVFN(0, 0), 0xce, 2, &win);
Olivier Galibert9358c692007-02-13 13:26:20 +010071
Olivier Galibertb5229db2007-05-02 19:27:22 +020072 win = win & 0xf000;
Yinghai Lu068258b2009-03-19 20:55:35 -070073 if (win == 0x0000 || win == 0xf000)
74 return NULL;
75
Bjorn Helgaas7da7d362009-11-13 17:33:53 -070076 if (pci_mmconfig_add(0, 0, 255, win << 16) == NULL)
Yinghai Lu068258b2009-03-19 20:55:35 -070077 return NULL;
78
Olivier Galibert9358c692007-02-13 13:26:20 +010079 return "Intel Corporation E7520 Memory Controller Hub";
80}
81
OGAWA Hirofumi429d5122007-02-13 13:26:20 +010082static const char __init *pci_mmcfg_intel_945(void)
Olivier Galibert9358c692007-02-13 13:26:20 +010083{
84 u32 pciexbar, mask = 0, len = 0;
85
Yinghai Lubb63b422008-02-28 23:56:50 -080086 raw_pci_ops->read(0, 0, PCI_DEVFN(0, 0), 0x48, 4, &pciexbar);
Olivier Galibert9358c692007-02-13 13:26:20 +010087
88 /* Enable bit */
89 if (!(pciexbar & 1))
Yinghai Lu068258b2009-03-19 20:55:35 -070090 return NULL;
Olivier Galibert9358c692007-02-13 13:26:20 +010091
92 /* Size bits */
93 switch ((pciexbar >> 1) & 3) {
94 case 0:
95 mask = 0xf0000000U;
96 len = 0x10000000U;
97 break;
98 case 1:
99 mask = 0xf8000000U;
100 len = 0x08000000U;
101 break;
102 case 2:
103 mask = 0xfc000000U;
104 len = 0x04000000U;
105 break;
106 default:
Yinghai Lu068258b2009-03-19 20:55:35 -0700107 return NULL;
Olivier Galibert9358c692007-02-13 13:26:20 +0100108 }
109
110 /* Errata #2, things break when not aligned on a 256Mb boundary */
111 /* Can only happen in 64M/128M mode */
112
113 if ((pciexbar & mask) & 0x0fffffffU)
Yinghai Lu068258b2009-03-19 20:55:35 -0700114 return NULL;
Olivier Galibert9358c692007-02-13 13:26:20 +0100115
Olivier Galibertb5229db2007-05-02 19:27:22 +0200116 /* Don't hit the APIC registers and their friends */
117 if ((pciexbar & mask) >= 0xf0000000U)
Yinghai Lu068258b2009-03-19 20:55:35 -0700118 return NULL;
Olivier Galibertb5229db2007-05-02 19:27:22 +0200119
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700120 if (pci_mmconfig_add(0, 0, (len >> 20) - 1, pciexbar & mask) == NULL)
Yinghai Lu068258b2009-03-19 20:55:35 -0700121 return NULL;
122
Olivier Galibert9358c692007-02-13 13:26:20 +0100123 return "Intel Corporation 945G/GZ/P/PL Express Memory Controller Hub";
124}
125
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800126static const char __init *pci_mmcfg_amd_fam10h(void)
127{
128 u32 low, high, address;
129 u64 base, msr;
130 int i;
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700131 unsigned segnbits = 0, busnbits, end_bus;
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800132
Yinghai Lu5f0b2972008-04-14 16:08:25 -0700133 if (!(pci_probe & PCI_CHECK_ENABLE_AMD_MMCONF))
134 return NULL;
135
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800136 address = MSR_FAM10H_MMIO_CONF_BASE;
137 if (rdmsr_safe(address, &low, &high))
138 return NULL;
139
140 msr = high;
141 msr <<= 32;
142 msr |= low;
143
144 /* mmconfig is not enable */
145 if (!(msr & FAM10H_MMIO_CONF_ENABLE))
146 return NULL;
147
148 base = msr & (FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT);
149
150 busnbits = (msr >> FAM10H_MMIO_CONF_BUSRANGE_SHIFT) &
151 FAM10H_MMIO_CONF_BUSRANGE_MASK;
152
153 /*
154 * only handle bus 0 ?
155 * need to skip it
156 */
157 if (!busnbits)
158 return NULL;
159
160 if (busnbits > 8) {
161 segnbits = busnbits - 8;
162 busnbits = 8;
163 }
164
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700165 end_bus = (1 << busnbits) - 1;
Yinghai Lu068258b2009-03-19 20:55:35 -0700166 for (i = 0; i < (1 << segnbits); i++)
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700167 if (pci_mmconfig_add(i, 0, end_bus,
168 base + (1<<28) * i) == NULL) {
169 free_all_mmcfg();
170 return NULL;
171 }
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800172
173 return "AMD Family 10h NB";
174}
175
Ed Swierk5546d6f2009-03-19 20:57:56 -0700176static bool __initdata mcp55_checked;
177static const char __init *pci_mmcfg_nvidia_mcp55(void)
178{
179 int bus;
180 int mcp55_mmconf_found = 0;
181
182 static const u32 extcfg_regnum = 0x90;
183 static const u32 extcfg_regsize = 4;
184 static const u32 extcfg_enable_mask = 1<<31;
185 static const u32 extcfg_start_mask = 0xff<<16;
186 static const int extcfg_start_shift = 16;
187 static const u32 extcfg_size_mask = 0x3<<28;
188 static const int extcfg_size_shift = 28;
189 static const int extcfg_sizebus[] = {0x100, 0x80, 0x40, 0x20};
190 static const u32 extcfg_base_mask[] = {0x7ff8, 0x7ffc, 0x7ffe, 0x7fff};
191 static const int extcfg_base_lshift = 25;
192
193 /*
194 * do check if amd fam10h already took over
195 */
196 if (!acpi_disabled || pci_mmcfg_config_num || mcp55_checked)
197 return NULL;
198
199 mcp55_checked = true;
200 for (bus = 0; bus < 256; bus++) {
201 u64 base;
202 u32 l, extcfg;
203 u16 vendor, device;
204 int start, size_index, end;
205
206 raw_pci_ops->read(0, bus, PCI_DEVFN(0, 0), 0, 4, &l);
207 vendor = l & 0xffff;
208 device = (l >> 16) & 0xffff;
209
210 if (PCI_VENDOR_ID_NVIDIA != vendor || 0x0369 != device)
211 continue;
212
213 raw_pci_ops->read(0, bus, PCI_DEVFN(0, 0), extcfg_regnum,
214 extcfg_regsize, &extcfg);
215
216 if (!(extcfg & extcfg_enable_mask))
217 continue;
218
Ed Swierk5546d6f2009-03-19 20:57:56 -0700219 size_index = (extcfg & extcfg_size_mask) >> extcfg_size_shift;
220 base = extcfg & extcfg_base_mask[size_index];
221 /* base could > 4G */
222 base <<= extcfg_base_lshift;
223 start = (extcfg & extcfg_start_mask) >> extcfg_start_shift;
224 end = start + extcfg_sizebus[size_index] - 1;
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700225 if (pci_mmconfig_add(0, start, end, base) == NULL)
226 continue;
Ed Swierk5546d6f2009-03-19 20:57:56 -0700227 mcp55_mmconf_found++;
228 }
229
230 if (!mcp55_mmconf_found)
231 return NULL;
232
233 return "nVidia MCP55";
234}
235
Olivier Galibert9358c692007-02-13 13:26:20 +0100236struct pci_mmcfg_hostbridge_probe {
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800237 u32 bus;
238 u32 devfn;
Olivier Galibert9358c692007-02-13 13:26:20 +0100239 u32 vendor;
240 u32 device;
241 const char *(*probe)(void);
242};
243
OGAWA Hirofumi429d5122007-02-13 13:26:20 +0100244static struct pci_mmcfg_hostbridge_probe pci_mmcfg_probes[] __initdata = {
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800245 { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
246 PCI_DEVICE_ID_INTEL_E7520_MCH, pci_mmcfg_e7520 },
247 { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
248 PCI_DEVICE_ID_INTEL_82945G_HB, pci_mmcfg_intel_945 },
249 { 0, PCI_DEVFN(0x18, 0), PCI_VENDOR_ID_AMD,
250 0x1200, pci_mmcfg_amd_fam10h },
251 { 0xff, PCI_DEVFN(0, 0), PCI_VENDOR_ID_AMD,
252 0x1200, pci_mmcfg_amd_fam10h },
Ed Swierk5546d6f2009-03-19 20:57:56 -0700253 { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_NVIDIA,
254 0x0369, pci_mmcfg_nvidia_mcp55 },
Olivier Galibert9358c692007-02-13 13:26:20 +0100255};
256
Yinghai Lu068258b2009-03-19 20:55:35 -0700257static int __init cmp_mmcfg(const void *x1, const void *x2)
258{
259 const typeof(pci_mmcfg_config[0]) *m1 = x1;
260 const typeof(pci_mmcfg_config[0]) *m2 = x2;
261 int start1, start2;
262
263 start1 = m1->start_bus_number;
264 start2 = m2->start_bus_number;
265
266 return start1 - start2;
267}
268
269static void __init pci_mmcfg_check_end_bus_number(void)
270{
271 int i;
272 typeof(pci_mmcfg_config[0]) *cfg, *cfgx;
273
274 /* sort them at first */
275 sort(pci_mmcfg_config, pci_mmcfg_config_num,
276 sizeof(pci_mmcfg_config[0]), cmp_mmcfg, NULL);
277
278 /* last one*/
279 if (pci_mmcfg_config_num > 0) {
280 i = pci_mmcfg_config_num - 1;
281 cfg = &pci_mmcfg_config[i];
282 if (cfg->end_bus_number < cfg->start_bus_number)
283 cfg->end_bus_number = 255;
284 }
285
286 /* don't overlap please */
287 for (i = 0; i < pci_mmcfg_config_num - 1; i++) {
288 cfg = &pci_mmcfg_config[i];
289 cfgx = &pci_mmcfg_config[i+1];
290
291 if (cfg->end_bus_number < cfg->start_bus_number)
292 cfg->end_bus_number = 255;
293
294 if (cfg->end_bus_number >= cfgx->start_bus_number)
295 cfg->end_bus_number = cfgx->start_bus_number - 1;
296 }
297}
298
Olivier Galibert9358c692007-02-13 13:26:20 +0100299static int __init pci_mmcfg_check_hostbridge(void)
300{
301 u32 l;
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800302 u32 bus, devfn;
Olivier Galibert9358c692007-02-13 13:26:20 +0100303 u16 vendor, device;
304 int i;
305 const char *name;
306
Yinghai Lubb63b422008-02-28 23:56:50 -0800307 if (!raw_pci_ops)
308 return 0;
309
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700310 free_all_mmcfg();
Olivier Galibert9358c692007-02-13 13:26:20 +0100311
Yinghai Lu068258b2009-03-19 20:55:35 -0700312 for (i = 0; i < ARRAY_SIZE(pci_mmcfg_probes); i++) {
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800313 bus = pci_mmcfg_probes[i].bus;
314 devfn = pci_mmcfg_probes[i].devfn;
Yinghai Lubb63b422008-02-28 23:56:50 -0800315 raw_pci_ops->read(0, bus, devfn, 0, 4, &l);
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800316 vendor = l & 0xffff;
317 device = (l >> 16) & 0xffff;
318
Yinghai Lu068258b2009-03-19 20:55:35 -0700319 name = NULL;
OGAWA Hirofumi429d5122007-02-13 13:26:20 +0100320 if (pci_mmcfg_probes[i].vendor == vendor &&
321 pci_mmcfg_probes[i].device == device)
Olivier Galibert9358c692007-02-13 13:26:20 +0100322 name = pci_mmcfg_probes[i].probe();
Yinghai Lu068258b2009-03-19 20:55:35 -0700323
324 if (name)
325 printk(KERN_INFO "PCI: Found %s with MMCONFIG support.\n",
326 name);
OGAWA Hirofumi429d5122007-02-13 13:26:20 +0100327 }
Olivier Galibert9358c692007-02-13 13:26:20 +0100328
Yinghai Lu068258b2009-03-19 20:55:35 -0700329 /* some end_bus_number is crazy, fix it */
330 pci_mmcfg_check_end_bus_number();
Olivier Galibert9358c692007-02-13 13:26:20 +0100331
Yinghai Lu068258b2009-03-19 20:55:35 -0700332 return pci_mmcfg_config_num != 0;
Olivier Galibert9358c692007-02-13 13:26:20 +0100333}
334
Yinghai Luebd60cd2008-09-04 21:04:32 +0200335static void __init pci_mmcfg_insert_resources(void)
Olivier Galibert6a0668f2007-02-13 13:26:20 +0100336{
Yinghai Lu068258b2009-03-19 20:55:35 -0700337#define PCI_MMCFG_RESOURCE_NAME_LEN 24
Olivier Galibert6a0668f2007-02-13 13:26:20 +0100338 int i;
339 struct resource *res;
340 char *names;
341 unsigned num_buses;
342
343 res = kcalloc(PCI_MMCFG_RESOURCE_NAME_LEN + sizeof(*res),
344 pci_mmcfg_config_num, GFP_KERNEL);
Olivier Galibert6a0668f2007-02-13 13:26:20 +0100345 if (!res) {
346 printk(KERN_ERR "PCI: Unable to allocate MMCONFIG resources\n");
347 return;
348 }
349
350 names = (void *)&res[pci_mmcfg_config_num];
351 for (i = 0; i < pci_mmcfg_config_num; i++, res++) {
OGAWA Hirofumi429d5122007-02-13 13:26:20 +0100352 struct acpi_mcfg_allocation *cfg = &pci_mmcfg_config[i];
353 num_buses = cfg->end_bus_number - cfg->start_bus_number + 1;
Olivier Galibert6a0668f2007-02-13 13:26:20 +0100354 res->name = names;
Yinghai Lu068258b2009-03-19 20:55:35 -0700355 snprintf(names, PCI_MMCFG_RESOURCE_NAME_LEN,
356 "PCI MMCONFIG %u [%02x-%02x]", cfg->pci_segment,
357 cfg->start_bus_number, cfg->end_bus_number);
358 res->start = cfg->address + (cfg->start_bus_number << 20);
Olivier Galibert6a0668f2007-02-13 13:26:20 +0100359 res->end = res->start + (num_buses << 20) - 1;
Yinghai Luebd60cd2008-09-04 21:04:32 +0200360 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
Olivier Galibert6a0668f2007-02-13 13:26:20 +0100361 insert_resource(&iomem_resource, res);
362 names += PCI_MMCFG_RESOURCE_NAME_LEN;
363 }
Aaron Durbina5ba7972007-07-21 17:10:34 +0200364
365 /* Mark that the resources have been inserted. */
366 pci_mmcfg_resources_inserted = 1;
Olivier Galibert6a0668f2007-02-13 13:26:20 +0100367}
368
Robert Hancock7752d5c2008-02-15 01:27:20 -0800369static acpi_status __init check_mcfg_resource(struct acpi_resource *res,
370 void *data)
371{
372 struct resource *mcfg_res = data;
373 struct acpi_resource_address64 address;
374 acpi_status status;
375
376 if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
377 struct acpi_resource_fixed_memory32 *fixmem32 =
378 &res->data.fixed_memory32;
379 if (!fixmem32)
380 return AE_OK;
381 if ((mcfg_res->start >= fixmem32->address) &&
Yinghai Lu75e613c2009-06-03 00:13:13 -0700382 (mcfg_res->end < (fixmem32->address +
Robert Hancock7752d5c2008-02-15 01:27:20 -0800383 fixmem32->address_length))) {
384 mcfg_res->flags = 1;
385 return AE_CTRL_TERMINATE;
386 }
387 }
388 if ((res->type != ACPI_RESOURCE_TYPE_ADDRESS32) &&
389 (res->type != ACPI_RESOURCE_TYPE_ADDRESS64))
390 return AE_OK;
391
392 status = acpi_resource_to_address64(res, &address);
393 if (ACPI_FAILURE(status) ||
394 (address.address_length <= 0) ||
395 (address.resource_type != ACPI_MEMORY_RANGE))
396 return AE_OK;
397
398 if ((mcfg_res->start >= address.minimum) &&
Yinghai Lu75e613c2009-06-03 00:13:13 -0700399 (mcfg_res->end < (address.minimum + address.address_length))) {
Robert Hancock7752d5c2008-02-15 01:27:20 -0800400 mcfg_res->flags = 1;
401 return AE_CTRL_TERMINATE;
402 }
403 return AE_OK;
404}
405
406static acpi_status __init find_mboard_resource(acpi_handle handle, u32 lvl,
407 void *context, void **rv)
408{
409 struct resource *mcfg_res = context;
410
411 acpi_walk_resources(handle, METHOD_NAME__CRS,
412 check_mcfg_resource, context);
413
414 if (mcfg_res->flags)
415 return AE_CTRL_TERMINATE;
416
417 return AE_OK;
418}
419
Yinghai Lua83fe322008-07-18 13:22:36 -0700420static int __init is_acpi_reserved(u64 start, u64 end, unsigned not_used)
Robert Hancock7752d5c2008-02-15 01:27:20 -0800421{
422 struct resource mcfg_res;
423
424 mcfg_res.start = start;
Yinghai Lu75e613c2009-06-03 00:13:13 -0700425 mcfg_res.end = end - 1;
Robert Hancock7752d5c2008-02-15 01:27:20 -0800426 mcfg_res.flags = 0;
427
428 acpi_get_devices("PNP0C01", find_mboard_resource, &mcfg_res, NULL);
429
430 if (!mcfg_res.flags)
431 acpi_get_devices("PNP0C02", find_mboard_resource, &mcfg_res,
432 NULL);
433
434 return mcfg_res.flags;
435}
436
Yinghai Lua83fe322008-07-18 13:22:36 -0700437typedef int (*check_reserved_t)(u64 start, u64 end, unsigned type);
438
439static int __init is_mmconf_reserved(check_reserved_t is_reserved,
440 u64 addr, u64 size, int i,
441 typeof(pci_mmcfg_config[0]) *cfg, int with_e820)
442{
443 u64 old_size = size;
444 int valid = 0;
445
Yinghai Lu044cd802009-04-18 01:43:46 -0700446 while (!is_reserved(addr, addr + size, E820_RESERVED)) {
Yinghai Lua83fe322008-07-18 13:22:36 -0700447 size >>= 1;
448 if (size < (16UL<<20))
449 break;
450 }
451
452 if (size >= (16UL<<20) || size == old_size) {
453 printk(KERN_NOTICE
454 "PCI: MCFG area at %Lx reserved in %s\n",
455 addr, with_e820?"E820":"ACPI motherboard resources");
456 valid = 1;
457
458 if (old_size != size) {
459 /* update end_bus_number */
460 cfg->end_bus_number = cfg->start_bus_number + ((size>>20) - 1);
461 printk(KERN_NOTICE "PCI: updated MCFG configuration %d: base %lx "
462 "segment %hu buses %u - %u\n",
463 i, (unsigned long)cfg->address, cfg->pci_segment,
464 (unsigned int)cfg->start_bus_number,
465 (unsigned int)cfg->end_bus_number);
466 }
467 }
468
469 return valid;
470}
471
Yinghai Lubb63b422008-02-28 23:56:50 -0800472static void __init pci_mmcfg_reject_broken(int early)
OGAWA Hirofumi44de0202007-02-13 13:26:20 +0100473{
OGAWA Hirofumi26054ed2007-02-13 13:26:20 +0100474 typeof(pci_mmcfg_config[0]) *cfg;
Robert Hancock7752d5c2008-02-15 01:27:20 -0800475 int i;
OGAWA Hirofumi26054ed2007-02-13 13:26:20 +0100476
Bjorn Helgaasf7ca6982009-11-13 17:34:03 -0700477 if (pci_mmcfg_config_num == 0)
OGAWA Hirofumi26054ed2007-02-13 13:26:20 +0100478 return;
479
Robert Hancock7752d5c2008-02-15 01:27:20 -0800480 for (i = 0; i < pci_mmcfg_config_num; i++) {
Yinghai Lu05c58b82008-02-15 01:30:14 -0800481 int valid = 0;
Yinghai Lua83fe322008-07-18 13:22:36 -0700482 u64 addr, size;
483
Robert Hancock7752d5c2008-02-15 01:27:20 -0800484 cfg = &pci_mmcfg_config[i];
Yinghai Lua83fe322008-07-18 13:22:36 -0700485 addr = cfg->start_bus_number;
486 addr <<= 20;
487 addr += cfg->address;
488 size = cfg->end_bus_number + 1 - cfg->start_bus_number;
489 size <<= 20;
Yinghai Lu05c58b82008-02-15 01:30:14 -0800490 printk(KERN_NOTICE "PCI: MCFG configuration %d: base %lx "
Robert Hancock7752d5c2008-02-15 01:27:20 -0800491 "segment %hu buses %u - %u\n",
492 i, (unsigned long)cfg->address, cfg->pci_segment,
493 (unsigned int)cfg->start_bus_number,
494 (unsigned int)cfg->end_bus_number);
Yinghai Lu05c58b82008-02-15 01:30:14 -0800495
Feng Tang5f0db7a2009-08-14 15:37:50 -0400496 if (!early && !acpi_disabled)
Yinghai Lua83fe322008-07-18 13:22:36 -0700497 valid = is_mmconf_reserved(is_acpi_reserved, addr, size, i, cfg, 0);
Yinghai Lu05c58b82008-02-15 01:30:14 -0800498
499 if (valid)
500 continue;
501
502 if (!early)
Robert Hancock7752d5c2008-02-15 01:27:20 -0800503 printk(KERN_ERR "PCI: BIOS Bug: MCFG area at %Lx is not"
504 " reserved in ACPI motherboard resources\n",
505 cfg->address);
Yinghai Lua83fe322008-07-18 13:22:36 -0700506
Yinghai Lu05c58b82008-02-15 01:30:14 -0800507 /* Don't try to do this check unless configuration
Yinghai Lubb63b422008-02-28 23:56:50 -0800508 type 1 is available. how about type 2 ?*/
Yinghai Lua83fe322008-07-18 13:22:36 -0700509 if (raw_pci_ops)
510 valid = is_mmconf_reserved(e820_all_mapped, addr, size, i, cfg, 1);
Yinghai Lu05c58b82008-02-15 01:30:14 -0800511
512 if (!valid)
513 goto reject;
OGAWA Hirofumi26054ed2007-02-13 13:26:20 +0100514 }
Robert Hancock7752d5c2008-02-15 01:27:20 -0800515
OGAWA Hirofumi26054ed2007-02-13 13:26:20 +0100516 return;
517
518reject:
Dave Jonesef310232008-08-14 15:07:03 -0400519 printk(KERN_INFO "PCI: Not using MMCONFIG.\n");
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700520 free_all_mmcfg();
OGAWA Hirofumi44de0202007-02-13 13:26:20 +0100521}
522
Yinghai Lu05c58b82008-02-15 01:30:14 -0800523static int __initdata known_bridge;
524
Len Brownc4bf2f32009-06-11 23:53:55 -0400525/* The physical address of the MMCONFIG aperture. Set from ACPI tables. */
526struct acpi_mcfg_allocation *pci_mmcfg_config;
527int pci_mmcfg_config_num;
528
Bjorn Helgaas9a08f7d2009-10-23 15:20:33 -0600529static int __init acpi_mcfg_check_entry(struct acpi_table_mcfg *mcfg,
530 struct acpi_mcfg_allocation *cfg)
Len Brownc4bf2f32009-06-11 23:53:55 -0400531{
Bjorn Helgaas9a08f7d2009-10-23 15:20:33 -0600532 int year;
Len Brownc4bf2f32009-06-11 23:53:55 -0400533
Bjorn Helgaas9a08f7d2009-10-23 15:20:33 -0600534 if (cfg->address < 0xFFFFFFFF)
535 return 0;
536
537 if (!strcmp(mcfg->header.oem_id, "SGI"))
538 return 0;
539
540 if (mcfg->header.revision >= 1) {
541 if (dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL) &&
542 year >= 2010)
543 return 0;
544 }
545
546 printk(KERN_ERR PREFIX "MCFG region for %04x:%02x-%02x at %#llx "
547 "is above 4GB, ignored\n", cfg->pci_segment,
548 cfg->start_bus_number, cfg->end_bus_number, cfg->address);
549 return -EINVAL;
Len Brownc4bf2f32009-06-11 23:53:55 -0400550}
551
552static int __init pci_parse_mcfg(struct acpi_table_header *header)
553{
554 struct acpi_table_mcfg *mcfg;
Bjorn Helgaasd3578ef2009-11-13 17:33:47 -0700555 struct acpi_mcfg_allocation *cfg_table, *cfg;
Len Brownc4bf2f32009-06-11 23:53:55 -0400556 unsigned long i;
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700557 int entries;
Len Brownc4bf2f32009-06-11 23:53:55 -0400558
559 if (!header)
560 return -EINVAL;
561
562 mcfg = (struct acpi_table_mcfg *)header;
563
564 /* how many config structures do we have */
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700565 free_all_mmcfg();
Bjorn Helgaase823d6f2009-11-13 17:33:42 -0700566 entries = 0;
Len Brownc4bf2f32009-06-11 23:53:55 -0400567 i = header->length - sizeof(struct acpi_table_mcfg);
568 while (i >= sizeof(struct acpi_mcfg_allocation)) {
Bjorn Helgaase823d6f2009-11-13 17:33:42 -0700569 entries++;
Len Brownc4bf2f32009-06-11 23:53:55 -0400570 i -= sizeof(struct acpi_mcfg_allocation);
571 };
Bjorn Helgaase823d6f2009-11-13 17:33:42 -0700572 if (entries == 0) {
Len Brownc4bf2f32009-06-11 23:53:55 -0400573 printk(KERN_ERR PREFIX "MMCONFIG has no entries\n");
574 return -ENODEV;
575 }
576
Bjorn Helgaasd3578ef2009-11-13 17:33:47 -0700577 cfg_table = (struct acpi_mcfg_allocation *) &mcfg[1];
Bjorn Helgaase823d6f2009-11-13 17:33:42 -0700578 for (i = 0; i < entries; i++) {
Bjorn Helgaasd3578ef2009-11-13 17:33:47 -0700579 cfg = &cfg_table[i];
580 if (acpi_mcfg_check_entry(mcfg, cfg)) {
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700581 free_all_mmcfg();
Len Brownc4bf2f32009-06-11 23:53:55 -0400582 return -ENODEV;
583 }
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700584
585 if (pci_mmconfig_add(cfg->pci_segment, cfg->start_bus_number,
586 cfg->end_bus_number, cfg->address) == NULL) {
587 printk(KERN_WARNING PREFIX
588 "no memory for MCFG entries\n");
589 free_all_mmcfg();
590 return -ENOMEM;
591 }
Len Brownc4bf2f32009-06-11 23:53:55 -0400592 }
593
594 return 0;
595}
596
Thomas Gleixner968cbfa2008-05-12 15:43:37 +0200597static void __init __pci_mmcfg_init(int early)
Olivier Galibertb7867392007-02-13 13:26:20 +0100598{
Robert Hancock7752d5c2008-02-15 01:27:20 -0800599 /* MMCONFIG disabled */
600 if ((pci_probe & PCI_PROBE_MMCONF) == 0)
601 return;
602
603 /* MMCONFIG already enabled */
Yinghai Lu05c58b82008-02-15 01:30:14 -0800604 if (!early && !(pci_probe & PCI_PROBE_MASK & ~PCI_PROBE_MMCONF))
Robert Hancock7752d5c2008-02-15 01:27:20 -0800605 return;
606
Yinghai Lu05c58b82008-02-15 01:30:14 -0800607 /* for late to exit */
608 if (known_bridge)
609 return;
Robert Hancock7752d5c2008-02-15 01:27:20 -0800610
Yinghai Lubb63b422008-02-28 23:56:50 -0800611 if (early) {
Yinghai Lu05c58b82008-02-15 01:30:14 -0800612 if (pci_mmcfg_check_hostbridge())
613 known_bridge = 1;
614 }
615
Yinghai Lu068258b2009-03-19 20:55:35 -0700616 if (!known_bridge)
Feng Tang5f0db7a2009-08-14 15:37:50 -0400617 acpi_sfi_table_parse(ACPI_SIG_MCFG, pci_parse_mcfg);
Yinghai Lu068258b2009-03-19 20:55:35 -0700618
619 pci_mmcfg_reject_broken(early);
Olivier Galibertb7867392007-02-13 13:26:20 +0100620
Bjorn Helgaasf7ca6982009-11-13 17:34:03 -0700621 if (pci_mmcfg_config_num == 0)
Olivier Galibertb7867392007-02-13 13:26:20 +0100622 return;
623
Yinghai Luebd60cd2008-09-04 21:04:32 +0200624 if (pci_mmcfg_arch_init())
Olivier Galibertb7867392007-02-13 13:26:20 +0100625 pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
Yinghai Luebd60cd2008-09-04 21:04:32 +0200626 else {
Aaron Durbina5ba7972007-07-21 17:10:34 +0200627 /*
628 * Signal not to attempt to insert mmcfg resources because
629 * the architecture mmcfg setup could not initialize.
630 */
631 pci_mmcfg_resources_inserted = 1;
Olivier Galibertb7867392007-02-13 13:26:20 +0100632 }
633}
Aaron Durbina5ba7972007-07-21 17:10:34 +0200634
Yinghai Lubb63b422008-02-28 23:56:50 -0800635void __init pci_mmcfg_early_init(void)
Yinghai Lu05c58b82008-02-15 01:30:14 -0800636{
Yinghai Lubb63b422008-02-28 23:56:50 -0800637 __pci_mmcfg_init(1);
Yinghai Lu05c58b82008-02-15 01:30:14 -0800638}
639
640void __init pci_mmcfg_late_init(void)
641{
Yinghai Lubb63b422008-02-28 23:56:50 -0800642 __pci_mmcfg_init(0);
Yinghai Lu05c58b82008-02-15 01:30:14 -0800643}
644
Aaron Durbina5ba7972007-07-21 17:10:34 +0200645static int __init pci_mmcfg_late_insert_resources(void)
646{
647 /*
648 * If resources are already inserted or we are not using MMCONFIG,
649 * don't insert the resources.
650 */
651 if ((pci_mmcfg_resources_inserted == 1) ||
652 (pci_probe & PCI_PROBE_MMCONF) == 0 ||
Bjorn Helgaasf7ca6982009-11-13 17:34:03 -0700653 (pci_mmcfg_config_num == 0))
Aaron Durbina5ba7972007-07-21 17:10:34 +0200654 return 1;
655
656 /*
657 * Attempt to insert the mmcfg resources but not with the busy flag
658 * marked so it won't cause request errors when __request_region is
659 * called.
660 */
Yinghai Luebd60cd2008-09-04 21:04:32 +0200661 pci_mmcfg_insert_resources();
Aaron Durbina5ba7972007-07-21 17:10:34 +0200662
663 return 0;
664}
665
666/*
667 * Perform MMCONFIG resource insertion after PCI initialization to allow for
668 * misprogrammed MCFG tables that state larger sizes but actually conflict
669 * with other system resources.
670 */
671late_initcall(pci_mmcfg_late_insert_resources);