mmconf-fam10h_64.c 5.3 KB

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