H. Peter Anvin | bc83ccc | 2010-09-17 15:36:40 -0700 | [diff] [blame] | 1 | #ifndef _ASM_X86_MWAIT_H |
| 2 | #define _ASM_X86_MWAIT_H |
| 3 | |
Peter Zijlstra | 1682425 | 2013-12-12 15:08:36 +0100 | [diff] [blame] | 4 | #include <linux/sched.h> |
| 5 | |
H. Peter Anvin | bc83ccc | 2010-09-17 15:36:40 -0700 | [diff] [blame] | 6 | #define MWAIT_SUBSTATE_MASK 0xf |
| 7 | #define MWAIT_CSTATE_MASK 0xf |
| 8 | #define MWAIT_SUBSTATE_SIZE 4 |
Len Brown | e022e7e | 2013-02-01 23:37:30 -0500 | [diff] [blame] | 9 | #define MWAIT_HINT2CSTATE(hint) (((hint) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK) |
| 10 | #define MWAIT_HINT2SUBSTATE(hint) ((hint) & MWAIT_CSTATE_MASK) |
H. Peter Anvin | bc83ccc | 2010-09-17 15:36:40 -0700 | [diff] [blame] | 11 | |
| 12 | #define CPUID_MWAIT_LEAF 5 |
| 13 | #define CPUID5_ECX_EXTENSIONS_SUPPORTED 0x1 |
| 14 | #define CPUID5_ECX_INTERRUPT_BREAK 0x2 |
| 15 | |
| 16 | #define MWAIT_ECX_INTERRUPT_BREAK 0x1 |
| 17 | |
Peter Zijlstra | 1682425 | 2013-12-12 15:08:36 +0100 | [diff] [blame] | 18 | static inline void __monitor(const void *eax, unsigned long ecx, |
| 19 | unsigned long edx) |
| 20 | { |
| 21 | /* "monitor %eax, %ecx, %edx;" */ |
| 22 | asm volatile(".byte 0x0f, 0x01, 0xc8;" |
| 23 | :: "a" (eax), "c" (ecx), "d"(edx)); |
| 24 | } |
| 25 | |
| 26 | static inline void __mwait(unsigned long eax, unsigned long ecx) |
| 27 | { |
| 28 | /* "mwait %eax, %ecx;" */ |
| 29 | asm volatile(".byte 0x0f, 0x01, 0xc9;" |
| 30 | :: "a" (eax), "c" (ecx)); |
| 31 | } |
| 32 | |
Len Brown | b253149 | 2014-01-15 00:37:34 -0500 | [diff] [blame] | 33 | static inline void __sti_mwait(unsigned long eax, unsigned long ecx) |
| 34 | { |
| 35 | trace_hardirqs_on(); |
| 36 | /* "mwait %eax, %ecx;" */ |
| 37 | asm volatile("sti; .byte 0x0f, 0x01, 0xc9;" |
| 38 | :: "a" (eax), "c" (ecx)); |
| 39 | } |
| 40 | |
Peter Zijlstra | 1682425 | 2013-12-12 15:08:36 +0100 | [diff] [blame] | 41 | /* |
| 42 | * This uses new MONITOR/MWAIT instructions on P4 processors with PNI, |
| 43 | * which can obviate IPI to trigger checking of need_resched. |
| 44 | * We execute MONITOR against need_resched and enter optimized wait state |
| 45 | * through MWAIT. Whenever someone changes need_resched, we would be woken |
| 46 | * up from MWAIT (without an IPI). |
| 47 | * |
| 48 | * New with Core Duo processors, MWAIT can take some hints based on CPU |
| 49 | * capability. |
| 50 | */ |
| 51 | static inline void mwait_idle_with_hints(unsigned long eax, unsigned long ecx) |
| 52 | { |
| 53 | if (!current_set_polling_and_test()) { |
Borislav Petkov | 9b13a93 | 2014-06-18 00:06:23 +0200 | [diff] [blame] | 54 | if (static_cpu_has_bug(X86_BUG_CLFLUSH_MONITOR)) { |
H. Peter Anvin | 7e98b71 | 2013-12-19 11:58:16 -0800 | [diff] [blame] | 55 | mb(); |
Peter Zijlstra | 1682425 | 2013-12-12 15:08:36 +0100 | [diff] [blame] | 56 | clflush((void *)¤t_thread_info()->flags); |
H. Peter Anvin | 7e98b71 | 2013-12-19 11:58:16 -0800 | [diff] [blame] | 57 | mb(); |
| 58 | } |
Peter Zijlstra | 1682425 | 2013-12-12 15:08:36 +0100 | [diff] [blame] | 59 | |
| 60 | __monitor((void *)¤t_thread_info()->flags, 0, 0); |
| 61 | if (!need_resched()) |
| 62 | __mwait(eax, ecx); |
| 63 | } |
Peter Zijlstra | 8cb75e0 | 2013-11-20 12:22:37 +0100 | [diff] [blame] | 64 | current_clr_polling(); |
Peter Zijlstra | 1682425 | 2013-12-12 15:08:36 +0100 | [diff] [blame] | 65 | } |
| 66 | |
H. Peter Anvin | bc83ccc | 2010-09-17 15:36:40 -0700 | [diff] [blame] | 67 | #endif /* _ASM_X86_MWAIT_H */ |