mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Zhang <yidong.zhang@amd.com>
To: <quic_jhugo@quicinc.com>, <karol.wachowski@linux.intel.com>,
	<max.zhen@amd.com>, <lizhi.hou@amd.com>, <ogabbay@kernel.org>,
	<dri-devel@lists.freedesktop.org>, <linux-kernel@vger.kernel.org>
Cc: David Zhang <yidong.zhang@amd.com>, <sonal.santan@amd.com>,
	<mario.limonciello@amd.com>
Subject: [PATCH V2 5/5] accel/amdxdna: Recover hung AIE4 contexts
Date: Mon, 30 Nov 2026 14:59:04 -0800	[thread overview]
Message-ID: <20261130225904.1103363-6-yidong.zhang@amd.com> (raw)
In-Reply-To: <20261130225904.1103363-1-yidong.zhang@amd.com>

The driver checks for a context job hung in firmware, so that the job
does not hang inside firmware. Detect the hang the same way as the
passive TDR detection in AIE2.

Signed-off-by: David Zhang <yidong.zhang@amd.com>
---
 drivers/accel/amdxdna/aie4_ctx.c   | 20 ++++++++---
 drivers/accel/amdxdna/aie4_error.c | 53 ++++++++++++++++++++++++++++--
 drivers/accel/amdxdna/aie4_pci.c   |  2 ++
 drivers/accel/amdxdna/aie4_pci.h   |  6 ++++
 4 files changed, 74 insertions(+), 7 deletions(-)

diff --git a/drivers/accel/amdxdna/aie4_ctx.c b/drivers/accel/amdxdna/aie4_ctx.c
index 9f61fcd6e518..25c801a89488 100644
--- a/drivers/accel/amdxdna/aie4_ctx.c
+++ b/drivers/accel/amdxdna/aie4_ctx.c
@@ -27,7 +27,7 @@
 
 #define CTX_INVALID_ID			(~0U)
 #define CTX_INVALID_DOORBELL		AMDXDNA_INVALID_DOORBELL_OFFSET
-#define AIE4_JOB_TIMEOUT_MS		2000
+#define AIE4_JOB_TIMEOUT_MS		10000
 
 static void job_worker(struct work_struct *work);
 
@@ -960,6 +960,7 @@ static void job_done(struct amdxdna_sched_job *job)
 
 static void job_complete(struct amdxdna_sched_job *job)
 {
+	AIE4_UPDATE_LAST_ACTIVE_TS(job->hwctx->client->xdna->dev_handle);
 	job_done(job);
 }
 
@@ -1090,8 +1091,16 @@ static bool aie4_hwctx_take_fault(struct amdxdna_hwctx *hwctx,
 	return fault;
 }
 
+bool aie4_hwctx_job_hung(struct amdxdna_hwctx *hwctx, u64 seq)
+{
+	unsigned long last = READ_ONCE(hwctx->client->xdna->dev_handle->last_signal_ts);
+
+	return AIE4_HWCTX_CONNECTED(hwctx) && get_read_index(hwctx) <= seq &&
+	       time_after_eq(jiffies, last + msecs_to_jiffies(AIE4_JOB_TIMEOUT_MS));
+}
+
 /*
- * Stop a context whose running job timed out, so the NPU no longer
+ * Reset a context whose running job timed out, so the NPU no longer
  * accesses the job BOs before the worker aborts the jobs and signals
  * their fences. Paths holding dev_lock flush this worker, so do not
  * block on dev_lock; if it is busy, wait for another timeout period.
@@ -1104,9 +1113,9 @@ static void job_timeout(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *j
 		return;
 
 	/* The job may have completed or the context disconnected meanwhile. */
-	if (AIE4_HWCTX_CONNECTED(hwctx) && get_read_index(hwctx) <= job->seq) {
+	if (aie4_hwctx_job_hung(hwctx, job->seq)) {
 		XDNA_ERR(xdna, "hwctx %s job %llu timed out", hwctx->name, job->seq);
-		aie4_hwctx_stop(hwctx);
+		aie4_ctx_hung_queue(hwctx, job->seq);
 	}
 	mutex_unlock(&xdna->dev_lock);
 }
@@ -1214,6 +1223,9 @@ void aie4_hwctx_reset(struct amdxdna_hwctx *hwctx,
 		XDNA_ERR(xdna, "Failed to create hwctx %s after reset, ret %d",
 			 hwctx->name, ret);
 
+	/* Give every context a full timeout after the reset. */
+	AIE4_UPDATE_LAST_ACTIVE_TS(xdna->dev_handle);
+
 	mutex_lock(&priv->io_lock);
 	priv->reset_fault = false;
 	WRITE_ONCE(priv->resetting, false);
diff --git a/drivers/accel/amdxdna/aie4_error.c b/drivers/accel/amdxdna/aie4_error.c
index 9ca99af3e2c1..56132fc14d46 100644
--- a/drivers/accel/amdxdna/aie4_error.c
+++ b/drivers/accel/amdxdna/aie4_error.c
@@ -148,7 +148,8 @@ static void aie4_record_async_error(struct amdxdna_dev_hdl *ndev,
 
 /* Reset the context with firmware ID ctx_id that existed at generation gen. */
 static void aie4_reset_hwctx_by_id(struct amdxdna_dev_hdl *ndev, u32 ctx_id,
-				   u64 gen, const struct aie4_async_ctx_error *ctx_err)
+				   u64 gen, bool timeout, u64 seq,
+				   const struct aie4_async_ctx_error *ctx_err)
 {
 	struct amdxdna_dev *xdna = ndev->aie.xdna;
 	bool found = false;
@@ -166,6 +167,11 @@ static void aie4_reset_hwctx_by_id(struct amdxdna_dev_hdl *ndev, u32 ctx_id,
 			    hwctx->priv->gen > gen)
 				continue;
 			found = true;
+			if (timeout && !aie4_hwctx_job_hung(hwctx, seq))
+				break;
+			if (timeout)
+				aie4_record_async_error(ndev, AMDXDNA_ERROR_NUM_KDS_EXEC,
+							0, ctx_id);
 			aie4_hwctx_reset(hwctx, ctx_err);
 			break;
 		}
@@ -230,7 +236,7 @@ static void aie4_async_ctx_error(struct aie4_async_event *e)
 	aie4_record_async_error(ndev, aie4_ctx_error_num(ctx_err->error_type),
 				ctx_status, ctx_id);
 
-	aie4_reset_hwctx_by_id(ndev, ctx_id, e->resp_ctx_gen, ctx_err);
+	aie4_reset_hwctx_by_id(ndev, ctx_id, e->resp_ctx_gen, false, 0, ctx_err);
 }
 
 static void aie4_async_event_report(struct aie4_async_event *e)
@@ -439,7 +445,7 @@ static void aie4_async_note_worker(struct work_struct *work)
 
 	/* There is no report, so record it like a job timeout. */
 	aie4_record_async_error(ndev, AMDXDNA_ERROR_NUM_KDS_EXEC, 0, note->ctx_id);
-	aie4_reset_hwctx_by_id(ndev, note->ctx_id, note->ctx_gen, NULL);
+	aie4_reset_hwctx_by_id(ndev, note->ctx_id, note->ctx_gen, false, 0, NULL);
 
 unlock:
 	mutex_unlock(&xdna->dev_lock);
@@ -511,3 +517,44 @@ int aie4_async_note_cb(void *handle, u32 opcode, void __iomem *data, size_t size
 	queue_work(events->wq, &note->work);
 	return 0;
 }
+
+struct aie4_ctx_hung_work {
+	struct work_struct	work;
+	struct amdxdna_dev_hdl	*ndev;
+	u32			ctx_id;
+	u64			ctx_gen;
+	u64			seq;
+};
+
+static void aie4_ctx_hung_worker(struct work_struct *work)
+{
+	struct aie4_ctx_hung_work *tw = container_of(work, struct aie4_ctx_hung_work, work);
+	struct amdxdna_dev *xdna = tw->ndev->aie.xdna;
+
+	mutex_lock(&xdna->dev_lock);
+	aie4_reset_hwctx_by_id(tw->ndev, tw->ctx_id, tw->ctx_gen, true, tw->seq, NULL);
+	mutex_unlock(&xdna->dev_lock);
+
+	kfree(tw);
+}
+
+void aie4_ctx_hung_queue(struct amdxdna_hwctx *hwctx, u64 seq)
+{
+	struct amdxdna_dev_hdl *ndev = hwctx->client->xdna->dev_handle;
+	struct aie4_ctx_hung_work *tw;
+
+	if (!ndev->async_events)
+		return;
+
+	/* Runs on the fence-signalling path under dev_lock; do not reclaim. */
+	tw = kzalloc_obj(*tw, GFP_NOWAIT);
+	if (!tw)
+		return;
+
+	tw->ndev = ndev;
+	tw->ctx_id = READ_ONCE(hwctx->priv->hw_ctx_id);
+	tw->ctx_gen = READ_ONCE(hwctx->priv->gen);
+	tw->seq = seq;
+	INIT_WORK(&tw->work, aie4_ctx_hung_worker);
+	queue_work(ndev->async_events->wq, &tw->work);
+}
diff --git a/drivers/accel/amdxdna/aie4_pci.c b/drivers/accel/amdxdna/aie4_pci.c
index 78a739573ad3..ebb87afa6afb 100644
--- a/drivers/accel/amdxdna/aie4_pci.c
+++ b/drivers/accel/amdxdna/aie4_pci.c
@@ -499,6 +499,7 @@ static int aie4_vf_hw_start(struct amdxdna_dev_hdl *ndev)
 	if (ret)
 		goto partition_fini;
 
+	AIE4_UPDATE_LAST_ACTIVE_TS(ndev);
 	return 0;
 
 partition_fini:
@@ -551,6 +552,7 @@ static int aie4_classic_hw_start(struct amdxdna_dev_hdl *ndev)
 	if (ret)
 		goto partition_fini;
 
+	AIE4_UPDATE_LAST_ACTIVE_TS(ndev);
 	return 0;
 
 partition_fini:
diff --git a/drivers/accel/amdxdna/aie4_pci.h b/drivers/accel/amdxdna/aie4_pci.h
index 6da2810c3fb4..6d572c6f9ad2 100644
--- a/drivers/accel/amdxdna/aie4_pci.h
+++ b/drivers/accel/amdxdna/aie4_pci.h
@@ -87,6 +87,8 @@ struct amdxdna_hwctx_priv {
 #define AIE4_HWCTX_CONNECTED(hwctx)	(!!READ_ONCE((hwctx)->priv->cert_comp))
 /* Set while a reset runs, and kept if the reset left the context dead. */
 #define AIE4_HWCTX_HAS_RESET(hwctx)	READ_ONCE((hwctx)->priv->has_reset)
+/* Update the time stamp of the last device activity. */
+#define AIE4_UPDATE_LAST_ACTIVE_TS(ndev)	WRITE_ONCE((ndev)->last_signal_ts, jiffies)
 
 struct amdxdna_dev_priv {
 	const char              *npufw_path;
@@ -143,6 +145,8 @@ struct amdxdna_dev_hdl {
 	/* Context switch hysteresis timeout in microseconds. */
 	u32				ctx_switch_hysteresis_us;
 
+	/* jiffies of the last device activity: job completion, start or reset */
+	unsigned long			last_signal_ts;
 	/* Bumped on each firmware context creation, under dev_lock. */
 	u64				ctx_gen;
 	/* Last context error reported by firmware, under dev_lock. */
@@ -161,6 +165,7 @@ int aie4_async_events_alloc(struct amdxdna_dev_hdl *ndev, u32 num_events);
 void aie4_async_events_free(struct amdxdna_dev_hdl *ndev);
 int aie4_async_events_register(struct amdxdna_dev_hdl *ndev, u32 num_events);
 void aie4_async_events_stop(struct amdxdna_dev_hdl *ndev);
+void aie4_ctx_hung_queue(struct amdxdna_hwctx *hwctx, u64 seq);
 int aie4_async_note_cb(void *handle, u32 opcode, void __iomem *data, size_t size);
 
 /* aie4_message.c */
@@ -214,6 +219,7 @@ int aie4_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value,
 int aie4_hwctx_sync_debug_bo(struct amdxdna_hwctx *hwctx, u32 debug_bo_hdl);
 void aie4_hwctx_reset(struct amdxdna_hwctx *hwctx,
 		      const struct aie4_async_ctx_error *ctx_err);
+bool aie4_hwctx_job_hung(struct amdxdna_hwctx *hwctx, u64 seq);
 
 /* aie4_pci.c */
 void aie4_hwctx_suspend_all(struct amdxdna_dev_hdl *ndev, int clean_jobs);
-- 
2.34.1


      parent reply	other threads:[~2026-11-30 22:59 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 22:58 [PATCH V2 0/5] accel/amdxdna: Add AIE4 async events and TDR David Zhang
2026-11-30 22:59 ` [PATCH V2 1/5] accel/amdxdna: Add AIE4 async event buffers David Zhang
2026-11-30 22:59 ` [PATCH V2 2/5] accel/amdxdna: Add AIE4 context recovery David Zhang
2026-11-30 22:59 ` [PATCH V2 3/5] accel/amdxdna: Check mailbox message overflow David Zhang
2026-11-30 22:59 ` [PATCH V2 4/5] accel/amdxdna: Handle AIE4 firmware notifications David Zhang
2026-11-30 22:59 ` David Zhang [this message]

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=20261130225904.1103363-6-yidong.zhang@amd.com \
    --to=yidong.zhang@amd.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=karol.wachowski@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lizhi.hou@amd.com \
    --cc=mario.limonciello@amd.com \
    --cc=max.zhen@amd.com \
    --cc=ogabbay@kernel.org \
    --cc=quic_jhugo@quicinc.com \
    --cc=sonal.santan@amd.com \
    /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

all inboxes | Powered by JetHome®