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.h>
35 #include <drm/ttm/ttm_placement.h>
36 #include <drm/ttm/ttm_tt.h>
38 #include <linux/jiffies.h>
39 #include <linux/slab.h>
40 #include <linux/sched.h>
42 #include <linux/file.h>
43 #include <linux/module.h>
44 #include <linux/atomic.h>
45 #include <linux/dma-resv.h>
47 #include "ttm_module.h"
49 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
50 struct ttm_placement *placement)
52 struct drm_printer p = drm_dbg_printer(NULL, DRM_UT_CORE, TTM_PFX);
53 struct ttm_resource_manager *man;
56 for (i = 0; i < placement->num_placement; i++) {
57 mem_type = placement->placement[i].mem_type;
58 drm_printf(&p, " placement[%d]=0x%08X (%d)\n",
59 i, placement->placement[i].flags, mem_type);
60 man = ttm_manager_type(bo->bdev, mem_type);
61 ttm_resource_manager_debug(man, &p);
66 * ttm_bo_move_to_lru_tail
68 * @bo: The buffer object.
70 * Move this BO to the tail of all lru lists used to lookup and reserve an
71 * object. This function must be called with struct ttm_global::lru_lock
72 * held, and is used to make a BO less likely to be considered for eviction.
74 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo)
76 dma_resv_assert_held(bo->base.resv);
79 ttm_resource_move_to_lru_tail(bo->resource);
81 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
84 * ttm_bo_set_bulk_move - update BOs bulk move object
86 * @bo: The buffer object.
87 * @bulk: bulk move structure
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 || ttm_manager_type(bdev, bo->resource->mem_type)->use_tt;
125 new_use_tt = ttm_manager_type(bdev, mem->mem_type)->use_tt;
127 ttm_bo_unmap_virtual(bo);
130 * Create and bind a ttm if required.
134 /* Zero init the new TTM structure if the old location should
135 * have used one as well.
137 ret = ttm_tt_create(bo, old_use_tt);
141 if (mem->mem_type != TTM_PL_SYSTEM) {
142 ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
148 ret = dma_resv_reserve_fences(bo->base.resv, 1);
152 ret = bdev->funcs->move(bo, evict, ctx, mem, hop);
154 if (ret == -EMULTIHOP)
159 ctx->bytes_moved += bo->base.size;
164 ttm_bo_tt_destroy(bo);
171 * Will release GPU memory type usage on destruction.
172 * This is the place to put in driver specific hooks to release
173 * driver private resources.
174 * Will release the bo::reserved lock.
177 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
179 if (bo->bdev->funcs->delete_mem_notify)
180 bo->bdev->funcs->delete_mem_notify(bo);
182 ttm_bo_tt_destroy(bo);
183 ttm_resource_free(bo, &bo->resource);
186 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
190 if (bo->base.resv == &bo->base._resv)
193 BUG_ON(!dma_resv_trylock(&bo->base._resv));
195 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
196 dma_resv_unlock(&bo->base._resv);
200 if (bo->type != ttm_bo_type_sg) {
201 /* This works because the BO is about to be destroyed and nobody
202 * reference it any more. The only tricky case is the trylock on
203 * the resv object while holding the lru_lock.
205 spin_lock(&bo->bdev->lru_lock);
206 bo->base.resv = &bo->base._resv;
207 spin_unlock(&bo->bdev->lru_lock);
213 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
215 struct dma_resv *resv = &bo->base._resv;
216 struct dma_resv_iter cursor;
217 struct dma_fence *fence;
219 dma_resv_iter_begin(&cursor, resv, DMA_RESV_USAGE_BOOKKEEP);
220 dma_resv_for_each_fence_unlocked(&cursor, fence) {
221 if (!fence->ops->signaled)
222 dma_fence_enable_sw_signaling(fence);
224 dma_resv_iter_end(&cursor);
228 * ttm_bo_cleanup_refs
229 * If bo idle, remove from lru lists, and unref.
230 * If not idle, block if possible.
232 * Must be called with lru_lock and reservation held, this function
233 * will drop the lru lock and optionally the reservation lock before returning.
235 * @bo: The buffer object to clean-up
236 * @interruptible: Any sleeps should occur interruptibly.
237 * @no_wait_gpu: Never wait for gpu. Return -EBUSY instead.
238 * @unlock_resv: Unlock the reservation lock as well.
241 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
242 bool interruptible, bool no_wait_gpu,
245 struct dma_resv *resv = &bo->base._resv;
248 if (dma_resv_test_signaled(resv, DMA_RESV_USAGE_BOOKKEEP))
253 if (ret && !no_wait_gpu) {
257 dma_resv_unlock(bo->base.resv);
258 spin_unlock(&bo->bdev->lru_lock);
260 lret = dma_resv_wait_timeout(resv, DMA_RESV_USAGE_BOOKKEEP,
269 spin_lock(&bo->bdev->lru_lock);
270 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
272 * We raced, and lost, someone else holds the reservation now,
273 * and is probably busy in ttm_bo_cleanup_memtype_use.
275 * Even if it's not the case, because we finished waiting any
276 * delayed destruction would succeed, so just return success
279 spin_unlock(&bo->bdev->lru_lock);
287 dma_resv_unlock(bo->base.resv);
288 spin_unlock(&bo->bdev->lru_lock);
292 spin_unlock(&bo->bdev->lru_lock);
293 ttm_bo_cleanup_memtype_use(bo);
296 dma_resv_unlock(bo->base.resv);
302 * Block for the dma_resv object to become idle, lock the buffer and clean up
303 * the resource and tt object.
305 static void ttm_bo_delayed_delete(struct work_struct *work)
307 struct ttm_buffer_object *bo;
309 bo = container_of(work, typeof(*bo), delayed_delete);
311 dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP, false,
312 MAX_SCHEDULE_TIMEOUT);
313 dma_resv_lock(bo->base.resv, NULL);
314 ttm_bo_cleanup_memtype_use(bo);
315 dma_resv_unlock(bo->base.resv);
319 static void ttm_bo_release(struct kref *kref)
321 struct ttm_buffer_object *bo =
322 container_of(kref, struct ttm_buffer_object, kref);
323 struct ttm_device *bdev = bo->bdev;
326 WARN_ON_ONCE(bo->pin_count);
327 WARN_ON_ONCE(bo->bulk_move);
330 ret = ttm_bo_individualize_resv(bo);
332 /* Last resort, if we fail to allocate memory for the
333 * fences block for the BO to become idle
335 dma_resv_wait_timeout(bo->base.resv,
336 DMA_RESV_USAGE_BOOKKEEP, false,
340 if (bo->bdev->funcs->release_notify)
341 bo->bdev->funcs->release_notify(bo);
343 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
344 ttm_mem_io_free(bdev, bo->resource);
346 if (!dma_resv_test_signaled(bo->base.resv,
347 DMA_RESV_USAGE_BOOKKEEP) ||
348 (want_init_on_free() && (bo->ttm != NULL)) ||
349 !dma_resv_trylock(bo->base.resv)) {
350 /* The BO is not idle, resurrect it for delayed destroy */
351 ttm_bo_flush_all_fences(bo);
354 spin_lock(&bo->bdev->lru_lock);
357 * Make pinned bos immediately available to
358 * shrinkers, now that they are queued for
361 * FIXME: QXL is triggering this. Can be removed when the
366 ttm_resource_move_to_lru_tail(bo->resource);
369 kref_init(&bo->kref);
370 spin_unlock(&bo->bdev->lru_lock);
372 INIT_WORK(&bo->delayed_delete, ttm_bo_delayed_delete);
374 /* Schedule the worker on the closest NUMA node. This
375 * improves performance since system memory might be
376 * cleared on free and that is best done on a CPU core
379 queue_work_node(bdev->pool.nid, bdev->wq, &bo->delayed_delete);
383 ttm_bo_cleanup_memtype_use(bo);
384 dma_resv_unlock(bo->base.resv);
387 atomic_dec(&ttm_glob.bo_count);
394 * @bo: The buffer object.
396 * Unreference a buffer object.
398 void ttm_bo_put(struct ttm_buffer_object *bo)
400 kref_put(&bo->kref, ttm_bo_release);
402 EXPORT_SYMBOL(ttm_bo_put);
404 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
405 struct ttm_resource **mem,
406 struct ttm_operation_ctx *ctx,
407 struct ttm_place *hop)
409 struct ttm_placement hop_placement;
410 struct ttm_resource *hop_mem;
413 hop_placement.num_placement = 1;
414 hop_placement.placement = hop;
416 /* find space in the bounce domain */
417 ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
420 /* move to the bounce domain */
421 ret = ttm_bo_handle_move_mem(bo, hop_mem, false, ctx, NULL);
423 ttm_resource_free(bo, &hop_mem);
429 static int ttm_bo_evict(struct ttm_buffer_object *bo,
430 struct ttm_operation_ctx *ctx)
432 struct ttm_device *bdev = bo->bdev;
433 struct ttm_resource *evict_mem;
434 struct ttm_placement placement;
435 struct ttm_place hop;
438 memset(&hop, 0, sizeof(hop));
440 dma_resv_assert_held(bo->base.resv);
442 placement.num_placement = 0;
443 bdev->funcs->evict_flags(bo, &placement);
445 if (!placement.num_placement) {
446 ret = ttm_bo_wait_ctx(bo, ctx);
451 * Since we've already synced, this frees backing store
454 return ttm_bo_pipeline_gutting(bo);
457 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
459 if (ret != -ERESTARTSYS) {
460 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
462 ttm_bo_mem_space_debug(bo, &placement);
468 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
469 if (ret != -EMULTIHOP)
472 ret = ttm_bo_bounce_temp_buffer(bo, &evict_mem, ctx, &hop);
476 ttm_resource_free(bo, &evict_mem);
477 if (ret != -ERESTARTSYS && ret != -EINTR)
478 pr_err("Buffer eviction failed\n");
485 * ttm_bo_eviction_valuable
487 * @bo: The buffer object to evict
488 * @place: the placement we need to make room for
490 * Check if it is valuable to evict the BO to make room for the given placement.
492 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
493 const struct ttm_place *place)
495 struct ttm_resource *res = bo->resource;
496 struct ttm_device *bdev = bo->bdev;
498 dma_resv_assert_held(bo->base.resv);
499 if (bo->resource->mem_type == TTM_PL_SYSTEM)
502 /* Don't evict this BO if it's outside of the
503 * requested placement range
505 return ttm_resource_intersects(bdev, res, place, bo->base.size);
507 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
510 * Check the target bo is allowable to be evicted or swapout, including cases:
512 * a. if share same reservation object with ctx->resv, have assumption
513 * reservation objects should already be locked, so not lock again and
514 * return true directly when either the opreation allow_reserved_eviction
515 * or the target bo already is in delayed free list;
517 * b. Otherwise, trylock it.
519 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
520 struct ttm_operation_ctx *ctx,
521 const struct ttm_place *place,
522 bool *locked, bool *busy)
533 if (bo->base.resv == ctx->resv) {
534 dma_resv_assert_held(bo->base.resv);
535 if (ctx->allow_res_evict)
541 ret = dma_resv_trylock(bo->base.resv);
547 if (ret && place && (bo->resource->mem_type != place->mem_type ||
548 !bo->bdev->funcs->eviction_valuable(bo, place))) {
551 dma_resv_unlock(bo->base.resv);
560 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
562 * @busy_bo: BO which couldn't be locked with trylock
563 * @ctx: operation context
564 * @ticket: acquire ticket
566 * Try to lock a busy buffer object to avoid failing eviction.
568 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
569 struct ttm_operation_ctx *ctx,
570 struct ww_acquire_ctx *ticket)
574 if (!busy_bo || !ticket)
577 if (ctx->interruptible)
578 r = dma_resv_lock_interruptible(busy_bo->base.resv,
581 r = dma_resv_lock(busy_bo->base.resv, ticket);
584 * TODO: It would be better to keep the BO locked until allocation is at
585 * least tried one more time, but that would mean a much larger rework
589 dma_resv_unlock(busy_bo->base.resv);
591 return r == -EDEADLK ? -EBUSY : r;
594 int ttm_mem_evict_first(struct ttm_device *bdev,
595 struct ttm_resource_manager *man,
596 const struct ttm_place *place,
597 struct ttm_operation_ctx *ctx,
598 struct ww_acquire_ctx *ticket)
600 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
601 struct ttm_resource_cursor cursor;
602 struct ttm_resource *res;
606 spin_lock(&bdev->lru_lock);
607 ttm_resource_manager_for_each_res(man, &cursor, res) {
610 if (!ttm_bo_evict_swapout_allowable(res->bo, ctx, place,
612 if (busy && !busy_bo && ticket !=
613 dma_resv_locking_ctx(res->bo->base.resv))
618 if (ttm_bo_get_unless_zero(res->bo)) {
623 dma_resv_unlock(res->bo->base.resv);
627 if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
629 spin_unlock(&bdev->lru_lock);
630 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
637 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
638 ctx->no_wait_gpu, locked);
643 spin_unlock(&bdev->lru_lock);
645 ret = ttm_bo_evict(bo, ctx);
647 ttm_bo_unreserve(bo);
649 ttm_bo_move_to_lru_tail_unlocked(bo);
656 * ttm_bo_pin - Pin the buffer object.
657 * @bo: The buffer object to pin
659 * Make sure the buffer is not evicted any more during memory pressure.
660 * @bo must be unpinned again by calling ttm_bo_unpin().
662 void ttm_bo_pin(struct ttm_buffer_object *bo)
664 dma_resv_assert_held(bo->base.resv);
665 WARN_ON_ONCE(!kref_read(&bo->kref));
666 spin_lock(&bo->bdev->lru_lock);
668 ttm_resource_del_bulk_move(bo->resource, bo);
670 spin_unlock(&bo->bdev->lru_lock);
672 EXPORT_SYMBOL(ttm_bo_pin);
675 * ttm_bo_unpin - Unpin the buffer object.
676 * @bo: The buffer object to unpin
678 * Allows the buffer object to be evicted again during memory pressure.
680 void ttm_bo_unpin(struct ttm_buffer_object *bo)
682 dma_resv_assert_held(bo->base.resv);
683 WARN_ON_ONCE(!kref_read(&bo->kref));
684 if (WARN_ON_ONCE(!bo->pin_count))
687 spin_lock(&bo->bdev->lru_lock);
690 ttm_resource_add_bulk_move(bo->resource, bo);
691 spin_unlock(&bo->bdev->lru_lock);
693 EXPORT_SYMBOL(ttm_bo_unpin);
696 * Add the last move fence to the BO as kernel dependency and reserve a new
699 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
700 struct ttm_resource_manager *man,
701 struct ttm_resource *mem,
704 struct dma_fence *fence;
707 spin_lock(&man->move_lock);
708 fence = dma_fence_get(man->move);
709 spin_unlock(&man->move_lock);
715 ret = dma_fence_is_signaled(fence) ? 0 : -EBUSY;
716 dma_fence_put(fence);
720 dma_resv_add_fence(bo->base.resv, fence, DMA_RESV_USAGE_KERNEL);
722 ret = dma_resv_reserve_fences(bo->base.resv, 1);
723 dma_fence_put(fence);
728 * Repeatedly evict memory from the LRU for @mem_type until we create enough
729 * space, or we've evicted everything and there isn't enough space.
731 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
732 const struct ttm_place *place,
733 struct ttm_resource **mem,
734 struct ttm_operation_ctx *ctx)
736 struct ttm_device *bdev = bo->bdev;
737 struct ttm_resource_manager *man;
738 struct ww_acquire_ctx *ticket;
741 man = ttm_manager_type(bdev, place->mem_type);
742 ticket = dma_resv_locking_ctx(bo->base.resv);
744 ret = ttm_resource_alloc(bo, place, mem);
747 if (unlikely(ret != -ENOSPC))
749 ret = ttm_mem_evict_first(bdev, man, place, ctx,
751 if (unlikely(ret != 0))
755 return ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
761 * @bo: Pointer to a struct ttm_buffer_object. the data of which
762 * we want to allocate space for.
763 * @placement: Proposed new placement for the buffer object.
764 * @mem: A struct ttm_resource.
765 * @ctx: if and how to sleep, lock buffers and alloc memory
767 * Allocate memory space for the buffer object pointed to by @bo, using
768 * the placement flags in @placement, potentially evicting other idle buffer objects.
769 * This function may sleep while waiting for space to become available.
771 * -EBUSY: No space available (only if no_wait == 1).
772 * -ENOSPC: Could not allocate space for the buffer object, either due to
773 * fragmentation or concurrent allocators.
774 * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
776 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
777 struct ttm_placement *placement,
778 struct ttm_resource **mem,
779 struct ttm_operation_ctx *ctx)
781 struct ttm_device *bdev = bo->bdev;
782 bool type_found = false;
785 ret = dma_resv_reserve_fences(bo->base.resv, 1);
789 for (i = 0; i < placement->num_placement; ++i) {
790 const struct ttm_place *place = &placement->placement[i];
791 struct ttm_resource_manager *man;
793 if (place->flags & TTM_PL_FLAG_FALLBACK)
796 man = ttm_manager_type(bdev, place->mem_type);
797 if (!man || !ttm_resource_manager_used(man))
801 ret = ttm_resource_alloc(bo, place, mem);
807 ret = ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
809 ttm_resource_free(bo, mem);
818 for (i = 0; i < placement->num_placement; ++i) {
819 const struct ttm_place *place = &placement->placement[i];
820 struct ttm_resource_manager *man;
822 if (place->flags & TTM_PL_FLAG_DESIRED)
825 man = ttm_manager_type(bdev, place->mem_type);
826 if (!man || !ttm_resource_manager_used(man))
830 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
834 if (ret && ret != -EBUSY)
840 pr_err(TTM_PFX "No compatible memory type found\n");
847 EXPORT_SYMBOL(ttm_bo_mem_space);
849 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
850 struct ttm_placement *placement,
851 struct ttm_operation_ctx *ctx)
853 struct ttm_resource *mem;
854 struct ttm_place hop;
857 dma_resv_assert_held(bo->base.resv);
860 * Determine where to move the buffer.
862 * If driver determines move is going to need
863 * an extra step then it will return -EMULTIHOP
864 * and the buffer will be moved to the temporary
865 * stop and the driver will be called to make
868 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
872 ret = ttm_bo_handle_move_mem(bo, mem, false, ctx, &hop);
873 if (ret == -EMULTIHOP) {
874 ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
877 /* try and move to final place now. */
882 ttm_resource_free(bo, &mem);
889 * @bo: The buffer object.
890 * @placement: Proposed placement for the buffer object.
891 * @ctx: validation parameters.
893 * Changes placement and caching policy of the buffer object
894 * according proposed placement.
896 * -EINVAL on invalid proposed placement.
897 * -ENOMEM on out-of-memory condition.
898 * -EBUSY if no_wait is true and buffer busy.
899 * -ERESTARTSYS if interrupted by a signal.
901 int ttm_bo_validate(struct ttm_buffer_object *bo,
902 struct ttm_placement *placement,
903 struct ttm_operation_ctx *ctx)
907 dma_resv_assert_held(bo->base.resv);
910 * Remove the backing store if no placement is given.
912 if (!placement->num_placement)
913 return ttm_bo_pipeline_gutting(bo);
915 /* Check whether we need to move buffer. */
916 if (bo->resource && ttm_resource_compatible(bo->resource, placement))
919 /* Moving of pinned BOs is forbidden */
923 ret = ttm_bo_move_buffer(bo, placement, ctx);
924 /* For backward compatibility with userspace */
931 * We might need to add a TTM.
933 if (!bo->resource || bo->resource->mem_type == TTM_PL_SYSTEM) {
934 ret = ttm_tt_create(bo, true);
940 EXPORT_SYMBOL(ttm_bo_validate);
943 * ttm_bo_init_reserved
945 * @bdev: Pointer to a ttm_device struct.
946 * @bo: Pointer to a ttm_buffer_object to be initialized.
947 * @type: Requested type of buffer object.
948 * @placement: Initial placement for buffer object.
949 * @alignment: Data alignment in pages.
950 * @ctx: TTM operation context for memory allocation.
951 * @sg: Scatter-gather table.
952 * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
953 * @destroy: Destroy function. Use NULL for kfree().
955 * This function initializes a pre-allocated struct ttm_buffer_object.
956 * As this object may be part of a larger structure, this function,
957 * together with the @destroy function, enables driver-specific objects
958 * derived from a ttm_buffer_object.
960 * On successful return, the caller owns an object kref to @bo. The kref and
961 * list_kref are usually set to 1, but note that in some situations, other
962 * tasks may already be holding references to @bo as well.
963 * Furthermore, if resv == NULL, the buffer's reservation lock will be held,
964 * and it is the caller's responsibility to call ttm_bo_unreserve.
966 * If a failure occurs, the function will call the @destroy function. Thus,
967 * after a failure, dereferencing @bo is illegal and will likely cause memory
971 * -ENOMEM: Out of memory.
972 * -EINVAL: Invalid placement flags.
973 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
975 int ttm_bo_init_reserved(struct ttm_device *bdev, struct ttm_buffer_object *bo,
976 enum ttm_bo_type type, struct ttm_placement *placement,
977 uint32_t alignment, struct ttm_operation_ctx *ctx,
978 struct sg_table *sg, struct dma_resv *resv,
979 void (*destroy) (struct ttm_buffer_object *))
983 kref_init(&bo->kref);
986 bo->page_alignment = alignment;
987 bo->destroy = destroy;
990 bo->bulk_move = NULL;
992 bo->base.resv = resv;
994 bo->base.resv = &bo->base._resv;
995 atomic_inc(&ttm_glob.bo_count);
998 * For ttm_bo_type_device buffers, allocate
999 * address space from the device.
1001 if (bo->type == ttm_bo_type_device || bo->type == ttm_bo_type_sg) {
1002 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
1003 PFN_UP(bo->base.size));
1008 /* passed reservation objects should already be locked,
1009 * since otherwise lockdep will be angered in radeon.
1012 WARN_ON(!dma_resv_trylock(bo->base.resv));
1014 dma_resv_assert_held(resv);
1016 ret = ttm_bo_validate(bo, placement, ctx);
1024 dma_resv_unlock(bo->base.resv);
1030 EXPORT_SYMBOL(ttm_bo_init_reserved);
1033 * ttm_bo_init_validate
1035 * @bdev: Pointer to a ttm_device struct.
1036 * @bo: Pointer to a ttm_buffer_object to be initialized.
1037 * @type: Requested type of buffer object.
1038 * @placement: Initial placement for buffer object.
1039 * @alignment: Data alignment in pages.
1040 * @interruptible: If needing to sleep to wait for GPU resources,
1041 * sleep interruptible.
1042 * pinned in physical memory. If this behaviour is not desired, this member
1043 * holds a pointer to a persistent shmem object. Typically, this would
1044 * point to the shmem object backing a GEM object if TTM is used to back a
1045 * GEM user interface.
1046 * @sg: Scatter-gather table.
1047 * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
1048 * @destroy: Destroy function. Use NULL for kfree().
1050 * This function initializes a pre-allocated struct ttm_buffer_object.
1051 * As this object may be part of a larger structure, this function,
1052 * together with the @destroy function,
1053 * enables driver-specific objects derived from a ttm_buffer_object.
1055 * On successful return, the caller owns an object kref to @bo. The kref and
1056 * list_kref are usually set to 1, but note that in some situations, other
1057 * tasks may already be holding references to @bo as well.
1059 * If a failure occurs, the function will call the @destroy function, Thus,
1060 * after a failure, dereferencing @bo is illegal and will likely cause memory
1064 * -ENOMEM: Out of memory.
1065 * -EINVAL: Invalid placement flags.
1066 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
1068 int ttm_bo_init_validate(struct ttm_device *bdev, struct ttm_buffer_object *bo,
1069 enum ttm_bo_type type, struct ttm_placement *placement,
1070 uint32_t alignment, bool interruptible,
1071 struct sg_table *sg, struct dma_resv *resv,
1072 void (*destroy) (struct ttm_buffer_object *))
1074 struct ttm_operation_ctx ctx = { interruptible, false };
1077 ret = ttm_bo_init_reserved(bdev, bo, type, placement, alignment, &ctx,
1083 ttm_bo_unreserve(bo);
1087 EXPORT_SYMBOL(ttm_bo_init_validate);
1090 * buffer object vm functions.
1094 * ttm_bo_unmap_virtual
1096 * @bo: tear down the virtual mappings for this BO
1098 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1100 struct ttm_device *bdev = bo->bdev;
1102 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1103 ttm_mem_io_free(bdev, bo->resource);
1105 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1108 * ttm_bo_wait_ctx - wait for buffer idle.
1110 * @bo: The buffer object.
1111 * @ctx: defines how to wait
1113 * Waits for the buffer to be idle. Used timeout depends on the context.
1114 * Returns -EBUSY if wait timed outt, -ERESTARTSYS if interrupted by a signal or
1117 int ttm_bo_wait_ctx(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx)
1121 if (ctx->no_wait_gpu) {
1122 if (dma_resv_test_signaled(bo->base.resv,
1123 DMA_RESV_USAGE_BOOKKEEP))
1129 ret = dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
1130 ctx->interruptible, 15 * HZ);
1131 if (unlikely(ret < 0))
1133 if (unlikely(ret == 0))
1137 EXPORT_SYMBOL(ttm_bo_wait_ctx);
1139 int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
1142 struct ttm_place place;
1147 * While the bo may already reside in SYSTEM placement, set
1148 * SYSTEM as new placement to cover also the move further below.
1149 * The driver may use the fact that we're moving from SYSTEM
1150 * as an indication that we're about to swap out.
1152 memset(&place, 0, sizeof(place));
1153 place.mem_type = bo->resource->mem_type;
1154 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place, &locked, NULL))
1157 if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
1158 bo->ttm->page_flags & TTM_TT_FLAG_EXTERNAL ||
1159 bo->ttm->page_flags & TTM_TT_FLAG_SWAPPED ||
1160 !ttm_bo_get_unless_zero(bo)) {
1162 dma_resv_unlock(bo->base.resv);
1167 ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1169 return ret == -EBUSY ? -ENOSPC : ret;
1172 /* TODO: Cleanup the locking */
1173 spin_unlock(&bo->bdev->lru_lock);
1176 * Move to system cached
1178 if (bo->resource->mem_type != TTM_PL_SYSTEM) {
1179 struct ttm_resource *evict_mem;
1180 struct ttm_place hop;
1182 memset(&hop, 0, sizeof(hop));
1183 place.mem_type = TTM_PL_SYSTEM;
1184 ret = ttm_resource_alloc(bo, &place, &evict_mem);
1188 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
1189 if (unlikely(ret != 0)) {
1190 WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
1191 ttm_resource_free(bo, &evict_mem);
1197 * Make sure BO is idle.
1199 ret = ttm_bo_wait_ctx(bo, ctx);
1200 if (unlikely(ret != 0))
1203 ttm_bo_unmap_virtual(bo);
1206 * Swap out. Buffer will be swapped in again as soon as
1207 * anyone tries to access a ttm page.
1209 if (bo->bdev->funcs->swap_notify)
1210 bo->bdev->funcs->swap_notify(bo);
1212 if (ttm_tt_is_populated(bo->ttm))
1213 ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);
1217 * Unreserve without putting on LRU to avoid swapping out an
1218 * already swapped buffer.
1221 dma_resv_unlock(bo->base.resv);
1223 return ret == -EBUSY ? -ENOSPC : ret;
1226 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1228 if (bo->ttm == NULL)
1231 ttm_tt_unpopulate(bo->bdev, bo->ttm);
1232 ttm_tt_destroy(bo->bdev, bo->ttm);