blob: 4c441be92641a3ae295c8dea3db10b86323e7de2 [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;
Steven Rostedt5c1ea082008-05-25 11:13:32 -040034 int cpu;
Andrew Morton35d5d082007-11-14 17:00:41 -080035
Steven Rostedt5c1ea082008-05-25 11:13:32 -040036 preempt_disable();
37 cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 rdtscl(bclock);
Steven Rostedt5c1ea082008-05-25 11:13:32 -040039 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 rdtscl(now);
Steven Rostedt5c1ea082008-05-25 11:13:32 -040041 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 Torvalds1da177e2005-04-16 15:20:36 -070063 }
Andrew Morton35d5d082007-11-14 17:00:41 -080064 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
Andi Kleen2ee60e172006-06-26 13:59:44 +020066EXPORT_SYMBOL(__delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68inline void __const_udelay(unsigned long xloops)
69{
Mike Travis92cb7612007-10-19 20:35:04 +020070 __delay(((xloops * HZ *
71 cpu_data(raw_smp_processor_id()).loops_per_jiffy) >> 32) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
Andi Kleen2ee60e172006-06-26 13:59:44 +020073EXPORT_SYMBOL(__const_udelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75void __udelay(unsigned long usecs)
76{
Paolo 'Blaisorblade' Giarrussob9a8d942006-12-07 02:14:07 +010077 __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
Andi Kleen2ee60e172006-06-26 13:59:44 +020079EXPORT_SYMBOL(__udelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81void __ndelay(unsigned long nsecs)
82{
83 __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
84}
Andi Kleen2ee60e172006-06-26 13:59:44 +020085EXPORT_SYMBOL(__ndelay);