delay.c 2.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*
 *	Precise Delay Loops for i386
 *
 *	Copyright (C) 1993 Linus Torvalds
 *	Copyright (C) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
6
 *	Copyright (C) 2008 Jiri Hladky <hladky _dot_ jiri _at_ gmail _dot_ com>
L
Linus Torvalds 已提交
7 8 9 10 11 12 13
 *
 *	The __delay function must _NOT_ be inlined as its execution time
 *	depends wildly on alignment on many x86 processors. The additional
 *	jump magic is needed to get the timing stable on all the CPU's
 *	we have to worry about.
 */

14
#include <linux/module.h>
L
Linus Torvalds 已提交
15
#include <linux/sched.h>
A
Andrew Morton 已提交
16
#include <linux/timex.h>
17
#include <linux/preempt.h>
L
Linus Torvalds 已提交
18
#include <linux/delay.h>
A
Andrew Morton 已提交
19
#include <linux/init.h>
20

L
Linus Torvalds 已提交
21 22 23 24 25
#include <asm/processor.h>
#include <asm/delay.h>
#include <asm/timer.h>

#ifdef CONFIG_SMP
26
# include <asm/smp.h>
L
Linus Torvalds 已提交
27 28
#endif

29 30 31
/* simple loop based delay: */
static void delay_loop(unsigned long loops)
{
G
Glauber Costa 已提交
32
	asm volatile(
33 34 35 36 37 38 39 40
		"	test %0,%0	\n"
		"	jz 3f		\n"
		"	jmp 1f		\n"

		".align 16		\n"
		"1:	jmp 2f		\n"

		".align 16		\n"
G
Glauber Costa 已提交
41
		"2:	dec %0		\n"
42
		"	jnz 2b		\n"
G
Glauber Costa 已提交
43
		"3:	dec %0		\n"
44 45 46 47

		: /* we don't need output */
		:"a" (loops)
	);
48 49 50 51 52 53
}

/* TSC based delay: */
static void delay_tsc(unsigned long loops)
{
	unsigned long bclock, now;
S
Steven Rostedt 已提交
54
	int cpu;
55

S
Steven Rostedt 已提交
56 57
	preempt_disable();
	cpu = smp_processor_id();
58
	rdtscl(bclock);
S
Steven Rostedt 已提交
59
	for (;;) {
60
		rdtscl(now);
S
Steven Rostedt 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
		if ((now - bclock) >= loops)
			break;

		/* Allow RT tasks to run */
		preempt_enable();
		rep_nop();
		preempt_disable();

		/*
		 * It is possible that we moved to another CPU, and
		 * since TSC's are per-cpu we need to calculate
		 * that. The delay must guarantee that we wait "at
		 * least" the amount of time. Being moved to another
		 * CPU could make the wait longer but we just need to
		 * make sure we waited long enough. Rebalance the
		 * counter for this CPU.
		 */
		if (unlikely(cpu != smp_processor_id())) {
			loops -= (now - bclock);
			cpu = smp_processor_id();
			rdtscl(bclock);
		}
	}
84
	preempt_enable();
85 86 87 88 89 90 91 92 93 94 95 96 97
}

/*
 * Since we calibrate only once at boot, this
 * function should be set once at boot and not changed
 */
static void (*delay_fn)(unsigned long) = delay_loop;

void use_tsc_delay(void)
{
	delay_fn = delay_tsc;
}

A
Andrew Morton 已提交
98
int __devinit read_current_timer(unsigned long *timer_val)
99 100
{
	if (delay_fn == delay_tsc) {
101
		rdtscll(*timer_val);
102 103 104 105
		return 0;
	}
	return -1;
}
L
Linus Torvalds 已提交
106 107 108

void __delay(unsigned long loops)
{
109
	delay_fn(loops);
L
Linus Torvalds 已提交
110
}
G
Glauber Costa 已提交
111
EXPORT_SYMBOL(__delay);
L
Linus Torvalds 已提交
112 113 114 115

inline void __const_udelay(unsigned long xloops)
{
	int d0;
116

L
Linus Torvalds 已提交
117
	xloops *= 4;
G
Glauber Costa 已提交
118
	asm("mull %%edx"
L
Linus Torvalds 已提交
119
		:"=d" (xloops), "=&a" (d0)
120
		:"1" (xloops), "0"
121
		(cpu_data(raw_smp_processor_id()).loops_per_jiffy * (HZ/4)));
122 123

	__delay(++xloops);
L
Linus Torvalds 已提交
124
}
G
Glauber Costa 已提交
125
EXPORT_SYMBOL(__const_udelay);
L
Linus Torvalds 已提交
126 127 128

void __udelay(unsigned long usecs)
{
129
	__const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
L
Linus Torvalds 已提交
130
}
G
Glauber Costa 已提交
131
EXPORT_SYMBOL(__udelay);
L
Linus Torvalds 已提交
132 133 134

void __ndelay(unsigned long nsecs)
{
135
	__const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
L
Linus Torvalds 已提交
136
}
137
EXPORT_SYMBOL(__ndelay);