cpucheck.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #include "string.h"
  29. static u32 err_flags[NCAPINTS];
  30. static const int req_level = CONFIG_X86_MINIMUM_CPU_FAMILY;
  31. static const u32 req_flags[NCAPINTS] =
  32. {
  33. REQUIRED_MASK0,
  34. REQUIRED_MASK1,
  35. 0, /* REQUIRED_MASK2 not implemented in this file */
  36. 0, /* REQUIRED_MASK3 not implemented in this file */
  37. REQUIRED_MASK4,
  38. 0, /* REQUIRED_MASK5 not implemented in this file */
  39. REQUIRED_MASK6,
  40. 0, /* REQUIRED_MASK7 not implemented in this file */
  41. };
  42. #define A32(a, b, c, d) (((d) << 24)+((c) << 16)+((b) << 8)+(a))
  43. static int is_amd(void)
  44. {
  45. return cpu_vendor[0] == A32('A', 'u', 't', 'h') &&
  46. cpu_vendor[1] == A32('e', 'n', 't', 'i') &&
  47. cpu_vendor[2] == A32('c', 'A', 'M', 'D');
  48. }
  49. static int is_centaur(void)
  50. {
  51. return cpu_vendor[0] == A32('C', 'e', 'n', 't') &&
  52. cpu_vendor[1] == A32('a', 'u', 'r', 'H') &&
  53. cpu_vendor[2] == A32('a', 'u', 'l', 's');
  54. }
  55. static int is_transmeta(void)
  56. {
  57. return cpu_vendor[0] == A32('G', 'e', 'n', 'u') &&
  58. cpu_vendor[1] == A32('i', 'n', 'e', 'T') &&
  59. cpu_vendor[2] == A32('M', 'x', '8', '6');
  60. }
  61. static int is_intel(void)
  62. {
  63. return cpu_vendor[0] == A32('G', 'e', 'n', 'u') &&
  64. cpu_vendor[1] == A32('i', 'n', 'e', 'I') &&
  65. cpu_vendor[2] == A32('n', 't', 'e', 'l');
  66. }
  67. /* Returns a bitmask of which words we have error bits in */
  68. static int check_cpuflags(void)
  69. {
  70. u32 err;
  71. int i;
  72. err = 0;
  73. for (i = 0; i < NCAPINTS; i++) {
  74. err_flags[i] = req_flags[i] & ~cpu.flags[i];
  75. if (err_flags[i])
  76. err |= 1 << i;
  77. }
  78. return err;
  79. }
  80. /*
  81. * Returns -1 on error.
  82. *
  83. * *cpu_level is set to the current CPU level; *req_level to the required
  84. * level. x86-64 is considered level 64 for this purpose.
  85. *
  86. * *err_flags_ptr is set to the flags error array if there are flags missing.
  87. */
  88. int check_cpu(int *cpu_level_ptr, int *req_level_ptr, u32 **err_flags_ptr)
  89. {
  90. int err;
  91. memset(&cpu.flags, 0, sizeof cpu.flags);
  92. cpu.level = 3;
  93. if (has_eflag(X86_EFLAGS_AC))
  94. cpu.level = 4;
  95. get_cpuflags();
  96. err = check_cpuflags();
  97. if (test_bit(X86_FEATURE_LM, cpu.flags))
  98. cpu.level = 64;
  99. if (err == 0x01 &&
  100. !(err_flags[0] &
  101. ~((1 << X86_FEATURE_XMM)|(1 << X86_FEATURE_XMM2))) &&
  102. is_amd()) {
  103. /* If this is an AMD and we're only missing SSE+SSE2, try to
  104. turn them on */
  105. u32 ecx = MSR_K7_HWCR;
  106. u32 eax, edx;
  107. asm("rdmsr" : "=a" (eax), "=d" (edx) : "c" (ecx));
  108. eax &= ~(1 << 15);
  109. asm("wrmsr" : : "a" (eax), "d" (edx), "c" (ecx));
  110. get_cpuflags(); /* Make sure it really did something */
  111. err = check_cpuflags();
  112. } else if (err == 0x01 &&
  113. !(err_flags[0] & ~(1 << X86_FEATURE_CX8)) &&
  114. is_centaur() && cpu.model >= 6) {
  115. /* If this is a VIA C3, we might have to enable CX8
  116. explicitly */
  117. u32 ecx = MSR_VIA_FCR;
  118. u32 eax, edx;
  119. asm("rdmsr" : "=a" (eax), "=d" (edx) : "c" (ecx));
  120. eax |= (1<<1)|(1<<7);
  121. asm("wrmsr" : : "a" (eax), "d" (edx), "c" (ecx));
  122. set_bit(X86_FEATURE_CX8, cpu.flags);
  123. err = check_cpuflags();
  124. } else if (err == 0x01 && is_transmeta()) {
  125. /* Transmeta might have masked feature bits in word 0 */
  126. u32 ecx = 0x80860004;
  127. u32 eax, edx;
  128. u32 level = 1;
  129. asm("rdmsr" : "=a" (eax), "=d" (edx) : "c" (ecx));
  130. asm("wrmsr" : : "a" (~0), "d" (edx), "c" (ecx));
  131. asm("cpuid"
  132. : "+a" (level), "=d" (cpu.flags[0])
  133. : : "ecx", "ebx");
  134. asm("wrmsr" : : "a" (eax), "d" (edx), "c" (ecx));
  135. err = check_cpuflags();
  136. } else if (err == 0x01 &&
  137. !(err_flags[0] & ~(1 << X86_FEATURE_PAE)) &&
  138. is_intel() && cpu.level == 6 &&
  139. (cpu.model == 9 || cpu.model == 13)) {
  140. /* PAE is disabled on this Pentium M but can be forced */
  141. if (cmdline_find_option_bool("forcepae")) {
  142. puts("WARNING: Forcing PAE in CPU flags\n");
  143. set_bit(X86_FEATURE_PAE, cpu.flags);
  144. err = check_cpuflags();
  145. }
  146. else {
  147. puts("WARNING: PAE disabled. Use parameter 'forcepae' to enable at your own risk!\n");
  148. }
  149. }
  150. if (err_flags_ptr)
  151. *err_flags_ptr = err ? err_flags : NULL;
  152. if (cpu_level_ptr)
  153. *cpu_level_ptr = cpu.level;
  154. if (req_level_ptr)
  155. *req_level_ptr = req_level;
  156. return (cpu.level < req_level || err) ? -1 : 0;
  157. }