blob: 45cdd3fbd91c514f38e2c2782f7dbc0dcecd8e90 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Kleen2ee60e172006-06-26 13:59:44 +020011#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/sched.h>
Andrew Morton35d5d082007-11-14 17:00:41 -080013#include <linux/preempt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/delay.h>
Andrew Morton35d5d082007-11-14 17:00:41 -080015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/delay.h>
Venkatesh Pallipadi8a9e1b02005-06-23 00:08:13 -070017#include <asm/msr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#ifdef CONFIG_SMP
20#include <asm/smp.h>
21#endif
22
Venkatesh Pallipadi8a9e1b02005-06-23 00:08:13 -070023int read_current_timer(unsigned long *timer_value)
24{
25 rdtscll(*timer_value);
26 return 0;
27}
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029void __delay(unsigned long loops)
30{
31 unsigned bclock, now;
Andrew Morton35d5d082007-11-14 17:00:41 -080032
33 preempt_disable(); /* TSC's are pre-cpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 rdtscl(bclock);
Andrew Morton35d5d082007-11-14 17:00:41 -080035 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 rep_nop();
37 rdtscl(now);
38 }
Andrew Morton35d5d082007-11-14 17:00:41 -080039 while ((now-bclock) < loops);
40 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
Andi Kleen2ee60e172006-06-26 13:59:44 +020042EXPORT_SYMBOL(__delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44inline void __const_udelay(unsigned long xloops)
45{
Mike Travis92cb7612007-10-19 20:35:04 +020046 __delay(((xloops * HZ *
47 cpu_data(raw_smp_processor_id()).loops_per_jiffy) >> 32) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
Andi Kleen2ee60e172006-06-26 13:59:44 +020049EXPORT_SYMBOL(__const_udelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51void __udelay(unsigned long usecs)
52{
Paolo 'Blaisorblade' Giarrussob9a8d942006-12-07 02:14:07 +010053 __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
Andi Kleen2ee60e172006-06-26 13:59:44 +020055EXPORT_SYMBOL(__udelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57void __ndelay(unsigned long nsecs)
58{
59 __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
60}
Andi Kleen2ee60e172006-06-26 13:59:44 +020061EXPORT_SYMBOL(__ndelay);