blob: 350a269a7b58230541c9404c1557dcf1845cb0be [file] [log] [blame]
Eric Anholt57692c92018-04-30 11:10:58 -07001// SPDX-License-Identifier: GPL-2.0+
2/* Copyright (C) 2014-2018 Broadcom */
3
4#include <drm/drmP.h>
5#include <drm/drm_syncobj.h>
6#include <linux/module.h>
7#include <linux/platform_device.h>
8#include <linux/pm_runtime.h>
Eric Anholteea9b972019-03-08 09:43:36 -08009#include <linux/reset.h>
Eric Anholt57692c92018-04-30 11:10:58 -070010#include <linux/device.h>
11#include <linux/io.h>
12#include <linux/sched/signal.h>
13
14#include "uapi/drm/v3d_drm.h"
15#include "v3d_drv.h"
16#include "v3d_regs.h"
17#include "v3d_trace.h"
18
19static void
20v3d_init_core(struct v3d_dev *v3d, int core)
21{
22 /* Set OVRTMUOUT, which means that the texture sampler uniform
23 * configuration's tmu output type field is used, instead of
24 * using the hardware default behavior based on the texture
25 * type. If you want the default behavior, you can still put
26 * "2" in the indirect texture state's output_type field.
27 */
Eric Anholta7dde1b2019-02-20 15:36:57 -080028 if (v3d->ver < 40)
29 V3D_CORE_WRITE(core, V3D_CTL_MISCCFG, V3D_MISCCFG_OVRTMUOUT);
Eric Anholt57692c92018-04-30 11:10:58 -070030
31 /* Whenever we flush the L2T cache, we always want to flush
32 * the whole thing.
33 */
34 V3D_CORE_WRITE(core, V3D_CTL_L2TFLSTA, 0);
35 V3D_CORE_WRITE(core, V3D_CTL_L2TFLEND, ~0);
36}
37
38/* Sets invariant state for the HW. */
39static void
40v3d_init_hw_state(struct v3d_dev *v3d)
41{
42 v3d_init_core(v3d, 0);
43}
44
45static void
46v3d_idle_axi(struct v3d_dev *v3d, int core)
47{
48 V3D_CORE_WRITE(core, V3D_GMP_CFG, V3D_GMP_CFG_STOP_REQ);
49
50 if (wait_for((V3D_CORE_READ(core, V3D_GMP_STATUS) &
51 (V3D_GMP_STATUS_RD_COUNT_MASK |
52 V3D_GMP_STATUS_WR_COUNT_MASK |
53 V3D_GMP_STATUS_CFG_BUSY)) == 0, 100)) {
54 DRM_ERROR("Failed to wait for safe GMP shutdown\n");
55 }
56}
57
58static void
59v3d_idle_gca(struct v3d_dev *v3d)
60{
61 if (v3d->ver >= 41)
62 return;
63
64 V3D_GCA_WRITE(V3D_GCA_SAFE_SHUTDOWN, V3D_GCA_SAFE_SHUTDOWN_EN);
65
66 if (wait_for((V3D_GCA_READ(V3D_GCA_SAFE_SHUTDOWN_ACK) &
67 V3D_GCA_SAFE_SHUTDOWN_ACK_ACKED) ==
68 V3D_GCA_SAFE_SHUTDOWN_ACK_ACKED, 100)) {
69 DRM_ERROR("Failed to wait for safe GCA shutdown\n");
70 }
71}
72
73static void
Eric Anholteea9b972019-03-08 09:43:36 -080074v3d_reset_by_bridge(struct v3d_dev *v3d)
Eric Anholt57692c92018-04-30 11:10:58 -070075{
76 int version = V3D_BRIDGE_READ(V3D_TOP_GR_BRIDGE_REVISION);
77
78 if (V3D_GET_FIELD(version, V3D_TOP_GR_BRIDGE_MAJOR) == 2) {
79 V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_0,
80 V3D_TOP_GR_BRIDGE_SW_INIT_0_V3D_CLK_108_SW_INIT);
81 V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_0, 0);
82
83 /* GFXH-1383: The SW_INIT may cause a stray write to address 0
84 * of the unit, so reset it to its power-on value here.
85 */
86 V3D_WRITE(V3D_HUB_AXICFG, V3D_HUB_AXICFG_MAX_LEN_MASK);
87 } else {
88 WARN_ON_ONCE(V3D_GET_FIELD(version,
89 V3D_TOP_GR_BRIDGE_MAJOR) != 7);
90 V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_1,
91 V3D_TOP_GR_BRIDGE_SW_INIT_1_V3D_CLK_108_SW_INIT);
92 V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_1, 0);
93 }
Eric Anholteea9b972019-03-08 09:43:36 -080094}
95
96static void
97v3d_reset_v3d(struct v3d_dev *v3d)
98{
99 if (v3d->reset)
100 reset_control_reset(v3d->reset);
101 else
102 v3d_reset_by_bridge(v3d);
Eric Anholt57692c92018-04-30 11:10:58 -0700103
104 v3d_init_hw_state(v3d);
105}
106
107void
108v3d_reset(struct v3d_dev *v3d)
109{
110 struct drm_device *dev = &v3d->drm;
111
112 DRM_ERROR("Resetting GPU.\n");
113 trace_v3d_reset_begin(dev);
114
115 /* XXX: only needed for safe powerdown, not reset. */
116 if (false)
117 v3d_idle_axi(v3d, 0);
118
119 v3d_idle_gca(v3d);
120 v3d_reset_v3d(v3d);
121
122 v3d_mmu_set_page_table(v3d);
123 v3d_irq_reset(v3d);
124
125 trace_v3d_reset_end(dev);
126}
127
128static void
129v3d_flush_l3(struct v3d_dev *v3d)
130{
131 if (v3d->ver < 41) {
132 u32 gca_ctrl = V3D_GCA_READ(V3D_GCA_CACHE_CTRL);
133
134 V3D_GCA_WRITE(V3D_GCA_CACHE_CTRL,
135 gca_ctrl | V3D_GCA_CACHE_CTRL_FLUSH);
136
137 if (v3d->ver < 33) {
138 V3D_GCA_WRITE(V3D_GCA_CACHE_CTRL,
139 gca_ctrl & ~V3D_GCA_CACHE_CTRL_FLUSH);
140 }
141 }
142}
143
Eric Anholt7b9d2fe2018-12-03 14:24:37 -0800144/* Invalidates the (read-only) L2C cache. This was the L2 cache for
145 * uniforms and instructions on V3D 3.2.
146 */
Eric Anholt57692c92018-04-30 11:10:58 -0700147static void
Eric Anholt7b9d2fe2018-12-03 14:24:37 -0800148v3d_invalidate_l2c(struct v3d_dev *v3d, int core)
Eric Anholt57692c92018-04-30 11:10:58 -0700149{
Eric Anholt7b9d2fe2018-12-03 14:24:37 -0800150 if (v3d->ver > 32)
151 return;
152
Eric Anholt57692c92018-04-30 11:10:58 -0700153 V3D_CORE_WRITE(core, V3D_CTL_L2CACTL,
154 V3D_L2CACTL_L2CCLR |
155 V3D_L2CACTL_L2CENA);
156}
157
Eric Anholt57692c92018-04-30 11:10:58 -0700158/* Invalidates texture L2 cachelines */
159static void
160v3d_flush_l2t(struct v3d_dev *v3d, int core)
161{
Eric Anholt51c1b6f2018-12-03 14:24:36 -0800162 /* While there is a busy bit (V3D_L2TCACTL_L2TFLS), we don't
163 * need to wait for completion before dispatching the job --
164 * L2T accesses will be stalled until the flush has completed.
165 */
Eric Anholt57692c92018-04-30 11:10:58 -0700166 V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL,
167 V3D_L2TCACTL_L2TFLS |
168 V3D_SET_FIELD(V3D_L2TCACTL_FLM_FLUSH, V3D_L2TCACTL_FLM));
Eric Anholt57692c92018-04-30 11:10:58 -0700169}
170
171/* Invalidates the slice caches. These are read-only caches. */
172static void
173v3d_invalidate_slices(struct v3d_dev *v3d, int core)
174{
175 V3D_CORE_WRITE(core, V3D_CTL_SLCACTL,
176 V3D_SET_FIELD(0xf, V3D_SLCACTL_TVCCS) |
177 V3D_SET_FIELD(0xf, V3D_SLCACTL_TDCCS) |
178 V3D_SET_FIELD(0xf, V3D_SLCACTL_UCC) |
179 V3D_SET_FIELD(0xf, V3D_SLCACTL_ICC));
180}
181
Eric Anholt57692c92018-04-30 11:10:58 -0700182void
183v3d_invalidate_caches(struct v3d_dev *v3d)
184{
Eric Anholtaa5beec2018-12-03 14:24:38 -0800185 /* Invalidate the caches from the outside in. That way if
186 * another CL's concurrent use of nearby memory were to pull
187 * an invalidated cacheline back in, we wouldn't leave stale
188 * data in the inner cache.
189 */
Eric Anholt57692c92018-04-30 11:10:58 -0700190 v3d_flush_l3(v3d);
Eric Anholt7b9d2fe2018-12-03 14:24:37 -0800191 v3d_invalidate_l2c(v3d, 0);
Eric Anholt57692c92018-04-30 11:10:58 -0700192 v3d_flush_l2t(v3d, 0);
Eric Anholtaa5beec2018-12-03 14:24:38 -0800193 v3d_invalidate_slices(v3d, 0);
Eric Anholt57692c92018-04-30 11:10:58 -0700194}
195
Eric Anholt57692c92018-04-30 11:10:58 -0700196/* Takes the reservation lock on all the BOs being referenced, so that
197 * at queue submit time we can update the reservations.
198 *
199 * We don't lock the RCL the tile alloc/state BOs, or overflow memory
200 * (all of which are on exec->unref_list). They're entirely private
201 * to v3d, so we don't attach dma-buf fences to them.
202 */
203static int
Eric Anholtd4c30222019-04-16 15:58:52 -0700204v3d_lock_bo_reservations(struct drm_gem_object **bos,
Eric Anholt1584f162018-11-28 15:09:25 -0800205 int bo_count,
Eric Anholt57692c92018-04-30 11:10:58 -0700206 struct ww_acquire_ctx *acquire_ctx)
207{
Eric Anholt57692c92018-04-30 11:10:58 -0700208 int i, ret;
Eric Anholt57692c92018-04-30 11:10:58 -0700209
Eric Anholtd4c30222019-04-16 15:58:52 -0700210 ret = drm_gem_lock_reservations(bos, bo_count, acquire_ctx);
Eric Anholtc2b3e612019-03-08 08:17:14 -0800211 if (ret)
212 return ret;
Eric Anholt57692c92018-04-30 11:10:58 -0700213
214 /* Reserve space for our shared (read-only) fence references,
215 * before we commit the CL to the hardware.
216 */
Eric Anholt1584f162018-11-28 15:09:25 -0800217 for (i = 0; i < bo_count; i++) {
Eric Anholtd4c30222019-04-16 15:58:52 -0700218 ret = reservation_object_reserve_shared(bos[i]->resv, 1);
Eric Anholt57692c92018-04-30 11:10:58 -0700219 if (ret) {
Eric Anholtd4c30222019-04-16 15:58:52 -0700220 drm_gem_unlock_reservations(bos, bo_count,
221 acquire_ctx);
Eric Anholt57692c92018-04-30 11:10:58 -0700222 return ret;
223 }
224 }
225
226 return 0;
227}
228
229/**
Eric Anholta783a092019-04-16 15:58:53 -0700230 * v3d_lookup_bos() - Sets up job->bo[] with the GEM objects
Eric Anholt57692c92018-04-30 11:10:58 -0700231 * referenced by the job.
232 * @dev: DRM device
233 * @file_priv: DRM file for this fd
Eric Anholta783a092019-04-16 15:58:53 -0700234 * @job: V3D job being set up
Eric Anholt57692c92018-04-30 11:10:58 -0700235 *
236 * The command validator needs to reference BOs by their index within
237 * the submitted job's BO list. This does the validation of the job's
238 * BO list and reference counting for the lifetime of the job.
239 *
240 * Note that this function doesn't need to unreference the BOs on
241 * failure, because that will happen at v3d_exec_cleanup() time.
242 */
243static int
Eric Anholta783a092019-04-16 15:58:53 -0700244v3d_lookup_bos(struct drm_device *dev,
245 struct drm_file *file_priv,
246 struct v3d_job *job,
247 u64 bo_handles,
248 u32 bo_count)
Eric Anholt57692c92018-04-30 11:10:58 -0700249{
250 u32 *handles;
251 int ret = 0;
252 int i;
253
Eric Anholta783a092019-04-16 15:58:53 -0700254 job->bo_count = bo_count;
Eric Anholt57692c92018-04-30 11:10:58 -0700255
Eric Anholta783a092019-04-16 15:58:53 -0700256 if (!job->bo_count) {
Eric Anholt57692c92018-04-30 11:10:58 -0700257 /* See comment on bo_index for why we have to check
258 * this.
259 */
260 DRM_DEBUG("Rendering requires BOs\n");
261 return -EINVAL;
262 }
263
Eric Anholta783a092019-04-16 15:58:53 -0700264 job->bo = kvmalloc_array(job->bo_count,
265 sizeof(struct drm_gem_cma_object *),
266 GFP_KERNEL | __GFP_ZERO);
267 if (!job->bo) {
Eric Anholt57692c92018-04-30 11:10:58 -0700268 DRM_DEBUG("Failed to allocate validated BO pointers\n");
269 return -ENOMEM;
270 }
271
Eric Anholta783a092019-04-16 15:58:53 -0700272 handles = kvmalloc_array(job->bo_count, sizeof(u32), GFP_KERNEL);
Eric Anholt57692c92018-04-30 11:10:58 -0700273 if (!handles) {
274 ret = -ENOMEM;
275 DRM_DEBUG("Failed to allocate incoming GEM handles\n");
276 goto fail;
277 }
278
279 if (copy_from_user(handles,
Eric Anholta783a092019-04-16 15:58:53 -0700280 (void __user *)(uintptr_t)bo_handles,
281 job->bo_count * sizeof(u32))) {
Eric Anholt57692c92018-04-30 11:10:58 -0700282 ret = -EFAULT;
283 DRM_DEBUG("Failed to copy in GEM handles\n");
284 goto fail;
285 }
286
287 spin_lock(&file_priv->table_lock);
Eric Anholta783a092019-04-16 15:58:53 -0700288 for (i = 0; i < job->bo_count; i++) {
Eric Anholt57692c92018-04-30 11:10:58 -0700289 struct drm_gem_object *bo = idr_find(&file_priv->object_idr,
290 handles[i]);
291 if (!bo) {
292 DRM_DEBUG("Failed to look up GEM BO %d: %d\n",
293 i, handles[i]);
294 ret = -ENOENT;
295 spin_unlock(&file_priv->table_lock);
296 goto fail;
297 }
298 drm_gem_object_get(bo);
Eric Anholta783a092019-04-16 15:58:53 -0700299 job->bo[i] = bo;
Eric Anholt57692c92018-04-30 11:10:58 -0700300 }
301 spin_unlock(&file_priv->table_lock);
302
303fail:
304 kvfree(handles);
305 return ret;
306}
307
308static void
Eric Anholta783a092019-04-16 15:58:53 -0700309v3d_job_free(struct kref *ref)
Eric Anholt57692c92018-04-30 11:10:58 -0700310{
Eric Anholta783a092019-04-16 15:58:53 -0700311 struct v3d_job *job = container_of(ref, struct v3d_job, refcount);
312 int i;
Eric Anholt57692c92018-04-30 11:10:58 -0700313
Eric Anholta783a092019-04-16 15:58:53 -0700314 for (i = 0; i < job->bo_count; i++) {
Eric Anholt1584f162018-11-28 15:09:25 -0800315 if (job->bo[i])
Eric Anholtd4c30222019-04-16 15:58:52 -0700316 drm_gem_object_put_unlocked(job->bo[i]);
Eric Anholt1584f162018-11-28 15:09:25 -0800317 }
Eric Anholta783a092019-04-16 15:58:53 -0700318 kvfree(job->bo);
Eric Anholt1584f162018-11-28 15:09:25 -0800319
Eric Anholta783a092019-04-16 15:58:53 -0700320 dma_fence_put(job->in_fence);
321 dma_fence_put(job->irq_fence);
322 dma_fence_put(job->done_fence);
323
324 pm_runtime_mark_last_busy(job->v3d->dev);
325 pm_runtime_put_autosuspend(job->v3d->dev);
Eric Anholt1584f162018-11-28 15:09:25 -0800326
327 kfree(job);
328}
329
Eric Anholta783a092019-04-16 15:58:53 -0700330static void
331v3d_render_job_free(struct kref *ref)
Eric Anholt1584f162018-11-28 15:09:25 -0800332{
Eric Anholta783a092019-04-16 15:58:53 -0700333 struct v3d_render_job *job = container_of(ref, struct v3d_render_job,
334 base.refcount);
335 struct v3d_bo *bo, *save;
336
337 list_for_each_entry_safe(bo, save, &job->unref_list, unref_head) {
338 drm_gem_object_put_unlocked(&bo->base.base);
339 }
340
341 v3d_job_free(ref);
342}
343
344void v3d_job_put(struct v3d_job *job)
345{
346 kref_put(&job->refcount, job->free);
Eric Anholt1584f162018-11-28 15:09:25 -0800347}
348
Eric Anholt57692c92018-04-30 11:10:58 -0700349int
350v3d_wait_bo_ioctl(struct drm_device *dev, void *data,
351 struct drm_file *file_priv)
352{
353 int ret;
354 struct drm_v3d_wait_bo *args = data;
Eric Anholt57692c92018-04-30 11:10:58 -0700355 ktime_t start = ktime_get();
356 u64 delta_ns;
357 unsigned long timeout_jiffies =
358 nsecs_to_jiffies_timeout(args->timeout_ns);
359
360 if (args->pad != 0)
361 return -EINVAL;
362
Rob Herring8d668302019-02-02 09:41:57 -0600363 ret = drm_gem_reservation_object_wait(file_priv, args->handle,
364 true, timeout_jiffies);
Eric Anholt57692c92018-04-30 11:10:58 -0700365
366 /* Decrement the user's timeout, in case we got interrupted
367 * such that the ioctl will be restarted.
368 */
369 delta_ns = ktime_to_ns(ktime_sub(ktime_get(), start));
370 if (delta_ns < args->timeout_ns)
371 args->timeout_ns -= delta_ns;
372 else
373 args->timeout_ns = 0;
374
375 /* Asked to wait beyond the jiffie/scheduler precision? */
376 if (ret == -ETIME && args->timeout_ns)
377 ret = -EAGAIN;
378
Eric Anholt57692c92018-04-30 11:10:58 -0700379 return ret;
380}
381
Eric Anholta783a092019-04-16 15:58:53 -0700382static int
383v3d_job_init(struct v3d_dev *v3d, struct drm_file *file_priv,
384 struct v3d_job *job, void (*free)(struct kref *ref),
385 u32 in_sync)
386{
387 int ret;
388
389 job->v3d = v3d;
390 job->free = free;
391
392 ret = pm_runtime_get_sync(v3d->dev);
393 if (ret < 0)
394 return ret;
395
396 ret = drm_syncobj_find_fence(file_priv, in_sync, 0, 0, &job->in_fence);
397 if (ret == -EINVAL) {
398 pm_runtime_put_autosuspend(v3d->dev);
399 return ret;
400 }
401
402 kref_init(&job->refcount);
403
404 return 0;
405}
406
407static int
408v3d_push_job(struct v3d_file_priv *v3d_priv,
409 struct v3d_job *job, enum v3d_queue queue)
410{
411 int ret;
412
413 ret = drm_sched_job_init(&job->base, &v3d_priv->sched_entity[queue],
414 v3d_priv);
415 if (ret)
416 return ret;
417
418 job->done_fence = dma_fence_get(&job->base.s_fence->finished);
419
420 /* put by scheduler job completion */
421 kref_get(&job->refcount);
422
423 drm_sched_entity_push_job(&job->base, &v3d_priv->sched_entity[queue]);
424
425 return 0;
426}
427
428static void
429v3d_attach_fences_and_unlock_reservation(struct drm_file *file_priv,
430 struct v3d_job *job,
431 struct ww_acquire_ctx *acquire_ctx,
432 u32 out_sync)
433{
434 struct drm_syncobj *sync_out;
435 int i;
436
437 for (i = 0; i < job->bo_count; i++) {
438 /* XXX: Use shared fences for read-only objects. */
439 reservation_object_add_excl_fence(job->bo[i]->resv,
440 job->done_fence);
441 }
442
443 drm_gem_unlock_reservations(job->bo, job->bo_count, acquire_ctx);
444
445 /* Update the return sync object for the job */
446 sync_out = drm_syncobj_find(file_priv, out_sync);
447 if (sync_out) {
448 drm_syncobj_replace_fence(sync_out, job->done_fence);
449 drm_syncobj_put(sync_out);
450 }
451}
452
Eric Anholt57692c92018-04-30 11:10:58 -0700453/**
454 * v3d_submit_cl_ioctl() - Submits a job (frame) to the V3D.
455 * @dev: DRM device
456 * @data: ioctl argument
457 * @file_priv: DRM file for this fd
458 *
459 * This is the main entrypoint for userspace to submit a 3D frame to
460 * the GPU. Userspace provides the binner command list (if
461 * applicable), and the kernel sets up the render command list to draw
462 * to the framebuffer described in the ioctl, using the command lists
463 * that the 3D engine's binner will produce.
464 */
465int
466v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
467 struct drm_file *file_priv)
468{
469 struct v3d_dev *v3d = to_v3d_dev(dev);
470 struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
471 struct drm_v3d_submit_cl *args = data;
Eric Anholta783a092019-04-16 15:58:53 -0700472 struct v3d_bin_job *bin = NULL;
473 struct v3d_render_job *render;
Eric Anholt57692c92018-04-30 11:10:58 -0700474 struct ww_acquire_ctx acquire_ctx;
Eric Anholt57692c92018-04-30 11:10:58 -0700475 int ret = 0;
476
Eric Anholt55a9b742018-11-30 16:57:58 -0800477 trace_v3d_submit_cl_ioctl(&v3d->drm, args->rcl_start, args->rcl_end);
478
Eric Anholt57692c92018-04-30 11:10:58 -0700479 if (args->pad != 0) {
480 DRM_INFO("pad must be zero: %d\n", args->pad);
481 return -EINVAL;
482 }
483
Eric Anholta783a092019-04-16 15:58:53 -0700484 render = kcalloc(1, sizeof(*render), GFP_KERNEL);
485 if (!render)
Eric Anholt57692c92018-04-30 11:10:58 -0700486 return -ENOMEM;
487
Eric Anholta783a092019-04-16 15:58:53 -0700488 render->start = args->rcl_start;
489 render->end = args->rcl_end;
490 INIT_LIST_HEAD(&render->unref_list);
491
492 ret = v3d_job_init(v3d, file_priv, &render->base,
493 v3d_render_job_free, args->in_sync_rcl);
494 if (ret) {
495 kfree(render);
Eric Anholt57692c92018-04-30 11:10:58 -0700496 return ret;
497 }
498
Eric Anholta783a092019-04-16 15:58:53 -0700499 if (args->bcl_start != args->bcl_end) {
500 bin = kcalloc(1, sizeof(*bin), GFP_KERNEL);
501 if (!bin)
502 return -ENOMEM;
Eric Anholt57692c92018-04-30 11:10:58 -0700503
Eric Anholta783a092019-04-16 15:58:53 -0700504 ret = v3d_job_init(v3d, file_priv, &bin->base,
505 v3d_job_free, args->in_sync_bcl);
506 if (ret) {
507 v3d_job_put(&render->base);
508 return ret;
509 }
Eric Anholt57692c92018-04-30 11:10:58 -0700510
Eric Anholta783a092019-04-16 15:58:53 -0700511 bin->start = args->bcl_start;
512 bin->end = args->bcl_end;
513 bin->qma = args->qma;
514 bin->qms = args->qms;
515 bin->qts = args->qts;
516 bin->render = render;
517 }
Eric Anholt57692c92018-04-30 11:10:58 -0700518
Eric Anholta783a092019-04-16 15:58:53 -0700519 ret = v3d_lookup_bos(dev, file_priv, &render->base,
520 args->bo_handles, args->bo_handle_count);
Eric Anholt57692c92018-04-30 11:10:58 -0700521 if (ret)
522 goto fail;
523
Eric Anholta783a092019-04-16 15:58:53 -0700524 ret = v3d_lock_bo_reservations(render->base.bo, render->base.bo_count,
Eric Anholt1584f162018-11-28 15:09:25 -0800525 &acquire_ctx);
Eric Anholt57692c92018-04-30 11:10:58 -0700526 if (ret)
527 goto fail;
528
Eric Anholt7122b682018-06-06 10:48:51 -0700529 mutex_lock(&v3d->sched_lock);
Eric Anholta783a092019-04-16 15:58:53 -0700530 if (bin) {
531 ret = v3d_push_job(v3d_priv, &bin->base, V3D_BIN);
Eric Anholt57692c92018-04-30 11:10:58 -0700532 if (ret)
533 goto fail_unreserve;
534
Eric Anholta783a092019-04-16 15:58:53 -0700535 render->bin_done_fence = dma_fence_get(bin->base.done_fence);
Eric Anholt57692c92018-04-30 11:10:58 -0700536 }
537
Eric Anholta783a092019-04-16 15:58:53 -0700538 ret = v3d_push_job(v3d_priv, &render->base, V3D_RENDER);
Eric Anholt57692c92018-04-30 11:10:58 -0700539 if (ret)
540 goto fail_unreserve;
Eric Anholt7122b682018-06-06 10:48:51 -0700541 mutex_unlock(&v3d->sched_lock);
Eric Anholt57692c92018-04-30 11:10:58 -0700542
Eric Anholta783a092019-04-16 15:58:53 -0700543 v3d_attach_fences_and_unlock_reservation(file_priv,
544 &render->base, &acquire_ctx,
545 args->out_sync);
Eric Anholt57692c92018-04-30 11:10:58 -0700546
Eric Anholta783a092019-04-16 15:58:53 -0700547 if (bin)
548 v3d_job_put(&bin->base);
549 v3d_job_put(&render->base);
Eric Anholt57692c92018-04-30 11:10:58 -0700550
551 return 0;
552
553fail_unreserve:
Eric Anholt7122b682018-06-06 10:48:51 -0700554 mutex_unlock(&v3d->sched_lock);
Eric Anholta783a092019-04-16 15:58:53 -0700555 drm_gem_unlock_reservations(render->base.bo,
556 render->base.bo_count, &acquire_ctx);
Eric Anholt57692c92018-04-30 11:10:58 -0700557fail:
Eric Anholta783a092019-04-16 15:58:53 -0700558 if (bin)
559 v3d_job_put(&bin->base);
560 v3d_job_put(&render->base);
Eric Anholt57692c92018-04-30 11:10:58 -0700561
562 return ret;
563}
564
Eric Anholt1584f162018-11-28 15:09:25 -0800565/**
566 * v3d_submit_tfu_ioctl() - Submits a TFU (texture formatting) job to the V3D.
567 * @dev: DRM device
568 * @data: ioctl argument
569 * @file_priv: DRM file for this fd
570 *
571 * Userspace provides the register setup for the TFU, which we don't
572 * need to validate since the TFU is behind the MMU.
573 */
574int
575v3d_submit_tfu_ioctl(struct drm_device *dev, void *data,
576 struct drm_file *file_priv)
577{
578 struct v3d_dev *v3d = to_v3d_dev(dev);
579 struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
580 struct drm_v3d_submit_tfu *args = data;
581 struct v3d_tfu_job *job;
582 struct ww_acquire_ctx acquire_ctx;
Eric Anholt1584f162018-11-28 15:09:25 -0800583 int ret = 0;
Eric Anholt1584f162018-11-28 15:09:25 -0800584
Eric Anholt55a9b742018-11-30 16:57:58 -0800585 trace_v3d_submit_tfu_ioctl(&v3d->drm, args->iia);
586
Eric Anholt1584f162018-11-28 15:09:25 -0800587 job = kcalloc(1, sizeof(*job), GFP_KERNEL);
588 if (!job)
589 return -ENOMEM;
590
Eric Anholta783a092019-04-16 15:58:53 -0700591 ret = v3d_job_init(v3d, file_priv, &job->base,
592 v3d_job_free, args->in_sync);
593 if (ret) {
Eric Anholt1584f162018-11-28 15:09:25 -0800594 kfree(job);
595 return ret;
596 }
597
Eric Anholta783a092019-04-16 15:58:53 -0700598 job->base.bo = kcalloc(ARRAY_SIZE(args->bo_handles),
599 sizeof(*job->base.bo), GFP_KERNEL);
600 if (!job->base.bo) {
601 v3d_job_put(&job->base);
602 return -ENOMEM;
603 }
Eric Anholt1584f162018-11-28 15:09:25 -0800604
605 job->args = *args;
Eric Anholt1584f162018-11-28 15:09:25 -0800606
607 spin_lock(&file_priv->table_lock);
Eric Anholta783a092019-04-16 15:58:53 -0700608 for (job->base.bo_count = 0;
609 job->base.bo_count < ARRAY_SIZE(args->bo_handles);
610 job->base.bo_count++) {
Eric Anholt1584f162018-11-28 15:09:25 -0800611 struct drm_gem_object *bo;
612
Eric Anholta783a092019-04-16 15:58:53 -0700613 if (!args->bo_handles[job->base.bo_count])
Eric Anholt1584f162018-11-28 15:09:25 -0800614 break;
615
616 bo = idr_find(&file_priv->object_idr,
Eric Anholta783a092019-04-16 15:58:53 -0700617 args->bo_handles[job->base.bo_count]);
Eric Anholt1584f162018-11-28 15:09:25 -0800618 if (!bo) {
619 DRM_DEBUG("Failed to look up GEM BO %d: %d\n",
Eric Anholta783a092019-04-16 15:58:53 -0700620 job->base.bo_count,
621 args->bo_handles[job->base.bo_count]);
Eric Anholt1584f162018-11-28 15:09:25 -0800622 ret = -ENOENT;
623 spin_unlock(&file_priv->table_lock);
624 goto fail;
625 }
626 drm_gem_object_get(bo);
Eric Anholta783a092019-04-16 15:58:53 -0700627 job->base.bo[job->base.bo_count] = bo;
Eric Anholt1584f162018-11-28 15:09:25 -0800628 }
629 spin_unlock(&file_priv->table_lock);
630
Eric Anholta783a092019-04-16 15:58:53 -0700631 ret = v3d_lock_bo_reservations(job->base.bo, job->base.bo_count,
632 &acquire_ctx);
Eric Anholt1584f162018-11-28 15:09:25 -0800633 if (ret)
634 goto fail;
635
636 mutex_lock(&v3d->sched_lock);
Eric Anholta783a092019-04-16 15:58:53 -0700637 ret = v3d_push_job(v3d_priv, &job->base, V3D_TFU);
Eric Anholt1584f162018-11-28 15:09:25 -0800638 if (ret)
639 goto fail_unreserve;
Eric Anholt1584f162018-11-28 15:09:25 -0800640 mutex_unlock(&v3d->sched_lock);
641
Eric Anholta783a092019-04-16 15:58:53 -0700642 v3d_attach_fences_and_unlock_reservation(file_priv,
643 &job->base, &acquire_ctx,
644 args->out_sync);
Eric Anholt1584f162018-11-28 15:09:25 -0800645
Eric Anholta783a092019-04-16 15:58:53 -0700646 v3d_job_put(&job->base);
Eric Anholt1584f162018-11-28 15:09:25 -0800647
648 return 0;
649
650fail_unreserve:
651 mutex_unlock(&v3d->sched_lock);
Eric Anholta783a092019-04-16 15:58:53 -0700652 drm_gem_unlock_reservations(job->base.bo, job->base.bo_count,
653 &acquire_ctx);
Eric Anholt1584f162018-11-28 15:09:25 -0800654fail:
Eric Anholta783a092019-04-16 15:58:53 -0700655 v3d_job_put(&job->base);
Eric Anholt1584f162018-11-28 15:09:25 -0800656
657 return ret;
658}
659
Eric Anholt57692c92018-04-30 11:10:58 -0700660int
661v3d_gem_init(struct drm_device *dev)
662{
663 struct v3d_dev *v3d = to_v3d_dev(dev);
664 u32 pt_size = 4096 * 1024;
665 int ret, i;
666
667 for (i = 0; i < V3D_MAX_QUEUES; i++)
668 v3d->queue[i].fence_context = dma_fence_context_alloc(1);
669
670 spin_lock_init(&v3d->mm_lock);
671 spin_lock_init(&v3d->job_lock);
672 mutex_init(&v3d->bo_lock);
673 mutex_init(&v3d->reset_lock);
Eric Anholt7122b682018-06-06 10:48:51 -0700674 mutex_init(&v3d->sched_lock);
Eric Anholt57692c92018-04-30 11:10:58 -0700675
676 /* Note: We don't allocate address 0. Various bits of HW
677 * treat 0 as special, such as the occlusion query counters
678 * where 0 means "disabled".
679 */
680 drm_mm_init(&v3d->mm, 1, pt_size / sizeof(u32) - 1);
681
682 v3d->pt = dma_alloc_wc(v3d->dev, pt_size,
683 &v3d->pt_paddr,
684 GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);
685 if (!v3d->pt) {
686 drm_mm_takedown(&v3d->mm);
687 dev_err(v3d->dev,
688 "Failed to allocate page tables. "
689 "Please ensure you have CMA enabled.\n");
690 return -ENOMEM;
691 }
692
693 v3d_init_hw_state(v3d);
694 v3d_mmu_set_page_table(v3d);
695
696 ret = v3d_sched_init(v3d);
697 if (ret) {
698 drm_mm_takedown(&v3d->mm);
699 dma_free_coherent(v3d->dev, 4096 * 1024, (void *)v3d->pt,
700 v3d->pt_paddr);
701 }
702
703 return 0;
704}
705
706void
707v3d_gem_destroy(struct drm_device *dev)
708{
709 struct v3d_dev *v3d = to_v3d_dev(dev);
Eric Anholt57692c92018-04-30 11:10:58 -0700710
711 v3d_sched_fini(v3d);
712
Eric Anholta783a092019-04-16 15:58:53 -0700713 /* Waiting for jobs to finish would need to be done before
Eric Anholt57692c92018-04-30 11:10:58 -0700714 * unregistering V3D.
715 */
Eric Anholt14d1d192018-06-05 12:03:01 -0700716 WARN_ON(v3d->bin_job);
717 WARN_ON(v3d->render_job);
Eric Anholt57692c92018-04-30 11:10:58 -0700718
719 drm_mm_takedown(&v3d->mm);
720
721 dma_free_coherent(v3d->dev, 4096 * 1024, (void *)v3d->pt, v3d->pt_paddr);
722}