* [PATCH V2 0/5] accel/amdxdna: Add AIE4 async events and TDR
@ 2026-11-30 22:58 David Zhang
2026-11-30 22:59 ` [PATCH V2 1/5] accel/amdxdna: Add AIE4 async event buffers David Zhang
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: David Zhang @ 2026-11-30 22:58 UTC (permalink / raw)
To: quic_jhugo, karol.wachowski, max.zhen, lizhi.hou, ogabbay,
dri-devel, linux-kernel
Cc: David Zhang, sonal.santan, mario.limonciello
This series adds AIE4 async event handling and timeout detection and
recovery (TDR) for AIE4 hardware contexts.
- Register AIE4 async event buffers with firmware.
- Check mailbox message overflow.
- Reset an AIE4 context on a context error reported by firmware, and
keep the last one for the last async error query.
- Handle AIE4 firmware notifications, which report the context errors
that have no async event buffer, including all errors on a VF.
- Recover hung AIE4 contexts, detected the way AIE2 detects TDR;
raise the job timeout to 10s.
Changes in V2:
- Add patch 3 to check mailbox message overflow.
- Rebase on drm-misc-next.
This series applies on top of drm-misc-next.
David Zhang (4):
accel/amdxdna: Add AIE4 async event buffers
accel/amdxdna: Add AIE4 context recovery
accel/amdxdna: Check mailbox message overflow
accel/amdxdna: Recover hung AIE4 contexts
Nishad Saraf (1):
accel/amdxdna: Handle AIE4 firmware notifications
drivers/accel/amdxdna/Makefile | 1 +
drivers/accel/amdxdna/aie4_ctx.c | 310 ++++++++++---
drivers/accel/amdxdna/aie4_error.c | 560 ++++++++++++++++++++++++
drivers/accel/amdxdna/aie4_message.c | 59 ++-
drivers/accel/amdxdna/aie4_msg_priv.h | 111 +++++
drivers/accel/amdxdna/aie4_pci.c | 162 ++++---
drivers/accel/amdxdna/aie4_pci.h | 69 ++-
drivers/accel/amdxdna/amdxdna_ctx.h | 1 +
drivers/accel/amdxdna/amdxdna_error.h | 10 +
drivers/accel/amdxdna/amdxdna_mailbox.c | 22 +-
drivers/accel/amdxdna/amdxdna_mailbox.h | 17 +
11 files changed, 1183 insertions(+), 139 deletions(-)
create mode 100644 drivers/accel/amdxdna/aie4_error.c
base-commit: 89e6c97edc99949b51aad91072caf61b07bfde66
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH V2 1/5] accel/amdxdna: Add AIE4 async event buffers
2026-11-30 22:58 [PATCH V2 0/5] accel/amdxdna: Add AIE4 async events and TDR David Zhang
@ 2026-11-30 22:59 ` David Zhang
2026-11-30 22:59 ` [PATCH V2 2/5] accel/amdxdna: Add AIE4 context recovery David Zhang
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: David Zhang @ 2026-11-30 22:59 UTC (permalink / raw)
To: quic_jhugo, karol.wachowski, max.zhen, lizhi.hou, ogabbay,
dri-devel, linux-kernel
Cc: David Zhang, sonal.santan, mario.limonciello
AIE4 firmware writes asynchronous event reports into buffers that the
driver registers with it. Allocate these buffers at probe and register
them at each hardware start.
Each buffer shared with firmware has a CPU address, a DMA address and
a size.
Signed-off-by: David Zhang <yidong.zhang@amd.com>
---
drivers/accel/amdxdna/Makefile | 1 +
drivers/accel/amdxdna/aie4_error.c | 333 ++++++++++++++++++++++++++
drivers/accel/amdxdna/aie4_message.c | 59 ++++-
drivers/accel/amdxdna/aie4_msg_priv.h | 80 +++++++
drivers/accel/amdxdna/aie4_pci.c | 135 ++++++-----
drivers/accel/amdxdna/aie4_pci.h | 35 ++-
6 files changed, 564 insertions(+), 79 deletions(-)
create mode 100644 drivers/accel/amdxdna/aie4_error.c
diff --git a/drivers/accel/amdxdna/Makefile b/drivers/accel/amdxdna/Makefile
index 05cce0a38692..3b43cbcf9dfb 100644
--- a/drivers/accel/amdxdna/Makefile
+++ b/drivers/accel/amdxdna/Makefile
@@ -11,6 +11,7 @@ amdxdna-y := \
aie2_pm.o \
aie2_solver.o \
aie4_ctx.o \
+ aie4_error.o \
aie4_message.o \
aie4_pci.o \
amdxdna_cbuf.o \
diff --git a/drivers/accel/amdxdna/aie4_error.c b/drivers/accel/amdxdna/aie4_error.c
new file mode 100644
index 000000000000..c1f4819d0107
--- /dev/null
+++ b/drivers/accel/amdxdna/aie4_error.c
@@ -0,0 +1,333 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2026, Advanced Micro Devices, Inc.
+ */
+
+#include <drm/drm_cache.h>
+#include <drm/drm_print.h>
+#include <linux/bitfield.h>
+#include <linux/io.h>
+#include <linux/minmax.h>
+#include <linux/slab.h>
+#include <linux/workqueue.h>
+
+#include "aie.h"
+#include "aie4_msg_priv.h"
+#include "aie4_pci.h"
+#include "amdxdna_mailbox.h"
+#include "amdxdna_pci_drv.h"
+
+/*
+ * struct aie4_async_event - one async event report slice
+ * @ndev: device handle
+ * @events: owning pool
+ * @work: worker that reports the event and registers the slice again
+ * @buf: CPU address of the slice
+ * @addr: DMA address of the slice
+ * @resp: last response; status is MAX_AIE4_MSG_STATUS_CODE when none
+ * @resp_gen: @gen when the last response arrived
+ */
+struct aie4_async_event {
+ struct amdxdna_dev_hdl *ndev;
+ struct aie4_async_events *events;
+ struct work_struct work;
+ void *buf;
+ dma_addr_t addr;
+ struct aie4_msg_async_event_config_resp resp;
+ u32 resp_gen;
+};
+
+/*
+ * struct aie4_async_events - async event work and report buffers
+ * @wq: ordered workqueue for async event work
+ * @buf: one buffer holding AIE4_ASYNC_BUF_SIZE per event; empty without events
+ * @gen: bumped when the mailbox channel stops
+ * @event_cnt: number of slices, 0 when no buffer is allocated
+ * @event: per slice state
+ */
+struct aie4_async_events {
+ struct workqueue_struct *wq;
+ struct aie4_msg_buf buf;
+ u32 gen;
+ u32 event_cnt;
+ struct aie4_async_event event[];
+};
+
+static int aie4_async_event_cb(void *handle, void __iomem *data, size_t size)
+{
+ struct aie4_async_event *e = handle;
+
+ /* The channel calls back with data == NULL when it stops; skip it. */
+ if (!data)
+ return 0;
+
+ /* Tag the response with the channel it arrived on. */
+ e->resp_gen = READ_ONCE(e->events->gen);
+ e->resp.type = readl(data +
+ offsetof(struct aie4_msg_async_event_config_resp, type));
+ wmb(); /* Update status last; the worker reads it first */
+ WRITE_ONCE(e->resp.status, readl(data +
+ offsetof(struct aie4_msg_async_event_config_resp, status)));
+ queue_work(e->events->wq, &e->work);
+ return 0;
+}
+
+static int aie4_async_event_send(struct aie4_async_event *e)
+{
+ /* Firmware may write a short report; do not leave an older one behind. */
+ memset(e->buf, 0, AIE4_ASYNC_BUF_SIZE);
+ drm_clflush_virt_range(e->buf, AIE4_ASYNC_BUF_SIZE); /* device can access */
+ return aie4_register_async_event_msg(e->ndev, e->addr, AIE4_ASYNC_BUF_SIZE,
+ e, aie4_async_event_cb);
+}
+
+static const char *aie4_ctx_error_str(u32 error_type)
+{
+ switch (error_type) {
+ case AIE4_ASYNC_EVENT_CTX_ERR_HWSCH_FAILURE:
+ return "HWSCH_FAILURE";
+ case AIE4_ASYNC_EVENT_CTX_ERR_STOP_FAILURE:
+ return "STOP_FAILURE";
+ case AIE4_ASYNC_EVENT_CTX_ERR_AIE_FAILURE:
+ return "AIE_FAILURE";
+ case AIE4_ASYNC_EVENT_CTX_ERR_PREEMPTION_TIMEOUT:
+ return "PREEMPTION_TIMEOUT";
+ case AIE4_ASYNC_EVENT_CTX_ERR_NEW_PROCESS_FAILURE:
+ return "NEW_PROCESS_FAILURE";
+ case AIE4_ASYNC_EVENT_CTX_ERR_UC_CRITICAL_ERROR:
+ return "UC_CRITICAL_ERROR";
+ case AIE4_ASYNC_EVENT_CTX_ERR_UC_COMPLETION_TIMEOUT:
+ return "UC_COMPLETION_TIMEOUT";
+ default:
+ return "UNKNOWN";
+ }
+}
+
+/* TDR: the context timed out rather than failed. */
+static bool aie4_tdr_detected(u32 error_type)
+{
+ return error_type == AIE4_ASYNC_EVENT_CTX_ERR_UC_COMPLETION_TIMEOUT ||
+ error_type == AIE4_ASYNC_EVENT_CTX_ERR_PREEMPTION_TIMEOUT;
+}
+
+static void aie4_log_health_report(struct amdxdna_dev *xdna,
+ struct aie4_msg_app_health_report *health)
+{
+ u32 num_uc, i;
+
+ if (!health->version) {
+ XDNA_ERR(xdna, "No app health report from firmware");
+ return;
+ }
+
+ num_uc = FIELD_GET(AIE4_APP_HEALTH_NUM_UC, health->ctx_num_uc);
+ XDNA_ERR(xdna, "App health report version %u.%u:",
+ (u32)FIELD_GET(AIE4_APP_HEALTH_MAJOR_VER, health->version),
+ (u32)FIELD_GET(AIE4_APP_HEALTH_MINOR_VER, health->version));
+ XDNA_ERR(xdna, "\tContext status: %u",
+ (u32)FIELD_GET(AIE4_APP_HEALTH_CTX_STATUS, health->ctx_num_uc));
+ XDNA_ERR(xdna, "\tActive uC count: %u", num_uc);
+ XDNA_ERR(xdna, "\tCurrent run list index: %u", health->runlist_read_idx);
+
+ for (i = 0; i < min(num_uc, AIE4_MPNPUFW_MAX_UC_COUNT); i++) {
+ struct aie4_uc_health_info *uc = &health->uc_info[i];
+
+ XDNA_ERR(xdna, "\tuC %u: fw_state %u idle 0x%x misc 0x%x page %u offset 0x%x",
+ uc->uc_idx, uc->fw_state, uc->uc_idle_status,
+ uc->misc_status, uc->page_idx, uc->offset);
+ XDNA_ERR(xdna, "\t\tPC 0x%x EAR 0x%x ESR 0x%x",
+ uc->uc_pc, uc->uc_ear, uc->uc_esr);
+ }
+}
+
+static void aie4_async_ctx_error(struct aie4_async_event *e)
+{
+ struct aie4_async_ctx_error *ctx_err = e->buf;
+ struct aie4_msg_app_health_report *health = &ctx_err->app_health_report;
+ struct amdxdna_dev_hdl *ndev = e->ndev;
+ struct amdxdna_dev *xdna = ndev->aie.xdna;
+ u32 ctx_id = ctx_err->ctx_id;
+
+ if (aie4_tdr_detected(ctx_err->error_type))
+ XDNA_ERR(xdna, "TDR on context %u: %s", ctx_id,
+ aie4_ctx_error_str(ctx_err->error_type));
+ else
+ XDNA_ERR(xdna, "Firmware reported context %u event: %s", ctx_id,
+ aie4_ctx_error_str(ctx_err->error_type));
+ aie4_log_health_report(xdna, health);
+}
+
+static void aie4_async_event_report(struct aie4_async_event *e)
+{
+ struct amdxdna_dev *xdna = e->ndev->aie.xdna;
+
+ switch (e->resp.type) {
+ case AIE4_ASYNC_EVENT_TYPE_AIE_ERROR:
+ XDNA_ERR(xdna, "Firmware reported an AIE event");
+ break;
+ case AIE4_ASYNC_EVENT_TYPE_CTX_ERROR:
+ aie4_async_ctx_error(e);
+ break;
+ default:
+ XDNA_WARN(xdna, "Unknown async event type %u", e->resp.type);
+ break;
+ }
+}
+
+static void aie4_async_event_worker(struct work_struct *work)
+{
+ struct aie4_async_event *e = container_of(work, struct aie4_async_event, work);
+ struct amdxdna_dev *xdna = e->ndev->aie.xdna;
+ u32 status;
+
+ /*
+ * Stop and register also hold dev_lock, so the slice is not reused
+ * while read.
+ */
+ guard(mutex)(&xdna->dev_lock);
+
+ status = READ_ONCE(e->resp.status);
+ /* Nothing to report, or the slice was registered again. */
+ if (status == MAX_AIE4_MSG_STATUS_CODE)
+ return;
+ smp_rmb(); /* Read type after status; pairs with wmb() in the callback */
+ e->resp.status = MAX_AIE4_MSG_STATUS_CODE;
+
+ /* Skip an event left over from a previous channel. */
+ if (e->resp_gen != e->events->gen)
+ return;
+
+ /* Firmware rejected the buffer, so stop sending it. */
+ if (status != AIE4_MSG_STATUS_SUCCESS) {
+ if (status == AIE4_MSG_STATUS_ASYNC_EVENT_MSGS_FULL)
+ XDNA_DBG(xdna, "Async event buffer not taken, firmware is full");
+ else
+ XDNA_WARN(xdna, "Firmware did not take async event buffer, status %u",
+ status);
+ return;
+ }
+
+ /* Read what firmware wrote, not cached data. */
+ drm_clflush_virt_range(e->buf, AIE4_ASYNC_BUF_SIZE);
+ print_hex_dump_debug("AIE4 async event: ", DUMP_PREFIX_OFFSET, 16, 4,
+ e->buf, 0x100, false);
+ aie4_async_event_report(e);
+
+ if (aie4_async_event_send(e))
+ XDNA_WARN(xdna, "Failed to register async event buffer again");
+}
+
+/* Allocate at probe; register at each hardware start. */
+int aie4_async_events_alloc(struct amdxdna_dev_hdl *ndev, u32 num_events)
+{
+ struct amdxdna_dev *xdna = ndev->aie.xdna;
+ struct aie4_async_events *events;
+ u32 i;
+ int ret;
+
+ events = kzalloc_flex(*events, event, num_events);
+ if (!events)
+ return -ENOMEM;
+ events->event_cnt = num_events;
+
+ events->wq = alloc_ordered_workqueue("aie4_async_wq", 0);
+ if (!events->wq) {
+ ret = -ENOMEM;
+ goto free_events;
+ }
+
+ /* A VF uses only the workqueue and registers no buffers. */
+ if (!num_events)
+ goto done;
+
+ ret = aie4_alloc_msg_buffer(xdna, &events->buf,
+ AIE4_ASYNC_BUF_SIZE * num_events);
+ if (ret) {
+ XDNA_ERR(xdna, "Failed to alloc async event buffers, ret %d", ret);
+ goto destroy_wq;
+ }
+
+ for (i = 0; i < num_events; i++) {
+ struct aie4_async_event *e = &events->event[i];
+ u32 offset = i * AIE4_ASYNC_BUF_SIZE;
+
+ e->ndev = ndev;
+ e->events = events;
+ e->buf = events->buf.vaddr + offset;
+ e->addr = events->buf.dma_addr + offset;
+ e->resp.status = MAX_AIE4_MSG_STATUS_CODE;
+ INIT_WORK(&e->work, aie4_async_event_worker);
+ }
+
+done:
+ ndev->async_events = events;
+ return 0;
+
+destroy_wq:
+ destroy_workqueue(events->wq);
+free_events:
+ kfree(events);
+ return ret;
+}
+
+/* Called after hw stop, when firmware has released the buffers. */
+void aie4_async_events_free(struct amdxdna_dev_hdl *ndev)
+{
+ struct amdxdna_dev *xdna = ndev->aie.xdna;
+ struct aie4_async_events *events = ndev->async_events;
+
+ drm_WARN_ON(&xdna->ddev, !mutex_is_locked(&xdna->dev_lock));
+
+ if (!events)
+ return;
+
+ ndev->async_events = NULL;
+
+ /* Workers take dev_lock; let them finish without it. */
+ mutex_unlock(&xdna->dev_lock);
+ destroy_workqueue(events->wq);
+ mutex_lock(&xdna->dev_lock);
+
+ aie4_free_msg_buffer(xdna, &events->buf);
+ kfree(events);
+}
+
+/*
+ * Register the slices on the current channel. If this does not succeed,
+ * the caller stops the channel.
+ */
+int aie4_async_events_register(struct amdxdna_dev_hdl *ndev, u32 num_events)
+{
+ struct aie4_async_events *events = ndev->async_events;
+ struct amdxdna_dev *xdna = ndev->aie.xdna;
+ u32 i, cnt;
+ int ret;
+
+ drm_WARN_ON(&xdna->ddev, !mutex_is_locked(&xdna->dev_lock));
+
+ cnt = min(num_events, events->event_cnt);
+ for (i = 0; i < cnt; i++) {
+ struct aie4_async_event *e = &events->event[i];
+
+ /* Clear any event the previous channel left. */
+ e->resp.status = MAX_AIE4_MSG_STATUS_CODE;
+ ret = aie4_async_event_send(e);
+ if (ret) {
+ XDNA_ERR(xdna, "Failed to register async event buffer %u, ret %d",
+ i, ret);
+ return ret;
+ }
+ }
+
+ XDNA_DBG(xdna, "Registered %u async event buffers", cnt);
+ return 0;
+}
+
+/* Bump @gen so workers skip events from the destroyed channel. */
+void aie4_async_events_stop(struct amdxdna_dev_hdl *ndev)
+{
+ struct aie4_async_events *events = ndev->async_events;
+
+ if (events)
+ WRITE_ONCE(events->gen, events->gen + 1);
+}
diff --git a/drivers/accel/amdxdna/aie4_message.c b/drivers/accel/amdxdna/aie4_message.c
index bc862cbe68f3..cb3135789398 100644
--- a/drivers/accel/amdxdna/aie4_message.c
+++ b/drivers/accel/amdxdna/aie4_message.c
@@ -25,6 +25,31 @@ u32 aie4_msg_pasid(struct amdxdna_client *client)
FIELD_PREP(AIE4_MSG_PASID_VLD, 1);
}
+int aie4_alloc_msg_buffer(struct amdxdna_dev *xdna, struct aie4_msg_buf *buf, u32 size)
+{
+ void *vaddr;
+
+ vaddr = amdxdna_alloc_msg_buffer(xdna, &size, &buf->dma_addr);
+ if (IS_ERR(vaddr)) {
+ /* Leave the buffer empty, so aie4_free_msg_buffer() skips it. */
+ buf->vaddr = NULL;
+ return PTR_ERR(vaddr);
+ }
+
+ buf->vaddr = vaddr;
+ buf->size = size;
+ return 0;
+}
+
+void aie4_free_msg_buffer(struct amdxdna_dev *xdna, struct aie4_msg_buf *buf)
+{
+ if (!buf->vaddr)
+ return;
+
+ amdxdna_free_msg_buffer(xdna, buf->size, buf->vaddr, buf->dma_addr);
+ buf->vaddr = NULL;
+}
+
int aie4_suspend_fw(struct amdxdna_dev_hdl *ndev)
{
DECLARE_AIE_MSG(aie4_msg_suspend, AIE4_MSG_OP_SUSPEND);
@@ -235,7 +260,7 @@ int aie4_attach_work_buffer(struct amdxdna_dev_hdl *ndev)
struct amdxdna_dev *xdna = ndev->aie.xdna;
int ret;
- req.buff_addr = ndev->work_buf_addr;
+ req.buff_addr = ndev->work_buf.dma_addr;
req.buff_size = AIE4_WORK_BUFFER_MIN_SIZE;
ret = aie_send_mgmt_msg_wait(&ndev->aie, &msg);
@@ -253,8 +278,8 @@ int aie4_start_fw_log(struct amdxdna_dev_hdl *ndev, u32 level)
struct amdxdna_dev *xdna = ndev->aie.xdna;
int ret;
- req.buff_addr = ndev->fw_log_buf_addr;
- req.buff_size = ndev->fw_log_buf_size;
+ req.buff_addr = ndev->fw_log_buf.dma_addr;
+ req.buff_size = ndev->fw_log_buf.size;
req.log_level = level;
ret = aie_send_mgmt_msg_wait(&ndev->aie, &msg);
@@ -262,7 +287,7 @@ int aie4_start_fw_log(struct amdxdna_dev_hdl *ndev, u32 level)
XDNA_WARN(xdna, "Failed to start fw log, ret %d", ret);
else
XDNA_DBG(xdna, "Started fw log, level %u size 0x%x",
- level, ndev->fw_log_buf_size);
+ level, ndev->fw_log_buf.size);
return ret;
}
@@ -346,3 +371,29 @@ int aie4_configure_hw_context_cert_log(struct amdxdna_dev_hdl *ndev,
return ret;
}
+
+int aie4_register_async_event_msg(struct amdxdna_dev_hdl *ndev, dma_addr_t addr,
+ u32 size, void *handle,
+ int (*cb)(void *, void __iomem *, size_t))
+{
+ struct aie4_msg_async_event_config_req req = { 0 };
+ struct xdna_mailbox_msg msg = {
+ .send_data = (u8 *)&req,
+ .send_size = sizeof(req),
+ .handle = handle,
+ .opcode = AIE4_MSG_OP_ASYNC_EVENT_REPORT,
+ .notify_cb = cb,
+ };
+
+ /* A mailbox timeout leaves no channel. */
+ if (!ndev->aie.mgmt_chann)
+ return -ENODEV;
+
+ /* Management buffers are in the default domain, so pasid stays 0. */
+ req.buff_addr = addr;
+ req.buff_size = size;
+
+ XDNA_DBG(ndev->aie.xdna, "Register async event buffer 0x%llx size 0x%x",
+ addr, size);
+ return xdna_mailbox_send_msg(ndev->aie.mgmt_chann, &msg);
+}
diff --git a/drivers/accel/amdxdna/aie4_msg_priv.h b/drivers/accel/amdxdna/aie4_msg_priv.h
index 977f7651341a..f8c94cf316fd 100644
--- a/drivers/accel/amdxdna/aie4_msg_priv.h
+++ b/drivers/accel/amdxdna/aie4_msg_priv.h
@@ -6,12 +6,14 @@
#ifndef _AIE4_MSG_PRIV_H_
#define _AIE4_MSG_PRIV_H_
+#include <linux/bits.h>
#include <linux/sizes.h>
#include <linux/types.h>
enum aie4_msg_opcode {
AIE4_MSG_OP_IDENTIFY = 0x10002,
AIE4_MSG_OP_SUSPEND = 0x10003,
+ AIE4_MSG_OP_ASYNC_EVENT_REPORT = 0x10004,
AIE4_MSG_OP_SET_RUNTIME_CONFIG = 0x10007,
AIE4_MSG_OP_QUERY_CERT_FIRMWARE_VERSION = 0x1000F,
@@ -37,6 +39,7 @@ enum aie4_msg_status {
AIE4_MSG_STATUS_SUCCESS = 0x0,
AIE4_MSG_STATUS_ERROR = 0x1,
AIE4_MSG_STATUS_NOTSUPP = 0x2,
+ AIE4_MSG_STATUS_ASYNC_EVENT_MSGS_FULL = 0x3,
MAX_AIE4_MSG_STATUS_CODE = 0x4,
};
@@ -322,4 +325,81 @@ struct aie4_msg_start_fw_log_resp {
enum aie4_msg_status status;
} __packed;
+#define AIE4_ASYNC_BUF_SIZE SZ_8K
+
+/* AIE4_MSG_OP_ASYNC_EVENT_REPORT: register one async event report buffer. */
+struct aie4_msg_async_event_config_req {
+ __u64 buff_addr;
+ __u32 pasid;
+ __u32 buff_size;
+} __packed;
+
+/* Sent by firmware once it has written an event into the buffer. */
+struct aie4_msg_async_event_config_resp {
+ enum aie4_msg_status status;
+ __u32 type;
+} __packed;
+
+enum aie4_msg_async_event_type {
+ AIE4_ASYNC_EVENT_TYPE_AIE_ERROR,
+ AIE4_ASYNC_EVENT_TYPE_RESERVED1,
+ AIE4_ASYNC_EVENT_TYPE_CTX_ERROR,
+ AIE4_ASYNC_EVENT_TYPE_RESERVED2,
+ MAX_AIE4_ASYNC_EVENT_TYPE,
+};
+
+/* Maximum number of uCs in an app health report. */
+#define AIE4_MPNPUFW_MAX_UC_COUNT 6
+
+/* Field masks for the packed version and ctx_num_uc words. */
+#define AIE4_APP_HEALTH_MAJOR_VER GENMASK(15, 0)
+#define AIE4_APP_HEALTH_MINOR_VER GENMASK(31, 16)
+#define AIE4_APP_HEALTH_CTX_STATUS GENMASK(15, 0)
+#define AIE4_APP_HEALTH_NUM_UC GENMASK(31, 16)
+
+/* Context error types in struct aie4_async_ctx_error. */
+enum aie4_msg_async_ctx_error_type {
+ AIE4_ASYNC_EVENT_CTX_ERR_HWSCH_FAILURE,
+ AIE4_ASYNC_EVENT_CTX_ERR_STOP_FAILURE,
+ AIE4_ASYNC_EVENT_CTX_ERR_AIE_FAILURE,
+ AIE4_ASYNC_EVENT_CTX_ERR_PREEMPTION_TIMEOUT,
+ AIE4_ASYNC_EVENT_CTX_ERR_NEW_PROCESS_FAILURE,
+ AIE4_ASYNC_EVENT_CTX_ERR_UC_CRITICAL_ERROR,
+ AIE4_ASYNC_EVENT_CTX_ERR_UC_COMPLETION_TIMEOUT,
+};
+
+struct aie4_uc_health_info {
+ __u32 uc_idx;
+ __u32 uc_idle_status;
+ __u32 misc_status;
+ __u32 fw_state;
+ __u32 page_idx;
+ __u32 offset;
+ __u32 restore_page;
+ __u32 restore_offset;
+ __u32 uc_ear;
+ __u32 uc_esr;
+ __u32 uc_pc;
+};
+
+/*
+ * App health report written with a context error. version is 0 when firmware
+ * could not collect one. runlist_read_idx is the index of the run list entry
+ * that was running.
+ */
+struct aie4_msg_app_health_report {
+ __u32 version;
+ __u32 context_id;
+ __u32 ctx_num_uc;
+ __u32 runlist_read_idx;
+ struct aie4_uc_health_info uc_info[AIE4_MPNPUFW_MAX_UC_COUNT];
+};
+
+/* Report in the buffer for a context event. */
+struct aie4_async_ctx_error {
+ __u32 ctx_id;
+ __u32 error_type;
+ struct aie4_msg_app_health_report app_health_report;
+};
+
#endif /* _AIE4_MSG_PRIV_H_ */
diff --git a/drivers/accel/amdxdna/aie4_pci.c b/drivers/accel/amdxdna/aie4_pci.c
index 7a7df655e5f8..5910563b6c1d 100644
--- a/drivers/accel/amdxdna/aie4_pci.c
+++ b/drivers/accel/amdxdna/aie4_pci.c
@@ -111,6 +111,11 @@ static void aie4_mailbox_fini(struct amdxdna_dev_hdl *ndev)
struct amdxdna_dev *xdna = ndev->aie.xdna;
aie_destroy_chann(&ndev->aie, &ndev->aie.mgmt_chann);
+ /*
+ * Ignore late events from this channel; the next hardware start
+ * registers the buffers again.
+ */
+ aie4_async_events_stop(ndev);
drmm_kfree(&xdna->ddev, ndev->mbox);
ndev->mbox = NULL;
}
@@ -389,11 +394,9 @@ static int aie4_config_fw(struct amdxdna_dev_hdl *ndev)
* Give the firmware high performance DRAM to log into with less
* verbose level.
*/
- if (ndev->fw_log_buf) {
- ret = aie4_start_fw_log(ndev, AIE4_FW_LOG_LEVEL_ERR);
- if (ret == -ETIME)
- return ret;
- }
+ ret = aie4_start_fw_log(ndev, AIE4_FW_LOG_LEVEL_ERR);
+ if (ret == -ETIME)
+ return ret;
return 0;
}
@@ -485,6 +488,11 @@ static int aie4_vf_hw_start(struct amdxdna_dev_hdl *ndev)
if (ret)
goto mbox_fini;
+ /*
+ * A VF registers no async event buffers, so firmware keeps none of
+ * its memory across a VF reset or driver reload.
+ */
+
ret = aie4_restore_power_mode(ndev);
if (ret)
goto partition_fini;
@@ -532,6 +540,11 @@ static int aie4_classic_hw_start(struct amdxdna_dev_hdl *ndev)
if (ret)
goto mbox_fini;
+ /* One async event buffer per column. */
+ ret = aie4_async_events_register(ndev, ndev->total_col);
+ if (ret)
+ goto partition_fini;
+
ret = aie4_restore_power_mode(ndev);
if (ret)
goto partition_fini;
@@ -1029,70 +1042,33 @@ static int aie4_set_state(struct amdxdna_client *client,
static int aie4_alloc_work_buffer(struct amdxdna_dev_hdl *ndev)
{
struct amdxdna_dev *xdna = ndev->aie.xdna;
- u32 buf_size = AIE4_WORK_BUFFER_MIN_SIZE;
-
- ndev->work_buf = amdxdna_alloc_msg_buffer(xdna, &buf_size,
- &ndev->work_buf_addr);
- if (IS_ERR(ndev->work_buf)) {
- int ret = PTR_ERR(ndev->work_buf);
+ int ret;
+ ret = aie4_alloc_msg_buffer(xdna, &ndev->work_buf, AIE4_WORK_BUFFER_MIN_SIZE);
+ if (ret) {
XDNA_ERR(xdna, "Failed to alloc work buffer, size 0x%x",
AIE4_WORK_BUFFER_MIN_SIZE);
- ndev->work_buf = NULL;
return ret;
}
- ndev->work_buf_size = buf_size;
- XDNA_DBG(xdna, "Work buffer allocated: size 0x%x", buf_size);
-
+ XDNA_DBG(xdna, "Work buffer allocated: size 0x%x", ndev->work_buf.size);
return 0;
}
-static void aie4_free_work_buffer(struct amdxdna_dev_hdl *ndev)
+static int aie4_alloc_fw_log_buffer(struct amdxdna_dev_hdl *ndev)
{
struct amdxdna_dev *xdna = ndev->aie.xdna;
+ int ret;
- if (!ndev->work_buf)
- return;
-
- amdxdna_free_msg_buffer(xdna, ndev->work_buf_size, ndev->work_buf,
- ndev->work_buf_addr);
- ndev->work_buf = NULL;
-}
-
-/*
- * Firmware logging is best effort: a device that cannot spare the buffer still
- * runs, it just does not log. Never fail hw start or probe on this.
- */
-static void aie4_alloc_fw_log_buffer(struct amdxdna_dev_hdl *ndev)
-{
- struct amdxdna_dev *xdna = ndev->aie.xdna;
- u32 buf_size = AIE4_FW_LOG_BUF_SIZE;
-
- ndev->fw_log_buf = amdxdna_alloc_msg_buffer(xdna, &buf_size,
- &ndev->fw_log_buf_addr);
- if (IS_ERR(ndev->fw_log_buf)) {
- XDNA_WARN(xdna, "Failed to alloc fw log buffer, size 0x%x",
- AIE4_FW_LOG_BUF_SIZE);
- ndev->fw_log_buf = NULL;
- return;
+ ret = aie4_alloc_msg_buffer(xdna, &ndev->fw_log_buf, AIE4_FW_LOG_BUF_SIZE);
+ if (ret) {
+ XDNA_ERR(xdna, "Failed to alloc fw log buffer, size 0x%x",
+ AIE4_FW_LOG_BUF_SIZE);
+ return ret;
}
- ndev->fw_log_buf_size = buf_size;
- XDNA_DBG(xdna, "FW log buffer allocated: size 0x%x", buf_size);
-}
-
-/* Free logging buffer after firmware execution has stopped. */
-static void aie4_free_fw_log_buffer(struct amdxdna_dev_hdl *ndev)
-{
- struct amdxdna_dev *xdna = ndev->aie.xdna;
-
- if (!ndev->fw_log_buf)
- return;
-
- amdxdna_free_msg_buffer(xdna, ndev->fw_log_buf_size, ndev->fw_log_buf,
- ndev->fw_log_buf_addr);
- ndev->fw_log_buf = NULL;
+ XDNA_DBG(xdna, "FW log buffer allocated: size 0x%x", ndev->fw_log_buf.size);
+ return 0;
}
static int aie4_pf_init(struct amdxdna_dev *xdna)
@@ -1107,17 +1083,20 @@ static int aie4_pf_init(struct amdxdna_dev *xdna)
if (ret)
return ret;
- aie4_alloc_fw_log_buffer(xdna->dev_handle);
+ ret = aie4_alloc_fw_log_buffer(xdna->dev_handle);
+ if (ret)
+ goto free_work_buf;
ret = aie4_pf_hw_start(xdna->dev_handle);
if (ret)
- goto free_work_buf;
+ goto free_fw_log_buf;
return 0;
+free_fw_log_buf:
+ aie4_free_msg_buffer(xdna, &xdna->dev_handle->fw_log_buf);
free_work_buf:
- aie4_free_fw_log_buffer(xdna->dev_handle);
- aie4_free_work_buffer(xdna->dev_handle);
+ aie4_free_msg_buffer(xdna, &xdna->dev_handle->work_buf);
return ret;
}
@@ -1129,7 +1108,16 @@ static int aie4_vf_init(struct amdxdna_dev *xdna)
if (ret)
return ret;
- return aie4_vf_hw_start(xdna->dev_handle);
+ /* A VF registers no buffers with firmware; allocate only the workqueue. */
+ ret = aie4_async_events_alloc(xdna->dev_handle, 0);
+ if (ret)
+ return ret;
+
+ ret = aie4_vf_hw_start(xdna->dev_handle);
+ if (ret)
+ aie4_async_events_free(xdna->dev_handle);
+
+ return ret;
}
static int aie4_classic_init(struct amdxdna_dev *xdna)
@@ -1144,18 +1132,27 @@ static int aie4_classic_init(struct amdxdna_dev *xdna)
if (ret)
return ret;
- aie4_alloc_fw_log_buffer(xdna->dev_handle);
+ ret = aie4_alloc_fw_log_buffer(xdna->dev_handle);
+ if (ret)
+ goto free_work_buf;
+
+ ret = aie4_async_events_alloc(xdna->dev_handle, AMDXDNA_MAX_ASYNC_EVENT_BUFS);
+ if (ret)
+ goto free_fw_log_buf;
ret = aie4_classic_hw_start(xdna->dev_handle);
if (ret)
- goto free_work_buf;
+ goto free_events_buffer;
amdxdna_pm_init(xdna);
return 0;
+free_events_buffer:
+ aie4_async_events_free(xdna->dev_handle);
+free_fw_log_buf:
+ aie4_free_msg_buffer(xdna, &xdna->dev_handle->fw_log_buf);
free_work_buf:
- aie4_free_fw_log_buffer(xdna->dev_handle);
- aie4_free_work_buffer(xdna->dev_handle);
+ aie4_free_msg_buffer(xdna, &xdna->dev_handle->work_buf);
return ret;
}
@@ -1163,21 +1160,23 @@ static void aie4_pf_fini(struct amdxdna_dev *xdna)
{
aie4_sriov_stop(xdna->dev_handle);
aie4_pf_hw_stop(xdna->dev_handle);
- aie4_free_fw_log_buffer(xdna->dev_handle);
- aie4_free_work_buffer(xdna->dev_handle);
+ aie4_free_msg_buffer(xdna, &xdna->dev_handle->fw_log_buf);
+ aie4_free_msg_buffer(xdna, &xdna->dev_handle->work_buf);
}
static void aie4_vf_fini(struct amdxdna_dev *xdna)
{
aie4_vf_hw_stop(xdna->dev_handle);
+ aie4_async_events_free(xdna->dev_handle);
}
static void aie4_classic_fini(struct amdxdna_dev *xdna)
{
amdxdna_pm_fini(xdna);
aie4_classic_hw_stop(xdna->dev_handle);
- aie4_free_fw_log_buffer(xdna->dev_handle);
- aie4_free_work_buffer(xdna->dev_handle);
+ aie4_async_events_free(xdna->dev_handle);
+ aie4_free_msg_buffer(xdna, &xdna->dev_handle->fw_log_buf);
+ aie4_free_msg_buffer(xdna, &xdna->dev_handle->work_buf);
}
static int aie4_ctx_hysteresis_get(void *data, u64 *val)
diff --git a/drivers/accel/amdxdna/aie4_pci.h b/drivers/accel/amdxdna/aie4_pci.h
index 158e68199952..9c37c78169bf 100644
--- a/drivers/accel/amdxdna/aie4_pci.h
+++ b/drivers/accel/amdxdna/aie4_pci.h
@@ -20,6 +20,8 @@
/* Default context switch hysteresis timeout in microseconds. */
#define AIE4_CTX_HYSTERESIS_US 1000
+struct aie4_async_events;
+
struct cert_comp {
struct amdxdna_dev_hdl *ndev;
u32 msix_idx;
@@ -87,6 +89,18 @@ struct amdxdna_dev_priv {
const struct aie_hw_ops *hw_ops;
};
+/*
+ * struct aie4_msg_buf - DMA buffer shared with firmware
+ * @vaddr: CPU address, NULL when not allocated
+ * @dma_addr: device address
+ * @size: allocated size, rounded up by aie4_alloc_msg_buffer()
+ */
+struct aie4_msg_buf {
+ void *vaddr;
+ dma_addr_t dma_addr;
+ u32 size;
+};
+
struct amdxdna_dev_hdl {
struct aie_device aie;
const struct amdxdna_dev_priv *priv;
@@ -106,13 +120,9 @@ struct amdxdna_dev_hdl {
struct xarray cert_comp_xa; /* device level indexed by msix id */
struct mutex cert_comp_lock; /* protects cert_comp operations*/
- void *work_buf;
- dma_addr_t work_buf_addr;
- u32 work_buf_size;
-
- void *fw_log_buf;
- dma_addr_t fw_log_buf_addr;
- u32 fw_log_buf_size;
+ struct aie4_msg_buf work_buf;
+ struct aie4_msg_buf fw_log_buf;
+ struct aie4_async_events *async_events;
u8 pw_mode;
@@ -127,6 +137,12 @@ enum aie4_fw_feature {
AIE4_FEATURE_MAX
};
+/* aie4_error.c */
+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);
+
/* aie4_message.c */
int aie4_query_aie_metadata(struct amdxdna_dev_hdl *ndev,
struct amdxdna_drm_query_aie_metadata *metadata);
@@ -149,7 +165,12 @@ int aie4_set_ctx_hysteresis(struct amdxdna_dev_hdl *ndev, u32 timeout_us);
int aie4_configure_hw_context_cert_log(struct amdxdna_dev_hdl *ndev,
u32 hw_context_id, u32 property,
const struct aie4_msg_context_config_cert_logging *cl);
+int aie4_register_async_event_msg(struct amdxdna_dev_hdl *ndev, dma_addr_t addr,
+ u32 size, void *handle,
+ int (*cb)(void *, void __iomem *, size_t));
u32 aie4_msg_pasid(struct amdxdna_client *client);
+int aie4_alloc_msg_buffer(struct amdxdna_dev *xdna, struct aie4_msg_buf *buf, u32 size);
+void aie4_free_msg_buffer(struct amdxdna_dev *xdna, struct aie4_msg_buf *buf);
/* aie4_ctx.c */
enum aie4_hwctx_flags {
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH V2 2/5] accel/amdxdna: Add AIE4 context recovery
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 ` David Zhang
2026-11-30 22:59 ` [PATCH V2 3/5] accel/amdxdna: Check mailbox message overflow David Zhang
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: David Zhang @ 2026-11-30 22:59 UTC (permalink / raw)
To: quic_jhugo, karol.wachowski, max.zhen, lizhi.hou, ogabbay,
dri-devel, linux-kernel
Cc: David Zhang, sonal.santan, mario.limonciello
Recover an AIE4 context when firmware reports a context error in an
async event: destroy and create the firmware context again, and
complete the jobs that were running on it.
Signed-off-by: David Zhang <yidong.zhang@amd.com>
---
drivers/accel/amdxdna/aie4_ctx.c | 292 +++++++++++++++++++++-----
drivers/accel/amdxdna/aie4_error.c | 76 +++++++
drivers/accel/amdxdna/aie4_pci.c | 25 +++
drivers/accel/amdxdna/aie4_pci.h | 27 ++-
drivers/accel/amdxdna/amdxdna_ctx.h | 1 +
drivers/accel/amdxdna/amdxdna_error.h | 10 +
6 files changed, 375 insertions(+), 56 deletions(-)
diff --git a/drivers/accel/amdxdna/aie4_ctx.c b/drivers/accel/amdxdna/aie4_ctx.c
index 7904e05ca826..9f61fcd6e518 100644
--- a/drivers/accel/amdxdna/aie4_ctx.c
+++ b/drivers/accel/amdxdna/aie4_ctx.c
@@ -258,6 +258,9 @@ int aie4_hwctx_create(struct amdxdna_hwctx *hwctx)
}
priv->hw_ctx_id = resp.hw_context_id;
+ /* Firmware reuses context IDs; let error handlers skip newer contexts. */
+ WRITE_ONCE(ndev->ctx_gen, ndev->ctx_gen + 1);
+ priv->gen = ndev->ctx_gen;
hwctx->fw_ctx_id = resp.hw_context_id;
hwctx->start_col = 0;
@@ -277,7 +280,7 @@ int aie4_hwctx_create(struct amdxdna_hwctx *hwctx)
hwctx->fw_ctx_id = -1;
return ret;
}
- WRITE_ONCE(priv->has_error, false);
+ WRITE_ONCE(priv->has_reset, false);
WRITE_ONCE(priv->cert_comp, cert_comp);
mutex_unlock(&priv->io_lock);
hwctx->doorbell_offset = CTX_INVALID_DOORBELL;
@@ -286,17 +289,6 @@ int aie4_hwctx_create(struct amdxdna_hwctx *hwctx)
return 0;
}
-/* Linked cert_comp acts as connected sentinel for submit waiters. */
-static bool aie4_hwctx_connected(struct amdxdna_hwctx *hwctx)
-{
- return !!READ_ONCE(hwctx->priv->cert_comp);
-}
-
-static bool aie4_hwctx_has_error(struct amdxdna_hwctx *hwctx)
-{
- return READ_ONCE(hwctx->priv->has_error);
-}
-
void aie4_hwctx_destroy(struct amdxdna_hwctx *hwctx, enum aie4_hwctx_flags flags)
{
struct amdxdna_client *client = hwctx->client;
@@ -304,26 +296,21 @@ void aie4_hwctx_destroy(struct amdxdna_hwctx *hwctx, enum aie4_hwctx_flags flags
struct amdxdna_dev *xdna = client->xdna;
struct amdxdna_dev_hdl *ndev = xdna->dev_handle;
struct cert_comp *cert_comp;
- bool has_error = false;
+ bool has_reset = false;
drm_WARN_ON(&xdna->ddev, !mutex_is_locked(&xdna->dev_lock));
if (flags == AIE4_HWCTX_DISCONNECT || flags == AIE4_HWCTX_ERROR)
- has_error = true;
+ has_reset = true;
mutex_lock(&priv->io_lock);
- if (has_error)
- WRITE_ONCE(priv->has_error, true);
- cert_comp = priv->cert_comp;
- WRITE_ONCE(priv->cert_comp, NULL);
- mutex_unlock(&priv->io_lock);
-
- if (cert_comp)
- wake_up_all(&cert_comp->waitq);
-
- if (has_error)
- wake_up_all(&priv->job_list_wq);
+ if (has_reset)
+ WRITE_ONCE(priv->has_reset, true);
+ /*
+ * Destroy the firmware context first; woken waiters complete its jobs.
+ * Hold io_lock so no submitter publishes to the context meanwhile.
+ */
if (flags != AIE4_HWCTX_DISCONNECT && priv->hw_ctx_id != CTX_INVALID_ID) {
/*
* Pairs with aie4_hwctx_create(): detach debug buffers.
@@ -334,6 +321,16 @@ void aie4_hwctx_destroy(struct amdxdna_hwctx *hwctx, enum aie4_hwctx_flags flags
aie4_msg_destroy_context(ndev, priv->hw_ctx_id);
}
+ cert_comp = priv->cert_comp;
+ WRITE_ONCE(priv->cert_comp, NULL);
+ mutex_unlock(&priv->io_lock);
+
+ if (cert_comp)
+ wake_up_all(&cert_comp->waitq);
+
+ if (has_reset)
+ wake_up_all(&priv->job_list_wq);
+
if (cert_comp)
aie4_put_cert_comp(cert_comp);
@@ -341,8 +338,8 @@ void aie4_hwctx_destroy(struct amdxdna_hwctx *hwctx, enum aie4_hwctx_flags flags
hwctx->fw_ctx_id = -1;
hwctx->doorbell_offset = CTX_INVALID_DOORBELL;
- /* Skip cancel_work_sync on error so worker can abort in-flight jobs. */
- if (!has_error)
+ /* On reset, the worker still retires running jobs: completed or faulted. */
+ if (!has_reset)
cancel_work_sync(&priv->job_work);
}
@@ -545,13 +542,6 @@ static u64 get_read_index(struct amdxdna_hwctx *hwctx)
return ri;
}
-/* Verify cert_comp remains linked to detect disconnect or reset. */
-static bool check_cert_comp_linked(struct amdxdna_hwctx *hwctx, struct cert_comp *comp)
-{
- /* READ_ONCE pairs with the link/unlink WRITE_ONCE. */
- return comp == READ_ONCE(hwctx->priv->cert_comp);
-}
-
/*
* Lockless wait condition: wake on completion or disconnect. Record
* completion, so callers do not re-read state that can change after the
@@ -561,30 +551,55 @@ static inline bool cmd_done_or_unlinked(struct amdxdna_hwctx *hwctx, u64 seq,
struct cert_comp *comp, bool *done)
{
*done = get_read_index(hwctx) > seq;
- return *done || !check_cert_comp_linked(hwctx, comp);
+ /* An unlinked cert_comp means a disconnect or reset; pairs with WRITE_ONCE. */
+ return *done || (comp != READ_ONCE(hwctx->priv->cert_comp));
}
/*
- * Return 0 once seq completed, -ENODEV if the context is lost, or -EAGAIN
- * if it is suspended by power management before seq completed.
+ * Return 0 once seq completed, -ETIME when the user timeout expires, -ENODEV
+ * if the context is lost, or -EAGAIN to wait again: the context was suspended
+ * or reset, or an unlimited wait reached the job timeout.
*/
int aie4_cmd_wait(struct amdxdna_hwctx *hwctx, u64 seq, u32 timeout)
{
unsigned long wait_jifs = MAX_SCHEDULE_TIMEOUT;
- struct cert_comp *cert_comp = aie4_get_cert_comp(hwctx);
+ struct amdxdna_hwctx_priv *priv = hwctx->priv;
+ struct cert_comp *cert_comp;
bool done = false;
long ret;
+ if (timeout)
+ wait_jifs = msecs_to_jiffies(timeout);
+
+ /* A reset disconnects the context for a while; wait for it to end. */
+ ret = wait_event_interruptible_timeout(priv->job_list_wq,
+ !READ_ONCE(priv->resetting), wait_jifs);
+ if (ret < 0)
+ return ret;
+ if (!ret)
+ return -ETIME;
+ wait_jifs = ret;
+ /*
+ * A completion can arrive without its interrupt. Bound an unlimited
+ * wait by the job timeout, which re-checks it; user space waits again
+ * on -EAGAIN.
+ */
+ if (!timeout)
+ wait_jifs = msecs_to_jiffies(AIE4_JOB_TIMEOUT_MS);
+
+ cert_comp = aie4_get_cert_comp(hwctx);
if (!cert_comp) {
/* Not connected: read_index in host memory is still valid. */
if (get_read_index(hwctx) > seq)
return 0;
- return aie4_hwctx_has_error(hwctx) ? -ENODEV : -EAGAIN;
+ /*
+ * A reset in progress is retried; a context still reset after it
+ * is dead. Read both flags under io_lock, where the reset sets them.
+ */
+ guard(mutex)(&priv->io_lock);
+ return priv->has_reset && !priv->resetting ? -ENODEV : -EAGAIN;
}
- if (timeout)
- wait_jifs = msecs_to_jiffies(timeout);
-
ret = wait_event_interruptible_timeout(cert_comp->waitq,
cmd_done_or_unlinked(hwctx, seq, cert_comp,
&done),
@@ -595,11 +610,11 @@ int aie4_cmd_wait(struct amdxdna_hwctx *hwctx, u64 seq, u32 timeout)
return ret;
if (done)
return 0;
- if (!ret)
+ if (!ret && timeout)
return -ETIME;
- /* Woken by disconnect before seq completed. */
- return aie4_hwctx_has_error(hwctx) ? -ENODEV : -EAGAIN;
+ /* User space retries -EAGAIN. */
+ return -EAGAIN;
}
/* ---- kernel-mode submission (driver fills the queue and rings doorbell) ---- */
@@ -648,6 +663,22 @@ static int wait_till_seq_completed(struct amdxdna_hwctx *hwctx, u64 seq,
return ret ? -EAGAIN : -ETIME;
}
+/*
+ * During a reset, a submitter with no published command waits for the new
+ * firmware context; one that published part of a chain returns so the reset
+ * can complete it.
+ */
+static bool aie4_submit_wait_done(struct amdxdna_hwctx *hwctx)
+{
+ struct amdxdna_sched_job *job = READ_ONCE(hwctx->priv->pending_head);
+
+ if (!AIE4_HWCTX_HAS_RESET(hwctx))
+ return AIE4_HWCTX_CONNECTED(hwctx);
+
+ return !(READ_ONCE(hwctx->priv->resetting) && job &&
+ job->aie4_job_state == AIE4_JOB_STATE_PENDING);
+}
+
static int wait_till_connected_hsa_not_full(struct amdxdna_hwctx *hwctx)
{
struct amdxdna_dev *xdna = hwctx->client->xdna;
@@ -669,17 +700,20 @@ static int wait_till_connected_hsa_not_full(struct amdxdna_hwctx *hwctx)
hsa_not_full = true;
}
ret = wait_event_freezable(priv->job_list_wq,
- aie4_hwctx_connected(hwctx) ||
- aie4_hwctx_has_error(hwctx));
+ aie4_submit_wait_done(hwctx));
mutex_lock(&priv->io_lock);
if (ret)
return ret;
- if (aie4_hwctx_has_error(hwctx)) {
- XDNA_DBG(xdna, "ctx %s in error; unwinding -ENODEV",
+ if (AIE4_HWCTX_HAS_RESET(hwctx)) {
+ /* A reset started after the wait; wait for the new context. */
+ if (!aie4_submit_wait_done(hwctx))
+ continue;
+ XDNA_DBG(xdna, "ctx %s is reset; unwinding -ENODEV",
hwctx->name);
return -ENODEV;
}
- } while (!hsa_not_full || !aie4_hwctx_connected(hwctx));
+ } while (!hsa_not_full || !AIE4_HWCTX_CONNECTED(hwctx) ||
+ AIE4_HWCTX_HAS_RESET(hwctx));
return 0;
}
@@ -955,6 +989,107 @@ static void job_abort(struct amdxdna_sched_job *job)
job_done(job);
}
+/*
+ * Mark the job that faulted, or that the job timeout found hung, as timed
+ * out. Use only the command BOs looked up at submit, as the command BO is
+ * writable by user space and its handles may be gone.
+ */
+/* Context health data of a timed-out command, read by user space. */
+struct aie4_ctx_health {
+ struct amdxdna_ctx_health header;
+ u32 ctx_state;
+ u32 num_uc;
+ u32 ctx_error_type;
+ struct aie4_uc_health_info uc_info[AIE4_MPNPUFW_MAX_UC_COUNT];
+};
+
+/* Write the health data, with no more uC entries than fit in @size. */
+static void aie4_fill_ctx_health(const struct aie4_async_ctx_error *report,
+ void *data, size_t size)
+{
+ const struct aie4_msg_app_health_report *app = &report->app_health_report;
+ struct aie4_ctx_health health = {
+ .header.version = AMDXDNA_CMD_CTX_HEALTH_V1,
+ .header.npu_gen = AMDXDNA_CMD_CTX_HEALTH_AIE4,
+ };
+ size_t len = offsetof(struct aie4_ctx_health, uc_info);
+ u32 num_uc = 0;
+
+ if (size < len)
+ return;
+
+ /* The error type is valid without an app health report. */
+ health.ctx_error_type = report->error_type;
+ if (app->version) {
+ num_uc = min_t(u32, FIELD_GET(AIE4_APP_HEALTH_NUM_UC, app->ctx_num_uc),
+ AIE4_MPNPUFW_MAX_UC_COUNT);
+ num_uc = min_t(u32, num_uc, (size - len) / sizeof(health.uc_info[0]));
+ health.ctx_state = FIELD_GET(AIE4_APP_HEALTH_CTX_STATUS, app->ctx_num_uc);
+ memcpy(health.uc_info, app->uc_info, num_uc * sizeof(health.uc_info[0]));
+ }
+ health.num_uc = num_uc;
+
+ memcpy(data, &health, len + num_uc * sizeof(health.uc_info[0]));
+}
+
+/*
+ * Mark the job that faulted as timed out, with the health data from
+ * @report. Use only the command BOs looked up at submit, as the command BO
+ * is writable by user space and its handles may be gone.
+ */
+static void job_fault(struct amdxdna_sched_job *job,
+ const struct aie4_async_ctx_error *report)
+{
+ const struct aie4_msg_app_health_report *app = &report->app_health_report;
+ struct amdxdna_hwctx *hwctx = job->hwctx;
+ struct amdxdna_gem_obj *abo = job->cmd_bo;
+ struct amdxdna_cmd_chain *cc;
+ struct amdxdna_cmd *cmd;
+ u32 cmd_idx, len;
+
+ XDNA_ERR(hwctx->client->xdna, "%s job %lld timed out", hwctx->name, job->seq);
+ amdxdna_cmd_set_state(job->cmd_bo, ERT_CMD_STATE_TIMEOUT);
+
+ if (job->aie4_cmd_bo_cnt) {
+ /* The run list index is valid only with a health report. */
+ cmd_idx = app->version ? app->runlist_read_idx : 0;
+ if (cmd_idx >= job->aie4_cmd_bo_cnt)
+ cmd_idx = 0;
+ /* Bounds-checked against the BO size. */
+ cc = amdxdna_cmd_get_payload(job->cmd_bo, &len);
+ if (cc && len >= offsetofend(struct amdxdna_cmd_chain, error_index))
+ cc->error_index = cmd_idx;
+ abo = job->aie4_cmd_bos[cmd_idx];
+ }
+
+ cmd = amdxdna_gem_vmap(abo);
+ if (cmd) {
+ memset(cmd->data, 0xff, abo->mem.size - sizeof(*cmd));
+ aie4_fill_ctx_health(report, cmd->data, abo->mem.size - sizeof(*cmd));
+ }
+
+ dma_fence_set_error(job->fence, -ETIME);
+ if (get_read_index(hwctx) <= job->seq)
+ update_read_index(hwctx, job->seq + 1);
+ job_done(job);
+}
+
+/* Return true once per reset for a fault, so only the oldest job times out. */
+static bool aie4_hwctx_take_fault(struct amdxdna_hwctx *hwctx,
+ struct aie4_async_ctx_error *report)
+{
+ struct amdxdna_hwctx_priv *priv = hwctx->priv;
+ bool fault;
+
+ mutex_lock(&priv->io_lock);
+ fault = priv->reset_fault;
+ priv->reset_fault = false;
+ *report = priv->reset_report;
+ mutex_unlock(&priv->io_lock);
+
+ return fault;
+}
+
/*
* Stop a context whose running job timed out, so the NPU no longer
* accesses the job BOs before the worker aborts the jobs and signals
@@ -969,7 +1104,7 @@ 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_CONNECTED(hwctx) && get_read_index(hwctx) <= job->seq) {
XDNA_ERR(xdna, "hwctx %s job %llu timed out", hwctx->name, job->seq);
aie4_hwctx_stop(hwctx);
}
@@ -982,6 +1117,7 @@ static void job_worker(struct work_struct *work)
container_of(work, struct amdxdna_hwctx_priv, job_work);
struct amdxdna_hwctx *hwctx = priv->hwctx;
struct amdxdna_sched_job *job;
+ struct aie4_async_ctx_error report;
int ret;
while ((job = peek_running_job(hwctx))) {
@@ -998,9 +1134,12 @@ static void job_worker(struct work_struct *work)
job_abort(job);
else
job_complete(job);
- } else if (aie4_hwctx_has_error(hwctx)) {
+ } else if (AIE4_HWCTX_HAS_RESET(hwctx)) {
dequeue_running_job(hwctx, job);
- job_abort(job);
+ if (aie4_hwctx_take_fault(hwctx, &report))
+ job_fault(job, &report);
+ else
+ job_abort(job);
} else {
/* Disconnected by suspend; resume requeues the worker. */
break;
@@ -1037,6 +1176,53 @@ int aie4_hwctx_wait_for_running(struct amdxdna_hwctx *hwctx)
return ret;
}
+/*
+ * Reset a context: destroy the firmware context, complete the running jobs,
+ * then create the firmware context again. Called with dev_lock held.
+ */
+void aie4_hwctx_reset(struct amdxdna_hwctx *hwctx,
+ const struct aie4_async_ctx_error *ctx_err)
+{
+ struct amdxdna_hwctx_priv *priv = hwctx->priv;
+ struct amdxdna_dev *xdna = hwctx->client->xdna;
+ int ret;
+
+ drm_WARN_ON(&xdna->ddev, !mutex_is_locked(&xdna->dev_lock));
+
+ XDNA_ERR(xdna, "Resetting hwctx %s", hwctx->name);
+
+ mutex_lock(&priv->io_lock);
+ WRITE_ONCE(priv->resetting, true);
+ /* Under io_lock, so a submitter does not publish to the old context. */
+ WRITE_ONCE(priv->has_reset, true);
+ priv->reset_fault = true;
+ if (ctx_err)
+ priv->reset_report = *ctx_err;
+ else
+ memset(&priv->reset_report, 0, sizeof(priv->reset_report));
+ mutex_unlock(&priv->io_lock);
+
+ aie4_hwctx_destroy(hwctx, AIE4_HWCTX_ERROR);
+ /*
+ * A chain submitter that did not stop could publish the rest of its
+ * chain to a new context, so leave the context reset instead.
+ */
+ ret = aie4_hwctx_wait_for_running(hwctx);
+ if (!ret)
+ ret = aie4_hwctx_create(hwctx);
+ if (ret)
+ XDNA_ERR(xdna, "Failed to create hwctx %s after reset, ret %d",
+ hwctx->name, ret);
+
+ mutex_lock(&priv->io_lock);
+ priv->reset_fault = false;
+ WRITE_ONCE(priv->resetting, false);
+ mutex_unlock(&priv->io_lock);
+
+ /* Waiters and submitters re-check now that the reset finished. */
+ wake_up_all(&priv->job_list_wq);
+}
+
void aie4_hwctx_resume_jobs(struct amdxdna_hwctx *hwctx)
{
struct amdxdna_hwctx_priv *priv = hwctx->priv;
diff --git a/drivers/accel/amdxdna/aie4_error.c b/drivers/accel/amdxdna/aie4_error.c
index c1f4819d0107..a56bf5891bf1 100644
--- a/drivers/accel/amdxdna/aie4_error.c
+++ b/drivers/accel/amdxdna/aie4_error.c
@@ -14,6 +14,8 @@
#include "aie.h"
#include "aie4_msg_priv.h"
#include "aie4_pci.h"
+#include "amdxdna_ctx.h"
+#include "amdxdna_error.h"
#include "amdxdna_mailbox.h"
#include "amdxdna_pci_drv.h"
@@ -26,6 +28,7 @@
* @addr: DMA address of the slice
* @resp: last response; status is MAX_AIE4_MSG_STATUS_CODE when none
* @resp_gen: @gen when the last response arrived
+ * @resp_ctx_gen: device context generation when the last response arrived
*/
struct aie4_async_event {
struct amdxdna_dev_hdl *ndev;
@@ -35,6 +38,7 @@ struct aie4_async_event {
dma_addr_t addr;
struct aie4_msg_async_event_config_resp resp;
u32 resp_gen;
+ u64 resp_ctx_gen;
};
/*
@@ -63,6 +67,8 @@ static int aie4_async_event_cb(void *handle, void __iomem *data, size_t size)
/* Tag the response with the channel it arrived on. */
e->resp_gen = READ_ONCE(e->events->gen);
+ /* Contexts created after this event are not the one it reports. */
+ e->resp_ctx_gen = READ_ONCE(e->ndev->ctx_gen);
e->resp.type = readl(data +
offsetof(struct aie4_msg_async_event_config_resp, type));
wmb(); /* Update status last; the worker reads it first */
@@ -110,6 +116,68 @@ static bool aie4_tdr_detected(u32 error_type)
error_type == AIE4_ASYNC_EVENT_CTX_ERR_PREEMPTION_TIMEOUT;
}
+static enum amdxdna_error_num aie4_ctx_error_num(u32 error_type)
+{
+ switch (error_type) {
+ case AIE4_ASYNC_EVENT_CTX_ERR_HWSCH_FAILURE:
+ case AIE4_ASYNC_EVENT_CTX_ERR_STOP_FAILURE:
+ return AMDXDNA_ERROR_NUM_KDS_CU;
+ case AIE4_ASYNC_EVENT_CTX_ERR_AIE_FAILURE:
+ case AIE4_ASYNC_EVENT_CTX_ERR_PREEMPTION_TIMEOUT:
+ case AIE4_ASYNC_EVENT_CTX_ERR_NEW_PROCESS_FAILURE:
+ case AIE4_ASYNC_EVENT_CTX_ERR_UC_CRITICAL_ERROR:
+ case AIE4_ASYNC_EVENT_CTX_ERR_UC_COMPLETION_TIMEOUT:
+ return AMDXDNA_ERROR_NUM_KDS_EXEC;
+ default:
+ return AMDXDNA_ERROR_NUM_UNKNOWN;
+ }
+}
+
+/* Keep the error for the DRM_AMDXDNA_HW_LAST_ASYNC_ERR query. */
+static void aie4_record_async_error(struct amdxdna_dev_hdl *ndev,
+ enum amdxdna_error_num err_num,
+ u32 ctx_status, u32 ctx_id)
+{
+ struct amdxdna_async_error *rec = &ndev->last_async_err;
+
+ drm_WARN_ON(&ndev->aie.xdna->ddev, !mutex_is_locked(&ndev->aie.xdna->dev_lock));
+ rec->err_code = AMDXDNA_ERROR_ENCODE(err_num, AMDXDNA_ERROR_MODULE_AIE_CORE);
+ rec->ts_us = ktime_to_us(ktime_get_real());
+ rec->ex_err_code = AMDXDNA_EXTRA_ERR_CTX_ENCODE(ctx_status, ctx_id);
+}
+
+/* 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)
+{
+ struct amdxdna_dev *xdna = ndev->aie.xdna;
+ bool found = false;
+ struct amdxdna_client *client;
+ struct amdxdna_hwctx *hwctx;
+ unsigned long hwctx_id;
+ int idx;
+
+ drm_WARN_ON(&xdna->ddev, !mutex_is_locked(&xdna->dev_lock));
+
+ amdxdna_for_each_client(xdna, client) {
+ idx = srcu_read_lock(&client->hwctx_srcu);
+ amdxdna_for_each_hwctx(client, hwctx_id, hwctx) {
+ if (!hwctx->priv || hwctx->priv->hw_ctx_id != ctx_id ||
+ hwctx->priv->gen > gen)
+ continue;
+ found = true;
+ aie4_hwctx_reset(hwctx, ctx_err);
+ break;
+ }
+ srcu_read_unlock(&client->hwctx_srcu, idx);
+ if (found)
+ break;
+ }
+
+ if (!found)
+ XDNA_DBG(xdna, "Context %u was already reset or destroyed", ctx_id);
+}
+
static void aie4_log_health_report(struct amdxdna_dev *xdna,
struct aie4_msg_app_health_report *health)
{
@@ -147,6 +215,7 @@ static void aie4_async_ctx_error(struct aie4_async_event *e)
struct amdxdna_dev_hdl *ndev = e->ndev;
struct amdxdna_dev *xdna = ndev->aie.xdna;
u32 ctx_id = ctx_err->ctx_id;
+ u32 ctx_status = 0;
if (aie4_tdr_detected(ctx_err->error_type))
XDNA_ERR(xdna, "TDR on context %u: %s", ctx_id,
@@ -155,6 +224,13 @@ static void aie4_async_ctx_error(struct aie4_async_event *e)
XDNA_ERR(xdna, "Firmware reported context %u event: %s", ctx_id,
aie4_ctx_error_str(ctx_err->error_type));
aie4_log_health_report(xdna, health);
+
+ if (health->version)
+ ctx_status = FIELD_GET(AIE4_APP_HEALTH_CTX_STATUS, health->ctx_num_uc);
+ 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);
}
static void aie4_async_event_report(struct aie4_async_event *e)
diff --git a/drivers/accel/amdxdna/aie4_pci.c b/drivers/accel/amdxdna/aie4_pci.c
index 5910563b6c1d..a45c6bb21aaf 100644
--- a/drivers/accel/amdxdna/aie4_pci.c
+++ b/drivers/accel/amdxdna/aie4_pci.c
@@ -1039,6 +1039,25 @@ static int aie4_set_state(struct amdxdna_client *client,
return ret;
}
+/* Only the last context error is kept, so return one element. */
+static int aie4_get_array(struct amdxdna_client *client,
+ struct amdxdna_drm_get_array *args)
+{
+ struct amdxdna_dev_hdl *ndev = client->xdna->dev_handle;
+
+ if (args->param != DRM_AMDXDNA_HW_LAST_ASYNC_ERR)
+ return -EOPNOTSUPP;
+
+ args->num_element = 1;
+ args->element_size = min_t(u32, args->element_size,
+ sizeof(ndev->last_async_err));
+ if (copy_to_user(u64_to_user_ptr(args->buffer), &ndev->last_async_err,
+ args->element_size))
+ return -EFAULT;
+
+ return 0;
+}
+
static int aie4_alloc_work_buffer(struct amdxdna_dev_hdl *ndev)
{
struct amdxdna_dev *xdna = ndev->aie.xdna;
@@ -1281,6 +1300,10 @@ int aie4_hwctx_resume_all(struct amdxdna_dev_hdl *ndev)
amdxdna_for_each_client(xdna, client) {
idx = srcu_read_lock(&client->hwctx_srcu);
amdxdna_for_each_hwctx(client, hwctx_id, hwctx) {
+ /* Skip recreating a context marked dead by a failed reset. */
+ if (AIE4_HWCTX_HAS_RESET(hwctx) &&
+ !READ_ONCE(hwctx->priv->resetting))
+ continue;
ret = aie4_hwctx_create(hwctx);
if (ret)
goto error;
@@ -1503,6 +1526,7 @@ const struct amdxdna_dev_ops aie4_vf_ops = {
.cmd_wait = aie4_cmd_wait,
.get_aie_info = aie4_get_info,
.set_aie_state = aie4_set_state,
+ .get_array = aie4_get_array,
.resume = pm_ptr(aie4_vf_resume),
.suspend = pm_ptr(aie4_vf_suspend),
};
@@ -1520,6 +1544,7 @@ const struct amdxdna_dev_ops aie4_classic_ops = {
.cmd_wait = aie4_cmd_wait,
.get_aie_info = aie4_get_info,
.set_aie_state = aie4_set_state,
+ .get_array = aie4_get_array,
.resume = pm_ptr(aie4_classic_resume),
.suspend = pm_ptr(aie4_classic_suspend),
.runtime_resume = pm_ptr(aie4_classic_resume),
diff --git a/drivers/accel/amdxdna/aie4_pci.h b/drivers/accel/amdxdna/aie4_pci.h
index 9c37c78169bf..b24865c9ebc8 100644
--- a/drivers/accel/amdxdna/aie4_pci.h
+++ b/drivers/accel/amdxdna/aie4_pci.h
@@ -50,7 +50,16 @@ struct amdxdna_hwctx_priv {
struct cert_comp *cert_comp;
u32 hw_ctx_id;
- bool has_error;
+ /* Number of this firmware context; see amdxdna_dev_hdl.ctx_gen. */
+ u64 gen;
+ /* The firmware context was destroyed; the worker completes running jobs. */
+ bool has_reset;
+ /* A reset is in progress and creates the firmware context again. */
+ bool resetting;
+ /* The oldest drained job faulted; mark it timed out with reset_report. */
+ bool reset_fault;
+ /* Firmware report of the fault; zeroed without a firmware report. */
+ struct aie4_async_ctx_error reset_report;
/* Attached debug buffers, held until removal or fini. */
struct amdxdna_gem_obj *fw_buf_bo[AMDXDNA_FW_BUF_NUM];
/* Their configuration, sent again when the context is created. */
@@ -74,6 +83,11 @@ struct amdxdna_hwctx_priv {
struct workqueue_struct *job_work_q;
};
+/* A linked cert_comp means the context has a firmware context. */
+#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)
+
struct amdxdna_dev_priv {
const char *npufw_path;
const char *certfw_path;
@@ -129,6 +143,11 @@ struct amdxdna_dev_hdl {
/* Context switch hysteresis timeout in microseconds. */
u32 ctx_switch_hysteresis_us;
+ /* Bumped on each firmware context creation, under dev_lock. */
+ u64 ctx_gen;
+ /* Last context error reported by firmware, under dev_lock. */
+ struct amdxdna_async_error last_async_err;
+
struct amdxdna_drm_query_firmware_version cert_version;
};
@@ -176,8 +195,8 @@ void aie4_free_msg_buffer(struct amdxdna_dev *xdna, struct aie4_msg_buf *buf);
enum aie4_hwctx_flags {
AIE4_HWCTX_NORMAL = 0,
AIE4_HWCTX_GRACEFUL,
- AIE4_HWCTX_DISCONNECT, /* sets has_error, do not destroy context */
- AIE4_HWCTX_ERROR, /* sets has_error, destroy context */
+ AIE4_HWCTX_DISCONNECT, /* sets has_reset, do not destroy context */
+ AIE4_HWCTX_ERROR, /* sets has_reset, destroy context */
};
int aie4_hwctx_init(struct amdxdna_hwctx *hwctx);
@@ -192,6 +211,8 @@ void aie4_hwctx_resume_jobs(struct amdxdna_hwctx *hwctx);
int aie4_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value,
void *buf, u32 size);
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);
/* aie4_pci.c */
void aie4_hwctx_suspend_all(struct amdxdna_dev_hdl *ndev, int clean_jobs);
diff --git a/drivers/accel/amdxdna/amdxdna_ctx.h b/drivers/accel/amdxdna/amdxdna_ctx.h
index bf248d3f16ed..7a7406c6f706 100644
--- a/drivers/accel/amdxdna/amdxdna_ctx.h
+++ b/drivers/accel/amdxdna/amdxdna_ctx.h
@@ -89,6 +89,7 @@ struct amdxdna_cmd_preempt_data {
#define AMDXDNA_CMD_CTX_HEALTH_V1 1
#define AMDXDNA_CMD_CTX_HEALTH_AIE2 0
+#define AMDXDNA_CMD_CTX_HEALTH_AIE4 1
struct amdxdna_ctx_health {
u32 version;
u32 npu_gen;
diff --git a/drivers/accel/amdxdna/amdxdna_error.h b/drivers/accel/amdxdna/amdxdna_error.h
index 326d7153b17d..c384eec3377e 100644
--- a/drivers/accel/amdxdna/amdxdna_error.h
+++ b/drivers/accel/amdxdna/amdxdna_error.h
@@ -30,6 +30,8 @@ enum amdxdna_error_num {
AMDXDNA_ERROR_NUM_AIE_LOCK,
AMDXDNA_ERROR_NUM_AIE_DMA,
AMDXDNA_ERROR_NUM_AIE_MEM_PARITY,
+ AMDXDNA_ERROR_NUM_KDS_CU = 13,
+ AMDXDNA_ERROR_NUM_KDS_EXEC,
AMDXDNA_ERROR_NUM_UNKNOWN = 15,
};
@@ -56,6 +58,14 @@ enum amdxdna_error_module {
(FIELD_PREP(AMDXDNA_EXTRA_ERR_COL_MASK, col) | \
FIELD_PREP(AMDXDNA_EXTRA_ERR_ROW_MASK, row))
+/* Extra error code of a context error: firmware context status and ID. */
+#define AMDXDNA_EXTRA_ERR_CTX_ID_MASK GENMASK_U64(31, 0)
+#define AMDXDNA_EXTRA_ERR_CTX_STATUS_MASK GENMASK_U64(63, 32)
+
+#define AMDXDNA_EXTRA_ERR_CTX_ENCODE(status, ctx_id) \
+ (FIELD_PREP(AMDXDNA_EXTRA_ERR_CTX_ID_MASK, ctx_id) | \
+ FIELD_PREP(AMDXDNA_EXTRA_ERR_CTX_STATUS_MASK, status))
+
/*
* Maximum number of async event buffers. This value is used in two places:
* (1) to size the management mailbox channel, which must happen before
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH V2 3/5] accel/amdxdna: Check mailbox message overflow
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 ` David Zhang
2026-11-30 22:59 ` [PATCH V2 4/5] accel/amdxdna: Handle AIE4 firmware notifications David Zhang
2026-11-30 22:59 ` [PATCH V2 5/5] accel/amdxdna: Recover hung AIE4 contexts David Zhang
4 siblings, 0 replies; 6+ messages in thread
From: David Zhang @ 2026-11-30 22:59 UTC (permalink / raw)
To: quic_jhugo, karol.wachowski, max.zhen, lizhi.hou, ogabbay,
dri-devel, linux-kernel
Cc: David Zhang, sonal.santan, mario.limonciello
The message size is the firmware-supplied total_size plus the header
size, computed in a u32. A total_size near U32_MAX wraps the sum to a
small value that passes the ring buffer bounds checks, and the consumer
is then called with the unchecked total_size.
Reject the message when the addition overflows.
Signed-off-by: David Zhang <yidong.zhang@amd.com>
---
drivers/accel/amdxdna/amdxdna_mailbox.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/accel/amdxdna/amdxdna_mailbox.c b/drivers/accel/amdxdna/amdxdna_mailbox.c
index c4701b2703d7..967f6f1eb764 100644
--- a/drivers/accel/amdxdna/amdxdna_mailbox.c
+++ b/drivers/accel/amdxdna/amdxdna_mailbox.c
@@ -381,9 +381,9 @@ static int mailbox_get_msg(struct mailbox_channel *mb_chann)
MB_WARN_ONCE(mb_chann, "Invalid total size 0x%x", header.total_size);
return -EINVAL;
}
- msg_size = sizeof(header) + header.total_size;
- if (msg_size > ringbuf_size - head || msg_size > tail - head) {
+ if (check_add_overflow(sizeof(header), header.total_size, &msg_size) ||
+ msg_size > ringbuf_size - head || msg_size > tail - head) {
MB_WARN_ONCE(mb_chann, "Invalid message size %d, tail %d, head %d",
msg_size, tail, head);
return -EINVAL;
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH V2 4/5] accel/amdxdna: Handle AIE4 firmware notifications
2026-11-30 22:58 [PATCH V2 0/5] accel/amdxdna: Add AIE4 async events and TDR David Zhang
` (2 preceding siblings ...)
2026-11-30 22:59 ` [PATCH V2 3/5] accel/amdxdna: Check mailbox message overflow David Zhang
@ 2026-11-30 22:59 ` David Zhang
2026-11-30 22:59 ` [PATCH V2 5/5] accel/amdxdna: Recover hung AIE4 contexts David Zhang
4 siblings, 0 replies; 6+ messages in thread
From: David Zhang @ 2026-11-30 22:59 UTC (permalink / raw)
To: quic_jhugo, karol.wachowski, max.zhen, lizhi.hou, ogabbay,
dri-devel, linux-kernel
Cc: Nishad Saraf, sonal.santan, mario.limonciello, David Zhang
From: Nishad Saraf <nishad.saraf@amd.com>
Firmware sends notifications, such as context errors, as messages with
ID 0. Add an optional mailbox handler for ID 0 messages, and use it on
the AIE4 management channel to reset the context on a context error.
Signed-off-by: Nishad Saraf <nishad.saraf@amd.com>
Signed-off-by: David Zhang <yidong.zhang@amd.com>
---
drivers/accel/amdxdna/aie4_error.c | 104 ++++++++++++++++++++++++
drivers/accel/amdxdna/aie4_msg_priv.h | 31 +++++++
drivers/accel/amdxdna/aie4_pci.c | 2 +
drivers/accel/amdxdna/aie4_pci.h | 1 +
drivers/accel/amdxdna/amdxdna_mailbox.c | 18 ++++
drivers/accel/amdxdna/amdxdna_mailbox.h | 17 ++++
6 files changed, 173 insertions(+)
diff --git a/drivers/accel/amdxdna/aie4_error.c b/drivers/accel/amdxdna/aie4_error.c
index a56bf5891bf1..9ca99af3e2c1 100644
--- a/drivers/accel/amdxdna/aie4_error.c
+++ b/drivers/accel/amdxdna/aie4_error.c
@@ -407,3 +407,107 @@ void aie4_async_events_stop(struct amdxdna_dev_hdl *ndev)
if (events)
WRITE_ONCE(events->gen, events->gen + 1);
}
+
+/*
+ * struct aie4_async_note - context error notification from firmware
+ * @work: worker that resets the context
+ * @ndev: device handle
+ * @events: pool owning the workqueue
+ * @gen: @events gen when the notification arrived
+ * @ctx_gen: device context generation when the notification arrived
+ * @ctx_id: firmware context ID
+ */
+struct aie4_async_note {
+ struct work_struct work;
+ struct amdxdna_dev_hdl *ndev;
+ struct aie4_async_events *events;
+ u32 gen;
+ u64 ctx_gen;
+ u32 ctx_id;
+};
+
+static void aie4_async_note_worker(struct work_struct *work)
+{
+ struct aie4_async_note *note = container_of(work, struct aie4_async_note, work);
+ struct amdxdna_dev_hdl *ndev = note->ndev;
+ struct amdxdna_dev *xdna = ndev->aie.xdna;
+
+ mutex_lock(&xdna->dev_lock);
+ /* Skip a notification left over from a previous channel. */
+ if (note->gen != note->events->gen)
+ goto unlock;
+
+ /* 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);
+
+unlock:
+ mutex_unlock(&xdna->dev_lock);
+ kfree(note);
+}
+
+/*
+ * Called from the mailbox receive worker for each message firmware sends on
+ * its own, so defer the context reset to the async event workqueue.
+ */
+int aie4_async_note_cb(void *handle, u32 opcode, void __iomem *data, size_t size)
+{
+ struct amdxdna_dev_hdl *ndev = handle;
+ struct aie4_async_events *events = ndev->async_events;
+ struct amdxdna_dev *xdna = ndev->aie.xdna;
+ struct aie4_async_note *note;
+ u32 event_id, type, ctx_id;
+
+ if (opcode != AIE4_MSG_OP_ASYNC_EVENT_NOTE ||
+ size < sizeof(struct aie4_msg_async_note))
+ return -EINVAL;
+
+ event_id = readl(data + offsetof(struct aie4_msg_async_note, event_id));
+ switch (event_id) {
+ case AIE4_ASYNC_NOTE_CONTEXT_ERROR:
+ ctx_id = readl(data + offsetof(struct aie4_msg_async_note,
+ context_error.ctx_id));
+ XDNA_ERR(xdna, "Firmware reported context %u error", ctx_id);
+ break;
+ case AIE4_ASYNC_NOTE_REPORT_DROPPED:
+ type = readl(data + offsetof(struct aie4_msg_async_note,
+ report_dropped.async_event_type));
+ ctx_id = readl(data + offsetof(struct aie4_msg_async_note,
+ report_dropped.ctx_id));
+ /* An AIE error also sends CONTEXT_ERROR; reset only for this one. */
+ if (type != AIE4_ASYNC_EVENT_TYPE_CTX_ERROR) {
+ XDNA_ERR(xdna, "Firmware had no async event buffer, type %u context %u",
+ type, ctx_id);
+ return 0;
+ }
+ XDNA_ERR(xdna,
+ "TDR or uC error on context %u without a report",
+ ctx_id);
+ break;
+ case AIE4_ASYNC_NOTE_POWER_ERROR:
+ XDNA_ERR(xdna, "Firmware reported a power sequencing error");
+ return 0;
+ default:
+ XDNA_DBG(xdna, "Firmware notification %u ignored", event_id);
+ return 0;
+ }
+
+ /* The PF runs no contexts. */
+ if (!events)
+ return 0;
+
+ /* Called from the mailbox receive worker, which must not block. */
+ note = kzalloc_obj(*note, GFP_NOWAIT);
+ if (!note)
+ return -ENOMEM;
+
+ note->ndev = ndev;
+ note->events = events;
+ note->gen = READ_ONCE(events->gen);
+ /* Contexts created after this notification are not the one it reports. */
+ note->ctx_gen = READ_ONCE(ndev->ctx_gen);
+ note->ctx_id = ctx_id;
+ INIT_WORK(¬e->work, aie4_async_note_worker);
+ queue_work(events->wq, ¬e->work);
+ return 0;
+}
diff --git a/drivers/accel/amdxdna/aie4_msg_priv.h b/drivers/accel/amdxdna/aie4_msg_priv.h
index f8c94cf316fd..7a0f89150bff 100644
--- a/drivers/accel/amdxdna/aie4_msg_priv.h
+++ b/drivers/accel/amdxdna/aie4_msg_priv.h
@@ -16,6 +16,8 @@ enum aie4_msg_opcode {
AIE4_MSG_OP_ASYNC_EVENT_REPORT = 0x10004,
AIE4_MSG_OP_SET_RUNTIME_CONFIG = 0x10007,
AIE4_MSG_OP_QUERY_CERT_FIRMWARE_VERSION = 0x1000F,
+ /* Sent by firmware with message ID 0; no response is expected. */
+ AIE4_MSG_OP_ASYNC_EVENT_NOTE = 0x10012,
AIE4_MSG_OP_CREATE_VFS = 0x20001,
AIE4_MSG_OP_DESTROY_VFS = 0x20002,
@@ -402,4 +404,33 @@ struct aie4_async_ctx_error {
struct aie4_msg_app_health_report app_health_report;
};
+/* Events reported with AIE4_MSG_OP_ASYNC_EVENT_NOTE. */
+enum aie4_msg_async_note_id {
+ AIE4_ASYNC_NOTE_FW_LOG_WATERMARK,
+ AIE4_ASYNC_NOTE_FW_TRACE_WATERMARK,
+ AIE4_ASYNC_NOTE_DEBUG_MODE_ACTIVATED,
+ AIE4_ASYNC_NOTE_CONTEXT_ERROR,
+ AIE4_ASYNC_NOTE_REPORT_DROPPED,
+ AIE4_ASYNC_NOTE_POWER_ERROR,
+};
+
+/*
+ * AIE4_MSG_OP_ASYNC_EVENT_NOTE: an event small enough to fit in the message.
+ * @event_id selects the union member.
+ */
+struct aie4_msg_async_note {
+ __u32 event_id;
+ union {
+ __u32 reserved[4];
+ struct {
+ __u32 ctx_id;
+ } context_error;
+ /* A report that firmware had no async event buffer for. */
+ struct {
+ __u32 async_event_type;
+ __u32 ctx_id;
+ } report_dropped;
+ };
+} __packed;
+
#endif /* _AIE4_MSG_PRIV_H_ */
diff --git a/drivers/accel/amdxdna/aie4_pci.c b/drivers/accel/amdxdna/aie4_pci.c
index a45c6bb21aaf..78a739573ad3 100644
--- a/drivers/accel/amdxdna/aie4_pci.c
+++ b/drivers/accel/amdxdna/aie4_pci.c
@@ -250,6 +250,7 @@ static int aie4_mailbox_start(struct amdxdna_dev *xdna,
XDNA_ERR(xdna, "failed to alloc mailbox channel");
return -ENODEV;
}
+ xdna_mailbox_set_async_cb(ndev->aie.mgmt_chann, ndev, aie4_async_note_cb);
mgmt_mb_irq = pci_irq_vector(pdev, ndev->aie.mgmt_chan_idx);
if (mgmt_mb_irq < 0) {
@@ -491,6 +492,7 @@ static int aie4_vf_hw_start(struct amdxdna_dev_hdl *ndev)
/*
* A VF registers no async event buffers, so firmware keeps none of
* its memory across a VF reset or driver reload.
+ * Firmware reports VF context errors with notifications instead.
*/
ret = aie4_restore_power_mode(ndev);
diff --git a/drivers/accel/amdxdna/aie4_pci.h b/drivers/accel/amdxdna/aie4_pci.h
index b24865c9ebc8..6da2810c3fb4 100644
--- a/drivers/accel/amdxdna/aie4_pci.h
+++ b/drivers/accel/amdxdna/aie4_pci.h
@@ -161,6 +161,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);
+int aie4_async_note_cb(void *handle, u32 opcode, void __iomem *data, size_t size);
/* aie4_message.c */
int aie4_query_aie_metadata(struct amdxdna_dev_hdl *ndev,
diff --git a/drivers/accel/amdxdna/amdxdna_mailbox.c b/drivers/accel/amdxdna/amdxdna_mailbox.c
index 967f6f1eb764..2d6cce06ecf6 100644
--- a/drivers/accel/amdxdna/amdxdna_mailbox.c
+++ b/drivers/accel/amdxdna/amdxdna_mailbox.c
@@ -66,6 +66,8 @@ struct mailbox_channel {
struct work_struct rx_work;
u32 i2x_head;
bool bad_state;
+ void *async_handle;
+ xdna_mailbox_async_cb_t async_cb;
/* Pre-allocated message buffer pool */
void *msg_buf;
@@ -302,6 +304,15 @@ mailbox_get_resp(struct mailbox_channel *mb_chann, struct xdna_msg_header *heade
u32 slot;
msg_id = header->id;
+ if (!msg_id && mb_chann->async_cb) {
+ ret = mb_chann->async_cb(mb_chann->async_handle, header->opcode,
+ data, header->total_size);
+ if (ret)
+ MB_ERR(mb_chann, "Async handler for opcode 0x%x ret %d",
+ header->opcode, ret);
+ return 0;
+ }
+
if (!mailbox_validate_msgid(msg_id)) {
MB_ERR(mb_chann, "Bad message ID 0x%x", msg_id);
return -EINVAL;
@@ -549,6 +560,13 @@ struct mailbox_channel *xdna_mailbox_alloc_channel(struct mailbox *mb)
return NULL;
}
+void xdna_mailbox_set_async_cb(struct mailbox_channel *mb_chann, void *async_handle,
+ xdna_mailbox_async_cb_t async_cb)
+{
+ mb_chann->async_handle = async_handle;
+ mb_chann->async_cb = async_cb;
+}
+
void xdna_mailbox_free_channel(struct mailbox_channel *mb_chann)
{
if (!mb_chann)
diff --git a/drivers/accel/amdxdna/amdxdna_mailbox.h b/drivers/accel/amdxdna/amdxdna_mailbox.h
index 7f4a40b4230b..cf06bc8c6c09 100644
--- a/drivers/accel/amdxdna/amdxdna_mailbox.h
+++ b/drivers/accel/amdxdna/amdxdna_mailbox.h
@@ -9,6 +9,10 @@
struct mailbox;
struct mailbox_channel;
+/* Handler for a message firmware sends on its own, with message ID 0. */
+typedef int (*xdna_mailbox_async_cb_t)(void *handle, u32 opcode,
+ void __iomem *data, size_t size);
+
/*
* xdna_mailbox_msg - message struct
*
@@ -102,6 +106,19 @@ xdna_mailbox_start_channel(struct mailbox_channel *mb_chann,
u32 xdna_mailbox_intr_reg,
int mb_irq, u32 n_msg);
+/*
+ * xdna_mailbox_set_async_cb() -- set the handler for firmware notifications
+ *
+ * @mb_chann: the handle return from xdna_mailbox_alloc_channel()
+ * @async_handle: handle passed to @async_cb
+ * @async_cb: called from the receive worker for each message with ID 0;
+ * must not block. Its return value is only logged.
+ *
+ * Call before xdna_mailbox_start_channel().
+ */
+void xdna_mailbox_set_async_cb(struct mailbox_channel *mb_chann, void *async_handle,
+ xdna_mailbox_async_cb_t async_cb);
+
/*
* xdna_mailbox_free_channel() -- free mailbox channel
*
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH V2 5/5] accel/amdxdna: Recover hung AIE4 contexts
2026-11-30 22:58 [PATCH V2 0/5] accel/amdxdna: Add AIE4 async events and TDR David Zhang
` (3 preceding siblings ...)
2026-11-30 22:59 ` [PATCH V2 4/5] accel/amdxdna: Handle AIE4 firmware notifications David Zhang
@ 2026-11-30 22:59 ` David Zhang
4 siblings, 0 replies; 6+ messages in thread
From: David Zhang @ 2026-11-30 22:59 UTC (permalink / raw)
To: quic_jhugo, karol.wachowski, max.zhen, lizhi.hou, ogabbay,
dri-devel, linux-kernel
Cc: David Zhang, sonal.santan, mario.limonciello
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, ¬e->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
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-30 22:59 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH V2 5/5] accel/amdxdna: Recover hung AIE4 contexts David Zhang
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®