blob: 3ac801b14d4eae0161d14efb2ff3a6c18785cf57 [file] [log] [blame]
Eric Anholtd5b1a782015-11-30 12:13:37 -08001/*
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>
Eric Anholt001bdb52016-02-05 17:41:49 -080026#include <linux/pm_runtime.h>
Eric Anholtd5b1a782015-11-30 12:13:37 -080027#include <linux/device.h>
28#include <linux/io.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010029#include <linux/sched/signal.h>
Eric Anholtd5b1a782015-11-30 12:13:37 -080030
31#include "uapi/drm/vc4_drm.h"
32#include "vc4_drv.h"
33#include "vc4_regs.h"
34#include "vc4_trace.h"
35
36static void
37vc4_queue_hangcheck(struct drm_device *dev)
38{
39 struct vc4_dev *vc4 = to_vc4_dev(dev);
40
41 mod_timer(&vc4->hangcheck.timer,
42 round_jiffies_up(jiffies + msecs_to_jiffies(100)));
43}
44
Eric Anholt21461362015-10-30 10:09:02 -070045struct vc4_hang_state {
46 struct drm_vc4_get_hang_state user_state;
47
48 u32 bo_count;
49 struct drm_gem_object **bo;
50};
51
52static void
53vc4_free_hang_state(struct drm_device *dev, struct vc4_hang_state *state)
54{
55 unsigned int i;
56
Eric Anholt21461362015-10-30 10:09:02 -070057 for (i = 0; i < state->user_state.bo_count; i++)
Cihangir Akturk1d5494e2017-08-03 14:58:40 +030058 drm_gem_object_put_unlocked(state->bo[i]);
Eric Anholt21461362015-10-30 10:09:02 -070059
60 kfree(state);
61}
62
63int
64vc4_get_hang_state_ioctl(struct drm_device *dev, void *data,
65 struct drm_file *file_priv)
66{
67 struct drm_vc4_get_hang_state *get_state = data;
68 struct drm_vc4_get_hang_state_bo *bo_state;
69 struct vc4_hang_state *kernel_state;
70 struct drm_vc4_get_hang_state *state;
71 struct vc4_dev *vc4 = to_vc4_dev(dev);
72 unsigned long irqflags;
73 u32 i;
Dan Carpenter65c47772015-12-17 15:36:28 +030074 int ret = 0;
Eric Anholt21461362015-10-30 10:09:02 -070075
76 spin_lock_irqsave(&vc4->job_lock, irqflags);
77 kernel_state = vc4->hang_state;
78 if (!kernel_state) {
79 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
80 return -ENOENT;
81 }
82 state = &kernel_state->user_state;
83
84 /* If the user's array isn't big enough, just return the
85 * required array size.
86 */
87 if (get_state->bo_count < state->bo_count) {
88 get_state->bo_count = state->bo_count;
89 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
90 return 0;
91 }
92
93 vc4->hang_state = NULL;
94 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
95
96 /* Save the user's BO pointer, so we don't stomp it with the memcpy. */
97 state->bo = get_state->bo;
98 memcpy(get_state, state, sizeof(*state));
99
100 bo_state = kcalloc(state->bo_count, sizeof(*bo_state), GFP_KERNEL);
101 if (!bo_state) {
102 ret = -ENOMEM;
103 goto err_free;
104 }
105
106 for (i = 0; i < state->bo_count; i++) {
107 struct vc4_bo *vc4_bo = to_vc4_bo(kernel_state->bo[i]);
108 u32 handle;
109
110 ret = drm_gem_handle_create(file_priv, kernel_state->bo[i],
111 &handle);
112
113 if (ret) {
Christophe JAILLETd0b1d252017-05-12 14:38:03 +0200114 state->bo_count = i;
115 goto err_delete_handle;
Eric Anholt21461362015-10-30 10:09:02 -0700116 }
117 bo_state[i].handle = handle;
118 bo_state[i].paddr = vc4_bo->base.paddr;
119 bo_state[i].size = vc4_bo->base.base.size;
120 }
121
Eric Anholt95d7cbc2017-07-25 11:27:16 -0700122 if (copy_to_user(u64_to_user_ptr(get_state->bo),
Dan Carpenter65c47772015-12-17 15:36:28 +0300123 bo_state,
124 state->bo_count * sizeof(*bo_state)))
125 ret = -EFAULT;
126
Christophe JAILLETd0b1d252017-05-12 14:38:03 +0200127err_delete_handle:
128 if (ret) {
129 for (i = 0; i < state->bo_count; i++)
130 drm_gem_handle_delete(file_priv, bo_state[i].handle);
131 }
Eric Anholt21461362015-10-30 10:09:02 -0700132
133err_free:
Eric Anholt21461362015-10-30 10:09:02 -0700134 vc4_free_hang_state(dev, kernel_state);
Christophe JAILLETd0b1d252017-05-12 14:38:03 +0200135 kfree(bo_state);
Eric Anholt21461362015-10-30 10:09:02 -0700136
Eric Anholt21461362015-10-30 10:09:02 -0700137 return ret;
138}
139
140static void
141vc4_save_hang_state(struct drm_device *dev)
142{
143 struct vc4_dev *vc4 = to_vc4_dev(dev);
144 struct drm_vc4_get_hang_state *state;
145 struct vc4_hang_state *kernel_state;
Varad Gautamca26d282016-02-17 19:08:21 +0530146 struct vc4_exec_info *exec[2];
Eric Anholt21461362015-10-30 10:09:02 -0700147 struct vc4_bo *bo;
148 unsigned long irqflags;
Varad Gautamca26d282016-02-17 19:08:21 +0530149 unsigned int i, j, unref_list_count, prev_idx;
Eric Anholt21461362015-10-30 10:09:02 -0700150
Dan Carpenter7e5082f2015-12-17 15:39:08 +0300151 kernel_state = kcalloc(1, sizeof(*kernel_state), GFP_KERNEL);
Eric Anholt21461362015-10-30 10:09:02 -0700152 if (!kernel_state)
153 return;
154
155 state = &kernel_state->user_state;
156
157 spin_lock_irqsave(&vc4->job_lock, irqflags);
Varad Gautamca26d282016-02-17 19:08:21 +0530158 exec[0] = vc4_first_bin_job(vc4);
159 exec[1] = vc4_first_render_job(vc4);
160 if (!exec[0] && !exec[1]) {
Eric Anholt21461362015-10-30 10:09:02 -0700161 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
162 return;
163 }
164
Varad Gautamca26d282016-02-17 19:08:21 +0530165 /* Get the bos from both binner and renderer into hang state. */
166 state->bo_count = 0;
167 for (i = 0; i < 2; i++) {
168 if (!exec[i])
169 continue;
Eric Anholt21461362015-10-30 10:09:02 -0700170
Varad Gautamca26d282016-02-17 19:08:21 +0530171 unref_list_count = 0;
172 list_for_each_entry(bo, &exec[i]->unref_list, unref_head)
173 unref_list_count++;
174 state->bo_count += exec[i]->bo_count + unref_list_count;
175 }
176
177 kernel_state->bo = kcalloc(state->bo_count,
178 sizeof(*kernel_state->bo), GFP_ATOMIC);
179
Eric Anholt21461362015-10-30 10:09:02 -0700180 if (!kernel_state->bo) {
181 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
182 return;
183 }
184
Varad Gautamca26d282016-02-17 19:08:21 +0530185 prev_idx = 0;
186 for (i = 0; i < 2; i++) {
187 if (!exec[i])
188 continue;
189
190 for (j = 0; j < exec[i]->bo_count; j++) {
Boris Brezillonb9f19252017-10-19 14:57:48 +0200191 bo = to_vc4_bo(&exec[i]->bo[j]->base);
192
193 /* Retain BOs just in case they were marked purgeable.
194 * This prevents the BO from being purged before
195 * someone had a chance to dump the hang state.
196 */
197 WARN_ON(!refcount_read(&bo->usecnt));
198 refcount_inc(&bo->usecnt);
Cihangir Akturk1d5494e2017-08-03 14:58:40 +0300199 drm_gem_object_get(&exec[i]->bo[j]->base);
Varad Gautamca26d282016-02-17 19:08:21 +0530200 kernel_state->bo[j + prev_idx] = &exec[i]->bo[j]->base;
201 }
202
203 list_for_each_entry(bo, &exec[i]->unref_list, unref_head) {
Boris Brezillonb9f19252017-10-19 14:57:48 +0200204 /* No need to retain BOs coming from the ->unref_list
205 * because they are naturally unpurgeable.
206 */
Cihangir Akturk1d5494e2017-08-03 14:58:40 +0300207 drm_gem_object_get(&bo->base.base);
Varad Gautamca26d282016-02-17 19:08:21 +0530208 kernel_state->bo[j + prev_idx] = &bo->base.base;
209 j++;
210 }
211 prev_idx = j + 1;
Eric Anholt21461362015-10-30 10:09:02 -0700212 }
213
Varad Gautamca26d282016-02-17 19:08:21 +0530214 if (exec[0])
215 state->start_bin = exec[0]->ct0ca;
216 if (exec[1])
217 state->start_render = exec[1]->ct1ca;
Eric Anholt21461362015-10-30 10:09:02 -0700218
219 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
220
221 state->ct0ca = V3D_READ(V3D_CTNCA(0));
222 state->ct0ea = V3D_READ(V3D_CTNEA(0));
223
224 state->ct1ca = V3D_READ(V3D_CTNCA(1));
225 state->ct1ea = V3D_READ(V3D_CTNEA(1));
226
227 state->ct0cs = V3D_READ(V3D_CTNCS(0));
228 state->ct1cs = V3D_READ(V3D_CTNCS(1));
229
230 state->ct0ra0 = V3D_READ(V3D_CT00RA0);
231 state->ct1ra0 = V3D_READ(V3D_CT01RA0);
232
233 state->bpca = V3D_READ(V3D_BPCA);
234 state->bpcs = V3D_READ(V3D_BPCS);
235 state->bpoa = V3D_READ(V3D_BPOA);
236 state->bpos = V3D_READ(V3D_BPOS);
237
238 state->vpmbase = V3D_READ(V3D_VPMBASE);
239
240 state->dbge = V3D_READ(V3D_DBGE);
241 state->fdbgo = V3D_READ(V3D_FDBGO);
242 state->fdbgb = V3D_READ(V3D_FDBGB);
243 state->fdbgr = V3D_READ(V3D_FDBGR);
244 state->fdbgs = V3D_READ(V3D_FDBGS);
245 state->errstat = V3D_READ(V3D_ERRSTAT);
246
Boris Brezillonb9f19252017-10-19 14:57:48 +0200247 /* We need to turn purgeable BOs into unpurgeable ones so that
248 * userspace has a chance to dump the hang state before the kernel
249 * decides to purge those BOs.
250 * Note that BO consistency at dump time cannot be guaranteed. For
251 * example, if the owner of these BOs decides to re-use them or mark
252 * them purgeable again there's nothing we can do to prevent it.
253 */
254 for (i = 0; i < kernel_state->user_state.bo_count; i++) {
255 struct vc4_bo *bo = to_vc4_bo(kernel_state->bo[i]);
256
257 if (bo->madv == __VC4_MADV_NOTSUPP)
258 continue;
259
260 mutex_lock(&bo->madv_lock);
261 if (!WARN_ON(bo->madv == __VC4_MADV_PURGED))
262 bo->madv = VC4_MADV_WILLNEED;
263 refcount_dec(&bo->usecnt);
264 mutex_unlock(&bo->madv_lock);
265 }
266
Eric Anholt21461362015-10-30 10:09:02 -0700267 spin_lock_irqsave(&vc4->job_lock, irqflags);
268 if (vc4->hang_state) {
269 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
270 vc4_free_hang_state(dev, kernel_state);
271 } else {
272 vc4->hang_state = kernel_state;
273 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
274 }
275}
276
Eric Anholtd5b1a782015-11-30 12:13:37 -0800277static void
278vc4_reset(struct drm_device *dev)
279{
280 struct vc4_dev *vc4 = to_vc4_dev(dev);
281
282 DRM_INFO("Resetting GPU.\n");
Eric Anholt36cb6252016-02-08 12:59:02 -0800283
284 mutex_lock(&vc4->power_lock);
285 if (vc4->power_refcount) {
286 /* Power the device off and back on the by dropping the
287 * reference on runtime PM.
288 */
289 pm_runtime_put_sync_suspend(&vc4->v3d->pdev->dev);
290 pm_runtime_get_sync(&vc4->v3d->pdev->dev);
291 }
292 mutex_unlock(&vc4->power_lock);
Eric Anholtd5b1a782015-11-30 12:13:37 -0800293
294 vc4_irq_reset(dev);
295
296 /* Rearm the hangcheck -- another job might have been waiting
297 * for our hung one to get kicked off, and vc4_irq_reset()
298 * would have started it.
299 */
300 vc4_queue_hangcheck(dev);
301}
302
303static void
304vc4_reset_work(struct work_struct *work)
305{
306 struct vc4_dev *vc4 =
307 container_of(work, struct vc4_dev, hangcheck.reset_work);
308
Eric Anholt21461362015-10-30 10:09:02 -0700309 vc4_save_hang_state(vc4->dev);
310
Eric Anholtd5b1a782015-11-30 12:13:37 -0800311 vc4_reset(vc4->dev);
312}
313
314static void
Kees Cook00787302017-10-24 08:16:48 -0700315vc4_hangcheck_elapsed(struct timer_list *t)
Eric Anholtd5b1a782015-11-30 12:13:37 -0800316{
Kees Cook00787302017-10-24 08:16:48 -0700317 struct vc4_dev *vc4 = from_timer(vc4, t, hangcheck.timer);
318 struct drm_device *dev = vc4->dev;
Eric Anholtd5b1a782015-11-30 12:13:37 -0800319 uint32_t ct0ca, ct1ca;
Eric Anholtc4ce60d2016-02-08 11:19:14 -0800320 unsigned long irqflags;
Varad Gautamca26d282016-02-17 19:08:21 +0530321 struct vc4_exec_info *bin_exec, *render_exec;
Eric Anholtc4ce60d2016-02-08 11:19:14 -0800322
323 spin_lock_irqsave(&vc4->job_lock, irqflags);
Varad Gautamca26d282016-02-17 19:08:21 +0530324
325 bin_exec = vc4_first_bin_job(vc4);
326 render_exec = vc4_first_render_job(vc4);
Eric Anholtd5b1a782015-11-30 12:13:37 -0800327
328 /* If idle, we can stop watching for hangs. */
Varad Gautamca26d282016-02-17 19:08:21 +0530329 if (!bin_exec && !render_exec) {
Eric Anholtc4ce60d2016-02-08 11:19:14 -0800330 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
Eric Anholtd5b1a782015-11-30 12:13:37 -0800331 return;
Eric Anholtc4ce60d2016-02-08 11:19:14 -0800332 }
Eric Anholtd5b1a782015-11-30 12:13:37 -0800333
334 ct0ca = V3D_READ(V3D_CTNCA(0));
335 ct1ca = V3D_READ(V3D_CTNCA(1));
336
337 /* If we've made any progress in execution, rearm the timer
338 * and wait.
339 */
Varad Gautamca26d282016-02-17 19:08:21 +0530340 if ((bin_exec && ct0ca != bin_exec->last_ct0ca) ||
341 (render_exec && ct1ca != render_exec->last_ct1ca)) {
342 if (bin_exec)
343 bin_exec->last_ct0ca = ct0ca;
344 if (render_exec)
345 render_exec->last_ct1ca = ct1ca;
Eric Anholtc4ce60d2016-02-08 11:19:14 -0800346 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
Eric Anholtd5b1a782015-11-30 12:13:37 -0800347 vc4_queue_hangcheck(dev);
348 return;
349 }
350
Eric Anholtc4ce60d2016-02-08 11:19:14 -0800351 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
352
Eric Anholtd5b1a782015-11-30 12:13:37 -0800353 /* We've gone too long with no progress, reset. This has to
354 * be done from a work struct, since resetting can sleep and
355 * this timer hook isn't allowed to.
356 */
357 schedule_work(&vc4->hangcheck.reset_work);
358}
359
360static void
361submit_cl(struct drm_device *dev, uint32_t thread, uint32_t start, uint32_t end)
362{
363 struct vc4_dev *vc4 = to_vc4_dev(dev);
364
365 /* Set the current and end address of the control list.
366 * Writing the end register is what starts the job.
367 */
368 V3D_WRITE(V3D_CTNCA(thread), start);
369 V3D_WRITE(V3D_CTNEA(thread), end);
370}
371
372int
373vc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno, uint64_t timeout_ns,
374 bool interruptible)
375{
376 struct vc4_dev *vc4 = to_vc4_dev(dev);
377 int ret = 0;
378 unsigned long timeout_expire;
379 DEFINE_WAIT(wait);
380
381 if (vc4->finished_seqno >= seqno)
382 return 0;
383
384 if (timeout_ns == 0)
385 return -ETIME;
386
387 timeout_expire = jiffies + nsecs_to_jiffies(timeout_ns);
388
389 trace_vc4_wait_for_seqno_begin(dev, seqno, timeout_ns);
390 for (;;) {
391 prepare_to_wait(&vc4->job_wait_queue, &wait,
392 interruptible ? TASK_INTERRUPTIBLE :
393 TASK_UNINTERRUPTIBLE);
394
395 if (interruptible && signal_pending(current)) {
396 ret = -ERESTARTSYS;
397 break;
398 }
399
400 if (vc4->finished_seqno >= seqno)
401 break;
402
403 if (timeout_ns != ~0ull) {
404 if (time_after_eq(jiffies, timeout_expire)) {
405 ret = -ETIME;
406 break;
407 }
408 schedule_timeout(timeout_expire - jiffies);
409 } else {
410 schedule();
411 }
412 }
413
414 finish_wait(&vc4->job_wait_queue, &wait);
415 trace_vc4_wait_for_seqno_end(dev, seqno);
416
Eric Anholt13cf8902016-01-25 14:32:41 -0800417 return ret;
Eric Anholtd5b1a782015-11-30 12:13:37 -0800418}
419
420static void
421vc4_flush_caches(struct drm_device *dev)
422{
423 struct vc4_dev *vc4 = to_vc4_dev(dev);
424
425 /* Flush the GPU L2 caches. These caches sit on top of system
426 * L3 (the 128kb or so shared with the CPU), and are
427 * non-allocating in the L3.
428 */
429 V3D_WRITE(V3D_L2CACTL,
430 V3D_L2CACTL_L2CCLR);
431
432 V3D_WRITE(V3D_SLCACTL,
433 VC4_SET_FIELD(0xf, V3D_SLCACTL_T1CC) |
434 VC4_SET_FIELD(0xf, V3D_SLCACTL_T0CC) |
435 VC4_SET_FIELD(0xf, V3D_SLCACTL_UCC) |
436 VC4_SET_FIELD(0xf, V3D_SLCACTL_ICC));
437}
438
439/* Sets the registers for the next job to be actually be executed in
440 * the hardware.
441 *
442 * The job_lock should be held during this.
443 */
444void
Varad Gautamca26d282016-02-17 19:08:21 +0530445vc4_submit_next_bin_job(struct drm_device *dev)
Eric Anholtd5b1a782015-11-30 12:13:37 -0800446{
447 struct vc4_dev *vc4 = to_vc4_dev(dev);
Varad Gautamca26d282016-02-17 19:08:21 +0530448 struct vc4_exec_info *exec;
Eric Anholtd5b1a782015-11-30 12:13:37 -0800449
Varad Gautamca26d282016-02-17 19:08:21 +0530450again:
451 exec = vc4_first_bin_job(vc4);
Eric Anholtd5b1a782015-11-30 12:13:37 -0800452 if (!exec)
453 return;
454
455 vc4_flush_caches(dev);
456
Boris Brezillon65101d82018-01-12 10:09:26 +0100457 /* Only start the perfmon if it was not already started by a previous
458 * job.
459 */
460 if (exec->perfmon && vc4->active_perfmon != exec->perfmon)
461 vc4_perfmon_start(vc4, exec->perfmon);
462
Varad Gautamca26d282016-02-17 19:08:21 +0530463 /* Either put the job in the binner if it uses the binner, or
464 * immediately move it to the to-be-rendered queue.
465 */
466 if (exec->ct0ca != exec->ct0ea) {
Eric Anholtd5b1a782015-11-30 12:13:37 -0800467 submit_cl(dev, 0, exec->ct0ca, exec->ct0ea);
Varad Gautamca26d282016-02-17 19:08:21 +0530468 } else {
Boris Brezillon65101d82018-01-12 10:09:26 +0100469 struct vc4_exec_info *next;
470
Varad Gautamca26d282016-02-17 19:08:21 +0530471 vc4_move_job_to_render(dev, exec);
Boris Brezillon65101d82018-01-12 10:09:26 +0100472 next = vc4_first_bin_job(vc4);
473
474 /* We can't start the next bin job if the previous job had a
475 * different perfmon instance attached to it. The same goes
476 * if one of them had a perfmon attached to it and the other
477 * one doesn't.
478 */
479 if (next && next->perfmon == exec->perfmon)
480 goto again;
Varad Gautamca26d282016-02-17 19:08:21 +0530481 }
482}
483
484void
485vc4_submit_next_render_job(struct drm_device *dev)
486{
487 struct vc4_dev *vc4 = to_vc4_dev(dev);
488 struct vc4_exec_info *exec = vc4_first_render_job(vc4);
489
490 if (!exec)
491 return;
492
Eric Anholtd5b1a782015-11-30 12:13:37 -0800493 submit_cl(dev, 1, exec->ct1ca, exec->ct1ea);
494}
495
Varad Gautamca26d282016-02-17 19:08:21 +0530496void
497vc4_move_job_to_render(struct drm_device *dev, struct vc4_exec_info *exec)
498{
499 struct vc4_dev *vc4 = to_vc4_dev(dev);
500 bool was_empty = list_empty(&vc4->render_job_list);
501
502 list_move_tail(&exec->head, &vc4->render_job_list);
503 if (was_empty)
504 vc4_submit_next_render_job(dev);
505}
506
Eric Anholtd5b1a782015-11-30 12:13:37 -0800507static void
508vc4_update_bo_seqnos(struct vc4_exec_info *exec, uint64_t seqno)
509{
510 struct vc4_bo *bo;
511 unsigned i;
512
513 for (i = 0; i < exec->bo_count; i++) {
514 bo = to_vc4_bo(&exec->bo[i]->base);
515 bo->seqno = seqno;
Eric Anholtcdec4d32017-04-12 12:12:02 -0700516
517 reservation_object_add_shared_fence(bo->resv, exec->fence);
Eric Anholtd5b1a782015-11-30 12:13:37 -0800518 }
519
520 list_for_each_entry(bo, &exec->unref_list, unref_head) {
521 bo->seqno = seqno;
522 }
Eric Anholt7edabee2016-09-27 09:03:13 -0700523
524 for (i = 0; i < exec->rcl_write_bo_count; i++) {
525 bo = to_vc4_bo(&exec->rcl_write_bo[i]->base);
526 bo->write_seqno = seqno;
Eric Anholtcdec4d32017-04-12 12:12:02 -0700527
528 reservation_object_add_excl_fence(bo->resv, exec->fence);
Eric Anholt7edabee2016-09-27 09:03:13 -0700529 }
Eric Anholtd5b1a782015-11-30 12:13:37 -0800530}
531
Eric Anholtcdec4d32017-04-12 12:12:02 -0700532static void
533vc4_unlock_bo_reservations(struct drm_device *dev,
534 struct vc4_exec_info *exec,
535 struct ww_acquire_ctx *acquire_ctx)
536{
537 int i;
538
539 for (i = 0; i < exec->bo_count; i++) {
540 struct vc4_bo *bo = to_vc4_bo(&exec->bo[i]->base);
541
542 ww_mutex_unlock(&bo->resv->lock);
543 }
544
545 ww_acquire_fini(acquire_ctx);
546}
547
548/* Takes the reservation lock on all the BOs being referenced, so that
549 * at queue submit time we can update the reservations.
550 *
551 * We don't lock the RCL the tile alloc/state BOs, or overflow memory
552 * (all of which are on exec->unref_list). They're entirely private
553 * to vc4, so we don't attach dma-buf fences to them.
554 */
555static int
556vc4_lock_bo_reservations(struct drm_device *dev,
557 struct vc4_exec_info *exec,
558 struct ww_acquire_ctx *acquire_ctx)
559{
560 int contended_lock = -1;
561 int i, ret;
562 struct vc4_bo *bo;
563
564 ww_acquire_init(acquire_ctx, &reservation_ww_class);
565
566retry:
567 if (contended_lock != -1) {
568 bo = to_vc4_bo(&exec->bo[contended_lock]->base);
569 ret = ww_mutex_lock_slow_interruptible(&bo->resv->lock,
570 acquire_ctx);
571 if (ret) {
572 ww_acquire_done(acquire_ctx);
573 return ret;
574 }
575 }
576
577 for (i = 0; i < exec->bo_count; i++) {
578 if (i == contended_lock)
579 continue;
580
581 bo = to_vc4_bo(&exec->bo[i]->base);
582
583 ret = ww_mutex_lock_interruptible(&bo->resv->lock, acquire_ctx);
584 if (ret) {
585 int j;
586
587 for (j = 0; j < i; j++) {
588 bo = to_vc4_bo(&exec->bo[j]->base);
589 ww_mutex_unlock(&bo->resv->lock);
590 }
591
592 if (contended_lock != -1 && contended_lock >= i) {
593 bo = to_vc4_bo(&exec->bo[contended_lock]->base);
594
595 ww_mutex_unlock(&bo->resv->lock);
596 }
597
598 if (ret == -EDEADLK) {
599 contended_lock = i;
600 goto retry;
601 }
602
603 ww_acquire_done(acquire_ctx);
604 return ret;
605 }
606 }
607
608 ww_acquire_done(acquire_ctx);
609
610 /* Reserve space for our shared (read-only) fence references,
611 * before we commit the CL to the hardware.
612 */
613 for (i = 0; i < exec->bo_count; i++) {
614 bo = to_vc4_bo(&exec->bo[i]->base);
615
616 ret = reservation_object_reserve_shared(bo->resv);
617 if (ret) {
618 vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
619 return ret;
620 }
621 }
622
623 return 0;
624}
625
Eric Anholtd5b1a782015-11-30 12:13:37 -0800626/* Queues a struct vc4_exec_info for execution. If no job is
627 * currently executing, then submits it.
628 *
629 * Unlike most GPUs, our hardware only handles one command list at a
630 * time. To queue multiple jobs at once, we'd need to edit the
631 * previous command list to have a jump to the new one at the end, and
632 * then bump the end address. That's a change for a later date,
633 * though.
634 */
Eric Anholtcdec4d32017-04-12 12:12:02 -0700635static int
636vc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
637 struct ww_acquire_ctx *acquire_ctx)
Eric Anholtd5b1a782015-11-30 12:13:37 -0800638{
639 struct vc4_dev *vc4 = to_vc4_dev(dev);
Boris Brezillon65101d82018-01-12 10:09:26 +0100640 struct vc4_exec_info *renderjob;
Eric Anholtd5b1a782015-11-30 12:13:37 -0800641 uint64_t seqno;
642 unsigned long irqflags;
Eric Anholtcdec4d32017-04-12 12:12:02 -0700643 struct vc4_fence *fence;
644
645 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
646 if (!fence)
647 return -ENOMEM;
648 fence->dev = dev;
Eric Anholtd5b1a782015-11-30 12:13:37 -0800649
650 spin_lock_irqsave(&vc4->job_lock, irqflags);
651
652 seqno = ++vc4->emit_seqno;
653 exec->seqno = seqno;
Eric Anholtcdec4d32017-04-12 12:12:02 -0700654
655 dma_fence_init(&fence->base, &vc4_fence_ops, &vc4->job_lock,
656 vc4->dma_fence_context, exec->seqno);
657 fence->seqno = exec->seqno;
658 exec->fence = &fence->base;
659
Eric Anholtd5b1a782015-11-30 12:13:37 -0800660 vc4_update_bo_seqnos(exec, seqno);
661
Eric Anholtcdec4d32017-04-12 12:12:02 -0700662 vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
663
Varad Gautamca26d282016-02-17 19:08:21 +0530664 list_add_tail(&exec->head, &vc4->bin_job_list);
Eric Anholtd5b1a782015-11-30 12:13:37 -0800665
Boris Brezillon65101d82018-01-12 10:09:26 +0100666 /* If no bin job was executing and if the render job (if any) has the
667 * same perfmon as our job attached to it (or if both jobs don't have
668 * perfmon activated), then kick ours off. Otherwise, it'll get
669 * started when the previous job's flush/render done interrupt occurs.
Eric Anholtd5b1a782015-11-30 12:13:37 -0800670 */
Boris Brezillon65101d82018-01-12 10:09:26 +0100671 renderjob = vc4_first_render_job(vc4);
672 if (vc4_first_bin_job(vc4) == exec &&
673 (!renderjob || renderjob->perfmon == exec->perfmon)) {
Varad Gautamca26d282016-02-17 19:08:21 +0530674 vc4_submit_next_bin_job(dev);
Eric Anholtd5b1a782015-11-30 12:13:37 -0800675 vc4_queue_hangcheck(dev);
676 }
677
678 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
Eric Anholtcdec4d32017-04-12 12:12:02 -0700679
680 return 0;
Eric Anholtd5b1a782015-11-30 12:13:37 -0800681}
682
683/**
Eric Anholt72f793f2017-02-27 12:11:41 -0800684 * vc4_cl_lookup_bos() - Sets up exec->bo[] with the GEM objects
685 * referenced by the job.
686 * @dev: DRM device
687 * @file_priv: DRM file for this fd
688 * @exec: V3D job being set up
689 *
690 * The command validator needs to reference BOs by their index within
691 * the submitted job's BO list. This does the validation of the job's
692 * BO list and reference counting for the lifetime of the job.
Eric Anholtd5b1a782015-11-30 12:13:37 -0800693 */
694static int
695vc4_cl_lookup_bos(struct drm_device *dev,
696 struct drm_file *file_priv,
697 struct vc4_exec_info *exec)
698{
699 struct drm_vc4_submit_cl *args = exec->args;
700 uint32_t *handles;
701 int ret = 0;
702 int i;
703
704 exec->bo_count = args->bo_handle_count;
705
706 if (!exec->bo_count) {
707 /* See comment on bo_index for why we have to check
708 * this.
709 */
Eric Anholtfb959922017-07-25 09:27:32 -0700710 DRM_DEBUG("Rendering requires BOs to validate\n");
Eric Anholtd5b1a782015-11-30 12:13:37 -0800711 return -EINVAL;
712 }
713
Michal Hocko20981052017-05-17 14:23:12 +0200714 exec->bo = kvmalloc_array(exec->bo_count,
715 sizeof(struct drm_gem_cma_object *),
716 GFP_KERNEL | __GFP_ZERO);
Eric Anholtd5b1a782015-11-30 12:13:37 -0800717 if (!exec->bo) {
718 DRM_ERROR("Failed to allocate validated BO pointers\n");
719 return -ENOMEM;
720 }
721
Michal Hocko20981052017-05-17 14:23:12 +0200722 handles = kvmalloc_array(exec->bo_count, sizeof(uint32_t), GFP_KERNEL);
Eric Anholtd5b1a782015-11-30 12:13:37 -0800723 if (!handles) {
Dan Carpenterb2cdeb12016-10-13 11:54:31 +0300724 ret = -ENOMEM;
Eric Anholtd5b1a782015-11-30 12:13:37 -0800725 DRM_ERROR("Failed to allocate incoming GEM handles\n");
726 goto fail;
727 }
728
Eric Anholt95d7cbc2017-07-25 11:27:16 -0700729 if (copy_from_user(handles, u64_to_user_ptr(args->bo_handles),
Dan Carpenterb2cdeb12016-10-13 11:54:31 +0300730 exec->bo_count * sizeof(uint32_t))) {
731 ret = -EFAULT;
Eric Anholtd5b1a782015-11-30 12:13:37 -0800732 DRM_ERROR("Failed to copy in GEM handles\n");
733 goto fail;
734 }
735
736 spin_lock(&file_priv->table_lock);
737 for (i = 0; i < exec->bo_count; i++) {
738 struct drm_gem_object *bo = idr_find(&file_priv->object_idr,
739 handles[i]);
740 if (!bo) {
Eric Anholtfb959922017-07-25 09:27:32 -0700741 DRM_DEBUG("Failed to look up GEM BO %d: %d\n",
Eric Anholtd5b1a782015-11-30 12:13:37 -0800742 i, handles[i]);
743 ret = -EINVAL;
Boris Brezillonb9f19252017-10-19 14:57:48 +0200744 break;
Eric Anholtd5b1a782015-11-30 12:13:37 -0800745 }
Boris Brezillonb9f19252017-10-19 14:57:48 +0200746
Cihangir Akturk1d5494e2017-08-03 14:58:40 +0300747 drm_gem_object_get(bo);
Eric Anholtd5b1a782015-11-30 12:13:37 -0800748 exec->bo[i] = (struct drm_gem_cma_object *)bo;
749 }
750 spin_unlock(&file_priv->table_lock);
751
Boris Brezillonb9f19252017-10-19 14:57:48 +0200752 if (ret)
753 goto fail_put_bo;
754
755 for (i = 0; i < exec->bo_count; i++) {
756 ret = vc4_bo_inc_usecnt(to_vc4_bo(&exec->bo[i]->base));
757 if (ret)
758 goto fail_dec_usecnt;
759 }
760
761 kvfree(handles);
762 return 0;
763
764fail_dec_usecnt:
765 /* Decrease usecnt on acquired objects.
766 * We cannot rely on vc4_complete_exec() to release resources here,
767 * because vc4_complete_exec() has no information about which BO has
768 * had its ->usecnt incremented.
769 * To make things easier we just free everything explicitly and set
770 * exec->bo to NULL so that vc4_complete_exec() skips the 'BO release'
771 * step.
772 */
773 for (i-- ; i >= 0; i--)
774 vc4_bo_dec_usecnt(to_vc4_bo(&exec->bo[i]->base));
775
776fail_put_bo:
777 /* Release any reference to acquired objects. */
778 for (i = 0; i < exec->bo_count && exec->bo[i]; i++)
779 drm_gem_object_put_unlocked(&exec->bo[i]->base);
780
Eric Anholtd5b1a782015-11-30 12:13:37 -0800781fail:
Michal Hocko20981052017-05-17 14:23:12 +0200782 kvfree(handles);
Boris Brezillonb9f19252017-10-19 14:57:48 +0200783 kvfree(exec->bo);
784 exec->bo = NULL;
Eric Anholt552416c2016-07-26 13:47:15 -0700785 return ret;
Eric Anholtd5b1a782015-11-30 12:13:37 -0800786}
787
788static int
789vc4_get_bcl(struct drm_device *dev, struct vc4_exec_info *exec)
790{
791 struct drm_vc4_submit_cl *args = exec->args;
792 void *temp = NULL;
793 void *bin;
794 int ret = 0;
795 uint32_t bin_offset = 0;
796 uint32_t shader_rec_offset = roundup(bin_offset + args->bin_cl_size,
797 16);
798 uint32_t uniforms_offset = shader_rec_offset + args->shader_rec_size;
799 uint32_t exec_size = uniforms_offset + args->uniforms_size;
800 uint32_t temp_size = exec_size + (sizeof(struct vc4_shader_state) *
801 args->shader_rec_count);
802 struct vc4_bo *bo;
803
Eric Anholt0f2ff822017-01-17 21:42:53 +1100804 if (shader_rec_offset < args->bin_cl_size ||
805 uniforms_offset < shader_rec_offset ||
Eric Anholtd5b1a782015-11-30 12:13:37 -0800806 exec_size < uniforms_offset ||
807 args->shader_rec_count >= (UINT_MAX /
808 sizeof(struct vc4_shader_state)) ||
809 temp_size < exec_size) {
Eric Anholtfb959922017-07-25 09:27:32 -0700810 DRM_DEBUG("overflow in exec arguments\n");
Eric Anholt6b8ac632017-01-17 21:58:06 +1100811 ret = -EINVAL;
Eric Anholtd5b1a782015-11-30 12:13:37 -0800812 goto fail;
813 }
814
815 /* Allocate space where we'll store the copied in user command lists
816 * and shader records.
817 *
818 * We don't just copy directly into the BOs because we need to
819 * read the contents back for validation, and I think the
820 * bo->vaddr is uncached access.
821 */
Michal Hocko20981052017-05-17 14:23:12 +0200822 temp = kvmalloc_array(temp_size, 1, GFP_KERNEL);
Eric Anholtd5b1a782015-11-30 12:13:37 -0800823 if (!temp) {
824 DRM_ERROR("Failed to allocate storage for copying "
825 "in bin/render CLs.\n");
826 ret = -ENOMEM;
827 goto fail;
828 }
829 bin = temp + bin_offset;
830 exec->shader_rec_u = temp + shader_rec_offset;
831 exec->uniforms_u = temp + uniforms_offset;
832 exec->shader_state = temp + exec_size;
833 exec->shader_state_size = args->shader_rec_count;
834
Dan Carpenter65c47772015-12-17 15:36:28 +0300835 if (copy_from_user(bin,
Eric Anholt95d7cbc2017-07-25 11:27:16 -0700836 u64_to_user_ptr(args->bin_cl),
Dan Carpenter65c47772015-12-17 15:36:28 +0300837 args->bin_cl_size)) {
838 ret = -EFAULT;
Eric Anholtd5b1a782015-11-30 12:13:37 -0800839 goto fail;
840 }
841
Dan Carpenter65c47772015-12-17 15:36:28 +0300842 if (copy_from_user(exec->shader_rec_u,
Eric Anholt95d7cbc2017-07-25 11:27:16 -0700843 u64_to_user_ptr(args->shader_rec),
Dan Carpenter65c47772015-12-17 15:36:28 +0300844 args->shader_rec_size)) {
845 ret = -EFAULT;
Eric Anholtd5b1a782015-11-30 12:13:37 -0800846 goto fail;
847 }
848
Dan Carpenter65c47772015-12-17 15:36:28 +0300849 if (copy_from_user(exec->uniforms_u,
Eric Anholt95d7cbc2017-07-25 11:27:16 -0700850 u64_to_user_ptr(args->uniforms),
Dan Carpenter65c47772015-12-17 15:36:28 +0300851 args->uniforms_size)) {
852 ret = -EFAULT;
Eric Anholtd5b1a782015-11-30 12:13:37 -0800853 goto fail;
854 }
855
Eric Anholtf3099462017-07-25 11:27:17 -0700856 bo = vc4_bo_create(dev, exec_size, true, VC4_BO_TYPE_BCL);
Eric Anholt2c68f1f2016-01-25 14:13:12 -0800857 if (IS_ERR(bo)) {
Eric Anholtd5b1a782015-11-30 12:13:37 -0800858 DRM_ERROR("Couldn't allocate BO for binning\n");
Eric Anholt2c68f1f2016-01-25 14:13:12 -0800859 ret = PTR_ERR(bo);
Eric Anholtd5b1a782015-11-30 12:13:37 -0800860 goto fail;
861 }
862 exec->exec_bo = &bo->base;
863
864 list_add_tail(&to_vc4_bo(&exec->exec_bo->base)->unref_head,
865 &exec->unref_list);
866
867 exec->ct0ca = exec->exec_bo->paddr + bin_offset;
868
869 exec->bin_u = bin;
870
871 exec->shader_rec_v = exec->exec_bo->vaddr + shader_rec_offset;
872 exec->shader_rec_p = exec->exec_bo->paddr + shader_rec_offset;
873 exec->shader_rec_size = args->shader_rec_size;
874
875 exec->uniforms_v = exec->exec_bo->vaddr + uniforms_offset;
876 exec->uniforms_p = exec->exec_bo->paddr + uniforms_offset;
877 exec->uniforms_size = args->uniforms_size;
878
879 ret = vc4_validate_bin_cl(dev,
880 exec->exec_bo->vaddr + bin_offset,
881 bin,
882 exec);
883 if (ret)
884 goto fail;
885
886 ret = vc4_validate_shader_recs(dev, exec);
Eric Anholt7edabee2016-09-27 09:03:13 -0700887 if (ret)
888 goto fail;
889
890 /* Block waiting on any previous rendering into the CS's VBO,
891 * IB, or textures, so that pixels are actually written by the
892 * time we try to read them.
893 */
894 ret = vc4_wait_for_seqno(dev, exec->bin_dep_seqno, ~0ull, true);
Eric Anholtd5b1a782015-11-30 12:13:37 -0800895
896fail:
Michal Hocko20981052017-05-17 14:23:12 +0200897 kvfree(temp);
Eric Anholtd5b1a782015-11-30 12:13:37 -0800898 return ret;
899}
900
901static void
902vc4_complete_exec(struct drm_device *dev, struct vc4_exec_info *exec)
903{
Eric Anholt001bdb52016-02-05 17:41:49 -0800904 struct vc4_dev *vc4 = to_vc4_dev(dev);
Eric Anholt553c9422017-03-27 16:10:25 -0700905 unsigned long irqflags;
Eric Anholtd5b1a782015-11-30 12:13:37 -0800906 unsigned i;
907
Eric Anholtcdec4d32017-04-12 12:12:02 -0700908 /* If we got force-completed because of GPU reset rather than
909 * through our IRQ handler, signal the fence now.
910 */
Stefan Schakebabc8112017-12-02 18:40:39 +0100911 if (exec->fence) {
Eric Anholtcdec4d32017-04-12 12:12:02 -0700912 dma_fence_signal(exec->fence);
Stefan Schakebabc8112017-12-02 18:40:39 +0100913 dma_fence_put(exec->fence);
914 }
Eric Anholtcdec4d32017-04-12 12:12:02 -0700915
Eric Anholtd5b1a782015-11-30 12:13:37 -0800916 if (exec->bo) {
Boris Brezillonb9f19252017-10-19 14:57:48 +0200917 for (i = 0; i < exec->bo_count; i++) {
918 struct vc4_bo *bo = to_vc4_bo(&exec->bo[i]->base);
919
920 vc4_bo_dec_usecnt(bo);
Cihangir Akturk1d5494e2017-08-03 14:58:40 +0300921 drm_gem_object_put_unlocked(&exec->bo[i]->base);
Boris Brezillonb9f19252017-10-19 14:57:48 +0200922 }
Michal Hocko20981052017-05-17 14:23:12 +0200923 kvfree(exec->bo);
Eric Anholtd5b1a782015-11-30 12:13:37 -0800924 }
925
926 while (!list_empty(&exec->unref_list)) {
927 struct vc4_bo *bo = list_first_entry(&exec->unref_list,
928 struct vc4_bo, unref_head);
929 list_del(&bo->unref_head);
Cihangir Akturk1d5494e2017-08-03 14:58:40 +0300930 drm_gem_object_put_unlocked(&bo->base.base);
Eric Anholtd5b1a782015-11-30 12:13:37 -0800931 }
Eric Anholtd5b1a782015-11-30 12:13:37 -0800932
Eric Anholt553c9422017-03-27 16:10:25 -0700933 /* Free up the allocation of any bin slots we used. */
934 spin_lock_irqsave(&vc4->job_lock, irqflags);
935 vc4->bin_alloc_used &= ~exec->bin_slots;
936 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
937
Boris Brezillon65101d82018-01-12 10:09:26 +0100938 /* Release the reference we had on the perf monitor. */
939 vc4_perfmon_put(exec->perfmon);
940
Eric Anholt36cb6252016-02-08 12:59:02 -0800941 mutex_lock(&vc4->power_lock);
Eric Anholt3a622342016-11-04 15:58:38 -0700942 if (--vc4->power_refcount == 0) {
943 pm_runtime_mark_last_busy(&vc4->v3d->pdev->dev);
944 pm_runtime_put_autosuspend(&vc4->v3d->pdev->dev);
945 }
Eric Anholt36cb6252016-02-08 12:59:02 -0800946 mutex_unlock(&vc4->power_lock);
Eric Anholt001bdb52016-02-05 17:41:49 -0800947
Eric Anholtd5b1a782015-11-30 12:13:37 -0800948 kfree(exec);
949}
950
951void
952vc4_job_handle_completed(struct vc4_dev *vc4)
953{
954 unsigned long irqflags;
Eric Anholtb501bac2015-11-30 12:34:01 -0800955 struct vc4_seqno_cb *cb, *cb_temp;
Eric Anholtd5b1a782015-11-30 12:13:37 -0800956
957 spin_lock_irqsave(&vc4->job_lock, irqflags);
958 while (!list_empty(&vc4->job_done_list)) {
959 struct vc4_exec_info *exec =
960 list_first_entry(&vc4->job_done_list,
961 struct vc4_exec_info, head);
962 list_del(&exec->head);
963
964 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
965 vc4_complete_exec(vc4->dev, exec);
966 spin_lock_irqsave(&vc4->job_lock, irqflags);
967 }
Eric Anholtb501bac2015-11-30 12:34:01 -0800968
969 list_for_each_entry_safe(cb, cb_temp, &vc4->seqno_cb_list, work.entry) {
970 if (cb->seqno <= vc4->finished_seqno) {
971 list_del_init(&cb->work.entry);
972 schedule_work(&cb->work);
973 }
974 }
975
Eric Anholtd5b1a782015-11-30 12:13:37 -0800976 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
977}
978
Eric Anholtb501bac2015-11-30 12:34:01 -0800979static void vc4_seqno_cb_work(struct work_struct *work)
980{
981 struct vc4_seqno_cb *cb = container_of(work, struct vc4_seqno_cb, work);
982
983 cb->func(cb);
984}
985
986int vc4_queue_seqno_cb(struct drm_device *dev,
987 struct vc4_seqno_cb *cb, uint64_t seqno,
988 void (*func)(struct vc4_seqno_cb *cb))
989{
990 struct vc4_dev *vc4 = to_vc4_dev(dev);
991 int ret = 0;
992 unsigned long irqflags;
993
994 cb->func = func;
995 INIT_WORK(&cb->work, vc4_seqno_cb_work);
996
997 spin_lock_irqsave(&vc4->job_lock, irqflags);
998 if (seqno > vc4->finished_seqno) {
999 cb->seqno = seqno;
1000 list_add_tail(&cb->work.entry, &vc4->seqno_cb_list);
1001 } else {
1002 schedule_work(&cb->work);
1003 }
1004 spin_unlock_irqrestore(&vc4->job_lock, irqflags);
1005
1006 return ret;
1007}
1008
Eric Anholtd5b1a782015-11-30 12:13:37 -08001009/* Scheduled when any job has been completed, this walks the list of
1010 * jobs that had completed and unrefs their BOs and frees their exec
1011 * structs.
1012 */
1013static void
1014vc4_job_done_work(struct work_struct *work)
1015{
1016 struct vc4_dev *vc4 =
1017 container_of(work, struct vc4_dev, job_done_work);
1018
1019 vc4_job_handle_completed(vc4);
1020}
1021
1022static int
1023vc4_wait_for_seqno_ioctl_helper(struct drm_device *dev,
1024 uint64_t seqno,
1025 uint64_t *timeout_ns)
1026{
1027 unsigned long start = jiffies;
1028 int ret = vc4_wait_for_seqno(dev, seqno, *timeout_ns, true);
1029
1030 if ((ret == -EINTR || ret == -ERESTARTSYS) && *timeout_ns != ~0ull) {
1031 uint64_t delta = jiffies_to_nsecs(jiffies - start);
1032
1033 if (*timeout_ns >= delta)
1034 *timeout_ns -= delta;
1035 }
1036
1037 return ret;
1038}
1039
1040int
1041vc4_wait_seqno_ioctl(struct drm_device *dev, void *data,
1042 struct drm_file *file_priv)
1043{
1044 struct drm_vc4_wait_seqno *args = data;
1045
1046 return vc4_wait_for_seqno_ioctl_helper(dev, args->seqno,
1047 &args->timeout_ns);
1048}
1049
1050int
1051vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
1052 struct drm_file *file_priv)
1053{
1054 int ret;
1055 struct drm_vc4_wait_bo *args = data;
1056 struct drm_gem_object *gem_obj;
1057 struct vc4_bo *bo;
1058
Eric Anholte0015232016-01-25 13:05:00 -08001059 if (args->pad != 0)
1060 return -EINVAL;
1061
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01001062 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
Eric Anholtd5b1a782015-11-30 12:13:37 -08001063 if (!gem_obj) {
Eric Anholtfb959922017-07-25 09:27:32 -07001064 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
Eric Anholtd5b1a782015-11-30 12:13:37 -08001065 return -EINVAL;
1066 }
1067 bo = to_vc4_bo(gem_obj);
1068
1069 ret = vc4_wait_for_seqno_ioctl_helper(dev, bo->seqno,
1070 &args->timeout_ns);
1071
Cihangir Akturk1d5494e2017-08-03 14:58:40 +03001072 drm_gem_object_put_unlocked(gem_obj);
Eric Anholtd5b1a782015-11-30 12:13:37 -08001073 return ret;
1074}
1075
1076/**
Eric Anholt72f793f2017-02-27 12:11:41 -08001077 * vc4_submit_cl_ioctl() - Submits a job (frame) to the VC4.
1078 * @dev: DRM device
1079 * @data: ioctl argument
1080 * @file_priv: DRM file for this fd
Eric Anholtd5b1a782015-11-30 12:13:37 -08001081 *
Eric Anholt72f793f2017-02-27 12:11:41 -08001082 * This is the main entrypoint for userspace to submit a 3D frame to
1083 * the GPU. Userspace provides the binner command list (if
1084 * applicable), and the kernel sets up the render command list to draw
1085 * to the framebuffer described in the ioctl, using the command lists
1086 * that the 3D engine's binner will produce.
Eric Anholtd5b1a782015-11-30 12:13:37 -08001087 */
1088int
1089vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
1090 struct drm_file *file_priv)
1091{
1092 struct vc4_dev *vc4 = to_vc4_dev(dev);
Boris Brezillon65101d82018-01-12 10:09:26 +01001093 struct vc4_file *vc4file = file_priv->driver_priv;
Eric Anholtd5b1a782015-11-30 12:13:37 -08001094 struct drm_vc4_submit_cl *args = data;
1095 struct vc4_exec_info *exec;
Eric Anholtcdec4d32017-04-12 12:12:02 -07001096 struct ww_acquire_ctx acquire_ctx;
Eric Anholt36cb6252016-02-08 12:59:02 -08001097 int ret = 0;
Eric Anholtd5b1a782015-11-30 12:13:37 -08001098
Eric Anholt3be8edd2017-07-25 09:27:33 -07001099 if ((args->flags & ~(VC4_SUBMIT_CL_USE_CLEAR_COLOR |
1100 VC4_SUBMIT_CL_FIXED_RCL_ORDER |
1101 VC4_SUBMIT_CL_RCL_ORDER_INCREASING_X |
1102 VC4_SUBMIT_CL_RCL_ORDER_INCREASING_Y)) != 0) {
Eric Anholtfb959922017-07-25 09:27:32 -07001103 DRM_DEBUG("Unknown flags: 0x%02x\n", args->flags);
Eric Anholtd5b1a782015-11-30 12:13:37 -08001104 return -EINVAL;
1105 }
1106
Boris Brezillon65101d82018-01-12 10:09:26 +01001107 if (args->pad2 != 0) {
1108 DRM_DEBUG("->pad2 must be set to zero\n");
1109 return -EINVAL;
1110 }
1111
Eric Anholtd5b1a782015-11-30 12:13:37 -08001112 exec = kcalloc(1, sizeof(*exec), GFP_KERNEL);
1113 if (!exec) {
1114 DRM_ERROR("malloc failure on exec struct\n");
1115 return -ENOMEM;
1116 }
1117
Eric Anholt36cb6252016-02-08 12:59:02 -08001118 mutex_lock(&vc4->power_lock);
Eric Anholt925d05e2017-04-17 09:26:03 -07001119 if (vc4->power_refcount++ == 0) {
Eric Anholt36cb6252016-02-08 12:59:02 -08001120 ret = pm_runtime_get_sync(&vc4->v3d->pdev->dev);
Eric Anholt925d05e2017-04-17 09:26:03 -07001121 if (ret < 0) {
1122 mutex_unlock(&vc4->power_lock);
1123 vc4->power_refcount--;
1124 kfree(exec);
1125 return ret;
1126 }
Eric Anholt001bdb52016-02-05 17:41:49 -08001127 }
Eric Anholt925d05e2017-04-17 09:26:03 -07001128 mutex_unlock(&vc4->power_lock);
Eric Anholt001bdb52016-02-05 17:41:49 -08001129
Eric Anholtd5b1a782015-11-30 12:13:37 -08001130 exec->args = args;
1131 INIT_LIST_HEAD(&exec->unref_list);
1132
1133 ret = vc4_cl_lookup_bos(dev, file_priv, exec);
1134 if (ret)
1135 goto fail;
1136
Boris Brezillon65101d82018-01-12 10:09:26 +01001137 if (args->perfmonid) {
1138 exec->perfmon = vc4_perfmon_find(vc4file,
1139 args->perfmonid);
1140 if (!exec->perfmon) {
1141 ret = -ENOENT;
1142 goto fail;
1143 }
1144 }
1145
Eric Anholtd5b1a782015-11-30 12:13:37 -08001146 if (exec->args->bin_cl_size != 0) {
1147 ret = vc4_get_bcl(dev, exec);
1148 if (ret)
1149 goto fail;
1150 } else {
1151 exec->ct0ca = 0;
1152 exec->ct0ea = 0;
1153 }
1154
1155 ret = vc4_get_rcl(dev, exec);
1156 if (ret)
1157 goto fail;
1158
Eric Anholtcdec4d32017-04-12 12:12:02 -07001159 ret = vc4_lock_bo_reservations(dev, exec, &acquire_ctx);
1160 if (ret)
1161 goto fail;
1162
Eric Anholtd5b1a782015-11-30 12:13:37 -08001163 /* Clear this out of the struct we'll be putting in the queue,
1164 * since it's part of our stack.
1165 */
1166 exec->args = NULL;
1167
Eric Anholtcdec4d32017-04-12 12:12:02 -07001168 ret = vc4_queue_submit(dev, exec, &acquire_ctx);
1169 if (ret)
1170 goto fail;
Eric Anholtd5b1a782015-11-30 12:13:37 -08001171
1172 /* Return the seqno for our job. */
1173 args->seqno = vc4->emit_seqno;
1174
1175 return 0;
1176
1177fail:
1178 vc4_complete_exec(vc4->dev, exec);
1179
1180 return ret;
1181}
1182
1183void
1184vc4_gem_init(struct drm_device *dev)
1185{
1186 struct vc4_dev *vc4 = to_vc4_dev(dev);
1187
Eric Anholtcdec4d32017-04-12 12:12:02 -07001188 vc4->dma_fence_context = dma_fence_context_alloc(1);
1189
Varad Gautamca26d282016-02-17 19:08:21 +05301190 INIT_LIST_HEAD(&vc4->bin_job_list);
1191 INIT_LIST_HEAD(&vc4->render_job_list);
Eric Anholtd5b1a782015-11-30 12:13:37 -08001192 INIT_LIST_HEAD(&vc4->job_done_list);
Eric Anholtb501bac2015-11-30 12:34:01 -08001193 INIT_LIST_HEAD(&vc4->seqno_cb_list);
Eric Anholtd5b1a782015-11-30 12:13:37 -08001194 spin_lock_init(&vc4->job_lock);
1195
1196 INIT_WORK(&vc4->hangcheck.reset_work, vc4_reset_work);
Kees Cook00787302017-10-24 08:16:48 -07001197 timer_setup(&vc4->hangcheck.timer, vc4_hangcheck_elapsed, 0);
Eric Anholtd5b1a782015-11-30 12:13:37 -08001198
1199 INIT_WORK(&vc4->job_done_work, vc4_job_done_work);
Eric Anholt36cb6252016-02-08 12:59:02 -08001200
1201 mutex_init(&vc4->power_lock);
Boris Brezillonb9f19252017-10-19 14:57:48 +02001202
1203 INIT_LIST_HEAD(&vc4->purgeable.list);
1204 mutex_init(&vc4->purgeable.lock);
Eric Anholtd5b1a782015-11-30 12:13:37 -08001205}
1206
1207void
1208vc4_gem_destroy(struct drm_device *dev)
1209{
1210 struct vc4_dev *vc4 = to_vc4_dev(dev);
1211
1212 /* Waiting for exec to finish would need to be done before
1213 * unregistering V3D.
1214 */
1215 WARN_ON(vc4->emit_seqno != vc4->finished_seqno);
1216
1217 /* V3D should already have disabled its interrupt and cleared
1218 * the overflow allocation registers. Now free the object.
1219 */
Eric Anholt553c9422017-03-27 16:10:25 -07001220 if (vc4->bin_bo) {
1221 drm_gem_object_put_unlocked(&vc4->bin_bo->base.base);
1222 vc4->bin_bo = NULL;
Eric Anholtd5b1a782015-11-30 12:13:37 -08001223 }
1224
Eric Anholt21461362015-10-30 10:09:02 -07001225 if (vc4->hang_state)
1226 vc4_free_hang_state(dev, vc4->hang_state);
Eric Anholtd5b1a782015-11-30 12:13:37 -08001227}
Boris Brezillonb9f19252017-10-19 14:57:48 +02001228
1229int vc4_gem_madvise_ioctl(struct drm_device *dev, void *data,
1230 struct drm_file *file_priv)
1231{
1232 struct drm_vc4_gem_madvise *args = data;
1233 struct drm_gem_object *gem_obj;
1234 struct vc4_bo *bo;
1235 int ret;
1236
1237 switch (args->madv) {
1238 case VC4_MADV_DONTNEED:
1239 case VC4_MADV_WILLNEED:
1240 break;
1241 default:
1242 return -EINVAL;
1243 }
1244
1245 if (args->pad != 0)
1246 return -EINVAL;
1247
1248 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
1249 if (!gem_obj) {
1250 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
1251 return -ENOENT;
1252 }
1253
1254 bo = to_vc4_bo(gem_obj);
1255
1256 /* Only BOs exposed to userspace can be purged. */
1257 if (bo->madv == __VC4_MADV_NOTSUPP) {
1258 DRM_DEBUG("madvise not supported on this BO\n");
1259 ret = -EINVAL;
1260 goto out_put_gem;
1261 }
1262
1263 /* Not sure it's safe to purge imported BOs. Let's just assume it's
1264 * not until proven otherwise.
1265 */
1266 if (gem_obj->import_attach) {
1267 DRM_DEBUG("madvise not supported on imported BOs\n");
1268 ret = -EINVAL;
1269 goto out_put_gem;
1270 }
1271
1272 mutex_lock(&bo->madv_lock);
1273
1274 if (args->madv == VC4_MADV_DONTNEED && bo->madv == VC4_MADV_WILLNEED &&
1275 !refcount_read(&bo->usecnt)) {
1276 /* If the BO is about to be marked as purgeable, is not used
1277 * and is not already purgeable or purged, add it to the
1278 * purgeable list.
1279 */
1280 vc4_bo_add_to_purgeable_pool(bo);
1281 } else if (args->madv == VC4_MADV_WILLNEED &&
1282 bo->madv == VC4_MADV_DONTNEED &&
1283 !refcount_read(&bo->usecnt)) {
1284 /* The BO has not been purged yet, just remove it from
1285 * the purgeable list.
1286 */
1287 vc4_bo_remove_from_purgeable_pool(bo);
1288 }
1289
1290 /* Save the purged state. */
1291 args->retained = bo->madv != __VC4_MADV_PURGED;
1292
1293 /* Update internal madv state only if the bo was not purged. */
1294 if (bo->madv != __VC4_MADV_PURGED)
1295 bo->madv = args->madv;
1296
1297 mutex_unlock(&bo->madv_lock);
1298
1299 ret = 0;
1300
1301out_put_gem:
1302 drm_gem_object_put_unlocked(gem_obj);
1303
1304 return ret;
1305}