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 inline void ttm_bo_move_to_pinned(struct ttm_buffer_object *bo)
74 struct ttm_device *bdev = bo->bdev;
76 list_move_tail(&bo->lru, &bdev->pinned);
78 if (bdev->funcs->del_from_lru_notify)
79 bdev->funcs->del_from_lru_notify(bo);
82 static inline void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
84 struct ttm_device *bdev = bo->bdev;
86 list_del_init(&bo->lru);
88 if (bdev->funcs->del_from_lru_notify)
89 bdev->funcs->del_from_lru_notify(bo);
92 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
93 struct ttm_buffer_object *bo)
100 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
101 struct ttm_resource *mem,
102 struct ttm_lru_bulk_move *bulk)
104 struct ttm_device *bdev = bo->bdev;
105 struct ttm_resource_manager *man;
108 dma_resv_assert_held(bo->base.resv);
111 ttm_bo_move_to_pinned(bo);
118 man = ttm_manager_type(bdev, mem->mem_type);
119 list_move_tail(&bo->lru, &man->lru[bo->priority]);
121 if (bdev->funcs->del_from_lru_notify)
122 bdev->funcs->del_from_lru_notify(bo);
124 if (bulk && !bo->pin_count) {
125 switch (bo->resource->mem_type) {
127 ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
131 ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
136 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
138 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
142 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
143 struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
144 struct ttm_resource_manager *man;
149 dma_resv_assert_held(pos->first->base.resv);
150 dma_resv_assert_held(pos->last->base.resv);
152 man = ttm_manager_type(pos->first->bdev, TTM_PL_TT);
153 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
157 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
158 struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
159 struct ttm_resource_manager *man;
164 dma_resv_assert_held(pos->first->base.resv);
165 dma_resv_assert_held(pos->last->base.resv);
167 man = ttm_manager_type(pos->first->bdev, TTM_PL_VRAM);
168 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
172 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
174 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
175 struct ttm_resource *mem, bool evict,
176 struct ttm_operation_ctx *ctx,
177 struct ttm_place *hop)
179 struct ttm_resource_manager *old_man, *new_man;
180 struct ttm_device *bdev = bo->bdev;
183 old_man = ttm_manager_type(bdev, bo->resource->mem_type);
184 new_man = ttm_manager_type(bdev, mem->mem_type);
186 ttm_bo_unmap_virtual(bo);
189 * Create and bind a ttm if required.
192 if (new_man->use_tt) {
193 /* Zero init the new TTM structure if the old location should
194 * have used one as well.
196 ret = ttm_tt_create(bo, old_man->use_tt);
200 if (mem->mem_type != TTM_PL_SYSTEM) {
201 ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
207 ret = bdev->funcs->move(bo, evict, ctx, mem, hop);
209 if (ret == -EMULTIHOP)
214 ctx->bytes_moved += bo->base.size;
218 new_man = ttm_manager_type(bdev, bo->resource->mem_type);
219 if (!new_man->use_tt)
220 ttm_bo_tt_destroy(bo);
227 * Will release GPU memory type usage on destruction.
228 * This is the place to put in driver specific hooks to release
229 * driver private resources.
230 * Will release the bo::reserved lock.
233 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
235 if (bo->bdev->funcs->delete_mem_notify)
236 bo->bdev->funcs->delete_mem_notify(bo);
238 ttm_bo_tt_destroy(bo);
239 ttm_resource_free(bo, &bo->resource);
242 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
246 if (bo->base.resv == &bo->base._resv)
249 BUG_ON(!dma_resv_trylock(&bo->base._resv));
251 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
252 dma_resv_unlock(&bo->base._resv);
256 if (bo->type != ttm_bo_type_sg) {
257 /* This works because the BO is about to be destroyed and nobody
258 * reference it any more. The only tricky case is the trylock on
259 * the resv object while holding the lru_lock.
261 spin_lock(&bo->bdev->lru_lock);
262 bo->base.resv = &bo->base._resv;
263 spin_unlock(&bo->bdev->lru_lock);
269 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
271 struct dma_resv *resv = &bo->base._resv;
272 struct dma_resv_iter cursor;
273 struct dma_fence *fence;
275 dma_resv_iter_begin(&cursor, resv, true);
276 dma_resv_for_each_fence_unlocked(&cursor, fence) {
277 if (!fence->ops->signaled)
278 dma_fence_enable_sw_signaling(fence);
280 dma_resv_iter_end(&cursor);
284 * ttm_bo_cleanup_refs
285 * If bo idle, remove from lru lists, and unref.
286 * If not idle, block if possible.
288 * Must be called with lru_lock and reservation held, this function
289 * will drop the lru lock and optionally the reservation lock before returning.
291 * @bo: The buffer object to clean-up
292 * @interruptible: Any sleeps should occur interruptibly.
293 * @no_wait_gpu: Never wait for gpu. Return -EBUSY instead.
294 * @unlock_resv: Unlock the reservation lock as well.
297 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
298 bool interruptible, bool no_wait_gpu,
301 struct dma_resv *resv = &bo->base._resv;
304 if (dma_resv_test_signaled(resv, true))
309 if (ret && !no_wait_gpu) {
313 dma_resv_unlock(bo->base.resv);
314 spin_unlock(&bo->bdev->lru_lock);
316 lret = dma_resv_wait_timeout(resv, true, interruptible,
324 spin_lock(&bo->bdev->lru_lock);
325 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
327 * We raced, and lost, someone else holds the reservation now,
328 * and is probably busy in ttm_bo_cleanup_memtype_use.
330 * Even if it's not the case, because we finished waiting any
331 * delayed destruction would succeed, so just return success
334 spin_unlock(&bo->bdev->lru_lock);
340 if (ret || unlikely(list_empty(&bo->ddestroy))) {
342 dma_resv_unlock(bo->base.resv);
343 spin_unlock(&bo->bdev->lru_lock);
347 ttm_bo_move_to_pinned(bo);
348 list_del_init(&bo->ddestroy);
349 spin_unlock(&bo->bdev->lru_lock);
350 ttm_bo_cleanup_memtype_use(bo);
353 dma_resv_unlock(bo->base.resv);
361 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
362 * encountered buffers.
364 bool ttm_bo_delayed_delete(struct ttm_device *bdev, bool remove_all)
366 struct list_head removed;
369 INIT_LIST_HEAD(&removed);
371 spin_lock(&bdev->lru_lock);
372 while (!list_empty(&bdev->ddestroy)) {
373 struct ttm_buffer_object *bo;
375 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
377 list_move_tail(&bo->ddestroy, &removed);
378 if (!ttm_bo_get_unless_zero(bo))
381 if (remove_all || bo->base.resv != &bo->base._resv) {
382 spin_unlock(&bdev->lru_lock);
383 dma_resv_lock(bo->base.resv, NULL);
385 spin_lock(&bdev->lru_lock);
386 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
388 } else if (dma_resv_trylock(bo->base.resv)) {
389 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
391 spin_unlock(&bdev->lru_lock);
395 spin_lock(&bdev->lru_lock);
397 list_splice_tail(&removed, &bdev->ddestroy);
398 empty = list_empty(&bdev->ddestroy);
399 spin_unlock(&bdev->lru_lock);
404 static void ttm_bo_release(struct kref *kref)
406 struct ttm_buffer_object *bo =
407 container_of(kref, struct ttm_buffer_object, kref);
408 struct ttm_device *bdev = bo->bdev;
411 WARN_ON_ONCE(bo->pin_count);
414 ret = ttm_bo_individualize_resv(bo);
416 /* Last resort, if we fail to allocate memory for the
417 * fences block for the BO to become idle
419 dma_resv_wait_timeout(bo->base.resv, true, false,
423 if (bo->bdev->funcs->release_notify)
424 bo->bdev->funcs->release_notify(bo);
426 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
427 ttm_mem_io_free(bdev, bo->resource);
430 if (!dma_resv_test_signaled(bo->base.resv, true) ||
431 !dma_resv_trylock(bo->base.resv)) {
432 /* The BO is not idle, resurrect it for delayed destroy */
433 ttm_bo_flush_all_fences(bo);
436 spin_lock(&bo->bdev->lru_lock);
439 * Make pinned bos immediately available to
440 * shrinkers, now that they are queued for
443 * FIXME: QXL is triggering this. Can be removed when the
448 ttm_bo_move_to_lru_tail(bo, bo->resource, NULL);
451 kref_init(&bo->kref);
452 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
453 spin_unlock(&bo->bdev->lru_lock);
455 schedule_delayed_work(&bdev->wq,
456 ((HZ / 100) < 1) ? 1 : HZ / 100);
460 spin_lock(&bo->bdev->lru_lock);
461 ttm_bo_del_from_lru(bo);
462 list_del(&bo->ddestroy);
463 spin_unlock(&bo->bdev->lru_lock);
465 ttm_bo_cleanup_memtype_use(bo);
466 dma_resv_unlock(bo->base.resv);
468 atomic_dec(&ttm_glob.bo_count);
469 dma_fence_put(bo->moving);
473 void ttm_bo_put(struct ttm_buffer_object *bo)
475 kref_put(&bo->kref, ttm_bo_release);
477 EXPORT_SYMBOL(ttm_bo_put);
479 int ttm_bo_lock_delayed_workqueue(struct ttm_device *bdev)
481 return cancel_delayed_work_sync(&bdev->wq);
483 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
485 void ttm_bo_unlock_delayed_workqueue(struct ttm_device *bdev, int resched)
488 schedule_delayed_work(&bdev->wq,
489 ((HZ / 100) < 1) ? 1 : HZ / 100);
491 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
493 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
494 struct ttm_resource **mem,
495 struct ttm_operation_ctx *ctx,
496 struct ttm_place *hop)
498 struct ttm_placement hop_placement;
499 struct ttm_resource *hop_mem;
502 hop_placement.num_placement = hop_placement.num_busy_placement = 1;
503 hop_placement.placement = hop_placement.busy_placement = hop;
505 /* find space in the bounce domain */
506 ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
509 /* move to the bounce domain */
510 ret = ttm_bo_handle_move_mem(bo, hop_mem, false, ctx, NULL);
512 ttm_resource_free(bo, &hop_mem);
518 static int ttm_bo_evict(struct ttm_buffer_object *bo,
519 struct ttm_operation_ctx *ctx)
521 struct ttm_device *bdev = bo->bdev;
522 struct ttm_resource *evict_mem;
523 struct ttm_placement placement;
524 struct ttm_place hop;
527 memset(&hop, 0, sizeof(hop));
529 dma_resv_assert_held(bo->base.resv);
531 placement.num_placement = 0;
532 placement.num_busy_placement = 0;
533 bdev->funcs->evict_flags(bo, &placement);
535 if (!placement.num_placement && !placement.num_busy_placement) {
536 ret = ttm_bo_wait(bo, true, false);
541 * Since we've already synced, this frees backing store
544 return ttm_bo_pipeline_gutting(bo);
547 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
549 if (ret != -ERESTARTSYS) {
550 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
552 ttm_bo_mem_space_debug(bo, &placement);
558 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
559 if (ret == -EMULTIHOP) {
560 ret = ttm_bo_bounce_temp_buffer(bo, &evict_mem, ctx, &hop);
562 pr_err("Buffer eviction failed\n");
563 ttm_resource_free(bo, &evict_mem);
566 /* try and move to final place now. */
573 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
574 const struct ttm_place *place)
576 dma_resv_assert_held(bo->base.resv);
577 if (bo->resource->mem_type == TTM_PL_SYSTEM)
580 /* Don't evict this BO if it's outside of the
581 * requested placement range
583 if (place->fpfn >= (bo->resource->start + bo->resource->num_pages) ||
584 (place->lpfn && place->lpfn <= bo->resource->start))
589 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
592 * Check the target bo is allowable to be evicted or swapout, including cases:
594 * a. if share same reservation object with ctx->resv, have assumption
595 * reservation objects should already be locked, so not lock again and
596 * return true directly when either the opreation allow_reserved_eviction
597 * or the target bo already is in delayed free list;
599 * b. Otherwise, trylock it.
601 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
602 struct ttm_operation_ctx *ctx,
603 const struct ttm_place *place,
604 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);
622 if (ret && place && (bo->resource->mem_type != place->mem_type ||
623 !bo->bdev->funcs->eviction_valuable(bo, place))) {
626 dma_resv_unlock(bo->base.resv);
635 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
637 * @busy_bo: BO which couldn't be locked with trylock
638 * @ctx: operation context
639 * @ticket: acquire ticket
641 * Try to lock a busy buffer object to avoid failing eviction.
643 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
644 struct ttm_operation_ctx *ctx,
645 struct ww_acquire_ctx *ticket)
649 if (!busy_bo || !ticket)
652 if (ctx->interruptible)
653 r = dma_resv_lock_interruptible(busy_bo->base.resv,
656 r = dma_resv_lock(busy_bo->base.resv, ticket);
659 * TODO: It would be better to keep the BO locked until allocation is at
660 * least tried one more time, but that would mean a much larger rework
664 dma_resv_unlock(busy_bo->base.resv);
666 return r == -EDEADLK ? -EBUSY : r;
669 int ttm_mem_evict_first(struct ttm_device *bdev,
670 struct ttm_resource_manager *man,
671 const struct ttm_place *place,
672 struct ttm_operation_ctx *ctx,
673 struct ww_acquire_ctx *ticket)
675 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
680 spin_lock(&bdev->lru_lock);
681 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
682 list_for_each_entry(bo, &man->lru[i], lru) {
685 if (!ttm_bo_evict_swapout_allowable(bo, ctx, place,
687 if (busy && !busy_bo && ticket !=
688 dma_resv_locking_ctx(bo->base.resv))
693 if (!ttm_bo_get_unless_zero(bo)) {
695 dma_resv_unlock(bo->base.resv);
701 /* If the inner loop terminated early, we have our candidate */
702 if (&bo->lru != &man->lru[i])
709 if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
711 spin_unlock(&bdev->lru_lock);
712 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
719 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
720 ctx->no_wait_gpu, locked);
725 spin_unlock(&bdev->lru_lock);
727 ret = ttm_bo_evict(bo, ctx);
729 ttm_bo_unreserve(bo);
731 ttm_bo_move_to_lru_tail_unlocked(bo);
738 * Add the last move fence to the BO and reserve a new shared slot. We only use
739 * a shared slot to avoid unecessary sync and rely on the subsequent bo move to
740 * either stall or use an exclusive fence respectively set bo->moving.
742 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
743 struct ttm_resource_manager *man,
744 struct ttm_resource *mem,
747 struct dma_fence *fence;
750 spin_lock(&man->move_lock);
751 fence = dma_fence_get(man->move);
752 spin_unlock(&man->move_lock);
758 ret = dma_fence_is_signaled(fence) ? 0 : -EBUSY;
759 dma_fence_put(fence);
763 dma_resv_add_shared_fence(bo->base.resv, fence);
765 ret = dma_resv_reserve_shared(bo->base.resv, 1);
767 dma_fence_put(fence);
771 dma_fence_put(bo->moving);
777 * Repeatedly evict memory from the LRU for @mem_type until we create enough
778 * space, or we've evicted everything and there isn't enough space.
780 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
781 const struct ttm_place *place,
782 struct ttm_resource **mem,
783 struct ttm_operation_ctx *ctx)
785 struct ttm_device *bdev = bo->bdev;
786 struct ttm_resource_manager *man;
787 struct ww_acquire_ctx *ticket;
790 man = ttm_manager_type(bdev, place->mem_type);
791 ticket = dma_resv_locking_ctx(bo->base.resv);
793 ret = ttm_resource_alloc(bo, place, mem);
796 if (unlikely(ret != -ENOSPC))
798 ret = ttm_mem_evict_first(bdev, man, place, ctx,
800 if (unlikely(ret != 0))
804 return ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
808 * Creates space for memory region @mem according to its type.
810 * This function first searches for free space in compatible memory types in
811 * the priority order defined by the driver. If free space isn't found, then
812 * ttm_bo_mem_force_space is attempted in priority order to evict and find
815 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
816 struct ttm_placement *placement,
817 struct ttm_resource **mem,
818 struct ttm_operation_ctx *ctx)
820 struct ttm_device *bdev = bo->bdev;
821 bool type_found = false;
824 ret = dma_resv_reserve_shared(bo->base.resv, 1);
828 for (i = 0; i < placement->num_placement; ++i) {
829 const struct ttm_place *place = &placement->placement[i];
830 struct ttm_resource_manager *man;
832 man = ttm_manager_type(bdev, place->mem_type);
833 if (!man || !ttm_resource_manager_used(man))
837 ret = ttm_resource_alloc(bo, place, mem);
843 ret = ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
845 ttm_resource_free(bo, mem);
854 for (i = 0; i < placement->num_busy_placement; ++i) {
855 const struct ttm_place *place = &placement->busy_placement[i];
856 struct ttm_resource_manager *man;
858 man = ttm_manager_type(bdev, place->mem_type);
859 if (!man || !ttm_resource_manager_used(man))
863 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
867 if (ret && ret != -EBUSY)
873 pr_err(TTM_PFX "No compatible memory type found\n");
878 if (bo->resource->mem_type == TTM_PL_SYSTEM && !bo->pin_count)
879 ttm_bo_move_to_lru_tail_unlocked(bo);
883 EXPORT_SYMBOL(ttm_bo_mem_space);
885 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
886 struct ttm_placement *placement,
887 struct ttm_operation_ctx *ctx)
889 struct ttm_resource *mem;
890 struct ttm_place hop;
893 dma_resv_assert_held(bo->base.resv);
896 * Determine where to move the buffer.
898 * If driver determines move is going to need
899 * an extra step then it will return -EMULTIHOP
900 * and the buffer will be moved to the temporary
901 * stop and the driver will be called to make
904 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
908 ret = ttm_bo_handle_move_mem(bo, mem, false, ctx, &hop);
909 if (ret == -EMULTIHOP) {
910 ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
913 /* try and move to final place now. */
918 ttm_resource_free(bo, &mem);
922 int ttm_bo_validate(struct ttm_buffer_object *bo,
923 struct ttm_placement *placement,
924 struct ttm_operation_ctx *ctx)
928 dma_resv_assert_held(bo->base.resv);
931 * Remove the backing store if no placement is given.
933 if (!placement->num_placement && !placement->num_busy_placement)
934 return ttm_bo_pipeline_gutting(bo);
937 * Check whether we need to move buffer.
939 if (!ttm_resource_compat(bo->resource, placement)) {
940 ret = ttm_bo_move_buffer(bo, placement, ctx);
945 * We might need to add a TTM.
947 if (bo->resource->mem_type == TTM_PL_SYSTEM) {
948 ret = ttm_tt_create(bo, true);
954 EXPORT_SYMBOL(ttm_bo_validate);
956 int ttm_bo_init_reserved(struct ttm_device *bdev,
957 struct ttm_buffer_object *bo,
959 enum ttm_bo_type type,
960 struct ttm_placement *placement,
961 uint32_t page_alignment,
962 struct ttm_operation_ctx *ctx,
964 struct dma_resv *resv,
965 void (*destroy) (struct ttm_buffer_object *))
967 static const struct ttm_place sys_mem = { .mem_type = TTM_PL_SYSTEM };
971 bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
973 kref_init(&bo->kref);
974 INIT_LIST_HEAD(&bo->lru);
975 INIT_LIST_HEAD(&bo->ddestroy);
978 bo->page_alignment = page_alignment;
983 bo->base.resv = resv;
984 dma_resv_assert_held(bo->base.resv);
986 bo->base.resv = &bo->base._resv;
988 atomic_inc(&ttm_glob.bo_count);
990 ret = ttm_resource_alloc(bo, &sys_mem, &bo->resource);
997 * For ttm_bo_type_device buffers, allocate
998 * address space from the device.
1000 if (bo->type == ttm_bo_type_device ||
1001 bo->type == ttm_bo_type_sg)
1002 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
1003 bo->resource->num_pages);
1005 /* passed reservation objects should already be locked,
1006 * since otherwise lockdep will be angered in radeon.
1009 locked = dma_resv_trylock(bo->base.resv);
1014 ret = ttm_bo_validate(bo, placement, ctx);
1016 if (unlikely(ret)) {
1018 ttm_bo_unreserve(bo);
1024 ttm_bo_move_to_lru_tail_unlocked(bo);
1028 EXPORT_SYMBOL(ttm_bo_init_reserved);
1030 int ttm_bo_init(struct ttm_device *bdev,
1031 struct ttm_buffer_object *bo,
1033 enum ttm_bo_type type,
1034 struct ttm_placement *placement,
1035 uint32_t page_alignment,
1037 struct sg_table *sg,
1038 struct dma_resv *resv,
1039 void (*destroy) (struct ttm_buffer_object *))
1041 struct ttm_operation_ctx ctx = { interruptible, false };
1044 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1045 page_alignment, &ctx, sg, resv, destroy);
1050 ttm_bo_unreserve(bo);
1054 EXPORT_SYMBOL(ttm_bo_init);
1057 * buffer object vm functions.
1060 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1062 struct ttm_device *bdev = bo->bdev;
1064 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1065 ttm_mem_io_free(bdev, bo->resource);
1067 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1069 int ttm_bo_wait(struct ttm_buffer_object *bo,
1070 bool interruptible, bool no_wait)
1072 long timeout = 15 * HZ;
1075 if (dma_resv_test_signaled(bo->base.resv, true))
1081 timeout = dma_resv_wait_timeout(bo->base.resv, true, interruptible,
1091 EXPORT_SYMBOL(ttm_bo_wait);
1093 int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
1096 struct ttm_place place;
1101 * While the bo may already reside in SYSTEM placement, set
1102 * SYSTEM as new placement to cover also the move further below.
1103 * The driver may use the fact that we're moving from SYSTEM
1104 * as an indication that we're about to swap out.
1106 memset(&place, 0, sizeof(place));
1107 place.mem_type = bo->resource->mem_type;
1108 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place, &locked, NULL))
1111 if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
1112 bo->ttm->page_flags & TTM_TT_FLAG_EXTERNAL ||
1113 bo->ttm->page_flags & TTM_TT_FLAG_SWAPPED ||
1114 !ttm_bo_get_unless_zero(bo)) {
1116 dma_resv_unlock(bo->base.resv);
1121 ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1123 return ret == -EBUSY ? -ENOSPC : ret;
1126 ttm_bo_move_to_pinned(bo);
1127 /* TODO: Cleanup the locking */
1128 spin_unlock(&bo->bdev->lru_lock);
1131 * Move to system cached
1133 if (bo->resource->mem_type != TTM_PL_SYSTEM) {
1134 struct ttm_operation_ctx ctx = { false, false };
1135 struct ttm_resource *evict_mem;
1136 struct ttm_place hop;
1138 memset(&hop, 0, sizeof(hop));
1139 place.mem_type = TTM_PL_SYSTEM;
1140 ret = ttm_resource_alloc(bo, &place, &evict_mem);
1144 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, &ctx, &hop);
1145 if (unlikely(ret != 0)) {
1146 WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
1152 * Make sure BO is idle.
1154 ret = ttm_bo_wait(bo, false, false);
1155 if (unlikely(ret != 0))
1158 ttm_bo_unmap_virtual(bo);
1161 * Swap out. Buffer will be swapped in again as soon as
1162 * anyone tries to access a ttm page.
1164 if (bo->bdev->funcs->swap_notify)
1165 bo->bdev->funcs->swap_notify(bo);
1167 if (ttm_tt_is_populated(bo->ttm))
1168 ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);
1172 * Unreserve without putting on LRU to avoid swapping out an
1173 * already swapped buffer.
1176 dma_resv_unlock(bo->base.resv);
1178 return ret == -EBUSY ? -ENOSPC : ret;
1181 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1183 if (bo->ttm == NULL)
1186 ttm_tt_unpopulate(bo->bdev, bo->ttm);
1187 ttm_tt_destroy(bo->bdev, bo->ttm);