x86: Use this_cpu_inc_return for nmi counter
[deliverable/linux.git] / arch / x86 / lib / delay.c
CommitLineData
1da177e4
LT
1/*
2 * Precise Delay Loops for i386
3 *
4 * Copyright (C) 1993 Linus Torvalds
5 * Copyright (C) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
e01b70ef 6 * Copyright (C) 2008 Jiri Hladky <hladky _dot_ jiri _at_ gmail _dot_ com>
1da177e4
LT
7 *
8 * The __delay function must _NOT_ be inlined as its execution time
9 * depends wildly on alignment on many x86 processors. The additional
10 * jump magic is needed to get the timing stable on all the CPU's
11 * we have to worry about.
12 */
13
6f84fa2f 14#include <linux/module.h>
1da177e4 15#include <linux/sched.h>
941e492b 16#include <linux/timex.h>
35d5d08a 17#include <linux/preempt.h>
1da177e4 18#include <linux/delay.h>
941e492b 19#include <linux/init.h>
6f84fa2f 20
1da177e4
LT
21#include <asm/processor.h>
22#include <asm/delay.h>
23#include <asm/timer.h>
24
25#ifdef CONFIG_SMP
6f84fa2f 26# include <asm/smp.h>
1da177e4
LT
27#endif
28
6f84fa2f 29/* simple loop based delay: */
30static void delay_loop(unsigned long loops)
31{
f0fbf0ab 32 asm volatile(
e01b70ef
JH
33 " test %0,%0 \n"
34 " jz 3f \n"
35 " jmp 1f \n"
36
37 ".align 16 \n"
38 "1: jmp 2f \n"
39
40 ".align 16 \n"
ff1b15b6 41 "2: dec %0 \n"
e01b70ef 42 " jnz 2b \n"
ff1b15b6 43 "3: dec %0 \n"
e01b70ef
JH
44
45 : /* we don't need output */
46 :"a" (loops)
47 );
6f84fa2f 48}
49
50/* TSC based delay: */
51static void delay_tsc(unsigned long loops)
52{
53 unsigned long bclock, now;
5c1ea082 54 int cpu;
6f84fa2f 55
5c1ea082
SR
56 preempt_disable();
57 cpu = smp_processor_id();
e888d7fa 58 rdtsc_barrier();
6f84fa2f 59 rdtscl(bclock);
5c1ea082 60 for (;;) {
e888d7fa 61 rdtsc_barrier();
6f84fa2f 62 rdtscl(now);
5c1ea082
SR
63 if ((now - bclock) >= loops)
64 break;
65
66 /* Allow RT tasks to run */
67 preempt_enable();
68 rep_nop();
69 preempt_disable();
70
71 /*
72 * It is possible that we moved to another CPU, and
73 * since TSC's are per-cpu we need to calculate
74 * that. The delay must guarantee that we wait "at
75 * least" the amount of time. Being moved to another
76 * CPU could make the wait longer but we just need to
77 * make sure we waited long enough. Rebalance the
78 * counter for this CPU.
79 */
80 if (unlikely(cpu != smp_processor_id())) {
81 loops -= (now - bclock);
82 cpu = smp_processor_id();
e888d7fa 83 rdtsc_barrier();
5c1ea082
SR
84 rdtscl(bclock);
85 }
86 }
35d5d08a 87 preempt_enable();
6f84fa2f 88}
89
90/*
91 * Since we calibrate only once at boot, this
92 * function should be set once at boot and not changed
93 */
94static void (*delay_fn)(unsigned long) = delay_loop;
95
96void use_tsc_delay(void)
97{
98 delay_fn = delay_tsc;
99}
100
941e492b 101int __devinit read_current_timer(unsigned long *timer_val)
6f84fa2f 102{
103 if (delay_fn == delay_tsc) {
a76febe9 104 rdtscll(*timer_val);
6f84fa2f 105 return 0;
106 }
107 return -1;
108}
1da177e4
LT
109
110void __delay(unsigned long loops)
111{
6f84fa2f 112 delay_fn(loops);
1da177e4 113}
f0fbf0ab 114EXPORT_SYMBOL(__delay);
1da177e4
LT
115
116inline void __const_udelay(unsigned long xloops)
117{
118 int d0;
6f84fa2f 119
1da177e4 120 xloops *= 4;
f0fbf0ab 121 asm("mull %%edx"
1da177e4 122 :"=d" (xloops), "=&a" (d0)
6f84fa2f 123 :"1" (xloops), "0"
92cb7612 124 (cpu_data(raw_smp_processor_id()).loops_per_jiffy * (HZ/4)));
6f84fa2f 125
126 __delay(++xloops);
1da177e4 127}
f0fbf0ab 128EXPORT_SYMBOL(__const_udelay);
1da177e4
LT
129
130void __udelay(unsigned long usecs)
131{
6f84fa2f 132 __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
1da177e4 133}
f0fbf0ab 134EXPORT_SYMBOL(__udelay);
1da177e4
LT
135
136void __ndelay(unsigned long nsecs)
137{
6f84fa2f 138 __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
1da177e4 139}
129f6946 140EXPORT_SYMBOL(__ndelay);
This page took 0.778337 seconds and 5 git commands to generate.