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>
31 static struct timecounter
*timecounter
;
32 static struct workqueue_struct
*wqueue
;
33 static unsigned int host_vtimer_irq
;
35 static cycle_t
kvm_phys_timer_read(void)
37 return timecounter
->cc
->read(timecounter
->cc
);
40 static bool timer_is_armed(struct arch_timer_cpu
*timer
)
45 /* timer_arm: as in "arm the timer", not as in ARM the company */
46 static void timer_arm(struct arch_timer_cpu
*timer
, u64 ns
)
49 hrtimer_start(&timer
->timer
, ktime_add_ns(ktime_get(), ns
),
53 static void timer_disarm(struct arch_timer_cpu
*timer
)
55 if (timer_is_armed(timer
)) {
56 hrtimer_cancel(&timer
->timer
);
57 cancel_work_sync(&timer
->expired
);
62 static void kvm_timer_inject_irq(struct kvm_vcpu
*vcpu
)
64 struct arch_timer_cpu
*timer
= &vcpu
->arch
.timer_cpu
;
66 timer
->cntv_ctl
|= ARCH_TIMER_CTRL_IT_MASK
;
67 kvm_vgic_inject_irq(vcpu
->kvm
, vcpu
->vcpu_id
,
72 static irqreturn_t
kvm_arch_timer_handler(int irq
, void *dev_id
)
74 struct kvm_vcpu
*vcpu
= *(struct kvm_vcpu
**)dev_id
;
77 * We disable the timer in the world switch and let it be
78 * handled by kvm_timer_sync_hwstate(). Getting a timer
79 * interrupt at this point is a sure sign of some major
82 pr_warn("Unexpected interrupt %d on vcpu %p\n", irq
, vcpu
);
86 static void kvm_timer_inject_irq_work(struct work_struct
*work
)
88 struct kvm_vcpu
*vcpu
;
90 vcpu
= container_of(work
, struct kvm_vcpu
, arch
.timer_cpu
.expired
);
91 vcpu
->arch
.timer_cpu
.armed
= false;
92 kvm_timer_inject_irq(vcpu
);
95 static enum hrtimer_restart
kvm_timer_expire(struct hrtimer
*hrt
)
97 struct arch_timer_cpu
*timer
;
98 timer
= container_of(hrt
, struct arch_timer_cpu
, timer
);
99 queue_work(wqueue
, &timer
->expired
);
100 return HRTIMER_NORESTART
;
104 * kvm_timer_flush_hwstate - prepare to move the virt timer to the cpu
105 * @vcpu: The vcpu pointer
107 * Disarm any pending soft timers, since the world-switch code will write the
108 * virtual timer state back to the physical CPU.
110 void kvm_timer_flush_hwstate(struct kvm_vcpu
*vcpu
)
112 struct arch_timer_cpu
*timer
= &vcpu
->arch
.timer_cpu
;
115 * We're about to run this vcpu again, so there is no need to
116 * keep the background timer running, as we're about to
117 * populate the CPU timer again.
123 * kvm_timer_sync_hwstate - sync timer state from cpu
124 * @vcpu: The vcpu pointer
126 * Check if the virtual timer was armed and either schedule a corresponding
127 * soft timer or inject directly if already expired.
129 void kvm_timer_sync_hwstate(struct kvm_vcpu
*vcpu
)
131 struct arch_timer_cpu
*timer
= &vcpu
->arch
.timer_cpu
;
135 if ((timer
->cntv_ctl
& ARCH_TIMER_CTRL_IT_MASK
) ||
136 !(timer
->cntv_ctl
& ARCH_TIMER_CTRL_ENABLE
))
139 cval
= timer
->cntv_cval
;
140 now
= kvm_phys_timer_read() - vcpu
->kvm
->arch
.timer
.cntvoff
;
142 BUG_ON(timer_is_armed(timer
));
146 * Timer has already expired while we were not
147 * looking. Inject the interrupt and carry on.
149 kvm_timer_inject_irq(vcpu
);
153 ns
= cyclecounter_cyc2ns(timecounter
->cc
, cval
- now
);
154 timer_arm(timer
, ns
);
157 void kvm_timer_vcpu_reset(struct kvm_vcpu
*vcpu
,
158 const struct kvm_irq_level
*irq
)
160 struct arch_timer_cpu
*timer
= &vcpu
->arch
.timer_cpu
;
163 * The vcpu timer irq number cannot be determined in
164 * kvm_timer_vcpu_init() because it is called much before
165 * kvm_vcpu_set_target(). To handle this, we determine
166 * vcpu timer irq number when the vcpu is reset.
171 void kvm_timer_vcpu_init(struct kvm_vcpu
*vcpu
)
173 struct arch_timer_cpu
*timer
= &vcpu
->arch
.timer_cpu
;
175 INIT_WORK(&timer
->expired
, kvm_timer_inject_irq_work
);
176 hrtimer_init(&timer
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
177 timer
->timer
.function
= kvm_timer_expire
;
180 static void kvm_timer_init_interrupt(void *info
)
182 enable_percpu_irq(host_vtimer_irq
, 0);
186 static int kvm_timer_cpu_notify(struct notifier_block
*self
,
187 unsigned long action
, void *cpu
)
191 case CPU_STARTING_FROZEN
:
192 kvm_timer_init_interrupt(NULL
);
195 case CPU_DYING_FROZEN
:
196 disable_percpu_irq(host_vtimer_irq
);
203 static struct notifier_block kvm_timer_cpu_nb
= {
204 .notifier_call
= kvm_timer_cpu_notify
,
207 static const struct of_device_id arch_timer_of_match
[] = {
208 { .compatible
= "arm,armv7-timer", },
209 { .compatible
= "arm,armv8-timer", },
213 int kvm_timer_hyp_init(void)
215 struct device_node
*np
;
219 timecounter
= arch_timer_get_timecounter();
223 np
= of_find_matching_node(NULL
, arch_timer_of_match
);
225 kvm_err("kvm_arch_timer: can't find DT node\n");
229 ppi
= irq_of_parse_and_map(np
, 2);
231 kvm_err("kvm_arch_timer: no virtual timer interrupt\n");
236 err
= request_percpu_irq(ppi
, kvm_arch_timer_handler
,
237 "kvm guest timer", kvm_get_running_vcpus());
239 kvm_err("kvm_arch_timer: can't request interrupt %d (%d)\n",
244 host_vtimer_irq
= ppi
;
246 err
= register_cpu_notifier(&kvm_timer_cpu_nb
);
248 kvm_err("Cannot register timer CPU notifier\n");
252 wqueue
= create_singlethread_workqueue("kvm_arch_timer");
258 kvm_info("%s IRQ%d\n", np
->name
, ppi
);
259 on_each_cpu(kvm_timer_init_interrupt
, NULL
, 1);
263 free_percpu_irq(ppi
, kvm_get_running_vcpus());
269 void kvm_timer_vcpu_terminate(struct kvm_vcpu
*vcpu
)
271 struct arch_timer_cpu
*timer
= &vcpu
->arch
.timer_cpu
;
276 int kvm_timer_init(struct kvm
*kvm
)
278 if (timecounter
&& wqueue
) {
279 kvm
->arch
.timer
.cntvoff
= kvm_phys_timer_read();
280 kvm
->arch
.timer
.enabled
= 1;