Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 ARM Ltd. |
| 3 | * Author: Marc Zyngier <marc.zyngier@arm.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | #include <linux/cpu.h> |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 20 | #include <linux/kvm.h> |
| 21 | #include <linux/kvm_host.h> |
| 22 | #include <linux/interrupt.h> |
Christoffer Dall | b452cb5 | 2016-06-04 15:41:00 +0100 | [diff] [blame] | 23 | #include <linux/irq.h> |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 24 | |
Mark Rutland | 372b7c1 | 2013-03-27 15:56:11 +0000 | [diff] [blame] | 25 | #include <clocksource/arm_arch_timer.h> |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 26 | #include <asm/arch_timer.h> |
Jintack Lim | 488f94d | 2016-12-01 14:32:05 -0500 | [diff] [blame] | 27 | #include <asm/kvm_hyp.h> |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 28 | |
Marc Zyngier | 7275acd | 2013-05-14 14:31:01 +0100 | [diff] [blame] | 29 | #include <kvm/arm_vgic.h> |
| 30 | #include <kvm/arm_arch_timer.h> |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 31 | |
Christoffer Dall | e21f091 | 2015-08-30 13:57:20 +0200 | [diff] [blame] | 32 | #include "trace.h" |
| 33 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 34 | static struct timecounter *timecounter; |
Anup Patel | 5ae7f87 | 2013-04-30 12:02:15 +0530 | [diff] [blame] | 35 | static unsigned int host_vtimer_irq; |
Marc Zyngier | cabdc5c | 2016-08-16 15:03:02 +0100 | [diff] [blame] | 36 | static u32 host_vtimer_irq_flags; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 37 | |
Marc Zyngier | 9b4a300 | 2016-01-29 19:04:48 +0000 | [diff] [blame] | 38 | void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu) |
| 39 | { |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 40 | vcpu_vtimer(vcpu)->active_cleared_last = false; |
Marc Zyngier | 9b4a300 | 2016-01-29 19:04:48 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Jintack Lim | 7b6b463 | 2017-02-03 10:20:08 -0500 | [diff] [blame] | 43 | u64 kvm_phys_timer_read(void) |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 44 | { |
| 45 | return timecounter->cc->read(timecounter->cc); |
| 46 | } |
| 47 | |
| 48 | static 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 */ |
| 54 | static 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 | |
| 61 | static 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 Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 70 | static 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 Dall | 1a74847 | 2015-03-13 17:02:55 +0000 | [diff] [blame] | 84 | /* |
| 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 Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 88 | static 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 Zyngier | 1c5631c | 2016-04-06 09:37:22 +0100 | [diff] [blame] | 93 | |
Christoffer Dall | 1a74847 | 2015-03-13 17:02:55 +0000 | [diff] [blame] | 94 | /* |
| 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 Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 99 | } |
| 100 | |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame] | 101 | static u64 kvm_timer_compute_delta(struct arch_timer_context *timer_ctx) |
Marc Zyngier | 1c5631c | 2016-04-06 09:37:22 +0100 | [diff] [blame] | 102 | { |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 103 | u64 cval, now; |
Marc Zyngier | 1c5631c | 2016-04-06 09:37:22 +0100 | [diff] [blame] | 104 | |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame] | 105 | cval = timer_ctx->cnt_cval; |
| 106 | now = kvm_phys_timer_read() - timer_ctx->cntvoff; |
Marc Zyngier | 1c5631c | 2016-04-06 09:37:22 +0100 | [diff] [blame] | 107 | |
| 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 Lim | fb280e9 | 2017-02-03 10:20:05 -0500 | [diff] [blame] | 121 | static 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 | */ |
| 131 | static 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 Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 150 | static enum hrtimer_restart kvm_timer_expire(struct hrtimer *hrt) |
| 151 | { |
| 152 | struct arch_timer_cpu *timer; |
Marc Zyngier | 1c5631c | 2016-04-06 09:37:22 +0100 | [diff] [blame] | 153 | struct kvm_vcpu *vcpu; |
| 154 | u64 ns; |
| 155 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 156 | timer = container_of(hrt, struct arch_timer_cpu, timer); |
Marc Zyngier | 1c5631c | 2016-04-06 09:37:22 +0100 | [diff] [blame] | 157 | 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 Lim | fb280e9 | 2017-02-03 10:20:05 -0500 | [diff] [blame] | 164 | ns = kvm_timer_earliest_exp(vcpu); |
Marc Zyngier | 1c5631c | 2016-04-06 09:37:22 +0100 | [diff] [blame] | 165 | if (unlikely(ns)) { |
| 166 | hrtimer_forward_now(hrt, ns_to_ktime(ns)); |
| 167 | return HRTIMER_RESTART; |
| 168 | } |
| 169 | |
Bhaktipriya Shridhar | 3706fea | 2016-08-30 23:29:51 +0530 | [diff] [blame] | 170 | schedule_work(&timer->expired); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 171 | return HRTIMER_NORESTART; |
| 172 | } |
| 173 | |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame] | 174 | bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx) |
Christoffer Dall | 1a74847 | 2015-03-13 17:02:55 +0000 | [diff] [blame] | 175 | { |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 176 | u64 cval, now; |
Christoffer Dall | 1a74847 | 2015-03-13 17:02:55 +0000 | [diff] [blame] | 177 | |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame] | 178 | if (!kvm_timer_irq_can_fire(timer_ctx)) |
Christoffer Dall | 1a74847 | 2015-03-13 17:02:55 +0000 | [diff] [blame] | 179 | return false; |
| 180 | |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame] | 181 | cval = timer_ctx->cnt_cval; |
| 182 | now = kvm_phys_timer_read() - timer_ctx->cntvoff; |
Christoffer Dall | 1a74847 | 2015-03-13 17:02:55 +0000 | [diff] [blame] | 183 | |
| 184 | return cval <= now; |
| 185 | } |
| 186 | |
Alexander Graf | d9e1397 | 2016-09-27 21:08:06 +0200 | [diff] [blame^] | 187 | /* |
| 188 | * Reflect the timer output level into the kvm_run structure |
| 189 | */ |
| 190 | void kvm_timer_update_run(struct kvm_vcpu *vcpu) |
| 191 | { |
| 192 | struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); |
| 193 | struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); |
| 194 | struct kvm_sync_regs *regs = &vcpu->run->s.regs; |
| 195 | |
| 196 | if (likely(irqchip_in_kernel(vcpu->kvm))) |
| 197 | return; |
| 198 | |
| 199 | /* Populate the device bitmap with the timer states */ |
| 200 | regs->device_irq_level &= ~(KVM_ARM_DEV_EL1_VTIMER | |
| 201 | KVM_ARM_DEV_EL1_PTIMER); |
| 202 | if (vtimer->irq.level) |
| 203 | regs->device_irq_level |= KVM_ARM_DEV_EL1_VTIMER; |
| 204 | if (ptimer->irq.level) |
| 205 | regs->device_irq_level |= KVM_ARM_DEV_EL1_PTIMER; |
| 206 | } |
| 207 | |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame] | 208 | static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level, |
| 209 | struct arch_timer_context *timer_ctx) |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 210 | { |
| 211 | int ret; |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 212 | |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame] | 213 | timer_ctx->active_cleared_last = false; |
| 214 | timer_ctx->irq.level = new_level; |
| 215 | trace_kvm_timer_update_irq(vcpu->vcpu_id, timer_ctx->irq.irq, |
| 216 | timer_ctx->irq.level); |
Christoffer Dall | 11710de | 2017-02-01 11:03:45 +0100 | [diff] [blame] | 217 | |
Alexander Graf | d9e1397 | 2016-09-27 21:08:06 +0200 | [diff] [blame^] | 218 | if (likely(irqchip_in_kernel(vcpu->kvm))) { |
| 219 | ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id, |
| 220 | timer_ctx->irq.irq, |
| 221 | timer_ctx->irq.level); |
| 222 | WARN_ON(ret); |
| 223 | } |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* |
| 227 | * Check if there was a change in the timer state (should we raise or lower |
| 228 | * the line level to the GIC). |
| 229 | */ |
Christoffer Dall | b22e7df | 2016-09-27 21:08:04 +0200 | [diff] [blame] | 230 | static void kvm_timer_update_state(struct kvm_vcpu *vcpu) |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 231 | { |
| 232 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 233 | struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); |
Jintack Lim | 58e0c97 | 2017-02-03 10:20:04 -0500 | [diff] [blame] | 234 | struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 235 | |
| 236 | /* |
| 237 | * If userspace modified the timer registers via SET_ONE_REG before |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 238 | * the vgic was initialized, we mustn't set the vtimer->irq.level value |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 239 | * because the guest would never see the interrupt. Instead wait |
| 240 | * until we call this function from kvm_timer_flush_hwstate. |
| 241 | */ |
Alexander Graf | d9e1397 | 2016-09-27 21:08:06 +0200 | [diff] [blame^] | 242 | if (unlikely(!timer->enabled)) |
Christoffer Dall | b22e7df | 2016-09-27 21:08:04 +0200 | [diff] [blame] | 243 | return; |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 244 | |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame] | 245 | if (kvm_timer_should_fire(vtimer) != vtimer->irq.level) |
| 246 | kvm_timer_update_irq(vcpu, !vtimer->irq.level, vtimer); |
Andre Przywara | b3aff6c | 2016-02-03 16:56:51 +0000 | [diff] [blame] | 247 | |
Jintack Lim | 58e0c97 | 2017-02-03 10:20:04 -0500 | [diff] [blame] | 248 | if (kvm_timer_should_fire(ptimer) != ptimer->irq.level) |
| 249 | kvm_timer_update_irq(vcpu, !ptimer->irq.level, ptimer); |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Jintack Lim | f242ada | 2017-02-03 10:20:06 -0500 | [diff] [blame] | 252 | /* Schedule the background timer for the emulated timer. */ |
| 253 | static void kvm_timer_emulate(struct kvm_vcpu *vcpu, |
| 254 | struct arch_timer_context *timer_ctx) |
| 255 | { |
| 256 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
| 257 | |
| 258 | if (kvm_timer_should_fire(timer_ctx)) |
| 259 | return; |
| 260 | |
| 261 | if (!kvm_timer_irq_can_fire(timer_ctx)) |
| 262 | return; |
| 263 | |
| 264 | /* The timer has not yet expired, schedule a background timer */ |
| 265 | timer_arm(timer, kvm_timer_compute_delta(timer_ctx)); |
| 266 | } |
| 267 | |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 268 | /* |
| 269 | * Schedule the background timer before calling kvm_vcpu_block, so that this |
| 270 | * thread is removed from its waitqueue and made runnable when there's a timer |
| 271 | * interrupt to handle. |
| 272 | */ |
| 273 | void kvm_timer_schedule(struct kvm_vcpu *vcpu) |
| 274 | { |
| 275 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame] | 276 | struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); |
Jintack Lim | fb280e9 | 2017-02-03 10:20:05 -0500 | [diff] [blame] | 277 | struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 278 | |
| 279 | BUG_ON(timer_is_armed(timer)); |
| 280 | |
| 281 | /* |
Jintack Lim | fb280e9 | 2017-02-03 10:20:05 -0500 | [diff] [blame] | 282 | * No need to schedule a background timer if any guest timer has |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 283 | * already expired, because kvm_vcpu_block will return before putting |
| 284 | * the thread to sleep. |
| 285 | */ |
Jintack Lim | fb280e9 | 2017-02-03 10:20:05 -0500 | [diff] [blame] | 286 | if (kvm_timer_should_fire(vtimer) || kvm_timer_should_fire(ptimer)) |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 287 | return; |
| 288 | |
| 289 | /* |
Jintack Lim | fb280e9 | 2017-02-03 10:20:05 -0500 | [diff] [blame] | 290 | * If both timers are not capable of raising interrupts (disabled or |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 291 | * masked), then there's no more work for us to do. |
| 292 | */ |
Jintack Lim | fb280e9 | 2017-02-03 10:20:05 -0500 | [diff] [blame] | 293 | if (!kvm_timer_irq_can_fire(vtimer) && !kvm_timer_irq_can_fire(ptimer)) |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 294 | return; |
| 295 | |
Jintack Lim | fb280e9 | 2017-02-03 10:20:05 -0500 | [diff] [blame] | 296 | /* |
| 297 | * The guest timers have not yet expired, schedule a background timer. |
| 298 | * Set the earliest expiration time among the guest timers. |
| 299 | */ |
| 300 | timer_arm(timer, kvm_timer_earliest_exp(vcpu)); |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | void kvm_timer_unschedule(struct kvm_vcpu *vcpu) |
| 304 | { |
| 305 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
| 306 | timer_disarm(timer); |
| 307 | } |
| 308 | |
Alexander Graf | d9e1397 | 2016-09-27 21:08:06 +0200 | [diff] [blame^] | 309 | static void kvm_timer_flush_hwstate_vgic(struct kvm_vcpu *vcpu) |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 310 | { |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 311 | struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); |
Christoffer Dall | cff9211 | 2015-10-16 12:41:21 +0200 | [diff] [blame] | 312 | bool phys_active; |
| 313 | int ret; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 314 | |
Christoffer Dall | cff9211 | 2015-10-16 12:41:21 +0200 | [diff] [blame] | 315 | /* |
Christoffer Dall | 0e3dfda | 2015-11-24 16:23:05 +0100 | [diff] [blame] | 316 | * If we enter the guest with the virtual input level to the VGIC |
| 317 | * asserted, then we have already told the VGIC what we need to, and |
| 318 | * we don't need to exit from the guest until the guest deactivates |
| 319 | * the already injected interrupt, so therefore we should set the |
| 320 | * hardware active state to prevent unnecessary exits from the guest. |
| 321 | * |
| 322 | * Also, if we enter the guest with the virtual timer interrupt active, |
| 323 | * then it must be active on the physical distributor, because we set |
| 324 | * the HW bit and the guest must be able to deactivate the virtual and |
| 325 | * physical interrupt at the same time. |
| 326 | * |
| 327 | * Conversely, if the virtual input level is deasserted and the virtual |
| 328 | * interrupt is not active, then always clear the hardware active state |
| 329 | * to ensure that hardware interrupts from the timer triggers a guest |
| 330 | * exit. |
| 331 | */ |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 332 | phys_active = vtimer->irq.level || |
| 333 | kvm_vgic_map_is_active(vcpu, vtimer->irq.irq); |
Christoffer Dall | cff9211 | 2015-10-16 12:41:21 +0200 | [diff] [blame] | 334 | |
Marc Zyngier | 9b4a300 | 2016-01-29 19:04:48 +0000 | [diff] [blame] | 335 | /* |
| 336 | * We want to avoid hitting the (re)distributor as much as |
| 337 | * possible, as this is a potentially expensive MMIO access |
| 338 | * (not to mention locks in the irq layer), and a solution for |
| 339 | * this is to cache the "active" state in memory. |
| 340 | * |
| 341 | * Things to consider: we cannot cache an "active set" state, |
| 342 | * because the HW can change this behind our back (it becomes |
| 343 | * "clear" in the HW). We must then restrict the caching to |
| 344 | * the "clear" state. |
| 345 | * |
| 346 | * The cache is invalidated on: |
| 347 | * - vcpu put, indicating that the HW cannot be trusted to be |
| 348 | * in a sane state on the next vcpu load, |
| 349 | * - any change in the interrupt state |
| 350 | * |
| 351 | * Usage conditions: |
| 352 | * - cached value is "active clear" |
| 353 | * - value to be programmed is "active clear" |
| 354 | */ |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 355 | if (vtimer->active_cleared_last && !phys_active) |
Marc Zyngier | 9b4a300 | 2016-01-29 19:04:48 +0000 | [diff] [blame] | 356 | return; |
| 357 | |
Christoffer Dall | b452cb5 | 2016-06-04 15:41:00 +0100 | [diff] [blame] | 358 | ret = irq_set_irqchip_state(host_vtimer_irq, |
Christoffer Dall | cff9211 | 2015-10-16 12:41:21 +0200 | [diff] [blame] | 359 | IRQCHIP_STATE_ACTIVE, |
| 360 | phys_active); |
| 361 | WARN_ON(ret); |
Marc Zyngier | 9b4a300 | 2016-01-29 19:04:48 +0000 | [diff] [blame] | 362 | |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 363 | vtimer->active_cleared_last = !phys_active; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 364 | } |
| 365 | |
Alexander Graf | d9e1397 | 2016-09-27 21:08:06 +0200 | [diff] [blame^] | 366 | bool kvm_timer_should_notify_user(struct kvm_vcpu *vcpu) |
| 367 | { |
| 368 | struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); |
| 369 | struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); |
| 370 | struct kvm_sync_regs *sregs = &vcpu->run->s.regs; |
| 371 | bool vlevel, plevel; |
| 372 | |
| 373 | if (likely(irqchip_in_kernel(vcpu->kvm))) |
| 374 | return false; |
| 375 | |
| 376 | vlevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_VTIMER; |
| 377 | plevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_PTIMER; |
| 378 | |
| 379 | return vtimer->irq.level != vlevel || |
| 380 | ptimer->irq.level != plevel; |
| 381 | } |
| 382 | |
| 383 | static void kvm_timer_flush_hwstate_user(struct kvm_vcpu *vcpu) |
| 384 | { |
| 385 | struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); |
| 386 | |
| 387 | /* |
| 388 | * To prevent continuously exiting from the guest, we mask the |
| 389 | * physical interrupt such that the guest can make forward progress. |
| 390 | * Once we detect the output level being deasserted, we unmask the |
| 391 | * interrupt again so that we exit from the guest when the timer |
| 392 | * fires. |
| 393 | */ |
| 394 | if (vtimer->irq.level) |
| 395 | disable_percpu_irq(host_vtimer_irq); |
| 396 | else |
| 397 | enable_percpu_irq(host_vtimer_irq, 0); |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * kvm_timer_flush_hwstate - prepare timers before running the vcpu |
| 402 | * @vcpu: The vcpu pointer |
| 403 | * |
| 404 | * Check if the virtual timer has expired while we were running in the host, |
| 405 | * and inject an interrupt if that was the case, making sure the timer is |
| 406 | * masked or disabled on the host so that we keep executing. Also schedule a |
| 407 | * software timer for the physical timer if it is enabled. |
| 408 | */ |
| 409 | void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu) |
| 410 | { |
| 411 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
| 412 | |
| 413 | if (unlikely(!timer->enabled)) |
| 414 | return; |
| 415 | |
| 416 | kvm_timer_update_state(vcpu); |
| 417 | |
| 418 | /* Set the background timer for the physical timer emulation. */ |
| 419 | kvm_timer_emulate(vcpu, vcpu_ptimer(vcpu)); |
| 420 | |
| 421 | if (unlikely(!irqchip_in_kernel(vcpu->kvm))) |
| 422 | kvm_timer_flush_hwstate_user(vcpu); |
| 423 | else |
| 424 | kvm_timer_flush_hwstate_vgic(vcpu); |
| 425 | } |
| 426 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 427 | /** |
| 428 | * kvm_timer_sync_hwstate - sync timer state from cpu |
| 429 | * @vcpu: The vcpu pointer |
| 430 | * |
Alexander Graf | d9e1397 | 2016-09-27 21:08:06 +0200 | [diff] [blame^] | 431 | * Check if any of the timers have expired while we were running in the guest, |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 432 | * and inject an interrupt if that was the case. |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 433 | */ |
| 434 | void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu) |
| 435 | { |
| 436 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 437 | |
Jintack Lim | f242ada | 2017-02-03 10:20:06 -0500 | [diff] [blame] | 438 | /* |
| 439 | * This is to cancel the background timer for the physical timer |
| 440 | * emulation if it is set. |
| 441 | */ |
| 442 | timer_disarm(timer); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 443 | |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 444 | /* |
| 445 | * The guest could have modified the timer registers or the timer |
| 446 | * could have expired, update the timer state. |
| 447 | */ |
| 448 | kvm_timer_update_state(vcpu); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 449 | } |
| 450 | |
Marc Zyngier | f120cd6 | 2014-06-23 13:59:13 +0100 | [diff] [blame] | 451 | int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu, |
Jintack Lim | a91d185 | 2017-02-03 10:20:03 -0500 | [diff] [blame] | 452 | const struct kvm_irq_level *virt_irq, |
| 453 | const struct kvm_irq_level *phys_irq) |
Anup Patel | 5ae7f87 | 2013-04-30 12:02:15 +0530 | [diff] [blame] | 454 | { |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 455 | struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); |
Jintack Lim | a91d185 | 2017-02-03 10:20:03 -0500 | [diff] [blame] | 456 | struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); |
Anup Patel | 5ae7f87 | 2013-04-30 12:02:15 +0530 | [diff] [blame] | 457 | |
| 458 | /* |
| 459 | * The vcpu timer irq number cannot be determined in |
| 460 | * kvm_timer_vcpu_init() because it is called much before |
| 461 | * kvm_vcpu_set_target(). To handle this, we determine |
| 462 | * vcpu timer irq number when the vcpu is reset. |
| 463 | */ |
Jintack Lim | a91d185 | 2017-02-03 10:20:03 -0500 | [diff] [blame] | 464 | vtimer->irq.irq = virt_irq->irq; |
| 465 | ptimer->irq.irq = phys_irq->irq; |
Marc Zyngier | f120cd6 | 2014-06-23 13:59:13 +0100 | [diff] [blame] | 466 | |
| 467 | /* |
Christoffer Dall | 4ad9e16 | 2015-09-04 16:24:39 +0200 | [diff] [blame] | 468 | * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8 |
| 469 | * and to 0 for ARMv7. We provide an implementation that always |
| 470 | * resets the timer to be disabled and unmasked and is compliant with |
| 471 | * the ARMv7 architecture. |
| 472 | */ |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 473 | vtimer->cnt_ctl = 0; |
Jintack Lim | a91d185 | 2017-02-03 10:20:03 -0500 | [diff] [blame] | 474 | ptimer->cnt_ctl = 0; |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 475 | kvm_timer_update_state(vcpu); |
Christoffer Dall | 4ad9e16 | 2015-09-04 16:24:39 +0200 | [diff] [blame] | 476 | |
Christoffer Dall | 41a5448 | 2016-05-18 16:26:00 +0100 | [diff] [blame] | 477 | return 0; |
Anup Patel | 5ae7f87 | 2013-04-30 12:02:15 +0530 | [diff] [blame] | 478 | } |
| 479 | |
Jintack Lim | 90de943 | 2017-02-03 10:20:00 -0500 | [diff] [blame] | 480 | /* Make the updates of cntvoff for all vtimer contexts atomic */ |
| 481 | static void update_vtimer_cntvoff(struct kvm_vcpu *vcpu, u64 cntvoff) |
| 482 | { |
| 483 | int i; |
| 484 | struct kvm *kvm = vcpu->kvm; |
| 485 | struct kvm_vcpu *tmp; |
| 486 | |
| 487 | mutex_lock(&kvm->lock); |
| 488 | kvm_for_each_vcpu(i, tmp, kvm) |
| 489 | vcpu_vtimer(tmp)->cntvoff = cntvoff; |
| 490 | |
| 491 | /* |
| 492 | * When called from the vcpu create path, the CPU being created is not |
| 493 | * included in the loop above, so we just set it here as well. |
| 494 | */ |
| 495 | vcpu_vtimer(vcpu)->cntvoff = cntvoff; |
| 496 | mutex_unlock(&kvm->lock); |
| 497 | } |
| 498 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 499 | void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu) |
| 500 | { |
| 501 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
| 502 | |
Jintack Lim | 90de943 | 2017-02-03 10:20:00 -0500 | [diff] [blame] | 503 | /* Synchronize cntvoff across all vtimers of a VM. */ |
| 504 | update_vtimer_cntvoff(vcpu, kvm_phys_timer_read()); |
Jintack Lim | a91d185 | 2017-02-03 10:20:03 -0500 | [diff] [blame] | 505 | vcpu_ptimer(vcpu)->cntvoff = 0; |
Jintack Lim | 90de943 | 2017-02-03 10:20:00 -0500 | [diff] [blame] | 506 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 507 | INIT_WORK(&timer->expired, kvm_timer_inject_irq_work); |
| 508 | hrtimer_init(&timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
| 509 | timer->timer.function = kvm_timer_expire; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | static void kvm_timer_init_interrupt(void *info) |
| 513 | { |
Marc Zyngier | cabdc5c | 2016-08-16 15:03:02 +0100 | [diff] [blame] | 514 | enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 515 | } |
| 516 | |
Andre Przywara | 39735a3 | 2013-12-13 14:23:26 +0100 | [diff] [blame] | 517 | int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value) |
| 518 | { |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 519 | struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); |
Andre Przywara | 39735a3 | 2013-12-13 14:23:26 +0100 | [diff] [blame] | 520 | |
| 521 | switch (regid) { |
| 522 | case KVM_REG_ARM_TIMER_CTL: |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 523 | vtimer->cnt_ctl = value; |
Andre Przywara | 39735a3 | 2013-12-13 14:23:26 +0100 | [diff] [blame] | 524 | break; |
| 525 | case KVM_REG_ARM_TIMER_CNT: |
Jintack Lim | 90de943 | 2017-02-03 10:20:00 -0500 | [diff] [blame] | 526 | update_vtimer_cntvoff(vcpu, kvm_phys_timer_read() - value); |
Andre Przywara | 39735a3 | 2013-12-13 14:23:26 +0100 | [diff] [blame] | 527 | break; |
| 528 | case KVM_REG_ARM_TIMER_CVAL: |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 529 | vtimer->cnt_cval = value; |
Andre Przywara | 39735a3 | 2013-12-13 14:23:26 +0100 | [diff] [blame] | 530 | break; |
| 531 | default: |
| 532 | return -1; |
| 533 | } |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 534 | |
| 535 | kvm_timer_update_state(vcpu); |
Andre Przywara | 39735a3 | 2013-12-13 14:23:26 +0100 | [diff] [blame] | 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid) |
| 540 | { |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 541 | struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); |
Andre Przywara | 39735a3 | 2013-12-13 14:23:26 +0100 | [diff] [blame] | 542 | |
| 543 | switch (regid) { |
| 544 | case KVM_REG_ARM_TIMER_CTL: |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 545 | return vtimer->cnt_ctl; |
Andre Przywara | 39735a3 | 2013-12-13 14:23:26 +0100 | [diff] [blame] | 546 | case KVM_REG_ARM_TIMER_CNT: |
Jintack Lim | 90de943 | 2017-02-03 10:20:00 -0500 | [diff] [blame] | 547 | return kvm_phys_timer_read() - vtimer->cntvoff; |
Andre Przywara | 39735a3 | 2013-12-13 14:23:26 +0100 | [diff] [blame] | 548 | case KVM_REG_ARM_TIMER_CVAL: |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 549 | return vtimer->cnt_cval; |
Andre Przywara | 39735a3 | 2013-12-13 14:23:26 +0100 | [diff] [blame] | 550 | } |
| 551 | return (u64)-1; |
| 552 | } |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 553 | |
Richard Cochran | b3c9950 | 2016-07-13 17:16:47 +0000 | [diff] [blame] | 554 | static int kvm_timer_starting_cpu(unsigned int cpu) |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 555 | { |
Richard Cochran | b3c9950 | 2016-07-13 17:16:47 +0000 | [diff] [blame] | 556 | kvm_timer_init_interrupt(NULL); |
| 557 | return 0; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 558 | } |
| 559 | |
Richard Cochran | b3c9950 | 2016-07-13 17:16:47 +0000 | [diff] [blame] | 560 | static int kvm_timer_dying_cpu(unsigned int cpu) |
| 561 | { |
| 562 | disable_percpu_irq(host_vtimer_irq); |
| 563 | return 0; |
| 564 | } |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 565 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 566 | int kvm_timer_hyp_init(void) |
| 567 | { |
Julien Grall | 29c2d6f | 2016-04-11 16:32:58 +0100 | [diff] [blame] | 568 | struct arch_timer_kvm_info *info; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 569 | int err; |
| 570 | |
Julien Grall | 29c2d6f | 2016-04-11 16:32:58 +0100 | [diff] [blame] | 571 | info = arch_timer_get_kvm_info(); |
| 572 | timecounter = &info->timecounter; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 573 | |
Christoffer Dall | 8e1a047 | 2016-12-05 10:32:11 +0100 | [diff] [blame] | 574 | if (!timecounter->cc) { |
| 575 | kvm_err("kvm_arch_timer: uninitialized timecounter\n"); |
| 576 | return -ENODEV; |
| 577 | } |
| 578 | |
Julien Grall | 29c2d6f | 2016-04-11 16:32:58 +0100 | [diff] [blame] | 579 | if (info->virtual_irq <= 0) { |
| 580 | kvm_err("kvm_arch_timer: invalid virtual timer IRQ: %d\n", |
| 581 | info->virtual_irq); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 582 | return -ENODEV; |
| 583 | } |
Julien Grall | 29c2d6f | 2016-04-11 16:32:58 +0100 | [diff] [blame] | 584 | host_vtimer_irq = info->virtual_irq; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 585 | |
Marc Zyngier | cabdc5c | 2016-08-16 15:03:02 +0100 | [diff] [blame] | 586 | host_vtimer_irq_flags = irq_get_trigger_type(host_vtimer_irq); |
| 587 | if (host_vtimer_irq_flags != IRQF_TRIGGER_HIGH && |
| 588 | host_vtimer_irq_flags != IRQF_TRIGGER_LOW) { |
| 589 | kvm_err("Invalid trigger for IRQ%d, assuming level low\n", |
| 590 | host_vtimer_irq); |
| 591 | host_vtimer_irq_flags = IRQF_TRIGGER_LOW; |
| 592 | } |
| 593 | |
Julien Grall | 29c2d6f | 2016-04-11 16:32:58 +0100 | [diff] [blame] | 594 | err = request_percpu_irq(host_vtimer_irq, kvm_arch_timer_handler, |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 595 | "kvm guest timer", kvm_get_running_vcpus()); |
| 596 | if (err) { |
| 597 | kvm_err("kvm_arch_timer: can't request interrupt %d (%d)\n", |
Julien Grall | 29c2d6f | 2016-04-11 16:32:58 +0100 | [diff] [blame] | 598 | host_vtimer_irq, err); |
Paolo Bonzini | 5d947a1 | 2016-09-08 12:45:59 +0200 | [diff] [blame] | 599 | return err; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 600 | } |
| 601 | |
Julien Grall | 29c2d6f | 2016-04-11 16:32:58 +0100 | [diff] [blame] | 602 | kvm_info("virtual timer IRQ%d\n", host_vtimer_irq); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 603 | |
Richard Cochran | b3c9950 | 2016-07-13 17:16:47 +0000 | [diff] [blame] | 604 | cpuhp_setup_state(CPUHP_AP_KVM_ARM_TIMER_STARTING, |
Thomas Gleixner | 73c1b41 | 2016-12-21 20:19:54 +0100 | [diff] [blame] | 605 | "kvm/arm/timer:starting", kvm_timer_starting_cpu, |
Richard Cochran | b3c9950 | 2016-07-13 17:16:47 +0000 | [diff] [blame] | 606 | kvm_timer_dying_cpu); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 607 | return err; |
| 608 | } |
| 609 | |
| 610 | void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu) |
| 611 | { |
| 612 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 613 | struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 614 | |
| 615 | timer_disarm(timer); |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 616 | kvm_vgic_unmap_phys_irq(vcpu, vtimer->irq.irq); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 617 | } |
| 618 | |
Christoffer Dall | 41a5448 | 2016-05-18 16:26:00 +0100 | [diff] [blame] | 619 | int kvm_timer_enable(struct kvm_vcpu *vcpu) |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 620 | { |
Christoffer Dall | 41a5448 | 2016-05-18 16:26:00 +0100 | [diff] [blame] | 621 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 622 | struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); |
Christoffer Dall | 41a5448 | 2016-05-18 16:26:00 +0100 | [diff] [blame] | 623 | struct irq_desc *desc; |
| 624 | struct irq_data *data; |
| 625 | int phys_irq; |
| 626 | int ret; |
| 627 | |
| 628 | if (timer->enabled) |
| 629 | return 0; |
| 630 | |
Alexander Graf | d9e1397 | 2016-09-27 21:08:06 +0200 | [diff] [blame^] | 631 | /* Without a VGIC we do not map virtual IRQs to physical IRQs */ |
| 632 | if (!irqchip_in_kernel(vcpu->kvm)) |
| 633 | goto no_vgic; |
| 634 | |
| 635 | if (!vgic_initialized(vcpu->kvm)) |
| 636 | return -ENODEV; |
| 637 | |
Christoffer Dall | 41a5448 | 2016-05-18 16:26:00 +0100 | [diff] [blame] | 638 | /* |
| 639 | * Find the physical IRQ number corresponding to the host_vtimer_irq |
| 640 | */ |
| 641 | desc = irq_to_desc(host_vtimer_irq); |
| 642 | if (!desc) { |
| 643 | kvm_err("%s: no interrupt descriptor\n", __func__); |
| 644 | return -EINVAL; |
| 645 | } |
| 646 | |
| 647 | data = irq_desc_get_irq_data(desc); |
| 648 | while (data->parent_data) |
| 649 | data = data->parent_data; |
| 650 | |
| 651 | phys_irq = data->hwirq; |
| 652 | |
| 653 | /* |
| 654 | * Tell the VGIC that the virtual interrupt is tied to a |
| 655 | * physical interrupt. We do that once per VCPU. |
| 656 | */ |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 657 | ret = kvm_vgic_map_phys_irq(vcpu, vtimer->irq.irq, phys_irq); |
Christoffer Dall | 41a5448 | 2016-05-18 16:26:00 +0100 | [diff] [blame] | 658 | if (ret) |
| 659 | return ret; |
| 660 | |
Alexander Graf | d9e1397 | 2016-09-27 21:08:06 +0200 | [diff] [blame^] | 661 | no_vgic: |
Longpeng(Mike) | fd5ebf9 | 2016-11-09 10:50:14 +0800 | [diff] [blame] | 662 | timer->enabled = 1; |
Christoffer Dall | 41a5448 | 2016-05-18 16:26:00 +0100 | [diff] [blame] | 663 | return 0; |
Christoffer Dall | 0597112 | 2014-12-12 21:19:23 +0100 | [diff] [blame] | 664 | } |
| 665 | |
Jintack Lim | 488f94d | 2016-12-01 14:32:05 -0500 | [diff] [blame] | 666 | /* |
| 667 | * On VHE system, we only need to configure trap on physical timer and counter |
| 668 | * accesses in EL0 and EL1 once, not for every world switch. |
| 669 | * The host kernel runs at EL2 with HCR_EL2.TGE == 1, |
| 670 | * and this makes those bits have no effect for the host kernel execution. |
| 671 | */ |
| 672 | void kvm_timer_init_vhe(void) |
| 673 | { |
| 674 | /* When HCR_EL2.E2H ==1, EL1PCEN and EL1PCTEN are shifted by 10 */ |
| 675 | u32 cnthctl_shift = 10; |
| 676 | u64 val; |
| 677 | |
| 678 | /* |
| 679 | * Disallow physical timer access for the guest. |
| 680 | * Physical counter access is allowed. |
| 681 | */ |
| 682 | val = read_sysreg(cnthctl_el2); |
| 683 | val &= ~(CNTHCTL_EL1PCEN << cnthctl_shift); |
| 684 | val |= (CNTHCTL_EL1PCTEN << cnthctl_shift); |
| 685 | write_sysreg(val, cnthctl_el2); |
| 686 | } |