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 if (!list_empty(&bo->lru))
157 if (mem->placement & TTM_PL_FLAG_NO_EVICT)
160 man = &bdev->man[mem->mem_type];
161 list_add_tail(&bo->lru, &man->lru[bo->priority]);
163 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm &&
164 !(bo->ttm->page_flags & (TTM_PAGE_FLAG_SG |
165 TTM_PAGE_FLAG_SWAPPED))) {
166 list_add_tail(&bo->swap, &ttm_bo_glob.swap_lru[bo->priority]);
170 static void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
172 struct ttm_bo_device *bdev = bo->bdev;
175 if (!list_empty(&bo->swap)) {
176 list_del_init(&bo->swap);
179 if (!list_empty(&bo->lru)) {
180 list_del_init(&bo->lru);
184 if (notify && bdev->driver->del_from_lru_notify)
185 bdev->driver->del_from_lru_notify(bo);
188 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
189 struct ttm_buffer_object *bo)
196 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
197 struct ttm_lru_bulk_move *bulk)
199 dma_resv_assert_held(bo->base.resv);
201 ttm_bo_del_from_lru(bo);
202 ttm_bo_add_mem_to_lru(bo, &bo->mem);
204 if (bulk && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
205 switch (bo->mem.mem_type) {
207 ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
211 ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
214 if (bo->ttm && !(bo->ttm->page_flags &
215 (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED)))
216 ttm_bo_bulk_move_set_pos(&bulk->swap[bo->priority], bo);
219 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
221 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
225 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
226 struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
227 struct ttm_mem_type_manager *man;
232 dma_resv_assert_held(pos->first->base.resv);
233 dma_resv_assert_held(pos->last->base.resv);
235 man = &pos->first->bdev->man[TTM_PL_TT];
236 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
240 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
241 struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
242 struct ttm_mem_type_manager *man;
247 dma_resv_assert_held(pos->first->base.resv);
248 dma_resv_assert_held(pos->last->base.resv);
250 man = &pos->first->bdev->man[TTM_PL_VRAM];
251 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
255 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
256 struct ttm_lru_bulk_move_pos *pos = &bulk->swap[i];
257 struct list_head *lru;
262 dma_resv_assert_held(pos->first->base.resv);
263 dma_resv_assert_held(pos->last->base.resv);
265 lru = &ttm_bo_glob.swap_lru[i];
266 list_bulk_move_tail(lru, &pos->first->swap, &pos->last->swap);
269 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
271 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
272 struct ttm_mem_reg *mem, bool evict,
273 struct ttm_operation_ctx *ctx)
275 struct ttm_bo_device *bdev = bo->bdev;
276 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
277 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
278 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
279 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
282 if (old_is_pci || new_is_pci ||
283 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
284 ret = ttm_mem_io_lock(old_man, true);
285 if (unlikely(ret != 0))
287 ttm_bo_unmap_virtual_locked(bo);
288 ttm_mem_io_unlock(old_man);
292 * Create and bind a ttm if required.
295 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
296 if (bo->ttm == NULL) {
297 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
298 ret = ttm_tt_create(bo, zero);
303 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
307 if (mem->mem_type != TTM_PL_SYSTEM) {
308 ret = ttm_tt_bind(bo->ttm, mem, ctx);
313 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
314 if (bdev->driver->move_notify)
315 bdev->driver->move_notify(bo, evict, mem);
322 if (bdev->driver->move_notify)
323 bdev->driver->move_notify(bo, evict, mem);
325 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
326 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
327 ret = ttm_bo_move_ttm(bo, ctx, mem);
328 else if (bdev->driver->move)
329 ret = bdev->driver->move(bo, evict, ctx, mem);
331 ret = ttm_bo_move_memcpy(bo, ctx, mem);
334 if (bdev->driver->move_notify) {
336 bdev->driver->move_notify(bo, false, mem);
347 bo->offset = (bo->mem.start << PAGE_SHIFT) +
348 bdev->man[bo->mem.mem_type].gpu_offset;
352 ctx->bytes_moved += bo->num_pages << PAGE_SHIFT;
356 new_man = &bdev->man[bo->mem.mem_type];
357 if (new_man->flags & TTM_MEMTYPE_FLAG_FIXED) {
358 ttm_tt_destroy(bo->ttm);
367 * Will release GPU memory type usage on destruction.
368 * This is the place to put in driver specific hooks to release
369 * driver private resources.
370 * Will release the bo::reserved lock.
373 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
375 if (bo->bdev->driver->move_notify)
376 bo->bdev->driver->move_notify(bo, false, NULL);
378 ttm_tt_destroy(bo->ttm);
380 ttm_bo_mem_put(bo, &bo->mem);
383 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
387 if (bo->base.resv == &bo->base._resv)
390 BUG_ON(!dma_resv_trylock(&bo->base._resv));
392 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
393 dma_resv_unlock(&bo->base._resv);
397 if (bo->type != ttm_bo_type_sg) {
398 /* This works because the BO is about to be destroyed and nobody
399 * reference it any more. The only tricky case is the trylock on
400 * the resv object while holding the lru_lock.
402 spin_lock(&ttm_bo_glob.lru_lock);
403 bo->base.resv = &bo->base._resv;
404 spin_unlock(&ttm_bo_glob.lru_lock);
410 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
412 struct dma_resv *resv = &bo->base._resv;
413 struct dma_resv_list *fobj;
414 struct dma_fence *fence;
418 fobj = rcu_dereference(resv->fence);
419 fence = rcu_dereference(resv->fence_excl);
420 if (fence && !fence->ops->signaled)
421 dma_fence_enable_sw_signaling(fence);
423 for (i = 0; fobj && i < fobj->shared_count; ++i) {
424 fence = rcu_dereference(fobj->shared[i]);
426 if (!fence->ops->signaled)
427 dma_fence_enable_sw_signaling(fence);
433 * function ttm_bo_cleanup_refs
434 * If bo idle, remove from lru lists, and unref.
435 * If not idle, block if possible.
437 * Must be called with lru_lock and reservation held, this function
438 * will drop the lru lock and optionally the reservation lock before returning.
440 * @interruptible Any sleeps should occur interruptibly.
441 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
442 * @unlock_resv Unlock the reservation lock as well.
445 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
446 bool interruptible, bool no_wait_gpu,
449 struct dma_resv *resv = &bo->base._resv;
452 if (dma_resv_test_signaled_rcu(resv, true))
457 if (ret && !no_wait_gpu) {
461 dma_resv_unlock(bo->base.resv);
462 spin_unlock(&ttm_bo_glob.lru_lock);
464 lret = dma_resv_wait_timeout_rcu(resv, true, interruptible,
472 spin_lock(&ttm_bo_glob.lru_lock);
473 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
475 * We raced, and lost, someone else holds the reservation now,
476 * and is probably busy in ttm_bo_cleanup_memtype_use.
478 * Even if it's not the case, because we finished waiting any
479 * delayed destruction would succeed, so just return success
482 spin_unlock(&ttm_bo_glob.lru_lock);
488 if (ret || unlikely(list_empty(&bo->ddestroy))) {
490 dma_resv_unlock(bo->base.resv);
491 spin_unlock(&ttm_bo_glob.lru_lock);
495 ttm_bo_del_from_lru(bo);
496 list_del_init(&bo->ddestroy);
497 spin_unlock(&ttm_bo_glob.lru_lock);
498 ttm_bo_cleanup_memtype_use(bo);
501 dma_resv_unlock(bo->base.resv);
509 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
510 * encountered buffers.
512 static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
514 struct ttm_bo_global *glob = &ttm_bo_glob;
515 struct list_head removed;
518 INIT_LIST_HEAD(&removed);
520 spin_lock(&glob->lru_lock);
521 while (!list_empty(&bdev->ddestroy)) {
522 struct ttm_buffer_object *bo;
524 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
526 list_move_tail(&bo->ddestroy, &removed);
527 if (!ttm_bo_get_unless_zero(bo))
530 if (remove_all || bo->base.resv != &bo->base._resv) {
531 spin_unlock(&glob->lru_lock);
532 dma_resv_lock(bo->base.resv, NULL);
534 spin_lock(&glob->lru_lock);
535 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
537 } else if (dma_resv_trylock(bo->base.resv)) {
538 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
540 spin_unlock(&glob->lru_lock);
544 spin_lock(&glob->lru_lock);
546 list_splice_tail(&removed, &bdev->ddestroy);
547 empty = list_empty(&bdev->ddestroy);
548 spin_unlock(&glob->lru_lock);
553 static void ttm_bo_delayed_workqueue(struct work_struct *work)
555 struct ttm_bo_device *bdev =
556 container_of(work, struct ttm_bo_device, wq.work);
558 if (!ttm_bo_delayed_delete(bdev, false))
559 schedule_delayed_work(&bdev->wq,
560 ((HZ / 100) < 1) ? 1 : HZ / 100);
563 static void ttm_bo_release(struct kref *kref)
565 struct ttm_buffer_object *bo =
566 container_of(kref, struct ttm_buffer_object, kref);
567 struct ttm_bo_device *bdev = bo->bdev;
568 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
569 size_t acc_size = bo->acc_size;
573 ret = ttm_bo_individualize_resv(bo);
575 /* Last resort, if we fail to allocate memory for the
576 * fences block for the BO to become idle
578 dma_resv_wait_timeout_rcu(bo->base.resv, true, false,
582 if (bo->bdev->driver->release_notify)
583 bo->bdev->driver->release_notify(bo);
585 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
586 ttm_mem_io_lock(man, false);
587 ttm_mem_io_free_vm(bo);
588 ttm_mem_io_unlock(man);
591 if (!dma_resv_test_signaled_rcu(bo->base.resv, true) ||
592 !dma_resv_trylock(bo->base.resv)) {
593 /* The BO is not idle, resurrect it for delayed destroy */
594 ttm_bo_flush_all_fences(bo);
597 spin_lock(&ttm_bo_glob.lru_lock);
600 * Make NO_EVICT bos immediately available to
601 * shrinkers, now that they are queued for
604 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
605 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
606 ttm_bo_del_from_lru(bo);
607 ttm_bo_add_mem_to_lru(bo, &bo->mem);
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);
625 dma_resv_unlock(bo->base.resv);
627 BUG_ON(bo->mem.mm_node != NULL);
628 atomic_dec(&ttm_bo_glob.bo_count);
629 dma_fence_put(bo->moving);
630 if (!ttm_bo_uses_embedded_gem_object(bo))
631 dma_resv_fini(&bo->base._resv);
633 ttm_mem_global_free(&ttm_mem_glob, acc_size);
636 void ttm_bo_put(struct ttm_buffer_object *bo)
638 kref_put(&bo->kref, ttm_bo_release);
640 EXPORT_SYMBOL(ttm_bo_put);
642 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
644 return cancel_delayed_work_sync(&bdev->wq);
646 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
648 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
651 schedule_delayed_work(&bdev->wq,
652 ((HZ / 100) < 1) ? 1 : HZ / 100);
654 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
656 static int ttm_bo_evict(struct ttm_buffer_object *bo,
657 struct ttm_operation_ctx *ctx)
659 struct ttm_bo_device *bdev = bo->bdev;
660 struct ttm_mem_reg evict_mem;
661 struct ttm_placement placement;
664 dma_resv_assert_held(bo->base.resv);
666 placement.num_placement = 0;
667 placement.num_busy_placement = 0;
668 bdev->driver->evict_flags(bo, &placement);
670 if (!placement.num_placement && !placement.num_busy_placement) {
671 ret = ttm_bo_pipeline_gutting(bo);
675 return ttm_tt_create(bo, false);
679 evict_mem.mm_node = NULL;
680 evict_mem.bus.io_reserved_vm = false;
681 evict_mem.bus.io_reserved_count = 0;
683 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
685 if (ret != -ERESTARTSYS) {
686 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
688 ttm_bo_mem_space_debug(bo, &placement);
693 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx);
695 if (ret != -ERESTARTSYS)
696 pr_err("Buffer eviction failed\n");
697 ttm_bo_mem_put(bo, &evict_mem);
705 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
706 const struct ttm_place *place)
708 /* Don't evict this BO if it's outside of the
709 * requested placement range
711 if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
712 (place->lpfn && place->lpfn <= bo->mem.start))
717 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
720 * Check the target bo is allowable to be evicted or swapout, including cases:
722 * a. if share same reservation object with ctx->resv, have assumption
723 * reservation objects should already be locked, so not lock again and
724 * return true directly when either the opreation allow_reserved_eviction
725 * or the target bo already is in delayed free list;
727 * b. Otherwise, trylock it.
729 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
730 struct ttm_operation_ctx *ctx, bool *locked, bool *busy)
734 if (bo->base.resv == ctx->resv) {
735 dma_resv_assert_held(bo->base.resv);
736 if (ctx->flags & TTM_OPT_FLAG_ALLOW_RES_EVICT)
742 ret = dma_resv_trylock(bo->base.resv);
752 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
754 * @busy_bo: BO which couldn't be locked with trylock
755 * @ctx: operation context
756 * @ticket: acquire ticket
758 * Try to lock a busy buffer object to avoid failing eviction.
760 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
761 struct ttm_operation_ctx *ctx,
762 struct ww_acquire_ctx *ticket)
766 if (!busy_bo || !ticket)
769 if (ctx->interruptible)
770 r = dma_resv_lock_interruptible(busy_bo->base.resv,
773 r = dma_resv_lock(busy_bo->base.resv, ticket);
776 * TODO: It would be better to keep the BO locked until allocation is at
777 * least tried one more time, but that would mean a much larger rework
781 dma_resv_unlock(busy_bo->base.resv);
783 return r == -EDEADLK ? -EBUSY : r;
786 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
788 const struct ttm_place *place,
789 struct ttm_operation_ctx *ctx,
790 struct ww_acquire_ctx *ticket)
792 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
793 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
798 spin_lock(&ttm_bo_glob.lru_lock);
799 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
800 list_for_each_entry(bo, &man->lru[i], lru) {
803 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
805 if (busy && !busy_bo && ticket !=
806 dma_resv_locking_ctx(bo->base.resv))
811 if (place && !bdev->driver->eviction_valuable(bo,
814 dma_resv_unlock(bo->base.resv);
817 if (!ttm_bo_get_unless_zero(bo)) {
819 dma_resv_unlock(bo->base.resv);
825 /* If the inner loop terminated early, we have our candidate */
826 if (&bo->lru != &man->lru[i])
833 if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
835 spin_unlock(&ttm_bo_glob.lru_lock);
836 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
843 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
844 ctx->no_wait_gpu, locked);
849 spin_unlock(&ttm_bo_glob.lru_lock);
851 ret = ttm_bo_evict(bo, ctx);
853 ttm_bo_unreserve(bo);
859 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
861 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
864 (*man->func->put_node)(man, mem);
866 EXPORT_SYMBOL(ttm_bo_mem_put);
869 * Add the last move fence to the BO and reserve a new shared slot.
871 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
872 struct ttm_mem_type_manager *man,
873 struct ttm_mem_reg *mem,
876 struct dma_fence *fence;
879 spin_lock(&man->move_lock);
880 fence = dma_fence_get(man->move);
881 spin_unlock(&man->move_lock);
889 dma_resv_add_shared_fence(bo->base.resv, fence);
891 ret = dma_resv_reserve_shared(bo->base.resv, 1);
893 dma_fence_put(fence);
897 dma_fence_put(bo->moving);
903 * Repeatedly evict memory from the LRU for @mem_type until we create enough
904 * space, or we've evicted everything and there isn't enough space.
906 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
907 const struct ttm_place *place,
908 struct ttm_mem_reg *mem,
909 struct ttm_operation_ctx *ctx)
911 struct ttm_bo_device *bdev = bo->bdev;
912 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
913 struct ww_acquire_ctx *ticket;
916 ticket = dma_resv_locking_ctx(bo->base.resv);
918 ret = (*man->func->get_node)(man, bo, place, mem);
919 if (unlikely(ret != 0))
923 ret = ttm_mem_evict_first(bdev, mem->mem_type, place, ctx,
925 if (unlikely(ret != 0))
929 return ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
932 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
933 uint32_t cur_placement,
934 uint32_t proposed_placement)
936 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
937 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
940 * Keep current caching if possible.
943 if ((cur_placement & caching) != 0)
944 result |= (cur_placement & caching);
945 else if ((man->default_caching & caching) != 0)
946 result |= man->default_caching;
947 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
948 result |= TTM_PL_FLAG_CACHED;
949 else if ((TTM_PL_FLAG_WC & caching) != 0)
950 result |= TTM_PL_FLAG_WC;
951 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
952 result |= TTM_PL_FLAG_UNCACHED;
957 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
959 const struct ttm_place *place,
960 uint32_t *masked_placement)
962 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
964 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
967 if ((place->flags & man->available_caching) == 0)
970 cur_flags |= (place->flags & man->available_caching);
972 *masked_placement = cur_flags;
977 * ttm_bo_mem_placement - check if placement is compatible
978 * @bo: BO to find memory for
979 * @place: where to search
980 * @mem: the memory object to fill in
981 * @ctx: operation context
983 * Check if placement is compatible and fill in mem structure.
984 * Returns -EBUSY if placement won't work or negative error code.
985 * 0 when placement can be used.
987 static int ttm_bo_mem_placement(struct ttm_buffer_object *bo,
988 const struct ttm_place *place,
989 struct ttm_mem_reg *mem,
990 struct ttm_operation_ctx *ctx)
992 struct ttm_bo_device *bdev = bo->bdev;
993 uint32_t mem_type = TTM_PL_SYSTEM;
994 struct ttm_mem_type_manager *man;
995 uint32_t cur_flags = 0;
998 ret = ttm_mem_type_from_place(place, &mem_type);
1002 man = &bdev->man[mem_type];
1003 if (!man->has_type || !man->use_type)
1006 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
1009 cur_flags = ttm_bo_select_caching(man, bo->mem.placement, cur_flags);
1011 * Use the access and other non-mapping-related flag bits from
1012 * the memory placement flags to the current flags
1014 ttm_flag_masked(&cur_flags, place->flags, ~TTM_PL_MASK_MEMTYPE);
1016 mem->mem_type = mem_type;
1017 mem->placement = cur_flags;
1019 spin_lock(&ttm_bo_glob.lru_lock);
1020 ttm_bo_del_from_lru(bo);
1021 ttm_bo_add_mem_to_lru(bo, mem);
1022 spin_unlock(&ttm_bo_glob.lru_lock);
1028 * Creates space for memory region @mem according to its type.
1030 * This function first searches for free space in compatible memory types in
1031 * the priority order defined by the driver. If free space isn't found, then
1032 * ttm_bo_mem_force_space is attempted in priority order to evict and find
1035 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
1036 struct ttm_placement *placement,
1037 struct ttm_mem_reg *mem,
1038 struct ttm_operation_ctx *ctx)
1040 struct ttm_bo_device *bdev = bo->bdev;
1041 bool type_found = false;
1044 ret = dma_resv_reserve_shared(bo->base.resv, 1);
1048 mem->mm_node = NULL;
1049 for (i = 0; i < placement->num_placement; ++i) {
1050 const struct ttm_place *place = &placement->placement[i];
1051 struct ttm_mem_type_manager *man;
1053 ret = ttm_bo_mem_placement(bo, place, mem, ctx);
1060 mem->mm_node = NULL;
1061 if (mem->mem_type == TTM_PL_SYSTEM)
1064 man = &bdev->man[mem->mem_type];
1065 ret = (*man->func->get_node)(man, bo, place, mem);
1072 ret = ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
1073 if (unlikely(ret)) {
1074 (*man->func->put_node)(man, mem);
1083 for (i = 0; i < placement->num_busy_placement; ++i) {
1084 const struct ttm_place *place = &placement->busy_placement[i];
1086 ret = ttm_bo_mem_placement(bo, place, mem, ctx);
1093 mem->mm_node = NULL;
1094 if (mem->mem_type == TTM_PL_SYSTEM)
1097 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
1098 if (ret == 0 && mem->mm_node)
1101 if (ret && ret != -EBUSY)
1107 pr_err(TTM_PFX "No compatible memory type found\n");
1112 if (bo->mem.mem_type == TTM_PL_SYSTEM && !list_empty(&bo->lru)) {
1113 spin_lock(&ttm_bo_glob.lru_lock);
1114 ttm_bo_move_to_lru_tail(bo, NULL);
1115 spin_unlock(&ttm_bo_glob.lru_lock);
1120 EXPORT_SYMBOL(ttm_bo_mem_space);
1122 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1123 struct ttm_placement *placement,
1124 struct ttm_operation_ctx *ctx)
1127 struct ttm_mem_reg mem;
1129 dma_resv_assert_held(bo->base.resv);
1131 mem.num_pages = bo->num_pages;
1132 mem.size = mem.num_pages << PAGE_SHIFT;
1133 mem.page_alignment = bo->mem.page_alignment;
1134 mem.bus.io_reserved_vm = false;
1135 mem.bus.io_reserved_count = 0;
1137 * Determine where to move the buffer.
1139 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
1142 ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx);
1144 if (ret && mem.mm_node)
1145 ttm_bo_mem_put(bo, &mem);
1149 static bool ttm_bo_places_compat(const struct ttm_place *places,
1150 unsigned num_placement,
1151 struct ttm_mem_reg *mem,
1152 uint32_t *new_flags)
1156 for (i = 0; i < num_placement; i++) {
1157 const struct ttm_place *heap = &places[i];
1159 if (mem->mm_node && (mem->start < heap->fpfn ||
1160 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1163 *new_flags = heap->flags;
1164 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1165 (*new_flags & mem->placement & TTM_PL_MASK_MEM) &&
1166 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
1167 (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
1173 bool ttm_bo_mem_compat(struct ttm_placement *placement,
1174 struct ttm_mem_reg *mem,
1175 uint32_t *new_flags)
1177 if (ttm_bo_places_compat(placement->placement, placement->num_placement,
1181 if ((placement->busy_placement != placement->placement ||
1182 placement->num_busy_placement > placement->num_placement) &&
1183 ttm_bo_places_compat(placement->busy_placement,
1184 placement->num_busy_placement,
1190 EXPORT_SYMBOL(ttm_bo_mem_compat);
1192 int ttm_bo_validate(struct ttm_buffer_object *bo,
1193 struct ttm_placement *placement,
1194 struct ttm_operation_ctx *ctx)
1199 dma_resv_assert_held(bo->base.resv);
1202 * Remove the backing store if no placement is given.
1204 if (!placement->num_placement && !placement->num_busy_placement) {
1205 ret = ttm_bo_pipeline_gutting(bo);
1209 return ttm_tt_create(bo, false);
1213 * Check whether we need to move buffer.
1215 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1216 ret = ttm_bo_move_buffer(bo, placement, ctx);
1221 * Use the access and other non-mapping-related flag bits from
1222 * the compatible memory placement flags to the active flags
1224 ttm_flag_masked(&bo->mem.placement, new_flags,
1225 ~TTM_PL_MASK_MEMTYPE);
1228 * We might need to add a TTM.
1230 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1231 ret = ttm_tt_create(bo, true);
1237 EXPORT_SYMBOL(ttm_bo_validate);
1239 int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
1240 struct ttm_buffer_object *bo,
1242 enum ttm_bo_type type,
1243 struct ttm_placement *placement,
1244 uint32_t page_alignment,
1245 struct ttm_operation_ctx *ctx,
1247 struct sg_table *sg,
1248 struct dma_resv *resv,
1249 void (*destroy) (struct ttm_buffer_object *))
1251 struct ttm_mem_global *mem_glob = &ttm_mem_glob;
1253 unsigned long num_pages;
1256 ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
1258 pr_err("Out of kernel memory\n");
1266 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1267 if (num_pages == 0) {
1268 pr_err("Illegal buffer object size\n");
1273 ttm_mem_global_free(mem_glob, acc_size);
1276 bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1278 kref_init(&bo->kref);
1279 INIT_LIST_HEAD(&bo->lru);
1280 INIT_LIST_HEAD(&bo->ddestroy);
1281 INIT_LIST_HEAD(&bo->swap);
1282 INIT_LIST_HEAD(&bo->io_reserve_lru);
1285 bo->num_pages = num_pages;
1286 bo->mem.size = num_pages << PAGE_SHIFT;
1287 bo->mem.mem_type = TTM_PL_SYSTEM;
1288 bo->mem.num_pages = bo->num_pages;
1289 bo->mem.mm_node = NULL;
1290 bo->mem.page_alignment = page_alignment;
1291 bo->mem.bus.io_reserved_vm = false;
1292 bo->mem.bus.io_reserved_count = 0;
1294 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1295 bo->acc_size = acc_size;
1298 bo->base.resv = resv;
1299 dma_resv_assert_held(bo->base.resv);
1301 bo->base.resv = &bo->base._resv;
1303 if (!ttm_bo_uses_embedded_gem_object(bo)) {
1305 * bo.gem is not initialized, so we have to setup the
1306 * struct elements we want use regardless.
1308 dma_resv_init(&bo->base._resv);
1309 drm_vma_node_reset(&bo->base.vma_node);
1311 atomic_inc(&ttm_bo_glob.bo_count);
1314 * For ttm_bo_type_device buffers, allocate
1315 * address space from the device.
1317 if (bo->type == ttm_bo_type_device ||
1318 bo->type == ttm_bo_type_sg)
1319 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
1322 /* passed reservation objects should already be locked,
1323 * since otherwise lockdep will be angered in radeon.
1326 locked = dma_resv_trylock(bo->base.resv);
1331 ret = ttm_bo_validate(bo, placement, ctx);
1333 if (unlikely(ret)) {
1335 ttm_bo_unreserve(bo);
1341 spin_lock(&ttm_bo_glob.lru_lock);
1342 ttm_bo_move_to_lru_tail(bo, NULL);
1343 spin_unlock(&ttm_bo_glob.lru_lock);
1347 EXPORT_SYMBOL(ttm_bo_init_reserved);
1349 int ttm_bo_init(struct ttm_bo_device *bdev,
1350 struct ttm_buffer_object *bo,
1352 enum ttm_bo_type type,
1353 struct ttm_placement *placement,
1354 uint32_t page_alignment,
1357 struct sg_table *sg,
1358 struct dma_resv *resv,
1359 void (*destroy) (struct ttm_buffer_object *))
1361 struct ttm_operation_ctx ctx = { interruptible, false };
1364 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1365 page_alignment, &ctx, acc_size,
1371 ttm_bo_unreserve(bo);
1375 EXPORT_SYMBOL(ttm_bo_init);
1377 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1378 unsigned long bo_size,
1379 unsigned struct_size)
1381 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1384 size += ttm_round_pot(struct_size);
1385 size += ttm_round_pot(npages * sizeof(void *));
1386 size += ttm_round_pot(sizeof(struct ttm_tt));
1389 EXPORT_SYMBOL(ttm_bo_acc_size);
1391 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1392 unsigned long bo_size,
1393 unsigned struct_size)
1395 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1398 size += ttm_round_pot(struct_size);
1399 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
1400 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1403 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1405 int ttm_bo_create(struct ttm_bo_device *bdev,
1407 enum ttm_bo_type type,
1408 struct ttm_placement *placement,
1409 uint32_t page_alignment,
1411 struct ttm_buffer_object **p_bo)
1413 struct ttm_buffer_object *bo;
1417 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1418 if (unlikely(bo == NULL))
1421 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1422 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1423 interruptible, acc_size,
1425 if (likely(ret == 0))
1430 EXPORT_SYMBOL(ttm_bo_create);
1432 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1435 struct ttm_operation_ctx ctx = {
1436 .interruptible = false,
1437 .no_wait_gpu = false,
1438 .flags = TTM_OPT_FLAG_FORCE_ALLOC
1440 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1441 struct ttm_bo_global *glob = &ttm_bo_glob;
1442 struct dma_fence *fence;
1447 * Can't use standard list traversal since we're unlocking.
1450 spin_lock(&glob->lru_lock);
1451 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1452 while (!list_empty(&man->lru[i])) {
1453 spin_unlock(&glob->lru_lock);
1454 ret = ttm_mem_evict_first(bdev, mem_type, NULL, &ctx,
1458 spin_lock(&glob->lru_lock);
1461 spin_unlock(&glob->lru_lock);
1463 spin_lock(&man->move_lock);
1464 fence = dma_fence_get(man->move);
1465 spin_unlock(&man->move_lock);
1468 ret = dma_fence_wait(fence, false);
1469 dma_fence_put(fence);
1477 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1479 struct ttm_mem_type_manager *man;
1482 if (mem_type >= TTM_NUM_MEM_TYPES) {
1483 pr_err("Illegal memory type %d\n", mem_type);
1486 man = &bdev->man[mem_type];
1488 if (!man->has_type) {
1489 pr_err("Trying to take down uninitialized memory manager type %u\n",
1494 man->use_type = false;
1495 man->has_type = false;
1499 ret = ttm_bo_force_list_clean(bdev, mem_type);
1501 pr_err("Cleanup eviction failed\n");
1505 ret = (*man->func->takedown)(man);
1508 dma_fence_put(man->move);
1513 EXPORT_SYMBOL(ttm_bo_clean_mm);
1515 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1517 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1519 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1520 pr_err("Illegal memory manager memory type %u\n", mem_type);
1524 if (!man->has_type) {
1525 pr_err("Memory type %u has not been initialized\n", mem_type);
1529 return ttm_bo_force_list_clean(bdev, mem_type);
1531 EXPORT_SYMBOL(ttm_bo_evict_mm);
1533 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1534 unsigned long p_size)
1537 struct ttm_mem_type_manager *man;
1540 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1541 man = &bdev->man[type];
1542 BUG_ON(man->has_type);
1543 man->io_reserve_fastpath = true;
1544 man->use_io_reserve_lru = false;
1545 mutex_init(&man->io_reserve_mutex);
1546 spin_lock_init(&man->move_lock);
1547 INIT_LIST_HEAD(&man->io_reserve_lru);
1549 ret = bdev->driver->init_mem_type(bdev, type, man);
1554 if (type != TTM_PL_SYSTEM) {
1555 ret = (*man->func->init)(man, p_size);
1559 man->has_type = true;
1560 man->use_type = true;
1563 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1564 INIT_LIST_HEAD(&man->lru[i]);
1569 EXPORT_SYMBOL(ttm_bo_init_mm);
1571 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1573 struct ttm_bo_global *glob =
1574 container_of(kobj, struct ttm_bo_global, kobj);
1576 __free_page(glob->dummy_read_page);
1579 static void ttm_bo_global_release(void)
1581 struct ttm_bo_global *glob = &ttm_bo_glob;
1583 mutex_lock(&ttm_global_mutex);
1584 if (--ttm_bo_glob_use_count > 0)
1587 kobject_del(&glob->kobj);
1588 kobject_put(&glob->kobj);
1589 ttm_mem_global_release(&ttm_mem_glob);
1590 memset(glob, 0, sizeof(*glob));
1592 mutex_unlock(&ttm_global_mutex);
1595 static int ttm_bo_global_init(void)
1597 struct ttm_bo_global *glob = &ttm_bo_glob;
1601 mutex_lock(&ttm_global_mutex);
1602 if (++ttm_bo_glob_use_count > 1)
1605 ret = ttm_mem_global_init(&ttm_mem_glob);
1609 spin_lock_init(&glob->lru_lock);
1610 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1612 if (unlikely(glob->dummy_read_page == NULL)) {
1617 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1618 INIT_LIST_HEAD(&glob->swap_lru[i]);
1619 INIT_LIST_HEAD(&glob->device_list);
1620 atomic_set(&glob->bo_count, 0);
1622 ret = kobject_init_and_add(
1623 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1624 if (unlikely(ret != 0))
1625 kobject_put(&glob->kobj);
1627 mutex_unlock(&ttm_global_mutex);
1631 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1633 struct ttm_bo_global *glob = &ttm_bo_glob;
1635 unsigned i = TTM_NUM_MEM_TYPES;
1636 struct ttm_mem_type_manager *man;
1639 man = &bdev->man[i];
1640 if (man->has_type) {
1641 man->use_type = false;
1642 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1644 pr_err("DRM memory manager type %d is not clean\n",
1647 man->has_type = false;
1651 mutex_lock(&ttm_global_mutex);
1652 list_del(&bdev->device_list);
1653 mutex_unlock(&ttm_global_mutex);
1655 cancel_delayed_work_sync(&bdev->wq);
1657 if (ttm_bo_delayed_delete(bdev, true))
1658 pr_debug("Delayed destroy list was clean\n");
1660 spin_lock(&glob->lru_lock);
1661 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1662 if (list_empty(&bdev->man[0].lru[0]))
1663 pr_debug("Swap list %d was clean\n", i);
1664 spin_unlock(&glob->lru_lock);
1667 ttm_bo_global_release();
1671 EXPORT_SYMBOL(ttm_bo_device_release);
1673 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1674 struct ttm_bo_driver *driver,
1675 struct address_space *mapping,
1676 struct drm_vma_offset_manager *vma_manager,
1679 struct ttm_bo_global *glob = &ttm_bo_glob;
1682 if (WARN_ON(vma_manager == NULL))
1685 ret = ttm_bo_global_init();
1689 bdev->driver = driver;
1691 memset(bdev->man, 0, sizeof(bdev->man));
1694 * Initialize the system memory buffer type.
1695 * Other types need to be driver / IOCTL initialized.
1697 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1698 if (unlikely(ret != 0))
1701 bdev->vma_manager = vma_manager;
1702 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1703 INIT_LIST_HEAD(&bdev->ddestroy);
1704 bdev->dev_mapping = mapping;
1705 bdev->need_dma32 = need_dma32;
1706 mutex_lock(&ttm_global_mutex);
1707 list_add_tail(&bdev->device_list, &glob->device_list);
1708 mutex_unlock(&ttm_global_mutex);
1712 ttm_bo_global_release();
1715 EXPORT_SYMBOL(ttm_bo_device_init);
1718 * buffer object vm functions.
1721 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1723 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1725 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1726 if (mem->mem_type == TTM_PL_SYSTEM)
1729 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1732 if (mem->placement & TTM_PL_FLAG_CACHED)
1738 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1740 struct ttm_bo_device *bdev = bo->bdev;
1742 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1743 ttm_mem_io_free_vm(bo);
1746 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1748 struct ttm_bo_device *bdev = bo->bdev;
1749 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1751 ttm_mem_io_lock(man, false);
1752 ttm_bo_unmap_virtual_locked(bo);
1753 ttm_mem_io_unlock(man);
1757 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1759 int ttm_bo_wait(struct ttm_buffer_object *bo,
1760 bool interruptible, bool no_wait)
1762 long timeout = 15 * HZ;
1765 if (dma_resv_test_signaled_rcu(bo->base.resv, true))
1771 timeout = dma_resv_wait_timeout_rcu(bo->base.resv, true,
1772 interruptible, timeout);
1779 dma_resv_add_excl_fence(bo->base.resv, NULL);
1782 EXPORT_SYMBOL(ttm_bo_wait);
1785 * A buffer object shrink method that tries to swap out the first
1786 * buffer object on the bo_global::swap_lru list.
1788 int ttm_bo_swapout(struct ttm_bo_global *glob, struct ttm_operation_ctx *ctx)
1790 struct ttm_buffer_object *bo;
1795 spin_lock(&glob->lru_lock);
1796 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1797 list_for_each_entry(bo, &glob->swap_lru[i], swap) {
1798 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
1802 if (!ttm_bo_get_unless_zero(bo)) {
1804 dma_resv_unlock(bo->base.resv);
1816 spin_unlock(&glob->lru_lock);
1821 ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1826 ttm_bo_del_from_lru(bo);
1827 spin_unlock(&glob->lru_lock);
1830 * Move to system cached
1833 if (bo->mem.mem_type != TTM_PL_SYSTEM ||
1834 bo->ttm->caching_state != tt_cached) {
1835 struct ttm_operation_ctx ctx = { false, false };
1836 struct ttm_mem_reg evict_mem;
1838 evict_mem = bo->mem;
1839 evict_mem.mm_node = NULL;
1840 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1841 evict_mem.mem_type = TTM_PL_SYSTEM;
1843 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx);
1844 if (unlikely(ret != 0))
1849 * Make sure BO is idle.
1852 ret = ttm_bo_wait(bo, false, false);
1853 if (unlikely(ret != 0))
1856 ttm_bo_unmap_virtual(bo);
1859 * Swap out. Buffer will be swapped in again as soon as
1860 * anyone tries to access a ttm page.
1863 if (bo->bdev->driver->swap_notify)
1864 bo->bdev->driver->swap_notify(bo);
1866 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1871 * Unreserve without putting on LRU to avoid swapping out an
1872 * already swapped buffer.
1875 dma_resv_unlock(bo->base.resv);
1879 EXPORT_SYMBOL(ttm_bo_swapout);
1881 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1883 struct ttm_operation_ctx ctx = {
1884 .interruptible = false,
1885 .no_wait_gpu = false
1888 while (ttm_bo_swapout(&ttm_bo_glob, &ctx) == 0);
1890 EXPORT_SYMBOL(ttm_bo_swapout_all);