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/acpi.h>
  14. #include <linux/pci.h>
  15. #include <linux/init.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. #define pci_mmcfg_lock_held() lock_is_held(&(pci_mmcfg_lock).dep_map)
  30. LIST_HEAD(pci_mmcfg_list);
  31. static void __init pci_mmconfig_remove(struct pci_mmcfg_region *cfg)
  32. {
  33. if (cfg->res.parent)
  34. release_resource(&cfg->res);
  35. list_del(&cfg->list);
  36. kfree(cfg);
  37. }
  38. static void __init free_all_mmcfg(void)
  39. {
  40. struct pci_mmcfg_region *cfg, *tmp;
  41. pci_mmcfg_arch_free();
  42. list_for_each_entry_safe(cfg, tmp, &pci_mmcfg_list, list)
  43. pci_mmconfig_remove(cfg);
  44. }
  45. static void list_add_sorted(struct pci_mmcfg_region *new)
  46. {
  47. struct pci_mmcfg_region *cfg;
  48. /* keep list sorted by segment and starting bus number */
  49. list_for_each_entry_rcu(cfg, &pci_mmcfg_list, list, pci_mmcfg_lock_held()) {
  50. if (cfg->segment > new->segment ||
  51. (cfg->segment == new->segment &&
  52. cfg->start_bus >= new->start_bus)) {
  53. list_add_tail_rcu(&new->list, &cfg->list);
  54. return;
  55. }
  56. }
  57. list_add_tail_rcu(&new->list, &pci_mmcfg_list);
  58. }
  59. static struct pci_mmcfg_region *pci_mmconfig_alloc(int segment, 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. struct pci_mmcfg_region *__init 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, pci_mmcfg_lock_held())
  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 __initconst = 0x90;
  198. static const u32 extcfg_regsize __initconst = 4;
  199. static const u32 extcfg_enable_mask __initconst = 1 << 31;
  200. static const u32 extcfg_start_mask __initconst = 0xff << 16;
  201. static const int extcfg_start_shift __initconst = 16;
  202. static const u32 extcfg_size_mask __initconst = 0x3 << 28;
  203. static const int extcfg_size_shift __initconst = 28;
  204. static const int extcfg_sizebus[] __initconst = {
  205. 0x100, 0x80, 0x40, 0x20
  206. };
  207. static const u32 extcfg_base_mask[] __initconst = {
  208. 0x7ff8, 0x7ffc, 0x7ffe, 0x7fff
  209. };
  210. static const int extcfg_base_lshift __initconst = 25;
  211. /*
  212. * do check if amd fam10h already took over
  213. */
  214. if (!acpi_disabled || !list_empty(&pci_mmcfg_list) || mcp55_checked)
  215. return NULL;
  216. mcp55_checked = true;
  217. for (bus = 0; bus < 256; bus++) {
  218. u64 base;
  219. u32 l, extcfg;
  220. u16 vendor, device;
  221. int start, size_index, end;
  222. raw_pci_ops->read(0, bus, PCI_DEVFN(0, 0), 0, 4, &l);
  223. vendor = l & 0xffff;
  224. device = (l >> 16) & 0xffff;
  225. if (PCI_VENDOR_ID_NVIDIA != vendor || 0x0369 != device)
  226. continue;
  227. raw_pci_ops->read(0, bus, PCI_DEVFN(0, 0), extcfg_regnum,
  228. extcfg_regsize, &extcfg);
  229. if (!(extcfg & extcfg_enable_mask))
  230. continue;
  231. size_index = (extcfg & extcfg_size_mask) >> extcfg_size_shift;
  232. base = extcfg & extcfg_base_mask[size_index];
  233. /* base could > 4G */
  234. base <<= extcfg_base_lshift;
  235. start = (extcfg & extcfg_start_mask) >> extcfg_start_shift;
  236. end = start + extcfg_sizebus[size_index] - 1;
  237. if (pci_mmconfig_add(0, start, end, base) == NULL)
  238. continue;
  239. mcp55_mmconf_found++;
  240. }
  241. if (!mcp55_mmconf_found)
  242. return NULL;
  243. return "nVidia MCP55";
  244. }
  245. struct pci_mmcfg_hostbridge_probe {
  246. u32 bus;
  247. u32 devfn;
  248. u32 vendor;
  249. u32 device;
  250. const char *(*probe)(void);
  251. };
  252. static const struct pci_mmcfg_hostbridge_probe pci_mmcfg_probes[] __initconst = {
  253. { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
  254. PCI_DEVICE_ID_INTEL_E7520_MCH, pci_mmcfg_e7520 },
  255. { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
  256. PCI_DEVICE_ID_INTEL_82945G_HB, pci_mmcfg_intel_945 },
  257. { 0, PCI_DEVFN(0x18, 0), PCI_VENDOR_ID_AMD,
  258. 0x1200, pci_mmcfg_amd_fam10h },
  259. { 0xff, PCI_DEVFN(0, 0), PCI_VENDOR_ID_AMD,
  260. 0x1200, pci_mmcfg_amd_fam10h },
  261. { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_NVIDIA,
  262. 0x0369, pci_mmcfg_nvidia_mcp55 },
  263. };
  264. static void __init pci_mmcfg_check_end_bus_number(void)
  265. {
  266. struct pci_mmcfg_region *cfg, *cfgx;
  267. /* Fixup overlaps */
  268. list_for_each_entry(cfg, &pci_mmcfg_list, list) {
  269. if (cfg->end_bus < cfg->start_bus)
  270. cfg->end_bus = 255;
  271. /* Don't access the list head ! */
  272. if (cfg->list.next == &pci_mmcfg_list)
  273. break;
  274. cfgx = list_entry(cfg->list.next, typeof(*cfg), list);
  275. if (cfg->end_bus >= cfgx->start_bus)
  276. cfg->end_bus = cfgx->start_bus - 1;
  277. }
  278. }
  279. static int __init pci_mmcfg_check_hostbridge(void)
  280. {
  281. u32 l;
  282. u32 bus, devfn;
  283. u16 vendor, device;
  284. int i;
  285. const char *name;
  286. if (!raw_pci_ops)
  287. return 0;
  288. free_all_mmcfg();
  289. for (i = 0; i < ARRAY_SIZE(pci_mmcfg_probes); i++) {
  290. bus = pci_mmcfg_probes[i].bus;
  291. devfn = pci_mmcfg_probes[i].devfn;
  292. raw_pci_ops->read(0, bus, devfn, 0, 4, &l);
  293. vendor = l & 0xffff;
  294. device = (l >> 16) & 0xffff;
  295. name = NULL;
  296. if (pci_mmcfg_probes[i].vendor == vendor &&
  297. pci_mmcfg_probes[i].device == device)
  298. name = pci_mmcfg_probes[i].probe();
  299. if (name)
  300. pr_info(PREFIX "%s with MMCONFIG support\n", name);
  301. }
  302. /* some end_bus_number is crazy, fix it */
  303. pci_mmcfg_check_end_bus_number();
  304. return !list_empty(&pci_mmcfg_list);
  305. }
  306. static acpi_status check_mcfg_resource(struct acpi_resource *res, void *data)
  307. {
  308. struct resource *mcfg_res = data;
  309. struct acpi_resource_address64 address;
  310. acpi_status status;
  311. if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
  312. struct acpi_resource_fixed_memory32 *fixmem32 =
  313. &res->data.fixed_memory32;
  314. if (!fixmem32)
  315. return AE_OK;
  316. if ((mcfg_res->start >= fixmem32->address) &&
  317. (mcfg_res->end < (fixmem32->address +
  318. fixmem32->address_length))) {
  319. mcfg_res->flags = 1;
  320. return AE_CTRL_TERMINATE;
  321. }
  322. }
  323. if ((res->type != ACPI_RESOURCE_TYPE_ADDRESS32) &&
  324. (res->type != ACPI_RESOURCE_TYPE_ADDRESS64))
  325. return AE_OK;
  326. status = acpi_resource_to_address64(res, &address);
  327. if (ACPI_FAILURE(status) ||
  328. (address.address.address_length <= 0) ||
  329. (address.resource_type != ACPI_MEMORY_RANGE))
  330. return AE_OK;
  331. if ((mcfg_res->start >= address.address.minimum) &&
  332. (mcfg_res->end < (address.address.minimum + address.address.address_length))) {
  333. mcfg_res->flags = 1;
  334. return AE_CTRL_TERMINATE;
  335. }
  336. return AE_OK;
  337. }
  338. static acpi_status find_mboard_resource(acpi_handle handle, u32 lvl,
  339. void *context, void **rv)
  340. {
  341. struct resource *mcfg_res = context;
  342. acpi_walk_resources(handle, METHOD_NAME__CRS,
  343. check_mcfg_resource, context);
  344. if (mcfg_res->flags)
  345. return AE_CTRL_TERMINATE;
  346. return AE_OK;
  347. }
  348. static bool is_acpi_reserved(u64 start, u64 end, enum e820_type not_used)
  349. {
  350. struct resource mcfg_res;
  351. mcfg_res.start = start;
  352. mcfg_res.end = end - 1;
  353. mcfg_res.flags = 0;
  354. acpi_get_devices("PNP0C01", find_mboard_resource, &mcfg_res, NULL);
  355. if (!mcfg_res.flags)
  356. acpi_get_devices("PNP0C02", find_mboard_resource, &mcfg_res,
  357. NULL);
  358. return mcfg_res.flags;
  359. }
  360. typedef bool (*check_reserved_t)(u64 start, u64 end, enum e820_type type);
  361. static bool __ref is_mmconf_reserved(check_reserved_t is_reserved,
  362. struct pci_mmcfg_region *cfg,
  363. struct device *dev, int with_e820)
  364. {
  365. u64 addr = cfg->res.start;
  366. u64 size = resource_size(&cfg->res);
  367. u64 old_size = size;
  368. int num_buses;
  369. char *method = with_e820 ? "E820" : "ACPI motherboard resources";
  370. while (!is_reserved(addr, addr + size, E820_TYPE_RESERVED)) {
  371. size >>= 1;
  372. if (size < (16UL<<20))
  373. break;
  374. }
  375. if (size < (16UL<<20) && size != old_size)
  376. return false;
  377. if (dev)
  378. dev_info(dev, "MMCONFIG at %pR reserved in %s\n",
  379. &cfg->res, method);
  380. else
  381. pr_info(PREFIX "MMCONFIG at %pR reserved in %s\n",
  382. &cfg->res, method);
  383. if (old_size != size) {
  384. /* update end_bus */
  385. cfg->end_bus = cfg->start_bus + ((size>>20) - 1);
  386. num_buses = cfg->end_bus - cfg->start_bus + 1;
  387. cfg->res.end = cfg->res.start +
  388. PCI_MMCFG_BUS_OFFSET(num_buses) - 1;
  389. snprintf(cfg->name, PCI_MMCFG_RESOURCE_NAME_LEN,
  390. "PCI MMCONFIG %04x [bus %02x-%02x]",
  391. cfg->segment, cfg->start_bus, cfg->end_bus);
  392. if (dev)
  393. dev_info(dev,
  394. "MMCONFIG "
  395. "at %pR (base %#lx) (size reduced!)\n",
  396. &cfg->res, (unsigned long) cfg->address);
  397. else
  398. pr_info(PREFIX
  399. "MMCONFIG for %04x [bus%02x-%02x] "
  400. "at %pR (base %#lx) (size reduced!)\n",
  401. cfg->segment, cfg->start_bus, cfg->end_bus,
  402. &cfg->res, (unsigned long) cfg->address);
  403. }
  404. return true;
  405. }
  406. static bool __ref
  407. pci_mmcfg_check_reserved(struct device *dev, struct pci_mmcfg_region *cfg, int early)
  408. {
  409. if (!early && !acpi_disabled) {
  410. if (is_mmconf_reserved(is_acpi_reserved, cfg, dev, 0))
  411. return true;
  412. if (dev)
  413. dev_info(dev, FW_INFO
  414. "MMCONFIG at %pR not reserved in "
  415. "ACPI motherboard resources\n",
  416. &cfg->res);
  417. else
  418. pr_info(FW_INFO PREFIX
  419. "MMCONFIG at %pR not reserved in "
  420. "ACPI motherboard resources\n",
  421. &cfg->res);
  422. }
  423. /*
  424. * e820__mapped_all() is marked as __init.
  425. * All entries from ACPI MCFG table have been checked at boot time.
  426. * For MCFG information constructed from hotpluggable host bridge's
  427. * _CBA method, just assume it's reserved.
  428. */
  429. if (pci_mmcfg_running_state)
  430. return true;
  431. /* Don't try to do this check unless configuration
  432. type 1 is available. how about type 2 ?*/
  433. if (raw_pci_ops)
  434. return is_mmconf_reserved(e820__mapped_all, cfg, dev, 1);
  435. return false;
  436. }
  437. static void __init pci_mmcfg_reject_broken(int early)
  438. {
  439. struct pci_mmcfg_region *cfg;
  440. list_for_each_entry(cfg, &pci_mmcfg_list, list) {
  441. if (pci_mmcfg_check_reserved(NULL, cfg, early) == 0) {
  442. pr_info(PREFIX "not using MMCONFIG\n");
  443. free_all_mmcfg();
  444. return;
  445. }
  446. }
  447. }
  448. static int __init acpi_mcfg_check_entry(struct acpi_table_mcfg *mcfg,
  449. struct acpi_mcfg_allocation *cfg)
  450. {
  451. if (cfg->address < 0xFFFFFFFF)
  452. return 0;
  453. if (!strncmp(mcfg->header.oem_id, "SGI", 3))
  454. return 0;
  455. if ((mcfg->header.revision >= 1) && (dmi_get_bios_year() >= 2010))
  456. return 0;
  457. pr_err(PREFIX "MCFG region for %04x [bus %02x-%02x] at %#llx "
  458. "is above 4GB, ignored\n", cfg->pci_segment,
  459. cfg->start_bus_number, cfg->end_bus_number, cfg->address);
  460. return -EINVAL;
  461. }
  462. static int __init pci_parse_mcfg(struct acpi_table_header *header)
  463. {
  464. struct acpi_table_mcfg *mcfg;
  465. struct acpi_mcfg_allocation *cfg_table, *cfg;
  466. unsigned long i;
  467. int entries;
  468. if (!header)
  469. return -EINVAL;
  470. mcfg = (struct acpi_table_mcfg *)header;
  471. /* how many config structures do we have */
  472. free_all_mmcfg();
  473. entries = 0;
  474. i = header->length - sizeof(struct acpi_table_mcfg);
  475. while (i >= sizeof(struct acpi_mcfg_allocation)) {
  476. entries++;
  477. i -= sizeof(struct acpi_mcfg_allocation);
  478. }
  479. if (entries == 0) {
  480. pr_err(PREFIX "MMCONFIG has no entries\n");
  481. return -ENODEV;
  482. }
  483. cfg_table = (struct acpi_mcfg_allocation *) &mcfg[1];
  484. for (i = 0; i < entries; i++) {
  485. cfg = &cfg_table[i];
  486. if (acpi_mcfg_check_entry(mcfg, cfg)) {
  487. free_all_mmcfg();
  488. return -ENODEV;
  489. }
  490. if (pci_mmconfig_add(cfg->pci_segment, cfg->start_bus_number,
  491. cfg->end_bus_number, cfg->address) == NULL) {
  492. pr_warn(PREFIX "no memory for MCFG entries\n");
  493. free_all_mmcfg();
  494. return -ENOMEM;
  495. }
  496. }
  497. return 0;
  498. }
  499. #ifdef CONFIG_ACPI_APEI
  500. extern int (*arch_apei_filter_addr)(int (*func)(__u64 start, __u64 size,
  501. void *data), void *data);
  502. static int pci_mmcfg_for_each_region(int (*func)(__u64 start, __u64 size,
  503. void *data), void *data)
  504. {
  505. struct pci_mmcfg_region *cfg;
  506. int rc;
  507. if (list_empty(&pci_mmcfg_list))
  508. return 0;
  509. list_for_each_entry(cfg, &pci_mmcfg_list, list) {
  510. rc = func(cfg->res.start, resource_size(&cfg->res), data);
  511. if (rc)
  512. return rc;
  513. }
  514. return 0;
  515. }
  516. #define set_apei_filter() (arch_apei_filter_addr = pci_mmcfg_for_each_region)
  517. #else
  518. #define set_apei_filter()
  519. #endif
  520. static void __init __pci_mmcfg_init(int early)
  521. {
  522. pci_mmcfg_reject_broken(early);
  523. if (list_empty(&pci_mmcfg_list))
  524. return;
  525. if (pcibios_last_bus < 0) {
  526. const struct pci_mmcfg_region *cfg;
  527. list_for_each_entry(cfg, &pci_mmcfg_list, list) {
  528. if (cfg->segment)
  529. break;
  530. pcibios_last_bus = cfg->end_bus;
  531. }
  532. }
  533. if (pci_mmcfg_arch_init())
  534. pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
  535. else {
  536. free_all_mmcfg();
  537. pci_mmcfg_arch_init_failed = true;
  538. }
  539. }
  540. static int __initdata known_bridge;
  541. void __init pci_mmcfg_early_init(void)
  542. {
  543. if (pci_probe & PCI_PROBE_MMCONF) {
  544. if (pci_mmcfg_check_hostbridge())
  545. known_bridge = 1;
  546. else
  547. acpi_table_parse(ACPI_SIG_MCFG, pci_parse_mcfg);
  548. __pci_mmcfg_init(1);
  549. set_apei_filter();
  550. }
  551. }
  552. void __init pci_mmcfg_late_init(void)
  553. {
  554. /* MMCONFIG disabled */
  555. if ((pci_probe & PCI_PROBE_MMCONF) == 0)
  556. return;
  557. if (known_bridge)
  558. return;
  559. /* MMCONFIG hasn't been enabled yet, try again */
  560. if (pci_probe & PCI_PROBE_MASK & ~PCI_PROBE_MMCONF) {
  561. acpi_table_parse(ACPI_SIG_MCFG, pci_parse_mcfg);
  562. __pci_mmcfg_init(0);
  563. }
  564. }
  565. static int __init pci_mmcfg_late_insert_resources(void)
  566. {
  567. struct pci_mmcfg_region *cfg;
  568. pci_mmcfg_running_state = true;
  569. /* If we are not using MMCONFIG, don't insert the resources. */
  570. if ((pci_probe & PCI_PROBE_MMCONF) == 0)
  571. return 1;
  572. /*
  573. * Attempt to insert the mmcfg resources but not with the busy flag
  574. * marked so it won't cause request errors when __request_region is
  575. * called.
  576. */
  577. list_for_each_entry(cfg, &pci_mmcfg_list, list)
  578. if (!cfg->res.parent)
  579. insert_resource(&iomem_resource, &cfg->res);
  580. return 0;
  581. }
  582. /*
  583. * Perform MMCONFIG resource insertion after PCI initialization to allow for
  584. * misprogrammed MCFG tables that state larger sizes but actually conflict
  585. * with other system resources.
  586. */
  587. late_initcall(pci_mmcfg_late_insert_resources);
  588. /* Add MMCFG information for host bridges */
  589. int pci_mmconfig_insert(struct device *dev, u16 seg, u8 start, u8 end,
  590. phys_addr_t addr)
  591. {
  592. int rc;
  593. struct resource *tmp = NULL;
  594. struct pci_mmcfg_region *cfg;
  595. if (!(pci_probe & PCI_PROBE_MMCONF) || pci_mmcfg_arch_init_failed)
  596. return -ENODEV;
  597. if (start > end)
  598. return -EINVAL;
  599. mutex_lock(&pci_mmcfg_lock);
  600. cfg = pci_mmconfig_lookup(seg, start);
  601. if (cfg) {
  602. if (cfg->end_bus < end)
  603. dev_info(dev, FW_INFO
  604. "MMCONFIG for "
  605. "domain %04x [bus %02x-%02x] "
  606. "only partially covers this bridge\n",
  607. cfg->segment, cfg->start_bus, cfg->end_bus);
  608. mutex_unlock(&pci_mmcfg_lock);
  609. return -EEXIST;
  610. }
  611. if (!addr) {
  612. mutex_unlock(&pci_mmcfg_lock);
  613. return -EINVAL;
  614. }
  615. rc = -EBUSY;
  616. cfg = pci_mmconfig_alloc(seg, start, end, addr);
  617. if (cfg == NULL) {
  618. dev_warn(dev, "fail to add MMCONFIG (out of memory)\n");
  619. rc = -ENOMEM;
  620. } else if (!pci_mmcfg_check_reserved(dev, cfg, 0)) {
  621. dev_warn(dev, FW_BUG "MMCONFIG %pR isn't reserved\n",
  622. &cfg->res);
  623. } else {
  624. /* Insert resource if it's not in boot stage */
  625. if (pci_mmcfg_running_state)
  626. tmp = insert_resource_conflict(&iomem_resource,
  627. &cfg->res);
  628. if (tmp) {
  629. dev_warn(dev,
  630. "MMCONFIG %pR conflicts with "
  631. "%s %pR\n",
  632. &cfg->res, tmp->name, tmp);
  633. } else if (pci_mmcfg_arch_map(cfg)) {
  634. dev_warn(dev, "fail to map MMCONFIG %pR.\n",
  635. &cfg->res);
  636. } else {
  637. list_add_sorted(cfg);
  638. dev_info(dev, "MMCONFIG at %pR (base %#lx)\n",
  639. &cfg->res, (unsigned long)addr);
  640. cfg = NULL;
  641. rc = 0;
  642. }
  643. }
  644. if (cfg) {
  645. if (cfg->res.parent)
  646. release_resource(&cfg->res);
  647. kfree(cfg);
  648. }
  649. mutex_unlock(&pci_mmcfg_lock);
  650. return rc;
  651. }
  652. /* Delete MMCFG information for host bridges */
  653. int pci_mmconfig_delete(u16 seg, u8 start, u8 end)
  654. {
  655. struct pci_mmcfg_region *cfg;
  656. mutex_lock(&pci_mmcfg_lock);
  657. list_for_each_entry_rcu(cfg, &pci_mmcfg_list, list)
  658. if (cfg->segment == seg && cfg->start_bus == start &&
  659. cfg->end_bus == end) {
  660. list_del_rcu(&cfg->list);
  661. synchronize_rcu();
  662. pci_mmcfg_arch_unmap(cfg);
  663. if (cfg->res.parent)
  664. release_resource(&cfg->res);
  665. mutex_unlock(&pci_mmcfg_lock);
  666. kfree(cfg);
  667. return 0;
  668. }
  669. mutex_unlock(&pci_mmcfg_lock);
  670. return -ENOENT;
  671. }