blob: 37f82c1c6bb76b75bf2b9c905a4059caca34a6dc [file] [log] [blame]
Marc Zyngier53e72402013-01-23 13:21:58 -05001/*
2 * Copyright (C) 2012 ARM Ltd.
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
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.
8 *
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.
13 *
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
17 */
18
19#include <linux/cpu.h>
Marc Zyngier53e72402013-01-23 13:21:58 -050020#include <linux/kvm.h>
21#include <linux/kvm_host.h>
22#include <linux/interrupt.h>
23
Mark Rutland372b7c12013-03-27 15:56:11 +000024#include <clocksource/arm_arch_timer.h>
Marc Zyngier53e72402013-01-23 13:21:58 -050025#include <asm/arch_timer.h>
26
Marc Zyngier7275acd2013-05-14 14:31:01 +010027#include <kvm/arm_vgic.h>
28#include <kvm/arm_arch_timer.h>
Marc Zyngier53e72402013-01-23 13:21:58 -050029
Christoffer Dalle21f0912015-08-30 13:57:20 +020030#include "trace.h"
31
Marc Zyngier53e72402013-01-23 13:21:58 -050032static struct timecounter *timecounter;
33static struct workqueue_struct *wqueue;
Anup Patel5ae7f872013-04-30 12:02:15 +053034static unsigned int host_vtimer_irq;
Marc Zyngier53e72402013-01-23 13:21:58 -050035
Marc Zyngier9b4a3002016-01-29 19:04:48 +000036void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
37{
38 vcpu->arch.timer_cpu.active_cleared_last = false;
39}
40
Marc Zyngier53e72402013-01-23 13:21:58 -050041static cycle_t kvm_phys_timer_read(void)
42{
43 return timecounter->cc->read(timecounter->cc);
44}
45
46static bool timer_is_armed(struct arch_timer_cpu *timer)
47{
48 return timer->armed;
49}
50
51/* timer_arm: as in "arm the timer", not as in ARM the company */
52static void timer_arm(struct arch_timer_cpu *timer, u64 ns)
53{
54 timer->armed = true;
55 hrtimer_start(&timer->timer, ktime_add_ns(ktime_get(), ns),
56 HRTIMER_MODE_ABS);
57}
58
59static void timer_disarm(struct arch_timer_cpu *timer)
60{
61 if (timer_is_armed(timer)) {
62 hrtimer_cancel(&timer->timer);
63 cancel_work_sync(&timer->expired);
64 timer->armed = false;
65 }
66}
67
Marc Zyngier53e72402013-01-23 13:21:58 -050068static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
69{
70 struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
71
72 /*
73 * We disable the timer in the world switch and let it be
74 * handled by kvm_timer_sync_hwstate(). Getting a timer
75 * interrupt at this point is a sure sign of some major
76 * breakage.
77 */
78 pr_warn("Unexpected interrupt %d on vcpu %p\n", irq, vcpu);
79 return IRQ_HANDLED;
80}
81
Christoffer Dall1a748472015-03-13 17:02:55 +000082/*
83 * Work function for handling the backup timer that we schedule when a vcpu is
84 * no longer running, but had a timer programmed to fire in the future.
85 */
Marc Zyngier53e72402013-01-23 13:21:58 -050086static void kvm_timer_inject_irq_work(struct work_struct *work)
87{
88 struct kvm_vcpu *vcpu;
89
90 vcpu = container_of(work, struct kvm_vcpu, arch.timer_cpu.expired);
91 vcpu->arch.timer_cpu.armed = false;
Christoffer Dall1a748472015-03-13 17:02:55 +000092
Marc Zyngier1c5631c2016-04-06 09:37:22 +010093 WARN_ON(!kvm_timer_should_fire(vcpu));
94
Christoffer Dall1a748472015-03-13 17:02:55 +000095 /*
96 * If the vcpu is blocked we want to wake it up so that it will see
97 * the timer has expired when entering the guest.
98 */
99 kvm_vcpu_kick(vcpu);
Marc Zyngier53e72402013-01-23 13:21:58 -0500100}
101
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100102static u64 kvm_timer_compute_delta(struct kvm_vcpu *vcpu)
103{
104 cycle_t cval, now;
105
106 cval = vcpu->arch.timer_cpu.cntv_cval;
107 now = kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff;
108
109 if (now < cval) {
110 u64 ns;
111
112 ns = cyclecounter_cyc2ns(timecounter->cc,
113 cval - now,
114 timecounter->mask,
115 &timecounter->frac);
116 return ns;
117 }
118
119 return 0;
120}
121
Marc Zyngier53e72402013-01-23 13:21:58 -0500122static enum hrtimer_restart kvm_timer_expire(struct hrtimer *hrt)
123{
124 struct arch_timer_cpu *timer;
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100125 struct kvm_vcpu *vcpu;
126 u64 ns;
127
Marc Zyngier53e72402013-01-23 13:21:58 -0500128 timer = container_of(hrt, struct arch_timer_cpu, timer);
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100129 vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
130
131 /*
132 * Check that the timer has really expired from the guest's
133 * PoV (NTP on the host may have forced it to expire
134 * early). If we should have slept longer, restart it.
135 */
136 ns = kvm_timer_compute_delta(vcpu);
137 if (unlikely(ns)) {
138 hrtimer_forward_now(hrt, ns_to_ktime(ns));
139 return HRTIMER_RESTART;
140 }
141
Marc Zyngier53e72402013-01-23 13:21:58 -0500142 queue_work(wqueue, &timer->expired);
143 return HRTIMER_NORESTART;
144}
145
Christoffer Dalld35268d2015-08-25 19:48:21 +0200146static bool kvm_timer_irq_can_fire(struct kvm_vcpu *vcpu)
147{
148 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
149
150 return !(timer->cntv_ctl & ARCH_TIMER_CTRL_IT_MASK) &&
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200151 (timer->cntv_ctl & ARCH_TIMER_CTRL_ENABLE);
Christoffer Dalld35268d2015-08-25 19:48:21 +0200152}
153
Christoffer Dall1a748472015-03-13 17:02:55 +0000154bool kvm_timer_should_fire(struct kvm_vcpu *vcpu)
155{
156 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
157 cycle_t cval, now;
158
Christoffer Dalld35268d2015-08-25 19:48:21 +0200159 if (!kvm_timer_irq_can_fire(vcpu))
Christoffer Dall1a748472015-03-13 17:02:55 +0000160 return false;
161
162 cval = timer->cntv_cval;
163 now = kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff;
164
165 return cval <= now;
166}
167
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200168static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level)
169{
170 int ret;
171 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
172
173 BUG_ON(!vgic_initialized(vcpu->kvm));
174
Marc Zyngier9b4a3002016-01-29 19:04:48 +0000175 timer->active_cleared_last = false;
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200176 timer->irq.level = new_level;
Christoffer Dalle21f0912015-08-30 13:57:20 +0200177 trace_kvm_timer_update_irq(vcpu->vcpu_id, timer->map->virt_irq,
178 timer->irq.level);
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200179 ret = kvm_vgic_inject_mapped_irq(vcpu->kvm, vcpu->vcpu_id,
Andre Przywara4f551a32016-04-13 09:48:02 +0100180 timer->map->virt_irq,
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200181 timer->irq.level);
182 WARN_ON(ret);
183}
184
185/*
186 * Check if there was a change in the timer state (should we raise or lower
187 * the line level to the GIC).
188 */
Andre Przywarab3aff6c2016-02-03 16:56:51 +0000189static int kvm_timer_update_state(struct kvm_vcpu *vcpu)
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200190{
191 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
192
193 /*
194 * If userspace modified the timer registers via SET_ONE_REG before
195 * the vgic was initialized, we mustn't set the timer->irq.level value
196 * because the guest would never see the interrupt. Instead wait
197 * until we call this function from kvm_timer_flush_hwstate.
198 */
199 if (!vgic_initialized(vcpu->kvm))
Andre Przywarab3aff6c2016-02-03 16:56:51 +0000200 return -ENODEV;
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200201
202 if (kvm_timer_should_fire(vcpu) != timer->irq.level)
203 kvm_timer_update_irq(vcpu, !timer->irq.level);
Andre Przywarab3aff6c2016-02-03 16:56:51 +0000204
205 return 0;
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200206}
207
Christoffer Dalld35268d2015-08-25 19:48:21 +0200208/*
209 * Schedule the background timer before calling kvm_vcpu_block, so that this
210 * thread is removed from its waitqueue and made runnable when there's a timer
211 * interrupt to handle.
212 */
213void kvm_timer_schedule(struct kvm_vcpu *vcpu)
214{
215 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
Christoffer Dalld35268d2015-08-25 19:48:21 +0200216
217 BUG_ON(timer_is_armed(timer));
218
219 /*
220 * No need to schedule a background timer if the guest timer has
221 * already expired, because kvm_vcpu_block will return before putting
222 * the thread to sleep.
223 */
224 if (kvm_timer_should_fire(vcpu))
225 return;
226
227 /*
228 * If the timer is not capable of raising interrupts (disabled or
229 * masked), then there's no more work for us to do.
230 */
231 if (!kvm_timer_irq_can_fire(vcpu))
232 return;
233
234 /* The timer has not yet expired, schedule a background timer */
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100235 timer_arm(timer, kvm_timer_compute_delta(vcpu));
Christoffer Dalld35268d2015-08-25 19:48:21 +0200236}
237
238void kvm_timer_unschedule(struct kvm_vcpu *vcpu)
239{
240 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
241 timer_disarm(timer);
242}
243
Marc Zyngier53e72402013-01-23 13:21:58 -0500244/**
245 * kvm_timer_flush_hwstate - prepare to move the virt timer to the cpu
246 * @vcpu: The vcpu pointer
247 *
Christoffer Dalld35268d2015-08-25 19:48:21 +0200248 * Check if the virtual timer has expired while we were running in the host,
249 * and inject an interrupt if that was the case.
Marc Zyngier53e72402013-01-23 13:21:58 -0500250 */
251void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu)
252{
253 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
Christoffer Dallcff92112015-10-16 12:41:21 +0200254 bool phys_active;
255 int ret;
Marc Zyngier53e72402013-01-23 13:21:58 -0500256
Andre Przywarab3aff6c2016-02-03 16:56:51 +0000257 if (kvm_timer_update_state(vcpu))
258 return;
Christoffer Dallcff92112015-10-16 12:41:21 +0200259
260 /*
Christoffer Dall0e3dfda2015-11-24 16:23:05 +0100261 * If we enter the guest with the virtual input level to the VGIC
262 * asserted, then we have already told the VGIC what we need to, and
263 * we don't need to exit from the guest until the guest deactivates
264 * the already injected interrupt, so therefore we should set the
265 * hardware active state to prevent unnecessary exits from the guest.
266 *
267 * Also, if we enter the guest with the virtual timer interrupt active,
268 * then it must be active on the physical distributor, because we set
269 * the HW bit and the guest must be able to deactivate the virtual and
270 * physical interrupt at the same time.
271 *
272 * Conversely, if the virtual input level is deasserted and the virtual
273 * interrupt is not active, then always clear the hardware active state
274 * to ensure that hardware interrupts from the timer triggers a guest
275 * exit.
276 */
Andre Przywarae262f412016-04-13 10:03:49 +0100277 phys_active = timer->irq.level ||
278 kvm_vgic_map_is_active(vcpu, timer->map->virt_irq);
Christoffer Dallcff92112015-10-16 12:41:21 +0200279
Marc Zyngier9b4a3002016-01-29 19:04:48 +0000280 /*
281 * We want to avoid hitting the (re)distributor as much as
282 * possible, as this is a potentially expensive MMIO access
283 * (not to mention locks in the irq layer), and a solution for
284 * this is to cache the "active" state in memory.
285 *
286 * Things to consider: we cannot cache an "active set" state,
287 * because the HW can change this behind our back (it becomes
288 * "clear" in the HW). We must then restrict the caching to
289 * the "clear" state.
290 *
291 * The cache is invalidated on:
292 * - vcpu put, indicating that the HW cannot be trusted to be
293 * in a sane state on the next vcpu load,
294 * - any change in the interrupt state
295 *
296 * Usage conditions:
297 * - cached value is "active clear"
298 * - value to be programmed is "active clear"
299 */
300 if (timer->active_cleared_last && !phys_active)
301 return;
302
Christoffer Dallcff92112015-10-16 12:41:21 +0200303 ret = irq_set_irqchip_state(timer->map->irq,
304 IRQCHIP_STATE_ACTIVE,
305 phys_active);
306 WARN_ON(ret);
Marc Zyngier9b4a3002016-01-29 19:04:48 +0000307
308 timer->active_cleared_last = !phys_active;
Marc Zyngier53e72402013-01-23 13:21:58 -0500309}
310
311/**
312 * kvm_timer_sync_hwstate - sync timer state from cpu
313 * @vcpu: The vcpu pointer
314 *
Christoffer Dalld35268d2015-08-25 19:48:21 +0200315 * Check if the virtual timer has expired while we were running in the guest,
316 * and inject an interrupt if that was the case.
Marc Zyngier53e72402013-01-23 13:21:58 -0500317 */
318void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
319{
320 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
Marc Zyngier53e72402013-01-23 13:21:58 -0500321
Marc Zyngier53e72402013-01-23 13:21:58 -0500322 BUG_ON(timer_is_armed(timer));
323
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200324 /*
325 * The guest could have modified the timer registers or the timer
326 * could have expired, update the timer state.
327 */
328 kvm_timer_update_state(vcpu);
Marc Zyngier53e72402013-01-23 13:21:58 -0500329}
330
Marc Zyngierf120cd62014-06-23 13:59:13 +0100331int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu,
332 const struct kvm_irq_level *irq)
Anup Patel5ae7f872013-04-30 12:02:15 +0530333{
334 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
Marc Zyngierf120cd62014-06-23 13:59:13 +0100335 struct irq_phys_map *map;
Anup Patel5ae7f872013-04-30 12:02:15 +0530336
337 /*
338 * The vcpu timer irq number cannot be determined in
339 * kvm_timer_vcpu_init() because it is called much before
340 * kvm_vcpu_set_target(). To handle this, we determine
341 * vcpu timer irq number when the vcpu is reset.
342 */
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200343 timer->irq.irq = irq->irq;
Marc Zyngierf120cd62014-06-23 13:59:13 +0100344
345 /*
Christoffer Dall4ad9e162015-09-04 16:24:39 +0200346 * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8
347 * and to 0 for ARMv7. We provide an implementation that always
348 * resets the timer to be disabled and unmasked and is compliant with
349 * the ARMv7 architecture.
350 */
351 timer->cntv_ctl = 0;
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200352 kvm_timer_update_state(vcpu);
Christoffer Dall4ad9e162015-09-04 16:24:39 +0200353
354 /*
Marc Zyngierf120cd62014-06-23 13:59:13 +0100355 * Tell the VGIC that the virtual interrupt is tied to a
356 * physical interrupt. We do that once per VCPU.
357 */
358 map = kvm_vgic_map_phys_irq(vcpu, irq->irq, host_vtimer_irq);
359 if (WARN_ON(IS_ERR(map)))
360 return PTR_ERR(map);
361
362 timer->map = map;
363 return 0;
Anup Patel5ae7f872013-04-30 12:02:15 +0530364}
365
Marc Zyngier53e72402013-01-23 13:21:58 -0500366void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
367{
368 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
369
370 INIT_WORK(&timer->expired, kvm_timer_inject_irq_work);
371 hrtimer_init(&timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
372 timer->timer.function = kvm_timer_expire;
Marc Zyngier53e72402013-01-23 13:21:58 -0500373}
374
375static void kvm_timer_init_interrupt(void *info)
376{
Anup Patel5ae7f872013-04-30 12:02:15 +0530377 enable_percpu_irq(host_vtimer_irq, 0);
Marc Zyngier53e72402013-01-23 13:21:58 -0500378}
379
Andre Przywara39735a32013-12-13 14:23:26 +0100380int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
381{
382 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
383
384 switch (regid) {
385 case KVM_REG_ARM_TIMER_CTL:
386 timer->cntv_ctl = value;
387 break;
388 case KVM_REG_ARM_TIMER_CNT:
389 vcpu->kvm->arch.timer.cntvoff = kvm_phys_timer_read() - value;
390 break;
391 case KVM_REG_ARM_TIMER_CVAL:
392 timer->cntv_cval = value;
393 break;
394 default:
395 return -1;
396 }
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200397
398 kvm_timer_update_state(vcpu);
Andre Przywara39735a32013-12-13 14:23:26 +0100399 return 0;
400}
401
402u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid)
403{
404 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
405
406 switch (regid) {
407 case KVM_REG_ARM_TIMER_CTL:
408 return timer->cntv_ctl;
409 case KVM_REG_ARM_TIMER_CNT:
410 return kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff;
411 case KVM_REG_ARM_TIMER_CVAL:
412 return timer->cntv_cval;
413 }
414 return (u64)-1;
415}
Marc Zyngier53e72402013-01-23 13:21:58 -0500416
417static int kvm_timer_cpu_notify(struct notifier_block *self,
418 unsigned long action, void *cpu)
419{
420 switch (action) {
421 case CPU_STARTING:
422 case CPU_STARTING_FROZEN:
423 kvm_timer_init_interrupt(NULL);
424 break;
425 case CPU_DYING:
426 case CPU_DYING_FROZEN:
Anup Patel5ae7f872013-04-30 12:02:15 +0530427 disable_percpu_irq(host_vtimer_irq);
Marc Zyngier53e72402013-01-23 13:21:58 -0500428 break;
429 }
430
431 return NOTIFY_OK;
432}
433
434static struct notifier_block kvm_timer_cpu_nb = {
435 .notifier_call = kvm_timer_cpu_notify,
436};
437
Marc Zyngier53e72402013-01-23 13:21:58 -0500438int kvm_timer_hyp_init(void)
439{
Julien Grall29c2d6f2016-04-11 16:32:58 +0100440 struct arch_timer_kvm_info *info;
Marc Zyngier53e72402013-01-23 13:21:58 -0500441 int err;
442
Julien Grall29c2d6f2016-04-11 16:32:58 +0100443 info = arch_timer_get_kvm_info();
444 timecounter = &info->timecounter;
Marc Zyngier53e72402013-01-23 13:21:58 -0500445
Julien Grall29c2d6f2016-04-11 16:32:58 +0100446 if (info->virtual_irq <= 0) {
447 kvm_err("kvm_arch_timer: invalid virtual timer IRQ: %d\n",
448 info->virtual_irq);
Marc Zyngier53e72402013-01-23 13:21:58 -0500449 return -ENODEV;
450 }
Julien Grall29c2d6f2016-04-11 16:32:58 +0100451 host_vtimer_irq = info->virtual_irq;
Marc Zyngier53e72402013-01-23 13:21:58 -0500452
Julien Grall29c2d6f2016-04-11 16:32:58 +0100453 err = request_percpu_irq(host_vtimer_irq, kvm_arch_timer_handler,
Marc Zyngier53e72402013-01-23 13:21:58 -0500454 "kvm guest timer", kvm_get_running_vcpus());
455 if (err) {
456 kvm_err("kvm_arch_timer: can't request interrupt %d (%d)\n",
Julien Grall29c2d6f2016-04-11 16:32:58 +0100457 host_vtimer_irq, err);
Marc Zyngier53e72402013-01-23 13:21:58 -0500458 goto out;
459 }
460
Ming Lei553f8092014-04-07 01:36:08 +0800461 err = __register_cpu_notifier(&kvm_timer_cpu_nb);
Marc Zyngier53e72402013-01-23 13:21:58 -0500462 if (err) {
463 kvm_err("Cannot register timer CPU notifier\n");
464 goto out_free;
465 }
466
467 wqueue = create_singlethread_workqueue("kvm_arch_timer");
468 if (!wqueue) {
469 err = -ENOMEM;
470 goto out_free;
471 }
472
Julien Grall29c2d6f2016-04-11 16:32:58 +0100473 kvm_info("virtual timer IRQ%d\n", host_vtimer_irq);
Marc Zyngier53e72402013-01-23 13:21:58 -0500474 on_each_cpu(kvm_timer_init_interrupt, NULL, 1);
475
476 goto out;
477out_free:
Julien Grall29c2d6f2016-04-11 16:32:58 +0100478 free_percpu_irq(host_vtimer_irq, kvm_get_running_vcpus());
Marc Zyngier53e72402013-01-23 13:21:58 -0500479out:
Marc Zyngier53e72402013-01-23 13:21:58 -0500480 return err;
481}
482
483void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
484{
485 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
486
487 timer_disarm(timer);
Marc Zyngierf120cd62014-06-23 13:59:13 +0100488 if (timer->map)
489 kvm_vgic_unmap_phys_irq(vcpu, timer->map);
Marc Zyngier53e72402013-01-23 13:21:58 -0500490}
491
Christoffer Dall05971122014-12-12 21:19:23 +0100492void kvm_timer_enable(struct kvm *kvm)
Marc Zyngier53e72402013-01-23 13:21:58 -0500493{
Christoffer Dall05971122014-12-12 21:19:23 +0100494 if (kvm->arch.timer.enabled)
495 return;
Marc Zyngier53e72402013-01-23 13:21:58 -0500496
Christoffer Dall05971122014-12-12 21:19:23 +0100497 /*
498 * There is a potential race here between VCPUs starting for the first
499 * time, which may be enabling the timer multiple times. That doesn't
500 * hurt though, because we're just setting a variable to the same
501 * variable that it already was. The important thing is that all
502 * VCPUs have the enabled variable set, before entering the guest, if
503 * the arch timers are enabled.
504 */
505 if (timecounter && wqueue)
506 kvm->arch.timer.enabled = 1;
507}
508
509void kvm_timer_init(struct kvm *kvm)
510{
511 kvm->arch.timer.cntvoff = kvm_phys_timer_read();
Marc Zyngier53e72402013-01-23 13:21:58 -0500512}