blob: f37f2d8a2989d08d37304ffd4689a381cc0ddb37 [file] [log] [blame]
H. Peter Anvinbc83ccc2010-09-17 15:36:40 -07001#ifndef _ASM_X86_MWAIT_H
2#define _ASM_X86_MWAIT_H
3
Peter Zijlstra16824252013-12-12 15:08:36 +01004#include <linux/sched.h>
5
Borislav Petkovcd4d09e2016-01-26 22:12:04 +01006#include <asm/cpufeature.h>
7
H. Peter Anvinbc83ccc2010-09-17 15:36:40 -07008#define MWAIT_SUBSTATE_MASK 0xf
9#define MWAIT_CSTATE_MASK 0xf
10#define MWAIT_SUBSTATE_SIZE 4
Len Browne022e7e2013-02-01 23:37:30 -050011#define MWAIT_HINT2CSTATE(hint) (((hint) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK)
12#define MWAIT_HINT2SUBSTATE(hint) ((hint) & MWAIT_CSTATE_MASK)
H. Peter Anvinbc83ccc2010-09-17 15:36:40 -070013
14#define CPUID_MWAIT_LEAF 5
15#define CPUID5_ECX_EXTENSIONS_SUPPORTED 0x1
16#define CPUID5_ECX_INTERRUPT_BREAK 0x2
17
18#define MWAIT_ECX_INTERRUPT_BREAK 0x1
Huang Ruif9675672015-08-10 12:19:53 +020019#define MWAITX_ECX_TIMER_ENABLE BIT(1)
20#define MWAITX_MAX_LOOPS ((u32)-1)
21#define MWAITX_DISABLE_CSTATES 0xf
H. Peter Anvinbc83ccc2010-09-17 15:36:40 -070022
Peter Zijlstra16824252013-12-12 15:08:36 +010023static 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
Huang Ruif9675672015-08-10 12:19:53 +020031static inline void __monitorx(const void *eax, unsigned long ecx,
32 unsigned long edx)
33{
34 /* "monitorx %eax, %ecx, %edx;" */
35 asm volatile(".byte 0x0f, 0x01, 0xfa;"
36 :: "a" (eax), "c" (ecx), "d"(edx));
37}
38
Peter Zijlstra16824252013-12-12 15:08:36 +010039static inline void __mwait(unsigned long eax, unsigned long ecx)
40{
41 /* "mwait %eax, %ecx;" */
42 asm volatile(".byte 0x0f, 0x01, 0xc9;"
43 :: "a" (eax), "c" (ecx));
44}
45
Huang Ruif9675672015-08-10 12:19:53 +020046/*
47 * MWAITX allows for a timer expiration to get the core out a wait state in
48 * addition to the default MWAIT exit condition of a store appearing at a
49 * monitored virtual address.
50 *
51 * Registers:
52 *
53 * MWAITX ECX[1]: enable timer if set
54 * MWAITX EBX[31:0]: max wait time expressed in SW P0 clocks. The software P0
55 * frequency is the same as the TSC frequency.
56 *
57 * Below is a comparison between MWAIT and MWAITX on AMD processors:
58 *
59 * MWAIT MWAITX
60 * opcode 0f 01 c9 | 0f 01 fb
61 * ECX[0] value of RFLAGS.IF seen by instruction
62 * ECX[1] unused/#GP if set | enable timer if set
63 * ECX[31:2] unused/#GP if set
64 * EAX unused (reserve for hint)
65 * EBX[31:0] unused | max wait time (P0 clocks)
66 *
67 * MONITOR MONITORX
68 * opcode 0f 01 c8 | 0f 01 fa
69 * EAX (logical) address to monitor
70 * ECX #GP if not zero
71 */
72static inline void __mwaitx(unsigned long eax, unsigned long ebx,
73 unsigned long ecx)
74{
75 /* "mwaitx %eax, %ebx, %ecx;" */
76 asm volatile(".byte 0x0f, 0x01, 0xfb;"
77 :: "a" (eax), "b" (ebx), "c" (ecx));
78}
79
Len Brownb2531492014-01-15 00:37:34 -050080static inline void __sti_mwait(unsigned long eax, unsigned long ecx)
81{
82 trace_hardirqs_on();
83 /* "mwait %eax, %ecx;" */
84 asm volatile("sti; .byte 0x0f, 0x01, 0xc9;"
85 :: "a" (eax), "c" (ecx));
86}
87
Peter Zijlstra16824252013-12-12 15:08:36 +010088/*
89 * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
90 * which can obviate IPI to trigger checking of need_resched.
91 * We execute MONITOR against need_resched and enter optimized wait state
92 * through MWAIT. Whenever someone changes need_resched, we would be woken
93 * up from MWAIT (without an IPI).
94 *
95 * New with Core Duo processors, MWAIT can take some hints based on CPU
96 * capability.
97 */
98static inline void mwait_idle_with_hints(unsigned long eax, unsigned long ecx)
99{
Peter Zijlstra08e237f2016-07-18 11:41:10 -0700100 if (static_cpu_has_bug(X86_BUG_MONITOR) || !current_set_polling_and_test()) {
Borislav Petkov9b13a932014-06-18 00:06:23 +0200101 if (static_cpu_has_bug(X86_BUG_CLFLUSH_MONITOR)) {
H. Peter Anvin7e98b712013-12-19 11:58:16 -0800102 mb();
Peter Zijlstra16824252013-12-12 15:08:36 +0100103 clflush((void *)&current_thread_info()->flags);
H. Peter Anvin7e98b712013-12-19 11:58:16 -0800104 mb();
105 }
Peter Zijlstra16824252013-12-12 15:08:36 +0100106
107 __monitor((void *)&current_thread_info()->flags, 0, 0);
108 if (!need_resched())
109 __mwait(eax, ecx);
110 }
Peter Zijlstra8cb75e02013-11-20 12:22:37 +0100111 current_clr_polling();
Peter Zijlstra16824252013-12-12 15:08:36 +0100112}
113
H. Peter Anvinbc83ccc2010-09-17 15:36:40 -0700114#endif /* _ASM_X86_MWAIT_H */