[RFC PATCH v3 09/10] iommu/riscv: Add riscv_iommu_attach_dev_nested
From: Gong Shuai
Date: Thu Nov 19 2026 - 08:29:12 EST
From: Wu Fei <wu.fei9@xxxxxxxxxxxxxxxx>
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@xxxxxxxxxxxxxxxx>
Co-developed-by: Gong Shuai <gong.shuai@xxxxxxxxxxxxxxxx>
Signed-off-by: Gong Shuai <gong.shuai@xxxxxxxxxxxxxxxx>
---
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