xref: /openbmc/linux/arch/x86/kernel/cpu/centaur.c (revision 5d5103595e9e53048bb7e70ee2673c897ab38300)
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 
6*5d510359SSean Christopherson #include <asm/cpu.h>
7cd4d09ecSBorislav Petkov #include <asm/cpufeature.h>
866441bd3SIngo Molnar #include <asm/e820/api.h>
9f7627e25SThomas Gleixner #include <asm/mtrr.h>
1048f4c485SSebastian Andrzej Siewior #include <asm/msr.h>
11edc05e6dSIngo Molnar 
12f7627e25SThomas Gleixner #include "cpu.h"
13f7627e25SThomas Gleixner 
14f7627e25SThomas Gleixner #define ACE_PRESENT	(1 << 6)
15f7627e25SThomas Gleixner #define ACE_ENABLED	(1 << 7)
16f7627e25SThomas Gleixner #define ACE_FCR		(1 << 28)	/* MSR_VIA_FCR */
17f7627e25SThomas Gleixner 
18f7627e25SThomas Gleixner #define RNG_PRESENT	(1 << 2)
19f7627e25SThomas Gleixner #define RNG_ENABLED	(1 << 3)
20f7627e25SThomas Gleixner #define RNG_ENABLE	(1 << 6)	/* MSR_VIA_RNG */
21f7627e25SThomas Gleixner 
22148f9bb8SPaul Gortmaker static void init_c3(struct cpuinfo_x86 *c)
23f7627e25SThomas Gleixner {
24f7627e25SThomas Gleixner 	u32  lo, hi;
25f7627e25SThomas Gleixner 
26f7627e25SThomas Gleixner 	/* Test for Centaur Extended Feature Flags presence */
27f7627e25SThomas Gleixner 	if (cpuid_eax(0xC0000000) >= 0xC0000001) {
28f7627e25SThomas Gleixner 		u32 tmp = cpuid_edx(0xC0000001);
29f7627e25SThomas Gleixner 
30f7627e25SThomas Gleixner 		/* enable ACE unit, if present and disabled */
31f7627e25SThomas Gleixner 		if ((tmp & (ACE_PRESENT | ACE_ENABLED)) == ACE_PRESENT) {
32f7627e25SThomas Gleixner 			rdmsr(MSR_VIA_FCR, lo, hi);
33f7627e25SThomas Gleixner 			lo |= ACE_FCR;		/* enable ACE unit */
34f7627e25SThomas Gleixner 			wrmsr(MSR_VIA_FCR, lo, hi);
351b74dde7SChen Yucong 			pr_info("CPU: Enabled ACE h/w crypto\n");
36f7627e25SThomas Gleixner 		}
37f7627e25SThomas Gleixner 
38f7627e25SThomas Gleixner 		/* enable RNG unit, if present and disabled */
39f7627e25SThomas Gleixner 		if ((tmp & (RNG_PRESENT | RNG_ENABLED)) == RNG_PRESENT) {
40f7627e25SThomas Gleixner 			rdmsr(MSR_VIA_RNG, lo, hi);
41f7627e25SThomas Gleixner 			lo |= RNG_ENABLE;	/* enable RNG unit */
42f7627e25SThomas Gleixner 			wrmsr(MSR_VIA_RNG, lo, hi);
431b74dde7SChen Yucong 			pr_info("CPU: Enabled h/w RNG\n");
44f7627e25SThomas Gleixner 		}
45f7627e25SThomas Gleixner 
46f7627e25SThomas Gleixner 		/* store Centaur Extended Feature Flags as
47f7627e25SThomas Gleixner 		 * word 5 of the CPU capability bit array
48f7627e25SThomas Gleixner 		 */
4939c06df4SBorislav Petkov 		c->x86_capability[CPUID_C000_0001_EDX] = cpuid_edx(0xC0000001);
50f7627e25SThomas Gleixner 	}
5148f4c485SSebastian Andrzej Siewior #ifdef CONFIG_X86_32
5227b46d76SSimon Arlott 	/* Cyrix III family needs CX8 & PGE explicitly enabled. */
53cb3f718dSTimo Teräs 	if (c->x86_model >= 6 && c->x86_model <= 13) {
54f7627e25SThomas Gleixner 		rdmsr(MSR_VIA_FCR, lo, hi);
55f7627e25SThomas Gleixner 		lo |= (1<<1 | 1<<7);
56f7627e25SThomas Gleixner 		wrmsr(MSR_VIA_FCR, lo, hi);
57e1a94a97SIngo Molnar 		set_cpu_cap(c, X86_FEATURE_CX8);
58f7627e25SThomas Gleixner 	}
59f7627e25SThomas Gleixner 
60f7627e25SThomas Gleixner 	/* Before Nehemiah, the C3's had 3dNOW! */
61f7627e25SThomas Gleixner 	if (c->x86_model >= 6 && c->x86_model < 9)
62e1a94a97SIngo Molnar 		set_cpu_cap(c, X86_FEATURE_3DNOW);
6348f4c485SSebastian Andrzej Siewior #endif
6448f4c485SSebastian Andrzej Siewior 	if (c->x86 == 0x6 && c->x86_model >= 0xf) {
6548f4c485SSebastian Andrzej Siewior 		c->x86_cache_alignment = c->x86_clflush_size * 2;
6648f4c485SSebastian Andrzej Siewior 		set_cpu_cap(c, X86_FEATURE_REP_GOOD);
6748f4c485SSebastian Andrzej Siewior 	}
68f7627e25SThomas Gleixner }
69f7627e25SThomas Gleixner 
70f7627e25SThomas Gleixner enum {
71f7627e25SThomas Gleixner 		ECX8		= 1<<1,
72f7627e25SThomas Gleixner 		EIERRINT	= 1<<2,
73f7627e25SThomas Gleixner 		DPM		= 1<<3,
74f7627e25SThomas Gleixner 		DMCE		= 1<<4,
75f7627e25SThomas Gleixner 		DSTPCLK		= 1<<5,
76f7627e25SThomas Gleixner 		ELINEAR		= 1<<6,
77f7627e25SThomas Gleixner 		DSMC		= 1<<7,
78f7627e25SThomas Gleixner 		DTLOCK		= 1<<8,
79f7627e25SThomas Gleixner 		EDCTLB		= 1<<8,
80f7627e25SThomas Gleixner 		EMMX		= 1<<9,
81f7627e25SThomas Gleixner 		DPDC		= 1<<11,
82f7627e25SThomas Gleixner 		EBRPRED		= 1<<12,
83f7627e25SThomas Gleixner 		DIC		= 1<<13,
84f7627e25SThomas Gleixner 		DDC		= 1<<14,
85f7627e25SThomas Gleixner 		DNA		= 1<<15,
86f7627e25SThomas Gleixner 		ERETSTK		= 1<<16,
87f7627e25SThomas Gleixner 		E2MMX		= 1<<19,
88f7627e25SThomas Gleixner 		EAMD3D		= 1<<20,
89f7627e25SThomas Gleixner };
90f7627e25SThomas Gleixner 
91148f9bb8SPaul Gortmaker static void early_init_centaur(struct cpuinfo_x86 *c)
925fef55fdSYinghai Lu {
935fef55fdSYinghai Lu 	switch (c->x86) {
9448f4c485SSebastian Andrzej Siewior #ifdef CONFIG_X86_32
955fef55fdSYinghai Lu 	case 5:
965fef55fdSYinghai Lu 		/* Emulate MTRRs using Centaur's MCR. */
975fef55fdSYinghai Lu 		set_cpu_cap(c, X86_FEATURE_CENTAUR_MCR);
985fef55fdSYinghai Lu 		break;
9948f4c485SSebastian Andrzej Siewior #endif
10048f4c485SSebastian Andrzej Siewior 	case 6:
10148f4c485SSebastian Andrzej Siewior 		if (c->x86_model >= 0xf)
10248f4c485SSebastian Andrzej Siewior 			set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
10348f4c485SSebastian Andrzej Siewior 		break;
1045fef55fdSYinghai Lu 	}
10548f4c485SSebastian Andrzej Siewior #ifdef CONFIG_X86_64
10648f4c485SSebastian Andrzej Siewior 	set_cpu_cap(c, X86_FEATURE_SYSENTER32);
10748f4c485SSebastian Andrzej Siewior #endif
108fe6daab1Sdavidwang 	if (c->x86_power & (1 << 8)) {
109fe6daab1Sdavidwang 		set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
110fe6daab1Sdavidwang 		set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC);
111fe6daab1Sdavidwang 	}
1125fef55fdSYinghai Lu }
1135fef55fdSYinghai Lu 
114148f9bb8SPaul Gortmaker static void init_centaur(struct cpuinfo_x86 *c)
115edc05e6dSIngo Molnar {
11648f4c485SSebastian Andrzej Siewior #ifdef CONFIG_X86_32
117f7627e25SThomas Gleixner 	char *name;
118f7627e25SThomas Gleixner 	u32  fcr_set = 0;
119f7627e25SThomas Gleixner 	u32  fcr_clr = 0;
120f7627e25SThomas Gleixner 	u32  lo, hi, newlo;
121f7627e25SThomas Gleixner 	u32  aa, bb, cc, dd;
122f7627e25SThomas Gleixner 
123edc05e6dSIngo Molnar 	/*
124edc05e6dSIngo Molnar 	 * Bit 31 in normal CPUID used for nonstandard 3DNow ID;
125edc05e6dSIngo Molnar 	 * 3DNow is IDd by bit 31 in extended CPUID (1*32+31) anyway
126edc05e6dSIngo Molnar 	 */
127e1a94a97SIngo Molnar 	clear_cpu_cap(c, 0*32+31);
12848f4c485SSebastian Andrzej Siewior #endif
12948f4c485SSebastian Andrzej Siewior 	early_init_centaur(c);
130a2aa578fSDavid Wang 	init_intel_cacheinfo(c);
1319305bd6cSThomas Gleixner 	detect_num_cpu_cores(c);
132a2aa578fSDavid Wang #ifdef CONFIG_X86_32
133a2aa578fSDavid Wang 	detect_ht(c);
134a2aa578fSDavid Wang #endif
13560882cc1SDavid Wang 
13660882cc1SDavid Wang 	if (c->cpuid_level > 9) {
13760882cc1SDavid Wang 		unsigned int eax = cpuid_eax(10);
13860882cc1SDavid Wang 
13960882cc1SDavid Wang 		/*
14060882cc1SDavid Wang 		 * Check for version and the number of counters
14160882cc1SDavid Wang 		 * Version(eax[7:0]) can't be 0;
14260882cc1SDavid Wang 		 * Counters(eax[15:8]) should be greater than 1;
14360882cc1SDavid Wang 		 */
14460882cc1SDavid Wang 		if ((eax & 0xff) && (((eax >> 8) & 0xff) > 1))
14560882cc1SDavid Wang 			set_cpu_cap(c, X86_FEATURE_ARCH_PERFMON);
14660882cc1SDavid Wang 	}
14760882cc1SDavid Wang 
148f7627e25SThomas Gleixner 	switch (c->x86) {
14948f4c485SSebastian Andrzej Siewior #ifdef CONFIG_X86_32
150f7627e25SThomas Gleixner 	case 5:
151f7627e25SThomas Gleixner 		switch (c->x86_model) {
152f7627e25SThomas Gleixner 		case 4:
153f7627e25SThomas Gleixner 			name = "C6";
154f7627e25SThomas Gleixner 			fcr_set = ECX8|DSMC|EDCTLB|EMMX|ERETSTK;
155f7627e25SThomas Gleixner 			fcr_clr = DPDC;
1561b74dde7SChen Yucong 			pr_notice("Disabling bugged TSC.\n");
157e1a94a97SIngo Molnar 			clear_cpu_cap(c, X86_FEATURE_TSC);
158f7627e25SThomas Gleixner 			break;
159f7627e25SThomas Gleixner 		case 8:
160b399151cSJia Zhang 			switch (c->x86_stepping) {
161f7627e25SThomas Gleixner 			default:
162f7627e25SThomas Gleixner 			name = "2";
163f7627e25SThomas Gleixner 				break;
164f7627e25SThomas Gleixner 			case 7 ... 9:
165f7627e25SThomas Gleixner 				name = "2A";
166f7627e25SThomas Gleixner 				break;
167f7627e25SThomas Gleixner 			case 10 ... 15:
168f7627e25SThomas Gleixner 				name = "2B";
169f7627e25SThomas Gleixner 				break;
170f7627e25SThomas Gleixner 			}
171edc05e6dSIngo Molnar 			fcr_set = ECX8|DSMC|DTLOCK|EMMX|EBRPRED|ERETSTK|
172edc05e6dSIngo Molnar 				  E2MMX|EAMD3D;
173f7627e25SThomas Gleixner 			fcr_clr = DPDC;
174f7627e25SThomas Gleixner 			break;
175f7627e25SThomas Gleixner 		case 9:
176f7627e25SThomas Gleixner 			name = "3";
177edc05e6dSIngo Molnar 			fcr_set = ECX8|DSMC|DTLOCK|EMMX|EBRPRED|ERETSTK|
178edc05e6dSIngo Molnar 				  E2MMX|EAMD3D;
179f7627e25SThomas Gleixner 			fcr_clr = DPDC;
180f7627e25SThomas Gleixner 			break;
181f7627e25SThomas Gleixner 		default:
182f7627e25SThomas Gleixner 			name = "??";
183f7627e25SThomas Gleixner 		}
184f7627e25SThomas Gleixner 
185f7627e25SThomas Gleixner 		rdmsr(MSR_IDT_FCR1, lo, hi);
186f7627e25SThomas Gleixner 		newlo = (lo|fcr_set) & (~fcr_clr);
187f7627e25SThomas Gleixner 
188f7627e25SThomas Gleixner 		if (newlo != lo) {
1891b74dde7SChen Yucong 			pr_info("Centaur FCR was 0x%X now 0x%X\n",
190edc05e6dSIngo Molnar 				lo, newlo);
191f7627e25SThomas Gleixner 			wrmsr(MSR_IDT_FCR1, newlo, hi);
192f7627e25SThomas Gleixner 		} else {
1931b74dde7SChen Yucong 			pr_info("Centaur FCR is 0x%X\n", lo);
194f7627e25SThomas Gleixner 		}
195f7627e25SThomas Gleixner 		/* Emulate MTRRs using Centaur's MCR. */
196e1a94a97SIngo Molnar 		set_cpu_cap(c, X86_FEATURE_CENTAUR_MCR);
197f7627e25SThomas Gleixner 		/* Report CX8 */
198e1a94a97SIngo Molnar 		set_cpu_cap(c, X86_FEATURE_CX8);
199f7627e25SThomas Gleixner 		/* Set 3DNow! on Winchip 2 and above. */
200f7627e25SThomas Gleixner 		if (c->x86_model >= 8)
201e1a94a97SIngo Molnar 			set_cpu_cap(c, X86_FEATURE_3DNOW);
202f7627e25SThomas Gleixner 		/* See if we can find out some more. */
203f7627e25SThomas Gleixner 		if (cpuid_eax(0x80000000) >= 0x80000005) {
204f7627e25SThomas Gleixner 			/* Yes, we can. */
205f7627e25SThomas Gleixner 			cpuid(0x80000005, &aa, &bb, &cc, &dd);
206f7627e25SThomas Gleixner 			/* Add L1 data and code cache sizes. */
207f7627e25SThomas Gleixner 			c->x86_cache_size = (cc>>24)+(dd>>24);
208f7627e25SThomas Gleixner 		}
209f7627e25SThomas Gleixner 		sprintf(c->x86_model_id, "WinChip %s", name);
210f7627e25SThomas Gleixner 		break;
21148f4c485SSebastian Andrzej Siewior #endif
212f7627e25SThomas Gleixner 	case 6:
213f7627e25SThomas Gleixner 		init_c3(c);
214f7627e25SThomas Gleixner 		break;
215f7627e25SThomas Gleixner 	}
21648f4c485SSebastian Andrzej Siewior #ifdef CONFIG_X86_64
21748f4c485SSebastian Andrzej Siewior 	set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC);
21848f4c485SSebastian Andrzej Siewior #endif
21960882cc1SDavid Wang 
22050144490SSean Christopherson 	init_ia32_feat_ctl(c);
221f7627e25SThomas Gleixner }
222f7627e25SThomas Gleixner 
22309dc68d9SJan Beulich #ifdef CONFIG_X86_32
224148f9bb8SPaul Gortmaker static unsigned int
225edc05e6dSIngo Molnar centaur_size_cache(struct cpuinfo_x86 *c, unsigned int size)
226f7627e25SThomas Gleixner {
227f7627e25SThomas Gleixner 	/* VIA C3 CPUs (670-68F) need further shifting. */
228f7627e25SThomas Gleixner 	if ((c->x86 == 6) && ((c->x86_model == 7) || (c->x86_model == 8)))
229f7627e25SThomas Gleixner 		size >>= 8;
230f7627e25SThomas Gleixner 
231edc05e6dSIngo Molnar 	/*
232edc05e6dSIngo Molnar 	 * There's also an erratum in Nehemiah stepping 1, which
233edc05e6dSIngo Molnar 	 * returns '65KB' instead of '64KB'
234edc05e6dSIngo Molnar 	 *  - Note, it seems this may only be in engineering samples.
235edc05e6dSIngo Molnar 	 */
236edc05e6dSIngo Molnar 	if ((c->x86 == 6) && (c->x86_model == 9) &&
237b399151cSJia Zhang 				(c->x86_stepping == 1) && (size == 65))
238f7627e25SThomas Gleixner 		size -= 1;
239f7627e25SThomas Gleixner 	return size;
240f7627e25SThomas Gleixner }
24109dc68d9SJan Beulich #endif
242f7627e25SThomas Gleixner 
243148f9bb8SPaul Gortmaker static const struct cpu_dev centaur_cpu_dev = {
244f7627e25SThomas Gleixner 	.c_vendor	= "Centaur",
245f7627e25SThomas Gleixner 	.c_ident	= { "CentaurHauls" },
2465fef55fdSYinghai Lu 	.c_early_init	= early_init_centaur,
247f7627e25SThomas Gleixner 	.c_init		= init_centaur,
24809dc68d9SJan Beulich #ifdef CONFIG_X86_32
24909dc68d9SJan Beulich 	.legacy_cache_size = centaur_size_cache,
25009dc68d9SJan Beulich #endif
25110a434fcSYinghai Lu 	.c_x86_vendor	= X86_VENDOR_CENTAUR,
252f7627e25SThomas Gleixner };
253f7627e25SThomas Gleixner 
25410a434fcSYinghai Lu cpu_dev_register(centaur_cpu_dev);
255