cpucheck.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright 2007 rPath, Inc. - All Rights Reserved
  5. *
  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.
  8. *
  9. * ----------------------------------------------------------------------- */
  10. /*
  11. * arch/i386/boot/cpucheck.c
  12. *
  13. * Check for obligatory CPU features and abort if the features are not
  14. * present. This code should be compilable as 16-, 32- or 64-bit
  15. * code, so be very careful with types and inline assembly.
  16. *
  17. * This code should not contain any messages; that requires an
  18. * additional wrapper.
  19. *
  20. * As written, this code is not safe for inclusion into the kernel
  21. * proper (after FPU initialization, in particular).
  22. */
  23. #ifdef _SETUP
  24. # include "boot.h"
  25. # include "bitops.h"
  26. #endif
  27. #include <linux/types.h>
  28. #include <asm/cpufeature.h>
  29. #include <asm/processor-flags.h>
  30. #include <asm/required-features.h>
  31. #include <asm/msr-index.h>
  32. struct cpu_features {
  33. int level; /* Family, or 64 for x86-64 */
  34. int model;
  35. u32 flags[NCAPINTS];
  36. };
  37. static struct cpu_features cpu;
  38. static u32 cpu_vendor[3];
  39. static u32 err_flags[NCAPINTS];
  40. #ifdef CONFIG_X86_64
  41. static const int req_level = 64;
  42. #elif defined(CONFIG_X86_MINIMUM_CPU_FAMILY)
  43. static const int req_level = CONFIG_X86_MINIMUM_CPU_FAMILY;
  44. #else
  45. static const int req_level = 3;
  46. #endif
  47. static const u32 req_flags[NCAPINTS] =
  48. {
  49. REQUIRED_MASK0,
  50. REQUIRED_MASK1,
  51. REQUIRED_MASK2,
  52. REQUIRED_MASK3,
  53. REQUIRED_MASK4,
  54. REQUIRED_MASK5,
  55. REQUIRED_MASK6,
  56. REQUIRED_MASK7,
  57. };
  58. #define A32(a,b,c,d) (((d) << 24)+((c) << 16)+((b) << 8)+(a))
  59. static int is_amd(void)
  60. {
  61. return cpu_vendor[0] == A32('A','u','t','h') &&
  62. cpu_vendor[1] == A32('e','n','t','i') &&
  63. cpu_vendor[2] == A32('c','A','M','D');
  64. }
  65. static int is_centaur(void)
  66. {
  67. return cpu_vendor[0] == A32('C','e','n','t') &&
  68. cpu_vendor[1] == A32('a','u','r','H') &&
  69. cpu_vendor[2] == A32('a','u','l','s');
  70. }
  71. static int is_transmeta(void)
  72. {
  73. return cpu_vendor[0] == A32('G','e','n','u') &&
  74. cpu_vendor[1] == A32('i','n','e','T') &&
  75. cpu_vendor[2] == A32('M','x','8','6');
  76. }
  77. static int has_fpu(void)
  78. {
  79. u16 fcw = -1, fsw = -1;
  80. u32 cr0;
  81. asm("movl %%cr0,%0" : "=r" (cr0));
  82. if (cr0 & (X86_CR0_EM|X86_CR0_TS)) {
  83. cr0 &= ~(X86_CR0_EM|X86_CR0_TS);
  84. asm volatile("movl %0,%%cr0" : : "r" (cr0));
  85. }
  86. asm volatile("fninit ; fnstsw %0 ; fnstcw %1"
  87. : "+m" (fsw), "+m" (fcw));
  88. return fsw == 0 && (fcw & 0x103f) == 0x003f;
  89. }
  90. static int has_eflag(u32 mask)
  91. {
  92. u32 f0, f1;
  93. asm("pushfl ; "
  94. "pushfl ; "
  95. "popl %0 ; "
  96. "movl %0,%1 ; "
  97. "xorl %2,%1 ; "
  98. "pushl %1 ; "
  99. "popfl ; "
  100. "pushfl ; "
  101. "popl %1 ; "
  102. "popfl"
  103. : "=&r" (f0), "=&r" (f1)
  104. : "ri" (mask));
  105. return !!((f0^f1) & mask);
  106. }
  107. static void get_flags(void)
  108. {
  109. u32 max_intel_level, max_amd_level;
  110. u32 tfms;
  111. if (has_fpu())
  112. set_bit(X86_FEATURE_FPU, cpu.flags);
  113. if (has_eflag(X86_EFLAGS_ID)) {
  114. asm("cpuid"
  115. : "=a" (max_intel_level),
  116. "=b" (cpu_vendor[0]),
  117. "=d" (cpu_vendor[1]),
  118. "=c" (cpu_vendor[2])
  119. : "a" (0));
  120. if (max_intel_level >= 0x00000001 &&
  121. max_intel_level <= 0x0000ffff) {
  122. asm("cpuid"
  123. : "=a" (tfms),
  124. "=c" (cpu.flags[4]),
  125. "=d" (cpu.flags[0])
  126. : "a" (0x00000001)
  127. : "ebx");
  128. cpu.level = (tfms >> 8) & 15;
  129. cpu.model = (tfms >> 4) & 15;
  130. if (cpu.level >= 6)
  131. cpu.model += ((tfms >> 16) & 0xf) << 4;
  132. }
  133. asm("cpuid"
  134. : "=a" (max_amd_level)
  135. : "a" (0x80000000)
  136. : "ebx", "ecx", "edx");
  137. if (max_amd_level >= 0x80000001 &&
  138. max_amd_level <= 0x8000ffff) {
  139. u32 eax = 0x80000001;
  140. asm("cpuid"
  141. : "+a" (eax),
  142. "=c" (cpu.flags[6]),
  143. "=d" (cpu.flags[1])
  144. : : "ebx");
  145. }
  146. }
  147. }
  148. /* Returns a bitmask of which words we have error bits in */
  149. static int check_flags(void)
  150. {
  151. u32 err;
  152. int i;
  153. err = 0;
  154. for (i = 0; i < NCAPINTS; i++) {
  155. err_flags[i] = req_flags[i] & ~cpu.flags[i];
  156. if (err_flags[i])
  157. err |= 1 << i;
  158. }
  159. return err;
  160. }
  161. /*
  162. * Returns -1 on error.
  163. *
  164. * *cpu_level is set to the current CPU level; *req_level to the required
  165. * level. x86-64 is considered level 64 for this purpose.
  166. *
  167. * *err_flags_ptr is set to the flags error array if there are flags missing.
  168. */
  169. int check_cpu(int *cpu_level_ptr, int *req_level_ptr, u32 **err_flags_ptr)
  170. {
  171. int err;
  172. memset(&cpu.flags, 0, sizeof cpu.flags);
  173. cpu.level = 3;
  174. if (has_eflag(X86_EFLAGS_AC))
  175. cpu.level = 4;
  176. get_flags();
  177. err = check_flags();
  178. if (test_bit(X86_FEATURE_LM, cpu.flags))
  179. cpu.level = 64;
  180. if (err == 0x01 &&
  181. !(err_flags[0] &
  182. ~((1 << X86_FEATURE_XMM)|(1 << X86_FEATURE_XMM2))) &&
  183. is_amd()) {
  184. /* If this is an AMD and we're only missing SSE+SSE2, try to
  185. turn them on */
  186. u32 ecx = MSR_K7_HWCR;
  187. u32 eax, edx;
  188. asm("rdmsr" : "=a" (eax), "=d" (edx) : "c" (ecx));
  189. eax &= ~(1 << 15);
  190. asm("wrmsr" : : "a" (eax), "d" (edx), "c" (ecx));
  191. get_flags(); /* Make sure it really did something */
  192. err = check_flags();
  193. } else if (err == 0x01 &&
  194. !(err_flags[0] & ~(1 << X86_FEATURE_CX8)) &&
  195. is_centaur() && cpu.model >= 6) {
  196. /* If this is a VIA C3, we might have to enable CX8
  197. explicitly */
  198. u32 ecx = MSR_VIA_FCR;
  199. u32 eax, edx;
  200. asm("rdmsr" : "=a" (eax), "=d" (edx) : "c" (ecx));
  201. eax |= (1<<1)|(1<<7);
  202. asm("wrmsr" : : "a" (eax), "d" (edx), "c" (ecx));
  203. set_bit(X86_FEATURE_CX8, cpu.flags);
  204. err = check_flags();
  205. } else if (err == 0x01 && is_transmeta()) {
  206. /* Transmeta might have masked feature bits in word 0 */
  207. u32 ecx = 0x80860004;
  208. u32 eax, edx;
  209. u32 level = 1;
  210. asm("rdmsr" : "=a" (eax), "=d" (edx) : "c" (ecx));
  211. asm("wrmsr" : : "a" (~0), "d" (edx), "c" (ecx));
  212. asm("cpuid"
  213. : "+a" (level), "=d" (cpu.flags[0])
  214. : : "ecx", "ebx");
  215. asm("wrmsr" : : "a" (eax), "d" (edx), "c" (ecx));
  216. err = check_flags();
  217. }
  218. if (err_flags_ptr)
  219. *err_flags_ptr = err ? err_flags : NULL;
  220. if (cpu_level_ptr)
  221. *cpu_level_ptr = cpu.level;
  222. if (req_level_ptr)
  223. *req_level_ptr = req_level;
  224. return (cpu.level < req_level || err) ? -1 : 0;
  225. }