1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 ARM Ltd.
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
9 #include <linux/kvm_host.h>
10 #include <linux/interrupt.h>
11 #include <linux/irq.h>
12 #include <linux/irqdomain.h>
13 #include <linux/uaccess.h>
15 #include <clocksource/arm_arch_timer.h>
16 #include <asm/arch_timer.h>
17 #include <asm/kvm_emulate.h>
18 #include <asm/kvm_hyp.h>
20 #include <kvm/arm_vgic.h>
21 #include <kvm/arm_arch_timer.h>
25 static struct timecounter *timecounter;
26 static unsigned int host_vtimer_irq;
27 static unsigned int host_ptimer_irq;
28 static u32 host_vtimer_irq_flags;
29 static u32 host_ptimer_irq_flags;
31 static DEFINE_STATIC_KEY_FALSE(has_gic_active_state);
33 static const struct kvm_irq_level default_ptimer_irq = {
38 static const struct kvm_irq_level default_vtimer_irq = {
43 static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx);
44 static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
45 struct arch_timer_context *timer_ctx);
46 static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx);
47 static void kvm_arm_timer_write(struct kvm_vcpu *vcpu,
48 struct arch_timer_context *timer,
49 enum kvm_arch_timer_regs treg,
51 static u64 kvm_arm_timer_read(struct kvm_vcpu *vcpu,
52 struct arch_timer_context *timer,
53 enum kvm_arch_timer_regs treg);
55 u32 timer_get_ctl(struct arch_timer_context *ctxt)
57 struct kvm_vcpu *vcpu = ctxt->vcpu;
59 switch(arch_timer_ctx_index(ctxt)) {
61 return __vcpu_sys_reg(vcpu, CNTV_CTL_EL0);
63 return __vcpu_sys_reg(vcpu, CNTP_CTL_EL0);
70 u64 timer_get_cval(struct arch_timer_context *ctxt)
72 struct kvm_vcpu *vcpu = ctxt->vcpu;
74 switch(arch_timer_ctx_index(ctxt)) {
76 return __vcpu_sys_reg(vcpu, CNTV_CVAL_EL0);
78 return __vcpu_sys_reg(vcpu, CNTP_CVAL_EL0);
85 static u64 timer_get_offset(struct arch_timer_context *ctxt)
87 struct kvm_vcpu *vcpu = ctxt->vcpu;
89 switch(arch_timer_ctx_index(ctxt)) {
91 return __vcpu_sys_reg(vcpu, CNTVOFF_EL2);
97 static void timer_set_ctl(struct arch_timer_context *ctxt, u32 ctl)
99 struct kvm_vcpu *vcpu = ctxt->vcpu;
101 switch(arch_timer_ctx_index(ctxt)) {
103 __vcpu_sys_reg(vcpu, CNTV_CTL_EL0) = ctl;
106 __vcpu_sys_reg(vcpu, CNTP_CTL_EL0) = ctl;
113 static void timer_set_cval(struct arch_timer_context *ctxt, u64 cval)
115 struct kvm_vcpu *vcpu = ctxt->vcpu;
117 switch(arch_timer_ctx_index(ctxt)) {
119 __vcpu_sys_reg(vcpu, CNTV_CVAL_EL0) = cval;
122 __vcpu_sys_reg(vcpu, CNTP_CVAL_EL0) = cval;
129 static void timer_set_offset(struct arch_timer_context *ctxt, u64 offset)
131 struct kvm_vcpu *vcpu = ctxt->vcpu;
133 switch(arch_timer_ctx_index(ctxt)) {
135 __vcpu_sys_reg(vcpu, CNTVOFF_EL2) = offset;
138 WARN(offset, "timer %ld\n", arch_timer_ctx_index(ctxt));
142 u64 kvm_phys_timer_read(void)
144 return timecounter->cc->read(timecounter->cc);
147 static void get_timer_map(struct kvm_vcpu *vcpu, struct timer_map *map)
150 map->direct_vtimer = vcpu_vtimer(vcpu);
151 map->direct_ptimer = vcpu_ptimer(vcpu);
152 map->emul_ptimer = NULL;
154 map->direct_vtimer = vcpu_vtimer(vcpu);
155 map->direct_ptimer = NULL;
156 map->emul_ptimer = vcpu_ptimer(vcpu);
159 trace_kvm_get_timer_map(vcpu->vcpu_id, map);
162 static inline bool userspace_irqchip(struct kvm *kvm)
164 return static_branch_unlikely(&userspace_irqchip_in_use) &&
165 unlikely(!irqchip_in_kernel(kvm));
168 static void soft_timer_start(struct hrtimer *hrt, u64 ns)
170 hrtimer_start(hrt, ktime_add_ns(ktime_get(), ns),
171 HRTIMER_MODE_ABS_HARD);
174 static void soft_timer_cancel(struct hrtimer *hrt)
179 static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
181 struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
182 struct arch_timer_context *ctx;
183 struct timer_map map;
186 * We may see a timer interrupt after vcpu_put() has been called which
187 * sets the CPU's vcpu pointer to NULL, because even though the timer
188 * has been disabled in timer_save_state(), the hardware interrupt
189 * signal may not have been retired from the interrupt controller yet.
194 get_timer_map(vcpu, &map);
196 if (irq == host_vtimer_irq)
197 ctx = map.direct_vtimer;
199 ctx = map.direct_ptimer;
201 if (kvm_timer_should_fire(ctx))
202 kvm_timer_update_irq(vcpu, true, ctx);
204 if (userspace_irqchip(vcpu->kvm) &&
205 !static_branch_unlikely(&has_gic_active_state))
206 disable_percpu_irq(host_vtimer_irq);
211 static u64 kvm_timer_compute_delta(struct arch_timer_context *timer_ctx)
215 cval = timer_get_cval(timer_ctx);
216 now = kvm_phys_timer_read() - timer_get_offset(timer_ctx);
221 ns = cyclecounter_cyc2ns(timecounter->cc,
231 static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx)
233 WARN_ON(timer_ctx && timer_ctx->loaded);
235 ((timer_get_ctl(timer_ctx) &
236 (ARCH_TIMER_CTRL_IT_MASK | ARCH_TIMER_CTRL_ENABLE)) == ARCH_TIMER_CTRL_ENABLE);
240 * Returns the earliest expiration time in ns among guest timers.
241 * Note that it will return 0 if none of timers can fire.
243 static u64 kvm_timer_earliest_exp(struct kvm_vcpu *vcpu)
245 u64 min_delta = ULLONG_MAX;
248 for (i = 0; i < NR_KVM_TIMERS; i++) {
249 struct arch_timer_context *ctx = &vcpu->arch.timer_cpu.timers[i];
251 WARN(ctx->loaded, "timer %d loaded\n", i);
252 if (kvm_timer_irq_can_fire(ctx))
253 min_delta = min(min_delta, kvm_timer_compute_delta(ctx));
256 /* If none of timers can fire, then return 0 */
257 if (min_delta == ULLONG_MAX)
263 static enum hrtimer_restart kvm_bg_timer_expire(struct hrtimer *hrt)
265 struct arch_timer_cpu *timer;
266 struct kvm_vcpu *vcpu;
269 timer = container_of(hrt, struct arch_timer_cpu, bg_timer);
270 vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
273 * Check that the timer has really expired from the guest's
274 * PoV (NTP on the host may have forced it to expire
275 * early). If we should have slept longer, restart it.
277 ns = kvm_timer_earliest_exp(vcpu);
279 hrtimer_forward_now(hrt, ns_to_ktime(ns));
280 return HRTIMER_RESTART;
283 kvm_vcpu_wake_up(vcpu);
284 return HRTIMER_NORESTART;
287 static enum hrtimer_restart kvm_hrtimer_expire(struct hrtimer *hrt)
289 struct arch_timer_context *ctx;
290 struct kvm_vcpu *vcpu;
293 ctx = container_of(hrt, struct arch_timer_context, hrtimer);
296 trace_kvm_timer_hrtimer_expire(ctx);
299 * Check that the timer has really expired from the guest's
300 * PoV (NTP on the host may have forced it to expire
301 * early). If not ready, schedule for a later time.
303 ns = kvm_timer_compute_delta(ctx);
305 hrtimer_forward_now(hrt, ns_to_ktime(ns));
306 return HRTIMER_RESTART;
309 kvm_timer_update_irq(vcpu, true, ctx);
310 return HRTIMER_NORESTART;
313 static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx)
315 enum kvm_arch_timers index;
321 index = arch_timer_ctx_index(timer_ctx);
323 if (timer_ctx->loaded) {
328 cnt_ctl = read_sysreg_el0(SYS_CNTV_CTL);
331 cnt_ctl = read_sysreg_el0(SYS_CNTP_CTL);
334 /* GCC is braindead */
339 return (cnt_ctl & ARCH_TIMER_CTRL_ENABLE) &&
340 (cnt_ctl & ARCH_TIMER_CTRL_IT_STAT) &&
341 !(cnt_ctl & ARCH_TIMER_CTRL_IT_MASK);
344 if (!kvm_timer_irq_can_fire(timer_ctx))
347 cval = timer_get_cval(timer_ctx);
348 now = kvm_phys_timer_read() - timer_get_offset(timer_ctx);
353 bool kvm_timer_is_pending(struct kvm_vcpu *vcpu)
355 struct timer_map map;
357 get_timer_map(vcpu, &map);
359 return kvm_timer_should_fire(map.direct_vtimer) ||
360 kvm_timer_should_fire(map.direct_ptimer) ||
361 kvm_timer_should_fire(map.emul_ptimer);
365 * Reflect the timer output level into the kvm_run structure
367 void kvm_timer_update_run(struct kvm_vcpu *vcpu)
369 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
370 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
371 struct kvm_sync_regs *regs = &vcpu->run->s.regs;
373 /* Populate the device bitmap with the timer states */
374 regs->device_irq_level &= ~(KVM_ARM_DEV_EL1_VTIMER |
375 KVM_ARM_DEV_EL1_PTIMER);
376 if (kvm_timer_should_fire(vtimer))
377 regs->device_irq_level |= KVM_ARM_DEV_EL1_VTIMER;
378 if (kvm_timer_should_fire(ptimer))
379 regs->device_irq_level |= KVM_ARM_DEV_EL1_PTIMER;
382 static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
383 struct arch_timer_context *timer_ctx)
387 timer_ctx->irq.level = new_level;
388 trace_kvm_timer_update_irq(vcpu->vcpu_id, timer_ctx->irq.irq,
389 timer_ctx->irq.level);
391 if (!userspace_irqchip(vcpu->kvm)) {
392 ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
394 timer_ctx->irq.level,
400 /* Only called for a fully emulated timer */
401 static void timer_emulate(struct arch_timer_context *ctx)
403 bool should_fire = kvm_timer_should_fire(ctx);
405 trace_kvm_timer_emulate(ctx, should_fire);
407 if (should_fire != ctx->irq.level) {
408 kvm_timer_update_irq(ctx->vcpu, should_fire, ctx);
413 * If the timer can fire now, we don't need to have a soft timer
414 * scheduled for the future. If the timer cannot fire at all,
415 * then we also don't need a soft timer.
417 if (!kvm_timer_irq_can_fire(ctx)) {
418 soft_timer_cancel(&ctx->hrtimer);
422 soft_timer_start(&ctx->hrtimer, kvm_timer_compute_delta(ctx));
425 static void timer_save_state(struct arch_timer_context *ctx)
427 struct arch_timer_cpu *timer = vcpu_timer(ctx->vcpu);
428 enum kvm_arch_timers index = arch_timer_ctx_index(ctx);
434 local_irq_save(flags);
441 timer_set_ctl(ctx, read_sysreg_el0(SYS_CNTV_CTL));
442 timer_set_cval(ctx, read_sysreg_el0(SYS_CNTV_CVAL));
444 /* Disable the timer */
445 write_sysreg_el0(0, SYS_CNTV_CTL);
450 timer_set_ctl(ctx, read_sysreg_el0(SYS_CNTP_CTL));
451 timer_set_cval(ctx, read_sysreg_el0(SYS_CNTP_CVAL));
453 /* Disable the timer */
454 write_sysreg_el0(0, SYS_CNTP_CTL);
462 trace_kvm_timer_save_state(ctx);
466 local_irq_restore(flags);
470 * Schedule the background timer before calling kvm_vcpu_block, so that this
471 * thread is removed from its waitqueue and made runnable when there's a timer
472 * interrupt to handle.
474 static void kvm_timer_blocking(struct kvm_vcpu *vcpu)
476 struct arch_timer_cpu *timer = vcpu_timer(vcpu);
477 struct timer_map map;
479 get_timer_map(vcpu, &map);
482 * If no timers are capable of raising interrupts (disabled or
483 * masked), then there's no more work for us to do.
485 if (!kvm_timer_irq_can_fire(map.direct_vtimer) &&
486 !kvm_timer_irq_can_fire(map.direct_ptimer) &&
487 !kvm_timer_irq_can_fire(map.emul_ptimer))
491 * At least one guest time will expire. Schedule a background timer.
492 * Set the earliest expiration time among the guest timers.
494 soft_timer_start(&timer->bg_timer, kvm_timer_earliest_exp(vcpu));
497 static void kvm_timer_unblocking(struct kvm_vcpu *vcpu)
499 struct arch_timer_cpu *timer = vcpu_timer(vcpu);
501 soft_timer_cancel(&timer->bg_timer);
504 static void timer_restore_state(struct arch_timer_context *ctx)
506 struct arch_timer_cpu *timer = vcpu_timer(ctx->vcpu);
507 enum kvm_arch_timers index = arch_timer_ctx_index(ctx);
513 local_irq_save(flags);
520 write_sysreg_el0(timer_get_cval(ctx), SYS_CNTV_CVAL);
522 write_sysreg_el0(timer_get_ctl(ctx), SYS_CNTV_CTL);
525 write_sysreg_el0(timer_get_cval(ctx), SYS_CNTP_CVAL);
527 write_sysreg_el0(timer_get_ctl(ctx), SYS_CNTP_CTL);
533 trace_kvm_timer_restore_state(ctx);
537 local_irq_restore(flags);
540 static void set_cntvoff(u64 cntvoff)
542 kvm_call_hyp(__kvm_timer_set_cntvoff, cntvoff);
545 static inline void set_timer_irq_phys_active(struct arch_timer_context *ctx, bool active)
548 r = irq_set_irqchip_state(ctx->host_timer_irq, IRQCHIP_STATE_ACTIVE, active);
552 static void kvm_timer_vcpu_load_gic(struct arch_timer_context *ctx)
554 struct kvm_vcpu *vcpu = ctx->vcpu;
555 bool phys_active = false;
558 * Update the timer output so that it is likely to match the
559 * state we're about to restore. If the timer expires between
560 * this point and the register restoration, we'll take the
563 kvm_timer_update_irq(ctx->vcpu, kvm_timer_should_fire(ctx), ctx);
565 if (irqchip_in_kernel(vcpu->kvm))
566 phys_active = kvm_vgic_map_is_active(vcpu, ctx->irq.irq);
568 phys_active |= ctx->irq.level;
570 set_timer_irq_phys_active(ctx, phys_active);
573 static void kvm_timer_vcpu_load_nogic(struct kvm_vcpu *vcpu)
575 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
578 * Update the timer output so that it is likely to match the
579 * state we're about to restore. If the timer expires between
580 * this point and the register restoration, we'll take the
583 kvm_timer_update_irq(vcpu, kvm_timer_should_fire(vtimer), vtimer);
586 * When using a userspace irqchip with the architected timers and a
587 * host interrupt controller that doesn't support an active state, we
588 * must still prevent continuously exiting from the guest, and
589 * therefore mask the physical interrupt by disabling it on the host
590 * interrupt controller when the virtual level is high, such that the
591 * guest can make forward progress. Once we detect the output level
592 * being de-asserted, we unmask the interrupt again so that we exit
593 * from the guest when the timer fires.
595 if (vtimer->irq.level)
596 disable_percpu_irq(host_vtimer_irq);
598 enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
601 void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu)
603 struct arch_timer_cpu *timer = vcpu_timer(vcpu);
604 struct timer_map map;
606 if (unlikely(!timer->enabled))
609 get_timer_map(vcpu, &map);
611 if (static_branch_likely(&has_gic_active_state)) {
612 kvm_timer_vcpu_load_gic(map.direct_vtimer);
613 if (map.direct_ptimer)
614 kvm_timer_vcpu_load_gic(map.direct_ptimer);
616 kvm_timer_vcpu_load_nogic(vcpu);
619 set_cntvoff(timer_get_offset(map.direct_vtimer));
621 kvm_timer_unblocking(vcpu);
623 timer_restore_state(map.direct_vtimer);
624 if (map.direct_ptimer)
625 timer_restore_state(map.direct_ptimer);
628 timer_emulate(map.emul_ptimer);
631 bool kvm_timer_should_notify_user(struct kvm_vcpu *vcpu)
633 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
634 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
635 struct kvm_sync_regs *sregs = &vcpu->run->s.regs;
638 if (likely(irqchip_in_kernel(vcpu->kvm)))
641 vlevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_VTIMER;
642 plevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_PTIMER;
644 return kvm_timer_should_fire(vtimer) != vlevel ||
645 kvm_timer_should_fire(ptimer) != plevel;
648 void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
650 struct arch_timer_cpu *timer = vcpu_timer(vcpu);
651 struct timer_map map;
652 struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
654 if (unlikely(!timer->enabled))
657 get_timer_map(vcpu, &map);
659 timer_save_state(map.direct_vtimer);
660 if (map.direct_ptimer)
661 timer_save_state(map.direct_ptimer);
664 * Cancel soft timer emulation, because the only case where we
665 * need it after a vcpu_put is in the context of a sleeping VCPU, and
666 * in that case we already factor in the deadline for the physical
667 * timer when scheduling the bg_timer.
669 * In any case, we re-schedule the hrtimer for the physical timer when
670 * coming back to the VCPU thread in kvm_timer_vcpu_load().
673 soft_timer_cancel(&map.emul_ptimer->hrtimer);
675 if (rcuwait_active(wait))
676 kvm_timer_blocking(vcpu);
679 * The kernel may decide to run userspace after calling vcpu_put, so
680 * we reset cntvoff to 0 to ensure a consistent read between user
681 * accesses to the virtual counter and kernel access to the physical
682 * counter of non-VHE case. For VHE, the virtual counter uses a fixed
683 * virtual offset of zero, so no need to zero CNTVOFF_EL2 register.
689 * With a userspace irqchip we have to check if the guest de-asserted the
690 * timer and if so, unmask the timer irq signal on the host interrupt
691 * controller to ensure that we see future timer signals.
693 static void unmask_vtimer_irq_user(struct kvm_vcpu *vcpu)
695 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
697 if (!kvm_timer_should_fire(vtimer)) {
698 kvm_timer_update_irq(vcpu, false, vtimer);
699 if (static_branch_likely(&has_gic_active_state))
700 set_timer_irq_phys_active(vtimer, false);
702 enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
706 void kvm_timer_sync_user(struct kvm_vcpu *vcpu)
708 struct arch_timer_cpu *timer = vcpu_timer(vcpu);
710 if (unlikely(!timer->enabled))
713 if (unlikely(!irqchip_in_kernel(vcpu->kvm)))
714 unmask_vtimer_irq_user(vcpu);
717 int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu)
719 struct arch_timer_cpu *timer = vcpu_timer(vcpu);
720 struct timer_map map;
722 get_timer_map(vcpu, &map);
725 * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8
726 * and to 0 for ARMv7. We provide an implementation that always
727 * resets the timer to be disabled and unmasked and is compliant with
728 * the ARMv7 architecture.
730 timer_set_ctl(vcpu_vtimer(vcpu), 0);
731 timer_set_ctl(vcpu_ptimer(vcpu), 0);
733 if (timer->enabled) {
734 kvm_timer_update_irq(vcpu, false, vcpu_vtimer(vcpu));
735 kvm_timer_update_irq(vcpu, false, vcpu_ptimer(vcpu));
737 if (irqchip_in_kernel(vcpu->kvm)) {
738 kvm_vgic_reset_mapped_irq(vcpu, map.direct_vtimer->irq.irq);
739 if (map.direct_ptimer)
740 kvm_vgic_reset_mapped_irq(vcpu, map.direct_ptimer->irq.irq);
745 soft_timer_cancel(&map.emul_ptimer->hrtimer);
750 /* Make the updates of cntvoff for all vtimer contexts atomic */
751 static void update_vtimer_cntvoff(struct kvm_vcpu *vcpu, u64 cntvoff)
754 struct kvm *kvm = vcpu->kvm;
755 struct kvm_vcpu *tmp;
757 mutex_lock(&kvm->lock);
758 kvm_for_each_vcpu(i, tmp, kvm)
759 timer_set_offset(vcpu_vtimer(tmp), cntvoff);
762 * When called from the vcpu create path, the CPU being created is not
763 * included in the loop above, so we just set it here as well.
765 timer_set_offset(vcpu_vtimer(vcpu), cntvoff);
766 mutex_unlock(&kvm->lock);
769 void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
771 struct arch_timer_cpu *timer = vcpu_timer(vcpu);
772 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
773 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
778 /* Synchronize cntvoff across all vtimers of a VM. */
779 update_vtimer_cntvoff(vcpu, kvm_phys_timer_read());
780 timer_set_offset(ptimer, 0);
782 hrtimer_init(&timer->bg_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
783 timer->bg_timer.function = kvm_bg_timer_expire;
785 hrtimer_init(&vtimer->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
786 hrtimer_init(&ptimer->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
787 vtimer->hrtimer.function = kvm_hrtimer_expire;
788 ptimer->hrtimer.function = kvm_hrtimer_expire;
790 vtimer->irq.irq = default_vtimer_irq.irq;
791 ptimer->irq.irq = default_ptimer_irq.irq;
793 vtimer->host_timer_irq = host_vtimer_irq;
794 ptimer->host_timer_irq = host_ptimer_irq;
796 vtimer->host_timer_irq_flags = host_vtimer_irq_flags;
797 ptimer->host_timer_irq_flags = host_ptimer_irq_flags;
800 static void kvm_timer_init_interrupt(void *info)
802 enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
803 enable_percpu_irq(host_ptimer_irq, host_ptimer_irq_flags);
806 int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
808 struct arch_timer_context *timer;
811 case KVM_REG_ARM_TIMER_CTL:
812 timer = vcpu_vtimer(vcpu);
813 kvm_arm_timer_write(vcpu, timer, TIMER_REG_CTL, value);
815 case KVM_REG_ARM_TIMER_CNT:
816 timer = vcpu_vtimer(vcpu);
817 update_vtimer_cntvoff(vcpu, kvm_phys_timer_read() - value);
819 case KVM_REG_ARM_TIMER_CVAL:
820 timer = vcpu_vtimer(vcpu);
821 kvm_arm_timer_write(vcpu, timer, TIMER_REG_CVAL, value);
823 case KVM_REG_ARM_PTIMER_CTL:
824 timer = vcpu_ptimer(vcpu);
825 kvm_arm_timer_write(vcpu, timer, TIMER_REG_CTL, value);
827 case KVM_REG_ARM_PTIMER_CVAL:
828 timer = vcpu_ptimer(vcpu);
829 kvm_arm_timer_write(vcpu, timer, TIMER_REG_CVAL, value);
839 static u64 read_timer_ctl(struct arch_timer_context *timer)
842 * Set ISTATUS bit if it's expired.
843 * Note that according to ARMv8 ARM Issue A.k, ISTATUS bit is
844 * UNKNOWN when ENABLE bit is 0, so we chose to set ISTATUS bit
845 * regardless of ENABLE bit for our implementation convenience.
847 u32 ctl = timer_get_ctl(timer);
849 if (!kvm_timer_compute_delta(timer))
850 ctl |= ARCH_TIMER_CTRL_IT_STAT;
855 u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid)
858 case KVM_REG_ARM_TIMER_CTL:
859 return kvm_arm_timer_read(vcpu,
860 vcpu_vtimer(vcpu), TIMER_REG_CTL);
861 case KVM_REG_ARM_TIMER_CNT:
862 return kvm_arm_timer_read(vcpu,
863 vcpu_vtimer(vcpu), TIMER_REG_CNT);
864 case KVM_REG_ARM_TIMER_CVAL:
865 return kvm_arm_timer_read(vcpu,
866 vcpu_vtimer(vcpu), TIMER_REG_CVAL);
867 case KVM_REG_ARM_PTIMER_CTL:
868 return kvm_arm_timer_read(vcpu,
869 vcpu_ptimer(vcpu), TIMER_REG_CTL);
870 case KVM_REG_ARM_PTIMER_CNT:
871 return kvm_arm_timer_read(vcpu,
872 vcpu_ptimer(vcpu), TIMER_REG_CNT);
873 case KVM_REG_ARM_PTIMER_CVAL:
874 return kvm_arm_timer_read(vcpu,
875 vcpu_ptimer(vcpu), TIMER_REG_CVAL);
880 static u64 kvm_arm_timer_read(struct kvm_vcpu *vcpu,
881 struct arch_timer_context *timer,
882 enum kvm_arch_timer_regs treg)
888 val = timer_get_cval(timer) - kvm_phys_timer_read() + timer_get_offset(timer);
889 val = lower_32_bits(val);
893 val = read_timer_ctl(timer);
897 val = timer_get_cval(timer);
901 val = kvm_phys_timer_read() - timer_get_offset(timer);
911 u64 kvm_arm_timer_read_sysreg(struct kvm_vcpu *vcpu,
912 enum kvm_arch_timers tmr,
913 enum kvm_arch_timer_regs treg)
918 kvm_timer_vcpu_put(vcpu);
920 val = kvm_arm_timer_read(vcpu, vcpu_get_timer(vcpu, tmr), treg);
922 kvm_timer_vcpu_load(vcpu);
928 static void kvm_arm_timer_write(struct kvm_vcpu *vcpu,
929 struct arch_timer_context *timer,
930 enum kvm_arch_timer_regs treg,
935 timer_set_cval(timer, kvm_phys_timer_read() - timer_get_offset(timer) + (s32)val);
939 timer_set_ctl(timer, val & ~ARCH_TIMER_CTRL_IT_STAT);
943 timer_set_cval(timer, val);
951 void kvm_arm_timer_write_sysreg(struct kvm_vcpu *vcpu,
952 enum kvm_arch_timers tmr,
953 enum kvm_arch_timer_regs treg,
957 kvm_timer_vcpu_put(vcpu);
959 kvm_arm_timer_write(vcpu, vcpu_get_timer(vcpu, tmr), treg, val);
961 kvm_timer_vcpu_load(vcpu);
965 static int kvm_timer_starting_cpu(unsigned int cpu)
967 kvm_timer_init_interrupt(NULL);
971 static int kvm_timer_dying_cpu(unsigned int cpu)
973 disable_percpu_irq(host_vtimer_irq);
977 static int timer_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu)
980 irqd_set_forwarded_to_vcpu(d);
982 irqd_clr_forwarded_to_vcpu(d);
987 static int timer_irq_set_irqchip_state(struct irq_data *d,
988 enum irqchip_irq_state which, bool val)
990 if (which != IRQCHIP_STATE_ACTIVE || !irqd_is_forwarded_to_vcpu(d))
991 return irq_chip_set_parent_state(d, which, val);
994 irq_chip_mask_parent(d);
996 irq_chip_unmask_parent(d);
1001 static void timer_irq_eoi(struct irq_data *d)
1003 if (!irqd_is_forwarded_to_vcpu(d))
1004 irq_chip_eoi_parent(d);
1007 static void timer_irq_ack(struct irq_data *d)
1010 if (d->chip->irq_ack)
1011 d->chip->irq_ack(d);
1014 static struct irq_chip timer_chip = {
1016 .irq_ack = timer_irq_ack,
1017 .irq_mask = irq_chip_mask_parent,
1018 .irq_unmask = irq_chip_unmask_parent,
1019 .irq_eoi = timer_irq_eoi,
1020 .irq_set_type = irq_chip_set_type_parent,
1021 .irq_set_vcpu_affinity = timer_irq_set_vcpu_affinity,
1022 .irq_set_irqchip_state = timer_irq_set_irqchip_state,
1025 static int timer_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
1026 unsigned int nr_irqs, void *arg)
1028 irq_hw_number_t hwirq = (uintptr_t)arg;
1030 return irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
1034 static void timer_irq_domain_free(struct irq_domain *domain, unsigned int virq,
1035 unsigned int nr_irqs)
1039 static const struct irq_domain_ops timer_domain_ops = {
1040 .alloc = timer_irq_domain_alloc,
1041 .free = timer_irq_domain_free,
1044 static struct irq_ops arch_timer_irq_ops = {
1045 .get_input_level = kvm_arch_timer_get_input_level,
1048 static void kvm_irq_fixup_flags(unsigned int virq, u32 *flags)
1050 *flags = irq_get_trigger_type(virq);
1051 if (*flags != IRQF_TRIGGER_HIGH && *flags != IRQF_TRIGGER_LOW) {
1052 kvm_err("Invalid trigger for timer IRQ%d, assuming level low\n",
1054 *flags = IRQF_TRIGGER_LOW;
1058 static int kvm_irq_init(struct arch_timer_kvm_info *info)
1060 struct irq_domain *domain = NULL;
1062 if (info->virtual_irq <= 0) {
1063 kvm_err("kvm_arch_timer: invalid virtual timer IRQ: %d\n",
1068 host_vtimer_irq = info->virtual_irq;
1069 kvm_irq_fixup_flags(host_vtimer_irq, &host_vtimer_irq_flags);
1071 if (kvm_vgic_global_state.no_hw_deactivation) {
1072 struct fwnode_handle *fwnode;
1073 struct irq_data *data;
1075 fwnode = irq_domain_alloc_named_fwnode("kvm-timer");
1079 /* Assume both vtimer and ptimer in the same parent */
1080 data = irq_get_irq_data(host_vtimer_irq);
1081 domain = irq_domain_create_hierarchy(data->domain, 0,
1082 NR_KVM_TIMERS, fwnode,
1083 &timer_domain_ops, NULL);
1085 irq_domain_free_fwnode(fwnode);
1089 arch_timer_irq_ops.flags |= VGIC_IRQ_SW_RESAMPLE;
1090 WARN_ON(irq_domain_push_irq(domain, host_vtimer_irq,
1091 (void *)TIMER_VTIMER));
1094 if (info->physical_irq > 0) {
1095 host_ptimer_irq = info->physical_irq;
1096 kvm_irq_fixup_flags(host_ptimer_irq, &host_ptimer_irq_flags);
1099 WARN_ON(irq_domain_push_irq(domain, host_ptimer_irq,
1100 (void *)TIMER_PTIMER));
1106 int kvm_timer_hyp_init(bool has_gic)
1108 struct arch_timer_kvm_info *info;
1111 info = arch_timer_get_kvm_info();
1112 timecounter = &info->timecounter;
1114 if (!timecounter->cc) {
1115 kvm_err("kvm_arch_timer: uninitialized timecounter\n");
1119 err = kvm_irq_init(info);
1123 /* First, do the virtual EL1 timer irq */
1125 err = request_percpu_irq(host_vtimer_irq, kvm_arch_timer_handler,
1126 "kvm guest vtimer", kvm_get_running_vcpus());
1128 kvm_err("kvm_arch_timer: can't request vtimer interrupt %d (%d)\n",
1129 host_vtimer_irq, err);
1134 err = irq_set_vcpu_affinity(host_vtimer_irq,
1135 kvm_get_running_vcpus());
1137 kvm_err("kvm_arch_timer: error setting vcpu affinity\n");
1141 static_branch_enable(&has_gic_active_state);
1144 kvm_debug("virtual timer IRQ%d\n", host_vtimer_irq);
1146 /* Now let's do the physical EL1 timer irq */
1148 if (info->physical_irq > 0) {
1149 err = request_percpu_irq(host_ptimer_irq, kvm_arch_timer_handler,
1150 "kvm guest ptimer", kvm_get_running_vcpus());
1152 kvm_err("kvm_arch_timer: can't request ptimer interrupt %d (%d)\n",
1153 host_ptimer_irq, err);
1158 err = irq_set_vcpu_affinity(host_ptimer_irq,
1159 kvm_get_running_vcpus());
1161 kvm_err("kvm_arch_timer: error setting vcpu affinity\n");
1166 kvm_debug("physical timer IRQ%d\n", host_ptimer_irq);
1167 } else if (has_vhe()) {
1168 kvm_err("kvm_arch_timer: invalid physical timer IRQ: %d\n",
1169 info->physical_irq);
1174 cpuhp_setup_state(CPUHP_AP_KVM_ARM_TIMER_STARTING,
1175 "kvm/arm/timer:starting", kvm_timer_starting_cpu,
1176 kvm_timer_dying_cpu);
1179 free_percpu_irq(host_vtimer_irq, kvm_get_running_vcpus());
1183 void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
1185 struct arch_timer_cpu *timer = vcpu_timer(vcpu);
1187 soft_timer_cancel(&timer->bg_timer);
1190 static bool timer_irqs_are_valid(struct kvm_vcpu *vcpu)
1192 int vtimer_irq, ptimer_irq;
1195 vtimer_irq = vcpu_vtimer(vcpu)->irq.irq;
1196 ret = kvm_vgic_set_owner(vcpu, vtimer_irq, vcpu_vtimer(vcpu));
1200 ptimer_irq = vcpu_ptimer(vcpu)->irq.irq;
1201 ret = kvm_vgic_set_owner(vcpu, ptimer_irq, vcpu_ptimer(vcpu));
1205 kvm_for_each_vcpu(i, vcpu, vcpu->kvm) {
1206 if (vcpu_vtimer(vcpu)->irq.irq != vtimer_irq ||
1207 vcpu_ptimer(vcpu)->irq.irq != ptimer_irq)
1214 bool kvm_arch_timer_get_input_level(int vintid)
1216 struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
1217 struct arch_timer_context *timer;
1219 if (vintid == vcpu_vtimer(vcpu)->irq.irq)
1220 timer = vcpu_vtimer(vcpu);
1221 else if (vintid == vcpu_ptimer(vcpu)->irq.irq)
1222 timer = vcpu_ptimer(vcpu);
1226 return kvm_timer_should_fire(timer);
1229 int kvm_timer_enable(struct kvm_vcpu *vcpu)
1231 struct arch_timer_cpu *timer = vcpu_timer(vcpu);
1232 struct timer_map map;
1238 /* Without a VGIC we do not map virtual IRQs to physical IRQs */
1239 if (!irqchip_in_kernel(vcpu->kvm))
1243 * At this stage, we have the guarantee that the vgic is both
1244 * available and initialized.
1246 if (!timer_irqs_are_valid(vcpu)) {
1247 kvm_debug("incorrectly configured timer irqs\n");
1251 get_timer_map(vcpu, &map);
1253 ret = kvm_vgic_map_phys_irq(vcpu,
1254 map.direct_vtimer->host_timer_irq,
1255 map.direct_vtimer->irq.irq,
1256 &arch_timer_irq_ops);
1260 if (map.direct_ptimer) {
1261 ret = kvm_vgic_map_phys_irq(vcpu,
1262 map.direct_ptimer->host_timer_irq,
1263 map.direct_ptimer->irq.irq,
1264 &arch_timer_irq_ops);
1276 * On VHE system, we only need to configure the EL2 timer trap register once,
1277 * not for every world switch.
1278 * The host kernel runs at EL2 with HCR_EL2.TGE == 1,
1279 * and this makes those bits have no effect for the host kernel execution.
1281 void kvm_timer_init_vhe(void)
1283 /* When HCR_EL2.E2H ==1, EL1PCEN and EL1PCTEN are shifted by 10 */
1284 u32 cnthctl_shift = 10;
1288 * VHE systems allow the guest direct access to the EL1 physical
1291 val = read_sysreg(cnthctl_el2);
1292 val |= (CNTHCTL_EL1PCEN << cnthctl_shift);
1293 val |= (CNTHCTL_EL1PCTEN << cnthctl_shift);
1294 write_sysreg(val, cnthctl_el2);
1297 static void set_timer_irqs(struct kvm *kvm, int vtimer_irq, int ptimer_irq)
1299 struct kvm_vcpu *vcpu;
1302 kvm_for_each_vcpu(i, vcpu, kvm) {
1303 vcpu_vtimer(vcpu)->irq.irq = vtimer_irq;
1304 vcpu_ptimer(vcpu)->irq.irq = ptimer_irq;
1308 int kvm_arm_timer_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
1310 int __user *uaddr = (int __user *)(long)attr->addr;
1311 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
1312 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
1315 if (!irqchip_in_kernel(vcpu->kvm))
1318 if (get_user(irq, uaddr))
1321 if (!(irq_is_ppi(irq)))
1324 if (vcpu->arch.timer_cpu.enabled)
1327 switch (attr->attr) {
1328 case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
1329 set_timer_irqs(vcpu->kvm, irq, ptimer->irq.irq);
1331 case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
1332 set_timer_irqs(vcpu->kvm, vtimer->irq.irq, irq);
1341 int kvm_arm_timer_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
1343 int __user *uaddr = (int __user *)(long)attr->addr;
1344 struct arch_timer_context *timer;
1347 switch (attr->attr) {
1348 case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
1349 timer = vcpu_vtimer(vcpu);
1351 case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
1352 timer = vcpu_ptimer(vcpu);
1358 irq = timer->irq.irq;
1359 return put_user(irq, uaddr);
1362 int kvm_arm_timer_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
1364 switch (attr->attr) {
1365 case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
1366 case KVM_ARM_VCPU_TIMER_IRQ_PTIMER: