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_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)
155 struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
156 bool has_multisync = se && (se->flags & DRM_V3D_EXT_ID_MULTI_SYNC);
161 job->file = file_priv;
163 ret = drm_sched_job_init(&job->base, &v3d_priv->sched_entity[queue],
169 if (se->in_sync_count && se->wait_stage == queue) {
170 struct drm_v3d_sem __user *handle = u64_to_user_ptr(se->in_syncs);
172 for (i = 0; i < se->in_sync_count; i++) {
173 struct drm_v3d_sem in;
175 if (copy_from_user(&in, handle++, sizeof(in))) {
177 DRM_DEBUG("Failed to copy wait dep handle.\n");
180 ret = drm_sched_job_add_syncobj_dependency(&job->base, file_priv, in.handle, 0);
182 // TODO: Investigate why this was filtered out for the IOCTL.
183 if (ret && ret != -ENOENT)
188 ret = drm_sched_job_add_syncobj_dependency(&job->base, file_priv, in_sync, 0);
190 // TODO: Investigate why this was filtered out for the IOCTL.
191 if (ret && ret != -ENOENT)
195 kref_init(&job->refcount);
200 drm_sched_job_cleanup(&job->base);
205 v3d_push_job(struct v3d_job *job)
207 drm_sched_job_arm(&job->base);
209 job->done_fence = dma_fence_get(&job->base.s_fence->finished);
211 /* put by scheduler job completion */
212 kref_get(&job->refcount);
214 drm_sched_entity_push_job(&job->base);
218 v3d_attach_fences_and_unlock_reservation(struct drm_file *file_priv,
220 struct ww_acquire_ctx *acquire_ctx,
222 struct v3d_submit_ext *se,
223 struct dma_fence *done_fence)
225 struct drm_syncobj *sync_out;
226 bool has_multisync = se && (se->flags & DRM_V3D_EXT_ID_MULTI_SYNC);
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);
235 drm_gem_unlock_reservations(job->bo, job->bo_count, acquire_ctx);
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);
242 drm_syncobj_replace_fence(sync_out, done_fence);
243 drm_syncobj_put(sync_out);
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,
253 drm_syncobj_put(se->out_syncs[i].syncobj);
255 kvfree(se->out_syncs);
260 v3d_setup_csd_jobs_and_bos(struct drm_file *file_priv,
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)
270 ret = v3d_job_allocate((void *)job, sizeof(**job));
274 ret = v3d_job_init(v3d, file_priv, &(*job)->base,
275 v3d_job_free, args->in_sync, se, V3D_CSD);
279 ret = v3d_job_allocate((void *)clean_job, sizeof(**clean_job));
283 ret = v3d_job_init(v3d, file_priv, *clean_job,
284 v3d_job_free, 0, NULL, V3D_CACHE_CLEAN);
288 (*job)->args = *args;
290 ret = v3d_lookup_bos(&v3d->drm, file_priv, *clean_job,
291 args->bo_handles, args->bo_handle_count);
295 return v3d_lock_bo_reservations(*clean_job, acquire_ctx);
299 v3d_put_multisync_post_deps(struct v3d_submit_ext *se)
303 if (!(se && se->out_sync_count))
306 for (i = 0; i < se->out_sync_count; i++)
307 drm_syncobj_put(se->out_syncs[i].syncobj);
308 kvfree(se->out_syncs);
312 v3d_get_multisync_post_deps(struct drm_file *file_priv,
313 struct v3d_submit_ext *se,
314 u32 count, u64 handles)
316 struct drm_v3d_sem __user *post_deps;
322 se->out_syncs = (struct v3d_submit_outsync *)
323 kvmalloc_array(count,
324 sizeof(struct v3d_submit_outsync),
329 post_deps = u64_to_user_ptr(handles);
331 for (i = 0; i < count; i++) {
332 struct drm_v3d_sem out;
334 if (copy_from_user(&out, post_deps++, sizeof(out))) {
336 DRM_DEBUG("Failed to copy post dep handles\n");
340 se->out_syncs[i].syncobj = drm_syncobj_find(file_priv,
342 if (!se->out_syncs[i].syncobj) {
347 se->out_sync_count = count;
352 for (i--; i >= 0; i--)
353 drm_syncobj_put(se->out_syncs[i].syncobj);
354 kvfree(se->out_syncs);
359 /* Get data for multiple binary semaphores synchronization. Parse syncobj
360 * to be signaled when job completes (out_sync).
363 v3d_get_multisync_submit_deps(struct drm_file *file_priv,
364 struct drm_v3d_extension __user *ext,
365 struct v3d_submit_ext *se)
367 struct drm_v3d_multi_sync multisync;
370 if (se->in_sync_count || se->out_sync_count) {
371 DRM_DEBUG("Two multisync extensions were added to the same job.");
375 if (copy_from_user(&multisync, ext, sizeof(multisync)))
381 ret = v3d_get_multisync_post_deps(file_priv, se, multisync.out_sync_count,
382 multisync.out_syncs);
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;
394 /* Get data for the indirect CSD job submission. */
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)
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;
406 DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
411 DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
415 if (copy_from_user(&indirect_csd, ext, sizeof(indirect_csd)))
418 if (!v3d_has_csd(v3d)) {
419 DRM_DEBUG("Attempting CSD submit on non-CSD hardware.\n");
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));
429 info->indirect = drm_gem_object_lookup(file_priv, indirect_csd.indirect);
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);
436 /* Get data for the query timestamp job submission. */
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)
442 u32 __user *offsets, *syncs;
443 struct drm_v3d_timestamp_query timestamp;
446 DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
451 DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
455 if (copy_from_user(×tamp, ext, sizeof(timestamp)))
461 job->job_type = V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY;
463 job->timestamp_query.queries = kvmalloc_array(timestamp.count,
464 sizeof(struct v3d_timestamp_query),
466 if (!job->timestamp_query.queries)
469 offsets = u64_to_user_ptr(timestamp.offsets);
470 syncs = u64_to_user_ptr(timestamp.syncs);
472 for (int i = 0; i < timestamp.count; i++) {
475 if (copy_from_user(&offset, offsets++, sizeof(offset))) {
476 kvfree(job->timestamp_query.queries);
480 job->timestamp_query.queries[i].offset = offset;
482 if (copy_from_user(&sync, syncs++, sizeof(sync))) {
483 kvfree(job->timestamp_query.queries);
487 job->timestamp_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
489 job->timestamp_query.count = timestamp.count;
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)
500 struct drm_v3d_reset_timestamp_query reset;
503 DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
508 DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
512 if (copy_from_user(&reset, ext, sizeof(reset)))
515 job->job_type = V3D_CPU_JOB_TYPE_RESET_TIMESTAMP_QUERY;
517 job->timestamp_query.queries = kvmalloc_array(reset.count,
518 sizeof(struct v3d_timestamp_query),
520 if (!job->timestamp_query.queries)
523 syncs = u64_to_user_ptr(reset.syncs);
525 for (int i = 0; i < reset.count; i++) {
528 job->timestamp_query.queries[i].offset = reset.offset + 8 * i;
530 if (copy_from_user(&sync, syncs++, sizeof(sync))) {
531 kvfree(job->timestamp_query.queries);
535 job->timestamp_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
537 job->timestamp_query.count = reset.count;
542 /* Get data for the copy timestamp query results job submission. */
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)
548 u32 __user *offsets, *syncs;
549 struct drm_v3d_copy_timestamp_query copy;
553 DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
558 DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
562 if (copy_from_user(©, ext, sizeof(copy)))
568 job->job_type = V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY;
570 job->timestamp_query.queries = kvmalloc_array(copy.count,
571 sizeof(struct v3d_timestamp_query),
573 if (!job->timestamp_query.queries)
576 offsets = u64_to_user_ptr(copy.offsets);
577 syncs = u64_to_user_ptr(copy.syncs);
579 for (i = 0; i < copy.count; i++) {
582 if (copy_from_user(&offset, offsets++, sizeof(offset))) {
583 kvfree(job->timestamp_query.queries);
587 job->timestamp_query.queries[i].offset = offset;
589 if (copy_from_user(&sync, syncs++, sizeof(sync))) {
590 kvfree(job->timestamp_query.queries);
594 job->timestamp_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
596 job->timestamp_query.count = copy.count;
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;
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)
613 u64 __user *kperfmon_ids;
614 struct drm_v3d_reset_performance_query reset;
617 DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
622 DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
626 if (copy_from_user(&reset, ext, sizeof(reset)))
629 job->job_type = V3D_CPU_JOB_TYPE_RESET_PERFORMANCE_QUERY;
631 job->performance_query.queries = kvmalloc_array(reset.count,
632 sizeof(struct v3d_performance_query),
634 if (!job->performance_query.queries)
637 syncs = u64_to_user_ptr(reset.syncs);
638 kperfmon_ids = u64_to_user_ptr(reset.kperfmon_ids);
640 for (int i = 0; i < reset.count; i++) {
643 u32 __user *ids_pointer;
646 if (copy_from_user(&sync, syncs++, sizeof(sync))) {
647 kvfree(job->performance_query.queries);
651 job->performance_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
653 if (copy_from_user(&ids, kperfmon_ids++, sizeof(ids))) {
654 kvfree(job->performance_query.queries);
658 ids_pointer = u64_to_user_ptr(ids);
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);
666 job->performance_query.queries[i].kperfmon_ids[j] = id;
669 job->performance_query.count = reset.count;
670 job->performance_query.nperfmons = reset.nperfmons;
676 v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv,
677 struct drm_v3d_extension __user *ext,
678 struct v3d_cpu_job *job)
681 u64 __user *kperfmon_ids;
682 struct drm_v3d_copy_performance_query copy;
685 DRM_DEBUG("CPU job extension was attached to a GPU job.\n");
690 DRM_DEBUG("Two CPU job extensions were added to the same CPU job.\n");
694 if (copy_from_user(©, ext, sizeof(copy)))
700 job->job_type = V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY;
702 job->performance_query.queries = kvmalloc_array(copy.count,
703 sizeof(struct v3d_performance_query),
705 if (!job->performance_query.queries)
708 syncs = u64_to_user_ptr(copy.syncs);
709 kperfmon_ids = u64_to_user_ptr(copy.kperfmon_ids);
711 for (int i = 0; i < copy.count; i++) {
714 u32 __user *ids_pointer;
717 if (copy_from_user(&sync, syncs++, sizeof(sync))) {
718 kvfree(job->performance_query.queries);
722 job->performance_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
724 if (copy_from_user(&ids, kperfmon_ids++, sizeof(ids))) {
725 kvfree(job->performance_query.queries);
729 ids_pointer = u64_to_user_ptr(ids);
731 for (int j = 0; j < copy.nperfmons; j++) {
732 if (copy_from_user(&id, ids_pointer++, sizeof(id))) {
733 kvfree(job->performance_query.queries);
737 job->performance_query.queries[i].kperfmon_ids[j] = id;
740 job->performance_query.count = copy.count;
741 job->performance_query.nperfmons = copy.nperfmons;
742 job->performance_query.ncounters = copy.ncounters;
744 job->copy.do_64bit = copy.do_64bit;
745 job->copy.do_partial = copy.do_partial;
746 job->copy.availability_bit = copy.availability_bit;
747 job->copy.offset = copy.offset;
748 job->copy.stride = copy.stride;
753 /* Whenever userspace sets ioctl extensions, v3d_get_extensions parses data
754 * according to the extension id (name).
757 v3d_get_extensions(struct drm_file *file_priv,
759 struct v3d_submit_ext *se,
760 struct v3d_cpu_job *job)
762 struct drm_v3d_extension __user *user_ext;
765 user_ext = u64_to_user_ptr(ext_handles);
767 struct drm_v3d_extension ext;
769 if (copy_from_user(&ext, user_ext, sizeof(ext))) {
770 DRM_DEBUG("Failed to copy submit extension\n");
775 case DRM_V3D_EXT_ID_MULTI_SYNC:
776 ret = v3d_get_multisync_submit_deps(file_priv, user_ext, se);
778 case DRM_V3D_EXT_ID_CPU_INDIRECT_CSD:
779 ret = v3d_get_cpu_indirect_csd_params(file_priv, user_ext, job);
781 case DRM_V3D_EXT_ID_CPU_TIMESTAMP_QUERY:
782 ret = v3d_get_cpu_timestamp_query_params(file_priv, user_ext, job);
784 case DRM_V3D_EXT_ID_CPU_RESET_TIMESTAMP_QUERY:
785 ret = v3d_get_cpu_reset_timestamp_params(file_priv, user_ext, job);
787 case DRM_V3D_EXT_ID_CPU_COPY_TIMESTAMP_QUERY:
788 ret = v3d_get_cpu_copy_query_results_params(file_priv, user_ext, job);
790 case DRM_V3D_EXT_ID_CPU_RESET_PERFORMANCE_QUERY:
791 ret = v3d_get_cpu_reset_performance_params(file_priv, user_ext, job);
793 case DRM_V3D_EXT_ID_CPU_COPY_PERFORMANCE_QUERY:
794 ret = v3d_get_cpu_copy_performance_query_params(file_priv, user_ext, job);
797 DRM_DEBUG_DRIVER("Unknown extension id: %d\n", ext.id);
804 user_ext = u64_to_user_ptr(ext.next);
811 * v3d_submit_cl_ioctl() - Submits a job (frame) to the V3D.
813 * @data: ioctl argument
814 * @file_priv: DRM file for this fd
816 * This is the main entrypoint for userspace to submit a 3D frame to
817 * the GPU. Userspace provides the binner command list (if
818 * applicable), and the kernel sets up the render command list to draw
819 * to the framebuffer described in the ioctl, using the command lists
820 * that the 3D engine's binner will produce.
823 v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
824 struct drm_file *file_priv)
826 struct v3d_dev *v3d = to_v3d_dev(dev);
827 struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
828 struct drm_v3d_submit_cl *args = data;
829 struct v3d_submit_ext se = {0};
830 struct v3d_bin_job *bin = NULL;
831 struct v3d_render_job *render = NULL;
832 struct v3d_job *clean_job = NULL;
833 struct v3d_job *last_job;
834 struct ww_acquire_ctx acquire_ctx;
837 trace_v3d_submit_cl_ioctl(&v3d->drm, args->rcl_start, args->rcl_end);
843 args->flags & ~(DRM_V3D_SUBMIT_CL_FLUSH_CACHE |
844 DRM_V3D_SUBMIT_EXTENSION)) {
845 DRM_INFO("invalid flags: %d\n", args->flags);
849 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
850 ret = v3d_get_extensions(file_priv, args->extensions, &se, NULL);
852 DRM_DEBUG("Failed to get extensions.\n");
857 ret = v3d_job_allocate((void *)&render, sizeof(*render));
861 ret = v3d_job_init(v3d, file_priv, &render->base,
862 v3d_render_job_free, args->in_sync_rcl, &se, V3D_RENDER);
866 render->start = args->rcl_start;
867 render->end = args->rcl_end;
868 INIT_LIST_HEAD(&render->unref_list);
870 if (args->bcl_start != args->bcl_end) {
871 ret = v3d_job_allocate((void *)&bin, sizeof(*bin));
875 ret = v3d_job_init(v3d, file_priv, &bin->base,
876 v3d_job_free, args->in_sync_bcl, &se, V3D_BIN);
880 bin->start = args->bcl_start;
881 bin->end = args->bcl_end;
882 bin->qma = args->qma;
883 bin->qms = args->qms;
884 bin->qts = args->qts;
885 bin->render = render;
888 if (args->flags & DRM_V3D_SUBMIT_CL_FLUSH_CACHE) {
889 ret = v3d_job_allocate((void *)&clean_job, sizeof(*clean_job));
893 ret = v3d_job_init(v3d, file_priv, clean_job,
894 v3d_job_free, 0, NULL, V3D_CACHE_CLEAN);
898 last_job = clean_job;
900 last_job = &render->base;
903 ret = v3d_lookup_bos(dev, file_priv, last_job,
904 args->bo_handles, args->bo_handle_count);
908 ret = v3d_lock_bo_reservations(last_job, &acquire_ctx);
912 if (args->perfmon_id) {
913 render->base.perfmon = v3d_perfmon_find(v3d_priv,
916 if (!render->base.perfmon) {
922 mutex_lock(&v3d->sched_lock);
924 bin->base.perfmon = render->base.perfmon;
925 v3d_perfmon_get(bin->base.perfmon);
926 v3d_push_job(&bin->base);
928 ret = drm_sched_job_add_dependency(&render->base.base,
929 dma_fence_get(bin->base.done_fence));
934 v3d_push_job(&render->base);
937 struct dma_fence *render_fence =
938 dma_fence_get(render->base.done_fence);
939 ret = drm_sched_job_add_dependency(&clean_job->base,
943 clean_job->perfmon = render->base.perfmon;
944 v3d_perfmon_get(clean_job->perfmon);
945 v3d_push_job(clean_job);
948 mutex_unlock(&v3d->sched_lock);
950 v3d_attach_fences_and_unlock_reservation(file_priv,
955 last_job->done_fence);
957 v3d_job_put(&bin->base);
958 v3d_job_put(&render->base);
959 v3d_job_put(clean_job);
964 mutex_unlock(&v3d->sched_lock);
966 drm_gem_unlock_reservations(last_job->bo,
967 last_job->bo_count, &acquire_ctx);
969 v3d_job_cleanup((void *)bin);
970 v3d_job_cleanup((void *)render);
971 v3d_job_cleanup(clean_job);
972 v3d_put_multisync_post_deps(&se);
978 * v3d_submit_tfu_ioctl() - Submits a TFU (texture formatting) job to the V3D.
980 * @data: ioctl argument
981 * @file_priv: DRM file for this fd
983 * Userspace provides the register setup for the TFU, which we don't
984 * need to validate since the TFU is behind the MMU.
987 v3d_submit_tfu_ioctl(struct drm_device *dev, void *data,
988 struct drm_file *file_priv)
990 struct v3d_dev *v3d = to_v3d_dev(dev);
991 struct drm_v3d_submit_tfu *args = data;
992 struct v3d_submit_ext se = {0};
993 struct v3d_tfu_job *job = NULL;
994 struct ww_acquire_ctx acquire_ctx;
997 trace_v3d_submit_tfu_ioctl(&v3d->drm, args->iia);
999 if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) {
1000 DRM_DEBUG("invalid flags: %d\n", args->flags);
1004 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
1005 ret = v3d_get_extensions(file_priv, args->extensions, &se, NULL);
1007 DRM_DEBUG("Failed to get extensions.\n");
1012 ret = v3d_job_allocate((void *)&job, sizeof(*job));
1016 ret = v3d_job_init(v3d, file_priv, &job->base,
1017 v3d_job_free, args->in_sync, &se, V3D_TFU);
1021 job->base.bo = kcalloc(ARRAY_SIZE(args->bo_handles),
1022 sizeof(*job->base.bo), GFP_KERNEL);
1023 if (!job->base.bo) {
1030 for (job->base.bo_count = 0;
1031 job->base.bo_count < ARRAY_SIZE(args->bo_handles);
1032 job->base.bo_count++) {
1033 struct drm_gem_object *bo;
1035 if (!args->bo_handles[job->base.bo_count])
1038 bo = drm_gem_object_lookup(file_priv, args->bo_handles[job->base.bo_count]);
1040 DRM_DEBUG("Failed to look up GEM BO %d: %d\n",
1042 args->bo_handles[job->base.bo_count]);
1046 job->base.bo[job->base.bo_count] = bo;
1049 ret = v3d_lock_bo_reservations(&job->base, &acquire_ctx);
1053 mutex_lock(&v3d->sched_lock);
1054 v3d_push_job(&job->base);
1055 mutex_unlock(&v3d->sched_lock);
1057 v3d_attach_fences_and_unlock_reservation(file_priv,
1058 &job->base, &acquire_ctx,
1061 job->base.done_fence);
1063 v3d_job_put(&job->base);
1068 v3d_job_cleanup((void *)job);
1069 v3d_put_multisync_post_deps(&se);
1075 * v3d_submit_csd_ioctl() - Submits a CSD (compute shader) job to the V3D.
1077 * @data: ioctl argument
1078 * @file_priv: DRM file for this fd
1080 * Userspace provides the register setup for the CSD, which we don't
1081 * need to validate since the CSD is behind the MMU.
1084 v3d_submit_csd_ioctl(struct drm_device *dev, void *data,
1085 struct drm_file *file_priv)
1087 struct v3d_dev *v3d = to_v3d_dev(dev);
1088 struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
1089 struct drm_v3d_submit_csd *args = data;
1090 struct v3d_submit_ext se = {0};
1091 struct v3d_csd_job *job = NULL;
1092 struct v3d_job *clean_job = NULL;
1093 struct ww_acquire_ctx acquire_ctx;
1096 trace_v3d_submit_csd_ioctl(&v3d->drm, args->cfg[5], args->cfg[6]);
1101 if (!v3d_has_csd(v3d)) {
1102 DRM_DEBUG("Attempting CSD submit on non-CSD hardware\n");
1106 if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) {
1107 DRM_INFO("invalid flags: %d\n", args->flags);
1111 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
1112 ret = v3d_get_extensions(file_priv, args->extensions, &se, NULL);
1114 DRM_DEBUG("Failed to get extensions.\n");
1119 ret = v3d_setup_csd_jobs_and_bos(file_priv, v3d, args,
1120 &job, &clean_job, &se,
1125 if (args->perfmon_id) {
1126 job->base.perfmon = v3d_perfmon_find(v3d_priv,
1128 if (!job->base.perfmon) {
1134 mutex_lock(&v3d->sched_lock);
1135 v3d_push_job(&job->base);
1137 ret = drm_sched_job_add_dependency(&clean_job->base,
1138 dma_fence_get(job->base.done_fence));
1140 goto fail_unreserve;
1142 v3d_push_job(clean_job);
1143 mutex_unlock(&v3d->sched_lock);
1145 v3d_attach_fences_and_unlock_reservation(file_priv,
1150 clean_job->done_fence);
1152 v3d_job_put(&job->base);
1153 v3d_job_put(clean_job);
1158 mutex_unlock(&v3d->sched_lock);
1160 drm_gem_unlock_reservations(clean_job->bo, clean_job->bo_count,
1163 v3d_job_cleanup((void *)job);
1164 v3d_job_cleanup(clean_job);
1165 v3d_put_multisync_post_deps(&se);
1170 static const unsigned int cpu_job_bo_handle_count[] = {
1171 [V3D_CPU_JOB_TYPE_INDIRECT_CSD] = 1,
1172 [V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY] = 1,
1173 [V3D_CPU_JOB_TYPE_RESET_TIMESTAMP_QUERY] = 1,
1174 [V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY] = 2,
1175 [V3D_CPU_JOB_TYPE_RESET_PERFORMANCE_QUERY] = 0,
1176 [V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY] = 1,
1180 * v3d_submit_cpu_ioctl() - Submits a CPU job to the V3D.
1182 * @data: ioctl argument
1183 * @file_priv: DRM file for this fd
1185 * Userspace specifies the CPU job type and data required to perform its
1186 * operations through the drm_v3d_extension struct.
1189 v3d_submit_cpu_ioctl(struct drm_device *dev, void *data,
1190 struct drm_file *file_priv)
1192 struct v3d_dev *v3d = to_v3d_dev(dev);
1193 struct drm_v3d_submit_cpu *args = data;
1194 struct v3d_submit_ext se = {0};
1195 struct v3d_submit_ext *out_se = NULL;
1196 struct v3d_cpu_job *cpu_job = NULL;
1197 struct v3d_csd_job *csd_job = NULL;
1198 struct v3d_job *clean_job = NULL;
1199 struct ww_acquire_ctx acquire_ctx;
1202 if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) {
1203 DRM_INFO("Invalid flags: %d\n", args->flags);
1207 ret = v3d_job_allocate((void *)&cpu_job, sizeof(*cpu_job));
1211 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
1212 ret = v3d_get_extensions(file_priv, args->extensions, &se, cpu_job);
1214 DRM_DEBUG("Failed to get extensions.\n");
1219 /* Every CPU job must have a CPU job user extension */
1220 if (!cpu_job->job_type) {
1221 DRM_DEBUG("CPU job must have a CPU job user extension.\n");
1226 if (args->bo_handle_count != cpu_job_bo_handle_count[cpu_job->job_type]) {
1227 DRM_DEBUG("This CPU job was not submitted with the proper number of BOs.\n");
1232 trace_v3d_submit_cpu_ioctl(&v3d->drm, cpu_job->job_type);
1234 ret = v3d_job_init(v3d, file_priv, &cpu_job->base,
1235 v3d_job_free, 0, &se, V3D_CPU);
1239 clean_job = cpu_job->indirect_csd.clean_job;
1240 csd_job = cpu_job->indirect_csd.job;
1242 if (args->bo_handle_count) {
1243 ret = v3d_lookup_bos(dev, file_priv, &cpu_job->base,
1244 args->bo_handles, args->bo_handle_count);
1248 ret = v3d_lock_bo_reservations(&cpu_job->base, &acquire_ctx);
1253 mutex_lock(&v3d->sched_lock);
1254 v3d_push_job(&cpu_job->base);
1256 switch (cpu_job->job_type) {
1257 case V3D_CPU_JOB_TYPE_INDIRECT_CSD:
1258 ret = drm_sched_job_add_dependency(&csd_job->base.base,
1259 dma_fence_get(cpu_job->base.done_fence));
1261 goto fail_unreserve;
1263 v3d_push_job(&csd_job->base);
1265 ret = drm_sched_job_add_dependency(&clean_job->base,
1266 dma_fence_get(csd_job->base.done_fence));
1268 goto fail_unreserve;
1270 v3d_push_job(clean_job);
1276 mutex_unlock(&v3d->sched_lock);
1278 out_se = (cpu_job->job_type == V3D_CPU_JOB_TYPE_INDIRECT_CSD) ? NULL : &se;
1280 v3d_attach_fences_and_unlock_reservation(file_priv,
1283 out_se, cpu_job->base.done_fence);
1285 switch (cpu_job->job_type) {
1286 case V3D_CPU_JOB_TYPE_INDIRECT_CSD:
1287 v3d_attach_fences_and_unlock_reservation(file_priv,
1289 &cpu_job->indirect_csd.acquire_ctx,
1290 0, &se, clean_job->done_fence);
1296 v3d_job_put(&cpu_job->base);
1297 v3d_job_put(&csd_job->base);
1298 v3d_job_put(clean_job);
1303 mutex_unlock(&v3d->sched_lock);
1305 drm_gem_unlock_reservations(cpu_job->base.bo, cpu_job->base.bo_count,
1308 drm_gem_unlock_reservations(clean_job->bo, clean_job->bo_count,
1309 &cpu_job->indirect_csd.acquire_ctx);
1312 v3d_job_cleanup((void *)cpu_job);
1313 v3d_job_cleanup((void *)csd_job);
1314 v3d_job_cleanup(clean_job);
1315 v3d_put_multisync_post_deps(&se);
1316 kvfree(cpu_job->timestamp_query.queries);
1317 kvfree(cpu_job->performance_query.queries);