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