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