mmconfig-shared.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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/bitmap.h>
  16. #include <asm/e820.h>
  17. #include "pci.h"
  18. /* aperture is up to 256MB but BIOS may reserve less */
  19. #define MMCONFIG_APER_MIN (2 * 1024*1024)
  20. #define MMCONFIG_APER_MAX (256 * 1024*1024)
  21. /* Indicate if the mmcfg resources have been placed into the resource table. */
  22. static int __initdata pci_mmcfg_resources_inserted;
  23. static const char __init *pci_mmcfg_e7520(void)
  24. {
  25. u32 win;
  26. raw_pci_ops->read(0, 0, PCI_DEVFN(0, 0), 0xce, 2, &win);
  27. win = win & 0xf000;
  28. if(win == 0x0000 || win == 0xf000)
  29. pci_mmcfg_config_num = 0;
  30. else {
  31. pci_mmcfg_config_num = 1;
  32. pci_mmcfg_config = kzalloc(sizeof(pci_mmcfg_config[0]), GFP_KERNEL);
  33. if (!pci_mmcfg_config)
  34. return NULL;
  35. pci_mmcfg_config[0].address = win << 16;
  36. pci_mmcfg_config[0].pci_segment = 0;
  37. pci_mmcfg_config[0].start_bus_number = 0;
  38. pci_mmcfg_config[0].end_bus_number = 255;
  39. }
  40. return "Intel Corporation E7520 Memory Controller Hub";
  41. }
  42. static const char __init *pci_mmcfg_intel_945(void)
  43. {
  44. u32 pciexbar, mask = 0, len = 0;
  45. pci_mmcfg_config_num = 1;
  46. raw_pci_ops->read(0, 0, PCI_DEVFN(0, 0), 0x48, 4, &pciexbar);
  47. /* Enable bit */
  48. if (!(pciexbar & 1))
  49. pci_mmcfg_config_num = 0;
  50. /* Size bits */
  51. switch ((pciexbar >> 1) & 3) {
  52. case 0:
  53. mask = 0xf0000000U;
  54. len = 0x10000000U;
  55. break;
  56. case 1:
  57. mask = 0xf8000000U;
  58. len = 0x08000000U;
  59. break;
  60. case 2:
  61. mask = 0xfc000000U;
  62. len = 0x04000000U;
  63. break;
  64. default:
  65. pci_mmcfg_config_num = 0;
  66. }
  67. /* Errata #2, things break when not aligned on a 256Mb boundary */
  68. /* Can only happen in 64M/128M mode */
  69. if ((pciexbar & mask) & 0x0fffffffU)
  70. pci_mmcfg_config_num = 0;
  71. /* Don't hit the APIC registers and their friends */
  72. if ((pciexbar & mask) >= 0xf0000000U)
  73. pci_mmcfg_config_num = 0;
  74. if (pci_mmcfg_config_num) {
  75. pci_mmcfg_config = kzalloc(sizeof(pci_mmcfg_config[0]), GFP_KERNEL);
  76. if (!pci_mmcfg_config)
  77. return NULL;
  78. pci_mmcfg_config[0].address = pciexbar & mask;
  79. pci_mmcfg_config[0].pci_segment = 0;
  80. pci_mmcfg_config[0].start_bus_number = 0;
  81. pci_mmcfg_config[0].end_bus_number = (len >> 20) - 1;
  82. }
  83. return "Intel Corporation 945G/GZ/P/PL Express Memory Controller Hub";
  84. }
  85. static const char __init *pci_mmcfg_amd_fam10h(void)
  86. {
  87. u32 low, high, address;
  88. u64 base, msr;
  89. int i;
  90. unsigned segnbits = 0, busnbits;
  91. if (!(pci_probe & PCI_CHECK_ENABLE_AMD_MMCONF))
  92. return NULL;
  93. address = MSR_FAM10H_MMIO_CONF_BASE;
  94. if (rdmsr_safe(address, &low, &high))
  95. return NULL;
  96. msr = high;
  97. msr <<= 32;
  98. msr |= low;
  99. /* mmconfig is not enable */
  100. if (!(msr & FAM10H_MMIO_CONF_ENABLE))
  101. return NULL;
  102. base = msr & (FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT);
  103. busnbits = (msr >> FAM10H_MMIO_CONF_BUSRANGE_SHIFT) &
  104. FAM10H_MMIO_CONF_BUSRANGE_MASK;
  105. /*
  106. * only handle bus 0 ?
  107. * need to skip it
  108. */
  109. if (!busnbits)
  110. return NULL;
  111. if (busnbits > 8) {
  112. segnbits = busnbits - 8;
  113. busnbits = 8;
  114. }
  115. pci_mmcfg_config_num = (1 << segnbits);
  116. pci_mmcfg_config = kzalloc(sizeof(pci_mmcfg_config[0]) *
  117. pci_mmcfg_config_num, GFP_KERNEL);
  118. if (!pci_mmcfg_config)
  119. return NULL;
  120. for (i = 0; i < (1 << segnbits); i++) {
  121. pci_mmcfg_config[i].address = base + (1<<28) * i;
  122. pci_mmcfg_config[i].pci_segment = i;
  123. pci_mmcfg_config[i].start_bus_number = 0;
  124. pci_mmcfg_config[i].end_bus_number = (1 << busnbits) - 1;
  125. }
  126. return "AMD Family 10h NB";
  127. }
  128. struct pci_mmcfg_hostbridge_probe {
  129. u32 bus;
  130. u32 devfn;
  131. u32 vendor;
  132. u32 device;
  133. const char *(*probe)(void);
  134. };
  135. static struct pci_mmcfg_hostbridge_probe pci_mmcfg_probes[] __initdata = {
  136. { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
  137. PCI_DEVICE_ID_INTEL_E7520_MCH, pci_mmcfg_e7520 },
  138. { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
  139. PCI_DEVICE_ID_INTEL_82945G_HB, pci_mmcfg_intel_945 },
  140. { 0, PCI_DEVFN(0x18, 0), PCI_VENDOR_ID_AMD,
  141. 0x1200, pci_mmcfg_amd_fam10h },
  142. { 0xff, PCI_DEVFN(0, 0), PCI_VENDOR_ID_AMD,
  143. 0x1200, pci_mmcfg_amd_fam10h },
  144. };
  145. static int __init pci_mmcfg_check_hostbridge(void)
  146. {
  147. u32 l;
  148. u32 bus, devfn;
  149. u16 vendor, device;
  150. int i;
  151. const char *name;
  152. if (!raw_pci_ops)
  153. return 0;
  154. pci_mmcfg_config_num = 0;
  155. pci_mmcfg_config = NULL;
  156. name = NULL;
  157. for (i = 0; !name && i < ARRAY_SIZE(pci_mmcfg_probes); i++) {
  158. bus = pci_mmcfg_probes[i].bus;
  159. devfn = pci_mmcfg_probes[i].devfn;
  160. raw_pci_ops->read(0, bus, devfn, 0, 4, &l);
  161. vendor = l & 0xffff;
  162. device = (l >> 16) & 0xffff;
  163. if (pci_mmcfg_probes[i].vendor == vendor &&
  164. pci_mmcfg_probes[i].device == device)
  165. name = pci_mmcfg_probes[i].probe();
  166. }
  167. if (name) {
  168. printk(KERN_INFO "PCI: Found %s %s MMCONFIG support.\n",
  169. name, pci_mmcfg_config_num ? "with" : "without");
  170. }
  171. return name != NULL;
  172. }
  173. static void __init pci_mmcfg_insert_resources(unsigned long resource_flags)
  174. {
  175. #define PCI_MMCFG_RESOURCE_NAME_LEN 19
  176. int i;
  177. struct resource *res;
  178. char *names;
  179. unsigned num_buses;
  180. res = kcalloc(PCI_MMCFG_RESOURCE_NAME_LEN + sizeof(*res),
  181. pci_mmcfg_config_num, GFP_KERNEL);
  182. if (!res) {
  183. printk(KERN_ERR "PCI: Unable to allocate MMCONFIG resources\n");
  184. return;
  185. }
  186. names = (void *)&res[pci_mmcfg_config_num];
  187. for (i = 0; i < pci_mmcfg_config_num; i++, res++) {
  188. struct acpi_mcfg_allocation *cfg = &pci_mmcfg_config[i];
  189. num_buses = cfg->end_bus_number - cfg->start_bus_number + 1;
  190. res->name = names;
  191. snprintf(names, PCI_MMCFG_RESOURCE_NAME_LEN, "PCI MMCONFIG %u",
  192. cfg->pci_segment);
  193. res->start = cfg->address;
  194. res->end = res->start + (num_buses << 20) - 1;
  195. res->flags = IORESOURCE_MEM | resource_flags;
  196. insert_resource(&iomem_resource, res);
  197. names += PCI_MMCFG_RESOURCE_NAME_LEN;
  198. }
  199. /* Mark that the resources have been inserted. */
  200. pci_mmcfg_resources_inserted = 1;
  201. }
  202. static acpi_status __init check_mcfg_resource(struct acpi_resource *res,
  203. void *data)
  204. {
  205. struct resource *mcfg_res = data;
  206. struct acpi_resource_address64 address;
  207. acpi_status status;
  208. if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
  209. struct acpi_resource_fixed_memory32 *fixmem32 =
  210. &res->data.fixed_memory32;
  211. if (!fixmem32)
  212. return AE_OK;
  213. if ((mcfg_res->start >= fixmem32->address) &&
  214. (mcfg_res->end < (fixmem32->address +
  215. fixmem32->address_length))) {
  216. mcfg_res->flags = 1;
  217. return AE_CTRL_TERMINATE;
  218. }
  219. }
  220. if ((res->type != ACPI_RESOURCE_TYPE_ADDRESS32) &&
  221. (res->type != ACPI_RESOURCE_TYPE_ADDRESS64))
  222. return AE_OK;
  223. status = acpi_resource_to_address64(res, &address);
  224. if (ACPI_FAILURE(status) ||
  225. (address.address_length <= 0) ||
  226. (address.resource_type != ACPI_MEMORY_RANGE))
  227. return AE_OK;
  228. if ((mcfg_res->start >= address.minimum) &&
  229. (mcfg_res->end < (address.minimum + address.address_length))) {
  230. mcfg_res->flags = 1;
  231. return AE_CTRL_TERMINATE;
  232. }
  233. return AE_OK;
  234. }
  235. static acpi_status __init find_mboard_resource(acpi_handle handle, u32 lvl,
  236. void *context, void **rv)
  237. {
  238. struct resource *mcfg_res = context;
  239. acpi_walk_resources(handle, METHOD_NAME__CRS,
  240. check_mcfg_resource, context);
  241. if (mcfg_res->flags)
  242. return AE_CTRL_TERMINATE;
  243. return AE_OK;
  244. }
  245. static int __init is_acpi_reserved(u64 start, u64 end, unsigned not_used)
  246. {
  247. struct resource mcfg_res;
  248. mcfg_res.start = start;
  249. mcfg_res.end = end;
  250. mcfg_res.flags = 0;
  251. acpi_get_devices("PNP0C01", find_mboard_resource, &mcfg_res, NULL);
  252. if (!mcfg_res.flags)
  253. acpi_get_devices("PNP0C02", find_mboard_resource, &mcfg_res,
  254. NULL);
  255. return mcfg_res.flags;
  256. }
  257. typedef int (*check_reserved_t)(u64 start, u64 end, unsigned type);
  258. static int __init is_mmconf_reserved(check_reserved_t is_reserved,
  259. u64 addr, u64 size, int i,
  260. typeof(pci_mmcfg_config[0]) *cfg, int with_e820)
  261. {
  262. u64 old_size = size;
  263. int valid = 0;
  264. while (!is_reserved(addr, addr + size - 1, E820_RESERVED)) {
  265. size >>= 1;
  266. if (size < (16UL<<20))
  267. break;
  268. }
  269. if (size >= (16UL<<20) || size == old_size) {
  270. printk(KERN_NOTICE
  271. "PCI: MCFG area at %Lx reserved in %s\n",
  272. addr, with_e820?"E820":"ACPI motherboard resources");
  273. valid = 1;
  274. if (old_size != size) {
  275. /* update end_bus_number */
  276. cfg->end_bus_number = cfg->start_bus_number + ((size>>20) - 1);
  277. printk(KERN_NOTICE "PCI: updated MCFG configuration %d: base %lx "
  278. "segment %hu buses %u - %u\n",
  279. i, (unsigned long)cfg->address, cfg->pci_segment,
  280. (unsigned int)cfg->start_bus_number,
  281. (unsigned int)cfg->end_bus_number);
  282. }
  283. }
  284. return valid;
  285. }
  286. static void __init pci_mmcfg_reject_broken(int early)
  287. {
  288. typeof(pci_mmcfg_config[0]) *cfg;
  289. int i;
  290. if ((pci_mmcfg_config_num == 0) ||
  291. (pci_mmcfg_config == NULL) ||
  292. (pci_mmcfg_config[0].address == 0))
  293. return;
  294. cfg = &pci_mmcfg_config[0];
  295. for (i = 0; i < pci_mmcfg_config_num; i++) {
  296. int valid = 0;
  297. u64 addr, size;
  298. cfg = &pci_mmcfg_config[i];
  299. addr = cfg->start_bus_number;
  300. addr <<= 20;
  301. addr += cfg->address;
  302. size = cfg->end_bus_number + 1 - cfg->start_bus_number;
  303. size <<= 20;
  304. printk(KERN_NOTICE "PCI: MCFG configuration %d: base %lx "
  305. "segment %hu buses %u - %u\n",
  306. i, (unsigned long)cfg->address, cfg->pci_segment,
  307. (unsigned int)cfg->start_bus_number,
  308. (unsigned int)cfg->end_bus_number);
  309. if (!early)
  310. valid = is_mmconf_reserved(is_acpi_reserved, addr, size, i, cfg, 0);
  311. if (valid)
  312. continue;
  313. if (!early)
  314. printk(KERN_ERR "PCI: BIOS Bug: MCFG area at %Lx is not"
  315. " reserved in ACPI motherboard resources\n",
  316. cfg->address);
  317. /* Don't try to do this check unless configuration
  318. type 1 is available. how about type 2 ?*/
  319. if (raw_pci_ops)
  320. valid = is_mmconf_reserved(e820_all_mapped, addr, size, i, cfg, 1);
  321. if (!valid)
  322. goto reject;
  323. }
  324. return;
  325. reject:
  326. printk(KERN_INFO "PCI: Not using MMCONFIG.\n");
  327. pci_mmcfg_arch_free();
  328. kfree(pci_mmcfg_config);
  329. pci_mmcfg_config = NULL;
  330. pci_mmcfg_config_num = 0;
  331. }
  332. static int __initdata known_bridge;
  333. static void __init __pci_mmcfg_init(int early)
  334. {
  335. /* MMCONFIG disabled */
  336. if ((pci_probe & PCI_PROBE_MMCONF) == 0)
  337. return;
  338. /* MMCONFIG already enabled */
  339. if (!early && !(pci_probe & PCI_PROBE_MASK & ~PCI_PROBE_MMCONF))
  340. return;
  341. /* for late to exit */
  342. if (known_bridge)
  343. return;
  344. if (early) {
  345. if (pci_mmcfg_check_hostbridge())
  346. known_bridge = 1;
  347. }
  348. if (!known_bridge) {
  349. acpi_table_parse(ACPI_SIG_MCFG, acpi_parse_mcfg);
  350. pci_mmcfg_reject_broken(early);
  351. }
  352. if ((pci_mmcfg_config_num == 0) ||
  353. (pci_mmcfg_config == NULL) ||
  354. (pci_mmcfg_config[0].address == 0))
  355. return;
  356. if (pci_mmcfg_arch_init()) {
  357. if (known_bridge)
  358. pci_mmcfg_insert_resources(IORESOURCE_BUSY);
  359. pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
  360. } else {
  361. /*
  362. * Signal not to attempt to insert mmcfg resources because
  363. * the architecture mmcfg setup could not initialize.
  364. */
  365. pci_mmcfg_resources_inserted = 1;
  366. }
  367. }
  368. void __init pci_mmcfg_early_init(void)
  369. {
  370. __pci_mmcfg_init(1);
  371. }
  372. void __init pci_mmcfg_late_init(void)
  373. {
  374. __pci_mmcfg_init(0);
  375. }
  376. static int __init pci_mmcfg_late_insert_resources(void)
  377. {
  378. /*
  379. * If resources are already inserted or we are not using MMCONFIG,
  380. * don't insert the resources.
  381. */
  382. if ((pci_mmcfg_resources_inserted == 1) ||
  383. (pci_probe & PCI_PROBE_MMCONF) == 0 ||
  384. (pci_mmcfg_config_num == 0) ||
  385. (pci_mmcfg_config == NULL) ||
  386. (pci_mmcfg_config[0].address == 0))
  387. return 1;
  388. /*
  389. * Attempt to insert the mmcfg resources but not with the busy flag
  390. * marked so it won't cause request errors when __request_region is
  391. * called.
  392. */
  393. pci_mmcfg_insert_resources(0);
  394. return 0;
  395. }
  396. /*
  397. * Perform MMCONFIG resource insertion after PCI initialization to allow for
  398. * misprogrammed MCFG tables that state larger sizes but actually conflict
  399. * with other system resources.
  400. */
  401. late_initcall(pci_mmcfg_late_insert_resources);