mmconf-fam10h_64.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * AMD Family 10h mmconfig enablement
  3. */
  4. #include <linux/types.h>
  5. #include <linux/mm.h>
  6. #include <linux/string.h>
  7. #include <linux/pci.h>
  8. #include <linux/dmi.h>
  9. #include <linux/range.h>
  10. #include <asm/pci-direct.h>
  11. #include <linux/sort.h>
  12. #include <asm/io.h>
  13. #include <asm/msr.h>
  14. #include <asm/acpi.h>
  15. #include <asm/mmconfig.h>
  16. #include <asm/pci_x86.h>
  17. struct pci_hostbridge_probe {
  18. u32 bus;
  19. u32 slot;
  20. u32 vendor;
  21. u32 device;
  22. };
  23. static u64 __cpuinitdata fam10h_pci_mmconf_base;
  24. static int __cpuinitdata fam10h_pci_mmconf_base_status;
  25. static struct pci_hostbridge_probe pci_probes[] __cpuinitdata = {
  26. { 0, 0x18, PCI_VENDOR_ID_AMD, 0x1200 },
  27. { 0xff, 0, PCI_VENDOR_ID_AMD, 0x1200 },
  28. };
  29. static int __cpuinit cmp_range(const void *x1, const void *x2)
  30. {
  31. const struct range *r1 = x1;
  32. const struct range *r2 = x2;
  33. int start1, start2;
  34. start1 = r1->start >> 32;
  35. start2 = r2->start >> 32;
  36. return start1 - start2;
  37. }
  38. /*[47:0] */
  39. /* need to avoid (0xfd<<32) and (0xfe<<32), ht used space */
  40. #define FAM10H_PCI_MMCONF_BASE (0xfcULL<<32)
  41. #define BASE_VALID(b) ((b != (0xfdULL << 32)) && (b != (0xfeULL << 32)))
  42. static void __cpuinit get_fam10h_pci_mmconf_base(void)
  43. {
  44. int i;
  45. unsigned bus;
  46. unsigned slot;
  47. int found;
  48. u64 val;
  49. u32 address;
  50. u64 tom2;
  51. u64 base = FAM10H_PCI_MMCONF_BASE;
  52. int hi_mmio_num;
  53. struct range range[8];
  54. /* only try to get setting from BSP */
  55. /* -1 or 1 */
  56. if (fam10h_pci_mmconf_base_status)
  57. return;
  58. if (!early_pci_allowed())
  59. goto fail;
  60. found = 0;
  61. for (i = 0; i < ARRAY_SIZE(pci_probes); i++) {
  62. u32 id;
  63. u16 device;
  64. u16 vendor;
  65. bus = pci_probes[i].bus;
  66. slot = pci_probes[i].slot;
  67. id = read_pci_config(bus, slot, 0, PCI_VENDOR_ID);
  68. vendor = id & 0xffff;
  69. device = (id>>16) & 0xffff;
  70. if (pci_probes[i].vendor == vendor &&
  71. pci_probes[i].device == device) {
  72. found = 1;
  73. break;
  74. }
  75. }
  76. if (!found)
  77. goto fail;
  78. /* SYS_CFG */
  79. address = MSR_K8_SYSCFG;
  80. rdmsrl(address, val);
  81. /* TOP_MEM2 is not enabled? */
  82. if (!(val & (1<<21))) {
  83. tom2 = 0;
  84. } else {
  85. /* TOP_MEM2 */
  86. address = MSR_K8_TOP_MEM2;
  87. rdmsrl(address, val);
  88. tom2 = val & (0xffffULL<<32);
  89. }
  90. if (base <= tom2)
  91. base = tom2 + (1ULL<<32);
  92. /*
  93. * need to check if the range is in the high mmio range that is
  94. * above 4G
  95. */
  96. hi_mmio_num = 0;
  97. for (i = 0; i < 8; i++) {
  98. u32 reg;
  99. u64 start;
  100. u64 end;
  101. reg = read_pci_config(bus, slot, 1, 0x80 + (i << 3));
  102. if (!(reg & 3))
  103. continue;
  104. start = (((u64)reg) << 8) & (0xffULL << 32); /* 39:16 on 31:8*/
  105. reg = read_pci_config(bus, slot, 1, 0x84 + (i << 3));
  106. end = (((u64)reg) << 8) & (0xffULL << 32); /* 39:16 on 31:8*/
  107. if (!end)
  108. continue;
  109. range[hi_mmio_num].start = start;
  110. range[hi_mmio_num].end = end;
  111. hi_mmio_num++;
  112. }
  113. if (!hi_mmio_num)
  114. goto out;
  115. /* sort the range */
  116. sort(range, hi_mmio_num, sizeof(struct range), cmp_range, NULL);
  117. if (range[hi_mmio_num - 1].end < base)
  118. goto out;
  119. if (range[0].start > base)
  120. goto out;
  121. /* need to find one window */
  122. base = range[0].start - (1ULL << 32);
  123. if ((base > tom2) && BASE_VALID(base))
  124. goto out;
  125. base = range[hi_mmio_num - 1].end + (1ULL << 32);
  126. if ((base > tom2) && BASE_VALID(base))
  127. goto out;
  128. /* need to find window between ranges */
  129. if (hi_mmio_num > 1)
  130. for (i = 0; i < hi_mmio_num - 1; i++) {
  131. if (range[i + 1].start > (range[i].end + (1ULL << 32))) {
  132. base = range[i].end + (1ULL << 32);
  133. if ((base > tom2) && BASE_VALID(base))
  134. goto out;
  135. }
  136. }
  137. fail:
  138. fam10h_pci_mmconf_base_status = -1;
  139. return;
  140. out:
  141. fam10h_pci_mmconf_base = base;
  142. fam10h_pci_mmconf_base_status = 1;
  143. }
  144. void __cpuinit fam10h_check_enable_mmcfg(void)
  145. {
  146. u64 val;
  147. u32 address;
  148. if (!(pci_probe & PCI_CHECK_ENABLE_AMD_MMCONF))
  149. return;
  150. address = MSR_FAM10H_MMIO_CONF_BASE;
  151. rdmsrl(address, val);
  152. /* try to make sure that AP's setting is identical to BSP setting */
  153. if (val & FAM10H_MMIO_CONF_ENABLE) {
  154. unsigned busnbits;
  155. busnbits = (val >> FAM10H_MMIO_CONF_BUSRANGE_SHIFT) &
  156. FAM10H_MMIO_CONF_BUSRANGE_MASK;
  157. /* only trust the one handle 256 buses, if acpi=off */
  158. if (!acpi_pci_disabled || busnbits >= 8) {
  159. u64 base;
  160. base = val & (0xffffULL << 32);
  161. if (fam10h_pci_mmconf_base_status <= 0) {
  162. fam10h_pci_mmconf_base = base;
  163. fam10h_pci_mmconf_base_status = 1;
  164. return;
  165. } else if (fam10h_pci_mmconf_base == base)
  166. return;
  167. }
  168. }
  169. /*
  170. * if it is not enabled, try to enable it and assume only one segment
  171. * with 256 buses
  172. */
  173. get_fam10h_pci_mmconf_base();
  174. if (fam10h_pci_mmconf_base_status <= 0)
  175. return;
  176. printk(KERN_INFO "Enable MMCONFIG on AMD Family 10h\n");
  177. val &= ~((FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT) |
  178. (FAM10H_MMIO_CONF_BUSRANGE_MASK<<FAM10H_MMIO_CONF_BUSRANGE_SHIFT));
  179. val |= fam10h_pci_mmconf_base | (8 << FAM10H_MMIO_CONF_BUSRANGE_SHIFT) |
  180. FAM10H_MMIO_CONF_ENABLE;
  181. wrmsrl(address, val);
  182. }
  183. static int __devinit set_check_enable_amd_mmconf(const struct dmi_system_id *d)
  184. {
  185. pci_probe |= PCI_CHECK_ENABLE_AMD_MMCONF;
  186. return 0;
  187. }
  188. static const struct dmi_system_id __cpuinitconst mmconf_dmi_table[] = {
  189. {
  190. .callback = set_check_enable_amd_mmconf,
  191. .ident = "Sun Microsystems Machine",
  192. .matches = {
  193. DMI_MATCH(DMI_SYS_VENDOR, "Sun Microsystems"),
  194. },
  195. },
  196. {}
  197. };
  198. void __cpuinit check_enable_amd_mmconf_dmi(void)
  199. {
  200. dmi_check_system(mmconf_dmi_table);
  201. }