blob: 22323019119530f5845287e65de2e6c59187de10 [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
Christoffer Dall8409a062017-06-17 01:09:19 -070059static void soft_timer_start(struct hrtimer *hrt, u64 ns)
Marc Zyngier53e72402013-01-23 13:21:58 -050060{
Christoffer Dall8409a062017-06-17 01:09:19 -070061 hrtimer_start(hrt, ktime_add_ns(ktime_get(), ns),
Marc Zyngier53e72402013-01-23 13:21:58 -050062 HRTIMER_MODE_ABS);
63}
64
Christoffer Dall8409a062017-06-17 01:09:19 -070065static void soft_timer_cancel(struct hrtimer *hrt, struct work_struct *work)
Marc Zyngier53e72402013-01-23 13:21:58 -050066{
Christoffer Dall8409a062017-06-17 01:09:19 -070067 hrtimer_cancel(hrt);
68 cancel_work_sync(work);
Marc Zyngier53e72402013-01-23 13:21:58 -050069}
70
Marc Zyngier53e72402013-01-23 13:21:58 -050071static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
72{
73 struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
74
75 /*
76 * We disable the timer in the world switch and let it be
77 * handled by kvm_timer_sync_hwstate(). Getting a timer
78 * interrupt at this point is a sure sign of some major
79 * breakage.
80 */
81 pr_warn("Unexpected interrupt %d on vcpu %p\n", irq, vcpu);
82 return IRQ_HANDLED;
83}
84
Christoffer Dall1a748472015-03-13 17:02:55 +000085/*
86 * Work function for handling the backup timer that we schedule when a vcpu is
87 * no longer running, but had a timer programmed to fire in the future.
88 */
Marc Zyngier53e72402013-01-23 13:21:58 -050089static void kvm_timer_inject_irq_work(struct work_struct *work)
90{
91 struct kvm_vcpu *vcpu;
92
93 vcpu = container_of(work, struct kvm_vcpu, arch.timer_cpu.expired);
Marc Zyngier1c5631c2016-04-06 09:37:22 +010094
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 */
Andrew Jones1b6502e2017-06-04 14:44:01 +020099 kvm_vcpu_wake_up(vcpu);
Marc Zyngier53e72402013-01-23 13:21:58 -0500100}
101
Jintack Lim9171fa22017-02-03 10:20:01 -0500102static u64 kvm_timer_compute_delta(struct arch_timer_context *timer_ctx)
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100103{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100104 u64 cval, now;
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100105
Jintack Lim9171fa22017-02-03 10:20:01 -0500106 cval = timer_ctx->cnt_cval;
107 now = kvm_phys_timer_read() - timer_ctx->cntvoff;
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100108
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
Jintack Limfb280e92017-02-03 10:20:05 -0500122static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx)
123{
124 return !(timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_IT_MASK) &&
125 (timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_ENABLE);
126}
127
128/*
129 * Returns the earliest expiration time in ns among guest timers.
130 * Note that it will return 0 if none of timers can fire.
131 */
132static u64 kvm_timer_earliest_exp(struct kvm_vcpu *vcpu)
133{
134 u64 min_virt = ULLONG_MAX, min_phys = ULLONG_MAX;
135 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
136 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
137
138 if (kvm_timer_irq_can_fire(vtimer))
139 min_virt = kvm_timer_compute_delta(vtimer);
140
141 if (kvm_timer_irq_can_fire(ptimer))
142 min_phys = kvm_timer_compute_delta(ptimer);
143
144 /* If none of timers can fire, then return 0 */
145 if ((min_virt == ULLONG_MAX) && (min_phys == ULLONG_MAX))
146 return 0;
147
148 return min(min_virt, min_phys);
149}
150
Marc Zyngier53e72402013-01-23 13:21:58 -0500151static enum hrtimer_restart kvm_timer_expire(struct hrtimer *hrt)
152{
153 struct arch_timer_cpu *timer;
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100154 struct kvm_vcpu *vcpu;
155 u64 ns;
156
Marc Zyngier53e72402013-01-23 13:21:58 -0500157 timer = container_of(hrt, struct arch_timer_cpu, timer);
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100158 vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
159
160 /*
161 * Check that the timer has really expired from the guest's
162 * PoV (NTP on the host may have forced it to expire
163 * early). If we should have slept longer, restart it.
164 */
Jintack Limfb280e92017-02-03 10:20:05 -0500165 ns = kvm_timer_earliest_exp(vcpu);
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100166 if (unlikely(ns)) {
167 hrtimer_forward_now(hrt, ns_to_ktime(ns));
168 return HRTIMER_RESTART;
169 }
170
Bhaktipriya Shridhar3706fea2016-08-30 23:29:51 +0530171 schedule_work(&timer->expired);
Marc Zyngier53e72402013-01-23 13:21:58 -0500172 return HRTIMER_NORESTART;
173}
174
Jintack Lim9171fa22017-02-03 10:20:01 -0500175bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx)
Christoffer Dall1a748472015-03-13 17:02:55 +0000176{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100177 u64 cval, now;
Christoffer Dall1a748472015-03-13 17:02:55 +0000178
Jintack Lim9171fa22017-02-03 10:20:01 -0500179 if (!kvm_timer_irq_can_fire(timer_ctx))
Christoffer Dall1a748472015-03-13 17:02:55 +0000180 return false;
181
Jintack Lim9171fa22017-02-03 10:20:01 -0500182 cval = timer_ctx->cnt_cval;
183 now = kvm_phys_timer_read() - timer_ctx->cntvoff;
Christoffer Dall1a748472015-03-13 17:02:55 +0000184
185 return cval <= now;
186}
187
Alexander Grafd9e13972016-09-27 21:08:06 +0200188/*
189 * Reflect the timer output level into the kvm_run structure
190 */
191void kvm_timer_update_run(struct kvm_vcpu *vcpu)
192{
193 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
194 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
195 struct kvm_sync_regs *regs = &vcpu->run->s.regs;
196
Alexander Grafd9e13972016-09-27 21:08:06 +0200197 /* Populate the device bitmap with the timer states */
198 regs->device_irq_level &= ~(KVM_ARM_DEV_EL1_VTIMER |
199 KVM_ARM_DEV_EL1_PTIMER);
200 if (vtimer->irq.level)
201 regs->device_irq_level |= KVM_ARM_DEV_EL1_VTIMER;
202 if (ptimer->irq.level)
203 regs->device_irq_level |= KVM_ARM_DEV_EL1_PTIMER;
204}
205
Jintack Lim9171fa22017-02-03 10:20:01 -0500206static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
207 struct arch_timer_context *timer_ctx)
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200208{
209 int ret;
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200210
Jintack Lim9171fa22017-02-03 10:20:01 -0500211 timer_ctx->active_cleared_last = false;
212 timer_ctx->irq.level = new_level;
213 trace_kvm_timer_update_irq(vcpu->vcpu_id, timer_ctx->irq.irq,
214 timer_ctx->irq.level);
Christoffer Dall11710de2017-02-01 11:03:45 +0100215
Alexander Grafd9e13972016-09-27 21:08:06 +0200216 if (likely(irqchip_in_kernel(vcpu->kvm))) {
217 ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
218 timer_ctx->irq.irq,
Christoffer Dallcb3f0ad2017-05-16 12:41:18 +0200219 timer_ctx->irq.level,
220 timer_ctx);
Alexander Grafd9e13972016-09-27 21:08:06 +0200221 WARN_ON(ret);
222 }
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200223}
224
225/*
226 * Check if there was a change in the timer state (should we raise or lower
227 * the line level to the GIC).
228 */
Christoffer Dallb22e7df2016-09-27 21:08:04 +0200229static void kvm_timer_update_state(struct kvm_vcpu *vcpu)
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200230{
231 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
Jintack Limfbb4aee2017-02-03 10:19:59 -0500232 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Jintack Lim58e0c972017-02-03 10:20:04 -0500233 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200234
235 /*
236 * If userspace modified the timer registers via SET_ONE_REG before
Jintack Limfbb4aee2017-02-03 10:19:59 -0500237 * the vgic was initialized, we mustn't set the vtimer->irq.level value
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200238 * because the guest would never see the interrupt. Instead wait
239 * until we call this function from kvm_timer_flush_hwstate.
240 */
Alexander Grafd9e13972016-09-27 21:08:06 +0200241 if (unlikely(!timer->enabled))
Christoffer Dallb22e7df2016-09-27 21:08:04 +0200242 return;
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200243
Jintack Lim9171fa22017-02-03 10:20:01 -0500244 if (kvm_timer_should_fire(vtimer) != vtimer->irq.level)
245 kvm_timer_update_irq(vcpu, !vtimer->irq.level, vtimer);
Andre Przywarab3aff6c2016-02-03 16:56:51 +0000246
Jintack Lim58e0c972017-02-03 10:20:04 -0500247 if (kvm_timer_should_fire(ptimer) != ptimer->irq.level)
248 kvm_timer_update_irq(vcpu, !ptimer->irq.level, ptimer);
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200249}
250
Jintack Limf242ada2017-02-03 10:20:06 -0500251/* Schedule the background timer for the emulated timer. */
252static void kvm_timer_emulate(struct kvm_vcpu *vcpu,
253 struct arch_timer_context *timer_ctx)
254{
255 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
256
257 if (kvm_timer_should_fire(timer_ctx))
258 return;
259
260 if (!kvm_timer_irq_can_fire(timer_ctx))
261 return;
262
263 /* The timer has not yet expired, schedule a background timer */
Christoffer Dall8409a062017-06-17 01:09:19 -0700264 soft_timer_start(&timer->timer, kvm_timer_compute_delta(timer_ctx));
Jintack Limf242ada2017-02-03 10:20:06 -0500265}
266
Christoffer Dalld35268d2015-08-25 19:48:21 +0200267/*
268 * Schedule the background timer before calling kvm_vcpu_block, so that this
269 * thread is removed from its waitqueue and made runnable when there's a timer
270 * interrupt to handle.
271 */
272void kvm_timer_schedule(struct kvm_vcpu *vcpu)
273{
274 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
Jintack Lim9171fa22017-02-03 10:20:01 -0500275 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Jintack Limfb280e92017-02-03 10:20:05 -0500276 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
Christoffer Dalld35268d2015-08-25 19:48:21 +0200277
Christoffer Dalld35268d2015-08-25 19:48:21 +0200278 /*
Jintack Limfb280e92017-02-03 10:20:05 -0500279 * No need to schedule a background timer if any guest timer has
Christoffer Dalld35268d2015-08-25 19:48:21 +0200280 * already expired, because kvm_vcpu_block will return before putting
281 * the thread to sleep.
282 */
Jintack Limfb280e92017-02-03 10:20:05 -0500283 if (kvm_timer_should_fire(vtimer) || kvm_timer_should_fire(ptimer))
Christoffer Dalld35268d2015-08-25 19:48:21 +0200284 return;
285
286 /*
Jintack Limfb280e92017-02-03 10:20:05 -0500287 * If both timers are not capable of raising interrupts (disabled or
Christoffer Dalld35268d2015-08-25 19:48:21 +0200288 * masked), then there's no more work for us to do.
289 */
Jintack Limfb280e92017-02-03 10:20:05 -0500290 if (!kvm_timer_irq_can_fire(vtimer) && !kvm_timer_irq_can_fire(ptimer))
Christoffer Dalld35268d2015-08-25 19:48:21 +0200291 return;
292
Jintack Limfb280e92017-02-03 10:20:05 -0500293 /*
294 * The guest timers have not yet expired, schedule a background timer.
295 * Set the earliest expiration time among the guest timers.
296 */
Christoffer Dall8409a062017-06-17 01:09:19 -0700297 soft_timer_start(&timer->timer, kvm_timer_earliest_exp(vcpu));
Christoffer Dalld35268d2015-08-25 19:48:21 +0200298}
299
300void kvm_timer_unschedule(struct kvm_vcpu *vcpu)
301{
302 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
Christoffer Dall8409a062017-06-17 01:09:19 -0700303
304 soft_timer_cancel(&timer->timer, &timer->expired);
Christoffer Dalld35268d2015-08-25 19:48:21 +0200305}
306
Alexander Grafd9e13972016-09-27 21:08:06 +0200307static void kvm_timer_flush_hwstate_vgic(struct kvm_vcpu *vcpu)
Marc Zyngier53e72402013-01-23 13:21:58 -0500308{
Jintack Limfbb4aee2017-02-03 10:19:59 -0500309 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Christoffer Dallcff92112015-10-16 12:41:21 +0200310 bool phys_active;
311 int ret;
Marc Zyngier53e72402013-01-23 13:21:58 -0500312
Christoffer Dallcff92112015-10-16 12:41:21 +0200313 /*
Christoffer Dall0e3dfda2015-11-24 16:23:05 +0100314 * If we enter the guest with the virtual input level to the VGIC
315 * asserted, then we have already told the VGIC what we need to, and
316 * we don't need to exit from the guest until the guest deactivates
317 * the already injected interrupt, so therefore we should set the
318 * hardware active state to prevent unnecessary exits from the guest.
319 *
320 * Also, if we enter the guest with the virtual timer interrupt active,
321 * then it must be active on the physical distributor, because we set
322 * the HW bit and the guest must be able to deactivate the virtual and
323 * physical interrupt at the same time.
324 *
325 * Conversely, if the virtual input level is deasserted and the virtual
326 * interrupt is not active, then always clear the hardware active state
327 * to ensure that hardware interrupts from the timer triggers a guest
328 * exit.
329 */
Jintack Limfbb4aee2017-02-03 10:19:59 -0500330 phys_active = vtimer->irq.level ||
331 kvm_vgic_map_is_active(vcpu, vtimer->irq.irq);
Christoffer Dallcff92112015-10-16 12:41:21 +0200332
Marc Zyngier9b4a3002016-01-29 19:04:48 +0000333 /*
334 * We want to avoid hitting the (re)distributor as much as
335 * possible, as this is a potentially expensive MMIO access
336 * (not to mention locks in the irq layer), and a solution for
337 * this is to cache the "active" state in memory.
338 *
339 * Things to consider: we cannot cache an "active set" state,
340 * because the HW can change this behind our back (it becomes
341 * "clear" in the HW). We must then restrict the caching to
342 * the "clear" state.
343 *
344 * The cache is invalidated on:
345 * - vcpu put, indicating that the HW cannot be trusted to be
346 * in a sane state on the next vcpu load,
347 * - any change in the interrupt state
348 *
349 * Usage conditions:
350 * - cached value is "active clear"
351 * - value to be programmed is "active clear"
352 */
Jintack Limfbb4aee2017-02-03 10:19:59 -0500353 if (vtimer->active_cleared_last && !phys_active)
Marc Zyngier9b4a3002016-01-29 19:04:48 +0000354 return;
355
Christoffer Dallb452cb52016-06-04 15:41:00 +0100356 ret = irq_set_irqchip_state(host_vtimer_irq,
Christoffer Dallcff92112015-10-16 12:41:21 +0200357 IRQCHIP_STATE_ACTIVE,
358 phys_active);
359 WARN_ON(ret);
Marc Zyngier9b4a3002016-01-29 19:04:48 +0000360
Jintack Limfbb4aee2017-02-03 10:19:59 -0500361 vtimer->active_cleared_last = !phys_active;
Marc Zyngier53e72402013-01-23 13:21:58 -0500362}
363
Alexander Grafd9e13972016-09-27 21:08:06 +0200364bool kvm_timer_should_notify_user(struct kvm_vcpu *vcpu)
365{
366 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
367 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
368 struct kvm_sync_regs *sregs = &vcpu->run->s.regs;
369 bool vlevel, plevel;
370
371 if (likely(irqchip_in_kernel(vcpu->kvm)))
372 return false;
373
374 vlevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_VTIMER;
375 plevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_PTIMER;
376
377 return vtimer->irq.level != vlevel ||
378 ptimer->irq.level != plevel;
379}
380
381static void kvm_timer_flush_hwstate_user(struct kvm_vcpu *vcpu)
382{
383 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
384
385 /*
386 * To prevent continuously exiting from the guest, we mask the
387 * physical interrupt such that the guest can make forward progress.
388 * Once we detect the output level being deasserted, we unmask the
389 * interrupt again so that we exit from the guest when the timer
390 * fires.
391 */
392 if (vtimer->irq.level)
393 disable_percpu_irq(host_vtimer_irq);
394 else
395 enable_percpu_irq(host_vtimer_irq, 0);
396}
397
398/**
399 * kvm_timer_flush_hwstate - prepare timers before running the vcpu
400 * @vcpu: The vcpu pointer
401 *
402 * Check if the virtual timer has expired while we were running in the host,
403 * and inject an interrupt if that was the case, making sure the timer is
404 * masked or disabled on the host so that we keep executing. Also schedule a
405 * software timer for the physical timer if it is enabled.
406 */
407void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu)
408{
409 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
410
411 if (unlikely(!timer->enabled))
412 return;
413
414 kvm_timer_update_state(vcpu);
415
416 /* Set the background timer for the physical timer emulation. */
417 kvm_timer_emulate(vcpu, vcpu_ptimer(vcpu));
418
419 if (unlikely(!irqchip_in_kernel(vcpu->kvm)))
420 kvm_timer_flush_hwstate_user(vcpu);
421 else
422 kvm_timer_flush_hwstate_vgic(vcpu);
423}
424
Marc Zyngier53e72402013-01-23 13:21:58 -0500425/**
426 * kvm_timer_sync_hwstate - sync timer state from cpu
427 * @vcpu: The vcpu pointer
428 *
Alexander Grafd9e13972016-09-27 21:08:06 +0200429 * Check if any of the timers have expired while we were running in the guest,
Christoffer Dalld35268d2015-08-25 19:48:21 +0200430 * and inject an interrupt if that was the case.
Marc Zyngier53e72402013-01-23 13:21:58 -0500431 */
432void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
433{
434 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
Marc Zyngier53e72402013-01-23 13:21:58 -0500435
Jintack Limf242ada2017-02-03 10:20:06 -0500436 /*
437 * This is to cancel the background timer for the physical timer
438 * emulation if it is set.
439 */
Christoffer Dall8409a062017-06-17 01:09:19 -0700440 soft_timer_cancel(&timer->timer, &timer->expired);
Marc Zyngier53e72402013-01-23 13:21:58 -0500441
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200442 /*
443 * The guest could have modified the timer registers or the timer
444 * could have expired, update the timer state.
445 */
446 kvm_timer_update_state(vcpu);
Marc Zyngier53e72402013-01-23 13:21:58 -0500447}
448
Christoffer Dall85e69ad2017-05-02 20:14:06 +0200449int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu)
Anup Patel5ae7f872013-04-30 12:02:15 +0530450{
Jintack Limfbb4aee2017-02-03 10:19:59 -0500451 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Jintack Lima91d1852017-02-03 10:20:03 -0500452 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
Anup Patel5ae7f872013-04-30 12:02:15 +0530453
454 /*
Christoffer Dall4ad9e162015-09-04 16:24:39 +0200455 * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8
456 * and to 0 for ARMv7. We provide an implementation that always
457 * resets the timer to be disabled and unmasked and is compliant with
458 * the ARMv7 architecture.
459 */
Jintack Limfbb4aee2017-02-03 10:19:59 -0500460 vtimer->cnt_ctl = 0;
Jintack Lima91d1852017-02-03 10:20:03 -0500461 ptimer->cnt_ctl = 0;
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200462 kvm_timer_update_state(vcpu);
Christoffer Dall4ad9e162015-09-04 16:24:39 +0200463
Christoffer Dall41a54482016-05-18 16:26:00 +0100464 return 0;
Anup Patel5ae7f872013-04-30 12:02:15 +0530465}
466
Jintack Lim90de9432017-02-03 10:20:00 -0500467/* Make the updates of cntvoff for all vtimer contexts atomic */
468static void update_vtimer_cntvoff(struct kvm_vcpu *vcpu, u64 cntvoff)
469{
470 int i;
471 struct kvm *kvm = vcpu->kvm;
472 struct kvm_vcpu *tmp;
473
474 mutex_lock(&kvm->lock);
475 kvm_for_each_vcpu(i, tmp, kvm)
476 vcpu_vtimer(tmp)->cntvoff = cntvoff;
477
478 /*
479 * When called from the vcpu create path, the CPU being created is not
480 * included in the loop above, so we just set it here as well.
481 */
482 vcpu_vtimer(vcpu)->cntvoff = cntvoff;
483 mutex_unlock(&kvm->lock);
484}
485
Marc Zyngier53e72402013-01-23 13:21:58 -0500486void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
487{
488 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
Christoffer Dall85e69ad2017-05-02 20:14:06 +0200489 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
490 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
Marc Zyngier53e72402013-01-23 13:21:58 -0500491
Jintack Lim90de9432017-02-03 10:20:00 -0500492 /* Synchronize cntvoff across all vtimers of a VM. */
493 update_vtimer_cntvoff(vcpu, kvm_phys_timer_read());
Jintack Lima91d1852017-02-03 10:20:03 -0500494 vcpu_ptimer(vcpu)->cntvoff = 0;
Jintack Lim90de9432017-02-03 10:20:00 -0500495
Marc Zyngier53e72402013-01-23 13:21:58 -0500496 INIT_WORK(&timer->expired, kvm_timer_inject_irq_work);
497 hrtimer_init(&timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
498 timer->timer.function = kvm_timer_expire;
Christoffer Dall85e69ad2017-05-02 20:14:06 +0200499
500 vtimer->irq.irq = default_vtimer_irq.irq;
501 ptimer->irq.irq = default_ptimer_irq.irq;
Marc Zyngier53e72402013-01-23 13:21:58 -0500502}
503
504static void kvm_timer_init_interrupt(void *info)
505{
Marc Zyngiercabdc5c2016-08-16 15:03:02 +0100506 enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
Marc Zyngier53e72402013-01-23 13:21:58 -0500507}
508
Andre Przywara39735a32013-12-13 14:23:26 +0100509int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
510{
Jintack Limfbb4aee2017-02-03 10:19:59 -0500511 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Andre Przywara39735a32013-12-13 14:23:26 +0100512
513 switch (regid) {
514 case KVM_REG_ARM_TIMER_CTL:
Jintack Limfbb4aee2017-02-03 10:19:59 -0500515 vtimer->cnt_ctl = value;
Andre Przywara39735a32013-12-13 14:23:26 +0100516 break;
517 case KVM_REG_ARM_TIMER_CNT:
Jintack Lim90de9432017-02-03 10:20:00 -0500518 update_vtimer_cntvoff(vcpu, kvm_phys_timer_read() - value);
Andre Przywara39735a32013-12-13 14:23:26 +0100519 break;
520 case KVM_REG_ARM_TIMER_CVAL:
Jintack Limfbb4aee2017-02-03 10:19:59 -0500521 vtimer->cnt_cval = value;
Andre Przywara39735a32013-12-13 14:23:26 +0100522 break;
523 default:
524 return -1;
525 }
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200526
527 kvm_timer_update_state(vcpu);
Andre Przywara39735a32013-12-13 14:23:26 +0100528 return 0;
529}
530
531u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid)
532{
Jintack Limfbb4aee2017-02-03 10:19:59 -0500533 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Andre Przywara39735a32013-12-13 14:23:26 +0100534
535 switch (regid) {
536 case KVM_REG_ARM_TIMER_CTL:
Jintack Limfbb4aee2017-02-03 10:19:59 -0500537 return vtimer->cnt_ctl;
Andre Przywara39735a32013-12-13 14:23:26 +0100538 case KVM_REG_ARM_TIMER_CNT:
Jintack Lim90de9432017-02-03 10:20:00 -0500539 return kvm_phys_timer_read() - vtimer->cntvoff;
Andre Przywara39735a32013-12-13 14:23:26 +0100540 case KVM_REG_ARM_TIMER_CVAL:
Jintack Limfbb4aee2017-02-03 10:19:59 -0500541 return vtimer->cnt_cval;
Andre Przywara39735a32013-12-13 14:23:26 +0100542 }
543 return (u64)-1;
544}
Marc Zyngier53e72402013-01-23 13:21:58 -0500545
Richard Cochranb3c99502016-07-13 17:16:47 +0000546static int kvm_timer_starting_cpu(unsigned int cpu)
Marc Zyngier53e72402013-01-23 13:21:58 -0500547{
Richard Cochranb3c99502016-07-13 17:16:47 +0000548 kvm_timer_init_interrupt(NULL);
549 return 0;
Marc Zyngier53e72402013-01-23 13:21:58 -0500550}
551
Richard Cochranb3c99502016-07-13 17:16:47 +0000552static int kvm_timer_dying_cpu(unsigned int cpu)
553{
554 disable_percpu_irq(host_vtimer_irq);
555 return 0;
556}
Marc Zyngier53e72402013-01-23 13:21:58 -0500557
Marc Zyngier53e72402013-01-23 13:21:58 -0500558int kvm_timer_hyp_init(void)
559{
Julien Grall29c2d6f2016-04-11 16:32:58 +0100560 struct arch_timer_kvm_info *info;
Marc Zyngier53e72402013-01-23 13:21:58 -0500561 int err;
562
Julien Grall29c2d6f2016-04-11 16:32:58 +0100563 info = arch_timer_get_kvm_info();
564 timecounter = &info->timecounter;
Marc Zyngier53e72402013-01-23 13:21:58 -0500565
Christoffer Dall8e1a0472016-12-05 10:32:11 +0100566 if (!timecounter->cc) {
567 kvm_err("kvm_arch_timer: uninitialized timecounter\n");
568 return -ENODEV;
569 }
570
Julien Grall29c2d6f2016-04-11 16:32:58 +0100571 if (info->virtual_irq <= 0) {
572 kvm_err("kvm_arch_timer: invalid virtual timer IRQ: %d\n",
573 info->virtual_irq);
Marc Zyngier53e72402013-01-23 13:21:58 -0500574 return -ENODEV;
575 }
Julien Grall29c2d6f2016-04-11 16:32:58 +0100576 host_vtimer_irq = info->virtual_irq;
Marc Zyngier53e72402013-01-23 13:21:58 -0500577
Marc Zyngiercabdc5c2016-08-16 15:03:02 +0100578 host_vtimer_irq_flags = irq_get_trigger_type(host_vtimer_irq);
579 if (host_vtimer_irq_flags != IRQF_TRIGGER_HIGH &&
580 host_vtimer_irq_flags != IRQF_TRIGGER_LOW) {
581 kvm_err("Invalid trigger for IRQ%d, assuming level low\n",
582 host_vtimer_irq);
583 host_vtimer_irq_flags = IRQF_TRIGGER_LOW;
584 }
585
Julien Grall29c2d6f2016-04-11 16:32:58 +0100586 err = request_percpu_irq(host_vtimer_irq, kvm_arch_timer_handler,
Marc Zyngier53e72402013-01-23 13:21:58 -0500587 "kvm guest timer", kvm_get_running_vcpus());
588 if (err) {
589 kvm_err("kvm_arch_timer: can't request interrupt %d (%d)\n",
Julien Grall29c2d6f2016-04-11 16:32:58 +0100590 host_vtimer_irq, err);
Paolo Bonzini5d947a12016-09-08 12:45:59 +0200591 return err;
Marc Zyngier53e72402013-01-23 13:21:58 -0500592 }
593
Julien Grall29c2d6f2016-04-11 16:32:58 +0100594 kvm_info("virtual timer IRQ%d\n", host_vtimer_irq);
Marc Zyngier53e72402013-01-23 13:21:58 -0500595
Richard Cochranb3c99502016-07-13 17:16:47 +0000596 cpuhp_setup_state(CPUHP_AP_KVM_ARM_TIMER_STARTING,
Thomas Gleixner73c1b412016-12-21 20:19:54 +0100597 "kvm/arm/timer:starting", kvm_timer_starting_cpu,
Richard Cochranb3c99502016-07-13 17:16:47 +0000598 kvm_timer_dying_cpu);
Marc Zyngier53e72402013-01-23 13:21:58 -0500599 return err;
600}
601
602void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
603{
604 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
Jintack Limfbb4aee2017-02-03 10:19:59 -0500605 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Marc Zyngier53e72402013-01-23 13:21:58 -0500606
Christoffer Dall8409a062017-06-17 01:09:19 -0700607 soft_timer_cancel(&timer->timer, &timer->expired);
Jintack Limfbb4aee2017-02-03 10:19:59 -0500608 kvm_vgic_unmap_phys_irq(vcpu, vtimer->irq.irq);
Marc Zyngier53e72402013-01-23 13:21:58 -0500609}
610
Christoffer Dallabcb8512017-05-04 13:32:53 +0200611static bool timer_irqs_are_valid(struct kvm_vcpu *vcpu)
Christoffer Dall99a1db72017-05-02 20:19:15 +0200612{
Christoffer Dall99a1db72017-05-02 20:19:15 +0200613 int vtimer_irq, ptimer_irq;
Christoffer Dallabcb8512017-05-04 13:32:53 +0200614 int i, ret;
Christoffer Dall99a1db72017-05-02 20:19:15 +0200615
Christoffer Dall99a1db72017-05-02 20:19:15 +0200616 vtimer_irq = vcpu_vtimer(vcpu)->irq.irq;
Christoffer Dallabcb8512017-05-04 13:32:53 +0200617 ret = kvm_vgic_set_owner(vcpu, vtimer_irq, vcpu_vtimer(vcpu));
618 if (ret)
Christoffer Dall99a1db72017-05-02 20:19:15 +0200619 return false;
620
Christoffer Dallabcb8512017-05-04 13:32:53 +0200621 ptimer_irq = vcpu_ptimer(vcpu)->irq.irq;
622 ret = kvm_vgic_set_owner(vcpu, ptimer_irq, vcpu_ptimer(vcpu));
623 if (ret)
624 return false;
625
626 kvm_for_each_vcpu(i, vcpu, vcpu->kvm) {
Christoffer Dall99a1db72017-05-02 20:19:15 +0200627 if (vcpu_vtimer(vcpu)->irq.irq != vtimer_irq ||
628 vcpu_ptimer(vcpu)->irq.irq != ptimer_irq)
629 return false;
630 }
631
632 return true;
633}
634
Christoffer Dall41a54482016-05-18 16:26:00 +0100635int kvm_timer_enable(struct kvm_vcpu *vcpu)
Marc Zyngier53e72402013-01-23 13:21:58 -0500636{
Christoffer Dall41a54482016-05-18 16:26:00 +0100637 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
Jintack Limfbb4aee2017-02-03 10:19:59 -0500638 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Christoffer Dall41a54482016-05-18 16:26:00 +0100639 struct irq_desc *desc;
640 struct irq_data *data;
641 int phys_irq;
642 int ret;
643
644 if (timer->enabled)
645 return 0;
646
Alexander Grafd9e13972016-09-27 21:08:06 +0200647 /* Without a VGIC we do not map virtual IRQs to physical IRQs */
648 if (!irqchip_in_kernel(vcpu->kvm))
649 goto no_vgic;
650
651 if (!vgic_initialized(vcpu->kvm))
652 return -ENODEV;
653
Christoffer Dallabcb8512017-05-04 13:32:53 +0200654 if (!timer_irqs_are_valid(vcpu)) {
Christoffer Dall99a1db72017-05-02 20:19:15 +0200655 kvm_debug("incorrectly configured timer irqs\n");
656 return -EINVAL;
657 }
658
Christoffer Dall41a54482016-05-18 16:26:00 +0100659 /*
660 * Find the physical IRQ number corresponding to the host_vtimer_irq
661 */
662 desc = irq_to_desc(host_vtimer_irq);
663 if (!desc) {
664 kvm_err("%s: no interrupt descriptor\n", __func__);
665 return -EINVAL;
666 }
667
668 data = irq_desc_get_irq_data(desc);
669 while (data->parent_data)
670 data = data->parent_data;
671
672 phys_irq = data->hwirq;
673
674 /*
675 * Tell the VGIC that the virtual interrupt is tied to a
676 * physical interrupt. We do that once per VCPU.
677 */
Jintack Limfbb4aee2017-02-03 10:19:59 -0500678 ret = kvm_vgic_map_phys_irq(vcpu, vtimer->irq.irq, phys_irq);
Christoffer Dall41a54482016-05-18 16:26:00 +0100679 if (ret)
680 return ret;
681
Alexander Grafd9e13972016-09-27 21:08:06 +0200682no_vgic:
Longpeng(Mike)fd5ebf92016-11-09 10:50:14 +0800683 timer->enabled = 1;
Christoffer Dall41a54482016-05-18 16:26:00 +0100684 return 0;
Christoffer Dall05971122014-12-12 21:19:23 +0100685}
686
Jintack Lim488f94d2016-12-01 14:32:05 -0500687/*
688 * On VHE system, we only need to configure trap on physical timer and counter
689 * accesses in EL0 and EL1 once, not for every world switch.
690 * The host kernel runs at EL2 with HCR_EL2.TGE == 1,
691 * and this makes those bits have no effect for the host kernel execution.
692 */
693void kvm_timer_init_vhe(void)
694{
695 /* When HCR_EL2.E2H ==1, EL1PCEN and EL1PCTEN are shifted by 10 */
696 u32 cnthctl_shift = 10;
697 u64 val;
698
699 /*
700 * Disallow physical timer access for the guest.
701 * Physical counter access is allowed.
702 */
703 val = read_sysreg(cnthctl_el2);
704 val &= ~(CNTHCTL_EL1PCEN << cnthctl_shift);
705 val |= (CNTHCTL_EL1PCTEN << cnthctl_shift);
706 write_sysreg(val, cnthctl_el2);
707}
Christoffer Dall99a1db72017-05-02 20:19:15 +0200708
709static void set_timer_irqs(struct kvm *kvm, int vtimer_irq, int ptimer_irq)
710{
711 struct kvm_vcpu *vcpu;
712 int i;
713
714 kvm_for_each_vcpu(i, vcpu, kvm) {
715 vcpu_vtimer(vcpu)->irq.irq = vtimer_irq;
716 vcpu_ptimer(vcpu)->irq.irq = ptimer_irq;
717 }
718}
719
720int kvm_arm_timer_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
721{
722 int __user *uaddr = (int __user *)(long)attr->addr;
723 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
724 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
725 int irq;
726
727 if (!irqchip_in_kernel(vcpu->kvm))
728 return -EINVAL;
729
730 if (get_user(irq, uaddr))
731 return -EFAULT;
732
733 if (!(irq_is_ppi(irq)))
734 return -EINVAL;
735
736 if (vcpu->arch.timer_cpu.enabled)
737 return -EBUSY;
738
739 switch (attr->attr) {
740 case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
741 set_timer_irqs(vcpu->kvm, irq, ptimer->irq.irq);
742 break;
743 case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
744 set_timer_irqs(vcpu->kvm, vtimer->irq.irq, irq);
745 break;
746 default:
747 return -ENXIO;
748 }
749
750 return 0;
751}
752
753int kvm_arm_timer_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
754{
755 int __user *uaddr = (int __user *)(long)attr->addr;
756 struct arch_timer_context *timer;
757 int irq;
758
759 switch (attr->attr) {
760 case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
761 timer = vcpu_vtimer(vcpu);
762 break;
763 case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
764 timer = vcpu_ptimer(vcpu);
765 break;
766 default:
767 return -ENXIO;
768 }
769
770 irq = timer->irq.irq;
771 return put_user(irq, uaddr);
772}
773
774int kvm_arm_timer_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
775{
776 switch (attr->attr) {
777 case KVM_ARM_VCPU_TIMER_IRQ_VTIMER:
778 case KVM_ARM_VCPU_TIMER_IRQ_PTIMER:
779 return 0;
780 }
781
782 return -ENXIO;
783}