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