xref: /linux/arch/arm64/lib/delay.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Delay loops based on the OpenRISC implementation.
4  *
5  * Copyright (C) 2012 ARM Limited
6  *
7  * Author: Will Deacon <will.deacon@arm.com>
8  */
9 
10 #include <linux/delay.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/timex.h>
15 
16 #include <clocksource/arm_arch_timer.h>
17 
18 #define USECS_TO_CYCLES(time_usecs)			\
19 	xloops_to_cycles((time_usecs) * 0x10C7UL)
20 
21 static inline unsigned long xloops_to_cycles(unsigned long xloops)
22 {
23 	return (xloops * loops_per_jiffy * HZ) >> 32;
24 }
25 
26 /*
27  * Force the use of CNTVCT_EL0 in order to have the same base as WFxT.
28  * This avoids some annoying issues when CNTVOFF_EL2 is not reset 0 on a
29  * KVM host running at EL1 until we do a vcpu_put() on the vcpu. When
30  * running at EL2, the effective offset is always 0.
31  *
32  * Note that userspace cannot change the offset behind our back either,
33  * as the vcpu mutex is held as long as KVM_RUN is in progress.
34  */
35 #define __delay_cycles()	__arch_counter_get_cntvct_stable()
36 
37 void __delay(unsigned long cycles)
38 {
39 	cycles_t start = __delay_cycles();
40 
41 	if (alternative_has_cap_unlikely(ARM64_HAS_WFXT)) {
42 		u64 end = start + cycles;
43 
44 		/*
45 		 * Start with WFIT. If an interrupt makes us resume
46 		 * early, use a WFET loop to complete the delay.
47 		 */
48 		wfit(end);
49 		while ((__delay_cycles() - start) < cycles)
50 			wfet(end);
51 	} else 	if (arch_timer_evtstrm_available()) {
52 		const cycles_t timer_evt_period =
53 			USECS_TO_CYCLES(ARCH_TIMER_EVT_STREAM_PERIOD_US);
54 
55 		while ((__delay_cycles() - start + timer_evt_period) < cycles)
56 			wfe();
57 	}
58 
59 	while ((__delay_cycles() - start) < cycles)
60 		cpu_relax();
61 }
62 EXPORT_SYMBOL(__delay);
63 
64 inline void __const_udelay(unsigned long xloops)
65 {
66 	__delay(xloops_to_cycles(xloops));
67 }
68 EXPORT_SYMBOL(__const_udelay);
69 
70 void __udelay(unsigned long usecs)
71 {
72 	__const_udelay(usecs * 0x10C7UL); /* 2**32 / 1000000 (rounded up) */
73 }
74 EXPORT_SYMBOL(__udelay);
75 
76 void __ndelay(unsigned long nsecs)
77 {
78 	__const_udelay(nsecs * 0x5UL); /* 2**32 / 1000000000 (rounded up) */
79 }
80 EXPORT_SYMBOL(__ndelay);
81