cpucheck.c 5.9 KB

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