mwait.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_MWAIT_H
  3. #define _ASM_X86_MWAIT_H
  4. #include <linux/sched.h>
  5. #include <linux/sched/idle.h>
  6. #include <asm/cpufeature.h>
  7. #include <asm/nospec-branch.h>
  8. #define MWAIT_SUBSTATE_MASK 0xf
  9. #define MWAIT_CSTATE_MASK 0xf
  10. #define MWAIT_SUBSTATE_SIZE 4
  11. #define MWAIT_HINT2CSTATE(hint) (((hint) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK)
  12. #define MWAIT_HINT2SUBSTATE(hint) ((hint) & MWAIT_CSTATE_MASK)
  13. #define MWAIT_C1_SUBSTATE_MASK 0xf0
  14. #define CPUID_MWAIT_LEAF 5
  15. #define CPUID5_ECX_EXTENSIONS_SUPPORTED 0x1
  16. #define CPUID5_ECX_INTERRUPT_BREAK 0x2
  17. #define MWAIT_ECX_INTERRUPT_BREAK 0x1
  18. #define MWAITX_ECX_TIMER_ENABLE BIT(1)
  19. #define MWAITX_MAX_WAIT_CYCLES UINT_MAX
  20. #define MWAITX_DISABLE_CSTATES 0xf0
  21. #define TPAUSE_C01_STATE 1
  22. #define TPAUSE_C02_STATE 0
  23. static inline void __monitor(const void *eax, unsigned long ecx,
  24. unsigned long edx)
  25. {
  26. /* "monitor %eax, %ecx, %edx;" */
  27. asm volatile(".byte 0x0f, 0x01, 0xc8;"
  28. :: "a" (eax), "c" (ecx), "d"(edx));
  29. }
  30. static inline void __monitorx(const void *eax, unsigned long ecx,
  31. unsigned long edx)
  32. {
  33. /* "monitorx %eax, %ecx, %edx;" */
  34. asm volatile(".byte 0x0f, 0x01, 0xfa;"
  35. :: "a" (eax), "c" (ecx), "d"(edx));
  36. }
  37. static inline void __mwait(unsigned long eax, unsigned long ecx)
  38. {
  39. mds_idle_clear_cpu_buffers();
  40. /* "mwait %eax, %ecx;" */
  41. asm volatile(".byte 0x0f, 0x01, 0xc9;"
  42. :: "a" (eax), "c" (ecx));
  43. }
  44. /*
  45. * MWAITX allows for a timer expiration to get the core out a wait state in
  46. * addition to the default MWAIT exit condition of a store appearing at a
  47. * monitored virtual address.
  48. *
  49. * Registers:
  50. *
  51. * MWAITX ECX[1]: enable timer if set
  52. * MWAITX EBX[31:0]: max wait time expressed in SW P0 clocks. The software P0
  53. * frequency is the same as the TSC frequency.
  54. *
  55. * Below is a comparison between MWAIT and MWAITX on AMD processors:
  56. *
  57. * MWAIT MWAITX
  58. * opcode 0f 01 c9 | 0f 01 fb
  59. * ECX[0] value of RFLAGS.IF seen by instruction
  60. * ECX[1] unused/#GP if set | enable timer if set
  61. * ECX[31:2] unused/#GP if set
  62. * EAX unused (reserve for hint)
  63. * EBX[31:0] unused | max wait time (P0 clocks)
  64. *
  65. * MONITOR MONITORX
  66. * opcode 0f 01 c8 | 0f 01 fa
  67. * EAX (logical) address to monitor
  68. * ECX #GP if not zero
  69. */
  70. static inline void __mwaitx(unsigned long eax, unsigned long ebx,
  71. unsigned long ecx)
  72. {
  73. /* No MDS buffer clear as this is AMD/HYGON only */
  74. /* "mwaitx %eax, %ebx, %ecx;" */
  75. asm volatile(".byte 0x0f, 0x01, 0xfb;"
  76. :: "a" (eax), "b" (ebx), "c" (ecx));
  77. }
  78. static inline void __sti_mwait(unsigned long eax, unsigned long ecx)
  79. {
  80. mds_idle_clear_cpu_buffers();
  81. /* "mwait %eax, %ecx;" */
  82. asm volatile("sti; .byte 0x0f, 0x01, 0xc9;"
  83. :: "a" (eax), "c" (ecx));
  84. }
  85. /*
  86. * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
  87. * which can obviate IPI to trigger checking of need_resched.
  88. * We execute MONITOR against need_resched and enter optimized wait state
  89. * through MWAIT. Whenever someone changes need_resched, we would be woken
  90. * up from MWAIT (without an IPI).
  91. *
  92. * New with Core Duo processors, MWAIT can take some hints based on CPU
  93. * capability.
  94. */
  95. static inline void mwait_idle_with_hints(unsigned long eax, unsigned long ecx)
  96. {
  97. if (static_cpu_has_bug(X86_BUG_MONITOR) || !current_set_polling_and_test()) {
  98. if (static_cpu_has_bug(X86_BUG_CLFLUSH_MONITOR)) {
  99. mb();
  100. clflush((void *)&current_thread_info()->flags);
  101. mb();
  102. }
  103. __monitor((void *)&current_thread_info()->flags, 0, 0);
  104. if (!need_resched())
  105. __mwait(eax, ecx);
  106. }
  107. current_clr_polling();
  108. }
  109. /*
  110. * Caller can specify whether to enter C0.1 (low latency, less
  111. * power saving) or C0.2 state (saves more power, but longer wakeup
  112. * latency). This may be overridden by the IA32_UMWAIT_CONTROL MSR
  113. * which can force requests for C0.2 to be downgraded to C0.1.
  114. */
  115. static inline void __tpause(u32 ecx, u32 edx, u32 eax)
  116. {
  117. /* "tpause %ecx, %edx, %eax;" */
  118. #ifdef CONFIG_AS_TPAUSE
  119. asm volatile("tpause %%ecx\n"
  120. :
  121. : "c"(ecx), "d"(edx), "a"(eax));
  122. #else
  123. asm volatile(".byte 0x66, 0x0f, 0xae, 0xf1\t\n"
  124. :
  125. : "c"(ecx), "d"(edx), "a"(eax));
  126. #endif
  127. }
  128. #endif /* _ASM_X86_MWAIT_H */