1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
4 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
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
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>
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 = {
61 /* default destructor */
62 static void ttm_bo_default_destroy(struct ttm_buffer_object *bo)
67 static inline int ttm_mem_type_from_place(const struct ttm_place *place,
72 pos = ffs(place->flags & TTM_PL_MASK_MEM);
80 static void ttm_mem_type_debug(struct ttm_bo_device *bdev, struct drm_printer *p,
83 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
85 drm_printf(p, " has_type: %d\n", man->has_type);
86 drm_printf(p, " use_type: %d\n", man->use_type);
87 drm_printf(p, " flags: 0x%08X\n", man->flags);
88 drm_printf(p, " gpu_offset: 0x%08llX\n", man->gpu_offset);
89 drm_printf(p, " size: %llu\n", man->size);
90 drm_printf(p, " available_caching: 0x%08X\n", man->available_caching);
91 drm_printf(p, " default_caching: 0x%08X\n", man->default_caching);
92 if (mem_type != TTM_PL_SYSTEM)
93 (*man->func->debug)(man, p);
96 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
97 struct ttm_placement *placement)
99 struct drm_printer p = drm_debug_printer(TTM_PFX);
100 int i, ret, mem_type;
102 drm_printf(&p, "No space for %p (%lu pages, %luK, %luM)\n",
103 bo, bo->mem.num_pages, bo->mem.size >> 10,
105 for (i = 0; i < placement->num_placement; i++) {
106 ret = ttm_mem_type_from_place(&placement->placement[i],
110 drm_printf(&p, " placement[%d]=0x%08X (%d)\n",
111 i, placement->placement[i].flags, mem_type);
112 ttm_mem_type_debug(bo->bdev, &p, mem_type);
116 static ssize_t ttm_bo_global_show(struct kobject *kobj,
117 struct attribute *attr,
120 struct ttm_bo_global *glob =
121 container_of(kobj, struct ttm_bo_global, kobj);
123 return snprintf(buffer, PAGE_SIZE, "%d\n",
124 atomic_read(&glob->bo_count));
127 static struct attribute *ttm_bo_global_attrs[] = {
132 static const struct sysfs_ops ttm_bo_global_ops = {
133 .show = &ttm_bo_global_show
136 static struct kobj_type ttm_bo_glob_kobj_type = {
137 .release = &ttm_bo_global_kobj_release,
138 .sysfs_ops = &ttm_bo_global_ops,
139 .default_attrs = ttm_bo_global_attrs
143 static inline uint32_t ttm_bo_type_flags(unsigned type)
148 static void ttm_bo_add_mem_to_lru(struct ttm_buffer_object *bo,
149 struct ttm_mem_reg *mem)
151 struct ttm_bo_device *bdev = bo->bdev;
152 struct ttm_mem_type_manager *man;
154 dma_resv_assert_held(bo->base.resv);
156 if (!list_empty(&bo->lru))
159 if (mem->placement & TTM_PL_FLAG_NO_EVICT)
162 man = &bdev->man[mem->mem_type];
163 list_add_tail(&bo->lru, &man->lru[bo->priority]);
165 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm &&
166 !(bo->ttm->page_flags & (TTM_PAGE_FLAG_SG |
167 TTM_PAGE_FLAG_SWAPPED))) {
168 list_add_tail(&bo->swap, &ttm_bo_glob.swap_lru[bo->priority]);
172 static void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
174 struct ttm_bo_device *bdev = bo->bdev;
177 if (!list_empty(&bo->swap)) {
178 list_del_init(&bo->swap);
181 if (!list_empty(&bo->lru)) {
182 list_del_init(&bo->lru);
186 if (notify && bdev->driver->del_from_lru_notify)
187 bdev->driver->del_from_lru_notify(bo);
190 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
191 struct ttm_buffer_object *bo)
198 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
199 struct ttm_lru_bulk_move *bulk)
201 dma_resv_assert_held(bo->base.resv);
203 ttm_bo_del_from_lru(bo);
204 ttm_bo_add_mem_to_lru(bo, &bo->mem);
206 if (bulk && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
207 switch (bo->mem.mem_type) {
209 ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
213 ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
216 if (bo->ttm && !(bo->ttm->page_flags &
217 (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED)))
218 ttm_bo_bulk_move_set_pos(&bulk->swap[bo->priority], bo);
221 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
223 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
227 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
228 struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
229 struct ttm_mem_type_manager *man;
234 dma_resv_assert_held(pos->first->base.resv);
235 dma_resv_assert_held(pos->last->base.resv);
237 man = &pos->first->bdev->man[TTM_PL_TT];
238 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
242 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
243 struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
244 struct ttm_mem_type_manager *man;
249 dma_resv_assert_held(pos->first->base.resv);
250 dma_resv_assert_held(pos->last->base.resv);
252 man = &pos->first->bdev->man[TTM_PL_VRAM];
253 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
257 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
258 struct ttm_lru_bulk_move_pos *pos = &bulk->swap[i];
259 struct list_head *lru;
264 dma_resv_assert_held(pos->first->base.resv);
265 dma_resv_assert_held(pos->last->base.resv);
267 lru = &ttm_bo_glob.swap_lru[i];
268 list_bulk_move_tail(lru, &pos->first->swap, &pos->last->swap);
271 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
273 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
274 struct ttm_mem_reg *mem, bool evict,
275 struct ttm_operation_ctx *ctx)
277 struct ttm_bo_device *bdev = bo->bdev;
278 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
279 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
280 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
281 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
284 if (old_is_pci || new_is_pci ||
285 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
286 ret = ttm_mem_io_lock(old_man, true);
287 if (unlikely(ret != 0))
289 ttm_bo_unmap_virtual_locked(bo);
290 ttm_mem_io_unlock(old_man);
294 * Create and bind a ttm if required.
297 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
298 if (bo->ttm == NULL) {
299 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
300 ret = ttm_tt_create(bo, zero);
305 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
309 if (mem->mem_type != TTM_PL_SYSTEM) {
310 ret = ttm_tt_bind(bo->ttm, mem, ctx);
315 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
316 if (bdev->driver->move_notify)
317 bdev->driver->move_notify(bo, evict, mem);
324 if (bdev->driver->move_notify)
325 bdev->driver->move_notify(bo, evict, mem);
327 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
328 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
329 ret = ttm_bo_move_ttm(bo, ctx, mem);
330 else if (bdev->driver->move)
331 ret = bdev->driver->move(bo, evict, ctx, mem);
333 ret = ttm_bo_move_memcpy(bo, ctx, mem);
336 if (bdev->driver->move_notify) {
338 bdev->driver->move_notify(bo, false, mem);
349 bo->offset = (bo->mem.start << PAGE_SHIFT) +
350 bdev->man[bo->mem.mem_type].gpu_offset;
354 ctx->bytes_moved += bo->num_pages << PAGE_SHIFT;
358 new_man = &bdev->man[bo->mem.mem_type];
359 if (new_man->flags & TTM_MEMTYPE_FLAG_FIXED) {
360 ttm_tt_destroy(bo->ttm);
369 * Will release GPU memory type usage on destruction.
370 * This is the place to put in driver specific hooks to release
371 * driver private resources.
372 * Will release the bo::reserved lock.
375 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
377 if (bo->bdev->driver->move_notify)
378 bo->bdev->driver->move_notify(bo, false, NULL);
380 ttm_tt_destroy(bo->ttm);
382 ttm_bo_mem_put(bo, &bo->mem);
385 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
389 if (bo->base.resv == &bo->base._resv)
392 BUG_ON(!dma_resv_trylock(&bo->base._resv));
394 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
395 dma_resv_unlock(&bo->base._resv);
399 if (bo->type != ttm_bo_type_sg) {
400 /* This works because the BO is about to be destroyed and nobody
401 * reference it any more. The only tricky case is the trylock on
402 * the resv object while holding the lru_lock.
404 spin_lock(&ttm_bo_glob.lru_lock);
405 bo->base.resv = &bo->base._resv;
406 spin_unlock(&ttm_bo_glob.lru_lock);
412 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
414 struct dma_resv *resv = &bo->base._resv;
415 struct dma_resv_list *fobj;
416 struct dma_fence *fence;
420 fobj = rcu_dereference(resv->fence);
421 fence = rcu_dereference(resv->fence_excl);
422 if (fence && !fence->ops->signaled)
423 dma_fence_enable_sw_signaling(fence);
425 for (i = 0; fobj && i < fobj->shared_count; ++i) {
426 fence = rcu_dereference(fobj->shared[i]);
428 if (!fence->ops->signaled)
429 dma_fence_enable_sw_signaling(fence);
435 * function ttm_bo_cleanup_refs
436 * If bo idle, remove from lru lists, and unref.
437 * If not idle, block if possible.
439 * Must be called with lru_lock and reservation held, this function
440 * will drop the lru lock and optionally the reservation lock before returning.
442 * @interruptible Any sleeps should occur interruptibly.
443 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
444 * @unlock_resv Unlock the reservation lock as well.
447 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
448 bool interruptible, bool no_wait_gpu,
451 struct dma_resv *resv = &bo->base._resv;
454 if (dma_resv_test_signaled_rcu(resv, true))
459 if (ret && !no_wait_gpu) {
463 dma_resv_unlock(bo->base.resv);
464 spin_unlock(&ttm_bo_glob.lru_lock);
466 lret = dma_resv_wait_timeout_rcu(resv, true, interruptible,
474 spin_lock(&ttm_bo_glob.lru_lock);
475 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
477 * We raced, and lost, someone else holds the reservation now,
478 * and is probably busy in ttm_bo_cleanup_memtype_use.
480 * Even if it's not the case, because we finished waiting any
481 * delayed destruction would succeed, so just return success
484 spin_unlock(&ttm_bo_glob.lru_lock);
490 if (ret || unlikely(list_empty(&bo->ddestroy))) {
492 dma_resv_unlock(bo->base.resv);
493 spin_unlock(&ttm_bo_glob.lru_lock);
497 ttm_bo_del_from_lru(bo);
498 list_del_init(&bo->ddestroy);
499 spin_unlock(&ttm_bo_glob.lru_lock);
500 ttm_bo_cleanup_memtype_use(bo);
503 dma_resv_unlock(bo->base.resv);
511 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
512 * encountered buffers.
514 static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
516 struct ttm_bo_global *glob = &ttm_bo_glob;
517 struct list_head removed;
520 INIT_LIST_HEAD(&removed);
522 spin_lock(&glob->lru_lock);
523 while (!list_empty(&bdev->ddestroy)) {
524 struct ttm_buffer_object *bo;
526 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
528 list_move_tail(&bo->ddestroy, &removed);
529 if (!ttm_bo_get_unless_zero(bo))
532 if (remove_all || bo->base.resv != &bo->base._resv) {
533 spin_unlock(&glob->lru_lock);
534 dma_resv_lock(bo->base.resv, NULL);
536 spin_lock(&glob->lru_lock);
537 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
539 } else if (dma_resv_trylock(bo->base.resv)) {
540 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
542 spin_unlock(&glob->lru_lock);
546 spin_lock(&glob->lru_lock);
548 list_splice_tail(&removed, &bdev->ddestroy);
549 empty = list_empty(&bdev->ddestroy);
550 spin_unlock(&glob->lru_lock);
555 static void ttm_bo_delayed_workqueue(struct work_struct *work)
557 struct ttm_bo_device *bdev =
558 container_of(work, struct ttm_bo_device, wq.work);
560 if (!ttm_bo_delayed_delete(bdev, false))
561 schedule_delayed_work(&bdev->wq,
562 ((HZ / 100) < 1) ? 1 : HZ / 100);
565 static void ttm_bo_release(struct kref *kref)
567 struct ttm_buffer_object *bo =
568 container_of(kref, struct ttm_buffer_object, kref);
569 struct ttm_bo_device *bdev = bo->bdev;
570 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
571 size_t acc_size = bo->acc_size;
575 ret = ttm_bo_individualize_resv(bo);
577 /* Last resort, if we fail to allocate memory for the
578 * fences block for the BO to become idle
580 dma_resv_wait_timeout_rcu(bo->base.resv, true, false,
584 if (bo->bdev->driver->release_notify)
585 bo->bdev->driver->release_notify(bo);
587 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
588 ttm_mem_io_lock(man, false);
589 ttm_mem_io_free_vm(bo);
590 ttm_mem_io_unlock(man);
593 if (!dma_resv_test_signaled_rcu(bo->base.resv, true)) {
594 /* The BO is not idle, resurrect it for delayed destroy */
595 ttm_bo_flush_all_fences(bo);
598 spin_lock(&ttm_bo_glob.lru_lock);
601 * Make NO_EVICT bos immediately available to
602 * shrinkers, now that they are queued for
605 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
606 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
607 ttm_bo_move_to_lru_tail(bo, NULL);
610 kref_init(&bo->kref);
611 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
612 spin_unlock(&ttm_bo_glob.lru_lock);
614 schedule_delayed_work(&bdev->wq,
615 ((HZ / 100) < 1) ? 1 : HZ / 100);
619 spin_lock(&ttm_bo_glob.lru_lock);
620 ttm_bo_del_from_lru(bo);
621 list_del(&bo->ddestroy);
622 spin_unlock(&ttm_bo_glob.lru_lock);
624 ttm_bo_cleanup_memtype_use(bo);
626 BUG_ON(bo->mem.mm_node != NULL);
627 atomic_dec(&ttm_bo_glob.bo_count);
628 dma_fence_put(bo->moving);
629 if (!ttm_bo_uses_embedded_gem_object(bo))
630 dma_resv_fini(&bo->base._resv);
632 ttm_mem_global_free(&ttm_mem_glob, acc_size);
635 void ttm_bo_put(struct ttm_buffer_object *bo)
637 kref_put(&bo->kref, ttm_bo_release);
639 EXPORT_SYMBOL(ttm_bo_put);
641 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
643 return cancel_delayed_work_sync(&bdev->wq);
645 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
647 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
650 schedule_delayed_work(&bdev->wq,
651 ((HZ / 100) < 1) ? 1 : HZ / 100);
653 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
655 static int ttm_bo_evict(struct ttm_buffer_object *bo,
656 struct ttm_operation_ctx *ctx)
658 struct ttm_bo_device *bdev = bo->bdev;
659 struct ttm_mem_reg evict_mem;
660 struct ttm_placement placement;
663 dma_resv_assert_held(bo->base.resv);
665 placement.num_placement = 0;
666 placement.num_busy_placement = 0;
667 bdev->driver->evict_flags(bo, &placement);
669 if (!placement.num_placement && !placement.num_busy_placement) {
670 ret = ttm_bo_pipeline_gutting(bo);
674 return ttm_tt_create(bo, false);
678 evict_mem.mm_node = NULL;
679 evict_mem.bus.io_reserved_vm = false;
680 evict_mem.bus.io_reserved_count = 0;
682 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
684 if (ret != -ERESTARTSYS) {
685 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
687 ttm_bo_mem_space_debug(bo, &placement);
692 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx);
694 if (ret != -ERESTARTSYS)
695 pr_err("Buffer eviction failed\n");
696 ttm_bo_mem_put(bo, &evict_mem);
704 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
705 const struct ttm_place *place)
707 /* Don't evict this BO if it's outside of the
708 * requested placement range
710 if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
711 (place->lpfn && place->lpfn <= bo->mem.start))
716 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
719 * Check the target bo is allowable to be evicted or swapout, including cases:
721 * a. if share same reservation object with ctx->resv, have assumption
722 * reservation objects should already be locked, so not lock again and
723 * return true directly when either the opreation allow_reserved_eviction
724 * or the target bo already is in delayed free list;
726 * b. Otherwise, trylock it.
728 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
729 struct ttm_operation_ctx *ctx, bool *locked, bool *busy)
733 if (bo->base.resv == ctx->resv) {
734 dma_resv_assert_held(bo->base.resv);
735 if (ctx->flags & TTM_OPT_FLAG_ALLOW_RES_EVICT)
741 ret = dma_resv_trylock(bo->base.resv);
751 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
753 * @busy_bo: BO which couldn't be locked with trylock
754 * @ctx: operation context
755 * @ticket: acquire ticket
757 * Try to lock a busy buffer object to avoid failing eviction.
759 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
760 struct ttm_operation_ctx *ctx,
761 struct ww_acquire_ctx *ticket)
765 if (!busy_bo || !ticket)
768 if (ctx->interruptible)
769 r = dma_resv_lock_interruptible(busy_bo->base.resv,
772 r = dma_resv_lock(busy_bo->base.resv, ticket);
775 * TODO: It would be better to keep the BO locked until allocation is at
776 * least tried one more time, but that would mean a much larger rework
780 dma_resv_unlock(busy_bo->base.resv);
782 return r == -EDEADLK ? -EBUSY : r;
785 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
787 const struct ttm_place *place,
788 struct ttm_operation_ctx *ctx,
789 struct ww_acquire_ctx *ticket)
791 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
792 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
797 spin_lock(&ttm_bo_glob.lru_lock);
798 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
799 list_for_each_entry(bo, &man->lru[i], lru) {
802 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
804 if (busy && !busy_bo && ticket !=
805 dma_resv_locking_ctx(bo->base.resv))
810 if (place && !bdev->driver->eviction_valuable(bo,
813 dma_resv_unlock(bo->base.resv);
816 if (!ttm_bo_get_unless_zero(bo)) {
818 dma_resv_unlock(bo->base.resv);
824 /* If the inner loop terminated early, we have our candidate */
825 if (&bo->lru != &man->lru[i])
832 if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
834 spin_unlock(&ttm_bo_glob.lru_lock);
835 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
842 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
843 ctx->no_wait_gpu, locked);
848 spin_unlock(&ttm_bo_glob.lru_lock);
850 ret = ttm_bo_evict(bo, ctx);
852 ttm_bo_unreserve(bo);
858 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
860 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
863 (*man->func->put_node)(man, mem);
865 EXPORT_SYMBOL(ttm_bo_mem_put);
868 * Add the last move fence to the BO and reserve a new shared slot.
870 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
871 struct ttm_mem_type_manager *man,
872 struct ttm_mem_reg *mem,
875 struct dma_fence *fence;
878 spin_lock(&man->move_lock);
879 fence = dma_fence_get(man->move);
880 spin_unlock(&man->move_lock);
888 dma_resv_add_shared_fence(bo->base.resv, fence);
890 ret = dma_resv_reserve_shared(bo->base.resv, 1);
892 dma_fence_put(fence);
896 dma_fence_put(bo->moving);
902 * Repeatedly evict memory from the LRU for @mem_type until we create enough
903 * space, or we've evicted everything and there isn't enough space.
905 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
906 const struct ttm_place *place,
907 struct ttm_mem_reg *mem,
908 struct ttm_operation_ctx *ctx)
910 struct ttm_bo_device *bdev = bo->bdev;
911 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
912 struct ww_acquire_ctx *ticket;
915 ticket = dma_resv_locking_ctx(bo->base.resv);
917 ret = (*man->func->get_node)(man, bo, place, mem);
918 if (unlikely(ret != 0))
922 ret = ttm_mem_evict_first(bdev, mem->mem_type, place, ctx,
924 if (unlikely(ret != 0))
928 return ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
931 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
932 uint32_t cur_placement,
933 uint32_t proposed_placement)
935 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
936 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
939 * Keep current caching if possible.
942 if ((cur_placement & caching) != 0)
943 result |= (cur_placement & caching);
944 else if ((man->default_caching & caching) != 0)
945 result |= man->default_caching;
946 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
947 result |= TTM_PL_FLAG_CACHED;
948 else if ((TTM_PL_FLAG_WC & caching) != 0)
949 result |= TTM_PL_FLAG_WC;
950 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
951 result |= TTM_PL_FLAG_UNCACHED;
956 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
958 const struct ttm_place *place,
959 uint32_t *masked_placement)
961 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
963 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
966 if ((place->flags & man->available_caching) == 0)
969 cur_flags |= (place->flags & man->available_caching);
971 *masked_placement = cur_flags;
976 * ttm_bo_mem_placement - check if placement is compatible
977 * @bo: BO to find memory for
978 * @place: where to search
979 * @mem: the memory object to fill in
980 * @ctx: operation context
982 * Check if placement is compatible and fill in mem structure.
983 * Returns -EBUSY if placement won't work or negative error code.
984 * 0 when placement can be used.
986 static int ttm_bo_mem_placement(struct ttm_buffer_object *bo,
987 const struct ttm_place *place,
988 struct ttm_mem_reg *mem,
989 struct ttm_operation_ctx *ctx)
991 struct ttm_bo_device *bdev = bo->bdev;
992 uint32_t mem_type = TTM_PL_SYSTEM;
993 struct ttm_mem_type_manager *man;
994 uint32_t cur_flags = 0;
997 ret = ttm_mem_type_from_place(place, &mem_type);
1001 man = &bdev->man[mem_type];
1002 if (!man->has_type || !man->use_type)
1005 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
1008 cur_flags = ttm_bo_select_caching(man, bo->mem.placement, cur_flags);
1010 * Use the access and other non-mapping-related flag bits from
1011 * the memory placement flags to the current flags
1013 ttm_flag_masked(&cur_flags, place->flags, ~TTM_PL_MASK_MEMTYPE);
1015 mem->mem_type = mem_type;
1016 mem->placement = cur_flags;
1018 spin_lock(&ttm_bo_glob.lru_lock);
1019 ttm_bo_del_from_lru(bo);
1020 ttm_bo_add_mem_to_lru(bo, mem);
1021 spin_unlock(&ttm_bo_glob.lru_lock);
1027 * Creates space for memory region @mem according to its type.
1029 * This function first searches for free space in compatible memory types in
1030 * the priority order defined by the driver. If free space isn't found, then
1031 * ttm_bo_mem_force_space is attempted in priority order to evict and find
1034 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
1035 struct ttm_placement *placement,
1036 struct ttm_mem_reg *mem,
1037 struct ttm_operation_ctx *ctx)
1039 struct ttm_bo_device *bdev = bo->bdev;
1040 bool type_found = false;
1043 ret = dma_resv_reserve_shared(bo->base.resv, 1);
1047 mem->mm_node = NULL;
1048 for (i = 0; i < placement->num_placement; ++i) {
1049 const struct ttm_place *place = &placement->placement[i];
1050 struct ttm_mem_type_manager *man;
1052 ret = ttm_bo_mem_placement(bo, place, mem, ctx);
1059 mem->mm_node = NULL;
1060 if (mem->mem_type == TTM_PL_SYSTEM)
1063 man = &bdev->man[mem->mem_type];
1064 ret = (*man->func->get_node)(man, bo, place, mem);
1071 ret = ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
1072 if (unlikely(ret)) {
1073 (*man->func->put_node)(man, mem);
1082 for (i = 0; i < placement->num_busy_placement; ++i) {
1083 const struct ttm_place *place = &placement->busy_placement[i];
1085 ret = ttm_bo_mem_placement(bo, place, mem, ctx);
1092 mem->mm_node = NULL;
1093 if (mem->mem_type == TTM_PL_SYSTEM)
1096 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
1097 if (ret == 0 && mem->mm_node)
1100 if (ret && ret != -EBUSY)
1106 pr_err(TTM_PFX "No compatible memory type found\n");
1111 if (bo->mem.mem_type == TTM_PL_SYSTEM && !list_empty(&bo->lru)) {
1112 spin_lock(&ttm_bo_glob.lru_lock);
1113 ttm_bo_move_to_lru_tail(bo, NULL);
1114 spin_unlock(&ttm_bo_glob.lru_lock);
1119 EXPORT_SYMBOL(ttm_bo_mem_space);
1121 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1122 struct ttm_placement *placement,
1123 struct ttm_operation_ctx *ctx)
1126 struct ttm_mem_reg mem;
1128 dma_resv_assert_held(bo->base.resv);
1130 mem.num_pages = bo->num_pages;
1131 mem.size = mem.num_pages << PAGE_SHIFT;
1132 mem.page_alignment = bo->mem.page_alignment;
1133 mem.bus.io_reserved_vm = false;
1134 mem.bus.io_reserved_count = 0;
1136 * Determine where to move the buffer.
1138 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
1141 ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx);
1143 if (ret && mem.mm_node)
1144 ttm_bo_mem_put(bo, &mem);
1148 static bool ttm_bo_places_compat(const struct ttm_place *places,
1149 unsigned num_placement,
1150 struct ttm_mem_reg *mem,
1151 uint32_t *new_flags)
1155 for (i = 0; i < num_placement; i++) {
1156 const struct ttm_place *heap = &places[i];
1158 if (mem->mm_node && (mem->start < heap->fpfn ||
1159 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1162 *new_flags = heap->flags;
1163 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1164 (*new_flags & mem->placement & TTM_PL_MASK_MEM) &&
1165 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
1166 (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
1172 bool ttm_bo_mem_compat(struct ttm_placement *placement,
1173 struct ttm_mem_reg *mem,
1174 uint32_t *new_flags)
1176 if (ttm_bo_places_compat(placement->placement, placement->num_placement,
1180 if ((placement->busy_placement != placement->placement ||
1181 placement->num_busy_placement > placement->num_placement) &&
1182 ttm_bo_places_compat(placement->busy_placement,
1183 placement->num_busy_placement,
1189 EXPORT_SYMBOL(ttm_bo_mem_compat);
1191 int ttm_bo_validate(struct ttm_buffer_object *bo,
1192 struct ttm_placement *placement,
1193 struct ttm_operation_ctx *ctx)
1198 dma_resv_assert_held(bo->base.resv);
1200 * Check whether we need to move buffer.
1202 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1203 ret = ttm_bo_move_buffer(bo, placement, ctx);
1208 * Use the access and other non-mapping-related flag bits from
1209 * the compatible memory placement flags to the active flags
1211 ttm_flag_masked(&bo->mem.placement, new_flags,
1212 ~TTM_PL_MASK_MEMTYPE);
1215 * We might need to add a TTM.
1217 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1218 ret = ttm_tt_create(bo, true);
1224 EXPORT_SYMBOL(ttm_bo_validate);
1226 int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
1227 struct ttm_buffer_object *bo,
1229 enum ttm_bo_type type,
1230 struct ttm_placement *placement,
1231 uint32_t page_alignment,
1232 struct ttm_operation_ctx *ctx,
1234 struct sg_table *sg,
1235 struct dma_resv *resv,
1236 void (*destroy) (struct ttm_buffer_object *))
1238 struct ttm_mem_global *mem_glob = &ttm_mem_glob;
1240 unsigned long num_pages;
1243 ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
1245 pr_err("Out of kernel memory\n");
1253 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1254 if (num_pages == 0) {
1255 pr_err("Illegal buffer object size\n");
1260 ttm_mem_global_free(mem_glob, acc_size);
1263 bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1265 kref_init(&bo->kref);
1266 INIT_LIST_HEAD(&bo->lru);
1267 INIT_LIST_HEAD(&bo->ddestroy);
1268 INIT_LIST_HEAD(&bo->swap);
1269 INIT_LIST_HEAD(&bo->io_reserve_lru);
1272 bo->num_pages = num_pages;
1273 bo->mem.size = num_pages << PAGE_SHIFT;
1274 bo->mem.mem_type = TTM_PL_SYSTEM;
1275 bo->mem.num_pages = bo->num_pages;
1276 bo->mem.mm_node = NULL;
1277 bo->mem.page_alignment = page_alignment;
1278 bo->mem.bus.io_reserved_vm = false;
1279 bo->mem.bus.io_reserved_count = 0;
1281 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1282 bo->acc_size = acc_size;
1285 bo->base.resv = resv;
1286 dma_resv_assert_held(bo->base.resv);
1288 bo->base.resv = &bo->base._resv;
1290 if (!ttm_bo_uses_embedded_gem_object(bo)) {
1292 * bo.gem is not initialized, so we have to setup the
1293 * struct elements we want use regardless.
1295 dma_resv_init(&bo->base._resv);
1296 drm_vma_node_reset(&bo->base.vma_node);
1298 atomic_inc(&ttm_bo_glob.bo_count);
1301 * For ttm_bo_type_device buffers, allocate
1302 * address space from the device.
1304 if (bo->type == ttm_bo_type_device ||
1305 bo->type == ttm_bo_type_sg)
1306 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
1309 /* passed reservation objects should already be locked,
1310 * since otherwise lockdep will be angered in radeon.
1313 locked = dma_resv_trylock(bo->base.resv);
1318 ret = ttm_bo_validate(bo, placement, ctx);
1320 if (unlikely(ret)) {
1322 ttm_bo_unreserve(bo);
1328 spin_lock(&ttm_bo_glob.lru_lock);
1329 ttm_bo_move_to_lru_tail(bo, NULL);
1330 spin_unlock(&ttm_bo_glob.lru_lock);
1334 EXPORT_SYMBOL(ttm_bo_init_reserved);
1336 int ttm_bo_init(struct ttm_bo_device *bdev,
1337 struct ttm_buffer_object *bo,
1339 enum ttm_bo_type type,
1340 struct ttm_placement *placement,
1341 uint32_t page_alignment,
1344 struct sg_table *sg,
1345 struct dma_resv *resv,
1346 void (*destroy) (struct ttm_buffer_object *))
1348 struct ttm_operation_ctx ctx = { interruptible, false };
1351 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1352 page_alignment, &ctx, acc_size,
1358 ttm_bo_unreserve(bo);
1362 EXPORT_SYMBOL(ttm_bo_init);
1364 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1365 unsigned long bo_size,
1366 unsigned struct_size)
1368 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1371 size += ttm_round_pot(struct_size);
1372 size += ttm_round_pot(npages * sizeof(void *));
1373 size += ttm_round_pot(sizeof(struct ttm_tt));
1376 EXPORT_SYMBOL(ttm_bo_acc_size);
1378 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1379 unsigned long bo_size,
1380 unsigned struct_size)
1382 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1385 size += ttm_round_pot(struct_size);
1386 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
1387 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1390 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1392 int ttm_bo_create(struct ttm_bo_device *bdev,
1394 enum ttm_bo_type type,
1395 struct ttm_placement *placement,
1396 uint32_t page_alignment,
1398 struct ttm_buffer_object **p_bo)
1400 struct ttm_buffer_object *bo;
1404 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1405 if (unlikely(bo == NULL))
1408 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1409 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1410 interruptible, acc_size,
1412 if (likely(ret == 0))
1417 EXPORT_SYMBOL(ttm_bo_create);
1419 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1422 struct ttm_operation_ctx ctx = {
1423 .interruptible = false,
1424 .no_wait_gpu = false,
1425 .flags = TTM_OPT_FLAG_FORCE_ALLOC
1427 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1428 struct ttm_bo_global *glob = &ttm_bo_glob;
1429 struct dma_fence *fence;
1434 * Can't use standard list traversal since we're unlocking.
1437 spin_lock(&glob->lru_lock);
1438 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1439 while (!list_empty(&man->lru[i])) {
1440 spin_unlock(&glob->lru_lock);
1441 ret = ttm_mem_evict_first(bdev, mem_type, NULL, &ctx,
1445 spin_lock(&glob->lru_lock);
1448 spin_unlock(&glob->lru_lock);
1450 spin_lock(&man->move_lock);
1451 fence = dma_fence_get(man->move);
1452 spin_unlock(&man->move_lock);
1455 ret = dma_fence_wait(fence, false);
1456 dma_fence_put(fence);
1464 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1466 struct ttm_mem_type_manager *man;
1469 if (mem_type >= TTM_NUM_MEM_TYPES) {
1470 pr_err("Illegal memory type %d\n", mem_type);
1473 man = &bdev->man[mem_type];
1475 if (!man->has_type) {
1476 pr_err("Trying to take down uninitialized memory manager type %u\n",
1481 man->use_type = false;
1482 man->has_type = false;
1486 ret = ttm_bo_force_list_clean(bdev, mem_type);
1488 pr_err("Cleanup eviction failed\n");
1492 ret = (*man->func->takedown)(man);
1495 dma_fence_put(man->move);
1500 EXPORT_SYMBOL(ttm_bo_clean_mm);
1502 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1504 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1506 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1507 pr_err("Illegal memory manager memory type %u\n", mem_type);
1511 if (!man->has_type) {
1512 pr_err("Memory type %u has not been initialized\n", mem_type);
1516 return ttm_bo_force_list_clean(bdev, mem_type);
1518 EXPORT_SYMBOL(ttm_bo_evict_mm);
1520 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1521 unsigned long p_size)
1524 struct ttm_mem_type_manager *man;
1527 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1528 man = &bdev->man[type];
1529 BUG_ON(man->has_type);
1530 man->io_reserve_fastpath = true;
1531 man->use_io_reserve_lru = false;
1532 mutex_init(&man->io_reserve_mutex);
1533 spin_lock_init(&man->move_lock);
1534 INIT_LIST_HEAD(&man->io_reserve_lru);
1536 ret = bdev->driver->init_mem_type(bdev, type, man);
1541 if (type != TTM_PL_SYSTEM) {
1542 ret = (*man->func->init)(man, p_size);
1546 man->has_type = true;
1547 man->use_type = true;
1550 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1551 INIT_LIST_HEAD(&man->lru[i]);
1556 EXPORT_SYMBOL(ttm_bo_init_mm);
1558 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1560 struct ttm_bo_global *glob =
1561 container_of(kobj, struct ttm_bo_global, kobj);
1563 __free_page(glob->dummy_read_page);
1566 static void ttm_bo_global_release(void)
1568 struct ttm_bo_global *glob = &ttm_bo_glob;
1570 mutex_lock(&ttm_global_mutex);
1571 if (--ttm_bo_glob_use_count > 0)
1574 kobject_del(&glob->kobj);
1575 kobject_put(&glob->kobj);
1576 ttm_mem_global_release(&ttm_mem_glob);
1577 memset(glob, 0, sizeof(*glob));
1579 mutex_unlock(&ttm_global_mutex);
1582 static int ttm_bo_global_init(void)
1584 struct ttm_bo_global *glob = &ttm_bo_glob;
1588 mutex_lock(&ttm_global_mutex);
1589 if (++ttm_bo_glob_use_count > 1)
1592 ret = ttm_mem_global_init(&ttm_mem_glob);
1596 spin_lock_init(&glob->lru_lock);
1597 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1599 if (unlikely(glob->dummy_read_page == NULL)) {
1604 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1605 INIT_LIST_HEAD(&glob->swap_lru[i]);
1606 INIT_LIST_HEAD(&glob->device_list);
1607 atomic_set(&glob->bo_count, 0);
1609 ret = kobject_init_and_add(
1610 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1611 if (unlikely(ret != 0))
1612 kobject_put(&glob->kobj);
1614 mutex_unlock(&ttm_global_mutex);
1618 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1620 struct ttm_bo_global *glob = &ttm_bo_glob;
1622 unsigned i = TTM_NUM_MEM_TYPES;
1623 struct ttm_mem_type_manager *man;
1626 man = &bdev->man[i];
1627 if (man->has_type) {
1628 man->use_type = false;
1629 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1631 pr_err("DRM memory manager type %d is not clean\n",
1634 man->has_type = false;
1638 mutex_lock(&ttm_global_mutex);
1639 list_del(&bdev->device_list);
1640 mutex_unlock(&ttm_global_mutex);
1642 cancel_delayed_work_sync(&bdev->wq);
1644 if (ttm_bo_delayed_delete(bdev, true))
1645 pr_debug("Delayed destroy list was clean\n");
1647 spin_lock(&glob->lru_lock);
1648 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1649 if (list_empty(&bdev->man[0].lru[0]))
1650 pr_debug("Swap list %d was clean\n", i);
1651 spin_unlock(&glob->lru_lock);
1654 ttm_bo_global_release();
1658 EXPORT_SYMBOL(ttm_bo_device_release);
1660 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1661 struct ttm_bo_driver *driver,
1662 struct address_space *mapping,
1663 struct drm_vma_offset_manager *vma_manager,
1666 struct ttm_bo_global *glob = &ttm_bo_glob;
1669 if (WARN_ON(vma_manager == NULL))
1672 ret = ttm_bo_global_init();
1676 bdev->driver = driver;
1678 memset(bdev->man, 0, sizeof(bdev->man));
1681 * Initialize the system memory buffer type.
1682 * Other types need to be driver / IOCTL initialized.
1684 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1685 if (unlikely(ret != 0))
1688 bdev->vma_manager = vma_manager;
1689 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1690 INIT_LIST_HEAD(&bdev->ddestroy);
1691 bdev->dev_mapping = mapping;
1692 bdev->need_dma32 = need_dma32;
1693 mutex_lock(&ttm_global_mutex);
1694 list_add_tail(&bdev->device_list, &glob->device_list);
1695 mutex_unlock(&ttm_global_mutex);
1699 ttm_bo_global_release();
1702 EXPORT_SYMBOL(ttm_bo_device_init);
1705 * buffer object vm functions.
1708 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1710 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1712 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1713 if (mem->mem_type == TTM_PL_SYSTEM)
1716 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1719 if (mem->placement & TTM_PL_FLAG_CACHED)
1725 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1727 struct ttm_bo_device *bdev = bo->bdev;
1729 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1730 ttm_mem_io_free_vm(bo);
1733 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1735 struct ttm_bo_device *bdev = bo->bdev;
1736 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1738 ttm_mem_io_lock(man, false);
1739 ttm_bo_unmap_virtual_locked(bo);
1740 ttm_mem_io_unlock(man);
1744 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1746 int ttm_bo_wait(struct ttm_buffer_object *bo,
1747 bool interruptible, bool no_wait)
1749 long timeout = 15 * HZ;
1752 if (dma_resv_test_signaled_rcu(bo->base.resv, true))
1758 timeout = dma_resv_wait_timeout_rcu(bo->base.resv, true,
1759 interruptible, timeout);
1766 dma_resv_add_excl_fence(bo->base.resv, NULL);
1769 EXPORT_SYMBOL(ttm_bo_wait);
1772 * A buffer object shrink method that tries to swap out the first
1773 * buffer object on the bo_global::swap_lru list.
1775 int ttm_bo_swapout(struct ttm_bo_global *glob, struct ttm_operation_ctx *ctx)
1777 struct ttm_buffer_object *bo;
1782 spin_lock(&glob->lru_lock);
1783 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1784 list_for_each_entry(bo, &glob->swap_lru[i], swap) {
1785 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
1789 if (!ttm_bo_get_unless_zero(bo)) {
1791 dma_resv_unlock(bo->base.resv);
1803 spin_unlock(&glob->lru_lock);
1808 ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1813 ttm_bo_del_from_lru(bo);
1814 spin_unlock(&glob->lru_lock);
1817 * Move to system cached
1820 if (bo->mem.mem_type != TTM_PL_SYSTEM ||
1821 bo->ttm->caching_state != tt_cached) {
1822 struct ttm_operation_ctx ctx = { false, false };
1823 struct ttm_mem_reg evict_mem;
1825 evict_mem = bo->mem;
1826 evict_mem.mm_node = NULL;
1827 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1828 evict_mem.mem_type = TTM_PL_SYSTEM;
1830 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx);
1831 if (unlikely(ret != 0))
1836 * Make sure BO is idle.
1839 ret = ttm_bo_wait(bo, false, false);
1840 if (unlikely(ret != 0))
1843 ttm_bo_unmap_virtual(bo);
1846 * Swap out. Buffer will be swapped in again as soon as
1847 * anyone tries to access a ttm page.
1850 if (bo->bdev->driver->swap_notify)
1851 bo->bdev->driver->swap_notify(bo);
1853 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1858 * Unreserve without putting on LRU to avoid swapping out an
1859 * already swapped buffer.
1862 dma_resv_unlock(bo->base.resv);
1866 EXPORT_SYMBOL(ttm_bo_swapout);
1868 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1870 struct ttm_operation_ctx ctx = {
1871 .interruptible = false,
1872 .no_wait_gpu = false
1875 while (ttm_bo_swapout(&ttm_bo_glob, &ctx) == 0);
1877 EXPORT_SYMBOL(ttm_bo_swapout_all);