xref: /openbmc/linux/arch/x86/kernel/cpu/centaur.c (revision fe6daab1ee9dfe7f89974ee6c486cccb0f18a61d)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2acb04058SPeter Zijlstra 
3acb04058SPeter Zijlstra #include <linux/sched.h>
4e6017571SIngo Molnar #include <linux/sched/clock.h>
5edc05e6dSIngo Molnar 
6cd4d09ecSBorislav Petkov #include <asm/cpufeature.h>
766441bd3SIngo Molnar #include <asm/e820/api.h>
8f7627e25SThomas Gleixner #include <asm/mtrr.h>
948f4c485SSebastian Andrzej Siewior #include <asm/msr.h>
10edc05e6dSIngo Molnar 
11f7627e25SThomas Gleixner #include "cpu.h"
12f7627e25SThomas Gleixner 
13f7627e25SThomas Gleixner #define ACE_PRESENT	(1 << 6)
14f7627e25SThomas Gleixner #define ACE_ENABLED	(1 << 7)
15f7627e25SThomas Gleixner #define ACE_FCR		(1 << 28)	/* MSR_VIA_FCR */
16f7627e25SThomas Gleixner 
17f7627e25SThomas Gleixner #define RNG_PRESENT	(1 << 2)
18f7627e25SThomas Gleixner #define RNG_ENABLED	(1 << 3)
19f7627e25SThomas Gleixner #define RNG_ENABLE	(1 << 6)	/* MSR_VIA_RNG */
20f7627e25SThomas Gleixner 
21148f9bb8SPaul Gortmaker static void init_c3(struct cpuinfo_x86 *c)
22f7627e25SThomas Gleixner {
23f7627e25SThomas Gleixner 	u32  lo, hi;
24f7627e25SThomas Gleixner 
25f7627e25SThomas Gleixner 	/* Test for Centaur Extended Feature Flags presence */
26f7627e25SThomas Gleixner 	if (cpuid_eax(0xC0000000) >= 0xC0000001) {
27f7627e25SThomas Gleixner 		u32 tmp = cpuid_edx(0xC0000001);
28f7627e25SThomas Gleixner 
29f7627e25SThomas Gleixner 		/* enable ACE unit, if present and disabled */
30f7627e25SThomas Gleixner 		if ((tmp & (ACE_PRESENT | ACE_ENABLED)) == ACE_PRESENT) {
31f7627e25SThomas Gleixner 			rdmsr(MSR_VIA_FCR, lo, hi);
32f7627e25SThomas Gleixner 			lo |= ACE_FCR;		/* enable ACE unit */
33f7627e25SThomas Gleixner 			wrmsr(MSR_VIA_FCR, lo, hi);
341b74dde7SChen Yucong 			pr_info("CPU: Enabled ACE h/w crypto\n");
35f7627e25SThomas Gleixner 		}
36f7627e25SThomas Gleixner 
37f7627e25SThomas Gleixner 		/* enable RNG unit, if present and disabled */
38f7627e25SThomas Gleixner 		if ((tmp & (RNG_PRESENT | RNG_ENABLED)) == RNG_PRESENT) {
39f7627e25SThomas Gleixner 			rdmsr(MSR_VIA_RNG, lo, hi);
40f7627e25SThomas Gleixner 			lo |= RNG_ENABLE;	/* enable RNG unit */
41f7627e25SThomas Gleixner 			wrmsr(MSR_VIA_RNG, lo, hi);
421b74dde7SChen Yucong 			pr_info("CPU: Enabled h/w RNG\n");
43f7627e25SThomas Gleixner 		}
44f7627e25SThomas Gleixner 
45f7627e25SThomas Gleixner 		/* store Centaur Extended Feature Flags as
46f7627e25SThomas Gleixner 		 * word 5 of the CPU capability bit array
47f7627e25SThomas Gleixner 		 */
4839c06df4SBorislav Petkov 		c->x86_capability[CPUID_C000_0001_EDX] = cpuid_edx(0xC0000001);
49f7627e25SThomas Gleixner 	}
5048f4c485SSebastian Andrzej Siewior #ifdef CONFIG_X86_32
5127b46d76SSimon Arlott 	/* Cyrix III family needs CX8 & PGE explicitly enabled. */
52cb3f718dSTimo Teräs 	if (c->x86_model >= 6 && c->x86_model <= 13) {
53f7627e25SThomas Gleixner 		rdmsr(MSR_VIA_FCR, lo, hi);
54f7627e25SThomas Gleixner 		lo |= (1<<1 | 1<<7);
55f7627e25SThomas Gleixner 		wrmsr(MSR_VIA_FCR, lo, hi);
56e1a94a97SIngo Molnar 		set_cpu_cap(c, X86_FEATURE_CX8);
57f7627e25SThomas Gleixner 	}
58f7627e25SThomas Gleixner 
59f7627e25SThomas Gleixner 	/* Before Nehemiah, the C3's had 3dNOW! */
60f7627e25SThomas Gleixner 	if (c->x86_model >= 6 && c->x86_model < 9)
61e1a94a97SIngo Molnar 		set_cpu_cap(c, X86_FEATURE_3DNOW);
6248f4c485SSebastian Andrzej Siewior #endif
6348f4c485SSebastian Andrzej Siewior 	if (c->x86 == 0x6 && c->x86_model >= 0xf) {
6448f4c485SSebastian Andrzej Siewior 		c->x86_cache_alignment = c->x86_clflush_size * 2;
6548f4c485SSebastian Andrzej Siewior 		set_cpu_cap(c, X86_FEATURE_REP_GOOD);
6648f4c485SSebastian Andrzej Siewior 	}
67f7627e25SThomas Gleixner 
6827c13eceSBorislav Petkov 	cpu_detect_cache_sizes(c);
69f7627e25SThomas Gleixner }
70f7627e25SThomas Gleixner 
71f7627e25SThomas Gleixner enum {
72f7627e25SThomas Gleixner 		ECX8		= 1<<1,
73f7627e25SThomas Gleixner 		EIERRINT	= 1<<2,
74f7627e25SThomas Gleixner 		DPM		= 1<<3,
75f7627e25SThomas Gleixner 		DMCE		= 1<<4,
76f7627e25SThomas Gleixner 		DSTPCLK		= 1<<5,
77f7627e25SThomas Gleixner 		ELINEAR		= 1<<6,
78f7627e25SThomas Gleixner 		DSMC		= 1<<7,
79f7627e25SThomas Gleixner 		DTLOCK		= 1<<8,
80f7627e25SThomas Gleixner 		EDCTLB		= 1<<8,
81f7627e25SThomas Gleixner 		EMMX		= 1<<9,
82f7627e25SThomas Gleixner 		DPDC		= 1<<11,
83f7627e25SThomas Gleixner 		EBRPRED		= 1<<12,
84f7627e25SThomas Gleixner 		DIC		= 1<<13,
85f7627e25SThomas Gleixner 		DDC		= 1<<14,
86f7627e25SThomas Gleixner 		DNA		= 1<<15,
87f7627e25SThomas Gleixner 		ERETSTK		= 1<<16,
88f7627e25SThomas Gleixner 		E2MMX		= 1<<19,
89f7627e25SThomas Gleixner 		EAMD3D		= 1<<20,
90f7627e25SThomas Gleixner };
91f7627e25SThomas Gleixner 
92148f9bb8SPaul Gortmaker static void early_init_centaur(struct cpuinfo_x86 *c)
935fef55fdSYinghai Lu {
945fef55fdSYinghai Lu 	switch (c->x86) {
9548f4c485SSebastian Andrzej Siewior #ifdef CONFIG_X86_32
965fef55fdSYinghai Lu 	case 5:
975fef55fdSYinghai Lu 		/* Emulate MTRRs using Centaur's MCR. */
985fef55fdSYinghai Lu 		set_cpu_cap(c, X86_FEATURE_CENTAUR_MCR);
995fef55fdSYinghai Lu 		break;
10048f4c485SSebastian Andrzej Siewior #endif
10148f4c485SSebastian Andrzej Siewior 	case 6:
10248f4c485SSebastian Andrzej Siewior 		if (c->x86_model >= 0xf)
10348f4c485SSebastian Andrzej Siewior 			set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
10448f4c485SSebastian Andrzej Siewior 		break;
1055fef55fdSYinghai Lu 	}
10648f4c485SSebastian Andrzej Siewior #ifdef CONFIG_X86_64
10748f4c485SSebastian Andrzej Siewior 	set_cpu_cap(c, X86_FEATURE_SYSENTER32);
10848f4c485SSebastian Andrzej Siewior #endif
109*fe6daab1Sdavidwang 	if (c->x86_power & (1 << 8)) {
110*fe6daab1Sdavidwang 		set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
111*fe6daab1Sdavidwang 		set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC);
112*fe6daab1Sdavidwang 	}
1135fef55fdSYinghai Lu }
1145fef55fdSYinghai Lu 
115148f9bb8SPaul Gortmaker static void init_centaur(struct cpuinfo_x86 *c)
116edc05e6dSIngo Molnar {
11748f4c485SSebastian Andrzej Siewior #ifdef CONFIG_X86_32
118f7627e25SThomas Gleixner 	char *name;
119f7627e25SThomas Gleixner 	u32  fcr_set = 0;
120f7627e25SThomas Gleixner 	u32  fcr_clr = 0;
121f7627e25SThomas Gleixner 	u32  lo, hi, newlo;
122f7627e25SThomas Gleixner 	u32  aa, bb, cc, dd;
123f7627e25SThomas Gleixner 
124edc05e6dSIngo Molnar 	/*
125edc05e6dSIngo Molnar 	 * Bit 31 in normal CPUID used for nonstandard 3DNow ID;
126edc05e6dSIngo Molnar 	 * 3DNow is IDd by bit 31 in extended CPUID (1*32+31) anyway
127edc05e6dSIngo Molnar 	 */
128e1a94a97SIngo Molnar 	clear_cpu_cap(c, 0*32+31);
12948f4c485SSebastian Andrzej Siewior #endif
13048f4c485SSebastian Andrzej Siewior 	early_init_centaur(c);
131f7627e25SThomas Gleixner 	switch (c->x86) {
13248f4c485SSebastian Andrzej Siewior #ifdef CONFIG_X86_32
133f7627e25SThomas Gleixner 	case 5:
134f7627e25SThomas Gleixner 		switch (c->x86_model) {
135f7627e25SThomas Gleixner 		case 4:
136f7627e25SThomas Gleixner 			name = "C6";
137f7627e25SThomas Gleixner 			fcr_set = ECX8|DSMC|EDCTLB|EMMX|ERETSTK;
138f7627e25SThomas Gleixner 			fcr_clr = DPDC;
1391b74dde7SChen Yucong 			pr_notice("Disabling bugged TSC.\n");
140e1a94a97SIngo Molnar 			clear_cpu_cap(c, X86_FEATURE_TSC);
141f7627e25SThomas Gleixner 			break;
142f7627e25SThomas Gleixner 		case 8:
143f7627e25SThomas Gleixner 			switch (c->x86_mask) {
144f7627e25SThomas Gleixner 			default:
145f7627e25SThomas Gleixner 			name = "2";
146f7627e25SThomas Gleixner 				break;
147f7627e25SThomas Gleixner 			case 7 ... 9:
148f7627e25SThomas Gleixner 				name = "2A";
149f7627e25SThomas Gleixner 				break;
150f7627e25SThomas Gleixner 			case 10 ... 15:
151f7627e25SThomas Gleixner 				name = "2B";
152f7627e25SThomas Gleixner 				break;
153f7627e25SThomas Gleixner 			}
154edc05e6dSIngo Molnar 			fcr_set = ECX8|DSMC|DTLOCK|EMMX|EBRPRED|ERETSTK|
155edc05e6dSIngo Molnar 				  E2MMX|EAMD3D;
156f7627e25SThomas Gleixner 			fcr_clr = DPDC;
157f7627e25SThomas Gleixner 			break;
158f7627e25SThomas Gleixner 		case 9:
159f7627e25SThomas Gleixner 			name = "3";
160edc05e6dSIngo Molnar 			fcr_set = ECX8|DSMC|DTLOCK|EMMX|EBRPRED|ERETSTK|
161edc05e6dSIngo Molnar 				  E2MMX|EAMD3D;
162f7627e25SThomas Gleixner 			fcr_clr = DPDC;
163f7627e25SThomas Gleixner 			break;
164f7627e25SThomas Gleixner 		default:
165f7627e25SThomas Gleixner 			name = "??";
166f7627e25SThomas Gleixner 		}
167f7627e25SThomas Gleixner 
168f7627e25SThomas Gleixner 		rdmsr(MSR_IDT_FCR1, lo, hi);
169f7627e25SThomas Gleixner 		newlo = (lo|fcr_set) & (~fcr_clr);
170f7627e25SThomas Gleixner 
171f7627e25SThomas Gleixner 		if (newlo != lo) {
1721b74dde7SChen Yucong 			pr_info("Centaur FCR was 0x%X now 0x%X\n",
173edc05e6dSIngo Molnar 				lo, newlo);
174f7627e25SThomas Gleixner 			wrmsr(MSR_IDT_FCR1, newlo, hi);
175f7627e25SThomas Gleixner 		} else {
1761b74dde7SChen Yucong 			pr_info("Centaur FCR is 0x%X\n", lo);
177f7627e25SThomas Gleixner 		}
178f7627e25SThomas Gleixner 		/* Emulate MTRRs using Centaur's MCR. */
179e1a94a97SIngo Molnar 		set_cpu_cap(c, X86_FEATURE_CENTAUR_MCR);
180f7627e25SThomas Gleixner 		/* Report CX8 */
181e1a94a97SIngo Molnar 		set_cpu_cap(c, X86_FEATURE_CX8);
182f7627e25SThomas Gleixner 		/* Set 3DNow! on Winchip 2 and above. */
183f7627e25SThomas Gleixner 		if (c->x86_model >= 8)
184e1a94a97SIngo Molnar 			set_cpu_cap(c, X86_FEATURE_3DNOW);
185f7627e25SThomas Gleixner 		/* See if we can find out some more. */
186f7627e25SThomas Gleixner 		if (cpuid_eax(0x80000000) >= 0x80000005) {
187f7627e25SThomas Gleixner 			/* Yes, we can. */
188f7627e25SThomas Gleixner 			cpuid(0x80000005, &aa, &bb, &cc, &dd);
189f7627e25SThomas Gleixner 			/* Add L1 data and code cache sizes. */
190f7627e25SThomas Gleixner 			c->x86_cache_size = (cc>>24)+(dd>>24);
191f7627e25SThomas Gleixner 		}
192f7627e25SThomas Gleixner 		sprintf(c->x86_model_id, "WinChip %s", name);
193f7627e25SThomas Gleixner 		break;
19448f4c485SSebastian Andrzej Siewior #endif
195f7627e25SThomas Gleixner 	case 6:
196f7627e25SThomas Gleixner 		init_c3(c);
197f7627e25SThomas Gleixner 		break;
198f7627e25SThomas Gleixner 	}
19948f4c485SSebastian Andrzej Siewior #ifdef CONFIG_X86_64
20048f4c485SSebastian Andrzej Siewior 	set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC);
20148f4c485SSebastian Andrzej Siewior #endif
202f7627e25SThomas Gleixner }
203f7627e25SThomas Gleixner 
20409dc68d9SJan Beulich #ifdef CONFIG_X86_32
205148f9bb8SPaul Gortmaker static unsigned int
206edc05e6dSIngo Molnar centaur_size_cache(struct cpuinfo_x86 *c, unsigned int size)
207f7627e25SThomas Gleixner {
208f7627e25SThomas Gleixner 	/* VIA C3 CPUs (670-68F) need further shifting. */
209f7627e25SThomas Gleixner 	if ((c->x86 == 6) && ((c->x86_model == 7) || (c->x86_model == 8)))
210f7627e25SThomas Gleixner 		size >>= 8;
211f7627e25SThomas Gleixner 
212edc05e6dSIngo Molnar 	/*
213edc05e6dSIngo Molnar 	 * There's also an erratum in Nehemiah stepping 1, which
214edc05e6dSIngo Molnar 	 * returns '65KB' instead of '64KB'
215edc05e6dSIngo Molnar 	 *  - Note, it seems this may only be in engineering samples.
216edc05e6dSIngo Molnar 	 */
217edc05e6dSIngo Molnar 	if ((c->x86 == 6) && (c->x86_model == 9) &&
218edc05e6dSIngo Molnar 				(c->x86_mask == 1) && (size == 65))
219f7627e25SThomas Gleixner 		size -= 1;
220f7627e25SThomas Gleixner 	return size;
221f7627e25SThomas Gleixner }
22209dc68d9SJan Beulich #endif
223f7627e25SThomas Gleixner 
224148f9bb8SPaul Gortmaker static const struct cpu_dev centaur_cpu_dev = {
225f7627e25SThomas Gleixner 	.c_vendor	= "Centaur",
226f7627e25SThomas Gleixner 	.c_ident	= { "CentaurHauls" },
2275fef55fdSYinghai Lu 	.c_early_init	= early_init_centaur,
228f7627e25SThomas Gleixner 	.c_init		= init_centaur,
22909dc68d9SJan Beulich #ifdef CONFIG_X86_32
23009dc68d9SJan Beulich 	.legacy_cache_size = centaur_size_cache,
23109dc68d9SJan Beulich #endif
23210a434fcSYinghai Lu 	.c_x86_vendor	= X86_VENDOR_CENTAUR,
233f7627e25SThomas Gleixner };
234f7627e25SThomas Gleixner 
23510a434fcSYinghai Lu cpu_dev_register(centaur_cpu_dev);
236