blob: 65d15df6212d67e4b936f0b9e764efd2b7737308 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Precise Delay Loops for i386
4 *
5 * Copyright (C) 1993 Linus Torvalds
6 * Copyright (C) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
Jiri Hladkye01b70e2008-06-02 12:00:19 +02007 * Copyright (C) 2008 Jiri Hladky <hladky _dot_ jiri _at_ gmail _dot_ com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * The __delay function must _NOT_ be inlined as its execution time
10 * depends wildly on alignment on many x86 processors. The additional
11 * jump magic is needed to get the timing stable on all the CPU's
12 * we have to worry about.
13 */
14
Paul Gortmakere6830142016-07-13 20:18:57 -040015#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/sched.h>
Andrew Morton941e4922008-02-06 01:36:42 -080017#include <linux/timex.h>
Andrew Morton35d5d082007-11-14 17:00:41 -080018#include <linux/preempt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/delay.h>
john stultz6f84fa22006-06-26 00:25:11 -070020
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/processor.h>
22#include <asm/delay.h>
23#include <asm/timer.h>
Huang Ruib466bdb2015-08-10 12:19:54 +020024#include <asm/mwait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#ifdef CONFIG_SMP
john stultz6f84fa22006-06-26 00:25:11 -070027# include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#endif
29
Thomas Gleixnere8824892020-04-24 12:37:54 -070030static void delay_loop(u64 __loops);
31
32/*
33 * Calibration and selection of the delay mechanism happens only once
34 * during boot.
35 */
36static void (*delay_fn)(u64) __ro_after_init = delay_loop;
Kyung Min Park46f90c72020-04-24 12:37:55 -070037static void (*delay_halt_fn)(u64 start, u64 cycles) __ro_after_init;
Thomas Gleixnere8824892020-04-24 12:37:54 -070038
john stultz6f84fa22006-06-26 00:25:11 -070039/* simple loop based delay: */
Thomas Gleixnere8824892020-04-24 12:37:54 -070040static void delay_loop(u64 __loops)
john stultz6f84fa22006-06-26 00:25:11 -070041{
Thomas Gleixnere8824892020-04-24 12:37:54 -070042 unsigned long loops = (unsigned long)__loops;
43
Glauber Costaf0fbf0a2008-07-03 12:35:41 -030044 asm volatile(
Jiri Hladkye01b70e2008-06-02 12:00:19 +020045 " test %0,%0 \n"
46 " jz 3f \n"
47 " jmp 1f \n"
48
49 ".align 16 \n"
50 "1: jmp 2f \n"
51
52 ".align 16 \n"
Glauber Costaff1b15b2008-06-24 09:27:19 -030053 "2: dec %0 \n"
Jiri Hladkye01b70e2008-06-02 12:00:19 +020054 " jnz 2b \n"
Glauber Costaff1b15b2008-06-24 09:27:19 -030055 "3: dec %0 \n"
Jiri Hladkye01b70e2008-06-02 12:00:19 +020056
57 : /* we don't need output */
58 :"a" (loops)
59 );
john stultz6f84fa22006-06-26 00:25:11 -070060}
61
62/* TSC based delay: */
Thomas Gleixnere8824892020-04-24 12:37:54 -070063static void delay_tsc(u64 cycles)
john stultz6f84fa22006-06-26 00:25:11 -070064{
Thomas Gleixnere8824892020-04-24 12:37:54 -070065 u64 bclock, now;
Steven Rostedt5c1ea082008-05-25 11:13:32 -040066 int cpu;
john stultz6f84fa22006-06-26 00:25:11 -070067
Steven Rostedt5c1ea082008-05-25 11:13:32 -040068 preempt_disable();
69 cpu = smp_processor_id();
Andy Lutomirski03b97302015-06-25 18:44:08 +020070 bclock = rdtsc_ordered();
Steven Rostedt5c1ea082008-05-25 11:13:32 -040071 for (;;) {
Andy Lutomirski03b97302015-06-25 18:44:08 +020072 now = rdtsc_ordered();
Thomas Gleixnere8824892020-04-24 12:37:54 -070073 if ((now - bclock) >= cycles)
Steven Rostedt5c1ea082008-05-25 11:13:32 -040074 break;
75
76 /* Allow RT tasks to run */
77 preempt_enable();
78 rep_nop();
79 preempt_disable();
80
81 /*
82 * It is possible that we moved to another CPU, and
83 * since TSC's are per-cpu we need to calculate
84 * that. The delay must guarantee that we wait "at
85 * least" the amount of time. Being moved to another
86 * CPU could make the wait longer but we just need to
87 * make sure we waited long enough. Rebalance the
88 * counter for this CPU.
89 */
90 if (unlikely(cpu != smp_processor_id())) {
Thomas Gleixnere8824892020-04-24 12:37:54 -070091 cycles -= (now - bclock);
Steven Rostedt5c1ea082008-05-25 11:13:32 -040092 cpu = smp_processor_id();
Andy Lutomirski03b97302015-06-25 18:44:08 +020093 bclock = rdtsc_ordered();
Steven Rostedt5c1ea082008-05-25 11:13:32 -040094 }
95 }
Andrew Morton35d5d082007-11-14 17:00:41 -080096 preempt_enable();
john stultz6f84fa22006-06-26 00:25:11 -070097}
98
99/*
Kyung Min Parkcec5f262020-04-24 12:37:56 -0700100 * On Intel the TPAUSE instruction waits until any of:
101 * 1) the TSC counter exceeds the value provided in EDX:EAX
102 * 2) global timeout in IA32_UMWAIT_CONTROL is exceeded
103 * 3) an external interrupt occurs
104 */
105static void delay_halt_tpause(u64 start, u64 cycles)
106{
107 u64 until = start + cycles;
108 u32 eax, edx;
109
110 eax = lower_32_bits(until);
111 edx = upper_32_bits(until);
112
113 /*
114 * Hard code the deeper (C0.2) sleep state because exit latency is
115 * small compared to the "microseconds" that usleep() will delay.
116 */
117 __tpause(TPAUSE_C02_STATE, edx, eax);
118}
119
120/*
Huang Ruib466bdb2015-08-10 12:19:54 +0200121 * On some AMD platforms, MWAITX has a configurable 32-bit timer, that
Thomas Gleixnere8824892020-04-24 12:37:54 -0700122 * counts with TSC frequency. The input value is the number of TSC cycles
123 * to wait. MWAITX will also exit when the timer expires.
Huang Ruib466bdb2015-08-10 12:19:54 +0200124 */
Kyung Min Park46f90c72020-04-24 12:37:55 -0700125static void delay_halt_mwaitx(u64 unused, u64 cycles)
Huang Ruib466bdb2015-08-10 12:19:54 +0200126{
Kyung Min Park46f90c72020-04-24 12:37:55 -0700127 u64 delay;
128
129 delay = min_t(u64, MWAITX_MAX_WAIT_CYCLES, cycles);
130 /*
131 * Use cpu_tss_rw as a cacheline-aligned, seldomly accessed per-cpu
132 * variable as the monitor target.
133 */
134 __monitorx(raw_cpu_ptr(&cpu_tss_rw), 0, 0);
135
136 /*
137 * AMD, like Intel, supports the EAX hint and EAX=0xf means, do not
138 * enter any deep C-state and we use it here in delay() to minimize
139 * wakeup latency.
140 */
141 __mwaitx(MWAITX_DISABLE_CSTATES, delay, MWAITX_ECX_TIMER_ENABLE);
142}
143
144/*
145 * Call a vendor specific function to delay for a given amount of time. Because
146 * these functions may return earlier than requested, check for actual elapsed
147 * time and call again until done.
148 */
149static void delay_halt(u64 __cycles)
150{
151 u64 start, end, cycles = __cycles;
Huang Ruib466bdb2015-08-10 12:19:54 +0200152
Janakarajan Natarajan88d879d2017-04-25 16:44:03 -0500153 /*
154 * Timer value of 0 causes MWAITX to wait indefinitely, unless there
155 * is a store on the memory monitored by MONITORX.
156 */
Thomas Gleixnere8824892020-04-24 12:37:54 -0700157 if (!cycles)
Janakarajan Natarajan88d879d2017-04-25 16:44:03 -0500158 return;
159
Huang Ruib466bdb2015-08-10 12:19:54 +0200160 start = rdtsc_ordered();
161
162 for (;;) {
Kyung Min Park46f90c72020-04-24 12:37:55 -0700163 delay_halt_fn(start, cycles);
Huang Ruib466bdb2015-08-10 12:19:54 +0200164 end = rdtsc_ordered();
165
Thomas Gleixnere8824892020-04-24 12:37:54 -0700166 if (cycles <= end - start)
Huang Ruib466bdb2015-08-10 12:19:54 +0200167 break;
168
Thomas Gleixnere8824892020-04-24 12:37:54 -0700169 cycles -= end - start;
Huang Ruib466bdb2015-08-10 12:19:54 +0200170 start = end;
171 }
172}
173
Thomas Gleixnere8824892020-04-24 12:37:54 -0700174void __init use_tsc_delay(void)
john stultz6f84fa22006-06-26 00:25:11 -0700175{
Huang Ruib466bdb2015-08-10 12:19:54 +0200176 if (delay_fn == delay_loop)
177 delay_fn = delay_tsc;
178}
179
Kyung Min Parkcec5f262020-04-24 12:37:56 -0700180void __init use_tpause_delay(void)
181{
182 delay_halt_fn = delay_halt_tpause;
183 delay_fn = delay_halt;
184}
185
Huang Ruib466bdb2015-08-10 12:19:54 +0200186void use_mwaitx_delay(void)
187{
Kyung Min Park46f90c72020-04-24 12:37:55 -0700188 delay_halt_fn = delay_halt_mwaitx;
189 delay_fn = delay_halt;
john stultz6f84fa22006-06-26 00:25:11 -0700190}
191
Greg Kroah-Hartmana18e3692012-12-21 14:02:53 -0800192int read_current_timer(unsigned long *timer_val)
john stultz6f84fa22006-06-26 00:25:11 -0700193{
194 if (delay_fn == delay_tsc) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +0200195 *timer_val = rdtsc();
john stultz6f84fa22006-06-26 00:25:11 -0700196 return 0;
197 }
198 return -1;
199}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201void __delay(unsigned long loops)
202{
john stultz6f84fa22006-06-26 00:25:11 -0700203 delay_fn(loops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
Glauber Costaf0fbf0a2008-07-03 12:35:41 -0300205EXPORT_SYMBOL(__delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Andi Kleen81423c32019-03-29 17:47:38 -0700207noinline void __const_udelay(unsigned long xloops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Jiri Slaby4c45c512017-01-19 12:47:30 +0100209 unsigned long lpj = this_cpu_read(cpu_info.loops_per_jiffy) ? : loops_per_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 int d0;
john stultz6f84fa22006-06-26 00:25:11 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 xloops *= 4;
Glauber Costaf0fbf0a2008-07-03 12:35:41 -0300213 asm("mull %%edx"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 :"=d" (xloops), "=&a" (d0)
Jiri Slaby4c45c512017-01-19 12:47:30 +0100215 :"1" (xloops), "0" (lpj * (HZ / 4)));
john stultz6f84fa22006-06-26 00:25:11 -0700216
217 __delay(++xloops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
Glauber Costaf0fbf0a2008-07-03 12:35:41 -0300219EXPORT_SYMBOL(__const_udelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221void __udelay(unsigned long usecs)
222{
john stultz6f84fa22006-06-26 00:25:11 -0700223 __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
Glauber Costaf0fbf0a2008-07-03 12:35:41 -0300225EXPORT_SYMBOL(__udelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227void __ndelay(unsigned long nsecs)
228{
john stultz6f84fa22006-06-26 00:25:11 -0700229 __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700231EXPORT_SYMBOL(__ndelay);