Release 4.14 arch/x86/lib/delay.c
// SPDX-License-Identifier: GPL-2.0
/*
* Precise Delay Loops for i386
*
* Copyright (C) 1993 Linus Torvalds
* Copyright (C) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
* Copyright (C) 2008 Jiri Hladky <hladky _dot_ jiri _at_ gmail _dot_ com>
*
* 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.
*/
#include <linux/export.h>
#include <linux/sched.h>
#include <linux/timex.h>
#include <linux/preempt.h>
#include <linux/delay.h>
#include <asm/processor.h>
#include <asm/delay.h>
#include <asm/timer.h>
#include <asm/mwait.h>
#ifdef CONFIG_SMP
# include <asm/smp.h>
#endif
/* simple loop based delay: */
static void delay_loop(unsigned long loops)
{
asm volatile(
" test %0,%0 \n"
" jz 3f \n"
" jmp 1f \n"
".align 16 \n"
"1: jmp 2f \n"
".align 16 \n"
"2: dec %0 \n"
" jnz 2b \n"
"3: dec %0 \n"
: /* we don't need output */
:"a" (loops)
);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
John Stultz | 10 | 66.67% | 1 | 25.00% |
Glauber de Oliveira Costa | 3 | 20.00% | 2 | 50.00% |
Jiri Hladky | 2 | 13.33% | 1 | 25.00% |
Total | 15 | 100.00% | 4 | 100.00% |
/* TSC based delay: */
static void delay_tsc(unsigned long __loops)
{
u64 bclock, now, loops = __loops;
int cpu;
preempt_disable();
cpu = smp_processor_id();
bclock = rdtsc_ordered();
for (;;) {
now = rdtsc_ordered();
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();
bclock = rdtsc_ordered();
}
}
preempt_enable();
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Steven Rostedt | 55 | 53.92% | 1 | 16.67% |
John Stultz | 26 | 25.49% | 1 | 16.67% |
Andrew Lutomirski | 10 | 9.80% | 2 | 33.33% |
Andrew Morton | 6 | 5.88% | 1 | 16.67% |
Thomas Gleixner | 5 | 4.90% | 1 | 16.67% |
Total | 102 | 100.00% | 6 | 100.00% |
/*
* 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;
/*
* Timer value of 0 causes MWAITX to wait indefinitely, unless there
* is a store on the memory monitored by MONITORX.
*/
if (loops == 0)
return;
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.
*/
__monitorx(raw_cpu_ptr(&cpu_tss), 0, 0);
/*
* 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;
}
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Huang Rui | 91 | 91.00% | 1 | 33.33% |
Janakarajan Natarajan | 8 | 8.00% | 1 | 33.33% |
Borislav Petkov | 1 | 1.00% | 1 | 33.33% |
Total | 100 | 100.00% | 3 | 100.00% |
/*
* 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)
{
if (delay_fn == delay_loop)
delay_fn = delay_tsc;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
John Stultz | 11 | 64.71% | 1 | 50.00% |
Huang Rui | 6 | 35.29% | 1 | 50.00% |
Total | 17 | 100.00% | 2 | 100.00% |
void use_mwaitx_delay(void)
{
delay_fn = delay_mwaitx;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Huang Rui | 11 | 100.00% | 1 | 100.00% |
Total | 11 | 100.00% | 1 | 100.00% |
int read_current_timer(unsigned long *timer_val)
{
if (delay_fn == delay_tsc) {
*timer_val = rdtsc();
return 0;
}
return -1;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
John Stultz | 28 | 90.32% | 1 | 33.33% |
Andrew Lutomirski | 3 | 9.68% | 2 | 66.67% |
Total | 31 | 100.00% | 3 | 100.00% |
void __delay(unsigned long loops)
{
delay_fn(loops);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Linus Torvalds (pre-git) | 13 | 92.86% | 1 | 50.00% |
John Stultz | 1 | 7.14% | 1 | 50.00% |
Total | 14 | 100.00% | 2 | 100.00% |
EXPORT_SYMBOL(__delay);
inline void __const_udelay(unsigned long xloops)
{
unsigned long lpj = this_cpu_read(cpu_info.loops_per_jiffy) ? : loops_per_jiffy;
int d0;
xloops *= 4;
asm("mull %%edx"
:"=d" (xloops), "=&a" (d0)
:"1" (xloops), "0" (lpj * (HZ / 4)));
__delay(++xloops);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Linus Torvalds (pre-git) | 18 | 47.37% | 2 | 40.00% |
Jiri Slaby | 15 | 39.47% | 1 | 20.00% |
Dominik Brodowski | 5 | 13.16% | 2 | 40.00% |
Total | 38 | 100.00% | 5 | 100.00% |
EXPORT_SYMBOL(__const_udelay);
void __udelay(unsigned long usecs)
{
__const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Linus Torvalds (pre-git) | 15 | 88.24% | 1 | 50.00% |
Dominik Brodowski | 2 | 11.76% | 1 | 50.00% |
Total | 17 | 100.00% | 2 | 100.00% |
EXPORT_SYMBOL(__udelay);
void __ndelay(unsigned long nsecs)
{
__const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Alan Cox | 17 | 100.00% | 1 | 100.00% |
Total | 17 | 100.00% | 1 | 100.00% |
EXPORT_SYMBOL(__ndelay);
Overall Contributors
Person | Tokens | Prop | Commits | CommitProp |
Huang Rui | 112 | 25.69% | 1 | 3.45% |
John Stultz | 97 | 22.25% | 2 | 6.90% |
Linus Torvalds (pre-git) | 61 | 13.99% | 4 | 13.79% |
Steven Rostedt | 55 | 12.61% | 1 | 3.45% |
Glauber de Oliveira Costa | 18 | 4.13% | 2 | 6.90% |
Alan Cox | 17 | 3.90% | 1 | 3.45% |
Jiri Slaby | 15 | 3.44% | 1 | 3.45% |
Andrew Lutomirski | 13 | 2.98% | 4 | 13.79% |
Andrew Morton | 12 | 2.75% | 2 | 6.90% |
Janakarajan Natarajan | 8 | 1.83% | 1 | 3.45% |
Alexey Dobriyan | 7 | 1.61% | 1 | 3.45% |
Dominik Brodowski | 7 | 1.61% | 3 | 10.34% |
Thomas Gleixner | 5 | 1.15% | 1 | 3.45% |
Jiri Hladky | 3 | 0.69% | 1 | 3.45% |
Linus Torvalds | 3 | 0.69% | 1 | 3.45% |
Greg Kroah-Hartman | 1 | 0.23% | 1 | 3.45% |
Paul Gortmaker | 1 | 0.23% | 1 | 3.45% |
Borislav Petkov | 1 | 0.23% | 1 | 3.45% |
Total | 436 | 100.00% | 29 | 100.00% |
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.