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/of_irq.h>
21 #include <linux/kvm.h>
22 #include <linux/kvm_host.h>
23 #include <linux/interrupt.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 struct workqueue_struct *wqueue;
35 static unsigned int host_vtimer_irq;
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);
92 vcpu->arch.timer_cpu.armed = false;
94 WARN_ON(!kvm_timer_should_fire(vcpu));
97 * If the vcpu is blocked we want to wake it up so that it will see
98 * the timer has expired when entering the guest.
103 static u64 kvm_timer_compute_delta(struct kvm_vcpu *vcpu)
107 cval = vcpu->arch.timer_cpu.cntv_cval;
108 now = kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff;
113 ns = cyclecounter_cyc2ns(timecounter->cc,
123 static enum hrtimer_restart kvm_timer_expire(struct hrtimer *hrt)
125 struct arch_timer_cpu *timer;
126 struct kvm_vcpu *vcpu;
129 timer = container_of(hrt, struct arch_timer_cpu, timer);
130 vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
133 * Check that the timer has really expired from the guest's
134 * PoV (NTP on the host may have forced it to expire
135 * early). If we should have slept longer, restart it.
137 ns = kvm_timer_compute_delta(vcpu);
139 hrtimer_forward_now(hrt, ns_to_ktime(ns));
140 return HRTIMER_RESTART;
143 queue_work(wqueue, &timer->expired);
144 return HRTIMER_NORESTART;
147 static bool kvm_timer_irq_can_fire(struct kvm_vcpu *vcpu)
149 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
151 return !(timer->cntv_ctl & ARCH_TIMER_CTRL_IT_MASK) &&
152 (timer->cntv_ctl & ARCH_TIMER_CTRL_ENABLE);
155 bool kvm_timer_should_fire(struct kvm_vcpu *vcpu)
157 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
160 if (!kvm_timer_irq_can_fire(vcpu))
163 cval = timer->cntv_cval;
164 now = kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff;
169 static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level)
172 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
174 BUG_ON(!vgic_initialized(vcpu->kvm));
176 timer->active_cleared_last = false;
177 timer->irq.level = new_level;
178 trace_kvm_timer_update_irq(vcpu->vcpu_id, timer->map->virt_irq,
180 ret = kvm_vgic_inject_mapped_irq(vcpu->kvm, vcpu->vcpu_id,
187 * Check if there was a change in the timer state (should we raise or lower
188 * the line level to the GIC).
190 static int kvm_timer_update_state(struct kvm_vcpu *vcpu)
192 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
195 * If userspace modified the timer registers via SET_ONE_REG before
196 * the vgic was initialized, we mustn't set the timer->irq.level value
197 * because the guest would never see the interrupt. Instead wait
198 * until we call this function from kvm_timer_flush_hwstate.
200 if (!vgic_initialized(vcpu->kvm))
203 if (kvm_timer_should_fire(vcpu) != timer->irq.level)
204 kvm_timer_update_irq(vcpu, !timer->irq.level);
210 * Schedule the background timer before calling kvm_vcpu_block, so that this
211 * thread is removed from its waitqueue and made runnable when there's a timer
212 * interrupt to handle.
214 void kvm_timer_schedule(struct kvm_vcpu *vcpu)
216 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
218 BUG_ON(timer_is_armed(timer));
221 * No need to schedule a background timer if the guest timer has
222 * already expired, because kvm_vcpu_block will return before putting
223 * the thread to sleep.
225 if (kvm_timer_should_fire(vcpu))
229 * If the timer is not capable of raising interrupts (disabled or
230 * masked), then there's no more work for us to do.
232 if (!kvm_timer_irq_can_fire(vcpu))
235 /* The timer has not yet expired, schedule a background timer */
236 timer_arm(timer, kvm_timer_compute_delta(vcpu));
239 void kvm_timer_unschedule(struct kvm_vcpu *vcpu)
241 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
246 * kvm_timer_flush_hwstate - prepare to move the virt timer to the cpu
247 * @vcpu: The vcpu pointer
249 * Check if the virtual timer has expired while we were running in the host,
250 * and inject an interrupt if that was the case.
252 void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu)
254 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
258 if (kvm_timer_update_state(vcpu))
262 * If we enter the guest with the virtual input level to the VGIC
263 * asserted, then we have already told the VGIC what we need to, and
264 * we don't need to exit from the guest until the guest deactivates
265 * the already injected interrupt, so therefore we should set the
266 * hardware active state to prevent unnecessary exits from the guest.
268 * Also, if we enter the guest with the virtual timer interrupt active,
269 * then it must be active on the physical distributor, because we set
270 * the HW bit and the guest must be able to deactivate the virtual and
271 * physical interrupt at the same time.
273 * Conversely, if the virtual input level is deasserted and the virtual
274 * interrupt is not active, then always clear the hardware active state
275 * to ensure that hardware interrupts from the timer triggers a guest
278 if (timer->irq.level || kvm_vgic_map_is_active(vcpu, timer->map))
284 * We want to avoid hitting the (re)distributor as much as
285 * possible, as this is a potentially expensive MMIO access
286 * (not to mention locks in the irq layer), and a solution for
287 * this is to cache the "active" state in memory.
289 * Things to consider: we cannot cache an "active set" state,
290 * because the HW can change this behind our back (it becomes
291 * "clear" in the HW). We must then restrict the caching to
294 * The cache is invalidated on:
295 * - vcpu put, indicating that the HW cannot be trusted to be
296 * in a sane state on the next vcpu load,
297 * - any change in the interrupt state
300 * - cached value is "active clear"
301 * - value to be programmed is "active clear"
303 if (timer->active_cleared_last && !phys_active)
306 ret = irq_set_irqchip_state(timer->map->irq,
307 IRQCHIP_STATE_ACTIVE,
311 timer->active_cleared_last = !phys_active;
315 * kvm_timer_sync_hwstate - sync timer state from cpu
316 * @vcpu: The vcpu pointer
318 * Check if the virtual timer has expired while we were running in the guest,
319 * and inject an interrupt if that was the case.
321 void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
323 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
325 BUG_ON(timer_is_armed(timer));
328 * The guest could have modified the timer registers or the timer
329 * could have expired, update the timer state.
331 kvm_timer_update_state(vcpu);
334 int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu,
335 const struct kvm_irq_level *irq)
337 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
338 struct irq_phys_map *map;
341 * The vcpu timer irq number cannot be determined in
342 * kvm_timer_vcpu_init() because it is called much before
343 * kvm_vcpu_set_target(). To handle this, we determine
344 * vcpu timer irq number when the vcpu is reset.
346 timer->irq.irq = irq->irq;
349 * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8
350 * and to 0 for ARMv7. We provide an implementation that always
351 * resets the timer to be disabled and unmasked and is compliant with
352 * the ARMv7 architecture.
355 kvm_timer_update_state(vcpu);
358 * Tell the VGIC that the virtual interrupt is tied to a
359 * physical interrupt. We do that once per VCPU.
361 map = kvm_vgic_map_phys_irq(vcpu, irq->irq, host_vtimer_irq);
362 if (WARN_ON(IS_ERR(map)))
369 void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
371 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
373 INIT_WORK(&timer->expired, kvm_timer_inject_irq_work);
374 hrtimer_init(&timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
375 timer->timer.function = kvm_timer_expire;
378 static void kvm_timer_init_interrupt(void *info)
380 enable_percpu_irq(host_vtimer_irq, 0);
383 int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
385 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
388 case KVM_REG_ARM_TIMER_CTL:
389 timer->cntv_ctl = value;
391 case KVM_REG_ARM_TIMER_CNT:
392 vcpu->kvm->arch.timer.cntvoff = kvm_phys_timer_read() - value;
394 case KVM_REG_ARM_TIMER_CVAL:
395 timer->cntv_cval = value;
401 kvm_timer_update_state(vcpu);
405 u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid)
407 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
410 case KVM_REG_ARM_TIMER_CTL:
411 return timer->cntv_ctl;
412 case KVM_REG_ARM_TIMER_CNT:
413 return kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff;
414 case KVM_REG_ARM_TIMER_CVAL:
415 return timer->cntv_cval;
420 static int kvm_timer_cpu_notify(struct notifier_block *self,
421 unsigned long action, void *cpu)
425 case CPU_STARTING_FROZEN:
426 kvm_timer_init_interrupt(NULL);
429 case CPU_DYING_FROZEN:
430 disable_percpu_irq(host_vtimer_irq);
437 static struct notifier_block kvm_timer_cpu_nb = {
438 .notifier_call = kvm_timer_cpu_notify,
441 static const struct of_device_id arch_timer_of_match[] = {
442 { .compatible = "arm,armv7-timer", },
443 { .compatible = "arm,armv8-timer", },
447 int kvm_timer_hyp_init(void)
449 struct device_node *np;
453 timecounter = arch_timer_get_timecounter();
457 np = of_find_matching_node(NULL, arch_timer_of_match);
459 kvm_err("kvm_arch_timer: can't find DT node\n");
463 ppi = irq_of_parse_and_map(np, 2);
465 kvm_err("kvm_arch_timer: no virtual timer interrupt\n");
470 err = request_percpu_irq(ppi, kvm_arch_timer_handler,
471 "kvm guest timer", kvm_get_running_vcpus());
473 kvm_err("kvm_arch_timer: can't request interrupt %d (%d)\n",
478 host_vtimer_irq = ppi;
480 err = __register_cpu_notifier(&kvm_timer_cpu_nb);
482 kvm_err("Cannot register timer CPU notifier\n");
486 wqueue = create_singlethread_workqueue("kvm_arch_timer");
492 kvm_info("%s IRQ%d\n", np->name, ppi);
493 on_each_cpu(kvm_timer_init_interrupt, NULL, 1);
497 free_percpu_irq(ppi, kvm_get_running_vcpus());
503 void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
505 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
509 kvm_vgic_unmap_phys_irq(vcpu, timer->map);
512 void kvm_timer_enable(struct kvm *kvm)
514 if (kvm->arch.timer.enabled)
518 * There is a potential race here between VCPUs starting for the first
519 * time, which may be enabling the timer multiple times. That doesn't
520 * hurt though, because we're just setting a variable to the same
521 * variable that it already was. The important thing is that all
522 * VCPUs have the enabled variable set, before entering the guest, if
523 * the arch timers are enabled.
525 if (timecounter && wqueue)
526 kvm->arch.timer.enabled = 1;
529 void kvm_timer_init(struct kvm *kvm)
531 kvm->arch.timer.cntvoff = kvm_phys_timer_read();