xref: /openbmc/linux/arch/arm64/kvm/hyp/nvhe/timer-sr.c (revision 726ccdba)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012-2015 - ARM Ltd
4  * Author: Marc Zyngier <marc.zyngier@arm.com>
5  */
6 
7 #include <clocksource/arm_arch_timer.h>
8 #include <linux/compiler.h>
9 #include <linux/kvm_host.h>
10 
11 #include <asm/kvm_hyp.h>
12 #include <asm/kvm_mmu.h>
13 
14 void __kvm_timer_set_cntvoff(u64 cntvoff)
15 {
16 	write_sysreg(cntvoff, cntvoff_el2);
17 }
18 
19 /*
20  * Should only be called on non-VHE systems.
21  * VHE systems use EL2 timers and configure EL1 timers in kvm_timer_init_vhe().
22  */
23 void __timer_disable_traps(struct kvm_vcpu *vcpu)
24 {
25 	u64 val;
26 
27 	/* Allow physical timer/counter access for the host */
28 	val = read_sysreg(cnthctl_el2);
29 	val |= CNTHCTL_EL1PCTEN | CNTHCTL_EL1PCEN;
30 	write_sysreg(val, cnthctl_el2);
31 }
32 
33 /*
34  * Should only be called on non-VHE systems.
35  * VHE systems use EL2 timers and configure EL1 timers in kvm_timer_init_vhe().
36  */
37 void __timer_enable_traps(struct kvm_vcpu *vcpu)
38 {
39 	u64 clr = 0, set = 0;
40 
41 	/*
42 	 * Disallow physical timer access for the guest
43 	 * Physical counter access is allowed if no offset is enforced
44 	 * or running protected (we don't offset anything in this case).
45 	 */
46 	clr = CNTHCTL_EL1PCEN;
47 	if (is_protected_kvm_enabled() ||
48 	    !kern_hyp_va(vcpu->kvm)->arch.timer_data.poffset)
49 		set |= CNTHCTL_EL1PCTEN;
50 	else
51 		clr |= CNTHCTL_EL1PCTEN;
52 
53 	sysreg_clear_set(cnthctl_el2, clr, set);
54 }
55