1 // © 2021 Qualcomm Innovation Center, Inc. All rights reserved.
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #include <hyptypes.h>
6 
7 #include <hypregisters.h>
8 
9 #include <irq.h>
10 
11 #include <asm/barrier.h>
12 
13 #include "idle_arch.h"
14 
15 bool
idle_arch_wait(void)16 idle_arch_wait(void)
17 {
18 	bool must_reschedule = false;
19 
20 	__asm__ volatile("dsb ish; wfi; isb" : "+m"(asm_ordering));
21 
22 	ISR_EL1_t isr = register_ISR_EL1_read_volatile_ordered(&asm_ordering);
23 	if (ISR_EL1_get_I(&isr)) {
24 		must_reschedule = irq_interrupt_dispatch();
25 	}
26 
27 	return must_reschedule;
28 }
29 
30 bool
idle_arch_wait_timeout(ticks_t timeout)31 idle_arch_wait_timeout(ticks_t timeout)
32 {
33 	bool must_reschedule = false;
34 
35 #if defined(ARCH_ARM_FEAT_WFxT) && ARCH_ARM_FEAT_WFxT
36 	// Note: WFIT timeouts are based on CNTVCT_EL0, so this assumes that we
37 	// always set CNTVOFF_EL2 to 0!
38 	__asm__ volatile("dsb ish; wfit %1; isb"
39 			 : "+m"(asm_ordering)
40 			 : "r"(timeout));
41 #else
42 	(void)timeout;
43 	asm_context_sync_ordered(&asm_ordering);
44 #endif
45 
46 	ISR_EL1_t isr = register_ISR_EL1_read_volatile_ordered(&asm_ordering);
47 	if (ISR_EL1_get_I(&isr)) {
48 		must_reschedule = irq_interrupt_dispatch();
49 	}
50 
51 	return must_reschedule;
52 }
53