blob: 9758524ee99f79260b2feb1307a20af9957a13a4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Precise Delay Loops for i386
3 *
4 * Copyright (C) 1993 Linus Torvalds
5 * Copyright (C) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
Jiri Hladkye01b70e2008-06-02 12:00:19 +02006 * Copyright (C) 2008 Jiri Hladky <hladky _dot_ jiri _at_ gmail _dot_ com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * The __delay function must _NOT_ be inlined as its execution time
9 * depends wildly on alignment on many x86 processors. The additional
10 * jump magic is needed to get the timing stable on all the CPU's
11 * we have to worry about.
12 */
13
Paul Gortmakere6830142016-07-13 20:18:57 -040014#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/sched.h>
Andrew Morton941e4922008-02-06 01:36:42 -080016#include <linux/timex.h>
Andrew Morton35d5d082007-11-14 17:00:41 -080017#include <linux/preempt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/delay.h>
john stultz6f84fa22006-06-26 00:25:11 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/processor.h>
21#include <asm/delay.h>
22#include <asm/timer.h>
Huang Ruib466bdb2015-08-10 12:19:54 +020023#include <asm/mwait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#ifdef CONFIG_SMP
john stultz6f84fa22006-06-26 00:25:11 -070026# include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#endif
28
john stultz6f84fa22006-06-26 00:25:11 -070029/* simple loop based delay: */
30static void delay_loop(unsigned long loops)
31{
Glauber Costaf0fbf0a2008-07-03 12:35:41 -030032 asm volatile(
Jiri Hladkye01b70e2008-06-02 12:00:19 +020033 " test %0,%0 \n"
34 " jz 3f \n"
35 " jmp 1f \n"
36
37 ".align 16 \n"
38 "1: jmp 2f \n"
39
40 ".align 16 \n"
Glauber Costaff1b15b2008-06-24 09:27:19 -030041 "2: dec %0 \n"
Jiri Hladkye01b70e2008-06-02 12:00:19 +020042 " jnz 2b \n"
Glauber Costaff1b15b2008-06-24 09:27:19 -030043 "3: dec %0 \n"
Jiri Hladkye01b70e2008-06-02 12:00:19 +020044
45 : /* we don't need output */
46 :"a" (loops)
47 );
john stultz6f84fa22006-06-26 00:25:11 -070048}
49
50/* TSC based delay: */
Thomas Gleixnera7f42552012-03-09 20:55:10 +010051static void delay_tsc(unsigned long __loops)
john stultz6f84fa22006-06-26 00:25:11 -070052{
Andy Lutomirski9cfa1a02015-06-25 18:44:00 +020053 u64 bclock, now, loops = __loops;
Steven Rostedt5c1ea082008-05-25 11:13:32 -040054 int cpu;
john stultz6f84fa22006-06-26 00:25:11 -070055
Steven Rostedt5c1ea082008-05-25 11:13:32 -040056 preempt_disable();
57 cpu = smp_processor_id();
Andy Lutomirski03b97302015-06-25 18:44:08 +020058 bclock = rdtsc_ordered();
Steven Rostedt5c1ea082008-05-25 11:13:32 -040059 for (;;) {
Andy Lutomirski03b97302015-06-25 18:44:08 +020060 now = rdtsc_ordered();
Steven Rostedt5c1ea082008-05-25 11:13:32 -040061 if ((now - bclock) >= loops)
62 break;
63
64 /* Allow RT tasks to run */
65 preempt_enable();
66 rep_nop();
67 preempt_disable();
68
69 /*
70 * It is possible that we moved to another CPU, and
71 * since TSC's are per-cpu we need to calculate
72 * that. The delay must guarantee that we wait "at
73 * least" the amount of time. Being moved to another
74 * CPU could make the wait longer but we just need to
75 * make sure we waited long enough. Rebalance the
76 * counter for this CPU.
77 */
78 if (unlikely(cpu != smp_processor_id())) {
79 loops -= (now - bclock);
80 cpu = smp_processor_id();
Andy Lutomirski03b97302015-06-25 18:44:08 +020081 bclock = rdtsc_ordered();
Steven Rostedt5c1ea082008-05-25 11:13:32 -040082 }
83 }
Andrew Morton35d5d082007-11-14 17:00:41 -080084 preempt_enable();
john stultz6f84fa22006-06-26 00:25:11 -070085}
86
87/*
Huang Ruib466bdb2015-08-10 12:19:54 +020088 * On some AMD platforms, MWAITX has a configurable 32-bit timer, that
89 * counts with TSC frequency. The input value is the loop of the
90 * counter, it will exit when the timer expires.
91 */
92static void delay_mwaitx(unsigned long __loops)
93{
94 u64 start, end, delay, loops = __loops;
95
Janakarajan Natarajan318e17d2017-04-25 16:44:03 -050096 /*
97 * Timer value of 0 causes MWAITX to wait indefinitely, unless there
98 * is a store on the memory monitored by MONITORX.
99 */
100 if (loops == 0)
101 return;
102
Huang Ruib466bdb2015-08-10 12:19:54 +0200103 start = rdtsc_ordered();
104
105 for (;;) {
106 delay = min_t(u64, MWAITX_MAX_LOOPS, loops);
107
108 /*
109 * Use cpu_tss as a cacheline-aligned, seldomly
110 * accessed per-cpu variable as the monitor target.
111 */
Borislav Petkov84477332016-03-09 21:56:22 +0100112 __monitorx(raw_cpu_ptr(&cpu_tss), 0, 0);
Huang Ruib466bdb2015-08-10 12:19:54 +0200113
114 /*
115 * AMD, like Intel, supports the EAX hint and EAX=0xf
116 * means, do not enter any deep C-state and we use it
117 * here in delay() to minimize wakeup latency.
118 */
119 __mwaitx(MWAITX_DISABLE_CSTATES, delay, MWAITX_ECX_TIMER_ENABLE);
120
121 end = rdtsc_ordered();
122
123 if (loops <= end - start)
124 break;
125
126 loops -= end - start;
127
128 start = end;
129 }
130}
131
132/*
john stultz6f84fa22006-06-26 00:25:11 -0700133 * Since we calibrate only once at boot, this
134 * function should be set once at boot and not changed
135 */
136static void (*delay_fn)(unsigned long) = delay_loop;
137
138void use_tsc_delay(void)
139{
Huang Ruib466bdb2015-08-10 12:19:54 +0200140 if (delay_fn == delay_loop)
141 delay_fn = delay_tsc;
142}
143
144void use_mwaitx_delay(void)
145{
146 delay_fn = delay_mwaitx;
john stultz6f84fa22006-06-26 00:25:11 -0700147}
148
Greg Kroah-Hartmana18e3692012-12-21 14:02:53 -0800149int read_current_timer(unsigned long *timer_val)
john stultz6f84fa22006-06-26 00:25:11 -0700150{
151 if (delay_fn == delay_tsc) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +0200152 *timer_val = rdtsc();
john stultz6f84fa22006-06-26 00:25:11 -0700153 return 0;
154 }
155 return -1;
156}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158void __delay(unsigned long loops)
159{
john stultz6f84fa22006-06-26 00:25:11 -0700160 delay_fn(loops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
Glauber Costaf0fbf0a2008-07-03 12:35:41 -0300162EXPORT_SYMBOL(__delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164inline void __const_udelay(unsigned long xloops)
165{
166 int d0;
john stultz6f84fa22006-06-26 00:25:11 -0700167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 xloops *= 4;
Glauber Costaf0fbf0a2008-07-03 12:35:41 -0300169 asm("mull %%edx"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 :"=d" (xloops), "=&a" (d0)
john stultz6f84fa22006-06-26 00:25:11 -0700171 :"1" (xloops), "0"
Christoph Lameter357089f2010-12-16 12:14:43 -0600172 (this_cpu_read(cpu_info.loops_per_jiffy) * (HZ/4)));
john stultz6f84fa22006-06-26 00:25:11 -0700173
174 __delay(++xloops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
Glauber Costaf0fbf0a2008-07-03 12:35:41 -0300176EXPORT_SYMBOL(__const_udelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178void __udelay(unsigned long usecs)
179{
john stultz6f84fa22006-06-26 00:25:11 -0700180 __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
Glauber Costaf0fbf0a2008-07-03 12:35:41 -0300182EXPORT_SYMBOL(__udelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184void __ndelay(unsigned long nsecs)
185{
john stultz6f84fa22006-06-26 00:25:11 -0700186 __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700188EXPORT_SYMBOL(__ndelay);