xref: /linux-6.15/drivers/gpu/drm/v3d/v3d_submit.c (revision 484de39f)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2014-2018 Broadcom
4  * Copyright (C) 2023 Raspberry Pi
5  */
6 
7 #include <drm/drm_syncobj.h>
8 
9 #include "v3d_drv.h"
10 #include "v3d_regs.h"
11 #include "v3d_trace.h"
12 
13 /* Takes the reservation lock on all the BOs being referenced, so that
14  * at queue submit time we can update the reservations.
15  *
16  * We don't lock the RCL the tile alloc/state BOs, or overflow memory
17  * (all of which are on exec->unref_list).  They're entirely private
18  * to v3d, so we don't attach dma-buf fences to them.
19  */
20 static int
21 v3d_lock_bo_reservations(struct v3d_job *job,
22 			 struct ww_acquire_ctx *acquire_ctx)
23 {
24 	int i, ret;
25 
26 	ret = drm_gem_lock_reservations(job->bo, job->bo_count, acquire_ctx);
27 	if (ret)
28 		return ret;
29 
30 	for (i = 0; i < job->bo_count; i++) {
31 		ret = dma_resv_reserve_fences(job->bo[i]->resv, 1);
32 		if (ret)
33 			goto fail;
34 
35 		ret = drm_sched_job_add_implicit_dependencies(&job->base,
36 							      job->bo[i], true);
37 		if (ret)
38 			goto fail;
39 	}
40 
41 	return 0;
42 
43 fail:
44 	drm_gem_unlock_reservations(job->bo, job->bo_count, acquire_ctx);
45 	return ret;
46 }
47 
48 /**
49  * v3d_lookup_bos() - Sets up job->bo[] with the GEM objects
50  * referenced by the job.
51  * @dev: DRM device
52  * @file_priv: DRM file for this fd
53  * @job: V3D job being set up
54  * @bo_handles: GEM handles
55  * @bo_count: Number of GEM handles passed in
56  *
57  * The command validator needs to reference BOs by their index within
58  * the submitted job's BO list.  This does the validation of the job's
59  * BO list and reference counting for the lifetime of the job.
60  *
61  * Note that this function doesn't need to unreference the BOs on
62  * failure, because that will happen at v3d_exec_cleanup() time.
63  */
64 static int
65 v3d_lookup_bos(struct drm_device *dev,
66 	       struct drm_file *file_priv,
67 	       struct v3d_job *job,
68 	       u64 bo_handles,
69 	       u32 bo_count)
70 {
71 	job->bo_count = bo_count;
72 
73 	if (!job->bo_count) {
74 		/* See comment on bo_index for why we have to check
75 		 * this.
76 		 */
77 		DRM_DEBUG("Rendering requires BOs\n");
78 		return -EINVAL;
79 	}
80 
81 	return drm_gem_objects_lookup(file_priv,
82 				      (void __user *)(uintptr_t)bo_handles,
83 				      job->bo_count, &job->bo);
84 }
85 
86 static void
87 v3d_job_free(struct kref *ref)
88 {
89 	struct v3d_job *job = container_of(ref, struct v3d_job, refcount);
90 	int i;
91 
92 	if (job->bo) {
93 		for (i = 0; i < job->bo_count; i++)
94 			drm_gem_object_put(job->bo[i]);
95 		kvfree(job->bo);
96 	}
97 
98 	dma_fence_put(job->irq_fence);
99 	dma_fence_put(job->done_fence);
100 
101 	if (job->perfmon)
102 		v3d_perfmon_put(job->perfmon);
103 
104 	kfree(job);
105 }
106 
107 static void
108 v3d_render_job_free(struct kref *ref)
109 {
110 	struct v3d_render_job *job = container_of(ref, struct v3d_render_job,
111 						  base.refcount);
112 	struct v3d_bo *bo, *save;
113 
114 	list_for_each_entry_safe(bo, save, &job->unref_list, unref_head) {
115 		drm_gem_object_put(&bo->base.base);
116 	}
117 
118 	v3d_job_free(ref);
119 }
120 
121 void v3d_job_cleanup(struct v3d_job *job)
122 {
123 	if (!job)
124 		return;
125 
126 	drm_sched_job_cleanup(&job->base);
127 	v3d_job_put(job);
128 }
129 
130 void v3d_job_put(struct v3d_job *job)
131 {
132 	if (!job)
133 		return;
134 
135 	kref_put(&job->refcount, job->free);
136 }
137 
138 static int
139 v3d_job_allocate(void **container, size_t size)
140 {
141 	*container = kcalloc(1, size, GFP_KERNEL);
142 	if (!*container) {
143 		DRM_ERROR("Cannot allocate memory for V3D job.\n");
144 		return -ENOMEM;
145 	}
146 
147 	return 0;
148 }
149 
150 static void
151 v3d_job_deallocate(void **container)
152 {
153 	kfree(*container);
154 	*container = NULL;
155 }
156 
157 static int
158 v3d_job_init(struct v3d_dev *v3d, struct drm_file *file_priv,
159 	     struct v3d_job *job, void (*free)(struct kref *ref),
160 	     u32 in_sync, struct v3d_submit_ext *se, enum v3d_queue queue)
161 {
162 	struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
163 	bool has_multisync = se && (se->flags & DRM_V3D_EXT_ID_MULTI_SYNC);
164 	int ret, i;
165 
166 	job->v3d = v3d;
167 	job->free = free;
168 	job->file = file_priv;
169 
170 	ret = drm_sched_job_init(&job->base, &v3d_priv->sched_entity[queue],
171 				 1, v3d_priv);
172 	if (ret)
173 		return ret;
174 
175 	if (has_multisync) {
176 		if (se->in_sync_count && se->wait_stage == queue) {
177 			struct drm_v3d_sem __user *handle = u64_to_user_ptr(se->in_syncs);
178 
179 			for (i = 0; i < se->in_sync_count; i++) {
180 				struct drm_v3d_sem in;
181 
182 				if (copy_from_user(&in, handle++, sizeof(in))) {
183 					ret = -EFAULT;
184 					DRM_DEBUG("Failed to copy wait dep handle.\n");
185 					goto fail_deps;
186 				}
187 				ret = drm_sched_job_add_syncobj_dependency(&job->base, file_priv, in.handle, 0);
188 
189 				// TODO: Investigate why this was filtered out for the IOCTL.
190 				if (ret && ret != -ENOENT)
191 					goto fail_deps;
192 			}
193 		}
194 	} else {
195 		ret = drm_sched_job_add_syncobj_dependency(&job->base, file_priv, in_sync, 0);
196 
197 		// TODO: Investigate why this was filtered out for the IOCTL.
198 		if (ret && ret != -ENOENT)
199 			goto fail_deps;
200 	}
201 
202 	kref_init(&job->refcount);
203 
204 	return 0;
205 
206 fail_deps:
207 	drm_sched_job_cleanup(&job->base);
208 	return ret;
209 }
210 
211 static void
212 v3d_push_job(struct v3d_job *job)
213 {
214 	drm_sched_job_arm(&job->base);
215 
216 	job->done_fence = dma_fence_get(&job->base.s_fence->finished);
217 
218 	/* put by scheduler job completion */
219 	kref_get(&job->refcount);
220 
221 	drm_sched_entity_push_job(&job->base);
222 }
223 
224 static void
225 v3d_attach_fences_and_unlock_reservation(struct drm_file *file_priv,
226 					 struct v3d_job *job,
227 					 struct ww_acquire_ctx *acquire_ctx,
228 					 u32 out_sync,
229 					 struct v3d_submit_ext *se,
230 					 struct dma_fence *done_fence)
231 {
232 	struct drm_syncobj *sync_out;
233 	bool has_multisync = se && (se->flags & DRM_V3D_EXT_ID_MULTI_SYNC);
234 	int i;
235 
236 	for (i = 0; i < job->bo_count; i++) {
237 		/* XXX: Use shared fences for read-only objects. */
238 		dma_resv_add_fence(job->bo[i]->resv, job->done_fence,
239 				   DMA_RESV_USAGE_WRITE);
240 	}
241 
242 	drm_gem_unlock_reservations(job->bo, job->bo_count, acquire_ctx);
243 
244 	/* Update the return sync object for the job */
245 	/* If it only supports a single signal semaphore*/
246 	if (!has_multisync) {
247 		sync_out = drm_syncobj_find(file_priv, out_sync);
248 		if (sync_out) {
249 			drm_syncobj_replace_fence(sync_out, done_fence);
250 			drm_syncobj_put(sync_out);
251 		}
252 		return;
253 	}
254 
255 	/* If multiple semaphores extension is supported */
256 	if (se->out_sync_count) {
257 		for (i = 0; i < se->out_sync_count; i++) {
258 			drm_syncobj_replace_fence(se->out_syncs[i].syncobj,
259 						  done_fence);
260 			drm_syncobj_put(se->out_syncs[i].syncobj);
261 		}
262 		kvfree(se->out_syncs);
263 	}
264 }
265 
266 static int
267 v3d_setup_csd_jobs_and_bos(struct drm_file *file_priv,
268 			   struct v3d_dev *v3d,
269 			   struct drm_v3d_submit_csd *args,
270 			   struct v3d_csd_job **job,
271 			   struct v3d_job **clean_job,
272 			   struct v3d_submit_ext *se,
273 			   struct ww_acquire_ctx *acquire_ctx)
274 {
275 	int ret;
276 
277 	ret = v3d_job_allocate((void *)job, sizeof(**job));
278 	if (ret)
279 		return ret;
280 
281 	ret = v3d_job_init(v3d, file_priv, &(*job)->base,
282 			   v3d_job_free, args->in_sync, se, V3D_CSD);
283 	if (ret) {
284 		v3d_job_deallocate((void *)job);
285 		return ret;
286 	}
287 
288 	ret = v3d_job_allocate((void *)clean_job, sizeof(**clean_job));
289 	if (ret)
290 		return ret;
291 
292 	ret = v3d_job_init(v3d, file_priv, *clean_job,
293 			   v3d_job_free, 0, NULL, V3D_CACHE_CLEAN);
294 	if (ret) {
295 		v3d_job_deallocate((void *)clean_job);
296 		return ret;
297 	}
298 
299 	(*job)->args = *args;
300 
301 	ret = v3d_lookup_bos(&v3d->drm, file_priv, *clean_job,
302 			     args->bo_handles, args->bo_handle_count);
303 	if (ret)
304 		return ret;
305 
306 	return v3d_lock_bo_reservations(*clean_job, acquire_ctx);
307 }
308 
309 static void
310 v3d_put_multisync_post_deps(struct v3d_submit_ext *se)
311 {
312 	unsigned int i;
313 
314 	if (!(se && se->out_sync_count))
315 		return;
316 
317 	for (i = 0; i < se->out_sync_count; i++)
318 		drm_syncobj_put(se->out_syncs[i].syncobj);
319 	kvfree(se->out_syncs);
320 }
321 
322 static int
323 v3d_get_multisync_post_deps(struct drm_file *file_priv,
324 			    struct v3d_submit_ext *se,
325 			    u32 count, u64 handles)
326 {
327 	struct drm_v3d_sem __user *post_deps;
328 	int i, ret;
329 
330 	if (!count)
331 		return 0;
332 
333 	se->out_syncs = (struct v3d_submit_outsync *)
334 			kvmalloc_array(count,
335 				       sizeof(struct v3d_submit_outsync),
336 				       GFP_KERNEL);
337 	if (!se->out_syncs)
338 		return -ENOMEM;
339 
340 	post_deps = u64_to_user_ptr(handles);
341 
342 	for (i = 0; i < count; i++) {
343 		struct drm_v3d_sem out;
344 
345 		if (copy_from_user(&out, post_deps++, sizeof(out))) {
346 			ret = -EFAULT;
347 			DRM_DEBUG("Failed to copy post dep handles\n");
348 			goto fail;
349 		}
350 
351 		se->out_syncs[i].syncobj = drm_syncobj_find(file_priv,
352 							    out.handle);
353 		if (!se->out_syncs[i].syncobj) {
354 			ret = -EINVAL;
355 			goto fail;
356 		}
357 	}
358 	se->out_sync_count = count;
359 
360 	return 0;
361 
362 fail:
363 	for (i--; i >= 0; i--)
364 		drm_syncobj_put(se->out_syncs[i].syncobj);
365 	kvfree(se->out_syncs);
366 
367 	return ret;
368 }
369 
370 /* Get data for multiple binary semaphores synchronization. Parse syncobj
371  * to be signaled when job completes (out_sync).
372  */
373 static int
374 v3d_get_multisync_submit_deps(struct drm_file *file_priv,
375 			      struct drm_v3d_extension __user *ext,
376 			      struct v3d_submit_ext *se)
377 {
378 	struct drm_v3d_multi_sync multisync;
379 	int ret;
380 
381 	if (se->in_sync_count || se->out_sync_count) {
382 		DRM_DEBUG("Two multisync extensions were added to the same job.");
383 		return -EINVAL;
384 	}
385 
386 	if (copy_from_user(&multisync, ext, sizeof(multisync)))
387 		return -EFAULT;
388 
389 	if (multisync.pad)
390 		return -EINVAL;
391 
392 	ret = v3d_get_multisync_post_deps(file_priv, se, multisync.out_sync_count,
393 					  multisync.out_syncs);
394 	if (ret)
395 		return ret;
396 
397 	se->in_sync_count = multisync.in_sync_count;
398 	se->in_syncs = multisync.in_syncs;
399 	se->flags |= DRM_V3D_EXT_ID_MULTI_SYNC;
400 	se->wait_stage = multisync.wait_stage;
401 
402 	return 0;
403 }
404 
405 /* Get data for the indirect CSD job submission. */
406 static int
407 v3d_get_cpu_indirect_csd_params(struct drm_file *file_priv,
408 				struct drm_v3d_extension __user *ext,
409 				struct v3d_cpu_job *job)
410 {
411 	struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
412 	struct v3d_dev *v3d = v3d_priv->v3d;
413 	struct drm_v3d_indirect_csd indirect_csd;
414 	struct v3d_indirect_csd_info *info = &job->indirect_csd;
415 
416 	if (!job) {
417 		DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
418 		return -EINVAL;
419 	}
420 
421 	if (job->job_type) {
422 		DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
423 		return -EINVAL;
424 	}
425 
426 	if (copy_from_user(&indirect_csd, ext, sizeof(indirect_csd)))
427 		return -EFAULT;
428 
429 	if (!v3d_has_csd(v3d)) {
430 		DRM_DEBUG("Attempting CSD submit on non-CSD hardware.\n");
431 		return -EINVAL;
432 	}
433 
434 	job->job_type = V3D_CPU_JOB_TYPE_INDIRECT_CSD;
435 	info->offset = indirect_csd.offset;
436 	info->wg_size = indirect_csd.wg_size;
437 	memcpy(&info->wg_uniform_offsets, &indirect_csd.wg_uniform_offsets,
438 	       sizeof(indirect_csd.wg_uniform_offsets));
439 
440 	info->indirect = drm_gem_object_lookup(file_priv, indirect_csd.indirect);
441 
442 	return v3d_setup_csd_jobs_and_bos(file_priv, v3d, &indirect_csd.submit,
443 					  &info->job, &info->clean_job,
444 					  NULL, &info->acquire_ctx);
445 }
446 
447 /* Get data for the query timestamp job submission. */
448 static int
449 v3d_get_cpu_timestamp_query_params(struct drm_file *file_priv,
450 				   struct drm_v3d_extension __user *ext,
451 				   struct v3d_cpu_job *job)
452 {
453 	u32 __user *offsets, *syncs;
454 	struct drm_v3d_timestamp_query timestamp;
455 	unsigned int i;
456 	int err;
457 
458 	if (!job) {
459 		DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
460 		return -EINVAL;
461 	}
462 
463 	if (job->job_type) {
464 		DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
465 		return -EINVAL;
466 	}
467 
468 	if (copy_from_user(&timestamp, ext, sizeof(timestamp)))
469 		return -EFAULT;
470 
471 	if (timestamp.pad)
472 		return -EINVAL;
473 
474 	job->job_type = V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY;
475 
476 	job->timestamp_query.queries = kvmalloc_array(timestamp.count,
477 						      sizeof(struct v3d_timestamp_query),
478 						      GFP_KERNEL);
479 	if (!job->timestamp_query.queries)
480 		return -ENOMEM;
481 
482 	offsets = u64_to_user_ptr(timestamp.offsets);
483 	syncs = u64_to_user_ptr(timestamp.syncs);
484 
485 	for (i = 0; i < timestamp.count; i++) {
486 		u32 offset, sync;
487 
488 		if (copy_from_user(&offset, offsets++, sizeof(offset))) {
489 			err = -EFAULT;
490 			goto error;
491 		}
492 
493 		job->timestamp_query.queries[i].offset = offset;
494 
495 		if (copy_from_user(&sync, syncs++, sizeof(sync))) {
496 			err = -EFAULT;
497 			goto error;
498 		}
499 
500 		job->timestamp_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
501 	}
502 	job->timestamp_query.count = timestamp.count;
503 
504 	return 0;
505 
506 error:
507 	v3d_timestamp_query_info_free(&job->timestamp_query, i);
508 	return err;
509 }
510 
511 static int
512 v3d_get_cpu_reset_timestamp_params(struct drm_file *file_priv,
513 				   struct drm_v3d_extension __user *ext,
514 				   struct v3d_cpu_job *job)
515 {
516 	u32 __user *syncs;
517 	struct drm_v3d_reset_timestamp_query reset;
518 	unsigned int i;
519 	int err;
520 
521 	if (!job) {
522 		DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
523 		return -EINVAL;
524 	}
525 
526 	if (job->job_type) {
527 		DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
528 		return -EINVAL;
529 	}
530 
531 	if (copy_from_user(&reset, ext, sizeof(reset)))
532 		return -EFAULT;
533 
534 	job->job_type = V3D_CPU_JOB_TYPE_RESET_TIMESTAMP_QUERY;
535 
536 	job->timestamp_query.queries = kvmalloc_array(reset.count,
537 						      sizeof(struct v3d_timestamp_query),
538 						      GFP_KERNEL);
539 	if (!job->timestamp_query.queries)
540 		return -ENOMEM;
541 
542 	syncs = u64_to_user_ptr(reset.syncs);
543 
544 	for (i = 0; i < reset.count; i++) {
545 		u32 sync;
546 
547 		job->timestamp_query.queries[i].offset = reset.offset + 8 * i;
548 
549 		if (copy_from_user(&sync, syncs++, sizeof(sync))) {
550 			err = -EFAULT;
551 			goto error;
552 		}
553 
554 		job->timestamp_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
555 	}
556 	job->timestamp_query.count = reset.count;
557 
558 	return 0;
559 
560 error:
561 	v3d_timestamp_query_info_free(&job->timestamp_query, i);
562 	return err;
563 }
564 
565 /* Get data for the copy timestamp query results job submission. */
566 static int
567 v3d_get_cpu_copy_query_results_params(struct drm_file *file_priv,
568 				      struct drm_v3d_extension __user *ext,
569 				      struct v3d_cpu_job *job)
570 {
571 	u32 __user *offsets, *syncs;
572 	struct drm_v3d_copy_timestamp_query copy;
573 	unsigned int i;
574 	int err;
575 
576 	if (!job) {
577 		DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
578 		return -EINVAL;
579 	}
580 
581 	if (job->job_type) {
582 		DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
583 		return -EINVAL;
584 	}
585 
586 	if (copy_from_user(&copy, ext, sizeof(copy)))
587 		return -EFAULT;
588 
589 	if (copy.pad)
590 		return -EINVAL;
591 
592 	job->job_type = V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY;
593 
594 	job->timestamp_query.queries = kvmalloc_array(copy.count,
595 						      sizeof(struct v3d_timestamp_query),
596 						      GFP_KERNEL);
597 	if (!job->timestamp_query.queries)
598 		return -ENOMEM;
599 
600 	offsets = u64_to_user_ptr(copy.offsets);
601 	syncs = u64_to_user_ptr(copy.syncs);
602 
603 	for (i = 0; i < copy.count; i++) {
604 		u32 offset, sync;
605 
606 		if (copy_from_user(&offset, offsets++, sizeof(offset))) {
607 			err = -EFAULT;
608 			goto error;
609 		}
610 
611 		job->timestamp_query.queries[i].offset = offset;
612 
613 		if (copy_from_user(&sync, syncs++, sizeof(sync))) {
614 			err = -EFAULT;
615 			goto error;
616 		}
617 
618 		job->timestamp_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
619 	}
620 	job->timestamp_query.count = copy.count;
621 
622 	job->copy.do_64bit = copy.do_64bit;
623 	job->copy.do_partial = copy.do_partial;
624 	job->copy.availability_bit = copy.availability_bit;
625 	job->copy.offset = copy.offset;
626 	job->copy.stride = copy.stride;
627 
628 	return 0;
629 
630 error:
631 	v3d_timestamp_query_info_free(&job->timestamp_query, i);
632 	return err;
633 }
634 
635 static int
636 v3d_get_cpu_reset_performance_params(struct drm_file *file_priv,
637 				     struct drm_v3d_extension __user *ext,
638 				     struct v3d_cpu_job *job)
639 {
640 	u32 __user *syncs;
641 	u64 __user *kperfmon_ids;
642 	struct drm_v3d_reset_performance_query reset;
643 	unsigned int i, j;
644 	int err;
645 
646 	if (!job) {
647 		DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
648 		return -EINVAL;
649 	}
650 
651 	if (job->job_type) {
652 		DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
653 		return -EINVAL;
654 	}
655 
656 	if (copy_from_user(&reset, ext, sizeof(reset)))
657 		return -EFAULT;
658 
659 	if (reset.nperfmons > V3D_MAX_PERFMONS)
660 		return -EINVAL;
661 
662 	job->job_type = V3D_CPU_JOB_TYPE_RESET_PERFORMANCE_QUERY;
663 
664 	job->performance_query.queries = kvmalloc_array(reset.count,
665 							sizeof(struct v3d_performance_query),
666 							GFP_KERNEL);
667 	if (!job->performance_query.queries)
668 		return -ENOMEM;
669 
670 	syncs = u64_to_user_ptr(reset.syncs);
671 	kperfmon_ids = u64_to_user_ptr(reset.kperfmon_ids);
672 
673 	for (i = 0; i < reset.count; i++) {
674 		u32 sync;
675 		u64 ids;
676 		u32 __user *ids_pointer;
677 		u32 id;
678 
679 		if (copy_from_user(&sync, syncs++, sizeof(sync))) {
680 			err = -EFAULT;
681 			goto error;
682 		}
683 
684 		if (copy_from_user(&ids, kperfmon_ids++, sizeof(ids))) {
685 			err = -EFAULT;
686 			goto error;
687 		}
688 
689 		ids_pointer = u64_to_user_ptr(ids);
690 
691 		for (j = 0; j < reset.nperfmons; j++) {
692 			if (copy_from_user(&id, ids_pointer++, sizeof(id))) {
693 				err = -EFAULT;
694 				goto error;
695 			}
696 
697 			job->performance_query.queries[i].kperfmon_ids[j] = id;
698 		}
699 
700 		job->performance_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
701 	}
702 	job->performance_query.count = reset.count;
703 	job->performance_query.nperfmons = reset.nperfmons;
704 
705 	return 0;
706 
707 error:
708 	v3d_performance_query_info_free(&job->performance_query, i);
709 	return err;
710 }
711 
712 static int
713 v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv,
714 					  struct drm_v3d_extension __user *ext,
715 					  struct v3d_cpu_job *job)
716 {
717 	u32 __user *syncs;
718 	u64 __user *kperfmon_ids;
719 	struct drm_v3d_copy_performance_query copy;
720 	unsigned int i, j;
721 	int err;
722 
723 	if (!job) {
724 		DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
725 		return -EINVAL;
726 	}
727 
728 	if (job->job_type) {
729 		DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
730 		return -EINVAL;
731 	}
732 
733 	if (copy_from_user(&copy, ext, sizeof(copy)))
734 		return -EFAULT;
735 
736 	if (copy.pad)
737 		return -EINVAL;
738 
739 	if (copy.nperfmons > V3D_MAX_PERFMONS)
740 		return -EINVAL;
741 
742 	job->job_type = V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY;
743 
744 	job->performance_query.queries = kvmalloc_array(copy.count,
745 							sizeof(struct v3d_performance_query),
746 							GFP_KERNEL);
747 	if (!job->performance_query.queries)
748 		return -ENOMEM;
749 
750 	syncs = u64_to_user_ptr(copy.syncs);
751 	kperfmon_ids = u64_to_user_ptr(copy.kperfmon_ids);
752 
753 	for (i = 0; i < copy.count; i++) {
754 		u32 sync;
755 		u64 ids;
756 		u32 __user *ids_pointer;
757 		u32 id;
758 
759 		if (copy_from_user(&sync, syncs++, sizeof(sync))) {
760 			err = -EFAULT;
761 			goto error;
762 		}
763 
764 		if (copy_from_user(&ids, kperfmon_ids++, sizeof(ids))) {
765 			err = -EFAULT;
766 			goto error;
767 		}
768 
769 		ids_pointer = u64_to_user_ptr(ids);
770 
771 		for (j = 0; j < copy.nperfmons; j++) {
772 			if (copy_from_user(&id, ids_pointer++, sizeof(id))) {
773 				err = -EFAULT;
774 				goto error;
775 			}
776 
777 			job->performance_query.queries[i].kperfmon_ids[j] = id;
778 		}
779 
780 		job->performance_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
781 	}
782 	job->performance_query.count = copy.count;
783 	job->performance_query.nperfmons = copy.nperfmons;
784 	job->performance_query.ncounters = copy.ncounters;
785 
786 	job->copy.do_64bit = copy.do_64bit;
787 	job->copy.do_partial = copy.do_partial;
788 	job->copy.availability_bit = copy.availability_bit;
789 	job->copy.offset = copy.offset;
790 	job->copy.stride = copy.stride;
791 
792 	return 0;
793 
794 error:
795 	v3d_performance_query_info_free(&job->performance_query, i);
796 	return err;
797 }
798 
799 /* Whenever userspace sets ioctl extensions, v3d_get_extensions parses data
800  * according to the extension id (name).
801  */
802 static int
803 v3d_get_extensions(struct drm_file *file_priv,
804 		   u64 ext_handles,
805 		   struct v3d_submit_ext *se,
806 		   struct v3d_cpu_job *job)
807 {
808 	struct drm_v3d_extension __user *user_ext;
809 	int ret;
810 
811 	user_ext = u64_to_user_ptr(ext_handles);
812 	while (user_ext) {
813 		struct drm_v3d_extension ext;
814 
815 		if (copy_from_user(&ext, user_ext, sizeof(ext))) {
816 			DRM_DEBUG("Failed to copy submit extension\n");
817 			return -EFAULT;
818 		}
819 
820 		switch (ext.id) {
821 		case DRM_V3D_EXT_ID_MULTI_SYNC:
822 			ret = v3d_get_multisync_submit_deps(file_priv, user_ext, se);
823 			break;
824 		case DRM_V3D_EXT_ID_CPU_INDIRECT_CSD:
825 			ret = v3d_get_cpu_indirect_csd_params(file_priv, user_ext, job);
826 			break;
827 		case DRM_V3D_EXT_ID_CPU_TIMESTAMP_QUERY:
828 			ret = v3d_get_cpu_timestamp_query_params(file_priv, user_ext, job);
829 			break;
830 		case DRM_V3D_EXT_ID_CPU_RESET_TIMESTAMP_QUERY:
831 			ret = v3d_get_cpu_reset_timestamp_params(file_priv, user_ext, job);
832 			break;
833 		case DRM_V3D_EXT_ID_CPU_COPY_TIMESTAMP_QUERY:
834 			ret = v3d_get_cpu_copy_query_results_params(file_priv, user_ext, job);
835 			break;
836 		case DRM_V3D_EXT_ID_CPU_RESET_PERFORMANCE_QUERY:
837 			ret = v3d_get_cpu_reset_performance_params(file_priv, user_ext, job);
838 			break;
839 		case DRM_V3D_EXT_ID_CPU_COPY_PERFORMANCE_QUERY:
840 			ret = v3d_get_cpu_copy_performance_query_params(file_priv, user_ext, job);
841 			break;
842 		default:
843 			DRM_DEBUG_DRIVER("Unknown extension id: %d\n", ext.id);
844 			return -EINVAL;
845 		}
846 
847 		if (ret)
848 			return ret;
849 
850 		user_ext = u64_to_user_ptr(ext.next);
851 	}
852 
853 	return 0;
854 }
855 
856 /**
857  * v3d_submit_cl_ioctl() - Submits a job (frame) to the V3D.
858  * @dev: DRM device
859  * @data: ioctl argument
860  * @file_priv: DRM file for this fd
861  *
862  * This is the main entrypoint for userspace to submit a 3D frame to
863  * the GPU.  Userspace provides the binner command list (if
864  * applicable), and the kernel sets up the render command list to draw
865  * to the framebuffer described in the ioctl, using the command lists
866  * that the 3D engine's binner will produce.
867  */
868 int
869 v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
870 		    struct drm_file *file_priv)
871 {
872 	struct v3d_dev *v3d = to_v3d_dev(dev);
873 	struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
874 	struct drm_v3d_submit_cl *args = data;
875 	struct v3d_submit_ext se = {0};
876 	struct v3d_bin_job *bin = NULL;
877 	struct v3d_render_job *render = NULL;
878 	struct v3d_job *clean_job = NULL;
879 	struct v3d_job *last_job;
880 	struct ww_acquire_ctx acquire_ctx;
881 	int ret = 0;
882 
883 	trace_v3d_submit_cl_ioctl(&v3d->drm, args->rcl_start, args->rcl_end);
884 
885 	if (args->pad)
886 		return -EINVAL;
887 
888 	if (args->flags &&
889 	    args->flags & ~(DRM_V3D_SUBMIT_CL_FLUSH_CACHE |
890 			    DRM_V3D_SUBMIT_EXTENSION)) {
891 		DRM_INFO("invalid flags: %d\n", args->flags);
892 		return -EINVAL;
893 	}
894 
895 	if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
896 		ret = v3d_get_extensions(file_priv, args->extensions, &se, NULL);
897 		if (ret) {
898 			DRM_DEBUG("Failed to get extensions.\n");
899 			return ret;
900 		}
901 	}
902 
903 	ret = v3d_job_allocate((void *)&render, sizeof(*render));
904 	if (ret)
905 		return ret;
906 
907 	ret = v3d_job_init(v3d, file_priv, &render->base,
908 			   v3d_render_job_free, args->in_sync_rcl, &se, V3D_RENDER);
909 	if (ret) {
910 		v3d_job_deallocate((void *)&render);
911 		goto fail;
912 	}
913 
914 	render->start = args->rcl_start;
915 	render->end = args->rcl_end;
916 	INIT_LIST_HEAD(&render->unref_list);
917 
918 	if (args->bcl_start != args->bcl_end) {
919 		ret = v3d_job_allocate((void *)&bin, sizeof(*bin));
920 		if (ret)
921 			goto fail;
922 
923 		ret = v3d_job_init(v3d, file_priv, &bin->base,
924 				   v3d_job_free, args->in_sync_bcl, &se, V3D_BIN);
925 		if (ret) {
926 			v3d_job_deallocate((void *)&bin);
927 			goto fail;
928 		}
929 
930 		bin->start = args->bcl_start;
931 		bin->end = args->bcl_end;
932 		bin->qma = args->qma;
933 		bin->qms = args->qms;
934 		bin->qts = args->qts;
935 		bin->render = render;
936 	}
937 
938 	if (args->flags & DRM_V3D_SUBMIT_CL_FLUSH_CACHE) {
939 		ret = v3d_job_allocate((void *)&clean_job, sizeof(*clean_job));
940 		if (ret)
941 			goto fail;
942 
943 		ret = v3d_job_init(v3d, file_priv, clean_job,
944 				   v3d_job_free, 0, NULL, V3D_CACHE_CLEAN);
945 		if (ret) {
946 			v3d_job_deallocate((void *)&clean_job);
947 			goto fail;
948 		}
949 
950 		last_job = clean_job;
951 	} else {
952 		last_job = &render->base;
953 	}
954 
955 	ret = v3d_lookup_bos(dev, file_priv, last_job,
956 			     args->bo_handles, args->bo_handle_count);
957 	if (ret)
958 		goto fail;
959 
960 	ret = v3d_lock_bo_reservations(last_job, &acquire_ctx);
961 	if (ret)
962 		goto fail;
963 
964 	if (args->perfmon_id) {
965 		render->base.perfmon = v3d_perfmon_find(v3d_priv,
966 							args->perfmon_id);
967 
968 		if (!render->base.perfmon) {
969 			ret = -ENOENT;
970 			goto fail_perfmon;
971 		}
972 	}
973 
974 	mutex_lock(&v3d->sched_lock);
975 	if (bin) {
976 		bin->base.perfmon = render->base.perfmon;
977 		v3d_perfmon_get(bin->base.perfmon);
978 		v3d_push_job(&bin->base);
979 
980 		ret = drm_sched_job_add_dependency(&render->base.base,
981 						   dma_fence_get(bin->base.done_fence));
982 		if (ret)
983 			goto fail_unreserve;
984 	}
985 
986 	v3d_push_job(&render->base);
987 
988 	if (clean_job) {
989 		struct dma_fence *render_fence =
990 			dma_fence_get(render->base.done_fence);
991 		ret = drm_sched_job_add_dependency(&clean_job->base,
992 						   render_fence);
993 		if (ret)
994 			goto fail_unreserve;
995 		clean_job->perfmon = render->base.perfmon;
996 		v3d_perfmon_get(clean_job->perfmon);
997 		v3d_push_job(clean_job);
998 	}
999 
1000 	mutex_unlock(&v3d->sched_lock);
1001 
1002 	v3d_attach_fences_and_unlock_reservation(file_priv,
1003 						 last_job,
1004 						 &acquire_ctx,
1005 						 args->out_sync,
1006 						 &se,
1007 						 last_job->done_fence);
1008 
1009 	v3d_job_put(&bin->base);
1010 	v3d_job_put(&render->base);
1011 	v3d_job_put(clean_job);
1012 
1013 	return 0;
1014 
1015 fail_unreserve:
1016 	mutex_unlock(&v3d->sched_lock);
1017 fail_perfmon:
1018 	drm_gem_unlock_reservations(last_job->bo,
1019 				    last_job->bo_count, &acquire_ctx);
1020 fail:
1021 	v3d_job_cleanup((void *)bin);
1022 	v3d_job_cleanup((void *)render);
1023 	v3d_job_cleanup(clean_job);
1024 	v3d_put_multisync_post_deps(&se);
1025 
1026 	return ret;
1027 }
1028 
1029 /**
1030  * v3d_submit_tfu_ioctl() - Submits a TFU (texture formatting) job to the V3D.
1031  * @dev: DRM device
1032  * @data: ioctl argument
1033  * @file_priv: DRM file for this fd
1034  *
1035  * Userspace provides the register setup for the TFU, which we don't
1036  * need to validate since the TFU is behind the MMU.
1037  */
1038 int
1039 v3d_submit_tfu_ioctl(struct drm_device *dev, void *data,
1040 		     struct drm_file *file_priv)
1041 {
1042 	struct v3d_dev *v3d = to_v3d_dev(dev);
1043 	struct drm_v3d_submit_tfu *args = data;
1044 	struct v3d_submit_ext se = {0};
1045 	struct v3d_tfu_job *job = NULL;
1046 	struct ww_acquire_ctx acquire_ctx;
1047 	int ret = 0;
1048 
1049 	trace_v3d_submit_tfu_ioctl(&v3d->drm, args->iia);
1050 
1051 	if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) {
1052 		DRM_DEBUG("invalid flags: %d\n", args->flags);
1053 		return -EINVAL;
1054 	}
1055 
1056 	if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
1057 		ret = v3d_get_extensions(file_priv, args->extensions, &se, NULL);
1058 		if (ret) {
1059 			DRM_DEBUG("Failed to get extensions.\n");
1060 			return ret;
1061 		}
1062 	}
1063 
1064 	ret = v3d_job_allocate((void *)&job, sizeof(*job));
1065 	if (ret)
1066 		return ret;
1067 
1068 	ret = v3d_job_init(v3d, file_priv, &job->base,
1069 			   v3d_job_free, args->in_sync, &se, V3D_TFU);
1070 	if (ret) {
1071 		v3d_job_deallocate((void *)&job);
1072 		goto fail;
1073 	}
1074 
1075 	job->base.bo = kcalloc(ARRAY_SIZE(args->bo_handles),
1076 			       sizeof(*job->base.bo), GFP_KERNEL);
1077 	if (!job->base.bo) {
1078 		ret = -ENOMEM;
1079 		goto fail;
1080 	}
1081 
1082 	job->args = *args;
1083 
1084 	for (job->base.bo_count = 0;
1085 	     job->base.bo_count < ARRAY_SIZE(args->bo_handles);
1086 	     job->base.bo_count++) {
1087 		struct drm_gem_object *bo;
1088 
1089 		if (!args->bo_handles[job->base.bo_count])
1090 			break;
1091 
1092 		bo = drm_gem_object_lookup(file_priv, args->bo_handles[job->base.bo_count]);
1093 		if (!bo) {
1094 			DRM_DEBUG("Failed to look up GEM BO %d: %d\n",
1095 				  job->base.bo_count,
1096 				  args->bo_handles[job->base.bo_count]);
1097 			ret = -ENOENT;
1098 			goto fail;
1099 		}
1100 		job->base.bo[job->base.bo_count] = bo;
1101 	}
1102 
1103 	ret = v3d_lock_bo_reservations(&job->base, &acquire_ctx);
1104 	if (ret)
1105 		goto fail;
1106 
1107 	mutex_lock(&v3d->sched_lock);
1108 	v3d_push_job(&job->base);
1109 	mutex_unlock(&v3d->sched_lock);
1110 
1111 	v3d_attach_fences_and_unlock_reservation(file_priv,
1112 						 &job->base, &acquire_ctx,
1113 						 args->out_sync,
1114 						 &se,
1115 						 job->base.done_fence);
1116 
1117 	v3d_job_put(&job->base);
1118 
1119 	return 0;
1120 
1121 fail:
1122 	v3d_job_cleanup((void *)job);
1123 	v3d_put_multisync_post_deps(&se);
1124 
1125 	return ret;
1126 }
1127 
1128 /**
1129  * v3d_submit_csd_ioctl() - Submits a CSD (compute shader) job to the V3D.
1130  * @dev: DRM device
1131  * @data: ioctl argument
1132  * @file_priv: DRM file for this fd
1133  *
1134  * Userspace provides the register setup for the CSD, which we don't
1135  * need to validate since the CSD is behind the MMU.
1136  */
1137 int
1138 v3d_submit_csd_ioctl(struct drm_device *dev, void *data,
1139 		     struct drm_file *file_priv)
1140 {
1141 	struct v3d_dev *v3d = to_v3d_dev(dev);
1142 	struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
1143 	struct drm_v3d_submit_csd *args = data;
1144 	struct v3d_submit_ext se = {0};
1145 	struct v3d_csd_job *job = NULL;
1146 	struct v3d_job *clean_job = NULL;
1147 	struct ww_acquire_ctx acquire_ctx;
1148 	int ret;
1149 
1150 	trace_v3d_submit_csd_ioctl(&v3d->drm, args->cfg[5], args->cfg[6]);
1151 
1152 	if (args->pad)
1153 		return -EINVAL;
1154 
1155 	if (!v3d_has_csd(v3d)) {
1156 		DRM_DEBUG("Attempting CSD submit on non-CSD hardware\n");
1157 		return -EINVAL;
1158 	}
1159 
1160 	if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) {
1161 		DRM_INFO("invalid flags: %d\n", args->flags);
1162 		return -EINVAL;
1163 	}
1164 
1165 	if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
1166 		ret = v3d_get_extensions(file_priv, args->extensions, &se, NULL);
1167 		if (ret) {
1168 			DRM_DEBUG("Failed to get extensions.\n");
1169 			return ret;
1170 		}
1171 	}
1172 
1173 	ret = v3d_setup_csd_jobs_and_bos(file_priv, v3d, args,
1174 					 &job, &clean_job, &se,
1175 					 &acquire_ctx);
1176 	if (ret)
1177 		goto fail;
1178 
1179 	if (args->perfmon_id) {
1180 		job->base.perfmon = v3d_perfmon_find(v3d_priv,
1181 						     args->perfmon_id);
1182 		if (!job->base.perfmon) {
1183 			ret = -ENOENT;
1184 			goto fail_perfmon;
1185 		}
1186 	}
1187 
1188 	mutex_lock(&v3d->sched_lock);
1189 	v3d_push_job(&job->base);
1190 
1191 	ret = drm_sched_job_add_dependency(&clean_job->base,
1192 					   dma_fence_get(job->base.done_fence));
1193 	if (ret)
1194 		goto fail_unreserve;
1195 
1196 	v3d_push_job(clean_job);
1197 	mutex_unlock(&v3d->sched_lock);
1198 
1199 	v3d_attach_fences_and_unlock_reservation(file_priv,
1200 						 clean_job,
1201 						 &acquire_ctx,
1202 						 args->out_sync,
1203 						 &se,
1204 						 clean_job->done_fence);
1205 
1206 	v3d_job_put(&job->base);
1207 	v3d_job_put(clean_job);
1208 
1209 	return 0;
1210 
1211 fail_unreserve:
1212 	mutex_unlock(&v3d->sched_lock);
1213 fail_perfmon:
1214 	drm_gem_unlock_reservations(clean_job->bo, clean_job->bo_count,
1215 				    &acquire_ctx);
1216 fail:
1217 	v3d_job_cleanup((void *)job);
1218 	v3d_job_cleanup(clean_job);
1219 	v3d_put_multisync_post_deps(&se);
1220 
1221 	return ret;
1222 }
1223 
1224 static const unsigned int cpu_job_bo_handle_count[] = {
1225 	[V3D_CPU_JOB_TYPE_INDIRECT_CSD] = 1,
1226 	[V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY] = 1,
1227 	[V3D_CPU_JOB_TYPE_RESET_TIMESTAMP_QUERY] = 1,
1228 	[V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY] = 2,
1229 	[V3D_CPU_JOB_TYPE_RESET_PERFORMANCE_QUERY] = 0,
1230 	[V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY] = 1,
1231 };
1232 
1233 /**
1234  * v3d_submit_cpu_ioctl() - Submits a CPU job to the V3D.
1235  * @dev: DRM device
1236  * @data: ioctl argument
1237  * @file_priv: DRM file for this fd
1238  *
1239  * Userspace specifies the CPU job type and data required to perform its
1240  * operations through the drm_v3d_extension struct.
1241  */
1242 int
1243 v3d_submit_cpu_ioctl(struct drm_device *dev, void *data,
1244 		     struct drm_file *file_priv)
1245 {
1246 	struct v3d_dev *v3d = to_v3d_dev(dev);
1247 	struct drm_v3d_submit_cpu *args = data;
1248 	struct v3d_submit_ext se = {0};
1249 	struct v3d_submit_ext *out_se = NULL;
1250 	struct v3d_cpu_job *cpu_job = NULL;
1251 	struct v3d_csd_job *csd_job = NULL;
1252 	struct v3d_job *clean_job = NULL;
1253 	struct ww_acquire_ctx acquire_ctx;
1254 	int ret;
1255 
1256 	if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) {
1257 		DRM_INFO("Invalid flags: %d\n", args->flags);
1258 		return -EINVAL;
1259 	}
1260 
1261 	ret = v3d_job_allocate((void *)&cpu_job, sizeof(*cpu_job));
1262 	if (ret)
1263 		return ret;
1264 
1265 	if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
1266 		ret = v3d_get_extensions(file_priv, args->extensions, &se, cpu_job);
1267 		if (ret) {
1268 			DRM_DEBUG("Failed to get extensions.\n");
1269 			goto fail;
1270 		}
1271 	}
1272 
1273 	/* Every CPU job must have a CPU job user extension */
1274 	if (!cpu_job->job_type) {
1275 		DRM_DEBUG("CPU job must have a CPU job user extension.\n");
1276 		ret = -EINVAL;
1277 		goto fail;
1278 	}
1279 
1280 	if (args->bo_handle_count != cpu_job_bo_handle_count[cpu_job->job_type]) {
1281 		DRM_DEBUG("This CPU job was not submitted with the proper number of BOs.\n");
1282 		ret = -EINVAL;
1283 		goto fail;
1284 	}
1285 
1286 	trace_v3d_submit_cpu_ioctl(&v3d->drm, cpu_job->job_type);
1287 
1288 	ret = v3d_job_init(v3d, file_priv, &cpu_job->base,
1289 			   v3d_job_free, 0, &se, V3D_CPU);
1290 	if (ret) {
1291 		v3d_job_deallocate((void *)&cpu_job);
1292 		goto fail;
1293 	}
1294 
1295 	clean_job = cpu_job->indirect_csd.clean_job;
1296 	csd_job = cpu_job->indirect_csd.job;
1297 
1298 	if (args->bo_handle_count) {
1299 		ret = v3d_lookup_bos(dev, file_priv, &cpu_job->base,
1300 				     args->bo_handles, args->bo_handle_count);
1301 		if (ret)
1302 			goto fail;
1303 
1304 		ret = v3d_lock_bo_reservations(&cpu_job->base, &acquire_ctx);
1305 		if (ret)
1306 			goto fail;
1307 	}
1308 
1309 	mutex_lock(&v3d->sched_lock);
1310 	v3d_push_job(&cpu_job->base);
1311 
1312 	switch (cpu_job->job_type) {
1313 	case V3D_CPU_JOB_TYPE_INDIRECT_CSD:
1314 		ret = drm_sched_job_add_dependency(&csd_job->base.base,
1315 						   dma_fence_get(cpu_job->base.done_fence));
1316 		if (ret)
1317 			goto fail_unreserve;
1318 
1319 		v3d_push_job(&csd_job->base);
1320 
1321 		ret = drm_sched_job_add_dependency(&clean_job->base,
1322 						   dma_fence_get(csd_job->base.done_fence));
1323 		if (ret)
1324 			goto fail_unreserve;
1325 
1326 		v3d_push_job(clean_job);
1327 
1328 		break;
1329 	default:
1330 		break;
1331 	}
1332 	mutex_unlock(&v3d->sched_lock);
1333 
1334 	out_se = (cpu_job->job_type == V3D_CPU_JOB_TYPE_INDIRECT_CSD) ? NULL : &se;
1335 
1336 	v3d_attach_fences_and_unlock_reservation(file_priv,
1337 						 &cpu_job->base,
1338 						 &acquire_ctx, 0,
1339 						 out_se, cpu_job->base.done_fence);
1340 
1341 	switch (cpu_job->job_type) {
1342 	case V3D_CPU_JOB_TYPE_INDIRECT_CSD:
1343 		v3d_attach_fences_and_unlock_reservation(file_priv,
1344 							 clean_job,
1345 							 &cpu_job->indirect_csd.acquire_ctx,
1346 							 0, &se, clean_job->done_fence);
1347 		break;
1348 	default:
1349 		break;
1350 	}
1351 
1352 	v3d_job_put(&cpu_job->base);
1353 	v3d_job_put(&csd_job->base);
1354 	v3d_job_put(clean_job);
1355 
1356 	return 0;
1357 
1358 fail_unreserve:
1359 	mutex_unlock(&v3d->sched_lock);
1360 
1361 	drm_gem_unlock_reservations(cpu_job->base.bo, cpu_job->base.bo_count,
1362 				    &acquire_ctx);
1363 
1364 	drm_gem_unlock_reservations(clean_job->bo, clean_job->bo_count,
1365 				    &cpu_job->indirect_csd.acquire_ctx);
1366 
1367 fail:
1368 	v3d_job_cleanup((void *)cpu_job);
1369 	v3d_job_cleanup((void *)csd_job);
1370 	v3d_job_cleanup(clean_job);
1371 	v3d_put_multisync_post_deps(&se);
1372 	kvfree(cpu_job->timestamp_query.queries);
1373 	kvfree(cpu_job->performance_query.queries);
1374 
1375 	return ret;
1376 }
1377