cpucheck.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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("fninit ; fnstsw %0 ; fnstcw %1" : "+m" (fsw), "+m" (fcw));
  87. return fsw == 0 && (fcw & 0x103f) == 0x003f;
  88. }
  89. static int has_eflag(u32 mask)
  90. {
  91. u32 f0, f1;
  92. asm("pushfl ; "
  93. "pushfl ; "
  94. "popl %0 ; "
  95. "movl %0,%1 ; "
  96. "xorl %2,%1 ; "
  97. "pushl %1 ; "
  98. "popfl ; "
  99. "pushfl ; "
  100. "popl %1 ; "
  101. "popfl"
  102. : "=r" (f0), "=r" (f1)
  103. : "g" (mask));
  104. return !!((f0^f1) & mask);
  105. }
  106. static void get_flags(void)
  107. {
  108. u32 max_intel_level, max_amd_level;
  109. u32 tfms;
  110. if (has_fpu())
  111. set_bit(X86_FEATURE_FPU, cpu.flags);
  112. if (has_eflag(X86_EFLAGS_ID)) {
  113. asm("cpuid"
  114. : "=a" (max_intel_level),
  115. "=b" (cpu_vendor[0]),
  116. "=d" (cpu_vendor[1]),
  117. "=c" (cpu_vendor[2])
  118. : "a" (0));
  119. if (max_intel_level >= 0x00000001 &&
  120. max_intel_level <= 0x0000ffff) {
  121. asm("cpuid"
  122. : "=a" (tfms),
  123. "=c" (cpu.flags[4]),
  124. "=d" (cpu.flags[0])
  125. : "a" (0x00000001)
  126. : "ebx");
  127. cpu.level = (tfms >> 8) & 15;
  128. cpu.model = (tfms >> 4) & 15;
  129. if (cpu.level >= 6)
  130. cpu.model += ((tfms >> 16) & 0xf) << 4;
  131. }
  132. asm("cpuid"
  133. : "=a" (max_amd_level)
  134. : "a" (0x80000000)
  135. : "ebx", "ecx", "edx");
  136. if (max_amd_level >= 0x80000001 &&
  137. max_amd_level <= 0x8000ffff) {
  138. u32 eax = 0x80000001;
  139. asm("cpuid"
  140. : "+a" (eax),
  141. "=c" (cpu.flags[6]),
  142. "=d" (cpu.flags[1])
  143. : : "ebx");
  144. }
  145. }
  146. }
  147. /* Returns a bitmask of which words we have error bits in */
  148. static int check_flags(void)
  149. {
  150. u32 err;
  151. int i;
  152. err = 0;
  153. for (i = 0; i < NCAPINTS; i++) {
  154. err_flags[i] = req_flags[i] & ~cpu.flags[i];
  155. if (err_flags[i])
  156. err |= 1 << i;
  157. }
  158. return err;
  159. }
  160. /*
  161. * Returns -1 on error.
  162. *
  163. * *cpu_level is set to the current CPU level; *req_level to the required
  164. * level. x86-64 is considered level 64 for this purpose.
  165. *
  166. * *err_flags_ptr is set to the flags error array if there are flags missing.
  167. */
  168. int check_cpu(int *cpu_level_ptr, int *req_level_ptr, u32 **err_flags_ptr)
  169. {
  170. int err;
  171. memset(&cpu.flags, 0, sizeof cpu.flags);
  172. cpu.level = 3;
  173. if (has_eflag(X86_EFLAGS_AC))
  174. cpu.level = 4;
  175. get_flags();
  176. err = check_flags();
  177. if (test_bit(X86_FEATURE_LM, cpu.flags))
  178. cpu.level = 64;
  179. if (err == 0x01 &&
  180. !(err_flags[0] &
  181. ~((1 << X86_FEATURE_XMM)|(1 << X86_FEATURE_XMM2))) &&
  182. is_amd()) {
  183. /* If this is an AMD and we're only missing SSE+SSE2, try to
  184. turn them on */
  185. u32 ecx = MSR_K7_HWCR;
  186. u32 eax, edx;
  187. asm("rdmsr" : "=a" (eax), "=d" (edx) : "c" (ecx));
  188. eax &= ~(1 << 15);
  189. asm("wrmsr" : : "a" (eax), "d" (edx), "c" (ecx));
  190. get_flags(); /* Make sure it really did something */
  191. err = check_flags();
  192. } else if (err == 0x01 &&
  193. !(err_flags[0] & ~(1 << X86_FEATURE_CX8)) &&
  194. is_centaur() && cpu.model >= 6) {
  195. /* If this is a VIA C3, we might have to enable CX8
  196. explicitly */
  197. u32 ecx = MSR_VIA_FCR;
  198. u32 eax, edx;
  199. asm("rdmsr" : "=a" (eax), "=d" (edx) : "c" (ecx));
  200. eax |= (1<<1)|(1<<7);
  201. asm("wrmsr" : : "a" (eax), "d" (edx), "c" (ecx));
  202. set_bit(X86_FEATURE_CX8, cpu.flags);
  203. err = check_flags();
  204. } else if (err == 0x01 && is_transmeta()) {
  205. /* Transmeta might have masked feature bits in word 0 */
  206. u32 ecx = 0x80860004;
  207. u32 eax, edx;
  208. u32 level = 1;
  209. asm("rdmsr" : "=a" (eax), "=d" (edx) : "c" (ecx));
  210. asm("wrmsr" : : "a" (~0), "d" (edx), "c" (ecx));
  211. asm("cpuid"
  212. : "+a" (level), "=d" (cpu.flags[0])
  213. : : "ecx", "ebx");
  214. asm("wrmsr" : : "a" (eax), "d" (edx), "c" (ecx));
  215. err = check_flags();
  216. }
  217. if (err_flags_ptr)
  218. *err_flags_ptr = err ? err_flags : NULL;
  219. if (cpu_level_ptr)
  220. *cpu_level_ptr = cpu.level;
  221. if (req_level_ptr)
  222. *req_level_ptr = req_level;
  223. return (cpu.level < req_level || err) ? -1 : 0;
  224. }