Skip to content

Commit 46f90c7

Browse files
Kyung Min ParkKAGA-KOKO
authored andcommitted
x86/delay: Refactor delay_mwaitx() for TPAUSE support
Refactor code to make it easier to add a new model specific function to delay for a number of cycles. No functional change. Co-developed-by: Fenghua Yu <[email protected]> Signed-off-by: Fenghua Yu <[email protected]> Signed-off-by: Kyung Min Park <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Tony Luck <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent e882489 commit 46f90c7

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

arch/x86/lib/delay.c

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ static void delay_loop(u64 __loops);
3434
* during boot.
3535
*/
3636
static void (*delay_fn)(u64) __ro_after_init = delay_loop;
37+
static void (*delay_halt_fn)(u64 start, u64 cycles) __ro_after_init;
3738

3839
/* simple loop based delay: */
3940
static void delay_loop(u64 __loops)
@@ -100,9 +101,33 @@ static void delay_tsc(u64 cycles)
100101
* counts with TSC frequency. The input value is the number of TSC cycles
101102
* to wait. MWAITX will also exit when the timer expires.
102103
*/
103-
static void delay_mwaitx(u64 cycles)
104+
static void delay_halt_mwaitx(u64 unused, u64 cycles)
104105
{
105-
u64 start, end, delay;
106+
u64 delay;
107+
108+
delay = min_t(u64, MWAITX_MAX_WAIT_CYCLES, cycles);
109+
/*
110+
* Use cpu_tss_rw as a cacheline-aligned, seldomly accessed per-cpu
111+
* variable as the monitor target.
112+
*/
113+
__monitorx(raw_cpu_ptr(&cpu_tss_rw), 0, 0);
114+
115+
/*
116+
* AMD, like Intel, supports the EAX hint and EAX=0xf means, do not
117+
* enter any deep C-state and we use it here in delay() to minimize
118+
* wakeup latency.
119+
*/
120+
__mwaitx(MWAITX_DISABLE_CSTATES, delay, MWAITX_ECX_TIMER_ENABLE);
121+
}
122+
123+
/*
124+
* Call a vendor specific function to delay for a given amount of time. Because
125+
* these functions may return earlier than requested, check for actual elapsed
126+
* time and call again until done.
127+
*/
128+
static void delay_halt(u64 __cycles)
129+
{
130+
u64 start, end, cycles = __cycles;
106131

107132
/*
108133
* Timer value of 0 causes MWAITX to wait indefinitely, unless there
@@ -114,21 +139,7 @@ static void delay_mwaitx(u64 cycles)
114139
start = rdtsc_ordered();
115140

116141
for (;;) {
117-
delay = min_t(u64, MWAITX_MAX_WAIT_CYCLES, cycles);
118-
119-
/*
120-
* Use cpu_tss_rw as a cacheline-aligned, seldomly
121-
* accessed per-cpu variable as the monitor target.
122-
*/
123-
__monitorx(raw_cpu_ptr(&cpu_tss_rw), 0, 0);
124-
125-
/*
126-
* AMD, like Intel's MWAIT version, supports the EAX hint and
127-
* EAX=0xf0 means, do not enter any deep C-state and we use it
128-
* here in delay() to minimize wakeup latency.
129-
*/
130-
__mwaitx(MWAITX_DISABLE_CSTATES, delay, MWAITX_ECX_TIMER_ENABLE);
131-
142+
delay_halt_fn(start, cycles);
132143
end = rdtsc_ordered();
133144

134145
if (cycles <= end - start)
@@ -147,7 +158,8 @@ void __init use_tsc_delay(void)
147158

148159
void use_mwaitx_delay(void)
149160
{
150-
delay_fn = delay_mwaitx;
161+
delay_halt_fn = delay_halt_mwaitx;
162+
delay_fn = delay_halt;
151163
}
152164

153165
int read_current_timer(unsigned long *timer_val)

0 commit comments

Comments
 (0)