1/*
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>
20#include <linux/of_irq.h>
21#include <linux/kvm.h>
22#include <linux/kvm_host.h>
23#include <linux/interrupt.h>
24
25#include <clocksource/arm_arch_timer.h>
26#include <asm/arch_timer.h>
27
28#include <kvm/arm_vgic.h>
29#include <kvm/arm_arch_timer.h>
30
31static struct timecounter *timecounter;
32static struct workqueue_struct *wqueue;
33static unsigned int host_vtimer_irq;
34
35static cycle_t kvm_phys_timer_read(void)
36{
37	return timecounter->cc->read(timecounter->cc);
38}
39
40static bool timer_is_armed(struct arch_timer_cpu *timer)
41{
42	return timer->armed;
43}
44
45/* timer_arm: as in "arm the timer", not as in ARM the company */
46static void timer_arm(struct arch_timer_cpu *timer, u64 ns)
47{
48	timer->armed = true;
49	hrtimer_start(&timer->timer, ktime_add_ns(ktime_get(), ns),
50		      HRTIMER_MODE_ABS);
51}
52
53static void timer_disarm(struct arch_timer_cpu *timer)
54{
55	if (timer_is_armed(timer)) {
56		hrtimer_cancel(&timer->timer);
57		cancel_work_sync(&timer->expired);
58		timer->armed = false;
59	}
60}
61
62static void kvm_timer_inject_irq(struct kvm_vcpu *vcpu)
63{
64	int ret;
65	struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
66
67	timer->cntv_ctl |= ARCH_TIMER_CTRL_IT_MASK;
68	ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
69				  timer->irq->irq,
70				  timer->irq->level);
71	WARN_ON(ret);
72}
73
74static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
75{
76	struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
77
78	/*
79	 * We disable the timer in the world switch and let it be
80	 * handled by kvm_timer_sync_hwstate(). Getting a timer
81	 * interrupt at this point is a sure sign of some major
82	 * breakage.
83	 */
84	pr_warn("Unexpected interrupt %d on vcpu %p\n", irq, vcpu);
85	return IRQ_HANDLED;
86}
87
88/*
89 * Work function for handling the backup timer that we schedule when a vcpu is
90 * no longer running, but had a timer programmed to fire in the future.
91 */
92static void kvm_timer_inject_irq_work(struct work_struct *work)
93{
94	struct kvm_vcpu *vcpu;
95
96	vcpu = container_of(work, struct kvm_vcpu, arch.timer_cpu.expired);
97	vcpu->arch.timer_cpu.armed = false;
98
99	/*
100	 * If the vcpu is blocked we want to wake it up so that it will see
101	 * the timer has expired when entering the guest.
102	 */
103	kvm_vcpu_kick(vcpu);
104}
105
106static enum hrtimer_restart kvm_timer_expire(struct hrtimer *hrt)
107{
108	struct arch_timer_cpu *timer;
109	timer = container_of(hrt, struct arch_timer_cpu, timer);
110	queue_work(wqueue, &timer->expired);
111	return HRTIMER_NORESTART;
112}
113
114bool kvm_timer_should_fire(struct kvm_vcpu *vcpu)
115{
116	struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
117	cycle_t cval, now;
118
119	if ((timer->cntv_ctl & ARCH_TIMER_CTRL_IT_MASK) ||
120		!(timer->cntv_ctl & ARCH_TIMER_CTRL_ENABLE))
121		return false;
122
123	cval = timer->cntv_cval;
124	now = kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff;
125
126	return cval <= now;
127}
128
129/**
130 * kvm_timer_flush_hwstate - prepare to move the virt timer to the cpu
131 * @vcpu: The vcpu pointer
132 *
133 * Disarm any pending soft timers, since the world-switch code will write the
134 * virtual timer state back to the physical CPU.
135 */
136void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu)
137{
138	struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
139
140	/*
141	 * We're about to run this vcpu again, so there is no need to
142	 * keep the background timer running, as we're about to
143	 * populate the CPU timer again.
144	 */
145	timer_disarm(timer);
146
147	/*
148	 * If the timer expired while we were not scheduled, now is the time
149	 * to inject it.
150	 */
151	if (kvm_timer_should_fire(vcpu))
152		kvm_timer_inject_irq(vcpu);
153}
154
155/**
156 * kvm_timer_sync_hwstate - sync timer state from cpu
157 * @vcpu: The vcpu pointer
158 *
159 * Check if the virtual timer was armed and either schedule a corresponding
160 * soft timer or inject directly if already expired.
161 */
162void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
163{
164	struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
165	cycle_t cval, now;
166	u64 ns;
167
168	BUG_ON(timer_is_armed(timer));
169
170	if (kvm_timer_should_fire(vcpu)) {
171		/*
172		 * Timer has already expired while we were not
173		 * looking. Inject the interrupt and carry on.
174		 */
175		kvm_timer_inject_irq(vcpu);
176		return;
177	}
178
179	cval = timer->cntv_cval;
180	now = kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff;
181
182	ns = cyclecounter_cyc2ns(timecounter->cc, cval - now, timecounter->mask,
183				 &timecounter->frac);
184	timer_arm(timer, ns);
185}
186
187void kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu,
188			  const struct kvm_irq_level *irq)
189{
190	struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
191
192	/*
193	 * The vcpu timer irq number cannot be determined in
194	 * kvm_timer_vcpu_init() because it is called much before
195	 * kvm_vcpu_set_target(). To handle this, we determine
196	 * vcpu timer irq number when the vcpu is reset.
197	 */
198	timer->irq = irq;
199}
200
201void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
202{
203	struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
204
205	INIT_WORK(&timer->expired, kvm_timer_inject_irq_work);
206	hrtimer_init(&timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
207	timer->timer.function = kvm_timer_expire;
208}
209
210static void kvm_timer_init_interrupt(void *info)
211{
212	enable_percpu_irq(host_vtimer_irq, 0);
213}
214
215int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
216{
217	struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
218
219	switch (regid) {
220	case KVM_REG_ARM_TIMER_CTL:
221		timer->cntv_ctl = value;
222		break;
223	case KVM_REG_ARM_TIMER_CNT:
224		vcpu->kvm->arch.timer.cntvoff = kvm_phys_timer_read() - value;
225		break;
226	case KVM_REG_ARM_TIMER_CVAL:
227		timer->cntv_cval = value;
228		break;
229	default:
230		return -1;
231	}
232	return 0;
233}
234
235u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid)
236{
237	struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
238
239	switch (regid) {
240	case KVM_REG_ARM_TIMER_CTL:
241		return timer->cntv_ctl;
242	case KVM_REG_ARM_TIMER_CNT:
243		return kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff;
244	case KVM_REG_ARM_TIMER_CVAL:
245		return timer->cntv_cval;
246	}
247	return (u64)-1;
248}
249
250static int kvm_timer_cpu_notify(struct notifier_block *self,
251				unsigned long action, void *cpu)
252{
253	switch (action) {
254	case CPU_STARTING:
255	case CPU_STARTING_FROZEN:
256		kvm_timer_init_interrupt(NULL);
257		break;
258	case CPU_DYING:
259	case CPU_DYING_FROZEN:
260		disable_percpu_irq(host_vtimer_irq);
261		break;
262	}
263
264	return NOTIFY_OK;
265}
266
267static struct notifier_block kvm_timer_cpu_nb = {
268	.notifier_call = kvm_timer_cpu_notify,
269};
270
271static const struct of_device_id arch_timer_of_match[] = {
272	{ .compatible	= "arm,armv7-timer",	},
273	{ .compatible	= "arm,armv8-timer",	},
274	{},
275};
276
277int kvm_timer_hyp_init(void)
278{
279	struct device_node *np;
280	unsigned int ppi;
281	int err;
282
283	timecounter = arch_timer_get_timecounter();
284	if (!timecounter)
285		return -ENODEV;
286
287	np = of_find_matching_node(NULL, arch_timer_of_match);
288	if (!np) {
289		kvm_err("kvm_arch_timer: can't find DT node\n");
290		return -ENODEV;
291	}
292
293	ppi = irq_of_parse_and_map(np, 2);
294	if (!ppi) {
295		kvm_err("kvm_arch_timer: no virtual timer interrupt\n");
296		err = -EINVAL;
297		goto out;
298	}
299
300	err = request_percpu_irq(ppi, kvm_arch_timer_handler,
301				 "kvm guest timer", kvm_get_running_vcpus());
302	if (err) {
303		kvm_err("kvm_arch_timer: can't request interrupt %d (%d)\n",
304			ppi, err);
305		goto out;
306	}
307
308	host_vtimer_irq = ppi;
309
310	err = __register_cpu_notifier(&kvm_timer_cpu_nb);
311	if (err) {
312		kvm_err("Cannot register timer CPU notifier\n");
313		goto out_free;
314	}
315
316	wqueue = create_singlethread_workqueue("kvm_arch_timer");
317	if (!wqueue) {
318		err = -ENOMEM;
319		goto out_free;
320	}
321
322	kvm_info("%s IRQ%d\n", np->name, ppi);
323	on_each_cpu(kvm_timer_init_interrupt, NULL, 1);
324
325	goto out;
326out_free:
327	free_percpu_irq(ppi, kvm_get_running_vcpus());
328out:
329	of_node_put(np);
330	return err;
331}
332
333void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
334{
335	struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
336
337	timer_disarm(timer);
338}
339
340void kvm_timer_enable(struct kvm *kvm)
341{
342	if (kvm->arch.timer.enabled)
343		return;
344
345	/*
346	 * There is a potential race here between VCPUs starting for the first
347	 * time, which may be enabling the timer multiple times.  That doesn't
348	 * hurt though, because we're just setting a variable to the same
349	 * variable that it already was.  The important thing is that all
350	 * VCPUs have the enabled variable set, before entering the guest, if
351	 * the arch timers are enabled.
352	 */
353	if (timecounter && wqueue)
354		kvm->arch.timer.enabled = 1;
355}
356
357void kvm_timer_init(struct kvm *kvm)
358{
359	kvm->arch.timer.cntvoff = kvm_phys_timer_read();
360}
361