iommu/riscv: Add IOTINVAL after updating DDT/PDT entries
Subsystems (3)
PATCH 1/1 iommu/riscv: Add IOTINVAL after updating DDT/PDT entries
From: Fangyu Yu <[email protected]>
Add riscv_iommu_iodir_iotinval() to perform required TLB and context cache invalidations after updating DDT or PDT entries, as mandated by the RISC-V IOMMU specification (Section 6.3.1 and 6.3.2).
--- drivers/iommu/riscv/iommu.c | 85 +++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+)
drivers/iommu/riscv/iommu.c +85 -0
@@ -996,7 +996,82 @@ static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain, }#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) +* after modifying DDT or PDT entries +*/ +static void riscv_iommu_iodir_iotinval (struct riscv_iommu_device *iommu, +bool inval_pdt,unsigned long iohgatp, +struct riscv_iommu_dc *dc,struct riscv_iommu_pc *pc) +{ +struct riscv_iommu_command cmd; +if (FIELD_GET (RISCV_IOMMU_DC_IOHGATP_MODE , iohgatp) == +RISCV_IOMMU_DC_IOHGATP_MODE_BARE ) { +if (inval_pdt) { +/* +* IOTINVAL.VMA with GV=AV=0, and PSCV=1, and +* PSCID=PC.PSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd); +riscv_iommu_cmd_inval_set_pscid (&cmd, +FIELD_GET (RISCV_IOMMU_PC_TA_PSCID , pc->ta)); + }else { +if (FIELD_GET (RISCV_IOMMU_DC_TC_PDTV , dc->tc) || ( +FIELD_GET (RISCV_IOMMU_DC_FSC_MODE , dc->fsc) == +RISCV_IOMMU_DC_FSC_MODE_BARE )) { +/* IOTINVAL.VMA with GV=AV=PSCV=0 */ +riscv_iommu_cmd_inval_vma (&cmd); + }else { +/* +* IOTINVAL.VMA with GV=AV=0, and PSCV=1, and +* PSCID=DC.ta.PSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd); +riscv_iommu_cmd_inval_set_pscid (&cmd, +FIELD_GET (RISCV_IOMMU_DC_TA_PSCID , dc->ta)); + } + } + }else { +if (inval_pdt) { +/* +* IOTINVAL.VMA with GV=1, AV=0, and PSCV=1, and +* GSCID=DC.iohgatp.GSCID, PSCID=PC.PSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd); +riscv_iommu_cmd_inval_set_gscid (&cmd, +FIELD_GET (RISCV_IOMMU_DC_IOHGATP_GSCID , iohgatp)); +riscv_iommu_cmd_inval_set_pscid (&cmd, +FIELD_GET (RISCV_IOMMU_PC_TA_PSCID , pc->ta)); + }else { +/* +* IOTINVAL.VMA with GV=1,AV=PSCV=0,and +* GSCID=DC.iohgatp.GSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd); +riscv_iommu_cmd_inval_set_gscid (&cmd, +FIELD_GET (RISCV_IOMMU_DC_IOHGATP_GSCID , iohgatp)); + +/* +* IOTINVAL.GVMA with GV=1,AV=0,and +* GSCID=DC.iohgatp.GSCID +*/ +/* +* For now, the Second-Stage feature have not yet been merged, so +* let's comment out the code first. +*/ +#if 0 +riscv_iommu_cmd_send (iommu, &cmd); +memset (&cmd,0 ,sizeof (cmd)); +riscv_iommu_cmd_inval_gvma (&cmd); +riscv_iommu_cmd_inval_set_gscid (&cmd, +FIELD_GET (RISCV_IOMMU_DC_IOHGATP_GSCID , iohgatp)); +#endif + } + } +riscv_iommu_cmd_send (iommu, &cmd); +}/* * Update IODIR for the device. *
@@ -1031,6 +1106,11 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,riscv_iommu_cmd_iodir_inval_ddt (&cmd);riscv_iommu_cmd_iodir_set_did (&cmd, fwspec->ids[i]);riscv_iommu_cmd_send (iommu, &cmd); +/* +* For now, the SVA and PASID features have not yet been merged, the +* default configuration is inval_pdt=false and pc=NULL. +*/ +riscv_iommu_iodir_iotinval (iommu, false, dc->iohgatp, dc,NULL ); sync_required = true; }
@@ -1055,6 +1135,11 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,/* Invalidate device context after update */ riscv_iommu_cmd_iodir_inval_ddt (&cmd);riscv_iommu_cmd_iodir_set_did (&cmd, fwspec->ids[i]); +/* +* For now, the SVA and PASID features have not yet been merged, the +* default configuration is inval_pdt=false and pc=NULL. +*/ +riscv_iommu_iodir_iotinval (iommu, false, dc->iohgatp, dc,NULL );riscv_iommu_cmd_send (iommu, &cmd); }
Signature · 1 line
2.50.1
Quoted 22 lines · On Thu, Jan 08, 2026 at 09:48:55PM +0800, [email protected] wrote:
From: Fangyu Yu <[email protected]>
Add
riscv_iommu_iodir_iotinval()to perform required TLB and context cache invalidations after updating DDT or PDT entries, as mandated by the RISC-V IOMMU specification (Section 6.3.1 and 6.3.2).Signed-off-by Fangyu Yu <[email protected]>--- drivers/iommu/riscv/iommu.c | 85 +++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+)drivers/iommu/riscv/iommu.c +3
-0@@ -996,7 +996,82 @@ static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain, }#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)
drivers/iommu/riscv/iommu.c static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain, · line 1011
Quoted 10 lines
drivers/iommu/riscv/iommu.c +9
-0+* after modifying DDT or PDT entries +*/ +static void riscv_iommu_iodir_iotinval (struct riscv_iommu_device *iommu, +bool inval_pdt,unsigned long iohgatp, +struct riscv_iommu_dc *dc,struct riscv_iommu_pc *pc) +{ +struct riscv_iommu_command cmd; +if (FIELD_GET (RISCV_IOMMU_DC_IOHGATP_MODE , iohgatp) == +RISCV_IOMMU_DC_IOHGATP_MODE_BARE ) {
nit: This condition can stick out. We have 100 chars.
drivers/iommu/riscv/iommu.c static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain, · line 1023
Quoted 12 lines
drivers/iommu/riscv/iommu.c +12
-0+if (inval_pdt) { +/* +* IOTINVAL.VMA with GV=AV=0, and PSCV=1, and +* PSCID=PC.PSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd); +riscv_iommu_cmd_inval_set_pscid (&cmd, +FIELD_GET (RISCV_IOMMU_PC_TA_PSCID , pc->ta)); + }else { +if (FIELD_GET (RISCV_IOMMU_DC_TC_PDTV , dc->tc) || ( +FIELD_GET (RISCV_IOMMU_DC_FSC_MODE , dc->fsc) == +RISCV_IOMMU_DC_FSC_MODE_BARE )) {
nit: formatting
if (FIELD_GET(RISCV_IOMMU_DC_TC_PDTV, dc->tc) ||
(FIELD_GET(RISCV_IOMMU_DC_FSC_MODE, dc->fsc) ==
RISCV_IOMMU_DC_FSC_MODE_BARE)) {Quoted 33 lines
drivers/iommu/riscv/iommu.c +33
-0+/* IOTINVAL.VMA with GV=AV=PSCV=0 */ +riscv_iommu_cmd_inval_vma (&cmd); + }else { +/* +* IOTINVAL.VMA with GV=AV=0, and PSCV=1, and +* PSCID=DC.ta.PSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd); +riscv_iommu_cmd_inval_set_pscid (&cmd, +FIELD_GET (RISCV_IOMMU_DC_TA_PSCID , dc->ta)); + } + } + }else { +if (inval_pdt) { +/* +* IOTINVAL.VMA with GV=1, AV=0, and PSCV=1, and +* GSCID=DC.iohgatp.GSCID, PSCID=PC.PSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd); +riscv_iommu_cmd_inval_set_gscid (&cmd, +FIELD_GET (RISCV_IOMMU_DC_IOHGATP_GSCID , iohgatp)); +riscv_iommu_cmd_inval_set_pscid (&cmd, +FIELD_GET (RISCV_IOMMU_PC_TA_PSCID , pc->ta)); + }else { +/* +* IOTINVAL.VMA with GV=1,AV=PSCV=0,and +* GSCID=DC.iohgatp.GSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd); +riscv_iommu_cmd_inval_set_gscid (&cmd, +FIELD_GET (RISCV_IOMMU_DC_IOHGATP_GSCID , iohgatp)); + +/*
drivers/iommu/riscv/iommu.c static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain, · line 1066
Quoted 10 lines
drivers/iommu/riscv/iommu.c +10
-0+* IOTINVAL.GVMA with GV=1,AV=0,and +* GSCID=DC.iohgatp.GSCID +*/ +/* +* For now, the Second-Stage feature have not yet been merged, so +* let's comment out the code first. +*/ +#if 0 +riscv_iommu_cmd_send (iommu, &cmd); +memset (&cmd,0 ,sizeof (cmd));
The memset isn't necessary since riscv_iommu_cmd_inval_gvma(), which doesn't yet exist, will overwrite dword0 and zero out dword1.
Quoted 38 lines
drivers/iommu/riscv/iommu.c +18
-0+riscv_iommu_cmd_inval_gvma (&cmd); +riscv_iommu_cmd_inval_set_gscid (&cmd, +FIELD_GET (RISCV_IOMMU_DC_IOHGATP_GSCID , iohgatp)); +#endif + } + } +riscv_iommu_cmd_send (iommu, &cmd); +}/* * Update IODIR for the device. * @@ -1031,6 +1106,11 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,riscv_iommu_cmd_iodir_inval_ddt (&cmd);riscv_iommu_cmd_iodir_set_did (&cmd, fwspec->ids[i]);riscv_iommu_cmd_send (iommu, &cmd); +/* +* For now, the SVA and PASID features have not yet been merged, the +* default configuration is inval_pdt=false and pc=NULL. +*/ +riscv_iommu_iodir_iotinval (iommu, false, dc->iohgatp, dc,NULL ); sync_required = true; }@@ -1055,6 +1135,11 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,/* Invalidate device context after update */ riscv_iommu_cmd_iodir_inval_ddt (&cmd);riscv_iommu_cmd_iodir_set_did (&cmd, fwspec->ids[i]); +/* +* For now, the SVA and PASID features have not yet been merged, the +* default configuration is inval_pdt=false and pc=NULL. +*/ +riscv_iommu_iodir_iotinval (iommu, false, dc->iohgatp, dc,NULL );riscv_iommu_cmd_send (iommu, &cmd); }Signature · 1 line
2.50.1
A faithful implementation of the 6.3.1 and 6.3.2 guidelines for what the code currently supports. I presume this is fixing an issue? If so, can you point that out in the commit message?
Otherwise,
Thanks,
drew
On Fri, Jan 16, 2026 at 02:12:06PM -0600, Andrew Jones wrote:
...
Quoted 9 lines
drivers/iommu/riscv/iommu.c static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu, · line 1142
Quoted 9 lines
drivers/iommu/riscv/iommu.c +5
-0@@ -1055,6 +1135,11 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,/* Invalidate device context after update */ riscv_iommu_cmd_iodir_inval_ddt (&cmd);riscv_iommu_cmd_iodir_set_did (&cmd, fwspec->ids[i]); +/* +* For now, the SVA and PASID features have not yet been merged, the +* default configuration is inval_pdt=false and pc=NULL. +*/ +riscv_iommu_iodir_iotinval (iommu, false, dc->iohgatp, dc,NULL );
Shouldn't this iotinval come after the cmd-send below?
Quoted 6 lines
Quoted 6 lines
drivers/iommu/riscv/iommu.c +0
-0riscv_iommu_cmd_send (iommu, &cmd); }Signature · 1 line
2.50.1
Thanks,
drew
Quoted 13 lines · Andrew Jones
...
Quoted 9 lines
drivers/iommu/riscv/iommu.c static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu, · line 1142
Quoted 9 lines
drivers/iommu/riscv/iommu.c +5
-0@@ -1055,6 +1135,11 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,/* Invalidate device context after update */ riscv_iommu_cmd_iodir_inval_ddt (&cmd);riscv_iommu_cmd_iodir_set_did (&cmd, fwspec->ids[i]); +/* +* For now, the SVA and PASID features have not yet been merged, the +* default configuration is inval_pdt=false and pc=NULL. +*/ +riscv_iommu_iodir_iotinval (iommu, false, dc->iohgatp, dc,NULL );Shouldn't this iotinval come after the cmd-send below?
Yes, that's a typo on my side.
I’ll fix in v2.
Quoted 11 lines
Quoted 7 lines
Quoted 7 lines
drivers/iommu/riscv/iommu.c +0
-0riscv_iommu_cmd_send (iommu, &cmd); }Signature · 1 line
2.50.1Thanks,
drew
Thanks,
Fangyu
Quoted 30 lines
Quoted 30 lines
From: Fangyu Yu <[email protected]>
Add
riscv_iommu_iodir_iotinval()to perform required TLB and context cache invalidations after updating DDT or PDT entries, as mandated by the RISC-V IOMMU specification (Section 6.3.1 and 6.3.2).Signed-off-by Fangyu Yu <[email protected]>--- drivers/iommu/riscv/iommu.c | 85 +++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+)drivers/iommu/riscv/iommu.c +10
-0@@ -996,7 +996,82 @@ static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain, }#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) +* after modifying DDT or PDT entries +*/ +static void riscv_iommu_iodir_iotinval (struct riscv_iommu_device *iommu, +bool inval_pdt,unsigned long iohgatp, +struct riscv_iommu_dc *dc,struct riscv_iommu_pc *pc) +{ +struct riscv_iommu_command cmd;
Quoted 5 lines
drivers/iommu/riscv/iommu.c static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain, · line 1011
Quoted 2 lines
drivers/iommu/riscv/iommu.c +2
-0+if (FIELD_GET (RISCV_IOMMU_DC_IOHGATP_MODE , iohgatp) == +RISCV_IOMMU_DC_IOHGATP_MODE_BARE ) {nit: This condition can stick out. We have 100 chars.
Ack.
Quoted 6 lines
Quoted 6 lines
drivers/iommu/riscv/iommu.c +6
-0+if (inval_pdt) { +/* +* IOTINVAL.VMA with GV=AV=0, and PSCV=1, and +* PSCID=PC.PSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd);
Quoted 8 lines
drivers/iommu/riscv/iommu.c static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain, · line 1023
Quoted 6 lines
drivers/iommu/riscv/iommu.c +6
-0+riscv_iommu_cmd_inval_set_pscid (&cmd, +FIELD_GET (RISCV_IOMMU_PC_TA_PSCID , pc->ta)); + }else { +if (FIELD_GET (RISCV_IOMMU_DC_TC_PDTV , dc->tc) || ( +FIELD_GET (RISCV_IOMMU_DC_FSC_MODE , dc->fsc) == +RISCV_IOMMU_DC_FSC_MODE_BARE )) {nit: formatting
Ack.
Quoted 42 lines
if (FIELD_GET(RISCV_IOMMU_DC_TC_PDTV, dc->tc) || (FIELD_GET(RISCV_IOMMU_DC_FSC_MODE, dc->fsc) == RISCV_IOMMU_DC_FSC_MODE_BARE)) {Quoted 38 lines
drivers/iommu/riscv/iommu.c +38
-0+/* IOTINVAL.VMA with GV=AV=PSCV=0 */ +riscv_iommu_cmd_inval_vma (&cmd); + }else { +/* +* IOTINVAL.VMA with GV=AV=0, and PSCV=1, and +* PSCID=DC.ta.PSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd); +riscv_iommu_cmd_inval_set_pscid (&cmd, +FIELD_GET (RISCV_IOMMU_DC_TA_PSCID , dc->ta)); + } + } + }else { +if (inval_pdt) { +/* +* IOTINVAL.VMA with GV=1, AV=0, and PSCV=1, and +* GSCID=DC.iohgatp.GSCID, PSCID=PC.PSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd); +riscv_iommu_cmd_inval_set_gscid (&cmd, +FIELD_GET (RISCV_IOMMU_DC_IOHGATP_GSCID , iohgatp)); +riscv_iommu_cmd_inval_set_pscid (&cmd, +FIELD_GET (RISCV_IOMMU_PC_TA_PSCID , pc->ta)); + }else { +/* +* IOTINVAL.VMA with GV=1,AV=PSCV=0,and +* GSCID=DC.iohgatp.GSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd); +riscv_iommu_cmd_inval_set_gscid (&cmd, +FIELD_GET (RISCV_IOMMU_DC_IOHGATP_GSCID , iohgatp)); + +/* +* IOTINVAL.GVMA with GV=1,AV=0,and +* GSCID=DC.iohgatp.GSCID +*/ +/* +* For now, the Second-Stage feature have not yet been merged, so
Quoted 8 lines
drivers/iommu/riscv/iommu.c static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain, · line 1066
Quoted 5 lines
drivers/iommu/riscv/iommu.c +5
-0+* let's comment out the code first. +*/ +#if 0 +riscv_iommu_cmd_send (iommu, &cmd); +memset (&cmd,0 ,sizeof (cmd));The memset isn't necessary since
riscv_iommu_cmd_inval_gvma(), which doesn't yet exist, will overwrite dword0 and zero out dword1.
Agreed, I’ll drop memset in v2.
Quoted 36 lines
Quoted 36 lines
drivers/iommu/riscv/iommu.c +18
-0+riscv_iommu_cmd_inval_gvma (&cmd); +riscv_iommu_cmd_inval_set_gscid (&cmd, +FIELD_GET (RISCV_IOMMU_DC_IOHGATP_GSCID , iohgatp)); +#endif + } + } +riscv_iommu_cmd_send (iommu, &cmd); +}/* * Update IODIR for the device. * @@ -1031,6 +1106,11 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,riscv_iommu_cmd_iodir_inval_ddt (&cmd);riscv_iommu_cmd_iodir_set_did (&cmd, fwspec->ids[i]);riscv_iommu_cmd_send (iommu, &cmd); +/* +* For now, the SVA and PASID features have not yet been merged, the +* default configuration is inval_pdt=false and pc=NULL. +*/ +riscv_iommu_iodir_iotinval (iommu, false, dc->iohgatp, dc,NULL ); sync_required = true; }@@ -1055,6 +1135,11 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,/* Invalidate device context after update */ riscv_iommu_cmd_iodir_inval_ddt (&cmd);riscv_iommu_cmd_iodir_set_did (&cmd, fwspec->ids[i]); +/* +* For now, the SVA and PASID features have not yet been merged, the +* default configuration is inval_pdt=false and pc=NULL. +*/ +riscv_iommu_iodir_iotinval (iommu, false, dc->iohgatp, dc,NULL );riscv_iommu_cmd_send (iommu, &cmd); }
Quoted 7 lines
Quoted 3 lines
--
2.50.1A faithful implementation of the 6.3.1 and 6.3.2 guidelines for what the code currently supports. I presume this is fixing an issue? If so, can you point that out in the commit message?
Yes, this is fixing a functional issue: when software changes a leaf-level DDT or PDT entry we weren't issuing the required IOTINVAL. I'll update the commit message in v2.
Quoted 8 lines
Otherwise,
Reviewed-by Andrew Jones <[email protected]>Thanks,
drew
Thanks,
Fangyu
Hi fangyu,
Quoted 58 lines · On Thu, Jan 8, 2026 at 9:49 PM <[email protected]> wrote:
From: Fangyu Yu <[email protected]>
Add
riscv_iommu_iodir_iotinval()to perform required TLB and context cache invalidations after updating DDT or PDT entries, as mandated by the RISC-V IOMMU specification (Section 6.3.1 and 6.3.2).Signed-off-by Fangyu Yu <[email protected]>--- drivers/iommu/riscv/iommu.c | 85 +++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+)drivers/iommu/riscv/iommu.c +37
-0@@ -996,7 +996,82 @@ static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain, }#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) +* after modifying DDT or PDT entries +*/ +static void riscv_iommu_iodir_iotinval (struct riscv_iommu_device *iommu, +bool inval_pdt,unsigned long iohgatp, +struct riscv_iommu_dc *dc,struct riscv_iommu_pc *pc) +{ +struct riscv_iommu_command cmd; +if (FIELD_GET (RISCV_IOMMU_DC_IOHGATP_MODE , iohgatp) == +RISCV_IOMMU_DC_IOHGATP_MODE_BARE ) { +if (inval_pdt) { +/* +* IOTINVAL.VMA with GV=AV=0, and PSCV=1, and +* PSCID=PC.PSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd); +riscv_iommu_cmd_inval_set_pscid (&cmd, +FIELD_GET (RISCV_IOMMU_PC_TA_PSCID , pc->ta)); + }else { +if (FIELD_GET (RISCV_IOMMU_DC_TC_PDTV , dc->tc) || ( +FIELD_GET (RISCV_IOMMU_DC_FSC_MODE , dc->fsc) == +RISCV_IOMMU_DC_FSC_MODE_BARE )) { +/* IOTINVAL.VMA with GV=AV=PSCV=0 */ +riscv_iommu_cmd_inval_vma (&cmd); + }else { +/* +* IOTINVAL.VMA with GV=AV=0, and PSCV=1, and +* PSCID=DC.ta.PSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd); +riscv_iommu_cmd_inval_set_pscid (&cmd, +FIELD_GET (RISCV_IOMMU_DC_TA_PSCID , dc->ta)); + } + } + }else {
drivers/iommu/riscv/iommu.c static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain, · line 1046
Quoted 10 lines
drivers/iommu/riscv/iommu.c +10
-0+if (inval_pdt) { +/* +* IOTINVAL.VMA with GV=1, AV=0, and PSCV=1, and +* GSCID=DC.iohgatp.GSCID, PSCID=PC.PSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd); +riscv_iommu_cmd_inval_set_gscid (&cmd, +FIELD_GET (RISCV_IOMMU_DC_IOHGATP_GSCID , iohgatp)); +riscv_iommu_cmd_inval_set_pscid (&cmd, +FIELD_GET (RISCV_IOMMU_PC_TA_PSCID , pc->ta));
The riscv_iommu_cmd_inval_vma() and riscv_iommu_cmd_inval_set_pscid() could be moved out to prevent duplicate code.
Quoted 14 lines
drivers/iommu/riscv/iommu.c +14
-0+ }else { +/* +* IOTINVAL.VMA with GV=1,AV=PSCV=0,and +* GSCID=DC.iohgatp.GSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd); +riscv_iommu_cmd_inval_set_gscid (&cmd, +FIELD_GET (RISCV_IOMMU_DC_IOHGATP_GSCID , iohgatp)); + +/* +* IOTINVAL.GVMA with GV=1,AV=0,and +* GSCID=DC.iohgatp.GSCID +*/ +/*
drivers/iommu/riscv/iommu.c static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain, · line 1070
Quoted 10 lines
drivers/iommu/riscv/iommu.c +10
-0+* For now, the Second-Stage feature have not yet been merged, so +* let's comment out the code first. +*/ +#if 0 +riscv_iommu_cmd_send (iommu, &cmd); +memset (&cmd,0 ,sizeof (cmd)); +riscv_iommu_cmd_inval_gvma (&cmd); +riscv_iommu_cmd_inval_set_gscid (&cmd, +FIELD_GET (RISCV_IOMMU_DC_IOHGATP_GSCID , iohgatp)); +#endif
All the above should be removed from the patch; we don't need draft code.
Quoted 8 lines
drivers/iommu/riscv/iommu.c +4
-0+ } + } +riscv_iommu_cmd_send (iommu, &cmd); +}/* * Update IODIR for the device. *
drivers/iommu/riscv/iommu.c static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu, · line 1113
Quoted 8 lines
drivers/iommu/riscv/iommu.c +5
-0@@ -1031,6 +1106,11 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,riscv_iommu_cmd_iodir_inval_ddt (&cmd);riscv_iommu_cmd_iodir_set_did (&cmd, fwspec->ids[i]);riscv_iommu_cmd_send (iommu, &cmd); +/* +* For now, the SVA and PASID features have not yet been merged, the +* default configuration is inval_pdt=false and pc=NULL. +*/ +riscv_iommu_iodir_iotinval (iommu, false, dc->iohgatp, dc,NULL );
The riscv_iommu_iodir_iotinval() is the same level as riscv_iommu_iodir_update(). Could we move it out and put it after riscv_iommu_iodir_update()?
drivers/iommu/riscv/iommu.c static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,
Quoted 4 lines
drivers/iommu/riscv/iommu.c +0
-0sync_required = true; }@@ -1055,6 +1135,11 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,
Why do you have two riscv_iommu_iodir_update() function definitions?
drivers/iommu/riscv/iommu.c static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu, · line 1142
Quoted 8 lines
drivers/iommu/riscv/iommu.c +5
-0/* Invalidate device context after update */ riscv_iommu_cmd_iodir_inval_ddt (&cmd);riscv_iommu_cmd_iodir_set_did (&cmd, fwspec->ids[i]); +/* +* For now, the SVA and PASID features have not yet been merged, the +* default configuration is inval_pdt=false and pc=NULL. +*/ +riscv_iommu_iodir_iotinval (iommu, false, dc->iohgatp, dc,NULL );
If IOTLB invalidation occurs before DDT_CACHE invalidation, the IOTLB may use DDT_CACHE's stall info, which may cause IOTLB invalidation to fail.
Quoted 6 lines
drivers/iommu/riscv/iommu.c +0
-1riscv_iommu_cmd_send (iommu, &cmd); } --2.50.1
Signature · 2 lines
Best Regards Guo Ren
Quoted 65 lines
Hi fangyu,
Quoted 62 lines · On Thu, Jan 8, 2026 at 9:49 PM <[email protected]> wrote:
From: Fangyu Yu <[email protected]>
Add
riscv_iommu_iodir_iotinval()to perform required TLB and context cache invalidations after updating DDT or PDT entries, as mandated by the RISC-V IOMMU specification (Section 6.3.1 and 6.3.2).Signed-off-by Fangyu Yu <[email protected]>--- drivers/iommu/riscv/iommu.c | 85 +++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+)drivers/iommu/riscv/iommu.c +41
-0@@ -996,7 +996,82 @@ static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain, }#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) +* after modifying DDT or PDT entries +*/ +static void riscv_iommu_iodir_iotinval (struct riscv_iommu_device *iommu, +bool inval_pdt,unsigned long iohgatp, +struct riscv_iommu_dc *dc,struct riscv_iommu_pc *pc) +{ +struct riscv_iommu_command cmd; +if (FIELD_GET (RISCV_IOMMU_DC_IOHGATP_MODE , iohgatp) == +RISCV_IOMMU_DC_IOHGATP_MODE_BARE ) { +if (inval_pdt) { +/* +* IOTINVAL.VMA with GV=AV=0, and PSCV=1, and +* PSCID=PC.PSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd); +riscv_iommu_cmd_inval_set_pscid (&cmd, +FIELD_GET (RISCV_IOMMU_PC_TA_PSCID , pc->ta)); + }else { +if (FIELD_GET (RISCV_IOMMU_DC_TC_PDTV , dc->tc) || ( +FIELD_GET (RISCV_IOMMU_DC_FSC_MODE , dc->fsc) == +RISCV_IOMMU_DC_FSC_MODE_BARE )) { +/* IOTINVAL.VMA with GV=AV=PSCV=0 */ +riscv_iommu_cmd_inval_vma (&cmd); + }else { +/* +* IOTINVAL.VMA with GV=AV=0, and PSCV=1, and +* PSCID=DC.ta.PSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd); +riscv_iommu_cmd_inval_set_pscid (&cmd, +FIELD_GET (RISCV_IOMMU_DC_TA_PSCID , dc->ta)); + } + } + }else { +if (inval_pdt) { +/* +* IOTINVAL.VMA with GV=1, AV=0, and PSCV=1, and +* GSCID=DC.iohgatp.GSCID, PSCID=PC.PSCID
Quoted 8 lines
drivers/iommu/riscv/iommu.c static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain, · line 1046
Quoted 6 lines
drivers/iommu/riscv/iommu.c +6
-0+*/ +riscv_iommu_cmd_inval_vma (&cmd); +riscv_iommu_cmd_inval_set_gscid (&cmd, +FIELD_GET (RISCV_IOMMU_DC_IOHGATP_GSCID , iohgatp)); +riscv_iommu_cmd_inval_set_pscid (&cmd, +FIELD_GET (RISCV_IOMMU_PC_TA_PSCID , pc->ta));The
riscv_iommu_cmd_inval_vma()andriscv_iommu_cmd_inval_set_pscid()could be moved out to prevent duplicate code.
Thanks, Agreed on reducing duplication.
I'll move riscv_iommu_cmd_inval_vma() to a common path (build the VMA command once), and only conditionally call riscv_iommu_cmd_inval_set_pscid() when PSCV=1 is required by the spec.
Quoted 18 lines
Quoted 18 lines
drivers/iommu/riscv/iommu.c +17
-0+ }else { +/* +* IOTINVAL.VMA with GV=1,AV=PSCV=0,and +* GSCID=DC.iohgatp.GSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd); +riscv_iommu_cmd_inval_set_gscid (&cmd, +FIELD_GET (RISCV_IOMMU_DC_IOHGATP_GSCID , iohgatp)); + +/* +* IOTINVAL.GVMA with GV=1,AV=0,and +* GSCID=DC.iohgatp.GSCID +*/ +/* +* For now, the Second-Stage feature have not yet been merged, so +* let's comment out the code first. +*/
Quoted 8 lines
drivers/iommu/riscv/iommu.c static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain, · line 1070
Quoted 7 lines
drivers/iommu/riscv/iommu.c +7
-0+#if 0 +riscv_iommu_cmd_send (iommu, &cmd); +memset (&cmd,0 ,sizeof (cmd)); +riscv_iommu_cmd_inval_gvma (&cmd); +riscv_iommu_cmd_inval_set_gscid (&cmd, +FIELD_GET (RISCV_IOMMU_DC_IOHGATP_GSCID , iohgatp)); +#endif All the above should be removed from the patch; we don't need draft code.
Agreed, I will drop the entire #if 0 draft block and add a TODO here.
Quoted 12 lines
Quoted 12 lines
drivers/iommu/riscv/iommu.c +4
-0+ } + } +riscv_iommu_cmd_send (iommu, &cmd); +}/* * Update IODIR for the device. * @@ -1031,6 +1106,11 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,riscv_iommu_cmd_iodir_inval_ddt (&cmd);riscv_iommu_cmd_iodir_set_did (&cmd, fwspec->ids[i]);riscv_iommu_cmd_send (iommu, &cmd);
Quoted 8 lines
drivers/iommu/riscv/iommu.c static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu, · line 1113
Quoted 5 lines
drivers/iommu/riscv/iommu.c +5
-0+/* +* For now, the SVA and PASID features have not yet been merged, the +* default configuration is inval_pdt=false and pc=NULL. +*/ +riscv_iommu_iodir_iotinval (iommu, false, dc->iohgatp, dc,NULL );The
riscv_iommu_iodir_iotinval()is the same level asriscv_iommu_iodir_update(). Could we move it out and put it afterriscv_iommu_iodir_update()?
In riscv_iommu_iodir_update() we first clear the valid bit in dc->tc, an IOTINVAL is required immediately after modifying the DDT/PDT entry to make the change effective.
So riscv_iommu_iodir_iotinval() is paired with the DDT/PDT update sequence at the same level as riscv_iommu_cmd_iodir_inval_ddt(), and keeping it adjacent preserves the required ordering and avoids a window where stale cached context could be used.
Quoted 15 lines
drivers/iommu/riscv/iommu.c static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,
Quoted 5 lines
drivers/iommu/riscv/iommu.c +0
-0sync_required = true; }@@ -1055,6 +1135,11 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,Why do you have two
riscv_iommu_iodir_update()function definitions?drivers/iommu/riscv/iommu.c static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu, · line 1142
Quoted 8 lines
drivers/iommu/riscv/iommu.c +5
-0/* Invalidate device context after update */ riscv_iommu_cmd_iodir_inval_ddt (&cmd);riscv_iommu_cmd_iodir_set_did (&cmd, fwspec->ids[i]); +/* +* For now, the SVA and PASID features have not yet been merged, the +* default configuration is inval_pdt=false and pc=NULL. +*/ +riscv_iommu_iodir_iotinval (iommu, false, dc->iohgatp, dc,NULL );
Quoted 3 lines
If IOTLB invalidation occurs before
DDT_CACHEinvalidation, the IOTLB may use DDT_CACHE's stall info, which may cause IOTLB invalidation to fail.
You're right. I placed riscv_iommu_iodir_iotinval() before riscv_iommu_cmd_send() by mistake, I’ll fix the ordering in v2.
Quoted 11 lines
Quoted 7 lines
drivers/iommu/riscv/iommu.c +0
-1riscv_iommu_cmd_send (iommu, &cmd); } --2.50.1
Signature · 2 lines
Best Regards Guo Ren
Thanks,
Fangyu
Quoted 60 lines · On Thu, Jan 8, 2026 at 9:49 PM <[email protected]> wrote:
From: Fangyu Yu <[email protected]>
Add
riscv_iommu_iodir_iotinval()to perform required TLB and context cache invalidations after updating DDT or PDT entries, as mandated by the RISC-V IOMMU specification (Section 6.3.1 and 6.3.2).Signed-off-by Fangyu Yu <[email protected]>--- drivers/iommu/riscv/iommu.c | 85 +++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+)drivers/iommu/riscv/iommu.c +23
-0@@ -996,7 +996,82 @@ static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain, }#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) +* after modifying DDT or PDT entries +*/ +static void riscv_iommu_iodir_iotinval (struct riscv_iommu_device *iommu, +bool inval_pdt,unsigned long iohgatp, +struct riscv_iommu_dc *dc,struct riscv_iommu_pc *pc) +{ +struct riscv_iommu_command cmd; +if (FIELD_GET (RISCV_IOMMU_DC_IOHGATP_MODE , iohgatp) == +RISCV_IOMMU_DC_IOHGATP_MODE_BARE ) { +if (inval_pdt) { +/* +* IOTINVAL.VMA with GV=AV=0, and PSCV=1, and +* PSCID=PC.PSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd); +riscv_iommu_cmd_inval_set_pscid (&cmd, +FIELD_GET (RISCV_IOMMU_PC_TA_PSCID , pc->ta)); + }else { + if (FIELD_GET(RISCV_IOMMU_DC_TC_PDTV, dc->tc) || ( + FIELD_GET(RISCV_IOMMU_DC_FSC_MODE,
dc->fsc) ==drivers/iommu/riscv/iommu.c +6
-0+ RISCV_IOMMU_DC_FSC_MODE_BARE)) { + /* IOTINVAL.VMA with GV=AV=PSCV=0 */ + riscv_iommu_cmd_inval_vma(&cmd); + } else { +/* +* IOTINVAL.VMA with GV=AV=0, and PSCV=1, and
drivers/iommu/riscv/iommu.c +5
-0+* PSCID=DC.ta.PSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd); + riscv_iommu_cmd_inval_set_pscid(&cmd, + FIELD_GET(RISCV_IOMMU_DC_TA_PSCID,
dc->ta));
drivers/iommu/riscv/iommu.c static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain, · line 1042
Quoted 9 lines
drivers/iommu/riscv/iommu.c +9
-0+ } + } + }else { +if (inval_pdt) { +/* +* IOTINVAL.VMA with GV=1, AV=0, and PSCV=1, and +* GSCID=DC.iohgatp.GSCID, PSCID=PC.PSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd);
+ riscv_iommu_cmd_inval_set_gscid(&cmd,
Quoted 6 lines
+ FIELD_GET(RISCV_IOMMU_DC_IOHGATP_GSCID,
iohgatp));drivers/iommu/riscv/iommu.c +2
-0+ riscv_iommu_cmd_inval_set_pscid(&cmd, + FIELD_GET(RISCV_IOMMU_PC_TA_PSCID,
pc->ta));
The riscv_iommu_cmd_inval_set_pscid() and riscv_iommu_cmd_inval_vma() could be moved out to prevent duplicate code.
Quoted 17 lines
drivers/iommu/riscv/iommu.c +8
-0+ }else { +/* +* IOTINVAL.VMA with GV=1,AV=PSCV=0,and +* GSCID=DC.iohgatp.GSCID +*/ +riscv_iommu_cmd_inval_vma (&cmd); + riscv_iommu_cmd_inval_set_gscid(&cmd, + FIELD_GET(RISCV_IOMMU_DC_IOHGATP_GSCID,iohgatp));
drivers/iommu/riscv/iommu.c +7
-0+ +/* +* IOTINVAL.GVMA with GV=1,AV=0,and +* GSCID=DC.iohgatp.GSCID +*/ +/* +* For now, the Second-Stage feature have not yet been merged, so
drivers/iommu/riscv/iommu.c static void riscv_iommu_iotlb_inval(struct riscv_iommu_domain *domain, · line 1064
Quoted 3 lines
drivers/iommu/riscv/iommu.c +3
-0+* let's comment out the code first. +*/ +#if 0
+ riscv_iommu_cmd_send(iommu, &cmd);
Quoted 7 lines
drivers/iommu/riscv/iommu.c +4
-0+memset (&cmd,0 ,sizeof (cmd)); +riscv_iommu_cmd_inval_gvma (&cmd); + riscv_iommu_cmd_inval_set_gscid(&cmd, + FIELD_GET(RISCV_IOMMU_DC_IOHGATP_GSCID,iohgatp));
+#endif
All the above should be removed from the patch; we don't need draft code.
+ }
Quoted 17 lines
drivers/iommu/riscv/iommu.c +5
-0+ } +riscv_iommu_cmd_send (iommu, &cmd); +}/* * Update IODIR for the device. * @@ -1031,6 +1106,11 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,riscv_iommu_cmd_iodir_inval_ddt (&cmd);riscv_iommu_cmd_iodir_set_did (&cmd, fwspec->ids[i]);riscv_iommu_cmd_send (iommu, &cmd); +/* +* For now, the SVA and PASID features have not yet been merged, the
drivers/iommu/riscv/iommu.c +3
-0+* default configuration is inval_pdt=false and pc=NULL. +*/ + riscv_iommu_iodir_iotinval(iommu, false, dc->iohgatp, dc,
Quoted 2 lines
NULL);
The riscv_iommu_iodir_iotinval() is the same level asriscv_iommu_iodir_update(). Could we move it out and put it after riscv_iommu_iodir_update()?
sync_required = true;
drivers/iommu/riscv/iommu.c static void riscv_iommu_iodir_update(struct · line 1136
Quoted 5 lines
}drivers/iommu/riscv/iommu.c +0
-0@@ -1055,6 +1135,11 @@ static void riscv_iommu_iodir_update(struct riscv_iommu_device *iommu,
Why do you have two riscv_iommu_iodir_update() function definitions?
Quoted 9 lines
drivers/iommu/riscv/iommu.c +2
-0/* Invalidate device context after update */ riscv_iommu_cmd_iodir_inval_ddt (&cmd);riscv_iommu_cmd_iodir_set_did (&cmd, fwspec->ids[i]); +/* +* For now, the SVA and PASID features have not yet been merged, the
drivers/iommu/riscv/iommu.c +3
-0+* default configuration is inval_pdt=false and pc=NULL. +*/ + riscv_iommu_iodir_iotinval(iommu, false, dc->iohgatp, dc,
Quoted 3 lines
NULL);
riscv_iommu_cmd_send(iommu, &cmd);If IOTLB invalidation occurs before DDT_CACHE invalidation, the IOTLB may use DDT_CACHE's stall info, which may cause IOTLB invalidation to fail.
Quoted 6 lines
}--
2.50.1
Signature · 2 lines
Best Regards Guo Ren
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv