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);
108 man = ttm_manager_type(bdev, mem->mem_type);
109 list_move_tail(&bo->lru, &man->lru[bo->priority]);
111 if (bdev->funcs->del_from_lru_notify)
112 bdev->funcs->del_from_lru_notify(bo);
114 if (bulk && !bo->pin_count) {
115 switch (bo->resource->mem_type) {
117 ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
121 ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
126 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
128 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
132 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
133 struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
134 struct ttm_resource_manager *man;
139 dma_resv_assert_held(pos->first->base.resv);
140 dma_resv_assert_held(pos->last->base.resv);
142 man = ttm_manager_type(pos->first->bdev, TTM_PL_TT);
143 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
147 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
148 struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
149 struct ttm_resource_manager *man;
154 dma_resv_assert_held(pos->first->base.resv);
155 dma_resv_assert_held(pos->last->base.resv);
157 man = ttm_manager_type(pos->first->bdev, TTM_PL_VRAM);
158 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
162 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
164 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
165 struct ttm_resource *mem, bool evict,
166 struct ttm_operation_ctx *ctx,
167 struct ttm_place *hop)
169 struct ttm_resource_manager *old_man, *new_man;
170 struct ttm_device *bdev = bo->bdev;
173 old_man = ttm_manager_type(bdev, bo->resource->mem_type);
174 new_man = ttm_manager_type(bdev, mem->mem_type);
176 ttm_bo_unmap_virtual(bo);
179 * Create and bind a ttm if required.
182 if (new_man->use_tt) {
183 /* Zero init the new TTM structure if the old location should
184 * have used one as well.
186 ret = ttm_tt_create(bo, old_man->use_tt);
190 if (mem->mem_type != TTM_PL_SYSTEM) {
191 ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
197 ret = bdev->funcs->move(bo, evict, ctx, mem, hop);
199 if (ret == -EMULTIHOP)
204 ctx->bytes_moved += bo->base.size;
208 new_man = ttm_manager_type(bdev, bo->resource->mem_type);
209 if (!new_man->use_tt)
210 ttm_bo_tt_destroy(bo);
217 * Will release GPU memory type usage on destruction.
218 * This is the place to put in driver specific hooks to release
219 * driver private resources.
220 * Will release the bo::reserved lock.
223 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
225 if (bo->bdev->funcs->delete_mem_notify)
226 bo->bdev->funcs->delete_mem_notify(bo);
228 ttm_bo_tt_destroy(bo);
229 ttm_resource_free(bo, &bo->resource);
232 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
236 if (bo->base.resv == &bo->base._resv)
239 BUG_ON(!dma_resv_trylock(&bo->base._resv));
241 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
242 dma_resv_unlock(&bo->base._resv);
246 if (bo->type != ttm_bo_type_sg) {
247 /* This works because the BO is about to be destroyed and nobody
248 * reference it any more. The only tricky case is the trylock on
249 * the resv object while holding the lru_lock.
251 spin_lock(&bo->bdev->lru_lock);
252 bo->base.resv = &bo->base._resv;
253 spin_unlock(&bo->bdev->lru_lock);
259 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
261 struct dma_resv *resv = &bo->base._resv;
262 struct dma_resv_list *fobj;
263 struct dma_fence *fence;
267 fobj = dma_resv_shared_list(resv);
268 fence = dma_resv_excl_fence(resv);
269 if (fence && !fence->ops->signaled)
270 dma_fence_enable_sw_signaling(fence);
272 for (i = 0; fobj && i < fobj->shared_count; ++i) {
273 fence = rcu_dereference(fobj->shared[i]);
275 if (!fence->ops->signaled)
276 dma_fence_enable_sw_signaling(fence);
282 * ttm_bo_cleanup_refs
283 * If bo idle, remove from lru lists, and unref.
284 * If not idle, block if possible.
286 * Must be called with lru_lock and reservation held, this function
287 * will drop the lru lock and optionally the reservation lock before returning.
289 * @bo: The buffer object to clean-up
290 * @interruptible: Any sleeps should occur interruptibly.
291 * @no_wait_gpu: Never wait for gpu. Return -EBUSY instead.
292 * @unlock_resv: Unlock the reservation lock as well.
295 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
296 bool interruptible, bool no_wait_gpu,
299 struct dma_resv *resv = &bo->base._resv;
302 if (dma_resv_test_signaled(resv, true))
307 if (ret && !no_wait_gpu) {
311 dma_resv_unlock(bo->base.resv);
312 spin_unlock(&bo->bdev->lru_lock);
314 lret = dma_resv_wait_timeout(resv, true, interruptible,
322 spin_lock(&bo->bdev->lru_lock);
323 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
325 * We raced, and lost, someone else holds the reservation now,
326 * and is probably busy in ttm_bo_cleanup_memtype_use.
328 * Even if it's not the case, because we finished waiting any
329 * delayed destruction would succeed, so just return success
332 spin_unlock(&bo->bdev->lru_lock);
338 if (ret || unlikely(list_empty(&bo->ddestroy))) {
340 dma_resv_unlock(bo->base.resv);
341 spin_unlock(&bo->bdev->lru_lock);
345 ttm_bo_del_from_lru(bo);
346 list_del_init(&bo->ddestroy);
347 spin_unlock(&bo->bdev->lru_lock);
348 ttm_bo_cleanup_memtype_use(bo);
351 dma_resv_unlock(bo->base.resv);
359 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
360 * encountered buffers.
362 bool ttm_bo_delayed_delete(struct ttm_device *bdev, bool remove_all)
364 struct list_head removed;
367 INIT_LIST_HEAD(&removed);
369 spin_lock(&bdev->lru_lock);
370 while (!list_empty(&bdev->ddestroy)) {
371 struct ttm_buffer_object *bo;
373 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
375 list_move_tail(&bo->ddestroy, &removed);
376 if (!ttm_bo_get_unless_zero(bo))
379 if (remove_all || bo->base.resv != &bo->base._resv) {
380 spin_unlock(&bdev->lru_lock);
381 dma_resv_lock(bo->base.resv, NULL);
383 spin_lock(&bdev->lru_lock);
384 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
386 } else if (dma_resv_trylock(bo->base.resv)) {
387 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
389 spin_unlock(&bdev->lru_lock);
393 spin_lock(&bdev->lru_lock);
395 list_splice_tail(&removed, &bdev->ddestroy);
396 empty = list_empty(&bdev->ddestroy);
397 spin_unlock(&bdev->lru_lock);
402 static void ttm_bo_release(struct kref *kref)
404 struct ttm_buffer_object *bo =
405 container_of(kref, struct ttm_buffer_object, kref);
406 struct ttm_device *bdev = bo->bdev;
409 WARN_ON_ONCE(bo->pin_count);
412 ret = ttm_bo_individualize_resv(bo);
414 /* Last resort, if we fail to allocate memory for the
415 * fences block for the BO to become idle
417 dma_resv_wait_timeout(bo->base.resv, true, false,
421 if (bo->bdev->funcs->release_notify)
422 bo->bdev->funcs->release_notify(bo);
424 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
425 ttm_mem_io_free(bdev, bo->resource);
428 if (!dma_resv_test_signaled(bo->base.resv, true) ||
429 !dma_resv_trylock(bo->base.resv)) {
430 /* The BO is not idle, resurrect it for delayed destroy */
431 ttm_bo_flush_all_fences(bo);
434 spin_lock(&bo->bdev->lru_lock);
437 * Make pinned bos immediately available to
438 * shrinkers, now that they are queued for
441 * FIXME: QXL is triggering this. Can be removed when the
446 ttm_bo_move_to_lru_tail(bo, bo->resource, NULL);
449 kref_init(&bo->kref);
450 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
451 spin_unlock(&bo->bdev->lru_lock);
453 schedule_delayed_work(&bdev->wq,
454 ((HZ / 100) < 1) ? 1 : HZ / 100);
458 spin_lock(&bo->bdev->lru_lock);
459 ttm_bo_del_from_lru(bo);
460 list_del(&bo->ddestroy);
461 spin_unlock(&bo->bdev->lru_lock);
463 ttm_bo_cleanup_memtype_use(bo);
464 dma_resv_unlock(bo->base.resv);
466 atomic_dec(&ttm_glob.bo_count);
467 dma_fence_put(bo->moving);
471 void ttm_bo_put(struct ttm_buffer_object *bo)
473 kref_put(&bo->kref, ttm_bo_release);
475 EXPORT_SYMBOL(ttm_bo_put);
477 int ttm_bo_lock_delayed_workqueue(struct ttm_device *bdev)
479 return cancel_delayed_work_sync(&bdev->wq);
481 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
483 void ttm_bo_unlock_delayed_workqueue(struct ttm_device *bdev, int resched)
486 schedule_delayed_work(&bdev->wq,
487 ((HZ / 100) < 1) ? 1 : HZ / 100);
489 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
491 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
492 struct ttm_resource **mem,
493 struct ttm_operation_ctx *ctx,
494 struct ttm_place *hop)
496 struct ttm_placement hop_placement;
497 struct ttm_resource *hop_mem;
500 hop_placement.num_placement = hop_placement.num_busy_placement = 1;
501 hop_placement.placement = hop_placement.busy_placement = hop;
503 /* find space in the bounce domain */
504 ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
507 /* move to the bounce domain */
508 ret = ttm_bo_handle_move_mem(bo, hop_mem, false, ctx, NULL);
510 ttm_resource_free(bo, &hop_mem);
516 static int ttm_bo_evict(struct ttm_buffer_object *bo,
517 struct ttm_operation_ctx *ctx)
519 struct ttm_device *bdev = bo->bdev;
520 struct ttm_resource *evict_mem;
521 struct ttm_placement placement;
522 struct ttm_place hop;
525 memset(&hop, 0, sizeof(hop));
527 dma_resv_assert_held(bo->base.resv);
529 placement.num_placement = 0;
530 placement.num_busy_placement = 0;
531 bdev->funcs->evict_flags(bo, &placement);
533 if (!placement.num_placement && !placement.num_busy_placement) {
534 ret = ttm_bo_wait(bo, true, false);
539 * Since we've already synced, this frees backing store
542 return ttm_bo_pipeline_gutting(bo);
545 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
547 if (ret != -ERESTARTSYS) {
548 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
550 ttm_bo_mem_space_debug(bo, &placement);
556 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
557 if (ret == -EMULTIHOP) {
558 ret = ttm_bo_bounce_temp_buffer(bo, &evict_mem, ctx, &hop);
560 pr_err("Buffer eviction failed\n");
561 ttm_resource_free(bo, &evict_mem);
564 /* try and move to final place now. */
571 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
572 const struct ttm_place *place)
574 dma_resv_assert_held(bo->base.resv);
575 if (bo->resource->mem_type == TTM_PL_SYSTEM)
578 /* Don't evict this BO if it's outside of the
579 * requested placement range
581 if (place->fpfn >= (bo->resource->start + bo->resource->num_pages) ||
582 (place->lpfn && place->lpfn <= bo->resource->start))
587 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
590 * Check the target bo is allowable to be evicted or swapout, including cases:
592 * a. if share same reservation object with ctx->resv, have assumption
593 * reservation objects should already be locked, so not lock again and
594 * return true directly when either the opreation allow_reserved_eviction
595 * or the target bo already is in delayed free list;
597 * b. Otherwise, trylock it.
599 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
600 struct ttm_operation_ctx *ctx,
601 const struct ttm_place *place,
602 bool *locked, bool *busy)
606 if (bo->base.resv == ctx->resv) {
607 dma_resv_assert_held(bo->base.resv);
608 if (ctx->allow_res_evict)
614 ret = dma_resv_trylock(bo->base.resv);
620 if (ret && place && !bo->bdev->funcs->eviction_valuable(bo, place)) {
623 dma_resv_unlock(bo->base.resv);
632 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
634 * @busy_bo: BO which couldn't be locked with trylock
635 * @ctx: operation context
636 * @ticket: acquire ticket
638 * Try to lock a busy buffer object to avoid failing eviction.
640 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
641 struct ttm_operation_ctx *ctx,
642 struct ww_acquire_ctx *ticket)
646 if (!busy_bo || !ticket)
649 if (ctx->interruptible)
650 r = dma_resv_lock_interruptible(busy_bo->base.resv,
653 r = dma_resv_lock(busy_bo->base.resv, ticket);
656 * TODO: It would be better to keep the BO locked until allocation is at
657 * least tried one more time, but that would mean a much larger rework
661 dma_resv_unlock(busy_bo->base.resv);
663 return r == -EDEADLK ? -EBUSY : r;
666 int ttm_mem_evict_first(struct ttm_device *bdev,
667 struct ttm_resource_manager *man,
668 const struct ttm_place *place,
669 struct ttm_operation_ctx *ctx,
670 struct ww_acquire_ctx *ticket)
672 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
677 spin_lock(&bdev->lru_lock);
678 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
679 list_for_each_entry(bo, &man->lru[i], lru) {
682 if (!ttm_bo_evict_swapout_allowable(bo, ctx, place,
684 if (busy && !busy_bo && ticket !=
685 dma_resv_locking_ctx(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(&bdev->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(&bdev->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. We only use
734 * a shared slot to avoid unecessary sync and rely on the subsequent bo move to
735 * either stall or use an exclusive fence respectively set bo->moving.
737 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
738 struct ttm_resource_manager *man,
739 struct ttm_resource *mem,
742 struct dma_fence *fence;
745 spin_lock(&man->move_lock);
746 fence = dma_fence_get(man->move);
747 spin_unlock(&man->move_lock);
753 ret = dma_fence_is_signaled(fence) ? 0 : -EBUSY;
754 dma_fence_put(fence);
758 dma_resv_add_shared_fence(bo->base.resv, fence);
760 ret = dma_resv_reserve_shared(bo->base.resv, 1);
762 dma_fence_put(fence);
766 dma_fence_put(bo->moving);
772 * Repeatedly evict memory from the LRU for @mem_type until we create enough
773 * space, or we've evicted everything and there isn't enough space.
775 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
776 const struct ttm_place *place,
777 struct ttm_resource **mem,
778 struct ttm_operation_ctx *ctx)
780 struct ttm_device *bdev = bo->bdev;
781 struct ttm_resource_manager *man;
782 struct ww_acquire_ctx *ticket;
785 man = ttm_manager_type(bdev, place->mem_type);
786 ticket = dma_resv_locking_ctx(bo->base.resv);
788 ret = ttm_resource_alloc(bo, place, mem);
791 if (unlikely(ret != -ENOSPC))
793 ret = ttm_mem_evict_first(bdev, man, place, ctx,
795 if (unlikely(ret != 0))
799 return ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
803 * Creates space for memory region @mem according to its type.
805 * This function first searches for free space in compatible memory types in
806 * the priority order defined by the driver. If free space isn't found, then
807 * ttm_bo_mem_force_space is attempted in priority order to evict and find
810 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
811 struct ttm_placement *placement,
812 struct ttm_resource **mem,
813 struct ttm_operation_ctx *ctx)
815 struct ttm_device *bdev = bo->bdev;
816 bool type_found = false;
819 ret = dma_resv_reserve_shared(bo->base.resv, 1);
823 for (i = 0; i < placement->num_placement; ++i) {
824 const struct ttm_place *place = &placement->placement[i];
825 struct ttm_resource_manager *man;
827 man = ttm_manager_type(bdev, place->mem_type);
828 if (!man || !ttm_resource_manager_used(man))
832 ret = ttm_resource_alloc(bo, place, mem);
838 ret = ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
840 ttm_resource_free(bo, mem);
849 for (i = 0; i < placement->num_busy_placement; ++i) {
850 const struct ttm_place *place = &placement->busy_placement[i];
851 struct ttm_resource_manager *man;
853 man = ttm_manager_type(bdev, place->mem_type);
854 if (!man || !ttm_resource_manager_used(man))
858 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
862 if (ret && ret != -EBUSY)
868 pr_err(TTM_PFX "No compatible memory type found\n");
873 if (bo->resource->mem_type == TTM_PL_SYSTEM && !bo->pin_count)
874 ttm_bo_move_to_lru_tail_unlocked(bo);
878 EXPORT_SYMBOL(ttm_bo_mem_space);
880 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
881 struct ttm_placement *placement,
882 struct ttm_operation_ctx *ctx)
884 struct ttm_resource *mem;
885 struct ttm_place hop;
888 dma_resv_assert_held(bo->base.resv);
891 * Determine where to move the buffer.
893 * If driver determines move is going to need
894 * an extra step then it will return -EMULTIHOP
895 * and the buffer will be moved to the temporary
896 * stop and the driver will be called to make
899 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
903 ret = ttm_bo_handle_move_mem(bo, mem, false, ctx, &hop);
904 if (ret == -EMULTIHOP) {
905 ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
908 /* try and move to final place now. */
913 ttm_resource_free(bo, &mem);
917 static bool ttm_bo_places_compat(const struct ttm_place *places,
918 unsigned num_placement,
919 struct ttm_resource *mem,
924 if (mem->placement & TTM_PL_FLAG_TEMPORARY)
927 for (i = 0; i < num_placement; i++) {
928 const struct ttm_place *heap = &places[i];
930 if ((mem->start < heap->fpfn ||
931 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
934 *new_flags = heap->flags;
935 if ((mem->mem_type == heap->mem_type) &&
936 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
937 (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
943 bool ttm_bo_mem_compat(struct ttm_placement *placement,
944 struct ttm_resource *mem,
947 if (ttm_bo_places_compat(placement->placement, placement->num_placement,
951 if ((placement->busy_placement != placement->placement ||
952 placement->num_busy_placement > placement->num_placement) &&
953 ttm_bo_places_compat(placement->busy_placement,
954 placement->num_busy_placement,
960 EXPORT_SYMBOL(ttm_bo_mem_compat);
962 int ttm_bo_validate(struct ttm_buffer_object *bo,
963 struct ttm_placement *placement,
964 struct ttm_operation_ctx *ctx)
969 dma_resv_assert_held(bo->base.resv);
972 * Remove the backing store if no placement is given.
974 if (!placement->num_placement && !placement->num_busy_placement)
975 return ttm_bo_pipeline_gutting(bo);
978 * Check whether we need to move buffer.
980 if (!ttm_bo_mem_compat(placement, bo->resource, &new_flags)) {
981 ret = ttm_bo_move_buffer(bo, placement, ctx);
986 * We might need to add a TTM.
988 if (bo->resource->mem_type == TTM_PL_SYSTEM) {
989 ret = ttm_tt_create(bo, true);
995 EXPORT_SYMBOL(ttm_bo_validate);
997 int ttm_bo_init_reserved(struct ttm_device *bdev,
998 struct ttm_buffer_object *bo,
1000 enum ttm_bo_type type,
1001 struct ttm_placement *placement,
1002 uint32_t page_alignment,
1003 struct ttm_operation_ctx *ctx,
1004 struct sg_table *sg,
1005 struct dma_resv *resv,
1006 void (*destroy) (struct ttm_buffer_object *))
1008 static const struct ttm_place sys_mem = { .mem_type = TTM_PL_SYSTEM };
1012 bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1014 kref_init(&bo->kref);
1015 INIT_LIST_HEAD(&bo->lru);
1016 INIT_LIST_HEAD(&bo->ddestroy);
1019 bo->page_alignment = page_alignment;
1024 bo->base.resv = resv;
1025 dma_resv_assert_held(bo->base.resv);
1027 bo->base.resv = &bo->base._resv;
1029 atomic_inc(&ttm_glob.bo_count);
1031 ret = ttm_resource_alloc(bo, &sys_mem, &bo->resource);
1032 if (unlikely(ret)) {
1038 * For ttm_bo_type_device buffers, allocate
1039 * address space from the device.
1041 if (bo->type == ttm_bo_type_device ||
1042 bo->type == ttm_bo_type_sg)
1043 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
1044 bo->resource->num_pages);
1046 /* passed reservation objects should already be locked,
1047 * since otherwise lockdep will be angered in radeon.
1050 locked = dma_resv_trylock(bo->base.resv);
1055 ret = ttm_bo_validate(bo, placement, ctx);
1057 if (unlikely(ret)) {
1059 ttm_bo_unreserve(bo);
1065 ttm_bo_move_to_lru_tail_unlocked(bo);
1069 EXPORT_SYMBOL(ttm_bo_init_reserved);
1071 int ttm_bo_init(struct ttm_device *bdev,
1072 struct ttm_buffer_object *bo,
1074 enum ttm_bo_type type,
1075 struct ttm_placement *placement,
1076 uint32_t page_alignment,
1078 struct sg_table *sg,
1079 struct dma_resv *resv,
1080 void (*destroy) (struct ttm_buffer_object *))
1082 struct ttm_operation_ctx ctx = { interruptible, false };
1085 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1086 page_alignment, &ctx, sg, resv, destroy);
1091 ttm_bo_unreserve(bo);
1095 EXPORT_SYMBOL(ttm_bo_init);
1098 * buffer object vm functions.
1101 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1103 struct ttm_device *bdev = bo->bdev;
1105 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1106 ttm_mem_io_free(bdev, bo->resource);
1108 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1110 int ttm_bo_wait(struct ttm_buffer_object *bo,
1111 bool interruptible, bool no_wait)
1113 long timeout = 15 * HZ;
1116 if (dma_resv_test_signaled(bo->base.resv, true))
1122 timeout = dma_resv_wait_timeout(bo->base.resv, true, interruptible,
1130 dma_resv_add_excl_fence(bo->base.resv, NULL);
1133 EXPORT_SYMBOL(ttm_bo_wait);
1135 int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
1138 struct ttm_place place;
1143 * While the bo may already reside in SYSTEM placement, set
1144 * SYSTEM as new placement to cover also the move further below.
1145 * The driver may use the fact that we're moving from SYSTEM
1146 * as an indication that we're about to swap out.
1148 memset(&place, 0, sizeof(place));
1149 place.mem_type = TTM_PL_SYSTEM;
1150 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place, &locked, NULL))
1153 if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
1154 bo->ttm->page_flags & TTM_PAGE_FLAG_SG ||
1155 bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED ||
1156 !ttm_bo_get_unless_zero(bo)) {
1158 dma_resv_unlock(bo->base.resv);
1163 ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1165 return ret == -EBUSY ? -ENOSPC : ret;
1168 ttm_bo_del_from_lru(bo);
1169 /* TODO: Cleanup the locking */
1170 spin_unlock(&bo->bdev->lru_lock);
1173 * Move to system cached
1175 if (bo->resource->mem_type != TTM_PL_SYSTEM) {
1176 struct ttm_operation_ctx ctx = { false, false };
1177 struct ttm_resource *evict_mem;
1178 struct ttm_place hop;
1180 memset(&hop, 0, sizeof(hop));
1181 ret = ttm_resource_alloc(bo, &place, &evict_mem);
1185 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, &ctx, &hop);
1186 if (unlikely(ret != 0)) {
1187 WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
1193 * Make sure BO is idle.
1195 ret = ttm_bo_wait(bo, false, false);
1196 if (unlikely(ret != 0))
1199 ttm_bo_unmap_virtual(bo);
1202 * Swap out. Buffer will be swapped in again as soon as
1203 * anyone tries to access a ttm page.
1205 if (bo->bdev->funcs->swap_notify)
1206 bo->bdev->funcs->swap_notify(bo);
1208 if (ttm_tt_is_populated(bo->ttm))
1209 ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);
1213 * Unreserve without putting on LRU to avoid swapping out an
1214 * already swapped buffer.
1217 dma_resv_unlock(bo->base.resv);
1219 return ret == -EBUSY ? -ENOSPC : ret;
1222 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1224 if (bo->ttm == NULL)
1227 ttm_tt_destroy(bo->bdev, bo->ttm);