Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / ttm / ttm_bo.c
blob9a03c7834b1eded3c7f9b26e6d3d69f3a81c1367
1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
4 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
5 * All Rights Reserved.
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:
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.
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.
27 **************************************************************************/
29 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
32 #define pr_fmt(fmt) "[TTM] " fmt
34 #include <drm/ttm/ttm_module.h>
35 #include <drm/ttm/ttm_bo_driver.h>
36 #include <drm/ttm/ttm_placement.h>
37 #include <linux/jiffies.h>
38 #include <linux/slab.h>
39 #include <linux/sched.h>
40 #include <linux/mm.h>
41 #include <linux/file.h>
42 #include <linux/module.h>
43 #include <linux/atomic.h>
44 #include <linux/dma-resv.h>
46 static void ttm_bo_global_kobj_release(struct kobject *kobj);
49 * ttm_global_mutex - protecting the global BO state
51 DEFINE_MUTEX(ttm_global_mutex);
52 unsigned ttm_bo_glob_use_count;
53 struct ttm_bo_global ttm_bo_glob;
54 EXPORT_SYMBOL(ttm_bo_glob);
56 static struct attribute ttm_bo_count = {
57 .name = "bo_count",
58 .mode = S_IRUGO
61 /* default destructor */
62 static void ttm_bo_default_destroy(struct ttm_buffer_object *bo)
64 kfree(bo);
67 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
68 struct ttm_placement *placement)
70 struct drm_printer p = drm_debug_printer(TTM_PFX);
71 struct ttm_resource_manager *man;
72 int i, mem_type;
74 drm_printf(&p, "No space for %p (%lu pages, %luK, %luM)\n",
75 bo, bo->mem.num_pages, bo->mem.size >> 10,
76 bo->mem.size >> 20);
77 for (i = 0; i < placement->num_placement; i++) {
78 mem_type = placement->placement[i].mem_type;
79 drm_printf(&p, " placement[%d]=0x%08X (%d)\n",
80 i, placement->placement[i].flags, mem_type);
81 man = ttm_manager_type(bo->bdev, mem_type);
82 ttm_resource_manager_debug(man, &p);
86 static ssize_t ttm_bo_global_show(struct kobject *kobj,
87 struct attribute *attr,
88 char *buffer)
90 struct ttm_bo_global *glob =
91 container_of(kobj, struct ttm_bo_global, kobj);
93 return snprintf(buffer, PAGE_SIZE, "%d\n",
94 atomic_read(&glob->bo_count));
97 static struct attribute *ttm_bo_global_attrs[] = {
98 &ttm_bo_count,
99 NULL
102 static const struct sysfs_ops ttm_bo_global_ops = {
103 .show = &ttm_bo_global_show
106 static struct kobj_type ttm_bo_glob_kobj_type = {
107 .release = &ttm_bo_global_kobj_release,
108 .sysfs_ops = &ttm_bo_global_ops,
109 .default_attrs = ttm_bo_global_attrs
112 static void ttm_bo_add_mem_to_lru(struct ttm_buffer_object *bo,
113 struct ttm_resource *mem)
115 struct ttm_bo_device *bdev = bo->bdev;
116 struct ttm_resource_manager *man;
118 if (!list_empty(&bo->lru) || bo->pin_count)
119 return;
121 man = ttm_manager_type(bdev, mem->mem_type);
122 list_add_tail(&bo->lru, &man->lru[bo->priority]);
124 if (man->use_tt && bo->ttm &&
125 !(bo->ttm->page_flags & (TTM_PAGE_FLAG_SG |
126 TTM_PAGE_FLAG_SWAPPED))) {
127 list_add_tail(&bo->swap, &ttm_bo_glob.swap_lru[bo->priority]);
131 static void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
133 struct ttm_bo_device *bdev = bo->bdev;
134 bool notify = false;
136 if (!list_empty(&bo->swap)) {
137 list_del_init(&bo->swap);
138 notify = true;
140 if (!list_empty(&bo->lru)) {
141 list_del_init(&bo->lru);
142 notify = true;
145 if (notify && bdev->driver->del_from_lru_notify)
146 bdev->driver->del_from_lru_notify(bo);
149 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
150 struct ttm_buffer_object *bo)
152 if (!pos->first)
153 pos->first = bo;
154 pos->last = bo;
157 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
158 struct ttm_lru_bulk_move *bulk)
160 dma_resv_assert_held(bo->base.resv);
162 ttm_bo_del_from_lru(bo);
163 ttm_bo_add_mem_to_lru(bo, &bo->mem);
165 if (bulk && !bo->pin_count) {
166 switch (bo->mem.mem_type) {
167 case TTM_PL_TT:
168 ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
169 break;
171 case TTM_PL_VRAM:
172 ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
173 break;
175 if (bo->ttm && !(bo->ttm->page_flags &
176 (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED)))
177 ttm_bo_bulk_move_set_pos(&bulk->swap[bo->priority], bo);
180 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
182 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
184 unsigned i;
186 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
187 struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
188 struct ttm_resource_manager *man;
190 if (!pos->first)
191 continue;
193 dma_resv_assert_held(pos->first->base.resv);
194 dma_resv_assert_held(pos->last->base.resv);
196 man = ttm_manager_type(pos->first->bdev, TTM_PL_TT);
197 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
198 &pos->last->lru);
201 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
202 struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
203 struct ttm_resource_manager *man;
205 if (!pos->first)
206 continue;
208 dma_resv_assert_held(pos->first->base.resv);
209 dma_resv_assert_held(pos->last->base.resv);
211 man = ttm_manager_type(pos->first->bdev, TTM_PL_VRAM);
212 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
213 &pos->last->lru);
216 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
217 struct ttm_lru_bulk_move_pos *pos = &bulk->swap[i];
218 struct list_head *lru;
220 if (!pos->first)
221 continue;
223 dma_resv_assert_held(pos->first->base.resv);
224 dma_resv_assert_held(pos->last->base.resv);
226 lru = &ttm_bo_glob.swap_lru[i];
227 list_bulk_move_tail(lru, &pos->first->swap, &pos->last->swap);
230 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
232 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
233 struct ttm_resource *mem, bool evict,
234 struct ttm_operation_ctx *ctx,
235 struct ttm_place *hop)
237 struct ttm_bo_device *bdev = bo->bdev;
238 struct ttm_resource_manager *old_man = ttm_manager_type(bdev, bo->mem.mem_type);
239 struct ttm_resource_manager *new_man = ttm_manager_type(bdev, mem->mem_type);
240 int ret;
242 ttm_bo_unmap_virtual(bo);
245 * Create and bind a ttm if required.
248 if (new_man->use_tt) {
249 /* Zero init the new TTM structure if the old location should
250 * have used one as well.
252 ret = ttm_tt_create(bo, old_man->use_tt);
253 if (ret)
254 goto out_err;
256 if (mem->mem_type != TTM_PL_SYSTEM) {
257 ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
258 if (ret)
259 goto out_err;
263 ret = bdev->driver->move(bo, evict, ctx, mem, hop);
264 if (ret) {
265 if (ret == -EMULTIHOP)
266 return ret;
267 goto out_err;
270 ctx->bytes_moved += bo->num_pages << PAGE_SHIFT;
271 return 0;
273 out_err:
274 new_man = ttm_manager_type(bdev, bo->mem.mem_type);
275 if (!new_man->use_tt)
276 ttm_bo_tt_destroy(bo);
278 return ret;
282 * Call bo::reserved.
283 * Will release GPU memory type usage on destruction.
284 * This is the place to put in driver specific hooks to release
285 * driver private resources.
286 * Will release the bo::reserved lock.
289 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
291 if (bo->bdev->driver->delete_mem_notify)
292 bo->bdev->driver->delete_mem_notify(bo);
294 ttm_bo_tt_destroy(bo);
295 ttm_resource_free(bo, &bo->mem);
298 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
300 int r;
302 if (bo->base.resv == &bo->base._resv)
303 return 0;
305 BUG_ON(!dma_resv_trylock(&bo->base._resv));
307 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
308 dma_resv_unlock(&bo->base._resv);
309 if (r)
310 return r;
312 if (bo->type != ttm_bo_type_sg) {
313 /* This works because the BO is about to be destroyed and nobody
314 * reference it any more. The only tricky case is the trylock on
315 * the resv object while holding the lru_lock.
317 spin_lock(&ttm_bo_glob.lru_lock);
318 bo->base.resv = &bo->base._resv;
319 spin_unlock(&ttm_bo_glob.lru_lock);
322 return r;
325 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
327 struct dma_resv *resv = &bo->base._resv;
328 struct dma_resv_list *fobj;
329 struct dma_fence *fence;
330 int i;
332 rcu_read_lock();
333 fobj = rcu_dereference(resv->fence);
334 fence = rcu_dereference(resv->fence_excl);
335 if (fence && !fence->ops->signaled)
336 dma_fence_enable_sw_signaling(fence);
338 for (i = 0; fobj && i < fobj->shared_count; ++i) {
339 fence = rcu_dereference(fobj->shared[i]);
341 if (!fence->ops->signaled)
342 dma_fence_enable_sw_signaling(fence);
344 rcu_read_unlock();
348 * function ttm_bo_cleanup_refs
349 * If bo idle, remove from lru lists, and unref.
350 * If not idle, block if possible.
352 * Must be called with lru_lock and reservation held, this function
353 * will drop the lru lock and optionally the reservation lock before returning.
355 * @bo: The buffer object to clean-up
356 * @interruptible: Any sleeps should occur interruptibly.
357 * @no_wait_gpu: Never wait for gpu. Return -EBUSY instead.
358 * @unlock_resv: Unlock the reservation lock as well.
361 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
362 bool interruptible, bool no_wait_gpu,
363 bool unlock_resv)
365 struct dma_resv *resv = &bo->base._resv;
366 int ret;
368 if (dma_resv_test_signaled_rcu(resv, true))
369 ret = 0;
370 else
371 ret = -EBUSY;
373 if (ret && !no_wait_gpu) {
374 long lret;
376 if (unlock_resv)
377 dma_resv_unlock(bo->base.resv);
378 spin_unlock(&ttm_bo_glob.lru_lock);
380 lret = dma_resv_wait_timeout_rcu(resv, true, interruptible,
381 30 * HZ);
383 if (lret < 0)
384 return lret;
385 else if (lret == 0)
386 return -EBUSY;
388 spin_lock(&ttm_bo_glob.lru_lock);
389 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
391 * We raced, and lost, someone else holds the reservation now,
392 * and is probably busy in ttm_bo_cleanup_memtype_use.
394 * Even if it's not the case, because we finished waiting any
395 * delayed destruction would succeed, so just return success
396 * here.
398 spin_unlock(&ttm_bo_glob.lru_lock);
399 return 0;
401 ret = 0;
404 if (ret || unlikely(list_empty(&bo->ddestroy))) {
405 if (unlock_resv)
406 dma_resv_unlock(bo->base.resv);
407 spin_unlock(&ttm_bo_glob.lru_lock);
408 return ret;
411 ttm_bo_del_from_lru(bo);
412 list_del_init(&bo->ddestroy);
413 spin_unlock(&ttm_bo_glob.lru_lock);
414 ttm_bo_cleanup_memtype_use(bo);
416 if (unlock_resv)
417 dma_resv_unlock(bo->base.resv);
419 ttm_bo_put(bo);
421 return 0;
425 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
426 * encountered buffers.
428 static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
430 struct ttm_bo_global *glob = &ttm_bo_glob;
431 struct list_head removed;
432 bool empty;
434 INIT_LIST_HEAD(&removed);
436 spin_lock(&glob->lru_lock);
437 while (!list_empty(&bdev->ddestroy)) {
438 struct ttm_buffer_object *bo;
440 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
441 ddestroy);
442 list_move_tail(&bo->ddestroy, &removed);
443 if (!ttm_bo_get_unless_zero(bo))
444 continue;
446 if (remove_all || bo->base.resv != &bo->base._resv) {
447 spin_unlock(&glob->lru_lock);
448 dma_resv_lock(bo->base.resv, NULL);
450 spin_lock(&glob->lru_lock);
451 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
453 } else if (dma_resv_trylock(bo->base.resv)) {
454 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
455 } else {
456 spin_unlock(&glob->lru_lock);
459 ttm_bo_put(bo);
460 spin_lock(&glob->lru_lock);
462 list_splice_tail(&removed, &bdev->ddestroy);
463 empty = list_empty(&bdev->ddestroy);
464 spin_unlock(&glob->lru_lock);
466 return empty;
469 static void ttm_bo_delayed_workqueue(struct work_struct *work)
471 struct ttm_bo_device *bdev =
472 container_of(work, struct ttm_bo_device, wq.work);
474 if (!ttm_bo_delayed_delete(bdev, false))
475 schedule_delayed_work(&bdev->wq,
476 ((HZ / 100) < 1) ? 1 : HZ / 100);
479 static void ttm_bo_release(struct kref *kref)
481 struct ttm_buffer_object *bo =
482 container_of(kref, struct ttm_buffer_object, kref);
483 struct ttm_bo_device *bdev = bo->bdev;
484 size_t acc_size = bo->acc_size;
485 int ret;
487 if (!bo->deleted) {
488 ret = ttm_bo_individualize_resv(bo);
489 if (ret) {
490 /* Last resort, if we fail to allocate memory for the
491 * fences block for the BO to become idle
493 dma_resv_wait_timeout_rcu(bo->base.resv, true, false,
494 30 * HZ);
497 if (bo->bdev->driver->release_notify)
498 bo->bdev->driver->release_notify(bo);
500 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
501 ttm_mem_io_free(bdev, &bo->mem);
504 if (!dma_resv_test_signaled_rcu(bo->base.resv, true) ||
505 !dma_resv_trylock(bo->base.resv)) {
506 /* The BO is not idle, resurrect it for delayed destroy */
507 ttm_bo_flush_all_fences(bo);
508 bo->deleted = true;
510 spin_lock(&ttm_bo_glob.lru_lock);
513 * Make pinned bos immediately available to
514 * shrinkers, now that they are queued for
515 * destruction.
517 if (bo->pin_count) {
518 bo->pin_count = 0;
519 ttm_bo_del_from_lru(bo);
520 ttm_bo_add_mem_to_lru(bo, &bo->mem);
523 kref_init(&bo->kref);
524 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
525 spin_unlock(&ttm_bo_glob.lru_lock);
527 schedule_delayed_work(&bdev->wq,
528 ((HZ / 100) < 1) ? 1 : HZ / 100);
529 return;
532 spin_lock(&ttm_bo_glob.lru_lock);
533 ttm_bo_del_from_lru(bo);
534 list_del(&bo->ddestroy);
535 spin_unlock(&ttm_bo_glob.lru_lock);
537 ttm_bo_cleanup_memtype_use(bo);
538 dma_resv_unlock(bo->base.resv);
540 atomic_dec(&ttm_bo_glob.bo_count);
541 dma_fence_put(bo->moving);
542 if (!ttm_bo_uses_embedded_gem_object(bo))
543 dma_resv_fini(&bo->base._resv);
544 bo->destroy(bo);
545 ttm_mem_global_free(&ttm_mem_glob, acc_size);
548 void ttm_bo_put(struct ttm_buffer_object *bo)
550 kref_put(&bo->kref, ttm_bo_release);
552 EXPORT_SYMBOL(ttm_bo_put);
554 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
556 return cancel_delayed_work_sync(&bdev->wq);
558 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
560 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
562 if (resched)
563 schedule_delayed_work(&bdev->wq,
564 ((HZ / 100) < 1) ? 1 : HZ / 100);
566 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
568 static int ttm_bo_evict(struct ttm_buffer_object *bo,
569 struct ttm_operation_ctx *ctx)
571 struct ttm_bo_device *bdev = bo->bdev;
572 struct ttm_resource evict_mem;
573 struct ttm_placement placement;
574 struct ttm_place hop;
575 int ret = 0;
577 memset(&hop, 0, sizeof(hop));
579 dma_resv_assert_held(bo->base.resv);
581 placement.num_placement = 0;
582 placement.num_busy_placement = 0;
583 bdev->driver->evict_flags(bo, &placement);
585 if (!placement.num_placement && !placement.num_busy_placement) {
586 ttm_bo_wait(bo, false, false);
588 ttm_bo_cleanup_memtype_use(bo);
589 return ttm_tt_create(bo, false);
592 evict_mem = bo->mem;
593 evict_mem.mm_node = NULL;
594 evict_mem.bus.offset = 0;
595 evict_mem.bus.addr = NULL;
597 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
598 if (ret) {
599 if (ret != -ERESTARTSYS) {
600 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
601 bo);
602 ttm_bo_mem_space_debug(bo, &placement);
604 goto out;
607 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx, &hop);
608 if (unlikely(ret)) {
609 WARN(ret == -EMULTIHOP, "Unexpected multihop in eviction - likely driver bug\n");
610 if (ret != -ERESTARTSYS)
611 pr_err("Buffer eviction failed\n");
612 ttm_resource_free(bo, &evict_mem);
614 out:
615 return ret;
618 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
619 const struct ttm_place *place)
621 /* Don't evict this BO if it's outside of the
622 * requested placement range
624 if (place->fpfn >= (bo->mem.start + bo->mem.num_pages) ||
625 (place->lpfn && place->lpfn <= bo->mem.start))
626 return false;
628 return true;
630 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
633 * Check the target bo is allowable to be evicted or swapout, including cases:
635 * a. if share same reservation object with ctx->resv, have assumption
636 * reservation objects should already be locked, so not lock again and
637 * return true directly when either the opreation allow_reserved_eviction
638 * or the target bo already is in delayed free list;
640 * b. Otherwise, trylock it.
642 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
643 struct ttm_operation_ctx *ctx, bool *locked, bool *busy)
645 bool ret = false;
647 if (bo->base.resv == ctx->resv) {
648 dma_resv_assert_held(bo->base.resv);
649 if (ctx->allow_res_evict)
650 ret = true;
651 *locked = false;
652 if (busy)
653 *busy = false;
654 } else {
655 ret = dma_resv_trylock(bo->base.resv);
656 *locked = ret;
657 if (busy)
658 *busy = !ret;
661 return ret;
665 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
667 * @busy_bo: BO which couldn't be locked with trylock
668 * @ctx: operation context
669 * @ticket: acquire ticket
671 * Try to lock a busy buffer object to avoid failing eviction.
673 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
674 struct ttm_operation_ctx *ctx,
675 struct ww_acquire_ctx *ticket)
677 int r;
679 if (!busy_bo || !ticket)
680 return -EBUSY;
682 if (ctx->interruptible)
683 r = dma_resv_lock_interruptible(busy_bo->base.resv,
684 ticket);
685 else
686 r = dma_resv_lock(busy_bo->base.resv, ticket);
689 * TODO: It would be better to keep the BO locked until allocation is at
690 * least tried one more time, but that would mean a much larger rework
691 * of TTM.
693 if (!r)
694 dma_resv_unlock(busy_bo->base.resv);
696 return r == -EDEADLK ? -EBUSY : r;
699 int ttm_mem_evict_first(struct ttm_bo_device *bdev,
700 struct ttm_resource_manager *man,
701 const struct ttm_place *place,
702 struct ttm_operation_ctx *ctx,
703 struct ww_acquire_ctx *ticket)
705 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
706 bool locked = false;
707 unsigned i;
708 int ret;
710 spin_lock(&ttm_bo_glob.lru_lock);
711 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
712 list_for_each_entry(bo, &man->lru[i], lru) {
713 bool busy;
715 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
716 &busy)) {
717 if (busy && !busy_bo && ticket !=
718 dma_resv_locking_ctx(bo->base.resv))
719 busy_bo = bo;
720 continue;
723 if (place && !bdev->driver->eviction_valuable(bo,
724 place)) {
725 if (locked)
726 dma_resv_unlock(bo->base.resv);
727 continue;
729 if (!ttm_bo_get_unless_zero(bo)) {
730 if (locked)
731 dma_resv_unlock(bo->base.resv);
732 continue;
734 break;
737 /* If the inner loop terminated early, we have our candidate */
738 if (&bo->lru != &man->lru[i])
739 break;
741 bo = NULL;
744 if (!bo) {
745 if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
746 busy_bo = NULL;
747 spin_unlock(&ttm_bo_glob.lru_lock);
748 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
749 if (busy_bo)
750 ttm_bo_put(busy_bo);
751 return ret;
754 if (bo->deleted) {
755 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
756 ctx->no_wait_gpu, locked);
757 ttm_bo_put(bo);
758 return ret;
761 spin_unlock(&ttm_bo_glob.lru_lock);
763 ret = ttm_bo_evict(bo, ctx);
764 if (locked)
765 ttm_bo_unreserve(bo);
767 ttm_bo_put(bo);
768 return ret;
772 * Add the last move fence to the BO and reserve a new shared slot.
774 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
775 struct ttm_resource_manager *man,
776 struct ttm_resource *mem,
777 bool no_wait_gpu)
779 struct dma_fence *fence;
780 int ret;
782 spin_lock(&man->move_lock);
783 fence = dma_fence_get(man->move);
784 spin_unlock(&man->move_lock);
786 if (!fence)
787 return 0;
789 if (no_wait_gpu) {
790 dma_fence_put(fence);
791 return -EBUSY;
794 dma_resv_add_shared_fence(bo->base.resv, fence);
796 ret = dma_resv_reserve_shared(bo->base.resv, 1);
797 if (unlikely(ret)) {
798 dma_fence_put(fence);
799 return ret;
802 dma_fence_put(bo->moving);
803 bo->moving = fence;
804 return 0;
808 * Repeatedly evict memory from the LRU for @mem_type until we create enough
809 * space, or we've evicted everything and there isn't enough space.
811 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
812 const struct ttm_place *place,
813 struct ttm_resource *mem,
814 struct ttm_operation_ctx *ctx)
816 struct ttm_bo_device *bdev = bo->bdev;
817 struct ttm_resource_manager *man = ttm_manager_type(bdev, mem->mem_type);
818 struct ww_acquire_ctx *ticket;
819 int ret;
821 ticket = dma_resv_locking_ctx(bo->base.resv);
822 do {
823 ret = ttm_resource_alloc(bo, place, mem);
824 if (likely(!ret))
825 break;
826 if (unlikely(ret != -ENOSPC))
827 return ret;
828 ret = ttm_mem_evict_first(bdev, man, place, ctx,
829 ticket);
830 if (unlikely(ret != 0))
831 return ret;
832 } while (1);
834 return ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
838 * ttm_bo_mem_placement - check if placement is compatible
839 * @bo: BO to find memory for
840 * @place: where to search
841 * @mem: the memory object to fill in
843 * Check if placement is compatible and fill in mem structure.
844 * Returns -EBUSY if placement won't work or negative error code.
845 * 0 when placement can be used.
847 static int ttm_bo_mem_placement(struct ttm_buffer_object *bo,
848 const struct ttm_place *place,
849 struct ttm_resource *mem)
851 struct ttm_bo_device *bdev = bo->bdev;
852 struct ttm_resource_manager *man;
854 man = ttm_manager_type(bdev, place->mem_type);
855 if (!man || !ttm_resource_manager_used(man))
856 return -EBUSY;
858 mem->mem_type = place->mem_type;
859 mem->placement = place->flags;
861 spin_lock(&ttm_bo_glob.lru_lock);
862 ttm_bo_del_from_lru(bo);
863 ttm_bo_add_mem_to_lru(bo, mem);
864 spin_unlock(&ttm_bo_glob.lru_lock);
866 return 0;
870 * Creates space for memory region @mem according to its type.
872 * This function first searches for free space in compatible memory types in
873 * the priority order defined by the driver. If free space isn't found, then
874 * ttm_bo_mem_force_space is attempted in priority order to evict and find
875 * space.
877 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
878 struct ttm_placement *placement,
879 struct ttm_resource *mem,
880 struct ttm_operation_ctx *ctx)
882 struct ttm_bo_device *bdev = bo->bdev;
883 bool type_found = false;
884 int i, ret;
886 ret = dma_resv_reserve_shared(bo->base.resv, 1);
887 if (unlikely(ret))
888 return ret;
890 for (i = 0; i < placement->num_placement; ++i) {
891 const struct ttm_place *place = &placement->placement[i];
892 struct ttm_resource_manager *man;
894 ret = ttm_bo_mem_placement(bo, place, mem);
895 if (ret)
896 continue;
898 type_found = true;
899 ret = ttm_resource_alloc(bo, place, mem);
900 if (ret == -ENOSPC)
901 continue;
902 if (unlikely(ret))
903 goto error;
905 man = ttm_manager_type(bdev, mem->mem_type);
906 ret = ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
907 if (unlikely(ret)) {
908 ttm_resource_free(bo, mem);
909 if (ret == -EBUSY)
910 continue;
912 goto error;
914 return 0;
917 for (i = 0; i < placement->num_busy_placement; ++i) {
918 const struct ttm_place *place = &placement->busy_placement[i];
920 ret = ttm_bo_mem_placement(bo, place, mem);
921 if (ret)
922 continue;
924 type_found = true;
925 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
926 if (likely(!ret))
927 return 0;
929 if (ret && ret != -EBUSY)
930 goto error;
933 ret = -ENOMEM;
934 if (!type_found) {
935 pr_err(TTM_PFX "No compatible memory type found\n");
936 ret = -EINVAL;
939 error:
940 if (bo->mem.mem_type == TTM_PL_SYSTEM && !list_empty(&bo->lru)) {
941 ttm_bo_move_to_lru_tail_unlocked(bo);
944 return ret;
946 EXPORT_SYMBOL(ttm_bo_mem_space);
948 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
949 struct ttm_resource *mem,
950 struct ttm_operation_ctx *ctx,
951 struct ttm_place *hop)
953 struct ttm_placement hop_placement;
954 int ret;
955 struct ttm_resource hop_mem = *mem;
957 hop_mem.mm_node = NULL;
958 hop_mem.mem_type = TTM_PL_SYSTEM;
959 hop_mem.placement = 0;
961 hop_placement.num_placement = hop_placement.num_busy_placement = 1;
962 hop_placement.placement = hop_placement.busy_placement = hop;
964 /* find space in the bounce domain */
965 ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
966 if (ret)
967 return ret;
968 /* move to the bounce domain */
969 ret = ttm_bo_handle_move_mem(bo, &hop_mem, false, ctx, NULL);
970 if (ret)
971 return ret;
972 return 0;
975 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
976 struct ttm_placement *placement,
977 struct ttm_operation_ctx *ctx)
979 int ret = 0;
980 struct ttm_place hop;
981 struct ttm_resource mem;
983 dma_resv_assert_held(bo->base.resv);
985 memset(&hop, 0, sizeof(hop));
987 mem.num_pages = bo->num_pages;
988 mem.size = mem.num_pages << PAGE_SHIFT;
989 mem.page_alignment = bo->mem.page_alignment;
990 mem.bus.offset = 0;
991 mem.bus.addr = NULL;
992 mem.mm_node = NULL;
995 * Determine where to move the buffer.
997 * If driver determines move is going to need
998 * an extra step then it will return -EMULTIHOP
999 * and the buffer will be moved to the temporary
1000 * stop and the driver will be called to make
1001 * the second hop.
1003 bounce:
1004 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
1005 if (ret)
1006 return ret;
1007 ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx, &hop);
1008 if (ret == -EMULTIHOP) {
1009 ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
1010 if (ret)
1011 return ret;
1012 /* try and move to final place now. */
1013 goto bounce;
1015 if (ret)
1016 ttm_resource_free(bo, &mem);
1017 return ret;
1020 static bool ttm_bo_places_compat(const struct ttm_place *places,
1021 unsigned num_placement,
1022 struct ttm_resource *mem,
1023 uint32_t *new_flags)
1025 unsigned i;
1027 for (i = 0; i < num_placement; i++) {
1028 const struct ttm_place *heap = &places[i];
1030 if ((mem->start < heap->fpfn ||
1031 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1032 continue;
1034 *new_flags = heap->flags;
1035 if ((mem->mem_type == heap->mem_type) &&
1036 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
1037 (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
1038 return true;
1040 return false;
1043 bool ttm_bo_mem_compat(struct ttm_placement *placement,
1044 struct ttm_resource *mem,
1045 uint32_t *new_flags)
1047 if (ttm_bo_places_compat(placement->placement, placement->num_placement,
1048 mem, new_flags))
1049 return true;
1051 if ((placement->busy_placement != placement->placement ||
1052 placement->num_busy_placement > placement->num_placement) &&
1053 ttm_bo_places_compat(placement->busy_placement,
1054 placement->num_busy_placement,
1055 mem, new_flags))
1056 return true;
1058 return false;
1060 EXPORT_SYMBOL(ttm_bo_mem_compat);
1062 int ttm_bo_validate(struct ttm_buffer_object *bo,
1063 struct ttm_placement *placement,
1064 struct ttm_operation_ctx *ctx)
1066 int ret;
1067 uint32_t new_flags;
1069 dma_resv_assert_held(bo->base.resv);
1072 * Remove the backing store if no placement is given.
1074 if (!placement->num_placement && !placement->num_busy_placement) {
1075 ret = ttm_bo_pipeline_gutting(bo);
1076 if (ret)
1077 return ret;
1079 return ttm_tt_create(bo, false);
1083 * Check whether we need to move buffer.
1085 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1086 ret = ttm_bo_move_buffer(bo, placement, ctx);
1087 if (ret)
1088 return ret;
1091 * We might need to add a TTM.
1093 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
1094 ret = ttm_tt_create(bo, true);
1095 if (ret)
1096 return ret;
1098 return 0;
1100 EXPORT_SYMBOL(ttm_bo_validate);
1102 int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
1103 struct ttm_buffer_object *bo,
1104 unsigned long size,
1105 enum ttm_bo_type type,
1106 struct ttm_placement *placement,
1107 uint32_t page_alignment,
1108 struct ttm_operation_ctx *ctx,
1109 size_t acc_size,
1110 struct sg_table *sg,
1111 struct dma_resv *resv,
1112 void (*destroy) (struct ttm_buffer_object *))
1114 struct ttm_mem_global *mem_glob = &ttm_mem_glob;
1115 int ret = 0;
1116 unsigned long num_pages;
1117 bool locked;
1119 ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
1120 if (ret) {
1121 pr_err("Out of kernel memory\n");
1122 if (destroy)
1123 (*destroy)(bo);
1124 else
1125 kfree(bo);
1126 return -ENOMEM;
1129 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1130 if (num_pages == 0) {
1131 pr_err("Illegal buffer object size\n");
1132 if (destroy)
1133 (*destroy)(bo);
1134 else
1135 kfree(bo);
1136 ttm_mem_global_free(mem_glob, acc_size);
1137 return -EINVAL;
1139 bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1141 kref_init(&bo->kref);
1142 INIT_LIST_HEAD(&bo->lru);
1143 INIT_LIST_HEAD(&bo->ddestroy);
1144 INIT_LIST_HEAD(&bo->swap);
1145 bo->bdev = bdev;
1146 bo->type = type;
1147 bo->num_pages = num_pages;
1148 bo->mem.size = num_pages << PAGE_SHIFT;
1149 bo->mem.mem_type = TTM_PL_SYSTEM;
1150 bo->mem.num_pages = bo->num_pages;
1151 bo->mem.mm_node = NULL;
1152 bo->mem.page_alignment = page_alignment;
1153 bo->mem.bus.offset = 0;
1154 bo->mem.bus.addr = NULL;
1155 bo->moving = NULL;
1156 bo->mem.placement = 0;
1157 bo->acc_size = acc_size;
1158 bo->pin_count = 0;
1159 bo->sg = sg;
1160 if (resv) {
1161 bo->base.resv = resv;
1162 dma_resv_assert_held(bo->base.resv);
1163 } else {
1164 bo->base.resv = &bo->base._resv;
1166 if (!ttm_bo_uses_embedded_gem_object(bo)) {
1168 * bo.gem is not initialized, so we have to setup the
1169 * struct elements we want use regardless.
1171 dma_resv_init(&bo->base._resv);
1172 drm_vma_node_reset(&bo->base.vma_node);
1174 atomic_inc(&ttm_bo_glob.bo_count);
1177 * For ttm_bo_type_device buffers, allocate
1178 * address space from the device.
1180 if (bo->type == ttm_bo_type_device ||
1181 bo->type == ttm_bo_type_sg)
1182 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
1183 bo->mem.num_pages);
1185 /* passed reservation objects should already be locked,
1186 * since otherwise lockdep will be angered in radeon.
1188 if (!resv) {
1189 locked = dma_resv_trylock(bo->base.resv);
1190 WARN_ON(!locked);
1193 if (likely(!ret))
1194 ret = ttm_bo_validate(bo, placement, ctx);
1196 if (unlikely(ret)) {
1197 if (!resv)
1198 ttm_bo_unreserve(bo);
1200 ttm_bo_put(bo);
1201 return ret;
1204 ttm_bo_move_to_lru_tail_unlocked(bo);
1206 return ret;
1208 EXPORT_SYMBOL(ttm_bo_init_reserved);
1210 int ttm_bo_init(struct ttm_bo_device *bdev,
1211 struct ttm_buffer_object *bo,
1212 unsigned long size,
1213 enum ttm_bo_type type,
1214 struct ttm_placement *placement,
1215 uint32_t page_alignment,
1216 bool interruptible,
1217 size_t acc_size,
1218 struct sg_table *sg,
1219 struct dma_resv *resv,
1220 void (*destroy) (struct ttm_buffer_object *))
1222 struct ttm_operation_ctx ctx = { interruptible, false };
1223 int ret;
1225 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1226 page_alignment, &ctx, acc_size,
1227 sg, resv, destroy);
1228 if (ret)
1229 return ret;
1231 if (!resv)
1232 ttm_bo_unreserve(bo);
1234 return 0;
1236 EXPORT_SYMBOL(ttm_bo_init);
1238 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1239 unsigned long bo_size,
1240 unsigned struct_size)
1242 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1243 size_t size = 0;
1245 size += ttm_round_pot(struct_size);
1246 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
1247 size += ttm_round_pot(sizeof(struct ttm_tt));
1248 return size;
1250 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1252 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1254 struct ttm_bo_global *glob =
1255 container_of(kobj, struct ttm_bo_global, kobj);
1257 __free_page(glob->dummy_read_page);
1260 static void ttm_bo_global_release(void)
1262 struct ttm_bo_global *glob = &ttm_bo_glob;
1264 mutex_lock(&ttm_global_mutex);
1265 if (--ttm_bo_glob_use_count > 0)
1266 goto out;
1268 kobject_del(&glob->kobj);
1269 kobject_put(&glob->kobj);
1270 ttm_mem_global_release(&ttm_mem_glob);
1271 memset(glob, 0, sizeof(*glob));
1272 out:
1273 mutex_unlock(&ttm_global_mutex);
1276 static int ttm_bo_global_init(void)
1278 struct ttm_bo_global *glob = &ttm_bo_glob;
1279 int ret = 0;
1280 unsigned i;
1282 mutex_lock(&ttm_global_mutex);
1283 if (++ttm_bo_glob_use_count > 1)
1284 goto out;
1286 ret = ttm_mem_global_init(&ttm_mem_glob);
1287 if (ret)
1288 goto out;
1290 spin_lock_init(&glob->lru_lock);
1291 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1293 if (unlikely(glob->dummy_read_page == NULL)) {
1294 ret = -ENOMEM;
1295 goto out;
1298 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1299 INIT_LIST_HEAD(&glob->swap_lru[i]);
1300 INIT_LIST_HEAD(&glob->device_list);
1301 atomic_set(&glob->bo_count, 0);
1303 ret = kobject_init_and_add(
1304 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1305 if (unlikely(ret != 0))
1306 kobject_put(&glob->kobj);
1307 out:
1308 mutex_unlock(&ttm_global_mutex);
1309 return ret;
1312 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1314 struct ttm_bo_global *glob = &ttm_bo_glob;
1315 int ret = 0;
1316 unsigned i;
1317 struct ttm_resource_manager *man;
1319 man = ttm_manager_type(bdev, TTM_PL_SYSTEM);
1320 ttm_resource_manager_set_used(man, false);
1321 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL);
1323 mutex_lock(&ttm_global_mutex);
1324 list_del(&bdev->device_list);
1325 mutex_unlock(&ttm_global_mutex);
1327 cancel_delayed_work_sync(&bdev->wq);
1329 if (ttm_bo_delayed_delete(bdev, true))
1330 pr_debug("Delayed destroy list was clean\n");
1332 spin_lock(&glob->lru_lock);
1333 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1334 if (list_empty(&man->lru[0]))
1335 pr_debug("Swap list %d was clean\n", i);
1336 spin_unlock(&glob->lru_lock);
1338 ttm_pool_fini(&bdev->pool);
1340 if (!ret)
1341 ttm_bo_global_release();
1343 return ret;
1345 EXPORT_SYMBOL(ttm_bo_device_release);
1347 static void ttm_bo_init_sysman(struct ttm_bo_device *bdev)
1349 struct ttm_resource_manager *man = &bdev->sysman;
1352 * Initialize the system memory buffer type.
1353 * Other types need to be driver / IOCTL initialized.
1355 man->use_tt = true;
1357 ttm_resource_manager_init(man, 0);
1358 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, man);
1359 ttm_resource_manager_set_used(man, true);
1362 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1363 struct ttm_bo_driver *driver,
1364 struct device *dev,
1365 struct address_space *mapping,
1366 struct drm_vma_offset_manager *vma_manager,
1367 bool use_dma_alloc, bool use_dma32)
1369 struct ttm_bo_global *glob = &ttm_bo_glob;
1370 int ret;
1372 if (WARN_ON(vma_manager == NULL))
1373 return -EINVAL;
1375 ret = ttm_bo_global_init();
1376 if (ret)
1377 return ret;
1379 bdev->driver = driver;
1381 ttm_bo_init_sysman(bdev);
1382 ttm_pool_init(&bdev->pool, dev, use_dma_alloc, use_dma32);
1384 bdev->vma_manager = vma_manager;
1385 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1386 INIT_LIST_HEAD(&bdev->ddestroy);
1387 bdev->dev_mapping = mapping;
1388 mutex_lock(&ttm_global_mutex);
1389 list_add_tail(&bdev->device_list, &glob->device_list);
1390 mutex_unlock(&ttm_global_mutex);
1392 return 0;
1394 EXPORT_SYMBOL(ttm_bo_device_init);
1397 * buffer object vm functions.
1400 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1402 struct ttm_bo_device *bdev = bo->bdev;
1404 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1405 ttm_mem_io_free(bdev, &bo->mem);
1407 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1409 int ttm_bo_wait(struct ttm_buffer_object *bo,
1410 bool interruptible, bool no_wait)
1412 long timeout = 15 * HZ;
1414 if (no_wait) {
1415 if (dma_resv_test_signaled_rcu(bo->base.resv, true))
1416 return 0;
1417 else
1418 return -EBUSY;
1421 timeout = dma_resv_wait_timeout_rcu(bo->base.resv, true,
1422 interruptible, timeout);
1423 if (timeout < 0)
1424 return timeout;
1426 if (timeout == 0)
1427 return -EBUSY;
1429 dma_resv_add_excl_fence(bo->base.resv, NULL);
1430 return 0;
1432 EXPORT_SYMBOL(ttm_bo_wait);
1435 * A buffer object shrink method that tries to swap out the first
1436 * buffer object on the bo_global::swap_lru list.
1438 int ttm_bo_swapout(struct ttm_operation_ctx *ctx)
1440 struct ttm_bo_global *glob = &ttm_bo_glob;
1441 struct ttm_buffer_object *bo;
1442 int ret = -EBUSY;
1443 bool locked;
1444 unsigned i;
1446 spin_lock(&glob->lru_lock);
1447 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1448 list_for_each_entry(bo, &glob->swap_lru[i], swap) {
1449 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
1450 NULL))
1451 continue;
1453 if (!ttm_bo_get_unless_zero(bo)) {
1454 if (locked)
1455 dma_resv_unlock(bo->base.resv);
1456 continue;
1459 ret = 0;
1460 break;
1462 if (!ret)
1463 break;
1466 if (ret) {
1467 spin_unlock(&glob->lru_lock);
1468 return ret;
1471 if (bo->deleted) {
1472 ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1473 ttm_bo_put(bo);
1474 return ret;
1477 ttm_bo_del_from_lru(bo);
1478 spin_unlock(&glob->lru_lock);
1481 * Move to system cached
1484 if (bo->mem.mem_type != TTM_PL_SYSTEM) {
1485 struct ttm_operation_ctx ctx = { false, false };
1486 struct ttm_resource evict_mem;
1487 struct ttm_place hop;
1489 memset(&hop, 0, sizeof(hop));
1491 evict_mem = bo->mem;
1492 evict_mem.mm_node = NULL;
1493 evict_mem.placement = 0;
1494 evict_mem.mem_type = TTM_PL_SYSTEM;
1496 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx, &hop);
1497 if (unlikely(ret != 0)) {
1498 WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
1499 goto out;
1504 * Make sure BO is idle.
1507 ret = ttm_bo_wait(bo, false, false);
1508 if (unlikely(ret != 0))
1509 goto out;
1511 ttm_bo_unmap_virtual(bo);
1514 * Swap out. Buffer will be swapped in again as soon as
1515 * anyone tries to access a ttm page.
1518 if (bo->bdev->driver->swap_notify)
1519 bo->bdev->driver->swap_notify(bo);
1521 ret = ttm_tt_swapout(bo->bdev, bo->ttm);
1522 out:
1526 * Unreserve without putting on LRU to avoid swapping out an
1527 * already swapped buffer.
1529 if (locked)
1530 dma_resv_unlock(bo->base.resv);
1531 ttm_bo_put(bo);
1532 return ret;
1534 EXPORT_SYMBOL(ttm_bo_swapout);
1536 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1538 if (bo->ttm == NULL)
1539 return;
1541 ttm_tt_destroy(bo->bdev, bo->ttm);
1542 bo->ttm = NULL;