Path: blob/master/tools/testing/selftests/kvm/include/arm64/arch_timer.h
49000 views
/* SPDX-License-Identifier: GPL-2.0 */1/*2* ARM Generic Timer specific interface3*/45#ifndef SELFTEST_KVM_ARCH_TIMER_H6#define SELFTEST_KVM_ARCH_TIMER_H78#include "processor.h"910enum arch_timer {11VIRTUAL,12PHYSICAL,13};1415#define CTL_ENABLE (1 << 0)16#define CTL_IMASK (1 << 1)17#define CTL_ISTATUS (1 << 2)1819#define msec_to_cycles(msec) \20(timer_get_cntfrq() * (uint64_t)(msec) / 1000)2122#define usec_to_cycles(usec) \23(timer_get_cntfrq() * (uint64_t)(usec) / 1000000)2425#define cycles_to_usec(cycles) \26((uint64_t)(cycles) * 1000000 / timer_get_cntfrq())2728static inline uint32_t timer_get_cntfrq(void)29{30return read_sysreg(cntfrq_el0);31}3233static inline uint64_t timer_get_cntct(enum arch_timer timer)34{35isb();3637switch (timer) {38case VIRTUAL:39return read_sysreg(cntvct_el0);40case PHYSICAL:41return read_sysreg(cntpct_el0);42default:43GUEST_FAIL("Unexpected timer type = %u", timer);44}4546/* We should not reach here */47return 0;48}4950static inline void timer_set_cval(enum arch_timer timer, uint64_t cval)51{52switch (timer) {53case VIRTUAL:54write_sysreg(cval, cntv_cval_el0);55break;56case PHYSICAL:57write_sysreg(cval, cntp_cval_el0);58break;59default:60GUEST_FAIL("Unexpected timer type = %u", timer);61}6263isb();64}6566static inline uint64_t timer_get_cval(enum arch_timer timer)67{68switch (timer) {69case VIRTUAL:70return read_sysreg(cntv_cval_el0);71case PHYSICAL:72return read_sysreg(cntp_cval_el0);73default:74GUEST_FAIL("Unexpected timer type = %u", timer);75}7677/* We should not reach here */78return 0;79}8081static inline void timer_set_tval(enum arch_timer timer, int32_t tval)82{83switch (timer) {84case VIRTUAL:85write_sysreg(tval, cntv_tval_el0);86break;87case PHYSICAL:88write_sysreg(tval, cntp_tval_el0);89break;90default:91GUEST_FAIL("Unexpected timer type = %u", timer);92}9394isb();95}9697static inline int32_t timer_get_tval(enum arch_timer timer)98{99isb();100switch (timer) {101case VIRTUAL:102return read_sysreg(cntv_tval_el0);103case PHYSICAL:104return read_sysreg(cntp_tval_el0);105default:106GUEST_FAIL("Could not get timer %d\n", timer);107}108109/* We should not reach here */110return 0;111}112113static inline void timer_set_ctl(enum arch_timer timer, uint32_t ctl)114{115switch (timer) {116case VIRTUAL:117write_sysreg(ctl, cntv_ctl_el0);118break;119case PHYSICAL:120write_sysreg(ctl, cntp_ctl_el0);121break;122default:123GUEST_FAIL("Unexpected timer type = %u", timer);124}125126isb();127}128129static inline uint32_t timer_get_ctl(enum arch_timer timer)130{131switch (timer) {132case VIRTUAL:133return read_sysreg(cntv_ctl_el0);134case PHYSICAL:135return read_sysreg(cntp_ctl_el0);136default:137GUEST_FAIL("Unexpected timer type = %u", timer);138}139140/* We should not reach here */141return 0;142}143144static inline void timer_set_next_cval_ms(enum arch_timer timer, uint32_t msec)145{146uint64_t now_ct = timer_get_cntct(timer);147uint64_t next_ct = now_ct + msec_to_cycles(msec);148149timer_set_cval(timer, next_ct);150}151152static inline void timer_set_next_tval_ms(enum arch_timer timer, uint32_t msec)153{154timer_set_tval(timer, msec_to_cycles(msec));155}156157static inline u32 vcpu_get_vtimer_irq(struct kvm_vcpu *vcpu)158{159u32 intid;160u64 attr;161162attr = vcpu_has_el2(vcpu) ? KVM_ARM_VCPU_TIMER_IRQ_HVTIMER :163KVM_ARM_VCPU_TIMER_IRQ_VTIMER;164vcpu_device_attr_get(vcpu, KVM_ARM_VCPU_TIMER_CTRL, attr, &intid);165166return intid;167}168169static inline u32 vcpu_get_ptimer_irq(struct kvm_vcpu *vcpu)170{171u32 intid;172u64 attr;173174attr = vcpu_has_el2(vcpu) ? KVM_ARM_VCPU_TIMER_IRQ_HPTIMER :175KVM_ARM_VCPU_TIMER_IRQ_PTIMER;176vcpu_device_attr_get(vcpu, KVM_ARM_VCPU_TIMER_CTRL, attr, &intid);177178return intid;179}180181#endif /* SELFTEST_KVM_ARCH_TIMER_H */182183184