mmconfig-shared.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. struct pci_mmcfg_hostbridge_probe {
  86. u32 vendor;
  87. u32 device;
  88. const char *(*probe)(void);
  89. };
  90. static struct pci_mmcfg_hostbridge_probe pci_mmcfg_probes[] __initdata = {
  91. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_E7520_MCH, pci_mmcfg_e7520 },
  92. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82945G_HB, pci_mmcfg_intel_945 },
  93. };
  94. static int __init pci_mmcfg_check_hostbridge(void)
  95. {
  96. u32 l;
  97. u16 vendor, device;
  98. int i;
  99. const char *name;
  100. pci_direct_conf1.read(0, 0, PCI_DEVFN(0,0), 0, 4, &l);
  101. vendor = l & 0xffff;
  102. device = (l >> 16) & 0xffff;
  103. pci_mmcfg_config_num = 0;
  104. pci_mmcfg_config = NULL;
  105. name = NULL;
  106. for (i = 0; !name && i < ARRAY_SIZE(pci_mmcfg_probes); i++) {
  107. if (pci_mmcfg_probes[i].vendor == vendor &&
  108. pci_mmcfg_probes[i].device == device)
  109. name = pci_mmcfg_probes[i].probe();
  110. }
  111. if (name) {
  112. printk(KERN_INFO "PCI: Found %s %s MMCONFIG support.\n",
  113. name, pci_mmcfg_config_num ? "with" : "without");
  114. }
  115. return name != NULL;
  116. }
  117. static void __init pci_mmcfg_insert_resources(unsigned long resource_flags)
  118. {
  119. #define PCI_MMCFG_RESOURCE_NAME_LEN 19
  120. int i;
  121. struct resource *res;
  122. char *names;
  123. unsigned num_buses;
  124. res = kcalloc(PCI_MMCFG_RESOURCE_NAME_LEN + sizeof(*res),
  125. pci_mmcfg_config_num, GFP_KERNEL);
  126. if (!res) {
  127. printk(KERN_ERR "PCI: Unable to allocate MMCONFIG resources\n");
  128. return;
  129. }
  130. names = (void *)&res[pci_mmcfg_config_num];
  131. for (i = 0; i < pci_mmcfg_config_num; i++, res++) {
  132. struct acpi_mcfg_allocation *cfg = &pci_mmcfg_config[i];
  133. num_buses = cfg->end_bus_number - cfg->start_bus_number + 1;
  134. res->name = names;
  135. snprintf(names, PCI_MMCFG_RESOURCE_NAME_LEN, "PCI MMCONFIG %u",
  136. cfg->pci_segment);
  137. res->start = cfg->address;
  138. res->end = res->start + (num_buses << 20) - 1;
  139. res->flags = IORESOURCE_MEM | resource_flags;
  140. insert_resource(&iomem_resource, res);
  141. names += PCI_MMCFG_RESOURCE_NAME_LEN;
  142. }
  143. /* Mark that the resources have been inserted. */
  144. pci_mmcfg_resources_inserted = 1;
  145. }
  146. static void __init pci_mmcfg_reject_broken(int type)
  147. {
  148. typeof(pci_mmcfg_config[0]) *cfg;
  149. if ((pci_mmcfg_config_num == 0) ||
  150. (pci_mmcfg_config == NULL) ||
  151. (pci_mmcfg_config[0].address == 0))
  152. return;
  153. cfg = &pci_mmcfg_config[0];
  154. /*
  155. * Handle more broken MCFG tables on Asus etc.
  156. * They only contain a single entry for bus 0-0.
  157. */
  158. if (pci_mmcfg_config_num == 1 &&
  159. cfg->pci_segment == 0 &&
  160. (cfg->start_bus_number | cfg->end_bus_number) == 0) {
  161. printk(KERN_ERR "PCI: start and end of bus number is 0. "
  162. "Rejected as broken MCFG.\n");
  163. goto reject;
  164. }
  165. /*
  166. * Only do this check when type 1 works. If it doesn't work
  167. * assume we run on a Mac and always use MCFG
  168. */
  169. if (type == 1 && !e820_all_mapped(cfg->address,
  170. cfg->address + MMCONFIG_APER_MIN,
  171. E820_RESERVED)) {
  172. printk(KERN_ERR "PCI: BIOS Bug: MCFG area at %Lx is not"
  173. " E820-reserved\n", cfg->address);
  174. goto reject;
  175. }
  176. return;
  177. reject:
  178. printk(KERN_ERR "PCI: Not using MMCONFIG.\n");
  179. kfree(pci_mmcfg_config);
  180. pci_mmcfg_config = NULL;
  181. pci_mmcfg_config_num = 0;
  182. }
  183. void __init pci_mmcfg_init(int type)
  184. {
  185. int known_bridge = 0;
  186. if ((pci_probe & PCI_PROBE_MMCONF) == 0)
  187. return;
  188. if (type == 1 && pci_mmcfg_check_hostbridge())
  189. known_bridge = 1;
  190. if (!known_bridge) {
  191. acpi_table_parse(ACPI_SIG_MCFG, acpi_parse_mcfg);
  192. pci_mmcfg_reject_broken(type);
  193. }
  194. if ((pci_mmcfg_config_num == 0) ||
  195. (pci_mmcfg_config == NULL) ||
  196. (pci_mmcfg_config[0].address == 0))
  197. return;
  198. if (pci_mmcfg_arch_init()) {
  199. if (known_bridge)
  200. pci_mmcfg_insert_resources(IORESOURCE_BUSY);
  201. pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
  202. } else {
  203. /*
  204. * Signal not to attempt to insert mmcfg resources because
  205. * the architecture mmcfg setup could not initialize.
  206. */
  207. pci_mmcfg_resources_inserted = 1;
  208. }
  209. }
  210. static int __init pci_mmcfg_late_insert_resources(void)
  211. {
  212. /*
  213. * If resources are already inserted or we are not using MMCONFIG,
  214. * don't insert the resources.
  215. */
  216. if ((pci_mmcfg_resources_inserted == 1) ||
  217. (pci_probe & PCI_PROBE_MMCONF) == 0 ||
  218. (pci_mmcfg_config_num == 0) ||
  219. (pci_mmcfg_config == NULL) ||
  220. (pci_mmcfg_config[0].address == 0))
  221. return 1;
  222. /*
  223. * Attempt to insert the mmcfg resources but not with the busy flag
  224. * marked so it won't cause request errors when __request_region is
  225. * called.
  226. */
  227. pci_mmcfg_insert_resources(0);
  228. return 0;
  229. }
  230. /*
  231. * Perform MMCONFIG resource insertion after PCI initialization to allow for
  232. * misprogrammed MCFG tables that state larger sizes but actually conflict
  233. * with other system resources.
  234. */
  235. late_initcall(pci_mmcfg_late_insert_resources);