mmconfig-shared.c 18 KB

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