| 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 | 35d5d08 | 2007-11-14 17:00:41 -0800 | [diff] [blame] | 13 | #include <linux/preempt.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/delay.h> | 
| Andrew Morton | 35d5d08 | 2007-11-14 17:00:41 -0800 | [diff] [blame] | 15 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/delay.h> | 
| Venkatesh Pallipadi | 8a9e1b0 | 2005-06-23 00:08:13 -0700 | [diff] [blame] | 17 | #include <asm/msr.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 |  | 
 | 19 | #ifdef CONFIG_SMP | 
 | 20 | #include <asm/smp.h> | 
 | 21 | #endif | 
 | 22 |  | 
| Venkatesh Pallipadi | 8a9e1b0 | 2005-06-23 00:08:13 -0700 | [diff] [blame] | 23 | int read_current_timer(unsigned long *timer_value) | 
 | 24 | { | 
 | 25 | 	rdtscll(*timer_value); | 
 | 26 | 	return 0; | 
 | 27 | } | 
 | 28 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | void __delay(unsigned long loops) | 
 | 30 | { | 
 | 31 | 	unsigned bclock, now; | 
| Andrew Morton | 35d5d08 | 2007-11-14 17:00:41 -0800 | [diff] [blame] | 32 |  | 
 | 33 | 	preempt_disable();		/* TSC's are pre-cpu */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | 	rdtscl(bclock); | 
| Andrew Morton | 35d5d08 | 2007-11-14 17:00:41 -0800 | [diff] [blame] | 35 | 	do { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | 		rep_nop();  | 
 | 37 | 		rdtscl(now); | 
 | 38 | 	} | 
| Andrew Morton | 35d5d08 | 2007-11-14 17:00:41 -0800 | [diff] [blame] | 39 | 	while ((now-bclock) < loops); | 
 | 40 | 	preempt_enable(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | } | 
| Andi Kleen | 2ee60e17 | 2006-06-26 13:59:44 +0200 | [diff] [blame] | 42 | EXPORT_SYMBOL(__delay); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 |  | 
 | 44 | inline void __const_udelay(unsigned long xloops) | 
 | 45 | { | 
| Mike Travis | 92cb761 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 46 | 	__delay(((xloops * HZ * | 
 | 47 | 		cpu_data(raw_smp_processor_id()).loops_per_jiffy) >> 32) + 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | } | 
| Andi Kleen | 2ee60e17 | 2006-06-26 13:59:44 +0200 | [diff] [blame] | 49 | EXPORT_SYMBOL(__const_udelay); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 |  | 
 | 51 | void __udelay(unsigned long usecs) | 
 | 52 | { | 
| Paolo 'Blaisorblade' Giarrusso | b9a8d94 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 53 | 	__const_udelay(usecs * 0x000010c7);  /* 2**32 / 1000000 (rounded up) */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } | 
| Andi Kleen | 2ee60e17 | 2006-06-26 13:59:44 +0200 | [diff] [blame] | 55 | EXPORT_SYMBOL(__udelay); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 |  | 
 | 57 | void __ndelay(unsigned long nsecs) | 
 | 58 | { | 
 | 59 | 	__const_udelay(nsecs * 0x00005);  /* 2**32 / 1000000000 (rounded up) */ | 
 | 60 | } | 
| Andi Kleen | 2ee60e17 | 2006-06-26 13:59:44 +0200 | [diff] [blame] | 61 | EXPORT_SYMBOL(__ndelay); |