Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / arch / x86 / lib / delay.c
blob39d6a3db0b964a081d8cda51c76083ec8c682f10
1 /*
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>
20 #include <asm/processor.h>
21 #include <asm/delay.h>
22 #include <asm/timer.h>
24 #ifdef CONFIG_SMP
25 # include <asm/smp.h>
26 #endif
28 /* simple loop based delay: */
29 static void delay_loop(unsigned long loops)
31 asm volatile(
32 " test %0,%0 \n"
33 " jz 3f \n"
34 " jmp 1f \n"
36 ".align 16 \n"
37 "1: jmp 2f \n"
39 ".align 16 \n"
40 "2: dec %0 \n"
41 " jnz 2b \n"
42 "3: dec %0 \n"
44 : /* we don't need output */
45 :"a" (loops)
49 /* TSC based delay: */
50 static void delay_tsc(unsigned long __loops)
52 u32 bclock, now, loops = __loops;
53 int cpu;
55 preempt_disable();
56 cpu = smp_processor_id();
57 rdtsc_barrier();
58 rdtscl(bclock);
59 for (;;) {
60 rdtsc_barrier();
61 rdtscl(now);
62 if ((now - bclock) >= loops)
63 break;
65 /* Allow RT tasks to run */
66 preempt_enable();
67 rep_nop();
68 preempt_disable();
71 * It is possible that we moved to another CPU, and
72 * since TSC's are per-cpu we need to calculate
73 * that. The delay must guarantee that we wait "at
74 * least" the amount of time. Being moved to another
75 * CPU could make the wait longer but we just need to
76 * make sure we waited long enough. Rebalance the
77 * counter for this CPU.
79 if (unlikely(cpu != smp_processor_id())) {
80 loops -= (now - bclock);
81 cpu = smp_processor_id();
82 rdtsc_barrier();
83 rdtscl(bclock);
86 preempt_enable();
90 * Since we calibrate only once at boot, this
91 * function should be set once at boot and not changed
93 static void (*delay_fn)(unsigned long) = delay_loop;
95 void use_tsc_delay(void)
97 delay_fn = delay_tsc;
100 int read_current_timer(unsigned long *timer_val)
102 if (delay_fn == delay_tsc) {
103 rdtscll(*timer_val);
104 return 0;
106 return -1;
109 void __delay(unsigned long loops)
111 delay_fn(loops);
113 EXPORT_SYMBOL(__delay);
115 inline void __const_udelay(unsigned long xloops)
117 int d0;
119 xloops *= 4;
120 asm("mull %%edx"
121 :"=d" (xloops), "=&a" (d0)
122 :"1" (xloops), "0"
123 (this_cpu_read(cpu_info.loops_per_jiffy) * (HZ/4)));
125 __delay(++xloops);
127 EXPORT_SYMBOL(__const_udelay);
129 void __udelay(unsigned long usecs)
131 __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
133 EXPORT_SYMBOL(__udelay);
135 void __ndelay(unsigned long nsecs)
137 __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
139 EXPORT_SYMBOL(__ndelay);