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>
19

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

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

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

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

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

		: /* we don't need output */
		:"a" (loops)
	);
47 48 49
}

/* TSC based delay: */
50
static void delay_tsc(unsigned long __loops)
51
{
52
	u32 bclock, now, loops = __loops;
S
Steven Rostedt 已提交
53
	int cpu;
54

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

/*
 * 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;
}

100
int read_current_timer(unsigned long *timer_val)
101 102
{
	if (delay_fn == delay_tsc) {
103
		*timer_val = native_read_tsc();
104 105 106 107
		return 0;
	}
	return -1;
}
L
Linus Torvalds 已提交
108 109 110

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

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

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

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

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

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