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