blob: 704b9ec043d709d787ffd732dc21c2200653de04 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Jiang Liu376f70a2012-06-22 14:55:12 +080020#include <linux/mutex.h>
21#include <linux/rculist.h>
Olivier Galibertb7867392007-02-13 13:26:20 +010022#include <asm/e820.h>
Jaswinder Singh Rajput82487712008-12-27 18:32:28 +053023#include <asm/pci_x86.h>
Feng Tang5f0db7a2009-08-14 15:37:50 -040024#include <asm/acpi.h>
Olivier Galibertb7867392007-02-13 13:26:20 +010025
Len Brownf4a2d582009-07-28 16:48:02 -040026#define PREFIX "PCI: "
Len Browna192a952009-07-28 16:45:54 -040027
Aaron Durbina5ba7972007-07-21 17:10:34 +020028/* Indicate if the mmcfg resources have been placed into the resource table. */
Jiang Liu95c5e922012-06-22 14:55:14 +080029static bool pci_mmcfg_running_state;
Jiang Liu9c951112012-06-22 14:55:15 +080030static bool pci_mmcfg_arch_init_failed;
Jiang Liu376f70a2012-06-22 14:55:12 +080031static DEFINE_MUTEX(pci_mmcfg_lock);
Aaron Durbina5ba7972007-07-21 17:10:34 +020032
Bjorn Helgaasff097dd2009-11-13 17:34:49 -070033LIST_HEAD(pci_mmcfg_list);
34
Bjorn Helgaasba2afba2009-11-13 17:34:54 -070035static __init void pci_mmconfig_remove(struct pci_mmcfg_region *cfg)
36{
37 if (cfg->res.parent)
38 release_resource(&cfg->res);
39 list_del(&cfg->list);
40 kfree(cfg);
41}
42
Bjorn Helgaas7da7d362009-11-13 17:33:53 -070043static __init void free_all_mmcfg(void)
44{
Bjorn Helgaasff097dd2009-11-13 17:34:49 -070045 struct pci_mmcfg_region *cfg, *tmp;
Bjorn Helgaas56ddf4d2009-11-13 17:34:29 -070046
Bjorn Helgaas7da7d362009-11-13 17:33:53 -070047 pci_mmcfg_arch_free();
Bjorn Helgaasba2afba2009-11-13 17:34:54 -070048 list_for_each_entry_safe(cfg, tmp, &pci_mmcfg_list, list)
49 pci_mmconfig_remove(cfg);
Bjorn Helgaasff097dd2009-11-13 17:34:49 -070050}
51
Jiang Liu376f70a2012-06-22 14:55:12 +080052static __devinit void list_add_sorted(struct pci_mmcfg_region *new)
Bjorn Helgaasff097dd2009-11-13 17:34:49 -070053{
54 struct pci_mmcfg_region *cfg;
55
56 /* keep list sorted by segment and starting bus number */
Jiang Liu376f70a2012-06-22 14:55:12 +080057 list_for_each_entry_rcu(cfg, &pci_mmcfg_list, list) {
Bjorn Helgaasff097dd2009-11-13 17:34:49 -070058 if (cfg->segment > new->segment ||
59 (cfg->segment == new->segment &&
60 cfg->start_bus >= new->start_bus)) {
Jiang Liu376f70a2012-06-22 14:55:12 +080061 list_add_tail_rcu(&new->list, &cfg->list);
Bjorn Helgaasff097dd2009-11-13 17:34:49 -070062 return;
63 }
64 }
Jiang Liu376f70a2012-06-22 14:55:12 +080065 list_add_tail_rcu(&new->list, &pci_mmcfg_list);
Bjorn Helgaas7da7d362009-11-13 17:33:53 -070066}
67
Jiang Liu846e4022012-06-22 14:55:11 +080068static __devinit struct pci_mmcfg_region *pci_mmconfig_alloc(int segment,
69 int start,
70 int end, u64 addr)
Yinghai Lu068258b2009-03-19 20:55:35 -070071{
Bjorn Helgaasd215a9c2009-11-13 17:34:13 -070072 struct pci_mmcfg_region *new;
Bjorn Helgaas56ddf4d2009-11-13 17:34:29 -070073 struct resource *res;
Yinghai Lu068258b2009-03-19 20:55:35 -070074
Bjorn Helgaasf7ca6982009-11-13 17:34:03 -070075 if (addr == 0)
76 return NULL;
77
Bjorn Helgaasff097dd2009-11-13 17:34:49 -070078 new = kzalloc(sizeof(*new), GFP_KERNEL);
Yinghai Lu068258b2009-03-19 20:55:35 -070079 if (!new)
Bjorn Helgaas7da7d362009-11-13 17:33:53 -070080 return NULL;
Yinghai Lu068258b2009-03-19 20:55:35 -070081
Bjorn Helgaas95cf1cf2009-11-13 17:34:24 -070082 new->address = addr;
83 new->segment = segment;
84 new->start_bus = start;
85 new->end_bus = end;
Bjorn Helgaas7da7d362009-11-13 17:33:53 -070086
Bjorn Helgaas56ddf4d2009-11-13 17:34:29 -070087 res = &new->res;
88 res->start = addr + PCI_MMCFG_BUS_OFFSET(start);
Bjorn Helgaas1ca98fa2010-10-04 12:49:24 -060089 res->end = addr + PCI_MMCFG_BUS_OFFSET(end + 1) - 1;
Bjorn Helgaas56ddf4d2009-11-13 17:34:29 -070090 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
91 snprintf(new->name, PCI_MMCFG_RESOURCE_NAME_LEN,
92 "PCI MMCONFIG %04x [bus %02x-%02x]", segment, start, end);
93 res->name = new->name;
94
Bjorn Helgaasff097dd2009-11-13 17:34:49 -070095 return new;
Yinghai Lu068258b2009-03-19 20:55:35 -070096}
97
Jiang Liu846e4022012-06-22 14:55:11 +080098static __init struct pci_mmcfg_region *pci_mmconfig_add(int segment, int start,
99 int end, u64 addr)
100{
101 struct pci_mmcfg_region *new;
102
103 new = pci_mmconfig_alloc(segment, start, end, addr);
Jiang Liu376f70a2012-06-22 14:55:12 +0800104 if (new) {
105 mutex_lock(&pci_mmcfg_lock);
Jiang Liu846e4022012-06-22 14:55:11 +0800106 list_add_sorted(new);
Jiang Liu376f70a2012-06-22 14:55:12 +0800107 mutex_unlock(&pci_mmcfg_lock);
Jiang Liu9c951112012-06-22 14:55:15 +0800108
Jiang Liu24c97f02012-06-22 14:55:22 +0800109 pr_info(PREFIX
Jiang Liu9c951112012-06-22 14:55:15 +0800110 "MMCONFIG for domain %04x [bus %02x-%02x] at %pR "
111 "(base %#lx)\n",
112 segment, start, end, &new->res, (unsigned long)addr);
Jiang Liu376f70a2012-06-22 14:55:12 +0800113 }
Jiang Liu846e4022012-06-22 14:55:11 +0800114
115 return new;
116}
117
Bjorn Helgaasf6e1d8c2009-11-13 17:35:04 -0700118struct pci_mmcfg_region *pci_mmconfig_lookup(int segment, int bus)
119{
120 struct pci_mmcfg_region *cfg;
121
Jiang Liu376f70a2012-06-22 14:55:12 +0800122 list_for_each_entry_rcu(cfg, &pci_mmcfg_list, list)
Bjorn Helgaasf6e1d8c2009-11-13 17:35:04 -0700123 if (cfg->segment == segment &&
124 cfg->start_bus <= bus && bus <= cfg->end_bus)
125 return cfg;
126
127 return NULL;
128}
129
OGAWA Hirofumi429d5122007-02-13 13:26:20 +0100130static const char __init *pci_mmcfg_e7520(void)
Olivier Galibert9358c692007-02-13 13:26:20 +0100131{
132 u32 win;
Yinghai Lubb63b422008-02-28 23:56:50 -0800133 raw_pci_ops->read(0, 0, PCI_DEVFN(0, 0), 0xce, 2, &win);
Olivier Galibert9358c692007-02-13 13:26:20 +0100134
Olivier Galibertb5229db2007-05-02 19:27:22 +0200135 win = win & 0xf000;
Yinghai Lu068258b2009-03-19 20:55:35 -0700136 if (win == 0x0000 || win == 0xf000)
137 return NULL;
138
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700139 if (pci_mmconfig_add(0, 0, 255, win << 16) == NULL)
Yinghai Lu068258b2009-03-19 20:55:35 -0700140 return NULL;
141
Olivier Galibert9358c692007-02-13 13:26:20 +0100142 return "Intel Corporation E7520 Memory Controller Hub";
143}
144
OGAWA Hirofumi429d5122007-02-13 13:26:20 +0100145static const char __init *pci_mmcfg_intel_945(void)
Olivier Galibert9358c692007-02-13 13:26:20 +0100146{
147 u32 pciexbar, mask = 0, len = 0;
148
Yinghai Lubb63b422008-02-28 23:56:50 -0800149 raw_pci_ops->read(0, 0, PCI_DEVFN(0, 0), 0x48, 4, &pciexbar);
Olivier Galibert9358c692007-02-13 13:26:20 +0100150
151 /* Enable bit */
152 if (!(pciexbar & 1))
Yinghai Lu068258b2009-03-19 20:55:35 -0700153 return NULL;
Olivier Galibert9358c692007-02-13 13:26:20 +0100154
155 /* Size bits */
156 switch ((pciexbar >> 1) & 3) {
157 case 0:
158 mask = 0xf0000000U;
159 len = 0x10000000U;
160 break;
161 case 1:
162 mask = 0xf8000000U;
163 len = 0x08000000U;
164 break;
165 case 2:
166 mask = 0xfc000000U;
167 len = 0x04000000U;
168 break;
169 default:
Yinghai Lu068258b2009-03-19 20:55:35 -0700170 return NULL;
Olivier Galibert9358c692007-02-13 13:26:20 +0100171 }
172
173 /* Errata #2, things break when not aligned on a 256Mb boundary */
174 /* Can only happen in 64M/128M mode */
175
176 if ((pciexbar & mask) & 0x0fffffffU)
Yinghai Lu068258b2009-03-19 20:55:35 -0700177 return NULL;
Olivier Galibert9358c692007-02-13 13:26:20 +0100178
Olivier Galibertb5229db2007-05-02 19:27:22 +0200179 /* Don't hit the APIC registers and their friends */
180 if ((pciexbar & mask) >= 0xf0000000U)
Yinghai Lu068258b2009-03-19 20:55:35 -0700181 return NULL;
Olivier Galibertb5229db2007-05-02 19:27:22 +0200182
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700183 if (pci_mmconfig_add(0, 0, (len >> 20) - 1, pciexbar & mask) == NULL)
Yinghai Lu068258b2009-03-19 20:55:35 -0700184 return NULL;
185
Olivier Galibert9358c692007-02-13 13:26:20 +0100186 return "Intel Corporation 945G/GZ/P/PL Express Memory Controller Hub";
187}
188
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800189static const char __init *pci_mmcfg_amd_fam10h(void)
190{
191 u32 low, high, address;
192 u64 base, msr;
193 int i;
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700194 unsigned segnbits = 0, busnbits, end_bus;
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800195
Yinghai Lu5f0b2972008-04-14 16:08:25 -0700196 if (!(pci_probe & PCI_CHECK_ENABLE_AMD_MMCONF))
197 return NULL;
198
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800199 address = MSR_FAM10H_MMIO_CONF_BASE;
200 if (rdmsr_safe(address, &low, &high))
201 return NULL;
202
203 msr = high;
204 msr <<= 32;
205 msr |= low;
206
207 /* mmconfig is not enable */
208 if (!(msr & FAM10H_MMIO_CONF_ENABLE))
209 return NULL;
210
211 base = msr & (FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT);
212
213 busnbits = (msr >> FAM10H_MMIO_CONF_BUSRANGE_SHIFT) &
214 FAM10H_MMIO_CONF_BUSRANGE_MASK;
215
216 /*
217 * only handle bus 0 ?
218 * need to skip it
219 */
220 if (!busnbits)
221 return NULL;
222
223 if (busnbits > 8) {
224 segnbits = busnbits - 8;
225 busnbits = 8;
226 }
227
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700228 end_bus = (1 << busnbits) - 1;
Yinghai Lu068258b2009-03-19 20:55:35 -0700229 for (i = 0; i < (1 << segnbits); i++)
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700230 if (pci_mmconfig_add(i, 0, end_bus,
231 base + (1<<28) * i) == NULL) {
232 free_all_mmcfg();
233 return NULL;
234 }
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800235
236 return "AMD Family 10h NB";
237}
238
Ed Swierk5546d6f2009-03-19 20:57:56 -0700239static bool __initdata mcp55_checked;
240static const char __init *pci_mmcfg_nvidia_mcp55(void)
241{
242 int bus;
243 int mcp55_mmconf_found = 0;
244
245 static const u32 extcfg_regnum = 0x90;
246 static const u32 extcfg_regsize = 4;
247 static const u32 extcfg_enable_mask = 1<<31;
248 static const u32 extcfg_start_mask = 0xff<<16;
249 static const int extcfg_start_shift = 16;
250 static const u32 extcfg_size_mask = 0x3<<28;
251 static const int extcfg_size_shift = 28;
252 static const int extcfg_sizebus[] = {0x100, 0x80, 0x40, 0x20};
253 static const u32 extcfg_base_mask[] = {0x7ff8, 0x7ffc, 0x7ffe, 0x7fff};
254 static const int extcfg_base_lshift = 25;
255
256 /*
257 * do check if amd fam10h already took over
258 */
Bjorn Helgaasff097dd2009-11-13 17:34:49 -0700259 if (!acpi_disabled || !list_empty(&pci_mmcfg_list) || mcp55_checked)
Ed Swierk5546d6f2009-03-19 20:57:56 -0700260 return NULL;
261
262 mcp55_checked = true;
263 for (bus = 0; bus < 256; bus++) {
264 u64 base;
265 u32 l, extcfg;
266 u16 vendor, device;
267 int start, size_index, end;
268
269 raw_pci_ops->read(0, bus, PCI_DEVFN(0, 0), 0, 4, &l);
270 vendor = l & 0xffff;
271 device = (l >> 16) & 0xffff;
272
273 if (PCI_VENDOR_ID_NVIDIA != vendor || 0x0369 != device)
274 continue;
275
276 raw_pci_ops->read(0, bus, PCI_DEVFN(0, 0), extcfg_regnum,
277 extcfg_regsize, &extcfg);
278
279 if (!(extcfg & extcfg_enable_mask))
280 continue;
281
Ed Swierk5546d6f2009-03-19 20:57:56 -0700282 size_index = (extcfg & extcfg_size_mask) >> extcfg_size_shift;
283 base = extcfg & extcfg_base_mask[size_index];
284 /* base could > 4G */
285 base <<= extcfg_base_lshift;
286 start = (extcfg & extcfg_start_mask) >> extcfg_start_shift;
287 end = start + extcfg_sizebus[size_index] - 1;
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700288 if (pci_mmconfig_add(0, start, end, base) == NULL)
289 continue;
Ed Swierk5546d6f2009-03-19 20:57:56 -0700290 mcp55_mmconf_found++;
291 }
292
293 if (!mcp55_mmconf_found)
294 return NULL;
295
296 return "nVidia MCP55";
297}
298
Olivier Galibert9358c692007-02-13 13:26:20 +0100299struct pci_mmcfg_hostbridge_probe {
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800300 u32 bus;
301 u32 devfn;
Olivier Galibert9358c692007-02-13 13:26:20 +0100302 u32 vendor;
303 u32 device;
304 const char *(*probe)(void);
305};
306
OGAWA Hirofumi429d5122007-02-13 13:26:20 +0100307static struct pci_mmcfg_hostbridge_probe pci_mmcfg_probes[] __initdata = {
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800308 { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
309 PCI_DEVICE_ID_INTEL_E7520_MCH, pci_mmcfg_e7520 },
310 { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
311 PCI_DEVICE_ID_INTEL_82945G_HB, pci_mmcfg_intel_945 },
312 { 0, PCI_DEVFN(0x18, 0), PCI_VENDOR_ID_AMD,
313 0x1200, pci_mmcfg_amd_fam10h },
314 { 0xff, PCI_DEVFN(0, 0), PCI_VENDOR_ID_AMD,
315 0x1200, pci_mmcfg_amd_fam10h },
Ed Swierk5546d6f2009-03-19 20:57:56 -0700316 { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_NVIDIA,
317 0x0369, pci_mmcfg_nvidia_mcp55 },
Olivier Galibert9358c692007-02-13 13:26:20 +0100318};
319
Yinghai Lu068258b2009-03-19 20:55:35 -0700320static void __init pci_mmcfg_check_end_bus_number(void)
321{
Bjorn Helgaas987c3672009-11-13 17:34:44 -0700322 struct pci_mmcfg_region *cfg, *cfgx;
Yinghai Lu068258b2009-03-19 20:55:35 -0700323
Thomas Gleixnerbb8d4132010-02-25 16:42:11 +0100324 /* Fixup overlaps */
Bjorn Helgaasff097dd2009-11-13 17:34:49 -0700325 list_for_each_entry(cfg, &pci_mmcfg_list, list) {
Bjorn Helgaasd7e6b662009-11-13 17:34:18 -0700326 if (cfg->end_bus < cfg->start_bus)
327 cfg->end_bus = 255;
Yinghai Lu068258b2009-03-19 20:55:35 -0700328
Thomas Gleixnerbb8d4132010-02-25 16:42:11 +0100329 /* Don't access the list head ! */
330 if (cfg->list.next == &pci_mmcfg_list)
331 break;
332
Bjorn Helgaasff097dd2009-11-13 17:34:49 -0700333 cfgx = list_entry(cfg->list.next, typeof(*cfg), list);
Thomas Gleixnerbb8d4132010-02-25 16:42:11 +0100334 if (cfg->end_bus >= cfgx->start_bus)
Bjorn Helgaasd7e6b662009-11-13 17:34:18 -0700335 cfg->end_bus = cfgx->start_bus - 1;
Yinghai Lu068258b2009-03-19 20:55:35 -0700336 }
337}
338
Olivier Galibert9358c692007-02-13 13:26:20 +0100339static int __init pci_mmcfg_check_hostbridge(void)
340{
341 u32 l;
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800342 u32 bus, devfn;
Olivier Galibert9358c692007-02-13 13:26:20 +0100343 u16 vendor, device;
344 int i;
345 const char *name;
346
Yinghai Lubb63b422008-02-28 23:56:50 -0800347 if (!raw_pci_ops)
348 return 0;
349
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700350 free_all_mmcfg();
Olivier Galibert9358c692007-02-13 13:26:20 +0100351
Yinghai Lu068258b2009-03-19 20:55:35 -0700352 for (i = 0; i < ARRAY_SIZE(pci_mmcfg_probes); i++) {
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800353 bus = pci_mmcfg_probes[i].bus;
354 devfn = pci_mmcfg_probes[i].devfn;
Yinghai Lubb63b422008-02-28 23:56:50 -0800355 raw_pci_ops->read(0, bus, devfn, 0, 4, &l);
Yinghai Lu7fd0da42008-02-19 03:13:02 -0800356 vendor = l & 0xffff;
357 device = (l >> 16) & 0xffff;
358
Yinghai Lu068258b2009-03-19 20:55:35 -0700359 name = NULL;
OGAWA Hirofumi429d5122007-02-13 13:26:20 +0100360 if (pci_mmcfg_probes[i].vendor == vendor &&
361 pci_mmcfg_probes[i].device == device)
Olivier Galibert9358c692007-02-13 13:26:20 +0100362 name = pci_mmcfg_probes[i].probe();
Yinghai Lu068258b2009-03-19 20:55:35 -0700363
364 if (name)
Jiang Liu24c97f02012-06-22 14:55:22 +0800365 pr_info(PREFIX "%s with MMCONFIG support\n", name);
OGAWA Hirofumi429d5122007-02-13 13:26:20 +0100366 }
Olivier Galibert9358c692007-02-13 13:26:20 +0100367
Yinghai Lu068258b2009-03-19 20:55:35 -0700368 /* some end_bus_number is crazy, fix it */
369 pci_mmcfg_check_end_bus_number();
Olivier Galibert9358c692007-02-13 13:26:20 +0100370
Bjorn Helgaasff097dd2009-11-13 17:34:49 -0700371 return !list_empty(&pci_mmcfg_list);
Olivier Galibert9358c692007-02-13 13:26:20 +0100372}
373
Jiang Liu95c5e922012-06-22 14:55:14 +0800374static acpi_status __devinit check_mcfg_resource(struct acpi_resource *res,
375 void *data)
Robert Hancock7752d5c2008-02-15 01:27:20 -0800376{
377 struct resource *mcfg_res = data;
378 struct acpi_resource_address64 address;
379 acpi_status status;
380
381 if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
382 struct acpi_resource_fixed_memory32 *fixmem32 =
383 &res->data.fixed_memory32;
384 if (!fixmem32)
385 return AE_OK;
386 if ((mcfg_res->start >= fixmem32->address) &&
Yinghai Lu75e613c2009-06-03 00:13:13 -0700387 (mcfg_res->end < (fixmem32->address +
Robert Hancock7752d5c2008-02-15 01:27:20 -0800388 fixmem32->address_length))) {
389 mcfg_res->flags = 1;
390 return AE_CTRL_TERMINATE;
391 }
392 }
393 if ((res->type != ACPI_RESOURCE_TYPE_ADDRESS32) &&
394 (res->type != ACPI_RESOURCE_TYPE_ADDRESS64))
395 return AE_OK;
396
397 status = acpi_resource_to_address64(res, &address);
398 if (ACPI_FAILURE(status) ||
399 (address.address_length <= 0) ||
400 (address.resource_type != ACPI_MEMORY_RANGE))
401 return AE_OK;
402
403 if ((mcfg_res->start >= address.minimum) &&
Yinghai Lu75e613c2009-06-03 00:13:13 -0700404 (mcfg_res->end < (address.minimum + address.address_length))) {
Robert Hancock7752d5c2008-02-15 01:27:20 -0800405 mcfg_res->flags = 1;
406 return AE_CTRL_TERMINATE;
407 }
408 return AE_OK;
409}
410
Jiang Liu95c5e922012-06-22 14:55:14 +0800411static acpi_status __devinit find_mboard_resource(acpi_handle handle, u32 lvl,
412 void *context, void **rv)
Robert Hancock7752d5c2008-02-15 01:27:20 -0800413{
414 struct resource *mcfg_res = context;
415
416 acpi_walk_resources(handle, METHOD_NAME__CRS,
417 check_mcfg_resource, context);
418
419 if (mcfg_res->flags)
420 return AE_CTRL_TERMINATE;
421
422 return AE_OK;
423}
424
Jiang Liu95c5e922012-06-22 14:55:14 +0800425static int __devinit is_acpi_reserved(u64 start, u64 end, unsigned not_used)
Robert Hancock7752d5c2008-02-15 01:27:20 -0800426{
427 struct resource mcfg_res;
428
429 mcfg_res.start = start;
Yinghai Lu75e613c2009-06-03 00:13:13 -0700430 mcfg_res.end = end - 1;
Robert Hancock7752d5c2008-02-15 01:27:20 -0800431 mcfg_res.flags = 0;
432
433 acpi_get_devices("PNP0C01", find_mboard_resource, &mcfg_res, NULL);
434
435 if (!mcfg_res.flags)
436 acpi_get_devices("PNP0C02", find_mboard_resource, &mcfg_res,
437 NULL);
438
439 return mcfg_res.flags;
440}
441
Yinghai Lua83fe322008-07-18 13:22:36 -0700442typedef int (*check_reserved_t)(u64 start, u64 end, unsigned type);
443
Jiang Liu95c5e922012-06-22 14:55:14 +0800444static int __ref is_mmconf_reserved(check_reserved_t is_reserved,
445 struct pci_mmcfg_region *cfg,
446 struct device *dev, int with_e820)
Yinghai Lua83fe322008-07-18 13:22:36 -0700447{
Bjorn Helgaas2f2a8b92009-11-13 17:34:34 -0700448 u64 addr = cfg->res.start;
449 u64 size = resource_size(&cfg->res);
Yinghai Lua83fe322008-07-18 13:22:36 -0700450 u64 old_size = size;
Jiang Liu95c5e922012-06-22 14:55:14 +0800451 int num_buses;
452 char *method = with_e820 ? "E820" : "ACPI motherboard resources";
Yinghai Lua83fe322008-07-18 13:22:36 -0700453
Yinghai Lu044cd802009-04-18 01:43:46 -0700454 while (!is_reserved(addr, addr + size, E820_RESERVED)) {
Yinghai Lua83fe322008-07-18 13:22:36 -0700455 size >>= 1;
456 if (size < (16UL<<20))
457 break;
458 }
459
Jiang Liu95c5e922012-06-22 14:55:14 +0800460 if (size < (16UL<<20) && size != old_size)
461 return 0;
Yinghai Lua83fe322008-07-18 13:22:36 -0700462
Jiang Liu95c5e922012-06-22 14:55:14 +0800463 if (dev)
464 dev_info(dev, "MMCONFIG at %pR reserved in %s\n",
465 &cfg->res, method);
466 else
Jiang Liu24c97f02012-06-22 14:55:22 +0800467 pr_info(PREFIX "MMCONFIG at %pR reserved in %s\n",
Jiang Liu95c5e922012-06-22 14:55:14 +0800468 &cfg->res, method);
469
470 if (old_size != size) {
471 /* update end_bus */
472 cfg->end_bus = cfg->start_bus + ((size>>20) - 1);
473 num_buses = cfg->end_bus - cfg->start_bus + 1;
474 cfg->res.end = cfg->res.start +
475 PCI_MMCFG_BUS_OFFSET(num_buses) - 1;
476 snprintf(cfg->name, PCI_MMCFG_RESOURCE_NAME_LEN,
477 "PCI MMCONFIG %04x [bus %02x-%02x]",
478 cfg->segment, cfg->start_bus, cfg->end_bus);
479
480 if (dev)
481 dev_info(dev,
482 "MMCONFIG "
483 "at %pR (base %#lx) (size reduced!)\n",
484 &cfg->res, (unsigned long) cfg->address);
485 else
Jiang Liu24c97f02012-06-22 14:55:22 +0800486 pr_info(PREFIX
Jiang Liu95c5e922012-06-22 14:55:14 +0800487 "MMCONFIG for %04x [bus%02x-%02x] "
488 "at %pR (base %#lx) (size reduced!)\n",
489 cfg->segment, cfg->start_bus, cfg->end_bus,
490 &cfg->res, (unsigned long) cfg->address);
Yinghai Lua83fe322008-07-18 13:22:36 -0700491 }
492
Jiang Liu95c5e922012-06-22 14:55:14 +0800493 return 1;
Yinghai Lua83fe322008-07-18 13:22:36 -0700494}
495
Jiang Liu95c5e922012-06-22 14:55:14 +0800496static int __ref pci_mmcfg_check_reserved(struct device *dev,
497 struct pci_mmcfg_region *cfg, int early)
Jiang Liu2a76c452012-06-22 14:55:10 +0800498{
499 if (!early && !acpi_disabled) {
Jiang Liu95c5e922012-06-22 14:55:14 +0800500 if (is_mmconf_reserved(is_acpi_reserved, cfg, dev, 0))
Jiang Liu2a76c452012-06-22 14:55:10 +0800501 return 1;
Jiang Liu95c5e922012-06-22 14:55:14 +0800502
503 if (dev)
504 dev_info(dev, FW_INFO
505 "MMCONFIG at %pR not reserved in "
506 "ACPI motherboard resources\n",
507 &cfg->res);
Jiang Liu2a76c452012-06-22 14:55:10 +0800508 else
Jiang Liu24c97f02012-06-22 14:55:22 +0800509 pr_info(FW_INFO PREFIX
Jiang Liu2a76c452012-06-22 14:55:10 +0800510 "MMCONFIG at %pR not reserved in "
511 "ACPI motherboard resources\n",
512 &cfg->res);
513 }
514
Jiang Liu95c5e922012-06-22 14:55:14 +0800515 /*
516 * e820_all_mapped() is marked as __init.
517 * All entries from ACPI MCFG table have been checked at boot time.
518 * For MCFG information constructed from hotpluggable host bridge's
519 * _CBA method, just assume it's reserved.
520 */
521 if (pci_mmcfg_running_state)
522 return 1;
523
Jiang Liu2a76c452012-06-22 14:55:10 +0800524 /* Don't try to do this check unless configuration
525 type 1 is available. how about type 2 ?*/
526 if (raw_pci_ops)
Jiang Liu95c5e922012-06-22 14:55:14 +0800527 return is_mmconf_reserved(e820_all_mapped, cfg, dev, 1);
Jiang Liu2a76c452012-06-22 14:55:10 +0800528
529 return 0;
530}
531
Yinghai Lubb63b422008-02-28 23:56:50 -0800532static void __init pci_mmcfg_reject_broken(int early)
OGAWA Hirofumi44de0202007-02-13 13:26:20 +0100533{
Bjorn Helgaas987c3672009-11-13 17:34:44 -0700534 struct pci_mmcfg_region *cfg;
OGAWA Hirofumi26054ed2007-02-13 13:26:20 +0100535
Bjorn Helgaasff097dd2009-11-13 17:34:49 -0700536 list_for_each_entry(cfg, &pci_mmcfg_list, list) {
Jiang Liu95c5e922012-06-22 14:55:14 +0800537 if (pci_mmcfg_check_reserved(NULL, cfg, early) == 0) {
Jiang Liu24c97f02012-06-22 14:55:22 +0800538 pr_info(PREFIX "not using MMCONFIG\n");
Jiang Liu2a76c452012-06-22 14:55:10 +0800539 free_all_mmcfg();
540 return;
Feng Tanga02ce952010-05-05 17:08:49 +0800541 }
OGAWA Hirofumi26054ed2007-02-13 13:26:20 +0100542 }
OGAWA Hirofumi44de0202007-02-13 13:26:20 +0100543}
544
Bjorn Helgaas9a08f7d2009-10-23 15:20:33 -0600545static int __init acpi_mcfg_check_entry(struct acpi_table_mcfg *mcfg,
546 struct acpi_mcfg_allocation *cfg)
Len Brownc4bf2f32009-06-11 23:53:55 -0400547{
Bjorn Helgaas9a08f7d2009-10-23 15:20:33 -0600548 int year;
Len Brownc4bf2f32009-06-11 23:53:55 -0400549
Bjorn Helgaas9a08f7d2009-10-23 15:20:33 -0600550 if (cfg->address < 0xFFFFFFFF)
551 return 0;
552
Jack Steiner68856852011-06-02 14:59:43 -0500553 if (!strcmp(mcfg->header.oem_id, "SGI") ||
554 !strcmp(mcfg->header.oem_id, "SGI2"))
Bjorn Helgaas9a08f7d2009-10-23 15:20:33 -0600555 return 0;
556
557 if (mcfg->header.revision >= 1) {
558 if (dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL) &&
559 year >= 2010)
560 return 0;
561 }
562
Jiang Liu24c97f02012-06-22 14:55:22 +0800563 pr_err(PREFIX "MCFG region for %04x [bus %02x-%02x] at %#llx "
Bjorn Helgaas9a08f7d2009-10-23 15:20:33 -0600564 "is above 4GB, ignored\n", cfg->pci_segment,
565 cfg->start_bus_number, cfg->end_bus_number, cfg->address);
566 return -EINVAL;
Len Brownc4bf2f32009-06-11 23:53:55 -0400567}
568
569static int __init pci_parse_mcfg(struct acpi_table_header *header)
570{
571 struct acpi_table_mcfg *mcfg;
Bjorn Helgaasd3578ef2009-11-13 17:33:47 -0700572 struct acpi_mcfg_allocation *cfg_table, *cfg;
Len Brownc4bf2f32009-06-11 23:53:55 -0400573 unsigned long i;
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700574 int entries;
Len Brownc4bf2f32009-06-11 23:53:55 -0400575
576 if (!header)
577 return -EINVAL;
578
579 mcfg = (struct acpi_table_mcfg *)header;
580
581 /* how many config structures do we have */
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700582 free_all_mmcfg();
Bjorn Helgaase823d6f2009-11-13 17:33:42 -0700583 entries = 0;
Len Brownc4bf2f32009-06-11 23:53:55 -0400584 i = header->length - sizeof(struct acpi_table_mcfg);
585 while (i >= sizeof(struct acpi_mcfg_allocation)) {
Bjorn Helgaase823d6f2009-11-13 17:33:42 -0700586 entries++;
Len Brownc4bf2f32009-06-11 23:53:55 -0400587 i -= sizeof(struct acpi_mcfg_allocation);
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +0200588 }
Bjorn Helgaase823d6f2009-11-13 17:33:42 -0700589 if (entries == 0) {
Jiang Liu24c97f02012-06-22 14:55:22 +0800590 pr_err(PREFIX "MMCONFIG has no entries\n");
Len Brownc4bf2f32009-06-11 23:53:55 -0400591 return -ENODEV;
592 }
593
Bjorn Helgaasd3578ef2009-11-13 17:33:47 -0700594 cfg_table = (struct acpi_mcfg_allocation *) &mcfg[1];
Bjorn Helgaase823d6f2009-11-13 17:33:42 -0700595 for (i = 0; i < entries; i++) {
Bjorn Helgaasd3578ef2009-11-13 17:33:47 -0700596 cfg = &cfg_table[i];
597 if (acpi_mcfg_check_entry(mcfg, cfg)) {
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700598 free_all_mmcfg();
Len Brownc4bf2f32009-06-11 23:53:55 -0400599 return -ENODEV;
600 }
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700601
602 if (pci_mmconfig_add(cfg->pci_segment, cfg->start_bus_number,
603 cfg->end_bus_number, cfg->address) == NULL) {
Jiang Liu24c97f02012-06-22 14:55:22 +0800604 pr_warn(PREFIX "no memory for MCFG entries\n");
Bjorn Helgaas7da7d362009-11-13 17:33:53 -0700605 free_all_mmcfg();
606 return -ENOMEM;
607 }
Len Brownc4bf2f32009-06-11 23:53:55 -0400608 }
609
610 return 0;
611}
612
Thomas Gleixner968cbfa2008-05-12 15:43:37 +0200613static void __init __pci_mmcfg_init(int early)
Olivier Galibertb7867392007-02-13 13:26:20 +0100614{
Yinghai Lu068258b2009-03-19 20:55:35 -0700615 pci_mmcfg_reject_broken(early);
Bjorn Helgaasff097dd2009-11-13 17:34:49 -0700616 if (list_empty(&pci_mmcfg_list))
Olivier Galibertb7867392007-02-13 13:26:20 +0100617 return;
618
Jan Beulicha3170c12011-02-23 10:08:10 +0000619 if (pcibios_last_bus < 0) {
620 const struct pci_mmcfg_region *cfg;
621
622 list_for_each_entry(cfg, &pci_mmcfg_list, list) {
623 if (cfg->segment)
624 break;
625 pcibios_last_bus = cfg->end_bus;
626 }
627 }
628
Yinghai Luebd60cd2008-09-04 21:04:32 +0200629 if (pci_mmcfg_arch_init())
Olivier Galibertb7867392007-02-13 13:26:20 +0100630 pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
Yinghai Luebd60cd2008-09-04 21:04:32 +0200631 else {
Jiang Liu66e88502012-06-22 14:55:18 +0800632 free_all_mmcfg();
Jiang Liu9c951112012-06-22 14:55:15 +0800633 pci_mmcfg_arch_init_failed = true;
Olivier Galibertb7867392007-02-13 13:26:20 +0100634 }
635}
Aaron Durbina5ba7972007-07-21 17:10:34 +0200636
Jiang Liu574a5942012-06-22 14:55:20 +0800637static int __initdata known_bridge;
638
Yinghai Lubb63b422008-02-28 23:56:50 -0800639void __init pci_mmcfg_early_init(void)
Yinghai Lu05c58b82008-02-15 01:30:14 -0800640{
Jiang Liu574a5942012-06-22 14:55:20 +0800641 if (pci_probe & PCI_PROBE_MMCONF) {
642 if (pci_mmcfg_check_hostbridge())
643 known_bridge = 1;
644 else
645 acpi_sfi_table_parse(ACPI_SIG_MCFG, pci_parse_mcfg);
646 __pci_mmcfg_init(1);
647 }
Yinghai Lu05c58b82008-02-15 01:30:14 -0800648}
649
650void __init pci_mmcfg_late_init(void)
651{
Jiang Liu574a5942012-06-22 14:55:20 +0800652 /* MMCONFIG disabled */
653 if ((pci_probe & PCI_PROBE_MMCONF) == 0)
654 return;
655
656 if (known_bridge)
657 return;
658
659 /* MMCONFIG hasn't been enabled yet, try again */
660 if (pci_probe & PCI_PROBE_MASK & ~PCI_PROBE_MMCONF) {
661 acpi_sfi_table_parse(ACPI_SIG_MCFG, pci_parse_mcfg);
662 __pci_mmcfg_init(0);
663 }
Yinghai Lu05c58b82008-02-15 01:30:14 -0800664}
665
Aaron Durbina5ba7972007-07-21 17:10:34 +0200666static int __init pci_mmcfg_late_insert_resources(void)
667{
Jiang Liu66e88502012-06-22 14:55:18 +0800668 struct pci_mmcfg_region *cfg;
669
Jiang Liu95c5e922012-06-22 14:55:14 +0800670 pci_mmcfg_running_state = true;
671
Jiang Liu66e88502012-06-22 14:55:18 +0800672 /* If we are not using MMCONFIG, don't insert the resources. */
673 if ((pci_probe & PCI_PROBE_MMCONF) == 0)
Aaron Durbina5ba7972007-07-21 17:10:34 +0200674 return 1;
675
676 /*
677 * Attempt to insert the mmcfg resources but not with the busy flag
678 * marked so it won't cause request errors when __request_region is
679 * called.
680 */
Jiang Liu66e88502012-06-22 14:55:18 +0800681 list_for_each_entry(cfg, &pci_mmcfg_list, list)
682 if (!cfg->res.parent)
683 insert_resource(&iomem_resource, &cfg->res);
Aaron Durbina5ba7972007-07-21 17:10:34 +0200684
685 return 0;
686}
687
688/*
689 * Perform MMCONFIG resource insertion after PCI initialization to allow for
690 * misprogrammed MCFG tables that state larger sizes but actually conflict
691 * with other system resources.
692 */
693late_initcall(pci_mmcfg_late_insert_resources);
Jiang Liu9c951112012-06-22 14:55:15 +0800694
695/* Add MMCFG information for host bridges */
696int __devinit pci_mmconfig_insert(struct device *dev,
697 u16 seg, u8 start, u8 end,
698 phys_addr_t addr)
699{
700 int rc;
701 struct resource *tmp = NULL;
702 struct pci_mmcfg_region *cfg;
703
704 if (!(pci_probe & PCI_PROBE_MMCONF) || pci_mmcfg_arch_init_failed)
705 return -ENODEV;
706
707 if (start > end)
708 return -EINVAL;
709
710 mutex_lock(&pci_mmcfg_lock);
711 cfg = pci_mmconfig_lookup(seg, start);
712 if (cfg) {
713 if (cfg->end_bus < end)
714 dev_info(dev, FW_INFO
715 "MMCONFIG for "
716 "domain %04x [bus %02x-%02x] "
717 "only partially covers this bridge\n",
718 cfg->segment, cfg->start_bus, cfg->end_bus);
719 mutex_unlock(&pci_mmcfg_lock);
720 return -EEXIST;
721 }
722
723 if (!addr) {
724 mutex_unlock(&pci_mmcfg_lock);
725 return -EINVAL;
726 }
727
728 rc = -EBUSY;
729 cfg = pci_mmconfig_alloc(seg, start, end, addr);
730 if (cfg == NULL) {
731 dev_warn(dev, "fail to add MMCONFIG (out of memory)\n");
732 rc = -ENOMEM;
733 } else if (!pci_mmcfg_check_reserved(dev, cfg, 0)) {
734 dev_warn(dev, FW_BUG "MMCONFIG %pR isn't reserved\n",
735 &cfg->res);
736 } else {
737 /* Insert resource if it's not in boot stage */
738 if (pci_mmcfg_running_state)
739 tmp = insert_resource_conflict(&iomem_resource,
740 &cfg->res);
741
742 if (tmp) {
743 dev_warn(dev,
744 "MMCONFIG %pR conflicts with "
745 "%s %pR\n",
746 &cfg->res, tmp->name, tmp);
747 } else if (pci_mmcfg_arch_map(cfg)) {
748 dev_warn(dev, "fail to map MMCONFIG %pR.\n",
749 &cfg->res);
750 } else {
751 list_add_sorted(cfg);
752 dev_info(dev, "MMCONFIG at %pR (base %#lx)\n",
753 &cfg->res, (unsigned long)addr);
754 cfg = NULL;
755 rc = 0;
756 }
757 }
758
759 if (cfg) {
760 if (cfg->res.parent)
761 release_resource(&cfg->res);
762 kfree(cfg);
763 }
764
765 mutex_unlock(&pci_mmcfg_lock);
766
767 return rc;
768}
769
770/* Delete MMCFG information for host bridges */
771int pci_mmconfig_delete(u16 seg, u8 start, u8 end)
772{
773 struct pci_mmcfg_region *cfg;
774
775 mutex_lock(&pci_mmcfg_lock);
776 list_for_each_entry_rcu(cfg, &pci_mmcfg_list, list)
777 if (cfg->segment == seg && cfg->start_bus == start &&
778 cfg->end_bus == end) {
779 list_del_rcu(&cfg->list);
780 synchronize_rcu();
781 pci_mmcfg_arch_unmap(cfg);
782 if (cfg->res.parent)
783 release_resource(&cfg->res);
784 mutex_unlock(&pci_mmcfg_lock);
785 kfree(cfg);
786 return 0;
787 }
788 mutex_unlock(&pci_mmcfg_lock);
789
790 return -ENOENT;
791}