xref: /linux-6.15/drivers/gpu/drm/v3d/v3d_submit.c (revision bae7cb5d)
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 int
151 v3d_job_init(struct v3d_dev *v3d, struct drm_file *file_priv,
152 	     struct v3d_job *job, void (*free)(struct kref *ref),
153 	     u32 in_sync, struct v3d_submit_ext *se, enum v3d_queue queue)
154 {
155 	struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
156 	bool has_multisync = se && (se->flags & DRM_V3D_EXT_ID_MULTI_SYNC);
157 	int ret, i;
158 
159 	job->v3d = v3d;
160 	job->free = free;
161 	job->file = file_priv;
162 
163 	ret = drm_sched_job_init(&job->base, &v3d_priv->sched_entity[queue],
164 				 1, v3d_priv);
165 	if (ret)
166 		return ret;
167 
168 	if (has_multisync) {
169 		if (se->in_sync_count && se->wait_stage == queue) {
170 			struct drm_v3d_sem __user *handle = u64_to_user_ptr(se->in_syncs);
171 
172 			for (i = 0; i < se->in_sync_count; i++) {
173 				struct drm_v3d_sem in;
174 
175 				if (copy_from_user(&in, handle++, sizeof(in))) {
176 					ret = -EFAULT;
177 					DRM_DEBUG("Failed to copy wait dep handle.\n");
178 					goto fail_deps;
179 				}
180 				ret = drm_sched_job_add_syncobj_dependency(&job->base, file_priv, in.handle, 0);
181 
182 				// TODO: Investigate why this was filtered out for the IOCTL.
183 				if (ret && ret != -ENOENT)
184 					goto fail_deps;
185 			}
186 		}
187 	} else {
188 		ret = drm_sched_job_add_syncobj_dependency(&job->base, file_priv, in_sync, 0);
189 
190 		// TODO: Investigate why this was filtered out for the IOCTL.
191 		if (ret && ret != -ENOENT)
192 			goto fail_deps;
193 	}
194 
195 	kref_init(&job->refcount);
196 
197 	return 0;
198 
199 fail_deps:
200 	drm_sched_job_cleanup(&job->base);
201 	return ret;
202 }
203 
204 static void
205 v3d_push_job(struct v3d_job *job)
206 {
207 	drm_sched_job_arm(&job->base);
208 
209 	job->done_fence = dma_fence_get(&job->base.s_fence->finished);
210 
211 	/* put by scheduler job completion */
212 	kref_get(&job->refcount);
213 
214 	drm_sched_entity_push_job(&job->base);
215 }
216 
217 static void
218 v3d_attach_fences_and_unlock_reservation(struct drm_file *file_priv,
219 					 struct v3d_job *job,
220 					 struct ww_acquire_ctx *acquire_ctx,
221 					 u32 out_sync,
222 					 struct v3d_submit_ext *se,
223 					 struct dma_fence *done_fence)
224 {
225 	struct drm_syncobj *sync_out;
226 	bool has_multisync = se && (se->flags & DRM_V3D_EXT_ID_MULTI_SYNC);
227 	int i;
228 
229 	for (i = 0; i < job->bo_count; i++) {
230 		/* XXX: Use shared fences for read-only objects. */
231 		dma_resv_add_fence(job->bo[i]->resv, job->done_fence,
232 				   DMA_RESV_USAGE_WRITE);
233 	}
234 
235 	drm_gem_unlock_reservations(job->bo, job->bo_count, acquire_ctx);
236 
237 	/* Update the return sync object for the job */
238 	/* If it only supports a single signal semaphore*/
239 	if (!has_multisync) {
240 		sync_out = drm_syncobj_find(file_priv, out_sync);
241 		if (sync_out) {
242 			drm_syncobj_replace_fence(sync_out, done_fence);
243 			drm_syncobj_put(sync_out);
244 		}
245 		return;
246 	}
247 
248 	/* If multiple semaphores extension is supported */
249 	if (se->out_sync_count) {
250 		for (i = 0; i < se->out_sync_count; i++) {
251 			drm_syncobj_replace_fence(se->out_syncs[i].syncobj,
252 						  done_fence);
253 			drm_syncobj_put(se->out_syncs[i].syncobj);
254 		}
255 		kvfree(se->out_syncs);
256 	}
257 }
258 
259 static int
260 v3d_setup_csd_jobs_and_bos(struct drm_file *file_priv,
261 			   struct v3d_dev *v3d,
262 			   struct drm_v3d_submit_csd *args,
263 			   struct v3d_csd_job **job,
264 			   struct v3d_job **clean_job,
265 			   struct v3d_submit_ext *se,
266 			   struct ww_acquire_ctx *acquire_ctx)
267 {
268 	int ret;
269 
270 	ret = v3d_job_allocate((void *)job, sizeof(**job));
271 	if (ret)
272 		return ret;
273 
274 	ret = v3d_job_init(v3d, file_priv, &(*job)->base,
275 			   v3d_job_free, args->in_sync, se, V3D_CSD);
276 	if (ret)
277 		return ret;
278 
279 	ret = v3d_job_allocate((void *)clean_job, sizeof(**clean_job));
280 	if (ret)
281 		return ret;
282 
283 	ret = v3d_job_init(v3d, file_priv, *clean_job,
284 			   v3d_job_free, 0, NULL, V3D_CACHE_CLEAN);
285 	if (ret)
286 		return ret;
287 
288 	(*job)->args = *args;
289 
290 	ret = v3d_lookup_bos(&v3d->drm, file_priv, *clean_job,
291 			     args->bo_handles, args->bo_handle_count);
292 	if (ret)
293 		return ret;
294 
295 	return v3d_lock_bo_reservations(*clean_job, acquire_ctx);
296 }
297 
298 static void
299 v3d_put_multisync_post_deps(struct v3d_submit_ext *se)
300 {
301 	unsigned int i;
302 
303 	if (!(se && se->out_sync_count))
304 		return;
305 
306 	for (i = 0; i < se->out_sync_count; i++)
307 		drm_syncobj_put(se->out_syncs[i].syncobj);
308 	kvfree(se->out_syncs);
309 }
310 
311 static int
312 v3d_get_multisync_post_deps(struct drm_file *file_priv,
313 			    struct v3d_submit_ext *se,
314 			    u32 count, u64 handles)
315 {
316 	struct drm_v3d_sem __user *post_deps;
317 	int i, ret;
318 
319 	if (!count)
320 		return 0;
321 
322 	se->out_syncs = (struct v3d_submit_outsync *)
323 			kvmalloc_array(count,
324 				       sizeof(struct v3d_submit_outsync),
325 				       GFP_KERNEL);
326 	if (!se->out_syncs)
327 		return -ENOMEM;
328 
329 	post_deps = u64_to_user_ptr(handles);
330 
331 	for (i = 0; i < count; i++) {
332 		struct drm_v3d_sem out;
333 
334 		if (copy_from_user(&out, post_deps++, sizeof(out))) {
335 			ret = -EFAULT;
336 			DRM_DEBUG("Failed to copy post dep handles\n");
337 			goto fail;
338 		}
339 
340 		se->out_syncs[i].syncobj = drm_syncobj_find(file_priv,
341 							    out.handle);
342 		if (!se->out_syncs[i].syncobj) {
343 			ret = -EINVAL;
344 			goto fail;
345 		}
346 	}
347 	se->out_sync_count = count;
348 
349 	return 0;
350 
351 fail:
352 	for (i--; i >= 0; i--)
353 		drm_syncobj_put(se->out_syncs[i].syncobj);
354 	kvfree(se->out_syncs);
355 
356 	return ret;
357 }
358 
359 /* Get data for multiple binary semaphores synchronization. Parse syncobj
360  * to be signaled when job completes (out_sync).
361  */
362 static int
363 v3d_get_multisync_submit_deps(struct drm_file *file_priv,
364 			      struct drm_v3d_extension __user *ext,
365 			      struct v3d_submit_ext *se)
366 {
367 	struct drm_v3d_multi_sync multisync;
368 	int ret;
369 
370 	if (se->in_sync_count || se->out_sync_count) {
371 		DRM_DEBUG("Two multisync extensions were added to the same job.");
372 		return -EINVAL;
373 	}
374 
375 	if (copy_from_user(&multisync, ext, sizeof(multisync)))
376 		return -EFAULT;
377 
378 	if (multisync.pad)
379 		return -EINVAL;
380 
381 	ret = v3d_get_multisync_post_deps(file_priv, se, multisync.out_sync_count,
382 					  multisync.out_syncs);
383 	if (ret)
384 		return ret;
385 
386 	se->in_sync_count = multisync.in_sync_count;
387 	se->in_syncs = multisync.in_syncs;
388 	se->flags |= DRM_V3D_EXT_ID_MULTI_SYNC;
389 	se->wait_stage = multisync.wait_stage;
390 
391 	return 0;
392 }
393 
394 /* Get data for the indirect CSD job submission. */
395 static int
396 v3d_get_cpu_indirect_csd_params(struct drm_file *file_priv,
397 				struct drm_v3d_extension __user *ext,
398 				struct v3d_cpu_job *job)
399 {
400 	struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
401 	struct v3d_dev *v3d = v3d_priv->v3d;
402 	struct drm_v3d_indirect_csd indirect_csd;
403 	struct v3d_indirect_csd_info *info = &job->indirect_csd;
404 
405 	if (!job) {
406 		DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
407 		return -EINVAL;
408 	}
409 
410 	if (job->job_type) {
411 		DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
412 		return -EINVAL;
413 	}
414 
415 	if (copy_from_user(&indirect_csd, ext, sizeof(indirect_csd)))
416 		return -EFAULT;
417 
418 	if (!v3d_has_csd(v3d)) {
419 		DRM_DEBUG("Attempting CSD submit on non-CSD hardware.\n");
420 		return -EINVAL;
421 	}
422 
423 	job->job_type = V3D_CPU_JOB_TYPE_INDIRECT_CSD;
424 	info->offset = indirect_csd.offset;
425 	info->wg_size = indirect_csd.wg_size;
426 	memcpy(&info->wg_uniform_offsets, &indirect_csd.wg_uniform_offsets,
427 	       sizeof(indirect_csd.wg_uniform_offsets));
428 
429 	info->indirect = drm_gem_object_lookup(file_priv, indirect_csd.indirect);
430 
431 	return v3d_setup_csd_jobs_and_bos(file_priv, v3d, &indirect_csd.submit,
432 					  &info->job, &info->clean_job,
433 					  NULL, &info->acquire_ctx);
434 }
435 
436 /* Get data for the query timestamp job submission. */
437 static int
438 v3d_get_cpu_timestamp_query_params(struct drm_file *file_priv,
439 				   struct drm_v3d_extension __user *ext,
440 				   struct v3d_cpu_job *job)
441 {
442 	u32 __user *offsets, *syncs;
443 	struct drm_v3d_timestamp_query timestamp;
444 
445 	if (!job) {
446 		DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
447 		return -EINVAL;
448 	}
449 
450 	if (job->job_type) {
451 		DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
452 		return -EINVAL;
453 	}
454 
455 	if (copy_from_user(&timestamp, ext, sizeof(timestamp)))
456 		return -EFAULT;
457 
458 	if (timestamp.pad)
459 		return -EINVAL;
460 
461 	job->job_type = V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY;
462 
463 	job->timestamp_query.queries = kvmalloc_array(timestamp.count,
464 						      sizeof(struct v3d_timestamp_query),
465 						      GFP_KERNEL);
466 	if (!job->timestamp_query.queries)
467 		return -ENOMEM;
468 
469 	offsets = u64_to_user_ptr(timestamp.offsets);
470 	syncs = u64_to_user_ptr(timestamp.syncs);
471 
472 	for (int i = 0; i < timestamp.count; i++) {
473 		u32 offset, sync;
474 
475 		if (copy_from_user(&offset, offsets++, sizeof(offset))) {
476 			kvfree(job->timestamp_query.queries);
477 			return -EFAULT;
478 		}
479 
480 		job->timestamp_query.queries[i].offset = offset;
481 
482 		if (copy_from_user(&sync, syncs++, sizeof(sync))) {
483 			kvfree(job->timestamp_query.queries);
484 			return -EFAULT;
485 		}
486 
487 		job->timestamp_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
488 	}
489 	job->timestamp_query.count = timestamp.count;
490 
491 	return 0;
492 }
493 
494 static int
495 v3d_get_cpu_reset_timestamp_params(struct drm_file *file_priv,
496 				   struct drm_v3d_extension __user *ext,
497 				   struct v3d_cpu_job *job)
498 {
499 	u32 __user *syncs;
500 	struct drm_v3d_reset_timestamp_query reset;
501 
502 	if (!job) {
503 		DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
504 		return -EINVAL;
505 	}
506 
507 	if (job->job_type) {
508 		DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
509 		return -EINVAL;
510 	}
511 
512 	if (copy_from_user(&reset, ext, sizeof(reset)))
513 		return -EFAULT;
514 
515 	job->job_type = V3D_CPU_JOB_TYPE_RESET_TIMESTAMP_QUERY;
516 
517 	job->timestamp_query.queries = kvmalloc_array(reset.count,
518 						      sizeof(struct v3d_timestamp_query),
519 						      GFP_KERNEL);
520 	if (!job->timestamp_query.queries)
521 		return -ENOMEM;
522 
523 	syncs = u64_to_user_ptr(reset.syncs);
524 
525 	for (int i = 0; i < reset.count; i++) {
526 		u32 sync;
527 
528 		job->timestamp_query.queries[i].offset = reset.offset + 8 * i;
529 
530 		if (copy_from_user(&sync, syncs++, sizeof(sync))) {
531 			kvfree(job->timestamp_query.queries);
532 			return -EFAULT;
533 		}
534 
535 		job->timestamp_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
536 	}
537 	job->timestamp_query.count = reset.count;
538 
539 	return 0;
540 }
541 
542 /* Get data for the copy timestamp query results job submission. */
543 static int
544 v3d_get_cpu_copy_query_results_params(struct drm_file *file_priv,
545 				      struct drm_v3d_extension __user *ext,
546 				      struct v3d_cpu_job *job)
547 {
548 	u32 __user *offsets, *syncs;
549 	struct drm_v3d_copy_timestamp_query copy;
550 	int i;
551 
552 	if (!job) {
553 		DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
554 		return -EINVAL;
555 	}
556 
557 	if (job->job_type) {
558 		DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
559 		return -EINVAL;
560 	}
561 
562 	if (copy_from_user(&copy, ext, sizeof(copy)))
563 		return -EFAULT;
564 
565 	if (copy.pad)
566 		return -EINVAL;
567 
568 	job->job_type = V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY;
569 
570 	job->timestamp_query.queries = kvmalloc_array(copy.count,
571 						      sizeof(struct v3d_timestamp_query),
572 						      GFP_KERNEL);
573 	if (!job->timestamp_query.queries)
574 		return -ENOMEM;
575 
576 	offsets = u64_to_user_ptr(copy.offsets);
577 	syncs = u64_to_user_ptr(copy.syncs);
578 
579 	for (i = 0; i < copy.count; i++) {
580 		u32 offset, sync;
581 
582 		if (copy_from_user(&offset, offsets++, sizeof(offset))) {
583 			kvfree(job->timestamp_query.queries);
584 			return -EFAULT;
585 		}
586 
587 		job->timestamp_query.queries[i].offset = offset;
588 
589 		if (copy_from_user(&sync, syncs++, sizeof(sync))) {
590 			kvfree(job->timestamp_query.queries);
591 			return -EFAULT;
592 		}
593 
594 		job->timestamp_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
595 	}
596 	job->timestamp_query.count = copy.count;
597 
598 	job->copy.do_64bit = copy.do_64bit;
599 	job->copy.do_partial = copy.do_partial;
600 	job->copy.availability_bit = copy.availability_bit;
601 	job->copy.offset = copy.offset;
602 	job->copy.stride = copy.stride;
603 
604 	return 0;
605 }
606 
607 static int
608 v3d_get_cpu_reset_performance_params(struct drm_file *file_priv,
609 				     struct drm_v3d_extension __user *ext,
610 				     struct v3d_cpu_job *job)
611 {
612 	u32 __user *syncs;
613 	u64 __user *kperfmon_ids;
614 	struct drm_v3d_reset_performance_query reset;
615 
616 	if (!job) {
617 		DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
618 		return -EINVAL;
619 	}
620 
621 	if (job->job_type) {
622 		DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
623 		return -EINVAL;
624 	}
625 
626 	if (copy_from_user(&reset, ext, sizeof(reset)))
627 		return -EFAULT;
628 
629 	job->job_type = V3D_CPU_JOB_TYPE_RESET_PERFORMANCE_QUERY;
630 
631 	job->performance_query.queries = kvmalloc_array(reset.count,
632 							sizeof(struct v3d_performance_query),
633 							GFP_KERNEL);
634 	if (!job->performance_query.queries)
635 		return -ENOMEM;
636 
637 	syncs = u64_to_user_ptr(reset.syncs);
638 	kperfmon_ids = u64_to_user_ptr(reset.kperfmon_ids);
639 
640 	for (int i = 0; i < reset.count; i++) {
641 		u32 sync;
642 		u64 ids;
643 		u32 __user *ids_pointer;
644 		u32 id;
645 
646 		if (copy_from_user(&sync, syncs++, sizeof(sync))) {
647 			kvfree(job->performance_query.queries);
648 			return -EFAULT;
649 		}
650 
651 		job->performance_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
652 
653 		if (copy_from_user(&ids, kperfmon_ids++, sizeof(ids))) {
654 			kvfree(job->performance_query.queries);
655 			return -EFAULT;
656 		}
657 
658 		ids_pointer = u64_to_user_ptr(ids);
659 
660 		for (int j = 0; j < reset.nperfmons; j++) {
661 			if (copy_from_user(&id, ids_pointer++, sizeof(id))) {
662 				kvfree(job->performance_query.queries);
663 				return -EFAULT;
664 			}
665 
666 			job->performance_query.queries[i].kperfmon_ids[j] = id;
667 		}
668 	}
669 	job->performance_query.count = reset.count;
670 	job->performance_query.nperfmons = reset.nperfmons;
671 
672 	return 0;
673 }
674 
675 /* Whenever userspace sets ioctl extensions, v3d_get_extensions parses data
676  * according to the extension id (name).
677  */
678 static int
679 v3d_get_extensions(struct drm_file *file_priv,
680 		   u64 ext_handles,
681 		   struct v3d_submit_ext *se,
682 		   struct v3d_cpu_job *job)
683 {
684 	struct drm_v3d_extension __user *user_ext;
685 	int ret;
686 
687 	user_ext = u64_to_user_ptr(ext_handles);
688 	while (user_ext) {
689 		struct drm_v3d_extension ext;
690 
691 		if (copy_from_user(&ext, user_ext, sizeof(ext))) {
692 			DRM_DEBUG("Failed to copy submit extension\n");
693 			return -EFAULT;
694 		}
695 
696 		switch (ext.id) {
697 		case DRM_V3D_EXT_ID_MULTI_SYNC:
698 			ret = v3d_get_multisync_submit_deps(file_priv, user_ext, se);
699 			break;
700 		case DRM_V3D_EXT_ID_CPU_INDIRECT_CSD:
701 			ret = v3d_get_cpu_indirect_csd_params(file_priv, user_ext, job);
702 			break;
703 		case DRM_V3D_EXT_ID_CPU_TIMESTAMP_QUERY:
704 			ret = v3d_get_cpu_timestamp_query_params(file_priv, user_ext, job);
705 			break;
706 		case DRM_V3D_EXT_ID_CPU_RESET_TIMESTAMP_QUERY:
707 			ret = v3d_get_cpu_reset_timestamp_params(file_priv, user_ext, job);
708 			break;
709 		case DRM_V3D_EXT_ID_CPU_COPY_TIMESTAMP_QUERY:
710 			ret = v3d_get_cpu_copy_query_results_params(file_priv, user_ext, job);
711 			break;
712 		case DRM_V3D_EXT_ID_CPU_RESET_PERFORMANCE_QUERY:
713 			ret = v3d_get_cpu_reset_performance_params(file_priv, user_ext, job);
714 			break;
715 		default:
716 			DRM_DEBUG_DRIVER("Unknown extension id: %d\n", ext.id);
717 			return -EINVAL;
718 		}
719 
720 		if (ret)
721 			return ret;
722 
723 		user_ext = u64_to_user_ptr(ext.next);
724 	}
725 
726 	return 0;
727 }
728 
729 /**
730  * v3d_submit_cl_ioctl() - Submits a job (frame) to the V3D.
731  * @dev: DRM device
732  * @data: ioctl argument
733  * @file_priv: DRM file for this fd
734  *
735  * This is the main entrypoint for userspace to submit a 3D frame to
736  * the GPU.  Userspace provides the binner command list (if
737  * applicable), and the kernel sets up the render command list to draw
738  * to the framebuffer described in the ioctl, using the command lists
739  * that the 3D engine's binner will produce.
740  */
741 int
742 v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
743 		    struct drm_file *file_priv)
744 {
745 	struct v3d_dev *v3d = to_v3d_dev(dev);
746 	struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
747 	struct drm_v3d_submit_cl *args = data;
748 	struct v3d_submit_ext se = {0};
749 	struct v3d_bin_job *bin = NULL;
750 	struct v3d_render_job *render = NULL;
751 	struct v3d_job *clean_job = NULL;
752 	struct v3d_job *last_job;
753 	struct ww_acquire_ctx acquire_ctx;
754 	int ret = 0;
755 
756 	trace_v3d_submit_cl_ioctl(&v3d->drm, args->rcl_start, args->rcl_end);
757 
758 	if (args->pad)
759 		return -EINVAL;
760 
761 	if (args->flags &&
762 	    args->flags & ~(DRM_V3D_SUBMIT_CL_FLUSH_CACHE |
763 			    DRM_V3D_SUBMIT_EXTENSION)) {
764 		DRM_INFO("invalid flags: %d\n", args->flags);
765 		return -EINVAL;
766 	}
767 
768 	if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
769 		ret = v3d_get_extensions(file_priv, args->extensions, &se, NULL);
770 		if (ret) {
771 			DRM_DEBUG("Failed to get extensions.\n");
772 			return ret;
773 		}
774 	}
775 
776 	ret = v3d_job_allocate((void *)&render, sizeof(*render));
777 	if (ret)
778 		return ret;
779 
780 	ret = v3d_job_init(v3d, file_priv, &render->base,
781 			   v3d_render_job_free, args->in_sync_rcl, &se, V3D_RENDER);
782 	if (ret)
783 		goto fail;
784 
785 	render->start = args->rcl_start;
786 	render->end = args->rcl_end;
787 	INIT_LIST_HEAD(&render->unref_list);
788 
789 	if (args->bcl_start != args->bcl_end) {
790 		ret = v3d_job_allocate((void *)&bin, sizeof(*bin));
791 		if (ret)
792 			goto fail;
793 
794 		ret = v3d_job_init(v3d, file_priv, &bin->base,
795 				   v3d_job_free, args->in_sync_bcl, &se, V3D_BIN);
796 		if (ret)
797 			goto fail;
798 
799 		bin->start = args->bcl_start;
800 		bin->end = args->bcl_end;
801 		bin->qma = args->qma;
802 		bin->qms = args->qms;
803 		bin->qts = args->qts;
804 		bin->render = render;
805 	}
806 
807 	if (args->flags & DRM_V3D_SUBMIT_CL_FLUSH_CACHE) {
808 		ret = v3d_job_allocate((void *)&clean_job, sizeof(*clean_job));
809 		if (ret)
810 			goto fail;
811 
812 		ret = v3d_job_init(v3d, file_priv, clean_job,
813 				   v3d_job_free, 0, NULL, V3D_CACHE_CLEAN);
814 		if (ret)
815 			goto fail;
816 
817 		last_job = clean_job;
818 	} else {
819 		last_job = &render->base;
820 	}
821 
822 	ret = v3d_lookup_bos(dev, file_priv, last_job,
823 			     args->bo_handles, args->bo_handle_count);
824 	if (ret)
825 		goto fail;
826 
827 	ret = v3d_lock_bo_reservations(last_job, &acquire_ctx);
828 	if (ret)
829 		goto fail;
830 
831 	if (args->perfmon_id) {
832 		render->base.perfmon = v3d_perfmon_find(v3d_priv,
833 							args->perfmon_id);
834 
835 		if (!render->base.perfmon) {
836 			ret = -ENOENT;
837 			goto fail_perfmon;
838 		}
839 	}
840 
841 	mutex_lock(&v3d->sched_lock);
842 	if (bin) {
843 		bin->base.perfmon = render->base.perfmon;
844 		v3d_perfmon_get(bin->base.perfmon);
845 		v3d_push_job(&bin->base);
846 
847 		ret = drm_sched_job_add_dependency(&render->base.base,
848 						   dma_fence_get(bin->base.done_fence));
849 		if (ret)
850 			goto fail_unreserve;
851 	}
852 
853 	v3d_push_job(&render->base);
854 
855 	if (clean_job) {
856 		struct dma_fence *render_fence =
857 			dma_fence_get(render->base.done_fence);
858 		ret = drm_sched_job_add_dependency(&clean_job->base,
859 						   render_fence);
860 		if (ret)
861 			goto fail_unreserve;
862 		clean_job->perfmon = render->base.perfmon;
863 		v3d_perfmon_get(clean_job->perfmon);
864 		v3d_push_job(clean_job);
865 	}
866 
867 	mutex_unlock(&v3d->sched_lock);
868 
869 	v3d_attach_fences_and_unlock_reservation(file_priv,
870 						 last_job,
871 						 &acquire_ctx,
872 						 args->out_sync,
873 						 &se,
874 						 last_job->done_fence);
875 
876 	v3d_job_put(&bin->base);
877 	v3d_job_put(&render->base);
878 	v3d_job_put(clean_job);
879 
880 	return 0;
881 
882 fail_unreserve:
883 	mutex_unlock(&v3d->sched_lock);
884 fail_perfmon:
885 	drm_gem_unlock_reservations(last_job->bo,
886 				    last_job->bo_count, &acquire_ctx);
887 fail:
888 	v3d_job_cleanup((void *)bin);
889 	v3d_job_cleanup((void *)render);
890 	v3d_job_cleanup(clean_job);
891 	v3d_put_multisync_post_deps(&se);
892 
893 	return ret;
894 }
895 
896 /**
897  * v3d_submit_tfu_ioctl() - Submits a TFU (texture formatting) job to the V3D.
898  * @dev: DRM device
899  * @data: ioctl argument
900  * @file_priv: DRM file for this fd
901  *
902  * Userspace provides the register setup for the TFU, which we don't
903  * need to validate since the TFU is behind the MMU.
904  */
905 int
906 v3d_submit_tfu_ioctl(struct drm_device *dev, void *data,
907 		     struct drm_file *file_priv)
908 {
909 	struct v3d_dev *v3d = to_v3d_dev(dev);
910 	struct drm_v3d_submit_tfu *args = data;
911 	struct v3d_submit_ext se = {0};
912 	struct v3d_tfu_job *job = NULL;
913 	struct ww_acquire_ctx acquire_ctx;
914 	int ret = 0;
915 
916 	trace_v3d_submit_tfu_ioctl(&v3d->drm, args->iia);
917 
918 	if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) {
919 		DRM_DEBUG("invalid flags: %d\n", args->flags);
920 		return -EINVAL;
921 	}
922 
923 	if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
924 		ret = v3d_get_extensions(file_priv, args->extensions, &se, NULL);
925 		if (ret) {
926 			DRM_DEBUG("Failed to get extensions.\n");
927 			return ret;
928 		}
929 	}
930 
931 	ret = v3d_job_allocate((void *)&job, sizeof(*job));
932 	if (ret)
933 		return ret;
934 
935 	ret = v3d_job_init(v3d, file_priv, &job->base,
936 			   v3d_job_free, args->in_sync, &se, V3D_TFU);
937 	if (ret)
938 		goto fail;
939 
940 	job->base.bo = kcalloc(ARRAY_SIZE(args->bo_handles),
941 			       sizeof(*job->base.bo), GFP_KERNEL);
942 	if (!job->base.bo) {
943 		ret = -ENOMEM;
944 		goto fail;
945 	}
946 
947 	job->args = *args;
948 
949 	for (job->base.bo_count = 0;
950 	     job->base.bo_count < ARRAY_SIZE(args->bo_handles);
951 	     job->base.bo_count++) {
952 		struct drm_gem_object *bo;
953 
954 		if (!args->bo_handles[job->base.bo_count])
955 			break;
956 
957 		bo = drm_gem_object_lookup(file_priv, args->bo_handles[job->base.bo_count]);
958 		if (!bo) {
959 			DRM_DEBUG("Failed to look up GEM BO %d: %d\n",
960 				  job->base.bo_count,
961 				  args->bo_handles[job->base.bo_count]);
962 			ret = -ENOENT;
963 			goto fail;
964 		}
965 		job->base.bo[job->base.bo_count] = bo;
966 	}
967 
968 	ret = v3d_lock_bo_reservations(&job->base, &acquire_ctx);
969 	if (ret)
970 		goto fail;
971 
972 	mutex_lock(&v3d->sched_lock);
973 	v3d_push_job(&job->base);
974 	mutex_unlock(&v3d->sched_lock);
975 
976 	v3d_attach_fences_and_unlock_reservation(file_priv,
977 						 &job->base, &acquire_ctx,
978 						 args->out_sync,
979 						 &se,
980 						 job->base.done_fence);
981 
982 	v3d_job_put(&job->base);
983 
984 	return 0;
985 
986 fail:
987 	v3d_job_cleanup((void *)job);
988 	v3d_put_multisync_post_deps(&se);
989 
990 	return ret;
991 }
992 
993 /**
994  * v3d_submit_csd_ioctl() - Submits a CSD (compute shader) job to the V3D.
995  * @dev: DRM device
996  * @data: ioctl argument
997  * @file_priv: DRM file for this fd
998  *
999  * Userspace provides the register setup for the CSD, which we don't
1000  * need to validate since the CSD is behind the MMU.
1001  */
1002 int
1003 v3d_submit_csd_ioctl(struct drm_device *dev, void *data,
1004 		     struct drm_file *file_priv)
1005 {
1006 	struct v3d_dev *v3d = to_v3d_dev(dev);
1007 	struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
1008 	struct drm_v3d_submit_csd *args = data;
1009 	struct v3d_submit_ext se = {0};
1010 	struct v3d_csd_job *job = NULL;
1011 	struct v3d_job *clean_job = NULL;
1012 	struct ww_acquire_ctx acquire_ctx;
1013 	int ret;
1014 
1015 	trace_v3d_submit_csd_ioctl(&v3d->drm, args->cfg[5], args->cfg[6]);
1016 
1017 	if (args->pad)
1018 		return -EINVAL;
1019 
1020 	if (!v3d_has_csd(v3d)) {
1021 		DRM_DEBUG("Attempting CSD submit on non-CSD hardware\n");
1022 		return -EINVAL;
1023 	}
1024 
1025 	if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) {
1026 		DRM_INFO("invalid flags: %d\n", args->flags);
1027 		return -EINVAL;
1028 	}
1029 
1030 	if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
1031 		ret = v3d_get_extensions(file_priv, args->extensions, &se, NULL);
1032 		if (ret) {
1033 			DRM_DEBUG("Failed to get extensions.\n");
1034 			return ret;
1035 		}
1036 	}
1037 
1038 	ret = v3d_setup_csd_jobs_and_bos(file_priv, v3d, args,
1039 					 &job, &clean_job, &se,
1040 					 &acquire_ctx);
1041 	if (ret)
1042 		goto fail;
1043 
1044 	if (args->perfmon_id) {
1045 		job->base.perfmon = v3d_perfmon_find(v3d_priv,
1046 						     args->perfmon_id);
1047 		if (!job->base.perfmon) {
1048 			ret = -ENOENT;
1049 			goto fail_perfmon;
1050 		}
1051 	}
1052 
1053 	mutex_lock(&v3d->sched_lock);
1054 	v3d_push_job(&job->base);
1055 
1056 	ret = drm_sched_job_add_dependency(&clean_job->base,
1057 					   dma_fence_get(job->base.done_fence));
1058 	if (ret)
1059 		goto fail_unreserve;
1060 
1061 	v3d_push_job(clean_job);
1062 	mutex_unlock(&v3d->sched_lock);
1063 
1064 	v3d_attach_fences_and_unlock_reservation(file_priv,
1065 						 clean_job,
1066 						 &acquire_ctx,
1067 						 args->out_sync,
1068 						 &se,
1069 						 clean_job->done_fence);
1070 
1071 	v3d_job_put(&job->base);
1072 	v3d_job_put(clean_job);
1073 
1074 	return 0;
1075 
1076 fail_unreserve:
1077 	mutex_unlock(&v3d->sched_lock);
1078 fail_perfmon:
1079 	drm_gem_unlock_reservations(clean_job->bo, clean_job->bo_count,
1080 				    &acquire_ctx);
1081 fail:
1082 	v3d_job_cleanup((void *)job);
1083 	v3d_job_cleanup(clean_job);
1084 	v3d_put_multisync_post_deps(&se);
1085 
1086 	return ret;
1087 }
1088 
1089 static const unsigned int cpu_job_bo_handle_count[] = {
1090 	[V3D_CPU_JOB_TYPE_INDIRECT_CSD] = 1,
1091 	[V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY] = 1,
1092 	[V3D_CPU_JOB_TYPE_RESET_TIMESTAMP_QUERY] = 1,
1093 	[V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY] = 2,
1094 	[V3D_CPU_JOB_TYPE_RESET_PERFORMANCE_QUERY] = 0,
1095 };
1096 
1097 /**
1098  * v3d_submit_cpu_ioctl() - Submits a CPU job to the V3D.
1099  * @dev: DRM device
1100  * @data: ioctl argument
1101  * @file_priv: DRM file for this fd
1102  *
1103  * Userspace specifies the CPU job type and data required to perform its
1104  * operations through the drm_v3d_extension struct.
1105  */
1106 int
1107 v3d_submit_cpu_ioctl(struct drm_device *dev, void *data,
1108 		     struct drm_file *file_priv)
1109 {
1110 	struct v3d_dev *v3d = to_v3d_dev(dev);
1111 	struct drm_v3d_submit_cpu *args = data;
1112 	struct v3d_submit_ext se = {0};
1113 	struct v3d_submit_ext *out_se = NULL;
1114 	struct v3d_cpu_job *cpu_job = NULL;
1115 	struct v3d_csd_job *csd_job = NULL;
1116 	struct v3d_job *clean_job = NULL;
1117 	struct ww_acquire_ctx acquire_ctx;
1118 	int ret;
1119 
1120 	if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) {
1121 		DRM_INFO("Invalid flags: %d\n", args->flags);
1122 		return -EINVAL;
1123 	}
1124 
1125 	ret = v3d_job_allocate((void *)&cpu_job, sizeof(*cpu_job));
1126 	if (ret)
1127 		return ret;
1128 
1129 	if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
1130 		ret = v3d_get_extensions(file_priv, args->extensions, &se, cpu_job);
1131 		if (ret) {
1132 			DRM_DEBUG("Failed to get extensions.\n");
1133 			goto fail;
1134 		}
1135 	}
1136 
1137 	/* Every CPU job must have a CPU job user extension */
1138 	if (!cpu_job->job_type) {
1139 		DRM_DEBUG("CPU job must have a CPU job user extension.\n");
1140 		goto fail;
1141 	}
1142 
1143 	if (args->bo_handle_count != cpu_job_bo_handle_count[cpu_job->job_type]) {
1144 		DRM_DEBUG("This CPU job was not submitted with the proper number of BOs.\n");
1145 		goto fail;
1146 	}
1147 
1148 	trace_v3d_submit_cpu_ioctl(&v3d->drm, cpu_job->job_type);
1149 
1150 	ret = v3d_job_init(v3d, file_priv, &cpu_job->base,
1151 			   v3d_job_free, 0, &se, V3D_CPU);
1152 	if (ret)
1153 		goto fail;
1154 
1155 	clean_job = cpu_job->indirect_csd.clean_job;
1156 	csd_job = cpu_job->indirect_csd.job;
1157 
1158 	if (args->bo_handle_count) {
1159 		ret = v3d_lookup_bos(dev, file_priv, &cpu_job->base,
1160 				     args->bo_handles, args->bo_handle_count);
1161 		if (ret)
1162 			goto fail;
1163 
1164 		ret = v3d_lock_bo_reservations(&cpu_job->base, &acquire_ctx);
1165 		if (ret)
1166 			goto fail;
1167 	}
1168 
1169 	mutex_lock(&v3d->sched_lock);
1170 	v3d_push_job(&cpu_job->base);
1171 
1172 	switch (cpu_job->job_type) {
1173 	case V3D_CPU_JOB_TYPE_INDIRECT_CSD:
1174 		ret = drm_sched_job_add_dependency(&csd_job->base.base,
1175 						   dma_fence_get(cpu_job->base.done_fence));
1176 		if (ret)
1177 			goto fail_unreserve;
1178 
1179 		v3d_push_job(&csd_job->base);
1180 
1181 		ret = drm_sched_job_add_dependency(&clean_job->base,
1182 						   dma_fence_get(csd_job->base.done_fence));
1183 		if (ret)
1184 			goto fail_unreserve;
1185 
1186 		v3d_push_job(clean_job);
1187 
1188 		break;
1189 	default:
1190 		break;
1191 	}
1192 	mutex_unlock(&v3d->sched_lock);
1193 
1194 	out_se = (cpu_job->job_type == V3D_CPU_JOB_TYPE_INDIRECT_CSD) ? NULL : &se;
1195 
1196 	v3d_attach_fences_and_unlock_reservation(file_priv,
1197 						 &cpu_job->base,
1198 						 &acquire_ctx, 0,
1199 						 out_se, cpu_job->base.done_fence);
1200 
1201 	switch (cpu_job->job_type) {
1202 	case V3D_CPU_JOB_TYPE_INDIRECT_CSD:
1203 		v3d_attach_fences_and_unlock_reservation(file_priv,
1204 							 clean_job,
1205 							 &cpu_job->indirect_csd.acquire_ctx,
1206 							 0, &se, clean_job->done_fence);
1207 		break;
1208 	default:
1209 		break;
1210 	}
1211 
1212 	v3d_job_put(&cpu_job->base);
1213 	v3d_job_put(&csd_job->base);
1214 	v3d_job_put(clean_job);
1215 
1216 	return 0;
1217 
1218 fail_unreserve:
1219 	mutex_unlock(&v3d->sched_lock);
1220 
1221 	drm_gem_unlock_reservations(cpu_job->base.bo, cpu_job->base.bo_count,
1222 				    &acquire_ctx);
1223 
1224 	drm_gem_unlock_reservations(clean_job->bo, clean_job->bo_count,
1225 				    &cpu_job->indirect_csd.acquire_ctx);
1226 
1227 fail:
1228 	v3d_job_cleanup((void *)cpu_job);
1229 	v3d_job_cleanup((void *)csd_job);
1230 	v3d_job_cleanup(clean_job);
1231 	v3d_put_multisync_post_deps(&se);
1232 	kvfree(cpu_job->timestamp_query.queries);
1233 	kvfree(cpu_job->performance_query.queries);
1234 
1235 	return ret;
1236 }
1237