blob: 02642773c29d3b2314c5b1c874a02b8b6024430a [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
Olivier Galibertb7867392007-02-13 13:26:20 +010026/* aperture is up to 256MB but BIOS may reserve less */
27#define MMCONFIG_APER_MIN (2 * 1024*1024)
28#define MMCONFIG_APER_MAX (256 * 1024*1024)
29
Aaron Durbina5ba7972007-07-21 17:10:34 +020030/* Indicate if the mmcfg resources have been placed into the resource table. */
31static int __initdata pci_mmcfg_resources_inserted;
32
Yinghai Lu068258b2009-03-19 20:55:35 -070033static __init int extend_mmcfg(int num)
34{
35 struct acpi_mcfg_allocation *new;
36 int new_num = pci_mmcfg_config_num + num;
37
38 new = kzalloc(sizeof(pci_mmcfg_config[0]) * new_num, GFP_KERNEL);
39 if (!new)
40 return -1;
41
42 if (pci_mmcfg_config) {
43 memcpy(new, pci_mmcfg_config,
44 sizeof(pci_mmcfg_config[0]) * new_num);
45 kfree(pci_mmcfg_config);
46 }
47 pci_mmcfg_config = new;
48
49 return 0;
50}
51
52static __init void fill_one_mmcfg(u64 addr, int segment, int start, int end)
53{
54 int i = pci_mmcfg_config_num;
55
56 pci_mmcfg_config_num++;
57 pci_mmcfg_config[i].address = addr;
58 pci_mmcfg_config[i].pci_segment = segment;
59 pci_mmcfg_config[i].start_bus_number = start;
60 pci_mmcfg_config[i].end_bus_number = end;
61}
62
OGAWA Hirofumi429d5122007-02-13 13:26:20 +010063static const char __init *pci_mmcfg_e7520(void)
Olivier Galibert9358c692007-02-13 13:26:20 +010064{
65 u32 win;
Yinghai Lubb63b422008-02-28 23:56:50 -080066 raw_pci_ops->read(0, 0, PCI_DEVFN(0, 0), 0xce, 2, &win);
Olivier Galibert9358c692007-02-13 13:26:20 +010067
Olivier Galibertb5229db2007-05-02 19:27:22 +020068 win = win & 0xf000;
Yinghai Lu068258b2009-03-19 20:55:35 -070069 if (win == 0x0000 || win == 0xf000)
70 return NULL;
71
72 if (extend_mmcfg(1) == -1)
73 return NULL;
74
75 fill_one_mmcfg(win << 16, 0, 0, 255);
Olivier Galibert9358c692007-02-13 13:26:20 +010076
77 return "Intel Corporation E7520 Memory Controller Hub";
78}
79
OGAWA Hirofumi429d5122007-02-13 13:26:20 +010080static const char __init *pci_mmcfg_intel_945(void)
Olivier Galibert9358c692007-02-13 13:26:20 +010081{
82 u32 pciexbar, mask = 0, len = 0;
83
Yinghai Lubb63b422008-02-28 23:56:50 -080084 raw_pci_ops->read(0, 0, PCI_DEVFN(0, 0), 0x48, 4, &pciexbar);
Olivier Galibert9358c692007-02-13 13:26:20 +010085
86 /* Enable bit */
87 if (!(pciexbar & 1))
Yinghai Lu068258b2009-03-19 20:55:35 -070088 return NULL;
Olivier Galibert9358c692007-02-13 13:26:20 +010089
90 /* Size bits */
91 switch ((pciexbar >> 1) & 3) {
92 case 0:
93 mask = 0xf0000000U;
94 len = 0x10000000U;
95 break;
96 case 1:
97 mask = 0xf8000000U;
98 len = 0x08000000U;
99 break;
100 case 2:
101 mask = 0xfc000000U;
102 len = 0x04000000U;
103 break;
104 default:
Yinghai Lu068258b2009-03-19 20:55:35 -0700105 return NULL;
Olivier Galibert9358c692007-02-13 13:26:20 +0100106 }
107
108 /* Errata #2, things break when not aligned on a 256Mb boundary */
109 /* Can only happen in 64M/128M mode */
110
111 if ((pciexbar & mask) & 0x0fffffffU)
Yinghai Lu068258b2009-03-19 20:55:35 -0700112 return NULL;
Olivier Galibert9358c692007-02-13 13:26:20 +0100113
Olivier Galibertb5229db2007-05-02 19:27:22 +0200114 /* Don't hit the APIC registers and their friends */
115 if ((pciexbar & mask) >= 0xf0000000U)
Yinghai Lu068258b2009-03-19 20:55:35 -0700116 return NULL;
Olivier Galibertb5229db2007-05-02 19:27:22 +0200117
Yinghai Lu068258b2009-03-19 20:55:35 -0700118 if (extend_mmcfg(1) == -1)
119 return NULL;
120
121 fill_one_mmcfg(pciexbar & mask, 0, 0, (len >> 20) - 1);
Olivier Galibert9358c692007-02-13 13:26:20 +0100122
123 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;
131 unsigned segnbits = 0, busnbits;
132
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
Yinghai Lu068258b2009-03-19 20:55:35 -0700165 if (extend_mmcfg(1 << segnbits) == -1)
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800166 return NULL;
167
Yinghai Lu068258b2009-03-19 20:55:35 -0700168 for (i = 0; i < (1 << segnbits); i++)
169 fill_one_mmcfg(base + (1<<28) * i, i, 0, (1 << busnbits) - 1);
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800170
171 return "AMD Family 10h NB";
172}
173
Ed Swierk5546d6f2009-03-19 20:57:56 -0700174static bool __initdata mcp55_checked;
175static const char __init *pci_mmcfg_nvidia_mcp55(void)
176{
177 int bus;
178 int mcp55_mmconf_found = 0;
179
180 static const u32 extcfg_regnum = 0x90;
181 static const u32 extcfg_regsize = 4;
182 static const u32 extcfg_enable_mask = 1<<31;
183 static const u32 extcfg_start_mask = 0xff<<16;
184 static const int extcfg_start_shift = 16;
185 static const u32 extcfg_size_mask = 0x3<<28;
186 static const int extcfg_size_shift = 28;
187 static const int extcfg_sizebus[] = {0x100, 0x80, 0x40, 0x20};
188 static const u32 extcfg_base_mask[] = {0x7ff8, 0x7ffc, 0x7ffe, 0x7fff};
189 static const int extcfg_base_lshift = 25;
190
191 /*
192 * do check if amd fam10h already took over
193 */
194 if (!acpi_disabled || pci_mmcfg_config_num || mcp55_checked)
195 return NULL;
196
197 mcp55_checked = true;
198 for (bus = 0; bus < 256; bus++) {
199 u64 base;
200 u32 l, extcfg;
201 u16 vendor, device;
202 int start, size_index, end;
203
204 raw_pci_ops->read(0, bus, PCI_DEVFN(0, 0), 0, 4, &l);
205 vendor = l & 0xffff;
206 device = (l >> 16) & 0xffff;
207
208 if (PCI_VENDOR_ID_NVIDIA != vendor || 0x0369 != device)
209 continue;
210
211 raw_pci_ops->read(0, bus, PCI_DEVFN(0, 0), extcfg_regnum,
212 extcfg_regsize, &extcfg);
213
214 if (!(extcfg & extcfg_enable_mask))
215 continue;
216
217 if (extend_mmcfg(1) == -1)
218 continue;
219
220 size_index = (extcfg & extcfg_size_mask) >> extcfg_size_shift;
221 base = extcfg & extcfg_base_mask[size_index];
222 /* base could > 4G */
223 base <<= extcfg_base_lshift;
224 start = (extcfg & extcfg_start_mask) >> extcfg_start_shift;
225 end = start + extcfg_sizebus[size_index] - 1;
226 fill_one_mmcfg(base, 0, start, end);
227 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
Olivier Galibert9358c692007-02-13 13:26:20 +0100310 pci_mmcfg_config_num = 0;
311 pci_mmcfg_config = NULL;
Olivier Galibert9358c692007-02-13 13:26:20 +0100312
Yinghai Lu068258b2009-03-19 20:55:35 -0700313 for (i = 0; i < ARRAY_SIZE(pci_mmcfg_probes); i++) {
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800314 bus = pci_mmcfg_probes[i].bus;
315 devfn = pci_mmcfg_probes[i].devfn;
Yinghai Lubb63b422008-02-28 23:56:50 -0800316 raw_pci_ops->read(0, bus, devfn, 0, 4, &l);
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800317 vendor = l & 0xffff;
318 device = (l >> 16) & 0xffff;
319
Yinghai Lu068258b2009-03-19 20:55:35 -0700320 name = NULL;
OGAWA Hirofumi429d5122007-02-13 13:26:20 +0100321 if (pci_mmcfg_probes[i].vendor == vendor &&
322 pci_mmcfg_probes[i].device == device)
Olivier Galibert9358c692007-02-13 13:26:20 +0100323 name = pci_mmcfg_probes[i].probe();
Yinghai Lu068258b2009-03-19 20:55:35 -0700324
325 if (name)
326 printk(KERN_INFO "PCI: Found %s with MMCONFIG support.\n",
327 name);
OGAWA Hirofumi429d5122007-02-13 13:26:20 +0100328 }
Olivier Galibert9358c692007-02-13 13:26:20 +0100329
Yinghai Lu068258b2009-03-19 20:55:35 -0700330 /* some end_bus_number is crazy, fix it */
331 pci_mmcfg_check_end_bus_number();
Olivier Galibert9358c692007-02-13 13:26:20 +0100332
Yinghai Lu068258b2009-03-19 20:55:35 -0700333 return pci_mmcfg_config_num != 0;
Olivier Galibert9358c692007-02-13 13:26:20 +0100334}
335
Yinghai Luebd60cd2008-09-04 21:04:32 +0200336static void __init pci_mmcfg_insert_resources(void)
Olivier Galibert6a0668f2007-02-13 13:26:20 +0100337{
Yinghai Lu068258b2009-03-19 20:55:35 -0700338#define PCI_MMCFG_RESOURCE_NAME_LEN 24
Olivier Galibert6a0668f2007-02-13 13:26:20 +0100339 int i;
340 struct resource *res;
341 char *names;
342 unsigned num_buses;
343
344 res = kcalloc(PCI_MMCFG_RESOURCE_NAME_LEN + sizeof(*res),
345 pci_mmcfg_config_num, GFP_KERNEL);
Olivier Galibert6a0668f2007-02-13 13:26:20 +0100346 if (!res) {
347 printk(KERN_ERR "PCI: Unable to allocate MMCONFIG resources\n");
348 return;
349 }
350
351 names = (void *)&res[pci_mmcfg_config_num];
352 for (i = 0; i < pci_mmcfg_config_num; i++, res++) {
OGAWA Hirofumi429d5122007-02-13 13:26:20 +0100353 struct acpi_mcfg_allocation *cfg = &pci_mmcfg_config[i];
354 num_buses = cfg->end_bus_number - cfg->start_bus_number + 1;
Olivier Galibert6a0668f2007-02-13 13:26:20 +0100355 res->name = names;
Yinghai Lu068258b2009-03-19 20:55:35 -0700356 snprintf(names, PCI_MMCFG_RESOURCE_NAME_LEN,
357 "PCI MMCONFIG %u [%02x-%02x]", cfg->pci_segment,
358 cfg->start_bus_number, cfg->end_bus_number);
359 res->start = cfg->address + (cfg->start_bus_number << 20);
Olivier Galibert6a0668f2007-02-13 13:26:20 +0100360 res->end = res->start + (num_buses << 20) - 1;
Yinghai Luebd60cd2008-09-04 21:04:32 +0200361 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
Olivier Galibert6a0668f2007-02-13 13:26:20 +0100362 insert_resource(&iomem_resource, res);
363 names += PCI_MMCFG_RESOURCE_NAME_LEN;
364 }
Aaron Durbina5ba7972007-07-21 17:10:34 +0200365
366 /* Mark that the resources have been inserted. */
367 pci_mmcfg_resources_inserted = 1;
Olivier Galibert6a0668f2007-02-13 13:26:20 +0100368}
369
Robert Hancock7752d5c2008-02-15 01:27:20 -0800370static acpi_status __init check_mcfg_resource(struct acpi_resource *res,
371 void *data)
372{
373 struct resource *mcfg_res = data;
374 struct acpi_resource_address64 address;
375 acpi_status status;
376
377 if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
378 struct acpi_resource_fixed_memory32 *fixmem32 =
379 &res->data.fixed_memory32;
380 if (!fixmem32)
381 return AE_OK;
382 if ((mcfg_res->start >= fixmem32->address) &&
Yinghai Lu75e613c2009-06-03 00:13:13 -0700383 (mcfg_res->end < (fixmem32->address +
Robert Hancock7752d5c2008-02-15 01:27:20 -0800384 fixmem32->address_length))) {
385 mcfg_res->flags = 1;
386 return AE_CTRL_TERMINATE;
387 }
388 }
389 if ((res->type != ACPI_RESOURCE_TYPE_ADDRESS32) &&
390 (res->type != ACPI_RESOURCE_TYPE_ADDRESS64))
391 return AE_OK;
392
393 status = acpi_resource_to_address64(res, &address);
394 if (ACPI_FAILURE(status) ||
395 (address.address_length <= 0) ||
396 (address.resource_type != ACPI_MEMORY_RANGE))
397 return AE_OK;
398
399 if ((mcfg_res->start >= address.minimum) &&
Yinghai Lu75e613c2009-06-03 00:13:13 -0700400 (mcfg_res->end < (address.minimum + address.address_length))) {
Robert Hancock7752d5c2008-02-15 01:27:20 -0800401 mcfg_res->flags = 1;
402 return AE_CTRL_TERMINATE;
403 }
404 return AE_OK;
405}
406
407static acpi_status __init find_mboard_resource(acpi_handle handle, u32 lvl,
408 void *context, void **rv)
409{
410 struct resource *mcfg_res = context;
411
412 acpi_walk_resources(handle, METHOD_NAME__CRS,
413 check_mcfg_resource, context);
414
415 if (mcfg_res->flags)
416 return AE_CTRL_TERMINATE;
417
418 return AE_OK;
419}
420
Yinghai Lua83fe322008-07-18 13:22:36 -0700421static int __init is_acpi_reserved(u64 start, u64 end, unsigned not_used)
Robert Hancock7752d5c2008-02-15 01:27:20 -0800422{
423 struct resource mcfg_res;
424
425 mcfg_res.start = start;
Yinghai Lu75e613c2009-06-03 00:13:13 -0700426 mcfg_res.end = end - 1;
Robert Hancock7752d5c2008-02-15 01:27:20 -0800427 mcfg_res.flags = 0;
428
429 acpi_get_devices("PNP0C01", find_mboard_resource, &mcfg_res, NULL);
430
431 if (!mcfg_res.flags)
432 acpi_get_devices("PNP0C02", find_mboard_resource, &mcfg_res,
433 NULL);
434
435 return mcfg_res.flags;
436}
437
Yinghai Lua83fe322008-07-18 13:22:36 -0700438typedef int (*check_reserved_t)(u64 start, u64 end, unsigned type);
439
440static int __init is_mmconf_reserved(check_reserved_t is_reserved,
441 u64 addr, u64 size, int i,
442 typeof(pci_mmcfg_config[0]) *cfg, int with_e820)
443{
444 u64 old_size = size;
445 int valid = 0;
446
Yinghai Lu044cd802009-04-18 01:43:46 -0700447 while (!is_reserved(addr, addr + size, E820_RESERVED)) {
Yinghai Lua83fe322008-07-18 13:22:36 -0700448 size >>= 1;
449 if (size < (16UL<<20))
450 break;
451 }
452
453 if (size >= (16UL<<20) || size == old_size) {
454 printk(KERN_NOTICE
455 "PCI: MCFG area at %Lx reserved in %s\n",
456 addr, with_e820?"E820":"ACPI motherboard resources");
457 valid = 1;
458
459 if (old_size != size) {
460 /* update end_bus_number */
461 cfg->end_bus_number = cfg->start_bus_number + ((size>>20) - 1);
462 printk(KERN_NOTICE "PCI: updated MCFG configuration %d: base %lx "
463 "segment %hu buses %u - %u\n",
464 i, (unsigned long)cfg->address, cfg->pci_segment,
465 (unsigned int)cfg->start_bus_number,
466 (unsigned int)cfg->end_bus_number);
467 }
468 }
469
470 return valid;
471}
472
Yinghai Lubb63b422008-02-28 23:56:50 -0800473static void __init pci_mmcfg_reject_broken(int early)
OGAWA Hirofumi44de0202007-02-13 13:26:20 +0100474{
OGAWA Hirofumi26054ed2007-02-13 13:26:20 +0100475 typeof(pci_mmcfg_config[0]) *cfg;
Robert Hancock7752d5c2008-02-15 01:27:20 -0800476 int i;
OGAWA Hirofumi26054ed2007-02-13 13:26:20 +0100477
478 if ((pci_mmcfg_config_num == 0) ||
479 (pci_mmcfg_config == NULL) ||
480 (pci_mmcfg_config[0].address == 0))
481 return;
482
Robert Hancock7752d5c2008-02-15 01:27:20 -0800483 for (i = 0; i < pci_mmcfg_config_num; i++) {
Yinghai Lu05c58b82008-02-15 01:30:14 -0800484 int valid = 0;
Yinghai Lua83fe322008-07-18 13:22:36 -0700485 u64 addr, size;
486
Robert Hancock7752d5c2008-02-15 01:27:20 -0800487 cfg = &pci_mmcfg_config[i];
Yinghai Lua83fe322008-07-18 13:22:36 -0700488 addr = cfg->start_bus_number;
489 addr <<= 20;
490 addr += cfg->address;
491 size = cfg->end_bus_number + 1 - cfg->start_bus_number;
492 size <<= 20;
Yinghai Lu05c58b82008-02-15 01:30:14 -0800493 printk(KERN_NOTICE "PCI: MCFG configuration %d: base %lx "
Robert Hancock7752d5c2008-02-15 01:27:20 -0800494 "segment %hu buses %u - %u\n",
495 i, (unsigned long)cfg->address, cfg->pci_segment,
496 (unsigned int)cfg->start_bus_number,
497 (unsigned int)cfg->end_bus_number);
Yinghai Lu05c58b82008-02-15 01:30:14 -0800498
Feng Tang5f0db7a2009-08-14 15:37:50 -0400499 if (!early && !acpi_disabled)
Yinghai Lua83fe322008-07-18 13:22:36 -0700500 valid = is_mmconf_reserved(is_acpi_reserved, addr, size, i, cfg, 0);
Yinghai Lu05c58b82008-02-15 01:30:14 -0800501
502 if (valid)
503 continue;
504
505 if (!early)
Robert Hancock7752d5c2008-02-15 01:27:20 -0800506 printk(KERN_ERR "PCI: BIOS Bug: MCFG area at %Lx is not"
507 " reserved in ACPI motherboard resources\n",
508 cfg->address);
Yinghai Lua83fe322008-07-18 13:22:36 -0700509
Yinghai Lu05c58b82008-02-15 01:30:14 -0800510 /* Don't try to do this check unless configuration
Yinghai Lubb63b422008-02-28 23:56:50 -0800511 type 1 is available. how about type 2 ?*/
Yinghai Lua83fe322008-07-18 13:22:36 -0700512 if (raw_pci_ops)
513 valid = is_mmconf_reserved(e820_all_mapped, addr, size, i, cfg, 1);
Yinghai Lu05c58b82008-02-15 01:30:14 -0800514
515 if (!valid)
516 goto reject;
OGAWA Hirofumi26054ed2007-02-13 13:26:20 +0100517 }
Robert Hancock7752d5c2008-02-15 01:27:20 -0800518
OGAWA Hirofumi26054ed2007-02-13 13:26:20 +0100519 return;
520
521reject:
Dave Jonesef310232008-08-14 15:07:03 -0400522 printk(KERN_INFO "PCI: Not using MMCONFIG.\n");
Yinghai Lu0b64ad72008-02-15 01:28:41 -0800523 pci_mmcfg_arch_free();
OGAWA Hirofumi26054ed2007-02-13 13:26:20 +0100524 kfree(pci_mmcfg_config);
525 pci_mmcfg_config = NULL;
526 pci_mmcfg_config_num = 0;
OGAWA Hirofumi44de0202007-02-13 13:26:20 +0100527}
528
Yinghai Lu05c58b82008-02-15 01:30:14 -0800529static int __initdata known_bridge;
530
Len Brownc4bf2f32009-06-11 23:53:55 -0400531/* The physical address of the MMCONFIG aperture. Set from ACPI tables. */
532struct acpi_mcfg_allocation *pci_mmcfg_config;
533int pci_mmcfg_config_num;
534
Bjorn Helgaas9a08f7d2009-10-23 15:20:33 -0600535static int __init acpi_mcfg_check_entry(struct acpi_table_mcfg *mcfg,
536 struct acpi_mcfg_allocation *cfg)
Len Brownc4bf2f32009-06-11 23:53:55 -0400537{
Bjorn Helgaas9a08f7d2009-10-23 15:20:33 -0600538 int year;
Len Brownc4bf2f32009-06-11 23:53:55 -0400539
Bjorn Helgaas9a08f7d2009-10-23 15:20:33 -0600540 if (cfg->address < 0xFFFFFFFF)
541 return 0;
542
543 if (!strcmp(mcfg->header.oem_id, "SGI"))
544 return 0;
545
546 if (mcfg->header.revision >= 1) {
547 if (dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL) &&
548 year >= 2010)
549 return 0;
550 }
551
552 printk(KERN_ERR PREFIX "MCFG region for %04x:%02x-%02x at %#llx "
553 "is above 4GB, ignored\n", cfg->pci_segment,
554 cfg->start_bus_number, cfg->end_bus_number, cfg->address);
555 return -EINVAL;
Len Brownc4bf2f32009-06-11 23:53:55 -0400556}
557
558static int __init pci_parse_mcfg(struct acpi_table_header *header)
559{
560 struct acpi_table_mcfg *mcfg;
561 unsigned long i;
562 int config_size;
563
564 if (!header)
565 return -EINVAL;
566
567 mcfg = (struct acpi_table_mcfg *)header;
568
569 /* how many config structures do we have */
570 pci_mmcfg_config_num = 0;
571 i = header->length - sizeof(struct acpi_table_mcfg);
572 while (i >= sizeof(struct acpi_mcfg_allocation)) {
573 ++pci_mmcfg_config_num;
574 i -= sizeof(struct acpi_mcfg_allocation);
575 };
576 if (pci_mmcfg_config_num == 0) {
577 printk(KERN_ERR PREFIX "MMCONFIG has no entries\n");
578 return -ENODEV;
579 }
580
581 config_size = pci_mmcfg_config_num * sizeof(*pci_mmcfg_config);
582 pci_mmcfg_config = kmalloc(config_size, GFP_KERNEL);
583 if (!pci_mmcfg_config) {
584 printk(KERN_WARNING PREFIX
585 "No memory for MCFG config tables\n");
586 return -ENOMEM;
587 }
588
589 memcpy(pci_mmcfg_config, &mcfg[1], config_size);
590
Len Brownc4bf2f32009-06-11 23:53:55 -0400591 for (i = 0; i < pci_mmcfg_config_num; ++i) {
Bjorn Helgaas9a08f7d2009-10-23 15:20:33 -0600592 if (acpi_mcfg_check_entry(mcfg, &pci_mmcfg_config[i])) {
Len Brownc4bf2f32009-06-11 23:53:55 -0400593 kfree(pci_mmcfg_config);
594 pci_mmcfg_config_num = 0;
595 return -ENODEV;
596 }
597 }
598
599 return 0;
600}
601
Thomas Gleixner968cbfa2008-05-12 15:43:37 +0200602static void __init __pci_mmcfg_init(int early)
Olivier Galibertb7867392007-02-13 13:26:20 +0100603{
Robert Hancock7752d5c2008-02-15 01:27:20 -0800604 /* MMCONFIG disabled */
605 if ((pci_probe & PCI_PROBE_MMCONF) == 0)
606 return;
607
608 /* MMCONFIG already enabled */
Yinghai Lu05c58b82008-02-15 01:30:14 -0800609 if (!early && !(pci_probe & PCI_PROBE_MASK & ~PCI_PROBE_MMCONF))
Robert Hancock7752d5c2008-02-15 01:27:20 -0800610 return;
611
Yinghai Lu05c58b82008-02-15 01:30:14 -0800612 /* for late to exit */
613 if (known_bridge)
614 return;
Robert Hancock7752d5c2008-02-15 01:27:20 -0800615
Yinghai Lubb63b422008-02-28 23:56:50 -0800616 if (early) {
Yinghai Lu05c58b82008-02-15 01:30:14 -0800617 if (pci_mmcfg_check_hostbridge())
618 known_bridge = 1;
619 }
620
Yinghai Lu068258b2009-03-19 20:55:35 -0700621 if (!known_bridge)
Feng Tang5f0db7a2009-08-14 15:37:50 -0400622 acpi_sfi_table_parse(ACPI_SIG_MCFG, pci_parse_mcfg);
Yinghai Lu068258b2009-03-19 20:55:35 -0700623
624 pci_mmcfg_reject_broken(early);
Olivier Galibertb7867392007-02-13 13:26:20 +0100625
626 if ((pci_mmcfg_config_num == 0) ||
627 (pci_mmcfg_config == NULL) ||
628 (pci_mmcfg_config[0].address == 0))
629 return;
630
Yinghai Luebd60cd2008-09-04 21:04:32 +0200631 if (pci_mmcfg_arch_init())
Olivier Galibertb7867392007-02-13 13:26:20 +0100632 pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
Yinghai Luebd60cd2008-09-04 21:04:32 +0200633 else {
Aaron Durbina5ba7972007-07-21 17:10:34 +0200634 /*
635 * Signal not to attempt to insert mmcfg resources because
636 * the architecture mmcfg setup could not initialize.
637 */
638 pci_mmcfg_resources_inserted = 1;
Olivier Galibertb7867392007-02-13 13:26:20 +0100639 }
640}
Aaron Durbina5ba7972007-07-21 17:10:34 +0200641
Yinghai Lubb63b422008-02-28 23:56:50 -0800642void __init pci_mmcfg_early_init(void)
Yinghai Lu05c58b82008-02-15 01:30:14 -0800643{
Yinghai Lubb63b422008-02-28 23:56:50 -0800644 __pci_mmcfg_init(1);
Yinghai Lu05c58b82008-02-15 01:30:14 -0800645}
646
647void __init pci_mmcfg_late_init(void)
648{
Yinghai Lubb63b422008-02-28 23:56:50 -0800649 __pci_mmcfg_init(0);
Yinghai Lu05c58b82008-02-15 01:30:14 -0800650}
651
Aaron Durbina5ba7972007-07-21 17:10:34 +0200652static int __init pci_mmcfg_late_insert_resources(void)
653{
654 /*
655 * If resources are already inserted or we are not using MMCONFIG,
656 * don't insert the resources.
657 */
658 if ((pci_mmcfg_resources_inserted == 1) ||
659 (pci_probe & PCI_PROBE_MMCONF) == 0 ||
660 (pci_mmcfg_config_num == 0) ||
661 (pci_mmcfg_config == NULL) ||
662 (pci_mmcfg_config[0].address == 0))
663 return 1;
664
665 /*
666 * Attempt to insert the mmcfg resources but not with the busy flag
667 * marked so it won't cause request errors when __request_region is
668 * called.
669 */
Yinghai Luebd60cd2008-09-04 21:04:32 +0200670 pci_mmcfg_insert_resources();
Aaron Durbina5ba7972007-07-21 17:10:34 +0200671
672 return 0;
673}
674
675/*
676 * Perform MMCONFIG resource insertion after PCI initialization to allow for
677 * misprogrammed MCFG tables that state larger sizes but actually conflict
678 * with other system resources.
679 */
680late_initcall(pci_mmcfg_late_insert_resources);