1 // SPDX-License-Identifier: GPL-2.0+
2 /* Copyright (C) 2014-2018 Broadcom */
4 #include <linux/device.h>
5 #include <linux/dma-mapping.h>
7 #include <linux/module.h>
8 #include <linux/platform_device.h>
9 #include <linux/reset.h>
10 #include <linux/sched/signal.h>
11 #include <linux/uaccess.h>
13 #include <drm/drm_managed.h>
14 #include <drm/drm_syncobj.h>
15 #include <uapi/drm/v3d_drm.h>
19 #include "v3d_trace.h"
22 v3d_init_core(struct v3d_dev *v3d, int core)
24 /* Set OVRTMUOUT, which means that the texture sampler uniform
25 * configuration's tmu output type field is used, instead of
26 * using the hardware default behavior based on the texture
27 * type. If you want the default behavior, you can still put
28 * "2" in the indirect texture state's output_type field.
31 V3D_CORE_WRITE(core, V3D_CTL_MISCCFG, V3D_MISCCFG_OVRTMUOUT);
33 /* Whenever we flush the L2T cache, we always want to flush
36 V3D_CORE_WRITE(core, V3D_CTL_L2TFLSTA, 0);
37 V3D_CORE_WRITE(core, V3D_CTL_L2TFLEND, ~0);
40 /* Sets invariant state for the HW. */
42 v3d_init_hw_state(struct v3d_dev *v3d)
44 v3d_init_core(v3d, 0);
48 v3d_idle_axi(struct v3d_dev *v3d, int core)
50 V3D_CORE_WRITE(core, V3D_GMP_CFG, V3D_GMP_CFG_STOP_REQ);
52 if (wait_for((V3D_CORE_READ(core, V3D_GMP_STATUS) &
53 (V3D_GMP_STATUS_RD_COUNT_MASK |
54 V3D_GMP_STATUS_WR_COUNT_MASK |
55 V3D_GMP_STATUS_CFG_BUSY)) == 0, 100)) {
56 DRM_ERROR("Failed to wait for safe GMP shutdown\n");
61 v3d_idle_gca(struct v3d_dev *v3d)
66 V3D_GCA_WRITE(V3D_GCA_SAFE_SHUTDOWN, V3D_GCA_SAFE_SHUTDOWN_EN);
68 if (wait_for((V3D_GCA_READ(V3D_GCA_SAFE_SHUTDOWN_ACK) &
69 V3D_GCA_SAFE_SHUTDOWN_ACK_ACKED) ==
70 V3D_GCA_SAFE_SHUTDOWN_ACK_ACKED, 100)) {
71 DRM_ERROR("Failed to wait for safe GCA shutdown\n");
76 v3d_reset_by_bridge(struct v3d_dev *v3d)
78 int version = V3D_BRIDGE_READ(V3D_TOP_GR_BRIDGE_REVISION);
80 if (V3D_GET_FIELD(version, V3D_TOP_GR_BRIDGE_MAJOR) == 2) {
81 V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_0,
82 V3D_TOP_GR_BRIDGE_SW_INIT_0_V3D_CLK_108_SW_INIT);
83 V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_0, 0);
85 /* GFXH-1383: The SW_INIT may cause a stray write to address 0
86 * of the unit, so reset it to its power-on value here.
88 V3D_WRITE(V3D_HUB_AXICFG, V3D_HUB_AXICFG_MAX_LEN_MASK);
90 WARN_ON_ONCE(V3D_GET_FIELD(version,
91 V3D_TOP_GR_BRIDGE_MAJOR) != 7);
92 V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_1,
93 V3D_TOP_GR_BRIDGE_SW_INIT_1_V3D_CLK_108_SW_INIT);
94 V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_1, 0);
99 v3d_reset_v3d(struct v3d_dev *v3d)
102 reset_control_reset(v3d->reset);
104 v3d_reset_by_bridge(v3d);
106 v3d_init_hw_state(v3d);
110 v3d_reset(struct v3d_dev *v3d)
112 struct drm_device *dev = &v3d->drm;
114 DRM_DEV_ERROR(dev->dev, "Resetting GPU for hang.\n");
115 DRM_DEV_ERROR(dev->dev, "V3D_ERR_STAT: 0x%08x\n",
116 V3D_CORE_READ(0, V3D_ERR_STAT));
117 trace_v3d_reset_begin(dev);
119 /* XXX: only needed for safe powerdown, not reset. */
121 v3d_idle_axi(v3d, 0);
126 v3d_mmu_set_page_table(v3d);
129 v3d_perfmon_stop(v3d, v3d->active_perfmon, false);
131 trace_v3d_reset_end(dev);
135 v3d_flush_l3(struct v3d_dev *v3d)
138 u32 gca_ctrl = V3D_GCA_READ(V3D_GCA_CACHE_CTRL);
140 V3D_GCA_WRITE(V3D_GCA_CACHE_CTRL,
141 gca_ctrl | V3D_GCA_CACHE_CTRL_FLUSH);
144 V3D_GCA_WRITE(V3D_GCA_CACHE_CTRL,
145 gca_ctrl & ~V3D_GCA_CACHE_CTRL_FLUSH);
150 /* Invalidates the (read-only) L2C cache. This was the L2 cache for
151 * uniforms and instructions on V3D 3.2.
154 v3d_invalidate_l2c(struct v3d_dev *v3d, int core)
159 V3D_CORE_WRITE(core, V3D_CTL_L2CACTL,
164 /* Invalidates texture L2 cachelines */
166 v3d_flush_l2t(struct v3d_dev *v3d, int core)
168 /* While there is a busy bit (V3D_L2TCACTL_L2TFLS), we don't
169 * need to wait for completion before dispatching the job --
170 * L2T accesses will be stalled until the flush has completed.
171 * However, we do need to make sure we don't try to trigger a
172 * new flush while the L2_CLEAN queue is trying to
173 * synchronously clean after a job.
175 mutex_lock(&v3d->cache_clean_lock);
176 V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL,
177 V3D_L2TCACTL_L2TFLS |
178 V3D_SET_FIELD(V3D_L2TCACTL_FLM_FLUSH, V3D_L2TCACTL_FLM));
179 mutex_unlock(&v3d->cache_clean_lock);
182 /* Cleans texture L1 and L2 cachelines (writing back dirty data).
184 * For cleaning, which happens from the CACHE_CLEAN queue after CSD has
185 * executed, we need to make sure that the clean is done before
186 * signaling job completion. So, we synchronously wait before
187 * returning, and we make sure that L2 invalidates don't happen in the
188 * meantime to confuse our are-we-done checks.
191 v3d_clean_caches(struct v3d_dev *v3d)
193 struct drm_device *dev = &v3d->drm;
196 trace_v3d_cache_clean_begin(dev);
198 V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL, V3D_L2TCACTL_TMUWCF);
199 if (wait_for(!(V3D_CORE_READ(core, V3D_CTL_L2TCACTL) &
200 V3D_L2TCACTL_TMUWCF), 100)) {
201 DRM_ERROR("Timeout waiting for TMU write combiner flush\n");
204 mutex_lock(&v3d->cache_clean_lock);
205 V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL,
206 V3D_L2TCACTL_L2TFLS |
207 V3D_SET_FIELD(V3D_L2TCACTL_FLM_CLEAN, V3D_L2TCACTL_FLM));
209 if (wait_for(!(V3D_CORE_READ(core, V3D_CTL_L2TCACTL) &
210 V3D_L2TCACTL_L2TFLS), 100)) {
211 DRM_ERROR("Timeout waiting for L2T clean\n");
214 mutex_unlock(&v3d->cache_clean_lock);
216 trace_v3d_cache_clean_end(dev);
219 /* Invalidates the slice caches. These are read-only caches. */
221 v3d_invalidate_slices(struct v3d_dev *v3d, int core)
223 V3D_CORE_WRITE(core, V3D_CTL_SLCACTL,
224 V3D_SET_FIELD(0xf, V3D_SLCACTL_TVCCS) |
225 V3D_SET_FIELD(0xf, V3D_SLCACTL_TDCCS) |
226 V3D_SET_FIELD(0xf, V3D_SLCACTL_UCC) |
227 V3D_SET_FIELD(0xf, V3D_SLCACTL_ICC));
231 v3d_invalidate_caches(struct v3d_dev *v3d)
233 /* Invalidate the caches from the outside in. That way if
234 * another CL's concurrent use of nearby memory were to pull
235 * an invalidated cacheline back in, we wouldn't leave stale
236 * data in the inner cache.
239 v3d_invalidate_l2c(v3d, 0);
240 v3d_flush_l2t(v3d, 0);
241 v3d_invalidate_slices(v3d, 0);
244 /* Takes the reservation lock on all the BOs being referenced, so that
245 * at queue submit time we can update the reservations.
247 * We don't lock the RCL the tile alloc/state BOs, or overflow memory
248 * (all of which are on exec->unref_list). They're entirely private
249 * to v3d, so we don't attach dma-buf fences to them.
252 v3d_lock_bo_reservations(struct v3d_job *job,
253 struct ww_acquire_ctx *acquire_ctx)
257 ret = drm_gem_lock_reservations(job->bo, job->bo_count, acquire_ctx);
261 for (i = 0; i < job->bo_count; i++) {
262 ret = dma_resv_reserve_fences(job->bo[i]->resv, 1);
266 ret = drm_sched_job_add_implicit_dependencies(&job->base,
275 drm_gem_unlock_reservations(job->bo, job->bo_count, acquire_ctx);
280 * v3d_lookup_bos() - Sets up job->bo[] with the GEM objects
281 * referenced by the job.
283 * @file_priv: DRM file for this fd
284 * @job: V3D job being set up
285 * @bo_handles: GEM handles
286 * @bo_count: Number of GEM handles passed in
288 * The command validator needs to reference BOs by their index within
289 * the submitted job's BO list. This does the validation of the job's
290 * BO list and reference counting for the lifetime of the job.
292 * Note that this function doesn't need to unreference the BOs on
293 * failure, because that will happen at v3d_exec_cleanup() time.
296 v3d_lookup_bos(struct drm_device *dev,
297 struct drm_file *file_priv,
302 job->bo_count = bo_count;
304 if (!job->bo_count) {
305 /* See comment on bo_index for why we have to check
308 DRM_DEBUG("Rendering requires BOs\n");
312 return drm_gem_objects_lookup(file_priv,
313 (void __user *)(uintptr_t)bo_handles,
314 job->bo_count, &job->bo);
318 v3d_job_free(struct kref *ref)
320 struct v3d_job *job = container_of(ref, struct v3d_job, refcount);
324 for (i = 0; i < job->bo_count; i++)
325 drm_gem_object_put(job->bo[i]);
329 dma_fence_put(job->irq_fence);
330 dma_fence_put(job->done_fence);
333 v3d_perfmon_put(job->perfmon);
339 v3d_render_job_free(struct kref *ref)
341 struct v3d_render_job *job = container_of(ref, struct v3d_render_job,
343 struct v3d_bo *bo, *save;
345 list_for_each_entry_safe(bo, save, &job->unref_list, unref_head) {
346 drm_gem_object_put(&bo->base.base);
352 void v3d_job_cleanup(struct v3d_job *job)
357 drm_sched_job_cleanup(&job->base);
361 void v3d_job_put(struct v3d_job *job)
363 kref_put(&job->refcount, job->free);
367 v3d_wait_bo_ioctl(struct drm_device *dev, void *data,
368 struct drm_file *file_priv)
371 struct drm_v3d_wait_bo *args = data;
372 ktime_t start = ktime_get();
374 unsigned long timeout_jiffies =
375 nsecs_to_jiffies_timeout(args->timeout_ns);
380 ret = drm_gem_dma_resv_wait(file_priv, args->handle,
381 true, timeout_jiffies);
383 /* Decrement the user's timeout, in case we got interrupted
384 * such that the ioctl will be restarted.
386 delta_ns = ktime_to_ns(ktime_sub(ktime_get(), start));
387 if (delta_ns < args->timeout_ns)
388 args->timeout_ns -= delta_ns;
390 args->timeout_ns = 0;
392 /* Asked to wait beyond the jiffie/scheduler precision? */
393 if (ret == -ETIME && args->timeout_ns)
400 v3d_job_add_deps(struct drm_file *file_priv, struct v3d_job *job,
401 u32 in_sync, u32 point)
403 struct dma_fence *in_fence = NULL;
406 ret = drm_syncobj_find_fence(file_priv, in_sync, point, 0, &in_fence);
410 return drm_sched_job_add_dependency(&job->base, in_fence);
414 v3d_job_init(struct v3d_dev *v3d, struct drm_file *file_priv,
415 void **container, size_t size, void (*free)(struct kref *ref),
416 u32 in_sync, struct v3d_submit_ext *se, enum v3d_queue queue)
418 struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
420 bool has_multisync = se && (se->flags & DRM_V3D_EXT_ID_MULTI_SYNC);
423 *container = kcalloc(1, size, GFP_KERNEL);
425 DRM_ERROR("Cannot allocate memory for v3d job.");
433 ret = drm_sched_job_init(&job->base, &v3d_priv->sched_entity[queue],
439 if (se->in_sync_count && se->wait_stage == queue) {
440 struct drm_v3d_sem __user *handle = u64_to_user_ptr(se->in_syncs);
442 for (i = 0; i < se->in_sync_count; i++) {
443 struct drm_v3d_sem in;
445 if (copy_from_user(&in, handle++, sizeof(in))) {
447 DRM_DEBUG("Failed to copy wait dep handle.\n");
450 ret = v3d_job_add_deps(file_priv, job, in.handle, 0);
456 ret = v3d_job_add_deps(file_priv, job, in_sync, 0);
461 kref_init(&job->refcount);
466 drm_sched_job_cleanup(&job->base);
475 v3d_push_job(struct v3d_job *job)
477 drm_sched_job_arm(&job->base);
479 job->done_fence = dma_fence_get(&job->base.s_fence->finished);
481 /* put by scheduler job completion */
482 kref_get(&job->refcount);
484 drm_sched_entity_push_job(&job->base);
488 v3d_attach_fences_and_unlock_reservation(struct drm_file *file_priv,
490 struct ww_acquire_ctx *acquire_ctx,
492 struct v3d_submit_ext *se,
493 struct dma_fence *done_fence)
495 struct drm_syncobj *sync_out;
496 bool has_multisync = se && (se->flags & DRM_V3D_EXT_ID_MULTI_SYNC);
499 for (i = 0; i < job->bo_count; i++) {
500 /* XXX: Use shared fences for read-only objects. */
501 dma_resv_add_fence(job->bo[i]->resv, job->done_fence,
502 DMA_RESV_USAGE_WRITE);
505 drm_gem_unlock_reservations(job->bo, job->bo_count, acquire_ctx);
507 /* Update the return sync object for the job */
508 /* If it only supports a single signal semaphore*/
509 if (!has_multisync) {
510 sync_out = drm_syncobj_find(file_priv, out_sync);
512 drm_syncobj_replace_fence(sync_out, done_fence);
513 drm_syncobj_put(sync_out);
518 /* If multiple semaphores extension is supported */
519 if (se->out_sync_count) {
520 for (i = 0; i < se->out_sync_count; i++) {
521 drm_syncobj_replace_fence(se->out_syncs[i].syncobj,
523 drm_syncobj_put(se->out_syncs[i].syncobj);
525 kvfree(se->out_syncs);
530 v3d_put_multisync_post_deps(struct v3d_submit_ext *se)
534 if (!(se && se->out_sync_count))
537 for (i = 0; i < se->out_sync_count; i++)
538 drm_syncobj_put(se->out_syncs[i].syncobj);
539 kvfree(se->out_syncs);
543 v3d_get_multisync_post_deps(struct drm_file *file_priv,
544 struct v3d_submit_ext *se,
545 u32 count, u64 handles)
547 struct drm_v3d_sem __user *post_deps;
553 se->out_syncs = (struct v3d_submit_outsync *)
554 kvmalloc_array(count,
555 sizeof(struct v3d_submit_outsync),
560 post_deps = u64_to_user_ptr(handles);
562 for (i = 0; i < count; i++) {
563 struct drm_v3d_sem out;
565 if (copy_from_user(&out, post_deps++, sizeof(out))) {
567 DRM_DEBUG("Failed to copy post dep handles\n");
571 se->out_syncs[i].syncobj = drm_syncobj_find(file_priv,
573 if (!se->out_syncs[i].syncobj) {
578 se->out_sync_count = count;
583 for (i--; i >= 0; i--)
584 drm_syncobj_put(se->out_syncs[i].syncobj);
585 kvfree(se->out_syncs);
590 /* Get data for multiple binary semaphores synchronization. Parse syncobj
591 * to be signaled when job completes (out_sync).
594 v3d_get_multisync_submit_deps(struct drm_file *file_priv,
595 struct drm_v3d_extension __user *ext,
598 struct drm_v3d_multi_sync multisync;
599 struct v3d_submit_ext *se = data;
602 if (copy_from_user(&multisync, ext, sizeof(multisync)))
608 ret = v3d_get_multisync_post_deps(file_priv, data, multisync.out_sync_count,
609 multisync.out_syncs);
613 se->in_sync_count = multisync.in_sync_count;
614 se->in_syncs = multisync.in_syncs;
615 se->flags |= DRM_V3D_EXT_ID_MULTI_SYNC;
616 se->wait_stage = multisync.wait_stage;
621 /* Whenever userspace sets ioctl extensions, v3d_get_extensions parses data
622 * according to the extension id (name).
625 v3d_get_extensions(struct drm_file *file_priv,
629 struct drm_v3d_extension __user *user_ext;
632 user_ext = u64_to_user_ptr(ext_handles);
634 struct drm_v3d_extension ext;
636 if (copy_from_user(&ext, user_ext, sizeof(ext))) {
637 DRM_DEBUG("Failed to copy submit extension\n");
642 case DRM_V3D_EXT_ID_MULTI_SYNC:
643 ret = v3d_get_multisync_submit_deps(file_priv, user_ext, data);
648 DRM_DEBUG_DRIVER("Unknown extension id: %d\n", ext.id);
652 user_ext = u64_to_user_ptr(ext.next);
659 * v3d_submit_cl_ioctl() - Submits a job (frame) to the V3D.
661 * @data: ioctl argument
662 * @file_priv: DRM file for this fd
664 * This is the main entrypoint for userspace to submit a 3D frame to
665 * the GPU. Userspace provides the binner command list (if
666 * applicable), and the kernel sets up the render command list to draw
667 * to the framebuffer described in the ioctl, using the command lists
668 * that the 3D engine's binner will produce.
671 v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
672 struct drm_file *file_priv)
674 struct v3d_dev *v3d = to_v3d_dev(dev);
675 struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
676 struct drm_v3d_submit_cl *args = data;
677 struct v3d_submit_ext se = {0};
678 struct v3d_bin_job *bin = NULL;
679 struct v3d_render_job *render = NULL;
680 struct v3d_job *clean_job = NULL;
681 struct v3d_job *last_job;
682 struct ww_acquire_ctx acquire_ctx;
685 trace_v3d_submit_cl_ioctl(&v3d->drm, args->rcl_start, args->rcl_end);
691 args->flags & ~(DRM_V3D_SUBMIT_CL_FLUSH_CACHE |
692 DRM_V3D_SUBMIT_EXTENSION)) {
693 DRM_INFO("invalid flags: %d\n", args->flags);
697 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
698 ret = v3d_get_extensions(file_priv, args->extensions, &se);
700 DRM_DEBUG("Failed to get extensions.\n");
705 ret = v3d_job_init(v3d, file_priv, (void *)&render, sizeof(*render),
706 v3d_render_job_free, args->in_sync_rcl, &se, V3D_RENDER);
710 render->start = args->rcl_start;
711 render->end = args->rcl_end;
712 INIT_LIST_HEAD(&render->unref_list);
714 if (args->bcl_start != args->bcl_end) {
715 ret = v3d_job_init(v3d, file_priv, (void *)&bin, sizeof(*bin),
716 v3d_job_free, args->in_sync_bcl, &se, V3D_BIN);
720 bin->start = args->bcl_start;
721 bin->end = args->bcl_end;
722 bin->qma = args->qma;
723 bin->qms = args->qms;
724 bin->qts = args->qts;
725 bin->render = render;
728 if (args->flags & DRM_V3D_SUBMIT_CL_FLUSH_CACHE) {
729 ret = v3d_job_init(v3d, file_priv, (void *)&clean_job, sizeof(*clean_job),
730 v3d_job_free, 0, NULL, V3D_CACHE_CLEAN);
734 last_job = clean_job;
736 last_job = &render->base;
739 ret = v3d_lookup_bos(dev, file_priv, last_job,
740 args->bo_handles, args->bo_handle_count);
744 ret = v3d_lock_bo_reservations(last_job, &acquire_ctx);
748 if (args->perfmon_id) {
749 render->base.perfmon = v3d_perfmon_find(v3d_priv,
752 if (!render->base.perfmon) {
758 mutex_lock(&v3d->sched_lock);
760 bin->base.perfmon = render->base.perfmon;
761 v3d_perfmon_get(bin->base.perfmon);
762 v3d_push_job(&bin->base);
764 ret = drm_sched_job_add_dependency(&render->base.base,
765 dma_fence_get(bin->base.done_fence));
770 v3d_push_job(&render->base);
773 struct dma_fence *render_fence =
774 dma_fence_get(render->base.done_fence);
775 ret = drm_sched_job_add_dependency(&clean_job->base,
779 clean_job->perfmon = render->base.perfmon;
780 v3d_perfmon_get(clean_job->perfmon);
781 v3d_push_job(clean_job);
784 mutex_unlock(&v3d->sched_lock);
786 v3d_attach_fences_and_unlock_reservation(file_priv,
791 last_job->done_fence);
794 v3d_job_put(&bin->base);
795 v3d_job_put(&render->base);
797 v3d_job_put(clean_job);
802 mutex_unlock(&v3d->sched_lock);
804 drm_gem_unlock_reservations(last_job->bo,
805 last_job->bo_count, &acquire_ctx);
807 v3d_job_cleanup((void *)bin);
808 v3d_job_cleanup((void *)render);
809 v3d_job_cleanup(clean_job);
810 v3d_put_multisync_post_deps(&se);
816 * v3d_submit_tfu_ioctl() - Submits a TFU (texture formatting) job to the V3D.
818 * @data: ioctl argument
819 * @file_priv: DRM file for this fd
821 * Userspace provides the register setup for the TFU, which we don't
822 * need to validate since the TFU is behind the MMU.
825 v3d_submit_tfu_ioctl(struct drm_device *dev, void *data,
826 struct drm_file *file_priv)
828 struct v3d_dev *v3d = to_v3d_dev(dev);
829 struct drm_v3d_submit_tfu *args = data;
830 struct v3d_submit_ext se = {0};
831 struct v3d_tfu_job *job = NULL;
832 struct ww_acquire_ctx acquire_ctx;
835 trace_v3d_submit_tfu_ioctl(&v3d->drm, args->iia);
837 if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) {
838 DRM_DEBUG("invalid flags: %d\n", args->flags);
842 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
843 ret = v3d_get_extensions(file_priv, args->extensions, &se);
845 DRM_DEBUG("Failed to get extensions.\n");
850 ret = v3d_job_init(v3d, file_priv, (void *)&job, sizeof(*job),
851 v3d_job_free, args->in_sync, &se, V3D_TFU);
855 job->base.bo = kcalloc(ARRAY_SIZE(args->bo_handles),
856 sizeof(*job->base.bo), GFP_KERNEL);
864 for (job->base.bo_count = 0;
865 job->base.bo_count < ARRAY_SIZE(args->bo_handles);
866 job->base.bo_count++) {
867 struct drm_gem_object *bo;
869 if (!args->bo_handles[job->base.bo_count])
872 bo = drm_gem_object_lookup(file_priv, args->bo_handles[job->base.bo_count]);
874 DRM_DEBUG("Failed to look up GEM BO %d: %d\n",
876 args->bo_handles[job->base.bo_count]);
880 job->base.bo[job->base.bo_count] = bo;
883 ret = v3d_lock_bo_reservations(&job->base, &acquire_ctx);
887 mutex_lock(&v3d->sched_lock);
888 v3d_push_job(&job->base);
889 mutex_unlock(&v3d->sched_lock);
891 v3d_attach_fences_and_unlock_reservation(file_priv,
892 &job->base, &acquire_ctx,
895 job->base.done_fence);
897 v3d_job_put(&job->base);
902 v3d_job_cleanup((void *)job);
903 v3d_put_multisync_post_deps(&se);
909 * v3d_submit_csd_ioctl() - Submits a CSD (texture formatting) job to the V3D.
911 * @data: ioctl argument
912 * @file_priv: DRM file for this fd
914 * Userspace provides the register setup for the CSD, which we don't
915 * need to validate since the CSD is behind the MMU.
918 v3d_submit_csd_ioctl(struct drm_device *dev, void *data,
919 struct drm_file *file_priv)
921 struct v3d_dev *v3d = to_v3d_dev(dev);
922 struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
923 struct drm_v3d_submit_csd *args = data;
924 struct v3d_submit_ext se = {0};
925 struct v3d_csd_job *job = NULL;
926 struct v3d_job *clean_job = NULL;
927 struct ww_acquire_ctx acquire_ctx;
930 trace_v3d_submit_csd_ioctl(&v3d->drm, args->cfg[5], args->cfg[6]);
935 if (!v3d_has_csd(v3d)) {
936 DRM_DEBUG("Attempting CSD submit on non-CSD hardware\n");
940 if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) {
941 DRM_INFO("invalid flags: %d\n", args->flags);
945 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
946 ret = v3d_get_extensions(file_priv, args->extensions, &se);
948 DRM_DEBUG("Failed to get extensions.\n");
953 ret = v3d_job_init(v3d, file_priv, (void *)&job, sizeof(*job),
954 v3d_job_free, args->in_sync, &se, V3D_CSD);
958 ret = v3d_job_init(v3d, file_priv, (void *)&clean_job, sizeof(*clean_job),
959 v3d_job_free, 0, NULL, V3D_CACHE_CLEAN);
965 ret = v3d_lookup_bos(dev, file_priv, clean_job,
966 args->bo_handles, args->bo_handle_count);
970 ret = v3d_lock_bo_reservations(clean_job, &acquire_ctx);
974 if (args->perfmon_id) {
975 job->base.perfmon = v3d_perfmon_find(v3d_priv,
977 if (!job->base.perfmon) {
983 mutex_lock(&v3d->sched_lock);
984 v3d_push_job(&job->base);
986 ret = drm_sched_job_add_dependency(&clean_job->base,
987 dma_fence_get(job->base.done_fence));
991 v3d_push_job(clean_job);
992 mutex_unlock(&v3d->sched_lock);
994 v3d_attach_fences_and_unlock_reservation(file_priv,
999 clean_job->done_fence);
1001 v3d_job_put(&job->base);
1002 v3d_job_put(clean_job);
1007 mutex_unlock(&v3d->sched_lock);
1009 drm_gem_unlock_reservations(clean_job->bo, clean_job->bo_count,
1012 v3d_job_cleanup((void *)job);
1013 v3d_job_cleanup(clean_job);
1014 v3d_put_multisync_post_deps(&se);
1020 v3d_gem_init(struct drm_device *dev)
1022 struct v3d_dev *v3d = to_v3d_dev(dev);
1023 u32 pt_size = 4096 * 1024;
1026 for (i = 0; i < V3D_MAX_QUEUES; i++)
1027 v3d->queue[i].fence_context = dma_fence_context_alloc(1);
1029 spin_lock_init(&v3d->mm_lock);
1030 spin_lock_init(&v3d->job_lock);
1031 ret = drmm_mutex_init(dev, &v3d->bo_lock);
1034 ret = drmm_mutex_init(dev, &v3d->reset_lock);
1037 ret = drmm_mutex_init(dev, &v3d->sched_lock);
1040 ret = drmm_mutex_init(dev, &v3d->cache_clean_lock);
1044 /* Note: We don't allocate address 0. Various bits of HW
1045 * treat 0 as special, such as the occlusion query counters
1046 * where 0 means "disabled".
1048 drm_mm_init(&v3d->mm, 1, pt_size / sizeof(u32) - 1);
1050 v3d->pt = dma_alloc_wc(v3d->drm.dev, pt_size,
1052 GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);
1054 drm_mm_takedown(&v3d->mm);
1055 dev_err(v3d->drm.dev,
1056 "Failed to allocate page tables. Please ensure you have DMA enabled.\n");
1060 v3d_init_hw_state(v3d);
1061 v3d_mmu_set_page_table(v3d);
1063 ret = v3d_sched_init(v3d);
1065 drm_mm_takedown(&v3d->mm);
1066 dma_free_coherent(v3d->drm.dev, 4096 * 1024, (void *)v3d->pt,
1074 v3d_gem_destroy(struct drm_device *dev)
1076 struct v3d_dev *v3d = to_v3d_dev(dev);
1078 v3d_sched_fini(v3d);
1080 /* Waiting for jobs to finish would need to be done before
1081 * unregistering V3D.
1083 WARN_ON(v3d->bin_job);
1084 WARN_ON(v3d->render_job);
1086 drm_mm_takedown(&v3d->mm);
1088 dma_free_coherent(v3d->drm.dev, 4096 * 1024, (void *)v3d->pt,