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_bo_driver.h>
35 #include <drm/ttm/ttm_placement.h>
36 #include <linux/jiffies.h>
37 #include <linux/slab.h>
38 #include <linux/sched.h>
40 #include <linux/file.h>
41 #include <linux/module.h>
42 #include <linux/atomic.h>
43 #include <linux/dma-resv.h>
45 #include "ttm_module.h"
47 static void ttm_bo_global_kobj_release(struct kobject *kobj);
50 * ttm_global_mutex - protecting the global BO state
52 DEFINE_MUTEX(ttm_global_mutex);
53 unsigned ttm_bo_glob_use_count;
54 struct ttm_bo_global ttm_bo_glob;
55 EXPORT_SYMBOL(ttm_bo_glob);
57 static struct attribute ttm_bo_count = {
62 /* default destructor */
63 static void ttm_bo_default_destroy(struct ttm_buffer_object *bo)
68 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
69 struct ttm_placement *placement)
71 struct drm_printer p = drm_debug_printer(TTM_PFX);
72 struct ttm_resource_manager *man;
75 drm_printf(&p, "No space for %p (%lu pages, %zuK, %zuM)\n",
76 bo, bo->mem.num_pages, bo->base.size >> 10,
78 for (i = 0; i < placement->num_placement; i++) {
79 mem_type = placement->placement[i].mem_type;
80 drm_printf(&p, " placement[%d]=0x%08X (%d)\n",
81 i, placement->placement[i].flags, mem_type);
82 man = ttm_manager_type(bo->bdev, mem_type);
83 ttm_resource_manager_debug(man, &p);
87 static ssize_t ttm_bo_global_show(struct kobject *kobj,
88 struct attribute *attr,
91 struct ttm_bo_global *glob =
92 container_of(kobj, struct ttm_bo_global, kobj);
94 return snprintf(buffer, PAGE_SIZE, "%d\n",
95 atomic_read(&glob->bo_count));
98 static struct attribute *ttm_bo_global_attrs[] = {
103 static const struct sysfs_ops ttm_bo_global_ops = {
104 .show = &ttm_bo_global_show
107 static struct kobj_type ttm_bo_glob_kobj_type = {
108 .release = &ttm_bo_global_kobj_release,
109 .sysfs_ops = &ttm_bo_global_ops,
110 .default_attrs = ttm_bo_global_attrs
113 static void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
115 struct ttm_bo_device *bdev = bo->bdev;
117 list_del_init(&bo->swap);
118 list_del_init(&bo->lru);
120 if (bdev->driver->del_from_lru_notify)
121 bdev->driver->del_from_lru_notify(bo);
124 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
125 struct ttm_buffer_object *bo)
132 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
133 struct ttm_resource *mem,
134 struct ttm_lru_bulk_move *bulk)
136 struct ttm_bo_device *bdev = bo->bdev;
137 struct ttm_resource_manager *man;
139 dma_resv_assert_held(bo->base.resv);
144 man = ttm_manager_type(bdev, mem->mem_type);
145 list_move_tail(&bo->lru, &man->lru[bo->priority]);
146 if (man->use_tt && bo->ttm &&
147 !(bo->ttm->page_flags & (TTM_PAGE_FLAG_SG |
148 TTM_PAGE_FLAG_SWAPPED))) {
149 struct list_head *swap;
151 swap = &ttm_bo_glob.swap_lru[bo->priority];
152 list_move_tail(&bo->swap, swap);
155 if (bdev->driver->del_from_lru_notify)
156 bdev->driver->del_from_lru_notify(bo);
158 if (bulk && !bo->pin_count) {
159 switch (bo->mem.mem_type) {
161 ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
165 ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
168 if (bo->ttm && !(bo->ttm->page_flags &
169 (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED)))
170 ttm_bo_bulk_move_set_pos(&bulk->swap[bo->priority], bo);
173 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
175 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
179 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
180 struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
181 struct ttm_resource_manager *man;
186 dma_resv_assert_held(pos->first->base.resv);
187 dma_resv_assert_held(pos->last->base.resv);
189 man = ttm_manager_type(pos->first->bdev, TTM_PL_TT);
190 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
194 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
195 struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
196 struct ttm_resource_manager *man;
201 dma_resv_assert_held(pos->first->base.resv);
202 dma_resv_assert_held(pos->last->base.resv);
204 man = ttm_manager_type(pos->first->bdev, TTM_PL_VRAM);
205 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
209 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
210 struct ttm_lru_bulk_move_pos *pos = &bulk->swap[i];
211 struct list_head *lru;
216 dma_resv_assert_held(pos->first->base.resv);
217 dma_resv_assert_held(pos->last->base.resv);
219 lru = &ttm_bo_glob.swap_lru[i];
220 list_bulk_move_tail(lru, &pos->first->swap, &pos->last->swap);
223 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
225 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
226 struct ttm_resource *mem, bool evict,
227 struct ttm_operation_ctx *ctx,
228 struct ttm_place *hop)
230 struct ttm_bo_device *bdev = bo->bdev;
231 struct ttm_resource_manager *old_man = ttm_manager_type(bdev, bo->mem.mem_type);
232 struct ttm_resource_manager *new_man = ttm_manager_type(bdev, mem->mem_type);
235 ttm_bo_unmap_virtual(bo);
238 * Create and bind a ttm if required.
241 if (new_man->use_tt) {
242 /* Zero init the new TTM structure if the old location should
243 * have used one as well.
245 ret = ttm_tt_create(bo, old_man->use_tt);
249 if (mem->mem_type != TTM_PL_SYSTEM) {
250 ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
256 ret = bdev->driver->move(bo, evict, ctx, mem, hop);
258 if (ret == -EMULTIHOP)
263 ctx->bytes_moved += bo->base.size;
267 new_man = ttm_manager_type(bdev, bo->mem.mem_type);
268 if (!new_man->use_tt)
269 ttm_bo_tt_destroy(bo);
276 * Will release GPU memory type usage on destruction.
277 * This is the place to put in driver specific hooks to release
278 * driver private resources.
279 * Will release the bo::reserved lock.
282 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
284 if (bo->bdev->driver->delete_mem_notify)
285 bo->bdev->driver->delete_mem_notify(bo);
287 ttm_bo_tt_destroy(bo);
288 ttm_resource_free(bo, &bo->mem);
291 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
295 if (bo->base.resv == &bo->base._resv)
298 BUG_ON(!dma_resv_trylock(&bo->base._resv));
300 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
301 dma_resv_unlock(&bo->base._resv);
305 if (bo->type != ttm_bo_type_sg) {
306 /* This works because the BO is about to be destroyed and nobody
307 * reference it any more. The only tricky case is the trylock on
308 * the resv object while holding the lru_lock.
310 spin_lock(&ttm_bo_glob.lru_lock);
311 bo->base.resv = &bo->base._resv;
312 spin_unlock(&ttm_bo_glob.lru_lock);
318 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
320 struct dma_resv *resv = &bo->base._resv;
321 struct dma_resv_list *fobj;
322 struct dma_fence *fence;
326 fobj = rcu_dereference(resv->fence);
327 fence = rcu_dereference(resv->fence_excl);
328 if (fence && !fence->ops->signaled)
329 dma_fence_enable_sw_signaling(fence);
331 for (i = 0; fobj && i < fobj->shared_count; ++i) {
332 fence = rcu_dereference(fobj->shared[i]);
334 if (!fence->ops->signaled)
335 dma_fence_enable_sw_signaling(fence);
341 * function ttm_bo_cleanup_refs
342 * If bo idle, remove from lru lists, and unref.
343 * If not idle, block if possible.
345 * Must be called with lru_lock and reservation held, this function
346 * will drop the lru lock and optionally the reservation lock before returning.
348 * @bo: The buffer object to clean-up
349 * @interruptible: Any sleeps should occur interruptibly.
350 * @no_wait_gpu: Never wait for gpu. Return -EBUSY instead.
351 * @unlock_resv: Unlock the reservation lock as well.
354 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
355 bool interruptible, bool no_wait_gpu,
358 struct dma_resv *resv = &bo->base._resv;
361 if (dma_resv_test_signaled_rcu(resv, true))
366 if (ret && !no_wait_gpu) {
370 dma_resv_unlock(bo->base.resv);
371 spin_unlock(&ttm_bo_glob.lru_lock);
373 lret = dma_resv_wait_timeout_rcu(resv, true, interruptible,
381 spin_lock(&ttm_bo_glob.lru_lock);
382 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
384 * We raced, and lost, someone else holds the reservation now,
385 * and is probably busy in ttm_bo_cleanup_memtype_use.
387 * Even if it's not the case, because we finished waiting any
388 * delayed destruction would succeed, so just return success
391 spin_unlock(&ttm_bo_glob.lru_lock);
397 if (ret || unlikely(list_empty(&bo->ddestroy))) {
399 dma_resv_unlock(bo->base.resv);
400 spin_unlock(&ttm_bo_glob.lru_lock);
404 ttm_bo_del_from_lru(bo);
405 list_del_init(&bo->ddestroy);
406 spin_unlock(&ttm_bo_glob.lru_lock);
407 ttm_bo_cleanup_memtype_use(bo);
410 dma_resv_unlock(bo->base.resv);
418 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
419 * encountered buffers.
421 static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
423 struct ttm_bo_global *glob = &ttm_bo_glob;
424 struct list_head removed;
427 INIT_LIST_HEAD(&removed);
429 spin_lock(&glob->lru_lock);
430 while (!list_empty(&bdev->ddestroy)) {
431 struct ttm_buffer_object *bo;
433 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
435 list_move_tail(&bo->ddestroy, &removed);
436 if (!ttm_bo_get_unless_zero(bo))
439 if (remove_all || bo->base.resv != &bo->base._resv) {
440 spin_unlock(&glob->lru_lock);
441 dma_resv_lock(bo->base.resv, NULL);
443 spin_lock(&glob->lru_lock);
444 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
446 } else if (dma_resv_trylock(bo->base.resv)) {
447 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
449 spin_unlock(&glob->lru_lock);
453 spin_lock(&glob->lru_lock);
455 list_splice_tail(&removed, &bdev->ddestroy);
456 empty = list_empty(&bdev->ddestroy);
457 spin_unlock(&glob->lru_lock);
462 static void ttm_bo_delayed_workqueue(struct work_struct *work)
464 struct ttm_bo_device *bdev =
465 container_of(work, struct ttm_bo_device, wq.work);
467 if (!ttm_bo_delayed_delete(bdev, false))
468 schedule_delayed_work(&bdev->wq,
469 ((HZ / 100) < 1) ? 1 : HZ / 100);
472 static void ttm_bo_release(struct kref *kref)
474 struct ttm_buffer_object *bo =
475 container_of(kref, struct ttm_buffer_object, kref);
476 struct ttm_bo_device *bdev = bo->bdev;
477 size_t acc_size = bo->acc_size;
481 ret = ttm_bo_individualize_resv(bo);
483 /* Last resort, if we fail to allocate memory for the
484 * fences block for the BO to become idle
486 dma_resv_wait_timeout_rcu(bo->base.resv, true, false,
490 if (bo->bdev->driver->release_notify)
491 bo->bdev->driver->release_notify(bo);
493 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
494 ttm_mem_io_free(bdev, &bo->mem);
497 if (!dma_resv_test_signaled_rcu(bo->base.resv, true) ||
498 !dma_resv_trylock(bo->base.resv)) {
499 /* The BO is not idle, resurrect it for delayed destroy */
500 ttm_bo_flush_all_fences(bo);
503 spin_lock(&ttm_bo_glob.lru_lock);
506 * Make pinned bos immediately available to
507 * shrinkers, now that they are queued for
510 if (WARN_ON(bo->pin_count)) {
512 ttm_bo_move_to_lru_tail(bo, &bo->mem, NULL);
515 kref_init(&bo->kref);
516 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
517 spin_unlock(&ttm_bo_glob.lru_lock);
519 schedule_delayed_work(&bdev->wq,
520 ((HZ / 100) < 1) ? 1 : HZ / 100);
524 spin_lock(&ttm_bo_glob.lru_lock);
525 ttm_bo_del_from_lru(bo);
526 list_del(&bo->ddestroy);
527 spin_unlock(&ttm_bo_glob.lru_lock);
529 ttm_bo_cleanup_memtype_use(bo);
530 dma_resv_unlock(bo->base.resv);
532 atomic_dec(&ttm_bo_glob.bo_count);
533 dma_fence_put(bo->moving);
534 if (!ttm_bo_uses_embedded_gem_object(bo))
535 dma_resv_fini(&bo->base._resv);
537 ttm_mem_global_free(&ttm_mem_glob, acc_size);
540 void ttm_bo_put(struct ttm_buffer_object *bo)
542 kref_put(&bo->kref, ttm_bo_release);
544 EXPORT_SYMBOL(ttm_bo_put);
546 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
548 return cancel_delayed_work_sync(&bdev->wq);
550 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
552 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
555 schedule_delayed_work(&bdev->wq,
556 ((HZ / 100) < 1) ? 1 : HZ / 100);
558 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
560 static int ttm_bo_evict(struct ttm_buffer_object *bo,
561 struct ttm_operation_ctx *ctx)
563 struct ttm_bo_device *bdev = bo->bdev;
564 struct ttm_resource evict_mem;
565 struct ttm_placement placement;
566 struct ttm_place hop;
569 memset(&hop, 0, sizeof(hop));
571 dma_resv_assert_held(bo->base.resv);
573 placement.num_placement = 0;
574 placement.num_busy_placement = 0;
575 bdev->driver->evict_flags(bo, &placement);
577 if (!placement.num_placement && !placement.num_busy_placement) {
578 ttm_bo_wait(bo, false, false);
580 ttm_bo_cleanup_memtype_use(bo);
581 return ttm_tt_create(bo, false);
585 evict_mem.mm_node = NULL;
586 evict_mem.bus.offset = 0;
587 evict_mem.bus.addr = NULL;
589 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
591 if (ret != -ERESTARTSYS) {
592 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
594 ttm_bo_mem_space_debug(bo, &placement);
599 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx, &hop);
601 WARN(ret == -EMULTIHOP, "Unexpected multihop in eviction - likely driver bug\n");
602 if (ret != -ERESTARTSYS)
603 pr_err("Buffer eviction failed\n");
604 ttm_resource_free(bo, &evict_mem);
610 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
611 const struct ttm_place *place)
613 /* Don't evict this BO if it's outside of the
614 * requested placement range
616 if (place->fpfn >= (bo->mem.start + bo->mem.num_pages) ||
617 (place->lpfn && place->lpfn <= bo->mem.start))
622 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
625 * Check the target bo is allowable to be evicted or swapout, including cases:
627 * a. if share same reservation object with ctx->resv, have assumption
628 * reservation objects should already be locked, so not lock again and
629 * return true directly when either the opreation allow_reserved_eviction
630 * or the target bo already is in delayed free list;
632 * b. Otherwise, trylock it.
634 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
635 struct ttm_operation_ctx *ctx, bool *locked, bool *busy)
639 if (bo->base.resv == ctx->resv) {
640 dma_resv_assert_held(bo->base.resv);
641 if (ctx->allow_res_evict)
647 ret = dma_resv_trylock(bo->base.resv);
657 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
659 * @busy_bo: BO which couldn't be locked with trylock
660 * @ctx: operation context
661 * @ticket: acquire ticket
663 * Try to lock a busy buffer object to avoid failing eviction.
665 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
666 struct ttm_operation_ctx *ctx,
667 struct ww_acquire_ctx *ticket)
671 if (!busy_bo || !ticket)
674 if (ctx->interruptible)
675 r = dma_resv_lock_interruptible(busy_bo->base.resv,
678 r = dma_resv_lock(busy_bo->base.resv, ticket);
681 * TODO: It would be better to keep the BO locked until allocation is at
682 * least tried one more time, but that would mean a much larger rework
686 dma_resv_unlock(busy_bo->base.resv);
688 return r == -EDEADLK ? -EBUSY : r;
691 int ttm_mem_evict_first(struct ttm_bo_device *bdev,
692 struct ttm_resource_manager *man,
693 const struct ttm_place *place,
694 struct ttm_operation_ctx *ctx,
695 struct ww_acquire_ctx *ticket)
697 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
702 spin_lock(&ttm_bo_glob.lru_lock);
703 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
704 list_for_each_entry(bo, &man->lru[i], lru) {
707 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
709 if (busy && !busy_bo && ticket !=
710 dma_resv_locking_ctx(bo->base.resv))
715 if (place && !bdev->driver->eviction_valuable(bo,
718 dma_resv_unlock(bo->base.resv);
721 if (!ttm_bo_get_unless_zero(bo)) {
723 dma_resv_unlock(bo->base.resv);
729 /* If the inner loop terminated early, we have our candidate */
730 if (&bo->lru != &man->lru[i])
737 if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
739 spin_unlock(&ttm_bo_glob.lru_lock);
740 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
747 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
748 ctx->no_wait_gpu, locked);
753 spin_unlock(&ttm_bo_glob.lru_lock);
755 ret = ttm_bo_evict(bo, ctx);
757 ttm_bo_unreserve(bo);
764 * Add the last move fence to the BO and reserve a new shared slot.
766 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
767 struct ttm_resource_manager *man,
768 struct ttm_resource *mem,
771 struct dma_fence *fence;
774 spin_lock(&man->move_lock);
775 fence = dma_fence_get(man->move);
776 spin_unlock(&man->move_lock);
782 dma_fence_put(fence);
786 dma_resv_add_shared_fence(bo->base.resv, fence);
788 ret = dma_resv_reserve_shared(bo->base.resv, 1);
790 dma_fence_put(fence);
794 dma_fence_put(bo->moving);
800 * Repeatedly evict memory from the LRU for @mem_type until we create enough
801 * space, or we've evicted everything and there isn't enough space.
803 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
804 const struct ttm_place *place,
805 struct ttm_resource *mem,
806 struct ttm_operation_ctx *ctx)
808 struct ttm_bo_device *bdev = bo->bdev;
809 struct ttm_resource_manager *man = ttm_manager_type(bdev, mem->mem_type);
810 struct ww_acquire_ctx *ticket;
813 ticket = dma_resv_locking_ctx(bo->base.resv);
815 ret = ttm_resource_alloc(bo, place, mem);
818 if (unlikely(ret != -ENOSPC))
820 ret = ttm_mem_evict_first(bdev, man, place, ctx,
822 if (unlikely(ret != 0))
826 return ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
830 * ttm_bo_mem_placement - check if placement is compatible
831 * @bo: BO to find memory for
832 * @place: where to search
833 * @mem: the memory object to fill in
835 * Check if placement is compatible and fill in mem structure.
836 * Returns -EBUSY if placement won't work or negative error code.
837 * 0 when placement can be used.
839 static int ttm_bo_mem_placement(struct ttm_buffer_object *bo,
840 const struct ttm_place *place,
841 struct ttm_resource *mem)
843 struct ttm_bo_device *bdev = bo->bdev;
844 struct ttm_resource_manager *man;
846 man = ttm_manager_type(bdev, place->mem_type);
847 if (!man || !ttm_resource_manager_used(man))
850 mem->mem_type = place->mem_type;
851 mem->placement = place->flags;
853 spin_lock(&ttm_bo_glob.lru_lock);
854 ttm_bo_move_to_lru_tail(bo, mem, NULL);
855 spin_unlock(&ttm_bo_glob.lru_lock);
861 * Creates space for memory region @mem according to its type.
863 * This function first searches for free space in compatible memory types in
864 * the priority order defined by the driver. If free space isn't found, then
865 * ttm_bo_mem_force_space is attempted in priority order to evict and find
868 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
869 struct ttm_placement *placement,
870 struct ttm_resource *mem,
871 struct ttm_operation_ctx *ctx)
873 struct ttm_bo_device *bdev = bo->bdev;
874 bool type_found = false;
877 ret = dma_resv_reserve_shared(bo->base.resv, 1);
881 for (i = 0; i < placement->num_placement; ++i) {
882 const struct ttm_place *place = &placement->placement[i];
883 struct ttm_resource_manager *man;
885 ret = ttm_bo_mem_placement(bo, place, mem);
890 ret = ttm_resource_alloc(bo, place, mem);
896 man = ttm_manager_type(bdev, mem->mem_type);
897 ret = ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
899 ttm_resource_free(bo, mem);
908 for (i = 0; i < placement->num_busy_placement; ++i) {
909 const struct ttm_place *place = &placement->busy_placement[i];
911 ret = ttm_bo_mem_placement(bo, place, mem);
916 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
920 if (ret && ret != -EBUSY)
926 pr_err(TTM_PFX "No compatible memory type found\n");
931 if (bo->mem.mem_type == TTM_PL_SYSTEM && !bo->pin_count)
932 ttm_bo_move_to_lru_tail_unlocked(bo);
936 EXPORT_SYMBOL(ttm_bo_mem_space);
938 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
939 struct ttm_resource *mem,
940 struct ttm_operation_ctx *ctx,
941 struct ttm_place *hop)
943 struct ttm_placement hop_placement;
945 struct ttm_resource hop_mem = *mem;
947 hop_mem.mm_node = NULL;
948 hop_mem.mem_type = TTM_PL_SYSTEM;
949 hop_mem.placement = 0;
951 hop_placement.num_placement = hop_placement.num_busy_placement = 1;
952 hop_placement.placement = hop_placement.busy_placement = hop;
954 /* find space in the bounce domain */
955 ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
958 /* move to the bounce domain */
959 ret = ttm_bo_handle_move_mem(bo, &hop_mem, false, ctx, NULL);
965 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
966 struct ttm_placement *placement,
967 struct ttm_operation_ctx *ctx)
970 struct ttm_place hop;
971 struct ttm_resource mem;
973 dma_resv_assert_held(bo->base.resv);
975 memset(&hop, 0, sizeof(hop));
977 mem.num_pages = PAGE_ALIGN(bo->base.size) >> PAGE_SHIFT;
978 mem.page_alignment = bo->mem.page_alignment;
984 * Determine where to move the buffer.
986 * If driver determines move is going to need
987 * an extra step then it will return -EMULTIHOP
988 * and the buffer will be moved to the temporary
989 * stop and the driver will be called to make
993 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
996 ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx, &hop);
997 if (ret == -EMULTIHOP) {
998 ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
1001 /* try and move to final place now. */
1005 ttm_resource_free(bo, &mem);
1009 static bool ttm_bo_places_compat(const struct ttm_place *places,
1010 unsigned num_placement,
1011 struct ttm_resource *mem,
1012 uint32_t *new_flags)
1016 for (i = 0; i < num_placement; i++) {
1017 const struct ttm_place *heap = &places[i];
1019 if ((mem->start < heap->fpfn ||
1020 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1023 *new_flags = heap->flags;
1024 if ((mem->mem_type == heap->mem_type) &&
1025 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
1026 (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
1032 bool ttm_bo_mem_compat(struct ttm_placement *placement,
1033 struct ttm_resource *mem,
1034 uint32_t *new_flags)
1036 if (ttm_bo_places_compat(placement->placement, placement->num_placement,
1040 if ((placement->busy_placement != placement->placement ||
1041 placement->num_busy_placement > placement->num_placement) &&
1042 ttm_bo_places_compat(placement->busy_placement,
1043 placement->num_busy_placement,
1049 EXPORT_SYMBOL(ttm_bo_mem_compat);
1051 int ttm_bo_validate(struct ttm_buffer_object *bo,
1052 struct ttm_placement *placement,
1053 struct ttm_operation_ctx *ctx)
1058 dma_resv_assert_held(bo->base.resv);
1061 * Remove the backing store if no placement is given.
1063 if (!placement->num_placement && !placement->num_busy_placement) {
1064 ret = ttm_bo_pipeline_gutting(bo);
1068 return ttm_tt_create(bo, false);
1072 * Check whether we need to move buffer.
1074 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1075 ret = ttm_bo_move_buffer(bo, placement, ctx);
1080 * We might need to add a TTM.
1082 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
1083 ret = ttm_tt_create(bo, true);
1089 EXPORT_SYMBOL(ttm_bo_validate);
1091 int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
1092 struct ttm_buffer_object *bo,
1094 enum ttm_bo_type type,
1095 struct ttm_placement *placement,
1096 uint32_t page_alignment,
1097 struct ttm_operation_ctx *ctx,
1099 struct sg_table *sg,
1100 struct dma_resv *resv,
1101 void (*destroy) (struct ttm_buffer_object *))
1103 struct ttm_mem_global *mem_glob = &ttm_mem_glob;
1107 ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
1109 pr_err("Out of kernel memory\n");
1117 bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1119 kref_init(&bo->kref);
1120 INIT_LIST_HEAD(&bo->lru);
1121 INIT_LIST_HEAD(&bo->ddestroy);
1122 INIT_LIST_HEAD(&bo->swap);
1125 bo->mem.mem_type = TTM_PL_SYSTEM;
1126 bo->mem.num_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1127 bo->mem.mm_node = NULL;
1128 bo->mem.page_alignment = page_alignment;
1129 bo->mem.bus.offset = 0;
1130 bo->mem.bus.addr = NULL;
1132 bo->mem.placement = 0;
1133 bo->acc_size = acc_size;
1137 bo->base.resv = resv;
1138 dma_resv_assert_held(bo->base.resv);
1140 bo->base.resv = &bo->base._resv;
1142 if (!ttm_bo_uses_embedded_gem_object(bo)) {
1144 * bo.base is not initialized, so we have to setup the
1145 * struct elements we want use regardless.
1147 bo->base.size = size;
1148 dma_resv_init(&bo->base._resv);
1149 drm_vma_node_reset(&bo->base.vma_node);
1151 atomic_inc(&ttm_bo_glob.bo_count);
1154 * For ttm_bo_type_device buffers, allocate
1155 * address space from the device.
1157 if (bo->type == ttm_bo_type_device ||
1158 bo->type == ttm_bo_type_sg)
1159 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
1162 /* passed reservation objects should already be locked,
1163 * since otherwise lockdep will be angered in radeon.
1166 locked = dma_resv_trylock(bo->base.resv);
1171 ret = ttm_bo_validate(bo, placement, ctx);
1173 if (unlikely(ret)) {
1175 ttm_bo_unreserve(bo);
1181 ttm_bo_move_to_lru_tail_unlocked(bo);
1185 EXPORT_SYMBOL(ttm_bo_init_reserved);
1187 int ttm_bo_init(struct ttm_bo_device *bdev,
1188 struct ttm_buffer_object *bo,
1190 enum ttm_bo_type type,
1191 struct ttm_placement *placement,
1192 uint32_t page_alignment,
1195 struct sg_table *sg,
1196 struct dma_resv *resv,
1197 void (*destroy) (struct ttm_buffer_object *))
1199 struct ttm_operation_ctx ctx = { interruptible, false };
1202 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1203 page_alignment, &ctx, acc_size,
1209 ttm_bo_unreserve(bo);
1213 EXPORT_SYMBOL(ttm_bo_init);
1215 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1216 unsigned long bo_size,
1217 unsigned struct_size)
1219 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1222 size += ttm_round_pot(struct_size);
1223 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
1224 size += ttm_round_pot(sizeof(struct ttm_tt));
1227 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1229 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1231 struct ttm_bo_global *glob =
1232 container_of(kobj, struct ttm_bo_global, kobj);
1234 __free_page(glob->dummy_read_page);
1237 static void ttm_bo_global_release(void)
1239 struct ttm_bo_global *glob = &ttm_bo_glob;
1241 mutex_lock(&ttm_global_mutex);
1242 if (--ttm_bo_glob_use_count > 0)
1245 kobject_del(&glob->kobj);
1246 kobject_put(&glob->kobj);
1247 ttm_mem_global_release(&ttm_mem_glob);
1248 memset(glob, 0, sizeof(*glob));
1250 mutex_unlock(&ttm_global_mutex);
1253 static int ttm_bo_global_init(void)
1255 struct ttm_bo_global *glob = &ttm_bo_glob;
1259 mutex_lock(&ttm_global_mutex);
1260 if (++ttm_bo_glob_use_count > 1)
1263 ret = ttm_mem_global_init(&ttm_mem_glob);
1267 spin_lock_init(&glob->lru_lock);
1268 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1270 if (unlikely(glob->dummy_read_page == NULL)) {
1275 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1276 INIT_LIST_HEAD(&glob->swap_lru[i]);
1277 INIT_LIST_HEAD(&glob->device_list);
1278 atomic_set(&glob->bo_count, 0);
1280 ret = kobject_init_and_add(
1281 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1282 if (unlikely(ret != 0))
1283 kobject_put(&glob->kobj);
1285 mutex_unlock(&ttm_global_mutex);
1289 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1291 struct ttm_bo_global *glob = &ttm_bo_glob;
1294 struct ttm_resource_manager *man;
1296 man = ttm_manager_type(bdev, TTM_PL_SYSTEM);
1297 ttm_resource_manager_set_used(man, false);
1298 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL);
1300 mutex_lock(&ttm_global_mutex);
1301 list_del(&bdev->device_list);
1302 mutex_unlock(&ttm_global_mutex);
1304 cancel_delayed_work_sync(&bdev->wq);
1306 if (ttm_bo_delayed_delete(bdev, true))
1307 pr_debug("Delayed destroy list was clean\n");
1309 spin_lock(&glob->lru_lock);
1310 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1311 if (list_empty(&man->lru[0]))
1312 pr_debug("Swap list %d was clean\n", i);
1313 spin_unlock(&glob->lru_lock);
1315 ttm_pool_fini(&bdev->pool);
1318 ttm_bo_global_release();
1322 EXPORT_SYMBOL(ttm_bo_device_release);
1324 static void ttm_bo_init_sysman(struct ttm_bo_device *bdev)
1326 struct ttm_resource_manager *man = &bdev->sysman;
1329 * Initialize the system memory buffer type.
1330 * Other types need to be driver / IOCTL initialized.
1334 ttm_resource_manager_init(man, 0);
1335 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, man);
1336 ttm_resource_manager_set_used(man, true);
1339 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1340 struct ttm_bo_driver *driver,
1342 struct address_space *mapping,
1343 struct drm_vma_offset_manager *vma_manager,
1344 bool use_dma_alloc, bool use_dma32)
1346 struct ttm_bo_global *glob = &ttm_bo_glob;
1349 if (WARN_ON(vma_manager == NULL))
1352 ret = ttm_bo_global_init();
1356 bdev->driver = driver;
1358 ttm_bo_init_sysman(bdev);
1359 ttm_pool_init(&bdev->pool, dev, use_dma_alloc, use_dma32);
1361 bdev->vma_manager = vma_manager;
1362 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1363 INIT_LIST_HEAD(&bdev->ddestroy);
1364 bdev->dev_mapping = mapping;
1365 mutex_lock(&ttm_global_mutex);
1366 list_add_tail(&bdev->device_list, &glob->device_list);
1367 mutex_unlock(&ttm_global_mutex);
1371 EXPORT_SYMBOL(ttm_bo_device_init);
1374 * buffer object vm functions.
1377 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1379 struct ttm_bo_device *bdev = bo->bdev;
1381 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1382 ttm_mem_io_free(bdev, &bo->mem);
1384 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1386 int ttm_bo_wait(struct ttm_buffer_object *bo,
1387 bool interruptible, bool no_wait)
1389 long timeout = 15 * HZ;
1392 if (dma_resv_test_signaled_rcu(bo->base.resv, true))
1398 timeout = dma_resv_wait_timeout_rcu(bo->base.resv, true,
1399 interruptible, timeout);
1406 dma_resv_add_excl_fence(bo->base.resv, NULL);
1409 EXPORT_SYMBOL(ttm_bo_wait);
1412 * A buffer object shrink method that tries to swap out the first
1413 * buffer object on the bo_global::swap_lru list.
1415 int ttm_bo_swapout(struct ttm_operation_ctx *ctx)
1417 struct ttm_bo_global *glob = &ttm_bo_glob;
1418 struct ttm_buffer_object *bo;
1423 spin_lock(&glob->lru_lock);
1424 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1425 list_for_each_entry(bo, &glob->swap_lru[i], swap) {
1426 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
1430 if (!ttm_bo_get_unless_zero(bo)) {
1432 dma_resv_unlock(bo->base.resv);
1444 spin_unlock(&glob->lru_lock);
1449 ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1454 ttm_bo_del_from_lru(bo);
1455 spin_unlock(&glob->lru_lock);
1458 * Move to system cached
1461 if (bo->mem.mem_type != TTM_PL_SYSTEM) {
1462 struct ttm_operation_ctx ctx = { false, false };
1463 struct ttm_resource evict_mem;
1464 struct ttm_place hop;
1466 memset(&hop, 0, sizeof(hop));
1468 evict_mem = bo->mem;
1469 evict_mem.mm_node = NULL;
1470 evict_mem.placement = 0;
1471 evict_mem.mem_type = TTM_PL_SYSTEM;
1473 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx, &hop);
1474 if (unlikely(ret != 0)) {
1475 WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
1481 * Make sure BO is idle.
1484 ret = ttm_bo_wait(bo, false, false);
1485 if (unlikely(ret != 0))
1488 ttm_bo_unmap_virtual(bo);
1491 * Swap out. Buffer will be swapped in again as soon as
1492 * anyone tries to access a ttm page.
1495 if (bo->bdev->driver->swap_notify)
1496 bo->bdev->driver->swap_notify(bo);
1498 ret = ttm_tt_swapout(bo->bdev, bo->ttm);
1503 * Unreserve without putting on LRU to avoid swapping out an
1504 * already swapped buffer.
1507 dma_resv_unlock(bo->base.resv);
1511 EXPORT_SYMBOL(ttm_bo_swapout);
1513 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1515 if (bo->ttm == NULL)
1518 ttm_tt_destroy(bo->bdev, bo->ttm);