mwait.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef _ASM_X86_MWAIT_H
  2. #define _ASM_X86_MWAIT_H
  3. #include <linux/sched.h>
  4. #include <asm/cpufeature.h>
  5. #define MWAIT_SUBSTATE_MASK 0xf
  6. #define MWAIT_CSTATE_MASK 0xf
  7. #define MWAIT_SUBSTATE_SIZE 4
  8. #define MWAIT_HINT2CSTATE(hint) (((hint) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK)
  9. #define MWAIT_HINT2SUBSTATE(hint) ((hint) & MWAIT_CSTATE_MASK)
  10. #define CPUID_MWAIT_LEAF 5
  11. #define CPUID5_ECX_EXTENSIONS_SUPPORTED 0x1
  12. #define CPUID5_ECX_INTERRUPT_BREAK 0x2
  13. #define MWAIT_ECX_INTERRUPT_BREAK 0x1
  14. #define MWAITX_ECX_TIMER_ENABLE BIT(1)
  15. #define MWAITX_MAX_LOOPS ((u32)-1)
  16. #define MWAITX_DISABLE_CSTATES 0xf
  17. static inline void __monitor(const void *eax, unsigned long ecx,
  18. unsigned long edx)
  19. {
  20. /* "monitor %eax, %ecx, %edx;" */
  21. asm volatile(".byte 0x0f, 0x01, 0xc8;"
  22. :: "a" (eax), "c" (ecx), "d"(edx));
  23. }
  24. static inline void __monitorx(const void *eax, unsigned long ecx,
  25. unsigned long edx)
  26. {
  27. /* "monitorx %eax, %ecx, %edx;" */
  28. asm volatile(".byte 0x0f, 0x01, 0xfa;"
  29. :: "a" (eax), "c" (ecx), "d"(edx));
  30. }
  31. static inline void __mwait(unsigned long eax, unsigned long ecx)
  32. {
  33. /* "mwait %eax, %ecx;" */
  34. asm volatile(".byte 0x0f, 0x01, 0xc9;"
  35. :: "a" (eax), "c" (ecx));
  36. }
  37. /*
  38. * MWAITX allows for a timer expiration to get the core out a wait state in
  39. * addition to the default MWAIT exit condition of a store appearing at a
  40. * monitored virtual address.
  41. *
  42. * Registers:
  43. *
  44. * MWAITX ECX[1]: enable timer if set
  45. * MWAITX EBX[31:0]: max wait time expressed in SW P0 clocks. The software P0
  46. * frequency is the same as the TSC frequency.
  47. *
  48. * Below is a comparison between MWAIT and MWAITX on AMD processors:
  49. *
  50. * MWAIT MWAITX
  51. * opcode 0f 01 c9 | 0f 01 fb
  52. * ECX[0] value of RFLAGS.IF seen by instruction
  53. * ECX[1] unused/#GP if set | enable timer if set
  54. * ECX[31:2] unused/#GP if set
  55. * EAX unused (reserve for hint)
  56. * EBX[31:0] unused | max wait time (P0 clocks)
  57. *
  58. * MONITOR MONITORX
  59. * opcode 0f 01 c8 | 0f 01 fa
  60. * EAX (logical) address to monitor
  61. * ECX #GP if not zero
  62. */
  63. static inline void __mwaitx(unsigned long eax, unsigned long ebx,
  64. unsigned long ecx)
  65. {
  66. /* "mwaitx %eax, %ebx, %ecx;" */
  67. asm volatile(".byte 0x0f, 0x01, 0xfb;"
  68. :: "a" (eax), "b" (ebx), "c" (ecx));
  69. }
  70. static inline void __sti_mwait(unsigned long eax, unsigned long ecx)
  71. {
  72. trace_hardirqs_on();
  73. /* "mwait %eax, %ecx;" */
  74. asm volatile("sti; .byte 0x0f, 0x01, 0xc9;"
  75. :: "a" (eax), "c" (ecx));
  76. }
  77. /*
  78. * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
  79. * which can obviate IPI to trigger checking of need_resched.
  80. * We execute MONITOR against need_resched and enter optimized wait state
  81. * through MWAIT. Whenever someone changes need_resched, we would be woken
  82. * up from MWAIT (without an IPI).
  83. *
  84. * New with Core Duo processors, MWAIT can take some hints based on CPU
  85. * capability.
  86. */
  87. static inline void mwait_idle_with_hints(unsigned long eax, unsigned long ecx)
  88. {
  89. if (static_cpu_has_bug(X86_BUG_MONITOR) || !current_set_polling_and_test()) {
  90. if (static_cpu_has_bug(X86_BUG_CLFLUSH_MONITOR)) {
  91. mb();
  92. clflush((void *)&current_thread_info()->flags);
  93. mb();
  94. }
  95. __monitor((void *)&current_thread_info()->flags, 0, 0);
  96. if (!need_resched())
  97. __mwait(eax, ecx);
  98. }
  99. current_clr_polling();
  100. }
  101. #endif /* _ASM_X86_MWAIT_H */