cpucheck.c 5.9 KB

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