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_module.h>
35 #include <drm/ttm/ttm_bo_driver.h>
36 #include <drm/ttm/ttm_placement.h>
37 #include <linux/jiffies.h>
38 #include <linux/slab.h>
39 #include <linux/sched.h>
41 #include <linux/file.h>
42 #include <linux/module.h>
43 #include <linux/atomic.h>
44 #include <linux/dma-resv.h>
46 static void ttm_bo_global_kobj_release(struct kobject *kobj);
49 * ttm_global_mutex - protecting the global BO state
51 DEFINE_MUTEX(ttm_global_mutex);
52 unsigned ttm_bo_glob_use_count;
53 struct ttm_bo_global ttm_bo_glob;
54 EXPORT_SYMBOL(ttm_bo_glob);
56 static struct attribute ttm_bo_count = {
61 /* default destructor */
62 static void ttm_bo_default_destroy(struct ttm_buffer_object *bo)
67 static inline int ttm_mem_type_from_place(const struct ttm_place *place,
72 pos = ffs(place->flags & TTM_PL_MASK_MEM);
80 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
81 struct ttm_placement *placement)
83 struct drm_printer p = drm_debug_printer(TTM_PFX);
85 struct ttm_resource_manager *man;
87 drm_printf(&p, "No space for %p (%lu pages, %luK, %luM)\n",
88 bo, bo->mem.num_pages, bo->mem.size >> 10,
90 for (i = 0; i < placement->num_placement; i++) {
91 ret = ttm_mem_type_from_place(&placement->placement[i],
95 drm_printf(&p, " placement[%d]=0x%08X (%d)\n",
96 i, placement->placement[i].flags, mem_type);
97 man = ttm_manager_type(bo->bdev, mem_type);
98 ttm_resource_manager_debug(man, &p);
102 static ssize_t ttm_bo_global_show(struct kobject *kobj,
103 struct attribute *attr,
106 struct ttm_bo_global *glob =
107 container_of(kobj, struct ttm_bo_global, kobj);
109 return snprintf(buffer, PAGE_SIZE, "%d\n",
110 atomic_read(&glob->bo_count));
113 static struct attribute *ttm_bo_global_attrs[] = {
118 static const struct sysfs_ops ttm_bo_global_ops = {
119 .show = &ttm_bo_global_show
122 static struct kobj_type ttm_bo_glob_kobj_type = {
123 .release = &ttm_bo_global_kobj_release,
124 .sysfs_ops = &ttm_bo_global_ops,
125 .default_attrs = ttm_bo_global_attrs
129 static inline uint32_t ttm_bo_type_flags(unsigned type)
134 static void ttm_bo_add_mem_to_lru(struct ttm_buffer_object *bo,
135 struct ttm_resource *mem)
137 struct ttm_bo_device *bdev = bo->bdev;
138 struct ttm_resource_manager *man;
140 if (!list_empty(&bo->lru))
143 if (mem->placement & TTM_PL_FLAG_NO_EVICT)
146 man = ttm_manager_type(bdev, mem->mem_type);
147 list_add_tail(&bo->lru, &man->lru[bo->priority]);
149 if (man->use_tt && bo->ttm &&
150 !(bo->ttm->page_flags & (TTM_PAGE_FLAG_SG |
151 TTM_PAGE_FLAG_SWAPPED))) {
152 list_add_tail(&bo->swap, &ttm_bo_glob.swap_lru[bo->priority]);
156 static void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
158 struct ttm_bo_device *bdev = bo->bdev;
161 if (!list_empty(&bo->swap)) {
162 list_del_init(&bo->swap);
165 if (!list_empty(&bo->lru)) {
166 list_del_init(&bo->lru);
170 if (notify && bdev->driver->del_from_lru_notify)
171 bdev->driver->del_from_lru_notify(bo);
174 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
175 struct ttm_buffer_object *bo)
182 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
183 struct ttm_lru_bulk_move *bulk)
185 dma_resv_assert_held(bo->base.resv);
187 ttm_bo_del_from_lru(bo);
188 ttm_bo_add_mem_to_lru(bo, &bo->mem);
190 if (bulk && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
191 switch (bo->mem.mem_type) {
193 ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
197 ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
200 if (bo->ttm && !(bo->ttm->page_flags &
201 (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED)))
202 ttm_bo_bulk_move_set_pos(&bulk->swap[bo->priority], bo);
205 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
207 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
211 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
212 struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
213 struct ttm_resource_manager *man;
218 dma_resv_assert_held(pos->first->base.resv);
219 dma_resv_assert_held(pos->last->base.resv);
221 man = ttm_manager_type(pos->first->bdev, TTM_PL_TT);
222 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
226 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
227 struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
228 struct ttm_resource_manager *man;
233 dma_resv_assert_held(pos->first->base.resv);
234 dma_resv_assert_held(pos->last->base.resv);
236 man = ttm_manager_type(pos->first->bdev, TTM_PL_VRAM);
237 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
241 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
242 struct ttm_lru_bulk_move_pos *pos = &bulk->swap[i];
243 struct list_head *lru;
248 dma_resv_assert_held(pos->first->base.resv);
249 dma_resv_assert_held(pos->last->base.resv);
251 lru = &ttm_bo_glob.swap_lru[i];
252 list_bulk_move_tail(lru, &pos->first->swap, &pos->last->swap);
255 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
257 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
258 struct ttm_resource *mem, bool evict,
259 struct ttm_operation_ctx *ctx)
261 struct ttm_bo_device *bdev = bo->bdev;
262 struct ttm_resource_manager *old_man = ttm_manager_type(bdev, bo->mem.mem_type);
263 struct ttm_resource_manager *new_man = ttm_manager_type(bdev, mem->mem_type);
266 ret = ttm_mem_io_lock(old_man, true);
267 if (unlikely(ret != 0))
269 ttm_bo_unmap_virtual_locked(bo);
270 ttm_mem_io_unlock(old_man);
273 * Create and bind a ttm if required.
276 if (new_man->use_tt) {
277 /* Zero init the new TTM structure if the old location should
278 * have used one as well.
280 ret = ttm_tt_create(bo, old_man->use_tt);
284 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
288 if (mem->mem_type != TTM_PL_SYSTEM) {
289 ret = ttm_tt_bind(bo->ttm, mem, ctx);
294 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
295 if (bdev->driver->move_notify)
296 bdev->driver->move_notify(bo, evict, mem);
302 if (bdev->driver->move_notify)
303 bdev->driver->move_notify(bo, evict, mem);
305 if (old_man->use_tt && new_man->use_tt)
306 ret = ttm_bo_move_ttm(bo, ctx, mem);
307 else if (bdev->driver->move)
308 ret = bdev->driver->move(bo, evict, ctx, mem);
310 ret = ttm_bo_move_memcpy(bo, ctx, mem);
313 if (bdev->driver->move_notify) {
315 bdev->driver->move_notify(bo, false, mem);
325 ctx->bytes_moved += bo->num_pages << PAGE_SHIFT;
329 new_man = ttm_manager_type(bdev, bo->mem.mem_type);
330 if (!new_man->use_tt) {
331 ttm_tt_destroy(bo->ttm);
340 * Will release GPU memory type usage on destruction.
341 * This is the place to put in driver specific hooks to release
342 * driver private resources.
343 * Will release the bo::reserved lock.
346 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
348 if (bo->bdev->driver->move_notify)
349 bo->bdev->driver->move_notify(bo, false, NULL);
351 ttm_tt_destroy(bo->ttm);
353 ttm_resource_free(bo, &bo->mem);
356 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
360 if (bo->base.resv == &bo->base._resv)
363 BUG_ON(!dma_resv_trylock(&bo->base._resv));
365 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
366 dma_resv_unlock(&bo->base._resv);
370 if (bo->type != ttm_bo_type_sg) {
371 /* This works because the BO is about to be destroyed and nobody
372 * reference it any more. The only tricky case is the trylock on
373 * the resv object while holding the lru_lock.
375 spin_lock(&ttm_bo_glob.lru_lock);
376 bo->base.resv = &bo->base._resv;
377 spin_unlock(&ttm_bo_glob.lru_lock);
383 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
385 struct dma_resv *resv = &bo->base._resv;
386 struct dma_resv_list *fobj;
387 struct dma_fence *fence;
391 fobj = rcu_dereference(resv->fence);
392 fence = rcu_dereference(resv->fence_excl);
393 if (fence && !fence->ops->signaled)
394 dma_fence_enable_sw_signaling(fence);
396 for (i = 0; fobj && i < fobj->shared_count; ++i) {
397 fence = rcu_dereference(fobj->shared[i]);
399 if (!fence->ops->signaled)
400 dma_fence_enable_sw_signaling(fence);
406 * function ttm_bo_cleanup_refs
407 * If bo idle, remove from lru lists, and unref.
408 * If not idle, block if possible.
410 * Must be called with lru_lock and reservation held, this function
411 * will drop the lru lock and optionally the reservation lock before returning.
413 * @interruptible Any sleeps should occur interruptibly.
414 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
415 * @unlock_resv Unlock the reservation lock as well.
418 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
419 bool interruptible, bool no_wait_gpu,
422 struct dma_resv *resv = &bo->base._resv;
425 if (dma_resv_test_signaled_rcu(resv, true))
430 if (ret && !no_wait_gpu) {
434 dma_resv_unlock(bo->base.resv);
435 spin_unlock(&ttm_bo_glob.lru_lock);
437 lret = dma_resv_wait_timeout_rcu(resv, true, interruptible,
445 spin_lock(&ttm_bo_glob.lru_lock);
446 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
448 * We raced, and lost, someone else holds the reservation now,
449 * and is probably busy in ttm_bo_cleanup_memtype_use.
451 * Even if it's not the case, because we finished waiting any
452 * delayed destruction would succeed, so just return success
455 spin_unlock(&ttm_bo_glob.lru_lock);
461 if (ret || unlikely(list_empty(&bo->ddestroy))) {
463 dma_resv_unlock(bo->base.resv);
464 spin_unlock(&ttm_bo_glob.lru_lock);
468 ttm_bo_del_from_lru(bo);
469 list_del_init(&bo->ddestroy);
470 spin_unlock(&ttm_bo_glob.lru_lock);
471 ttm_bo_cleanup_memtype_use(bo);
474 dma_resv_unlock(bo->base.resv);
482 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
483 * encountered buffers.
485 static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
487 struct ttm_bo_global *glob = &ttm_bo_glob;
488 struct list_head removed;
491 INIT_LIST_HEAD(&removed);
493 spin_lock(&glob->lru_lock);
494 while (!list_empty(&bdev->ddestroy)) {
495 struct ttm_buffer_object *bo;
497 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
499 list_move_tail(&bo->ddestroy, &removed);
500 if (!ttm_bo_get_unless_zero(bo))
503 if (remove_all || bo->base.resv != &bo->base._resv) {
504 spin_unlock(&glob->lru_lock);
505 dma_resv_lock(bo->base.resv, NULL);
507 spin_lock(&glob->lru_lock);
508 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
510 } else if (dma_resv_trylock(bo->base.resv)) {
511 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
513 spin_unlock(&glob->lru_lock);
517 spin_lock(&glob->lru_lock);
519 list_splice_tail(&removed, &bdev->ddestroy);
520 empty = list_empty(&bdev->ddestroy);
521 spin_unlock(&glob->lru_lock);
526 static void ttm_bo_delayed_workqueue(struct work_struct *work)
528 struct ttm_bo_device *bdev =
529 container_of(work, struct ttm_bo_device, wq.work);
531 if (!ttm_bo_delayed_delete(bdev, false))
532 schedule_delayed_work(&bdev->wq,
533 ((HZ / 100) < 1) ? 1 : HZ / 100);
536 static void ttm_bo_release(struct kref *kref)
538 struct ttm_buffer_object *bo =
539 container_of(kref, struct ttm_buffer_object, kref);
540 struct ttm_bo_device *bdev = bo->bdev;
541 struct ttm_resource_manager *man = ttm_manager_type(bdev, bo->mem.mem_type);
542 size_t acc_size = bo->acc_size;
546 ret = ttm_bo_individualize_resv(bo);
548 /* Last resort, if we fail to allocate memory for the
549 * fences block for the BO to become idle
551 dma_resv_wait_timeout_rcu(bo->base.resv, true, false,
555 if (bo->bdev->driver->release_notify)
556 bo->bdev->driver->release_notify(bo);
558 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
559 ttm_mem_io_lock(man, false);
560 ttm_mem_io_free_vm(bo);
561 ttm_mem_io_unlock(man);
564 if (!dma_resv_test_signaled_rcu(bo->base.resv, true) ||
565 !dma_resv_trylock(bo->base.resv)) {
566 /* The BO is not idle, resurrect it for delayed destroy */
567 ttm_bo_flush_all_fences(bo);
570 spin_lock(&ttm_bo_glob.lru_lock);
573 * Make NO_EVICT bos immediately available to
574 * shrinkers, now that they are queued for
577 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
578 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
579 ttm_bo_del_from_lru(bo);
580 ttm_bo_add_mem_to_lru(bo, &bo->mem);
583 kref_init(&bo->kref);
584 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
585 spin_unlock(&ttm_bo_glob.lru_lock);
587 schedule_delayed_work(&bdev->wq,
588 ((HZ / 100) < 1) ? 1 : HZ / 100);
592 spin_lock(&ttm_bo_glob.lru_lock);
593 ttm_bo_del_from_lru(bo);
594 list_del(&bo->ddestroy);
595 spin_unlock(&ttm_bo_glob.lru_lock);
597 ttm_bo_cleanup_memtype_use(bo);
598 dma_resv_unlock(bo->base.resv);
600 atomic_dec(&ttm_bo_glob.bo_count);
601 dma_fence_put(bo->moving);
602 if (!ttm_bo_uses_embedded_gem_object(bo))
603 dma_resv_fini(&bo->base._resv);
605 ttm_mem_global_free(&ttm_mem_glob, acc_size);
608 void ttm_bo_put(struct ttm_buffer_object *bo)
610 kref_put(&bo->kref, ttm_bo_release);
612 EXPORT_SYMBOL(ttm_bo_put);
614 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
616 return cancel_delayed_work_sync(&bdev->wq);
618 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
620 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
623 schedule_delayed_work(&bdev->wq,
624 ((HZ / 100) < 1) ? 1 : HZ / 100);
626 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
628 static int ttm_bo_evict(struct ttm_buffer_object *bo,
629 struct ttm_operation_ctx *ctx)
631 struct ttm_bo_device *bdev = bo->bdev;
632 struct ttm_resource evict_mem;
633 struct ttm_placement placement;
636 dma_resv_assert_held(bo->base.resv);
638 placement.num_placement = 0;
639 placement.num_busy_placement = 0;
640 bdev->driver->evict_flags(bo, &placement);
642 if (!placement.num_placement && !placement.num_busy_placement) {
643 ttm_bo_wait(bo, false, false);
645 ttm_bo_cleanup_memtype_use(bo);
646 return ttm_tt_create(bo, false);
650 evict_mem.mm_node = NULL;
651 evict_mem.bus.io_reserved_vm = false;
652 evict_mem.bus.io_reserved_count = 0;
654 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
656 if (ret != -ERESTARTSYS) {
657 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
659 ttm_bo_mem_space_debug(bo, &placement);
664 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx);
666 if (ret != -ERESTARTSYS)
667 pr_err("Buffer eviction failed\n");
668 ttm_resource_free(bo, &evict_mem);
676 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
677 const struct ttm_place *place)
679 /* Don't evict this BO if it's outside of the
680 * requested placement range
682 if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
683 (place->lpfn && place->lpfn <= bo->mem.start))
688 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
691 * Check the target bo is allowable to be evicted or swapout, including cases:
693 * a. if share same reservation object with ctx->resv, have assumption
694 * reservation objects should already be locked, so not lock again and
695 * return true directly when either the opreation allow_reserved_eviction
696 * or the target bo already is in delayed free list;
698 * b. Otherwise, trylock it.
700 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
701 struct ttm_operation_ctx *ctx, bool *locked, bool *busy)
705 if (bo->base.resv == ctx->resv) {
706 dma_resv_assert_held(bo->base.resv);
707 if (ctx->flags & TTM_OPT_FLAG_ALLOW_RES_EVICT)
713 ret = dma_resv_trylock(bo->base.resv);
723 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
725 * @busy_bo: BO which couldn't be locked with trylock
726 * @ctx: operation context
727 * @ticket: acquire ticket
729 * Try to lock a busy buffer object to avoid failing eviction.
731 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
732 struct ttm_operation_ctx *ctx,
733 struct ww_acquire_ctx *ticket)
737 if (!busy_bo || !ticket)
740 if (ctx->interruptible)
741 r = dma_resv_lock_interruptible(busy_bo->base.resv,
744 r = dma_resv_lock(busy_bo->base.resv, ticket);
747 * TODO: It would be better to keep the BO locked until allocation is at
748 * least tried one more time, but that would mean a much larger rework
752 dma_resv_unlock(busy_bo->base.resv);
754 return r == -EDEADLK ? -EBUSY : r;
757 int ttm_mem_evict_first(struct ttm_bo_device *bdev,
758 struct ttm_resource_manager *man,
759 const struct ttm_place *place,
760 struct ttm_operation_ctx *ctx,
761 struct ww_acquire_ctx *ticket)
763 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
768 spin_lock(&ttm_bo_glob.lru_lock);
769 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
770 list_for_each_entry(bo, &man->lru[i], lru) {
773 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
775 if (busy && !busy_bo && ticket !=
776 dma_resv_locking_ctx(bo->base.resv))
781 if (place && !bdev->driver->eviction_valuable(bo,
784 dma_resv_unlock(bo->base.resv);
787 if (!ttm_bo_get_unless_zero(bo)) {
789 dma_resv_unlock(bo->base.resv);
795 /* If the inner loop terminated early, we have our candidate */
796 if (&bo->lru != &man->lru[i])
803 if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
805 spin_unlock(&ttm_bo_glob.lru_lock);
806 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
813 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
814 ctx->no_wait_gpu, locked);
819 spin_unlock(&ttm_bo_glob.lru_lock);
821 ret = ttm_bo_evict(bo, ctx);
823 ttm_bo_unreserve(bo);
830 * Add the last move fence to the BO and reserve a new shared slot.
832 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
833 struct ttm_resource_manager *man,
834 struct ttm_resource *mem,
837 struct dma_fence *fence;
840 spin_lock(&man->move_lock);
841 fence = dma_fence_get(man->move);
842 spin_unlock(&man->move_lock);
848 dma_fence_put(fence);
852 dma_resv_add_shared_fence(bo->base.resv, fence);
854 ret = dma_resv_reserve_shared(bo->base.resv, 1);
856 dma_fence_put(fence);
860 dma_fence_put(bo->moving);
866 * Repeatedly evict memory from the LRU for @mem_type until we create enough
867 * space, or we've evicted everything and there isn't enough space.
869 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
870 const struct ttm_place *place,
871 struct ttm_resource *mem,
872 struct ttm_operation_ctx *ctx)
874 struct ttm_bo_device *bdev = bo->bdev;
875 struct ttm_resource_manager *man = ttm_manager_type(bdev, mem->mem_type);
876 struct ww_acquire_ctx *ticket;
879 ticket = dma_resv_locking_ctx(bo->base.resv);
881 ret = ttm_resource_alloc(bo, place, mem);
884 if (unlikely(ret != -ENOSPC))
886 ret = ttm_mem_evict_first(bdev, man, place, ctx,
888 if (unlikely(ret != 0))
892 return ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
895 static uint32_t ttm_bo_select_caching(struct ttm_resource_manager *man,
896 uint32_t cur_placement,
897 uint32_t proposed_placement)
899 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
900 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
903 * Keep current caching if possible.
906 if ((cur_placement & caching) != 0)
907 result |= (cur_placement & caching);
908 else if ((man->default_caching & caching) != 0)
909 result |= man->default_caching;
910 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
911 result |= TTM_PL_FLAG_CACHED;
912 else if ((TTM_PL_FLAG_WC & caching) != 0)
913 result |= TTM_PL_FLAG_WC;
914 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
915 result |= TTM_PL_FLAG_UNCACHED;
920 static bool ttm_bo_mt_compatible(struct ttm_resource_manager *man,
922 const struct ttm_place *place,
923 uint32_t *masked_placement)
925 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
927 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
930 if ((place->flags & man->available_caching) == 0)
933 cur_flags |= (place->flags & man->available_caching);
935 *masked_placement = cur_flags;
940 * ttm_bo_mem_placement - check if placement is compatible
941 * @bo: BO to find memory for
942 * @place: where to search
943 * @mem: the memory object to fill in
944 * @ctx: operation context
946 * Check if placement is compatible and fill in mem structure.
947 * Returns -EBUSY if placement won't work or negative error code.
948 * 0 when placement can be used.
950 static int ttm_bo_mem_placement(struct ttm_buffer_object *bo,
951 const struct ttm_place *place,
952 struct ttm_resource *mem,
953 struct ttm_operation_ctx *ctx)
955 struct ttm_bo_device *bdev = bo->bdev;
956 uint32_t mem_type = TTM_PL_SYSTEM;
957 struct ttm_resource_manager *man;
958 uint32_t cur_flags = 0;
961 ret = ttm_mem_type_from_place(place, &mem_type);
965 man = ttm_manager_type(bdev, mem_type);
966 if (!man || !ttm_resource_manager_used(man))
969 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
972 cur_flags = ttm_bo_select_caching(man, bo->mem.placement, cur_flags);
974 * Use the access and other non-mapping-related flag bits from
975 * the memory placement flags to the current flags
977 ttm_flag_masked(&cur_flags, place->flags, ~TTM_PL_MASK_MEMTYPE);
979 mem->mem_type = mem_type;
980 mem->placement = cur_flags;
982 spin_lock(&ttm_bo_glob.lru_lock);
983 ttm_bo_del_from_lru(bo);
984 ttm_bo_add_mem_to_lru(bo, mem);
985 spin_unlock(&ttm_bo_glob.lru_lock);
991 * Creates space for memory region @mem according to its type.
993 * This function first searches for free space in compatible memory types in
994 * the priority order defined by the driver. If free space isn't found, then
995 * ttm_bo_mem_force_space is attempted in priority order to evict and find
998 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
999 struct ttm_placement *placement,
1000 struct ttm_resource *mem,
1001 struct ttm_operation_ctx *ctx)
1003 struct ttm_bo_device *bdev = bo->bdev;
1004 bool type_found = false;
1007 ret = dma_resv_reserve_shared(bo->base.resv, 1);
1011 for (i = 0; i < placement->num_placement; ++i) {
1012 const struct ttm_place *place = &placement->placement[i];
1013 struct ttm_resource_manager *man;
1015 ret = ttm_bo_mem_placement(bo, place, mem, ctx);
1022 ret = ttm_resource_alloc(bo, place, mem);
1028 man = ttm_manager_type(bdev, mem->mem_type);
1029 ret = ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
1030 if (unlikely(ret)) {
1031 ttm_resource_free(bo, mem);
1040 for (i = 0; i < placement->num_busy_placement; ++i) {
1041 const struct ttm_place *place = &placement->busy_placement[i];
1043 ret = ttm_bo_mem_placement(bo, place, mem, ctx);
1050 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
1054 if (ret && ret != -EBUSY)
1060 pr_err(TTM_PFX "No compatible memory type found\n");
1065 if (bo->mem.mem_type == TTM_PL_SYSTEM && !list_empty(&bo->lru)) {
1066 ttm_bo_move_to_lru_tail_unlocked(bo);
1071 EXPORT_SYMBOL(ttm_bo_mem_space);
1073 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1074 struct ttm_placement *placement,
1075 struct ttm_operation_ctx *ctx)
1078 struct ttm_resource mem;
1080 dma_resv_assert_held(bo->base.resv);
1082 mem.num_pages = bo->num_pages;
1083 mem.size = mem.num_pages << PAGE_SHIFT;
1084 mem.page_alignment = bo->mem.page_alignment;
1085 mem.bus.io_reserved_vm = false;
1086 mem.bus.io_reserved_count = 0;
1090 * Determine where to move the buffer.
1092 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
1095 ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx);
1098 ttm_resource_free(bo, &mem);
1102 static bool ttm_bo_places_compat(const struct ttm_place *places,
1103 unsigned num_placement,
1104 struct ttm_resource *mem,
1105 uint32_t *new_flags)
1109 for (i = 0; i < num_placement; i++) {
1110 const struct ttm_place *heap = &places[i];
1112 if ((mem->start < heap->fpfn ||
1113 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1116 *new_flags = heap->flags;
1117 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1118 (*new_flags & mem->placement & TTM_PL_MASK_MEM) &&
1119 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
1120 (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
1126 bool ttm_bo_mem_compat(struct ttm_placement *placement,
1127 struct ttm_resource *mem,
1128 uint32_t *new_flags)
1130 if (ttm_bo_places_compat(placement->placement, placement->num_placement,
1134 if ((placement->busy_placement != placement->placement ||
1135 placement->num_busy_placement > placement->num_placement) &&
1136 ttm_bo_places_compat(placement->busy_placement,
1137 placement->num_busy_placement,
1143 EXPORT_SYMBOL(ttm_bo_mem_compat);
1145 int ttm_bo_validate(struct ttm_buffer_object *bo,
1146 struct ttm_placement *placement,
1147 struct ttm_operation_ctx *ctx)
1152 dma_resv_assert_held(bo->base.resv);
1155 * Remove the backing store if no placement is given.
1157 if (!placement->num_placement && !placement->num_busy_placement) {
1158 ret = ttm_bo_pipeline_gutting(bo);
1162 return ttm_tt_create(bo, false);
1166 * Check whether we need to move buffer.
1168 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1169 ret = ttm_bo_move_buffer(bo, placement, ctx);
1174 * Use the access and other non-mapping-related flag bits from
1175 * the compatible memory placement flags to the active flags
1177 ttm_flag_masked(&bo->mem.placement, new_flags,
1178 ~TTM_PL_MASK_MEMTYPE);
1181 * We might need to add a TTM.
1183 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1184 ret = ttm_tt_create(bo, true);
1190 EXPORT_SYMBOL(ttm_bo_validate);
1192 int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
1193 struct ttm_buffer_object *bo,
1195 enum ttm_bo_type type,
1196 struct ttm_placement *placement,
1197 uint32_t page_alignment,
1198 struct ttm_operation_ctx *ctx,
1200 struct sg_table *sg,
1201 struct dma_resv *resv,
1202 void (*destroy) (struct ttm_buffer_object *))
1204 struct ttm_mem_global *mem_glob = &ttm_mem_glob;
1206 unsigned long num_pages;
1209 ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
1211 pr_err("Out of kernel memory\n");
1219 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1220 if (num_pages == 0) {
1221 pr_err("Illegal buffer object size\n");
1226 ttm_mem_global_free(mem_glob, acc_size);
1229 bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1231 kref_init(&bo->kref);
1232 INIT_LIST_HEAD(&bo->lru);
1233 INIT_LIST_HEAD(&bo->ddestroy);
1234 INIT_LIST_HEAD(&bo->swap);
1235 INIT_LIST_HEAD(&bo->io_reserve_lru);
1238 bo->num_pages = num_pages;
1239 bo->mem.size = num_pages << PAGE_SHIFT;
1240 bo->mem.mem_type = TTM_PL_SYSTEM;
1241 bo->mem.num_pages = bo->num_pages;
1242 bo->mem.mm_node = NULL;
1243 bo->mem.page_alignment = page_alignment;
1244 bo->mem.bus.io_reserved_vm = false;
1245 bo->mem.bus.io_reserved_count = 0;
1247 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1248 bo->acc_size = acc_size;
1251 bo->base.resv = resv;
1252 dma_resv_assert_held(bo->base.resv);
1254 bo->base.resv = &bo->base._resv;
1256 if (!ttm_bo_uses_embedded_gem_object(bo)) {
1258 * bo.gem is not initialized, so we have to setup the
1259 * struct elements we want use regardless.
1261 dma_resv_init(&bo->base._resv);
1262 drm_vma_node_reset(&bo->base.vma_node);
1264 atomic_inc(&ttm_bo_glob.bo_count);
1267 * For ttm_bo_type_device buffers, allocate
1268 * address space from the device.
1270 if (bo->type == ttm_bo_type_device ||
1271 bo->type == ttm_bo_type_sg)
1272 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
1275 /* passed reservation objects should already be locked,
1276 * since otherwise lockdep will be angered in radeon.
1279 locked = dma_resv_trylock(bo->base.resv);
1284 ret = ttm_bo_validate(bo, placement, ctx);
1286 if (unlikely(ret)) {
1288 ttm_bo_unreserve(bo);
1294 ttm_bo_move_to_lru_tail_unlocked(bo);
1298 EXPORT_SYMBOL(ttm_bo_init_reserved);
1300 int ttm_bo_init(struct ttm_bo_device *bdev,
1301 struct ttm_buffer_object *bo,
1303 enum ttm_bo_type type,
1304 struct ttm_placement *placement,
1305 uint32_t page_alignment,
1308 struct sg_table *sg,
1309 struct dma_resv *resv,
1310 void (*destroy) (struct ttm_buffer_object *))
1312 struct ttm_operation_ctx ctx = { interruptible, false };
1315 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1316 page_alignment, &ctx, acc_size,
1322 ttm_bo_unreserve(bo);
1326 EXPORT_SYMBOL(ttm_bo_init);
1328 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1329 unsigned long bo_size,
1330 unsigned struct_size)
1332 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1335 size += ttm_round_pot(struct_size);
1336 size += ttm_round_pot(npages * sizeof(void *));
1337 size += ttm_round_pot(sizeof(struct ttm_tt));
1340 EXPORT_SYMBOL(ttm_bo_acc_size);
1342 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1343 unsigned long bo_size,
1344 unsigned struct_size)
1346 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1349 size += ttm_round_pot(struct_size);
1350 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
1351 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1354 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1356 int ttm_bo_create(struct ttm_bo_device *bdev,
1358 enum ttm_bo_type type,
1359 struct ttm_placement *placement,
1360 uint32_t page_alignment,
1362 struct ttm_buffer_object **p_bo)
1364 struct ttm_buffer_object *bo;
1368 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1369 if (unlikely(bo == NULL))
1372 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1373 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1374 interruptible, acc_size,
1376 if (likely(ret == 0))
1381 EXPORT_SYMBOL(ttm_bo_create);
1383 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1385 struct ttm_resource_manager *man = ttm_manager_type(bdev, mem_type);
1387 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1388 pr_err("Illegal memory manager memory type %u\n", mem_type);
1393 pr_err("Memory type %u has not been initialized\n", mem_type);
1397 return ttm_resource_manager_force_list_clean(bdev, man);
1399 EXPORT_SYMBOL(ttm_bo_evict_mm);
1401 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1403 struct ttm_bo_global *glob =
1404 container_of(kobj, struct ttm_bo_global, kobj);
1406 __free_page(glob->dummy_read_page);
1409 static void ttm_bo_global_release(void)
1411 struct ttm_bo_global *glob = &ttm_bo_glob;
1413 mutex_lock(&ttm_global_mutex);
1414 if (--ttm_bo_glob_use_count > 0)
1417 kobject_del(&glob->kobj);
1418 kobject_put(&glob->kobj);
1419 ttm_mem_global_release(&ttm_mem_glob);
1420 memset(glob, 0, sizeof(*glob));
1422 mutex_unlock(&ttm_global_mutex);
1425 static int ttm_bo_global_init(void)
1427 struct ttm_bo_global *glob = &ttm_bo_glob;
1431 mutex_lock(&ttm_global_mutex);
1432 if (++ttm_bo_glob_use_count > 1)
1435 ret = ttm_mem_global_init(&ttm_mem_glob);
1439 spin_lock_init(&glob->lru_lock);
1440 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1442 if (unlikely(glob->dummy_read_page == NULL)) {
1447 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1448 INIT_LIST_HEAD(&glob->swap_lru[i]);
1449 INIT_LIST_HEAD(&glob->device_list);
1450 atomic_set(&glob->bo_count, 0);
1452 ret = kobject_init_and_add(
1453 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1454 if (unlikely(ret != 0))
1455 kobject_put(&glob->kobj);
1457 mutex_unlock(&ttm_global_mutex);
1461 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1463 struct ttm_bo_global *glob = &ttm_bo_glob;
1466 struct ttm_resource_manager *man;
1468 man = ttm_manager_type(bdev, TTM_PL_SYSTEM);
1469 ttm_resource_manager_set_used(man, false);
1470 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL);
1472 mutex_lock(&ttm_global_mutex);
1473 list_del(&bdev->device_list);
1474 mutex_unlock(&ttm_global_mutex);
1476 cancel_delayed_work_sync(&bdev->wq);
1478 if (ttm_bo_delayed_delete(bdev, true))
1479 pr_debug("Delayed destroy list was clean\n");
1481 spin_lock(&glob->lru_lock);
1482 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1483 if (list_empty(&man->lru[0]))
1484 pr_debug("Swap list %d was clean\n", i);
1485 spin_unlock(&glob->lru_lock);
1488 ttm_bo_global_release();
1492 EXPORT_SYMBOL(ttm_bo_device_release);
1494 static void ttm_bo_init_sysman(struct ttm_bo_device *bdev)
1496 struct ttm_resource_manager *man = &bdev->sysman;
1499 * Initialize the system memory buffer type.
1500 * Other types need to be driver / IOCTL initialized.
1503 man->available_caching = TTM_PL_MASK_CACHING;
1504 man->default_caching = TTM_PL_FLAG_CACHED;
1506 ttm_resource_manager_init(man, 0);
1507 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, man);
1508 ttm_resource_manager_set_used(man, true);
1511 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1512 struct ttm_bo_driver *driver,
1513 struct address_space *mapping,
1514 struct drm_vma_offset_manager *vma_manager,
1517 struct ttm_bo_global *glob = &ttm_bo_glob;
1520 if (WARN_ON(vma_manager == NULL))
1523 ret = ttm_bo_global_init();
1527 bdev->driver = driver;
1529 ttm_bo_init_sysman(bdev);
1531 bdev->vma_manager = vma_manager;
1532 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1533 INIT_LIST_HEAD(&bdev->ddestroy);
1534 bdev->dev_mapping = mapping;
1535 bdev->need_dma32 = need_dma32;
1536 mutex_lock(&ttm_global_mutex);
1537 list_add_tail(&bdev->device_list, &glob->device_list);
1538 mutex_unlock(&ttm_global_mutex);
1542 EXPORT_SYMBOL(ttm_bo_device_init);
1545 * buffer object vm functions.
1548 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1550 struct ttm_bo_device *bdev = bo->bdev;
1552 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1553 ttm_mem_io_free_vm(bo);
1556 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1558 struct ttm_bo_device *bdev = bo->bdev;
1559 struct ttm_resource_manager *man = ttm_manager_type(bdev, bo->mem.mem_type);
1561 ttm_mem_io_lock(man, false);
1562 ttm_bo_unmap_virtual_locked(bo);
1563 ttm_mem_io_unlock(man);
1567 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1569 int ttm_bo_wait(struct ttm_buffer_object *bo,
1570 bool interruptible, bool no_wait)
1572 long timeout = 15 * HZ;
1575 if (dma_resv_test_signaled_rcu(bo->base.resv, true))
1581 timeout = dma_resv_wait_timeout_rcu(bo->base.resv, true,
1582 interruptible, timeout);
1589 dma_resv_add_excl_fence(bo->base.resv, NULL);
1592 EXPORT_SYMBOL(ttm_bo_wait);
1595 * A buffer object shrink method that tries to swap out the first
1596 * buffer object on the bo_global::swap_lru list.
1598 int ttm_bo_swapout(struct ttm_bo_global *glob, struct ttm_operation_ctx *ctx)
1600 struct ttm_buffer_object *bo;
1605 spin_lock(&glob->lru_lock);
1606 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1607 list_for_each_entry(bo, &glob->swap_lru[i], swap) {
1608 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
1612 if (!ttm_bo_get_unless_zero(bo)) {
1614 dma_resv_unlock(bo->base.resv);
1626 spin_unlock(&glob->lru_lock);
1631 ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1636 ttm_bo_del_from_lru(bo);
1637 spin_unlock(&glob->lru_lock);
1640 * Move to system cached
1643 if (bo->mem.mem_type != TTM_PL_SYSTEM ||
1644 bo->ttm->caching_state != tt_cached) {
1645 struct ttm_operation_ctx ctx = { false, false };
1646 struct ttm_resource evict_mem;
1648 evict_mem = bo->mem;
1649 evict_mem.mm_node = NULL;
1650 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1651 evict_mem.mem_type = TTM_PL_SYSTEM;
1653 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx);
1654 if (unlikely(ret != 0))
1659 * Make sure BO is idle.
1662 ret = ttm_bo_wait(bo, false, false);
1663 if (unlikely(ret != 0))
1666 ttm_bo_unmap_virtual(bo);
1669 * Swap out. Buffer will be swapped in again as soon as
1670 * anyone tries to access a ttm page.
1673 if (bo->bdev->driver->swap_notify)
1674 bo->bdev->driver->swap_notify(bo);
1676 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1681 * Unreserve without putting on LRU to avoid swapping out an
1682 * already swapped buffer.
1685 dma_resv_unlock(bo->base.resv);
1689 EXPORT_SYMBOL(ttm_bo_swapout);
1691 void ttm_bo_swapout_all(void)
1693 struct ttm_operation_ctx ctx = {
1694 .interruptible = false,
1695 .no_wait_gpu = false
1698 while (ttm_bo_swapout(&ttm_bo_glob, &ctx) == 0);
1700 EXPORT_SYMBOL(ttm_bo_swapout_all);