* [RFC PATCH v3 01/10] iommu/riscv: Use DC.tc.V as the device context valid bit
2026-11-19 13:27 [RFC PATCH v3 00/10] iommu/riscv: Add nested translation (vIOMMU) support Gong Shuai
@ 2026-11-19 13:27 ` Gong Shuai
2026-11-19 13:27 ` [RFC PATCH v3 02/10] iommufd: Add RISC-V IOMMU hwpt and vIOMMU types Gong Shuai
` (8 subsequent siblings)
9 siblings, 0 replies; 13+ messages in thread
From: Gong Shuai @ 2026-11-19 13:27 UTC (permalink / raw)
To: kvm, kvm-riscv, linux-riscv, linux-kernel, iommu
Cc: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt,
Albert Ou, Alexandre Ghiti, Tomasz Jeznach, Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Paolo Bonzini, Sean Christopherson,
Jason Gunthorpe, Kevin Tian, Andrew Jones, Fangyu Yu, Tan Chi,
Wu Fei, Zhao Qingye, Gong Shuai
Previously, riscv_iommu_iodir_update() accepted only a partial device
context and used bit 0 of the translation attributes as the valid
flag. It also kept only GADE in DC.tc and dropped the other fields
needed to pass guest translation settings for nested domains.
Now that the function accepts a complete device context, take the
valid bit from DC.tc.V and copy all defined TC fields so the guest
configuration reaches the hardware.
Use the DC field macros for the remaining device context updates as
well.
Signed-off-by: Gong Shuai <gong.shuai@sanechips.com.cn>
---
drivers/iommu/riscv/iommu-bits.h | 6 ++++++
drivers/iommu/riscv/iommu.c | 28 ++++++++++++----------------
2 files changed, 18 insertions(+), 16 deletions(-)
diff --git a/drivers/iommu/riscv/iommu-bits.h b/drivers/iommu/riscv/iommu-bits.h
index 65d1f2250011..80c32f00ee5e 100644
--- a/drivers/iommu/riscv/iommu-bits.h
+++ b/drivers/iommu/riscv/iommu-bits.h
@@ -348,6 +348,12 @@ struct riscv_iommu_dc {
#define RISCV_IOMMU_DC_TC_SBE BIT_ULL(10)
#define RISCV_IOMMU_DC_TC_SXL BIT_ULL(11)
+/*
+ * All bits of dc.tc as defined by the spec. Bits 12-23 and 32-63
+ * are reserved for future standard use, bits 24-31 are for custom use.
+ */
+#define RISCV_IOMMU_DC_TC_DEFINED GENMASK_ULL(11, 0)
+
/* Second-stage (aka G-stage) context fields */
#define RISCV_IOMMU_DC_IOHGATP_PPN RISCV_IOMMU_ATP_PPN_FIELD
#define RISCV_IOMMU_DC_IOHGATP_GSCID GENMASK_ULL(59, 44)
diff --git a/drivers/iommu/riscv/iommu.c b/drivers/iommu/riscv/iommu.c
index 8c15710d60b6..f6e92b086c12 100644
--- a/drivers/iommu/riscv/iommu.c
+++ b/drivers/iommu/riscv/iommu.c
@@ -1132,7 +1132,6 @@ static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain,
rcu_read_unlock();
}
-#define RISCV_IOMMU_FSC_BARE 0
/*
* This function sends IOTINVAL commands as required by the RISC-V
* IOMMU specification (Section 6.3.1 and 6.3.2 in 1.0 spec version)
@@ -1243,18 +1242,14 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,
riscv_iommu_cmd_sync(iommu, RISCV_IOMMU_IOTINVAL_TIMEOUT);
/*
- * For device context with DC_TC_PDTV = 0, translation attributes valid bit
- * is stored as DC_TC_V bit (both sharing the same location at BIT(0)).
+ * Only defined bits are writable by the driver.
*/
for (i = 0; i < fwspec->num_ids; i++) {
dc = riscv_iommu_get_dc(iommu, fwspec->ids[i]);
- tc = READ_ONCE(dc->tc);
- tc &= ~RISCV_IOMMU_DC_TC_GADE;
- tc |= new_dc->tc & RISCV_IOMMU_DC_TC_GADE;
- tc |= new_dc->ta & RISCV_IOMMU_DC_TC_V;
+ tc = new_dc->tc & RISCV_IOMMU_DC_TC_DEFINED;
WRITE_ONCE(dc->fsc, new_dc->fsc);
- WRITE_ONCE(dc->ta, new_dc->ta & RISCV_IOMMU_PC_TA_PSCID);
+ WRITE_ONCE(dc->ta, new_dc->ta & RISCV_IOMMU_DC_TA_PSCID);
WRITE_ONCE(dc->iohgatp, new_dc->iohgatp);
/* Update device context, write TC.V as the last step. */
dma_wmb();
@@ -1377,11 +1372,11 @@ static int riscv_iommu_attach_paging_domain(struct iommu_domain *iommu_domain,
if (!riscv_iommu_fsc_supported(iommu, pt_info.fsc_iosatp_mode))
return -ENODEV;
- dc.fsc = FIELD_PREP(RISCV_IOMMU_PC_FSC_MODE, pt_info.fsc_iosatp_mode) |
- FIELD_PREP(RISCV_IOMMU_PC_FSC_PPN, pt_info.ppn);
+ dc.fsc = FIELD_PREP(RISCV_IOMMU_DC_FSC_MODE, pt_info.fsc_iosatp_mode) |
+ FIELD_PREP(RISCV_IOMMU_DC_FSC_PPN, pt_info.ppn);
}
- dc.ta = FIELD_PREP(RISCV_IOMMU_PC_TA_PSCID, domain->pscid) |
- RISCV_IOMMU_PC_TA_V;
+ dc.ta = FIELD_PREP(RISCV_IOMMU_DC_TA_PSCID, domain->pscid);
+ dc.tc |= RISCV_IOMMU_DC_TC_V;
if (riscv_iommu_bond_link(domain, dev))
return -ENOMEM;
@@ -1544,9 +1539,10 @@ static int riscv_iommu_attach_blocking_domain(struct iommu_domain *iommu_domain,
struct riscv_iommu_info *info = dev_iommu_priv_get(dev);
struct riscv_iommu_dc dc = {0};
- dc.fsc = RISCV_IOMMU_FSC_BARE;
+ dc.fsc = RISCV_IOMMU_DC_FSC_MODE_BARE;
+ dc.tc = 0;
- /* Make device context invalid, translation requests will fault w/ #258 */
+ /* Make device context invalid (DC.tc.V = 0), translation requests will fault w/ #258 */
riscv_iommu_iodir_update(iommu, dev, &dc);
riscv_iommu_bond_unlink(info->domain, dev);
info->domain = NULL;
@@ -1569,8 +1565,8 @@ static int riscv_iommu_attach_identity_domain(struct iommu_domain *iommu_domain,
struct riscv_iommu_info *info = dev_iommu_priv_get(dev);
struct riscv_iommu_dc dc = {0};
- dc.fsc = RISCV_IOMMU_FSC_BARE;
- dc.ta = RISCV_IOMMU_PC_TA_V;
+ dc.fsc = RISCV_IOMMU_DC_FSC_MODE_BARE;
+ dc.tc = RISCV_IOMMU_DC_TC_V;
riscv_iommu_iodir_update(iommu, dev, &dc);
riscv_iommu_bond_unlink(info->domain, dev);
--
2.53.0
^ permalink raw reply [flat|nested] 13+ messages in thread* [RFC PATCH v3 02/10] iommufd: Add RISC-V IOMMU hwpt and vIOMMU types
2026-11-19 13:27 [RFC PATCH v3 00/10] iommu/riscv: Add nested translation (vIOMMU) support Gong Shuai
2026-11-19 13:27 ` [RFC PATCH v3 01/10] iommu/riscv: Use DC.tc.V as the device context valid bit Gong Shuai
@ 2026-11-19 13:27 ` Gong Shuai
2026-11-19 13:27 ` [RFC PATCH v3 03/10] iommufd: Add RISC-V IOMMU cache invalidation data type Gong Shuai
` (7 subsequent siblings)
9 siblings, 0 replies; 13+ messages in thread
From: Gong Shuai @ 2026-11-19 13:27 UTC (permalink / raw)
To: kvm, kvm-riscv, linux-riscv, linux-kernel, iommu
Cc: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt,
Albert Ou, Alexandre Ghiti, Tomasz Jeznach, Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Paolo Bonzini, Sean Christopherson,
Jason Gunthorpe, Kevin Tian, Andrew Jones, Fangyu Yu, Tan Chi,
Wu Fei, Zhao Qingye, Gong Shuai
From: Wu Fei <wu.fei9@sanechips.com.cn>
Add IOMMU_HWPT_DATA_RISCV_S1 and struct iommu_hwpt_riscv to carry
the guest device context in base format for nested hwpt allocation.
Add IOMMU_VIOMMU_TYPE_RISCV so user space can request a RISC-V
vIOMMU.
The RISC-V nested translation support added later needs these types.
Signed-off-by: Wu Fei <wu.fei9@sanechips.com.cn>
Co-developed-by: Gong Shuai <gong.shuai@sanechips.com.cn>
Signed-off-by: Gong Shuai <gong.shuai@sanechips.com.cn>
---
include/uapi/linux/iommufd.h | 32 ++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h
index 010f47f7d3d2..809f4d44c0b0 100644
--- a/include/uapi/linux/iommufd.h
+++ b/include/uapi/linux/iommufd.h
@@ -506,18 +506,48 @@ struct iommu_hwpt_amd_guest {
__aligned_u64 dte[4];
};
+/**
+ * struct iommu_hwpt_riscv - RISC-V IOMMU stage-1 page table data
+ * (IOMMU_HWPT_DATA_RISCV_S1)
+ *
+ * @dc: The guest device-context (DC) in the base format defined in
+ * "Leaf DDT entry" of the RISC-V IOMMU Architecture Specification,
+ * refer to "Device-context fields" for the meaning of each
+ * doubleword. Only the guest-owned doublewords are validated:
+ * - dc[0] (tc): Allowed bits are V, EN_ATS, EN_PRI, T2GPA, DTF,
+ * PRPR, SADE and SBE. GADE is owned by the host and the value
+ * here is ignored. PDTV, DPE and SXL are not supported and must
+ * be 0.
+ * - dc[2] (ta): PSCID is guest-owned. Within one vIOMMU the guest
+ * must use a unique PSCID for each stage-1 address space. All
+ * other fields are ignored and managed by the host.
+ * - dc[3] (fsc): Carries iosatp when tc.PDTV=0, allowed fields
+ * are MODE (Bare, Sv39, Sv48, Sv57) and PPN; Carries pdtp when
+ * tc.PDTV=1, which not supported now.
+ *
+ * The host-owned doubleword dc[1] (iohgatp) is ignored and installed
+ * by the kernel from the nesting parent at attach time. The
+ * extended-format MSI doublewords are configured by the host for
+ * interrupt remapping and are not passed via this structure.
+ */
+struct iommu_hwpt_riscv {
+ __aligned_u64 dc[4];
+};
+
/**
* enum iommu_hwpt_data_type - IOMMU HWPT Data Type
* @IOMMU_HWPT_DATA_NONE: no data
* @IOMMU_HWPT_DATA_VTD_S1: Intel VT-d stage-1 page table
* @IOMMU_HWPT_DATA_ARM_SMMUV3: ARM SMMUv3 Context Descriptor Table
* @IOMMU_HWPT_DATA_AMD_GUEST: AMD IOMMU guest page table
+ * @IOMMU_HWPT_DATA_RISCV_S1: RISC-V IOMMU stage-1 page table
*/
enum iommu_hwpt_data_type {
IOMMU_HWPT_DATA_NONE = 0,
IOMMU_HWPT_DATA_VTD_S1 = 1,
IOMMU_HWPT_DATA_ARM_SMMUV3 = 2,
IOMMU_HWPT_DATA_AMD_GUEST = 3,
+ IOMMU_HWPT_DATA_RISCV_S1 = 4,
};
/**
@@ -1103,6 +1133,7 @@ struct iommu_fault_alloc {
* @IOMMU_VIOMMU_TYPE_ARM_SMMUV3: ARM SMMUv3 driver specific type
* @IOMMU_VIOMMU_TYPE_TEGRA241_CMDQV: NVIDIA Tegra241 CMDQV (extension for ARM
* SMMUv3) enabled ARM SMMUv3 type
+ * @IOMMU_VIOMMU_TYPE_RISCV: RISC-V IOMMU driver specific type
*/
enum iommu_viommu_type {
IOMMU_VIOMMU_TYPE_DEFAULT = 0,
@@ -1113,6 +1144,7 @@ enum iommu_viommu_type {
* VMM must wire the HYP_OWN bit to 0 in guest VINTF_CONFIG register
*/
IOMMU_VIOMMU_TYPE_TEGRA241_CMDQV = 2,
+ IOMMU_VIOMMU_TYPE_RISCV = 3,
};
/**
--
2.53.0
^ permalink raw reply [flat|nested] 13+ messages in thread* [RFC PATCH v3 03/10] iommufd: Add RISC-V IOMMU cache invalidation data type
2026-11-19 13:27 [RFC PATCH v3 00/10] iommu/riscv: Add nested translation (vIOMMU) support Gong Shuai
2026-11-19 13:27 ` [RFC PATCH v3 01/10] iommu/riscv: Use DC.tc.V as the device context valid bit Gong Shuai
2026-11-19 13:27 ` [RFC PATCH v3 02/10] iommufd: Add RISC-V IOMMU hwpt and vIOMMU types Gong Shuai
@ 2026-11-19 13:27 ` Gong Shuai
2026-11-19 13:27 ` [RFC PATCH v3 04/10] iommu/riscv: Add vIOMMU object support Gong Shuai
` (6 subsequent siblings)
9 siblings, 0 replies; 13+ messages in thread
From: Gong Shuai @ 2026-11-19 13:27 UTC (permalink / raw)
To: kvm, kvm-riscv, linux-riscv, linux-kernel, iommu
Cc: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt,
Albert Ou, Alexandre Ghiti, Tomasz Jeznach, Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Paolo Bonzini, Sean Christopherson,
Jason Gunthorpe, Kevin Tian, Andrew Jones, Fangyu Yu, Tan Chi,
Wu Fei, Zhao Qingye, Gong Shuai
From: Wu Fei <wu.fei9@sanechips.com.cn>
Add IOMMU_VIOMMU_INVALIDATE_DATA_RISCV and
struct iommu_viommu_riscv_invalidate to carry RISC-V IOTINVAL
commands from user space. Define the command payload in the uAPI
instead of relying on the driver internal struct
riscv_iommu_command.
The vIOMMU cache invalidation support added later uses this format
to forward guest IOTINVAL commands to the physical IOMMU.
Signed-off-by: Wu Fei <wu.fei9@sanechips.com.cn>
Co-developed-by: Gong Shuai <gong.shuai@sanechips.com.cn>
Signed-off-by: Gong Shuai <gong.shuai@sanechips.com.cn>
---
include/uapi/linux/iommufd.h | 28 ++++++++++++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h
index 809f4d44c0b0..fee63e6cbb7a 100644
--- a/include/uapi/linux/iommufd.h
+++ b/include/uapi/linux/iommufd.h
@@ -926,10 +926,12 @@ struct iommu_hwpt_get_dirty_bitmap {
* Data Type
* @IOMMU_HWPT_INVALIDATE_DATA_VTD_S1: Invalidation data for VTD_S1
* @IOMMU_VIOMMU_INVALIDATE_DATA_ARM_SMMUV3: Invalidation data for ARM SMMUv3
+ * @IOMMU_VIOMMU_INVALIDATE_DATA_RISCV: Invalidation data for RISC-V IOMMU
*/
enum iommu_hwpt_invalidate_data_type {
IOMMU_HWPT_INVALIDATE_DATA_VTD_S1 = 0,
IOMMU_VIOMMU_INVALIDATE_DATA_ARM_SMMUV3 = 1,
+ IOMMU_VIOMMU_INVALIDATE_DATA_RISCV = 2,
};
/**
@@ -992,6 +994,32 @@ struct iommu_viommu_arm_smmuv3_invalidate {
__aligned_le64 cmd[2];
};
+/**
+ * struct iommu_viommu_riscv_invalidate - RISC-V IOMMU cache invalidation
+ * (IOMMU_VIOMMU_INVALIDATE_DATA_RISCV)
+ * @cmd: 128-bit cache invalidation command that runs in the IOMMU command
+ * queue, in the format defined by the "IOMMU Page-table cache
+ * invalidation" section of the RISC-V IOMMU specification. Must be
+ * little-endian.
+ *
+ * Supported command list only when passing in a vIOMMU via @hwpt_id:
+ * IOTINVAL.VMA
+ *
+ * The kernel overwrites GV and GSCID before issuing the command:
+ * GV = 1 and GSCID = the GSCID assigned to the vIOMMU
+ * PSCV and PSCID are taken unchanged from @cmd; When PSCV=1, PSCID
+ * must identify a stage-1 address space of this vIOMMU and match the
+ * PSCID installed in its vDC. When PSCV=0, PSCID is ignored.
+ *
+ * All other reserved bits must be 0.
+ *
+ * -EIO will be returned if the command is not supported, -EINVAL if it is
+ * malformed.
+ */
+struct iommu_viommu_riscv_invalidate {
+ __aligned_le64 cmd[2];
+};
+
/**
* struct iommu_hwpt_invalidate - ioctl(IOMMU_HWPT_INVALIDATE)
* @size: sizeof(struct iommu_hwpt_invalidate)
--
2.53.0
^ permalink raw reply [flat|nested] 13+ messages in thread* [RFC PATCH v3 04/10] iommu/riscv: Add vIOMMU object support
2026-11-19 13:27 [RFC PATCH v3 00/10] iommu/riscv: Add nested translation (vIOMMU) support Gong Shuai
` (2 preceding siblings ...)
2026-11-19 13:27 ` [RFC PATCH v3 03/10] iommufd: Add RISC-V IOMMU cache invalidation data type Gong Shuai
@ 2026-11-19 13:27 ` Gong Shuai
2026-11-19 13:27 ` [RFC PATCH v3 05/10] iommu/riscv: Add nested domain allocation Gong Shuai
` (5 subsequent siblings)
9 siblings, 0 replies; 13+ messages in thread
From: Gong Shuai @ 2026-11-19 13:27 UTC (permalink / raw)
To: kvm, kvm-riscv, linux-riscv, linux-kernel, iommu
Cc: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt,
Albert Ou, Alexandre Ghiti, Tomasz Jeznach, Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Paolo Bonzini, Sean Christopherson,
Jason Gunthorpe, Kevin Tian, Andrew Jones, Fangyu Yu, Tan Chi,
Wu Fei, Zhao Qingye, Gong Shuai
From: Wu Fei <wu.fei9@sanechips.com.cn>
Allow iommufd to allocate a RISC-V vIOMMU when the physical IOMMU
supports both translation stages.
Nested translation needs to associate guest-owned stage 1 contexts
with a host-owned stage 2 parent domain. A vIOMMU provides this
association and owns a dedicated GSCID so that guest PSCIDs cannot
alias address spaces belonging to another vIOMMU.
Signed-off-by: Wu Fei <wu.fei9@sanechips.com.cn>
Co-developed-by: Gong Shuai <gong.shuai@sanechips.com.cn>
Signed-off-by: Gong Shuai <gong.shuai@sanechips.com.cn>
---
drivers/iommu/riscv/iommu.c | 77 +++++++++++++++++++++++++++++++++++++
1 file changed, 77 insertions(+)
diff --git a/drivers/iommu/riscv/iommu.c b/drivers/iommu/riscv/iommu.c
index f6e92b086c12..f2305b953f71 100644
--- a/drivers/iommu/riscv/iommu.c
+++ b/drivers/iommu/riscv/iommu.c
@@ -19,6 +19,7 @@
#include <linux/dma-map-ops.h>
#include <linux/init.h>
#include <linux/iommu.h>
+#include <linux/iommufd.h>
#include <linux/iopoll.h>
#include <linux/kernel.h>
#include <linux/pci.h>
@@ -856,6 +857,7 @@ struct riscv_iommu_domain {
spinlock_t lock; /* protect bonds list updates. */
int pscid;
int gscid;
+ bool nest_parent;
};
PT_IOMMU_CHECK_DOMAIN(struct riscv_iommu_domain, riscvpt.iommu, domain);
@@ -1342,6 +1344,20 @@ static bool riscv_iommu_iohgatp_supported(struct riscv_iommu_device *iommu,
return false;
}
+static bool riscv_iommu_supports_s1(struct riscv_iommu_device *iommu)
+{
+ return (iommu->caps & RISCV_IOMMU_CAPABILITIES_SV57) ||
+ (iommu->caps & RISCV_IOMMU_CAPABILITIES_SV48) ||
+ (iommu->caps & RISCV_IOMMU_CAPABILITIES_SV39);
+}
+
+static bool riscv_iommu_supports_s2(struct riscv_iommu_device *iommu)
+{
+ return (iommu->caps & RISCV_IOMMU_CAPABILITIES_SV57X4) ||
+ (iommu->caps & RISCV_IOMMU_CAPABILITIES_SV48X4) ||
+ (iommu->caps & RISCV_IOMMU_CAPABILITIES_SV39X4);
+}
+
static int riscv_iommu_attach_paging_domain(struct iommu_domain *iommu_domain,
struct device *dev,
struct iommu_domain *old)
@@ -1466,6 +1482,8 @@ static int riscv_iommu_init_domain_s2(struct device *dev,
if (domain->gscid < 0)
return -ENOMEM;
+ domain->nest_parent = !!(flags & IOMMU_HWPT_ALLOC_NEST_PARENT);
+
/* AMO_HWAD is checked by iommufd via IOMMU_CAP_DIRTY_TRACKING */
if (flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING)
domain->domain.dirty_ops = &riscv_iommu_dirty_ops;
@@ -1662,6 +1680,63 @@ static void riscv_iommu_release_device(struct device *dev)
kfree_rcu_mightsleep(info);
}
+struct riscv_viommu {
+ struct iommufd_viommu core;
+ struct riscv_iommu_device *iommu;
+ struct riscv_iommu_domain *s2_parent;
+ int gscid;
+};
+
+static void riscv_viommu_destroy(struct iommufd_viommu *viommu)
+{
+ struct riscv_viommu *rv_viommu =
+ container_of(viommu, struct riscv_viommu, core);
+ /*
+ * All devices are detached and iodir_update() has flushed this
+ * GSCID, so the ID can be recycled here.
+ */
+ ida_free(&riscv_iommu_gscids, rv_viommu->gscid);
+}
+
+static const struct iommufd_viommu_ops riscv_viommu_ops = {
+ .destroy = riscv_viommu_destroy,
+};
+
+static size_t riscv_iommu_get_viommu_size(struct device *dev,
+ enum iommu_viommu_type viommu_type)
+{
+ struct riscv_iommu_device *iommu = dev_to_iommu(dev);
+
+ if (viommu_type != IOMMU_VIOMMU_TYPE_RISCV)
+ return 0;
+
+ if (!riscv_iommu_supports_s1(iommu) || !riscv_iommu_supports_s2(iommu))
+ return 0;
+
+ return VIOMMU_STRUCT_SIZE(struct riscv_viommu, core);
+}
+
+static int riscv_viommu_init(struct iommufd_viommu *viommu,
+ struct iommu_domain *parent_domain,
+ const struct iommu_user_data *user_data)
+{
+ struct riscv_viommu *rv_viommu = container_of(viommu, struct riscv_viommu, core);
+ struct riscv_iommu_device *iommu =
+ container_of(viommu->iommu_dev, struct riscv_iommu_device, iommu);
+ struct riscv_iommu_domain *s2_parent = iommu_domain_to_riscv(parent_domain);
+
+ rv_viommu->gscid = ida_alloc_range(&riscv_iommu_gscids, 1,
+ RISCV_IOMMU_MAX_GSCID, GFP_KERNEL);
+ if (rv_viommu->gscid < 0)
+ return rv_viommu->gscid;
+
+ rv_viommu->iommu = iommu;
+ rv_viommu->s2_parent = s2_parent;
+ viommu->ops = &riscv_viommu_ops;
+
+ return 0;
+}
+
static const struct iommu_ops riscv_iommu_ops = {
.of_xlate = riscv_iommu_of_xlate,
.capable = riscv_iommu_capable,
@@ -1669,6 +1744,8 @@ static const struct iommu_ops riscv_iommu_ops = {
.blocked_domain = &riscv_iommu_blocking_domain,
.release_domain = &riscv_iommu_blocking_domain,
.domain_alloc_paging_flags = riscv_iommu_domain_alloc_paging_flags,
+ .get_viommu_size = riscv_iommu_get_viommu_size,
+ .viommu_init = riscv_viommu_init,
.device_group = riscv_iommu_device_group,
.probe_device = riscv_iommu_probe_device,
.release_device = riscv_iommu_release_device,
--
2.53.0
^ permalink raw reply [flat|nested] 13+ messages in thread* [RFC PATCH v3 05/10] iommu/riscv: Add nested domain allocation
2026-11-19 13:27 [RFC PATCH v3 00/10] iommu/riscv: Add nested translation (vIOMMU) support Gong Shuai
` (3 preceding siblings ...)
2026-11-19 13:27 ` [RFC PATCH v3 04/10] iommu/riscv: Add vIOMMU object support Gong Shuai
@ 2026-11-19 13:27 ` Gong Shuai
2026-11-21 12:37 ` Andrew Jones
2026-11-19 13:27 ` [RFC PATCH v3 06/10] iommu/riscv: Add vIOMMU cache invalidation Gong Shuai
` (4 subsequent siblings)
9 siblings, 1 reply; 13+ messages in thread
From: Gong Shuai @ 2026-11-19 13:27 UTC (permalink / raw)
To: kvm, kvm-riscv, linux-riscv, linux-kernel, iommu
Cc: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt,
Albert Ou, Alexandre Ghiti, Tomasz Jeznach, Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Paolo Bonzini, Sean Christopherson,
Jason Gunthorpe, Kevin Tian, Andrew Jones, Fangyu Yu, Tan Chi,
Wu Fei, Zhao Qingye, Gong Shuai
From: Wu Fei <wu.fei9@sanechips.com.cn>
A nested domain describes two-stage translation through guest and
host page tables. Iommufd exposes this domain as a nested hwpt.
User space needs to provide the guest stage 1 configuration when
allocating a nested hwpt. Validate the configuration before storing
it for use in a physical device context.
Signed-off-by: Wu Fei <wu.fei9@sanechips.com.cn>
Co-developed-by: Gong Shuai <gong.shuai@sanechips.com.cn>
Signed-off-by: Gong Shuai <gong.shuai@sanechips.com.cn>
---
drivers/iommu/riscv/iommu.c | 153 ++++++++++++++++++++++++++++++++++++
1 file changed, 153 insertions(+)
diff --git a/drivers/iommu/riscv/iommu.c b/drivers/iommu/riscv/iommu.c
index f2305b953f71..4f8ce579ef08 100644
--- a/drivers/iommu/riscv/iommu.c
+++ b/drivers/iommu/riscv/iommu.c
@@ -1698,8 +1698,161 @@ static void riscv_viommu_destroy(struct iommufd_viommu *viommu)
ida_free(&riscv_iommu_gscids, rv_viommu->gscid);
}
+struct riscv_iommu_nested_domain {
+ struct iommu_domain domain;
+ struct riscv_viommu *viommu;
+
+ u64 tc;
+ u64 ta;
+ u64 iosatp;
+};
+
+#define to_riscv_iommu_nested_domain(domain) \
+ container_of(domain, struct riscv_iommu_nested_domain, domain)
+
+static void riscv_iommu_domain_nested_free(struct iommu_domain *domain)
+{
+ kfree(to_riscv_iommu_nested_domain(domain));
+}
+
+static const struct iommu_domain_ops riscv_iommu_nested_ops = {
+ .free = riscv_iommu_domain_nested_free,
+};
+
+static int riscv_iommu_validate_vdc(struct riscv_iommu_device *iommu,
+ struct iommu_hwpt_riscv *arg)
+{
+ u64 tc = arg->dc[0];
+ u64 fsc = arg->dc[3];
+ u64 mode;
+
+ if (!(tc & RISCV_IOMMU_DC_TC_V)) {
+ arg->dc[0] = 0;
+ arg->dc[3] = 0;
+ return 0;
+ }
+
+ /*
+ * Reject reserved/custom bits, the physical IOMMU may treat the
+ * DC as misconfigured.
+ */
+ if (tc & ~RISCV_IOMMU_DC_TC_DEFINED)
+ return -EINVAL;
+ if (fsc & ~(RISCV_IOMMU_DC_FSC_PPN | RISCV_IOMMU_DC_FSC_MODE))
+ return -EINVAL;
+
+ /* Process directory tables are not supported now. */
+ if (tc & RISCV_IOMMU_DC_TC_PDTV)
+ return -EOPNOTSUPP;
+
+ /* With PDTV=0 the DPE bit is reserved for future use. */
+ if (tc & RISCV_IOMMU_DC_TC_DPE)
+ return -EINVAL;
+
+ /* Only 64-bit (SXL=0) stage-1 is supported. */
+ if (tc & RISCV_IOMMU_DC_TC_SXL)
+ return -EOPNOTSUPP;
+
+ /* ATS/PRI/T2GPA/PRPR consistency. */
+ if (!(iommu->caps & RISCV_IOMMU_CAPABILITIES_ATS) &&
+ (tc & (RISCV_IOMMU_DC_TC_EN_ATS | RISCV_IOMMU_DC_TC_EN_PRI |
+ RISCV_IOMMU_DC_TC_PRPR)))
+ return -EINVAL;
+ if ((tc & RISCV_IOMMU_DC_TC_EN_PRI) &&
+ !(tc & RISCV_IOMMU_DC_TC_EN_ATS))
+ return -EINVAL;
+ if ((tc & RISCV_IOMMU_DC_TC_PRPR) &&
+ !(tc & RISCV_IOMMU_DC_TC_EN_PRI))
+ return -EINVAL;
+ if ((tc & RISCV_IOMMU_DC_TC_T2GPA) &&
+ (!(iommu->caps & RISCV_IOMMU_CAPABILITIES_T2GPA) ||
+ !(tc & RISCV_IOMMU_DC_TC_EN_ATS)))
+ return -EINVAL;
+
+ /*
+ * A/D update behavior on stage-2 PTEs is owned by the host,
+ * decide it's value from the AMO_HWAD capability.
+ */
+ tc &= ~RISCV_IOMMU_DC_TC_GADE;
+ if (iommu->caps & RISCV_IOMMU_CAPABILITIES_AMO_HWAD)
+ tc |= RISCV_IOMMU_DC_TC_GADE;
+
+ /*
+ * A/D update behavior on stage-1 PTEs is owned by the guest but
+ * needs AMO_HWAD support.
+ */
+ if ((tc & RISCV_IOMMU_DC_TC_SADE) &&
+ !(iommu->caps & RISCV_IOMMU_CAPABILITIES_AMO_HWAD))
+ return -EOPNOTSUPP;
+
+ /* SBE must match the IOMMU byte order unless END is supported. */
+ if (!(iommu->caps & RISCV_IOMMU_CAPABILITIES_END) &&
+ (!!(tc & RISCV_IOMMU_DC_TC_SBE) !=
+ !!(iommu->fctl & RISCV_IOMMU_FCTL_BE)))
+ return -EINVAL;
+
+ /* Validate the stage-1 mode. */
+ mode = FIELD_GET(RISCV_IOMMU_DC_FSC_MODE, fsc);
+ if (mode == RISCV_IOMMU_DC_FSC_MODE_BARE) {
+ /* No stage-1 translation, root pointer is unused. */
+ fsc = 0;
+ } else {
+ switch (mode) {
+ case RISCV_IOMMU_DC_FSC_IOSATP_MODE_SV39:
+ case RISCV_IOMMU_DC_FSC_IOSATP_MODE_SV48:
+ case RISCV_IOMMU_DC_FSC_IOSATP_MODE_SV57:
+ break;
+ default:
+ return -EINVAL;
+ }
+ if (!riscv_iommu_fsc_supported(iommu, mode))
+ return -EOPNOTSUPP;
+ }
+
+ arg->dc[0] = tc;
+ arg->dc[3] = fsc;
+
+ return 0;
+}
+
+static struct iommu_domain *
+riscv_viommu_alloc_domain_nested(struct iommufd_viommu *viommu, u32 flags,
+ const struct iommu_user_data *user_data)
+{
+ struct riscv_viommu *rv_viommu = container_of(viommu, struct riscv_viommu, core);
+ struct riscv_iommu_nested_domain *nested_domain;
+ struct iommu_hwpt_riscv arg;
+ int ret;
+
+ if (flags)
+ return ERR_PTR(-EOPNOTSUPP);
+
+ ret = iommu_copy_struct_from_user(&arg, user_data,
+ IOMMU_HWPT_DATA_RISCV_S1, dc);
+ if (ret)
+ return ERR_PTR(ret);
+
+ ret = riscv_iommu_validate_vdc(rv_viommu->iommu, &arg);
+ if (ret)
+ return ERR_PTR(ret);
+
+ nested_domain = kzalloc_obj(*nested_domain, GFP_KERNEL_ACCOUNT);
+ if (!nested_domain)
+ return ERR_PTR(-ENOMEM);
+
+ nested_domain->domain.type = IOMMU_DOMAIN_NESTED;
+ nested_domain->domain.ops = &riscv_iommu_nested_ops;
+ nested_domain->viommu = rv_viommu;
+ nested_domain->tc = arg.dc[0];
+ nested_domain->ta = arg.dc[2] & RISCV_IOMMU_DC_TA_PSCID;
+ nested_domain->iosatp = arg.dc[3];
+
+ return &nested_domain->domain;
+}
+
static const struct iommufd_viommu_ops riscv_viommu_ops = {
.destroy = riscv_viommu_destroy,
+ .alloc_domain_nested = riscv_viommu_alloc_domain_nested,
};
static size_t riscv_iommu_get_viommu_size(struct device *dev,
--
2.53.0
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: [RFC PATCH v3 05/10] iommu/riscv: Add nested domain allocation
2026-11-19 13:27 ` [RFC PATCH v3 05/10] iommu/riscv: Add nested domain allocation Gong Shuai
@ 2026-11-21 12:37 ` Andrew Jones
0 siblings, 0 replies; 13+ messages in thread
From: Andrew Jones @ 2026-11-21 12:37 UTC (permalink / raw)
To: Gong Shuai
Cc: kvm, kvm-riscv, linux-riscv, linux-kernel, iommu, Anup Patel,
Atish Patra, Paul Walmsley, Palmer Dabbelt, Albert Ou,
Alexandre Ghiti, Tomasz Jeznach, Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Paolo Bonzini, Sean Christopherson,
Jason Gunthorpe, Kevin Tian, Andrew Jones, Fangyu Yu, Tan Chi,
Wu Fei, Zhao Qingye
On Thu, Nov 19, 2026 at 09:27:39PM +0800, Gong Shuai wrote:
> From: Wu Fei <wu.fei9@sanechips.com.cn>
>
> A nested domain describes two-stage translation through guest and
> host page tables. Iommufd exposes this domain as a nested hwpt.
>
> User space needs to provide the guest stage 1 configuration when
> allocating a nested hwpt. Validate the configuration before storing
> it for use in a physical device context.
>
> Signed-off-by: Wu Fei <wu.fei9@sanechips.com.cn>
> Co-developed-by: Gong Shuai <gong.shuai@sanechips.com.cn>
> Signed-off-by: Gong Shuai <gong.shuai@sanechips.com.cn>
> ---
> drivers/iommu/riscv/iommu.c | 153 ++++++++++++++++++++++++++++++++++++
> 1 file changed, 153 insertions(+)
>
> diff --git a/drivers/iommu/riscv/iommu.c b/drivers/iommu/riscv/iommu.c
> index f2305b953f71..4f8ce579ef08 100644
> --- a/drivers/iommu/riscv/iommu.c
> +++ b/drivers/iommu/riscv/iommu.c
> @@ -1698,8 +1698,161 @@ static void riscv_viommu_destroy(struct iommufd_viommu *viommu)
> ida_free(&riscv_iommu_gscids, rv_viommu->gscid);
> }
>
> +struct riscv_iommu_nested_domain {
> + struct iommu_domain domain;
> + struct riscv_viommu *viommu;
> +
> + u64 tc;
> + u64 ta;
> + u64 iosatp;
> +};
> +
> +#define to_riscv_iommu_nested_domain(domain) \
> + container_of(domain, struct riscv_iommu_nested_domain, domain)
> +
Please rename the macro parameter so it doesn't collide with the domain
member name, otherwise macro expansion replaces the member name too.
Thanks,
drew
^ permalink raw reply [flat|nested] 13+ messages in thread
* [RFC PATCH v3 06/10] iommu/riscv: Add vIOMMU cache invalidation
2026-11-19 13:27 [RFC PATCH v3 00/10] iommu/riscv: Add nested translation (vIOMMU) support Gong Shuai
` (4 preceding siblings ...)
2026-11-19 13:27 ` [RFC PATCH v3 05/10] iommu/riscv: Add nested domain allocation Gong Shuai
@ 2026-11-19 13:27 ` Gong Shuai
2026-11-19 13:27 ` [RFC PATCH v3 07/10] iommufd: Add RISC-V IOMMU hardware info type Gong Shuai
` (3 subsequent siblings)
9 siblings, 0 replies; 13+ messages in thread
From: Gong Shuai @ 2026-11-19 13:27 UTC (permalink / raw)
To: kvm, kvm-riscv, linux-riscv, linux-kernel, iommu
Cc: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt,
Albert Ou, Alexandre Ghiti, Tomasz Jeznach, Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Paolo Bonzini, Sean Christopherson,
Jason Gunthorpe, Kevin Tian, Andrew Jones, Fangyu Yu, Tan Chi,
Wu Fei, Zhao Qingye, Gong Shuai
Guest page table updates require IOTINVAL.VMA commands to invalidate
cached stage 1 translations in the physical IOMMU. Allow user space
to pass these commands through the vIOMMU.
Do not forward guest commands unchanged. Validate them and replace
the guest GSCID with the host-assigned value so a guest cannot
invalidate cached translations belonging to another guest. Preserve
PSCV and PSCID to retain the invalidation scope requested by the guest,
including global mappings.
Co-developed-by: Wu Fei <wu.fei9@sanechips.com.cn>
Signed-off-by: Wu Fei <wu.fei9@sanechips.com.cn>
Signed-off-by: Gong Shuai <gong.shuai@sanechips.com.cn>
---
drivers/iommu/riscv/iommu-bits.h | 13 +++++
drivers/iommu/riscv/iommu.c | 89 ++++++++++++++++++++++++++++++++
2 files changed, 102 insertions(+)
diff --git a/drivers/iommu/riscv/iommu-bits.h b/drivers/iommu/riscv/iommu-bits.h
index 80c32f00ee5e..2f32330fcd16 100644
--- a/drivers/iommu/riscv/iommu-bits.h
+++ b/drivers/iommu/riscv/iommu-bits.h
@@ -486,6 +486,19 @@ struct riscv_iommu_command {
/* dword1[61:10] is the 4K-aligned page address */
#define RISCV_IOMMU_CMD1_IOTINVAL_ADDR GENMASK_ULL(61, 10)
+/*
+ * All standard bits of the IOTINVAL command. The remaining bits are
+ * reserved for future standard use, forwarding them to the physical IOMMU
+ * may make it treat the command as illegal.
+ */
+#define RISCV_IOMMU_CMD0_IOTINVAL_STANDARD \
+ (RISCV_IOMMU_CMD0_OPCODE | RISCV_IOMMU_CMD0_FUNC | \
+ RISCV_IOMMU_CMD0_IOTINVAL_AV | RISCV_IOMMU_CMD0_IOTINVAL_PSCID | \
+ RISCV_IOMMU_CMD0_IOTINVAL_PSCV | RISCV_IOMMU_CMD0_IOTINVAL_GV | \
+ RISCV_IOMMU_CMD0_IOTINVAL_NL | RISCV_IOMMU_CMD0_IOTINVAL_GSCID)
+#define RISCV_IOMMU_CMD1_IOTINVAL_STANDARD \
+ (RISCV_IOMMU_CMD1_IOTINVAL_S | RISCV_IOMMU_CMD1_IOTINVAL_ADDR)
+
/* 3.1.2 IOMMU Command Queue Fences */
/* Fields on dword0 */
#define RISCV_IOMMU_CMD_IOFENCE_OPCODE 2
diff --git a/drivers/iommu/riscv/iommu.c b/drivers/iommu/riscv/iommu.c
index 4f8ce579ef08..4df4411be417 100644
--- a/drivers/iommu/riscv/iommu.c
+++ b/drivers/iommu/riscv/iommu.c
@@ -1850,9 +1850,98 @@ riscv_viommu_alloc_domain_nested(struct iommufd_viommu *viommu, u32 flags,
return &nested_domain->domain;
}
+/*
+ * One invalidation command, viewed as either the little-endian uAPI
+ * payload or the native command words.
+ */
+struct riscv_viommu_invalidation_cmd {
+ union {
+ struct riscv_iommu_command cmd;
+ struct iommu_viommu_riscv_invalidate ucmd;
+ };
+};
+
+static int riscv_viommu_cache_invalidate(struct iommufd_viommu *viommu,
+ struct iommu_user_data_array *array)
+{
+ struct riscv_viommu *rv_viommu = container_of(viommu, struct riscv_viommu, core);
+ struct riscv_iommu_device *iommu = rv_viommu->iommu;
+ struct riscv_viommu_invalidation_cmd *cmds;
+ unsigned int i = 0;
+ int ret;
+
+ if (array->type != IOMMU_VIOMMU_INVALIDATE_DATA_RISCV)
+ return -EINVAL;
+
+ /* entry_num==0 is the data_type support probe */
+ if (!array->entry_num)
+ return 0;
+
+ static_assert(sizeof(*cmds) == 2 * sizeof(u64));
+
+ cmds = kcalloc(array->entry_num, sizeof(*cmds), GFP_KERNEL);
+ if (!cmds)
+ return -ENOMEM;
+
+ ret = iommu_copy_struct_from_full_user_array(cmds, sizeof(*cmds), array,
+ IOMMU_VIOMMU_INVALIDATE_DATA_RISCV);
+ if (ret)
+ goto out_free;
+
+ for (i = 0; i < array->entry_num; i++) {
+ struct riscv_iommu_command *cmd = &cmds[i].cmd;
+ u64 opcode, func;
+
+ /* The uAPI payload is little-endian */
+ cmd->dword0 = le64_to_cpu(cmds[i].ucmd.cmd[0]);
+ cmd->dword1 = le64_to_cpu(cmds[i].ucmd.cmd[1]);
+
+ opcode = FIELD_GET(RISCV_IOMMU_CMD0_OPCODE, cmd->dword0);
+ if (opcode != RISCV_IOMMU_CMD_IOTINVAL_OPCODE) {
+ /* Only page-table cache invalidation is forwarded */
+ ret = -EIO;
+ goto out_free;
+ }
+
+ /* Reject reserved bits instead of forwarding them to the physical IOMMU */
+ if (cmd->dword0 & ~RISCV_IOMMU_CMD0_IOTINVAL_STANDARD ||
+ cmd->dword1 & ~RISCV_IOMMU_CMD1_IOTINVAL_STANDARD) {
+ ret = -EINVAL;
+ goto out_free;
+ }
+
+ func = FIELD_GET(RISCV_IOMMU_CMD0_FUNC, cmd->dword0);
+ if (func != RISCV_IOMMU_CMD_IOTINVAL_FUNC_VMA) {
+ ret = -EIO;
+ goto out_free;
+ }
+
+ /*
+ * Each vIOMMU has its own host GSCID, so the guest PSCID can
+ * be used directly. Preserve PSCV and PSCID to retain the
+ * invalidation scope requested by the guest.
+ */
+ cmd->dword0 &= ~(RISCV_IOMMU_CMD0_IOTINVAL_GV |
+ RISCV_IOMMU_CMD0_IOTINVAL_GSCID);
+ cmd->dword0 |= FIELD_PREP(RISCV_IOMMU_CMD0_IOTINVAL_GSCID,
+ rv_viommu->gscid) |
+ RISCV_IOMMU_CMD0_IOTINVAL_GV;
+
+ riscv_iommu_cmd_send(iommu, cmd);
+ }
+
+ riscv_iommu_cmd_sync(iommu, RISCV_IOMMU_IOTINVAL_TIMEOUT);
+
+out_free:
+ array->entry_num = i;
+ kfree(cmds);
+ return ret;
+}
+
static const struct iommufd_viommu_ops riscv_viommu_ops = {
.destroy = riscv_viommu_destroy,
.alloc_domain_nested = riscv_viommu_alloc_domain_nested,
+ .cache_invalidate = riscv_viommu_cache_invalidate,
};
static size_t riscv_iommu_get_viommu_size(struct device *dev,
--
2.53.0
^ permalink raw reply [flat|nested] 13+ messages in thread* [RFC PATCH v3 07/10] iommufd: Add RISC-V IOMMU hardware info type
2026-11-19 13:27 [RFC PATCH v3 00/10] iommu/riscv: Add nested translation (vIOMMU) support Gong Shuai
` (5 preceding siblings ...)
2026-11-19 13:27 ` [RFC PATCH v3 06/10] iommu/riscv: Add vIOMMU cache invalidation Gong Shuai
@ 2026-11-19 13:27 ` Gong Shuai
2026-11-19 13:27 ` [RFC PATCH v3 08/10] iommu/riscv: Add riscv_iommu_hw_info Gong Shuai
` (2 subsequent siblings)
9 siblings, 0 replies; 13+ messages in thread
From: Gong Shuai @ 2026-11-19 13:27 UTC (permalink / raw)
To: kvm, kvm-riscv, linux-riscv, linux-kernel, iommu
Cc: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt,
Albert Ou, Alexandre Ghiti, Tomasz Jeznach, Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Paolo Bonzini, Sean Christopherson,
Jason Gunthorpe, Kevin Tian, Andrew Jones, Fangyu Yu, Tan Chi,
Wu Fei, Zhao Qingye, Gong Shuai
From: Wu Fei <wu.fei9@sanechips.com.cn>
A VMM needs the physical IOMMU capabilities to decide which features
it can expose to a guest. Define IOMMU_HW_INFO_TYPE_RISCV and struct
iommu_hw_info_riscv so IOMMU_GET_HW_INFO can return the RISC-V
capabilities register.
Signed-off-by: Wu Fei <wu.fei9@sanechips.com.cn>
Signed-off-by: Gong Shuai <gong.shuai@sanechips.com.cn>
---
include/uapi/linux/iommufd.h | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h
index fee63e6cbb7a..adc7f30b2b18 100644
--- a/include/uapi/linux/iommufd.h
+++ b/include/uapi/linux/iommufd.h
@@ -733,6 +733,28 @@ struct iommu_hw_info_amd {
__aligned_u64 efr2;
};
+/**
+ * struct iommu_hw_info_riscv - RISC-V IOMMU device info
+ * (IOMMU_HW_INFO_TYPE_RISCV)
+ *
+ * @capabilities: Value of the RISC-V IOMMU capabilities register, defined
+ * in "IOMMU capabilities (capabilities)" of the RISC-V
+ * IOMMU Architecture Specification. The value is reported
+ * raw by the hardware.
+ *
+ * The VMM is responsible to determine the appropriate fields to be
+ * exposed to the VM since certain features are not currently supported
+ * by the kernel for nested domains. Current list of supported fields:
+ * - capabilities[Sv39, Sv48, Sv57, END, AMO_HWAD]
+ *
+ * This list may expand in the future. It is important that VMMs do not
+ * expose bits outside the list to the VM, to allow for compatibility
+ * with future kernels.
+ */
+struct iommu_hw_info_riscv {
+ __aligned_u64 capabilities;
+};
+
/**
* enum iommu_hw_info_type - IOMMU Hardware Info Types
* @IOMMU_HW_INFO_TYPE_NONE: Output by the drivers that do not report hardware
@@ -743,6 +765,7 @@ struct iommu_hw_info_amd {
* @IOMMU_HW_INFO_TYPE_TEGRA241_CMDQV: NVIDIA Tegra241 CMDQV (extension for ARM
* SMMUv3) info type
* @IOMMU_HW_INFO_TYPE_AMD: AMD IOMMU info type
+ * @IOMMU_HW_INFO_TYPE_RISCV: RISC-V IOMMU info type
*/
enum iommu_hw_info_type {
IOMMU_HW_INFO_TYPE_NONE = 0,
@@ -751,6 +774,7 @@ enum iommu_hw_info_type {
IOMMU_HW_INFO_TYPE_ARM_SMMUV3 = 2,
IOMMU_HW_INFO_TYPE_TEGRA241_CMDQV = 3,
IOMMU_HW_INFO_TYPE_AMD = 4,
+ IOMMU_HW_INFO_TYPE_RISCV = 5,
};
/**
--
2.53.0
^ permalink raw reply [flat|nested] 13+ messages in thread* [RFC PATCH v3 08/10] iommu/riscv: Add riscv_iommu_hw_info
2026-11-19 13:27 [RFC PATCH v3 00/10] iommu/riscv: Add nested translation (vIOMMU) support Gong Shuai
` (6 preceding siblings ...)
2026-11-19 13:27 ` [RFC PATCH v3 07/10] iommufd: Add RISC-V IOMMU hardware info type Gong Shuai
@ 2026-11-19 13:27 ` Gong Shuai
2026-11-19 13:27 ` [RFC PATCH v3 09/10] iommu/riscv: Add riscv_iommu_attach_dev_nested Gong Shuai
2026-11-19 13:27 ` [RFC PATCH v3 10/10] iommu/riscv: Flush the right address space on bond unlink Gong Shuai
9 siblings, 0 replies; 13+ messages in thread
From: Gong Shuai @ 2026-11-19 13:27 UTC (permalink / raw)
To: kvm, kvm-riscv, linux-riscv, linux-kernel, iommu
Cc: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt,
Albert Ou, Alexandre Ghiti, Tomasz Jeznach, Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Paolo Bonzini, Sean Christopherson,
Jason Gunthorpe, Kevin Tian, Andrew Jones, Fangyu Yu, Tan Chi,
Wu Fei, Zhao Qingye, Gong Shuai
From: Wu Fei <wu.fei9@sanechips.com.cn>
Implement the hw_info op to return the physical IOMMU capabilities
register. This allows IOMMU_GET_HW_INFO to report the available host
features to a VMM.
Signed-off-by: Wu Fei <wu.fei9@sanechips.com.cn>
Signed-off-by: Gong Shuai <gong.shuai@sanechips.com.cn>
---
drivers/iommu/riscv/iommu.c | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/drivers/iommu/riscv/iommu.c b/drivers/iommu/riscv/iommu.c
index 4df4411be417..97d360318fe2 100644
--- a/drivers/iommu/riscv/iommu.c
+++ b/drivers/iommu/riscv/iommu.c
@@ -1979,9 +1979,27 @@ static int riscv_viommu_init(struct iommufd_viommu *viommu,
return 0;
}
+static void *riscv_iommu_hw_info(struct device *dev, u32 *length,
+ enum iommu_hw_info_type *type)
+{
+ struct iommu_hw_info_riscv *info;
+ struct riscv_iommu_device *iommu = dev_to_iommu(dev);
+
+ info = kzalloc_obj(*info);
+ if (!info)
+ return ERR_PTR(-ENOMEM);
+
+ info->capabilities = iommu->caps;
+ *length = sizeof(*info);
+ *type = IOMMU_HW_INFO_TYPE_RISCV;
+
+ return info;
+}
+
static const struct iommu_ops riscv_iommu_ops = {
.of_xlate = riscv_iommu_of_xlate,
.capable = riscv_iommu_capable,
+ .hw_info = riscv_iommu_hw_info,
.identity_domain = &riscv_iommu_identity_domain,
.blocked_domain = &riscv_iommu_blocking_domain,
.release_domain = &riscv_iommu_blocking_domain,
--
2.53.0
^ permalink raw reply [flat|nested] 13+ messages in thread* [RFC PATCH v3 09/10] iommu/riscv: Add riscv_iommu_attach_dev_nested
2026-11-19 13:27 [RFC PATCH v3 00/10] iommu/riscv: Add nested translation (vIOMMU) support Gong Shuai
` (7 preceding siblings ...)
2026-11-19 13:27 ` [RFC PATCH v3 08/10] iommu/riscv: Add riscv_iommu_hw_info Gong Shuai
@ 2026-11-19 13:27 ` Gong Shuai
2026-11-21 15:48 ` Andrew Jones
2026-11-19 13:27 ` [RFC PATCH v3 10/10] iommu/riscv: Flush the right address space on bond unlink Gong Shuai
9 siblings, 1 reply; 13+ messages in thread
From: Gong Shuai @ 2026-11-19 13:27 UTC (permalink / raw)
To: kvm, kvm-riscv, linux-riscv, linux-kernel, iommu
Cc: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt,
Albert Ou, Alexandre Ghiti, Tomasz Jeznach, Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Paolo Bonzini, Sean Christopherson,
Jason Gunthorpe, Kevin Tian, Andrew Jones, Fangyu Yu, Tan Chi,
Wu Fei, Zhao Qingye, Gong Shuai
From: Wu Fei <wu.fei9@sanechips.com.cn>
Attach a device to a nested domain by combining the guest provided
stage 1 context with the host-owned stage 2 context in the device
directory entry. This allows the physical IOMMU to translate device
DMA through both stages.
The device is bonded to the stage 2 parent domain so that it shares
the parent's invalidation state.
Signed-off-by: Wu Fei <wu.fei9@sanechips.com.cn>
Co-developed-by: Gong Shuai <gong.shuai@sanechips.com.cn>
Signed-off-by: Gong Shuai <gong.shuai@sanechips.com.cn>
---
drivers/iommu/riscv/iommu.c | 83 +++++++++++++++++++++++++++++--------
1 file changed, 66 insertions(+), 17 deletions(-)
diff --git a/drivers/iommu/riscv/iommu.c b/drivers/iommu/riscv/iommu.c
index 97d360318fe2..e8d894f3e760 100644
--- a/drivers/iommu/riscv/iommu.c
+++ b/drivers/iommu/riscv/iommu.c
@@ -889,29 +889,36 @@ struct riscv_iommu_bond {
struct list_head list;
struct rcu_head rcu;
struct device *dev;
+ int gscid;
};
static int riscv_iommu_bond_link(struct riscv_iommu_domain *domain,
- struct device *dev)
+ struct device *dev, int gscid)
{
struct riscv_iommu_device *iommu = dev_to_iommu(dev);
- struct riscv_iommu_bond *bond;
- struct list_head *bonds;
+ struct riscv_iommu_bond *bond, *pos;
+ struct list_head *bonds = &domain->bonds;
bond = kzalloc_obj(*bond);
if (!bond)
return -ENOMEM;
bond->dev = dev;
+ bond->gscid = gscid;
/*
* List of devices attached to the domain is arranged based on
- * managed IOMMU device.
+ * managed IOMMU device and GSCID.
*/
spin_lock(&domain->lock);
- list_for_each(bonds, &domain->bonds)
- if (dev_to_iommu(list_entry(bonds, struct riscv_iommu_bond, list)->dev) == iommu)
+ list_for_each_entry(pos, &domain->bonds, list) {
+ if (dev_to_iommu(pos->dev) != iommu)
+ continue;
+
+ bonds = &pos->list;
+ if (pos->gscid == bond->gscid)
break;
+ }
list_add_rcu(&bond->list, bonds);
spin_unlock(&domain->lock);
@@ -1076,12 +1083,13 @@ static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain,
struct riscv_iommu_device *iommu, *prev;
struct riscv_iommu_bond *bond;
struct riscv_iommu_tlbi tlbi;
+ int prev_gscid = -1;
riscv_iommu_tlbi_calc(&tlbi, gather);
/*
- * For each IOMMU linked with this protection domain (via bonds->dev),
- * an IOTLB invaliation command will be submitted and executed.
+ * For each IOMMU and GSCID linked with this protection domain through
+ * its bonds, an IOTLB invalidation command will be submitted and executed.
*
* Possbile race with domain attach flow is handled by sequencing
* bond creation - riscv_iommu_bond_link(), and device directory
@@ -1091,9 +1099,9 @@ static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain,
* -------------------------- --------------------------
* update page table entries add dev to the bond list
* FENCE RW,RW FENCE RW,RW
- * For all IOMMUs: (can be empty) Update FSC/PSCID
- * FENCE IOW,IOW FENCE IOW,IOW
- * IOTLB.INVAL IODIR.INVAL
+ * For all IOMMU/GSCID pairs: Update FSC/PSCID
+ * FENCE IOW,IOW FENCE IOW,IOW
+ * IOTLB.INVAL IODIR.INVAL
* IOFENCE.C
*
* If bond list is not updated with new device, directory context will
@@ -1111,15 +1119,16 @@ static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain,
/*
* IOTLB invalidation request can be safely omitted if already sent
- * to the IOMMU for the same PSCID, and with domain->bonds list
- * arranged based on the device's IOMMU, it's sufficient to check
- * last device the invalidation was sent to.
+ * to the IOMMU for the same GSCID. Since bonds are arranged by
+ * IOMMU and GSCID, it is sufficient to check the previous bond.
*/
- if (iommu == prev)
+ if (iommu == prev && bond->gscid == prev_gscid)
continue;
- riscv_iommu_iotlb_inval_iommu(iommu, domain->pscid, domain->gscid, &tlbi);
+ riscv_iommu_iotlb_inval_iommu(iommu, domain->pscid, bond->gscid,
+ &tlbi);
prev = iommu;
+ prev_gscid = bond->gscid;
}
prev = NULL;
@@ -1394,7 +1403,7 @@ static int riscv_iommu_attach_paging_domain(struct iommu_domain *iommu_domain,
dc.ta = FIELD_PREP(RISCV_IOMMU_DC_TA_PSCID, domain->pscid);
dc.tc |= RISCV_IOMMU_DC_TC_V;
- if (riscv_iommu_bond_link(domain, dev))
+ if (riscv_iommu_bond_link(domain, dev, domain->gscid))
return -ENOMEM;
riscv_iommu_iodir_update(iommu, dev, &dc);
@@ -1710,12 +1719,52 @@ struct riscv_iommu_nested_domain {
#define to_riscv_iommu_nested_domain(domain) \
container_of(domain, struct riscv_iommu_nested_domain, domain)
+static int riscv_iommu_attach_dev_nested(struct iommu_domain *domain,
+ struct device *dev,
+ struct iommu_domain *old_domain)
+{
+ struct riscv_iommu_nested_domain *nested_domain =
+ to_riscv_iommu_nested_domain(domain);
+ struct riscv_iommu_device *iommu = dev_to_iommu(dev);
+ struct riscv_iommu_domain *s2_parent = nested_domain->viommu->s2_parent;
+ struct riscv_iommu_info *info = dev_iommu_priv_get(dev);
+ struct pt_iommu_riscv_64_hw_info pt_info;
+ struct riscv_iommu_dc dc = {0};
+
+ if (nested_domain->viommu->iommu != iommu)
+ return -EINVAL;
+
+ if (!s2_parent->nest_parent)
+ return -EINVAL;
+
+ pt_iommu_riscv_64_hw_info(&s2_parent->riscvpt, &pt_info);
+
+ dc.tc = nested_domain->tc;
+ dc.fsc = nested_domain->iosatp;
+
+ dc.iohgatp = FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_MODE, pt_info.iohgatp_mode) |
+ FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_PPN, pt_info.ppn) |
+ FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_GSCID,
+ nested_domain->viommu->gscid);
+ dc.ta = nested_domain->ta;
+
+ if (riscv_iommu_bond_link(s2_parent, dev, nested_domain->viommu->gscid))
+ return -ENOMEM;
+
+ riscv_iommu_iodir_update(iommu, dev, &dc);
+ riscv_iommu_bond_unlink(info->domain, dev);
+ info->domain = s2_parent;
+
+ return 0;
+}
+
static void riscv_iommu_domain_nested_free(struct iommu_domain *domain)
{
kfree(to_riscv_iommu_nested_domain(domain));
}
static const struct iommu_domain_ops riscv_iommu_nested_ops = {
+ .attach_dev = riscv_iommu_attach_dev_nested,
.free = riscv_iommu_domain_nested_free,
};
--
2.53.0
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: [RFC PATCH v3 09/10] iommu/riscv: Add riscv_iommu_attach_dev_nested
2026-11-19 13:27 ` [RFC PATCH v3 09/10] iommu/riscv: Add riscv_iommu_attach_dev_nested Gong Shuai
@ 2026-11-21 15:48 ` Andrew Jones
0 siblings, 0 replies; 13+ messages in thread
From: Andrew Jones @ 2026-11-21 15:48 UTC (permalink / raw)
To: Gong Shuai
Cc: kvm, kvm-riscv, linux-riscv, linux-kernel, iommu, Anup Patel,
Atish Patra, Paul Walmsley, Palmer Dabbelt, Albert Ou,
Alexandre Ghiti, Tomasz Jeznach, Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Paolo Bonzini, Sean Christopherson,
Jason Gunthorpe, Kevin Tian, Andrew Jones, Fangyu Yu, Tan Chi,
Wu Fei, Zhao Qingye
On Thu, Nov 19, 2026 at 09:27:43PM +0800, Gong Shuai wrote:
...
> + if (riscv_iommu_bond_link(s2_parent, dev, nested_domain->viommu->gscid))
> + return -ENOMEM;
> +
> + riscv_iommu_iodir_update(iommu, dev, &dc);
> + riscv_iommu_bond_unlink(info->domain, dev);
> + info->domain = s2_parent;
> +
When moving between the S2 parent and a nested domain on it, the parent
briefly has two bonds for dev. bond_link() puts the new bond after the
first bond with the same GSCID, which may come before dev's old bond,
and since it's then dev's first bond, bond_unlink() removes it. dev is
left bonded with its old GSCID, so its new GSCID misses invalidations
once no other device's bond has it.
Passing the old domain to bond_unlink(), like I do in [1] for a
different reason, would let it remove the bond matching both dev and
old's GSCID.
[1] https://lore.kernel.org/all/20260928143113.49838-12-andrew.jones@oss.qualcomm.com/
Thanks,
drew
^ permalink raw reply [flat|nested] 13+ messages in thread
* [RFC PATCH v3 10/10] iommu/riscv: Flush the right address space on bond unlink
2026-11-19 13:27 [RFC PATCH v3 00/10] iommu/riscv: Add nested translation (vIOMMU) support Gong Shuai
` (8 preceding siblings ...)
2026-11-19 13:27 ` [RFC PATCH v3 09/10] iommu/riscv: Add riscv_iommu_attach_dev_nested Gong Shuai
@ 2026-11-19 13:27 ` Gong Shuai
9 siblings, 0 replies; 13+ messages in thread
From: Gong Shuai @ 2026-11-19 13:27 UTC (permalink / raw)
To: kvm, kvm-riscv, linux-riscv, linux-kernel, iommu
Cc: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt,
Albert Ou, Alexandre Ghiti, Tomasz Jeznach, Joerg Roedel (AMD),
Will Deacon, Robin Murphy, Paolo Bonzini, Sean Christopherson,
Jason Gunthorpe, Kevin Tian, Andrew Jones, Fangyu Yu, Tan Chi,
Wu Fei, Zhao Qingye, Gong Shuai
When the last bond to an IOMMU is removed, the entries cached for the
address space it used must be invalidated. Nested and second-stage bonds
are identified by GSCID, first-stage paging bonds by PSCID.
Signed-off-by: Gong Shuai <gong.shuai@sanechips.com.cn>
---
drivers/iommu/riscv/iommu.c | 20 +++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)
diff --git a/drivers/iommu/riscv/iommu.c b/drivers/iommu/riscv/iommu.c
index e8d894f3e760..a106105bd31b 100644
--- a/drivers/iommu/riscv/iommu.c
+++ b/drivers/iommu/riscv/iommu.c
@@ -934,6 +934,7 @@ static void riscv_iommu_bond_unlink(struct riscv_iommu_domain *domain,
struct riscv_iommu_device *iommu = dev_to_iommu(dev);
struct riscv_iommu_bond *bond, *found = NULL;
struct riscv_iommu_command cmd;
+ int gscid = 0;
int count = 0;
if (!domain)
@@ -948,18 +949,27 @@ static void riscv_iommu_bond_unlink(struct riscv_iommu_domain *domain,
else if (dev_to_iommu(bond->dev) == iommu)
count++;
}
- if (found)
+ if (found) {
+ gscid = found->gscid;
list_del_rcu(&found->list);
+ }
spin_unlock(&domain->lock);
kfree_rcu(found, rcu);
/*
- * If this was the last bond between this domain and the IOMMU
- * invalidate all cached entries for domain's PSCID.
+ * If this was the last bond between this domain and the IOMMU,
+ * invalidate all cached entries for the address space used by that
+ * bond. Nested and second-stage bonds are identified by GSCID,
+ * first-stage paging bonds by PSCID.
*/
if (!count) {
- riscv_iommu_cmd_inval_vma(&cmd);
- riscv_iommu_cmd_inval_set_pscid(&cmd, domain->pscid);
+ if (gscid) {
+ riscv_iommu_cmd_inval_gvma(&cmd);
+ riscv_iommu_cmd_inval_set_gscid(&cmd, gscid);
+ } else {
+ riscv_iommu_cmd_inval_vma(&cmd);
+ riscv_iommu_cmd_inval_set_pscid(&cmd, domain->pscid);
+ }
riscv_iommu_cmd_send(iommu, &cmd);
riscv_iommu_cmd_sync(iommu, RISCV_IOMMU_IOTINVAL_TIMEOUT);
--
2.53.0
^ permalink raw reply [flat|nested] 13+ messages in thread