xref: /openbmc/linux/drivers/gpu/drm/v3d/v3d_gem.c (revision 1584f16ca96ef124aad79efa3303cff5f3530e2c)
157692c94SEric Anholt // SPDX-License-Identifier: GPL-2.0+
257692c94SEric Anholt /* Copyright (C) 2014-2018 Broadcom */
357692c94SEric Anholt 
457692c94SEric Anholt #include <drm/drmP.h>
557692c94SEric Anholt #include <drm/drm_syncobj.h>
657692c94SEric Anholt #include <linux/module.h>
757692c94SEric Anholt #include <linux/platform_device.h>
857692c94SEric Anholt #include <linux/pm_runtime.h>
957692c94SEric Anholt #include <linux/device.h>
1057692c94SEric Anholt #include <linux/io.h>
1157692c94SEric Anholt #include <linux/sched/signal.h>
1257692c94SEric Anholt 
1357692c94SEric Anholt #include "uapi/drm/v3d_drm.h"
1457692c94SEric Anholt #include "v3d_drv.h"
1557692c94SEric Anholt #include "v3d_regs.h"
1657692c94SEric Anholt #include "v3d_trace.h"
1757692c94SEric Anholt 
1857692c94SEric Anholt static void
1957692c94SEric Anholt v3d_init_core(struct v3d_dev *v3d, int core)
2057692c94SEric Anholt {
2157692c94SEric Anholt 	/* Set OVRTMUOUT, which means that the texture sampler uniform
2257692c94SEric Anholt 	 * configuration's tmu output type field is used, instead of
2357692c94SEric Anholt 	 * using the hardware default behavior based on the texture
2457692c94SEric Anholt 	 * type.  If you want the default behavior, you can still put
2557692c94SEric Anholt 	 * "2" in the indirect texture state's output_type field.
2657692c94SEric Anholt 	 */
2757692c94SEric Anholt 	V3D_CORE_WRITE(core, V3D_CTL_MISCCFG, V3D_MISCCFG_OVRTMUOUT);
2857692c94SEric Anholt 
2957692c94SEric Anholt 	/* Whenever we flush the L2T cache, we always want to flush
3057692c94SEric Anholt 	 * the whole thing.
3157692c94SEric Anholt 	 */
3257692c94SEric Anholt 	V3D_CORE_WRITE(core, V3D_CTL_L2TFLSTA, 0);
3357692c94SEric Anholt 	V3D_CORE_WRITE(core, V3D_CTL_L2TFLEND, ~0);
3457692c94SEric Anholt }
3557692c94SEric Anholt 
3657692c94SEric Anholt /* Sets invariant state for the HW. */
3757692c94SEric Anholt static void
3857692c94SEric Anholt v3d_init_hw_state(struct v3d_dev *v3d)
3957692c94SEric Anholt {
4057692c94SEric Anholt 	v3d_init_core(v3d, 0);
4157692c94SEric Anholt }
4257692c94SEric Anholt 
4357692c94SEric Anholt static void
4457692c94SEric Anholt v3d_idle_axi(struct v3d_dev *v3d, int core)
4557692c94SEric Anholt {
4657692c94SEric Anholt 	V3D_CORE_WRITE(core, V3D_GMP_CFG, V3D_GMP_CFG_STOP_REQ);
4757692c94SEric Anholt 
4857692c94SEric Anholt 	if (wait_for((V3D_CORE_READ(core, V3D_GMP_STATUS) &
4957692c94SEric Anholt 		      (V3D_GMP_STATUS_RD_COUNT_MASK |
5057692c94SEric Anholt 		       V3D_GMP_STATUS_WR_COUNT_MASK |
5157692c94SEric Anholt 		       V3D_GMP_STATUS_CFG_BUSY)) == 0, 100)) {
5257692c94SEric Anholt 		DRM_ERROR("Failed to wait for safe GMP shutdown\n");
5357692c94SEric Anholt 	}
5457692c94SEric Anholt }
5557692c94SEric Anholt 
5657692c94SEric Anholt static void
5757692c94SEric Anholt v3d_idle_gca(struct v3d_dev *v3d)
5857692c94SEric Anholt {
5957692c94SEric Anholt 	if (v3d->ver >= 41)
6057692c94SEric Anholt 		return;
6157692c94SEric Anholt 
6257692c94SEric Anholt 	V3D_GCA_WRITE(V3D_GCA_SAFE_SHUTDOWN, V3D_GCA_SAFE_SHUTDOWN_EN);
6357692c94SEric Anholt 
6457692c94SEric Anholt 	if (wait_for((V3D_GCA_READ(V3D_GCA_SAFE_SHUTDOWN_ACK) &
6557692c94SEric Anholt 		      V3D_GCA_SAFE_SHUTDOWN_ACK_ACKED) ==
6657692c94SEric Anholt 		     V3D_GCA_SAFE_SHUTDOWN_ACK_ACKED, 100)) {
6757692c94SEric Anholt 		DRM_ERROR("Failed to wait for safe GCA shutdown\n");
6857692c94SEric Anholt 	}
6957692c94SEric Anholt }
7057692c94SEric Anholt 
7157692c94SEric Anholt static void
7257692c94SEric Anholt v3d_reset_v3d(struct v3d_dev *v3d)
7357692c94SEric Anholt {
7457692c94SEric Anholt 	int version = V3D_BRIDGE_READ(V3D_TOP_GR_BRIDGE_REVISION);
7557692c94SEric Anholt 
7657692c94SEric Anholt 	if (V3D_GET_FIELD(version, V3D_TOP_GR_BRIDGE_MAJOR) == 2) {
7757692c94SEric Anholt 		V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_0,
7857692c94SEric Anholt 				 V3D_TOP_GR_BRIDGE_SW_INIT_0_V3D_CLK_108_SW_INIT);
7957692c94SEric Anholt 		V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_0, 0);
8057692c94SEric Anholt 
8157692c94SEric Anholt 		/* GFXH-1383: The SW_INIT may cause a stray write to address 0
8257692c94SEric Anholt 		 * of the unit, so reset it to its power-on value here.
8357692c94SEric Anholt 		 */
8457692c94SEric Anholt 		V3D_WRITE(V3D_HUB_AXICFG, V3D_HUB_AXICFG_MAX_LEN_MASK);
8557692c94SEric Anholt 	} else {
8657692c94SEric Anholt 		WARN_ON_ONCE(V3D_GET_FIELD(version,
8757692c94SEric Anholt 					   V3D_TOP_GR_BRIDGE_MAJOR) != 7);
8857692c94SEric Anholt 		V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_1,
8957692c94SEric Anholt 				 V3D_TOP_GR_BRIDGE_SW_INIT_1_V3D_CLK_108_SW_INIT);
9057692c94SEric Anholt 		V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_1, 0);
9157692c94SEric Anholt 	}
9257692c94SEric Anholt 
9357692c94SEric Anholt 	v3d_init_hw_state(v3d);
9457692c94SEric Anholt }
9557692c94SEric Anholt 
9657692c94SEric Anholt void
9757692c94SEric Anholt v3d_reset(struct v3d_dev *v3d)
9857692c94SEric Anholt {
9957692c94SEric Anholt 	struct drm_device *dev = &v3d->drm;
10057692c94SEric Anholt 
10157692c94SEric Anholt 	DRM_ERROR("Resetting GPU.\n");
10257692c94SEric Anholt 	trace_v3d_reset_begin(dev);
10357692c94SEric Anholt 
10457692c94SEric Anholt 	/* XXX: only needed for safe powerdown, not reset. */
10557692c94SEric Anholt 	if (false)
10657692c94SEric Anholt 		v3d_idle_axi(v3d, 0);
10757692c94SEric Anholt 
10857692c94SEric Anholt 	v3d_idle_gca(v3d);
10957692c94SEric Anholt 	v3d_reset_v3d(v3d);
11057692c94SEric Anholt 
11157692c94SEric Anholt 	v3d_mmu_set_page_table(v3d);
11257692c94SEric Anholt 	v3d_irq_reset(v3d);
11357692c94SEric Anholt 
11457692c94SEric Anholt 	trace_v3d_reset_end(dev);
11557692c94SEric Anholt }
11657692c94SEric Anholt 
11757692c94SEric Anholt static void
11857692c94SEric Anholt v3d_flush_l3(struct v3d_dev *v3d)
11957692c94SEric Anholt {
12057692c94SEric Anholt 	if (v3d->ver < 41) {
12157692c94SEric Anholt 		u32 gca_ctrl = V3D_GCA_READ(V3D_GCA_CACHE_CTRL);
12257692c94SEric Anholt 
12357692c94SEric Anholt 		V3D_GCA_WRITE(V3D_GCA_CACHE_CTRL,
12457692c94SEric Anholt 			      gca_ctrl | V3D_GCA_CACHE_CTRL_FLUSH);
12557692c94SEric Anholt 
12657692c94SEric Anholt 		if (v3d->ver < 33) {
12757692c94SEric Anholt 			V3D_GCA_WRITE(V3D_GCA_CACHE_CTRL,
12857692c94SEric Anholt 				      gca_ctrl & ~V3D_GCA_CACHE_CTRL_FLUSH);
12957692c94SEric Anholt 		}
13057692c94SEric Anholt 	}
13157692c94SEric Anholt }
13257692c94SEric Anholt 
13357692c94SEric Anholt /* Invalidates the (read-only) L2 cache. */
13457692c94SEric Anholt static void
13557692c94SEric Anholt v3d_invalidate_l2(struct v3d_dev *v3d, int core)
13657692c94SEric Anholt {
13757692c94SEric Anholt 	V3D_CORE_WRITE(core, V3D_CTL_L2CACTL,
13857692c94SEric Anholt 		       V3D_L2CACTL_L2CCLR |
13957692c94SEric Anholt 		       V3D_L2CACTL_L2CENA);
14057692c94SEric Anholt }
14157692c94SEric Anholt 
14257692c94SEric Anholt static void
14357692c94SEric Anholt v3d_invalidate_l1td(struct v3d_dev *v3d, int core)
14457692c94SEric Anholt {
14557692c94SEric Anholt 	V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL, V3D_L2TCACTL_TMUWCF);
14657692c94SEric Anholt 	if (wait_for(!(V3D_CORE_READ(core, V3D_CTL_L2TCACTL) &
14757692c94SEric Anholt 		       V3D_L2TCACTL_L2TFLS), 100)) {
14857692c94SEric Anholt 		DRM_ERROR("Timeout waiting for L1T write combiner flush\n");
14957692c94SEric Anholt 	}
15057692c94SEric Anholt }
15157692c94SEric Anholt 
15257692c94SEric Anholt /* Invalidates texture L2 cachelines */
15357692c94SEric Anholt static void
15457692c94SEric Anholt v3d_flush_l2t(struct v3d_dev *v3d, int core)
15557692c94SEric Anholt {
15657692c94SEric Anholt 	v3d_invalidate_l1td(v3d, core);
15757692c94SEric Anholt 
15857692c94SEric Anholt 	V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL,
15957692c94SEric Anholt 		       V3D_L2TCACTL_L2TFLS |
16057692c94SEric Anholt 		       V3D_SET_FIELD(V3D_L2TCACTL_FLM_FLUSH, V3D_L2TCACTL_FLM));
16157692c94SEric Anholt 	if (wait_for(!(V3D_CORE_READ(core, V3D_CTL_L2TCACTL) &
16257692c94SEric Anholt 		       V3D_L2TCACTL_L2TFLS), 100)) {
16357692c94SEric Anholt 		DRM_ERROR("Timeout waiting for L2T flush\n");
16457692c94SEric Anholt 	}
16557692c94SEric Anholt }
16657692c94SEric Anholt 
16757692c94SEric Anholt /* Invalidates the slice caches.  These are read-only caches. */
16857692c94SEric Anholt static void
16957692c94SEric Anholt v3d_invalidate_slices(struct v3d_dev *v3d, int core)
17057692c94SEric Anholt {
17157692c94SEric Anholt 	V3D_CORE_WRITE(core, V3D_CTL_SLCACTL,
17257692c94SEric Anholt 		       V3D_SET_FIELD(0xf, V3D_SLCACTL_TVCCS) |
17357692c94SEric Anholt 		       V3D_SET_FIELD(0xf, V3D_SLCACTL_TDCCS) |
17457692c94SEric Anholt 		       V3D_SET_FIELD(0xf, V3D_SLCACTL_UCC) |
17557692c94SEric Anholt 		       V3D_SET_FIELD(0xf, V3D_SLCACTL_ICC));
17657692c94SEric Anholt }
17757692c94SEric Anholt 
17857692c94SEric Anholt /* Invalidates texture L2 cachelines */
17957692c94SEric Anholt static void
18057692c94SEric Anholt v3d_invalidate_l2t(struct v3d_dev *v3d, int core)
18157692c94SEric Anholt {
18257692c94SEric Anholt 	V3D_CORE_WRITE(core,
18357692c94SEric Anholt 		       V3D_CTL_L2TCACTL,
18457692c94SEric Anholt 		       V3D_L2TCACTL_L2TFLS |
18557692c94SEric Anholt 		       V3D_SET_FIELD(V3D_L2TCACTL_FLM_CLEAR, V3D_L2TCACTL_FLM));
18657692c94SEric Anholt 	if (wait_for(!(V3D_CORE_READ(core, V3D_CTL_L2TCACTL) &
18757692c94SEric Anholt 		       V3D_L2TCACTL_L2TFLS), 100)) {
18857692c94SEric Anholt 		DRM_ERROR("Timeout waiting for L2T invalidate\n");
18957692c94SEric Anholt 	}
19057692c94SEric Anholt }
19157692c94SEric Anholt 
19257692c94SEric Anholt void
19357692c94SEric Anholt v3d_invalidate_caches(struct v3d_dev *v3d)
19457692c94SEric Anholt {
19557692c94SEric Anholt 	v3d_flush_l3(v3d);
19657692c94SEric Anholt 
19757692c94SEric Anholt 	v3d_invalidate_l2(v3d, 0);
19857692c94SEric Anholt 	v3d_invalidate_slices(v3d, 0);
19957692c94SEric Anholt 	v3d_flush_l2t(v3d, 0);
20057692c94SEric Anholt }
20157692c94SEric Anholt 
20257692c94SEric Anholt void
20357692c94SEric Anholt v3d_flush_caches(struct v3d_dev *v3d)
20457692c94SEric Anholt {
20557692c94SEric Anholt 	v3d_invalidate_l1td(v3d, 0);
20657692c94SEric Anholt 	v3d_invalidate_l2t(v3d, 0);
20757692c94SEric Anholt }
20857692c94SEric Anholt 
20957692c94SEric Anholt static void
210*1584f16cSEric Anholt v3d_attach_object_fences(struct v3d_bo **bos, int bo_count,
211*1584f16cSEric Anholt 			 struct dma_fence *fence)
21257692c94SEric Anholt {
21357692c94SEric Anholt 	int i;
21457692c94SEric Anholt 
215*1584f16cSEric Anholt 	for (i = 0; i < bo_count; i++) {
21657692c94SEric Anholt 		/* XXX: Use shared fences for read-only objects. */
217*1584f16cSEric Anholt 		reservation_object_add_excl_fence(bos[i]->resv, fence);
21857692c94SEric Anholt 	}
21957692c94SEric Anholt }
22057692c94SEric Anholt 
22157692c94SEric Anholt static void
22257692c94SEric Anholt v3d_unlock_bo_reservations(struct drm_device *dev,
223*1584f16cSEric Anholt 			   struct v3d_bo **bos,
224*1584f16cSEric Anholt 			   int bo_count,
22557692c94SEric Anholt 			   struct ww_acquire_ctx *acquire_ctx)
22657692c94SEric Anholt {
22757692c94SEric Anholt 	int i;
22857692c94SEric Anholt 
229*1584f16cSEric Anholt 	for (i = 0; i < bo_count; i++)
230*1584f16cSEric Anholt 		ww_mutex_unlock(&bos[i]->resv->lock);
23157692c94SEric Anholt 
23257692c94SEric Anholt 	ww_acquire_fini(acquire_ctx);
23357692c94SEric Anholt }
23457692c94SEric Anholt 
23557692c94SEric Anholt /* Takes the reservation lock on all the BOs being referenced, so that
23657692c94SEric Anholt  * at queue submit time we can update the reservations.
23757692c94SEric Anholt  *
23857692c94SEric Anholt  * We don't lock the RCL the tile alloc/state BOs, or overflow memory
23957692c94SEric Anholt  * (all of which are on exec->unref_list).  They're entirely private
24057692c94SEric Anholt  * to v3d, so we don't attach dma-buf fences to them.
24157692c94SEric Anholt  */
24257692c94SEric Anholt static int
24357692c94SEric Anholt v3d_lock_bo_reservations(struct drm_device *dev,
244*1584f16cSEric Anholt 			 struct v3d_bo **bos,
245*1584f16cSEric Anholt 			 int bo_count,
24657692c94SEric Anholt 			 struct ww_acquire_ctx *acquire_ctx)
24757692c94SEric Anholt {
24857692c94SEric Anholt 	int contended_lock = -1;
24957692c94SEric Anholt 	int i, ret;
25057692c94SEric Anholt 
25157692c94SEric Anholt 	ww_acquire_init(acquire_ctx, &reservation_ww_class);
25257692c94SEric Anholt 
25357692c94SEric Anholt retry:
25457692c94SEric Anholt 	if (contended_lock != -1) {
255*1584f16cSEric Anholt 		struct v3d_bo *bo = bos[contended_lock];
2568f1cd826SEric Anholt 
25757692c94SEric Anholt 		ret = ww_mutex_lock_slow_interruptible(&bo->resv->lock,
25857692c94SEric Anholt 						       acquire_ctx);
25957692c94SEric Anholt 		if (ret) {
26057692c94SEric Anholt 			ww_acquire_done(acquire_ctx);
26157692c94SEric Anholt 			return ret;
26257692c94SEric Anholt 		}
26357692c94SEric Anholt 	}
26457692c94SEric Anholt 
265*1584f16cSEric Anholt 	for (i = 0; i < bo_count; i++) {
26657692c94SEric Anholt 		if (i == contended_lock)
26757692c94SEric Anholt 			continue;
26857692c94SEric Anholt 
269*1584f16cSEric Anholt 		ret = ww_mutex_lock_interruptible(&bos[i]->resv->lock,
2708f1cd826SEric Anholt 						  acquire_ctx);
27157692c94SEric Anholt 		if (ret) {
27257692c94SEric Anholt 			int j;
27357692c94SEric Anholt 
2748f1cd826SEric Anholt 			for (j = 0; j < i; j++)
275*1584f16cSEric Anholt 				ww_mutex_unlock(&bos[j]->resv->lock);
27657692c94SEric Anholt 
27757692c94SEric Anholt 			if (contended_lock != -1 && contended_lock >= i) {
278*1584f16cSEric Anholt 				struct v3d_bo *bo = bos[contended_lock];
27957692c94SEric Anholt 
28057692c94SEric Anholt 				ww_mutex_unlock(&bo->resv->lock);
28157692c94SEric Anholt 			}
28257692c94SEric Anholt 
28357692c94SEric Anholt 			if (ret == -EDEADLK) {
28457692c94SEric Anholt 				contended_lock = i;
28557692c94SEric Anholt 				goto retry;
28657692c94SEric Anholt 			}
28757692c94SEric Anholt 
28857692c94SEric Anholt 			ww_acquire_done(acquire_ctx);
28957692c94SEric Anholt 			return ret;
29057692c94SEric Anholt 		}
29157692c94SEric Anholt 	}
29257692c94SEric Anholt 
29357692c94SEric Anholt 	ww_acquire_done(acquire_ctx);
29457692c94SEric Anholt 
29557692c94SEric Anholt 	/* Reserve space for our shared (read-only) fence references,
29657692c94SEric Anholt 	 * before we commit the CL to the hardware.
29757692c94SEric Anholt 	 */
298*1584f16cSEric Anholt 	for (i = 0; i < bo_count; i++) {
299*1584f16cSEric Anholt 		ret = reservation_object_reserve_shared(bos[i]->resv, 1);
30057692c94SEric Anholt 		if (ret) {
301*1584f16cSEric Anholt 			v3d_unlock_bo_reservations(dev, bos, bo_count,
302*1584f16cSEric Anholt 						   acquire_ctx);
30357692c94SEric Anholt 			return ret;
30457692c94SEric Anholt 		}
30557692c94SEric Anholt 	}
30657692c94SEric Anholt 
30757692c94SEric Anholt 	return 0;
30857692c94SEric Anholt }
30957692c94SEric Anholt 
31057692c94SEric Anholt /**
31157692c94SEric Anholt  * v3d_cl_lookup_bos() - Sets up exec->bo[] with the GEM objects
31257692c94SEric Anholt  * referenced by the job.
31357692c94SEric Anholt  * @dev: DRM device
31457692c94SEric Anholt  * @file_priv: DRM file for this fd
31557692c94SEric Anholt  * @exec: V3D job being set up
31657692c94SEric Anholt  *
31757692c94SEric Anholt  * The command validator needs to reference BOs by their index within
31857692c94SEric Anholt  * the submitted job's BO list.  This does the validation of the job's
31957692c94SEric Anholt  * BO list and reference counting for the lifetime of the job.
32057692c94SEric Anholt  *
32157692c94SEric Anholt  * Note that this function doesn't need to unreference the BOs on
32257692c94SEric Anholt  * failure, because that will happen at v3d_exec_cleanup() time.
32357692c94SEric Anholt  */
32457692c94SEric Anholt static int
32557692c94SEric Anholt v3d_cl_lookup_bos(struct drm_device *dev,
32657692c94SEric Anholt 		  struct drm_file *file_priv,
32757692c94SEric Anholt 		  struct drm_v3d_submit_cl *args,
32857692c94SEric Anholt 		  struct v3d_exec_info *exec)
32957692c94SEric Anholt {
33057692c94SEric Anholt 	u32 *handles;
33157692c94SEric Anholt 	int ret = 0;
33257692c94SEric Anholt 	int i;
33357692c94SEric Anholt 
33457692c94SEric Anholt 	exec->bo_count = args->bo_handle_count;
33557692c94SEric Anholt 
33657692c94SEric Anholt 	if (!exec->bo_count) {
33757692c94SEric Anholt 		/* See comment on bo_index for why we have to check
33857692c94SEric Anholt 		 * this.
33957692c94SEric Anholt 		 */
34057692c94SEric Anholt 		DRM_DEBUG("Rendering requires BOs\n");
34157692c94SEric Anholt 		return -EINVAL;
34257692c94SEric Anholt 	}
34357692c94SEric Anholt 
34457692c94SEric Anholt 	exec->bo = kvmalloc_array(exec->bo_count,
34557692c94SEric Anholt 				  sizeof(struct drm_gem_cma_object *),
34657692c94SEric Anholt 				  GFP_KERNEL | __GFP_ZERO);
34757692c94SEric Anholt 	if (!exec->bo) {
34857692c94SEric Anholt 		DRM_DEBUG("Failed to allocate validated BO pointers\n");
34957692c94SEric Anholt 		return -ENOMEM;
35057692c94SEric Anholt 	}
35157692c94SEric Anholt 
35257692c94SEric Anholt 	handles = kvmalloc_array(exec->bo_count, sizeof(u32), GFP_KERNEL);
35357692c94SEric Anholt 	if (!handles) {
35457692c94SEric Anholt 		ret = -ENOMEM;
35557692c94SEric Anholt 		DRM_DEBUG("Failed to allocate incoming GEM handles\n");
35657692c94SEric Anholt 		goto fail;
35757692c94SEric Anholt 	}
35857692c94SEric Anholt 
35957692c94SEric Anholt 	if (copy_from_user(handles,
36057692c94SEric Anholt 			   (void __user *)(uintptr_t)args->bo_handles,
36157692c94SEric Anholt 			   exec->bo_count * sizeof(u32))) {
36257692c94SEric Anholt 		ret = -EFAULT;
36357692c94SEric Anholt 		DRM_DEBUG("Failed to copy in GEM handles\n");
36457692c94SEric Anholt 		goto fail;
36557692c94SEric Anholt 	}
36657692c94SEric Anholt 
36757692c94SEric Anholt 	spin_lock(&file_priv->table_lock);
36857692c94SEric Anholt 	for (i = 0; i < exec->bo_count; i++) {
36957692c94SEric Anholt 		struct drm_gem_object *bo = idr_find(&file_priv->object_idr,
37057692c94SEric Anholt 						     handles[i]);
37157692c94SEric Anholt 		if (!bo) {
37257692c94SEric Anholt 			DRM_DEBUG("Failed to look up GEM BO %d: %d\n",
37357692c94SEric Anholt 				  i, handles[i]);
37457692c94SEric Anholt 			ret = -ENOENT;
37557692c94SEric Anholt 			spin_unlock(&file_priv->table_lock);
37657692c94SEric Anholt 			goto fail;
37757692c94SEric Anholt 		}
37857692c94SEric Anholt 		drm_gem_object_get(bo);
37957692c94SEric Anholt 		exec->bo[i] = to_v3d_bo(bo);
38057692c94SEric Anholt 	}
38157692c94SEric Anholt 	spin_unlock(&file_priv->table_lock);
38257692c94SEric Anholt 
38357692c94SEric Anholt fail:
38457692c94SEric Anholt 	kvfree(handles);
38557692c94SEric Anholt 	return ret;
38657692c94SEric Anholt }
38757692c94SEric Anholt 
38857692c94SEric Anholt static void
38957692c94SEric Anholt v3d_exec_cleanup(struct kref *ref)
39057692c94SEric Anholt {
39157692c94SEric Anholt 	struct v3d_exec_info *exec = container_of(ref, struct v3d_exec_info,
39257692c94SEric Anholt 						  refcount);
39357692c94SEric Anholt 	struct v3d_dev *v3d = exec->v3d;
39457692c94SEric Anholt 	unsigned int i;
39557692c94SEric Anholt 	struct v3d_bo *bo, *save;
39657692c94SEric Anholt 
39757692c94SEric Anholt 	dma_fence_put(exec->bin.in_fence);
39857692c94SEric Anholt 	dma_fence_put(exec->render.in_fence);
39957692c94SEric Anholt 
40057692c94SEric Anholt 	dma_fence_put(exec->bin.done_fence);
40157692c94SEric Anholt 	dma_fence_put(exec->render.done_fence);
40257692c94SEric Anholt 
40357692c94SEric Anholt 	dma_fence_put(exec->bin_done_fence);
40434c2c4f6SEric Anholt 	dma_fence_put(exec->render_done_fence);
40557692c94SEric Anholt 
40657692c94SEric Anholt 	for (i = 0; i < exec->bo_count; i++)
40757692c94SEric Anholt 		drm_gem_object_put_unlocked(&exec->bo[i]->base);
40857692c94SEric Anholt 	kvfree(exec->bo);
40957692c94SEric Anholt 
41057692c94SEric Anholt 	list_for_each_entry_safe(bo, save, &exec->unref_list, unref_head) {
41157692c94SEric Anholt 		drm_gem_object_put_unlocked(&bo->base);
41257692c94SEric Anholt 	}
41357692c94SEric Anholt 
41457692c94SEric Anholt 	pm_runtime_mark_last_busy(v3d->dev);
41557692c94SEric Anholt 	pm_runtime_put_autosuspend(v3d->dev);
41657692c94SEric Anholt 
41757692c94SEric Anholt 	kfree(exec);
41857692c94SEric Anholt }
41957692c94SEric Anholt 
42057692c94SEric Anholt void v3d_exec_put(struct v3d_exec_info *exec)
42157692c94SEric Anholt {
42257692c94SEric Anholt 	kref_put(&exec->refcount, v3d_exec_cleanup);
42357692c94SEric Anholt }
42457692c94SEric Anholt 
425*1584f16cSEric Anholt static void
426*1584f16cSEric Anholt v3d_tfu_job_cleanup(struct kref *ref)
427*1584f16cSEric Anholt {
428*1584f16cSEric Anholt 	struct v3d_tfu_job *job = container_of(ref, struct v3d_tfu_job,
429*1584f16cSEric Anholt 					       refcount);
430*1584f16cSEric Anholt 	struct v3d_dev *v3d = job->v3d;
431*1584f16cSEric Anholt 	unsigned int i;
432*1584f16cSEric Anholt 
433*1584f16cSEric Anholt 	dma_fence_put(job->in_fence);
434*1584f16cSEric Anholt 	dma_fence_put(job->done_fence);
435*1584f16cSEric Anholt 
436*1584f16cSEric Anholt 	for (i = 0; i < ARRAY_SIZE(job->bo); i++) {
437*1584f16cSEric Anholt 		if (job->bo[i])
438*1584f16cSEric Anholt 			drm_gem_object_put_unlocked(&job->bo[i]->base);
439*1584f16cSEric Anholt 	}
440*1584f16cSEric Anholt 
441*1584f16cSEric Anholt 	pm_runtime_mark_last_busy(v3d->dev);
442*1584f16cSEric Anholt 	pm_runtime_put_autosuspend(v3d->dev);
443*1584f16cSEric Anholt 
444*1584f16cSEric Anholt 	kfree(job);
445*1584f16cSEric Anholt }
446*1584f16cSEric Anholt 
447*1584f16cSEric Anholt void v3d_tfu_job_put(struct v3d_tfu_job *job)
448*1584f16cSEric Anholt {
449*1584f16cSEric Anholt 	kref_put(&job->refcount, v3d_tfu_job_cleanup);
450*1584f16cSEric Anholt }
451*1584f16cSEric Anholt 
45257692c94SEric Anholt int
45357692c94SEric Anholt v3d_wait_bo_ioctl(struct drm_device *dev, void *data,
45457692c94SEric Anholt 		  struct drm_file *file_priv)
45557692c94SEric Anholt {
45657692c94SEric Anholt 	int ret;
45757692c94SEric Anholt 	struct drm_v3d_wait_bo *args = data;
45857692c94SEric Anholt 	struct drm_gem_object *gem_obj;
45957692c94SEric Anholt 	struct v3d_bo *bo;
46057692c94SEric Anholt 	ktime_t start = ktime_get();
46157692c94SEric Anholt 	u64 delta_ns;
46257692c94SEric Anholt 	unsigned long timeout_jiffies =
46357692c94SEric Anholt 		nsecs_to_jiffies_timeout(args->timeout_ns);
46457692c94SEric Anholt 
46557692c94SEric Anholt 	if (args->pad != 0)
46657692c94SEric Anholt 		return -EINVAL;
46757692c94SEric Anholt 
46857692c94SEric Anholt 	gem_obj = drm_gem_object_lookup(file_priv, args->handle);
46957692c94SEric Anholt 	if (!gem_obj) {
47057692c94SEric Anholt 		DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
47157692c94SEric Anholt 		return -EINVAL;
47257692c94SEric Anholt 	}
47357692c94SEric Anholt 	bo = to_v3d_bo(gem_obj);
47457692c94SEric Anholt 
47557692c94SEric Anholt 	ret = reservation_object_wait_timeout_rcu(bo->resv,
47657692c94SEric Anholt 						  true, true,
47757692c94SEric Anholt 						  timeout_jiffies);
47857692c94SEric Anholt 
47957692c94SEric Anholt 	if (ret == 0)
48057692c94SEric Anholt 		ret = -ETIME;
48157692c94SEric Anholt 	else if (ret > 0)
48257692c94SEric Anholt 		ret = 0;
48357692c94SEric Anholt 
48457692c94SEric Anholt 	/* Decrement the user's timeout, in case we got interrupted
48557692c94SEric Anholt 	 * such that the ioctl will be restarted.
48657692c94SEric Anholt 	 */
48757692c94SEric Anholt 	delta_ns = ktime_to_ns(ktime_sub(ktime_get(), start));
48857692c94SEric Anholt 	if (delta_ns < args->timeout_ns)
48957692c94SEric Anholt 		args->timeout_ns -= delta_ns;
49057692c94SEric Anholt 	else
49157692c94SEric Anholt 		args->timeout_ns = 0;
49257692c94SEric Anholt 
49357692c94SEric Anholt 	/* Asked to wait beyond the jiffie/scheduler precision? */
49457692c94SEric Anholt 	if (ret == -ETIME && args->timeout_ns)
49557692c94SEric Anholt 		ret = -EAGAIN;
49657692c94SEric Anholt 
49757692c94SEric Anholt 	drm_gem_object_put_unlocked(gem_obj);
49857692c94SEric Anholt 
49957692c94SEric Anholt 	return ret;
50057692c94SEric Anholt }
50157692c94SEric Anholt 
50257692c94SEric Anholt /**
50357692c94SEric Anholt  * v3d_submit_cl_ioctl() - Submits a job (frame) to the V3D.
50457692c94SEric Anholt  * @dev: DRM device
50557692c94SEric Anholt  * @data: ioctl argument
50657692c94SEric Anholt  * @file_priv: DRM file for this fd
50757692c94SEric Anholt  *
50857692c94SEric Anholt  * This is the main entrypoint for userspace to submit a 3D frame to
50957692c94SEric Anholt  * the GPU.  Userspace provides the binner command list (if
51057692c94SEric Anholt  * applicable), and the kernel sets up the render command list to draw
51157692c94SEric Anholt  * to the framebuffer described in the ioctl, using the command lists
51257692c94SEric Anholt  * that the 3D engine's binner will produce.
51357692c94SEric Anholt  */
51457692c94SEric Anholt int
51557692c94SEric Anholt v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
51657692c94SEric Anholt 		    struct drm_file *file_priv)
51757692c94SEric Anholt {
51857692c94SEric Anholt 	struct v3d_dev *v3d = to_v3d_dev(dev);
51957692c94SEric Anholt 	struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
52057692c94SEric Anholt 	struct drm_v3d_submit_cl *args = data;
52157692c94SEric Anholt 	struct v3d_exec_info *exec;
52257692c94SEric Anholt 	struct ww_acquire_ctx acquire_ctx;
52357692c94SEric Anholt 	struct drm_syncobj *sync_out;
52457692c94SEric Anholt 	int ret = 0;
52557692c94SEric Anholt 
52657692c94SEric Anholt 	if (args->pad != 0) {
52757692c94SEric Anholt 		DRM_INFO("pad must be zero: %d\n", args->pad);
52857692c94SEric Anholt 		return -EINVAL;
52957692c94SEric Anholt 	}
53057692c94SEric Anholt 
53157692c94SEric Anholt 	exec = kcalloc(1, sizeof(*exec), GFP_KERNEL);
53257692c94SEric Anholt 	if (!exec)
53357692c94SEric Anholt 		return -ENOMEM;
53457692c94SEric Anholt 
53557692c94SEric Anholt 	ret = pm_runtime_get_sync(v3d->dev);
53657692c94SEric Anholt 	if (ret < 0) {
53757692c94SEric Anholt 		kfree(exec);
53857692c94SEric Anholt 		return ret;
53957692c94SEric Anholt 	}
54057692c94SEric Anholt 
54157692c94SEric Anholt 	kref_init(&exec->refcount);
54257692c94SEric Anholt 
54357692c94SEric Anholt 	ret = drm_syncobj_find_fence(file_priv, args->in_sync_bcl,
544649fdce2SChunming Zhou 				     0, 0, &exec->bin.in_fence);
54557692c94SEric Anholt 	if (ret == -EINVAL)
54657692c94SEric Anholt 		goto fail;
54757692c94SEric Anholt 
54857692c94SEric Anholt 	ret = drm_syncobj_find_fence(file_priv, args->in_sync_rcl,
549649fdce2SChunming Zhou 				     0, 0, &exec->render.in_fence);
55057692c94SEric Anholt 	if (ret == -EINVAL)
55157692c94SEric Anholt 		goto fail;
55257692c94SEric Anholt 
55357692c94SEric Anholt 	exec->qma = args->qma;
55457692c94SEric Anholt 	exec->qms = args->qms;
55557692c94SEric Anholt 	exec->qts = args->qts;
55657692c94SEric Anholt 	exec->bin.exec = exec;
55757692c94SEric Anholt 	exec->bin.start = args->bcl_start;
55857692c94SEric Anholt 	exec->bin.end = args->bcl_end;
55957692c94SEric Anholt 	exec->render.exec = exec;
56057692c94SEric Anholt 	exec->render.start = args->rcl_start;
56157692c94SEric Anholt 	exec->render.end = args->rcl_end;
56257692c94SEric Anholt 	exec->v3d = v3d;
56357692c94SEric Anholt 	INIT_LIST_HEAD(&exec->unref_list);
56457692c94SEric Anholt 
56557692c94SEric Anholt 	ret = v3d_cl_lookup_bos(dev, file_priv, args, exec);
56657692c94SEric Anholt 	if (ret)
56757692c94SEric Anholt 		goto fail;
56857692c94SEric Anholt 
569*1584f16cSEric Anholt 	ret = v3d_lock_bo_reservations(dev, exec->bo, exec->bo_count,
570*1584f16cSEric Anholt 				       &acquire_ctx);
57157692c94SEric Anholt 	if (ret)
57257692c94SEric Anholt 		goto fail;
57357692c94SEric Anholt 
5747122b68bSEric Anholt 	mutex_lock(&v3d->sched_lock);
57557692c94SEric Anholt 	if (exec->bin.start != exec->bin.end) {
57657692c94SEric Anholt 		ret = drm_sched_job_init(&exec->bin.base,
57757692c94SEric Anholt 					 &v3d_priv->sched_entity[V3D_BIN],
57857692c94SEric Anholt 					 v3d_priv);
57957692c94SEric Anholt 		if (ret)
58057692c94SEric Anholt 			goto fail_unreserve;
58157692c94SEric Anholt 
58257692c94SEric Anholt 		exec->bin_done_fence =
58357692c94SEric Anholt 			dma_fence_get(&exec->bin.base.s_fence->finished);
58457692c94SEric Anholt 
58557692c94SEric Anholt 		kref_get(&exec->refcount); /* put by scheduler job completion */
58657692c94SEric Anholt 		drm_sched_entity_push_job(&exec->bin.base,
58757692c94SEric Anholt 					  &v3d_priv->sched_entity[V3D_BIN]);
58857692c94SEric Anholt 	}
58957692c94SEric Anholt 
59057692c94SEric Anholt 	ret = drm_sched_job_init(&exec->render.base,
59157692c94SEric Anholt 				 &v3d_priv->sched_entity[V3D_RENDER],
59257692c94SEric Anholt 				 v3d_priv);
59357692c94SEric Anholt 	if (ret)
59457692c94SEric Anholt 		goto fail_unreserve;
59557692c94SEric Anholt 
59634c2c4f6SEric Anholt 	exec->render_done_fence =
59734c2c4f6SEric Anholt 		dma_fence_get(&exec->render.base.s_fence->finished);
59834c2c4f6SEric Anholt 
59957692c94SEric Anholt 	kref_get(&exec->refcount); /* put by scheduler job completion */
60057692c94SEric Anholt 	drm_sched_entity_push_job(&exec->render.base,
60157692c94SEric Anholt 				  &v3d_priv->sched_entity[V3D_RENDER]);
6027122b68bSEric Anholt 	mutex_unlock(&v3d->sched_lock);
60357692c94SEric Anholt 
604*1584f16cSEric Anholt 	v3d_attach_object_fences(exec->bo, exec->bo_count,
605*1584f16cSEric Anholt 				 exec->render_done_fence);
60657692c94SEric Anholt 
607*1584f16cSEric Anholt 	v3d_unlock_bo_reservations(dev, exec->bo, exec->bo_count, &acquire_ctx);
60857692c94SEric Anholt 
60957692c94SEric Anholt 	/* Update the return sync object for the */
61057692c94SEric Anholt 	sync_out = drm_syncobj_find(file_priv, args->out_sync);
61157692c94SEric Anholt 	if (sync_out) {
6129a09a423SChunming Zhou 		drm_syncobj_replace_fence(sync_out, 0,
61334c2c4f6SEric Anholt 					  exec->render_done_fence);
61457692c94SEric Anholt 		drm_syncobj_put(sync_out);
61557692c94SEric Anholt 	}
61657692c94SEric Anholt 
61757692c94SEric Anholt 	v3d_exec_put(exec);
61857692c94SEric Anholt 
61957692c94SEric Anholt 	return 0;
62057692c94SEric Anholt 
62157692c94SEric Anholt fail_unreserve:
6227122b68bSEric Anholt 	mutex_unlock(&v3d->sched_lock);
623*1584f16cSEric Anholt 	v3d_unlock_bo_reservations(dev, exec->bo, exec->bo_count, &acquire_ctx);
62457692c94SEric Anholt fail:
62557692c94SEric Anholt 	v3d_exec_put(exec);
62657692c94SEric Anholt 
62757692c94SEric Anholt 	return ret;
62857692c94SEric Anholt }
62957692c94SEric Anholt 
630*1584f16cSEric Anholt /**
631*1584f16cSEric Anholt  * v3d_submit_tfu_ioctl() - Submits a TFU (texture formatting) job to the V3D.
632*1584f16cSEric Anholt  * @dev: DRM device
633*1584f16cSEric Anholt  * @data: ioctl argument
634*1584f16cSEric Anholt  * @file_priv: DRM file for this fd
635*1584f16cSEric Anholt  *
636*1584f16cSEric Anholt  * Userspace provides the register setup for the TFU, which we don't
637*1584f16cSEric Anholt  * need to validate since the TFU is behind the MMU.
638*1584f16cSEric Anholt  */
639*1584f16cSEric Anholt int
640*1584f16cSEric Anholt v3d_submit_tfu_ioctl(struct drm_device *dev, void *data,
641*1584f16cSEric Anholt 		     struct drm_file *file_priv)
642*1584f16cSEric Anholt {
643*1584f16cSEric Anholt 	struct v3d_dev *v3d = to_v3d_dev(dev);
644*1584f16cSEric Anholt 	struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
645*1584f16cSEric Anholt 	struct drm_v3d_submit_tfu *args = data;
646*1584f16cSEric Anholt 	struct v3d_tfu_job *job;
647*1584f16cSEric Anholt 	struct ww_acquire_ctx acquire_ctx;
648*1584f16cSEric Anholt 	struct drm_syncobj *sync_out;
649*1584f16cSEric Anholt 	struct dma_fence *sched_done_fence;
650*1584f16cSEric Anholt 	int ret = 0;
651*1584f16cSEric Anholt 	int bo_count;
652*1584f16cSEric Anholt 
653*1584f16cSEric Anholt 	job = kcalloc(1, sizeof(*job), GFP_KERNEL);
654*1584f16cSEric Anholt 	if (!job)
655*1584f16cSEric Anholt 		return -ENOMEM;
656*1584f16cSEric Anholt 
657*1584f16cSEric Anholt 	ret = pm_runtime_get_sync(v3d->dev);
658*1584f16cSEric Anholt 	if (ret < 0) {
659*1584f16cSEric Anholt 		kfree(job);
660*1584f16cSEric Anholt 		return ret;
661*1584f16cSEric Anholt 	}
662*1584f16cSEric Anholt 
663*1584f16cSEric Anholt 	kref_init(&job->refcount);
664*1584f16cSEric Anholt 
665*1584f16cSEric Anholt 	ret = drm_syncobj_find_fence(file_priv, args->in_sync,
666*1584f16cSEric Anholt 				     0, 0, &job->in_fence);
667*1584f16cSEric Anholt 	if (ret == -EINVAL)
668*1584f16cSEric Anholt 		goto fail;
669*1584f16cSEric Anholt 
670*1584f16cSEric Anholt 	job->args = *args;
671*1584f16cSEric Anholt 	job->v3d = v3d;
672*1584f16cSEric Anholt 
673*1584f16cSEric Anholt 	spin_lock(&file_priv->table_lock);
674*1584f16cSEric Anholt 	for (bo_count = 0; bo_count < ARRAY_SIZE(job->bo); bo_count++) {
675*1584f16cSEric Anholt 		struct drm_gem_object *bo;
676*1584f16cSEric Anholt 
677*1584f16cSEric Anholt 		if (!args->bo_handles[bo_count])
678*1584f16cSEric Anholt 			break;
679*1584f16cSEric Anholt 
680*1584f16cSEric Anholt 		bo = idr_find(&file_priv->object_idr,
681*1584f16cSEric Anholt 			      args->bo_handles[bo_count]);
682*1584f16cSEric Anholt 		if (!bo) {
683*1584f16cSEric Anholt 			DRM_DEBUG("Failed to look up GEM BO %d: %d\n",
684*1584f16cSEric Anholt 				  bo_count, args->bo_handles[bo_count]);
685*1584f16cSEric Anholt 			ret = -ENOENT;
686*1584f16cSEric Anholt 			spin_unlock(&file_priv->table_lock);
687*1584f16cSEric Anholt 			goto fail;
688*1584f16cSEric Anholt 		}
689*1584f16cSEric Anholt 		drm_gem_object_get(bo);
690*1584f16cSEric Anholt 		job->bo[bo_count] = to_v3d_bo(bo);
691*1584f16cSEric Anholt 	}
692*1584f16cSEric Anholt 	spin_unlock(&file_priv->table_lock);
693*1584f16cSEric Anholt 
694*1584f16cSEric Anholt 	ret = v3d_lock_bo_reservations(dev, job->bo, bo_count, &acquire_ctx);
695*1584f16cSEric Anholt 	if (ret)
696*1584f16cSEric Anholt 		goto fail;
697*1584f16cSEric Anholt 
698*1584f16cSEric Anholt 	mutex_lock(&v3d->sched_lock);
699*1584f16cSEric Anholt 	ret = drm_sched_job_init(&job->base,
700*1584f16cSEric Anholt 				 &v3d_priv->sched_entity[V3D_TFU],
701*1584f16cSEric Anholt 				 v3d_priv);
702*1584f16cSEric Anholt 	if (ret)
703*1584f16cSEric Anholt 		goto fail_unreserve;
704*1584f16cSEric Anholt 
705*1584f16cSEric Anholt 	sched_done_fence = dma_fence_get(&job->base.s_fence->finished);
706*1584f16cSEric Anholt 
707*1584f16cSEric Anholt 	kref_get(&job->refcount); /* put by scheduler job completion */
708*1584f16cSEric Anholt 	drm_sched_entity_push_job(&job->base, &v3d_priv->sched_entity[V3D_TFU]);
709*1584f16cSEric Anholt 	mutex_unlock(&v3d->sched_lock);
710*1584f16cSEric Anholt 
711*1584f16cSEric Anholt 	v3d_attach_object_fences(job->bo, bo_count, sched_done_fence);
712*1584f16cSEric Anholt 
713*1584f16cSEric Anholt 	v3d_unlock_bo_reservations(dev, job->bo, bo_count, &acquire_ctx);
714*1584f16cSEric Anholt 
715*1584f16cSEric Anholt 	/* Update the return sync object */
716*1584f16cSEric Anholt 	sync_out = drm_syncobj_find(file_priv, args->out_sync);
717*1584f16cSEric Anholt 	if (sync_out) {
718*1584f16cSEric Anholt 		drm_syncobj_replace_fence(sync_out, 0, sched_done_fence);
719*1584f16cSEric Anholt 		drm_syncobj_put(sync_out);
720*1584f16cSEric Anholt 	}
721*1584f16cSEric Anholt 	dma_fence_put(sched_done_fence);
722*1584f16cSEric Anholt 
723*1584f16cSEric Anholt 	v3d_tfu_job_put(job);
724*1584f16cSEric Anholt 
725*1584f16cSEric Anholt 	return 0;
726*1584f16cSEric Anholt 
727*1584f16cSEric Anholt fail_unreserve:
728*1584f16cSEric Anholt 	mutex_unlock(&v3d->sched_lock);
729*1584f16cSEric Anholt 	v3d_unlock_bo_reservations(dev, job->bo, bo_count, &acquire_ctx);
730*1584f16cSEric Anholt fail:
731*1584f16cSEric Anholt 	v3d_tfu_job_put(job);
732*1584f16cSEric Anholt 
733*1584f16cSEric Anholt 	return ret;
734*1584f16cSEric Anholt }
735*1584f16cSEric Anholt 
73657692c94SEric Anholt int
73757692c94SEric Anholt v3d_gem_init(struct drm_device *dev)
73857692c94SEric Anholt {
73957692c94SEric Anholt 	struct v3d_dev *v3d = to_v3d_dev(dev);
74057692c94SEric Anholt 	u32 pt_size = 4096 * 1024;
74157692c94SEric Anholt 	int ret, i;
74257692c94SEric Anholt 
74357692c94SEric Anholt 	for (i = 0; i < V3D_MAX_QUEUES; i++)
74457692c94SEric Anholt 		v3d->queue[i].fence_context = dma_fence_context_alloc(1);
74557692c94SEric Anholt 
74657692c94SEric Anholt 	spin_lock_init(&v3d->mm_lock);
74757692c94SEric Anholt 	spin_lock_init(&v3d->job_lock);
74857692c94SEric Anholt 	mutex_init(&v3d->bo_lock);
74957692c94SEric Anholt 	mutex_init(&v3d->reset_lock);
7507122b68bSEric Anholt 	mutex_init(&v3d->sched_lock);
75157692c94SEric Anholt 
75257692c94SEric Anholt 	/* Note: We don't allocate address 0.  Various bits of HW
75357692c94SEric Anholt 	 * treat 0 as special, such as the occlusion query counters
75457692c94SEric Anholt 	 * where 0 means "disabled".
75557692c94SEric Anholt 	 */
75657692c94SEric Anholt 	drm_mm_init(&v3d->mm, 1, pt_size / sizeof(u32) - 1);
75757692c94SEric Anholt 
75857692c94SEric Anholt 	v3d->pt = dma_alloc_wc(v3d->dev, pt_size,
75957692c94SEric Anholt 			       &v3d->pt_paddr,
76057692c94SEric Anholt 			       GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);
76157692c94SEric Anholt 	if (!v3d->pt) {
76257692c94SEric Anholt 		drm_mm_takedown(&v3d->mm);
76357692c94SEric Anholt 		dev_err(v3d->dev,
76457692c94SEric Anholt 			"Failed to allocate page tables. "
76557692c94SEric Anholt 			"Please ensure you have CMA enabled.\n");
76657692c94SEric Anholt 		return -ENOMEM;
76757692c94SEric Anholt 	}
76857692c94SEric Anholt 
76957692c94SEric Anholt 	v3d_init_hw_state(v3d);
77057692c94SEric Anholt 	v3d_mmu_set_page_table(v3d);
77157692c94SEric Anholt 
77257692c94SEric Anholt 	ret = v3d_sched_init(v3d);
77357692c94SEric Anholt 	if (ret) {
77457692c94SEric Anholt 		drm_mm_takedown(&v3d->mm);
77557692c94SEric Anholt 		dma_free_coherent(v3d->dev, 4096 * 1024, (void *)v3d->pt,
77657692c94SEric Anholt 				  v3d->pt_paddr);
77757692c94SEric Anholt 	}
77857692c94SEric Anholt 
77957692c94SEric Anholt 	return 0;
78057692c94SEric Anholt }
78157692c94SEric Anholt 
78257692c94SEric Anholt void
78357692c94SEric Anholt v3d_gem_destroy(struct drm_device *dev)
78457692c94SEric Anholt {
78557692c94SEric Anholt 	struct v3d_dev *v3d = to_v3d_dev(dev);
78657692c94SEric Anholt 
78757692c94SEric Anholt 	v3d_sched_fini(v3d);
78857692c94SEric Anholt 
78957692c94SEric Anholt 	/* Waiting for exec to finish would need to be done before
79057692c94SEric Anholt 	 * unregistering V3D.
79157692c94SEric Anholt 	 */
79214d1d190SEric Anholt 	WARN_ON(v3d->bin_job);
79314d1d190SEric Anholt 	WARN_ON(v3d->render_job);
79457692c94SEric Anholt 
79557692c94SEric Anholt 	drm_mm_takedown(&v3d->mm);
79657692c94SEric Anholt 
79757692c94SEric Anholt 	dma_free_coherent(v3d->dev, 4096 * 1024, (void *)v3d->pt, v3d->pt_paddr);
79857692c94SEric Anholt }
799