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>
28 #include <kvm/arm_vgic.h>
29 #include <kvm/arm_arch_timer.h>
33 static struct timecounter *timecounter;
34 static unsigned int host_vtimer_irq;
35 static u32 host_vtimer_irq_flags;
37 void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
39 vcpu->arch.timer_cpu.active_cleared_last = false;
42 static cycle_t kvm_phys_timer_read(void)
44 return timecounter->cc->read(timecounter->cc);
47 static bool timer_is_armed(struct arch_timer_cpu *timer)
52 /* timer_arm: as in "arm the timer", not as in ARM the company */
53 static void timer_arm(struct arch_timer_cpu *timer, u64 ns)
56 hrtimer_start(&timer->timer, ktime_add_ns(ktime_get(), ns),
60 static void timer_disarm(struct arch_timer_cpu *timer)
62 if (timer_is_armed(timer)) {
63 hrtimer_cancel(&timer->timer);
64 cancel_work_sync(&timer->expired);
69 static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
71 struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
74 * We disable the timer in the world switch and let it be
75 * handled by kvm_timer_sync_hwstate(). Getting a timer
76 * interrupt at this point is a sure sign of some major
79 pr_warn("Unexpected interrupt %d on vcpu %p\n", irq, vcpu);
84 * Work function for handling the backup timer that we schedule when a vcpu is
85 * no longer running, but had a timer programmed to fire in the future.
87 static void kvm_timer_inject_irq_work(struct work_struct *work)
89 struct kvm_vcpu *vcpu;
91 vcpu = container_of(work, struct kvm_vcpu, arch.timer_cpu.expired);
94 * If the vcpu is blocked we want to wake it up so that it will see
95 * the timer has expired when entering the guest.
100 static u64 kvm_timer_compute_delta(struct kvm_vcpu *vcpu)
104 cval = vcpu->arch.timer_cpu.cntv_cval;
105 now = kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff;
110 ns = cyclecounter_cyc2ns(timecounter->cc,
120 static enum hrtimer_restart kvm_timer_expire(struct hrtimer *hrt)
122 struct arch_timer_cpu *timer;
123 struct kvm_vcpu *vcpu;
126 timer = container_of(hrt, struct arch_timer_cpu, timer);
127 vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
130 * Check that the timer has really expired from the guest's
131 * PoV (NTP on the host may have forced it to expire
132 * early). If we should have slept longer, restart it.
134 ns = kvm_timer_compute_delta(vcpu);
136 hrtimer_forward_now(hrt, ns_to_ktime(ns));
137 return HRTIMER_RESTART;
140 schedule_work(&timer->expired);
141 return HRTIMER_NORESTART;
144 static bool kvm_timer_irq_can_fire(struct kvm_vcpu *vcpu)
146 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
148 return !(timer->cntv_ctl & ARCH_TIMER_CTRL_IT_MASK) &&
149 (timer->cntv_ctl & ARCH_TIMER_CTRL_ENABLE);
152 bool kvm_timer_should_fire(struct kvm_vcpu *vcpu)
154 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
157 if (!kvm_timer_irq_can_fire(vcpu))
160 cval = timer->cntv_cval;
161 now = kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff;
166 static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level)
169 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
171 BUG_ON(!vgic_initialized(vcpu->kvm));
173 timer->active_cleared_last = false;
174 timer->irq.level = new_level;
175 trace_kvm_timer_update_irq(vcpu->vcpu_id, timer->irq.irq,
177 ret = kvm_vgic_inject_mapped_irq(vcpu->kvm, vcpu->vcpu_id,
184 * Check if there was a change in the timer state (should we raise or lower
185 * the line level to the GIC).
187 static int kvm_timer_update_state(struct kvm_vcpu *vcpu)
189 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
192 * If userspace modified the timer registers via SET_ONE_REG before
193 * the vgic was initialized, we mustn't set the timer->irq.level value
194 * because the guest would never see the interrupt. Instead wait
195 * until we call this function from kvm_timer_flush_hwstate.
197 if (!vgic_initialized(vcpu->kvm) || !timer->enabled)
200 if (kvm_timer_should_fire(vcpu) != timer->irq.level)
201 kvm_timer_update_irq(vcpu, !timer->irq.level);
207 * Schedule the background timer before calling kvm_vcpu_block, so that this
208 * thread is removed from its waitqueue and made runnable when there's a timer
209 * interrupt to handle.
211 void kvm_timer_schedule(struct kvm_vcpu *vcpu)
213 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
215 BUG_ON(timer_is_armed(timer));
218 * No need to schedule a background timer if the guest timer has
219 * already expired, because kvm_vcpu_block will return before putting
220 * the thread to sleep.
222 if (kvm_timer_should_fire(vcpu))
226 * If the timer is not capable of raising interrupts (disabled or
227 * masked), then there's no more work for us to do.
229 if (!kvm_timer_irq_can_fire(vcpu))
232 /* The timer has not yet expired, schedule a background timer */
233 timer_arm(timer, kvm_timer_compute_delta(vcpu));
236 void kvm_timer_unschedule(struct kvm_vcpu *vcpu)
238 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
243 * kvm_timer_flush_hwstate - prepare to move the virt timer to the cpu
244 * @vcpu: The vcpu pointer
246 * Check if the virtual timer has expired while we were running in the host,
247 * and inject an interrupt if that was the case.
249 void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu)
251 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
255 if (kvm_timer_update_state(vcpu))
259 * If we enter the guest with the virtual input level to the VGIC
260 * asserted, then we have already told the VGIC what we need to, and
261 * we don't need to exit from the guest until the guest deactivates
262 * the already injected interrupt, so therefore we should set the
263 * hardware active state to prevent unnecessary exits from the guest.
265 * Also, if we enter the guest with the virtual timer interrupt active,
266 * then it must be active on the physical distributor, because we set
267 * the HW bit and the guest must be able to deactivate the virtual and
268 * physical interrupt at the same time.
270 * Conversely, if the virtual input level is deasserted and the virtual
271 * interrupt is not active, then always clear the hardware active state
272 * to ensure that hardware interrupts from the timer triggers a guest
275 phys_active = timer->irq.level ||
276 kvm_vgic_map_is_active(vcpu, timer->irq.irq);
279 * We want to avoid hitting the (re)distributor as much as
280 * possible, as this is a potentially expensive MMIO access
281 * (not to mention locks in the irq layer), and a solution for
282 * this is to cache the "active" state in memory.
284 * Things to consider: we cannot cache an "active set" state,
285 * because the HW can change this behind our back (it becomes
286 * "clear" in the HW). We must then restrict the caching to
289 * The cache is invalidated on:
290 * - vcpu put, indicating that the HW cannot be trusted to be
291 * in a sane state on the next vcpu load,
292 * - any change in the interrupt state
295 * - cached value is "active clear"
296 * - value to be programmed is "active clear"
298 if (timer->active_cleared_last && !phys_active)
301 ret = irq_set_irqchip_state(host_vtimer_irq,
302 IRQCHIP_STATE_ACTIVE,
306 timer->active_cleared_last = !phys_active;
310 * kvm_timer_sync_hwstate - sync timer state from cpu
311 * @vcpu: The vcpu pointer
313 * Check if the virtual timer has expired while we were running in the guest,
314 * and inject an interrupt if that was the case.
316 void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
318 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
320 BUG_ON(timer_is_armed(timer));
323 * The guest could have modified the timer registers or the timer
324 * could have expired, update the timer state.
326 kvm_timer_update_state(vcpu);
329 int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu,
330 const struct kvm_irq_level *irq)
332 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
335 * The vcpu timer irq number cannot be determined in
336 * kvm_timer_vcpu_init() because it is called much before
337 * kvm_vcpu_set_target(). To handle this, we determine
338 * vcpu timer irq number when the vcpu is reset.
340 timer->irq.irq = irq->irq;
343 * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8
344 * and to 0 for ARMv7. We provide an implementation that always
345 * resets the timer to be disabled and unmasked and is compliant with
346 * the ARMv7 architecture.
349 kvm_timer_update_state(vcpu);
354 void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
356 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
358 INIT_WORK(&timer->expired, kvm_timer_inject_irq_work);
359 hrtimer_init(&timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
360 timer->timer.function = kvm_timer_expire;
363 static void kvm_timer_init_interrupt(void *info)
365 enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
368 int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
370 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
373 case KVM_REG_ARM_TIMER_CTL:
374 timer->cntv_ctl = value;
376 case KVM_REG_ARM_TIMER_CNT:
377 vcpu->kvm->arch.timer.cntvoff = kvm_phys_timer_read() - value;
379 case KVM_REG_ARM_TIMER_CVAL:
380 timer->cntv_cval = value;
386 kvm_timer_update_state(vcpu);
390 u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid)
392 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
395 case KVM_REG_ARM_TIMER_CTL:
396 return timer->cntv_ctl;
397 case KVM_REG_ARM_TIMER_CNT:
398 return kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff;
399 case KVM_REG_ARM_TIMER_CVAL:
400 return timer->cntv_cval;
405 static int kvm_timer_starting_cpu(unsigned int cpu)
407 kvm_timer_init_interrupt(NULL);
411 static int kvm_timer_dying_cpu(unsigned int cpu)
413 disable_percpu_irq(host_vtimer_irq);
417 int kvm_timer_hyp_init(void)
419 struct arch_timer_kvm_info *info;
422 info = arch_timer_get_kvm_info();
423 timecounter = &info->timecounter;
425 if (info->virtual_irq <= 0) {
426 kvm_err("kvm_arch_timer: invalid virtual timer IRQ: %d\n",
430 host_vtimer_irq = info->virtual_irq;
432 host_vtimer_irq_flags = irq_get_trigger_type(host_vtimer_irq);
433 if (host_vtimer_irq_flags != IRQF_TRIGGER_HIGH &&
434 host_vtimer_irq_flags != IRQF_TRIGGER_LOW) {
435 kvm_err("Invalid trigger for IRQ%d, assuming level low\n",
437 host_vtimer_irq_flags = IRQF_TRIGGER_LOW;
440 err = request_percpu_irq(host_vtimer_irq, kvm_arch_timer_handler,
441 "kvm guest timer", kvm_get_running_vcpus());
443 kvm_err("kvm_arch_timer: can't request interrupt %d (%d)\n",
444 host_vtimer_irq, err);
448 kvm_info("virtual timer IRQ%d\n", host_vtimer_irq);
450 cpuhp_setup_state(CPUHP_AP_KVM_ARM_TIMER_STARTING,
451 "AP_KVM_ARM_TIMER_STARTING", kvm_timer_starting_cpu,
452 kvm_timer_dying_cpu);
456 void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
458 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
461 kvm_vgic_unmap_phys_irq(vcpu, timer->irq.irq);
464 int kvm_timer_enable(struct kvm_vcpu *vcpu)
466 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
467 struct irq_desc *desc;
468 struct irq_data *data;
476 * Find the physical IRQ number corresponding to the host_vtimer_irq
478 desc = irq_to_desc(host_vtimer_irq);
480 kvm_err("%s: no interrupt descriptor\n", __func__);
484 data = irq_desc_get_irq_data(desc);
485 while (data->parent_data)
486 data = data->parent_data;
488 phys_irq = data->hwirq;
491 * Tell the VGIC that the virtual interrupt is tied to a
492 * physical interrupt. We do that once per VCPU.
494 ret = kvm_vgic_map_phys_irq(vcpu, timer->irq.irq, phys_irq);
500 * There is a potential race here between VCPUs starting for the first
501 * time, which may be enabling the timer multiple times. That doesn't
502 * hurt though, because we're just setting a variable to the same
503 * variable that it already was. The important thing is that all
504 * VCPUs have the enabled variable set, before entering the guest, if
505 * the arch timers are enabled.
513 void kvm_timer_init(struct kvm *kvm)
515 kvm->arch.timer.cntvoff = kvm_phys_timer_read();