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
	rdtsc_barrier();
59
	rdtscl(bclock);
S
Steven Rostedt 已提交
60
	for (;;) {
61
		rdtsc_barrier();
62
		rdtscl(now);
S
Steven Rostedt 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
		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();
83
			rdtsc_barrier();
S
Steven Rostedt 已提交
84 85 86
			rdtscl(bclock);
		}
	}
87
	preempt_enable();
88 89 90 91 92 93 94 95 96 97 98 99 100
}

/*
 * 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 已提交
101
int __devinit read_current_timer(unsigned long *timer_val)
102 103
{
	if (delay_fn == delay_tsc) {
104
		rdtscll(*timer_val);
105 106 107 108
		return 0;
	}
	return -1;
}
L
Linus Torvalds 已提交
109 110 111

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

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

L
Linus Torvalds 已提交
120
	xloops *= 4;
G
Glauber Costa 已提交
121
	asm("mull %%edx"
L
Linus Torvalds 已提交
122
		:"=d" (xloops), "=&a" (d0)
123
		:"1" (xloops), "0"
124
		(this_cpu_read(cpu_info.loops_per_jiffy) * (HZ/4)));
125 126

	__delay(++xloops);
L
Linus Torvalds 已提交
127
}
G
Glauber Costa 已提交
128
EXPORT_SYMBOL(__const_udelay);
L
Linus Torvalds 已提交
129 130 131

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

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