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