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 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;
74 drm_printf(&p, "No space for %p (%lu pages, %luK, %luM)\n",
75 bo, bo->mem.num_pages, bo->mem.size >> 10,
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,
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[] = {
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))
121 if (mem->placement & TTM_PL_FLAG_NO_EVICT)
124 man = ttm_manager_type(bdev, mem->mem_type);
125 list_add_tail(&bo->lru, &man->lru[bo->priority]);
127 if (man->use_tt && bo->ttm &&
128 !(bo->ttm->page_flags & (TTM_PAGE_FLAG_SG |
129 TTM_PAGE_FLAG_SWAPPED))) {
130 list_add_tail(&bo->swap, &ttm_bo_glob.swap_lru[bo->priority]);
134 static void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
136 struct ttm_bo_device *bdev = bo->bdev;
139 if (!list_empty(&bo->swap)) {
140 list_del_init(&bo->swap);
143 if (!list_empty(&bo->lru)) {
144 list_del_init(&bo->lru);
148 if (notify && bdev->driver->del_from_lru_notify)
149 bdev->driver->del_from_lru_notify(bo);
152 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
153 struct ttm_buffer_object *bo)
160 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
161 struct ttm_lru_bulk_move *bulk)
163 dma_resv_assert_held(bo->base.resv);
165 ttm_bo_del_from_lru(bo);
166 ttm_bo_add_mem_to_lru(bo, &bo->mem);
168 if (bulk && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
169 switch (bo->mem.mem_type) {
171 ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
175 ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
178 if (bo->ttm && !(bo->ttm->page_flags &
179 (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED)))
180 ttm_bo_bulk_move_set_pos(&bulk->swap[bo->priority], bo);
183 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
185 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
189 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
190 struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
191 struct ttm_resource_manager *man;
196 dma_resv_assert_held(pos->first->base.resv);
197 dma_resv_assert_held(pos->last->base.resv);
199 man = ttm_manager_type(pos->first->bdev, TTM_PL_TT);
200 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
204 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
205 struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
206 struct ttm_resource_manager *man;
211 dma_resv_assert_held(pos->first->base.resv);
212 dma_resv_assert_held(pos->last->base.resv);
214 man = ttm_manager_type(pos->first->bdev, TTM_PL_VRAM);
215 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
219 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
220 struct ttm_lru_bulk_move_pos *pos = &bulk->swap[i];
221 struct list_head *lru;
226 dma_resv_assert_held(pos->first->base.resv);
227 dma_resv_assert_held(pos->last->base.resv);
229 lru = &ttm_bo_glob.swap_lru[i];
230 list_bulk_move_tail(lru, &pos->first->swap, &pos->last->swap);
233 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
235 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
236 struct ttm_resource *mem, bool evict,
237 struct ttm_operation_ctx *ctx)
239 struct ttm_bo_device *bdev = bo->bdev;
240 struct ttm_resource_manager *old_man = ttm_manager_type(bdev, bo->mem.mem_type);
241 struct ttm_resource_manager *new_man = ttm_manager_type(bdev, mem->mem_type);
244 ttm_bo_unmap_virtual(bo);
247 * Create and bind a ttm if required.
250 if (new_man->use_tt) {
251 /* Zero init the new TTM structure if the old location should
252 * have used one as well.
254 ret = ttm_tt_create(bo, old_man->use_tt);
258 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
262 if (mem->mem_type != TTM_PL_SYSTEM) {
263 ret = ttm_tt_populate(bdev, bo->ttm, ctx);
267 ret = ttm_bo_tt_bind(bo, mem);
272 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
273 if (bdev->driver->move_notify)
274 bdev->driver->move_notify(bo, evict, mem);
280 if (bdev->driver->move_notify)
281 bdev->driver->move_notify(bo, evict, mem);
283 if (old_man->use_tt && new_man->use_tt)
284 ret = ttm_bo_move_ttm(bo, ctx, mem);
285 else if (bdev->driver->move)
286 ret = bdev->driver->move(bo, evict, ctx, mem);
288 ret = ttm_bo_move_memcpy(bo, ctx, mem);
291 if (bdev->driver->move_notify) {
293 bdev->driver->move_notify(bo, false, mem);
301 ctx->bytes_moved += bo->num_pages << PAGE_SHIFT;
305 new_man = ttm_manager_type(bdev, bo->mem.mem_type);
306 if (!new_man->use_tt)
307 ttm_bo_tt_destroy(bo);
314 * Will release GPU memory type usage on destruction.
315 * This is the place to put in driver specific hooks to release
316 * driver private resources.
317 * Will release the bo::reserved lock.
320 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
322 if (bo->bdev->driver->move_notify)
323 bo->bdev->driver->move_notify(bo, false, NULL);
325 ttm_bo_tt_destroy(bo);
326 ttm_resource_free(bo, &bo->mem);
329 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
333 if (bo->base.resv == &bo->base._resv)
336 BUG_ON(!dma_resv_trylock(&bo->base._resv));
338 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
339 dma_resv_unlock(&bo->base._resv);
343 if (bo->type != ttm_bo_type_sg) {
344 /* This works because the BO is about to be destroyed and nobody
345 * reference it any more. The only tricky case is the trylock on
346 * the resv object while holding the lru_lock.
348 spin_lock(&ttm_bo_glob.lru_lock);
349 bo->base.resv = &bo->base._resv;
350 spin_unlock(&ttm_bo_glob.lru_lock);
356 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
358 struct dma_resv *resv = &bo->base._resv;
359 struct dma_resv_list *fobj;
360 struct dma_fence *fence;
364 fobj = rcu_dereference(resv->fence);
365 fence = rcu_dereference(resv->fence_excl);
366 if (fence && !fence->ops->signaled)
367 dma_fence_enable_sw_signaling(fence);
369 for (i = 0; fobj && i < fobj->shared_count; ++i) {
370 fence = rcu_dereference(fobj->shared[i]);
372 if (!fence->ops->signaled)
373 dma_fence_enable_sw_signaling(fence);
379 * function ttm_bo_cleanup_refs
380 * If bo idle, remove from lru lists, and unref.
381 * If not idle, block if possible.
383 * Must be called with lru_lock and reservation held, this function
384 * will drop the lru lock and optionally the reservation lock before returning.
386 * @interruptible Any sleeps should occur interruptibly.
387 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
388 * @unlock_resv Unlock the reservation lock as well.
391 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
392 bool interruptible, bool no_wait_gpu,
395 struct dma_resv *resv = &bo->base._resv;
398 if (dma_resv_test_signaled_rcu(resv, true))
403 if (ret && !no_wait_gpu) {
407 dma_resv_unlock(bo->base.resv);
408 spin_unlock(&ttm_bo_glob.lru_lock);
410 lret = dma_resv_wait_timeout_rcu(resv, true, interruptible,
418 spin_lock(&ttm_bo_glob.lru_lock);
419 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
421 * We raced, and lost, someone else holds the reservation now,
422 * and is probably busy in ttm_bo_cleanup_memtype_use.
424 * Even if it's not the case, because we finished waiting any
425 * delayed destruction would succeed, so just return success
428 spin_unlock(&ttm_bo_glob.lru_lock);
434 if (ret || unlikely(list_empty(&bo->ddestroy))) {
436 dma_resv_unlock(bo->base.resv);
437 spin_unlock(&ttm_bo_glob.lru_lock);
441 ttm_bo_del_from_lru(bo);
442 list_del_init(&bo->ddestroy);
443 spin_unlock(&ttm_bo_glob.lru_lock);
444 ttm_bo_cleanup_memtype_use(bo);
447 dma_resv_unlock(bo->base.resv);
455 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
456 * encountered buffers.
458 static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
460 struct ttm_bo_global *glob = &ttm_bo_glob;
461 struct list_head removed;
464 INIT_LIST_HEAD(&removed);
466 spin_lock(&glob->lru_lock);
467 while (!list_empty(&bdev->ddestroy)) {
468 struct ttm_buffer_object *bo;
470 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
472 list_move_tail(&bo->ddestroy, &removed);
473 if (!ttm_bo_get_unless_zero(bo))
476 if (remove_all || bo->base.resv != &bo->base._resv) {
477 spin_unlock(&glob->lru_lock);
478 dma_resv_lock(bo->base.resv, NULL);
480 spin_lock(&glob->lru_lock);
481 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
483 } else if (dma_resv_trylock(bo->base.resv)) {
484 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
486 spin_unlock(&glob->lru_lock);
490 spin_lock(&glob->lru_lock);
492 list_splice_tail(&removed, &bdev->ddestroy);
493 empty = list_empty(&bdev->ddestroy);
494 spin_unlock(&glob->lru_lock);
499 static void ttm_bo_delayed_workqueue(struct work_struct *work)
501 struct ttm_bo_device *bdev =
502 container_of(work, struct ttm_bo_device, wq.work);
504 if (!ttm_bo_delayed_delete(bdev, false))
505 schedule_delayed_work(&bdev->wq,
506 ((HZ / 100) < 1) ? 1 : HZ / 100);
509 static void ttm_bo_release(struct kref *kref)
511 struct ttm_buffer_object *bo =
512 container_of(kref, struct ttm_buffer_object, kref);
513 struct ttm_bo_device *bdev = bo->bdev;
514 size_t acc_size = bo->acc_size;
518 ret = ttm_bo_individualize_resv(bo);
520 /* Last resort, if we fail to allocate memory for the
521 * fences block for the BO to become idle
523 dma_resv_wait_timeout_rcu(bo->base.resv, true, false,
527 if (bo->bdev->driver->release_notify)
528 bo->bdev->driver->release_notify(bo);
530 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
531 ttm_mem_io_free(bdev, &bo->mem);
534 if (!dma_resv_test_signaled_rcu(bo->base.resv, true) ||
535 !dma_resv_trylock(bo->base.resv)) {
536 /* The BO is not idle, resurrect it for delayed destroy */
537 ttm_bo_flush_all_fences(bo);
540 spin_lock(&ttm_bo_glob.lru_lock);
543 * Make NO_EVICT bos immediately available to
544 * shrinkers, now that they are queued for
547 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
548 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
549 ttm_bo_del_from_lru(bo);
550 ttm_bo_add_mem_to_lru(bo, &bo->mem);
553 kref_init(&bo->kref);
554 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
555 spin_unlock(&ttm_bo_glob.lru_lock);
557 schedule_delayed_work(&bdev->wq,
558 ((HZ / 100) < 1) ? 1 : HZ / 100);
562 spin_lock(&ttm_bo_glob.lru_lock);
563 ttm_bo_del_from_lru(bo);
564 list_del(&bo->ddestroy);
565 spin_unlock(&ttm_bo_glob.lru_lock);
567 ttm_bo_cleanup_memtype_use(bo);
568 dma_resv_unlock(bo->base.resv);
570 atomic_dec(&ttm_bo_glob.bo_count);
571 dma_fence_put(bo->moving);
572 if (!ttm_bo_uses_embedded_gem_object(bo))
573 dma_resv_fini(&bo->base._resv);
575 ttm_mem_global_free(&ttm_mem_glob, acc_size);
578 void ttm_bo_put(struct ttm_buffer_object *bo)
580 kref_put(&bo->kref, ttm_bo_release);
582 EXPORT_SYMBOL(ttm_bo_put);
584 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
586 return cancel_delayed_work_sync(&bdev->wq);
588 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
590 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
593 schedule_delayed_work(&bdev->wq,
594 ((HZ / 100) < 1) ? 1 : HZ / 100);
596 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
598 static int ttm_bo_evict(struct ttm_buffer_object *bo,
599 struct ttm_operation_ctx *ctx)
601 struct ttm_bo_device *bdev = bo->bdev;
602 struct ttm_resource evict_mem;
603 struct ttm_placement placement;
606 dma_resv_assert_held(bo->base.resv);
608 placement.num_placement = 0;
609 placement.num_busy_placement = 0;
610 bdev->driver->evict_flags(bo, &placement);
612 if (!placement.num_placement && !placement.num_busy_placement) {
613 ttm_bo_wait(bo, false, false);
615 ttm_bo_cleanup_memtype_use(bo);
616 return ttm_tt_create(bo, false);
620 evict_mem.mm_node = NULL;
621 evict_mem.bus.offset = 0;
622 evict_mem.bus.addr = NULL;
624 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
626 if (ret != -ERESTARTSYS) {
627 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
629 ttm_bo_mem_space_debug(bo, &placement);
634 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx);
636 if (ret != -ERESTARTSYS)
637 pr_err("Buffer eviction failed\n");
638 ttm_resource_free(bo, &evict_mem);
644 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
645 const struct ttm_place *place)
647 /* Don't evict this BO if it's outside of the
648 * requested placement range
650 if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
651 (place->lpfn && place->lpfn <= bo->mem.start))
656 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
659 * Check the target bo is allowable to be evicted or swapout, including cases:
661 * a. if share same reservation object with ctx->resv, have assumption
662 * reservation objects should already be locked, so not lock again and
663 * return true directly when either the opreation allow_reserved_eviction
664 * or the target bo already is in delayed free list;
666 * b. Otherwise, trylock it.
668 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
669 struct ttm_operation_ctx *ctx, bool *locked, bool *busy)
673 if (bo->base.resv == ctx->resv) {
674 dma_resv_assert_held(bo->base.resv);
675 if (ctx->flags & TTM_OPT_FLAG_ALLOW_RES_EVICT)
681 ret = dma_resv_trylock(bo->base.resv);
691 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
693 * @busy_bo: BO which couldn't be locked with trylock
694 * @ctx: operation context
695 * @ticket: acquire ticket
697 * Try to lock a busy buffer object to avoid failing eviction.
699 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
700 struct ttm_operation_ctx *ctx,
701 struct ww_acquire_ctx *ticket)
705 if (!busy_bo || !ticket)
708 if (ctx->interruptible)
709 r = dma_resv_lock_interruptible(busy_bo->base.resv,
712 r = dma_resv_lock(busy_bo->base.resv, ticket);
715 * TODO: It would be better to keep the BO locked until allocation is at
716 * least tried one more time, but that would mean a much larger rework
720 dma_resv_unlock(busy_bo->base.resv);
722 return r == -EDEADLK ? -EBUSY : r;
725 int ttm_mem_evict_first(struct ttm_bo_device *bdev,
726 struct ttm_resource_manager *man,
727 const struct ttm_place *place,
728 struct ttm_operation_ctx *ctx,
729 struct ww_acquire_ctx *ticket)
731 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
736 spin_lock(&ttm_bo_glob.lru_lock);
737 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
738 list_for_each_entry(bo, &man->lru[i], lru) {
741 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
743 if (busy && !busy_bo && ticket !=
744 dma_resv_locking_ctx(bo->base.resv))
749 if (place && !bdev->driver->eviction_valuable(bo,
752 dma_resv_unlock(bo->base.resv);
755 if (!ttm_bo_get_unless_zero(bo)) {
757 dma_resv_unlock(bo->base.resv);
763 /* If the inner loop terminated early, we have our candidate */
764 if (&bo->lru != &man->lru[i])
771 if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
773 spin_unlock(&ttm_bo_glob.lru_lock);
774 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
781 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
782 ctx->no_wait_gpu, locked);
787 spin_unlock(&ttm_bo_glob.lru_lock);
789 ret = ttm_bo_evict(bo, ctx);
791 ttm_bo_unreserve(bo);
798 * Add the last move fence to the BO and reserve a new shared slot.
800 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
801 struct ttm_resource_manager *man,
802 struct ttm_resource *mem,
805 struct dma_fence *fence;
808 spin_lock(&man->move_lock);
809 fence = dma_fence_get(man->move);
810 spin_unlock(&man->move_lock);
816 dma_fence_put(fence);
820 dma_resv_add_shared_fence(bo->base.resv, fence);
822 ret = dma_resv_reserve_shared(bo->base.resv, 1);
824 dma_fence_put(fence);
828 dma_fence_put(bo->moving);
834 * Repeatedly evict memory from the LRU for @mem_type until we create enough
835 * space, or we've evicted everything and there isn't enough space.
837 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
838 const struct ttm_place *place,
839 struct ttm_resource *mem,
840 struct ttm_operation_ctx *ctx)
842 struct ttm_bo_device *bdev = bo->bdev;
843 struct ttm_resource_manager *man = ttm_manager_type(bdev, mem->mem_type);
844 struct ww_acquire_ctx *ticket;
847 ticket = dma_resv_locking_ctx(bo->base.resv);
849 ret = ttm_resource_alloc(bo, place, mem);
852 if (unlikely(ret != -ENOSPC))
854 ret = ttm_mem_evict_first(bdev, man, place, ctx,
856 if (unlikely(ret != 0))
860 return ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
863 static uint32_t ttm_bo_select_caching(struct ttm_resource_manager *man,
864 uint32_t cur_placement,
865 uint32_t proposed_placement)
867 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
868 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
871 * Keep current caching if possible.
874 if ((cur_placement & caching) != 0)
875 result |= (cur_placement & caching);
876 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
877 result |= TTM_PL_FLAG_CACHED;
878 else if ((TTM_PL_FLAG_WC & caching) != 0)
879 result |= TTM_PL_FLAG_WC;
880 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
881 result |= TTM_PL_FLAG_UNCACHED;
887 * ttm_bo_mem_placement - check if placement is compatible
888 * @bo: BO to find memory for
889 * @place: where to search
890 * @mem: the memory object to fill in
891 * @ctx: operation context
893 * Check if placement is compatible and fill in mem structure.
894 * Returns -EBUSY if placement won't work or negative error code.
895 * 0 when placement can be used.
897 static int ttm_bo_mem_placement(struct ttm_buffer_object *bo,
898 const struct ttm_place *place,
899 struct ttm_resource *mem,
900 struct ttm_operation_ctx *ctx)
902 struct ttm_bo_device *bdev = bo->bdev;
903 struct ttm_resource_manager *man;
904 uint32_t cur_flags = 0;
906 man = ttm_manager_type(bdev, place->mem_type);
907 if (!man || !ttm_resource_manager_used(man))
910 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
912 cur_flags |= place->flags & ~TTM_PL_MASK_CACHING;
914 mem->mem_type = place->mem_type;
915 mem->placement = cur_flags;
917 spin_lock(&ttm_bo_glob.lru_lock);
918 ttm_bo_del_from_lru(bo);
919 ttm_bo_add_mem_to_lru(bo, mem);
920 spin_unlock(&ttm_bo_glob.lru_lock);
926 * Creates space for memory region @mem according to its type.
928 * This function first searches for free space in compatible memory types in
929 * the priority order defined by the driver. If free space isn't found, then
930 * ttm_bo_mem_force_space is attempted in priority order to evict and find
933 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
934 struct ttm_placement *placement,
935 struct ttm_resource *mem,
936 struct ttm_operation_ctx *ctx)
938 struct ttm_bo_device *bdev = bo->bdev;
939 bool type_found = false;
942 ret = dma_resv_reserve_shared(bo->base.resv, 1);
946 for (i = 0; i < placement->num_placement; ++i) {
947 const struct ttm_place *place = &placement->placement[i];
948 struct ttm_resource_manager *man;
950 ret = ttm_bo_mem_placement(bo, place, mem, ctx);
955 ret = ttm_resource_alloc(bo, place, mem);
961 man = ttm_manager_type(bdev, mem->mem_type);
962 ret = ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
964 ttm_resource_free(bo, mem);
973 for (i = 0; i < placement->num_busy_placement; ++i) {
974 const struct ttm_place *place = &placement->busy_placement[i];
976 ret = ttm_bo_mem_placement(bo, place, mem, ctx);
981 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
985 if (ret && ret != -EBUSY)
991 pr_err(TTM_PFX "No compatible memory type found\n");
996 if (bo->mem.mem_type == TTM_PL_SYSTEM && !list_empty(&bo->lru)) {
997 ttm_bo_move_to_lru_tail_unlocked(bo);
1002 EXPORT_SYMBOL(ttm_bo_mem_space);
1004 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1005 struct ttm_placement *placement,
1006 struct ttm_operation_ctx *ctx)
1009 struct ttm_resource mem;
1011 dma_resv_assert_held(bo->base.resv);
1013 mem.num_pages = bo->num_pages;
1014 mem.size = mem.num_pages << PAGE_SHIFT;
1015 mem.page_alignment = bo->mem.page_alignment;
1017 mem.bus.addr = NULL;
1021 * Determine where to move the buffer.
1023 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
1026 ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx);
1029 ttm_resource_free(bo, &mem);
1033 static bool ttm_bo_places_compat(const struct ttm_place *places,
1034 unsigned num_placement,
1035 struct ttm_resource *mem,
1036 uint32_t *new_flags)
1040 for (i = 0; i < num_placement; i++) {
1041 const struct ttm_place *heap = &places[i];
1043 if ((mem->start < heap->fpfn ||
1044 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1047 *new_flags = heap->flags;
1048 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1049 (mem->mem_type == heap->mem_type) &&
1050 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
1051 (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
1057 bool ttm_bo_mem_compat(struct ttm_placement *placement,
1058 struct ttm_resource *mem,
1059 uint32_t *new_flags)
1061 if (ttm_bo_places_compat(placement->placement, placement->num_placement,
1065 if ((placement->busy_placement != placement->placement ||
1066 placement->num_busy_placement > placement->num_placement) &&
1067 ttm_bo_places_compat(placement->busy_placement,
1068 placement->num_busy_placement,
1074 EXPORT_SYMBOL(ttm_bo_mem_compat);
1076 int ttm_bo_validate(struct ttm_buffer_object *bo,
1077 struct ttm_placement *placement,
1078 struct ttm_operation_ctx *ctx)
1083 dma_resv_assert_held(bo->base.resv);
1086 * Remove the backing store if no placement is given.
1088 if (!placement->num_placement && !placement->num_busy_placement) {
1089 ret = ttm_bo_pipeline_gutting(bo);
1093 return ttm_tt_create(bo, false);
1097 * Check whether we need to move buffer.
1099 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1100 ret = ttm_bo_move_buffer(bo, placement, ctx);
1104 bo->mem.placement &= TTM_PL_MASK_CACHING;
1105 bo->mem.placement |= new_flags & ~TTM_PL_MASK_CACHING;
1108 * We might need to add a TTM.
1110 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
1111 ret = ttm_tt_create(bo, true);
1117 EXPORT_SYMBOL(ttm_bo_validate);
1119 int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
1120 struct ttm_buffer_object *bo,
1122 enum ttm_bo_type type,
1123 struct ttm_placement *placement,
1124 uint32_t page_alignment,
1125 struct ttm_operation_ctx *ctx,
1127 struct sg_table *sg,
1128 struct dma_resv *resv,
1129 void (*destroy) (struct ttm_buffer_object *))
1131 struct ttm_mem_global *mem_glob = &ttm_mem_glob;
1133 unsigned long num_pages;
1136 ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
1138 pr_err("Out of kernel memory\n");
1146 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1147 if (num_pages == 0) {
1148 pr_err("Illegal buffer object size\n");
1153 ttm_mem_global_free(mem_glob, acc_size);
1156 bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1158 kref_init(&bo->kref);
1159 INIT_LIST_HEAD(&bo->lru);
1160 INIT_LIST_HEAD(&bo->ddestroy);
1161 INIT_LIST_HEAD(&bo->swap);
1164 bo->num_pages = num_pages;
1165 bo->mem.size = num_pages << PAGE_SHIFT;
1166 bo->mem.mem_type = TTM_PL_SYSTEM;
1167 bo->mem.num_pages = bo->num_pages;
1168 bo->mem.mm_node = NULL;
1169 bo->mem.page_alignment = page_alignment;
1170 bo->mem.bus.offset = 0;
1171 bo->mem.bus.addr = NULL;
1173 bo->mem.placement = TTM_PL_FLAG_CACHED;
1174 bo->acc_size = acc_size;
1177 bo->base.resv = resv;
1178 dma_resv_assert_held(bo->base.resv);
1180 bo->base.resv = &bo->base._resv;
1182 if (!ttm_bo_uses_embedded_gem_object(bo)) {
1184 * bo.gem is not initialized, so we have to setup the
1185 * struct elements we want use regardless.
1187 dma_resv_init(&bo->base._resv);
1188 drm_vma_node_reset(&bo->base.vma_node);
1190 atomic_inc(&ttm_bo_glob.bo_count);
1193 * For ttm_bo_type_device buffers, allocate
1194 * address space from the device.
1196 if (bo->type == ttm_bo_type_device ||
1197 bo->type == ttm_bo_type_sg)
1198 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
1201 /* passed reservation objects should already be locked,
1202 * since otherwise lockdep will be angered in radeon.
1205 locked = dma_resv_trylock(bo->base.resv);
1210 ret = ttm_bo_validate(bo, placement, ctx);
1212 if (unlikely(ret)) {
1214 ttm_bo_unreserve(bo);
1220 ttm_bo_move_to_lru_tail_unlocked(bo);
1224 EXPORT_SYMBOL(ttm_bo_init_reserved);
1226 int ttm_bo_init(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,
1234 struct sg_table *sg,
1235 struct dma_resv *resv,
1236 void (*destroy) (struct ttm_buffer_object *))
1238 struct ttm_operation_ctx ctx = { interruptible, false };
1241 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1242 page_alignment, &ctx, acc_size,
1248 ttm_bo_unreserve(bo);
1252 EXPORT_SYMBOL(ttm_bo_init);
1254 static size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1255 unsigned long bo_size,
1256 unsigned struct_size)
1258 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1261 size += ttm_round_pot(struct_size);
1262 size += ttm_round_pot(npages * sizeof(void *));
1263 size += ttm_round_pot(sizeof(struct ttm_tt));
1267 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1268 unsigned long bo_size,
1269 unsigned struct_size)
1271 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1274 size += ttm_round_pot(struct_size);
1275 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
1276 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1279 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1281 int ttm_bo_create(struct ttm_bo_device *bdev,
1283 enum ttm_bo_type type,
1284 struct ttm_placement *placement,
1285 uint32_t page_alignment,
1287 struct ttm_buffer_object **p_bo)
1289 struct ttm_buffer_object *bo;
1293 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1294 if (unlikely(bo == NULL))
1297 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1298 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1299 interruptible, acc_size,
1301 if (likely(ret == 0))
1306 EXPORT_SYMBOL(ttm_bo_create);
1308 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1310 struct ttm_resource_manager *man = ttm_manager_type(bdev, mem_type);
1312 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1313 pr_err("Illegal memory manager memory type %u\n", mem_type);
1318 pr_err("Memory type %u has not been initialized\n", mem_type);
1322 return ttm_resource_manager_force_list_clean(bdev, man);
1324 EXPORT_SYMBOL(ttm_bo_evict_mm);
1326 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1328 struct ttm_bo_global *glob =
1329 container_of(kobj, struct ttm_bo_global, kobj);
1331 __free_page(glob->dummy_read_page);
1334 static void ttm_bo_global_release(void)
1336 struct ttm_bo_global *glob = &ttm_bo_glob;
1338 mutex_lock(&ttm_global_mutex);
1339 if (--ttm_bo_glob_use_count > 0)
1342 kobject_del(&glob->kobj);
1343 kobject_put(&glob->kobj);
1344 ttm_mem_global_release(&ttm_mem_glob);
1345 memset(glob, 0, sizeof(*glob));
1347 mutex_unlock(&ttm_global_mutex);
1350 static int ttm_bo_global_init(void)
1352 struct ttm_bo_global *glob = &ttm_bo_glob;
1356 mutex_lock(&ttm_global_mutex);
1357 if (++ttm_bo_glob_use_count > 1)
1360 ret = ttm_mem_global_init(&ttm_mem_glob);
1364 spin_lock_init(&glob->lru_lock);
1365 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1367 if (unlikely(glob->dummy_read_page == NULL)) {
1372 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1373 INIT_LIST_HEAD(&glob->swap_lru[i]);
1374 INIT_LIST_HEAD(&glob->device_list);
1375 atomic_set(&glob->bo_count, 0);
1377 ret = kobject_init_and_add(
1378 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1379 if (unlikely(ret != 0))
1380 kobject_put(&glob->kobj);
1382 mutex_unlock(&ttm_global_mutex);
1386 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1388 struct ttm_bo_global *glob = &ttm_bo_glob;
1391 struct ttm_resource_manager *man;
1393 man = ttm_manager_type(bdev, TTM_PL_SYSTEM);
1394 ttm_resource_manager_set_used(man, false);
1395 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL);
1397 mutex_lock(&ttm_global_mutex);
1398 list_del(&bdev->device_list);
1399 mutex_unlock(&ttm_global_mutex);
1401 cancel_delayed_work_sync(&bdev->wq);
1403 if (ttm_bo_delayed_delete(bdev, true))
1404 pr_debug("Delayed destroy list was clean\n");
1406 spin_lock(&glob->lru_lock);
1407 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1408 if (list_empty(&man->lru[0]))
1409 pr_debug("Swap list %d was clean\n", i);
1410 spin_unlock(&glob->lru_lock);
1413 ttm_bo_global_release();
1417 EXPORT_SYMBOL(ttm_bo_device_release);
1419 static void ttm_bo_init_sysman(struct ttm_bo_device *bdev)
1421 struct ttm_resource_manager *man = &bdev->sysman;
1424 * Initialize the system memory buffer type.
1425 * Other types need to be driver / IOCTL initialized.
1429 ttm_resource_manager_init(man, 0);
1430 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, man);
1431 ttm_resource_manager_set_used(man, true);
1434 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1435 struct ttm_bo_driver *driver,
1436 struct address_space *mapping,
1437 struct drm_vma_offset_manager *vma_manager,
1440 struct ttm_bo_global *glob = &ttm_bo_glob;
1443 if (WARN_ON(vma_manager == NULL))
1446 ret = ttm_bo_global_init();
1450 bdev->driver = driver;
1452 ttm_bo_init_sysman(bdev);
1454 bdev->vma_manager = vma_manager;
1455 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1456 INIT_LIST_HEAD(&bdev->ddestroy);
1457 bdev->dev_mapping = mapping;
1458 bdev->need_dma32 = need_dma32;
1459 mutex_lock(&ttm_global_mutex);
1460 list_add_tail(&bdev->device_list, &glob->device_list);
1461 mutex_unlock(&ttm_global_mutex);
1465 EXPORT_SYMBOL(ttm_bo_device_init);
1468 * buffer object vm functions.
1471 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1473 struct ttm_bo_device *bdev = bo->bdev;
1475 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1476 ttm_mem_io_free(bdev, &bo->mem);
1478 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1480 int ttm_bo_wait(struct ttm_buffer_object *bo,
1481 bool interruptible, bool no_wait)
1483 long timeout = 15 * HZ;
1486 if (dma_resv_test_signaled_rcu(bo->base.resv, true))
1492 timeout = dma_resv_wait_timeout_rcu(bo->base.resv, true,
1493 interruptible, timeout);
1500 dma_resv_add_excl_fence(bo->base.resv, NULL);
1503 EXPORT_SYMBOL(ttm_bo_wait);
1506 * A buffer object shrink method that tries to swap out the first
1507 * buffer object on the bo_global::swap_lru list.
1509 int ttm_bo_swapout(struct ttm_bo_global *glob, struct ttm_operation_ctx *ctx)
1511 struct ttm_buffer_object *bo;
1516 spin_lock(&glob->lru_lock);
1517 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1518 list_for_each_entry(bo, &glob->swap_lru[i], swap) {
1519 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
1523 if (!ttm_bo_get_unless_zero(bo)) {
1525 dma_resv_unlock(bo->base.resv);
1537 spin_unlock(&glob->lru_lock);
1542 ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1547 ttm_bo_del_from_lru(bo);
1548 spin_unlock(&glob->lru_lock);
1551 * Move to system cached
1554 if (bo->mem.mem_type != TTM_PL_SYSTEM ||
1555 bo->ttm->caching_state != tt_cached) {
1556 struct ttm_operation_ctx ctx = { false, false };
1557 struct ttm_resource evict_mem;
1559 evict_mem = bo->mem;
1560 evict_mem.mm_node = NULL;
1561 evict_mem.placement = TTM_PL_FLAG_CACHED;
1562 evict_mem.mem_type = TTM_PL_SYSTEM;
1564 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx);
1565 if (unlikely(ret != 0))
1570 * Make sure BO is idle.
1573 ret = ttm_bo_wait(bo, false, false);
1574 if (unlikely(ret != 0))
1577 ttm_bo_unmap_virtual(bo);
1580 * Swap out. Buffer will be swapped in again as soon as
1581 * anyone tries to access a ttm page.
1584 if (bo->bdev->driver->swap_notify)
1585 bo->bdev->driver->swap_notify(bo);
1587 ret = ttm_tt_swapout(bo->bdev, bo->ttm, bo->persistent_swap_storage);
1592 * Unreserve without putting on LRU to avoid swapping out an
1593 * already swapped buffer.
1596 dma_resv_unlock(bo->base.resv);
1600 EXPORT_SYMBOL(ttm_bo_swapout);
1602 void ttm_bo_swapout_all(void)
1604 struct ttm_operation_ctx ctx = {
1605 .interruptible = false,
1606 .no_wait_gpu = false
1609 while (ttm_bo_swapout(&ttm_bo_glob, &ctx) == 0);
1611 EXPORT_SYMBOL(ttm_bo_swapout_all);
1613 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1615 if (bo->ttm == NULL)
1618 ttm_tt_destroy(bo->bdev, bo->ttm);
1622 int ttm_bo_tt_bind(struct ttm_buffer_object *bo, struct ttm_resource *mem)
1624 return bo->bdev->driver->ttm_tt_bind(bo->bdev, bo->ttm, mem);
1627 void ttm_bo_tt_unbind(struct ttm_buffer_object *bo)
1629 bo->bdev->driver->ttm_tt_unbind(bo->bdev, bo->ttm);