diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index d1705fdc046ac9be06c4f2ac443b8df598c8605d..9db5bd439a29081536cbce9db78561bf325bb589 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -2175,6 +2175,16 @@ config ARM64_EPAN if the cpu does not implement the feature. endmenu # "ARMv8.7 architectural features" +menu "ARMv8.6 architectural features" + +config ARM64_TWED + bool "Enable support for delayed trapping of WFE" + default y + help + Delayed Trapping of WFE (part of the ARMv8.6 Extensions) + +endmenu + config ARM64_SVE bool "ARM Scalable Vector Extension support" default y diff --git a/arch/arm64/include/asm/kvm_arm.h b/arch/arm64/include/asm/kvm_arm.h index 1095c6647e9665267e6aa67bac2dd7bb11b091f1..a661c99d129a52e40c12fe2d2abe8ad57913c7ab 100644 --- a/arch/arm64/include/asm/kvm_arm.h +++ b/arch/arm64/include/asm/kvm_arm.h @@ -14,6 +14,7 @@ /* Hyp Configuration Register (HCR) bits */ +#define HCR_TWEDEN (UL(1) << 59) #define HCR_TID5 (UL(1) << 58) #define HCR_DCT (UL(1) << 57) #define HCR_ATA_SHIFT 56 @@ -74,6 +75,13 @@ #define HCR_VM (UL(1) << 0) #define HCR_RES0 ((UL(1) << 48) | (UL(1) << 39)) +#ifdef CONFIG_ARM64_TWED +#define HCR_TWEDEL_SHIFT 60 +#define HCR_TWEDEL_MAX (UL(0xf)) +#define HCR_TWEDEL_MASK (HCR_TWEDEL_MAX << HCR_TWEDEL_SHIFT) +#define HCR_TWEDEL (UL(1) << HCR_TWEDEL_SHIFT) +#endif + /* * The bits we set in HCR: * TLOR: Trap LORegion register accesses diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h index 3d6725ff0bf6d24577c84c6814fcc814b0681fb8..8bca453739d03e1488f4359eb89f19190a046211 100644 --- a/arch/arm64/include/asm/kvm_emulate.h +++ b/arch/arm64/include/asm/kvm_emulate.h @@ -124,6 +124,33 @@ static inline void vcpu_set_wfx_traps(struct kvm_vcpu *vcpu) vcpu->arch.hcr_el2 |= HCR_TWI; } +#ifdef CONFIG_ARM64_TWED +static inline void vcpu_twed_enable(struct kvm_vcpu *vcpu) +{ + vcpu->arch.hcr_el2 |= HCR_TWEDEN; +} + +static inline void vcpu_twed_disable(struct kvm_vcpu *vcpu) +{ + vcpu->arch.hcr_el2 &= ~HCR_TWEDEN; +} + +static inline void vcpu_set_twed(struct kvm_vcpu *vcpu) +{ + u64 delay = (u64)twedel; + + if (delay > HCR_TWEDEL_MAX) + delay = HCR_TWEDEL_MAX; + + vcpu->arch.hcr_el2 &= ~HCR_TWEDEL_MASK; + vcpu->arch.hcr_el2 |= (delay << HCR_TWEDEL_SHIFT); +} +#else +static inline void vcpu_twed_enable(struct kvm_vcpu *vcpu) {}; +static inline void vcpu_twed_disable(struct kvm_vcpu *vcpu) {}; +static inline void vcpu_set_twed(struct kvm_vcpu *vcpu) {}; +#endif + static inline void vcpu_ptrauth_enable(struct kvm_vcpu *vcpu) { vcpu->arch.hcr_el2 |= (HCR_API | HCR_APK); diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index af06ccb7ee343304535c30b8855d6de8d8b1f4fc..d1ff5a095735bab5a9f447219c8b2d184c22b684 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -1154,4 +1154,12 @@ static inline void kvm_hyp_reserve(void) { } void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu); bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu); +#ifdef CONFIG_ARM64_TWED +#define use_twed() (has_twed() && twed_enable) +extern bool twed_enable; +extern unsigned int twedel; +#else +#define use_twed() (false) +#endif + #endif /* __ARM64_KVM_HOST_H__ */ diff --git a/arch/arm64/include/asm/virt.h b/arch/arm64/include/asm/virt.h index 261d6e9df2e1009f8efab95d90dda611fcba1b3e..198f4f35534e17a1d63b0b468a6b5d0a0e9b08c7 100644 --- a/arch/arm64/include/asm/virt.h +++ b/arch/arm64/include/asm/virt.h @@ -156,6 +156,11 @@ static inline bool is_hyp_nvhe(void) return is_hyp_mode_available() && !is_kernel_in_hyp_mode(); } +static __always_inline bool has_twed(void) +{ + return cpus_have_const_cap(ARM64_HAS_TWED); +} + #endif /* __ASSEMBLY__ */ #endif /* ! __ASM__VIRT_H */ diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c index 455e72ce080a8b2c684185d8400477ec37b89856..9169d622805d98ecffd36920f8b83b660e69f416 100644 --- a/arch/arm64/kernel/cpufeature.c +++ b/arch/arm64/kernel/cpufeature.c @@ -2722,6 +2722,18 @@ static const struct arm64_cpu_capabilities arm64_features[] = { .matches = has_cpuid_feature, ARM64_CPUID_FIELDS(ID_AA64MMFR2_EL1, EVT, IMP) }, +#ifdef CONFIG_ARM64_TWED + { + .desc = "Delayed Trapping of WFE", + .capability = ARM64_HAS_TWED, + .type = ARM64_CPUCAP_SYSTEM_FEATURE, + .matches = has_cpuid_feature, + .sys_reg = SYS_ID_AA64MMFR1_EL1, + .field_pos = ID_AA64MMFR1_EL1_TWED_SHIFT, + .sign = FTR_UNSIGNED, + .min_field_value = 1, + }, +#endif {}, }; diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 4866b3f7b4ea3847d885e00cfac47a4d7abf9da3..05f74655b1716c857dfcdb95158f272f550cd89b 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -64,6 +64,14 @@ bool is_kvm_arm_initialised(void) return kvm_arm_initialised; } +#ifdef CONFIG_ARM64_TWED +bool twed_enable = false; +module_param(twed_enable, bool, S_IRUGO | S_IWUSR); + +unsigned int twedel = 0; +module_param(twedel, uint, S_IRUGO | S_IWUSR); +#endif + int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) { return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE; @@ -983,6 +991,13 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) kvm_arm_setup_debug(vcpu); kvm_arch_vcpu_ctxflush_fp(vcpu); + if (use_twed()) { + vcpu_twed_enable(vcpu); + vcpu_set_twed(vcpu); + } else { + vcpu_twed_disable(vcpu); + } + /************************************************************** * Enter the guest */ diff --git a/arch/arm64/tools/cpucaps b/arch/arm64/tools/cpucaps index 569ecec76c16767d1e6d33519a59fbe2a623f734..fea9f598f4ef59ecf9eb829c1c7552d30aba12cb 100644 --- a/arch/arm64/tools/cpucaps +++ b/arch/arm64/tools/cpucaps @@ -52,6 +52,7 @@ HAS_TIDCP1 HAS_TLB_RANGE HAS_VIRT_HOST_EXTN HAS_WFXT +HAS_TWED HW_DBM KVM_HVHE KVM_PROTECTED_MODE