xref: /linux/drivers/accel/amdxdna/aie2_message.c (revision aa8a8c7e4641ebcef02dd6fa387ca334857367f4)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2023-2024, Advanced Micro Devices, Inc.
4  */
5 
6 #include <drm/amdxdna_accel.h>
7 #include <drm/drm_cache.h>
8 #include <drm/drm_device.h>
9 #include <drm/drm_gem.h>
10 #include <drm/drm_gem_shmem_helper.h>
11 #include <drm/drm_print.h>
12 #include <drm/gpu_scheduler.h>
13 #include <linux/bitfield.h>
14 #include <linux/errno.h>
15 #include <linux/pci.h>
16 #include <linux/types.h>
17 #include <linux/xarray.h>
18 
19 #include "aie.h"
20 #include "aie2_msg_priv.h"
21 #include "aie2_pci.h"
22 #include "amdxdna_ctx.h"
23 #include "amdxdna_gem.h"
24 #include "amdxdna_mailbox.h"
25 #include "amdxdna_mailbox_helper.h"
26 #include "amdxdna_pci_drv.h"
27 
28 #define EXEC_MSG_OPS(xdna)	((xdna)->dev_handle->exec_msg_ops)
29 
30 int aie2_suspend_fw(struct amdxdna_dev_hdl *ndev)
31 {
32 	DECLARE_AIE_MSG(suspend, MSG_OP_SUSPEND);
33 	int ret;
34 
35 	ret = aie_send_mgmt_msg_wait(&ndev->aie, &msg);
36 	if (ret) {
37 		XDNA_ERR(ndev->aie.xdna, "Failed to suspend fw, ret %d", ret);
38 		return ret;
39 	}
40 
41 	return aie_psp_waitmode_poll(ndev->aie.psp_hdl);
42 }
43 
44 int aie2_resume_fw(struct amdxdna_dev_hdl *ndev)
45 {
46 	DECLARE_AIE_MSG(suspend, MSG_OP_RESUME);
47 
48 	return aie_send_mgmt_msg_wait(&ndev->aie, &msg);
49 }
50 
51 int aie2_set_runtime_cfg(struct amdxdna_dev_hdl *ndev, u32 type, u64 value)
52 {
53 	DECLARE_AIE_MSG(set_runtime_cfg, MSG_OP_SET_RUNTIME_CONFIG);
54 	int ret;
55 
56 	req.type = type;
57 	req.value = value;
58 
59 	ret = aie_send_mgmt_msg_wait(&ndev->aie, &msg);
60 	if (ret) {
61 		XDNA_ERR(ndev->aie.xdna, "Failed to set runtime config, ret %d", ret);
62 		return ret;
63 	}
64 
65 	return 0;
66 }
67 
68 int aie2_get_runtime_cfg(struct amdxdna_dev_hdl *ndev, u32 type, u64 *value)
69 {
70 	DECLARE_AIE_MSG(get_runtime_cfg, MSG_OP_GET_RUNTIME_CONFIG);
71 	int ret;
72 
73 	req.type = type;
74 	ret = aie_send_mgmt_msg_wait(&ndev->aie, &msg);
75 	if (ret) {
76 		XDNA_ERR(ndev->aie.xdna, "Failed to get runtime config, ret %d", ret);
77 		return ret;
78 	}
79 
80 	*value = resp.value;
81 	return 0;
82 }
83 
84 int aie2_assign_mgmt_pasid(struct amdxdna_dev_hdl *ndev, u16 pasid)
85 {
86 	DECLARE_AIE_MSG(assign_mgmt_pasid, MSG_OP_ASSIGN_MGMT_PASID);
87 
88 	req.pasid = pasid;
89 
90 	return aie_send_mgmt_msg_wait(&ndev->aie, &msg);
91 }
92 
93 int aie2_query_aie_version(struct amdxdna_dev_hdl *ndev,
94 			   struct amdxdna_drm_query_aie_version *version)
95 {
96 	DECLARE_AIE_MSG(aie_version_info, MSG_OP_QUERY_AIE_VERSION);
97 	struct amdxdna_dev *xdna = ndev->aie.xdna;
98 	int ret;
99 
100 	ret = aie_send_mgmt_msg_wait(&ndev->aie, &msg);
101 	if (ret)
102 		return ret;
103 
104 	XDNA_DBG(xdna, "Query AIE version - major: %u minor: %u completed",
105 		 resp.major, resp.minor);
106 
107 	version->major = resp.major;
108 	version->minor = resp.minor;
109 
110 	return 0;
111 }
112 
113 int aie2_query_aie_metadata(struct amdxdna_dev_hdl *ndev,
114 			    struct amdxdna_drm_query_aie_metadata *metadata)
115 {
116 	DECLARE_AIE_MSG(aie_tile_info, MSG_OP_QUERY_AIE_TILE_INFO);
117 	int ret;
118 
119 	ret = aie_send_mgmt_msg_wait(&ndev->aie, &msg);
120 	if (ret)
121 		return ret;
122 
123 	metadata->col_size = resp.info.size;
124 	metadata->cols = resp.info.cols;
125 	metadata->rows = resp.info.rows;
126 
127 	metadata->version.major = resp.info.major;
128 	metadata->version.minor = resp.info.minor;
129 
130 	metadata->core.row_count = resp.info.core_rows;
131 	metadata->core.row_start = resp.info.core_row_start;
132 	metadata->core.dma_channel_count = resp.info.core_dma_channels;
133 	metadata->core.lock_count = resp.info.core_locks;
134 	metadata->core.event_reg_count = resp.info.core_events;
135 
136 	metadata->mem.row_count = resp.info.mem_rows;
137 	metadata->mem.row_start = resp.info.mem_row_start;
138 	metadata->mem.dma_channel_count = resp.info.mem_dma_channels;
139 	metadata->mem.lock_count = resp.info.mem_locks;
140 	metadata->mem.event_reg_count = resp.info.mem_events;
141 
142 	metadata->shim.row_count = resp.info.shim_rows;
143 	metadata->shim.row_start = resp.info.shim_row_start;
144 	metadata->shim.dma_channel_count = resp.info.shim_dma_channels;
145 	metadata->shim.lock_count = resp.info.shim_locks;
146 	metadata->shim.event_reg_count = resp.info.shim_events;
147 
148 	return 0;
149 }
150 
151 int aie2_query_firmware_version(struct amdxdna_dev_hdl *ndev,
152 				struct amdxdna_fw_ver *fw_ver)
153 {
154 	DECLARE_AIE_MSG(firmware_version, MSG_OP_GET_FIRMWARE_VERSION);
155 	int ret;
156 
157 	ret = aie_send_mgmt_msg_wait(&ndev->aie, &msg);
158 	if (ret)
159 		return ret;
160 
161 	fw_ver->major = resp.major;
162 	fw_ver->minor = resp.minor;
163 	fw_ver->sub = resp.sub;
164 	fw_ver->build = resp.build;
165 
166 	return 0;
167 }
168 
169 static int aie2_destroy_context_req(struct amdxdna_dev_hdl *ndev, u32 id)
170 {
171 	DECLARE_AIE_MSG(destroy_ctx, MSG_OP_DESTROY_CONTEXT);
172 	struct amdxdna_dev *xdna = ndev->aie.xdna;
173 	int ret;
174 
175 	req.context_id = id;
176 	ret = aie_send_mgmt_msg_wait(&ndev->aie, &msg);
177 	if (ret && ret != -ENODEV)
178 		XDNA_WARN(xdna, "Destroy context failed, ret %d", ret);
179 	else if (ret == -ENODEV)
180 		XDNA_DBG(xdna, "Destroy context: device already stopped");
181 
182 	return ret;
183 }
184 
185 static u32 aie2_get_context_priority(struct amdxdna_dev_hdl *ndev,
186 				     struct amdxdna_hwctx *hwctx)
187 {
188 	if (!AIE_FEATURE_ON(&ndev->aie, AIE2_PREEMPT))
189 		return PRIORITY_HIGH;
190 
191 	switch (hwctx->qos.priority) {
192 	case AMDXDNA_QOS_REALTIME_PRIORITY:
193 		return PRIORITY_REALTIME;
194 	case AMDXDNA_QOS_HIGH_PRIORITY:
195 		return PRIORITY_HIGH;
196 	case AMDXDNA_QOS_NORMAL_PRIORITY:
197 		return PRIORITY_NORMAL;
198 	case AMDXDNA_QOS_LOW_PRIORITY:
199 		return PRIORITY_LOW;
200 	default:
201 		return PRIORITY_HIGH;
202 	}
203 }
204 
205 int aie2_create_context(struct amdxdna_dev_hdl *ndev, struct amdxdna_hwctx *hwctx)
206 {
207 	DECLARE_AIE_MSG(create_ctx, MSG_OP_CREATE_CONTEXT);
208 	struct amdxdna_dev *xdna = ndev->aie.xdna;
209 	struct xdna_mailbox_chann_res x2i;
210 	struct xdna_mailbox_chann_res i2x;
211 	struct cq_pair *cq_pair;
212 	u32 intr_reg;
213 	int ret;
214 
215 	req.aie_type = 1;
216 	req.start_col = hwctx->start_col;
217 	req.num_col = hwctx->num_col;
218 	req.num_unused_col = hwctx->num_unused_col;
219 	req.num_cq_pairs_requested = 1;
220 	req.pasid = amdxdna_pasid_on(hwctx->client) ? hwctx->client->pasid : 0;
221 	req.context_priority = aie2_get_context_priority(ndev, hwctx);
222 
223 	ret = aie_send_mgmt_msg_wait(&ndev->aie, &msg);
224 	if (ret)
225 		return ret;
226 
227 	hwctx->fw_ctx_id = resp.context_id;
228 	if (WARN_ON_ONCE(hwctx->fw_ctx_id == -1))
229 		return -EINVAL;
230 
231 	if (ndev->force_preempt_enabled) {
232 		ret = aie2_runtime_cfg(ndev, AIE2_RT_CFG_FORCE_PREEMPT, &hwctx->fw_ctx_id);
233 		if (ret) {
234 			XDNA_ERR(xdna, "failed to enable force preempt %d", ret);
235 			goto del_ctx_req;
236 		}
237 	}
238 
239 	cq_pair = &resp.cq_pair[0];
240 	x2i.mb_head_ptr_reg = AIE2_MBOX_OFF(ndev, cq_pair->x2i_q.head_addr);
241 	x2i.mb_tail_ptr_reg = AIE2_MBOX_OFF(ndev, cq_pair->x2i_q.tail_addr);
242 	x2i.rb_start_addr   = AIE2_SRAM_OFF(ndev, cq_pair->x2i_q.buf_addr);
243 	x2i.rb_size	    = cq_pair->x2i_q.buf_size;
244 
245 	i2x.mb_head_ptr_reg = AIE2_MBOX_OFF(ndev, cq_pair->i2x_q.head_addr);
246 	i2x.mb_tail_ptr_reg = AIE2_MBOX_OFF(ndev, cq_pair->i2x_q.tail_addr);
247 	i2x.rb_start_addr   = AIE2_SRAM_OFF(ndev, cq_pair->i2x_q.buf_addr);
248 	i2x.rb_size	    = cq_pair->i2x_q.buf_size;
249 
250 	ret = pci_irq_vector(to_pci_dev(xdna->ddev.dev), resp.msix_id);
251 	if (ret == -EINVAL) {
252 		XDNA_ERR(xdna, "Alloc IRQ failed %d", ret);
253 		goto del_ctx_req;
254 	}
255 
256 	intr_reg = i2x.mb_head_ptr_reg + 4;
257 	hwctx->priv->mbox_chann = xdna_mailbox_alloc_channel(ndev->mbox);
258 	if (!hwctx->priv->mbox_chann) {
259 		XDNA_ERR(xdna, "Not able to create channel");
260 		ret = -EINVAL;
261 		goto del_ctx_req;
262 	}
263 
264 	ret = xdna_mailbox_start_channel(hwctx->priv->mbox_chann, &x2i, &i2x,
265 					 intr_reg, ret);
266 	if (ret) {
267 		XDNA_ERR(xdna, "Not able to create channel");
268 		ret = -EINVAL;
269 		goto free_channel;
270 	}
271 	ndev->hwctx_num++;
272 
273 	XDNA_DBG(xdna, "Mailbox channel irq: %d, msix_id: %d", ret, resp.msix_id);
274 	XDNA_DBG(xdna, "Created fw ctx %d pasid %d", hwctx->fw_ctx_id, hwctx->client->pasid);
275 
276 	return 0;
277 
278 free_channel:
279 	xdna_mailbox_free_channel(hwctx->priv->mbox_chann);
280 	hwctx->priv->mbox_chann = NULL;
281 del_ctx_req:
282 	aie2_destroy_context_req(ndev, hwctx->fw_ctx_id);
283 	return ret;
284 }
285 
286 int aie2_destroy_context(struct amdxdna_dev_hdl *ndev, struct amdxdna_hwctx *hwctx)
287 {
288 	struct amdxdna_dev *xdna = ndev->aie.xdna;
289 	int ret;
290 
291 	if (!hwctx->priv->mbox_chann)
292 		return 0;
293 
294 	xdna_mailbox_stop_channel(hwctx->priv->mbox_chann);
295 	ret = aie2_destroy_context_req(ndev, hwctx->fw_ctx_id);
296 	xdna_mailbox_free_channel(hwctx->priv->mbox_chann);
297 	XDNA_DBG(xdna, "Destroyed fw ctx %d", hwctx->fw_ctx_id);
298 	hwctx->priv->mbox_chann = NULL;
299 	hwctx->fw_ctx_id = -1;
300 	ndev->hwctx_num--;
301 
302 	return ret;
303 }
304 
305 static int aie2_send_host_buf_msgs(struct amdxdna_dev_hdl *ndev, u32 context_id,
306 				   u64 addr, u64 size, u32 initial_opcode)
307 {
308 	DECLARE_AIE_MSG(map_host_buffer, MSG_OP_MAP_HOST_BUFFER);
309 	struct amdxdna_dev *xdna = ndev->aie.xdna;
310 	size_t chunk_size;
311 	int ret;
312 
313 	chunk_size = xdna->dev_info->dev_mem_size;
314 	if (!size || !IS_ALIGNED(size, chunk_size)) {
315 		XDNA_ERR(xdna, "Invalid size 0x%llx for chunk 0x%lx",
316 			 size, chunk_size);
317 		return -EINVAL;
318 	}
319 
320 	msg.opcode = initial_opcode;
321 	do {
322 		req.context_id = context_id;
323 		req.buf_addr = addr;
324 		req.buf_size = chunk_size;
325 		ret = aie_send_mgmt_msg_wait(&ndev->aie, &msg);
326 		if (ret) {
327 			XDNA_ERR(xdna, "fw ctx %d addr 0x%llx size 0x%lx",
328 				 context_id, addr, chunk_size);
329 			return ret;
330 		}
331 
332 		XDNA_DBG(xdna, "fw ctx %d host buf op 0x%x addr 0x%llx size 0x%lx",
333 			 context_id, msg.opcode, addr, chunk_size);
334 
335 		addr += chunk_size;
336 		size -= chunk_size;
337 		msg.opcode = MSG_OP_ADD_HOST_BUFFER;
338 	} while (size);
339 
340 	return 0;
341 }
342 
343 int aie2_map_host_buf(struct amdxdna_dev_hdl *ndev, u32 context_id, u64 addr, u64 size)
344 {
345 	return aie2_send_host_buf_msgs(ndev, context_id, addr, size,
346 				       MSG_OP_MAP_HOST_BUFFER);
347 }
348 
349 int aie2_add_host_buf(struct amdxdna_dev_hdl *ndev, u32 context_id, u64 addr, u64 size)
350 {
351 	if (!AIE_FEATURE_ON(&ndev->aie, AIE2_ADD_HOST_BUFFER))
352 		return -EOPNOTSUPP;
353 
354 	return aie2_send_host_buf_msgs(ndev, context_id, addr, size,
355 				       MSG_OP_ADD_HOST_BUFFER);
356 }
357 
358 static int amdxdna_hwctx_col_map(struct amdxdna_hwctx *hwctx, void *arg)
359 {
360 	u32 *bitmap = arg;
361 
362 	*bitmap |= GENMASK(hwctx->start_col + hwctx->num_col - 1, hwctx->start_col);
363 
364 	return 0;
365 }
366 
367 int aie2_query_status(struct amdxdna_dev_hdl *ndev, char __user *buf,
368 		      u32 size, u32 *cols_filled)
369 {
370 	DECLARE_AIE_MSG(aie_column_info, MSG_OP_QUERY_COL_STATUS);
371 	struct amdxdna_dev *xdna = ndev->aie.xdna;
372 	u32 buf_sz, aie_bitmap = 0;
373 	struct amdxdna_client *client;
374 	dma_addr_t dma_addr;
375 	u8 *buff_addr;
376 	int ret;
377 
378 	buf_sz = ndev->aie.metadata.cols * ndev->aie.metadata.col_size;
379 	buff_addr = amdxdna_alloc_msg_buffer(xdna, &buf_sz, &dma_addr);
380 	if (IS_ERR(buff_addr))
381 		return PTR_ERR(buff_addr);
382 
383 	/* Go through each hardware context and mark the AIE columns that are active */
384 	list_for_each_entry(client, &xdna->client_list, node)
385 		amdxdna_hwctx_walk(client, &aie_bitmap, amdxdna_hwctx_col_map);
386 
387 	*cols_filled = 0;
388 	req.dump_buff_addr = dma_addr;
389 	req.dump_buff_size = buf_sz;
390 	req.num_cols = hweight32(aie_bitmap);
391 	req.aie_bitmap = aie_bitmap;
392 
393 	drm_clflush_virt_range(buff_addr, req.dump_buff_size); /* device can access */
394 	ret = aie_send_mgmt_msg_wait(&ndev->aie, &msg);
395 	if (ret) {
396 		XDNA_ERR(xdna, "Error during NPU query, status %d", ret);
397 		goto fail;
398 	}
399 
400 	XDNA_DBG(xdna, "Query NPU status completed");
401 
402 	if (buf_sz < resp.size) {
403 		ret = -EINVAL;
404 		XDNA_ERR(xdna, "Bad buffer size. Available: %u. Needs: %u", buf_sz, resp.size);
405 		goto fail;
406 	}
407 
408 	size = min(size, resp.size);
409 	if (copy_to_user(buf, buff_addr, size)) {
410 		ret = -EFAULT;
411 		XDNA_ERR(xdna, "Failed to copy NPU status to user space");
412 		goto fail;
413 	}
414 
415 	*cols_filled = aie_bitmap;
416 
417 fail:
418 	amdxdna_free_msg_buffer(xdna, buf_sz, buff_addr, dma_addr);
419 	return ret;
420 }
421 
422 int aie2_query_telemetry(struct amdxdna_dev_hdl *ndev,
423 			 char __user *buf, u32 size,
424 			 struct amdxdna_drm_query_telemetry_header *header)
425 {
426 	DECLARE_AIE_MSG(get_telemetry, MSG_OP_GET_TELEMETRY);
427 	struct amdxdna_dev *xdna = ndev->aie.xdna;
428 	dma_addr_t dma_addr;
429 	u32 buf_sz;
430 	u8 *addr;
431 	int ret;
432 
433 	if (header->type >= MAX_TELEMETRY_TYPE)
434 		return -EINVAL;
435 
436 	buf_sz = min(size, SZ_4M);
437 	addr = amdxdna_alloc_msg_buffer(xdna, &buf_sz, &dma_addr);
438 	if (IS_ERR(addr))
439 		return PTR_ERR(addr);
440 
441 	req.buf_addr = dma_addr;
442 	req.buf_size = buf_sz;
443 	req.type = header->type;
444 
445 	drm_clflush_virt_range(addr, req.buf_size); /* device can access */
446 	ret = aie_send_mgmt_msg_wait(&ndev->aie, &msg);
447 	if (ret) {
448 		XDNA_ERR(xdna, "Query telemetry failed, status %d", ret);
449 		goto free_buf;
450 	}
451 
452 	if (buf_sz < resp.size) {
453 		ret = -EINVAL;
454 		XDNA_ERR(xdna, "Bad buffer size. Available: %u. Needs: %u", buf_sz, resp.size);
455 		goto free_buf;
456 	}
457 
458 	size = min(size, resp.size);
459 	if (copy_to_user(buf, addr, size)) {
460 		ret = -EFAULT;
461 		XDNA_ERR(xdna, "Failed to copy telemetry to user space");
462 		goto free_buf;
463 	}
464 
465 	header->major = resp.major;
466 	header->minor = resp.minor;
467 
468 free_buf:
469 	amdxdna_free_msg_buffer(xdna, buf_sz, addr, dma_addr);
470 	return ret;
471 }
472 
473 int aie2_register_asyn_event_msg(struct amdxdna_dev_hdl *ndev, dma_addr_t addr, u32 size,
474 				 void *handle, int (*cb)(void*, void __iomem *, size_t))
475 {
476 	struct async_event_msg_req req = { 0 };
477 	struct xdna_mailbox_msg msg = {
478 		.send_data = (u8 *)&req,
479 		.send_size = sizeof(req),
480 		.handle = handle,
481 		.opcode = MSG_OP_REGISTER_ASYNC_EVENT_MSG,
482 		.notify_cb = cb,
483 	};
484 
485 	req.buf_addr = addr;
486 	req.buf_size = size;
487 
488 	XDNA_DBG(ndev->aie.xdna, "Register addr 0x%llx size 0x%x", addr, size);
489 	return xdna_mailbox_send_msg(ndev->aie.mgmt_chann, &msg, TX_TIMEOUT);
490 }
491 
492 int aie2_config_cu(struct amdxdna_hwctx *hwctx,
493 		   int (*notify_cb)(void *, void __iomem *, size_t))
494 {
495 	struct mailbox_channel *chann = hwctx->priv->mbox_chann;
496 	struct amdxdna_dev *xdna = hwctx->client->xdna;
497 	u32 shift = xdna->dev_info->dev_mem_buf_shift;
498 	struct config_cu_req req = { 0 };
499 	struct xdna_mailbox_msg msg;
500 	struct drm_gem_object *gobj;
501 	struct amdxdna_gem_obj *abo;
502 	int i;
503 
504 	if (!chann)
505 		return -ENODEV;
506 
507 	if (!hwctx->cus)
508 		return 0;
509 
510 	if (hwctx->cus->num_cus > MAX_NUM_CUS) {
511 		XDNA_DBG(xdna, "Exceed maximum CU %d", MAX_NUM_CUS);
512 		return -EINVAL;
513 	}
514 
515 	for (i = 0; i < hwctx->cus->num_cus; i++) {
516 		struct amdxdna_cu_config *cu = &hwctx->cus->cu_configs[i];
517 
518 		if (XDNA_MBZ_DBG(xdna, cu->pad, sizeof(cu->pad)))
519 			return -EINVAL;
520 
521 		gobj = drm_gem_object_lookup(hwctx->client->filp, cu->cu_bo);
522 		if (!gobj) {
523 			XDNA_ERR(xdna, "Lookup GEM object failed");
524 			return -EINVAL;
525 		}
526 		abo = to_xdna_obj(gobj);
527 
528 		if (abo->type != AMDXDNA_BO_DEV) {
529 			drm_gem_object_put(gobj);
530 			XDNA_ERR(xdna, "Invalid BO type");
531 			return -EINVAL;
532 		}
533 
534 		req.cfgs[i] = FIELD_PREP(AIE2_MSG_CFG_CU_PDI_ADDR,
535 					 amdxdna_gem_dev_addr(abo) >> shift);
536 		req.cfgs[i] |= FIELD_PREP(AIE2_MSG_CFG_CU_FUNC, cu->cu_func);
537 		XDNA_DBG(xdna, "CU %d full addr 0x%llx, cfg 0x%x", i,
538 			 amdxdna_gem_dev_addr(abo), req.cfgs[i]);
539 		drm_gem_object_put(gobj);
540 	}
541 	req.num_cus = hwctx->cus->num_cus;
542 
543 	msg.send_data = (u8 *)&req;
544 	msg.send_size = sizeof(req);
545 	msg.handle = hwctx;
546 	msg.opcode = MSG_OP_CONFIG_CU;
547 	msg.notify_cb = notify_cb;
548 	return xdna_mailbox_send_msg(chann, &msg, TX_TIMEOUT);
549 }
550 
551 static int aie2_init_exec_cu_req(struct amdxdna_gem_obj *cmd_bo, void *req,
552 				 size_t *size, u32 *msg_op)
553 {
554 	struct execute_buffer_req *cu_req = req;
555 	u32 cmd_len;
556 	void *cmd;
557 
558 	cmd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
559 	if (cmd_len > sizeof(cu_req->payload))
560 		return -EINVAL;
561 
562 	cu_req->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo);
563 	if (cu_req->cu_idx == INVALID_CU_IDX)
564 		return -EINVAL;
565 
566 	memcpy(cu_req->payload, cmd, cmd_len);
567 
568 	*size = sizeof(*cu_req);
569 	*msg_op = MSG_OP_EXECUTE_BUFFER_CF;
570 	return 0;
571 }
572 
573 static int aie2_init_exec_dpu_req(struct amdxdna_gem_obj *cmd_bo, void *req,
574 				  size_t *size, u32 *msg_op)
575 {
576 	struct exec_dpu_req *dpu_req = req;
577 	struct amdxdna_cmd_start_npu *sn;
578 	u32 cmd_len;
579 
580 	sn = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
581 	if (cmd_len - sizeof(*sn) > sizeof(dpu_req->payload))
582 		return -EINVAL;
583 
584 	dpu_req->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo);
585 	if (dpu_req->cu_idx == INVALID_CU_IDX)
586 		return -EINVAL;
587 
588 	dpu_req->inst_buf_addr = sn->buffer;
589 	dpu_req->inst_size = sn->buffer_size;
590 	dpu_req->inst_prop_cnt = sn->prop_count;
591 	memcpy(dpu_req->payload, sn->prop_args, cmd_len - sizeof(*sn));
592 
593 	*size = sizeof(*dpu_req);
594 	*msg_op = MSG_OP_EXEC_DPU;
595 	return 0;
596 }
597 
598 static void aie2_init_exec_chain_req(void *req, u64 slot_addr, size_t size, u32 cmd_cnt)
599 {
600 	struct cmd_chain_req *chain_req = req;
601 
602 	chain_req->buf_addr = slot_addr;
603 	chain_req->buf_size = size;
604 	chain_req->count = cmd_cnt;
605 }
606 
607 static void aie2_init_npu_chain_req(void *req, u64 slot_addr, size_t size, u32 cmd_cnt)
608 {
609 	struct cmd_chain_npu_req *npu_chain_req = req;
610 
611 	npu_chain_req->flags = 0;
612 	npu_chain_req->reserved = 0;
613 	npu_chain_req->buf_addr = slot_addr;
614 	npu_chain_req->buf_size = size;
615 	npu_chain_req->count = cmd_cnt;
616 }
617 
618 static int
619 aie2_cmdlist_fill_cf(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *size)
620 {
621 	struct cmd_chain_slot_execbuf_cf *cf_slot = slot;
622 	u32 cmd_len;
623 	void *cmd;
624 
625 	cmd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
626 	if (*size < sizeof(*cf_slot) + cmd_len)
627 		return -EINVAL;
628 
629 	cf_slot->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo);
630 	if (cf_slot->cu_idx == INVALID_CU_IDX)
631 		return -EINVAL;
632 
633 	cf_slot->arg_cnt = cmd_len / sizeof(u32);
634 	memcpy(cf_slot->args, cmd, cmd_len);
635 	/* Accurate slot size to hint firmware to do necessary copy */
636 	*size = sizeof(*cf_slot) + cmd_len;
637 	return 0;
638 }
639 
640 static int
641 aie2_cmdlist_fill_dpu(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *size)
642 {
643 	struct cmd_chain_slot_dpu *dpu_slot = slot;
644 	struct amdxdna_cmd_start_npu *sn;
645 	u32 cmd_len;
646 	u32 arg_sz;
647 
648 	sn = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
649 	arg_sz = cmd_len - sizeof(*sn);
650 	if (cmd_len < sizeof(*sn) || arg_sz > MAX_DPU_ARGS_SIZE)
651 		return -EINVAL;
652 
653 	if (*size < sizeof(*dpu_slot) + arg_sz)
654 		return -EINVAL;
655 
656 	dpu_slot->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo);
657 	if (dpu_slot->cu_idx == INVALID_CU_IDX)
658 		return -EINVAL;
659 
660 	dpu_slot->inst_buf_addr = sn->buffer;
661 	dpu_slot->inst_size = sn->buffer_size;
662 	dpu_slot->inst_prop_cnt = sn->prop_count;
663 	dpu_slot->arg_cnt = arg_sz / sizeof(u32);
664 	memcpy(dpu_slot->args, sn->prop_args, arg_sz);
665 
666 	/* Accurate slot size to hint firmware to do necessary copy */
667 	*size = sizeof(*dpu_slot) + arg_sz;
668 	return 0;
669 }
670 
671 static int aie2_cmdlist_unsupp(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *size)
672 {
673 	return -EOPNOTSUPP;
674 }
675 
676 static u32 aie2_get_chain_msg_op(u32 cmd_op)
677 {
678 	switch (cmd_op) {
679 	case ERT_START_CU:
680 		return MSG_OP_CHAIN_EXEC_BUFFER_CF;
681 	case ERT_START_NPU:
682 		return MSG_OP_CHAIN_EXEC_DPU;
683 	default:
684 		break;
685 	}
686 
687 	return MSG_OP_MAX_OPCODE;
688 }
689 
690 static struct aie2_exec_msg_ops legacy_exec_message_ops = {
691 	.init_cu_req = aie2_init_exec_cu_req,
692 	.init_dpu_req = aie2_init_exec_dpu_req,
693 	.init_chain_req = aie2_init_exec_chain_req,
694 	.fill_cf_slot = aie2_cmdlist_fill_cf,
695 	.fill_dpu_slot = aie2_cmdlist_fill_dpu,
696 	.fill_preempt_slot = aie2_cmdlist_unsupp,
697 	.fill_elf_slot = aie2_cmdlist_unsupp,
698 	.get_chain_msg_op = aie2_get_chain_msg_op,
699 };
700 
701 static int
702 aie2_cmdlist_fill_npu_cf(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *size)
703 {
704 	struct cmd_chain_slot_npu *npu_slot = slot;
705 	u32 cmd_len;
706 	void *cmd;
707 
708 	cmd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
709 	if (*size < sizeof(*npu_slot) + cmd_len)
710 		return -EINVAL;
711 
712 	memset(npu_slot, 0, sizeof(*npu_slot));
713 	npu_slot->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo);
714 	if (npu_slot->cu_idx == INVALID_CU_IDX)
715 		return -EINVAL;
716 
717 	npu_slot->type = EXEC_NPU_TYPE_NON_ELF;
718 	npu_slot->arg_cnt = cmd_len / sizeof(u32);
719 	memcpy(npu_slot->args, cmd, cmd_len);
720 
721 	*size = sizeof(*npu_slot) + cmd_len;
722 	return 0;
723 }
724 
725 static int
726 aie2_cmdlist_fill_npu_dpu(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *size)
727 {
728 	struct cmd_chain_slot_npu *npu_slot = slot;
729 	struct amdxdna_cmd_start_npu *sn;
730 	u32 cmd_len;
731 	u32 arg_sz;
732 
733 	sn = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
734 	arg_sz = cmd_len - sizeof(*sn);
735 	if (cmd_len < sizeof(*sn) || arg_sz > MAX_NPU_ARGS_SIZE)
736 		return -EINVAL;
737 
738 	if (*size < sizeof(*npu_slot) + arg_sz)
739 		return -EINVAL;
740 
741 	memset(npu_slot, 0, sizeof(*npu_slot));
742 	npu_slot->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo);
743 	if (npu_slot->cu_idx == INVALID_CU_IDX)
744 		return -EINVAL;
745 
746 	npu_slot->type = EXEC_NPU_TYPE_PARTIAL_ELF;
747 	npu_slot->inst_buf_addr = sn->buffer;
748 	npu_slot->inst_size = sn->buffer_size;
749 	npu_slot->inst_prop_cnt = sn->prop_count;
750 	npu_slot->arg_cnt = arg_sz / sizeof(u32);
751 	memcpy(npu_slot->args, sn->prop_args, arg_sz);
752 
753 	*size = sizeof(*npu_slot) + arg_sz;
754 	return 0;
755 }
756 
757 static int
758 aie2_cmdlist_fill_npu_preempt(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *size)
759 {
760 	struct cmd_chain_slot_npu *npu_slot = slot;
761 	struct amdxdna_cmd_preempt_data *pd;
762 	u32 cmd_len;
763 	u32 arg_sz;
764 
765 	pd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
766 	arg_sz = cmd_len - sizeof(*pd);
767 	if (cmd_len < sizeof(*pd) || arg_sz > MAX_NPU_ARGS_SIZE)
768 		return -EINVAL;
769 
770 	if (*size < sizeof(*npu_slot) + arg_sz)
771 		return -EINVAL;
772 
773 	memset(npu_slot, 0, sizeof(*npu_slot));
774 	npu_slot->cu_idx = amdxdna_cmd_get_cu_idx(cmd_bo);
775 	if (npu_slot->cu_idx == INVALID_CU_IDX)
776 		return -EINVAL;
777 
778 	npu_slot->type = EXEC_NPU_TYPE_PREEMPT;
779 	npu_slot->inst_buf_addr = pd->inst_buf;
780 	npu_slot->save_buf_addr = pd->save_buf;
781 	npu_slot->restore_buf_addr = pd->restore_buf;
782 	npu_slot->inst_size = pd->inst_size;
783 	npu_slot->save_size = pd->save_size;
784 	npu_slot->restore_size = pd->restore_size;
785 	npu_slot->inst_prop_cnt = pd->inst_prop_cnt;
786 	npu_slot->arg_cnt = arg_sz / sizeof(u32);
787 	memcpy(npu_slot->args, pd->prop_args, arg_sz);
788 
789 	*size = sizeof(*npu_slot) + arg_sz;
790 	return 0;
791 }
792 
793 static int
794 aie2_cmdlist_fill_npu_elf(struct amdxdna_gem_obj *cmd_bo, void *slot, size_t *size)
795 {
796 	struct cmd_chain_slot_npu *npu_slot = slot;
797 	struct amdxdna_cmd_preempt_data *pd;
798 	u32 cmd_len;
799 	u32 arg_sz;
800 
801 	pd = amdxdna_cmd_get_payload(cmd_bo, &cmd_len);
802 	arg_sz = cmd_len - sizeof(*pd);
803 	if (cmd_len < sizeof(*pd) || arg_sz > MAX_NPU_ARGS_SIZE)
804 		return -EINVAL;
805 
806 	if (*size < sizeof(*npu_slot) + arg_sz)
807 		return -EINVAL;
808 
809 	memset(npu_slot, 0, sizeof(*npu_slot));
810 	npu_slot->type = EXEC_NPU_TYPE_ELF;
811 	npu_slot->inst_buf_addr = pd->inst_buf;
812 	npu_slot->save_buf_addr = pd->save_buf;
813 	npu_slot->restore_buf_addr = pd->restore_buf;
814 	npu_slot->inst_size = pd->inst_size;
815 	npu_slot->save_size = pd->save_size;
816 	npu_slot->restore_size = pd->restore_size;
817 	npu_slot->inst_prop_cnt = pd->inst_prop_cnt;
818 	npu_slot->arg_cnt = 1;
819 	npu_slot->args[0] = AIE2_EXEC_BUFFER_KERNEL_OP_TXN;
820 
821 	*size = struct_size(npu_slot, args, npu_slot->arg_cnt);
822 	return 0;
823 }
824 
825 static u32 aie2_get_npu_chain_msg_op(u32 cmd_op)
826 {
827 	return MSG_OP_CHAIN_EXEC_NPU;
828 }
829 
830 static struct aie2_exec_msg_ops npu_exec_message_ops = {
831 	.init_cu_req = aie2_init_exec_cu_req,
832 	.init_dpu_req = aie2_init_exec_dpu_req,
833 	.init_chain_req = aie2_init_npu_chain_req,
834 	.fill_cf_slot = aie2_cmdlist_fill_npu_cf,
835 	.fill_dpu_slot = aie2_cmdlist_fill_npu_dpu,
836 	.fill_preempt_slot = aie2_cmdlist_fill_npu_preempt,
837 	.fill_elf_slot = aie2_cmdlist_fill_npu_elf,
838 	.get_chain_msg_op = aie2_get_npu_chain_msg_op,
839 };
840 
841 static int aie2_init_exec_req(void *req, struct amdxdna_gem_obj *cmd_abo,
842 			      size_t *size, u32 *msg_op)
843 {
844 	struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(cmd_abo)->dev);
845 	int ret;
846 	u32 op;
847 
848 	op = amdxdna_cmd_get_op(cmd_abo);
849 	switch (op) {
850 	case ERT_START_CU:
851 		ret = EXEC_MSG_OPS(xdna)->init_cu_req(cmd_abo, req, size, msg_op);
852 		if (ret) {
853 			XDNA_DBG(xdna, "Init CU req failed ret %d", ret);
854 			return ret;
855 		}
856 		break;
857 	case ERT_START_NPU:
858 		ret = EXEC_MSG_OPS(xdna)->init_dpu_req(cmd_abo, req, size, msg_op);
859 		if (ret) {
860 			XDNA_DBG(xdna, "Init DPU req failed ret %d", ret);
861 			return ret;
862 		}
863 
864 		break;
865 	default:
866 		XDNA_ERR(xdna, "Unsupported op %d", op);
867 		ret = -EOPNOTSUPP;
868 		break;
869 	}
870 
871 	return ret;
872 }
873 
874 static int
875 aie2_cmdlist_fill_slot(void *slot, struct amdxdna_gem_obj *cmd_abo,
876 		       size_t *size, u32 *cmd_op)
877 {
878 	struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(cmd_abo)->dev);
879 	int ret;
880 	u32 op;
881 
882 	op = amdxdna_cmd_get_op(cmd_abo);
883 	if (*cmd_op == ERT_INVALID_CMD)
884 		*cmd_op = op;
885 	else if (op != *cmd_op)
886 		return -EINVAL;
887 
888 	switch (op) {
889 	case ERT_START_CU:
890 		ret = EXEC_MSG_OPS(xdna)->fill_cf_slot(cmd_abo, slot, size);
891 		break;
892 	case ERT_START_NPU:
893 		ret = EXEC_MSG_OPS(xdna)->fill_dpu_slot(cmd_abo, slot, size);
894 		break;
895 	case ERT_START_NPU_PREEMPT:
896 		if (!AIE_FEATURE_ON(&xdna->dev_handle->aie, AIE2_PREEMPT))
897 			return -EOPNOTSUPP;
898 		ret = EXEC_MSG_OPS(xdna)->fill_preempt_slot(cmd_abo, slot, size);
899 		break;
900 	case ERT_START_NPU_PREEMPT_ELF:
901 		if (!AIE_FEATURE_ON(&xdna->dev_handle->aie, AIE2_PREEMPT))
902 			return -EOPNOTSUPP;
903 		ret = EXEC_MSG_OPS(xdna)->fill_elf_slot(cmd_abo, slot, size);
904 		break;
905 	default:
906 		XDNA_INFO(xdna, "Unsupported op %d", op);
907 		ret = -EOPNOTSUPP;
908 		break;
909 	}
910 
911 	return ret;
912 }
913 
914 void aie2_msg_init(struct amdxdna_dev_hdl *ndev)
915 {
916 	if (AIE_FEATURE_ON(&ndev->aie, AIE2_NPU_COMMAND))
917 		ndev->exec_msg_ops = &npu_exec_message_ops;
918 	else
919 		ndev->exec_msg_ops = &legacy_exec_message_ops;
920 }
921 
922 static inline struct amdxdna_gem_obj *
923 aie2_cmdlist_get_cmd_buf(struct amdxdna_sched_job *job)
924 {
925 	int idx = get_job_idx(job->seq);
926 
927 	return job->hwctx->priv->cmd_buf[idx];
928 }
929 
930 int aie2_execbuf(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job,
931 		 int (*notify_cb)(void *, void __iomem *, size_t))
932 {
933 	struct mailbox_channel *chann = hwctx->priv->mbox_chann;
934 	struct amdxdna_dev *xdna = hwctx->client->xdna;
935 	struct amdxdna_gem_obj *cmd_abo = job->cmd_bo;
936 	struct xdna_mailbox_msg msg;
937 	union exec_req req;
938 	int ret;
939 
940 	if (!chann)
941 		return -ENODEV;
942 
943 	ret = aie2_init_exec_req(&req, cmd_abo, &msg.send_size, &msg.opcode);
944 	if (ret)
945 		return ret;
946 
947 	msg.handle = job;
948 	msg.notify_cb = notify_cb;
949 	msg.send_data = (u8 *)&req;
950 	print_hex_dump_debug("cmd: ", DUMP_PREFIX_OFFSET, 16, 4, &req,
951 			     0x40, false);
952 
953 	ret = xdna_mailbox_send_msg(chann, &msg, TX_TIMEOUT);
954 	if (ret) {
955 		XDNA_ERR(xdna, "Send message failed");
956 		return ret;
957 	}
958 
959 	return 0;
960 }
961 
962 int aie2_cmdlist_multi_execbuf(struct amdxdna_hwctx *hwctx,
963 			       struct amdxdna_sched_job *job,
964 			       int (*notify_cb)(void *, void __iomem *, size_t))
965 {
966 	struct amdxdna_gem_obj *cmdbuf_abo = aie2_cmdlist_get_cmd_buf(job);
967 	struct mailbox_channel *chann = hwctx->priv->mbox_chann;
968 	struct amdxdna_client *client = hwctx->client;
969 	struct amdxdna_gem_obj *cmd_abo = job->cmd_bo;
970 	void *cmd_buf = amdxdna_gem_vmap(cmdbuf_abo);
971 	struct amdxdna_dev *xdna = client->xdna;
972 	struct amdxdna_cmd_chain *payload;
973 	struct xdna_mailbox_msg msg;
974 	union exec_chain_req req;
975 	u32 payload_len, ccnt;
976 	u32 offset = 0;
977 	size_t size;
978 	int ret;
979 	u32 op;
980 	u32 i;
981 
982 	if (!cmd_buf)
983 		return -ENOMEM;
984 
985 	op = amdxdna_cmd_get_op(cmd_abo);
986 	payload = amdxdna_cmd_get_payload(cmd_abo, &payload_len);
987 	if (op != ERT_CMD_CHAIN) {
988 		XDNA_DBG(xdna, "Invalid op code %d", op);
989 		return -EINVAL;
990 	}
991 
992 	if (!payload) {
993 		XDNA_DBG(xdna, "Failed to get command payload");
994 		return -EINVAL;
995 	}
996 
997 	ccnt = payload->command_count;
998 	if (!ccnt || payload_len < struct_size(payload, data, ccnt)) {
999 		XDNA_DBG(xdna, "Invalid command count %d", ccnt);
1000 		return -EINVAL;
1001 	}
1002 
1003 	op = ERT_INVALID_CMD;
1004 	for (i = 0; i < ccnt; i++) {
1005 		u32 boh = (u32)(payload->data[i]);
1006 		struct amdxdna_gem_obj *abo;
1007 
1008 		abo = amdxdna_gem_get_obj(client, boh, AMDXDNA_BO_SHARE);
1009 		if (!abo) {
1010 			XDNA_ERR(xdna, "Failed to find cmd BO %d", boh);
1011 			return -ENOENT;
1012 		}
1013 
1014 		size = cmdbuf_abo->mem.size - offset;
1015 		ret = aie2_cmdlist_fill_slot(cmd_buf + offset, abo, &size, &op);
1016 		amdxdna_gem_put_obj(abo);
1017 		if (ret)
1018 			return ret;
1019 
1020 		offset += size;
1021 	}
1022 
1023 	XDNA_DBG(xdna, "Total %d commands:", ccnt);
1024 	print_hex_dump_debug("cmdbufs: ", DUMP_PREFIX_OFFSET, 16, 4,
1025 			     cmd_buf, offset, false);
1026 
1027 	msg.opcode = EXEC_MSG_OPS(xdna)->get_chain_msg_op(op);
1028 	if (msg.opcode == MSG_OP_MAX_OPCODE)
1029 		return -EOPNOTSUPP;
1030 
1031 	/* The offset is the accumulated total size of the cmd buffer */
1032 	EXEC_MSG_OPS(xdna)->init_chain_req(&req, amdxdna_gem_dev_addr(cmdbuf_abo),
1033 					   offset, ccnt);
1034 	drm_clflush_virt_range(cmd_buf, offset);
1035 
1036 	msg.handle = job;
1037 	msg.notify_cb = notify_cb;
1038 	msg.send_data = (u8 *)&req;
1039 	msg.send_size = sizeof(req);
1040 	print_hex_dump_debug("cmdlist msg: ", DUMP_PREFIX_OFFSET, 16, 4,
1041 			     &req, msg.send_size, false);
1042 	ret = xdna_mailbox_send_msg(chann, &msg, TX_TIMEOUT);
1043 	if (ret) {
1044 		XDNA_ERR(xdna, "Send message failed");
1045 		return ret;
1046 	}
1047 
1048 	return 0;
1049 }
1050 
1051 int aie2_cmdlist_single_execbuf(struct amdxdna_hwctx *hwctx,
1052 				struct amdxdna_sched_job *job,
1053 				int (*notify_cb)(void *, void __iomem *, size_t))
1054 {
1055 	struct amdxdna_gem_obj *cmdbuf_abo = aie2_cmdlist_get_cmd_buf(job);
1056 	struct mailbox_channel *chann = hwctx->priv->mbox_chann;
1057 	struct amdxdna_dev *xdna = hwctx->client->xdna;
1058 	struct amdxdna_gem_obj *cmd_abo = job->cmd_bo;
1059 	void *cmd_buf = amdxdna_gem_vmap(cmdbuf_abo);
1060 	struct xdna_mailbox_msg msg;
1061 	union exec_chain_req req;
1062 	u32 op = ERT_INVALID_CMD;
1063 	size_t size;
1064 	int ret;
1065 
1066 	if (!cmd_buf)
1067 		return -ENOMEM;
1068 
1069 	size = cmdbuf_abo->mem.size;
1070 	ret = aie2_cmdlist_fill_slot(cmd_buf, cmd_abo, &size, &op);
1071 	if (ret)
1072 		return ret;
1073 
1074 	print_hex_dump_debug("cmdbuf: ", DUMP_PREFIX_OFFSET, 16, 4, cmd_buf, size, false);
1075 
1076 	msg.opcode = EXEC_MSG_OPS(xdna)->get_chain_msg_op(op);
1077 	if (msg.opcode == MSG_OP_MAX_OPCODE)
1078 		return -EOPNOTSUPP;
1079 
1080 	EXEC_MSG_OPS(xdna)->init_chain_req(&req, amdxdna_gem_dev_addr(cmdbuf_abo), size, 1);
1081 	drm_clflush_virt_range(cmd_buf, size);
1082 
1083 	msg.handle = job;
1084 	msg.notify_cb = notify_cb;
1085 	msg.send_data = (u8 *)&req;
1086 	msg.send_size = sizeof(req);
1087 	print_hex_dump_debug("cmdlist msg: ", DUMP_PREFIX_OFFSET, 16, 4,
1088 			     &req, msg.send_size, false);
1089 	ret = xdna_mailbox_send_msg(chann, &msg, TX_TIMEOUT);
1090 	if (ret) {
1091 		XDNA_ERR(hwctx->client->xdna, "Send message failed");
1092 		return ret;
1093 	}
1094 
1095 	return 0;
1096 }
1097 
1098 int aie2_sync_bo(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job,
1099 		 int (*notify_cb)(void *, void __iomem *, size_t))
1100 {
1101 	struct mailbox_channel *chann = hwctx->priv->mbox_chann;
1102 	struct amdxdna_gem_obj *abo = to_xdna_obj(job->bos[0]);
1103 	struct amdxdna_dev *xdna = hwctx->client->xdna;
1104 	struct xdna_mailbox_msg msg;
1105 	struct sync_bo_req req;
1106 	int ret = 0;
1107 
1108 	req.src_addr = 0;
1109 	req.dst_addr = amdxdna_dev_bo_offset(abo);
1110 	req.size = abo->mem.size;
1111 
1112 	/* Device to Host */
1113 	req.type = FIELD_PREP(AIE2_MSG_SYNC_BO_SRC_TYPE, SYNC_BO_DEV_MEM) |
1114 		FIELD_PREP(AIE2_MSG_SYNC_BO_DST_TYPE, SYNC_BO_HOST_MEM);
1115 
1116 	XDNA_DBG(xdna, "sync %d bytes src(0x%llx) to dst(0x%llx) completed",
1117 		 req.size, req.src_addr, req.dst_addr);
1118 
1119 	msg.handle = job;
1120 	msg.notify_cb = notify_cb;
1121 	msg.send_data = (u8 *)&req;
1122 	msg.send_size = sizeof(req);
1123 	msg.opcode = MSG_OP_SYNC_BO;
1124 
1125 	ret = xdna_mailbox_send_msg(chann, &msg, TX_TIMEOUT);
1126 	if (ret) {
1127 		XDNA_ERR(xdna, "Send message failed");
1128 		return ret;
1129 	}
1130 
1131 	return 0;
1132 }
1133 
1134 int aie2_config_debug_bo(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job,
1135 			 int (*notify_cb)(void *, void __iomem *, size_t))
1136 {
1137 	struct mailbox_channel *chann = hwctx->priv->mbox_chann;
1138 	struct amdxdna_gem_obj *abo = to_xdna_obj(job->bos[0]);
1139 	struct amdxdna_dev *xdna = hwctx->client->xdna;
1140 	struct config_debug_bo_req req;
1141 	struct xdna_mailbox_msg msg;
1142 
1143 	if (job->drv_cmd->opcode == ATTACH_DEBUG_BO)
1144 		req.config = DEBUG_BO_REGISTER;
1145 	else
1146 		req.config = DEBUG_BO_UNREGISTER;
1147 
1148 	req.offset = amdxdna_dev_bo_offset(abo);
1149 	req.size = abo->mem.size;
1150 
1151 	XDNA_DBG(xdna, "offset 0x%llx size 0x%llx config %d",
1152 		 req.offset, req.size, req.config);
1153 
1154 	msg.handle = job;
1155 	msg.notify_cb = notify_cb;
1156 	msg.send_data = (u8 *)&req;
1157 	msg.send_size = sizeof(req);
1158 	msg.opcode = MSG_OP_CONFIG_DEBUG_BO;
1159 
1160 	return xdna_mailbox_send_msg(chann, &msg, TX_TIMEOUT);
1161 }
1162 
1163 int aie2_query_app_health(struct amdxdna_dev_hdl *ndev, u32 context_id,
1164 			  struct app_health_report *report)
1165 {
1166 	DECLARE_AIE_MSG(get_app_health, MSG_OP_GET_APP_HEALTH);
1167 	struct amdxdna_dev *xdna = ndev->aie.xdna;
1168 	struct app_health_report *buf;
1169 	dma_addr_t dma_addr;
1170 	u32 buf_size;
1171 	int ret;
1172 
1173 	if (!AIE_FEATURE_ON(&ndev->aie, AIE2_APP_HEALTH)) {
1174 		XDNA_DBG(xdna, "App health feature not supported");
1175 		return -EOPNOTSUPP;
1176 	}
1177 
1178 	buf_size = sizeof(*report);
1179 	buf = amdxdna_alloc_msg_buffer(xdna, &buf_size, &dma_addr);
1180 	if (IS_ERR(buf)) {
1181 		XDNA_ERR(xdna, "Failed to allocate buffer for app health");
1182 		return PTR_ERR(buf);
1183 	}
1184 
1185 	req.buf_addr = dma_addr;
1186 	req.context_id = context_id;
1187 	req.buf_size = buf_size;
1188 
1189 	drm_clflush_virt_range(buf, req.buf_size);
1190 	ret = aie_send_mgmt_msg_wait(&ndev->aie, &msg);
1191 	if (ret) {
1192 		XDNA_ERR(xdna, "Get app health failed, ret %d status 0x%x", ret, resp.status);
1193 		goto free_buf;
1194 	}
1195 
1196 	/* Copy the report to caller's buffer */
1197 	memcpy(report, buf, sizeof(*report));
1198 
1199 free_buf:
1200 	amdxdna_free_msg_buffer(xdna, buf_size, buf, dma_addr);
1201 	return ret;
1202 }
1203 
1204 static int
1205 aie2_runtime_update_ctx_prop(struct amdxdna_dev_hdl *ndev,
1206 			     struct amdxdna_hwctx *ctx, u32 type, u32 value)
1207 {
1208 	DECLARE_AIE_MSG(update_property, MSG_OP_UPDATE_PROPERTY);
1209 	struct amdxdna_dev *xdna = ndev->aie.xdna;
1210 	int ret;
1211 
1212 	if (!AIE_FEATURE_ON(&ndev->aie, AIE2_UPDATE_PROPERTY))
1213 		return -EOPNOTSUPP;
1214 
1215 	if (ctx)
1216 		req.context_id = ctx->fw_ctx_id;
1217 	else
1218 		req.context_id = AIE2_UPDATE_PROPERTY_ALL_CTX;
1219 
1220 	req.time_quota_us = value;
1221 	req.type = type;
1222 
1223 	ret = aie_send_mgmt_msg_wait(&ndev->aie, &msg);
1224 	if (ret) {
1225 		XDNA_ERR(xdna, "%s update property failed, type %d ret %d",
1226 			 ctx ? ctx->name : "ctx.all", type, ret);
1227 		return ret;
1228 	}
1229 
1230 	return 0;
1231 }
1232 
1233 int aie2_update_prop_time_quota(struct amdxdna_dev_hdl *ndev, u32 us)
1234 {
1235 	struct amdxdna_dev *xdna = ndev->aie.xdna;
1236 	int ret;
1237 
1238 	ret = aie2_runtime_update_ctx_prop(ndev, NULL, UPDATE_PROPERTY_TIME_QUOTA, us);
1239 	if (ret == -EOPNOTSUPP) {
1240 		XDNA_DBG(xdna, "update time quota not support, skipped");
1241 		ret = 0;
1242 	} else if (!ret) {
1243 		XDNA_DBG(xdna, "Ctx exec time quantum updated to %u us", us);
1244 	}
1245 	return ret;
1246 }
1247 
1248 int aie2_get_dev_revision(struct amdxdna_dev_hdl *ndev, enum aie2_dev_revision *rev)
1249 {
1250 	DECLARE_AIE_MSG(get_dev_revision, MSG_OP_GET_DEV_REVISION);
1251 	struct amdxdna_dev *xdna = ndev->aie.xdna;
1252 	int ret;
1253 
1254 	if (!AIE_FEATURE_ON(&ndev->aie, AIE2_GET_DEV_REVISION))
1255 		return -EOPNOTSUPP;
1256 
1257 	ret = aie_send_mgmt_msg_wait(&ndev->aie, &msg);
1258 	if (ret)
1259 		return ret;
1260 
1261 	*rev = resp.rev;
1262 
1263 	if (*rev < AIE2_DEV_REVISION_STXA || *rev >= AIE2_DEV_REVISION_UNKN) {
1264 		XDNA_ERR(xdna, "Unknown device revision: %d (raw fuse: 0x%x)",
1265 			 *rev, resp.raw_fuse_data);
1266 		return -EINVAL;
1267 	}
1268 
1269 	XDNA_DBG(xdna, "Device revision: %d (raw fuse: 0x%x)", *rev, resp.raw_fuse_data);
1270 	return 0;
1271 }
1272