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 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 43 | static 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 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 121 | static enum hrtimer_restart kvm_timer_expire(struct hrtimer *hrt) |
| 122 | { |
| 123 | struct arch_timer_cpu *timer; |
Marc Zyngier | 1c5631c | 2016-04-06 09:37:22 +0100 | [diff] [blame] | 124 | struct kvm_vcpu *vcpu; |
| 125 | u64 ns; |
| 126 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 127 | timer = container_of(hrt, struct arch_timer_cpu, timer); |
Marc Zyngier | 1c5631c | 2016-04-06 09:37:22 +0100 | [diff] [blame] | 128 | vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu); |
| 129 | |
| 130 | /* |
| 131 | * Check that the timer has really expired from the guest's |
| 132 | * PoV (NTP on the host may have forced it to expire |
| 133 | * early). If we should have slept longer, restart it. |
| 134 | */ |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame^] | 135 | ns = kvm_timer_compute_delta(vcpu_vtimer(vcpu)); |
Marc Zyngier | 1c5631c | 2016-04-06 09:37:22 +0100 | [diff] [blame] | 136 | if (unlikely(ns)) { |
| 137 | hrtimer_forward_now(hrt, ns_to_ktime(ns)); |
| 138 | return HRTIMER_RESTART; |
| 139 | } |
| 140 | |
Bhaktipriya Shridhar | 3706fea | 2016-08-30 23:29:51 +0530 | [diff] [blame] | 141 | schedule_work(&timer->expired); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 142 | return HRTIMER_NORESTART; |
| 143 | } |
| 144 | |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame^] | 145 | static bool kvm_timer_irq_can_fire(struct arch_timer_context *timer_ctx) |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 146 | { |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame^] | 147 | return !(timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_IT_MASK) && |
| 148 | (timer_ctx->cnt_ctl & ARCH_TIMER_CTRL_ENABLE); |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame^] | 151 | bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx) |
Christoffer Dall | 1a74847 | 2015-03-13 17:02:55 +0000 | [diff] [blame] | 152 | { |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 153 | u64 cval, now; |
Christoffer Dall | 1a74847 | 2015-03-13 17:02:55 +0000 | [diff] [blame] | 154 | |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame^] | 155 | if (!kvm_timer_irq_can_fire(timer_ctx)) |
Christoffer Dall | 1a74847 | 2015-03-13 17:02:55 +0000 | [diff] [blame] | 156 | return false; |
| 157 | |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame^] | 158 | cval = timer_ctx->cnt_cval; |
| 159 | now = kvm_phys_timer_read() - timer_ctx->cntvoff; |
Christoffer Dall | 1a74847 | 2015-03-13 17:02:55 +0000 | [diff] [blame] | 160 | |
| 161 | return cval <= now; |
| 162 | } |
| 163 | |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame^] | 164 | static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level, |
| 165 | struct arch_timer_context *timer_ctx) |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 166 | { |
| 167 | int ret; |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 168 | |
| 169 | BUG_ON(!vgic_initialized(vcpu->kvm)); |
| 170 | |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame^] | 171 | timer_ctx->active_cleared_last = false; |
| 172 | timer_ctx->irq.level = new_level; |
| 173 | trace_kvm_timer_update_irq(vcpu->vcpu_id, timer_ctx->irq.irq, |
| 174 | timer_ctx->irq.level); |
Christoffer Dall | 11710de | 2017-02-01 11:03:45 +0100 | [diff] [blame] | 175 | |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame^] | 176 | ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id, timer_ctx->irq.irq, |
| 177 | timer_ctx->irq.level); |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 178 | WARN_ON(ret); |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Check if there was a change in the timer state (should we raise or lower |
| 183 | * the line level to the GIC). |
| 184 | */ |
Andre Przywara | b3aff6c | 2016-02-03 16:56:51 +0000 | [diff] [blame] | 185 | static int kvm_timer_update_state(struct kvm_vcpu *vcpu) |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 186 | { |
| 187 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 188 | struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 189 | |
| 190 | /* |
| 191 | * If userspace modified the timer registers via SET_ONE_REG before |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 192 | * 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] | 193 | * because the guest would never see the interrupt. Instead wait |
| 194 | * until we call this function from kvm_timer_flush_hwstate. |
| 195 | */ |
Christoffer Dall | 41a5448 | 2016-05-18 16:26:00 +0100 | [diff] [blame] | 196 | if (!vgic_initialized(vcpu->kvm) || !timer->enabled) |
Andre Przywara | b3aff6c | 2016-02-03 16:56:51 +0000 | [diff] [blame] | 197 | return -ENODEV; |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 198 | |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame^] | 199 | if (kvm_timer_should_fire(vtimer) != vtimer->irq.level) |
| 200 | kvm_timer_update_irq(vcpu, !vtimer->irq.level, vtimer); |
Andre Przywara | b3aff6c | 2016-02-03 16:56:51 +0000 | [diff] [blame] | 201 | |
| 202 | return 0; |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 205 | /* |
| 206 | * Schedule the background timer before calling kvm_vcpu_block, so that this |
| 207 | * thread is removed from its waitqueue and made runnable when there's a timer |
| 208 | * interrupt to handle. |
| 209 | */ |
| 210 | void kvm_timer_schedule(struct kvm_vcpu *vcpu) |
| 211 | { |
| 212 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame^] | 213 | struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 214 | |
| 215 | BUG_ON(timer_is_armed(timer)); |
| 216 | |
| 217 | /* |
| 218 | * No need to schedule a background timer if the guest timer has |
| 219 | * already expired, because kvm_vcpu_block will return before putting |
| 220 | * the thread to sleep. |
| 221 | */ |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame^] | 222 | if (kvm_timer_should_fire(vtimer)) |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 223 | return; |
| 224 | |
| 225 | /* |
| 226 | * If the timer is not capable of raising interrupts (disabled or |
| 227 | * masked), then there's no more work for us to do. |
| 228 | */ |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame^] | 229 | if (!kvm_timer_irq_can_fire(vtimer)) |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 230 | return; |
| 231 | |
| 232 | /* The timer has not yet expired, schedule a background timer */ |
Jintack Lim | 9171fa2 | 2017-02-03 10:20:01 -0500 | [diff] [blame^] | 233 | timer_arm(timer, kvm_timer_compute_delta(vtimer)); |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | void kvm_timer_unschedule(struct kvm_vcpu *vcpu) |
| 237 | { |
| 238 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
| 239 | timer_disarm(timer); |
| 240 | } |
| 241 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 242 | /** |
| 243 | * kvm_timer_flush_hwstate - prepare to move the virt timer to the cpu |
| 244 | * @vcpu: The vcpu pointer |
| 245 | * |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 246 | * Check if the virtual timer has expired while we were running in the host, |
| 247 | * and inject an interrupt if that was the case. |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 248 | */ |
| 249 | void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu) |
| 250 | { |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 251 | struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); |
Christoffer Dall | cff9211 | 2015-10-16 12:41:21 +0200 | [diff] [blame] | 252 | bool phys_active; |
| 253 | int ret; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 254 | |
Andre Przywara | b3aff6c | 2016-02-03 16:56:51 +0000 | [diff] [blame] | 255 | if (kvm_timer_update_state(vcpu)) |
| 256 | return; |
Christoffer Dall | cff9211 | 2015-10-16 12:41:21 +0200 | [diff] [blame] | 257 | |
| 258 | /* |
Christoffer Dall | 0e3dfda | 2015-11-24 16:23:05 +0100 | [diff] [blame] | 259 | * If we enter the guest with the virtual input level to the VGIC |
| 260 | * asserted, then we have already told the VGIC what we need to, and |
| 261 | * we don't need to exit from the guest until the guest deactivates |
| 262 | * the already injected interrupt, so therefore we should set the |
| 263 | * hardware active state to prevent unnecessary exits from the guest. |
| 264 | * |
| 265 | * Also, if we enter the guest with the virtual timer interrupt active, |
| 266 | * then it must be active on the physical distributor, because we set |
| 267 | * the HW bit and the guest must be able to deactivate the virtual and |
| 268 | * physical interrupt at the same time. |
| 269 | * |
| 270 | * Conversely, if the virtual input level is deasserted and the virtual |
| 271 | * interrupt is not active, then always clear the hardware active state |
| 272 | * to ensure that hardware interrupts from the timer triggers a guest |
| 273 | * exit. |
| 274 | */ |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 275 | phys_active = vtimer->irq.level || |
| 276 | kvm_vgic_map_is_active(vcpu, vtimer->irq.irq); |
Christoffer Dall | cff9211 | 2015-10-16 12:41:21 +0200 | [diff] [blame] | 277 | |
Marc Zyngier | 9b4a300 | 2016-01-29 19:04:48 +0000 | [diff] [blame] | 278 | /* |
| 279 | * We want to avoid hitting the (re)distributor as much as |
| 280 | * possible, as this is a potentially expensive MMIO access |
| 281 | * (not to mention locks in the irq layer), and a solution for |
| 282 | * this is to cache the "active" state in memory. |
| 283 | * |
| 284 | * Things to consider: we cannot cache an "active set" state, |
| 285 | * because the HW can change this behind our back (it becomes |
| 286 | * "clear" in the HW). We must then restrict the caching to |
| 287 | * the "clear" state. |
| 288 | * |
| 289 | * The cache is invalidated on: |
| 290 | * - vcpu put, indicating that the HW cannot be trusted to be |
| 291 | * in a sane state on the next vcpu load, |
| 292 | * - any change in the interrupt state |
| 293 | * |
| 294 | * Usage conditions: |
| 295 | * - cached value is "active clear" |
| 296 | * - value to be programmed is "active clear" |
| 297 | */ |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 298 | if (vtimer->active_cleared_last && !phys_active) |
Marc Zyngier | 9b4a300 | 2016-01-29 19:04:48 +0000 | [diff] [blame] | 299 | return; |
| 300 | |
Christoffer Dall | b452cb5 | 2016-06-04 15:41:00 +0100 | [diff] [blame] | 301 | ret = irq_set_irqchip_state(host_vtimer_irq, |
Christoffer Dall | cff9211 | 2015-10-16 12:41:21 +0200 | [diff] [blame] | 302 | IRQCHIP_STATE_ACTIVE, |
| 303 | phys_active); |
| 304 | WARN_ON(ret); |
Marc Zyngier | 9b4a300 | 2016-01-29 19:04:48 +0000 | [diff] [blame] | 305 | |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 306 | vtimer->active_cleared_last = !phys_active; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /** |
| 310 | * kvm_timer_sync_hwstate - sync timer state from cpu |
| 311 | * @vcpu: The vcpu pointer |
| 312 | * |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 313 | * Check if the virtual timer has expired while we were running in the guest, |
| 314 | * and inject an interrupt if that was the case. |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 315 | */ |
| 316 | void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu) |
| 317 | { |
| 318 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 319 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 320 | BUG_ON(timer_is_armed(timer)); |
| 321 | |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 322 | /* |
| 323 | * The guest could have modified the timer registers or the timer |
| 324 | * could have expired, update the timer state. |
| 325 | */ |
| 326 | kvm_timer_update_state(vcpu); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 327 | } |
| 328 | |
Marc Zyngier | f120cd6 | 2014-06-23 13:59:13 +0100 | [diff] [blame] | 329 | int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu, |
| 330 | const struct kvm_irq_level *irq) |
Anup Patel | 5ae7f87 | 2013-04-30 12:02:15 +0530 | [diff] [blame] | 331 | { |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 332 | struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); |
Anup Patel | 5ae7f87 | 2013-04-30 12:02:15 +0530 | [diff] [blame] | 333 | |
| 334 | /* |
| 335 | * The vcpu timer irq number cannot be determined in |
| 336 | * kvm_timer_vcpu_init() because it is called much before |
| 337 | * kvm_vcpu_set_target(). To handle this, we determine |
| 338 | * vcpu timer irq number when the vcpu is reset. |
| 339 | */ |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 340 | vtimer->irq.irq = irq->irq; |
Marc Zyngier | f120cd6 | 2014-06-23 13:59:13 +0100 | [diff] [blame] | 341 | |
| 342 | /* |
Christoffer Dall | 4ad9e16 | 2015-09-04 16:24:39 +0200 | [diff] [blame] | 343 | * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8 |
| 344 | * and to 0 for ARMv7. We provide an implementation that always |
| 345 | * resets the timer to be disabled and unmasked and is compliant with |
| 346 | * the ARMv7 architecture. |
| 347 | */ |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 348 | vtimer->cnt_ctl = 0; |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 349 | kvm_timer_update_state(vcpu); |
Christoffer Dall | 4ad9e16 | 2015-09-04 16:24:39 +0200 | [diff] [blame] | 350 | |
Christoffer Dall | 41a5448 | 2016-05-18 16:26:00 +0100 | [diff] [blame] | 351 | return 0; |
Anup Patel | 5ae7f87 | 2013-04-30 12:02:15 +0530 | [diff] [blame] | 352 | } |
| 353 | |
Jintack Lim | 90de943 | 2017-02-03 10:20:00 -0500 | [diff] [blame] | 354 | /* Make the updates of cntvoff for all vtimer contexts atomic */ |
| 355 | static void update_vtimer_cntvoff(struct kvm_vcpu *vcpu, u64 cntvoff) |
| 356 | { |
| 357 | int i; |
| 358 | struct kvm *kvm = vcpu->kvm; |
| 359 | struct kvm_vcpu *tmp; |
| 360 | |
| 361 | mutex_lock(&kvm->lock); |
| 362 | kvm_for_each_vcpu(i, tmp, kvm) |
| 363 | vcpu_vtimer(tmp)->cntvoff = cntvoff; |
| 364 | |
| 365 | /* |
| 366 | * When called from the vcpu create path, the CPU being created is not |
| 367 | * included in the loop above, so we just set it here as well. |
| 368 | */ |
| 369 | vcpu_vtimer(vcpu)->cntvoff = cntvoff; |
| 370 | mutex_unlock(&kvm->lock); |
| 371 | } |
| 372 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 373 | void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu) |
| 374 | { |
| 375 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
| 376 | |
Jintack Lim | 90de943 | 2017-02-03 10:20:00 -0500 | [diff] [blame] | 377 | /* Synchronize cntvoff across all vtimers of a VM. */ |
| 378 | update_vtimer_cntvoff(vcpu, kvm_phys_timer_read()); |
| 379 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 380 | INIT_WORK(&timer->expired, kvm_timer_inject_irq_work); |
| 381 | hrtimer_init(&timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
| 382 | timer->timer.function = kvm_timer_expire; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | static void kvm_timer_init_interrupt(void *info) |
| 386 | { |
Marc Zyngier | cabdc5c | 2016-08-16 15:03:02 +0100 | [diff] [blame] | 387 | enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 388 | } |
| 389 | |
Andre Przywara | 39735a3 | 2013-12-13 14:23:26 +0100 | [diff] [blame] | 390 | int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value) |
| 391 | { |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 392 | struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); |
Andre Przywara | 39735a3 | 2013-12-13 14:23:26 +0100 | [diff] [blame] | 393 | |
| 394 | switch (regid) { |
| 395 | case KVM_REG_ARM_TIMER_CTL: |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 396 | vtimer->cnt_ctl = value; |
Andre Przywara | 39735a3 | 2013-12-13 14:23:26 +0100 | [diff] [blame] | 397 | break; |
| 398 | case KVM_REG_ARM_TIMER_CNT: |
Jintack Lim | 90de943 | 2017-02-03 10:20:00 -0500 | [diff] [blame] | 399 | update_vtimer_cntvoff(vcpu, kvm_phys_timer_read() - value); |
Andre Przywara | 39735a3 | 2013-12-13 14:23:26 +0100 | [diff] [blame] | 400 | break; |
| 401 | case KVM_REG_ARM_TIMER_CVAL: |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 402 | vtimer->cnt_cval = value; |
Andre Przywara | 39735a3 | 2013-12-13 14:23:26 +0100 | [diff] [blame] | 403 | break; |
| 404 | default: |
| 405 | return -1; |
| 406 | } |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 407 | |
| 408 | kvm_timer_update_state(vcpu); |
Andre Przywara | 39735a3 | 2013-12-13 14:23:26 +0100 | [diff] [blame] | 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid) |
| 413 | { |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 414 | struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); |
Andre Przywara | 39735a3 | 2013-12-13 14:23:26 +0100 | [diff] [blame] | 415 | |
| 416 | switch (regid) { |
| 417 | case KVM_REG_ARM_TIMER_CTL: |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 418 | return vtimer->cnt_ctl; |
Andre Przywara | 39735a3 | 2013-12-13 14:23:26 +0100 | [diff] [blame] | 419 | case KVM_REG_ARM_TIMER_CNT: |
Jintack Lim | 90de943 | 2017-02-03 10:20:00 -0500 | [diff] [blame] | 420 | return kvm_phys_timer_read() - vtimer->cntvoff; |
Andre Przywara | 39735a3 | 2013-12-13 14:23:26 +0100 | [diff] [blame] | 421 | case KVM_REG_ARM_TIMER_CVAL: |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 422 | return vtimer->cnt_cval; |
Andre Przywara | 39735a3 | 2013-12-13 14:23:26 +0100 | [diff] [blame] | 423 | } |
| 424 | return (u64)-1; |
| 425 | } |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 426 | |
Richard Cochran | b3c9950 | 2016-07-13 17:16:47 +0000 | [diff] [blame] | 427 | static int kvm_timer_starting_cpu(unsigned int cpu) |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 428 | { |
Richard Cochran | b3c9950 | 2016-07-13 17:16:47 +0000 | [diff] [blame] | 429 | kvm_timer_init_interrupt(NULL); |
| 430 | return 0; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 431 | } |
| 432 | |
Richard Cochran | b3c9950 | 2016-07-13 17:16:47 +0000 | [diff] [blame] | 433 | static int kvm_timer_dying_cpu(unsigned int cpu) |
| 434 | { |
| 435 | disable_percpu_irq(host_vtimer_irq); |
| 436 | return 0; |
| 437 | } |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 438 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 439 | int kvm_timer_hyp_init(void) |
| 440 | { |
Julien Grall | 29c2d6f | 2016-04-11 16:32:58 +0100 | [diff] [blame] | 441 | struct arch_timer_kvm_info *info; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 442 | int err; |
| 443 | |
Julien Grall | 29c2d6f | 2016-04-11 16:32:58 +0100 | [diff] [blame] | 444 | info = arch_timer_get_kvm_info(); |
| 445 | timecounter = &info->timecounter; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 446 | |
Christoffer Dall | 8e1a047 | 2016-12-05 10:32:11 +0100 | [diff] [blame] | 447 | if (!timecounter->cc) { |
| 448 | kvm_err("kvm_arch_timer: uninitialized timecounter\n"); |
| 449 | return -ENODEV; |
| 450 | } |
| 451 | |
Julien Grall | 29c2d6f | 2016-04-11 16:32:58 +0100 | [diff] [blame] | 452 | if (info->virtual_irq <= 0) { |
| 453 | kvm_err("kvm_arch_timer: invalid virtual timer IRQ: %d\n", |
| 454 | info->virtual_irq); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 455 | return -ENODEV; |
| 456 | } |
Julien Grall | 29c2d6f | 2016-04-11 16:32:58 +0100 | [diff] [blame] | 457 | host_vtimer_irq = info->virtual_irq; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 458 | |
Marc Zyngier | cabdc5c | 2016-08-16 15:03:02 +0100 | [diff] [blame] | 459 | host_vtimer_irq_flags = irq_get_trigger_type(host_vtimer_irq); |
| 460 | if (host_vtimer_irq_flags != IRQF_TRIGGER_HIGH && |
| 461 | host_vtimer_irq_flags != IRQF_TRIGGER_LOW) { |
| 462 | kvm_err("Invalid trigger for IRQ%d, assuming level low\n", |
| 463 | host_vtimer_irq); |
| 464 | host_vtimer_irq_flags = IRQF_TRIGGER_LOW; |
| 465 | } |
| 466 | |
Julien Grall | 29c2d6f | 2016-04-11 16:32:58 +0100 | [diff] [blame] | 467 | err = request_percpu_irq(host_vtimer_irq, kvm_arch_timer_handler, |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 468 | "kvm guest timer", kvm_get_running_vcpus()); |
| 469 | if (err) { |
| 470 | kvm_err("kvm_arch_timer: can't request interrupt %d (%d)\n", |
Julien Grall | 29c2d6f | 2016-04-11 16:32:58 +0100 | [diff] [blame] | 471 | host_vtimer_irq, err); |
Paolo Bonzini | 5d947a1 | 2016-09-08 12:45:59 +0200 | [diff] [blame] | 472 | return err; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 473 | } |
| 474 | |
Julien Grall | 29c2d6f | 2016-04-11 16:32:58 +0100 | [diff] [blame] | 475 | kvm_info("virtual timer IRQ%d\n", host_vtimer_irq); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 476 | |
Richard Cochran | b3c9950 | 2016-07-13 17:16:47 +0000 | [diff] [blame] | 477 | cpuhp_setup_state(CPUHP_AP_KVM_ARM_TIMER_STARTING, |
Thomas Gleixner | 73c1b41 | 2016-12-21 20:19:54 +0100 | [diff] [blame] | 478 | "kvm/arm/timer:starting", kvm_timer_starting_cpu, |
Richard Cochran | b3c9950 | 2016-07-13 17:16:47 +0000 | [diff] [blame] | 479 | kvm_timer_dying_cpu); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 480 | return err; |
| 481 | } |
| 482 | |
| 483 | void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu) |
| 484 | { |
| 485 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 486 | struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 487 | |
| 488 | timer_disarm(timer); |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 489 | kvm_vgic_unmap_phys_irq(vcpu, vtimer->irq.irq); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 490 | } |
| 491 | |
Christoffer Dall | 41a5448 | 2016-05-18 16:26:00 +0100 | [diff] [blame] | 492 | int kvm_timer_enable(struct kvm_vcpu *vcpu) |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 493 | { |
Christoffer Dall | 41a5448 | 2016-05-18 16:26:00 +0100 | [diff] [blame] | 494 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 495 | struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); |
Christoffer Dall | 41a5448 | 2016-05-18 16:26:00 +0100 | [diff] [blame] | 496 | struct irq_desc *desc; |
| 497 | struct irq_data *data; |
| 498 | int phys_irq; |
| 499 | int ret; |
| 500 | |
| 501 | if (timer->enabled) |
| 502 | return 0; |
| 503 | |
| 504 | /* |
| 505 | * Find the physical IRQ number corresponding to the host_vtimer_irq |
| 506 | */ |
| 507 | desc = irq_to_desc(host_vtimer_irq); |
| 508 | if (!desc) { |
| 509 | kvm_err("%s: no interrupt descriptor\n", __func__); |
| 510 | return -EINVAL; |
| 511 | } |
| 512 | |
| 513 | data = irq_desc_get_irq_data(desc); |
| 514 | while (data->parent_data) |
| 515 | data = data->parent_data; |
| 516 | |
| 517 | phys_irq = data->hwirq; |
| 518 | |
| 519 | /* |
| 520 | * Tell the VGIC that the virtual interrupt is tied to a |
| 521 | * physical interrupt. We do that once per VCPU. |
| 522 | */ |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 523 | ret = kvm_vgic_map_phys_irq(vcpu, vtimer->irq.irq, phys_irq); |
Christoffer Dall | 41a5448 | 2016-05-18 16:26:00 +0100 | [diff] [blame] | 524 | if (ret) |
| 525 | return ret; |
| 526 | |
Longpeng(Mike) | fd5ebf9 | 2016-11-09 10:50:14 +0800 | [diff] [blame] | 527 | timer->enabled = 1; |
Christoffer Dall | 41a5448 | 2016-05-18 16:26:00 +0100 | [diff] [blame] | 528 | |
| 529 | return 0; |
Christoffer Dall | 0597112 | 2014-12-12 21:19:23 +0100 | [diff] [blame] | 530 | } |
| 531 | |
Jintack Lim | 488f94d | 2016-12-01 14:32:05 -0500 | [diff] [blame] | 532 | /* |
| 533 | * On VHE system, we only need to configure trap on physical timer and counter |
| 534 | * accesses in EL0 and EL1 once, not for every world switch. |
| 535 | * The host kernel runs at EL2 with HCR_EL2.TGE == 1, |
| 536 | * and this makes those bits have no effect for the host kernel execution. |
| 537 | */ |
| 538 | void kvm_timer_init_vhe(void) |
| 539 | { |
| 540 | /* When HCR_EL2.E2H ==1, EL1PCEN and EL1PCTEN are shifted by 10 */ |
| 541 | u32 cnthctl_shift = 10; |
| 542 | u64 val; |
| 543 | |
| 544 | /* |
| 545 | * Disallow physical timer access for the guest. |
| 546 | * Physical counter access is allowed. |
| 547 | */ |
| 548 | val = read_sysreg(cnthctl_el2); |
| 549 | val &= ~(CNTHCTL_EL1PCEN << cnthctl_shift); |
| 550 | val |= (CNTHCTL_EL1PCTEN << cnthctl_shift); |
| 551 | write_sysreg(val, cnthctl_el2); |
| 552 | } |