mmconfig-shared.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. DECLARE_BITMAP(pci_mmcfg_fallback_slots, 32*PCI_MMCFG_MAX_CHECK_BUS);
  22. /* K8 systems have some devices (typically in the builtin northbridge)
  23. that are only accessible using type1
  24. Normally this can be expressed in the MCFG by not listing them
  25. and assigning suitable _SEGs, but this isn't implemented in some BIOS.
  26. Instead try to discover all devices on bus 0 that are unreachable using MM
  27. and fallback for them. */
  28. static void __init unreachable_devices(void)
  29. {
  30. int i, bus;
  31. /* Use the max bus number from ACPI here? */
  32. for (bus = 0; bus < PCI_MMCFG_MAX_CHECK_BUS; bus++) {
  33. for (i = 0; i < 32; i++) {
  34. unsigned int devfn = PCI_DEVFN(i, 0);
  35. u32 val1, val2;
  36. pci_conf1_read(0, bus, devfn, 0, 4, &val1);
  37. if (val1 == 0xffffffff)
  38. continue;
  39. if (pci_mmcfg_arch_reachable(0, bus, devfn)) {
  40. raw_pci_ops->read(0, bus, devfn, 0, 4, &val2);
  41. if (val1 == val2)
  42. continue;
  43. }
  44. set_bit(i + 32 * bus, pci_mmcfg_fallback_slots);
  45. printk(KERN_NOTICE "PCI: No mmconfig possible on device"
  46. " %02x:%02x\n", bus, i);
  47. }
  48. }
  49. }
  50. static const char __init *pci_mmcfg_e7520(void)
  51. {
  52. u32 win;
  53. pci_conf1_read(0, 0, PCI_DEVFN(0,0), 0xce, 2, &win);
  54. win = win & 0xf000;
  55. if(win == 0x0000 || win == 0xf000)
  56. pci_mmcfg_config_num = 0;
  57. else {
  58. pci_mmcfg_config_num = 1;
  59. pci_mmcfg_config = kzalloc(sizeof(pci_mmcfg_config[0]), GFP_KERNEL);
  60. if (!pci_mmcfg_config)
  61. return NULL;
  62. pci_mmcfg_config[0].address = win << 16;
  63. pci_mmcfg_config[0].pci_segment = 0;
  64. pci_mmcfg_config[0].start_bus_number = 0;
  65. pci_mmcfg_config[0].end_bus_number = 255;
  66. }
  67. return "Intel Corporation E7520 Memory Controller Hub";
  68. }
  69. static const char __init *pci_mmcfg_intel_945(void)
  70. {
  71. u32 pciexbar, mask = 0, len = 0;
  72. pci_mmcfg_config_num = 1;
  73. pci_conf1_read(0, 0, PCI_DEVFN(0,0), 0x48, 4, &pciexbar);
  74. /* Enable bit */
  75. if (!(pciexbar & 1))
  76. pci_mmcfg_config_num = 0;
  77. /* Size bits */
  78. switch ((pciexbar >> 1) & 3) {
  79. case 0:
  80. mask = 0xf0000000U;
  81. len = 0x10000000U;
  82. break;
  83. case 1:
  84. mask = 0xf8000000U;
  85. len = 0x08000000U;
  86. break;
  87. case 2:
  88. mask = 0xfc000000U;
  89. len = 0x04000000U;
  90. break;
  91. default:
  92. pci_mmcfg_config_num = 0;
  93. }
  94. /* Errata #2, things break when not aligned on a 256Mb boundary */
  95. /* Can only happen in 64M/128M mode */
  96. if ((pciexbar & mask) & 0x0fffffffU)
  97. pci_mmcfg_config_num = 0;
  98. /* Don't hit the APIC registers and their friends */
  99. if ((pciexbar & mask) >= 0xf0000000U)
  100. pci_mmcfg_config_num = 0;
  101. if (pci_mmcfg_config_num) {
  102. pci_mmcfg_config = kzalloc(sizeof(pci_mmcfg_config[0]), GFP_KERNEL);
  103. if (!pci_mmcfg_config)
  104. return NULL;
  105. pci_mmcfg_config[0].address = pciexbar & mask;
  106. pci_mmcfg_config[0].pci_segment = 0;
  107. pci_mmcfg_config[0].start_bus_number = 0;
  108. pci_mmcfg_config[0].end_bus_number = (len >> 20) - 1;
  109. }
  110. return "Intel Corporation 945G/GZ/P/PL Express Memory Controller Hub";
  111. }
  112. struct pci_mmcfg_hostbridge_probe {
  113. u32 vendor;
  114. u32 device;
  115. const char *(*probe)(void);
  116. };
  117. static struct pci_mmcfg_hostbridge_probe pci_mmcfg_probes[] __initdata = {
  118. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_E7520_MCH, pci_mmcfg_e7520 },
  119. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82945G_HB, pci_mmcfg_intel_945 },
  120. };
  121. static int __init pci_mmcfg_check_hostbridge(void)
  122. {
  123. u32 l;
  124. u16 vendor, device;
  125. int i;
  126. const char *name;
  127. pci_conf1_read(0, 0, PCI_DEVFN(0,0), 0, 4, &l);
  128. vendor = l & 0xffff;
  129. device = (l >> 16) & 0xffff;
  130. pci_mmcfg_config_num = 0;
  131. pci_mmcfg_config = NULL;
  132. name = NULL;
  133. for (i = 0; !name && i < ARRAY_SIZE(pci_mmcfg_probes); i++) {
  134. if (pci_mmcfg_probes[i].vendor == vendor &&
  135. pci_mmcfg_probes[i].device == device)
  136. name = pci_mmcfg_probes[i].probe();
  137. }
  138. if (name) {
  139. printk(KERN_INFO "PCI: Found %s %s MMCONFIG support.\n",
  140. name, pci_mmcfg_config_num ? "with" : "without");
  141. }
  142. return name != NULL;
  143. }
  144. static void __init pci_mmcfg_insert_resources(void)
  145. {
  146. #define PCI_MMCFG_RESOURCE_NAME_LEN 19
  147. int i;
  148. struct resource *res;
  149. char *names;
  150. unsigned num_buses;
  151. res = kcalloc(PCI_MMCFG_RESOURCE_NAME_LEN + sizeof(*res),
  152. pci_mmcfg_config_num, GFP_KERNEL);
  153. if (!res) {
  154. printk(KERN_ERR "PCI: Unable to allocate MMCONFIG resources\n");
  155. return;
  156. }
  157. names = (void *)&res[pci_mmcfg_config_num];
  158. for (i = 0; i < pci_mmcfg_config_num; i++, res++) {
  159. struct acpi_mcfg_allocation *cfg = &pci_mmcfg_config[i];
  160. num_buses = cfg->end_bus_number - cfg->start_bus_number + 1;
  161. res->name = names;
  162. snprintf(names, PCI_MMCFG_RESOURCE_NAME_LEN, "PCI MMCONFIG %u",
  163. cfg->pci_segment);
  164. res->start = cfg->address;
  165. res->end = res->start + (num_buses << 20) - 1;
  166. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  167. insert_resource(&iomem_resource, res);
  168. names += PCI_MMCFG_RESOURCE_NAME_LEN;
  169. }
  170. }
  171. static void __init pci_mmcfg_reject_broken(int type)
  172. {
  173. typeof(pci_mmcfg_config[0]) *cfg;
  174. if ((pci_mmcfg_config_num == 0) ||
  175. (pci_mmcfg_config == NULL) ||
  176. (pci_mmcfg_config[0].address == 0))
  177. return;
  178. cfg = &pci_mmcfg_config[0];
  179. /*
  180. * Handle more broken MCFG tables on Asus etc.
  181. * They only contain a single entry for bus 0-0.
  182. */
  183. if (pci_mmcfg_config_num == 1 &&
  184. cfg->pci_segment == 0 &&
  185. (cfg->start_bus_number | cfg->end_bus_number) == 0) {
  186. printk(KERN_ERR "PCI: start and end of bus number is 0. "
  187. "Rejected as broken MCFG.\n");
  188. goto reject;
  189. }
  190. /*
  191. * Only do this check when type 1 works. If it doesn't work
  192. * assume we run on a Mac and always use MCFG
  193. */
  194. if (type == 1 && !e820_all_mapped(cfg->address,
  195. cfg->address + MMCONFIG_APER_MIN,
  196. E820_RESERVED)) {
  197. printk(KERN_ERR "PCI: BIOS Bug: MCFG area at %Lx is not"
  198. " E820-reserved\n", cfg->address);
  199. goto reject;
  200. }
  201. return;
  202. reject:
  203. printk(KERN_ERR "PCI: Not using MMCONFIG.\n");
  204. kfree(pci_mmcfg_config);
  205. pci_mmcfg_config = NULL;
  206. pci_mmcfg_config_num = 0;
  207. }
  208. void __init pci_mmcfg_init(int type)
  209. {
  210. int known_bridge = 0;
  211. if ((pci_probe & PCI_PROBE_MMCONF) == 0)
  212. return;
  213. if (type == 1 && pci_mmcfg_check_hostbridge())
  214. known_bridge = 1;
  215. if (!known_bridge) {
  216. acpi_table_parse(ACPI_SIG_MCFG, acpi_parse_mcfg);
  217. pci_mmcfg_reject_broken(type);
  218. }
  219. if ((pci_mmcfg_config_num == 0) ||
  220. (pci_mmcfg_config == NULL) ||
  221. (pci_mmcfg_config[0].address == 0))
  222. return;
  223. if (pci_mmcfg_arch_init()) {
  224. if (type == 1)
  225. unreachable_devices();
  226. if (known_bridge)
  227. pci_mmcfg_insert_resources();
  228. pci_probe = (pci_probe & ~PCI_PROBE_MASK) | PCI_PROBE_MMCONF;
  229. }
  230. }