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