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