cpufeature.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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(__ASSEMBLER__)
  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, cpu_info.x86_capability))
  33. /*
  34. * This macro is for detection of features which need kernel
  35. * infrastructure to be used. It may *not* directly test the CPU
  36. * itself. Use the cpu_has() family if you want true runtime
  37. * testing of CPU features, like in hypervisor code where you are
  38. * supporting a possible guest feature where host support for it
  39. * is not relevant.
  40. */
  41. #define cpu_feature_enabled(bit) \
  42. (__builtin_constant_p(bit) && DISABLED_MASK_BIT_SET(bit) ? 0 : static_cpu_has(bit))
  43. #define boot_cpu_has(bit) cpu_has(&boot_cpu_data, bit)
  44. #define set_cpu_cap(c, bit) set_bit(bit, (unsigned long *)((c)->x86_capability))
  45. extern void setup_clear_cpu_cap(unsigned int bit);
  46. #define setup_force_cpu_cap(bit) do { \
  47. set_cpu_cap(&boot_cpu_data, bit); \
  48. set_bit(bit, (unsigned long *)cpu_caps_set); \
  49. } while (0)
  50. #define setup_force_cpu_bug(bit) setup_force_cpu_cap(bit)
  51. /*
  52. * Static testing of CPU features. Used the same as boot_cpu_has(). It
  53. * statically patches the target code for additional performance. Use
  54. * static_cpu_has() only in fast paths, where every cycle counts. Which
  55. * means that the boot_cpu_has() variant is already fast enough for the
  56. * majority of cases and you should stick to using it as it is generally
  57. * only two instructions: a RIP-relative MOV and a TEST.
  58. */
  59. static __always_inline bool _static_cpu_has(u16 bit)
  60. {
  61. asm goto("1: jmp 6f\n"
  62. "2:\n"
  63. ".skip -(((5f-4f) - (2b-1b)) > 0) * "
  64. "((5f-4f) - (2b-1b)),0x90\n"
  65. "3:\n"
  66. ".section .altinstructions,\"a\"\n"
  67. " .long 1b - .\n" /* src offset */
  68. " .long 4f - .\n" /* repl offset */
  69. " .word %P[always]\n" /* always replace */
  70. " .byte 3b - 1b\n" /* src len */
  71. " .byte 5f - 4f\n" /* repl len */
  72. " .byte 3b - 2b\n" /* pad len */
  73. ".previous\n"
  74. ".section .altinstr_replacement,\"ax\"\n"
  75. "4: jmp %l[t_no]\n"
  76. "5:\n"
  77. ".previous\n"
  78. ".section .altinstructions,\"a\"\n"
  79. " .long 1b - .\n" /* src offset */
  80. " .long 0\n" /* no replacement */
  81. " .word %P[feature]\n" /* feature bit */
  82. " .byte 3b - 1b\n" /* src len */
  83. " .byte 0\n" /* repl len */
  84. " .byte 0\n" /* pad len */
  85. ".previous\n"
  86. ".section .altinstr_aux,\"ax\"\n"
  87. "6:\n"
  88. " testb %[bitnum],%[cap_byte]\n"
  89. " jnz %l[t_yes]\n"
  90. " jmp %l[t_no]\n"
  91. ".previous\n"
  92. : : [feature] "i" (bit),
  93. [always] "i" (X86_FEATURE_ALWAYS),
  94. [bitnum] "i" (1 << (bit & 7)),
  95. [cap_byte] "m" (((const char *)boot_cpu_data.x86_capability)[bit >> 3])
  96. : : t_yes, t_no);
  97. t_yes:
  98. return true;
  99. t_no:
  100. return false;
  101. }
  102. #define static_cpu_has(bit) \
  103. ( \
  104. __builtin_constant_p(boot_cpu_has(bit)) ? \
  105. boot_cpu_has(bit) : \
  106. _static_cpu_has(bit) \
  107. )
  108. #define cpu_has_bug(c, bit) cpu_has(c, (bit))
  109. #define set_cpu_bug(c, bit) set_cpu_cap(c, (bit))
  110. #define static_cpu_has_bug(bit) static_cpu_has((bit))
  111. #define boot_cpu_has_bug(bit) cpu_has_bug(&boot_cpu_data, (bit))
  112. #define boot_cpu_set_bug(bit) set_cpu_cap(&boot_cpu_data, (bit))
  113. #define MAX_CPU_FEATURES (NCAPINTS * 32)
  114. #define cpu_have_feature boot_cpu_has
  115. #define CPU_FEATURE_TYPEFMT "x86,ven%04Xfam%04Xmod%04X"
  116. #define CPU_FEATURE_TYPEVAL boot_cpu_data.x86_vendor, boot_cpu_data.x86, \
  117. boot_cpu_data.x86_model
  118. #endif /* defined(__KERNEL__) && !defined(__ASSEMBLER__) */
  119. #endif /* _ASM_UM_CPUFEATURE_H */