hw/riscv/riscv-iommu.c: Introduce a translation tag for the page table cache
PATCH 1/1 hw/riscv/riscv-iommu.c: Introduce a translation tag for the page table cache
This commit introduces a translation tag to avoid invalidating an entry that should not be invalidated when IOMMU executes invalidation commands. E.g. IOTINVAL.VMA with GV=0, AV=0, PSCV=1 invalidates both a mapping of single stage translation and a mapping of nested translation with the same PSCID, but only the former one should be invalidated.
--- hw/riscv/riscv-iommu.c | 205 ++++++++++++++++++++++++++++++----------- 1 file changed, 153 insertions(+), 52 deletions(-)
hw/riscv/riscv-iommu.c +153 -52
@@ -64,8 +64,16 @@ struct RISCVIOMMUContext {uint64_t msiptp;/* MSI redirection page table pointer */ }; +typedef enum RISCVIOMMUTransTag { +RISCV_IOMMU_TRANS_TAG_BY ,/* Bypass */ +RISCV_IOMMU_TRANS_TAG_SS ,/* Single Stage */ +RISCV_IOMMU_TRANS_TAG_VG ,/* G-stage only */ +RISCV_IOMMU_TRANS_TAG_VN ,/* Nested translation */ +}RISCVIOMMUTransTag ; +/* Address translation cache entry */ struct RISCVIOMMUEntry { +RISCVIOMMUTransTag tag;/* Translation Tag */ uint64_t iova:44 ;/* IOVA Page Number */ uint64_t pscid:20 ;/* Process Soft-Context identifier */ uint64_t phys:44 ;/* Physical Page Number */
@@ -1228,7 +1236,7 @@ static gboolean riscv_iommu_iot_equal(gconstpointer v1, gconstpointer v2)RISCVIOMMUEntry *t1 = (RISCVIOMMUEntry *) v1;RISCVIOMMUEntry *t2 = (RISCVIOMMUEntry *) v2;return t1->gscid == t2->gscid && t1->pscid == t2->pscid && - t1->iova == t2->iova; + t1->iova == t2->iova && t1->tag == t2->tag; }static guint riscv_iommu_iot_hash (gconstpointer v)
@@ -1237,67 +1245,115 @@ static guint riscv_iommu_iot_hash(gconstpointer v)return (guint )t->iova; } -/* GV: 1 PSCV: 1 AV: 1 */ +/* GV: 0 AV: 0 PSCV: 0 GVMA: 0 */ +/* GV: 0 AV: 0 GVMA: 1 */ +static +void riscv_iommu_iot_inval_all (gpointer key,gpointer value,gpointer data) +{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 0 AV: 0 PSCV: 1 GVMA: 0 */ +static +void riscv_iommu_iot_inval_pscid (gpointer key,gpointer value,gpointer data) +{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag && + iot->pscid == arg->pscid) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 0 AV: 1 PSCV: 0 GVMA: 0 */ +static +void riscv_iommu_iot_inval_iova (gpointer key,gpointer value,gpointer data) +{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag && + iot->iova == arg->iova) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 0 AV: 1 PSCV: 1 GVMA: 0 */ static void riscv_iommu_iot_inval_pscid_iova (gpointer key,gpointer value,gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid && +if (iot->tag == arg->tag && iot->pscid == arg->pscid && iot->iova == arg->iova) { iot->perm =IOMMU_NONE ; } } -/* GV: 1 PSCV: 1 AV: 0 */ -static void riscv_iommu_iot_inval_pscid (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 0 PSCV: 0 GVMA: 0 */ +/* GV: 1 AV: 0 GVMA: 1 */ +static +void riscv_iommu_iot_inval_gscid (gpointer key,gpointer value,gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid && - iot->pscid == arg->pscid) { +if (iot->tag == arg->tag && + iot->gscid == arg->gscid) { iot->perm =IOMMU_NONE ; } } -/* GV: 1 GVMA: 1 */ -static void riscv_iommu_iot_inval_gscid_gpa (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 0 PSCV: 1 GVMA: 0 */ +static void riscv_iommu_iot_inval_gscid_pscid (gpointer key,gpointer value, +gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid) { -/* simplified cache, no GPA matching */ +if (iot->tag == arg->tag && + iot->gscid == arg->gscid && + iot->pscid == arg->pscid) { iot->perm =IOMMU_NONE ; } } -/* GV: 1 GVMA: 0 */ -static void riscv_iommu_iot_inval_gscid (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 1 PSCV: 0 GVMA: 0 */ +/* GV: 1 AV: 1 GVMA: 1 */ +static void riscv_iommu_iot_inval_gscid_iova (gpointer key,gpointer value, +gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid) { +if (iot->tag == arg->tag && + iot->gscid == arg->gscid && + iot->iova == arg->iova) { iot->perm =IOMMU_NONE ; } } -/* GV: 0 */ -static void riscv_iommu_iot_inval_all (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 1 PSCV: 1 GVMA: 0 */ +static void riscv_iommu_iot_inval_gscid_pscid_iova (gpointer key,gpointer value, +gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; - iot->perm =IOMMU_NONE ; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag && + iot->gscid == arg->gscid && + iot->pscid == arg->pscid && + iot->iova == arg->iova) { + iot->perm =IOMMU_NONE ; + } }/* caller should keep ref-count for iot_cache object */ static RISCVIOMMUEntry *riscv_iommu_iot_lookup (RISCVIOMMUContext *ctx, -GHashTable *iot_cache,hwaddr iova) +GHashTable *iot_cache,hwaddr iova,RISCVIOMMUTransTag transtag) {RISCVIOMMUEntry key = { + .tag = transtag, .gscid =get_field (ctx->gatp,RISCV_IOMMU_DC_IOHGATP_GSCID ), .pscid =get_field (ctx->ta,RISCV_IOMMU_DC_TA_PSCID ), .iova =PPN_DOWN (iova),
@@ -1323,10 +1379,11 @@ static void riscv_iommu_iot_update(RISCVIOMMUState *s, }static void riscv_iommu_iot_inval (RISCVIOMMUState *s,GHFunc func, -uint32_t gscid,uint32_t pscid,hwaddr iova) +uint32_t gscid,uint32_t pscid,hwaddr iova,RISCVIOMMUTransTag transtag) {GHashTable *iot_cache;RISCVIOMMUEntry key = { + .tag = transtag, .gscid = gscid, .pscid = pscid, .iova =PPN_DOWN (iova),
@@ -1337,9 +1394,24 @@ static void riscv_iommu_iot_inval(RISCVIOMMUState *s, GHFunc func,g_hash_table_unref (iot_cache); } +static RISCVIOMMUTransTag riscv_iommu_get_transtag (RISCVIOMMUContext *ctx) +{ +uint64_t satp =get_field (ctx->satp,RISCV_IOMMU_ATP_MODE_FIELD ); +uint64_t gatp =get_field (ctx->gatp,RISCV_IOMMU_ATP_MODE_FIELD ); + +if (satp ==RISCV_IOMMU_DC_FSC_MODE_BARE ) { +return (gatp ==RISCV_IOMMU_DC_IOHGATP_MODE_BARE ) ? +RISCV_IOMMU_TRANS_TAG_BY :RISCV_IOMMU_TRANS_TAG_VG ; + }else { +return (gatp ==RISCV_IOMMU_DC_IOHGATP_MODE_BARE ) ? +RISCV_IOMMU_TRANS_TAG_SS :RISCV_IOMMU_TRANS_TAG_VN ; + } +} +static int riscv_iommu_translate (RISCVIOMMUState *s,RISCVIOMMUContext *ctx,IOMMUTLBEntry *iotlb,bool enable_cache) { +RISCVIOMMUTransTag transtag =riscv_iommu_get_transtag (ctx);RISCVIOMMUEntry *iot;IOMMUAccessFlags perm;bool enable_pid;
@@ -1365,7 +1437,7 @@ static int riscv_iommu_translate(RISCVIOMMUState *s, RISCVIOMMUContext *ctx, } } - iot =riscv_iommu_iot_lookup (ctx, iot_cache, iotlb->iova); + iot =riscv_iommu_iot_lookup (ctx, iot_cache, iotlb->iova, transtag); perm = iot ? iot->perm :IOMMU_NONE ;if (perm !=IOMMU_NONE ) { iotlb->translated_addr =PPN_PHYS (iot->phys);
@@ -1396,6 +1468,7 @@ static int riscv_iommu_translate(RISCVIOMMUState *s, RISCVIOMMUContext *ctx, iot->gscid =get_field (ctx->gatp,RISCV_IOMMU_DC_IOHGATP_GSCID ); iot->pscid =get_field (ctx->ta,RISCV_IOMMU_DC_TA_PSCID ); iot->perm = iotlb->perm; + iot->tag = transtag;riscv_iommu_iot_update (s, iot_cache, iot); }
@@ -1603,44 +1676,72 @@ static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)case RISCV_IOMMU_CMD (RISCV_IOMMU_CMD_IOTINVAL_FUNC_GVMA ,RISCV_IOMMU_CMD_IOTINVAL_OPCODE ): -if (cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV ) { + { +bool gv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV ); +bool av = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV ); +bool pscv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV ); +uint32_t gscid =get_field (cmd.dword0, +RISCV_IOMMU_CMD_IOTINVAL_GSCID ); +uint32_t pscid =get_field (cmd.dword0, +RISCV_IOMMU_CMD_IOTINVAL_PSCID ); +hwaddr iova = (cmd.dword1 <<2 ) &TARGET_PAGE_MASK ; + +if (pscv) {/* illegal command arguments IOTINVAL.GVMA & PSCV == 1 */ goto cmd_ill; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV )) { -/* invalidate all cache mappings */ - func = riscv_iommu_iot_inval_all; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV )) { -/* invalidate cache matching GSCID */ - func = riscv_iommu_iot_inval_gscid; - }else { -/* invalidate cache matching GSCID and ADDR (GPA) */ - func = riscv_iommu_iot_inval_gscid_gpa; } -riscv_iommu_iot_inval (s, func, -get_field (cmd.dword0,RISCV_IOMMU_CMD_IOTINVAL_GSCID ),0 , - cmd.dword1 <<2 &TARGET_PAGE_MASK ); + + func = riscv_iommu_iot_inval_all; + +if (gv) { + func = (av) ? riscv_iommu_iot_inval_gscid_iova : + riscv_iommu_iot_inval_gscid; + } + +riscv_iommu_iot_inval ( + s, func, gscid, pscid, iova,RISCV_IOMMU_TRANS_TAG_VG ); + +riscv_iommu_iot_inval ( + s, func, gscid, pscid, iova,RISCV_IOMMU_TRANS_TAG_VN );break ; + }case RISCV_IOMMU_CMD (RISCV_IOMMU_CMD_IOTINVAL_FUNC_VMA ,RISCV_IOMMU_CMD_IOTINVAL_OPCODE ): -if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV )) { -/* invalidate all cache mappings, simplified model */ - func = riscv_iommu_iot_inval_all; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV )) { -/* invalidate cache matching GSCID, simplified model */ - func = riscv_iommu_iot_inval_gscid; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV )) { -/* invalidate cache matching GSCID and PSCID */ - func = riscv_iommu_iot_inval_pscid; + { +bool gv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV ); +bool av = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV ); +bool pscv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV ); +uint32_t gscid =get_field (cmd.dword0, +RISCV_IOMMU_CMD_IOTINVAL_GSCID ); +uint32_t pscid =get_field (cmd.dword0, +RISCV_IOMMU_CMD_IOTINVAL_PSCID ); +hwaddr iova = (cmd.dword1 <<2 ) &TARGET_PAGE_MASK ; +RISCVIOMMUTransTag transtag; + +if (gv) { + transtag =RISCV_IOMMU_TRANS_TAG_VN ; +if (pscv) { + func = (av) ? riscv_iommu_iot_inval_gscid_pscid_iova : + riscv_iommu_iot_inval_gscid_pscid; + }else { + func = (av) ? riscv_iommu_iot_inval_gscid_iova : + riscv_iommu_iot_inval_gscid; + } }else { -/* invalidate cache matching GSCID and PSCID and ADDR (IOVA) */ - func = riscv_iommu_iot_inval_pscid_iova; + transtag =RISCV_IOMMU_TRANS_TAG_SS ; +if (pscv) { + func = (av) ? riscv_iommu_iot_inval_pscid_iova : + riscv_iommu_iot_inval_pscid; + }else { + func = (av) ? riscv_iommu_iot_inval_iova : + riscv_iommu_iot_inval_all; + } } -riscv_iommu_iot_inval (s, func, -get_field (cmd.dword0,RISCV_IOMMU_CMD_IOTINVAL_GSCID ), -get_field (cmd.dword0,RISCV_IOMMU_CMD_IOTINVAL_PSCID ), - cmd.dword1 <<2 &TARGET_PAGE_MASK ); + +riscv_iommu_iot_inval (s, func, gscid, pscid, iova, transtag);break ; + }case RISCV_IOMMU_CMD (RISCV_IOMMU_CMD_IODIR_FUNC_INVAL_DDT ,RISCV_IOMMU_CMD_IODIR_OPCODE ):
Signature · 1 line
2.43.2
CCing Tomasz
Quoted 8 lines · Jason Chien
This commit introduces a translation tag to avoid invalidating an entry that should not be invalidated when IOMMU executes invalidation commands. E.g. IOTINVAL.VMA with GV=0, AV=0, PSCV=1 invalidates both a mapping of single stage translation and a mapping of nested translation with the same PSCID, but only the former one should be invalidated.
Signed-off-by Jason Chien <[email protected]>---
LGTM but I would like to hear from Tomasz if adding this new abstraction is the best way to what seems to be a bug in riscv_iommu_process_cq_tail().
Daniel
Quoted 326 lines
hw/riscv/riscv-iommu.c | 205 ++++++++++++++++++++++++++++++----------- 1 file changed, 153 insertions(+), 52 deletions(-)hw/riscv/riscv-iommu.c +153
-52@@ -64,8 +64,16 @@ struct RISCVIOMMUContext {uint64_t msiptp;/* MSI redirection page table pointer */ }; +typedef enum RISCVIOMMUTransTag { +RISCV_IOMMU_TRANS_TAG_BY ,/* Bypass */ +RISCV_IOMMU_TRANS_TAG_SS ,/* Single Stage */ +RISCV_IOMMU_TRANS_TAG_VG ,/* G-stage only */ +RISCV_IOMMU_TRANS_TAG_VN ,/* Nested translation */ +}RISCVIOMMUTransTag ; +/* Address translation cache entry */ struct RISCVIOMMUEntry { +RISCVIOMMUTransTag tag;/* Translation Tag */ uint64_t iova:44 ;/* IOVA Page Number */ uint64_t pscid:20 ;/* Process Soft-Context identifier */ uint64_t phys:44 ;/* Physical Page Number */ @@ -1228,7 +1236,7 @@ static gboolean riscv_iommu_iot_equal(gconstpointer v1, gconstpointer v2)RISCVIOMMUEntry *t1 = (RISCVIOMMUEntry *) v1;RISCVIOMMUEntry *t2 = (RISCVIOMMUEntry *) v2;return t1->gscid == t2->gscid && t1->pscid == t2->pscid && - t1->iova == t2->iova; + t1->iova == t2->iova && t1->tag == t2->tag; }static guint riscv_iommu_iot_hash (gconstpointer v)@@ -1237,67 +1245,115 @@ static guint riscv_iommu_iot_hash(gconstpointer v)return (guint )t->iova; } -/* GV: 1 PSCV: 1 AV: 1 */ +/* GV: 0 AV: 0 PSCV: 0 GVMA: 0 */ +/* GV: 0 AV: 0 GVMA: 1 */ +static +void riscv_iommu_iot_inval_all (gpointer key,gpointer value,gpointer data) +{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 0 AV: 0 PSCV: 1 GVMA: 0 */ +static +void riscv_iommu_iot_inval_pscid (gpointer key,gpointer value,gpointer data) +{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag && + iot->pscid == arg->pscid) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 0 AV: 1 PSCV: 0 GVMA: 0 */ +static +void riscv_iommu_iot_inval_iova (gpointer key,gpointer value,gpointer data) +{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag && + iot->iova == arg->iova) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 0 AV: 1 PSCV: 1 GVMA: 0 */ static void riscv_iommu_iot_inval_pscid_iova (gpointer key,gpointer value,gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid && +if (iot->tag == arg->tag && iot->pscid == arg->pscid && iot->iova == arg->iova) { iot->perm =IOMMU_NONE ; } } -/* GV: 1 PSCV: 1 AV: 0 */ -static void riscv_iommu_iot_inval_pscid (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 0 PSCV: 0 GVMA: 0 */ +/* GV: 1 AV: 0 GVMA: 1 */ +static +void riscv_iommu_iot_inval_gscid (gpointer key,gpointer value,gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid && - iot->pscid == arg->pscid) { +if (iot->tag == arg->tag && + iot->gscid == arg->gscid) { iot->perm =IOMMU_NONE ; } } -/* GV: 1 GVMA: 1 */ -static void riscv_iommu_iot_inval_gscid_gpa (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 0 PSCV: 1 GVMA: 0 */ +static void riscv_iommu_iot_inval_gscid_pscid (gpointer key,gpointer value, +gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid) { -/* simplified cache, no GPA matching */ +if (iot->tag == arg->tag && + iot->gscid == arg->gscid && + iot->pscid == arg->pscid) { iot->perm =IOMMU_NONE ; } } -/* GV: 1 GVMA: 0 */ -static void riscv_iommu_iot_inval_gscid (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 1 PSCV: 0 GVMA: 0 */ +/* GV: 1 AV: 1 GVMA: 1 */ +static void riscv_iommu_iot_inval_gscid_iova (gpointer key,gpointer value, +gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid) { +if (iot->tag == arg->tag && + iot->gscid == arg->gscid && + iot->iova == arg->iova) { iot->perm =IOMMU_NONE ; } } -/* GV: 0 */ -static void riscv_iommu_iot_inval_all (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 1 PSCV: 1 GVMA: 0 */ +static void riscv_iommu_iot_inval_gscid_pscid_iova (gpointer key,gpointer value, +gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; - iot->perm =IOMMU_NONE ; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag && + iot->gscid == arg->gscid && + iot->pscid == arg->pscid && + iot->iova == arg->iova) { + iot->perm =IOMMU_NONE ; + } }/* caller should keep ref-count for iot_cache object */ static RISCVIOMMUEntry *riscv_iommu_iot_lookup (RISCVIOMMUContext *ctx, -GHashTable *iot_cache,hwaddr iova) +GHashTable *iot_cache,hwaddr iova,RISCVIOMMUTransTag transtag) {RISCVIOMMUEntry key = { + .tag = transtag, .gscid =get_field (ctx->gatp,RISCV_IOMMU_DC_IOHGATP_GSCID ), .pscid =get_field (ctx->ta,RISCV_IOMMU_DC_TA_PSCID ), .iova =PPN_DOWN (iova),@@ -1323,10 +1379,11 @@ static void riscv_iommu_iot_update(RISCVIOMMUState *s, }static void riscv_iommu_iot_inval (RISCVIOMMUState *s,GHFunc func, -uint32_t gscid,uint32_t pscid,hwaddr iova) +uint32_t gscid,uint32_t pscid,hwaddr iova,RISCVIOMMUTransTag transtag) {GHashTable *iot_cache;RISCVIOMMUEntry key = { + .tag = transtag, .gscid = gscid, .pscid = pscid, .iova =PPN_DOWN (iova),@@ -1337,9 +1394,24 @@ static void riscv_iommu_iot_inval(RISCVIOMMUState *s, GHFunc func,g_hash_table_unref (iot_cache); } +static RISCVIOMMUTransTag riscv_iommu_get_transtag (RISCVIOMMUContext *ctx) +{ +uint64_t satp =get_field (ctx->satp,RISCV_IOMMU_ATP_MODE_FIELD ); +uint64_t gatp =get_field (ctx->gatp,RISCV_IOMMU_ATP_MODE_FIELD ); + +if (satp ==RISCV_IOMMU_DC_FSC_MODE_BARE ) { +return (gatp ==RISCV_IOMMU_DC_IOHGATP_MODE_BARE ) ? +RISCV_IOMMU_TRANS_TAG_BY :RISCV_IOMMU_TRANS_TAG_VG ; + }else { +return (gatp ==RISCV_IOMMU_DC_IOHGATP_MODE_BARE ) ? +RISCV_IOMMU_TRANS_TAG_SS :RISCV_IOMMU_TRANS_TAG_VN ; + } +} +static int riscv_iommu_translate (RISCVIOMMUState *s,RISCVIOMMUContext *ctx,IOMMUTLBEntry *iotlb,bool enable_cache) { +RISCVIOMMUTransTag transtag =riscv_iommu_get_transtag (ctx);RISCVIOMMUEntry *iot;IOMMUAccessFlags perm;bool enable_pid;@@ -1365,7 +1437,7 @@ static int riscv_iommu_translate(RISCVIOMMUState *s, RISCVIOMMUContext *ctx, } } - iot =riscv_iommu_iot_lookup (ctx, iot_cache, iotlb->iova); + iot =riscv_iommu_iot_lookup (ctx, iot_cache, iotlb->iova, transtag); perm = iot ? iot->perm :IOMMU_NONE ;if (perm !=IOMMU_NONE ) { iotlb->translated_addr =PPN_PHYS (iot->phys);@@ -1396,6 +1468,7 @@ static int riscv_iommu_translate(RISCVIOMMUState *s, RISCVIOMMUContext *ctx, iot->gscid =get_field (ctx->gatp,RISCV_IOMMU_DC_IOHGATP_GSCID ); iot->pscid =get_field (ctx->ta,RISCV_IOMMU_DC_TA_PSCID ); iot->perm = iotlb->perm; + iot->tag = transtag;riscv_iommu_iot_update (s, iot_cache, iot); }@@ -1603,44 +1676,72 @@ static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)case RISCV_IOMMU_CMD (RISCV_IOMMU_CMD_IOTINVAL_FUNC_GVMA ,RISCV_IOMMU_CMD_IOTINVAL_OPCODE ): -if (cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV ) { + { +bool gv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV ); +bool av = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV ); +bool pscv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV ); +uint32_t gscid =get_field (cmd.dword0, +RISCV_IOMMU_CMD_IOTINVAL_GSCID ); +uint32_t pscid =get_field (cmd.dword0, +RISCV_IOMMU_CMD_IOTINVAL_PSCID ); +hwaddr iova = (cmd.dword1 <<2 ) &TARGET_PAGE_MASK ; + +if (pscv) {/* illegal command arguments IOTINVAL.GVMA & PSCV == 1 */ goto cmd_ill; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV )) { -/* invalidate all cache mappings */ - func = riscv_iommu_iot_inval_all; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV )) { -/* invalidate cache matching GSCID */ - func = riscv_iommu_iot_inval_gscid; - }else { -/* invalidate cache matching GSCID and ADDR (GPA) */ - func = riscv_iommu_iot_inval_gscid_gpa; } -riscv_iommu_iot_inval (s, func, -get_field (cmd.dword0,RISCV_IOMMU_CMD_IOTINVAL_GSCID ),0 , - cmd.dword1 <<2 &TARGET_PAGE_MASK ); + + func = riscv_iommu_iot_inval_all; + +if (gv) { + func = (av) ? riscv_iommu_iot_inval_gscid_iova : + riscv_iommu_iot_inval_gscid; + } + +riscv_iommu_iot_inval ( + s, func, gscid, pscid, iova,RISCV_IOMMU_TRANS_TAG_VG ); + +riscv_iommu_iot_inval ( + s, func, gscid, pscid, iova,RISCV_IOMMU_TRANS_TAG_VN );break ; + }case RISCV_IOMMU_CMD (RISCV_IOMMU_CMD_IOTINVAL_FUNC_VMA ,RISCV_IOMMU_CMD_IOTINVAL_OPCODE ): -if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV )) { -/* invalidate all cache mappings, simplified model */ - func = riscv_iommu_iot_inval_all; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV )) { -/* invalidate cache matching GSCID, simplified model */ - func = riscv_iommu_iot_inval_gscid; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV )) { -/* invalidate cache matching GSCID and PSCID */ - func = riscv_iommu_iot_inval_pscid; + { +bool gv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV ); +bool av = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV ); +bool pscv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV ); +uint32_t gscid =get_field (cmd.dword0, +RISCV_IOMMU_CMD_IOTINVAL_GSCID ); +uint32_t pscid =get_field (cmd.dword0, +RISCV_IOMMU_CMD_IOTINVAL_PSCID ); +hwaddr iova = (cmd.dword1 <<2 ) &TARGET_PAGE_MASK ; +RISCVIOMMUTransTag transtag; + +if (gv) { + transtag =RISCV_IOMMU_TRANS_TAG_VN ; +if (pscv) { + func = (av) ? riscv_iommu_iot_inval_gscid_pscid_iova : + riscv_iommu_iot_inval_gscid_pscid; + }else { + func = (av) ? riscv_iommu_iot_inval_gscid_iova : + riscv_iommu_iot_inval_gscid; + } }else { -/* invalidate cache matching GSCID and PSCID and ADDR (IOVA) */ - func = riscv_iommu_iot_inval_pscid_iova; + transtag =RISCV_IOMMU_TRANS_TAG_SS ; +if (pscv) { + func = (av) ? riscv_iommu_iot_inval_pscid_iova : + riscv_iommu_iot_inval_pscid; + }else { + func = (av) ? riscv_iommu_iot_inval_iova : + riscv_iommu_iot_inval_all; + } } -riscv_iommu_iot_inval (s, func, -get_field (cmd.dword0,RISCV_IOMMU_CMD_IOTINVAL_GSCID ), -get_field (cmd.dword0,RISCV_IOMMU_CMD_IOTINVAL_PSCID ), - cmd.dword1 <<2 &TARGET_PAGE_MASK ); + +riscv_iommu_iot_inval (s, func, gscid, pscid, iova, transtag);break ; + }case RISCV_IOMMU_CMD (RISCV_IOMMU_CMD_IODIR_FUNC_INVAL_DDT ,RISCV_IOMMU_CMD_IODIR_OPCODE ):
Hi Tomasz, any thoughs?
Daniel Henrique Barboza <[email protected]> 於 2024年11月12日 週二 下午8:26寫道:
Quoted 375 lines
CCing Tomasz
Quoted 8 lines · Jason Chien
This commit introduces a translation tag to avoid invalidating an entry that should not be invalidated when IOMMU executes invalidation commands. E.g. IOTINVAL.VMA with GV=0, AV=0, PSCV=1 invalidates both a mapping of single stage translation and a mapping of nested translation with the same PSCID, but only the former one should be invalidated.
Signed-off-by Jason Chien <[email protected]>---
LGTM but I would like to hear from Tomasz if adding this new abstraction is the best way to what seems to be a bug in
riscv_iommu_process_cq_tail().Daniel
hw/riscv/riscv-iommu.c struct RISCVIOMMUContext { · line 64
Quoted 9 lines
hw/riscv/riscv-iommu.c | 205 ++++++++++++++++++++++++++++++----------- 1 file changed, 153 insertions(+), 52 deletions(-)hw/riscv/riscv-iommu.c +0
-0@@ -64,8 +64,16 @@ struct RISCVIOMMUContext {uint64_t msiptp;/* MSI redirection page table pointer */
hw/riscv/riscv-iommu.c static gboolean
Quoted 16 lines
hw/riscv/riscv-iommu.c +8
-0}; +typedef enum RISCVIOMMUTransTag { +RISCV_IOMMU_TRANS_TAG_BY ,/* Bypass */ +RISCV_IOMMU_TRANS_TAG_SS ,/* Single Stage */ +RISCV_IOMMU_TRANS_TAG_VG ,/* G-stage only */ +RISCV_IOMMU_TRANS_TAG_VN ,/* Nested translation */ +}RISCVIOMMUTransTag ; +/* Address translation cache entry */ struct RISCVIOMMUEntry { +RISCVIOMMUTransTag tag;/* Translation Tag */ uint64_t iova:44 ;/* IOVA Page Number */ uint64_t pscid:20 ;/* Process Soft-Context identifier */ uint64_t phys:44 ;/* Physical Page Number */ @@ -1228,7 +1236,7 @@ static gboolean
riscv_iommu_iot_equal(gconstpointer v1, gconstpointer v2)hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer
Quoted 9 lines
hw/riscv/riscv-iommu.c +1
-1RISCVIOMMUEntry *t1 = (RISCVIOMMUEntry *) v1;RISCVIOMMUEntry *t2 = (RISCVIOMMUEntry *) v2;return t1->gscid == t2->gscid && t1->pscid == t2->pscid && - t1->iova == t2->iova; + t1->iova == t2->iova && t1->tag == t2->tag; }static guint riscv_iommu_iot_hash (gconstpointer v)@@ -1237,67 +1245,115 @@ static guint riscv_iommu_iot_hash(gconstpointerv)
hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 8 lines
hw/riscv/riscv-iommu.c +4
-1return (guint )t->iova; } -/* GV: 1 PSCV: 1 AV: 1 */ +/* GV: 0 AV: 0 PSCV: 0 GVMA: 0 */ +/* GV: 0 AV: 0 GVMA: 1 */ +static +void riscv_iommu_iot_inval_all(gpointer key, gpointer value, gpointerdata)
hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 11 lines
hw/riscv/riscv-iommu.c +11
-0+{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 0 AV: 0 PSCV: 1 GVMA: 0 */ +static +void riscv_iommu_iot_inval_pscid(gpointer key, gpointer value, gpointerdata)
hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 12 lines
hw/riscv/riscv-iommu.c +12
-0+{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag && + iot->pscid == arg->pscid) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 0 AV: 1 PSCV: 0 GVMA: 0 */ +static +void riscv_iommu_iot_inval_iova(gpointer key, gpointer value, gpointerdata)
hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 11 lines
hw/riscv/riscv-iommu.c +10
-0+{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag && + iot->iova == arg->iova) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 0 AV: 1 PSCV: 1 GVMA: 0 */ static void riscv_iommu_iot_inval_pscid_iova(gpointer key, gpointervalue,
Quoted 9 lines
hw/riscv/riscv-iommu.c +1
-1gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid && +if (iot->tag == arg->tag && iot->pscid == arg->pscid && iot->iova == arg->iova) { iot->perm =IOMMU_NONE ;hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 10 lines
hw/riscv/riscv-iommu.c +4
-3} } -/* GV: 1 PSCV: 1 AV: 0 */ -static void riscv_iommu_iot_inval_pscid (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 0 PSCV: 0 GVMA: 0 */ +/* GV: 1 AV: 0 GVMA: 1 */ +static +void riscv_iommu_iot_inval_gscid(gpointer key, gpointer value, gpointerdata)
hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 13 lines
hw/riscv/riscv-iommu.c +2
-4{RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid && - iot->pscid == arg->pscid) { +if (iot->tag == arg->tag && + iot->gscid == arg->gscid) { iot->perm =IOMMU_NONE ; } } -/* GV: 1 GVMA: 1 */ -static void riscv_iommu_iot_inval_gscid_gpa(gpointer key, gpointervalue,
hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 3 lines
hw/riscv/riscv-iommu.c +2
-1- gpointer data) +/* GV: 1 AV: 0 PSCV: 1 GVMA: 0 */ +static void riscv_iommu_iot_inval_gscid_pscid(gpointer key, gpointervalue,
Quoted 9 lines
hw/riscv/riscv-iommu.c +4
-2+ gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid) { -/* simplified cache, no GPA matching */ +if (iot->tag == arg->tag && + iot->gscid == arg->gscid && + iot->pscid == arg->pscid) {hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 10 lines
hw/riscv/riscv-iommu.c +3
-3iot->perm =IOMMU_NONE ; } } -/* GV: 1 GVMA: 0 */ -static void riscv_iommu_iot_inval_gscid (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 1 PSCV: 0 GVMA: 0 */ +/* GV: 1 AV: 1 GVMA: 1 */ +static void riscv_iommu_iot_inval_gscid_iova(gpointer key, gpointervalue,
Quoted 7 lines
hw/riscv/riscv-iommu.c +3
-1+ gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid) { + if (iot->tag == arg->tag && + iot->gscid == arg->gscid &&hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 10 lines
hw/riscv/riscv-iommu.c +3
-3+ iot->iova == arg->iova) { iot->perm =IOMMU_NONE ; } } -/* GV: 0 */ -static void riscv_iommu_iot_inval_all (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 1 PSCV: 1 GVMA: 0 */ +static void riscv_iommu_iot_inval_gscid_pscid_iova(gpointer key,gpointer value,
hw/riscv/riscv-iommu.c static void
Quoted 24 lines
hw/riscv/riscv-iommu.c +10
-2+ gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; - iot->perm =IOMMU_NONE ; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag && + iot->gscid == arg->gscid && + iot->pscid == arg->pscid && + iot->iova == arg->iova) { + iot->perm =IOMMU_NONE ; + } }/* caller should keep ref-count for iot_cache object */ static RISCVIOMMUEntry *riscv_iommu_iot_lookup (RISCVIOMMUContext *ctx, -GHashTable *iot_cache,hwaddr iova) +GHashTable *iot_cache,hwaddr iova,RISCVIOMMUTransTag transtag) {RISCVIOMMUEntry key = { + .tag = transtag, .gscid =get_field (ctx->gatp,RISCV_IOMMU_DC_IOHGATP_GSCID ), .pscid =get_field (ctx->ta,RISCV_IOMMU_DC_TA_PSCID ), .iova =PPN_DOWN (iova),@@ -1323,10 +1379,11 @@ static voidriscv_iommu_iot_update(RISCVIOMMUState *s,
hw/riscv/riscv-iommu.c static void riscv_iommu_iot_update(RISCVIOMMUState *s,
Quoted 5 lines
hw/riscv/riscv-iommu.c +1
-1}static void riscv_iommu_iot_inval (RISCVIOMMUState *s,GHFunc func, -uint32_t gscid,uint32_t pscid,hwaddr iova) + uint32_t gscid, uint32_t pscid, hwaddr iova, RISCVIOMMUTransTagtranstag)
hw/riscv/riscv-iommu.c static void riscv_iommu_iot_inval(RISCVIOMMUState
Quoted 8 lines
hw/riscv/riscv-iommu.c +1
-0{GHashTable *iot_cache;RISCVIOMMUEntry key = { + .tag = transtag, .gscid = gscid, .pscid = pscid, .iova =PPN_DOWN (iova),@@ -1337,9 +1394,24 @@ static void riscv_iommu_iot_inval(RISCVIOMMUState*s, GHFunc func,
hw/riscv/riscv-iommu.c static void riscv_iommu_iot_inval(RISCVIOMMUState *s, GHFunc func,
Quoted 4 lines
hw/riscv/riscv-iommu.c +1
-0g_hash_table_unref (iot_cache); } +static RISCVIOMMUTransTag riscv_iommu_get_transtag(RISCVIOMMUContext*ctx)
Quoted 4 lines
hw/riscv/riscv-iommu.c +4
-0+{ +uint64_t satp =get_field (ctx->satp,RISCV_IOMMU_ATP_MODE_FIELD ); +uint64_t gatp =get_field (ctx->gatp,RISCV_IOMMU_ATP_MODE_FIELD ); +hw/riscv/riscv-iommu.c static void riscv_iommu_iot_inval(RISCVIOMMUState *s, GHFunc func,
Quoted 10 lines
hw/riscv/riscv-iommu.c +9
-0+if (satp ==RISCV_IOMMU_DC_FSC_MODE_BARE ) { +return (gatp ==RISCV_IOMMU_DC_IOHGATP_MODE_BARE ) ? +RISCV_IOMMU_TRANS_TAG_BY :RISCV_IOMMU_TRANS_TAG_VG ; + }else { +return (gatp ==RISCV_IOMMU_DC_IOHGATP_MODE_BARE ) ? +RISCV_IOMMU_TRANS_TAG_SS :RISCV_IOMMU_TRANS_TAG_VN ; + } +} + static int riscv_iommu_translate(RISCVIOMMUState *s, RISCVIOMMUContext*ctx,
hw/riscv/riscv-iommu.c static int riscv_iommu_translate(RISCVIOMMUState
Quoted 7 lines
hw/riscv/riscv-iommu.c +1
-0IOMMUTLBEntry *iotlb, bool enable_cache) { +RISCVIOMMUTransTag transtag =riscv_iommu_get_transtag (ctx);RISCVIOMMUEntry *iot;IOMMUAccessFlags perm;bool enable_pid;@@ -1365,7 +1437,7 @@ static int riscv_iommu_translate(RISCVIOMMUState*s, RISCVIOMMUContext *ctx,
hw/riscv/riscv-iommu.c static int riscv_iommu_translate(RISCVIOMMUState
Quoted 9 lines
hw/riscv/riscv-iommu.c +1
-1} } - iot =riscv_iommu_iot_lookup (ctx, iot_cache, iotlb->iova); + iot =riscv_iommu_iot_lookup (ctx, iot_cache, iotlb->iova, transtag); perm = iot ? iot->perm :IOMMU_NONE ;if (perm !=IOMMU_NONE ) { iotlb->translated_addr =PPN_PHYS (iot->phys);@@ -1396,6 +1468,7 @@ static int riscv_iommu_translate(RISCVIOMMUState*s, RISCVIOMMUContext *ctx,
hw/riscv/riscv-iommu.c static int riscv_iommu_translate(RISCVIOMMUState · line 1468
Quoted 1 line
hw/riscv/riscv-iommu.c +0
-0iot->gscid = get_field(ctx->gatp,
RISCV_IOMMU_DC_IOHGATP_GSCID);hw/riscv/riscv-iommu.c static void
Quoted 7 lines
hw/riscv/riscv-iommu.c +1
-0iot->pscid =get_field (ctx->ta,RISCV_IOMMU_DC_TA_PSCID ); iot->perm = iotlb->perm; + iot->tag = transtag;riscv_iommu_iot_update (s, iot_cache, iot); }@@ -1603,44 +1676,72 @@ static void
riscv_iommu_process_cq_tail(RISCVIOMMUState *s)Quoted 6 lines
hw/riscv/riscv-iommu.c +2
-1case RISCV_IOMMU_CMD (RISCV_IOMMU_CMD_IOTINVAL_FUNC_GVMA ,RISCV_IOMMU_CMD_IOTINVAL_OPCODE ): -if (cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV ) { + { +bool gv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV );hw/riscv/riscv-iommu.c static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
Quoted 10 lines
hw/riscv/riscv-iommu.c +9
-0+bool av = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV ); +bool pscv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV ); +uint32_t gscid =get_field (cmd.dword0, +RISCV_IOMMU_CMD_IOTINVAL_GSCID ); +uint32_t pscid =get_field (cmd.dword0, +RISCV_IOMMU_CMD_IOTINVAL_PSCID ); +hwaddr iova = (cmd.dword1 <<2 ) &TARGET_PAGE_MASK ; + +if (pscv) {/* illegal command arguments IOTINVAL.GVMA & PSCV == 1 */
hw/riscv/riscv-iommu.c static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s) · line 1640
Quoted 13 lines
hw/riscv/riscv-iommu.c +0
-11goto cmd_ill; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV )) { -/* invalidate all cache mappings */ - func = riscv_iommu_iot_inval_all; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV )) { -/* invalidate cache matching GSCID */ - func = riscv_iommu_iot_inval_gscid; - }else { -/* invalidate cache matching GSCID and ADDR (GPA) */ - func = riscv_iommu_iot_inval_gscid_gpa; } - riscv_iommu_iot_inval(s, func, - get_field(cmd.dword0, RISCV_IOMMU_CMD_IOTINVAL_GSCID),0,
Quoted 33 lines
hw/riscv/riscv-iommu.c +19
-10- cmd.dword1 << 2 & TARGET_PAGE_MASK); + + func = riscv_iommu_iot_inval_all; + +if (gv) { + func = (av) ? riscv_iommu_iot_inval_gscid_iova : + riscv_iommu_iot_inval_gscid; + } + +riscv_iommu_iot_inval ( + s, func, gscid, pscid, iova,RISCV_IOMMU_TRANS_TAG_VG ); + +riscv_iommu_iot_inval ( + s, func, gscid, pscid, iova,RISCV_IOMMU_TRANS_TAG_VN );break ; + }case RISCV_IOMMU_CMD (RISCV_IOMMU_CMD_IOTINVAL_FUNC_VMA ,RISCV_IOMMU_CMD_IOTINVAL_OPCODE ): -if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV )) { -/* invalidate all cache mappings, simplified model */ - func = riscv_iommu_iot_inval_all; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV )) { -/* invalidate cache matching GSCID, simplified model */ - func = riscv_iommu_iot_inval_gscid; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV )) { -/* invalidate cache matching GSCID and PSCID */ - func = riscv_iommu_iot_inval_pscid; + { +bool gv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV ); +bool av = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV ); +bool pscv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV ); + uint32_t gscid = get_field(cmd.dword0,hw/riscv/riscv-iommu.c static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
Quoted 10 lines
hw/riscv/riscv-iommu.c +10
-0+ RISCV_IOMMU_CMD_IOTINVAL_GSCID); +uint32_t pscid =get_field (cmd.dword0, +RISCV_IOMMU_CMD_IOTINVAL_PSCID ); +hwaddr iova = (cmd.dword1 <<2 ) &TARGET_PAGE_MASK ; +RISCVIOMMUTransTag transtag; + +if (gv) { + transtag =RISCV_IOMMU_TRANS_TAG_VN ; +if (pscv) { + func = (av) ?
riscv_iommu_iot_inval_gscid_pscid_iova:hw/riscv/riscv-iommu.c static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
Quoted 7 lines
hw/riscv/riscv-iommu.c +5
-1+ riscv_iommu_iot_inval_gscid_pscid; + }else { + func = (av) ? riscv_iommu_iot_inval_gscid_iova : + riscv_iommu_iot_inval_gscid; + } }else { -/* invalidate cache matching GSCID and PSCID and ADDR (IOVA) */
Quoted 6 lines
hw/riscv/riscv-iommu.c +5
-1- func = riscv_iommu_iot_inval_pscid_iova; + transtag =RISCV_IOMMU_TRANS_TAG_SS ; +if (pscv) { + func = (av) ? riscv_iommu_iot_inval_pscid_iova : + riscv_iommu_iot_inval_pscid; + }else {hw/riscv/riscv-iommu.c static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
Quoted 10 lines
hw/riscv/riscv-iommu.c +5
-4+ func = (av) ? riscv_iommu_iot_inval_iova : + riscv_iommu_iot_inval_all; + } } -riscv_iommu_iot_inval (s, func, -get_field (cmd.dword0,RISCV_IOMMU_CMD_IOTINVAL_GSCID ), -get_field (cmd.dword0,RISCV_IOMMU_CMD_IOTINVAL_PSCID ), - cmd.dword1 <<2 &TARGET_PAGE_MASK ); + + riscv_iommu_iot_inval(s, func, gscid, pscid, iova,transtag);
Quoted 5 lines
hw/riscv/riscv-iommu.c +1
-0break ; + }case RISCV_IOMMU_CMD (RISCV_IOMMU_CMD_IODIR_FUNC_INVAL_DDT ,RISCV_IOMMU_CMD_IODIR_OPCODE ):
Ping.
Jason Chien <[email protected]> 於 2024年11月25日 週一 下午8:49寫道:
Quoted 383 lines
Hi Tomasz, any thoughs?
Daniel Henrique Barboza <[email protected]> 於 2024年11月12日 週二 下午8:26寫道:
Quoted 378 lines
CCing Tomasz
Quoted 2 lines · Jason Chien
This commit introduces a translation tag to avoid invalidating an entry that should not be invalidated when IOMMU executes invalidation
commands.
Quoted 6 lines
E.g. IOTINVAL.VMA with GV=0, AV=0, PSCV=1 invalidates both a mapping of single stage translation and a mapping of nested translation with the same PSCID, but only the former one should be invalidated.
Signed-off-by Jason Chien <[email protected]>---
LGTM but I would like to hear from Tomasz if adding this new abstraction is
the best way to what seems to be a bug inriscv_iommu_process_cq_tail().Daniel
hw/riscv/riscv-iommu.c struct RISCVIOMMUContext { · line 64
Quoted 9 lines
hw/riscv/riscv-iommu.c | 205 ++++++++++++++++++++++++++++++----------- 1 file changed, 153 insertions(+), 52 deletions(-)hw/riscv/riscv-iommu.c +0
-0@@ -64,8 +64,16 @@ struct RISCVIOMMUContext {uint64_t msiptp;/* MSI redirection page table pointer */
hw/riscv/riscv-iommu.c static gboolean
Quoted 16 lines
hw/riscv/riscv-iommu.c +8
-0}; +typedef enum RISCVIOMMUTransTag { +RISCV_IOMMU_TRANS_TAG_BY ,/* Bypass */ +RISCV_IOMMU_TRANS_TAG_SS ,/* Single Stage */ +RISCV_IOMMU_TRANS_TAG_VG ,/* G-stage only */ +RISCV_IOMMU_TRANS_TAG_VN ,/* Nested translation */ +}RISCVIOMMUTransTag ; +/* Address translation cache entry */ struct RISCVIOMMUEntry { +RISCVIOMMUTransTag tag;/* Translation Tag */ uint64_t iova:44 ;/* IOVA Page Number */ uint64_t pscid:20 ;/* Process Soft-Context identifier */ uint64_t phys:44 ;/* Physical Page Number */ @@ -1228,7 +1236,7 @@ static gboolean
riscv_iommu_iot_equal(gconstpointer v1, gconstpointer v2)hw/riscv/riscv-iommu.c static guint
Quoted 9 lines
hw/riscv/riscv-iommu.c +1
-1RISCVIOMMUEntry *t1 = (RISCVIOMMUEntry *) v1;RISCVIOMMUEntry *t2 = (RISCVIOMMUEntry *) v2;return t1->gscid == t2->gscid && t1->pscid == t2->pscid && - t1->iova == t2->iova; + t1->iova == t2->iova && t1->tag == t2->tag; }static guint riscv_iommu_iot_hash (gconstpointer v)@@ -1237,67 +1245,115 @@ static guint
riscv_iommu_iot_hash(gconstpointer v)hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 8 lines
hw/riscv/riscv-iommu.c +4
-1return (guint )t->iova; } -/* GV: 1 PSCV: 1 AV: 1 */ +/* GV: 0 AV: 0 PSCV: 0 GVMA: 0 */ +/* GV: 0 AV: 0 GVMA: 1 */ +static +void riscv_iommu_iot_inval_all(gpointer key, gpointer value, gpointerdata)
hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 11 lines
hw/riscv/riscv-iommu.c +11
-0+{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 0 AV: 0 PSCV: 1 GVMA: 0 */ +static +void riscv_iommu_iot_inval_pscid(gpointer key, gpointer value,gpointer data)
hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 12 lines
hw/riscv/riscv-iommu.c +12
-0+{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag && + iot->pscid == arg->pscid) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 0 AV: 1 PSCV: 0 GVMA: 0 */ +static +void riscv_iommu_iot_inval_iova(gpointer key, gpointer value, gpointerdata)
hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 11 lines
hw/riscv/riscv-iommu.c +10
-0+{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag && + iot->iova == arg->iova) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 0 AV: 1 PSCV: 1 GVMA: 0 */ static void riscv_iommu_iot_inval_pscid_iova(gpointer key, gpointervalue,
Quoted 9 lines
hw/riscv/riscv-iommu.c +1
-1gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid && +if (iot->tag == arg->tag && iot->pscid == arg->pscid && iot->iova == arg->iova) { iot->perm =IOMMU_NONE ;hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 10 lines
hw/riscv/riscv-iommu.c +4
-3} } -/* GV: 1 PSCV: 1 AV: 0 */ -static void riscv_iommu_iot_inval_pscid (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 0 PSCV: 0 GVMA: 0 */ +/* GV: 1 AV: 0 GVMA: 1 */ +static +void riscv_iommu_iot_inval_gscid(gpointer key, gpointer value,gpointer data)
hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 13 lines
hw/riscv/riscv-iommu.c +2
-4{RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid && - iot->pscid == arg->pscid) { +if (iot->tag == arg->tag && + iot->gscid == arg->gscid) { iot->perm =IOMMU_NONE ; } } -/* GV: 1 GVMA: 1 */ -static void riscv_iommu_iot_inval_gscid_gpa(gpointer key, gpointervalue,
hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 3 lines
hw/riscv/riscv-iommu.c +2
-1- gpointer data) +/* GV: 1 AV: 0 PSCV: 1 GVMA: 0 */ +static void riscv_iommu_iot_inval_gscid_pscid(gpointer key, gpointervalue,
Quoted 9 lines
hw/riscv/riscv-iommu.c +4
-2+ gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid) { -/* simplified cache, no GPA matching */ +if (iot->tag == arg->tag && + iot->gscid == arg->gscid && + iot->pscid == arg->pscid) {hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 10 lines
hw/riscv/riscv-iommu.c +3
-3iot->perm =IOMMU_NONE ; } } -/* GV: 1 GVMA: 0 */ -static void riscv_iommu_iot_inval_gscid (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 1 PSCV: 0 GVMA: 0 */ +/* GV: 1 AV: 1 GVMA: 1 */ +static void riscv_iommu_iot_inval_gscid_iova(gpointer key, gpointervalue,
Quoted 7 lines
hw/riscv/riscv-iommu.c +3
-1+ gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid) { + if (iot->tag == arg->tag && + iot->gscid == arg->gscid &&hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 10 lines
hw/riscv/riscv-iommu.c +3
-3+ iot->iova == arg->iova) { iot->perm =IOMMU_NONE ; } } -/* GV: 0 */ -static void riscv_iommu_iot_inval_all (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 1 PSCV: 1 GVMA: 0 */ +static void riscv_iommu_iot_inval_gscid_pscid_iova(gpointer key,gpointer value,
hw/riscv/riscv-iommu.c static void
Quoted 24 lines
hw/riscv/riscv-iommu.c +10
-2+ gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; - iot->perm =IOMMU_NONE ; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag && + iot->gscid == arg->gscid && + iot->pscid == arg->pscid && + iot->iova == arg->iova) { + iot->perm =IOMMU_NONE ; + } }/* caller should keep ref-count for iot_cache object */ static RISCVIOMMUEntry *riscv_iommu_iot_lookup (RISCVIOMMUContext *ctx, -GHashTable *iot_cache,hwaddr iova) +GHashTable *iot_cache,hwaddr iova,RISCVIOMMUTransTag transtag) {RISCVIOMMUEntry key = { + .tag = transtag, .gscid =get_field (ctx->gatp,RISCV_IOMMU_DC_IOHGATP_GSCID ), .pscid =get_field (ctx->ta,RISCV_IOMMU_DC_TA_PSCID ), .iova =PPN_DOWN (iova),@@ -1323,10 +1379,11 @@ static voidriscv_iommu_iot_update(RISCVIOMMUState *s,
hw/riscv/riscv-iommu.c static void riscv_iommu_iot_update(RISCVIOMMUState *s,
Quoted 5 lines
hw/riscv/riscv-iommu.c +1
-1}static void riscv_iommu_iot_inval (RISCVIOMMUState *s,GHFunc func, -uint32_t gscid,uint32_t pscid,hwaddr iova) + uint32_t gscid, uint32_t pscid, hwaddr iova, RISCVIOMMUTransTagtranstag)
hw/riscv/riscv-iommu.c static void
Quoted 8 lines
hw/riscv/riscv-iommu.c +1
-0{GHashTable *iot_cache;RISCVIOMMUEntry key = { + .tag = transtag, .gscid = gscid, .pscid = pscid, .iova =PPN_DOWN (iova),@@ -1337,9 +1394,24 @@ static voidriscv_iommu_iot_inval(RISCVIOMMUState *s, GHFunc func,
hw/riscv/riscv-iommu.c static void riscv_iommu_iot_inval(RISCVIOMMUState *s, GHFunc func,
Quoted 4 lines
hw/riscv/riscv-iommu.c +1
-0g_hash_table_unref (iot_cache); } +static RISCVIOMMUTransTag riscv_iommu_get_transtag(RISCVIOMMUContext*ctx)
Quoted 4 lines
hw/riscv/riscv-iommu.c +4
-0+{ +uint64_t satp =get_field (ctx->satp,RISCV_IOMMU_ATP_MODE_FIELD ); +uint64_t gatp =get_field (ctx->gatp,RISCV_IOMMU_ATP_MODE_FIELD ); +hw/riscv/riscv-iommu.c static void riscv_iommu_iot_inval(RISCVIOMMUState *s, GHFunc func,
Quoted 10 lines
hw/riscv/riscv-iommu.c +9
-0+if (satp ==RISCV_IOMMU_DC_FSC_MODE_BARE ) { +return (gatp ==RISCV_IOMMU_DC_IOHGATP_MODE_BARE ) ? +RISCV_IOMMU_TRANS_TAG_BY :RISCV_IOMMU_TRANS_TAG_VG ; + }else { +return (gatp ==RISCV_IOMMU_DC_IOHGATP_MODE_BARE ) ? +RISCV_IOMMU_TRANS_TAG_SS :RISCV_IOMMU_TRANS_TAG_VN ; + } +} + static int riscv_iommu_translate(RISCVIOMMUState *s,RISCVIOMMUContext *ctx,
hw/riscv/riscv-iommu.c static int riscv_iommu_translate(RISCVIOMMUState
Quoted 7 lines
hw/riscv/riscv-iommu.c +1
-0IOMMUTLBEntry *iotlb, bool enable_cache) { +RISCVIOMMUTransTag transtag =riscv_iommu_get_transtag (ctx);RISCVIOMMUEntry *iot;IOMMUAccessFlags perm;bool enable_pid;@@ -1365,7 +1437,7 @@ static int riscv_iommu_translate(RISCVIOMMUState*s, RISCVIOMMUContext *ctx,
hw/riscv/riscv-iommu.c static int riscv_iommu_translate(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
Quoted 5 lines
hw/riscv/riscv-iommu.c +1
-1} } - iot =riscv_iommu_iot_lookup (ctx, iot_cache, iotlb->iova); + iot = riscv_iommu_iot_lookup(ctx, iot_cache, iotlb->iova,transtag);
hw/riscv/riscv-iommu.c static int riscv_iommu_translate(RISCVIOMMUState
Quoted 4 lines
hw/riscv/riscv-iommu.c +0
-0perm = iot ? iot->perm :IOMMU_NONE ;if (perm !=IOMMU_NONE ) { iotlb->translated_addr =PPN_PHYS (iot->phys);@@ -1396,6 +1468,7 @@ static int riscv_iommu_translate(RISCVIOMMUState*s, RISCVIOMMUContext *ctx,
hw/riscv/riscv-iommu.c static int riscv_iommu_translate(RISCVIOMMUState · line 1468
Quoted 1 line
hw/riscv/riscv-iommu.c +0
-0iot->gscid = get_field(ctx->gatp,
RISCV_IOMMU_DC_IOHGATP_GSCID);hw/riscv/riscv-iommu.c static void
Quoted 7 lines
hw/riscv/riscv-iommu.c +1
-0iot->pscid =get_field (ctx->ta,RISCV_IOMMU_DC_TA_PSCID ); iot->perm = iotlb->perm; + iot->tag = transtag;riscv_iommu_iot_update (s, iot_cache, iot); }@@ -1603,44 +1676,72 @@ static void
riscv_iommu_process_cq_tail(RISCVIOMMUState *s)Quoted 6 lines
hw/riscv/riscv-iommu.c +2
-1case RISCV_IOMMU_CMD (RISCV_IOMMU_CMD_IOTINVAL_FUNC_GVMA ,RISCV_IOMMU_CMD_IOTINVAL_OPCODE ): -if (cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV ) { + { +bool gv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV );hw/riscv/riscv-iommu.c static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
Quoted 10 lines
hw/riscv/riscv-iommu.c +9
-0+bool av = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV ); +bool pscv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV ); +uint32_t gscid =get_field (cmd.dword0, +RISCV_IOMMU_CMD_IOTINVAL_GSCID ); +uint32_t pscid =get_field (cmd.dword0, +RISCV_IOMMU_CMD_IOTINVAL_PSCID ); +hwaddr iova = (cmd.dword1 <<2 ) &TARGET_PAGE_MASK ; + +if (pscv) {/* illegal command arguments IOTINVAL.GVMA & PSCV == 1 */
hw/riscv/riscv-iommu.c static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s) · line 1640
Quoted 13 lines
hw/riscv/riscv-iommu.c +0
-11goto cmd_ill; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV )) { -/* invalidate all cache mappings */ - func = riscv_iommu_iot_inval_all; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV )) { -/* invalidate cache matching GSCID */ - func = riscv_iommu_iot_inval_gscid; - }else { -/* invalidate cache matching GSCID and ADDR (GPA) */ - func = riscv_iommu_iot_inval_gscid_gpa; } - riscv_iommu_iot_inval(s, func, - get_field(cmd.dword0, RISCV_IOMMU_CMD_IOTINVAL_GSCID),0,
Quoted 33 lines
hw/riscv/riscv-iommu.c +19
-10- cmd.dword1 << 2 & TARGET_PAGE_MASK); + + func = riscv_iommu_iot_inval_all; + +if (gv) { + func = (av) ? riscv_iommu_iot_inval_gscid_iova : + riscv_iommu_iot_inval_gscid; + } + +riscv_iommu_iot_inval ( + s, func, gscid, pscid, iova,RISCV_IOMMU_TRANS_TAG_VG ); + +riscv_iommu_iot_inval ( + s, func, gscid, pscid, iova,RISCV_IOMMU_TRANS_TAG_VN );break ; + }case RISCV_IOMMU_CMD (RISCV_IOMMU_CMD_IOTINVAL_FUNC_VMA ,RISCV_IOMMU_CMD_IOTINVAL_OPCODE ): -if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV )) { -/* invalidate all cache mappings, simplified model */ - func = riscv_iommu_iot_inval_all; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV )) { -/* invalidate cache matching GSCID, simplified model */ - func = riscv_iommu_iot_inval_gscid; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV )) { -/* invalidate cache matching GSCID and PSCID */ - func = riscv_iommu_iot_inval_pscid; + { +bool gv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV ); +bool av = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV ); +bool pscv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV ); + uint32_t gscid = get_field(cmd.dword0,hw/riscv/riscv-iommu.c static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
Quoted 10 lines
hw/riscv/riscv-iommu.c +10
-0+ RISCV_IOMMU_CMD_IOTINVAL_GSCID); +uint32_t pscid =get_field (cmd.dword0, +RISCV_IOMMU_CMD_IOTINVAL_PSCID ); +hwaddr iova = (cmd.dword1 <<2 ) &TARGET_PAGE_MASK ; +RISCVIOMMUTransTag transtag; + +if (gv) { + transtag =RISCV_IOMMU_TRANS_TAG_VN ; +if (pscv) { + func = (av) ?
riscv_iommu_iot_inval_gscid_pscid_iova:hw/riscv/riscv-iommu.c static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
Quoted 7 lines
hw/riscv/riscv-iommu.c +5
-1+ riscv_iommu_iot_inval_gscid_pscid; + }else { + func = (av) ? riscv_iommu_iot_inval_gscid_iova : + riscv_iommu_iot_inval_gscid; + } }else { -/* invalidate cache matching GSCID and PSCID and ADDR (IOVA) */
Quoted 6 lines
hw/riscv/riscv-iommu.c +5
-1- func = riscv_iommu_iot_inval_pscid_iova; + transtag =RISCV_IOMMU_TRANS_TAG_SS ; +if (pscv) { + func = (av) ? riscv_iommu_iot_inval_pscid_iova : + riscv_iommu_iot_inval_pscid; + }else {hw/riscv/riscv-iommu.c static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
Quoted 10 lines
hw/riscv/riscv-iommu.c +5
-4+ func = (av) ? riscv_iommu_iot_inval_iova : + riscv_iommu_iot_inval_all; + } } -riscv_iommu_iot_inval (s, func, -get_field (cmd.dword0,RISCV_IOMMU_CMD_IOTINVAL_GSCID ), -get_field (cmd.dword0,RISCV_IOMMU_CMD_IOTINVAL_PSCID ), - cmd.dword1 <<2 &TARGET_PAGE_MASK ); + + riscv_iommu_iot_inval(s, func, gscid, pscid, iova,transtag);
Quoted 5 lines
hw/riscv/riscv-iommu.c +1
-0break ; + }case RISCV_IOMMU_CMD (RISCV_IOMMU_CMD_IODIR_FUNC_INVAL_DDT ,RISCV_IOMMU_CMD_IODIR_OPCODE ):
Hi Daniel,
There is no response from Tomasz. Should this patch be merged?
Jason Chien <[email protected]> 於 2024年12月18日 週三 下午5:52寫道:
Quoted 399 lines
Ping.
Jason Chien <[email protected]> 於 2024年11月25日 週一 下午8:49寫道:
Quoted 395 lines
Hi Tomasz, any thoughs?
Daniel Henrique Barboza <[email protected]> 於 2024年11月12日 週二 下午8:26寫道:
Quoted 390 lines
CCing Tomasz
Quoted 2 lines · Jason Chien
This commit introduces a translation tag to avoid invalidating an entry that should not be invalidated when IOMMU executes invalidation
commands.
Quoted 6 lines
E.g. IOTINVAL.VMA with GV=0, AV=0, PSCV=1 invalidates both a mapping of single stage translation and a mapping of nested translation with the same PSCID, but only the former one should be invalidated.
Signed-off-by Jason Chien <[email protected]>---
LGTM but I would like to hear from Tomasz if adding this new abstraction is
the best way to what seems to be a bug inriscv_iommu_process_cq_tail().Daniel
Quoted 1 line
hw/riscv/riscv-iommu.c | 205++++++++++++++++++++++++++++++-----------
hw/riscv/riscv-iommu.c struct RISCVIOMMUContext { · line 64
Quoted 8 lines
1 file changed, 153 insertions(+), 52 deletions(-)hw/riscv/riscv-iommu.c +0
-0@@ -64,8 +64,16 @@ struct RISCVIOMMUContext {uint64_t msiptp;/* MSI redirection page table pointer */
hw/riscv/riscv-iommu.c static gboolean
Quoted 16 lines
hw/riscv/riscv-iommu.c +8
-0}; +typedef enum RISCVIOMMUTransTag { +RISCV_IOMMU_TRANS_TAG_BY ,/* Bypass */ +RISCV_IOMMU_TRANS_TAG_SS ,/* Single Stage */ +RISCV_IOMMU_TRANS_TAG_VG ,/* G-stage only */ +RISCV_IOMMU_TRANS_TAG_VN ,/* Nested translation */ +}RISCVIOMMUTransTag ; +/* Address translation cache entry */ struct RISCVIOMMUEntry { +RISCVIOMMUTransTag tag;/* Translation Tag */ uint64_t iova:44 ;/* IOVA Page Number */ uint64_t pscid:20 ;/* Process Soft-Context identifier */ uint64_t phys:44 ;/* Physical Page Number */ @@ -1228,7 +1236,7 @@ static gboolean
riscv_iommu_iot_equal(gconstpointer v1, gconstpointer v2)hw/riscv/riscv-iommu.c static guint
Quoted 9 lines
hw/riscv/riscv-iommu.c +1
-1RISCVIOMMUEntry *t1 = (RISCVIOMMUEntry *) v1;RISCVIOMMUEntry *t2 = (RISCVIOMMUEntry *) v2;return t1->gscid == t2->gscid && t1->pscid == t2->pscid && - t1->iova == t2->iova; + t1->iova == t2->iova && t1->tag == t2->tag; }static guint riscv_iommu_iot_hash (gconstpointer v)@@ -1237,67 +1245,115 @@ static guint
riscv_iommu_iot_hash(gconstpointer v)hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 8 lines
hw/riscv/riscv-iommu.c +4
-1return (guint )t->iova; } -/* GV: 1 PSCV: 1 AV: 1 */ +/* GV: 0 AV: 0 PSCV: 0 GVMA: 0 */ +/* GV: 0 AV: 0 GVMA: 1 */ +static +void riscv_iommu_iot_inval_all(gpointer key, gpointer value, gpointerdata)
hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 11 lines
hw/riscv/riscv-iommu.c +11
-0+{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 0 AV: 0 PSCV: 1 GVMA: 0 */ +static +void riscv_iommu_iot_inval_pscid(gpointer key, gpointer value,gpointer data)
hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 12 lines
hw/riscv/riscv-iommu.c +12
-0+{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag && + iot->pscid == arg->pscid) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 0 AV: 1 PSCV: 0 GVMA: 0 */ +static +void riscv_iommu_iot_inval_iova(gpointer key, gpointer value,gpointer data)
hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 11 lines
hw/riscv/riscv-iommu.c +10
-0+{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag && + iot->iova == arg->iova) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 0 AV: 1 PSCV: 1 GVMA: 0 */ static void riscv_iommu_iot_inval_pscid_iova(gpointer key, gpointervalue,
Quoted 9 lines
hw/riscv/riscv-iommu.c +1
-1gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid && +if (iot->tag == arg->tag && iot->pscid == arg->pscid && iot->iova == arg->iova) { iot->perm =IOMMU_NONE ;hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 10 lines
hw/riscv/riscv-iommu.c +4
-3} } -/* GV: 1 PSCV: 1 AV: 0 */ -static void riscv_iommu_iot_inval_pscid (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 0 PSCV: 0 GVMA: 0 */ +/* GV: 1 AV: 0 GVMA: 1 */ +static +void riscv_iommu_iot_inval_gscid(gpointer key, gpointer value,gpointer data)
hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 13 lines
hw/riscv/riscv-iommu.c +2
-4{RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid && - iot->pscid == arg->pscid) { +if (iot->tag == arg->tag && + iot->gscid == arg->gscid) { iot->perm =IOMMU_NONE ; } } -/* GV: 1 GVMA: 1 */ -static void riscv_iommu_iot_inval_gscid_gpa(gpointer key, gpointervalue,
hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 3 lines
hw/riscv/riscv-iommu.c +2
-1- gpointer data) +/* GV: 1 AV: 0 PSCV: 1 GVMA: 0 */ +static void riscv_iommu_iot_inval_gscid_pscid(gpointer key, gpointervalue,
Quoted 9 lines
hw/riscv/riscv-iommu.c +4
-2+ gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid) { -/* simplified cache, no GPA matching */ +if (iot->tag == arg->tag && + iot->gscid == arg->gscid && + iot->pscid == arg->pscid) {hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 10 lines
hw/riscv/riscv-iommu.c +3
-3iot->perm =IOMMU_NONE ; } } -/* GV: 1 GVMA: 0 */ -static void riscv_iommu_iot_inval_gscid (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 1 PSCV: 0 GVMA: 0 */ +/* GV: 1 AV: 1 GVMA: 1 */ +static void riscv_iommu_iot_inval_gscid_iova(gpointer key, gpointervalue,
Quoted 7 lines
hw/riscv/riscv-iommu.c +3
-1+ gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid) { + if (iot->tag == arg->tag && + iot->gscid == arg->gscid &&hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 10 lines
hw/riscv/riscv-iommu.c +3
-3+ iot->iova == arg->iova) { iot->perm =IOMMU_NONE ; } } -/* GV: 0 */ -static void riscv_iommu_iot_inval_all (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 1 PSCV: 1 GVMA: 0 */ +static void riscv_iommu_iot_inval_gscid_pscid_iova(gpointer key,gpointer value,
Quoted 5 lines
hw/riscv/riscv-iommu.c +2
-1+ gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; - iot->perm =IOMMU_NONE ; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data;hw/riscv/riscv-iommu.c static guint riscv_iommu_iot_hash(gconstpointer v)
Quoted 10 lines
hw/riscv/riscv-iommu.c +6
-0+if (iot->tag == arg->tag && + iot->gscid == arg->gscid && + iot->pscid == arg->pscid && + iot->iova == arg->iova) { + iot->perm =IOMMU_NONE ; + } }/* caller should keep ref-count for iot_cache object */ static RISCVIOMMUEntry *riscv_iommu_iot_lookup(RISCVIOMMUContext*ctx,
hw/riscv/riscv-iommu.c static void
Quoted 9 lines
hw/riscv/riscv-iommu.c +2
-1- GHashTable *iot_cache, hwaddr iova) + GHashTable *iot_cache, hwaddr iova, RISCVIOMMUTransTag transtag) { RISCVIOMMUEntry key = { + .tag = transtag, .gscid = get_field(ctx->gatp, RISCV_IOMMU_DC_IOHGATP_GSCID), .pscid = get_field(ctx->ta, RISCV_IOMMU_DC_TA_PSCID), .iova = PPN_DOWN(iova),@@ -1323,10 +1379,11 @@ static voidriscv_iommu_iot_update(RISCVIOMMUState *s,
hw/riscv/riscv-iommu.c static void riscv_iommu_iot_update(RISCVIOMMUState *s,
Quoted 5 lines
hw/riscv/riscv-iommu.c +1
-1}static void riscv_iommu_iot_inval (RISCVIOMMUState *s,GHFunc func, -uint32_t gscid,uint32_t pscid,hwaddr iova) + uint32_t gscid, uint32_t pscid, hwaddr iova, RISCVIOMMUTransTagtranstag)
hw/riscv/riscv-iommu.c static void
Quoted 8 lines
hw/riscv/riscv-iommu.c +1
-0{GHashTable *iot_cache;RISCVIOMMUEntry key = { + .tag = transtag, .gscid = gscid, .pscid = pscid, .iova =PPN_DOWN (iova),@@ -1337,9 +1394,24 @@ static voidriscv_iommu_iot_inval(RISCVIOMMUState *s, GHFunc func,
hw/riscv/riscv-iommu.c static void riscv_iommu_iot_inval(RISCVIOMMUState *s, GHFunc func,
Quoted 4 lines
hw/riscv/riscv-iommu.c +1
-0g_hash_table_unref (iot_cache); } +static RISCVIOMMUTransTag riscv_iommu_get_transtag(RISCVIOMMUContext*ctx)
Quoted 4 lines
hw/riscv/riscv-iommu.c +4
-0+{ +uint64_t satp =get_field (ctx->satp,RISCV_IOMMU_ATP_MODE_FIELD ); +uint64_t gatp =get_field (ctx->gatp,RISCV_IOMMU_ATP_MODE_FIELD ); +hw/riscv/riscv-iommu.c static void riscv_iommu_iot_inval(RISCVIOMMUState *s, GHFunc func,
Quoted 10 lines
hw/riscv/riscv-iommu.c +9
-0+if (satp ==RISCV_IOMMU_DC_FSC_MODE_BARE ) { +return (gatp ==RISCV_IOMMU_DC_IOHGATP_MODE_BARE ) ? +RISCV_IOMMU_TRANS_TAG_BY :RISCV_IOMMU_TRANS_TAG_VG ; + }else { +return (gatp ==RISCV_IOMMU_DC_IOHGATP_MODE_BARE ) ? +RISCV_IOMMU_TRANS_TAG_SS :RISCV_IOMMU_TRANS_TAG_VN ; + } +} + static int riscv_iommu_translate(RISCVIOMMUState *s,RISCVIOMMUContext *ctx,
hw/riscv/riscv-iommu.c static int riscv_iommu_translate(RISCVIOMMUState
Quoted 7 lines
hw/riscv/riscv-iommu.c +1
-0IOMMUTLBEntry *iotlb, bool enable_cache) { +RISCVIOMMUTransTag transtag =riscv_iommu_get_transtag (ctx);RISCVIOMMUEntry *iot;IOMMUAccessFlags perm;bool enable_pid;@@ -1365,7 +1437,7 @@ static int riscv_iommu_translate(RISCVIOMMUState*s, RISCVIOMMUContext *ctx,
hw/riscv/riscv-iommu.c static int riscv_iommu_translate(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
Quoted 5 lines
hw/riscv/riscv-iommu.c +1
-1} } - iot =riscv_iommu_iot_lookup (ctx, iot_cache, iotlb->iova); + iot = riscv_iommu_iot_lookup(ctx, iot_cache, iotlb->iova,transtag);
hw/riscv/riscv-iommu.c static int riscv_iommu_translate(RISCVIOMMUState
Quoted 4 lines
hw/riscv/riscv-iommu.c +0
-0perm = iot ? iot->perm :IOMMU_NONE ;if (perm !=IOMMU_NONE ) { iotlb->translated_addr =PPN_PHYS (iot->phys);@@ -1396,6 +1468,7 @@ static int riscv_iommu_translate(RISCVIOMMUState*s, RISCVIOMMUContext *ctx,
hw/riscv/riscv-iommu.c static int riscv_iommu_translate(RISCVIOMMUState · line 1468
Quoted 1 line
hw/riscv/riscv-iommu.c +0
-0iot->gscid = get_field(ctx->gatp,
RISCV_IOMMU_DC_IOHGATP_GSCID);hw/riscv/riscv-iommu.c static void
Quoted 7 lines
hw/riscv/riscv-iommu.c +1
-0iot->pscid =get_field (ctx->ta,RISCV_IOMMU_DC_TA_PSCID ); iot->perm = iotlb->perm; + iot->tag = transtag;riscv_iommu_iot_update (s, iot_cache, iot); }@@ -1603,44 +1676,72 @@ static void
riscv_iommu_process_cq_tail(RISCVIOMMUState *s)hw/riscv/riscv-iommu.c static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
Quoted 8 lines
hw/riscv/riscv-iommu.c +4
-1case RISCV_IOMMU_CMD (RISCV_IOMMU_CMD_IOTINVAL_FUNC_GVMA ,RISCV_IOMMU_CMD_IOTINVAL_OPCODE ): -if (cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV ) { + { +bool gv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV ); +bool av = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV ); + bool pscv = !!(cmd.dword0 &
RISCV_IOMMU_CMD_IOTINVAL_PSCV);hw/riscv/riscv-iommu.c static void · line 1683
Quoted 2 lines
hw/riscv/riscv-iommu.c +2
-0+ uint32_t gscid = get_field(cmd.dword0, +
RISCV_IOMMU_CMD_IOTINVAL_GSCID);hw/riscv/riscv-iommu.c static void · line 1685
Quoted 2 lines
hw/riscv/riscv-iommu.c +2
-0+ uint32_t pscid = get_field(cmd.dword0, +
RISCV_IOMMU_CMD_IOTINVAL_PSCID);hw/riscv/riscv-iommu.c static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
Quoted 4 lines
hw/riscv/riscv-iommu.c +3
-0+hwaddr iova = (cmd.dword1 <<2 ) &TARGET_PAGE_MASK ; + +if (pscv) {/* illegal command arguments IOTINVAL.GVMA & PSCV == 1 */
hw/riscv/riscv-iommu.c static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
Quoted 13 lines
hw/riscv/riscv-iommu.c +0
-11goto cmd_ill; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV )) { -/* invalidate all cache mappings */ - func = riscv_iommu_iot_inval_all; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV )) { -/* invalidate cache matching GSCID */ - func = riscv_iommu_iot_inval_gscid; - }else { -/* invalidate cache matching GSCID and ADDR (GPA) */ - func = riscv_iommu_iot_inval_gscid_gpa; } - riscv_iommu_iot_inval(s, func, - get_field(cmd.dword0,
RISCV_IOMMU_CMD_IOTINVAL_GSCID), 0,hw/riscv/riscv-iommu.c static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
Quoted 11 lines
hw/riscv/riscv-iommu.c +10
-1- cmd.dword1 << 2 & TARGET_PAGE_MASK); + + func = riscv_iommu_iot_inval_all; + +if (gv) { + func = (av) ? riscv_iommu_iot_inval_gscid_iova : + riscv_iommu_iot_inval_gscid; + } + + riscv_iommu_iot_inval( + s, func, gscid, pscid, iova,
RISCV_IOMMU_TRANS_TAG_VG);hw/riscv/riscv-iommu.c static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
Quoted 3 lines
hw/riscv/riscv-iommu.c +3
-0+ + riscv_iommu_iot_inval( + s, func, gscid, pscid, iova,
RISCV_IOMMU_TRANS_TAG_VN);hw/riscv/riscv-iommu.c static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
Quoted 9 lines
hw/riscv/riscv-iommu.c +1
-4break ; + }case RISCV_IOMMU_CMD (RISCV_IOMMU_CMD_IOTINVAL_FUNC_VMA ,RISCV_IOMMU_CMD_IOTINVAL_OPCODE ): -if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV )) { -/* invalidate all cache mappings, simplified model */ - func = riscv_iommu_iot_inval_all; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV )){
hw/riscv/riscv-iommu.c static void · line 1629
Quoted 1 line
hw/riscv/riscv-iommu.c +0
-1-/* invalidate cache matching GSCID, simplified model */
hw/riscv/riscv-iommu.c static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
Quoted 8 lines
hw/riscv/riscv-iommu.c +4
-4- func = riscv_iommu_iot_inval_gscid; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV )) { -/* invalidate cache matching GSCID and PSCID */ - func = riscv_iommu_iot_inval_pscid; + { +bool gv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV ); +bool av = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV ); + bool pscv = !!(cmd.dword0 &
RISCV_IOMMU_CMD_IOTINVAL_PSCV);hw/riscv/riscv-iommu.c static void · line 1715
Quoted 2 lines
hw/riscv/riscv-iommu.c +2
-0+ uint32_t gscid = get_field(cmd.dword0, +
RISCV_IOMMU_CMD_IOTINVAL_GSCID);hw/riscv/riscv-iommu.c static void · line 1717
Quoted 2 lines
hw/riscv/riscv-iommu.c +2
-0+ uint32_t pscid = get_field(cmd.dword0, +
RISCV_IOMMU_CMD_IOTINVAL_PSCID);hw/riscv/riscv-iommu.c static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
Quoted 7 lines
hw/riscv/riscv-iommu.c +7
-0+hwaddr iova = (cmd.dword1 <<2 ) &TARGET_PAGE_MASK ; +RISCVIOMMUTransTag transtag; + +if (gv) { + transtag =RISCV_IOMMU_TRANS_TAG_VN ; +if (pscv) { + func = (av) ?
riscv_iommu_iot_inval_gscid_pscid_iova:hw/riscv/riscv-iommu.c static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
Quoted 7 lines
hw/riscv/riscv-iommu.c +5
-1+ riscv_iommu_iot_inval_gscid_pscid; + }else { + func = (av) ? riscv_iommu_iot_inval_gscid_iova : + riscv_iommu_iot_inval_gscid; + } }else { -/* invalidate cache matching GSCID and PSCID and ADDR (IOVA) */
Quoted 6 lines
hw/riscv/riscv-iommu.c +5
-1- func = riscv_iommu_iot_inval_pscid_iova; + transtag =RISCV_IOMMU_TRANS_TAG_SS ; +if (pscv) { + func = (av) ? riscv_iommu_iot_inval_pscid_iova : + riscv_iommu_iot_inval_pscid; + }else {hw/riscv/riscv-iommu.c static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)
Quoted 10 lines
hw/riscv/riscv-iommu.c +5
-4+ func = (av) ? riscv_iommu_iot_inval_iova : + riscv_iommu_iot_inval_all; + } } -riscv_iommu_iot_inval (s, func, -get_field (cmd.dword0,RISCV_IOMMU_CMD_IOTINVAL_GSCID ), -get_field (cmd.dword0,RISCV_IOMMU_CMD_IOTINVAL_PSCID ), - cmd.dword1 <<2 &TARGET_PAGE_MASK ); + + riscv_iommu_iot_inval(s, func, gscid, pscid, iova,transtag);
Quoted 5 lines
hw/riscv/riscv-iommu.c +1
-0break ; + }case RISCV_IOMMU_CMD (RISCV_IOMMU_CMD_IODIR_FUNC_INVAL_DDT ,RISCV_IOMMU_CMD_IODIR_OPCODE ):
Quoted 8 lines · Jason Chien
This commit introduces a translation tag to avoid invalidating an entry that should not be invalidated when IOMMU executes invalidation commands. E.g. IOTINVAL.VMA with GV=0, AV=0, PSCV=1 invalidates both a mapping of single stage translation and a mapping of nested translation with the same PSCID, but only the former one should be invalidated.
Signed-off-by Jason Chien <[email protected]>---
Quoted 326 lines
hw/riscv/riscv-iommu.c | 205 ++++++++++++++++++++++++++++++----------- 1 file changed, 153 insertions(+), 52 deletions(-)hw/riscv/riscv-iommu.c +153
-52@@ -64,8 +64,16 @@ struct RISCVIOMMUContext {uint64_t msiptp;/* MSI redirection page table pointer */ }; +typedef enum RISCVIOMMUTransTag { +RISCV_IOMMU_TRANS_TAG_BY ,/* Bypass */ +RISCV_IOMMU_TRANS_TAG_SS ,/* Single Stage */ +RISCV_IOMMU_TRANS_TAG_VG ,/* G-stage only */ +RISCV_IOMMU_TRANS_TAG_VN ,/* Nested translation */ +}RISCVIOMMUTransTag ; +/* Address translation cache entry */ struct RISCVIOMMUEntry { +RISCVIOMMUTransTag tag;/* Translation Tag */ uint64_t iova:44 ;/* IOVA Page Number */ uint64_t pscid:20 ;/* Process Soft-Context identifier */ uint64_t phys:44 ;/* Physical Page Number */ @@ -1228,7 +1236,7 @@ static gboolean riscv_iommu_iot_equal(gconstpointer v1, gconstpointer v2)RISCVIOMMUEntry *t1 = (RISCVIOMMUEntry *) v1;RISCVIOMMUEntry *t2 = (RISCVIOMMUEntry *) v2;return t1->gscid == t2->gscid && t1->pscid == t2->pscid && - t1->iova == t2->iova; + t1->iova == t2->iova && t1->tag == t2->tag; }static guint riscv_iommu_iot_hash (gconstpointer v)@@ -1237,67 +1245,115 @@ static guint riscv_iommu_iot_hash(gconstpointer v)return (guint )t->iova; } -/* GV: 1 PSCV: 1 AV: 1 */ +/* GV: 0 AV: 0 PSCV: 0 GVMA: 0 */ +/* GV: 0 AV: 0 GVMA: 1 */ +static +void riscv_iommu_iot_inval_all (gpointer key,gpointer value,gpointer data) +{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 0 AV: 0 PSCV: 1 GVMA: 0 */ +static +void riscv_iommu_iot_inval_pscid (gpointer key,gpointer value,gpointer data) +{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag && + iot->pscid == arg->pscid) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 0 AV: 1 PSCV: 0 GVMA: 0 */ +static +void riscv_iommu_iot_inval_iova (gpointer key,gpointer value,gpointer data) +{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag && + iot->iova == arg->iova) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 0 AV: 1 PSCV: 1 GVMA: 0 */ static void riscv_iommu_iot_inval_pscid_iova (gpointer key,gpointer value,gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid && +if (iot->tag == arg->tag && iot->pscid == arg->pscid && iot->iova == arg->iova) { iot->perm =IOMMU_NONE ; } } -/* GV: 1 PSCV: 1 AV: 0 */ -static void riscv_iommu_iot_inval_pscid (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 0 PSCV: 0 GVMA: 0 */ +/* GV: 1 AV: 0 GVMA: 1 */ +static +void riscv_iommu_iot_inval_gscid (gpointer key,gpointer value,gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid && - iot->pscid == arg->pscid) { +if (iot->tag == arg->tag && + iot->gscid == arg->gscid) { iot->perm =IOMMU_NONE ; } } -/* GV: 1 GVMA: 1 */ -static void riscv_iommu_iot_inval_gscid_gpa (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 0 PSCV: 1 GVMA: 0 */ +static void riscv_iommu_iot_inval_gscid_pscid (gpointer key,gpointer value, +gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid) { -/* simplified cache, no GPA matching */ +if (iot->tag == arg->tag && + iot->gscid == arg->gscid && + iot->pscid == arg->pscid) { iot->perm =IOMMU_NONE ; } } -/* GV: 1 GVMA: 0 */ -static void riscv_iommu_iot_inval_gscid (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 1 PSCV: 0 GVMA: 0 */ +/* GV: 1 AV: 1 GVMA: 1 */ +static void riscv_iommu_iot_inval_gscid_iova (gpointer key,gpointer value, +gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid) { +if (iot->tag == arg->tag && + iot->gscid == arg->gscid && + iot->iova == arg->iova) { iot->perm =IOMMU_NONE ; } } -/* GV: 0 */ -static void riscv_iommu_iot_inval_all (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 1 PSCV: 1 GVMA: 0 */ +static void riscv_iommu_iot_inval_gscid_pscid_iova (gpointer key,gpointer value, +gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; - iot->perm =IOMMU_NONE ; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag && + iot->gscid == arg->gscid && + iot->pscid == arg->pscid && + iot->iova == arg->iova) { + iot->perm =IOMMU_NONE ; + } }/* caller should keep ref-count for iot_cache object */ static RISCVIOMMUEntry *riscv_iommu_iot_lookup (RISCVIOMMUContext *ctx, -GHashTable *iot_cache,hwaddr iova) +GHashTable *iot_cache,hwaddr iova,RISCVIOMMUTransTag transtag) {RISCVIOMMUEntry key = { + .tag = transtag, .gscid =get_field (ctx->gatp,RISCV_IOMMU_DC_IOHGATP_GSCID ), .pscid =get_field (ctx->ta,RISCV_IOMMU_DC_TA_PSCID ), .iova =PPN_DOWN (iova),@@ -1323,10 +1379,11 @@ static void riscv_iommu_iot_update(RISCVIOMMUState *s, }static void riscv_iommu_iot_inval (RISCVIOMMUState *s,GHFunc func, -uint32_t gscid,uint32_t pscid,hwaddr iova) +uint32_t gscid,uint32_t pscid,hwaddr iova,RISCVIOMMUTransTag transtag) {GHashTable *iot_cache;RISCVIOMMUEntry key = { + .tag = transtag, .gscid = gscid, .pscid = pscid, .iova =PPN_DOWN (iova),@@ -1337,9 +1394,24 @@ static void riscv_iommu_iot_inval(RISCVIOMMUState *s, GHFunc func,g_hash_table_unref (iot_cache); } +static RISCVIOMMUTransTag riscv_iommu_get_transtag (RISCVIOMMUContext *ctx) +{ +uint64_t satp =get_field (ctx->satp,RISCV_IOMMU_ATP_MODE_FIELD ); +uint64_t gatp =get_field (ctx->gatp,RISCV_IOMMU_ATP_MODE_FIELD ); + +if (satp ==RISCV_IOMMU_DC_FSC_MODE_BARE ) { +return (gatp ==RISCV_IOMMU_DC_IOHGATP_MODE_BARE ) ? +RISCV_IOMMU_TRANS_TAG_BY :RISCV_IOMMU_TRANS_TAG_VG ; + }else { +return (gatp ==RISCV_IOMMU_DC_IOHGATP_MODE_BARE ) ? +RISCV_IOMMU_TRANS_TAG_SS :RISCV_IOMMU_TRANS_TAG_VN ; + } +} +static int riscv_iommu_translate (RISCVIOMMUState *s,RISCVIOMMUContext *ctx,IOMMUTLBEntry *iotlb,bool enable_cache) { +RISCVIOMMUTransTag transtag =riscv_iommu_get_transtag (ctx);RISCVIOMMUEntry *iot;IOMMUAccessFlags perm;bool enable_pid;@@ -1365,7 +1437,7 @@ static int riscv_iommu_translate(RISCVIOMMUState *s, RISCVIOMMUContext *ctx, } } - iot =riscv_iommu_iot_lookup (ctx, iot_cache, iotlb->iova); + iot =riscv_iommu_iot_lookup (ctx, iot_cache, iotlb->iova, transtag); perm = iot ? iot->perm :IOMMU_NONE ;if (perm !=IOMMU_NONE ) { iotlb->translated_addr =PPN_PHYS (iot->phys);@@ -1396,6 +1468,7 @@ static int riscv_iommu_translate(RISCVIOMMUState *s, RISCVIOMMUContext *ctx, iot->gscid =get_field (ctx->gatp,RISCV_IOMMU_DC_IOHGATP_GSCID ); iot->pscid =get_field (ctx->ta,RISCV_IOMMU_DC_TA_PSCID ); iot->perm = iotlb->perm; + iot->tag = transtag;riscv_iommu_iot_update (s, iot_cache, iot); }@@ -1603,44 +1676,72 @@ static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)case RISCV_IOMMU_CMD (RISCV_IOMMU_CMD_IOTINVAL_FUNC_GVMA ,RISCV_IOMMU_CMD_IOTINVAL_OPCODE ): -if (cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV ) { + { +bool gv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV ); +bool av = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV ); +bool pscv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV ); +uint32_t gscid =get_field (cmd.dword0, +RISCV_IOMMU_CMD_IOTINVAL_GSCID ); +uint32_t pscid =get_field (cmd.dword0, +RISCV_IOMMU_CMD_IOTINVAL_PSCID ); +hwaddr iova = (cmd.dword1 <<2 ) &TARGET_PAGE_MASK ; + +if (pscv) {/* illegal command arguments IOTINVAL.GVMA & PSCV == 1 */ goto cmd_ill; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV )) { -/* invalidate all cache mappings */ - func = riscv_iommu_iot_inval_all; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV )) { -/* invalidate cache matching GSCID */ - func = riscv_iommu_iot_inval_gscid; - }else { -/* invalidate cache matching GSCID and ADDR (GPA) */ - func = riscv_iommu_iot_inval_gscid_gpa; } -riscv_iommu_iot_inval (s, func, -get_field (cmd.dword0,RISCV_IOMMU_CMD_IOTINVAL_GSCID ),0 , - cmd.dword1 <<2 &TARGET_PAGE_MASK ); + + func = riscv_iommu_iot_inval_all; + +if (gv) { + func = (av) ? riscv_iommu_iot_inval_gscid_iova : + riscv_iommu_iot_inval_gscid; + } + +riscv_iommu_iot_inval ( + s, func, gscid, pscid, iova,RISCV_IOMMU_TRANS_TAG_VG ); + +riscv_iommu_iot_inval ( + s, func, gscid, pscid, iova,RISCV_IOMMU_TRANS_TAG_VN );break ; + }case RISCV_IOMMU_CMD (RISCV_IOMMU_CMD_IOTINVAL_FUNC_VMA ,RISCV_IOMMU_CMD_IOTINVAL_OPCODE ): -if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV )) { -/* invalidate all cache mappings, simplified model */ - func = riscv_iommu_iot_inval_all; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV )) { -/* invalidate cache matching GSCID, simplified model */ - func = riscv_iommu_iot_inval_gscid; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV )) { -/* invalidate cache matching GSCID and PSCID */ - func = riscv_iommu_iot_inval_pscid; + { +bool gv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV ); +bool av = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV ); +bool pscv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV ); +uint32_t gscid =get_field (cmd.dword0, +RISCV_IOMMU_CMD_IOTINVAL_GSCID ); +uint32_t pscid =get_field (cmd.dword0, +RISCV_IOMMU_CMD_IOTINVAL_PSCID ); +hwaddr iova = (cmd.dword1 <<2 ) &TARGET_PAGE_MASK ; +RISCVIOMMUTransTag transtag; + +if (gv) { + transtag =RISCV_IOMMU_TRANS_TAG_VN ; +if (pscv) { + func = (av) ? riscv_iommu_iot_inval_gscid_pscid_iova : + riscv_iommu_iot_inval_gscid_pscid; + }else { + func = (av) ? riscv_iommu_iot_inval_gscid_iova : + riscv_iommu_iot_inval_gscid; + } }else { -/* invalidate cache matching GSCID and PSCID and ADDR (IOVA) */ - func = riscv_iommu_iot_inval_pscid_iova; + transtag =RISCV_IOMMU_TRANS_TAG_SS ; +if (pscv) { + func = (av) ? riscv_iommu_iot_inval_pscid_iova : + riscv_iommu_iot_inval_pscid; + }else { + func = (av) ? riscv_iommu_iot_inval_iova : + riscv_iommu_iot_inval_all; + } } -riscv_iommu_iot_inval (s, func, -get_field (cmd.dword0,RISCV_IOMMU_CMD_IOTINVAL_GSCID ), -get_field (cmd.dword0,RISCV_IOMMU_CMD_IOTINVAL_PSCID ), - cmd.dword1 <<2 &TARGET_PAGE_MASK ); + +riscv_iommu_iot_inval (s, func, gscid, pscid, iova, transtag);break ; + }case RISCV_IOMMU_CMD (RISCV_IOMMU_CMD_IODIR_FUNC_INVAL_DDT ,RISCV_IOMMU_CMD_IODIR_OPCODE ):
Alistair Francis
AppliedQuoted 8 lines · Jason Chien
This commit introduces a translation tag to avoid invalidating an entry that should not be invalidated when IOMMU executes invalidation commands. E.g. IOTINVAL.VMA with GV=0, AV=0, PSCV=1 invalidates both a mapping of single stage translation and a mapping of nested translation with the same PSCID, but only the former one should be invalidated.
Signed-off-by Jason Chien <[email protected]>
Thanks!
Applied to riscv-to-apply.next
Alistair
Quoted 331 lines
--- hw/riscv/riscv-iommu.c | 205 ++++++++++++++++++++++++++++++----------- 1 file changed, 153 insertions(+), 52 deletions(-)hw/riscv/riscv-iommu.c +153
-53@@ -64,8 +64,16 @@ struct RISCVIOMMUContext {uint64_t msiptp;/* MSI redirection page table pointer */ }; +typedef enum RISCVIOMMUTransTag { +RISCV_IOMMU_TRANS_TAG_BY ,/* Bypass */ +RISCV_IOMMU_TRANS_TAG_SS ,/* Single Stage */ +RISCV_IOMMU_TRANS_TAG_VG ,/* G-stage only */ +RISCV_IOMMU_TRANS_TAG_VN ,/* Nested translation */ +}RISCVIOMMUTransTag ; +/* Address translation cache entry */ struct RISCVIOMMUEntry { +RISCVIOMMUTransTag tag;/* Translation Tag */ uint64_t iova:44 ;/* IOVA Page Number */ uint64_t pscid:20 ;/* Process Soft-Context identifier */ uint64_t phys:44 ;/* Physical Page Number */ @@ -1228,7 +1236,7 @@ static gboolean riscv_iommu_iot_equal(gconstpointer v1, gconstpointer v2)RISCVIOMMUEntry *t1 = (RISCVIOMMUEntry *) v1;RISCVIOMMUEntry *t2 = (RISCVIOMMUEntry *) v2;return t1->gscid == t2->gscid && t1->pscid == t2->pscid && - t1->iova == t2->iova; + t1->iova == t2->iova && t1->tag == t2->tag; }static guint riscv_iommu_iot_hash (gconstpointer v)@@ -1237,67 +1245,115 @@ static guint riscv_iommu_iot_hash(gconstpointer v)return (guint )t->iova; } -/* GV: 1 PSCV: 1 AV: 1 */ +/* GV: 0 AV: 0 PSCV: 0 GVMA: 0 */ +/* GV: 0 AV: 0 GVMA: 1 */ +static +void riscv_iommu_iot_inval_all (gpointer key,gpointer value,gpointer data) +{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 0 AV: 0 PSCV: 1 GVMA: 0 */ +static +void riscv_iommu_iot_inval_pscid (gpointer key,gpointer value,gpointer data) +{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag && + iot->pscid == arg->pscid) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 0 AV: 1 PSCV: 0 GVMA: 0 */ +static +void riscv_iommu_iot_inval_iova (gpointer key,gpointer value,gpointer data) +{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag && + iot->iova == arg->iova) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 0 AV: 1 PSCV: 1 GVMA: 0 */ static void riscv_iommu_iot_inval_pscid_iova (gpointer key,gpointer value,gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid && +if (iot->tag == arg->tag && iot->pscid == arg->pscid && iot->iova == arg->iova) { iot->perm =IOMMU_NONE ; } } -/* GV: 1 PSCV: 1 AV: 0 */ -static void riscv_iommu_iot_inval_pscid (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 0 PSCV: 0 GVMA: 0 */ +/* GV: 1 AV: 0 GVMA: 1 */ +static +void riscv_iommu_iot_inval_gscid (gpointer key,gpointer value,gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid && - iot->pscid == arg->pscid) { +if (iot->tag == arg->tag && + iot->gscid == arg->gscid) { iot->perm =IOMMU_NONE ; } } -/* GV: 1 GVMA: 1 */ -static void riscv_iommu_iot_inval_gscid_gpa (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 0 PSCV: 1 GVMA: 0 */ +static void riscv_iommu_iot_inval_gscid_pscid (gpointer key,gpointer value, +gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid) { -/* simplified cache, no GPA matching */ +if (iot->tag == arg->tag && + iot->gscid == arg->gscid && + iot->pscid == arg->pscid) { iot->perm =IOMMU_NONE ; } } -/* GV: 1 GVMA: 0 */ -static void riscv_iommu_iot_inval_gscid (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 1 PSCV: 0 GVMA: 0 */ +/* GV: 1 AV: 1 GVMA: 1 */ +static void riscv_iommu_iot_inval_gscid_iova (gpointer key,gpointer value, +gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value;RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; -if (iot->gscid == arg->gscid) { +if (iot->tag == arg->tag && + iot->gscid == arg->gscid && + iot->iova == arg->iova) { iot->perm =IOMMU_NONE ; } } -/* GV: 0 */ -static void riscv_iommu_iot_inval_all (gpointer key,gpointer value, -gpointer data) +/* GV: 1 AV: 1 PSCV: 1 GVMA: 0 */ +static void riscv_iommu_iot_inval_gscid_pscid_iova (gpointer key,gpointer value, +gpointer data) {RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; - iot->perm =IOMMU_NONE ; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->tag == arg->tag && + iot->gscid == arg->gscid && + iot->pscid == arg->pscid && + iot->iova == arg->iova) { + iot->perm =IOMMU_NONE ; + } }/* caller should keep ref-count for iot_cache object */ static RISCVIOMMUEntry *riscv_iommu_iot_lookup (RISCVIOMMUContext *ctx, -GHashTable *iot_cache,hwaddr iova) +GHashTable *iot_cache,hwaddr iova,RISCVIOMMUTransTag transtag) {RISCVIOMMUEntry key = { + .tag = transtag, .gscid =get_field (ctx->gatp,RISCV_IOMMU_DC_IOHGATP_GSCID ), .pscid =get_field (ctx->ta,RISCV_IOMMU_DC_TA_PSCID ), .iova =PPN_DOWN (iova),@@ -1323,10 +1379,11 @@ static void riscv_iommu_iot_update(RISCVIOMMUState *s, }static void riscv_iommu_iot_inval (RISCVIOMMUState *s,GHFunc func, -uint32_t gscid,uint32_t pscid,hwaddr iova) +uint32_t gscid,uint32_t pscid,hwaddr iova,RISCVIOMMUTransTag transtag) {GHashTable *iot_cache;RISCVIOMMUEntry key = { + .tag = transtag, .gscid = gscid, .pscid = pscid, .iova =PPN_DOWN (iova),@@ -1337,9 +1394,24 @@ static void riscv_iommu_iot_inval(RISCVIOMMUState *s, GHFunc func,g_hash_table_unref (iot_cache); } +static RISCVIOMMUTransTag riscv_iommu_get_transtag (RISCVIOMMUContext *ctx) +{ +uint64_t satp =get_field (ctx->satp,RISCV_IOMMU_ATP_MODE_FIELD ); +uint64_t gatp =get_field (ctx->gatp,RISCV_IOMMU_ATP_MODE_FIELD ); + +if (satp ==RISCV_IOMMU_DC_FSC_MODE_BARE ) { +return (gatp ==RISCV_IOMMU_DC_IOHGATP_MODE_BARE ) ? +RISCV_IOMMU_TRANS_TAG_BY :RISCV_IOMMU_TRANS_TAG_VG ; + }else { +return (gatp ==RISCV_IOMMU_DC_IOHGATP_MODE_BARE ) ? +RISCV_IOMMU_TRANS_TAG_SS :RISCV_IOMMU_TRANS_TAG_VN ; + } +} +static int riscv_iommu_translate (RISCVIOMMUState *s,RISCVIOMMUContext *ctx,IOMMUTLBEntry *iotlb,bool enable_cache) { +RISCVIOMMUTransTag transtag =riscv_iommu_get_transtag (ctx);RISCVIOMMUEntry *iot;IOMMUAccessFlags perm;bool enable_pid;@@ -1365,7 +1437,7 @@ static int riscv_iommu_translate(RISCVIOMMUState *s, RISCVIOMMUContext *ctx, } } - iot =riscv_iommu_iot_lookup (ctx, iot_cache, iotlb->iova); + iot =riscv_iommu_iot_lookup (ctx, iot_cache, iotlb->iova, transtag); perm = iot ? iot->perm :IOMMU_NONE ;if (perm !=IOMMU_NONE ) { iotlb->translated_addr =PPN_PHYS (iot->phys);@@ -1396,6 +1468,7 @@ static int riscv_iommu_translate(RISCVIOMMUState *s, RISCVIOMMUContext *ctx, iot->gscid =get_field (ctx->gatp,RISCV_IOMMU_DC_IOHGATP_GSCID ); iot->pscid =get_field (ctx->ta,RISCV_IOMMU_DC_TA_PSCID ); iot->perm = iotlb->perm; + iot->tag = transtag;riscv_iommu_iot_update (s, iot_cache, iot); }@@ -1603,44 +1676,72 @@ static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)case RISCV_IOMMU_CMD (RISCV_IOMMU_CMD_IOTINVAL_FUNC_GVMA ,RISCV_IOMMU_CMD_IOTINVAL_OPCODE ): -if (cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV ) { + { +bool gv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV ); +bool av = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV ); +bool pscv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV ); +uint32_t gscid =get_field (cmd.dword0, +RISCV_IOMMU_CMD_IOTINVAL_GSCID ); +uint32_t pscid =get_field (cmd.dword0, +RISCV_IOMMU_CMD_IOTINVAL_PSCID ); +hwaddr iova = (cmd.dword1 <<2 ) &TARGET_PAGE_MASK ; + +if (pscv) {/* illegal command arguments IOTINVAL.GVMA & PSCV == 1 */ goto cmd_ill; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV )) { -/* invalidate all cache mappings */ - func = riscv_iommu_iot_inval_all; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV )) { -/* invalidate cache matching GSCID */ - func = riscv_iommu_iot_inval_gscid; - }else { -/* invalidate cache matching GSCID and ADDR (GPA) */ - func = riscv_iommu_iot_inval_gscid_gpa; } -riscv_iommu_iot_inval (s, func, -get_field (cmd.dword0,RISCV_IOMMU_CMD_IOTINVAL_GSCID ),0 , - cmd.dword1 <<2 &TARGET_PAGE_MASK ); + + func = riscv_iommu_iot_inval_all; + +if (gv) { + func = (av) ? riscv_iommu_iot_inval_gscid_iova : + riscv_iommu_iot_inval_gscid; + } + +riscv_iommu_iot_inval ( + s, func, gscid, pscid, iova,RISCV_IOMMU_TRANS_TAG_VG ); + +riscv_iommu_iot_inval ( + s, func, gscid, pscid, iova,RISCV_IOMMU_TRANS_TAG_VN );break ; + }case RISCV_IOMMU_CMD (RISCV_IOMMU_CMD_IOTINVAL_FUNC_VMA ,RISCV_IOMMU_CMD_IOTINVAL_OPCODE ): -if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV )) { -/* invalidate all cache mappings, simplified model */ - func = riscv_iommu_iot_inval_all; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV )) { -/* invalidate cache matching GSCID, simplified model */ - func = riscv_iommu_iot_inval_gscid; - }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV )) { -/* invalidate cache matching GSCID and PSCID */ - func = riscv_iommu_iot_inval_pscid; + { +bool gv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV ); +bool av = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV ); +bool pscv = !!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV ); +uint32_t gscid =get_field (cmd.dword0, +RISCV_IOMMU_CMD_IOTINVAL_GSCID ); +uint32_t pscid =get_field (cmd.dword0, +RISCV_IOMMU_CMD_IOTINVAL_PSCID ); +hwaddr iova = (cmd.dword1 <<2 ) &TARGET_PAGE_MASK ; +RISCVIOMMUTransTag transtag; + +if (gv) { + transtag =RISCV_IOMMU_TRANS_TAG_VN ; +if (pscv) { + func = (av) ? riscv_iommu_iot_inval_gscid_pscid_iova : + riscv_iommu_iot_inval_gscid_pscid; + }else { + func = (av) ? riscv_iommu_iot_inval_gscid_iova : + riscv_iommu_iot_inval_gscid; + } }else { -/* invalidate cache matching GSCID and PSCID and ADDR (IOVA) */ - func = riscv_iommu_iot_inval_pscid_iova; + transtag =RISCV_IOMMU_TRANS_TAG_SS ; +if (pscv) { + func = (av) ? riscv_iommu_iot_inval_pscid_iova : + riscv_iommu_iot_inval_pscid; + }else { + func = (av) ? riscv_iommu_iot_inval_iova : + riscv_iommu_iot_inval_all; + } } -riscv_iommu_iot_inval (s, func, -get_field (cmd.dword0,RISCV_IOMMU_CMD_IOTINVAL_GSCID ), -get_field (cmd.dword0,RISCV_IOMMU_CMD_IOTINVAL_PSCID ), - cmd.dword1 <<2 &TARGET_PAGE_MASK ); + +riscv_iommu_iot_inval (s, func, gscid, pscid, iova, transtag);break ; + }case RISCV_IOMMU_CMD (RISCV_IOMMU_CMD_IODIR_FUNC_INVAL_DDT ,RISCV_IOMMU_CMD_IODIR_OPCODE ): --2.43.2