1 /* -*- linux-c -*- ------------------------------------------------------- *
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright 2007 rPath, Inc. - All Rights Reserved
6 * This file is part of the Linux kernel, and is made available under
7 * the terms of the GNU General Public License version 2.
9 * ----------------------------------------------------------------------- */
12 * arch/i386/boot/cpucheck.c
14 * Check for obligatory CPU features and abort if the features are not
15 * present. This code should be compilable as 16-, 32- or 64-bit
16 * code, so be very careful with types and inline assembly.
18 * This code should not contain any messages; that requires an
21 * As written, this code is not safe for inclusion into the kernel
22 * proper (after FPU initialization, in particular).
29 #include <linux/types.h>
30 #include <asm/cpufeature.h>
31 #include <asm/processor-flags.h>
32 #include <asm/required-features.h>
33 #include <asm/msr-index.h>
36 int level; /* Family, or 64 for x86-64 */
41 static struct cpu_features cpu;
42 static u32 cpu_vendor[3];
43 static u32 err_flags[NCAPINTS];
46 static const int req_level = 64;
47 #elif defined(CONFIG_X86_MINIMUM_CPU_FAMILY)
48 static const int req_level = CONFIG_X86_MINIMUM_CPU_FAMILY;
50 static const int req_level = 3;
53 static const u32 req_flags[NCAPINTS] =
65 #define A32(a,b,c,d) (((d) << 24)+((c) << 16)+((b) << 8)+(a))
67 static int is_amd(void)
69 return cpu_vendor[0] == A32('A','u','t','h') &&
70 cpu_vendor[1] == A32('e','n','t','i') &&
71 cpu_vendor[2] == A32('c','A','M','D');
74 static int is_centaur(void)
76 return cpu_vendor[0] == A32('C','e','n','t') &&
77 cpu_vendor[1] == A32('a','u','r','H') &&
78 cpu_vendor[2] == A32('a','u','l','s');
81 static int is_transmeta(void)
83 return cpu_vendor[0] == A32('G','e','n','u') &&
84 cpu_vendor[1] == A32('i','n','e','T') &&
85 cpu_vendor[2] == A32('M','x','8','6');
88 static int has_fpu(void)
90 u16 fcw = -1, fsw = -1;
93 asm("movl %%cr0,%0" : "=r" (cr0));
94 if (cr0 & (X86_CR0_EM|X86_CR0_TS)) {
95 cr0 &= ~(X86_CR0_EM|X86_CR0_TS);
96 asm volatile("movl %0,%%cr0" : : "r" (cr0));
99 asm("fninit ; fnstsw %0 ; fnstcw %1" : "+m" (fsw), "+m" (fcw));
101 return fsw == 0 && (fcw & 0x103f) == 0x003f;
104 static int has_eflag(u32 mask)
118 : "=r" (f0), "=r" (f1)
121 return !!((f0^f1) & mask);
124 static void get_flags(void)
126 u32 max_intel_level, max_amd_level;
130 set_bit(X86_FEATURE_FPU, cpu.flags);
132 if (has_eflag(X86_EFLAGS_ID)) {
134 : "=a" (max_intel_level),
135 "=b" (cpu_vendor[0]),
136 "=d" (cpu_vendor[1]),
140 if (max_intel_level >= 0x00000001 &&
141 max_intel_level <= 0x0000ffff) {
148 cpu.level = (tfms >> 8) & 15;
149 cpu.model = (tfms >> 4) & 15;
151 cpu.model += ((tfms >> 16) & 0xf) << 4;
155 : "=a" (max_amd_level)
157 : "ebx", "ecx", "edx");
159 if (max_amd_level >= 0x80000001 &&
160 max_amd_level <= 0x8000ffff) {
161 u32 eax = 0x80000001;
171 /* Returns a bitmask of which words we have error bits in */
172 static int check_flags(void)
178 for (i = 0; i < NCAPINTS; i++) {
179 err_flags[i] = req_flags[i] & ~cpu.flags[i];
188 * Returns -1 on error.
190 * *cpu_level is set to the current CPU level; *req_level to the required
191 * level. x86-64 is considered level 64 for this purpose.
193 * *err_flags_ptr is set to the flags error array if there are flags missing.
195 int check_cpu(int *cpu_level_ptr, int *req_level_ptr, u32 **err_flags_ptr)
199 memset(&cpu.flags, 0, sizeof cpu.flags);
202 if (has_eflag(X86_EFLAGS_AC))
208 if (test_bit(X86_FEATURE_LM, cpu.flags))
213 ~((1 << X86_FEATURE_XMM)|(1 << X86_FEATURE_XMM2))) &&
215 /* If this is an AMD and we're only missing SSE+SSE2, try to
218 u32 ecx = MSR_K7_HWCR;
221 asm("rdmsr" : "=a" (eax), "=d" (edx) : "c" (ecx));
223 asm("wrmsr" : : "a" (eax), "d" (edx), "c" (ecx));
225 get_flags(); /* Make sure it really did something */
227 } else if (err == 0x01 &&
228 !(err_flags[0] & ~(1 << X86_FEATURE_CX8)) &&
229 is_centaur() && cpu.model >= 6) {
230 /* If this is a VIA C3, we might have to enable CX8
233 u32 ecx = MSR_VIA_FCR;
236 asm("rdmsr" : "=a" (eax), "=d" (edx) : "c" (ecx));
237 eax |= (1<<1)|(1<<7);
238 asm("wrmsr" : : "a" (eax), "d" (edx), "c" (ecx));
240 set_bit(X86_FEATURE_CX8, cpu.flags);
242 } else if (err == 0x01 && is_transmeta()) {
243 /* Transmeta might have masked feature bits in word 0 */
245 u32 ecx = 0x80860004;
249 asm("rdmsr" : "=a" (eax), "=d" (edx) : "c" (ecx));
250 asm("wrmsr" : : "a" (~0), "d" (edx), "c" (ecx));
252 : "+a" (level), "=d" (cpu.flags[0])
254 asm("wrmsr" : : "a" (eax), "d" (edx), "c" (ecx));
260 *err_flags_ptr = err ? err_flags : NULL;
262 *cpu_level_ptr = cpu.level;
264 *req_level_ptr = req_level;
266 return (cpu.level < req_level || err) ? -1 : 0;