blob: fc45ba887d051e504dd592be40ec2e78d70eea33 [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>
Andrew Morton941e4922008-02-06 01:36:42 -080019#include <linux/init.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>
24
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: */
51static void delay_tsc(unsigned long loops)
52{
53 unsigned long bclock, now;
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();
Pallipadi, Venkateshe888d7f2009-06-25 16:44:31 -070058 rdtsc_barrier();
john stultz6f84fa22006-06-26 00:25:11 -070059 rdtscl(bclock);
Steven Rostedt5c1ea082008-05-25 11:13:32 -040060 for (;;) {
Pallipadi, Venkateshe888d7f2009-06-25 16:44:31 -070061 rdtsc_barrier();
john stultz6f84fa22006-06-26 00:25:11 -070062 rdtscl(now);
Steven Rostedt5c1ea082008-05-25 11:13:32 -040063 if ((now - bclock) >= loops)
64 break;
65
66 /* Allow RT tasks to run */
67 preempt_enable();
68 rep_nop();
69 preempt_disable();
70
71 /*
72 * It is possible that we moved to another CPU, and
73 * since TSC's are per-cpu we need to calculate
74 * that. The delay must guarantee that we wait "at
75 * least" the amount of time. Being moved to another
76 * CPU could make the wait longer but we just need to
77 * make sure we waited long enough. Rebalance the
78 * counter for this CPU.
79 */
80 if (unlikely(cpu != smp_processor_id())) {
81 loops -= (now - bclock);
82 cpu = smp_processor_id();
Pallipadi, Venkateshe888d7f2009-06-25 16:44:31 -070083 rdtsc_barrier();
Steven Rostedt5c1ea082008-05-25 11:13:32 -040084 rdtscl(bclock);
85 }
86 }
Andrew Morton35d5d082007-11-14 17:00:41 -080087 preempt_enable();
john stultz6f84fa22006-06-26 00:25:11 -070088}
89
90/*
91 * Since we calibrate only once at boot, this
92 * function should be set once at boot and not changed
93 */
94static void (*delay_fn)(unsigned long) = delay_loop;
95
96void use_tsc_delay(void)
97{
98 delay_fn = delay_tsc;
99}
100
Andrew Morton941e4922008-02-06 01:36:42 -0800101int __devinit read_current_timer(unsigned long *timer_val)
john stultz6f84fa22006-06-26 00:25:11 -0700102{
103 if (delay_fn == delay_tsc) {
Glauber Costaa76febe2008-06-24 09:52:36 -0300104 rdtscll(*timer_val);
john stultz6f84fa22006-06-26 00:25:11 -0700105 return 0;
106 }
107 return -1;
108}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110void __delay(unsigned long loops)
111{
john stultz6f84fa22006-06-26 00:25:11 -0700112 delay_fn(loops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
Glauber Costaf0fbf0a2008-07-03 12:35:41 -0300114EXPORT_SYMBOL(__delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116inline void __const_udelay(unsigned long xloops)
117{
118 int d0;
john stultz6f84fa22006-06-26 00:25:11 -0700119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 xloops *= 4;
Glauber Costaf0fbf0a2008-07-03 12:35:41 -0300121 asm("mull %%edx"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 :"=d" (xloops), "=&a" (d0)
john stultz6f84fa22006-06-26 00:25:11 -0700123 :"1" (xloops), "0"
Christoph Lameter357089f2010-12-16 12:14:43 -0600124 (this_cpu_read(cpu_info.loops_per_jiffy) * (HZ/4)));
john stultz6f84fa22006-06-26 00:25:11 -0700125
126 __delay(++xloops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
Glauber Costaf0fbf0a2008-07-03 12:35:41 -0300128EXPORT_SYMBOL(__const_udelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130void __udelay(unsigned long usecs)
131{
john stultz6f84fa22006-06-26 00:25:11 -0700132 __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
Glauber Costaf0fbf0a2008-07-03 12:35:41 -0300134EXPORT_SYMBOL(__udelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136void __ndelay(unsigned long nsecs)
137{
john stultz6f84fa22006-06-26 00:25:11 -0700138 __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700140EXPORT_SYMBOL(__ndelay);