xref: /openbmc/linux/arch/x86/kernel/cpu/centaur.c (revision 1b74dde7c47c19a73ea3e9fac95ac27b5d3d50c5)
148f4c485SSebastian Andrzej Siewior #include <linux/bitops.h>
2f7627e25SThomas Gleixner #include <linux/kernel.h>
3edc05e6dSIngo Molnar 
4f7627e25SThomas Gleixner #include <asm/processor.h>
5f7627e25SThomas Gleixner #include <asm/e820.h>
6f7627e25SThomas Gleixner #include <asm/mtrr.h>
748f4c485SSebastian Andrzej Siewior #include <asm/msr.h>
8edc05e6dSIngo Molnar 
9f7627e25SThomas Gleixner #include "cpu.h"
10f7627e25SThomas Gleixner 
11f7627e25SThomas Gleixner #define ACE_PRESENT	(1 << 6)
12f7627e25SThomas Gleixner #define ACE_ENABLED	(1 << 7)
13f7627e25SThomas Gleixner #define ACE_FCR		(1 << 28)	/* MSR_VIA_FCR */
14f7627e25SThomas Gleixner 
15f7627e25SThomas Gleixner #define RNG_PRESENT	(1 << 2)
16f7627e25SThomas Gleixner #define RNG_ENABLED	(1 << 3)
17f7627e25SThomas Gleixner #define RNG_ENABLE	(1 << 6)	/* MSR_VIA_RNG */
18f7627e25SThomas Gleixner 
19148f9bb8SPaul Gortmaker static void init_c3(struct cpuinfo_x86 *c)
20f7627e25SThomas Gleixner {
21f7627e25SThomas Gleixner 	u32  lo, hi;
22f7627e25SThomas Gleixner 
23f7627e25SThomas Gleixner 	/* Test for Centaur Extended Feature Flags presence */
24f7627e25SThomas Gleixner 	if (cpuid_eax(0xC0000000) >= 0xC0000001) {
25f7627e25SThomas Gleixner 		u32 tmp = cpuid_edx(0xC0000001);
26f7627e25SThomas Gleixner 
27f7627e25SThomas Gleixner 		/* enable ACE unit, if present and disabled */
28f7627e25SThomas Gleixner 		if ((tmp & (ACE_PRESENT | ACE_ENABLED)) == ACE_PRESENT) {
29f7627e25SThomas Gleixner 			rdmsr(MSR_VIA_FCR, lo, hi);
30f7627e25SThomas Gleixner 			lo |= ACE_FCR;		/* enable ACE unit */
31f7627e25SThomas Gleixner 			wrmsr(MSR_VIA_FCR, lo, hi);
32*1b74dde7SChen Yucong 			pr_info("CPU: Enabled ACE h/w crypto\n");
33f7627e25SThomas Gleixner 		}
34f7627e25SThomas Gleixner 
35f7627e25SThomas Gleixner 		/* enable RNG unit, if present and disabled */
36f7627e25SThomas Gleixner 		if ((tmp & (RNG_PRESENT | RNG_ENABLED)) == RNG_PRESENT) {
37f7627e25SThomas Gleixner 			rdmsr(MSR_VIA_RNG, lo, hi);
38f7627e25SThomas Gleixner 			lo |= RNG_ENABLE;	/* enable RNG unit */
39f7627e25SThomas Gleixner 			wrmsr(MSR_VIA_RNG, lo, hi);
40*1b74dde7SChen Yucong 			pr_info("CPU: Enabled h/w RNG\n");
41f7627e25SThomas Gleixner 		}
42f7627e25SThomas Gleixner 
43f7627e25SThomas Gleixner 		/* store Centaur Extended Feature Flags as
44f7627e25SThomas Gleixner 		 * word 5 of the CPU capability bit array
45f7627e25SThomas Gleixner 		 */
4639c06df4SBorislav Petkov 		c->x86_capability[CPUID_C000_0001_EDX] = cpuid_edx(0xC0000001);
47f7627e25SThomas Gleixner 	}
4848f4c485SSebastian Andrzej Siewior #ifdef CONFIG_X86_32
4927b46d76SSimon Arlott 	/* Cyrix III family needs CX8 & PGE explicitly enabled. */
50cb3f718dSTimo Teräs 	if (c->x86_model >= 6 && c->x86_model <= 13) {
51f7627e25SThomas Gleixner 		rdmsr(MSR_VIA_FCR, lo, hi);
52f7627e25SThomas Gleixner 		lo |= (1<<1 | 1<<7);
53f7627e25SThomas Gleixner 		wrmsr(MSR_VIA_FCR, lo, hi);
54e1a94a97SIngo Molnar 		set_cpu_cap(c, X86_FEATURE_CX8);
55f7627e25SThomas Gleixner 	}
56f7627e25SThomas Gleixner 
57f7627e25SThomas Gleixner 	/* Before Nehemiah, the C3's had 3dNOW! */
58f7627e25SThomas Gleixner 	if (c->x86_model >= 6 && c->x86_model < 9)
59e1a94a97SIngo Molnar 		set_cpu_cap(c, X86_FEATURE_3DNOW);
6048f4c485SSebastian Andrzej Siewior #endif
6148f4c485SSebastian Andrzej Siewior 	if (c->x86 == 0x6 && c->x86_model >= 0xf) {
6248f4c485SSebastian Andrzej Siewior 		c->x86_cache_alignment = c->x86_clflush_size * 2;
6348f4c485SSebastian Andrzej Siewior 		set_cpu_cap(c, X86_FEATURE_REP_GOOD);
6448f4c485SSebastian Andrzej Siewior 	}
65f7627e25SThomas Gleixner 
6627c13eceSBorislav Petkov 	cpu_detect_cache_sizes(c);
67f7627e25SThomas Gleixner }
68f7627e25SThomas Gleixner 
69f7627e25SThomas Gleixner enum {
70f7627e25SThomas Gleixner 		ECX8		= 1<<1,
71f7627e25SThomas Gleixner 		EIERRINT	= 1<<2,
72f7627e25SThomas Gleixner 		DPM		= 1<<3,
73f7627e25SThomas Gleixner 		DMCE		= 1<<4,
74f7627e25SThomas Gleixner 		DSTPCLK		= 1<<5,
75f7627e25SThomas Gleixner 		ELINEAR		= 1<<6,
76f7627e25SThomas Gleixner 		DSMC		= 1<<7,
77f7627e25SThomas Gleixner 		DTLOCK		= 1<<8,
78f7627e25SThomas Gleixner 		EDCTLB		= 1<<8,
79f7627e25SThomas Gleixner 		EMMX		= 1<<9,
80f7627e25SThomas Gleixner 		DPDC		= 1<<11,
81f7627e25SThomas Gleixner 		EBRPRED		= 1<<12,
82f7627e25SThomas Gleixner 		DIC		= 1<<13,
83f7627e25SThomas Gleixner 		DDC		= 1<<14,
84f7627e25SThomas Gleixner 		DNA		= 1<<15,
85f7627e25SThomas Gleixner 		ERETSTK		= 1<<16,
86f7627e25SThomas Gleixner 		E2MMX		= 1<<19,
87f7627e25SThomas Gleixner 		EAMD3D		= 1<<20,
88f7627e25SThomas Gleixner };
89f7627e25SThomas Gleixner 
90148f9bb8SPaul Gortmaker static void early_init_centaur(struct cpuinfo_x86 *c)
915fef55fdSYinghai Lu {
925fef55fdSYinghai Lu 	switch (c->x86) {
9348f4c485SSebastian Andrzej Siewior #ifdef CONFIG_X86_32
945fef55fdSYinghai Lu 	case 5:
955fef55fdSYinghai Lu 		/* Emulate MTRRs using Centaur's MCR. */
965fef55fdSYinghai Lu 		set_cpu_cap(c, X86_FEATURE_CENTAUR_MCR);
975fef55fdSYinghai Lu 		break;
9848f4c485SSebastian Andrzej Siewior #endif
9948f4c485SSebastian Andrzej Siewior 	case 6:
10048f4c485SSebastian Andrzej Siewior 		if (c->x86_model >= 0xf)
10148f4c485SSebastian Andrzej Siewior 			set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
10248f4c485SSebastian Andrzej Siewior 		break;
1035fef55fdSYinghai Lu 	}
10448f4c485SSebastian Andrzej Siewior #ifdef CONFIG_X86_64
10548f4c485SSebastian Andrzej Siewior 	set_cpu_cap(c, X86_FEATURE_SYSENTER32);
10648f4c485SSebastian Andrzej Siewior #endif
1075fef55fdSYinghai Lu }
1085fef55fdSYinghai Lu 
109148f9bb8SPaul Gortmaker static void init_centaur(struct cpuinfo_x86 *c)
110edc05e6dSIngo Molnar {
11148f4c485SSebastian Andrzej Siewior #ifdef CONFIG_X86_32
112f7627e25SThomas Gleixner 	char *name;
113f7627e25SThomas Gleixner 	u32  fcr_set = 0;
114f7627e25SThomas Gleixner 	u32  fcr_clr = 0;
115f7627e25SThomas Gleixner 	u32  lo, hi, newlo;
116f7627e25SThomas Gleixner 	u32  aa, bb, cc, dd;
117f7627e25SThomas Gleixner 
118edc05e6dSIngo Molnar 	/*
119edc05e6dSIngo Molnar 	 * Bit 31 in normal CPUID used for nonstandard 3DNow ID;
120edc05e6dSIngo Molnar 	 * 3DNow is IDd by bit 31 in extended CPUID (1*32+31) anyway
121edc05e6dSIngo Molnar 	 */
122e1a94a97SIngo Molnar 	clear_cpu_cap(c, 0*32+31);
12348f4c485SSebastian Andrzej Siewior #endif
12448f4c485SSebastian Andrzej Siewior 	early_init_centaur(c);
125f7627e25SThomas Gleixner 	switch (c->x86) {
12648f4c485SSebastian Andrzej Siewior #ifdef CONFIG_X86_32
127f7627e25SThomas Gleixner 	case 5:
128f7627e25SThomas Gleixner 		switch (c->x86_model) {
129f7627e25SThomas Gleixner 		case 4:
130f7627e25SThomas Gleixner 			name = "C6";
131f7627e25SThomas Gleixner 			fcr_set = ECX8|DSMC|EDCTLB|EMMX|ERETSTK;
132f7627e25SThomas Gleixner 			fcr_clr = DPDC;
133*1b74dde7SChen Yucong 			pr_notice("Disabling bugged TSC.\n");
134e1a94a97SIngo Molnar 			clear_cpu_cap(c, X86_FEATURE_TSC);
135f7627e25SThomas Gleixner 			break;
136f7627e25SThomas Gleixner 		case 8:
137f7627e25SThomas Gleixner 			switch (c->x86_mask) {
138f7627e25SThomas Gleixner 			default:
139f7627e25SThomas Gleixner 			name = "2";
140f7627e25SThomas Gleixner 				break;
141f7627e25SThomas Gleixner 			case 7 ... 9:
142f7627e25SThomas Gleixner 				name = "2A";
143f7627e25SThomas Gleixner 				break;
144f7627e25SThomas Gleixner 			case 10 ... 15:
145f7627e25SThomas Gleixner 				name = "2B";
146f7627e25SThomas Gleixner 				break;
147f7627e25SThomas Gleixner 			}
148edc05e6dSIngo Molnar 			fcr_set = ECX8|DSMC|DTLOCK|EMMX|EBRPRED|ERETSTK|
149edc05e6dSIngo Molnar 				  E2MMX|EAMD3D;
150f7627e25SThomas Gleixner 			fcr_clr = DPDC;
151f7627e25SThomas Gleixner 			break;
152f7627e25SThomas Gleixner 		case 9:
153f7627e25SThomas Gleixner 			name = "3";
154edc05e6dSIngo Molnar 			fcr_set = ECX8|DSMC|DTLOCK|EMMX|EBRPRED|ERETSTK|
155edc05e6dSIngo Molnar 				  E2MMX|EAMD3D;
156f7627e25SThomas Gleixner 			fcr_clr = DPDC;
157f7627e25SThomas Gleixner 			break;
158f7627e25SThomas Gleixner 		default:
159f7627e25SThomas Gleixner 			name = "??";
160f7627e25SThomas Gleixner 		}
161f7627e25SThomas Gleixner 
162f7627e25SThomas Gleixner 		rdmsr(MSR_IDT_FCR1, lo, hi);
163f7627e25SThomas Gleixner 		newlo = (lo|fcr_set) & (~fcr_clr);
164f7627e25SThomas Gleixner 
165f7627e25SThomas Gleixner 		if (newlo != lo) {
166*1b74dde7SChen Yucong 			pr_info("Centaur FCR was 0x%X now 0x%X\n",
167edc05e6dSIngo Molnar 				lo, newlo);
168f7627e25SThomas Gleixner 			wrmsr(MSR_IDT_FCR1, newlo, hi);
169f7627e25SThomas Gleixner 		} else {
170*1b74dde7SChen Yucong 			pr_info("Centaur FCR is 0x%X\n", lo);
171f7627e25SThomas Gleixner 		}
172f7627e25SThomas Gleixner 		/* Emulate MTRRs using Centaur's MCR. */
173e1a94a97SIngo Molnar 		set_cpu_cap(c, X86_FEATURE_CENTAUR_MCR);
174f7627e25SThomas Gleixner 		/* Report CX8 */
175e1a94a97SIngo Molnar 		set_cpu_cap(c, X86_FEATURE_CX8);
176f7627e25SThomas Gleixner 		/* Set 3DNow! on Winchip 2 and above. */
177f7627e25SThomas Gleixner 		if (c->x86_model >= 8)
178e1a94a97SIngo Molnar 			set_cpu_cap(c, X86_FEATURE_3DNOW);
179f7627e25SThomas Gleixner 		/* See if we can find out some more. */
180f7627e25SThomas Gleixner 		if (cpuid_eax(0x80000000) >= 0x80000005) {
181f7627e25SThomas Gleixner 			/* Yes, we can. */
182f7627e25SThomas Gleixner 			cpuid(0x80000005, &aa, &bb, &cc, &dd);
183f7627e25SThomas Gleixner 			/* Add L1 data and code cache sizes. */
184f7627e25SThomas Gleixner 			c->x86_cache_size = (cc>>24)+(dd>>24);
185f7627e25SThomas Gleixner 		}
186f7627e25SThomas Gleixner 		sprintf(c->x86_model_id, "WinChip %s", name);
187f7627e25SThomas Gleixner 		break;
18848f4c485SSebastian Andrzej Siewior #endif
189f7627e25SThomas Gleixner 	case 6:
190f7627e25SThomas Gleixner 		init_c3(c);
191f7627e25SThomas Gleixner 		break;
192f7627e25SThomas Gleixner 	}
19348f4c485SSebastian Andrzej Siewior #ifdef CONFIG_X86_64
19448f4c485SSebastian Andrzej Siewior 	set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC);
19548f4c485SSebastian Andrzej Siewior #endif
196f7627e25SThomas Gleixner }
197f7627e25SThomas Gleixner 
19809dc68d9SJan Beulich #ifdef CONFIG_X86_32
199148f9bb8SPaul Gortmaker static unsigned int
200edc05e6dSIngo Molnar centaur_size_cache(struct cpuinfo_x86 *c, unsigned int size)
201f7627e25SThomas Gleixner {
202f7627e25SThomas Gleixner 	/* VIA C3 CPUs (670-68F) need further shifting. */
203f7627e25SThomas Gleixner 	if ((c->x86 == 6) && ((c->x86_model == 7) || (c->x86_model == 8)))
204f7627e25SThomas Gleixner 		size >>= 8;
205f7627e25SThomas Gleixner 
206edc05e6dSIngo Molnar 	/*
207edc05e6dSIngo Molnar 	 * There's also an erratum in Nehemiah stepping 1, which
208edc05e6dSIngo Molnar 	 * returns '65KB' instead of '64KB'
209edc05e6dSIngo Molnar 	 *  - Note, it seems this may only be in engineering samples.
210edc05e6dSIngo Molnar 	 */
211edc05e6dSIngo Molnar 	if ((c->x86 == 6) && (c->x86_model == 9) &&
212edc05e6dSIngo Molnar 				(c->x86_mask == 1) && (size == 65))
213f7627e25SThomas Gleixner 		size -= 1;
214f7627e25SThomas Gleixner 	return size;
215f7627e25SThomas Gleixner }
21609dc68d9SJan Beulich #endif
217f7627e25SThomas Gleixner 
218148f9bb8SPaul Gortmaker static const struct cpu_dev centaur_cpu_dev = {
219f7627e25SThomas Gleixner 	.c_vendor	= "Centaur",
220f7627e25SThomas Gleixner 	.c_ident	= { "CentaurHauls" },
2215fef55fdSYinghai Lu 	.c_early_init	= early_init_centaur,
222f7627e25SThomas Gleixner 	.c_init		= init_centaur,
22309dc68d9SJan Beulich #ifdef CONFIG_X86_32
22409dc68d9SJan Beulich 	.legacy_cache_size = centaur_size_cache,
22509dc68d9SJan Beulich #endif
22610a434fcSYinghai Lu 	.c_x86_vendor	= X86_VENDOR_CENTAUR,
227f7627e25SThomas Gleixner };
228f7627e25SThomas Gleixner 
22910a434fcSYinghai Lu cpu_dev_register(centaur_cpu_dev);
230