Skip to contentSkip to conversation

iommu/riscv: Add hardware dirty tracking for second-stage domains

RFC PATCH Superseded [email protected] 4 participants 26 messages Sep 15 – Oct 20

Subsystems (4)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#1 lore raw

RFC PATCH v4 1/10 iommupt: Add RISC-V Second-stage (iohgatp) page table support

In reply to [email protected]

From: Fangyu Yu <[email protected]>

Add support for Sv39x4/Sv48x4/Sv57x4 Second-stage page tables used by the RISC-V IOMMU iohgatp register. The x4 root page table is 16 KiB instead of the usual 4 KiB, covering 2 extra GPA bits (hw_max_vasz_lg2 = 41/50/59).

---
 drivers/iommu/generic_pt/fmt/iommu_riscv64.c |   2 +-
 drivers/iommu/generic_pt/fmt/riscv.h         | 109 +++++++++++++++----
 include/linux/generic_pt/common.h            |   4 +
 include/linux/generic_pt/iommu.h             |  11 ++
 4 files changed, 102 insertions(+), 24 deletions(-)
drivers/iommu/generic_pt/fmt/iommu_riscv64.c +1 -1
index 1bb74114fb02..fa61839e5599 100644
@@ -8,7 +8,7 @@
 	(BIT(PT_FEAT_SIGN_EXTEND) | BIT(PT_FEAT_FLUSH_RANGE) | \
 	 BIT(PT_FEAT_RISCV_SVNAPOT_64K) |                      \
 	 BIT(PT_FEAT_DETAILED_GATHER) |                        \
-	 BIT(PT_FEAT_RISCV_SVPBMT))
+	 BIT(PT_FEAT_RISCV_SVPBMT) | BIT(PT_FEAT_RISCV_S2))
 #define PT_FORCE_ENABLED_FEATURES BIT(PT_FEAT_DETAILED_GATHER)
 
 #include "iommu_template.h"
drivers/iommu/generic_pt/fmt/riscv.h +86 -23

149 lines not on this page · raw message

include/linux/generic_pt/common.h +4 -0
index 07ef1c8341a4..f2e130c2b804 100644
@@ -197,6 +197,10 @@ enum {
 	 * Support Svpbmt extension: encode page-based memory type (PBMT) in PTEs.
 	 */
 	PT_FEAT_RISCV_SVPBMT,
+	/*
+	 * Using second-stage / iohgatp address translation.
+	 */
+	PT_FEAT_RISCV_S2,
 
 };
 
include/linux/generic_pt/iommu.h +11 -0
index dd0edd02a48a..2eed9b78f56c 100644
@@ -328,7 +328,18 @@ struct pt_iommu_riscv_64_cfg {
 
 struct pt_iommu_riscv_64_hw_info {
 	u64 ppn;
+	/*
+	 * First-stage (fsc/iosatp) MODE encoding:
+	 *   8 = Sv39, 9 = Sv48, 10 = Sv57
+	 * Used to program DC.fsc.iosatp.MODE.
+	 */
 	u8 fsc_iosatp_mode;
+	/*
+	 * Second-stage (iohgatp) MODE encoding:
+	 *   8 = Sv39x4, 9 = Sv48x4, 10 = Sv57x4
+	 * Used to program DC.iohgatp.MODE.
+	 */
+	u8 iohgatp_mode;
 };
 
 IOMMU_FORMAT(riscv_64, riscv_64pt);
Signature · 1 line
2.50.1
#2 lore raw

RFC PATCH v4 2/10 iommupt: Add RISC-V dirty tracking PTE ops

In reply to [email protected]

From: Fangyu Yu <[email protected]>

Implement the three dirty-tracking hooks required by the generic page table framework for the RISC-V format:

pt_entry_is_write_dirty():
Check the D bit (bit 7) in the PTE.

pt_entry_make_write_clean():
Clear the D bit across the full contiguous range.

pt_entry_make_write_dirty():
Atomically set D via try_cmpxchg64() on a single PTE.

---
 drivers/iommu/generic_pt/fmt/riscv.h | 49 ++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
drivers/iommu/generic_pt/fmt/riscv.h +49 -0
index d4445e463fa9..2f4838eb44af 100644
@@ -223,6 +223,55 @@ static inline void riscvpt_attr_from_entry(const struct pt_state *pts,
 }
 #define pt_attr_from_entry riscvpt_attr_from_entry
 
+/*
+ * Dirty tracking: RISC-V PTEs use D (bit 7) as the hardware dirty bit.
+ * When Svnapot 64K is active a leaf entry spans 16 consecutive PTEs; we
+ * must check / clear all of them so that no dirty indication is lost.
+ */
+static inline bool riscvpt_entry_is_write_dirty(const struct pt_state *pts)
+{
+	unsigned int num_contig_lg2 = riscvpt_entry_num_contig_lg2(pts);
+	const pt_riscv_entry_t *tablep =
+		pt_cur_table(pts, pt_riscv_entry_t) +
+		log2_set_mod(pts->index, 0, num_contig_lg2);
+	const pt_riscv_entry_t *end = tablep + log2_to_int(num_contig_lg2);
+
+	for (; tablep != end; tablep++)
+		if (READ_ONCE(*tablep) & RISCVPT_D)
+			return true;
+	return false;
+}
+#define pt_entry_is_write_dirty riscvpt_entry_is_write_dirty
+
+static inline void riscvpt_entry_make_write_clean(struct pt_state *pts)
+{
+	unsigned int num_contig_lg2 = riscvpt_entry_num_contig_lg2(pts);
+	pt_riscv_entry_t *tablep =
+		pt_cur_table(pts, pt_riscv_entry_t) +
+		log2_set_mod(pts->index, 0, num_contig_lg2);
+	pt_riscv_entry_t *end = tablep + log2_to_int(num_contig_lg2);
+
+	for (; tablep != end; tablep++)
+		WRITE_ONCE(*tablep, READ_ONCE(*tablep) & ~(pt_riscv_entry_t)RISCVPT_D);
+}
+#define pt_entry_make_write_clean riscvpt_entry_make_write_clean
+
+static inline bool riscvpt_entry_make_write_dirty(struct pt_state *pts)
+{
+	pt_riscv_entry_t *tablep =
+		pt_cur_table(pts, pt_riscv_entry_t) + pts->index;
+	pt_riscv_entry_t new = pts->entry | RISCVPT_D;
+
+	return try_cmpxchg64(tablep, &pts->entry, new);
+}
+#define pt_entry_make_write_dirty riscvpt_entry_make_write_dirty
+
+static inline bool riscvpt_dirty_supported(struct pt_common *common)
+{
+	return pt_feature(common, PT_FEAT_RISCV_S2);
+}
+#define pt_dirty_supported riscvpt_dirty_supported
+
 /* --- iommu */
 #include <linux/generic_pt/iommu.h>
 #include <linux/iommu.h>
Signature · 1 line
2.50.1
#0 lore raw

v4 cover iommu/riscv: Add hardware dirty tracking for second-stage domains

From: Fangyu Yu <[email protected]>

The RISC-V IOMMU architecture defines an AMO_HWAD capability (Hardware Access/Dirty update) that allows the IOMMU to atomically set the A/D bits in second-stage PTEs on DMA access. When DC.tc.GADE is asserted, the IOMMU autonomously sets D on the first write to a page mapped by an iohgatp domain. This series wires that capability up to the iommufd dirty-tracking interface (IOMMU_HWPT_SET_DIRTY_TRACKING / IOMMU_HWPT_GET_DIRTY_BITMAP) and reports IOMMU_CAP_DIRTY_TRACKING.

Design notes
------------

* The feature is scoped to second-stage (iohgatp) domains only; these are the domains created for KVM / VFIO device pass-through when userspace allocates an HWPT with IOMMU_HWPT_ALLOC_NEST_PARENT or IOMMU_HWPT_ALLOC_DIRTY_TRACKING. First-stage (iosatp) domains are not touched by this series.

* The page-table side plugs into the existing generic_pt dirty hook framework (amdv1 / vtdss style). RISC-V adds the three required PTE ops – is_write_dirty / make_write_clean / make_write_dirty.

Testing
-------

* Test on QEMU RISC-V, a nvme and an e1000e device was passed through to an L2 guest via vfio-pci + iommufd.

* generic_pt KUnit: the existing test_dirty case now runs and passes for the RISC-V 64-bit format.

Follow-up work
--------------
* Build a dedicated end-to-end test case that drives the full flow (HWPT_ALLOC with DIRTY_TRACKING -> attach -> IOAS_MAP -> generate real DMA -> SET_DIRTY_TRACKING -> GET_DIRTY_BITMAP -> verify bitmap against expected IOVA footprint) so that the behaviour can be regression-tested beyond the KUnit PTE-level coverage.

* If possible, rebase and retest on top of the updated "iommu irqbypass" patchset.

---
Changes in v4 (Jason's suggestions):
- Rebased the series on top of [1] and [2].
- Drop the RISC-V-specific pt_num_items_lg2() top-level special case.
- Validate page-table address widths by translation stage: accept only Sv39/Sv48/Sv57 widths for first-stage tables and only Sv39x4/Sv48x4/ Sv57x4 widths for second-stage tables.
- Replace the aliased first-stage/second-stage MODE union with independent FSC/IOSATP and IOHGATP MODE fields.
- Add pt_dirty_supported() to restrict RISC-V generic_pt dirty tracking to second-stage tables.
- Link to v3:
https://lore.kernel.org/linux-riscv/[email protected]/
Changes in v3 (Andrew Jones's suggestions):
- Rebased the series on top of Andrew Jones' generic_pt RISC-V supported feature mask fix, which adds PT_FEAT_RISCV_SVPBMT to the supported feature set.
- Added PT_FEAT_RISCV_S2 to the RISC-V generic_pt supported feature mask and KUnit feature matrix.
- Kept PT_FEAT_SIGN_EXTEND only for first-stage RISC-V KUnit configs; second stage configs now use PT_FEAT_RISCV_S2 without sign-extension semantics.
- Split RISC-V IOMMU capability checks into first-stage FSC and second-stage IOHGATP helpers.
- Updated second-stage paging domain setup to use GPA widths 41/50/59 and to avoid PT_FEAT_SIGN_EXTEND.
- Link to v2:
https://lore.kernel.org/linux-riscv/[email protected]/
Changes in v2 (Jason's suggestions):
- Introduced a single PT_FEAT_RISCV_S2: second-stage selection is driven purely by this feature bit.
- Switched from dynamic DC.tc.GADE toggling to static pre-enable.
- domain_alloc_paging_flags: follow the switch/case design from other drivers.
- Drop IOMMU_CAP_DEFERRED_FLUSH in riscv_iommu_capable.
- Remove the .hw_info-related patch.
- Link to v1:
https://lore.kernel.org/linux-riscv/[email protected]/

[1] https://lore.kernel.org/linux-iommu/[email protected]/
[2] https://lore.kernel.org/linux-iommu/[email protected]/

Fangyu Yu (6):
iommupt: Add RISC-V Second-stage (iohgatp) page table support
iommupt: Add RISC-V dirty tracking PTE ops
iommu/riscv: Add domain_alloc_paging_flags for second-stage domain iommu/riscv: Pre-enable GADE for second-stage domains iommu/riscv: Add dirty tracking support for second-stage domains iommu/riscv: Add IOTINVAL.GVMA after updating DDT/PDT entries

Tomasz Jeznach (2):
iommu/riscv: report iommu capabilities
RISC-V: KVM: Enable KVM_VFIO interfaces on RISC-V arch

Zong Li (2):
iommu/riscv: use data structure instead of individual values iommu/riscv: support GSCID and GVMA invalidation command

 arch/riscv/kvm/Kconfig                       |   2 +
 drivers/iommu/generic_pt/fmt/iommu_riscv64.c |   2 +-
 drivers/iommu/generic_pt/fmt/riscv.h         | 158 +++++++++++--
 drivers/iommu/riscv/iommu-bits.h             |   7 +
 drivers/iommu/riscv/iommu.c                  | 224 +++++++++++++++----
 include/linux/generic_pt/common.h            |   4 +
 include/linux/generic_pt/iommu.h             |  11 +
 7 files changed, 336 insertions(+), 72 deletions(-)
Signature · 1 line
2.50.1
#3 lore raw

RFC PATCH v4 3/10 iommu/riscv: report iommu capabilities

From: Tomasz Jeznach <[email protected]>

Report RISC-V IOMMU capabilities required by VFIO subsystem to enable PCIe device assignment.

---
 drivers/iommu/riscv/iommu.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/iommu/riscv/iommu.c +11 -0
index cec3ddd7ab10..a6c8307e82ea 100644
@@ -1422,6 +1422,16 @@ static struct iommu_group *riscv_iommu_device_group(struct device *dev)
 	return generic_device_group(dev);
 }
 
+static bool riscv_iommu_capable(struct device *dev, enum iommu_cap cap)
+{
+	switch (cap) {
+	case IOMMU_CAP_CACHE_COHERENCY:
+		return true;
+	default:
+		return false;
+	}
+}
+
 static int riscv_iommu_of_xlate(struct device *dev, const struct of_phandle_args *args)
 {
 	return iommu_fwspec_add_ids(dev, args->args, 1);
@@ -1483,6 +1493,7 @@ static void riscv_iommu_release_device(struct device *dev)
 
 static const struct iommu_ops riscv_iommu_ops = {
 	.of_xlate = riscv_iommu_of_xlate,
+	.capable = riscv_iommu_capable,
 	.identity_domain = &riscv_iommu_identity_domain,
 	.blocked_domain = &riscv_iommu_blocking_domain,
 	.release_domain = &riscv_iommu_blocking_domain,
Signature · 1 line
2.50.1
#5 lore raw

RFC PATCH v4 4/10 iommu/riscv: use data structure instead of individual values

In reply to [email protected]

From: Zong Li <[email protected]>

The parameter will be increased when we need to set up more bit fields in the device context. Use a data structure to wrap them up.

---
 drivers/iommu/riscv/iommu.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
drivers/iommu/riscv/iommu.c +17 -10
index a6c8307e82ea..c5b6214ee672 100644
@@ -1165,7 +1165,7 @@ static void riscv_iommu_iodir_iotinval(struct riscv_iommu_device *iommu,
  * interim translation faults.
  */
 static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,
-				     struct device *dev, u64 fsc, u64 ta)
+				     struct device *dev, struct riscv_iommu_dc *new_dc)
 {
 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
 	struct riscv_iommu_dc *dc;
@@ -1204,10 +1204,10 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,
 	for (i = 0; i < fwspec->num_ids; i++) {
 		dc = riscv_iommu_get_dc(iommu, fwspec->ids[i]);
 		tc = READ_ONCE(dc->tc);
-		tc |= ta & RISCV_IOMMU_DC_TC_V;
+		tc |= new_dc->ta & RISCV_IOMMU_DC_TC_V;
 
-		WRITE_ONCE(dc->fsc, fsc);
-		WRITE_ONCE(dc->ta, ta & RISCV_IOMMU_PC_TA_PSCID);
+		WRITE_ONCE(dc->fsc, new_dc->fsc);
+		WRITE_ONCE(dc->ta, new_dc->ta & RISCV_IOMMU_PC_TA_PSCID);
 		/* Update device context, write TC.V as the last step. */
 		dma_wmb();
 		WRITE_ONCE(dc->tc, tc);
@@ -1288,22 +1288,22 @@ static int riscv_iommu_attach_paging_domain(struct iommu_domain *iommu_domain,
 	struct riscv_iommu_device *iommu = dev_to_iommu(dev);
 	struct riscv_iommu_info *info = dev_iommu_priv_get(dev);
 	struct pt_iommu_riscv_64_hw_info pt_info;
-	u64 fsc, ta;
+	struct riscv_iommu_dc dc = {0};
 
 	pt_iommu_riscv_64_hw_info(&domain->riscvpt, &pt_info);
 
 	if (!riscv_iommu_pt_supported(iommu, pt_info.fsc_iosatp_mode))
 		return -ENODEV;
 
-	fsc = FIELD_PREP(RISCV_IOMMU_PC_FSC_MODE, pt_info.fsc_iosatp_mode) |
+	dc.fsc = FIELD_PREP(RISCV_IOMMU_PC_FSC_MODE, pt_info.fsc_iosatp_mode) |
 	      FIELD_PREP(RISCV_IOMMU_PC_FSC_PPN, pt_info.ppn);
-	ta = FIELD_PREP(RISCV_IOMMU_PC_TA_PSCID, domain->pscid) |
+	dc.ta = FIELD_PREP(RISCV_IOMMU_PC_TA_PSCID, domain->pscid) |
 	     RISCV_IOMMU_PC_TA_V;
 
 	if (riscv_iommu_bond_link(domain, dev))
 		return -ENOMEM;
 
-	riscv_iommu_iodir_update(iommu, dev, fsc, ta);
+	riscv_iommu_iodir_update(iommu, dev, &dc);
 	riscv_iommu_bond_unlink(info->domain, dev);
 	info->domain = domain;
 
@@ -1378,9 +1378,12 @@ static int riscv_iommu_attach_blocking_domain(struct iommu_domain *iommu_domain,
 {
 	struct riscv_iommu_device *iommu = dev_to_iommu(dev);
 	struct riscv_iommu_info *info = dev_iommu_priv_get(dev);
+	struct riscv_iommu_dc dc = {0};
+
+	dc.fsc = RISCV_IOMMU_FSC_BARE;
 
 	/* Make device context invalid, translation requests will fault w/ #258 */
-	riscv_iommu_iodir_update(iommu, dev, RISCV_IOMMU_FSC_BARE, 0);
+	riscv_iommu_iodir_update(iommu, dev, &dc);
 	riscv_iommu_bond_unlink(info->domain, dev);
 	info->domain = NULL;
 
@@ -1400,8 +1403,12 @@ static int riscv_iommu_attach_identity_domain(struct iommu_domain *iommu_domain,
 {
 	struct riscv_iommu_device *iommu = dev_to_iommu(dev);
 	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;
 
-	riscv_iommu_iodir_update(iommu, dev, RISCV_IOMMU_FSC_BARE, RISCV_IOMMU_PC_TA_V);
+	riscv_iommu_iodir_update(iommu, dev, &dc);
 	riscv_iommu_bond_unlink(info->domain, dev);
 	info->domain = NULL;
 
Signature · 1 line
2.50.1
#10 lore raw

RFC PATCH v4 5/10 iommu/riscv: support GSCID and GVMA invalidation command

In reply to [email protected]

From: Zong Li <[email protected]>

This patch adds a ID Allocator for GSCID and a wrap for setting up GSCID in IOTLB invalidation command.

Set up iohgatp to enable second stage table and flush stage-2 table if the GSCID is set.

The GSCID of domain should be freed when release domain. GSCID will be allocated for parent domain in nested IOMMU process.

---
 drivers/iommu/riscv/iommu-bits.h |  7 ++++
 drivers/iommu/riscv/iommu.c      | 62 ++++++++++++++++++++++++++------
 2 files changed, 58 insertions(+), 11 deletions(-)
drivers/iommu/riscv/iommu-bits.h +7 -0
index f2ef9bd3cde9..65d1f2250011 100644
@@ -720,6 +720,13 @@ static inline void riscv_iommu_cmd_inval_vma(struct riscv_iommu_command *cmd)
 	cmd->dword1 = 0;
 }
 
+static inline void riscv_iommu_cmd_inval_gvma(struct riscv_iommu_command *cmd)
+{
+	cmd->dword0 = FIELD_PREP(RISCV_IOMMU_CMD0_OPCODE, RISCV_IOMMU_CMD_IOTINVAL_OPCODE) |
+		      FIELD_PREP(RISCV_IOMMU_CMD0_FUNC, RISCV_IOMMU_CMD_IOTINVAL_FUNC_GVMA);
+	cmd->dword1 = 0;
+}
+
 static inline void riscv_iommu_cmd_inval_set_addr(struct riscv_iommu_command *cmd,
 						  u64 addr)
 {
drivers/iommu/riscv/iommu.c +51 -11

124 lines not on this page · raw message

Signature · 1 line
2.50.1
#12 lore raw

RFC PATCH v4 6/10 RISC-V: KVM: Enable KVM_VFIO interfaces on RISC-V arch

In reply to [email protected]

From: Tomasz Jeznach <[email protected]>

Enable KVM/VFIO support on RISC-V architecture.

---
 arch/riscv/kvm/Kconfig | 2 ++
 1 file changed, 2 insertions(+)
arch/riscv/kvm/Kconfig +2 -0
index ec2cee0a39e0..54ee90f010ef 100644
@@ -30,8 +30,10 @@ config KVM
 	select KVM_GENERIC_HARDWARE_ENABLING
 	select KVM_MMIO
 	select VIRT_XFER_TO_GUEST_WORK
+	select KVM_VFIO
 	select SCHED_INFO
 	select GUEST_PERF_EVENTS if PERF_EVENTS
+	select SRCU
 	help
 	  Support hosting virtualized guest machines.
 
Signature · 1 line
2.50.1
#14 lore raw

RFC PATCH v4 7/10 iommu/riscv: Add domain_alloc_paging_flags for second-stage domain

In reply to [email protected]

From: Fangyu Yu <[email protected]>

Replace .domain_alloc_paging with .domain_alloc_paging_flags so callers can pass allocation flags to select the appropriate page-table type.

When IOMMU_HWPT_ALLOC_NEST_PARENT or IOMMU_HWPT_ALLOC_DIRTY_TRACKING is set in @flags, allocate a second-stage (iohgatp) domain.

When @flags is 0 the behaviour is identical to the previous
domain_alloc_paging: first-stage (iosatp) domain.

---
 drivers/iommu/riscv/iommu.c | 94 +++++++++++++++++++++++++++----------
 1 file changed, 69 insertions(+), 25 deletions(-)
drivers/iommu/riscv/iommu.c +69 -25
index 99f05f1db35b..16779877351b 100644
@@ -1358,25 +1358,21 @@ static const struct iommu_domain_ops riscv_iommu_paging_domain_ops = {
 	.flush_iotlb_all = riscv_iommu_iotlb_flush_all,
 };
 
-static struct iommu_domain *riscv_iommu_alloc_paging_domain(struct device *dev)
+static struct iommu_domain *
+riscv_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags,
+				      const struct iommu_user_data *user_data)
 {
 	struct pt_iommu_riscv_64_cfg cfg = {};
 	struct riscv_iommu_domain *domain;
 	struct riscv_iommu_device *iommu;
 	int ret;
+	const u32 supported_flags = IOMMU_HWPT_ALLOC_DIRTY_TRACKING |
+				    IOMMU_HWPT_ALLOC_NEST_PARENT;
 
-	iommu = dev_to_iommu(dev);
-	if (iommu->caps & RISCV_IOMMU_CAPABILITIES_SV57) {
-		cfg.common.hw_max_vasz_lg2 = 57;
-	} else if (iommu->caps & RISCV_IOMMU_CAPABILITIES_SV48) {
-		cfg.common.hw_max_vasz_lg2 = 48;
-	} else if (iommu->caps & RISCV_IOMMU_CAPABILITIES_SV39) {
-		cfg.common.hw_max_vasz_lg2 = 39;
-	} else {
-		dev_err(dev, "cannot find supported page table mode\n");
-		return ERR_PTR(-ENODEV);
-	}
-	cfg.common.hw_max_oasz_lg2 = 56;
+	if (flags & ~supported_flags)
+		return ERR_PTR(-EOPNOTSUPP);
+	if (user_data)
+		return ERR_PTR(-EOPNOTSUPP);
 
 	domain = kzalloc_obj(*domain);
 	if (!domain)
@@ -1384,12 +1380,13 @@ static struct iommu_domain *riscv_iommu_alloc_paging_domain(struct device *dev)
 
 	INIT_LIST_HEAD_RCU(&domain->bonds);
 	spin_lock_init(&domain->lock);
+	iommu = dev_to_iommu(dev);
+	cfg.common.hw_max_oasz_lg2 = 56;
 	/*
 	 * 6.4 IOMMU capabilities [..] IOMMU implementations must support the
 	 * Svnapot standard extension for NAPOT Translation Contiguity.
 	 */
-	cfg.common.features = BIT(PT_FEAT_SIGN_EXTEND) |
-			      BIT(PT_FEAT_FLUSH_RANGE) |
+	cfg.common.features = BIT(PT_FEAT_FLUSH_RANGE) |
 			      BIT(PT_FEAT_RISCV_SVNAPOT_64K) |
 			      BIT(PT_FEAT_DETAILED_GATHER);
 	if (iommu->caps & RISCV_IOMMU_CAPABILITIES_SVPBMT)
@@ -1397,19 +1394,66 @@ static struct iommu_domain *riscv_iommu_alloc_paging_domain(struct device *dev)
 	domain->riscvpt.iommu.nid = dev_to_node(iommu->dev);
 	domain->domain.ops = &riscv_iommu_paging_domain_ops;
 
-	domain->pscid = ida_alloc_range(&riscv_iommu_pscids, 1,
-					RISCV_IOMMU_MAX_PSCID, GFP_KERNEL);
-	if (domain->pscid < 0) {
-		riscv_iommu_free_paging_domain(&domain->domain);
-		return ERR_PTR(-ENOMEM);
+	switch (flags) {
+	case 0:
+		cfg.common.features |= BIT(PT_FEAT_SIGN_EXTEND);
+		if (iommu->caps & RISCV_IOMMU_CAPABILITIES_SV57) {
+			cfg.common.hw_max_vasz_lg2 = 57;
+		} else if (iommu->caps & RISCV_IOMMU_CAPABILITIES_SV48) {
+			cfg.common.hw_max_vasz_lg2 = 48;
+		} else if (iommu->caps & RISCV_IOMMU_CAPABILITIES_SV39) {
+			cfg.common.hw_max_vasz_lg2 = 39;
+		} else {
+			ret = -ENODEV;
+			goto err_free;
+		}
+		domain->pscid = ida_alloc_range(&riscv_iommu_pscids, 1,
+						RISCV_IOMMU_MAX_PSCID, GFP_KERNEL);
+		if (domain->pscid < 0) {
+			ret = -ENOMEM;
+			goto err_free;
+		}
+		break;
+	case IOMMU_HWPT_ALLOC_NEST_PARENT:
+	case IOMMU_HWPT_ALLOC_DIRTY_TRACKING:
+	case IOMMU_HWPT_ALLOC_DIRTY_TRACKING | IOMMU_HWPT_ALLOC_NEST_PARENT:
+		/*
+		 * Second-stage (iohgatp) page table for KVM VFIO device
+		 * pass-through and dirty tracking. The GPA space is 2 bits
+		 * wider than the corresponding first-stage VA space (x4 root
+		 * page table), so hw_max_vasz_lg2 values are 41/50/59.
+		 */
+		if (iommu->caps & RISCV_IOMMU_CAPABILITIES_SV57X4) {
+			cfg.common.hw_max_vasz_lg2 = 59;
+		} else if (iommu->caps & RISCV_IOMMU_CAPABILITIES_SV48X4) {
+			cfg.common.hw_max_vasz_lg2 = 50;
+		} else if (iommu->caps & RISCV_IOMMU_CAPABILITIES_SV39X4) {
+			cfg.common.hw_max_vasz_lg2 = 41;
+		} else {
+			ret = -ENODEV;
+			goto err_free;
+		}
+		domain->gscid = ida_alloc_range(&riscv_iommu_gscids, 1,
+						RISCV_IOMMU_MAX_GSCID, GFP_KERNEL);
+		if (domain->gscid < 0) {
+			ret = -ENOMEM;
+			goto err_free;
+		}
+		cfg.common.features |= BIT(PT_FEAT_RISCV_S2);
+		break;
+	default:
+		ret = -EOPNOTSUPP;
+		goto err_free;
 	}
 
 	ret = pt_iommu_riscv_64_init(&domain->riscvpt, &cfg, GFP_KERNEL);
-	if (ret) {
-		riscv_iommu_free_paging_domain(&domain->domain);
-		return ERR_PTR(ret);
-	}
+	if (ret)
+		goto err_free;
 	return &domain->domain;
+
+err_free:
+	riscv_iommu_free_paging_domain(&domain->domain);
+	return ERR_PTR(ret);
 }
 
 static int riscv_iommu_attach_blocking_domain(struct iommu_domain *iommu_domain,
@@ -1544,7 +1588,7 @@ static const struct iommu_ops riscv_iommu_ops = {
 	.identity_domain = &riscv_iommu_identity_domain,
 	.blocked_domain = &riscv_iommu_blocking_domain,
 	.release_domain = &riscv_iommu_blocking_domain,
-	.domain_alloc_paging = riscv_iommu_alloc_paging_domain,
+	.domain_alloc_paging_flags = riscv_iommu_domain_alloc_paging_flags,
 	.device_group = riscv_iommu_device_group,
 	.probe_device = riscv_iommu_probe_device,
 	.release_device	= riscv_iommu_release_device,
Signature · 1 line
2.50.1
#15 lore raw

RFC PATCH v4 8/10 iommu/riscv: Pre-enable GADE for second-stage domains

In reply to [email protected]

From: Fangyu Yu <[email protected]>

Pre-enable RISCV_IOMMU_DC_TC_GADE in the device context when attaching a second-stage domain, if the IOMMU supports AMO_HWAD.

Software pre-populates second-stage page tables with D set, so enabling GADE by default does not change normal behavior. When dirty tracking is enabled, iommufd clears the pre-set D bits and GADE becomes necessary for hardware to update the dirty bit on write access.

This avoids toggling GADE dynamically and keeps device context setup consistent with second-stage domain attachment.

---
 drivers/iommu/riscv/iommu.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iommu/riscv/iommu.c +2 -0
index 16779877351b..f01fd4e2f840 100644
@@ -1330,6 +1330,8 @@ static int riscv_iommu_attach_paging_domain(struct iommu_domain *iommu_domain,
 			FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_MODE, pt_info.iohgatp_mode) |
 			FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_GSCID, domain->gscid) |
 			FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_PPN, pt_info.ppn);
+		if (iommu->caps & RISCV_IOMMU_CAPABILITIES_AMO_HWAD)
+			dc.tc |= RISCV_IOMMU_DC_TC_GADE;
 	} else {
 		if (!riscv_iommu_fsc_supported(iommu, pt_info.fsc_iosatp_mode))
 			return -ENODEV;
Signature · 1 line
2.50.1
#20 lore raw

RFC PATCH v4 9/10 iommu/riscv: Add dirty tracking support for second-stage domains

In reply to [email protected]

From: Fangyu Yu <[email protected]>

Add hardware dirty tracking support for second-stage (iohgatp) domains used in KVM VFIO device pass-through.

The RISC-V IOMMU can automatically set the dirty bit in PTEs on write access when DC.tc.GADE is set and the hardware has AMO_HWAD capability. Wire this up to the iommufd dirty tracking interface:

- riscv_iommu_set_dirty_tracking(): Always enabled dirty tracking for second-stage domain.

- riscv_iommu_dirty_ops: Exposes set_dirty_tracking and the generic page-table read_and_clear_dirty via IOMMU_PT_DIRTY_OPS(riscv_64).

- domain_alloc_paging_flags: Assigns dirty_ops to second-stage domains when AMO_HWAD is advertised in hardware capabilities.

- riscv_iommu_capable: Reports IOMMU_CAP_DIRTY_TRACKING when AMO_HWAD is present.

---
 drivers/iommu/riscv/iommu.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
drivers/iommu/riscv/iommu.c +21 -0
index f01fd4e2f840..41b593def1f5 100644
@@ -1352,6 +1352,21 @@ static int riscv_iommu_attach_paging_domain(struct iommu_domain *iommu_domain,
 	return 0;
 }
 
+static int riscv_iommu_set_dirty_tracking(struct iommu_domain *iommu_domain,
+					  bool enable)
+{
+	/*
+	 * Always enabled and the dirty bitmap is cleared prior to
+	 * set_dirty_tracking().
+	 */
+	return 0;
+}
+
+static const struct iommu_dirty_ops riscv_iommu_dirty_ops = {
+	IOMMU_PT_DIRTY_OPS(riscv_64),
+	.set_dirty_tracking = riscv_iommu_set_dirty_tracking,
+};
+
 static const struct iommu_domain_ops riscv_iommu_paging_domain_ops = {
 	IOMMU_PT_DOMAIN_OPS(riscv_64),
 	.attach_dev = riscv_iommu_attach_paging_domain,
@@ -1442,6 +1457,8 @@ riscv_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags,
 			goto err_free;
 		}
 		cfg.common.features |= BIT(PT_FEAT_RISCV_S2);
+		if (iommu->caps & RISCV_IOMMU_CAPABILITIES_AMO_HWAD)
+			domain->domain.dirty_ops = &riscv_iommu_dirty_ops;
 		break;
 	default:
 		ret = -EOPNOTSUPP;
@@ -1517,9 +1534,13 @@ static struct iommu_group *riscv_iommu_device_group(struct device *dev)
 
 static bool riscv_iommu_capable(struct device *dev, enum iommu_cap cap)
 {
+	struct riscv_iommu_device *iommu = dev_to_iommu(dev);
+
 	switch (cap) {
 	case IOMMU_CAP_CACHE_COHERENCY:
 		return true;
+	case IOMMU_CAP_DIRTY_TRACKING:
+		return !!(iommu->caps & RISCV_IOMMU_CAPABILITIES_AMO_HWAD);
 	default:
 		return false;
 	}
Signature · 1 line
2.50.1
#21 lore raw
In reply to [email protected]
Quoted 84 lines · On 9/15/2026 11:28 AM, [email protected] wrote:

From: Fangyu Yu <[email protected]>

The RISC-V IOMMU architecture defines an AMO_HWAD capability (Hardware Access/Dirty update) that allows the IOMMU to atomically set the A/D bits in second-stage PTEs on DMA access. When DC.tc.GADE is asserted, the IOMMU autonomously sets D on the first write to a page mapped by an iohgatp domain. This series wires that capability up to the iommufd dirty-tracking interface (IOMMU_HWPT_SET_DIRTY_TRACKING / IOMMU_HWPT_GET_DIRTY_BITMAP) and reports IOMMU_CAP_DIRTY_TRACKING.

Design notes
------------

* The feature is scoped to second-stage (iohgatp) domains only; these are the domains created for KVM / VFIO device pass-through when userspace allocates an HWPT with IOMMU_HWPT_ALLOC_NEST_PARENT or IOMMU_HWPT_ALLOC_DIRTY_TRACKING. First-stage (iosatp) domains are not touched by this series.

* The page-table side plugs into the existing generic_pt dirty hook framework (amdv1 / vtdss style). RISC-V adds the three required PTE ops – is_write_dirty / make_write_clean / make_write_dirty.

Testing
-------

* Test on QEMU RISC-V, a nvme and an e1000e device was passed through to an L2 guest via vfio-pci + iommufd.

* generic_pt KUnit: the existing test_dirty case now runs and passes for the RISC-V 64-bit format.

Follow-up work
--------------
* Build a dedicated end-to-end test case that drives the full flow (HWPT_ALLOC with DIRTY_TRACKING -> attach -> IOAS_MAP -> generate real DMA -> SET_DIRTY_TRACKING -> GET_DIRTY_BITMAP -> verify bitmap against expected IOVA footprint) so that the behaviour can be regression-tested beyond the KUnit PTE-level coverage.

* If possible, rebase and retest on top of the updated "iommu irqbypass" patchset.

---
Changes in v4 (Jason's suggestions):
- Rebased the series on top of [1] and [2].
- Drop the RISC-V-specific pt_num_items_lg2() top-level special case.
- Validate page-table address widths by translation stage: accept only Sv39/Sv48/Sv57 widths for first-stage tables and only Sv39x4/Sv48x4/ Sv57x4 widths for second-stage tables.
- Replace the aliased first-stage/second-stage MODE union with independent FSC/IOSATP and IOHGATP MODE fields.
- Add pt_dirty_supported() to restrict RISC-V generic_pt dirty tracking to second-stage tables.
- Link to v3:
https://lore.kernel.org/linux-riscv/[email protected]/
Changes in v3 (Andrew Jones's suggestions):
- Rebased the series on top of Andrew Jones' generic_pt RISC-V supported feature mask fix, which adds PT_FEAT_RISCV_SVPBMT to the supported feature set.
- Added PT_FEAT_RISCV_S2 to the RISC-V generic_pt supported feature mask and KUnit feature matrix.
- Kept PT_FEAT_SIGN_EXTEND only for first-stage RISC-V KUnit configs; second stage configs now use PT_FEAT_RISCV_S2 without sign-extension semantics.
- Split RISC-V IOMMU capability checks into first-stage FSC and second-stage IOHGATP helpers.
- Updated second-stage paging domain setup to use GPA widths 41/50/59 and to avoid PT_FEAT_SIGN_EXTEND.
- Link to v2:
https://lore.kernel.org/linux-riscv/[email protected]/
Changes in v2 (Jason's suggestions):
- Introduced a single PT_FEAT_RISCV_S2: second-stage selection is driven purely by this feature bit.
- Switched from dynamic DC.tc.GADE toggling to static pre-enable.
- domain_alloc_paging_flags: follow the switch/case design from other drivers.
- Drop IOMMU_CAP_DEFERRED_FLUSH in riscv_iommu_capable.
- Remove the .hw_info-related patch.
- Link to v1:
https://lore.kernel.org/linux-riscv/[email protected]/

[1] https://lore.kernel.org/linux-iommu/[email protected]/
[2] https://lore.kernel.org/linux-iommu/[email protected]/

Quoted 7 lines

Fangyu Yu (6):
iommupt: Add RISC-V Second-stage (iohgatp) page table support
iommupt: Add RISC-V dirty tracking PTE ops
iommu/riscv: Add domain_alloc_paging_flags for second-stage domain iommu/riscv: Pre-enable GADE for second-stage domainsgon iommu/riscv: Add dirty tracking support for second-stage domains iommu/riscv: Add IOTINVAL.GVMA after updating DDT/PDT entries

Hi Fangyu,

This patch (10/10) is missing from this series.
It was also missing in RFC v3.

Thanks,
Shuai

Quoted 18 lines

Tomasz Jeznach (2):
iommu/riscv: report iommu capabilities
RISC-V: KVM: Enable KVM_VFIO interfaces on RISC-V arch

Zong Li (2):
iommu/riscv: use data structure instead of individual values iommu/riscv: support GSCID and GVMA invalidation command

  arch/riscv/kvm/Kconfig                       |   2 +
  drivers/iommu/generic_pt/fmt/iommu_riscv64.c |   2 +-
  drivers/iommu/generic_pt/fmt/riscv.h         | 158 +++++++++++--
  drivers/iommu/riscv/iommu-bits.h             |   7 +
  drivers/iommu/riscv/iommu.c                  | 224 +++++++++++++++----
  include/linux/generic_pt/common.h            |   4 +
  include/linux/generic_pt/iommu.h             |  11 +
  7 files changed, 336 insertions(+), 72 deletions(-)
#22 lore raw
Quoted 92 lines
Quoted 84 lines

From: Fangyu Yu <[email protected]>

The RISC-V IOMMU architecture defines an AMO_HWAD capability (Hardware Access/Dirty update) that allows the IOMMU to atomically set the A/D bits in second-stage PTEs on DMA access. When DC.tc.GADE is asserted, the IOMMU autonomously sets D on the first write to a page mapped by an iohgatp domain. This series wires that capability up to the iommufd dirty-tracking interface (IOMMU_HWPT_SET_DIRTY_TRACKING / IOMMU_HWPT_GET_DIRTY_BITMAP) and reports IOMMU_CAP_DIRTY_TRACKING.

Design notes
------------

* The feature is scoped to second-stage (iohgatp) domains only; these are the domains created for KVM / VFIO device pass-through when userspace allocates an HWPT with IOMMU_HWPT_ALLOC_NEST_PARENT or IOMMU_HWPT_ALLOC_DIRTY_TRACKING. First-stage (iosatp) domains are not touched by this series.

* The page-table side plugs into the existing generic_pt dirty hook framework (amdv1 / vtdss style). RISC-V adds the three required PTE ops – is_write_dirty / make_write_clean / make_write_dirty.

Testing
-------

* Test on QEMU RISC-V, a nvme and an e1000e device was passed through to an L2 guest via vfio-pci + iommufd.

* generic_pt KUnit: the existing test_dirty case now runs and passes for the RISC-V 64-bit format.

Follow-up work
--------------
* Build a dedicated end-to-end test case that drives the full flow (HWPT_ALLOC with DIRTY_TRACKING -> attach -> IOAS_MAP -> generate real DMA -> SET_DIRTY_TRACKING -> GET_DIRTY_BITMAP -> verify bitmap against expected IOVA footprint) so that the behaviour can be regression-tested beyond the KUnit PTE-level coverage.

* If possible, rebase and retest on top of the updated "iommu irqbypass" patchset.

---
Changes in v4 (Jason's suggestions):
- Rebased the series on top of [1] and [2].
- Drop the RISC-V-specific pt_num_items_lg2() top-level special case.
- Validate page-table address widths by translation stage: accept only Sv39/Sv48/Sv57 widths for first-stage tables and only Sv39x4/Sv48x4/ Sv57x4 widths for second-stage tables.
- Replace the aliased first-stage/second-stage MODE union with independent FSC/IOSATP and IOHGATP MODE fields.
- Add pt_dirty_supported() to restrict RISC-V generic_pt dirty tracking to second-stage tables.
- Link to v3:
https://lore.kernel.org/linux-riscv/[email protected]/
Changes in v3 (Andrew Jones's suggestions):
- Rebased the series on top of Andrew Jones' generic_pt RISC-V supported feature mask fix, which adds PT_FEAT_RISCV_SVPBMT to the supported feature set.
- Added PT_FEAT_RISCV_S2 to the RISC-V generic_pt supported feature mask and KUnit feature matrix.
- Kept PT_FEAT_SIGN_EXTEND only for first-stage RISC-V KUnit configs; second stage configs now use PT_FEAT_RISCV_S2 without sign-extension semantics.
- Split RISC-V IOMMU capability checks into first-stage FSC and second-stage IOHGATP helpers.
- Updated second-stage paging domain setup to use GPA widths 41/50/59 and to avoid PT_FEAT_SIGN_EXTEND.
- Link to v2:
https://lore.kernel.org/linux-riscv/[email protected]/
Changes in v2 (Jason's suggestions):
- Introduced a single PT_FEAT_RISCV_S2: second-stage selection is driven purely by this feature bit.
- Switched from dynamic DC.tc.GADE toggling to static pre-enable.
- domain_alloc_paging_flags: follow the switch/case design from other drivers.
- Drop IOMMU_CAP_DEFERRED_FLUSH in riscv_iommu_capable.
- Remove the .hw_info-related patch.
- Link to v1:
https://lore.kernel.org/linux-riscv/[email protected]/

[1] https://lore.kernel.org/linux-iommu/[email protected]/
[2] https://lore.kernel.org/linux-iommu/[email protected]/

Quoted 7 lines

Fangyu Yu (6):
iommupt: Add RISC-V Second-stage (iohgatp) page table support
iommupt: Add RISC-V dirty tracking PTE ops
iommu/riscv: Add domain_alloc_paging_flags for second-stage domain iommu/riscv: Pre-enable GADE for second-stage domainsgon iommu/riscv: Add dirty tracking support for second-stage domains iommu/riscv: Add IOTINVAL.GVMA after updating DDT/PDT entries

Quoted 5 lines

Hi Fangyu,

This patch (10/10) is missing from this series.
It was also missing in RFC v3.

Hi Shuai,

Thanks for catching that. You’re right — I missed patch 10/10, and it was indeed omitted from the series.
I’ll resend it.

Thanks,
Fangyu

Quoted 21 lines

Thanks,
Shuai

Quoted 18 lines

Tomasz Jeznach (2):
iommu/riscv: report iommu capabilities
RISC-V: KVM: Enable KVM_VFIO interfaces on RISC-V arch

Zong Li (2):
iommu/riscv: use data structure instead of individual values iommu/riscv: support GSCID and GVMA invalidation command

  arch/riscv/kvm/Kconfig                       |   2 +
  drivers/iommu/generic_pt/fmt/iommu_riscv64.c |   2 +-
  drivers/iommu/generic_pt/fmt/riscv.h         | 158 +++++++++++--
  drivers/iommu/riscv/iommu-bits.h             |   7 +
  drivers/iommu/riscv/iommu.c                  | 224 +++++++++++++++----
  include/linux/generic_pt/common.h            |   4 +
  include/linux/generic_pt/iommu.h             |  11 +
  7 files changed, 336 insertions(+), 72 deletions(-)
#23 lore raw

RFC PATCH v4 10/10 iommu/riscv: Add IOTINVAL.GVMA after updating DDT/PDT entries

In reply to [email protected]

From: Fangyu Yu <[email protected]>

Previously, only IOTINVAL.VMA was issued, which is insufficient for second-stage address translation consistency.

---
 drivers/iommu/riscv/iommu.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/iommu/riscv/iommu.c +6 -3
index 41b593def1f5..e848a7f97771 100644
@@ -1153,12 +1153,15 @@ static void riscv_iommu_iodir_iotinval(struct riscv_iommu_device *iommu,
 		/*
 		 * else: IOTINVAL.VMA with GV=1,AV=PSCV=0,and
 		 * GSCID=DC.iohgatp.GSCID
-		 *
+		 */
+		riscv_iommu_cmd_send(iommu, &cmd);
+		/*
 		 * IOTINVAL.GVMA with GV=1,AV=0,and
 		 * GSCID=DC.iohgatp.GSCID
-		 * TODO: For now, the Second-Stage feature have not yet been merged,
-		 * also issue IOTINVAL.GVMA once second-stage support is merged.
 		 */
+		riscv_iommu_cmd_inval_gvma(&cmd);
+		riscv_iommu_cmd_inval_set_gscid(&cmd,
+			FIELD_GET(RISCV_IOMMU_DC_IOHGATP_GSCID, iohgatp));
 	}
 	riscv_iommu_cmd_send(iommu, &cmd);
 }
Signature · 1 line
2.50.1
Reviewed-by on v4 3/10
#4 lore raw

Re: [RFC PATCH v4 03/10] iommu/riscv: report iommu capabilities

In reply to [email protected]
Quoted 7 lines · On 9/15/2026 11:28 AM, [email protected] wrote:

From: Tomasz Jeznach <[email protected]>

Report RISC-V IOMMU capabilities required by VFIO subsystem to enable PCIe device assignment.

Thanks,
Nutty

Quoted 33 lines
---
  drivers/iommu/riscv/iommu.c | 11 +++++++++++
  1 file changed, 11 insertions(+)
drivers/iommu/riscv/iommu.c +11 -0
index cec3ddd7ab10..a6c8307e82ea 100644
@@ -1422,6 +1422,16 @@ static struct iommu_group *riscv_iommu_device_group(struct device *dev)
  	return generic_device_group(dev);
  }
 
+static bool riscv_iommu_capable(struct device *dev, enum iommu_cap cap)
+{
+	switch (cap) {
+	case IOMMU_CAP_CACHE_COHERENCY:
+		return true;
+	default:
+		return false;
+	}
+}
+
  static int riscv_iommu_of_xlate(struct device *dev, const struct of_phandle_args *args)
  {
  	return iommu_fwspec_add_ids(dev, args->args, 1);
@@ -1483,6 +1493,7 @@ static void riscv_iommu_release_device(struct device *dev)
 
  static const struct iommu_ops riscv_iommu_ops = {
  	.of_xlate = riscv_iommu_of_xlate,
+	.capable = riscv_iommu_capable,
  	.identity_domain = &riscv_iommu_identity_domain,
  	.blocked_domain = &riscv_iommu_blocking_domain,
  	.release_domain = &riscv_iommu_blocking_domain,
Reviewed-by on v4 4/10
#6 lore raw

Re: [RFC PATCH v4 04/10] iommu/riscv: use data structure instead of individual values

In reply to [email protected]
Quoted 8 lines · On 9/15/2026 11:28 AM, [email protected] wrote:

From: Zong Li <[email protected]>

The parameter will be increased when we need to set up more bit fields in the device context. Use a data structure to wrap them up.

Thanks,
Nutty

Quoted 86 lines
---
  drivers/iommu/riscv/iommu.c | 27 +++++++++++++++++----------
  1 file changed, 17 insertions(+), 10 deletions(-)
drivers/iommu/riscv/iommu.c +17 -10
index a6c8307e82ea..c5b6214ee672 100644
@@ -1165,7 +1165,7 @@ static void riscv_iommu_iodir_iotinval(struct riscv_iommu_device *iommu,
   * interim translation faults.
   */
  static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,
-				     struct device *dev, u64 fsc, u64 ta)
+				     struct device *dev, struct riscv_iommu_dc *new_dc)
  {
  	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  	struct riscv_iommu_dc *dc;
@@ -1204,10 +1204,10 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,
  	for (i = 0; i < fwspec->num_ids; i++) {
  		dc = riscv_iommu_get_dc(iommu, fwspec->ids[i]);
  		tc = READ_ONCE(dc->tc);
-		tc |= ta & RISCV_IOMMU_DC_TC_V;
+		tc |= new_dc->ta & RISCV_IOMMU_DC_TC_V;
 
-		WRITE_ONCE(dc->fsc, fsc);
-		WRITE_ONCE(dc->ta, ta & RISCV_IOMMU_PC_TA_PSCID);
+		WRITE_ONCE(dc->fsc, new_dc->fsc);
+		WRITE_ONCE(dc->ta, new_dc->ta & RISCV_IOMMU_PC_TA_PSCID);
  		/* Update device context, write TC.V as the last step. */
  		dma_wmb();
  		WRITE_ONCE(dc->tc, tc);
@@ -1288,22 +1288,22 @@ static int riscv_iommu_attach_paging_domain(struct iommu_domain *iommu_domain,
  	struct riscv_iommu_device *iommu = dev_to_iommu(dev);
  	struct riscv_iommu_info *info = dev_iommu_priv_get(dev);
  	struct pt_iommu_riscv_64_hw_info pt_info;
-	u64 fsc, ta;
+	struct riscv_iommu_dc dc = {0};
 
  	pt_iommu_riscv_64_hw_info(&domain->riscvpt, &pt_info);
 
  	if (!riscv_iommu_pt_supported(iommu, pt_info.fsc_iosatp_mode))
  		return -ENODEV;
 
-	fsc = FIELD_PREP(RISCV_IOMMU_PC_FSC_MODE, pt_info.fsc_iosatp_mode) |
+	dc.fsc = FIELD_PREP(RISCV_IOMMU_PC_FSC_MODE, pt_info.fsc_iosatp_mode) |
  	      FIELD_PREP(RISCV_IOMMU_PC_FSC_PPN, pt_info.ppn);
-	ta = FIELD_PREP(RISCV_IOMMU_PC_TA_PSCID, domain->pscid) |
+	dc.ta = FIELD_PREP(RISCV_IOMMU_PC_TA_PSCID, domain->pscid) |
  	     RISCV_IOMMU_PC_TA_V;
 
  	if (riscv_iommu_bond_link(domain, dev))
  		return -ENOMEM;
 
-	riscv_iommu_iodir_update(iommu, dev, fsc, ta);
+	riscv_iommu_iodir_update(iommu, dev, &dc);
  	riscv_iommu_bond_unlink(info->domain, dev);
  	info->domain = domain;
 
@@ -1378,9 +1378,12 @@ static int riscv_iommu_attach_blocking_domain(struct iommu_domain *iommu_domain,
  {
  	struct riscv_iommu_device *iommu = dev_to_iommu(dev);
  	struct riscv_iommu_info *info = dev_iommu_priv_get(dev);
+	struct riscv_iommu_dc dc = {0};
+
+	dc.fsc = RISCV_IOMMU_FSC_BARE;
 
  	/* Make device context invalid, translation requests will fault w/ #258 */
-	riscv_iommu_iodir_update(iommu, dev, RISCV_IOMMU_FSC_BARE, 0);
+	riscv_iommu_iodir_update(iommu, dev, &dc);
  	riscv_iommu_bond_unlink(info->domain, dev);
  	info->domain = NULL;
 
@@ -1400,8 +1403,12 @@ static int riscv_iommu_attach_identity_domain(struct iommu_domain *iommu_domain,
  {
  	struct riscv_iommu_device *iommu = dev_to_iommu(dev);
  	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;
 
-	riscv_iommu_iodir_update(iommu, dev, RISCV_IOMMU_FSC_BARE, RISCV_IOMMU_PC_TA_V);
+	riscv_iommu_iodir_update(iommu, dev, &dc);
  	riscv_iommu_bond_unlink(info->domain, dev);
  	info->domain = NULL;
 
Reviewed-by on v4 5/10
#11 lore raw

Re: [RFC PATCH v4 05/10] iommu/riscv: support GSCID and GVMA invalidation command

In reply to [email protected]
Quoted 5 lines · On 9/15/2026 11:28 AM, [email protected] wrote:

From: Zong Li <[email protected]>

This patch adds a ID Allocator for GSCID and a wrap for setting up GSCID in IOTLB invalidation command.

Quoted 8 lines

Set up iohgatp to enable second stage table and flush stage-2 table if the GSCID is set.

The GSCID of domain should be freed when release domain. GSCID will be allocated for parent domain in nested IOMMU process.

Thanks,
Nutty

Quoted 160 lines
---
  drivers/iommu/riscv/iommu-bits.h |  7 ++++
  drivers/iommu/riscv/iommu.c      | 62 ++++++++++++++++++++++++++------
  2 files changed, 58 insertions(+), 11 deletions(-)
drivers/iommu/riscv/iommu-bits.h +7 -0
index f2ef9bd3cde9..65d1f2250011 100644
@@ -720,6 +720,13 @@ static inline void riscv_iommu_cmd_inval_vma(struct riscv_iommu_command *cmd)
  	cmd->dword1 = 0;
  }
 
+static inline void riscv_iommu_cmd_inval_gvma(struct riscv_iommu_command *cmd)
+{
+	cmd->dword0 = FIELD_PREP(RISCV_IOMMU_CMD0_OPCODE, RISCV_IOMMU_CMD_IOTINVAL_OPCODE) |
+		      FIELD_PREP(RISCV_IOMMU_CMD0_FUNC, RISCV_IOMMU_CMD_IOTINVAL_FUNC_GVMA);
+	cmd->dword1 = 0;
+}
+
  static inline void riscv_iommu_cmd_inval_set_addr(struct riscv_iommu_command *cmd,
  						  u64 addr)
  {
drivers/iommu/riscv/iommu.c +51 -11
index c5b6214ee672..99f05f1db35b 100644
@@ -48,6 +48,10 @@
  static DEFINE_IDA(riscv_iommu_pscids);
  #define RISCV_IOMMU_MAX_PSCID		(BIT(20) - 1)
 
+/* IOMMU GSCID allocation namespace. */
+static DEFINE_IDA(riscv_iommu_gscids);
+#define RISCV_IOMMU_MAX_GSCID		(BIT(16) - 1)
+
  /* Device resource-managed allocations */
  struct riscv_iommu_devres {
  	void *addr;
@@ -819,6 +823,7 @@ struct riscv_iommu_domain {
  	struct list_head bonds;
  	spinlock_t lock;		/* protect bonds list updates. */
  	int pscid;
+	int gscid;
  };
  PT_IOMMU_CHECK_DOMAIN(struct riscv_iommu_domain, riscvpt.iommu, domain);
 
@@ -981,7 +986,7 @@ static void riscv_iommu_tlbi_calc(struct riscv_iommu_tlbi *tlbi,
  }
 
  static void riscv_iommu_iotlb_inval_iommu(struct riscv_iommu_device *iommu,
-					  int pscid,
+					  int pscid, int gscid,
  					  struct riscv_iommu_tlbi *tlbi)
  {
  	bool use_nl = tlbi->non_leaf &&
@@ -989,8 +994,13 @@ static void riscv_iommu_iotlb_inval_iommu(struct riscv_iommu_device *iommu,
  	struct riscv_iommu_command cmd;
  	unsigned int i;
 
-	riscv_iommu_cmd_inval_vma(&cmd);
-	riscv_iommu_cmd_inval_set_pscid(&cmd, 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, pscid);
+	}
 
  	/*
  	 * If non-leaf entries were changed and the IOMMU doesn't
@@ -1074,7 +1084,7 @@ static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain,
  		if (iommu == prev)
  			continue;
 
-		riscv_iommu_iotlb_inval_iommu(iommu, domain->pscid, &tlbi);
+		riscv_iommu_iotlb_inval_iommu(iommu, domain->pscid, domain->gscid, &tlbi);
  		prev = iommu;
  	}
 
@@ -1208,6 +1218,7 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,
 
  		WRITE_ONCE(dc->fsc, new_dc->fsc);
  		WRITE_ONCE(dc->ta, new_dc->ta & RISCV_IOMMU_PC_TA_PSCID);
+		WRITE_ONCE(dc->iohgatp, new_dc->iohgatp);
  		/* Update device context, write TC.V as the last step. */
  		dma_wmb();
  		WRITE_ONCE(dc->tc, tc);
@@ -1258,16 +1269,19 @@ static void riscv_iommu_free_paging_domain(struct iommu_domain *iommu_domain)
 
  	WARN_ON(!list_empty(&domain->bonds));
 
-	if ((int)domain->pscid > 0)
+	if (domain->pscid > 0)
  		ida_free(&riscv_iommu_pscids, domain->pscid);
+	if (domain->gscid > 0)
+		ida_free(&riscv_iommu_gscids, domain->gscid);
 
  	pt_iommu_deinit(&domain->riscvpt.iommu);
  	kfree(domain);
  }
 
-static bool riscv_iommu_pt_supported(struct riscv_iommu_device *iommu, int pgd_mode)
+static bool riscv_iommu_fsc_supported(struct riscv_iommu_device *iommu,
+				      int mode)
  {
-	switch (pgd_mode) {
+	switch (mode) {
  	case RISCV_IOMMU_DC_FSC_IOSATP_MODE_SV39:
  		return iommu->caps & RISCV_IOMMU_CAPABILITIES_SV39;
 
@@ -1280,6 +1294,22 @@ static bool riscv_iommu_pt_supported(struct riscv_iommu_device *iommu, int pgd_m
  	return false;
  }
 
+static bool riscv_iommu_iohgatp_supported(struct riscv_iommu_device *iommu,
+					  int mode)
+{
+	switch (mode) {
+	case RISCV_IOMMU_DC_IOHGATP_MODE_SV39X4:
+		return iommu->caps & RISCV_IOMMU_CAPABILITIES_SV39X4;
+
+	case RISCV_IOMMU_DC_IOHGATP_MODE_SV48X4:
+		return iommu->caps & RISCV_IOMMU_CAPABILITIES_SV48X4;
+
+	case RISCV_IOMMU_DC_IOHGATP_MODE_SV57X4:
+		return iommu->caps & RISCV_IOMMU_CAPABILITIES_SV57X4;
+	}
+	return false;
+}
+
  static int riscv_iommu_attach_paging_domain(struct iommu_domain *iommu_domain,
  					    struct device *dev,
  					    struct iommu_domain *old)
@@ -1292,11 +1322,21 @@ static int riscv_iommu_attach_paging_domain(struct iommu_domain *iommu_domain,
 
  	pt_iommu_riscv_64_hw_info(&domain->riscvpt, &pt_info);
 
-	if (!riscv_iommu_pt_supported(iommu, pt_info.fsc_iosatp_mode))
-		return -ENODEV;
+	if (domain->gscid) {
+		if (!riscv_iommu_iohgatp_supported(iommu, pt_info.iohgatp_mode))
+			return -ENODEV;
+
+		dc.iohgatp =
+			FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_MODE, pt_info.iohgatp_mode) |
+			FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_GSCID, domain->gscid) |
+			FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_PPN, pt_info.ppn);
+	} else {
+		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_PC_FSC_MODE, pt_info.fsc_iosatp_mode) |
+			 FIELD_PREP(RISCV_IOMMU_PC_FSC_PPN, pt_info.ppn);
+	}
  	dc.ta = FIELD_PREP(RISCV_IOMMU_PC_TA_PSCID, domain->pscid) |
  	     RISCV_IOMMU_PC_TA_V;
 
Reviewed-by on v4 6/10
#13 lore raw

Re: [RFC PATCH v4 06/10] RISC-V: KVM: Enable KVM_VFIO interfaces on RISC-V arch

In reply to [email protected]
Quoted 7 lines · On 9/15/2026 11:28 AM, [email protected] wrote:

From: Tomasz Jeznach <[email protected]>

Enable KVM/VFIO support on RISC-V architecture.

Thanks,
Nutty

Quoted 19 lines
---
  arch/riscv/kvm/Kconfig | 2 ++
  1 file changed, 2 insertions(+)
arch/riscv/kvm/Kconfig +2 -0
index ec2cee0a39e0..54ee90f010ef 100644
@@ -30,8 +30,10 @@ config KVM
  	select KVM_GENERIC_HARDWARE_ENABLING
  	select KVM_MMIO
  	select VIRT_XFER_TO_GUEST_WORK
+	select KVM_VFIO
  	select SCHED_INFO
  	select GUEST_PERF_EVENTS if PERF_EVENTS
+	select SRCU
  	help
  	  Support hosting virtualized guest machines.
 
#7 lore raw

Re: [RFC PATCH v4 04/10] iommu/riscv: use data structure instead of individual values

In reply to [email protected]

Hi Fangyu,

Quoted 26 lines · On 9/15/2026 11:28 AM, [email protected] wrote:

From: Zong Li <[email protected]>

The parameter will be increased when we need to set up more bit fields in the device context. Use a data structure to wrap them up.

---
  drivers/iommu/riscv/iommu.c | 27 +++++++++++++++++----------
  1 file changed, 17 insertions(+), 10 deletions(-)
drivers/iommu/riscv/iommu.c +1 -1
index a6c8307e82ea..c5b6214ee672 100644
@@ -1165,7 +1165,7 @@ static void riscv_iommu_iodir_iotinval(struct riscv_iommu_device *iommu,
   * interim translation faults.
   */
  static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,
-				     struct device *dev, u64 fsc, u64 ta)
+				     struct device *dev, struct riscv_iommu_dc *new_dc)
  {
  	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  	struct riscv_iommu_dc *dc;

drivers/iommu/riscv/iommu.c static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu, · line 1207

Quoted 5 lines
drivers/iommu/riscv/iommu.c +1 -1
@@ -1204,10 +1204,10 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,
  	for (i = 0; i < fwspec->num_ids; i++) {
  		dc = riscv_iommu_get_dc(iommu, fwspec->ids[i]);
  		tc = READ_ONCE(dc->tc);
-		tc |= ta & RISCV_IOMMU_DC_TC_V;
+		tc |= new_dc->ta & RISCV_IOMMU_DC_TC_V;

According to SPEC 3.1.3.3, the "V bit" does not exist in DC.ta. This should be a convention from when the iodir_update() function did not yet use struct riscv_iommu_dc as a parameter, using bit 0 of DC.ta to indicate that the DC is valid. Now that iodir_update() receives the complete dc, it can directly access DC.tc.V, so this convention should no longer be necessary and will cause confusion.

drivers/iommu/riscv/iommu.c static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu, · line 1210

Quoted 5 lines
drivers/iommu/riscv/iommu.c +2 -2
-		WRITE_ONCE(dc->fsc, fsc);
-		WRITE_ONCE(dc->ta, ta & RISCV_IOMMU_PC_TA_PSCID);
+		WRITE_ONCE(dc->fsc, new_dc->fsc);
+		WRITE_ONCE(dc->ta, new_dc->ta & RISCV_IOMMU_PC_TA_PSCID);

RISCV_IOMMU_PC_* should be replaced with RISCV_IOMMU_DC_* for readability. There are several other similar places in this file that are unrelated to Process Context or PASID but use the *PC* macros.

Thanks,
Shuai

Quoted 58 lines
drivers/iommu/riscv/iommu.c +13 -6
  		/* Update device context, write TC.V as the last step. */
  		dma_wmb();
  		WRITE_ONCE(dc->tc, tc);
@@ -1288,22 +1288,22 @@ static int riscv_iommu_attach_paging_domain(struct iommu_domain *iommu_domain,
  	struct riscv_iommu_device *iommu = dev_to_iommu(dev);
  	struct riscv_iommu_info *info = dev_iommu_priv_get(dev);
  	struct pt_iommu_riscv_64_hw_info pt_info;
-	u64 fsc, ta;
+	struct riscv_iommu_dc dc = {0};
 
  	pt_iommu_riscv_64_hw_info(&domain->riscvpt, &pt_info);
 
  	if (!riscv_iommu_pt_supported(iommu, pt_info.fsc_iosatp_mode))
  		return -ENODEV;
 
-	fsc = FIELD_PREP(RISCV_IOMMU_PC_FSC_MODE, pt_info.fsc_iosatp_mode) |
+	dc.fsc = FIELD_PREP(RISCV_IOMMU_PC_FSC_MODE, pt_info.fsc_iosatp_mode) |
  	      FIELD_PREP(RISCV_IOMMU_PC_FSC_PPN, pt_info.ppn);
-	ta = FIELD_PREP(RISCV_IOMMU_PC_TA_PSCID, domain->pscid) |
+	dc.ta = FIELD_PREP(RISCV_IOMMU_PC_TA_PSCID, domain->pscid) |
  	     RISCV_IOMMU_PC_TA_V;
 
  	if (riscv_iommu_bond_link(domain, dev))
  		return -ENOMEM;
 
-	riscv_iommu_iodir_update(iommu, dev, fsc, ta);
+	riscv_iommu_iodir_update(iommu, dev, &dc);
  	riscv_iommu_bond_unlink(info->domain, dev);
  	info->domain = domain;
 
@@ -1378,9 +1378,12 @@ static int riscv_iommu_attach_blocking_domain(struct iommu_domain *iommu_domain,
  {
  	struct riscv_iommu_device *iommu = dev_to_iommu(dev);
  	struct riscv_iommu_info *info = dev_iommu_priv_get(dev);
+	struct riscv_iommu_dc dc = {0};
+
+	dc.fsc = RISCV_IOMMU_FSC_BARE;
 
  	/* Make device context invalid, translation requests will fault w/ #258 */
-	riscv_iommu_iodir_update(iommu, dev, RISCV_IOMMU_FSC_BARE, 0);
+	riscv_iommu_iodir_update(iommu, dev, &dc);
  	riscv_iommu_bond_unlink(info->domain, dev);
  	info->domain = NULL;
 
@@ -1400,8 +1403,12 @@ static int riscv_iommu_attach_identity_domain(struct iommu_domain *iommu_domain,
  {
  	struct riscv_iommu_device *iommu = dev_to_iommu(dev);
  	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;
 
-	riscv_iommu_iodir_update(iommu, dev, RISCV_IOMMU_FSC_BARE, RISCV_IOMMU_PC_TA_V);
+	riscv_iommu_iodir_update(iommu, dev, &dc);
  	riscv_iommu_bond_unlink(info->domain, dev);
  	info->domain = NULL;
 
#16 lore raw

Re: [RFC PATCH v4 08/10] iommu/riscv: Pre-enable GADE for second-stage domains

In reply to [email protected]

Hi Fangyu,

Quoted 24 lines · On 9/15/2026 11:28 AM, [email protected] wrote:

From: Fangyu Yu <[email protected]>

Pre-enable RISCV_IOMMU_DC_TC_GADE in the device context when attaching a second-stage domain, if the IOMMU supports AMO_HWAD.

Software pre-populates second-stage page tables with D set, so enabling GADE by default does not change normal behavior. When dirty tracking is enabled, iommufd clears the pre-set D bits and GADE becomes necessary for hardware to update the dirty bit on write access.

This avoids toggling GADE dynamically and keeps device context setup consistent with second-stage domain attachment.

---
  drivers/iommu/riscv/iommu.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/iommu/riscv/iommu.c +0 -0
index 16779877351b..f01fd4e2f840 100644

drivers/iommu/riscv/iommu.c static int riscv_iommu_attach_paging_domain(struct iommu_domain *iommu_domain, · line 1334

Quoted 5 lines
drivers/iommu/riscv/iommu.c +2 -0
@@ -1330,6 +1330,8 @@ static int riscv_iommu_attach_paging_domain(struct iommu_domain *iommu_domain,
  			FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_MODE, pt_info.iohgatp_mode) |
  			FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_GSCID, domain->gscid) |
  			FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_PPN, pt_info.ppn);
+		if (iommu->caps & RISCV_IOMMU_CAPABILITIES_AMO_HWAD)
+			dc.tc |= RISCV_IOMMU_DC_TC_GADE;

I might be missing something, but I don't see how the GADE bit set here reaches the hardware IOMMU DC. In riscv_iommu_iodir_update() (in patch 04/10), it looks like only the V bit is taken:

	tc = READ_ONCE(dc->tc);
	tc |= new_dc->ta & RISCV_IOMMU_DC_TC_V;
	...
	WRITE_ONCE(dc->tc, tc);

I saw that you mentioned you tested it on QEMU. As far as I know, mainline QEMU has not yet declared the AMO_HWAD capability. Could you clarify whether you used a modified QEMU for this test?

Thanks,
Shuai

Quoted 3 lines
drivers/iommu/riscv/iommu.c +0 -0
  	} else {
  		if (!riscv_iommu_fsc_supported(iommu, pt_info.fsc_iosatp_mode))
  			return -ENODEV;
#8 lore raw

Re: [RFC PATCH v4 04/10] iommu/riscv: use data structure instead of individual values

In reply to Gong Shuai
Quoted 33 lines

Hi Fangyu,

Quoted 30 lines · On 9/15/2026 11:28 AM, [email protected] wrote:

From: Zong Li <[email protected]>

The parameter will be increased when we need to set up more bit fields in the device context. Use a data structure to wrap them up.

---
  drivers/iommu/riscv/iommu.c | 27 +++++++++++++++++----------
  1 file changed, 17 insertions(+), 10 deletions(-)
drivers/iommu/riscv/iommu.c +1 -2
index a6c8307e82ea..c5b6214ee672 100644
@@ -1165,7 +1165,7 @@ static void riscv_iommu_iodir_iotinval(struct riscv_iommu_device *iommu,
   * interim translation faults.
   */
  static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,
-				     struct device *dev, u64 fsc, u64 ta)
+				     struct device *dev, struct riscv_iommu_dc *new_dc)
  {
  	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  	struct riscv_iommu_dc *dc;
@@ -1204,10 +1204,10 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,
  	for (i = 0; i < fwspec->num_ids; i++) {
  		dc = riscv_iommu_get_dc(iommu, fwspec->ids[i]);
  		tc = READ_ONCE(dc->tc);
-		tc |= ta & RISCV_IOMMU_DC_TC_V;
Quoted 9 lines

drivers/iommu/riscv/iommu.c static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu, · line 1207

Quoted 1 line
drivers/iommu/riscv/iommu.c +1 -0
+		tc |= new_dc->ta & RISCV_IOMMU_DC_TC_V;

According to SPEC 3.1.3.3, the "V bit" does not exist in DC.ta. This should be a convention from when the iodir_update() function did not yet use struct riscv_iommu_dc as a parameter, using bit 0 of DC.ta to indicate that the DC is valid. Now that iodir_update() receives the complete dc, it can directly access DC.tc.V, so this convention should no longer be necessary and will cause confusion.

Hi Shuai:

Thanks for the clarification.

My understanding is that ta here is used as a combined concept, not only for DC.ta, but also for PC.ta in this flow.

The V bit is only initialized when ta is first set up, so the current usage still follows the original design, without changing most of the existing logic.

The original code also has comments describing this behavior.

Thanks,
Fangyu

Quoted 10 lines

drivers/iommu/riscv/iommu.c static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu, · line 1210

Quoted 5 lines
drivers/iommu/riscv/iommu.c +2 -2
-		WRITE_ONCE(dc->fsc, fsc);
-		WRITE_ONCE(dc->ta, ta & RISCV_IOMMU_PC_TA_PSCID);
+		WRITE_ONCE(dc->fsc, new_dc->fsc);
+		WRITE_ONCE(dc->ta, new_dc->ta & RISCV_IOMMU_PC_TA_PSCID);

RISCV_IOMMU_PC_* should be replaced with RISCV_IOMMU_DC_* for readability. There are several other similar places in this file that are unrelated to Process Context or PASID but use the *PC* macros.

My understanding is similar to the ta case: fsc likely refers to both dc.fsc and pc.fsc in this path, since some of their fields share the same bit layout.

If we want to rename these for readability, I think that should be done in a separate patch set, rather than as part of this one.

Quoted 62 lines

Thanks,
Shuai

Quoted 58 lines
drivers/iommu/riscv/iommu.c +13 -6
  		/* Update device context, write TC.V as the last step. */
  		dma_wmb();
  		WRITE_ONCE(dc->tc, tc);
@@ -1288,22 +1288,22 @@ static int riscv_iommu_attach_paging_domain(struct iommu_domain *iommu_domain,
  	struct riscv_iommu_device *iommu = dev_to_iommu(dev);
  	struct riscv_iommu_info *info = dev_iommu_priv_get(dev);
  	struct pt_iommu_riscv_64_hw_info pt_info;
-	u64 fsc, ta;
+	struct riscv_iommu_dc dc = {0};
 
  	pt_iommu_riscv_64_hw_info(&domain->riscvpt, &pt_info);
 
  	if (!riscv_iommu_pt_supported(iommu, pt_info.fsc_iosatp_mode))
  		return -ENODEV;
 
-	fsc = FIELD_PREP(RISCV_IOMMU_PC_FSC_MODE, pt_info.fsc_iosatp_mode) |
+	dc.fsc = FIELD_PREP(RISCV_IOMMU_PC_FSC_MODE, pt_info.fsc_iosatp_mode) |
  	      FIELD_PREP(RISCV_IOMMU_PC_FSC_PPN, pt_info.ppn);
-	ta = FIELD_PREP(RISCV_IOMMU_PC_TA_PSCID, domain->pscid) |
+	dc.ta = FIELD_PREP(RISCV_IOMMU_PC_TA_PSCID, domain->pscid) |
  	     RISCV_IOMMU_PC_TA_V;
 
  	if (riscv_iommu_bond_link(domain, dev))
  		return -ENOMEM;
 
-	riscv_iommu_iodir_update(iommu, dev, fsc, ta);
+	riscv_iommu_iodir_update(iommu, dev, &dc);
  	riscv_iommu_bond_unlink(info->domain, dev);
  	info->domain = domain;
 
@@ -1378,9 +1378,12 @@ static int riscv_iommu_attach_blocking_domain(struct iommu_domain *iommu_domain,
  {
  	struct riscv_iommu_device *iommu = dev_to_iommu(dev);
  	struct riscv_iommu_info *info = dev_iommu_priv_get(dev);
+	struct riscv_iommu_dc dc = {0};
+
+	dc.fsc = RISCV_IOMMU_FSC_BARE;
 
  	/* Make device context invalid, translation requests will fault w/ #258 */
-	riscv_iommu_iodir_update(iommu, dev, RISCV_IOMMU_FSC_BARE, 0);
+	riscv_iommu_iodir_update(iommu, dev, &dc);
  	riscv_iommu_bond_unlink(info->domain, dev);
  	info->domain = NULL;
 
@@ -1400,8 +1403,12 @@ static int riscv_iommu_attach_identity_domain(struct iommu_domain *iommu_domain,
  {
  	struct riscv_iommu_device *iommu = dev_to_iommu(dev);
  	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;
 
-	riscv_iommu_iodir_update(iommu, dev, RISCV_IOMMU_FSC_BARE, RISCV_IOMMU_PC_TA_V);
+	riscv_iommu_iodir_update(iommu, dev, &dc);
  	riscv_iommu_bond_unlink(info->domain, dev);
  	info->domain = NULL;
 
#17 lore raw

Re: [RFC PATCH v4 08/10] iommu/riscv: Pre-enable GADE for second-stage domains

In reply to Gong Shuai
Quoted 22 lines

Hi Fangyu,

Quoted 19 lines · On 9/15/2026 11:28 AM, [email protected] wrote:

From: Fangyu Yu <[email protected]>

Pre-enable RISCV_IOMMU_DC_TC_GADE in the device context when attaching a second-stage domain, if the IOMMU supports AMO_HWAD.

Software pre-populates second-stage page tables with D set, so enabling GADE by default does not change normal behavior. When dirty tracking is enabled, iommufd clears the pre-set D bits and GADE becomes necessary for hardware to update the dirty bit on write access.

This avoids toggling GADE dynamically and keeps device context setup consistent with second-stage domain attachment.

---
  drivers/iommu/riscv/iommu.c | 2 ++
  1 file changed, 2 insertions(+)
Quoted 10 lines

drivers/iommu/riscv/iommu.c static int riscv_iommu_attach_paging_domain(struct iommu_domain *iommu_domain, · line 1334

Quoted 10 lines
drivers/iommu/riscv/iommu.c +2 -0
index 16779877351b..f01fd4e2f840 100644
@@ -1330,6 +1330,8 @@ static int riscv_iommu_attach_paging_domain(struct iommu_domain *iommu_domain,
  			FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_MODE, pt_info.iohgatp_mode) |
  			FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_GSCID, domain->gscid) |
  			FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_PPN, pt_info.ppn);
+		if (iommu->caps & RISCV_IOMMU_CAPABILITIES_AMO_HWAD)
+			dc.tc |= RISCV_IOMMU_DC_TC_GADE;

Hi Shuai:

Quoted 10 lines

I might be missing something, but I don't see how the GADE bit set here reaches the hardware IOMMU DC. In riscv_iommu_iodir_update() (in patch 04/10), it looks like only the V bit is taken:

	tc = READ_ONCE(dc->tc);
	tc |= new_dc->ta & RISCV_IOMMU_DC_TC_V;
	...
	WRITE_ONCE(dc->tc, tc);

You’re right — this is a real issue.

GADE was being set in the software-side dc, but it was not propagated into the hardware DC in riscv_iommu_iodir_update().

I have already fixed this in my local tree, and I’ll include the change in the next version of the patchset:

    tc &= ~RISCV_IOMMU_DC_TC_GADE;
    tc |= new_dc->tc & RISCV_IOMMU_DC_TC_GADE;
    tc |= new_dc->ta & RISCV_IOMMU_DC_TC_V;
Quoted 4 lines

I saw that you mentioned you tested it on QEMU. As far as I know, mainline QEMU has not yet declared the AMO_HWAD capability. Could you clarify whether you used a modified QEMU for this test?

I used mainline QEMU, not a modified one.

In QEMU, I only tested the second-stage path with a VM using device passthrough.

I did not test the dirty-log related part there, so that path cannot be covered there.

That is why I noted in the TODO that I will build a dedicated test case for dirty log handling later.

Thanks,
Fangyu

Quoted 6 lines

Thanks,
Shuai

Quoted 3 lines
drivers/iommu/riscv/iommu.c +0 -0
  	} else {
  		if (!riscv_iommu_fsc_supported(iommu, pt_info.fsc_iosatp_mode))
  			return -ENODEV;
#18 lore raw

Re: [RFC PATCH v4 08/10] iommu/riscv: Pre-enable GADE for second-stage domains

Hi Fangyu,

Quoted 48 lines · On 9/20/2026 11:12 AM, [email protected] wrote:
Quoted 22 lines

Hi Fangyu,

Quoted 19 lines · On 9/15/2026 11:28 AM, [email protected] wrote:

From: Fangyu Yu <[email protected]>

Pre-enable RISCV_IOMMU_DC_TC_GADE in the device context when attaching a second-stage domain, if the IOMMU supports AMO_HWAD.

Software pre-populates second-stage page tables with D set, so enabling GADE by default does not change normal behavior. When dirty tracking is enabled, iommufd clears the pre-set D bits and GADE becomes necessary for hardware to update the dirty bit on write access.

This avoids toggling GADE dynamically and keeps device context setup consistent with second-stage domain attachment.

---
  drivers/iommu/riscv/iommu.c | 2 ++
  1 file changed, 2 insertions(+)
Quoted 10 lines

drivers/iommu/riscv/iommu.c static int riscv_iommu_attach_paging_domain(struct iommu_domain *iommu_domain, · line 1334

Quoted 10 lines
drivers/iommu/riscv/iommu.c +2 -0
index 16779877351b..f01fd4e2f840 100644
@@ -1330,6 +1330,8 @@ static int riscv_iommu_attach_paging_domain(struct iommu_domain *iommu_domain,
  			FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_MODE, pt_info.iohgatp_mode) |
  			FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_GSCID, domain->gscid) |
  			FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_PPN, pt_info.ppn);
+		if (iommu->caps & RISCV_IOMMU_CAPABILITIES_AMO_HWAD)
+			dc.tc |= RISCV_IOMMU_DC_TC_GADE;

Hi Shuai:

Quoted 10 lines

I might be missing something, but I don't see how the GADE bit set here reaches the hardware IOMMU DC. In riscv_iommu_iodir_update() (in patch 04/10), it looks like only the V bit is taken:

	tc = READ_ONCE(dc->tc);
	tc |= new_dc->ta & RISCV_IOMMU_DC_TC_V;
	...
	WRITE_ONCE(dc->tc, tc);

You’re right — this is a real issue.

Quoted 9 lines

GADE was being set in the software-side dc, but it was not propagated into the hardware DC in riscv_iommu_iodir_update().

I have already fixed this in my local tree, and I’ll include the change in the next version of the patchset:

     tc &= ~RISCV_IOMMU_DC_TC_GADE;
     tc |= new_dc->tc & RISCV_IOMMU_DC_TC_GADE;
     tc |= new_dc->ta & RISCV_IOMMU_DC_TC_V;

One thought here, instead of updating individual TC bits one by one in riscv_iommu_iodir_update(), could we transfer all software-configurable bits from the incoming DC to the hardware DC in one go?

Something like:

	...
	tc = (READ_ONCE(dc->tc) & ~GENMASK_ULL(11, 0)) |
		 (new_dc->tc & GENMASK_ULL(11, 0));
	...
	WRITE_ONCE(dc->tc, tc);

I’m not sure whether this could have negative side effects.

Quoted 7 lines
Quoted 4 lines

I saw that you mentioned you tested it on QEMU. As far as I know, mainline QEMU has not yet declared the AMO_HWAD capability. Could you clarify whether you used a modified QEMU for this test?

I used mainline QEMU, not a modified one.

Quoted 8 lines

In QEMU, I only tested the second-stage path with a VM using device passthrough.

I did not test the dirty-log related part there, so that path cannot be covered there.

That is why I noted in the TODO that I will build a dedicated test case for dirty log handling later.

Sorry, I missed that TODO.

Thanks,
Shuai

Quoted 10 lines

Thanks,
Fangyu

Quoted 6 lines

Thanks,
Shuai

Quoted 3 lines
drivers/iommu/riscv/iommu.c +0 -0
  	} else {
  		if (!riscv_iommu_fsc_supported(iommu, pt_info.fsc_iosatp_mode))
  			return -ENODEV;
#9 lore raw

Re: [RFC PATCH v4 04/10] iommu/riscv: use data structure instead of individual values

In reply to [email protected]

Hi Fangyu,

Quoted 51 lines · On 9/20/2026 11:08 AM, [email protected] wrote:
Quoted 33 lines

Hi Fangyu,

Quoted 30 lines · On 9/15/2026 11:28 AM, [email protected] wrote:

From: Zong Li <[email protected]>

The parameter will be increased when we need to set up more bit fields in the device context. Use a data structure to wrap them up.

---
  drivers/iommu/riscv/iommu.c | 27 +++++++++++++++++----------
  1 file changed, 17 insertions(+), 10 deletions(-)
drivers/iommu/riscv/iommu.c +1 -2
index a6c8307e82ea..c5b6214ee672 100644
@@ -1165,7 +1165,7 @@ static void riscv_iommu_iodir_iotinval(struct riscv_iommu_device *iommu,
   * interim translation faults.
   */
  static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,
-				     struct device *dev, u64 fsc, u64 ta)
+				     struct device *dev, struct riscv_iommu_dc *new_dc)
  {
  	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  	struct riscv_iommu_dc *dc;
@@ -1204,10 +1204,10 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,
  	for (i = 0; i < fwspec->num_ids; i++) {
  		dc = riscv_iommu_get_dc(iommu, fwspec->ids[i]);
  		tc = READ_ONCE(dc->tc);
-		tc |= ta & RISCV_IOMMU_DC_TC_V;
Quoted 9 lines

drivers/iommu/riscv/iommu.c static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu, · line 1207

Quoted 1 line
drivers/iommu/riscv/iommu.c +1 -0
+		tc |= new_dc->ta & RISCV_IOMMU_DC_TC_V;

According to SPEC 3.1.3.3, the "V bit" does not exist in DC.ta. This should be a convention from when the iodir_update() function did not yet use struct riscv_iommu_dc as a parameter, using bit 0 of DC.ta to indicate that the DC is valid. Now that iodir_update() receives the complete dc, it can directly access DC.tc.V, so this convention should no longer be necessary and will cause confusion.

Hi Shuai:

Thanks for the clarification.

My understanding is that ta here is used as a combined concept, not only for DC.ta, but also for PC.ta in this flow.

Quoted 7 lines

The V bit is only initialized when ta is first set up, so the current usage still follows the original design, without changing most of the existing logic.

The original code also has comments describing this behavior.

I got your point.

When fsc and ta were passed as separate raw parameters, treating them as a combined concept was arguably reasonable, since the same bit layout is shared between DC and PC in this path. But now that iodir_update() takes a struct riscv_iommu_dc, I think it would be better to keep the semantics explicit.

If PDT/PC support is added later, we can introduce a separate struct riscv_iommu_pc parameter for the PC context, instead of trying to make one ta/fsc cover both cases.

Quoted 14 lines

Thanks,
Fangyu

Quoted 10 lines

drivers/iommu/riscv/iommu.c static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu, · line 1210

Quoted 5 lines
drivers/iommu/riscv/iommu.c +2 -2
-		WRITE_ONCE(dc->fsc, fsc);
-		WRITE_ONCE(dc->ta, ta & RISCV_IOMMU_PC_TA_PSCID);
+		WRITE_ONCE(dc->fsc, new_dc->fsc);
+		WRITE_ONCE(dc->ta, new_dc->ta & RISCV_IOMMU_PC_TA_PSCID);

RISCV_IOMMU_PC_* should be replaced with RISCV_IOMMU_DC_* for readability. There are several other similar places in this file that are unrelated to Process Context or PASID but use the *PC* macros.

Quoted 4 lines

My understanding is similar to the ta case: fsc likely refers to both dc.fsc and pc.fsc in this path, since some of their fields share the same bit layout.

Yeah, exactly, they share the same layout, so logically it's fine.

Quoted 4 lines

If we want to rename these for readability, I think that should be done in a separate patch set, rather than as part of this one.

Agree that it should be done in a separate patch.

Thanks,
Shuai

Quoted 63 lines
Quoted 63 lines

Thanks,
Shuai

Quoted 58 lines
drivers/iommu/riscv/iommu.c +13 -6
  		/* Update device context, write TC.V as the last step. */
  		dma_wmb();
  		WRITE_ONCE(dc->tc, tc);
@@ -1288,22 +1288,22 @@ static int riscv_iommu_attach_paging_domain(struct iommu_domain *iommu_domain,
  	struct riscv_iommu_device *iommu = dev_to_iommu(dev);
  	struct riscv_iommu_info *info = dev_iommu_priv_get(dev);
  	struct pt_iommu_riscv_64_hw_info pt_info;
-	u64 fsc, ta;
+	struct riscv_iommu_dc dc = {0};
 
  	pt_iommu_riscv_64_hw_info(&domain->riscvpt, &pt_info);
 
  	if (!riscv_iommu_pt_supported(iommu, pt_info.fsc_iosatp_mode))
  		return -ENODEV;
 
-	fsc = FIELD_PREP(RISCV_IOMMU_PC_FSC_MODE, pt_info.fsc_iosatp_mode) |
+	dc.fsc = FIELD_PREP(RISCV_IOMMU_PC_FSC_MODE, pt_info.fsc_iosatp_mode) |
  	      FIELD_PREP(RISCV_IOMMU_PC_FSC_PPN, pt_info.ppn);
-	ta = FIELD_PREP(RISCV_IOMMU_PC_TA_PSCID, domain->pscid) |
+	dc.ta = FIELD_PREP(RISCV_IOMMU_PC_TA_PSCID, domain->pscid) |
  	     RISCV_IOMMU_PC_TA_V;
 
  	if (riscv_iommu_bond_link(domain, dev))
  		return -ENOMEM;
 
-	riscv_iommu_iodir_update(iommu, dev, fsc, ta);
+	riscv_iommu_iodir_update(iommu, dev, &dc);
  	riscv_iommu_bond_unlink(info->domain, dev);
  	info->domain = domain;
 
@@ -1378,9 +1378,12 @@ static int riscv_iommu_attach_blocking_domain(struct iommu_domain *iommu_domain,
  {
  	struct riscv_iommu_device *iommu = dev_to_iommu(dev);
  	struct riscv_iommu_info *info = dev_iommu_priv_get(dev);
+	struct riscv_iommu_dc dc = {0};
+
+	dc.fsc = RISCV_IOMMU_FSC_BARE;
 
  	/* Make device context invalid, translation requests will fault w/ #258 */
-	riscv_iommu_iodir_update(iommu, dev, RISCV_IOMMU_FSC_BARE, 0);
+	riscv_iommu_iodir_update(iommu, dev, &dc);
  	riscv_iommu_bond_unlink(info->domain, dev);
  	info->domain = NULL;
 
@@ -1400,8 +1403,12 @@ static int riscv_iommu_attach_identity_domain(struct iommu_domain *iommu_domain,
  {
  	struct riscv_iommu_device *iommu = dev_to_iommu(dev);
  	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;
 
-	riscv_iommu_iodir_update(iommu, dev, RISCV_IOMMU_FSC_BARE, RISCV_IOMMU_PC_TA_V);
+	riscv_iommu_iodir_update(iommu, dev, &dc);
  	riscv_iommu_bond_unlink(info->domain, dev);
  	info->domain = NULL;
 
#19 lore raw

Re: [RFC PATCH v4 08/10] iommu/riscv: Pre-enable GADE for second-stage domains

In reply to Gong Shuai
Quoted 65 lines

Hi Fangyu,

Quoted 48 lines · On 9/20/2026 11:12 AM, [email protected] wrote:
Quoted 22 lines

Hi Fangyu,

Quoted 19 lines · On 9/15/2026 11:28 AM, [email protected] wrote:

From: Fangyu Yu <[email protected]>

Pre-enable RISCV_IOMMU_DC_TC_GADE in the device context when attaching a second-stage domain, if the IOMMU supports AMO_HWAD.

Software pre-populates second-stage page tables with D set, so enabling GADE by default does not change normal behavior. When dirty tracking is enabled, iommufd clears the pre-set D bits and GADE becomes necessary for hardware to update the dirty bit on write access.

This avoids toggling GADE dynamically and keeps device context setup consistent with second-stage domain attachment.

---
  drivers/iommu/riscv/iommu.c | 2 ++
  1 file changed, 2 insertions(+)
Quoted 10 lines

drivers/iommu/riscv/iommu.c static int riscv_iommu_attach_paging_domain(struct iommu_domain *iommu_domain, · line 1334

Quoted 10 lines
drivers/iommu/riscv/iommu.c +2 -0
index 16779877351b..f01fd4e2f840 100644
@@ -1330,6 +1330,8 @@ static int riscv_iommu_attach_paging_domain(struct iommu_domain *iommu_domain,
  			FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_MODE, pt_info.iohgatp_mode) |
  			FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_GSCID, domain->gscid) |
  			FIELD_PREP(RISCV_IOMMU_DC_IOHGATP_PPN, pt_info.ppn);
+		if (iommu->caps & RISCV_IOMMU_CAPABILITIES_AMO_HWAD)
+			dc.tc |= RISCV_IOMMU_DC_TC_GADE;

Hi Shuai:

Quoted 10 lines

I might be missing something, but I don't see how the GADE bit set here reaches the hardware IOMMU DC. In riscv_iommu_iodir_update() (in patch 04/10), it looks like only the V bit is taken:

	tc = READ_ONCE(dc->tc);
	tc |= new_dc->ta & RISCV_IOMMU_DC_TC_V;
	...
	WRITE_ONCE(dc->tc, tc);

You’re right — this is a real issue.

Quoted 9 lines

GADE was being set in the software-side dc, but it was not propagated into the hardware DC in riscv_iommu_iodir_update().

I have already fixed this in my local tree, and I’ll include the change in the next version of the patchset:

     tc &= ~RISCV_IOMMU_DC_TC_GADE;
     tc |= new_dc->tc & RISCV_IOMMU_DC_TC_GADE;
     tc |= new_dc->ta & RISCV_IOMMU_DC_TC_V;

One thought here, instead of updating individual TC bits one by one in riscv_iommu_iodir_update(), could we transfer all software-configurable bits from the incoming DC to the hardware DC in one go?

Quoted 9 lines

Something like:

	...
	tc = (READ_ONCE(dc->tc) & ~GENMASK_ULL(11, 0)) |
		 (new_dc->tc & GENMASK_ULL(11, 0));
	...
	WRITE_ONCE(dc->tc, tc);

I’m not sure whether this could have negative side effects.

Hi Shuai:

That is a reasonable idea in principle.

However, TC bits may have different lifetimes and update rules, and several features such as ATS and PRI are still not implemented yet.

For now, I would prefer to keep the change minimal and only handle GADE here.

Once the remaining features are in place, we can consider a broader cleanup and unify the handling of these bits.

Thanks,
Fangyu

Quoted 22 lines
Quoted 7 lines
Quoted 4 lines

I saw that you mentioned you tested it on QEMU. As far as I know, mainline QEMU has not yet declared the AMO_HWAD capability. Could you clarify whether you used a modified QEMU for this test?

I used mainline QEMU, not a modified one.

Quoted 8 lines

In QEMU, I only tested the second-stage path with a VM using device passthrough.

I did not test the dirty-log related part there, so that path cannot be covered there.

That is why I noted in the TODO that I will build a dedicated test case for dirty log handling later.

Sorry, I missed that TODO.

Thanks,
Shuai

#24 lore raw

Re: [RFC PATCH v4 10/10] iommu/riscv: Add IOTINVAL.GVMA after updating DDT/PDT entries

In reply to [email protected]

Hi Fangyu,

I wonder if riscv_iommu_iodir_set_mode() also needs a global IOTINVAL.GVMA now that second-stage translations are supported.

It currently issues IODIR.INVAL_DDT and IOTINVAL.VMA after updating DDTP, but cached G-stage translations may still remain, for example when the IOMMU is reinitialized after a previous kernel used second-stage domains.

Would a global IOTINVAL.GVMA (GV=0) be needed here as well?

Thanks,
Tan Chi

Signature · 3 lines
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
#25 lore raw

Re: [RFC PATCH v4 10/10] iommu/riscv: Add IOTINVAL.GVMA after updating DDT/PDT entries

Quoted 5 lines

Hi Fangyu,

I wonder if riscv_iommu_iodir_set_mode() also needs a global IOTINVAL.GVMA now that second-stage translations are supported.

Quoted 7 lines

It currently issues IODIR.INVAL_DDT and IOTINVAL.VMA after updating DDTP, but cached G-stage translations may still remain, for example when the IOMMU is reinitialized after a previous kernel used second-stage domains.

Would a global IOTINVAL.GVMA (GV=0) be needed here as well?

Good catch.

Per the spec, adding an IOTINVAL.GVMA here is more accurate when second-stage translation is enabled.

I'll add an IOTINVAL.GVMA in set_mode() in the next version.

Thanks,
Fangyu

Quoted 3 lines

Thanks,
Tan Chi