cpucheck.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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/intel-family.h>
  26. #include <asm/processor-flags.h>
  27. #include <asm/required-features.h>
  28. #include <asm/msr-index.h>
  29. #include "string.h"
  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 is_intel(void)
  63. {
  64. return cpu_vendor[0] == A32('G', 'e', 'n', 'u') &&
  65. cpu_vendor[1] == A32('i', 'n', 'e', 'I') &&
  66. cpu_vendor[2] == A32('n', 't', 'e', 'l');
  67. }
  68. /* Returns a bitmask of which words we have error bits in */
  69. static int check_cpuflags(void)
  70. {
  71. u32 err;
  72. int i;
  73. err = 0;
  74. for (i = 0; i < NCAPINTS; i++) {
  75. err_flags[i] = req_flags[i] & ~cpu.flags[i];
  76. if (err_flags[i])
  77. err |= 1 << i;
  78. }
  79. return err;
  80. }
  81. /*
  82. * Returns -1 on error.
  83. *
  84. * *cpu_level is set to the current CPU level; *req_level to the required
  85. * level. x86-64 is considered level 64 for this purpose.
  86. *
  87. * *err_flags_ptr is set to the flags error array if there are flags missing.
  88. */
  89. int check_cpu(int *cpu_level_ptr, int *req_level_ptr, u32 **err_flags_ptr)
  90. {
  91. int err;
  92. memset(&cpu.flags, 0, sizeof cpu.flags);
  93. cpu.level = 3;
  94. if (has_eflag(X86_EFLAGS_AC))
  95. cpu.level = 4;
  96. get_cpuflags();
  97. err = check_cpuflags();
  98. if (test_bit(X86_FEATURE_LM, cpu.flags))
  99. cpu.level = 64;
  100. if (err == 0x01 &&
  101. !(err_flags[0] &
  102. ~((1 << X86_FEATURE_XMM)|(1 << X86_FEATURE_XMM2))) &&
  103. is_amd()) {
  104. /* If this is an AMD and we're only missing SSE+SSE2, try to
  105. turn them on */
  106. u32 ecx = MSR_K7_HWCR;
  107. u32 eax, edx;
  108. asm("rdmsr" : "=a" (eax), "=d" (edx) : "c" (ecx));
  109. eax &= ~(1 << 15);
  110. asm("wrmsr" : : "a" (eax), "d" (edx), "c" (ecx));
  111. get_cpuflags(); /* Make sure it really did something */
  112. err = check_cpuflags();
  113. } else if (err == 0x01 &&
  114. !(err_flags[0] & ~(1 << X86_FEATURE_CX8)) &&
  115. is_centaur() && cpu.model >= 6) {
  116. /* If this is a VIA C3, we might have to enable CX8
  117. explicitly */
  118. u32 ecx = MSR_VIA_FCR;
  119. u32 eax, edx;
  120. asm("rdmsr" : "=a" (eax), "=d" (edx) : "c" (ecx));
  121. eax |= (1<<1)|(1<<7);
  122. asm("wrmsr" : : "a" (eax), "d" (edx), "c" (ecx));
  123. set_bit(X86_FEATURE_CX8, cpu.flags);
  124. err = check_cpuflags();
  125. } else if (err == 0x01 && is_transmeta()) {
  126. /* Transmeta might have masked feature bits in word 0 */
  127. u32 ecx = 0x80860004;
  128. u32 eax, edx;
  129. u32 level = 1;
  130. asm("rdmsr" : "=a" (eax), "=d" (edx) : "c" (ecx));
  131. asm("wrmsr" : : "a" (~0), "d" (edx), "c" (ecx));
  132. asm("cpuid"
  133. : "+a" (level), "=d" (cpu.flags[0])
  134. : : "ecx", "ebx");
  135. asm("wrmsr" : : "a" (eax), "d" (edx), "c" (ecx));
  136. err = check_cpuflags();
  137. } else if (err == 0x01 &&
  138. !(err_flags[0] & ~(1 << X86_FEATURE_PAE)) &&
  139. is_intel() && cpu.level == 6 &&
  140. (cpu.model == 9 || cpu.model == 13)) {
  141. /* PAE is disabled on this Pentium M but can be forced */
  142. if (cmdline_find_option_bool("forcepae")) {
  143. puts("WARNING: Forcing PAE in CPU flags\n");
  144. set_bit(X86_FEATURE_PAE, cpu.flags);
  145. err = check_cpuflags();
  146. }
  147. else {
  148. puts("WARNING: PAE disabled. Use parameter 'forcepae' to enable at your own risk!\n");
  149. }
  150. }
  151. if (!err)
  152. err = check_knl_erratum();
  153. if (err_flags_ptr)
  154. *err_flags_ptr = err ? err_flags : NULL;
  155. if (cpu_level_ptr)
  156. *cpu_level_ptr = cpu.level;
  157. if (req_level_ptr)
  158. *req_level_ptr = req_level;
  159. return (cpu.level < req_level || err) ? -1 : 0;
  160. }
  161. int check_knl_erratum(void)
  162. {
  163. /*
  164. * First check for the affected model/family:
  165. */
  166. if (!is_intel() ||
  167. cpu.family != 6 ||
  168. cpu.model != INTEL_FAM6_XEON_PHI_KNL)
  169. return 0;
  170. /*
  171. * This erratum affects the Accessed/Dirty bits, and can
  172. * cause stray bits to be set in !Present PTEs. We have
  173. * enough bits in our 64-bit PTEs (which we have on real
  174. * 64-bit mode or PAE) to avoid using these troublesome
  175. * bits. But, we do not have enough space in our 32-bit
  176. * PTEs. So, refuse to run on 32-bit non-PAE kernels.
  177. */
  178. if (IS_ENABLED(CONFIG_X86_64) || IS_ENABLED(CONFIG_X86_PAE))
  179. return 0;
  180. puts("This 32-bit kernel can not run on this Xeon Phi x200\n"
  181. "processor due to a processor erratum. Use a 64-bit\n"
  182. "kernel, or enable PAE in this 32-bit kernel.\n\n");
  183. return -1;
  184. }