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>
25 #include <clocksource/arm_arch_timer.h>
26 #include <asm/arch_timer.h>
27 #include <asm/kvm_hyp.h>
29 #include <kvm/arm_vgic.h>
30 #include <kvm/arm_arch_timer.h>
34 static struct timecounter *timecounter;
35 static unsigned int host_vtimer_irq;
36 static u32 host_vtimer_irq_flags;
38 void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
40 vcpu_vtimer(vcpu)->active_cleared_last = false;
43 u64 kvm_phys_timer_read(void)
45 return timecounter->cc->read(timecounter->cc);
48 static bool timer_is_armed(struct arch_timer_cpu *timer)
53 /* timer_arm: as in "arm the timer", not as in ARM the company */
54 static void timer_arm(struct arch_timer_cpu *timer, u64 ns)
57 hrtimer_start(&timer->timer, ktime_add_ns(ktime_get(), ns),
61 static void timer_disarm(struct arch_timer_cpu *timer)
63 if (timer_is_armed(timer)) {
64 hrtimer_cancel(&timer->timer);
65 cancel_work_sync(&timer->expired);
70 static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
72 struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
75 * We disable the timer in the world switch and let it be
76 * handled by kvm_timer_sync_hwstate(). Getting a timer
77 * interrupt at this point is a sure sign of some major
80 pr_warn("Unexpected interrupt %d on vcpu %p\n", irq, vcpu);
85 * Work function for handling the backup timer that we schedule when a vcpu is
86 * no longer running, but had a timer programmed to fire in the future.
88 static void kvm_timer_inject_irq_work(struct work_struct *work)
90 struct kvm_vcpu *vcpu;
92 vcpu = container_of(work, struct kvm_vcpu, arch.timer_cpu.expired);
95 * If the vcpu is blocked we want to wake it up so that it will see
96 * the timer has expired when entering the guest.
101 static u64 kvm_timer_compute_delta(struct arch_timer_context *timer_ctx)
105 cval = timer_ctx->cnt_cval;
106 now = kvm_phys_timer_read() - timer_ctx->cntvoff;
111 ns = cyclecounter_cyc2ns(timecounter->cc,
121 static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx)
123 return !(timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_IT_MASK) &&
124 (timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_ENABLE);
128 * Returns the earliest expiration time in ns among guest timers.
129 * Note that it will return 0 if none of timers can fire.
131 static u64 kvm_timer_earliest_exp(struct kvm_vcpu *vcpu)
133 u64 min_virt = ULLONG_MAX, min_phys = ULLONG_MAX;
134 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
135 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
137 if (kvm_timer_irq_can_fire(vtimer))
138 min_virt = kvm_timer_compute_delta(vtimer);
140 if (kvm_timer_irq_can_fire(ptimer))
141 min_phys = kvm_timer_compute_delta(ptimer);
143 /* If none of timers can fire, then return 0 */
144 if ((min_virt == ULLONG_MAX) && (min_phys == ULLONG_MAX))
147 return min(min_virt, min_phys);
150 static enum hrtimer_restart kvm_timer_expire(struct hrtimer *hrt)
152 struct arch_timer_cpu *timer;
153 struct kvm_vcpu *vcpu;
156 timer = container_of(hrt, struct arch_timer_cpu, timer);
157 vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
160 * Check that the timer has really expired from the guest's
161 * PoV (NTP on the host may have forced it to expire
162 * early). If we should have slept longer, restart it.
164 ns = kvm_timer_earliest_exp(vcpu);
166 hrtimer_forward_now(hrt, ns_to_ktime(ns));
167 return HRTIMER_RESTART;
170 schedule_work(&timer->expired);
171 return HRTIMER_NORESTART;
174 bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx)
178 if (!kvm_timer_irq_can_fire(timer_ctx))
181 cval = timer_ctx->cnt_cval;
182 now = kvm_phys_timer_read() - timer_ctx->cntvoff;
187 static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
188 struct arch_timer_context *timer_ctx)
192 BUG_ON(!vgic_initialized(vcpu->kvm));
194 timer_ctx->active_cleared_last = false;
195 timer_ctx->irq.level = new_level;
196 trace_kvm_timer_update_irq(vcpu->vcpu_id, timer_ctx->irq.irq,
197 timer_ctx->irq.level);
199 ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id, timer_ctx->irq.irq,
200 timer_ctx->irq.level);
205 * Check if there was a change in the timer state (should we raise or lower
206 * the line level to the GIC).
208 static int kvm_timer_update_state(struct kvm_vcpu *vcpu)
210 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
211 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
212 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
215 * If userspace modified the timer registers via SET_ONE_REG before
216 * the vgic was initialized, we mustn't set the vtimer->irq.level value
217 * because the guest would never see the interrupt. Instead wait
218 * until we call this function from kvm_timer_flush_hwstate.
220 if (!vgic_initialized(vcpu->kvm) || !timer->enabled)
223 if (kvm_timer_should_fire(vtimer) != vtimer->irq.level)
224 kvm_timer_update_irq(vcpu, !vtimer->irq.level, vtimer);
226 if (kvm_timer_should_fire(ptimer) != ptimer->irq.level)
227 kvm_timer_update_irq(vcpu, !ptimer->irq.level, ptimer);
232 /* Schedule the background timer for the emulated timer. */
233 static void kvm_timer_emulate(struct kvm_vcpu *vcpu,
234 struct arch_timer_context *timer_ctx)
236 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
238 if (kvm_timer_should_fire(timer_ctx))
241 if (!kvm_timer_irq_can_fire(timer_ctx))
244 /* The timer has not yet expired, schedule a background timer */
245 timer_arm(timer, kvm_timer_compute_delta(timer_ctx));
249 * Schedule the background timer before calling kvm_vcpu_block, so that this
250 * thread is removed from its waitqueue and made runnable when there's a timer
251 * interrupt to handle.
253 void kvm_timer_schedule(struct kvm_vcpu *vcpu)
255 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
256 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
257 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
259 BUG_ON(timer_is_armed(timer));
262 * No need to schedule a background timer if any guest timer has
263 * already expired, because kvm_vcpu_block will return before putting
264 * the thread to sleep.
266 if (kvm_timer_should_fire(vtimer) || kvm_timer_should_fire(ptimer))
270 * If both timers are not capable of raising interrupts (disabled or
271 * masked), then there's no more work for us to do.
273 if (!kvm_timer_irq_can_fire(vtimer) && !kvm_timer_irq_can_fire(ptimer))
277 * The guest timers have not yet expired, schedule a background timer.
278 * Set the earliest expiration time among the guest timers.
280 timer_arm(timer, kvm_timer_earliest_exp(vcpu));
283 void kvm_timer_unschedule(struct kvm_vcpu *vcpu)
285 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
290 * kvm_timer_flush_hwstate - prepare to move the virt timer to the cpu
291 * @vcpu: The vcpu pointer
293 * Check if the virtual timer has expired while we were running in the host,
294 * and inject an interrupt if that was the case.
296 void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu)
298 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
302 if (kvm_timer_update_state(vcpu))
305 /* Set the background timer for the physical timer emulation. */
306 kvm_timer_emulate(vcpu, vcpu_ptimer(vcpu));
309 * If we enter the guest with the virtual input level to the VGIC
310 * asserted, then we have already told the VGIC what we need to, and
311 * we don't need to exit from the guest until the guest deactivates
312 * the already injected interrupt, so therefore we should set the
313 * hardware active state to prevent unnecessary exits from the guest.
315 * Also, if we enter the guest with the virtual timer interrupt active,
316 * then it must be active on the physical distributor, because we set
317 * the HW bit and the guest must be able to deactivate the virtual and
318 * physical interrupt at the same time.
320 * Conversely, if the virtual input level is deasserted and the virtual
321 * interrupt is not active, then always clear the hardware active state
322 * to ensure that hardware interrupts from the timer triggers a guest
325 phys_active = vtimer->irq.level ||
326 kvm_vgic_map_is_active(vcpu, vtimer->irq.irq);
329 * We want to avoid hitting the (re)distributor as much as
330 * possible, as this is a potentially expensive MMIO access
331 * (not to mention locks in the irq layer), and a solution for
332 * this is to cache the "active" state in memory.
334 * Things to consider: we cannot cache an "active set" state,
335 * because the HW can change this behind our back (it becomes
336 * "clear" in the HW). We must then restrict the caching to
339 * The cache is invalidated on:
340 * - vcpu put, indicating that the HW cannot be trusted to be
341 * in a sane state on the next vcpu load,
342 * - any change in the interrupt state
345 * - cached value is "active clear"
346 * - value to be programmed is "active clear"
348 if (vtimer->active_cleared_last && !phys_active)
351 ret = irq_set_irqchip_state(host_vtimer_irq,
352 IRQCHIP_STATE_ACTIVE,
356 vtimer->active_cleared_last = !phys_active;
360 * kvm_timer_sync_hwstate - sync timer state from cpu
361 * @vcpu: The vcpu pointer
363 * Check if the virtual timer has expired while we were running in the guest,
364 * and inject an interrupt if that was the case.
366 void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
368 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
371 * This is to cancel the background timer for the physical timer
372 * emulation if it is set.
377 * The guest could have modified the timer registers or the timer
378 * could have expired, update the timer state.
380 kvm_timer_update_state(vcpu);
383 int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu,
384 const struct kvm_irq_level *virt_irq,
385 const struct kvm_irq_level *phys_irq)
387 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
388 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
391 * The vcpu timer irq number cannot be determined in
392 * kvm_timer_vcpu_init() because it is called much before
393 * kvm_vcpu_set_target(). To handle this, we determine
394 * vcpu timer irq number when the vcpu is reset.
396 vtimer->irq.irq = virt_irq->irq;
397 ptimer->irq.irq = phys_irq->irq;
400 * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8
401 * and to 0 for ARMv7. We provide an implementation that always
402 * resets the timer to be disabled and unmasked and is compliant with
403 * the ARMv7 architecture.
407 kvm_timer_update_state(vcpu);
412 /* Make the updates of cntvoff for all vtimer contexts atomic */
413 static void update_vtimer_cntvoff(struct kvm_vcpu *vcpu, u64 cntvoff)
416 struct kvm *kvm = vcpu->kvm;
417 struct kvm_vcpu *tmp;
419 mutex_lock(&kvm->lock);
420 kvm_for_each_vcpu(i, tmp, kvm)
421 vcpu_vtimer(tmp)->cntvoff = cntvoff;
424 * When called from the vcpu create path, the CPU being created is not
425 * included in the loop above, so we just set it here as well.
427 vcpu_vtimer(vcpu)->cntvoff = cntvoff;
428 mutex_unlock(&kvm->lock);
431 void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
433 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
435 /* Synchronize cntvoff across all vtimers of a VM. */
436 update_vtimer_cntvoff(vcpu, kvm_phys_timer_read());
437 vcpu_ptimer(vcpu)->cntvoff = 0;
439 INIT_WORK(&timer->expired, kvm_timer_inject_irq_work);
440 hrtimer_init(&timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
441 timer->timer.function = kvm_timer_expire;
444 static void kvm_timer_init_interrupt(void *info)
446 enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
449 int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
451 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
454 case KVM_REG_ARM_TIMER_CTL:
455 vtimer->cnt_ctl = value;
457 case KVM_REG_ARM_TIMER_CNT:
458 update_vtimer_cntvoff(vcpu, kvm_phys_timer_read() - value);
460 case KVM_REG_ARM_TIMER_CVAL:
461 vtimer->cnt_cval = value;
467 kvm_timer_update_state(vcpu);
471 u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid)
473 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
476 case KVM_REG_ARM_TIMER_CTL:
477 return vtimer->cnt_ctl;
478 case KVM_REG_ARM_TIMER_CNT:
479 return kvm_phys_timer_read() - vtimer->cntvoff;
480 case KVM_REG_ARM_TIMER_CVAL:
481 return vtimer->cnt_cval;
486 static int kvm_timer_starting_cpu(unsigned int cpu)
488 kvm_timer_init_interrupt(NULL);
492 static int kvm_timer_dying_cpu(unsigned int cpu)
494 disable_percpu_irq(host_vtimer_irq);
498 int kvm_timer_hyp_init(void)
500 struct arch_timer_kvm_info *info;
503 info = arch_timer_get_kvm_info();
504 timecounter = &info->timecounter;
506 if (!timecounter->cc) {
507 kvm_err("kvm_arch_timer: uninitialized timecounter\n");
511 if (info->virtual_irq <= 0) {
512 kvm_err("kvm_arch_timer: invalid virtual timer IRQ: %d\n",
516 host_vtimer_irq = info->virtual_irq;
518 host_vtimer_irq_flags = irq_get_trigger_type(host_vtimer_irq);
519 if (host_vtimer_irq_flags != IRQF_TRIGGER_HIGH &&
520 host_vtimer_irq_flags != IRQF_TRIGGER_LOW) {
521 kvm_err("Invalid trigger for IRQ%d, assuming level low\n",
523 host_vtimer_irq_flags = IRQF_TRIGGER_LOW;
526 err = request_percpu_irq(host_vtimer_irq, kvm_arch_timer_handler,
527 "kvm guest timer", kvm_get_running_vcpus());
529 kvm_err("kvm_arch_timer: can't request interrupt %d (%d)\n",
530 host_vtimer_irq, err);
534 kvm_info("virtual timer IRQ%d\n", host_vtimer_irq);
536 cpuhp_setup_state(CPUHP_AP_KVM_ARM_TIMER_STARTING,
537 "kvm/arm/timer:starting", kvm_timer_starting_cpu,
538 kvm_timer_dying_cpu);
542 void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
544 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
545 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
548 kvm_vgic_unmap_phys_irq(vcpu, vtimer->irq.irq);
551 int kvm_timer_enable(struct kvm_vcpu *vcpu)
553 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
554 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
555 struct irq_desc *desc;
556 struct irq_data *data;
564 * Find the physical IRQ number corresponding to the host_vtimer_irq
566 desc = irq_to_desc(host_vtimer_irq);
568 kvm_err("%s: no interrupt descriptor\n", __func__);
572 data = irq_desc_get_irq_data(desc);
573 while (data->parent_data)
574 data = data->parent_data;
576 phys_irq = data->hwirq;
579 * Tell the VGIC that the virtual interrupt is tied to a
580 * physical interrupt. We do that once per VCPU.
582 ret = kvm_vgic_map_phys_irq(vcpu, vtimer->irq.irq, phys_irq);
592 * On VHE system, we only need to configure trap on physical timer and counter
593 * accesses in EL0 and EL1 once, not for every world switch.
594 * The host kernel runs at EL2 with HCR_EL2.TGE == 1,
595 * and this makes those bits have no effect for the host kernel execution.
597 void kvm_timer_init_vhe(void)
599 /* When HCR_EL2.E2H ==1, EL1PCEN and EL1PCTEN are shifted by 10 */
600 u32 cnthctl_shift = 10;
604 * Disallow physical timer access for the guest.
605 * Physical counter access is allowed.
607 val = read_sysreg(cnthctl_el2);
608 val &= ~(CNTHCTL_EL1PCEN << cnthctl_shift);
609 val |= (CNTHCTL_EL1PCTEN << cnthctl_shift);
610 write_sysreg(val, cnthctl_el2);