18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2012-2015 - ARM Ltd
48c2ecf20Sopenharmony_ci * Author: Marc Zyngier <marc.zyngier@arm.com>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <clocksource/arm_arch_timer.h>
88c2ecf20Sopenharmony_ci#include <linux/compiler.h>
98c2ecf20Sopenharmony_ci#include <linux/kvm_host.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <asm/kvm_hyp.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_civoid __kvm_timer_set_cntvoff(u64 cntvoff)
148c2ecf20Sopenharmony_ci{
158c2ecf20Sopenharmony_ci	write_sysreg(cntvoff, cntvoff_el2);
168c2ecf20Sopenharmony_ci}
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/*
198c2ecf20Sopenharmony_ci * Should only be called on non-VHE systems.
208c2ecf20Sopenharmony_ci * VHE systems use EL2 timers and configure EL1 timers in kvm_timer_init_vhe().
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_civoid __timer_disable_traps(struct kvm_vcpu *vcpu)
238c2ecf20Sopenharmony_ci{
248c2ecf20Sopenharmony_ci	u64 val;
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci	/* Allow physical timer/counter access for the host */
278c2ecf20Sopenharmony_ci	val = read_sysreg(cnthctl_el2);
288c2ecf20Sopenharmony_ci	val |= CNTHCTL_EL1PCTEN | CNTHCTL_EL1PCEN;
298c2ecf20Sopenharmony_ci	write_sysreg(val, cnthctl_el2);
308c2ecf20Sopenharmony_ci}
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/*
338c2ecf20Sopenharmony_ci * Should only be called on non-VHE systems.
348c2ecf20Sopenharmony_ci * VHE systems use EL2 timers and configure EL1 timers in kvm_timer_init_vhe().
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_civoid __timer_enable_traps(struct kvm_vcpu *vcpu)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	u64 val;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	/*
418c2ecf20Sopenharmony_ci	 * Disallow physical timer access for the guest
428c2ecf20Sopenharmony_ci	 * Physical counter access is allowed
438c2ecf20Sopenharmony_ci	 */
448c2ecf20Sopenharmony_ci	val = read_sysreg(cnthctl_el2);
458c2ecf20Sopenharmony_ci	val &= ~CNTHCTL_EL1PCEN;
468c2ecf20Sopenharmony_ci	val |= CNTHCTL_EL1PCTEN;
478c2ecf20Sopenharmony_ci	write_sysreg(val, cnthctl_el2);
488c2ecf20Sopenharmony_ci}
49