blob: 35d7100e0815c4fc34acdebc17e794dfd569dbcf [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>
Marc Zyngier53e72402013-01-23 13:21:58 -050024
Mark Rutland372b7c12013-03-27 15:56:11 +000025#include <clocksource/arm_arch_timer.h>
Marc Zyngier53e72402013-01-23 13:21:58 -050026#include <asm/arch_timer.h>
Jintack Lim488f94d2016-12-01 14:32:05 -050027#include <asm/kvm_hyp.h>
Marc Zyngier53e72402013-01-23 13:21:58 -050028
Marc Zyngier7275acd2013-05-14 14:31:01 +010029#include <kvm/arm_vgic.h>
30#include <kvm/arm_arch_timer.h>
Marc Zyngier53e72402013-01-23 13:21:58 -050031
Christoffer Dalle21f0912015-08-30 13:57:20 +020032#include "trace.h"
33
Marc Zyngier53e72402013-01-23 13:21:58 -050034static struct timecounter *timecounter;
Anup Patel5ae7f872013-04-30 12:02:15 +053035static unsigned int host_vtimer_irq;
Marc Zyngiercabdc5c2016-08-16 15:03:02 +010036static u32 host_vtimer_irq_flags;
Marc Zyngier53e72402013-01-23 13:21:58 -050037
Marc Zyngier9b4a3002016-01-29 19:04:48 +000038void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
39{
Jintack Limfbb4aee2017-02-03 10:19:59 -050040 vcpu_vtimer(vcpu)->active_cleared_last = false;
Marc Zyngier9b4a3002016-01-29 19:04:48 +000041}
42
Jintack Lim7b6b4632017-02-03 10:20:08 -050043u64 kvm_phys_timer_read(void)
Marc Zyngier53e72402013-01-23 13:21:58 -050044{
45 return timecounter->cc->read(timecounter->cc);
46}
47
48static bool timer_is_armed(struct arch_timer_cpu *timer)
49{
50 return timer->armed;
51}
52
53/* timer_arm: as in "arm the timer", not as in ARM the company */
54static void timer_arm(struct arch_timer_cpu *timer, u64 ns)
55{
56 timer->armed = true;
57 hrtimer_start(&timer->timer, ktime_add_ns(ktime_get(), ns),
58 HRTIMER_MODE_ABS);
59}
60
61static void timer_disarm(struct arch_timer_cpu *timer)
62{
63 if (timer_is_armed(timer)) {
64 hrtimer_cancel(&timer->timer);
65 cancel_work_sync(&timer->expired);
66 timer->armed = false;
67 }
68}
69
Marc Zyngier53e72402013-01-23 13:21:58 -050070static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
71{
72 struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
73
74 /*
75 * We disable the timer in the world switch and let it be
76 * handled by kvm_timer_sync_hwstate(). Getting a timer
77 * interrupt at this point is a sure sign of some major
78 * breakage.
79 */
80 pr_warn("Unexpected interrupt %d on vcpu %p\n", irq, vcpu);
81 return IRQ_HANDLED;
82}
83
Christoffer Dall1a748472015-03-13 17:02:55 +000084/*
85 * Work function for handling the backup timer that we schedule when a vcpu is
86 * no longer running, but had a timer programmed to fire in the future.
87 */
Marc Zyngier53e72402013-01-23 13:21:58 -050088static void kvm_timer_inject_irq_work(struct work_struct *work)
89{
90 struct kvm_vcpu *vcpu;
91
92 vcpu = container_of(work, struct kvm_vcpu, arch.timer_cpu.expired);
Marc Zyngier1c5631c2016-04-06 09:37:22 +010093
Christoffer Dall1a748472015-03-13 17:02:55 +000094 /*
95 * If the vcpu is blocked we want to wake it up so that it will see
96 * the timer has expired when entering the guest.
97 */
98 kvm_vcpu_kick(vcpu);
Marc Zyngier53e72402013-01-23 13:21:58 -050099}
100
Jintack Lim9171fa22017-02-03 10:20:01 -0500101static u64 kvm_timer_compute_delta(struct arch_timer_context *timer_ctx)
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100102{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100103 u64 cval, now;
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100104
Jintack Lim9171fa22017-02-03 10:20:01 -0500105 cval = timer_ctx->cnt_cval;
106 now = kvm_phys_timer_read() - timer_ctx->cntvoff;
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100107
108 if (now < cval) {
109 u64 ns;
110
111 ns = cyclecounter_cyc2ns(timecounter->cc,
112 cval - now,
113 timecounter->mask,
114 &timecounter->frac);
115 return ns;
116 }
117
118 return 0;
119}
120
Jintack Limfb280e92017-02-03 10:20:05 -0500121static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx)
122{
123 return !(timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_IT_MASK) &&
124 (timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_ENABLE);
125}
126
127/*
128 * Returns the earliest expiration time in ns among guest timers.
129 * Note that it will return 0 if none of timers can fire.
130 */
131static u64 kvm_timer_earliest_exp(struct kvm_vcpu *vcpu)
132{
133 u64 min_virt = ULLONG_MAX, min_phys = ULLONG_MAX;
134 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
135 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
136
137 if (kvm_timer_irq_can_fire(vtimer))
138 min_virt = kvm_timer_compute_delta(vtimer);
139
140 if (kvm_timer_irq_can_fire(ptimer))
141 min_phys = kvm_timer_compute_delta(ptimer);
142
143 /* If none of timers can fire, then return 0 */
144 if ((min_virt == ULLONG_MAX) && (min_phys == ULLONG_MAX))
145 return 0;
146
147 return min(min_virt, min_phys);
148}
149
Marc Zyngier53e72402013-01-23 13:21:58 -0500150static enum hrtimer_restart kvm_timer_expire(struct hrtimer *hrt)
151{
152 struct arch_timer_cpu *timer;
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100153 struct kvm_vcpu *vcpu;
154 u64 ns;
155
Marc Zyngier53e72402013-01-23 13:21:58 -0500156 timer = container_of(hrt, struct arch_timer_cpu, timer);
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100157 vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
158
159 /*
160 * Check that the timer has really expired from the guest's
161 * PoV (NTP on the host may have forced it to expire
162 * early). If we should have slept longer, restart it.
163 */
Jintack Limfb280e92017-02-03 10:20:05 -0500164 ns = kvm_timer_earliest_exp(vcpu);
Marc Zyngier1c5631c2016-04-06 09:37:22 +0100165 if (unlikely(ns)) {
166 hrtimer_forward_now(hrt, ns_to_ktime(ns));
167 return HRTIMER_RESTART;
168 }
169
Bhaktipriya Shridhar3706fea2016-08-30 23:29:51 +0530170 schedule_work(&timer->expired);
Marc Zyngier53e72402013-01-23 13:21:58 -0500171 return HRTIMER_NORESTART;
172}
173
Jintack Lim9171fa22017-02-03 10:20:01 -0500174bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx)
Christoffer Dall1a748472015-03-13 17:02:55 +0000175{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100176 u64 cval, now;
Christoffer Dall1a748472015-03-13 17:02:55 +0000177
Jintack Lim9171fa22017-02-03 10:20:01 -0500178 if (!kvm_timer_irq_can_fire(timer_ctx))
Christoffer Dall1a748472015-03-13 17:02:55 +0000179 return false;
180
Jintack Lim9171fa22017-02-03 10:20:01 -0500181 cval = timer_ctx->cnt_cval;
182 now = kvm_phys_timer_read() - timer_ctx->cntvoff;
Christoffer Dall1a748472015-03-13 17:02:55 +0000183
184 return cval <= now;
185}
186
Jintack Lim9171fa22017-02-03 10:20:01 -0500187static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
188 struct arch_timer_context *timer_ctx)
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200189{
190 int ret;
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200191
192 BUG_ON(!vgic_initialized(vcpu->kvm));
193
Jintack Lim9171fa22017-02-03 10:20:01 -0500194 timer_ctx->active_cleared_last = false;
195 timer_ctx->irq.level = new_level;
196 trace_kvm_timer_update_irq(vcpu->vcpu_id, timer_ctx->irq.irq,
197 timer_ctx->irq.level);
Christoffer Dall11710de2017-02-01 11:03:45 +0100198
Jintack Lim9171fa22017-02-03 10:20:01 -0500199 ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id, timer_ctx->irq.irq,
200 timer_ctx->irq.level);
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200201 WARN_ON(ret);
202}
203
204/*
205 * Check if there was a change in the timer state (should we raise or lower
206 * the line level to the GIC).
207 */
Andre Przywarab3aff6c2016-02-03 16:56:51 +0000208static int kvm_timer_update_state(struct kvm_vcpu *vcpu)
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200209{
210 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
Jintack Limfbb4aee2017-02-03 10:19:59 -0500211 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Jintack Lim58e0c972017-02-03 10:20:04 -0500212 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200213
214 /*
215 * If userspace modified the timer registers via SET_ONE_REG before
Jintack Limfbb4aee2017-02-03 10:19:59 -0500216 * the vgic was initialized, we mustn't set the vtimer->irq.level value
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200217 * because the guest would never see the interrupt. Instead wait
218 * until we call this function from kvm_timer_flush_hwstate.
219 */
Christoffer Dall41a54482016-05-18 16:26:00 +0100220 if (!vgic_initialized(vcpu->kvm) || !timer->enabled)
Andre Przywarab3aff6c2016-02-03 16:56:51 +0000221 return -ENODEV;
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200222
Jintack Lim9171fa22017-02-03 10:20:01 -0500223 if (kvm_timer_should_fire(vtimer) != vtimer->irq.level)
224 kvm_timer_update_irq(vcpu, !vtimer->irq.level, vtimer);
Andre Przywarab3aff6c2016-02-03 16:56:51 +0000225
Jintack Lim58e0c972017-02-03 10:20:04 -0500226 if (kvm_timer_should_fire(ptimer) != ptimer->irq.level)
227 kvm_timer_update_irq(vcpu, !ptimer->irq.level, ptimer);
228
Andre Przywarab3aff6c2016-02-03 16:56:51 +0000229 return 0;
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200230}
231
Jintack Limf242ada2017-02-03 10:20:06 -0500232/* Schedule the background timer for the emulated timer. */
233static void kvm_timer_emulate(struct kvm_vcpu *vcpu,
234 struct arch_timer_context *timer_ctx)
235{
236 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
237
238 if (kvm_timer_should_fire(timer_ctx))
239 return;
240
241 if (!kvm_timer_irq_can_fire(timer_ctx))
242 return;
243
244 /* The timer has not yet expired, schedule a background timer */
245 timer_arm(timer, kvm_timer_compute_delta(timer_ctx));
246}
247
Christoffer Dalld35268d2015-08-25 19:48:21 +0200248/*
249 * Schedule the background timer before calling kvm_vcpu_block, so that this
250 * thread is removed from its waitqueue and made runnable when there's a timer
251 * interrupt to handle.
252 */
253void kvm_timer_schedule(struct kvm_vcpu *vcpu)
254{
255 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
Jintack Lim9171fa22017-02-03 10:20:01 -0500256 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Jintack Limfb280e92017-02-03 10:20:05 -0500257 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
Christoffer Dalld35268d2015-08-25 19:48:21 +0200258
259 BUG_ON(timer_is_armed(timer));
260
261 /*
Jintack Limfb280e92017-02-03 10:20:05 -0500262 * No need to schedule a background timer if any guest timer has
Christoffer Dalld35268d2015-08-25 19:48:21 +0200263 * already expired, because kvm_vcpu_block will return before putting
264 * the thread to sleep.
265 */
Jintack Limfb280e92017-02-03 10:20:05 -0500266 if (kvm_timer_should_fire(vtimer) || kvm_timer_should_fire(ptimer))
Christoffer Dalld35268d2015-08-25 19:48:21 +0200267 return;
268
269 /*
Jintack Limfb280e92017-02-03 10:20:05 -0500270 * If both timers are not capable of raising interrupts (disabled or
Christoffer Dalld35268d2015-08-25 19:48:21 +0200271 * masked), then there's no more work for us to do.
272 */
Jintack Limfb280e92017-02-03 10:20:05 -0500273 if (!kvm_timer_irq_can_fire(vtimer) && !kvm_timer_irq_can_fire(ptimer))
Christoffer Dalld35268d2015-08-25 19:48:21 +0200274 return;
275
Jintack Limfb280e92017-02-03 10:20:05 -0500276 /*
277 * The guest timers have not yet expired, schedule a background timer.
278 * Set the earliest expiration time among the guest timers.
279 */
280 timer_arm(timer, kvm_timer_earliest_exp(vcpu));
Christoffer Dalld35268d2015-08-25 19:48:21 +0200281}
282
283void kvm_timer_unschedule(struct kvm_vcpu *vcpu)
284{
285 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
286 timer_disarm(timer);
287}
288
Marc Zyngier53e72402013-01-23 13:21:58 -0500289/**
290 * kvm_timer_flush_hwstate - prepare to move the virt timer to the cpu
291 * @vcpu: The vcpu pointer
292 *
Christoffer Dalld35268d2015-08-25 19:48:21 +0200293 * Check if the virtual timer has expired while we were running in the host,
294 * and inject an interrupt if that was the case.
Marc Zyngier53e72402013-01-23 13:21:58 -0500295 */
296void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu)
297{
Jintack Limfbb4aee2017-02-03 10:19:59 -0500298 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Christoffer Dallcff92112015-10-16 12:41:21 +0200299 bool phys_active;
300 int ret;
Marc Zyngier53e72402013-01-23 13:21:58 -0500301
Andre Przywarab3aff6c2016-02-03 16:56:51 +0000302 if (kvm_timer_update_state(vcpu))
303 return;
Christoffer Dallcff92112015-10-16 12:41:21 +0200304
Jintack Limf242ada2017-02-03 10:20:06 -0500305 /* Set the background timer for the physical timer emulation. */
306 kvm_timer_emulate(vcpu, vcpu_ptimer(vcpu));
307
Christoffer Dallcff92112015-10-16 12:41:21 +0200308 /*
Christoffer Dall0e3dfda2015-11-24 16:23:05 +0100309 * If we enter the guest with the virtual input level to the VGIC
310 * asserted, then we have already told the VGIC what we need to, and
311 * we don't need to exit from the guest until the guest deactivates
312 * the already injected interrupt, so therefore we should set the
313 * hardware active state to prevent unnecessary exits from the guest.
314 *
315 * Also, if we enter the guest with the virtual timer interrupt active,
316 * then it must be active on the physical distributor, because we set
317 * the HW bit and the guest must be able to deactivate the virtual and
318 * physical interrupt at the same time.
319 *
320 * Conversely, if the virtual input level is deasserted and the virtual
321 * interrupt is not active, then always clear the hardware active state
322 * to ensure that hardware interrupts from the timer triggers a guest
323 * exit.
324 */
Jintack Limfbb4aee2017-02-03 10:19:59 -0500325 phys_active = vtimer->irq.level ||
326 kvm_vgic_map_is_active(vcpu, vtimer->irq.irq);
Christoffer Dallcff92112015-10-16 12:41:21 +0200327
Marc Zyngier9b4a3002016-01-29 19:04:48 +0000328 /*
329 * We want to avoid hitting the (re)distributor as much as
330 * possible, as this is a potentially expensive MMIO access
331 * (not to mention locks in the irq layer), and a solution for
332 * this is to cache the "active" state in memory.
333 *
334 * Things to consider: we cannot cache an "active set" state,
335 * because the HW can change this behind our back (it becomes
336 * "clear" in the HW). We must then restrict the caching to
337 * the "clear" state.
338 *
339 * The cache is invalidated on:
340 * - vcpu put, indicating that the HW cannot be trusted to be
341 * in a sane state on the next vcpu load,
342 * - any change in the interrupt state
343 *
344 * Usage conditions:
345 * - cached value is "active clear"
346 * - value to be programmed is "active clear"
347 */
Jintack Limfbb4aee2017-02-03 10:19:59 -0500348 if (vtimer->active_cleared_last && !phys_active)
Marc Zyngier9b4a3002016-01-29 19:04:48 +0000349 return;
350
Christoffer Dallb452cb52016-06-04 15:41:00 +0100351 ret = irq_set_irqchip_state(host_vtimer_irq,
Christoffer Dallcff92112015-10-16 12:41:21 +0200352 IRQCHIP_STATE_ACTIVE,
353 phys_active);
354 WARN_ON(ret);
Marc Zyngier9b4a3002016-01-29 19:04:48 +0000355
Jintack Limfbb4aee2017-02-03 10:19:59 -0500356 vtimer->active_cleared_last = !phys_active;
Marc Zyngier53e72402013-01-23 13:21:58 -0500357}
358
359/**
360 * kvm_timer_sync_hwstate - sync timer state from cpu
361 * @vcpu: The vcpu pointer
362 *
Christoffer Dalld35268d2015-08-25 19:48:21 +0200363 * Check if the virtual timer has expired while we were running in the guest,
364 * and inject an interrupt if that was the case.
Marc Zyngier53e72402013-01-23 13:21:58 -0500365 */
366void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
367{
368 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
Marc Zyngier53e72402013-01-23 13:21:58 -0500369
Jintack Limf242ada2017-02-03 10:20:06 -0500370 /*
371 * This is to cancel the background timer for the physical timer
372 * emulation if it is set.
373 */
374 timer_disarm(timer);
Marc Zyngier53e72402013-01-23 13:21:58 -0500375
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200376 /*
377 * The guest could have modified the timer registers or the timer
378 * could have expired, update the timer state.
379 */
380 kvm_timer_update_state(vcpu);
Marc Zyngier53e72402013-01-23 13:21:58 -0500381}
382
Marc Zyngierf120cd62014-06-23 13:59:13 +0100383int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu,
Jintack Lima91d1852017-02-03 10:20:03 -0500384 const struct kvm_irq_level *virt_irq,
385 const struct kvm_irq_level *phys_irq)
Anup Patel5ae7f872013-04-30 12:02:15 +0530386{
Jintack Limfbb4aee2017-02-03 10:19:59 -0500387 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Jintack Lima91d1852017-02-03 10:20:03 -0500388 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
Anup Patel5ae7f872013-04-30 12:02:15 +0530389
390 /*
391 * The vcpu timer irq number cannot be determined in
392 * kvm_timer_vcpu_init() because it is called much before
393 * kvm_vcpu_set_target(). To handle this, we determine
394 * vcpu timer irq number when the vcpu is reset.
395 */
Jintack Lima91d1852017-02-03 10:20:03 -0500396 vtimer->irq.irq = virt_irq->irq;
397 ptimer->irq.irq = phys_irq->irq;
Marc Zyngierf120cd62014-06-23 13:59:13 +0100398
399 /*
Christoffer Dall4ad9e162015-09-04 16:24:39 +0200400 * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8
401 * and to 0 for ARMv7. We provide an implementation that always
402 * resets the timer to be disabled and unmasked and is compliant with
403 * the ARMv7 architecture.
404 */
Jintack Limfbb4aee2017-02-03 10:19:59 -0500405 vtimer->cnt_ctl = 0;
Jintack Lima91d1852017-02-03 10:20:03 -0500406 ptimer->cnt_ctl = 0;
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200407 kvm_timer_update_state(vcpu);
Christoffer Dall4ad9e162015-09-04 16:24:39 +0200408
Christoffer Dall41a54482016-05-18 16:26:00 +0100409 return 0;
Anup Patel5ae7f872013-04-30 12:02:15 +0530410}
411
Jintack Lim90de9432017-02-03 10:20:00 -0500412/* Make the updates of cntvoff for all vtimer contexts atomic */
413static void update_vtimer_cntvoff(struct kvm_vcpu *vcpu, u64 cntvoff)
414{
415 int i;
416 struct kvm *kvm = vcpu->kvm;
417 struct kvm_vcpu *tmp;
418
419 mutex_lock(&kvm->lock);
420 kvm_for_each_vcpu(i, tmp, kvm)
421 vcpu_vtimer(tmp)->cntvoff = cntvoff;
422
423 /*
424 * When called from the vcpu create path, the CPU being created is not
425 * included in the loop above, so we just set it here as well.
426 */
427 vcpu_vtimer(vcpu)->cntvoff = cntvoff;
428 mutex_unlock(&kvm->lock);
429}
430
Marc Zyngier53e72402013-01-23 13:21:58 -0500431void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
432{
433 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
434
Jintack Lim90de9432017-02-03 10:20:00 -0500435 /* Synchronize cntvoff across all vtimers of a VM. */
436 update_vtimer_cntvoff(vcpu, kvm_phys_timer_read());
Jintack Lima91d1852017-02-03 10:20:03 -0500437 vcpu_ptimer(vcpu)->cntvoff = 0;
Jintack Lim90de9432017-02-03 10:20:00 -0500438
Marc Zyngier53e72402013-01-23 13:21:58 -0500439 INIT_WORK(&timer->expired, kvm_timer_inject_irq_work);
440 hrtimer_init(&timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
441 timer->timer.function = kvm_timer_expire;
Marc Zyngier53e72402013-01-23 13:21:58 -0500442}
443
444static void kvm_timer_init_interrupt(void *info)
445{
Marc Zyngiercabdc5c2016-08-16 15:03:02 +0100446 enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
Marc Zyngier53e72402013-01-23 13:21:58 -0500447}
448
Andre Przywara39735a32013-12-13 14:23:26 +0100449int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
450{
Jintack Limfbb4aee2017-02-03 10:19:59 -0500451 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Andre Przywara39735a32013-12-13 14:23:26 +0100452
453 switch (regid) {
454 case KVM_REG_ARM_TIMER_CTL:
Jintack Limfbb4aee2017-02-03 10:19:59 -0500455 vtimer->cnt_ctl = value;
Andre Przywara39735a32013-12-13 14:23:26 +0100456 break;
457 case KVM_REG_ARM_TIMER_CNT:
Jintack Lim90de9432017-02-03 10:20:00 -0500458 update_vtimer_cntvoff(vcpu, kvm_phys_timer_read() - value);
Andre Przywara39735a32013-12-13 14:23:26 +0100459 break;
460 case KVM_REG_ARM_TIMER_CVAL:
Jintack Limfbb4aee2017-02-03 10:19:59 -0500461 vtimer->cnt_cval = value;
Andre Przywara39735a32013-12-13 14:23:26 +0100462 break;
463 default:
464 return -1;
465 }
Christoffer Dall4b4b4512015-08-30 15:01:27 +0200466
467 kvm_timer_update_state(vcpu);
Andre Przywara39735a32013-12-13 14:23:26 +0100468 return 0;
469}
470
471u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid)
472{
Jintack Limfbb4aee2017-02-03 10:19:59 -0500473 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Andre Przywara39735a32013-12-13 14:23:26 +0100474
475 switch (regid) {
476 case KVM_REG_ARM_TIMER_CTL:
Jintack Limfbb4aee2017-02-03 10:19:59 -0500477 return vtimer->cnt_ctl;
Andre Przywara39735a32013-12-13 14:23:26 +0100478 case KVM_REG_ARM_TIMER_CNT:
Jintack Lim90de9432017-02-03 10:20:00 -0500479 return kvm_phys_timer_read() - vtimer->cntvoff;
Andre Przywara39735a32013-12-13 14:23:26 +0100480 case KVM_REG_ARM_TIMER_CVAL:
Jintack Limfbb4aee2017-02-03 10:19:59 -0500481 return vtimer->cnt_cval;
Andre Przywara39735a32013-12-13 14:23:26 +0100482 }
483 return (u64)-1;
484}
Marc Zyngier53e72402013-01-23 13:21:58 -0500485
Richard Cochranb3c99502016-07-13 17:16:47 +0000486static int kvm_timer_starting_cpu(unsigned int cpu)
Marc Zyngier53e72402013-01-23 13:21:58 -0500487{
Richard Cochranb3c99502016-07-13 17:16:47 +0000488 kvm_timer_init_interrupt(NULL);
489 return 0;
Marc Zyngier53e72402013-01-23 13:21:58 -0500490}
491
Richard Cochranb3c99502016-07-13 17:16:47 +0000492static int kvm_timer_dying_cpu(unsigned int cpu)
493{
494 disable_percpu_irq(host_vtimer_irq);
495 return 0;
496}
Marc Zyngier53e72402013-01-23 13:21:58 -0500497
Marc Zyngier53e72402013-01-23 13:21:58 -0500498int kvm_timer_hyp_init(void)
499{
Julien Grall29c2d6f2016-04-11 16:32:58 +0100500 struct arch_timer_kvm_info *info;
Marc Zyngier53e72402013-01-23 13:21:58 -0500501 int err;
502
Julien Grall29c2d6f2016-04-11 16:32:58 +0100503 info = arch_timer_get_kvm_info();
504 timecounter = &info->timecounter;
Marc Zyngier53e72402013-01-23 13:21:58 -0500505
Christoffer Dall8e1a0472016-12-05 10:32:11 +0100506 if (!timecounter->cc) {
507 kvm_err("kvm_arch_timer: uninitialized timecounter\n");
508 return -ENODEV;
509 }
510
Julien Grall29c2d6f2016-04-11 16:32:58 +0100511 if (info->virtual_irq <= 0) {
512 kvm_err("kvm_arch_timer: invalid virtual timer IRQ: %d\n",
513 info->virtual_irq);
Marc Zyngier53e72402013-01-23 13:21:58 -0500514 return -ENODEV;
515 }
Julien Grall29c2d6f2016-04-11 16:32:58 +0100516 host_vtimer_irq = info->virtual_irq;
Marc Zyngier53e72402013-01-23 13:21:58 -0500517
Marc Zyngiercabdc5c2016-08-16 15:03:02 +0100518 host_vtimer_irq_flags = irq_get_trigger_type(host_vtimer_irq);
519 if (host_vtimer_irq_flags != IRQF_TRIGGER_HIGH &&
520 host_vtimer_irq_flags != IRQF_TRIGGER_LOW) {
521 kvm_err("Invalid trigger for IRQ%d, assuming level low\n",
522 host_vtimer_irq);
523 host_vtimer_irq_flags = IRQF_TRIGGER_LOW;
524 }
525
Julien Grall29c2d6f2016-04-11 16:32:58 +0100526 err = request_percpu_irq(host_vtimer_irq, kvm_arch_timer_handler,
Marc Zyngier53e72402013-01-23 13:21:58 -0500527 "kvm guest timer", kvm_get_running_vcpus());
528 if (err) {
529 kvm_err("kvm_arch_timer: can't request interrupt %d (%d)\n",
Julien Grall29c2d6f2016-04-11 16:32:58 +0100530 host_vtimer_irq, err);
Paolo Bonzini5d947a12016-09-08 12:45:59 +0200531 return err;
Marc Zyngier53e72402013-01-23 13:21:58 -0500532 }
533
Julien Grall29c2d6f2016-04-11 16:32:58 +0100534 kvm_info("virtual timer IRQ%d\n", host_vtimer_irq);
Marc Zyngier53e72402013-01-23 13:21:58 -0500535
Richard Cochranb3c99502016-07-13 17:16:47 +0000536 cpuhp_setup_state(CPUHP_AP_KVM_ARM_TIMER_STARTING,
Thomas Gleixner73c1b412016-12-21 20:19:54 +0100537 "kvm/arm/timer:starting", kvm_timer_starting_cpu,
Richard Cochranb3c99502016-07-13 17:16:47 +0000538 kvm_timer_dying_cpu);
Marc Zyngier53e72402013-01-23 13:21:58 -0500539 return err;
540}
541
542void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
543{
544 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
Jintack Limfbb4aee2017-02-03 10:19:59 -0500545 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Marc Zyngier53e72402013-01-23 13:21:58 -0500546
547 timer_disarm(timer);
Jintack Limfbb4aee2017-02-03 10:19:59 -0500548 kvm_vgic_unmap_phys_irq(vcpu, vtimer->irq.irq);
Marc Zyngier53e72402013-01-23 13:21:58 -0500549}
550
Christoffer Dall41a54482016-05-18 16:26:00 +0100551int kvm_timer_enable(struct kvm_vcpu *vcpu)
Marc Zyngier53e72402013-01-23 13:21:58 -0500552{
Christoffer Dall41a54482016-05-18 16:26:00 +0100553 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
Jintack Limfbb4aee2017-02-03 10:19:59 -0500554 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
Christoffer Dall41a54482016-05-18 16:26:00 +0100555 struct irq_desc *desc;
556 struct irq_data *data;
557 int phys_irq;
558 int ret;
559
560 if (timer->enabled)
561 return 0;
562
563 /*
564 * Find the physical IRQ number corresponding to the host_vtimer_irq
565 */
566 desc = irq_to_desc(host_vtimer_irq);
567 if (!desc) {
568 kvm_err("%s: no interrupt descriptor\n", __func__);
569 return -EINVAL;
570 }
571
572 data = irq_desc_get_irq_data(desc);
573 while (data->parent_data)
574 data = data->parent_data;
575
576 phys_irq = data->hwirq;
577
578 /*
579 * Tell the VGIC that the virtual interrupt is tied to a
580 * physical interrupt. We do that once per VCPU.
581 */
Jintack Limfbb4aee2017-02-03 10:19:59 -0500582 ret = kvm_vgic_map_phys_irq(vcpu, vtimer->irq.irq, phys_irq);
Christoffer Dall41a54482016-05-18 16:26:00 +0100583 if (ret)
584 return ret;
585
Longpeng(Mike)fd5ebf92016-11-09 10:50:14 +0800586 timer->enabled = 1;
Christoffer Dall41a54482016-05-18 16:26:00 +0100587
588 return 0;
Christoffer Dall05971122014-12-12 21:19:23 +0100589}
590
Jintack Lim488f94d2016-12-01 14:32:05 -0500591/*
592 * On VHE system, we only need to configure trap on physical timer and counter
593 * accesses in EL0 and EL1 once, not for every world switch.
594 * The host kernel runs at EL2 with HCR_EL2.TGE == 1,
595 * and this makes those bits have no effect for the host kernel execution.
596 */
597void kvm_timer_init_vhe(void)
598{
599 /* When HCR_EL2.E2H ==1, EL1PCEN and EL1PCTEN are shifted by 10 */
600 u32 cnthctl_shift = 10;
601 u64 val;
602
603 /*
604 * Disallow physical timer access for the guest.
605 * Physical counter access is allowed.
606 */
607 val = read_sysreg(cnthctl_el2);
608 val &= ~(CNTHCTL_EL1PCEN << cnthctl_shift);
609 val |= (CNTHCTL_EL1PCTEN << cnthctl_shift);
610 write_sysreg(val, cnthctl_el2);
611}