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