mmconf-fam10h_64.c 5.4 KB

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