// SPDX-License-Identifier: GPL-2.0-only1/*2* Copyright (C) 2012-2015 - ARM Ltd3* Author: Marc Zyngier <[email protected]>4*/56#include <clocksource/arm_arch_timer.h>7#include <linux/compiler.h>8#include <linux/kvm_host.h>910#include <asm/kvm_hyp.h>11#include <asm/kvm_mmu.h>1213void __kvm_timer_set_cntvoff(u64 cntvoff)14{15write_sysreg(cntvoff, cntvoff_el2);16}1718/*19* Should only be called on non-VHE or hVHE setups.20* VHE systems use EL2 timers and configure EL1 timers in kvm_timer_init_vhe().21*/22void __timer_disable_traps(struct kvm_vcpu *vcpu)23{24u64 set, clr, shift = 0;2526if (has_hvhe())27shift = 10;2829/* Allow physical timer/counter access for the host */30set = (CNTHCTL_EL1PCTEN | CNTHCTL_EL1PCEN) << shift;31clr = CNTHCTL_EL1TVT | CNTHCTL_EL1TVCT;3233sysreg_clear_set(cnthctl_el2, clr, set);34}3536/*37* Should only be called on non-VHE or hVHE setups.38* VHE systems use EL2 timers and configure EL1 timers in kvm_timer_init_vhe().39*/40void __timer_enable_traps(struct kvm_vcpu *vcpu)41{42u64 clr = 0, set = 0;4344/*45* Disallow physical timer access for the guest46* Physical counter access is allowed if no offset is enforced47* or running protected (we don't offset anything in this case).48*/49clr = CNTHCTL_EL1PCEN;50if (is_protected_kvm_enabled() ||51!kern_hyp_va(vcpu->kvm)->arch.timer_data.poffset)52set |= CNTHCTL_EL1PCTEN;53else54clr |= CNTHCTL_EL1PCTEN;5556if (has_hvhe()) {57clr <<= 10;58set <<= 10;59}6061/*62* Trap the virtual counter/timer if we have a broken cntvoff63* implementation.64*/65if (has_broken_cntvoff())66set |= CNTHCTL_EL1TVT | CNTHCTL_EL1TVCT;6768sysreg_clear_set(cnthctl_el2, clr, set);69}707172