xref: /linux/arch/arm64/kvm/hyp/nvhe/timer-sr.c (revision e2ee2e9b159094527ae7ad78058b1316f62fc5b7)
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 or hVHE setups.
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 set, clr, shift = 0;
26 
27 	if (has_hvhe())
28 		shift = 10;
29 
30 	/* Allow physical timer/counter access for the host */
31 	set = (CNTHCTL_EL1PCTEN | CNTHCTL_EL1PCEN) << shift;
32 	clr = CNTHCTL_EL1TVT | CNTHCTL_EL1TVCT;
33 
34 	sysreg_clear_set(cnthctl_el2, clr, set);
35 }
36 
37 /*
38  * Should only be called on non-VHE or hVHE setups.
39  * VHE systems use EL2 timers and configure EL1 timers in kvm_timer_init_vhe().
40  */
41 void __timer_enable_traps(struct kvm_vcpu *vcpu)
42 {
43 	u64 clr = 0, set = 0;
44 
45 	/*
46 	 * Disallow physical timer access for the guest
47 	 * Physical counter access is allowed if no offset is enforced
48 	 * or running protected (we don't offset anything in this case).
49 	 */
50 	clr = CNTHCTL_EL1PCEN;
51 	if (is_protected_kvm_enabled() ||
52 	    !kern_hyp_va(vcpu->kvm)->arch.timer_data.poffset)
53 		set |= CNTHCTL_EL1PCTEN;
54 	else
55 		clr |= CNTHCTL_EL1PCTEN;
56 
57 	if (has_hvhe()) {
58 		clr <<= 10;
59 		set <<= 10;
60 	}
61 
62 	/*
63 	 * Trap the virtual counter/timer if we have a broken cntvoff
64 	 * implementation.
65 	 */
66 	if (has_broken_cntvoff())
67 		set |= CNTHCTL_EL1TVT | CNTHCTL_EL1TVCT;
68 
69 	sysreg_clear_set(cnthctl_el2, clr, set);
70 }
71