hygon.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Hygon Processor Support for Linux
  4. *
  5. * Copyright (C) 2018 Chengdu Haiguang IC Design Co., Ltd.
  6. *
  7. * Author: Pu Wen <puwen@hygon.cn>
  8. */
  9. #include <linux/io.h>
  10. #include <asm/apic.h>
  11. #include <asm/cpu.h>
  12. #include <asm/smp.h>
  13. #include <asm/numa.h>
  14. #include <asm/cacheinfo.h>
  15. #include <asm/spec-ctrl.h>
  16. #include <asm/delay.h>
  17. #include <asm/resctrl.h>
  18. #include "cpu.h"
  19. #ifdef CONFIG_NUMA
  20. /*
  21. * To workaround broken NUMA config. Read the comment in
  22. * srat_detect_node().
  23. */
  24. static int nearby_node(int apicid)
  25. {
  26. int i, node;
  27. for (i = apicid - 1; i >= 0; i--) {
  28. node = __apicid_to_node[i];
  29. if (node != NUMA_NO_NODE && node_online(node))
  30. return node;
  31. }
  32. for (i = apicid + 1; i < MAX_LOCAL_APIC; i++) {
  33. node = __apicid_to_node[i];
  34. if (node != NUMA_NO_NODE && node_online(node))
  35. return node;
  36. }
  37. return first_node(node_online_map); /* Shouldn't happen */
  38. }
  39. #endif
  40. static void srat_detect_node(struct cpuinfo_x86 *c)
  41. {
  42. #ifdef CONFIG_NUMA
  43. int cpu = smp_processor_id();
  44. int node;
  45. unsigned int apicid = c->topo.apicid;
  46. node = numa_cpu_node(cpu);
  47. if (node == NUMA_NO_NODE)
  48. node = c->topo.llc_id;
  49. /*
  50. * On multi-fabric platform (e.g. Numascale NumaChip) a
  51. * platform-specific handler needs to be called to fixup some
  52. * IDs of the CPU.
  53. */
  54. if (x86_cpuinit.fixup_cpu_id)
  55. x86_cpuinit.fixup_cpu_id(c, node);
  56. if (!node_online(node)) {
  57. /*
  58. * Two possibilities here:
  59. *
  60. * - The CPU is missing memory and no node was created. In
  61. * that case try picking one from a nearby CPU.
  62. *
  63. * - The APIC IDs differ from the HyperTransport node IDs.
  64. * Assume they are all increased by a constant offset, but
  65. * in the same order as the HT nodeids. If that doesn't
  66. * result in a usable node fall back to the path for the
  67. * previous case.
  68. *
  69. * This workaround operates directly on the mapping between
  70. * APIC ID and NUMA node, assuming certain relationship
  71. * between APIC ID, HT node ID and NUMA topology. As going
  72. * through CPU mapping may alter the outcome, directly
  73. * access __apicid_to_node[].
  74. */
  75. int ht_nodeid = c->topo.initial_apicid;
  76. if (__apicid_to_node[ht_nodeid] != NUMA_NO_NODE)
  77. node = __apicid_to_node[ht_nodeid];
  78. /* Pick a nearby node */
  79. if (!node_online(node))
  80. node = nearby_node(apicid);
  81. }
  82. numa_set_node(cpu, node);
  83. #endif
  84. }
  85. static void bsp_init_hygon(struct cpuinfo_x86 *c)
  86. {
  87. if (cpu_has(c, X86_FEATURE_CONSTANT_TSC)) {
  88. u64 val;
  89. rdmsrl(MSR_K7_HWCR, val);
  90. if (!(val & BIT(24)))
  91. pr_warn(FW_BUG "TSC doesn't count with P0 frequency!\n");
  92. }
  93. if (cpu_has(c, X86_FEATURE_MWAITX))
  94. use_mwaitx_delay();
  95. if (!boot_cpu_has(X86_FEATURE_AMD_SSBD) &&
  96. !boot_cpu_has(X86_FEATURE_VIRT_SSBD)) {
  97. /*
  98. * Try to cache the base value so further operations can
  99. * avoid RMW. If that faults, do not enable SSBD.
  100. */
  101. if (!rdmsrl_safe(MSR_AMD64_LS_CFG, &x86_amd_ls_cfg_base)) {
  102. setup_force_cpu_cap(X86_FEATURE_LS_CFG_SSBD);
  103. setup_force_cpu_cap(X86_FEATURE_SSBD);
  104. x86_amd_ls_cfg_ssbd_mask = 1ULL << 10;
  105. }
  106. }
  107. resctrl_cpu_detect(c);
  108. }
  109. static void early_init_hygon(struct cpuinfo_x86 *c)
  110. {
  111. u32 dummy;
  112. set_cpu_cap(c, X86_FEATURE_K8);
  113. rdmsr_safe(MSR_AMD64_PATCH_LEVEL, &c->microcode, &dummy);
  114. /*
  115. * c->x86_power is 8000_0007 edx. Bit 8 is TSC runs at constant rate
  116. * with P/T states and does not stop in deep C-states
  117. */
  118. if (c->x86_power & (1 << 8)) {
  119. set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
  120. set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC);
  121. }
  122. /* Bit 12 of 8000_0007 edx is accumulated power mechanism. */
  123. if (c->x86_power & BIT(12))
  124. set_cpu_cap(c, X86_FEATURE_ACC_POWER);
  125. /* Bit 14 indicates the Runtime Average Power Limit interface. */
  126. if (c->x86_power & BIT(14))
  127. set_cpu_cap(c, X86_FEATURE_RAPL);
  128. #ifdef CONFIG_X86_64
  129. set_cpu_cap(c, X86_FEATURE_SYSCALL32);
  130. #endif
  131. #if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_PCI)
  132. /*
  133. * ApicID can always be treated as an 8-bit value for Hygon APIC So, we
  134. * can safely set X86_FEATURE_EXTD_APICID unconditionally.
  135. */
  136. if (boot_cpu_has(X86_FEATURE_APIC))
  137. set_cpu_cap(c, X86_FEATURE_EXTD_APICID);
  138. #endif
  139. /*
  140. * This is only needed to tell the kernel whether to use VMCALL
  141. * and VMMCALL. VMMCALL is never executed except under virt, so
  142. * we can set it unconditionally.
  143. */
  144. set_cpu_cap(c, X86_FEATURE_VMMCALL);
  145. }
  146. static void init_hygon(struct cpuinfo_x86 *c)
  147. {
  148. u64 vm_cr;
  149. early_init_hygon(c);
  150. /*
  151. * Bit 31 in normal CPUID used for nonstandard 3DNow ID;
  152. * 3DNow is IDd by bit 31 in extended CPUID (1*32+31) anyway
  153. */
  154. clear_cpu_cap(c, 0*32+31);
  155. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  156. /*
  157. * XXX someone from Hygon needs to confirm this DTRT
  158. *
  159. init_spectral_chicken(c);
  160. */
  161. set_cpu_cap(c, X86_FEATURE_ZEN);
  162. set_cpu_cap(c, X86_FEATURE_CPB);
  163. cpu_detect_cache_sizes(c);
  164. srat_detect_node(c);
  165. init_hygon_cacheinfo(c);
  166. if (cpu_has(c, X86_FEATURE_SVM)) {
  167. rdmsrl(MSR_VM_CR, vm_cr);
  168. if (vm_cr & SVM_VM_CR_SVM_DIS_MASK) {
  169. pr_notice_once("SVM disabled (by BIOS) in MSR_VM_CR\n");
  170. clear_cpu_cap(c, X86_FEATURE_SVM);
  171. }
  172. }
  173. if (cpu_has(c, X86_FEATURE_XMM2)) {
  174. /*
  175. * Use LFENCE for execution serialization. On families which
  176. * don't have that MSR, LFENCE is already serializing.
  177. * msr_set_bit() uses the safe accessors, too, even if the MSR
  178. * is not present.
  179. */
  180. msr_set_bit(MSR_AMD64_DE_CFG,
  181. MSR_AMD64_DE_CFG_LFENCE_SERIALIZE_BIT);
  182. /* A serializing LFENCE stops RDTSC speculation */
  183. set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC);
  184. }
  185. /*
  186. * Hygon processors have APIC timer running in deep C states.
  187. */
  188. set_cpu_cap(c, X86_FEATURE_ARAT);
  189. /* Hygon CPUs don't reset SS attributes on SYSRET, Xen does. */
  190. if (!cpu_feature_enabled(X86_FEATURE_XENPV))
  191. set_cpu_bug(c, X86_BUG_SYSRET_SS_ATTRS);
  192. check_null_seg_clears_base(c);
  193. /* Hygon CPUs don't need fencing after x2APIC/TSC_DEADLINE MSR writes. */
  194. clear_cpu_cap(c, X86_FEATURE_APIC_MSRS_FENCE);
  195. }
  196. static void cpu_detect_tlb_hygon(struct cpuinfo_x86 *c)
  197. {
  198. u32 ebx, eax, ecx, edx;
  199. u16 mask = 0xfff;
  200. if (c->extended_cpuid_level < 0x80000006)
  201. return;
  202. cpuid(0x80000006, &eax, &ebx, &ecx, &edx);
  203. tlb_lld_4k[ENTRIES] = (ebx >> 16) & mask;
  204. tlb_lli_4k[ENTRIES] = ebx & mask;
  205. /* Handle DTLB 2M and 4M sizes, fall back to L1 if L2 is disabled */
  206. if (!((eax >> 16) & mask))
  207. tlb_lld_2m[ENTRIES] = (cpuid_eax(0x80000005) >> 16) & 0xff;
  208. else
  209. tlb_lld_2m[ENTRIES] = (eax >> 16) & mask;
  210. /* a 4M entry uses two 2M entries */
  211. tlb_lld_4m[ENTRIES] = tlb_lld_2m[ENTRIES] >> 1;
  212. /* Handle ITLB 2M and 4M sizes, fall back to L1 if L2 is disabled */
  213. if (!(eax & mask)) {
  214. cpuid(0x80000005, &eax, &ebx, &ecx, &edx);
  215. tlb_lli_2m[ENTRIES] = eax & 0xff;
  216. } else
  217. tlb_lli_2m[ENTRIES] = eax & mask;
  218. tlb_lli_4m[ENTRIES] = tlb_lli_2m[ENTRIES] >> 1;
  219. }
  220. static const struct cpu_dev hygon_cpu_dev = {
  221. .c_vendor = "Hygon",
  222. .c_ident = { "HygonGenuine" },
  223. .c_early_init = early_init_hygon,
  224. .c_detect_tlb = cpu_detect_tlb_hygon,
  225. .c_bsp_init = bsp_init_hygon,
  226. .c_init = init_hygon,
  227. .c_x86_vendor = X86_VENDOR_HYGON,
  228. };
  229. cpu_dev_register(hygon_cpu_dev);