xref: /linux/arch/x86/boot/cpuflags.c (revision 8a3dc0f7c4ccf13098dba804be06799b4bd46c7a)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/types.h>
3 #include "bitops.h"
4 
5 #include <asm/processor-flags.h>
6 #include <asm/msr-index.h>
7 #include "cpuflags.h"
8 
9 struct cpu_features cpu;
10 u32 cpu_vendor[3];
11 
12 static bool loaded_flags;
13 
14 static int has_fpu(void)
15 {
16 	u16 fcw = -1, fsw = -1;
17 	unsigned long cr0;
18 
19 	asm volatile("mov %%cr0,%0" : "=r" (cr0));
20 	if (cr0 & (X86_CR0_EM|X86_CR0_TS)) {
21 		cr0 &= ~(X86_CR0_EM|X86_CR0_TS);
22 		asm volatile("mov %0,%%cr0" : : "r" (cr0));
23 	}
24 
25 	asm volatile("fninit ; fnstsw %0 ; fnstcw %1"
26 		     : "+m" (fsw), "+m" (fcw));
27 
28 	return fsw == 0 && (fcw & 0x103f) == 0x003f;
29 }
30 
31 /*
32  * For building the 16-bit code we want to explicitly specify 32-bit
33  * push/pop operations, rather than just saying 'pushf' or 'popf' and
34  * letting the compiler choose. But this is also included from the
35  * compressed/ directory where it may be 64-bit code, and thus needs
36  * to be 'pushfq' or 'popfq' in that case.
37  */
38 #ifdef __x86_64__
39 #define PUSHF "pushfq"
40 #define POPF "popfq"
41 #else
42 #define PUSHF "pushfl"
43 #define POPF "popfl"
44 #endif
45 
46 int has_eflag(unsigned long mask)
47 {
48 	unsigned long f0, f1;
49 
50 	asm volatile(PUSHF "	\n\t"
51 		     PUSHF "	\n\t"
52 		     "pop %0	\n\t"
53 		     "mov %0,%1	\n\t"
54 		     "xor %2,%1	\n\t"
55 		     "push %1	\n\t"
56 		     POPF "	\n\t"
57 		     PUSHF "	\n\t"
58 		     "pop %1	\n\t"
59 		     POPF
60 		     : "=&r" (f0), "=&r" (f1)
61 		     : "ri" (mask));
62 
63 	return !!((f0^f1) & mask);
64 }
65 
66 void cpuid_count(u32 id, u32 count, u32 *a, u32 *b, u32 *c, u32 *d)
67 {
68 	asm volatile("cpuid"
69 		     : "=a" (*a), "=b" (*b), "=c" (*c), "=d" (*d)
70 		     : "0" (id), "2" (count)
71 	);
72 }
73 
74 #define cpuid(id, a, b, c, d) cpuid_count(id, 0, a, b, c, d)
75 
76 void get_cpuflags(void)
77 {
78 	u32 max_intel_level, max_amd_level;
79 	u32 tfms;
80 	u32 ignored;
81 
82 	if (loaded_flags)
83 		return;
84 	loaded_flags = true;
85 
86 	if (has_fpu())
87 		set_bit(X86_FEATURE_FPU, cpu.flags);
88 
89 	if (has_eflag(X86_EFLAGS_ID)) {
90 		cpuid(0x0, &max_intel_level, &cpu_vendor[0], &cpu_vendor[2],
91 		      &cpu_vendor[1]);
92 
93 		if (max_intel_level >= 0x00000001 &&
94 		    max_intel_level <= 0x0000ffff) {
95 			cpuid(0x1, &tfms, &ignored, &cpu.flags[4],
96 			      &cpu.flags[0]);
97 			cpu.level = (tfms >> 8) & 15;
98 			cpu.family = cpu.level;
99 			cpu.model = (tfms >> 4) & 15;
100 			if (cpu.level >= 6)
101 				cpu.model += ((tfms >> 16) & 0xf) << 4;
102 		}
103 
104 		if (max_intel_level >= 0x00000007) {
105 			cpuid_count(0x00000007, 0, &ignored, &ignored,
106 					&cpu.flags[16], &ignored);
107 		}
108 
109 		cpuid(0x80000000, &max_amd_level, &ignored, &ignored,
110 		      &ignored);
111 
112 		if (max_amd_level >= 0x80000001 &&
113 		    max_amd_level <= 0x8000ffff) {
114 			cpuid(0x80000001, &ignored, &ignored, &cpu.flags[6],
115 			      &cpu.flags[1]);
116 		}
117 	}
118 }
119