Skip to content

Commit 7b77452

Browse files
Julien Thierrywildea01
authored andcommitted
arm64: use WFE for long delays
The current delay implementation uses the yield instruction, which is a hint that it is beneficial to schedule another thread. As this is a hint, it may be implemented as a NOP, causing all delays to be busy loops. This is the case for many existing CPUs. Taking advantage of the generic timer sending periodic events to all cores, we can use WFE during delays to reduce power consumption. This is beneficial only for delays longer than the period of the timer event stream. If timer event stream is not enabled, delays will behave as yield/busy loops. Signed-off-by: Julien Thierry <julien.thierry@arm.com> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Will Deacon <will.deacon@arm.com> Cc: Mark Rutland <mark.rutland@arm.com> Signed-off-by: Will Deacon <will.deacon@arm.com>
1 parent ec5c8e4 commit 7b77452

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

‎arch/arm64/lib/delay.c‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,36 @@
2424
#include <linux/module.h>
2525
#include <linux/timex.h>
2626

27+
#include <clocksource/arm_arch_timer.h>
28+
29+
#define USECS_TO_CYCLES(time_usecs) \
30+
xloops_to_cycles((time_usecs) * 0x10C7UL)
31+
32+
static inline unsigned long xloops_to_cycles(unsigned long xloops)
33+
{
34+
return (xloops * loops_per_jiffy * HZ) >> 32;
35+
}
36+
2737
void __delay(unsigned long cycles)
2838
{
2939
cycles_t start = get_cycles();
3040

41+
if (arch_timer_evtstrm_available()) {
42+
const cycles_t timer_evt_period =
43+
USECS_TO_CYCLES(ARCH_TIMER_EVT_STREAM_PERIOD_US);
44+
45+
while ((get_cycles() - start + timer_evt_period) < cycles)
46+
wfe();
47+
}
48+
3149
while ((get_cycles() - start) < cycles)
3250
cpu_relax();
3351
}
3452
EXPORT_SYMBOL(__delay);
3553

3654
inline void __const_udelay(unsigned long xloops)
3755
{
38-
unsigned long loops;
39-
40-
loops = xloops * loops_per_jiffy * HZ;
41-
__delay(loops >> 32);
56+
__delay(xloops_to_cycles(xloops));
4257
}
4358
EXPORT_SYMBOL(__const_udelay);
4459

‎include/clocksource/arm_arch_timer.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ enum arch_timer_spi_nr {
6767
#define ARCH_TIMER_USR_VT_ACCESS_EN (1 << 8) /* virtual timer registers */
6868
#define ARCH_TIMER_USR_PT_ACCESS_EN (1 << 9) /* physical timer registers */
6969

70-
#define ARCH_TIMER_EVT_STREAM_FREQ 10000 /* 100us */
70+
#define ARCH_TIMER_EVT_STREAM_PERIOD_US 100
71+
#define ARCH_TIMER_EVT_STREAM_FREQ \
72+
(USEC_PER_SEC / ARCH_TIMER_EVT_STREAM_PERIOD_US)
7173

7274
struct arch_timer_kvm_info {
7375
struct timecounter timecounter;

0 commit comments

Comments
 (0)