data = FIELD_DP32(data, CPUCFG2, LLACQ_SCREL, 1);
data = FIELD_DP32(data, CPUCFG2, SCQ, 1);
cpu->env.cpucfg[2] = data;
+
+ data = cpu->env.cpucfg[3];
+ data = FIELD_DP32(data, CPUCFG3, DBAR_HINTS, 1);
+ cpu->env.cpucfg[3] = data;
}
}
return true;
}
+/*
+ * Decode dbar hint and emit appropriate TCG memory barrier.
+ *
+ * The hint is a 5-bit field (0-31) encoded in the instruction.
+ * For hint 0x700 (special LL/SC loop barrier), treat as full barrier.
+ *
+ * See LoongArch Reference Manual v1.10, Section 4.2.2 for details.
+ */
static bool trans_dbar(DisasContext *ctx, arg_dbar * a)
{
- tcg_gen_mb(TCG_BAR_SC | TCG_MO_ALL);
+ int hint = a->imm;
+ TCGBar bar_flags = 0;
+
+ /* Reserved/no-op hints: 0xf and 0x1f */
+ if (hint == 0xf || hint == 0x1f) {
+ return true;
+ }
+
+ /* If the CPU does not support fine-grained hints,or for the special LL/SC
+ * loop barrier (0x700), emit a full barrier.
+ */
+ if (!avail_DBAR_HINT(ctx) || hint == 0x700) {
+ tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
+ return true;
+ }
+
+ /*
+ * Fine-grained hint decoding:
+ * The hint is a 5-bit field (bits 4-0). Bit4 is reserved and currently
+ * ignored/discarded. Only bits 3-0 are used for ordering control.
+ * Bit3: barrier for previous read (0: true, 1: false)
+ * Bit2: barrier for previous write (0: true, 1: false)
+ * Bit1: barrier for succeeding read (0: true, 1: false)
+ * Bit0: barrier for succeeding write (0: true, 1: false)
+ *
+ * For each combination, we set the corresponding TCG_MO_* flag if both
+ * sides of the barrier require ordering.
+ */
+
+ bool prev_rd = !(hint & 0x08); /* bit3 */
+ bool prev_wr = !(hint & 0x04); /* bit2 */
+ bool succ_rd = !(hint & 0x02); /* bit1 */
+ bool succ_wr = !(hint & 0x01); /* bit0 */
+
+ if (prev_rd) {
+ bar_flags |= TCG_MO_LD_LD | TCG_MO_LD_ST;
+ }
+ if (prev_wr) {
+ bar_flags |= TCG_MO_ST_LD | TCG_MO_ST_ST;
+ }
+ if (succ_rd) {
+ bar_flags |= TCG_MO_LD_LD | TCG_MO_ST_LD;
+ }
+ if (succ_wr) {
+ bar_flags |= TCG_MO_ST_ST | TCG_MO_LD_ST;
+ }
+
+ if (bar_flags == 0) {
+ bar_flags = TCG_MO_ALL;
+ }
+
+ tcg_gen_mb(bar_flags | TCG_BAR_SC);
return true;
-}
+ }
static bool trans_ibar(DisasContext *ctx, arg_ibar *a)
{
#define avail_LLACQ_SCREL(C) (FIELD_EX32((C)->cpucfg2, CPUCFG2, LLACQ_SCREL))
#define avail_LLACQ_SCREL_64(C) (avail_64(C) && avail_LLACQ_SCREL(C))
+#define avail_DBAR_HINT(C) (FIELD_EX32((C)->cpucfg3, CPUCFG3, DBAR_HINTS))
+
/*
* If an operation is being performed on less than TARGET_LONG_BITS,
* it may require the inputs to be sign- or zero-extended; which will
bool va32; /* 32-bit virtual address */
uint32_t cpucfg1;
uint32_t cpucfg2;
+ uint32_t cpucfg3;
} DisasContext;
void generate_exception(DisasContext *ctx, int excp);