cpuflags.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/types.h>
  3. #include "bitops.h"
  4. #include <asm/processor-flags.h>
  5. #include <asm/required-features.h>
  6. #include <asm/msr-index.h>
  7. #include "cpuflags.h"
  8. struct cpu_features cpu;
  9. u32 cpu_vendor[3];
  10. static bool loaded_flags;
  11. static int has_fpu(void)
  12. {
  13. u16 fcw = -1, fsw = -1;
  14. unsigned long cr0;
  15. asm volatile("mov %%cr0,%0" : "=r" (cr0));
  16. if (cr0 & (X86_CR0_EM|X86_CR0_TS)) {
  17. cr0 &= ~(X86_CR0_EM|X86_CR0_TS);
  18. asm volatile("mov %0,%%cr0" : : "r" (cr0));
  19. }
  20. asm volatile("fninit ; fnstsw %0 ; fnstcw %1"
  21. : "+m" (fsw), "+m" (fcw));
  22. return fsw == 0 && (fcw & 0x103f) == 0x003f;
  23. }
  24. /*
  25. * For building the 16-bit code we want to explicitly specify 32-bit
  26. * push/pop operations, rather than just saying 'pushf' or 'popf' and
  27. * letting the compiler choose. But this is also included from the
  28. * compressed/ directory where it may be 64-bit code, and thus needs
  29. * to be 'pushfq' or 'popfq' in that case.
  30. */
  31. #ifdef __x86_64__
  32. #define PUSHF "pushfq"
  33. #define POPF "popfq"
  34. #else
  35. #define PUSHF "pushfl"
  36. #define POPF "popfl"
  37. #endif
  38. int has_eflag(unsigned long mask)
  39. {
  40. unsigned long f0, f1;
  41. asm volatile(PUSHF " \n\t"
  42. PUSHF " \n\t"
  43. "pop %0 \n\t"
  44. "mov %0,%1 \n\t"
  45. "xor %2,%1 \n\t"
  46. "push %1 \n\t"
  47. POPF " \n\t"
  48. PUSHF " \n\t"
  49. "pop %1 \n\t"
  50. POPF
  51. : "=&r" (f0), "=&r" (f1)
  52. : "ri" (mask));
  53. return !!((f0^f1) & mask);
  54. }
  55. void cpuid_count(u32 id, u32 count, u32 *a, u32 *b, u32 *c, u32 *d)
  56. {
  57. asm volatile("cpuid"
  58. : "=a" (*a), "=b" (*b), "=c" (*c), "=d" (*d)
  59. : "0" (id), "2" (count)
  60. );
  61. }
  62. #define cpuid(id, a, b, c, d) cpuid_count(id, 0, a, b, c, d)
  63. void get_cpuflags(void)
  64. {
  65. u32 max_intel_level, max_amd_level;
  66. u32 tfms;
  67. u32 ignored;
  68. if (loaded_flags)
  69. return;
  70. loaded_flags = true;
  71. if (has_fpu())
  72. set_bit(X86_FEATURE_FPU, cpu.flags);
  73. if (has_eflag(X86_EFLAGS_ID)) {
  74. cpuid(0x0, &max_intel_level, &cpu_vendor[0], &cpu_vendor[2],
  75. &cpu_vendor[1]);
  76. if (max_intel_level >= 0x00000001 &&
  77. max_intel_level <= 0x0000ffff) {
  78. cpuid(0x1, &tfms, &ignored, &cpu.flags[4],
  79. &cpu.flags[0]);
  80. cpu.level = (tfms >> 8) & 15;
  81. cpu.family = cpu.level;
  82. cpu.model = (tfms >> 4) & 15;
  83. if (cpu.level >= 6)
  84. cpu.model += ((tfms >> 16) & 0xf) << 4;
  85. }
  86. if (max_intel_level >= 0x00000007) {
  87. cpuid_count(0x00000007, 0, &ignored, &ignored,
  88. &cpu.flags[16], &ignored);
  89. }
  90. cpuid(0x80000000, &max_amd_level, &ignored, &ignored,
  91. &ignored);
  92. if (max_amd_level >= 0x80000001 &&
  93. max_amd_level <= 0x8000ffff) {
  94. cpuid(0x80000001, &ignored, &ignored, &cpu.flags[6],
  95. &cpu.flags[1]);
  96. }
  97. if (max_amd_level >= 0x8000001f) {
  98. u32 ebx;
  99. /*
  100. * The X86_FEATURE_COHERENCY_SFW_NO feature bit is in
  101. * the virtualization flags entry (word 8) and set by
  102. * scattered.c, so the bit needs to be explicitly set.
  103. */
  104. cpuid(0x8000001f, &ignored, &ebx, &ignored, &ignored);
  105. if (ebx & BIT(31))
  106. set_bit(X86_FEATURE_COHERENCY_SFW_NO, cpu.flags);
  107. }
  108. }
  109. }