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> |
Jiri Hladky | e01b70e | 2008-06-02 12:00:19 +0200 | [diff] [blame] | 6 | * Copyright (C) 2008 Jiri Hladky <hladky _dot_ jiri _at_ gmail _dot_ com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 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. |
| 12 | */ |
| 13 | |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/sched.h> |
Andrew Morton | 941e492 | 2008-02-06 01:36:42 -0800 | [diff] [blame] | 16 | #include <linux/timex.h> |
Andrew Morton | 35d5d08 | 2007-11-14 17:00:41 -0800 | [diff] [blame] | 17 | #include <linux/preempt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/delay.h> |
Andrew Morton | 941e492 | 2008-02-06 01:36:42 -0800 | [diff] [blame] | 19 | #include <linux/init.h> |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/processor.h> |
| 22 | #include <asm/delay.h> |
| 23 | #include <asm/timer.h> |
| 24 | |
| 25 | #ifdef CONFIG_SMP |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 26 | # include <asm/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #endif |
| 28 | |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 29 | /* simple loop based delay: */ |
| 30 | static void delay_loop(unsigned long loops) |
| 31 | { |
Glauber Costa | f0fbf0a | 2008-07-03 12:35:41 -0300 | [diff] [blame] | 32 | asm volatile( |
Jiri Hladky | e01b70e | 2008-06-02 12:00:19 +0200 | [diff] [blame] | 33 | " test %0,%0 \n" |
| 34 | " jz 3f \n" |
| 35 | " jmp 1f \n" |
| 36 | |
| 37 | ".align 16 \n" |
| 38 | "1: jmp 2f \n" |
| 39 | |
| 40 | ".align 16 \n" |
Glauber Costa | ff1b15b | 2008-06-24 09:27:19 -0300 | [diff] [blame] | 41 | "2: dec %0 \n" |
Jiri Hladky | e01b70e | 2008-06-02 12:00:19 +0200 | [diff] [blame] | 42 | " jnz 2b \n" |
Glauber Costa | ff1b15b | 2008-06-24 09:27:19 -0300 | [diff] [blame] | 43 | "3: dec %0 \n" |
Jiri Hladky | e01b70e | 2008-06-02 12:00:19 +0200 | [diff] [blame] | 44 | |
| 45 | : /* we don't need output */ |
| 46 | :"a" (loops) |
| 47 | ); |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /* TSC based delay: */ |
Thomas Gleixner | a7f4255 | 2012-03-09 20:55:10 +0100 | [diff] [blame] | 51 | static void delay_tsc(unsigned long __loops) |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 52 | { |
Thomas Gleixner | a7f4255 | 2012-03-09 20:55:10 +0100 | [diff] [blame] | 53 | u32 bclock, now, loops = __loops; |
Steven Rostedt | 5c1ea08 | 2008-05-25 11:13:32 -0400 | [diff] [blame] | 54 | int cpu; |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 55 | |
Steven Rostedt | 5c1ea08 | 2008-05-25 11:13:32 -0400 | [diff] [blame] | 56 | preempt_disable(); |
| 57 | cpu = smp_processor_id(); |
Pallipadi, Venkatesh | e888d7f | 2009-06-25 16:44:31 -0700 | [diff] [blame] | 58 | rdtsc_barrier(); |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 59 | rdtscl(bclock); |
Steven Rostedt | 5c1ea08 | 2008-05-25 11:13:32 -0400 | [diff] [blame] | 60 | for (;;) { |
Pallipadi, Venkatesh | e888d7f | 2009-06-25 16:44:31 -0700 | [diff] [blame] | 61 | rdtsc_barrier(); |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 62 | rdtscl(now); |
Steven Rostedt | 5c1ea08 | 2008-05-25 11:13:32 -0400 | [diff] [blame] | 63 | if ((now - bclock) >= loops) |
| 64 | break; |
| 65 | |
| 66 | /* Allow RT tasks to run */ |
| 67 | preempt_enable(); |
| 68 | rep_nop(); |
| 69 | preempt_disable(); |
| 70 | |
| 71 | /* |
| 72 | * It is possible that we moved to another CPU, and |
| 73 | * since TSC's are per-cpu we need to calculate |
| 74 | * that. The delay must guarantee that we wait "at |
| 75 | * least" the amount of time. Being moved to another |
| 76 | * CPU could make the wait longer but we just need to |
| 77 | * make sure we waited long enough. Rebalance the |
| 78 | * counter for this CPU. |
| 79 | */ |
| 80 | if (unlikely(cpu != smp_processor_id())) { |
| 81 | loops -= (now - bclock); |
| 82 | cpu = smp_processor_id(); |
Pallipadi, Venkatesh | e888d7f | 2009-06-25 16:44:31 -0700 | [diff] [blame] | 83 | rdtsc_barrier(); |
Steven Rostedt | 5c1ea08 | 2008-05-25 11:13:32 -0400 | [diff] [blame] | 84 | rdtscl(bclock); |
| 85 | } |
| 86 | } |
Andrew Morton | 35d5d08 | 2007-11-14 17:00:41 -0800 | [diff] [blame] | 87 | preempt_enable(); |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Since we calibrate only once at boot, this |
| 92 | * function should be set once at boot and not changed |
| 93 | */ |
| 94 | static void (*delay_fn)(unsigned long) = delay_loop; |
| 95 | |
| 96 | void use_tsc_delay(void) |
| 97 | { |
| 98 | delay_fn = delay_tsc; |
| 99 | } |
| 100 | |
Andrew Morton | 941e492 | 2008-02-06 01:36:42 -0800 | [diff] [blame] | 101 | int __devinit read_current_timer(unsigned long *timer_val) |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 102 | { |
| 103 | if (delay_fn == delay_tsc) { |
Glauber Costa | a76febe | 2008-06-24 09:52:36 -0300 | [diff] [blame] | 104 | rdtscll(*timer_val); |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | return -1; |
| 108 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | void __delay(unsigned long loops) |
| 111 | { |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 112 | delay_fn(loops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
Glauber Costa | f0fbf0a | 2008-07-03 12:35:41 -0300 | [diff] [blame] | 114 | EXPORT_SYMBOL(__delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | inline void __const_udelay(unsigned long xloops) |
| 117 | { |
| 118 | int d0; |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | xloops *= 4; |
Glauber Costa | f0fbf0a | 2008-07-03 12:35:41 -0300 | [diff] [blame] | 121 | asm("mull %%edx" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | :"=d" (xloops), "=&a" (d0) |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 123 | :"1" (xloops), "0" |
Christoph Lameter | 357089f | 2010-12-16 12:14:43 -0600 | [diff] [blame] | 124 | (this_cpu_read(cpu_info.loops_per_jiffy) * (HZ/4))); |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 125 | |
| 126 | __delay(++xloops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
Glauber Costa | f0fbf0a | 2008-07-03 12:35:41 -0300 | [diff] [blame] | 128 | EXPORT_SYMBOL(__const_udelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | void __udelay(unsigned long usecs) |
| 131 | { |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 132 | __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
Glauber Costa | f0fbf0a | 2008-07-03 12:35:41 -0300 | [diff] [blame] | 134 | EXPORT_SYMBOL(__udelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
| 136 | void __ndelay(unsigned long nsecs) |
| 137 | { |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 138 | __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
Alexey Dobriyan | 129f694 | 2005-06-23 00:08:33 -0700 | [diff] [blame] | 140 | EXPORT_SYMBOL(__ndelay); |