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/reservation.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 reservation_object_fini(&bo->ttm_resv);
164 mutex_destroy(&bo->wu_mutex);
166 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
169 static void ttm_bo_add_mem_to_lru(struct ttm_buffer_object *bo,
170 struct ttm_mem_reg *mem)
172 struct ttm_bo_device *bdev = bo->bdev;
173 struct ttm_mem_type_manager *man;
175 reservation_object_assert_held(bo->resv);
177 if (!list_empty(&bo->lru))
180 if (mem->placement & TTM_PL_FLAG_NO_EVICT)
183 man = &bdev->man[mem->mem_type];
184 list_add_tail(&bo->lru, &man->lru[bo->priority]);
185 kref_get(&bo->list_kref);
187 if (bo->ttm && !(bo->ttm->page_flags &
188 (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED))) {
189 list_add_tail(&bo->swap, &bdev->glob->swap_lru[bo->priority]);
190 kref_get(&bo->list_kref);
194 void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
196 ttm_bo_add_mem_to_lru(bo, &bo->mem);
198 EXPORT_SYMBOL(ttm_bo_add_to_lru);
200 static void ttm_bo_ref_bug(struct kref *list_kref)
205 void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
207 struct ttm_bo_device *bdev = bo->bdev;
210 if (!list_empty(&bo->swap)) {
211 list_del_init(&bo->swap);
212 kref_put(&bo->list_kref, ttm_bo_ref_bug);
215 if (!list_empty(&bo->lru)) {
216 list_del_init(&bo->lru);
217 kref_put(&bo->list_kref, ttm_bo_ref_bug);
221 if (notify && bdev->driver->del_from_lru_notify)
222 bdev->driver->del_from_lru_notify(bo);
225 void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
227 struct ttm_bo_global *glob = bo->bdev->glob;
229 spin_lock(&glob->lru_lock);
230 ttm_bo_del_from_lru(bo);
231 spin_unlock(&glob->lru_lock);
233 EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
235 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
236 struct ttm_buffer_object *bo)
243 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
244 struct ttm_lru_bulk_move *bulk)
246 reservation_object_assert_held(bo->resv);
248 ttm_bo_del_from_lru(bo);
249 ttm_bo_add_to_lru(bo);
251 if (bulk && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
252 switch (bo->mem.mem_type) {
254 ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
258 ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
261 if (bo->ttm && !(bo->ttm->page_flags &
262 (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED)))
263 ttm_bo_bulk_move_set_pos(&bulk->swap[bo->priority], bo);
266 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
268 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
272 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
273 struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
274 struct ttm_mem_type_manager *man;
279 reservation_object_assert_held(pos->first->resv);
280 reservation_object_assert_held(pos->last->resv);
282 man = &pos->first->bdev->man[TTM_PL_TT];
283 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
287 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
288 struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
289 struct ttm_mem_type_manager *man;
294 reservation_object_assert_held(pos->first->resv);
295 reservation_object_assert_held(pos->last->resv);
297 man = &pos->first->bdev->man[TTM_PL_VRAM];
298 list_bulk_move_tail(&man->lru[i], &pos->first->lru,
302 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
303 struct ttm_lru_bulk_move_pos *pos = &bulk->swap[i];
304 struct list_head *lru;
309 reservation_object_assert_held(pos->first->resv);
310 reservation_object_assert_held(pos->last->resv);
312 lru = &pos->first->bdev->glob->swap_lru[i];
313 list_bulk_move_tail(lru, &pos->first->swap, &pos->last->swap);
316 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
318 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
319 struct ttm_mem_reg *mem, bool evict,
320 struct ttm_operation_ctx *ctx)
322 struct ttm_bo_device *bdev = bo->bdev;
323 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
324 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
325 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
326 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
329 if (old_is_pci || new_is_pci ||
330 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
331 ret = ttm_mem_io_lock(old_man, true);
332 if (unlikely(ret != 0))
334 ttm_bo_unmap_virtual_locked(bo);
335 ttm_mem_io_unlock(old_man);
339 * Create and bind a ttm if required.
342 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
343 if (bo->ttm == NULL) {
344 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
345 ret = ttm_tt_create(bo, zero);
350 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
354 if (mem->mem_type != TTM_PL_SYSTEM) {
355 ret = ttm_tt_bind(bo->ttm, mem, ctx);
360 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
361 if (bdev->driver->move_notify)
362 bdev->driver->move_notify(bo, evict, mem);
369 if (bdev->driver->move_notify)
370 bdev->driver->move_notify(bo, evict, mem);
372 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
373 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
374 ret = ttm_bo_move_ttm(bo, ctx, mem);
375 else if (bdev->driver->move)
376 ret = bdev->driver->move(bo, evict, ctx, mem);
378 ret = ttm_bo_move_memcpy(bo, ctx, mem);
381 if (bdev->driver->move_notify) {
383 bdev->driver->move_notify(bo, false, mem);
392 if (bdev->driver->invalidate_caches) {
393 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
395 pr_err("Can not flush read caches\n");
401 bo->offset = (bo->mem.start << PAGE_SHIFT) +
402 bdev->man[bo->mem.mem_type].gpu_offset;
406 ctx->bytes_moved += bo->num_pages << PAGE_SHIFT;
410 new_man = &bdev->man[bo->mem.mem_type];
411 if (new_man->flags & TTM_MEMTYPE_FLAG_FIXED) {
412 ttm_tt_destroy(bo->ttm);
421 * Will release GPU memory type usage on destruction.
422 * This is the place to put in driver specific hooks to release
423 * driver private resources.
424 * Will release the bo::reserved lock.
427 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
429 if (bo->bdev->driver->move_notify)
430 bo->bdev->driver->move_notify(bo, false, NULL);
432 ttm_tt_destroy(bo->ttm);
434 ttm_bo_mem_put(bo, &bo->mem);
437 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
441 if (bo->resv == &bo->ttm_resv)
444 BUG_ON(!reservation_object_trylock(&bo->ttm_resv));
446 r = reservation_object_copy_fences(&bo->ttm_resv, bo->resv);
448 reservation_object_unlock(&bo->ttm_resv);
453 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
455 struct reservation_object_list *fobj;
456 struct dma_fence *fence;
459 fobj = reservation_object_get_list(&bo->ttm_resv);
460 fence = reservation_object_get_excl(&bo->ttm_resv);
461 if (fence && !fence->ops->signaled)
462 dma_fence_enable_sw_signaling(fence);
464 for (i = 0; fobj && i < fobj->shared_count; ++i) {
465 fence = rcu_dereference_protected(fobj->shared[i],
466 reservation_object_held(bo->resv));
468 if (!fence->ops->signaled)
469 dma_fence_enable_sw_signaling(fence);
473 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
475 struct ttm_bo_device *bdev = bo->bdev;
476 struct ttm_bo_global *glob = bdev->glob;
479 ret = ttm_bo_individualize_resv(bo);
481 /* Last resort, if we fail to allocate memory for the
482 * fences block for the BO to become idle
484 reservation_object_wait_timeout_rcu(bo->resv, true, false,
486 spin_lock(&glob->lru_lock);
490 spin_lock(&glob->lru_lock);
491 ret = reservation_object_trylock(bo->resv) ? 0 : -EBUSY;
493 if (reservation_object_test_signaled_rcu(&bo->ttm_resv, true)) {
494 ttm_bo_del_from_lru(bo);
495 spin_unlock(&glob->lru_lock);
496 if (bo->resv != &bo->ttm_resv)
497 reservation_object_unlock(&bo->ttm_resv);
499 ttm_bo_cleanup_memtype_use(bo);
500 reservation_object_unlock(bo->resv);
504 ttm_bo_flush_all_fences(bo);
507 * Make NO_EVICT bos immediately available to
508 * shrinkers, now that they are queued for
511 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
512 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
513 ttm_bo_add_to_lru(bo);
516 reservation_object_unlock(bo->resv);
518 if (bo->resv != &bo->ttm_resv)
519 reservation_object_unlock(&bo->ttm_resv);
522 kref_get(&bo->list_kref);
523 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
524 spin_unlock(&glob->lru_lock);
526 schedule_delayed_work(&bdev->wq,
527 ((HZ / 100) < 1) ? 1 : HZ / 100);
531 * function ttm_bo_cleanup_refs
532 * If bo idle, remove from delayed- and lru lists, and unref.
533 * If not idle, do nothing.
535 * Must be called with lru_lock and reservation held, this function
536 * will drop the lru lock and optionally the reservation lock before returning.
538 * @interruptible Any sleeps should occur interruptibly.
539 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
540 * @unlock_resv Unlock the reservation lock as well.
543 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
544 bool interruptible, bool no_wait_gpu,
547 struct ttm_bo_global *glob = bo->bdev->glob;
548 struct reservation_object *resv;
551 if (unlikely(list_empty(&bo->ddestroy)))
554 resv = &bo->ttm_resv;
556 if (reservation_object_test_signaled_rcu(resv, true))
561 if (ret && !no_wait_gpu) {
565 reservation_object_unlock(bo->resv);
566 spin_unlock(&glob->lru_lock);
568 lret = reservation_object_wait_timeout_rcu(resv, true,
577 spin_lock(&glob->lru_lock);
578 if (unlock_resv && !reservation_object_trylock(bo->resv)) {
580 * We raced, and lost, someone else holds the reservation now,
581 * and is probably busy in ttm_bo_cleanup_memtype_use.
583 * Even if it's not the case, because we finished waiting any
584 * delayed destruction would succeed, so just return success
587 spin_unlock(&glob->lru_lock);
593 if (ret || unlikely(list_empty(&bo->ddestroy))) {
595 reservation_object_unlock(bo->resv);
596 spin_unlock(&glob->lru_lock);
600 ttm_bo_del_from_lru(bo);
601 list_del_init(&bo->ddestroy);
602 kref_put(&bo->list_kref, ttm_bo_ref_bug);
604 spin_unlock(&glob->lru_lock);
605 ttm_bo_cleanup_memtype_use(bo);
608 reservation_object_unlock(bo->resv);
614 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
615 * encountered buffers.
617 static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
619 struct ttm_bo_global *glob = bdev->glob;
620 struct list_head removed;
623 INIT_LIST_HEAD(&removed);
625 spin_lock(&glob->lru_lock);
626 while (!list_empty(&bdev->ddestroy)) {
627 struct ttm_buffer_object *bo;
629 bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
631 kref_get(&bo->list_kref);
632 list_move_tail(&bo->ddestroy, &removed);
634 if (remove_all || bo->resv != &bo->ttm_resv) {
635 spin_unlock(&glob->lru_lock);
636 reservation_object_lock(bo->resv, NULL);
638 spin_lock(&glob->lru_lock);
639 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
641 } else if (reservation_object_trylock(bo->resv)) {
642 ttm_bo_cleanup_refs(bo, false, !remove_all, true);
644 spin_unlock(&glob->lru_lock);
647 kref_put(&bo->list_kref, ttm_bo_release_list);
648 spin_lock(&glob->lru_lock);
650 list_splice_tail(&removed, &bdev->ddestroy);
651 empty = list_empty(&bdev->ddestroy);
652 spin_unlock(&glob->lru_lock);
657 static void ttm_bo_delayed_workqueue(struct work_struct *work)
659 struct ttm_bo_device *bdev =
660 container_of(work, struct ttm_bo_device, wq.work);
662 if (!ttm_bo_delayed_delete(bdev, false))
663 schedule_delayed_work(&bdev->wq,
664 ((HZ / 100) < 1) ? 1 : HZ / 100);
667 static void ttm_bo_release(struct kref *kref)
669 struct ttm_buffer_object *bo =
670 container_of(kref, struct ttm_buffer_object, kref);
671 struct ttm_bo_device *bdev = bo->bdev;
672 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
674 drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
675 ttm_mem_io_lock(man, false);
676 ttm_mem_io_free_vm(bo);
677 ttm_mem_io_unlock(man);
678 ttm_bo_cleanup_refs_or_queue(bo);
679 kref_put(&bo->list_kref, ttm_bo_release_list);
682 void ttm_bo_put(struct ttm_buffer_object *bo)
684 kref_put(&bo->kref, ttm_bo_release);
686 EXPORT_SYMBOL(ttm_bo_put);
688 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
690 return cancel_delayed_work_sync(&bdev->wq);
692 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
694 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
697 schedule_delayed_work(&bdev->wq,
698 ((HZ / 100) < 1) ? 1 : HZ / 100);
700 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
702 static int ttm_bo_evict(struct ttm_buffer_object *bo,
703 struct ttm_operation_ctx *ctx)
705 struct ttm_bo_device *bdev = bo->bdev;
706 struct ttm_mem_reg evict_mem;
707 struct ttm_placement placement;
710 reservation_object_assert_held(bo->resv);
712 placement.num_placement = 0;
713 placement.num_busy_placement = 0;
714 bdev->driver->evict_flags(bo, &placement);
716 if (!placement.num_placement && !placement.num_busy_placement) {
717 ret = ttm_bo_pipeline_gutting(bo);
721 return ttm_tt_create(bo, false);
725 evict_mem.mm_node = NULL;
726 evict_mem.bus.io_reserved_vm = false;
727 evict_mem.bus.io_reserved_count = 0;
729 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
731 if (ret != -ERESTARTSYS) {
732 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
734 ttm_bo_mem_space_debug(bo, &placement);
739 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx);
741 if (ret != -ERESTARTSYS)
742 pr_err("Buffer eviction failed\n");
743 ttm_bo_mem_put(bo, &evict_mem);
751 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
752 const struct ttm_place *place)
754 /* Don't evict this BO if it's outside of the
755 * requested placement range
757 if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
758 (place->lpfn && place->lpfn <= bo->mem.start))
763 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
766 * Check the target bo is allowable to be evicted or swapout, including cases:
768 * a. if share same reservation object with ctx->resv, have assumption
769 * reservation objects should already be locked, so not lock again and
770 * return true directly when either the opreation allow_reserved_eviction
771 * or the target bo already is in delayed free list;
773 * b. Otherwise, trylock it.
775 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
776 struct ttm_operation_ctx *ctx, bool *locked)
781 if (bo->resv == ctx->resv) {
782 reservation_object_assert_held(bo->resv);
783 if (ctx->flags & TTM_OPT_FLAG_ALLOW_RES_EVICT
784 || !list_empty(&bo->ddestroy))
787 *locked = reservation_object_trylock(bo->resv);
794 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
796 const struct ttm_place *place,
797 struct ttm_operation_ctx *ctx)
799 struct ttm_bo_global *glob = bdev->glob;
800 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
801 struct ttm_buffer_object *bo = NULL;
806 spin_lock(&glob->lru_lock);
807 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
808 list_for_each_entry(bo, &man->lru[i], lru) {
809 if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked))
812 if (place && !bdev->driver->eviction_valuable(bo,
815 reservation_object_unlock(bo->resv);
821 /* If the inner loop terminated early, we have our candidate */
822 if (&bo->lru != &man->lru[i])
829 spin_unlock(&glob->lru_lock);
833 kref_get(&bo->list_kref);
835 if (!list_empty(&bo->ddestroy)) {
836 ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
837 ctx->no_wait_gpu, locked);
838 kref_put(&bo->list_kref, ttm_bo_release_list);
842 ttm_bo_del_from_lru(bo);
843 spin_unlock(&glob->lru_lock);
845 ret = ttm_bo_evict(bo, ctx);
847 ttm_bo_unreserve(bo);
849 spin_lock(&glob->lru_lock);
850 ttm_bo_add_to_lru(bo);
851 spin_unlock(&glob->lru_lock);
854 kref_put(&bo->list_kref, ttm_bo_release_list);
858 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
860 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
863 (*man->func->put_node)(man, mem);
865 EXPORT_SYMBOL(ttm_bo_mem_put);
868 * Add the last move fence to the BO and reserve a new shared slot.
870 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
871 struct ttm_mem_type_manager *man,
872 struct ttm_mem_reg *mem)
874 struct dma_fence *fence;
877 spin_lock(&man->move_lock);
878 fence = dma_fence_get(man->move);
879 spin_unlock(&man->move_lock);
882 reservation_object_add_shared_fence(bo->resv, fence);
884 ret = reservation_object_reserve_shared(bo->resv, 1);
886 dma_fence_put(fence);
890 dma_fence_put(bo->moving);
898 * Repeatedly evict memory from the LRU for @mem_type until we create enough
899 * space, or we've evicted everything and there isn't enough space.
901 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
902 const struct ttm_place *place,
903 struct ttm_mem_reg *mem,
904 struct ttm_operation_ctx *ctx)
906 struct ttm_bo_device *bdev = bo->bdev;
907 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
911 ret = (*man->func->get_node)(man, bo, place, mem);
912 if (unlikely(ret != 0))
916 ret = ttm_mem_evict_first(bdev, mem->mem_type, place, ctx);
917 if (unlikely(ret != 0))
921 return ttm_bo_add_move_fence(bo, man, mem);
924 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
925 uint32_t cur_placement,
926 uint32_t proposed_placement)
928 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
929 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
932 * Keep current caching if possible.
935 if ((cur_placement & caching) != 0)
936 result |= (cur_placement & caching);
937 else if ((man->default_caching & caching) != 0)
938 result |= man->default_caching;
939 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
940 result |= TTM_PL_FLAG_CACHED;
941 else if ((TTM_PL_FLAG_WC & caching) != 0)
942 result |= TTM_PL_FLAG_WC;
943 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
944 result |= TTM_PL_FLAG_UNCACHED;
949 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
951 const struct ttm_place *place,
952 uint32_t *masked_placement)
954 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
956 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
959 if ((place->flags & man->available_caching) == 0)
962 cur_flags |= (place->flags & man->available_caching);
964 *masked_placement = cur_flags;
969 * ttm_bo_mem_placement - check if placement is compatible
970 * @bo: BO to find memory for
971 * @place: where to search
972 * @mem: the memory object to fill in
973 * @ctx: operation context
975 * Check if placement is compatible and fill in mem structure.
976 * Returns -EBUSY if placement won't work or negative error code.
977 * 0 when placement can be used.
979 static int ttm_bo_mem_placement(struct ttm_buffer_object *bo,
980 const struct ttm_place *place,
981 struct ttm_mem_reg *mem,
982 struct ttm_operation_ctx *ctx)
984 struct ttm_bo_device *bdev = bo->bdev;
985 uint32_t mem_type = TTM_PL_SYSTEM;
986 struct ttm_mem_type_manager *man;
987 uint32_t cur_flags = 0;
990 ret = ttm_mem_type_from_place(place, &mem_type);
994 man = &bdev->man[mem_type];
995 if (!man->has_type || !man->use_type)
998 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
1001 cur_flags = ttm_bo_select_caching(man, bo->mem.placement, cur_flags);
1003 * Use the access and other non-mapping-related flag bits from
1004 * the memory placement flags to the current flags
1006 ttm_flag_masked(&cur_flags, place->flags, ~TTM_PL_MASK_MEMTYPE);
1008 mem->mem_type = mem_type;
1009 mem->placement = cur_flags;
1011 if (bo->mem.mem_type < mem_type && !list_empty(&bo->lru)) {
1012 spin_lock(&bo->bdev->glob->lru_lock);
1013 ttm_bo_del_from_lru(bo);
1014 ttm_bo_add_mem_to_lru(bo, mem);
1015 spin_unlock(&bo->bdev->glob->lru_lock);
1022 * Creates space for memory region @mem according to its type.
1024 * This function first searches for free space in compatible memory types in
1025 * the priority order defined by the driver. If free space isn't found, then
1026 * ttm_bo_mem_force_space is attempted in priority order to evict and find
1029 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
1030 struct ttm_placement *placement,
1031 struct ttm_mem_reg *mem,
1032 struct ttm_operation_ctx *ctx)
1034 struct ttm_bo_device *bdev = bo->bdev;
1035 bool type_found = false;
1038 ret = reservation_object_reserve_shared(bo->resv, 1);
1042 mem->mm_node = NULL;
1043 for (i = 0; i < placement->num_placement; ++i) {
1044 const struct ttm_place *place = &placement->placement[i];
1045 struct ttm_mem_type_manager *man;
1047 ret = ttm_bo_mem_placement(bo, place, mem, ctx);
1054 mem->mm_node = NULL;
1055 if (mem->mem_type == TTM_PL_SYSTEM)
1058 man = &bdev->man[mem->mem_type];
1059 ret = (*man->func->get_node)(man, bo, place, mem);
1064 ret = ttm_bo_add_move_fence(bo, man, mem);
1065 if (unlikely(ret)) {
1066 (*man->func->put_node)(man, mem);
1073 for (i = 0; i < placement->num_busy_placement; ++i) {
1074 const struct ttm_place *place = &placement->busy_placement[i];
1076 ret = ttm_bo_mem_placement(bo, place, mem, ctx);
1083 mem->mm_node = NULL;
1084 if (mem->mem_type == TTM_PL_SYSTEM)
1087 ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
1088 if (ret == 0 && mem->mm_node)
1091 if (ret && ret != -EBUSY)
1097 pr_err(TTM_PFX "No compatible memory type found\n");
1102 if (bo->mem.mem_type == TTM_PL_SYSTEM && !list_empty(&bo->lru)) {
1103 spin_lock(&bo->bdev->glob->lru_lock);
1104 ttm_bo_move_to_lru_tail(bo, NULL);
1105 spin_unlock(&bo->bdev->glob->lru_lock);
1110 EXPORT_SYMBOL(ttm_bo_mem_space);
1112 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
1113 struct ttm_placement *placement,
1114 struct ttm_operation_ctx *ctx)
1117 struct ttm_mem_reg mem;
1119 reservation_object_assert_held(bo->resv);
1121 mem.num_pages = bo->num_pages;
1122 mem.size = mem.num_pages << PAGE_SHIFT;
1123 mem.page_alignment = bo->mem.page_alignment;
1124 mem.bus.io_reserved_vm = false;
1125 mem.bus.io_reserved_count = 0;
1127 * Determine where to move the buffer.
1129 ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
1132 ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx);
1134 if (ret && mem.mm_node)
1135 ttm_bo_mem_put(bo, &mem);
1139 static bool ttm_bo_places_compat(const struct ttm_place *places,
1140 unsigned num_placement,
1141 struct ttm_mem_reg *mem,
1142 uint32_t *new_flags)
1146 for (i = 0; i < num_placement; i++) {
1147 const struct ttm_place *heap = &places[i];
1149 if (mem->mm_node && (mem->start < heap->fpfn ||
1150 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1153 *new_flags = heap->flags;
1154 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1155 (*new_flags & mem->placement & TTM_PL_MASK_MEM) &&
1156 (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
1157 (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
1163 bool ttm_bo_mem_compat(struct ttm_placement *placement,
1164 struct ttm_mem_reg *mem,
1165 uint32_t *new_flags)
1167 if (ttm_bo_places_compat(placement->placement, placement->num_placement,
1171 if ((placement->busy_placement != placement->placement ||
1172 placement->num_busy_placement > placement->num_placement) &&
1173 ttm_bo_places_compat(placement->busy_placement,
1174 placement->num_busy_placement,
1180 EXPORT_SYMBOL(ttm_bo_mem_compat);
1182 int ttm_bo_validate(struct ttm_buffer_object *bo,
1183 struct ttm_placement *placement,
1184 struct ttm_operation_ctx *ctx)
1189 reservation_object_assert_held(bo->resv);
1191 * Check whether we need to move buffer.
1193 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1194 ret = ttm_bo_move_buffer(bo, placement, ctx);
1199 * Use the access and other non-mapping-related flag bits from
1200 * the compatible memory placement flags to the active flags
1202 ttm_flag_masked(&bo->mem.placement, new_flags,
1203 ~TTM_PL_MASK_MEMTYPE);
1206 * We might need to add a TTM.
1208 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1209 ret = ttm_tt_create(bo, true);
1215 EXPORT_SYMBOL(ttm_bo_validate);
1217 int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
1218 struct ttm_buffer_object *bo,
1220 enum ttm_bo_type type,
1221 struct ttm_placement *placement,
1222 uint32_t page_alignment,
1223 struct ttm_operation_ctx *ctx,
1225 struct sg_table *sg,
1226 struct reservation_object *resv,
1227 void (*destroy) (struct ttm_buffer_object *))
1230 unsigned long num_pages;
1231 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1234 ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
1236 pr_err("Out of kernel memory\n");
1244 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1245 if (num_pages == 0) {
1246 pr_err("Illegal buffer object size\n");
1251 ttm_mem_global_free(mem_glob, acc_size);
1254 bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1256 kref_init(&bo->kref);
1257 kref_init(&bo->list_kref);
1258 atomic_set(&bo->cpu_writers, 0);
1259 INIT_LIST_HEAD(&bo->lru);
1260 INIT_LIST_HEAD(&bo->ddestroy);
1261 INIT_LIST_HEAD(&bo->swap);
1262 INIT_LIST_HEAD(&bo->io_reserve_lru);
1263 mutex_init(&bo->wu_mutex);
1266 bo->num_pages = num_pages;
1267 bo->mem.size = num_pages << PAGE_SHIFT;
1268 bo->mem.mem_type = TTM_PL_SYSTEM;
1269 bo->mem.num_pages = bo->num_pages;
1270 bo->mem.mm_node = NULL;
1271 bo->mem.page_alignment = page_alignment;
1272 bo->mem.bus.io_reserved_vm = false;
1273 bo->mem.bus.io_reserved_count = 0;
1275 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1276 bo->acc_size = acc_size;
1280 reservation_object_assert_held(bo->resv);
1282 bo->resv = &bo->ttm_resv;
1284 reservation_object_init(&bo->ttm_resv);
1285 atomic_inc(&bo->bdev->glob->bo_count);
1286 drm_vma_node_reset(&bo->vma_node);
1289 * For ttm_bo_type_device buffers, allocate
1290 * address space from the device.
1292 if (bo->type == ttm_bo_type_device ||
1293 bo->type == ttm_bo_type_sg)
1294 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
1297 /* passed reservation objects should already be locked,
1298 * since otherwise lockdep will be angered in radeon.
1301 locked = reservation_object_trylock(bo->resv);
1306 ret = ttm_bo_validate(bo, placement, ctx);
1308 if (unlikely(ret)) {
1310 ttm_bo_unreserve(bo);
1316 if (resv && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
1317 spin_lock(&bdev->glob->lru_lock);
1318 ttm_bo_add_to_lru(bo);
1319 spin_unlock(&bdev->glob->lru_lock);
1324 EXPORT_SYMBOL(ttm_bo_init_reserved);
1326 int ttm_bo_init(struct ttm_bo_device *bdev,
1327 struct ttm_buffer_object *bo,
1329 enum ttm_bo_type type,
1330 struct ttm_placement *placement,
1331 uint32_t page_alignment,
1334 struct sg_table *sg,
1335 struct reservation_object *resv,
1336 void (*destroy) (struct ttm_buffer_object *))
1338 struct ttm_operation_ctx ctx = { interruptible, false };
1341 ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1342 page_alignment, &ctx, acc_size,
1348 ttm_bo_unreserve(bo);
1352 EXPORT_SYMBOL(ttm_bo_init);
1354 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1355 unsigned long bo_size,
1356 unsigned struct_size)
1358 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1361 size += ttm_round_pot(struct_size);
1362 size += ttm_round_pot(npages * sizeof(void *));
1363 size += ttm_round_pot(sizeof(struct ttm_tt));
1366 EXPORT_SYMBOL(ttm_bo_acc_size);
1368 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1369 unsigned long bo_size,
1370 unsigned struct_size)
1372 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1375 size += ttm_round_pot(struct_size);
1376 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
1377 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1380 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1382 int ttm_bo_create(struct ttm_bo_device *bdev,
1384 enum ttm_bo_type type,
1385 struct ttm_placement *placement,
1386 uint32_t page_alignment,
1388 struct ttm_buffer_object **p_bo)
1390 struct ttm_buffer_object *bo;
1394 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1395 if (unlikely(bo == NULL))
1398 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1399 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1400 interruptible, acc_size,
1402 if (likely(ret == 0))
1407 EXPORT_SYMBOL(ttm_bo_create);
1409 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1412 struct ttm_operation_ctx ctx = {
1413 .interruptible = false,
1414 .no_wait_gpu = false,
1415 .flags = TTM_OPT_FLAG_FORCE_ALLOC
1417 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1418 struct ttm_bo_global *glob = bdev->glob;
1419 struct dma_fence *fence;
1424 * Can't use standard list traversal since we're unlocking.
1427 spin_lock(&glob->lru_lock);
1428 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1429 while (!list_empty(&man->lru[i])) {
1430 spin_unlock(&glob->lru_lock);
1431 ret = ttm_mem_evict_first(bdev, mem_type, NULL, &ctx);
1434 spin_lock(&glob->lru_lock);
1437 spin_unlock(&glob->lru_lock);
1439 spin_lock(&man->move_lock);
1440 fence = dma_fence_get(man->move);
1441 spin_unlock(&man->move_lock);
1444 ret = dma_fence_wait(fence, false);
1445 dma_fence_put(fence);
1453 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1455 struct ttm_mem_type_manager *man;
1458 if (mem_type >= TTM_NUM_MEM_TYPES) {
1459 pr_err("Illegal memory type %d\n", mem_type);
1462 man = &bdev->man[mem_type];
1464 if (!man->has_type) {
1465 pr_err("Trying to take down uninitialized memory manager type %u\n",
1470 man->use_type = false;
1471 man->has_type = false;
1475 ret = ttm_bo_force_list_clean(bdev, mem_type);
1477 pr_err("Cleanup eviction failed\n");
1481 ret = (*man->func->takedown)(man);
1484 dma_fence_put(man->move);
1489 EXPORT_SYMBOL(ttm_bo_clean_mm);
1491 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1493 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1495 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1496 pr_err("Illegal memory manager memory type %u\n", mem_type);
1500 if (!man->has_type) {
1501 pr_err("Memory type %u has not been initialized\n", mem_type);
1505 return ttm_bo_force_list_clean(bdev, mem_type);
1507 EXPORT_SYMBOL(ttm_bo_evict_mm);
1509 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1510 unsigned long p_size)
1513 struct ttm_mem_type_manager *man;
1516 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1517 man = &bdev->man[type];
1518 BUG_ON(man->has_type);
1519 man->io_reserve_fastpath = true;
1520 man->use_io_reserve_lru = false;
1521 mutex_init(&man->io_reserve_mutex);
1522 spin_lock_init(&man->move_lock);
1523 INIT_LIST_HEAD(&man->io_reserve_lru);
1525 ret = bdev->driver->init_mem_type(bdev, type, man);
1530 if (type != TTM_PL_SYSTEM) {
1531 ret = (*man->func->init)(man, p_size);
1535 man->has_type = true;
1536 man->use_type = true;
1539 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1540 INIT_LIST_HEAD(&man->lru[i]);
1545 EXPORT_SYMBOL(ttm_bo_init_mm);
1547 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1549 struct ttm_bo_global *glob =
1550 container_of(kobj, struct ttm_bo_global, kobj);
1552 __free_page(glob->dummy_read_page);
1555 static void ttm_bo_global_release(void)
1557 struct ttm_bo_global *glob = &ttm_bo_glob;
1559 mutex_lock(&ttm_global_mutex);
1560 if (--ttm_bo_glob_use_count > 0)
1563 kobject_del(&glob->kobj);
1564 kobject_put(&glob->kobj);
1565 ttm_mem_global_release(&ttm_mem_glob);
1566 memset(glob, 0, sizeof(*glob));
1568 mutex_unlock(&ttm_global_mutex);
1571 static int ttm_bo_global_init(void)
1573 struct ttm_bo_global *glob = &ttm_bo_glob;
1577 mutex_lock(&ttm_global_mutex);
1578 if (++ttm_bo_glob_use_count > 1)
1581 ret = ttm_mem_global_init(&ttm_mem_glob);
1585 spin_lock_init(&glob->lru_lock);
1586 glob->mem_glob = &ttm_mem_glob;
1587 glob->mem_glob->bo_glob = glob;
1588 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1590 if (unlikely(glob->dummy_read_page == NULL)) {
1595 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1596 INIT_LIST_HEAD(&glob->swap_lru[i]);
1597 INIT_LIST_HEAD(&glob->device_list);
1598 atomic_set(&glob->bo_count, 0);
1600 ret = kobject_init_and_add(
1601 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1602 if (unlikely(ret != 0))
1603 kobject_put(&glob->kobj);
1605 mutex_unlock(&ttm_global_mutex);
1609 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1612 unsigned i = TTM_NUM_MEM_TYPES;
1613 struct ttm_mem_type_manager *man;
1614 struct ttm_bo_global *glob = bdev->glob;
1617 man = &bdev->man[i];
1618 if (man->has_type) {
1619 man->use_type = false;
1620 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1622 pr_err("DRM memory manager type %d is not clean\n",
1625 man->has_type = false;
1629 mutex_lock(&ttm_global_mutex);
1630 list_del(&bdev->device_list);
1631 mutex_unlock(&ttm_global_mutex);
1633 cancel_delayed_work_sync(&bdev->wq);
1635 if (ttm_bo_delayed_delete(bdev, true))
1636 pr_debug("Delayed destroy list was clean\n");
1638 spin_lock(&glob->lru_lock);
1639 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1640 if (list_empty(&bdev->man[0].lru[0]))
1641 pr_debug("Swap list %d was clean\n", i);
1642 spin_unlock(&glob->lru_lock);
1644 drm_vma_offset_manager_destroy(&bdev->vma_manager);
1647 ttm_bo_global_release();
1651 EXPORT_SYMBOL(ttm_bo_device_release);
1653 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1654 struct ttm_bo_driver *driver,
1655 struct address_space *mapping,
1658 struct ttm_bo_global *glob = &ttm_bo_glob;
1661 ret = ttm_bo_global_init();
1665 bdev->driver = driver;
1667 memset(bdev->man, 0, sizeof(bdev->man));
1670 * Initialize the system memory buffer type.
1671 * Other types need to be driver / IOCTL initialized.
1673 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1674 if (unlikely(ret != 0))
1677 drm_vma_offset_manager_init(&bdev->vma_manager,
1678 DRM_FILE_PAGE_OFFSET_START,
1679 DRM_FILE_PAGE_OFFSET_SIZE);
1680 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1681 INIT_LIST_HEAD(&bdev->ddestroy);
1682 bdev->dev_mapping = mapping;
1684 bdev->need_dma32 = need_dma32;
1685 mutex_lock(&ttm_global_mutex);
1686 list_add_tail(&bdev->device_list, &glob->device_list);
1687 mutex_unlock(&ttm_global_mutex);
1691 ttm_bo_global_release();
1694 EXPORT_SYMBOL(ttm_bo_device_init);
1697 * buffer object vm functions.
1700 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1702 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1704 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1705 if (mem->mem_type == TTM_PL_SYSTEM)
1708 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1711 if (mem->placement & TTM_PL_FLAG_CACHED)
1717 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1719 struct ttm_bo_device *bdev = bo->bdev;
1721 drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
1722 ttm_mem_io_free_vm(bo);
1725 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1727 struct ttm_bo_device *bdev = bo->bdev;
1728 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1730 ttm_mem_io_lock(man, false);
1731 ttm_bo_unmap_virtual_locked(bo);
1732 ttm_mem_io_unlock(man);
1736 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1738 int ttm_bo_wait(struct ttm_buffer_object *bo,
1739 bool interruptible, bool no_wait)
1741 long timeout = 15 * HZ;
1744 if (reservation_object_test_signaled_rcu(bo->resv, true))
1750 timeout = reservation_object_wait_timeout_rcu(bo->resv, true,
1751 interruptible, timeout);
1758 reservation_object_add_excl_fence(bo->resv, NULL);
1761 EXPORT_SYMBOL(ttm_bo_wait);
1763 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1768 * Using ttm_bo_reserve makes sure the lru lists are updated.
1771 ret = ttm_bo_reserve(bo, true, no_wait, NULL);
1772 if (unlikely(ret != 0))
1774 ret = ttm_bo_wait(bo, true, no_wait);
1775 if (likely(ret == 0))
1776 atomic_inc(&bo->cpu_writers);
1777 ttm_bo_unreserve(bo);
1780 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1782 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1784 atomic_dec(&bo->cpu_writers);
1786 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1789 * A buffer object shrink method that tries to swap out the first
1790 * buffer object on the bo_global::swap_lru list.
1792 int ttm_bo_swapout(struct ttm_bo_global *glob, struct ttm_operation_ctx *ctx)
1794 struct ttm_buffer_object *bo;
1799 spin_lock(&glob->lru_lock);
1800 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1801 list_for_each_entry(bo, &glob->swap_lru[i], swap) {
1802 if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked)) {
1812 spin_unlock(&glob->lru_lock);
1816 kref_get(&bo->list_kref);
1818 if (!list_empty(&bo->ddestroy)) {
1819 ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1820 kref_put(&bo->list_kref, ttm_bo_release_list);
1824 ttm_bo_del_from_lru(bo);
1825 spin_unlock(&glob->lru_lock);
1828 * Move to system cached
1831 if (bo->mem.mem_type != TTM_PL_SYSTEM ||
1832 bo->ttm->caching_state != tt_cached) {
1833 struct ttm_operation_ctx ctx = { false, false };
1834 struct ttm_mem_reg evict_mem;
1836 evict_mem = bo->mem;
1837 evict_mem.mm_node = NULL;
1838 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1839 evict_mem.mem_type = TTM_PL_SYSTEM;
1841 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx);
1842 if (unlikely(ret != 0))
1847 * Make sure BO is idle.
1850 ret = ttm_bo_wait(bo, false, false);
1851 if (unlikely(ret != 0))
1854 ttm_bo_unmap_virtual(bo);
1857 * Swap out. Buffer will be swapped in again as soon as
1858 * anyone tries to access a ttm page.
1861 if (bo->bdev->driver->swap_notify)
1862 bo->bdev->driver->swap_notify(bo);
1864 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1869 * Unreserve without putting on LRU to avoid swapping out an
1870 * already swapped buffer.
1873 reservation_object_unlock(bo->resv);
1874 kref_put(&bo->list_kref, ttm_bo_release_list);
1877 EXPORT_SYMBOL(ttm_bo_swapout);
1879 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1881 struct ttm_operation_ctx ctx = {
1882 .interruptible = false,
1883 .no_wait_gpu = false
1886 while (ttm_bo_swapout(bdev->glob, &ctx) == 0)
1889 EXPORT_SYMBOL(ttm_bo_swapout_all);
1892 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1895 * @bo: Pointer to buffer
1897 int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1902 * In the absense of a wait_unlocked API,
1903 * Use the bo::wu_mutex to avoid triggering livelocks due to
1904 * concurrent use of this function. Note that this use of
1905 * bo::wu_mutex can go away if we change locking order to
1906 * mmap_sem -> bo::reserve.
1908 ret = mutex_lock_interruptible(&bo->wu_mutex);
1909 if (unlikely(ret != 0))
1910 return -ERESTARTSYS;
1911 if (!ww_mutex_is_locked(&bo->resv->lock))
1913 ret = reservation_object_lock_interruptible(bo->resv, NULL);
1916 if (unlikely(ret != 0))
1918 reservation_object_unlock(bo->resv);
1921 mutex_unlock(&bo->wu_mutex);