1
2#ifndef _ASM_X86_MWAIT_H
3#define _ASM_X86_MWAIT_H
4
5#include <linux/sched.h>
6#include <linux/sched/idle.h>
7
8#include <asm/cpufeature.h>
9
10#define MWAIT_SUBSTATE_MASK 0xf
11#define MWAIT_CSTATE_MASK 0xf
12#define MWAIT_SUBSTATE_SIZE 4
13#define MWAIT_HINT2CSTATE(hint) (((hint) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK)
14#define MWAIT_HINT2SUBSTATE(hint) ((hint) & MWAIT_CSTATE_MASK)
15
16#define CPUID_MWAIT_LEAF 5
17#define CPUID5_ECX_EXTENSIONS_SUPPORTED 0x1
18#define CPUID5_ECX_INTERRUPT_BREAK 0x2
19
20#define MWAIT_ECX_INTERRUPT_BREAK 0x1
21#define MWAITX_ECX_TIMER_ENABLE BIT(1)
22#define MWAITX_MAX_LOOPS ((u32)-1)
23#define MWAITX_DISABLE_CSTATES 0xf
24
25static inline void __monitor(const void *eax, unsigned long ecx,
26 unsigned long edx)
27{
28
29 asm volatile(".byte 0x0f, 0x01, 0xc8;"
30 :: "a" (eax), "c" (ecx), "d"(edx));
31}
32
33static inline void __monitorx(const void *eax, unsigned long ecx,
34 unsigned long edx)
35{
36
37 asm volatile(".byte 0x0f, 0x01, 0xfa;"
38 :: "a" (eax), "c" (ecx), "d"(edx));
39}
40
41static inline void __mwait(unsigned long eax, unsigned long ecx)
42{
43
44 asm volatile(".byte 0x0f, 0x01, 0xc9;"
45 :: "a" (eax), "c" (ecx));
46}
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74static inline void __mwaitx(unsigned long eax, unsigned long ebx,
75 unsigned long ecx)
76{
77
78 asm volatile(".byte 0x0f, 0x01, 0xfb;"
79 :: "a" (eax), "b" (ebx), "c" (ecx));
80}
81
82static inline void __sti_mwait(unsigned long eax, unsigned long ecx)
83{
84 trace_hardirqs_on();
85
86 asm volatile("sti; .byte 0x0f, 0x01, 0xc9;"
87 :: "a" (eax), "c" (ecx));
88}
89
90
91
92
93
94
95
96
97
98
99
100static inline void mwait_idle_with_hints(unsigned long eax, unsigned long ecx)
101{
102 if (static_cpu_has_bug(X86_BUG_MONITOR) || !current_set_polling_and_test()) {
103 if (static_cpu_has_bug(X86_BUG_CLFLUSH_MONITOR)) {
104 mb();
105 clflush((void *)¤t_thread_info()->flags);
106 mb();
107 }
108
109 __monitor((void *)¤t_thread_info()->flags, 0, 0);
110 if (!need_resched())
111 __mwait(eax, ecx);
112 }
113 current_clr_polling();
114}
115
116#endif
117