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_list *fobj;
273 struct dma_fence *fence;
277 fobj = dma_resv_shared_list(resv);
278 fence = dma_resv_excl_fence(resv);
279 if (fence && !fence->ops->signaled)
280 dma_fence_enable_sw_signaling(fence);
282 for (i = 0; fobj && i < fobj->shared_count; ++i) {
283 fence = rcu_dereference(fobj->shared[i]);
285 if (!fence->ops->signaled)
286 dma_fence_enable_sw_signaling(fence);
292 * ttm_bo_cleanup_refs
293 * If bo idle, remove from lru lists, and unref.
294 * If not idle, block if possible.
296 * Must be called with lru_lock and reservation held, this function
297 * will drop the lru lock and optionally the reservation lock before returning.
299 * @bo: The buffer object to clean-up
300 * @interruptible: Any sleeps should occur interruptibly.
301 * @no_wait_gpu: Never wait for gpu. Return -EBUSY instead.
302 * @unlock_resv: Unlock the reservation lock as well.
305 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
306 bool interruptible, bool no_wait_gpu,
309 struct dma_resv *resv = &bo->base._resv;
312 if (dma_resv_test_signaled(resv, true))
317 if (ret && !no_wait_gpu) {
321 dma_resv_unlock(bo->base.resv);
322 spin_unlock(&bo->bdev->lru_lock);
324 lret = dma_resv_wait_timeout(resv, true, interruptible,
332 spin_lock(&bo->bdev->lru_lock);
333 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
335 * We raced, and lost, someone else holds the reservation now,
336 * and is probably busy in ttm_bo_cleanup_memtype_use.
338 * Even if it's not the case, because we finished waiting any
339 * delayed destruction would succeed, so just return success
342 spin_unlock(&bo->bdev->lru_lock);
348 if (ret || unlikely(list_empty(&bo->ddestroy))) {
350 dma_resv_unlock(bo->base.resv);
351 spin_unlock(&bo->bdev->lru_lock);
355 ttm_bo_move_to_pinned(bo);
356 list_del_init(&bo->ddestroy);
357 spin_unlock(&bo->bdev->lru_lock);
358 ttm_bo_cleanup_memtype_use(bo);
361 dma_resv_unlock(bo->base.resv);
369 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
370 * encountered buffers.
372 bool ttm_bo_delayed_delete(struct ttm_device *bdev, bool remove_all)
374 struct list_head removed;
377 INIT_LIST_HEAD(&removed);
379 spin_lock(&bdev->lru_lock);
380 while (!list_empty(&bdev->ddestroy)) {
381 struct ttm_buffer_object *bo;
383 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
385 list_move_tail(&bo->ddestroy, &removed);
386 if (!ttm_bo_get_unless_zero(bo))
389 if (remove_all || bo->base.resv != &bo->base._resv) {
390 spin_unlock(&bdev->lru_lock);
391 dma_resv_lock(bo->base.resv, NULL);
393 spin_lock(&bdev->lru_lock);
394 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
396 } else if (dma_resv_trylock(bo->base.resv)) {
397 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
399 spin_unlock(&bdev->lru_lock);
403 spin_lock(&bdev->lru_lock);
405 list_splice_tail(&removed, &bdev->ddestroy);
406 empty = list_empty(&bdev->ddestroy);
407 spin_unlock(&bdev->lru_lock);
412 static void ttm_bo_release(struct kref *kref)
414 struct ttm_buffer_object *bo =
415 container_of(kref, struct ttm_buffer_object, kref);
416 struct ttm_device *bdev = bo->bdev;
419 WARN_ON_ONCE(bo->pin_count);
422 ret = ttm_bo_individualize_resv(bo);
424 /* Last resort, if we fail to allocate memory for the
425 * fences block for the BO to become idle
427 dma_resv_wait_timeout(bo->base.resv, true, false,
431 if (bo->bdev->funcs->release_notify)
432 bo->bdev->funcs->release_notify(bo);
434 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
435 ttm_mem_io_free(bdev, bo->resource);
438 if (!dma_resv_test_signaled(bo->base.resv, true) ||
439 !dma_resv_trylock(bo->base.resv)) {
440 /* The BO is not idle, resurrect it for delayed destroy */
441 ttm_bo_flush_all_fences(bo);
444 spin_lock(&bo->bdev->lru_lock);
447 * Make pinned bos immediately available to
448 * shrinkers, now that they are queued for
451 * FIXME: QXL is triggering this. Can be removed when the
456 ttm_bo_move_to_lru_tail(bo, bo->resource, NULL);
459 kref_init(&bo->kref);
460 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
461 spin_unlock(&bo->bdev->lru_lock);
463 schedule_delayed_work(&bdev->wq,
464 ((HZ / 100) < 1) ? 1 : HZ / 100);
468 spin_lock(&bo->bdev->lru_lock);
469 ttm_bo_del_from_lru(bo);
470 list_del(&bo->ddestroy);
471 spin_unlock(&bo->bdev->lru_lock);
473 ttm_bo_cleanup_memtype_use(bo);
474 dma_resv_unlock(bo->base.resv);
476 atomic_dec(&ttm_glob.bo_count);
477 dma_fence_put(bo->moving);
481 void ttm_bo_put(struct ttm_buffer_object *bo)
483 kref_put(&bo->kref, ttm_bo_release);
485 EXPORT_SYMBOL(ttm_bo_put);
487 int ttm_bo_lock_delayed_workqueue(struct ttm_device *bdev)
489 return cancel_delayed_work_sync(&bdev->wq);
491 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
493 void ttm_bo_unlock_delayed_workqueue(struct ttm_device *bdev, int resched)
496 schedule_delayed_work(&bdev->wq,
497 ((HZ / 100) < 1) ? 1 : HZ / 100);
499 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
501 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
502 struct ttm_resource **mem,
503 struct ttm_operation_ctx *ctx,
504 struct ttm_place *hop)
506 struct ttm_placement hop_placement;
507 struct ttm_resource *hop_mem;
510 hop_placement.num_placement = hop_placement.num_busy_placement = 1;
511 hop_placement.placement = hop_placement.busy_placement = hop;
513 /* find space in the bounce domain */
514 ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
517 /* move to the bounce domain */
518 ret = ttm_bo_handle_move_mem(bo, hop_mem, false, ctx, NULL);
520 ttm_resource_free(bo, &hop_mem);
526 static int ttm_bo_evict(struct ttm_buffer_object *bo,
527 struct ttm_operation_ctx *ctx)
529 struct ttm_device *bdev = bo->bdev;
530 struct ttm_resource *evict_mem;
531 struct ttm_placement placement;
532 struct ttm_place hop;
535 memset(&hop, 0, sizeof(hop));
537 dma_resv_assert_held(bo->base.resv);
539 placement.num_placement = 0;
540 placement.num_busy_placement = 0;
541 bdev->funcs->evict_flags(bo, &placement);
543 if (!placement.num_placement && !placement.num_busy_placement) {
544 ret = ttm_bo_wait(bo, true, false);
549 * Since we've already synced, this frees backing store
552 return ttm_bo_pipeline_gutting(bo);
555 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
557 if (ret != -ERESTARTSYS) {
558 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
560 ttm_bo_mem_space_debug(bo, &placement);
566 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
567 if (ret == -EMULTIHOP) {
568 ret = ttm_bo_bounce_temp_buffer(bo, &evict_mem, ctx, &hop);
570 pr_err("Buffer eviction failed\n");
571 ttm_resource_free(bo, &evict_mem);
574 /* try and move to final place now. */
581 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
582 const struct ttm_place *place)
584 dma_resv_assert_held(bo->base.resv);
585 if (bo->resource->mem_type == TTM_PL_SYSTEM)
588 /* Don't evict this BO if it's outside of the
589 * requested placement range
591 if (place->fpfn >= (bo->resource->start + bo->resource->num_pages) ||
592 (place->lpfn && place->lpfn <= bo->resource->start))
597 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
600 * Check the target bo is allowable to be evicted or swapout, including cases:
602 * a. if share same reservation object with ctx->resv, have assumption
603 * reservation objects should already be locked, so not lock again and
604 * return true directly when either the opreation allow_reserved_eviction
605 * or the target bo already is in delayed free list;
607 * b. Otherwise, trylock it.
609 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
610 struct ttm_operation_ctx *ctx,
611 const struct ttm_place *place,
612 bool *locked, bool *busy)
616 if (bo->base.resv == ctx->resv) {
617 dma_resv_assert_held(bo->base.resv);
618 if (ctx->allow_res_evict)
624 ret = dma_resv_trylock(bo->base.resv);
630 if (ret && place && !bo->bdev->funcs->eviction_valuable(bo, place)) {
633 dma_resv_unlock(bo->base.resv);
642 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
644 * @busy_bo: BO which couldn't be locked with trylock
645 * @ctx: operation context
646 * @ticket: acquire ticket
648 * Try to lock a busy buffer object to avoid failing eviction.
650 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
651 struct ttm_operation_ctx *ctx,
652 struct ww_acquire_ctx *ticket)
656 if (!busy_bo || !ticket)
659 if (ctx->interruptible)
660 r = dma_resv_lock_interruptible(busy_bo->base.resv,
663 r = dma_resv_lock(busy_bo->base.resv, ticket);
666 * TODO: It would be better to keep the BO locked until allocation is at
667 * least tried one more time, but that would mean a much larger rework
671 dma_resv_unlock(busy_bo->base.resv);
673 return r == -EDEADLK ? -EBUSY : r;
676 int ttm_mem_evict_first(struct ttm_device *bdev,
677 struct ttm_resource_manager *man,
678 const struct ttm_place *place,
679 struct ttm_operation_ctx *ctx,
680 struct ww_acquire_ctx *ticket)
682 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
687 spin_lock(&bdev->lru_lock);
688 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
689 list_for_each_entry(bo, &man->lru[i], lru) {
692 if (!ttm_bo_evict_swapout_allowable(bo, ctx, place,
694 if (busy && !busy_bo && ticket !=
695 dma_resv_locking_ctx(bo->base.resv))
700 if (!ttm_bo_get_unless_zero(bo)) {
702 dma_resv_unlock(bo->base.resv);
708 /* If the inner loop terminated early, we have our candidate */
709 if (&bo->lru != &man->lru[i])
716 if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
718 spin_unlock(&bdev->lru_lock);
719 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
726 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
727 ctx->no_wait_gpu, locked);
732 spin_unlock(&bdev->lru_lock);
734 ret = ttm_bo_evict(bo, ctx);
736 ttm_bo_unreserve(bo);
743 * Add the last move fence to the BO and reserve a new shared slot. We only use
744 * a shared slot to avoid unecessary sync and rely on the subsequent bo move to
745 * either stall or use an exclusive fence respectively set bo->moving.
747 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
748 struct ttm_resource_manager *man,
749 struct ttm_resource *mem,
752 struct dma_fence *fence;
755 spin_lock(&man->move_lock);
756 fence = dma_fence_get(man->move);
757 spin_unlock(&man->move_lock);
763 ret = dma_fence_is_signaled(fence) ? 0 : -EBUSY;
764 dma_fence_put(fence);
768 dma_resv_add_shared_fence(bo->base.resv, fence);
770 ret = dma_resv_reserve_shared(bo->base.resv, 1);
772 dma_fence_put(fence);
776 dma_fence_put(bo->moving);
782 * Repeatedly evict memory from the LRU for @mem_type until we create enough
783 * space, or we've evicted everything and there isn't enough space.
785 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
786 const struct ttm_place *place,
787 struct ttm_resource **mem,
788 struct ttm_operation_ctx *ctx)
790 struct ttm_device *bdev = bo->bdev;
791 struct ttm_resource_manager *man;
792 struct ww_acquire_ctx *ticket;
795 man = ttm_manager_type(bdev, place->mem_type);
796 ticket = dma_resv_locking_ctx(bo->base.resv);
798 ret = ttm_resource_alloc(bo, place, mem);
801 if (unlikely(ret != -ENOSPC))
803 ret = ttm_mem_evict_first(bdev, man, place, ctx,
805 if (unlikely(ret != 0))
809 return ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
813 * Creates space for memory region @mem according to its type.
815 * This function first searches for free space in compatible memory types in
816 * the priority order defined by the driver. If free space isn't found, then
817 * ttm_bo_mem_force_space is attempted in priority order to evict and find
820 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
821 struct ttm_placement *placement,
822 struct ttm_resource **mem,
823 struct ttm_operation_ctx *ctx)
825 struct ttm_device *bdev = bo->bdev;
826 bool type_found = false;
829 ret = dma_resv_reserve_shared(bo->base.resv, 1);
833 for (i = 0; i < placement->num_placement; ++i) {
834 const struct ttm_place *place = &placement->placement[i];
835 struct ttm_resource_manager *man;
837 man = ttm_manager_type(bdev, place->mem_type);
838 if (!man || !ttm_resource_manager_used(man))
842 ret = ttm_resource_alloc(bo, place, mem);
848 ret = ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
850 ttm_resource_free(bo, mem);
859 for (i = 0; i < placement->num_busy_placement; ++i) {
860 const struct ttm_place *place = &placement->busy_placement[i];
861 struct ttm_resource_manager *man;
863 man = ttm_manager_type(bdev, place->mem_type);
864 if (!man || !ttm_resource_manager_used(man))
868 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
872 if (ret && ret != -EBUSY)
878 pr_err(TTM_PFX "No compatible memory type found\n");
883 if (bo->resource->mem_type == TTM_PL_SYSTEM && !bo->pin_count)
884 ttm_bo_move_to_lru_tail_unlocked(bo);
888 EXPORT_SYMBOL(ttm_bo_mem_space);
890 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
891 struct ttm_placement *placement,
892 struct ttm_operation_ctx *ctx)
894 struct ttm_resource *mem;
895 struct ttm_place hop;
898 dma_resv_assert_held(bo->base.resv);
901 * Determine where to move the buffer.
903 * If driver determines move is going to need
904 * an extra step then it will return -EMULTIHOP
905 * and the buffer will be moved to the temporary
906 * stop and the driver will be called to make
909 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
913 ret = ttm_bo_handle_move_mem(bo, mem, false, ctx, &hop);
914 if (ret == -EMULTIHOP) {
915 ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
918 /* try and move to final place now. */
923 ttm_resource_free(bo, &mem);
927 int ttm_bo_validate(struct ttm_buffer_object *bo,
928 struct ttm_placement *placement,
929 struct ttm_operation_ctx *ctx)
933 dma_resv_assert_held(bo->base.resv);
936 * Remove the backing store if no placement is given.
938 if (!placement->num_placement && !placement->num_busy_placement)
939 return ttm_bo_pipeline_gutting(bo);
942 * Check whether we need to move buffer.
944 if (!ttm_resource_compat(bo->resource, placement)) {
945 ret = ttm_bo_move_buffer(bo, placement, ctx);
950 * We might need to add a TTM.
952 if (bo->resource->mem_type == TTM_PL_SYSTEM) {
953 ret = ttm_tt_create(bo, true);
959 EXPORT_SYMBOL(ttm_bo_validate);
961 int ttm_bo_init_reserved(struct ttm_device *bdev,
962 struct ttm_buffer_object *bo,
964 enum ttm_bo_type type,
965 struct ttm_placement *placement,
966 uint32_t page_alignment,
967 struct ttm_operation_ctx *ctx,
969 struct dma_resv *resv,
970 void (*destroy) (struct ttm_buffer_object *))
972 static const struct ttm_place sys_mem = { .mem_type = TTM_PL_SYSTEM };
976 bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
978 kref_init(&bo->kref);
979 INIT_LIST_HEAD(&bo->lru);
980 INIT_LIST_HEAD(&bo->ddestroy);
983 bo->page_alignment = page_alignment;
988 bo->base.resv = resv;
989 dma_resv_assert_held(bo->base.resv);
991 bo->base.resv = &bo->base._resv;
993 atomic_inc(&ttm_glob.bo_count);
995 ret = ttm_resource_alloc(bo, &sys_mem, &bo->resource);
1002 * For ttm_bo_type_device buffers, allocate
1003 * address space from the device.
1005 if (bo->type == ttm_bo_type_device ||
1006 bo->type == ttm_bo_type_sg)
1007 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
1008 bo->resource->num_pages);
1010 /* passed reservation objects should already be locked,
1011 * since otherwise lockdep will be angered in radeon.
1014 locked = dma_resv_trylock(bo->base.resv);
1019 ret = ttm_bo_validate(bo, placement, ctx);
1021 if (unlikely(ret)) {
1023 ttm_bo_unreserve(bo);
1029 ttm_bo_move_to_lru_tail_unlocked(bo);
1033 EXPORT_SYMBOL(ttm_bo_init_reserved);
1035 int ttm_bo_init(struct ttm_device *bdev,
1036 struct ttm_buffer_object *bo,
1038 enum ttm_bo_type type,
1039 struct ttm_placement *placement,
1040 uint32_t page_alignment,
1042 struct sg_table *sg,
1043 struct dma_resv *resv,
1044 void (*destroy) (struct ttm_buffer_object *))
1046 struct ttm_operation_ctx ctx = { interruptible, false };
1049 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1050 page_alignment, &ctx, sg, resv, destroy);
1055 ttm_bo_unreserve(bo);
1059 EXPORT_SYMBOL(ttm_bo_init);
1062 * buffer object vm functions.
1065 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1067 struct ttm_device *bdev = bo->bdev;
1069 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1070 ttm_mem_io_free(bdev, bo->resource);
1072 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1074 int ttm_bo_wait(struct ttm_buffer_object *bo,
1075 bool interruptible, bool no_wait)
1077 long timeout = 15 * HZ;
1080 if (dma_resv_test_signaled(bo->base.resv, true))
1086 timeout = dma_resv_wait_timeout(bo->base.resv, true, interruptible,
1094 dma_resv_add_excl_fence(bo->base.resv, NULL);
1097 EXPORT_SYMBOL(ttm_bo_wait);
1099 int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
1102 struct ttm_place place;
1107 * While the bo may already reside in SYSTEM placement, set
1108 * SYSTEM as new placement to cover also the move further below.
1109 * The driver may use the fact that we're moving from SYSTEM
1110 * as an indication that we're about to swap out.
1112 memset(&place, 0, sizeof(place));
1113 place.mem_type = TTM_PL_SYSTEM;
1114 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place, &locked, NULL))
1117 if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
1118 bo->ttm->page_flags & TTM_PAGE_FLAG_SG ||
1119 bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED ||
1120 !ttm_bo_get_unless_zero(bo)) {
1122 dma_resv_unlock(bo->base.resv);
1127 ttm_bo_cleanup_refs(bo, false, false, locked);
1132 ttm_bo_move_to_pinned(bo);
1133 /* TODO: Cleanup the locking */
1134 spin_unlock(&bo->bdev->lru_lock);
1137 * Move to system cached
1139 if (bo->resource->mem_type != TTM_PL_SYSTEM) {
1140 struct ttm_operation_ctx ctx = { false, false };
1141 struct ttm_resource *evict_mem;
1142 struct ttm_place hop;
1144 memset(&hop, 0, sizeof(hop));
1145 ret = ttm_resource_alloc(bo, &place, &evict_mem);
1149 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, &ctx, &hop);
1150 if (unlikely(ret != 0)) {
1151 WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
1157 * Make sure BO is idle.
1159 ret = ttm_bo_wait(bo, false, false);
1160 if (unlikely(ret != 0))
1163 ttm_bo_unmap_virtual(bo);
1166 * Swap out. Buffer will be swapped in again as soon as
1167 * anyone tries to access a ttm page.
1169 if (bo->bdev->funcs->swap_notify)
1170 bo->bdev->funcs->swap_notify(bo);
1172 if (ttm_tt_is_populated(bo->ttm))
1173 ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);
1177 * Unreserve without putting on LRU to avoid swapping out an
1178 * already swapped buffer.
1181 dma_resv_unlock(bo->base.resv);
1186 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1188 if (bo->ttm == NULL)
1191 ttm_tt_unpopulate(bo->bdev, bo->ttm);
1192 ttm_tt_destroy(bo->bdev, bo->ttm);