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 (!(man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm &&
189 !(bo->ttm->page_flags & (TTM_PAGE_FLAG_SG |
190 TTM_PAGE_FLAG_SWAPPED))) {
191 list_add_tail(&bo->swap, &bdev->glob->swap_lru[bo->priority]);
192 kref_get(&bo->list_kref);
196 void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
198 ttm_bo_add_mem_to_lru(bo, &bo->mem);
200 EXPORT_SYMBOL(ttm_bo_add_to_lru);
202 static void ttm_bo_ref_bug(struct kref *list_kref)
207 void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
209 struct ttm_bo_device *bdev = bo->bdev;
212 if (!list_empty(&bo->swap)) {
213 list_del_init(&bo->swap);
214 kref_put(&bo->list_kref, ttm_bo_ref_bug);
217 if (!list_empty(&bo->lru)) {
218 list_del_init(&bo->lru);
219 kref_put(&bo->list_kref, ttm_bo_ref_bug);
223 if (notify && bdev->driver->del_from_lru_notify)
224 bdev->driver->del_from_lru_notify(bo);
227 void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
229 struct ttm_bo_global *glob = bo->bdev->glob;
231 spin_lock(&glob->lru_lock);
232 ttm_bo_del_from_lru(bo);
233 spin_unlock(&glob->lru_lock);
235 EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
237 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
238 struct ttm_buffer_object *bo)
245 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
246 struct ttm_lru_bulk_move *bulk)
248 dma_resv_assert_held(bo->base.resv);
250 ttm_bo_del_from_lru(bo);
251 ttm_bo_add_to_lru(bo);
253 if (bulk && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
254 switch (bo->mem.mem_type) {
256 ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
260 ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
263 if (bo->ttm && !(bo->ttm->page_flags &
264 (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED)))
265 ttm_bo_bulk_move_set_pos(&bulk->swap[bo->priority], bo);
268 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
270 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
274 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
275 struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
276 struct ttm_mem_type_manager *man;
281 dma_resv_assert_held(pos->first->base.resv);
282 dma_resv_assert_held(pos->last->base.resv);
284 man = &pos->first->bdev->man[TTM_PL_TT];
285 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
289 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
290 struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
291 struct ttm_mem_type_manager *man;
296 dma_resv_assert_held(pos->first->base.resv);
297 dma_resv_assert_held(pos->last->base.resv);
299 man = &pos->first->bdev->man[TTM_PL_VRAM];
300 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
304 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
305 struct ttm_lru_bulk_move_pos *pos = &bulk->swap[i];
306 struct list_head *lru;
311 dma_resv_assert_held(pos->first->base.resv);
312 dma_resv_assert_held(pos->last->base.resv);
314 lru = &pos->first->bdev->glob->swap_lru[i];
315 list_bulk_move_tail(lru, &pos->first->swap, &pos->last->swap);
318 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
320 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
321 struct ttm_mem_reg *mem, bool evict,
322 struct ttm_operation_ctx *ctx)
324 struct ttm_bo_device *bdev = bo->bdev;
325 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
326 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
327 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
328 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
331 if (old_is_pci || new_is_pci ||
332 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
333 ret = ttm_mem_io_lock(old_man, true);
334 if (unlikely(ret != 0))
336 ttm_bo_unmap_virtual_locked(bo);
337 ttm_mem_io_unlock(old_man);
341 * Create and bind a ttm if required.
344 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
345 if (bo->ttm == NULL) {
346 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
347 ret = ttm_tt_create(bo, zero);
352 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
356 if (mem->mem_type != TTM_PL_SYSTEM) {
357 ret = ttm_tt_bind(bo->ttm, mem, ctx);
362 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
363 if (bdev->driver->move_notify)
364 bdev->driver->move_notify(bo, evict, mem);
371 if (bdev->driver->move_notify)
372 bdev->driver->move_notify(bo, evict, mem);
374 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
375 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
376 ret = ttm_bo_move_ttm(bo, ctx, mem);
377 else if (bdev->driver->move)
378 ret = bdev->driver->move(bo, evict, ctx, mem);
380 ret = ttm_bo_move_memcpy(bo, ctx, mem);
383 if (bdev->driver->move_notify) {
385 bdev->driver->move_notify(bo, false, mem);
394 if (bdev->driver->invalidate_caches) {
395 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
397 pr_err("Can not flush read caches\n");
403 bo->offset = (bo->mem.start << PAGE_SHIFT) +
404 bdev->man[bo->mem.mem_type].gpu_offset;
408 ctx->bytes_moved += bo->num_pages << PAGE_SHIFT;
412 new_man = &bdev->man[bo->mem.mem_type];
413 if (new_man->flags & TTM_MEMTYPE_FLAG_FIXED) {
414 ttm_tt_destroy(bo->ttm);
423 * Will release GPU memory type usage on destruction.
424 * This is the place to put in driver specific hooks to release
425 * driver private resources.
426 * Will release the bo::reserved lock.
429 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
431 if (bo->bdev->driver->move_notify)
432 bo->bdev->driver->move_notify(bo, false, NULL);
434 ttm_tt_destroy(bo->ttm);
436 ttm_bo_mem_put(bo, &bo->mem);
439 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
443 if (bo->base.resv == &bo->base._resv)
446 BUG_ON(!dma_resv_trylock(&bo->base._resv));
448 r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
450 dma_resv_unlock(&bo->base._resv);
455 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
457 struct dma_resv_list *fobj;
458 struct dma_fence *fence;
461 fobj = dma_resv_get_list(&bo->base._resv);
462 fence = dma_resv_get_excl(&bo->base._resv);
463 if (fence && !fence->ops->signaled)
464 dma_fence_enable_sw_signaling(fence);
466 for (i = 0; fobj && i < fobj->shared_count; ++i) {
467 fence = rcu_dereference_protected(fobj->shared[i],
468 dma_resv_held(bo->base.resv));
470 if (!fence->ops->signaled)
471 dma_fence_enable_sw_signaling(fence);
475 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
477 struct ttm_bo_device *bdev = bo->bdev;
478 struct ttm_bo_global *glob = bdev->glob;
481 ret = ttm_bo_individualize_resv(bo);
483 /* Last resort, if we fail to allocate memory for the
484 * fences block for the BO to become idle
486 dma_resv_wait_timeout_rcu(bo->base.resv, true, false,
488 spin_lock(&glob->lru_lock);
492 spin_lock(&glob->lru_lock);
493 ret = dma_resv_trylock(bo->base.resv) ? 0 : -EBUSY;
495 if (dma_resv_test_signaled_rcu(&bo->base._resv, true)) {
496 ttm_bo_del_from_lru(bo);
497 spin_unlock(&glob->lru_lock);
498 if (bo->base.resv != &bo->base._resv)
499 dma_resv_unlock(&bo->base._resv);
501 ttm_bo_cleanup_memtype_use(bo);
502 dma_resv_unlock(bo->base.resv);
506 ttm_bo_flush_all_fences(bo);
509 * Make NO_EVICT bos immediately available to
510 * shrinkers, now that they are queued for
513 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
514 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
515 ttm_bo_add_to_lru(bo);
518 dma_resv_unlock(bo->base.resv);
520 if (bo->base.resv != &bo->base._resv) {
521 ttm_bo_flush_all_fences(bo);
522 dma_resv_unlock(&bo->base._resv);
526 kref_get(&bo->list_kref);
527 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
528 spin_unlock(&glob->lru_lock);
530 schedule_delayed_work(&bdev->wq,
531 ((HZ / 100) < 1) ? 1 : HZ / 100);
535 * function ttm_bo_cleanup_refs
536 * If bo idle, remove from delayed- and lru lists, and unref.
537 * If not idle, do nothing.
539 * Must be called with lru_lock and reservation held, this function
540 * will drop the lru lock and optionally the reservation lock before returning.
542 * @interruptible Any sleeps should occur interruptibly.
543 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
544 * @unlock_resv Unlock the reservation lock as well.
547 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
548 bool interruptible, bool no_wait_gpu,
551 struct ttm_bo_global *glob = bo->bdev->glob;
552 struct dma_resv *resv;
555 if (unlikely(list_empty(&bo->ddestroy)))
556 resv = bo->base.resv;
558 resv = &bo->base._resv;
560 if (dma_resv_test_signaled_rcu(resv, true))
565 if (ret && !no_wait_gpu) {
569 dma_resv_unlock(bo->base.resv);
570 spin_unlock(&glob->lru_lock);
572 lret = dma_resv_wait_timeout_rcu(resv, true,
581 spin_lock(&glob->lru_lock);
582 if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
584 * We raced, and lost, someone else holds the reservation now,
585 * and is probably busy in ttm_bo_cleanup_memtype_use.
587 * Even if it's not the case, because we finished waiting any
588 * delayed destruction would succeed, so just return success
591 spin_unlock(&glob->lru_lock);
597 if (ret || unlikely(list_empty(&bo->ddestroy))) {
599 dma_resv_unlock(bo->base.resv);
600 spin_unlock(&glob->lru_lock);
604 ttm_bo_del_from_lru(bo);
605 list_del_init(&bo->ddestroy);
606 kref_put(&bo->list_kref, ttm_bo_ref_bug);
608 spin_unlock(&glob->lru_lock);
609 ttm_bo_cleanup_memtype_use(bo);
612 dma_resv_unlock(bo->base.resv);
618 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
619 * encountered buffers.
621 static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
623 struct ttm_bo_global *glob = bdev->glob;
624 struct list_head removed;
627 INIT_LIST_HEAD(&removed);
629 spin_lock(&glob->lru_lock);
630 while (!list_empty(&bdev->ddestroy)) {
631 struct ttm_buffer_object *bo;
633 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
635 kref_get(&bo->list_kref);
636 list_move_tail(&bo->ddestroy, &removed);
638 if (remove_all || bo->base.resv != &bo->base._resv) {
639 spin_unlock(&glob->lru_lock);
640 dma_resv_lock(bo->base.resv, NULL);
642 spin_lock(&glob->lru_lock);
643 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
645 } else if (dma_resv_trylock(bo->base.resv)) {
646 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
648 spin_unlock(&glob->lru_lock);
651 kref_put(&bo->list_kref, ttm_bo_release_list);
652 spin_lock(&glob->lru_lock);
654 list_splice_tail(&removed, &bdev->ddestroy);
655 empty = list_empty(&bdev->ddestroy);
656 spin_unlock(&glob->lru_lock);
661 static void ttm_bo_delayed_workqueue(struct work_struct *work)
663 struct ttm_bo_device *bdev =
664 container_of(work, struct ttm_bo_device, wq.work);
666 if (!ttm_bo_delayed_delete(bdev, false))
667 schedule_delayed_work(&bdev->wq,
668 ((HZ / 100) < 1) ? 1 : HZ / 100);
671 static void ttm_bo_release(struct kref *kref)
673 struct ttm_buffer_object *bo =
674 container_of(kref, struct ttm_buffer_object, kref);
675 struct ttm_bo_device *bdev = bo->bdev;
676 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
678 if (bo->bdev->driver->release_notify)
679 bo->bdev->driver->release_notify(bo);
681 drm_vma_offset_remove(&bdev->vma_manager, &bo->base.vma_node);
682 ttm_mem_io_lock(man, false);
683 ttm_mem_io_free_vm(bo);
684 ttm_mem_io_unlock(man);
685 ttm_bo_cleanup_refs_or_queue(bo);
686 kref_put(&bo->list_kref, ttm_bo_release_list);
689 void ttm_bo_put(struct ttm_buffer_object *bo)
691 kref_put(&bo->kref, ttm_bo_release);
693 EXPORT_SYMBOL(ttm_bo_put);
695 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
697 return cancel_delayed_work_sync(&bdev->wq);
699 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
701 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
704 schedule_delayed_work(&bdev->wq,
705 ((HZ / 100) < 1) ? 1 : HZ / 100);
707 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
709 static int ttm_bo_evict(struct ttm_buffer_object *bo,
710 struct ttm_operation_ctx *ctx)
712 struct ttm_bo_device *bdev = bo->bdev;
713 struct ttm_mem_reg evict_mem;
714 struct ttm_placement placement;
717 dma_resv_assert_held(bo->base.resv);
719 placement.num_placement = 0;
720 placement.num_busy_placement = 0;
721 bdev->driver->evict_flags(bo, &placement);
723 if (!placement.num_placement && !placement.num_busy_placement) {
724 ret = ttm_bo_pipeline_gutting(bo);
728 return ttm_tt_create(bo, false);
732 evict_mem.mm_node = NULL;
733 evict_mem.bus.io_reserved_vm = false;
734 evict_mem.bus.io_reserved_count = 0;
736 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
738 if (ret != -ERESTARTSYS) {
739 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
741 ttm_bo_mem_space_debug(bo, &placement);
746 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx);
748 if (ret != -ERESTARTSYS)
749 pr_err("Buffer eviction failed\n");
750 ttm_bo_mem_put(bo, &evict_mem);
758 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
759 const struct ttm_place *place)
761 /* Don't evict this BO if it's outside of the
762 * requested placement range
764 if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
765 (place->lpfn && place->lpfn <= bo->mem.start))
770 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
773 * Check the target bo is allowable to be evicted or swapout, including cases:
775 * a. if share same reservation object with ctx->resv, have assumption
776 * reservation objects should already be locked, so not lock again and
777 * return true directly when either the opreation allow_reserved_eviction
778 * or the target bo already is in delayed free list;
780 * b. Otherwise, trylock it.
782 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
783 struct ttm_operation_ctx *ctx, bool *locked, bool *busy)
787 if (bo->base.resv == ctx->resv) {
788 dma_resv_assert_held(bo->base.resv);
789 if (ctx->flags & TTM_OPT_FLAG_ALLOW_RES_EVICT
790 || !list_empty(&bo->ddestroy))
796 ret = dma_resv_trylock(bo->base.resv);
806 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
808 * @busy_bo: BO which couldn't be locked with trylock
809 * @ctx: operation context
810 * @ticket: acquire ticket
812 * Try to lock a busy buffer object to avoid failing eviction.
814 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
815 struct ttm_operation_ctx *ctx,
816 struct ww_acquire_ctx *ticket)
820 if (!busy_bo || !ticket)
823 if (ctx->interruptible)
824 r = dma_resv_lock_interruptible(busy_bo->base.resv,
827 r = dma_resv_lock(busy_bo->base.resv, ticket);
830 * TODO: It would be better to keep the BO locked until allocation is at
831 * least tried one more time, but that would mean a much larger rework
835 dma_resv_unlock(busy_bo->base.resv);
837 return r == -EDEADLK ? -EBUSY : r;
840 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
842 const struct ttm_place *place,
843 struct ttm_operation_ctx *ctx,
844 struct ww_acquire_ctx *ticket)
846 struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
847 struct ttm_bo_global *glob = bdev->glob;
848 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
853 spin_lock(&glob->lru_lock);
854 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
855 list_for_each_entry(bo, &man->lru[i], lru) {
858 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
860 if (busy && !busy_bo && ticket !=
861 dma_resv_locking_ctx(bo->base.resv))
866 if (place && !bdev->driver->eviction_valuable(bo,
869 dma_resv_unlock(bo->base.resv);
875 /* If the inner loop terminated early, we have our candidate */
876 if (&bo->lru != &man->lru[i])
884 kref_get(&busy_bo->list_kref);
885 spin_unlock(&glob->lru_lock);
886 ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
888 kref_put(&busy_bo->list_kref, ttm_bo_release_list);
892 kref_get(&bo->list_kref);
894 if (!list_empty(&bo->ddestroy)) {
895 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
896 ctx->no_wait_gpu, locked);
897 kref_put(&bo->list_kref, ttm_bo_release_list);
901 ttm_bo_del_from_lru(bo);
902 spin_unlock(&glob->lru_lock);
904 ret = ttm_bo_evict(bo, ctx);
906 ttm_bo_unreserve(bo);
908 spin_lock(&glob->lru_lock);
909 ttm_bo_add_to_lru(bo);
910 spin_unlock(&glob->lru_lock);
913 kref_put(&bo->list_kref, ttm_bo_release_list);
917 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
919 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
922 (*man->func->put_node)(man, mem);
924 EXPORT_SYMBOL(ttm_bo_mem_put);
927 * Add the last move fence to the BO and reserve a new shared slot.
929 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
930 struct ttm_mem_type_manager *man,
931 struct ttm_mem_reg *mem,
934 struct dma_fence *fence;
937 spin_lock(&man->move_lock);
938 fence = dma_fence_get(man->move);
939 spin_unlock(&man->move_lock);
947 dma_resv_add_shared_fence(bo->base.resv, fence);
949 ret = dma_resv_reserve_shared(bo->base.resv, 1);
951 dma_fence_put(fence);
955 dma_fence_put(bo->moving);
961 * Repeatedly evict memory from the LRU for @mem_type until we create enough
962 * space, or we've evicted everything and there isn't enough space.
964 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
965 const struct ttm_place *place,
966 struct ttm_mem_reg *mem,
967 struct ttm_operation_ctx *ctx)
969 struct ttm_bo_device *bdev = bo->bdev;
970 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
971 struct ww_acquire_ctx *ticket;
974 ticket = dma_resv_locking_ctx(bo->base.resv);
976 ret = (*man->func->get_node)(man, bo, place, mem);
977 if (unlikely(ret != 0))
981 ret = ttm_mem_evict_first(bdev, mem->mem_type, place, ctx,
983 if (unlikely(ret != 0))
987 return ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
990 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
991 uint32_t cur_placement,
992 uint32_t proposed_placement)
994 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
995 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
998 * Keep current caching if possible.
1001 if ((cur_placement & caching) != 0)
1002 result |= (cur_placement & caching);
1003 else if ((man->default_caching & caching) != 0)
1004 result |= man->default_caching;
1005 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
1006 result |= TTM_PL_FLAG_CACHED;
1007 else if ((TTM_PL_FLAG_WC & caching) != 0)
1008 result |= TTM_PL_FLAG_WC;
1009 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
1010 result |= TTM_PL_FLAG_UNCACHED;
1015 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
1017 const struct ttm_place *place,
1018 uint32_t *masked_placement)
1020 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
1022 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
1025 if ((place->flags & man->available_caching) == 0)
1028 cur_flags |= (place->flags & man->available_caching);
1030 *masked_placement = cur_flags;
1035 * ttm_bo_mem_placement - check if placement is compatible
1036 * @bo: BO to find memory for
1037 * @place: where to search
1038 * @mem: the memory object to fill in
1039 * @ctx: operation context
1041 * Check if placement is compatible and fill in mem structure.
1042 * Returns -EBUSY if placement won't work or negative error code.
1043 * 0 when placement can be used.
1045 static int ttm_bo_mem_placement(struct ttm_buffer_object *bo,
1046 const struct ttm_place *place,
1047 struct ttm_mem_reg *mem,
1048 struct ttm_operation_ctx *ctx)
1050 struct ttm_bo_device *bdev = bo->bdev;
1051 uint32_t mem_type = TTM_PL_SYSTEM;
1052 struct ttm_mem_type_manager *man;
1053 uint32_t cur_flags = 0;
1056 ret = ttm_mem_type_from_place(place, &mem_type);
1060 man = &bdev->man[mem_type];
1061 if (!man->has_type || !man->use_type)
1064 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
1067 cur_flags = ttm_bo_select_caching(man, bo->mem.placement, cur_flags);
1069 * Use the access and other non-mapping-related flag bits from
1070 * the memory placement flags to the current flags
1072 ttm_flag_masked(&cur_flags, place->flags, ~TTM_PL_MASK_MEMTYPE);
1074 mem->mem_type = mem_type;
1075 mem->placement = cur_flags;
1077 if (bo->mem.mem_type < mem_type && !list_empty(&bo->lru)) {
1078 spin_lock(&bo->bdev->glob->lru_lock);
1079 ttm_bo_del_from_lru(bo);
1080 ttm_bo_add_mem_to_lru(bo, mem);
1081 spin_unlock(&bo->bdev->glob->lru_lock);
1088 * Creates space for memory region @mem according to its type.
1090 * This function first searches for free space in compatible memory types in
1091 * the priority order defined by the driver. If free space isn't found, then
1092 * ttm_bo_mem_force_space is attempted in priority order to evict and find
1095 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
1096 struct ttm_placement *placement,
1097 struct ttm_mem_reg *mem,
1098 struct ttm_operation_ctx *ctx)
1100 struct ttm_bo_device *bdev = bo->bdev;
1101 bool type_found = false;
1104 ret = dma_resv_reserve_shared(bo->base.resv, 1);
1108 mem->mm_node = NULL;
1109 for (i = 0; i < placement->num_placement; ++i) {
1110 const struct ttm_place *place = &placement->placement[i];
1111 struct ttm_mem_type_manager *man;
1113 ret = ttm_bo_mem_placement(bo, place, mem, ctx);
1120 mem->mm_node = NULL;
1121 if (mem->mem_type == TTM_PL_SYSTEM)
1124 man = &bdev->man[mem->mem_type];
1125 ret = (*man->func->get_node)(man, bo, place, mem);
1132 ret = ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
1133 if (unlikely(ret)) {
1134 (*man->func->put_node)(man, mem);
1143 for (i = 0; i < placement->num_busy_placement; ++i) {
1144 const struct ttm_place *place = &placement->busy_placement[i];
1146 ret = ttm_bo_mem_placement(bo, place, mem, ctx);
1153 mem->mm_node = NULL;
1154 if (mem->mem_type == TTM_PL_SYSTEM)
1157 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
1158 if (ret == 0 && mem->mm_node)
1161 if (ret && ret != -EBUSY)
1167 pr_err(TTM_PFX "No compatible memory type found\n");
1172 if (bo->mem.mem_type == TTM_PL_SYSTEM && !list_empty(&bo->lru)) {
1173 spin_lock(&bo->bdev->glob->lru_lock);
1174 ttm_bo_move_to_lru_tail(bo, NULL);
1175 spin_unlock(&bo->bdev->glob->lru_lock);
1180 EXPORT_SYMBOL(ttm_bo_mem_space);
1182 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1183 struct ttm_placement *placement,
1184 struct ttm_operation_ctx *ctx)
1187 struct ttm_mem_reg mem;
1189 dma_resv_assert_held(bo->base.resv);
1191 mem.num_pages = bo->num_pages;
1192 mem.size = mem.num_pages << PAGE_SHIFT;
1193 mem.page_alignment = bo->mem.page_alignment;
1194 mem.bus.io_reserved_vm = false;
1195 mem.bus.io_reserved_count = 0;
1197 * Determine where to move the buffer.
1199 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
1202 ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx);
1204 if (ret && mem.mm_node)
1205 ttm_bo_mem_put(bo, &mem);
1209 static bool ttm_bo_places_compat(const struct ttm_place *places,
1210 unsigned num_placement,
1211 struct ttm_mem_reg *mem,
1212 uint32_t *new_flags)
1216 for (i = 0; i < num_placement; i++) {
1217 const struct ttm_place *heap = &places[i];
1219 if (mem->mm_node && (mem->start < heap->fpfn ||
1220 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1223 *new_flags = heap->flags;
1224 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1225 (*new_flags & mem->placement & TTM_PL_MASK_MEM) &&
1226 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
1227 (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
1233 bool ttm_bo_mem_compat(struct ttm_placement *placement,
1234 struct ttm_mem_reg *mem,
1235 uint32_t *new_flags)
1237 if (ttm_bo_places_compat(placement->placement, placement->num_placement,
1241 if ((placement->busy_placement != placement->placement ||
1242 placement->num_busy_placement > placement->num_placement) &&
1243 ttm_bo_places_compat(placement->busy_placement,
1244 placement->num_busy_placement,
1250 EXPORT_SYMBOL(ttm_bo_mem_compat);
1252 int ttm_bo_validate(struct ttm_buffer_object *bo,
1253 struct ttm_placement *placement,
1254 struct ttm_operation_ctx *ctx)
1259 dma_resv_assert_held(bo->base.resv);
1261 * Check whether we need to move buffer.
1263 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1264 ret = ttm_bo_move_buffer(bo, placement, ctx);
1269 * Use the access and other non-mapping-related flag bits from
1270 * the compatible memory placement flags to the active flags
1272 ttm_flag_masked(&bo->mem.placement, new_flags,
1273 ~TTM_PL_MASK_MEMTYPE);
1276 * We might need to add a TTM.
1278 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1279 ret = ttm_tt_create(bo, true);
1285 EXPORT_SYMBOL(ttm_bo_validate);
1287 int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
1288 struct ttm_buffer_object *bo,
1290 enum ttm_bo_type type,
1291 struct ttm_placement *placement,
1292 uint32_t page_alignment,
1293 struct ttm_operation_ctx *ctx,
1295 struct sg_table *sg,
1296 struct dma_resv *resv,
1297 void (*destroy) (struct ttm_buffer_object *))
1300 unsigned long num_pages;
1301 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1304 ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
1306 pr_err("Out of kernel memory\n");
1314 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1315 if (num_pages == 0) {
1316 pr_err("Illegal buffer object size\n");
1321 ttm_mem_global_free(mem_glob, acc_size);
1324 bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1326 kref_init(&bo->kref);
1327 kref_init(&bo->list_kref);
1328 atomic_set(&bo->cpu_writers, 0);
1329 INIT_LIST_HEAD(&bo->lru);
1330 INIT_LIST_HEAD(&bo->ddestroy);
1331 INIT_LIST_HEAD(&bo->swap);
1332 INIT_LIST_HEAD(&bo->io_reserve_lru);
1333 mutex_init(&bo->wu_mutex);
1336 bo->num_pages = num_pages;
1337 bo->mem.size = num_pages << PAGE_SHIFT;
1338 bo->mem.mem_type = TTM_PL_SYSTEM;
1339 bo->mem.num_pages = bo->num_pages;
1340 bo->mem.mm_node = NULL;
1341 bo->mem.page_alignment = page_alignment;
1342 bo->mem.bus.io_reserved_vm = false;
1343 bo->mem.bus.io_reserved_count = 0;
1345 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1346 bo->acc_size = acc_size;
1349 bo->base.resv = resv;
1350 dma_resv_assert_held(bo->base.resv);
1352 bo->base.resv = &bo->base._resv;
1354 if (!ttm_bo_uses_embedded_gem_object(bo)) {
1356 * bo.gem is not initialized, so we have to setup the
1357 * struct elements we want use regardless.
1359 dma_resv_init(&bo->base._resv);
1360 drm_vma_node_reset(&bo->base.vma_node);
1362 atomic_inc(&bo->bdev->glob->bo_count);
1365 * For ttm_bo_type_device buffers, allocate
1366 * address space from the device.
1368 if (bo->type == ttm_bo_type_device ||
1369 bo->type == ttm_bo_type_sg)
1370 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->base.vma_node,
1373 /* passed reservation objects should already be locked,
1374 * since otherwise lockdep will be angered in radeon.
1377 locked = dma_resv_trylock(bo->base.resv);
1382 ret = ttm_bo_validate(bo, placement, ctx);
1384 if (unlikely(ret)) {
1386 ttm_bo_unreserve(bo);
1392 if (resv && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
1393 spin_lock(&bdev->glob->lru_lock);
1394 ttm_bo_add_to_lru(bo);
1395 spin_unlock(&bdev->glob->lru_lock);
1400 EXPORT_SYMBOL(ttm_bo_init_reserved);
1402 int ttm_bo_init(struct ttm_bo_device *bdev,
1403 struct ttm_buffer_object *bo,
1405 enum ttm_bo_type type,
1406 struct ttm_placement *placement,
1407 uint32_t page_alignment,
1410 struct sg_table *sg,
1411 struct dma_resv *resv,
1412 void (*destroy) (struct ttm_buffer_object *))
1414 struct ttm_operation_ctx ctx = { interruptible, false };
1417 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1418 page_alignment, &ctx, acc_size,
1424 ttm_bo_unreserve(bo);
1428 EXPORT_SYMBOL(ttm_bo_init);
1430 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1431 unsigned long bo_size,
1432 unsigned struct_size)
1434 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1437 size += ttm_round_pot(struct_size);
1438 size += ttm_round_pot(npages * sizeof(void *));
1439 size += ttm_round_pot(sizeof(struct ttm_tt));
1442 EXPORT_SYMBOL(ttm_bo_acc_size);
1444 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1445 unsigned long bo_size,
1446 unsigned struct_size)
1448 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1451 size += ttm_round_pot(struct_size);
1452 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
1453 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1456 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1458 int ttm_bo_create(struct ttm_bo_device *bdev,
1460 enum ttm_bo_type type,
1461 struct ttm_placement *placement,
1462 uint32_t page_alignment,
1464 struct ttm_buffer_object **p_bo)
1466 struct ttm_buffer_object *bo;
1470 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1471 if (unlikely(bo == NULL))
1474 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1475 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1476 interruptible, acc_size,
1478 if (likely(ret == 0))
1483 EXPORT_SYMBOL(ttm_bo_create);
1485 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1488 struct ttm_operation_ctx ctx = {
1489 .interruptible = false,
1490 .no_wait_gpu = false,
1491 .flags = TTM_OPT_FLAG_FORCE_ALLOC
1493 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1494 struct ttm_bo_global *glob = bdev->glob;
1495 struct dma_fence *fence;
1500 * Can't use standard list traversal since we're unlocking.
1503 spin_lock(&glob->lru_lock);
1504 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1505 while (!list_empty(&man->lru[i])) {
1506 spin_unlock(&glob->lru_lock);
1507 ret = ttm_mem_evict_first(bdev, mem_type, NULL, &ctx,
1511 spin_lock(&glob->lru_lock);
1514 spin_unlock(&glob->lru_lock);
1516 spin_lock(&man->move_lock);
1517 fence = dma_fence_get(man->move);
1518 spin_unlock(&man->move_lock);
1521 ret = dma_fence_wait(fence, false);
1522 dma_fence_put(fence);
1530 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1532 struct ttm_mem_type_manager *man;
1535 if (mem_type >= TTM_NUM_MEM_TYPES) {
1536 pr_err("Illegal memory type %d\n", mem_type);
1539 man = &bdev->man[mem_type];
1541 if (!man->has_type) {
1542 pr_err("Trying to take down uninitialized memory manager type %u\n",
1547 man->use_type = false;
1548 man->has_type = false;
1552 ret = ttm_bo_force_list_clean(bdev, mem_type);
1554 pr_err("Cleanup eviction failed\n");
1558 ret = (*man->func->takedown)(man);
1561 dma_fence_put(man->move);
1566 EXPORT_SYMBOL(ttm_bo_clean_mm);
1568 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1570 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1572 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1573 pr_err("Illegal memory manager memory type %u\n", mem_type);
1577 if (!man->has_type) {
1578 pr_err("Memory type %u has not been initialized\n", mem_type);
1582 return ttm_bo_force_list_clean(bdev, mem_type);
1584 EXPORT_SYMBOL(ttm_bo_evict_mm);
1586 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1587 unsigned long p_size)
1590 struct ttm_mem_type_manager *man;
1593 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1594 man = &bdev->man[type];
1595 BUG_ON(man->has_type);
1596 man->io_reserve_fastpath = true;
1597 man->use_io_reserve_lru = false;
1598 mutex_init(&man->io_reserve_mutex);
1599 spin_lock_init(&man->move_lock);
1600 INIT_LIST_HEAD(&man->io_reserve_lru);
1602 ret = bdev->driver->init_mem_type(bdev, type, man);
1607 if (type != TTM_PL_SYSTEM) {
1608 ret = (*man->func->init)(man, p_size);
1612 man->has_type = true;
1613 man->use_type = true;
1616 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1617 INIT_LIST_HEAD(&man->lru[i]);
1622 EXPORT_SYMBOL(ttm_bo_init_mm);
1624 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1626 struct ttm_bo_global *glob =
1627 container_of(kobj, struct ttm_bo_global, kobj);
1629 __free_page(glob->dummy_read_page);
1632 static void ttm_bo_global_release(void)
1634 struct ttm_bo_global *glob = &ttm_bo_glob;
1636 mutex_lock(&ttm_global_mutex);
1637 if (--ttm_bo_glob_use_count > 0)
1640 kobject_del(&glob->kobj);
1641 kobject_put(&glob->kobj);
1642 ttm_mem_global_release(&ttm_mem_glob);
1643 memset(glob, 0, sizeof(*glob));
1645 mutex_unlock(&ttm_global_mutex);
1648 static int ttm_bo_global_init(void)
1650 struct ttm_bo_global *glob = &ttm_bo_glob;
1654 mutex_lock(&ttm_global_mutex);
1655 if (++ttm_bo_glob_use_count > 1)
1658 ret = ttm_mem_global_init(&ttm_mem_glob);
1662 spin_lock_init(&glob->lru_lock);
1663 glob->mem_glob = &ttm_mem_glob;
1664 glob->mem_glob->bo_glob = glob;
1665 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1667 if (unlikely(glob->dummy_read_page == NULL)) {
1672 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1673 INIT_LIST_HEAD(&glob->swap_lru[i]);
1674 INIT_LIST_HEAD(&glob->device_list);
1675 atomic_set(&glob->bo_count, 0);
1677 ret = kobject_init_and_add(
1678 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1679 if (unlikely(ret != 0))
1680 kobject_put(&glob->kobj);
1682 mutex_unlock(&ttm_global_mutex);
1686 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1689 unsigned i = TTM_NUM_MEM_TYPES;
1690 struct ttm_mem_type_manager *man;
1691 struct ttm_bo_global *glob = bdev->glob;
1694 man = &bdev->man[i];
1695 if (man->has_type) {
1696 man->use_type = false;
1697 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1699 pr_err("DRM memory manager type %d is not clean\n",
1702 man->has_type = false;
1706 mutex_lock(&ttm_global_mutex);
1707 list_del(&bdev->device_list);
1708 mutex_unlock(&ttm_global_mutex);
1710 cancel_delayed_work_sync(&bdev->wq);
1712 if (ttm_bo_delayed_delete(bdev, true))
1713 pr_debug("Delayed destroy list was clean\n");
1715 spin_lock(&glob->lru_lock);
1716 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1717 if (list_empty(&bdev->man[0].lru[0]))
1718 pr_debug("Swap list %d was clean\n", i);
1719 spin_unlock(&glob->lru_lock);
1721 drm_vma_offset_manager_destroy(&bdev->vma_manager);
1724 ttm_bo_global_release();
1728 EXPORT_SYMBOL(ttm_bo_device_release);
1730 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1731 struct ttm_bo_driver *driver,
1732 struct address_space *mapping,
1735 struct ttm_bo_global *glob = &ttm_bo_glob;
1738 ret = ttm_bo_global_init();
1742 bdev->driver = driver;
1744 memset(bdev->man, 0, sizeof(bdev->man));
1747 * Initialize the system memory buffer type.
1748 * Other types need to be driver / IOCTL initialized.
1750 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1751 if (unlikely(ret != 0))
1754 drm_vma_offset_manager_init(&bdev->vma_manager,
1755 DRM_FILE_PAGE_OFFSET_START,
1756 DRM_FILE_PAGE_OFFSET_SIZE);
1757 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1758 INIT_LIST_HEAD(&bdev->ddestroy);
1759 bdev->dev_mapping = mapping;
1761 bdev->need_dma32 = need_dma32;
1762 mutex_lock(&ttm_global_mutex);
1763 list_add_tail(&bdev->device_list, &glob->device_list);
1764 mutex_unlock(&ttm_global_mutex);
1768 ttm_bo_global_release();
1771 EXPORT_SYMBOL(ttm_bo_device_init);
1774 * buffer object vm functions.
1777 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1779 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1781 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1782 if (mem->mem_type == TTM_PL_SYSTEM)
1785 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1788 if (mem->placement & TTM_PL_FLAG_CACHED)
1794 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1796 struct ttm_bo_device *bdev = bo->bdev;
1798 drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1799 ttm_mem_io_free_vm(bo);
1802 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1804 struct ttm_bo_device *bdev = bo->bdev;
1805 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1807 ttm_mem_io_lock(man, false);
1808 ttm_bo_unmap_virtual_locked(bo);
1809 ttm_mem_io_unlock(man);
1813 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1815 int ttm_bo_wait(struct ttm_buffer_object *bo,
1816 bool interruptible, bool no_wait)
1818 long timeout = 15 * HZ;
1821 if (dma_resv_test_signaled_rcu(bo->base.resv, true))
1827 timeout = dma_resv_wait_timeout_rcu(bo->base.resv, true,
1828 interruptible, timeout);
1835 dma_resv_add_excl_fence(bo->base.resv, NULL);
1838 EXPORT_SYMBOL(ttm_bo_wait);
1840 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1845 * Using ttm_bo_reserve makes sure the lru lists are updated.
1848 ret = ttm_bo_reserve(bo, true, no_wait, NULL);
1849 if (unlikely(ret != 0))
1851 ret = ttm_bo_wait(bo, true, no_wait);
1852 if (likely(ret == 0))
1853 atomic_inc(&bo->cpu_writers);
1854 ttm_bo_unreserve(bo);
1857 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1859 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1861 atomic_dec(&bo->cpu_writers);
1863 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1866 * A buffer object shrink method that tries to swap out the first
1867 * buffer object on the bo_global::swap_lru list.
1869 int ttm_bo_swapout(struct ttm_bo_global *glob, struct ttm_operation_ctx *ctx)
1871 struct ttm_buffer_object *bo;
1876 spin_lock(&glob->lru_lock);
1877 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1878 list_for_each_entry(bo, &glob->swap_lru[i], swap) {
1879 if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
1890 spin_unlock(&glob->lru_lock);
1894 kref_get(&bo->list_kref);
1896 if (!list_empty(&bo->ddestroy)) {
1897 ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1898 kref_put(&bo->list_kref, ttm_bo_release_list);
1902 ttm_bo_del_from_lru(bo);
1903 spin_unlock(&glob->lru_lock);
1906 * Move to system cached
1909 if (bo->mem.mem_type != TTM_PL_SYSTEM ||
1910 bo->ttm->caching_state != tt_cached) {
1911 struct ttm_operation_ctx ctx = { false, false };
1912 struct ttm_mem_reg evict_mem;
1914 evict_mem = bo->mem;
1915 evict_mem.mm_node = NULL;
1916 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1917 evict_mem.mem_type = TTM_PL_SYSTEM;
1919 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx);
1920 if (unlikely(ret != 0))
1925 * Make sure BO is idle.
1928 ret = ttm_bo_wait(bo, false, false);
1929 if (unlikely(ret != 0))
1932 ttm_bo_unmap_virtual(bo);
1935 * Swap out. Buffer will be swapped in again as soon as
1936 * anyone tries to access a ttm page.
1939 if (bo->bdev->driver->swap_notify)
1940 bo->bdev->driver->swap_notify(bo);
1942 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1947 * Unreserve without putting on LRU to avoid swapping out an
1948 * already swapped buffer.
1951 dma_resv_unlock(bo->base.resv);
1952 kref_put(&bo->list_kref, ttm_bo_release_list);
1955 EXPORT_SYMBOL(ttm_bo_swapout);
1957 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1959 struct ttm_operation_ctx ctx = {
1960 .interruptible = false,
1961 .no_wait_gpu = false
1964 while (ttm_bo_swapout(bdev->glob, &ctx) == 0)
1967 EXPORT_SYMBOL(ttm_bo_swapout_all);
1970 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1973 * @bo: Pointer to buffer
1975 int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1980 * In the absense of a wait_unlocked API,
1981 * Use the bo::wu_mutex to avoid triggering livelocks due to
1982 * concurrent use of this function. Note that this use of
1983 * bo::wu_mutex can go away if we change locking order to
1984 * mmap_sem -> bo::reserve.
1986 ret = mutex_lock_interruptible(&bo->wu_mutex);
1987 if (unlikely(ret != 0))
1988 return -ERESTARTSYS;
1989 if (!dma_resv_is_locked(bo->base.resv))
1991 ret = dma_resv_lock_interruptible(bo->base.resv, NULL);
1994 if (unlikely(ret != 0))
1996 dma_resv_unlock(bo->base.resv);
1999 mutex_unlock(&bo->wu_mutex);