xref: /linux/drivers/accel/amdxdna/aie2_ctx.c (revision 815e260a18a3af4dab59025ee99a7156c0e8b5e0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2024, Advanced Micro Devices, Inc.
4  */
5 
6 #include <drm/amdxdna_accel.h>
7 #include <drm/drm_device.h>
8 #include <drm/drm_gem.h>
9 #include <drm/drm_gem_shmem_helper.h>
10 #include <drm/drm_print.h>
11 #include <drm/drm_syncobj.h>
12 #include <linux/hmm.h>
13 #include <linux/types.h>
14 #include <linux/xarray.h>
15 #include <trace/events/amdxdna.h>
16 
17 #include "aie2_msg_priv.h"
18 #include "aie2_pci.h"
19 #include "aie2_solver.h"
20 #include "amdxdna_ctx.h"
21 #include "amdxdna_gem.h"
22 #include "amdxdna_mailbox.h"
23 #include "amdxdna_pci_drv.h"
24 #include "amdxdna_pm.h"
25 
26 static bool force_cmdlist;
27 module_param(force_cmdlist, bool, 0600);
28 MODULE_PARM_DESC(force_cmdlist, "Force use command list (Default false)");
29 
30 #define HWCTX_MAX_TIMEOUT	60000 /* milliseconds */
31 
32 static void aie2_job_release(struct kref *ref)
33 {
34 	struct amdxdna_sched_job *job;
35 
36 	job = container_of(ref, struct amdxdna_sched_job, refcnt);
37 	amdxdna_sched_job_cleanup(job);
38 	atomic64_inc(&job->hwctx->job_free_cnt);
39 	wake_up(&job->hwctx->priv->job_free_wq);
40 	if (job->out_fence)
41 		dma_fence_put(job->out_fence);
42 	kfree(job);
43 }
44 
45 static void aie2_job_put(struct amdxdna_sched_job *job)
46 {
47 	kref_put(&job->refcnt, aie2_job_release);
48 }
49 
50 static void aie2_hwctx_status_shift_stop(struct amdxdna_hwctx *hwctx)
51 {
52 	 hwctx->old_status = hwctx->status;
53 	 hwctx->status = HWCTX_STAT_STOP;
54 }
55 
56 static void aie2_hwctx_status_restore(struct amdxdna_hwctx *hwctx)
57 {
58 	hwctx->status = hwctx->old_status;
59 }
60 
61 /* The bad_job is used in aie2_sched_job_timedout, otherwise, set it to NULL */
62 static void aie2_hwctx_stop(struct amdxdna_dev *xdna, struct amdxdna_hwctx *hwctx,
63 			    struct drm_sched_job *bad_job)
64 {
65 	drm_sched_stop(&hwctx->priv->sched, bad_job);
66 	aie2_destroy_context(xdna->dev_handle, hwctx);
67 }
68 
69 static int aie2_hwctx_restart(struct amdxdna_dev *xdna, struct amdxdna_hwctx *hwctx)
70 {
71 	struct amdxdna_gem_obj *heap = hwctx->priv->heap;
72 	int ret;
73 
74 	ret = aie2_create_context(xdna->dev_handle, hwctx);
75 	if (ret) {
76 		XDNA_ERR(xdna, "Create hwctx failed, ret %d", ret);
77 		goto out;
78 	}
79 
80 	ret = aie2_map_host_buf(xdna->dev_handle, hwctx->fw_ctx_id,
81 				heap->mem.userptr, heap->mem.size);
82 	if (ret) {
83 		XDNA_ERR(xdna, "Map host buf failed, ret %d", ret);
84 		goto out;
85 	}
86 
87 	if (hwctx->status != HWCTX_STAT_READY) {
88 		XDNA_DBG(xdna, "hwctx is not ready, status %d", hwctx->status);
89 		goto out;
90 	}
91 
92 	ret = aie2_config_cu(hwctx, NULL);
93 	if (ret) {
94 		XDNA_ERR(xdna, "Config cu failed, ret %d", ret);
95 		goto out;
96 	}
97 
98 out:
99 	drm_sched_start(&hwctx->priv->sched, 0);
100 	XDNA_DBG(xdna, "%s restarted, ret %d", hwctx->name, ret);
101 	return ret;
102 }
103 
104 static struct dma_fence *aie2_cmd_get_out_fence(struct amdxdna_hwctx *hwctx, u64 seq)
105 {
106 	struct dma_fence *fence, *out_fence = NULL;
107 	int ret;
108 
109 	fence = drm_syncobj_fence_get(hwctx->priv->syncobj);
110 	if (!fence)
111 		return NULL;
112 
113 	ret = dma_fence_chain_find_seqno(&fence,  seq);
114 	if (ret)
115 		goto out;
116 
117 	out_fence = dma_fence_get(dma_fence_chain_contained(fence));
118 
119 out:
120 	dma_fence_put(fence);
121 	return out_fence;
122 }
123 
124 static void aie2_hwctx_wait_for_idle(struct amdxdna_hwctx *hwctx)
125 {
126 	struct dma_fence *fence;
127 
128 	fence = aie2_cmd_get_out_fence(hwctx, hwctx->priv->seq - 1);
129 	if (!fence)
130 		return;
131 
132 	/* Wait up to 2 seconds for fw to finish all pending requests */
133 	dma_fence_wait_timeout(fence, false, msecs_to_jiffies(2000));
134 	dma_fence_put(fence);
135 }
136 
137 static int aie2_hwctx_suspend_cb(struct amdxdna_hwctx *hwctx, void *arg)
138 {
139 	struct amdxdna_dev *xdna = hwctx->client->xdna;
140 
141 	aie2_hwctx_wait_for_idle(hwctx);
142 	aie2_hwctx_stop(xdna, hwctx, NULL);
143 	aie2_hwctx_status_shift_stop(hwctx);
144 
145 	return 0;
146 }
147 
148 void aie2_hwctx_suspend(struct amdxdna_client *client)
149 {
150 	struct amdxdna_dev *xdna = client->xdna;
151 
152 	/*
153 	 * Command timeout is unlikely. But if it happens, it doesn't
154 	 * break the system. aie2_hwctx_stop() will destroy mailbox
155 	 * and abort all commands.
156 	 */
157 	drm_WARN_ON(&xdna->ddev, !mutex_is_locked(&xdna->dev_lock));
158 	amdxdna_hwctx_walk(client, NULL, aie2_hwctx_suspend_cb);
159 }
160 
161 static int aie2_hwctx_resume_cb(struct amdxdna_hwctx *hwctx, void *arg)
162 {
163 	struct amdxdna_dev *xdna = hwctx->client->xdna;
164 
165 	aie2_hwctx_status_restore(hwctx);
166 	return aie2_hwctx_restart(xdna, hwctx);
167 }
168 
169 int aie2_hwctx_resume(struct amdxdna_client *client)
170 {
171 	/*
172 	 * The resume path cannot guarantee that mailbox channel can be
173 	 * regenerated. If this happen, when submit message to this
174 	 * mailbox channel, error will return.
175 	 */
176 	return amdxdna_hwctx_walk(client, NULL, aie2_hwctx_resume_cb);
177 }
178 
179 static void
180 aie2_sched_notify(struct amdxdna_sched_job *job)
181 {
182 	struct dma_fence *fence = job->fence;
183 
184 	trace_xdna_job(&job->base, job->hwctx->name, "signaled fence", job->seq);
185 
186 	amdxdna_pm_suspend_put(job->hwctx->client->xdna);
187 	job->hwctx->priv->completed++;
188 	dma_fence_signal(fence);
189 
190 	up(&job->hwctx->priv->job_sem);
191 	job->job_done = true;
192 	dma_fence_put(fence);
193 	mmput_async(job->mm);
194 	aie2_job_put(job);
195 }
196 
197 static int
198 aie2_sched_resp_handler(void *handle, void __iomem *data, size_t size)
199 {
200 	struct amdxdna_sched_job *job = handle;
201 	struct amdxdna_gem_obj *cmd_abo;
202 	int ret = 0;
203 	u32 status;
204 
205 	cmd_abo = job->cmd_bo;
206 
207 	if (unlikely(job->job_timeout)) {
208 		amdxdna_cmd_set_state(cmd_abo, ERT_CMD_STATE_TIMEOUT);
209 		ret = -EINVAL;
210 		goto out;
211 	}
212 
213 	if (unlikely(!data) || unlikely(size != sizeof(u32))) {
214 		amdxdna_cmd_set_state(cmd_abo, ERT_CMD_STATE_ABORT);
215 		ret = -EINVAL;
216 		goto out;
217 	}
218 
219 	status = readl(data);
220 	XDNA_DBG(job->hwctx->client->xdna, "Resp status 0x%x", status);
221 	if (status == AIE2_STATUS_SUCCESS)
222 		amdxdna_cmd_set_state(cmd_abo, ERT_CMD_STATE_COMPLETED);
223 	else
224 		amdxdna_cmd_set_state(cmd_abo, ERT_CMD_STATE_ERROR);
225 
226 out:
227 	aie2_sched_notify(job);
228 	return ret;
229 }
230 
231 static int
232 aie2_sched_drvcmd_resp_handler(void *handle, void __iomem *data, size_t size)
233 {
234 	struct amdxdna_sched_job *job = handle;
235 	int ret = 0;
236 
237 	if (unlikely(!data))
238 		goto out;
239 
240 	if (unlikely(size != sizeof(u32))) {
241 		ret = -EINVAL;
242 		goto out;
243 	}
244 
245 	job->drv_cmd->result = readl(data);
246 
247 out:
248 	aie2_sched_notify(job);
249 	return ret;
250 }
251 
252 static int
253 aie2_sched_cmdlist_resp_handler(void *handle, void __iomem *data, size_t size)
254 {
255 	struct amdxdna_sched_job *job = handle;
256 	struct amdxdna_gem_obj *cmd_abo;
257 	struct amdxdna_dev *xdna;
258 	u32 fail_cmd_status;
259 	u32 fail_cmd_idx;
260 	u32 cmd_status;
261 	int ret = 0;
262 
263 	cmd_abo = job->cmd_bo;
264 
265 	if (unlikely(job->job_timeout)) {
266 		amdxdna_cmd_set_state(cmd_abo, ERT_CMD_STATE_TIMEOUT);
267 		ret = -EINVAL;
268 		goto out;
269 	}
270 
271 	if (unlikely(!data) || unlikely(size != sizeof(u32) * 3)) {
272 		amdxdna_cmd_set_state(cmd_abo, ERT_CMD_STATE_ABORT);
273 		ret = -EINVAL;
274 		goto out;
275 	}
276 
277 	cmd_status = readl(data + offsetof(struct cmd_chain_resp, status));
278 	xdna = job->hwctx->client->xdna;
279 	XDNA_DBG(xdna, "Status 0x%x", cmd_status);
280 	if (cmd_status == AIE2_STATUS_SUCCESS) {
281 		amdxdna_cmd_set_state(cmd_abo, ERT_CMD_STATE_COMPLETED);
282 		goto out;
283 	}
284 
285 	/* Slow path to handle error, read from ringbuf on BAR */
286 	fail_cmd_idx = readl(data + offsetof(struct cmd_chain_resp, fail_cmd_idx));
287 	fail_cmd_status = readl(data + offsetof(struct cmd_chain_resp, fail_cmd_status));
288 	XDNA_DBG(xdna, "Failed cmd idx %d, status 0x%x",
289 		 fail_cmd_idx, fail_cmd_status);
290 
291 	if (fail_cmd_status == AIE2_STATUS_SUCCESS) {
292 		amdxdna_cmd_set_state(cmd_abo, ERT_CMD_STATE_ABORT);
293 		ret = -EINVAL;
294 		goto out;
295 	}
296 	amdxdna_cmd_set_state(cmd_abo, fail_cmd_status);
297 
298 	if (amdxdna_cmd_get_op(cmd_abo) == ERT_CMD_CHAIN) {
299 		struct amdxdna_cmd_chain *cc = amdxdna_cmd_get_payload(cmd_abo, NULL);
300 
301 		cc->error_index = fail_cmd_idx;
302 		if (cc->error_index >= cc->command_count)
303 			cc->error_index = 0;
304 	}
305 out:
306 	aie2_sched_notify(job);
307 	return ret;
308 }
309 
310 static struct dma_fence *
311 aie2_sched_job_run(struct drm_sched_job *sched_job)
312 {
313 	struct amdxdna_sched_job *job = drm_job_to_xdna_job(sched_job);
314 	struct amdxdna_gem_obj *cmd_abo = job->cmd_bo;
315 	struct amdxdna_hwctx *hwctx = job->hwctx;
316 	struct dma_fence *fence;
317 	int ret;
318 
319 	if (!mmget_not_zero(job->mm))
320 		return ERR_PTR(-ESRCH);
321 
322 	kref_get(&job->refcnt);
323 	fence = dma_fence_get(job->fence);
324 
325 	if (job->drv_cmd) {
326 		switch (job->drv_cmd->opcode) {
327 		case SYNC_DEBUG_BO:
328 			ret = aie2_sync_bo(hwctx, job, aie2_sched_drvcmd_resp_handler);
329 			break;
330 		case ATTACH_DEBUG_BO:
331 			ret = aie2_config_debug_bo(hwctx, job, aie2_sched_drvcmd_resp_handler);
332 			break;
333 		default:
334 			ret = -EINVAL;
335 			break;
336 		}
337 		goto out;
338 	}
339 
340 	amdxdna_cmd_set_state(cmd_abo, ERT_CMD_STATE_NEW);
341 
342 	if (amdxdna_cmd_get_op(cmd_abo) == ERT_CMD_CHAIN)
343 		ret = aie2_cmdlist_multi_execbuf(hwctx, job, aie2_sched_cmdlist_resp_handler);
344 	else if (force_cmdlist)
345 		ret = aie2_cmdlist_single_execbuf(hwctx, job, aie2_sched_cmdlist_resp_handler);
346 	else
347 		ret = aie2_execbuf(hwctx, job, aie2_sched_resp_handler);
348 
349 out:
350 	if (ret) {
351 		dma_fence_put(job->fence);
352 		aie2_job_put(job);
353 		mmput(job->mm);
354 		fence = ERR_PTR(ret);
355 	}
356 	trace_xdna_job(sched_job, hwctx->name, "sent to device", job->seq);
357 
358 	return fence;
359 }
360 
361 static void aie2_sched_job_free(struct drm_sched_job *sched_job)
362 {
363 	struct amdxdna_sched_job *job = drm_job_to_xdna_job(sched_job);
364 	struct amdxdna_hwctx *hwctx = job->hwctx;
365 
366 	trace_xdna_job(sched_job, hwctx->name, "job free", job->seq);
367 	if (!job->job_done)
368 		up(&hwctx->priv->job_sem);
369 
370 	drm_sched_job_cleanup(sched_job);
371 	aie2_job_put(job);
372 }
373 
374 static enum drm_gpu_sched_stat
375 aie2_sched_job_timedout(struct drm_sched_job *sched_job)
376 {
377 	struct amdxdna_sched_job *job = drm_job_to_xdna_job(sched_job);
378 	struct amdxdna_hwctx *hwctx = job->hwctx;
379 	struct amdxdna_dev *xdna;
380 
381 	xdna = hwctx->client->xdna;
382 	trace_xdna_job(sched_job, hwctx->name, "job timedout", job->seq);
383 	job->job_timeout = true;
384 	mutex_lock(&xdna->dev_lock);
385 	aie2_hwctx_stop(xdna, hwctx, sched_job);
386 
387 	aie2_hwctx_restart(xdna, hwctx);
388 	mutex_unlock(&xdna->dev_lock);
389 
390 	return DRM_GPU_SCHED_STAT_RESET;
391 }
392 
393 static const struct drm_sched_backend_ops sched_ops = {
394 	.run_job = aie2_sched_job_run,
395 	.free_job = aie2_sched_job_free,
396 	.timedout_job = aie2_sched_job_timedout,
397 };
398 
399 static int aie2_hwctx_col_list(struct amdxdna_hwctx *hwctx)
400 {
401 	struct amdxdna_dev *xdna = hwctx->client->xdna;
402 	struct amdxdna_dev_hdl *ndev;
403 	int start, end, first, last;
404 	u32 width = 1, entries = 0;
405 	int i;
406 
407 	if (!hwctx->num_tiles) {
408 		XDNA_ERR(xdna, "Number of tiles is zero");
409 		return -EINVAL;
410 	}
411 
412 	ndev = xdna->dev_handle;
413 	if (unlikely(!ndev->metadata.core.row_count)) {
414 		XDNA_WARN(xdna, "Core tile row count is zero");
415 		return -EINVAL;
416 	}
417 
418 	hwctx->num_col = hwctx->num_tiles / ndev->metadata.core.row_count;
419 	if (!hwctx->num_col || hwctx->num_col > ndev->total_col) {
420 		XDNA_ERR(xdna, "Invalid num_col %d", hwctx->num_col);
421 		return -EINVAL;
422 	}
423 
424 	if (ndev->priv->col_align == COL_ALIGN_NATURE)
425 		width = hwctx->num_col;
426 
427 	/*
428 	 * In range [start, end], find out columns that is multiple of width.
429 	 *	'first' is the first column,
430 	 *	'last' is the last column,
431 	 *	'entries' is the total number of columns.
432 	 */
433 	start =  xdna->dev_info->first_col;
434 	end =  ndev->total_col - hwctx->num_col;
435 	if (start > 0 && end == 0) {
436 		XDNA_DBG(xdna, "Force start from col 0");
437 		start = 0;
438 	}
439 	first = start + (width - start % width) % width;
440 	last = end - end % width;
441 	if (last >= first)
442 		entries = (last - first) / width + 1;
443 	XDNA_DBG(xdna, "start %d end %d first %d last %d",
444 		 start, end, first, last);
445 
446 	if (unlikely(!entries)) {
447 		XDNA_ERR(xdna, "Start %d end %d width %d",
448 			 start, end, width);
449 		return -EINVAL;
450 	}
451 
452 	hwctx->col_list = kmalloc_array(entries, sizeof(*hwctx->col_list), GFP_KERNEL);
453 	if (!hwctx->col_list)
454 		return -ENOMEM;
455 
456 	hwctx->col_list_len = entries;
457 	hwctx->col_list[0] = first;
458 	for (i = 1; i < entries; i++)
459 		hwctx->col_list[i] = hwctx->col_list[i - 1] + width;
460 
461 	print_hex_dump_debug("col_list: ", DUMP_PREFIX_OFFSET, 16, 4, hwctx->col_list,
462 			     entries * sizeof(*hwctx->col_list), false);
463 	return 0;
464 }
465 
466 static int aie2_alloc_resource(struct amdxdna_hwctx *hwctx)
467 {
468 	struct amdxdna_dev *xdna = hwctx->client->xdna;
469 	struct alloc_requests *xrs_req;
470 	int ret;
471 
472 	xrs_req = kzalloc(sizeof(*xrs_req), GFP_KERNEL);
473 	if (!xrs_req)
474 		return -ENOMEM;
475 
476 	xrs_req->cdo.start_cols = hwctx->col_list;
477 	xrs_req->cdo.cols_len = hwctx->col_list_len;
478 	xrs_req->cdo.ncols = hwctx->num_col;
479 	xrs_req->cdo.qos_cap.opc = hwctx->max_opc;
480 
481 	xrs_req->rqos.gops = hwctx->qos.gops;
482 	xrs_req->rqos.fps = hwctx->qos.fps;
483 	xrs_req->rqos.dma_bw = hwctx->qos.dma_bandwidth;
484 	xrs_req->rqos.latency = hwctx->qos.latency;
485 	xrs_req->rqos.exec_time = hwctx->qos.frame_exec_time;
486 	xrs_req->rqos.priority = hwctx->qos.priority;
487 
488 	xrs_req->rid = (uintptr_t)hwctx;
489 
490 	ret = xrs_allocate_resource(xdna->xrs_hdl, xrs_req, hwctx);
491 	if (ret)
492 		XDNA_ERR(xdna, "Allocate AIE resource failed, ret %d", ret);
493 
494 	kfree(xrs_req);
495 	return ret;
496 }
497 
498 static void aie2_release_resource(struct amdxdna_hwctx *hwctx)
499 {
500 	struct amdxdna_dev *xdna = hwctx->client->xdna;
501 	int ret;
502 
503 	ret = xrs_release_resource(xdna->xrs_hdl, (uintptr_t)hwctx);
504 	if (ret)
505 		XDNA_ERR(xdna, "Release AIE resource failed, ret %d", ret);
506 }
507 
508 static int aie2_ctx_syncobj_create(struct amdxdna_hwctx *hwctx)
509 {
510 	struct amdxdna_dev *xdna = hwctx->client->xdna;
511 	struct drm_file *filp = hwctx->client->filp;
512 	struct drm_syncobj *syncobj;
513 	u32 hdl;
514 	int ret;
515 
516 	hwctx->syncobj_hdl = AMDXDNA_INVALID_FENCE_HANDLE;
517 
518 	ret = drm_syncobj_create(&syncobj, 0, NULL);
519 	if (ret) {
520 		XDNA_ERR(xdna, "Create ctx syncobj failed, ret %d", ret);
521 		return ret;
522 	}
523 	ret = drm_syncobj_get_handle(filp, syncobj, &hdl);
524 	if (ret) {
525 		drm_syncobj_put(syncobj);
526 		XDNA_ERR(xdna, "Create ctx syncobj handle failed, ret %d", ret);
527 		return ret;
528 	}
529 	hwctx->priv->syncobj = syncobj;
530 	hwctx->syncobj_hdl = hdl;
531 
532 	return 0;
533 }
534 
535 static void aie2_ctx_syncobj_destroy(struct amdxdna_hwctx *hwctx)
536 {
537 	/*
538 	 * The syncobj_hdl is owned by user space and will be cleaned up
539 	 * separately.
540 	 */
541 	drm_syncobj_put(hwctx->priv->syncobj);
542 }
543 
544 int aie2_hwctx_init(struct amdxdna_hwctx *hwctx)
545 {
546 	struct amdxdna_client *client = hwctx->client;
547 	struct amdxdna_dev *xdna = client->xdna;
548 	const struct drm_sched_init_args args = {
549 		.ops = &sched_ops,
550 		.num_rqs = DRM_SCHED_PRIORITY_COUNT,
551 		.credit_limit = HWCTX_MAX_CMDS,
552 		.timeout = msecs_to_jiffies(HWCTX_MAX_TIMEOUT),
553 		.name = "amdxdna_js",
554 		.dev = xdna->ddev.dev,
555 	};
556 	struct drm_gpu_scheduler *sched;
557 	struct amdxdna_hwctx_priv *priv;
558 	struct amdxdna_gem_obj *heap;
559 	int i, ret;
560 
561 	priv = kzalloc(sizeof(*hwctx->priv), GFP_KERNEL);
562 	if (!priv)
563 		return -ENOMEM;
564 	hwctx->priv = priv;
565 
566 	mutex_lock(&client->mm_lock);
567 	heap = client->dev_heap;
568 	if (!heap) {
569 		XDNA_ERR(xdna, "The client dev heap object not exist");
570 		mutex_unlock(&client->mm_lock);
571 		ret = -ENOENT;
572 		goto free_priv;
573 	}
574 	drm_gem_object_get(to_gobj(heap));
575 	mutex_unlock(&client->mm_lock);
576 	priv->heap = heap;
577 	sema_init(&priv->job_sem, HWCTX_MAX_CMDS);
578 
579 	ret = amdxdna_gem_pin(heap);
580 	if (ret) {
581 		XDNA_ERR(xdna, "Dev heap pin failed, ret %d", ret);
582 		goto put_heap;
583 	}
584 
585 	for (i = 0; i < ARRAY_SIZE(priv->cmd_buf); i++) {
586 		struct amdxdna_gem_obj *abo;
587 		struct amdxdna_drm_create_bo args = {
588 			.flags = 0,
589 			.type = AMDXDNA_BO_DEV,
590 			.vaddr = 0,
591 			.size = MAX_CHAIN_CMDBUF_SIZE,
592 		};
593 
594 		abo = amdxdna_drm_alloc_dev_bo(&xdna->ddev, &args, client->filp);
595 		if (IS_ERR(abo)) {
596 			ret = PTR_ERR(abo);
597 			goto free_cmd_bufs;
598 		}
599 
600 		XDNA_DBG(xdna, "Command buf %d addr 0x%llx size 0x%lx",
601 			 i, abo->mem.dev_addr, abo->mem.size);
602 		priv->cmd_buf[i] = abo;
603 	}
604 
605 	sched = &priv->sched;
606 	mutex_init(&priv->io_lock);
607 
608 	fs_reclaim_acquire(GFP_KERNEL);
609 	might_lock(&priv->io_lock);
610 	fs_reclaim_release(GFP_KERNEL);
611 
612 	ret = drm_sched_init(sched, &args);
613 	if (ret) {
614 		XDNA_ERR(xdna, "Failed to init DRM scheduler. ret %d", ret);
615 		goto free_cmd_bufs;
616 	}
617 
618 	ret = drm_sched_entity_init(&priv->entity, DRM_SCHED_PRIORITY_NORMAL,
619 				    &sched, 1, NULL);
620 	if (ret) {
621 		XDNA_ERR(xdna, "Failed to initial sched entiry. ret %d", ret);
622 		goto free_sched;
623 	}
624 
625 	ret = aie2_hwctx_col_list(hwctx);
626 	if (ret) {
627 		XDNA_ERR(xdna, "Create col list failed, ret %d", ret);
628 		goto free_entity;
629 	}
630 
631 	ret = amdxdna_pm_resume_get(xdna);
632 	if (ret)
633 		goto free_col_list;
634 
635 	ret = aie2_alloc_resource(hwctx);
636 	if (ret) {
637 		XDNA_ERR(xdna, "Alloc hw resource failed, ret %d", ret);
638 		goto suspend_put;
639 	}
640 
641 	ret = aie2_map_host_buf(xdna->dev_handle, hwctx->fw_ctx_id,
642 				heap->mem.userptr, heap->mem.size);
643 	if (ret) {
644 		XDNA_ERR(xdna, "Map host buffer failed, ret %d", ret);
645 		goto release_resource;
646 	}
647 
648 	ret = aie2_ctx_syncobj_create(hwctx);
649 	if (ret) {
650 		XDNA_ERR(xdna, "Create syncobj failed, ret %d", ret);
651 		goto release_resource;
652 	}
653 	amdxdna_pm_suspend_put(xdna);
654 
655 	hwctx->status = HWCTX_STAT_INIT;
656 	init_waitqueue_head(&priv->job_free_wq);
657 
658 	XDNA_DBG(xdna, "hwctx %s init completed", hwctx->name);
659 
660 	return 0;
661 
662 release_resource:
663 	aie2_release_resource(hwctx);
664 suspend_put:
665 	amdxdna_pm_suspend_put(xdna);
666 free_col_list:
667 	kfree(hwctx->col_list);
668 free_entity:
669 	drm_sched_entity_destroy(&priv->entity);
670 free_sched:
671 	drm_sched_fini(&priv->sched);
672 free_cmd_bufs:
673 	for (i = 0; i < ARRAY_SIZE(priv->cmd_buf); i++) {
674 		if (!priv->cmd_buf[i])
675 			continue;
676 		drm_gem_object_put(to_gobj(priv->cmd_buf[i]));
677 	}
678 	amdxdna_gem_unpin(heap);
679 put_heap:
680 	drm_gem_object_put(to_gobj(heap));
681 free_priv:
682 	kfree(priv);
683 	return ret;
684 }
685 
686 void aie2_hwctx_fini(struct amdxdna_hwctx *hwctx)
687 {
688 	struct amdxdna_dev *xdna;
689 	int idx;
690 
691 	xdna = hwctx->client->xdna;
692 
693 	XDNA_DBG(xdna, "%s sequence number %lld", hwctx->name, hwctx->priv->seq);
694 	drm_sched_entity_destroy(&hwctx->priv->entity);
695 
696 	aie2_hwctx_wait_for_idle(hwctx);
697 
698 	/* Request fw to destroy hwctx and cancel the rest pending requests */
699 	aie2_release_resource(hwctx);
700 
701 	/* Wait for all submitted jobs to be completed or canceled */
702 	wait_event(hwctx->priv->job_free_wq,
703 		   atomic64_read(&hwctx->job_submit_cnt) ==
704 		   atomic64_read(&hwctx->job_free_cnt));
705 
706 	drm_sched_fini(&hwctx->priv->sched);
707 	aie2_ctx_syncobj_destroy(hwctx);
708 
709 	for (idx = 0; idx < ARRAY_SIZE(hwctx->priv->cmd_buf); idx++)
710 		drm_gem_object_put(to_gobj(hwctx->priv->cmd_buf[idx]));
711 	amdxdna_gem_unpin(hwctx->priv->heap);
712 	drm_gem_object_put(to_gobj(hwctx->priv->heap));
713 
714 	mutex_destroy(&hwctx->priv->io_lock);
715 	kfree(hwctx->col_list);
716 	kfree(hwctx->priv);
717 	kfree(hwctx->cus);
718 }
719 
720 static int aie2_config_cu_resp_handler(void *handle, void __iomem *data, size_t size)
721 {
722 	struct amdxdna_hwctx *hwctx = handle;
723 
724 	amdxdna_pm_suspend_put(hwctx->client->xdna);
725 	return 0;
726 }
727 
728 static int aie2_hwctx_cu_config(struct amdxdna_hwctx *hwctx, void *buf, u32 size)
729 {
730 	struct amdxdna_hwctx_param_config_cu *config = buf;
731 	struct amdxdna_dev *xdna = hwctx->client->xdna;
732 	u32 total_size;
733 	int ret;
734 
735 	XDNA_DBG(xdna, "Config %d CU to %s", config->num_cus, hwctx->name);
736 	if (XDNA_MBZ_DBG(xdna, config->pad, sizeof(config->pad)))
737 		return -EINVAL;
738 
739 	if (hwctx->status != HWCTX_STAT_INIT) {
740 		XDNA_ERR(xdna, "Not support re-config CU");
741 		return -EINVAL;
742 	}
743 
744 	if (!config->num_cus) {
745 		XDNA_ERR(xdna, "Number of CU is zero");
746 		return -EINVAL;
747 	}
748 
749 	total_size = struct_size(config, cu_configs, config->num_cus);
750 	if (total_size > size) {
751 		XDNA_ERR(xdna, "CU config larger than size");
752 		return -EINVAL;
753 	}
754 
755 	hwctx->cus = kmemdup(config, total_size, GFP_KERNEL);
756 	if (!hwctx->cus)
757 		return -ENOMEM;
758 
759 	ret = amdxdna_pm_resume_get(xdna);
760 	if (ret)
761 		goto free_cus;
762 
763 	ret = aie2_config_cu(hwctx, aie2_config_cu_resp_handler);
764 	if (ret) {
765 		XDNA_ERR(xdna, "Config CU to firmware failed, ret %d", ret);
766 		goto pm_suspend_put;
767 	}
768 
769 	wmb(); /* To avoid locking in command submit when check status */
770 	hwctx->status = HWCTX_STAT_READY;
771 
772 	return 0;
773 
774 pm_suspend_put:
775 	amdxdna_pm_suspend_put(xdna);
776 free_cus:
777 	kfree(hwctx->cus);
778 	hwctx->cus = NULL;
779 	return ret;
780 }
781 
782 static void aie2_cmd_wait(struct amdxdna_hwctx *hwctx, u64 seq)
783 {
784 	struct dma_fence *out_fence = aie2_cmd_get_out_fence(hwctx, seq);
785 
786 	if (!out_fence) {
787 		XDNA_ERR(hwctx->client->xdna, "Failed to get fence");
788 		return;
789 	}
790 
791 	dma_fence_wait_timeout(out_fence, false, MAX_SCHEDULE_TIMEOUT);
792 	dma_fence_put(out_fence);
793 }
794 
795 static int aie2_hwctx_cfg_debug_bo(struct amdxdna_hwctx *hwctx, u32 bo_hdl,
796 				   bool attach)
797 {
798 	struct amdxdna_client *client = hwctx->client;
799 	struct amdxdna_dev *xdna = client->xdna;
800 	struct amdxdna_drv_cmd cmd = { 0 };
801 	struct amdxdna_gem_obj *abo;
802 	u64 seq;
803 	int ret;
804 
805 	abo = amdxdna_gem_get_obj(client, bo_hdl, AMDXDNA_BO_DEV);
806 	if (!abo) {
807 		XDNA_ERR(xdna, "Get bo %d failed", bo_hdl);
808 		return -EINVAL;
809 	}
810 
811 	if (attach) {
812 		if (abo->assigned_hwctx != AMDXDNA_INVALID_CTX_HANDLE) {
813 			ret = -EBUSY;
814 			goto put_obj;
815 		}
816 		cmd.opcode = ATTACH_DEBUG_BO;
817 	} else {
818 		if (abo->assigned_hwctx != hwctx->id) {
819 			ret = -EINVAL;
820 			goto put_obj;
821 		}
822 		cmd.opcode = DETACH_DEBUG_BO;
823 	}
824 
825 	ret = amdxdna_cmd_submit(client, &cmd, AMDXDNA_INVALID_BO_HANDLE,
826 				 &bo_hdl, 1, hwctx->id, &seq);
827 	if (ret) {
828 		XDNA_ERR(xdna, "Submit command failed");
829 		goto put_obj;
830 	}
831 
832 	aie2_cmd_wait(hwctx, seq);
833 	if (cmd.result) {
834 		XDNA_ERR(xdna, "Response failure 0x%x", cmd.result);
835 		goto put_obj;
836 	}
837 
838 	if (attach)
839 		abo->assigned_hwctx = hwctx->id;
840 	else
841 		abo->assigned_hwctx = AMDXDNA_INVALID_CTX_HANDLE;
842 
843 	XDNA_DBG(xdna, "Config debug BO %d to %s", bo_hdl, hwctx->name);
844 
845 put_obj:
846 	amdxdna_gem_put_obj(abo);
847 	return ret;
848 }
849 
850 int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size)
851 {
852 	struct amdxdna_dev *xdna = hwctx->client->xdna;
853 
854 	drm_WARN_ON(&xdna->ddev, !mutex_is_locked(&xdna->dev_lock));
855 	switch (type) {
856 	case DRM_AMDXDNA_HWCTX_CONFIG_CU:
857 		return aie2_hwctx_cu_config(hwctx, buf, size);
858 	case DRM_AMDXDNA_HWCTX_ASSIGN_DBG_BUF:
859 		return aie2_hwctx_cfg_debug_bo(hwctx, (u32)value, true);
860 	case DRM_AMDXDNA_HWCTX_REMOVE_DBG_BUF:
861 		return aie2_hwctx_cfg_debug_bo(hwctx, (u32)value, false);
862 	default:
863 		XDNA_DBG(xdna, "Not supported type %d", type);
864 		return -EOPNOTSUPP;
865 	}
866 }
867 
868 int aie2_hwctx_sync_debug_bo(struct amdxdna_hwctx *hwctx, u32 debug_bo_hdl)
869 {
870 	struct amdxdna_client *client = hwctx->client;
871 	struct amdxdna_dev *xdna = client->xdna;
872 	struct amdxdna_drv_cmd cmd = { 0 };
873 	u64 seq;
874 	int ret;
875 
876 	cmd.opcode = SYNC_DEBUG_BO;
877 	ret = amdxdna_cmd_submit(client, &cmd, AMDXDNA_INVALID_BO_HANDLE,
878 				 &debug_bo_hdl, 1, hwctx->id, &seq);
879 	if (ret) {
880 		XDNA_ERR(xdna, "Submit command failed");
881 		return ret;
882 	}
883 
884 	aie2_cmd_wait(hwctx, seq);
885 	if (cmd.result) {
886 		XDNA_ERR(xdna, "Response failure 0x%x", cmd.result);
887 		return -EINVAL;
888 	}
889 
890 	return 0;
891 }
892 
893 static int aie2_populate_range(struct amdxdna_gem_obj *abo)
894 {
895 	struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev);
896 	struct amdxdna_umap *mapp;
897 	unsigned long timeout;
898 	struct mm_struct *mm;
899 	bool found;
900 	int ret;
901 
902 	timeout = jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
903 again:
904 	found = false;
905 	down_write(&xdna->notifier_lock);
906 	list_for_each_entry(mapp, &abo->mem.umap_list, node) {
907 		if (mapp->invalid) {
908 			found = true;
909 			break;
910 		}
911 	}
912 
913 	if (!found) {
914 		abo->mem.map_invalid = false;
915 		up_write(&xdna->notifier_lock);
916 		return 0;
917 	}
918 	kref_get(&mapp->refcnt);
919 	up_write(&xdna->notifier_lock);
920 
921 	XDNA_DBG(xdna, "populate memory range %lx %lx",
922 		 mapp->vma->vm_start, mapp->vma->vm_end);
923 	mm = mapp->notifier.mm;
924 	if (!mmget_not_zero(mm)) {
925 		amdxdna_umap_put(mapp);
926 		return -EFAULT;
927 	}
928 
929 	mapp->range.notifier_seq = mmu_interval_read_begin(&mapp->notifier);
930 	mmap_read_lock(mm);
931 	ret = hmm_range_fault(&mapp->range);
932 	mmap_read_unlock(mm);
933 	if (ret) {
934 		if (time_after(jiffies, timeout)) {
935 			ret = -ETIME;
936 			goto put_mm;
937 		}
938 
939 		if (ret == -EBUSY) {
940 			amdxdna_umap_put(mapp);
941 			goto again;
942 		}
943 
944 		goto put_mm;
945 	}
946 
947 	down_write(&xdna->notifier_lock);
948 	if (mmu_interval_read_retry(&mapp->notifier, mapp->range.notifier_seq)) {
949 		up_write(&xdna->notifier_lock);
950 		amdxdna_umap_put(mapp);
951 		goto again;
952 	}
953 	mapp->invalid = false;
954 	up_write(&xdna->notifier_lock);
955 	amdxdna_umap_put(mapp);
956 	goto again;
957 
958 put_mm:
959 	amdxdna_umap_put(mapp);
960 	mmput(mm);
961 	return ret;
962 }
963 
964 int aie2_cmd_submit(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, u64 *seq)
965 {
966 	struct amdxdna_dev *xdna = hwctx->client->xdna;
967 	struct ww_acquire_ctx acquire_ctx;
968 	struct dma_fence_chain *chain;
969 	struct amdxdna_gem_obj *abo;
970 	unsigned long timeout = 0;
971 	int ret, i;
972 
973 	ret = down_interruptible(&hwctx->priv->job_sem);
974 	if (ret) {
975 		XDNA_ERR(xdna, "Grab job sem failed, ret %d", ret);
976 		return ret;
977 	}
978 
979 	chain = dma_fence_chain_alloc();
980 	if (!chain) {
981 		XDNA_ERR(xdna, "Alloc fence chain failed");
982 		ret = -ENOMEM;
983 		goto up_sem;
984 	}
985 
986 	ret = drm_sched_job_init(&job->base, &hwctx->priv->entity, 1, hwctx,
987 				 hwctx->client->filp->client_id);
988 	if (ret) {
989 		XDNA_ERR(xdna, "DRM job init failed, ret %d", ret);
990 		goto free_chain;
991 	}
992 
993 	ret = amdxdna_pm_resume_get(xdna);
994 	if (ret)
995 		goto cleanup_job;
996 
997 retry:
998 	ret = drm_gem_lock_reservations(job->bos, job->bo_cnt, &acquire_ctx);
999 	if (ret) {
1000 		XDNA_WARN(xdna, "Failed to lock BOs, ret %d", ret);
1001 		goto suspend_put;
1002 	}
1003 
1004 	for (i = 0; i < job->bo_cnt; i++) {
1005 		ret = dma_resv_reserve_fences(job->bos[i]->resv, 1);
1006 		if (ret) {
1007 			XDNA_WARN(xdna, "Failed to reserve fences %d", ret);
1008 			drm_gem_unlock_reservations(job->bos, job->bo_cnt, &acquire_ctx);
1009 			goto suspend_put;
1010 		}
1011 	}
1012 
1013 	down_read(&xdna->notifier_lock);
1014 	for (i = 0; i < job->bo_cnt; i++) {
1015 		abo = to_xdna_obj(job->bos[i]);
1016 		if (abo->mem.map_invalid) {
1017 			up_read(&xdna->notifier_lock);
1018 			drm_gem_unlock_reservations(job->bos, job->bo_cnt, &acquire_ctx);
1019 			if (!timeout) {
1020 				timeout = jiffies +
1021 					msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
1022 			} else if (time_after(jiffies, timeout)) {
1023 				ret = -ETIME;
1024 				goto suspend_put;
1025 			}
1026 
1027 			ret = aie2_populate_range(abo);
1028 			if (ret)
1029 				goto suspend_put;
1030 			goto retry;
1031 		}
1032 	}
1033 
1034 	mutex_lock(&hwctx->priv->io_lock);
1035 	drm_sched_job_arm(&job->base);
1036 	job->out_fence = dma_fence_get(&job->base.s_fence->finished);
1037 	for (i = 0; i < job->bo_cnt; i++)
1038 		dma_resv_add_fence(job->bos[i]->resv, job->out_fence, DMA_RESV_USAGE_WRITE);
1039 	job->seq = hwctx->priv->seq++;
1040 	kref_get(&job->refcnt);
1041 	drm_sched_entity_push_job(&job->base);
1042 
1043 	*seq = job->seq;
1044 	drm_syncobj_add_point(hwctx->priv->syncobj, chain, job->out_fence, *seq);
1045 	mutex_unlock(&hwctx->priv->io_lock);
1046 
1047 	up_read(&xdna->notifier_lock);
1048 	drm_gem_unlock_reservations(job->bos, job->bo_cnt, &acquire_ctx);
1049 
1050 	aie2_job_put(job);
1051 	atomic64_inc(&hwctx->job_submit_cnt);
1052 
1053 	return 0;
1054 
1055 suspend_put:
1056 	amdxdna_pm_suspend_put(xdna);
1057 cleanup_job:
1058 	drm_sched_job_cleanup(&job->base);
1059 free_chain:
1060 	dma_fence_chain_free(chain);
1061 up_sem:
1062 	up(&hwctx->priv->job_sem);
1063 	job->job_done = true;
1064 	return ret;
1065 }
1066 
1067 void aie2_hmm_invalidate(struct amdxdna_gem_obj *abo,
1068 			 unsigned long cur_seq)
1069 {
1070 	struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev);
1071 	struct drm_gem_object *gobj = to_gobj(abo);
1072 	long ret;
1073 
1074 	ret = dma_resv_wait_timeout(gobj->resv, DMA_RESV_USAGE_BOOKKEEP,
1075 				    true, MAX_SCHEDULE_TIMEOUT);
1076 	if (!ret || ret == -ERESTARTSYS)
1077 		XDNA_ERR(xdna, "Failed to wait for bo, ret %ld", ret);
1078 }
1079