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#include <asm/nospec-branch.h>
10
11#define MWAIT_SUBSTATE_MASK 0xf
12#define MWAIT_CSTATE_MASK 0xf
13#define MWAIT_SUBSTATE_SIZE 4
14#define MWAIT_HINT2CSTATE(hint) (((hint) >> MWAIT_SUBSTATE_SIZE) & MWAIT_CSTATE_MASK)
15#define MWAIT_HINT2SUBSTATE(hint) ((hint) & MWAIT_CSTATE_MASK)
16
17#define CPUID_MWAIT_LEAF 5
18#define CPUID5_ECX_EXTENSIONS_SUPPORTED 0x1
19#define CPUID5_ECX_INTERRUPT_BREAK 0x2
20
21#define MWAIT_ECX_INTERRUPT_BREAK 0x1
22#define MWAITX_ECX_TIMER_ENABLE BIT(1)
23#define MWAITX_MAX_LOOPS ((u32)-1)
24#define MWAITX_DISABLE_CSTATES 0xf0
25
26static inline void __monitor(const void *eax, unsigned long ecx,
27 unsigned long edx)
28{
29
30 asm volatile(".byte 0x0f, 0x01, 0xc8;"
31 :: "a" (eax), "c" (ecx), "d"(edx));
32}
33
34static inline void __monitorx(const void *eax, unsigned long ecx,
35 unsigned long edx)
36{
37
38 asm volatile(".byte 0x0f, 0x01, 0xfa;"
39 :: "a" (eax), "c" (ecx), "d"(edx));
40}
41
42static inline void __mwait(unsigned long eax, unsigned long ecx)
43{
44 mds_idle_clear_cpu_buffers();
45
46
47 asm volatile(".byte 0x0f, 0x01, 0xc9;"
48 :: "a" (eax), "c" (ecx));
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
74
75
76
77static inline void __mwaitx(unsigned long eax, unsigned long ebx,
78 unsigned long ecx)
79{
80
81
82
83 asm volatile(".byte 0x0f, 0x01, 0xfb;"
84 :: "a" (eax), "b" (ebx), "c" (ecx));
85}
86
87static inline void __sti_mwait(unsigned long eax, unsigned long ecx)
88{
89 trace_hardirqs_on();
90
91 mds_idle_clear_cpu_buffers();
92
93 asm volatile("sti; .byte 0x0f, 0x01, 0xc9;"
94 :: "a" (eax), "c" (ecx));
95}
96
97
98
99
100
101
102
103
104
105
106
107static inline void mwait_idle_with_hints(unsigned long eax, unsigned long ecx)
108{
109 if (static_cpu_has_bug(X86_BUG_MONITOR) || !current_set_polling_and_test()) {
110 if (static_cpu_has_bug(X86_BUG_CLFLUSH_MONITOR)) {
111 mb();
112 clflush((void *)¤t_thread_info()->flags);
113 mb();
114 }
115
116 __monitor((void *)¤t_thread_info()->flags, 0, 0);
117 if (!need_resched())
118 __mwait(eax, ecx);
119 }
120 current_clr_polling();
121}
122
123#endif
124