Thomas Gleixner | 4505153 | 2019-05-29 16:57:47 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 ARM Ltd. |
| 4 | * Author: Marc Zyngier <marc.zyngier@arm.com> |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef __ASM_ARM_KVM_ARCH_TIMER_H |
| 8 | #define __ASM_ARM_KVM_ARCH_TIMER_H |
| 9 | |
| 10 | #include <linux/clocksource.h> |
| 11 | #include <linux/hrtimer.h> |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 12 | |
Andre Przywara | 84135d3 | 2018-07-05 16:48:23 +0100 | [diff] [blame] | 13 | enum kvm_arch_timers { |
| 14 | TIMER_PTIMER, |
| 15 | TIMER_VTIMER, |
| 16 | NR_KVM_TIMERS |
| 17 | }; |
| 18 | |
| 19 | enum kvm_arch_timer_regs { |
| 20 | TIMER_REG_CNT, |
| 21 | TIMER_REG_CVAL, |
| 22 | TIMER_REG_TVAL, |
| 23 | TIMER_REG_CTL, |
| 24 | }; |
| 25 | |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 26 | struct arch_timer_context { |
Christoffer Dall | e604dd5 | 2018-09-18 10:08:18 -0700 | [diff] [blame] | 27 | struct kvm_vcpu *vcpu; |
| 28 | |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 29 | /* Timer IRQ */ |
| 30 | struct kvm_irq_level irq; |
| 31 | |
Christoffer Dall | e604dd5 | 2018-09-18 10:08:18 -0700 | [diff] [blame] | 32 | /* Emulated Timer (may be unused) */ |
| 33 | struct hrtimer hrtimer; |
Christoffer Dall | 9e01dc7 | 2019-02-19 14:04:30 +0100 | [diff] [blame] | 34 | |
Christoffer Dall | bee038a6 | 2019-01-04 13:31:22 +0100 | [diff] [blame] | 35 | /* |
| 36 | * We have multiple paths which can save/restore the timer state onto |
| 37 | * the hardware, so we need some way of keeping track of where the |
| 38 | * latest state is. |
| 39 | */ |
| 40 | bool loaded; |
| 41 | |
Christoffer Dall | 9e01dc7 | 2019-02-19 14:04:30 +0100 | [diff] [blame] | 42 | /* Duplicated state from arch_timer.c for convenience */ |
| 43 | u32 host_timer_irq; |
| 44 | u32 host_timer_irq_flags; |
Christoffer Dall | e604dd5 | 2018-09-18 10:08:18 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
Christoffer Dall | bee038a6 | 2019-01-04 13:31:22 +0100 | [diff] [blame] | 47 | struct timer_map { |
| 48 | struct arch_timer_context *direct_vtimer; |
| 49 | struct arch_timer_context *direct_ptimer; |
| 50 | struct arch_timer_context *emul_ptimer; |
| 51 | }; |
| 52 | |
Christoffer Dall | e604dd5 | 2018-09-18 10:08:18 -0700 | [diff] [blame] | 53 | struct arch_timer_cpu { |
| 54 | struct arch_timer_context timers[NR_KVM_TIMERS]; |
| 55 | |
| 56 | /* Background timer used when the guest is not running */ |
| 57 | struct hrtimer bg_timer; |
| 58 | |
| 59 | /* Is the timer enabled */ |
| 60 | bool enabled; |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 61 | }; |
| 62 | |
Marc Zyngier | f384dcf | 2017-12-07 11:46:15 +0000 | [diff] [blame] | 63 | int kvm_timer_hyp_init(bool); |
Christoffer Dall | 41a5448 | 2016-05-18 16:26:00 +0100 | [diff] [blame] | 64 | int kvm_timer_enable(struct kvm_vcpu *vcpu); |
Christoffer Dall | 85e69ad | 2017-05-02 20:14:06 +0200 | [diff] [blame] | 65 | int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 66 | void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu); |
Marc Zyngier | 3c5ff0c | 2020-04-22 08:58:22 +0100 | [diff] [blame] | 67 | void kvm_timer_sync_user(struct kvm_vcpu *vcpu); |
Alexander Graf | d9e1397 | 2016-09-27 21:08:06 +0200 | [diff] [blame] | 68 | bool kvm_timer_should_notify_user(struct kvm_vcpu *vcpu); |
| 69 | void kvm_timer_update_run(struct kvm_vcpu *vcpu); |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 70 | void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu); |
Alex Bennée | 1df08ba | 2014-07-04 15:54:14 +0100 | [diff] [blame] | 71 | |
| 72 | u64 kvm_arm_timer_get_reg(struct kvm_vcpu *, u64 regid); |
| 73 | int kvm_arm_timer_set_reg(struct kvm_vcpu *, u64 regid, u64 value); |
| 74 | |
Christoffer Dall | 99a1db7 | 2017-05-02 20:19:15 +0200 | [diff] [blame] | 75 | int kvm_arm_timer_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr); |
| 76 | int kvm_arm_timer_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr); |
| 77 | int kvm_arm_timer_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr); |
| 78 | |
Christoffer Dall | 1c88ab7 | 2017-01-06 16:07:48 +0100 | [diff] [blame] | 79 | bool kvm_timer_is_pending(struct kvm_vcpu *vcpu); |
| 80 | |
Jintack Lim | 7b6b463 | 2017-02-03 10:20:08 -0500 | [diff] [blame] | 81 | u64 kvm_phys_timer_read(void); |
| 82 | |
Christoffer Dall | b103cc3 | 2016-10-16 20:30:38 +0200 | [diff] [blame] | 83 | void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu); |
Marc Zyngier | 9b4a300 | 2016-01-29 19:04:48 +0000 | [diff] [blame] | 84 | void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu); |
| 85 | |
Jintack Lim | 488f94d | 2016-12-01 14:32:05 -0500 | [diff] [blame] | 86 | void kvm_timer_init_vhe(void); |
Jintack Lim | fbb4aee | 2017-02-03 10:19:59 -0500 | [diff] [blame] | 87 | |
Christoffer Dall | 4c60e36 | 2017-10-27 19:34:30 +0200 | [diff] [blame] | 88 | bool kvm_arch_timer_get_input_level(int vintid); |
| 89 | |
Christoffer Dall | e604dd5 | 2018-09-18 10:08:18 -0700 | [diff] [blame] | 90 | #define vcpu_timer(v) (&(v)->arch.timer_cpu) |
| 91 | #define vcpu_get_timer(v,t) (&vcpu_timer(v)->timers[(t)]) |
| 92 | #define vcpu_vtimer(v) (&(v)->arch.timer_cpu.timers[TIMER_VTIMER]) |
| 93 | #define vcpu_ptimer(v) (&(v)->arch.timer_cpu.timers[TIMER_PTIMER]) |
Andre Przywara | 84135d3 | 2018-07-05 16:48:23 +0100 | [diff] [blame] | 94 | |
Christoffer Dall | 9e01dc7 | 2019-02-19 14:04:30 +0100 | [diff] [blame] | 95 | #define arch_timer_ctx_index(ctx) ((ctx) - vcpu_timer((ctx)->vcpu)->timers) |
| 96 | |
Andre Przywara | 84135d3 | 2018-07-05 16:48:23 +0100 | [diff] [blame] | 97 | u64 kvm_arm_timer_read_sysreg(struct kvm_vcpu *vcpu, |
| 98 | enum kvm_arch_timers tmr, |
| 99 | enum kvm_arch_timer_regs treg); |
| 100 | void kvm_arm_timer_write_sysreg(struct kvm_vcpu *vcpu, |
| 101 | enum kvm_arch_timers tmr, |
| 102 | enum kvm_arch_timer_regs treg, |
| 103 | u64 val); |
Christoffer Dall | b103cc3 | 2016-10-16 20:30:38 +0200 | [diff] [blame] | 104 | |
Marc Zyngier | 41ce82f | 2019-06-28 15:23:43 +0100 | [diff] [blame] | 105 | /* Needed for tracing */ |
| 106 | u32 timer_get_ctl(struct arch_timer_context *ctxt); |
| 107 | u64 timer_get_cval(struct arch_timer_context *ctxt); |
| 108 | |
Marc Zyngier | 53e7240 | 2013-01-23 13:21:58 -0500 | [diff] [blame] | 109 | #endif |