blob: d710f2d167bb49505e56ed4c0e9c5cc91a52546e [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>
6 *
7 * The __delay function must _NOT_ be inlined as its execution time
8 * depends wildly on alignment on many x86 processors. The additional
9 * jump magic is needed to get the timing stable on all the CPU's
10 * we have to worry about.
11 */
12
john stultz6f84fa22006-06-26 00:25:11 -070013#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/sched.h>
Andrew Morton941e4922008-02-06 01:36:42 -080015#include <linux/timex.h>
Andrew Morton35d5d082007-11-14 17:00:41 -080016#include <linux/preempt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/delay.h>
Andrew Morton941e4922008-02-06 01:36:42 -080018#include <linux/init.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>
23
24#ifdef CONFIG_SMP
john stultz6f84fa22006-06-26 00:25:11 -070025# include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#endif
27
john stultz6f84fa22006-06-26 00:25:11 -070028/* simple loop based delay: */
29static void delay_loop(unsigned long loops)
30{
31 int d0;
32
33 __asm__ __volatile__(
34 "\tjmp 1f\n"
35 ".align 16\n"
36 "1:\tjmp 2f\n"
37 ".align 16\n"
38 "2:\tdecl %0\n\tjns 2b"
39 :"=&a" (d0)
40 :"0" (loops));
41}
42
43/* TSC based delay: */
44static void delay_tsc(unsigned long loops)
45{
46 unsigned long bclock, now;
Steven Rostedt5c1ea082008-05-25 11:13:32 -040047 int cpu;
john stultz6f84fa22006-06-26 00:25:11 -070048
Steven Rostedt5c1ea082008-05-25 11:13:32 -040049 preempt_disable();
50 cpu = smp_processor_id();
john stultz6f84fa22006-06-26 00:25:11 -070051 rdtscl(bclock);
Steven Rostedt5c1ea082008-05-25 11:13:32 -040052 for (;;) {
john stultz6f84fa22006-06-26 00:25:11 -070053 rdtscl(now);
Steven Rostedt5c1ea082008-05-25 11:13:32 -040054 if ((now - bclock) >= loops)
55 break;
56
57 /* Allow RT tasks to run */
58 preempt_enable();
59 rep_nop();
60 preempt_disable();
61
62 /*
63 * It is possible that we moved to another CPU, and
64 * since TSC's are per-cpu we need to calculate
65 * that. The delay must guarantee that we wait "at
66 * least" the amount of time. Being moved to another
67 * CPU could make the wait longer but we just need to
68 * make sure we waited long enough. Rebalance the
69 * counter for this CPU.
70 */
71 if (unlikely(cpu != smp_processor_id())) {
72 loops -= (now - bclock);
73 cpu = smp_processor_id();
74 rdtscl(bclock);
75 }
76 }
Andrew Morton35d5d082007-11-14 17:00:41 -080077 preempt_enable();
john stultz6f84fa22006-06-26 00:25:11 -070078}
79
80/*
81 * Since we calibrate only once at boot, this
82 * function should be set once at boot and not changed
83 */
84static void (*delay_fn)(unsigned long) = delay_loop;
85
86void use_tsc_delay(void)
87{
88 delay_fn = delay_tsc;
89}
90
Andrew Morton941e4922008-02-06 01:36:42 -080091int __devinit read_current_timer(unsigned long *timer_val)
john stultz6f84fa22006-06-26 00:25:11 -070092{
93 if (delay_fn == delay_tsc) {
94 rdtscl(*timer_val);
95 return 0;
96 }
97 return -1;
98}
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100void __delay(unsigned long loops)
101{
john stultz6f84fa22006-06-26 00:25:11 -0700102 delay_fn(loops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
105inline void __const_udelay(unsigned long xloops)
106{
107 int d0;
john stultz6f84fa22006-06-26 00:25:11 -0700108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 xloops *= 4;
110 __asm__("mull %0"
111 :"=d" (xloops), "=&a" (d0)
john stultz6f84fa22006-06-26 00:25:11 -0700112 :"1" (xloops), "0"
Mike Travis92cb7612007-10-19 20:35:04 +0200113 (cpu_data(raw_smp_processor_id()).loops_per_jiffy * (HZ/4)));
john stultz6f84fa22006-06-26 00:25:11 -0700114
115 __delay(++xloops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118void __udelay(unsigned long usecs)
119{
john stultz6f84fa22006-06-26 00:25:11 -0700120 __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123void __ndelay(unsigned long nsecs)
124{
john stultz6f84fa22006-06-26 00:25:11 -0700125 __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700127
128EXPORT_SYMBOL(__delay);
129EXPORT_SYMBOL(__const_udelay);
130EXPORT_SYMBOL(__udelay);
131EXPORT_SYMBOL(__ndelay);