blob: 7a7b6ba3abbbd8018671fa0c432cbdae4ecc94f7 [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
Yinghai Lu068258b2009-03-19 20:55:35 -070029static __init int extend_mmcfg(int num)
30{
31 struct acpi_mcfg_allocation *new;
32 int new_num = pci_mmcfg_config_num + num;
33
34 new = kzalloc(sizeof(pci_mmcfg_config[0]) * new_num, GFP_KERNEL);
35 if (!new)
36 return -1;
37
38 if (pci_mmcfg_config) {
39 memcpy(new, pci_mmcfg_config,
40 sizeof(pci_mmcfg_config[0]) * new_num);
41 kfree(pci_mmcfg_config);
42 }
43 pci_mmcfg_config = new;
44
45 return 0;
46}
47
48static __init void fill_one_mmcfg(u64 addr, int segment, int start, int end)
49{
50 int i = pci_mmcfg_config_num;
51
52 pci_mmcfg_config_num++;
53 pci_mmcfg_config[i].address = addr;
54 pci_mmcfg_config[i].pci_segment = segment;
55 pci_mmcfg_config[i].start_bus_number = start;
56 pci_mmcfg_config[i].end_bus_number = end;
57}
58
OGAWA Hirofumi429d5122007-02-13 13:26:20 +010059static const char __init *pci_mmcfg_e7520(void)
Olivier Galibert9358c692007-02-13 13:26:20 +010060{
61 u32 win;
Yinghai Lubb63b422008-02-28 23:56:50 -080062 raw_pci_ops->read(0, 0, PCI_DEVFN(0, 0), 0xce, 2, &win);
Olivier Galibert9358c692007-02-13 13:26:20 +010063
Olivier Galibertb5229db2007-05-02 19:27:22 +020064 win = win & 0xf000;
Yinghai Lu068258b2009-03-19 20:55:35 -070065 if (win == 0x0000 || win == 0xf000)
66 return NULL;
67
68 if (extend_mmcfg(1) == -1)
69 return NULL;
70
71 fill_one_mmcfg(win << 16, 0, 0, 255);
Olivier Galibert9358c692007-02-13 13:26:20 +010072
73 return "Intel Corporation E7520 Memory Controller Hub";
74}
75
OGAWA Hirofumi429d5122007-02-13 13:26:20 +010076static const char __init *pci_mmcfg_intel_945(void)
Olivier Galibert9358c692007-02-13 13:26:20 +010077{
78 u32 pciexbar, mask = 0, len = 0;
79
Yinghai Lubb63b422008-02-28 23:56:50 -080080 raw_pci_ops->read(0, 0, PCI_DEVFN(0, 0), 0x48, 4, &pciexbar);
Olivier Galibert9358c692007-02-13 13:26:20 +010081
82 /* Enable bit */
83 if (!(pciexbar & 1))
Yinghai Lu068258b2009-03-19 20:55:35 -070084 return NULL;
Olivier Galibert9358c692007-02-13 13:26:20 +010085
86 /* Size bits */
87 switch ((pciexbar >> 1) & 3) {
88 case 0:
89 mask = 0xf0000000U;
90 len = 0x10000000U;
91 break;
92 case 1:
93 mask = 0xf8000000U;
94 len = 0x08000000U;
95 break;
96 case 2:
97 mask = 0xfc000000U;
98 len = 0x04000000U;
99 break;
100 default:
Yinghai Lu068258b2009-03-19 20:55:35 -0700101 return NULL;
Olivier Galibert9358c692007-02-13 13:26:20 +0100102 }
103
104 /* Errata #2, things break when not aligned on a 256Mb boundary */
105 /* Can only happen in 64M/128M mode */
106
107 if ((pciexbar & mask) & 0x0fffffffU)
Yinghai Lu068258b2009-03-19 20:55:35 -0700108 return NULL;
Olivier Galibert9358c692007-02-13 13:26:20 +0100109
Olivier Galibertb5229db2007-05-02 19:27:22 +0200110 /* Don't hit the APIC registers and their friends */
111 if ((pciexbar & mask) >= 0xf0000000U)
Yinghai Lu068258b2009-03-19 20:55:35 -0700112 return NULL;
Olivier Galibertb5229db2007-05-02 19:27:22 +0200113
Yinghai Lu068258b2009-03-19 20:55:35 -0700114 if (extend_mmcfg(1) == -1)
115 return NULL;
116
117 fill_one_mmcfg(pciexbar & mask, 0, 0, (len >> 20) - 1);
Olivier Galibert9358c692007-02-13 13:26:20 +0100118
119 return "Intel Corporation 945G/GZ/P/PL Express Memory Controller Hub";
120}
121
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800122static const char __init *pci_mmcfg_amd_fam10h(void)
123{
124 u32 low, high, address;
125 u64 base, msr;
126 int i;
127 unsigned segnbits = 0, busnbits;
128
Yinghai Lu5f0b2972008-04-14 16:08:25 -0700129 if (!(pci_probe & PCI_CHECK_ENABLE_AMD_MMCONF))
130 return NULL;
131
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800132 address = MSR_FAM10H_MMIO_CONF_BASE;
133 if (rdmsr_safe(address, &low, &high))
134 return NULL;
135
136 msr = high;
137 msr <<= 32;
138 msr |= low;
139
140 /* mmconfig is not enable */
141 if (!(msr & FAM10H_MMIO_CONF_ENABLE))
142 return NULL;
143
144 base = msr & (FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT);
145
146 busnbits = (msr >> FAM10H_MMIO_CONF_BUSRANGE_SHIFT) &
147 FAM10H_MMIO_CONF_BUSRANGE_MASK;
148
149 /*
150 * only handle bus 0 ?
151 * need to skip it
152 */
153 if (!busnbits)
154 return NULL;
155
156 if (busnbits > 8) {
157 segnbits = busnbits - 8;
158 busnbits = 8;
159 }
160
Yinghai Lu068258b2009-03-19 20:55:35 -0700161 if (extend_mmcfg(1 << segnbits) == -1)
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800162 return NULL;
163
Yinghai Lu068258b2009-03-19 20:55:35 -0700164 for (i = 0; i < (1 << segnbits); i++)
165 fill_one_mmcfg(base + (1<<28) * i, i, 0, (1 << busnbits) - 1);
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800166
167 return "AMD Family 10h NB";
168}
169
Ed Swierk5546d6f2009-03-19 20:57:56 -0700170static bool __initdata mcp55_checked;
171static const char __init *pci_mmcfg_nvidia_mcp55(void)
172{
173 int bus;
174 int mcp55_mmconf_found = 0;
175
176 static const u32 extcfg_regnum = 0x90;
177 static const u32 extcfg_regsize = 4;
178 static const u32 extcfg_enable_mask = 1<<31;
179 static const u32 extcfg_start_mask = 0xff<<16;
180 static const int extcfg_start_shift = 16;
181 static const u32 extcfg_size_mask = 0x3<<28;
182 static const int extcfg_size_shift = 28;
183 static const int extcfg_sizebus[] = {0x100, 0x80, 0x40, 0x20};
184 static const u32 extcfg_base_mask[] = {0x7ff8, 0x7ffc, 0x7ffe, 0x7fff};
185 static const int extcfg_base_lshift = 25;
186
187 /*
188 * do check if amd fam10h already took over
189 */
190 if (!acpi_disabled || pci_mmcfg_config_num || mcp55_checked)
191 return NULL;
192
193 mcp55_checked = true;
194 for (bus = 0; bus < 256; bus++) {
195 u64 base;
196 u32 l, extcfg;
197 u16 vendor, device;
198 int start, size_index, end;
199
200 raw_pci_ops->read(0, bus, PCI_DEVFN(0, 0), 0, 4, &l);
201 vendor = l & 0xffff;
202 device = (l >> 16) & 0xffff;
203
204 if (PCI_VENDOR_ID_NVIDIA != vendor || 0x0369 != device)
205 continue;
206
207 raw_pci_ops->read(0, bus, PCI_DEVFN(0, 0), extcfg_regnum,
208 extcfg_regsize, &extcfg);
209
210 if (!(extcfg & extcfg_enable_mask))
211 continue;
212
213 if (extend_mmcfg(1) == -1)
214 continue;
215
216 size_index = (extcfg & extcfg_size_mask) >> extcfg_size_shift;
217 base = extcfg & extcfg_base_mask[size_index];
218 /* base could > 4G */
219 base <<= extcfg_base_lshift;
220 start = (extcfg & extcfg_start_mask) >> extcfg_start_shift;
221 end = start + extcfg_sizebus[size_index] - 1;
222 fill_one_mmcfg(base, 0, start, end);
223 mcp55_mmconf_found++;
224 }
225
226 if (!mcp55_mmconf_found)
227 return NULL;
228
229 return "nVidia MCP55";
230}
231
Olivier Galibert9358c692007-02-13 13:26:20 +0100232struct pci_mmcfg_hostbridge_probe {
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800233 u32 bus;
234 u32 devfn;
Olivier Galibert9358c692007-02-13 13:26:20 +0100235 u32 vendor;
236 u32 device;
237 const char *(*probe)(void);
238};
239
OGAWA Hirofumi429d5122007-02-13 13:26:20 +0100240static struct pci_mmcfg_hostbridge_probe pci_mmcfg_probes[] __initdata = {
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800241 { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
242 PCI_DEVICE_ID_INTEL_E7520_MCH, pci_mmcfg_e7520 },
243 { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
244 PCI_DEVICE_ID_INTEL_82945G_HB, pci_mmcfg_intel_945 },
245 { 0, PCI_DEVFN(0x18, 0), PCI_VENDOR_ID_AMD,
246 0x1200, pci_mmcfg_amd_fam10h },
247 { 0xff, PCI_DEVFN(0, 0), PCI_VENDOR_ID_AMD,
248 0x1200, pci_mmcfg_amd_fam10h },
Ed Swierk5546d6f2009-03-19 20:57:56 -0700249 { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_NVIDIA,
250 0x0369, pci_mmcfg_nvidia_mcp55 },
Olivier Galibert9358c692007-02-13 13:26:20 +0100251};
252
Yinghai Lu068258b2009-03-19 20:55:35 -0700253static int __init cmp_mmcfg(const void *x1, const void *x2)
254{
255 const typeof(pci_mmcfg_config[0]) *m1 = x1;
256 const typeof(pci_mmcfg_config[0]) *m2 = x2;
257 int start1, start2;
258
259 start1 = m1->start_bus_number;
260 start2 = m2->start_bus_number;
261
262 return start1 - start2;
263}
264
265static void __init pci_mmcfg_check_end_bus_number(void)
266{
267 int i;
268 typeof(pci_mmcfg_config[0]) *cfg, *cfgx;
269
270 /* sort them at first */
271 sort(pci_mmcfg_config, pci_mmcfg_config_num,
272 sizeof(pci_mmcfg_config[0]), cmp_mmcfg, NULL);
273
274 /* last one*/
275 if (pci_mmcfg_config_num > 0) {
276 i = pci_mmcfg_config_num - 1;
277 cfg = &pci_mmcfg_config[i];
278 if (cfg->end_bus_number < cfg->start_bus_number)
279 cfg->end_bus_number = 255;
280 }
281
282 /* don't overlap please */
283 for (i = 0; i < pci_mmcfg_config_num - 1; i++) {
284 cfg = &pci_mmcfg_config[i];
285 cfgx = &pci_mmcfg_config[i+1];
286
287 if (cfg->end_bus_number < cfg->start_bus_number)
288 cfg->end_bus_number = 255;
289
290 if (cfg->end_bus_number >= cfgx->start_bus_number)
291 cfg->end_bus_number = cfgx->start_bus_number - 1;
292 }
293}
294
Olivier Galibert9358c692007-02-13 13:26:20 +0100295static int __init pci_mmcfg_check_hostbridge(void)
296{
297 u32 l;
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800298 u32 bus, devfn;
Olivier Galibert9358c692007-02-13 13:26:20 +0100299 u16 vendor, device;
300 int i;
301 const char *name;
302
Yinghai Lubb63b422008-02-28 23:56:50 -0800303 if (!raw_pci_ops)
304 return 0;
305
Olivier Galibert9358c692007-02-13 13:26:20 +0100306 pci_mmcfg_config_num = 0;
307 pci_mmcfg_config = NULL;
Olivier Galibert9358c692007-02-13 13:26:20 +0100308
Yinghai Lu068258b2009-03-19 20:55:35 -0700309 for (i = 0; i < ARRAY_SIZE(pci_mmcfg_probes); i++) {
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800310 bus = pci_mmcfg_probes[i].bus;
311 devfn = pci_mmcfg_probes[i].devfn;
Yinghai Lubb63b422008-02-28 23:56:50 -0800312 raw_pci_ops->read(0, bus, devfn, 0, 4, &l);
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800313 vendor = l & 0xffff;
314 device = (l >> 16) & 0xffff;
315
Yinghai Lu068258b2009-03-19 20:55:35 -0700316 name = NULL;
OGAWA Hirofumi429d5122007-02-13 13:26:20 +0100317 if (pci_mmcfg_probes[i].vendor == vendor &&
318 pci_mmcfg_probes[i].device == device)
Olivier Galibert9358c692007-02-13 13:26:20 +0100319 name = pci_mmcfg_probes[i].probe();
Yinghai Lu068258b2009-03-19 20:55:35 -0700320
321 if (name)
322 printk(KERN_INFO "PCI: Found %s with MMCONFIG support.\n",
323 name);
OGAWA Hirofumi429d5122007-02-13 13:26:20 +0100324 }
Olivier Galibert9358c692007-02-13 13:26:20 +0100325
Yinghai Lu068258b2009-03-19 20:55:35 -0700326 /* some end_bus_number is crazy, fix it */
327 pci_mmcfg_check_end_bus_number();
Olivier Galibert9358c692007-02-13 13:26:20 +0100328
Yinghai Lu068258b2009-03-19 20:55:35 -0700329 return pci_mmcfg_config_num != 0;
Olivier Galibert9358c692007-02-13 13:26:20 +0100330}
331
Yinghai Luebd60cd2008-09-04 21:04:32 +0200332static void __init pci_mmcfg_insert_resources(void)
Olivier Galibert6a0668f2007-02-13 13:26:20 +0100333{
Yinghai Lu068258b2009-03-19 20:55:35 -0700334#define PCI_MMCFG_RESOURCE_NAME_LEN 24
Olivier Galibert6a0668f2007-02-13 13:26:20 +0100335 int i;
336 struct resource *res;
337 char *names;
338 unsigned num_buses;
339
340 res = kcalloc(PCI_MMCFG_RESOURCE_NAME_LEN + sizeof(*res),
341 pci_mmcfg_config_num, GFP_KERNEL);
Olivier Galibert6a0668f2007-02-13 13:26:20 +0100342 if (!res) {
343 printk(KERN_ERR "PCI: Unable to allocate MMCONFIG resources\n");
344 return;
345 }
346
347 names = (void *)&res[pci_mmcfg_config_num];
348 for (i = 0; i < pci_mmcfg_config_num; i++, res++) {
OGAWA Hirofumi429d5122007-02-13 13:26:20 +0100349 struct acpi_mcfg_allocation *cfg = &pci_mmcfg_config[i];
350 num_buses = cfg->end_bus_number - cfg->start_bus_number + 1;
Olivier Galibert6a0668f2007-02-13 13:26:20 +0100351 res->name = names;
Yinghai Lu068258b2009-03-19 20:55:35 -0700352 snprintf(names, PCI_MMCFG_RESOURCE_NAME_LEN,
353 "PCI MMCONFIG %u [%02x-%02x]", cfg->pci_segment,
354 cfg->start_bus_number, cfg->end_bus_number);
355 res->start = cfg->address + (cfg->start_bus_number << 20);
Olivier Galibert6a0668f2007-02-13 13:26:20 +0100356 res->end = res->start + (num_buses << 20) - 1;
Yinghai Luebd60cd2008-09-04 21:04:32 +0200357 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
Olivier Galibert6a0668f2007-02-13 13:26:20 +0100358 insert_resource(&iomem_resource, res);
359 names += PCI_MMCFG_RESOURCE_NAME_LEN;
360 }
Aaron Durbina5ba7972007-07-21 17:10:34 +0200361
362 /* Mark that the resources have been inserted. */
363 pci_mmcfg_resources_inserted = 1;
Olivier Galibert6a0668f2007-02-13 13:26:20 +0100364}
365
Robert Hancock7752d5c2008-02-15 01:27:20 -0800366static acpi_status __init check_mcfg_resource(struct acpi_resource *res,
367 void *data)
368{
369 struct resource *mcfg_res = data;
370 struct acpi_resource_address64 address;
371 acpi_status status;
372
373 if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
374 struct acpi_resource_fixed_memory32 *fixmem32 =
375 &res->data.fixed_memory32;
376 if (!fixmem32)
377 return AE_OK;
378 if ((mcfg_res->start >= fixmem32->address) &&
Yinghai Lu75e613c2009-06-03 00:13:13 -0700379 (mcfg_res->end < (fixmem32->address +
Robert Hancock7752d5c2008-02-15 01:27:20 -0800380 fixmem32->address_length))) {
381 mcfg_res->flags = 1;
382 return AE_CTRL_TERMINATE;
383 }
384 }
385 if ((res->type != ACPI_RESOURCE_TYPE_ADDRESS32) &&
386 (res->type != ACPI_RESOURCE_TYPE_ADDRESS64))
387 return AE_OK;
388
389 status = acpi_resource_to_address64(res, &address);
390 if (ACPI_FAILURE(status) ||
391 (address.address_length <= 0) ||
392 (address.resource_type != ACPI_MEMORY_RANGE))
393 return AE_OK;
394
395 if ((mcfg_res->start >= address.minimum) &&
Yinghai Lu75e613c2009-06-03 00:13:13 -0700396 (mcfg_res->end < (address.minimum + address.address_length))) {
Robert Hancock7752d5c2008-02-15 01:27:20 -0800397 mcfg_res->flags = 1;
398 return AE_CTRL_TERMINATE;
399 }
400 return AE_OK;
401}
402
403static acpi_status __init find_mboard_resource(acpi_handle handle, u32 lvl,
404 void *context, void **rv)
405{
406 struct resource *mcfg_res = context;
407
408 acpi_walk_resources(handle, METHOD_NAME__CRS,
409 check_mcfg_resource, context);
410
411 if (mcfg_res->flags)
412 return AE_CTRL_TERMINATE;
413
414 return AE_OK;
415}
416
Yinghai Lua83fe322008-07-18 13:22:36 -0700417static int __init is_acpi_reserved(u64 start, u64 end, unsigned not_used)
Robert Hancock7752d5c2008-02-15 01:27:20 -0800418{
419 struct resource mcfg_res;
420
421 mcfg_res.start = start;
Yinghai Lu75e613c2009-06-03 00:13:13 -0700422 mcfg_res.end = end - 1;
Robert Hancock7752d5c2008-02-15 01:27:20 -0800423 mcfg_res.flags = 0;
424
425 acpi_get_devices("PNP0C01", find_mboard_resource, &mcfg_res, NULL);
426
427 if (!mcfg_res.flags)
428 acpi_get_devices("PNP0C02", find_mboard_resource, &mcfg_res,
429 NULL);
430
431 return mcfg_res.flags;
432}
433
Yinghai Lua83fe322008-07-18 13:22:36 -0700434typedef int (*check_reserved_t)(u64 start, u64 end, unsigned type);
435
436static int __init is_mmconf_reserved(check_reserved_t is_reserved,
437 u64 addr, u64 size, int i,
438 typeof(pci_mmcfg_config[0]) *cfg, int with_e820)
439{
440 u64 old_size = size;
441 int valid = 0;
442
Yinghai Lu044cd802009-04-18 01:43:46 -0700443 while (!is_reserved(addr, addr + size, E820_RESERVED)) {
Yinghai Lua83fe322008-07-18 13:22:36 -0700444 size >>= 1;
445 if (size < (16UL<<20))
446 break;
447 }
448
449 if (size >= (16UL<<20) || size == old_size) {
450 printk(KERN_NOTICE
451 "PCI: MCFG area at %Lx reserved in %s\n",
452 addr, with_e820?"E820":"ACPI motherboard resources");
453 valid = 1;
454
455 if (old_size != size) {
456 /* update end_bus_number */
457 cfg->end_bus_number = cfg->start_bus_number + ((size>>20) - 1);
458 printk(KERN_NOTICE "PCI: updated MCFG configuration %d: base %lx "
459 "segment %hu buses %u - %u\n",
460 i, (unsigned long)cfg->address, cfg->pci_segment,
461 (unsigned int)cfg->start_bus_number,
462 (unsigned int)cfg->end_bus_number);
463 }
464 }
465
466 return valid;
467}
468
Yinghai Lubb63b422008-02-28 23:56:50 -0800469static void __init pci_mmcfg_reject_broken(int early)
OGAWA Hirofumi44de0202007-02-13 13:26:20 +0100470{
OGAWA Hirofumi26054ed2007-02-13 13:26:20 +0100471 typeof(pci_mmcfg_config[0]) *cfg;
Robert Hancock7752d5c2008-02-15 01:27:20 -0800472 int i;
OGAWA Hirofumi26054ed2007-02-13 13:26:20 +0100473
474 if ((pci_mmcfg_config_num == 0) ||
475 (pci_mmcfg_config == NULL) ||
476 (pci_mmcfg_config[0].address == 0))
477 return;
478
Robert Hancock7752d5c2008-02-15 01:27:20 -0800479 for (i = 0; i < pci_mmcfg_config_num; i++) {
Yinghai Lu05c58b82008-02-15 01:30:14 -0800480 int valid = 0;
Yinghai Lua83fe322008-07-18 13:22:36 -0700481 u64 addr, size;
482
Robert Hancock7752d5c2008-02-15 01:27:20 -0800483 cfg = &pci_mmcfg_config[i];
Yinghai Lua83fe322008-07-18 13:22:36 -0700484 addr = cfg->start_bus_number;
485 addr <<= 20;
486 addr += cfg->address;
487 size = cfg->end_bus_number + 1 - cfg->start_bus_number;
488 size <<= 20;
Yinghai Lu05c58b82008-02-15 01:30:14 -0800489 printk(KERN_NOTICE "PCI: MCFG configuration %d: base %lx "
Robert Hancock7752d5c2008-02-15 01:27:20 -0800490 "segment %hu buses %u - %u\n",
491 i, (unsigned long)cfg->address, cfg->pci_segment,
492 (unsigned int)cfg->start_bus_number,
493 (unsigned int)cfg->end_bus_number);
Yinghai Lu05c58b82008-02-15 01:30:14 -0800494
Feng Tang5f0db7a2009-08-14 15:37:50 -0400495 if (!early && !acpi_disabled)
Yinghai Lua83fe322008-07-18 13:22:36 -0700496 valid = is_mmconf_reserved(is_acpi_reserved, addr, size, i, cfg, 0);
Yinghai Lu05c58b82008-02-15 01:30:14 -0800497
498 if (valid)
499 continue;
500
501 if (!early)
Robert Hancock7752d5c2008-02-15 01:27:20 -0800502 printk(KERN_ERR "PCI: BIOS Bug: MCFG area at %Lx is not"
503 " reserved in ACPI motherboard resources\n",
504 cfg->address);
Yinghai Lua83fe322008-07-18 13:22:36 -0700505
Yinghai Lu05c58b82008-02-15 01:30:14 -0800506 /* Don't try to do this check unless configuration
Yinghai Lubb63b422008-02-28 23:56:50 -0800507 type 1 is available. how about type 2 ?*/
Yinghai Lua83fe322008-07-18 13:22:36 -0700508 if (raw_pci_ops)
509 valid = is_mmconf_reserved(e820_all_mapped, addr, size, i, cfg, 1);
Yinghai Lu05c58b82008-02-15 01:30:14 -0800510
511 if (!valid)
512 goto reject;
OGAWA Hirofumi26054ed2007-02-13 13:26:20 +0100513 }
Robert Hancock7752d5c2008-02-15 01:27:20 -0800514
OGAWA Hirofumi26054ed2007-02-13 13:26:20 +0100515 return;
516
517reject:
Dave Jonesef310232008-08-14 15:07:03 -0400518 printk(KERN_INFO "PCI: Not using MMCONFIG.\n");
Yinghai Lu0b64ad72008-02-15 01:28:41 -0800519 pci_mmcfg_arch_free();
OGAWA Hirofumi26054ed2007-02-13 13:26:20 +0100520 kfree(pci_mmcfg_config);
521 pci_mmcfg_config = NULL;
522 pci_mmcfg_config_num = 0;
OGAWA Hirofumi44de0202007-02-13 13:26:20 +0100523}
524
Yinghai Lu05c58b82008-02-15 01:30:14 -0800525static int __initdata known_bridge;
526
Len Brownc4bf2f32009-06-11 23:53:55 -0400527/* The physical address of the MMCONFIG aperture. Set from ACPI tables. */
528struct acpi_mcfg_allocation *pci_mmcfg_config;
529int pci_mmcfg_config_num;
530
Bjorn Helgaas9a08f7d2009-10-23 15:20:33 -0600531static int __init acpi_mcfg_check_entry(struct acpi_table_mcfg *mcfg,
532 struct acpi_mcfg_allocation *cfg)
Len Brownc4bf2f32009-06-11 23:53:55 -0400533{
Bjorn Helgaas9a08f7d2009-10-23 15:20:33 -0600534 int year;
Len Brownc4bf2f32009-06-11 23:53:55 -0400535
Bjorn Helgaas9a08f7d2009-10-23 15:20:33 -0600536 if (cfg->address < 0xFFFFFFFF)
537 return 0;
538
539 if (!strcmp(mcfg->header.oem_id, "SGI"))
540 return 0;
541
542 if (mcfg->header.revision >= 1) {
543 if (dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL) &&
544 year >= 2010)
545 return 0;
546 }
547
548 printk(KERN_ERR PREFIX "MCFG region for %04x:%02x-%02x at %#llx "
549 "is above 4GB, ignored\n", cfg->pci_segment,
550 cfg->start_bus_number, cfg->end_bus_number, cfg->address);
551 return -EINVAL;
Len Brownc4bf2f32009-06-11 23:53:55 -0400552}
553
554static int __init pci_parse_mcfg(struct acpi_table_header *header)
555{
556 struct acpi_table_mcfg *mcfg;
Bjorn Helgaasd3578ef2009-11-13 17:33:47 -0700557 struct acpi_mcfg_allocation *cfg_table, *cfg;
Len Brownc4bf2f32009-06-11 23:53:55 -0400558 unsigned long i;
Bjorn Helgaase823d6f2009-11-13 17:33:42 -0700559 int entries, config_size;
Len Brownc4bf2f32009-06-11 23:53:55 -0400560
561 if (!header)
562 return -EINVAL;
563
564 mcfg = (struct acpi_table_mcfg *)header;
565
566 /* how many config structures do we have */
567 pci_mmcfg_config_num = 0;
Bjorn Helgaase823d6f2009-11-13 17:33:42 -0700568 entries = 0;
Len Brownc4bf2f32009-06-11 23:53:55 -0400569 i = header->length - sizeof(struct acpi_table_mcfg);
570 while (i >= sizeof(struct acpi_mcfg_allocation)) {
Bjorn Helgaase823d6f2009-11-13 17:33:42 -0700571 entries++;
Len Brownc4bf2f32009-06-11 23:53:55 -0400572 i -= sizeof(struct acpi_mcfg_allocation);
573 };
Bjorn Helgaase823d6f2009-11-13 17:33:42 -0700574 if (entries == 0) {
Len Brownc4bf2f32009-06-11 23:53:55 -0400575 printk(KERN_ERR PREFIX "MMCONFIG has no entries\n");
576 return -ENODEV;
577 }
578
Bjorn Helgaase823d6f2009-11-13 17:33:42 -0700579 config_size = entries * sizeof(*pci_mmcfg_config);
Len Brownc4bf2f32009-06-11 23:53:55 -0400580 pci_mmcfg_config = kmalloc(config_size, GFP_KERNEL);
581 if (!pci_mmcfg_config) {
582 printk(KERN_WARNING PREFIX
583 "No memory for MCFG config tables\n");
584 return -ENOMEM;
585 }
586
587 memcpy(pci_mmcfg_config, &mcfg[1], config_size);
Bjorn Helgaase823d6f2009-11-13 17:33:42 -0700588 pci_mmcfg_config_num = entries;
Len Brownc4bf2f32009-06-11 23:53:55 -0400589
Bjorn Helgaasd3578ef2009-11-13 17:33:47 -0700590 cfg_table = (struct acpi_mcfg_allocation *) &mcfg[1];
Bjorn Helgaase823d6f2009-11-13 17:33:42 -0700591 for (i = 0; i < entries; i++) {
Bjorn Helgaasd3578ef2009-11-13 17:33:47 -0700592 cfg = &cfg_table[i];
593 if (acpi_mcfg_check_entry(mcfg, cfg)) {
Len Brownc4bf2f32009-06-11 23:53:55 -0400594 kfree(pci_mmcfg_config);
595 pci_mmcfg_config_num = 0;
596 return -ENODEV;
597 }
598 }
599
600 return 0;
601}
602
Thomas Gleixner968cbfa2008-05-12 15:43:37 +0200603static void __init __pci_mmcfg_init(int early)
Olivier Galibertb7867392007-02-13 13:26:20 +0100604{
Robert Hancock7752d5c2008-02-15 01:27:20 -0800605 /* MMCONFIG disabled */
606 if ((pci_probe & PCI_PROBE_MMCONF) == 0)
607 return;
608
609 /* MMCONFIG already enabled */
Yinghai Lu05c58b82008-02-15 01:30:14 -0800610 if (!early && !(pci_probe & PCI_PROBE_MASK & ~PCI_PROBE_MMCONF))
Robert Hancock7752d5c2008-02-15 01:27:20 -0800611 return;
612
Yinghai Lu05c58b82008-02-15 01:30:14 -0800613 /* for late to exit */
614 if (known_bridge)
615 return;
Robert Hancock7752d5c2008-02-15 01:27:20 -0800616
Yinghai Lubb63b422008-02-28 23:56:50 -0800617 if (early) {
Yinghai Lu05c58b82008-02-15 01:30:14 -0800618 if (pci_mmcfg_check_hostbridge())
619 known_bridge = 1;
620 }
621
Yinghai Lu068258b2009-03-19 20:55:35 -0700622 if (!known_bridge)
Feng Tang5f0db7a2009-08-14 15:37:50 -0400623 acpi_sfi_table_parse(ACPI_SIG_MCFG, pci_parse_mcfg);
Yinghai Lu068258b2009-03-19 20:55:35 -0700624
625 pci_mmcfg_reject_broken(early);
Olivier Galibertb7867392007-02-13 13:26:20 +0100626
627 if ((pci_mmcfg_config_num == 0) ||
628 (pci_mmcfg_config == NULL) ||
629 (pci_mmcfg_config[0].address == 0))
630 return;
631
Yinghai Luebd60cd2008-09-04 21:04:32 +0200632 if (pci_mmcfg_arch_init())
Olivier Galibertb7867392007-02-13 13:26:20 +0100633 pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
Yinghai Luebd60cd2008-09-04 21:04:32 +0200634 else {
Aaron Durbina5ba7972007-07-21 17:10:34 +0200635 /*
636 * Signal not to attempt to insert mmcfg resources because
637 * the architecture mmcfg setup could not initialize.
638 */
639 pci_mmcfg_resources_inserted = 1;
Olivier Galibertb7867392007-02-13 13:26:20 +0100640 }
641}
Aaron Durbina5ba7972007-07-21 17:10:34 +0200642
Yinghai Lubb63b422008-02-28 23:56:50 -0800643void __init pci_mmcfg_early_init(void)
Yinghai Lu05c58b82008-02-15 01:30:14 -0800644{
Yinghai Lubb63b422008-02-28 23:56:50 -0800645 __pci_mmcfg_init(1);
Yinghai Lu05c58b82008-02-15 01:30:14 -0800646}
647
648void __init pci_mmcfg_late_init(void)
649{
Yinghai Lubb63b422008-02-28 23:56:50 -0800650 __pci_mmcfg_init(0);
Yinghai Lu05c58b82008-02-15 01:30:14 -0800651}
652
Aaron Durbina5ba7972007-07-21 17:10:34 +0200653static int __init pci_mmcfg_late_insert_resources(void)
654{
655 /*
656 * If resources are already inserted or we are not using MMCONFIG,
657 * don't insert the resources.
658 */
659 if ((pci_mmcfg_resources_inserted == 1) ||
660 (pci_probe & PCI_PROBE_MMCONF) == 0 ||
661 (pci_mmcfg_config_num == 0) ||
662 (pci_mmcfg_config == NULL) ||
663 (pci_mmcfg_config[0].address == 0))
664 return 1;
665
666 /*
667 * Attempt to insert the mmcfg resources but not with the busy flag
668 * marked so it won't cause request errors when __request_region is
669 * called.
670 */
Yinghai Luebd60cd2008-09-04 21:04:32 +0200671 pci_mmcfg_insert_resources();
Aaron Durbina5ba7972007-07-21 17:10:34 +0200672
673 return 0;
674}
675
676/*
677 * Perform MMCONFIG resource insertion after PCI initialization to allow for
678 * misprogrammed MCFG tables that state larger sizes but actually conflict
679 * with other system resources.
680 */
681late_initcall(pci_mmcfg_late_insert_resources);