^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2012 ARM Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Author: Marc Zyngier <marc.zyngier@arm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/kvm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/kvm_host.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <clocksource/arm_arch_timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <asm/arch_timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <asm/kvm_emulate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <asm/kvm_hyp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <kvm/arm_vgic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <kvm/arm_arch_timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static struct timecounter *timecounter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static unsigned int host_vtimer_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static unsigned int host_ptimer_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static u32 host_vtimer_irq_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static u32 host_ptimer_irq_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static DEFINE_STATIC_KEY_FALSE(has_gic_active_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static const struct kvm_irq_level default_ptimer_irq = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) .irq = 30,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) .level = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static const struct kvm_irq_level default_vtimer_irq = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) .irq = 27,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) .level = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct arch_timer_context *timer_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static void kvm_arm_timer_write(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct arch_timer_context *timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) enum kvm_arch_timer_regs treg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) u64 val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static u64 kvm_arm_timer_read(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct arch_timer_context *timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) enum kvm_arch_timer_regs treg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) u32 timer_get_ctl(struct arch_timer_context *ctxt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct kvm_vcpu *vcpu = ctxt->vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) switch(arch_timer_ctx_index(ctxt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) case TIMER_VTIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return __vcpu_sys_reg(vcpu, CNTV_CTL_EL0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) case TIMER_PTIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return __vcpu_sys_reg(vcpu, CNTP_CTL_EL0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) u64 timer_get_cval(struct arch_timer_context *ctxt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct kvm_vcpu *vcpu = ctxt->vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) switch(arch_timer_ctx_index(ctxt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) case TIMER_VTIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return __vcpu_sys_reg(vcpu, CNTV_CVAL_EL0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) case TIMER_PTIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return __vcpu_sys_reg(vcpu, CNTP_CVAL_EL0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static u64 timer_get_offset(struct arch_timer_context *ctxt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct kvm_vcpu *vcpu = ctxt->vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) switch(arch_timer_ctx_index(ctxt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) case TIMER_VTIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return __vcpu_sys_reg(vcpu, CNTVOFF_EL2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static void timer_set_ctl(struct arch_timer_context *ctxt, u32 ctl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct kvm_vcpu *vcpu = ctxt->vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) switch(arch_timer_ctx_index(ctxt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) case TIMER_VTIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) __vcpu_sys_reg(vcpu, CNTV_CTL_EL0) = ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) case TIMER_PTIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) __vcpu_sys_reg(vcpu, CNTP_CTL_EL0) = ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static void timer_set_cval(struct arch_timer_context *ctxt, u64 cval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct kvm_vcpu *vcpu = ctxt->vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) switch(arch_timer_ctx_index(ctxt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) case TIMER_VTIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) __vcpu_sys_reg(vcpu, CNTV_CVAL_EL0) = cval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) case TIMER_PTIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) __vcpu_sys_reg(vcpu, CNTP_CVAL_EL0) = cval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static void timer_set_offset(struct arch_timer_context *ctxt, u64 offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct kvm_vcpu *vcpu = ctxt->vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) switch(arch_timer_ctx_index(ctxt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) case TIMER_VTIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) __vcpu_sys_reg(vcpu, CNTVOFF_EL2) = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) WARN(offset, "timer %ld\n", arch_timer_ctx_index(ctxt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) u64 kvm_phys_timer_read(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return timecounter->cc->read(timecounter->cc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static void get_timer_map(struct kvm_vcpu *vcpu, struct timer_map *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (has_vhe()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) map->direct_vtimer = vcpu_vtimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) map->direct_ptimer = vcpu_ptimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) map->emul_ptimer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) map->direct_vtimer = vcpu_vtimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) map->direct_ptimer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) map->emul_ptimer = vcpu_ptimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) trace_kvm_get_timer_map(vcpu->vcpu_id, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static inline bool userspace_irqchip(struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return static_branch_unlikely(&userspace_irqchip_in_use) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) unlikely(!irqchip_in_kernel(kvm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static void soft_timer_start(struct hrtimer *hrt, u64 ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) hrtimer_start(hrt, ktime_add_ns(ktime_get(), ns),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) HRTIMER_MODE_ABS_HARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static void soft_timer_cancel(struct hrtimer *hrt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) hrtimer_cancel(hrt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) struct arch_timer_context *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct timer_map map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * We may see a timer interrupt after vcpu_put() has been called which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * sets the CPU's vcpu pointer to NULL, because even though the timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * has been disabled in timer_save_state(), the hardware interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * signal may not have been retired from the interrupt controller yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (!vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) get_timer_map(vcpu, &map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (irq == host_vtimer_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) ctx = map.direct_vtimer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) ctx = map.direct_ptimer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (kvm_timer_should_fire(ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) kvm_timer_update_irq(vcpu, true, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (userspace_irqchip(vcpu->kvm) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) !static_branch_unlikely(&has_gic_active_state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) disable_percpu_irq(host_vtimer_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static u64 kvm_timer_compute_delta(struct arch_timer_context *timer_ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) u64 cval, now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) cval = timer_get_cval(timer_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) now = kvm_phys_timer_read() - timer_get_offset(timer_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (now < cval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) u64 ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) ns = cyclecounter_cyc2ns(timecounter->cc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) cval - now,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) timecounter->mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) &timecounter->frac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) WARN_ON(timer_ctx && timer_ctx->loaded);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) return timer_ctx &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) ((timer_get_ctl(timer_ctx) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) (ARCH_TIMER_CTRL_IT_MASK | ARCH_TIMER_CTRL_ENABLE)) == ARCH_TIMER_CTRL_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * Returns the earliest expiration time in ns among guest timers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * Note that it will return 0 if none of timers can fire.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static u64 kvm_timer_earliest_exp(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) u64 min_delta = ULLONG_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) for (i = 0; i < NR_KVM_TIMERS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) struct arch_timer_context *ctx = &vcpu->arch.timer_cpu.timers[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) WARN(ctx->loaded, "timer %d loaded\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (kvm_timer_irq_can_fire(ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) min_delta = min(min_delta, kvm_timer_compute_delta(ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) /* If none of timers can fire, then return 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (min_delta == ULLONG_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return min_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) static enum hrtimer_restart kvm_bg_timer_expire(struct hrtimer *hrt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct arch_timer_cpu *timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct kvm_vcpu *vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) u64 ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) timer = container_of(hrt, struct arch_timer_cpu, bg_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * Check that the timer has really expired from the guest's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * PoV (NTP on the host may have forced it to expire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * early). If we should have slept longer, restart it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) ns = kvm_timer_earliest_exp(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (unlikely(ns)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) hrtimer_forward_now(hrt, ns_to_ktime(ns));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return HRTIMER_RESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) kvm_vcpu_wake_up(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return HRTIMER_NORESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static enum hrtimer_restart kvm_hrtimer_expire(struct hrtimer *hrt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) struct arch_timer_context *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) struct kvm_vcpu *vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) u64 ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) ctx = container_of(hrt, struct arch_timer_context, hrtimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) vcpu = ctx->vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) trace_kvm_timer_hrtimer_expire(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * Check that the timer has really expired from the guest's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * PoV (NTP on the host may have forced it to expire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * early). If not ready, schedule for a later time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) ns = kvm_timer_compute_delta(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (unlikely(ns)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) hrtimer_forward_now(hrt, ns_to_ktime(ns));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return HRTIMER_RESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) kvm_timer_update_irq(vcpu, true, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) return HRTIMER_NORESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) enum kvm_arch_timers index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) u64 cval, now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (!timer_ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) index = arch_timer_ctx_index(timer_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (timer_ctx->loaded) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) u32 cnt_ctl = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) switch (index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) case TIMER_VTIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) cnt_ctl = read_sysreg_el0(SYS_CNTV_CTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) case TIMER_PTIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) cnt_ctl = read_sysreg_el0(SYS_CNTP_CTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) case NR_KVM_TIMERS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /* GCC is braindead */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) cnt_ctl = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return (cnt_ctl & ARCH_TIMER_CTRL_ENABLE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) (cnt_ctl & ARCH_TIMER_CTRL_IT_STAT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) !(cnt_ctl & ARCH_TIMER_CTRL_IT_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (!kvm_timer_irq_can_fire(timer_ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) cval = timer_get_cval(timer_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) now = kvm_phys_timer_read() - timer_get_offset(timer_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return cval <= now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) bool kvm_timer_is_pending(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) struct timer_map map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) get_timer_map(vcpu, &map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return kvm_timer_should_fire(map.direct_vtimer) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) kvm_timer_should_fire(map.direct_ptimer) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) kvm_timer_should_fire(map.emul_ptimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * Reflect the timer output level into the kvm_run structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) void kvm_timer_update_run(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) struct kvm_sync_regs *regs = &vcpu->run->s.regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) /* Populate the device bitmap with the timer states */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) regs->device_irq_level &= ~(KVM_ARM_DEV_EL1_VTIMER |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) KVM_ARM_DEV_EL1_PTIMER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (kvm_timer_should_fire(vtimer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) regs->device_irq_level |= KVM_ARM_DEV_EL1_VTIMER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) if (kvm_timer_should_fire(ptimer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) regs->device_irq_level |= KVM_ARM_DEV_EL1_PTIMER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) struct arch_timer_context *timer_ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) timer_ctx->irq.level = new_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) trace_kvm_timer_update_irq(vcpu->vcpu_id, timer_ctx->irq.irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) timer_ctx->irq.level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (!userspace_irqchip(vcpu->kvm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) timer_ctx->irq.irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) timer_ctx->irq.level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) timer_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) WARN_ON(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) /* Only called for a fully emulated timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) static void timer_emulate(struct arch_timer_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) bool should_fire = kvm_timer_should_fire(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) trace_kvm_timer_emulate(ctx, should_fire);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (should_fire != ctx->irq.level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) kvm_timer_update_irq(ctx->vcpu, should_fire, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * If the timer can fire now, we don't need to have a soft timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * scheduled for the future. If the timer cannot fire at all,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) * then we also don't need a soft timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (!kvm_timer_irq_can_fire(ctx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) soft_timer_cancel(&ctx->hrtimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) soft_timer_start(&ctx->hrtimer, kvm_timer_compute_delta(ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) static void timer_save_state(struct arch_timer_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) struct arch_timer_cpu *timer = vcpu_timer(ctx->vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) enum kvm_arch_timers index = arch_timer_ctx_index(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) if (!timer->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) if (!ctx->loaded)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) switch (index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) case TIMER_VTIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) timer_set_ctl(ctx, read_sysreg_el0(SYS_CNTV_CTL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) timer_set_cval(ctx, read_sysreg_el0(SYS_CNTV_CVAL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) /* Disable the timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) write_sysreg_el0(0, SYS_CNTV_CTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) isb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) case TIMER_PTIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) timer_set_ctl(ctx, read_sysreg_el0(SYS_CNTP_CTL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) timer_set_cval(ctx, read_sysreg_el0(SYS_CNTP_CVAL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) /* Disable the timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) write_sysreg_el0(0, SYS_CNTP_CTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) isb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) case NR_KVM_TIMERS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) trace_kvm_timer_save_state(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) ctx->loaded = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) * Schedule the background timer before calling kvm_vcpu_block, so that this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) * thread is removed from its waitqueue and made runnable when there's a timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) * interrupt to handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) static void kvm_timer_blocking(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) struct arch_timer_cpu *timer = vcpu_timer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) struct timer_map map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) get_timer_map(vcpu, &map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * If no timers are capable of raising interrupts (disabled or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * masked), then there's no more work for us to do.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) if (!kvm_timer_irq_can_fire(map.direct_vtimer) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) !kvm_timer_irq_can_fire(map.direct_ptimer) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) !kvm_timer_irq_can_fire(map.emul_ptimer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) * At least one guest time will expire. Schedule a background timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) * Set the earliest expiration time among the guest timers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) soft_timer_start(&timer->bg_timer, kvm_timer_earliest_exp(vcpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) static void kvm_timer_unblocking(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) struct arch_timer_cpu *timer = vcpu_timer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) soft_timer_cancel(&timer->bg_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) static void timer_restore_state(struct arch_timer_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) struct arch_timer_cpu *timer = vcpu_timer(ctx->vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) enum kvm_arch_timers index = arch_timer_ctx_index(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) if (!timer->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (ctx->loaded)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) switch (index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) case TIMER_VTIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) write_sysreg_el0(timer_get_cval(ctx), SYS_CNTV_CVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) isb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) write_sysreg_el0(timer_get_ctl(ctx), SYS_CNTV_CTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) case TIMER_PTIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) write_sysreg_el0(timer_get_cval(ctx), SYS_CNTP_CVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) isb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) write_sysreg_el0(timer_get_ctl(ctx), SYS_CNTP_CTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) case NR_KVM_TIMERS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) trace_kvm_timer_restore_state(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) ctx->loaded = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) static void set_cntvoff(u64 cntvoff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) kvm_call_hyp(__kvm_timer_set_cntvoff, cntvoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) static inline void set_timer_irq_phys_active(struct arch_timer_context *ctx, bool active)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) r = irq_set_irqchip_state(ctx->host_timer_irq, IRQCHIP_STATE_ACTIVE, active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) WARN_ON(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) static void kvm_timer_vcpu_load_gic(struct arch_timer_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) struct kvm_vcpu *vcpu = ctx->vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) bool phys_active = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) * Update the timer output so that it is likely to match the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * state we're about to restore. If the timer expires between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) * this point and the register restoration, we'll take the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) * interrupt anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) kvm_timer_update_irq(ctx->vcpu, kvm_timer_should_fire(ctx), ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) if (irqchip_in_kernel(vcpu->kvm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) phys_active = kvm_vgic_map_is_active(vcpu, ctx->irq.irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) phys_active |= ctx->irq.level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) set_timer_irq_phys_active(ctx, phys_active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) static void kvm_timer_vcpu_load_nogic(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) * Update the timer output so that it is likely to match the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) * state we're about to restore. If the timer expires between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) * this point and the register restoration, we'll take the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) * interrupt anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) kvm_timer_update_irq(vcpu, kvm_timer_should_fire(vtimer), vtimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) * When using a userspace irqchip with the architected timers and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) * host interrupt controller that doesn't support an active state, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) * must still prevent continuously exiting from the guest, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) * therefore mask the physical interrupt by disabling it on the host
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) * interrupt controller when the virtual level is high, such that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) * guest can make forward progress. Once we detect the output level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) * being de-asserted, we unmask the interrupt again so that we exit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) * from the guest when the timer fires.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) if (vtimer->irq.level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) disable_percpu_irq(host_vtimer_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) struct arch_timer_cpu *timer = vcpu_timer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) struct timer_map map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) if (unlikely(!timer->enabled))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) get_timer_map(vcpu, &map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) if (static_branch_likely(&has_gic_active_state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) kvm_timer_vcpu_load_gic(map.direct_vtimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) if (map.direct_ptimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) kvm_timer_vcpu_load_gic(map.direct_ptimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) kvm_timer_vcpu_load_nogic(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) set_cntvoff(timer_get_offset(map.direct_vtimer));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) kvm_timer_unblocking(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) timer_restore_state(map.direct_vtimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) if (map.direct_ptimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) timer_restore_state(map.direct_ptimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) if (map.emul_ptimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) timer_emulate(map.emul_ptimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) bool kvm_timer_should_notify_user(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) struct kvm_sync_regs *sregs = &vcpu->run->s.regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) bool vlevel, plevel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) if (likely(irqchip_in_kernel(vcpu->kvm)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) vlevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_VTIMER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) plevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_PTIMER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) return kvm_timer_should_fire(vtimer) != vlevel ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) kvm_timer_should_fire(ptimer) != plevel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) struct arch_timer_cpu *timer = vcpu_timer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) struct timer_map map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) if (unlikely(!timer->enabled))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) get_timer_map(vcpu, &map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) timer_save_state(map.direct_vtimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) if (map.direct_ptimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) timer_save_state(map.direct_ptimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) * Cancel soft timer emulation, because the only case where we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) * need it after a vcpu_put is in the context of a sleeping VCPU, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) * in that case we already factor in the deadline for the physical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) * timer when scheduling the bg_timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) * In any case, we re-schedule the hrtimer for the physical timer when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) * coming back to the VCPU thread in kvm_timer_vcpu_load().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) if (map.emul_ptimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) soft_timer_cancel(&map.emul_ptimer->hrtimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) if (rcuwait_active(wait))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) kvm_timer_blocking(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) * The kernel may decide to run userspace after calling vcpu_put, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) * we reset cntvoff to 0 to ensure a consistent read between user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) * accesses to the virtual counter and kernel access to the physical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) * counter of non-VHE case. For VHE, the virtual counter uses a fixed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) * virtual offset of zero, so no need to zero CNTVOFF_EL2 register.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) set_cntvoff(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) * With a userspace irqchip we have to check if the guest de-asserted the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) * timer and if so, unmask the timer irq signal on the host interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) * controller to ensure that we see future timer signals.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) static void unmask_vtimer_irq_user(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) if (!kvm_timer_should_fire(vtimer)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) kvm_timer_update_irq(vcpu, false, vtimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) if (static_branch_likely(&has_gic_active_state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) set_timer_irq_phys_active(vtimer, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) void kvm_timer_sync_user(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) struct arch_timer_cpu *timer = vcpu_timer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) if (unlikely(!timer->enabled))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) if (unlikely(!irqchip_in_kernel(vcpu->kvm)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) unmask_vtimer_irq_user(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) struct arch_timer_cpu *timer = vcpu_timer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) struct timer_map map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) get_timer_map(vcpu, &map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) * and to 0 for ARMv7. We provide an implementation that always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * resets the timer to be disabled and unmasked and is compliant with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) * the ARMv7 architecture.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) timer_set_ctl(vcpu_vtimer(vcpu), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) timer_set_ctl(vcpu_ptimer(vcpu), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) if (timer->enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) kvm_timer_update_irq(vcpu, false, vcpu_vtimer(vcpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) kvm_timer_update_irq(vcpu, false, vcpu_ptimer(vcpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) if (irqchip_in_kernel(vcpu->kvm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) kvm_vgic_reset_mapped_irq(vcpu, map.direct_vtimer->irq.irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) if (map.direct_ptimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) kvm_vgic_reset_mapped_irq(vcpu, map.direct_ptimer->irq.irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) if (map.emul_ptimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) soft_timer_cancel(&map.emul_ptimer->hrtimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) /* Make the updates of cntvoff for all vtimer contexts atomic */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) static void update_vtimer_cntvoff(struct kvm_vcpu *vcpu, u64 cntvoff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) struct kvm *kvm = vcpu->kvm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) struct kvm_vcpu *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) mutex_lock(&kvm->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) kvm_for_each_vcpu(i, tmp, kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) timer_set_offset(vcpu_vtimer(tmp), cntvoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) * When called from the vcpu create path, the CPU being created is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) * included in the loop above, so we just set it here as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) timer_set_offset(vcpu_vtimer(vcpu), cntvoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) mutex_unlock(&kvm->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) struct arch_timer_cpu *timer = vcpu_timer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) vtimer->vcpu = vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) ptimer->vcpu = vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) /* Synchronize cntvoff across all vtimers of a VM. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) update_vtimer_cntvoff(vcpu, kvm_phys_timer_read());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) timer_set_offset(ptimer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) hrtimer_init(&timer->bg_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) timer->bg_timer.function = kvm_bg_timer_expire;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) hrtimer_init(&vtimer->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) hrtimer_init(&ptimer->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) vtimer->hrtimer.function = kvm_hrtimer_expire;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) ptimer->hrtimer.function = kvm_hrtimer_expire;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) vtimer->irq.irq = default_vtimer_irq.irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) ptimer->irq.irq = default_ptimer_irq.irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) vtimer->host_timer_irq = host_vtimer_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) ptimer->host_timer_irq = host_ptimer_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) vtimer->host_timer_irq_flags = host_vtimer_irq_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) ptimer->host_timer_irq_flags = host_ptimer_irq_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) static void kvm_timer_init_interrupt(void *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) enable_percpu_irq(host_ptimer_irq, host_ptimer_irq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) struct arch_timer_context *timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) switch (regid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) case KVM_REG_ARM_TIMER_CTL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) timer = vcpu_vtimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) kvm_arm_timer_write(vcpu, timer, TIMER_REG_CTL, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) case KVM_REG_ARM_TIMER_CNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) timer = vcpu_vtimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) update_vtimer_cntvoff(vcpu, kvm_phys_timer_read() - value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) case KVM_REG_ARM_TIMER_CVAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) timer = vcpu_vtimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) kvm_arm_timer_write(vcpu, timer, TIMER_REG_CVAL, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) case KVM_REG_ARM_PTIMER_CTL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) timer = vcpu_ptimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) kvm_arm_timer_write(vcpu, timer, TIMER_REG_CTL, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) case KVM_REG_ARM_PTIMER_CVAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) timer = vcpu_ptimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) kvm_arm_timer_write(vcpu, timer, TIMER_REG_CVAL, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) static u64 read_timer_ctl(struct arch_timer_context *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) * Set ISTATUS bit if it's expired.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) * Note that according to ARMv8 ARM Issue A.k, ISTATUS bit is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) * UNKNOWN when ENABLE bit is 0, so we chose to set ISTATUS bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) * regardless of ENABLE bit for our implementation convenience.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) u32 ctl = timer_get_ctl(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) if (!kvm_timer_compute_delta(timer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) ctl |= ARCH_TIMER_CTRL_IT_STAT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) return ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) switch (regid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) case KVM_REG_ARM_TIMER_CTL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) return kvm_arm_timer_read(vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) vcpu_vtimer(vcpu), TIMER_REG_CTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) case KVM_REG_ARM_TIMER_CNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) return kvm_arm_timer_read(vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) vcpu_vtimer(vcpu), TIMER_REG_CNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) case KVM_REG_ARM_TIMER_CVAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) return kvm_arm_timer_read(vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) vcpu_vtimer(vcpu), TIMER_REG_CVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) case KVM_REG_ARM_PTIMER_CTL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) return kvm_arm_timer_read(vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) vcpu_ptimer(vcpu), TIMER_REG_CTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) case KVM_REG_ARM_PTIMER_CNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) return kvm_arm_timer_read(vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) vcpu_ptimer(vcpu), TIMER_REG_CNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) case KVM_REG_ARM_PTIMER_CVAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) return kvm_arm_timer_read(vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) vcpu_ptimer(vcpu), TIMER_REG_CVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) return (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) static u64 kvm_arm_timer_read(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) struct arch_timer_context *timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) enum kvm_arch_timer_regs treg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) u64 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) switch (treg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) case TIMER_REG_TVAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) val = timer_get_cval(timer) - kvm_phys_timer_read() + timer_get_offset(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) val = lower_32_bits(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) case TIMER_REG_CTL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) val = read_timer_ctl(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) case TIMER_REG_CVAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) val = timer_get_cval(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) case TIMER_REG_CNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) val = kvm_phys_timer_read() - timer_get_offset(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) u64 kvm_arm_timer_read_sysreg(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) enum kvm_arch_timers tmr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) enum kvm_arch_timer_regs treg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) u64 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) kvm_timer_vcpu_put(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) val = kvm_arm_timer_read(vcpu, vcpu_get_timer(vcpu, tmr), treg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) kvm_timer_vcpu_load(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) static void kvm_arm_timer_write(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) struct arch_timer_context *timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) enum kvm_arch_timer_regs treg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) switch (treg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) case TIMER_REG_TVAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) timer_set_cval(timer, kvm_phys_timer_read() - timer_get_offset(timer) + (s32)val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) case TIMER_REG_CTL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) timer_set_ctl(timer, val & ~ARCH_TIMER_CTRL_IT_STAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) case TIMER_REG_CVAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) timer_set_cval(timer, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) void kvm_arm_timer_write_sysreg(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) enum kvm_arch_timers tmr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) enum kvm_arch_timer_regs treg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) kvm_timer_vcpu_put(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) kvm_arm_timer_write(vcpu, vcpu_get_timer(vcpu, tmr), treg, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) kvm_timer_vcpu_load(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) static int kvm_timer_starting_cpu(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) kvm_timer_init_interrupt(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) static int kvm_timer_dying_cpu(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) disable_percpu_irq(host_vtimer_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) int kvm_timer_hyp_init(bool has_gic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) struct arch_timer_kvm_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) info = arch_timer_get_kvm_info();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) timecounter = &info->timecounter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) if (!timecounter->cc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) kvm_err("kvm_arch_timer: uninitialized timecounter\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) /* First, do the virtual EL1 timer irq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) if (info->virtual_irq <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) kvm_err("kvm_arch_timer: invalid virtual timer IRQ: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) info->virtual_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) host_vtimer_irq = info->virtual_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) host_vtimer_irq_flags = irq_get_trigger_type(host_vtimer_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) if (host_vtimer_irq_flags != IRQF_TRIGGER_HIGH &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) host_vtimer_irq_flags != IRQF_TRIGGER_LOW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) kvm_err("Invalid trigger for vtimer IRQ%d, assuming level low\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) host_vtimer_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) host_vtimer_irq_flags = IRQF_TRIGGER_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) err = request_percpu_irq(host_vtimer_irq, kvm_arch_timer_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) "kvm guest vtimer", kvm_get_running_vcpus());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) kvm_err("kvm_arch_timer: can't request vtimer interrupt %d (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) host_vtimer_irq, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) if (has_gic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) err = irq_set_vcpu_affinity(host_vtimer_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) kvm_get_running_vcpus());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) kvm_err("kvm_arch_timer: error setting vcpu affinity\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) goto out_free_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) static_branch_enable(&has_gic_active_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) kvm_debug("virtual timer IRQ%d\n", host_vtimer_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) /* Now let's do the physical EL1 timer irq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) if (info->physical_irq > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) host_ptimer_irq = info->physical_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) host_ptimer_irq_flags = irq_get_trigger_type(host_ptimer_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) if (host_ptimer_irq_flags != IRQF_TRIGGER_HIGH &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) host_ptimer_irq_flags != IRQF_TRIGGER_LOW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) kvm_err("Invalid trigger for ptimer IRQ%d, assuming level low\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) host_ptimer_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) host_ptimer_irq_flags = IRQF_TRIGGER_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) err = request_percpu_irq(host_ptimer_irq, kvm_arch_timer_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) "kvm guest ptimer", kvm_get_running_vcpus());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) kvm_err("kvm_arch_timer: can't request ptimer interrupt %d (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) host_ptimer_irq, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) if (has_gic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) err = irq_set_vcpu_affinity(host_ptimer_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) kvm_get_running_vcpus());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) kvm_err("kvm_arch_timer: error setting vcpu affinity\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) goto out_free_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) kvm_debug("physical timer IRQ%d\n", host_ptimer_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) } else if (has_vhe()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) kvm_err("kvm_arch_timer: invalid physical timer IRQ: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) info->physical_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) goto out_free_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) cpuhp_setup_state(CPUHP_AP_KVM_ARM_TIMER_STARTING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) "kvm/arm/timer:starting", kvm_timer_starting_cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) kvm_timer_dying_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) out_free_irq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) free_percpu_irq(host_vtimer_irq, kvm_get_running_vcpus());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) struct arch_timer_cpu *timer = vcpu_timer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) soft_timer_cancel(&timer->bg_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) static bool timer_irqs_are_valid(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) int vtimer_irq, ptimer_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) vtimer_irq = vcpu_vtimer(vcpu)->irq.irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) ret = kvm_vgic_set_owner(vcpu, vtimer_irq, vcpu_vtimer(vcpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) ptimer_irq = vcpu_ptimer(vcpu)->irq.irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) ret = kvm_vgic_set_owner(vcpu, ptimer_irq, vcpu_ptimer(vcpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) kvm_for_each_vcpu(i, vcpu, vcpu->kvm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) if (vcpu_vtimer(vcpu)->irq.irq != vtimer_irq ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) vcpu_ptimer(vcpu)->irq.irq != ptimer_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) bool kvm_arch_timer_get_input_level(int vintid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) struct arch_timer_context *timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) if (vintid == vcpu_vtimer(vcpu)->irq.irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) timer = vcpu_vtimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) else if (vintid == vcpu_ptimer(vcpu)->irq.irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) timer = vcpu_ptimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) return kvm_timer_should_fire(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) int kvm_timer_enable(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) struct arch_timer_cpu *timer = vcpu_timer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) struct timer_map map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) if (timer->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) /* Without a VGIC we do not map virtual IRQs to physical IRQs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) if (!irqchip_in_kernel(vcpu->kvm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) goto no_vgic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) * At this stage, we have the guarantee that the vgic is both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) * available and initialized.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) if (!timer_irqs_are_valid(vcpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) kvm_debug("incorrectly configured timer irqs\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) get_timer_map(vcpu, &map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) ret = kvm_vgic_map_phys_irq(vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) map.direct_vtimer->host_timer_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) map.direct_vtimer->irq.irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) kvm_arch_timer_get_input_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) if (map.direct_ptimer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) ret = kvm_vgic_map_phys_irq(vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) map.direct_ptimer->host_timer_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) map.direct_ptimer->irq.irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) kvm_arch_timer_get_input_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) no_vgic:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) timer->enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) * On VHE system, we only need to configure the EL2 timer trap register once,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) * not for every world switch.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) * The host kernel runs at EL2 with HCR_EL2.TGE == 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) * and this makes those bits have no effect for the host kernel execution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) void kvm_timer_init_vhe(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) /* When HCR_EL2.E2H ==1, EL1PCEN and EL1PCTEN are shifted by 10 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) u32 cnthctl_shift = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) u64 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) * VHE systems allow the guest direct access to the EL1 physical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) * timer/counter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) val = read_sysreg(cnthctl_el2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) val |= (CNTHCTL_EL1PCEN << cnthctl_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) val |= (CNTHCTL_EL1PCTEN << cnthctl_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) write_sysreg(val, cnthctl_el2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) static void set_timer_irqs(struct kvm *kvm, int vtimer_irq, int ptimer_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) struct kvm_vcpu *vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) kvm_for_each_vcpu(i, vcpu, kvm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) vcpu_vtimer(vcpu)->irq.irq = vtimer_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) vcpu_ptimer(vcpu)->irq.irq = ptimer_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) int kvm_arm_timer_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) int __user *uaddr = (int __user *)(long)attr->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) if (!irqchip_in_kernel(vcpu->kvm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) if (get_user(irq, uaddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) if (!(irq_is_ppi(irq)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) if (vcpu->arch.timer_cpu.enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) switch (attr->attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) set_timer_irqs(vcpu->kvm, irq, ptimer->irq.irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) set_timer_irqs(vcpu->kvm, vtimer->irq.irq, irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) int kvm_arm_timer_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) int __user *uaddr = (int __user *)(long)attr->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) struct arch_timer_context *timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) switch (attr->attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) timer = vcpu_vtimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) timer = vcpu_ptimer(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) irq = timer->irq.irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) return put_user(irq, uaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) int kvm_arm_timer_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) switch (attr->attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) }