x86: enable preemption in delay
[deliverable/linux.git] / arch / x86 / lib / delay_64.c
CommitLineData
1da177e4
LT
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
2ee60e17 11#include <linux/module.h>
1da177e4 12#include <linux/sched.h>
941e492b 13#include <linux/timex.h>
35d5d08a 14#include <linux/preempt.h>
1da177e4 15#include <linux/delay.h>
941e492b 16#include <linux/init.h>
35d5d08a 17
1da177e4 18#include <asm/delay.h>
8a9e1b0f 19#include <asm/msr.h>
1da177e4
LT
20
21#ifdef CONFIG_SMP
22#include <asm/smp.h>
23#endif
24
941e492b 25int __devinit read_current_timer(unsigned long *timer_value)
8a9e1b0f
VP
26{
27 rdtscll(*timer_value);
28 return 0;
29}
30
1da177e4
LT
31void __delay(unsigned long loops)
32{
33 unsigned bclock, now;
5c1ea082 34 int cpu;
35d5d08a 35
5c1ea082
SR
36 preempt_disable();
37 cpu = smp_processor_id();
1da177e4 38 rdtscl(bclock);
5c1ea082 39 for (;;) {
1da177e4 40 rdtscl(now);
5c1ea082
SR
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 }
1da177e4 63 }
35d5d08a 64 preempt_enable();
1da177e4 65}
2ee60e17 66EXPORT_SYMBOL(__delay);
1da177e4
LT
67
68inline void __const_udelay(unsigned long xloops)
69{
92cb7612
MT
70 __delay(((xloops * HZ *
71 cpu_data(raw_smp_processor_id()).loops_per_jiffy) >> 32) + 1);
1da177e4 72}
2ee60e17 73EXPORT_SYMBOL(__const_udelay);
1da177e4
LT
74
75void __udelay(unsigned long usecs)
76{
b9a8d94a 77 __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
1da177e4 78}
2ee60e17 79EXPORT_SYMBOL(__udelay);
1da177e4
LT
80
81void __ndelay(unsigned long nsecs)
82{
83 __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
84}
2ee60e17 85EXPORT_SYMBOL(__ndelay);
This page took 0.323199 seconds and 5 git commands to generate.