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> |
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 | { |
Glauber Costa | f0fbf0a | 2008-07-03 12:35:41 -0300 | [diff] [blame] | 31 | asm volatile( |
Jiri Hladky | e01b70e | 2008-06-02 12:00:19 +0200 | [diff] [blame] | 32 | " test %0,%0 \n" |
| 33 | " jz 3f \n" |
| 34 | " jmp 1f \n" |
| 35 | |
| 36 | ".align 16 \n" |
| 37 | "1: jmp 2f \n" |
| 38 | |
| 39 | ".align 16 \n" |
Glauber Costa | ff1b15b | 2008-06-24 09:27:19 -0300 | [diff] [blame] | 40 | "2: dec %0 \n" |
Jiri Hladky | e01b70e | 2008-06-02 12:00:19 +0200 | [diff] [blame] | 41 | " jnz 2b \n" |
Glauber Costa | ff1b15b | 2008-06-24 09:27:19 -0300 | [diff] [blame] | 42 | "3: dec %0 \n" |
Jiri Hladky | e01b70e | 2008-06-02 12:00:19 +0200 | [diff] [blame] | 43 | |
| 44 | : /* we don't need output */ |
| 45 | :"a" (loops) |
| 46 | ); |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /* TSC based delay: */ |
Thomas Gleixner | a7f4255 | 2012-03-09 20:55:10 +0100 | [diff] [blame] | 50 | static void delay_tsc(unsigned long __loops) |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 51 | { |
Thomas Gleixner | a7f4255 | 2012-03-09 20:55:10 +0100 | [diff] [blame] | 52 | u32 bclock, now, loops = __loops; |
Steven Rostedt | 5c1ea08 | 2008-05-25 11:13:32 -0400 | [diff] [blame] | 53 | int cpu; |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 54 | |
Steven Rostedt | 5c1ea08 | 2008-05-25 11:13:32 -0400 | [diff] [blame] | 55 | preempt_disable(); |
| 56 | cpu = smp_processor_id(); |
Pallipadi, Venkatesh | e888d7f | 2009-06-25 16:44:31 -0700 | [diff] [blame] | 57 | rdtsc_barrier(); |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 58 | rdtscl(bclock); |
Steven Rostedt | 5c1ea08 | 2008-05-25 11:13:32 -0400 | [diff] [blame] | 59 | for (;;) { |
Pallipadi, Venkatesh | e888d7f | 2009-06-25 16:44:31 -0700 | [diff] [blame] | 60 | rdtsc_barrier(); |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 61 | rdtscl(now); |
Steven Rostedt | 5c1ea08 | 2008-05-25 11:13:32 -0400 | [diff] [blame] | 62 | if ((now - bclock) >= loops) |
| 63 | break; |
| 64 | |
| 65 | /* Allow RT tasks to run */ |
| 66 | preempt_enable(); |
| 67 | rep_nop(); |
| 68 | preempt_disable(); |
| 69 | |
| 70 | /* |
| 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. |
| 78 | */ |
| 79 | if (unlikely(cpu != smp_processor_id())) { |
| 80 | loops -= (now - bclock); |
| 81 | cpu = smp_processor_id(); |
Pallipadi, Venkatesh | e888d7f | 2009-06-25 16:44:31 -0700 | [diff] [blame] | 82 | rdtsc_barrier(); |
Steven Rostedt | 5c1ea08 | 2008-05-25 11:13:32 -0400 | [diff] [blame] | 83 | rdtscl(bclock); |
| 84 | } |
| 85 | } |
Andrew Morton | 35d5d08 | 2007-11-14 17:00:41 -0800 | [diff] [blame] | 86 | preempt_enable(); |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Since we calibrate only once at boot, this |
| 91 | * function should be set once at boot and not changed |
| 92 | */ |
| 93 | static void (*delay_fn)(unsigned long) = delay_loop; |
| 94 | |
| 95 | void use_tsc_delay(void) |
| 96 | { |
| 97 | delay_fn = delay_tsc; |
| 98 | } |
| 99 | |
Greg Kroah-Hartman | a18e369 | 2012-12-21 14:02:53 -0800 | [diff] [blame] | 100 | int read_current_timer(unsigned long *timer_val) |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 101 | { |
| 102 | if (delay_fn == delay_tsc) { |
Glauber Costa | a76febe | 2008-06-24 09:52:36 -0300 | [diff] [blame] | 103 | rdtscll(*timer_val); |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 104 | return 0; |
| 105 | } |
| 106 | return -1; |
| 107 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | void __delay(unsigned long loops) |
| 110 | { |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 111 | delay_fn(loops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
Glauber Costa | f0fbf0a | 2008-07-03 12:35:41 -0300 | [diff] [blame] | 113 | EXPORT_SYMBOL(__delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
| 115 | inline void __const_udelay(unsigned long xloops) |
| 116 | { |
| 117 | int d0; |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | xloops *= 4; |
Glauber Costa | f0fbf0a | 2008-07-03 12:35:41 -0300 | [diff] [blame] | 120 | asm("mull %%edx" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | :"=d" (xloops), "=&a" (d0) |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 122 | :"1" (xloops), "0" |
Christoph Lameter | 357089f | 2010-12-16 12:14:43 -0600 | [diff] [blame] | 123 | (this_cpu_read(cpu_info.loops_per_jiffy) * (HZ/4))); |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 124 | |
| 125 | __delay(++xloops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
Glauber Costa | f0fbf0a | 2008-07-03 12:35:41 -0300 | [diff] [blame] | 127 | EXPORT_SYMBOL(__const_udelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
| 129 | void __udelay(unsigned long usecs) |
| 130 | { |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 131 | __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
Glauber Costa | f0fbf0a | 2008-07-03 12:35:41 -0300 | [diff] [blame] | 133 | EXPORT_SYMBOL(__udelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
| 135 | void __ndelay(unsigned long nsecs) |
| 136 | { |
john stultz | 6f84fa2 | 2006-06-26 00:25:11 -0700 | [diff] [blame] | 137 | __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
Alexey Dobriyan | 129f694 | 2005-06-23 00:08:33 -0700 | [diff] [blame] | 139 | EXPORT_SYMBOL(__ndelay); |