xref: /linux/drivers/accel/ivpu/ivpu_job.c (revision 75079df919efcc30eb5bf0427c83fb578f4fe4fc)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2020-2023 Intel Corporation
4  */
5 
6 #include <drm/drm_file.h>
7 
8 #include <linux/bitfield.h>
9 #include <linux/highmem.h>
10 #include <linux/pci.h>
11 #include <linux/module.h>
12 #include <uapi/drm/ivpu_accel.h>
13 
14 #include "ivpu_drv.h"
15 #include "ivpu_fw.h"
16 #include "ivpu_hw.h"
17 #include "ivpu_ipc.h"
18 #include "ivpu_job.h"
19 #include "ivpu_jsm_msg.h"
20 #include "ivpu_pm.h"
21 #include "vpu_boot_api.h"
22 
23 #define CMD_BUF_IDX	     0
24 #define JOB_ID_JOB_MASK	     GENMASK(7, 0)
25 #define JOB_ID_CONTEXT_MASK  GENMASK(31, 8)
26 #define JOB_MAX_BUFFER_COUNT 65535
27 
28 static void ivpu_cmdq_ring_db(struct ivpu_device *vdev, struct ivpu_cmdq *cmdq)
29 {
30 	ivpu_hw_db_set(vdev, cmdq->db_id);
31 }
32 
33 static int ivpu_preemption_buffers_create(struct ivpu_device *vdev,
34 					  struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq)
35 {
36 	u64 primary_size = ALIGN(vdev->fw->primary_preempt_buf_size, PAGE_SIZE);
37 	u64 secondary_size = ALIGN(vdev->fw->secondary_preempt_buf_size, PAGE_SIZE);
38 	struct ivpu_addr_range range;
39 
40 	if (vdev->hw->sched_mode != VPU_SCHEDULING_MODE_HW)
41 		return 0;
42 
43 	range.start = vdev->hw->ranges.user.end - (primary_size * IVPU_NUM_CMDQS_PER_CTX);
44 	range.end = vdev->hw->ranges.user.end;
45 	cmdq->primary_preempt_buf = ivpu_bo_create(vdev, &file_priv->ctx, &range, primary_size,
46 						   DRM_IVPU_BO_WC);
47 	if (!cmdq->primary_preempt_buf) {
48 		ivpu_err(vdev, "Failed to create primary preemption buffer\n");
49 		return -ENOMEM;
50 	}
51 
52 	range.start = vdev->hw->ranges.shave.end - (secondary_size * IVPU_NUM_CMDQS_PER_CTX);
53 	range.end = vdev->hw->ranges.shave.end;
54 	cmdq->secondary_preempt_buf = ivpu_bo_create(vdev, &file_priv->ctx, &range, secondary_size,
55 						     DRM_IVPU_BO_WC);
56 	if (!cmdq->secondary_preempt_buf) {
57 		ivpu_err(vdev, "Failed to create secondary preemption buffer\n");
58 		goto err_free_primary;
59 	}
60 
61 	return 0;
62 
63 err_free_primary:
64 	ivpu_bo_free(cmdq->primary_preempt_buf);
65 	return -ENOMEM;
66 }
67 
68 static void ivpu_preemption_buffers_free(struct ivpu_device *vdev,
69 					 struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq)
70 {
71 	if (vdev->hw->sched_mode != VPU_SCHEDULING_MODE_HW)
72 		return;
73 
74 	drm_WARN_ON(&vdev->drm, !cmdq->primary_preempt_buf);
75 	drm_WARN_ON(&vdev->drm, !cmdq->secondary_preempt_buf);
76 	ivpu_bo_free(cmdq->primary_preempt_buf);
77 	ivpu_bo_free(cmdq->secondary_preempt_buf);
78 }
79 
80 static struct ivpu_cmdq *ivpu_cmdq_alloc(struct ivpu_file_priv *file_priv)
81 {
82 	struct xa_limit db_xa_limit = {.max = IVPU_MAX_DB, .min = IVPU_MIN_DB};
83 	struct ivpu_device *vdev = file_priv->vdev;
84 	struct ivpu_cmdq *cmdq;
85 	int ret;
86 
87 	cmdq = kzalloc(sizeof(*cmdq), GFP_KERNEL);
88 	if (!cmdq)
89 		return NULL;
90 
91 	ret = xa_alloc(&vdev->db_xa, &cmdq->db_id, NULL, db_xa_limit, GFP_KERNEL);
92 	if (ret) {
93 		ivpu_err(vdev, "Failed to allocate doorbell id: %d\n", ret);
94 		goto err_free_cmdq;
95 	}
96 
97 	cmdq->mem = ivpu_bo_create_global(vdev, SZ_4K, DRM_IVPU_BO_WC | DRM_IVPU_BO_MAPPABLE);
98 	if (!cmdq->mem)
99 		goto err_erase_xa;
100 
101 	ret = ivpu_preemption_buffers_create(vdev, file_priv, cmdq);
102 	if (ret)
103 		goto err_free_cmdq_mem;
104 
105 	return cmdq;
106 
107 err_free_cmdq_mem:
108 	ivpu_bo_free(cmdq->mem);
109 err_erase_xa:
110 	xa_erase(&vdev->db_xa, cmdq->db_id);
111 err_free_cmdq:
112 	kfree(cmdq);
113 	return NULL;
114 }
115 
116 static void ivpu_cmdq_free(struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq)
117 {
118 	if (!cmdq)
119 		return;
120 
121 	ivpu_preemption_buffers_free(file_priv->vdev, file_priv, cmdq);
122 	ivpu_bo_free(cmdq->mem);
123 	xa_erase(&file_priv->vdev->db_xa, cmdq->db_id);
124 	kfree(cmdq);
125 }
126 
127 static int ivpu_hws_cmdq_init(struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq, u16 engine,
128 			      u8 priority)
129 {
130 	struct ivpu_device *vdev = file_priv->vdev;
131 	int ret;
132 
133 	ret = ivpu_jsm_hws_create_cmdq(vdev, file_priv->ctx.id, file_priv->ctx.id, cmdq->db_id,
134 				       task_pid_nr(current), engine,
135 				       cmdq->mem->vpu_addr, ivpu_bo_size(cmdq->mem));
136 	if (ret)
137 		return ret;
138 
139 	ret = ivpu_jsm_hws_set_context_sched_properties(vdev, file_priv->ctx.id, cmdq->db_id,
140 							priority);
141 	if (ret)
142 		return ret;
143 
144 	return 0;
145 }
146 
147 static int ivpu_register_db(struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq)
148 {
149 	struct ivpu_device *vdev = file_priv->vdev;
150 	int ret;
151 
152 	if (vdev->hw->sched_mode == VPU_SCHEDULING_MODE_HW)
153 		ret = ivpu_jsm_hws_register_db(vdev, file_priv->ctx.id, cmdq->db_id, cmdq->db_id,
154 					       cmdq->mem->vpu_addr, ivpu_bo_size(cmdq->mem));
155 	else
156 		ret = ivpu_jsm_register_db(vdev, file_priv->ctx.id, cmdq->db_id,
157 					   cmdq->mem->vpu_addr, ivpu_bo_size(cmdq->mem));
158 
159 	if (!ret)
160 		ivpu_dbg(vdev, JOB, "DB %d registered to ctx %d\n", cmdq->db_id, file_priv->ctx.id);
161 
162 	return ret;
163 }
164 
165 static int
166 ivpu_cmdq_init(struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq, u16 engine, u8 priority)
167 {
168 	struct ivpu_device *vdev = file_priv->vdev;
169 	struct vpu_job_queue_header *jobq_header;
170 	int ret;
171 
172 	lockdep_assert_held(&file_priv->lock);
173 
174 	if (cmdq->db_registered)
175 		return 0;
176 
177 	cmdq->entry_count = (u32)((ivpu_bo_size(cmdq->mem) - sizeof(struct vpu_job_queue_header)) /
178 				  sizeof(struct vpu_job_queue_entry));
179 
180 	cmdq->jobq = (struct vpu_job_queue *)ivpu_bo_vaddr(cmdq->mem);
181 	jobq_header = &cmdq->jobq->header;
182 	jobq_header->engine_idx = engine;
183 	jobq_header->head = 0;
184 	jobq_header->tail = 0;
185 	wmb(); /* Flush WC buffer for jobq->header */
186 
187 	if (vdev->hw->sched_mode == VPU_SCHEDULING_MODE_HW) {
188 		ret = ivpu_hws_cmdq_init(file_priv, cmdq, engine, priority);
189 		if (ret)
190 			return ret;
191 	}
192 
193 	ret = ivpu_register_db(file_priv, cmdq);
194 	if (ret)
195 		return ret;
196 
197 	cmdq->db_registered = true;
198 
199 	return 0;
200 }
201 
202 static int ivpu_cmdq_fini(struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq)
203 {
204 	struct ivpu_device *vdev = file_priv->vdev;
205 	int ret;
206 
207 	lockdep_assert_held(&file_priv->lock);
208 
209 	if (!cmdq->db_registered)
210 		return 0;
211 
212 	cmdq->db_registered = false;
213 
214 	if (vdev->hw->sched_mode == VPU_SCHEDULING_MODE_HW) {
215 		ret = ivpu_jsm_hws_destroy_cmdq(vdev, file_priv->ctx.id, cmdq->db_id);
216 		if (!ret)
217 			ivpu_dbg(vdev, JOB, "Command queue %d destroyed\n", cmdq->db_id);
218 	}
219 
220 	ret = ivpu_jsm_unregister_db(vdev, cmdq->db_id);
221 	if (!ret)
222 		ivpu_dbg(vdev, JOB, "DB %d unregistered\n", cmdq->db_id);
223 
224 	return 0;
225 }
226 
227 static struct ivpu_cmdq *ivpu_cmdq_acquire(struct ivpu_file_priv *file_priv, u16 engine,
228 					   u8 priority)
229 {
230 	int cmdq_idx = IVPU_CMDQ_INDEX(engine, priority);
231 	struct ivpu_cmdq *cmdq = file_priv->cmdq[cmdq_idx];
232 	int ret;
233 
234 	lockdep_assert_held(&file_priv->lock);
235 
236 	if (!cmdq) {
237 		cmdq = ivpu_cmdq_alloc(file_priv);
238 		if (!cmdq)
239 			return NULL;
240 		file_priv->cmdq[cmdq_idx] = cmdq;
241 	}
242 
243 	ret = ivpu_cmdq_init(file_priv, cmdq, engine, priority);
244 	if (ret)
245 		return NULL;
246 
247 	return cmdq;
248 }
249 
250 static void ivpu_cmdq_release_locked(struct ivpu_file_priv *file_priv, u16 engine, u8 priority)
251 {
252 	int cmdq_idx = IVPU_CMDQ_INDEX(engine, priority);
253 	struct ivpu_cmdq *cmdq = file_priv->cmdq[cmdq_idx];
254 
255 	lockdep_assert_held(&file_priv->lock);
256 
257 	if (cmdq) {
258 		file_priv->cmdq[cmdq_idx] = NULL;
259 		ivpu_cmdq_fini(file_priv, cmdq);
260 		ivpu_cmdq_free(file_priv, cmdq);
261 	}
262 }
263 
264 void ivpu_cmdq_release_all_locked(struct ivpu_file_priv *file_priv)
265 {
266 	u16 engine;
267 	u8 priority;
268 
269 	lockdep_assert_held(&file_priv->lock);
270 
271 	for (engine = 0; engine < IVPU_NUM_ENGINES; engine++)
272 		for (priority = 0; priority < IVPU_NUM_PRIORITIES; priority++)
273 			ivpu_cmdq_release_locked(file_priv, engine, priority);
274 }
275 
276 /*
277  * Mark the doorbell as unregistered
278  * This function needs to be called when the VPU hardware is restarted
279  * and FW loses job queue state. The next time job queue is used it
280  * will be registered again.
281  */
282 static void ivpu_cmdq_reset(struct ivpu_file_priv *file_priv)
283 {
284 	u16 engine;
285 	u8 priority;
286 
287 	mutex_lock(&file_priv->lock);
288 
289 	for (engine = 0; engine < IVPU_NUM_ENGINES; engine++) {
290 		for (priority = 0; priority < IVPU_NUM_PRIORITIES; priority++) {
291 			int cmdq_idx = IVPU_CMDQ_INDEX(engine, priority);
292 			struct ivpu_cmdq *cmdq = file_priv->cmdq[cmdq_idx];
293 
294 			if (cmdq)
295 				cmdq->db_registered = false;
296 		}
297 	}
298 
299 	mutex_unlock(&file_priv->lock);
300 }
301 
302 void ivpu_cmdq_reset_all_contexts(struct ivpu_device *vdev)
303 {
304 	struct ivpu_file_priv *file_priv;
305 	unsigned long ctx_id;
306 
307 	mutex_lock(&vdev->context_list_lock);
308 
309 	xa_for_each(&vdev->context_xa, ctx_id, file_priv)
310 		ivpu_cmdq_reset(file_priv);
311 
312 	mutex_unlock(&vdev->context_list_lock);
313 }
314 
315 static int ivpu_cmdq_push_job(struct ivpu_cmdq *cmdq, struct ivpu_job *job)
316 {
317 	struct ivpu_device *vdev = job->vdev;
318 	struct vpu_job_queue_header *header = &cmdq->jobq->header;
319 	struct vpu_job_queue_entry *entry;
320 	u32 tail = READ_ONCE(header->tail);
321 	u32 next_entry = (tail + 1) % cmdq->entry_count;
322 
323 	/* Check if there is space left in job queue */
324 	if (next_entry == header->head) {
325 		ivpu_dbg(vdev, JOB, "Job queue full: ctx %d engine %d db %d head %d tail %d\n",
326 			 job->file_priv->ctx.id, job->engine_idx, cmdq->db_id, header->head, tail);
327 		return -EBUSY;
328 	}
329 
330 	entry = &cmdq->jobq->job[tail];
331 	entry->batch_buf_addr = job->cmd_buf_vpu_addr;
332 	entry->job_id = job->job_id;
333 	entry->flags = 0;
334 	if (unlikely(ivpu_test_mode & IVPU_TEST_MODE_NULL_SUBMISSION))
335 		entry->flags = VPU_JOB_FLAGS_NULL_SUBMISSION_MASK;
336 
337 	if (vdev->hw->sched_mode == VPU_SCHEDULING_MODE_HW &&
338 	    (unlikely(!(ivpu_test_mode & IVPU_TEST_MODE_PREEMPTION_DISABLE)))) {
339 		entry->primary_preempt_buf_addr = cmdq->primary_preempt_buf->vpu_addr;
340 		entry->primary_preempt_buf_size = ivpu_bo_size(cmdq->primary_preempt_buf);
341 		entry->secondary_preempt_buf_addr = cmdq->secondary_preempt_buf->vpu_addr;
342 		entry->secondary_preempt_buf_size = ivpu_bo_size(cmdq->secondary_preempt_buf);
343 	}
344 
345 	wmb(); /* Ensure that tail is updated after filling entry */
346 	header->tail = next_entry;
347 	wmb(); /* Flush WC buffer for jobq header */
348 
349 	return 0;
350 }
351 
352 struct ivpu_fence {
353 	struct dma_fence base;
354 	spinlock_t lock; /* protects base */
355 	struct ivpu_device *vdev;
356 };
357 
358 static inline struct ivpu_fence *to_vpu_fence(struct dma_fence *fence)
359 {
360 	return container_of(fence, struct ivpu_fence, base);
361 }
362 
363 static const char *ivpu_fence_get_driver_name(struct dma_fence *fence)
364 {
365 	return DRIVER_NAME;
366 }
367 
368 static const char *ivpu_fence_get_timeline_name(struct dma_fence *fence)
369 {
370 	struct ivpu_fence *ivpu_fence = to_vpu_fence(fence);
371 
372 	return dev_name(ivpu_fence->vdev->drm.dev);
373 }
374 
375 static const struct dma_fence_ops ivpu_fence_ops = {
376 	.get_driver_name = ivpu_fence_get_driver_name,
377 	.get_timeline_name = ivpu_fence_get_timeline_name,
378 };
379 
380 static struct dma_fence *ivpu_fence_create(struct ivpu_device *vdev)
381 {
382 	struct ivpu_fence *fence;
383 
384 	fence = kzalloc(sizeof(*fence), GFP_KERNEL);
385 	if (!fence)
386 		return NULL;
387 
388 	fence->vdev = vdev;
389 	spin_lock_init(&fence->lock);
390 	dma_fence_init(&fence->base, &ivpu_fence_ops, &fence->lock, dma_fence_context_alloc(1), 1);
391 
392 	return &fence->base;
393 }
394 
395 static void ivpu_job_destroy(struct ivpu_job *job)
396 {
397 	struct ivpu_device *vdev = job->vdev;
398 	u32 i;
399 
400 	ivpu_dbg(vdev, JOB, "Job destroyed: id %3u ctx %2d engine %d",
401 		 job->job_id, job->file_priv->ctx.id, job->engine_idx);
402 
403 	for (i = 0; i < job->bo_count; i++)
404 		if (job->bos[i])
405 			drm_gem_object_put(&job->bos[i]->base.base);
406 
407 	dma_fence_put(job->done_fence);
408 	ivpu_file_priv_put(&job->file_priv);
409 	kfree(job);
410 }
411 
412 static struct ivpu_job *
413 ivpu_job_create(struct ivpu_file_priv *file_priv, u32 engine_idx, u32 bo_count)
414 {
415 	struct ivpu_device *vdev = file_priv->vdev;
416 	struct ivpu_job *job;
417 
418 	job = kzalloc(struct_size(job, bos, bo_count), GFP_KERNEL);
419 	if (!job)
420 		return NULL;
421 
422 	job->vdev = vdev;
423 	job->engine_idx = engine_idx;
424 	job->bo_count = bo_count;
425 	job->done_fence = ivpu_fence_create(vdev);
426 	if (!job->done_fence) {
427 		ivpu_warn_ratelimited(vdev, "Failed to create a fence\n");
428 		goto err_free_job;
429 	}
430 
431 	job->file_priv = ivpu_file_priv_get(file_priv);
432 
433 	ivpu_dbg(vdev, JOB, "Job created: ctx %2d engine %d", file_priv->ctx.id, job->engine_idx);
434 	return job;
435 
436 err_free_job:
437 	kfree(job);
438 	return NULL;
439 }
440 
441 static struct ivpu_job *ivpu_job_remove_from_submitted_jobs(struct ivpu_device *vdev, u32 job_id)
442 {
443 	struct ivpu_job *job;
444 
445 	xa_lock(&vdev->submitted_jobs_xa);
446 	job = __xa_erase(&vdev->submitted_jobs_xa, job_id);
447 
448 	if (xa_empty(&vdev->submitted_jobs_xa) && job) {
449 		vdev->busy_time = ktime_add(ktime_sub(ktime_get(), vdev->busy_start_ts),
450 					    vdev->busy_time);
451 	}
452 
453 	xa_unlock(&vdev->submitted_jobs_xa);
454 
455 	return job;
456 }
457 
458 static int ivpu_job_signal_and_destroy(struct ivpu_device *vdev, u32 job_id, u32 job_status)
459 {
460 	struct ivpu_job *job;
461 
462 	job = ivpu_job_remove_from_submitted_jobs(vdev, job_id);
463 	if (!job)
464 		return -ENOENT;
465 
466 	if (job->file_priv->has_mmu_faults)
467 		job_status = DRM_IVPU_JOB_STATUS_ABORTED;
468 
469 	job->bos[CMD_BUF_IDX]->job_status = job_status;
470 	dma_fence_signal(job->done_fence);
471 
472 	ivpu_dbg(vdev, JOB, "Job complete:  id %3u ctx %2d engine %d status 0x%x\n",
473 		 job->job_id, job->file_priv->ctx.id, job->engine_idx, job_status);
474 
475 	ivpu_job_destroy(job);
476 	ivpu_stop_job_timeout_detection(vdev);
477 
478 	ivpu_rpm_put(vdev);
479 	return 0;
480 }
481 
482 void ivpu_jobs_abort_all(struct ivpu_device *vdev)
483 {
484 	struct ivpu_job *job;
485 	unsigned long id;
486 
487 	xa_for_each(&vdev->submitted_jobs_xa, id, job)
488 		ivpu_job_signal_and_destroy(vdev, id, DRM_IVPU_JOB_STATUS_ABORTED);
489 }
490 
491 static int ivpu_job_submit(struct ivpu_job *job, u8 priority)
492 {
493 	struct ivpu_file_priv *file_priv = job->file_priv;
494 	struct ivpu_device *vdev = job->vdev;
495 	struct xa_limit job_id_range;
496 	struct ivpu_cmdq *cmdq;
497 	bool is_first_job;
498 	int ret;
499 
500 	ret = ivpu_rpm_get(vdev);
501 	if (ret < 0)
502 		return ret;
503 
504 	mutex_lock(&file_priv->lock);
505 
506 	cmdq = ivpu_cmdq_acquire(job->file_priv, job->engine_idx, priority);
507 	if (!cmdq) {
508 		ivpu_warn_ratelimited(vdev, "Failed to get job queue, ctx %d engine %d prio %d\n",
509 				      file_priv->ctx.id, job->engine_idx, priority);
510 		ret = -EINVAL;
511 		goto err_unlock_file_priv;
512 	}
513 
514 	job_id_range.min = FIELD_PREP(JOB_ID_CONTEXT_MASK, (file_priv->ctx.id - 1));
515 	job_id_range.max = job_id_range.min | JOB_ID_JOB_MASK;
516 
517 	xa_lock(&vdev->submitted_jobs_xa);
518 	is_first_job = xa_empty(&vdev->submitted_jobs_xa);
519 	ret = __xa_alloc(&vdev->submitted_jobs_xa, &job->job_id, job, job_id_range, GFP_KERNEL);
520 	if (ret) {
521 		ivpu_dbg(vdev, JOB, "Too many active jobs in ctx %d\n",
522 			 file_priv->ctx.id);
523 		ret = -EBUSY;
524 		goto err_unlock_submitted_jobs_xa;
525 	}
526 
527 	ret = ivpu_cmdq_push_job(cmdq, job);
528 	if (ret)
529 		goto err_erase_xa;
530 
531 	ivpu_start_job_timeout_detection(vdev);
532 
533 	if (unlikely(ivpu_test_mode & IVPU_TEST_MODE_NULL_HW)) {
534 		cmdq->jobq->header.head = cmdq->jobq->header.tail;
535 		wmb(); /* Flush WC buffer for jobq header */
536 	} else {
537 		ivpu_cmdq_ring_db(vdev, cmdq);
538 		if (is_first_job)
539 			vdev->busy_start_ts = ktime_get();
540 	}
541 
542 	ivpu_dbg(vdev, JOB, "Job submitted: id %3u ctx %2d engine %d prio %d addr 0x%llx next %d\n",
543 		 job->job_id, file_priv->ctx.id, job->engine_idx, priority,
544 		 job->cmd_buf_vpu_addr, cmdq->jobq->header.tail);
545 
546 	xa_unlock(&vdev->submitted_jobs_xa);
547 
548 	mutex_unlock(&file_priv->lock);
549 
550 	if (unlikely(ivpu_test_mode & IVPU_TEST_MODE_NULL_HW))
551 		ivpu_job_signal_and_destroy(vdev, job->job_id, VPU_JSM_STATUS_SUCCESS);
552 
553 	return 0;
554 
555 err_erase_xa:
556 	__xa_erase(&vdev->submitted_jobs_xa, job->job_id);
557 err_unlock_submitted_jobs_xa:
558 	xa_unlock(&vdev->submitted_jobs_xa);
559 err_unlock_file_priv:
560 	mutex_unlock(&file_priv->lock);
561 	ivpu_rpm_put(vdev);
562 	return ret;
563 }
564 
565 static int
566 ivpu_job_prepare_bos_for_submit(struct drm_file *file, struct ivpu_job *job, u32 *buf_handles,
567 				u32 buf_count, u32 commands_offset)
568 {
569 	struct ivpu_file_priv *file_priv = file->driver_priv;
570 	struct ivpu_device *vdev = file_priv->vdev;
571 	struct ww_acquire_ctx acquire_ctx;
572 	enum dma_resv_usage usage;
573 	struct ivpu_bo *bo;
574 	int ret;
575 	u32 i;
576 
577 	for (i = 0; i < buf_count; i++) {
578 		struct drm_gem_object *obj = drm_gem_object_lookup(file, buf_handles[i]);
579 
580 		if (!obj)
581 			return -ENOENT;
582 
583 		job->bos[i] = to_ivpu_bo(obj);
584 
585 		ret = ivpu_bo_pin(job->bos[i]);
586 		if (ret)
587 			return ret;
588 	}
589 
590 	bo = job->bos[CMD_BUF_IDX];
591 	if (!dma_resv_test_signaled(bo->base.base.resv, DMA_RESV_USAGE_READ)) {
592 		ivpu_warn(vdev, "Buffer is already in use\n");
593 		return -EBUSY;
594 	}
595 
596 	if (commands_offset >= ivpu_bo_size(bo)) {
597 		ivpu_warn(vdev, "Invalid command buffer offset %u\n", commands_offset);
598 		return -EINVAL;
599 	}
600 
601 	job->cmd_buf_vpu_addr = bo->vpu_addr + commands_offset;
602 
603 	ret = drm_gem_lock_reservations((struct drm_gem_object **)job->bos, buf_count,
604 					&acquire_ctx);
605 	if (ret) {
606 		ivpu_warn(vdev, "Failed to lock reservations: %d\n", ret);
607 		return ret;
608 	}
609 
610 	for (i = 0; i < buf_count; i++) {
611 		ret = dma_resv_reserve_fences(job->bos[i]->base.base.resv, 1);
612 		if (ret) {
613 			ivpu_warn(vdev, "Failed to reserve fences: %d\n", ret);
614 			goto unlock_reservations;
615 		}
616 	}
617 
618 	for (i = 0; i < buf_count; i++) {
619 		usage = (i == CMD_BUF_IDX) ? DMA_RESV_USAGE_WRITE : DMA_RESV_USAGE_BOOKKEEP;
620 		dma_resv_add_fence(job->bos[i]->base.base.resv, job->done_fence, usage);
621 	}
622 
623 unlock_reservations:
624 	drm_gem_unlock_reservations((struct drm_gem_object **)job->bos, buf_count, &acquire_ctx);
625 
626 	wmb(); /* Flush write combining buffers */
627 
628 	return ret;
629 }
630 
631 static inline u8 ivpu_job_to_hws_priority(struct ivpu_file_priv *file_priv, u8 priority)
632 {
633 	if (priority == DRM_IVPU_JOB_PRIORITY_DEFAULT)
634 		return DRM_IVPU_JOB_PRIORITY_NORMAL;
635 
636 	return priority - 1;
637 }
638 
639 int ivpu_submit_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
640 {
641 	struct ivpu_file_priv *file_priv = file->driver_priv;
642 	struct ivpu_device *vdev = file_priv->vdev;
643 	struct drm_ivpu_submit *params = data;
644 	struct ivpu_job *job;
645 	u32 *buf_handles;
646 	int idx, ret;
647 	u8 priority;
648 
649 	if (params->engine > DRM_IVPU_ENGINE_COPY)
650 		return -EINVAL;
651 
652 	if (params->priority > DRM_IVPU_JOB_PRIORITY_REALTIME)
653 		return -EINVAL;
654 
655 	if (params->buffer_count == 0 || params->buffer_count > JOB_MAX_BUFFER_COUNT)
656 		return -EINVAL;
657 
658 	if (!IS_ALIGNED(params->commands_offset, 8))
659 		return -EINVAL;
660 
661 	if (!file_priv->ctx.id)
662 		return -EINVAL;
663 
664 	if (file_priv->has_mmu_faults)
665 		return -EBADFD;
666 
667 	buf_handles = kcalloc(params->buffer_count, sizeof(u32), GFP_KERNEL);
668 	if (!buf_handles)
669 		return -ENOMEM;
670 
671 	ret = copy_from_user(buf_handles,
672 			     (void __user *)params->buffers_ptr,
673 			     params->buffer_count * sizeof(u32));
674 	if (ret) {
675 		ret = -EFAULT;
676 		goto err_free_handles;
677 	}
678 
679 	if (!drm_dev_enter(&vdev->drm, &idx)) {
680 		ret = -ENODEV;
681 		goto err_free_handles;
682 	}
683 
684 	ivpu_dbg(vdev, JOB, "Submit ioctl: ctx %u buf_count %u\n",
685 		 file_priv->ctx.id, params->buffer_count);
686 
687 	job = ivpu_job_create(file_priv, params->engine, params->buffer_count);
688 	if (!job) {
689 		ivpu_err(vdev, "Failed to create job\n");
690 		ret = -ENOMEM;
691 		goto err_exit_dev;
692 	}
693 
694 	ret = ivpu_job_prepare_bos_for_submit(file, job, buf_handles, params->buffer_count,
695 					      params->commands_offset);
696 	if (ret) {
697 		ivpu_err(vdev, "Failed to prepare job: %d\n", ret);
698 		goto err_destroy_job;
699 	}
700 
701 	priority = ivpu_job_to_hws_priority(file_priv, params->priority);
702 
703 	down_read(&vdev->pm->reset_lock);
704 	ret = ivpu_job_submit(job, priority);
705 	up_read(&vdev->pm->reset_lock);
706 	if (ret)
707 		goto err_signal_fence;
708 
709 	drm_dev_exit(idx);
710 	kfree(buf_handles);
711 	return ret;
712 
713 err_signal_fence:
714 	dma_fence_signal(job->done_fence);
715 err_destroy_job:
716 	ivpu_job_destroy(job);
717 err_exit_dev:
718 	drm_dev_exit(idx);
719 err_free_handles:
720 	kfree(buf_handles);
721 	return ret;
722 }
723 
724 static void
725 ivpu_job_done_callback(struct ivpu_device *vdev, struct ivpu_ipc_hdr *ipc_hdr,
726 		       struct vpu_jsm_msg *jsm_msg)
727 {
728 	struct vpu_ipc_msg_payload_job_done *payload;
729 	int ret;
730 
731 	if (!jsm_msg) {
732 		ivpu_err(vdev, "IPC message has no JSM payload\n");
733 		return;
734 	}
735 
736 	if (jsm_msg->result != VPU_JSM_STATUS_SUCCESS) {
737 		ivpu_err(vdev, "Invalid JSM message result: %d\n", jsm_msg->result);
738 		return;
739 	}
740 
741 	payload = (struct vpu_ipc_msg_payload_job_done *)&jsm_msg->payload;
742 	ret = ivpu_job_signal_and_destroy(vdev, payload->job_id, payload->job_status);
743 	if (!ret && !xa_empty(&vdev->submitted_jobs_xa))
744 		ivpu_start_job_timeout_detection(vdev);
745 }
746 
747 void ivpu_job_done_consumer_init(struct ivpu_device *vdev)
748 {
749 	ivpu_ipc_consumer_add(vdev, &vdev->job_done_consumer,
750 			      VPU_IPC_CHAN_JOB_RET, ivpu_job_done_callback);
751 }
752 
753 void ivpu_job_done_consumer_fini(struct ivpu_device *vdev)
754 {
755 	ivpu_ipc_consumer_del(vdev, &vdev->job_done_consumer);
756 }
757