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 || ttm_manager_type(bdev, bo->resource->mem_type)->use_tt;
124 new_use_tt = ttm_manager_type(bdev, mem->mem_type)->use_tt;
126 ttm_bo_unmap_virtual(bo);
129 * Create and bind a ttm if required.
133 /* Zero init the new TTM structure if the old location should
134 * have used one as well.
136 ret = ttm_tt_create(bo, old_use_tt);
140 if (mem->mem_type != TTM_PL_SYSTEM) {
141 ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
147 ret = dma_resv_reserve_fences(bo->base.resv, 1);
151 ret = bdev->funcs->move(bo, evict, ctx, mem, hop);
153 if (ret == -EMULTIHOP)
158 ctx->bytes_moved += bo->base.size;
163 ttm_bo_tt_destroy(bo);
170 * Will release GPU memory type usage on destruction.
171 * This is the place to put in driver specific hooks to release
172 * driver private resources.
173 * Will release the bo::reserved lock.
176 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
178 if (bo->bdev->funcs->delete_mem_notify)
179 bo->bdev->funcs->delete_mem_notify(bo);
181 ttm_bo_tt_destroy(bo);
182 ttm_resource_free(bo, &bo->resource);
185 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
189 if (bo->base.resv == &bo->base._resv)
192 BUG_ON(!dma_resv_trylock(&bo->base._resv));
194 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
195 dma_resv_unlock(&bo->base._resv);
199 if (bo->type != ttm_bo_type_sg) {
200 /* This works because the BO is about to be destroyed and nobody
201 * reference it any more. The only tricky case is the trylock on
202 * the resv object while holding the lru_lock.
204 spin_lock(&bo->bdev->lru_lock);
205 bo->base.resv = &bo->base._resv;
206 spin_unlock(&bo->bdev->lru_lock);
212 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
214 struct dma_resv *resv = &bo->base._resv;
215 struct dma_resv_iter cursor;
216 struct dma_fence *fence;
218 dma_resv_iter_begin(&cursor, resv, DMA_RESV_USAGE_BOOKKEEP);
219 dma_resv_for_each_fence_unlocked(&cursor, fence) {
220 if (!fence->ops->signaled)
221 dma_fence_enable_sw_signaling(fence);
223 dma_resv_iter_end(&cursor);
227 * ttm_bo_cleanup_refs
228 * If bo idle, remove from lru lists, and unref.
229 * If not idle, block if possible.
231 * Must be called with lru_lock and reservation held, this function
232 * will drop the lru lock and optionally the reservation lock before returning.
234 * @bo: The buffer object to clean-up
235 * @interruptible: Any sleeps should occur interruptibly.
236 * @no_wait_gpu: Never wait for gpu. Return -EBUSY instead.
237 * @unlock_resv: Unlock the reservation lock as well.
240 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
241 bool interruptible, bool no_wait_gpu,
244 struct dma_resv *resv = &bo->base._resv;
247 if (dma_resv_test_signaled(resv, DMA_RESV_USAGE_BOOKKEEP))
252 if (ret && !no_wait_gpu) {
256 dma_resv_unlock(bo->base.resv);
257 spin_unlock(&bo->bdev->lru_lock);
259 lret = dma_resv_wait_timeout(resv, DMA_RESV_USAGE_BOOKKEEP,
268 spin_lock(&bo->bdev->lru_lock);
269 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
271 * We raced, and lost, someone else holds the reservation now,
272 * and is probably busy in ttm_bo_cleanup_memtype_use.
274 * Even if it's not the case, because we finished waiting any
275 * delayed destruction would succeed, so just return success
278 spin_unlock(&bo->bdev->lru_lock);
286 dma_resv_unlock(bo->base.resv);
287 spin_unlock(&bo->bdev->lru_lock);
291 spin_unlock(&bo->bdev->lru_lock);
292 ttm_bo_cleanup_memtype_use(bo);
295 dma_resv_unlock(bo->base.resv);
303 * Block for the dma_resv object to become idle, lock the buffer and clean up
304 * the resource and tt object.
306 static void ttm_bo_delayed_delete(struct work_struct *work)
308 struct ttm_buffer_object *bo;
310 bo = container_of(work, typeof(*bo), delayed_delete);
312 dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP, false,
313 MAX_SCHEDULE_TIMEOUT);
314 dma_resv_lock(bo->base.resv, NULL);
315 ttm_bo_cleanup_memtype_use(bo);
316 dma_resv_unlock(bo->base.resv);
320 static void ttm_bo_release(struct kref *kref)
322 struct ttm_buffer_object *bo =
323 container_of(kref, struct ttm_buffer_object, kref);
324 struct ttm_device *bdev = bo->bdev;
327 WARN_ON_ONCE(bo->pin_count);
328 WARN_ON_ONCE(bo->bulk_move);
331 ret = ttm_bo_individualize_resv(bo);
333 /* Last resort, if we fail to allocate memory for the
334 * fences block for the BO to become idle
336 dma_resv_wait_timeout(bo->base.resv,
337 DMA_RESV_USAGE_BOOKKEEP, false,
341 if (bo->bdev->funcs->release_notify)
342 bo->bdev->funcs->release_notify(bo);
344 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
345 ttm_mem_io_free(bdev, bo->resource);
347 if (!dma_resv_test_signaled(bo->base.resv,
348 DMA_RESV_USAGE_BOOKKEEP) ||
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);
373 queue_work(bdev->wq, &bo->delayed_delete);
377 ttm_bo_cleanup_memtype_use(bo);
378 dma_resv_unlock(bo->base.resv);
381 atomic_dec(&ttm_glob.bo_count);
388 * @bo: The buffer object.
390 * Unreference a buffer object.
392 void ttm_bo_put(struct ttm_buffer_object *bo)
394 kref_put(&bo->kref, ttm_bo_release);
396 EXPORT_SYMBOL(ttm_bo_put);
398 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
399 struct ttm_resource **mem,
400 struct ttm_operation_ctx *ctx,
401 struct ttm_place *hop)
403 struct ttm_placement hop_placement;
404 struct ttm_resource *hop_mem;
407 hop_placement.num_placement = hop_placement.num_busy_placement = 1;
408 hop_placement.placement = hop_placement.busy_placement = hop;
410 /* find space in the bounce domain */
411 ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
414 /* move to the bounce domain */
415 ret = ttm_bo_handle_move_mem(bo, hop_mem, false, ctx, NULL);
417 ttm_resource_free(bo, &hop_mem);
423 static int ttm_bo_evict(struct ttm_buffer_object *bo,
424 struct ttm_operation_ctx *ctx)
426 struct ttm_device *bdev = bo->bdev;
427 struct ttm_resource *evict_mem;
428 struct ttm_placement placement;
429 struct ttm_place hop;
432 memset(&hop, 0, sizeof(hop));
434 dma_resv_assert_held(bo->base.resv);
436 placement.num_placement = 0;
437 placement.num_busy_placement = 0;
438 bdev->funcs->evict_flags(bo, &placement);
440 if (!placement.num_placement && !placement.num_busy_placement) {
441 ret = ttm_bo_wait_ctx(bo, ctx);
446 * Since we've already synced, this frees backing store
449 return ttm_bo_pipeline_gutting(bo);
452 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
454 if (ret != -ERESTARTSYS) {
455 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
457 ttm_bo_mem_space_debug(bo, &placement);
463 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
464 if (ret == -EMULTIHOP) {
465 ret = ttm_bo_bounce_temp_buffer(bo, &evict_mem, ctx, &hop);
467 if (ret != -ERESTARTSYS && ret != -EINTR)
468 pr_err("Buffer eviction failed\n");
469 ttm_resource_free(bo, &evict_mem);
472 /* try and move to final place now. */
480 * ttm_bo_eviction_valuable
482 * @bo: The buffer object to evict
483 * @place: the placement we need to make room for
485 * Check if it is valuable to evict the BO to make room for the given placement.
487 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
488 const struct ttm_place *place)
490 struct ttm_resource *res = bo->resource;
491 struct ttm_device *bdev = bo->bdev;
493 dma_resv_assert_held(bo->base.resv);
494 if (bo->resource->mem_type == TTM_PL_SYSTEM)
497 /* Don't evict this BO if it's outside of the
498 * requested placement range
500 return ttm_resource_intersects(bdev, res, place, bo->base.size);
502 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
505 * Check the target bo is allowable to be evicted or swapout, including cases:
507 * a. if share same reservation object with ctx->resv, have assumption
508 * reservation objects should already be locked, so not lock again and
509 * return true directly when either the opreation allow_reserved_eviction
510 * or the target bo already is in delayed free list;
512 * b. Otherwise, trylock it.
514 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
515 struct ttm_operation_ctx *ctx,
516 const struct ttm_place *place,
517 bool *locked, bool *busy)
521 if (bo->base.resv == ctx->resv) {
522 dma_resv_assert_held(bo->base.resv);
523 if (ctx->allow_res_evict)
529 ret = dma_resv_trylock(bo->base.resv);
535 if (ret && place && (bo->resource->mem_type != place->mem_type ||
536 !bo->bdev->funcs->eviction_valuable(bo, place))) {
539 dma_resv_unlock(bo->base.resv);
548 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
550 * @busy_bo: BO which couldn't be locked with trylock
551 * @ctx: operation context
552 * @ticket: acquire ticket
554 * Try to lock a busy buffer object to avoid failing eviction.
556 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
557 struct ttm_operation_ctx *ctx,
558 struct ww_acquire_ctx *ticket)
562 if (!busy_bo || !ticket)
565 if (ctx->interruptible)
566 r = dma_resv_lock_interruptible(busy_bo->base.resv,
569 r = dma_resv_lock(busy_bo->base.resv, ticket);
572 * TODO: It would be better to keep the BO locked until allocation is at
573 * least tried one more time, but that would mean a much larger rework
577 dma_resv_unlock(busy_bo->base.resv);
579 return r == -EDEADLK ? -EBUSY : r;
582 int ttm_mem_evict_first(struct ttm_device *bdev,
583 struct ttm_resource_manager *man,
584 const struct ttm_place *place,
585 struct ttm_operation_ctx *ctx,
586 struct ww_acquire_ctx *ticket)
588 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
589 struct ttm_resource_cursor cursor;
590 struct ttm_resource *res;
594 spin_lock(&bdev->lru_lock);
595 ttm_resource_manager_for_each_res(man, &cursor, res) {
598 if (!ttm_bo_evict_swapout_allowable(res->bo, ctx, place,
600 if (busy && !busy_bo && ticket !=
601 dma_resv_locking_ctx(res->bo->base.resv))
606 if (ttm_bo_get_unless_zero(res->bo)) {
611 dma_resv_unlock(res->bo->base.resv);
615 if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
617 spin_unlock(&bdev->lru_lock);
618 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
625 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
626 ctx->no_wait_gpu, locked);
631 spin_unlock(&bdev->lru_lock);
633 ret = ttm_bo_evict(bo, ctx);
635 ttm_bo_unreserve(bo);
637 ttm_bo_move_to_lru_tail_unlocked(bo);
644 * ttm_bo_pin - Pin the buffer object.
645 * @bo: The buffer object to pin
647 * Make sure the buffer is not evicted any more during memory pressure.
648 * @bo must be unpinned again by calling ttm_bo_unpin().
650 void ttm_bo_pin(struct ttm_buffer_object *bo)
652 dma_resv_assert_held(bo->base.resv);
653 WARN_ON_ONCE(!kref_read(&bo->kref));
654 spin_lock(&bo->bdev->lru_lock);
656 ttm_resource_del_bulk_move(bo->resource, bo);
658 spin_unlock(&bo->bdev->lru_lock);
660 EXPORT_SYMBOL(ttm_bo_pin);
663 * ttm_bo_unpin - Unpin the buffer object.
664 * @bo: The buffer object to unpin
666 * Allows the buffer object to be evicted again during memory pressure.
668 void ttm_bo_unpin(struct ttm_buffer_object *bo)
670 dma_resv_assert_held(bo->base.resv);
671 WARN_ON_ONCE(!kref_read(&bo->kref));
672 if (WARN_ON_ONCE(!bo->pin_count))
675 spin_lock(&bo->bdev->lru_lock);
678 ttm_resource_add_bulk_move(bo->resource, bo);
679 spin_unlock(&bo->bdev->lru_lock);
681 EXPORT_SYMBOL(ttm_bo_unpin);
684 * Add the last move fence to the BO as kernel dependency and reserve a new
687 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
688 struct ttm_resource_manager *man,
689 struct ttm_resource *mem,
692 struct dma_fence *fence;
695 spin_lock(&man->move_lock);
696 fence = dma_fence_get(man->move);
697 spin_unlock(&man->move_lock);
703 ret = dma_fence_is_signaled(fence) ? 0 : -EBUSY;
704 dma_fence_put(fence);
708 dma_resv_add_fence(bo->base.resv, fence, DMA_RESV_USAGE_KERNEL);
710 ret = dma_resv_reserve_fences(bo->base.resv, 1);
711 dma_fence_put(fence);
716 * Repeatedly evict memory from the LRU for @mem_type until we create enough
717 * space, or we've evicted everything and there isn't enough space.
719 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
720 const struct ttm_place *place,
721 struct ttm_resource **mem,
722 struct ttm_operation_ctx *ctx)
724 struct ttm_device *bdev = bo->bdev;
725 struct ttm_resource_manager *man;
726 struct ww_acquire_ctx *ticket;
729 man = ttm_manager_type(bdev, place->mem_type);
730 ticket = dma_resv_locking_ctx(bo->base.resv);
732 ret = ttm_resource_alloc(bo, place, mem);
735 if (unlikely(ret != -ENOSPC))
737 ret = ttm_mem_evict_first(bdev, man, place, ctx,
739 if (unlikely(ret != 0))
743 return ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
749 * @bo: Pointer to a struct ttm_buffer_object. the data of which
750 * we want to allocate space for.
751 * @proposed_placement: Proposed new placement for the buffer object.
752 * @mem: A struct ttm_resource.
753 * @ctx: if and how to sleep, lock buffers and alloc memory
755 * Allocate memory space for the buffer object pointed to by @bo, using
756 * the placement flags in @placement, potentially evicting other idle buffer objects.
757 * This function may sleep while waiting for space to become available.
759 * -EBUSY: No space available (only if no_wait == 1).
760 * -ENOMEM: Could not allocate memory for the buffer object, either due to
761 * fragmentation or concurrent allocators.
762 * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
764 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
765 struct ttm_placement *placement,
766 struct ttm_resource **mem,
767 struct ttm_operation_ctx *ctx)
769 struct ttm_device *bdev = bo->bdev;
770 bool type_found = false;
773 ret = dma_resv_reserve_fences(bo->base.resv, 1);
777 for (i = 0; i < placement->num_placement; ++i) {
778 const struct ttm_place *place = &placement->placement[i];
779 struct ttm_resource_manager *man;
781 man = ttm_manager_type(bdev, place->mem_type);
782 if (!man || !ttm_resource_manager_used(man))
786 ret = ttm_resource_alloc(bo, place, mem);
792 ret = ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
794 ttm_resource_free(bo, mem);
803 for (i = 0; i < placement->num_busy_placement; ++i) {
804 const struct ttm_place *place = &placement->busy_placement[i];
805 struct ttm_resource_manager *man;
807 man = ttm_manager_type(bdev, place->mem_type);
808 if (!man || !ttm_resource_manager_used(man))
812 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
816 if (ret && ret != -EBUSY)
822 pr_err(TTM_PFX "No compatible memory type found\n");
829 EXPORT_SYMBOL(ttm_bo_mem_space);
831 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
832 struct ttm_placement *placement,
833 struct ttm_operation_ctx *ctx)
835 struct ttm_resource *mem;
836 struct ttm_place hop;
839 dma_resv_assert_held(bo->base.resv);
842 * Determine where to move the buffer.
844 * If driver determines move is going to need
845 * an extra step then it will return -EMULTIHOP
846 * and the buffer will be moved to the temporary
847 * stop and the driver will be called to make
850 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
854 ret = ttm_bo_handle_move_mem(bo, mem, false, ctx, &hop);
855 if (ret == -EMULTIHOP) {
856 ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
859 /* try and move to final place now. */
864 ttm_resource_free(bo, &mem);
871 * @bo: The buffer object.
872 * @placement: Proposed placement for the buffer object.
873 * @ctx: validation parameters.
875 * Changes placement and caching policy of the buffer object
876 * according proposed placement.
878 * -EINVAL on invalid proposed placement.
879 * -ENOMEM on out-of-memory condition.
880 * -EBUSY if no_wait is true and buffer busy.
881 * -ERESTARTSYS if interrupted by a signal.
883 int ttm_bo_validate(struct ttm_buffer_object *bo,
884 struct ttm_placement *placement,
885 struct ttm_operation_ctx *ctx)
889 dma_resv_assert_held(bo->base.resv);
892 * Remove the backing store if no placement is given.
894 if (!placement->num_placement && !placement->num_busy_placement)
895 return ttm_bo_pipeline_gutting(bo);
897 /* Check whether we need to move buffer. */
898 if (bo->resource && ttm_resource_compat(bo->resource, placement))
901 /* Moving of pinned BOs is forbidden */
905 ret = ttm_bo_move_buffer(bo, placement, ctx);
910 * We might need to add a TTM.
912 if (!bo->resource || bo->resource->mem_type == TTM_PL_SYSTEM) {
913 ret = ttm_tt_create(bo, true);
919 EXPORT_SYMBOL(ttm_bo_validate);
922 * ttm_bo_init_reserved
924 * @bdev: Pointer to a ttm_device struct.
925 * @bo: Pointer to a ttm_buffer_object to be initialized.
926 * @type: Requested type of buffer object.
927 * @placement: Initial placement for buffer object.
928 * @alignment: Data alignment in pages.
929 * @ctx: TTM operation context for memory allocation.
930 * @sg: Scatter-gather table.
931 * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
932 * @destroy: Destroy function. Use NULL for kfree().
934 * This function initializes a pre-allocated struct ttm_buffer_object.
935 * As this object may be part of a larger structure, this function,
936 * together with the @destroy function, enables driver-specific objects
937 * derived from a ttm_buffer_object.
939 * On successful return, the caller owns an object kref to @bo. The kref and
940 * list_kref are usually set to 1, but note that in some situations, other
941 * tasks may already be holding references to @bo as well.
942 * Furthermore, if resv == NULL, the buffer's reservation lock will be held,
943 * and it is the caller's responsibility to call ttm_bo_unreserve.
945 * If a failure occurs, the function will call the @destroy function. Thus,
946 * after a failure, dereferencing @bo is illegal and will likely cause memory
950 * -ENOMEM: Out of memory.
951 * -EINVAL: Invalid placement flags.
952 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
954 int ttm_bo_init_reserved(struct ttm_device *bdev, struct ttm_buffer_object *bo,
955 enum ttm_bo_type type, struct ttm_placement *placement,
956 uint32_t alignment, struct ttm_operation_ctx *ctx,
957 struct sg_table *sg, struct dma_resv *resv,
958 void (*destroy) (struct ttm_buffer_object *))
962 kref_init(&bo->kref);
965 bo->page_alignment = alignment;
966 bo->destroy = destroy;
969 bo->bulk_move = NULL;
971 bo->base.resv = resv;
973 bo->base.resv = &bo->base._resv;
974 atomic_inc(&ttm_glob.bo_count);
977 * For ttm_bo_type_device buffers, allocate
978 * address space from the device.
980 if (bo->type == ttm_bo_type_device || bo->type == ttm_bo_type_sg) {
981 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
982 PFN_UP(bo->base.size));
987 /* passed reservation objects should already be locked,
988 * since otherwise lockdep will be angered in radeon.
991 WARN_ON(!dma_resv_trylock(bo->base.resv));
993 dma_resv_assert_held(resv);
995 ret = ttm_bo_validate(bo, placement, ctx);
1003 dma_resv_unlock(bo->base.resv);
1009 EXPORT_SYMBOL(ttm_bo_init_reserved);
1012 * ttm_bo_init_validate
1014 * @bdev: Pointer to a ttm_device struct.
1015 * @bo: Pointer to a ttm_buffer_object to be initialized.
1016 * @type: Requested type of buffer object.
1017 * @placement: Initial placement for buffer object.
1018 * @alignment: Data alignment in pages.
1019 * @interruptible: If needing to sleep to wait for GPU resources,
1020 * sleep interruptible.
1021 * pinned in physical memory. If this behaviour is not desired, this member
1022 * holds a pointer to a persistent shmem object. Typically, this would
1023 * point to the shmem object backing a GEM object if TTM is used to back a
1024 * GEM user interface.
1025 * @sg: Scatter-gather table.
1026 * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
1027 * @destroy: Destroy function. Use NULL for kfree().
1029 * This function initializes a pre-allocated struct ttm_buffer_object.
1030 * As this object may be part of a larger structure, this function,
1031 * together with the @destroy function,
1032 * enables driver-specific objects derived from a ttm_buffer_object.
1034 * On successful return, the caller owns an object kref to @bo. The kref and
1035 * list_kref are usually set to 1, but note that in some situations, other
1036 * tasks may already be holding references to @bo as well.
1038 * If a failure occurs, the function will call the @destroy function, Thus,
1039 * after a failure, dereferencing @bo is illegal and will likely cause memory
1043 * -ENOMEM: Out of memory.
1044 * -EINVAL: Invalid placement flags.
1045 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
1047 int ttm_bo_init_validate(struct ttm_device *bdev, struct ttm_buffer_object *bo,
1048 enum ttm_bo_type type, struct ttm_placement *placement,
1049 uint32_t alignment, bool interruptible,
1050 struct sg_table *sg, struct dma_resv *resv,
1051 void (*destroy) (struct ttm_buffer_object *))
1053 struct ttm_operation_ctx ctx = { interruptible, false };
1056 ret = ttm_bo_init_reserved(bdev, bo, type, placement, alignment, &ctx,
1062 ttm_bo_unreserve(bo);
1066 EXPORT_SYMBOL(ttm_bo_init_validate);
1069 * buffer object vm functions.
1073 * ttm_bo_unmap_virtual
1075 * @bo: tear down the virtual mappings for this BO
1077 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1079 struct ttm_device *bdev = bo->bdev;
1081 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1082 ttm_mem_io_free(bdev, bo->resource);
1084 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1087 * ttm_bo_wait_ctx - wait for buffer idle.
1089 * @bo: The buffer object.
1090 * @ctx: defines how to wait
1092 * Waits for the buffer to be idle. Used timeout depends on the context.
1093 * Returns -EBUSY if wait timed outt, -ERESTARTSYS if interrupted by a signal or
1096 int ttm_bo_wait_ctx(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx)
1100 if (ctx->no_wait_gpu) {
1101 if (dma_resv_test_signaled(bo->base.resv,
1102 DMA_RESV_USAGE_BOOKKEEP))
1108 ret = dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
1109 ctx->interruptible, 15 * HZ);
1110 if (unlikely(ret < 0))
1112 if (unlikely(ret == 0))
1116 EXPORT_SYMBOL(ttm_bo_wait_ctx);
1118 int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
1121 struct ttm_place place;
1126 * While the bo may already reside in SYSTEM placement, set
1127 * SYSTEM as new placement to cover also the move further below.
1128 * The driver may use the fact that we're moving from SYSTEM
1129 * as an indication that we're about to swap out.
1131 memset(&place, 0, sizeof(place));
1132 place.mem_type = bo->resource->mem_type;
1133 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place, &locked, NULL))
1136 if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
1137 bo->ttm->page_flags & TTM_TT_FLAG_EXTERNAL ||
1138 bo->ttm->page_flags & TTM_TT_FLAG_SWAPPED ||
1139 !ttm_bo_get_unless_zero(bo)) {
1141 dma_resv_unlock(bo->base.resv);
1146 ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1148 return ret == -EBUSY ? -ENOSPC : ret;
1151 /* TODO: Cleanup the locking */
1152 spin_unlock(&bo->bdev->lru_lock);
1155 * Move to system cached
1157 if (bo->resource->mem_type != TTM_PL_SYSTEM) {
1158 struct ttm_operation_ctx ctx = { false, false };
1159 struct ttm_resource *evict_mem;
1160 struct ttm_place hop;
1162 memset(&hop, 0, sizeof(hop));
1163 place.mem_type = TTM_PL_SYSTEM;
1164 ret = ttm_resource_alloc(bo, &place, &evict_mem);
1168 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, &ctx, &hop);
1169 if (unlikely(ret != 0)) {
1170 WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
1176 * Make sure BO is idle.
1178 ret = ttm_bo_wait_ctx(bo, ctx);
1179 if (unlikely(ret != 0))
1182 ttm_bo_unmap_virtual(bo);
1185 * Swap out. Buffer will be swapped in again as soon as
1186 * anyone tries to access a ttm page.
1188 if (bo->bdev->funcs->swap_notify)
1189 bo->bdev->funcs->swap_notify(bo);
1191 if (ttm_tt_is_populated(bo->ttm))
1192 ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);
1196 * Unreserve without putting on LRU to avoid swapping out an
1197 * already swapped buffer.
1200 dma_resv_unlock(bo->base.resv);
1202 return ret == -EBUSY ? -ENOSPC : ret;
1205 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1207 if (bo->ttm == NULL)
1210 ttm_tt_unpopulate(bo->bdev, bo->ttm);
1211 ttm_tt_destroy(bo->bdev, bo->ttm);