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 | |
Borislav Petkov | cd4d09e | 2016-01-26 22:12:04 +0100 | [diff] [blame] | 6 | #include <asm/cpufeature.h> |
Thomas Gleixner | 2394f59 | 2019-02-18 23:04:01 +0100 | [diff] [blame] | 7 | #include <asm/nospec-branch.h> |
Borislav Petkov | cd4d09e | 2016-01-26 22:12:04 +0100 | [diff] [blame] | 8 | |
H. Peter Anvin | bc83ccc | 2010-09-17 15:36:40 -0700 | [diff] [blame] | 9 | #define MWAIT_SUBSTATE_MASK 0xf |
| 10 | #define MWAIT_CSTATE_MASK 0xf |
| 11 | #define MWAIT_SUBSTATE_SIZE 4 |
Len Brown | e022e7e | 2013-02-01 23:37:30 -0500 | [diff] [blame] | 12 | #define MWAIT_HINT2CSTATE(hint) (((hint) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK) |
| 13 | #define MWAIT_HINT2SUBSTATE(hint) ((hint) & MWAIT_CSTATE_MASK) |
H. Peter Anvin | bc83ccc | 2010-09-17 15:36:40 -0700 | [diff] [blame] | 14 | |
| 15 | #define CPUID_MWAIT_LEAF 5 |
| 16 | #define CPUID5_ECX_EXTENSIONS_SUPPORTED 0x1 |
| 17 | #define CPUID5_ECX_INTERRUPT_BREAK 0x2 |
| 18 | |
| 19 | #define MWAIT_ECX_INTERRUPT_BREAK 0x1 |
Huang Rui | f967567 | 2015-08-10 12:19:53 +0200 | [diff] [blame] | 20 | #define MWAITX_ECX_TIMER_ENABLE BIT(1) |
| 21 | #define MWAITX_MAX_LOOPS ((u32)-1) |
Janakarajan Natarajan | 1cf0865 | 2019-10-07 19:00:22 +0000 | [diff] [blame] | 22 | #define MWAITX_DISABLE_CSTATES 0xf0 |
H. Peter Anvin | bc83ccc | 2010-09-17 15:36:40 -0700 | [diff] [blame] | 23 | |
Peter Zijlstra | 1682425 | 2013-12-12 15:08:36 +0100 | [diff] [blame] | 24 | static inline void __monitor(const void *eax, unsigned long ecx, |
| 25 | unsigned long edx) |
| 26 | { |
| 27 | /* "monitor %eax, %ecx, %edx;" */ |
| 28 | asm volatile(".byte 0x0f, 0x01, 0xc8;" |
| 29 | :: "a" (eax), "c" (ecx), "d"(edx)); |
| 30 | } |
| 31 | |
Huang Rui | f967567 | 2015-08-10 12:19:53 +0200 | [diff] [blame] | 32 | static inline void __monitorx(const void *eax, unsigned long ecx, |
| 33 | unsigned long edx) |
| 34 | { |
| 35 | /* "monitorx %eax, %ecx, %edx;" */ |
| 36 | asm volatile(".byte 0x0f, 0x01, 0xfa;" |
| 37 | :: "a" (eax), "c" (ecx), "d"(edx)); |
| 38 | } |
| 39 | |
Peter Zijlstra | 1682425 | 2013-12-12 15:08:36 +0100 | [diff] [blame] | 40 | static inline void __mwait(unsigned long eax, unsigned long ecx) |
| 41 | { |
Thomas Gleixner | 2394f59 | 2019-02-18 23:04:01 +0100 | [diff] [blame] | 42 | mds_idle_clear_cpu_buffers(); |
| 43 | |
Peter Zijlstra | 1682425 | 2013-12-12 15:08:36 +0100 | [diff] [blame] | 44 | /* "mwait %eax, %ecx;" */ |
| 45 | asm volatile(".byte 0x0f, 0x01, 0xc9;" |
| 46 | :: "a" (eax), "c" (ecx)); |
| 47 | } |
| 48 | |
Huang Rui | f967567 | 2015-08-10 12:19:53 +0200 | [diff] [blame] | 49 | /* |
| 50 | * MWAITX allows for a timer expiration to get the core out a wait state in |
| 51 | * addition to the default MWAIT exit condition of a store appearing at a |
| 52 | * monitored virtual address. |
| 53 | * |
| 54 | * Registers: |
| 55 | * |
| 56 | * MWAITX ECX[1]: enable timer if set |
| 57 | * MWAITX EBX[31:0]: max wait time expressed in SW P0 clocks. The software P0 |
| 58 | * frequency is the same as the TSC frequency. |
| 59 | * |
| 60 | * Below is a comparison between MWAIT and MWAITX on AMD processors: |
| 61 | * |
| 62 | * MWAIT MWAITX |
| 63 | * opcode 0f 01 c9 | 0f 01 fb |
| 64 | * ECX[0] value of RFLAGS.IF seen by instruction |
| 65 | * ECX[1] unused/#GP if set | enable timer if set |
| 66 | * ECX[31:2] unused/#GP if set |
| 67 | * EAX unused (reserve for hint) |
| 68 | * EBX[31:0] unused | max wait time (P0 clocks) |
| 69 | * |
| 70 | * MONITOR MONITORX |
| 71 | * opcode 0f 01 c8 | 0f 01 fa |
| 72 | * EAX (logical) address to monitor |
| 73 | * ECX #GP if not zero |
| 74 | */ |
| 75 | static inline void __mwaitx(unsigned long eax, unsigned long ebx, |
| 76 | unsigned long ecx) |
| 77 | { |
Thomas Gleixner | 2394f59 | 2019-02-18 23:04:01 +0100 | [diff] [blame] | 78 | /* No MDS buffer clear as this is AMD/HYGON only */ |
| 79 | |
Huang Rui | f967567 | 2015-08-10 12:19:53 +0200 | [diff] [blame] | 80 | /* "mwaitx %eax, %ebx, %ecx;" */ |
| 81 | asm volatile(".byte 0x0f, 0x01, 0xfb;" |
| 82 | :: "a" (eax), "b" (ebx), "c" (ecx)); |
| 83 | } |
| 84 | |
Len Brown | b253149 | 2014-01-15 00:37:34 -0500 | [diff] [blame] | 85 | static inline void __sti_mwait(unsigned long eax, unsigned long ecx) |
| 86 | { |
Thomas Gleixner | 2394f59 | 2019-02-18 23:04:01 +0100 | [diff] [blame] | 87 | mds_idle_clear_cpu_buffers(); |
| 88 | |
Len Brown | b253149 | 2014-01-15 00:37:34 -0500 | [diff] [blame] | 89 | trace_hardirqs_on(); |
| 90 | /* "mwait %eax, %ecx;" */ |
| 91 | asm volatile("sti; .byte 0x0f, 0x01, 0xc9;" |
| 92 | :: "a" (eax), "c" (ecx)); |
| 93 | } |
| 94 | |
Peter Zijlstra | 1682425 | 2013-12-12 15:08:36 +0100 | [diff] [blame] | 95 | /* |
| 96 | * This uses new MONITOR/MWAIT instructions on P4 processors with PNI, |
| 97 | * which can obviate IPI to trigger checking of need_resched. |
| 98 | * We execute MONITOR against need_resched and enter optimized wait state |
| 99 | * through MWAIT. Whenever someone changes need_resched, we would be woken |
| 100 | * up from MWAIT (without an IPI). |
| 101 | * |
| 102 | * New with Core Duo processors, MWAIT can take some hints based on CPU |
| 103 | * capability. |
| 104 | */ |
| 105 | static inline void mwait_idle_with_hints(unsigned long eax, unsigned long ecx) |
| 106 | { |
Peter Zijlstra | 08e237f | 2016-07-18 11:41:10 -0700 | [diff] [blame] | 107 | if (static_cpu_has_bug(X86_BUG_MONITOR) || !current_set_polling_and_test()) { |
Borislav Petkov | 9b13a93 | 2014-06-18 00:06:23 +0200 | [diff] [blame] | 108 | if (static_cpu_has_bug(X86_BUG_CLFLUSH_MONITOR)) { |
H. Peter Anvin | 7e98b71 | 2013-12-19 11:58:16 -0800 | [diff] [blame] | 109 | mb(); |
Peter Zijlstra | 1682425 | 2013-12-12 15:08:36 +0100 | [diff] [blame] | 110 | clflush((void *)¤t_thread_info()->flags); |
H. Peter Anvin | 7e98b71 | 2013-12-19 11:58:16 -0800 | [diff] [blame] | 111 | mb(); |
| 112 | } |
Peter Zijlstra | 1682425 | 2013-12-12 15:08:36 +0100 | [diff] [blame] | 113 | |
| 114 | __monitor((void *)¤t_thread_info()->flags, 0, 0); |
| 115 | if (!need_resched()) |
| 116 | __mwait(eax, ecx); |
| 117 | } |
Peter Zijlstra | 8cb75e0 | 2013-11-20 12:22:37 +0100 | [diff] [blame] | 118 | current_clr_polling(); |
Peter Zijlstra | 1682425 | 2013-12-12 15:08:36 +0100 | [diff] [blame] | 119 | } |
| 120 | |
H. Peter Anvin | bc83ccc | 2010-09-17 15:36:40 -0700 | [diff] [blame] | 121 | #endif /* _ASM_X86_MWAIT_H */ |