mmconfig-shared.c 19 KB

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