Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 13 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/sched.h> |
Andrew Morton | 941e492 | 2008-02-06 01:36:42 -0800 | [diff] [blame] | 15 | #include <linux/timex.h> |
Andrew Morton | 35d5d08 | 2007-11-14 17:00:41 -0800 | [diff] [blame] | 16 | #include <linux/preempt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/delay.h> |
Andrew Morton | 941e492 | 2008-02-06 01:36:42 -0800 | [diff] [blame] | 18 | #include <linux/init.h> |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/processor.h> |
| 21 | #include <asm/delay.h> |
| 22 | #include <asm/timer.h> |
| 23 | |
| 24 | #ifdef CONFIG_SMP |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 25 | # include <asm/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #endif |
| 27 | |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 28 | /* simple loop based delay: */ |
| 29 | static 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: */ |
| 44 | static void delay_tsc(unsigned long loops) |
| 45 | { |
| 46 | unsigned long bclock, now; |
| 47 | |
Andrew Morton | 35d5d08 | 2007-11-14 17:00:41 -0800 | [diff] [blame] | 48 | preempt_disable(); /* TSC's are per-cpu */ |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 49 | rdtscl(bclock); |
| 50 | do { |
| 51 | rep_nop(); |
| 52 | rdtscl(now); |
| 53 | } while ((now-bclock) < loops); |
Andrew Morton | 35d5d08 | 2007-11-14 17:00:41 -0800 | [diff] [blame] | 54 | preempt_enable(); |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Since we calibrate only once at boot, this |
| 59 | * function should be set once at boot and not changed |
| 60 | */ |
| 61 | static void (*delay_fn)(unsigned long) = delay_loop; |
| 62 | |
| 63 | void use_tsc_delay(void) |
| 64 | { |
| 65 | delay_fn = delay_tsc; |
| 66 | } |
| 67 | |
Andrew Morton | 941e492 | 2008-02-06 01:36:42 -0800 | [diff] [blame] | 68 | int __devinit read_current_timer(unsigned long *timer_val) |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 69 | { |
| 70 | if (delay_fn == delay_tsc) { |
| 71 | rdtscl(*timer_val); |
| 72 | return 0; |
| 73 | } |
| 74 | return -1; |
| 75 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
| 77 | void __delay(unsigned long loops) |
| 78 | { |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 79 | delay_fn(loops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | inline void __const_udelay(unsigned long xloops) |
| 83 | { |
| 84 | int d0; |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | xloops *= 4; |
| 87 | __asm__("mull %0" |
| 88 | :"=d" (xloops), "=&a" (d0) |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 89 | :"1" (xloops), "0" |
Mike Travis | 92cb761 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 90 | (cpu_data(raw_smp_processor_id()).loops_per_jiffy * (HZ/4))); |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 91 | |
| 92 | __delay(++xloops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void __udelay(unsigned long usecs) |
| 96 | { |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 97 | __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void __ndelay(unsigned long nsecs) |
| 101 | { |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 102 | __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
Alexey Dobriyan | 129f694 | 2005-06-23 00:08:33 -0700 | [diff] [blame] | 104 | |
| 105 | EXPORT_SYMBOL(__delay); |
| 106 | EXPORT_SYMBOL(__const_udelay); |
| 107 | EXPORT_SYMBOL(__udelay); |
| 108 | EXPORT_SYMBOL(__ndelay); |