Skip to contentSkip to conversation

accel/amdxdna: Support firmware debug buffer

PATCH In review Lizhi Hou 3 participants 6 messages Oct 16, 2025 – Oct 20, 2025

Subsystems (2)
lore
Share & tools
lore

Lizhi Hou

author maintainer
#0 lore raw

PATCH 1/1 accel/amdxdna: Support firmware debug buffer

To collect firmware debug information, the userspace application allocates a AMDXDNA_BO_DEV buffer object through DRM_IOCTL_AMDXDNA_CREATE_BO. Then it associates the buffer with the hardware context through DRM_IOCTL_AMDXDNA_CONFIG_HWCTX which requests firmware to bind the buffer through a mailbox command. The firmware then writes the debug data into this buffer. The buffer can be mapped into userspace so that applications can retrieve and analyze the firmware debug information.

---
 drivers/accel/amdxdna/TODO              |   1 -
 drivers/accel/amdxdna/aie2_ctx.c        | 116 ++++++++++++++++++++++--
 drivers/accel/amdxdna/aie2_message.c    |  31 ++++++-
 drivers/accel/amdxdna/aie2_msg_priv.h   |  13 +++
 drivers/accel/amdxdna/aie2_pci.c        |   1 +
 drivers/accel/amdxdna/aie2_pci.h        |   3 +
 drivers/accel/amdxdna/amdxdna_ctx.c     |  39 +++++++-
 drivers/accel/amdxdna/amdxdna_ctx.h     |  16 +++-
 drivers/accel/amdxdna/amdxdna_gem.c     |   3 +
 drivers/accel/amdxdna/amdxdna_gem.h     |   6 ++
 drivers/accel/amdxdna/amdxdna_pci_drv.c |   3 +-
 drivers/accel/amdxdna/amdxdna_pci_drv.h |   1 +
 drivers/accel/amdxdna/npu1_regs.c       |   1 +
 drivers/accel/amdxdna/npu4_regs.c       |   1 +
 14 files changed, 221 insertions(+), 14 deletions(-)
drivers/accel/amdxdna/TODO +0 -1
index ad8ac6e315b6..0e4bbebeaedf 100644
@@ -1,2 +1 @@
 - Add debugfs support
-- Add debug BO support
drivers/accel/amdxdna/aie2_ctx.c +109 -7

156 lines not on this page · raw message

drivers/accel/amdxdna/aie2_message.c +30 -1
index 4660e8297ed8..0ec1dc6fe668 100644
@@ -749,7 +749,7 @@ int aie2_sync_bo(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job,
 	int ret = 0;
 
 	req.src_addr = 0;
-	req.dst_addr = abo->mem.dev_addr - hwctx->client->dev_heap->mem.dev_addr;
+	req.dst_addr = amdxdna_dev_bo_offset(abo);
 	req.size = abo->mem.size;
 
 	/* Device to Host */
@@ -773,3 +773,32 @@ int aie2_sync_bo(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job,
 
 	return 0;
 }
+
+int aie2_config_debug_bo(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job,
+			 int (*notify_cb)(void *, void __iomem *, size_t))
+{
+	struct mailbox_channel *chann = hwctx->priv->mbox_chann;
+	struct amdxdna_gem_obj *abo = to_xdna_obj(job->bos[0]);
+	struct amdxdna_dev *xdna = hwctx->client->xdna;
+	struct config_debug_bo_req req;
+	struct xdna_mailbox_msg msg;
+
+	if (job->drv_cmd->opcode == ATTACH_DEBUG_BO)
+		req.config = DEBUG_BO_REGISTER;
+	else
+		req.config = DEBUG_BO_UNREGISTER;
+
+	req.offset = amdxdna_dev_bo_offset(abo);
+	req.size = abo->mem.size;
+
+	XDNA_DBG(xdna, "offset 0x%llx size 0x%llx config %d",
+		 req.offset, req.size, req.config);
+
+	msg.handle = job;
+	msg.notify_cb = notify_cb;
+	msg.send_data = (u8 *)&req;
+	msg.send_size = sizeof(req);
+	msg.opcode = MSG_OP_CONFIG_DEBUG_BO;
+
+	return xdna_mailbox_send_msg(chann, &msg, TX_TIMEOUT);
+}
drivers/accel/amdxdna/aie2_msg_priv.h +13 -0
index 6df9065b13f6..6a5c70bff5e9 100644
@@ -18,6 +18,7 @@ enum aie2_msg_opcode {
 	MSG_OP_CONFIG_CU                   = 0x11,
 	MSG_OP_CHAIN_EXEC_BUFFER_CF        = 0x12,
 	MSG_OP_CHAIN_EXEC_DPU              = 0x13,
+	MSG_OP_CONFIG_DEBUG_BO             = 0x14,
 	MSG_OP_MAX_XRT_OPCODE,
 	MSG_OP_SUSPEND                     = 0x101,
 	MSG_OP_RESUME                      = 0x102,
@@ -365,4 +366,16 @@ struct sync_bo_req {
 struct sync_bo_resp {
 	enum aie2_msg_status	status;
 } __packed;
+
+struct config_debug_bo_req {
+	__u64	offset;
+	__u64	size;
+#define DEBUG_BO_UNREGISTER 0
+#define DEBUG_BO_REGISTER   1
+	__u32	config;
+} __packed;
+
+struct config_debug_bo_resp {
+	enum aie2_msg_status	status;
+} __packed;
 #endif /* _AIE2_MSG_PRIV_H_ */
drivers/accel/amdxdna/aie2_pci.c +1 -0
index cfca4e456b61..f48045318dc0 100644
@@ -1004,6 +1004,7 @@ const struct amdxdna_dev_ops aie2_ops = {
 	.hwctx_init = aie2_hwctx_init,
 	.hwctx_fini = aie2_hwctx_fini,
 	.hwctx_config = aie2_hwctx_config,
+	.hwctx_sync_debug_bo = aie2_hwctx_sync_debug_bo,
 	.cmd_submit = aie2_cmd_submit,
 	.hmm_invalidate = aie2_hmm_invalidate,
 	.get_array = aie2_get_array,
drivers/accel/amdxdna/aie2_pci.h +3 -0
index 34bc35479f42..243ac21d50c1 100644
@@ -287,11 +287,14 @@ int aie2_cmdlist_multi_execbuf(struct amdxdna_hwctx *hwctx,
 			       int (*notify_cb)(void *, void __iomem *, size_t));
 int aie2_sync_bo(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job,
 		 int (*notify_cb)(void *, void __iomem *, size_t));
+int aie2_config_debug_bo(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job,
+			 int (*notify_cb)(void *, void __iomem *, size_t));
 
 /* aie2_hwctx.c */
 int aie2_hwctx_init(struct amdxdna_hwctx *hwctx);
 void aie2_hwctx_fini(struct amdxdna_hwctx *hwctx);
 int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size);
+int aie2_hwctx_sync_debug_bo(struct amdxdna_hwctx *hwctx, u32 debug_bo_hdl);
 void aie2_hwctx_suspend(struct amdxdna_client *client);
 int aie2_hwctx_resume(struct amdxdna_client *client);
 int aie2_cmd_submit(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, u64 *seq);
drivers/accel/amdxdna/amdxdna_ctx.c +36 -3
index 868ca369e0a0..d18182c59668 100644
@@ -328,6 +328,38 @@ int amdxdna_drm_config_hwctx_ioctl(struct drm_device *dev, void *data, struct dr
 	return ret;
 }
 
+int amdxdna_hwctx_sync_debug_bo(struct amdxdna_client *client, u32 debug_bo_hdl)
+{
+	struct amdxdna_dev *xdna = client->xdna;
+	struct amdxdna_hwctx *hwctx;
+	struct amdxdna_gem_obj *abo;
+	struct drm_gem_object *gobj;
+	int ret, idx;
+
+	if (!xdna->dev_info->ops->hwctx_sync_debug_bo)
+		return -EOPNOTSUPP;
+
+	gobj = drm_gem_object_lookup(client->filp, debug_bo_hdl);
+	if (!gobj)
+		return -EINVAL;
+
+	abo = to_xdna_obj(gobj);
+	guard(mutex)(&xdna->dev_lock);
+	idx = srcu_read_lock(&client->hwctx_srcu);
+	hwctx = xa_load(&client->hwctx_xa, abo->assigned_hwctx);
+	if (!hwctx) {
+		ret = -EINVAL;
+		goto unlock_srcu;
+	}
+
+	ret = xdna->dev_info->ops->hwctx_sync_debug_bo(hwctx, debug_bo_hdl);
+
+unlock_srcu:
+	srcu_read_unlock(&client->hwctx_srcu, idx);
+	drm_gem_object_put(gobj);
+	return ret;
+}
+
 static void
 amdxdna_arg_bos_put(struct amdxdna_sched_job *job)
 {
@@ -393,6 +425,7 @@ void amdxdna_sched_job_cleanup(struct amdxdna_sched_job *job)
 }
 
 int amdxdna_cmd_submit(struct amdxdna_client *client,
+		       struct amdxdna_drv_cmd *drv_cmd,
 		       u32 cmd_bo_hdl, u32 *arg_bo_hdls, u32 arg_bo_cnt,
 		       u32 hwctx_hdl, u64 *seq)
 {
@@ -406,6 +439,8 @@ int amdxdna_cmd_submit(struct amdxdna_client *client,
 	if (!job)
 		return -ENOMEM;
 
+	job->drv_cmd = drv_cmd;
+
 	if (cmd_bo_hdl != AMDXDNA_INVALID_BO_HANDLE) {
 		job->cmd_bo = amdxdna_gem_get_obj(client, cmd_bo_hdl, AMDXDNA_BO_CMD);
 		if (!job->cmd_bo) {
@@ -413,8 +448,6 @@ int amdxdna_cmd_submit(struct amdxdna_client *client,
 			ret = -EINVAL;
 			goto free_job;
 		}
-	} else {
-		job->cmd_bo = NULL;
 	}
 
 	ret = amdxdna_arg_bos_lookup(client, job, arg_bo_hdls, arg_bo_cnt);
@@ -508,7 +541,7 @@ static int amdxdna_drm_submit_execbuf(struct amdxdna_client *client,
 		}
 	}
 
-	ret = amdxdna_cmd_submit(client, cmd_bo_hdl, arg_bo_hdls,
+	ret = amdxdna_cmd_submit(client, NULL, cmd_bo_hdl, arg_bo_hdls,
 				 args->arg_count, args->hwctx, &args->seq);
 	if (ret)
 		XDNA_DBG(xdna, "Submit cmds failed, ret %d", ret);
drivers/accel/amdxdna/amdxdna_ctx.h +15 -1
index 7cd7a55936f0..cbe60efbe60b 100644
@@ -95,6 +95,17 @@ struct amdxdna_hwctx {
 #define drm_job_to_xdna_job(j) \
 	container_of(j, struct amdxdna_sched_job, base)
 
+enum amdxdna_job_opcode {
+	SYNC_DEBUG_BO,
+	ATTACH_DEBUG_BO,
+	DETACH_DEBUG_BO,
+};
+
+struct amdxdna_drv_cmd {
+	enum amdxdna_job_opcode	opcode;
+	u32			result;
+};
+
 struct amdxdna_sched_job {
 	struct drm_sched_job	base;
 	struct kref		refcnt;
@@ -106,6 +117,7 @@ struct amdxdna_sched_job {
 	struct dma_fence	*out_fence;
 	bool			job_done;
 	u64			seq;
+	struct amdxdna_drv_cmd	*drv_cmd;
 	struct amdxdna_gem_obj	*cmd_bo;
 	size_t			bo_cnt;
 	struct drm_gem_object	*bos[] __counted_by(bo_cnt);
@@ -143,9 +155,11 @@ void amdxdna_sched_job_cleanup(struct amdxdna_sched_job *job);
 void amdxdna_hwctx_remove_all(struct amdxdna_client *client);
 int amdxdna_hwctx_walk(struct amdxdna_client *client, void *arg,
 		       int (*walk)(struct amdxdna_hwctx *hwctx, void *arg));
+int amdxdna_hwctx_sync_debug_bo(struct amdxdna_client *client, u32 debug_bo_hdl);
 
 int amdxdna_cmd_submit(struct amdxdna_client *client,
-		       u32 cmd_bo_hdls, u32 *arg_bo_hdls, u32 arg_bo_cnt,
+		       struct amdxdna_drv_cmd *drv_cmd, u32 cmd_bo_hdls,
+		       u32 *arg_bo_hdls, u32 arg_bo_cnt,
 		       u32 hwctx_hdl, u64 *seq);
 
 int amdxdna_cmd_wait(struct amdxdna_client *client, u32 hwctx_hdl,
drivers/accel/amdxdna/amdxdna_gem.c +3 -0
index 7f91863c3f24..61e0136c21a8 100644
@@ -962,6 +962,9 @@ int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev,
 	XDNA_DBG(xdna, "Sync bo %d offset 0x%llx, size 0x%llx\n",
 		 args->handle, args->offset, args->size);
 
+	if (args->direction == SYNC_DIRECT_FROM_DEVICE)
+		ret = amdxdna_hwctx_sync_debug_bo(abo->client, args->handle);
+
 put_obj:
 	drm_gem_object_put(gobj);
 	return ret;
drivers/accel/amdxdna/amdxdna_gem.h +6 -0
index ae29db94a9d3..f79fc7f3c93b 100644
@@ -7,6 +7,7 @@
 #define _AMDXDNA_GEM_H_
 
 #include <linux/hmm.h>
+#include "amdxdna_pci_drv.h"
 
 struct amdxdna_umap {
 	struct vm_area_struct		*vma;
@@ -62,6 +63,11 @@ static inline void amdxdna_gem_put_obj(struct amdxdna_gem_obj *abo)
 	drm_gem_object_put(to_gobj(abo));
 }
 
+static inline u64 amdxdna_dev_bo_offset(struct amdxdna_gem_obj *abo)
+{
+	return abo->mem.dev_addr - abo->client->dev_heap->mem.dev_addr;
+}
+
 void amdxdna_umap_put(struct amdxdna_umap *mapp);
 
 struct drm_gem_object *
drivers/accel/amdxdna/amdxdna_pci_drv.c +2 -1
index 696fdac8ad3c..3599e713bfcb 100644
@@ -28,9 +28,10 @@ MODULE_FIRMWARE("amdnpu/17f0_20/npu.sbin");
  * 0.0: Initial version
  * 0.1: Support getting all hardware contexts by DRM_IOCTL_AMDXDNA_GET_ARRAY
  * 0.2: Support getting last error hardware error
+ * 0.3: Support firmware debug buffer
  */
 #define AMDXDNA_DRIVER_MAJOR		0
-#define AMDXDNA_DRIVER_MINOR		2
+#define AMDXDNA_DRIVER_MINOR		3
 
 /*
  * Bind the driver base on (vendor_id, device_id) pair and later use the
drivers/accel/amdxdna/amdxdna_pci_drv.h +1 -0
index 626beebf730e..c99477f5e454 100644
@@ -55,6 +55,7 @@ struct amdxdna_dev_ops {
 	int (*hwctx_init)(struct amdxdna_hwctx *hwctx);
 	void (*hwctx_fini)(struct amdxdna_hwctx *hwctx);
 	int (*hwctx_config)(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size);
+	int (*hwctx_sync_debug_bo)(struct amdxdna_hwctx *hwctx, u32 debug_bo_hdl);
 	void (*hmm_invalidate)(struct amdxdna_gem_obj *abo, unsigned long cur_seq);
 	int (*cmd_submit)(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, u64 *seq);
 	int (*get_aie_info)(struct amdxdna_client *client, struct amdxdna_drm_get_info *args);
drivers/accel/amdxdna/npu1_regs.c +1 -0
index e4f6dac7d00f..10124cccb102 100644
@@ -46,6 +46,7 @@
 
 const struct rt_config npu1_default_rt_cfg[] = {
 	{ 2, 1, AIE2_RT_CFG_INIT }, /* PDI APP LOAD MODE */
+	{ 4, 1, AIE2_RT_CFG_INIT }, /* Debug BO */
 	{ 1, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */
 	{ 0 },
 };
drivers/accel/amdxdna/npu4_regs.c +1 -0
index 9f2e33182ec6..e1da882420ec 100644
@@ -63,6 +63,7 @@
 
 const struct rt_config npu4_default_rt_cfg[] = {
 	{ 5, 1, AIE2_RT_CFG_INIT }, /* PDI APP LOAD MODE */
+	{ 10, 1, AIE2_RT_CFG_INIT }, /* DEBUG BUF */
 	{ 1, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */
 	{ 2, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */
 	{ 3, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */
Signature · 1 line
2.34.1
#1 lore raw
Quoted 7 lines · Lizhi Hou

To collect firmware debug information, the userspace application allocates a AMDXDNA_BO_DEV buffer object through DRM_IOCTL_AMDXDNA_CREATE_BO. Then it associates the buffer with the hardware context through DRM_IOCTL_AMDXDNA_CONFIG_HWCTX which requests firmware to bind the buffer through a mailbox command. The firmware then writes the debug data into this buffer. The buffer can be mapped into userspace so that applications can retrieve and analyze the firmware debug information.

Let me ask a high level question. Do we really want all userspace to always have access to this debug information? Could this leak information between processes potentially?

Two ideas:

1) I wonder if this is better placed in debugfs files.
2) If this architecture does make sense - what are your thoughts on tying the ability to use it to kernel lockdown?

Quoted 508 lines
---
  drivers/accel/amdxdna/TODO              |   1 -
  drivers/accel/amdxdna/aie2_ctx.c        | 116 ++++++++++++++++++++++--
  drivers/accel/amdxdna/aie2_message.c    |  31 ++++++-
  drivers/accel/amdxdna/aie2_msg_priv.h   |  13 +++
  drivers/accel/amdxdna/aie2_pci.c        |   1 +
  drivers/accel/amdxdna/aie2_pci.h        |   3 +
  drivers/accel/amdxdna/amdxdna_ctx.c     |  39 +++++++-
  drivers/accel/amdxdna/amdxdna_ctx.h     |  16 +++-
  drivers/accel/amdxdna/amdxdna_gem.c     |   3 +
  drivers/accel/amdxdna/amdxdna_gem.h     |   6 ++
  drivers/accel/amdxdna/amdxdna_pci_drv.c |   3 +-
  drivers/accel/amdxdna/amdxdna_pci_drv.h |   1 +
  drivers/accel/amdxdna/npu1_regs.c       |   1 +
  drivers/accel/amdxdna/npu4_regs.c       |   1 +
  14 files changed, 221 insertions(+), 14 deletions(-)
drivers/accel/amdxdna/TODO +0 -1
index ad8ac6e315b6..0e4bbebeaedf 100644
@@ -1,2 +1 @@
  - Add debugfs support
-- Add debug BO support
drivers/accel/amdxdna/aie2_ctx.c +109 -7
index ab4d66f1325d..63450b7773ac 100644
@@ -226,11 +226,10 @@ aie2_sched_resp_handler(void *handle, void __iomem *data, size_t size)
  }
 
  static int
-aie2_sched_nocmd_resp_handler(void *handle, void __iomem *data, size_t size)
+aie2_sched_drvcmd_resp_handler(void *handle, void __iomem *data, size_t size)
  {
  	struct amdxdna_sched_job *job = handle;
  	int ret = 0;
-	u32 status;
 
  	if (unlikely(!data))
  		goto out;
@@ -240,8 +239,7 @@ aie2_sched_nocmd_resp_handler(void *handle, void __iomem *data, size_t size)
  		goto out;
  	}
 
-	status = readl(data);
-	XDNA_DBG(job->hwctx->client->xdna, "Resp status 0x%x", status);
+	job->drv_cmd->result = readl(data);
 
  out:
  	aie2_sched_notify(job);
@@ -314,8 +312,18 @@ aie2_sched_job_run(struct drm_sched_job *sched_job)
  	kref_get(&job->refcnt);
  	fence = dma_fence_get(job->fence);
 
-	if (unlikely(!cmd_abo)) {
-		ret = aie2_sync_bo(hwctx, job, aie2_sched_nocmd_resp_handler);
+	if (job->drv_cmd) {
+		switch (job->drv_cmd->opcode) {
+		case SYNC_DEBUG_BO:
+			ret = aie2_sync_bo(hwctx, job, aie2_sched_drvcmd_resp_handler);
+			break;
+		case ATTACH_DEBUG_BO:
+			ret = aie2_config_debug_bo(hwctx, job, aie2_sched_drvcmd_resp_handler);
+			break;
+		default:
+			ret = -EINVAL;
+			break;
+		}
  		goto out;
  	}
 
@@ -766,6 +774,74 @@ static int aie2_hwctx_cu_config(struct amdxdna_hwctx *hwctx, void *buf, u32 size
  	return ret;
  }
 
+static void aie2_cmd_wait(struct amdxdna_hwctx *hwctx, u64 seq)
+{
+	struct dma_fence *out_fence = aie2_cmd_get_out_fence(hwctx, seq);
+
+	if (!out_fence) {
+		XDNA_ERR(hwctx->client->xdna, "Failed to get fence");
+		return;
+	}
+
+	dma_fence_wait_timeout(out_fence, false, MAX_SCHEDULE_TIMEOUT);
+	dma_fence_put(out_fence);
+}
+
+static int aie2_hwctx_cfg_debug_bo(struct amdxdna_hwctx *hwctx, u32 bo_hdl,
+				   bool attach)
+{
+	struct amdxdna_client *client = hwctx->client;
+	struct amdxdna_dev *xdna = client->xdna;
+	struct amdxdna_drv_cmd cmd = { 0 };
+	struct amdxdna_gem_obj *abo;
+	u64 seq;
+	int ret;
+
+	abo = amdxdna_gem_get_obj(client, bo_hdl, AMDXDNA_BO_DEV);
+	if (!abo) {
+		XDNA_ERR(xdna, "Get bo %d failed", bo_hdl);
+		return -EINVAL;
+	}
+
+	if (attach) {
+		if (abo->assigned_hwctx != AMDXDNA_INVALID_CTX_HANDLE) {
+			ret = -EBUSY;
+			goto put_obj;
+		}
+		cmd.opcode = ATTACH_DEBUG_BO;
+	} else {
+		if (abo->assigned_hwctx != hwctx->id) {
+			ret = -EINVAL;
+			goto put_obj;
+		}
+		cmd.opcode = DETACH_DEBUG_BO;
+	}
+
+	ret = amdxdna_cmd_submit(client, &cmd, AMDXDNA_INVALID_BO_HANDLE,
+				 &bo_hdl, 1, hwctx->id, &seq);
+	if (ret) {
+		XDNA_ERR(xdna, "Submit command failed");
+		goto put_obj;
+	}
+
+	aie2_cmd_wait(hwctx, seq);
+	if (cmd.result) {
+		XDNA_ERR(xdna, "Response failure 0x%x", cmd.result);
+		goto put_obj;
+	}
+
+	if (attach)
+		abo->assigned_hwctx = hwctx->id;
+	else
+		abo->assigned_hwctx = AMDXDNA_INVALID_CTX_HANDLE;
+
+	XDNA_DBG(xdna, "Config debug BO %d to %s", bo_hdl, hwctx->name);
+
+put_obj:
+	amdxdna_gem_put_obj(abo);
+	return ret;
+}
+
  int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size)
  {
  	struct amdxdna_dev *xdna = hwctx->client->xdna;
@@ -775,14 +851,40 @@ int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *bu
  	case DRM_AMDXDNA_HWCTX_CONFIG_CU:
  		return aie2_hwctx_cu_config(hwctx, buf, size);
  	case DRM_AMDXDNA_HWCTX_ASSIGN_DBG_BUF:
+		return aie2_hwctx_cfg_debug_bo(hwctx, (u32)value, true);
  	case DRM_AMDXDNA_HWCTX_REMOVE_DBG_BUF:
-		return -EOPNOTSUPP;
+		return aie2_hwctx_cfg_debug_bo(hwctx, (u32)value, false);
  	default:
  		XDNA_DBG(xdna, "Not supported type %d", type);
  		return -EOPNOTSUPP;
  	}
  }
 
+int aie2_hwctx_sync_debug_bo(struct amdxdna_hwctx *hwctx, u32 debug_bo_hdl)
+{
+	struct amdxdna_client *client = hwctx->client;
+	struct amdxdna_dev *xdna = client->xdna;
+	struct amdxdna_drv_cmd cmd = { 0 };
+	u64 seq;
+	int ret;
+
+	cmd.opcode = SYNC_DEBUG_BO;
+	ret = amdxdna_cmd_submit(client, &cmd, AMDXDNA_INVALID_BO_HANDLE,
+				 &debug_bo_hdl, 1, hwctx->id, &seq);
+	if (ret) {
+		XDNA_ERR(xdna, "Submit command failed");
+		return ret;
+	}
+
+	aie2_cmd_wait(hwctx, seq);
+	if (cmd.result) {
+		XDNA_ERR(xdna, "Response failure 0x%x", cmd.result);
+		return ret;
+	}
+
+	return 0;
+}
+
  static int aie2_populate_range(struct amdxdna_gem_obj *abo)
  {
  	struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev);
drivers/accel/amdxdna/aie2_message.c +30 -1
index 4660e8297ed8..0ec1dc6fe668 100644
@@ -749,7 +749,7 @@ int aie2_sync_bo(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job,
  	int ret = 0;
 
  	req.src_addr = 0;
-	req.dst_addr = abo->mem.dev_addr - hwctx->client->dev_heap->mem.dev_addr;
+	req.dst_addr = amdxdna_dev_bo_offset(abo);
  	req.size = abo->mem.size;
 
  	/* Device to Host */
@@ -773,3 +773,32 @@ int aie2_sync_bo(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job,
 
  	return 0;
  }
+
+int aie2_config_debug_bo(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job,
+			 int (*notify_cb)(void *, void __iomem *, size_t))
+{
+	struct mailbox_channel *chann = hwctx->priv->mbox_chann;
+	struct amdxdna_gem_obj *abo = to_xdna_obj(job->bos[0]);
+	struct amdxdna_dev *xdna = hwctx->client->xdna;
+	struct config_debug_bo_req req;
+	struct xdna_mailbox_msg msg;
+
+	if (job->drv_cmd->opcode == ATTACH_DEBUG_BO)
+		req.config = DEBUG_BO_REGISTER;
+	else
+		req.config = DEBUG_BO_UNREGISTER;
+
+	req.offset = amdxdna_dev_bo_offset(abo);
+	req.size = abo->mem.size;
+
+	XDNA_DBG(xdna, "offset 0x%llx size 0x%llx config %d",
+		 req.offset, req.size, req.config);
+
+	msg.handle = job;
+	msg.notify_cb = notify_cb;
+	msg.send_data = (u8 *)&req;
+	msg.send_size = sizeof(req);
+	msg.opcode = MSG_OP_CONFIG_DEBUG_BO;
+
+	return xdna_mailbox_send_msg(chann, &msg, TX_TIMEOUT);
+}
drivers/accel/amdxdna/aie2_msg_priv.h +13 -0
index 6df9065b13f6..6a5c70bff5e9 100644
@@ -18,6 +18,7 @@ enum aie2_msg_opcode {
  	MSG_OP_CONFIG_CU                   = 0x11,
  	MSG_OP_CHAIN_EXEC_BUFFER_CF        = 0x12,
  	MSG_OP_CHAIN_EXEC_DPU              = 0x13,
+	MSG_OP_CONFIG_DEBUG_BO             = 0x14,
  	MSG_OP_MAX_XRT_OPCODE,
  	MSG_OP_SUSPEND                     = 0x101,
  	MSG_OP_RESUME                      = 0x102,
@@ -365,4 +366,16 @@ struct sync_bo_req {
  struct sync_bo_resp {
  	enum aie2_msg_status	status;
  } __packed;
+
+struct config_debug_bo_req {
+	__u64	offset;
+	__u64	size;
+#define DEBUG_BO_UNREGISTER 0
+#define DEBUG_BO_REGISTER   1
+	__u32	config;
+} __packed;
+
+struct config_debug_bo_resp {
+	enum aie2_msg_status	status;
+} __packed;
  #endif /* _AIE2_MSG_PRIV_H_ */
drivers/accel/amdxdna/aie2_pci.c +1 -0
index cfca4e456b61..f48045318dc0 100644
@@ -1004,6 +1004,7 @@ const struct amdxdna_dev_ops aie2_ops = {
  	.hwctx_init = aie2_hwctx_init,
  	.hwctx_fini = aie2_hwctx_fini,
  	.hwctx_config = aie2_hwctx_config,
+	.hwctx_sync_debug_bo = aie2_hwctx_sync_debug_bo,
  	.cmd_submit = aie2_cmd_submit,
  	.hmm_invalidate = aie2_hmm_invalidate,
  	.get_array = aie2_get_array,
drivers/accel/amdxdna/aie2_pci.h +3 -0
index 34bc35479f42..243ac21d50c1 100644
@@ -287,11 +287,14 @@ int aie2_cmdlist_multi_execbuf(struct amdxdna_hwctx *hwctx,
  			       int (*notify_cb)(void *, void __iomem *, size_t));
  int aie2_sync_bo(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job,
  		 int (*notify_cb)(void *, void __iomem *, size_t));
+int aie2_config_debug_bo(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job,
+			 int (*notify_cb)(void *, void __iomem *, size_t));
 
  /* aie2_hwctx.c */
  int aie2_hwctx_init(struct amdxdna_hwctx *hwctx);
  void aie2_hwctx_fini(struct amdxdna_hwctx *hwctx);
  int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size);
+int aie2_hwctx_sync_debug_bo(struct amdxdna_hwctx *hwctx, u32 debug_bo_hdl);
  void aie2_hwctx_suspend(struct amdxdna_client *client);
  int aie2_hwctx_resume(struct amdxdna_client *client);
  int aie2_cmd_submit(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, u64 *seq);
drivers/accel/amdxdna/amdxdna_ctx.c +36 -3
index 868ca369e0a0..d18182c59668 100644
@@ -328,6 +328,38 @@ int amdxdna_drm_config_hwctx_ioctl(struct drm_device *dev, void *data, struct dr
  	return ret;
  }
 
+int amdxdna_hwctx_sync_debug_bo(struct amdxdna_client *client, u32 debug_bo_hdl)
+{
+	struct amdxdna_dev *xdna = client->xdna;
+	struct amdxdna_hwctx *hwctx;
+	struct amdxdna_gem_obj *abo;
+	struct drm_gem_object *gobj;
+	int ret, idx;
+
+	if (!xdna->dev_info->ops->hwctx_sync_debug_bo)
+		return -EOPNOTSUPP;
+
+	gobj = drm_gem_object_lookup(client->filp, debug_bo_hdl);
+	if (!gobj)
+		return -EINVAL;
+
+	abo = to_xdna_obj(gobj);
+	guard(mutex)(&xdna->dev_lock);
+	idx = srcu_read_lock(&client->hwctx_srcu);
+	hwctx = xa_load(&client->hwctx_xa, abo->assigned_hwctx);
+	if (!hwctx) {
+		ret = -EINVAL;
+		goto unlock_srcu;
+	}
+
+	ret = xdna->dev_info->ops->hwctx_sync_debug_bo(hwctx, debug_bo_hdl);
+
+unlock_srcu:
+	srcu_read_unlock(&client->hwctx_srcu, idx);
+	drm_gem_object_put(gobj);
+	return ret;
+}
+
  static void
  amdxdna_arg_bos_put(struct amdxdna_sched_job *job)
  {
@@ -393,6 +425,7 @@ void amdxdna_sched_job_cleanup(struct amdxdna_sched_job *job)
  }
 
  int amdxdna_cmd_submit(struct amdxdna_client *client,
+		       struct amdxdna_drv_cmd *drv_cmd,
  		       u32 cmd_bo_hdl, u32 *arg_bo_hdls, u32 arg_bo_cnt,
  		       u32 hwctx_hdl, u64 *seq)
  {
@@ -406,6 +439,8 @@ int amdxdna_cmd_submit(struct amdxdna_client *client,
  	if (!job)
  		return -ENOMEM;
 
+	job->drv_cmd = drv_cmd;
+
  	if (cmd_bo_hdl != AMDXDNA_INVALID_BO_HANDLE) {
  		job->cmd_bo = amdxdna_gem_get_obj(client, cmd_bo_hdl, AMDXDNA_BO_CMD);
  		if (!job->cmd_bo) {
@@ -413,8 +448,6 @@ int amdxdna_cmd_submit(struct amdxdna_client *client,
  			ret = -EINVAL;
  			goto free_job;
  		}
-	} else {
-		job->cmd_bo = NULL;
  	}
 
  	ret = amdxdna_arg_bos_lookup(client, job, arg_bo_hdls, arg_bo_cnt);
@@ -508,7 +541,7 @@ static int amdxdna_drm_submit_execbuf(struct amdxdna_client *client,
  		}
  	}
 
-	ret = amdxdna_cmd_submit(client, cmd_bo_hdl, arg_bo_hdls,
+	ret = amdxdna_cmd_submit(client, NULL, cmd_bo_hdl, arg_bo_hdls,
  				 args->arg_count, args->hwctx, &args->seq);
  	if (ret)
  		XDNA_DBG(xdna, "Submit cmds failed, ret %d", ret);
drivers/accel/amdxdna/amdxdna_ctx.h +15 -1
index 7cd7a55936f0..cbe60efbe60b 100644
@@ -95,6 +95,17 @@ struct amdxdna_hwctx {
  #define drm_job_to_xdna_job(j) \
  	container_of(j, struct amdxdna_sched_job, base)
 
+enum amdxdna_job_opcode {
+	SYNC_DEBUG_BO,
+	ATTACH_DEBUG_BO,
+	DETACH_DEBUG_BO,
+};
+
+struct amdxdna_drv_cmd {
+	enum amdxdna_job_opcode	opcode;
+	u32			result;
+};
+
  struct amdxdna_sched_job {
  	struct drm_sched_job	base;
  	struct kref		refcnt;
@@ -106,6 +117,7 @@ struct amdxdna_sched_job {
  	struct dma_fence	*out_fence;
  	bool			job_done;
  	u64			seq;
+	struct amdxdna_drv_cmd	*drv_cmd;
  	struct amdxdna_gem_obj	*cmd_bo;
  	size_t			bo_cnt;
  	struct drm_gem_object	*bos[] __counted_by(bo_cnt);
@@ -143,9 +155,11 @@ void amdxdna_sched_job_cleanup(struct amdxdna_sched_job *job);
  void amdxdna_hwctx_remove_all(struct amdxdna_client *client);
  int amdxdna_hwctx_walk(struct amdxdna_client *client, void *arg,
  		       int (*walk)(struct amdxdna_hwctx *hwctx, void *arg));
+int amdxdna_hwctx_sync_debug_bo(struct amdxdna_client *client, u32 debug_bo_hdl);
 
  int amdxdna_cmd_submit(struct amdxdna_client *client,
-		       u32 cmd_bo_hdls, u32 *arg_bo_hdls, u32 arg_bo_cnt,
+		       struct amdxdna_drv_cmd *drv_cmd, u32 cmd_bo_hdls,
+		       u32 *arg_bo_hdls, u32 arg_bo_cnt,
  		       u32 hwctx_hdl, u64 *seq);
 
  int amdxdna_cmd_wait(struct amdxdna_client *client, u32 hwctx_hdl,
drivers/accel/amdxdna/amdxdna_gem.c +3 -0
index 7f91863c3f24..61e0136c21a8 100644
@@ -962,6 +962,9 @@ int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev,
  	XDNA_DBG(xdna, "Sync bo %d offset 0x%llx, size 0x%llx\n",
  		 args->handle, args->offset, args->size);
 
+	if (args->direction == SYNC_DIRECT_FROM_DEVICE)
+		ret = amdxdna_hwctx_sync_debug_bo(abo->client, args->handle);
+
  put_obj:
  	drm_gem_object_put(gobj);
  	return ret;
drivers/accel/amdxdna/amdxdna_gem.h +6 -0
index ae29db94a9d3..f79fc7f3c93b 100644
@@ -7,6 +7,7 @@
  #define _AMDXDNA_GEM_H_
 
  #include <linux/hmm.h>
+#include "amdxdna_pci_drv.h"
 
  struct amdxdna_umap {
  	struct vm_area_struct		*vma;
@@ -62,6 +63,11 @@ static inline void amdxdna_gem_put_obj(struct amdxdna_gem_obj *abo)
  	drm_gem_object_put(to_gobj(abo));
  }
 
+static inline u64 amdxdna_dev_bo_offset(struct amdxdna_gem_obj *abo)
+{
+	return abo->mem.dev_addr - abo->client->dev_heap->mem.dev_addr;
+}
+
  void amdxdna_umap_put(struct amdxdna_umap *mapp);
 
  struct drm_gem_object *
drivers/accel/amdxdna/amdxdna_pci_drv.c +2 -1
index 696fdac8ad3c..3599e713bfcb 100644
@@ -28,9 +28,10 @@ MODULE_FIRMWARE("amdnpu/17f0_20/npu.sbin");
   * 0.0: Initial version
   * 0.1: Support getting all hardware contexts by DRM_IOCTL_AMDXDNA_GET_ARRAY
   * 0.2: Support getting last error hardware error
+ * 0.3: Support firmware debug buffer
   */
  #define AMDXDNA_DRIVER_MAJOR		0
-#define AMDXDNA_DRIVER_MINOR		2
+#define AMDXDNA_DRIVER_MINOR		3
 
  /*
   * Bind the driver base on (vendor_id, device_id) pair and later use the
drivers/accel/amdxdna/amdxdna_pci_drv.h +1 -0
index 626beebf730e..c99477f5e454 100644
@@ -55,6 +55,7 @@ struct amdxdna_dev_ops {
  	int (*hwctx_init)(struct amdxdna_hwctx *hwctx);
  	void (*hwctx_fini)(struct amdxdna_hwctx *hwctx);
  	int (*hwctx_config)(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size);
+	int (*hwctx_sync_debug_bo)(struct amdxdna_hwctx *hwctx, u32 debug_bo_hdl);
  	void (*hmm_invalidate)(struct amdxdna_gem_obj *abo, unsigned long cur_seq);
  	int (*cmd_submit)(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, u64 *seq);
  	int (*get_aie_info)(struct amdxdna_client *client, struct amdxdna_drm_get_info *args);
drivers/accel/amdxdna/npu1_regs.c +1 -0
index e4f6dac7d00f..10124cccb102 100644
@@ -46,6 +46,7 @@
 
  const struct rt_config npu1_default_rt_cfg[] = {
  	{ 2, 1, AIE2_RT_CFG_INIT }, /* PDI APP LOAD MODE */
+	{ 4, 1, AIE2_RT_CFG_INIT }, /* Debug BO */
  	{ 1, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */
  	{ 0 },
  };
drivers/accel/amdxdna/npu4_regs.c +1 -0
index 9f2e33182ec6..e1da882420ec 100644
@@ -63,6 +63,7 @@
 
  const struct rt_config npu4_default_rt_cfg[] = {
  	{ 5, 1, AIE2_RT_CFG_INIT }, /* PDI APP LOAD MODE */
+	{ 10, 1, AIE2_RT_CFG_INIT }, /* DEBUG BUF */
  	{ 1, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */
  	{ 2, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */
  	{ 3, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */
author maintainer
#2 lore raw
Quoted 10 lines · Mario Limonciello
Quoted 7 lines · Lizhi Hou

To collect firmware debug information, the userspace application allocates a AMDXDNA_BO_DEV buffer object through DRM_IOCTL_AMDXDNA_CREATE_BO. Then it associates the buffer with the hardware context through DRM_IOCTL_AMDXDNA_CONFIG_HWCTX which requests firmware to bind the buffer through a mailbox command. The firmware then writes the debug data into this buffer. The buffer can be mapped into userspace so that applications can retrieve and analyze the firmware debug information.

Let me ask a high level question.  Do we really want all userspace to always have access to this debug information?  Could this leak information between processes potentially?

The buffer is allocated by the application and bound to the hardware context. Thus,  the debug data is per hardware context (per application). Only the process who owns the hardware context will have access to the debug information.  So there is not leak to other process.

Quoted 4 lines

Two ideas:

1) I wonder if this is better placed in debugfs files.

The debug bo is per process. The application allocates and owns the buffer and only the debug data for this application will be output to this buffer.  So debugfs might not fit here.

Quoted 1 line

2) If this architecture does make sense - what are your thoughts on tying the ability to use it to kernel lockdown?

I do not fully understand the question. This feature is useful for application debugging. And it is also required for a sanity test of xrt tools. Hopefully this helps. :)

Thanks,

Lizhi

Quoted 508 lines
Quoted 507 lines

---
  drivers/accel/amdxdna/TODO              |   1 - drivers/accel/amdxdna/aie2_ctx.c        | 116 ++++++++++++++++++++++-- drivers/accel/amdxdna/aie2_message.c    |  31 ++++++- drivers/accel/amdxdna/aie2_msg_priv.h   |  13 +++ drivers/accel/amdxdna/aie2_pci.c        |   1 + drivers/accel/amdxdna/aie2_pci.h        |   3 +

  drivers/accel/amdxdna/amdxdna_ctx.c     |  39 +++++++-
  drivers/accel/amdxdna/amdxdna_ctx.h     |  16 +++-
  drivers/accel/amdxdna/amdxdna_gem.c     |   3 +
  drivers/accel/amdxdna/amdxdna_gem.h     |   6 ++

  drivers/accel/amdxdna/amdxdna_pci_drv.c |   3 +- drivers/accel/amdxdna/amdxdna_pci_drv.h |   1 + drivers/accel/amdxdna/npu1_regs.c       |   1 + drivers/accel/amdxdna/npu4_regs.c       |   1 +

  14 files changed, 221 insertions(+), 14 deletions(-)
drivers/accel/amdxdna/TODO +0 -0
index ad8ac6e315b6..0e4bbebeaedf 100644
@@ -1,2 +1 @@

  - Add debugfs support
-- Add debug BO support

drivers/accel/amdxdna/aie2_ctx.c +0 -0
index ab4d66f1325d..63450b7773ac 100644
@@ -226,11 +226,10 @@ aie2_sched_resp_handler(void *handle, void 
 __iomem *data, size_t size)
   }

    static int
-aie2_sched_nocmd_resp_handler(void *handle, void __iomem *data, size_t size)
+aie2_sched_drvcmd_resp_handler(void *handle, void __iomem *data, size_t size)
  {
      struct amdxdna_sched_job *job = handle;
      int ret = 0;
-    u32 status;
        if (unlikely(!data))
          goto out;

drivers/accel/amdxdna/aie2_ctx.c +1 -1
@@ -240,8 +239,7 @@ aie2_sched_nocmd_resp_handler(void *handle, void 
 __iomem *data, size_t size)
           goto out;
       }
   -    status = readl(data);
-    XDNA_DBG(job->hwctx->client->xdna, "Resp status 0x%x", status);
+    job->drv_cmd->result = readl(data);

    out:
      aie2_sched_notify(job);

drivers/accel/amdxdna/aie2_ctx.c +12 -1
@@ -314,8 +312,18 @@ aie2_sched_job_run(struct drm_sched_job *sched_job)
       kref_get(&job->refcnt);
       fence = dma_fence_get(job->fence);
   -    if (unlikely(!cmd_abo)) {
-        ret = aie2_sync_bo(hwctx, job, aie2_sched_nocmd_resp_handler);
+    if (job->drv_cmd) {
+        switch (job->drv_cmd->opcode) {
+        case SYNC_DEBUG_BO:
+            ret = aie2_sync_bo(hwctx, job, 
 aie2_sched_drvcmd_resp_handler);
+            break;
+        case ATTACH_DEBUG_BO:
+            ret = aie2_config_debug_bo(hwctx, job, 
 aie2_sched_drvcmd_resp_handler);
+            break;
+        default:
+            ret = -EINVAL;
+            break;
+        }
           goto out;

      }
  @@ -766,6 +774,74 @@ static int aie2_hwctx_cu_config(struct amdxdna_hwctx *hwctx, void *buf, u32 size return ret;
  }
  +static void aie2_cmd_wait(struct amdxdna_hwctx *hwctx, u64 seq)

drivers/accel/amdxdna/aie2_ctx.c +13 -0
+{
+    struct dma_fence *out_fence = aie2_cmd_get_out_fence(hwctx, seq);
+
+    if (!out_fence) {
+        XDNA_ERR(hwctx->client->xdna, "Failed to get fence");
+        return;
+    }
+
+    dma_fence_wait_timeout(out_fence, false, MAX_SCHEDULE_TIMEOUT);
+    dma_fence_put(out_fence);
+}
+
+static int aie2_hwctx_cfg_debug_bo(struct amdxdna_hwctx *hwctx, u32 

bo_hdl,
+                   bool attach)

drivers/accel/amdxdna/aie2_ctx.c +53 -0
+{
+    struct amdxdna_client *client = hwctx->client;
+    struct amdxdna_dev *xdna = client->xdna;
+    struct amdxdna_drv_cmd cmd = { 0 };
+    struct amdxdna_gem_obj *abo;
+    u64 seq;
+    int ret;
+
+    abo = amdxdna_gem_get_obj(client, bo_hdl, AMDXDNA_BO_DEV);
+    if (!abo) {
+        XDNA_ERR(xdna, "Get bo %d failed", bo_hdl);
+        return -EINVAL;
+    }
+
+    if (attach) {
+        if (abo->assigned_hwctx != AMDXDNA_INVALID_CTX_HANDLE) {
+            ret = -EBUSY;
+            goto put_obj;
+        }
+        cmd.opcode = ATTACH_DEBUG_BO;
+    } else {
+        if (abo->assigned_hwctx != hwctx->id) {
+            ret = -EINVAL;
+            goto put_obj;
+        }
+        cmd.opcode = DETACH_DEBUG_BO;
+    }
+
+    ret = amdxdna_cmd_submit(client, &cmd, AMDXDNA_INVALID_BO_HANDLE,
+                 &bo_hdl, 1, hwctx->id, &seq);
+    if (ret) {
+        XDNA_ERR(xdna, "Submit command failed");
+        goto put_obj;
+    }
+
+    aie2_cmd_wait(hwctx, seq);
+    if (cmd.result) {
+        XDNA_ERR(xdna, "Response failure 0x%x", cmd.result);
+        goto put_obj;
+    }
+
+    if (attach)
+        abo->assigned_hwctx = hwctx->id;
+    else
+        abo->assigned_hwctx = AMDXDNA_INVALID_CTX_HANDLE;
+
+    XDNA_DBG(xdna, "Config debug BO %d to %s", bo_hdl, hwctx->name);
+
+put_obj:
+    amdxdna_gem_put_obj(abo);
+    return ret;
+}
+

  int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size) {
      struct amdxdna_dev *xdna = hwctx->client->xdna;

drivers/accel/amdxdna/aie2_ctx.c +0 -0
@@ -775,14 +851,40 @@ int aie2_hwctx_config(struct amdxdna_hwctx 
 *hwctx, u32 type, u64 value, void *bu

      case DRM_AMDXDNA_HWCTX_CONFIG_CU:
          return aie2_hwctx_cu_config(hwctx, buf, size);
      case DRM_AMDXDNA_HWCTX_ASSIGN_DBG_BUF:
+        return aie2_hwctx_cfg_debug_bo(hwctx, (u32)value, true); case DRM_AMDXDNA_HWCTX_REMOVE_DBG_BUF:
-        return -EOPNOTSUPP;
+        return aie2_hwctx_cfg_debug_bo(hwctx, (u32)value, false); default:
          XDNA_DBG(xdna, "Not supported type %d", type);
          return -EOPNOTSUPP;
      }
  }
  +int aie2_hwctx_sync_debug_bo(struct amdxdna_hwctx *hwctx, u32 debug_bo_hdl)

drivers/accel/amdxdna/aie2_ctx.c +24 -0
+{
+    struct amdxdna_client *client = hwctx->client;
+    struct amdxdna_dev *xdna = client->xdna;
+    struct amdxdna_drv_cmd cmd = { 0 };
+    u64 seq;
+    int ret;
+
+    cmd.opcode = SYNC_DEBUG_BO;
+    ret = amdxdna_cmd_submit(client, &cmd, AMDXDNA_INVALID_BO_HANDLE,
+                 &debug_bo_hdl, 1, hwctx->id, &seq);
+    if (ret) {
+        XDNA_ERR(xdna, "Submit command failed");
+        return ret;
+    }
+
+    aie2_cmd_wait(hwctx, seq);
+    if (cmd.result) {
+        XDNA_ERR(xdna, "Response failure 0x%x", cmd.result);
+        return ret;
+    }
+
+    return 0;
+}
+

  static int aie2_populate_range(struct amdxdna_gem_obj *abo) {
      struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev);

drivers/accel/amdxdna/aie2_message.c +3 -1
index 4660e8297ed8..0ec1dc6fe668 100644
@@ -749,7 +749,7 @@ int aie2_sync_bo(struct amdxdna_hwctx *hwctx, 
 struct amdxdna_sched_job *job,
       int ret = 0;
         req.src_addr = 0;
-    req.dst_addr = abo->mem.dev_addr - 
 hwctx->client->dev_heap->mem.dev_addr;
+    req.dst_addr = amdxdna_dev_bo_offset(abo);
       req.size = abo->mem.size;
         /* Device to Host */
@@ -773,3 +773,32 @@ int aie2_sync_bo(struct amdxdna_hwctx *hwctx, 
 struct amdxdna_sched_job *job,
         return 0;
   }
+
+int aie2_config_debug_bo(struct amdxdna_hwctx *hwctx, struct 

amdxdna_sched_job *job,
+             int (*notify_cb)(void *, void __iomem *, size_t))

drivers/accel/amdxdna/aie2_message.c +26 -0
+{
+    struct mailbox_channel *chann = hwctx->priv->mbox_chann;
+    struct amdxdna_gem_obj *abo = to_xdna_obj(job->bos[0]);
+    struct amdxdna_dev *xdna = hwctx->client->xdna;
+    struct config_debug_bo_req req;
+    struct xdna_mailbox_msg msg;
+
+    if (job->drv_cmd->opcode == ATTACH_DEBUG_BO)
+        req.config = DEBUG_BO_REGISTER;
+    else
+        req.config = DEBUG_BO_UNREGISTER;
+
+    req.offset = amdxdna_dev_bo_offset(abo);
+    req.size = abo->mem.size;
+
+    XDNA_DBG(xdna, "offset 0x%llx size 0x%llx config %d",
+         req.offset, req.size, req.config);
+
+    msg.handle = job;
+    msg.notify_cb = notify_cb;
+    msg.send_data = (u8 *)&req;
+    msg.send_size = sizeof(req);
+    msg.opcode = MSG_OP_CONFIG_DEBUG_BO;
+
+    return xdna_mailbox_send_msg(chann, &msg, TX_TIMEOUT);
+}
drivers/accel/amdxdna/aie2_msg_priv.h +13 -0
index 6df9065b13f6..6a5c70bff5e9 100644
@@ -18,6 +18,7 @@ enum aie2_msg_opcode {
       MSG_OP_CONFIG_CU                   = 0x11,
       MSG_OP_CHAIN_EXEC_BUFFER_CF        = 0x12,
       MSG_OP_CHAIN_EXEC_DPU              = 0x13,
+    MSG_OP_CONFIG_DEBUG_BO             = 0x14,
       MSG_OP_MAX_XRT_OPCODE,
       MSG_OP_SUSPEND                     = 0x101,
       MSG_OP_RESUME                      = 0x102,
@@ -365,4 +366,16 @@ struct sync_bo_req {
   struct sync_bo_resp {
       enum aie2_msg_status    status;
   } __packed;
+
+struct config_debug_bo_req {
+    __u64    offset;
+    __u64    size;
+#define DEBUG_BO_UNREGISTER 0
+#define DEBUG_BO_REGISTER   1
+    __u32    config;
+} __packed;
+
+struct config_debug_bo_resp {
+    enum aie2_msg_status    status;
+} __packed;
   #endif /* _AIE2_MSG_PRIV_H_ */
drivers/accel/amdxdna/aie2_pci.c +1 -0
index cfca4e456b61..f48045318dc0 100644
@@ -1004,6 +1004,7 @@ const struct amdxdna_dev_ops aie2_ops = {
       .hwctx_init = aie2_hwctx_init,
       .hwctx_fini = aie2_hwctx_fini,
       .hwctx_config = aie2_hwctx_config,
+    .hwctx_sync_debug_bo = aie2_hwctx_sync_debug_bo,
       .cmd_submit = aie2_cmd_submit,
       .hmm_invalidate = aie2_hmm_invalidate,
       .get_array = aie2_get_array,
drivers/accel/amdxdna/aie2_pci.h +3 -0
index 34bc35479f42..243ac21d50c1 100644
@@ -287,11 +287,14 @@ int aie2_cmdlist_multi_execbuf(struct 
 amdxdna_hwctx *hwctx,
                      int (*notify_cb)(void *, void __iomem *, size_t));
   int aie2_sync_bo(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job,
            int (*notify_cb)(void *, void __iomem *, size_t));
+int aie2_config_debug_bo(struct amdxdna_hwctx *hwctx, struct 
 amdxdna_sched_job *job,
+             int (*notify_cb)(void *, void __iomem *, size_t));
     /* aie2_hwctx.c */
   int aie2_hwctx_init(struct amdxdna_hwctx *hwctx);
   void aie2_hwctx_fini(struct amdxdna_hwctx *hwctx);
   int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size);
+int aie2_hwctx_sync_debug_bo(struct amdxdna_hwctx *hwctx, u32 
 debug_bo_hdl);
   void aie2_hwctx_suspend(struct amdxdna_client *client);

  int aie2_hwctx_resume(struct amdxdna_client *client);
  int aie2_cmd_submit(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, u64 *seq);

drivers/accel/amdxdna/amdxdna_ctx.c +0 -0
index 868ca369e0a0..d18182c59668 100644
@@ -328,6 +328,38 @@ int amdxdna_drm_config_hwctx_ioctl(struct 

drm_device *dev, void *data, struct dr
      return ret;
  }
  +int amdxdna_hwctx_sync_debug_bo(struct amdxdna_client *client, u32 debug_bo_hdl)

drivers/accel/amdxdna/amdxdna_ctx.c +24 -0
+{
+    struct amdxdna_dev *xdna = client->xdna;
+    struct amdxdna_hwctx *hwctx;
+    struct amdxdna_gem_obj *abo;
+    struct drm_gem_object *gobj;
+    int ret, idx;
+
+    if (!xdna->dev_info->ops->hwctx_sync_debug_bo)
+        return -EOPNOTSUPP;
+
+    gobj = drm_gem_object_lookup(client->filp, debug_bo_hdl);
+    if (!gobj)
+        return -EINVAL;
+
+    abo = to_xdna_obj(gobj);
+    guard(mutex)(&xdna->dev_lock);
+    idx = srcu_read_lock(&client->hwctx_srcu);
+    hwctx = xa_load(&client->hwctx_xa, abo->assigned_hwctx);
+    if (!hwctx) {
+        ret = -EINVAL;
+        goto unlock_srcu;
+    }
+
+    ret = xdna->dev_info->ops->hwctx_sync_debug_bo(hwctx, 

debug_bo_hdl);

drivers/accel/amdxdna/amdxdna_ctx.c +7 -0
+
+unlock_srcu:
+    srcu_read_unlock(&client->hwctx_srcu, idx);
+    drm_gem_object_put(gobj);
+    return ret;
+}
+

  static void
  amdxdna_arg_bos_put(struct amdxdna_sched_job *job) {

drivers/accel/amdxdna/amdxdna_ctx.c +2 -0
@@ -393,6 +425,7 @@ void amdxdna_sched_job_cleanup(struct 
 amdxdna_sched_job *job)
   }
     int amdxdna_cmd_submit(struct amdxdna_client *client,
+               struct amdxdna_drv_cmd *drv_cmd,
                  u32 cmd_bo_hdl, u32 *arg_bo_hdls, u32 arg_bo_cnt,
                  u32 hwctx_hdl, u64 *seq)
   {
@@ -406,6 +439,8 @@ int amdxdna_cmd_submit(struct amdxdna_client 
 *client,
       if (!job)
           return -ENOMEM;
   +    job->drv_cmd = drv_cmd;
+
       if (cmd_bo_hdl != AMDXDNA_INVALID_BO_HANDLE) {
           job->cmd_bo = amdxdna_gem_get_obj(client, cmd_bo_hdl, AMDXDNA_BO_CMD);

          if (!job->cmd_bo) {

drivers/accel/amdxdna/amdxdna_ctx.c +1 -2
@@ -413,8 +448,6 @@ int amdxdna_cmd_submit(struct amdxdna_client 
 *client,
               ret = -EINVAL;
               goto free_job;
           }
-    } else {
-        job->cmd_bo = NULL;
       }
         ret = amdxdna_arg_bos_lookup(client, job, arg_bo_hdls, arg_bo_cnt);
@@ -508,7 +541,7 @@ static int amdxdna_drm_submit_execbuf(struct 
 amdxdna_client *client,
           }
       }
   -    ret = amdxdna_cmd_submit(client, cmd_bo_hdl, arg_bo_hdls,
+    ret = amdxdna_cmd_submit(client, NULL, cmd_bo_hdl, arg_bo_hdls,
                    args->arg_count, args->hwctx, &args->seq);
       if (ret)

          XDNA_DBG(xdna, "Submit cmds failed, ret %d", ret);

drivers/accel/amdxdna/amdxdna_ctx.h +14 -1
index 7cd7a55936f0..cbe60efbe60b 100644
@@ -95,6 +95,17 @@ struct amdxdna_hwctx {
   #define drm_job_to_xdna_job(j) \
       container_of(j, struct amdxdna_sched_job, base)
   +enum amdxdna_job_opcode {
+    SYNC_DEBUG_BO,
+    ATTACH_DEBUG_BO,
+    DETACH_DEBUG_BO,
+};
+
+struct amdxdna_drv_cmd {
+    enum amdxdna_job_opcode    opcode;
+    u32            result;
+};
+
   struct amdxdna_sched_job {
       struct drm_sched_job    base;
       struct kref        refcnt;
@@ -106,6 +117,7 @@ struct amdxdna_sched_job {
       struct dma_fence    *out_fence;
       bool            job_done;
       u64            seq;
+    struct amdxdna_drv_cmd    *drv_cmd;
       struct amdxdna_gem_obj    *cmd_bo;
       size_t            bo_cnt;
       struct drm_gem_object    *bos[] __counted_by(bo_cnt);
@@ -143,9 +155,11 @@ void amdxdna_sched_job_cleanup(struct 
 amdxdna_sched_job *job);
   void amdxdna_hwctx_remove_all(struct amdxdna_client *client);
   int amdxdna_hwctx_walk(struct amdxdna_client *client, void *arg,
                  int (*walk)(struct amdxdna_hwctx *hwctx, void *arg));
+int amdxdna_hwctx_sync_debug_bo(struct amdxdna_client *client, u32 
 debug_bo_hdl);
     int amdxdna_cmd_submit(struct amdxdna_client *client,
-               u32 cmd_bo_hdls, u32 *arg_bo_hdls, u32 arg_bo_cnt,
+               struct amdxdna_drv_cmd *drv_cmd, u32 cmd_bo_hdls,
+               u32 *arg_bo_hdls, u32 arg_bo_cnt,
                  u32 hwctx_hdl, u64 *seq);
     int amdxdna_cmd_wait(struct amdxdna_client *client, u32 hwctx_hdl,
drivers/accel/amdxdna/amdxdna_gem.c +2 -0
index 7f91863c3f24..61e0136c21a8 100644
@@ -962,6 +962,9 @@ int amdxdna_drm_sync_bo_ioctl(struct drm_device 
 *dev,
       XDNA_DBG(xdna, "Sync bo %d offset 0x%llx, size 0x%llx\n",
            args->handle, args->offset, args->size);
   +    if (args->direction == SYNC_DIRECT_FROM_DEVICE)
+        ret = amdxdna_hwctx_sync_debug_bo(abo->client, args->handle);
+

  put_obj:
      drm_gem_object_put(gobj);
      return ret;

drivers/accel/amdxdna/amdxdna_gem.h +5 -0
index ae29db94a9d3..f79fc7f3c93b 100644
@@ -7,6 +7,7 @@
   #define _AMDXDNA_GEM_H_
     #include <linux/hmm.h>
+#include "amdxdna_pci_drv.h"
     struct amdxdna_umap {
       struct vm_area_struct        *vma;
@@ -62,6 +63,11 @@ static inline void amdxdna_gem_put_obj(struct 
 amdxdna_gem_obj *abo)
       drm_gem_object_put(to_gobj(abo));
   }
   +static inline u64 amdxdna_dev_bo_offset(struct amdxdna_gem_obj *abo)
+{
+    return abo->mem.dev_addr - abo->client->dev_heap->mem.dev_addr;
+}
+
   void amdxdna_umap_put(struct amdxdna_umap *mapp);

    struct drm_gem_object *

drivers/accel/amdxdna/amdxdna_pci_drv.c +2 -1
index 696fdac8ad3c..3599e713bfcb 100644
@@ -28,9 +28,10 @@ MODULE_FIRMWARE("amdnpu/17f0_20/npu.sbin");
    * 0.0: Initial version
    * 0.1: Support getting all hardware contexts by DRM_IOCTL_AMDXDNA_GET_ARRAY
    * 0.2: Support getting last error hardware error
+ * 0.3: Support firmware debug buffer
    */
   #define AMDXDNA_DRIVER_MAJOR        0
-#define AMDXDNA_DRIVER_MINOR        2
+#define AMDXDNA_DRIVER_MINOR        3
     /*
    * Bind the driver base on (vendor_id, device_id) pair and later use the
drivers/accel/amdxdna/amdxdna_pci_drv.h +1 -0
index 626beebf730e..c99477f5e454 100644
@@ -55,6 +55,7 @@ struct amdxdna_dev_ops {
       int (*hwctx_init)(struct amdxdna_hwctx *hwctx);
       void (*hwctx_fini)(struct amdxdna_hwctx *hwctx);
       int (*hwctx_config)(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size);
+    int (*hwctx_sync_debug_bo)(struct amdxdna_hwctx *hwctx, u32 
 debug_bo_hdl);
       void (*hmm_invalidate)(struct amdxdna_gem_obj *abo, unsigned long cur_seq);
       int (*cmd_submit)(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, u64 *seq);

      int (*get_aie_info)(struct amdxdna_client *client, struct amdxdna_drm_get_info *args);

drivers/accel/amdxdna/npu1_regs.c +0 -0
index e4f6dac7d00f..10124cccb102 100644
@@ -46,6 +46,7 @@
     const struct rt_config npu1_default_rt_cfg[] = {

      { 2, 1, AIE2_RT_CFG_INIT }, /* PDI APP LOAD MODE */ +    { 4, 1, AIE2_RT_CFG_INIT }, /* Debug BO */
      { 1, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */ { 0 },
  };

drivers/accel/amdxdna/npu4_regs.c +0 -0
index 9f2e33182ec6..e1da882420ec 100644
@@ -63,6 +63,7 @@
     const struct rt_config npu4_default_rt_cfg[] = {

      { 5, 1, AIE2_RT_CFG_INIT }, /* PDI APP LOAD MODE */ +    { 10, 1, AIE2_RT_CFG_INIT }, /* DEBUG BUF */
      { 1, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */ { 2, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */ { 3, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */

#3 lore raw
Quoted 16 lines · Lizhi Hou
Quoted 10 lines · Mario Limonciello
Quoted 7 lines · Lizhi Hou

To collect firmware debug information, the userspace application allocates a AMDXDNA_BO_DEV buffer object through DRM_IOCTL_AMDXDNA_CREATE_BO. Then it associates the buffer with the hardware context through DRM_IOCTL_AMDXDNA_CONFIG_HWCTX which requests firmware to bind the buffer through a mailbox command. The firmware then writes the debug data into this buffer. The buffer can be mapped into userspace so that applications can retrieve and analyze the firmware debug information.

Let me ask a high level question.  Do we really want all userspace to always have access to this debug information?  Could this leak information between processes potentially?

The buffer is allocated by the application and bound to the hardware context. Thus,  the debug data is per hardware context (per application). Only the process who owns the hardware context will have access to the debug information.  So there is not leak to other process.

Quoted 1 line
Quoted 9 lines
Quoted 3 lines

Two ideas:

1) I wonder if this is better placed in debugfs files.

The debug bo is per process. The application allocates and owns the buffer and only the debug data for this application will be output to this buffer.  So debugfs might not fit here.

Quoted 1 line

2) If this architecture does make sense - what are your thoughts on tying the ability to use it to kernel lockdown?

I do not fully understand the question. This feature is useful for application debugging. And it is also required for a sanity test of xrt tools. Hopefully this helps. :)

Thanks, that helps. I have one small comment below.

Quoted 257 lines

Thanks,

Lizhi

Quoted 253 lines
Quoted 253 lines

---
  drivers/accel/amdxdna/TODO              |   1 - drivers/accel/amdxdna/aie2_ctx.c        | 116 ++++++++++++++++++++++-- drivers/accel/amdxdna/aie2_message.c    |  31 ++++++- drivers/accel/amdxdna/aie2_msg_priv.h   |  13 +++ drivers/accel/amdxdna/aie2_pci.c        |   1 + drivers/accel/amdxdna/aie2_pci.h        |   3 +

  drivers/accel/amdxdna/amdxdna_ctx.c     |  39 +++++++-
  drivers/accel/amdxdna/amdxdna_ctx.h     |  16 +++-
  drivers/accel/amdxdna/amdxdna_gem.c     |   3 +
  drivers/accel/amdxdna/amdxdna_gem.h     |   6 ++

  drivers/accel/amdxdna/amdxdna_pci_drv.c |   3 +- drivers/accel/amdxdna/amdxdna_pci_drv.h |   1 + drivers/accel/amdxdna/npu1_regs.c       |   1 + drivers/accel/amdxdna/npu4_regs.c       |   1 +

  14 files changed, 221 insertions(+), 14 deletions(-)
drivers/accel/amdxdna/TODO +0 -0
index ad8ac6e315b6..0e4bbebeaedf 100644
@@ -1,2 +1 @@

  - Add debugfs support
-- Add debug BO support

drivers/accel/amdxdna/aie2_ctx.c +0 -0
index ab4d66f1325d..63450b7773ac 100644
@@ -226,11 +226,10 @@ aie2_sched_resp_handler(void *handle, void 
 __iomem *data, size_t size)
   }

    static int
-aie2_sched_nocmd_resp_handler(void *handle, void __iomem *data, size_t size)
+aie2_sched_drvcmd_resp_handler(void *handle, void __iomem *data, size_t size)
  {
      struct amdxdna_sched_job *job = handle;
      int ret = 0;
-    u32 status;
        if (unlikely(!data))
          goto out;

drivers/accel/amdxdna/aie2_ctx.c +1 -1
@@ -240,8 +239,7 @@ aie2_sched_nocmd_resp_handler(void *handle, void 
 __iomem *data, size_t size)
           goto out;
       }
   -    status = readl(data);
-    XDNA_DBG(job->hwctx->client->xdna, "Resp status 0x%x", status);
+    job->drv_cmd->result = readl(data);

    out:
      aie2_sched_notify(job);

drivers/accel/amdxdna/aie2_ctx.c +12 -1
@@ -314,8 +312,18 @@ aie2_sched_job_run(struct drm_sched_job *sched_job)
       kref_get(&job->refcnt);
       fence = dma_fence_get(job->fence);
   -    if (unlikely(!cmd_abo)) {
-        ret = aie2_sync_bo(hwctx, job, aie2_sched_nocmd_resp_handler);
+    if (job->drv_cmd) {
+        switch (job->drv_cmd->opcode) {
+        case SYNC_DEBUG_BO:
+            ret = aie2_sync_bo(hwctx, job, 
 aie2_sched_drvcmd_resp_handler);
+            break;
+        case ATTACH_DEBUG_BO:
+            ret = aie2_config_debug_bo(hwctx, job, 
 aie2_sched_drvcmd_resp_handler);
+            break;
+        default:
+            ret = -EINVAL;
+            break;
+        }
           goto out;

      }
  @@ -766,6 +774,74 @@ static int aie2_hwctx_cu_config(struct amdxdna_hwctx *hwctx, void *buf, u32 size return ret;
  }
  +static void aie2_cmd_wait(struct amdxdna_hwctx *hwctx, u64 seq)

drivers/accel/amdxdna/aie2_ctx.c +13 -0
+{
+    struct dma_fence *out_fence = aie2_cmd_get_out_fence(hwctx, seq);
+
+    if (!out_fence) {
+        XDNA_ERR(hwctx->client->xdna, "Failed to get fence");
+        return;
+    }
+
+    dma_fence_wait_timeout(out_fence, false, MAX_SCHEDULE_TIMEOUT);
+    dma_fence_put(out_fence);
+}
+
+static int aie2_hwctx_cfg_debug_bo(struct amdxdna_hwctx *hwctx, u32 

bo_hdl,
+                   bool attach)

drivers/accel/amdxdna/aie2_ctx.c +53 -0
+{
+    struct amdxdna_client *client = hwctx->client;
+    struct amdxdna_dev *xdna = client->xdna;
+    struct amdxdna_drv_cmd cmd = { 0 };
+    struct amdxdna_gem_obj *abo;
+    u64 seq;
+    int ret;
+
+    abo = amdxdna_gem_get_obj(client, bo_hdl, AMDXDNA_BO_DEV);
+    if (!abo) {
+        XDNA_ERR(xdna, "Get bo %d failed", bo_hdl);
+        return -EINVAL;
+    }
+
+    if (attach) {
+        if (abo->assigned_hwctx != AMDXDNA_INVALID_CTX_HANDLE) {
+            ret = -EBUSY;
+            goto put_obj;
+        }
+        cmd.opcode = ATTACH_DEBUG_BO;
+    } else {
+        if (abo->assigned_hwctx != hwctx->id) {
+            ret = -EINVAL;
+            goto put_obj;
+        }
+        cmd.opcode = DETACH_DEBUG_BO;
+    }
+
+    ret = amdxdna_cmd_submit(client, &cmd, AMDXDNA_INVALID_BO_HANDLE,
+                 &bo_hdl, 1, hwctx->id, &seq);
+    if (ret) {
+        XDNA_ERR(xdna, "Submit command failed");
+        goto put_obj;
+    }
+
+    aie2_cmd_wait(hwctx, seq);
+    if (cmd.result) {
+        XDNA_ERR(xdna, "Response failure 0x%x", cmd.result);
+        goto put_obj;
+    }
+
+    if (attach)
+        abo->assigned_hwctx = hwctx->id;
+    else
+        abo->assigned_hwctx = AMDXDNA_INVALID_CTX_HANDLE;
+
+    XDNA_DBG(xdna, "Config debug BO %d to %s", bo_hdl, hwctx->name);
+
+put_obj:
+    amdxdna_gem_put_obj(abo);
+    return ret;
+}
+

  int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size) {
      struct amdxdna_dev *xdna = hwctx->client->xdna;

drivers/accel/amdxdna/aie2_ctx.c +0 -0
@@ -775,14 +851,40 @@ int aie2_hwctx_config(struct amdxdna_hwctx 
 *hwctx, u32 type, u64 value, void *bu

      case DRM_AMDXDNA_HWCTX_CONFIG_CU:
          return aie2_hwctx_cu_config(hwctx, buf, size);
      case DRM_AMDXDNA_HWCTX_ASSIGN_DBG_BUF:
+        return aie2_hwctx_cfg_debug_bo(hwctx, (u32)value, true); case DRM_AMDXDNA_HWCTX_REMOVE_DBG_BUF:
-        return -EOPNOTSUPP;
+        return aie2_hwctx_cfg_debug_bo(hwctx, (u32)value, false); default:
          XDNA_DBG(xdna, "Not supported type %d", type);
          return -EOPNOTSUPP;
      }
  }
  +int aie2_hwctx_sync_debug_bo(struct amdxdna_hwctx *hwctx, u32 debug_bo_hdl)

drivers/accel/amdxdna/aie2_ctx.c +24 -0
+{
+    struct amdxdna_client *client = hwctx->client;
+    struct amdxdna_dev *xdna = client->xdna;
+    struct amdxdna_drv_cmd cmd = { 0 };
+    u64 seq;
+    int ret;
+
+    cmd.opcode = SYNC_DEBUG_BO;
+    ret = amdxdna_cmd_submit(client, &cmd, AMDXDNA_INVALID_BO_HANDLE,
+                 &debug_bo_hdl, 1, hwctx->id, &seq);
+    if (ret) {
+        XDNA_ERR(xdna, "Submit command failed");
+        return ret;
+    }
+
+    aie2_cmd_wait(hwctx, seq);
+    if (cmd.result) {
+        XDNA_ERR(xdna, "Response failure 0x%x", cmd.result);
+        return ret;
+    }
+
+    return 0;
+}
+

  static int aie2_populate_range(struct amdxdna_gem_obj *abo) {
      struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev);

drivers/accel/amdxdna/aie2_message.c +3 -1
index 4660e8297ed8..0ec1dc6fe668 100644
@@ -749,7 +749,7 @@ int aie2_sync_bo(struct amdxdna_hwctx *hwctx, 
 struct amdxdna_sched_job *job,
       int ret = 0;
         req.src_addr = 0;
-    req.dst_addr = abo->mem.dev_addr - hwctx->client->dev_heap- 
 >mem.dev_addr;
+    req.dst_addr = amdxdna_dev_bo_offset(abo);
       req.size = abo->mem.size;
         /* Device to Host */
@@ -773,3 +773,32 @@ int aie2_sync_bo(struct amdxdna_hwctx *hwctx, 
 struct amdxdna_sched_job *job,
         return 0;
   }
+
+int aie2_config_debug_bo(struct amdxdna_hwctx *hwctx, struct 

amdxdna_sched_job *job,
+             int (*notify_cb)(void *, void __iomem *, size_t))

drivers/accel/amdxdna/aie2_message.c +26 -0
+{
+    struct mailbox_channel *chann = hwctx->priv->mbox_chann;
+    struct amdxdna_gem_obj *abo = to_xdna_obj(job->bos[0]);
+    struct amdxdna_dev *xdna = hwctx->client->xdna;
+    struct config_debug_bo_req req;
+    struct xdna_mailbox_msg msg;
+
+    if (job->drv_cmd->opcode == ATTACH_DEBUG_BO)
+        req.config = DEBUG_BO_REGISTER;
+    else
+        req.config = DEBUG_BO_UNREGISTER;
+
+    req.offset = amdxdna_dev_bo_offset(abo);
+    req.size = abo->mem.size;
+
+    XDNA_DBG(xdna, "offset 0x%llx size 0x%llx config %d",
+         req.offset, req.size, req.config);
+
+    msg.handle = job;
+    msg.notify_cb = notify_cb;
+    msg.send_data = (u8 *)&req;
+    msg.send_size = sizeof(req);
+    msg.opcode = MSG_OP_CONFIG_DEBUG_BO;
+
+    return xdna_mailbox_send_msg(chann, &msg, TX_TIMEOUT);
+}
drivers/accel/amdxdna/aie2_msg_priv.h +1 -0
index 6df9065b13f6..6a5c70bff5e9 100644
@@ -18,6 +18,7 @@ enum aie2_msg_opcode {
       MSG_OP_CONFIG_CU                   = 0x11,
       MSG_OP_CHAIN_EXEC_BUFFER_CF        = 0x12,
       MSG_OP_CHAIN_EXEC_DPU              = 0x13,
+    MSG_OP_CONFIG_DEBUG_BO             = 0x14,
       MSG_OP_MAX_XRT_OPCODE,
       MSG_OP_SUSPEND                     = 0x101,
       MSG_OP_RESUME                      = 0x102,
@@ -365,4 +366,16 @@ struct sync_bo_req {
   struct sync_bo_resp {
Quoted 8 lines
Quoted 8 lines

drivers/accel/amdxdna/aie2_msg_priv.h struct sync_bo_req { · line 374

Quoted 8 lines
drivers/accel/amdxdna/aie2_msg_priv.h +6 -0
       enum aie2_msg_status    status;
   } __packed;
+
+struct config_debug_bo_req {
+    __u64    offset;
+    __u64    size;
+#define DEBUG_BO_UNREGISTER 0
+#define DEBUG_BO_REGISTER   1

Why are these defines placed in the middle of the struct? Is it because they're indicating the options for 'config'?

This seems like a weird place. I think they should be placed before the struct and instead have a comment indicating they're possible options.

Quoted 247 lines
Quoted 247 lines
Quoted 246 lines
drivers/accel/amdxdna/aie2_msg_priv.h +6 -0
+    __u32    config;
+} __packed;
+
+struct config_debug_bo_resp {
+    enum aie2_msg_status    status;
+} __packed;
   #endif /* _AIE2_MSG_PRIV_H_ */
drivers/accel/amdxdna/aie2_pci.c +1 -0
index cfca4e456b61..f48045318dc0 100644
@@ -1004,6 +1004,7 @@ const struct amdxdna_dev_ops aie2_ops = {
       .hwctx_init = aie2_hwctx_init,
       .hwctx_fini = aie2_hwctx_fini,
       .hwctx_config = aie2_hwctx_config,
+    .hwctx_sync_debug_bo = aie2_hwctx_sync_debug_bo,
       .cmd_submit = aie2_cmd_submit,
       .hmm_invalidate = aie2_hmm_invalidate,
       .get_array = aie2_get_array,
drivers/accel/amdxdna/aie2_pci.h +3 -0
index 34bc35479f42..243ac21d50c1 100644
@@ -287,11 +287,14 @@ int aie2_cmdlist_multi_execbuf(struct 
 amdxdna_hwctx *hwctx,
                      int (*notify_cb)(void *, void __iomem *, size_t));
   int aie2_sync_bo(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job,
            int (*notify_cb)(void *, void __iomem *, size_t));
+int aie2_config_debug_bo(struct amdxdna_hwctx *hwctx, struct 
 amdxdna_sched_job *job,
+             int (*notify_cb)(void *, void __iomem *, size_t));
     /* aie2_hwctx.c */
   int aie2_hwctx_init(struct amdxdna_hwctx *hwctx);
   void aie2_hwctx_fini(struct amdxdna_hwctx *hwctx);
   int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size);
+int aie2_hwctx_sync_debug_bo(struct amdxdna_hwctx *hwctx, u32 
 debug_bo_hdl);
   void aie2_hwctx_suspend(struct amdxdna_client *client);

  int aie2_hwctx_resume(struct amdxdna_client *client);
  int aie2_cmd_submit(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, u64 *seq);

drivers/accel/amdxdna/amdxdna_ctx.c +0 -0
index 868ca369e0a0..d18182c59668 100644
@@ -328,6 +328,38 @@ int amdxdna_drm_config_hwctx_ioctl(struct 

drm_device *dev, void *data, struct dr
      return ret;
  }
  +int amdxdna_hwctx_sync_debug_bo(struct amdxdna_client *client, u32 debug_bo_hdl)

drivers/accel/amdxdna/amdxdna_ctx.c +24 -0
+{
+    struct amdxdna_dev *xdna = client->xdna;
+    struct amdxdna_hwctx *hwctx;
+    struct amdxdna_gem_obj *abo;
+    struct drm_gem_object *gobj;
+    int ret, idx;
+
+    if (!xdna->dev_info->ops->hwctx_sync_debug_bo)
+        return -EOPNOTSUPP;
+
+    gobj = drm_gem_object_lookup(client->filp, debug_bo_hdl);
+    if (!gobj)
+        return -EINVAL;
+
+    abo = to_xdna_obj(gobj);
+    guard(mutex)(&xdna->dev_lock);
+    idx = srcu_read_lock(&client->hwctx_srcu);
+    hwctx = xa_load(&client->hwctx_xa, abo->assigned_hwctx);
+    if (!hwctx) {
+        ret = -EINVAL;
+        goto unlock_srcu;
+    }
+
+    ret = xdna->dev_info->ops->hwctx_sync_debug_bo(hwctx, 

debug_bo_hdl);

drivers/accel/amdxdna/amdxdna_ctx.c +7 -0
+
+unlock_srcu:
+    srcu_read_unlock(&client->hwctx_srcu, idx);
+    drm_gem_object_put(gobj);
+    return ret;
+}
+

  static void
  amdxdna_arg_bos_put(struct amdxdna_sched_job *job) {

drivers/accel/amdxdna/amdxdna_ctx.c +2 -0
@@ -393,6 +425,7 @@ void amdxdna_sched_job_cleanup(struct 
 amdxdna_sched_job *job)
   }
     int amdxdna_cmd_submit(struct amdxdna_client *client,
+               struct amdxdna_drv_cmd *drv_cmd,
                  u32 cmd_bo_hdl, u32 *arg_bo_hdls, u32 arg_bo_cnt,
                  u32 hwctx_hdl, u64 *seq)
   {
@@ -406,6 +439,8 @@ int amdxdna_cmd_submit(struct amdxdna_client 
 *client,
       if (!job)
           return -ENOMEM;
   +    job->drv_cmd = drv_cmd;
+
       if (cmd_bo_hdl != AMDXDNA_INVALID_BO_HANDLE) {
           job->cmd_bo = amdxdna_gem_get_obj(client, cmd_bo_hdl, AMDXDNA_BO_CMD);

          if (!job->cmd_bo) {

drivers/accel/amdxdna/amdxdna_ctx.c +1 -2
@@ -413,8 +448,6 @@ int amdxdna_cmd_submit(struct amdxdna_client 
 *client,
               ret = -EINVAL;
               goto free_job;
           }
-    } else {
-        job->cmd_bo = NULL;
       }
         ret = amdxdna_arg_bos_lookup(client, job, arg_bo_hdls, arg_bo_cnt);
@@ -508,7 +541,7 @@ static int amdxdna_drm_submit_execbuf(struct 
 amdxdna_client *client,
           }
       }
   -    ret = amdxdna_cmd_submit(client, cmd_bo_hdl, arg_bo_hdls,
+    ret = amdxdna_cmd_submit(client, NULL, cmd_bo_hdl, arg_bo_hdls,
                    args->arg_count, args->hwctx, &args->seq);
       if (ret)

          XDNA_DBG(xdna, "Submit cmds failed, ret %d", ret);

drivers/accel/amdxdna/amdxdna_ctx.h +14 -1
index 7cd7a55936f0..cbe60efbe60b 100644
@@ -95,6 +95,17 @@ struct amdxdna_hwctx {
   #define drm_job_to_xdna_job(j) \
       container_of(j, struct amdxdna_sched_job, base)
   +enum amdxdna_job_opcode {
+    SYNC_DEBUG_BO,
+    ATTACH_DEBUG_BO,
+    DETACH_DEBUG_BO,
+};
+
+struct amdxdna_drv_cmd {
+    enum amdxdna_job_opcode    opcode;
+    u32            result;
+};
+
   struct amdxdna_sched_job {
       struct drm_sched_job    base;
       struct kref        refcnt;
@@ -106,6 +117,7 @@ struct amdxdna_sched_job {
       struct dma_fence    *out_fence;
       bool            job_done;
       u64            seq;
+    struct amdxdna_drv_cmd    *drv_cmd;
       struct amdxdna_gem_obj    *cmd_bo;
       size_t            bo_cnt;
       struct drm_gem_object    *bos[] __counted_by(bo_cnt);
@@ -143,9 +155,11 @@ void amdxdna_sched_job_cleanup(struct 
 amdxdna_sched_job *job);
   void amdxdna_hwctx_remove_all(struct amdxdna_client *client);
   int amdxdna_hwctx_walk(struct amdxdna_client *client, void *arg,
                  int (*walk)(struct amdxdna_hwctx *hwctx, void *arg));
+int amdxdna_hwctx_sync_debug_bo(struct amdxdna_client *client, u32 
 debug_bo_hdl);
     int amdxdna_cmd_submit(struct amdxdna_client *client,
-               u32 cmd_bo_hdls, u32 *arg_bo_hdls, u32 arg_bo_cnt,
+               struct amdxdna_drv_cmd *drv_cmd, u32 cmd_bo_hdls,
+               u32 *arg_bo_hdls, u32 arg_bo_cnt,
                  u32 hwctx_hdl, u64 *seq);
     int amdxdna_cmd_wait(struct amdxdna_client *client, u32 hwctx_hdl,
drivers/accel/amdxdna/amdxdna_gem.c +2 -0
index 7f91863c3f24..61e0136c21a8 100644
@@ -962,6 +962,9 @@ int amdxdna_drm_sync_bo_ioctl(struct drm_device 
 *dev,
       XDNA_DBG(xdna, "Sync bo %d offset 0x%llx, size 0x%llx\n",
            args->handle, args->offset, args->size);
   +    if (args->direction == SYNC_DIRECT_FROM_DEVICE)
+        ret = amdxdna_hwctx_sync_debug_bo(abo->client, args->handle);
+

  put_obj:
      drm_gem_object_put(gobj);
      return ret;

drivers/accel/amdxdna/amdxdna_gem.h +5 -0
index ae29db94a9d3..f79fc7f3c93b 100644
@@ -7,6 +7,7 @@
   #define _AMDXDNA_GEM_H_
     #include <linux/hmm.h>
+#include "amdxdna_pci_drv.h"
     struct amdxdna_umap {
       struct vm_area_struct        *vma;
@@ -62,6 +63,11 @@ static inline void amdxdna_gem_put_obj(struct 
 amdxdna_gem_obj *abo)
       drm_gem_object_put(to_gobj(abo));
   }
   +static inline u64 amdxdna_dev_bo_offset(struct amdxdna_gem_obj *abo)
+{
+    return abo->mem.dev_addr - abo->client->dev_heap->mem.dev_addr;
+}
+
   void amdxdna_umap_put(struct amdxdna_umap *mapp);

    struct drm_gem_object *

drivers/accel/amdxdna/amdxdna_pci_drv.c +2 -1
index 696fdac8ad3c..3599e713bfcb 100644
@@ -28,9 +28,10 @@ MODULE_FIRMWARE("amdnpu/17f0_20/npu.sbin");
    * 0.0: Initial version
    * 0.1: Support getting all hardware contexts by DRM_IOCTL_AMDXDNA_GET_ARRAY
    * 0.2: Support getting last error hardware error
+ * 0.3: Support firmware debug buffer
    */
   #define AMDXDNA_DRIVER_MAJOR        0
-#define AMDXDNA_DRIVER_MINOR        2
+#define AMDXDNA_DRIVER_MINOR        3
     /*
    * Bind the driver base on (vendor_id, device_id) pair and later use the
drivers/accel/amdxdna/amdxdna_pci_drv.h +1 -0
index 626beebf730e..c99477f5e454 100644
@@ -55,6 +55,7 @@ struct amdxdna_dev_ops {
       int (*hwctx_init)(struct amdxdna_hwctx *hwctx);
       void (*hwctx_fini)(struct amdxdna_hwctx *hwctx);
       int (*hwctx_config)(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size);
+    int (*hwctx_sync_debug_bo)(struct amdxdna_hwctx *hwctx, u32 
 debug_bo_hdl);
       void (*hmm_invalidate)(struct amdxdna_gem_obj *abo, unsigned long cur_seq);
       int (*cmd_submit)(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, u64 *seq);

      int (*get_aie_info)(struct amdxdna_client *client, struct amdxdna_drm_get_info *args);

drivers/accel/amdxdna/npu1_regs.c +0 -0
index e4f6dac7d00f..10124cccb102 100644
@@ -46,6 +46,7 @@
     const struct rt_config npu1_default_rt_cfg[] = {

      { 2, 1, AIE2_RT_CFG_INIT }, /* PDI APP LOAD MODE */ +    { 4, 1, AIE2_RT_CFG_INIT }, /* Debug BO */
      { 1, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */ { 0 },
  };

drivers/accel/amdxdna/npu4_regs.c +0 -0
index 9f2e33182ec6..e1da882420ec 100644
@@ -63,6 +63,7 @@
     const struct rt_config npu4_default_rt_cfg[] = {

      { 5, 1, AIE2_RT_CFG_INIT }, /* PDI APP LOAD MODE */ +    { 10, 1, AIE2_RT_CFG_INIT }, /* DEBUG BUF */
      { 1, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */ { 2, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */ { 3, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */

author maintainer
#4 lore raw
Quoted 302 lines · Mario Limonciello (AMD) (kernel.org)
Quoted 16 lines · Lizhi Hou
Quoted 10 lines · Mario Limonciello
Quoted 7 lines · Lizhi Hou

To collect firmware debug information, the userspace application allocates a AMDXDNA_BO_DEV buffer object through DRM_IOCTL_AMDXDNA_CREATE_BO. Then it associates the buffer with the hardware context through DRM_IOCTL_AMDXDNA_CONFIG_HWCTX which requests firmware to bind the buffer through a mailbox command. The firmware then writes the debug data into this buffer. The buffer can be mapped into userspace so that applications can retrieve and analyze the firmware debug information.

Let me ask a high level question.  Do we really want all userspace to always have access to this debug information? Could this leak information between processes potentially?

The buffer is allocated by the application and bound to the hardware context. Thus,  the debug data is per hardware context (per application). Only the process who owns the hardware context will have access to the debug information.  So there is not leak to other process.

Quoted 1 line
Quoted 9 lines
Quoted 3 lines

Two ideas:

1) I wonder if this is better placed in debugfs files.

The debug bo is per process. The application allocates and owns the buffer and only the debug data for this application will be output to this buffer.  So debugfs might not fit here.

Quoted 1 line

2) If this architecture does make sense - what are your thoughts on tying the ability to use it to kernel lockdown?

I do not fully understand the question. This feature is useful for application debugging. And it is also required for a sanity test of xrt tools. Hopefully this helps. :)

Thanks, that helps.  I have one small comment below.

Quoted 260 lines

Thanks,

Lizhi

Quoted 256 lines
Quoted 256 lines

---
  drivers/accel/amdxdna/TODO              |   1 - drivers/accel/amdxdna/aie2_ctx.c        | 116
++++++++++++++++++++++--
  drivers/accel/amdxdna/aie2_message.c    |  31 ++++++- drivers/accel/amdxdna/aie2_msg_priv.h   |  13 +++ drivers/accel/amdxdna/aie2_pci.c        |   1 + drivers/accel/amdxdna/aie2_pci.h        |   3 +

  drivers/accel/amdxdna/amdxdna_ctx.c     |  39 +++++++-
  drivers/accel/amdxdna/amdxdna_ctx.h     |  16 +++-
  drivers/accel/amdxdna/amdxdna_gem.c     |   3 +
  drivers/accel/amdxdna/amdxdna_gem.h     |   6 ++

  drivers/accel/amdxdna/amdxdna_pci_drv.c |   3 +- drivers/accel/amdxdna/amdxdna_pci_drv.h |   1 + drivers/accel/amdxdna/npu1_regs.c       |   1 + drivers/accel/amdxdna/npu4_regs.c       |   1 +

  14 files changed, 221 insertions(+), 14 deletions(-)
drivers/accel/amdxdna/TODO +0 -0
index ad8ac6e315b6..0e4bbebeaedf 100644
@@ -1,2 +1 @@

  - Add debugfs support
-- Add debug BO support

drivers/accel/amdxdna/aie2_ctx.c +0 -0
index ab4d66f1325d..63450b7773ac 100644
@@ -226,11 +226,10 @@ aie2_sched_resp_handler(void *handle, void 
 __iomem *data, size_t size)
   }

    static int
-aie2_sched_nocmd_resp_handler(void *handle, void __iomem *data, size_t size)
+aie2_sched_drvcmd_resp_handler(void *handle, void __iomem *data, size_t size)
  {
      struct amdxdna_sched_job *job = handle;
      int ret = 0;
-    u32 status;
        if (unlikely(!data))
          goto out;

drivers/accel/amdxdna/aie2_ctx.c +1 -1
@@ -240,8 +239,7 @@ aie2_sched_nocmd_resp_handler(void *handle, 
 void __iomem *data, size_t size)
           goto out;
       }
   -    status = readl(data);
-    XDNA_DBG(job->hwctx->client->xdna, "Resp status 0x%x", status);
+    job->drv_cmd->result = readl(data);

    out:
      aie2_sched_notify(job);

drivers/accel/amdxdna/aie2_ctx.c +12 -1
@@ -314,8 +312,18 @@ aie2_sched_job_run(struct drm_sched_job 
 *sched_job)
       kref_get(&job->refcnt);
       fence = dma_fence_get(job->fence);
   -    if (unlikely(!cmd_abo)) {
-        ret = aie2_sync_bo(hwctx, job, 
 aie2_sched_nocmd_resp_handler);
+    if (job->drv_cmd) {
+        switch (job->drv_cmd->opcode) {
+        case SYNC_DEBUG_BO:
+            ret = aie2_sync_bo(hwctx, job, 
 aie2_sched_drvcmd_resp_handler);
+            break;
+        case ATTACH_DEBUG_BO:
+            ret = aie2_config_debug_bo(hwctx, job, 
 aie2_sched_drvcmd_resp_handler);
+            break;
+        default:
+            ret = -EINVAL;
+            break;
+        }

          goto out;
      }
  @@ -766,6 +774,74 @@ static int aie2_hwctx_cu_config(struct amdxdna_hwctx *hwctx, void *buf, u32 size return ret;
  }
  +static void aie2_cmd_wait(struct amdxdna_hwctx *hwctx, u64 seq)

drivers/accel/amdxdna/aie2_ctx.c +13 -0
+{
+    struct dma_fence *out_fence = aie2_cmd_get_out_fence(hwctx, seq);
+
+    if (!out_fence) {
+        XDNA_ERR(hwctx->client->xdna, "Failed to get fence");
+        return;
+    }
+
+    dma_fence_wait_timeout(out_fence, false, MAX_SCHEDULE_TIMEOUT);
+    dma_fence_put(out_fence);
+}
+
+static int aie2_hwctx_cfg_debug_bo(struct amdxdna_hwctx *hwctx, 

u32 bo_hdl,
+                   bool attach)

drivers/accel/amdxdna/aie2_ctx.c +53 -0
+{
+    struct amdxdna_client *client = hwctx->client;
+    struct amdxdna_dev *xdna = client->xdna;
+    struct amdxdna_drv_cmd cmd = { 0 };
+    struct amdxdna_gem_obj *abo;
+    u64 seq;
+    int ret;
+
+    abo = amdxdna_gem_get_obj(client, bo_hdl, AMDXDNA_BO_DEV);
+    if (!abo) {
+        XDNA_ERR(xdna, "Get bo %d failed", bo_hdl);
+        return -EINVAL;
+    }
+
+    if (attach) {
+        if (abo->assigned_hwctx != AMDXDNA_INVALID_CTX_HANDLE) {
+            ret = -EBUSY;
+            goto put_obj;
+        }
+        cmd.opcode = ATTACH_DEBUG_BO;
+    } else {
+        if (abo->assigned_hwctx != hwctx->id) {
+            ret = -EINVAL;
+            goto put_obj;
+        }
+        cmd.opcode = DETACH_DEBUG_BO;
+    }
+
+    ret = amdxdna_cmd_submit(client, &cmd, AMDXDNA_INVALID_BO_HANDLE,
+                 &bo_hdl, 1, hwctx->id, &seq);
+    if (ret) {
+        XDNA_ERR(xdna, "Submit command failed");
+        goto put_obj;
+    }
+
+    aie2_cmd_wait(hwctx, seq);
+    if (cmd.result) {
+        XDNA_ERR(xdna, "Response failure 0x%x", cmd.result);
+        goto put_obj;
+    }
+
+    if (attach)
+        abo->assigned_hwctx = hwctx->id;
+    else
+        abo->assigned_hwctx = AMDXDNA_INVALID_CTX_HANDLE;
+
+    XDNA_DBG(xdna, "Config debug BO %d to %s", bo_hdl, hwctx->name);
+
+put_obj:
+    amdxdna_gem_put_obj(abo);
+    return ret;
+}
+

  int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size) {
      struct amdxdna_dev *xdna = hwctx->client->xdna;

drivers/accel/amdxdna/aie2_ctx.c +0 -0
@@ -775,14 +851,40 @@ int aie2_hwctx_config(struct amdxdna_hwctx 
 *hwctx, u32 type, u64 value, void *bu

      case DRM_AMDXDNA_HWCTX_CONFIG_CU:
          return aie2_hwctx_cu_config(hwctx, buf, size);
      case DRM_AMDXDNA_HWCTX_ASSIGN_DBG_BUF:
+        return aie2_hwctx_cfg_debug_bo(hwctx, (u32)value, true); case DRM_AMDXDNA_HWCTX_REMOVE_DBG_BUF:
-        return -EOPNOTSUPP;
+        return aie2_hwctx_cfg_debug_bo(hwctx, (u32)value, false); default:
          XDNA_DBG(xdna, "Not supported type %d", type);
          return -EOPNOTSUPP;
      }
  }
  +int aie2_hwctx_sync_debug_bo(struct amdxdna_hwctx *hwctx, u32 debug_bo_hdl)

drivers/accel/amdxdna/aie2_ctx.c +24 -0
+{
+    struct amdxdna_client *client = hwctx->client;
+    struct amdxdna_dev *xdna = client->xdna;
+    struct amdxdna_drv_cmd cmd = { 0 };
+    u64 seq;
+    int ret;
+
+    cmd.opcode = SYNC_DEBUG_BO;
+    ret = amdxdna_cmd_submit(client, &cmd, AMDXDNA_INVALID_BO_HANDLE,
+                 &debug_bo_hdl, 1, hwctx->id, &seq);
+    if (ret) {
+        XDNA_ERR(xdna, "Submit command failed");
+        return ret;
+    }
+
+    aie2_cmd_wait(hwctx, seq);
+    if (cmd.result) {
+        XDNA_ERR(xdna, "Response failure 0x%x", cmd.result);
+        return ret;
+    }
+
+    return 0;
+}
+

  static int aie2_populate_range(struct amdxdna_gem_obj *abo) {
      struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev);

drivers/accel/amdxdna/aie2_message.c +3 -1
index 4660e8297ed8..0ec1dc6fe668 100644
@@ -749,7 +749,7 @@ int aie2_sync_bo(struct amdxdna_hwctx *hwctx, 
 struct amdxdna_sched_job *job,
       int ret = 0;
         req.src_addr = 0;
-    req.dst_addr = abo->mem.dev_addr - hwctx->client->dev_heap- 
 >mem.dev_addr;
+    req.dst_addr = amdxdna_dev_bo_offset(abo);
       req.size = abo->mem.size;
         /* Device to Host */
@@ -773,3 +773,32 @@ int aie2_sync_bo(struct amdxdna_hwctx *hwctx, 
 struct amdxdna_sched_job *job,
         return 0;
   }
+
+int aie2_config_debug_bo(struct amdxdna_hwctx *hwctx, struct 

amdxdna_sched_job *job,
+             int (*notify_cb)(void *, void __iomem *, size_t))

drivers/accel/amdxdna/aie2_message.c +26 -0
+{
+    struct mailbox_channel *chann = hwctx->priv->mbox_chann;
+    struct amdxdna_gem_obj *abo = to_xdna_obj(job->bos[0]);
+    struct amdxdna_dev *xdna = hwctx->client->xdna;
+    struct config_debug_bo_req req;
+    struct xdna_mailbox_msg msg;
+
+    if (job->drv_cmd->opcode == ATTACH_DEBUG_BO)
+        req.config = DEBUG_BO_REGISTER;
+    else
+        req.config = DEBUG_BO_UNREGISTER;
+
+    req.offset = amdxdna_dev_bo_offset(abo);
+    req.size = abo->mem.size;
+
+    XDNA_DBG(xdna, "offset 0x%llx size 0x%llx config %d",
+         req.offset, req.size, req.config);
+
+    msg.handle = job;
+    msg.notify_cb = notify_cb;
+    msg.send_data = (u8 *)&req;
+    msg.send_size = sizeof(req);
+    msg.opcode = MSG_OP_CONFIG_DEBUG_BO;
+
+    return xdna_mailbox_send_msg(chann, &msg, TX_TIMEOUT);
+}
drivers/accel/amdxdna/aie2_msg_priv.h +1 -0
index 6df9065b13f6..6a5c70bff5e9 100644
@@ -18,6 +18,7 @@ enum aie2_msg_opcode {
       MSG_OP_CONFIG_CU                   = 0x11,
       MSG_OP_CHAIN_EXEC_BUFFER_CF        = 0x12,
       MSG_OP_CHAIN_EXEC_DPU              = 0x13,
+    MSG_OP_CONFIG_DEBUG_BO             = 0x14,
       MSG_OP_MAX_XRT_OPCODE,
       MSG_OP_SUSPEND                     = 0x101,
       MSG_OP_RESUME                      = 0x102,
@@ -365,4 +366,16 @@ struct sync_bo_req {
   struct sync_bo_resp {
Quoted 8 lines
Quoted 8 lines

drivers/accel/amdxdna/aie2_msg_priv.h struct sync_bo_req { · line 374

Quoted 8 lines
drivers/accel/amdxdna/aie2_msg_priv.h +6 -0
       enum aie2_msg_status    status;
   } __packed;
+
+struct config_debug_bo_req {
+    __u64    offset;
+    __u64    size;
+#define DEBUG_BO_UNREGISTER 0
+#define DEBUG_BO_REGISTER   1

Why are these defines placed in the middle of the struct?  Is it because they're indicating the options for 'config'?

Quoted 1 line

This seems like a weird place.  I think they should be placed before the struct and instead have a comment indicating they're possible options.

Yes, it indicates the options for 'config'. And I will move it out and add comment when I merge the change.

Thanks,

Lizhi

Quoted 249 lines
Quoted 248 lines
Quoted 248 lines
Quoted 247 lines
drivers/accel/amdxdna/aie2_msg_priv.h +6 -0
+    __u32    config;
+} __packed;
+
+struct config_debug_bo_resp {
+    enum aie2_msg_status    status;
+} __packed;
   #endif /* _AIE2_MSG_PRIV_H_ */
drivers/accel/amdxdna/aie2_pci.c +1 -0
index cfca4e456b61..f48045318dc0 100644
@@ -1004,6 +1004,7 @@ const struct amdxdna_dev_ops aie2_ops = {
       .hwctx_init = aie2_hwctx_init,
       .hwctx_fini = aie2_hwctx_fini,
       .hwctx_config = aie2_hwctx_config,
+    .hwctx_sync_debug_bo = aie2_hwctx_sync_debug_bo,
       .cmd_submit = aie2_cmd_submit,
       .hmm_invalidate = aie2_hmm_invalidate,
       .get_array = aie2_get_array,
drivers/accel/amdxdna/aie2_pci.h +3 -0
index 34bc35479f42..243ac21d50c1 100644
@@ -287,11 +287,14 @@ int aie2_cmdlist_multi_execbuf(struct 
 amdxdna_hwctx *hwctx,
                      int (*notify_cb)(void *, void __iomem *, size_t));
   int aie2_sync_bo(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job,
            int (*notify_cb)(void *, void __iomem *, size_t));
+int aie2_config_debug_bo(struct amdxdna_hwctx *hwctx, struct 
 amdxdna_sched_job *job,
+             int (*notify_cb)(void *, void __iomem *, size_t));
     /* aie2_hwctx.c */
   int aie2_hwctx_init(struct amdxdna_hwctx *hwctx);
   void aie2_hwctx_fini(struct amdxdna_hwctx *hwctx);
   int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size);
+int aie2_hwctx_sync_debug_bo(struct amdxdna_hwctx *hwctx, u32 
 debug_bo_hdl);
   void aie2_hwctx_suspend(struct amdxdna_client *client);

  int aie2_hwctx_resume(struct amdxdna_client *client);
  int aie2_cmd_submit(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, u64 *seq);

drivers/accel/amdxdna/amdxdna_ctx.c +0 -0
index 868ca369e0a0..d18182c59668 100644
@@ -328,6 +328,38 @@ int amdxdna_drm_config_hwctx_ioctl(struct 

drm_device *dev, void *data, struct dr
      return ret;
  }
  +int amdxdna_hwctx_sync_debug_bo(struct amdxdna_client *client, u32 debug_bo_hdl)

drivers/accel/amdxdna/amdxdna_ctx.c +24 -0
+{
+    struct amdxdna_dev *xdna = client->xdna;
+    struct amdxdna_hwctx *hwctx;
+    struct amdxdna_gem_obj *abo;
+    struct drm_gem_object *gobj;
+    int ret, idx;
+
+    if (!xdna->dev_info->ops->hwctx_sync_debug_bo)
+        return -EOPNOTSUPP;
+
+    gobj = drm_gem_object_lookup(client->filp, debug_bo_hdl);
+    if (!gobj)
+        return -EINVAL;
+
+    abo = to_xdna_obj(gobj);
+    guard(mutex)(&xdna->dev_lock);
+    idx = srcu_read_lock(&client->hwctx_srcu);
+    hwctx = xa_load(&client->hwctx_xa, abo->assigned_hwctx);
+    if (!hwctx) {
+        ret = -EINVAL;
+        goto unlock_srcu;
+    }
+
+    ret = xdna->dev_info->ops->hwctx_sync_debug_bo(hwctx, 

debug_bo_hdl);

drivers/accel/amdxdna/amdxdna_ctx.c +7 -0
+
+unlock_srcu:
+    srcu_read_unlock(&client->hwctx_srcu, idx);
+    drm_gem_object_put(gobj);
+    return ret;
+}
+

  static void
  amdxdna_arg_bos_put(struct amdxdna_sched_job *job) {

drivers/accel/amdxdna/amdxdna_ctx.c +2 -0
@@ -393,6 +425,7 @@ void amdxdna_sched_job_cleanup(struct 
 amdxdna_sched_job *job)
   }
     int amdxdna_cmd_submit(struct amdxdna_client *client,
+               struct amdxdna_drv_cmd *drv_cmd,
                  u32 cmd_bo_hdl, u32 *arg_bo_hdls, u32 arg_bo_cnt,
                  u32 hwctx_hdl, u64 *seq)
   {
@@ -406,6 +439,8 @@ int amdxdna_cmd_submit(struct amdxdna_client 
 *client,
       if (!job)
           return -ENOMEM;
   +    job->drv_cmd = drv_cmd;
+
       if (cmd_bo_hdl != AMDXDNA_INVALID_BO_HANDLE) {
           job->cmd_bo = amdxdna_gem_get_obj(client, cmd_bo_hdl, AMDXDNA_BO_CMD);

          if (!job->cmd_bo) {

drivers/accel/amdxdna/amdxdna_ctx.c +1 -2
@@ -413,8 +448,6 @@ int amdxdna_cmd_submit(struct amdxdna_client 
 *client,
               ret = -EINVAL;
               goto free_job;
           }
-    } else {
-        job->cmd_bo = NULL;
       }
         ret = amdxdna_arg_bos_lookup(client, job, arg_bo_hdls, arg_bo_cnt);
@@ -508,7 +541,7 @@ static int amdxdna_drm_submit_execbuf(struct 
 amdxdna_client *client,
           }
       }
   -    ret = amdxdna_cmd_submit(client, cmd_bo_hdl, arg_bo_hdls,
+    ret = amdxdna_cmd_submit(client, NULL, cmd_bo_hdl, arg_bo_hdls,
                    args->arg_count, args->hwctx, &args->seq);
       if (ret)

          XDNA_DBG(xdna, "Submit cmds failed, ret %d", ret);

drivers/accel/amdxdna/amdxdna_ctx.h +14 -1
index 7cd7a55936f0..cbe60efbe60b 100644
@@ -95,6 +95,17 @@ struct amdxdna_hwctx {
   #define drm_job_to_xdna_job(j) \
       container_of(j, struct amdxdna_sched_job, base)
   +enum amdxdna_job_opcode {
+    SYNC_DEBUG_BO,
+    ATTACH_DEBUG_BO,
+    DETACH_DEBUG_BO,
+};
+
+struct amdxdna_drv_cmd {
+    enum amdxdna_job_opcode    opcode;
+    u32            result;
+};
+
   struct amdxdna_sched_job {
       struct drm_sched_job    base;
       struct kref        refcnt;
@@ -106,6 +117,7 @@ struct amdxdna_sched_job {
       struct dma_fence    *out_fence;
       bool            job_done;
       u64            seq;
+    struct amdxdna_drv_cmd    *drv_cmd;
       struct amdxdna_gem_obj    *cmd_bo;
       size_t            bo_cnt;
       struct drm_gem_object    *bos[] __counted_by(bo_cnt);
@@ -143,9 +155,11 @@ void amdxdna_sched_job_cleanup(struct 
 amdxdna_sched_job *job);
   void amdxdna_hwctx_remove_all(struct amdxdna_client *client);
   int amdxdna_hwctx_walk(struct amdxdna_client *client, void *arg,
                  int (*walk)(struct amdxdna_hwctx *hwctx, void *arg));
+int amdxdna_hwctx_sync_debug_bo(struct amdxdna_client *client, u32 
 debug_bo_hdl);
     int amdxdna_cmd_submit(struct amdxdna_client *client,
-               u32 cmd_bo_hdls, u32 *arg_bo_hdls, u32 arg_bo_cnt,
+               struct amdxdna_drv_cmd *drv_cmd, u32 cmd_bo_hdls,
+               u32 *arg_bo_hdls, u32 arg_bo_cnt,
                  u32 hwctx_hdl, u64 *seq);
     int amdxdna_cmd_wait(struct amdxdna_client *client, u32 hwctx_hdl,
drivers/accel/amdxdna/amdxdna_gem.c +2 -0
index 7f91863c3f24..61e0136c21a8 100644
@@ -962,6 +962,9 @@ int amdxdna_drm_sync_bo_ioctl(struct drm_device 
 *dev,
       XDNA_DBG(xdna, "Sync bo %d offset 0x%llx, size 0x%llx\n",
            args->handle, args->offset, args->size);
   +    if (args->direction == SYNC_DIRECT_FROM_DEVICE)
+        ret = amdxdna_hwctx_sync_debug_bo(abo->client, args->handle);
+

  put_obj:
      drm_gem_object_put(gobj);
      return ret;

drivers/accel/amdxdna/amdxdna_gem.h +5 -0
index ae29db94a9d3..f79fc7f3c93b 100644
@@ -7,6 +7,7 @@
   #define _AMDXDNA_GEM_H_
     #include <linux/hmm.h>
+#include "amdxdna_pci_drv.h"
     struct amdxdna_umap {
       struct vm_area_struct        *vma;
@@ -62,6 +63,11 @@ static inline void amdxdna_gem_put_obj(struct 
 amdxdna_gem_obj *abo)
       drm_gem_object_put(to_gobj(abo));
   }
   +static inline u64 amdxdna_dev_bo_offset(struct amdxdna_gem_obj *abo)
+{
+    return abo->mem.dev_addr - abo->client->dev_heap->mem.dev_addr;
+}
+
   void amdxdna_umap_put(struct amdxdna_umap *mapp);

    struct drm_gem_object *

drivers/accel/amdxdna/amdxdna_pci_drv.c +2 -1
index 696fdac8ad3c..3599e713bfcb 100644
@@ -28,9 +28,10 @@ MODULE_FIRMWARE("amdnpu/17f0_20/npu.sbin");
    * 0.0: Initial version
    * 0.1: Support getting all hardware contexts by DRM_IOCTL_AMDXDNA_GET_ARRAY
    * 0.2: Support getting last error hardware error
+ * 0.3: Support firmware debug buffer
    */
   #define AMDXDNA_DRIVER_MAJOR        0
-#define AMDXDNA_DRIVER_MINOR        2
+#define AMDXDNA_DRIVER_MINOR        3
     /*
    * Bind the driver base on (vendor_id, device_id) pair and later use the
drivers/accel/amdxdna/amdxdna_pci_drv.h +1 -0
index 626beebf730e..c99477f5e454 100644
@@ -55,6 +55,7 @@ struct amdxdna_dev_ops {
       int (*hwctx_init)(struct amdxdna_hwctx *hwctx);
       void (*hwctx_fini)(struct amdxdna_hwctx *hwctx);
       int (*hwctx_config)(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size);
+    int (*hwctx_sync_debug_bo)(struct amdxdna_hwctx *hwctx, u32 
 debug_bo_hdl);
       void (*hmm_invalidate)(struct amdxdna_gem_obj *abo, unsigned long cur_seq);
       int (*cmd_submit)(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, u64 *seq);

      int (*get_aie_info)(struct amdxdna_client *client, struct amdxdna_drm_get_info *args);

drivers/accel/amdxdna/npu1_regs.c +0 -0
index e4f6dac7d00f..10124cccb102 100644
@@ -46,6 +46,7 @@
     const struct rt_config npu1_default_rt_cfg[] = {

      { 2, 1, AIE2_RT_CFG_INIT }, /* PDI APP LOAD MODE */ +    { 4, 1, AIE2_RT_CFG_INIT }, /* Debug BO */
      { 1, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */ { 0 },
  };

drivers/accel/amdxdna/npu4_regs.c +0 -0
index 9f2e33182ec6..e1da882420ec 100644
@@ -63,6 +63,7 @@
     const struct rt_config npu4_default_rt_cfg[] = {

      { 5, 1, AIE2_RT_CFG_INIT }, /* PDI APP LOAD MODE */ +    { 10, 1, AIE2_RT_CFG_INIT }, /* DEBUG BUF */
      { 1, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */ { 2, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */ { 3, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */

author maintainer Applied
#5 lore raw

Applied to drm-misc-next.

Quoted 566 lines · Lizhi Hou
Quoted 305 lines · Mario Limonciello (AMD) (kernel.org)
Quoted 16 lines · Lizhi Hou
Quoted 10 lines · Mario Limonciello
Quoted 7 lines · Lizhi Hou

To collect firmware debug information, the userspace application allocates a AMDXDNA_BO_DEV buffer object through DRM_IOCTL_AMDXDNA_CREATE_BO. Then it associates the buffer with the hardware context through DRM_IOCTL_AMDXDNA_CONFIG_HWCTX which requests firmware to bind the buffer through a mailbox command. The firmware then writes the debug data into this buffer. The buffer can be mapped into userspace so that applications can retrieve and analyze the firmware debug information.

Let me ask a high level question.  Do we really want all userspace to always have access to this debug information? Could this leak information between processes potentially?

The buffer is allocated by the application and bound to the hardware context. Thus,  the debug data is per hardware context (per application). Only the process who owns the hardware context will have access to the debug information. So there is not leak to other process.

Quoted 1 line
Quoted 9 lines
Quoted 3 lines

Two ideas:

1) I wonder if this is better placed in debugfs files.

The debug bo is per process. The application allocates and owns the buffer and only the debug data for this application will be output to this buffer.  So debugfs might not fit here.

Quoted 1 line

2) If this architecture does make sense - what are your thoughts on tying the ability to use it to kernel lockdown?

I do not fully understand the question. This feature is useful for application debugging. And it is also required for a sanity test of xrt tools. Hopefully this helps. :)

Thanks, that helps.  I have one small comment below.

Quoted 263 lines

Thanks,

Lizhi

Quoted 259 lines
Quoted 259 lines

---
  drivers/accel/amdxdna/TODO              |   1 - drivers/accel/amdxdna/aie2_ctx.c        | 116
++++++++++++++++++++++--
  drivers/accel/amdxdna/aie2_message.c    |  31 ++++++- drivers/accel/amdxdna/aie2_msg_priv.h   |  13 +++ drivers/accel/amdxdna/aie2_pci.c        |   1 + drivers/accel/amdxdna/aie2_pci.h        |   3 +

  drivers/accel/amdxdna/amdxdna_ctx.c     |  39 +++++++-
  drivers/accel/amdxdna/amdxdna_ctx.h     |  16 +++-
  drivers/accel/amdxdna/amdxdna_gem.c     |   3 +
  drivers/accel/amdxdna/amdxdna_gem.h     |   6 ++

  drivers/accel/amdxdna/amdxdna_pci_drv.c |   3 +- drivers/accel/amdxdna/amdxdna_pci_drv.h |   1 + drivers/accel/amdxdna/npu1_regs.c       |   1 + drivers/accel/amdxdna/npu4_regs.c       |   1 +

  14 files changed, 221 insertions(+), 14 deletions(-)
drivers/accel/amdxdna/TODO +0 -0
index ad8ac6e315b6..0e4bbebeaedf 100644
@@ -1,2 +1 @@

  - Add debugfs support
-- Add debug BO support

drivers/accel/amdxdna/aie2_ctx.c +0 -0
index ab4d66f1325d..63450b7773ac 100644
@@ -226,11 +226,10 @@ aie2_sched_resp_handler(void *handle, void 
 __iomem *data, size_t size)
   }

    static int
-aie2_sched_nocmd_resp_handler(void *handle, void __iomem *data, size_t size)
+aie2_sched_drvcmd_resp_handler(void *handle, void __iomem *data, size_t size)
  {
      struct amdxdna_sched_job *job = handle;
      int ret = 0;
-    u32 status;
        if (unlikely(!data))
          goto out;

drivers/accel/amdxdna/aie2_ctx.c +1 -1
@@ -240,8 +239,7 @@ aie2_sched_nocmd_resp_handler(void *handle, 
 void __iomem *data, size_t size)
           goto out;
       }
   -    status = readl(data);
-    XDNA_DBG(job->hwctx->client->xdna, "Resp status 0x%x", status);
+    job->drv_cmd->result = readl(data);

    out:
      aie2_sched_notify(job);

drivers/accel/amdxdna/aie2_ctx.c +12 -1
@@ -314,8 +312,18 @@ aie2_sched_job_run(struct drm_sched_job 
 *sched_job)
       kref_get(&job->refcnt);
       fence = dma_fence_get(job->fence);
   -    if (unlikely(!cmd_abo)) {
-        ret = aie2_sync_bo(hwctx, job, 
 aie2_sched_nocmd_resp_handler);
+    if (job->drv_cmd) {
+        switch (job->drv_cmd->opcode) {
+        case SYNC_DEBUG_BO:
+            ret = aie2_sync_bo(hwctx, job, 
 aie2_sched_drvcmd_resp_handler);
+            break;
+        case ATTACH_DEBUG_BO:
+            ret = aie2_config_debug_bo(hwctx, job, 
 aie2_sched_drvcmd_resp_handler);
+            break;
+        default:
+            ret = -EINVAL;
+            break;
+        }

          goto out;
      }
  @@ -766,6 +774,74 @@ static int aie2_hwctx_cu_config(struct amdxdna_hwctx *hwctx, void *buf, u32 size return ret;
  }
  +static void aie2_cmd_wait(struct amdxdna_hwctx *hwctx, u64 seq)
+{
+    struct dma_fence *out_fence = aie2_cmd_get_out_fence(hwctx,
seq);

drivers/accel/amdxdna/aie2_ctx.c +11 -0
+
+    if (!out_fence) {
+        XDNA_ERR(hwctx->client->xdna, "Failed to get fence");
+        return;
+    }
+
+    dma_fence_wait_timeout(out_fence, false, MAX_SCHEDULE_TIMEOUT);
+    dma_fence_put(out_fence);
+}
+
+static int aie2_hwctx_cfg_debug_bo(struct amdxdna_hwctx *hwctx, 

u32 bo_hdl,
+                   bool attach)

drivers/accel/amdxdna/aie2_ctx.c +29 -0
+{
+    struct amdxdna_client *client = hwctx->client;
+    struct amdxdna_dev *xdna = client->xdna;
+    struct amdxdna_drv_cmd cmd = { 0 };
+    struct amdxdna_gem_obj *abo;
+    u64 seq;
+    int ret;
+
+    abo = amdxdna_gem_get_obj(client, bo_hdl, AMDXDNA_BO_DEV);
+    if (!abo) {
+        XDNA_ERR(xdna, "Get bo %d failed", bo_hdl);
+        return -EINVAL;
+    }
+
+    if (attach) {
+        if (abo->assigned_hwctx != AMDXDNA_INVALID_CTX_HANDLE) {
+            ret = -EBUSY;
+            goto put_obj;
+        }
+        cmd.opcode = ATTACH_DEBUG_BO;
+    } else {
+        if (abo->assigned_hwctx != hwctx->id) {
+            ret = -EINVAL;
+            goto put_obj;
+        }
+        cmd.opcode = DETACH_DEBUG_BO;
+    }
+
+    ret = amdxdna_cmd_submit(client, &cmd, 

AMDXDNA_INVALID_BO_HANDLE,
+                 &bo_hdl, 1, hwctx->id, &seq);
+    if (ret) {
+        XDNA_ERR(xdna, "Submit command failed");
+        goto put_obj;
+    }

drivers/accel/amdxdna/aie2_ctx.c +19 -0
+
+    aie2_cmd_wait(hwctx, seq);
+    if (cmd.result) {
+        XDNA_ERR(xdna, "Response failure 0x%x", cmd.result);
+        goto put_obj;
+    }
+
+    if (attach)
+        abo->assigned_hwctx = hwctx->id;
+    else
+        abo->assigned_hwctx = AMDXDNA_INVALID_CTX_HANDLE;
+
+    XDNA_DBG(xdna, "Config debug BO %d to %s", bo_hdl, hwctx->name);
+
+put_obj:
+    amdxdna_gem_put_obj(abo);
+    return ret;
+}
+

  int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size) {
      struct amdxdna_dev *xdna = hwctx->client->xdna;

drivers/accel/amdxdna/aie2_ctx.c +0 -0
@@ -775,14 +851,40 @@ int aie2_hwctx_config(struct amdxdna_hwctx 
 *hwctx, u32 type, u64 value, void *bu

      case DRM_AMDXDNA_HWCTX_CONFIG_CU:
          return aie2_hwctx_cu_config(hwctx, buf, size);
      case DRM_AMDXDNA_HWCTX_ASSIGN_DBG_BUF:
+        return aie2_hwctx_cfg_debug_bo(hwctx, (u32)value, true); case DRM_AMDXDNA_HWCTX_REMOVE_DBG_BUF:
-        return -EOPNOTSUPP;
+        return aie2_hwctx_cfg_debug_bo(hwctx, (u32)value, false); default:
          XDNA_DBG(xdna, "Not supported type %d", type);
          return -EOPNOTSUPP;
      }
  }
  +int aie2_hwctx_sync_debug_bo(struct amdxdna_hwctx *hwctx, u32 debug_bo_hdl)

drivers/accel/amdxdna/aie2_ctx.c +9 -0
+{
+    struct amdxdna_client *client = hwctx->client;
+    struct amdxdna_dev *xdna = client->xdna;
+    struct amdxdna_drv_cmd cmd = { 0 };
+    u64 seq;
+    int ret;
+
+    cmd.opcode = SYNC_DEBUG_BO;
+    ret = amdxdna_cmd_submit(client, &cmd, 

AMDXDNA_INVALID_BO_HANDLE,
+                 &debug_bo_hdl, 1, hwctx->id, &seq); +    if (ret) {
+        XDNA_ERR(xdna, "Submit command failed"); +        return ret;
+    }

drivers/accel/amdxdna/aie2_ctx.c +10 -0
+
+    aie2_cmd_wait(hwctx, seq);
+    if (cmd.result) {
+        XDNA_ERR(xdna, "Response failure 0x%x", cmd.result);
+        return ret;
+    }
+
+    return 0;
+}
+

  static int aie2_populate_range(struct amdxdna_gem_obj *abo) {
      struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev);

drivers/accel/amdxdna/aie2_message.c +3 -1
index 4660e8297ed8..0ec1dc6fe668 100644
@@ -749,7 +749,7 @@ int aie2_sync_bo(struct amdxdna_hwctx *hwctx, 
 struct amdxdna_sched_job *job,
       int ret = 0;
         req.src_addr = 0;
-    req.dst_addr = abo->mem.dev_addr - hwctx->client->dev_heap- 
 >mem.dev_addr;
+    req.dst_addr = amdxdna_dev_bo_offset(abo);
       req.size = abo->mem.size;
         /* Device to Host */
@@ -773,3 +773,32 @@ int aie2_sync_bo(struct amdxdna_hwctx *hwctx, 
 struct amdxdna_sched_job *job,
         return 0;
   }
+
+int aie2_config_debug_bo(struct amdxdna_hwctx *hwctx, struct 

amdxdna_sched_job *job,
+             int (*notify_cb)(void *, void __iomem *, size_t))

drivers/accel/amdxdna/aie2_message.c +26 -0
+{
+    struct mailbox_channel *chann = hwctx->priv->mbox_chann;
+    struct amdxdna_gem_obj *abo = to_xdna_obj(job->bos[0]);
+    struct amdxdna_dev *xdna = hwctx->client->xdna;
+    struct config_debug_bo_req req;
+    struct xdna_mailbox_msg msg;
+
+    if (job->drv_cmd->opcode == ATTACH_DEBUG_BO)
+        req.config = DEBUG_BO_REGISTER;
+    else
+        req.config = DEBUG_BO_UNREGISTER;
+
+    req.offset = amdxdna_dev_bo_offset(abo);
+    req.size = abo->mem.size;
+
+    XDNA_DBG(xdna, "offset 0x%llx size 0x%llx config %d",
+         req.offset, req.size, req.config);
+
+    msg.handle = job;
+    msg.notify_cb = notify_cb;
+    msg.send_data = (u8 *)&req;
+    msg.send_size = sizeof(req);
+    msg.opcode = MSG_OP_CONFIG_DEBUG_BO;
+
+    return xdna_mailbox_send_msg(chann, &msg, TX_TIMEOUT);
+}
drivers/accel/amdxdna/aie2_msg_priv.h +1 -0
index 6df9065b13f6..6a5c70bff5e9 100644
@@ -18,6 +18,7 @@ enum aie2_msg_opcode {
       MSG_OP_CONFIG_CU                   = 0x11,
       MSG_OP_CHAIN_EXEC_BUFFER_CF        = 0x12,
       MSG_OP_CHAIN_EXEC_DPU              = 0x13,
+    MSG_OP_CONFIG_DEBUG_BO             = 0x14,
       MSG_OP_MAX_XRT_OPCODE,
       MSG_OP_SUSPEND                     = 0x101,
       MSG_OP_RESUME                      = 0x102,
@@ -365,4 +366,16 @@ struct sync_bo_req {
   struct sync_bo_resp {
Quoted 8 lines
Quoted 8 lines

drivers/accel/amdxdna/aie2_msg_priv.h struct sync_bo_req { · line 374

Quoted 8 lines
drivers/accel/amdxdna/aie2_msg_priv.h +6 -0
       enum aie2_msg_status    status;
   } __packed;
+
+struct config_debug_bo_req {
+    __u64    offset;
+    __u64    size;
+#define DEBUG_BO_UNREGISTER 0
+#define DEBUG_BO_REGISTER   1

Why are these defines placed in the middle of the struct?  Is it because they're indicating the options for 'config'?

Quoted 1 line

This seems like a weird place.  I think they should be placed before the struct and instead have a comment indicating they're possible options.

Yes, it indicates the options for 'config'. And I will move it out and add comment when I merge the change.

Thanks,

Lizhi

Quoted 250 lines
Quoted 249 lines
Quoted 249 lines
Quoted 248 lines
drivers/accel/amdxdna/aie2_msg_priv.h +6 -0
+    __u32    config;
+} __packed;
+
+struct config_debug_bo_resp {
+    enum aie2_msg_status    status;
+} __packed;
   #endif /* _AIE2_MSG_PRIV_H_ */
drivers/accel/amdxdna/aie2_pci.c +1 -0
index cfca4e456b61..f48045318dc0 100644
@@ -1004,6 +1004,7 @@ const struct amdxdna_dev_ops aie2_ops = {
       .hwctx_init = aie2_hwctx_init,
       .hwctx_fini = aie2_hwctx_fini,
       .hwctx_config = aie2_hwctx_config,
+    .hwctx_sync_debug_bo = aie2_hwctx_sync_debug_bo,
       .cmd_submit = aie2_cmd_submit,
       .hmm_invalidate = aie2_hmm_invalidate,
       .get_array = aie2_get_array,
drivers/accel/amdxdna/aie2_pci.h +3 -0
index 34bc35479f42..243ac21d50c1 100644
@@ -287,11 +287,14 @@ int aie2_cmdlist_multi_execbuf(struct 
 amdxdna_hwctx *hwctx,
                      int (*notify_cb)(void *, void __iomem *, size_t));
   int aie2_sync_bo(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job,
            int (*notify_cb)(void *, void __iomem *, size_t));
+int aie2_config_debug_bo(struct amdxdna_hwctx *hwctx, struct 
 amdxdna_sched_job *job,
+             int (*notify_cb)(void *, void __iomem *, size_t));
     /* aie2_hwctx.c */
   int aie2_hwctx_init(struct amdxdna_hwctx *hwctx);
   void aie2_hwctx_fini(struct amdxdna_hwctx *hwctx);
   int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size);
+int aie2_hwctx_sync_debug_bo(struct amdxdna_hwctx *hwctx, u32 
 debug_bo_hdl);
   void aie2_hwctx_suspend(struct amdxdna_client *client);

  int aie2_hwctx_resume(struct amdxdna_client *client);
  int aie2_cmd_submit(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, u64 *seq);

drivers/accel/amdxdna/amdxdna_ctx.c +0 -0
index 868ca369e0a0..d18182c59668 100644
@@ -328,6 +328,38 @@ int amdxdna_drm_config_hwctx_ioctl(struct 

drm_device *dev, void *data, struct dr
      return ret;
  }
  +int amdxdna_hwctx_sync_debug_bo(struct amdxdna_client *client, u32 debug_bo_hdl)

drivers/accel/amdxdna/amdxdna_ctx.c +24 -0
+{
+    struct amdxdna_dev *xdna = client->xdna;
+    struct amdxdna_hwctx *hwctx;
+    struct amdxdna_gem_obj *abo;
+    struct drm_gem_object *gobj;
+    int ret, idx;
+
+    if (!xdna->dev_info->ops->hwctx_sync_debug_bo)
+        return -EOPNOTSUPP;
+
+    gobj = drm_gem_object_lookup(client->filp, debug_bo_hdl);
+    if (!gobj)
+        return -EINVAL;
+
+    abo = to_xdna_obj(gobj);
+    guard(mutex)(&xdna->dev_lock);
+    idx = srcu_read_lock(&client->hwctx_srcu);
+    hwctx = xa_load(&client->hwctx_xa, abo->assigned_hwctx);
+    if (!hwctx) {
+        ret = -EINVAL;
+        goto unlock_srcu;
+    }
+
+    ret = xdna->dev_info->ops->hwctx_sync_debug_bo(hwctx, 

debug_bo_hdl);

drivers/accel/amdxdna/amdxdna_ctx.c +7 -0
+
+unlock_srcu:
+    srcu_read_unlock(&client->hwctx_srcu, idx);
+    drm_gem_object_put(gobj);
+    return ret;
+}
+

  static void
  amdxdna_arg_bos_put(struct amdxdna_sched_job *job) {

drivers/accel/amdxdna/amdxdna_ctx.c +2 -0
@@ -393,6 +425,7 @@ void amdxdna_sched_job_cleanup(struct 
 amdxdna_sched_job *job)
   }
     int amdxdna_cmd_submit(struct amdxdna_client *client,
+               struct amdxdna_drv_cmd *drv_cmd,
                  u32 cmd_bo_hdl, u32 *arg_bo_hdls, u32 arg_bo_cnt,
                  u32 hwctx_hdl, u64 *seq)
   {
@@ -406,6 +439,8 @@ int amdxdna_cmd_submit(struct amdxdna_client 
 *client,
       if (!job)
           return -ENOMEM;
   +    job->drv_cmd = drv_cmd;
+
       if (cmd_bo_hdl != AMDXDNA_INVALID_BO_HANDLE) {
           job->cmd_bo = amdxdna_gem_get_obj(client, cmd_bo_hdl, AMDXDNA_BO_CMD);

          if (!job->cmd_bo) {

drivers/accel/amdxdna/amdxdna_ctx.c +1 -2
@@ -413,8 +448,6 @@ int amdxdna_cmd_submit(struct amdxdna_client 
 *client,
               ret = -EINVAL;
               goto free_job;
           }
-    } else {
-        job->cmd_bo = NULL;
       }
         ret = amdxdna_arg_bos_lookup(client, job, arg_bo_hdls, arg_bo_cnt);
@@ -508,7 +541,7 @@ static int amdxdna_drm_submit_execbuf(struct 
 amdxdna_client *client,
           }
       }
   -    ret = amdxdna_cmd_submit(client, cmd_bo_hdl, arg_bo_hdls,
+    ret = amdxdna_cmd_submit(client, NULL, cmd_bo_hdl, arg_bo_hdls,
                    args->arg_count, args->hwctx, &args->seq);
       if (ret)

          XDNA_DBG(xdna, "Submit cmds failed, ret %d", ret);

drivers/accel/amdxdna/amdxdna_ctx.h +14 -1
index 7cd7a55936f0..cbe60efbe60b 100644
@@ -95,6 +95,17 @@ struct amdxdna_hwctx {
   #define drm_job_to_xdna_job(j) \
       container_of(j, struct amdxdna_sched_job, base)
   +enum amdxdna_job_opcode {
+    SYNC_DEBUG_BO,
+    ATTACH_DEBUG_BO,
+    DETACH_DEBUG_BO,
+};
+
+struct amdxdna_drv_cmd {
+    enum amdxdna_job_opcode    opcode;
+    u32            result;
+};
+
   struct amdxdna_sched_job {
       struct drm_sched_job    base;
       struct kref        refcnt;
@@ -106,6 +117,7 @@ struct amdxdna_sched_job {
       struct dma_fence    *out_fence;
       bool            job_done;
       u64            seq;
+    struct amdxdna_drv_cmd    *drv_cmd;
       struct amdxdna_gem_obj    *cmd_bo;
       size_t            bo_cnt;
       struct drm_gem_object    *bos[] __counted_by(bo_cnt);
@@ -143,9 +155,11 @@ void amdxdna_sched_job_cleanup(struct 
 amdxdna_sched_job *job);
   void amdxdna_hwctx_remove_all(struct amdxdna_client *client);
   int amdxdna_hwctx_walk(struct amdxdna_client *client, void *arg,
                  int (*walk)(struct amdxdna_hwctx *hwctx, void *arg));
+int amdxdna_hwctx_sync_debug_bo(struct amdxdna_client *client, 
 u32 debug_bo_hdl);
     int amdxdna_cmd_submit(struct amdxdna_client *client,
-               u32 cmd_bo_hdls, u32 *arg_bo_hdls, u32 arg_bo_cnt,
+               struct amdxdna_drv_cmd *drv_cmd, u32 cmd_bo_hdls,
+               u32 *arg_bo_hdls, u32 arg_bo_cnt,
                  u32 hwctx_hdl, u64 *seq);
     int amdxdna_cmd_wait(struct amdxdna_client *client, u32 hwctx_hdl,
drivers/accel/amdxdna/amdxdna_gem.c +2 -0
index 7f91863c3f24..61e0136c21a8 100644
@@ -962,6 +962,9 @@ int amdxdna_drm_sync_bo_ioctl(struct 
 drm_device *dev,
       XDNA_DBG(xdna, "Sync bo %d offset 0x%llx, size 0x%llx\n",
            args->handle, args->offset, args->size);
   +    if (args->direction == SYNC_DIRECT_FROM_DEVICE)
+        ret = amdxdna_hwctx_sync_debug_bo(abo->client, 
 args->handle);
+

  put_obj:
      drm_gem_object_put(gobj);
      return ret;

drivers/accel/amdxdna/amdxdna_gem.h +5 -0
index ae29db94a9d3..f79fc7f3c93b 100644
@@ -7,6 +7,7 @@
   #define _AMDXDNA_GEM_H_
     #include <linux/hmm.h>
+#include "amdxdna_pci_drv.h"
     struct amdxdna_umap {
       struct vm_area_struct        *vma;
@@ -62,6 +63,11 @@ static inline void amdxdna_gem_put_obj(struct 
 amdxdna_gem_obj *abo)
       drm_gem_object_put(to_gobj(abo));
   }
   +static inline u64 amdxdna_dev_bo_offset(struct amdxdna_gem_obj *abo)
+{
+    return abo->mem.dev_addr - abo->client->dev_heap->mem.dev_addr;
+}
+
   void amdxdna_umap_put(struct amdxdna_umap *mapp);

    struct drm_gem_object *

drivers/accel/amdxdna/amdxdna_pci_drv.c +2 -1
index 696fdac8ad3c..3599e713bfcb 100644
@@ -28,9 +28,10 @@ MODULE_FIRMWARE("amdnpu/17f0_20/npu.sbin");
    * 0.0: Initial version
    * 0.1: Support getting all hardware contexts by DRM_IOCTL_AMDXDNA_GET_ARRAY
    * 0.2: Support getting last error hardware error
+ * 0.3: Support firmware debug buffer
    */
   #define AMDXDNA_DRIVER_MAJOR        0
-#define AMDXDNA_DRIVER_MINOR        2
+#define AMDXDNA_DRIVER_MINOR        3
     /*
    * Bind the driver base on (vendor_id, device_id) pair and later use the
drivers/accel/amdxdna/amdxdna_pci_drv.h +1 -0
index 626beebf730e..c99477f5e454 100644
@@ -55,6 +55,7 @@ struct amdxdna_dev_ops {
       int (*hwctx_init)(struct amdxdna_hwctx *hwctx);
       void (*hwctx_fini)(struct amdxdna_hwctx *hwctx);
       int (*hwctx_config)(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size);
+    int (*hwctx_sync_debug_bo)(struct amdxdna_hwctx *hwctx, u32 
 debug_bo_hdl);
       void (*hmm_invalidate)(struct amdxdna_gem_obj *abo, unsigned long cur_seq);
       int (*cmd_submit)(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, u64 *seq);

      int (*get_aie_info)(struct amdxdna_client *client, struct amdxdna_drm_get_info *args);

drivers/accel/amdxdna/npu1_regs.c +0 -0
index e4f6dac7d00f..10124cccb102 100644
@@ -46,6 +46,7 @@
     const struct rt_config npu1_default_rt_cfg[] = {

      { 2, 1, AIE2_RT_CFG_INIT }, /* PDI APP LOAD MODE */ +    { 4, 1, AIE2_RT_CFG_INIT }, /* Debug BO */
      { 1, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */ { 0 },
  };

drivers/accel/amdxdna/npu4_regs.c +0 -0
index 9f2e33182ec6..e1da882420ec 100644
@@ -63,6 +63,7 @@
     const struct rt_config npu4_default_rt_cfg[] = {

      { 5, 1, AIE2_RT_CFG_INIT }, /* PDI APP LOAD MODE */ +    { 10, 1, AIE2_RT_CFG_INIT }, /* DEBUG BUF */
      { 1, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */ { 2, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */ { 3, 1, AIE2_RT_CFG_CLK_GATING }, /* Clock gating on */