1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015 - ARM Ltd
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
7 #include <hyp/debug-sr.h>
9 #include <linux/compiler.h>
10 #include <linux/kvm_host.h>
12 #include <asm/debug-monitors.h>
13 #include <asm/kvm_asm.h>
14 #include <asm/kvm_hyp.h>
15 #include <asm/kvm_mmu.h>
17 static void __debug_save_spe(u64 *pmscr_el1)
21 /* Clear pmscr in case of early return */
24 /* SPE present on this CPU? */
25 if (!cpuid_feature_extract_unsigned_field(read_sysreg(id_aa64dfr0_el1),
26 ID_AA64DFR0_PMSVER_SHIFT))
29 /* Yes; is it owned by EL3? */
30 reg = read_sysreg_s(SYS_PMBIDR_EL1);
31 if (reg & BIT(SYS_PMBIDR_EL1_P_SHIFT))
34 /* No; is the host actually using the thing? */
35 reg = read_sysreg_s(SYS_PMBLIMITR_EL1);
36 if (!(reg & BIT(SYS_PMBLIMITR_EL1_E_SHIFT)))
39 /* Yes; save the control register and disable data generation */
40 *pmscr_el1 = read_sysreg_s(SYS_PMSCR_EL1);
41 write_sysreg_s(0, SYS_PMSCR_EL1);
44 /* Now drain all buffered data to memory */
49 static void __debug_restore_spe(u64 pmscr_el1)
54 /* The host page table is installed, but not yet synchronised */
57 /* Re-enable data generation */
58 write_sysreg_s(pmscr_el1, SYS_PMSCR_EL1);
61 void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu)
63 /* Disable and flush SPE data generation */
64 __debug_save_spe(&vcpu->arch.host_debug_state.pmscr_el1);
67 void __debug_switch_to_guest(struct kvm_vcpu *vcpu)
69 __debug_switch_to_guest_common(vcpu);
72 void __debug_restore_host_buffers_nvhe(struct kvm_vcpu *vcpu)
74 __debug_restore_spe(vcpu->arch.host_debug_state.pmscr_el1);
77 void __debug_switch_to_host(struct kvm_vcpu *vcpu)
79 __debug_switch_to_host_common(vcpu);
82 u32 __kvm_get_mdcr_el2(void)
84 return read_sysreg(mdcr_el2);