1761fdd5eSTony W Wang-oc // SPDX-License-Identifier: GPL-2.0
2761fdd5eSTony W Wang-oc #include <linux/sched.h>
3761fdd5eSTony W Wang-oc #include <linux/sched/clock.h>
4761fdd5eSTony W Wang-oc
5*5d510359SSean Christopherson #include <asm/cpu.h>
6761fdd5eSTony W Wang-oc #include <asm/cpufeature.h>
7761fdd5eSTony W Wang-oc
8761fdd5eSTony W Wang-oc #include "cpu.h"
9761fdd5eSTony W Wang-oc
10761fdd5eSTony W Wang-oc #define MSR_ZHAOXIN_FCR57 0x00001257
11761fdd5eSTony W Wang-oc
12761fdd5eSTony W Wang-oc #define ACE_PRESENT (1 << 6)
13761fdd5eSTony W Wang-oc #define ACE_ENABLED (1 << 7)
14761fdd5eSTony W Wang-oc #define ACE_FCR (1 << 7) /* MSR_ZHAOXIN_FCR */
15761fdd5eSTony W Wang-oc
16761fdd5eSTony W Wang-oc #define RNG_PRESENT (1 << 2)
17761fdd5eSTony W Wang-oc #define RNG_ENABLED (1 << 3)
18761fdd5eSTony W Wang-oc #define RNG_ENABLE (1 << 8) /* MSR_ZHAOXIN_RNG */
19761fdd5eSTony W Wang-oc
init_zhaoxin_cap(struct cpuinfo_x86 * c)20761fdd5eSTony W Wang-oc static void init_zhaoxin_cap(struct cpuinfo_x86 *c)
21761fdd5eSTony W Wang-oc {
22761fdd5eSTony W Wang-oc u32 lo, hi;
23761fdd5eSTony W Wang-oc
24761fdd5eSTony W Wang-oc /* Test for Extended Feature Flags presence */
25761fdd5eSTony W Wang-oc if (cpuid_eax(0xC0000000) >= 0xC0000001) {
26761fdd5eSTony W Wang-oc u32 tmp = cpuid_edx(0xC0000001);
27761fdd5eSTony W Wang-oc
28761fdd5eSTony W Wang-oc /* Enable ACE unit, if present and disabled */
29761fdd5eSTony W Wang-oc if ((tmp & (ACE_PRESENT | ACE_ENABLED)) == ACE_PRESENT) {
30761fdd5eSTony W Wang-oc rdmsr(MSR_ZHAOXIN_FCR57, lo, hi);
31761fdd5eSTony W Wang-oc /* Enable ACE unit */
32761fdd5eSTony W Wang-oc lo |= ACE_FCR;
33761fdd5eSTony W Wang-oc wrmsr(MSR_ZHAOXIN_FCR57, lo, hi);
34761fdd5eSTony W Wang-oc pr_info("CPU: Enabled ACE h/w crypto\n");
35761fdd5eSTony W Wang-oc }
36761fdd5eSTony W Wang-oc
37761fdd5eSTony W Wang-oc /* Enable RNG unit, if present and disabled */
38761fdd5eSTony W Wang-oc if ((tmp & (RNG_PRESENT | RNG_ENABLED)) == RNG_PRESENT) {
39761fdd5eSTony W Wang-oc rdmsr(MSR_ZHAOXIN_FCR57, lo, hi);
40761fdd5eSTony W Wang-oc /* Enable RNG unit */
41761fdd5eSTony W Wang-oc lo |= RNG_ENABLE;
42761fdd5eSTony W Wang-oc wrmsr(MSR_ZHAOXIN_FCR57, lo, hi);
43761fdd5eSTony W Wang-oc pr_info("CPU: Enabled h/w RNG\n");
44761fdd5eSTony W Wang-oc }
45761fdd5eSTony W Wang-oc
46761fdd5eSTony W Wang-oc /*
47761fdd5eSTony W Wang-oc * Store Extended Feature Flags as word 5 of the CPU
48761fdd5eSTony W Wang-oc * capability bit array
49761fdd5eSTony W Wang-oc */
50761fdd5eSTony W Wang-oc c->x86_capability[CPUID_C000_0001_EDX] = cpuid_edx(0xC0000001);
51761fdd5eSTony W Wang-oc }
52761fdd5eSTony W Wang-oc
53761fdd5eSTony W Wang-oc if (c->x86 >= 0x6)
54761fdd5eSTony W Wang-oc set_cpu_cap(c, X86_FEATURE_REP_GOOD);
55761fdd5eSTony W Wang-oc }
56761fdd5eSTony W Wang-oc
early_init_zhaoxin(struct cpuinfo_x86 * c)57761fdd5eSTony W Wang-oc static void early_init_zhaoxin(struct cpuinfo_x86 *c)
58761fdd5eSTony W Wang-oc {
59761fdd5eSTony W Wang-oc if (c->x86 >= 0x6)
60761fdd5eSTony W Wang-oc set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
61761fdd5eSTony W Wang-oc #ifdef CONFIG_X86_64
62761fdd5eSTony W Wang-oc set_cpu_cap(c, X86_FEATURE_SYSENTER32);
63761fdd5eSTony W Wang-oc #endif
64761fdd5eSTony W Wang-oc if (c->x86_power & (1 << 8)) {
65761fdd5eSTony W Wang-oc set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
66761fdd5eSTony W Wang-oc set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC);
67761fdd5eSTony W Wang-oc }
68761fdd5eSTony W Wang-oc
69761fdd5eSTony W Wang-oc if (c->cpuid_level >= 0x00000001) {
70761fdd5eSTony W Wang-oc u32 eax, ebx, ecx, edx;
71761fdd5eSTony W Wang-oc
72761fdd5eSTony W Wang-oc cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
73761fdd5eSTony W Wang-oc /*
74761fdd5eSTony W Wang-oc * If HTT (EDX[28]) is set EBX[16:23] contain the number of
75761fdd5eSTony W Wang-oc * apicids which are reserved per package. Store the resulting
76761fdd5eSTony W Wang-oc * shift value for the package management code.
77761fdd5eSTony W Wang-oc */
78761fdd5eSTony W Wang-oc if (edx & (1U << 28))
79761fdd5eSTony W Wang-oc c->x86_coreid_bits = get_count_order((ebx >> 16) & 0xff);
80761fdd5eSTony W Wang-oc }
81761fdd5eSTony W Wang-oc
82761fdd5eSTony W Wang-oc }
83761fdd5eSTony W Wang-oc
init_zhaoxin(struct cpuinfo_x86 * c)84761fdd5eSTony W Wang-oc static void init_zhaoxin(struct cpuinfo_x86 *c)
85761fdd5eSTony W Wang-oc {
86761fdd5eSTony W Wang-oc early_init_zhaoxin(c);
87761fdd5eSTony W Wang-oc init_intel_cacheinfo(c);
88761fdd5eSTony W Wang-oc detect_num_cpu_cores(c);
89761fdd5eSTony W Wang-oc #ifdef CONFIG_X86_32
90761fdd5eSTony W Wang-oc detect_ht(c);
91761fdd5eSTony W Wang-oc #endif
92761fdd5eSTony W Wang-oc
93761fdd5eSTony W Wang-oc if (c->cpuid_level > 9) {
94761fdd5eSTony W Wang-oc unsigned int eax = cpuid_eax(10);
95761fdd5eSTony W Wang-oc
96761fdd5eSTony W Wang-oc /*
97761fdd5eSTony W Wang-oc * Check for version and the number of counters
98761fdd5eSTony W Wang-oc * Version(eax[7:0]) can't be 0;
99761fdd5eSTony W Wang-oc * Counters(eax[15:8]) should be greater than 1;
100761fdd5eSTony W Wang-oc */
101761fdd5eSTony W Wang-oc if ((eax & 0xff) && (((eax >> 8) & 0xff) > 1))
102761fdd5eSTony W Wang-oc set_cpu_cap(c, X86_FEATURE_ARCH_PERFMON);
103761fdd5eSTony W Wang-oc }
104761fdd5eSTony W Wang-oc
105761fdd5eSTony W Wang-oc if (c->x86 >= 0x6)
106761fdd5eSTony W Wang-oc init_zhaoxin_cap(c);
107761fdd5eSTony W Wang-oc #ifdef CONFIG_X86_64
108761fdd5eSTony W Wang-oc set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC);
109761fdd5eSTony W Wang-oc #endif
110761fdd5eSTony W Wang-oc
1117d37953bSSean Christopherson init_ia32_feat_ctl(c);
112761fdd5eSTony W Wang-oc }
113761fdd5eSTony W Wang-oc
114761fdd5eSTony W Wang-oc #ifdef CONFIG_X86_32
115761fdd5eSTony W Wang-oc static unsigned int
zhaoxin_size_cache(struct cpuinfo_x86 * c,unsigned int size)116761fdd5eSTony W Wang-oc zhaoxin_size_cache(struct cpuinfo_x86 *c, unsigned int size)
117761fdd5eSTony W Wang-oc {
118761fdd5eSTony W Wang-oc return size;
119761fdd5eSTony W Wang-oc }
120761fdd5eSTony W Wang-oc #endif
121761fdd5eSTony W Wang-oc
122761fdd5eSTony W Wang-oc static const struct cpu_dev zhaoxin_cpu_dev = {
123761fdd5eSTony W Wang-oc .c_vendor = "zhaoxin",
124761fdd5eSTony W Wang-oc .c_ident = { " Shanghai " },
125761fdd5eSTony W Wang-oc .c_early_init = early_init_zhaoxin,
126761fdd5eSTony W Wang-oc .c_init = init_zhaoxin,
127761fdd5eSTony W Wang-oc #ifdef CONFIG_X86_32
128761fdd5eSTony W Wang-oc .legacy_cache_size = zhaoxin_size_cache,
129761fdd5eSTony W Wang-oc #endif
130761fdd5eSTony W Wang-oc .c_x86_vendor = X86_VENDOR_ZHAOXIN,
131761fdd5eSTony W Wang-oc };
132761fdd5eSTony W Wang-oc
133761fdd5eSTony W Wang-oc cpu_dev_register(zhaoxin_cpu_dev);
134