blob: e912b2f6d36e5392b33bf6749f19a53cff6d7a06 [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
john stultz6f84fa22006-06-26 00:25:11 -070014#include <linux/module.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
96 start = rdtsc_ordered();
97
98 for (;;) {
99 delay = min_t(u64, MWAITX_MAX_LOOPS, loops);
100
101 /*
102 * Use cpu_tss as a cacheline-aligned, seldomly
103 * accessed per-cpu variable as the monitor target.
104 */
105 __monitorx(this_cpu_ptr(&cpu_tss), 0, 0);
106
107 /*
108 * AMD, like Intel, supports the EAX hint and EAX=0xf
109 * means, do not enter any deep C-state and we use it
110 * here in delay() to minimize wakeup latency.
111 */
112 __mwaitx(MWAITX_DISABLE_CSTATES, delay, MWAITX_ECX_TIMER_ENABLE);
113
114 end = rdtsc_ordered();
115
116 if (loops <= end - start)
117 break;
118
119 loops -= end - start;
120
121 start = end;
122 }
123}
124
125/*
john stultz6f84fa22006-06-26 00:25:11 -0700126 * Since we calibrate only once at boot, this
127 * function should be set once at boot and not changed
128 */
129static void (*delay_fn)(unsigned long) = delay_loop;
130
131void use_tsc_delay(void)
132{
Huang Ruib466bdb2015-08-10 12:19:54 +0200133 if (delay_fn == delay_loop)
134 delay_fn = delay_tsc;
135}
136
137void use_mwaitx_delay(void)
138{
139 delay_fn = delay_mwaitx;
john stultz6f84fa22006-06-26 00:25:11 -0700140}
141
Greg Kroah-Hartmana18e3692012-12-21 14:02:53 -0800142int read_current_timer(unsigned long *timer_val)
john stultz6f84fa22006-06-26 00:25:11 -0700143{
144 if (delay_fn == delay_tsc) {
Andy Lutomirski4ea16362015-06-25 18:44:07 +0200145 *timer_val = rdtsc();
john stultz6f84fa22006-06-26 00:25:11 -0700146 return 0;
147 }
148 return -1;
149}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151void __delay(unsigned long loops)
152{
john stultz6f84fa22006-06-26 00:25:11 -0700153 delay_fn(loops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
Glauber Costaf0fbf0a2008-07-03 12:35:41 -0300155EXPORT_SYMBOL(__delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157inline void __const_udelay(unsigned long xloops)
158{
159 int d0;
john stultz6f84fa22006-06-26 00:25:11 -0700160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 xloops *= 4;
Glauber Costaf0fbf0a2008-07-03 12:35:41 -0300162 asm("mull %%edx"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 :"=d" (xloops), "=&a" (d0)
john stultz6f84fa22006-06-26 00:25:11 -0700164 :"1" (xloops), "0"
Christoph Lameter357089f2010-12-16 12:14:43 -0600165 (this_cpu_read(cpu_info.loops_per_jiffy) * (HZ/4)));
john stultz6f84fa22006-06-26 00:25:11 -0700166
167 __delay(++xloops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
Glauber Costaf0fbf0a2008-07-03 12:35:41 -0300169EXPORT_SYMBOL(__const_udelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171void __udelay(unsigned long usecs)
172{
john stultz6f84fa22006-06-26 00:25:11 -0700173 __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
Glauber Costaf0fbf0a2008-07-03 12:35:41 -0300175EXPORT_SYMBOL(__udelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177void __ndelay(unsigned long nsecs)
178{
john stultz6f84fa22006-06-26 00:25:11 -0700179 __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700181EXPORT_SYMBOL(__ndelay);