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;
55 static struct attribute ttm_bo_count = {
60 /* default destructor */
61 static void ttm_bo_default_destroy(struct ttm_buffer_object *bo)
66 static inline int ttm_mem_type_from_place(const struct ttm_place *place,
71 pos = ffs(place->flags & TTM_PL_MASK_MEM);
79 static void ttm_mem_type_debug(struct ttm_bo_device *bdev, struct drm_printer *p,
82 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
84 drm_printf(p, " has_type: %d\n", man->has_type);
85 drm_printf(p, " use_type: %d\n", man->use_type);
86 drm_printf(p, " flags: 0x%08X\n", man->flags);
87 drm_printf(p, " gpu_offset: 0x%08llX\n", man->gpu_offset);
88 drm_printf(p, " size: %llu\n", man->size);
89 drm_printf(p, " available_caching: 0x%08X\n", man->available_caching);
90 drm_printf(p, " default_caching: 0x%08X\n", man->default_caching);
91 if (mem_type != TTM_PL_SYSTEM)
92 (*man->func->debug)(man, p);
95 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
96 struct ttm_placement *placement)
98 struct drm_printer p = drm_debug_printer(TTM_PFX);
101 drm_printf(&p, "No space for %p (%lu pages, %luK, %luM)\n",
102 bo, bo->mem.num_pages, bo->mem.size >> 10,
104 for (i = 0; i < placement->num_placement; i++) {
105 ret = ttm_mem_type_from_place(&placement->placement[i],
109 drm_printf(&p, " placement[%d]=0x%08X (%d)\n",
110 i, placement->placement[i].flags, mem_type);
111 ttm_mem_type_debug(bo->bdev, &p, mem_type);
115 static ssize_t ttm_bo_global_show(struct kobject *kobj,
116 struct attribute *attr,
119 struct ttm_bo_global *glob =
120 container_of(kobj, struct ttm_bo_global, kobj);
122 return snprintf(buffer, PAGE_SIZE, "%d\n",
123 atomic_read(&glob->bo_count));
126 static struct attribute *ttm_bo_global_attrs[] = {
131 static const struct sysfs_ops ttm_bo_global_ops = {
132 .show = &ttm_bo_global_show
135 static struct kobj_type ttm_bo_glob_kobj_type = {
136 .release = &ttm_bo_global_kobj_release,
137 .sysfs_ops = &ttm_bo_global_ops,
138 .default_attrs = ttm_bo_global_attrs
142 static inline uint32_t ttm_bo_type_flags(unsigned type)
147 static void ttm_bo_release_list(struct kref *list_kref)
149 struct ttm_buffer_object *bo =
150 container_of(list_kref, struct ttm_buffer_object, list_kref);
151 struct ttm_bo_device *bdev = bo->bdev;
152 size_t acc_size = bo->acc_size;
154 BUG_ON(kref_read(&bo->list_kref));
155 BUG_ON(kref_read(&bo->kref));
156 BUG_ON(atomic_read(&bo->cpu_writers));
157 BUG_ON(bo->mem.mm_node != NULL);
158 BUG_ON(!list_empty(&bo->lru));
159 BUG_ON(!list_empty(&bo->ddestroy));
160 ttm_tt_destroy(bo->ttm);
161 atomic_dec(&bo->bdev->glob->bo_count);
162 dma_fence_put(bo->moving);
163 if (!ttm_bo_uses_embedded_gem_object(bo))
164 dma_resv_fini(&bo->base._resv);
165 mutex_destroy(&bo->wu_mutex);
167 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
170 static void ttm_bo_add_mem_to_lru(struct ttm_buffer_object *bo,
171 struct ttm_mem_reg *mem)
173 struct ttm_bo_device *bdev = bo->bdev;
174 struct ttm_mem_type_manager *man;
176 dma_resv_assert_held(bo->base.resv);
178 if (!list_empty(&bo->lru))
181 if (mem->placement & TTM_PL_FLAG_NO_EVICT)
184 man = &bdev->man[mem->mem_type];
185 list_add_tail(&bo->lru, &man->lru[bo->priority]);
186 kref_get(&bo->list_kref);
188 if (bo->ttm && !(bo->ttm->page_flags &
189 (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED))) {
190 list_add_tail(&bo->swap, &bdev->glob->swap_lru[bo->priority]);
191 kref_get(&bo->list_kref);
195 void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
197 ttm_bo_add_mem_to_lru(bo, &bo->mem);
199 EXPORT_SYMBOL(ttm_bo_add_to_lru);
201 static void ttm_bo_ref_bug(struct kref *list_kref)
206 void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
208 struct ttm_bo_device *bdev = bo->bdev;
211 if (!list_empty(&bo->swap)) {
212 list_del_init(&bo->swap);
213 kref_put(&bo->list_kref, ttm_bo_ref_bug);
216 if (!list_empty(&bo->lru)) {
217 list_del_init(&bo->lru);
218 kref_put(&bo->list_kref, ttm_bo_ref_bug);
222 if (notify && bdev->driver->del_from_lru_notify)
223 bdev->driver->del_from_lru_notify(bo);
226 void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
228 struct ttm_bo_global *glob = bo->bdev->glob;
230 spin_lock(&glob->lru_lock);
231 ttm_bo_del_from_lru(bo);
232 spin_unlock(&glob->lru_lock);
234 EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
236 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
237 struct ttm_buffer_object *bo)
244 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
245 struct ttm_lru_bulk_move *bulk)
247 dma_resv_assert_held(bo->base.resv);
249 ttm_bo_del_from_lru(bo);
250 ttm_bo_add_to_lru(bo);
252 if (bulk && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
253 switch (bo->mem.mem_type) {
255 ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
259 ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
262 if (bo->ttm && !(bo->ttm->page_flags &
263 (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED)))
264 ttm_bo_bulk_move_set_pos(&bulk->swap[bo->priority], bo);
267 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
269 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
273 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
274 struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
275 struct ttm_mem_type_manager *man;
280 dma_resv_assert_held(pos->first->base.resv);
281 dma_resv_assert_held(pos->last->base.resv);
283 man = &pos->first->bdev->man[TTM_PL_TT];
284 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
288 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
289 struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
290 struct ttm_mem_type_manager *man;
295 dma_resv_assert_held(pos->first->base.resv);
296 dma_resv_assert_held(pos->last->base.resv);
298 man = &pos->first->bdev->man[TTM_PL_VRAM];
299 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
303 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
304 struct ttm_lru_bulk_move_pos *pos = &bulk->swap[i];
305 struct list_head *lru;
310 dma_resv_assert_held(pos->first->base.resv);
311 dma_resv_assert_held(pos->last->base.resv);
313 lru = &pos->first->bdev->glob->swap_lru[i];
314 list_bulk_move_tail(lru, &pos->first->swap, &pos->last->swap);
317 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
319 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
320 struct ttm_mem_reg *mem, bool evict,
321 struct ttm_operation_ctx *ctx)
323 struct ttm_bo_device *bdev = bo->bdev;
324 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
325 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
326 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
327 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
330 if (old_is_pci || new_is_pci ||
331 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
332 ret = ttm_mem_io_lock(old_man, true);
333 if (unlikely(ret != 0))
335 ttm_bo_unmap_virtual_locked(bo);
336 ttm_mem_io_unlock(old_man);
340 * Create and bind a ttm if required.
343 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
344 if (bo->ttm == NULL) {
345 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
346 ret = ttm_tt_create(bo, zero);
351 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
355 if (mem->mem_type != TTM_PL_SYSTEM) {
356 ret = ttm_tt_bind(bo->ttm, mem, ctx);
361 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
362 if (bdev->driver->move_notify)
363 bdev->driver->move_notify(bo, evict, mem);
370 if (bdev->driver->move_notify)
371 bdev->driver->move_notify(bo, evict, mem);
373 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
374 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
375 ret = ttm_bo_move_ttm(bo, ctx, mem);
376 else if (bdev->driver->move)
377 ret = bdev->driver->move(bo, evict, ctx, mem);
379 ret = ttm_bo_move_memcpy(bo, ctx, mem);
382 if (bdev->driver->move_notify) {
384 bdev->driver->move_notify(bo, false, mem);
393 if (bdev->driver->invalidate_caches) {
394 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
396 pr_err("Can not flush read caches\n");
402 bo->offset = (bo->mem.start << PAGE_SHIFT) +
403 bdev->man[bo->mem.mem_type].gpu_offset;
407 ctx->bytes_moved += bo->num_pages << PAGE_SHIFT;
411 new_man = &bdev->man[bo->mem.mem_type];
412 if (new_man->flags & TTM_MEMTYPE_FLAG_FIXED) {
413 ttm_tt_destroy(bo->ttm);
422 * Will release GPU memory type usage on destruction.
423 * This is the place to put in driver specific hooks to release
424 * driver private resources.
425 * Will release the bo::reserved lock.
428 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
430 if (bo->bdev->driver->move_notify)
431 bo->bdev->driver->move_notify(bo, false, NULL);
433 ttm_tt_destroy(bo->ttm);
435 ttm_bo_mem_put(bo, &bo->mem);
438 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
442 if (bo->base.resv == &bo->base._resv)
445 BUG_ON(!dma_resv_trylock(&bo->base._resv));
447 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
449 dma_resv_unlock(&bo->base._resv);
454 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
456 struct dma_resv_list *fobj;
457 struct dma_fence *fence;
460 fobj = dma_resv_get_list(&bo->base._resv);
461 fence = dma_resv_get_excl(&bo->base._resv);
462 if (fence && !fence->ops->signaled)
463 dma_fence_enable_sw_signaling(fence);
465 for (i = 0; fobj && i < fobj->shared_count; ++i) {
466 fence = rcu_dereference_protected(fobj->shared[i],
467 dma_resv_held(bo->base.resv));
469 if (!fence->ops->signaled)
470 dma_fence_enable_sw_signaling(fence);
474 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
476 struct ttm_bo_device *bdev = bo->bdev;
477 struct ttm_bo_global *glob = bdev->glob;
480 ret = ttm_bo_individualize_resv(bo);
482 /* Last resort, if we fail to allocate memory for the
483 * fences block for the BO to become idle
485 dma_resv_wait_timeout_rcu(bo->base.resv, true, false,
487 spin_lock(&glob->lru_lock);
491 spin_lock(&glob->lru_lock);
492 ret = dma_resv_trylock(bo->base.resv) ? 0 : -EBUSY;
494 if (dma_resv_test_signaled_rcu(&bo->base._resv, true)) {
495 ttm_bo_del_from_lru(bo);
496 spin_unlock(&glob->lru_lock);
497 if (bo->base.resv != &bo->base._resv)
498 dma_resv_unlock(&bo->base._resv);
500 ttm_bo_cleanup_memtype_use(bo);
501 dma_resv_unlock(bo->base.resv);
505 ttm_bo_flush_all_fences(bo);
508 * Make NO_EVICT bos immediately available to
509 * shrinkers, now that they are queued for
512 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
513 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
514 ttm_bo_add_to_lru(bo);
517 dma_resv_unlock(bo->base.resv);
519 if (bo->base.resv != &bo->base._resv)
520 dma_resv_unlock(&bo->base._resv);
523 kref_get(&bo->list_kref);
524 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
525 spin_unlock(&glob->lru_lock);
527 schedule_delayed_work(&bdev->wq,
528 ((HZ / 100) < 1) ? 1 : HZ / 100);
532 * function ttm_bo_cleanup_refs
533 * If bo idle, remove from delayed- and lru lists, and unref.
534 * If not idle, do nothing.
536 * Must be called with lru_lock and reservation held, this function
537 * will drop the lru lock and optionally the reservation lock before returning.
539 * @interruptible Any sleeps should occur interruptibly.
540 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
541 * @unlock_resv Unlock the reservation lock as well.
544 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
545 bool interruptible, bool no_wait_gpu,
548 struct ttm_bo_global *glob = bo->bdev->glob;
549 struct dma_resv *resv;
552 if (unlikely(list_empty(&bo->ddestroy)))
553 resv = bo->base.resv;
555 resv = &bo->base._resv;
557 if (dma_resv_test_signaled_rcu(resv, true))
562 if (ret && !no_wait_gpu) {
566 dma_resv_unlock(bo->base.resv);
567 spin_unlock(&glob->lru_lock);
569 lret = dma_resv_wait_timeout_rcu(resv, true,
578 spin_lock(&glob->lru_lock);
579 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
581 * We raced, and lost, someone else holds the reservation now,
582 * and is probably busy in ttm_bo_cleanup_memtype_use.
584 * Even if it's not the case, because we finished waiting any
585 * delayed destruction would succeed, so just return success
588 spin_unlock(&glob->lru_lock);
594 if (ret || unlikely(list_empty(&bo->ddestroy))) {
596 dma_resv_unlock(bo->base.resv);
597 spin_unlock(&glob->lru_lock);
601 ttm_bo_del_from_lru(bo);
602 list_del_init(&bo->ddestroy);
603 kref_put(&bo->list_kref, ttm_bo_ref_bug);
605 spin_unlock(&glob->lru_lock);
606 ttm_bo_cleanup_memtype_use(bo);
609 dma_resv_unlock(bo->base.resv);
615 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
616 * encountered buffers.
618 static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
620 struct ttm_bo_global *glob = bdev->glob;
621 struct list_head removed;
624 INIT_LIST_HEAD(&removed);
626 spin_lock(&glob->lru_lock);
627 while (!list_empty(&bdev->ddestroy)) {
628 struct ttm_buffer_object *bo;
630 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
632 kref_get(&bo->list_kref);
633 list_move_tail(&bo->ddestroy, &removed);
635 if (remove_all || bo->base.resv != &bo->base._resv) {
636 spin_unlock(&glob->lru_lock);
637 dma_resv_lock(bo->base.resv, NULL);
639 spin_lock(&glob->lru_lock);
640 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
642 } else if (dma_resv_trylock(bo->base.resv)) {
643 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
645 spin_unlock(&glob->lru_lock);
648 kref_put(&bo->list_kref, ttm_bo_release_list);
649 spin_lock(&glob->lru_lock);
651 list_splice_tail(&removed, &bdev->ddestroy);
652 empty = list_empty(&bdev->ddestroy);
653 spin_unlock(&glob->lru_lock);
658 static void ttm_bo_delayed_workqueue(struct work_struct *work)
660 struct ttm_bo_device *bdev =
661 container_of(work, struct ttm_bo_device, wq.work);
663 if (!ttm_bo_delayed_delete(bdev, false))
664 schedule_delayed_work(&bdev->wq,
665 ((HZ / 100) < 1) ? 1 : HZ / 100);
668 static void ttm_bo_release(struct kref *kref)
670 struct ttm_buffer_object *bo =
671 container_of(kref, struct ttm_buffer_object, kref);
672 struct ttm_bo_device *bdev = bo->bdev;
673 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
675 if (bo->bdev->driver->release_notify)
676 bo->bdev->driver->release_notify(bo);
678 drm_vma_offset_remove(&bdev->vma_manager, &bo->base.vma_node);
679 ttm_mem_io_lock(man, false);
680 ttm_mem_io_free_vm(bo);
681 ttm_mem_io_unlock(man);
682 ttm_bo_cleanup_refs_or_queue(bo);
683 kref_put(&bo->list_kref, ttm_bo_release_list);
686 void ttm_bo_put(struct ttm_buffer_object *bo)
688 kref_put(&bo->kref, ttm_bo_release);
690 EXPORT_SYMBOL(ttm_bo_put);
692 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
694 return cancel_delayed_work_sync(&bdev->wq);
696 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
698 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
701 schedule_delayed_work(&bdev->wq,
702 ((HZ / 100) < 1) ? 1 : HZ / 100);
704 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
706 static int ttm_bo_evict(struct ttm_buffer_object *bo,
707 struct ttm_operation_ctx *ctx)
709 struct ttm_bo_device *bdev = bo->bdev;
710 struct ttm_mem_reg evict_mem;
711 struct ttm_placement placement;
714 dma_resv_assert_held(bo->base.resv);
716 placement.num_placement = 0;
717 placement.num_busy_placement = 0;
718 bdev->driver->evict_flags(bo, &placement);
720 if (!placement.num_placement && !placement.num_busy_placement) {
721 ret = ttm_bo_pipeline_gutting(bo);
725 return ttm_tt_create(bo, false);
729 evict_mem.mm_node = NULL;
730 evict_mem.bus.io_reserved_vm = false;
731 evict_mem.bus.io_reserved_count = 0;
733 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
735 if (ret != -ERESTARTSYS) {
736 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
738 ttm_bo_mem_space_debug(bo, &placement);
743 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx);
745 if (ret != -ERESTARTSYS)
746 pr_err("Buffer eviction failed\n");
747 ttm_bo_mem_put(bo, &evict_mem);
755 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
756 const struct ttm_place *place)
758 /* Don't evict this BO if it's outside of the
759 * requested placement range
761 if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
762 (place->lpfn && place->lpfn <= bo->mem.start))
767 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
770 * Check the target bo is allowable to be evicted or swapout, including cases:
772 * a. if share same reservation object with ctx->resv, have assumption
773 * reservation objects should already be locked, so not lock again and
774 * return true directly when either the opreation allow_reserved_eviction
775 * or the target bo already is in delayed free list;
777 * b. Otherwise, trylock it.
779 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
780 struct ttm_operation_ctx *ctx, bool *locked, bool *busy)
784 if (bo->base.resv == ctx->resv) {
785 dma_resv_assert_held(bo->base.resv);
786 if (ctx->flags & TTM_OPT_FLAG_ALLOW_RES_EVICT
787 || !list_empty(&bo->ddestroy))
793 ret = dma_resv_trylock(bo->base.resv);
803 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
805 * @busy_bo: BO which couldn't be locked with trylock
806 * @ctx: operation context
807 * @ticket: acquire ticket
809 * Try to lock a busy buffer object to avoid failing eviction.
811 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
812 struct ttm_operation_ctx *ctx,
813 struct ww_acquire_ctx *ticket)
817 if (!busy_bo || !ticket)
820 if (ctx->interruptible)
821 r = dma_resv_lock_interruptible(busy_bo->base.resv,
824 r = dma_resv_lock(busy_bo->base.resv, ticket);
827 * TODO: It would be better to keep the BO locked until allocation is at
828 * least tried one more time, but that would mean a much larger rework
832 dma_resv_unlock(busy_bo->base.resv);
834 return r == -EDEADLK ? -EBUSY : r;
837 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
839 const struct ttm_place *place,
840 struct ttm_operation_ctx *ctx,
841 struct ww_acquire_ctx *ticket)
843 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
844 struct ttm_bo_global *glob = bdev->glob;
845 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
850 spin_lock(&glob->lru_lock);
851 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
852 list_for_each_entry(bo, &man->lru[i], lru) {
855 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
857 if (busy && !busy_bo && ticket !=
858 dma_resv_locking_ctx(bo->base.resv))
863 if (place && !bdev->driver->eviction_valuable(bo,
866 dma_resv_unlock(bo->base.resv);
872 /* If the inner loop terminated early, we have our candidate */
873 if (&bo->lru != &man->lru[i])
882 spin_unlock(&glob->lru_lock);
883 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
889 kref_get(&bo->list_kref);
891 if (!list_empty(&bo->ddestroy)) {
892 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
893 ctx->no_wait_gpu, locked);
894 kref_put(&bo->list_kref, ttm_bo_release_list);
898 ttm_bo_del_from_lru(bo);
899 spin_unlock(&glob->lru_lock);
901 ret = ttm_bo_evict(bo, ctx);
903 ttm_bo_unreserve(bo);
905 spin_lock(&glob->lru_lock);
906 ttm_bo_add_to_lru(bo);
907 spin_unlock(&glob->lru_lock);
910 kref_put(&bo->list_kref, ttm_bo_release_list);
914 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
916 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
919 (*man->func->put_node)(man, mem);
921 EXPORT_SYMBOL(ttm_bo_mem_put);
924 * Add the last move fence to the BO and reserve a new shared slot.
926 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
927 struct ttm_mem_type_manager *man,
928 struct ttm_mem_reg *mem)
930 struct dma_fence *fence;
933 spin_lock(&man->move_lock);
934 fence = dma_fence_get(man->move);
935 spin_unlock(&man->move_lock);
938 dma_resv_add_shared_fence(bo->base.resv, fence);
940 ret = dma_resv_reserve_shared(bo->base.resv, 1);
942 dma_fence_put(fence);
946 dma_fence_put(bo->moving);
954 * Repeatedly evict memory from the LRU for @mem_type until we create enough
955 * space, or we've evicted everything and there isn't enough space.
957 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
958 const struct ttm_place *place,
959 struct ttm_mem_reg *mem,
960 struct ttm_operation_ctx *ctx)
962 struct ttm_bo_device *bdev = bo->bdev;
963 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
964 struct ww_acquire_ctx *ticket;
967 ticket = dma_resv_locking_ctx(bo->base.resv);
969 ret = (*man->func->get_node)(man, bo, place, mem);
970 if (unlikely(ret != 0))
974 ret = ttm_mem_evict_first(bdev, mem->mem_type, place, ctx,
976 if (unlikely(ret != 0))
980 return ttm_bo_add_move_fence(bo, man, mem);
983 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
984 uint32_t cur_placement,
985 uint32_t proposed_placement)
987 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
988 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
991 * Keep current caching if possible.
994 if ((cur_placement & caching) != 0)
995 result |= (cur_placement & caching);
996 else if ((man->default_caching & caching) != 0)
997 result |= man->default_caching;
998 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
999 result |= TTM_PL_FLAG_CACHED;
1000 else if ((TTM_PL_FLAG_WC & caching) != 0)
1001 result |= TTM_PL_FLAG_WC;
1002 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
1003 result |= TTM_PL_FLAG_UNCACHED;
1008 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
1010 const struct ttm_place *place,
1011 uint32_t *masked_placement)
1013 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
1015 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
1018 if ((place->flags & man->available_caching) == 0)
1021 cur_flags |= (place->flags & man->available_caching);
1023 *masked_placement = cur_flags;
1028 * ttm_bo_mem_placement - check if placement is compatible
1029 * @bo: BO to find memory for
1030 * @place: where to search
1031 * @mem: the memory object to fill in
1032 * @ctx: operation context
1034 * Check if placement is compatible and fill in mem structure.
1035 * Returns -EBUSY if placement won't work or negative error code.
1036 * 0 when placement can be used.
1038 static int ttm_bo_mem_placement(struct ttm_buffer_object *bo,
1039 const struct ttm_place *place,
1040 struct ttm_mem_reg *mem,
1041 struct ttm_operation_ctx *ctx)
1043 struct ttm_bo_device *bdev = bo->bdev;
1044 uint32_t mem_type = TTM_PL_SYSTEM;
1045 struct ttm_mem_type_manager *man;
1046 uint32_t cur_flags = 0;
1049 ret = ttm_mem_type_from_place(place, &mem_type);
1053 man = &bdev->man[mem_type];
1054 if (!man->has_type || !man->use_type)
1057 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
1060 cur_flags = ttm_bo_select_caching(man, bo->mem.placement, cur_flags);
1062 * Use the access and other non-mapping-related flag bits from
1063 * the memory placement flags to the current flags
1065 ttm_flag_masked(&cur_flags, place->flags, ~TTM_PL_MASK_MEMTYPE);
1067 mem->mem_type = mem_type;
1068 mem->placement = cur_flags;
1070 if (bo->mem.mem_type < mem_type && !list_empty(&bo->lru)) {
1071 spin_lock(&bo->bdev->glob->lru_lock);
1072 ttm_bo_del_from_lru(bo);
1073 ttm_bo_add_mem_to_lru(bo, mem);
1074 spin_unlock(&bo->bdev->glob->lru_lock);
1081 * Creates space for memory region @mem according to its type.
1083 * This function first searches for free space in compatible memory types in
1084 * the priority order defined by the driver. If free space isn't found, then
1085 * ttm_bo_mem_force_space is attempted in priority order to evict and find
1088 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
1089 struct ttm_placement *placement,
1090 struct ttm_mem_reg *mem,
1091 struct ttm_operation_ctx *ctx)
1093 struct ttm_bo_device *bdev = bo->bdev;
1094 bool type_found = false;
1097 ret = dma_resv_reserve_shared(bo->base.resv, 1);
1101 mem->mm_node = NULL;
1102 for (i = 0; i < placement->num_placement; ++i) {
1103 const struct ttm_place *place = &placement->placement[i];
1104 struct ttm_mem_type_manager *man;
1106 ret = ttm_bo_mem_placement(bo, place, mem, ctx);
1113 mem->mm_node = NULL;
1114 if (mem->mem_type == TTM_PL_SYSTEM)
1117 man = &bdev->man[mem->mem_type];
1118 ret = (*man->func->get_node)(man, bo, place, mem);
1123 ret = ttm_bo_add_move_fence(bo, man, mem);
1124 if (unlikely(ret)) {
1125 (*man->func->put_node)(man, mem);
1132 for (i = 0; i < placement->num_busy_placement; ++i) {
1133 const struct ttm_place *place = &placement->busy_placement[i];
1135 ret = ttm_bo_mem_placement(bo, place, mem, ctx);
1142 mem->mm_node = NULL;
1143 if (mem->mem_type == TTM_PL_SYSTEM)
1146 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
1147 if (ret == 0 && mem->mm_node)
1150 if (ret && ret != -EBUSY)
1156 pr_err(TTM_PFX "No compatible memory type found\n");
1161 if (bo->mem.mem_type == TTM_PL_SYSTEM && !list_empty(&bo->lru)) {
1162 spin_lock(&bo->bdev->glob->lru_lock);
1163 ttm_bo_move_to_lru_tail(bo, NULL);
1164 spin_unlock(&bo->bdev->glob->lru_lock);
1169 EXPORT_SYMBOL(ttm_bo_mem_space);
1171 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1172 struct ttm_placement *placement,
1173 struct ttm_operation_ctx *ctx)
1176 struct ttm_mem_reg mem;
1178 dma_resv_assert_held(bo->base.resv);
1180 mem.num_pages = bo->num_pages;
1181 mem.size = mem.num_pages << PAGE_SHIFT;
1182 mem.page_alignment = bo->mem.page_alignment;
1183 mem.bus.io_reserved_vm = false;
1184 mem.bus.io_reserved_count = 0;
1186 * Determine where to move the buffer.
1188 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
1191 ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx);
1193 if (ret && mem.mm_node)
1194 ttm_bo_mem_put(bo, &mem);
1198 static bool ttm_bo_places_compat(const struct ttm_place *places,
1199 unsigned num_placement,
1200 struct ttm_mem_reg *mem,
1201 uint32_t *new_flags)
1205 for (i = 0; i < num_placement; i++) {
1206 const struct ttm_place *heap = &places[i];
1208 if (mem->mm_node && (mem->start < heap->fpfn ||
1209 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1212 *new_flags = heap->flags;
1213 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1214 (*new_flags & mem->placement & TTM_PL_MASK_MEM) &&
1215 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
1216 (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
1222 bool ttm_bo_mem_compat(struct ttm_placement *placement,
1223 struct ttm_mem_reg *mem,
1224 uint32_t *new_flags)
1226 if (ttm_bo_places_compat(placement->placement, placement->num_placement,
1230 if ((placement->busy_placement != placement->placement ||
1231 placement->num_busy_placement > placement->num_placement) &&
1232 ttm_bo_places_compat(placement->busy_placement,
1233 placement->num_busy_placement,
1239 EXPORT_SYMBOL(ttm_bo_mem_compat);
1241 int ttm_bo_validate(struct ttm_buffer_object *bo,
1242 struct ttm_placement *placement,
1243 struct ttm_operation_ctx *ctx)
1248 dma_resv_assert_held(bo->base.resv);
1250 * Check whether we need to move buffer.
1252 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1253 ret = ttm_bo_move_buffer(bo, placement, ctx);
1258 * Use the access and other non-mapping-related flag bits from
1259 * the compatible memory placement flags to the active flags
1261 ttm_flag_masked(&bo->mem.placement, new_flags,
1262 ~TTM_PL_MASK_MEMTYPE);
1265 * We might need to add a TTM.
1267 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1268 ret = ttm_tt_create(bo, true);
1274 EXPORT_SYMBOL(ttm_bo_validate);
1276 int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
1277 struct ttm_buffer_object *bo,
1279 enum ttm_bo_type type,
1280 struct ttm_placement *placement,
1281 uint32_t page_alignment,
1282 struct ttm_operation_ctx *ctx,
1284 struct sg_table *sg,
1285 struct dma_resv *resv,
1286 void (*destroy) (struct ttm_buffer_object *))
1289 unsigned long num_pages;
1290 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1293 ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
1295 pr_err("Out of kernel memory\n");
1303 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1304 if (num_pages == 0) {
1305 pr_err("Illegal buffer object size\n");
1310 ttm_mem_global_free(mem_glob, acc_size);
1313 bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1315 kref_init(&bo->kref);
1316 kref_init(&bo->list_kref);
1317 atomic_set(&bo->cpu_writers, 0);
1318 INIT_LIST_HEAD(&bo->lru);
1319 INIT_LIST_HEAD(&bo->ddestroy);
1320 INIT_LIST_HEAD(&bo->swap);
1321 INIT_LIST_HEAD(&bo->io_reserve_lru);
1322 mutex_init(&bo->wu_mutex);
1325 bo->num_pages = num_pages;
1326 bo->mem.size = num_pages << PAGE_SHIFT;
1327 bo->mem.mem_type = TTM_PL_SYSTEM;
1328 bo->mem.num_pages = bo->num_pages;
1329 bo->mem.mm_node = NULL;
1330 bo->mem.page_alignment = page_alignment;
1331 bo->mem.bus.io_reserved_vm = false;
1332 bo->mem.bus.io_reserved_count = 0;
1334 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1335 bo->acc_size = acc_size;
1338 bo->base.resv = resv;
1339 dma_resv_assert_held(bo->base.resv);
1341 bo->base.resv = &bo->base._resv;
1343 if (!ttm_bo_uses_embedded_gem_object(bo)) {
1345 * bo.gem is not initialized, so we have to setup the
1346 * struct elements we want use regardless.
1348 dma_resv_init(&bo->base._resv);
1349 drm_vma_node_reset(&bo->base.vma_node);
1351 atomic_inc(&bo->bdev->glob->bo_count);
1354 * For ttm_bo_type_device buffers, allocate
1355 * address space from the device.
1357 if (bo->type == ttm_bo_type_device ||
1358 bo->type == ttm_bo_type_sg)
1359 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->base.vma_node,
1362 /* passed reservation objects should already be locked,
1363 * since otherwise lockdep will be angered in radeon.
1366 locked = dma_resv_trylock(bo->base.resv);
1371 ret = ttm_bo_validate(bo, placement, ctx);
1373 if (unlikely(ret)) {
1375 ttm_bo_unreserve(bo);
1381 if (resv && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
1382 spin_lock(&bdev->glob->lru_lock);
1383 ttm_bo_add_to_lru(bo);
1384 spin_unlock(&bdev->glob->lru_lock);
1389 EXPORT_SYMBOL(ttm_bo_init_reserved);
1391 int ttm_bo_init(struct ttm_bo_device *bdev,
1392 struct ttm_buffer_object *bo,
1394 enum ttm_bo_type type,
1395 struct ttm_placement *placement,
1396 uint32_t page_alignment,
1399 struct sg_table *sg,
1400 struct dma_resv *resv,
1401 void (*destroy) (struct ttm_buffer_object *))
1403 struct ttm_operation_ctx ctx = { interruptible, false };
1406 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1407 page_alignment, &ctx, acc_size,
1413 ttm_bo_unreserve(bo);
1417 EXPORT_SYMBOL(ttm_bo_init);
1419 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1420 unsigned long bo_size,
1421 unsigned struct_size)
1423 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1426 size += ttm_round_pot(struct_size);
1427 size += ttm_round_pot(npages * sizeof(void *));
1428 size += ttm_round_pot(sizeof(struct ttm_tt));
1431 EXPORT_SYMBOL(ttm_bo_acc_size);
1433 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1434 unsigned long bo_size,
1435 unsigned struct_size)
1437 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1440 size += ttm_round_pot(struct_size);
1441 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
1442 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1445 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1447 int ttm_bo_create(struct ttm_bo_device *bdev,
1449 enum ttm_bo_type type,
1450 struct ttm_placement *placement,
1451 uint32_t page_alignment,
1453 struct ttm_buffer_object **p_bo)
1455 struct ttm_buffer_object *bo;
1459 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1460 if (unlikely(bo == NULL))
1463 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1464 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1465 interruptible, acc_size,
1467 if (likely(ret == 0))
1472 EXPORT_SYMBOL(ttm_bo_create);
1474 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1477 struct ttm_operation_ctx ctx = {
1478 .interruptible = false,
1479 .no_wait_gpu = false,
1480 .flags = TTM_OPT_FLAG_FORCE_ALLOC
1482 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1483 struct ttm_bo_global *glob = bdev->glob;
1484 struct dma_fence *fence;
1489 * Can't use standard list traversal since we're unlocking.
1492 spin_lock(&glob->lru_lock);
1493 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1494 while (!list_empty(&man->lru[i])) {
1495 spin_unlock(&glob->lru_lock);
1496 ret = ttm_mem_evict_first(bdev, mem_type, NULL, &ctx,
1500 spin_lock(&glob->lru_lock);
1503 spin_unlock(&glob->lru_lock);
1505 spin_lock(&man->move_lock);
1506 fence = dma_fence_get(man->move);
1507 spin_unlock(&man->move_lock);
1510 ret = dma_fence_wait(fence, false);
1511 dma_fence_put(fence);
1519 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1521 struct ttm_mem_type_manager *man;
1524 if (mem_type >= TTM_NUM_MEM_TYPES) {
1525 pr_err("Illegal memory type %d\n", mem_type);
1528 man = &bdev->man[mem_type];
1530 if (!man->has_type) {
1531 pr_err("Trying to take down uninitialized memory manager type %u\n",
1536 man->use_type = false;
1537 man->has_type = false;
1541 ret = ttm_bo_force_list_clean(bdev, mem_type);
1543 pr_err("Cleanup eviction failed\n");
1547 ret = (*man->func->takedown)(man);
1550 dma_fence_put(man->move);
1555 EXPORT_SYMBOL(ttm_bo_clean_mm);
1557 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1559 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1561 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1562 pr_err("Illegal memory manager memory type %u\n", mem_type);
1566 if (!man->has_type) {
1567 pr_err("Memory type %u has not been initialized\n", mem_type);
1571 return ttm_bo_force_list_clean(bdev, mem_type);
1573 EXPORT_SYMBOL(ttm_bo_evict_mm);
1575 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1576 unsigned long p_size)
1579 struct ttm_mem_type_manager *man;
1582 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1583 man = &bdev->man[type];
1584 BUG_ON(man->has_type);
1585 man->io_reserve_fastpath = true;
1586 man->use_io_reserve_lru = false;
1587 mutex_init(&man->io_reserve_mutex);
1588 spin_lock_init(&man->move_lock);
1589 INIT_LIST_HEAD(&man->io_reserve_lru);
1591 ret = bdev->driver->init_mem_type(bdev, type, man);
1596 if (type != TTM_PL_SYSTEM) {
1597 ret = (*man->func->init)(man, p_size);
1601 man->has_type = true;
1602 man->use_type = true;
1605 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1606 INIT_LIST_HEAD(&man->lru[i]);
1611 EXPORT_SYMBOL(ttm_bo_init_mm);
1613 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1615 struct ttm_bo_global *glob =
1616 container_of(kobj, struct ttm_bo_global, kobj);
1618 __free_page(glob->dummy_read_page);
1621 static void ttm_bo_global_release(void)
1623 struct ttm_bo_global *glob = &ttm_bo_glob;
1625 mutex_lock(&ttm_global_mutex);
1626 if (--ttm_bo_glob_use_count > 0)
1629 kobject_del(&glob->kobj);
1630 kobject_put(&glob->kobj);
1631 ttm_mem_global_release(&ttm_mem_glob);
1632 memset(glob, 0, sizeof(*glob));
1634 mutex_unlock(&ttm_global_mutex);
1637 static int ttm_bo_global_init(void)
1639 struct ttm_bo_global *glob = &ttm_bo_glob;
1643 mutex_lock(&ttm_global_mutex);
1644 if (++ttm_bo_glob_use_count > 1)
1647 ret = ttm_mem_global_init(&ttm_mem_glob);
1651 spin_lock_init(&glob->lru_lock);
1652 glob->mem_glob = &ttm_mem_glob;
1653 glob->mem_glob->bo_glob = glob;
1654 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1656 if (unlikely(glob->dummy_read_page == NULL)) {
1661 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1662 INIT_LIST_HEAD(&glob->swap_lru[i]);
1663 INIT_LIST_HEAD(&glob->device_list);
1664 atomic_set(&glob->bo_count, 0);
1666 ret = kobject_init_and_add(
1667 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1668 if (unlikely(ret != 0))
1669 kobject_put(&glob->kobj);
1671 mutex_unlock(&ttm_global_mutex);
1675 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1678 unsigned i = TTM_NUM_MEM_TYPES;
1679 struct ttm_mem_type_manager *man;
1680 struct ttm_bo_global *glob = bdev->glob;
1683 man = &bdev->man[i];
1684 if (man->has_type) {
1685 man->use_type = false;
1686 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1688 pr_err("DRM memory manager type %d is not clean\n",
1691 man->has_type = false;
1695 mutex_lock(&ttm_global_mutex);
1696 list_del(&bdev->device_list);
1697 mutex_unlock(&ttm_global_mutex);
1699 cancel_delayed_work_sync(&bdev->wq);
1701 if (ttm_bo_delayed_delete(bdev, true))
1702 pr_debug("Delayed destroy list was clean\n");
1704 spin_lock(&glob->lru_lock);
1705 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1706 if (list_empty(&bdev->man[0].lru[0]))
1707 pr_debug("Swap list %d was clean\n", i);
1708 spin_unlock(&glob->lru_lock);
1710 drm_vma_offset_manager_destroy(&bdev->vma_manager);
1713 ttm_bo_global_release();
1717 EXPORT_SYMBOL(ttm_bo_device_release);
1719 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1720 struct ttm_bo_driver *driver,
1721 struct address_space *mapping,
1724 struct ttm_bo_global *glob = &ttm_bo_glob;
1727 ret = ttm_bo_global_init();
1731 bdev->driver = driver;
1733 memset(bdev->man, 0, sizeof(bdev->man));
1736 * Initialize the system memory buffer type.
1737 * Other types need to be driver / IOCTL initialized.
1739 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1740 if (unlikely(ret != 0))
1743 drm_vma_offset_manager_init(&bdev->vma_manager,
1744 DRM_FILE_PAGE_OFFSET_START,
1745 DRM_FILE_PAGE_OFFSET_SIZE);
1746 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1747 INIT_LIST_HEAD(&bdev->ddestroy);
1748 bdev->dev_mapping = mapping;
1750 bdev->need_dma32 = need_dma32;
1751 mutex_lock(&ttm_global_mutex);
1752 list_add_tail(&bdev->device_list, &glob->device_list);
1753 mutex_unlock(&ttm_global_mutex);
1757 ttm_bo_global_release();
1760 EXPORT_SYMBOL(ttm_bo_device_init);
1763 * buffer object vm functions.
1766 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1768 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1770 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1771 if (mem->mem_type == TTM_PL_SYSTEM)
1774 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1777 if (mem->placement & TTM_PL_FLAG_CACHED)
1783 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1785 struct ttm_bo_device *bdev = bo->bdev;
1787 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1788 ttm_mem_io_free_vm(bo);
1791 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1793 struct ttm_bo_device *bdev = bo->bdev;
1794 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1796 ttm_mem_io_lock(man, false);
1797 ttm_bo_unmap_virtual_locked(bo);
1798 ttm_mem_io_unlock(man);
1802 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1804 int ttm_bo_wait(struct ttm_buffer_object *bo,
1805 bool interruptible, bool no_wait)
1807 long timeout = 15 * HZ;
1810 if (dma_resv_test_signaled_rcu(bo->base.resv, true))
1816 timeout = dma_resv_wait_timeout_rcu(bo->base.resv, true,
1817 interruptible, timeout);
1824 dma_resv_add_excl_fence(bo->base.resv, NULL);
1827 EXPORT_SYMBOL(ttm_bo_wait);
1829 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1834 * Using ttm_bo_reserve makes sure the lru lists are updated.
1837 ret = ttm_bo_reserve(bo, true, no_wait, NULL);
1838 if (unlikely(ret != 0))
1840 ret = ttm_bo_wait(bo, true, no_wait);
1841 if (likely(ret == 0))
1842 atomic_inc(&bo->cpu_writers);
1843 ttm_bo_unreserve(bo);
1846 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1848 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1850 atomic_dec(&bo->cpu_writers);
1852 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1855 * A buffer object shrink method that tries to swap out the first
1856 * buffer object on the bo_global::swap_lru list.
1858 int ttm_bo_swapout(struct ttm_bo_global *glob, struct ttm_operation_ctx *ctx)
1860 struct ttm_buffer_object *bo;
1865 spin_lock(&glob->lru_lock);
1866 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1867 list_for_each_entry(bo, &glob->swap_lru[i], swap) {
1868 if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
1879 spin_unlock(&glob->lru_lock);
1883 kref_get(&bo->list_kref);
1885 if (!list_empty(&bo->ddestroy)) {
1886 ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1887 kref_put(&bo->list_kref, ttm_bo_release_list);
1891 ttm_bo_del_from_lru(bo);
1892 spin_unlock(&glob->lru_lock);
1895 * Move to system cached
1898 if (bo->mem.mem_type != TTM_PL_SYSTEM ||
1899 bo->ttm->caching_state != tt_cached) {
1900 struct ttm_operation_ctx ctx = { false, false };
1901 struct ttm_mem_reg evict_mem;
1903 evict_mem = bo->mem;
1904 evict_mem.mm_node = NULL;
1905 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1906 evict_mem.mem_type = TTM_PL_SYSTEM;
1908 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx);
1909 if (unlikely(ret != 0))
1914 * Make sure BO is idle.
1917 ret = ttm_bo_wait(bo, false, false);
1918 if (unlikely(ret != 0))
1921 ttm_bo_unmap_virtual(bo);
1924 * Swap out. Buffer will be swapped in again as soon as
1925 * anyone tries to access a ttm page.
1928 if (bo->bdev->driver->swap_notify)
1929 bo->bdev->driver->swap_notify(bo);
1931 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1936 * Unreserve without putting on LRU to avoid swapping out an
1937 * already swapped buffer.
1940 dma_resv_unlock(bo->base.resv);
1941 kref_put(&bo->list_kref, ttm_bo_release_list);
1944 EXPORT_SYMBOL(ttm_bo_swapout);
1946 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1948 struct ttm_operation_ctx ctx = {
1949 .interruptible = false,
1950 .no_wait_gpu = false
1953 while (ttm_bo_swapout(bdev->glob, &ctx) == 0)
1956 EXPORT_SYMBOL(ttm_bo_swapout_all);
1959 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1962 * @bo: Pointer to buffer
1964 int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1969 * In the absense of a wait_unlocked API,
1970 * Use the bo::wu_mutex to avoid triggering livelocks due to
1971 * concurrent use of this function. Note that this use of
1972 * bo::wu_mutex can go away if we change locking order to
1973 * mmap_sem -> bo::reserve.
1975 ret = mutex_lock_interruptible(&bo->wu_mutex);
1976 if (unlikely(ret != 0))
1977 return -ERESTARTSYS;
1978 if (!dma_resv_is_locked(bo->base.resv))
1980 ret = dma_resv_lock_interruptible(bo->base.resv, NULL);
1983 if (unlikely(ret != 0))
1985 dma_resv_unlock(bo->base.resv);
1988 mutex_unlock(&bo->wu_mutex);