2 * Precise Delay Loops for i386
4 * Copyright (C) 1993 Linus Torvalds
5 * Copyright (C) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
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.
13 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/timex.h>
16 #include <linux/preempt.h>
17 #include <linux/delay.h>
18 #include <linux/init.h>
20 #include <asm/processor.h>
21 #include <asm/delay.h>
22 #include <asm/timer.h>
28 /* simple loop based delay: */
29 static void delay_loop(unsigned long loops)
38 "2:\tdecl %0\n\tjns 2b"
43 /* TSC based delay: */
44 static void delay_tsc(unsigned long loops)
46 unsigned long bclock, now;
50 cpu = smp_processor_id();
54 if ((now - bclock) >= loops)
57 /* Allow RT tasks to run */
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.
71 if (unlikely(cpu != smp_processor_id())) {
72 loops -= (now - bclock);
73 cpu = smp_processor_id();
81 * Since we calibrate only once at boot, this
82 * function should be set once at boot and not changed
84 static void (*delay_fn)(unsigned long) = delay_loop;
86 void use_tsc_delay(void)
91 int __devinit read_current_timer(unsigned long *timer_val)
93 if (delay_fn == delay_tsc) {
100 void __delay(unsigned long loops)
105 inline void __const_udelay(unsigned long xloops)
111 :"=d" (xloops), "=&a" (d0)
113 (cpu_data(raw_smp_processor_id()).loops_per_jiffy * (HZ/4)));
118 void __udelay(unsigned long usecs)
120 __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
123 void __ndelay(unsigned long nsecs)
125 __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
128 EXPORT_SYMBOL(__delay);
129 EXPORT_SYMBOL(__const_udelay);
130 EXPORT_SYMBOL(__udelay);
131 EXPORT_SYMBOL(__ndelay);