mmconfig-shared.c 19 KB

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