1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2014-2018 Broadcom
4 * Copyright (C) 2023 Raspberry Pi
7 #include <drm/drm_syncobj.h>
11 #include "v3d_trace.h"
13 /* Takes the reservation lock on all the BOs being referenced, so that
14 * at queue submit time we can update the reservations.
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.
21 v3d_lock_bo_reservations(struct v3d_job *job,
22 struct ww_acquire_ctx *acquire_ctx)
26 ret = drm_gem_lock_reservations(job->bo, job->bo_count, acquire_ctx);
30 for (i = 0; i < job->bo_count; i++) {
31 ret = dma_resv_reserve_fences(job->bo[i]->resv, 1);
35 ret = drm_sched_job_add_implicit_dependencies(&job->base,
44 drm_gem_unlock_reservations(job->bo, job->bo_count, acquire_ctx);
49 * v3d_lookup_bos() - Sets up job->bo[] with the GEM objects
50 * referenced by the job.
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
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.
61 * Note that this function doesn't need to unreference the BOs on
62 * failure, because that will happen at v3d_exec_cleanup() time.
65 v3d_lookup_bos(struct drm_device *dev,
66 struct drm_file *file_priv,
71 job->bo_count = bo_count;
74 /* See comment on bo_index for why we have to check
77 DRM_DEBUG("Rendering requires BOs\n");
81 return drm_gem_objects_lookup(file_priv,
82 (void __user *)(uintptr_t)bo_handles,
83 job->bo_count, &job->bo);
87 v3d_job_free(struct kref *ref)
89 struct v3d_job *job = container_of(ref, struct v3d_job, refcount);
93 for (i = 0; i < job->bo_count; i++)
94 drm_gem_object_put(job->bo[i]);
98 dma_fence_put(job->irq_fence);
99 dma_fence_put(job->done_fence);
102 v3d_perfmon_put(job->perfmon);
108 v3d_render_job_free(struct kref *ref)
110 struct v3d_render_job *job = container_of(ref, struct v3d_render_job,
112 struct v3d_bo *bo, *save;
114 list_for_each_entry_safe(bo, save, &job->unref_list, unref_head) {
115 drm_gem_object_put(&bo->base.base);
121 void v3d_job_cleanup(struct v3d_job *job)
126 drm_sched_job_cleanup(&job->base);
130 void v3d_job_put(struct v3d_job *job)
135 kref_put(&job->refcount, job->free);
139 v3d_job_allocate(void **container, size_t size)
141 *container = kcalloc(1, size, GFP_KERNEL);
143 DRM_ERROR("Cannot allocate memory for V3D job.\n");
151 v3d_job_deallocate(void **container)
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)
162 struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
163 bool has_multisync = se && (se->flags & DRM_V3D_EXT_ID_MULTI_SYNC);
168 job->file = file_priv;
170 ret = drm_sched_job_init(&job->base, &v3d_priv->sched_entity[queue],
176 if (se->in_sync_count && se->wait_stage == queue) {
177 struct drm_v3d_sem __user *handle = u64_to_user_ptr(se->in_syncs);
179 for (i = 0; i < se->in_sync_count; i++) {
180 struct drm_v3d_sem in;
182 if (copy_from_user(&in, handle++, sizeof(in))) {
184 DRM_DEBUG("Failed to copy wait dep handle.\n");
187 ret = drm_sched_job_add_syncobj_dependency(&job->base, file_priv, in.handle, 0);
189 // TODO: Investigate why this was filtered out for the IOCTL.
190 if (ret && ret != -ENOENT)
195 ret = drm_sched_job_add_syncobj_dependency(&job->base, file_priv, in_sync, 0);
197 // TODO: Investigate why this was filtered out for the IOCTL.
198 if (ret && ret != -ENOENT)
202 kref_init(&job->refcount);
207 drm_sched_job_cleanup(&job->base);
212 v3d_push_job(struct v3d_job *job)
214 drm_sched_job_arm(&job->base);
216 job->done_fence = dma_fence_get(&job->base.s_fence->finished);
218 /* put by scheduler job completion */
219 kref_get(&job->refcount);
221 drm_sched_entity_push_job(&job->base);
225 v3d_attach_fences_and_unlock_reservation(struct drm_file *file_priv,
227 struct ww_acquire_ctx *acquire_ctx,
229 struct v3d_submit_ext *se,
230 struct dma_fence *done_fence)
232 struct drm_syncobj *sync_out;
233 bool has_multisync = se && (se->flags & DRM_V3D_EXT_ID_MULTI_SYNC);
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);
242 drm_gem_unlock_reservations(job->bo, job->bo_count, acquire_ctx);
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);
249 drm_syncobj_replace_fence(sync_out, done_fence);
250 drm_syncobj_put(sync_out);
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,
260 drm_syncobj_put(se->out_syncs[i].syncobj);
262 kvfree(se->out_syncs);
267 v3d_setup_csd_jobs_and_bos(struct drm_file *file_priv,
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)
277 ret = v3d_job_allocate((void *)job, sizeof(**job));
281 ret = v3d_job_init(v3d, file_priv, &(*job)->base,
282 v3d_job_free, args->in_sync, se, V3D_CSD);
284 v3d_job_deallocate((void *)job);
288 ret = v3d_job_allocate((void *)clean_job, sizeof(**clean_job));
292 ret = v3d_job_init(v3d, file_priv, *clean_job,
293 v3d_job_free, 0, NULL, V3D_CACHE_CLEAN);
295 v3d_job_deallocate((void *)clean_job);
299 (*job)->args = *args;
301 ret = v3d_lookup_bos(&v3d->drm, file_priv, *clean_job,
302 args->bo_handles, args->bo_handle_count);
306 return v3d_lock_bo_reservations(*clean_job, acquire_ctx);
310 v3d_put_multisync_post_deps(struct v3d_submit_ext *se)
314 if (!(se && se->out_sync_count))
317 for (i = 0; i < se->out_sync_count; i++)
318 drm_syncobj_put(se->out_syncs[i].syncobj);
319 kvfree(se->out_syncs);
323 v3d_get_multisync_post_deps(struct drm_file *file_priv,
324 struct v3d_submit_ext *se,
325 u32 count, u64 handles)
327 struct drm_v3d_sem __user *post_deps;
333 se->out_syncs = (struct v3d_submit_outsync *)
334 kvmalloc_array(count,
335 sizeof(struct v3d_submit_outsync),
340 post_deps = u64_to_user_ptr(handles);
342 for (i = 0; i < count; i++) {
343 struct drm_v3d_sem out;
345 if (copy_from_user(&out, post_deps++, sizeof(out))) {
347 DRM_DEBUG("Failed to copy post dep handles\n");
351 se->out_syncs[i].syncobj = drm_syncobj_find(file_priv,
353 if (!se->out_syncs[i].syncobj) {
358 se->out_sync_count = count;
363 for (i--; i >= 0; i--)
364 drm_syncobj_put(se->out_syncs[i].syncobj);
365 kvfree(se->out_syncs);
370 /* Get data for multiple binary semaphores synchronization. Parse syncobj
371 * to be signaled when job completes (out_sync).
374 v3d_get_multisync_submit_deps(struct drm_file *file_priv,
375 struct drm_v3d_extension __user *ext,
376 struct v3d_submit_ext *se)
378 struct drm_v3d_multi_sync multisync;
381 if (se->in_sync_count || se->out_sync_count) {
382 DRM_DEBUG("Two multisync extensions were added to the same job.");
386 if (copy_from_user(&multisync, ext, sizeof(multisync)))
392 ret = v3d_get_multisync_post_deps(file_priv, se, multisync.out_sync_count,
393 multisync.out_syncs);
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;
405 /* Get data for the indirect CSD job submission. */
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)
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;
417 DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
422 DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
426 if (copy_from_user(&indirect_csd, ext, sizeof(indirect_csd)))
429 if (!v3d_has_csd(v3d)) {
430 DRM_DEBUG("Attempting CSD submit on non-CSD hardware.\n");
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));
440 info->indirect = drm_gem_object_lookup(file_priv, indirect_csd.indirect);
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);
447 /* Get data for the query timestamp job submission. */
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)
453 u32 __user *offsets, *syncs;
454 struct drm_v3d_timestamp_query timestamp;
455 struct v3d_timestamp_query_info *query_info = &job->timestamp_query;
460 DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
465 DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
469 if (copy_from_user(×tamp, ext, sizeof(timestamp)))
475 job->job_type = V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY;
477 query_info->queries = kvmalloc_array(timestamp.count,
478 sizeof(struct v3d_timestamp_query),
480 if (!query_info->queries)
483 offsets = u64_to_user_ptr(timestamp.offsets);
484 syncs = u64_to_user_ptr(timestamp.syncs);
486 for (i = 0; i < timestamp.count; i++) {
489 if (get_user(offset, offsets++)) {
494 query_info->queries[i].offset = offset;
496 if (get_user(sync, syncs++)) {
501 query_info->queries[i].syncobj = drm_syncobj_find(file_priv,
503 if (!query_info->queries[i].syncobj) {
508 query_info->count = timestamp.count;
513 v3d_timestamp_query_info_free(&job->timestamp_query, i);
518 v3d_get_cpu_reset_timestamp_params(struct drm_file *file_priv,
519 struct drm_v3d_extension __user *ext,
520 struct v3d_cpu_job *job)
523 struct drm_v3d_reset_timestamp_query reset;
524 struct v3d_timestamp_query_info *query_info = &job->timestamp_query;
529 DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
534 DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
538 if (copy_from_user(&reset, ext, sizeof(reset)))
541 job->job_type = V3D_CPU_JOB_TYPE_RESET_TIMESTAMP_QUERY;
543 query_info->queries = kvmalloc_array(reset.count,
544 sizeof(struct v3d_timestamp_query),
546 if (!query_info->queries)
549 syncs = u64_to_user_ptr(reset.syncs);
551 for (i = 0; i < reset.count; i++) {
554 query_info->queries[i].offset = reset.offset + 8 * i;
556 if (get_user(sync, syncs++)) {
561 query_info->queries[i].syncobj = drm_syncobj_find(file_priv,
563 if (!query_info->queries[i].syncobj) {
568 query_info->count = reset.count;
573 v3d_timestamp_query_info_free(&job->timestamp_query, i);
577 /* Get data for the copy timestamp query results job submission. */
579 v3d_get_cpu_copy_query_results_params(struct drm_file *file_priv,
580 struct drm_v3d_extension __user *ext,
581 struct v3d_cpu_job *job)
583 u32 __user *offsets, *syncs;
584 struct drm_v3d_copy_timestamp_query copy;
585 struct v3d_timestamp_query_info *query_info = &job->timestamp_query;
590 DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
595 DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
599 if (copy_from_user(©, ext, sizeof(copy)))
605 job->job_type = V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY;
607 query_info->queries = kvmalloc_array(copy.count,
608 sizeof(struct v3d_timestamp_query),
610 if (!query_info->queries)
613 offsets = u64_to_user_ptr(copy.offsets);
614 syncs = u64_to_user_ptr(copy.syncs);
616 for (i = 0; i < copy.count; i++) {
619 if (get_user(offset, offsets++)) {
624 query_info->queries[i].offset = offset;
626 if (get_user(sync, syncs++)) {
631 query_info->queries[i].syncobj = drm_syncobj_find(file_priv,
633 if (!query_info->queries[i].syncobj) {
638 query_info->count = copy.count;
640 job->copy.do_64bit = copy.do_64bit;
641 job->copy.do_partial = copy.do_partial;
642 job->copy.availability_bit = copy.availability_bit;
643 job->copy.offset = copy.offset;
644 job->copy.stride = copy.stride;
649 v3d_timestamp_query_info_free(&job->timestamp_query, i);
654 v3d_copy_query_info(struct v3d_performance_query_info *query_info,
656 unsigned int nperfmons,
658 u64 __user *kperfmon_ids,
659 struct drm_file *file_priv)
664 for (i = 0; i < count; i++) {
665 struct v3d_performance_query *query = &query_info->queries[i];
666 u32 __user *ids_pointer;
670 if (get_user(sync, syncs++)) {
675 if (get_user(ids, kperfmon_ids++)) {
680 query->kperfmon_ids =
681 kvmalloc_array(nperfmons,
682 sizeof(struct v3d_performance_query *),
684 if (!query->kperfmon_ids) {
689 ids_pointer = u64_to_user_ptr(ids);
691 for (j = 0; j < nperfmons; j++) {
692 if (get_user(id, ids_pointer++)) {
693 kvfree(query->kperfmon_ids);
698 query->kperfmon_ids[j] = id;
701 query->syncobj = drm_syncobj_find(file_priv, sync);
702 if (!query->syncobj) {
703 kvfree(query->kperfmon_ids);
712 v3d_performance_query_info_free(query_info, i);
717 v3d_get_cpu_reset_performance_params(struct drm_file *file_priv,
718 struct drm_v3d_extension __user *ext,
719 struct v3d_cpu_job *job)
721 struct v3d_performance_query_info *query_info = &job->performance_query;
722 struct drm_v3d_reset_performance_query reset;
726 DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
731 DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
735 if (copy_from_user(&reset, ext, sizeof(reset)))
738 job->job_type = V3D_CPU_JOB_TYPE_RESET_PERFORMANCE_QUERY;
740 query_info->queries =
741 kvmalloc_array(reset.count,
742 sizeof(struct v3d_performance_query),
744 if (!query_info->queries)
747 err = v3d_copy_query_info(query_info,
750 u64_to_user_ptr(reset.syncs),
751 u64_to_user_ptr(reset.kperfmon_ids),
756 query_info->count = reset.count;
757 query_info->nperfmons = reset.nperfmons;
763 v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv,
764 struct drm_v3d_extension __user *ext,
765 struct v3d_cpu_job *job)
767 struct v3d_performance_query_info *query_info = &job->performance_query;
768 struct drm_v3d_copy_performance_query copy;
772 DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
777 DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
781 if (copy_from_user(©, ext, sizeof(copy)))
787 job->job_type = V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY;
789 query_info->queries =
790 kvmalloc_array(copy.count,
791 sizeof(struct v3d_performance_query),
793 if (!query_info->queries)
796 err = v3d_copy_query_info(query_info,
799 u64_to_user_ptr(copy.syncs),
800 u64_to_user_ptr(copy.kperfmon_ids),
805 query_info->count = copy.count;
806 query_info->nperfmons = copy.nperfmons;
807 query_info->ncounters = copy.ncounters;
809 job->copy.do_64bit = copy.do_64bit;
810 job->copy.do_partial = copy.do_partial;
811 job->copy.availability_bit = copy.availability_bit;
812 job->copy.offset = copy.offset;
813 job->copy.stride = copy.stride;
818 /* Whenever userspace sets ioctl extensions, v3d_get_extensions parses data
819 * according to the extension id (name).
822 v3d_get_extensions(struct drm_file *file_priv,
824 struct v3d_submit_ext *se,
825 struct v3d_cpu_job *job)
827 struct drm_v3d_extension __user *user_ext;
830 user_ext = u64_to_user_ptr(ext_handles);
832 struct drm_v3d_extension ext;
834 if (copy_from_user(&ext, user_ext, sizeof(ext))) {
835 DRM_DEBUG("Failed to copy submit extension\n");
840 case DRM_V3D_EXT_ID_MULTI_SYNC:
841 ret = v3d_get_multisync_submit_deps(file_priv, user_ext, se);
843 case DRM_V3D_EXT_ID_CPU_INDIRECT_CSD:
844 ret = v3d_get_cpu_indirect_csd_params(file_priv, user_ext, job);
846 case DRM_V3D_EXT_ID_CPU_TIMESTAMP_QUERY:
847 ret = v3d_get_cpu_timestamp_query_params(file_priv, user_ext, job);
849 case DRM_V3D_EXT_ID_CPU_RESET_TIMESTAMP_QUERY:
850 ret = v3d_get_cpu_reset_timestamp_params(file_priv, user_ext, job);
852 case DRM_V3D_EXT_ID_CPU_COPY_TIMESTAMP_QUERY:
853 ret = v3d_get_cpu_copy_query_results_params(file_priv, user_ext, job);
855 case DRM_V3D_EXT_ID_CPU_RESET_PERFORMANCE_QUERY:
856 ret = v3d_get_cpu_reset_performance_params(file_priv, user_ext, job);
858 case DRM_V3D_EXT_ID_CPU_COPY_PERFORMANCE_QUERY:
859 ret = v3d_get_cpu_copy_performance_query_params(file_priv, user_ext, job);
862 DRM_DEBUG_DRIVER("Unknown extension id: %d\n", ext.id);
869 user_ext = u64_to_user_ptr(ext.next);
876 * v3d_submit_cl_ioctl() - Submits a job (frame) to the V3D.
878 * @data: ioctl argument
879 * @file_priv: DRM file for this fd
881 * This is the main entrypoint for userspace to submit a 3D frame to
882 * the GPU. Userspace provides the binner command list (if
883 * applicable), and the kernel sets up the render command list to draw
884 * to the framebuffer described in the ioctl, using the command lists
885 * that the 3D engine's binner will produce.
888 v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
889 struct drm_file *file_priv)
891 struct v3d_dev *v3d = to_v3d_dev(dev);
892 struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
893 struct drm_v3d_submit_cl *args = data;
894 struct v3d_submit_ext se = {0};
895 struct v3d_bin_job *bin = NULL;
896 struct v3d_render_job *render = NULL;
897 struct v3d_job *clean_job = NULL;
898 struct v3d_job *last_job;
899 struct ww_acquire_ctx acquire_ctx;
902 trace_v3d_submit_cl_ioctl(&v3d->drm, args->rcl_start, args->rcl_end);
908 args->flags & ~(DRM_V3D_SUBMIT_CL_FLUSH_CACHE |
909 DRM_V3D_SUBMIT_EXTENSION)) {
910 DRM_INFO("invalid flags: %d\n", args->flags);
914 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
915 ret = v3d_get_extensions(file_priv, args->extensions, &se, NULL);
917 DRM_DEBUG("Failed to get extensions.\n");
922 ret = v3d_job_allocate((void *)&render, sizeof(*render));
926 ret = v3d_job_init(v3d, file_priv, &render->base,
927 v3d_render_job_free, args->in_sync_rcl, &se, V3D_RENDER);
929 v3d_job_deallocate((void *)&render);
933 render->start = args->rcl_start;
934 render->end = args->rcl_end;
935 INIT_LIST_HEAD(&render->unref_list);
937 if (args->bcl_start != args->bcl_end) {
938 ret = v3d_job_allocate((void *)&bin, sizeof(*bin));
942 ret = v3d_job_init(v3d, file_priv, &bin->base,
943 v3d_job_free, args->in_sync_bcl, &se, V3D_BIN);
945 v3d_job_deallocate((void *)&bin);
949 bin->start = args->bcl_start;
950 bin->end = args->bcl_end;
951 bin->qma = args->qma;
952 bin->qms = args->qms;
953 bin->qts = args->qts;
954 bin->render = render;
957 if (args->flags & DRM_V3D_SUBMIT_CL_FLUSH_CACHE) {
958 ret = v3d_job_allocate((void *)&clean_job, sizeof(*clean_job));
962 ret = v3d_job_init(v3d, file_priv, clean_job,
963 v3d_job_free, 0, NULL, V3D_CACHE_CLEAN);
965 v3d_job_deallocate((void *)&clean_job);
969 last_job = clean_job;
971 last_job = &render->base;
974 ret = v3d_lookup_bos(dev, file_priv, last_job,
975 args->bo_handles, args->bo_handle_count);
979 ret = v3d_lock_bo_reservations(last_job, &acquire_ctx);
983 if (args->perfmon_id) {
984 render->base.perfmon = v3d_perfmon_find(v3d_priv,
987 if (!render->base.perfmon) {
993 mutex_lock(&v3d->sched_lock);
995 bin->base.perfmon = render->base.perfmon;
996 v3d_perfmon_get(bin->base.perfmon);
997 v3d_push_job(&bin->base);
999 ret = drm_sched_job_add_dependency(&render->base.base,
1000 dma_fence_get(bin->base.done_fence));
1002 goto fail_unreserve;
1005 v3d_push_job(&render->base);
1008 struct dma_fence *render_fence =
1009 dma_fence_get(render->base.done_fence);
1010 ret = drm_sched_job_add_dependency(&clean_job->base,
1013 goto fail_unreserve;
1014 clean_job->perfmon = render->base.perfmon;
1015 v3d_perfmon_get(clean_job->perfmon);
1016 v3d_push_job(clean_job);
1019 mutex_unlock(&v3d->sched_lock);
1021 v3d_attach_fences_and_unlock_reservation(file_priv,
1026 last_job->done_fence);
1028 v3d_job_put(&bin->base);
1029 v3d_job_put(&render->base);
1030 v3d_job_put(clean_job);
1035 mutex_unlock(&v3d->sched_lock);
1037 drm_gem_unlock_reservations(last_job->bo,
1038 last_job->bo_count, &acquire_ctx);
1040 v3d_job_cleanup((void *)bin);
1041 v3d_job_cleanup((void *)render);
1042 v3d_job_cleanup(clean_job);
1043 v3d_put_multisync_post_deps(&se);
1049 * v3d_submit_tfu_ioctl() - Submits a TFU (texture formatting) job to the V3D.
1051 * @data: ioctl argument
1052 * @file_priv: DRM file for this fd
1054 * Userspace provides the register setup for the TFU, which we don't
1055 * need to validate since the TFU is behind the MMU.
1058 v3d_submit_tfu_ioctl(struct drm_device *dev, void *data,
1059 struct drm_file *file_priv)
1061 struct v3d_dev *v3d = to_v3d_dev(dev);
1062 struct drm_v3d_submit_tfu *args = data;
1063 struct v3d_submit_ext se = {0};
1064 struct v3d_tfu_job *job = NULL;
1065 struct ww_acquire_ctx acquire_ctx;
1068 trace_v3d_submit_tfu_ioctl(&v3d->drm, args->iia);
1070 if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) {
1071 DRM_DEBUG("invalid flags: %d\n", args->flags);
1075 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
1076 ret = v3d_get_extensions(file_priv, args->extensions, &se, NULL);
1078 DRM_DEBUG("Failed to get extensions.\n");
1083 ret = v3d_job_allocate((void *)&job, sizeof(*job));
1087 ret = v3d_job_init(v3d, file_priv, &job->base,
1088 v3d_job_free, args->in_sync, &se, V3D_TFU);
1090 v3d_job_deallocate((void *)&job);
1094 job->base.bo = kcalloc(ARRAY_SIZE(args->bo_handles),
1095 sizeof(*job->base.bo), GFP_KERNEL);
1096 if (!job->base.bo) {
1103 for (job->base.bo_count = 0;
1104 job->base.bo_count < ARRAY_SIZE(args->bo_handles);
1105 job->base.bo_count++) {
1106 struct drm_gem_object *bo;
1108 if (!args->bo_handles[job->base.bo_count])
1111 bo = drm_gem_object_lookup(file_priv, args->bo_handles[job->base.bo_count]);
1113 DRM_DEBUG("Failed to look up GEM BO %d: %d\n",
1115 args->bo_handles[job->base.bo_count]);
1119 job->base.bo[job->base.bo_count] = bo;
1122 ret = v3d_lock_bo_reservations(&job->base, &acquire_ctx);
1126 mutex_lock(&v3d->sched_lock);
1127 v3d_push_job(&job->base);
1128 mutex_unlock(&v3d->sched_lock);
1130 v3d_attach_fences_and_unlock_reservation(file_priv,
1131 &job->base, &acquire_ctx,
1134 job->base.done_fence);
1136 v3d_job_put(&job->base);
1141 v3d_job_cleanup((void *)job);
1142 v3d_put_multisync_post_deps(&se);
1148 * v3d_submit_csd_ioctl() - Submits a CSD (compute shader) job to the V3D.
1150 * @data: ioctl argument
1151 * @file_priv: DRM file for this fd
1153 * Userspace provides the register setup for the CSD, which we don't
1154 * need to validate since the CSD is behind the MMU.
1157 v3d_submit_csd_ioctl(struct drm_device *dev, void *data,
1158 struct drm_file *file_priv)
1160 struct v3d_dev *v3d = to_v3d_dev(dev);
1161 struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
1162 struct drm_v3d_submit_csd *args = data;
1163 struct v3d_submit_ext se = {0};
1164 struct v3d_csd_job *job = NULL;
1165 struct v3d_job *clean_job = NULL;
1166 struct ww_acquire_ctx acquire_ctx;
1169 trace_v3d_submit_csd_ioctl(&v3d->drm, args->cfg[5], args->cfg[6]);
1174 if (!v3d_has_csd(v3d)) {
1175 DRM_DEBUG("Attempting CSD submit on non-CSD hardware\n");
1179 if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) {
1180 DRM_INFO("invalid flags: %d\n", args->flags);
1184 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
1185 ret = v3d_get_extensions(file_priv, args->extensions, &se, NULL);
1187 DRM_DEBUG("Failed to get extensions.\n");
1192 ret = v3d_setup_csd_jobs_and_bos(file_priv, v3d, args,
1193 &job, &clean_job, &se,
1198 if (args->perfmon_id) {
1199 job->base.perfmon = v3d_perfmon_find(v3d_priv,
1201 if (!job->base.perfmon) {
1207 mutex_lock(&v3d->sched_lock);
1208 v3d_push_job(&job->base);
1210 ret = drm_sched_job_add_dependency(&clean_job->base,
1211 dma_fence_get(job->base.done_fence));
1213 goto fail_unreserve;
1215 v3d_push_job(clean_job);
1216 mutex_unlock(&v3d->sched_lock);
1218 v3d_attach_fences_and_unlock_reservation(file_priv,
1223 clean_job->done_fence);
1225 v3d_job_put(&job->base);
1226 v3d_job_put(clean_job);
1231 mutex_unlock(&v3d->sched_lock);
1233 drm_gem_unlock_reservations(clean_job->bo, clean_job->bo_count,
1236 v3d_job_cleanup((void *)job);
1237 v3d_job_cleanup(clean_job);
1238 v3d_put_multisync_post_deps(&se);
1243 static const unsigned int cpu_job_bo_handle_count[] = {
1244 [V3D_CPU_JOB_TYPE_INDIRECT_CSD] = 1,
1245 [V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY] = 1,
1246 [V3D_CPU_JOB_TYPE_RESET_TIMESTAMP_QUERY] = 1,
1247 [V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY] = 2,
1248 [V3D_CPU_JOB_TYPE_RESET_PERFORMANCE_QUERY] = 0,
1249 [V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY] = 1,
1253 * v3d_submit_cpu_ioctl() - Submits a CPU job to the V3D.
1255 * @data: ioctl argument
1256 * @file_priv: DRM file for this fd
1258 * Userspace specifies the CPU job type and data required to perform its
1259 * operations through the drm_v3d_extension struct.
1262 v3d_submit_cpu_ioctl(struct drm_device *dev, void *data,
1263 struct drm_file *file_priv)
1265 struct v3d_dev *v3d = to_v3d_dev(dev);
1266 struct drm_v3d_submit_cpu *args = data;
1267 struct v3d_submit_ext se = {0};
1268 struct v3d_submit_ext *out_se = NULL;
1269 struct v3d_cpu_job *cpu_job = NULL;
1270 struct v3d_csd_job *csd_job = NULL;
1271 struct v3d_job *clean_job = NULL;
1272 struct ww_acquire_ctx acquire_ctx;
1275 if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) {
1276 DRM_INFO("Invalid flags: %d\n", args->flags);
1280 ret = v3d_job_allocate((void *)&cpu_job, sizeof(*cpu_job));
1284 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
1285 ret = v3d_get_extensions(file_priv, args->extensions, &se, cpu_job);
1287 DRM_DEBUG("Failed to get extensions.\n");
1292 /* Every CPU job must have a CPU job user extension */
1293 if (!cpu_job->job_type) {
1294 DRM_DEBUG("CPU job must have a CPU job user extension.\n");
1299 if (args->bo_handle_count != cpu_job_bo_handle_count[cpu_job->job_type]) {
1300 DRM_DEBUG("This CPU job was not submitted with the proper number of BOs.\n");
1305 trace_v3d_submit_cpu_ioctl(&v3d->drm, cpu_job->job_type);
1307 ret = v3d_job_init(v3d, file_priv, &cpu_job->base,
1308 v3d_job_free, 0, &se, V3D_CPU);
1310 v3d_job_deallocate((void *)&cpu_job);
1314 clean_job = cpu_job->indirect_csd.clean_job;
1315 csd_job = cpu_job->indirect_csd.job;
1317 if (args->bo_handle_count) {
1318 ret = v3d_lookup_bos(dev, file_priv, &cpu_job->base,
1319 args->bo_handles, args->bo_handle_count);
1323 ret = v3d_lock_bo_reservations(&cpu_job->base, &acquire_ctx);
1328 mutex_lock(&v3d->sched_lock);
1329 v3d_push_job(&cpu_job->base);
1331 switch (cpu_job->job_type) {
1332 case V3D_CPU_JOB_TYPE_INDIRECT_CSD:
1333 ret = drm_sched_job_add_dependency(&csd_job->base.base,
1334 dma_fence_get(cpu_job->base.done_fence));
1336 goto fail_unreserve;
1338 v3d_push_job(&csd_job->base);
1340 ret = drm_sched_job_add_dependency(&clean_job->base,
1341 dma_fence_get(csd_job->base.done_fence));
1343 goto fail_unreserve;
1345 v3d_push_job(clean_job);
1351 mutex_unlock(&v3d->sched_lock);
1353 out_se = (cpu_job->job_type == V3D_CPU_JOB_TYPE_INDIRECT_CSD) ? NULL : &se;
1355 v3d_attach_fences_and_unlock_reservation(file_priv,
1358 out_se, cpu_job->base.done_fence);
1360 switch (cpu_job->job_type) {
1361 case V3D_CPU_JOB_TYPE_INDIRECT_CSD:
1362 v3d_attach_fences_and_unlock_reservation(file_priv,
1364 &cpu_job->indirect_csd.acquire_ctx,
1365 0, &se, clean_job->done_fence);
1371 v3d_job_put(&cpu_job->base);
1372 v3d_job_put(&csd_job->base);
1373 v3d_job_put(clean_job);
1378 mutex_unlock(&v3d->sched_lock);
1380 drm_gem_unlock_reservations(cpu_job->base.bo, cpu_job->base.bo_count,
1383 drm_gem_unlock_reservations(clean_job->bo, clean_job->bo_count,
1384 &cpu_job->indirect_csd.acquire_ctx);
1387 v3d_job_cleanup((void *)cpu_job);
1388 v3d_job_cleanup((void *)csd_job);
1389 v3d_job_cleanup(clean_job);
1390 v3d_put_multisync_post_deps(&se);
1391 kvfree(cpu_job->timestamp_query.queries);
1392 kvfree(cpu_job->performance_query.queries);