1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
4 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
29 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
32 #define pr_fmt(fmt) "[TTM] " fmt
34 #include <drm/ttm/ttm_module.h>
35 #include <drm/ttm/ttm_bo_driver.h>
36 #include <drm/ttm/ttm_placement.h>
37 #include <linux/jiffies.h>
38 #include <linux/slab.h>
39 #include <linux/sched.h>
41 #include <linux/file.h>
42 #include <linux/module.h>
43 #include <linux/atomic.h>
44 #include <linux/dma-resv.h>
46 static void ttm_bo_global_kobj_release(struct kobject
*kobj
);
49 * ttm_global_mutex - protecting the global BO state
51 DEFINE_MUTEX(ttm_global_mutex
);
52 unsigned ttm_bo_glob_use_count
;
53 struct ttm_bo_global ttm_bo_glob
;
54 EXPORT_SYMBOL(ttm_bo_glob
);
56 static struct attribute ttm_bo_count
= {
61 /* default destructor */
62 static void ttm_bo_default_destroy(struct ttm_buffer_object
*bo
)
67 static void ttm_bo_mem_space_debug(struct ttm_buffer_object
*bo
,
68 struct ttm_placement
*placement
)
70 struct drm_printer p
= drm_debug_printer(TTM_PFX
);
71 struct ttm_resource_manager
*man
;
74 drm_printf(&p
, "No space for %p (%lu pages, %luK, %luM)\n",
75 bo
, bo
->mem
.num_pages
, bo
->mem
.size
>> 10,
77 for (i
= 0; i
< placement
->num_placement
; i
++) {
78 mem_type
= placement
->placement
[i
].mem_type
;
79 drm_printf(&p
, " placement[%d]=0x%08X (%d)\n",
80 i
, placement
->placement
[i
].flags
, mem_type
);
81 man
= ttm_manager_type(bo
->bdev
, mem_type
);
82 ttm_resource_manager_debug(man
, &p
);
86 static ssize_t
ttm_bo_global_show(struct kobject
*kobj
,
87 struct attribute
*attr
,
90 struct ttm_bo_global
*glob
=
91 container_of(kobj
, struct ttm_bo_global
, kobj
);
93 return snprintf(buffer
, PAGE_SIZE
, "%d\n",
94 atomic_read(&glob
->bo_count
));
97 static struct attribute
*ttm_bo_global_attrs
[] = {
102 static const struct sysfs_ops ttm_bo_global_ops
= {
103 .show
= &ttm_bo_global_show
106 static struct kobj_type ttm_bo_glob_kobj_type
= {
107 .release
= &ttm_bo_global_kobj_release
,
108 .sysfs_ops
= &ttm_bo_global_ops
,
109 .default_attrs
= ttm_bo_global_attrs
112 static void ttm_bo_add_mem_to_lru(struct ttm_buffer_object
*bo
,
113 struct ttm_resource
*mem
)
115 struct ttm_bo_device
*bdev
= bo
->bdev
;
116 struct ttm_resource_manager
*man
;
118 if (!list_empty(&bo
->lru
) || bo
->pin_count
)
121 man
= ttm_manager_type(bdev
, mem
->mem_type
);
122 list_add_tail(&bo
->lru
, &man
->lru
[bo
->priority
]);
124 if (man
->use_tt
&& bo
->ttm
&&
125 !(bo
->ttm
->page_flags
& (TTM_PAGE_FLAG_SG
|
126 TTM_PAGE_FLAG_SWAPPED
))) {
127 list_add_tail(&bo
->swap
, &ttm_bo_glob
.swap_lru
[bo
->priority
]);
131 static void ttm_bo_del_from_lru(struct ttm_buffer_object
*bo
)
133 struct ttm_bo_device
*bdev
= bo
->bdev
;
136 if (!list_empty(&bo
->swap
)) {
137 list_del_init(&bo
->swap
);
140 if (!list_empty(&bo
->lru
)) {
141 list_del_init(&bo
->lru
);
145 if (notify
&& bdev
->driver
->del_from_lru_notify
)
146 bdev
->driver
->del_from_lru_notify(bo
);
149 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos
*pos
,
150 struct ttm_buffer_object
*bo
)
157 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object
*bo
,
158 struct ttm_lru_bulk_move
*bulk
)
160 dma_resv_assert_held(bo
->base
.resv
);
162 ttm_bo_del_from_lru(bo
);
163 ttm_bo_add_mem_to_lru(bo
, &bo
->mem
);
165 if (bulk
&& !bo
->pin_count
) {
166 switch (bo
->mem
.mem_type
) {
168 ttm_bo_bulk_move_set_pos(&bulk
->tt
[bo
->priority
], bo
);
172 ttm_bo_bulk_move_set_pos(&bulk
->vram
[bo
->priority
], bo
);
175 if (bo
->ttm
&& !(bo
->ttm
->page_flags
&
176 (TTM_PAGE_FLAG_SG
| TTM_PAGE_FLAG_SWAPPED
)))
177 ttm_bo_bulk_move_set_pos(&bulk
->swap
[bo
->priority
], bo
);
180 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail
);
182 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move
*bulk
)
186 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
187 struct ttm_lru_bulk_move_pos
*pos
= &bulk
->tt
[i
];
188 struct ttm_resource_manager
*man
;
193 dma_resv_assert_held(pos
->first
->base
.resv
);
194 dma_resv_assert_held(pos
->last
->base
.resv
);
196 man
= ttm_manager_type(pos
->first
->bdev
, TTM_PL_TT
);
197 list_bulk_move_tail(&man
->lru
[i
], &pos
->first
->lru
,
201 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
202 struct ttm_lru_bulk_move_pos
*pos
= &bulk
->vram
[i
];
203 struct ttm_resource_manager
*man
;
208 dma_resv_assert_held(pos
->first
->base
.resv
);
209 dma_resv_assert_held(pos
->last
->base
.resv
);
211 man
= ttm_manager_type(pos
->first
->bdev
, TTM_PL_VRAM
);
212 list_bulk_move_tail(&man
->lru
[i
], &pos
->first
->lru
,
216 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
217 struct ttm_lru_bulk_move_pos
*pos
= &bulk
->swap
[i
];
218 struct list_head
*lru
;
223 dma_resv_assert_held(pos
->first
->base
.resv
);
224 dma_resv_assert_held(pos
->last
->base
.resv
);
226 lru
= &ttm_bo_glob
.swap_lru
[i
];
227 list_bulk_move_tail(lru
, &pos
->first
->swap
, &pos
->last
->swap
);
230 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail
);
232 static int ttm_bo_handle_move_mem(struct ttm_buffer_object
*bo
,
233 struct ttm_resource
*mem
, bool evict
,
234 struct ttm_operation_ctx
*ctx
,
235 struct ttm_place
*hop
)
237 struct ttm_bo_device
*bdev
= bo
->bdev
;
238 struct ttm_resource_manager
*old_man
= ttm_manager_type(bdev
, bo
->mem
.mem_type
);
239 struct ttm_resource_manager
*new_man
= ttm_manager_type(bdev
, mem
->mem_type
);
242 ttm_bo_unmap_virtual(bo
);
245 * Create and bind a ttm if required.
248 if (new_man
->use_tt
) {
249 /* Zero init the new TTM structure if the old location should
250 * have used one as well.
252 ret
= ttm_tt_create(bo
, old_man
->use_tt
);
256 if (mem
->mem_type
!= TTM_PL_SYSTEM
) {
257 ret
= ttm_tt_populate(bo
->bdev
, bo
->ttm
, ctx
);
263 ret
= bdev
->driver
->move(bo
, evict
, ctx
, mem
, hop
);
265 if (ret
== -EMULTIHOP
)
270 ctx
->bytes_moved
+= bo
->num_pages
<< PAGE_SHIFT
;
274 new_man
= ttm_manager_type(bdev
, bo
->mem
.mem_type
);
275 if (!new_man
->use_tt
)
276 ttm_bo_tt_destroy(bo
);
283 * Will release GPU memory type usage on destruction.
284 * This is the place to put in driver specific hooks to release
285 * driver private resources.
286 * Will release the bo::reserved lock.
289 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object
*bo
)
291 if (bo
->bdev
->driver
->delete_mem_notify
)
292 bo
->bdev
->driver
->delete_mem_notify(bo
);
294 ttm_bo_tt_destroy(bo
);
295 ttm_resource_free(bo
, &bo
->mem
);
298 static int ttm_bo_individualize_resv(struct ttm_buffer_object
*bo
)
302 if (bo
->base
.resv
== &bo
->base
._resv
)
305 BUG_ON(!dma_resv_trylock(&bo
->base
._resv
));
307 r
= dma_resv_copy_fences(&bo
->base
._resv
, bo
->base
.resv
);
308 dma_resv_unlock(&bo
->base
._resv
);
312 if (bo
->type
!= ttm_bo_type_sg
) {
313 /* This works because the BO is about to be destroyed and nobody
314 * reference it any more. The only tricky case is the trylock on
315 * the resv object while holding the lru_lock.
317 spin_lock(&ttm_bo_glob
.lru_lock
);
318 bo
->base
.resv
= &bo
->base
._resv
;
319 spin_unlock(&ttm_bo_glob
.lru_lock
);
325 static void ttm_bo_flush_all_fences(struct ttm_buffer_object
*bo
)
327 struct dma_resv
*resv
= &bo
->base
._resv
;
328 struct dma_resv_list
*fobj
;
329 struct dma_fence
*fence
;
333 fobj
= rcu_dereference(resv
->fence
);
334 fence
= rcu_dereference(resv
->fence_excl
);
335 if (fence
&& !fence
->ops
->signaled
)
336 dma_fence_enable_sw_signaling(fence
);
338 for (i
= 0; fobj
&& i
< fobj
->shared_count
; ++i
) {
339 fence
= rcu_dereference(fobj
->shared
[i
]);
341 if (!fence
->ops
->signaled
)
342 dma_fence_enable_sw_signaling(fence
);
348 * function ttm_bo_cleanup_refs
349 * If bo idle, remove from lru lists, and unref.
350 * If not idle, block if possible.
352 * Must be called with lru_lock and reservation held, this function
353 * will drop the lru lock and optionally the reservation lock before returning.
355 * @bo: The buffer object to clean-up
356 * @interruptible: Any sleeps should occur interruptibly.
357 * @no_wait_gpu: Never wait for gpu. Return -EBUSY instead.
358 * @unlock_resv: Unlock the reservation lock as well.
361 static int ttm_bo_cleanup_refs(struct ttm_buffer_object
*bo
,
362 bool interruptible
, bool no_wait_gpu
,
365 struct dma_resv
*resv
= &bo
->base
._resv
;
368 if (dma_resv_test_signaled_rcu(resv
, true))
373 if (ret
&& !no_wait_gpu
) {
377 dma_resv_unlock(bo
->base
.resv
);
378 spin_unlock(&ttm_bo_glob
.lru_lock
);
380 lret
= dma_resv_wait_timeout_rcu(resv
, true, interruptible
,
388 spin_lock(&ttm_bo_glob
.lru_lock
);
389 if (unlock_resv
&& !dma_resv_trylock(bo
->base
.resv
)) {
391 * We raced, and lost, someone else holds the reservation now,
392 * and is probably busy in ttm_bo_cleanup_memtype_use.
394 * Even if it's not the case, because we finished waiting any
395 * delayed destruction would succeed, so just return success
398 spin_unlock(&ttm_bo_glob
.lru_lock
);
404 if (ret
|| unlikely(list_empty(&bo
->ddestroy
))) {
406 dma_resv_unlock(bo
->base
.resv
);
407 spin_unlock(&ttm_bo_glob
.lru_lock
);
411 ttm_bo_del_from_lru(bo
);
412 list_del_init(&bo
->ddestroy
);
413 spin_unlock(&ttm_bo_glob
.lru_lock
);
414 ttm_bo_cleanup_memtype_use(bo
);
417 dma_resv_unlock(bo
->base
.resv
);
425 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
426 * encountered buffers.
428 static bool ttm_bo_delayed_delete(struct ttm_bo_device
*bdev
, bool remove_all
)
430 struct ttm_bo_global
*glob
= &ttm_bo_glob
;
431 struct list_head removed
;
434 INIT_LIST_HEAD(&removed
);
436 spin_lock(&glob
->lru_lock
);
437 while (!list_empty(&bdev
->ddestroy
)) {
438 struct ttm_buffer_object
*bo
;
440 bo
= list_first_entry(&bdev
->ddestroy
, struct ttm_buffer_object
,
442 list_move_tail(&bo
->ddestroy
, &removed
);
443 if (!ttm_bo_get_unless_zero(bo
))
446 if (remove_all
|| bo
->base
.resv
!= &bo
->base
._resv
) {
447 spin_unlock(&glob
->lru_lock
);
448 dma_resv_lock(bo
->base
.resv
, NULL
);
450 spin_lock(&glob
->lru_lock
);
451 ttm_bo_cleanup_refs(bo
, false, !remove_all
, true);
453 } else if (dma_resv_trylock(bo
->base
.resv
)) {
454 ttm_bo_cleanup_refs(bo
, false, !remove_all
, true);
456 spin_unlock(&glob
->lru_lock
);
460 spin_lock(&glob
->lru_lock
);
462 list_splice_tail(&removed
, &bdev
->ddestroy
);
463 empty
= list_empty(&bdev
->ddestroy
);
464 spin_unlock(&glob
->lru_lock
);
469 static void ttm_bo_delayed_workqueue(struct work_struct
*work
)
471 struct ttm_bo_device
*bdev
=
472 container_of(work
, struct ttm_bo_device
, wq
.work
);
474 if (!ttm_bo_delayed_delete(bdev
, false))
475 schedule_delayed_work(&bdev
->wq
,
476 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
479 static void ttm_bo_release(struct kref
*kref
)
481 struct ttm_buffer_object
*bo
=
482 container_of(kref
, struct ttm_buffer_object
, kref
);
483 struct ttm_bo_device
*bdev
= bo
->bdev
;
484 size_t acc_size
= bo
->acc_size
;
488 ret
= ttm_bo_individualize_resv(bo
);
490 /* Last resort, if we fail to allocate memory for the
491 * fences block for the BO to become idle
493 dma_resv_wait_timeout_rcu(bo
->base
.resv
, true, false,
497 if (bo
->bdev
->driver
->release_notify
)
498 bo
->bdev
->driver
->release_notify(bo
);
500 drm_vma_offset_remove(bdev
->vma_manager
, &bo
->base
.vma_node
);
501 ttm_mem_io_free(bdev
, &bo
->mem
);
504 if (!dma_resv_test_signaled_rcu(bo
->base
.resv
, true) ||
505 !dma_resv_trylock(bo
->base
.resv
)) {
506 /* The BO is not idle, resurrect it for delayed destroy */
507 ttm_bo_flush_all_fences(bo
);
510 spin_lock(&ttm_bo_glob
.lru_lock
);
513 * Make pinned bos immediately available to
514 * shrinkers, now that they are queued for
519 ttm_bo_del_from_lru(bo
);
520 ttm_bo_add_mem_to_lru(bo
, &bo
->mem
);
523 kref_init(&bo
->kref
);
524 list_add_tail(&bo
->ddestroy
, &bdev
->ddestroy
);
525 spin_unlock(&ttm_bo_glob
.lru_lock
);
527 schedule_delayed_work(&bdev
->wq
,
528 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
532 spin_lock(&ttm_bo_glob
.lru_lock
);
533 ttm_bo_del_from_lru(bo
);
534 list_del(&bo
->ddestroy
);
535 spin_unlock(&ttm_bo_glob
.lru_lock
);
537 ttm_bo_cleanup_memtype_use(bo
);
538 dma_resv_unlock(bo
->base
.resv
);
540 atomic_dec(&ttm_bo_glob
.bo_count
);
541 dma_fence_put(bo
->moving
);
542 if (!ttm_bo_uses_embedded_gem_object(bo
))
543 dma_resv_fini(&bo
->base
._resv
);
545 ttm_mem_global_free(&ttm_mem_glob
, acc_size
);
548 void ttm_bo_put(struct ttm_buffer_object
*bo
)
550 kref_put(&bo
->kref
, ttm_bo_release
);
552 EXPORT_SYMBOL(ttm_bo_put
);
554 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device
*bdev
)
556 return cancel_delayed_work_sync(&bdev
->wq
);
558 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue
);
560 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device
*bdev
, int resched
)
563 schedule_delayed_work(&bdev
->wq
,
564 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
566 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue
);
568 static int ttm_bo_evict(struct ttm_buffer_object
*bo
,
569 struct ttm_operation_ctx
*ctx
)
571 struct ttm_bo_device
*bdev
= bo
->bdev
;
572 struct ttm_resource evict_mem
;
573 struct ttm_placement placement
;
574 struct ttm_place hop
;
577 memset(&hop
, 0, sizeof(hop
));
579 dma_resv_assert_held(bo
->base
.resv
);
581 placement
.num_placement
= 0;
582 placement
.num_busy_placement
= 0;
583 bdev
->driver
->evict_flags(bo
, &placement
);
585 if (!placement
.num_placement
&& !placement
.num_busy_placement
) {
586 ttm_bo_wait(bo
, false, false);
588 ttm_bo_cleanup_memtype_use(bo
);
589 return ttm_tt_create(bo
, false);
593 evict_mem
.mm_node
= NULL
;
594 evict_mem
.bus
.offset
= 0;
595 evict_mem
.bus
.addr
= NULL
;
597 ret
= ttm_bo_mem_space(bo
, &placement
, &evict_mem
, ctx
);
599 if (ret
!= -ERESTARTSYS
) {
600 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
602 ttm_bo_mem_space_debug(bo
, &placement
);
607 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true, ctx
, &hop
);
609 WARN(ret
== -EMULTIHOP
, "Unexpected multihop in eviction - likely driver bug\n");
610 if (ret
!= -ERESTARTSYS
)
611 pr_err("Buffer eviction failed\n");
612 ttm_resource_free(bo
, &evict_mem
);
618 bool ttm_bo_eviction_valuable(struct ttm_buffer_object
*bo
,
619 const struct ttm_place
*place
)
621 /* Don't evict this BO if it's outside of the
622 * requested placement range
624 if (place
->fpfn
>= (bo
->mem
.start
+ bo
->mem
.num_pages
) ||
625 (place
->lpfn
&& place
->lpfn
<= bo
->mem
.start
))
630 EXPORT_SYMBOL(ttm_bo_eviction_valuable
);
633 * Check the target bo is allowable to be evicted or swapout, including cases:
635 * a. if share same reservation object with ctx->resv, have assumption
636 * reservation objects should already be locked, so not lock again and
637 * return true directly when either the opreation allow_reserved_eviction
638 * or the target bo already is in delayed free list;
640 * b. Otherwise, trylock it.
642 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object
*bo
,
643 struct ttm_operation_ctx
*ctx
, bool *locked
, bool *busy
)
647 if (bo
->base
.resv
== ctx
->resv
) {
648 dma_resv_assert_held(bo
->base
.resv
);
649 if (ctx
->allow_res_evict
)
655 ret
= dma_resv_trylock(bo
->base
.resv
);
665 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
667 * @busy_bo: BO which couldn't be locked with trylock
668 * @ctx: operation context
669 * @ticket: acquire ticket
671 * Try to lock a busy buffer object to avoid failing eviction.
673 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object
*busy_bo
,
674 struct ttm_operation_ctx
*ctx
,
675 struct ww_acquire_ctx
*ticket
)
679 if (!busy_bo
|| !ticket
)
682 if (ctx
->interruptible
)
683 r
= dma_resv_lock_interruptible(busy_bo
->base
.resv
,
686 r
= dma_resv_lock(busy_bo
->base
.resv
, ticket
);
689 * TODO: It would be better to keep the BO locked until allocation is at
690 * least tried one more time, but that would mean a much larger rework
694 dma_resv_unlock(busy_bo
->base
.resv
);
696 return r
== -EDEADLK
? -EBUSY
: r
;
699 int ttm_mem_evict_first(struct ttm_bo_device
*bdev
,
700 struct ttm_resource_manager
*man
,
701 const struct ttm_place
*place
,
702 struct ttm_operation_ctx
*ctx
,
703 struct ww_acquire_ctx
*ticket
)
705 struct ttm_buffer_object
*bo
= NULL
, *busy_bo
= NULL
;
710 spin_lock(&ttm_bo_glob
.lru_lock
);
711 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
712 list_for_each_entry(bo
, &man
->lru
[i
], lru
) {
715 if (!ttm_bo_evict_swapout_allowable(bo
, ctx
, &locked
,
717 if (busy
&& !busy_bo
&& ticket
!=
718 dma_resv_locking_ctx(bo
->base
.resv
))
723 if (place
&& !bdev
->driver
->eviction_valuable(bo
,
726 dma_resv_unlock(bo
->base
.resv
);
729 if (!ttm_bo_get_unless_zero(bo
)) {
731 dma_resv_unlock(bo
->base
.resv
);
737 /* If the inner loop terminated early, we have our candidate */
738 if (&bo
->lru
!= &man
->lru
[i
])
745 if (busy_bo
&& !ttm_bo_get_unless_zero(busy_bo
))
747 spin_unlock(&ttm_bo_glob
.lru_lock
);
748 ret
= ttm_mem_evict_wait_busy(busy_bo
, ctx
, ticket
);
755 ret
= ttm_bo_cleanup_refs(bo
, ctx
->interruptible
,
756 ctx
->no_wait_gpu
, locked
);
761 spin_unlock(&ttm_bo_glob
.lru_lock
);
763 ret
= ttm_bo_evict(bo
, ctx
);
765 ttm_bo_unreserve(bo
);
772 * Add the last move fence to the BO and reserve a new shared slot.
774 static int ttm_bo_add_move_fence(struct ttm_buffer_object
*bo
,
775 struct ttm_resource_manager
*man
,
776 struct ttm_resource
*mem
,
779 struct dma_fence
*fence
;
782 spin_lock(&man
->move_lock
);
783 fence
= dma_fence_get(man
->move
);
784 spin_unlock(&man
->move_lock
);
790 dma_fence_put(fence
);
794 dma_resv_add_shared_fence(bo
->base
.resv
, fence
);
796 ret
= dma_resv_reserve_shared(bo
->base
.resv
, 1);
798 dma_fence_put(fence
);
802 dma_fence_put(bo
->moving
);
808 * Repeatedly evict memory from the LRU for @mem_type until we create enough
809 * space, or we've evicted everything and there isn't enough space.
811 static int ttm_bo_mem_force_space(struct ttm_buffer_object
*bo
,
812 const struct ttm_place
*place
,
813 struct ttm_resource
*mem
,
814 struct ttm_operation_ctx
*ctx
)
816 struct ttm_bo_device
*bdev
= bo
->bdev
;
817 struct ttm_resource_manager
*man
= ttm_manager_type(bdev
, mem
->mem_type
);
818 struct ww_acquire_ctx
*ticket
;
821 ticket
= dma_resv_locking_ctx(bo
->base
.resv
);
823 ret
= ttm_resource_alloc(bo
, place
, mem
);
826 if (unlikely(ret
!= -ENOSPC
))
828 ret
= ttm_mem_evict_first(bdev
, man
, place
, ctx
,
830 if (unlikely(ret
!= 0))
834 return ttm_bo_add_move_fence(bo
, man
, mem
, ctx
->no_wait_gpu
);
838 * ttm_bo_mem_placement - check if placement is compatible
839 * @bo: BO to find memory for
840 * @place: where to search
841 * @mem: the memory object to fill in
843 * Check if placement is compatible and fill in mem structure.
844 * Returns -EBUSY if placement won't work or negative error code.
845 * 0 when placement can be used.
847 static int ttm_bo_mem_placement(struct ttm_buffer_object
*bo
,
848 const struct ttm_place
*place
,
849 struct ttm_resource
*mem
)
851 struct ttm_bo_device
*bdev
= bo
->bdev
;
852 struct ttm_resource_manager
*man
;
854 man
= ttm_manager_type(bdev
, place
->mem_type
);
855 if (!man
|| !ttm_resource_manager_used(man
))
858 mem
->mem_type
= place
->mem_type
;
859 mem
->placement
= place
->flags
;
861 spin_lock(&ttm_bo_glob
.lru_lock
);
862 ttm_bo_del_from_lru(bo
);
863 ttm_bo_add_mem_to_lru(bo
, mem
);
864 spin_unlock(&ttm_bo_glob
.lru_lock
);
870 * Creates space for memory region @mem according to its type.
872 * This function first searches for free space in compatible memory types in
873 * the priority order defined by the driver. If free space isn't found, then
874 * ttm_bo_mem_force_space is attempted in priority order to evict and find
877 int ttm_bo_mem_space(struct ttm_buffer_object
*bo
,
878 struct ttm_placement
*placement
,
879 struct ttm_resource
*mem
,
880 struct ttm_operation_ctx
*ctx
)
882 struct ttm_bo_device
*bdev
= bo
->bdev
;
883 bool type_found
= false;
886 ret
= dma_resv_reserve_shared(bo
->base
.resv
, 1);
890 for (i
= 0; i
< placement
->num_placement
; ++i
) {
891 const struct ttm_place
*place
= &placement
->placement
[i
];
892 struct ttm_resource_manager
*man
;
894 ret
= ttm_bo_mem_placement(bo
, place
, mem
);
899 ret
= ttm_resource_alloc(bo
, place
, mem
);
905 man
= ttm_manager_type(bdev
, mem
->mem_type
);
906 ret
= ttm_bo_add_move_fence(bo
, man
, mem
, ctx
->no_wait_gpu
);
908 ttm_resource_free(bo
, mem
);
917 for (i
= 0; i
< placement
->num_busy_placement
; ++i
) {
918 const struct ttm_place
*place
= &placement
->busy_placement
[i
];
920 ret
= ttm_bo_mem_placement(bo
, place
, mem
);
925 ret
= ttm_bo_mem_force_space(bo
, place
, mem
, ctx
);
929 if (ret
&& ret
!= -EBUSY
)
935 pr_err(TTM_PFX
"No compatible memory type found\n");
940 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
&& !list_empty(&bo
->lru
)) {
941 ttm_bo_move_to_lru_tail_unlocked(bo
);
946 EXPORT_SYMBOL(ttm_bo_mem_space
);
948 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object
*bo
,
949 struct ttm_resource
*mem
,
950 struct ttm_operation_ctx
*ctx
,
951 struct ttm_place
*hop
)
953 struct ttm_placement hop_placement
;
955 struct ttm_resource hop_mem
= *mem
;
957 hop_mem
.mm_node
= NULL
;
958 hop_mem
.mem_type
= TTM_PL_SYSTEM
;
959 hop_mem
.placement
= 0;
961 hop_placement
.num_placement
= hop_placement
.num_busy_placement
= 1;
962 hop_placement
.placement
= hop_placement
.busy_placement
= hop
;
964 /* find space in the bounce domain */
965 ret
= ttm_bo_mem_space(bo
, &hop_placement
, &hop_mem
, ctx
);
968 /* move to the bounce domain */
969 ret
= ttm_bo_handle_move_mem(bo
, &hop_mem
, false, ctx
, NULL
);
975 static int ttm_bo_move_buffer(struct ttm_buffer_object
*bo
,
976 struct ttm_placement
*placement
,
977 struct ttm_operation_ctx
*ctx
)
980 struct ttm_place hop
;
981 struct ttm_resource mem
;
983 dma_resv_assert_held(bo
->base
.resv
);
985 memset(&hop
, 0, sizeof(hop
));
987 mem
.num_pages
= bo
->num_pages
;
988 mem
.size
= mem
.num_pages
<< PAGE_SHIFT
;
989 mem
.page_alignment
= bo
->mem
.page_alignment
;
995 * Determine where to move the buffer.
997 * If driver determines move is going to need
998 * an extra step then it will return -EMULTIHOP
999 * and the buffer will be moved to the temporary
1000 * stop and the driver will be called to make
1004 ret
= ttm_bo_mem_space(bo
, placement
, &mem
, ctx
);
1007 ret
= ttm_bo_handle_move_mem(bo
, &mem
, false, ctx
, &hop
);
1008 if (ret
== -EMULTIHOP
) {
1009 ret
= ttm_bo_bounce_temp_buffer(bo
, &mem
, ctx
, &hop
);
1012 /* try and move to final place now. */
1016 ttm_resource_free(bo
, &mem
);
1020 static bool ttm_bo_places_compat(const struct ttm_place
*places
,
1021 unsigned num_placement
,
1022 struct ttm_resource
*mem
,
1023 uint32_t *new_flags
)
1027 for (i
= 0; i
< num_placement
; i
++) {
1028 const struct ttm_place
*heap
= &places
[i
];
1030 if ((mem
->start
< heap
->fpfn
||
1031 (heap
->lpfn
!= 0 && (mem
->start
+ mem
->num_pages
) > heap
->lpfn
)))
1034 *new_flags
= heap
->flags
;
1035 if ((mem
->mem_type
== heap
->mem_type
) &&
1036 (!(*new_flags
& TTM_PL_FLAG_CONTIGUOUS
) ||
1037 (mem
->placement
& TTM_PL_FLAG_CONTIGUOUS
)))
1043 bool ttm_bo_mem_compat(struct ttm_placement
*placement
,
1044 struct ttm_resource
*mem
,
1045 uint32_t *new_flags
)
1047 if (ttm_bo_places_compat(placement
->placement
, placement
->num_placement
,
1051 if ((placement
->busy_placement
!= placement
->placement
||
1052 placement
->num_busy_placement
> placement
->num_placement
) &&
1053 ttm_bo_places_compat(placement
->busy_placement
,
1054 placement
->num_busy_placement
,
1060 EXPORT_SYMBOL(ttm_bo_mem_compat
);
1062 int ttm_bo_validate(struct ttm_buffer_object
*bo
,
1063 struct ttm_placement
*placement
,
1064 struct ttm_operation_ctx
*ctx
)
1069 dma_resv_assert_held(bo
->base
.resv
);
1072 * Remove the backing store if no placement is given.
1074 if (!placement
->num_placement
&& !placement
->num_busy_placement
) {
1075 ret
= ttm_bo_pipeline_gutting(bo
);
1079 return ttm_tt_create(bo
, false);
1083 * Check whether we need to move buffer.
1085 if (!ttm_bo_mem_compat(placement
, &bo
->mem
, &new_flags
)) {
1086 ret
= ttm_bo_move_buffer(bo
, placement
, ctx
);
1091 * We might need to add a TTM.
1093 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
) {
1094 ret
= ttm_tt_create(bo
, true);
1100 EXPORT_SYMBOL(ttm_bo_validate
);
1102 int ttm_bo_init_reserved(struct ttm_bo_device
*bdev
,
1103 struct ttm_buffer_object
*bo
,
1105 enum ttm_bo_type type
,
1106 struct ttm_placement
*placement
,
1107 uint32_t page_alignment
,
1108 struct ttm_operation_ctx
*ctx
,
1110 struct sg_table
*sg
,
1111 struct dma_resv
*resv
,
1112 void (*destroy
) (struct ttm_buffer_object
*))
1114 struct ttm_mem_global
*mem_glob
= &ttm_mem_glob
;
1116 unsigned long num_pages
;
1119 ret
= ttm_mem_global_alloc(mem_glob
, acc_size
, ctx
);
1121 pr_err("Out of kernel memory\n");
1129 num_pages
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1130 if (num_pages
== 0) {
1131 pr_err("Illegal buffer object size\n");
1136 ttm_mem_global_free(mem_glob
, acc_size
);
1139 bo
->destroy
= destroy
? destroy
: ttm_bo_default_destroy
;
1141 kref_init(&bo
->kref
);
1142 INIT_LIST_HEAD(&bo
->lru
);
1143 INIT_LIST_HEAD(&bo
->ddestroy
);
1144 INIT_LIST_HEAD(&bo
->swap
);
1147 bo
->num_pages
= num_pages
;
1148 bo
->mem
.size
= num_pages
<< PAGE_SHIFT
;
1149 bo
->mem
.mem_type
= TTM_PL_SYSTEM
;
1150 bo
->mem
.num_pages
= bo
->num_pages
;
1151 bo
->mem
.mm_node
= NULL
;
1152 bo
->mem
.page_alignment
= page_alignment
;
1153 bo
->mem
.bus
.offset
= 0;
1154 bo
->mem
.bus
.addr
= NULL
;
1156 bo
->mem
.placement
= 0;
1157 bo
->acc_size
= acc_size
;
1161 bo
->base
.resv
= resv
;
1162 dma_resv_assert_held(bo
->base
.resv
);
1164 bo
->base
.resv
= &bo
->base
._resv
;
1166 if (!ttm_bo_uses_embedded_gem_object(bo
)) {
1168 * bo.gem is not initialized, so we have to setup the
1169 * struct elements we want use regardless.
1171 dma_resv_init(&bo
->base
._resv
);
1172 drm_vma_node_reset(&bo
->base
.vma_node
);
1174 atomic_inc(&ttm_bo_glob
.bo_count
);
1177 * For ttm_bo_type_device buffers, allocate
1178 * address space from the device.
1180 if (bo
->type
== ttm_bo_type_device
||
1181 bo
->type
== ttm_bo_type_sg
)
1182 ret
= drm_vma_offset_add(bdev
->vma_manager
, &bo
->base
.vma_node
,
1185 /* passed reservation objects should already be locked,
1186 * since otherwise lockdep will be angered in radeon.
1189 locked
= dma_resv_trylock(bo
->base
.resv
);
1194 ret
= ttm_bo_validate(bo
, placement
, ctx
);
1196 if (unlikely(ret
)) {
1198 ttm_bo_unreserve(bo
);
1204 ttm_bo_move_to_lru_tail_unlocked(bo
);
1208 EXPORT_SYMBOL(ttm_bo_init_reserved
);
1210 int ttm_bo_init(struct ttm_bo_device
*bdev
,
1211 struct ttm_buffer_object
*bo
,
1213 enum ttm_bo_type type
,
1214 struct ttm_placement
*placement
,
1215 uint32_t page_alignment
,
1218 struct sg_table
*sg
,
1219 struct dma_resv
*resv
,
1220 void (*destroy
) (struct ttm_buffer_object
*))
1222 struct ttm_operation_ctx ctx
= { interruptible
, false };
1225 ret
= ttm_bo_init_reserved(bdev
, bo
, size
, type
, placement
,
1226 page_alignment
, &ctx
, acc_size
,
1232 ttm_bo_unreserve(bo
);
1236 EXPORT_SYMBOL(ttm_bo_init
);
1238 size_t ttm_bo_dma_acc_size(struct ttm_bo_device
*bdev
,
1239 unsigned long bo_size
,
1240 unsigned struct_size
)
1242 unsigned npages
= (PAGE_ALIGN(bo_size
)) >> PAGE_SHIFT
;
1245 size
+= ttm_round_pot(struct_size
);
1246 size
+= ttm_round_pot(npages
* (2*sizeof(void *) + sizeof(dma_addr_t
)));
1247 size
+= ttm_round_pot(sizeof(struct ttm_tt
));
1250 EXPORT_SYMBOL(ttm_bo_dma_acc_size
);
1252 static void ttm_bo_global_kobj_release(struct kobject
*kobj
)
1254 struct ttm_bo_global
*glob
=
1255 container_of(kobj
, struct ttm_bo_global
, kobj
);
1257 __free_page(glob
->dummy_read_page
);
1260 static void ttm_bo_global_release(void)
1262 struct ttm_bo_global
*glob
= &ttm_bo_glob
;
1264 mutex_lock(&ttm_global_mutex
);
1265 if (--ttm_bo_glob_use_count
> 0)
1268 kobject_del(&glob
->kobj
);
1269 kobject_put(&glob
->kobj
);
1270 ttm_mem_global_release(&ttm_mem_glob
);
1271 memset(glob
, 0, sizeof(*glob
));
1273 mutex_unlock(&ttm_global_mutex
);
1276 static int ttm_bo_global_init(void)
1278 struct ttm_bo_global
*glob
= &ttm_bo_glob
;
1282 mutex_lock(&ttm_global_mutex
);
1283 if (++ttm_bo_glob_use_count
> 1)
1286 ret
= ttm_mem_global_init(&ttm_mem_glob
);
1290 spin_lock_init(&glob
->lru_lock
);
1291 glob
->dummy_read_page
= alloc_page(__GFP_ZERO
| GFP_DMA32
);
1293 if (unlikely(glob
->dummy_read_page
== NULL
)) {
1298 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
)
1299 INIT_LIST_HEAD(&glob
->swap_lru
[i
]);
1300 INIT_LIST_HEAD(&glob
->device_list
);
1301 atomic_set(&glob
->bo_count
, 0);
1303 ret
= kobject_init_and_add(
1304 &glob
->kobj
, &ttm_bo_glob_kobj_type
, ttm_get_kobj(), "buffer_objects");
1305 if (unlikely(ret
!= 0))
1306 kobject_put(&glob
->kobj
);
1308 mutex_unlock(&ttm_global_mutex
);
1312 int ttm_bo_device_release(struct ttm_bo_device
*bdev
)
1314 struct ttm_bo_global
*glob
= &ttm_bo_glob
;
1317 struct ttm_resource_manager
*man
;
1319 man
= ttm_manager_type(bdev
, TTM_PL_SYSTEM
);
1320 ttm_resource_manager_set_used(man
, false);
1321 ttm_set_driver_manager(bdev
, TTM_PL_SYSTEM
, NULL
);
1323 mutex_lock(&ttm_global_mutex
);
1324 list_del(&bdev
->device_list
);
1325 mutex_unlock(&ttm_global_mutex
);
1327 cancel_delayed_work_sync(&bdev
->wq
);
1329 if (ttm_bo_delayed_delete(bdev
, true))
1330 pr_debug("Delayed destroy list was clean\n");
1332 spin_lock(&glob
->lru_lock
);
1333 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
)
1334 if (list_empty(&man
->lru
[0]))
1335 pr_debug("Swap list %d was clean\n", i
);
1336 spin_unlock(&glob
->lru_lock
);
1338 ttm_pool_fini(&bdev
->pool
);
1341 ttm_bo_global_release();
1345 EXPORT_SYMBOL(ttm_bo_device_release
);
1347 static void ttm_bo_init_sysman(struct ttm_bo_device
*bdev
)
1349 struct ttm_resource_manager
*man
= &bdev
->sysman
;
1352 * Initialize the system memory buffer type.
1353 * Other types need to be driver / IOCTL initialized.
1357 ttm_resource_manager_init(man
, 0);
1358 ttm_set_driver_manager(bdev
, TTM_PL_SYSTEM
, man
);
1359 ttm_resource_manager_set_used(man
, true);
1362 int ttm_bo_device_init(struct ttm_bo_device
*bdev
,
1363 struct ttm_bo_driver
*driver
,
1365 struct address_space
*mapping
,
1366 struct drm_vma_offset_manager
*vma_manager
,
1367 bool use_dma_alloc
, bool use_dma32
)
1369 struct ttm_bo_global
*glob
= &ttm_bo_glob
;
1372 if (WARN_ON(vma_manager
== NULL
))
1375 ret
= ttm_bo_global_init();
1379 bdev
->driver
= driver
;
1381 ttm_bo_init_sysman(bdev
);
1382 ttm_pool_init(&bdev
->pool
, dev
, use_dma_alloc
, use_dma32
);
1384 bdev
->vma_manager
= vma_manager
;
1385 INIT_DELAYED_WORK(&bdev
->wq
, ttm_bo_delayed_workqueue
);
1386 INIT_LIST_HEAD(&bdev
->ddestroy
);
1387 bdev
->dev_mapping
= mapping
;
1388 mutex_lock(&ttm_global_mutex
);
1389 list_add_tail(&bdev
->device_list
, &glob
->device_list
);
1390 mutex_unlock(&ttm_global_mutex
);
1394 EXPORT_SYMBOL(ttm_bo_device_init
);
1397 * buffer object vm functions.
1400 void ttm_bo_unmap_virtual(struct ttm_buffer_object
*bo
)
1402 struct ttm_bo_device
*bdev
= bo
->bdev
;
1404 drm_vma_node_unmap(&bo
->base
.vma_node
, bdev
->dev_mapping
);
1405 ttm_mem_io_free(bdev
, &bo
->mem
);
1407 EXPORT_SYMBOL(ttm_bo_unmap_virtual
);
1409 int ttm_bo_wait(struct ttm_buffer_object
*bo
,
1410 bool interruptible
, bool no_wait
)
1412 long timeout
= 15 * HZ
;
1415 if (dma_resv_test_signaled_rcu(bo
->base
.resv
, true))
1421 timeout
= dma_resv_wait_timeout_rcu(bo
->base
.resv
, true,
1422 interruptible
, timeout
);
1429 dma_resv_add_excl_fence(bo
->base
.resv
, NULL
);
1432 EXPORT_SYMBOL(ttm_bo_wait
);
1435 * A buffer object shrink method that tries to swap out the first
1436 * buffer object on the bo_global::swap_lru list.
1438 int ttm_bo_swapout(struct ttm_operation_ctx
*ctx
)
1440 struct ttm_bo_global
*glob
= &ttm_bo_glob
;
1441 struct ttm_buffer_object
*bo
;
1446 spin_lock(&glob
->lru_lock
);
1447 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
1448 list_for_each_entry(bo
, &glob
->swap_lru
[i
], swap
) {
1449 if (!ttm_bo_evict_swapout_allowable(bo
, ctx
, &locked
,
1453 if (!ttm_bo_get_unless_zero(bo
)) {
1455 dma_resv_unlock(bo
->base
.resv
);
1467 spin_unlock(&glob
->lru_lock
);
1472 ret
= ttm_bo_cleanup_refs(bo
, false, false, locked
);
1477 ttm_bo_del_from_lru(bo
);
1478 spin_unlock(&glob
->lru_lock
);
1481 * Move to system cached
1484 if (bo
->mem
.mem_type
!= TTM_PL_SYSTEM
) {
1485 struct ttm_operation_ctx ctx
= { false, false };
1486 struct ttm_resource evict_mem
;
1487 struct ttm_place hop
;
1489 memset(&hop
, 0, sizeof(hop
));
1491 evict_mem
= bo
->mem
;
1492 evict_mem
.mm_node
= NULL
;
1493 evict_mem
.placement
= 0;
1494 evict_mem
.mem_type
= TTM_PL_SYSTEM
;
1496 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true, &ctx
, &hop
);
1497 if (unlikely(ret
!= 0)) {
1498 WARN(ret
== -EMULTIHOP
, "Unexpected multihop in swaput - likely driver bug.\n");
1504 * Make sure BO is idle.
1507 ret
= ttm_bo_wait(bo
, false, false);
1508 if (unlikely(ret
!= 0))
1511 ttm_bo_unmap_virtual(bo
);
1514 * Swap out. Buffer will be swapped in again as soon as
1515 * anyone tries to access a ttm page.
1518 if (bo
->bdev
->driver
->swap_notify
)
1519 bo
->bdev
->driver
->swap_notify(bo
);
1521 ret
= ttm_tt_swapout(bo
->bdev
, bo
->ttm
);
1526 * Unreserve without putting on LRU to avoid swapping out an
1527 * already swapped buffer.
1530 dma_resv_unlock(bo
->base
.resv
);
1534 EXPORT_SYMBOL(ttm_bo_swapout
);
1536 void ttm_bo_tt_destroy(struct ttm_buffer_object
*bo
)
1538 if (bo
->ttm
== NULL
)
1541 ttm_tt_destroy(bo
->bdev
, bo
->ttm
);