delay.c 2.82 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
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>
Linus Torvalds's avatar
Linus Torvalds committed
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>
Linus Torvalds's avatar
Linus Torvalds committed
15
#include <linux/sched.h>
Andrew Morton's avatar
Andrew Morton committed
16
#include <linux/timex.h>
17
#include <linux/preempt.h>
Linus Torvalds's avatar
Linus Torvalds committed
18
#include <linux/delay.h>
Andrew Morton's avatar
Andrew Morton committed
19
#include <linux/init.h>
20

Linus Torvalds's avatar
Linus Torvalds committed
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>
Linus Torvalds's avatar
Linus Torvalds committed
27 28
#endif

29 30 31
/* simple loop based delay: */
static void delay_loop(unsigned long loops)
{
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"
41
		"2:	dec %0		\n"
42
		"	jnz 2b		\n"
43
		"3:	dec %0		\n"
44 45 46 47

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

/* TSC based delay: */
51
static void delay_tsc(unsigned long __loops)
52
{
53
	u32 bclock, now, loops = __loops;
54
	int cpu;
55

56 57
	preempt_disable();
	cpu = smp_processor_id();
58
	rdtsc_barrier();
59
	rdtscl(bclock);
60
	for (;;) {
61
		rdtsc_barrier();
62
		rdtscl(now);
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();
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;
}

101
int 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;
}
Linus Torvalds's avatar
Linus Torvalds committed
109 110 111

void __delay(unsigned long loops)
{
112
	delay_fn(loops);
Linus Torvalds's avatar
Linus Torvalds committed
113
}
114
EXPORT_SYMBOL(__delay);
Linus Torvalds's avatar
Linus Torvalds committed
115 116 117 118

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

Linus Torvalds's avatar
Linus Torvalds committed
120
	xloops *= 4;
121
	asm("mull %%edx"
Linus Torvalds's avatar
Linus Torvalds committed
122
		:"=d" (xloops), "=&a" (d0)
123
		:"1" (xloops), "0"
124
		(this_cpu_read(cpu_info.loops_per_jiffy) * (HZ/4)));
125 126

	__delay(++xloops);
Linus Torvalds's avatar
Linus Torvalds committed
127
}
128
EXPORT_SYMBOL(__const_udelay);
Linus Torvalds's avatar
Linus Torvalds committed
129 130 131

void __udelay(unsigned long usecs)
{
132
	__const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
Linus Torvalds's avatar
Linus Torvalds committed
133
}
134
EXPORT_SYMBOL(__udelay);
Linus Torvalds's avatar
Linus Torvalds committed
135 136 137

void __ndelay(unsigned long nsecs)
{
138
	__const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
Linus Torvalds's avatar
Linus Torvalds committed
139
}
140
EXPORT_SYMBOL(__ndelay);