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 <asm/arch_timer.h>
27 #include <asm/kvm_vgic.h>
28 #include <asm/kvm_arch_timer.h>
30 static struct timecounter *timecounter;
31 static struct workqueue_struct *wqueue;
32 static struct kvm_irq_level timer_irq = {
36 static cycle_t kvm_phys_timer_read(void)
38 return timecounter->cc->read(timecounter->cc);
41 static bool timer_is_armed(struct arch_timer_cpu *timer)
46 /* timer_arm: as in "arm the timer", not as in ARM the company */
47 static void timer_arm(struct arch_timer_cpu *timer, u64 ns)
50 hrtimer_start(&timer->timer, ktime_add_ns(ktime_get(), ns),
54 static void timer_disarm(struct arch_timer_cpu *timer)
56 if (timer_is_armed(timer)) {
57 hrtimer_cancel(&timer->timer);
58 cancel_work_sync(&timer->expired);
63 static void kvm_timer_inject_irq(struct kvm_vcpu *vcpu)
65 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
67 timer->cntv_ctl |= 1 << 1; /* Mask the interrupt in the guest */
68 kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
69 vcpu->arch.timer_cpu.irq->irq,
70 vcpu->arch.timer_cpu.irq->level);
73 static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
75 struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
78 * We disable the timer in the world switch and let it be
79 * handled by kvm_timer_sync_hwstate(). Getting a timer
80 * interrupt at this point is a sure sign of some major
83 pr_warn("Unexpected interrupt %d on vcpu %p\n", irq, vcpu);
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;
93 kvm_timer_inject_irq(vcpu);
96 static enum hrtimer_restart kvm_timer_expire(struct hrtimer *hrt)
98 struct arch_timer_cpu *timer;
99 timer = container_of(hrt, struct arch_timer_cpu, timer);
100 queue_work(wqueue, &timer->expired);
101 return HRTIMER_NORESTART;
105 * kvm_timer_flush_hwstate - prepare to move the virt timer to the cpu
106 * @vcpu: The vcpu pointer
108 * Disarm any pending soft timers, since the world-switch code will write the
109 * virtual timer state back to the physical CPU.
111 void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu)
113 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
116 * We're about to run this vcpu again, so there is no need to
117 * keep the background timer running, as we're about to
118 * populate the CPU timer again.
124 * kvm_timer_sync_hwstate - sync timer state from cpu
125 * @vcpu: The vcpu pointer
127 * Check if the virtual timer was armed and either schedule a corresponding
128 * soft timer or inject directly if already expired.
130 void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
132 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
136 /* Check if the timer is enabled and unmasked first */
137 if ((timer->cntv_ctl & 3) != 1)
140 cval = timer->cntv_cval;
141 now = kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff;
143 BUG_ON(timer_is_armed(timer));
147 * Timer has already expired while we were not
148 * looking. Inject the interrupt and carry on.
150 kvm_timer_inject_irq(vcpu);
154 ns = cyclecounter_cyc2ns(timecounter->cc, cval - now);
155 timer_arm(timer, ns);
158 void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
160 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
162 INIT_WORK(&timer->expired, kvm_timer_inject_irq_work);
163 hrtimer_init(&timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
164 timer->timer.function = kvm_timer_expire;
165 timer->irq = &timer_irq;
168 static void kvm_timer_init_interrupt(void *info)
170 enable_percpu_irq(timer_irq.irq, 0);
174 static int kvm_timer_cpu_notify(struct notifier_block *self,
175 unsigned long action, void *cpu)
179 case CPU_STARTING_FROZEN:
180 kvm_timer_init_interrupt(NULL);
183 case CPU_DYING_FROZEN:
184 disable_percpu_irq(timer_irq.irq);
191 static struct notifier_block kvm_timer_cpu_nb = {
192 .notifier_call = kvm_timer_cpu_notify,
195 static const struct of_device_id arch_timer_of_match[] = {
196 { .compatible = "arm,armv7-timer", },
200 int kvm_timer_hyp_init(void)
202 struct device_node *np;
206 timecounter = arch_timer_get_timecounter();
210 np = of_find_matching_node(NULL, arch_timer_of_match);
212 kvm_err("kvm_arch_timer: can't find DT node\n");
216 ppi = irq_of_parse_and_map(np, 2);
218 kvm_err("kvm_arch_timer: no virtual timer interrupt\n");
223 err = request_percpu_irq(ppi, kvm_arch_timer_handler,
224 "kvm guest timer", kvm_get_running_vcpus());
226 kvm_err("kvm_arch_timer: can't request interrupt %d (%d)\n",
233 err = register_cpu_notifier(&kvm_timer_cpu_nb);
235 kvm_err("Cannot register timer CPU notifier\n");
239 wqueue = create_singlethread_workqueue("kvm_arch_timer");
245 kvm_info("%s IRQ%d\n", np->name, ppi);
246 on_each_cpu(kvm_timer_init_interrupt, NULL, 1);
250 free_percpu_irq(ppi, kvm_get_running_vcpus());
256 void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
258 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
263 int kvm_timer_init(struct kvm *kvm)
265 if (timecounter && wqueue) {
266 kvm->arch.timer.cntvoff = kvm_phys_timer_read();
267 kvm->arch.timer.enabled = 1;