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> |
| 27 | |
Marc Zyngier | 7275acd | 2013-05-14 14:31:01 +0100 | [diff] [blame] | 28 | #include <kvm/arm_vgic.h> |
| 29 | #include <kvm/arm_arch_timer.h> |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 30 | |
Christoffer Dall | e21f091 | 2015-08-30 13:57:20 +0200 | [diff] [blame] | 31 | #include "trace.h" |
| 32 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 33 | static struct timecounter *timecounter; |
| 34 | static struct workqueue_struct *wqueue; |
Anup Patel | 5ae7f87 | 2013-04-30 12:02:15 +0530 | [diff] [blame] | 35 | static unsigned int host_vtimer_irq; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 36 | |
Marc Zyngier | 9b4a300 | 2016-01-29 19:04:48 +0000 | [diff] [blame] | 37 | void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu) |
| 38 | { |
| 39 | vcpu->arch.timer_cpu.active_cleared_last = false; |
| 40 | } |
| 41 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 42 | static cycle_t kvm_phys_timer_read(void) |
| 43 | { |
| 44 | return timecounter->cc->read(timecounter->cc); |
| 45 | } |
| 46 | |
| 47 | static bool timer_is_armed(struct arch_timer_cpu *timer) |
| 48 | { |
| 49 | return timer->armed; |
| 50 | } |
| 51 | |
| 52 | /* timer_arm: as in "arm the timer", not as in ARM the company */ |
| 53 | static void timer_arm(struct arch_timer_cpu *timer, u64 ns) |
| 54 | { |
| 55 | timer->armed = true; |
| 56 | hrtimer_start(&timer->timer, ktime_add_ns(ktime_get(), ns), |
| 57 | HRTIMER_MODE_ABS); |
| 58 | } |
| 59 | |
| 60 | static void timer_disarm(struct arch_timer_cpu *timer) |
| 61 | { |
| 62 | if (timer_is_armed(timer)) { |
| 63 | hrtimer_cancel(&timer->timer); |
| 64 | cancel_work_sync(&timer->expired); |
| 65 | timer->armed = false; |
| 66 | } |
| 67 | } |
| 68 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 69 | static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id) |
| 70 | { |
| 71 | struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id; |
| 72 | |
| 73 | /* |
| 74 | * We disable the timer in the world switch and let it be |
| 75 | * handled by kvm_timer_sync_hwstate(). Getting a timer |
| 76 | * interrupt at this point is a sure sign of some major |
| 77 | * breakage. |
| 78 | */ |
| 79 | pr_warn("Unexpected interrupt %d on vcpu %p\n", irq, vcpu); |
| 80 | return IRQ_HANDLED; |
| 81 | } |
| 82 | |
Christoffer Dall | 1a74847 | 2015-03-13 17:02:55 +0000 | [diff] [blame] | 83 | /* |
| 84 | * Work function for handling the backup timer that we schedule when a vcpu is |
| 85 | * no longer running, but had a timer programmed to fire in the future. |
| 86 | */ |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 87 | static void kvm_timer_inject_irq_work(struct work_struct *work) |
| 88 | { |
| 89 | struct kvm_vcpu *vcpu; |
| 90 | |
| 91 | vcpu = container_of(work, struct kvm_vcpu, arch.timer_cpu.expired); |
| 92 | vcpu->arch.timer_cpu.armed = false; |
Christoffer Dall | 1a74847 | 2015-03-13 17:02:55 +0000 | [diff] [blame] | 93 | |
Marc Zyngier | 1c5631c | 2016-04-06 09:37:22 +0100 | [diff] [blame] | 94 | WARN_ON(!kvm_timer_should_fire(vcpu)); |
| 95 | |
Christoffer Dall | 1a74847 | 2015-03-13 17:02:55 +0000 | [diff] [blame] | 96 | /* |
| 97 | * If the vcpu is blocked we want to wake it up so that it will see |
| 98 | * the timer has expired when entering the guest. |
| 99 | */ |
| 100 | kvm_vcpu_kick(vcpu); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 101 | } |
| 102 | |
Marc Zyngier | 1c5631c | 2016-04-06 09:37:22 +0100 | [diff] [blame] | 103 | static u64 kvm_timer_compute_delta(struct kvm_vcpu *vcpu) |
| 104 | { |
| 105 | cycle_t cval, now; |
| 106 | |
| 107 | cval = vcpu->arch.timer_cpu.cntv_cval; |
| 108 | now = kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff; |
| 109 | |
| 110 | if (now < cval) { |
| 111 | u64 ns; |
| 112 | |
| 113 | ns = cyclecounter_cyc2ns(timecounter->cc, |
| 114 | cval - now, |
| 115 | timecounter->mask, |
| 116 | &timecounter->frac); |
| 117 | return ns; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 123 | static enum hrtimer_restart kvm_timer_expire(struct hrtimer *hrt) |
| 124 | { |
| 125 | struct arch_timer_cpu *timer; |
Marc Zyngier | 1c5631c | 2016-04-06 09:37:22 +0100 | [diff] [blame] | 126 | struct kvm_vcpu *vcpu; |
| 127 | u64 ns; |
| 128 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 129 | timer = container_of(hrt, struct arch_timer_cpu, timer); |
Marc Zyngier | 1c5631c | 2016-04-06 09:37:22 +0100 | [diff] [blame] | 130 | vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu); |
| 131 | |
| 132 | /* |
| 133 | * Check that the timer has really expired from the guest's |
| 134 | * PoV (NTP on the host may have forced it to expire |
| 135 | * early). If we should have slept longer, restart it. |
| 136 | */ |
| 137 | ns = kvm_timer_compute_delta(vcpu); |
| 138 | if (unlikely(ns)) { |
| 139 | hrtimer_forward_now(hrt, ns_to_ktime(ns)); |
| 140 | return HRTIMER_RESTART; |
| 141 | } |
| 142 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 143 | queue_work(wqueue, &timer->expired); |
| 144 | return HRTIMER_NORESTART; |
| 145 | } |
| 146 | |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 147 | static bool kvm_timer_irq_can_fire(struct kvm_vcpu *vcpu) |
| 148 | { |
| 149 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
| 150 | |
| 151 | return !(timer->cntv_ctl & ARCH_TIMER_CTRL_IT_MASK) && |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 152 | (timer->cntv_ctl & ARCH_TIMER_CTRL_ENABLE); |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Christoffer Dall | 1a74847 | 2015-03-13 17:02:55 +0000 | [diff] [blame] | 155 | bool kvm_timer_should_fire(struct kvm_vcpu *vcpu) |
| 156 | { |
| 157 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
| 158 | cycle_t cval, now; |
| 159 | |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 160 | if (!kvm_timer_irq_can_fire(vcpu)) |
Christoffer Dall | 1a74847 | 2015-03-13 17:02:55 +0000 | [diff] [blame] | 161 | return false; |
| 162 | |
| 163 | cval = timer->cntv_cval; |
| 164 | now = kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff; |
| 165 | |
| 166 | return cval <= now; |
| 167 | } |
| 168 | |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 169 | static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level) |
| 170 | { |
| 171 | int ret; |
| 172 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
| 173 | |
| 174 | BUG_ON(!vgic_initialized(vcpu->kvm)); |
| 175 | |
Marc Zyngier | 9b4a300 | 2016-01-29 19:04:48 +0000 | [diff] [blame] | 176 | timer->active_cleared_last = false; |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 177 | timer->irq.level = new_level; |
Andre Przywara | a7e33ad | 2016-04-13 11:03:02 +0100 | [diff] [blame] | 178 | trace_kvm_timer_update_irq(vcpu->vcpu_id, timer->irq.irq, |
Christoffer Dall | e21f091 | 2015-08-30 13:57:20 +0200 | [diff] [blame] | 179 | timer->irq.level); |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 180 | ret = kvm_vgic_inject_mapped_irq(vcpu->kvm, vcpu->vcpu_id, |
Andre Przywara | a7e33ad | 2016-04-13 11:03:02 +0100 | [diff] [blame] | 181 | timer->irq.irq, |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 182 | timer->irq.level); |
| 183 | WARN_ON(ret); |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Check if there was a change in the timer state (should we raise or lower |
| 188 | * the line level to the GIC). |
| 189 | */ |
Andre Przywara | b3aff6c | 2016-02-03 16:56:51 +0000 | [diff] [blame] | 190 | static int kvm_timer_update_state(struct kvm_vcpu *vcpu) |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 191 | { |
| 192 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
| 193 | |
| 194 | /* |
| 195 | * If userspace modified the timer registers via SET_ONE_REG before |
| 196 | * the vgic was initialized, we mustn't set the timer->irq.level value |
| 197 | * because the guest would never see the interrupt. Instead wait |
| 198 | * until we call this function from kvm_timer_flush_hwstate. |
| 199 | */ |
Christoffer Dall | 41a5448 | 2016-05-18 16:26:00 +0100 | [diff] [blame^] | 200 | if (!vgic_initialized(vcpu->kvm) || !timer->enabled) |
Andre Przywara | b3aff6c | 2016-02-03 16:56:51 +0000 | [diff] [blame] | 201 | return -ENODEV; |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 202 | |
| 203 | if (kvm_timer_should_fire(vcpu) != timer->irq.level) |
| 204 | kvm_timer_update_irq(vcpu, !timer->irq.level); |
Andre Przywara | b3aff6c | 2016-02-03 16:56:51 +0000 | [diff] [blame] | 205 | |
| 206 | return 0; |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 209 | /* |
| 210 | * Schedule the background timer before calling kvm_vcpu_block, so that this |
| 211 | * thread is removed from its waitqueue and made runnable when there's a timer |
| 212 | * interrupt to handle. |
| 213 | */ |
| 214 | void kvm_timer_schedule(struct kvm_vcpu *vcpu) |
| 215 | { |
| 216 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 217 | |
| 218 | BUG_ON(timer_is_armed(timer)); |
| 219 | |
| 220 | /* |
| 221 | * No need to schedule a background timer if the guest timer has |
| 222 | * already expired, because kvm_vcpu_block will return before putting |
| 223 | * the thread to sleep. |
| 224 | */ |
| 225 | if (kvm_timer_should_fire(vcpu)) |
| 226 | return; |
| 227 | |
| 228 | /* |
| 229 | * If the timer is not capable of raising interrupts (disabled or |
| 230 | * masked), then there's no more work for us to do. |
| 231 | */ |
| 232 | if (!kvm_timer_irq_can_fire(vcpu)) |
| 233 | return; |
| 234 | |
| 235 | /* The timer has not yet expired, schedule a background timer */ |
Marc Zyngier | 1c5631c | 2016-04-06 09:37:22 +0100 | [diff] [blame] | 236 | timer_arm(timer, kvm_timer_compute_delta(vcpu)); |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | void kvm_timer_unschedule(struct kvm_vcpu *vcpu) |
| 240 | { |
| 241 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
| 242 | timer_disarm(timer); |
| 243 | } |
| 244 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 245 | /** |
| 246 | * kvm_timer_flush_hwstate - prepare to move the virt timer to the cpu |
| 247 | * @vcpu: The vcpu pointer |
| 248 | * |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 249 | * Check if the virtual timer has expired while we were running in the host, |
| 250 | * and inject an interrupt if that was the case. |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 251 | */ |
| 252 | void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu) |
| 253 | { |
| 254 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
Christoffer Dall | cff9211 | 2015-10-16 12:41:21 +0200 | [diff] [blame] | 255 | bool phys_active; |
| 256 | int ret; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 257 | |
Andre Przywara | b3aff6c | 2016-02-03 16:56:51 +0000 | [diff] [blame] | 258 | if (kvm_timer_update_state(vcpu)) |
| 259 | return; |
Christoffer Dall | cff9211 | 2015-10-16 12:41:21 +0200 | [diff] [blame] | 260 | |
| 261 | /* |
Christoffer Dall | 0e3dfda | 2015-11-24 16:23:05 +0100 | [diff] [blame] | 262 | * If we enter the guest with the virtual input level to the VGIC |
| 263 | * asserted, then we have already told the VGIC what we need to, and |
| 264 | * we don't need to exit from the guest until the guest deactivates |
| 265 | * the already injected interrupt, so therefore we should set the |
| 266 | * hardware active state to prevent unnecessary exits from the guest. |
| 267 | * |
| 268 | * Also, if we enter the guest with the virtual timer interrupt active, |
| 269 | * then it must be active on the physical distributor, because we set |
| 270 | * the HW bit and the guest must be able to deactivate the virtual and |
| 271 | * physical interrupt at the same time. |
| 272 | * |
| 273 | * Conversely, if the virtual input level is deasserted and the virtual |
| 274 | * interrupt is not active, then always clear the hardware active state |
| 275 | * to ensure that hardware interrupts from the timer triggers a guest |
| 276 | * exit. |
| 277 | */ |
Andre Przywara | e262f41 | 2016-04-13 10:03:49 +0100 | [diff] [blame] | 278 | phys_active = timer->irq.level || |
Andre Przywara | a7e33ad | 2016-04-13 11:03:02 +0100 | [diff] [blame] | 279 | kvm_vgic_map_is_active(vcpu, timer->irq.irq); |
Christoffer Dall | cff9211 | 2015-10-16 12:41:21 +0200 | [diff] [blame] | 280 | |
Marc Zyngier | 9b4a300 | 2016-01-29 19:04:48 +0000 | [diff] [blame] | 281 | /* |
| 282 | * We want to avoid hitting the (re)distributor as much as |
| 283 | * possible, as this is a potentially expensive MMIO access |
| 284 | * (not to mention locks in the irq layer), and a solution for |
| 285 | * this is to cache the "active" state in memory. |
| 286 | * |
| 287 | * Things to consider: we cannot cache an "active set" state, |
| 288 | * because the HW can change this behind our back (it becomes |
| 289 | * "clear" in the HW). We must then restrict the caching to |
| 290 | * the "clear" state. |
| 291 | * |
| 292 | * The cache is invalidated on: |
| 293 | * - vcpu put, indicating that the HW cannot be trusted to be |
| 294 | * in a sane state on the next vcpu load, |
| 295 | * - any change in the interrupt state |
| 296 | * |
| 297 | * Usage conditions: |
| 298 | * - cached value is "active clear" |
| 299 | * - value to be programmed is "active clear" |
| 300 | */ |
| 301 | if (timer->active_cleared_last && !phys_active) |
| 302 | return; |
| 303 | |
Christoffer Dall | b452cb5 | 2016-06-04 15:41:00 +0100 | [diff] [blame] | 304 | ret = irq_set_irqchip_state(host_vtimer_irq, |
Christoffer Dall | cff9211 | 2015-10-16 12:41:21 +0200 | [diff] [blame] | 305 | IRQCHIP_STATE_ACTIVE, |
| 306 | phys_active); |
| 307 | WARN_ON(ret); |
Marc Zyngier | 9b4a300 | 2016-01-29 19:04:48 +0000 | [diff] [blame] | 308 | |
| 309 | timer->active_cleared_last = !phys_active; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /** |
| 313 | * kvm_timer_sync_hwstate - sync timer state from cpu |
| 314 | * @vcpu: The vcpu pointer |
| 315 | * |
Christoffer Dall | d35268d | 2015-08-25 19:48:21 +0200 | [diff] [blame] | 316 | * Check if the virtual timer has expired while we were running in the guest, |
| 317 | * and inject an interrupt if that was the case. |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 318 | */ |
| 319 | void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu) |
| 320 | { |
| 321 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 322 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 323 | BUG_ON(timer_is_armed(timer)); |
| 324 | |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 325 | /* |
| 326 | * The guest could have modified the timer registers or the timer |
| 327 | * could have expired, update the timer state. |
| 328 | */ |
| 329 | kvm_timer_update_state(vcpu); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 330 | } |
| 331 | |
Marc Zyngier | f120cd6 | 2014-06-23 13:59:13 +0100 | [diff] [blame] | 332 | int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu, |
| 333 | const struct kvm_irq_level *irq) |
Anup Patel | 5ae7f87 | 2013-04-30 12:02:15 +0530 | [diff] [blame] | 334 | { |
| 335 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
| 336 | |
| 337 | /* |
| 338 | * The vcpu timer irq number cannot be determined in |
| 339 | * kvm_timer_vcpu_init() because it is called much before |
| 340 | * kvm_vcpu_set_target(). To handle this, we determine |
| 341 | * vcpu timer irq number when the vcpu is reset. |
| 342 | */ |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 343 | timer->irq.irq = irq->irq; |
Marc Zyngier | f120cd6 | 2014-06-23 13:59:13 +0100 | [diff] [blame] | 344 | |
| 345 | /* |
Christoffer Dall | 4ad9e16 | 2015-09-04 16:24:39 +0200 | [diff] [blame] | 346 | * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8 |
| 347 | * and to 0 for ARMv7. We provide an implementation that always |
| 348 | * resets the timer to be disabled and unmasked and is compliant with |
| 349 | * the ARMv7 architecture. |
| 350 | */ |
| 351 | timer->cntv_ctl = 0; |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 352 | kvm_timer_update_state(vcpu); |
Christoffer Dall | 4ad9e16 | 2015-09-04 16:24:39 +0200 | [diff] [blame] | 353 | |
Christoffer Dall | 41a5448 | 2016-05-18 16:26:00 +0100 | [diff] [blame^] | 354 | return 0; |
Anup Patel | 5ae7f87 | 2013-04-30 12:02:15 +0530 | [diff] [blame] | 355 | } |
| 356 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 357 | void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu) |
| 358 | { |
| 359 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
| 360 | |
| 361 | INIT_WORK(&timer->expired, kvm_timer_inject_irq_work); |
| 362 | hrtimer_init(&timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
| 363 | timer->timer.function = kvm_timer_expire; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | static void kvm_timer_init_interrupt(void *info) |
| 367 | { |
Anup Patel | 5ae7f87 | 2013-04-30 12:02:15 +0530 | [diff] [blame] | 368 | enable_percpu_irq(host_vtimer_irq, 0); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 369 | } |
| 370 | |
Andre Przywara | 39735a3 | 2013-12-13 14:23:26 +0100 | [diff] [blame] | 371 | int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value) |
| 372 | { |
| 373 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
| 374 | |
| 375 | switch (regid) { |
| 376 | case KVM_REG_ARM_TIMER_CTL: |
| 377 | timer->cntv_ctl = value; |
| 378 | break; |
| 379 | case KVM_REG_ARM_TIMER_CNT: |
| 380 | vcpu->kvm->arch.timer.cntvoff = kvm_phys_timer_read() - value; |
| 381 | break; |
| 382 | case KVM_REG_ARM_TIMER_CVAL: |
| 383 | timer->cntv_cval = value; |
| 384 | break; |
| 385 | default: |
| 386 | return -1; |
| 387 | } |
Christoffer Dall | 4b4b451 | 2015-08-30 15:01:27 +0200 | [diff] [blame] | 388 | |
| 389 | kvm_timer_update_state(vcpu); |
Andre Przywara | 39735a3 | 2013-12-13 14:23:26 +0100 | [diff] [blame] | 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid) |
| 394 | { |
| 395 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
| 396 | |
| 397 | switch (regid) { |
| 398 | case KVM_REG_ARM_TIMER_CTL: |
| 399 | return timer->cntv_ctl; |
| 400 | case KVM_REG_ARM_TIMER_CNT: |
| 401 | return kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff; |
| 402 | case KVM_REG_ARM_TIMER_CVAL: |
| 403 | return timer->cntv_cval; |
| 404 | } |
| 405 | return (u64)-1; |
| 406 | } |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 407 | |
| 408 | static int kvm_timer_cpu_notify(struct notifier_block *self, |
| 409 | unsigned long action, void *cpu) |
| 410 | { |
| 411 | switch (action) { |
| 412 | case CPU_STARTING: |
| 413 | case CPU_STARTING_FROZEN: |
| 414 | kvm_timer_init_interrupt(NULL); |
| 415 | break; |
| 416 | case CPU_DYING: |
| 417 | case CPU_DYING_FROZEN: |
Anup Patel | 5ae7f87 | 2013-04-30 12:02:15 +0530 | [diff] [blame] | 418 | disable_percpu_irq(host_vtimer_irq); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 419 | break; |
| 420 | } |
| 421 | |
| 422 | return NOTIFY_OK; |
| 423 | } |
| 424 | |
| 425 | static struct notifier_block kvm_timer_cpu_nb = { |
| 426 | .notifier_call = kvm_timer_cpu_notify, |
| 427 | }; |
| 428 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 429 | int kvm_timer_hyp_init(void) |
| 430 | { |
Julien Grall | 29c2d6f | 2016-04-11 16:32:58 +0100 | [diff] [blame] | 431 | struct arch_timer_kvm_info *info; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 432 | int err; |
| 433 | |
Julien Grall | 29c2d6f | 2016-04-11 16:32:58 +0100 | [diff] [blame] | 434 | info = arch_timer_get_kvm_info(); |
| 435 | timecounter = &info->timecounter; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 436 | |
Julien Grall | 29c2d6f | 2016-04-11 16:32:58 +0100 | [diff] [blame] | 437 | if (info->virtual_irq <= 0) { |
| 438 | kvm_err("kvm_arch_timer: invalid virtual timer IRQ: %d\n", |
| 439 | info->virtual_irq); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 440 | return -ENODEV; |
| 441 | } |
Julien Grall | 29c2d6f | 2016-04-11 16:32:58 +0100 | [diff] [blame] | 442 | host_vtimer_irq = info->virtual_irq; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 443 | |
Julien Grall | 29c2d6f | 2016-04-11 16:32:58 +0100 | [diff] [blame] | 444 | err = request_percpu_irq(host_vtimer_irq, kvm_arch_timer_handler, |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 445 | "kvm guest timer", kvm_get_running_vcpus()); |
| 446 | if (err) { |
| 447 | kvm_err("kvm_arch_timer: can't request interrupt %d (%d)\n", |
Julien Grall | 29c2d6f | 2016-04-11 16:32:58 +0100 | [diff] [blame] | 448 | host_vtimer_irq, err); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 449 | goto out; |
| 450 | } |
| 451 | |
Ming Lei | 553f809 | 2014-04-07 01:36:08 +0800 | [diff] [blame] | 452 | err = __register_cpu_notifier(&kvm_timer_cpu_nb); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 453 | if (err) { |
| 454 | kvm_err("Cannot register timer CPU notifier\n"); |
| 455 | goto out_free; |
| 456 | } |
| 457 | |
| 458 | wqueue = create_singlethread_workqueue("kvm_arch_timer"); |
| 459 | if (!wqueue) { |
| 460 | err = -ENOMEM; |
| 461 | goto out_free; |
| 462 | } |
| 463 | |
Julien Grall | 29c2d6f | 2016-04-11 16:32:58 +0100 | [diff] [blame] | 464 | kvm_info("virtual timer IRQ%d\n", host_vtimer_irq); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 465 | on_each_cpu(kvm_timer_init_interrupt, NULL, 1); |
| 466 | |
| 467 | goto out; |
| 468 | out_free: |
Julien Grall | 29c2d6f | 2016-04-11 16:32:58 +0100 | [diff] [blame] | 469 | free_percpu_irq(host_vtimer_irq, kvm_get_running_vcpus()); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 470 | out: |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 471 | return err; |
| 472 | } |
| 473 | |
| 474 | void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu) |
| 475 | { |
| 476 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
| 477 | |
| 478 | timer_disarm(timer); |
Andre Przywara | a7e33ad | 2016-04-13 11:03:02 +0100 | [diff] [blame] | 479 | kvm_vgic_unmap_phys_irq(vcpu, timer->irq.irq); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 480 | } |
| 481 | |
Christoffer Dall | 41a5448 | 2016-05-18 16:26:00 +0100 | [diff] [blame^] | 482 | int kvm_timer_enable(struct kvm_vcpu *vcpu) |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 483 | { |
Christoffer Dall | 41a5448 | 2016-05-18 16:26:00 +0100 | [diff] [blame^] | 484 | struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; |
| 485 | struct irq_desc *desc; |
| 486 | struct irq_data *data; |
| 487 | int phys_irq; |
| 488 | int ret; |
| 489 | |
| 490 | if (timer->enabled) |
| 491 | return 0; |
| 492 | |
| 493 | /* |
| 494 | * Find the physical IRQ number corresponding to the host_vtimer_irq |
| 495 | */ |
| 496 | desc = irq_to_desc(host_vtimer_irq); |
| 497 | if (!desc) { |
| 498 | kvm_err("%s: no interrupt descriptor\n", __func__); |
| 499 | return -EINVAL; |
| 500 | } |
| 501 | |
| 502 | data = irq_desc_get_irq_data(desc); |
| 503 | while (data->parent_data) |
| 504 | data = data->parent_data; |
| 505 | |
| 506 | phys_irq = data->hwirq; |
| 507 | |
| 508 | /* |
| 509 | * Tell the VGIC that the virtual interrupt is tied to a |
| 510 | * physical interrupt. We do that once per VCPU. |
| 511 | */ |
| 512 | ret = kvm_vgic_map_phys_irq(vcpu, timer->irq.irq, phys_irq); |
| 513 | if (ret) |
| 514 | return ret; |
| 515 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 516 | |
Christoffer Dall | 0597112 | 2014-12-12 21:19:23 +0100 | [diff] [blame] | 517 | /* |
| 518 | * There is a potential race here between VCPUs starting for the first |
| 519 | * time, which may be enabling the timer multiple times. That doesn't |
| 520 | * hurt though, because we're just setting a variable to the same |
| 521 | * variable that it already was. The important thing is that all |
| 522 | * VCPUs have the enabled variable set, before entering the guest, if |
| 523 | * the arch timers are enabled. |
| 524 | */ |
| 525 | if (timecounter && wqueue) |
Christoffer Dall | 41a5448 | 2016-05-18 16:26:00 +0100 | [diff] [blame^] | 526 | timer->enabled = 1; |
| 527 | |
| 528 | return 0; |
Christoffer Dall | 0597112 | 2014-12-12 21:19:23 +0100 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | void kvm_timer_init(struct kvm *kvm) |
| 532 | { |
| 533 | kvm->arch.timer.cntvoff = kvm_phys_timer_read(); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 534 | } |