blob: aad9d95469dc68dcb25b9fac93b743d44b7d9038 [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 Morton35d5d082007-11-14 17:00:41 -080015#include <linux/preempt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/delay.h>
john stultz6f84fa22006-06-26 00:25:11 -070017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/processor.h>
19#include <asm/delay.h>
20#include <asm/timer.h>
21
22#ifdef CONFIG_SMP
john stultz6f84fa22006-06-26 00:25:11 -070023# include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#endif
25
john stultz6f84fa22006-06-26 00:25:11 -070026/* simple loop based delay: */
27static void delay_loop(unsigned long loops)
28{
29 int d0;
30
31 __asm__ __volatile__(
32 "\tjmp 1f\n"
33 ".align 16\n"
34 "1:\tjmp 2f\n"
35 ".align 16\n"
36 "2:\tdecl %0\n\tjns 2b"
37 :"=&a" (d0)
38 :"0" (loops));
39}
40
41/* TSC based delay: */
42static void delay_tsc(unsigned long loops)
43{
44 unsigned long bclock, now;
45
Andrew Morton35d5d082007-11-14 17:00:41 -080046 preempt_disable(); /* TSC's are per-cpu */
john stultz6f84fa22006-06-26 00:25:11 -070047 rdtscl(bclock);
48 do {
49 rep_nop();
50 rdtscl(now);
51 } while ((now-bclock) < loops);
Andrew Morton35d5d082007-11-14 17:00:41 -080052 preempt_enable();
john stultz6f84fa22006-06-26 00:25:11 -070053}
54
55/*
56 * Since we calibrate only once at boot, this
57 * function should be set once at boot and not changed
58 */
59static void (*delay_fn)(unsigned long) = delay_loop;
60
61void use_tsc_delay(void)
62{
63 delay_fn = delay_tsc;
64}
65
66int read_current_timer(unsigned long *timer_val)
67{
68 if (delay_fn == delay_tsc) {
69 rdtscl(*timer_val);
70 return 0;
71 }
72 return -1;
73}
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75void __delay(unsigned long loops)
76{
john stultz6f84fa22006-06-26 00:25:11 -070077 delay_fn(loops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
80inline void __const_udelay(unsigned long xloops)
81{
82 int d0;
john stultz6f84fa22006-06-26 00:25:11 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 xloops *= 4;
85 __asm__("mull %0"
86 :"=d" (xloops), "=&a" (d0)
john stultz6f84fa22006-06-26 00:25:11 -070087 :"1" (xloops), "0"
Mike Travis92cb7612007-10-19 20:35:04 +020088 (cpu_data(raw_smp_processor_id()).loops_per_jiffy * (HZ/4)));
john stultz6f84fa22006-06-26 00:25:11 -070089
90 __delay(++xloops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93void __udelay(unsigned long usecs)
94{
john stultz6f84fa22006-06-26 00:25:11 -070095 __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
98void __ndelay(unsigned long nsecs)
99{
john stultz6f84fa22006-06-26 00:25:11 -0700100 __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700102
103EXPORT_SYMBOL(__delay);
104EXPORT_SYMBOL(__const_udelay);
105EXPORT_SYMBOL(__udelay);
106EXPORT_SYMBOL(__ndelay);