xref: /openbmc/linux/drivers/gpu/drm/ttm/ttm_bo.c (revision 8dda2eac)
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 = dma_resv_shared_list(resv);
265 	fence = dma_resv_excl_fence(resv);
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(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(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(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(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 		ret = ttm_bo_wait(bo, true, false);
507 		if (ret)
508 			return ret;
509 
510 		/*
511 		 * Since we've already synced, this frees backing store
512 		 * immediately.
513 		 */
514 		return ttm_bo_pipeline_gutting(bo);
515 	}
516 
517 	ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
518 	if (ret) {
519 		if (ret != -ERESTARTSYS) {
520 			pr_err("Failed to find memory space for buffer 0x%p eviction\n",
521 			       bo);
522 			ttm_bo_mem_space_debug(bo, &placement);
523 		}
524 		goto out;
525 	}
526 
527 	ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
528 	if (unlikely(ret)) {
529 		WARN(ret == -EMULTIHOP, "Unexpected multihop in eviction - likely driver bug\n");
530 		if (ret != -ERESTARTSYS)
531 			pr_err("Buffer eviction failed\n");
532 		ttm_resource_free(bo, &evict_mem);
533 	}
534 out:
535 	return ret;
536 }
537 
538 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
539 			      const struct ttm_place *place)
540 {
541 	dma_resv_assert_held(bo->base.resv);
542 	if (bo->resource->mem_type == TTM_PL_SYSTEM)
543 		return true;
544 
545 	/* Don't evict this BO if it's outside of the
546 	 * requested placement range
547 	 */
548 	if (place->fpfn >= (bo->resource->start + bo->resource->num_pages) ||
549 	    (place->lpfn && place->lpfn <= bo->resource->start))
550 		return false;
551 
552 	return true;
553 }
554 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
555 
556 /*
557  * Check the target bo is allowable to be evicted or swapout, including cases:
558  *
559  * a. if share same reservation object with ctx->resv, have assumption
560  * reservation objects should already be locked, so not lock again and
561  * return true directly when either the opreation allow_reserved_eviction
562  * or the target bo already is in delayed free list;
563  *
564  * b. Otherwise, trylock it.
565  */
566 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
567 					   struct ttm_operation_ctx *ctx,
568 					   const struct ttm_place *place,
569 					   bool *locked, bool *busy)
570 {
571 	bool ret = false;
572 
573 	if (bo->base.resv == ctx->resv) {
574 		dma_resv_assert_held(bo->base.resv);
575 		if (ctx->allow_res_evict)
576 			ret = true;
577 		*locked = false;
578 		if (busy)
579 			*busy = false;
580 	} else {
581 		ret = dma_resv_trylock(bo->base.resv);
582 		*locked = ret;
583 		if (busy)
584 			*busy = !ret;
585 	}
586 
587 	if (ret && place && !bo->bdev->funcs->eviction_valuable(bo, place)) {
588 		ret = false;
589 		if (*locked) {
590 			dma_resv_unlock(bo->base.resv);
591 			*locked = false;
592 		}
593 	}
594 
595 	return ret;
596 }
597 
598 /**
599  * ttm_mem_evict_wait_busy - wait for a busy BO to become available
600  *
601  * @busy_bo: BO which couldn't be locked with trylock
602  * @ctx: operation context
603  * @ticket: acquire ticket
604  *
605  * Try to lock a busy buffer object to avoid failing eviction.
606  */
607 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
608 				   struct ttm_operation_ctx *ctx,
609 				   struct ww_acquire_ctx *ticket)
610 {
611 	int r;
612 
613 	if (!busy_bo || !ticket)
614 		return -EBUSY;
615 
616 	if (ctx->interruptible)
617 		r = dma_resv_lock_interruptible(busy_bo->base.resv,
618 							  ticket);
619 	else
620 		r = dma_resv_lock(busy_bo->base.resv, ticket);
621 
622 	/*
623 	 * TODO: It would be better to keep the BO locked until allocation is at
624 	 * least tried one more time, but that would mean a much larger rework
625 	 * of TTM.
626 	 */
627 	if (!r)
628 		dma_resv_unlock(busy_bo->base.resv);
629 
630 	return r == -EDEADLK ? -EBUSY : r;
631 }
632 
633 int ttm_mem_evict_first(struct ttm_device *bdev,
634 			struct ttm_resource_manager *man,
635 			const struct ttm_place *place,
636 			struct ttm_operation_ctx *ctx,
637 			struct ww_acquire_ctx *ticket)
638 {
639 	struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
640 	bool locked = false;
641 	unsigned i;
642 	int ret;
643 
644 	spin_lock(&bdev->lru_lock);
645 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
646 		list_for_each_entry(bo, &man->lru[i], lru) {
647 			bool busy;
648 
649 			if (!ttm_bo_evict_swapout_allowable(bo, ctx, place,
650 							    &locked, &busy)) {
651 				if (busy && !busy_bo && ticket !=
652 				    dma_resv_locking_ctx(bo->base.resv))
653 					busy_bo = bo;
654 				continue;
655 			}
656 
657 			if (!ttm_bo_get_unless_zero(bo)) {
658 				if (locked)
659 					dma_resv_unlock(bo->base.resv);
660 				continue;
661 			}
662 			break;
663 		}
664 
665 		/* If the inner loop terminated early, we have our candidate */
666 		if (&bo->lru != &man->lru[i])
667 			break;
668 
669 		bo = NULL;
670 	}
671 
672 	if (!bo) {
673 		if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
674 			busy_bo = NULL;
675 		spin_unlock(&bdev->lru_lock);
676 		ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
677 		if (busy_bo)
678 			ttm_bo_put(busy_bo);
679 		return ret;
680 	}
681 
682 	if (bo->deleted) {
683 		ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
684 					  ctx->no_wait_gpu, locked);
685 		ttm_bo_put(bo);
686 		return ret;
687 	}
688 
689 	spin_unlock(&bdev->lru_lock);
690 
691 	ret = ttm_bo_evict(bo, ctx);
692 	if (locked)
693 		ttm_bo_unreserve(bo);
694 
695 	ttm_bo_put(bo);
696 	return ret;
697 }
698 
699 /*
700  * Add the last move fence to the BO and reserve a new shared slot. We only use
701  * a shared slot to avoid unecessary sync and rely on the subsequent bo move to
702  * either stall or use an exclusive fence respectively set bo->moving.
703  */
704 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
705 				 struct ttm_resource_manager *man,
706 				 struct ttm_resource *mem,
707 				 bool no_wait_gpu)
708 {
709 	struct dma_fence *fence;
710 	int ret;
711 
712 	spin_lock(&man->move_lock);
713 	fence = dma_fence_get(man->move);
714 	spin_unlock(&man->move_lock);
715 
716 	if (!fence)
717 		return 0;
718 
719 	if (no_wait_gpu) {
720 		ret = dma_fence_is_signaled(fence) ? 0 : -EBUSY;
721 		dma_fence_put(fence);
722 		return ret;
723 	}
724 
725 	dma_resv_add_shared_fence(bo->base.resv, fence);
726 
727 	ret = dma_resv_reserve_shared(bo->base.resv, 1);
728 	if (unlikely(ret)) {
729 		dma_fence_put(fence);
730 		return ret;
731 	}
732 
733 	dma_fence_put(bo->moving);
734 	bo->moving = fence;
735 	return 0;
736 }
737 
738 /*
739  * Repeatedly evict memory from the LRU for @mem_type until we create enough
740  * space, or we've evicted everything and there isn't enough space.
741  */
742 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
743 				  const struct ttm_place *place,
744 				  struct ttm_resource **mem,
745 				  struct ttm_operation_ctx *ctx)
746 {
747 	struct ttm_device *bdev = bo->bdev;
748 	struct ttm_resource_manager *man;
749 	struct ww_acquire_ctx *ticket;
750 	int ret;
751 
752 	man = ttm_manager_type(bdev, place->mem_type);
753 	ticket = dma_resv_locking_ctx(bo->base.resv);
754 	do {
755 		ret = ttm_resource_alloc(bo, place, mem);
756 		if (likely(!ret))
757 			break;
758 		if (unlikely(ret != -ENOSPC))
759 			return ret;
760 		ret = ttm_mem_evict_first(bdev, man, place, ctx,
761 					  ticket);
762 		if (unlikely(ret != 0))
763 			return ret;
764 	} while (1);
765 
766 	return ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
767 }
768 
769 /*
770  * Creates space for memory region @mem according to its type.
771  *
772  * This function first searches for free space in compatible memory types in
773  * the priority order defined by the driver.  If free space isn't found, then
774  * ttm_bo_mem_force_space is attempted in priority order to evict and find
775  * space.
776  */
777 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
778 			struct ttm_placement *placement,
779 			struct ttm_resource **mem,
780 			struct ttm_operation_ctx *ctx)
781 {
782 	struct ttm_device *bdev = bo->bdev;
783 	bool type_found = false;
784 	int i, ret;
785 
786 	ret = dma_resv_reserve_shared(bo->base.resv, 1);
787 	if (unlikely(ret))
788 		return ret;
789 
790 	for (i = 0; i < placement->num_placement; ++i) {
791 		const struct ttm_place *place = &placement->placement[i];
792 		struct ttm_resource_manager *man;
793 
794 		man = ttm_manager_type(bdev, place->mem_type);
795 		if (!man || !ttm_resource_manager_used(man))
796 			continue;
797 
798 		type_found = true;
799 		ret = ttm_resource_alloc(bo, place, mem);
800 		if (ret == -ENOSPC)
801 			continue;
802 		if (unlikely(ret))
803 			goto error;
804 
805 		ret = ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
806 		if (unlikely(ret)) {
807 			ttm_resource_free(bo, mem);
808 			if (ret == -EBUSY)
809 				continue;
810 
811 			goto error;
812 		}
813 		return 0;
814 	}
815 
816 	for (i = 0; i < placement->num_busy_placement; ++i) {
817 		const struct ttm_place *place = &placement->busy_placement[i];
818 		struct ttm_resource_manager *man;
819 
820 		man = ttm_manager_type(bdev, place->mem_type);
821 		if (!man || !ttm_resource_manager_used(man))
822 			continue;
823 
824 		type_found = true;
825 		ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
826 		if (likely(!ret))
827 			return 0;
828 
829 		if (ret && ret != -EBUSY)
830 			goto error;
831 	}
832 
833 	ret = -ENOMEM;
834 	if (!type_found) {
835 		pr_err(TTM_PFX "No compatible memory type found\n");
836 		ret = -EINVAL;
837 	}
838 
839 error:
840 	if (bo->resource->mem_type == TTM_PL_SYSTEM && !bo->pin_count)
841 		ttm_bo_move_to_lru_tail_unlocked(bo);
842 
843 	return ret;
844 }
845 EXPORT_SYMBOL(ttm_bo_mem_space);
846 
847 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
848 				     struct ttm_resource **mem,
849 				     struct ttm_operation_ctx *ctx,
850 				     struct ttm_place *hop)
851 {
852 	struct ttm_placement hop_placement;
853 	struct ttm_resource *hop_mem;
854 	int ret;
855 
856 	hop_placement.num_placement = hop_placement.num_busy_placement = 1;
857 	hop_placement.placement = hop_placement.busy_placement = hop;
858 
859 	/* find space in the bounce domain */
860 	ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
861 	if (ret)
862 		return ret;
863 	/* move to the bounce domain */
864 	ret = ttm_bo_handle_move_mem(bo, hop_mem, false, ctx, NULL);
865 	if (ret) {
866 		ttm_resource_free(bo, &hop_mem);
867 		return ret;
868 	}
869 	return 0;
870 }
871 
872 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
873 			      struct ttm_placement *placement,
874 			      struct ttm_operation_ctx *ctx)
875 {
876 	struct ttm_resource *mem;
877 	struct ttm_place hop;
878 	int ret;
879 
880 	dma_resv_assert_held(bo->base.resv);
881 
882 	/*
883 	 * Determine where to move the buffer.
884 	 *
885 	 * If driver determines move is going to need
886 	 * an extra step then it will return -EMULTIHOP
887 	 * and the buffer will be moved to the temporary
888 	 * stop and the driver will be called to make
889 	 * the second hop.
890 	 */
891 	ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
892 	if (ret)
893 		return ret;
894 bounce:
895 	ret = ttm_bo_handle_move_mem(bo, mem, false, ctx, &hop);
896 	if (ret == -EMULTIHOP) {
897 		ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
898 		if (ret)
899 			goto out;
900 		/* try and move to final place now. */
901 		goto bounce;
902 	}
903 out:
904 	if (ret)
905 		ttm_resource_free(bo, &mem);
906 	return ret;
907 }
908 
909 static bool ttm_bo_places_compat(const struct ttm_place *places,
910 				 unsigned num_placement,
911 				 struct ttm_resource *mem,
912 				 uint32_t *new_flags)
913 {
914 	unsigned i;
915 
916 	for (i = 0; i < num_placement; i++) {
917 		const struct ttm_place *heap = &places[i];
918 
919 		if ((mem->start < heap->fpfn ||
920 		     (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
921 			continue;
922 
923 		*new_flags = heap->flags;
924 		if ((mem->mem_type == heap->mem_type) &&
925 		    (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
926 		     (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
927 			return true;
928 	}
929 	return false;
930 }
931 
932 bool ttm_bo_mem_compat(struct ttm_placement *placement,
933 		       struct ttm_resource *mem,
934 		       uint32_t *new_flags)
935 {
936 	if (ttm_bo_places_compat(placement->placement, placement->num_placement,
937 				 mem, new_flags))
938 		return true;
939 
940 	if ((placement->busy_placement != placement->placement ||
941 	     placement->num_busy_placement > placement->num_placement) &&
942 	    ttm_bo_places_compat(placement->busy_placement,
943 				 placement->num_busy_placement,
944 				 mem, new_flags))
945 		return true;
946 
947 	return false;
948 }
949 EXPORT_SYMBOL(ttm_bo_mem_compat);
950 
951 int ttm_bo_validate(struct ttm_buffer_object *bo,
952 		    struct ttm_placement *placement,
953 		    struct ttm_operation_ctx *ctx)
954 {
955 	int ret;
956 	uint32_t new_flags;
957 
958 	dma_resv_assert_held(bo->base.resv);
959 
960 	/*
961 	 * Remove the backing store if no placement is given.
962 	 */
963 	if (!placement->num_placement && !placement->num_busy_placement)
964 		return ttm_bo_pipeline_gutting(bo);
965 
966 	/*
967 	 * Check whether we need to move buffer.
968 	 */
969 	if (!ttm_bo_mem_compat(placement, bo->resource, &new_flags)) {
970 		ret = ttm_bo_move_buffer(bo, placement, ctx);
971 		if (ret)
972 			return ret;
973 	}
974 	/*
975 	 * We might need to add a TTM.
976 	 */
977 	if (bo->resource->mem_type == TTM_PL_SYSTEM) {
978 		ret = ttm_tt_create(bo, true);
979 		if (ret)
980 			return ret;
981 	}
982 	return 0;
983 }
984 EXPORT_SYMBOL(ttm_bo_validate);
985 
986 int ttm_bo_init_reserved(struct ttm_device *bdev,
987 			 struct ttm_buffer_object *bo,
988 			 size_t size,
989 			 enum ttm_bo_type type,
990 			 struct ttm_placement *placement,
991 			 uint32_t page_alignment,
992 			 struct ttm_operation_ctx *ctx,
993 			 struct sg_table *sg,
994 			 struct dma_resv *resv,
995 			 void (*destroy) (struct ttm_buffer_object *))
996 {
997 	static const struct ttm_place sys_mem = { .mem_type = TTM_PL_SYSTEM };
998 	bool locked;
999 	int ret;
1000 
1001 	bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1002 
1003 	kref_init(&bo->kref);
1004 	INIT_LIST_HEAD(&bo->lru);
1005 	INIT_LIST_HEAD(&bo->ddestroy);
1006 	bo->bdev = bdev;
1007 	bo->type = type;
1008 	bo->page_alignment = page_alignment;
1009 	bo->moving = NULL;
1010 	bo->pin_count = 0;
1011 	bo->sg = sg;
1012 	if (resv) {
1013 		bo->base.resv = resv;
1014 		dma_resv_assert_held(bo->base.resv);
1015 	} else {
1016 		bo->base.resv = &bo->base._resv;
1017 	}
1018 	atomic_inc(&ttm_glob.bo_count);
1019 
1020 	ret = ttm_resource_alloc(bo, &sys_mem, &bo->resource);
1021 	if (unlikely(ret)) {
1022 		ttm_bo_put(bo);
1023 		return ret;
1024 	}
1025 
1026 	/*
1027 	 * For ttm_bo_type_device buffers, allocate
1028 	 * address space from the device.
1029 	 */
1030 	if (bo->type == ttm_bo_type_device ||
1031 	    bo->type == ttm_bo_type_sg)
1032 		ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
1033 					 bo->resource->num_pages);
1034 
1035 	/* passed reservation objects should already be locked,
1036 	 * since otherwise lockdep will be angered in radeon.
1037 	 */
1038 	if (!resv) {
1039 		locked = dma_resv_trylock(bo->base.resv);
1040 		WARN_ON(!locked);
1041 	}
1042 
1043 	if (likely(!ret))
1044 		ret = ttm_bo_validate(bo, placement, ctx);
1045 
1046 	if (unlikely(ret)) {
1047 		if (!resv)
1048 			ttm_bo_unreserve(bo);
1049 
1050 		ttm_bo_put(bo);
1051 		return ret;
1052 	}
1053 
1054 	ttm_bo_move_to_lru_tail_unlocked(bo);
1055 
1056 	return ret;
1057 }
1058 EXPORT_SYMBOL(ttm_bo_init_reserved);
1059 
1060 int ttm_bo_init(struct ttm_device *bdev,
1061 		struct ttm_buffer_object *bo,
1062 		size_t size,
1063 		enum ttm_bo_type type,
1064 		struct ttm_placement *placement,
1065 		uint32_t page_alignment,
1066 		bool interruptible,
1067 		struct sg_table *sg,
1068 		struct dma_resv *resv,
1069 		void (*destroy) (struct ttm_buffer_object *))
1070 {
1071 	struct ttm_operation_ctx ctx = { interruptible, false };
1072 	int ret;
1073 
1074 	ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1075 				   page_alignment, &ctx, sg, resv, destroy);
1076 	if (ret)
1077 		return ret;
1078 
1079 	if (!resv)
1080 		ttm_bo_unreserve(bo);
1081 
1082 	return 0;
1083 }
1084 EXPORT_SYMBOL(ttm_bo_init);
1085 
1086 /*
1087  * buffer object vm functions.
1088  */
1089 
1090 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1091 {
1092 	struct ttm_device *bdev = bo->bdev;
1093 
1094 	drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1095 	ttm_mem_io_free(bdev, bo->resource);
1096 }
1097 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1098 
1099 int ttm_bo_wait(struct ttm_buffer_object *bo,
1100 		bool interruptible, bool no_wait)
1101 {
1102 	long timeout = 15 * HZ;
1103 
1104 	if (no_wait) {
1105 		if (dma_resv_test_signaled(bo->base.resv, true))
1106 			return 0;
1107 		else
1108 			return -EBUSY;
1109 	}
1110 
1111 	timeout = dma_resv_wait_timeout(bo->base.resv, true, interruptible,
1112 					timeout);
1113 	if (timeout < 0)
1114 		return timeout;
1115 
1116 	if (timeout == 0)
1117 		return -EBUSY;
1118 
1119 	dma_resv_add_excl_fence(bo->base.resv, NULL);
1120 	return 0;
1121 }
1122 EXPORT_SYMBOL(ttm_bo_wait);
1123 
1124 int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
1125 		   gfp_t gfp_flags)
1126 {
1127 	struct ttm_place place;
1128 	bool locked;
1129 	int ret;
1130 
1131 	/*
1132 	 * While the bo may already reside in SYSTEM placement, set
1133 	 * SYSTEM as new placement to cover also the move further below.
1134 	 * The driver may use the fact that we're moving from SYSTEM
1135 	 * as an indication that we're about to swap out.
1136 	 */
1137 	memset(&place, 0, sizeof(place));
1138 	place.mem_type = TTM_PL_SYSTEM;
1139 	if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place, &locked, NULL))
1140 		return -EBUSY;
1141 
1142 	if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
1143 	    bo->ttm->page_flags & TTM_PAGE_FLAG_SG ||
1144 	    bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED ||
1145 	    !ttm_bo_get_unless_zero(bo)) {
1146 		if (locked)
1147 			dma_resv_unlock(bo->base.resv);
1148 		return -EBUSY;
1149 	}
1150 
1151 	if (bo->deleted) {
1152 		ttm_bo_cleanup_refs(bo, false, false, locked);
1153 		ttm_bo_put(bo);
1154 		return 0;
1155 	}
1156 
1157 	ttm_bo_del_from_lru(bo);
1158 	/* TODO: Cleanup the locking */
1159 	spin_unlock(&bo->bdev->lru_lock);
1160 
1161 	/*
1162 	 * Move to system cached
1163 	 */
1164 	if (bo->resource->mem_type != TTM_PL_SYSTEM) {
1165 		struct ttm_operation_ctx ctx = { false, false };
1166 		struct ttm_resource *evict_mem;
1167 		struct ttm_place hop;
1168 
1169 		memset(&hop, 0, sizeof(hop));
1170 		ret = ttm_resource_alloc(bo, &place, &evict_mem);
1171 		if (unlikely(ret))
1172 			goto out;
1173 
1174 		ret = ttm_bo_handle_move_mem(bo, evict_mem, true, &ctx, &hop);
1175 		if (unlikely(ret != 0)) {
1176 			WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
1177 			goto out;
1178 		}
1179 	}
1180 
1181 	/*
1182 	 * Make sure BO is idle.
1183 	 */
1184 	ret = ttm_bo_wait(bo, false, false);
1185 	if (unlikely(ret != 0))
1186 		goto out;
1187 
1188 	ttm_bo_unmap_virtual(bo);
1189 
1190 	/*
1191 	 * Swap out. Buffer will be swapped in again as soon as
1192 	 * anyone tries to access a ttm page.
1193 	 */
1194 	if (bo->bdev->funcs->swap_notify)
1195 		bo->bdev->funcs->swap_notify(bo);
1196 
1197 	if (ttm_tt_is_populated(bo->ttm))
1198 		ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);
1199 out:
1200 
1201 	/*
1202 	 * Unreserve without putting on LRU to avoid swapping out an
1203 	 * already swapped buffer.
1204 	 */
1205 	if (locked)
1206 		dma_resv_unlock(bo->base.resv);
1207 	ttm_bo_put(bo);
1208 	return ret;
1209 }
1210 
1211 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1212 {
1213 	if (bo->ttm == NULL)
1214 		return;
1215 
1216 	ttm_tt_destroy(bo->bdev, bo->ttm);
1217 	bo->ttm = NULL;
1218 }
1219