1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
4 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
29 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
32 #define pr_fmt(fmt) "[TTM] " fmt
34 #include <drm/ttm/ttm_bo_driver.h>
35 #include <drm/ttm/ttm_placement.h>
36 #include <linux/jiffies.h>
37 #include <linux/slab.h>
38 #include <linux/sched.h>
40 #include <linux/file.h>
41 #include <linux/module.h>
42 #include <linux/atomic.h>
43 #include <linux/dma-resv.h>
45 #include "ttm_module.h"
47 /* default destructor */
48 static void ttm_bo_default_destroy(struct ttm_buffer_object *bo)
53 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
54 struct ttm_placement *placement)
56 struct drm_printer p = drm_debug_printer(TTM_PFX);
57 struct ttm_resource_manager *man;
60 drm_printf(&p, "No space for %p (%lu pages, %zuK, %zuM)\n",
61 bo, bo->resource->num_pages, bo->base.size >> 10,
63 for (i = 0; i < placement->num_placement; i++) {
64 mem_type = placement->placement[i].mem_type;
65 drm_printf(&p, " placement[%d]=0x%08X (%d)\n",
66 i, placement->placement[i].flags, mem_type);
67 man = ttm_manager_type(bo->bdev, mem_type);
68 ttm_resource_manager_debug(man, &p);
72 static void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
74 struct ttm_device *bdev = bo->bdev;
76 list_del_init(&bo->lru);
78 if (bdev->funcs->del_from_lru_notify)
79 bdev->funcs->del_from_lru_notify(bo);
82 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
83 struct ttm_buffer_object *bo)
90 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
91 struct ttm_resource *mem,
92 struct ttm_lru_bulk_move *bulk)
94 struct ttm_device *bdev = bo->bdev;
95 struct ttm_resource_manager *man;
98 dma_resv_assert_held(bo->base.resv);
101 ttm_bo_del_from_lru(bo);
105 man = ttm_manager_type(bdev, mem->mem_type);
106 list_move_tail(&bo->lru, &man->lru[bo->priority]);
108 if (bdev->funcs->del_from_lru_notify)
109 bdev->funcs->del_from_lru_notify(bo);
111 if (bulk && !bo->pin_count) {
112 switch (bo->resource->mem_type) {
114 ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
118 ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
123 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
125 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
129 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
130 struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
131 struct ttm_resource_manager *man;
136 dma_resv_assert_held(pos->first->base.resv);
137 dma_resv_assert_held(pos->last->base.resv);
139 man = ttm_manager_type(pos->first->bdev, TTM_PL_TT);
140 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
144 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
145 struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
146 struct ttm_resource_manager *man;
151 dma_resv_assert_held(pos->first->base.resv);
152 dma_resv_assert_held(pos->last->base.resv);
154 man = ttm_manager_type(pos->first->bdev, TTM_PL_VRAM);
155 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
159 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
161 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
162 struct ttm_resource *mem, bool evict,
163 struct ttm_operation_ctx *ctx,
164 struct ttm_place *hop)
166 struct ttm_resource_manager *old_man, *new_man;
167 struct ttm_device *bdev = bo->bdev;
170 old_man = ttm_manager_type(bdev, bo->resource->mem_type);
171 new_man = ttm_manager_type(bdev, mem->mem_type);
173 ttm_bo_unmap_virtual(bo);
176 * Create and bind a ttm if required.
179 if (new_man->use_tt) {
180 /* Zero init the new TTM structure if the old location should
181 * have used one as well.
183 ret = ttm_tt_create(bo, old_man->use_tt);
187 if (mem->mem_type != TTM_PL_SYSTEM) {
188 ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
194 ret = bdev->funcs->move(bo, evict, ctx, mem, hop);
196 if (ret == -EMULTIHOP)
201 ctx->bytes_moved += bo->base.size;
205 new_man = ttm_manager_type(bdev, bo->resource->mem_type);
206 if (!new_man->use_tt)
207 ttm_bo_tt_destroy(bo);
214 * Will release GPU memory type usage on destruction.
215 * This is the place to put in driver specific hooks to release
216 * driver private resources.
217 * Will release the bo::reserved lock.
220 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
222 if (bo->bdev->funcs->delete_mem_notify)
223 bo->bdev->funcs->delete_mem_notify(bo);
225 ttm_bo_tt_destroy(bo);
226 ttm_resource_free(bo, &bo->resource);
229 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
233 if (bo->base.resv == &bo->base._resv)
236 BUG_ON(!dma_resv_trylock(&bo->base._resv));
238 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
239 dma_resv_unlock(&bo->base._resv);
243 if (bo->type != ttm_bo_type_sg) {
244 /* This works because the BO is about to be destroyed and nobody
245 * reference it any more. The only tricky case is the trylock on
246 * the resv object while holding the lru_lock.
248 spin_lock(&bo->bdev->lru_lock);
249 bo->base.resv = &bo->base._resv;
250 spin_unlock(&bo->bdev->lru_lock);
256 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
258 struct dma_resv *resv = &bo->base._resv;
259 struct dma_resv_list *fobj;
260 struct dma_fence *fence;
264 fobj = dma_resv_shared_list(resv);
265 fence = dma_resv_excl_fence(resv);
266 if (fence && !fence->ops->signaled)
267 dma_fence_enable_sw_signaling(fence);
269 for (i = 0; fobj && i < fobj->shared_count; ++i) {
270 fence = rcu_dereference(fobj->shared[i]);
272 if (!fence->ops->signaled)
273 dma_fence_enable_sw_signaling(fence);
279 * ttm_bo_cleanup_refs
280 * If bo idle, remove from lru lists, and unref.
281 * If not idle, block if possible.
283 * Must be called with lru_lock and reservation held, this function
284 * will drop the lru lock and optionally the reservation lock before returning.
286 * @bo: The buffer object to clean-up
287 * @interruptible: Any sleeps should occur interruptibly.
288 * @no_wait_gpu: Never wait for gpu. Return -EBUSY instead.
289 * @unlock_resv: Unlock the reservation lock as well.
292 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
293 bool interruptible, bool no_wait_gpu,
296 struct dma_resv *resv = &bo->base._resv;
299 if (dma_resv_test_signaled(resv, true))
304 if (ret && !no_wait_gpu) {
308 dma_resv_unlock(bo->base.resv);
309 spin_unlock(&bo->bdev->lru_lock);
311 lret = dma_resv_wait_timeout(resv, true, interruptible,
319 spin_lock(&bo->bdev->lru_lock);
320 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
322 * We raced, and lost, someone else holds the reservation now,
323 * and is probably busy in ttm_bo_cleanup_memtype_use.
325 * Even if it's not the case, because we finished waiting any
326 * delayed destruction would succeed, so just return success
329 spin_unlock(&bo->bdev->lru_lock);
335 if (ret || unlikely(list_empty(&bo->ddestroy))) {
337 dma_resv_unlock(bo->base.resv);
338 spin_unlock(&bo->bdev->lru_lock);
342 ttm_bo_del_from_lru(bo);
343 list_del_init(&bo->ddestroy);
344 spin_unlock(&bo->bdev->lru_lock);
345 ttm_bo_cleanup_memtype_use(bo);
348 dma_resv_unlock(bo->base.resv);
356 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
357 * encountered buffers.
359 bool ttm_bo_delayed_delete(struct ttm_device *bdev, bool remove_all)
361 struct list_head removed;
364 INIT_LIST_HEAD(&removed);
366 spin_lock(&bdev->lru_lock);
367 while (!list_empty(&bdev->ddestroy)) {
368 struct ttm_buffer_object *bo;
370 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
372 list_move_tail(&bo->ddestroy, &removed);
373 if (!ttm_bo_get_unless_zero(bo))
376 if (remove_all || bo->base.resv != &bo->base._resv) {
377 spin_unlock(&bdev->lru_lock);
378 dma_resv_lock(bo->base.resv, NULL);
380 spin_lock(&bdev->lru_lock);
381 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
383 } else if (dma_resv_trylock(bo->base.resv)) {
384 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
386 spin_unlock(&bdev->lru_lock);
390 spin_lock(&bdev->lru_lock);
392 list_splice_tail(&removed, &bdev->ddestroy);
393 empty = list_empty(&bdev->ddestroy);
394 spin_unlock(&bdev->lru_lock);
399 static void ttm_bo_release(struct kref *kref)
401 struct ttm_buffer_object *bo =
402 container_of(kref, struct ttm_buffer_object, kref);
403 struct ttm_device *bdev = bo->bdev;
406 WARN_ON_ONCE(bo->pin_count);
409 ret = ttm_bo_individualize_resv(bo);
411 /* Last resort, if we fail to allocate memory for the
412 * fences block for the BO to become idle
414 dma_resv_wait_timeout(bo->base.resv, true, false,
418 if (bo->bdev->funcs->release_notify)
419 bo->bdev->funcs->release_notify(bo);
421 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
422 ttm_mem_io_free(bdev, bo->resource);
425 if (!dma_resv_test_signaled(bo->base.resv, true) ||
426 !dma_resv_trylock(bo->base.resv)) {
427 /* The BO is not idle, resurrect it for delayed destroy */
428 ttm_bo_flush_all_fences(bo);
431 spin_lock(&bo->bdev->lru_lock);
434 * Make pinned bos immediately available to
435 * shrinkers, now that they are queued for
438 * FIXME: QXL is triggering this. Can be removed when the
443 ttm_bo_move_to_lru_tail(bo, bo->resource, NULL);
446 kref_init(&bo->kref);
447 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
448 spin_unlock(&bo->bdev->lru_lock);
450 schedule_delayed_work(&bdev->wq,
451 ((HZ / 100) < 1) ? 1 : HZ / 100);
455 spin_lock(&bo->bdev->lru_lock);
456 ttm_bo_del_from_lru(bo);
457 list_del(&bo->ddestroy);
458 spin_unlock(&bo->bdev->lru_lock);
460 ttm_bo_cleanup_memtype_use(bo);
461 dma_resv_unlock(bo->base.resv);
463 atomic_dec(&ttm_glob.bo_count);
464 dma_fence_put(bo->moving);
468 void ttm_bo_put(struct ttm_buffer_object *bo)
470 kref_put(&bo->kref, ttm_bo_release);
472 EXPORT_SYMBOL(ttm_bo_put);
474 int ttm_bo_lock_delayed_workqueue(struct ttm_device *bdev)
476 return cancel_delayed_work_sync(&bdev->wq);
478 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
480 void ttm_bo_unlock_delayed_workqueue(struct ttm_device *bdev, int resched)
483 schedule_delayed_work(&bdev->wq,
484 ((HZ / 100) < 1) ? 1 : HZ / 100);
486 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
488 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
489 struct ttm_resource **mem,
490 struct ttm_operation_ctx *ctx,
491 struct ttm_place *hop)
493 struct ttm_placement hop_placement;
494 struct ttm_resource *hop_mem;
497 hop_placement.num_placement = hop_placement.num_busy_placement = 1;
498 hop_placement.placement = hop_placement.busy_placement = hop;
500 /* find space in the bounce domain */
501 ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
504 /* move to the bounce domain */
505 ret = ttm_bo_handle_move_mem(bo, hop_mem, false, ctx, NULL);
507 ttm_resource_free(bo, &hop_mem);
513 static int ttm_bo_evict(struct ttm_buffer_object *bo,
514 struct ttm_operation_ctx *ctx)
516 struct ttm_device *bdev = bo->bdev;
517 struct ttm_resource *evict_mem;
518 struct ttm_placement placement;
519 struct ttm_place hop;
522 memset(&hop, 0, sizeof(hop));
524 dma_resv_assert_held(bo->base.resv);
526 placement.num_placement = 0;
527 placement.num_busy_placement = 0;
528 bdev->funcs->evict_flags(bo, &placement);
530 if (!placement.num_placement && !placement.num_busy_placement) {
531 ret = ttm_bo_wait(bo, true, false);
536 * Since we've already synced, this frees backing store
539 return ttm_bo_pipeline_gutting(bo);
542 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
544 if (ret != -ERESTARTSYS) {
545 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
547 ttm_bo_mem_space_debug(bo, &placement);
553 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
554 if (ret == -EMULTIHOP) {
555 ret = ttm_bo_bounce_temp_buffer(bo, &evict_mem, ctx, &hop);
557 pr_err("Buffer eviction failed\n");
558 ttm_resource_free(bo, &evict_mem);
561 /* try and move to final place now. */
568 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
569 const struct ttm_place *place)
571 dma_resv_assert_held(bo->base.resv);
572 if (bo->resource->mem_type == TTM_PL_SYSTEM)
575 /* Don't evict this BO if it's outside of the
576 * requested placement range
578 if (place->fpfn >= (bo->resource->start + bo->resource->num_pages) ||
579 (place->lpfn && place->lpfn <= bo->resource->start))
584 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
587 * Check the target bo is allowable to be evicted or swapout, including cases:
589 * a. if share same reservation object with ctx->resv, have assumption
590 * reservation objects should already be locked, so not lock again and
591 * return true directly when either the opreation allow_reserved_eviction
592 * or the target bo already is in delayed free list;
594 * b. Otherwise, trylock it.
596 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
597 struct ttm_operation_ctx *ctx,
598 const struct ttm_place *place,
599 bool *locked, bool *busy)
603 if (bo->base.resv == ctx->resv) {
604 dma_resv_assert_held(bo->base.resv);
605 if (ctx->allow_res_evict)
611 ret = dma_resv_trylock(bo->base.resv);
617 if (ret && place && !bo->bdev->funcs->eviction_valuable(bo, place)) {
620 dma_resv_unlock(bo->base.resv);
629 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
631 * @busy_bo: BO which couldn't be locked with trylock
632 * @ctx: operation context
633 * @ticket: acquire ticket
635 * Try to lock a busy buffer object to avoid failing eviction.
637 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
638 struct ttm_operation_ctx *ctx,
639 struct ww_acquire_ctx *ticket)
643 if (!busy_bo || !ticket)
646 if (ctx->interruptible)
647 r = dma_resv_lock_interruptible(busy_bo->base.resv,
650 r = dma_resv_lock(busy_bo->base.resv, ticket);
653 * TODO: It would be better to keep the BO locked until allocation is at
654 * least tried one more time, but that would mean a much larger rework
658 dma_resv_unlock(busy_bo->base.resv);
660 return r == -EDEADLK ? -EBUSY : r;
663 int ttm_mem_evict_first(struct ttm_device *bdev,
664 struct ttm_resource_manager *man,
665 const struct ttm_place *place,
666 struct ttm_operation_ctx *ctx,
667 struct ww_acquire_ctx *ticket)
669 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
674 spin_lock(&bdev->lru_lock);
675 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
676 list_for_each_entry(bo, &man->lru[i], lru) {
679 if (!ttm_bo_evict_swapout_allowable(bo, ctx, place,
681 if (busy && !busy_bo && ticket !=
682 dma_resv_locking_ctx(bo->base.resv))
687 if (!ttm_bo_get_unless_zero(bo)) {
689 dma_resv_unlock(bo->base.resv);
695 /* If the inner loop terminated early, we have our candidate */
696 if (&bo->lru != &man->lru[i])
703 if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
705 spin_unlock(&bdev->lru_lock);
706 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
713 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
714 ctx->no_wait_gpu, locked);
719 spin_unlock(&bdev->lru_lock);
721 ret = ttm_bo_evict(bo, ctx);
723 ttm_bo_unreserve(bo);
730 * Add the last move fence to the BO and reserve a new shared slot. We only use
731 * a shared slot to avoid unecessary sync and rely on the subsequent bo move to
732 * either stall or use an exclusive fence respectively set bo->moving.
734 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
735 struct ttm_resource_manager *man,
736 struct ttm_resource *mem,
739 struct dma_fence *fence;
742 spin_lock(&man->move_lock);
743 fence = dma_fence_get(man->move);
744 spin_unlock(&man->move_lock);
750 ret = dma_fence_is_signaled(fence) ? 0 : -EBUSY;
751 dma_fence_put(fence);
755 dma_resv_add_shared_fence(bo->base.resv, fence);
757 ret = dma_resv_reserve_shared(bo->base.resv, 1);
759 dma_fence_put(fence);
763 dma_fence_put(bo->moving);
769 * Repeatedly evict memory from the LRU for @mem_type until we create enough
770 * space, or we've evicted everything and there isn't enough space.
772 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
773 const struct ttm_place *place,
774 struct ttm_resource **mem,
775 struct ttm_operation_ctx *ctx)
777 struct ttm_device *bdev = bo->bdev;
778 struct ttm_resource_manager *man;
779 struct ww_acquire_ctx *ticket;
782 man = ttm_manager_type(bdev, place->mem_type);
783 ticket = dma_resv_locking_ctx(bo->base.resv);
785 ret = ttm_resource_alloc(bo, place, mem);
788 if (unlikely(ret != -ENOSPC))
790 ret = ttm_mem_evict_first(bdev, man, place, ctx,
792 if (unlikely(ret != 0))
796 return ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
800 * Creates space for memory region @mem according to its type.
802 * This function first searches for free space in compatible memory types in
803 * the priority order defined by the driver. If free space isn't found, then
804 * ttm_bo_mem_force_space is attempted in priority order to evict and find
807 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
808 struct ttm_placement *placement,
809 struct ttm_resource **mem,
810 struct ttm_operation_ctx *ctx)
812 struct ttm_device *bdev = bo->bdev;
813 bool type_found = false;
816 ret = dma_resv_reserve_shared(bo->base.resv, 1);
820 for (i = 0; i < placement->num_placement; ++i) {
821 const struct ttm_place *place = &placement->placement[i];
822 struct ttm_resource_manager *man;
824 man = ttm_manager_type(bdev, place->mem_type);
825 if (!man || !ttm_resource_manager_used(man))
829 ret = ttm_resource_alloc(bo, place, mem);
835 ret = ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
837 ttm_resource_free(bo, mem);
846 for (i = 0; i < placement->num_busy_placement; ++i) {
847 const struct ttm_place *place = &placement->busy_placement[i];
848 struct ttm_resource_manager *man;
850 man = ttm_manager_type(bdev, place->mem_type);
851 if (!man || !ttm_resource_manager_used(man))
855 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
859 if (ret && ret != -EBUSY)
865 pr_err(TTM_PFX "No compatible memory type found\n");
870 if (bo->resource->mem_type == TTM_PL_SYSTEM && !bo->pin_count)
871 ttm_bo_move_to_lru_tail_unlocked(bo);
875 EXPORT_SYMBOL(ttm_bo_mem_space);
877 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
878 struct ttm_placement *placement,
879 struct ttm_operation_ctx *ctx)
881 struct ttm_resource *mem;
882 struct ttm_place hop;
885 dma_resv_assert_held(bo->base.resv);
888 * Determine where to move the buffer.
890 * If driver determines move is going to need
891 * an extra step then it will return -EMULTIHOP
892 * and the buffer will be moved to the temporary
893 * stop and the driver will be called to make
896 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
900 ret = ttm_bo_handle_move_mem(bo, mem, false, ctx, &hop);
901 if (ret == -EMULTIHOP) {
902 ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
905 /* try and move to final place now. */
910 ttm_resource_free(bo, &mem);
914 static bool ttm_bo_places_compat(const struct ttm_place *places,
915 unsigned num_placement,
916 struct ttm_resource *mem,
921 if (mem->placement & TTM_PL_FLAG_TEMPORARY)
924 for (i = 0; i < num_placement; i++) {
925 const struct ttm_place *heap = &places[i];
927 if ((mem->start < heap->fpfn ||
928 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
931 *new_flags = heap->flags;
932 if ((mem->mem_type == heap->mem_type) &&
933 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
934 (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
940 bool ttm_bo_mem_compat(struct ttm_placement *placement,
941 struct ttm_resource *mem,
944 if (ttm_bo_places_compat(placement->placement, placement->num_placement,
948 if ((placement->busy_placement != placement->placement ||
949 placement->num_busy_placement > placement->num_placement) &&
950 ttm_bo_places_compat(placement->busy_placement,
951 placement->num_busy_placement,
957 EXPORT_SYMBOL(ttm_bo_mem_compat);
959 int ttm_bo_validate(struct ttm_buffer_object *bo,
960 struct ttm_placement *placement,
961 struct ttm_operation_ctx *ctx)
966 dma_resv_assert_held(bo->base.resv);
969 * Remove the backing store if no placement is given.
971 if (!placement->num_placement && !placement->num_busy_placement)
972 return ttm_bo_pipeline_gutting(bo);
975 * Check whether we need to move buffer.
977 if (!ttm_bo_mem_compat(placement, bo->resource, &new_flags)) {
978 ret = ttm_bo_move_buffer(bo, placement, ctx);
983 * We might need to add a TTM.
985 if (bo->resource->mem_type == TTM_PL_SYSTEM) {
986 ret = ttm_tt_create(bo, true);
992 EXPORT_SYMBOL(ttm_bo_validate);
994 int ttm_bo_init_reserved(struct ttm_device *bdev,
995 struct ttm_buffer_object *bo,
997 enum ttm_bo_type type,
998 struct ttm_placement *placement,
999 uint32_t page_alignment,
1000 struct ttm_operation_ctx *ctx,
1001 struct sg_table *sg,
1002 struct dma_resv *resv,
1003 void (*destroy) (struct ttm_buffer_object *))
1005 static const struct ttm_place sys_mem = { .mem_type = TTM_PL_SYSTEM };
1009 bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1011 kref_init(&bo->kref);
1012 INIT_LIST_HEAD(&bo->lru);
1013 INIT_LIST_HEAD(&bo->ddestroy);
1016 bo->page_alignment = page_alignment;
1021 bo->base.resv = resv;
1022 dma_resv_assert_held(bo->base.resv);
1024 bo->base.resv = &bo->base._resv;
1026 atomic_inc(&ttm_glob.bo_count);
1028 ret = ttm_resource_alloc(bo, &sys_mem, &bo->resource);
1029 if (unlikely(ret)) {
1035 * For ttm_bo_type_device buffers, allocate
1036 * address space from the device.
1038 if (bo->type == ttm_bo_type_device ||
1039 bo->type == ttm_bo_type_sg)
1040 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
1041 bo->resource->num_pages);
1043 /* passed reservation objects should already be locked,
1044 * since otherwise lockdep will be angered in radeon.
1047 locked = dma_resv_trylock(bo->base.resv);
1052 ret = ttm_bo_validate(bo, placement, ctx);
1054 if (unlikely(ret)) {
1056 ttm_bo_unreserve(bo);
1062 ttm_bo_move_to_lru_tail_unlocked(bo);
1066 EXPORT_SYMBOL(ttm_bo_init_reserved);
1068 int ttm_bo_init(struct ttm_device *bdev,
1069 struct ttm_buffer_object *bo,
1071 enum ttm_bo_type type,
1072 struct ttm_placement *placement,
1073 uint32_t page_alignment,
1075 struct sg_table *sg,
1076 struct dma_resv *resv,
1077 void (*destroy) (struct ttm_buffer_object *))
1079 struct ttm_operation_ctx ctx = { interruptible, false };
1082 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1083 page_alignment, &ctx, sg, resv, destroy);
1088 ttm_bo_unreserve(bo);
1092 EXPORT_SYMBOL(ttm_bo_init);
1095 * buffer object vm functions.
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);
1107 int ttm_bo_wait(struct ttm_buffer_object *bo,
1108 bool interruptible, bool no_wait)
1110 long timeout = 15 * HZ;
1113 if (dma_resv_test_signaled(bo->base.resv, true))
1119 timeout = dma_resv_wait_timeout(bo->base.resv, true, interruptible,
1127 dma_resv_add_excl_fence(bo->base.resv, NULL);
1130 EXPORT_SYMBOL(ttm_bo_wait);
1132 int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
1135 struct ttm_place place;
1140 * While the bo may already reside in SYSTEM placement, set
1141 * SYSTEM as new placement to cover also the move further below.
1142 * The driver may use the fact that we're moving from SYSTEM
1143 * as an indication that we're about to swap out.
1145 memset(&place, 0, sizeof(place));
1146 place.mem_type = TTM_PL_SYSTEM;
1147 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place, &locked, NULL))
1150 if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
1151 bo->ttm->page_flags & TTM_PAGE_FLAG_SG ||
1152 bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED ||
1153 !ttm_bo_get_unless_zero(bo)) {
1155 dma_resv_unlock(bo->base.resv);
1160 ttm_bo_cleanup_refs(bo, false, false, locked);
1165 ttm_bo_del_from_lru(bo);
1166 /* TODO: Cleanup the locking */
1167 spin_unlock(&bo->bdev->lru_lock);
1170 * Move to system cached
1172 if (bo->resource->mem_type != TTM_PL_SYSTEM) {
1173 struct ttm_operation_ctx ctx = { false, false };
1174 struct ttm_resource *evict_mem;
1175 struct ttm_place hop;
1177 memset(&hop, 0, sizeof(hop));
1178 ret = ttm_resource_alloc(bo, &place, &evict_mem);
1182 ret = ttm_bo_handle_move_mem(bo, evict_mem, true, &ctx, &hop);
1183 if (unlikely(ret != 0)) {
1184 WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
1190 * Make sure BO is idle.
1192 ret = ttm_bo_wait(bo, false, false);
1193 if (unlikely(ret != 0))
1196 ttm_bo_unmap_virtual(bo);
1199 * Swap out. Buffer will be swapped in again as soon as
1200 * anyone tries to access a ttm page.
1202 if (bo->bdev->funcs->swap_notify)
1203 bo->bdev->funcs->swap_notify(bo);
1205 if (ttm_tt_is_populated(bo->ttm))
1206 ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);
1210 * Unreserve without putting on LRU to avoid swapping out an
1211 * already swapped buffer.
1214 dma_resv_unlock(bo->base.resv);
1219 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1221 if (bo->ttm == NULL)
1224 ttm_tt_destroy(bo->bdev, bo->ttm);