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 /* default destructor */
48 static void ttm_bo_default_destroy(struct ttm_buffer_object *bo)
53 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
54 struct ttm_placement *placement)
56 struct drm_printer p = drm_debug_printer(TTM_PFX);
57 struct ttm_resource_manager *man;
60 drm_printf(&p, "No space for %p (%lu pages, %zuK, %zuM)\n",
61 bo, bo->mem.num_pages, bo->base.size >> 10,
63 for (i = 0; i < placement->num_placement; i++) {
64 mem_type = placement->placement[i].mem_type;
65 drm_printf(&p, " placement[%d]=0x%08X (%d)\n",
66 i, placement->placement[i].flags, mem_type);
67 man = ttm_manager_type(bo->bdev, mem_type);
68 ttm_resource_manager_debug(man, &p);
72 static void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
74 struct ttm_device *bdev = bo->bdev;
76 list_del_init(&bo->lru);
78 if (bdev->funcs->del_from_lru_notify)
79 bdev->funcs->del_from_lru_notify(bo);
82 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
83 struct ttm_buffer_object *bo)
90 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
91 struct ttm_resource *mem,
92 struct ttm_lru_bulk_move *bulk)
94 struct ttm_device *bdev = bo->bdev;
95 struct ttm_resource_manager *man;
98 dma_resv_assert_held(bo->base.resv);
101 ttm_bo_del_from_lru(bo);
105 man = ttm_manager_type(bdev, mem->mem_type);
106 list_move_tail(&bo->lru, &man->lru[bo->priority]);
108 if (bdev->funcs->del_from_lru_notify)
109 bdev->funcs->del_from_lru_notify(bo);
111 if (bulk && !bo->pin_count) {
112 switch (bo->mem.mem_type) {
114 ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
118 ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
123 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
125 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
129 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
130 struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
131 struct ttm_resource_manager *man;
136 dma_resv_assert_held(pos->first->base.resv);
137 dma_resv_assert_held(pos->last->base.resv);
139 man = ttm_manager_type(pos->first->bdev, TTM_PL_TT);
140 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
144 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
145 struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
146 struct ttm_resource_manager *man;
151 dma_resv_assert_held(pos->first->base.resv);
152 dma_resv_assert_held(pos->last->base.resv);
154 man = ttm_manager_type(pos->first->bdev, TTM_PL_VRAM);
155 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
159 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
161 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
162 struct ttm_resource *mem, bool evict,
163 struct ttm_operation_ctx *ctx,
164 struct ttm_place *hop)
166 struct ttm_device *bdev = bo->bdev;
167 struct ttm_resource_manager *old_man = ttm_manager_type(bdev, bo->mem.mem_type);
168 struct ttm_resource_manager *new_man = ttm_manager_type(bdev, mem->mem_type);
171 ttm_bo_unmap_virtual(bo);
174 * Create and bind a ttm if required.
177 if (new_man->use_tt) {
178 /* Zero init the new TTM structure if the old location should
179 * have used one as well.
181 ret = ttm_tt_create(bo, old_man->use_tt);
185 if (mem->mem_type != TTM_PL_SYSTEM) {
186 ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
192 ret = bdev->funcs->move(bo, evict, ctx, mem, hop);
194 if (ret == -EMULTIHOP)
199 ctx->bytes_moved += bo->base.size;
203 new_man = ttm_manager_type(bdev, bo->mem.mem_type);
204 if (!new_man->use_tt)
205 ttm_bo_tt_destroy(bo);
212 * Will release GPU memory type usage on destruction.
213 * This is the place to put in driver specific hooks to release
214 * driver private resources.
215 * Will release the bo::reserved lock.
218 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
220 if (bo->bdev->funcs->delete_mem_notify)
221 bo->bdev->funcs->delete_mem_notify(bo);
223 ttm_bo_tt_destroy(bo);
224 ttm_resource_free(bo, &bo->mem);
227 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
231 if (bo->base.resv == &bo->base._resv)
234 BUG_ON(!dma_resv_trylock(&bo->base._resv));
236 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
237 dma_resv_unlock(&bo->base._resv);
241 if (bo->type != ttm_bo_type_sg) {
242 /* This works because the BO is about to be destroyed and nobody
243 * reference it any more. The only tricky case is the trylock on
244 * the resv object while holding the lru_lock.
246 spin_lock(&bo->bdev->lru_lock);
247 bo->base.resv = &bo->base._resv;
248 spin_unlock(&bo->bdev->lru_lock);
254 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
256 struct dma_resv *resv = &bo->base._resv;
257 struct dma_resv_list *fobj;
258 struct dma_fence *fence;
262 fobj = rcu_dereference(resv->fence);
263 fence = rcu_dereference(resv->fence_excl);
264 if (fence && !fence->ops->signaled)
265 dma_fence_enable_sw_signaling(fence);
267 for (i = 0; fobj && i < fobj->shared_count; ++i) {
268 fence = rcu_dereference(fobj->shared[i]);
270 if (!fence->ops->signaled)
271 dma_fence_enable_sw_signaling(fence);
277 * function ttm_bo_cleanup_refs
278 * If bo idle, remove from lru lists, and unref.
279 * If not idle, block if possible.
281 * Must be called with lru_lock and reservation held, this function
282 * will drop the lru lock and optionally the reservation lock before returning.
284 * @bo: The buffer object to clean-up
285 * @interruptible: Any sleeps should occur interruptibly.
286 * @no_wait_gpu: Never wait for gpu. Return -EBUSY instead.
287 * @unlock_resv: Unlock the reservation lock as well.
290 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
291 bool interruptible, bool no_wait_gpu,
294 struct dma_resv *resv = &bo->base._resv;
297 if (dma_resv_test_signaled_rcu(resv, true))
302 if (ret && !no_wait_gpu) {
306 dma_resv_unlock(bo->base.resv);
307 spin_unlock(&bo->bdev->lru_lock);
309 lret = dma_resv_wait_timeout_rcu(resv, true, interruptible,
317 spin_lock(&bo->bdev->lru_lock);
318 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
320 * We raced, and lost, someone else holds the reservation now,
321 * and is probably busy in ttm_bo_cleanup_memtype_use.
323 * Even if it's not the case, because we finished waiting any
324 * delayed destruction would succeed, so just return success
327 spin_unlock(&bo->bdev->lru_lock);
333 if (ret || unlikely(list_empty(&bo->ddestroy))) {
335 dma_resv_unlock(bo->base.resv);
336 spin_unlock(&bo->bdev->lru_lock);
340 ttm_bo_del_from_lru(bo);
341 list_del_init(&bo->ddestroy);
342 spin_unlock(&bo->bdev->lru_lock);
343 ttm_bo_cleanup_memtype_use(bo);
346 dma_resv_unlock(bo->base.resv);
354 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
355 * encountered buffers.
357 bool ttm_bo_delayed_delete(struct ttm_device *bdev, bool remove_all)
359 struct list_head removed;
362 INIT_LIST_HEAD(&removed);
364 spin_lock(&bdev->lru_lock);
365 while (!list_empty(&bdev->ddestroy)) {
366 struct ttm_buffer_object *bo;
368 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
370 list_move_tail(&bo->ddestroy, &removed);
371 if (!ttm_bo_get_unless_zero(bo))
374 if (remove_all || bo->base.resv != &bo->base._resv) {
375 spin_unlock(&bdev->lru_lock);
376 dma_resv_lock(bo->base.resv, NULL);
378 spin_lock(&bdev->lru_lock);
379 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
381 } else if (dma_resv_trylock(bo->base.resv)) {
382 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
384 spin_unlock(&bdev->lru_lock);
388 spin_lock(&bdev->lru_lock);
390 list_splice_tail(&removed, &bdev->ddestroy);
391 empty = list_empty(&bdev->ddestroy);
392 spin_unlock(&bdev->lru_lock);
397 static void ttm_bo_release(struct kref *kref)
399 struct ttm_buffer_object *bo =
400 container_of(kref, struct ttm_buffer_object, kref);
401 struct ttm_device *bdev = bo->bdev;
405 ret = ttm_bo_individualize_resv(bo);
407 /* Last resort, if we fail to allocate memory for the
408 * fences block for the BO to become idle
410 dma_resv_wait_timeout_rcu(bo->base.resv, true, false,
414 if (bo->bdev->funcs->release_notify)
415 bo->bdev->funcs->release_notify(bo);
417 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
418 ttm_mem_io_free(bdev, &bo->mem);
421 if (!dma_resv_test_signaled_rcu(bo->base.resv, true) ||
422 !dma_resv_trylock(bo->base.resv)) {
423 /* The BO is not idle, resurrect it for delayed destroy */
424 ttm_bo_flush_all_fences(bo);
427 spin_lock(&bo->bdev->lru_lock);
430 * Make pinned bos immediately available to
431 * shrinkers, now that they are queued for
434 * FIXME: QXL is triggering this. Can be removed when the
437 if (WARN_ON_ONCE(bo->pin_count)) {
439 ttm_bo_move_to_lru_tail(bo, &bo->mem, NULL);
442 kref_init(&bo->kref);
443 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
444 spin_unlock(&bo->bdev->lru_lock);
446 schedule_delayed_work(&bdev->wq,
447 ((HZ / 100) < 1) ? 1 : HZ / 100);
451 spin_lock(&bo->bdev->lru_lock);
452 ttm_bo_del_from_lru(bo);
453 list_del(&bo->ddestroy);
454 spin_unlock(&bo->bdev->lru_lock);
456 ttm_bo_cleanup_memtype_use(bo);
457 dma_resv_unlock(bo->base.resv);
459 atomic_dec(&ttm_glob.bo_count);
460 dma_fence_put(bo->moving);
461 if (!ttm_bo_uses_embedded_gem_object(bo))
462 dma_resv_fini(&bo->base._resv);
466 void ttm_bo_put(struct ttm_buffer_object *bo)
468 kref_put(&bo->kref, ttm_bo_release);
470 EXPORT_SYMBOL(ttm_bo_put);
472 int ttm_bo_lock_delayed_workqueue(struct ttm_device *bdev)
474 return cancel_delayed_work_sync(&bdev->wq);
476 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
478 void ttm_bo_unlock_delayed_workqueue(struct ttm_device *bdev, int resched)
481 schedule_delayed_work(&bdev->wq,
482 ((HZ / 100) < 1) ? 1 : HZ / 100);
484 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
486 static int ttm_bo_evict(struct ttm_buffer_object *bo,
487 struct ttm_operation_ctx *ctx)
489 struct ttm_device *bdev = bo->bdev;
490 struct ttm_resource evict_mem;
491 struct ttm_placement placement;
492 struct ttm_place hop;
495 memset(&hop, 0, sizeof(hop));
497 dma_resv_assert_held(bo->base.resv);
499 placement.num_placement = 0;
500 placement.num_busy_placement = 0;
501 bdev->funcs->evict_flags(bo, &placement);
503 if (!placement.num_placement && !placement.num_busy_placement) {
504 ttm_bo_wait(bo, false, false);
506 ttm_bo_cleanup_memtype_use(bo);
507 return ttm_tt_create(bo, false);
511 evict_mem.mm_node = NULL;
512 evict_mem.bus.offset = 0;
513 evict_mem.bus.addr = NULL;
515 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
517 if (ret != -ERESTARTSYS) {
518 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
520 ttm_bo_mem_space_debug(bo, &placement);
525 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx, &hop);
527 WARN(ret == -EMULTIHOP, "Unexpected multihop in eviction - likely driver bug\n");
528 if (ret != -ERESTARTSYS)
529 pr_err("Buffer eviction failed\n");
530 ttm_resource_free(bo, &evict_mem);
536 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
537 const struct ttm_place *place)
539 /* Don't evict this BO if it's outside of the
540 * requested placement range
542 if (place->fpfn >= (bo->mem.start + bo->mem.num_pages) ||
543 (place->lpfn && place->lpfn <= bo->mem.start))
548 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
551 * Check the target bo is allowable to be evicted or swapout, including cases:
553 * a. if share same reservation object with ctx->resv, have assumption
554 * reservation objects should already be locked, so not lock again and
555 * return true directly when either the opreation allow_reserved_eviction
556 * or the target bo already is in delayed free list;
558 * b. Otherwise, trylock it.
560 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
561 struct ttm_operation_ctx *ctx, bool *locked, bool *busy)
565 if (bo->base.resv == ctx->resv) {
566 dma_resv_assert_held(bo->base.resv);
567 if (ctx->allow_res_evict)
573 ret = dma_resv_trylock(bo->base.resv);
583 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
585 * @busy_bo: BO which couldn't be locked with trylock
586 * @ctx: operation context
587 * @ticket: acquire ticket
589 * Try to lock a busy buffer object to avoid failing eviction.
591 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
592 struct ttm_operation_ctx *ctx,
593 struct ww_acquire_ctx *ticket)
597 if (!busy_bo || !ticket)
600 if (ctx->interruptible)
601 r = dma_resv_lock_interruptible(busy_bo->base.resv,
604 r = dma_resv_lock(busy_bo->base.resv, ticket);
607 * TODO: It would be better to keep the BO locked until allocation is at
608 * least tried one more time, but that would mean a much larger rework
612 dma_resv_unlock(busy_bo->base.resv);
614 return r == -EDEADLK ? -EBUSY : r;
617 int ttm_mem_evict_first(struct ttm_device *bdev,
618 struct ttm_resource_manager *man,
619 const struct ttm_place *place,
620 struct ttm_operation_ctx *ctx,
621 struct ww_acquire_ctx *ticket)
623 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
628 spin_lock(&bdev->lru_lock);
629 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
630 list_for_each_entry(bo, &man->lru[i], lru) {
633 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
635 if (busy && !busy_bo && ticket !=
636 dma_resv_locking_ctx(bo->base.resv))
641 if (place && !bdev->funcs->eviction_valuable(bo,
644 dma_resv_unlock(bo->base.resv);
647 if (!ttm_bo_get_unless_zero(bo)) {
649 dma_resv_unlock(bo->base.resv);
655 /* If the inner loop terminated early, we have our candidate */
656 if (&bo->lru != &man->lru[i])
663 if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
665 spin_unlock(&bdev->lru_lock);
666 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
673 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
674 ctx->no_wait_gpu, locked);
679 spin_unlock(&bdev->lru_lock);
681 ret = ttm_bo_evict(bo, ctx);
683 ttm_bo_unreserve(bo);
690 * Add the last move fence to the BO and reserve a new shared slot.
692 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
693 struct ttm_resource_manager *man,
694 struct ttm_resource *mem,
697 struct dma_fence *fence;
700 spin_lock(&man->move_lock);
701 fence = dma_fence_get(man->move);
702 spin_unlock(&man->move_lock);
708 ret = dma_fence_is_signaled(fence) ? 0 : -EBUSY;
709 dma_fence_put(fence);
713 dma_resv_add_shared_fence(bo->base.resv, fence);
715 ret = dma_resv_reserve_shared(bo->base.resv, 1);
717 dma_fence_put(fence);
721 dma_fence_put(bo->moving);
727 * Repeatedly evict memory from the LRU for @mem_type until we create enough
728 * space, or we've evicted everything and there isn't enough space.
730 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
731 const struct ttm_place *place,
732 struct ttm_resource *mem,
733 struct ttm_operation_ctx *ctx)
735 struct ttm_device *bdev = bo->bdev;
736 struct ttm_resource_manager *man = ttm_manager_type(bdev, mem->mem_type);
737 struct ww_acquire_ctx *ticket;
740 ticket = dma_resv_locking_ctx(bo->base.resv);
742 ret = ttm_resource_alloc(bo, place, mem);
745 if (unlikely(ret != -ENOSPC))
747 ret = ttm_mem_evict_first(bdev, man, place, ctx,
749 if (unlikely(ret != 0))
753 return ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
757 * ttm_bo_mem_placement - check if placement is compatible
758 * @bo: BO to find memory for
759 * @place: where to search
760 * @mem: the memory object to fill in
762 * Check if placement is compatible and fill in mem structure.
763 * Returns -EBUSY if placement won't work or negative error code.
764 * 0 when placement can be used.
766 static int ttm_bo_mem_placement(struct ttm_buffer_object *bo,
767 const struct ttm_place *place,
768 struct ttm_resource *mem)
770 struct ttm_device *bdev = bo->bdev;
771 struct ttm_resource_manager *man;
773 man = ttm_manager_type(bdev, place->mem_type);
774 if (!man || !ttm_resource_manager_used(man))
777 mem->mem_type = place->mem_type;
778 mem->placement = place->flags;
780 spin_lock(&bo->bdev->lru_lock);
781 ttm_bo_move_to_lru_tail(bo, mem, NULL);
782 spin_unlock(&bo->bdev->lru_lock);
787 * Creates space for memory region @mem according to its type.
789 * This function first searches for free space in compatible memory types in
790 * the priority order defined by the driver. If free space isn't found, then
791 * ttm_bo_mem_force_space is attempted in priority order to evict and find
794 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
795 struct ttm_placement *placement,
796 struct ttm_resource *mem,
797 struct ttm_operation_ctx *ctx)
799 struct ttm_device *bdev = bo->bdev;
800 bool type_found = false;
803 ret = dma_resv_reserve_shared(bo->base.resv, 1);
807 for (i = 0; i < placement->num_placement; ++i) {
808 const struct ttm_place *place = &placement->placement[i];
809 struct ttm_resource_manager *man;
811 ret = ttm_bo_mem_placement(bo, place, mem);
816 ret = ttm_resource_alloc(bo, place, mem);
822 man = ttm_manager_type(bdev, mem->mem_type);
823 ret = ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
825 ttm_resource_free(bo, mem);
834 for (i = 0; i < placement->num_busy_placement; ++i) {
835 const struct ttm_place *place = &placement->busy_placement[i];
837 ret = ttm_bo_mem_placement(bo, place, mem);
842 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
846 if (ret && ret != -EBUSY)
852 pr_err(TTM_PFX "No compatible memory type found\n");
857 if (bo->mem.mem_type == TTM_PL_SYSTEM && !bo->pin_count)
858 ttm_bo_move_to_lru_tail_unlocked(bo);
862 EXPORT_SYMBOL(ttm_bo_mem_space);
864 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
865 struct ttm_resource *mem,
866 struct ttm_operation_ctx *ctx,
867 struct ttm_place *hop)
869 struct ttm_placement hop_placement;
871 struct ttm_resource hop_mem = *mem;
873 hop_mem.mm_node = NULL;
874 hop_mem.mem_type = TTM_PL_SYSTEM;
875 hop_mem.placement = 0;
877 hop_placement.num_placement = hop_placement.num_busy_placement = 1;
878 hop_placement.placement = hop_placement.busy_placement = hop;
880 /* find space in the bounce domain */
881 ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
884 /* move to the bounce domain */
885 ret = ttm_bo_handle_move_mem(bo, &hop_mem, false, ctx, NULL);
887 ttm_resource_free(bo, &hop_mem);
893 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
894 struct ttm_placement *placement,
895 struct ttm_operation_ctx *ctx)
898 struct ttm_place hop;
899 struct ttm_resource mem;
901 dma_resv_assert_held(bo->base.resv);
903 memset(&hop, 0, sizeof(hop));
905 mem.num_pages = PAGE_ALIGN(bo->base.size) >> PAGE_SHIFT;
906 mem.page_alignment = bo->mem.page_alignment;
912 * Determine where to move the buffer.
914 * If driver determines move is going to need
915 * an extra step then it will return -EMULTIHOP
916 * and the buffer will be moved to the temporary
917 * stop and the driver will be called to make
920 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
924 ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx, &hop);
925 if (ret == -EMULTIHOP) {
926 ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
929 /* try and move to final place now. */
934 ttm_resource_free(bo, &mem);
938 static bool ttm_bo_places_compat(const struct ttm_place *places,
939 unsigned num_placement,
940 struct ttm_resource *mem,
945 for (i = 0; i < num_placement; i++) {
946 const struct ttm_place *heap = &places[i];
948 if ((mem->start < heap->fpfn ||
949 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
952 *new_flags = heap->flags;
953 if ((mem->mem_type == heap->mem_type) &&
954 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
955 (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
961 bool ttm_bo_mem_compat(struct ttm_placement *placement,
962 struct ttm_resource *mem,
965 if (ttm_bo_places_compat(placement->placement, placement->num_placement,
969 if ((placement->busy_placement != placement->placement ||
970 placement->num_busy_placement > placement->num_placement) &&
971 ttm_bo_places_compat(placement->busy_placement,
972 placement->num_busy_placement,
978 EXPORT_SYMBOL(ttm_bo_mem_compat);
980 int ttm_bo_validate(struct ttm_buffer_object *bo,
981 struct ttm_placement *placement,
982 struct ttm_operation_ctx *ctx)
987 dma_resv_assert_held(bo->base.resv);
990 * Remove the backing store if no placement is given.
992 if (!placement->num_placement && !placement->num_busy_placement) {
993 ret = ttm_bo_pipeline_gutting(bo);
997 return ttm_tt_create(bo, false);
1001 * Check whether we need to move buffer.
1003 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1004 ret = ttm_bo_move_buffer(bo, placement, ctx);
1009 * We might need to add a TTM.
1011 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
1012 ret = ttm_tt_create(bo, true);
1018 EXPORT_SYMBOL(ttm_bo_validate);
1020 int ttm_bo_init_reserved(struct ttm_device *bdev,
1021 struct ttm_buffer_object *bo,
1023 enum ttm_bo_type type,
1024 struct ttm_placement *placement,
1025 uint32_t page_alignment,
1026 struct ttm_operation_ctx *ctx,
1027 struct sg_table *sg,
1028 struct dma_resv *resv,
1029 void (*destroy) (struct ttm_buffer_object *))
1034 bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1036 kref_init(&bo->kref);
1037 INIT_LIST_HEAD(&bo->lru);
1038 INIT_LIST_HEAD(&bo->ddestroy);
1041 bo->mem.mem_type = TTM_PL_SYSTEM;
1042 bo->mem.num_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1043 bo->mem.mm_node = NULL;
1044 bo->mem.page_alignment = page_alignment;
1045 bo->mem.bus.offset = 0;
1046 bo->mem.bus.addr = NULL;
1048 bo->mem.placement = 0;
1052 bo->base.resv = resv;
1053 dma_resv_assert_held(bo->base.resv);
1055 bo->base.resv = &bo->base._resv;
1057 if (!ttm_bo_uses_embedded_gem_object(bo)) {
1059 * bo.base is not initialized, so we have to setup the
1060 * struct elements we want use regardless.
1062 bo->base.size = size;
1063 dma_resv_init(&bo->base._resv);
1064 drm_vma_node_reset(&bo->base.vma_node);
1066 atomic_inc(&ttm_glob.bo_count);
1069 * For ttm_bo_type_device buffers, allocate
1070 * address space from the device.
1072 if (bo->type == ttm_bo_type_device ||
1073 bo->type == ttm_bo_type_sg)
1074 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
1077 /* passed reservation objects should already be locked,
1078 * since otherwise lockdep will be angered in radeon.
1081 locked = dma_resv_trylock(bo->base.resv);
1086 ret = ttm_bo_validate(bo, placement, ctx);
1088 if (unlikely(ret)) {
1090 ttm_bo_unreserve(bo);
1096 ttm_bo_move_to_lru_tail_unlocked(bo);
1100 EXPORT_SYMBOL(ttm_bo_init_reserved);
1102 int ttm_bo_init(struct ttm_device *bdev,
1103 struct ttm_buffer_object *bo,
1105 enum ttm_bo_type type,
1106 struct ttm_placement *placement,
1107 uint32_t page_alignment,
1109 struct sg_table *sg,
1110 struct dma_resv *resv,
1111 void (*destroy) (struct ttm_buffer_object *))
1113 struct ttm_operation_ctx ctx = { interruptible, false };
1116 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1117 page_alignment, &ctx, sg, resv, destroy);
1122 ttm_bo_unreserve(bo);
1126 EXPORT_SYMBOL(ttm_bo_init);
1129 * buffer object vm functions.
1132 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1134 struct ttm_device *bdev = bo->bdev;
1136 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1137 ttm_mem_io_free(bdev, &bo->mem);
1139 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1141 int ttm_bo_wait(struct ttm_buffer_object *bo,
1142 bool interruptible, bool no_wait)
1144 long timeout = 15 * HZ;
1147 if (dma_resv_test_signaled_rcu(bo->base.resv, true))
1153 timeout = dma_resv_wait_timeout_rcu(bo->base.resv, true,
1154 interruptible, timeout);
1161 dma_resv_add_excl_fence(bo->base.resv, NULL);
1164 EXPORT_SYMBOL(ttm_bo_wait);
1166 int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
1172 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked, NULL))
1175 if (!ttm_bo_get_unless_zero(bo)) {
1177 dma_resv_unlock(bo->base.resv);
1182 ttm_bo_cleanup_refs(bo, false, false, locked);
1187 ttm_bo_del_from_lru(bo);
1188 /* TODO: Cleanup the locking */
1189 spin_unlock(&bo->bdev->lru_lock);
1192 * Move to system cached
1194 if (bo->mem.mem_type != TTM_PL_SYSTEM) {
1195 struct ttm_operation_ctx ctx = { false, false };
1196 struct ttm_resource evict_mem;
1197 struct ttm_place hop;
1199 memset(&hop, 0, sizeof(hop));
1201 evict_mem = bo->mem;
1202 evict_mem.mm_node = NULL;
1203 evict_mem.placement = 0;
1204 evict_mem.mem_type = TTM_PL_SYSTEM;
1206 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx, &hop);
1207 if (unlikely(ret != 0)) {
1208 WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
1214 * Make sure BO is idle.
1216 ret = ttm_bo_wait(bo, false, false);
1217 if (unlikely(ret != 0))
1220 ttm_bo_unmap_virtual(bo);
1223 * Swap out. Buffer will be swapped in again as soon as
1224 * anyone tries to access a ttm page.
1226 if (bo->bdev->funcs->swap_notify)
1227 bo->bdev->funcs->swap_notify(bo);
1229 ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);
1233 * Unreserve without putting on LRU to avoid swapping out an
1234 * already swapped buffer.
1237 dma_resv_unlock(bo->base.resv);
1242 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1244 if (bo->ttm == NULL)
1247 ttm_tt_destroy(bo->bdev, bo->ttm);