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