xref: /openbmc/linux/drivers/gpu/drm/v3d/v3d_gem.c (revision 57692c94dcbe99a1e0444409a3da13fb3443562c)
1*57692c94SEric Anholt // SPDX-License-Identifier: GPL-2.0+
2*57692c94SEric Anholt /* Copyright (C) 2014-2018 Broadcom */
3*57692c94SEric Anholt 
4*57692c94SEric Anholt #include <drm/drmP.h>
5*57692c94SEric Anholt #include <drm/drm_syncobj.h>
6*57692c94SEric Anholt #include <linux/module.h>
7*57692c94SEric Anholt #include <linux/platform_device.h>
8*57692c94SEric Anholt #include <linux/pm_runtime.h>
9*57692c94SEric Anholt #include <linux/device.h>
10*57692c94SEric Anholt #include <linux/io.h>
11*57692c94SEric Anholt #include <linux/sched/signal.h>
12*57692c94SEric Anholt 
13*57692c94SEric Anholt #include "uapi/drm/v3d_drm.h"
14*57692c94SEric Anholt #include "v3d_drv.h"
15*57692c94SEric Anholt #include "v3d_regs.h"
16*57692c94SEric Anholt #include "v3d_trace.h"
17*57692c94SEric Anholt 
18*57692c94SEric Anholt static void
19*57692c94SEric Anholt v3d_init_core(struct v3d_dev *v3d, int core)
20*57692c94SEric Anholt {
21*57692c94SEric Anholt 	/* Set OVRTMUOUT, which means that the texture sampler uniform
22*57692c94SEric Anholt 	 * configuration's tmu output type field is used, instead of
23*57692c94SEric Anholt 	 * using the hardware default behavior based on the texture
24*57692c94SEric Anholt 	 * type.  If you want the default behavior, you can still put
25*57692c94SEric Anholt 	 * "2" in the indirect texture state's output_type field.
26*57692c94SEric Anholt 	 */
27*57692c94SEric Anholt 	V3D_CORE_WRITE(core, V3D_CTL_MISCCFG, V3D_MISCCFG_OVRTMUOUT);
28*57692c94SEric Anholt 
29*57692c94SEric Anholt 	/* Whenever we flush the L2T cache, we always want to flush
30*57692c94SEric Anholt 	 * the whole thing.
31*57692c94SEric Anholt 	 */
32*57692c94SEric Anholt 	V3D_CORE_WRITE(core, V3D_CTL_L2TFLSTA, 0);
33*57692c94SEric Anholt 	V3D_CORE_WRITE(core, V3D_CTL_L2TFLEND, ~0);
34*57692c94SEric Anholt }
35*57692c94SEric Anholt 
36*57692c94SEric Anholt /* Sets invariant state for the HW. */
37*57692c94SEric Anholt static void
38*57692c94SEric Anholt v3d_init_hw_state(struct v3d_dev *v3d)
39*57692c94SEric Anholt {
40*57692c94SEric Anholt 	v3d_init_core(v3d, 0);
41*57692c94SEric Anholt }
42*57692c94SEric Anholt 
43*57692c94SEric Anholt static void
44*57692c94SEric Anholt v3d_idle_axi(struct v3d_dev *v3d, int core)
45*57692c94SEric Anholt {
46*57692c94SEric Anholt 	V3D_CORE_WRITE(core, V3D_GMP_CFG, V3D_GMP_CFG_STOP_REQ);
47*57692c94SEric Anholt 
48*57692c94SEric Anholt 	if (wait_for((V3D_CORE_READ(core, V3D_GMP_STATUS) &
49*57692c94SEric Anholt 		      (V3D_GMP_STATUS_RD_COUNT_MASK |
50*57692c94SEric Anholt 		       V3D_GMP_STATUS_WR_COUNT_MASK |
51*57692c94SEric Anholt 		       V3D_GMP_STATUS_CFG_BUSY)) == 0, 100)) {
52*57692c94SEric Anholt 		DRM_ERROR("Failed to wait for safe GMP shutdown\n");
53*57692c94SEric Anholt 	}
54*57692c94SEric Anholt }
55*57692c94SEric Anholt 
56*57692c94SEric Anholt static void
57*57692c94SEric Anholt v3d_idle_gca(struct v3d_dev *v3d)
58*57692c94SEric Anholt {
59*57692c94SEric Anholt 	if (v3d->ver >= 41)
60*57692c94SEric Anholt 		return;
61*57692c94SEric Anholt 
62*57692c94SEric Anholt 	V3D_GCA_WRITE(V3D_GCA_SAFE_SHUTDOWN, V3D_GCA_SAFE_SHUTDOWN_EN);
63*57692c94SEric Anholt 
64*57692c94SEric Anholt 	if (wait_for((V3D_GCA_READ(V3D_GCA_SAFE_SHUTDOWN_ACK) &
65*57692c94SEric Anholt 		      V3D_GCA_SAFE_SHUTDOWN_ACK_ACKED) ==
66*57692c94SEric Anholt 		     V3D_GCA_SAFE_SHUTDOWN_ACK_ACKED, 100)) {
67*57692c94SEric Anholt 		DRM_ERROR("Failed to wait for safe GCA shutdown\n");
68*57692c94SEric Anholt 	}
69*57692c94SEric Anholt }
70*57692c94SEric Anholt 
71*57692c94SEric Anholt static void
72*57692c94SEric Anholt v3d_reset_v3d(struct v3d_dev *v3d)
73*57692c94SEric Anholt {
74*57692c94SEric Anholt 	int version = V3D_BRIDGE_READ(V3D_TOP_GR_BRIDGE_REVISION);
75*57692c94SEric Anholt 
76*57692c94SEric Anholt 	if (V3D_GET_FIELD(version, V3D_TOP_GR_BRIDGE_MAJOR) == 2) {
77*57692c94SEric Anholt 		V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_0,
78*57692c94SEric Anholt 				 V3D_TOP_GR_BRIDGE_SW_INIT_0_V3D_CLK_108_SW_INIT);
79*57692c94SEric Anholt 		V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_0, 0);
80*57692c94SEric Anholt 
81*57692c94SEric Anholt 		/* GFXH-1383: The SW_INIT may cause a stray write to address 0
82*57692c94SEric Anholt 		 * of the unit, so reset it to its power-on value here.
83*57692c94SEric Anholt 		 */
84*57692c94SEric Anholt 		V3D_WRITE(V3D_HUB_AXICFG, V3D_HUB_AXICFG_MAX_LEN_MASK);
85*57692c94SEric Anholt 	} else {
86*57692c94SEric Anholt 		WARN_ON_ONCE(V3D_GET_FIELD(version,
87*57692c94SEric Anholt 					   V3D_TOP_GR_BRIDGE_MAJOR) != 7);
88*57692c94SEric Anholt 		V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_1,
89*57692c94SEric Anholt 				 V3D_TOP_GR_BRIDGE_SW_INIT_1_V3D_CLK_108_SW_INIT);
90*57692c94SEric Anholt 		V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_1, 0);
91*57692c94SEric Anholt 	}
92*57692c94SEric Anholt 
93*57692c94SEric Anholt 	v3d_init_hw_state(v3d);
94*57692c94SEric Anholt }
95*57692c94SEric Anholt 
96*57692c94SEric Anholt void
97*57692c94SEric Anholt v3d_reset(struct v3d_dev *v3d)
98*57692c94SEric Anholt {
99*57692c94SEric Anholt 	struct drm_device *dev = &v3d->drm;
100*57692c94SEric Anholt 
101*57692c94SEric Anholt 	DRM_ERROR("Resetting GPU.\n");
102*57692c94SEric Anholt 	trace_v3d_reset_begin(dev);
103*57692c94SEric Anholt 
104*57692c94SEric Anholt 	/* XXX: only needed for safe powerdown, not reset. */
105*57692c94SEric Anholt 	if (false)
106*57692c94SEric Anholt 		v3d_idle_axi(v3d, 0);
107*57692c94SEric Anholt 
108*57692c94SEric Anholt 	v3d_idle_gca(v3d);
109*57692c94SEric Anholt 	v3d_reset_v3d(v3d);
110*57692c94SEric Anholt 
111*57692c94SEric Anholt 	v3d_mmu_set_page_table(v3d);
112*57692c94SEric Anholt 	v3d_irq_reset(v3d);
113*57692c94SEric Anholt 
114*57692c94SEric Anholt 	trace_v3d_reset_end(dev);
115*57692c94SEric Anholt }
116*57692c94SEric Anholt 
117*57692c94SEric Anholt static void
118*57692c94SEric Anholt v3d_flush_l3(struct v3d_dev *v3d)
119*57692c94SEric Anholt {
120*57692c94SEric Anholt 	if (v3d->ver < 41) {
121*57692c94SEric Anholt 		u32 gca_ctrl = V3D_GCA_READ(V3D_GCA_CACHE_CTRL);
122*57692c94SEric Anholt 
123*57692c94SEric Anholt 		V3D_GCA_WRITE(V3D_GCA_CACHE_CTRL,
124*57692c94SEric Anholt 			      gca_ctrl | V3D_GCA_CACHE_CTRL_FLUSH);
125*57692c94SEric Anholt 
126*57692c94SEric Anholt 		if (v3d->ver < 33) {
127*57692c94SEric Anholt 			V3D_GCA_WRITE(V3D_GCA_CACHE_CTRL,
128*57692c94SEric Anholt 				      gca_ctrl & ~V3D_GCA_CACHE_CTRL_FLUSH);
129*57692c94SEric Anholt 		}
130*57692c94SEric Anholt 	}
131*57692c94SEric Anholt }
132*57692c94SEric Anholt 
133*57692c94SEric Anholt /* Invalidates the (read-only) L2 cache. */
134*57692c94SEric Anholt static void
135*57692c94SEric Anholt v3d_invalidate_l2(struct v3d_dev *v3d, int core)
136*57692c94SEric Anholt {
137*57692c94SEric Anholt 	V3D_CORE_WRITE(core, V3D_CTL_L2CACTL,
138*57692c94SEric Anholt 		       V3D_L2CACTL_L2CCLR |
139*57692c94SEric Anholt 		       V3D_L2CACTL_L2CENA);
140*57692c94SEric Anholt }
141*57692c94SEric Anholt 
142*57692c94SEric Anholt static void
143*57692c94SEric Anholt v3d_invalidate_l1td(struct v3d_dev *v3d, int core)
144*57692c94SEric Anholt {
145*57692c94SEric Anholt 	V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL, V3D_L2TCACTL_TMUWCF);
146*57692c94SEric Anholt 	if (wait_for(!(V3D_CORE_READ(core, V3D_CTL_L2TCACTL) &
147*57692c94SEric Anholt 		       V3D_L2TCACTL_L2TFLS), 100)) {
148*57692c94SEric Anholt 		DRM_ERROR("Timeout waiting for L1T write combiner flush\n");
149*57692c94SEric Anholt 	}
150*57692c94SEric Anholt }
151*57692c94SEric Anholt 
152*57692c94SEric Anholt /* Invalidates texture L2 cachelines */
153*57692c94SEric Anholt static void
154*57692c94SEric Anholt v3d_flush_l2t(struct v3d_dev *v3d, int core)
155*57692c94SEric Anholt {
156*57692c94SEric Anholt 	v3d_invalidate_l1td(v3d, core);
157*57692c94SEric Anholt 
158*57692c94SEric Anholt 	V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL,
159*57692c94SEric Anholt 		       V3D_L2TCACTL_L2TFLS |
160*57692c94SEric Anholt 		       V3D_SET_FIELD(V3D_L2TCACTL_FLM_FLUSH, V3D_L2TCACTL_FLM));
161*57692c94SEric Anholt 	if (wait_for(!(V3D_CORE_READ(core, V3D_CTL_L2TCACTL) &
162*57692c94SEric Anholt 		       V3D_L2TCACTL_L2TFLS), 100)) {
163*57692c94SEric Anholt 		DRM_ERROR("Timeout waiting for L2T flush\n");
164*57692c94SEric Anholt 	}
165*57692c94SEric Anholt }
166*57692c94SEric Anholt 
167*57692c94SEric Anholt /* Invalidates the slice caches.  These are read-only caches. */
168*57692c94SEric Anholt static void
169*57692c94SEric Anholt v3d_invalidate_slices(struct v3d_dev *v3d, int core)
170*57692c94SEric Anholt {
171*57692c94SEric Anholt 	V3D_CORE_WRITE(core, V3D_CTL_SLCACTL,
172*57692c94SEric Anholt 		       V3D_SET_FIELD(0xf, V3D_SLCACTL_TVCCS) |
173*57692c94SEric Anholt 		       V3D_SET_FIELD(0xf, V3D_SLCACTL_TDCCS) |
174*57692c94SEric Anholt 		       V3D_SET_FIELD(0xf, V3D_SLCACTL_UCC) |
175*57692c94SEric Anholt 		       V3D_SET_FIELD(0xf, V3D_SLCACTL_ICC));
176*57692c94SEric Anholt }
177*57692c94SEric Anholt 
178*57692c94SEric Anholt /* Invalidates texture L2 cachelines */
179*57692c94SEric Anholt static void
180*57692c94SEric Anholt v3d_invalidate_l2t(struct v3d_dev *v3d, int core)
181*57692c94SEric Anholt {
182*57692c94SEric Anholt 	V3D_CORE_WRITE(core,
183*57692c94SEric Anholt 		       V3D_CTL_L2TCACTL,
184*57692c94SEric Anholt 		       V3D_L2TCACTL_L2TFLS |
185*57692c94SEric Anholt 		       V3D_SET_FIELD(V3D_L2TCACTL_FLM_CLEAR, V3D_L2TCACTL_FLM));
186*57692c94SEric Anholt 	if (wait_for(!(V3D_CORE_READ(core, V3D_CTL_L2TCACTL) &
187*57692c94SEric Anholt 		       V3D_L2TCACTL_L2TFLS), 100)) {
188*57692c94SEric Anholt 		DRM_ERROR("Timeout waiting for L2T invalidate\n");
189*57692c94SEric Anholt 	}
190*57692c94SEric Anholt }
191*57692c94SEric Anholt 
192*57692c94SEric Anholt void
193*57692c94SEric Anholt v3d_invalidate_caches(struct v3d_dev *v3d)
194*57692c94SEric Anholt {
195*57692c94SEric Anholt 	v3d_flush_l3(v3d);
196*57692c94SEric Anholt 
197*57692c94SEric Anholt 	v3d_invalidate_l2(v3d, 0);
198*57692c94SEric Anholt 	v3d_invalidate_slices(v3d, 0);
199*57692c94SEric Anholt 	v3d_flush_l2t(v3d, 0);
200*57692c94SEric Anholt }
201*57692c94SEric Anholt 
202*57692c94SEric Anholt void
203*57692c94SEric Anholt v3d_flush_caches(struct v3d_dev *v3d)
204*57692c94SEric Anholt {
205*57692c94SEric Anholt 	v3d_invalidate_l1td(v3d, 0);
206*57692c94SEric Anholt 	v3d_invalidate_l2t(v3d, 0);
207*57692c94SEric Anholt }
208*57692c94SEric Anholt 
209*57692c94SEric Anholt static void
210*57692c94SEric Anholt v3d_attach_object_fences(struct v3d_exec_info *exec)
211*57692c94SEric Anholt {
212*57692c94SEric Anholt 	struct dma_fence *out_fence = &exec->render.base.s_fence->finished;
213*57692c94SEric Anholt 	struct v3d_bo *bo;
214*57692c94SEric Anholt 	int i;
215*57692c94SEric Anholt 
216*57692c94SEric Anholt 	for (i = 0; i < exec->bo_count; i++) {
217*57692c94SEric Anholt 		bo = to_v3d_bo(&exec->bo[i]->base);
218*57692c94SEric Anholt 
219*57692c94SEric Anholt 		/* XXX: Use shared fences for read-only objects. */
220*57692c94SEric Anholt 		reservation_object_add_excl_fence(bo->resv, out_fence);
221*57692c94SEric Anholt 	}
222*57692c94SEric Anholt }
223*57692c94SEric Anholt 
224*57692c94SEric Anholt static void
225*57692c94SEric Anholt v3d_unlock_bo_reservations(struct drm_device *dev,
226*57692c94SEric Anholt 			   struct v3d_exec_info *exec,
227*57692c94SEric Anholt 			   struct ww_acquire_ctx *acquire_ctx)
228*57692c94SEric Anholt {
229*57692c94SEric Anholt 	int i;
230*57692c94SEric Anholt 
231*57692c94SEric Anholt 	for (i = 0; i < exec->bo_count; i++) {
232*57692c94SEric Anholt 		struct v3d_bo *bo = to_v3d_bo(&exec->bo[i]->base);
233*57692c94SEric Anholt 
234*57692c94SEric Anholt 		ww_mutex_unlock(&bo->resv->lock);
235*57692c94SEric Anholt 	}
236*57692c94SEric Anholt 
237*57692c94SEric Anholt 	ww_acquire_fini(acquire_ctx);
238*57692c94SEric Anholt }
239*57692c94SEric Anholt 
240*57692c94SEric Anholt /* Takes the reservation lock on all the BOs being referenced, so that
241*57692c94SEric Anholt  * at queue submit time we can update the reservations.
242*57692c94SEric Anholt  *
243*57692c94SEric Anholt  * We don't lock the RCL the tile alloc/state BOs, or overflow memory
244*57692c94SEric Anholt  * (all of which are on exec->unref_list).  They're entirely private
245*57692c94SEric Anholt  * to v3d, so we don't attach dma-buf fences to them.
246*57692c94SEric Anholt  */
247*57692c94SEric Anholt static int
248*57692c94SEric Anholt v3d_lock_bo_reservations(struct drm_device *dev,
249*57692c94SEric Anholt 			 struct v3d_exec_info *exec,
250*57692c94SEric Anholt 			 struct ww_acquire_ctx *acquire_ctx)
251*57692c94SEric Anholt {
252*57692c94SEric Anholt 	int contended_lock = -1;
253*57692c94SEric Anholt 	int i, ret;
254*57692c94SEric Anholt 	struct v3d_bo *bo;
255*57692c94SEric Anholt 
256*57692c94SEric Anholt 	ww_acquire_init(acquire_ctx, &reservation_ww_class);
257*57692c94SEric Anholt 
258*57692c94SEric Anholt retry:
259*57692c94SEric Anholt 	if (contended_lock != -1) {
260*57692c94SEric Anholt 		bo = to_v3d_bo(&exec->bo[contended_lock]->base);
261*57692c94SEric Anholt 		ret = ww_mutex_lock_slow_interruptible(&bo->resv->lock,
262*57692c94SEric Anholt 						       acquire_ctx);
263*57692c94SEric Anholt 		if (ret) {
264*57692c94SEric Anholt 			ww_acquire_done(acquire_ctx);
265*57692c94SEric Anholt 			return ret;
266*57692c94SEric Anholt 		}
267*57692c94SEric Anholt 	}
268*57692c94SEric Anholt 
269*57692c94SEric Anholt 	for (i = 0; i < exec->bo_count; i++) {
270*57692c94SEric Anholt 		if (i == contended_lock)
271*57692c94SEric Anholt 			continue;
272*57692c94SEric Anholt 
273*57692c94SEric Anholt 		bo = to_v3d_bo(&exec->bo[i]->base);
274*57692c94SEric Anholt 
275*57692c94SEric Anholt 		ret = ww_mutex_lock_interruptible(&bo->resv->lock, acquire_ctx);
276*57692c94SEric Anholt 		if (ret) {
277*57692c94SEric Anholt 			int j;
278*57692c94SEric Anholt 
279*57692c94SEric Anholt 			for (j = 0; j < i; j++) {
280*57692c94SEric Anholt 				bo = to_v3d_bo(&exec->bo[j]->base);
281*57692c94SEric Anholt 				ww_mutex_unlock(&bo->resv->lock);
282*57692c94SEric Anholt 			}
283*57692c94SEric Anholt 
284*57692c94SEric Anholt 			if (contended_lock != -1 && contended_lock >= i) {
285*57692c94SEric Anholt 				bo = to_v3d_bo(&exec->bo[contended_lock]->base);
286*57692c94SEric Anholt 
287*57692c94SEric Anholt 				ww_mutex_unlock(&bo->resv->lock);
288*57692c94SEric Anholt 			}
289*57692c94SEric Anholt 
290*57692c94SEric Anholt 			if (ret == -EDEADLK) {
291*57692c94SEric Anholt 				contended_lock = i;
292*57692c94SEric Anholt 				goto retry;
293*57692c94SEric Anholt 			}
294*57692c94SEric Anholt 
295*57692c94SEric Anholt 			ww_acquire_done(acquire_ctx);
296*57692c94SEric Anholt 			return ret;
297*57692c94SEric Anholt 		}
298*57692c94SEric Anholt 	}
299*57692c94SEric Anholt 
300*57692c94SEric Anholt 	ww_acquire_done(acquire_ctx);
301*57692c94SEric Anholt 
302*57692c94SEric Anholt 	/* Reserve space for our shared (read-only) fence references,
303*57692c94SEric Anholt 	 * before we commit the CL to the hardware.
304*57692c94SEric Anholt 	 */
305*57692c94SEric Anholt 	for (i = 0; i < exec->bo_count; i++) {
306*57692c94SEric Anholt 		bo = to_v3d_bo(&exec->bo[i]->base);
307*57692c94SEric Anholt 
308*57692c94SEric Anholt 		ret = reservation_object_reserve_shared(bo->resv);
309*57692c94SEric Anholt 		if (ret) {
310*57692c94SEric Anholt 			v3d_unlock_bo_reservations(dev, exec, acquire_ctx);
311*57692c94SEric Anholt 			return ret;
312*57692c94SEric Anholt 		}
313*57692c94SEric Anholt 	}
314*57692c94SEric Anholt 
315*57692c94SEric Anholt 	return 0;
316*57692c94SEric Anholt }
317*57692c94SEric Anholt 
318*57692c94SEric Anholt /**
319*57692c94SEric Anholt  * v3d_cl_lookup_bos() - Sets up exec->bo[] with the GEM objects
320*57692c94SEric Anholt  * referenced by the job.
321*57692c94SEric Anholt  * @dev: DRM device
322*57692c94SEric Anholt  * @file_priv: DRM file for this fd
323*57692c94SEric Anholt  * @exec: V3D job being set up
324*57692c94SEric Anholt  *
325*57692c94SEric Anholt  * The command validator needs to reference BOs by their index within
326*57692c94SEric Anholt  * the submitted job's BO list.  This does the validation of the job's
327*57692c94SEric Anholt  * BO list and reference counting for the lifetime of the job.
328*57692c94SEric Anholt  *
329*57692c94SEric Anholt  * Note that this function doesn't need to unreference the BOs on
330*57692c94SEric Anholt  * failure, because that will happen at v3d_exec_cleanup() time.
331*57692c94SEric Anholt  */
332*57692c94SEric Anholt static int
333*57692c94SEric Anholt v3d_cl_lookup_bos(struct drm_device *dev,
334*57692c94SEric Anholt 		  struct drm_file *file_priv,
335*57692c94SEric Anholt 		  struct drm_v3d_submit_cl *args,
336*57692c94SEric Anholt 		  struct v3d_exec_info *exec)
337*57692c94SEric Anholt {
338*57692c94SEric Anholt 	u32 *handles;
339*57692c94SEric Anholt 	int ret = 0;
340*57692c94SEric Anholt 	int i;
341*57692c94SEric Anholt 
342*57692c94SEric Anholt 	exec->bo_count = args->bo_handle_count;
343*57692c94SEric Anholt 
344*57692c94SEric Anholt 	if (!exec->bo_count) {
345*57692c94SEric Anholt 		/* See comment on bo_index for why we have to check
346*57692c94SEric Anholt 		 * this.
347*57692c94SEric Anholt 		 */
348*57692c94SEric Anholt 		DRM_DEBUG("Rendering requires BOs\n");
349*57692c94SEric Anholt 		return -EINVAL;
350*57692c94SEric Anholt 	}
351*57692c94SEric Anholt 
352*57692c94SEric Anholt 	exec->bo = kvmalloc_array(exec->bo_count,
353*57692c94SEric Anholt 				  sizeof(struct drm_gem_cma_object *),
354*57692c94SEric Anholt 				  GFP_KERNEL | __GFP_ZERO);
355*57692c94SEric Anholt 	if (!exec->bo) {
356*57692c94SEric Anholt 		DRM_DEBUG("Failed to allocate validated BO pointers\n");
357*57692c94SEric Anholt 		return -ENOMEM;
358*57692c94SEric Anholt 	}
359*57692c94SEric Anholt 
360*57692c94SEric Anholt 	handles = kvmalloc_array(exec->bo_count, sizeof(u32), GFP_KERNEL);
361*57692c94SEric Anholt 	if (!handles) {
362*57692c94SEric Anholt 		ret = -ENOMEM;
363*57692c94SEric Anholt 		DRM_DEBUG("Failed to allocate incoming GEM handles\n");
364*57692c94SEric Anholt 		goto fail;
365*57692c94SEric Anholt 	}
366*57692c94SEric Anholt 
367*57692c94SEric Anholt 	if (copy_from_user(handles,
368*57692c94SEric Anholt 			   (void __user *)(uintptr_t)args->bo_handles,
369*57692c94SEric Anholt 			   exec->bo_count * sizeof(u32))) {
370*57692c94SEric Anholt 		ret = -EFAULT;
371*57692c94SEric Anholt 		DRM_DEBUG("Failed to copy in GEM handles\n");
372*57692c94SEric Anholt 		goto fail;
373*57692c94SEric Anholt 	}
374*57692c94SEric Anholt 
375*57692c94SEric Anholt 	spin_lock(&file_priv->table_lock);
376*57692c94SEric Anholt 	for (i = 0; i < exec->bo_count; i++) {
377*57692c94SEric Anholt 		struct drm_gem_object *bo = idr_find(&file_priv->object_idr,
378*57692c94SEric Anholt 						     handles[i]);
379*57692c94SEric Anholt 		if (!bo) {
380*57692c94SEric Anholt 			DRM_DEBUG("Failed to look up GEM BO %d: %d\n",
381*57692c94SEric Anholt 				  i, handles[i]);
382*57692c94SEric Anholt 			ret = -ENOENT;
383*57692c94SEric Anholt 			spin_unlock(&file_priv->table_lock);
384*57692c94SEric Anholt 			goto fail;
385*57692c94SEric Anholt 		}
386*57692c94SEric Anholt 		drm_gem_object_get(bo);
387*57692c94SEric Anholt 		exec->bo[i] = to_v3d_bo(bo);
388*57692c94SEric Anholt 	}
389*57692c94SEric Anholt 	spin_unlock(&file_priv->table_lock);
390*57692c94SEric Anholt 
391*57692c94SEric Anholt fail:
392*57692c94SEric Anholt 	kvfree(handles);
393*57692c94SEric Anholt 	return ret;
394*57692c94SEric Anholt }
395*57692c94SEric Anholt 
396*57692c94SEric Anholt static void
397*57692c94SEric Anholt v3d_exec_cleanup(struct kref *ref)
398*57692c94SEric Anholt {
399*57692c94SEric Anholt 	struct v3d_exec_info *exec = container_of(ref, struct v3d_exec_info,
400*57692c94SEric Anholt 						  refcount);
401*57692c94SEric Anholt 	struct v3d_dev *v3d = exec->v3d;
402*57692c94SEric Anholt 	unsigned int i;
403*57692c94SEric Anholt 	struct v3d_bo *bo, *save;
404*57692c94SEric Anholt 
405*57692c94SEric Anholt 	dma_fence_put(exec->bin.in_fence);
406*57692c94SEric Anholt 	dma_fence_put(exec->render.in_fence);
407*57692c94SEric Anholt 
408*57692c94SEric Anholt 	dma_fence_put(exec->bin.done_fence);
409*57692c94SEric Anholt 	dma_fence_put(exec->render.done_fence);
410*57692c94SEric Anholt 
411*57692c94SEric Anholt 	dma_fence_put(exec->bin_done_fence);
412*57692c94SEric Anholt 
413*57692c94SEric Anholt 	for (i = 0; i < exec->bo_count; i++)
414*57692c94SEric Anholt 		drm_gem_object_put_unlocked(&exec->bo[i]->base);
415*57692c94SEric Anholt 	kvfree(exec->bo);
416*57692c94SEric Anholt 
417*57692c94SEric Anholt 	list_for_each_entry_safe(bo, save, &exec->unref_list, unref_head) {
418*57692c94SEric Anholt 		drm_gem_object_put_unlocked(&bo->base);
419*57692c94SEric Anholt 	}
420*57692c94SEric Anholt 
421*57692c94SEric Anholt 	pm_runtime_mark_last_busy(v3d->dev);
422*57692c94SEric Anholt 	pm_runtime_put_autosuspend(v3d->dev);
423*57692c94SEric Anholt 
424*57692c94SEric Anholt 	kfree(exec);
425*57692c94SEric Anholt }
426*57692c94SEric Anholt 
427*57692c94SEric Anholt void v3d_exec_put(struct v3d_exec_info *exec)
428*57692c94SEric Anholt {
429*57692c94SEric Anholt 	kref_put(&exec->refcount, v3d_exec_cleanup);
430*57692c94SEric Anholt }
431*57692c94SEric Anholt 
432*57692c94SEric Anholt int
433*57692c94SEric Anholt v3d_wait_bo_ioctl(struct drm_device *dev, void *data,
434*57692c94SEric Anholt 		  struct drm_file *file_priv)
435*57692c94SEric Anholt {
436*57692c94SEric Anholt 	int ret;
437*57692c94SEric Anholt 	struct drm_v3d_wait_bo *args = data;
438*57692c94SEric Anholt 	struct drm_gem_object *gem_obj;
439*57692c94SEric Anholt 	struct v3d_bo *bo;
440*57692c94SEric Anholt 	ktime_t start = ktime_get();
441*57692c94SEric Anholt 	u64 delta_ns;
442*57692c94SEric Anholt 	unsigned long timeout_jiffies =
443*57692c94SEric Anholt 		nsecs_to_jiffies_timeout(args->timeout_ns);
444*57692c94SEric Anholt 
445*57692c94SEric Anholt 	if (args->pad != 0)
446*57692c94SEric Anholt 		return -EINVAL;
447*57692c94SEric Anholt 
448*57692c94SEric Anholt 	gem_obj = drm_gem_object_lookup(file_priv, args->handle);
449*57692c94SEric Anholt 	if (!gem_obj) {
450*57692c94SEric Anholt 		DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
451*57692c94SEric Anholt 		return -EINVAL;
452*57692c94SEric Anholt 	}
453*57692c94SEric Anholt 	bo = to_v3d_bo(gem_obj);
454*57692c94SEric Anholt 
455*57692c94SEric Anholt 	ret = reservation_object_wait_timeout_rcu(bo->resv,
456*57692c94SEric Anholt 						  true, true,
457*57692c94SEric Anholt 						  timeout_jiffies);
458*57692c94SEric Anholt 
459*57692c94SEric Anholt 	if (ret == 0)
460*57692c94SEric Anholt 		ret = -ETIME;
461*57692c94SEric Anholt 	else if (ret > 0)
462*57692c94SEric Anholt 		ret = 0;
463*57692c94SEric Anholt 
464*57692c94SEric Anholt 	/* Decrement the user's timeout, in case we got interrupted
465*57692c94SEric Anholt 	 * such that the ioctl will be restarted.
466*57692c94SEric Anholt 	 */
467*57692c94SEric Anholt 	delta_ns = ktime_to_ns(ktime_sub(ktime_get(), start));
468*57692c94SEric Anholt 	if (delta_ns < args->timeout_ns)
469*57692c94SEric Anholt 		args->timeout_ns -= delta_ns;
470*57692c94SEric Anholt 	else
471*57692c94SEric Anholt 		args->timeout_ns = 0;
472*57692c94SEric Anholt 
473*57692c94SEric Anholt 	/* Asked to wait beyond the jiffie/scheduler precision? */
474*57692c94SEric Anholt 	if (ret == -ETIME && args->timeout_ns)
475*57692c94SEric Anholt 		ret = -EAGAIN;
476*57692c94SEric Anholt 
477*57692c94SEric Anholt 	drm_gem_object_put_unlocked(gem_obj);
478*57692c94SEric Anholt 
479*57692c94SEric Anholt 	return ret;
480*57692c94SEric Anholt }
481*57692c94SEric Anholt 
482*57692c94SEric Anholt /**
483*57692c94SEric Anholt  * v3d_submit_cl_ioctl() - Submits a job (frame) to the V3D.
484*57692c94SEric Anholt  * @dev: DRM device
485*57692c94SEric Anholt  * @data: ioctl argument
486*57692c94SEric Anholt  * @file_priv: DRM file for this fd
487*57692c94SEric Anholt  *
488*57692c94SEric Anholt  * This is the main entrypoint for userspace to submit a 3D frame to
489*57692c94SEric Anholt  * the GPU.  Userspace provides the binner command list (if
490*57692c94SEric Anholt  * applicable), and the kernel sets up the render command list to draw
491*57692c94SEric Anholt  * to the framebuffer described in the ioctl, using the command lists
492*57692c94SEric Anholt  * that the 3D engine's binner will produce.
493*57692c94SEric Anholt  */
494*57692c94SEric Anholt int
495*57692c94SEric Anholt v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
496*57692c94SEric Anholt 		    struct drm_file *file_priv)
497*57692c94SEric Anholt {
498*57692c94SEric Anholt 	struct v3d_dev *v3d = to_v3d_dev(dev);
499*57692c94SEric Anholt 	struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
500*57692c94SEric Anholt 	struct drm_v3d_submit_cl *args = data;
501*57692c94SEric Anholt 	struct v3d_exec_info *exec;
502*57692c94SEric Anholt 	struct ww_acquire_ctx acquire_ctx;
503*57692c94SEric Anholt 	struct drm_syncobj *sync_out;
504*57692c94SEric Anholt 	int ret = 0;
505*57692c94SEric Anholt 
506*57692c94SEric Anholt 	if (args->pad != 0) {
507*57692c94SEric Anholt 		DRM_INFO("pad must be zero: %d\n", args->pad);
508*57692c94SEric Anholt 		return -EINVAL;
509*57692c94SEric Anholt 	}
510*57692c94SEric Anholt 
511*57692c94SEric Anholt 	exec = kcalloc(1, sizeof(*exec), GFP_KERNEL);
512*57692c94SEric Anholt 	if (!exec)
513*57692c94SEric Anholt 		return -ENOMEM;
514*57692c94SEric Anholt 
515*57692c94SEric Anholt 	ret = pm_runtime_get_sync(v3d->dev);
516*57692c94SEric Anholt 	if (ret < 0) {
517*57692c94SEric Anholt 		kfree(exec);
518*57692c94SEric Anholt 		return ret;
519*57692c94SEric Anholt 	}
520*57692c94SEric Anholt 
521*57692c94SEric Anholt 	kref_init(&exec->refcount);
522*57692c94SEric Anholt 
523*57692c94SEric Anholt 	ret = drm_syncobj_find_fence(file_priv, args->in_sync_bcl,
524*57692c94SEric Anholt 				     &exec->bin.in_fence);
525*57692c94SEric Anholt 	if (ret == -EINVAL)
526*57692c94SEric Anholt 		goto fail;
527*57692c94SEric Anholt 
528*57692c94SEric Anholt 	ret = drm_syncobj_find_fence(file_priv, args->in_sync_rcl,
529*57692c94SEric Anholt 				     &exec->render.in_fence);
530*57692c94SEric Anholt 	if (ret == -EINVAL)
531*57692c94SEric Anholt 		goto fail;
532*57692c94SEric Anholt 
533*57692c94SEric Anholt 	exec->qma = args->qma;
534*57692c94SEric Anholt 	exec->qms = args->qms;
535*57692c94SEric Anholt 	exec->qts = args->qts;
536*57692c94SEric Anholt 	exec->bin.exec = exec;
537*57692c94SEric Anholt 	exec->bin.start = args->bcl_start;
538*57692c94SEric Anholt 	exec->bin.end = args->bcl_end;
539*57692c94SEric Anholt 	exec->render.exec = exec;
540*57692c94SEric Anholt 	exec->render.start = args->rcl_start;
541*57692c94SEric Anholt 	exec->render.end = args->rcl_end;
542*57692c94SEric Anholt 	exec->v3d = v3d;
543*57692c94SEric Anholt 	INIT_LIST_HEAD(&exec->unref_list);
544*57692c94SEric Anholt 
545*57692c94SEric Anholt 	ret = v3d_cl_lookup_bos(dev, file_priv, args, exec);
546*57692c94SEric Anholt 	if (ret)
547*57692c94SEric Anholt 		goto fail;
548*57692c94SEric Anholt 
549*57692c94SEric Anholt 	ret = v3d_lock_bo_reservations(dev, exec, &acquire_ctx);
550*57692c94SEric Anholt 	if (ret)
551*57692c94SEric Anholt 		goto fail;
552*57692c94SEric Anholt 
553*57692c94SEric Anholt 	if (exec->bin.start != exec->bin.end) {
554*57692c94SEric Anholt 		ret = drm_sched_job_init(&exec->bin.base,
555*57692c94SEric Anholt 					 &v3d->queue[V3D_BIN].sched,
556*57692c94SEric Anholt 					 &v3d_priv->sched_entity[V3D_BIN],
557*57692c94SEric Anholt 					 v3d_priv);
558*57692c94SEric Anholt 		if (ret)
559*57692c94SEric Anholt 			goto fail_unreserve;
560*57692c94SEric Anholt 
561*57692c94SEric Anholt 		exec->bin_done_fence =
562*57692c94SEric Anholt 			dma_fence_get(&exec->bin.base.s_fence->finished);
563*57692c94SEric Anholt 
564*57692c94SEric Anholt 		kref_get(&exec->refcount); /* put by scheduler job completion */
565*57692c94SEric Anholt 		drm_sched_entity_push_job(&exec->bin.base,
566*57692c94SEric Anholt 					  &v3d_priv->sched_entity[V3D_BIN]);
567*57692c94SEric Anholt 	}
568*57692c94SEric Anholt 
569*57692c94SEric Anholt 	ret = drm_sched_job_init(&exec->render.base,
570*57692c94SEric Anholt 				 &v3d->queue[V3D_RENDER].sched,
571*57692c94SEric Anholt 				 &v3d_priv->sched_entity[V3D_RENDER],
572*57692c94SEric Anholt 				 v3d_priv);
573*57692c94SEric Anholt 	if (ret)
574*57692c94SEric Anholt 		goto fail_unreserve;
575*57692c94SEric Anholt 
576*57692c94SEric Anholt 	kref_get(&exec->refcount); /* put by scheduler job completion */
577*57692c94SEric Anholt 	drm_sched_entity_push_job(&exec->render.base,
578*57692c94SEric Anholt 				  &v3d_priv->sched_entity[V3D_RENDER]);
579*57692c94SEric Anholt 
580*57692c94SEric Anholt 	v3d_attach_object_fences(exec);
581*57692c94SEric Anholt 
582*57692c94SEric Anholt 	v3d_unlock_bo_reservations(dev, exec, &acquire_ctx);
583*57692c94SEric Anholt 
584*57692c94SEric Anholt 	/* Update the return sync object for the */
585*57692c94SEric Anholt 	sync_out = drm_syncobj_find(file_priv, args->out_sync);
586*57692c94SEric Anholt 	if (sync_out) {
587*57692c94SEric Anholt 		drm_syncobj_replace_fence(sync_out,
588*57692c94SEric Anholt 					  &exec->render.base.s_fence->finished);
589*57692c94SEric Anholt 		drm_syncobj_put(sync_out);
590*57692c94SEric Anholt 	}
591*57692c94SEric Anholt 
592*57692c94SEric Anholt 	v3d_exec_put(exec);
593*57692c94SEric Anholt 
594*57692c94SEric Anholt 	return 0;
595*57692c94SEric Anholt 
596*57692c94SEric Anholt fail_unreserve:
597*57692c94SEric Anholt 	v3d_unlock_bo_reservations(dev, exec, &acquire_ctx);
598*57692c94SEric Anholt fail:
599*57692c94SEric Anholt 	v3d_exec_put(exec);
600*57692c94SEric Anholt 
601*57692c94SEric Anholt 	return ret;
602*57692c94SEric Anholt }
603*57692c94SEric Anholt 
604*57692c94SEric Anholt int
605*57692c94SEric Anholt v3d_gem_init(struct drm_device *dev)
606*57692c94SEric Anholt {
607*57692c94SEric Anholt 	struct v3d_dev *v3d = to_v3d_dev(dev);
608*57692c94SEric Anholt 	u32 pt_size = 4096 * 1024;
609*57692c94SEric Anholt 	int ret, i;
610*57692c94SEric Anholt 
611*57692c94SEric Anholt 	for (i = 0; i < V3D_MAX_QUEUES; i++)
612*57692c94SEric Anholt 		v3d->queue[i].fence_context = dma_fence_context_alloc(1);
613*57692c94SEric Anholt 
614*57692c94SEric Anholt 	spin_lock_init(&v3d->mm_lock);
615*57692c94SEric Anholt 	spin_lock_init(&v3d->job_lock);
616*57692c94SEric Anholt 	mutex_init(&v3d->bo_lock);
617*57692c94SEric Anholt 	mutex_init(&v3d->reset_lock);
618*57692c94SEric Anholt 
619*57692c94SEric Anholt 	/* Note: We don't allocate address 0.  Various bits of HW
620*57692c94SEric Anholt 	 * treat 0 as special, such as the occlusion query counters
621*57692c94SEric Anholt 	 * where 0 means "disabled".
622*57692c94SEric Anholt 	 */
623*57692c94SEric Anholt 	drm_mm_init(&v3d->mm, 1, pt_size / sizeof(u32) - 1);
624*57692c94SEric Anholt 
625*57692c94SEric Anholt 	v3d->pt = dma_alloc_wc(v3d->dev, pt_size,
626*57692c94SEric Anholt 			       &v3d->pt_paddr,
627*57692c94SEric Anholt 			       GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);
628*57692c94SEric Anholt 	if (!v3d->pt) {
629*57692c94SEric Anholt 		drm_mm_takedown(&v3d->mm);
630*57692c94SEric Anholt 		dev_err(v3d->dev,
631*57692c94SEric Anholt 			"Failed to allocate page tables. "
632*57692c94SEric Anholt 			"Please ensure you have CMA enabled.\n");
633*57692c94SEric Anholt 		return -ENOMEM;
634*57692c94SEric Anholt 	}
635*57692c94SEric Anholt 
636*57692c94SEric Anholt 	v3d_init_hw_state(v3d);
637*57692c94SEric Anholt 	v3d_mmu_set_page_table(v3d);
638*57692c94SEric Anholt 
639*57692c94SEric Anholt 	ret = v3d_sched_init(v3d);
640*57692c94SEric Anholt 	if (ret) {
641*57692c94SEric Anholt 		drm_mm_takedown(&v3d->mm);
642*57692c94SEric Anholt 		dma_free_coherent(v3d->dev, 4096 * 1024, (void *)v3d->pt,
643*57692c94SEric Anholt 				  v3d->pt_paddr);
644*57692c94SEric Anholt 	}
645*57692c94SEric Anholt 
646*57692c94SEric Anholt 	return 0;
647*57692c94SEric Anholt }
648*57692c94SEric Anholt 
649*57692c94SEric Anholt void
650*57692c94SEric Anholt v3d_gem_destroy(struct drm_device *dev)
651*57692c94SEric Anholt {
652*57692c94SEric Anholt 	struct v3d_dev *v3d = to_v3d_dev(dev);
653*57692c94SEric Anholt 	enum v3d_queue q;
654*57692c94SEric Anholt 
655*57692c94SEric Anholt 	v3d_sched_fini(v3d);
656*57692c94SEric Anholt 
657*57692c94SEric Anholt 	/* Waiting for exec to finish would need to be done before
658*57692c94SEric Anholt 	 * unregistering V3D.
659*57692c94SEric Anholt 	 */
660*57692c94SEric Anholt 	for (q = 0; q < V3D_MAX_QUEUES; q++) {
661*57692c94SEric Anholt 		WARN_ON(v3d->queue[q].emit_seqno !=
662*57692c94SEric Anholt 			v3d->queue[q].finished_seqno);
663*57692c94SEric Anholt 	}
664*57692c94SEric Anholt 
665*57692c94SEric Anholt 	drm_mm_takedown(&v3d->mm);
666*57692c94SEric Anholt 
667*57692c94SEric Anholt 	dma_free_coherent(v3d->dev, 4096 * 1024, (void *)v3d->pt, v3d->pt_paddr);
668*57692c94SEric Anholt }
669