xref: /openbmc/linux/drivers/gpu/drm/ttm/ttm_bo.c (revision 56cb61f7)
1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
3  *
4  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sub license, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial portions
17  * of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25  * USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  **************************************************************************/
28 /*
29  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
30  */
31 
32 #define pr_fmt(fmt) "[TTM] " fmt
33 
34 #include <drm/ttm/ttm_bo_driver.h>
35 #include <drm/ttm/ttm_placement.h>
36 #include <linux/jiffies.h>
37 #include <linux/slab.h>
38 #include <linux/sched.h>
39 #include <linux/mm.h>
40 #include <linux/file.h>
41 #include <linux/module.h>
42 #include <linux/atomic.h>
43 #include <linux/dma-resv.h>
44 
45 #include "ttm_module.h"
46 
47 /* default destructor */
48 static void ttm_bo_default_destroy(struct ttm_buffer_object *bo)
49 {
50 	kfree(bo);
51 }
52 
53 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
54 					struct ttm_placement *placement)
55 {
56 	struct drm_printer p = drm_debug_printer(TTM_PFX);
57 	struct ttm_resource_manager *man;
58 	int i, mem_type;
59 
60 	drm_printf(&p, "No space for %p (%lu pages, %zuK, %zuM)\n",
61 		   bo, bo->resource->num_pages, bo->base.size >> 10,
62 		   bo->base.size >> 20);
63 	for (i = 0; i < placement->num_placement; i++) {
64 		mem_type = placement->placement[i].mem_type;
65 		drm_printf(&p, "  placement[%d]=0x%08X (%d)\n",
66 			   i, placement->placement[i].flags, mem_type);
67 		man = ttm_manager_type(bo->bdev, mem_type);
68 		ttm_resource_manager_debug(man, &p);
69 	}
70 }
71 
72 static inline void ttm_bo_move_to_pinned(struct ttm_buffer_object *bo)
73 {
74 	struct ttm_device *bdev = bo->bdev;
75 
76 	list_move_tail(&bo->lru, &bdev->pinned);
77 
78 	if (bdev->funcs->del_from_lru_notify)
79 		bdev->funcs->del_from_lru_notify(bo);
80 }
81 
82 static inline void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
83 {
84 	struct ttm_device *bdev = bo->bdev;
85 
86 	list_del_init(&bo->lru);
87 
88 	if (bdev->funcs->del_from_lru_notify)
89 		bdev->funcs->del_from_lru_notify(bo);
90 }
91 
92 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
93 				     struct ttm_buffer_object *bo)
94 {
95 	if (!pos->first)
96 		pos->first = bo;
97 	pos->last = bo;
98 }
99 
100 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
101 			     struct ttm_resource *mem,
102 			     struct ttm_lru_bulk_move *bulk)
103 {
104 	struct ttm_device *bdev = bo->bdev;
105 	struct ttm_resource_manager *man;
106 
107 	if (!bo->deleted)
108 		dma_resv_assert_held(bo->base.resv);
109 
110 	if (bo->pin_count) {
111 		ttm_bo_move_to_pinned(bo);
112 		return;
113 	}
114 
115 	if (!mem)
116 		return;
117 
118 	man = ttm_manager_type(bdev, mem->mem_type);
119 	list_move_tail(&bo->lru, &man->lru[bo->priority]);
120 
121 	if (bdev->funcs->del_from_lru_notify)
122 		bdev->funcs->del_from_lru_notify(bo);
123 
124 	if (bulk && !bo->pin_count) {
125 		switch (bo->resource->mem_type) {
126 		case TTM_PL_TT:
127 			ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
128 			break;
129 
130 		case TTM_PL_VRAM:
131 			ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
132 			break;
133 		}
134 	}
135 }
136 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
137 
138 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
139 {
140 	unsigned i;
141 
142 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
143 		struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
144 		struct ttm_resource_manager *man;
145 
146 		if (!pos->first)
147 			continue;
148 
149 		dma_resv_assert_held(pos->first->base.resv);
150 		dma_resv_assert_held(pos->last->base.resv);
151 
152 		man = ttm_manager_type(pos->first->bdev, TTM_PL_TT);
153 		list_bulk_move_tail(&man->lru[i], &pos->first->lru,
154 				    &pos->last->lru);
155 	}
156 
157 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
158 		struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
159 		struct ttm_resource_manager *man;
160 
161 		if (!pos->first)
162 			continue;
163 
164 		dma_resv_assert_held(pos->first->base.resv);
165 		dma_resv_assert_held(pos->last->base.resv);
166 
167 		man = ttm_manager_type(pos->first->bdev, TTM_PL_VRAM);
168 		list_bulk_move_tail(&man->lru[i], &pos->first->lru,
169 				    &pos->last->lru);
170 	}
171 }
172 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
173 
174 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
175 				  struct ttm_resource *mem, bool evict,
176 				  struct ttm_operation_ctx *ctx,
177 				  struct ttm_place *hop)
178 {
179 	struct ttm_resource_manager *old_man, *new_man;
180 	struct ttm_device *bdev = bo->bdev;
181 	int ret;
182 
183 	old_man = ttm_manager_type(bdev, bo->resource->mem_type);
184 	new_man = ttm_manager_type(bdev, mem->mem_type);
185 
186 	ttm_bo_unmap_virtual(bo);
187 
188 	/*
189 	 * Create and bind a ttm if required.
190 	 */
191 
192 	if (new_man->use_tt) {
193 		/* Zero init the new TTM structure if the old location should
194 		 * have used one as well.
195 		 */
196 		ret = ttm_tt_create(bo, old_man->use_tt);
197 		if (ret)
198 			goto out_err;
199 
200 		if (mem->mem_type != TTM_PL_SYSTEM) {
201 			ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
202 			if (ret)
203 				goto out_err;
204 		}
205 	}
206 
207 	ret = bdev->funcs->move(bo, evict, ctx, mem, hop);
208 	if (ret) {
209 		if (ret == -EMULTIHOP)
210 			return ret;
211 		goto out_err;
212 	}
213 
214 	ctx->bytes_moved += bo->base.size;
215 	return 0;
216 
217 out_err:
218 	new_man = ttm_manager_type(bdev, bo->resource->mem_type);
219 	if (!new_man->use_tt)
220 		ttm_bo_tt_destroy(bo);
221 
222 	return ret;
223 }
224 
225 /*
226  * Call bo::reserved.
227  * Will release GPU memory type usage on destruction.
228  * This is the place to put in driver specific hooks to release
229  * driver private resources.
230  * Will release the bo::reserved lock.
231  */
232 
233 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
234 {
235 	if (bo->bdev->funcs->delete_mem_notify)
236 		bo->bdev->funcs->delete_mem_notify(bo);
237 
238 	ttm_bo_tt_destroy(bo);
239 	ttm_resource_free(bo, &bo->resource);
240 }
241 
242 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
243 {
244 	int r;
245 
246 	if (bo->base.resv == &bo->base._resv)
247 		return 0;
248 
249 	BUG_ON(!dma_resv_trylock(&bo->base._resv));
250 
251 	r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
252 	dma_resv_unlock(&bo->base._resv);
253 	if (r)
254 		return r;
255 
256 	if (bo->type != ttm_bo_type_sg) {
257 		/* This works because the BO is about to be destroyed and nobody
258 		 * reference it any more. The only tricky case is the trylock on
259 		 * the resv object while holding the lru_lock.
260 		 */
261 		spin_lock(&bo->bdev->lru_lock);
262 		bo->base.resv = &bo->base._resv;
263 		spin_unlock(&bo->bdev->lru_lock);
264 	}
265 
266 	return r;
267 }
268 
269 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
270 {
271 	struct dma_resv *resv = &bo->base._resv;
272 	struct dma_resv_iter cursor;
273 	struct dma_fence *fence;
274 
275 	dma_resv_iter_begin(&cursor, resv, true);
276 	dma_resv_for_each_fence_unlocked(&cursor, fence) {
277 		if (!fence->ops->signaled)
278 			dma_fence_enable_sw_signaling(fence);
279 	}
280 	dma_resv_iter_end(&cursor);
281 }
282 
283 /**
284  * ttm_bo_cleanup_refs
285  * If bo idle, remove from lru lists, and unref.
286  * If not idle, block if possible.
287  *
288  * Must be called with lru_lock and reservation held, this function
289  * will drop the lru lock and optionally the reservation lock before returning.
290  *
291  * @bo:                    The buffer object to clean-up
292  * @interruptible:         Any sleeps should occur interruptibly.
293  * @no_wait_gpu:           Never wait for gpu. Return -EBUSY instead.
294  * @unlock_resv:           Unlock the reservation lock as well.
295  */
296 
297 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
298 			       bool interruptible, bool no_wait_gpu,
299 			       bool unlock_resv)
300 {
301 	struct dma_resv *resv = &bo->base._resv;
302 	int ret;
303 
304 	if (dma_resv_test_signaled(resv, true))
305 		ret = 0;
306 	else
307 		ret = -EBUSY;
308 
309 	if (ret && !no_wait_gpu) {
310 		long lret;
311 
312 		if (unlock_resv)
313 			dma_resv_unlock(bo->base.resv);
314 		spin_unlock(&bo->bdev->lru_lock);
315 
316 		lret = dma_resv_wait_timeout(resv, true, interruptible,
317 					     30 * HZ);
318 
319 		if (lret < 0)
320 			return lret;
321 		else if (lret == 0)
322 			return -EBUSY;
323 
324 		spin_lock(&bo->bdev->lru_lock);
325 		if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
326 			/*
327 			 * We raced, and lost, someone else holds the reservation now,
328 			 * and is probably busy in ttm_bo_cleanup_memtype_use.
329 			 *
330 			 * Even if it's not the case, because we finished waiting any
331 			 * delayed destruction would succeed, so just return success
332 			 * here.
333 			 */
334 			spin_unlock(&bo->bdev->lru_lock);
335 			return 0;
336 		}
337 		ret = 0;
338 	}
339 
340 	if (ret || unlikely(list_empty(&bo->ddestroy))) {
341 		if (unlock_resv)
342 			dma_resv_unlock(bo->base.resv);
343 		spin_unlock(&bo->bdev->lru_lock);
344 		return ret;
345 	}
346 
347 	ttm_bo_move_to_pinned(bo);
348 	list_del_init(&bo->ddestroy);
349 	spin_unlock(&bo->bdev->lru_lock);
350 	ttm_bo_cleanup_memtype_use(bo);
351 
352 	if (unlock_resv)
353 		dma_resv_unlock(bo->base.resv);
354 
355 	ttm_bo_put(bo);
356 
357 	return 0;
358 }
359 
360 /*
361  * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
362  * encountered buffers.
363  */
364 bool ttm_bo_delayed_delete(struct ttm_device *bdev, bool remove_all)
365 {
366 	struct list_head removed;
367 	bool empty;
368 
369 	INIT_LIST_HEAD(&removed);
370 
371 	spin_lock(&bdev->lru_lock);
372 	while (!list_empty(&bdev->ddestroy)) {
373 		struct ttm_buffer_object *bo;
374 
375 		bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
376 				      ddestroy);
377 		list_move_tail(&bo->ddestroy, &removed);
378 		if (!ttm_bo_get_unless_zero(bo))
379 			continue;
380 
381 		if (remove_all || bo->base.resv != &bo->base._resv) {
382 			spin_unlock(&bdev->lru_lock);
383 			dma_resv_lock(bo->base.resv, NULL);
384 
385 			spin_lock(&bdev->lru_lock);
386 			ttm_bo_cleanup_refs(bo, false, !remove_all, true);
387 
388 		} else if (dma_resv_trylock(bo->base.resv)) {
389 			ttm_bo_cleanup_refs(bo, false, !remove_all, true);
390 		} else {
391 			spin_unlock(&bdev->lru_lock);
392 		}
393 
394 		ttm_bo_put(bo);
395 		spin_lock(&bdev->lru_lock);
396 	}
397 	list_splice_tail(&removed, &bdev->ddestroy);
398 	empty = list_empty(&bdev->ddestroy);
399 	spin_unlock(&bdev->lru_lock);
400 
401 	return empty;
402 }
403 
404 static void ttm_bo_release(struct kref *kref)
405 {
406 	struct ttm_buffer_object *bo =
407 	    container_of(kref, struct ttm_buffer_object, kref);
408 	struct ttm_device *bdev = bo->bdev;
409 	int ret;
410 
411 	WARN_ON_ONCE(bo->pin_count);
412 
413 	if (!bo->deleted) {
414 		ret = ttm_bo_individualize_resv(bo);
415 		if (ret) {
416 			/* Last resort, if we fail to allocate memory for the
417 			 * fences block for the BO to become idle
418 			 */
419 			dma_resv_wait_timeout(bo->base.resv, true, false,
420 					      30 * HZ);
421 		}
422 
423 		if (bo->bdev->funcs->release_notify)
424 			bo->bdev->funcs->release_notify(bo);
425 
426 		drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
427 		ttm_mem_io_free(bdev, bo->resource);
428 	}
429 
430 	if (!dma_resv_test_signaled(bo->base.resv, true) ||
431 	    !dma_resv_trylock(bo->base.resv)) {
432 		/* The BO is not idle, resurrect it for delayed destroy */
433 		ttm_bo_flush_all_fences(bo);
434 		bo->deleted = true;
435 
436 		spin_lock(&bo->bdev->lru_lock);
437 
438 		/*
439 		 * Make pinned bos immediately available to
440 		 * shrinkers, now that they are queued for
441 		 * destruction.
442 		 *
443 		 * FIXME: QXL is triggering this. Can be removed when the
444 		 * driver is fixed.
445 		 */
446 		if (bo->pin_count) {
447 			bo->pin_count = 0;
448 			ttm_bo_move_to_lru_tail(bo, bo->resource, NULL);
449 		}
450 
451 		kref_init(&bo->kref);
452 		list_add_tail(&bo->ddestroy, &bdev->ddestroy);
453 		spin_unlock(&bo->bdev->lru_lock);
454 
455 		schedule_delayed_work(&bdev->wq,
456 				      ((HZ / 100) < 1) ? 1 : HZ / 100);
457 		return;
458 	}
459 
460 	spin_lock(&bo->bdev->lru_lock);
461 	ttm_bo_del_from_lru(bo);
462 	list_del(&bo->ddestroy);
463 	spin_unlock(&bo->bdev->lru_lock);
464 
465 	ttm_bo_cleanup_memtype_use(bo);
466 	dma_resv_unlock(bo->base.resv);
467 
468 	atomic_dec(&ttm_glob.bo_count);
469 	dma_fence_put(bo->moving);
470 	bo->destroy(bo);
471 }
472 
473 void ttm_bo_put(struct ttm_buffer_object *bo)
474 {
475 	kref_put(&bo->kref, ttm_bo_release);
476 }
477 EXPORT_SYMBOL(ttm_bo_put);
478 
479 int ttm_bo_lock_delayed_workqueue(struct ttm_device *bdev)
480 {
481 	return cancel_delayed_work_sync(&bdev->wq);
482 }
483 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
484 
485 void ttm_bo_unlock_delayed_workqueue(struct ttm_device *bdev, int resched)
486 {
487 	if (resched)
488 		schedule_delayed_work(&bdev->wq,
489 				      ((HZ / 100) < 1) ? 1 : HZ / 100);
490 }
491 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
492 
493 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
494 				     struct ttm_resource **mem,
495 				     struct ttm_operation_ctx *ctx,
496 				     struct ttm_place *hop)
497 {
498 	struct ttm_placement hop_placement;
499 	struct ttm_resource *hop_mem;
500 	int ret;
501 
502 	hop_placement.num_placement = hop_placement.num_busy_placement = 1;
503 	hop_placement.placement = hop_placement.busy_placement = hop;
504 
505 	/* find space in the bounce domain */
506 	ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
507 	if (ret)
508 		return ret;
509 	/* move to the bounce domain */
510 	ret = ttm_bo_handle_move_mem(bo, hop_mem, false, ctx, NULL);
511 	if (ret) {
512 		ttm_resource_free(bo, &hop_mem);
513 		return ret;
514 	}
515 	return 0;
516 }
517 
518 static int ttm_bo_evict(struct ttm_buffer_object *bo,
519 			struct ttm_operation_ctx *ctx)
520 {
521 	struct ttm_device *bdev = bo->bdev;
522 	struct ttm_resource *evict_mem;
523 	struct ttm_placement placement;
524 	struct ttm_place hop;
525 	int ret = 0;
526 
527 	memset(&hop, 0, sizeof(hop));
528 
529 	dma_resv_assert_held(bo->base.resv);
530 
531 	placement.num_placement = 0;
532 	placement.num_busy_placement = 0;
533 	bdev->funcs->evict_flags(bo, &placement);
534 
535 	if (!placement.num_placement && !placement.num_busy_placement) {
536 		ret = ttm_bo_wait(bo, true, false);
537 		if (ret)
538 			return ret;
539 
540 		/*
541 		 * Since we've already synced, this frees backing store
542 		 * immediately.
543 		 */
544 		return ttm_bo_pipeline_gutting(bo);
545 	}
546 
547 	ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
548 	if (ret) {
549 		if (ret != -ERESTARTSYS) {
550 			pr_err("Failed to find memory space for buffer 0x%p eviction\n",
551 			       bo);
552 			ttm_bo_mem_space_debug(bo, &placement);
553 		}
554 		goto out;
555 	}
556 
557 bounce:
558 	ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
559 	if (ret == -EMULTIHOP) {
560 		ret = ttm_bo_bounce_temp_buffer(bo, &evict_mem, ctx, &hop);
561 		if (ret) {
562 			pr_err("Buffer eviction failed\n");
563 			ttm_resource_free(bo, &evict_mem);
564 			goto out;
565 		}
566 		/* try and move to final place now. */
567 		goto bounce;
568 	}
569 out:
570 	return ret;
571 }
572 
573 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
574 			      const struct ttm_place *place)
575 {
576 	dma_resv_assert_held(bo->base.resv);
577 	if (bo->resource->mem_type == TTM_PL_SYSTEM)
578 		return true;
579 
580 	/* Don't evict this BO if it's outside of the
581 	 * requested placement range
582 	 */
583 	if (place->fpfn >= (bo->resource->start + bo->resource->num_pages) ||
584 	    (place->lpfn && place->lpfn <= bo->resource->start))
585 		return false;
586 
587 	return true;
588 }
589 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
590 
591 /*
592  * Check the target bo is allowable to be evicted or swapout, including cases:
593  *
594  * a. if share same reservation object with ctx->resv, have assumption
595  * reservation objects should already be locked, so not lock again and
596  * return true directly when either the opreation allow_reserved_eviction
597  * or the target bo already is in delayed free list;
598  *
599  * b. Otherwise, trylock it.
600  */
601 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
602 					   struct ttm_operation_ctx *ctx,
603 					   const struct ttm_place *place,
604 					   bool *locked, bool *busy)
605 {
606 	bool ret = false;
607 
608 	if (bo->base.resv == ctx->resv) {
609 		dma_resv_assert_held(bo->base.resv);
610 		if (ctx->allow_res_evict)
611 			ret = true;
612 		*locked = false;
613 		if (busy)
614 			*busy = false;
615 	} else {
616 		ret = dma_resv_trylock(bo->base.resv);
617 		*locked = ret;
618 		if (busy)
619 			*busy = !ret;
620 	}
621 
622 	if (ret && place && (bo->resource->mem_type != place->mem_type ||
623 		!bo->bdev->funcs->eviction_valuable(bo, place))) {
624 		ret = false;
625 		if (*locked) {
626 			dma_resv_unlock(bo->base.resv);
627 			*locked = false;
628 		}
629 	}
630 
631 	return ret;
632 }
633 
634 /**
635  * ttm_mem_evict_wait_busy - wait for a busy BO to become available
636  *
637  * @busy_bo: BO which couldn't be locked with trylock
638  * @ctx: operation context
639  * @ticket: acquire ticket
640  *
641  * Try to lock a busy buffer object to avoid failing eviction.
642  */
643 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
644 				   struct ttm_operation_ctx *ctx,
645 				   struct ww_acquire_ctx *ticket)
646 {
647 	int r;
648 
649 	if (!busy_bo || !ticket)
650 		return -EBUSY;
651 
652 	if (ctx->interruptible)
653 		r = dma_resv_lock_interruptible(busy_bo->base.resv,
654 							  ticket);
655 	else
656 		r = dma_resv_lock(busy_bo->base.resv, ticket);
657 
658 	/*
659 	 * TODO: It would be better to keep the BO locked until allocation is at
660 	 * least tried one more time, but that would mean a much larger rework
661 	 * of TTM.
662 	 */
663 	if (!r)
664 		dma_resv_unlock(busy_bo->base.resv);
665 
666 	return r == -EDEADLK ? -EBUSY : r;
667 }
668 
669 int ttm_mem_evict_first(struct ttm_device *bdev,
670 			struct ttm_resource_manager *man,
671 			const struct ttm_place *place,
672 			struct ttm_operation_ctx *ctx,
673 			struct ww_acquire_ctx *ticket)
674 {
675 	struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
676 	bool locked = false;
677 	unsigned i;
678 	int ret;
679 
680 	spin_lock(&bdev->lru_lock);
681 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
682 		list_for_each_entry(bo, &man->lru[i], lru) {
683 			bool busy;
684 
685 			if (!ttm_bo_evict_swapout_allowable(bo, ctx, place,
686 							    &locked, &busy)) {
687 				if (busy && !busy_bo && ticket !=
688 				    dma_resv_locking_ctx(bo->base.resv))
689 					busy_bo = bo;
690 				continue;
691 			}
692 
693 			if (!ttm_bo_get_unless_zero(bo)) {
694 				if (locked)
695 					dma_resv_unlock(bo->base.resv);
696 				continue;
697 			}
698 			break;
699 		}
700 
701 		/* If the inner loop terminated early, we have our candidate */
702 		if (&bo->lru != &man->lru[i])
703 			break;
704 
705 		bo = NULL;
706 	}
707 
708 	if (!bo) {
709 		if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
710 			busy_bo = NULL;
711 		spin_unlock(&bdev->lru_lock);
712 		ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
713 		if (busy_bo)
714 			ttm_bo_put(busy_bo);
715 		return ret;
716 	}
717 
718 	if (bo->deleted) {
719 		ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
720 					  ctx->no_wait_gpu, locked);
721 		ttm_bo_put(bo);
722 		return ret;
723 	}
724 
725 	spin_unlock(&bdev->lru_lock);
726 
727 	ret = ttm_bo_evict(bo, ctx);
728 	if (locked)
729 		ttm_bo_unreserve(bo);
730 	else
731 		ttm_bo_move_to_lru_tail_unlocked(bo);
732 
733 	ttm_bo_put(bo);
734 	return ret;
735 }
736 
737 /*
738  * Add the last move fence to the BO and reserve a new shared slot. We only use
739  * a shared slot to avoid unecessary sync and rely on the subsequent bo move to
740  * either stall or use an exclusive fence respectively set bo->moving.
741  */
742 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
743 				 struct ttm_resource_manager *man,
744 				 struct ttm_resource *mem,
745 				 bool no_wait_gpu)
746 {
747 	struct dma_fence *fence;
748 	int ret;
749 
750 	spin_lock(&man->move_lock);
751 	fence = dma_fence_get(man->move);
752 	spin_unlock(&man->move_lock);
753 
754 	if (!fence)
755 		return 0;
756 
757 	if (no_wait_gpu) {
758 		ret = dma_fence_is_signaled(fence) ? 0 : -EBUSY;
759 		dma_fence_put(fence);
760 		return ret;
761 	}
762 
763 	dma_resv_add_shared_fence(bo->base.resv, fence);
764 
765 	ret = dma_resv_reserve_shared(bo->base.resv, 1);
766 	if (unlikely(ret)) {
767 		dma_fence_put(fence);
768 		return ret;
769 	}
770 
771 	dma_fence_put(bo->moving);
772 	bo->moving = fence;
773 	return 0;
774 }
775 
776 /*
777  * Repeatedly evict memory from the LRU for @mem_type until we create enough
778  * space, or we've evicted everything and there isn't enough space.
779  */
780 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
781 				  const struct ttm_place *place,
782 				  struct ttm_resource **mem,
783 				  struct ttm_operation_ctx *ctx)
784 {
785 	struct ttm_device *bdev = bo->bdev;
786 	struct ttm_resource_manager *man;
787 	struct ww_acquire_ctx *ticket;
788 	int ret;
789 
790 	man = ttm_manager_type(bdev, place->mem_type);
791 	ticket = dma_resv_locking_ctx(bo->base.resv);
792 	do {
793 		ret = ttm_resource_alloc(bo, place, mem);
794 		if (likely(!ret))
795 			break;
796 		if (unlikely(ret != -ENOSPC))
797 			return ret;
798 		ret = ttm_mem_evict_first(bdev, man, place, ctx,
799 					  ticket);
800 		if (unlikely(ret != 0))
801 			return ret;
802 	} while (1);
803 
804 	return ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
805 }
806 
807 /*
808  * Creates space for memory region @mem according to its type.
809  *
810  * This function first searches for free space in compatible memory types in
811  * the priority order defined by the driver.  If free space isn't found, then
812  * ttm_bo_mem_force_space is attempted in priority order to evict and find
813  * space.
814  */
815 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
816 			struct ttm_placement *placement,
817 			struct ttm_resource **mem,
818 			struct ttm_operation_ctx *ctx)
819 {
820 	struct ttm_device *bdev = bo->bdev;
821 	bool type_found = false;
822 	int i, ret;
823 
824 	ret = dma_resv_reserve_shared(bo->base.resv, 1);
825 	if (unlikely(ret))
826 		return ret;
827 
828 	for (i = 0; i < placement->num_placement; ++i) {
829 		const struct ttm_place *place = &placement->placement[i];
830 		struct ttm_resource_manager *man;
831 
832 		man = ttm_manager_type(bdev, place->mem_type);
833 		if (!man || !ttm_resource_manager_used(man))
834 			continue;
835 
836 		type_found = true;
837 		ret = ttm_resource_alloc(bo, place, mem);
838 		if (ret == -ENOSPC)
839 			continue;
840 		if (unlikely(ret))
841 			goto error;
842 
843 		ret = ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
844 		if (unlikely(ret)) {
845 			ttm_resource_free(bo, mem);
846 			if (ret == -EBUSY)
847 				continue;
848 
849 			goto error;
850 		}
851 		return 0;
852 	}
853 
854 	for (i = 0; i < placement->num_busy_placement; ++i) {
855 		const struct ttm_place *place = &placement->busy_placement[i];
856 		struct ttm_resource_manager *man;
857 
858 		man = ttm_manager_type(bdev, place->mem_type);
859 		if (!man || !ttm_resource_manager_used(man))
860 			continue;
861 
862 		type_found = true;
863 		ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
864 		if (likely(!ret))
865 			return 0;
866 
867 		if (ret && ret != -EBUSY)
868 			goto error;
869 	}
870 
871 	ret = -ENOMEM;
872 	if (!type_found) {
873 		pr_err(TTM_PFX "No compatible memory type found\n");
874 		ret = -EINVAL;
875 	}
876 
877 error:
878 	if (bo->resource->mem_type == TTM_PL_SYSTEM && !bo->pin_count)
879 		ttm_bo_move_to_lru_tail_unlocked(bo);
880 
881 	return ret;
882 }
883 EXPORT_SYMBOL(ttm_bo_mem_space);
884 
885 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
886 			      struct ttm_placement *placement,
887 			      struct ttm_operation_ctx *ctx)
888 {
889 	struct ttm_resource *mem;
890 	struct ttm_place hop;
891 	int ret;
892 
893 	dma_resv_assert_held(bo->base.resv);
894 
895 	/*
896 	 * Determine where to move the buffer.
897 	 *
898 	 * If driver determines move is going to need
899 	 * an extra step then it will return -EMULTIHOP
900 	 * and the buffer will be moved to the temporary
901 	 * stop and the driver will be called to make
902 	 * the second hop.
903 	 */
904 	ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
905 	if (ret)
906 		return ret;
907 bounce:
908 	ret = ttm_bo_handle_move_mem(bo, mem, false, ctx, &hop);
909 	if (ret == -EMULTIHOP) {
910 		ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
911 		if (ret)
912 			goto out;
913 		/* try and move to final place now. */
914 		goto bounce;
915 	}
916 out:
917 	if (ret)
918 		ttm_resource_free(bo, &mem);
919 	return ret;
920 }
921 
922 int ttm_bo_validate(struct ttm_buffer_object *bo,
923 		    struct ttm_placement *placement,
924 		    struct ttm_operation_ctx *ctx)
925 {
926 	int ret;
927 
928 	dma_resv_assert_held(bo->base.resv);
929 
930 	/*
931 	 * Remove the backing store if no placement is given.
932 	 */
933 	if (!placement->num_placement && !placement->num_busy_placement)
934 		return ttm_bo_pipeline_gutting(bo);
935 
936 	/*
937 	 * Check whether we need to move buffer.
938 	 */
939 	if (!ttm_resource_compat(bo->resource, placement)) {
940 		ret = ttm_bo_move_buffer(bo, placement, ctx);
941 		if (ret)
942 			return ret;
943 	}
944 	/*
945 	 * We might need to add a TTM.
946 	 */
947 	if (bo->resource->mem_type == TTM_PL_SYSTEM) {
948 		ret = ttm_tt_create(bo, true);
949 		if (ret)
950 			return ret;
951 	}
952 	return 0;
953 }
954 EXPORT_SYMBOL(ttm_bo_validate);
955 
956 int ttm_bo_init_reserved(struct ttm_device *bdev,
957 			 struct ttm_buffer_object *bo,
958 			 size_t size,
959 			 enum ttm_bo_type type,
960 			 struct ttm_placement *placement,
961 			 uint32_t page_alignment,
962 			 struct ttm_operation_ctx *ctx,
963 			 struct sg_table *sg,
964 			 struct dma_resv *resv,
965 			 void (*destroy) (struct ttm_buffer_object *))
966 {
967 	static const struct ttm_place sys_mem = { .mem_type = TTM_PL_SYSTEM };
968 	bool locked;
969 	int ret;
970 
971 	bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
972 
973 	kref_init(&bo->kref);
974 	INIT_LIST_HEAD(&bo->lru);
975 	INIT_LIST_HEAD(&bo->ddestroy);
976 	bo->bdev = bdev;
977 	bo->type = type;
978 	bo->page_alignment = page_alignment;
979 	bo->moving = NULL;
980 	bo->pin_count = 0;
981 	bo->sg = sg;
982 	if (resv) {
983 		bo->base.resv = resv;
984 		dma_resv_assert_held(bo->base.resv);
985 	} else {
986 		bo->base.resv = &bo->base._resv;
987 	}
988 	atomic_inc(&ttm_glob.bo_count);
989 
990 	ret = ttm_resource_alloc(bo, &sys_mem, &bo->resource);
991 	if (unlikely(ret)) {
992 		ttm_bo_put(bo);
993 		return ret;
994 	}
995 
996 	/*
997 	 * For ttm_bo_type_device buffers, allocate
998 	 * address space from the device.
999 	 */
1000 	if (bo->type == ttm_bo_type_device ||
1001 	    bo->type == ttm_bo_type_sg)
1002 		ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
1003 					 bo->resource->num_pages);
1004 
1005 	/* passed reservation objects should already be locked,
1006 	 * since otherwise lockdep will be angered in radeon.
1007 	 */
1008 	if (!resv) {
1009 		locked = dma_resv_trylock(bo->base.resv);
1010 		WARN_ON(!locked);
1011 	}
1012 
1013 	if (likely(!ret))
1014 		ret = ttm_bo_validate(bo, placement, ctx);
1015 
1016 	if (unlikely(ret)) {
1017 		if (!resv)
1018 			ttm_bo_unreserve(bo);
1019 
1020 		ttm_bo_put(bo);
1021 		return ret;
1022 	}
1023 
1024 	ttm_bo_move_to_lru_tail_unlocked(bo);
1025 
1026 	return ret;
1027 }
1028 EXPORT_SYMBOL(ttm_bo_init_reserved);
1029 
1030 int ttm_bo_init(struct ttm_device *bdev,
1031 		struct ttm_buffer_object *bo,
1032 		size_t size,
1033 		enum ttm_bo_type type,
1034 		struct ttm_placement *placement,
1035 		uint32_t page_alignment,
1036 		bool interruptible,
1037 		struct sg_table *sg,
1038 		struct dma_resv *resv,
1039 		void (*destroy) (struct ttm_buffer_object *))
1040 {
1041 	struct ttm_operation_ctx ctx = { interruptible, false };
1042 	int ret;
1043 
1044 	ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1045 				   page_alignment, &ctx, sg, resv, destroy);
1046 	if (ret)
1047 		return ret;
1048 
1049 	if (!resv)
1050 		ttm_bo_unreserve(bo);
1051 
1052 	return 0;
1053 }
1054 EXPORT_SYMBOL(ttm_bo_init);
1055 
1056 /*
1057  * buffer object vm functions.
1058  */
1059 
1060 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1061 {
1062 	struct ttm_device *bdev = bo->bdev;
1063 
1064 	drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1065 	ttm_mem_io_free(bdev, bo->resource);
1066 }
1067 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1068 
1069 int ttm_bo_wait(struct ttm_buffer_object *bo,
1070 		bool interruptible, bool no_wait)
1071 {
1072 	long timeout = 15 * HZ;
1073 
1074 	if (no_wait) {
1075 		if (dma_resv_test_signaled(bo->base.resv, true))
1076 			return 0;
1077 		else
1078 			return -EBUSY;
1079 	}
1080 
1081 	timeout = dma_resv_wait_timeout(bo->base.resv, true, interruptible,
1082 					timeout);
1083 	if (timeout < 0)
1084 		return timeout;
1085 
1086 	if (timeout == 0)
1087 		return -EBUSY;
1088 
1089 	return 0;
1090 }
1091 EXPORT_SYMBOL(ttm_bo_wait);
1092 
1093 int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
1094 		   gfp_t gfp_flags)
1095 {
1096 	struct ttm_place place;
1097 	bool locked;
1098 	int ret;
1099 
1100 	/*
1101 	 * While the bo may already reside in SYSTEM placement, set
1102 	 * SYSTEM as new placement to cover also the move further below.
1103 	 * The driver may use the fact that we're moving from SYSTEM
1104 	 * as an indication that we're about to swap out.
1105 	 */
1106 	memset(&place, 0, sizeof(place));
1107 	place.mem_type = bo->resource->mem_type;
1108 	if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place, &locked, NULL))
1109 		return -EBUSY;
1110 
1111 	if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
1112 	    bo->ttm->page_flags & TTM_TT_FLAG_EXTERNAL ||
1113 	    bo->ttm->page_flags & TTM_TT_FLAG_SWAPPED ||
1114 	    !ttm_bo_get_unless_zero(bo)) {
1115 		if (locked)
1116 			dma_resv_unlock(bo->base.resv);
1117 		return -EBUSY;
1118 	}
1119 
1120 	if (bo->deleted) {
1121 		ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1122 		ttm_bo_put(bo);
1123 		return ret == -EBUSY ? -ENOSPC : ret;
1124 	}
1125 
1126 	ttm_bo_move_to_pinned(bo);
1127 	/* TODO: Cleanup the locking */
1128 	spin_unlock(&bo->bdev->lru_lock);
1129 
1130 	/*
1131 	 * Move to system cached
1132 	 */
1133 	if (bo->resource->mem_type != TTM_PL_SYSTEM) {
1134 		struct ttm_operation_ctx ctx = { false, false };
1135 		struct ttm_resource *evict_mem;
1136 		struct ttm_place hop;
1137 
1138 		memset(&hop, 0, sizeof(hop));
1139 		place.mem_type = TTM_PL_SYSTEM;
1140 		ret = ttm_resource_alloc(bo, &place, &evict_mem);
1141 		if (unlikely(ret))
1142 			goto out;
1143 
1144 		ret = ttm_bo_handle_move_mem(bo, evict_mem, true, &ctx, &hop);
1145 		if (unlikely(ret != 0)) {
1146 			WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
1147 			goto out;
1148 		}
1149 	}
1150 
1151 	/*
1152 	 * Make sure BO is idle.
1153 	 */
1154 	ret = ttm_bo_wait(bo, false, false);
1155 	if (unlikely(ret != 0))
1156 		goto out;
1157 
1158 	ttm_bo_unmap_virtual(bo);
1159 
1160 	/*
1161 	 * Swap out. Buffer will be swapped in again as soon as
1162 	 * anyone tries to access a ttm page.
1163 	 */
1164 	if (bo->bdev->funcs->swap_notify)
1165 		bo->bdev->funcs->swap_notify(bo);
1166 
1167 	if (ttm_tt_is_populated(bo->ttm))
1168 		ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);
1169 out:
1170 
1171 	/*
1172 	 * Unreserve without putting on LRU to avoid swapping out an
1173 	 * already swapped buffer.
1174 	 */
1175 	if (locked)
1176 		dma_resv_unlock(bo->base.resv);
1177 	ttm_bo_put(bo);
1178 	return ret == -EBUSY ? -ENOSPC : ret;
1179 }
1180 
1181 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1182 {
1183 	if (bo->ttm == NULL)
1184 		return;
1185 
1186 	ttm_tt_unpopulate(bo->bdev, bo->ttm);
1187 	ttm_tt_destroy(bo->bdev, bo->ttm);
1188 	bo->ttm = NULL;
1189 }
1190