mmconfig-shared.c 19 KB

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