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 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
48 struct ttm_placement *placement)
50 struct drm_printer p = drm_debug_printer(TTM_PFX);
51 struct ttm_resource_manager *man;
54 drm_printf(&p, "No space for %p (%lu pages, %zuK, %zuM)\n",
55 bo, bo->resource->num_pages, bo->base.size >> 10,
57 for (i = 0; i < placement->num_placement; i++) {
58 mem_type = placement->placement[i].mem_type;
59 drm_printf(&p, " placement[%d]=0x%08X (%d)\n",
60 i, placement->placement[i].flags, mem_type);
61 man = ttm_manager_type(bo->bdev, mem_type);
62 ttm_resource_manager_debug(man, &p);
67 * ttm_bo_move_to_lru_tail
69 * @bo: The buffer object.
71 * Move this BO to the tail of all lru lists used to lookup and reserve an
72 * object. This function must be called with struct ttm_global::lru_lock
73 * held, and is used to make a BO less likely to be considered for eviction.
75 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo)
77 dma_resv_assert_held(bo->base.resv);
80 ttm_resource_move_to_lru_tail(bo->resource);
82 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
85 * ttm_bo_set_bulk_move - update BOs bulk move object
87 * @bo: The buffer object.
89 * Update the BOs bulk move object, making sure that resources are added/removed
90 * as well. A bulk move allows to move many resource on the LRU at once,
91 * resulting in much less overhead of maintaining the LRU.
92 * The only requirement is that the resources stay together on the LRU and are
93 * never separated. This is enforces by setting the bulk_move structure on a BO.
94 * ttm_lru_bulk_move_tail() should be used to move all resources to the tail of
97 void ttm_bo_set_bulk_move(struct ttm_buffer_object *bo,
98 struct ttm_lru_bulk_move *bulk)
100 dma_resv_assert_held(bo->base.resv);
102 if (bo->bulk_move == bulk)
105 spin_lock(&bo->bdev->lru_lock);
107 ttm_resource_del_bulk_move(bo->resource, bo);
108 bo->bulk_move = bulk;
110 ttm_resource_add_bulk_move(bo->resource, bo);
111 spin_unlock(&bo->bdev->lru_lock);
113 EXPORT_SYMBOL(ttm_bo_set_bulk_move);
115 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
116 struct ttm_resource *mem, bool evict,
117 struct ttm_operation_ctx *ctx,
118 struct ttm_place *hop)
120 struct ttm_device *bdev = bo->bdev;
121 bool old_use_tt, new_use_tt;
124 old_use_tt = bo->resource &&
125 ttm_manager_type(bdev, bo->resource->mem_type)->use_tt;
126 new_use_tt = ttm_manager_type(bdev, mem->mem_type)->use_tt;
128 ttm_bo_unmap_virtual(bo);
131 * Create and bind a ttm if required.
135 /* Zero init the new TTM structure if the old location should
136 * have used one as well.
138 ret = ttm_tt_create(bo, old_use_tt);
142 if (mem->mem_type != TTM_PL_SYSTEM) {
143 ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
149 ret = dma_resv_reserve_fences(bo->base.resv, 1);
153 ret = bdev->funcs->move(bo, evict, ctx, mem, hop);
155 if (ret == -EMULTIHOP)
160 ctx->bytes_moved += bo->base.size;
165 ttm_bo_tt_destroy(bo);
172 * Will release GPU memory type usage on destruction.
173 * This is the place to put in driver specific hooks to release
174 * driver private resources.
175 * Will release the bo::reserved lock.
178 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
180 if (bo->bdev->funcs->delete_mem_notify)
181 bo->bdev->funcs->delete_mem_notify(bo);
183 ttm_bo_tt_destroy(bo);
184 ttm_resource_free(bo, &bo->resource);
187 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
191 if (bo->base.resv == &bo->base._resv)
194 BUG_ON(!dma_resv_trylock(&bo->base._resv));
196 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
197 dma_resv_unlock(&bo->base._resv);
201 if (bo->type != ttm_bo_type_sg) {
202 /* This works because the BO is about to be destroyed and nobody
203 * reference it any more. The only tricky case is the trylock on
204 * the resv object while holding the lru_lock.
206 spin_lock(&bo->bdev->lru_lock);
207 bo->base.resv = &bo->base._resv;
208 spin_unlock(&bo->bdev->lru_lock);
214 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
216 struct dma_resv *resv = &bo->base._resv;
217 struct dma_resv_iter cursor;
218 struct dma_fence *fence;
220 dma_resv_iter_begin(&cursor, resv, DMA_RESV_USAGE_BOOKKEEP);
221 dma_resv_for_each_fence_unlocked(&cursor, fence) {
222 if (!fence->ops->signaled)
223 dma_fence_enable_sw_signaling(fence);
225 dma_resv_iter_end(&cursor);
229 * ttm_bo_cleanup_refs
230 * If bo idle, remove from lru lists, and unref.
231 * If not idle, block if possible.
233 * Must be called with lru_lock and reservation held, this function
234 * will drop the lru lock and optionally the reservation lock before returning.
236 * @bo: The buffer object to clean-up
237 * @interruptible: Any sleeps should occur interruptibly.
238 * @no_wait_gpu: Never wait for gpu. Return -EBUSY instead.
239 * @unlock_resv: Unlock the reservation lock as well.
242 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
243 bool interruptible, bool no_wait_gpu,
246 struct dma_resv *resv = &bo->base._resv;
249 if (dma_resv_test_signaled(resv, DMA_RESV_USAGE_BOOKKEEP))
254 if (ret && !no_wait_gpu) {
258 dma_resv_unlock(bo->base.resv);
259 spin_unlock(&bo->bdev->lru_lock);
261 lret = dma_resv_wait_timeout(resv, DMA_RESV_USAGE_BOOKKEEP,
270 spin_lock(&bo->bdev->lru_lock);
271 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
273 * We raced, and lost, someone else holds the reservation now,
274 * and is probably busy in ttm_bo_cleanup_memtype_use.
276 * Even if it's not the case, because we finished waiting any
277 * delayed destruction would succeed, so just return success
280 spin_unlock(&bo->bdev->lru_lock);
286 if (ret || unlikely(list_empty(&bo->ddestroy))) {
288 dma_resv_unlock(bo->base.resv);
289 spin_unlock(&bo->bdev->lru_lock);
293 list_del_init(&bo->ddestroy);
294 spin_unlock(&bo->bdev->lru_lock);
295 ttm_bo_cleanup_memtype_use(bo);
298 dma_resv_unlock(bo->base.resv);
306 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
307 * encountered buffers.
309 bool ttm_bo_delayed_delete(struct ttm_device *bdev, bool remove_all)
311 struct list_head removed;
314 INIT_LIST_HEAD(&removed);
316 spin_lock(&bdev->lru_lock);
317 while (!list_empty(&bdev->ddestroy)) {
318 struct ttm_buffer_object *bo;
320 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
322 list_move_tail(&bo->ddestroy, &removed);
323 if (!ttm_bo_get_unless_zero(bo))
326 if (remove_all || bo->base.resv != &bo->base._resv) {
327 spin_unlock(&bdev->lru_lock);
328 dma_resv_lock(bo->base.resv, NULL);
330 spin_lock(&bdev->lru_lock);
331 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
333 } else if (dma_resv_trylock(bo->base.resv)) {
334 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
336 spin_unlock(&bdev->lru_lock);
340 spin_lock(&bdev->lru_lock);
342 list_splice_tail(&removed, &bdev->ddestroy);
343 empty = list_empty(&bdev->ddestroy);
344 spin_unlock(&bdev->lru_lock);
349 static void ttm_bo_release(struct kref *kref)
351 struct ttm_buffer_object *bo =
352 container_of(kref, struct ttm_buffer_object, kref);
353 struct ttm_device *bdev = bo->bdev;
356 WARN_ON_ONCE(bo->pin_count);
357 WARN_ON_ONCE(bo->bulk_move);
360 ret = ttm_bo_individualize_resv(bo);
362 /* Last resort, if we fail to allocate memory for the
363 * fences block for the BO to become idle
365 dma_resv_wait_timeout(bo->base.resv,
366 DMA_RESV_USAGE_BOOKKEEP, false,
370 if (bo->bdev->funcs->release_notify)
371 bo->bdev->funcs->release_notify(bo);
373 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
374 ttm_mem_io_free(bdev, bo->resource);
377 if (!dma_resv_test_signaled(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP) ||
378 !dma_resv_trylock(bo->base.resv)) {
379 /* The BO is not idle, resurrect it for delayed destroy */
380 ttm_bo_flush_all_fences(bo);
383 spin_lock(&bo->bdev->lru_lock);
386 * Make pinned bos immediately available to
387 * shrinkers, now that they are queued for
390 * FIXME: QXL is triggering this. Can be removed when the
395 ttm_resource_move_to_lru_tail(bo->resource);
398 kref_init(&bo->kref);
399 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
400 spin_unlock(&bo->bdev->lru_lock);
402 schedule_delayed_work(&bdev->wq,
403 ((HZ / 100) < 1) ? 1 : HZ / 100);
407 spin_lock(&bo->bdev->lru_lock);
408 list_del(&bo->ddestroy);
409 spin_unlock(&bo->bdev->lru_lock);
411 ttm_bo_cleanup_memtype_use(bo);
412 dma_resv_unlock(bo->base.resv);
414 atomic_dec(&ttm_glob.bo_count);
418 void ttm_bo_put(struct ttm_buffer_object *bo)
420 kref_put(&bo->kref, ttm_bo_release);
422 EXPORT_SYMBOL(ttm_bo_put);
424 int ttm_bo_lock_delayed_workqueue(struct ttm_device *bdev)
426 return cancel_delayed_work_sync(&bdev->wq);
428 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
430 void ttm_bo_unlock_delayed_workqueue(struct ttm_device *bdev, int resched)
433 schedule_delayed_work(&bdev->wq,
434 ((HZ / 100) < 1) ? 1 : HZ / 100);
436 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
438 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
439 struct ttm_resource **mem,
440 struct ttm_operation_ctx *ctx,
441 struct ttm_place *hop)
443 struct ttm_placement hop_placement;
444 struct ttm_resource *hop_mem;
447 hop_placement.num_placement = hop_placement.num_busy_placement = 1;
448 hop_placement.placement = hop_placement.busy_placement = hop;
450 /* find space in the bounce domain */
451 ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
454 /* move to the bounce domain */
455 ret = ttm_bo_handle_move_mem(bo, hop_mem, false, ctx, NULL);
457 ttm_resource_free(bo, &hop_mem);
463 static int ttm_bo_evict(struct ttm_buffer_object *bo,
464 struct ttm_operation_ctx *ctx)
466 struct ttm_device *bdev = bo->bdev;
467 struct ttm_resource *evict_mem;
468 struct ttm_placement placement;
469 struct ttm_place hop;
472 memset(&hop, 0, sizeof(hop));
474 dma_resv_assert_held(bo->base.resv);
476 placement.num_placement = 0;
477 placement.num_busy_placement = 0;
478 bdev->funcs->evict_flags(bo, &placement);
480 if (!placement.num_placement && !placement.num_busy_placement) {
481 ret = ttm_bo_wait(bo, true, false);
486 * Since we've already synced, this frees backing store
489 return ttm_bo_pipeline_gutting(bo);
492 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
494 if (ret != -ERESTARTSYS) {
495 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
497 ttm_bo_mem_space_debug(bo, &placement);
503 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
504 if (ret == -EMULTIHOP) {
505 ret = ttm_bo_bounce_temp_buffer(bo, &evict_mem, ctx, &hop);
507 pr_err("Buffer eviction failed\n");
508 ttm_resource_free(bo, &evict_mem);
511 /* try and move to final place now. */
518 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
519 const struct ttm_place *place)
521 dma_resv_assert_held(bo->base.resv);
522 if (bo->resource->mem_type == TTM_PL_SYSTEM)
525 /* Don't evict this BO if it's outside of the
526 * requested placement range
528 if (place->fpfn >= (bo->resource->start + bo->resource->num_pages) ||
529 (place->lpfn && place->lpfn <= bo->resource->start))
534 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
537 * Check the target bo is allowable to be evicted or swapout, including cases:
539 * a. if share same reservation object with ctx->resv, have assumption
540 * reservation objects should already be locked, so not lock again and
541 * return true directly when either the opreation allow_reserved_eviction
542 * or the target bo already is in delayed free list;
544 * b. Otherwise, trylock it.
546 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
547 struct ttm_operation_ctx *ctx,
548 const struct ttm_place *place,
549 bool *locked, bool *busy)
553 if (bo->base.resv == ctx->resv) {
554 dma_resv_assert_held(bo->base.resv);
555 if (ctx->allow_res_evict)
561 ret = dma_resv_trylock(bo->base.resv);
567 if (ret && place && (bo->resource->mem_type != place->mem_type ||
568 !bo->bdev->funcs->eviction_valuable(bo, place))) {
571 dma_resv_unlock(bo->base.resv);
580 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
582 * @busy_bo: BO which couldn't be locked with trylock
583 * @ctx: operation context
584 * @ticket: acquire ticket
586 * Try to lock a busy buffer object to avoid failing eviction.
588 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
589 struct ttm_operation_ctx *ctx,
590 struct ww_acquire_ctx *ticket)
594 if (!busy_bo || !ticket)
597 if (ctx->interruptible)
598 r = dma_resv_lock_interruptible(busy_bo->base.resv,
601 r = dma_resv_lock(busy_bo->base.resv, ticket);
604 * TODO: It would be better to keep the BO locked until allocation is at
605 * least tried one more time, but that would mean a much larger rework
609 dma_resv_unlock(busy_bo->base.resv);
611 return r == -EDEADLK ? -EBUSY : r;
614 int ttm_mem_evict_first(struct ttm_device *bdev,
615 struct ttm_resource_manager *man,
616 const struct ttm_place *place,
617 struct ttm_operation_ctx *ctx,
618 struct ww_acquire_ctx *ticket)
620 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
621 struct ttm_resource_cursor cursor;
622 struct ttm_resource *res;
626 spin_lock(&bdev->lru_lock);
627 ttm_resource_manager_for_each_res(man, &cursor, res) {
630 if (!ttm_bo_evict_swapout_allowable(res->bo, ctx, place,
632 if (busy && !busy_bo && ticket !=
633 dma_resv_locking_ctx(res->bo->base.resv))
638 if (ttm_bo_get_unless_zero(res->bo)) {
643 dma_resv_unlock(res->bo->base.resv);
647 if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
649 spin_unlock(&bdev->lru_lock);
650 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
657 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
658 ctx->no_wait_gpu, locked);
663 spin_unlock(&bdev->lru_lock);
665 ret = ttm_bo_evict(bo, ctx);
667 ttm_bo_unreserve(bo);
669 ttm_bo_move_to_lru_tail_unlocked(bo);
676 * ttm_bo_pin - Pin the buffer object.
677 * @bo: The buffer object to pin
679 * Make sure the buffer is not evicted any more during memory pressure.
680 * @bo must be unpinned again by calling ttm_bo_unpin().
682 void ttm_bo_pin(struct ttm_buffer_object *bo)
684 dma_resv_assert_held(bo->base.resv);
685 WARN_ON_ONCE(!kref_read(&bo->kref));
686 spin_lock(&bo->bdev->lru_lock);
688 ttm_resource_del_bulk_move(bo->resource, bo);
690 spin_unlock(&bo->bdev->lru_lock);
692 EXPORT_SYMBOL(ttm_bo_pin);
695 * ttm_bo_unpin - Unpin the buffer object.
696 * @bo: The buffer object to unpin
698 * Allows the buffer object to be evicted again during memory pressure.
700 void ttm_bo_unpin(struct ttm_buffer_object *bo)
702 dma_resv_assert_held(bo->base.resv);
703 WARN_ON_ONCE(!kref_read(&bo->kref));
704 if (WARN_ON_ONCE(!bo->pin_count))
707 spin_lock(&bo->bdev->lru_lock);
710 ttm_resource_add_bulk_move(bo->resource, bo);
711 spin_unlock(&bo->bdev->lru_lock);
713 EXPORT_SYMBOL(ttm_bo_unpin);
716 * Add the last move fence to the BO as kernel dependency and reserve a new
719 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
720 struct ttm_resource_manager *man,
721 struct ttm_resource *mem,
724 struct dma_fence *fence;
727 spin_lock(&man->move_lock);
728 fence = dma_fence_get(man->move);
729 spin_unlock(&man->move_lock);
735 ret = dma_fence_is_signaled(fence) ? 0 : -EBUSY;
736 dma_fence_put(fence);
740 dma_resv_add_fence(bo->base.resv, fence, DMA_RESV_USAGE_KERNEL);
742 ret = dma_resv_reserve_fences(bo->base.resv, 1);
743 dma_fence_put(fence);
748 * Repeatedly evict memory from the LRU for @mem_type until we create enough
749 * space, or we've evicted everything and there isn't enough space.
751 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
752 const struct ttm_place *place,
753 struct ttm_resource **mem,
754 struct ttm_operation_ctx *ctx)
756 struct ttm_device *bdev = bo->bdev;
757 struct ttm_resource_manager *man;
758 struct ww_acquire_ctx *ticket;
761 man = ttm_manager_type(bdev, place->mem_type);
762 ticket = dma_resv_locking_ctx(bo->base.resv);
764 ret = ttm_resource_alloc(bo, place, mem);
767 if (unlikely(ret != -ENOSPC))
769 ret = ttm_mem_evict_first(bdev, man, place, ctx,
771 if (unlikely(ret != 0))
775 return ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
779 * Creates space for memory region @mem according to its type.
781 * This function first searches for free space in compatible memory types in
782 * the priority order defined by the driver. If free space isn't found, then
783 * ttm_bo_mem_force_space is attempted in priority order to evict and find
786 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
787 struct ttm_placement *placement,
788 struct ttm_resource **mem,
789 struct ttm_operation_ctx *ctx)
791 struct ttm_device *bdev = bo->bdev;
792 bool type_found = false;
795 ret = dma_resv_reserve_fences(bo->base.resv, 1);
799 for (i = 0; i < placement->num_placement; ++i) {
800 const struct ttm_place *place = &placement->placement[i];
801 struct ttm_resource_manager *man;
803 man = ttm_manager_type(bdev, place->mem_type);
804 if (!man || !ttm_resource_manager_used(man))
808 ret = ttm_resource_alloc(bo, place, mem);
814 ret = ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
816 ttm_resource_free(bo, mem);
825 for (i = 0; i < placement->num_busy_placement; ++i) {
826 const struct ttm_place *place = &placement->busy_placement[i];
827 struct ttm_resource_manager *man;
829 man = ttm_manager_type(bdev, place->mem_type);
830 if (!man || !ttm_resource_manager_used(man))
834 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
838 if (ret && ret != -EBUSY)
844 pr_err(TTM_PFX "No compatible memory type found\n");
851 EXPORT_SYMBOL(ttm_bo_mem_space);
853 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
854 struct ttm_placement *placement,
855 struct ttm_operation_ctx *ctx)
857 struct ttm_resource *mem;
858 struct ttm_place hop;
861 dma_resv_assert_held(bo->base.resv);
864 * Determine where to move the buffer.
866 * If driver determines move is going to need
867 * an extra step then it will return -EMULTIHOP
868 * and the buffer will be moved to the temporary
869 * stop and the driver will be called to make
872 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
876 ret = ttm_bo_handle_move_mem(bo, mem, false, ctx, &hop);
877 if (ret == -EMULTIHOP) {
878 ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
881 /* try and move to final place now. */
886 ttm_resource_free(bo, &mem);
890 int ttm_bo_validate(struct ttm_buffer_object *bo,
891 struct ttm_placement *placement,
892 struct ttm_operation_ctx *ctx)
896 dma_resv_assert_held(bo->base.resv);
899 * Remove the backing store if no placement is given.
901 if (!placement->num_placement && !placement->num_busy_placement)
902 return ttm_bo_pipeline_gutting(bo);
905 * Check whether we need to move buffer.
907 if (!bo->resource || !ttm_resource_compat(bo->resource, placement)) {
908 ret = ttm_bo_move_buffer(bo, placement, ctx);
913 * We might need to add a TTM.
915 if (!bo->resource || bo->resource->mem_type == TTM_PL_SYSTEM) {
916 ret = ttm_tt_create(bo, true);
922 EXPORT_SYMBOL(ttm_bo_validate);
925 * ttm_bo_init_reserved
927 * @bdev: Pointer to a ttm_device struct.
928 * @bo: Pointer to a ttm_buffer_object to be initialized.
929 * @type: Requested type of buffer object.
930 * @placement: Initial placement for buffer object.
931 * @alignment: Data alignment in pages.
932 * @ctx: TTM operation context for memory allocation.
933 * @sg: Scatter-gather table.
934 * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
935 * @destroy: Destroy function. Use NULL for kfree().
937 * This function initializes a pre-allocated struct ttm_buffer_object.
938 * As this object may be part of a larger structure, this function,
939 * together with the @destroy function, enables driver-specific objects
940 * derived from a ttm_buffer_object.
942 * On successful return, the caller owns an object kref to @bo. The kref and
943 * list_kref are usually set to 1, but note that in some situations, other
944 * tasks may already be holding references to @bo as well.
945 * Furthermore, if resv == NULL, the buffer's reservation lock will be held,
946 * and it is the caller's responsibility to call ttm_bo_unreserve.
948 * If a failure occurs, the function will call the @destroy function. Thus,
949 * after a failure, dereferencing @bo is illegal and will likely cause memory
953 * -ENOMEM: Out of memory.
954 * -EINVAL: Invalid placement flags.
955 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
957 int ttm_bo_init_reserved(struct ttm_device *bdev, struct ttm_buffer_object *bo,
958 enum ttm_bo_type type, struct ttm_placement *placement,
959 uint32_t alignment, struct ttm_operation_ctx *ctx,
960 struct sg_table *sg, struct dma_resv *resv,
961 void (*destroy) (struct ttm_buffer_object *))
963 static const struct ttm_place sys_mem = { .mem_type = TTM_PL_SYSTEM };
966 kref_init(&bo->kref);
967 INIT_LIST_HEAD(&bo->ddestroy);
970 bo->page_alignment = alignment;
971 bo->destroy = destroy;
974 bo->bulk_move = NULL;
976 bo->base.resv = resv;
978 bo->base.resv = &bo->base._resv;
979 atomic_inc(&ttm_glob.bo_count);
981 ret = ttm_resource_alloc(bo, &sys_mem, &bo->resource);
988 * For ttm_bo_type_device buffers, allocate
989 * address space from the device.
991 if (bo->type == ttm_bo_type_device || bo->type == ttm_bo_type_sg) {
992 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
993 PFN_UP(bo->base.size));
998 /* passed reservation objects should already be locked,
999 * since otherwise lockdep will be angered in radeon.
1002 WARN_ON(!dma_resv_trylock(bo->base.resv));
1004 dma_resv_assert_held(resv);
1006 ret = ttm_bo_validate(bo, placement, ctx);
1014 dma_resv_unlock(bo->base.resv);
1020 EXPORT_SYMBOL(ttm_bo_init_reserved);
1023 * ttm_bo_init_validate
1025 * @bdev: Pointer to a ttm_device struct.
1026 * @bo: Pointer to a ttm_buffer_object to be initialized.
1027 * @type: Requested type of buffer object.
1028 * @placement: Initial placement for buffer object.
1029 * @alignment: Data alignment in pages.
1030 * @interruptible: If needing to sleep to wait for GPU resources,
1031 * sleep interruptible.
1032 * pinned in physical memory. If this behaviour is not desired, this member
1033 * holds a pointer to a persistent shmem object. Typically, this would
1034 * point to the shmem object backing a GEM object if TTM is used to back a
1035 * GEM user interface.
1036 * @sg: Scatter-gather table.
1037 * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
1038 * @destroy: Destroy function. Use NULL for kfree().
1040 * This function initializes a pre-allocated struct ttm_buffer_object.
1041 * As this object may be part of a larger structure, this function,
1042 * together with the @destroy function,
1043 * enables driver-specific objects derived from a ttm_buffer_object.
1045 * On successful return, the caller owns an object kref to @bo. The kref and
1046 * list_kref are usually set to 1, but note that in some situations, other
1047 * tasks may already be holding references to @bo as well.
1049 * If a failure occurs, the function will call the @destroy function, Thus,
1050 * after a failure, dereferencing @bo is illegal and will likely cause memory
1054 * -ENOMEM: Out of memory.
1055 * -EINVAL: Invalid placement flags.
1056 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
1058 int ttm_bo_init_validate(struct ttm_device *bdev, struct ttm_buffer_object *bo,
1059 enum ttm_bo_type type, struct ttm_placement *placement,
1060 uint32_t alignment, bool interruptible,
1061 struct sg_table *sg, struct dma_resv *resv,
1062 void (*destroy) (struct ttm_buffer_object *))
1064 struct ttm_operation_ctx ctx = { interruptible, false };
1067 ret = ttm_bo_init_reserved(bdev, bo, type, placement, alignment, &ctx,
1073 ttm_bo_unreserve(bo);
1077 EXPORT_SYMBOL(ttm_bo_init_validate);
1080 * buffer object vm functions.
1083 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1085 struct ttm_device *bdev = bo->bdev;
1087 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1088 ttm_mem_io_free(bdev, bo->resource);
1090 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1092 int ttm_bo_wait(struct ttm_buffer_object *bo,
1093 bool interruptible, bool no_wait)
1095 long timeout = 15 * HZ;
1098 if (dma_resv_test_signaled(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP))
1104 timeout = dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
1105 interruptible, timeout);
1114 EXPORT_SYMBOL(ttm_bo_wait);
1116 int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
1119 struct ttm_place place;
1124 * While the bo may already reside in SYSTEM placement, set
1125 * SYSTEM as new placement to cover also the move further below.
1126 * The driver may use the fact that we're moving from SYSTEM
1127 * as an indication that we're about to swap out.
1129 memset(&place, 0, sizeof(place));
1130 place.mem_type = bo->resource->mem_type;
1131 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place, &locked, NULL))
1134 if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
1135 bo->ttm->page_flags & TTM_TT_FLAG_EXTERNAL ||
1136 bo->ttm->page_flags & TTM_TT_FLAG_SWAPPED ||
1137 !ttm_bo_get_unless_zero(bo)) {
1139 dma_resv_unlock(bo->base.resv);
1144 ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1146 return ret == -EBUSY ? -ENOSPC : ret;
1149 /* TODO: Cleanup the locking */
1150 spin_unlock(&bo->bdev->lru_lock);
1153 * Move to system cached
1155 if (bo->resource->mem_type != TTM_PL_SYSTEM) {
1156 struct ttm_operation_ctx ctx = { false, false };
1157 struct ttm_resource *evict_mem;
1158 struct ttm_place hop;
1160 memset(&hop, 0, sizeof(hop));
1161 place.mem_type = TTM_PL_SYSTEM;
1162 ret = ttm_resource_alloc(bo, &place, &evict_mem);
1166 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, &ctx, &hop);
1167 if (unlikely(ret != 0)) {
1168 WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
1174 * Make sure BO is idle.
1176 ret = ttm_bo_wait(bo, false, false);
1177 if (unlikely(ret != 0))
1180 ttm_bo_unmap_virtual(bo);
1183 * Swap out. Buffer will be swapped in again as soon as
1184 * anyone tries to access a ttm page.
1186 if (bo->bdev->funcs->swap_notify)
1187 bo->bdev->funcs->swap_notify(bo);
1189 if (ttm_tt_is_populated(bo->ttm))
1190 ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);
1194 * Unreserve without putting on LRU to avoid swapping out an
1195 * already swapped buffer.
1198 dma_resv_unlock(bo->base.resv);
1200 return ret == -EBUSY ? -ENOSPC : ret;
1203 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1205 if (bo->ttm == NULL)
1208 ttm_tt_unpopulate(bo->bdev, bo->ttm);
1209 ttm_tt_destroy(bo->bdev, bo->ttm);