Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Precise Delay Loops for x86-64 |
| 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. |
| 9 | */ |
| 10 | |
Andi Kleen | 2ee60e17 | 2006-06-26 13:59:44 +0200 | [diff] [blame] | 11 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/sched.h> |
Andrew Morton | 941e492 | 2008-02-06 01:36:42 -0800 | [diff] [blame] | 13 | #include <linux/timex.h> |
Andrew Morton | 35d5d08 | 2007-11-14 17:00:41 -0800 | [diff] [blame] | 14 | #include <linux/preempt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/delay.h> |
Andrew Morton | 941e492 | 2008-02-06 01:36:42 -0800 | [diff] [blame] | 16 | #include <linux/init.h> |
Andrew Morton | 35d5d08 | 2007-11-14 17:00:41 -0800 | [diff] [blame] | 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <asm/delay.h> |
Venkatesh Pallipadi | 8a9e1b0 | 2005-06-23 00:08:13 -0700 | [diff] [blame] | 19 | #include <asm/msr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | #ifdef CONFIG_SMP |
| 22 | #include <asm/smp.h> |
| 23 | #endif |
| 24 | |
Andrew Morton | 941e492 | 2008-02-06 01:36:42 -0800 | [diff] [blame] | 25 | int __devinit read_current_timer(unsigned long *timer_value) |
Venkatesh Pallipadi | 8a9e1b0 | 2005-06-23 00:08:13 -0700 | [diff] [blame] | 26 | { |
| 27 | rdtscll(*timer_value); |
| 28 | return 0; |
| 29 | } |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | void __delay(unsigned long loops) |
| 32 | { |
| 33 | unsigned bclock, now; |
Steven Rostedt | 5c1ea08 | 2008-05-25 11:13:32 -0400 | [diff] [blame] | 34 | int cpu; |
Andrew Morton | 35d5d08 | 2007-11-14 17:00:41 -0800 | [diff] [blame] | 35 | |
Steven Rostedt | 5c1ea08 | 2008-05-25 11:13:32 -0400 | [diff] [blame] | 36 | preempt_disable(); |
| 37 | cpu = smp_processor_id(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | rdtscl(bclock); |
Steven Rostedt | 5c1ea08 | 2008-05-25 11:13:32 -0400 | [diff] [blame] | 39 | for (;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | rdtscl(now); |
Steven Rostedt | 5c1ea08 | 2008-05-25 11:13:32 -0400 | [diff] [blame] | 41 | if ((now - bclock) >= loops) |
| 42 | break; |
| 43 | |
| 44 | /* Allow RT tasks to run */ |
| 45 | preempt_enable(); |
| 46 | rep_nop(); |
| 47 | preempt_disable(); |
| 48 | |
| 49 | /* |
| 50 | * It is possible that we moved to another CPU, and |
| 51 | * since TSC's are per-cpu we need to calculate |
| 52 | * that. The delay must guarantee that we wait "at |
| 53 | * least" the amount of time. Being moved to another |
| 54 | * CPU could make the wait longer but we just need to |
| 55 | * make sure we waited long enough. Rebalance the |
| 56 | * counter for this CPU. |
| 57 | */ |
| 58 | if (unlikely(cpu != smp_processor_id())) { |
| 59 | loops -= (now - bclock); |
| 60 | cpu = smp_processor_id(); |
| 61 | rdtscl(bclock); |
| 62 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } |
Andrew Morton | 35d5d08 | 2007-11-14 17:00:41 -0800 | [diff] [blame] | 64 | preempt_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
Andi Kleen | 2ee60e17 | 2006-06-26 13:59:44 +0200 | [diff] [blame] | 66 | EXPORT_SYMBOL(__delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | inline void __const_udelay(unsigned long xloops) |
| 69 | { |
Mike Travis | 92cb761 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 70 | __delay(((xloops * HZ * |
| 71 | cpu_data(raw_smp_processor_id()).loops_per_jiffy) >> 32) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
Andi Kleen | 2ee60e17 | 2006-06-26 13:59:44 +0200 | [diff] [blame] | 73 | EXPORT_SYMBOL(__const_udelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | void __udelay(unsigned long usecs) |
| 76 | { |
Paolo 'Blaisorblade' Giarrusso | b9a8d94 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 77 | __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
Andi Kleen | 2ee60e17 | 2006-06-26 13:59:44 +0200 | [diff] [blame] | 79 | EXPORT_SYMBOL(__udelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | void __ndelay(unsigned long nsecs) |
| 82 | { |
| 83 | __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */ |
| 84 | } |
Andi Kleen | 2ee60e17 | 2006-06-26 13:59:44 +0200 | [diff] [blame] | 85 | EXPORT_SYMBOL(__ndelay); |