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