2 * Precise Delay Loops for i386
4 * Copyright (C) 1993 Linus Torvalds
5 * Copyright (C) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
6 * Copyright (C) 2008 Jiri Hladky <hladky _dot_ jiri _at_ gmail _dot_ com>
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.
14 #include <linux/module.h>
15 #include <linux/sched.h>
16 #include <linux/timex.h>
17 #include <linux/preempt.h>
18 #include <linux/delay.h>
19 #include <linux/init.h>
21 #include <asm/processor.h>
22 #include <asm/delay.h>
23 #include <asm/timer.h>
29 /* simple loop based delay: */
30 static void delay_loop(unsigned long loops)
45 : /* we don't need output */
50 /* TSC based delay: */
51 static void delay_tsc(unsigned long loops)
53 unsigned long bclock, now;
57 cpu = smp_processor_id();
61 if ((now - bclock) >= loops)
64 /* Allow RT tasks to run */
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.
78 if (unlikely(cpu != smp_processor_id())) {
79 loops -= (now - bclock);
80 cpu = smp_processor_id();
88 * Since we calibrate only once at boot, this
89 * function should be set once at boot and not changed
91 static void (*delay_fn)(unsigned long) = delay_loop;
93 void use_tsc_delay(void)
98 int __devinit read_current_timer(unsigned long *timer_val)
100 if (delay_fn == delay_tsc) {
107 void __delay(unsigned long loops)
111 EXPORT_SYMBOL(__delay);
113 inline void __const_udelay(unsigned long xloops)
119 :"=d" (xloops), "=&a" (d0)
121 (cpu_data(raw_smp_processor_id()).loops_per_jiffy * (HZ/4)));
125 EXPORT_SYMBOL(__const_udelay);
127 void __udelay(unsigned long usecs)
129 __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
131 EXPORT_SYMBOL(__udelay);
133 void __ndelay(unsigned long nsecs)
135 __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
137 EXPORT_SYMBOL(__ndelay);