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 * we can attach fences and update the reservations after pushing the job
17 * We don't lock the RCL the tile alloc/state BOs, or overflow memory
18 * (all of which are on render->unref_list). They're entirely private
19 * to v3d, so we don't attach dma-buf fences to them.
22 v3d_lock_bo_reservations(struct v3d_job *job,
23 struct ww_acquire_ctx *acquire_ctx)
27 ret = drm_gem_lock_reservations(job->bo, job->bo_count, acquire_ctx);
31 for (i = 0; i < job->bo_count; i++) {
32 ret = dma_resv_reserve_fences(job->bo[i]->resv, 1);
36 ret = drm_sched_job_add_implicit_dependencies(&job->base,
45 drm_gem_unlock_reservations(job->bo, job->bo_count, acquire_ctx);
50 * v3d_lookup_bos() - Sets up job->bo[] with the GEM objects
51 * referenced by the job.
53 * @file_priv: DRM file for this fd
54 * @job: V3D job being set up
55 * @bo_handles: GEM handles
56 * @bo_count: Number of GEM handles passed in
58 * The command validator needs to reference BOs by their index within
59 * the submitted job's BO list. This does the validation of the job's
60 * BO list and reference counting for the lifetime of the job.
62 * Note that this function doesn't need to unreference the BOs on
63 * failure, because that will happen at `v3d_job_free()`.
66 v3d_lookup_bos(struct drm_device *dev,
67 struct drm_file *file_priv,
72 job->bo_count = bo_count;
75 /* See comment on bo_index for why we have to check
78 DRM_DEBUG("Rendering requires BOs\n");
82 return drm_gem_objects_lookup(file_priv,
83 (void __user *)(uintptr_t)bo_handles,
84 job->bo_count, &job->bo);
88 v3d_job_free(struct kref *ref)
90 struct v3d_job *job = container_of(ref, struct v3d_job, refcount);
94 for (i = 0; i < job->bo_count; i++)
95 drm_gem_object_put(job->bo[i]);
99 dma_fence_put(job->irq_fence);
100 dma_fence_put(job->done_fence);
103 v3d_perfmon_put(job->perfmon);
109 v3d_render_job_free(struct kref *ref)
111 struct v3d_render_job *job = container_of(ref, struct v3d_render_job,
113 struct v3d_bo *bo, *save;
115 list_for_each_entry_safe(bo, save, &job->unref_list, unref_head) {
116 drm_gem_object_put(&bo->base.base);
122 void v3d_job_cleanup(struct v3d_job *job)
127 drm_sched_job_cleanup(&job->base);
131 void v3d_job_put(struct v3d_job *job)
136 kref_put(&job->refcount, job->free);
140 v3d_job_allocate(void **container, size_t size)
142 *container = kcalloc(1, size, GFP_KERNEL);
144 DRM_ERROR("Cannot allocate memory for V3D job.\n");
152 v3d_job_deallocate(void **container)
159 v3d_job_init(struct v3d_dev *v3d, struct drm_file *file_priv,
160 struct v3d_job *job, void (*free)(struct kref *ref),
161 u32 in_sync, struct v3d_submit_ext *se, enum v3d_queue queue)
163 struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
164 bool has_multisync = se && (se->flags & DRM_V3D_EXT_ID_MULTI_SYNC);
169 job->file = file_priv;
171 ret = drm_sched_job_init(&job->base, &v3d_priv->sched_entity[queue],
177 if (se->in_sync_count && se->wait_stage == queue) {
178 struct drm_v3d_sem __user *handle = u64_to_user_ptr(se->in_syncs);
180 for (i = 0; i < se->in_sync_count; i++) {
181 struct drm_v3d_sem in;
183 if (copy_from_user(&in, handle++, sizeof(in))) {
185 DRM_DEBUG("Failed to copy wait dep handle.\n");
188 ret = drm_sched_job_add_syncobj_dependency(&job->base, file_priv, in.handle, 0);
190 // TODO: Investigate why this was filtered out for the IOCTL.
191 if (ret && ret != -ENOENT)
196 ret = drm_sched_job_add_syncobj_dependency(&job->base, file_priv, in_sync, 0);
198 // TODO: Investigate why this was filtered out for the IOCTL.
199 if (ret && ret != -ENOENT)
203 kref_init(&job->refcount);
208 drm_sched_job_cleanup(&job->base);
213 v3d_push_job(struct v3d_job *job)
215 drm_sched_job_arm(&job->base);
217 job->done_fence = dma_fence_get(&job->base.s_fence->finished);
219 /* put by scheduler job completion */
220 kref_get(&job->refcount);
222 drm_sched_entity_push_job(&job->base);
226 v3d_attach_fences_and_unlock_reservation(struct drm_file *file_priv,
228 struct ww_acquire_ctx *acquire_ctx,
230 struct v3d_submit_ext *se,
231 struct dma_fence *done_fence)
233 struct drm_syncobj *sync_out;
234 bool has_multisync = se && (se->flags & DRM_V3D_EXT_ID_MULTI_SYNC);
237 for (i = 0; i < job->bo_count; i++) {
238 /* XXX: Use shared fences for read-only objects. */
239 dma_resv_add_fence(job->bo[i]->resv, job->done_fence,
240 DMA_RESV_USAGE_WRITE);
243 drm_gem_unlock_reservations(job->bo, job->bo_count, acquire_ctx);
245 /* Update the return sync object for the job */
246 /* If it only supports a single signal semaphore*/
247 if (!has_multisync) {
248 sync_out = drm_syncobj_find(file_priv, out_sync);
250 drm_syncobj_replace_fence(sync_out, done_fence);
251 drm_syncobj_put(sync_out);
256 /* If multiple semaphores extension is supported */
257 if (se->out_sync_count) {
258 for (i = 0; i < se->out_sync_count; i++) {
259 drm_syncobj_replace_fence(se->out_syncs[i].syncobj,
261 drm_syncobj_put(se->out_syncs[i].syncobj);
263 kvfree(se->out_syncs);
268 v3d_setup_csd_jobs_and_bos(struct drm_file *file_priv,
270 struct drm_v3d_submit_csd *args,
271 struct v3d_csd_job **job,
272 struct v3d_job **clean_job,
273 struct v3d_submit_ext *se,
274 struct ww_acquire_ctx *acquire_ctx)
278 ret = v3d_job_allocate((void *)job, sizeof(**job));
282 ret = v3d_job_init(v3d, file_priv, &(*job)->base,
283 v3d_job_free, args->in_sync, se, V3D_CSD);
285 v3d_job_deallocate((void *)job);
289 ret = v3d_job_allocate((void *)clean_job, sizeof(**clean_job));
293 ret = v3d_job_init(v3d, file_priv, *clean_job,
294 v3d_job_free, 0, NULL, V3D_CACHE_CLEAN);
296 v3d_job_deallocate((void *)clean_job);
300 (*job)->args = *args;
302 ret = v3d_lookup_bos(&v3d->drm, file_priv, *clean_job,
303 args->bo_handles, args->bo_handle_count);
307 return v3d_lock_bo_reservations(*clean_job, acquire_ctx);
311 v3d_put_multisync_post_deps(struct v3d_submit_ext *se)
315 if (!(se && se->out_sync_count))
318 for (i = 0; i < se->out_sync_count; i++)
319 drm_syncobj_put(se->out_syncs[i].syncobj);
320 kvfree(se->out_syncs);
324 v3d_get_multisync_post_deps(struct drm_file *file_priv,
325 struct v3d_submit_ext *se,
326 u32 count, u64 handles)
328 struct drm_v3d_sem __user *post_deps;
334 se->out_syncs = (struct v3d_submit_outsync *)
335 kvmalloc_array(count,
336 sizeof(struct v3d_submit_outsync),
341 post_deps = u64_to_user_ptr(handles);
343 for (i = 0; i < count; i++) {
344 struct drm_v3d_sem out;
346 if (copy_from_user(&out, post_deps++, sizeof(out))) {
348 DRM_DEBUG("Failed to copy post dep handles\n");
352 se->out_syncs[i].syncobj = drm_syncobj_find(file_priv,
354 if (!se->out_syncs[i].syncobj) {
359 se->out_sync_count = count;
364 for (i--; i >= 0; i--)
365 drm_syncobj_put(se->out_syncs[i].syncobj);
366 kvfree(se->out_syncs);
371 /* Get data for multiple binary semaphores synchronization. Parse syncobj
372 * to be signaled when job completes (out_sync).
375 v3d_get_multisync_submit_deps(struct drm_file *file_priv,
376 struct drm_v3d_extension __user *ext,
377 struct v3d_submit_ext *se)
379 struct drm_v3d_multi_sync multisync;
382 if (se->in_sync_count || se->out_sync_count) {
383 DRM_DEBUG("Two multisync extensions were added to the same job.");
387 if (copy_from_user(&multisync, ext, sizeof(multisync)))
393 ret = v3d_get_multisync_post_deps(file_priv, se, multisync.out_sync_count,
394 multisync.out_syncs);
398 se->in_sync_count = multisync.in_sync_count;
399 se->in_syncs = multisync.in_syncs;
400 se->flags |= DRM_V3D_EXT_ID_MULTI_SYNC;
401 se->wait_stage = multisync.wait_stage;
406 /* Get data for the indirect CSD job submission. */
408 v3d_get_cpu_indirect_csd_params(struct drm_file *file_priv,
409 struct drm_v3d_extension __user *ext,
410 struct v3d_cpu_job *job)
412 struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
413 struct v3d_dev *v3d = v3d_priv->v3d;
414 struct drm_v3d_indirect_csd indirect_csd;
415 struct v3d_indirect_csd_info *info = &job->indirect_csd;
418 DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
423 DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
427 if (copy_from_user(&indirect_csd, ext, sizeof(indirect_csd)))
430 if (!v3d_has_csd(v3d)) {
431 DRM_DEBUG("Attempting CSD submit on non-CSD hardware.\n");
435 job->job_type = V3D_CPU_JOB_TYPE_INDIRECT_CSD;
436 info->offset = indirect_csd.offset;
437 info->wg_size = indirect_csd.wg_size;
438 memcpy(&info->wg_uniform_offsets, &indirect_csd.wg_uniform_offsets,
439 sizeof(indirect_csd.wg_uniform_offsets));
441 info->indirect = drm_gem_object_lookup(file_priv, indirect_csd.indirect);
443 return v3d_setup_csd_jobs_and_bos(file_priv, v3d, &indirect_csd.submit,
444 &info->job, &info->clean_job,
445 NULL, &info->acquire_ctx);
448 /* Get data for the query timestamp job submission. */
450 v3d_get_cpu_timestamp_query_params(struct drm_file *file_priv,
451 struct drm_v3d_extension __user *ext,
452 struct v3d_cpu_job *job)
454 u32 __user *offsets, *syncs;
455 struct drm_v3d_timestamp_query timestamp;
456 struct v3d_timestamp_query_info *query_info = &job->timestamp_query;
461 DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
466 DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
470 if (copy_from_user(×tamp, ext, sizeof(timestamp)))
476 job->job_type = V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY;
478 query_info->queries = kvmalloc_array(timestamp.count,
479 sizeof(struct v3d_timestamp_query),
481 if (!query_info->queries)
484 offsets = u64_to_user_ptr(timestamp.offsets);
485 syncs = u64_to_user_ptr(timestamp.syncs);
487 for (i = 0; i < timestamp.count; i++) {
490 if (get_user(offset, offsets++)) {
495 query_info->queries[i].offset = offset;
497 if (get_user(sync, syncs++)) {
502 query_info->queries[i].syncobj = drm_syncobj_find(file_priv,
504 if (!query_info->queries[i].syncobj) {
509 query_info->count = timestamp.count;
514 v3d_timestamp_query_info_free(&job->timestamp_query, i);
519 v3d_get_cpu_reset_timestamp_params(struct drm_file *file_priv,
520 struct drm_v3d_extension __user *ext,
521 struct v3d_cpu_job *job)
524 struct drm_v3d_reset_timestamp_query reset;
525 struct v3d_timestamp_query_info *query_info = &job->timestamp_query;
530 DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
535 DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
539 if (copy_from_user(&reset, ext, sizeof(reset)))
542 job->job_type = V3D_CPU_JOB_TYPE_RESET_TIMESTAMP_QUERY;
544 query_info->queries = kvmalloc_array(reset.count,
545 sizeof(struct v3d_timestamp_query),
547 if (!query_info->queries)
550 syncs = u64_to_user_ptr(reset.syncs);
552 for (i = 0; i < reset.count; i++) {
555 query_info->queries[i].offset = reset.offset + 8 * i;
557 if (get_user(sync, syncs++)) {
562 query_info->queries[i].syncobj = drm_syncobj_find(file_priv,
564 if (!query_info->queries[i].syncobj) {
569 query_info->count = reset.count;
574 v3d_timestamp_query_info_free(&job->timestamp_query, i);
578 /* Get data for the copy timestamp query results job submission. */
580 v3d_get_cpu_copy_query_results_params(struct drm_file *file_priv,
581 struct drm_v3d_extension __user *ext,
582 struct v3d_cpu_job *job)
584 u32 __user *offsets, *syncs;
585 struct drm_v3d_copy_timestamp_query copy;
586 struct v3d_timestamp_query_info *query_info = &job->timestamp_query;
591 DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
596 DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
600 if (copy_from_user(©, ext, sizeof(copy)))
606 job->job_type = V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY;
608 query_info->queries = kvmalloc_array(copy.count,
609 sizeof(struct v3d_timestamp_query),
611 if (!query_info->queries)
614 offsets = u64_to_user_ptr(copy.offsets);
615 syncs = u64_to_user_ptr(copy.syncs);
617 for (i = 0; i < copy.count; i++) {
620 if (get_user(offset, offsets++)) {
625 query_info->queries[i].offset = offset;
627 if (get_user(sync, syncs++)) {
632 query_info->queries[i].syncobj = drm_syncobj_find(file_priv,
634 if (!query_info->queries[i].syncobj) {
639 query_info->count = copy.count;
641 job->copy.do_64bit = copy.do_64bit;
642 job->copy.do_partial = copy.do_partial;
643 job->copy.availability_bit = copy.availability_bit;
644 job->copy.offset = copy.offset;
645 job->copy.stride = copy.stride;
650 v3d_timestamp_query_info_free(&job->timestamp_query, i);
655 v3d_copy_query_info(struct v3d_performance_query_info *query_info,
657 unsigned int nperfmons,
659 u64 __user *kperfmon_ids,
660 struct drm_file *file_priv)
665 for (i = 0; i < count; i++) {
666 struct v3d_performance_query *query = &query_info->queries[i];
667 u32 __user *ids_pointer;
671 if (get_user(sync, syncs++)) {
676 if (get_user(ids, kperfmon_ids++)) {
681 query->kperfmon_ids =
682 kvmalloc_array(nperfmons,
683 sizeof(struct v3d_performance_query *),
685 if (!query->kperfmon_ids) {
690 ids_pointer = u64_to_user_ptr(ids);
692 for (j = 0; j < nperfmons; j++) {
693 if (get_user(id, ids_pointer++)) {
694 kvfree(query->kperfmon_ids);
699 query->kperfmon_ids[j] = id;
702 query->syncobj = drm_syncobj_find(file_priv, sync);
703 if (!query->syncobj) {
704 kvfree(query->kperfmon_ids);
713 v3d_performance_query_info_free(query_info, i);
718 v3d_get_cpu_reset_performance_params(struct drm_file *file_priv,
719 struct drm_v3d_extension __user *ext,
720 struct v3d_cpu_job *job)
722 struct v3d_performance_query_info *query_info = &job->performance_query;
723 struct drm_v3d_reset_performance_query reset;
727 DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
732 DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
736 if (copy_from_user(&reset, ext, sizeof(reset)))
739 job->job_type = V3D_CPU_JOB_TYPE_RESET_PERFORMANCE_QUERY;
741 query_info->queries =
742 kvmalloc_array(reset.count,
743 sizeof(struct v3d_performance_query),
745 if (!query_info->queries)
748 err = v3d_copy_query_info(query_info,
751 u64_to_user_ptr(reset.syncs),
752 u64_to_user_ptr(reset.kperfmon_ids),
757 query_info->count = reset.count;
758 query_info->nperfmons = reset.nperfmons;
764 v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv,
765 struct drm_v3d_extension __user *ext,
766 struct v3d_cpu_job *job)
768 struct v3d_performance_query_info *query_info = &job->performance_query;
769 struct drm_v3d_copy_performance_query copy;
773 DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
778 DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
782 if (copy_from_user(©, ext, sizeof(copy)))
788 job->job_type = V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY;
790 query_info->queries =
791 kvmalloc_array(copy.count,
792 sizeof(struct v3d_performance_query),
794 if (!query_info->queries)
797 err = v3d_copy_query_info(query_info,
800 u64_to_user_ptr(copy.syncs),
801 u64_to_user_ptr(copy.kperfmon_ids),
806 query_info->count = copy.count;
807 query_info->nperfmons = copy.nperfmons;
808 query_info->ncounters = copy.ncounters;
810 job->copy.do_64bit = copy.do_64bit;
811 job->copy.do_partial = copy.do_partial;
812 job->copy.availability_bit = copy.availability_bit;
813 job->copy.offset = copy.offset;
814 job->copy.stride = copy.stride;
819 /* Whenever userspace sets ioctl extensions, v3d_get_extensions parses data
820 * according to the extension id (name).
823 v3d_get_extensions(struct drm_file *file_priv,
825 struct v3d_submit_ext *se,
826 struct v3d_cpu_job *job)
828 struct drm_v3d_extension __user *user_ext;
831 user_ext = u64_to_user_ptr(ext_handles);
833 struct drm_v3d_extension ext;
835 if (copy_from_user(&ext, user_ext, sizeof(ext))) {
836 DRM_DEBUG("Failed to copy submit extension\n");
841 case DRM_V3D_EXT_ID_MULTI_SYNC:
842 ret = v3d_get_multisync_submit_deps(file_priv, user_ext, se);
844 case DRM_V3D_EXT_ID_CPU_INDIRECT_CSD:
845 ret = v3d_get_cpu_indirect_csd_params(file_priv, user_ext, job);
847 case DRM_V3D_EXT_ID_CPU_TIMESTAMP_QUERY:
848 ret = v3d_get_cpu_timestamp_query_params(file_priv, user_ext, job);
850 case DRM_V3D_EXT_ID_CPU_RESET_TIMESTAMP_QUERY:
851 ret = v3d_get_cpu_reset_timestamp_params(file_priv, user_ext, job);
853 case DRM_V3D_EXT_ID_CPU_COPY_TIMESTAMP_QUERY:
854 ret = v3d_get_cpu_copy_query_results_params(file_priv, user_ext, job);
856 case DRM_V3D_EXT_ID_CPU_RESET_PERFORMANCE_QUERY:
857 ret = v3d_get_cpu_reset_performance_params(file_priv, user_ext, job);
859 case DRM_V3D_EXT_ID_CPU_COPY_PERFORMANCE_QUERY:
860 ret = v3d_get_cpu_copy_performance_query_params(file_priv, user_ext, job);
863 DRM_DEBUG_DRIVER("Unknown extension id: %d\n", ext.id);
870 user_ext = u64_to_user_ptr(ext.next);
877 * v3d_submit_cl_ioctl() - Submits a job (frame) to the V3D.
879 * @data: ioctl argument
880 * @file_priv: DRM file for this fd
882 * This is the main entrypoint for userspace to submit a 3D frame to
883 * the GPU. Userspace provides the binner command list (if
884 * applicable), and the kernel sets up the render command list to draw
885 * to the framebuffer described in the ioctl, using the command lists
886 * that the 3D engine's binner will produce.
889 v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
890 struct drm_file *file_priv)
892 struct v3d_dev *v3d = to_v3d_dev(dev);
893 struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
894 struct drm_v3d_submit_cl *args = data;
895 struct v3d_submit_ext se = {0};
896 struct v3d_bin_job *bin = NULL;
897 struct v3d_render_job *render = NULL;
898 struct v3d_job *clean_job = NULL;
899 struct v3d_job *last_job;
900 struct ww_acquire_ctx acquire_ctx;
903 trace_v3d_submit_cl_ioctl(&v3d->drm, args->rcl_start, args->rcl_end);
909 args->flags & ~(DRM_V3D_SUBMIT_CL_FLUSH_CACHE |
910 DRM_V3D_SUBMIT_EXTENSION)) {
911 DRM_INFO("invalid flags: %d\n", args->flags);
915 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
916 ret = v3d_get_extensions(file_priv, args->extensions, &se, NULL);
918 DRM_DEBUG("Failed to get extensions.\n");
923 ret = v3d_job_allocate((void *)&render, sizeof(*render));
927 ret = v3d_job_init(v3d, file_priv, &render->base,
928 v3d_render_job_free, args->in_sync_rcl, &se, V3D_RENDER);
930 v3d_job_deallocate((void *)&render);
934 render->start = args->rcl_start;
935 render->end = args->rcl_end;
936 INIT_LIST_HEAD(&render->unref_list);
938 if (args->bcl_start != args->bcl_end) {
939 ret = v3d_job_allocate((void *)&bin, sizeof(*bin));
943 ret = v3d_job_init(v3d, file_priv, &bin->base,
944 v3d_job_free, args->in_sync_bcl, &se, V3D_BIN);
946 v3d_job_deallocate((void *)&bin);
950 bin->start = args->bcl_start;
951 bin->end = args->bcl_end;
952 bin->qma = args->qma;
953 bin->qms = args->qms;
954 bin->qts = args->qts;
955 bin->render = render;
958 if (args->flags & DRM_V3D_SUBMIT_CL_FLUSH_CACHE) {
959 ret = v3d_job_allocate((void *)&clean_job, sizeof(*clean_job));
963 ret = v3d_job_init(v3d, file_priv, clean_job,
964 v3d_job_free, 0, NULL, V3D_CACHE_CLEAN);
966 v3d_job_deallocate((void *)&clean_job);
970 last_job = clean_job;
972 last_job = &render->base;
975 ret = v3d_lookup_bos(dev, file_priv, last_job,
976 args->bo_handles, args->bo_handle_count);
980 ret = v3d_lock_bo_reservations(last_job, &acquire_ctx);
984 if (args->perfmon_id) {
985 if (v3d->global_perfmon) {
990 render->base.perfmon = v3d_perfmon_find(v3d_priv,
993 if (!render->base.perfmon) {
999 mutex_lock(&v3d->sched_lock);
1001 bin->base.perfmon = render->base.perfmon;
1002 v3d_perfmon_get(bin->base.perfmon);
1003 v3d_push_job(&bin->base);
1005 ret = drm_sched_job_add_dependency(&render->base.base,
1006 dma_fence_get(bin->base.done_fence));
1008 goto fail_unreserve;
1011 v3d_push_job(&render->base);
1014 struct dma_fence *render_fence =
1015 dma_fence_get(render->base.done_fence);
1016 ret = drm_sched_job_add_dependency(&clean_job->base,
1019 goto fail_unreserve;
1020 clean_job->perfmon = render->base.perfmon;
1021 v3d_perfmon_get(clean_job->perfmon);
1022 v3d_push_job(clean_job);
1025 mutex_unlock(&v3d->sched_lock);
1027 v3d_attach_fences_and_unlock_reservation(file_priv,
1032 last_job->done_fence);
1034 v3d_job_put(&bin->base);
1035 v3d_job_put(&render->base);
1036 v3d_job_put(clean_job);
1041 mutex_unlock(&v3d->sched_lock);
1043 drm_gem_unlock_reservations(last_job->bo,
1044 last_job->bo_count, &acquire_ctx);
1046 v3d_job_cleanup((void *)bin);
1047 v3d_job_cleanup((void *)render);
1048 v3d_job_cleanup(clean_job);
1049 v3d_put_multisync_post_deps(&se);
1055 * v3d_submit_tfu_ioctl() - Submits a TFU (texture formatting) job to the V3D.
1057 * @data: ioctl argument
1058 * @file_priv: DRM file for this fd
1060 * Userspace provides the register setup for the TFU, which we don't
1061 * need to validate since the TFU is behind the MMU.
1064 v3d_submit_tfu_ioctl(struct drm_device *dev, void *data,
1065 struct drm_file *file_priv)
1067 struct v3d_dev *v3d = to_v3d_dev(dev);
1068 struct drm_v3d_submit_tfu *args = data;
1069 struct v3d_submit_ext se = {0};
1070 struct v3d_tfu_job *job = NULL;
1071 struct ww_acquire_ctx acquire_ctx;
1074 trace_v3d_submit_tfu_ioctl(&v3d->drm, args->iia);
1076 if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) {
1077 DRM_DEBUG("invalid flags: %d\n", args->flags);
1081 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
1082 ret = v3d_get_extensions(file_priv, args->extensions, &se, NULL);
1084 DRM_DEBUG("Failed to get extensions.\n");
1089 ret = v3d_job_allocate((void *)&job, sizeof(*job));
1093 ret = v3d_job_init(v3d, file_priv, &job->base,
1094 v3d_job_free, args->in_sync, &se, V3D_TFU);
1096 v3d_job_deallocate((void *)&job);
1100 job->base.bo = kcalloc(ARRAY_SIZE(args->bo_handles),
1101 sizeof(*job->base.bo), GFP_KERNEL);
1102 if (!job->base.bo) {
1109 for (job->base.bo_count = 0;
1110 job->base.bo_count < ARRAY_SIZE(args->bo_handles);
1111 job->base.bo_count++) {
1112 struct drm_gem_object *bo;
1114 if (!args->bo_handles[job->base.bo_count])
1117 bo = drm_gem_object_lookup(file_priv, args->bo_handles[job->base.bo_count]);
1119 DRM_DEBUG("Failed to look up GEM BO %d: %d\n",
1121 args->bo_handles[job->base.bo_count]);
1125 job->base.bo[job->base.bo_count] = bo;
1128 ret = v3d_lock_bo_reservations(&job->base, &acquire_ctx);
1132 mutex_lock(&v3d->sched_lock);
1133 v3d_push_job(&job->base);
1134 mutex_unlock(&v3d->sched_lock);
1136 v3d_attach_fences_and_unlock_reservation(file_priv,
1137 &job->base, &acquire_ctx,
1140 job->base.done_fence);
1142 v3d_job_put(&job->base);
1147 v3d_job_cleanup((void *)job);
1148 v3d_put_multisync_post_deps(&se);
1154 * v3d_submit_csd_ioctl() - Submits a CSD (compute shader) job to the V3D.
1156 * @data: ioctl argument
1157 * @file_priv: DRM file for this fd
1159 * Userspace provides the register setup for the CSD, which we don't
1160 * need to validate since the CSD is behind the MMU.
1163 v3d_submit_csd_ioctl(struct drm_device *dev, void *data,
1164 struct drm_file *file_priv)
1166 struct v3d_dev *v3d = to_v3d_dev(dev);
1167 struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
1168 struct drm_v3d_submit_csd *args = data;
1169 struct v3d_submit_ext se = {0};
1170 struct v3d_csd_job *job = NULL;
1171 struct v3d_job *clean_job = NULL;
1172 struct ww_acquire_ctx acquire_ctx;
1175 trace_v3d_submit_csd_ioctl(&v3d->drm, args->cfg[5], args->cfg[6]);
1180 if (!v3d_has_csd(v3d)) {
1181 DRM_DEBUG("Attempting CSD submit on non-CSD hardware\n");
1185 if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) {
1186 DRM_INFO("invalid flags: %d\n", args->flags);
1190 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
1191 ret = v3d_get_extensions(file_priv, args->extensions, &se, NULL);
1193 DRM_DEBUG("Failed to get extensions.\n");
1198 ret = v3d_setup_csd_jobs_and_bos(file_priv, v3d, args,
1199 &job, &clean_job, &se,
1204 if (args->perfmon_id) {
1205 if (v3d->global_perfmon) {
1210 job->base.perfmon = v3d_perfmon_find(v3d_priv,
1212 if (!job->base.perfmon) {
1218 mutex_lock(&v3d->sched_lock);
1219 v3d_push_job(&job->base);
1221 ret = drm_sched_job_add_dependency(&clean_job->base,
1222 dma_fence_get(job->base.done_fence));
1224 goto fail_unreserve;
1226 v3d_push_job(clean_job);
1227 mutex_unlock(&v3d->sched_lock);
1229 v3d_attach_fences_and_unlock_reservation(file_priv,
1234 clean_job->done_fence);
1236 v3d_job_put(&job->base);
1237 v3d_job_put(clean_job);
1242 mutex_unlock(&v3d->sched_lock);
1244 drm_gem_unlock_reservations(clean_job->bo, clean_job->bo_count,
1247 v3d_job_cleanup((void *)job);
1248 v3d_job_cleanup(clean_job);
1249 v3d_put_multisync_post_deps(&se);
1254 static const unsigned int cpu_job_bo_handle_count[] = {
1255 [V3D_CPU_JOB_TYPE_INDIRECT_CSD] = 1,
1256 [V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY] = 1,
1257 [V3D_CPU_JOB_TYPE_RESET_TIMESTAMP_QUERY] = 1,
1258 [V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY] = 2,
1259 [V3D_CPU_JOB_TYPE_RESET_PERFORMANCE_QUERY] = 0,
1260 [V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY] = 1,
1264 * v3d_submit_cpu_ioctl() - Submits a CPU job to the V3D.
1266 * @data: ioctl argument
1267 * @file_priv: DRM file for this fd
1269 * Userspace specifies the CPU job type and data required to perform its
1270 * operations through the drm_v3d_extension struct.
1273 v3d_submit_cpu_ioctl(struct drm_device *dev, void *data,
1274 struct drm_file *file_priv)
1276 struct v3d_dev *v3d = to_v3d_dev(dev);
1277 struct drm_v3d_submit_cpu *args = data;
1278 struct v3d_submit_ext se = {0};
1279 struct v3d_submit_ext *out_se = NULL;
1280 struct v3d_cpu_job *cpu_job = NULL;
1281 struct v3d_csd_job *csd_job = NULL;
1282 struct v3d_job *clean_job = NULL;
1283 struct ww_acquire_ctx acquire_ctx;
1286 if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) {
1287 DRM_INFO("Invalid flags: %d\n", args->flags);
1291 ret = v3d_job_allocate((void *)&cpu_job, sizeof(*cpu_job));
1295 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
1296 ret = v3d_get_extensions(file_priv, args->extensions, &se, cpu_job);
1298 DRM_DEBUG("Failed to get extensions.\n");
1303 /* Every CPU job must have a CPU job user extension */
1304 if (!cpu_job->job_type) {
1305 DRM_DEBUG("CPU job must have a CPU job user extension.\n");
1310 if (args->bo_handle_count != cpu_job_bo_handle_count[cpu_job->job_type]) {
1311 DRM_DEBUG("This CPU job was not submitted with the proper number of BOs.\n");
1316 trace_v3d_submit_cpu_ioctl(&v3d->drm, cpu_job->job_type);
1318 ret = v3d_job_init(v3d, file_priv, &cpu_job->base,
1319 v3d_job_free, 0, &se, V3D_CPU);
1321 v3d_job_deallocate((void *)&cpu_job);
1325 clean_job = cpu_job->indirect_csd.clean_job;
1326 csd_job = cpu_job->indirect_csd.job;
1328 if (args->bo_handle_count) {
1329 ret = v3d_lookup_bos(dev, file_priv, &cpu_job->base,
1330 args->bo_handles, args->bo_handle_count);
1334 ret = v3d_lock_bo_reservations(&cpu_job->base, &acquire_ctx);
1339 mutex_lock(&v3d->sched_lock);
1340 v3d_push_job(&cpu_job->base);
1342 switch (cpu_job->job_type) {
1343 case V3D_CPU_JOB_TYPE_INDIRECT_CSD:
1344 ret = drm_sched_job_add_dependency(&csd_job->base.base,
1345 dma_fence_get(cpu_job->base.done_fence));
1347 goto fail_unreserve;
1349 v3d_push_job(&csd_job->base);
1351 ret = drm_sched_job_add_dependency(&clean_job->base,
1352 dma_fence_get(csd_job->base.done_fence));
1354 goto fail_unreserve;
1356 v3d_push_job(clean_job);
1362 mutex_unlock(&v3d->sched_lock);
1364 out_se = (cpu_job->job_type == V3D_CPU_JOB_TYPE_INDIRECT_CSD) ? NULL : &se;
1366 v3d_attach_fences_and_unlock_reservation(file_priv,
1369 out_se, cpu_job->base.done_fence);
1371 switch (cpu_job->job_type) {
1372 case V3D_CPU_JOB_TYPE_INDIRECT_CSD:
1373 v3d_attach_fences_and_unlock_reservation(file_priv,
1375 &cpu_job->indirect_csd.acquire_ctx,
1376 0, &se, clean_job->done_fence);
1382 v3d_job_put(&cpu_job->base);
1383 v3d_job_put(&csd_job->base);
1384 v3d_job_put(clean_job);
1389 mutex_unlock(&v3d->sched_lock);
1391 drm_gem_unlock_reservations(cpu_job->base.bo, cpu_job->base.bo_count,
1394 drm_gem_unlock_reservations(clean_job->bo, clean_job->bo_count,
1395 &cpu_job->indirect_csd.acquire_ctx);
1398 v3d_job_cleanup((void *)cpu_job);
1399 v3d_job_cleanup((void *)csd_job);
1400 v3d_job_cleanup(clean_job);
1401 v3d_put_multisync_post_deps(&se);
1402 kvfree(cpu_job->timestamp_query.queries);
1403 kvfree(cpu_job->performance_query.queries);