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 1/5] accel/amdxdna: Add AIE4 async event buffers
Date: Mon, 30 Nov 2026 14:59:00 -0800	[thread overview]
Message-ID: <20261130225904.1103363-2-yidong.zhang@amd.com> (raw)
In-Reply-To: <20261130225904.1103363-1-yidong.zhang@amd.com>

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


  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 ` David Zhang [this message]
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

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-2-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®