git: 22554457440b - main - arm64/vmm: Implement vgic_v5

From: Andrew Turner <andrew_at_FreeBSD.org>
Date: Mon, 23 Nov 2026 17:01:58 UTC
The branch main has been updated by andrew:

URL: https://cgit.FreeBSD.org/src/commit/?id=22554457440bcf26076c7996a6c3d92592d72b92

commit 22554457440bcf26076c7996a6c3d92592d72b92
Author:     Sarah Walker <sarah.walker2@arm.com>
AuthorDate: 2026-07-30 11:44:26 +0000
Commit:     Andrew Turner <andrew@FreeBSD.org>
CommitDate: 2026-11-23 16:47:19 +0000

    arm64/vmm: Implement vgic_v5
    
    Adds support for GICv5 as both a host and guest. Additionally, on platforms with
    FEAT_GICE_LEGACY, supports a GICv3 guest under a GICv5 host.
    
    The current implementation only implements the IRS component. The ITS is not
    currently emulated, which prevents the use of MSIs.
    
    Co-developed-by: Andrew Turner <andrew@FreeBSD.org>
    Reviewed by:    andrew
    Sponsored by:   Arm Ltd
    Differential Revision:  https://reviews.freebsd.org/D60115
---
 sys/arm64/arm64/gicv5.c           |   3 -
 sys/arm64/arm64/gicv5reg.h        |  47 ++
 sys/arm64/arm64/gicv5var.h        |   3 +
 sys/arm64/vmm/arm64.h             |  55 ++-
 sys/arm64/vmm/io/vgic.c           |  37 +-
 sys/arm64/vmm/io/vgic.h           |   1 +
 sys/arm64/vmm/io/vgic_if.m        |   4 +
 sys/arm64/vmm/io/vgic_internal.h  |  61 ++-
 sys/arm64/vmm/io/vgic_v3.c        | 131 +++---
 sys/arm64/vmm/io/vgic_v5.c        | 906 ++++++++++++++++++++++++++++++++++++++
 sys/arm64/vmm/io/vtimer.c         |  36 +-
 sys/arm64/vmm/vmm.c               |  41 +-
 sys/arm64/vmm/vmm_arm64.c         |  21 +
 sys/arm64/vmm/vmm_dev_machdep.c   |   3 +-
 sys/arm64/vmm/vmm_hyp.c           | 230 ++++++++--
 sys/arm64/vmm/vmm_hyp_exception.S |  17 +-
 sys/conf/files.arm64              |   1 +
 sys/modules/vmm/Makefile          |   1 +
 18 files changed, 1467 insertions(+), 131 deletions(-)

diff --git a/sys/arm64/arm64/gicv5.c b/sys/arm64/arm64/gicv5.c
index 8a4b16f80b75..cfca19e6ebf9 100644
--- a/sys/arm64/arm64/gicv5.c
+++ b/sys/arm64/arm64/gicv5.c
@@ -69,9 +69,6 @@
 #include "gicv5var.h"
 #include "gic_v3_var.h" /* For GICV3_IVAR_NIRQS */
 
-#define	GICV5_PPIS_PER_REG			64
-#define	GICV5_PPI_COUNT				128
-
 #define	LPI_IPI_BASE		0
 #define	LPI_IPI_LIMIT		(LPI_IPI_BASE + (mp_maxid + 1) * INTR_IPI_COUNT)
 #define	LPI_IS_IPI(lpi)		((lpi) < LPI_ITS_BASE)
diff --git a/sys/arm64/arm64/gicv5reg.h b/sys/arm64/arm64/gicv5reg.h
index 1522bcf68213..701737540c86 100644
--- a/sys/arm64/arm64/gicv5reg.h
+++ b/sys/arm64/arm64/gicv5reg.h
@@ -662,6 +662,19 @@
 #define	ICH_HFGRTR_EL2_CRn		12
 #define	ICH_HFGRTR_EL2_CRm		9
 #define	ICH_HFGRTR_EL2_op2		4
+#define ICH_HFGRTR_EL2_ICC_PPI_ACTIVERn_EL1     (0x1ul << 20)
+#define ICH_HFGRTR_EL2_ICC_PPI_PRIORITYn_EL1    (0x1ul << 19)
+#define ICH_HFGRTR_EL2_ICC_PPI_PENDRn_EL1       (0x1ul << 18)
+#define ICH_HFGRTR_EL2_ICC_PPI_ENABLERn_EL1     (0x1ul << 17)
+#define ICH_HFGRTR_EL2_ICC_PPI_HMRn_EL1         (0x1ul << 16)
+#define ICH_HFGRTR_EL2_ICC_IAFFIDR_EL1          (0x1ul << 7)
+#define ICH_HFGRTR_EL2_ICC_ICSR_EL1             (0x1ul << 6)
+#define ICH_HFGRTR_EL2_ICC_PCR_EL1              (0x1ul << 5)
+#define ICH_HFGRTR_EL2_ICC_HPPIR_EL1            (0x1ul << 4)
+#define ICH_HFGRTR_EL2_ICC_HAPR_EL1             (0x1ul << 3)
+#define ICH_HFGRTR_EL2_ICC_CR0_EL1              (0x1ul << 2)
+#define ICH_HFGRTR_EL2_ICC_IDRn_EL1             (0x1ul << 1)
+#define ICH_HFGRTR_EL2_ICC_APR_EL1              (0x1ul << 0)
 
 /*
  * ICH_HFGWTR_EL2
@@ -673,6 +686,14 @@
 #define	ICH_HFGWTR_EL2_CRn		12
 #define	ICH_HFGWTR_EL2_CRm		9
 #define	ICH_HFGWTR_EL2_op2		6
+#define ICH_HFGWTR_EL2_ICC_PPI_ACTIVERn_EL1     (0x1ul << 20)
+#define ICH_HFGWTR_EL2_ICC_PPI_PRIORITYn_EL1    (0x1ul << 19)
+#define ICH_HFGWTR_EL2_ICC_PPI_PENDRn_EL1       (0x1ul << 18)
+#define ICH_HFGWTR_EL2_ICC_PPI_ENABLERn_EL1     (0x1ul << 17)
+#define ICH_HFGWTR_EL2_ICC_ICSR_EL1             (0x1ul << 6)
+#define ICH_HFGWTR_EL2_ICC_PCR_EL1              (0x1ul << 5)
+#define ICH_HFGWTR_EL2_ICC_CR0_EL1              (0x1ul << 2)
+#define ICH_HFGWTR_EL2_ICC_APR_EL1              (0x1ul << 0)
 
 /*
  * ICH_HPPIR_EL2
@@ -858,7 +879,26 @@
 #define	IRS_IDR7			0x001c
 #define	 IRS_IDR7_SPI_BASE		0x00ffffff
 #define	IRS_IIDR			0x0040
+#define	 IRS_IIDR_PRODUCT_ID_SHIFT	20
+#define	 IRS_IIDR_PRODUCT_ID_MASK	(0xfffu << IRS_IIDR_PRODUCT_ID_SHIFT)
+#define	 IRS_IIDR_VARIANT_SHIFT		16
+#define	 IRS_IIDR_VARIANT_MASK		(0xfu << IRS_IIDR_VARIANT_SHIFT)
+#define	 IRS_IIDR_REVISION_SHIFT	12
+#define	 IRS_IIDR_REVISION_MASK		(0xfu << IRS_IIDR_REVISION_SHIFT)
+#define	 IRS_IIDR_IMPLEMENTER_SHIFT	0
+#define	 IRS_IIDR_IMPLEMENTER_MASK	(0xfffu << IRS_IIDR_IMPLEMENTED_SHIFT)
 #define	IRS_AIDR			0x0044
+#define	 IRS_AIDR_COMPONENT_SHIFT	0
+#define	 IRS_AIDR_COMPONENT_IRS		(0x0u << IRS_AIDR_COMPONENT_SHIFT)
+#define	 IRS_AIDR_COMPONENT_ITS		(0x1u << IRS_AIDR_COMPONENT_SHIFT)
+#define	 IRS_AIDR_COMPONENT_IWB		(0x2u << IRS_AIDR_COMPONENT_SHIFT)
+#define	 IRS_AIDR_COMPONENT_MASK	(0xfu << IRS_AIDR_COMPONENT_SHIFT)
+#define	 IRS_AIDR_ARCH_MAJOR_REV_SHIFT	4
+#define	 IRS_AIDR_ARCH_MAJOR_REV_V5	(0x0u << IRS_AIDR_ARCH_MAJOR_REV_SHIFT)
+#define	 IRS_AIDR_ARCH_MAJOR_REV_MASK	(0xfu << IRS_AIDR_ARCH_MAJOR_REV_SHIFT)
+#define	 IRS_AIDR_ARCH_MINOR_REV_SHIFT	0
+#define	 IRS_AIDR_ARCH_MINOR_REV_0	(0x0u << IRS_AIDR_ARCH_MINOR_REV_SHIFT)
+#define	 IRS_AIDR_ARCH_MINOR_REV_MASK	(0xfu << IRS_AIDR_ARCH_MINOR_REV_SHIFT)
 #define	IRS_CR0				0x0080
 #define	 IRS_CR0_IDLE			(0x1u << 1)
 #define	 IRS_CR0_IRSEN			(0x1u << 0)
@@ -904,6 +944,7 @@
 #define	IRS_SYNC_STATUSR		0x00c4
 #define	IRS_SPI_VMR			0x0100
 #define	IRS_SPI_SELR			0x0108
+#define	 IRS_SPI_SELR_ID_MASK		(0xffffffu << 0)
 #define	IRS_SPI_DOMAINR			0x010c
 #define	IRS_SPI_RESAMPLER		0x0110
 #define	IRS_SPI_CFGR			0x0114
@@ -913,8 +954,14 @@
 #define	 IRS_SPI_STATUSR_V		(0x1u << 1)
 #define	 IRS_SPI_STATUSR_IDLE		(0x1u << 0)
 #define	IRS_PE_SELR			0x0140
+#define	 IRS_PE_SELR_IAFFID_SHIFT	0
+#define	 IRS_PE_SELR_IAFFID_MASK	(0xffffu << IRS_PE_SELR_IAFFID_SHIFT)
 #define	IRS_PE_STATUSR			0x0144
+#define	 IRS_PE_CR0_ONLINE		(0x1 << 2)
+#define	 IRS_PE_CR0_V			(0x1 << 1)
+#define	 IRS_PE_CR0_IDLE		(0x1 << 0)
 #define	IRS_PE_CR0			0x0148
+#define	 IRS_PE_CR0_DPS			(0x1 << 0)
 #define	IRS_IST_BASER			0x0180
 #define	 IRS_IST_BASER_ADDR_LIMIT	0x0100000000000000ul
 #define	 IRS_IST_BASER_ADDR_MASK	0x00ffffffffffffc0ul
diff --git a/sys/arm64/arm64/gicv5var.h b/sys/arm64/arm64/gicv5var.h
index 24da63953ba7..a03bfbb59601 100644
--- a/sys/arm64/arm64/gicv5var.h
+++ b/sys/arm64/arm64/gicv5var.h
@@ -60,6 +60,9 @@ struct gicv5_devinfo {
 	struct gicv5_irs	*di_irs;
 };
 
+#define GICV5_PPIS_PER_REG	64
+#define GICV5_PPI_COUNT		128
+
 #define	GICV5_IVAR_LPI_START	5000
 
 __BUS_ACCESSOR(gicv5, lpi_start, GICV5, LPI_START, u_int);
diff --git a/sys/arm64/vmm/arm64.h b/sys/arm64/vmm/arm64.h
index 5279ca546984..9014546bd0b4 100644
--- a/sys/arm64/vmm/arm64.h
+++ b/sys/arm64/vmm/arm64.h
@@ -36,8 +36,8 @@
 #include "io/vgic_v3.h"
 #include "io/vtimer.h"
 
-struct vgic_v3;
-struct vgic_v3_cpu;
+struct vgic;
+struct vgic_cpu;
 
 /*
   Encode the index of a given register within VNCR into the enum value.
@@ -149,6 +149,40 @@ enum hypctx_sysreg {
 	HOST_CNTHCTL_EL2,
 	HOST_CNTVOFF_EL2,
 
+
+	/* GICv5 registers */
+	HOST_ICH_PPI_ACTIVER0_EL2,
+	HOST_ICH_PPI_ACTIVER1_EL2,
+	HOST_ICH_PPI_DVIR0_EL2,
+	HOST_ICH_PPI_DVIR1_EL2,
+	HOST_ICH_PPI_ENABLER0_EL2,
+	HOST_ICH_PPI_ENABLER1_EL2,
+	HOST_ICH_PPI_PENDR0_EL2,
+	HOST_ICH_PPI_PENDR1_EL2,
+	HOST_ICH_PPI_PRIORITYR0_EL2,
+	HOST_ICH_PPI_PRIORITYR1_EL2,
+	HOST_ICH_PPI_PRIORITYR2_EL2,
+	HOST_ICH_PPI_PRIORITYR3_EL2,
+	HOST_ICH_PPI_PRIORITYR4_EL2,
+	HOST_ICH_PPI_PRIORITYR5_EL2,
+	HOST_ICH_PPI_PRIORITYR6_EL2,
+	HOST_ICH_PPI_PRIORITYR7_EL2,
+	HOST_ICH_PPI_PRIORITYR8_EL2,
+	HOST_ICH_PPI_PRIORITYR9_EL2,
+	HOST_ICH_PPI_PRIORITYR10_EL2,
+	HOST_ICH_PPI_PRIORITYR11_EL2,
+	HOST_ICH_PPI_PRIORITYR12_EL2,
+	HOST_ICH_PPI_PRIORITYR13_EL2,
+	HOST_ICH_PPI_PRIORITYR14_EL2,
+	HOST_ICH_PPI_PRIORITYR15_EL2,
+	HOST_ICH_HFGITR_EL2,
+	HOST_ICH_HFGRTR_EL2,
+	HOST_ICH_HFGWTR_EL2,
+	HOST_ICH_VCTLR_EL2,
+	HOST_ICH_HPPIR_EL2,
+	HOST_ICH_APR_EL2,
+	HOST_ICH_APR_RING_EL2,
+
 	NR_NON_VNCR_REGS,
 
 	/* VNCR Registers */
@@ -285,7 +319,8 @@ struct hypctx {
 		uint16_t	ich_lr_num;
 		uint16_t	ich_apr_num;
 	} vgic_v3;
-	struct vgic_v3_cpu	*vgic_cpu;
+	struct vgic_cpu		*vgic_cpu;
+
 	bool			has_exception;
 	bool			dbg_oslock;
 
@@ -346,12 +381,15 @@ struct hyp {
 	uint64_t	cntvoff_el2;    /* VM-wide virtual timer offset */
 	uint64_t	el2_addr;	/* The address of this in el2 space */
 	uint64_t	feats;		/* Which features are enabled */
-#define	HYP_FEAT_HCX		(0x1ul << 0)
-#define	HYP_FEAT_ECV_POFF	(0x1ul << 1)
-#define	HYP_FEAT_FGT		(0x1ul << 2)
-#define	HYP_FEAT_FGT2		(0x1ul << 3)
+#define	HYP_FEAT_HCX			(0x1ul << 0)
+#define	HYP_FEAT_ECV_POFF		(0x1ul << 1)
+#define	HYP_FEAT_FGT			(0x1ul << 2)
+#define	HYP_FEAT_FGT2			(0x1ul << 3)
+#define	HYP_FEAT_GICV3_OR_LEGACY	(0x1ul << 4)
+#define	HYP_FEAT_GICV5			(0x1ul << 5)
 	bool		vgic_attached;
-	struct vgic_v3	*vgic;
+	struct vgic	*vgic;
+	int		vgic_ver;
 	struct hypctx	*ctx[];
 };
 
@@ -365,5 +403,6 @@ uint64_t	vmm_call_hyp(uint64_t, ...);
 
 struct hypctx *arm64_get_active_vcpu(void);
 void raise_data_insn_abort(struct hypctx *, uint64_t, bool, int);
+int vmm_get_max_ipa_bits(void);
 
 #endif /* !_VMM_ARM64_H_ */
diff --git a/sys/arm64/vmm/io/vgic.c b/sys/arm64/vmm/io/vgic.c
index 582280823f1e..a71b93a0d02f 100644
--- a/sys/arm64/vmm/io/vgic.c
+++ b/sys/arm64/vmm/io/vgic.c
@@ -29,6 +29,11 @@
 #include <sys/types.h>
 #include <sys/systm.h>
 #include <sys/bus.h>
+#include <sys/malloc.h>
+
+#include <dev/vmm/vmm_vm.h>
+
+#include <arm64/vmm/arm64.h>
 
 #include "vgic.h"
 #include "vgic_if.h"
@@ -124,10 +129,34 @@ vgic_init(void)
 	VGIC_INIT(vgic_dev);
 }
 
+int
+vgic_get_version(void)
+{
+	return (VGIC_GET_VERSION(vgic_dev));
+}
+
 int
 vgic_attach_to_vm(struct hyp *hyp, struct vm_vgic_descr *descr)
 {
-	return (VGIC_ATTACH_TO_VM(vgic_dev, hyp, descr));
+	int rv;
+
+	rv = VGIC_ATTACH_TO_VM(vgic_dev, hyp, descr);
+	if (rv != 0)
+		return (rv);
+
+	for (u_int cpu = 0; cpu < vm_get_maxcpus(hyp->vm); cpu++) {
+		struct hypctx *hypctx;
+
+		hypctx = hyp->ctx[cpu];
+		if (hypctx == NULL)
+			continue;
+		MPASS(hypctx->vgic_cpu != NULL);
+		VGIC_CPUINIT(vgic_dev, hypctx);
+	}
+
+	hyp->vgic_ver = hyp->vgic->vgic_ver;
+
+	return (0);
 }
 
 void
@@ -139,25 +168,29 @@ vgic_detach_from_vm(struct hyp *hyp)
 void
 vgic_vminit(struct hyp *hyp)
 {
+	hyp->vgic = malloc(sizeof(*hyp->vgic), M_DEVBUF, M_WAITOK | M_ZERO);
 	VGIC_VMINIT(vgic_dev, hyp);
 }
 
 void
 vgic_cpuinit(struct hypctx *hypctx)
 {
-	VGIC_CPUINIT(vgic_dev, hypctx);
+	hypctx->vgic_cpu = malloc(sizeof(*hypctx->vgic_cpu), M_DEVBUF,
+	    M_WAITOK | M_ZERO);
 }
 
 void
 vgic_cpucleanup(struct hypctx *hypctx)
 {
 	VGIC_CPUCLEANUP(vgic_dev, hypctx);
+	free(hypctx->vgic_cpu, M_DEVBUF);
 }
 
 void
 vgic_vmcleanup(struct hyp *hyp)
 {
 	VGIC_VMCLEANUP(vgic_dev, hyp);
+	free(hyp->vgic, M_DEVBUF);
 }
 
 int
diff --git a/sys/arm64/vmm/io/vgic.h b/sys/arm64/vmm/io/vgic.h
index b9b9f1e39f9c..fdd23dfd6d31 100644
--- a/sys/arm64/vmm/io/vgic.h
+++ b/sys/arm64/vmm/io/vgic.h
@@ -36,6 +36,7 @@ extern device_t vgic_dev;
 
 bool vgic_present(void);
 void vgic_init(void);
+int vgic_get_version(void);
 int vgic_attach_to_vm(struct hyp *hyp, struct vm_vgic_descr *descr);
 void vgic_detach_from_vm(struct hyp *hyp);
 void vgic_vminit(struct hyp *hyp);
diff --git a/sys/arm64/vmm/io/vgic_if.m b/sys/arm64/vmm/io/vgic_if.m
index 571e133cd74b..c2ad1dc0ec72 100644
--- a/sys/arm64/vmm/io/vgic_if.m
+++ b/sys/arm64/vmm/io/vgic_if.m
@@ -102,3 +102,7 @@ METHOD void sync_hwstate {
 	device_t dev;
 	struct hypctx *hypctx;
 }
+
+METHOD int get_version {
+	device_t dev;
+}
diff --git a/sys/arm64/vmm/io/vgic_internal.h b/sys/arm64/vmm/io/vgic_internal.h
index 2f47aef50724..bc56ae741b75 100644
--- a/sys/arm64/vmm/io/vgic_internal.h
+++ b/sys/arm64/vmm/io/vgic_internal.h
@@ -30,6 +30,9 @@
 #ifndef _VGIC_INTERNAL_H_
 #define	_VGIC_INTERNAL_H_
 
+struct hyp;
+struct hypctx;
+
 struct vgic_v3_irq {
 	/* List of IRQs that are active or pending */
 	TAILQ_ENTRY(vgic_v3_irq) act_pend_list;
@@ -80,6 +83,48 @@ struct vgic_v3_cpu {
 	u_int		ich_lr_used;
 };
 
+/* Global data not needed by EL2 */
+struct vgic_v5 {
+	struct mtx 	irs_mtx;
+	uint64_t 	irs_start;
+	size_t   	irs_end;
+
+	bool		irs_en;
+	uint32_t 	irs_cr1;
+
+	uint64_t	irs_ist_baser;
+	uint32_t	irs_ist_cfgr;
+
+	int		irs_pe_selr;
+
+	uint32_t	irs_spi_statusr;
+	uint32_t	irs_spi_selr;
+
+	int		max_cpu_count;
+	int		vm_id;
+};
+
+/* Per-CPU data not needed by EL2 */
+struct vgic_v5_cpu {
+	bool		dps;
+};
+
+struct vgic {
+	u_int	vgic_ver;
+
+	union {
+		struct vgic_v3 v3;
+		struct vgic_v5 v5;
+	};
+};
+
+struct vgic_cpu {
+	union {
+		struct vgic_v3_cpu v3;
+		struct vgic_v5_cpu v5;
+	};
+};
+
 typedef void (register_read)(struct hypctx *, u_int, uint64_t *, void *);
 typedef void (register_write)(struct hypctx *, u_int, u_int, u_int,
     uint64_t, void *);
@@ -88,7 +133,7 @@ register_read vgic_zero_read;
 register_write vgic_ignore_write;
 
 #define	VGIC_8_BIT	(1 << 0)
-/* (1 << 1) is reserved for 16 bit accesses */
+#define	VGIC_16_BIT	(1 << 1)
 #define	VGIC_32_BIT	(1 << 2)
 #define	VGIC_64_BIT	(1 << 3)
 
@@ -129,4 +174,18 @@ bool vgic_register_read(struct hypctx *, struct vgic_register *, u_int, u_int,
 bool vgic_register_write(struct hypctx *, struct vgic_register *, u_int, u_int,
     u_int, uint64_t, void *);
 
+void vgic_v3_vminit(device_t, struct hyp *);
+void vgic_v3_cpuinit(device_t, struct hypctx *);
+void vgic_v3_cpucleanup(device_t, struct hypctx *);
+void vgic_v3_vmcleanup(device_t, struct hyp *);
+int vgic_v3_max_cpu_count(device_t, struct hyp *);
+int vgic_v3_attach_to_vm(device_t, struct hyp *, struct vm_vgic_descr *);
+void vgic_v3_detach_from_vm(device_t, struct hyp *);
+bool vgic_v3_has_pending_irq(device_t, struct hypctx *);
+int vgic_v3_inject_irq(device_t, struct hyp *, int, uint32_t, bool);
+int vgic_v3_inject_msi(device_t dev, struct hyp *, uint64_t, uint64_t);
+void vgic_v3_flush_hwstate(device_t, struct hypctx *);
+void vgic_v3_sync_hwstate(device_t, struct hypctx *);
+void vgic_v3_init(device_t);
+
 #endif /* _VGIC_INTERNAL_H_ */
diff --git a/sys/arm64/vmm/io/vgic_v3.c b/sys/arm64/vmm/io/vgic_v3.c
index 22352beec87b..eede81186370 100644
--- a/sys/arm64/vmm/io/vgic_v3.c
+++ b/sys/arm64/vmm/io/vgic_v3.c
@@ -93,17 +93,11 @@ struct vgic_v3_virt_features {
 	size_t ich_apr_num;
 };
 
-
 /* How many IRQs we support (SGIs + PPIs + SPIs). Not including LPIs */
 #define	VGIC_NIRQS	1023
 /* Pretend to be an Arm design */
 #define	VGIC_IIDR	0x43b
 
-static vgic_inject_irq_t vgic_v3_inject_irq;
-static vgic_inject_msi_t vgic_v3_inject_msi;
-
-static int vgic_v3_max_cpu_count(device_t dev, struct hyp *hyp);
-
 #define	INJECT_IRQ(hyp, vcpuid, irqid, level)			\
     vgic_v3_inject_irq(NULL, (hyp), (vcpuid), (irqid), (level))
 
@@ -346,40 +340,19 @@ mpidr_to_vcpu(struct hyp *hyp, uint64_t mpidr)
 	return (-1);
 }
 
-static void
+void
 vgic_v3_vminit(device_t dev, struct hyp *hyp)
 {
-	struct vgic_v3 *vgic;
-
-	hyp->vgic = malloc(sizeof(*hyp->vgic), M_VGIC_V3,
-	    M_WAITOK | M_ZERO);
-	vgic = hyp->vgic;
-
-	/*
-	 * Configure the Distributor control register. The register resets to an
-	 * architecturally UNKNOWN value, so we reset to 0 to disable all
-	 * functionality controlled by the register.
-	 *
-	 * The exception is GICD_CTLR.DS, which is RA0/WI when the Distributor
-	 * supports one security state (ARM GIC Architecture Specification for
-	 * GICv3 and GICv4, p. 4-464)
-	 */
-	vgic->gicd_ctlr = 0;
-
-	mtx_init(&vgic->dist_mtx, "VGICv3 Distributor lock", NULL,
-	    MTX_SPIN);
 }
 
-static void
+void
 vgic_v3_cpuinit(device_t dev, struct hypctx *hypctx)
 {
 	struct vgic_v3_cpu *vgic_cpu;
 	struct vgic_v3_irq *irq;
 	int i, irqid;
 
-	hypctx->vgic_cpu = malloc(sizeof(*hypctx->vgic_cpu),
-	    M_VGIC_V3, M_WAITOK | M_ZERO);
-	vgic_cpu = hypctx->vgic_cpu;
+	vgic_cpu = &hypctx->vgic_cpu->v3;
 
 	mtx_init(&vgic_cpu->lr_mtx, "VGICv3 ICH_LR_EL2 lock", NULL, MTX_SPIN);
 
@@ -447,39 +420,36 @@ vgic_v3_cpuinit(device_t dev, struct hypctx *hypctx)
 	hypctx->vgic_v3.ich_apr_num = virt_features.ich_apr_num;
 }
 
-static void
+void
 vgic_v3_cpucleanup(device_t dev, struct hypctx *hypctx)
 {
 	struct vgic_v3_cpu *vgic_cpu;
 	struct vgic_v3_irq *irq;
 	int irqid;
 
-	vgic_cpu = hypctx->vgic_cpu;
+	vgic_cpu = &hypctx->vgic_cpu->v3;
 	for (irqid = 0; irqid < VGIC_PRV_I_NUM; irqid++) {
 		irq = &vgic_cpu->private_irqs[irqid];
 		mtx_destroy(&irq->irq_spinmtx);
 	}
 
 	mtx_destroy(&vgic_cpu->lr_mtx);
-	free(hypctx->vgic_cpu->private_irqs, M_VGIC_V3);
-	free(hypctx->vgic_cpu, M_VGIC_V3);
+	free(vgic_cpu->private_irqs, M_VGIC_V3);
 }
 
-static void
+void
 vgic_v3_vmcleanup(device_t dev, struct hyp *hyp)
 {
-	mtx_destroy(&hyp->vgic->dist_mtx);
-	free(hyp->vgic, M_VGIC_V3);
 }
 
-static int
+int
 vgic_v3_max_cpu_count(device_t dev, struct hyp *hyp)
 {
 	struct vgic_v3 *vgic;
 	size_t count;
 	int16_t max_count;
 
-	vgic = hyp->vgic;
+	vgic = &hyp->vgic->v3;
 	max_count = vm_get_maxcpus(hyp->vm);
 
 	/* No registers, assume the maximum CPUs */
@@ -659,7 +629,7 @@ write_pendr(struct hypctx *hypctx, int n, bool set, uint64_t val)
 		target_hypctx = hyp->ctx[target_vcpu];
 		if (target_hypctx == NULL)
 			goto next_irq;
-		vgic_cpu = target_hypctx->vgic_cpu;
+		vgic_cpu = &target_hypctx->vgic_cpu->v3;
 
 		if (!set) {
 			/* pending -> not pending */
@@ -735,7 +705,7 @@ write_activer(struct hypctx *hypctx, u_int n, bool set, uint64_t val)
 		target_hypctx = hyp->ctx[target_vcpu];
 		if (target_hypctx == NULL)
 			goto next_irq;
-		vgic_cpu = target_hypctx->vgic_cpu;
+		vgic_cpu = &target_hypctx->vgic_cpu->v3;
 
 		if (!set) {
 			/* active -> not active */
@@ -895,16 +865,20 @@ dist_ctlr_read(struct hypctx *hypctx, u_int reg, uint64_t *rval,
 {
 	struct hyp *hyp;
 	struct vgic_v3 *vgic;
+	uint64_t val;
 
 	hyp = hypctx->hyp;
-	vgic = hyp->vgic;
+	vgic = &hyp->vgic->v3;
 
-	mtx_lock_spin(&vgic->dist_mtx);
-	*rval = vgic->gicd_ctlr;
-	mtx_unlock_spin(&vgic->dist_mtx);
+	val = atomic_load_32(&vgic->gicd_ctlr);
+
+	/* No GICv2 emulation, so this is RAO */
+	val |= GICD_CTLR_ARE_NS;
 
 	/* Writes are never pending */
-	*rval &= ~GICD_CTLR_RWP;
+	val &= ~GICD_CTLR_RWP;
+
+	*rval = val;
 }
 
 static void
@@ -915,7 +889,7 @@ dist_ctlr_write(struct hypctx *hypctx, u_int reg, u_int offset, u_int size,
 
 	MPASS(offset == 0);
 	MPASS(size == 4);
-	vgic = hypctx->hyp->vgic;
+	vgic = &hypctx->hyp->vgic->v3;
 
 	/*
 	 * GICv2 backwards compatibility is not implemented so
@@ -929,10 +903,8 @@ dist_ctlr_write(struct hypctx *hypctx, u_int reg, u_int offset, u_int size,
 	 */
 	wval &= GICD_CTLR_G1A;
 	wval |= GICD_CTLR_ARE_NS;
-	mtx_lock_spin(&vgic->dist_mtx);
-	vgic->gicd_ctlr = wval;
+	atomic_store_32(&vgic->gicd_ctlr, wval);
 	/* TODO: Wake any vcpus that have interrupts pending */
-	mtx_unlock_spin(&vgic->dist_mtx);
 }
 
 /* GICD_TYPER */
@@ -1216,7 +1188,7 @@ dist_read(struct vcpu *vcpu, uint64_t fault_ipa, uint64_t *rval,
 
 	hypctx = vcpu_get_cookie(vcpu);
 	hyp = hypctx->hyp;
-	vgic = hyp->vgic;
+	vgic = &hyp->vgic->v3;
 
 	/* Check the register is one of ours and is the correct size */
 	if (fault_ipa < vgic->dist_start || fault_ipa + size > vgic->dist_end) {
@@ -1254,7 +1226,7 @@ dist_write(struct vcpu *vcpu, uint64_t fault_ipa, uint64_t wval,
 
 	hypctx = vcpu_get_cookie(vcpu);
 	hyp = hypctx->hyp;
-	vgic = hyp->vgic;
+	vgic = &hyp->vgic->v3;
 
 	/* Check the register is one of ours and is the correct size */
 	if (fault_ipa < vgic->dist_start || fault_ipa + size > vgic->dist_end) {
@@ -1461,7 +1433,7 @@ redist_read(struct vcpu *vcpu, uint64_t fault_ipa, uint64_t *rval,
 	/* Find the current vcpu ctx to get the vgic struct */
 	hypctx = vcpu_get_cookie(vcpu);
 	hyp = hypctx->hyp;
-	vgic = hyp->vgic;
+	vgic = &hyp->vgic->v3;
 
 	/* Check the register is one of ours and is the correct size */
 	if (fault_ipa < vgic->redist_start ||
@@ -1539,7 +1511,7 @@ redist_write(struct vcpu *vcpu, uint64_t fault_ipa, uint64_t wval,
 	/* Find the current vcpu ctx to get the vgic struct */
 	hypctx = vcpu_get_cookie(vcpu);
 	hyp = hypctx->hyp;
-	vgic = hyp->vgic;
+	vgic = &hyp->vgic->v3;
 
 	/* Check the register is one of ours and is the correct size */
 	if (fault_ipa < vgic->redist_start ||
@@ -1675,7 +1647,7 @@ vgic_v3_mmio_init(struct hyp *hyp)
 	int i;
 
 	/* Allocate memory for the SPIs */
-	vgic = hyp->vgic;
+	vgic = &hyp->vgic->v3;
 	vgic->irqs = malloc((VGIC_NIRQS - VGIC_PRV_I_NUM) *
 	    sizeof(*vgic->irqs), M_VGIC_V3, M_WAITOK | M_ZERO);
 
@@ -1696,7 +1668,7 @@ vgic_v3_mmio_destroy(struct hyp *hyp)
 	struct vgic_v3_irq *irq;
 	int i;
 
-	vgic = hyp->vgic;
+	vgic = &hyp->vgic->v3;
 	for (i = 0; i < VGIC_NIRQS - VGIC_PRV_I_NUM; i++) {
 		irq = &vgic->irqs[i];
 
@@ -1706,7 +1678,7 @@ vgic_v3_mmio_destroy(struct hyp *hyp)
 	free(vgic->irqs, M_VGIC_V3);
 }
 
-static int
+int
 vgic_v3_attach_to_vm(device_t dev, struct hyp *hyp, struct vm_vgic_descr *descr)
 {
 	struct vm *vm;
@@ -1741,7 +1713,8 @@ vgic_v3_attach_to_vm(device_t dev, struct hyp *hyp, struct vm_vgic_descr *descr)
 	if (cpu_count > vm_get_maxcpus(vm))
 		return (EINVAL);
 
-	vgic = hyp->vgic;
+	hyp->vgic->vgic_ver = 3;
+	vgic = &hyp->vgic->v3;
 
 	/* Set the distributor address and size for trapping guest access. */
 	vgic->dist_start = descr->v3_regs.dist_start;
@@ -1767,7 +1740,7 @@ vgic_v3_attach_to_vm(device_t dev, struct hyp *hyp, struct vm_vgic_descr *descr)
 	return (0);
 }
 
-static void
+void
 vgic_v3_detach_from_vm(device_t dev, struct hyp *hyp)
 {
 	if (hyp->vgic_attached) {
@@ -1789,13 +1762,13 @@ vgic_v3_get_irq(struct hyp *hyp, int vcpuid, uint32_t irqid)
 		hypctx = hyp->ctx[vcpuid];
 		if (hypctx == NULL)
 			return (NULL);
-		vgic_cpu = hypctx->vgic_cpu;
+		vgic_cpu = &hypctx->vgic_cpu->v3;
 		irq = &vgic_cpu->private_irqs[irqid];
 	} else if (irqid <= GIC_LAST_SPI) {
 		irqid -= VGIC_PRV_I_NUM;
 		if (irqid >= VGIC_NIRQS)
 			return (NULL);
-		irq = &hyp->vgic->irqs[irqid];
+		irq = &hyp->vgic->v3.irqs[irqid];
 	} else if (irqid < GIC_FIRST_LPI) {
 		return (NULL);
 	} else {
@@ -1814,13 +1787,13 @@ vgic_v3_release_irq(struct vgic_v3_irq *irq)
 	mtx_unlock_spin(&irq->irq_spinmtx);
 }
 
-static bool
+bool
 vgic_v3_has_pending_irq(device_t dev, struct hypctx *hypctx)
 {
 	struct vgic_v3_cpu *vgic_cpu;
 	bool empty;
 
-	vgic_cpu = hypctx->vgic_cpu;
+	vgic_cpu = &hypctx->vgic_cpu->v3;
 	mtx_lock_spin(&vgic_cpu->lr_mtx);
 	empty = TAILQ_EMPTY(&vgic_cpu->irq_act_pend);
 	mtx_unlock_spin(&vgic_cpu->lr_mtx);
@@ -1848,7 +1821,7 @@ vgic_v3_check_irq(struct vgic_v3_irq *irq, bool level)
 	return (false);
 }
 
-static int
+int
 vgic_v3_inject_irq(device_t dev, struct hyp *hyp, int vcpuid, uint32_t irqid,
     bool level)
 {
@@ -1894,7 +1867,7 @@ vgic_v3_inject_irq(device_t dev, struct hyp *hyp, int vcpuid, uint32_t irqid,
 	}
 
 	notify = false;
-	vgic_cpu = hypctx->vgic_cpu;
+	vgic_cpu = &hypctx->vgic_cpu->v3;
 
 	mtx_lock_spin(&vgic_cpu->lr_mtx);
 
@@ -1919,13 +1892,13 @@ out:
 	return (0);
 }
 
-static int
+int
 vgic_v3_inject_msi(device_t dev, struct hyp *hyp, uint64_t msg, uint64_t addr)
 {
 	struct vgic_v3 *vgic;
 	uint64_t reg;
 
-	vgic = hyp->vgic;
+	vgic = &hyp->vgic->v3;
 
 	/* This is a 4 byte register */
 	if (addr < vgic->dist_start || addr + 4 > vgic->dist_end) {
@@ -1939,14 +1912,14 @@ vgic_v3_inject_msi(device_t dev, struct hyp *hyp, uint64_t msg, uint64_t addr)
 	return (INJECT_IRQ(hyp, -1, msg, true));
 }
 
-static void
+void
 vgic_v3_flush_hwstate(device_t dev, struct hypctx *hypctx)
 {
 	struct vgic_v3_cpu *vgic_cpu;
 	struct vgic_v3_irq *irq;
 	int i;
 
-	vgic_cpu = hypctx->vgic_cpu;
+	vgic_cpu = &hypctx->vgic_cpu->v3;
 
 	/*
 	 * All Distributor writes have been executed at this point, do not
@@ -2021,7 +1994,7 @@ out:
 	mtx_unlock_spin(&vgic_cpu->lr_mtx);
 }
 
-static void
+void
 vgic_v3_sync_hwstate(device_t dev, struct hypctx *hypctx)
 {
 	struct vgic_v3_cpu *vgic_cpu;
@@ -2029,7 +2002,7 @@ vgic_v3_sync_hwstate(device_t dev, struct hypctx *hypctx)
 	uint64_t lr;
 	int i;
 
-	vgic_cpu = hypctx->vgic_cpu;
+	vgic_cpu = &hypctx->vgic_cpu->v3;
 
 	/* Exit early if there are no buffered interrupts */
 	if (vgic_cpu->ich_lr_used == 0)
@@ -2093,7 +2066,7 @@ vgic_v3_sync_hwstate(device_t dev, struct hypctx *hypctx)
 	vgic_cpu->ich_lr_used = 0;
 }
 
-static void
+void
 vgic_v3_init(device_t dev)
 {
 	uint64_t ich_vtr_el2;
@@ -2146,11 +2119,14 @@ struct vgic_v3_softc {
 static int
 vgic_v3_probe(device_t dev)
 {
+	u_int rev;
+
 	if (!gic_get_vgic(dev))
 		return (EINVAL);
 
-	/* We currently only support the GICv3 */
-	if (gic_get_hw_rev(dev) < 3)
+	/* We currently only support the GICv3/v4 */
+	rev = gic_get_hw_rev(dev);
+	if (rev < 3 || rev > 4)
 		return (EINVAL);
 
 	device_set_desc(dev, "Virtual GIC v3");
@@ -2255,6 +2231,12 @@ vgic_v3_detach(device_t dev)
 	return (0);
 }
 
+static int
+vgic_v3_get_version(device_t dev)
+{
+        return (3);
+}
+
 static device_method_t vgic_v3_methods[] = {
 	/* Device interface */
 	DEVMETHOD(device_probe,		vgic_v3_probe),
@@ -2275,6 +2257,7 @@ static device_method_t vgic_v3_methods[] = {
 	DEVMETHOD(vgic_inject_msi,	vgic_v3_inject_msi),
 	DEVMETHOD(vgic_flush_hwstate,	vgic_v3_flush_hwstate),
 	DEVMETHOD(vgic_sync_hwstate,	vgic_v3_sync_hwstate),
+	DEVMETHOD(vgic_get_version,	vgic_v3_get_version),
 
 	/* End */
 	DEVMETHOD_END
diff --git a/sys/arm64/vmm/io/vgic_v5.c b/sys/arm64/vmm/io/vgic_v5.c
new file mode 100644
index 000000000000..ec5e918c93ef
--- /dev/null
+++ b/sys/arm64/vmm/io/vgic_v5.c
@@ -0,0 +1,906 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (C) 2018 Alexandru Elisei <alexandru.elisei@gmail.com>
+ * Copyright (C) 2020-2022 Andrew Turner
+ * Copyright (C) 2023 Arm Ltd
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <sys/errno.h>
+#include <sys/systm.h>
+#include <sys/bitstring.h>
+#include <sys/bus.h>
+#include <sys/kernel.h>
+#include <sys/lock.h>
+#include <sys/malloc.h>
+#include <sys/module.h>
+#include <sys/mutex.h>
+#include <sys/rman.h>
+#include <sys/smp.h>
+#include <sys/intr.h>
+
+#include <vm/vm.h>
+#include <vm/pmap.h>
+
+#include <dev/ofw/openfirm.h>
+
+#include <machine/atomic.h>
+#include <machine/bus.h>
+#include <machine/cpufunc.h>
+#include <machine/cpu.h>
+#include <machine/machdep.h>
+#include <machine/param.h>
+#include <machine/pmap.h>
+#include <machine/vmparam.h>
+#include <machine/intr.h>
+#include <machine/vmm.h>
+#include <machine/vmm_instruction_emul.h>
+
+#include <arm/arm/gic_common.h>
+
+#include <arm64/arm64/gicv5reg.h>
+#include <arm64/arm64/gicv5var.h>
+
+#include <arm64/vmm/hyp.h>
+#include <arm64/vmm/mmu.h>
+#include <arm64/vmm/arm64.h>
+#include <arm64/vmm/vmm_handlers.h>
+
+#include <dev/vmm/vmm_dev.h>
+#include <dev/vmm/vmm_vm.h>
+
+#include "vgic.h"
+#include "vgic_internal.h"
+
+#include "vgic_if.h"
+
+MALLOC_DEFINE(M_VGIC_V5, "ARM VMM VGIC V5", "ARM VMM VGIC V5");
+
+#define	SPI_COUNT	4096
+
+#define VGIC_IAFFID_BITS	16
+#define VGIC_INTID_BITS		12
+#define VGIC_PRI_BITS		5
+#define VGIC_MIN_LPI_BITS	8
+
+static int vgic_v5_max_cpu_count(device_t dev, struct hyp *hyp);
+
+static bool
+has_gcie_legacy(void)
+{
+	uint64_t idr0;
+
+	get_kernel_reg(ICC_IDR0_EL1, &idr0);
+
+	return ((idr0 & ICC_IDR0_GCIE_LEGACY_MASK) ? true : false);
+}
+
+static void
+vgic_v5_vminit(device_t dev, struct hyp *hyp)
+{
+	struct vgic_v5 *vgic;
+
+	vgic = &hyp->vgic->v5;
+	vgic->max_cpu_count = vgic_v5_max_cpu_count(dev, hyp);
+}
+
+static void
+vgic_v5_cpuinit(device_t dev, struct hypctx *hypctx)
+{
+	if (hypctx->hyp->vgic->vgic_ver == 3) {
+		vgic_v3_cpuinit(dev, hypctx);
+		return;
+	}
+
+	hypctx_write_sys_reg(hypctx, HOST_ICH_HFGITR_EL2,
+	    ICH_HFGITR_EL2_GICCDPEND |
+	    ICH_HFGITR_EL2_GICCDEN |
+	    ICH_HFGITR_EL2_GICCDAFF |
+	    ICH_HFGITR_EL2_GICCDPRI |
*** 1412 LINES SKIPPED ***