blob: bbc610518516b11c4bdbe1d10dacd29ce41130c6 [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 Morton941e4922008-02-06 01:36:42 -080013#include <linux/timex.h>
Andrew Morton35d5d082007-11-14 17:00:41 -080014#include <linux/preempt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/delay.h>
Andrew Morton941e4922008-02-06 01:36:42 -080016#include <linux/init.h>
Andrew Morton35d5d082007-11-14 17:00:41 -080017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/delay.h>
Venkatesh Pallipadi8a9e1b02005-06-23 00:08:13 -070019#include <asm/msr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#ifdef CONFIG_SMP
22#include <asm/smp.h>
23#endif
24
Andrew Morton941e4922008-02-06 01:36:42 -080025int __devinit read_current_timer(unsigned long *timer_value)
Venkatesh Pallipadi8a9e1b02005-06-23 00:08:13 -070026{
27 rdtscll(*timer_value);
28 return 0;
29}
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031void __delay(unsigned long loops)
32{
33 unsigned bclock, now;
Andrew Morton35d5d082007-11-14 17:00:41 -080034
35 preempt_disable(); /* TSC's are pre-cpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 rdtscl(bclock);
Andrew Morton35d5d082007-11-14 17:00:41 -080037 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 rep_nop();
39 rdtscl(now);
40 }
Andrew Morton35d5d082007-11-14 17:00:41 -080041 while ((now-bclock) < loops);
42 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
Andi Kleen2ee60e172006-06-26 13:59:44 +020044EXPORT_SYMBOL(__delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46inline void __const_udelay(unsigned long xloops)
47{
Mike Travis92cb7612007-10-19 20:35:04 +020048 __delay(((xloops * HZ *
49 cpu_data(raw_smp_processor_id()).loops_per_jiffy) >> 32) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
Andi Kleen2ee60e172006-06-26 13:59:44 +020051EXPORT_SYMBOL(__const_udelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53void __udelay(unsigned long usecs)
54{
Paolo 'Blaisorblade' Giarrussob9a8d942006-12-07 02:14:07 +010055 __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
Andi Kleen2ee60e172006-06-26 13:59:44 +020057EXPORT_SYMBOL(__udelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59void __ndelay(unsigned long nsecs)
60{
61 __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
62}
Andi Kleen2ee60e172006-06-26 13:59:44 +020063EXPORT_SYMBOL(__ndelay);