cpufeature.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_UM_CPUFEATURE_H
  3. #define _ASM_UM_CPUFEATURE_H
  4. #include <asm/processor.h>
  5. #if defined(__KERNEL__) && !defined(__ASSEMBLY__)
  6. #include <asm/asm.h>
  7. #include <linux/bitops.h>
  8. extern const char * const x86_cap_flags[NCAPINTS*32];
  9. extern const char * const x86_power_flags[32];
  10. #define X86_CAP_FMT "%s"
  11. #define x86_cap_flag(flag) x86_cap_flags[flag]
  12. /*
  13. * In order to save room, we index into this array by doing
  14. * X86_BUG_<name> - NCAPINTS*32.
  15. */
  16. extern const char * const x86_bug_flags[NBUGINTS*32];
  17. #define test_cpu_cap(c, bit) \
  18. test_bit(bit, (unsigned long *)((c)->x86_capability))
  19. /*
  20. * There are 32 bits/features in each mask word. The high bits
  21. * (selected with (bit>>5) give us the word number and the low 5
  22. * bits give us the bit/feature number inside the word.
  23. * (1UL<<((bit)&31) gives us a mask for the feature_bit so we can
  24. * see if it is set in the mask word.
  25. */
  26. #define CHECK_BIT_IN_MASK_WORD(maskname, word, bit) \
  27. (((bit)>>5)==(word) && (1UL<<((bit)&31) & maskname##word ))
  28. #define cpu_has(c, bit) \
  29. test_cpu_cap(c, bit)
  30. #define this_cpu_has(bit) \
  31. (__builtin_constant_p(bit) && REQUIRED_MASK_BIT_SET(bit) ? 1 : \
  32. x86_this_cpu_test_bit(bit, \
  33. (unsigned long __percpu *)&cpu_info.x86_capability))
  34. /*
  35. * This macro is for detection of features which need kernel
  36. * infrastructure to be used. It may *not* directly test the CPU
  37. * itself. Use the cpu_has() family if you want true runtime
  38. * testing of CPU features, like in hypervisor code where you are
  39. * supporting a possible guest feature where host support for it
  40. * is not relevant.
  41. */
  42. #define cpu_feature_enabled(bit) \
  43. (__builtin_constant_p(bit) && DISABLED_MASK_BIT_SET(bit) ? 0 : static_cpu_has(bit))
  44. #define boot_cpu_has(bit) cpu_has(&boot_cpu_data, bit)
  45. #define set_cpu_cap(c, bit) set_bit(bit, (unsigned long *)((c)->x86_capability))
  46. extern void setup_clear_cpu_cap(unsigned int bit);
  47. #define setup_force_cpu_cap(bit) do { \
  48. set_cpu_cap(&boot_cpu_data, bit); \
  49. set_bit(bit, (unsigned long *)cpu_caps_set); \
  50. } while (0)
  51. #define setup_force_cpu_bug(bit) setup_force_cpu_cap(bit)
  52. /*
  53. * Static testing of CPU features. Used the same as boot_cpu_has(). It
  54. * statically patches the target code for additional performance. Use
  55. * static_cpu_has() only in fast paths, where every cycle counts. Which
  56. * means that the boot_cpu_has() variant is already fast enough for the
  57. * majority of cases and you should stick to using it as it is generally
  58. * only two instructions: a RIP-relative MOV and a TEST.
  59. */
  60. static __always_inline bool _static_cpu_has(u16 bit)
  61. {
  62. asm_volatile_goto("1: jmp 6f\n"
  63. "2:\n"
  64. ".skip -(((5f-4f) - (2b-1b)) > 0) * "
  65. "((5f-4f) - (2b-1b)),0x90\n"
  66. "3:\n"
  67. ".section .altinstructions,\"a\"\n"
  68. " .long 1b - .\n" /* src offset */
  69. " .long 4f - .\n" /* repl offset */
  70. " .word %P[always]\n" /* always replace */
  71. " .byte 3b - 1b\n" /* src len */
  72. " .byte 5f - 4f\n" /* repl len */
  73. " .byte 3b - 2b\n" /* pad len */
  74. ".previous\n"
  75. ".section .altinstr_replacement,\"ax\"\n"
  76. "4: jmp %l[t_no]\n"
  77. "5:\n"
  78. ".previous\n"
  79. ".section .altinstructions,\"a\"\n"
  80. " .long 1b - .\n" /* src offset */
  81. " .long 0\n" /* no replacement */
  82. " .word %P[feature]\n" /* feature bit */
  83. " .byte 3b - 1b\n" /* src len */
  84. " .byte 0\n" /* repl len */
  85. " .byte 0\n" /* pad len */
  86. ".previous\n"
  87. ".section .altinstr_aux,\"ax\"\n"
  88. "6:\n"
  89. " testb %[bitnum],%[cap_byte]\n"
  90. " jnz %l[t_yes]\n"
  91. " jmp %l[t_no]\n"
  92. ".previous\n"
  93. : : [feature] "i" (bit),
  94. [always] "i" (X86_FEATURE_ALWAYS),
  95. [bitnum] "i" (1 << (bit & 7)),
  96. [cap_byte] "m" (((const char *)boot_cpu_data.x86_capability)[bit >> 3])
  97. : : t_yes, t_no);
  98. t_yes:
  99. return true;
  100. t_no:
  101. return false;
  102. }
  103. #define static_cpu_has(bit) \
  104. ( \
  105. __builtin_constant_p(boot_cpu_has(bit)) ? \
  106. boot_cpu_has(bit) : \
  107. _static_cpu_has(bit) \
  108. )
  109. #define cpu_has_bug(c, bit) cpu_has(c, (bit))
  110. #define set_cpu_bug(c, bit) set_cpu_cap(c, (bit))
  111. #define static_cpu_has_bug(bit) static_cpu_has((bit))
  112. #define boot_cpu_has_bug(bit) cpu_has_bug(&boot_cpu_data, (bit))
  113. #define boot_cpu_set_bug(bit) set_cpu_cap(&boot_cpu_data, (bit))
  114. #define MAX_CPU_FEATURES (NCAPINTS * 32)
  115. #define cpu_have_feature boot_cpu_has
  116. #define CPU_FEATURE_TYPEFMT "x86,ven%04Xfam%04Xmod%04X"
  117. #define CPU_FEATURE_TYPEVAL boot_cpu_data.x86_vendor, boot_cpu_data.x86, \
  118. boot_cpu_data.x86_model
  119. #endif /* defined(__KERNEL__) && !defined(__ASSEMBLY__) */
  120. #endif /* _ASM_UM_CPUFEATURE_H */