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_del_from_lru(struct ttm_buffer_object *bo)
115 struct ttm_bo_device *bdev = bo->bdev;
117 list_del_init(&bo->swap);
118 list_del_init(&bo->lru);
120 if (bdev->driver->del_from_lru_notify)
121 bdev->driver->del_from_lru_notify(bo);
124 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
125 struct ttm_buffer_object *bo)
132 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
133 struct ttm_resource *mem,
134 struct ttm_lru_bulk_move *bulk)
136 struct ttm_bo_device *bdev = bo->bdev;
137 struct ttm_resource_manager *man;
139 dma_resv_assert_held(bo->base.resv);
142 ttm_bo_del_from_lru(bo);
146 man = ttm_manager_type(bdev, mem->mem_type);
147 list_move_tail(&bo->lru, &man->lru[bo->priority]);
148 if (man->use_tt && bo->ttm &&
149 !(bo->ttm->page_flags & (TTM_PAGE_FLAG_SG |
150 TTM_PAGE_FLAG_SWAPPED))) {
151 struct list_head *swap;
153 swap = &ttm_bo_glob.swap_lru[bo->priority];
154 list_move_tail(&bo->swap, swap);
157 if (bdev->driver->del_from_lru_notify)
158 bdev->driver->del_from_lru_notify(bo);
160 if (bulk && !bo->pin_count) {
161 switch (bo->mem.mem_type) {
163 ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
167 ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
170 if (bo->ttm && !(bo->ttm->page_flags &
171 (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED)))
172 ttm_bo_bulk_move_set_pos(&bulk->swap[bo->priority], bo);
175 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
177 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
181 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
182 struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
183 struct ttm_resource_manager *man;
188 dma_resv_assert_held(pos->first->base.resv);
189 dma_resv_assert_held(pos->last->base.resv);
191 man = ttm_manager_type(pos->first->bdev, TTM_PL_TT);
192 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
196 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
197 struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
198 struct ttm_resource_manager *man;
203 dma_resv_assert_held(pos->first->base.resv);
204 dma_resv_assert_held(pos->last->base.resv);
206 man = ttm_manager_type(pos->first->bdev, TTM_PL_VRAM);
207 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
211 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
212 struct ttm_lru_bulk_move_pos *pos = &bulk->swap[i];
213 struct list_head *lru;
218 dma_resv_assert_held(pos->first->base.resv);
219 dma_resv_assert_held(pos->last->base.resv);
221 lru = &ttm_bo_glob.swap_lru[i];
222 list_bulk_move_tail(lru, &pos->first->swap, &pos->last->swap);
225 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
227 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
228 struct ttm_resource *mem, bool evict,
229 struct ttm_operation_ctx *ctx,
230 struct ttm_place *hop)
232 struct ttm_bo_device *bdev = bo->bdev;
233 struct ttm_resource_manager *old_man = ttm_manager_type(bdev, bo->mem.mem_type);
234 struct ttm_resource_manager *new_man = ttm_manager_type(bdev, mem->mem_type);
237 ttm_bo_unmap_virtual(bo);
240 * Create and bind a ttm if required.
243 if (new_man->use_tt) {
244 /* Zero init the new TTM structure if the old location should
245 * have used one as well.
247 ret = ttm_tt_create(bo, old_man->use_tt);
251 if (mem->mem_type != TTM_PL_SYSTEM) {
252 ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
258 ret = bdev->driver->move(bo, evict, ctx, mem, hop);
260 if (ret == -EMULTIHOP)
265 ctx->bytes_moved += bo->base.size;
269 new_man = ttm_manager_type(bdev, bo->mem.mem_type);
270 if (!new_man->use_tt)
271 ttm_bo_tt_destroy(bo);
278 * Will release GPU memory type usage on destruction.
279 * This is the place to put in driver specific hooks to release
280 * driver private resources.
281 * Will release the bo::reserved lock.
284 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
286 if (bo->bdev->driver->delete_mem_notify)
287 bo->bdev->driver->delete_mem_notify(bo);
289 ttm_bo_tt_destroy(bo);
290 ttm_resource_free(bo, &bo->mem);
293 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
297 if (bo->base.resv == &bo->base._resv)
300 BUG_ON(!dma_resv_trylock(&bo->base._resv));
302 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
303 dma_resv_unlock(&bo->base._resv);
307 if (bo->type != ttm_bo_type_sg) {
308 /* This works because the BO is about to be destroyed and nobody
309 * reference it any more. The only tricky case is the trylock on
310 * the resv object while holding the lru_lock.
312 spin_lock(&ttm_bo_glob.lru_lock);
313 bo->base.resv = &bo->base._resv;
314 spin_unlock(&ttm_bo_glob.lru_lock);
320 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
322 struct dma_resv *resv = &bo->base._resv;
323 struct dma_resv_list *fobj;
324 struct dma_fence *fence;
328 fobj = rcu_dereference(resv->fence);
329 fence = rcu_dereference(resv->fence_excl);
330 if (fence && !fence->ops->signaled)
331 dma_fence_enable_sw_signaling(fence);
333 for (i = 0; fobj && i < fobj->shared_count; ++i) {
334 fence = rcu_dereference(fobj->shared[i]);
336 if (!fence->ops->signaled)
337 dma_fence_enable_sw_signaling(fence);
343 * function ttm_bo_cleanup_refs
344 * If bo idle, remove from lru lists, and unref.
345 * If not idle, block if possible.
347 * Must be called with lru_lock and reservation held, this function
348 * will drop the lru lock and optionally the reservation lock before returning.
350 * @bo: The buffer object to clean-up
351 * @interruptible: Any sleeps should occur interruptibly.
352 * @no_wait_gpu: Never wait for gpu. Return -EBUSY instead.
353 * @unlock_resv: Unlock the reservation lock as well.
356 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
357 bool interruptible, bool no_wait_gpu,
360 struct dma_resv *resv = &bo->base._resv;
363 if (dma_resv_test_signaled_rcu(resv, true))
368 if (ret && !no_wait_gpu) {
372 dma_resv_unlock(bo->base.resv);
373 spin_unlock(&ttm_bo_glob.lru_lock);
375 lret = dma_resv_wait_timeout_rcu(resv, true, interruptible,
383 spin_lock(&ttm_bo_glob.lru_lock);
384 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
386 * We raced, and lost, someone else holds the reservation now,
387 * and is probably busy in ttm_bo_cleanup_memtype_use.
389 * Even if it's not the case, because we finished waiting any
390 * delayed destruction would succeed, so just return success
393 spin_unlock(&ttm_bo_glob.lru_lock);
399 if (ret || unlikely(list_empty(&bo->ddestroy))) {
401 dma_resv_unlock(bo->base.resv);
402 spin_unlock(&ttm_bo_glob.lru_lock);
406 ttm_bo_del_from_lru(bo);
407 list_del_init(&bo->ddestroy);
408 spin_unlock(&ttm_bo_glob.lru_lock);
409 ttm_bo_cleanup_memtype_use(bo);
412 dma_resv_unlock(bo->base.resv);
420 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
421 * encountered buffers.
423 static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
425 struct ttm_bo_global *glob = &ttm_bo_glob;
426 struct list_head removed;
429 INIT_LIST_HEAD(&removed);
431 spin_lock(&glob->lru_lock);
432 while (!list_empty(&bdev->ddestroy)) {
433 struct ttm_buffer_object *bo;
435 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
437 list_move_tail(&bo->ddestroy, &removed);
438 if (!ttm_bo_get_unless_zero(bo))
441 if (remove_all || bo->base.resv != &bo->base._resv) {
442 spin_unlock(&glob->lru_lock);
443 dma_resv_lock(bo->base.resv, NULL);
445 spin_lock(&glob->lru_lock);
446 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
448 } else if (dma_resv_trylock(bo->base.resv)) {
449 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
451 spin_unlock(&glob->lru_lock);
455 spin_lock(&glob->lru_lock);
457 list_splice_tail(&removed, &bdev->ddestroy);
458 empty = list_empty(&bdev->ddestroy);
459 spin_unlock(&glob->lru_lock);
464 static void ttm_bo_delayed_workqueue(struct work_struct *work)
466 struct ttm_bo_device *bdev =
467 container_of(work, struct ttm_bo_device, wq.work);
469 if (!ttm_bo_delayed_delete(bdev, false))
470 schedule_delayed_work(&bdev->wq,
471 ((HZ / 100) < 1) ? 1 : HZ / 100);
474 static void ttm_bo_release(struct kref *kref)
476 struct ttm_buffer_object *bo =
477 container_of(kref, struct ttm_buffer_object, kref);
478 struct ttm_bo_device *bdev = bo->bdev;
479 size_t acc_size = bo->acc_size;
483 ret = ttm_bo_individualize_resv(bo);
485 /* Last resort, if we fail to allocate memory for the
486 * fences block for the BO to become idle
488 dma_resv_wait_timeout_rcu(bo->base.resv, true, false,
492 if (bo->bdev->driver->release_notify)
493 bo->bdev->driver->release_notify(bo);
495 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
496 ttm_mem_io_free(bdev, &bo->mem);
499 if (!dma_resv_test_signaled_rcu(bo->base.resv, true) ||
500 !dma_resv_trylock(bo->base.resv)) {
501 /* The BO is not idle, resurrect it for delayed destroy */
502 ttm_bo_flush_all_fences(bo);
505 spin_lock(&ttm_bo_glob.lru_lock);
508 * Make pinned bos immediately available to
509 * shrinkers, now that they are queued for
512 if (WARN_ON(bo->pin_count)) {
514 ttm_bo_move_to_lru_tail(bo, &bo->mem, NULL);
517 kref_init(&bo->kref);
518 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
519 spin_unlock(&ttm_bo_glob.lru_lock);
521 schedule_delayed_work(&bdev->wq,
522 ((HZ / 100) < 1) ? 1 : HZ / 100);
526 spin_lock(&ttm_bo_glob.lru_lock);
527 ttm_bo_del_from_lru(bo);
528 list_del(&bo->ddestroy);
529 spin_unlock(&ttm_bo_glob.lru_lock);
531 ttm_bo_cleanup_memtype_use(bo);
532 dma_resv_unlock(bo->base.resv);
534 atomic_dec(&ttm_bo_glob.bo_count);
535 dma_fence_put(bo->moving);
536 if (!ttm_bo_uses_embedded_gem_object(bo))
537 dma_resv_fini(&bo->base._resv);
539 ttm_mem_global_free(&ttm_mem_glob, acc_size);
542 void ttm_bo_put(struct ttm_buffer_object *bo)
544 kref_put(&bo->kref, ttm_bo_release);
546 EXPORT_SYMBOL(ttm_bo_put);
548 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
550 return cancel_delayed_work_sync(&bdev->wq);
552 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
554 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
557 schedule_delayed_work(&bdev->wq,
558 ((HZ / 100) < 1) ? 1 : HZ / 100);
560 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
562 static int ttm_bo_evict(struct ttm_buffer_object *bo,
563 struct ttm_operation_ctx *ctx)
565 struct ttm_bo_device *bdev = bo->bdev;
566 struct ttm_resource evict_mem;
567 struct ttm_placement placement;
568 struct ttm_place hop;
571 memset(&hop, 0, sizeof(hop));
573 dma_resv_assert_held(bo->base.resv);
575 placement.num_placement = 0;
576 placement.num_busy_placement = 0;
577 bdev->driver->evict_flags(bo, &placement);
579 if (!placement.num_placement && !placement.num_busy_placement) {
580 ttm_bo_wait(bo, false, false);
582 ttm_bo_cleanup_memtype_use(bo);
583 return ttm_tt_create(bo, false);
587 evict_mem.mm_node = NULL;
588 evict_mem.bus.offset = 0;
589 evict_mem.bus.addr = NULL;
591 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
593 if (ret != -ERESTARTSYS) {
594 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
596 ttm_bo_mem_space_debug(bo, &placement);
601 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx, &hop);
603 WARN(ret == -EMULTIHOP, "Unexpected multihop in eviction - likely driver bug\n");
604 if (ret != -ERESTARTSYS)
605 pr_err("Buffer eviction failed\n");
606 ttm_resource_free(bo, &evict_mem);
612 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
613 const struct ttm_place *place)
615 /* Don't evict this BO if it's outside of the
616 * requested placement range
618 if (place->fpfn >= (bo->mem.start + bo->mem.num_pages) ||
619 (place->lpfn && place->lpfn <= bo->mem.start))
624 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
627 * Check the target bo is allowable to be evicted or swapout, including cases:
629 * a. if share same reservation object with ctx->resv, have assumption
630 * reservation objects should already be locked, so not lock again and
631 * return true directly when either the opreation allow_reserved_eviction
632 * or the target bo already is in delayed free list;
634 * b. Otherwise, trylock it.
636 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
637 struct ttm_operation_ctx *ctx, bool *locked, bool *busy)
641 if (bo->base.resv == ctx->resv) {
642 dma_resv_assert_held(bo->base.resv);
643 if (ctx->allow_res_evict)
649 ret = dma_resv_trylock(bo->base.resv);
659 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
661 * @busy_bo: BO which couldn't be locked with trylock
662 * @ctx: operation context
663 * @ticket: acquire ticket
665 * Try to lock a busy buffer object to avoid failing eviction.
667 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
668 struct ttm_operation_ctx *ctx,
669 struct ww_acquire_ctx *ticket)
673 if (!busy_bo || !ticket)
676 if (ctx->interruptible)
677 r = dma_resv_lock_interruptible(busy_bo->base.resv,
680 r = dma_resv_lock(busy_bo->base.resv, ticket);
683 * TODO: It would be better to keep the BO locked until allocation is at
684 * least tried one more time, but that would mean a much larger rework
688 dma_resv_unlock(busy_bo->base.resv);
690 return r == -EDEADLK ? -EBUSY : r;
693 int ttm_mem_evict_first(struct ttm_bo_device *bdev,
694 struct ttm_resource_manager *man,
695 const struct ttm_place *place,
696 struct ttm_operation_ctx *ctx,
697 struct ww_acquire_ctx *ticket)
699 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
704 spin_lock(&ttm_bo_glob.lru_lock);
705 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
706 list_for_each_entry(bo, &man->lru[i], lru) {
709 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
711 if (busy && !busy_bo && ticket !=
712 dma_resv_locking_ctx(bo->base.resv))
717 if (place && !bdev->driver->eviction_valuable(bo,
720 dma_resv_unlock(bo->base.resv);
723 if (!ttm_bo_get_unless_zero(bo)) {
725 dma_resv_unlock(bo->base.resv);
731 /* If the inner loop terminated early, we have our candidate */
732 if (&bo->lru != &man->lru[i])
739 if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
741 spin_unlock(&ttm_bo_glob.lru_lock);
742 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
749 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
750 ctx->no_wait_gpu, locked);
755 spin_unlock(&ttm_bo_glob.lru_lock);
757 ret = ttm_bo_evict(bo, ctx);
759 ttm_bo_unreserve(bo);
766 * Add the last move fence to the BO and reserve a new shared slot.
768 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
769 struct ttm_resource_manager *man,
770 struct ttm_resource *mem,
773 struct dma_fence *fence;
776 spin_lock(&man->move_lock);
777 fence = dma_fence_get(man->move);
778 spin_unlock(&man->move_lock);
784 dma_fence_put(fence);
788 dma_resv_add_shared_fence(bo->base.resv, fence);
790 ret = dma_resv_reserve_shared(bo->base.resv, 1);
792 dma_fence_put(fence);
796 dma_fence_put(bo->moving);
802 * Repeatedly evict memory from the LRU for @mem_type until we create enough
803 * space, or we've evicted everything and there isn't enough space.
805 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
806 const struct ttm_place *place,
807 struct ttm_resource *mem,
808 struct ttm_operation_ctx *ctx)
810 struct ttm_bo_device *bdev = bo->bdev;
811 struct ttm_resource_manager *man = ttm_manager_type(bdev, mem->mem_type);
812 struct ww_acquire_ctx *ticket;
815 ticket = dma_resv_locking_ctx(bo->base.resv);
817 ret = ttm_resource_alloc(bo, place, mem);
820 if (unlikely(ret != -ENOSPC))
822 ret = ttm_mem_evict_first(bdev, man, place, ctx,
824 if (unlikely(ret != 0))
828 return ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
832 * ttm_bo_mem_placement - check if placement is compatible
833 * @bo: BO to find memory for
834 * @place: where to search
835 * @mem: the memory object to fill in
837 * Check if placement is compatible and fill in mem structure.
838 * Returns -EBUSY if placement won't work or negative error code.
839 * 0 when placement can be used.
841 static int ttm_bo_mem_placement(struct ttm_buffer_object *bo,
842 const struct ttm_place *place,
843 struct ttm_resource *mem)
845 struct ttm_bo_device *bdev = bo->bdev;
846 struct ttm_resource_manager *man;
848 man = ttm_manager_type(bdev, place->mem_type);
849 if (!man || !ttm_resource_manager_used(man))
852 mem->mem_type = place->mem_type;
853 mem->placement = place->flags;
855 spin_lock(&ttm_bo_glob.lru_lock);
856 ttm_bo_move_to_lru_tail(bo, mem, NULL);
857 spin_unlock(&ttm_bo_glob.lru_lock);
863 * Creates space for memory region @mem according to its type.
865 * This function first searches for free space in compatible memory types in
866 * the priority order defined by the driver. If free space isn't found, then
867 * ttm_bo_mem_force_space is attempted in priority order to evict and find
870 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
871 struct ttm_placement *placement,
872 struct ttm_resource *mem,
873 struct ttm_operation_ctx *ctx)
875 struct ttm_bo_device *bdev = bo->bdev;
876 bool type_found = false;
879 ret = dma_resv_reserve_shared(bo->base.resv, 1);
883 for (i = 0; i < placement->num_placement; ++i) {
884 const struct ttm_place *place = &placement->placement[i];
885 struct ttm_resource_manager *man;
887 ret = ttm_bo_mem_placement(bo, place, mem);
892 ret = ttm_resource_alloc(bo, place, mem);
898 man = ttm_manager_type(bdev, mem->mem_type);
899 ret = ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
901 ttm_resource_free(bo, mem);
910 for (i = 0; i < placement->num_busy_placement; ++i) {
911 const struct ttm_place *place = &placement->busy_placement[i];
913 ret = ttm_bo_mem_placement(bo, place, mem);
918 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
922 if (ret && ret != -EBUSY)
928 pr_err(TTM_PFX "No compatible memory type found\n");
933 if (bo->mem.mem_type == TTM_PL_SYSTEM && !bo->pin_count)
934 ttm_bo_move_to_lru_tail_unlocked(bo);
938 EXPORT_SYMBOL(ttm_bo_mem_space);
940 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
941 struct ttm_resource *mem,
942 struct ttm_operation_ctx *ctx,
943 struct ttm_place *hop)
945 struct ttm_placement hop_placement;
947 struct ttm_resource hop_mem = *mem;
949 hop_mem.mm_node = NULL;
950 hop_mem.mem_type = TTM_PL_SYSTEM;
951 hop_mem.placement = 0;
953 hop_placement.num_placement = hop_placement.num_busy_placement = 1;
954 hop_placement.placement = hop_placement.busy_placement = hop;
956 /* find space in the bounce domain */
957 ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
960 /* move to the bounce domain */
961 ret = ttm_bo_handle_move_mem(bo, &hop_mem, false, ctx, NULL);
963 ttm_resource_free(bo, &hop_mem);
969 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
970 struct ttm_placement *placement,
971 struct ttm_operation_ctx *ctx)
974 struct ttm_place hop;
975 struct ttm_resource mem;
977 dma_resv_assert_held(bo->base.resv);
979 memset(&hop, 0, sizeof(hop));
981 mem.num_pages = PAGE_ALIGN(bo->base.size) >> PAGE_SHIFT;
982 mem.page_alignment = bo->mem.page_alignment;
988 * Determine where to move the buffer.
990 * If driver determines move is going to need
991 * an extra step then it will return -EMULTIHOP
992 * and the buffer will be moved to the temporary
993 * stop and the driver will be called to make
996 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
1000 ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx, &hop);
1001 if (ret == -EMULTIHOP) {
1002 ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
1005 /* try and move to final place now. */
1010 ttm_resource_free(bo, &mem);
1014 static bool ttm_bo_places_compat(const struct ttm_place *places,
1015 unsigned num_placement,
1016 struct ttm_resource *mem,
1017 uint32_t *new_flags)
1021 for (i = 0; i < num_placement; i++) {
1022 const struct ttm_place *heap = &places[i];
1024 if ((mem->start < heap->fpfn ||
1025 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1028 *new_flags = heap->flags;
1029 if ((mem->mem_type == heap->mem_type) &&
1030 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
1031 (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
1037 bool ttm_bo_mem_compat(struct ttm_placement *placement,
1038 struct ttm_resource *mem,
1039 uint32_t *new_flags)
1041 if (ttm_bo_places_compat(placement->placement, placement->num_placement,
1045 if ((placement->busy_placement != placement->placement ||
1046 placement->num_busy_placement > placement->num_placement) &&
1047 ttm_bo_places_compat(placement->busy_placement,
1048 placement->num_busy_placement,
1054 EXPORT_SYMBOL(ttm_bo_mem_compat);
1056 int ttm_bo_validate(struct ttm_buffer_object *bo,
1057 struct ttm_placement *placement,
1058 struct ttm_operation_ctx *ctx)
1063 dma_resv_assert_held(bo->base.resv);
1066 * Remove the backing store if no placement is given.
1068 if (!placement->num_placement && !placement->num_busy_placement) {
1069 ret = ttm_bo_pipeline_gutting(bo);
1073 return ttm_tt_create(bo, false);
1077 * Check whether we need to move buffer.
1079 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1080 ret = ttm_bo_move_buffer(bo, placement, ctx);
1085 * We might need to add a TTM.
1087 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
1088 ret = ttm_tt_create(bo, true);
1094 EXPORT_SYMBOL(ttm_bo_validate);
1096 int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
1097 struct ttm_buffer_object *bo,
1099 enum ttm_bo_type type,
1100 struct ttm_placement *placement,
1101 uint32_t page_alignment,
1102 struct ttm_operation_ctx *ctx,
1104 struct sg_table *sg,
1105 struct dma_resv *resv,
1106 void (*destroy) (struct ttm_buffer_object *))
1108 struct ttm_mem_global *mem_glob = &ttm_mem_glob;
1112 ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
1114 pr_err("Out of kernel memory\n");
1122 bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1124 kref_init(&bo->kref);
1125 INIT_LIST_HEAD(&bo->lru);
1126 INIT_LIST_HEAD(&bo->ddestroy);
1127 INIT_LIST_HEAD(&bo->swap);
1130 bo->mem.mem_type = TTM_PL_SYSTEM;
1131 bo->mem.num_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1132 bo->mem.mm_node = NULL;
1133 bo->mem.page_alignment = page_alignment;
1134 bo->mem.bus.offset = 0;
1135 bo->mem.bus.addr = NULL;
1137 bo->mem.placement = 0;
1138 bo->acc_size = acc_size;
1142 bo->base.resv = resv;
1143 dma_resv_assert_held(bo->base.resv);
1145 bo->base.resv = &bo->base._resv;
1147 if (!ttm_bo_uses_embedded_gem_object(bo)) {
1149 * bo.base is not initialized, so we have to setup the
1150 * struct elements we want use regardless.
1152 bo->base.size = size;
1153 dma_resv_init(&bo->base._resv);
1154 drm_vma_node_reset(&bo->base.vma_node);
1156 atomic_inc(&ttm_bo_glob.bo_count);
1159 * For ttm_bo_type_device buffers, allocate
1160 * address space from the device.
1162 if (bo->type == ttm_bo_type_device ||
1163 bo->type == ttm_bo_type_sg)
1164 ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
1167 /* passed reservation objects should already be locked,
1168 * since otherwise lockdep will be angered in radeon.
1171 locked = dma_resv_trylock(bo->base.resv);
1176 ret = ttm_bo_validate(bo, placement, ctx);
1178 if (unlikely(ret)) {
1180 ttm_bo_unreserve(bo);
1186 ttm_bo_move_to_lru_tail_unlocked(bo);
1190 EXPORT_SYMBOL(ttm_bo_init_reserved);
1192 int ttm_bo_init(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,
1200 struct sg_table *sg,
1201 struct dma_resv *resv,
1202 void (*destroy) (struct ttm_buffer_object *))
1204 struct ttm_operation_ctx ctx = { interruptible, false };
1207 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1208 page_alignment, &ctx, acc_size,
1214 ttm_bo_unreserve(bo);
1218 EXPORT_SYMBOL(ttm_bo_init);
1220 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1221 unsigned long bo_size,
1222 unsigned struct_size)
1224 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1227 size += ttm_round_pot(struct_size);
1228 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
1229 size += ttm_round_pot(sizeof(struct ttm_tt));
1232 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1234 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1236 struct ttm_bo_global *glob =
1237 container_of(kobj, struct ttm_bo_global, kobj);
1239 __free_page(glob->dummy_read_page);
1242 static void ttm_bo_global_release(void)
1244 struct ttm_bo_global *glob = &ttm_bo_glob;
1246 mutex_lock(&ttm_global_mutex);
1247 if (--ttm_bo_glob_use_count > 0)
1250 kobject_del(&glob->kobj);
1251 kobject_put(&glob->kobj);
1252 ttm_mem_global_release(&ttm_mem_glob);
1253 memset(glob, 0, sizeof(*glob));
1255 mutex_unlock(&ttm_global_mutex);
1258 static int ttm_bo_global_init(void)
1260 struct ttm_bo_global *glob = &ttm_bo_glob;
1264 mutex_lock(&ttm_global_mutex);
1265 if (++ttm_bo_glob_use_count > 1)
1268 ret = ttm_mem_global_init(&ttm_mem_glob);
1272 spin_lock_init(&glob->lru_lock);
1273 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1275 if (unlikely(glob->dummy_read_page == NULL)) {
1280 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1281 INIT_LIST_HEAD(&glob->swap_lru[i]);
1282 INIT_LIST_HEAD(&glob->device_list);
1283 atomic_set(&glob->bo_count, 0);
1285 ret = kobject_init_and_add(
1286 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1287 if (unlikely(ret != 0))
1288 kobject_put(&glob->kobj);
1290 mutex_unlock(&ttm_global_mutex);
1294 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1296 struct ttm_bo_global *glob = &ttm_bo_glob;
1299 struct ttm_resource_manager *man;
1301 man = ttm_manager_type(bdev, TTM_PL_SYSTEM);
1302 ttm_resource_manager_set_used(man, false);
1303 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL);
1305 mutex_lock(&ttm_global_mutex);
1306 list_del(&bdev->device_list);
1307 mutex_unlock(&ttm_global_mutex);
1309 cancel_delayed_work_sync(&bdev->wq);
1311 if (ttm_bo_delayed_delete(bdev, true))
1312 pr_debug("Delayed destroy list was clean\n");
1314 spin_lock(&glob->lru_lock);
1315 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1316 if (list_empty(&man->lru[0]))
1317 pr_debug("Swap list %d was clean\n", i);
1318 spin_unlock(&glob->lru_lock);
1320 ttm_pool_fini(&bdev->pool);
1323 ttm_bo_global_release();
1327 EXPORT_SYMBOL(ttm_bo_device_release);
1329 static void ttm_bo_init_sysman(struct ttm_bo_device *bdev)
1331 struct ttm_resource_manager *man = &bdev->sysman;
1334 * Initialize the system memory buffer type.
1335 * Other types need to be driver / IOCTL initialized.
1339 ttm_resource_manager_init(man, 0);
1340 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, man);
1341 ttm_resource_manager_set_used(man, true);
1344 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1345 struct ttm_bo_driver *driver,
1347 struct address_space *mapping,
1348 struct drm_vma_offset_manager *vma_manager,
1349 bool use_dma_alloc, bool use_dma32)
1351 struct ttm_bo_global *glob = &ttm_bo_glob;
1354 if (WARN_ON(vma_manager == NULL))
1357 ret = ttm_bo_global_init();
1361 bdev->driver = driver;
1363 ttm_bo_init_sysman(bdev);
1364 ttm_pool_init(&bdev->pool, dev, use_dma_alloc, use_dma32);
1366 bdev->vma_manager = vma_manager;
1367 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1368 INIT_LIST_HEAD(&bdev->ddestroy);
1369 bdev->dev_mapping = mapping;
1370 mutex_lock(&ttm_global_mutex);
1371 list_add_tail(&bdev->device_list, &glob->device_list);
1372 mutex_unlock(&ttm_global_mutex);
1376 EXPORT_SYMBOL(ttm_bo_device_init);
1379 * buffer object vm functions.
1382 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1384 struct ttm_bo_device *bdev = bo->bdev;
1386 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1387 ttm_mem_io_free(bdev, &bo->mem);
1389 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1391 int ttm_bo_wait(struct ttm_buffer_object *bo,
1392 bool interruptible, bool no_wait)
1394 long timeout = 15 * HZ;
1397 if (dma_resv_test_signaled_rcu(bo->base.resv, true))
1403 timeout = dma_resv_wait_timeout_rcu(bo->base.resv, true,
1404 interruptible, timeout);
1411 dma_resv_add_excl_fence(bo->base.resv, NULL);
1414 EXPORT_SYMBOL(ttm_bo_wait);
1417 * A buffer object shrink method that tries to swap out the first
1418 * buffer object on the bo_global::swap_lru list.
1420 int ttm_bo_swapout(struct ttm_operation_ctx *ctx)
1422 struct ttm_bo_global *glob = &ttm_bo_glob;
1423 struct ttm_buffer_object *bo;
1428 spin_lock(&glob->lru_lock);
1429 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1430 list_for_each_entry(bo, &glob->swap_lru[i], swap) {
1431 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
1435 if (!ttm_bo_get_unless_zero(bo)) {
1437 dma_resv_unlock(bo->base.resv);
1449 spin_unlock(&glob->lru_lock);
1454 ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1459 ttm_bo_del_from_lru(bo);
1460 spin_unlock(&glob->lru_lock);
1463 * Move to system cached
1466 if (bo->mem.mem_type != TTM_PL_SYSTEM) {
1467 struct ttm_operation_ctx ctx = { false, false };
1468 struct ttm_resource evict_mem;
1469 struct ttm_place hop;
1471 memset(&hop, 0, sizeof(hop));
1473 evict_mem = bo->mem;
1474 evict_mem.mm_node = NULL;
1475 evict_mem.placement = 0;
1476 evict_mem.mem_type = TTM_PL_SYSTEM;
1478 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx, &hop);
1479 if (unlikely(ret != 0)) {
1480 WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
1486 * Make sure BO is idle.
1489 ret = ttm_bo_wait(bo, false, false);
1490 if (unlikely(ret != 0))
1493 ttm_bo_unmap_virtual(bo);
1496 * Swap out. Buffer will be swapped in again as soon as
1497 * anyone tries to access a ttm page.
1500 if (bo->bdev->driver->swap_notify)
1501 bo->bdev->driver->swap_notify(bo);
1503 ret = ttm_tt_swapout(bo->bdev, bo->ttm);
1508 * Unreserve without putting on LRU to avoid swapping out an
1509 * already swapped buffer.
1512 dma_resv_unlock(bo->base.resv);
1516 EXPORT_SYMBOL(ttm_bo_swapout);
1518 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1520 if (bo->ttm == NULL)
1523 ttm_tt_destroy(bo->bdev, bo->ttm);