mmconfig-shared.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. pci_direct_conf1.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. pci_direct_conf1.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. address = MSR_FAM10H_MMIO_CONF_BASE;
  92. if (rdmsr_safe(address, &low, &high))
  93. return NULL;
  94. msr = high;
  95. msr <<= 32;
  96. msr |= low;
  97. /* mmconfig is not enable */
  98. if (!(msr & FAM10H_MMIO_CONF_ENABLE))
  99. return NULL;
  100. base = msr & (FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT);
  101. busnbits = (msr >> FAM10H_MMIO_CONF_BUSRANGE_SHIFT) &
  102. FAM10H_MMIO_CONF_BUSRANGE_MASK;
  103. /*
  104. * only handle bus 0 ?
  105. * need to skip it
  106. */
  107. if (!busnbits)
  108. return NULL;
  109. if (busnbits > 8) {
  110. segnbits = busnbits - 8;
  111. busnbits = 8;
  112. }
  113. pci_mmcfg_config_num = (1 << segnbits);
  114. pci_mmcfg_config = kzalloc(sizeof(pci_mmcfg_config[0]) *
  115. pci_mmcfg_config_num, GFP_KERNEL);
  116. if (!pci_mmcfg_config)
  117. return NULL;
  118. for (i = 0; i < (1 << segnbits); i++) {
  119. pci_mmcfg_config[i].address = base + (1<<28) * i;
  120. pci_mmcfg_config[i].pci_segment = i;
  121. pci_mmcfg_config[i].start_bus_number = 0;
  122. pci_mmcfg_config[i].end_bus_number = (1 << busnbits) - 1;
  123. }
  124. return "AMD Family 10h NB";
  125. }
  126. struct pci_mmcfg_hostbridge_probe {
  127. u32 bus;
  128. u32 devfn;
  129. u32 vendor;
  130. u32 device;
  131. const char *(*probe)(void);
  132. };
  133. static struct pci_mmcfg_hostbridge_probe pci_mmcfg_probes[] __initdata = {
  134. { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
  135. PCI_DEVICE_ID_INTEL_E7520_MCH, pci_mmcfg_e7520 },
  136. { 0, PCI_DEVFN(0, 0), PCI_VENDOR_ID_INTEL,
  137. PCI_DEVICE_ID_INTEL_82945G_HB, pci_mmcfg_intel_945 },
  138. { 0, PCI_DEVFN(0x18, 0), PCI_VENDOR_ID_AMD,
  139. 0x1200, pci_mmcfg_amd_fam10h },
  140. { 0xff, PCI_DEVFN(0, 0), PCI_VENDOR_ID_AMD,
  141. 0x1200, pci_mmcfg_amd_fam10h },
  142. };
  143. static int __init pci_mmcfg_check_hostbridge(void)
  144. {
  145. u32 l;
  146. u32 bus, devfn;
  147. u16 vendor, device;
  148. int i;
  149. const char *name;
  150. pci_mmcfg_config_num = 0;
  151. pci_mmcfg_config = NULL;
  152. name = NULL;
  153. for (i = 0; !name && i < ARRAY_SIZE(pci_mmcfg_probes); i++) {
  154. bus = pci_mmcfg_probes[i].bus;
  155. devfn = pci_mmcfg_probes[i].devfn;
  156. pci_direct_conf1.read(0, bus, devfn, 0, 4, &l);
  157. vendor = l & 0xffff;
  158. device = (l >> 16) & 0xffff;
  159. if (pci_mmcfg_probes[i].vendor == vendor &&
  160. pci_mmcfg_probes[i].device == device)
  161. name = pci_mmcfg_probes[i].probe();
  162. }
  163. if (name) {
  164. printk(KERN_INFO "PCI: Found %s %s MMCONFIG support.\n",
  165. name, pci_mmcfg_config_num ? "with" : "without");
  166. }
  167. return name != NULL;
  168. }
  169. static void __init pci_mmcfg_insert_resources(unsigned long resource_flags)
  170. {
  171. #define PCI_MMCFG_RESOURCE_NAME_LEN 19
  172. int i;
  173. struct resource *res;
  174. char *names;
  175. unsigned num_buses;
  176. res = kcalloc(PCI_MMCFG_RESOURCE_NAME_LEN + sizeof(*res),
  177. pci_mmcfg_config_num, GFP_KERNEL);
  178. if (!res) {
  179. printk(KERN_ERR "PCI: Unable to allocate MMCONFIG resources\n");
  180. return;
  181. }
  182. names = (void *)&res[pci_mmcfg_config_num];
  183. for (i = 0; i < pci_mmcfg_config_num; i++, res++) {
  184. struct acpi_mcfg_allocation *cfg = &pci_mmcfg_config[i];
  185. num_buses = cfg->end_bus_number - cfg->start_bus_number + 1;
  186. res->name = names;
  187. snprintf(names, PCI_MMCFG_RESOURCE_NAME_LEN, "PCI MMCONFIG %u",
  188. cfg->pci_segment);
  189. res->start = cfg->address;
  190. res->end = res->start + (num_buses << 20) - 1;
  191. res->flags = IORESOURCE_MEM | resource_flags;
  192. insert_resource(&iomem_resource, res);
  193. names += PCI_MMCFG_RESOURCE_NAME_LEN;
  194. }
  195. /* Mark that the resources have been inserted. */
  196. pci_mmcfg_resources_inserted = 1;
  197. }
  198. static acpi_status __init check_mcfg_resource(struct acpi_resource *res,
  199. void *data)
  200. {
  201. struct resource *mcfg_res = data;
  202. struct acpi_resource_address64 address;
  203. acpi_status status;
  204. if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
  205. struct acpi_resource_fixed_memory32 *fixmem32 =
  206. &res->data.fixed_memory32;
  207. if (!fixmem32)
  208. return AE_OK;
  209. if ((mcfg_res->start >= fixmem32->address) &&
  210. (mcfg_res->end < (fixmem32->address +
  211. fixmem32->address_length))) {
  212. mcfg_res->flags = 1;
  213. return AE_CTRL_TERMINATE;
  214. }
  215. }
  216. if ((res->type != ACPI_RESOURCE_TYPE_ADDRESS32) &&
  217. (res->type != ACPI_RESOURCE_TYPE_ADDRESS64))
  218. return AE_OK;
  219. status = acpi_resource_to_address64(res, &address);
  220. if (ACPI_FAILURE(status) ||
  221. (address.address_length <= 0) ||
  222. (address.resource_type != ACPI_MEMORY_RANGE))
  223. return AE_OK;
  224. if ((mcfg_res->start >= address.minimum) &&
  225. (mcfg_res->end < (address.minimum + address.address_length))) {
  226. mcfg_res->flags = 1;
  227. return AE_CTRL_TERMINATE;
  228. }
  229. return AE_OK;
  230. }
  231. static acpi_status __init find_mboard_resource(acpi_handle handle, u32 lvl,
  232. void *context, void **rv)
  233. {
  234. struct resource *mcfg_res = context;
  235. acpi_walk_resources(handle, METHOD_NAME__CRS,
  236. check_mcfg_resource, context);
  237. if (mcfg_res->flags)
  238. return AE_CTRL_TERMINATE;
  239. return AE_OK;
  240. }
  241. static int __init is_acpi_reserved(unsigned long start, unsigned long end)
  242. {
  243. struct resource mcfg_res;
  244. mcfg_res.start = start;
  245. mcfg_res.end = end;
  246. mcfg_res.flags = 0;
  247. acpi_get_devices("PNP0C01", find_mboard_resource, &mcfg_res, NULL);
  248. if (!mcfg_res.flags)
  249. acpi_get_devices("PNP0C02", find_mboard_resource, &mcfg_res,
  250. NULL);
  251. return mcfg_res.flags;
  252. }
  253. static void __init pci_mmcfg_reject_broken(int type, int early)
  254. {
  255. typeof(pci_mmcfg_config[0]) *cfg;
  256. int i;
  257. if ((pci_mmcfg_config_num == 0) ||
  258. (pci_mmcfg_config == NULL) ||
  259. (pci_mmcfg_config[0].address == 0))
  260. return;
  261. cfg = &pci_mmcfg_config[0];
  262. for (i = 0; i < pci_mmcfg_config_num; i++) {
  263. int valid = 0;
  264. u32 size = (cfg->end_bus_number + 1) << 20;
  265. cfg = &pci_mmcfg_config[i];
  266. printk(KERN_NOTICE "PCI: MCFG configuration %d: base %lx "
  267. "segment %hu buses %u - %u\n",
  268. i, (unsigned long)cfg->address, cfg->pci_segment,
  269. (unsigned int)cfg->start_bus_number,
  270. (unsigned int)cfg->end_bus_number);
  271. if (!early &&
  272. is_acpi_reserved(cfg->address, cfg->address + size - 1)) {
  273. printk(KERN_NOTICE "PCI: MCFG area at %Lx reserved "
  274. "in ACPI motherboard resources\n",
  275. cfg->address);
  276. valid = 1;
  277. }
  278. if (valid)
  279. continue;
  280. if (!early)
  281. printk(KERN_ERR "PCI: BIOS Bug: MCFG area at %Lx is not"
  282. " reserved in ACPI motherboard resources\n",
  283. cfg->address);
  284. /* Don't try to do this check unless configuration
  285. type 1 is available. */
  286. if (type == 1 && e820_all_mapped(cfg->address,
  287. cfg->address + size - 1,
  288. E820_RESERVED)) {
  289. printk(KERN_NOTICE
  290. "PCI: MCFG area at %Lx reserved in E820\n",
  291. cfg->address);
  292. valid = 1;
  293. }
  294. if (!valid)
  295. goto reject;
  296. }
  297. return;
  298. reject:
  299. printk(KERN_ERR "PCI: Not using MMCONFIG.\n");
  300. pci_mmcfg_arch_free();
  301. kfree(pci_mmcfg_config);
  302. pci_mmcfg_config = NULL;
  303. pci_mmcfg_config_num = 0;
  304. }
  305. static int __initdata known_bridge;
  306. void __init __pci_mmcfg_init(int type, int early)
  307. {
  308. /* MMCONFIG disabled */
  309. if ((pci_probe & PCI_PROBE_MMCONF) == 0)
  310. return;
  311. /* MMCONFIG already enabled */
  312. if (!early && !(pci_probe & PCI_PROBE_MASK & ~PCI_PROBE_MMCONF))
  313. return;
  314. /* for late to exit */
  315. if (known_bridge)
  316. return;
  317. if (early && type == 1) {
  318. if (pci_mmcfg_check_hostbridge())
  319. known_bridge = 1;
  320. }
  321. if (!known_bridge) {
  322. acpi_table_parse(ACPI_SIG_MCFG, acpi_parse_mcfg);
  323. pci_mmcfg_reject_broken(type, early);
  324. }
  325. if ((pci_mmcfg_config_num == 0) ||
  326. (pci_mmcfg_config == NULL) ||
  327. (pci_mmcfg_config[0].address == 0))
  328. return;
  329. if (pci_mmcfg_arch_init()) {
  330. if (known_bridge)
  331. pci_mmcfg_insert_resources(IORESOURCE_BUSY);
  332. pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
  333. } else {
  334. /*
  335. * Signal not to attempt to insert mmcfg resources because
  336. * the architecture mmcfg setup could not initialize.
  337. */
  338. pci_mmcfg_resources_inserted = 1;
  339. }
  340. }
  341. void __init pci_mmcfg_early_init(int type)
  342. {
  343. __pci_mmcfg_init(type, 1);
  344. }
  345. void __init pci_mmcfg_late_init(void)
  346. {
  347. int type = 0;
  348. if (pci_probe & PCI_PROBE_CONF1)
  349. type = 1;
  350. __pci_mmcfg_init(type, 0);
  351. }
  352. static int __init pci_mmcfg_late_insert_resources(void)
  353. {
  354. /*
  355. * If resources are already inserted or we are not using MMCONFIG,
  356. * don't insert the resources.
  357. */
  358. if ((pci_mmcfg_resources_inserted == 1) ||
  359. (pci_probe & PCI_PROBE_MMCONF) == 0 ||
  360. (pci_mmcfg_config_num == 0) ||
  361. (pci_mmcfg_config == NULL) ||
  362. (pci_mmcfg_config[0].address == 0))
  363. return 1;
  364. /*
  365. * Attempt to insert the mmcfg resources but not with the busy flag
  366. * marked so it won't cause request errors when __request_region is
  367. * called.
  368. */
  369. pci_mmcfg_insert_resources(0);
  370. return 0;
  371. }
  372. /*
  373. * Perform MMCONFIG resource insertion after PCI initialization to allow for
  374. * misprogrammed MCFG tables that state larger sizes but actually conflict
  375. * with other system resources.
  376. */
  377. late_initcall(pci_mmcfg_late_insert_resources);