delay.c 3.9 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/export.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
#include <asm/processor.h>
#include <asm/delay.h>
#include <asm/timer.h>
23
#include <asm/mwait.h>
L
Linus Torvalds 已提交
24 25

#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
}

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

S
Steven Rostedt 已提交
56 57
	preempt_disable();
	cpu = smp_processor_id();
58
	bclock = rdtsc_ordered();
S
Steven Rostedt 已提交
59
	for (;;) {
60
		now = rdtsc_ordered();
S
Steven Rostedt 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
		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();
81
			bclock = rdtsc_ordered();
S
Steven Rostedt 已提交
82 83
		}
	}
84
	preempt_enable();
85 86
}

87 88 89 90 91 92 93 94 95
/*
 * On some AMD platforms, MWAITX has a configurable 32-bit timer, that
 * counts with TSC frequency. The input value is the loop of the
 * counter, it will exit when the timer expires.
 */
static void delay_mwaitx(unsigned long __loops)
{
	u64 start, end, delay, loops = __loops;

96 97 98 99 100 101 102
	/*
	 * Timer value of 0 causes MWAITX to wait indefinitely, unless there
	 * is a store on the memory monitored by MONITORX.
	 */
	if (loops == 0)
		return;

103 104 105 106 107 108 109 110 111
	start = rdtsc_ordered();

	for (;;) {
		delay = min_t(u64, MWAITX_MAX_LOOPS, loops);

		/*
		 * Use cpu_tss as a cacheline-aligned, seldomly
		 * accessed per-cpu variable as the monitor target.
		 */
112
		__monitorx(raw_cpu_ptr(&cpu_tss), 0, 0);
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

		/*
		 * AMD, like Intel, supports the EAX hint and EAX=0xf
		 * means, do not enter any deep C-state and we use it
		 * here in delay() to minimize wakeup latency.
		 */
		__mwaitx(MWAITX_DISABLE_CSTATES, delay, MWAITX_ECX_TIMER_ENABLE);

		end = rdtsc_ordered();

		if (loops <= end - start)
			break;

		loops -= end - start;

		start = end;
	}
}

132 133 134 135 136 137 138 139
/*
 * 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)
{
140 141 142 143 144 145 146
	if (delay_fn == delay_loop)
		delay_fn = delay_tsc;
}

void use_mwaitx_delay(void)
{
	delay_fn = delay_mwaitx;
147 148
}

149
int read_current_timer(unsigned long *timer_val)
150 151
{
	if (delay_fn == delay_tsc) {
152
		*timer_val = rdtsc();
153 154 155 156
		return 0;
	}
	return -1;
}
L
Linus Torvalds 已提交
157 158 159

void __delay(unsigned long loops)
{
160
	delay_fn(loops);
L
Linus Torvalds 已提交
161
}
G
Glauber Costa 已提交
162
EXPORT_SYMBOL(__delay);
L
Linus Torvalds 已提交
163 164 165

inline void __const_udelay(unsigned long xloops)
{
166
	unsigned long lpj = this_cpu_read(cpu_info.loops_per_jiffy) ? : loops_per_jiffy;
L
Linus Torvalds 已提交
167
	int d0;
168

L
Linus Torvalds 已提交
169
	xloops *= 4;
G
Glauber Costa 已提交
170
	asm("mull %%edx"
L
Linus Torvalds 已提交
171
		:"=d" (xloops), "=&a" (d0)
172
		:"1" (xloops), "0" (lpj * (HZ / 4)));
173 174

	__delay(++xloops);
L
Linus Torvalds 已提交
175
}
G
Glauber Costa 已提交
176
EXPORT_SYMBOL(__const_udelay);
L
Linus Torvalds 已提交
177 178 179

void __udelay(unsigned long usecs)
{
180
	__const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
L
Linus Torvalds 已提交
181
}
G
Glauber Costa 已提交
182
EXPORT_SYMBOL(__udelay);
L
Linus Torvalds 已提交
183 184 185

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