xref: /openbmc/linux/drivers/gpu/drm/v3d/v3d_gem.c (revision bb3425efdcd99f2b4e608e850226f7107b2f993e)
157692c94SEric Anholt // SPDX-License-Identifier: GPL-2.0+
257692c94SEric Anholt /* Copyright (C) 2014-2018 Broadcom */
357692c94SEric Anholt 
4220989e7SSam Ravnborg #include <linux/device.h>
5220989e7SSam Ravnborg #include <linux/dma-mapping.h>
6220989e7SSam Ravnborg #include <linux/io.h>
757692c94SEric Anholt #include <linux/module.h>
857692c94SEric Anholt #include <linux/platform_device.h>
957692c94SEric Anholt #include <linux/pm_runtime.h>
10eea9b97bSEric Anholt #include <linux/reset.h>
1157692c94SEric Anholt #include <linux/sched/signal.h>
12220989e7SSam Ravnborg #include <linux/uaccess.h>
1357692c94SEric Anholt 
14220989e7SSam Ravnborg #include <drm/drm_syncobj.h>
15220989e7SSam Ravnborg #include <uapi/drm/v3d_drm.h>
16220989e7SSam Ravnborg 
1757692c94SEric Anholt #include "v3d_drv.h"
1857692c94SEric Anholt #include "v3d_regs.h"
1957692c94SEric Anholt #include "v3d_trace.h"
2057692c94SEric Anholt 
2157692c94SEric Anholt static void
2257692c94SEric Anholt v3d_init_core(struct v3d_dev *v3d, int core)
2357692c94SEric Anholt {
2457692c94SEric Anholt 	/* Set OVRTMUOUT, which means that the texture sampler uniform
2557692c94SEric Anholt 	 * configuration's tmu output type field is used, instead of
2657692c94SEric Anholt 	 * using the hardware default behavior based on the texture
2757692c94SEric Anholt 	 * type.  If you want the default behavior, you can still put
2857692c94SEric Anholt 	 * "2" in the indirect texture state's output_type field.
2957692c94SEric Anholt 	 */
30a7dde1b7SEric Anholt 	if (v3d->ver < 40)
3157692c94SEric Anholt 		V3D_CORE_WRITE(core, V3D_CTL_MISCCFG, V3D_MISCCFG_OVRTMUOUT);
3257692c94SEric Anholt 
3357692c94SEric Anholt 	/* Whenever we flush the L2T cache, we always want to flush
3457692c94SEric Anholt 	 * the whole thing.
3557692c94SEric Anholt 	 */
3657692c94SEric Anholt 	V3D_CORE_WRITE(core, V3D_CTL_L2TFLSTA, 0);
3757692c94SEric Anholt 	V3D_CORE_WRITE(core, V3D_CTL_L2TFLEND, ~0);
3857692c94SEric Anholt }
3957692c94SEric Anholt 
4057692c94SEric Anholt /* Sets invariant state for the HW. */
4157692c94SEric Anholt static void
4257692c94SEric Anholt v3d_init_hw_state(struct v3d_dev *v3d)
4357692c94SEric Anholt {
4457692c94SEric Anholt 	v3d_init_core(v3d, 0);
4557692c94SEric Anholt }
4657692c94SEric Anholt 
4757692c94SEric Anholt static void
4857692c94SEric Anholt v3d_idle_axi(struct v3d_dev *v3d, int core)
4957692c94SEric Anholt {
5057692c94SEric Anholt 	V3D_CORE_WRITE(core, V3D_GMP_CFG, V3D_GMP_CFG_STOP_REQ);
5157692c94SEric Anholt 
5257692c94SEric Anholt 	if (wait_for((V3D_CORE_READ(core, V3D_GMP_STATUS) &
5357692c94SEric Anholt 		      (V3D_GMP_STATUS_RD_COUNT_MASK |
5457692c94SEric Anholt 		       V3D_GMP_STATUS_WR_COUNT_MASK |
5557692c94SEric Anholt 		       V3D_GMP_STATUS_CFG_BUSY)) == 0, 100)) {
5657692c94SEric Anholt 		DRM_ERROR("Failed to wait for safe GMP shutdown\n");
5757692c94SEric Anholt 	}
5857692c94SEric Anholt }
5957692c94SEric Anholt 
6057692c94SEric Anholt static void
6157692c94SEric Anholt v3d_idle_gca(struct v3d_dev *v3d)
6257692c94SEric Anholt {
6357692c94SEric Anholt 	if (v3d->ver >= 41)
6457692c94SEric Anholt 		return;
6557692c94SEric Anholt 
6657692c94SEric Anholt 	V3D_GCA_WRITE(V3D_GCA_SAFE_SHUTDOWN, V3D_GCA_SAFE_SHUTDOWN_EN);
6757692c94SEric Anholt 
6857692c94SEric Anholt 	if (wait_for((V3D_GCA_READ(V3D_GCA_SAFE_SHUTDOWN_ACK) &
6957692c94SEric Anholt 		      V3D_GCA_SAFE_SHUTDOWN_ACK_ACKED) ==
7057692c94SEric Anholt 		     V3D_GCA_SAFE_SHUTDOWN_ACK_ACKED, 100)) {
7157692c94SEric Anholt 		DRM_ERROR("Failed to wait for safe GCA shutdown\n");
7257692c94SEric Anholt 	}
7357692c94SEric Anholt }
7457692c94SEric Anholt 
7557692c94SEric Anholt static void
76eea9b97bSEric Anholt v3d_reset_by_bridge(struct v3d_dev *v3d)
7757692c94SEric Anholt {
7857692c94SEric Anholt 	int version = V3D_BRIDGE_READ(V3D_TOP_GR_BRIDGE_REVISION);
7957692c94SEric Anholt 
8057692c94SEric Anholt 	if (V3D_GET_FIELD(version, V3D_TOP_GR_BRIDGE_MAJOR) == 2) {
8157692c94SEric Anholt 		V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_0,
8257692c94SEric Anholt 				 V3D_TOP_GR_BRIDGE_SW_INIT_0_V3D_CLK_108_SW_INIT);
8357692c94SEric Anholt 		V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_0, 0);
8457692c94SEric Anholt 
8557692c94SEric Anholt 		/* GFXH-1383: The SW_INIT may cause a stray write to address 0
8657692c94SEric Anholt 		 * of the unit, so reset it to its power-on value here.
8757692c94SEric Anholt 		 */
8857692c94SEric Anholt 		V3D_WRITE(V3D_HUB_AXICFG, V3D_HUB_AXICFG_MAX_LEN_MASK);
8957692c94SEric Anholt 	} else {
9057692c94SEric Anholt 		WARN_ON_ONCE(V3D_GET_FIELD(version,
9157692c94SEric Anholt 					   V3D_TOP_GR_BRIDGE_MAJOR) != 7);
9257692c94SEric Anholt 		V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_1,
9357692c94SEric Anholt 				 V3D_TOP_GR_BRIDGE_SW_INIT_1_V3D_CLK_108_SW_INIT);
9457692c94SEric Anholt 		V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_1, 0);
9557692c94SEric Anholt 	}
96eea9b97bSEric Anholt }
97eea9b97bSEric Anholt 
98eea9b97bSEric Anholt static void
99eea9b97bSEric Anholt v3d_reset_v3d(struct v3d_dev *v3d)
100eea9b97bSEric Anholt {
101eea9b97bSEric Anholt 	if (v3d->reset)
102eea9b97bSEric Anholt 		reset_control_reset(v3d->reset);
103eea9b97bSEric Anholt 	else
104eea9b97bSEric Anholt 		v3d_reset_by_bridge(v3d);
10557692c94SEric Anholt 
10657692c94SEric Anholt 	v3d_init_hw_state(v3d);
10757692c94SEric Anholt }
10857692c94SEric Anholt 
10957692c94SEric Anholt void
11057692c94SEric Anholt v3d_reset(struct v3d_dev *v3d)
11157692c94SEric Anholt {
11257692c94SEric Anholt 	struct drm_device *dev = &v3d->drm;
11357692c94SEric Anholt 
1141ba9d7cbSEric Anholt 	DRM_DEV_ERROR(dev->dev, "Resetting GPU for hang.\n");
1151ba9d7cbSEric Anholt 	DRM_DEV_ERROR(dev->dev, "V3D_ERR_STAT: 0x%08x\n",
1161ba9d7cbSEric Anholt 		      V3D_CORE_READ(0, V3D_ERR_STAT));
11757692c94SEric Anholt 	trace_v3d_reset_begin(dev);
11857692c94SEric Anholt 
11957692c94SEric Anholt 	/* XXX: only needed for safe powerdown, not reset. */
12057692c94SEric Anholt 	if (false)
12157692c94SEric Anholt 		v3d_idle_axi(v3d, 0);
12257692c94SEric Anholt 
12357692c94SEric Anholt 	v3d_idle_gca(v3d);
12457692c94SEric Anholt 	v3d_reset_v3d(v3d);
12557692c94SEric Anholt 
12657692c94SEric Anholt 	v3d_mmu_set_page_table(v3d);
12757692c94SEric Anholt 	v3d_irq_reset(v3d);
12857692c94SEric Anholt 
12926a4dc29SJuan A. Suarez Romero 	v3d_perfmon_stop(v3d, v3d->active_perfmon, false);
13026a4dc29SJuan A. Suarez Romero 
13157692c94SEric Anholt 	trace_v3d_reset_end(dev);
13257692c94SEric Anholt }
13357692c94SEric Anholt 
13457692c94SEric Anholt static void
13557692c94SEric Anholt v3d_flush_l3(struct v3d_dev *v3d)
13657692c94SEric Anholt {
13757692c94SEric Anholt 	if (v3d->ver < 41) {
13857692c94SEric Anholt 		u32 gca_ctrl = V3D_GCA_READ(V3D_GCA_CACHE_CTRL);
13957692c94SEric Anholt 
14057692c94SEric Anholt 		V3D_GCA_WRITE(V3D_GCA_CACHE_CTRL,
14157692c94SEric Anholt 			      gca_ctrl | V3D_GCA_CACHE_CTRL_FLUSH);
14257692c94SEric Anholt 
14357692c94SEric Anholt 		if (v3d->ver < 33) {
14457692c94SEric Anholt 			V3D_GCA_WRITE(V3D_GCA_CACHE_CTRL,
14557692c94SEric Anholt 				      gca_ctrl & ~V3D_GCA_CACHE_CTRL_FLUSH);
14657692c94SEric Anholt 		}
14757692c94SEric Anholt 	}
14857692c94SEric Anholt }
14957692c94SEric Anholt 
1507b9d2fe4SEric Anholt /* Invalidates the (read-only) L2C cache.  This was the L2 cache for
1517b9d2fe4SEric Anholt  * uniforms and instructions on V3D 3.2.
1527b9d2fe4SEric Anholt  */
15357692c94SEric Anholt static void
1547b9d2fe4SEric Anholt v3d_invalidate_l2c(struct v3d_dev *v3d, int core)
15557692c94SEric Anholt {
1567b9d2fe4SEric Anholt 	if (v3d->ver > 32)
1577b9d2fe4SEric Anholt 		return;
1587b9d2fe4SEric Anholt 
15957692c94SEric Anholt 	V3D_CORE_WRITE(core, V3D_CTL_L2CACTL,
16057692c94SEric Anholt 		       V3D_L2CACTL_L2CCLR |
16157692c94SEric Anholt 		       V3D_L2CACTL_L2CENA);
16257692c94SEric Anholt }
16357692c94SEric Anholt 
16457692c94SEric Anholt /* Invalidates texture L2 cachelines */
16557692c94SEric Anholt static void
16657692c94SEric Anholt v3d_flush_l2t(struct v3d_dev *v3d, int core)
16757692c94SEric Anholt {
16851c1b6f9SEric Anholt 	/* While there is a busy bit (V3D_L2TCACTL_L2TFLS), we don't
16951c1b6f9SEric Anholt 	 * need to wait for completion before dispatching the job --
17051c1b6f9SEric Anholt 	 * L2T accesses will be stalled until the flush has completed.
171d223f98fSEric Anholt 	 * However, we do need to make sure we don't try to trigger a
172d223f98fSEric Anholt 	 * new flush while the L2_CLEAN queue is trying to
173d223f98fSEric Anholt 	 * synchronously clean after a job.
17451c1b6f9SEric Anholt 	 */
175d223f98fSEric Anholt 	mutex_lock(&v3d->cache_clean_lock);
17657692c94SEric Anholt 	V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL,
17757692c94SEric Anholt 		       V3D_L2TCACTL_L2TFLS |
17857692c94SEric Anholt 		       V3D_SET_FIELD(V3D_L2TCACTL_FLM_FLUSH, V3D_L2TCACTL_FLM));
179d223f98fSEric Anholt 	mutex_unlock(&v3d->cache_clean_lock);
180d223f98fSEric Anholt }
181d223f98fSEric Anholt 
182d223f98fSEric Anholt /* Cleans texture L1 and L2 cachelines (writing back dirty data).
183d223f98fSEric Anholt  *
184d223f98fSEric Anholt  * For cleaning, which happens from the CACHE_CLEAN queue after CSD has
185d223f98fSEric Anholt  * executed, we need to make sure that the clean is done before
186d223f98fSEric Anholt  * signaling job completion.  So, we synchronously wait before
187d223f98fSEric Anholt  * returning, and we make sure that L2 invalidates don't happen in the
188d223f98fSEric Anholt  * meantime to confuse our are-we-done checks.
189d223f98fSEric Anholt  */
190d223f98fSEric Anholt void
191d223f98fSEric Anholt v3d_clean_caches(struct v3d_dev *v3d)
192d223f98fSEric Anholt {
193d223f98fSEric Anholt 	struct drm_device *dev = &v3d->drm;
194d223f98fSEric Anholt 	int core = 0;
195d223f98fSEric Anholt 
196d223f98fSEric Anholt 	trace_v3d_cache_clean_begin(dev);
197d223f98fSEric Anholt 
198d223f98fSEric Anholt 	V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL, V3D_L2TCACTL_TMUWCF);
199d223f98fSEric Anholt 	if (wait_for(!(V3D_CORE_READ(core, V3D_CTL_L2TCACTL) &
200e4f86819SIago Toral Quiroga 		       V3D_L2TCACTL_TMUWCF), 100)) {
201e4f86819SIago Toral Quiroga 		DRM_ERROR("Timeout waiting for TMU write combiner flush\n");
202d223f98fSEric Anholt 	}
203d223f98fSEric Anholt 
204d223f98fSEric Anholt 	mutex_lock(&v3d->cache_clean_lock);
205d223f98fSEric Anholt 	V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL,
206d223f98fSEric Anholt 		       V3D_L2TCACTL_L2TFLS |
207d223f98fSEric Anholt 		       V3D_SET_FIELD(V3D_L2TCACTL_FLM_CLEAN, V3D_L2TCACTL_FLM));
208d223f98fSEric Anholt 
209d223f98fSEric Anholt 	if (wait_for(!(V3D_CORE_READ(core, V3D_CTL_L2TCACTL) &
210d223f98fSEric Anholt 		       V3D_L2TCACTL_L2TFLS), 100)) {
211d223f98fSEric Anholt 		DRM_ERROR("Timeout waiting for L2T clean\n");
212d223f98fSEric Anholt 	}
213d223f98fSEric Anholt 
214d223f98fSEric Anholt 	mutex_unlock(&v3d->cache_clean_lock);
215d223f98fSEric Anholt 
216d223f98fSEric Anholt 	trace_v3d_cache_clean_end(dev);
21757692c94SEric Anholt }
21857692c94SEric Anholt 
21957692c94SEric Anholt /* Invalidates the slice caches.  These are read-only caches. */
22057692c94SEric Anholt static void
22157692c94SEric Anholt v3d_invalidate_slices(struct v3d_dev *v3d, int core)
22257692c94SEric Anholt {
22357692c94SEric Anholt 	V3D_CORE_WRITE(core, V3D_CTL_SLCACTL,
22457692c94SEric Anholt 		       V3D_SET_FIELD(0xf, V3D_SLCACTL_TVCCS) |
22557692c94SEric Anholt 		       V3D_SET_FIELD(0xf, V3D_SLCACTL_TDCCS) |
22657692c94SEric Anholt 		       V3D_SET_FIELD(0xf, V3D_SLCACTL_UCC) |
22757692c94SEric Anholt 		       V3D_SET_FIELD(0xf, V3D_SLCACTL_ICC));
22857692c94SEric Anholt }
22957692c94SEric Anholt 
23057692c94SEric Anholt void
23157692c94SEric Anholt v3d_invalidate_caches(struct v3d_dev *v3d)
23257692c94SEric Anholt {
233aa5beec3SEric Anholt 	/* Invalidate the caches from the outside in.  That way if
234aa5beec3SEric Anholt 	 * another CL's concurrent use of nearby memory were to pull
235aa5beec3SEric Anholt 	 * an invalidated cacheline back in, we wouldn't leave stale
236aa5beec3SEric Anholt 	 * data in the inner cache.
237aa5beec3SEric Anholt 	 */
23857692c94SEric Anholt 	v3d_flush_l3(v3d);
2397b9d2fe4SEric Anholt 	v3d_invalidate_l2c(v3d, 0);
24057692c94SEric Anholt 	v3d_flush_l2t(v3d, 0);
241aa5beec3SEric Anholt 	v3d_invalidate_slices(v3d, 0);
24257692c94SEric Anholt }
24357692c94SEric Anholt 
24457692c94SEric Anholt /* Takes the reservation lock on all the BOs being referenced, so that
24557692c94SEric Anholt  * at queue submit time we can update the reservations.
24657692c94SEric Anholt  *
24757692c94SEric Anholt  * We don't lock the RCL the tile alloc/state BOs, or overflow memory
24857692c94SEric Anholt  * (all of which are on exec->unref_list).  They're entirely private
24957692c94SEric Anholt  * to v3d, so we don't attach dma-buf fences to them.
25057692c94SEric Anholt  */
25157692c94SEric Anholt static int
252dffa9b7aSEric Anholt v3d_lock_bo_reservations(struct v3d_job *job,
25357692c94SEric Anholt 			 struct ww_acquire_ctx *acquire_ctx)
25457692c94SEric Anholt {
25557692c94SEric Anholt 	int i, ret;
25657692c94SEric Anholt 
257dffa9b7aSEric Anholt 	ret = drm_gem_lock_reservations(job->bo, job->bo_count, acquire_ctx);
258c2b3e61aSEric Anholt 	if (ret)
25957692c94SEric Anholt 		return ret;
26057692c94SEric Anholt 
261dffa9b7aSEric Anholt 	for (i = 0; i < job->bo_count; i++) {
262da3208e8SDaniel Vetter 		ret = drm_sched_job_add_implicit_dependencies(&job->base,
263dffa9b7aSEric Anholt 							      job->bo[i], true);
264dffa9b7aSEric Anholt 		if (ret) {
265dffa9b7aSEric Anholt 			drm_gem_unlock_reservations(job->bo, job->bo_count,
266dffa9b7aSEric Anholt 						    acquire_ctx);
267dffa9b7aSEric Anholt 			return ret;
268dffa9b7aSEric Anholt 		}
269dffa9b7aSEric Anholt 	}
270dffa9b7aSEric Anholt 
27157692c94SEric Anholt 	return 0;
27257692c94SEric Anholt }
27357692c94SEric Anholt 
27457692c94SEric Anholt /**
275a783a09eSEric Anholt  * v3d_lookup_bos() - Sets up job->bo[] with the GEM objects
27657692c94SEric Anholt  * referenced by the job.
27757692c94SEric Anholt  * @dev: DRM device
27857692c94SEric Anholt  * @file_priv: DRM file for this fd
279a783a09eSEric Anholt  * @job: V3D job being set up
280e226878eSLee Jones  * @bo_handles: GEM handles
281e226878eSLee Jones  * @bo_count: Number of GEM handles passed in
28257692c94SEric Anholt  *
28357692c94SEric Anholt  * The command validator needs to reference BOs by their index within
28457692c94SEric Anholt  * the submitted job's BO list.  This does the validation of the job's
28557692c94SEric Anholt  * BO list and reference counting for the lifetime of the job.
28657692c94SEric Anholt  *
28757692c94SEric Anholt  * Note that this function doesn't need to unreference the BOs on
28857692c94SEric Anholt  * failure, because that will happen at v3d_exec_cleanup() time.
28957692c94SEric Anholt  */
29057692c94SEric Anholt static int
291a783a09eSEric Anholt v3d_lookup_bos(struct drm_device *dev,
29257692c94SEric Anholt 	       struct drm_file *file_priv,
293a783a09eSEric Anholt 	       struct v3d_job *job,
294a783a09eSEric Anholt 	       u64 bo_handles,
295a783a09eSEric Anholt 	       u32 bo_count)
29657692c94SEric Anholt {
29757692c94SEric Anholt 	u32 *handles;
29857692c94SEric Anholt 	int ret = 0;
29957692c94SEric Anholt 	int i;
30057692c94SEric Anholt 
301a783a09eSEric Anholt 	job->bo_count = bo_count;
30257692c94SEric Anholt 
303a783a09eSEric Anholt 	if (!job->bo_count) {
30457692c94SEric Anholt 		/* See comment on bo_index for why we have to check
30557692c94SEric Anholt 		 * this.
30657692c94SEric Anholt 		 */
30757692c94SEric Anholt 		DRM_DEBUG("Rendering requires BOs\n");
30857692c94SEric Anholt 		return -EINVAL;
30957692c94SEric Anholt 	}
31057692c94SEric Anholt 
311a783a09eSEric Anholt 	job->bo = kvmalloc_array(job->bo_count,
31257692c94SEric Anholt 				 sizeof(struct drm_gem_cma_object *),
31357692c94SEric Anholt 				 GFP_KERNEL | __GFP_ZERO);
314a783a09eSEric Anholt 	if (!job->bo) {
31557692c94SEric Anholt 		DRM_DEBUG("Failed to allocate validated BO pointers\n");
31657692c94SEric Anholt 		return -ENOMEM;
31757692c94SEric Anholt 	}
31857692c94SEric Anholt 
319a783a09eSEric Anholt 	handles = kvmalloc_array(job->bo_count, sizeof(u32), GFP_KERNEL);
32057692c94SEric Anholt 	if (!handles) {
32157692c94SEric Anholt 		ret = -ENOMEM;
32257692c94SEric Anholt 		DRM_DEBUG("Failed to allocate incoming GEM handles\n");
32357692c94SEric Anholt 		goto fail;
32457692c94SEric Anholt 	}
32557692c94SEric Anholt 
32657692c94SEric Anholt 	if (copy_from_user(handles,
327a783a09eSEric Anholt 			   (void __user *)(uintptr_t)bo_handles,
328a783a09eSEric Anholt 			   job->bo_count * sizeof(u32))) {
32957692c94SEric Anholt 		ret = -EFAULT;
33057692c94SEric Anholt 		DRM_DEBUG("Failed to copy in GEM handles\n");
33157692c94SEric Anholt 		goto fail;
33257692c94SEric Anholt 	}
33357692c94SEric Anholt 
33457692c94SEric Anholt 	spin_lock(&file_priv->table_lock);
335a783a09eSEric Anholt 	for (i = 0; i < job->bo_count; i++) {
33657692c94SEric Anholt 		struct drm_gem_object *bo = idr_find(&file_priv->object_idr,
33757692c94SEric Anholt 						     handles[i]);
33857692c94SEric Anholt 		if (!bo) {
33957692c94SEric Anholt 			DRM_DEBUG("Failed to look up GEM BO %d: %d\n",
34057692c94SEric Anholt 				  i, handles[i]);
34157692c94SEric Anholt 			ret = -ENOENT;
34257692c94SEric Anholt 			spin_unlock(&file_priv->table_lock);
34357692c94SEric Anholt 			goto fail;
34457692c94SEric Anholt 		}
34557692c94SEric Anholt 		drm_gem_object_get(bo);
346a783a09eSEric Anholt 		job->bo[i] = bo;
34757692c94SEric Anholt 	}
34857692c94SEric Anholt 	spin_unlock(&file_priv->table_lock);
34957692c94SEric Anholt 
35057692c94SEric Anholt fail:
35157692c94SEric Anholt 	kvfree(handles);
35257692c94SEric Anholt 	return ret;
35357692c94SEric Anholt }
35457692c94SEric Anholt 
35557692c94SEric Anholt static void
356a783a09eSEric Anholt v3d_job_free(struct kref *ref)
35757692c94SEric Anholt {
358a783a09eSEric Anholt 	struct v3d_job *job = container_of(ref, struct v3d_job, refcount);
359a783a09eSEric Anholt 	int i;
36057692c94SEric Anholt 
361a783a09eSEric Anholt 	for (i = 0; i < job->bo_count; i++) {
3621584f16cSEric Anholt 		if (job->bo[i])
3632b86189eSEmil Velikov 			drm_gem_object_put(job->bo[i]);
3641584f16cSEric Anholt 	}
365a783a09eSEric Anholt 	kvfree(job->bo);
3661584f16cSEric Anholt 
367a783a09eSEric Anholt 	dma_fence_put(job->irq_fence);
368a783a09eSEric Anholt 	dma_fence_put(job->done_fence);
369a783a09eSEric Anholt 
370bc662528SDaniel Vetter 	pm_runtime_mark_last_busy(job->v3d->drm.dev);
371bc662528SDaniel Vetter 	pm_runtime_put_autosuspend(job->v3d->drm.dev);
3721584f16cSEric Anholt 
37326a4dc29SJuan A. Suarez Romero 	if (job->perfmon)
37426a4dc29SJuan A. Suarez Romero 		v3d_perfmon_put(job->perfmon);
37526a4dc29SJuan A. Suarez Romero 
3761584f16cSEric Anholt 	kfree(job);
3771584f16cSEric Anholt }
3781584f16cSEric Anholt 
379a783a09eSEric Anholt static void
380a783a09eSEric Anholt v3d_render_job_free(struct kref *ref)
3811584f16cSEric Anholt {
382a783a09eSEric Anholt 	struct v3d_render_job *job = container_of(ref, struct v3d_render_job,
383a783a09eSEric Anholt 						  base.refcount);
384a783a09eSEric Anholt 	struct v3d_bo *bo, *save;
385a783a09eSEric Anholt 
386a783a09eSEric Anholt 	list_for_each_entry_safe(bo, save, &job->unref_list, unref_head) {
3872b86189eSEmil Velikov 		drm_gem_object_put(&bo->base.base);
388a783a09eSEric Anholt 	}
389a783a09eSEric Anholt 
390a783a09eSEric Anholt 	v3d_job_free(ref);
391a783a09eSEric Anholt }
392a783a09eSEric Anholt 
393916044faSDaniel Vetter void v3d_job_cleanup(struct v3d_job *job)
394916044faSDaniel Vetter {
39507c2a416SMelissa Wen 	if (!job)
39607c2a416SMelissa Wen 		return;
39707c2a416SMelissa Wen 
398916044faSDaniel Vetter 	drm_sched_job_cleanup(&job->base);
399916044faSDaniel Vetter 	v3d_job_put(job);
400916044faSDaniel Vetter }
401916044faSDaniel Vetter 
402a783a09eSEric Anholt void v3d_job_put(struct v3d_job *job)
403a783a09eSEric Anholt {
404a783a09eSEric Anholt 	kref_put(&job->refcount, job->free);
4051584f16cSEric Anholt }
4061584f16cSEric Anholt 
40757692c94SEric Anholt int
40857692c94SEric Anholt v3d_wait_bo_ioctl(struct drm_device *dev, void *data,
40957692c94SEric Anholt 		  struct drm_file *file_priv)
41057692c94SEric Anholt {
41157692c94SEric Anholt 	int ret;
41257692c94SEric Anholt 	struct drm_v3d_wait_bo *args = data;
41357692c94SEric Anholt 	ktime_t start = ktime_get();
41457692c94SEric Anholt 	u64 delta_ns;
41557692c94SEric Anholt 	unsigned long timeout_jiffies =
41657692c94SEric Anholt 		nsecs_to_jiffies_timeout(args->timeout_ns);
41757692c94SEric Anholt 
41857692c94SEric Anholt 	if (args->pad != 0)
41957692c94SEric Anholt 		return -EINVAL;
42057692c94SEric Anholt 
42152791eeeSChristian König 	ret = drm_gem_dma_resv_wait(file_priv, args->handle,
4228d668309SRob Herring 				    true, timeout_jiffies);
42357692c94SEric Anholt 
42457692c94SEric Anholt 	/* Decrement the user's timeout, in case we got interrupted
42557692c94SEric Anholt 	 * such that the ioctl will be restarted.
42657692c94SEric Anholt 	 */
42757692c94SEric Anholt 	delta_ns = ktime_to_ns(ktime_sub(ktime_get(), start));
42857692c94SEric Anholt 	if (delta_ns < args->timeout_ns)
42957692c94SEric Anholt 		args->timeout_ns -= delta_ns;
43057692c94SEric Anholt 	else
43157692c94SEric Anholt 		args->timeout_ns = 0;
43257692c94SEric Anholt 
43357692c94SEric Anholt 	/* Asked to wait beyond the jiffie/scheduler precision? */
43457692c94SEric Anholt 	if (ret == -ETIME && args->timeout_ns)
43557692c94SEric Anholt 		ret = -EAGAIN;
43657692c94SEric Anholt 
43757692c94SEric Anholt 	return ret;
43857692c94SEric Anholt }
43957692c94SEric Anholt 
440a783a09eSEric Anholt static int
441223583ddSMelissa Wen v3d_job_add_deps(struct drm_file *file_priv, struct v3d_job *job,
442223583ddSMelissa Wen 		 u32 in_sync, u32 point)
443223583ddSMelissa Wen {
444223583ddSMelissa Wen 	struct dma_fence *in_fence = NULL;
445223583ddSMelissa Wen 	int ret;
446223583ddSMelissa Wen 
447223583ddSMelissa Wen 	ret = drm_syncobj_find_fence(file_priv, in_sync, point, 0, &in_fence);
448223583ddSMelissa Wen 	if (ret == -EINVAL)
449223583ddSMelissa Wen 		return ret;
450223583ddSMelissa Wen 
451223583ddSMelissa Wen 	return drm_sched_job_add_dependency(&job->base, in_fence);
452223583ddSMelissa Wen }
453223583ddSMelissa Wen 
454223583ddSMelissa Wen static int
455a783a09eSEric Anholt v3d_job_init(struct v3d_dev *v3d, struct drm_file *file_priv,
45607c2a416SMelissa Wen 	     void **container, size_t size, void (*free)(struct kref *ref),
457916044faSDaniel Vetter 	     u32 in_sync, enum v3d_queue queue)
458a783a09eSEric Anholt {
459916044faSDaniel Vetter 	struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
46007c2a416SMelissa Wen 	struct v3d_job *job;
461a783a09eSEric Anholt 	int ret;
462a783a09eSEric Anholt 
46307c2a416SMelissa Wen 	*container = kcalloc(1, size, GFP_KERNEL);
46407c2a416SMelissa Wen 	if (!*container) {
46507c2a416SMelissa Wen 		DRM_ERROR("Cannot allocate memory for v3d job.");
46607c2a416SMelissa Wen 		return -ENOMEM;
46707c2a416SMelissa Wen 	}
46807c2a416SMelissa Wen 
46907c2a416SMelissa Wen 	job = *container;
470a783a09eSEric Anholt 	job->v3d = v3d;
471a783a09eSEric Anholt 	job->free = free;
472a783a09eSEric Anholt 
473bc662528SDaniel Vetter 	ret = pm_runtime_get_sync(v3d->drm.dev);
474a783a09eSEric Anholt 	if (ret < 0)
47507c2a416SMelissa Wen 		goto fail;
476a783a09eSEric Anholt 
477916044faSDaniel Vetter 	ret = drm_sched_job_init(&job->base, &v3d_priv->sched_entity[queue],
478916044faSDaniel Vetter 				 v3d_priv);
479916044faSDaniel Vetter 	if (ret)
48007c2a416SMelissa Wen 		goto fail_job;
481dffa9b7aSEric Anholt 
482223583ddSMelissa Wen 	ret = v3d_job_add_deps(file_priv, job, in_sync, 0);
483dffa9b7aSEric Anholt 	if (ret)
48407c2a416SMelissa Wen 		goto fail_deps;
485a783a09eSEric Anholt 
486a783a09eSEric Anholt 	kref_init(&job->refcount);
487a783a09eSEric Anholt 
488a783a09eSEric Anholt 	return 0;
48907c2a416SMelissa Wen 
49007c2a416SMelissa Wen fail_deps:
491916044faSDaniel Vetter 	drm_sched_job_cleanup(&job->base);
49207c2a416SMelissa Wen fail_job:
493bc662528SDaniel Vetter 	pm_runtime_put_autosuspend(v3d->drm.dev);
49407c2a416SMelissa Wen fail:
49507c2a416SMelissa Wen 	kfree(*container);
49607c2a416SMelissa Wen 	*container = NULL;
49707c2a416SMelissa Wen 
498dffa9b7aSEric Anholt 	return ret;
499a783a09eSEric Anholt }
500a783a09eSEric Anholt 
501916044faSDaniel Vetter static void
502916044faSDaniel Vetter v3d_push_job(struct v3d_job *job)
503a783a09eSEric Anholt {
504dbe48d03SDaniel Vetter 	drm_sched_job_arm(&job->base);
505dbe48d03SDaniel Vetter 
506a783a09eSEric Anholt 	job->done_fence = dma_fence_get(&job->base.s_fence->finished);
507a783a09eSEric Anholt 
508a783a09eSEric Anholt 	/* put by scheduler job completion */
509a783a09eSEric Anholt 	kref_get(&job->refcount);
510a783a09eSEric Anholt 
5110e10e9a1SDaniel Vetter 	drm_sched_entity_push_job(&job->base);
512a783a09eSEric Anholt }
513a783a09eSEric Anholt 
514a783a09eSEric Anholt static void
515a783a09eSEric Anholt v3d_attach_fences_and_unlock_reservation(struct drm_file *file_priv,
516a783a09eSEric Anholt 					 struct v3d_job *job,
517a783a09eSEric Anholt 					 struct ww_acquire_ctx *acquire_ctx,
518d223f98fSEric Anholt 					 u32 out_sync,
519d223f98fSEric Anholt 					 struct dma_fence *done_fence)
520a783a09eSEric Anholt {
521a783a09eSEric Anholt 	struct drm_syncobj *sync_out;
522a783a09eSEric Anholt 	int i;
523a783a09eSEric Anholt 
524a783a09eSEric Anholt 	for (i = 0; i < job->bo_count; i++) {
525a783a09eSEric Anholt 		/* XXX: Use shared fences for read-only objects. */
52652791eeeSChristian König 		dma_resv_add_excl_fence(job->bo[i]->resv,
527a783a09eSEric Anholt 					job->done_fence);
528a783a09eSEric Anholt 	}
529a783a09eSEric Anholt 
530a783a09eSEric Anholt 	drm_gem_unlock_reservations(job->bo, job->bo_count, acquire_ctx);
531a783a09eSEric Anholt 
532a783a09eSEric Anholt 	/* Update the return sync object for the job */
533a783a09eSEric Anholt 	sync_out = drm_syncobj_find(file_priv, out_sync);
534a783a09eSEric Anholt 	if (sync_out) {
535d223f98fSEric Anholt 		drm_syncobj_replace_fence(sync_out, done_fence);
536a783a09eSEric Anholt 		drm_syncobj_put(sync_out);
537a783a09eSEric Anholt 	}
538a783a09eSEric Anholt }
539a783a09eSEric Anholt 
540*bb3425efSMelissa Wen /* Whenever userspace sets ioctl extensions, v3d_get_extensions parses data
541*bb3425efSMelissa Wen  * according to the extension id (name).
542*bb3425efSMelissa Wen  */
543*bb3425efSMelissa Wen static int
544*bb3425efSMelissa Wen v3d_get_extensions(struct drm_file *file_priv, u64 ext_handles)
545*bb3425efSMelissa Wen {
546*bb3425efSMelissa Wen 	struct drm_v3d_extension __user *user_ext;
547*bb3425efSMelissa Wen 
548*bb3425efSMelissa Wen 	user_ext = u64_to_user_ptr(ext_handles);
549*bb3425efSMelissa Wen 	while (user_ext) {
550*bb3425efSMelissa Wen 		struct drm_v3d_extension ext;
551*bb3425efSMelissa Wen 
552*bb3425efSMelissa Wen 		if (copy_from_user(&ext, user_ext, sizeof(ext))) {
553*bb3425efSMelissa Wen 			DRM_DEBUG("Failed to copy submit extension\n");
554*bb3425efSMelissa Wen 			return -EFAULT;
555*bb3425efSMelissa Wen 		}
556*bb3425efSMelissa Wen 
557*bb3425efSMelissa Wen 		switch (ext.id) {
558*bb3425efSMelissa Wen 		case 0:
559*bb3425efSMelissa Wen 		default:
560*bb3425efSMelissa Wen 			DRM_DEBUG_DRIVER("Unknown extension id: %d\n", ext.id);
561*bb3425efSMelissa Wen 			return -EINVAL;
562*bb3425efSMelissa Wen 		}
563*bb3425efSMelissa Wen 
564*bb3425efSMelissa Wen 		user_ext = u64_to_user_ptr(ext.next);
565*bb3425efSMelissa Wen 	}
566*bb3425efSMelissa Wen 
567*bb3425efSMelissa Wen 	return 0;
568*bb3425efSMelissa Wen }
569*bb3425efSMelissa Wen 
57057692c94SEric Anholt /**
57157692c94SEric Anholt  * v3d_submit_cl_ioctl() - Submits a job (frame) to the V3D.
57257692c94SEric Anholt  * @dev: DRM device
57357692c94SEric Anholt  * @data: ioctl argument
57457692c94SEric Anholt  * @file_priv: DRM file for this fd
57557692c94SEric Anholt  *
57657692c94SEric Anholt  * This is the main entrypoint for userspace to submit a 3D frame to
57757692c94SEric Anholt  * the GPU.  Userspace provides the binner command list (if
57857692c94SEric Anholt  * applicable), and the kernel sets up the render command list to draw
57957692c94SEric Anholt  * to the framebuffer described in the ioctl, using the command lists
58057692c94SEric Anholt  * that the 3D engine's binner will produce.
58157692c94SEric Anholt  */
58257692c94SEric Anholt int
58357692c94SEric Anholt v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
58457692c94SEric Anholt 		    struct drm_file *file_priv)
58557692c94SEric Anholt {
58657692c94SEric Anholt 	struct v3d_dev *v3d = to_v3d_dev(dev);
58757692c94SEric Anholt 	struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
58857692c94SEric Anholt 	struct drm_v3d_submit_cl *args = data;
589a783a09eSEric Anholt 	struct v3d_bin_job *bin = NULL;
59007c2a416SMelissa Wen 	struct v3d_render_job *render = NULL;
591455d56ceSIago Toral Quiroga 	struct v3d_job *clean_job = NULL;
592455d56ceSIago Toral Quiroga 	struct v3d_job *last_job;
59357692c94SEric Anholt 	struct ww_acquire_ctx acquire_ctx;
59457692c94SEric Anholt 	int ret = 0;
59557692c94SEric Anholt 
59655a9b748SEric Anholt 	trace_v3d_submit_cl_ioctl(&v3d->drm, args->rcl_start, args->rcl_end);
59755a9b748SEric Anholt 
598*bb3425efSMelissa Wen 	if (args->pad)
59926a4dc29SJuan A. Suarez Romero 		return -EINVAL;
60026a4dc29SJuan A. Suarez Romero 
601*bb3425efSMelissa Wen 	if (args->flags &&
602*bb3425efSMelissa Wen 	    args->flags & ~(DRM_V3D_SUBMIT_CL_FLUSH_CACHE |
603*bb3425efSMelissa Wen 			    DRM_V3D_SUBMIT_EXTENSION)) {
604455d56ceSIago Toral Quiroga 		DRM_INFO("invalid flags: %d\n", args->flags);
60557692c94SEric Anholt 		return -EINVAL;
60657692c94SEric Anholt 	}
60757692c94SEric Anholt 
608*bb3425efSMelissa Wen 	if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
609*bb3425efSMelissa Wen 		ret = v3d_get_extensions(file_priv, args->extensions);
610*bb3425efSMelissa Wen 		if (ret) {
611*bb3425efSMelissa Wen 			DRM_DEBUG("Failed to get extensions.\n");
612*bb3425efSMelissa Wen 			return ret;
613*bb3425efSMelissa Wen 		}
614*bb3425efSMelissa Wen 	}
615*bb3425efSMelissa Wen 
61607c2a416SMelissa Wen 	ret = v3d_job_init(v3d, file_priv, (void *)&render, sizeof(*render),
61707c2a416SMelissa Wen 			   v3d_render_job_free, args->in_sync_rcl, V3D_RENDER);
61807c2a416SMelissa Wen 	if (ret)
61907c2a416SMelissa Wen 		goto fail;
62057692c94SEric Anholt 
621a783a09eSEric Anholt 	render->start = args->rcl_start;
622a783a09eSEric Anholt 	render->end = args->rcl_end;
623a783a09eSEric Anholt 	INIT_LIST_HEAD(&render->unref_list);
624a783a09eSEric Anholt 
625a783a09eSEric Anholt 	if (args->bcl_start != args->bcl_end) {
62607c2a416SMelissa Wen 		ret = v3d_job_init(v3d, file_priv, (void *)&bin, sizeof(*bin),
627916044faSDaniel Vetter 				   v3d_job_free, args->in_sync_bcl, V3D_BIN);
62807c2a416SMelissa Wen 		if (ret)
62907c2a416SMelissa Wen 			goto fail;
63057692c94SEric Anholt 
631a783a09eSEric Anholt 		bin->start = args->bcl_start;
632a783a09eSEric Anholt 		bin->end = args->bcl_end;
633a783a09eSEric Anholt 		bin->qma = args->qma;
634a783a09eSEric Anholt 		bin->qms = args->qms;
635a783a09eSEric Anholt 		bin->qts = args->qts;
636a783a09eSEric Anholt 		bin->render = render;
637a783a09eSEric Anholt 	}
63857692c94SEric Anholt 
639455d56ceSIago Toral Quiroga 	if (args->flags & DRM_V3D_SUBMIT_CL_FLUSH_CACHE) {
64007c2a416SMelissa Wen 		ret = v3d_job_init(v3d, file_priv, (void *)&clean_job, sizeof(*clean_job),
64107c2a416SMelissa Wen 				   v3d_job_free, 0, V3D_CACHE_CLEAN);
64207c2a416SMelissa Wen 		if (ret)
643455d56ceSIago Toral Quiroga 			goto fail;
644455d56ceSIago Toral Quiroga 
645455d56ceSIago Toral Quiroga 		last_job = clean_job;
646455d56ceSIago Toral Quiroga 	} else {
647455d56ceSIago Toral Quiroga 		last_job = &render->base;
648455d56ceSIago Toral Quiroga 	}
649455d56ceSIago Toral Quiroga 
650455d56ceSIago Toral Quiroga 	ret = v3d_lookup_bos(dev, file_priv, last_job,
651a783a09eSEric Anholt 			     args->bo_handles, args->bo_handle_count);
65257692c94SEric Anholt 	if (ret)
65357692c94SEric Anholt 		goto fail;
65457692c94SEric Anholt 
655455d56ceSIago Toral Quiroga 	ret = v3d_lock_bo_reservations(last_job, &acquire_ctx);
65657692c94SEric Anholt 	if (ret)
65757692c94SEric Anholt 		goto fail;
65857692c94SEric Anholt 
65926a4dc29SJuan A. Suarez Romero 	if (args->perfmon_id) {
66026a4dc29SJuan A. Suarez Romero 		render->base.perfmon = v3d_perfmon_find(v3d_priv,
66126a4dc29SJuan A. Suarez Romero 							args->perfmon_id);
66226a4dc29SJuan A. Suarez Romero 
66326a4dc29SJuan A. Suarez Romero 		if (!render->base.perfmon) {
66426a4dc29SJuan A. Suarez Romero 			ret = -ENOENT;
66526a4dc29SJuan A. Suarez Romero 			goto fail;
66626a4dc29SJuan A. Suarez Romero 		}
66726a4dc29SJuan A. Suarez Romero 	}
66826a4dc29SJuan A. Suarez Romero 
6697122b68bSEric Anholt 	mutex_lock(&v3d->sched_lock);
670a783a09eSEric Anholt 	if (bin) {
67126a4dc29SJuan A. Suarez Romero 		bin->base.perfmon = render->base.perfmon;
67226a4dc29SJuan A. Suarez Romero 		v3d_perfmon_get(bin->base.perfmon);
673916044faSDaniel Vetter 		v3d_push_job(&bin->base);
67457692c94SEric Anholt 
675da3208e8SDaniel Vetter 		ret = drm_sched_job_add_dependency(&render->base.base,
676dffa9b7aSEric Anholt 						   dma_fence_get(bin->base.done_fence));
677dffa9b7aSEric Anholt 		if (ret)
678dffa9b7aSEric Anholt 			goto fail_unreserve;
67957692c94SEric Anholt 	}
68057692c94SEric Anholt 
681916044faSDaniel Vetter 	v3d_push_job(&render->base);
682455d56ceSIago Toral Quiroga 
683455d56ceSIago Toral Quiroga 	if (clean_job) {
684455d56ceSIago Toral Quiroga 		struct dma_fence *render_fence =
685455d56ceSIago Toral Quiroga 			dma_fence_get(render->base.done_fence);
686da3208e8SDaniel Vetter 		ret = drm_sched_job_add_dependency(&clean_job->base,
687da3208e8SDaniel Vetter 						   render_fence);
688455d56ceSIago Toral Quiroga 		if (ret)
689455d56ceSIago Toral Quiroga 			goto fail_unreserve;
69026a4dc29SJuan A. Suarez Romero 		clean_job->perfmon = render->base.perfmon;
69126a4dc29SJuan A. Suarez Romero 		v3d_perfmon_get(clean_job->perfmon);
692916044faSDaniel Vetter 		v3d_push_job(clean_job);
693455d56ceSIago Toral Quiroga 	}
694455d56ceSIago Toral Quiroga 
6957122b68bSEric Anholt 	mutex_unlock(&v3d->sched_lock);
69657692c94SEric Anholt 
697a783a09eSEric Anholt 	v3d_attach_fences_and_unlock_reservation(file_priv,
698455d56ceSIago Toral Quiroga 						 last_job,
699d223f98fSEric Anholt 						 &acquire_ctx,
700d223f98fSEric Anholt 						 args->out_sync,
701455d56ceSIago Toral Quiroga 						 last_job->done_fence);
70257692c94SEric Anholt 
703a783a09eSEric Anholt 	if (bin)
704a783a09eSEric Anholt 		v3d_job_put(&bin->base);
705a783a09eSEric Anholt 	v3d_job_put(&render->base);
706455d56ceSIago Toral Quiroga 	if (clean_job)
707455d56ceSIago Toral Quiroga 		v3d_job_put(clean_job);
70857692c94SEric Anholt 
70957692c94SEric Anholt 	return 0;
71057692c94SEric Anholt 
71157692c94SEric Anholt fail_unreserve:
7127122b68bSEric Anholt 	mutex_unlock(&v3d->sched_lock);
713455d56ceSIago Toral Quiroga 	drm_gem_unlock_reservations(last_job->bo,
714455d56ceSIago Toral Quiroga 				    last_job->bo_count, &acquire_ctx);
71557692c94SEric Anholt fail:
71607c2a416SMelissa Wen 	v3d_job_cleanup((void *)bin);
71707c2a416SMelissa Wen 	v3d_job_cleanup((void *)render);
718916044faSDaniel Vetter 	v3d_job_cleanup(clean_job);
71957692c94SEric Anholt 
72057692c94SEric Anholt 	return ret;
72157692c94SEric Anholt }
72257692c94SEric Anholt 
7231584f16cSEric Anholt /**
7241584f16cSEric Anholt  * v3d_submit_tfu_ioctl() - Submits a TFU (texture formatting) job to the V3D.
7251584f16cSEric Anholt  * @dev: DRM device
7261584f16cSEric Anholt  * @data: ioctl argument
7271584f16cSEric Anholt  * @file_priv: DRM file for this fd
7281584f16cSEric Anholt  *
7291584f16cSEric Anholt  * Userspace provides the register setup for the TFU, which we don't
7301584f16cSEric Anholt  * need to validate since the TFU is behind the MMU.
7311584f16cSEric Anholt  */
7321584f16cSEric Anholt int
7331584f16cSEric Anholt v3d_submit_tfu_ioctl(struct drm_device *dev, void *data,
7341584f16cSEric Anholt 		     struct drm_file *file_priv)
7351584f16cSEric Anholt {
7361584f16cSEric Anholt 	struct v3d_dev *v3d = to_v3d_dev(dev);
7371584f16cSEric Anholt 	struct drm_v3d_submit_tfu *args = data;
73807c2a416SMelissa Wen 	struct v3d_tfu_job *job = NULL;
7391584f16cSEric Anholt 	struct ww_acquire_ctx acquire_ctx;
7401584f16cSEric Anholt 	int ret = 0;
7411584f16cSEric Anholt 
74255a9b748SEric Anholt 	trace_v3d_submit_tfu_ioctl(&v3d->drm, args->iia);
74355a9b748SEric Anholt 
744*bb3425efSMelissa Wen 	if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) {
745*bb3425efSMelissa Wen 		DRM_DEBUG("invalid flags: %d\n", args->flags);
746*bb3425efSMelissa Wen 		return -EINVAL;
747*bb3425efSMelissa Wen 	}
748*bb3425efSMelissa Wen 
749*bb3425efSMelissa Wen 	if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
750*bb3425efSMelissa Wen 		ret = v3d_get_extensions(file_priv, args->extensions);
751*bb3425efSMelissa Wen 		if (ret) {
752*bb3425efSMelissa Wen 			DRM_DEBUG("Failed to get extensions.\n");
753*bb3425efSMelissa Wen 			return ret;
754*bb3425efSMelissa Wen 		}
755*bb3425efSMelissa Wen 	}
756*bb3425efSMelissa Wen 
75707c2a416SMelissa Wen 	ret = v3d_job_init(v3d, file_priv, (void *)&job, sizeof(*job),
758916044faSDaniel Vetter 			   v3d_job_free, args->in_sync, V3D_TFU);
75907c2a416SMelissa Wen 	if (ret)
76007c2a416SMelissa Wen 		goto fail;
7611584f16cSEric Anholt 
762a783a09eSEric Anholt 	job->base.bo = kcalloc(ARRAY_SIZE(args->bo_handles),
763a783a09eSEric Anholt 			       sizeof(*job->base.bo), GFP_KERNEL);
764a783a09eSEric Anholt 	if (!job->base.bo) {
76507c2a416SMelissa Wen 		ret = -ENOMEM;
76607c2a416SMelissa Wen 		goto fail;
767a783a09eSEric Anholt 	}
7681584f16cSEric Anholt 
7691584f16cSEric Anholt 	job->args = *args;
7701584f16cSEric Anholt 
7711584f16cSEric Anholt 	spin_lock(&file_priv->table_lock);
772a783a09eSEric Anholt 	for (job->base.bo_count = 0;
773a783a09eSEric Anholt 	     job->base.bo_count < ARRAY_SIZE(args->bo_handles);
774a783a09eSEric Anholt 	     job->base.bo_count++) {
7751584f16cSEric Anholt 		struct drm_gem_object *bo;
7761584f16cSEric Anholt 
777a783a09eSEric Anholt 		if (!args->bo_handles[job->base.bo_count])
7781584f16cSEric Anholt 			break;
7791584f16cSEric Anholt 
7801584f16cSEric Anholt 		bo = idr_find(&file_priv->object_idr,
781a783a09eSEric Anholt 			      args->bo_handles[job->base.bo_count]);
7821584f16cSEric Anholt 		if (!bo) {
7831584f16cSEric Anholt 			DRM_DEBUG("Failed to look up GEM BO %d: %d\n",
784a783a09eSEric Anholt 				  job->base.bo_count,
785a783a09eSEric Anholt 				  args->bo_handles[job->base.bo_count]);
7861584f16cSEric Anholt 			ret = -ENOENT;
7871584f16cSEric Anholt 			spin_unlock(&file_priv->table_lock);
7881584f16cSEric Anholt 			goto fail;
7891584f16cSEric Anholt 		}
7901584f16cSEric Anholt 		drm_gem_object_get(bo);
791a783a09eSEric Anholt 		job->base.bo[job->base.bo_count] = bo;
7921584f16cSEric Anholt 	}
7931584f16cSEric Anholt 	spin_unlock(&file_priv->table_lock);
7941584f16cSEric Anholt 
795dffa9b7aSEric Anholt 	ret = v3d_lock_bo_reservations(&job->base, &acquire_ctx);
7961584f16cSEric Anholt 	if (ret)
7971584f16cSEric Anholt 		goto fail;
7981584f16cSEric Anholt 
7991584f16cSEric Anholt 	mutex_lock(&v3d->sched_lock);
800916044faSDaniel Vetter 	v3d_push_job(&job->base);
8011584f16cSEric Anholt 	mutex_unlock(&v3d->sched_lock);
8021584f16cSEric Anholt 
803a783a09eSEric Anholt 	v3d_attach_fences_and_unlock_reservation(file_priv,
804a783a09eSEric Anholt 						 &job->base, &acquire_ctx,
805d223f98fSEric Anholt 						 args->out_sync,
806d223f98fSEric Anholt 						 job->base.done_fence);
8071584f16cSEric Anholt 
808a783a09eSEric Anholt 	v3d_job_put(&job->base);
8091584f16cSEric Anholt 
8101584f16cSEric Anholt 	return 0;
8111584f16cSEric Anholt 
8121584f16cSEric Anholt fail:
81307c2a416SMelissa Wen 	v3d_job_cleanup((void *)job);
8141584f16cSEric Anholt 
8151584f16cSEric Anholt 	return ret;
8161584f16cSEric Anholt }
8171584f16cSEric Anholt 
818d223f98fSEric Anholt /**
819d223f98fSEric Anholt  * v3d_submit_csd_ioctl() - Submits a CSD (texture formatting) job to the V3D.
820d223f98fSEric Anholt  * @dev: DRM device
821d223f98fSEric Anholt  * @data: ioctl argument
822d223f98fSEric Anholt  * @file_priv: DRM file for this fd
823d223f98fSEric Anholt  *
824d223f98fSEric Anholt  * Userspace provides the register setup for the CSD, which we don't
825d223f98fSEric Anholt  * need to validate since the CSD is behind the MMU.
826d223f98fSEric Anholt  */
827d223f98fSEric Anholt int
828d223f98fSEric Anholt v3d_submit_csd_ioctl(struct drm_device *dev, void *data,
829d223f98fSEric Anholt 		     struct drm_file *file_priv)
830d223f98fSEric Anholt {
831d223f98fSEric Anholt 	struct v3d_dev *v3d = to_v3d_dev(dev);
832d223f98fSEric Anholt 	struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
833d223f98fSEric Anholt 	struct drm_v3d_submit_csd *args = data;
83407c2a416SMelissa Wen 	struct v3d_csd_job *job = NULL;
83507c2a416SMelissa Wen 	struct v3d_job *clean_job = NULL;
836d223f98fSEric Anholt 	struct ww_acquire_ctx acquire_ctx;
837d223f98fSEric Anholt 	int ret;
838d223f98fSEric Anholt 
839d223f98fSEric Anholt 	trace_v3d_submit_csd_ioctl(&v3d->drm, args->cfg[5], args->cfg[6]);
840d223f98fSEric Anholt 
841*bb3425efSMelissa Wen 	if (args->pad)
842*bb3425efSMelissa Wen 		return -EINVAL;
843*bb3425efSMelissa Wen 
844d223f98fSEric Anholt 	if (!v3d_has_csd(v3d)) {
845d223f98fSEric Anholt 		DRM_DEBUG("Attempting CSD submit on non-CSD hardware\n");
846d223f98fSEric Anholt 		return -EINVAL;
847d223f98fSEric Anholt 	}
848d223f98fSEric Anholt 
849*bb3425efSMelissa Wen 	if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) {
850*bb3425efSMelissa Wen 		DRM_INFO("invalid flags: %d\n", args->flags);
851*bb3425efSMelissa Wen 		return -EINVAL;
852*bb3425efSMelissa Wen 	}
853*bb3425efSMelissa Wen 
854*bb3425efSMelissa Wen 	if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
855*bb3425efSMelissa Wen 		ret = v3d_get_extensions(file_priv, args->extensions);
856*bb3425efSMelissa Wen 		if (ret) {
857*bb3425efSMelissa Wen 			DRM_DEBUG("Failed to get extensions.\n");
858*bb3425efSMelissa Wen 			return ret;
859*bb3425efSMelissa Wen 		}
860*bb3425efSMelissa Wen 	}
861*bb3425efSMelissa Wen 
86207c2a416SMelissa Wen 	ret = v3d_job_init(v3d, file_priv, (void *)&job, sizeof(*job),
863916044faSDaniel Vetter 			   v3d_job_free, args->in_sync, V3D_CSD);
86407c2a416SMelissa Wen 	if (ret)
86507c2a416SMelissa Wen 		goto fail;
866d223f98fSEric Anholt 
86707c2a416SMelissa Wen 	ret = v3d_job_init(v3d, file_priv, (void *)&clean_job, sizeof(*clean_job),
86807c2a416SMelissa Wen 			   v3d_job_free, 0, V3D_CACHE_CLEAN);
86907c2a416SMelissa Wen 	if (ret)
87007c2a416SMelissa Wen 		goto fail;
871d223f98fSEric Anholt 
872d223f98fSEric Anholt 	job->args = *args;
873d223f98fSEric Anholt 
874d223f98fSEric Anholt 	ret = v3d_lookup_bos(dev, file_priv, clean_job,
875d223f98fSEric Anholt 			     args->bo_handles, args->bo_handle_count);
876d223f98fSEric Anholt 	if (ret)
877d223f98fSEric Anholt 		goto fail;
878d223f98fSEric Anholt 
879dffa9b7aSEric Anholt 	ret = v3d_lock_bo_reservations(clean_job, &acquire_ctx);
880d223f98fSEric Anholt 	if (ret)
881d223f98fSEric Anholt 		goto fail;
882d223f98fSEric Anholt 
88326a4dc29SJuan A. Suarez Romero 	if (args->perfmon_id) {
88426a4dc29SJuan A. Suarez Romero 		job->base.perfmon = v3d_perfmon_find(v3d_priv,
88526a4dc29SJuan A. Suarez Romero 						     args->perfmon_id);
88626a4dc29SJuan A. Suarez Romero 		if (!job->base.perfmon) {
88726a4dc29SJuan A. Suarez Romero 			ret = -ENOENT;
88826a4dc29SJuan A. Suarez Romero 			goto fail;
88926a4dc29SJuan A. Suarez Romero 		}
89026a4dc29SJuan A. Suarez Romero 	}
89126a4dc29SJuan A. Suarez Romero 
892d223f98fSEric Anholt 	mutex_lock(&v3d->sched_lock);
893916044faSDaniel Vetter 	v3d_push_job(&job->base);
894d223f98fSEric Anholt 
895da3208e8SDaniel Vetter 	ret = drm_sched_job_add_dependency(&clean_job->base,
896dffa9b7aSEric Anholt 					   dma_fence_get(job->base.done_fence));
897dffa9b7aSEric Anholt 	if (ret)
898dffa9b7aSEric Anholt 		goto fail_unreserve;
899dffa9b7aSEric Anholt 
900916044faSDaniel Vetter 	v3d_push_job(clean_job);
901d223f98fSEric Anholt 	mutex_unlock(&v3d->sched_lock);
902d223f98fSEric Anholt 
903d223f98fSEric Anholt 	v3d_attach_fences_and_unlock_reservation(file_priv,
904d223f98fSEric Anholt 						 clean_job,
905d223f98fSEric Anholt 						 &acquire_ctx,
906d223f98fSEric Anholt 						 args->out_sync,
907d223f98fSEric Anholt 						 clean_job->done_fence);
908d223f98fSEric Anholt 
909d223f98fSEric Anholt 	v3d_job_put(&job->base);
910d223f98fSEric Anholt 	v3d_job_put(clean_job);
911d223f98fSEric Anholt 
912d223f98fSEric Anholt 	return 0;
913d223f98fSEric Anholt 
914d223f98fSEric Anholt fail_unreserve:
915d223f98fSEric Anholt 	mutex_unlock(&v3d->sched_lock);
916d223f98fSEric Anholt 	drm_gem_unlock_reservations(clean_job->bo, clean_job->bo_count,
917d223f98fSEric Anholt 				    &acquire_ctx);
918d223f98fSEric Anholt fail:
91907c2a416SMelissa Wen 	v3d_job_cleanup((void *)job);
920916044faSDaniel Vetter 	v3d_job_cleanup(clean_job);
921d223f98fSEric Anholt 
922d223f98fSEric Anholt 	return ret;
923d223f98fSEric Anholt }
924d223f98fSEric Anholt 
92557692c94SEric Anholt int
92657692c94SEric Anholt v3d_gem_init(struct drm_device *dev)
92757692c94SEric Anholt {
92857692c94SEric Anholt 	struct v3d_dev *v3d = to_v3d_dev(dev);
92957692c94SEric Anholt 	u32 pt_size = 4096 * 1024;
93057692c94SEric Anholt 	int ret, i;
93157692c94SEric Anholt 
93257692c94SEric Anholt 	for (i = 0; i < V3D_MAX_QUEUES; i++)
93357692c94SEric Anholt 		v3d->queue[i].fence_context = dma_fence_context_alloc(1);
93457692c94SEric Anholt 
93557692c94SEric Anholt 	spin_lock_init(&v3d->mm_lock);
93657692c94SEric Anholt 	spin_lock_init(&v3d->job_lock);
93757692c94SEric Anholt 	mutex_init(&v3d->bo_lock);
93857692c94SEric Anholt 	mutex_init(&v3d->reset_lock);
9397122b68bSEric Anholt 	mutex_init(&v3d->sched_lock);
940d223f98fSEric Anholt 	mutex_init(&v3d->cache_clean_lock);
94157692c94SEric Anholt 
94257692c94SEric Anholt 	/* Note: We don't allocate address 0.  Various bits of HW
94357692c94SEric Anholt 	 * treat 0 as special, such as the occlusion query counters
94457692c94SEric Anholt 	 * where 0 means "disabled".
94557692c94SEric Anholt 	 */
94657692c94SEric Anholt 	drm_mm_init(&v3d->mm, 1, pt_size / sizeof(u32) - 1);
94757692c94SEric Anholt 
948bc662528SDaniel Vetter 	v3d->pt = dma_alloc_wc(v3d->drm.dev, pt_size,
94957692c94SEric Anholt 			       &v3d->pt_paddr,
95057692c94SEric Anholt 			       GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);
95157692c94SEric Anholt 	if (!v3d->pt) {
95257692c94SEric Anholt 		drm_mm_takedown(&v3d->mm);
953bc662528SDaniel Vetter 		dev_err(v3d->drm.dev,
954223583ddSMelissa Wen 			"Failed to allocate page tables. Please ensure you have CMA enabled.\n");
95557692c94SEric Anholt 		return -ENOMEM;
95657692c94SEric Anholt 	}
95757692c94SEric Anholt 
95857692c94SEric Anholt 	v3d_init_hw_state(v3d);
95957692c94SEric Anholt 	v3d_mmu_set_page_table(v3d);
96057692c94SEric Anholt 
96157692c94SEric Anholt 	ret = v3d_sched_init(v3d);
96257692c94SEric Anholt 	if (ret) {
96357692c94SEric Anholt 		drm_mm_takedown(&v3d->mm);
964bc662528SDaniel Vetter 		dma_free_coherent(v3d->drm.dev, 4096 * 1024, (void *)v3d->pt,
96557692c94SEric Anholt 				  v3d->pt_paddr);
96657692c94SEric Anholt 	}
96757692c94SEric Anholt 
96857692c94SEric Anholt 	return 0;
96957692c94SEric Anholt }
97057692c94SEric Anholt 
97157692c94SEric Anholt void
97257692c94SEric Anholt v3d_gem_destroy(struct drm_device *dev)
97357692c94SEric Anholt {
97457692c94SEric Anholt 	struct v3d_dev *v3d = to_v3d_dev(dev);
97557692c94SEric Anholt 
97657692c94SEric Anholt 	v3d_sched_fini(v3d);
97757692c94SEric Anholt 
978a783a09eSEric Anholt 	/* Waiting for jobs to finish would need to be done before
97957692c94SEric Anholt 	 * unregistering V3D.
98057692c94SEric Anholt 	 */
98114d1d190SEric Anholt 	WARN_ON(v3d->bin_job);
98214d1d190SEric Anholt 	WARN_ON(v3d->render_job);
98357692c94SEric Anholt 
98457692c94SEric Anholt 	drm_mm_takedown(&v3d->mm);
98557692c94SEric Anholt 
986bc662528SDaniel Vetter 	dma_free_coherent(v3d->drm.dev, 4096 * 1024, (void *)v3d->pt,
987bc662528SDaniel Vetter 			  v3d->pt_paddr);
98857692c94SEric Anholt }
989