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->resource->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->resource->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_resource_manager *old_man, *new_man;
167 struct ttm_device *bdev = bo->bdev;
170 old_man = ttm_manager_type(bdev, bo->resource->mem_type);
171 new_man = ttm_manager_type(bdev, mem->mem_type);
173 ttm_bo_unmap_virtual(bo);
176 * Create and bind a ttm if required.
179 if (new_man->use_tt) {
180 /* Zero init the new TTM structure if the old location should
181 * have used one as well.
183 ret = ttm_tt_create(bo, old_man->use_tt);
187 if (mem->mem_type != TTM_PL_SYSTEM) {
188 ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
194 ret = bdev->funcs->move(bo, evict, ctx, mem, hop);
196 if (ret == -EMULTIHOP)
201 ctx->bytes_moved += bo->base.size;
205 new_man = ttm_manager_type(bdev, bo->resource->mem_type);
206 if (!new_man->use_tt)
207 ttm_bo_tt_destroy(bo);
214 * Will release GPU memory type usage on destruction.
215 * This is the place to put in driver specific hooks to release
216 * driver private resources.
217 * Will release the bo::reserved lock.
220 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
222 if (bo->bdev->funcs->delete_mem_notify)
223 bo->bdev->funcs->delete_mem_notify(bo);
225 ttm_bo_tt_destroy(bo);
226 ttm_resource_free(bo, &bo->resource);
229 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
233 if (bo->base.resv == &bo->base._resv)
236 BUG_ON(!dma_resv_trylock(&bo->base._resv));
238 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
239 dma_resv_unlock(&bo->base._resv);
243 if (bo->type != ttm_bo_type_sg) {
244 /* This works because the BO is about to be destroyed and nobody
245 * reference it any more. The only tricky case is the trylock on
246 * the resv object while holding the lru_lock.
248 spin_lock(&bo->bdev->lru_lock);
249 bo->base.resv = &bo->base._resv;
250 spin_unlock(&bo->bdev->lru_lock);
256 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
258 struct dma_resv *resv = &bo->base._resv;
259 struct dma_resv_list *fobj;
260 struct dma_fence *fence;
264 fobj = dma_resv_shared_list(resv);
265 fence = dma_resv_excl_fence(resv);
266 if (fence && !fence->ops->signaled)
267 dma_fence_enable_sw_signaling(fence);
269 for (i = 0; fobj && i < fobj->shared_count; ++i) {
270 fence = rcu_dereference(fobj->shared[i]);
272 if (!fence->ops->signaled)
273 dma_fence_enable_sw_signaling(fence);
279 * ttm_bo_cleanup_refs
280 * If bo idle, remove from lru lists, and unref.
281 * If not idle, block if possible.
283 * Must be called with lru_lock and reservation held, this function
284 * will drop the lru lock and optionally the reservation lock before returning.
286 * @bo: The buffer object to clean-up
287 * @interruptible: Any sleeps should occur interruptibly.
288 * @no_wait_gpu: Never wait for gpu. Return -EBUSY instead.
289 * @unlock_resv: Unlock the reservation lock as well.
292 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
293 bool interruptible, bool no_wait_gpu,
296 struct dma_resv *resv = &bo->base._resv;
299 if (dma_resv_test_signaled(resv, true))
304 if (ret && !no_wait_gpu) {
308 dma_resv_unlock(bo->base.resv);
309 spin_unlock(&bo->bdev->lru_lock);
311 lret = dma_resv_wait_timeout(resv, true, interruptible,
319 spin_lock(&bo->bdev->lru_lock);
320 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
322 * We raced, and lost, someone else holds the reservation now,
323 * and is probably busy in ttm_bo_cleanup_memtype_use.
325 * Even if it's not the case, because we finished waiting any
326 * delayed destruction would succeed, so just return success
329 spin_unlock(&bo->bdev->lru_lock);
335 if (ret || unlikely(list_empty(&bo->ddestroy))) {
337 dma_resv_unlock(bo->base.resv);
338 spin_unlock(&bo->bdev->lru_lock);
342 ttm_bo_del_from_lru(bo);
343 list_del_init(&bo->ddestroy);
344 spin_unlock(&bo->bdev->lru_lock);
345 ttm_bo_cleanup_memtype_use(bo);
348 dma_resv_unlock(bo->base.resv);
356 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
357 * encountered buffers.
359 bool ttm_bo_delayed_delete(struct ttm_device *bdev, bool remove_all)
361 struct list_head removed;
364 INIT_LIST_HEAD(&removed);
366 spin_lock(&bdev->lru_lock);
367 while (!list_empty(&bdev->ddestroy)) {
368 struct ttm_buffer_object *bo;
370 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
372 list_move_tail(&bo->ddestroy, &removed);
373 if (!ttm_bo_get_unless_zero(bo))
376 if (remove_all || bo->base.resv != &bo->base._resv) {
377 spin_unlock(&bdev->lru_lock);
378 dma_resv_lock(bo->base.resv, NULL);
380 spin_lock(&bdev->lru_lock);
381 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
383 } else if (dma_resv_trylock(bo->base.resv)) {
384 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
386 spin_unlock(&bdev->lru_lock);
390 spin_lock(&bdev->lru_lock);
392 list_splice_tail(&removed, &bdev->ddestroy);
393 empty = list_empty(&bdev->ddestroy);
394 spin_unlock(&bdev->lru_lock);
399 static void ttm_bo_release(struct kref *kref)
401 struct ttm_buffer_object *bo =
402 container_of(kref, struct ttm_buffer_object, kref);
403 struct ttm_device *bdev = bo->bdev;
406 WARN_ON_ONCE(bo->pin_count);
409 ret = ttm_bo_individualize_resv(bo);
411 /* Last resort, if we fail to allocate memory for the
412 * fences block for the BO to become idle
414 dma_resv_wait_timeout(bo->base.resv, true, false,
418 if (bo->bdev->funcs->release_notify)
419 bo->bdev->funcs->release_notify(bo);
421 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
422 ttm_mem_io_free(bdev, bo->resource);
425 if (!dma_resv_test_signaled(bo->base.resv, true) ||
426 !dma_resv_trylock(bo->base.resv)) {
427 /* The BO is not idle, resurrect it for delayed destroy */
428 ttm_bo_flush_all_fences(bo);
431 spin_lock(&bo->bdev->lru_lock);
434 * Make pinned bos immediately available to
435 * shrinkers, now that they are queued for
438 * FIXME: QXL is triggering this. Can be removed when the
443 ttm_bo_move_to_lru_tail(bo, bo->resource, NULL);
446 kref_init(&bo->kref);
447 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
448 spin_unlock(&bo->bdev->lru_lock);
450 schedule_delayed_work(&bdev->wq,
451 ((HZ / 100) < 1) ? 1 : HZ / 100);
455 spin_lock(&bo->bdev->lru_lock);
456 ttm_bo_del_from_lru(bo);
457 list_del(&bo->ddestroy);
458 spin_unlock(&bo->bdev->lru_lock);
460 ttm_bo_cleanup_memtype_use(bo);
461 dma_resv_unlock(bo->base.resv);
463 atomic_dec(&ttm_glob.bo_count);
464 dma_fence_put(bo->moving);
468 void ttm_bo_put(struct ttm_buffer_object *bo)
470 kref_put(&bo->kref, ttm_bo_release);
472 EXPORT_SYMBOL(ttm_bo_put);
474 int ttm_bo_lock_delayed_workqueue(struct ttm_device *bdev)
476 return cancel_delayed_work_sync(&bdev->wq);
478 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
480 void ttm_bo_unlock_delayed_workqueue(struct ttm_device *bdev, int resched)
483 schedule_delayed_work(&bdev->wq,
484 ((HZ / 100) < 1) ? 1 : HZ / 100);
486 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
488 static int ttm_bo_evict(struct ttm_buffer_object *bo,
489 struct ttm_operation_ctx *ctx)
491 struct ttm_device *bdev = bo->bdev;
492 struct ttm_resource *evict_mem;
493 struct ttm_placement placement;
494 struct ttm_place hop;
497 memset(&hop, 0, sizeof(hop));
499 dma_resv_assert_held(bo->base.resv);
501 placement.num_placement = 0;
502 placement.num_busy_placement = 0;
503 bdev->funcs->evict_flags(bo, &placement);
505 if (!placement.num_placement && !placement.num_busy_placement) {
506 ret = ttm_bo_wait(bo, true, false);
511 * Since we've already synced, this frees backing store
514 return ttm_bo_pipeline_gutting(bo);
517 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
519 if (ret != -ERESTARTSYS) {
520 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
522 ttm_bo_mem_space_debug(bo, &placement);
527 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
529 WARN(ret == -EMULTIHOP, "Unexpected multihop in eviction - likely driver bug\n");
530 if (ret != -ERESTARTSYS)
531 pr_err("Buffer eviction failed\n");
532 ttm_resource_free(bo, &evict_mem);
538 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
539 const struct ttm_place *place)
541 dma_resv_assert_held(bo->base.resv);
542 if (bo->resource->mem_type == TTM_PL_SYSTEM)
545 /* Don't evict this BO if it's outside of the
546 * requested placement range
548 if (place->fpfn >= (bo->resource->start + bo->resource->num_pages) ||
549 (place->lpfn && place->lpfn <= bo->resource->start))
554 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
557 * Check the target bo is allowable to be evicted or swapout, including cases:
559 * a. if share same reservation object with ctx->resv, have assumption
560 * reservation objects should already be locked, so not lock again and
561 * return true directly when either the opreation allow_reserved_eviction
562 * or the target bo already is in delayed free list;
564 * b. Otherwise, trylock it.
566 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
567 struct ttm_operation_ctx *ctx,
568 const struct ttm_place *place,
569 bool *locked, bool *busy)
573 if (bo->base.resv == ctx->resv) {
574 dma_resv_assert_held(bo->base.resv);
575 if (ctx->allow_res_evict)
581 ret = dma_resv_trylock(bo->base.resv);
587 if (ret && place && !bo->bdev->funcs->eviction_valuable(bo, place)) {
590 dma_resv_unlock(bo->base.resv);
599 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
601 * @busy_bo: BO which couldn't be locked with trylock
602 * @ctx: operation context
603 * @ticket: acquire ticket
605 * Try to lock a busy buffer object to avoid failing eviction.
607 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
608 struct ttm_operation_ctx *ctx,
609 struct ww_acquire_ctx *ticket)
613 if (!busy_bo || !ticket)
616 if (ctx->interruptible)
617 r = dma_resv_lock_interruptible(busy_bo->base.resv,
620 r = dma_resv_lock(busy_bo->base.resv, ticket);
623 * TODO: It would be better to keep the BO locked until allocation is at
624 * least tried one more time, but that would mean a much larger rework
628 dma_resv_unlock(busy_bo->base.resv);
630 return r == -EDEADLK ? -EBUSY : r;
633 int ttm_mem_evict_first(struct ttm_device *bdev,
634 struct ttm_resource_manager *man,
635 const struct ttm_place *place,
636 struct ttm_operation_ctx *ctx,
637 struct ww_acquire_ctx *ticket)
639 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
644 spin_lock(&bdev->lru_lock);
645 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
646 list_for_each_entry(bo, &man->lru[i], lru) {
649 if (!ttm_bo_evict_swapout_allowable(bo, ctx, place,
651 if (busy && !busy_bo && ticket !=
652 dma_resv_locking_ctx(bo->base.resv))
657 if (!ttm_bo_get_unless_zero(bo)) {
659 dma_resv_unlock(bo->base.resv);
665 /* If the inner loop terminated early, we have our candidate */
666 if (&bo->lru != &man->lru[i])
673 if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
675 spin_unlock(&bdev->lru_lock);
676 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
683 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
684 ctx->no_wait_gpu, locked);
689 spin_unlock(&bdev->lru_lock);
691 ret = ttm_bo_evict(bo, ctx);
693 ttm_bo_unreserve(bo);
700 * Add the last move fence to the BO and reserve a new shared slot. We only use
701 * a shared slot to avoid unecessary sync and rely on the subsequent bo move to
702 * either stall or use an exclusive fence respectively set bo->moving.
704 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
705 struct ttm_resource_manager *man,
706 struct ttm_resource *mem,
709 struct dma_fence *fence;
712 spin_lock(&man->move_lock);
713 fence = dma_fence_get(man->move);
714 spin_unlock(&man->move_lock);
720 ret = dma_fence_is_signaled(fence) ? 0 : -EBUSY;
721 dma_fence_put(fence);
725 dma_resv_add_shared_fence(bo->base.resv, fence);
727 ret = dma_resv_reserve_shared(bo->base.resv, 1);
729 dma_fence_put(fence);
733 dma_fence_put(bo->moving);
739 * Repeatedly evict memory from the LRU for @mem_type until we create enough
740 * space, or we've evicted everything and there isn't enough space.
742 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
743 const struct ttm_place *place,
744 struct ttm_resource **mem,
745 struct ttm_operation_ctx *ctx)
747 struct ttm_device *bdev = bo->bdev;
748 struct ttm_resource_manager *man;
749 struct ww_acquire_ctx *ticket;
752 man = ttm_manager_type(bdev, place->mem_type);
753 ticket = dma_resv_locking_ctx(bo->base.resv);
755 ret = ttm_resource_alloc(bo, place, mem);
758 if (unlikely(ret != -ENOSPC))
760 ret = ttm_mem_evict_first(bdev, man, place, ctx,
762 if (unlikely(ret != 0))
766 return ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
770 * Creates space for memory region @mem according to its type.
772 * This function first searches for free space in compatible memory types in
773 * the priority order defined by the driver. If free space isn't found, then
774 * ttm_bo_mem_force_space is attempted in priority order to evict and find
777 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
778 struct ttm_placement *placement,
779 struct ttm_resource **mem,
780 struct ttm_operation_ctx *ctx)
782 struct ttm_device *bdev = bo->bdev;
783 bool type_found = false;
786 ret = dma_resv_reserve_shared(bo->base.resv, 1);
790 for (i = 0; i < placement->num_placement; ++i) {
791 const struct ttm_place *place = &placement->placement[i];
792 struct ttm_resource_manager *man;
794 man = ttm_manager_type(bdev, place->mem_type);
795 if (!man || !ttm_resource_manager_used(man))
799 ret = ttm_resource_alloc(bo, place, mem);
805 ret = ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
807 ttm_resource_free(bo, mem);
816 for (i = 0; i < placement->num_busy_placement; ++i) {
817 const struct ttm_place *place = &placement->busy_placement[i];
818 struct ttm_resource_manager *man;
820 man = ttm_manager_type(bdev, place->mem_type);
821 if (!man || !ttm_resource_manager_used(man))
825 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
829 if (ret && ret != -EBUSY)
835 pr_err(TTM_PFX "No compatible memory type found\n");
840 if (bo->resource->mem_type == TTM_PL_SYSTEM && !bo->pin_count)
841 ttm_bo_move_to_lru_tail_unlocked(bo);
845 EXPORT_SYMBOL(ttm_bo_mem_space);
847 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
848 struct ttm_resource **mem,
849 struct ttm_operation_ctx *ctx,
850 struct ttm_place *hop)
852 struct ttm_placement hop_placement;
853 struct ttm_resource *hop_mem;
856 hop_placement.num_placement = hop_placement.num_busy_placement = 1;
857 hop_placement.placement = hop_placement.busy_placement = hop;
859 /* find space in the bounce domain */
860 ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
863 /* move to the bounce domain */
864 ret = ttm_bo_handle_move_mem(bo, hop_mem, false, ctx, NULL);
866 ttm_resource_free(bo, &hop_mem);
872 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
873 struct ttm_placement *placement,
874 struct ttm_operation_ctx *ctx)
876 struct ttm_resource *mem;
877 struct ttm_place hop;
880 dma_resv_assert_held(bo->base.resv);
883 * Determine where to move the buffer.
885 * If driver determines move is going to need
886 * an extra step then it will return -EMULTIHOP
887 * and the buffer will be moved to the temporary
888 * stop and the driver will be called to make
891 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
895 ret = ttm_bo_handle_move_mem(bo, mem, false, ctx, &hop);
896 if (ret == -EMULTIHOP) {
897 ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
900 /* try and move to final place now. */
905 ttm_resource_free(bo, &mem);
909 static bool ttm_bo_places_compat(const struct ttm_place *places,
910 unsigned num_placement,
911 struct ttm_resource *mem,
916 for (i = 0; i < num_placement; i++) {
917 const struct ttm_place *heap = &places[i];
919 if ((mem->start < heap->fpfn ||
920 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
923 *new_flags = heap->flags;
924 if ((mem->mem_type == heap->mem_type) &&
925 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
926 (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
932 bool ttm_bo_mem_compat(struct ttm_placement *placement,
933 struct ttm_resource *mem,
936 if (ttm_bo_places_compat(placement->placement, placement->num_placement,
940 if ((placement->busy_placement != placement->placement ||
941 placement->num_busy_placement > placement->num_placement) &&
942 ttm_bo_places_compat(placement->busy_placement,
943 placement->num_busy_placement,
949 EXPORT_SYMBOL(ttm_bo_mem_compat);
951 int ttm_bo_validate(struct ttm_buffer_object *bo,
952 struct ttm_placement *placement,
953 struct ttm_operation_ctx *ctx)
958 dma_resv_assert_held(bo->base.resv);
961 * Remove the backing store if no placement is given.
963 if (!placement->num_placement && !placement->num_busy_placement)
964 return ttm_bo_pipeline_gutting(bo);
967 * Check whether we need to move buffer.
969 if (!ttm_bo_mem_compat(placement, bo->resource, &new_flags)) {
970 ret = ttm_bo_move_buffer(bo, placement, ctx);
975 * We might need to add a TTM.
977 if (bo->resource->mem_type == TTM_PL_SYSTEM) {
978 ret = ttm_tt_create(bo, true);
984 EXPORT_SYMBOL(ttm_bo_validate);
986 int ttm_bo_init_reserved(struct ttm_device *bdev,
987 struct ttm_buffer_object *bo,
989 enum ttm_bo_type type,
990 struct ttm_placement *placement,
991 uint32_t page_alignment,
992 struct ttm_operation_ctx *ctx,
994 struct dma_resv *resv,
995 void (*destroy) (struct ttm_buffer_object *))
997 static const struct ttm_place sys_mem = { .mem_type = TTM_PL_SYSTEM };
1001 bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1003 kref_init(&bo->kref);
1004 INIT_LIST_HEAD(&bo->lru);
1005 INIT_LIST_HEAD(&bo->ddestroy);
1008 bo->page_alignment = page_alignment;
1013 bo->base.resv = resv;
1014 dma_resv_assert_held(bo->base.resv);
1016 bo->base.resv = &bo->base._resv;
1018 atomic_inc(&ttm_glob.bo_count);
1020 ret = ttm_resource_alloc(bo, &sys_mem, &bo->resource);
1021 if (unlikely(ret)) {
1027 * For ttm_bo_type_device buffers, allocate
1028 * address space from the device.
1030 if (bo->type == ttm_bo_type_device ||
1031 bo->type == ttm_bo_type_sg)
1032 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
1033 bo->resource->num_pages);
1035 /* passed reservation objects should already be locked,
1036 * since otherwise lockdep will be angered in radeon.
1039 locked = dma_resv_trylock(bo->base.resv);
1044 ret = ttm_bo_validate(bo, placement, ctx);
1046 if (unlikely(ret)) {
1048 ttm_bo_unreserve(bo);
1054 ttm_bo_move_to_lru_tail_unlocked(bo);
1058 EXPORT_SYMBOL(ttm_bo_init_reserved);
1060 int ttm_bo_init(struct ttm_device *bdev,
1061 struct ttm_buffer_object *bo,
1063 enum ttm_bo_type type,
1064 struct ttm_placement *placement,
1065 uint32_t page_alignment,
1067 struct sg_table *sg,
1068 struct dma_resv *resv,
1069 void (*destroy) (struct ttm_buffer_object *))
1071 struct ttm_operation_ctx ctx = { interruptible, false };
1074 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1075 page_alignment, &ctx, sg, resv, destroy);
1080 ttm_bo_unreserve(bo);
1084 EXPORT_SYMBOL(ttm_bo_init);
1087 * buffer object vm functions.
1090 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1092 struct ttm_device *bdev = bo->bdev;
1094 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1095 ttm_mem_io_free(bdev, bo->resource);
1097 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1099 int ttm_bo_wait(struct ttm_buffer_object *bo,
1100 bool interruptible, bool no_wait)
1102 long timeout = 15 * HZ;
1105 if (dma_resv_test_signaled(bo->base.resv, true))
1111 timeout = dma_resv_wait_timeout(bo->base.resv, true, interruptible,
1119 dma_resv_add_excl_fence(bo->base.resv, NULL);
1122 EXPORT_SYMBOL(ttm_bo_wait);
1124 int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
1127 struct ttm_place place;
1132 * While the bo may already reside in SYSTEM placement, set
1133 * SYSTEM as new placement to cover also the move further below.
1134 * The driver may use the fact that we're moving from SYSTEM
1135 * as an indication that we're about to swap out.
1137 memset(&place, 0, sizeof(place));
1138 place.mem_type = TTM_PL_SYSTEM;
1139 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place, &locked, NULL))
1142 if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
1143 bo->ttm->page_flags & TTM_PAGE_FLAG_SG ||
1144 bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED ||
1145 !ttm_bo_get_unless_zero(bo)) {
1147 dma_resv_unlock(bo->base.resv);
1152 ttm_bo_cleanup_refs(bo, false, false, locked);
1157 ttm_bo_del_from_lru(bo);
1158 /* TODO: Cleanup the locking */
1159 spin_unlock(&bo->bdev->lru_lock);
1162 * Move to system cached
1164 if (bo->resource->mem_type != TTM_PL_SYSTEM) {
1165 struct ttm_operation_ctx ctx = { false, false };
1166 struct ttm_resource *evict_mem;
1167 struct ttm_place hop;
1169 memset(&hop, 0, sizeof(hop));
1170 ret = ttm_resource_alloc(bo, &place, &evict_mem);
1174 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, &ctx, &hop);
1175 if (unlikely(ret != 0)) {
1176 WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
1182 * Make sure BO is idle.
1184 ret = ttm_bo_wait(bo, false, false);
1185 if (unlikely(ret != 0))
1188 ttm_bo_unmap_virtual(bo);
1191 * Swap out. Buffer will be swapped in again as soon as
1192 * anyone tries to access a ttm page.
1194 if (bo->bdev->funcs->swap_notify)
1195 bo->bdev->funcs->swap_notify(bo);
1197 if (ttm_tt_is_populated(bo->ttm))
1198 ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);
1202 * Unreserve without putting on LRU to avoid swapping out an
1203 * already swapped buffer.
1206 dma_resv_unlock(bo->base.resv);
1211 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1213 if (bo->ttm == NULL)
1216 ttm_tt_destroy(bo->bdev, bo->ttm);