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_debug_printer(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.
88 * Update the BOs bulk move object, making sure that resources are added/removed
89 * as well. A bulk move allows to move many resource on the LRU at once,
90 * resulting in much less overhead of maintaining the LRU.
91 * The only requirement is that the resources stay together on the LRU and are
92 * never separated. This is enforces by setting the bulk_move structure on a BO.
93 * ttm_lru_bulk_move_tail() should be used to move all resources to the tail of
96 void ttm_bo_set_bulk_move(struct ttm_buffer_object *bo,
97 struct ttm_lru_bulk_move *bulk)
99 dma_resv_assert_held(bo->base.resv);
101 if (bo->bulk_move == bulk)
104 spin_lock(&bo->bdev->lru_lock);
106 ttm_resource_del_bulk_move(bo->resource, bo);
107 bo->bulk_move = bulk;
109 ttm_resource_add_bulk_move(bo->resource, bo);
110 spin_unlock(&bo->bdev->lru_lock);
112 EXPORT_SYMBOL(ttm_bo_set_bulk_move);
114 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
115 struct ttm_resource *mem, bool evict,
116 struct ttm_operation_ctx *ctx,
117 struct ttm_place *hop)
119 struct ttm_device *bdev = bo->bdev;
120 bool old_use_tt, new_use_tt;
123 old_use_tt = bo->resource &&
124 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 !dma_resv_trylock(bo->base.resv)) {
349 /* The BO is not idle, resurrect it for delayed destroy */
350 ttm_bo_flush_all_fences(bo);
353 spin_lock(&bo->bdev->lru_lock);
356 * Make pinned bos immediately available to
357 * shrinkers, now that they are queued for
360 * FIXME: QXL is triggering this. Can be removed when the
365 ttm_resource_move_to_lru_tail(bo->resource);
368 kref_init(&bo->kref);
369 spin_unlock(&bo->bdev->lru_lock);
371 INIT_WORK(&bo->delayed_delete, ttm_bo_delayed_delete);
372 queue_work(bdev->wq, &bo->delayed_delete);
376 ttm_bo_cleanup_memtype_use(bo);
377 dma_resv_unlock(bo->base.resv);
380 atomic_dec(&ttm_glob.bo_count);
387 * @bo: The buffer object.
389 * Unreference a buffer object.
391 void ttm_bo_put(struct ttm_buffer_object *bo)
393 kref_put(&bo->kref, ttm_bo_release);
395 EXPORT_SYMBOL(ttm_bo_put);
397 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
398 struct ttm_resource **mem,
399 struct ttm_operation_ctx *ctx,
400 struct ttm_place *hop)
402 struct ttm_placement hop_placement;
403 struct ttm_resource *hop_mem;
406 hop_placement.num_placement = hop_placement.num_busy_placement = 1;
407 hop_placement.placement = hop_placement.busy_placement = hop;
409 /* find space in the bounce domain */
410 ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
413 /* move to the bounce domain */
414 ret = ttm_bo_handle_move_mem(bo, hop_mem, false, ctx, NULL);
416 ttm_resource_free(bo, &hop_mem);
422 static int ttm_bo_evict(struct ttm_buffer_object *bo,
423 struct ttm_operation_ctx *ctx)
425 struct ttm_device *bdev = bo->bdev;
426 struct ttm_resource *evict_mem;
427 struct ttm_placement placement;
428 struct ttm_place hop;
431 memset(&hop, 0, sizeof(hop));
433 dma_resv_assert_held(bo->base.resv);
435 placement.num_placement = 0;
436 placement.num_busy_placement = 0;
437 bdev->funcs->evict_flags(bo, &placement);
439 if (!placement.num_placement && !placement.num_busy_placement) {
440 ret = ttm_bo_wait_ctx(bo, ctx);
445 * Since we've already synced, this frees backing store
448 return ttm_bo_pipeline_gutting(bo);
451 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
453 if (ret != -ERESTARTSYS) {
454 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
456 ttm_bo_mem_space_debug(bo, &placement);
462 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
463 if (ret == -EMULTIHOP) {
464 ret = ttm_bo_bounce_temp_buffer(bo, &evict_mem, ctx, &hop);
466 pr_err("Buffer eviction failed\n");
467 ttm_resource_free(bo, &evict_mem);
470 /* try and move to final place now. */
478 * ttm_bo_eviction_valuable
480 * @bo: The buffer object to evict
481 * @place: the placement we need to make room for
483 * Check if it is valuable to evict the BO to make room for the given placement.
485 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
486 const struct ttm_place *place)
488 struct ttm_resource *res = bo->resource;
489 struct ttm_device *bdev = bo->bdev;
491 dma_resv_assert_held(bo->base.resv);
492 if (bo->resource->mem_type == TTM_PL_SYSTEM)
495 /* Don't evict this BO if it's outside of the
496 * requested placement range
498 return ttm_resource_intersects(bdev, res, place, bo->base.size);
500 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
503 * Check the target bo is allowable to be evicted or swapout, including cases:
505 * a. if share same reservation object with ctx->resv, have assumption
506 * reservation objects should already be locked, so not lock again and
507 * return true directly when either the opreation allow_reserved_eviction
508 * or the target bo already is in delayed free list;
510 * b. Otherwise, trylock it.
512 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
513 struct ttm_operation_ctx *ctx,
514 const struct ttm_place *place,
515 bool *locked, bool *busy)
519 if (bo->base.resv == ctx->resv) {
520 dma_resv_assert_held(bo->base.resv);
521 if (ctx->allow_res_evict)
527 ret = dma_resv_trylock(bo->base.resv);
533 if (ret && place && (bo->resource->mem_type != place->mem_type ||
534 !bo->bdev->funcs->eviction_valuable(bo, place))) {
537 dma_resv_unlock(bo->base.resv);
546 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
548 * @busy_bo: BO which couldn't be locked with trylock
549 * @ctx: operation context
550 * @ticket: acquire ticket
552 * Try to lock a busy buffer object to avoid failing eviction.
554 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
555 struct ttm_operation_ctx *ctx,
556 struct ww_acquire_ctx *ticket)
560 if (!busy_bo || !ticket)
563 if (ctx->interruptible)
564 r = dma_resv_lock_interruptible(busy_bo->base.resv,
567 r = dma_resv_lock(busy_bo->base.resv, ticket);
570 * TODO: It would be better to keep the BO locked until allocation is at
571 * least tried one more time, but that would mean a much larger rework
575 dma_resv_unlock(busy_bo->base.resv);
577 return r == -EDEADLK ? -EBUSY : r;
580 int ttm_mem_evict_first(struct ttm_device *bdev,
581 struct ttm_resource_manager *man,
582 const struct ttm_place *place,
583 struct ttm_operation_ctx *ctx,
584 struct ww_acquire_ctx *ticket)
586 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
587 struct ttm_resource_cursor cursor;
588 struct ttm_resource *res;
592 spin_lock(&bdev->lru_lock);
593 ttm_resource_manager_for_each_res(man, &cursor, res) {
596 if (!ttm_bo_evict_swapout_allowable(res->bo, ctx, place,
598 if (busy && !busy_bo && ticket !=
599 dma_resv_locking_ctx(res->bo->base.resv))
604 if (ttm_bo_get_unless_zero(res->bo)) {
609 dma_resv_unlock(res->bo->base.resv);
613 if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
615 spin_unlock(&bdev->lru_lock);
616 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
623 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
624 ctx->no_wait_gpu, locked);
629 spin_unlock(&bdev->lru_lock);
631 ret = ttm_bo_evict(bo, ctx);
633 ttm_bo_unreserve(bo);
635 ttm_bo_move_to_lru_tail_unlocked(bo);
642 * ttm_bo_pin - Pin the buffer object.
643 * @bo: The buffer object to pin
645 * Make sure the buffer is not evicted any more during memory pressure.
646 * @bo must be unpinned again by calling ttm_bo_unpin().
648 void ttm_bo_pin(struct ttm_buffer_object *bo)
650 dma_resv_assert_held(bo->base.resv);
651 WARN_ON_ONCE(!kref_read(&bo->kref));
652 spin_lock(&bo->bdev->lru_lock);
654 ttm_resource_del_bulk_move(bo->resource, bo);
656 spin_unlock(&bo->bdev->lru_lock);
658 EXPORT_SYMBOL(ttm_bo_pin);
661 * ttm_bo_unpin - Unpin the buffer object.
662 * @bo: The buffer object to unpin
664 * Allows the buffer object to be evicted again during memory pressure.
666 void ttm_bo_unpin(struct ttm_buffer_object *bo)
668 dma_resv_assert_held(bo->base.resv);
669 WARN_ON_ONCE(!kref_read(&bo->kref));
670 if (WARN_ON_ONCE(!bo->pin_count))
673 spin_lock(&bo->bdev->lru_lock);
676 ttm_resource_add_bulk_move(bo->resource, bo);
677 spin_unlock(&bo->bdev->lru_lock);
679 EXPORT_SYMBOL(ttm_bo_unpin);
682 * Add the last move fence to the BO as kernel dependency and reserve a new
685 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
686 struct ttm_resource_manager *man,
687 struct ttm_resource *mem,
690 struct dma_fence *fence;
693 spin_lock(&man->move_lock);
694 fence = dma_fence_get(man->move);
695 spin_unlock(&man->move_lock);
701 ret = dma_fence_is_signaled(fence) ? 0 : -EBUSY;
702 dma_fence_put(fence);
706 dma_resv_add_fence(bo->base.resv, fence, DMA_RESV_USAGE_KERNEL);
708 ret = dma_resv_reserve_fences(bo->base.resv, 1);
709 dma_fence_put(fence);
714 * Repeatedly evict memory from the LRU for @mem_type until we create enough
715 * space, or we've evicted everything and there isn't enough space.
717 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
718 const struct ttm_place *place,
719 struct ttm_resource **mem,
720 struct ttm_operation_ctx *ctx)
722 struct ttm_device *bdev = bo->bdev;
723 struct ttm_resource_manager *man;
724 struct ww_acquire_ctx *ticket;
727 man = ttm_manager_type(bdev, place->mem_type);
728 ticket = dma_resv_locking_ctx(bo->base.resv);
730 ret = ttm_resource_alloc(bo, place, mem);
733 if (unlikely(ret != -ENOSPC))
735 ret = ttm_mem_evict_first(bdev, man, place, ctx,
737 if (unlikely(ret != 0))
741 return ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
747 * @bo: Pointer to a struct ttm_buffer_object. the data of which
748 * we want to allocate space for.
749 * @proposed_placement: Proposed new placement for the buffer object.
750 * @mem: A struct ttm_resource.
751 * @ctx: if and how to sleep, lock buffers and alloc memory
753 * Allocate memory space for the buffer object pointed to by @bo, using
754 * the placement flags in @placement, potentially evicting other idle buffer objects.
755 * This function may sleep while waiting for space to become available.
757 * -EBUSY: No space available (only if no_wait == 1).
758 * -ENOMEM: Could not allocate memory for the buffer object, either due to
759 * fragmentation or concurrent allocators.
760 * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
762 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
763 struct ttm_placement *placement,
764 struct ttm_resource **mem,
765 struct ttm_operation_ctx *ctx)
767 struct ttm_device *bdev = bo->bdev;
768 bool type_found = false;
771 ret = dma_resv_reserve_fences(bo->base.resv, 1);
775 for (i = 0; i < placement->num_placement; ++i) {
776 const struct ttm_place *place = &placement->placement[i];
777 struct ttm_resource_manager *man;
779 man = ttm_manager_type(bdev, place->mem_type);
780 if (!man || !ttm_resource_manager_used(man))
784 ret = ttm_resource_alloc(bo, place, mem);
790 ret = ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
792 ttm_resource_free(bo, mem);
801 for (i = 0; i < placement->num_busy_placement; ++i) {
802 const struct ttm_place *place = &placement->busy_placement[i];
803 struct ttm_resource_manager *man;
805 man = ttm_manager_type(bdev, place->mem_type);
806 if (!man || !ttm_resource_manager_used(man))
810 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
814 if (ret && ret != -EBUSY)
820 pr_err(TTM_PFX "No compatible memory type found\n");
827 EXPORT_SYMBOL(ttm_bo_mem_space);
829 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
830 struct ttm_placement *placement,
831 struct ttm_operation_ctx *ctx)
833 struct ttm_resource *mem;
834 struct ttm_place hop;
837 dma_resv_assert_held(bo->base.resv);
840 * Determine where to move the buffer.
842 * If driver determines move is going to need
843 * an extra step then it will return -EMULTIHOP
844 * and the buffer will be moved to the temporary
845 * stop and the driver will be called to make
848 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
852 ret = ttm_bo_handle_move_mem(bo, mem, false, ctx, &hop);
853 if (ret == -EMULTIHOP) {
854 ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
857 /* try and move to final place now. */
862 ttm_resource_free(bo, &mem);
869 * @bo: The buffer object.
870 * @placement: Proposed placement for the buffer object.
871 * @ctx: validation parameters.
873 * Changes placement and caching policy of the buffer object
874 * according proposed placement.
876 * -EINVAL on invalid proposed placement.
877 * -ENOMEM on out-of-memory condition.
878 * -EBUSY if no_wait is true and buffer busy.
879 * -ERESTARTSYS if interrupted by a signal.
881 int ttm_bo_validate(struct ttm_buffer_object *bo,
882 struct ttm_placement *placement,
883 struct ttm_operation_ctx *ctx)
887 dma_resv_assert_held(bo->base.resv);
890 * Remove the backing store if no placement is given.
892 if (!placement->num_placement && !placement->num_busy_placement)
893 return ttm_bo_pipeline_gutting(bo);
896 * Check whether we need to move buffer.
898 if (!bo->resource || !ttm_resource_compat(bo->resource, placement)) {
899 ret = ttm_bo_move_buffer(bo, placement, ctx);
904 * We might need to add a TTM.
906 if (!bo->resource || bo->resource->mem_type == TTM_PL_SYSTEM) {
907 ret = ttm_tt_create(bo, true);
913 EXPORT_SYMBOL(ttm_bo_validate);
916 * ttm_bo_init_reserved
918 * @bdev: Pointer to a ttm_device struct.
919 * @bo: Pointer to a ttm_buffer_object to be initialized.
920 * @type: Requested type of buffer object.
921 * @placement: Initial placement for buffer object.
922 * @alignment: Data alignment in pages.
923 * @ctx: TTM operation context for memory allocation.
924 * @sg: Scatter-gather table.
925 * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
926 * @destroy: Destroy function. Use NULL for kfree().
928 * This function initializes a pre-allocated struct ttm_buffer_object.
929 * As this object may be part of a larger structure, this function,
930 * together with the @destroy function, enables driver-specific objects
931 * derived from a ttm_buffer_object.
933 * On successful return, the caller owns an object kref to @bo. The kref and
934 * list_kref are usually set to 1, but note that in some situations, other
935 * tasks may already be holding references to @bo as well.
936 * Furthermore, if resv == NULL, the buffer's reservation lock will be held,
937 * and it is the caller's responsibility to call ttm_bo_unreserve.
939 * If a failure occurs, the function will call the @destroy function. Thus,
940 * after a failure, dereferencing @bo is illegal and will likely cause memory
944 * -ENOMEM: Out of memory.
945 * -EINVAL: Invalid placement flags.
946 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
948 int ttm_bo_init_reserved(struct ttm_device *bdev, struct ttm_buffer_object *bo,
949 enum ttm_bo_type type, struct ttm_placement *placement,
950 uint32_t alignment, struct ttm_operation_ctx *ctx,
951 struct sg_table *sg, struct dma_resv *resv,
952 void (*destroy) (struct ttm_buffer_object *))
954 static const struct ttm_place sys_mem = { .mem_type = TTM_PL_SYSTEM };
957 kref_init(&bo->kref);
960 bo->page_alignment = alignment;
961 bo->destroy = destroy;
964 bo->bulk_move = NULL;
966 bo->base.resv = resv;
968 bo->base.resv = &bo->base._resv;
969 atomic_inc(&ttm_glob.bo_count);
971 ret = ttm_resource_alloc(bo, &sys_mem, &bo->resource);
978 * For ttm_bo_type_device buffers, allocate
979 * address space from the device.
981 if (bo->type == ttm_bo_type_device || bo->type == ttm_bo_type_sg) {
982 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
983 PFN_UP(bo->base.size));
988 /* passed reservation objects should already be locked,
989 * since otherwise lockdep will be angered in radeon.
992 WARN_ON(!dma_resv_trylock(bo->base.resv));
994 dma_resv_assert_held(resv);
996 ret = ttm_bo_validate(bo, placement, ctx);
1004 dma_resv_unlock(bo->base.resv);
1010 EXPORT_SYMBOL(ttm_bo_init_reserved);
1013 * ttm_bo_init_validate
1015 * @bdev: Pointer to a ttm_device struct.
1016 * @bo: Pointer to a ttm_buffer_object to be initialized.
1017 * @type: Requested type of buffer object.
1018 * @placement: Initial placement for buffer object.
1019 * @alignment: Data alignment in pages.
1020 * @interruptible: If needing to sleep to wait for GPU resources,
1021 * sleep interruptible.
1022 * pinned in physical memory. If this behaviour is not desired, this member
1023 * holds a pointer to a persistent shmem object. Typically, this would
1024 * point to the shmem object backing a GEM object if TTM is used to back a
1025 * GEM user interface.
1026 * @sg: Scatter-gather table.
1027 * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
1028 * @destroy: Destroy function. Use NULL for kfree().
1030 * This function initializes a pre-allocated struct ttm_buffer_object.
1031 * As this object may be part of a larger structure, this function,
1032 * together with the @destroy function,
1033 * enables driver-specific objects derived from a ttm_buffer_object.
1035 * On successful return, the caller owns an object kref to @bo. The kref and
1036 * list_kref are usually set to 1, but note that in some situations, other
1037 * tasks may already be holding references to @bo as well.
1039 * If a failure occurs, the function will call the @destroy function, Thus,
1040 * after a failure, dereferencing @bo is illegal and will likely cause memory
1044 * -ENOMEM: Out of memory.
1045 * -EINVAL: Invalid placement flags.
1046 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
1048 int ttm_bo_init_validate(struct ttm_device *bdev, struct ttm_buffer_object *bo,
1049 enum ttm_bo_type type, struct ttm_placement *placement,
1050 uint32_t alignment, bool interruptible,
1051 struct sg_table *sg, struct dma_resv *resv,
1052 void (*destroy) (struct ttm_buffer_object *))
1054 struct ttm_operation_ctx ctx = { interruptible, false };
1057 ret = ttm_bo_init_reserved(bdev, bo, type, placement, alignment, &ctx,
1063 ttm_bo_unreserve(bo);
1067 EXPORT_SYMBOL(ttm_bo_init_validate);
1070 * buffer object vm functions.
1074 * ttm_bo_unmap_virtual
1076 * @bo: tear down the virtual mappings for this BO
1078 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1080 struct ttm_device *bdev = bo->bdev;
1082 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1083 ttm_mem_io_free(bdev, bo->resource);
1085 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1088 * ttm_bo_wait_ctx - wait for buffer idle.
1090 * @bo: The buffer object.
1091 * @ctx: defines how to wait
1093 * Waits for the buffer to be idle. Used timeout depends on the context.
1094 * Returns -EBUSY if wait timed outt, -ERESTARTSYS if interrupted by a signal or
1097 int ttm_bo_wait_ctx(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx)
1101 if (ctx->no_wait_gpu) {
1102 if (dma_resv_test_signaled(bo->base.resv,
1103 DMA_RESV_USAGE_BOOKKEEP))
1109 ret = dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
1110 ctx->interruptible, 15 * HZ);
1111 if (unlikely(ret < 0))
1113 if (unlikely(ret == 0))
1117 EXPORT_SYMBOL(ttm_bo_wait_ctx);
1119 int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
1122 struct ttm_place place;
1127 * While the bo may already reside in SYSTEM placement, set
1128 * SYSTEM as new placement to cover also the move further below.
1129 * The driver may use the fact that we're moving from SYSTEM
1130 * as an indication that we're about to swap out.
1132 memset(&place, 0, sizeof(place));
1133 place.mem_type = bo->resource->mem_type;
1134 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place, &locked, NULL))
1137 if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
1138 bo->ttm->page_flags & TTM_TT_FLAG_EXTERNAL ||
1139 bo->ttm->page_flags & TTM_TT_FLAG_SWAPPED ||
1140 !ttm_bo_get_unless_zero(bo)) {
1142 dma_resv_unlock(bo->base.resv);
1147 ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1149 return ret == -EBUSY ? -ENOSPC : ret;
1152 /* TODO: Cleanup the locking */
1153 spin_unlock(&bo->bdev->lru_lock);
1156 * Move to system cached
1158 if (bo->resource->mem_type != TTM_PL_SYSTEM) {
1159 struct ttm_operation_ctx ctx = { false, false };
1160 struct ttm_resource *evict_mem;
1161 struct ttm_place hop;
1163 memset(&hop, 0, sizeof(hop));
1164 place.mem_type = TTM_PL_SYSTEM;
1165 ret = ttm_resource_alloc(bo, &place, &evict_mem);
1169 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, &ctx, &hop);
1170 if (unlikely(ret != 0)) {
1171 WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
1177 * Make sure BO is idle.
1179 ret = ttm_bo_wait_ctx(bo, ctx);
1180 if (unlikely(ret != 0))
1183 ttm_bo_unmap_virtual(bo);
1186 * Swap out. Buffer will be swapped in again as soon as
1187 * anyone tries to access a ttm page.
1189 if (bo->bdev->funcs->swap_notify)
1190 bo->bdev->funcs->swap_notify(bo);
1192 if (ttm_tt_is_populated(bo->ttm))
1193 ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);
1197 * Unreserve without putting on LRU to avoid swapping out an
1198 * already swapped buffer.
1201 dma_resv_unlock(bo->base.resv);
1203 return ret == -EBUSY ? -ENOSPC : ret;
1206 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1208 if (bo->ttm == NULL)
1211 ttm_tt_unpopulate(bo->bdev, bo->ttm);
1212 ttm_tt_destroy(bo->bdev, bo->ttm);