blob: eea40439066c2eb828ded2a0d87409140b658c2f [file] [log] [blame]
Marcelo Tosattid3c7b772009-02-23 10:57:41 -03001#include <linux/kvm_host.h>
2#include <linux/kvm.h>
3#include <linux/hrtimer.h>
4#include <asm/atomic.h>
5#include "kvm_timer.h"
6
7static int __kvm_timer_fn(struct kvm_vcpu *vcpu, struct kvm_timer *ktimer)
8{
9 int restart_timer = 0;
10 wait_queue_head_t *q = &vcpu->wq;
11
Jan Kiszkaf7104db2009-06-09 15:37:01 +020012 /*
13 * There is a race window between reading and incrementing, but we do
14 * not care about potentially loosing timer events in the !reinject
15 * case anyway.
16 */
17 if (ktimer->reinject || !atomic_read(&ktimer->pending)) {
Jan Kiszka681405b2009-06-09 15:37:03 +020018 atomic_inc(&ktimer->pending);
Jan Kiszkaf7104db2009-06-09 15:37:01 +020019 /* FIXME: this code should not know anything about vcpus */
Jan Kiszka681405b2009-06-09 15:37:03 +020020 set_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
Jan Kiszkaf7104db2009-06-09 15:37:01 +020021 }
Marcelo Tosattid3c7b772009-02-23 10:57:41 -030022
23 if (waitqueue_active(q))
24 wake_up_interruptible(q);
25
26 if (ktimer->t_ops->is_periodic(ktimer)) {
27 hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
28 restart_timer = 1;
29 }
30
31 return restart_timer;
32}
33
34enum hrtimer_restart kvm_timer_fn(struct hrtimer *data)
35{
36 int restart_timer;
37 struct kvm_vcpu *vcpu;
38 struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
39
Gleb Natapov1ed0ce02009-06-09 15:56:27 +030040 vcpu = ktimer->vcpu;
Marcelo Tosattid3c7b772009-02-23 10:57:41 -030041 if (!vcpu)
42 return HRTIMER_NORESTART;
43
44 restart_timer = __kvm_timer_fn(vcpu, ktimer);
45 if (restart_timer)
46 return HRTIMER_RESTART;
47 else
48 return HRTIMER_NORESTART;
49}
50