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"
48 * ttm_global_mutex - protecting the global BO state
50 DEFINE_MUTEX(ttm_global_mutex);
51 unsigned ttm_bo_glob_use_count;
52 struct ttm_bo_global ttm_bo_glob;
53 EXPORT_SYMBOL(ttm_bo_glob);
55 /* default destructor */
56 static void ttm_bo_default_destroy(struct ttm_buffer_object *bo)
61 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
62 struct ttm_placement *placement)
64 struct drm_printer p = drm_debug_printer(TTM_PFX);
65 struct ttm_resource_manager *man;
68 drm_printf(&p, "No space for %p (%lu pages, %zuK, %zuM)\n",
69 bo, bo->mem.num_pages, bo->base.size >> 10,
71 for (i = 0; i < placement->num_placement; i++) {
72 mem_type = placement->placement[i].mem_type;
73 drm_printf(&p, " placement[%d]=0x%08X (%d)\n",
74 i, placement->placement[i].flags, mem_type);
75 man = ttm_manager_type(bo->bdev, mem_type);
76 ttm_resource_manager_debug(man, &p);
80 static void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
82 struct ttm_bo_device *bdev = bo->bdev;
84 list_del_init(&bo->swap);
85 list_del_init(&bo->lru);
87 if (bdev->driver->del_from_lru_notify)
88 bdev->driver->del_from_lru_notify(bo);
91 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
92 struct ttm_buffer_object *bo)
99 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
100 struct ttm_resource *mem,
101 struct ttm_lru_bulk_move *bulk)
103 struct ttm_bo_device *bdev = bo->bdev;
104 struct ttm_resource_manager *man;
106 dma_resv_assert_held(bo->base.resv);
109 ttm_bo_del_from_lru(bo);
113 man = ttm_manager_type(bdev, mem->mem_type);
114 list_move_tail(&bo->lru, &man->lru[bo->priority]);
115 if (man->use_tt && bo->ttm &&
116 !(bo->ttm->page_flags & (TTM_PAGE_FLAG_SG |
117 TTM_PAGE_FLAG_SWAPPED))) {
118 struct list_head *swap;
120 swap = &ttm_bo_glob.swap_lru[bo->priority];
121 list_move_tail(&bo->swap, swap);
124 if (bdev->driver->del_from_lru_notify)
125 bdev->driver->del_from_lru_notify(bo);
127 if (bulk && !bo->pin_count) {
128 switch (bo->mem.mem_type) {
130 ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
134 ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
137 if (bo->ttm && !(bo->ttm->page_flags &
138 (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED)))
139 ttm_bo_bulk_move_set_pos(&bulk->swap[bo->priority], bo);
142 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
144 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
148 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
149 struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
150 struct ttm_resource_manager *man;
155 dma_resv_assert_held(pos->first->base.resv);
156 dma_resv_assert_held(pos->last->base.resv);
158 man = ttm_manager_type(pos->first->bdev, TTM_PL_TT);
159 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
163 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
164 struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
165 struct ttm_resource_manager *man;
170 dma_resv_assert_held(pos->first->base.resv);
171 dma_resv_assert_held(pos->last->base.resv);
173 man = ttm_manager_type(pos->first->bdev, TTM_PL_VRAM);
174 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
178 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
179 struct ttm_lru_bulk_move_pos *pos = &bulk->swap[i];
180 struct list_head *lru;
185 dma_resv_assert_held(pos->first->base.resv);
186 dma_resv_assert_held(pos->last->base.resv);
188 lru = &ttm_bo_glob.swap_lru[i];
189 list_bulk_move_tail(lru, &pos->first->swap, &pos->last->swap);
192 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
194 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
195 struct ttm_resource *mem, bool evict,
196 struct ttm_operation_ctx *ctx,
197 struct ttm_place *hop)
199 struct ttm_bo_device *bdev = bo->bdev;
200 struct ttm_resource_manager *old_man = ttm_manager_type(bdev, bo->mem.mem_type);
201 struct ttm_resource_manager *new_man = ttm_manager_type(bdev, mem->mem_type);
204 ttm_bo_unmap_virtual(bo);
207 * Create and bind a ttm if required.
210 if (new_man->use_tt) {
211 /* Zero init the new TTM structure if the old location should
212 * have used one as well.
214 ret = ttm_tt_create(bo, old_man->use_tt);
218 if (mem->mem_type != TTM_PL_SYSTEM) {
219 ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
225 ret = bdev->driver->move(bo, evict, ctx, mem, hop);
227 if (ret == -EMULTIHOP)
232 ctx->bytes_moved += bo->base.size;
236 new_man = ttm_manager_type(bdev, bo->mem.mem_type);
237 if (!new_man->use_tt)
238 ttm_bo_tt_destroy(bo);
245 * Will release GPU memory type usage on destruction.
246 * This is the place to put in driver specific hooks to release
247 * driver private resources.
248 * Will release the bo::reserved lock.
251 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
253 if (bo->bdev->driver->delete_mem_notify)
254 bo->bdev->driver->delete_mem_notify(bo);
256 ttm_bo_tt_destroy(bo);
257 ttm_resource_free(bo, &bo->mem);
260 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
264 if (bo->base.resv == &bo->base._resv)
267 BUG_ON(!dma_resv_trylock(&bo->base._resv));
269 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
270 dma_resv_unlock(&bo->base._resv);
274 if (bo->type != ttm_bo_type_sg) {
275 /* This works because the BO is about to be destroyed and nobody
276 * reference it any more. The only tricky case is the trylock on
277 * the resv object while holding the lru_lock.
279 spin_lock(&ttm_bo_glob.lru_lock);
280 bo->base.resv = &bo->base._resv;
281 spin_unlock(&ttm_bo_glob.lru_lock);
287 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
289 struct dma_resv *resv = &bo->base._resv;
290 struct dma_resv_list *fobj;
291 struct dma_fence *fence;
295 fobj = rcu_dereference(resv->fence);
296 fence = rcu_dereference(resv->fence_excl);
297 if (fence && !fence->ops->signaled)
298 dma_fence_enable_sw_signaling(fence);
300 for (i = 0; fobj && i < fobj->shared_count; ++i) {
301 fence = rcu_dereference(fobj->shared[i]);
303 if (!fence->ops->signaled)
304 dma_fence_enable_sw_signaling(fence);
310 * function ttm_bo_cleanup_refs
311 * If bo idle, remove from lru lists, and unref.
312 * If not idle, block if possible.
314 * Must be called with lru_lock and reservation held, this function
315 * will drop the lru lock and optionally the reservation lock before returning.
317 * @bo: The buffer object to clean-up
318 * @interruptible: Any sleeps should occur interruptibly.
319 * @no_wait_gpu: Never wait for gpu. Return -EBUSY instead.
320 * @unlock_resv: Unlock the reservation lock as well.
323 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
324 bool interruptible, bool no_wait_gpu,
327 struct dma_resv *resv = &bo->base._resv;
330 if (dma_resv_test_signaled_rcu(resv, true))
335 if (ret && !no_wait_gpu) {
339 dma_resv_unlock(bo->base.resv);
340 spin_unlock(&ttm_bo_glob.lru_lock);
342 lret = dma_resv_wait_timeout_rcu(resv, true, interruptible,
350 spin_lock(&ttm_bo_glob.lru_lock);
351 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
353 * We raced, and lost, someone else holds the reservation now,
354 * and is probably busy in ttm_bo_cleanup_memtype_use.
356 * Even if it's not the case, because we finished waiting any
357 * delayed destruction would succeed, so just return success
360 spin_unlock(&ttm_bo_glob.lru_lock);
366 if (ret || unlikely(list_empty(&bo->ddestroy))) {
368 dma_resv_unlock(bo->base.resv);
369 spin_unlock(&ttm_bo_glob.lru_lock);
373 ttm_bo_del_from_lru(bo);
374 list_del_init(&bo->ddestroy);
375 spin_unlock(&ttm_bo_glob.lru_lock);
376 ttm_bo_cleanup_memtype_use(bo);
379 dma_resv_unlock(bo->base.resv);
387 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
388 * encountered buffers.
390 static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
392 struct ttm_bo_global *glob = &ttm_bo_glob;
393 struct list_head removed;
396 INIT_LIST_HEAD(&removed);
398 spin_lock(&glob->lru_lock);
399 while (!list_empty(&bdev->ddestroy)) {
400 struct ttm_buffer_object *bo;
402 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
404 list_move_tail(&bo->ddestroy, &removed);
405 if (!ttm_bo_get_unless_zero(bo))
408 if (remove_all || bo->base.resv != &bo->base._resv) {
409 spin_unlock(&glob->lru_lock);
410 dma_resv_lock(bo->base.resv, NULL);
412 spin_lock(&glob->lru_lock);
413 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
415 } else if (dma_resv_trylock(bo->base.resv)) {
416 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
418 spin_unlock(&glob->lru_lock);
422 spin_lock(&glob->lru_lock);
424 list_splice_tail(&removed, &bdev->ddestroy);
425 empty = list_empty(&bdev->ddestroy);
426 spin_unlock(&glob->lru_lock);
431 static void ttm_bo_delayed_workqueue(struct work_struct *work)
433 struct ttm_bo_device *bdev =
434 container_of(work, struct ttm_bo_device, wq.work);
436 if (!ttm_bo_delayed_delete(bdev, false))
437 schedule_delayed_work(&bdev->wq,
438 ((HZ / 100) < 1) ? 1 : HZ / 100);
441 static void ttm_bo_release(struct kref *kref)
443 struct ttm_buffer_object *bo =
444 container_of(kref, struct ttm_buffer_object, kref);
445 struct ttm_bo_device *bdev = bo->bdev;
446 size_t acc_size = bo->acc_size;
450 ret = ttm_bo_individualize_resv(bo);
452 /* Last resort, if we fail to allocate memory for the
453 * fences block for the BO to become idle
455 dma_resv_wait_timeout_rcu(bo->base.resv, true, false,
459 if (bo->bdev->driver->release_notify)
460 bo->bdev->driver->release_notify(bo);
462 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
463 ttm_mem_io_free(bdev, &bo->mem);
466 if (!dma_resv_test_signaled_rcu(bo->base.resv, true) ||
467 !dma_resv_trylock(bo->base.resv)) {
468 /* The BO is not idle, resurrect it for delayed destroy */
469 ttm_bo_flush_all_fences(bo);
472 spin_lock(&ttm_bo_glob.lru_lock);
475 * Make pinned bos immediately available to
476 * shrinkers, now that they are queued for
479 if (WARN_ON(bo->pin_count)) {
481 ttm_bo_move_to_lru_tail(bo, &bo->mem, NULL);
484 kref_init(&bo->kref);
485 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
486 spin_unlock(&ttm_bo_glob.lru_lock);
488 schedule_delayed_work(&bdev->wq,
489 ((HZ / 100) < 1) ? 1 : HZ / 100);
493 spin_lock(&ttm_bo_glob.lru_lock);
494 ttm_bo_del_from_lru(bo);
495 list_del(&bo->ddestroy);
496 spin_unlock(&ttm_bo_glob.lru_lock);
498 ttm_bo_cleanup_memtype_use(bo);
499 dma_resv_unlock(bo->base.resv);
501 atomic_dec(&ttm_bo_glob.bo_count);
502 dma_fence_put(bo->moving);
503 if (!ttm_bo_uses_embedded_gem_object(bo))
504 dma_resv_fini(&bo->base._resv);
506 ttm_mem_global_free(&ttm_mem_glob, acc_size);
509 void ttm_bo_put(struct ttm_buffer_object *bo)
511 kref_put(&bo->kref, ttm_bo_release);
513 EXPORT_SYMBOL(ttm_bo_put);
515 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
517 return cancel_delayed_work_sync(&bdev->wq);
519 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
521 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
524 schedule_delayed_work(&bdev->wq,
525 ((HZ / 100) < 1) ? 1 : HZ / 100);
527 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
529 static int ttm_bo_evict(struct ttm_buffer_object *bo,
530 struct ttm_operation_ctx *ctx)
532 struct ttm_bo_device *bdev = bo->bdev;
533 struct ttm_resource evict_mem;
534 struct ttm_placement placement;
535 struct ttm_place hop;
538 memset(&hop, 0, sizeof(hop));
540 dma_resv_assert_held(bo->base.resv);
542 placement.num_placement = 0;
543 placement.num_busy_placement = 0;
544 bdev->driver->evict_flags(bo, &placement);
546 if (!placement.num_placement && !placement.num_busy_placement) {
547 ttm_bo_wait(bo, false, false);
549 ttm_bo_cleanup_memtype_use(bo);
550 return ttm_tt_create(bo, false);
554 evict_mem.mm_node = NULL;
555 evict_mem.bus.offset = 0;
556 evict_mem.bus.addr = NULL;
558 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
560 if (ret != -ERESTARTSYS) {
561 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
563 ttm_bo_mem_space_debug(bo, &placement);
568 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx, &hop);
570 WARN(ret == -EMULTIHOP, "Unexpected multihop in eviction - likely driver bug\n");
571 if (ret != -ERESTARTSYS)
572 pr_err("Buffer eviction failed\n");
573 ttm_resource_free(bo, &evict_mem);
579 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
580 const struct ttm_place *place)
582 /* Don't evict this BO if it's outside of the
583 * requested placement range
585 if (place->fpfn >= (bo->mem.start + bo->mem.num_pages) ||
586 (place->lpfn && place->lpfn <= bo->mem.start))
591 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
594 * Check the target bo is allowable to be evicted or swapout, including cases:
596 * a. if share same reservation object with ctx->resv, have assumption
597 * reservation objects should already be locked, so not lock again and
598 * return true directly when either the opreation allow_reserved_eviction
599 * or the target bo already is in delayed free list;
601 * b. Otherwise, trylock it.
603 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
604 struct ttm_operation_ctx *ctx, bool *locked, bool *busy)
608 if (bo->base.resv == ctx->resv) {
609 dma_resv_assert_held(bo->base.resv);
610 if (ctx->allow_res_evict)
616 ret = dma_resv_trylock(bo->base.resv);
626 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
628 * @busy_bo: BO which couldn't be locked with trylock
629 * @ctx: operation context
630 * @ticket: acquire ticket
632 * Try to lock a busy buffer object to avoid failing eviction.
634 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
635 struct ttm_operation_ctx *ctx,
636 struct ww_acquire_ctx *ticket)
640 if (!busy_bo || !ticket)
643 if (ctx->interruptible)
644 r = dma_resv_lock_interruptible(busy_bo->base.resv,
647 r = dma_resv_lock(busy_bo->base.resv, ticket);
650 * TODO: It would be better to keep the BO locked until allocation is at
651 * least tried one more time, but that would mean a much larger rework
655 dma_resv_unlock(busy_bo->base.resv);
657 return r == -EDEADLK ? -EBUSY : r;
660 int ttm_mem_evict_first(struct ttm_bo_device *bdev,
661 struct ttm_resource_manager *man,
662 const struct ttm_place *place,
663 struct ttm_operation_ctx *ctx,
664 struct ww_acquire_ctx *ticket)
666 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
671 spin_lock(&ttm_bo_glob.lru_lock);
672 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
673 list_for_each_entry(bo, &man->lru[i], lru) {
676 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
678 if (busy && !busy_bo && ticket !=
679 dma_resv_locking_ctx(bo->base.resv))
684 if (place && !bdev->driver->eviction_valuable(bo,
687 dma_resv_unlock(bo->base.resv);
690 if (!ttm_bo_get_unless_zero(bo)) {
692 dma_resv_unlock(bo->base.resv);
698 /* If the inner loop terminated early, we have our candidate */
699 if (&bo->lru != &man->lru[i])
706 if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
708 spin_unlock(&ttm_bo_glob.lru_lock);
709 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
716 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
717 ctx->no_wait_gpu, locked);
722 spin_unlock(&ttm_bo_glob.lru_lock);
724 ret = ttm_bo_evict(bo, ctx);
726 ttm_bo_unreserve(bo);
733 * Add the last move fence to the BO and reserve a new shared slot.
735 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
736 struct ttm_resource_manager *man,
737 struct ttm_resource *mem,
740 struct dma_fence *fence;
743 spin_lock(&man->move_lock);
744 fence = dma_fence_get(man->move);
745 spin_unlock(&man->move_lock);
751 dma_fence_put(fence);
755 dma_resv_add_shared_fence(bo->base.resv, fence);
757 ret = dma_resv_reserve_shared(bo->base.resv, 1);
759 dma_fence_put(fence);
763 dma_fence_put(bo->moving);
769 * Repeatedly evict memory from the LRU for @mem_type until we create enough
770 * space, or we've evicted everything and there isn't enough space.
772 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
773 const struct ttm_place *place,
774 struct ttm_resource *mem,
775 struct ttm_operation_ctx *ctx)
777 struct ttm_bo_device *bdev = bo->bdev;
778 struct ttm_resource_manager *man = ttm_manager_type(bdev, mem->mem_type);
779 struct ww_acquire_ctx *ticket;
782 ticket = dma_resv_locking_ctx(bo->base.resv);
784 ret = ttm_resource_alloc(bo, place, mem);
787 if (unlikely(ret != -ENOSPC))
789 ret = ttm_mem_evict_first(bdev, man, place, ctx,
791 if (unlikely(ret != 0))
795 return ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
799 * ttm_bo_mem_placement - check if placement is compatible
800 * @bo: BO to find memory for
801 * @place: where to search
802 * @mem: the memory object to fill in
804 * Check if placement is compatible and fill in mem structure.
805 * Returns -EBUSY if placement won't work or negative error code.
806 * 0 when placement can be used.
808 static int ttm_bo_mem_placement(struct ttm_buffer_object *bo,
809 const struct ttm_place *place,
810 struct ttm_resource *mem)
812 struct ttm_bo_device *bdev = bo->bdev;
813 struct ttm_resource_manager *man;
815 man = ttm_manager_type(bdev, place->mem_type);
816 if (!man || !ttm_resource_manager_used(man))
819 mem->mem_type = place->mem_type;
820 mem->placement = place->flags;
822 spin_lock(&ttm_bo_glob.lru_lock);
823 ttm_bo_move_to_lru_tail(bo, mem, NULL);
824 spin_unlock(&ttm_bo_glob.lru_lock);
830 * Creates space for memory region @mem according to its type.
832 * This function first searches for free space in compatible memory types in
833 * the priority order defined by the driver. If free space isn't found, then
834 * ttm_bo_mem_force_space is attempted in priority order to evict and find
837 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
838 struct ttm_placement *placement,
839 struct ttm_resource *mem,
840 struct ttm_operation_ctx *ctx)
842 struct ttm_bo_device *bdev = bo->bdev;
843 bool type_found = false;
846 ret = dma_resv_reserve_shared(bo->base.resv, 1);
850 for (i = 0; i < placement->num_placement; ++i) {
851 const struct ttm_place *place = &placement->placement[i];
852 struct ttm_resource_manager *man;
854 ret = ttm_bo_mem_placement(bo, place, mem);
859 ret = ttm_resource_alloc(bo, place, mem);
865 man = ttm_manager_type(bdev, mem->mem_type);
866 ret = ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
868 ttm_resource_free(bo, mem);
877 for (i = 0; i < placement->num_busy_placement; ++i) {
878 const struct ttm_place *place = &placement->busy_placement[i];
880 ret = ttm_bo_mem_placement(bo, place, mem);
885 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
889 if (ret && ret != -EBUSY)
895 pr_err(TTM_PFX "No compatible memory type found\n");
900 if (bo->mem.mem_type == TTM_PL_SYSTEM && !bo->pin_count)
901 ttm_bo_move_to_lru_tail_unlocked(bo);
905 EXPORT_SYMBOL(ttm_bo_mem_space);
907 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
908 struct ttm_resource *mem,
909 struct ttm_operation_ctx *ctx,
910 struct ttm_place *hop)
912 struct ttm_placement hop_placement;
914 struct ttm_resource hop_mem = *mem;
916 hop_mem.mm_node = NULL;
917 hop_mem.mem_type = TTM_PL_SYSTEM;
918 hop_mem.placement = 0;
920 hop_placement.num_placement = hop_placement.num_busy_placement = 1;
921 hop_placement.placement = hop_placement.busy_placement = hop;
923 /* find space in the bounce domain */
924 ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
927 /* move to the bounce domain */
928 ret = ttm_bo_handle_move_mem(bo, &hop_mem, false, ctx, NULL);
934 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
935 struct ttm_placement *placement,
936 struct ttm_operation_ctx *ctx)
939 struct ttm_place hop;
940 struct ttm_resource mem;
942 dma_resv_assert_held(bo->base.resv);
944 memset(&hop, 0, sizeof(hop));
946 mem.num_pages = PAGE_ALIGN(bo->base.size) >> PAGE_SHIFT;
947 mem.page_alignment = bo->mem.page_alignment;
953 * Determine where to move the buffer.
955 * If driver determines move is going to need
956 * an extra step then it will return -EMULTIHOP
957 * and the buffer will be moved to the temporary
958 * stop and the driver will be called to make
962 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
965 ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx, &hop);
966 if (ret == -EMULTIHOP) {
967 ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
970 /* try and move to final place now. */
974 ttm_resource_free(bo, &mem);
978 static bool ttm_bo_places_compat(const struct ttm_place *places,
979 unsigned num_placement,
980 struct ttm_resource *mem,
985 for (i = 0; i < num_placement; i++) {
986 const struct ttm_place *heap = &places[i];
988 if ((mem->start < heap->fpfn ||
989 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
992 *new_flags = heap->flags;
993 if ((mem->mem_type == heap->mem_type) &&
994 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
995 (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
1001 bool ttm_bo_mem_compat(struct ttm_placement *placement,
1002 struct ttm_resource *mem,
1003 uint32_t *new_flags)
1005 if (ttm_bo_places_compat(placement->placement, placement->num_placement,
1009 if ((placement->busy_placement != placement->placement ||
1010 placement->num_busy_placement > placement->num_placement) &&
1011 ttm_bo_places_compat(placement->busy_placement,
1012 placement->num_busy_placement,
1018 EXPORT_SYMBOL(ttm_bo_mem_compat);
1020 int ttm_bo_validate(struct ttm_buffer_object *bo,
1021 struct ttm_placement *placement,
1022 struct ttm_operation_ctx *ctx)
1027 dma_resv_assert_held(bo->base.resv);
1030 * Remove the backing store if no placement is given.
1032 if (!placement->num_placement && !placement->num_busy_placement) {
1033 ret = ttm_bo_pipeline_gutting(bo);
1037 return ttm_tt_create(bo, false);
1041 * Check whether we need to move buffer.
1043 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1044 ret = ttm_bo_move_buffer(bo, placement, ctx);
1049 * We might need to add a TTM.
1051 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
1052 ret = ttm_tt_create(bo, true);
1058 EXPORT_SYMBOL(ttm_bo_validate);
1060 int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
1061 struct ttm_buffer_object *bo,
1063 enum ttm_bo_type type,
1064 struct ttm_placement *placement,
1065 uint32_t page_alignment,
1066 struct ttm_operation_ctx *ctx,
1068 struct sg_table *sg,
1069 struct dma_resv *resv,
1070 void (*destroy) (struct ttm_buffer_object *))
1072 struct ttm_mem_global *mem_glob = &ttm_mem_glob;
1076 ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
1078 pr_err("Out of kernel memory\n");
1086 bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1088 kref_init(&bo->kref);
1089 INIT_LIST_HEAD(&bo->lru);
1090 INIT_LIST_HEAD(&bo->ddestroy);
1091 INIT_LIST_HEAD(&bo->swap);
1094 bo->mem.mem_type = TTM_PL_SYSTEM;
1095 bo->mem.num_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1096 bo->mem.mm_node = NULL;
1097 bo->mem.page_alignment = page_alignment;
1098 bo->mem.bus.offset = 0;
1099 bo->mem.bus.addr = NULL;
1101 bo->mem.placement = 0;
1102 bo->acc_size = acc_size;
1106 bo->base.resv = resv;
1107 dma_resv_assert_held(bo->base.resv);
1109 bo->base.resv = &bo->base._resv;
1111 if (!ttm_bo_uses_embedded_gem_object(bo)) {
1113 * bo.base is not initialized, so we have to setup the
1114 * struct elements we want use regardless.
1116 bo->base.size = size;
1117 dma_resv_init(&bo->base._resv);
1118 drm_vma_node_reset(&bo->base.vma_node);
1120 atomic_inc(&ttm_bo_glob.bo_count);
1123 * For ttm_bo_type_device buffers, allocate
1124 * address space from the device.
1126 if (bo->type == ttm_bo_type_device ||
1127 bo->type == ttm_bo_type_sg)
1128 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
1131 /* passed reservation objects should already be locked,
1132 * since otherwise lockdep will be angered in radeon.
1135 locked = dma_resv_trylock(bo->base.resv);
1140 ret = ttm_bo_validate(bo, placement, ctx);
1142 if (unlikely(ret)) {
1144 ttm_bo_unreserve(bo);
1150 ttm_bo_move_to_lru_tail_unlocked(bo);
1154 EXPORT_SYMBOL(ttm_bo_init_reserved);
1156 int ttm_bo_init(struct ttm_bo_device *bdev,
1157 struct ttm_buffer_object *bo,
1159 enum ttm_bo_type type,
1160 struct ttm_placement *placement,
1161 uint32_t page_alignment,
1164 struct sg_table *sg,
1165 struct dma_resv *resv,
1166 void (*destroy) (struct ttm_buffer_object *))
1168 struct ttm_operation_ctx ctx = { interruptible, false };
1171 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1172 page_alignment, &ctx, acc_size,
1178 ttm_bo_unreserve(bo);
1182 EXPORT_SYMBOL(ttm_bo_init);
1184 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1185 unsigned long bo_size,
1186 unsigned struct_size)
1188 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1191 size += ttm_round_pot(struct_size);
1192 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
1193 size += ttm_round_pot(sizeof(struct ttm_tt));
1196 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1198 static void ttm_bo_global_release(void)
1200 struct ttm_bo_global *glob = &ttm_bo_glob;
1202 mutex_lock(&ttm_global_mutex);
1203 if (--ttm_bo_glob_use_count > 0)
1206 kobject_del(&glob->kobj);
1207 kobject_put(&glob->kobj);
1208 ttm_mem_global_release(&ttm_mem_glob);
1209 __free_page(glob->dummy_read_page);
1210 memset(glob, 0, sizeof(*glob));
1212 mutex_unlock(&ttm_global_mutex);
1215 static int ttm_bo_global_init(void)
1217 struct ttm_bo_global *glob = &ttm_bo_glob;
1221 mutex_lock(&ttm_global_mutex);
1222 if (++ttm_bo_glob_use_count > 1)
1225 ret = ttm_mem_global_init(&ttm_mem_glob);
1229 spin_lock_init(&glob->lru_lock);
1230 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1232 if (unlikely(glob->dummy_read_page == NULL)) {
1237 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1238 INIT_LIST_HEAD(&glob->swap_lru[i]);
1239 INIT_LIST_HEAD(&glob->device_list);
1240 atomic_set(&glob->bo_count, 0);
1242 debugfs_create_atomic_t("buffer_objects", 0444, ttm_debugfs_root,
1245 mutex_unlock(&ttm_global_mutex);
1249 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1251 struct ttm_bo_global *glob = &ttm_bo_glob;
1254 struct ttm_resource_manager *man;
1256 man = ttm_manager_type(bdev, TTM_PL_SYSTEM);
1257 ttm_resource_manager_set_used(man, false);
1258 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL);
1260 mutex_lock(&ttm_global_mutex);
1261 list_del(&bdev->device_list);
1262 mutex_unlock(&ttm_global_mutex);
1264 cancel_delayed_work_sync(&bdev->wq);
1266 if (ttm_bo_delayed_delete(bdev, true))
1267 pr_debug("Delayed destroy list was clean\n");
1269 spin_lock(&glob->lru_lock);
1270 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1271 if (list_empty(&man->lru[0]))
1272 pr_debug("Swap list %d was clean\n", i);
1273 spin_unlock(&glob->lru_lock);
1275 ttm_pool_fini(&bdev->pool);
1278 ttm_bo_global_release();
1282 EXPORT_SYMBOL(ttm_bo_device_release);
1284 static void ttm_bo_init_sysman(struct ttm_bo_device *bdev)
1286 struct ttm_resource_manager *man = &bdev->sysman;
1289 * Initialize the system memory buffer type.
1290 * Other types need to be driver / IOCTL initialized.
1294 ttm_resource_manager_init(man, 0);
1295 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, man);
1296 ttm_resource_manager_set_used(man, true);
1299 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1300 struct ttm_bo_driver *driver,
1302 struct address_space *mapping,
1303 struct drm_vma_offset_manager *vma_manager,
1304 bool use_dma_alloc, bool use_dma32)
1306 struct ttm_bo_global *glob = &ttm_bo_glob;
1309 if (WARN_ON(vma_manager == NULL))
1312 ret = ttm_bo_global_init();
1316 bdev->driver = driver;
1318 ttm_bo_init_sysman(bdev);
1319 ttm_pool_init(&bdev->pool, dev, use_dma_alloc, use_dma32);
1321 bdev->vma_manager = vma_manager;
1322 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1323 INIT_LIST_HEAD(&bdev->ddestroy);
1324 bdev->dev_mapping = mapping;
1325 mutex_lock(&ttm_global_mutex);
1326 list_add_tail(&bdev->device_list, &glob->device_list);
1327 mutex_unlock(&ttm_global_mutex);
1331 EXPORT_SYMBOL(ttm_bo_device_init);
1334 * buffer object vm functions.
1337 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1339 struct ttm_bo_device *bdev = bo->bdev;
1341 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1342 ttm_mem_io_free(bdev, &bo->mem);
1344 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1346 int ttm_bo_wait(struct ttm_buffer_object *bo,
1347 bool interruptible, bool no_wait)
1349 long timeout = 15 * HZ;
1352 if (dma_resv_test_signaled_rcu(bo->base.resv, true))
1358 timeout = dma_resv_wait_timeout_rcu(bo->base.resv, true,
1359 interruptible, timeout);
1366 dma_resv_add_excl_fence(bo->base.resv, NULL);
1369 EXPORT_SYMBOL(ttm_bo_wait);
1372 * A buffer object shrink method that tries to swap out the first
1373 * buffer object on the bo_global::swap_lru list.
1375 int ttm_bo_swapout(struct ttm_operation_ctx *ctx)
1377 struct ttm_bo_global *glob = &ttm_bo_glob;
1378 struct ttm_buffer_object *bo;
1383 spin_lock(&glob->lru_lock);
1384 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1385 list_for_each_entry(bo, &glob->swap_lru[i], swap) {
1386 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
1390 if (!ttm_bo_get_unless_zero(bo)) {
1392 dma_resv_unlock(bo->base.resv);
1404 spin_unlock(&glob->lru_lock);
1409 ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1414 ttm_bo_del_from_lru(bo);
1415 spin_unlock(&glob->lru_lock);
1418 * Move to system cached
1421 if (bo->mem.mem_type != TTM_PL_SYSTEM) {
1422 struct ttm_operation_ctx ctx = { false, false };
1423 struct ttm_resource evict_mem;
1424 struct ttm_place hop;
1426 memset(&hop, 0, sizeof(hop));
1428 evict_mem = bo->mem;
1429 evict_mem.mm_node = NULL;
1430 evict_mem.placement = 0;
1431 evict_mem.mem_type = TTM_PL_SYSTEM;
1433 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx, &hop);
1434 if (unlikely(ret != 0)) {
1435 WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
1441 * Make sure BO is idle.
1444 ret = ttm_bo_wait(bo, false, false);
1445 if (unlikely(ret != 0))
1448 ttm_bo_unmap_virtual(bo);
1451 * Swap out. Buffer will be swapped in again as soon as
1452 * anyone tries to access a ttm page.
1455 if (bo->bdev->driver->swap_notify)
1456 bo->bdev->driver->swap_notify(bo);
1458 ret = ttm_tt_swapout(bo->bdev, bo->ttm);
1463 * Unreserve without putting on LRU to avoid swapping out an
1464 * already swapped buffer.
1467 dma_resv_unlock(bo->base.resv);
1471 EXPORT_SYMBOL(ttm_bo_swapout);
1473 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1475 if (bo->ttm == NULL)
1478 ttm_tt_destroy(bo->bdev, bo->ttm);