git: 353b6a5bcb5a - main - arm64: Add in_vhe() to find if the kernel is in VHE

From: Andrew Turner <andrew_at_FreeBSD.org>
Date: Wed, 21 Feb 2024 11:27:52 UTC
The branch main has been updated by andrew:

URL: https://cgit.FreeBSD.org/src/commit/?id=353b6a5bcb5ae6943903b38ebcc0922b059ff8b8

commit 353b6a5bcb5ae6943903b38ebcc0922b059ff8b8
Author:     Andrew Turner <andrew@FreeBSD.org>
AuthorDate: 2024-01-11 17:01:52 +0000
Commit:     Andrew Turner <andrew@FreeBSD.org>
CommitDate: 2024-02-21 11:26:29 +0000

    arm64: Add in_vhe() to find if the kernel is in VHE
    
    Add a function to support devices that may need to know if the kernel
    has enabled the Armv8.1 Virtulization Host Extensions (FEAT_VHE).
    Some devices, e.g. the generic timer, will need to know, e.g. use a
    different interrupt.
    
    Sponsored by:   Arm Ltd
    Differential Revision:  https://reviews.freebsd.org/D43973
---
 sys/arm64/arm64/machdep.c   | 8 ++++++++
 sys/arm64/include/machdep.h | 1 +
 2 files changed, 9 insertions(+)

diff --git a/sys/arm64/arm64/machdep.c b/sys/arm64/arm64/machdep.c
index 9ce1c40b674c..c7f451543687 100644
--- a/sys/arm64/arm64/machdep.c
+++ b/sys/arm64/arm64/machdep.c
@@ -215,6 +215,14 @@ has_hyp(void)
 	return (boot_el == 2 && (hcr_el2 & HCR_E2H) == 0);
 }
 
+bool
+in_vhe(void)
+{
+	/* If we are currently in EL2 then must be in VHE */
+	return ((READ_SPECIALREG(CurrentEL) & CURRENTEL_EL_MASK) ==
+	    CURRENTEL_EL_EL2);
+}
+
 static void
 cpu_startup(void *dummy)
 {
diff --git a/sys/arm64/include/machdep.h b/sys/arm64/include/machdep.h
index 80cb28bcfbeb..2f2960ae39f2 100644
--- a/sys/arm64/include/machdep.h
+++ b/sys/arm64/include/machdep.h
@@ -48,6 +48,7 @@ extern enum arm64_bus arm64_bus_method;
 
 void dbg_init(void);
 bool has_hyp(void);
+bool in_vhe(void);
 void initarm(struct arm64_bootparams *);
 vm_offset_t parse_boot_param(struct arm64_bootparams *abp);
 #ifdef FDT