1 // SPDX-License-Identifier: GPL-2.0+
2 /* Copyright (C) 2014-2018 Broadcom */
3
4 #include <linux/device.h>
5 #include <linux/dma-mapping.h>
6 #include <linux/io.h>
7 #include <linux/module.h>
8 #include <linux/platform_device.h>
9 #include <linux/reset.h>
10 #include <linux/sched/signal.h>
11 #include <linux/uaccess.h>
12
13 #include <drm/drm_managed.h>
14 #include <drm/drm_syncobj.h>
15 #include <uapi/drm/v3d_drm.h>
16
17 #include "v3d_drv.h"
18 #include "v3d_regs.h"
19 #include "v3d_trace.h"
20
21 static void
v3d_init_core(struct v3d_dev * v3d,int core)22 v3d_init_core(struct v3d_dev *v3d, int core)
23 {
24 /* Set OVRTMUOUT, which means that the texture sampler uniform
25 * configuration's tmu output type field is used, instead of
26 * using the hardware default behavior based on the texture
27 * type. If you want the default behavior, you can still put
28 * "2" in the indirect texture state's output_type field.
29 */
30 if (v3d->ver < 40)
31 V3D_CORE_WRITE(core, V3D_CTL_MISCCFG, V3D_MISCCFG_OVRTMUOUT);
32
33 /* Whenever we flush the L2T cache, we always want to flush
34 * the whole thing.
35 */
36 V3D_CORE_WRITE(core, V3D_CTL_L2TFLSTA, 0);
37 V3D_CORE_WRITE(core, V3D_CTL_L2TFLEND, ~0);
38 }
39
40 /* Sets invariant state for the HW. */
41 static void
v3d_init_hw_state(struct v3d_dev * v3d)42 v3d_init_hw_state(struct v3d_dev *v3d)
43 {
44 v3d_init_core(v3d, 0);
45 }
46
47 static void
v3d_idle_axi(struct v3d_dev * v3d,int core)48 v3d_idle_axi(struct v3d_dev *v3d, int core)
49 {
50 V3D_CORE_WRITE(core, V3D_GMP_CFG, V3D_GMP_CFG_STOP_REQ);
51
52 if (wait_for((V3D_CORE_READ(core, V3D_GMP_STATUS) &
53 (V3D_GMP_STATUS_RD_COUNT_MASK |
54 V3D_GMP_STATUS_WR_COUNT_MASK |
55 V3D_GMP_STATUS_CFG_BUSY)) == 0, 100)) {
56 DRM_ERROR("Failed to wait for safe GMP shutdown\n");
57 }
58 }
59
60 static void
v3d_idle_gca(struct v3d_dev * v3d)61 v3d_idle_gca(struct v3d_dev *v3d)
62 {
63 if (v3d->ver >= 41)
64 return;
65
66 V3D_GCA_WRITE(V3D_GCA_SAFE_SHUTDOWN, V3D_GCA_SAFE_SHUTDOWN_EN);
67
68 if (wait_for((V3D_GCA_READ(V3D_GCA_SAFE_SHUTDOWN_ACK) &
69 V3D_GCA_SAFE_SHUTDOWN_ACK_ACKED) ==
70 V3D_GCA_SAFE_SHUTDOWN_ACK_ACKED, 100)) {
71 DRM_ERROR("Failed to wait for safe GCA shutdown\n");
72 }
73 }
74
75 static void
v3d_reset_by_bridge(struct v3d_dev * v3d)76 v3d_reset_by_bridge(struct v3d_dev *v3d)
77 {
78 int version = V3D_BRIDGE_READ(V3D_TOP_GR_BRIDGE_REVISION);
79
80 if (V3D_GET_FIELD(version, V3D_TOP_GR_BRIDGE_MAJOR) == 2) {
81 V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_0,
82 V3D_TOP_GR_BRIDGE_SW_INIT_0_V3D_CLK_108_SW_INIT);
83 V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_0, 0);
84
85 /* GFXH-1383: The SW_INIT may cause a stray write to address 0
86 * of the unit, so reset it to its power-on value here.
87 */
88 V3D_WRITE(V3D_HUB_AXICFG, V3D_HUB_AXICFG_MAX_LEN_MASK);
89 } else {
90 WARN_ON_ONCE(V3D_GET_FIELD(version,
91 V3D_TOP_GR_BRIDGE_MAJOR) != 7);
92 V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_1,
93 V3D_TOP_GR_BRIDGE_SW_INIT_1_V3D_CLK_108_SW_INIT);
94 V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_1, 0);
95 }
96 }
97
98 static void
v3d_reset_v3d(struct v3d_dev * v3d)99 v3d_reset_v3d(struct v3d_dev *v3d)
100 {
101 if (v3d->reset)
102 reset_control_reset(v3d->reset);
103 else
104 v3d_reset_by_bridge(v3d);
105
106 v3d_init_hw_state(v3d);
107 }
108
109 void
v3d_reset(struct v3d_dev * v3d)110 v3d_reset(struct v3d_dev *v3d)
111 {
112 struct drm_device *dev = &v3d->drm;
113
114 DRM_DEV_ERROR(dev->dev, "Resetting GPU for hang.\n");
115 DRM_DEV_ERROR(dev->dev, "V3D_ERR_STAT: 0x%08x\n",
116 V3D_CORE_READ(0, V3D_ERR_STAT));
117 trace_v3d_reset_begin(dev);
118
119 /* XXX: only needed for safe powerdown, not reset. */
120 if (false)
121 v3d_idle_axi(v3d, 0);
122
123 v3d_irq_disable(v3d);
124
125 v3d_idle_gca(v3d);
126 v3d_reset_v3d(v3d);
127
128 v3d_mmu_set_page_table(v3d);
129 v3d_irq_reset(v3d);
130
131 v3d_perfmon_stop(v3d, v3d->active_perfmon, false);
132
133 trace_v3d_reset_end(dev);
134 }
135
136 static void
v3d_flush_l3(struct v3d_dev * v3d)137 v3d_flush_l3(struct v3d_dev *v3d)
138 {
139 if (v3d->ver < 41) {
140 u32 gca_ctrl = V3D_GCA_READ(V3D_GCA_CACHE_CTRL);
141
142 V3D_GCA_WRITE(V3D_GCA_CACHE_CTRL,
143 gca_ctrl | V3D_GCA_CACHE_CTRL_FLUSH);
144
145 if (v3d->ver < 33) {
146 V3D_GCA_WRITE(V3D_GCA_CACHE_CTRL,
147 gca_ctrl & ~V3D_GCA_CACHE_CTRL_FLUSH);
148 }
149 }
150 }
151
152 /* Invalidates the (read-only) L2C cache. This was the L2 cache for
153 * uniforms and instructions on V3D 3.2.
154 */
155 static void
v3d_invalidate_l2c(struct v3d_dev * v3d,int core)156 v3d_invalidate_l2c(struct v3d_dev *v3d, int core)
157 {
158 if (v3d->ver > 32)
159 return;
160
161 V3D_CORE_WRITE(core, V3D_CTL_L2CACTL,
162 V3D_L2CACTL_L2CCLR |
163 V3D_L2CACTL_L2CENA);
164 }
165
166 /* Invalidates texture L2 cachelines */
167 static void
v3d_flush_l2t(struct v3d_dev * v3d,int core)168 v3d_flush_l2t(struct v3d_dev *v3d, int core)
169 {
170 /* While there is a busy bit (V3D_L2TCACTL_L2TFLS), we don't
171 * need to wait for completion before dispatching the job --
172 * L2T accesses will be stalled until the flush has completed.
173 * However, we do need to make sure we don't try to trigger a
174 * new flush while the L2_CLEAN queue is trying to
175 * synchronously clean after a job.
176 */
177 mutex_lock(&v3d->cache_clean_lock);
178 V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL,
179 V3D_L2TCACTL_L2TFLS |
180 V3D_SET_FIELD(V3D_L2TCACTL_FLM_FLUSH, V3D_L2TCACTL_FLM));
181 mutex_unlock(&v3d->cache_clean_lock);
182 }
183
184 /* Cleans texture L1 and L2 cachelines (writing back dirty data).
185 *
186 * For cleaning, which happens from the CACHE_CLEAN queue after CSD has
187 * executed, we need to make sure that the clean is done before
188 * signaling job completion. So, we synchronously wait before
189 * returning, and we make sure that L2 invalidates don't happen in the
190 * meantime to confuse our are-we-done checks.
191 */
192 void
v3d_clean_caches(struct v3d_dev * v3d)193 v3d_clean_caches(struct v3d_dev *v3d)
194 {
195 struct drm_device *dev = &v3d->drm;
196 int core = 0;
197
198 trace_v3d_cache_clean_begin(dev);
199
200 V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL, V3D_L2TCACTL_TMUWCF);
201 if (wait_for(!(V3D_CORE_READ(core, V3D_CTL_L2TCACTL) &
202 V3D_L2TCACTL_TMUWCF), 100)) {
203 DRM_ERROR("Timeout waiting for TMU write combiner flush\n");
204 }
205
206 mutex_lock(&v3d->cache_clean_lock);
207 V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL,
208 V3D_L2TCACTL_L2TFLS |
209 V3D_SET_FIELD(V3D_L2TCACTL_FLM_CLEAN, V3D_L2TCACTL_FLM));
210
211 if (wait_for(!(V3D_CORE_READ(core, V3D_CTL_L2TCACTL) &
212 V3D_L2TCACTL_L2TFLS), 100)) {
213 DRM_ERROR("Timeout waiting for L2T clean\n");
214 }
215
216 mutex_unlock(&v3d->cache_clean_lock);
217
218 trace_v3d_cache_clean_end(dev);
219 }
220
221 /* Invalidates the slice caches. These are read-only caches. */
222 static void
v3d_invalidate_slices(struct v3d_dev * v3d,int core)223 v3d_invalidate_slices(struct v3d_dev *v3d, int core)
224 {
225 V3D_CORE_WRITE(core, V3D_CTL_SLCACTL,
226 V3D_SET_FIELD(0xf, V3D_SLCACTL_TVCCS) |
227 V3D_SET_FIELD(0xf, V3D_SLCACTL_TDCCS) |
228 V3D_SET_FIELD(0xf, V3D_SLCACTL_UCC) |
229 V3D_SET_FIELD(0xf, V3D_SLCACTL_ICC));
230 }
231
232 void
v3d_invalidate_caches(struct v3d_dev * v3d)233 v3d_invalidate_caches(struct v3d_dev *v3d)
234 {
235 /* Invalidate the caches from the outside in. That way if
236 * another CL's concurrent use of nearby memory were to pull
237 * an invalidated cacheline back in, we wouldn't leave stale
238 * data in the inner cache.
239 */
240 v3d_flush_l3(v3d);
241 v3d_invalidate_l2c(v3d, 0);
242 v3d_flush_l2t(v3d, 0);
243 v3d_invalidate_slices(v3d, 0);
244 }
245
246 /* Takes the reservation lock on all the BOs being referenced, so that
247 * at queue submit time we can update the reservations.
248 *
249 * We don't lock the RCL the tile alloc/state BOs, or overflow memory
250 * (all of which are on exec->unref_list). They're entirely private
251 * to v3d, so we don't attach dma-buf fences to them.
252 */
253 static int
v3d_lock_bo_reservations(struct v3d_job * job,struct ww_acquire_ctx * acquire_ctx)254 v3d_lock_bo_reservations(struct v3d_job *job,
255 struct ww_acquire_ctx *acquire_ctx)
256 {
257 int i, ret;
258
259 ret = drm_gem_lock_reservations(job->bo, job->bo_count, acquire_ctx);
260 if (ret)
261 return ret;
262
263 for (i = 0; i < job->bo_count; i++) {
264 ret = dma_resv_reserve_fences(job->bo[i]->resv, 1);
265 if (ret)
266 goto fail;
267
268 ret = drm_sched_job_add_implicit_dependencies(&job->base,
269 job->bo[i], true);
270 if (ret)
271 goto fail;
272 }
273
274 return 0;
275
276 fail:
277 drm_gem_unlock_reservations(job->bo, job->bo_count, acquire_ctx);
278 return ret;
279 }
280
281 /**
282 * v3d_lookup_bos() - Sets up job->bo[] with the GEM objects
283 * referenced by the job.
284 * @dev: DRM device
285 * @file_priv: DRM file for this fd
286 * @job: V3D job being set up
287 * @bo_handles: GEM handles
288 * @bo_count: Number of GEM handles passed in
289 *
290 * The command validator needs to reference BOs by their index within
291 * the submitted job's BO list. This does the validation of the job's
292 * BO list and reference counting for the lifetime of the job.
293 *
294 * Note that this function doesn't need to unreference the BOs on
295 * failure, because that will happen at v3d_exec_cleanup() time.
296 */
297 static int
v3d_lookup_bos(struct drm_device * dev,struct drm_file * file_priv,struct v3d_job * job,u64 bo_handles,u32 bo_count)298 v3d_lookup_bos(struct drm_device *dev,
299 struct drm_file *file_priv,
300 struct v3d_job *job,
301 u64 bo_handles,
302 u32 bo_count)
303 {
304 job->bo_count = bo_count;
305
306 if (!job->bo_count) {
307 /* See comment on bo_index for why we have to check
308 * this.
309 */
310 DRM_DEBUG("Rendering requires BOs\n");
311 return -EINVAL;
312 }
313
314 return drm_gem_objects_lookup(file_priv,
315 (void __user *)(uintptr_t)bo_handles,
316 job->bo_count, &job->bo);
317 }
318
319 static void
v3d_job_free(struct kref * ref)320 v3d_job_free(struct kref *ref)
321 {
322 struct v3d_job *job = container_of(ref, struct v3d_job, refcount);
323 int i;
324
325 if (job->bo) {
326 for (i = 0; i < job->bo_count; i++)
327 drm_gem_object_put(job->bo[i]);
328 kvfree(job->bo);
329 }
330
331 dma_fence_put(job->irq_fence);
332 dma_fence_put(job->done_fence);
333
334 if (job->perfmon)
335 v3d_perfmon_put(job->perfmon);
336
337 kfree(job);
338 }
339
340 static void
v3d_render_job_free(struct kref * ref)341 v3d_render_job_free(struct kref *ref)
342 {
343 struct v3d_render_job *job = container_of(ref, struct v3d_render_job,
344 base.refcount);
345 struct v3d_bo *bo, *save;
346
347 list_for_each_entry_safe(bo, save, &job->unref_list, unref_head) {
348 drm_gem_object_put(&bo->base.base);
349 }
350
351 v3d_job_free(ref);
352 }
353
v3d_job_cleanup(struct v3d_job * job)354 void v3d_job_cleanup(struct v3d_job *job)
355 {
356 if (!job)
357 return;
358
359 drm_sched_job_cleanup(&job->base);
360 v3d_job_put(job);
361 }
362
v3d_job_put(struct v3d_job * job)363 void v3d_job_put(struct v3d_job *job)
364 {
365 kref_put(&job->refcount, job->free);
366 }
367
368 int
v3d_wait_bo_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)369 v3d_wait_bo_ioctl(struct drm_device *dev, void *data,
370 struct drm_file *file_priv)
371 {
372 int ret;
373 struct drm_v3d_wait_bo *args = data;
374 ktime_t start = ktime_get();
375 u64 delta_ns;
376 unsigned long timeout_jiffies =
377 nsecs_to_jiffies_timeout(args->timeout_ns);
378
379 if (args->pad != 0)
380 return -EINVAL;
381
382 ret = drm_gem_dma_resv_wait(file_priv, args->handle,
383 true, timeout_jiffies);
384
385 /* Decrement the user's timeout, in case we got interrupted
386 * such that the ioctl will be restarted.
387 */
388 delta_ns = ktime_to_ns(ktime_sub(ktime_get(), start));
389 if (delta_ns < args->timeout_ns)
390 args->timeout_ns -= delta_ns;
391 else
392 args->timeout_ns = 0;
393
394 /* Asked to wait beyond the jiffie/scheduler precision? */
395 if (ret == -ETIME && args->timeout_ns)
396 ret = -EAGAIN;
397
398 return ret;
399 }
400
401 static int
v3d_job_init(struct v3d_dev * v3d,struct drm_file * file_priv,void ** container,size_t size,void (* free)(struct kref * ref),u32 in_sync,struct v3d_submit_ext * se,enum v3d_queue queue)402 v3d_job_init(struct v3d_dev *v3d, struct drm_file *file_priv,
403 void **container, size_t size, void (*free)(struct kref *ref),
404 u32 in_sync, struct v3d_submit_ext *se, enum v3d_queue queue)
405 {
406 struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
407 struct v3d_job *job;
408 bool has_multisync = se && (se->flags & DRM_V3D_EXT_ID_MULTI_SYNC);
409 int ret, i;
410
411 *container = kcalloc(1, size, GFP_KERNEL);
412 if (!*container) {
413 DRM_ERROR("Cannot allocate memory for v3d job.");
414 return -ENOMEM;
415 }
416
417 job = *container;
418 job->v3d = v3d;
419 job->free = free;
420
421 ret = drm_sched_job_init(&job->base, &v3d_priv->sched_entity[queue],
422 v3d_priv);
423 if (ret)
424 goto fail;
425
426 if (has_multisync) {
427 if (se->in_sync_count && se->wait_stage == queue) {
428 struct drm_v3d_sem __user *handle = u64_to_user_ptr(se->in_syncs);
429
430 for (i = 0; i < se->in_sync_count; i++) {
431 struct drm_v3d_sem in;
432
433 if (copy_from_user(&in, handle++, sizeof(in))) {
434 ret = -EFAULT;
435 DRM_DEBUG("Failed to copy wait dep handle.\n");
436 goto fail_deps;
437 }
438 ret = drm_sched_job_add_syncobj_dependency(&job->base, file_priv, in.handle, 0);
439
440 // TODO: Investigate why this was filtered out for the IOCTL.
441 if (ret && ret != -ENOENT)
442 goto fail_deps;
443 }
444 }
445 } else {
446 ret = drm_sched_job_add_syncobj_dependency(&job->base, file_priv, in_sync, 0);
447
448 // TODO: Investigate why this was filtered out for the IOCTL.
449 if (ret && ret != -ENOENT)
450 goto fail_deps;
451 }
452
453 kref_init(&job->refcount);
454
455 return 0;
456
457 fail_deps:
458 drm_sched_job_cleanup(&job->base);
459 fail:
460 kfree(*container);
461 *container = NULL;
462
463 return ret;
464 }
465
466 static void
v3d_push_job(struct v3d_job * job)467 v3d_push_job(struct v3d_job *job)
468 {
469 drm_sched_job_arm(&job->base);
470
471 job->done_fence = dma_fence_get(&job->base.s_fence->finished);
472
473 /* put by scheduler job completion */
474 kref_get(&job->refcount);
475
476 drm_sched_entity_push_job(&job->base);
477 }
478
479 static void
v3d_attach_fences_and_unlock_reservation(struct drm_file * file_priv,struct v3d_job * job,struct ww_acquire_ctx * acquire_ctx,u32 out_sync,struct v3d_submit_ext * se,struct dma_fence * done_fence)480 v3d_attach_fences_and_unlock_reservation(struct drm_file *file_priv,
481 struct v3d_job *job,
482 struct ww_acquire_ctx *acquire_ctx,
483 u32 out_sync,
484 struct v3d_submit_ext *se,
485 struct dma_fence *done_fence)
486 {
487 struct drm_syncobj *sync_out;
488 bool has_multisync = se && (se->flags & DRM_V3D_EXT_ID_MULTI_SYNC);
489 int i;
490
491 for (i = 0; i < job->bo_count; i++) {
492 /* XXX: Use shared fences for read-only objects. */
493 dma_resv_add_fence(job->bo[i]->resv, job->done_fence,
494 DMA_RESV_USAGE_WRITE);
495 }
496
497 drm_gem_unlock_reservations(job->bo, job->bo_count, acquire_ctx);
498
499 /* Update the return sync object for the job */
500 /* If it only supports a single signal semaphore*/
501 if (!has_multisync) {
502 sync_out = drm_syncobj_find(file_priv, out_sync);
503 if (sync_out) {
504 drm_syncobj_replace_fence(sync_out, done_fence);
505 drm_syncobj_put(sync_out);
506 }
507 return;
508 }
509
510 /* If multiple semaphores extension is supported */
511 if (se->out_sync_count) {
512 for (i = 0; i < se->out_sync_count; i++) {
513 drm_syncobj_replace_fence(se->out_syncs[i].syncobj,
514 done_fence);
515 drm_syncobj_put(se->out_syncs[i].syncobj);
516 }
517 kvfree(se->out_syncs);
518 }
519 }
520
521 static void
v3d_put_multisync_post_deps(struct v3d_submit_ext * se)522 v3d_put_multisync_post_deps(struct v3d_submit_ext *se)
523 {
524 unsigned int i;
525
526 if (!(se && se->out_sync_count))
527 return;
528
529 for (i = 0; i < se->out_sync_count; i++)
530 drm_syncobj_put(se->out_syncs[i].syncobj);
531 kvfree(se->out_syncs);
532 }
533
534 static int
v3d_get_multisync_post_deps(struct drm_file * file_priv,struct v3d_submit_ext * se,u32 count,u64 handles)535 v3d_get_multisync_post_deps(struct drm_file *file_priv,
536 struct v3d_submit_ext *se,
537 u32 count, u64 handles)
538 {
539 struct drm_v3d_sem __user *post_deps;
540 int i, ret;
541
542 if (!count)
543 return 0;
544
545 se->out_syncs = (struct v3d_submit_outsync *)
546 kvmalloc_array(count,
547 sizeof(struct v3d_submit_outsync),
548 GFP_KERNEL);
549 if (!se->out_syncs)
550 return -ENOMEM;
551
552 post_deps = u64_to_user_ptr(handles);
553
554 for (i = 0; i < count; i++) {
555 struct drm_v3d_sem out;
556
557 if (copy_from_user(&out, post_deps++, sizeof(out))) {
558 ret = -EFAULT;
559 DRM_DEBUG("Failed to copy post dep handles\n");
560 goto fail;
561 }
562
563 se->out_syncs[i].syncobj = drm_syncobj_find(file_priv,
564 out.handle);
565 if (!se->out_syncs[i].syncobj) {
566 ret = -EINVAL;
567 goto fail;
568 }
569 }
570 se->out_sync_count = count;
571
572 return 0;
573
574 fail:
575 for (i--; i >= 0; i--)
576 drm_syncobj_put(se->out_syncs[i].syncobj);
577 kvfree(se->out_syncs);
578
579 return ret;
580 }
581
582 /* Get data for multiple binary semaphores synchronization. Parse syncobj
583 * to be signaled when job completes (out_sync).
584 */
585 static int
v3d_get_multisync_submit_deps(struct drm_file * file_priv,struct drm_v3d_extension __user * ext,void * data)586 v3d_get_multisync_submit_deps(struct drm_file *file_priv,
587 struct drm_v3d_extension __user *ext,
588 void *data)
589 {
590 struct drm_v3d_multi_sync multisync;
591 struct v3d_submit_ext *se = data;
592 int ret;
593
594 if (copy_from_user(&multisync, ext, sizeof(multisync)))
595 return -EFAULT;
596
597 if (multisync.pad)
598 return -EINVAL;
599
600 ret = v3d_get_multisync_post_deps(file_priv, data, multisync.out_sync_count,
601 multisync.out_syncs);
602 if (ret)
603 return ret;
604
605 se->in_sync_count = multisync.in_sync_count;
606 se->in_syncs = multisync.in_syncs;
607 se->flags |= DRM_V3D_EXT_ID_MULTI_SYNC;
608 se->wait_stage = multisync.wait_stage;
609
610 return 0;
611 }
612
613 /* Whenever userspace sets ioctl extensions, v3d_get_extensions parses data
614 * according to the extension id (name).
615 */
616 static int
v3d_get_extensions(struct drm_file * file_priv,u64 ext_handles,void * data)617 v3d_get_extensions(struct drm_file *file_priv,
618 u64 ext_handles,
619 void *data)
620 {
621 struct drm_v3d_extension __user *user_ext;
622 int ret;
623
624 user_ext = u64_to_user_ptr(ext_handles);
625 while (user_ext) {
626 struct drm_v3d_extension ext;
627
628 if (copy_from_user(&ext, user_ext, sizeof(ext))) {
629 DRM_DEBUG("Failed to copy submit extension\n");
630 return -EFAULT;
631 }
632
633 switch (ext.id) {
634 case DRM_V3D_EXT_ID_MULTI_SYNC:
635 ret = v3d_get_multisync_submit_deps(file_priv, user_ext, data);
636 if (ret)
637 return ret;
638 break;
639 default:
640 DRM_DEBUG_DRIVER("Unknown extension id: %d\n", ext.id);
641 return -EINVAL;
642 }
643
644 user_ext = u64_to_user_ptr(ext.next);
645 }
646
647 return 0;
648 }
649
650 /**
651 * v3d_submit_cl_ioctl() - Submits a job (frame) to the V3D.
652 * @dev: DRM device
653 * @data: ioctl argument
654 * @file_priv: DRM file for this fd
655 *
656 * This is the main entrypoint for userspace to submit a 3D frame to
657 * the GPU. Userspace provides the binner command list (if
658 * applicable), and the kernel sets up the render command list to draw
659 * to the framebuffer described in the ioctl, using the command lists
660 * that the 3D engine's binner will produce.
661 */
662 int
v3d_submit_cl_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)663 v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
664 struct drm_file *file_priv)
665 {
666 struct v3d_dev *v3d = to_v3d_dev(dev);
667 struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
668 struct drm_v3d_submit_cl *args = data;
669 struct v3d_submit_ext se = {0};
670 struct v3d_bin_job *bin = NULL;
671 struct v3d_render_job *render = NULL;
672 struct v3d_job *clean_job = NULL;
673 struct v3d_job *last_job;
674 struct ww_acquire_ctx acquire_ctx;
675 int ret = 0;
676
677 trace_v3d_submit_cl_ioctl(&v3d->drm, args->rcl_start, args->rcl_end);
678
679 if (args->pad)
680 return -EINVAL;
681
682 if (args->flags &&
683 args->flags & ~(DRM_V3D_SUBMIT_CL_FLUSH_CACHE |
684 DRM_V3D_SUBMIT_EXTENSION)) {
685 DRM_INFO("invalid flags: %d\n", args->flags);
686 return -EINVAL;
687 }
688
689 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
690 ret = v3d_get_extensions(file_priv, args->extensions, &se);
691 if (ret) {
692 DRM_DEBUG("Failed to get extensions.\n");
693 return ret;
694 }
695 }
696
697 ret = v3d_job_init(v3d, file_priv, (void *)&render, sizeof(*render),
698 v3d_render_job_free, args->in_sync_rcl, &se, V3D_RENDER);
699 if (ret)
700 goto fail;
701
702 render->start = args->rcl_start;
703 render->end = args->rcl_end;
704 INIT_LIST_HEAD(&render->unref_list);
705
706 if (args->bcl_start != args->bcl_end) {
707 ret = v3d_job_init(v3d, file_priv, (void *)&bin, sizeof(*bin),
708 v3d_job_free, args->in_sync_bcl, &se, V3D_BIN);
709 if (ret)
710 goto fail;
711
712 bin->start = args->bcl_start;
713 bin->end = args->bcl_end;
714 bin->qma = args->qma;
715 bin->qms = args->qms;
716 bin->qts = args->qts;
717 bin->render = render;
718 }
719
720 if (args->flags & DRM_V3D_SUBMIT_CL_FLUSH_CACHE) {
721 ret = v3d_job_init(v3d, file_priv, (void *)&clean_job, sizeof(*clean_job),
722 v3d_job_free, 0, NULL, V3D_CACHE_CLEAN);
723 if (ret)
724 goto fail;
725
726 last_job = clean_job;
727 } else {
728 last_job = &render->base;
729 }
730
731 ret = v3d_lookup_bos(dev, file_priv, last_job,
732 args->bo_handles, args->bo_handle_count);
733 if (ret)
734 goto fail;
735
736 ret = v3d_lock_bo_reservations(last_job, &acquire_ctx);
737 if (ret)
738 goto fail;
739
740 if (args->perfmon_id) {
741 render->base.perfmon = v3d_perfmon_find(v3d_priv,
742 args->perfmon_id);
743
744 if (!render->base.perfmon) {
745 ret = -ENOENT;
746 goto fail_perfmon;
747 }
748 }
749
750 mutex_lock(&v3d->sched_lock);
751 if (bin) {
752 bin->base.perfmon = render->base.perfmon;
753 v3d_perfmon_get(bin->base.perfmon);
754 v3d_push_job(&bin->base);
755
756 ret = drm_sched_job_add_dependency(&render->base.base,
757 dma_fence_get(bin->base.done_fence));
758 if (ret)
759 goto fail_unreserve;
760 }
761
762 v3d_push_job(&render->base);
763
764 if (clean_job) {
765 struct dma_fence *render_fence =
766 dma_fence_get(render->base.done_fence);
767 ret = drm_sched_job_add_dependency(&clean_job->base,
768 render_fence);
769 if (ret)
770 goto fail_unreserve;
771 clean_job->perfmon = render->base.perfmon;
772 v3d_perfmon_get(clean_job->perfmon);
773 v3d_push_job(clean_job);
774 }
775
776 mutex_unlock(&v3d->sched_lock);
777
778 v3d_attach_fences_and_unlock_reservation(file_priv,
779 last_job,
780 &acquire_ctx,
781 args->out_sync,
782 &se,
783 last_job->done_fence);
784
785 if (bin)
786 v3d_job_put(&bin->base);
787 v3d_job_put(&render->base);
788 if (clean_job)
789 v3d_job_put(clean_job);
790
791 return 0;
792
793 fail_unreserve:
794 mutex_unlock(&v3d->sched_lock);
795 fail_perfmon:
796 drm_gem_unlock_reservations(last_job->bo,
797 last_job->bo_count, &acquire_ctx);
798 fail:
799 v3d_job_cleanup((void *)bin);
800 v3d_job_cleanup((void *)render);
801 v3d_job_cleanup(clean_job);
802 v3d_put_multisync_post_deps(&se);
803
804 return ret;
805 }
806
807 /**
808 * v3d_submit_tfu_ioctl() - Submits a TFU (texture formatting) job to the V3D.
809 * @dev: DRM device
810 * @data: ioctl argument
811 * @file_priv: DRM file for this fd
812 *
813 * Userspace provides the register setup for the TFU, which we don't
814 * need to validate since the TFU is behind the MMU.
815 */
816 int
v3d_submit_tfu_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)817 v3d_submit_tfu_ioctl(struct drm_device *dev, void *data,
818 struct drm_file *file_priv)
819 {
820 struct v3d_dev *v3d = to_v3d_dev(dev);
821 struct drm_v3d_submit_tfu *args = data;
822 struct v3d_submit_ext se = {0};
823 struct v3d_tfu_job *job = NULL;
824 struct ww_acquire_ctx acquire_ctx;
825 int ret = 0;
826
827 trace_v3d_submit_tfu_ioctl(&v3d->drm, args->iia);
828
829 if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) {
830 DRM_DEBUG("invalid flags: %d\n", args->flags);
831 return -EINVAL;
832 }
833
834 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
835 ret = v3d_get_extensions(file_priv, args->extensions, &se);
836 if (ret) {
837 DRM_DEBUG("Failed to get extensions.\n");
838 return ret;
839 }
840 }
841
842 ret = v3d_job_init(v3d, file_priv, (void *)&job, sizeof(*job),
843 v3d_job_free, args->in_sync, &se, V3D_TFU);
844 if (ret)
845 goto fail;
846
847 job->base.bo = kcalloc(ARRAY_SIZE(args->bo_handles),
848 sizeof(*job->base.bo), GFP_KERNEL);
849 if (!job->base.bo) {
850 ret = -ENOMEM;
851 goto fail;
852 }
853
854 job->args = *args;
855
856 for (job->base.bo_count = 0;
857 job->base.bo_count < ARRAY_SIZE(args->bo_handles);
858 job->base.bo_count++) {
859 struct drm_gem_object *bo;
860
861 if (!args->bo_handles[job->base.bo_count])
862 break;
863
864 bo = drm_gem_object_lookup(file_priv, args->bo_handles[job->base.bo_count]);
865 if (!bo) {
866 DRM_DEBUG("Failed to look up GEM BO %d: %d\n",
867 job->base.bo_count,
868 args->bo_handles[job->base.bo_count]);
869 ret = -ENOENT;
870 goto fail;
871 }
872 job->base.bo[job->base.bo_count] = bo;
873 }
874
875 ret = v3d_lock_bo_reservations(&job->base, &acquire_ctx);
876 if (ret)
877 goto fail;
878
879 mutex_lock(&v3d->sched_lock);
880 v3d_push_job(&job->base);
881 mutex_unlock(&v3d->sched_lock);
882
883 v3d_attach_fences_and_unlock_reservation(file_priv,
884 &job->base, &acquire_ctx,
885 args->out_sync,
886 &se,
887 job->base.done_fence);
888
889 v3d_job_put(&job->base);
890
891 return 0;
892
893 fail:
894 v3d_job_cleanup((void *)job);
895 v3d_put_multisync_post_deps(&se);
896
897 return ret;
898 }
899
900 /**
901 * v3d_submit_csd_ioctl() - Submits a CSD (texture formatting) job to the V3D.
902 * @dev: DRM device
903 * @data: ioctl argument
904 * @file_priv: DRM file for this fd
905 *
906 * Userspace provides the register setup for the CSD, which we don't
907 * need to validate since the CSD is behind the MMU.
908 */
909 int
v3d_submit_csd_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)910 v3d_submit_csd_ioctl(struct drm_device *dev, void *data,
911 struct drm_file *file_priv)
912 {
913 struct v3d_dev *v3d = to_v3d_dev(dev);
914 struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
915 struct drm_v3d_submit_csd *args = data;
916 struct v3d_submit_ext se = {0};
917 struct v3d_csd_job *job = NULL;
918 struct v3d_job *clean_job = NULL;
919 struct ww_acquire_ctx acquire_ctx;
920 int ret;
921
922 trace_v3d_submit_csd_ioctl(&v3d->drm, args->cfg[5], args->cfg[6]);
923
924 if (args->pad)
925 return -EINVAL;
926
927 if (!v3d_has_csd(v3d)) {
928 DRM_DEBUG("Attempting CSD submit on non-CSD hardware\n");
929 return -EINVAL;
930 }
931
932 if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) {
933 DRM_INFO("invalid flags: %d\n", args->flags);
934 return -EINVAL;
935 }
936
937 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
938 ret = v3d_get_extensions(file_priv, args->extensions, &se);
939 if (ret) {
940 DRM_DEBUG("Failed to get extensions.\n");
941 return ret;
942 }
943 }
944
945 ret = v3d_job_init(v3d, file_priv, (void *)&job, sizeof(*job),
946 v3d_job_free, args->in_sync, &se, V3D_CSD);
947 if (ret)
948 goto fail;
949
950 ret = v3d_job_init(v3d, file_priv, (void *)&clean_job, sizeof(*clean_job),
951 v3d_job_free, 0, NULL, V3D_CACHE_CLEAN);
952 if (ret)
953 goto fail;
954
955 job->args = *args;
956
957 ret = v3d_lookup_bos(dev, file_priv, clean_job,
958 args->bo_handles, args->bo_handle_count);
959 if (ret)
960 goto fail;
961
962 ret = v3d_lock_bo_reservations(clean_job, &acquire_ctx);
963 if (ret)
964 goto fail;
965
966 if (args->perfmon_id) {
967 job->base.perfmon = v3d_perfmon_find(v3d_priv,
968 args->perfmon_id);
969 if (!job->base.perfmon) {
970 ret = -ENOENT;
971 goto fail_perfmon;
972 }
973 }
974
975 mutex_lock(&v3d->sched_lock);
976 v3d_push_job(&job->base);
977
978 ret = drm_sched_job_add_dependency(&clean_job->base,
979 dma_fence_get(job->base.done_fence));
980 if (ret)
981 goto fail_unreserve;
982
983 v3d_push_job(clean_job);
984 mutex_unlock(&v3d->sched_lock);
985
986 v3d_attach_fences_and_unlock_reservation(file_priv,
987 clean_job,
988 &acquire_ctx,
989 args->out_sync,
990 &se,
991 clean_job->done_fence);
992
993 v3d_job_put(&job->base);
994 v3d_job_put(clean_job);
995
996 return 0;
997
998 fail_unreserve:
999 mutex_unlock(&v3d->sched_lock);
1000 fail_perfmon:
1001 drm_gem_unlock_reservations(clean_job->bo, clean_job->bo_count,
1002 &acquire_ctx);
1003 fail:
1004 v3d_job_cleanup((void *)job);
1005 v3d_job_cleanup(clean_job);
1006 v3d_put_multisync_post_deps(&se);
1007
1008 return ret;
1009 }
1010
1011 int
v3d_gem_init(struct drm_device * dev)1012 v3d_gem_init(struct drm_device *dev)
1013 {
1014 struct v3d_dev *v3d = to_v3d_dev(dev);
1015 u32 pt_size = 4096 * 1024;
1016 int ret, i;
1017
1018 for (i = 0; i < V3D_MAX_QUEUES; i++)
1019 v3d->queue[i].fence_context = dma_fence_context_alloc(1);
1020
1021 spin_lock_init(&v3d->mm_lock);
1022 spin_lock_init(&v3d->job_lock);
1023 ret = drmm_mutex_init(dev, &v3d->bo_lock);
1024 if (ret)
1025 return ret;
1026 ret = drmm_mutex_init(dev, &v3d->reset_lock);
1027 if (ret)
1028 return ret;
1029 ret = drmm_mutex_init(dev, &v3d->sched_lock);
1030 if (ret)
1031 return ret;
1032 ret = drmm_mutex_init(dev, &v3d->cache_clean_lock);
1033 if (ret)
1034 return ret;
1035
1036 /* Note: We don't allocate address 0. Various bits of HW
1037 * treat 0 as special, such as the occlusion query counters
1038 * where 0 means "disabled".
1039 */
1040 drm_mm_init(&v3d->mm, 1, pt_size / sizeof(u32) - 1);
1041
1042 v3d->pt = dma_alloc_wc(v3d->drm.dev, pt_size,
1043 &v3d->pt_paddr,
1044 GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);
1045 if (!v3d->pt) {
1046 drm_mm_takedown(&v3d->mm);
1047 dev_err(v3d->drm.dev,
1048 "Failed to allocate page tables. Please ensure you have DMA enabled.\n");
1049 return -ENOMEM;
1050 }
1051
1052 v3d_init_hw_state(v3d);
1053 v3d_mmu_set_page_table(v3d);
1054
1055 ret = v3d_sched_init(v3d);
1056 if (ret) {
1057 drm_mm_takedown(&v3d->mm);
1058 dma_free_coherent(v3d->drm.dev, 4096 * 1024, (void *)v3d->pt,
1059 v3d->pt_paddr);
1060 }
1061
1062 return 0;
1063 }
1064
1065 void
v3d_gem_destroy(struct drm_device * dev)1066 v3d_gem_destroy(struct drm_device *dev)
1067 {
1068 struct v3d_dev *v3d = to_v3d_dev(dev);
1069
1070 v3d_sched_fini(v3d);
1071
1072 /* Waiting for jobs to finish would need to be done before
1073 * unregistering V3D.
1074 */
1075 WARN_ON(v3d->bin_job);
1076 WARN_ON(v3d->render_job);
1077
1078 drm_mm_takedown(&v3d->mm);
1079
1080 dma_free_coherent(v3d->drm.dev, 4096 * 1024, (void *)v3d->pt,
1081 v3d->pt_paddr);
1082 }
1083