mmconfig-shared.c 18 KB

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