blob: 07f6f9bfc1f2e64ff2f82fc9072d74ad79741e19 [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>
Christoffer Dallb452cb52016-06-04 15:41:00 +010023#include <linux/irq.h>
Christoffer Dall99a1db72017-05-02 20:19:15 +020024#include <linux/uaccess.h>
Marc Zyngier53e72402013-01-23 13:21:58 -050025
Mark Rutland372b7c12013-03-27 15:56:11 +000026#include <clocksource/arm_arch_timer.h>
Marc Zyngier53e72402013-01-23 13:21:58 -050027#include <asm/arch_timer.h>
Jintack Lim488f94d2016-12-01 14:32:05 -050028#include <asm/kvm_hyp.h>
Marc Zyngier53e72402013-01-23 13:21:58 -050029
Marc Zyngier7275acd2013-05-14 14:31:01 +010030#include <kvm/arm_vgic.h>
31#include <kvm/arm_arch_timer.h>
Marc Zyngier53e72402013-01-23 13:21:58 -050032
Christoffer Dalle21f0912015-08-30 13:57:20 +020033#include "trace.h"
34
Marc Zyngier53e72402013-01-23 13:21:58 -050035static struct timecounter *timecounter;
Anup Patel5ae7f872013-04-30 12:02:15 +053036static unsigned int host_vtimer_irq;
Marc Zyngiercabdc5c2016-08-16 15:03:02 +010037static u32 host_vtimer_irq_flags;
Marc Zyngier53e72402013-01-23 13:21:58 -050038
Christoffer Dall85e69ad2017-05-02 20:14:06 +020039static const struct kvm_irq_level default_ptimer_irq = {
40 .irq = 30,
41 .level = 1,
42};
43
44static const struct kvm_irq_level default_vtimer_irq = {
45 .irq = 27,
46 .level = 1,
47};
48
Marc Zyngier9b4a3002016-01-29 19:04:48 +000049void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
50{
Jintack Limfbb4aee2017-02-03 10:19:59 -050051 vcpu_vtimer(vcpu)->active_cleared_last = false;
Marc Zyngier9b4a3002016-01-29 19:04:48 +000052}
53
Jintack Lim7b6b4632017-02-03 10:20:08 -050054u64 kvm_phys_timer_read(void)
Marc Zyngier53e72402013-01-23 13:21:58 -050055{
56 return timecounter->cc->read(timecounter->cc);
57}
58
59static bool timer_is_armed(struct arch_timer_cpu *timer)
60{
61 return timer->armed;
62}
63
64/* timer_arm: as in "arm the timer", not as in ARM the company */
65static void timer_arm(struct arch_timer_cpu *timer, u64 ns)
66{
67 timer->armed = true;
68 hrtimer_start(&timer->timer, ktime_add_ns(ktime_get(), ns),
69 HRTIMER_MODE_ABS);
70}
71
72static void timer_disarm(struct arch_timer_cpu *timer)
73{
74 if (timer_is_armed(timer)) {
75 hrtimer_cancel(&timer->timer);
76 cancel_work_sync(&timer->expired);
77 timer->armed = false;
78 }
79}
80
Marc Zyngier53e72402013-01-23 13:21:58 -050081static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
82{
83 struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
84
85 /*
86 * We disable the timer in the world switch and let it be
87 * handled by kvm_timer_sync_hwstate(). Getting a timer
88 * interrupt at this point is a sure sign of some major
89 * breakage.
90 */
91 pr_warn("Unexpected interrupt %d on vcpu %p\n", irq, vcpu);
92 return IRQ_HANDLED;
93}
94
Christoffer Dall1a748472015-03-13 17:02:55 +000095/*
96 * Work function for handling the backup timer that we schedule when a vcpu is
97 * no longer running, but had a timer programmed to fire in the future.
98 */
Marc Zyngier53e72402013-01-23 13:21:58 -050099static void kvm_timer_inject_irq_work(struct work_struct *work)
100{
101 struct kvm_vcpu *vcpu;
102
103 vcpu = container_of(work, struct kvm_vcpu, arch.timer_cpu.expired);
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100104
Christoffer Dall1a748472015-03-13 17:02:55 +0000105 /*
106 * If the vcpu is blocked we want to wake it up so that it will see
107 * the timer has expired when entering the guest.
108 */
Andrew Jones1b6502e2017-06-04 14:44:01 +0200109 kvm_vcpu_wake_up(vcpu);
Marc Zyngier53e72402013-01-23 13:21:58 -0500110}
111
Jintack Lim9171fa22017-02-03 10:20:01 -0500112static u64 kvm_timer_compute_delta(struct arch_timer_context *timer_ctx)
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100113{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100114 u64 cval, now;
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100115
Jintack Lim9171fa22017-02-03 10:20:01 -0500116 cval = timer_ctx->cnt_cval;
117 now = kvm_phys_timer_read() - timer_ctx->cntvoff;
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100118
119 if (now < cval) {
120 u64 ns;
121
122 ns = cyclecounter_cyc2ns(timecounter->cc,
123 cval - now,
124 timecounter->mask,
125 &timecounter->frac);
126 return ns;
127 }
128
129 return 0;
130}
131
Jintack Limfb280e92017-02-03 10:20:05 -0500132static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx)
133{
134 return !(timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_IT_MASK) &&
135 (timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_ENABLE);
136}
137
138/*
139 * Returns the earliest expiration time in ns among guest timers.
140 * Note that it will return 0 if none of timers can fire.
141 */
142static u64 kvm_timer_earliest_exp(struct kvm_vcpu *vcpu)
143{
144 u64 min_virt = ULLONG_MAX, min_phys = ULLONG_MAX;
145 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
146 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
147
148 if (kvm_timer_irq_can_fire(vtimer))
149 min_virt = kvm_timer_compute_delta(vtimer);
150
151 if (kvm_timer_irq_can_fire(ptimer))
152 min_phys = kvm_timer_compute_delta(ptimer);
153
154 /* If none of timers can fire, then return 0 */
155 if ((min_virt == ULLONG_MAX) && (min_phys == ULLONG_MAX))
156 return 0;
157
158 return min(min_virt, min_phys);
159}
160
Marc Zyngier53e72402013-01-23 13:21:58 -0500161static enum hrtimer_restart kvm_timer_expire(struct hrtimer *hrt)
162{
163 struct arch_timer_cpu *timer;
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100164 struct kvm_vcpu *vcpu;
165 u64 ns;
166
Marc Zyngier53e72402013-01-23 13:21:58 -0500167 timer = container_of(hrt, struct arch_timer_cpu, timer);
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100168 vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
169
170 /*
171 * Check that the timer has really expired from the guest's
172 * PoV (NTP on the host may have forced it to expire
173 * early). If we should have slept longer, restart it.
174 */
Jintack Limfb280e92017-02-03 10:20:05 -0500175 ns = kvm_timer_earliest_exp(vcpu);
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100176 if (unlikely(ns)) {
177 hrtimer_forward_now(hrt, ns_to_ktime(ns));
178 return HRTIMER_RESTART;
179 }
180
Bhaktipriya Shridhar3706fea2016-08-30 23:29:51 +0530181 schedule_work(&timer->expired);
Marc Zyngier53e72402013-01-23 13:21:58 -0500182 return HRTIMER_NORESTART;
183}
184
Jintack Lim9171fa22017-02-03 10:20:01 -0500185bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx)
Christoffer Dall1a748472015-03-13 17:02:55 +0000186{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100187 u64 cval, now;
Christoffer Dall1a748472015-03-13 17:02:55 +0000188
Jintack Lim9171fa22017-02-03 10:20:01 -0500189 if (!kvm_timer_irq_can_fire(timer_ctx))
Christoffer Dall1a748472015-03-13 17:02:55 +0000190 return false;
191
Jintack Lim9171fa22017-02-03 10:20:01 -0500192 cval = timer_ctx->cnt_cval;
193 now = kvm_phys_timer_read() - timer_ctx->cntvoff;
Christoffer Dall1a748472015-03-13 17:02:55 +0000194
195 return cval <= now;
196}
197
Alexander Grafd9e13972016-09-27 21:08:06 +0200198/*
199 * Reflect the timer output level into the kvm_run structure
200 */
201void kvm_timer_update_run(struct kvm_vcpu *vcpu)
202{
203 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
204 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
205 struct kvm_sync_regs *regs = &vcpu->run->s.regs;
206
Alexander Grafd9e13972016-09-27 21:08:06 +0200207 /* Populate the device bitmap with the timer states */
208 regs->device_irq_level &= ~(KVM_ARM_DEV_EL1_VTIMER |
209 KVM_ARM_DEV_EL1_PTIMER);
210 if (vtimer->irq.level)
211 regs->device_irq_level |= KVM_ARM_DEV_EL1_VTIMER;
212 if (ptimer->irq.level)
213 regs->device_irq_level |= KVM_ARM_DEV_EL1_PTIMER;
214}
215
Jintack Lim9171fa22017-02-03 10:20:01 -0500216static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
217 struct arch_timer_context *timer_ctx)
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200218{
219 int ret;
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200220
Jintack Lim9171fa22017-02-03 10:20:01 -0500221 timer_ctx->active_cleared_last = false;
222 timer_ctx->irq.level = new_level;
223 trace_kvm_timer_update_irq(vcpu->vcpu_id, timer_ctx->irq.irq,
224 timer_ctx->irq.level);
Christoffer Dall11710de2017-02-01 11:03:45 +0100225
Alexander Grafd9e13972016-09-27 21:08:06 +0200226 if (likely(irqchip_in_kernel(vcpu->kvm))) {
227 ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
228 timer_ctx->irq.irq,
229 timer_ctx->irq.level);
230 WARN_ON(ret);
231 }
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200232}
233
234/*
235 * Check if there was a change in the timer state (should we raise or lower
236 * the line level to the GIC).
237 */
Christoffer Dallb22e7df2016-09-27 21:08:04 +0200238static void kvm_timer_update_state(struct kvm_vcpu *vcpu)
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200239{
240 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
Jintack Limfbb4aee2017-02-03 10:19:59 -0500241 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Jintack Lim58e0c972017-02-03 10:20:04 -0500242 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200243
244 /*
245 * If userspace modified the timer registers via SET_ONE_REG before
Jintack Limfbb4aee2017-02-03 10:19:59 -0500246 * the vgic was initialized, we mustn't set the vtimer->irq.level value
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200247 * because the guest would never see the interrupt. Instead wait
248 * until we call this function from kvm_timer_flush_hwstate.
249 */
Alexander Grafd9e13972016-09-27 21:08:06 +0200250 if (unlikely(!timer->enabled))
Christoffer Dallb22e7df2016-09-27 21:08:04 +0200251 return;
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200252
Jintack Lim9171fa22017-02-03 10:20:01 -0500253 if (kvm_timer_should_fire(vtimer) != vtimer->irq.level)
254 kvm_timer_update_irq(vcpu, !vtimer->irq.level, vtimer);
Andre Przywarab3aff6c2016-02-03 16:56:51 +0000255
Jintack Lim58e0c972017-02-03 10:20:04 -0500256 if (kvm_timer_should_fire(ptimer) != ptimer->irq.level)
257 kvm_timer_update_irq(vcpu, !ptimer->irq.level, ptimer);
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200258}
259
Jintack Limf242ada2017-02-03 10:20:06 -0500260/* Schedule the background timer for the emulated timer. */
261static void kvm_timer_emulate(struct kvm_vcpu *vcpu,
262 struct arch_timer_context *timer_ctx)
263{
264 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
265
266 if (kvm_timer_should_fire(timer_ctx))
267 return;
268
269 if (!kvm_timer_irq_can_fire(timer_ctx))
270 return;
271
272 /* The timer has not yet expired, schedule a background timer */
273 timer_arm(timer, kvm_timer_compute_delta(timer_ctx));
274}
275
Christoffer Dalld35268d2015-08-25 19:48:21 +0200276/*
277 * Schedule the background timer before calling kvm_vcpu_block, so that this
278 * thread is removed from its waitqueue and made runnable when there's a timer
279 * interrupt to handle.
280 */
281void kvm_timer_schedule(struct kvm_vcpu *vcpu)
282{
283 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
Jintack Lim9171fa22017-02-03 10:20:01 -0500284 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Jintack Limfb280e92017-02-03 10:20:05 -0500285 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
Christoffer Dalld35268d2015-08-25 19:48:21 +0200286
287 BUG_ON(timer_is_armed(timer));
288
289 /*
Jintack Limfb280e92017-02-03 10:20:05 -0500290 * No need to schedule a background timer if any guest timer has
Christoffer Dalld35268d2015-08-25 19:48:21 +0200291 * already expired, because kvm_vcpu_block will return before putting
292 * the thread to sleep.
293 */
Jintack Limfb280e92017-02-03 10:20:05 -0500294 if (kvm_timer_should_fire(vtimer) || kvm_timer_should_fire(ptimer))
Christoffer Dalld35268d2015-08-25 19:48:21 +0200295 return;
296
297 /*
Jintack Limfb280e92017-02-03 10:20:05 -0500298 * If both timers are not capable of raising interrupts (disabled or
Christoffer Dalld35268d2015-08-25 19:48:21 +0200299 * masked), then there's no more work for us to do.
300 */
Jintack Limfb280e92017-02-03 10:20:05 -0500301 if (!kvm_timer_irq_can_fire(vtimer) && !kvm_timer_irq_can_fire(ptimer))
Christoffer Dalld35268d2015-08-25 19:48:21 +0200302 return;
303
Jintack Limfb280e92017-02-03 10:20:05 -0500304 /*
305 * The guest timers have not yet expired, schedule a background timer.
306 * Set the earliest expiration time among the guest timers.
307 */
308 timer_arm(timer, kvm_timer_earliest_exp(vcpu));
Christoffer Dalld35268d2015-08-25 19:48:21 +0200309}
310
311void kvm_timer_unschedule(struct kvm_vcpu *vcpu)
312{
313 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
314 timer_disarm(timer);
315}
316
Alexander Grafd9e13972016-09-27 21:08:06 +0200317static void kvm_timer_flush_hwstate_vgic(struct kvm_vcpu *vcpu)
Marc Zyngier53e72402013-01-23 13:21:58 -0500318{
Jintack Limfbb4aee2017-02-03 10:19:59 -0500319 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Christoffer Dallcff92112015-10-16 12:41:21 +0200320 bool phys_active;
321 int ret;
Marc Zyngier53e72402013-01-23 13:21:58 -0500322
Christoffer Dallcff92112015-10-16 12:41:21 +0200323 /*
Christoffer Dall0e3dfda2015-11-24 16:23:05 +0100324 * If we enter the guest with the virtual input level to the VGIC
325 * asserted, then we have already told the VGIC what we need to, and
326 * we don't need to exit from the guest until the guest deactivates
327 * the already injected interrupt, so therefore we should set the
328 * hardware active state to prevent unnecessary exits from the guest.
329 *
330 * Also, if we enter the guest with the virtual timer interrupt active,
331 * then it must be active on the physical distributor, because we set
332 * the HW bit and the guest must be able to deactivate the virtual and
333 * physical interrupt at the same time.
334 *
335 * Conversely, if the virtual input level is deasserted and the virtual
336 * interrupt is not active, then always clear the hardware active state
337 * to ensure that hardware interrupts from the timer triggers a guest
338 * exit.
339 */
Jintack Limfbb4aee2017-02-03 10:19:59 -0500340 phys_active = vtimer->irq.level ||
341 kvm_vgic_map_is_active(vcpu, vtimer->irq.irq);
Christoffer Dallcff92112015-10-16 12:41:21 +0200342
Marc Zyngier9b4a3002016-01-29 19:04:48 +0000343 /*
344 * We want to avoid hitting the (re)distributor as much as
345 * possible, as this is a potentially expensive MMIO access
346 * (not to mention locks in the irq layer), and a solution for
347 * this is to cache the "active" state in memory.
348 *
349 * Things to consider: we cannot cache an "active set" state,
350 * because the HW can change this behind our back (it becomes
351 * "clear" in the HW). We must then restrict the caching to
352 * the "clear" state.
353 *
354 * The cache is invalidated on:
355 * - vcpu put, indicating that the HW cannot be trusted to be
356 * in a sane state on the next vcpu load,
357 * - any change in the interrupt state
358 *
359 * Usage conditions:
360 * - cached value is "active clear"
361 * - value to be programmed is "active clear"
362 */
Jintack Limfbb4aee2017-02-03 10:19:59 -0500363 if (vtimer->active_cleared_last && !phys_active)
Marc Zyngier9b4a3002016-01-29 19:04:48 +0000364 return;
365
Christoffer Dallb452cb52016-06-04 15:41:00 +0100366 ret = irq_set_irqchip_state(host_vtimer_irq,
Christoffer Dallcff92112015-10-16 12:41:21 +0200367 IRQCHIP_STATE_ACTIVE,
368 phys_active);
369 WARN_ON(ret);
Marc Zyngier9b4a3002016-01-29 19:04:48 +0000370
Jintack Limfbb4aee2017-02-03 10:19:59 -0500371 vtimer->active_cleared_last = !phys_active;
Marc Zyngier53e72402013-01-23 13:21:58 -0500372}
373
Alexander Grafd9e13972016-09-27 21:08:06 +0200374bool kvm_timer_should_notify_user(struct kvm_vcpu *vcpu)
375{
376 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
377 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
378 struct kvm_sync_regs *sregs = &vcpu->run->s.regs;
379 bool vlevel, plevel;
380
381 if (likely(irqchip_in_kernel(vcpu->kvm)))
382 return false;
383
384 vlevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_VTIMER;
385 plevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_PTIMER;
386
387 return vtimer->irq.level != vlevel ||
388 ptimer->irq.level != plevel;
389}
390
391static void kvm_timer_flush_hwstate_user(struct kvm_vcpu *vcpu)
392{
393 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
394
395 /*
396 * To prevent continuously exiting from the guest, we mask the
397 * physical interrupt such that the guest can make forward progress.
398 * Once we detect the output level being deasserted, we unmask the
399 * interrupt again so that we exit from the guest when the timer
400 * fires.
401 */
402 if (vtimer->irq.level)
403 disable_percpu_irq(host_vtimer_irq);
404 else
405 enable_percpu_irq(host_vtimer_irq, 0);
406}
407
408/**
409 * kvm_timer_flush_hwstate - prepare timers before running the vcpu
410 * @vcpu: The vcpu pointer
411 *
412 * Check if the virtual timer has expired while we were running in the host,
413 * and inject an interrupt if that was the case, making sure the timer is
414 * masked or disabled on the host so that we keep executing. Also schedule a
415 * software timer for the physical timer if it is enabled.
416 */
417void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu)
418{
419 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
420
421 if (unlikely(!timer->enabled))
422 return;
423
424 kvm_timer_update_state(vcpu);
425
426 /* Set the background timer for the physical timer emulation. */
427 kvm_timer_emulate(vcpu, vcpu_ptimer(vcpu));
428
429 if (unlikely(!irqchip_in_kernel(vcpu->kvm)))
430 kvm_timer_flush_hwstate_user(vcpu);
431 else
432 kvm_timer_flush_hwstate_vgic(vcpu);
433}
434
Marc Zyngier53e72402013-01-23 13:21:58 -0500435/**
436 * kvm_timer_sync_hwstate - sync timer state from cpu
437 * @vcpu: The vcpu pointer
438 *
Alexander Grafd9e13972016-09-27 21:08:06 +0200439 * Check if any of the timers have expired while we were running in the guest,
Christoffer Dalld35268d2015-08-25 19:48:21 +0200440 * and inject an interrupt if that was the case.
Marc Zyngier53e72402013-01-23 13:21:58 -0500441 */
442void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
443{
444 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
Marc Zyngier53e72402013-01-23 13:21:58 -0500445
Jintack Limf242ada2017-02-03 10:20:06 -0500446 /*
447 * This is to cancel the background timer for the physical timer
448 * emulation if it is set.
449 */
450 timer_disarm(timer);
Marc Zyngier53e72402013-01-23 13:21:58 -0500451
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200452 /*
453 * The guest could have modified the timer registers or the timer
454 * could have expired, update the timer state.
455 */
456 kvm_timer_update_state(vcpu);
Marc Zyngier53e72402013-01-23 13:21:58 -0500457}
458
Christoffer Dall85e69ad2017-05-02 20:14:06 +0200459int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu)
Anup Patel5ae7f872013-04-30 12:02:15 +0530460{
Jintack Limfbb4aee2017-02-03 10:19:59 -0500461 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Jintack Lima91d1852017-02-03 10:20:03 -0500462 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
Anup Patel5ae7f872013-04-30 12:02:15 +0530463
464 /*
Christoffer Dall4ad9e162015-09-04 16:24:39 +0200465 * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8
466 * and to 0 for ARMv7. We provide an implementation that always
467 * resets the timer to be disabled and unmasked and is compliant with
468 * the ARMv7 architecture.
469 */
Jintack Limfbb4aee2017-02-03 10:19:59 -0500470 vtimer->cnt_ctl = 0;
Jintack Lima91d1852017-02-03 10:20:03 -0500471 ptimer->cnt_ctl = 0;
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200472 kvm_timer_update_state(vcpu);
Christoffer Dall4ad9e162015-09-04 16:24:39 +0200473
Christoffer Dall41a54482016-05-18 16:26:00 +0100474 return 0;
Anup Patel5ae7f872013-04-30 12:02:15 +0530475}
476
Jintack Lim90de9432017-02-03 10:20:00 -0500477/* Make the updates of cntvoff for all vtimer contexts atomic */
478static void update_vtimer_cntvoff(struct kvm_vcpu *vcpu, u64 cntvoff)
479{
480 int i;
481 struct kvm *kvm = vcpu->kvm;
482 struct kvm_vcpu *tmp;
483
484 mutex_lock(&kvm->lock);
485 kvm_for_each_vcpu(i, tmp, kvm)
486 vcpu_vtimer(tmp)->cntvoff = cntvoff;
487
488 /*
489 * When called from the vcpu create path, the CPU being created is not
490 * included in the loop above, so we just set it here as well.
491 */
492 vcpu_vtimer(vcpu)->cntvoff = cntvoff;
493 mutex_unlock(&kvm->lock);
494}
495
Marc Zyngier53e72402013-01-23 13:21:58 -0500496void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
497{
498 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
Christoffer Dall85e69ad2017-05-02 20:14:06 +0200499 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
500 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
Marc Zyngier53e72402013-01-23 13:21:58 -0500501
Jintack Lim90de9432017-02-03 10:20:00 -0500502 /* Synchronize cntvoff across all vtimers of a VM. */
503 update_vtimer_cntvoff(vcpu, kvm_phys_timer_read());
Jintack Lima91d1852017-02-03 10:20:03 -0500504 vcpu_ptimer(vcpu)->cntvoff = 0;
Jintack Lim90de9432017-02-03 10:20:00 -0500505
Marc Zyngier53e72402013-01-23 13:21:58 -0500506 INIT_WORK(&timer->expired, kvm_timer_inject_irq_work);
507 hrtimer_init(&timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
508 timer->timer.function = kvm_timer_expire;
Christoffer Dall85e69ad2017-05-02 20:14:06 +0200509
510 vtimer->irq.irq = default_vtimer_irq.irq;
511 ptimer->irq.irq = default_ptimer_irq.irq;
Marc Zyngier53e72402013-01-23 13:21:58 -0500512}
513
514static void kvm_timer_init_interrupt(void *info)
515{
Marc Zyngiercabdc5c2016-08-16 15:03:02 +0100516 enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
Marc Zyngier53e72402013-01-23 13:21:58 -0500517}
518
Andre Przywara39735a32013-12-13 14:23:26 +0100519int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
520{
Jintack Limfbb4aee2017-02-03 10:19:59 -0500521 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Andre Przywara39735a32013-12-13 14:23:26 +0100522
523 switch (regid) {
524 case KVM_REG_ARM_TIMER_CTL:
Jintack Limfbb4aee2017-02-03 10:19:59 -0500525 vtimer->cnt_ctl = value;
Andre Przywara39735a32013-12-13 14:23:26 +0100526 break;
527 case KVM_REG_ARM_TIMER_CNT:
Jintack Lim90de9432017-02-03 10:20:00 -0500528 update_vtimer_cntvoff(vcpu, kvm_phys_timer_read() - value);
Andre Przywara39735a32013-12-13 14:23:26 +0100529 break;
530 case KVM_REG_ARM_TIMER_CVAL:
Jintack Limfbb4aee2017-02-03 10:19:59 -0500531 vtimer->cnt_cval = value;
Andre Przywara39735a32013-12-13 14:23:26 +0100532 break;
533 default:
534 return -1;
535 }
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200536
537 kvm_timer_update_state(vcpu);
Andre Przywara39735a32013-12-13 14:23:26 +0100538 return 0;
539}
540
541u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid)
542{
Jintack Limfbb4aee2017-02-03 10:19:59 -0500543 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Andre Przywara39735a32013-12-13 14:23:26 +0100544
545 switch (regid) {
546 case KVM_REG_ARM_TIMER_CTL:
Jintack Limfbb4aee2017-02-03 10:19:59 -0500547 return vtimer->cnt_ctl;
Andre Przywara39735a32013-12-13 14:23:26 +0100548 case KVM_REG_ARM_TIMER_CNT:
Jintack Lim90de9432017-02-03 10:20:00 -0500549 return kvm_phys_timer_read() - vtimer->cntvoff;
Andre Przywara39735a32013-12-13 14:23:26 +0100550 case KVM_REG_ARM_TIMER_CVAL:
Jintack Limfbb4aee2017-02-03 10:19:59 -0500551 return vtimer->cnt_cval;
Andre Przywara39735a32013-12-13 14:23:26 +0100552 }
553 return (u64)-1;
554}
Marc Zyngier53e72402013-01-23 13:21:58 -0500555
Richard Cochranb3c99502016-07-13 17:16:47 +0000556static int kvm_timer_starting_cpu(unsigned int cpu)
Marc Zyngier53e72402013-01-23 13:21:58 -0500557{
Richard Cochranb3c99502016-07-13 17:16:47 +0000558 kvm_timer_init_interrupt(NULL);
559 return 0;
Marc Zyngier53e72402013-01-23 13:21:58 -0500560}
561
Richard Cochranb3c99502016-07-13 17:16:47 +0000562static int kvm_timer_dying_cpu(unsigned int cpu)
563{
564 disable_percpu_irq(host_vtimer_irq);
565 return 0;
566}
Marc Zyngier53e72402013-01-23 13:21:58 -0500567
Marc Zyngier53e72402013-01-23 13:21:58 -0500568int kvm_timer_hyp_init(void)
569{
Julien Grall29c2d6f2016-04-11 16:32:58 +0100570 struct arch_timer_kvm_info *info;
Marc Zyngier53e72402013-01-23 13:21:58 -0500571 int err;
572
Julien Grall29c2d6f2016-04-11 16:32:58 +0100573 info = arch_timer_get_kvm_info();
574 timecounter = &info->timecounter;
Marc Zyngier53e72402013-01-23 13:21:58 -0500575
Christoffer Dall8e1a0472016-12-05 10:32:11 +0100576 if (!timecounter->cc) {
577 kvm_err("kvm_arch_timer: uninitialized timecounter\n");
578 return -ENODEV;
579 }
580
Julien Grall29c2d6f2016-04-11 16:32:58 +0100581 if (info->virtual_irq <= 0) {
582 kvm_err("kvm_arch_timer: invalid virtual timer IRQ: %d\n",
583 info->virtual_irq);
Marc Zyngier53e72402013-01-23 13:21:58 -0500584 return -ENODEV;
585 }
Julien Grall29c2d6f2016-04-11 16:32:58 +0100586 host_vtimer_irq = info->virtual_irq;
Marc Zyngier53e72402013-01-23 13:21:58 -0500587
Marc Zyngiercabdc5c2016-08-16 15:03:02 +0100588 host_vtimer_irq_flags = irq_get_trigger_type(host_vtimer_irq);
589 if (host_vtimer_irq_flags != IRQF_TRIGGER_HIGH &&
590 host_vtimer_irq_flags != IRQF_TRIGGER_LOW) {
591 kvm_err("Invalid trigger for IRQ%d, assuming level low\n",
592 host_vtimer_irq);
593 host_vtimer_irq_flags = IRQF_TRIGGER_LOW;
594 }
595
Julien Grall29c2d6f2016-04-11 16:32:58 +0100596 err = request_percpu_irq(host_vtimer_irq, kvm_arch_timer_handler,
Marc Zyngier53e72402013-01-23 13:21:58 -0500597 "kvm guest timer", kvm_get_running_vcpus());
598 if (err) {
599 kvm_err("kvm_arch_timer: can't request interrupt %d (%d)\n",
Julien Grall29c2d6f2016-04-11 16:32:58 +0100600 host_vtimer_irq, err);
Paolo Bonzini5d947a12016-09-08 12:45:59 +0200601 return err;
Marc Zyngier53e72402013-01-23 13:21:58 -0500602 }
603
Julien Grall29c2d6f2016-04-11 16:32:58 +0100604 kvm_info("virtual timer IRQ%d\n", host_vtimer_irq);
Marc Zyngier53e72402013-01-23 13:21:58 -0500605
Richard Cochranb3c99502016-07-13 17:16:47 +0000606 cpuhp_setup_state(CPUHP_AP_KVM_ARM_TIMER_STARTING,
Thomas Gleixner73c1b412016-12-21 20:19:54 +0100607 "kvm/arm/timer:starting", kvm_timer_starting_cpu,
Richard Cochranb3c99502016-07-13 17:16:47 +0000608 kvm_timer_dying_cpu);
Marc Zyngier53e72402013-01-23 13:21:58 -0500609 return err;
610}
611
612void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
613{
614 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
Jintack Limfbb4aee2017-02-03 10:19:59 -0500615 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Marc Zyngier53e72402013-01-23 13:21:58 -0500616
617 timer_disarm(timer);
Jintack Limfbb4aee2017-02-03 10:19:59 -0500618 kvm_vgic_unmap_phys_irq(vcpu, vtimer->irq.irq);
Marc Zyngier53e72402013-01-23 13:21:58 -0500619}
620
Christoffer Dallabcb8512017-05-04 13:32:53 +0200621static bool timer_irqs_are_valid(struct kvm_vcpu *vcpu)
Christoffer Dall99a1db72017-05-02 20:19:15 +0200622{
Christoffer Dall99a1db72017-05-02 20:19:15 +0200623 int vtimer_irq, ptimer_irq;
Christoffer Dallabcb8512017-05-04 13:32:53 +0200624 int i, ret;
Christoffer Dall99a1db72017-05-02 20:19:15 +0200625
Christoffer Dall99a1db72017-05-02 20:19:15 +0200626 vtimer_irq = vcpu_vtimer(vcpu)->irq.irq;
Christoffer Dallabcb8512017-05-04 13:32:53 +0200627 ret = kvm_vgic_set_owner(vcpu, vtimer_irq, vcpu_vtimer(vcpu));
628 if (ret)
Christoffer Dall99a1db72017-05-02 20:19:15 +0200629 return false;
630
Christoffer Dallabcb8512017-05-04 13:32:53 +0200631 ptimer_irq = vcpu_ptimer(vcpu)->irq.irq;
632 ret = kvm_vgic_set_owner(vcpu, ptimer_irq, vcpu_ptimer(vcpu));
633 if (ret)
634 return false;
635
636 kvm_for_each_vcpu(i, vcpu, vcpu->kvm) {
Christoffer Dall99a1db72017-05-02 20:19:15 +0200637 if (vcpu_vtimer(vcpu)->irq.irq != vtimer_irq ||
638 vcpu_ptimer(vcpu)->irq.irq != ptimer_irq)
639 return false;
640 }
641
642 return true;
643}
644
Christoffer Dall41a54482016-05-18 16:26:00 +0100645int kvm_timer_enable(struct kvm_vcpu *vcpu)
Marc Zyngier53e72402013-01-23 13:21:58 -0500646{
Christoffer Dall41a54482016-05-18 16:26:00 +0100647 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
Jintack Limfbb4aee2017-02-03 10:19:59 -0500648 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Christoffer Dall41a54482016-05-18 16:26:00 +0100649 struct irq_desc *desc;
650 struct irq_data *data;
651 int phys_irq;
652 int ret;
653
654 if (timer->enabled)
655 return 0;
656
Alexander Grafd9e13972016-09-27 21:08:06 +0200657 /* Without a VGIC we do not map virtual IRQs to physical IRQs */
658 if (!irqchip_in_kernel(vcpu->kvm))
659 goto no_vgic;
660
661 if (!vgic_initialized(vcpu->kvm))
662 return -ENODEV;
663
Christoffer Dallabcb8512017-05-04 13:32:53 +0200664 if (!timer_irqs_are_valid(vcpu)) {
Christoffer Dall99a1db72017-05-02 20:19:15 +0200665 kvm_debug("incorrectly configured timer irqs\n");
666 return -EINVAL;
667 }
668
Christoffer Dall41a54482016-05-18 16:26:00 +0100669 /*
670 * Find the physical IRQ number corresponding to the host_vtimer_irq
671 */
672 desc = irq_to_desc(host_vtimer_irq);
673 if (!desc) {
674 kvm_err("%s: no interrupt descriptor\n", __func__);
675 return -EINVAL;
676 }
677
678 data = irq_desc_get_irq_data(desc);
679 while (data->parent_data)
680 data = data->parent_data;
681
682 phys_irq = data->hwirq;
683
684 /*
685 * Tell the VGIC that the virtual interrupt is tied to a
686 * physical interrupt. We do that once per VCPU.
687 */
Jintack Limfbb4aee2017-02-03 10:19:59 -0500688 ret = kvm_vgic_map_phys_irq(vcpu, vtimer->irq.irq, phys_irq);
Christoffer Dall41a54482016-05-18 16:26:00 +0100689 if (ret)
690 return ret;
691
Alexander Grafd9e13972016-09-27 21:08:06 +0200692no_vgic:
Longpeng(Mike)fd5ebf92016-11-09 10:50:14 +0800693 timer->enabled = 1;
Christoffer Dall41a54482016-05-18 16:26:00 +0100694 return 0;
Christoffer Dall05971122014-12-12 21:19:23 +0100695}
696
Jintack Lim488f94d2016-12-01 14:32:05 -0500697/*
698 * On VHE system, we only need to configure trap on physical timer and counter
699 * accesses in EL0 and EL1 once, not for every world switch.
700 * The host kernel runs at EL2 with HCR_EL2.TGE == 1,
701 * and this makes those bits have no effect for the host kernel execution.
702 */
703void kvm_timer_init_vhe(void)
704{
705 /* When HCR_EL2.E2H ==1, EL1PCEN and EL1PCTEN are shifted by 10 */
706 u32 cnthctl_shift = 10;
707 u64 val;
708
709 /*
710 * Disallow physical timer access for the guest.
711 * Physical counter access is allowed.
712 */
713 val = read_sysreg(cnthctl_el2);
714 val &= ~(CNTHCTL_EL1PCEN << cnthctl_shift);
715 val |= (CNTHCTL_EL1PCTEN << cnthctl_shift);
716 write_sysreg(val, cnthctl_el2);
717}
Christoffer Dall99a1db72017-05-02 20:19:15 +0200718
719static void set_timer_irqs(struct kvm *kvm, int vtimer_irq, int ptimer_irq)
720{
721 struct kvm_vcpu *vcpu;
722 int i;
723
724 kvm_for_each_vcpu(i, vcpu, kvm) {
725 vcpu_vtimer(vcpu)->irq.irq = vtimer_irq;
726 vcpu_ptimer(vcpu)->irq.irq = ptimer_irq;
727 }
728}
729
730int kvm_arm_timer_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
731{
732 int __user *uaddr = (int __user *)(long)attr->addr;
733 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
734 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
735 int irq;
736
737 if (!irqchip_in_kernel(vcpu->kvm))
738 return -EINVAL;
739
740 if (get_user(irq, uaddr))
741 return -EFAULT;
742
743 if (!(irq_is_ppi(irq)))
744 return -EINVAL;
745
746 if (vcpu->arch.timer_cpu.enabled)
747 return -EBUSY;
748
749 switch (attr->attr) {
750 case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
751 set_timer_irqs(vcpu->kvm, irq, ptimer->irq.irq);
752 break;
753 case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
754 set_timer_irqs(vcpu->kvm, vtimer->irq.irq, irq);
755 break;
756 default:
757 return -ENXIO;
758 }
759
760 return 0;
761}
762
763int kvm_arm_timer_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
764{
765 int __user *uaddr = (int __user *)(long)attr->addr;
766 struct arch_timer_context *timer;
767 int irq;
768
769 switch (attr->attr) {
770 case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
771 timer = vcpu_vtimer(vcpu);
772 break;
773 case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
774 timer = vcpu_ptimer(vcpu);
775 break;
776 default:
777 return -ENXIO;
778 }
779
780 irq = timer->irq.irq;
781 return put_user(irq, uaddr);
782}
783
784int kvm_arm_timer_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
785{
786 switch (attr->attr) {
787 case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
788 case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
789 return 0;
790 }
791
792 return -ENXIO;
793}