Skip to contentSkip to conversation

target/riscv: add RVTSO memory ordering for vector memory accesses

PATCH New WX Chen 1 participant 1 message Nov 25 RISC-V

lore
Share & tools
lore

WX Chen

author
#0 lore raw

PATCH v2 1/1 target/riscv: add RVTSO memory ordering for vector memory accesses

The RISC-V Ztso extension provides default memory ordering of:

    R -> R,  R -> W,  W -> W,  without W -> R,

but ldst_stride_trans, ldst_index_trans, ldff_trans and ldst_whole_trans currently lack the ordering listed above. This may not satisfy the guest requirements on hosts with weak memory ordering.
This patch adds the above ordering in these functions.

---
Changes in v2:
- Handle ldst_index_trans, ldff_trans, ldst_whole_trans.
- Add references to the Ztso comment in ldst_us_trans.
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]

---
 target/riscv/tcg/insn_trans/trans_rvv.c.inc | 56 +++++++++++++++++++++++++----
 1 file changed, 50 insertions(+), 6 deletions(-)
target/riscv/tcg/insn_trans/trans_rvv.c.inc +50 -6
index 7e1295f33e7d4b7a2ff26cbf428f6b5d9eb682ff..0949884b426b7d0995c76d11c1fde9445a9dcac9 100644
@@ -880,7 +880,7 @@ typedef void gen_helper_ldst_stride(TCGv_ptr, TCGv_ptr, TCGv,
 
 static bool ldst_stride_trans(uint32_t vd, uint32_t rs1, uint32_t rs2,
                               uint32_t data, gen_helper_ldst_stride *fn,
-                              DisasContext *s)
+                              DisasContext *s, bool is_store)
 {
     TCGv_ptr dest, mask;
     TCGv base, stride;
@@ -896,10 +896,22 @@ static bool ldst_stride_trans(uint32_t vd, uint32_t rs1, uint32_t rs2,
     tcg_gen_addi_ptr(dest, tcg_env, vreg_ofs(s, vd));
     tcg_gen_addi_ptr(mask, tcg_env, vreg_ofs(s, 0));
 
+    /*
+     * See ztso comment in ldst_us_trans for more info on why
+     * the barriers are put around the loop.
+     */
+    if (is_store && s->ztso) {
+        tcg_gen_mb(TCG_MO_LD_ST | TCG_MO_ST_ST | TCG_BAR_STRL);
+    }
+
     mark_vs_dirty(s);
 
     fn(dest, mask, base, stride, tcg_env, desc);
 
+    if (!is_store && s->ztso) {
+        tcg_gen_mb(TCG_MO_LD_LD | TCG_MO_LD_ST | TCG_BAR_LDAQ);
+    }
+
     finalize_rvv_inst(s);
     return true;
 }
@@ -924,7 +936,7 @@ static bool ld_stride_op(DisasContext *s, arg_rnfvm *a, uint8_t eew)
     data = FIELD_DP32(data, VDATA, NF, a->nf);
     data = FIELD_DP32(data, VDATA, VTA, s->vta);
     data = FIELD_DP32(data, VDATA, VMA, s->vma);
-    return ldst_stride_trans(a->rd, a->rs1, a->rs2, data, fn, s);
+    return ldst_stride_trans(a->rd, a->rs1, a->rs2, data, fn, s, false);
 }
 
 static bool ld_stride_check(DisasContext *s, arg_rnfvm* a, uint8_t eew)
@@ -958,7 +970,7 @@ static bool st_stride_op(DisasContext *s, arg_rnfvm *a, uint8_t eew)
         return false;
     }
 
-    return ldst_stride_trans(a->rd, a->rs1, a->rs2, data, fn, s);
+    return ldst_stride_trans(a->rd, a->rs1, a->rs2, data, fn, s, true);
 }
 
 static bool st_stride_check(DisasContext *s, arg_rnfvm* a, uint8_t eew)
@@ -981,7 +993,7 @@ typedef void gen_helper_ldst_index(TCGv_ptr, TCGv_ptr, TCGv,
 
 static bool ldst_index_trans(uint32_t vd, uint32_t rs1, uint32_t vs2,
                              uint32_t data, gen_helper_ldst_index *fn,
-                             DisasContext *s)
+                             DisasContext *s, bool is_store)
 {
     TCGv_ptr dest, mask, index;
     TCGv base;
@@ -998,10 +1010,22 @@ static bool ldst_index_trans(uint32_t vd, uint32_t rs1, uint32_t vs2,
     tcg_gen_addi_ptr(index, tcg_env, vreg_ofs(s, vs2));
     tcg_gen_addi_ptr(mask, tcg_env, vreg_ofs(s, 0));
 
+    /*
+     * See ztso comment in ldst_us_trans for more info on why
+     * the barriers are put around the loop.
+     */
+    if (is_store && s->ztso) {
+        tcg_gen_mb(TCG_MO_LD_ST | TCG_MO_ST_ST | TCG_BAR_STRL);
+    }
+
     mark_vs_dirty(s);
 
     fn(dest, mask, base, index, tcg_env, desc);
 
+    if (!is_store && s->ztso) {
+        tcg_gen_mb(TCG_MO_LD_LD | TCG_MO_LD_ST | TCG_BAR_LDAQ);
+    }
+
     finalize_rvv_inst(s);
     return true;
 }
@@ -1045,7 +1069,7 @@ static bool ld_index_op(DisasContext *s, arg_rnfvm *a, uint8_t eew)
     data = FIELD_DP32(data, VDATA, NF, a->nf);
     data = FIELD_DP32(data, VDATA, VTA, s->vta);
     data = FIELD_DP32(data, VDATA, VMA, s->vma);
-    return ldst_index_trans(a->rd, a->rs1, a->rs2, data, fn, s);
+    return ldst_index_trans(a->rd, a->rs1, a->rs2, data, fn, s, false);
 }
 
 static bool ld_index_check(DisasContext *s, arg_rnfvm* a, uint8_t eew)
@@ -1098,7 +1122,7 @@ static bool st_index_op(DisasContext *s, arg_rnfvm *a, uint8_t eew)
     data = FIELD_DP32(data, VDATA, VM, a->vm);
     data = FIELD_DP32(data, VDATA, LMUL, emul);
     data = FIELD_DP32(data, VDATA, NF, a->nf);
-    return ldst_index_trans(a->rd, a->rs1, a->rs2, data, fn, s);
+    return ldst_index_trans(a->rd, a->rs1, a->rs2, data, fn, s, true);
 }
 
 static bool st_index_check(DisasContext *s, arg_rnfvm* a, uint8_t eew)
@@ -1135,6 +1159,14 @@ static bool ldff_trans(uint32_t vd, uint32_t rs1, uint32_t data,
 
     fn(dest, mask, base, tcg_env, desc);
 
+    /*
+     * See ztso comment in ldst_us_trans for more info on why
+     * the barriers are put around the loop.
+     */
+    if (s->ztso) {
+        tcg_gen_mb(TCG_MO_LD_LD | TCG_MO_LD_ST | TCG_BAR_LDAQ);
+    }
+
     finalize_rvv_inst(s);
 
     /* vector unit-stride fault-only-first load may modify vl CSR */
@@ -1193,6 +1225,14 @@ static bool ldst_whole_trans(uint32_t vd, uint32_t rs1, uint32_t nf,
      */
     bool use_helper_fn = !s->vstart_eq_zero;
 
+    /*
+     * See ztso comment in ldst_us_trans for more info on why
+     * the barriers are put around the loop.
+     */
+    if (!is_load && s->ztso) {
+        tcg_gen_mb(TCG_MO_LD_ST | TCG_MO_ST_ST | TCG_BAR_STRL);
+    }
+
     if (!use_helper_fn) {
         uint32_t size = s->cfg_ptr->vlenb * nf;
         TCGv_i64 t8 = tcg_temp_new_i64();
@@ -1244,6 +1284,10 @@ static bool ldst_whole_trans(uint32_t vd, uint32_t rs1, uint32_t nf,
         fn(dest, base, tcg_env, desc);
     }
 
+    if (is_load && s->ztso) {
+        tcg_gen_mb(TCG_MO_LD_LD | TCG_MO_LD_ST | TCG_BAR_LDAQ);
+    }
+
     finalize_rvv_inst(s);
     return true;
 }

---
base-commit: 08ed97c33c3818ef51a7c852347a28e9f66807b5
change-id: 20261121-fix-rvtso-vector-stride-39fd6f035f17

Best regards,

Signature · 1 line
WX Chen <[email protected]>