hygon.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 <[email protected]>
  8. */
  9. #include <linux/io.h>
  10. #include <asm/cpu.h>
  11. #include <asm/smp.h>
  12. #include <asm/numa.h>
  13. #include <asm/cacheinfo.h>
  14. #include <asm/spec-ctrl.h>
  15. #include <asm/delay.h>
  16. #include "cpu.h"
  17. #define APICID_SOCKET_ID_BIT 6
  18. /*
  19. * nodes_per_socket: Stores the number of nodes per socket.
  20. * Refer to CPUID Fn8000_001E_ECX Node Identifiers[10:8]
  21. */
  22. static u32 nodes_per_socket = 1;
  23. #ifdef CONFIG_NUMA
  24. /*
  25. * To workaround broken NUMA config. Read the comment in
  26. * srat_detect_node().
  27. */
  28. static int nearby_node(int apicid)
  29. {
  30. int i, node;
  31. for (i = apicid - 1; i >= 0; i--) {
  32. node = __apicid_to_node[i];
  33. if (node != NUMA_NO_NODE && node_online(node))
  34. return node;
  35. }
  36. for (i = apicid + 1; i < MAX_LOCAL_APIC; i++) {
  37. node = __apicid_to_node[i];
  38. if (node != NUMA_NO_NODE && node_online(node))
  39. return node;
  40. }
  41. return first_node(node_online_map); /* Shouldn't happen */
  42. }
  43. #endif
  44. static void hygon_get_topology_early(struct cpuinfo_x86 *c)
  45. {
  46. if (cpu_has(c, X86_FEATURE_TOPOEXT))
  47. smp_num_siblings = ((cpuid_ebx(0x8000001e) >> 8) & 0xff) + 1;
  48. }
  49. /*
  50. * Fixup core topology information for
  51. * (1) Hygon multi-node processors
  52. * Assumption: Number of cores in each internal node is the same.
  53. * (2) Hygon processors supporting compute units
  54. */
  55. static void hygon_get_topology(struct cpuinfo_x86 *c)
  56. {
  57. int cpu = smp_processor_id();
  58. /* get information required for multi-node processors */
  59. if (boot_cpu_has(X86_FEATURE_TOPOEXT)) {
  60. int err;
  61. u32 eax, ebx, ecx, edx;
  62. cpuid(0x8000001e, &eax, &ebx, &ecx, &edx);
  63. c->cpu_die_id = ecx & 0xff;
  64. c->cpu_core_id = ebx & 0xff;
  65. if (smp_num_siblings > 1)
  66. c->x86_max_cores /= smp_num_siblings;
  67. /*
  68. * In case leaf B is available, use it to derive
  69. * topology information.
  70. */
  71. err = detect_extended_topology(c);
  72. if (!err)
  73. c->x86_coreid_bits = get_count_order(c->x86_max_cores);
  74. /*
  75. * Socket ID is ApicId[6] for the processors with model <= 0x3
  76. * when running on host.
  77. */
  78. if (!boot_cpu_has(X86_FEATURE_HYPERVISOR) && c->x86_model <= 0x3)
  79. c->phys_proc_id = c->apicid >> APICID_SOCKET_ID_BIT;
  80. cacheinfo_hygon_init_llc_id(c, cpu);
  81. } else if (cpu_has(c, X86_FEATURE_NODEID_MSR)) {
  82. u64 value;
  83. rdmsrl(MSR_FAM10H_NODE_ID, value);
  84. c->cpu_die_id = value & 7;
  85. per_cpu(cpu_llc_id, cpu) = c->cpu_die_id;
  86. } else
  87. return;
  88. if (nodes_per_socket > 1)
  89. set_cpu_cap(c, X86_FEATURE_AMD_DCM);
  90. }
  91. /*
  92. * On Hygon setup the lower bits of the APIC id distinguish the cores.
  93. * Assumes number of cores is a power of two.
  94. */
  95. static void hygon_detect_cmp(struct cpuinfo_x86 *c)
  96. {
  97. unsigned int bits;
  98. int cpu = smp_processor_id();
  99. bits = c->x86_coreid_bits;
  100. /* Low order bits define the core id (index of core in socket) */
  101. c->cpu_core_id = c->initial_apicid & ((1 << bits)-1);
  102. /* Convert the initial APIC ID into the socket ID */
  103. c->phys_proc_id = c->initial_apicid >> bits;
  104. /* use socket ID also for last level cache */
  105. per_cpu(cpu_llc_id, cpu) = c->cpu_die_id = c->phys_proc_id;
  106. }
  107. static void srat_detect_node(struct cpuinfo_x86 *c)
  108. {
  109. #ifdef CONFIG_NUMA
  110. int cpu = smp_processor_id();
  111. int node;
  112. unsigned int apicid = c->apicid;
  113. node = numa_cpu_node(cpu);
  114. if (node == NUMA_NO_NODE)
  115. node = per_cpu(cpu_llc_id, cpu);
  116. /*
  117. * On multi-fabric platform (e.g. Numascale NumaChip) a
  118. * platform-specific handler needs to be called to fixup some
  119. * IDs of the CPU.
  120. */
  121. if (x86_cpuinit.fixup_cpu_id)
  122. x86_cpuinit.fixup_cpu_id(c, node);
  123. if (!node_online(node)) {
  124. /*
  125. * Two possibilities here:
  126. *
  127. * - The CPU is missing memory and no node was created. In
  128. * that case try picking one from a nearby CPU.
  129. *
  130. * - The APIC IDs differ from the HyperTransport node IDs.
  131. * Assume they are all increased by a constant offset, but
  132. * in the same order as the HT nodeids. If that doesn't
  133. * result in a usable node fall back to the path for the
  134. * previous case.
  135. *
  136. * This workaround operates directly on the mapping between
  137. * APIC ID and NUMA node, assuming certain relationship
  138. * between APIC ID, HT node ID and NUMA topology. As going
  139. * through CPU mapping may alter the outcome, directly
  140. * access __apicid_to_node[].
  141. */
  142. int ht_nodeid = c->initial_apicid;
  143. if (__apicid_to_node[ht_nodeid] != NUMA_NO_NODE)
  144. node = __apicid_to_node[ht_nodeid];
  145. /* Pick a nearby node */
  146. if (!node_online(node))
  147. node = nearby_node(apicid);
  148. }
  149. numa_set_node(cpu, node);
  150. #endif
  151. }
  152. static void early_init_hygon_mc(struct cpuinfo_x86 *c)
  153. {
  154. #ifdef CONFIG_SMP
  155. unsigned int bits, ecx;
  156. /* Multi core CPU? */
  157. if (c->extended_cpuid_level < 0x80000008)
  158. return;
  159. ecx = cpuid_ecx(0x80000008);
  160. c->x86_max_cores = (ecx & 0xff) + 1;
  161. /* CPU telling us the core id bits shift? */
  162. bits = (ecx >> 12) & 0xF;
  163. /* Otherwise recompute */
  164. if (bits == 0) {
  165. while ((1 << bits) < c->x86_max_cores)
  166. bits++;
  167. }
  168. c->x86_coreid_bits = bits;
  169. #endif
  170. }
  171. static void bsp_init_hygon(struct cpuinfo_x86 *c)
  172. {
  173. if (cpu_has(c, X86_FEATURE_CONSTANT_TSC)) {
  174. u64 val;
  175. rdmsrl(MSR_K7_HWCR, val);
  176. if (!(val & BIT(24)))
  177. pr_warn(FW_BUG "TSC doesn't count with P0 frequency!\n");
  178. }
  179. if (cpu_has(c, X86_FEATURE_MWAITX))
  180. use_mwaitx_delay();
  181. if (boot_cpu_has(X86_FEATURE_TOPOEXT)) {
  182. u32 ecx;
  183. ecx = cpuid_ecx(0x8000001e);
  184. __max_die_per_package = nodes_per_socket = ((ecx >> 8) & 7) + 1;
  185. } else if (boot_cpu_has(X86_FEATURE_NODEID_MSR)) {
  186. u64 value;
  187. rdmsrl(MSR_FAM10H_NODE_ID, value);
  188. __max_die_per_package = nodes_per_socket = ((value >> 3) & 7) + 1;
  189. }
  190. if (!boot_cpu_has(X86_FEATURE_AMD_SSBD) &&
  191. !boot_cpu_has(X86_FEATURE_VIRT_SSBD)) {
  192. /*
  193. * Try to cache the base value so further operations can
  194. * avoid RMW. If that faults, do not enable SSBD.
  195. */
  196. if (!rdmsrl_safe(MSR_AMD64_LS_CFG, &x86_amd_ls_cfg_base)) {
  197. setup_force_cpu_cap(X86_FEATURE_LS_CFG_SSBD);
  198. setup_force_cpu_cap(X86_FEATURE_SSBD);
  199. x86_amd_ls_cfg_ssbd_mask = 1ULL << 10;
  200. }
  201. }
  202. }
  203. static void early_init_hygon(struct cpuinfo_x86 *c)
  204. {
  205. u32 dummy;
  206. early_init_hygon_mc(c);
  207. set_cpu_cap(c, X86_FEATURE_K8);
  208. rdmsr_safe(MSR_AMD64_PATCH_LEVEL, &c->microcode, &dummy);
  209. /*
  210. * c->x86_power is 8000_0007 edx. Bit 8 is TSC runs at constant rate
  211. * with P/T states and does not stop in deep C-states
  212. */
  213. if (c->x86_power & (1 << 8)) {
  214. set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
  215. set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC);
  216. }
  217. /* Bit 12 of 8000_0007 edx is accumulated power mechanism. */
  218. if (c->x86_power & BIT(12))
  219. set_cpu_cap(c, X86_FEATURE_ACC_POWER);
  220. /* Bit 14 indicates the Runtime Average Power Limit interface. */
  221. if (c->x86_power & BIT(14))
  222. set_cpu_cap(c, X86_FEATURE_RAPL);
  223. #ifdef CONFIG_X86_64
  224. set_cpu_cap(c, X86_FEATURE_SYSCALL32);
  225. #endif
  226. #if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_PCI)
  227. /*
  228. * ApicID can always be treated as an 8-bit value for Hygon APIC So, we
  229. * can safely set X86_FEATURE_EXTD_APICID unconditionally.
  230. */
  231. if (boot_cpu_has(X86_FEATURE_APIC))
  232. set_cpu_cap(c, X86_FEATURE_EXTD_APICID);
  233. #endif
  234. /*
  235. * This is only needed to tell the kernel whether to use VMCALL
  236. * and VMMCALL. VMMCALL is never executed except under virt, so
  237. * we can set it unconditionally.
  238. */
  239. set_cpu_cap(c, X86_FEATURE_VMMCALL);
  240. hygon_get_topology_early(c);
  241. }
  242. static void init_hygon(struct cpuinfo_x86 *c)
  243. {
  244. early_init_hygon(c);
  245. /*
  246. * Bit 31 in normal CPUID used for nonstandard 3DNow ID;
  247. * 3DNow is IDd by bit 31 in extended CPUID (1*32+31) anyway
  248. */
  249. clear_cpu_cap(c, 0*32+31);
  250. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  251. /* get apicid instead of initial apic id from cpuid */
  252. c->apicid = hard_smp_processor_id();
  253. /*
  254. * XXX someone from Hygon needs to confirm this DTRT
  255. *
  256. init_spectral_chicken(c);
  257. */
  258. set_cpu_cap(c, X86_FEATURE_ZEN);
  259. set_cpu_cap(c, X86_FEATURE_CPB);
  260. cpu_detect_cache_sizes(c);
  261. hygon_detect_cmp(c);
  262. hygon_get_topology(c);
  263. srat_detect_node(c);
  264. init_hygon_cacheinfo(c);
  265. if (cpu_has(c, X86_FEATURE_XMM2)) {
  266. /*
  267. * Use LFENCE for execution serialization. On families which
  268. * don't have that MSR, LFENCE is already serializing.
  269. * msr_set_bit() uses the safe accessors, too, even if the MSR
  270. * is not present.
  271. */
  272. msr_set_bit(MSR_AMD64_DE_CFG,
  273. MSR_AMD64_DE_CFG_LFENCE_SERIALIZE_BIT);
  274. /* A serializing LFENCE stops RDTSC speculation */
  275. set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC);
  276. }
  277. /*
  278. * Hygon processors have APIC timer running in deep C states.
  279. */
  280. set_cpu_cap(c, X86_FEATURE_ARAT);
  281. /* Hygon CPUs don't reset SS attributes on SYSRET, Xen does. */
  282. if (!cpu_has(c, X86_FEATURE_XENPV))
  283. set_cpu_bug(c, X86_BUG_SYSRET_SS_ATTRS);
  284. check_null_seg_clears_base(c);
  285. }
  286. static void cpu_detect_tlb_hygon(struct cpuinfo_x86 *c)
  287. {
  288. u32 ebx, eax, ecx, edx;
  289. u16 mask = 0xfff;
  290. if (c->extended_cpuid_level < 0x80000006)
  291. return;
  292. cpuid(0x80000006, &eax, &ebx, &ecx, &edx);
  293. tlb_lld_4k[ENTRIES] = (ebx >> 16) & mask;
  294. tlb_lli_4k[ENTRIES] = ebx & mask;
  295. /* Handle DTLB 2M and 4M sizes, fall back to L1 if L2 is disabled */
  296. if (!((eax >> 16) & mask))
  297. tlb_lld_2m[ENTRIES] = (cpuid_eax(0x80000005) >> 16) & 0xff;
  298. else
  299. tlb_lld_2m[ENTRIES] = (eax >> 16) & mask;
  300. /* a 4M entry uses two 2M entries */
  301. tlb_lld_4m[ENTRIES] = tlb_lld_2m[ENTRIES] >> 1;
  302. /* Handle ITLB 2M and 4M sizes, fall back to L1 if L2 is disabled */
  303. if (!(eax & mask)) {
  304. cpuid(0x80000005, &eax, &ebx, &ecx, &edx);
  305. tlb_lli_2m[ENTRIES] = eax & 0xff;
  306. } else
  307. tlb_lli_2m[ENTRIES] = eax & mask;
  308. tlb_lli_4m[ENTRIES] = tlb_lli_2m[ENTRIES] >> 1;
  309. }
  310. static const struct cpu_dev hygon_cpu_dev = {
  311. .c_vendor = "Hygon",
  312. .c_ident = { "HygonGenuine" },
  313. .c_early_init = early_init_hygon,
  314. .c_detect_tlb = cpu_detect_tlb_hygon,
  315. .c_bsp_init = bsp_init_hygon,
  316. .c_init = init_hygon,
  317. .c_x86_vendor = X86_VENDOR_HYGON,
  318. };
  319. cpu_dev_register(hygon_cpu_dev);