1 /* SPDX-License-Identifier: GPL-2.0 */
3 * ARM Generic Timer specific interface
6 #ifndef SELFTEST_KVM_ARCH_TIMER_H
7 #define SELFTEST_KVM_ARCH_TIMER_H
16 #define CTL_ENABLE (1 << 0)
17 #define CTL_IMASK (1 << 1)
18 #define CTL_ISTATUS (1 << 2)
20 #define msec_to_cycles(msec) \
21 (timer_get_cntfrq() * (uint64_t)(msec) / 1000)
23 #define usec_to_cycles(usec) \
24 (timer_get_cntfrq() * (uint64_t)(usec) / 1000000)
26 #define cycles_to_usec(cycles) \
27 ((uint64_t)(cycles) * 1000000 / timer_get_cntfrq())
29 static inline uint32_t timer_get_cntfrq(void)
31 return read_sysreg(cntfrq_el0);
34 static inline uint64_t timer_get_cntct(enum arch_timer timer)
40 return read_sysreg(cntvct_el0);
42 return read_sysreg(cntpct_el0);
44 GUEST_ASSERT_1(0, timer);
47 /* We should not reach here */
51 static inline void timer_set_cval(enum arch_timer timer, uint64_t cval)
55 write_sysreg(cval, cntv_cval_el0);
58 write_sysreg(cval, cntp_cval_el0);
61 GUEST_ASSERT_1(0, timer);
67 static inline uint64_t timer_get_cval(enum arch_timer timer)
71 return read_sysreg(cntv_cval_el0);
73 return read_sysreg(cntp_cval_el0);
75 GUEST_ASSERT_1(0, timer);
78 /* We should not reach here */
82 static inline void timer_set_tval(enum arch_timer timer, uint32_t tval)
86 write_sysreg(tval, cntv_tval_el0);
89 write_sysreg(tval, cntp_tval_el0);
92 GUEST_ASSERT_1(0, timer);
98 static inline void timer_set_ctl(enum arch_timer timer, uint32_t ctl)
102 write_sysreg(ctl, cntv_ctl_el0);
105 write_sysreg(ctl, cntp_ctl_el0);
108 GUEST_ASSERT_1(0, timer);
114 static inline uint32_t timer_get_ctl(enum arch_timer timer)
118 return read_sysreg(cntv_ctl_el0);
120 return read_sysreg(cntp_ctl_el0);
122 GUEST_ASSERT_1(0, timer);
125 /* We should not reach here */
129 static inline void timer_set_next_cval_ms(enum arch_timer timer, uint32_t msec)
131 uint64_t now_ct = timer_get_cntct(timer);
132 uint64_t next_ct = now_ct + msec_to_cycles(msec);
134 timer_set_cval(timer, next_ct);
137 static inline void timer_set_next_tval_ms(enum arch_timer timer, uint32_t msec)
139 timer_set_tval(timer, msec_to_cycles(msec));
142 #endif /* SELFTEST_KVM_ARCH_TIMER_H */