mmconfig-shared.c 19 KB

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