1 /*
2 * Copyright © 2014 Broadcom
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/device.h>
28 #include <linux/io.h>
29 #include <linux/sched/signal.h>
30 #include <linux/dma-fence-array.h>
31
32 #include <drm/drm_exec.h>
33 #include <drm/drm_print.h>
34 #include <drm/drm_syncobj.h>
35
36 #include "uapi/drm/vc4_drm.h"
37 #include "vc4_drv.h"
38 #include "vc4_regs.h"
39 #include "vc4_trace.h"
40
41 static void
vc4_queue_hangcheck(struct drm_device * dev)42 vc4_queue_hangcheck(struct drm_device *dev)
43 {
44 struct vc4_dev *vc4 = to_vc4_dev(dev);
45
46 mod_timer(&vc4->hangcheck.timer,
47 round_jiffies_up(jiffies + msecs_to_jiffies(100)));
48 }
49
50 struct vc4_hang_state {
51 struct drm_vc4_get_hang_state user_state;
52
53 u32 bo_count;
54 struct drm_gem_object **bo;
55 };
56
57 static void
vc4_free_hang_state(struct drm_device * dev,struct vc4_hang_state * state)58 vc4_free_hang_state(struct drm_device *dev, struct vc4_hang_state *state)
59 {
60 unsigned int i;
61
62 for (i = 0; i < state->user_state.bo_count; i++)
63 drm_gem_object_put(state->bo[i]);
64
65 kfree(state);
66 }
67
68 int
vc4_get_hang_state_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)69 vc4_get_hang_state_ioctl(struct drm_device *dev, void *data,
70 struct drm_file *file_priv)
71 {
72 struct drm_vc4_get_hang_state *get_state = data;
73 struct drm_vc4_get_hang_state_bo *bo_state;
74 struct vc4_hang_state *kernel_state;
75 struct drm_vc4_get_hang_state *state;
76 struct vc4_dev *vc4 = to_vc4_dev(dev);
77 unsigned long irqflags;
78 u32 i;
79 int ret = 0;
80
81 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
82 return -ENODEV;
83
84 if (!vc4->v3d) {
85 DRM_DEBUG("VC4_GET_HANG_STATE with no VC4 V3D probed\n");
86 return -ENODEV;
87 }
88
89 spin_lock_irqsave(&vc4->job_lock, irqflags);
90 kernel_state = vc4->hang_state;
91 if (!kernel_state) {
92 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
93 return -ENOENT;
94 }
95 state = &kernel_state->user_state;
96
97 /* If the user's array isn't big enough, just return the
98 * required array size.
99 */
100 if (get_state->bo_count < state->bo_count) {
101 get_state->bo_count = state->bo_count;
102 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
103 return 0;
104 }
105
106 vc4->hang_state = NULL;
107 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
108
109 /* Save the user's BO pointer, so we don't stomp it with the memcpy. */
110 state->bo = get_state->bo;
111 memcpy(get_state, state, sizeof(*state));
112
113 bo_state = kzalloc_objs(*bo_state, state->bo_count);
114 if (!bo_state) {
115 ret = -ENOMEM;
116 goto err_free;
117 }
118
119 for (i = 0; i < state->bo_count; i++) {
120 struct vc4_bo *vc4_bo = to_vc4_bo(kernel_state->bo[i]);
121 u32 handle;
122
123 ret = drm_gem_handle_create(file_priv, kernel_state->bo[i],
124 &handle);
125
126 if (ret) {
127 state->bo_count = i;
128 goto err_delete_handle;
129 }
130 bo_state[i].handle = handle;
131 bo_state[i].paddr = vc4_bo->base.dma_addr;
132 bo_state[i].size = vc4_bo->base.base.size;
133 }
134
135 if (copy_to_user(u64_to_user_ptr(get_state->bo),
136 bo_state,
137 state->bo_count * sizeof(*bo_state)))
138 ret = -EFAULT;
139
140 err_delete_handle:
141 if (ret) {
142 for (i = 0; i < state->bo_count; i++)
143 drm_gem_handle_delete(file_priv, bo_state[i].handle);
144 }
145
146 err_free:
147 vc4_free_hang_state(dev, kernel_state);
148 kfree(bo_state);
149
150 return ret;
151 }
152
153 static void
vc4_save_hang_state(struct drm_device * dev)154 vc4_save_hang_state(struct drm_device *dev)
155 {
156 struct vc4_dev *vc4 = to_vc4_dev(dev);
157 struct drm_vc4_get_hang_state *state;
158 struct vc4_hang_state *kernel_state;
159 struct vc4_exec_info *exec[2];
160 struct vc4_bo *bo;
161 unsigned long irqflags;
162 unsigned int i, j, k, unref_list_count;
163
164 kernel_state = kzalloc_objs(*kernel_state, 1);
165 if (!kernel_state)
166 return;
167
168 state = &kernel_state->user_state;
169
170 spin_lock_irqsave(&vc4->job_lock, irqflags);
171 exec[0] = vc4_first_bin_job(vc4);
172 exec[1] = vc4_first_render_job(vc4);
173 if (!exec[0] && !exec[1]) {
174 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
175 return;
176 }
177
178 /* Get the bos from both binner and renderer into hang state. */
179 state->bo_count = 0;
180 for (i = 0; i < 2; i++) {
181 if (!exec[i])
182 continue;
183
184 unref_list_count = 0;
185 list_for_each_entry(bo, &exec[i]->unref_list, unref_head)
186 unref_list_count++;
187 state->bo_count += exec[i]->bo_count + unref_list_count;
188 }
189
190 kernel_state->bo = kzalloc_objs(*kernel_state->bo, state->bo_count,
191 GFP_ATOMIC);
192
193 if (!kernel_state->bo) {
194 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
195 return;
196 }
197
198 k = 0;
199 for (i = 0; i < 2; i++) {
200 if (!exec[i])
201 continue;
202
203 for (j = 0; j < exec[i]->bo_count; j++) {
204 bo = to_vc4_bo(exec[i]->bo[j]);
205
206 /* Retain BOs just in case they were marked purgeable.
207 * This prevents the BO from being purged before
208 * someone had a chance to dump the hang state.
209 */
210 WARN_ON(!refcount_read(&bo->usecnt));
211 refcount_inc(&bo->usecnt);
212 drm_gem_object_get(exec[i]->bo[j]);
213 kernel_state->bo[k++] = exec[i]->bo[j];
214 }
215
216 list_for_each_entry(bo, &exec[i]->unref_list, unref_head) {
217 /* No need to retain BOs coming from the ->unref_list
218 * because they are naturally unpurgeable.
219 */
220 drm_gem_object_get(&bo->base.base);
221 kernel_state->bo[k++] = &bo->base.base;
222 }
223 }
224
225 WARN_ON_ONCE(k != state->bo_count);
226
227 if (exec[0])
228 state->start_bin = exec[0]->ct0ca;
229 if (exec[1])
230 state->start_render = exec[1]->ct1ca;
231
232 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
233
234 state->ct0ca = V3D_READ(V3D_CTNCA(0));
235 state->ct0ea = V3D_READ(V3D_CTNEA(0));
236
237 state->ct1ca = V3D_READ(V3D_CTNCA(1));
238 state->ct1ea = V3D_READ(V3D_CTNEA(1));
239
240 state->ct0cs = V3D_READ(V3D_CTNCS(0));
241 state->ct1cs = V3D_READ(V3D_CTNCS(1));
242
243 state->ct0ra0 = V3D_READ(V3D_CT00RA0);
244 state->ct1ra0 = V3D_READ(V3D_CT01RA0);
245
246 state->bpca = V3D_READ(V3D_BPCA);
247 state->bpcs = V3D_READ(V3D_BPCS);
248 state->bpoa = V3D_READ(V3D_BPOA);
249 state->bpos = V3D_READ(V3D_BPOS);
250
251 state->vpmbase = V3D_READ(V3D_VPMBASE);
252
253 state->dbge = V3D_READ(V3D_DBGE);
254 state->fdbgo = V3D_READ(V3D_FDBGO);
255 state->fdbgb = V3D_READ(V3D_FDBGB);
256 state->fdbgr = V3D_READ(V3D_FDBGR);
257 state->fdbgs = V3D_READ(V3D_FDBGS);
258 state->errstat = V3D_READ(V3D_ERRSTAT);
259
260 /* We need to turn purgeable BOs into unpurgeable ones so that
261 * userspace has a chance to dump the hang state before the kernel
262 * decides to purge those BOs.
263 * Note that BO consistency at dump time cannot be guaranteed. For
264 * example, if the owner of these BOs decides to re-use them or mark
265 * them purgeable again there's nothing we can do to prevent it.
266 */
267 for (i = 0; i < kernel_state->user_state.bo_count; i++) {
268 struct vc4_bo *bo = to_vc4_bo(kernel_state->bo[i]);
269
270 if (bo->madv == __VC4_MADV_NOTSUPP)
271 continue;
272
273 mutex_lock(&bo->madv_lock);
274 if (!WARN_ON(bo->madv == __VC4_MADV_PURGED))
275 bo->madv = VC4_MADV_WILLNEED;
276 refcount_dec(&bo->usecnt);
277 mutex_unlock(&bo->madv_lock);
278 }
279
280 spin_lock_irqsave(&vc4->job_lock, irqflags);
281 if (vc4->hang_state) {
282 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
283 vc4_free_hang_state(dev, kernel_state);
284 } else {
285 vc4->hang_state = kernel_state;
286 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
287 }
288 }
289
290 static void
vc4_reset(struct drm_device * dev)291 vc4_reset(struct drm_device *dev)
292 {
293 struct vc4_dev *vc4 = to_vc4_dev(dev);
294
295 DRM_INFO("Resetting GPU.\n");
296
297 mutex_lock(&vc4->power_lock);
298 if (vc4->power_refcount) {
299 /* Power the device off and back on the by dropping the
300 * reference on runtime PM.
301 */
302 pm_runtime_put_sync_suspend(&vc4->v3d->pdev->dev);
303 pm_runtime_get_sync(&vc4->v3d->pdev->dev);
304 }
305 mutex_unlock(&vc4->power_lock);
306
307 vc4_irq_reset(dev);
308
309 /* Rearm the hangcheck -- another job might have been waiting
310 * for our hung one to get kicked off, and vc4_irq_reset()
311 * would have started it.
312 */
313 vc4_queue_hangcheck(dev);
314 }
315
316 static void
vc4_reset_work(struct work_struct * work)317 vc4_reset_work(struct work_struct *work)
318 {
319 struct vc4_dev *vc4 =
320 container_of(work, struct vc4_dev, hangcheck.reset_work);
321
322 vc4_save_hang_state(&vc4->base);
323
324 vc4_reset(&vc4->base);
325 }
326
327 static void
vc4_hangcheck_elapsed(struct timer_list * t)328 vc4_hangcheck_elapsed(struct timer_list *t)
329 {
330 struct vc4_dev *vc4 = timer_container_of(vc4, t, hangcheck.timer);
331 struct drm_device *dev = &vc4->base;
332 uint32_t ct0ca, ct1ca;
333 unsigned long irqflags;
334 struct vc4_exec_info *bin_exec, *render_exec;
335
336 spin_lock_irqsave(&vc4->job_lock, irqflags);
337
338 bin_exec = vc4_first_bin_job(vc4);
339 render_exec = vc4_first_render_job(vc4);
340
341 /* If idle, we can stop watching for hangs. */
342 if (!bin_exec && !render_exec) {
343 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
344 return;
345 }
346
347 ct0ca = V3D_READ(V3D_CTNCA(0));
348 ct1ca = V3D_READ(V3D_CTNCA(1));
349
350 /* If we've made any progress in execution, rearm the timer
351 * and wait.
352 */
353 if ((bin_exec && ct0ca != bin_exec->last_ct0ca) ||
354 (render_exec && ct1ca != render_exec->last_ct1ca)) {
355 if (bin_exec)
356 bin_exec->last_ct0ca = ct0ca;
357 if (render_exec)
358 render_exec->last_ct1ca = ct1ca;
359 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
360 vc4_queue_hangcheck(dev);
361 return;
362 }
363
364 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
365
366 /* We've gone too long with no progress, reset. This has to
367 * be done from a work struct, since resetting can sleep and
368 * this timer hook isn't allowed to.
369 */
370 schedule_work(&vc4->hangcheck.reset_work);
371 }
372
373 static void
submit_cl(struct drm_device * dev,uint32_t thread,uint32_t start,uint32_t end)374 submit_cl(struct drm_device *dev, uint32_t thread, uint32_t start, uint32_t end)
375 {
376 struct vc4_dev *vc4 = to_vc4_dev(dev);
377
378 /* Set the current and end address of the control list.
379 * Writing the end register is what starts the job.
380 */
381 V3D_WRITE(V3D_CTNCA(thread), start);
382 V3D_WRITE(V3D_CTNEA(thread), end);
383 }
384
385 int
vc4_wait_for_seqno(struct drm_device * dev,uint64_t seqno,uint64_t timeout_ns,bool interruptible)386 vc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno, uint64_t timeout_ns,
387 bool interruptible)
388 {
389 struct vc4_dev *vc4 = to_vc4_dev(dev);
390 int ret = 0;
391 unsigned long timeout_expire;
392 DEFINE_WAIT(wait);
393
394 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
395 return -ENODEV;
396
397 if (vc4->finished_seqno >= seqno)
398 return 0;
399
400 if (timeout_ns == 0)
401 return -ETIME;
402
403 timeout_expire = jiffies + nsecs_to_jiffies(timeout_ns);
404
405 trace_vc4_wait_for_seqno_begin(dev, seqno, timeout_ns);
406 for (;;) {
407 prepare_to_wait(&vc4->job_wait_queue, &wait,
408 interruptible ? TASK_INTERRUPTIBLE :
409 TASK_UNINTERRUPTIBLE);
410
411 if (interruptible && signal_pending(current)) {
412 ret = -ERESTARTSYS;
413 break;
414 }
415
416 if (vc4->finished_seqno >= seqno)
417 break;
418
419 if (timeout_ns != ~0ull) {
420 if (time_after_eq(jiffies, timeout_expire)) {
421 ret = -ETIME;
422 break;
423 }
424 schedule_timeout(timeout_expire - jiffies);
425 } else {
426 schedule();
427 }
428 }
429
430 finish_wait(&vc4->job_wait_queue, &wait);
431 trace_vc4_wait_for_seqno_end(dev, seqno);
432
433 return ret;
434 }
435
436 static void
vc4_flush_caches(struct drm_device * dev)437 vc4_flush_caches(struct drm_device *dev)
438 {
439 struct vc4_dev *vc4 = to_vc4_dev(dev);
440
441 /* Flush the GPU L2 caches. These caches sit on top of system
442 * L3 (the 128kb or so shared with the CPU), and are
443 * non-allocating in the L3.
444 */
445 V3D_WRITE(V3D_L2CACTL,
446 V3D_L2CACTL_L2CCLR);
447
448 V3D_WRITE(V3D_SLCACTL,
449 VC4_SET_FIELD(0xf, V3D_SLCACTL_T1CC) |
450 VC4_SET_FIELD(0xf, V3D_SLCACTL_T0CC) |
451 VC4_SET_FIELD(0xf, V3D_SLCACTL_UCC) |
452 VC4_SET_FIELD(0xf, V3D_SLCACTL_ICC));
453 }
454
455 static void
vc4_flush_texture_caches(struct drm_device * dev)456 vc4_flush_texture_caches(struct drm_device *dev)
457 {
458 struct vc4_dev *vc4 = to_vc4_dev(dev);
459
460 V3D_WRITE(V3D_L2CACTL,
461 V3D_L2CACTL_L2CCLR);
462
463 V3D_WRITE(V3D_SLCACTL,
464 VC4_SET_FIELD(0xf, V3D_SLCACTL_T1CC) |
465 VC4_SET_FIELD(0xf, V3D_SLCACTL_T0CC));
466 }
467
468 /* Sets the registers for the next job to be actually be executed in
469 * the hardware.
470 *
471 * The job_lock should be held during this.
472 */
473 void
vc4_submit_next_bin_job(struct drm_device * dev)474 vc4_submit_next_bin_job(struct drm_device *dev)
475 {
476 struct vc4_dev *vc4 = to_vc4_dev(dev);
477 struct vc4_exec_info *exec;
478
479 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
480 return;
481
482 again:
483 exec = vc4_first_bin_job(vc4);
484 if (!exec)
485 return;
486
487 vc4_flush_caches(dev);
488
489 /* Only start the perfmon if it was not already started by a previous
490 * job.
491 */
492 if (exec->perfmon && vc4->active_perfmon != exec->perfmon)
493 vc4_perfmon_start(vc4, exec->perfmon);
494
495 /* Either put the job in the binner if it uses the binner, or
496 * immediately move it to the to-be-rendered queue.
497 */
498 if (exec->ct0ca != exec->ct0ea) {
499 trace_vc4_submit_cl(dev, false, exec->seqno, exec->ct0ca,
500 exec->ct0ea);
501 submit_cl(dev, 0, exec->ct0ca, exec->ct0ea);
502 } else {
503 struct vc4_exec_info *next;
504
505 vc4_move_job_to_render(dev, exec);
506 next = vc4_first_bin_job(vc4);
507
508 /* We can't start the next bin job if the previous job had a
509 * different perfmon instance attached to it. The same goes
510 * if one of them had a perfmon attached to it and the other
511 * one doesn't.
512 */
513 if (next && next->perfmon == exec->perfmon)
514 goto again;
515 }
516 }
517
518 void
vc4_submit_next_render_job(struct drm_device * dev)519 vc4_submit_next_render_job(struct drm_device *dev)
520 {
521 struct vc4_dev *vc4 = to_vc4_dev(dev);
522 struct vc4_exec_info *exec = vc4_first_render_job(vc4);
523
524 if (!exec)
525 return;
526
527 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
528 return;
529
530 /* A previous RCL may have written to one of our textures, and
531 * our full cache flush at bin time may have occurred before
532 * that RCL completed. Flush the texture cache now, but not
533 * the instructions or uniforms (since we don't write those
534 * from an RCL).
535 */
536 vc4_flush_texture_caches(dev);
537
538 trace_vc4_submit_cl(dev, true, exec->seqno, exec->ct1ca, exec->ct1ea);
539 submit_cl(dev, 1, exec->ct1ca, exec->ct1ea);
540 }
541
542 void
vc4_move_job_to_render(struct drm_device * dev,struct vc4_exec_info * exec)543 vc4_move_job_to_render(struct drm_device *dev, struct vc4_exec_info *exec)
544 {
545 struct vc4_dev *vc4 = to_vc4_dev(dev);
546 bool was_empty = list_empty(&vc4->render_job_list);
547
548 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
549 return;
550
551 list_move_tail(&exec->head, &vc4->render_job_list);
552 if (was_empty)
553 vc4_submit_next_render_job(dev);
554 }
555
556 static void
vc4_attach_fences(struct vc4_exec_info * exec)557 vc4_attach_fences(struct vc4_exec_info *exec)
558 {
559 struct vc4_bo *bo;
560 unsigned i;
561
562 for (i = 0; i < exec->bo_count; i++) {
563 bo = to_vc4_bo(exec->bo[i]);
564 dma_resv_add_fence(bo->base.base.resv, exec->fence,
565 DMA_RESV_USAGE_READ);
566 }
567
568 for (i = 0; i < exec->rcl_write_bo_count; i++) {
569 bo = to_vc4_bo(&exec->rcl_write_bo[i]->base);
570 dma_resv_add_fence(bo->base.base.resv, exec->fence,
571 DMA_RESV_USAGE_WRITE);
572 }
573 }
574
575 /* Takes the reservation lock on all the BOs being referenced, so that
576 * at queue submit time we can update the reservations.
577 *
578 * We don't lock the RCL the tile alloc/state BOs, or overflow memory
579 * (all of which are on exec->unref_list). They're entirely private
580 * to vc4, so we don't attach dma-buf fences to them.
581 */
582 static int
vc4_lock_bo_reservations(struct vc4_exec_info * exec,struct drm_exec * exec_ctx)583 vc4_lock_bo_reservations(struct vc4_exec_info *exec,
584 struct drm_exec *exec_ctx)
585 {
586 int ret;
587
588 /* Reserve space for our shared (read-only) fence references,
589 * before we commit the CL to the hardware.
590 */
591 drm_exec_init(exec_ctx, DRM_EXEC_INTERRUPTIBLE_WAIT, exec->bo_count);
592 drm_exec_until_all_locked(exec_ctx) {
593 ret = drm_exec_prepare_array(exec_ctx, exec->bo,
594 exec->bo_count, 1);
595 }
596
597 if (ret) {
598 drm_exec_fini(exec_ctx);
599 return ret;
600 }
601
602 return 0;
603 }
604
605 /* Queues a struct vc4_exec_info for execution. If no job is
606 * currently executing, then submits it.
607 *
608 * Unlike most GPUs, our hardware only handles one command list at a
609 * time. To queue multiple jobs at once, we'd need to edit the
610 * previous command list to have a jump to the new one at the end, and
611 * then bump the end address. That's a change for a later date,
612 * though.
613 */
614 static int
vc4_queue_submit(struct drm_device * dev,struct vc4_exec_info * exec,struct drm_exec * exec_ctx,struct drm_syncobj * out_sync)615 vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
616 struct drm_exec *exec_ctx,
617 struct drm_syncobj *out_sync)
618 {
619 struct vc4_dev *vc4 = to_vc4_dev(dev);
620 struct vc4_exec_info *renderjob;
621 uint64_t seqno;
622 unsigned long irqflags;
623 struct vc4_fence *fence;
624
625 fence = kzalloc_obj(*fence);
626 if (!fence)
627 return -ENOMEM;
628 fence->dev = dev;
629
630 spin_lock_irqsave(&vc4->job_lock, irqflags);
631
632 seqno = ++vc4->emit_seqno;
633 exec->seqno = seqno;
634
635 dma_fence_init(&fence->base, &vc4_fence_ops, &vc4->job_lock,
636 vc4->dma_fence_context, exec->seqno);
637 fence->seqno = exec->seqno;
638 exec->fence = &fence->base;
639
640 if (out_sync)
641 drm_syncobj_replace_fence(out_sync, exec->fence);
642
643 vc4_attach_fences(exec);
644
645 drm_exec_fini(exec_ctx);
646
647 list_add_tail(&exec->head, &vc4->bin_job_list);
648
649 /* If no bin job was executing and if the render job (if any) has the
650 * same perfmon as our job attached to it (or if both jobs don't have
651 * perfmon activated), then kick ours off. Otherwise, it'll get
652 * started when the previous job's flush/render done interrupt occurs.
653 */
654 renderjob = vc4_first_render_job(vc4);
655 if (vc4_first_bin_job(vc4) == exec &&
656 (!renderjob || renderjob->perfmon == exec->perfmon)) {
657 vc4_submit_next_bin_job(dev);
658 vc4_queue_hangcheck(dev);
659 }
660
661 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
662
663 return 0;
664 }
665
666 /**
667 * vc4_cl_lookup_bos() - Sets up exec->bo[] with the GEM objects
668 * referenced by the job.
669 * @dev: DRM device
670 * @file_priv: DRM file for this fd
671 * @exec: V3D job being set up
672 *
673 * The command validator needs to reference BOs by their index within
674 * the submitted job's BO list. This does the validation of the job's
675 * BO list and reference counting for the lifetime of the job.
676 */
677 static int
vc4_cl_lookup_bos(struct drm_device * dev,struct drm_file * file_priv,struct vc4_exec_info * exec)678 vc4_cl_lookup_bos(struct drm_device *dev,
679 struct drm_file *file_priv,
680 struct vc4_exec_info *exec)
681 {
682 struct drm_vc4_submit_cl *args = exec->args;
683 int ret = 0;
684 int i;
685
686 exec->bo_count = args->bo_handle_count;
687
688 if (!exec->bo_count) {
689 /* See comment on bo_index for why we have to check
690 * this.
691 */
692 DRM_DEBUG("Rendering requires BOs to validate\n");
693 return -EINVAL;
694 }
695
696 ret = drm_gem_objects_lookup(file_priv, u64_to_user_ptr(args->bo_handles),
697 exec->bo_count, &exec->bo);
698
699 if (ret)
700 goto fail_put_bo;
701
702 for (i = 0; i < exec->bo_count; i++) {
703 ret = vc4_bo_inc_usecnt(to_vc4_bo(exec->bo[i]));
704 if (ret)
705 goto fail_dec_usecnt;
706 }
707
708 return 0;
709
710 fail_dec_usecnt:
711 /* Decrease usecnt on acquired objects.
712 * We cannot rely on vc4_complete_exec() to release resources here,
713 * because vc4_complete_exec() has no information about which BO has
714 * had its ->usecnt incremented.
715 * To make things easier we just free everything explicitly and set
716 * exec->bo to NULL so that vc4_complete_exec() skips the 'BO release'
717 * step.
718 */
719 for (i-- ; i >= 0; i--)
720 vc4_bo_dec_usecnt(to_vc4_bo(exec->bo[i]));
721
722 fail_put_bo:
723 /* Release any reference to acquired objects. */
724 for (i = 0; i < exec->bo_count && exec->bo[i]; i++)
725 drm_gem_object_put(exec->bo[i]);
726
727 kvfree(exec->bo);
728 exec->bo = NULL;
729 return ret;
730 }
731
732 static int
vc4_get_bcl(struct drm_device * dev,struct vc4_exec_info * exec)733 vc4_get_bcl(struct drm_device *dev, struct vc4_exec_info *exec)
734 {
735 struct drm_vc4_submit_cl *args = exec->args;
736 struct vc4_dev *vc4 = to_vc4_dev(dev);
737 void *temp = NULL;
738 void *bin;
739 int ret = 0;
740 uint32_t bin_offset = 0;
741 uint32_t shader_rec_offset = roundup(bin_offset + args->bin_cl_size,
742 16);
743 uint32_t uniforms_offset = shader_rec_offset + args->shader_rec_size;
744 uint32_t exec_size = uniforms_offset + args->uniforms_size;
745 uint32_t temp_size = exec_size + (sizeof(struct vc4_shader_state) *
746 args->shader_rec_count);
747 struct vc4_bo *bo;
748
749 if (shader_rec_offset < args->bin_cl_size ||
750 uniforms_offset < shader_rec_offset ||
751 exec_size < uniforms_offset ||
752 args->shader_rec_count >= (UINT_MAX /
753 sizeof(struct vc4_shader_state)) ||
754 temp_size < exec_size) {
755 DRM_DEBUG("overflow in exec arguments\n");
756 ret = -EINVAL;
757 goto fail;
758 }
759
760 /* Allocate space where we'll store the copied in user command lists
761 * and shader records.
762 *
763 * We don't just copy directly into the BOs because we need to
764 * read the contents back for validation, and I think the
765 * bo->vaddr is uncached access.
766 */
767 temp = kvmalloc_array(temp_size, 1, GFP_KERNEL);
768 if (!temp) {
769 drm_err(dev, "Failed to allocate storage for copying "
770 "in bin/render CLs.\n");
771 ret = -ENOMEM;
772 goto fail;
773 }
774 bin = temp + bin_offset;
775 exec->shader_rec_u = temp + shader_rec_offset;
776 exec->uniforms_u = temp + uniforms_offset;
777 exec->shader_state = temp + exec_size;
778 exec->shader_state_size = args->shader_rec_count;
779
780 if (copy_from_user(bin,
781 u64_to_user_ptr(args->bin_cl),
782 args->bin_cl_size)) {
783 ret = -EFAULT;
784 goto fail;
785 }
786
787 if (copy_from_user(exec->shader_rec_u,
788 u64_to_user_ptr(args->shader_rec),
789 args->shader_rec_size)) {
790 ret = -EFAULT;
791 goto fail;
792 }
793
794 if (copy_from_user(exec->uniforms_u,
795 u64_to_user_ptr(args->uniforms),
796 args->uniforms_size)) {
797 ret = -EFAULT;
798 goto fail;
799 }
800
801 bo = vc4_bo_create(dev, exec_size, true, VC4_BO_TYPE_BCL);
802 if (IS_ERR(bo)) {
803 drm_err(dev, "Couldn't allocate BO for binning\n");
804 ret = PTR_ERR(bo);
805 goto fail;
806 }
807 exec->exec_bo = &bo->base;
808
809 list_add_tail(&to_vc4_bo(&exec->exec_bo->base)->unref_head,
810 &exec->unref_list);
811
812 exec->ct0ca = exec->exec_bo->dma_addr + bin_offset;
813
814 exec->bin_u = bin;
815
816 exec->shader_rec_v = exec->exec_bo->vaddr + shader_rec_offset;
817 exec->shader_rec_p = exec->exec_bo->dma_addr + shader_rec_offset;
818 exec->shader_rec_size = args->shader_rec_size;
819
820 exec->uniforms_v = exec->exec_bo->vaddr + uniforms_offset;
821 exec->uniforms_p = exec->exec_bo->dma_addr + uniforms_offset;
822 exec->uniforms_size = args->uniforms_size;
823
824 ret = vc4_validate_bin_cl(dev,
825 exec->exec_bo->vaddr + bin_offset,
826 bin,
827 exec);
828 if (ret)
829 goto fail;
830
831 ret = vc4_validate_shader_recs(dev, exec);
832 if (ret)
833 goto fail;
834
835 if (exec->found_tile_binning_mode_config_packet) {
836 ret = vc4_v3d_bin_bo_get(vc4, &exec->bin_bo_used);
837 if (ret)
838 goto fail;
839 }
840
841 fail:
842 kvfree(temp);
843 return ret;
844 }
845
846 static void
vc4_complete_exec(struct drm_device * dev,struct vc4_exec_info * exec)847 vc4_complete_exec(struct drm_device *dev, struct vc4_exec_info *exec)
848 {
849 struct vc4_dev *vc4 = to_vc4_dev(dev);
850 unsigned long irqflags;
851 unsigned i;
852
853 /* If we got force-completed because of GPU reset rather than
854 * through our IRQ handler, signal the fence now.
855 */
856 if (exec->fence) {
857 dma_fence_signal(exec->fence);
858 dma_fence_put(exec->fence);
859 }
860
861 if (exec->bo) {
862 for (i = 0; i < exec->bo_count; i++) {
863 struct vc4_bo *bo = to_vc4_bo(exec->bo[i]);
864
865 vc4_bo_dec_usecnt(bo);
866 drm_gem_object_put(exec->bo[i]);
867 }
868 kvfree(exec->bo);
869 }
870
871 while (!list_empty(&exec->unref_list)) {
872 struct vc4_bo *bo = list_first_entry(&exec->unref_list,
873 struct vc4_bo, unref_head);
874 list_del(&bo->unref_head);
875 drm_gem_object_put(&bo->base.base);
876 }
877
878 /* Free up the allocation of any bin slots we used. */
879 spin_lock_irqsave(&vc4->job_lock, irqflags);
880 vc4->bin_alloc_used &= ~exec->bin_slots;
881 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
882
883 /* Release the reference on the binner BO if needed. */
884 if (exec->bin_bo_used)
885 vc4_v3d_bin_bo_put(vc4);
886
887 /* Release the reference we had on the perf monitor. */
888 vc4_perfmon_put(exec->perfmon);
889
890 vc4_v3d_pm_put(vc4);
891
892 kfree(exec);
893 }
894
895 void
vc4_job_handle_completed(struct vc4_dev * vc4)896 vc4_job_handle_completed(struct vc4_dev *vc4)
897 {
898 unsigned long irqflags;
899
900 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
901 return;
902
903 spin_lock_irqsave(&vc4->job_lock, irqflags);
904 while (!list_empty(&vc4->job_done_list)) {
905 struct vc4_exec_info *exec =
906 list_first_entry(&vc4->job_done_list,
907 struct vc4_exec_info, head);
908 list_del(&exec->head);
909
910 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
911 vc4_complete_exec(&vc4->base, exec);
912 spin_lock_irqsave(&vc4->job_lock, irqflags);
913 }
914
915 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
916 }
917
918 /* Scheduled when any job has been completed, this walks the list of
919 * jobs that had completed and unrefs their BOs and frees their exec
920 * structs.
921 */
922 static void
vc4_job_done_work(struct work_struct * work)923 vc4_job_done_work(struct work_struct *work)
924 {
925 struct vc4_dev *vc4 =
926 container_of(work, struct vc4_dev, job_done_work);
927
928 vc4_job_handle_completed(vc4);
929 }
930
931 static int
vc4_wait_for_seqno_ioctl_helper(struct drm_device * dev,uint64_t seqno,uint64_t * timeout_ns)932 vc4_wait_for_seqno_ioctl_helper(struct drm_device *dev,
933 uint64_t seqno,
934 uint64_t *timeout_ns)
935 {
936 unsigned long start = jiffies;
937 int ret = vc4_wait_for_seqno(dev, seqno, *timeout_ns, true);
938
939 if ((ret == -EINTR || ret == -ERESTARTSYS) && *timeout_ns != ~0ull) {
940 uint64_t delta = jiffies_to_nsecs(jiffies - start);
941
942 if (*timeout_ns >= delta)
943 *timeout_ns -= delta;
944 }
945
946 return ret;
947 }
948
949 int
vc4_wait_seqno_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)950 vc4_wait_seqno_ioctl(struct drm_device *dev, void *data,
951 struct drm_file *file_priv)
952 {
953 struct vc4_dev *vc4 = to_vc4_dev(dev);
954 struct drm_vc4_wait_seqno *args = data;
955
956 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
957 return -ENODEV;
958
959 return vc4_wait_for_seqno_ioctl_helper(dev, args->seqno,
960 &args->timeout_ns);
961 }
962
963 int
vc4_wait_bo_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)964 vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
965 struct drm_file *file_priv)
966 {
967 struct vc4_dev *vc4 = to_vc4_dev(dev);
968 int ret;
969 struct drm_vc4_wait_bo *args = data;
970 unsigned long timeout_jiffies =
971 usecs_to_jiffies(div_u64(args->timeout_ns, 1000));
972 ktime_t start = ktime_get();
973 u64 delta_ns;
974
975 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
976 return -ENODEV;
977
978 if (args->pad != 0)
979 return -EINVAL;
980
981 ret = drm_gem_dma_resv_wait(file_priv, args->handle,
982 true, timeout_jiffies);
983
984 /* Decrement the user's timeout, in case we got interrupted
985 * such that the ioctl will be restarted.
986 */
987 delta_ns = ktime_to_ns(ktime_sub(ktime_get(), start));
988 if (delta_ns < args->timeout_ns)
989 args->timeout_ns -= delta_ns;
990 else
991 args->timeout_ns = 0;
992
993 return ret;
994 }
995
996 /**
997 * vc4_submit_cl_ioctl() - Submits a job (frame) to the VC4.
998 * @dev: DRM device
999 * @data: ioctl argument
1000 * @file_priv: DRM file for this fd
1001 *
1002 * This is the main entrypoint for userspace to submit a 3D frame to
1003 * the GPU. Userspace provides the binner command list (if
1004 * applicable), and the kernel sets up the render command list to draw
1005 * to the framebuffer described in the ioctl, using the command lists
1006 * that the 3D engine's binner will produce.
1007 */
1008 int
vc4_submit_cl_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1009 vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
1010 struct drm_file *file_priv)
1011 {
1012 struct vc4_dev *vc4 = to_vc4_dev(dev);
1013 struct vc4_file *vc4file = file_priv->driver_priv;
1014 struct drm_vc4_submit_cl *args = data;
1015 struct drm_syncobj *out_sync = NULL;
1016 struct vc4_exec_info *exec;
1017 struct drm_exec exec_ctx;
1018 struct dma_fence *in_fence;
1019 int ret = 0;
1020
1021 trace_vc4_submit_cl_ioctl(dev, args->bin_cl_size,
1022 args->shader_rec_size,
1023 args->bo_handle_count);
1024
1025 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
1026 return -ENODEV;
1027
1028 if (!vc4->v3d) {
1029 DRM_DEBUG("VC4_SUBMIT_CL with no VC4 V3D probed\n");
1030 return -ENODEV;
1031 }
1032
1033 if ((args->flags & ~(VC4_SUBMIT_CL_USE_CLEAR_COLOR |
1034 VC4_SUBMIT_CL_FIXED_RCL_ORDER |
1035 VC4_SUBMIT_CL_RCL_ORDER_INCREASING_X |
1036 VC4_SUBMIT_CL_RCL_ORDER_INCREASING_Y)) != 0) {
1037 DRM_DEBUG("Unknown flags: 0x%02x\n", args->flags);
1038 return -EINVAL;
1039 }
1040
1041 if (args->pad2 != 0) {
1042 DRM_DEBUG("Invalid pad: 0x%08x\n", args->pad2);
1043 return -EINVAL;
1044 }
1045
1046 exec = kzalloc_objs(*exec, 1);
1047 if (!exec)
1048 return -ENOMEM;
1049
1050 exec->dev = vc4;
1051
1052 ret = vc4_v3d_pm_get(vc4);
1053 if (ret) {
1054 kfree(exec);
1055 return ret;
1056 }
1057
1058 exec->args = args;
1059 INIT_LIST_HEAD(&exec->unref_list);
1060
1061 ret = vc4_cl_lookup_bos(dev, file_priv, exec);
1062 if (ret)
1063 goto fail;
1064
1065 if (args->perfmonid) {
1066 exec->perfmon = vc4_perfmon_find(vc4file,
1067 args->perfmonid);
1068 if (!exec->perfmon) {
1069 ret = -ENOENT;
1070 goto fail;
1071 }
1072 }
1073
1074 if (args->in_sync) {
1075 ret = drm_syncobj_find_fence(file_priv, args->in_sync,
1076 0, 0, &in_fence);
1077 if (ret)
1078 goto fail;
1079
1080 /* When the fence (or fence array) is exclusively from our
1081 * context we can skip the wait since jobs are executed in
1082 * order of their submission through this ioctl and this can
1083 * only have fences from a prior job.
1084 */
1085 if (!dma_fence_match_context(in_fence,
1086 vc4->dma_fence_context)) {
1087 ret = dma_fence_wait(in_fence, true);
1088 if (ret) {
1089 dma_fence_put(in_fence);
1090 goto fail;
1091 }
1092 }
1093
1094 dma_fence_put(in_fence);
1095 }
1096
1097 if (exec->args->bin_cl_size != 0) {
1098 ret = vc4_get_bcl(dev, exec);
1099 if (ret)
1100 goto fail;
1101 } else {
1102 exec->ct0ca = 0;
1103 exec->ct0ea = 0;
1104 }
1105
1106 ret = vc4_get_rcl(dev, exec);
1107 if (ret)
1108 goto fail;
1109
1110 ret = vc4_lock_bo_reservations(exec, &exec_ctx);
1111 if (ret)
1112 goto fail;
1113
1114 if (args->out_sync) {
1115 out_sync = drm_syncobj_find(file_priv, args->out_sync);
1116 if (!out_sync) {
1117 ret = -EINVAL;
1118 goto fail_unreserve;
1119 }
1120
1121 /* We replace the fence in out_sync in vc4_queue_submit since
1122 * the render job could execute immediately after that call.
1123 * If it finishes before our ioctl processing resumes the
1124 * render job fence could already have been freed.
1125 */
1126 }
1127
1128 /* Clear this out of the struct we'll be putting in the queue,
1129 * since it's part of our stack.
1130 */
1131 exec->args = NULL;
1132
1133 ret = vc4_queue_submit(dev, exec, &exec_ctx, out_sync);
1134
1135 /* The syncobj isn't part of the exec data and we need to free our
1136 * reference even if job submission failed.
1137 */
1138 if (out_sync)
1139 drm_syncobj_put(out_sync);
1140
1141 if (ret)
1142 goto fail_unreserve;
1143
1144 /* Return the seqno for our job. */
1145 args->seqno = vc4->emit_seqno;
1146
1147 return 0;
1148
1149 fail_unreserve:
1150 drm_exec_fini(&exec_ctx);
1151 fail:
1152 vc4_complete_exec(&vc4->base, exec);
1153
1154 return ret;
1155 }
1156
1157 static void vc4_gem_destroy(struct drm_device *dev, void *unused);
vc4_gem_init(struct drm_device * dev)1158 int vc4_gem_init(struct drm_device *dev)
1159 {
1160 struct vc4_dev *vc4 = to_vc4_dev(dev);
1161 int ret;
1162
1163 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
1164 return -ENODEV;
1165
1166 vc4->dma_fence_context = dma_fence_context_alloc(1);
1167
1168 INIT_LIST_HEAD(&vc4->bin_job_list);
1169 INIT_LIST_HEAD(&vc4->render_job_list);
1170 INIT_LIST_HEAD(&vc4->job_done_list);
1171 spin_lock_init(&vc4->job_lock);
1172
1173 INIT_WORK(&vc4->hangcheck.reset_work, vc4_reset_work);
1174 timer_setup(&vc4->hangcheck.timer, vc4_hangcheck_elapsed, 0);
1175
1176 INIT_WORK(&vc4->job_done_work, vc4_job_done_work);
1177
1178 ret = drmm_mutex_init(dev, &vc4->power_lock);
1179 if (ret)
1180 return ret;
1181
1182 INIT_LIST_HEAD(&vc4->purgeable.list);
1183
1184 ret = drmm_mutex_init(dev, &vc4->purgeable.lock);
1185 if (ret)
1186 return ret;
1187
1188 return drmm_add_action_or_reset(dev, vc4_gem_destroy, NULL);
1189 }
1190
vc4_gem_destroy(struct drm_device * dev,void * unused)1191 static void vc4_gem_destroy(struct drm_device *dev, void *unused)
1192 {
1193 struct vc4_dev *vc4 = to_vc4_dev(dev);
1194
1195 /* Waiting for exec to finish would need to be done before
1196 * unregistering V3D.
1197 */
1198 WARN_ON(vc4->emit_seqno != vc4->finished_seqno);
1199
1200 /* V3D should already have disabled its interrupt and cleared
1201 * the overflow allocation registers. Now free the object.
1202 */
1203 if (vc4->bin_bo) {
1204 drm_gem_object_put(&vc4->bin_bo->base.base);
1205 vc4->bin_bo = NULL;
1206 }
1207
1208 if (vc4->hang_state)
1209 vc4_free_hang_state(dev, vc4->hang_state);
1210 }
1211
vc4_gem_madvise_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1212 int vc4_gem_madvise_ioctl(struct drm_device *dev, void *data,
1213 struct drm_file *file_priv)
1214 {
1215 struct vc4_dev *vc4 = to_vc4_dev(dev);
1216 struct drm_vc4_gem_madvise *args = data;
1217 struct drm_gem_object *gem_obj;
1218 struct vc4_bo *bo;
1219 int ret;
1220
1221 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
1222 return -ENODEV;
1223
1224 switch (args->madv) {
1225 case VC4_MADV_DONTNEED:
1226 case VC4_MADV_WILLNEED:
1227 break;
1228 default:
1229 return -EINVAL;
1230 }
1231
1232 if (args->pad != 0)
1233 return -EINVAL;
1234
1235 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
1236 if (!gem_obj) {
1237 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
1238 return -ENOENT;
1239 }
1240
1241 bo = to_vc4_bo(gem_obj);
1242
1243 /* Only BOs exposed to userspace can be purged. */
1244 if (bo->madv == __VC4_MADV_NOTSUPP) {
1245 DRM_DEBUG("madvise not supported on this BO\n");
1246 ret = -EINVAL;
1247 goto out_put_gem;
1248 }
1249
1250 /* Not sure it's safe to purge imported BOs. Let's just assume it's
1251 * not until proven otherwise.
1252 */
1253 if (gem_obj->import_attach) {
1254 DRM_DEBUG("madvise not supported on imported BOs\n");
1255 ret = -EINVAL;
1256 goto out_put_gem;
1257 }
1258
1259 mutex_lock(&bo->madv_lock);
1260
1261 if (args->madv == VC4_MADV_DONTNEED && bo->madv == VC4_MADV_WILLNEED &&
1262 !refcount_read(&bo->usecnt)) {
1263 /* If the BO is about to be marked as purgeable, is not used
1264 * and is not already purgeable or purged, add it to the
1265 * purgeable list.
1266 */
1267 vc4_bo_add_to_purgeable_pool(bo);
1268 } else if (args->madv == VC4_MADV_WILLNEED &&
1269 bo->madv == VC4_MADV_DONTNEED &&
1270 !refcount_read(&bo->usecnt)) {
1271 /* The BO has not been purged yet, just remove it from
1272 * the purgeable list.
1273 */
1274 vc4_bo_remove_from_purgeable_pool(bo);
1275 }
1276
1277 /* Save the purged state. */
1278 args->retained = bo->madv != __VC4_MADV_PURGED;
1279
1280 /* Update internal madv state only if the bo was not purged. */
1281 if (bo->madv != __VC4_MADV_PURGED)
1282 bo->madv = args->madv;
1283
1284 mutex_unlock(&bo->madv_lock);
1285
1286 ret = 0;
1287
1288 out_put_gem:
1289 drm_gem_object_put(gem_obj);
1290
1291 return ret;
1292 }
1293