Skip to content

Commit 1431af3

Browse files
author
Marc Zyngier
committed
arm64: KVM: Implement timer save/restore
Implement the timer save restore as a direct translation of the assembly code version. Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
1 parent f68d2b1 commit 1431af3

4 files changed

Lines changed: 81 additions & 0 deletions

File tree

‎arch/arm64/kvm/hyp/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
obj-$(CONFIG_KVM_ARM_HOST) += vgic-v2-sr.o
66
obj-$(CONFIG_KVM_ARM_HOST) += vgic-v3-sr.o
7+
obj-$(CONFIG_KVM_ARM_HOST) += timer-sr.o

‎arch/arm64/kvm/hyp/hyp.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,8 @@ void __vgic_v2_restore_state(struct kvm_vcpu *vcpu);
3535
void __vgic_v3_save_state(struct kvm_vcpu *vcpu);
3636
void __vgic_v3_restore_state(struct kvm_vcpu *vcpu);
3737

38+
void __timer_save_state(struct kvm_vcpu *vcpu);
39+
void __timer_restore_state(struct kvm_vcpu *vcpu);
40+
3841
#endif /* __ARM64_KVM_HYP_H__ */
3942

‎arch/arm64/kvm/hyp/timer-sr.c‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (C) 2012-2015 - 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, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#include <clocksource/arm_arch_timer.h>
19+
#include <linux/compiler.h>
20+
#include <linux/kvm_host.h>
21+
22+
#include <asm/kvm_mmu.h>
23+
24+
#include "hyp.h"
25+
26+
/* vcpu is already in the HYP VA space */
27+
void __hyp_text __timer_save_state(struct kvm_vcpu *vcpu)
28+
{
29+
struct kvm *kvm = kern_hyp_va(vcpu->kvm);
30+
struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
31+
u64 val;
32+
33+
if (kvm->arch.timer.enabled) {
34+
timer->cntv_ctl = read_sysreg(cntv_ctl_el0);
35+
timer->cntv_cval = read_sysreg(cntv_cval_el0);
36+
}
37+
38+
/* Disable the virtual timer */
39+
write_sysreg(0, cntv_ctl_el0);
40+
41+
/* Allow physical timer/counter access for the host */
42+
val = read_sysreg(cnthctl_el2);
43+
val |= CNTHCTL_EL1PCTEN | CNTHCTL_EL1PCEN;
44+
write_sysreg(val, cnthctl_el2);
45+
46+
/* Clear cntvoff for the host */
47+
write_sysreg(0, cntvoff_el2);
48+
}
49+
50+
void __hyp_text __timer_restore_state(struct kvm_vcpu *vcpu)
51+
{
52+
struct kvm *kvm = kern_hyp_va(vcpu->kvm);
53+
struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
54+
u64 val;
55+
56+
/*
57+
* Disallow physical timer access for the guest
58+
* Physical counter access is allowed
59+
*/
60+
val = read_sysreg(cnthctl_el2);
61+
val &= ~CNTHCTL_EL1PCEN;
62+
val |= CNTHCTL_EL1PCTEN;
63+
write_sysreg(val, cnthctl_el2);
64+
65+
if (kvm->arch.timer.enabled) {
66+
write_sysreg(kvm->arch.timer.cntvoff, cntvoff_el2);
67+
write_sysreg(timer->cntv_cval, cntv_cval_el0);
68+
isb();
69+
write_sysreg(timer->cntv_ctl, cntv_ctl_el0);
70+
}
71+
}

‎include/clocksource/arm_arch_timer.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
#define ARCH_TIMER_CTRL_IT_MASK (1 << 1)
2424
#define ARCH_TIMER_CTRL_IT_STAT (1 << 2)
2525

26+
#define CNTHCTL_EL1PCTEN (1 << 0)
27+
#define CNTHCTL_EL1PCEN (1 << 1)
28+
#define CNTHCTL_EVNTEN (1 << 2)
29+
#define CNTHCTL_EVNTDIR (1 << 3)
30+
#define CNTHCTL_EVNTI (0xF << 4)
31+
2632
enum arch_timer_reg {
2733
ARCH_TIMER_REG_CTRL,
2834
ARCH_TIMER_REG_TVAL,

0 commit comments

Comments
 (0)