xref: /linux/drivers/gpu/drm/vc4/vc4_gem.c (revision 5b0764a53235d3c77e1ed674e836e1b3fe1a4b1f)
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 	if (out_sync)
656 		drm_syncobj_replace_fence(out_sync, exec->fence);
657 
658 	vc4_attach_fences(exec);
659 
660 	drm_exec_fini(exec_ctx);
661 
662 	list_add_tail(&exec->head, &vc4->bin_job_list);
663 
664 	/* If no bin job was executing and if the render job (if any) has the
665 	 * same perfmon as our job attached to it (or if both jobs don't have
666 	 * perfmon activated), then kick ours off.  Otherwise, it'll get
667 	 * started when the previous job's flush/render done interrupt occurs.
668 	 */
669 	renderjob = vc4_first_render_job(vc4);
670 	if (vc4_first_bin_job(vc4) == exec &&
671 	    (!renderjob || renderjob->perfmon == exec->perfmon)) {
672 		vc4_submit_next_bin_job(dev);
673 		vc4_queue_hangcheck(dev);
674 	}
675 
676 	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
677 
678 	return 0;
679 }
680 
681 /**
682  * vc4_cl_lookup_bos() - Sets up exec->bo[] with the GEM objects
683  * referenced by the job.
684  * @dev: DRM device
685  * @file_priv: DRM file for this fd
686  * @exec: V3D job being set up
687  *
688  * The command validator needs to reference BOs by their index within
689  * the submitted job's BO list.  This does the validation of the job's
690  * BO list and reference counting for the lifetime of the job.
691  */
692 static int
693 vc4_cl_lookup_bos(struct drm_device *dev,
694 		  struct drm_file *file_priv,
695 		  struct vc4_exec_info *exec)
696 {
697 	struct drm_vc4_submit_cl *args = exec->args;
698 	int ret = 0;
699 	int i;
700 
701 	exec->bo_count = args->bo_handle_count;
702 
703 	if (!exec->bo_count) {
704 		/* See comment on bo_index for why we have to check
705 		 * this.
706 		 */
707 		DRM_DEBUG("Rendering requires BOs to validate\n");
708 		return -EINVAL;
709 	}
710 
711 	ret = drm_gem_objects_lookup(file_priv, u64_to_user_ptr(args->bo_handles),
712 				     exec->bo_count, &exec->bo);
713 
714 	if (ret)
715 		goto fail_put_bo;
716 
717 	for (i = 0; i < exec->bo_count; i++) {
718 		ret = vc4_bo_inc_usecnt(to_vc4_bo(exec->bo[i]));
719 		if (ret)
720 			goto fail_dec_usecnt;
721 	}
722 
723 	return 0;
724 
725 fail_dec_usecnt:
726 	/* Decrease usecnt on acquired objects.
727 	 * We cannot rely on  vc4_complete_exec() to release resources here,
728 	 * because vc4_complete_exec() has no information about which BO has
729 	 * had its ->usecnt incremented.
730 	 * To make things easier we just free everything explicitly and set
731 	 * exec->bo to NULL so that vc4_complete_exec() skips the 'BO release'
732 	 * step.
733 	 */
734 	for (i-- ; i >= 0; i--)
735 		vc4_bo_dec_usecnt(to_vc4_bo(exec->bo[i]));
736 
737 fail_put_bo:
738 	/* Release any reference to acquired objects. */
739 	for (i = 0; i < exec->bo_count && exec->bo[i]; i++)
740 		drm_gem_object_put(exec->bo[i]);
741 
742 	kvfree(exec->bo);
743 	exec->bo = NULL;
744 	return ret;
745 }
746 
747 static int
748 vc4_get_bcl(struct drm_device *dev, struct vc4_exec_info *exec)
749 {
750 	struct drm_vc4_submit_cl *args = exec->args;
751 	struct vc4_dev *vc4 = to_vc4_dev(dev);
752 	void *temp = NULL;
753 	void *bin;
754 	int ret = 0;
755 	uint32_t bin_offset = 0;
756 	uint32_t shader_rec_offset = roundup(bin_offset + args->bin_cl_size,
757 					     16);
758 	uint32_t uniforms_offset = shader_rec_offset + args->shader_rec_size;
759 	uint32_t exec_size = uniforms_offset + args->uniforms_size;
760 	uint32_t temp_size = exec_size + (sizeof(struct vc4_shader_state) *
761 					  args->shader_rec_count);
762 	struct vc4_bo *bo;
763 
764 	if (shader_rec_offset < args->bin_cl_size ||
765 	    uniforms_offset < shader_rec_offset ||
766 	    exec_size < uniforms_offset ||
767 	    args->shader_rec_count >= (UINT_MAX /
768 					  sizeof(struct vc4_shader_state)) ||
769 	    temp_size < exec_size) {
770 		DRM_DEBUG("overflow in exec arguments\n");
771 		ret = -EINVAL;
772 		goto fail;
773 	}
774 
775 	/* Allocate space where we'll store the copied in user command lists
776 	 * and shader records.
777 	 *
778 	 * We don't just copy directly into the BOs because we need to
779 	 * read the contents back for validation, and I think the
780 	 * bo->vaddr is uncached access.
781 	 */
782 	temp = kvmalloc_array(temp_size, 1, GFP_KERNEL);
783 	if (!temp) {
784 		drm_err(dev, "Failed to allocate storage for copying "
785 			"in bin/render CLs.\n");
786 		ret = -ENOMEM;
787 		goto fail;
788 	}
789 	bin = temp + bin_offset;
790 	exec->shader_rec_u = temp + shader_rec_offset;
791 	exec->uniforms_u = temp + uniforms_offset;
792 	exec->shader_state = temp + exec_size;
793 	exec->shader_state_size = args->shader_rec_count;
794 
795 	if (copy_from_user(bin,
796 			   u64_to_user_ptr(args->bin_cl),
797 			   args->bin_cl_size)) {
798 		ret = -EFAULT;
799 		goto fail;
800 	}
801 
802 	if (copy_from_user(exec->shader_rec_u,
803 			   u64_to_user_ptr(args->shader_rec),
804 			   args->shader_rec_size)) {
805 		ret = -EFAULT;
806 		goto fail;
807 	}
808 
809 	if (copy_from_user(exec->uniforms_u,
810 			   u64_to_user_ptr(args->uniforms),
811 			   args->uniforms_size)) {
812 		ret = -EFAULT;
813 		goto fail;
814 	}
815 
816 	bo = vc4_bo_create(dev, exec_size, true, VC4_BO_TYPE_BCL);
817 	if (IS_ERR(bo)) {
818 		drm_err(dev, "Couldn't allocate BO for binning\n");
819 		ret = PTR_ERR(bo);
820 		goto fail;
821 	}
822 	exec->exec_bo = &bo->base;
823 
824 	list_add_tail(&to_vc4_bo(&exec->exec_bo->base)->unref_head,
825 		      &exec->unref_list);
826 
827 	exec->ct0ca = exec->exec_bo->dma_addr + bin_offset;
828 
829 	exec->bin_u = bin;
830 
831 	exec->shader_rec_v = exec->exec_bo->vaddr + shader_rec_offset;
832 	exec->shader_rec_p = exec->exec_bo->dma_addr + shader_rec_offset;
833 	exec->shader_rec_size = args->shader_rec_size;
834 
835 	exec->uniforms_v = exec->exec_bo->vaddr + uniforms_offset;
836 	exec->uniforms_p = exec->exec_bo->dma_addr + uniforms_offset;
837 	exec->uniforms_size = args->uniforms_size;
838 
839 	ret = vc4_validate_bin_cl(dev,
840 				  exec->exec_bo->vaddr + bin_offset,
841 				  bin,
842 				  exec);
843 	if (ret)
844 		goto fail;
845 
846 	ret = vc4_validate_shader_recs(dev, exec);
847 	if (ret)
848 		goto fail;
849 
850 	if (exec->found_tile_binning_mode_config_packet) {
851 		ret = vc4_v3d_bin_bo_get(vc4, &exec->bin_bo_used);
852 		if (ret)
853 			goto fail;
854 	}
855 
856 fail:
857 	kvfree(temp);
858 	return ret;
859 }
860 
861 static void
862 vc4_complete_exec(struct drm_device *dev, struct vc4_exec_info *exec)
863 {
864 	struct vc4_dev *vc4 = to_vc4_dev(dev);
865 	unsigned long irqflags;
866 	unsigned i;
867 
868 	/* If we got force-completed because of GPU reset rather than
869 	 * through our IRQ handler, signal the fence now.
870 	 */
871 	if (exec->fence) {
872 		dma_fence_signal(exec->fence);
873 		dma_fence_put(exec->fence);
874 	}
875 
876 	if (exec->bo) {
877 		for (i = 0; i < exec->bo_count; i++) {
878 			struct vc4_bo *bo = to_vc4_bo(exec->bo[i]);
879 
880 			vc4_bo_dec_usecnt(bo);
881 			drm_gem_object_put(exec->bo[i]);
882 		}
883 		kvfree(exec->bo);
884 	}
885 
886 	while (!list_empty(&exec->unref_list)) {
887 		struct vc4_bo *bo = list_first_entry(&exec->unref_list,
888 						     struct vc4_bo, unref_head);
889 		list_del(&bo->unref_head);
890 		drm_gem_object_put(&bo->base.base);
891 	}
892 
893 	/* Free up the allocation of any bin slots we used. Jobs that ran to
894 	 * completion had their slots released in vc4_irq_finish_render_job().
895 	 * Only jobs that never completed still have slots to be released here.
896 	 */
897 	spin_lock_irqsave(&vc4->job_lock, irqflags);
898 	vc4->bin_alloc_used &= ~exec->bin_slots;
899 	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
900 
901 	/* Let anyone waiting on the binner pool retry. */
902 	wake_up_all(&vc4->job_wait_queue);
903 
904 	/* Release the reference on the binner BO if needed. */
905 	if (exec->bin_bo_used)
906 		vc4_v3d_bin_bo_put(vc4);
907 
908 	/* Release the reference we had on the perf monitor. */
909 	vc4_perfmon_put(exec->perfmon);
910 
911 	vc4_v3d_pm_put(vc4);
912 
913 	kfree(exec);
914 }
915 
916 void
917 vc4_job_handle_completed(struct vc4_dev *vc4)
918 {
919 	unsigned long irqflags;
920 
921 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
922 		return;
923 
924 	spin_lock_irqsave(&vc4->job_lock, irqflags);
925 	while (!list_empty(&vc4->job_done_list)) {
926 		struct vc4_exec_info *exec =
927 			list_first_entry(&vc4->job_done_list,
928 					 struct vc4_exec_info, head);
929 		list_del(&exec->head);
930 
931 		spin_unlock_irqrestore(&vc4->job_lock, irqflags);
932 		vc4_complete_exec(&vc4->base, exec);
933 		spin_lock_irqsave(&vc4->job_lock, irqflags);
934 	}
935 
936 	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
937 }
938 
939 /* Scheduled when any job has been completed, this walks the list of
940  * jobs that had completed and unrefs their BOs and frees their exec
941  * structs.
942  */
943 static void
944 vc4_job_done_work(struct work_struct *work)
945 {
946 	struct vc4_dev *vc4 =
947 		container_of(work, struct vc4_dev, job_done_work);
948 
949 	vc4_job_handle_completed(vc4);
950 }
951 
952 static int
953 vc4_wait_for_seqno_ioctl_helper(struct drm_device *dev,
954 				uint64_t seqno,
955 				uint64_t *timeout_ns)
956 {
957 	unsigned long start = jiffies;
958 	int ret = vc4_wait_for_seqno(dev, seqno, *timeout_ns, true);
959 
960 	if ((ret == -EINTR || ret == -ERESTARTSYS) && *timeout_ns != ~0ull) {
961 		uint64_t delta = jiffies_to_nsecs(jiffies - start);
962 
963 		if (*timeout_ns >= delta)
964 			*timeout_ns -= delta;
965 	}
966 
967 	return ret;
968 }
969 
970 int
971 vc4_wait_seqno_ioctl(struct drm_device *dev, void *data,
972 		     struct drm_file *file_priv)
973 {
974 	struct vc4_dev *vc4 = to_vc4_dev(dev);
975 	struct drm_vc4_wait_seqno *args = data;
976 
977 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
978 		return -ENODEV;
979 
980 	return vc4_wait_for_seqno_ioctl_helper(dev, args->seqno,
981 					       &args->timeout_ns);
982 }
983 
984 int
985 vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
986 		  struct drm_file *file_priv)
987 {
988 	struct vc4_dev *vc4 = to_vc4_dev(dev);
989 	int ret;
990 	struct drm_vc4_wait_bo *args = data;
991 	unsigned long timeout_jiffies =
992 		drm_timeout_rel_to_jiffies(args->timeout_ns);
993 	ktime_t start = ktime_get();
994 	u64 delta_ns;
995 
996 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
997 		return -ENODEV;
998 
999 	if (args->pad != 0)
1000 		return -EINVAL;
1001 
1002 	ret = drm_gem_dma_resv_wait(file_priv, args->handle,
1003 				    true, timeout_jiffies);
1004 
1005 	/* Decrement the user's timeout, in case we got interrupted
1006 	 * such that the ioctl will be restarted.
1007 	 */
1008 	delta_ns = ktime_to_ns(ktime_sub(ktime_get(), start));
1009 	if (delta_ns < args->timeout_ns)
1010 		args->timeout_ns -= delta_ns;
1011 	else
1012 		args->timeout_ns = 0;
1013 
1014 	return ret;
1015 }
1016 
1017 /**
1018  * vc4_submit_cl_ioctl() - Submits a job (frame) to the VC4.
1019  * @dev: DRM device
1020  * @data: ioctl argument
1021  * @file_priv: DRM file for this fd
1022  *
1023  * This is the main entrypoint for userspace to submit a 3D frame to
1024  * the GPU.  Userspace provides the binner command list (if
1025  * applicable), and the kernel sets up the render command list to draw
1026  * to the framebuffer described in the ioctl, using the command lists
1027  * that the 3D engine's binner will produce.
1028  */
1029 int
1030 vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
1031 		    struct drm_file *file_priv)
1032 {
1033 	struct vc4_dev *vc4 = to_vc4_dev(dev);
1034 	struct vc4_file *vc4file = file_priv->driver_priv;
1035 	struct drm_vc4_submit_cl *args = data;
1036 	struct drm_syncobj *out_sync = NULL;
1037 	struct vc4_exec_info *exec;
1038 	struct drm_exec exec_ctx;
1039 	struct dma_fence *in_fence;
1040 	int ret = 0;
1041 
1042 	trace_vc4_submit_cl_ioctl(dev, args->bin_cl_size,
1043 				  args->shader_rec_size,
1044 				  args->bo_handle_count);
1045 
1046 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
1047 		return -ENODEV;
1048 
1049 	if (!vc4->v3d) {
1050 		DRM_DEBUG("VC4_SUBMIT_CL with no VC4 V3D probed\n");
1051 		return -ENODEV;
1052 	}
1053 
1054 	if ((args->flags & ~(VC4_SUBMIT_CL_USE_CLEAR_COLOR |
1055 			     VC4_SUBMIT_CL_FIXED_RCL_ORDER |
1056 			     VC4_SUBMIT_CL_RCL_ORDER_INCREASING_X |
1057 			     VC4_SUBMIT_CL_RCL_ORDER_INCREASING_Y)) != 0) {
1058 		DRM_DEBUG("Unknown flags: 0x%02x\n", args->flags);
1059 		return -EINVAL;
1060 	}
1061 
1062 	if (args->pad2 != 0) {
1063 		DRM_DEBUG("Invalid pad: 0x%08x\n", args->pad2);
1064 		return -EINVAL;
1065 	}
1066 
1067 	exec = kzalloc_objs(*exec, 1);
1068 	if (!exec)
1069 		return -ENOMEM;
1070 
1071 	exec->dev = vc4;
1072 
1073 	ret = vc4_v3d_pm_get(vc4);
1074 	if (ret) {
1075 		kfree(exec);
1076 		return ret;
1077 	}
1078 
1079 	exec->args = args;
1080 	INIT_LIST_HEAD(&exec->unref_list);
1081 
1082 	ret = vc4_cl_lookup_bos(dev, file_priv, exec);
1083 	if (ret)
1084 		goto fail;
1085 
1086 	if (args->perfmonid) {
1087 		exec->perfmon = vc4_perfmon_find(vc4file,
1088 						 args->perfmonid);
1089 		if (!exec->perfmon) {
1090 			ret = -ENOENT;
1091 			goto fail;
1092 		}
1093 	}
1094 
1095 	if (args->in_sync) {
1096 		ret = drm_syncobj_find_fence(file_priv, args->in_sync,
1097 					     0, 0, &in_fence);
1098 		if (ret)
1099 			goto fail;
1100 
1101 		/* When the fence (or fence array) is exclusively from our
1102 		 * context we can skip the wait since jobs are executed in
1103 		 * order of their submission through this ioctl and this can
1104 		 * only have fences from a prior job.
1105 		 */
1106 		if (!dma_fence_match_context(in_fence,
1107 					     vc4->dma_fence_context)) {
1108 			ret = dma_fence_wait(in_fence, true);
1109 			if (ret) {
1110 				dma_fence_put(in_fence);
1111 				goto fail;
1112 			}
1113 		}
1114 
1115 		dma_fence_put(in_fence);
1116 	}
1117 
1118 	if (exec->args->bin_cl_size != 0) {
1119 		ret = vc4_get_bcl(dev, exec);
1120 		if (ret)
1121 			goto fail;
1122 	} else {
1123 		exec->ct0ca = 0;
1124 		exec->ct0ea = 0;
1125 	}
1126 
1127 	ret = vc4_get_rcl(dev, exec);
1128 	if (ret)
1129 		goto fail;
1130 
1131 	ret = vc4_lock_bo_reservations(exec, &exec_ctx);
1132 	if (ret)
1133 		goto fail;
1134 
1135 	if (args->out_sync) {
1136 		out_sync = drm_syncobj_find(file_priv, args->out_sync);
1137 		if (!out_sync) {
1138 			ret = -EINVAL;
1139 			goto fail_unreserve;
1140 		}
1141 
1142 		/* We replace the fence in out_sync in vc4_queue_submit since
1143 		 * the render job could execute immediately after that call.
1144 		 * If it finishes before our ioctl processing resumes the
1145 		 * render job fence could already have been freed.
1146 		 */
1147 	}
1148 
1149 	/* Clear this out of the struct we'll be putting in the queue,
1150 	 * since it's part of our stack.
1151 	 */
1152 	exec->args = NULL;
1153 
1154 	ret = vc4_queue_submit(dev, exec, &exec_ctx, out_sync);
1155 
1156 	/* The syncobj isn't part of the exec data and we need to free our
1157 	 * reference even if job submission failed.
1158 	 */
1159 	if (out_sync)
1160 		drm_syncobj_put(out_sync);
1161 
1162 	if (ret)
1163 		goto fail_unreserve;
1164 
1165 	/* Return the seqno for our job. */
1166 	args->seqno = vc4->emit_seqno;
1167 
1168 	return 0;
1169 
1170 fail_unreserve:
1171 	drm_exec_fini(&exec_ctx);
1172 fail:
1173 	vc4_complete_exec(&vc4->base, exec);
1174 
1175 	return ret;
1176 }
1177 
1178 static void vc4_gem_destroy(struct drm_device *dev, void *unused);
1179 int vc4_gem_init(struct drm_device *dev)
1180 {
1181 	struct vc4_dev *vc4 = to_vc4_dev(dev);
1182 	int ret;
1183 
1184 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
1185 		return -ENODEV;
1186 
1187 	vc4->dma_fence_context = dma_fence_context_alloc(1);
1188 
1189 	INIT_LIST_HEAD(&vc4->bin_job_list);
1190 	INIT_LIST_HEAD(&vc4->render_job_list);
1191 	INIT_LIST_HEAD(&vc4->job_done_list);
1192 	spin_lock_init(&vc4->job_lock);
1193 
1194 	INIT_WORK(&vc4->hangcheck.reset_work, vc4_reset_work);
1195 	timer_setup(&vc4->hangcheck.timer, vc4_hangcheck_elapsed, 0);
1196 
1197 	INIT_WORK(&vc4->job_done_work, vc4_job_done_work);
1198 
1199 	INIT_LIST_HEAD(&vc4->purgeable.list);
1200 
1201 	ret = drmm_mutex_init(dev, &vc4->purgeable.lock);
1202 	if (ret)
1203 		return ret;
1204 
1205 	return drmm_add_action_or_reset(dev, vc4_gem_destroy, NULL);
1206 }
1207 
1208 static void vc4_gem_destroy(struct drm_device *dev, void *unused)
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 	 */
1220 	if (vc4->bin_bo) {
1221 		drm_gem_object_put(&vc4->bin_bo->base.base);
1222 		vc4->bin_bo = NULL;
1223 	}
1224 
1225 	if (vc4->hang_state)
1226 		vc4_free_hang_state(dev, vc4->hang_state);
1227 }
1228 
1229 int vc4_gem_madvise_ioctl(struct drm_device *dev, void *data,
1230 			  struct drm_file *file_priv)
1231 {
1232 	struct vc4_dev *vc4 = to_vc4_dev(dev);
1233 	struct drm_vc4_gem_madvise *args = data;
1234 	struct drm_gem_object *gem_obj;
1235 	struct vc4_bo *bo;
1236 	int ret;
1237 
1238 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
1239 		return -ENODEV;
1240 
1241 	switch (args->madv) {
1242 	case VC4_MADV_DONTNEED:
1243 	case VC4_MADV_WILLNEED:
1244 		break;
1245 	default:
1246 		return -EINVAL;
1247 	}
1248 
1249 	if (args->pad != 0)
1250 		return -EINVAL;
1251 
1252 	gem_obj = drm_gem_object_lookup(file_priv, args->handle);
1253 	if (!gem_obj) {
1254 		DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
1255 		return -ENOENT;
1256 	}
1257 
1258 	bo = to_vc4_bo(gem_obj);
1259 
1260 	/* Only BOs exposed to userspace can be purged. */
1261 	if (bo->madv == __VC4_MADV_NOTSUPP) {
1262 		DRM_DEBUG("madvise not supported on this BO\n");
1263 		ret = -EINVAL;
1264 		goto out_put_gem;
1265 	}
1266 
1267 	/* Not sure it's safe to purge imported BOs. Let's just assume it's
1268 	 * not until proven otherwise.
1269 	 */
1270 	if (drm_gem_is_imported(gem_obj)) {
1271 		DRM_DEBUG("madvise not supported on imported BOs\n");
1272 		ret = -EINVAL;
1273 		goto out_put_gem;
1274 	}
1275 
1276 	mutex_lock(&bo->madv_lock);
1277 
1278 	if (args->madv == VC4_MADV_DONTNEED && bo->madv == VC4_MADV_WILLNEED &&
1279 	    !refcount_read(&bo->usecnt)) {
1280 		/* If the BO is about to be marked as purgeable, is not used
1281 		 * and is not already purgeable or purged, add it to the
1282 		 * purgeable list.
1283 		 */
1284 		vc4_bo_add_to_purgeable_pool(bo);
1285 	} else if (args->madv == VC4_MADV_WILLNEED &&
1286 		   bo->madv == VC4_MADV_DONTNEED &&
1287 		   !refcount_read(&bo->usecnt)) {
1288 		/* The BO has not been purged yet, just remove it from
1289 		 * the purgeable list.
1290 		 */
1291 		vc4_bo_remove_from_purgeable_pool(bo);
1292 	}
1293 
1294 	/* Save the purged state. */
1295 	args->retained = bo->madv != __VC4_MADV_PURGED;
1296 
1297 	/* Update internal madv state only if the bo was not purged. */
1298 	if (bo->madv != __VC4_MADV_PURGED)
1299 		bo->madv = args->madv;
1300 
1301 	mutex_unlock(&bo->madv_lock);
1302 
1303 	ret = 0;
1304 
1305 out_put_gem:
1306 	drm_gem_object_put(gem_obj);
1307 
1308 	return ret;
1309 }
1310