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