public inbox for opensbi@lists.infradead.org
 help / color / mirror / Atom feed
From: Nicholas Piggin <npiggin@gmail.com>
To: opensbi@lists.infradead.org
Cc: Nicholas Piggin <npiggin@gmail.com>,
	Himanshu Chauhan <hchauhan@ventanamicro.com>
Subject: [PATCH 17/18] dbtr: Heterogeneous access type matching for mcontrol triggers
Date: Fri, 13 Mar 2026 15:19:46 +1000	[thread overview]
Message-ID: <20260313051948.4017134-18-npiggin@gmail.com> (raw)
In-Reply-To: <20260313051948.4017134-1-npiggin@gmail.com>

The Tenstorrent Ascalon core has a set of mcontrol6 triggers which can
match load/store, and a set which can match execute.

Detect access types supported in mc/mc6 triggers by writing 1 to
load/store/access fields and reading back the result. Unsupported bits
read back 0.

Account for this capability when allocating HW triggers.

Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
 include/sbi/sbi_dbtr.h |  1 +
 lib/sbi/sbi_dbtr.c     | 77 ++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 76 insertions(+), 2 deletions(-)

diff --git a/include/sbi/sbi_dbtr.h b/include/sbi/sbi_dbtr.h
index c0b9369e..16407f34 100644
--- a/include/sbi/sbi_dbtr.h
+++ b/include/sbi/sbi_dbtr.h
@@ -52,6 +52,7 @@ struct sbi_hw_trigger {
 	unsigned long index;
 	bool inuse; /* Allocated for a DBTR trigger */
 	unsigned long type_mask; /* Types supported by this trigger */
+	unsigned long mcontrol_rwx_mask; /* rwx match bits supported */
 };
 
 struct sbi_dbtr_trigger {
diff --git a/lib/sbi/sbi_dbtr.c b/lib/sbi/sbi_dbtr.c
index f91a2b0d..f3186993 100644
--- a/lib/sbi/sbi_dbtr.c
+++ b/lib/sbi/sbi_dbtr.c
@@ -60,6 +60,15 @@ static unsigned long hart_state_ptr_offset;
 
 #define DBTR_SHMEM_MAKE_PHYS(_p_hi, _p_lo) (_p_lo)
 
+/*
+ * MC and MC6 type and access fields are the same, no
+ * point duplicating this with MC vs MC6 accessors.
+ */
+#define RV_DBTR_MC_RWX_MASK						\
+		(RV_DBTR_BIT_MASK(MC, LOAD) |				\
+		 RV_DBTR_BIT_MASK(MC, STORE) |				\
+		 RV_DBTR_BIT_MASK(MC, EXEC))
+
 /* must call with hs != NULL */
 static inline bool sbi_dbtr_shmem_disabled(
 	struct sbi_dbtr_hart_triggers_state *hs)
@@ -90,6 +99,19 @@ static bool sbi_hw_trigger_supports(struct sbi_hw_trigger *hw_trig,
 {
 	if (!(BIT(type) & hw_trig->type_mask))
 		return false;
+
+	switch (type) {
+	case RISCV_DBTR_TRIG_MCONTROL:
+	case RISCV_DBTR_TRIG_MCONTROL6:
+		if ((hw_trig->mcontrol_rwx_mask & tdata1) != (tdata1 & RV_DBTR_MC_RWX_MASK))
+			return false;
+		break;
+	case RISCV_DBTR_TRIG_ICOUNT:
+		break;
+	default:
+		break;
+	}
+
 	return true;
 }
 
@@ -180,6 +202,7 @@ static bool sbi_hw_trigger_probe(int i)
 {
 	struct sbi_hw_trigger *hw_trig = INDEX_TO_HW_TRIGGER(i);
 	struct sbi_trap_info trap = {0};
+	unsigned long type;
 	unsigned long tdata1;
 	unsigned long val;
 
@@ -204,8 +227,6 @@ static bool sbi_hw_trigger_probe(int i)
 
 	val = csr_read_allowed(CSR_TINFO, &trap);
 	if (trap.cause) {
-		unsigned long type;
-
 		/*
 		 * If reading tinfo caused an exception, the
 		 * debugger must read tdata1 to discover the
@@ -227,6 +248,51 @@ static bool sbi_hw_trigger_probe(int i)
 		hw_trig->type_mask = val;
 	}
 
+	/* Probe supported features for each supported type */
+	for (type = 0; type < 32; type++) {
+		if (!(hw_trig->type_mask & (1UL << type)))
+			continue;
+		csr_write(CSR_TDATA1, 0x0);
+		csr_write(CSR_TDATA2, 0x0);
+
+		switch (type) {
+		case RISCV_DBTR_TRIG_MCONTROL:
+		case RISCV_DBTR_TRIG_MCONTROL6:
+			/*
+			 * Set an MC/MC6 type trigger with load/store/exec bits
+			 * set, then read which remain set. The implementation
+			 * should read back zeroes for unsupported bits.
+			 */
+			tdata1 = RV_DBTR_MC_RWX_MASK;
+			RV_DBTR_SET_MC_TYPE(tdata1, type);
+			csr_write(CSR_TDATA1, tdata1);
+			tdata1 = csr_read(CSR_TDATA1);
+			if (TDATA1_GET_TYPE(tdata1) != type) {
+				sbi_printf("DBTR error: Hardware trigger type could not be set\n");
+				/* Disable the trigger type */
+				hw_trig->type_mask &= ~(1UL << type);
+			}
+			hw_trig->mcontrol_rwx_mask = tdata1 & RV_DBTR_MC_RWX_MASK;
+			if (hw_trig->mcontrol_rwx_mask == 0) {
+				sbi_printf("DBTR error: Hardware trigger type has no access bits\n");
+				/* Disable the trigger type */
+				hw_trig->type_mask &= ~(1UL << type);
+			}
+
+			csr_write(CSR_TDATA1, 0x0); /* Reset tdata1 */
+			break;
+		case RISCV_DBTR_TRIG_ICOUNT:
+			break;
+		default:
+			break;
+		}
+	}
+
+	if (hw_trig->type_mask == 0) {
+		/* May get here if the above "impossible" conditions are hit */
+		return false;
+	}
+
 	return true;
 }
 
@@ -917,6 +983,13 @@ int sbi_dbtr_update_trig(unsigned long smode,
 			err = SBI_ERR_INVALID_PARAM;
 			goto out_unmap;
 		}
+
+		hw_trig = INDEX_TO_HW_TRIGGER(GET_TRIG_HW_INDEX(trig->state));
+		if (!sbi_hw_trigger_supports(hw_trig, type, tdata1)) {
+			*out = _idx;
+			err = SBI_ERR_NOT_SUPPORTED;
+			goto out_unmap;
+		}
 	}
 
 	/* Update triggers */
-- 
2.51.0


-- 
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

  parent reply	other threads:[~2026-03-13  5:21 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-13  5:19 [PATCH 00/18] dbtr: Fixes and heterogeneous trigger types Nicholas Piggin
2026-03-13  5:19 ` [PATCH 01/18] dbtr: Add consistent range checks to trigger ecalls Nicholas Piggin
2026-03-20 12:46   ` Ilya Mamay
2026-04-07  5:22   ` Himanshu Chauhan
2026-03-13  5:19 ` [PATCH 02/18] dbtr: Trigger update should set sbiret.value on failure Nicholas Piggin
2026-03-13  5:19 ` [PATCH 03/18] dbtr: Fix endian conversion in trigger install handler Nicholas Piggin
2026-03-13  5:19 ` [PATCH 04/18] dbtr: Return correct error on install not supported Nicholas Piggin
2026-03-13  5:19 ` [PATCH 05/18] dbtr: Do not support chain bit Nicholas Piggin
2026-03-13  5:19 ` [PATCH 06/18] dbtr: Improve trigger update error checking Nicholas Piggin
2026-03-13  5:19 ` [PATCH 07/18] dbtr: Check for invalid and unsupported triggers in update Nicholas Piggin
2026-03-13  5:19 ` [PATCH 08/18] dbtr: Improve error handling for trigger enable, disable, uninstall Nicholas Piggin
2026-03-13  5:19 ` [PATCH 09/18] dbtr: Read triggers should not read HW trigger if not mapped Nicholas Piggin
2026-03-13  5:19 ` [PATCH 10/18] dbtr: Avoid crash in sbi_debug_read_triggers Nicholas Piggin
2026-03-13  5:19 ` [PATCH 11/18] dbtr: Succeed operations with no triggers in mask Nicholas Piggin
2026-03-13  5:19 ` [PATCH 12/18] dbtr: Move hardware trigger probing to a function Nicholas Piggin
2026-03-13  5:19 ` [PATCH 13/18] dbtr: Rework install and update error handling Nicholas Piggin
2026-03-13  5:19 ` [PATCH 14/18] dbtr: Decouple dbtr trigger index from hardware trigger number Nicholas Piggin
2026-03-13  5:19 ` [PATCH 15/18] dbtr: Move trigger feature support test into a function Nicholas Piggin
2026-03-13  5:19 ` [PATCH 16/18] dbtr: Heterogeneous trigger type support Nicholas Piggin
2026-03-13  5:19 ` Nicholas Piggin [this message]
2026-03-13  5:19 ` [PATCH 18/18] dbtr: Work around specification bug in range checks Nicholas Piggin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260313051948.4017134-18-npiggin@gmail.com \
    --to=npiggin@gmail.com \
    --cc=hchauhan@ventanamicro.com \
    --cc=opensbi@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox