xtensa: support DMA buffers in high memory
[cris-mirror.git] / virt / kvm / arm / arch_timer.c
blob70268c0bec799c0ce85c2f27267e0ab514c5f852
1 /*
2 * Copyright (C) 2012 ARM Ltd.
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/cpu.h>
20 #include <linux/kvm.h>
21 #include <linux/kvm_host.h>
22 #include <linux/interrupt.h>
23 #include <linux/irq.h>
24 #include <linux/uaccess.h>
26 #include <clocksource/arm_arch_timer.h>
27 #include <asm/arch_timer.h>
28 #include <asm/kvm_hyp.h>
30 #include <kvm/arm_vgic.h>
31 #include <kvm/arm_arch_timer.h>
33 #include "trace.h"
35 static struct timecounter *timecounter;
36 static unsigned int host_vtimer_irq;
37 static u32 host_vtimer_irq_flags;
39 static const struct kvm_irq_level default_ptimer_irq = {
40 .irq = 30,
41 .level = 1,
44 static const struct kvm_irq_level default_vtimer_irq = {
45 .irq = 27,
46 .level = 1,
49 static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx);
50 static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
51 struct arch_timer_context *timer_ctx);
52 static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx);
54 u64 kvm_phys_timer_read(void)
56 return timecounter->cc->read(timecounter->cc);
59 static void soft_timer_start(struct hrtimer *hrt, u64 ns)
61 hrtimer_start(hrt, ktime_add_ns(ktime_get(), ns),
62 HRTIMER_MODE_ABS);
65 static void soft_timer_cancel(struct hrtimer *hrt, struct work_struct *work)
67 hrtimer_cancel(hrt);
68 if (work)
69 cancel_work_sync(work);
72 static void kvm_vtimer_update_mask_user(struct kvm_vcpu *vcpu)
74 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
77 * When using a userspace irqchip with the architected timers, we must
78 * prevent continuously exiting from the guest, and therefore mask the
79 * physical interrupt by disabling it on the host interrupt controller
80 * when the virtual level is high, such that the guest can make
81 * forward progress. Once we detect the output level being
82 * de-asserted, we unmask the interrupt again so that we exit from the
83 * guest when the timer fires.
85 if (vtimer->irq.level)
86 disable_percpu_irq(host_vtimer_irq);
87 else
88 enable_percpu_irq(host_vtimer_irq, 0);
91 static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
93 struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
94 struct arch_timer_context *vtimer;
97 * We may see a timer interrupt after vcpu_put() has been called which
98 * sets the CPU's vcpu pointer to NULL, because even though the timer
99 * has been disabled in vtimer_save_state(), the hardware interrupt
100 * signal may not have been retired from the interrupt controller yet.
102 if (!vcpu)
103 return IRQ_HANDLED;
105 vtimer = vcpu_vtimer(vcpu);
106 if (kvm_timer_should_fire(vtimer))
107 kvm_timer_update_irq(vcpu, true, vtimer);
109 if (static_branch_unlikely(&userspace_irqchip_in_use) &&
110 unlikely(!irqchip_in_kernel(vcpu->kvm)))
111 kvm_vtimer_update_mask_user(vcpu);
113 return IRQ_HANDLED;
117 * Work function for handling the backup timer that we schedule when a vcpu is
118 * no longer running, but had a timer programmed to fire in the future.
120 static void kvm_timer_inject_irq_work(struct work_struct *work)
122 struct kvm_vcpu *vcpu;
124 vcpu = container_of(work, struct kvm_vcpu, arch.timer_cpu.expired);
127 * If the vcpu is blocked we want to wake it up so that it will see
128 * the timer has expired when entering the guest.
130 kvm_vcpu_wake_up(vcpu);
133 static u64 kvm_timer_compute_delta(struct arch_timer_context *timer_ctx)
135 u64 cval, now;
137 cval = timer_ctx->cnt_cval;
138 now = kvm_phys_timer_read() - timer_ctx->cntvoff;
140 if (now < cval) {
141 u64 ns;
143 ns = cyclecounter_cyc2ns(timecounter->cc,
144 cval - now,
145 timecounter->mask,
146 &timecounter->frac);
147 return ns;
150 return 0;
153 static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx)
155 return !(timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_IT_MASK) &&
156 (timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_ENABLE);
160 * Returns the earliest expiration time in ns among guest timers.
161 * Note that it will return 0 if none of timers can fire.
163 static u64 kvm_timer_earliest_exp(struct kvm_vcpu *vcpu)
165 u64 min_virt = ULLONG_MAX, min_phys = ULLONG_MAX;
166 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
167 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
169 if (kvm_timer_irq_can_fire(vtimer))
170 min_virt = kvm_timer_compute_delta(vtimer);
172 if (kvm_timer_irq_can_fire(ptimer))
173 min_phys = kvm_timer_compute_delta(ptimer);
175 /* If none of timers can fire, then return 0 */
176 if ((min_virt == ULLONG_MAX) && (min_phys == ULLONG_MAX))
177 return 0;
179 return min(min_virt, min_phys);
182 static enum hrtimer_restart kvm_bg_timer_expire(struct hrtimer *hrt)
184 struct arch_timer_cpu *timer;
185 struct kvm_vcpu *vcpu;
186 u64 ns;
188 timer = container_of(hrt, struct arch_timer_cpu, bg_timer);
189 vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
192 * Check that the timer has really expired from the guest's
193 * PoV (NTP on the host may have forced it to expire
194 * early). If we should have slept longer, restart it.
196 ns = kvm_timer_earliest_exp(vcpu);
197 if (unlikely(ns)) {
198 hrtimer_forward_now(hrt, ns_to_ktime(ns));
199 return HRTIMER_RESTART;
202 schedule_work(&timer->expired);
203 return HRTIMER_NORESTART;
206 static enum hrtimer_restart kvm_phys_timer_expire(struct hrtimer *hrt)
208 struct arch_timer_context *ptimer;
209 struct arch_timer_cpu *timer;
210 struct kvm_vcpu *vcpu;
211 u64 ns;
213 timer = container_of(hrt, struct arch_timer_cpu, phys_timer);
214 vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
215 ptimer = vcpu_ptimer(vcpu);
218 * Check that the timer has really expired from the guest's
219 * PoV (NTP on the host may have forced it to expire
220 * early). If not ready, schedule for a later time.
222 ns = kvm_timer_compute_delta(ptimer);
223 if (unlikely(ns)) {
224 hrtimer_forward_now(hrt, ns_to_ktime(ns));
225 return HRTIMER_RESTART;
228 kvm_timer_update_irq(vcpu, true, ptimer);
229 return HRTIMER_NORESTART;
232 static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx)
234 u64 cval, now;
236 if (timer_ctx->loaded) {
237 u32 cnt_ctl;
239 /* Only the virtual timer can be loaded so far */
240 cnt_ctl = read_sysreg_el0(cntv_ctl);
241 return (cnt_ctl & ARCH_TIMER_CTRL_ENABLE) &&
242 (cnt_ctl & ARCH_TIMER_CTRL_IT_STAT) &&
243 !(cnt_ctl & ARCH_TIMER_CTRL_IT_MASK);
246 if (!kvm_timer_irq_can_fire(timer_ctx))
247 return false;
249 cval = timer_ctx->cnt_cval;
250 now = kvm_phys_timer_read() - timer_ctx->cntvoff;
252 return cval <= now;
255 bool kvm_timer_is_pending(struct kvm_vcpu *vcpu)
257 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
258 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
260 if (kvm_timer_should_fire(vtimer))
261 return true;
263 return kvm_timer_should_fire(ptimer);
267 * Reflect the timer output level into the kvm_run structure
269 void kvm_timer_update_run(struct kvm_vcpu *vcpu)
271 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
272 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
273 struct kvm_sync_regs *regs = &vcpu->run->s.regs;
275 /* Populate the device bitmap with the timer states */
276 regs->device_irq_level &= ~(KVM_ARM_DEV_EL1_VTIMER |
277 KVM_ARM_DEV_EL1_PTIMER);
278 if (kvm_timer_should_fire(vtimer))
279 regs->device_irq_level |= KVM_ARM_DEV_EL1_VTIMER;
280 if (kvm_timer_should_fire(ptimer))
281 regs->device_irq_level |= KVM_ARM_DEV_EL1_PTIMER;
284 static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
285 struct arch_timer_context *timer_ctx)
287 int ret;
289 timer_ctx->irq.level = new_level;
290 trace_kvm_timer_update_irq(vcpu->vcpu_id, timer_ctx->irq.irq,
291 timer_ctx->irq.level);
293 if (!static_branch_unlikely(&userspace_irqchip_in_use) ||
294 likely(irqchip_in_kernel(vcpu->kvm))) {
295 ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
296 timer_ctx->irq.irq,
297 timer_ctx->irq.level,
298 timer_ctx);
299 WARN_ON(ret);
303 /* Schedule the background timer for the emulated timer. */
304 static void phys_timer_emulate(struct kvm_vcpu *vcpu)
306 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
307 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
310 * If the timer can fire now we have just raised the IRQ line and we
311 * don't need to have a soft timer scheduled for the future. If the
312 * timer cannot fire at all, then we also don't need a soft timer.
314 if (kvm_timer_should_fire(ptimer) || !kvm_timer_irq_can_fire(ptimer)) {
315 soft_timer_cancel(&timer->phys_timer, NULL);
316 return;
319 soft_timer_start(&timer->phys_timer, kvm_timer_compute_delta(ptimer));
323 * Check if there was a change in the timer state, so that we should either
324 * raise or lower the line level to the GIC or schedule a background timer to
325 * emulate the physical timer.
327 static void kvm_timer_update_state(struct kvm_vcpu *vcpu)
329 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
330 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
331 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
332 bool level;
334 if (unlikely(!timer->enabled))
335 return;
338 * The vtimer virtual interrupt is a 'mapped' interrupt, meaning part
339 * of its lifecycle is offloaded to the hardware, and we therefore may
340 * not have lowered the irq.level value before having to signal a new
341 * interrupt, but have to signal an interrupt every time the level is
342 * asserted.
344 level = kvm_timer_should_fire(vtimer);
345 kvm_timer_update_irq(vcpu, level, vtimer);
347 if (kvm_timer_should_fire(ptimer) != ptimer->irq.level)
348 kvm_timer_update_irq(vcpu, !ptimer->irq.level, ptimer);
350 phys_timer_emulate(vcpu);
353 static void __timer_snapshot_state(struct arch_timer_context *timer)
355 timer->cnt_ctl = read_sysreg_el0(cntv_ctl);
356 timer->cnt_cval = read_sysreg_el0(cntv_cval);
359 static void vtimer_save_state(struct kvm_vcpu *vcpu)
361 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
362 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
363 unsigned long flags;
365 local_irq_save(flags);
367 if (!vtimer->loaded)
368 goto out;
370 if (timer->enabled)
371 __timer_snapshot_state(vtimer);
373 /* Disable the virtual timer */
374 write_sysreg_el0(0, cntv_ctl);
375 isb();
377 vtimer->loaded = false;
378 out:
379 local_irq_restore(flags);
383 * Schedule the background timer before calling kvm_vcpu_block, so that this
384 * thread is removed from its waitqueue and made runnable when there's a timer
385 * interrupt to handle.
387 void kvm_timer_schedule(struct kvm_vcpu *vcpu)
389 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
390 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
391 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
393 vtimer_save_state(vcpu);
396 * No need to schedule a background timer if any guest timer has
397 * already expired, because kvm_vcpu_block will return before putting
398 * the thread to sleep.
400 if (kvm_timer_should_fire(vtimer) || kvm_timer_should_fire(ptimer))
401 return;
404 * If both timers are not capable of raising interrupts (disabled or
405 * masked), then there's no more work for us to do.
407 if (!kvm_timer_irq_can_fire(vtimer) && !kvm_timer_irq_can_fire(ptimer))
408 return;
411 * The guest timers have not yet expired, schedule a background timer.
412 * Set the earliest expiration time among the guest timers.
414 soft_timer_start(&timer->bg_timer, kvm_timer_earliest_exp(vcpu));
417 static void vtimer_restore_state(struct kvm_vcpu *vcpu)
419 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
420 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
421 unsigned long flags;
423 local_irq_save(flags);
425 if (vtimer->loaded)
426 goto out;
428 if (timer->enabled) {
429 write_sysreg_el0(vtimer->cnt_cval, cntv_cval);
430 isb();
431 write_sysreg_el0(vtimer->cnt_ctl, cntv_ctl);
434 vtimer->loaded = true;
435 out:
436 local_irq_restore(flags);
439 void kvm_timer_unschedule(struct kvm_vcpu *vcpu)
441 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
443 vtimer_restore_state(vcpu);
445 soft_timer_cancel(&timer->bg_timer, &timer->expired);
448 static void set_cntvoff(u64 cntvoff)
450 u32 low = lower_32_bits(cntvoff);
451 u32 high = upper_32_bits(cntvoff);
454 * Since kvm_call_hyp doesn't fully support the ARM PCS especially on
455 * 32-bit systems, but rather passes register by register shifted one
456 * place (we put the function address in r0/x0), we cannot simply pass
457 * a 64-bit value as an argument, but have to split the value in two
458 * 32-bit halves.
460 kvm_call_hyp(__kvm_timer_set_cntvoff, low, high);
463 static void kvm_timer_vcpu_load_vgic(struct kvm_vcpu *vcpu)
465 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
466 bool phys_active;
467 int ret;
469 phys_active = kvm_vgic_map_is_active(vcpu, vtimer->irq.irq);
471 ret = irq_set_irqchip_state(host_vtimer_irq,
472 IRQCHIP_STATE_ACTIVE,
473 phys_active);
474 WARN_ON(ret);
477 static void kvm_timer_vcpu_load_user(struct kvm_vcpu *vcpu)
479 kvm_vtimer_update_mask_user(vcpu);
482 void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu)
484 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
485 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
487 if (unlikely(!timer->enabled))
488 return;
490 if (unlikely(!irqchip_in_kernel(vcpu->kvm)))
491 kvm_timer_vcpu_load_user(vcpu);
492 else
493 kvm_timer_vcpu_load_vgic(vcpu);
495 set_cntvoff(vtimer->cntvoff);
497 vtimer_restore_state(vcpu);
499 /* Set the background timer for the physical timer emulation. */
500 phys_timer_emulate(vcpu);
503 bool kvm_timer_should_notify_user(struct kvm_vcpu *vcpu)
505 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
506 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
507 struct kvm_sync_regs *sregs = &vcpu->run->s.regs;
508 bool vlevel, plevel;
510 if (likely(irqchip_in_kernel(vcpu->kvm)))
511 return false;
513 vlevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_VTIMER;
514 plevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_PTIMER;
516 return kvm_timer_should_fire(vtimer) != vlevel ||
517 kvm_timer_should_fire(ptimer) != plevel;
520 void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
522 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
524 if (unlikely(!timer->enabled))
525 return;
527 vtimer_save_state(vcpu);
530 * Cancel the physical timer emulation, because the only case where we
531 * need it after a vcpu_put is in the context of a sleeping VCPU, and
532 * in that case we already factor in the deadline for the physical
533 * timer when scheduling the bg_timer.
535 * In any case, we re-schedule the hrtimer for the physical timer when
536 * coming back to the VCPU thread in kvm_timer_vcpu_load().
538 soft_timer_cancel(&timer->phys_timer, NULL);
541 * The kernel may decide to run userspace after calling vcpu_put, so
542 * we reset cntvoff to 0 to ensure a consistent read between user
543 * accesses to the virtual counter and kernel access to the physical
544 * counter.
546 set_cntvoff(0);
550 * With a userspace irqchip we have to check if the guest de-asserted the
551 * timer and if so, unmask the timer irq signal on the host interrupt
552 * controller to ensure that we see future timer signals.
554 static void unmask_vtimer_irq_user(struct kvm_vcpu *vcpu)
556 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
558 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
559 __timer_snapshot_state(vtimer);
560 if (!kvm_timer_should_fire(vtimer)) {
561 kvm_timer_update_irq(vcpu, false, vtimer);
562 kvm_vtimer_update_mask_user(vcpu);
567 void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
569 unmask_vtimer_irq_user(vcpu);
572 int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu)
574 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
575 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
578 * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8
579 * and to 0 for ARMv7. We provide an implementation that always
580 * resets the timer to be disabled and unmasked and is compliant with
581 * the ARMv7 architecture.
583 vtimer->cnt_ctl = 0;
584 ptimer->cnt_ctl = 0;
585 kvm_timer_update_state(vcpu);
587 return 0;
590 /* Make the updates of cntvoff for all vtimer contexts atomic */
591 static void update_vtimer_cntvoff(struct kvm_vcpu *vcpu, u64 cntvoff)
593 int i;
594 struct kvm *kvm = vcpu->kvm;
595 struct kvm_vcpu *tmp;
597 mutex_lock(&kvm->lock);
598 kvm_for_each_vcpu(i, tmp, kvm)
599 vcpu_vtimer(tmp)->cntvoff = cntvoff;
602 * When called from the vcpu create path, the CPU being created is not
603 * included in the loop above, so we just set it here as well.
605 vcpu_vtimer(vcpu)->cntvoff = cntvoff;
606 mutex_unlock(&kvm->lock);
609 void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
611 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
612 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
613 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
615 /* Synchronize cntvoff across all vtimers of a VM. */
616 update_vtimer_cntvoff(vcpu, kvm_phys_timer_read());
617 vcpu_ptimer(vcpu)->cntvoff = 0;
619 INIT_WORK(&timer->expired, kvm_timer_inject_irq_work);
620 hrtimer_init(&timer->bg_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
621 timer->bg_timer.function = kvm_bg_timer_expire;
623 hrtimer_init(&timer->phys_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
624 timer->phys_timer.function = kvm_phys_timer_expire;
626 vtimer->irq.irq = default_vtimer_irq.irq;
627 ptimer->irq.irq = default_ptimer_irq.irq;
630 static void kvm_timer_init_interrupt(void *info)
632 enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
635 int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
637 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
638 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
640 switch (regid) {
641 case KVM_REG_ARM_TIMER_CTL:
642 vtimer->cnt_ctl = value & ~ARCH_TIMER_CTRL_IT_STAT;
643 break;
644 case KVM_REG_ARM_TIMER_CNT:
645 update_vtimer_cntvoff(vcpu, kvm_phys_timer_read() - value);
646 break;
647 case KVM_REG_ARM_TIMER_CVAL:
648 vtimer->cnt_cval = value;
649 break;
650 case KVM_REG_ARM_PTIMER_CTL:
651 ptimer->cnt_ctl = value & ~ARCH_TIMER_CTRL_IT_STAT;
652 break;
653 case KVM_REG_ARM_PTIMER_CVAL:
654 ptimer->cnt_cval = value;
655 break;
657 default:
658 return -1;
661 kvm_timer_update_state(vcpu);
662 return 0;
665 static u64 read_timer_ctl(struct arch_timer_context *timer)
668 * Set ISTATUS bit if it's expired.
669 * Note that according to ARMv8 ARM Issue A.k, ISTATUS bit is
670 * UNKNOWN when ENABLE bit is 0, so we chose to set ISTATUS bit
671 * regardless of ENABLE bit for our implementation convenience.
673 if (!kvm_timer_compute_delta(timer))
674 return timer->cnt_ctl | ARCH_TIMER_CTRL_IT_STAT;
675 else
676 return timer->cnt_ctl;
679 u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid)
681 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
682 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
684 switch (regid) {
685 case KVM_REG_ARM_TIMER_CTL:
686 return read_timer_ctl(vtimer);
687 case KVM_REG_ARM_TIMER_CNT:
688 return kvm_phys_timer_read() - vtimer->cntvoff;
689 case KVM_REG_ARM_TIMER_CVAL:
690 return vtimer->cnt_cval;
691 case KVM_REG_ARM_PTIMER_CTL:
692 return read_timer_ctl(ptimer);
693 case KVM_REG_ARM_PTIMER_CVAL:
694 return ptimer->cnt_cval;
695 case KVM_REG_ARM_PTIMER_CNT:
696 return kvm_phys_timer_read();
698 return (u64)-1;
701 static int kvm_timer_starting_cpu(unsigned int cpu)
703 kvm_timer_init_interrupt(NULL);
704 return 0;
707 static int kvm_timer_dying_cpu(unsigned int cpu)
709 disable_percpu_irq(host_vtimer_irq);
710 return 0;
713 int kvm_timer_hyp_init(bool has_gic)
715 struct arch_timer_kvm_info *info;
716 int err;
718 info = arch_timer_get_kvm_info();
719 timecounter = &info->timecounter;
721 if (!timecounter->cc) {
722 kvm_err("kvm_arch_timer: uninitialized timecounter\n");
723 return -ENODEV;
726 if (info->virtual_irq <= 0) {
727 kvm_err("kvm_arch_timer: invalid virtual timer IRQ: %d\n",
728 info->virtual_irq);
729 return -ENODEV;
731 host_vtimer_irq = info->virtual_irq;
733 host_vtimer_irq_flags = irq_get_trigger_type(host_vtimer_irq);
734 if (host_vtimer_irq_flags != IRQF_TRIGGER_HIGH &&
735 host_vtimer_irq_flags != IRQF_TRIGGER_LOW) {
736 kvm_err("Invalid trigger for IRQ%d, assuming level low\n",
737 host_vtimer_irq);
738 host_vtimer_irq_flags = IRQF_TRIGGER_LOW;
741 err = request_percpu_irq(host_vtimer_irq, kvm_arch_timer_handler,
742 "kvm guest timer", kvm_get_running_vcpus());
743 if (err) {
744 kvm_err("kvm_arch_timer: can't request interrupt %d (%d)\n",
745 host_vtimer_irq, err);
746 return err;
749 if (has_gic) {
750 err = irq_set_vcpu_affinity(host_vtimer_irq,
751 kvm_get_running_vcpus());
752 if (err) {
753 kvm_err("kvm_arch_timer: error setting vcpu affinity\n");
754 goto out_free_irq;
758 kvm_info("virtual timer IRQ%d\n", host_vtimer_irq);
760 cpuhp_setup_state(CPUHP_AP_KVM_ARM_TIMER_STARTING,
761 "kvm/arm/timer:starting", kvm_timer_starting_cpu,
762 kvm_timer_dying_cpu);
763 return 0;
764 out_free_irq:
765 free_percpu_irq(host_vtimer_irq, kvm_get_running_vcpus());
766 return err;
769 void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
771 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
772 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
774 soft_timer_cancel(&timer->bg_timer, &timer->expired);
775 soft_timer_cancel(&timer->phys_timer, NULL);
776 kvm_vgic_unmap_phys_irq(vcpu, vtimer->irq.irq);
779 static bool timer_irqs_are_valid(struct kvm_vcpu *vcpu)
781 int vtimer_irq, ptimer_irq;
782 int i, ret;
784 vtimer_irq = vcpu_vtimer(vcpu)->irq.irq;
785 ret = kvm_vgic_set_owner(vcpu, vtimer_irq, vcpu_vtimer(vcpu));
786 if (ret)
787 return false;
789 ptimer_irq = vcpu_ptimer(vcpu)->irq.irq;
790 ret = kvm_vgic_set_owner(vcpu, ptimer_irq, vcpu_ptimer(vcpu));
791 if (ret)
792 return false;
794 kvm_for_each_vcpu(i, vcpu, vcpu->kvm) {
795 if (vcpu_vtimer(vcpu)->irq.irq != vtimer_irq ||
796 vcpu_ptimer(vcpu)->irq.irq != ptimer_irq)
797 return false;
800 return true;
803 bool kvm_arch_timer_get_input_level(int vintid)
805 struct kvm_vcpu *vcpu = kvm_arm_get_running_vcpu();
806 struct arch_timer_context *timer;
808 if (vintid == vcpu_vtimer(vcpu)->irq.irq)
809 timer = vcpu_vtimer(vcpu);
810 else
811 BUG(); /* We only map the vtimer so far */
813 return kvm_timer_should_fire(timer);
816 int kvm_timer_enable(struct kvm_vcpu *vcpu)
818 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
819 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
820 int ret;
822 if (timer->enabled)
823 return 0;
825 /* Without a VGIC we do not map virtual IRQs to physical IRQs */
826 if (!irqchip_in_kernel(vcpu->kvm))
827 goto no_vgic;
829 if (!vgic_initialized(vcpu->kvm))
830 return -ENODEV;
832 if (!timer_irqs_are_valid(vcpu)) {
833 kvm_debug("incorrectly configured timer irqs\n");
834 return -EINVAL;
837 ret = kvm_vgic_map_phys_irq(vcpu, host_vtimer_irq, vtimer->irq.irq,
838 kvm_arch_timer_get_input_level);
839 if (ret)
840 return ret;
842 no_vgic:
843 preempt_disable();
844 timer->enabled = 1;
845 kvm_timer_vcpu_load(vcpu);
846 preempt_enable();
848 return 0;
852 * On VHE system, we only need to configure trap on physical timer and counter
853 * accesses in EL0 and EL1 once, not for every world switch.
854 * The host kernel runs at EL2 with HCR_EL2.TGE == 1,
855 * and this makes those bits have no effect for the host kernel execution.
857 void kvm_timer_init_vhe(void)
859 /* When HCR_EL2.E2H ==1, EL1PCEN and EL1PCTEN are shifted by 10 */
860 u32 cnthctl_shift = 10;
861 u64 val;
864 * Disallow physical timer access for the guest.
865 * Physical counter access is allowed.
867 val = read_sysreg(cnthctl_el2);
868 val &= ~(CNTHCTL_EL1PCEN << cnthctl_shift);
869 val |= (CNTHCTL_EL1PCTEN << cnthctl_shift);
870 write_sysreg(val, cnthctl_el2);
873 static void set_timer_irqs(struct kvm *kvm, int vtimer_irq, int ptimer_irq)
875 struct kvm_vcpu *vcpu;
876 int i;
878 kvm_for_each_vcpu(i, vcpu, kvm) {
879 vcpu_vtimer(vcpu)->irq.irq = vtimer_irq;
880 vcpu_ptimer(vcpu)->irq.irq = ptimer_irq;
884 int kvm_arm_timer_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
886 int __user *uaddr = (int __user *)(long)attr->addr;
887 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
888 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
889 int irq;
891 if (!irqchip_in_kernel(vcpu->kvm))
892 return -EINVAL;
894 if (get_user(irq, uaddr))
895 return -EFAULT;
897 if (!(irq_is_ppi(irq)))
898 return -EINVAL;
900 if (vcpu->arch.timer_cpu.enabled)
901 return -EBUSY;
903 switch (attr->attr) {
904 case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
905 set_timer_irqs(vcpu->kvm, irq, ptimer->irq.irq);
906 break;
907 case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
908 set_timer_irqs(vcpu->kvm, vtimer->irq.irq, irq);
909 break;
910 default:
911 return -ENXIO;
914 return 0;
917 int kvm_arm_timer_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
919 int __user *uaddr = (int __user *)(long)attr->addr;
920 struct arch_timer_context *timer;
921 int irq;
923 switch (attr->attr) {
924 case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
925 timer = vcpu_vtimer(vcpu);
926 break;
927 case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
928 timer = vcpu_ptimer(vcpu);
929 break;
930 default:
931 return -ENXIO;
934 irq = timer->irq.irq;
935 return put_user(irq, uaddr);
938 int kvm_arm_timer_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
940 switch (attr->attr) {
941 case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
942 case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
943 return 0;
946 return -ENXIO;