1 // SPDX-License-Identifier: GPL-2.0+
2 /* Copyright (C) 2014-2018 Broadcom */
5 #include <drm/drm_syncobj.h>
6 #include <linux/module.h>
7 #include <linux/platform_device.h>
8 #include <linux/pm_runtime.h>
9 #include <linux/device.h>
11 #include <linux/sched/signal.h>
13 #include "uapi/drm/v3d_drm.h"
16 #include "v3d_trace.h"
19 v3d_init_core(struct v3d_dev *v3d, int core)
21 /* Set OVRTMUOUT, which means that the texture sampler uniform
22 * configuration's tmu output type field is used, instead of
23 * using the hardware default behavior based on the texture
24 * type. If you want the default behavior, you can still put
25 * "2" in the indirect texture state's output_type field.
27 V3D_CORE_WRITE(core, V3D_CTL_MISCCFG, V3D_MISCCFG_OVRTMUOUT);
29 /* Whenever we flush the L2T cache, we always want to flush
32 V3D_CORE_WRITE(core, V3D_CTL_L2TFLSTA, 0);
33 V3D_CORE_WRITE(core, V3D_CTL_L2TFLEND, ~0);
36 /* Sets invariant state for the HW. */
38 v3d_init_hw_state(struct v3d_dev *v3d)
40 v3d_init_core(v3d, 0);
44 v3d_idle_axi(struct v3d_dev *v3d, int core)
46 V3D_CORE_WRITE(core, V3D_GMP_CFG, V3D_GMP_CFG_STOP_REQ);
48 if (wait_for((V3D_CORE_READ(core, V3D_GMP_STATUS) &
49 (V3D_GMP_STATUS_RD_COUNT_MASK |
50 V3D_GMP_STATUS_WR_COUNT_MASK |
51 V3D_GMP_STATUS_CFG_BUSY)) == 0, 100)) {
52 DRM_ERROR("Failed to wait for safe GMP shutdown\n");
57 v3d_idle_gca(struct v3d_dev *v3d)
62 V3D_GCA_WRITE(V3D_GCA_SAFE_SHUTDOWN, V3D_GCA_SAFE_SHUTDOWN_EN);
64 if (wait_for((V3D_GCA_READ(V3D_GCA_SAFE_SHUTDOWN_ACK) &
65 V3D_GCA_SAFE_SHUTDOWN_ACK_ACKED) ==
66 V3D_GCA_SAFE_SHUTDOWN_ACK_ACKED, 100)) {
67 DRM_ERROR("Failed to wait for safe GCA shutdown\n");
72 v3d_reset_v3d(struct v3d_dev *v3d)
74 int version = V3D_BRIDGE_READ(V3D_TOP_GR_BRIDGE_REVISION);
76 if (V3D_GET_FIELD(version, V3D_TOP_GR_BRIDGE_MAJOR) == 2) {
77 V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_0,
78 V3D_TOP_GR_BRIDGE_SW_INIT_0_V3D_CLK_108_SW_INIT);
79 V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_0, 0);
81 /* GFXH-1383: The SW_INIT may cause a stray write to address 0
82 * of the unit, so reset it to its power-on value here.
84 V3D_WRITE(V3D_HUB_AXICFG, V3D_HUB_AXICFG_MAX_LEN_MASK);
86 WARN_ON_ONCE(V3D_GET_FIELD(version,
87 V3D_TOP_GR_BRIDGE_MAJOR) != 7);
88 V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_1,
89 V3D_TOP_GR_BRIDGE_SW_INIT_1_V3D_CLK_108_SW_INIT);
90 V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_1, 0);
93 v3d_init_hw_state(v3d);
97 v3d_reset(struct v3d_dev *v3d)
99 struct drm_device *dev = &v3d->drm;
101 DRM_ERROR("Resetting GPU.\n");
102 trace_v3d_reset_begin(dev);
104 /* XXX: only needed for safe powerdown, not reset. */
106 v3d_idle_axi(v3d, 0);
111 v3d_mmu_set_page_table(v3d);
114 trace_v3d_reset_end(dev);
118 v3d_flush_l3(struct v3d_dev *v3d)
121 u32 gca_ctrl = V3D_GCA_READ(V3D_GCA_CACHE_CTRL);
123 V3D_GCA_WRITE(V3D_GCA_CACHE_CTRL,
124 gca_ctrl | V3D_GCA_CACHE_CTRL_FLUSH);
127 V3D_GCA_WRITE(V3D_GCA_CACHE_CTRL,
128 gca_ctrl & ~V3D_GCA_CACHE_CTRL_FLUSH);
133 /* Invalidates the (read-only) L2C cache. This was the L2 cache for
134 * uniforms and instructions on V3D 3.2.
137 v3d_invalidate_l2c(struct v3d_dev *v3d, int core)
142 V3D_CORE_WRITE(core, V3D_CTL_L2CACTL,
147 /* Invalidates texture L2 cachelines */
149 v3d_flush_l2t(struct v3d_dev *v3d, int core)
151 /* While there is a busy bit (V3D_L2TCACTL_L2TFLS), we don't
152 * need to wait for completion before dispatching the job --
153 * L2T accesses will be stalled until the flush has completed.
155 V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL,
156 V3D_L2TCACTL_L2TFLS |
157 V3D_SET_FIELD(V3D_L2TCACTL_FLM_FLUSH, V3D_L2TCACTL_FLM));
160 /* Invalidates the slice caches. These are read-only caches. */
162 v3d_invalidate_slices(struct v3d_dev *v3d, int core)
164 V3D_CORE_WRITE(core, V3D_CTL_SLCACTL,
165 V3D_SET_FIELD(0xf, V3D_SLCACTL_TVCCS) |
166 V3D_SET_FIELD(0xf, V3D_SLCACTL_TDCCS) |
167 V3D_SET_FIELD(0xf, V3D_SLCACTL_UCC) |
168 V3D_SET_FIELD(0xf, V3D_SLCACTL_ICC));
172 v3d_invalidate_caches(struct v3d_dev *v3d)
174 /* Invalidate the caches from the outside in. That way if
175 * another CL's concurrent use of nearby memory were to pull
176 * an invalidated cacheline back in, we wouldn't leave stale
177 * data in the inner cache.
180 v3d_invalidate_l2c(v3d, 0);
181 v3d_flush_l2t(v3d, 0);
182 v3d_invalidate_slices(v3d, 0);
186 v3d_attach_object_fences(struct v3d_bo **bos, int bo_count,
187 struct dma_fence *fence)
191 for (i = 0; i < bo_count; i++) {
192 /* XXX: Use shared fences for read-only objects. */
193 reservation_object_add_excl_fence(bos[i]->resv, fence);
198 v3d_unlock_bo_reservations(struct v3d_bo **bos,
200 struct ww_acquire_ctx *acquire_ctx)
204 for (i = 0; i < bo_count; i++)
205 ww_mutex_unlock(&bos[i]->resv->lock);
207 ww_acquire_fini(acquire_ctx);
210 /* Takes the reservation lock on all the BOs being referenced, so that
211 * at queue submit time we can update the reservations.
213 * We don't lock the RCL the tile alloc/state BOs, or overflow memory
214 * (all of which are on exec->unref_list). They're entirely private
215 * to v3d, so we don't attach dma-buf fences to them.
218 v3d_lock_bo_reservations(struct v3d_bo **bos,
220 struct ww_acquire_ctx *acquire_ctx)
222 int contended_lock = -1;
225 ww_acquire_init(acquire_ctx, &reservation_ww_class);
228 if (contended_lock != -1) {
229 struct v3d_bo *bo = bos[contended_lock];
231 ret = ww_mutex_lock_slow_interruptible(&bo->resv->lock,
234 ww_acquire_done(acquire_ctx);
239 for (i = 0; i < bo_count; i++) {
240 if (i == contended_lock)
243 ret = ww_mutex_lock_interruptible(&bos[i]->resv->lock,
248 for (j = 0; j < i; j++)
249 ww_mutex_unlock(&bos[j]->resv->lock);
251 if (contended_lock != -1 && contended_lock >= i) {
252 struct v3d_bo *bo = bos[contended_lock];
254 ww_mutex_unlock(&bo->resv->lock);
257 if (ret == -EDEADLK) {
262 ww_acquire_done(acquire_ctx);
267 ww_acquire_done(acquire_ctx);
269 /* Reserve space for our shared (read-only) fence references,
270 * before we commit the CL to the hardware.
272 for (i = 0; i < bo_count; i++) {
273 ret = reservation_object_reserve_shared(bos[i]->resv, 1);
275 v3d_unlock_bo_reservations(bos, bo_count,
285 * v3d_cl_lookup_bos() - Sets up exec->bo[] with the GEM objects
286 * referenced by the job.
288 * @file_priv: DRM file for this fd
289 * @exec: V3D job being set up
291 * The command validator needs to reference BOs by their index within
292 * the submitted job's BO list. This does the validation of the job's
293 * BO list and reference counting for the lifetime of the job.
295 * Note that this function doesn't need to unreference the BOs on
296 * failure, because that will happen at v3d_exec_cleanup() time.
299 v3d_cl_lookup_bos(struct drm_device *dev,
300 struct drm_file *file_priv,
301 struct drm_v3d_submit_cl *args,
302 struct v3d_exec_info *exec)
308 exec->bo_count = args->bo_handle_count;
310 if (!exec->bo_count) {
311 /* See comment on bo_index for why we have to check
314 DRM_DEBUG("Rendering requires BOs\n");
318 exec->bo = kvmalloc_array(exec->bo_count,
319 sizeof(struct drm_gem_cma_object *),
320 GFP_KERNEL | __GFP_ZERO);
322 DRM_DEBUG("Failed to allocate validated BO pointers\n");
326 handles = kvmalloc_array(exec->bo_count, sizeof(u32), GFP_KERNEL);
329 DRM_DEBUG("Failed to allocate incoming GEM handles\n");
333 if (copy_from_user(handles,
334 (void __user *)(uintptr_t)args->bo_handles,
335 exec->bo_count * sizeof(u32))) {
337 DRM_DEBUG("Failed to copy in GEM handles\n");
341 spin_lock(&file_priv->table_lock);
342 for (i = 0; i < exec->bo_count; i++) {
343 struct drm_gem_object *bo = idr_find(&file_priv->object_idr,
346 DRM_DEBUG("Failed to look up GEM BO %d: %d\n",
349 spin_unlock(&file_priv->table_lock);
352 drm_gem_object_get(bo);
353 exec->bo[i] = to_v3d_bo(bo);
355 spin_unlock(&file_priv->table_lock);
363 v3d_exec_cleanup(struct kref *ref)
365 struct v3d_exec_info *exec = container_of(ref, struct v3d_exec_info,
367 struct v3d_dev *v3d = exec->v3d;
369 struct v3d_bo *bo, *save;
371 dma_fence_put(exec->bin.in_fence);
372 dma_fence_put(exec->render.in_fence);
374 dma_fence_put(exec->bin.done_fence);
375 dma_fence_put(exec->render.done_fence);
377 dma_fence_put(exec->bin_done_fence);
378 dma_fence_put(exec->render_done_fence);
380 for (i = 0; i < exec->bo_count; i++)
381 drm_gem_object_put_unlocked(&exec->bo[i]->base);
384 list_for_each_entry_safe(bo, save, &exec->unref_list, unref_head) {
385 drm_gem_object_put_unlocked(&bo->base);
388 pm_runtime_mark_last_busy(v3d->dev);
389 pm_runtime_put_autosuspend(v3d->dev);
394 void v3d_exec_put(struct v3d_exec_info *exec)
396 kref_put(&exec->refcount, v3d_exec_cleanup);
400 v3d_tfu_job_cleanup(struct kref *ref)
402 struct v3d_tfu_job *job = container_of(ref, struct v3d_tfu_job,
404 struct v3d_dev *v3d = job->v3d;
407 dma_fence_put(job->in_fence);
408 dma_fence_put(job->done_fence);
410 for (i = 0; i < ARRAY_SIZE(job->bo); i++) {
412 drm_gem_object_put_unlocked(&job->bo[i]->base);
415 pm_runtime_mark_last_busy(v3d->dev);
416 pm_runtime_put_autosuspend(v3d->dev);
421 void v3d_tfu_job_put(struct v3d_tfu_job *job)
423 kref_put(&job->refcount, v3d_tfu_job_cleanup);
427 v3d_wait_bo_ioctl(struct drm_device *dev, void *data,
428 struct drm_file *file_priv)
431 struct drm_v3d_wait_bo *args = data;
432 struct drm_gem_object *gem_obj;
434 ktime_t start = ktime_get();
436 unsigned long timeout_jiffies =
437 nsecs_to_jiffies_timeout(args->timeout_ns);
442 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
444 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
447 bo = to_v3d_bo(gem_obj);
449 ret = reservation_object_wait_timeout_rcu(bo->resv,
458 /* Decrement the user's timeout, in case we got interrupted
459 * such that the ioctl will be restarted.
461 delta_ns = ktime_to_ns(ktime_sub(ktime_get(), start));
462 if (delta_ns < args->timeout_ns)
463 args->timeout_ns -= delta_ns;
465 args->timeout_ns = 0;
467 /* Asked to wait beyond the jiffie/scheduler precision? */
468 if (ret == -ETIME && args->timeout_ns)
471 drm_gem_object_put_unlocked(gem_obj);
477 * v3d_submit_cl_ioctl() - Submits a job (frame) to the V3D.
479 * @data: ioctl argument
480 * @file_priv: DRM file for this fd
482 * This is the main entrypoint for userspace to submit a 3D frame to
483 * the GPU. Userspace provides the binner command list (if
484 * applicable), and the kernel sets up the render command list to draw
485 * to the framebuffer described in the ioctl, using the command lists
486 * that the 3D engine's binner will produce.
489 v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
490 struct drm_file *file_priv)
492 struct v3d_dev *v3d = to_v3d_dev(dev);
493 struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
494 struct drm_v3d_submit_cl *args = data;
495 struct v3d_exec_info *exec;
496 struct ww_acquire_ctx acquire_ctx;
497 struct drm_syncobj *sync_out;
500 trace_v3d_submit_cl_ioctl(&v3d->drm, args->rcl_start, args->rcl_end);
502 if (args->pad != 0) {
503 DRM_INFO("pad must be zero: %d\n", args->pad);
507 exec = kcalloc(1, sizeof(*exec), GFP_KERNEL);
511 ret = pm_runtime_get_sync(v3d->dev);
517 kref_init(&exec->refcount);
519 ret = drm_syncobj_find_fence(file_priv, args->in_sync_bcl,
520 0, 0, &exec->bin.in_fence);
524 ret = drm_syncobj_find_fence(file_priv, args->in_sync_rcl,
525 0, 0, &exec->render.in_fence);
529 exec->qma = args->qma;
530 exec->qms = args->qms;
531 exec->qts = args->qts;
532 exec->bin.exec = exec;
533 exec->bin.start = args->bcl_start;
534 exec->bin.end = args->bcl_end;
535 exec->render.exec = exec;
536 exec->render.start = args->rcl_start;
537 exec->render.end = args->rcl_end;
539 INIT_LIST_HEAD(&exec->unref_list);
541 ret = v3d_cl_lookup_bos(dev, file_priv, args, exec);
545 ret = v3d_lock_bo_reservations(exec->bo, exec->bo_count,
550 mutex_lock(&v3d->sched_lock);
551 if (exec->bin.start != exec->bin.end) {
552 ret = drm_sched_job_init(&exec->bin.base,
553 &v3d_priv->sched_entity[V3D_BIN],
558 exec->bin_done_fence =
559 dma_fence_get(&exec->bin.base.s_fence->finished);
561 kref_get(&exec->refcount); /* put by scheduler job completion */
562 drm_sched_entity_push_job(&exec->bin.base,
563 &v3d_priv->sched_entity[V3D_BIN]);
566 ret = drm_sched_job_init(&exec->render.base,
567 &v3d_priv->sched_entity[V3D_RENDER],
572 exec->render_done_fence =
573 dma_fence_get(&exec->render.base.s_fence->finished);
575 kref_get(&exec->refcount); /* put by scheduler job completion */
576 drm_sched_entity_push_job(&exec->render.base,
577 &v3d_priv->sched_entity[V3D_RENDER]);
578 mutex_unlock(&v3d->sched_lock);
580 v3d_attach_object_fences(exec->bo, exec->bo_count,
581 exec->render_done_fence);
583 v3d_unlock_bo_reservations(exec->bo, exec->bo_count, &acquire_ctx);
585 /* Update the return sync object for the */
586 sync_out = drm_syncobj_find(file_priv, args->out_sync);
588 drm_syncobj_replace_fence(sync_out, exec->render_done_fence);
589 drm_syncobj_put(sync_out);
597 mutex_unlock(&v3d->sched_lock);
598 v3d_unlock_bo_reservations(exec->bo, exec->bo_count, &acquire_ctx);
606 * v3d_submit_tfu_ioctl() - Submits a TFU (texture formatting) job to the V3D.
608 * @data: ioctl argument
609 * @file_priv: DRM file for this fd
611 * Userspace provides the register setup for the TFU, which we don't
612 * need to validate since the TFU is behind the MMU.
615 v3d_submit_tfu_ioctl(struct drm_device *dev, void *data,
616 struct drm_file *file_priv)
618 struct v3d_dev *v3d = to_v3d_dev(dev);
619 struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
620 struct drm_v3d_submit_tfu *args = data;
621 struct v3d_tfu_job *job;
622 struct ww_acquire_ctx acquire_ctx;
623 struct drm_syncobj *sync_out;
624 struct dma_fence *sched_done_fence;
628 trace_v3d_submit_tfu_ioctl(&v3d->drm, args->iia);
630 job = kcalloc(1, sizeof(*job), GFP_KERNEL);
634 ret = pm_runtime_get_sync(v3d->dev);
640 kref_init(&job->refcount);
642 ret = drm_syncobj_find_fence(file_priv, args->in_sync,
643 0, 0, &job->in_fence);
650 spin_lock(&file_priv->table_lock);
651 for (bo_count = 0; bo_count < ARRAY_SIZE(job->bo); bo_count++) {
652 struct drm_gem_object *bo;
654 if (!args->bo_handles[bo_count])
657 bo = idr_find(&file_priv->object_idr,
658 args->bo_handles[bo_count]);
660 DRM_DEBUG("Failed to look up GEM BO %d: %d\n",
661 bo_count, args->bo_handles[bo_count]);
663 spin_unlock(&file_priv->table_lock);
666 drm_gem_object_get(bo);
667 job->bo[bo_count] = to_v3d_bo(bo);
669 spin_unlock(&file_priv->table_lock);
671 ret = v3d_lock_bo_reservations(job->bo, bo_count, &acquire_ctx);
675 mutex_lock(&v3d->sched_lock);
676 ret = drm_sched_job_init(&job->base,
677 &v3d_priv->sched_entity[V3D_TFU],
682 sched_done_fence = dma_fence_get(&job->base.s_fence->finished);
684 kref_get(&job->refcount); /* put by scheduler job completion */
685 drm_sched_entity_push_job(&job->base, &v3d_priv->sched_entity[V3D_TFU]);
686 mutex_unlock(&v3d->sched_lock);
688 v3d_attach_object_fences(job->bo, bo_count, sched_done_fence);
690 v3d_unlock_bo_reservations(job->bo, bo_count, &acquire_ctx);
692 /* Update the return sync object */
693 sync_out = drm_syncobj_find(file_priv, args->out_sync);
695 drm_syncobj_replace_fence(sync_out, sched_done_fence);
696 drm_syncobj_put(sync_out);
698 dma_fence_put(sched_done_fence);
700 v3d_tfu_job_put(job);
705 mutex_unlock(&v3d->sched_lock);
706 v3d_unlock_bo_reservations(job->bo, bo_count, &acquire_ctx);
708 v3d_tfu_job_put(job);
714 v3d_gem_init(struct drm_device *dev)
716 struct v3d_dev *v3d = to_v3d_dev(dev);
717 u32 pt_size = 4096 * 1024;
720 for (i = 0; i < V3D_MAX_QUEUES; i++)
721 v3d->queue[i].fence_context = dma_fence_context_alloc(1);
723 spin_lock_init(&v3d->mm_lock);
724 spin_lock_init(&v3d->job_lock);
725 mutex_init(&v3d->bo_lock);
726 mutex_init(&v3d->reset_lock);
727 mutex_init(&v3d->sched_lock);
729 /* Note: We don't allocate address 0. Various bits of HW
730 * treat 0 as special, such as the occlusion query counters
731 * where 0 means "disabled".
733 drm_mm_init(&v3d->mm, 1, pt_size / sizeof(u32) - 1);
735 v3d->pt = dma_alloc_wc(v3d->dev, pt_size,
737 GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);
739 drm_mm_takedown(&v3d->mm);
741 "Failed to allocate page tables. "
742 "Please ensure you have CMA enabled.\n");
746 v3d_init_hw_state(v3d);
747 v3d_mmu_set_page_table(v3d);
749 ret = v3d_sched_init(v3d);
751 drm_mm_takedown(&v3d->mm);
752 dma_free_coherent(v3d->dev, 4096 * 1024, (void *)v3d->pt,
760 v3d_gem_destroy(struct drm_device *dev)
762 struct v3d_dev *v3d = to_v3d_dev(dev);
766 /* Waiting for exec to finish would need to be done before
769 WARN_ON(v3d->bin_job);
770 WARN_ON(v3d->render_job);
772 drm_mm_takedown(&v3d->mm);
774 dma_free_coherent(v3d->dev, 4096 * 1024, (void *)v3d->pt, v3d->pt_paddr);