1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
4 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
29 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
32 #define pr_fmt(fmt) "[TTM] " fmt
34 #include <drm/ttm/ttm_bo_driver.h>
35 #include <drm/ttm/ttm_placement.h>
36 #include <linux/jiffies.h>
37 #include <linux/slab.h>
38 #include <linux/sched.h>
40 #include <linux/file.h>
41 #include <linux/module.h>
42 #include <linux/atomic.h>
43 #include <linux/dma-resv.h>
45 #include "ttm_module.h"
47 static void ttm_bo_global_kobj_release(struct kobject *kobj);
50 * ttm_global_mutex - protecting the global BO state
52 DEFINE_MUTEX(ttm_global_mutex);
53 unsigned ttm_bo_glob_use_count;
54 struct ttm_bo_global ttm_bo_glob;
55 EXPORT_SYMBOL(ttm_bo_glob);
57 static struct attribute ttm_bo_count = {
62 /* default destructor */
63 static void ttm_bo_default_destroy(struct ttm_buffer_object *bo)
68 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
69 struct ttm_placement *placement)
71 struct drm_printer p = drm_debug_printer(TTM_PFX);
72 struct ttm_resource_manager *man;
75 drm_printf(&p, "No space for %p (%lu pages, %zuK, %zuM)\n",
76 bo, bo->mem.num_pages, bo->base.size >> 10,
78 for (i = 0; i < placement->num_placement; i++) {
79 mem_type = placement->placement[i].mem_type;
80 drm_printf(&p, " placement[%d]=0x%08X (%d)\n",
81 i, placement->placement[i].flags, mem_type);
82 man = ttm_manager_type(bo->bdev, mem_type);
83 ttm_resource_manager_debug(man, &p);
87 static ssize_t ttm_bo_global_show(struct kobject *kobj,
88 struct attribute *attr,
91 struct ttm_bo_global *glob =
92 container_of(kobj, struct ttm_bo_global, kobj);
94 return snprintf(buffer, PAGE_SIZE, "%d\n",
95 atomic_read(&glob->bo_count));
98 static struct attribute *ttm_bo_global_attrs[] = {
103 static const struct sysfs_ops ttm_bo_global_ops = {
104 .show = &ttm_bo_global_show
107 static struct kobj_type ttm_bo_glob_kobj_type = {
108 .release = &ttm_bo_global_kobj_release,
109 .sysfs_ops = &ttm_bo_global_ops,
110 .default_attrs = ttm_bo_global_attrs
113 static void ttm_bo_add_mem_to_lru(struct ttm_buffer_object *bo,
114 struct ttm_resource *mem)
116 struct ttm_bo_device *bdev = bo->bdev;
117 struct ttm_resource_manager *man;
119 if (!list_empty(&bo->lru) || bo->pin_count)
122 man = ttm_manager_type(bdev, mem->mem_type);
123 list_add_tail(&bo->lru, &man->lru[bo->priority]);
125 if (man->use_tt && bo->ttm &&
126 !(bo->ttm->page_flags & (TTM_PAGE_FLAG_SG |
127 TTM_PAGE_FLAG_SWAPPED))) {
128 list_add_tail(&bo->swap, &ttm_bo_glob.swap_lru[bo->priority]);
132 static void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
134 struct ttm_bo_device *bdev = bo->bdev;
137 if (!list_empty(&bo->swap)) {
138 list_del_init(&bo->swap);
141 if (!list_empty(&bo->lru)) {
142 list_del_init(&bo->lru);
146 if (notify && bdev->driver->del_from_lru_notify)
147 bdev->driver->del_from_lru_notify(bo);
150 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
151 struct ttm_buffer_object *bo)
158 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
159 struct ttm_lru_bulk_move *bulk)
161 dma_resv_assert_held(bo->base.resv);
163 ttm_bo_del_from_lru(bo);
164 ttm_bo_add_mem_to_lru(bo, &bo->mem);
166 if (bulk && !bo->pin_count) {
167 switch (bo->mem.mem_type) {
169 ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
173 ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
176 if (bo->ttm && !(bo->ttm->page_flags &
177 (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED)))
178 ttm_bo_bulk_move_set_pos(&bulk->swap[bo->priority], bo);
181 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
183 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
187 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
188 struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
189 struct ttm_resource_manager *man;
194 dma_resv_assert_held(pos->first->base.resv);
195 dma_resv_assert_held(pos->last->base.resv);
197 man = ttm_manager_type(pos->first->bdev, TTM_PL_TT);
198 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
202 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
203 struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
204 struct ttm_resource_manager *man;
209 dma_resv_assert_held(pos->first->base.resv);
210 dma_resv_assert_held(pos->last->base.resv);
212 man = ttm_manager_type(pos->first->bdev, TTM_PL_VRAM);
213 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
217 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
218 struct ttm_lru_bulk_move_pos *pos = &bulk->swap[i];
219 struct list_head *lru;
224 dma_resv_assert_held(pos->first->base.resv);
225 dma_resv_assert_held(pos->last->base.resv);
227 lru = &ttm_bo_glob.swap_lru[i];
228 list_bulk_move_tail(lru, &pos->first->swap, &pos->last->swap);
231 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
233 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
234 struct ttm_resource *mem, bool evict,
235 struct ttm_operation_ctx *ctx,
236 struct ttm_place *hop)
238 struct ttm_bo_device *bdev = bo->bdev;
239 struct ttm_resource_manager *old_man = ttm_manager_type(bdev, bo->mem.mem_type);
240 struct ttm_resource_manager *new_man = ttm_manager_type(bdev, mem->mem_type);
243 ttm_bo_unmap_virtual(bo);
246 * Create and bind a ttm if required.
249 if (new_man->use_tt) {
250 /* Zero init the new TTM structure if the old location should
251 * have used one as well.
253 ret = ttm_tt_create(bo, old_man->use_tt);
257 if (mem->mem_type != TTM_PL_SYSTEM) {
258 ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
264 ret = bdev->driver->move(bo, evict, ctx, mem, hop);
266 if (ret == -EMULTIHOP)
271 ctx->bytes_moved += bo->base.size;
275 new_man = ttm_manager_type(bdev, bo->mem.mem_type);
276 if (!new_man->use_tt)
277 ttm_bo_tt_destroy(bo);
284 * Will release GPU memory type usage on destruction.
285 * This is the place to put in driver specific hooks to release
286 * driver private resources.
287 * Will release the bo::reserved lock.
290 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
292 if (bo->bdev->driver->delete_mem_notify)
293 bo->bdev->driver->delete_mem_notify(bo);
295 ttm_bo_tt_destroy(bo);
296 ttm_resource_free(bo, &bo->mem);
299 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
303 if (bo->base.resv == &bo->base._resv)
306 BUG_ON(!dma_resv_trylock(&bo->base._resv));
308 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
309 dma_resv_unlock(&bo->base._resv);
313 if (bo->type != ttm_bo_type_sg) {
314 /* This works because the BO is about to be destroyed and nobody
315 * reference it any more. The only tricky case is the trylock on
316 * the resv object while holding the lru_lock.
318 spin_lock(&ttm_bo_glob.lru_lock);
319 bo->base.resv = &bo->base._resv;
320 spin_unlock(&ttm_bo_glob.lru_lock);
326 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
328 struct dma_resv *resv = &bo->base._resv;
329 struct dma_resv_list *fobj;
330 struct dma_fence *fence;
334 fobj = rcu_dereference(resv->fence);
335 fence = rcu_dereference(resv->fence_excl);
336 if (fence && !fence->ops->signaled)
337 dma_fence_enable_sw_signaling(fence);
339 for (i = 0; fobj && i < fobj->shared_count; ++i) {
340 fence = rcu_dereference(fobj->shared[i]);
342 if (!fence->ops->signaled)
343 dma_fence_enable_sw_signaling(fence);
349 * function ttm_bo_cleanup_refs
350 * If bo idle, remove from lru lists, and unref.
351 * If not idle, block if possible.
353 * Must be called with lru_lock and reservation held, this function
354 * will drop the lru lock and optionally the reservation lock before returning.
356 * @bo: The buffer object to clean-up
357 * @interruptible: Any sleeps should occur interruptibly.
358 * @no_wait_gpu: Never wait for gpu. Return -EBUSY instead.
359 * @unlock_resv: Unlock the reservation lock as well.
362 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
363 bool interruptible, bool no_wait_gpu,
366 struct dma_resv *resv = &bo->base._resv;
369 if (dma_resv_test_signaled_rcu(resv, true))
374 if (ret && !no_wait_gpu) {
378 dma_resv_unlock(bo->base.resv);
379 spin_unlock(&ttm_bo_glob.lru_lock);
381 lret = dma_resv_wait_timeout_rcu(resv, true, interruptible,
389 spin_lock(&ttm_bo_glob.lru_lock);
390 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
392 * We raced, and lost, someone else holds the reservation now,
393 * and is probably busy in ttm_bo_cleanup_memtype_use.
395 * Even if it's not the case, because we finished waiting any
396 * delayed destruction would succeed, so just return success
399 spin_unlock(&ttm_bo_glob.lru_lock);
405 if (ret || unlikely(list_empty(&bo->ddestroy))) {
407 dma_resv_unlock(bo->base.resv);
408 spin_unlock(&ttm_bo_glob.lru_lock);
412 ttm_bo_del_from_lru(bo);
413 list_del_init(&bo->ddestroy);
414 spin_unlock(&ttm_bo_glob.lru_lock);
415 ttm_bo_cleanup_memtype_use(bo);
418 dma_resv_unlock(bo->base.resv);
426 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
427 * encountered buffers.
429 static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
431 struct ttm_bo_global *glob = &ttm_bo_glob;
432 struct list_head removed;
435 INIT_LIST_HEAD(&removed);
437 spin_lock(&glob->lru_lock);
438 while (!list_empty(&bdev->ddestroy)) {
439 struct ttm_buffer_object *bo;
441 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
443 list_move_tail(&bo->ddestroy, &removed);
444 if (!ttm_bo_get_unless_zero(bo))
447 if (remove_all || bo->base.resv != &bo->base._resv) {
448 spin_unlock(&glob->lru_lock);
449 dma_resv_lock(bo->base.resv, NULL);
451 spin_lock(&glob->lru_lock);
452 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
454 } else if (dma_resv_trylock(bo->base.resv)) {
455 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
457 spin_unlock(&glob->lru_lock);
461 spin_lock(&glob->lru_lock);
463 list_splice_tail(&removed, &bdev->ddestroy);
464 empty = list_empty(&bdev->ddestroy);
465 spin_unlock(&glob->lru_lock);
470 static void ttm_bo_delayed_workqueue(struct work_struct *work)
472 struct ttm_bo_device *bdev =
473 container_of(work, struct ttm_bo_device, wq.work);
475 if (!ttm_bo_delayed_delete(bdev, false))
476 schedule_delayed_work(&bdev->wq,
477 ((HZ / 100) < 1) ? 1 : HZ / 100);
480 static void ttm_bo_release(struct kref *kref)
482 struct ttm_buffer_object *bo =
483 container_of(kref, struct ttm_buffer_object, kref);
484 struct ttm_bo_device *bdev = bo->bdev;
485 size_t acc_size = bo->acc_size;
489 ret = ttm_bo_individualize_resv(bo);
491 /* Last resort, if we fail to allocate memory for the
492 * fences block for the BO to become idle
494 dma_resv_wait_timeout_rcu(bo->base.resv, true, false,
498 if (bo->bdev->driver->release_notify)
499 bo->bdev->driver->release_notify(bo);
501 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
502 ttm_mem_io_free(bdev, &bo->mem);
505 if (!dma_resv_test_signaled_rcu(bo->base.resv, true) ||
506 !dma_resv_trylock(bo->base.resv)) {
507 /* The BO is not idle, resurrect it for delayed destroy */
508 ttm_bo_flush_all_fences(bo);
511 spin_lock(&ttm_bo_glob.lru_lock);
514 * Make pinned bos immediately available to
515 * shrinkers, now that they are queued for
518 if (WARN_ON(bo->pin_count)) {
520 ttm_bo_del_from_lru(bo);
521 ttm_bo_add_mem_to_lru(bo, &bo->mem);
524 kref_init(&bo->kref);
525 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
526 spin_unlock(&ttm_bo_glob.lru_lock);
528 schedule_delayed_work(&bdev->wq,
529 ((HZ / 100) < 1) ? 1 : HZ / 100);
533 spin_lock(&ttm_bo_glob.lru_lock);
534 ttm_bo_del_from_lru(bo);
535 list_del(&bo->ddestroy);
536 spin_unlock(&ttm_bo_glob.lru_lock);
538 ttm_bo_cleanup_memtype_use(bo);
539 dma_resv_unlock(bo->base.resv);
541 atomic_dec(&ttm_bo_glob.bo_count);
542 dma_fence_put(bo->moving);
543 if (!ttm_bo_uses_embedded_gem_object(bo))
544 dma_resv_fini(&bo->base._resv);
546 ttm_mem_global_free(&ttm_mem_glob, acc_size);
549 void ttm_bo_put(struct ttm_buffer_object *bo)
551 kref_put(&bo->kref, ttm_bo_release);
553 EXPORT_SYMBOL(ttm_bo_put);
555 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
557 return cancel_delayed_work_sync(&bdev->wq);
559 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
561 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
564 schedule_delayed_work(&bdev->wq,
565 ((HZ / 100) < 1) ? 1 : HZ / 100);
567 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
569 static int ttm_bo_evict(struct ttm_buffer_object *bo,
570 struct ttm_operation_ctx *ctx)
572 struct ttm_bo_device *bdev = bo->bdev;
573 struct ttm_resource evict_mem;
574 struct ttm_placement placement;
575 struct ttm_place hop;
578 memset(&hop, 0, sizeof(hop));
580 dma_resv_assert_held(bo->base.resv);
582 placement.num_placement = 0;
583 placement.num_busy_placement = 0;
584 bdev->driver->evict_flags(bo, &placement);
586 if (!placement.num_placement && !placement.num_busy_placement) {
587 ttm_bo_wait(bo, false, false);
589 ttm_bo_cleanup_memtype_use(bo);
590 return ttm_tt_create(bo, false);
594 evict_mem.mm_node = NULL;
595 evict_mem.bus.offset = 0;
596 evict_mem.bus.addr = NULL;
598 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
600 if (ret != -ERESTARTSYS) {
601 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
603 ttm_bo_mem_space_debug(bo, &placement);
608 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx, &hop);
610 WARN(ret == -EMULTIHOP, "Unexpected multihop in eviction - likely driver bug\n");
611 if (ret != -ERESTARTSYS)
612 pr_err("Buffer eviction failed\n");
613 ttm_resource_free(bo, &evict_mem);
619 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
620 const struct ttm_place *place)
622 /* Don't evict this BO if it's outside of the
623 * requested placement range
625 if (place->fpfn >= (bo->mem.start + bo->mem.num_pages) ||
626 (place->lpfn && place->lpfn <= bo->mem.start))
631 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
634 * Check the target bo is allowable to be evicted or swapout, including cases:
636 * a. if share same reservation object with ctx->resv, have assumption
637 * reservation objects should already be locked, so not lock again and
638 * return true directly when either the opreation allow_reserved_eviction
639 * or the target bo already is in delayed free list;
641 * b. Otherwise, trylock it.
643 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
644 struct ttm_operation_ctx *ctx, bool *locked, bool *busy)
648 if (bo->base.resv == ctx->resv) {
649 dma_resv_assert_held(bo->base.resv);
650 if (ctx->allow_res_evict)
656 ret = dma_resv_trylock(bo->base.resv);
666 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
668 * @busy_bo: BO which couldn't be locked with trylock
669 * @ctx: operation context
670 * @ticket: acquire ticket
672 * Try to lock a busy buffer object to avoid failing eviction.
674 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
675 struct ttm_operation_ctx *ctx,
676 struct ww_acquire_ctx *ticket)
680 if (!busy_bo || !ticket)
683 if (ctx->interruptible)
684 r = dma_resv_lock_interruptible(busy_bo->base.resv,
687 r = dma_resv_lock(busy_bo->base.resv, ticket);
690 * TODO: It would be better to keep the BO locked until allocation is at
691 * least tried one more time, but that would mean a much larger rework
695 dma_resv_unlock(busy_bo->base.resv);
697 return r == -EDEADLK ? -EBUSY : r;
700 int ttm_mem_evict_first(struct ttm_bo_device *bdev,
701 struct ttm_resource_manager *man,
702 const struct ttm_place *place,
703 struct ttm_operation_ctx *ctx,
704 struct ww_acquire_ctx *ticket)
706 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
711 spin_lock(&ttm_bo_glob.lru_lock);
712 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
713 list_for_each_entry(bo, &man->lru[i], lru) {
716 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
718 if (busy && !busy_bo && ticket !=
719 dma_resv_locking_ctx(bo->base.resv))
724 if (place && !bdev->driver->eviction_valuable(bo,
727 dma_resv_unlock(bo->base.resv);
730 if (!ttm_bo_get_unless_zero(bo)) {
732 dma_resv_unlock(bo->base.resv);
738 /* If the inner loop terminated early, we have our candidate */
739 if (&bo->lru != &man->lru[i])
746 if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
748 spin_unlock(&ttm_bo_glob.lru_lock);
749 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
756 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
757 ctx->no_wait_gpu, locked);
762 spin_unlock(&ttm_bo_glob.lru_lock);
764 ret = ttm_bo_evict(bo, ctx);
766 ttm_bo_unreserve(bo);
773 * Add the last move fence to the BO and reserve a new shared slot.
775 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
776 struct ttm_resource_manager *man,
777 struct ttm_resource *mem,
780 struct dma_fence *fence;
783 spin_lock(&man->move_lock);
784 fence = dma_fence_get(man->move);
785 spin_unlock(&man->move_lock);
791 dma_fence_put(fence);
795 dma_resv_add_shared_fence(bo->base.resv, fence);
797 ret = dma_resv_reserve_shared(bo->base.resv, 1);
799 dma_fence_put(fence);
803 dma_fence_put(bo->moving);
809 * Repeatedly evict memory from the LRU for @mem_type until we create enough
810 * space, or we've evicted everything and there isn't enough space.
812 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
813 const struct ttm_place *place,
814 struct ttm_resource *mem,
815 struct ttm_operation_ctx *ctx)
817 struct ttm_bo_device *bdev = bo->bdev;
818 struct ttm_resource_manager *man = ttm_manager_type(bdev, mem->mem_type);
819 struct ww_acquire_ctx *ticket;
822 ticket = dma_resv_locking_ctx(bo->base.resv);
824 ret = ttm_resource_alloc(bo, place, mem);
827 if (unlikely(ret != -ENOSPC))
829 ret = ttm_mem_evict_first(bdev, man, place, ctx,
831 if (unlikely(ret != 0))
835 return ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
839 * ttm_bo_mem_placement - check if placement is compatible
840 * @bo: BO to find memory for
841 * @place: where to search
842 * @mem: the memory object to fill in
844 * Check if placement is compatible and fill in mem structure.
845 * Returns -EBUSY if placement won't work or negative error code.
846 * 0 when placement can be used.
848 static int ttm_bo_mem_placement(struct ttm_buffer_object *bo,
849 const struct ttm_place *place,
850 struct ttm_resource *mem)
852 struct ttm_bo_device *bdev = bo->bdev;
853 struct ttm_resource_manager *man;
855 man = ttm_manager_type(bdev, place->mem_type);
856 if (!man || !ttm_resource_manager_used(man))
859 mem->mem_type = place->mem_type;
860 mem->placement = place->flags;
862 spin_lock(&ttm_bo_glob.lru_lock);
863 ttm_bo_del_from_lru(bo);
864 ttm_bo_add_mem_to_lru(bo, mem);
865 spin_unlock(&ttm_bo_glob.lru_lock);
871 * Creates space for memory region @mem according to its type.
873 * This function first searches for free space in compatible memory types in
874 * the priority order defined by the driver. If free space isn't found, then
875 * ttm_bo_mem_force_space is attempted in priority order to evict and find
878 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
879 struct ttm_placement *placement,
880 struct ttm_resource *mem,
881 struct ttm_operation_ctx *ctx)
883 struct ttm_bo_device *bdev = bo->bdev;
884 bool type_found = false;
887 ret = dma_resv_reserve_shared(bo->base.resv, 1);
891 for (i = 0; i < placement->num_placement; ++i) {
892 const struct ttm_place *place = &placement->placement[i];
893 struct ttm_resource_manager *man;
895 ret = ttm_bo_mem_placement(bo, place, mem);
900 ret = ttm_resource_alloc(bo, place, mem);
906 man = ttm_manager_type(bdev, mem->mem_type);
907 ret = ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
909 ttm_resource_free(bo, mem);
918 for (i = 0; i < placement->num_busy_placement; ++i) {
919 const struct ttm_place *place = &placement->busy_placement[i];
921 ret = ttm_bo_mem_placement(bo, place, mem);
926 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
930 if (ret && ret != -EBUSY)
936 pr_err(TTM_PFX "No compatible memory type found\n");
941 if (bo->mem.mem_type == TTM_PL_SYSTEM && !bo->pin_count)
942 ttm_bo_move_to_lru_tail_unlocked(bo);
946 EXPORT_SYMBOL(ttm_bo_mem_space);
948 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
949 struct ttm_resource *mem,
950 struct ttm_operation_ctx *ctx,
951 struct ttm_place *hop)
953 struct ttm_placement hop_placement;
955 struct ttm_resource hop_mem = *mem;
957 hop_mem.mm_node = NULL;
958 hop_mem.mem_type = TTM_PL_SYSTEM;
959 hop_mem.placement = 0;
961 hop_placement.num_placement = hop_placement.num_busy_placement = 1;
962 hop_placement.placement = hop_placement.busy_placement = hop;
964 /* find space in the bounce domain */
965 ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
968 /* move to the bounce domain */
969 ret = ttm_bo_handle_move_mem(bo, &hop_mem, false, ctx, NULL);
975 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
976 struct ttm_placement *placement,
977 struct ttm_operation_ctx *ctx)
980 struct ttm_place hop;
981 struct ttm_resource mem;
983 dma_resv_assert_held(bo->base.resv);
985 memset(&hop, 0, sizeof(hop));
987 mem.num_pages = PAGE_ALIGN(bo->base.size) >> PAGE_SHIFT;
988 mem.page_alignment = bo->mem.page_alignment;
994 * Determine where to move the buffer.
996 * If driver determines move is going to need
997 * an extra step then it will return -EMULTIHOP
998 * and the buffer will be moved to the temporary
999 * stop and the driver will be called to make
1003 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
1006 ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx, &hop);
1007 if (ret == -EMULTIHOP) {
1008 ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
1011 /* try and move to final place now. */
1015 ttm_resource_free(bo, &mem);
1019 static bool ttm_bo_places_compat(const struct ttm_place *places,
1020 unsigned num_placement,
1021 struct ttm_resource *mem,
1022 uint32_t *new_flags)
1026 for (i = 0; i < num_placement; i++) {
1027 const struct ttm_place *heap = &places[i];
1029 if ((mem->start < heap->fpfn ||
1030 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1033 *new_flags = heap->flags;
1034 if ((mem->mem_type == heap->mem_type) &&
1035 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
1036 (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
1042 bool ttm_bo_mem_compat(struct ttm_placement *placement,
1043 struct ttm_resource *mem,
1044 uint32_t *new_flags)
1046 if (ttm_bo_places_compat(placement->placement, placement->num_placement,
1050 if ((placement->busy_placement != placement->placement ||
1051 placement->num_busy_placement > placement->num_placement) &&
1052 ttm_bo_places_compat(placement->busy_placement,
1053 placement->num_busy_placement,
1059 EXPORT_SYMBOL(ttm_bo_mem_compat);
1061 int ttm_bo_validate(struct ttm_buffer_object *bo,
1062 struct ttm_placement *placement,
1063 struct ttm_operation_ctx *ctx)
1068 dma_resv_assert_held(bo->base.resv);
1071 * Remove the backing store if no placement is given.
1073 if (!placement->num_placement && !placement->num_busy_placement) {
1074 ret = ttm_bo_pipeline_gutting(bo);
1078 return ttm_tt_create(bo, false);
1082 * Check whether we need to move buffer.
1084 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1085 ret = ttm_bo_move_buffer(bo, placement, ctx);
1090 * We might need to add a TTM.
1092 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
1093 ret = ttm_tt_create(bo, true);
1099 EXPORT_SYMBOL(ttm_bo_validate);
1101 int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
1102 struct ttm_buffer_object *bo,
1104 enum ttm_bo_type type,
1105 struct ttm_placement *placement,
1106 uint32_t page_alignment,
1107 struct ttm_operation_ctx *ctx,
1109 struct sg_table *sg,
1110 struct dma_resv *resv,
1111 void (*destroy) (struct ttm_buffer_object *))
1113 struct ttm_mem_global *mem_glob = &ttm_mem_glob;
1117 ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
1119 pr_err("Out of kernel memory\n");
1127 bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1129 kref_init(&bo->kref);
1130 INIT_LIST_HEAD(&bo->lru);
1131 INIT_LIST_HEAD(&bo->ddestroy);
1132 INIT_LIST_HEAD(&bo->swap);
1135 bo->mem.mem_type = TTM_PL_SYSTEM;
1136 bo->mem.num_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1137 bo->mem.mm_node = NULL;
1138 bo->mem.page_alignment = page_alignment;
1139 bo->mem.bus.offset = 0;
1140 bo->mem.bus.addr = NULL;
1142 bo->mem.placement = 0;
1143 bo->acc_size = acc_size;
1147 bo->base.resv = resv;
1148 dma_resv_assert_held(bo->base.resv);
1150 bo->base.resv = &bo->base._resv;
1152 if (!ttm_bo_uses_embedded_gem_object(bo)) {
1154 * bo.base is not initialized, so we have to setup the
1155 * struct elements we want use regardless.
1157 bo->base.size = size;
1158 dma_resv_init(&bo->base._resv);
1159 drm_vma_node_reset(&bo->base.vma_node);
1161 atomic_inc(&ttm_bo_glob.bo_count);
1164 * For ttm_bo_type_device buffers, allocate
1165 * address space from the device.
1167 if (bo->type == ttm_bo_type_device ||
1168 bo->type == ttm_bo_type_sg)
1169 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
1172 /* passed reservation objects should already be locked,
1173 * since otherwise lockdep will be angered in radeon.
1176 locked = dma_resv_trylock(bo->base.resv);
1181 ret = ttm_bo_validate(bo, placement, ctx);
1183 if (unlikely(ret)) {
1185 ttm_bo_unreserve(bo);
1191 ttm_bo_move_to_lru_tail_unlocked(bo);
1195 EXPORT_SYMBOL(ttm_bo_init_reserved);
1197 int ttm_bo_init(struct ttm_bo_device *bdev,
1198 struct ttm_buffer_object *bo,
1200 enum ttm_bo_type type,
1201 struct ttm_placement *placement,
1202 uint32_t page_alignment,
1205 struct sg_table *sg,
1206 struct dma_resv *resv,
1207 void (*destroy) (struct ttm_buffer_object *))
1209 struct ttm_operation_ctx ctx = { interruptible, false };
1212 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1213 page_alignment, &ctx, acc_size,
1219 ttm_bo_unreserve(bo);
1223 EXPORT_SYMBOL(ttm_bo_init);
1225 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1226 unsigned long bo_size,
1227 unsigned struct_size)
1229 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1232 size += ttm_round_pot(struct_size);
1233 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
1234 size += ttm_round_pot(sizeof(struct ttm_tt));
1237 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1239 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1241 struct ttm_bo_global *glob =
1242 container_of(kobj, struct ttm_bo_global, kobj);
1244 __free_page(glob->dummy_read_page);
1247 static void ttm_bo_global_release(void)
1249 struct ttm_bo_global *glob = &ttm_bo_glob;
1251 mutex_lock(&ttm_global_mutex);
1252 if (--ttm_bo_glob_use_count > 0)
1255 kobject_del(&glob->kobj);
1256 kobject_put(&glob->kobj);
1257 ttm_mem_global_release(&ttm_mem_glob);
1258 memset(glob, 0, sizeof(*glob));
1260 mutex_unlock(&ttm_global_mutex);
1263 static int ttm_bo_global_init(void)
1265 struct ttm_bo_global *glob = &ttm_bo_glob;
1269 mutex_lock(&ttm_global_mutex);
1270 if (++ttm_bo_glob_use_count > 1)
1273 ret = ttm_mem_global_init(&ttm_mem_glob);
1277 spin_lock_init(&glob->lru_lock);
1278 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1280 if (unlikely(glob->dummy_read_page == NULL)) {
1285 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1286 INIT_LIST_HEAD(&glob->swap_lru[i]);
1287 INIT_LIST_HEAD(&glob->device_list);
1288 atomic_set(&glob->bo_count, 0);
1290 ret = kobject_init_and_add(
1291 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1292 if (unlikely(ret != 0))
1293 kobject_put(&glob->kobj);
1295 mutex_unlock(&ttm_global_mutex);
1299 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1301 struct ttm_bo_global *glob = &ttm_bo_glob;
1304 struct ttm_resource_manager *man;
1306 man = ttm_manager_type(bdev, TTM_PL_SYSTEM);
1307 ttm_resource_manager_set_used(man, false);
1308 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL);
1310 mutex_lock(&ttm_global_mutex);
1311 list_del(&bdev->device_list);
1312 mutex_unlock(&ttm_global_mutex);
1314 cancel_delayed_work_sync(&bdev->wq);
1316 if (ttm_bo_delayed_delete(bdev, true))
1317 pr_debug("Delayed destroy list was clean\n");
1319 spin_lock(&glob->lru_lock);
1320 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1321 if (list_empty(&man->lru[0]))
1322 pr_debug("Swap list %d was clean\n", i);
1323 spin_unlock(&glob->lru_lock);
1325 ttm_pool_fini(&bdev->pool);
1328 ttm_bo_global_release();
1332 EXPORT_SYMBOL(ttm_bo_device_release);
1334 static void ttm_bo_init_sysman(struct ttm_bo_device *bdev)
1336 struct ttm_resource_manager *man = &bdev->sysman;
1339 * Initialize the system memory buffer type.
1340 * Other types need to be driver / IOCTL initialized.
1344 ttm_resource_manager_init(man, 0);
1345 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, man);
1346 ttm_resource_manager_set_used(man, true);
1349 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1350 struct ttm_bo_driver *driver,
1352 struct address_space *mapping,
1353 struct drm_vma_offset_manager *vma_manager,
1354 bool use_dma_alloc, bool use_dma32)
1356 struct ttm_bo_global *glob = &ttm_bo_glob;
1359 if (WARN_ON(vma_manager == NULL))
1362 ret = ttm_bo_global_init();
1366 bdev->driver = driver;
1368 ttm_bo_init_sysman(bdev);
1369 ttm_pool_init(&bdev->pool, dev, use_dma_alloc, use_dma32);
1371 bdev->vma_manager = vma_manager;
1372 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1373 INIT_LIST_HEAD(&bdev->ddestroy);
1374 bdev->dev_mapping = mapping;
1375 mutex_lock(&ttm_global_mutex);
1376 list_add_tail(&bdev->device_list, &glob->device_list);
1377 mutex_unlock(&ttm_global_mutex);
1381 EXPORT_SYMBOL(ttm_bo_device_init);
1384 * buffer object vm functions.
1387 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1389 struct ttm_bo_device *bdev = bo->bdev;
1391 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1392 ttm_mem_io_free(bdev, &bo->mem);
1394 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1396 int ttm_bo_wait(struct ttm_buffer_object *bo,
1397 bool interruptible, bool no_wait)
1399 long timeout = 15 * HZ;
1402 if (dma_resv_test_signaled_rcu(bo->base.resv, true))
1408 timeout = dma_resv_wait_timeout_rcu(bo->base.resv, true,
1409 interruptible, timeout);
1416 dma_resv_add_excl_fence(bo->base.resv, NULL);
1419 EXPORT_SYMBOL(ttm_bo_wait);
1422 * A buffer object shrink method that tries to swap out the first
1423 * buffer object on the bo_global::swap_lru list.
1425 int ttm_bo_swapout(struct ttm_operation_ctx *ctx)
1427 struct ttm_bo_global *glob = &ttm_bo_glob;
1428 struct ttm_buffer_object *bo;
1433 spin_lock(&glob->lru_lock);
1434 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1435 list_for_each_entry(bo, &glob->swap_lru[i], swap) {
1436 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
1440 if (!ttm_bo_get_unless_zero(bo)) {
1442 dma_resv_unlock(bo->base.resv);
1454 spin_unlock(&glob->lru_lock);
1459 ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1464 ttm_bo_del_from_lru(bo);
1465 spin_unlock(&glob->lru_lock);
1468 * Move to system cached
1471 if (bo->mem.mem_type != TTM_PL_SYSTEM) {
1472 struct ttm_operation_ctx ctx = { false, false };
1473 struct ttm_resource evict_mem;
1474 struct ttm_place hop;
1476 memset(&hop, 0, sizeof(hop));
1478 evict_mem = bo->mem;
1479 evict_mem.mm_node = NULL;
1480 evict_mem.placement = 0;
1481 evict_mem.mem_type = TTM_PL_SYSTEM;
1483 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx, &hop);
1484 if (unlikely(ret != 0)) {
1485 WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
1491 * Make sure BO is idle.
1494 ret = ttm_bo_wait(bo, false, false);
1495 if (unlikely(ret != 0))
1498 ttm_bo_unmap_virtual(bo);
1501 * Swap out. Buffer will be swapped in again as soon as
1502 * anyone tries to access a ttm page.
1505 if (bo->bdev->driver->swap_notify)
1506 bo->bdev->driver->swap_notify(bo);
1508 ret = ttm_tt_swapout(bo->bdev, bo->ttm);
1513 * Unreserve without putting on LRU to avoid swapping out an
1514 * already swapped buffer.
1517 dma_resv_unlock(bo->base.resv);
1521 EXPORT_SYMBOL(ttm_bo_swapout);
1523 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1525 if (bo->ttm == NULL)
1528 ttm_tt_destroy(bo->bdev, bo->ttm);