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 struct ttm_bo_global 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 inline int ttm_mem_type_from_place(const struct ttm_place
*place
,
72 pos
= ffs(place
->flags
& TTM_PL_MASK_MEM
);
80 static void ttm_mem_type_debug(struct ttm_bo_device
*bdev
, struct drm_printer
*p
,
83 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
85 drm_printf(p
, " has_type: %d\n", man
->has_type
);
86 drm_printf(p
, " use_type: %d\n", man
->use_type
);
87 drm_printf(p
, " flags: 0x%08X\n", man
->flags
);
88 drm_printf(p
, " gpu_offset: 0x%08llX\n", man
->gpu_offset
);
89 drm_printf(p
, " size: %llu\n", man
->size
);
90 drm_printf(p
, " available_caching: 0x%08X\n", man
->available_caching
);
91 drm_printf(p
, " default_caching: 0x%08X\n", man
->default_caching
);
92 if (mem_type
!= TTM_PL_SYSTEM
)
93 (*man
->func
->debug
)(man
, p
);
96 static void ttm_bo_mem_space_debug(struct ttm_buffer_object
*bo
,
97 struct ttm_placement
*placement
)
99 struct drm_printer p
= drm_debug_printer(TTM_PFX
);
100 int i
, ret
, mem_type
;
102 drm_printf(&p
, "No space for %p (%lu pages, %luK, %luM)\n",
103 bo
, bo
->mem
.num_pages
, bo
->mem
.size
>> 10,
105 for (i
= 0; i
< placement
->num_placement
; i
++) {
106 ret
= ttm_mem_type_from_place(&placement
->placement
[i
],
110 drm_printf(&p
, " placement[%d]=0x%08X (%d)\n",
111 i
, placement
->placement
[i
].flags
, mem_type
);
112 ttm_mem_type_debug(bo
->bdev
, &p
, mem_type
);
116 static ssize_t
ttm_bo_global_show(struct kobject
*kobj
,
117 struct attribute
*attr
,
120 struct ttm_bo_global
*glob
=
121 container_of(kobj
, struct ttm_bo_global
, kobj
);
123 return snprintf(buffer
, PAGE_SIZE
, "%d\n",
124 atomic_read(&glob
->bo_count
));
127 static struct attribute
*ttm_bo_global_attrs
[] = {
132 static const struct sysfs_ops ttm_bo_global_ops
= {
133 .show
= &ttm_bo_global_show
136 static struct kobj_type ttm_bo_glob_kobj_type
= {
137 .release
= &ttm_bo_global_kobj_release
,
138 .sysfs_ops
= &ttm_bo_global_ops
,
139 .default_attrs
= ttm_bo_global_attrs
143 static inline uint32_t ttm_bo_type_flags(unsigned type
)
148 static void ttm_bo_release_list(struct kref
*list_kref
)
150 struct ttm_buffer_object
*bo
=
151 container_of(list_kref
, struct ttm_buffer_object
, list_kref
);
152 struct ttm_bo_device
*bdev
= bo
->bdev
;
153 size_t acc_size
= bo
->acc_size
;
155 BUG_ON(kref_read(&bo
->list_kref
));
156 BUG_ON(kref_read(&bo
->kref
));
157 BUG_ON(atomic_read(&bo
->cpu_writers
));
158 BUG_ON(bo
->mem
.mm_node
!= NULL
);
159 BUG_ON(!list_empty(&bo
->lru
));
160 BUG_ON(!list_empty(&bo
->ddestroy
));
161 ttm_tt_destroy(bo
->ttm
);
162 atomic_dec(&bo
->bdev
->glob
->bo_count
);
163 dma_fence_put(bo
->moving
);
164 reservation_object_fini(&bo
->ttm_resv
);
165 mutex_destroy(&bo
->wu_mutex
);
167 ttm_mem_global_free(bdev
->glob
->mem_glob
, acc_size
);
170 void ttm_bo_add_to_lru(struct ttm_buffer_object
*bo
)
172 struct ttm_bo_device
*bdev
= bo
->bdev
;
173 struct ttm_mem_type_manager
*man
;
175 reservation_object_assert_held(bo
->resv
);
177 if (!(bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
)) {
178 BUG_ON(!list_empty(&bo
->lru
));
180 man
= &bdev
->man
[bo
->mem
.mem_type
];
181 list_add_tail(&bo
->lru
, &man
->lru
[bo
->priority
]);
182 kref_get(&bo
->list_kref
);
184 if (bo
->ttm
&& !(bo
->ttm
->page_flags
&
185 (TTM_PAGE_FLAG_SG
| TTM_PAGE_FLAG_SWAPPED
))) {
186 list_add_tail(&bo
->swap
,
187 &bdev
->glob
->swap_lru
[bo
->priority
]);
188 kref_get(&bo
->list_kref
);
192 EXPORT_SYMBOL(ttm_bo_add_to_lru
);
194 static void ttm_bo_ref_bug(struct kref
*list_kref
)
199 void ttm_bo_del_from_lru(struct ttm_buffer_object
*bo
)
201 struct ttm_bo_device
*bdev
= bo
->bdev
;
204 if (!list_empty(&bo
->swap
)) {
205 list_del_init(&bo
->swap
);
206 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
209 if (!list_empty(&bo
->lru
)) {
210 list_del_init(&bo
->lru
);
211 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
215 if (notify
&& bdev
->driver
->del_from_lru_notify
)
216 bdev
->driver
->del_from_lru_notify(bo
);
219 void ttm_bo_del_sub_from_lru(struct ttm_buffer_object
*bo
)
221 struct ttm_bo_global
*glob
= bo
->bdev
->glob
;
223 spin_lock(&glob
->lru_lock
);
224 ttm_bo_del_from_lru(bo
);
225 spin_unlock(&glob
->lru_lock
);
227 EXPORT_SYMBOL(ttm_bo_del_sub_from_lru
);
229 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos
*pos
,
230 struct ttm_buffer_object
*bo
)
237 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object
*bo
,
238 struct ttm_lru_bulk_move
*bulk
)
240 reservation_object_assert_held(bo
->resv
);
242 ttm_bo_del_from_lru(bo
);
243 ttm_bo_add_to_lru(bo
);
245 if (bulk
&& !(bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
)) {
246 switch (bo
->mem
.mem_type
) {
248 ttm_bo_bulk_move_set_pos(&bulk
->tt
[bo
->priority
], bo
);
252 ttm_bo_bulk_move_set_pos(&bulk
->vram
[bo
->priority
], bo
);
255 if (bo
->ttm
&& !(bo
->ttm
->page_flags
&
256 (TTM_PAGE_FLAG_SG
| TTM_PAGE_FLAG_SWAPPED
)))
257 ttm_bo_bulk_move_set_pos(&bulk
->swap
[bo
->priority
], bo
);
260 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail
);
262 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move
*bulk
)
266 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
267 struct ttm_lru_bulk_move_pos
*pos
= &bulk
->tt
[i
];
268 struct ttm_mem_type_manager
*man
;
273 reservation_object_assert_held(pos
->first
->resv
);
274 reservation_object_assert_held(pos
->last
->resv
);
276 man
= &pos
->first
->bdev
->man
[TTM_PL_TT
];
277 list_bulk_move_tail(&man
->lru
[i
], &pos
->first
->lru
,
281 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
282 struct ttm_lru_bulk_move_pos
*pos
= &bulk
->vram
[i
];
283 struct ttm_mem_type_manager
*man
;
288 reservation_object_assert_held(pos
->first
->resv
);
289 reservation_object_assert_held(pos
->last
->resv
);
291 man
= &pos
->first
->bdev
->man
[TTM_PL_VRAM
];
292 list_bulk_move_tail(&man
->lru
[i
], &pos
->first
->lru
,
296 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
297 struct ttm_lru_bulk_move_pos
*pos
= &bulk
->swap
[i
];
298 struct list_head
*lru
;
303 reservation_object_assert_held(pos
->first
->resv
);
304 reservation_object_assert_held(pos
->last
->resv
);
306 lru
= &pos
->first
->bdev
->glob
->swap_lru
[i
];
307 list_bulk_move_tail(lru
, &pos
->first
->swap
, &pos
->last
->swap
);
310 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail
);
312 static int ttm_bo_handle_move_mem(struct ttm_buffer_object
*bo
,
313 struct ttm_mem_reg
*mem
, bool evict
,
314 struct ttm_operation_ctx
*ctx
)
316 struct ttm_bo_device
*bdev
= bo
->bdev
;
317 bool old_is_pci
= ttm_mem_reg_is_pci(bdev
, &bo
->mem
);
318 bool new_is_pci
= ttm_mem_reg_is_pci(bdev
, mem
);
319 struct ttm_mem_type_manager
*old_man
= &bdev
->man
[bo
->mem
.mem_type
];
320 struct ttm_mem_type_manager
*new_man
= &bdev
->man
[mem
->mem_type
];
323 if (old_is_pci
|| new_is_pci
||
324 ((mem
->placement
& bo
->mem
.placement
& TTM_PL_MASK_CACHING
) == 0)) {
325 ret
= ttm_mem_io_lock(old_man
, true);
326 if (unlikely(ret
!= 0))
328 ttm_bo_unmap_virtual_locked(bo
);
329 ttm_mem_io_unlock(old_man
);
333 * Create and bind a ttm if required.
336 if (!(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
)) {
337 if (bo
->ttm
== NULL
) {
338 bool zero
= !(old_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
);
339 ret
= ttm_tt_create(bo
, zero
);
344 ret
= ttm_tt_set_placement_caching(bo
->ttm
, mem
->placement
);
348 if (mem
->mem_type
!= TTM_PL_SYSTEM
) {
349 ret
= ttm_tt_bind(bo
->ttm
, mem
, ctx
);
354 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
) {
355 if (bdev
->driver
->move_notify
)
356 bdev
->driver
->move_notify(bo
, evict
, mem
);
363 if (bdev
->driver
->move_notify
)
364 bdev
->driver
->move_notify(bo
, evict
, mem
);
366 if (!(old_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) &&
367 !(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
))
368 ret
= ttm_bo_move_ttm(bo
, ctx
, mem
);
369 else if (bdev
->driver
->move
)
370 ret
= bdev
->driver
->move(bo
, evict
, ctx
, mem
);
372 ret
= ttm_bo_move_memcpy(bo
, ctx
, mem
);
375 if (bdev
->driver
->move_notify
) {
377 bdev
->driver
->move_notify(bo
, false, mem
);
386 if (bdev
->driver
->invalidate_caches
) {
387 ret
= bdev
->driver
->invalidate_caches(bdev
, bo
->mem
.placement
);
389 pr_err("Can not flush read caches\n");
395 bo
->offset
= (bo
->mem
.start
<< PAGE_SHIFT
) +
396 bdev
->man
[bo
->mem
.mem_type
].gpu_offset
;
400 ctx
->bytes_moved
+= bo
->num_pages
<< PAGE_SHIFT
;
404 new_man
= &bdev
->man
[bo
->mem
.mem_type
];
405 if (new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) {
406 ttm_tt_destroy(bo
->ttm
);
415 * Will release GPU memory type usage on destruction.
416 * This is the place to put in driver specific hooks to release
417 * driver private resources.
418 * Will release the bo::reserved lock.
421 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object
*bo
)
423 if (bo
->bdev
->driver
->move_notify
)
424 bo
->bdev
->driver
->move_notify(bo
, false, NULL
);
426 ttm_tt_destroy(bo
->ttm
);
428 ttm_bo_mem_put(bo
, &bo
->mem
);
431 static int ttm_bo_individualize_resv(struct ttm_buffer_object
*bo
)
435 if (bo
->resv
== &bo
->ttm_resv
)
438 BUG_ON(!reservation_object_trylock(&bo
->ttm_resv
));
440 r
= reservation_object_copy_fences(&bo
->ttm_resv
, bo
->resv
);
442 reservation_object_unlock(&bo
->ttm_resv
);
447 static void ttm_bo_flush_all_fences(struct ttm_buffer_object
*bo
)
449 struct reservation_object_list
*fobj
;
450 struct dma_fence
*fence
;
453 fobj
= reservation_object_get_list(&bo
->ttm_resv
);
454 fence
= reservation_object_get_excl(&bo
->ttm_resv
);
455 if (fence
&& !fence
->ops
->signaled
)
456 dma_fence_enable_sw_signaling(fence
);
458 for (i
= 0; fobj
&& i
< fobj
->shared_count
; ++i
) {
459 fence
= rcu_dereference_protected(fobj
->shared
[i
],
460 reservation_object_held(bo
->resv
));
462 if (!fence
->ops
->signaled
)
463 dma_fence_enable_sw_signaling(fence
);
467 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object
*bo
)
469 struct ttm_bo_device
*bdev
= bo
->bdev
;
470 struct ttm_bo_global
*glob
= bdev
->glob
;
473 ret
= ttm_bo_individualize_resv(bo
);
475 /* Last resort, if we fail to allocate memory for the
476 * fences block for the BO to become idle
478 reservation_object_wait_timeout_rcu(bo
->resv
, true, false,
480 spin_lock(&glob
->lru_lock
);
484 spin_lock(&glob
->lru_lock
);
485 ret
= reservation_object_trylock(bo
->resv
) ? 0 : -EBUSY
;
487 if (reservation_object_test_signaled_rcu(&bo
->ttm_resv
, true)) {
488 ttm_bo_del_from_lru(bo
);
489 spin_unlock(&glob
->lru_lock
);
490 if (bo
->resv
!= &bo
->ttm_resv
)
491 reservation_object_unlock(&bo
->ttm_resv
);
493 ttm_bo_cleanup_memtype_use(bo
);
494 reservation_object_unlock(bo
->resv
);
498 ttm_bo_flush_all_fences(bo
);
501 * Make NO_EVICT bos immediately available to
502 * shrinkers, now that they are queued for
505 if (bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
) {
506 bo
->mem
.placement
&= ~TTM_PL_FLAG_NO_EVICT
;
507 ttm_bo_add_to_lru(bo
);
510 reservation_object_unlock(bo
->resv
);
512 if (bo
->resv
!= &bo
->ttm_resv
)
513 reservation_object_unlock(&bo
->ttm_resv
);
516 kref_get(&bo
->list_kref
);
517 list_add_tail(&bo
->ddestroy
, &bdev
->ddestroy
);
518 spin_unlock(&glob
->lru_lock
);
520 schedule_delayed_work(&bdev
->wq
,
521 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
525 * function ttm_bo_cleanup_refs
526 * If bo idle, remove from delayed- and lru lists, and unref.
527 * If not idle, do nothing.
529 * Must be called with lru_lock and reservation held, this function
530 * will drop the lru lock and optionally the reservation lock before returning.
532 * @interruptible Any sleeps should occur interruptibly.
533 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
534 * @unlock_resv Unlock the reservation lock as well.
537 static int ttm_bo_cleanup_refs(struct ttm_buffer_object
*bo
,
538 bool interruptible
, bool no_wait_gpu
,
541 struct ttm_bo_global
*glob
= bo
->bdev
->glob
;
542 struct reservation_object
*resv
;
545 if (unlikely(list_empty(&bo
->ddestroy
)))
548 resv
= &bo
->ttm_resv
;
550 if (reservation_object_test_signaled_rcu(resv
, true))
555 if (ret
&& !no_wait_gpu
) {
559 reservation_object_unlock(bo
->resv
);
560 spin_unlock(&glob
->lru_lock
);
562 lret
= reservation_object_wait_timeout_rcu(resv
, true,
571 spin_lock(&glob
->lru_lock
);
572 if (unlock_resv
&& !reservation_object_trylock(bo
->resv
)) {
574 * We raced, and lost, someone else holds the reservation now,
575 * and is probably busy in ttm_bo_cleanup_memtype_use.
577 * Even if it's not the case, because we finished waiting any
578 * delayed destruction would succeed, so just return success
581 spin_unlock(&glob
->lru_lock
);
587 if (ret
|| unlikely(list_empty(&bo
->ddestroy
))) {
589 reservation_object_unlock(bo
->resv
);
590 spin_unlock(&glob
->lru_lock
);
594 ttm_bo_del_from_lru(bo
);
595 list_del_init(&bo
->ddestroy
);
596 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
598 spin_unlock(&glob
->lru_lock
);
599 ttm_bo_cleanup_memtype_use(bo
);
602 reservation_object_unlock(bo
->resv
);
608 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
609 * encountered buffers.
611 static bool ttm_bo_delayed_delete(struct ttm_bo_device
*bdev
, bool remove_all
)
613 struct ttm_bo_global
*glob
= bdev
->glob
;
614 struct list_head removed
;
617 INIT_LIST_HEAD(&removed
);
619 spin_lock(&glob
->lru_lock
);
620 while (!list_empty(&bdev
->ddestroy
)) {
621 struct ttm_buffer_object
*bo
;
623 bo
= list_first_entry(&bdev
->ddestroy
, struct ttm_buffer_object
,
625 kref_get(&bo
->list_kref
);
626 list_move_tail(&bo
->ddestroy
, &removed
);
628 if (remove_all
|| bo
->resv
!= &bo
->ttm_resv
) {
629 spin_unlock(&glob
->lru_lock
);
630 reservation_object_lock(bo
->resv
, NULL
);
632 spin_lock(&glob
->lru_lock
);
633 ttm_bo_cleanup_refs(bo
, false, !remove_all
, true);
635 } else if (reservation_object_trylock(bo
->resv
)) {
636 ttm_bo_cleanup_refs(bo
, false, !remove_all
, true);
638 spin_unlock(&glob
->lru_lock
);
641 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
642 spin_lock(&glob
->lru_lock
);
644 list_splice_tail(&removed
, &bdev
->ddestroy
);
645 empty
= list_empty(&bdev
->ddestroy
);
646 spin_unlock(&glob
->lru_lock
);
651 static void ttm_bo_delayed_workqueue(struct work_struct
*work
)
653 struct ttm_bo_device
*bdev
=
654 container_of(work
, struct ttm_bo_device
, wq
.work
);
656 if (!ttm_bo_delayed_delete(bdev
, false))
657 schedule_delayed_work(&bdev
->wq
,
658 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
661 static void ttm_bo_release(struct kref
*kref
)
663 struct ttm_buffer_object
*bo
=
664 container_of(kref
, struct ttm_buffer_object
, kref
);
665 struct ttm_bo_device
*bdev
= bo
->bdev
;
666 struct ttm_mem_type_manager
*man
= &bdev
->man
[bo
->mem
.mem_type
];
668 drm_vma_offset_remove(&bdev
->vma_manager
, &bo
->vma_node
);
669 ttm_mem_io_lock(man
, false);
670 ttm_mem_io_free_vm(bo
);
671 ttm_mem_io_unlock(man
);
672 ttm_bo_cleanup_refs_or_queue(bo
);
673 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
676 void ttm_bo_put(struct ttm_buffer_object
*bo
)
678 kref_put(&bo
->kref
, ttm_bo_release
);
680 EXPORT_SYMBOL(ttm_bo_put
);
682 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device
*bdev
)
684 return cancel_delayed_work_sync(&bdev
->wq
);
686 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue
);
688 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device
*bdev
, int resched
)
691 schedule_delayed_work(&bdev
->wq
,
692 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
694 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue
);
696 static int ttm_bo_evict(struct ttm_buffer_object
*bo
,
697 struct ttm_operation_ctx
*ctx
)
699 struct ttm_bo_device
*bdev
= bo
->bdev
;
700 struct ttm_mem_reg evict_mem
;
701 struct ttm_placement placement
;
704 reservation_object_assert_held(bo
->resv
);
706 placement
.num_placement
= 0;
707 placement
.num_busy_placement
= 0;
708 bdev
->driver
->evict_flags(bo
, &placement
);
710 if (!placement
.num_placement
&& !placement
.num_busy_placement
) {
711 ret
= ttm_bo_pipeline_gutting(bo
);
715 return ttm_tt_create(bo
, false);
719 evict_mem
.mm_node
= NULL
;
720 evict_mem
.bus
.io_reserved_vm
= false;
721 evict_mem
.bus
.io_reserved_count
= 0;
723 ret
= ttm_bo_mem_space(bo
, &placement
, &evict_mem
, ctx
);
725 if (ret
!= -ERESTARTSYS
) {
726 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
728 ttm_bo_mem_space_debug(bo
, &placement
);
733 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true, ctx
);
735 if (ret
!= -ERESTARTSYS
)
736 pr_err("Buffer eviction failed\n");
737 ttm_bo_mem_put(bo
, &evict_mem
);
745 bool ttm_bo_eviction_valuable(struct ttm_buffer_object
*bo
,
746 const struct ttm_place
*place
)
748 /* Don't evict this BO if it's outside of the
749 * requested placement range
751 if (place
->fpfn
>= (bo
->mem
.start
+ bo
->mem
.size
) ||
752 (place
->lpfn
&& place
->lpfn
<= bo
->mem
.start
))
757 EXPORT_SYMBOL(ttm_bo_eviction_valuable
);
760 * Check the target bo is allowable to be evicted or swapout, including cases:
762 * a. if share same reservation object with ctx->resv, have assumption
763 * reservation objects should already be locked, so not lock again and
764 * return true directly when either the opreation allow_reserved_eviction
765 * or the target bo already is in delayed free list;
767 * b. Otherwise, trylock it.
769 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object
*bo
,
770 struct ttm_operation_ctx
*ctx
, bool *locked
)
775 if (bo
->resv
== ctx
->resv
) {
776 reservation_object_assert_held(bo
->resv
);
777 if (ctx
->flags
& TTM_OPT_FLAG_ALLOW_RES_EVICT
778 || !list_empty(&bo
->ddestroy
))
781 *locked
= reservation_object_trylock(bo
->resv
);
788 static int ttm_mem_evict_first(struct ttm_bo_device
*bdev
,
790 const struct ttm_place
*place
,
791 struct ttm_operation_ctx
*ctx
)
793 struct ttm_bo_global
*glob
= bdev
->glob
;
794 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
795 struct ttm_buffer_object
*bo
= NULL
;
800 spin_lock(&glob
->lru_lock
);
801 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
802 list_for_each_entry(bo
, &man
->lru
[i
], lru
) {
803 if (!ttm_bo_evict_swapout_allowable(bo
, ctx
, &locked
))
806 if (place
&& !bdev
->driver
->eviction_valuable(bo
,
809 reservation_object_unlock(bo
->resv
);
815 /* If the inner loop terminated early, we have our candidate */
816 if (&bo
->lru
!= &man
->lru
[i
])
823 spin_unlock(&glob
->lru_lock
);
827 kref_get(&bo
->list_kref
);
829 if (!list_empty(&bo
->ddestroy
)) {
830 ret
= ttm_bo_cleanup_refs(bo
, ctx
->interruptible
,
831 ctx
->no_wait_gpu
, locked
);
832 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
836 ttm_bo_del_from_lru(bo
);
837 spin_unlock(&glob
->lru_lock
);
839 ret
= ttm_bo_evict(bo
, ctx
);
841 ttm_bo_unreserve(bo
);
843 spin_lock(&glob
->lru_lock
);
844 ttm_bo_add_to_lru(bo
);
845 spin_unlock(&glob
->lru_lock
);
848 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
852 void ttm_bo_mem_put(struct ttm_buffer_object
*bo
, struct ttm_mem_reg
*mem
)
854 struct ttm_mem_type_manager
*man
= &bo
->bdev
->man
[mem
->mem_type
];
857 (*man
->func
->put_node
)(man
, mem
);
859 EXPORT_SYMBOL(ttm_bo_mem_put
);
862 * Add the last move fence to the BO and reserve a new shared slot.
864 static int ttm_bo_add_move_fence(struct ttm_buffer_object
*bo
,
865 struct ttm_mem_type_manager
*man
,
866 struct ttm_mem_reg
*mem
)
868 struct dma_fence
*fence
;
871 spin_lock(&man
->move_lock
);
872 fence
= dma_fence_get(man
->move
);
873 spin_unlock(&man
->move_lock
);
876 reservation_object_add_shared_fence(bo
->resv
, fence
);
878 ret
= reservation_object_reserve_shared(bo
->resv
, 1);
882 dma_fence_put(bo
->moving
);
890 * Repeatedly evict memory from the LRU for @mem_type until we create enough
891 * space, or we've evicted everything and there isn't enough space.
893 static int ttm_bo_mem_force_space(struct ttm_buffer_object
*bo
,
895 const struct ttm_place
*place
,
896 struct ttm_mem_reg
*mem
,
897 struct ttm_operation_ctx
*ctx
)
899 struct ttm_bo_device
*bdev
= bo
->bdev
;
900 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
904 ret
= (*man
->func
->get_node
)(man
, bo
, place
, mem
);
905 if (unlikely(ret
!= 0))
909 ret
= ttm_mem_evict_first(bdev
, mem_type
, place
, ctx
);
910 if (unlikely(ret
!= 0))
913 mem
->mem_type
= mem_type
;
914 return ttm_bo_add_move_fence(bo
, man
, mem
);
917 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager
*man
,
918 uint32_t cur_placement
,
919 uint32_t proposed_placement
)
921 uint32_t caching
= proposed_placement
& TTM_PL_MASK_CACHING
;
922 uint32_t result
= proposed_placement
& ~TTM_PL_MASK_CACHING
;
925 * Keep current caching if possible.
928 if ((cur_placement
& caching
) != 0)
929 result
|= (cur_placement
& caching
);
930 else if ((man
->default_caching
& caching
) != 0)
931 result
|= man
->default_caching
;
932 else if ((TTM_PL_FLAG_CACHED
& caching
) != 0)
933 result
|= TTM_PL_FLAG_CACHED
;
934 else if ((TTM_PL_FLAG_WC
& caching
) != 0)
935 result
|= TTM_PL_FLAG_WC
;
936 else if ((TTM_PL_FLAG_UNCACHED
& caching
) != 0)
937 result
|= TTM_PL_FLAG_UNCACHED
;
942 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager
*man
,
944 const struct ttm_place
*place
,
945 uint32_t *masked_placement
)
947 uint32_t cur_flags
= ttm_bo_type_flags(mem_type
);
949 if ((cur_flags
& place
->flags
& TTM_PL_MASK_MEM
) == 0)
952 if ((place
->flags
& man
->available_caching
) == 0)
955 cur_flags
|= (place
->flags
& man
->available_caching
);
957 *masked_placement
= cur_flags
;
962 * Creates space for memory region @mem according to its type.
964 * This function first searches for free space in compatible memory types in
965 * the priority order defined by the driver. If free space isn't found, then
966 * ttm_bo_mem_force_space is attempted in priority order to evict and find
969 int ttm_bo_mem_space(struct ttm_buffer_object
*bo
,
970 struct ttm_placement
*placement
,
971 struct ttm_mem_reg
*mem
,
972 struct ttm_operation_ctx
*ctx
)
974 struct ttm_bo_device
*bdev
= bo
->bdev
;
975 struct ttm_mem_type_manager
*man
;
976 uint32_t mem_type
= TTM_PL_SYSTEM
;
977 uint32_t cur_flags
= 0;
978 bool type_found
= false;
979 bool type_ok
= false;
980 bool has_erestartsys
= false;
983 ret
= reservation_object_reserve_shared(bo
->resv
, 1);
988 for (i
= 0; i
< placement
->num_placement
; ++i
) {
989 const struct ttm_place
*place
= &placement
->placement
[i
];
991 ret
= ttm_mem_type_from_place(place
, &mem_type
);
994 man
= &bdev
->man
[mem_type
];
995 if (!man
->has_type
|| !man
->use_type
)
998 type_ok
= ttm_bo_mt_compatible(man
, mem_type
, place
,
1005 cur_flags
= ttm_bo_select_caching(man
, bo
->mem
.placement
,
1008 * Use the access and other non-mapping-related flag bits from
1009 * the memory placement flags to the current flags
1011 ttm_flag_masked(&cur_flags
, place
->flags
,
1012 ~TTM_PL_MASK_MEMTYPE
);
1014 if (mem_type
== TTM_PL_SYSTEM
)
1017 ret
= (*man
->func
->get_node
)(man
, bo
, place
, mem
);
1022 ret
= ttm_bo_add_move_fence(bo
, man
, mem
);
1023 if (unlikely(ret
)) {
1024 (*man
->func
->put_node
)(man
, mem
);
1031 if ((type_ok
&& (mem_type
== TTM_PL_SYSTEM
)) || mem
->mm_node
) {
1032 mem
->mem_type
= mem_type
;
1033 mem
->placement
= cur_flags
;
1037 for (i
= 0; i
< placement
->num_busy_placement
; ++i
) {
1038 const struct ttm_place
*place
= &placement
->busy_placement
[i
];
1040 ret
= ttm_mem_type_from_place(place
, &mem_type
);
1043 man
= &bdev
->man
[mem_type
];
1044 if (!man
->has_type
|| !man
->use_type
)
1046 if (!ttm_bo_mt_compatible(man
, mem_type
, place
, &cur_flags
))
1050 cur_flags
= ttm_bo_select_caching(man
, bo
->mem
.placement
,
1053 * Use the access and other non-mapping-related flag bits from
1054 * the memory placement flags to the current flags
1056 ttm_flag_masked(&cur_flags
, place
->flags
,
1057 ~TTM_PL_MASK_MEMTYPE
);
1059 if (mem_type
== TTM_PL_SYSTEM
) {
1060 mem
->mem_type
= mem_type
;
1061 mem
->placement
= cur_flags
;
1062 mem
->mm_node
= NULL
;
1066 ret
= ttm_bo_mem_force_space(bo
, mem_type
, place
, mem
, ctx
);
1067 if (ret
== 0 && mem
->mm_node
) {
1068 mem
->placement
= cur_flags
;
1071 if (ret
== -ERESTARTSYS
)
1072 has_erestartsys
= true;
1076 pr_err(TTM_PFX
"No compatible memory type found\n");
1080 return (has_erestartsys
) ? -ERESTARTSYS
: -ENOMEM
;
1082 EXPORT_SYMBOL(ttm_bo_mem_space
);
1084 static int ttm_bo_move_buffer(struct ttm_buffer_object
*bo
,
1085 struct ttm_placement
*placement
,
1086 struct ttm_operation_ctx
*ctx
)
1089 struct ttm_mem_reg mem
;
1091 reservation_object_assert_held(bo
->resv
);
1093 mem
.num_pages
= bo
->num_pages
;
1094 mem
.size
= mem
.num_pages
<< PAGE_SHIFT
;
1095 mem
.page_alignment
= bo
->mem
.page_alignment
;
1096 mem
.bus
.io_reserved_vm
= false;
1097 mem
.bus
.io_reserved_count
= 0;
1099 * Determine where to move the buffer.
1101 ret
= ttm_bo_mem_space(bo
, placement
, &mem
, ctx
);
1104 ret
= ttm_bo_handle_move_mem(bo
, &mem
, false, ctx
);
1106 if (ret
&& mem
.mm_node
)
1107 ttm_bo_mem_put(bo
, &mem
);
1111 static bool ttm_bo_places_compat(const struct ttm_place
*places
,
1112 unsigned num_placement
,
1113 struct ttm_mem_reg
*mem
,
1114 uint32_t *new_flags
)
1118 for (i
= 0; i
< num_placement
; i
++) {
1119 const struct ttm_place
*heap
= &places
[i
];
1121 if (mem
->mm_node
&& (mem
->start
< heap
->fpfn
||
1122 (heap
->lpfn
!= 0 && (mem
->start
+ mem
->num_pages
) > heap
->lpfn
)))
1125 *new_flags
= heap
->flags
;
1126 if ((*new_flags
& mem
->placement
& TTM_PL_MASK_CACHING
) &&
1127 (*new_flags
& mem
->placement
& TTM_PL_MASK_MEM
) &&
1128 (!(*new_flags
& TTM_PL_FLAG_CONTIGUOUS
) ||
1129 (mem
->placement
& TTM_PL_FLAG_CONTIGUOUS
)))
1135 bool ttm_bo_mem_compat(struct ttm_placement
*placement
,
1136 struct ttm_mem_reg
*mem
,
1137 uint32_t *new_flags
)
1139 if (ttm_bo_places_compat(placement
->placement
, placement
->num_placement
,
1143 if ((placement
->busy_placement
!= placement
->placement
||
1144 placement
->num_busy_placement
> placement
->num_placement
) &&
1145 ttm_bo_places_compat(placement
->busy_placement
,
1146 placement
->num_busy_placement
,
1152 EXPORT_SYMBOL(ttm_bo_mem_compat
);
1154 int ttm_bo_validate(struct ttm_buffer_object
*bo
,
1155 struct ttm_placement
*placement
,
1156 struct ttm_operation_ctx
*ctx
)
1161 reservation_object_assert_held(bo
->resv
);
1163 * Check whether we need to move buffer.
1165 if (!ttm_bo_mem_compat(placement
, &bo
->mem
, &new_flags
)) {
1166 ret
= ttm_bo_move_buffer(bo
, placement
, ctx
);
1171 * Use the access and other non-mapping-related flag bits from
1172 * the compatible memory placement flags to the active flags
1174 ttm_flag_masked(&bo
->mem
.placement
, new_flags
,
1175 ~TTM_PL_MASK_MEMTYPE
);
1178 * We might need to add a TTM.
1180 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
&& bo
->ttm
== NULL
) {
1181 ret
= ttm_tt_create(bo
, true);
1187 EXPORT_SYMBOL(ttm_bo_validate
);
1189 int ttm_bo_init_reserved(struct ttm_bo_device
*bdev
,
1190 struct ttm_buffer_object
*bo
,
1192 enum ttm_bo_type type
,
1193 struct ttm_placement
*placement
,
1194 uint32_t page_alignment
,
1195 struct ttm_operation_ctx
*ctx
,
1197 struct sg_table
*sg
,
1198 struct reservation_object
*resv
,
1199 void (*destroy
) (struct ttm_buffer_object
*))
1202 unsigned long num_pages
;
1203 struct ttm_mem_global
*mem_glob
= bdev
->glob
->mem_glob
;
1206 ret
= ttm_mem_global_alloc(mem_glob
, acc_size
, ctx
);
1208 pr_err("Out of kernel memory\n");
1216 num_pages
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1217 if (num_pages
== 0) {
1218 pr_err("Illegal buffer object size\n");
1223 ttm_mem_global_free(mem_glob
, acc_size
);
1226 bo
->destroy
= destroy
? destroy
: ttm_bo_default_destroy
;
1228 kref_init(&bo
->kref
);
1229 kref_init(&bo
->list_kref
);
1230 atomic_set(&bo
->cpu_writers
, 0);
1231 INIT_LIST_HEAD(&bo
->lru
);
1232 INIT_LIST_HEAD(&bo
->ddestroy
);
1233 INIT_LIST_HEAD(&bo
->swap
);
1234 INIT_LIST_HEAD(&bo
->io_reserve_lru
);
1235 mutex_init(&bo
->wu_mutex
);
1238 bo
->num_pages
= num_pages
;
1239 bo
->mem
.size
= num_pages
<< PAGE_SHIFT
;
1240 bo
->mem
.mem_type
= TTM_PL_SYSTEM
;
1241 bo
->mem
.num_pages
= bo
->num_pages
;
1242 bo
->mem
.mm_node
= NULL
;
1243 bo
->mem
.page_alignment
= page_alignment
;
1244 bo
->mem
.bus
.io_reserved_vm
= false;
1245 bo
->mem
.bus
.io_reserved_count
= 0;
1247 bo
->mem
.placement
= (TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
);
1248 bo
->acc_size
= acc_size
;
1252 reservation_object_assert_held(bo
->resv
);
1254 bo
->resv
= &bo
->ttm_resv
;
1256 reservation_object_init(&bo
->ttm_resv
);
1257 atomic_inc(&bo
->bdev
->glob
->bo_count
);
1258 drm_vma_node_reset(&bo
->vma_node
);
1261 * For ttm_bo_type_device buffers, allocate
1262 * address space from the device.
1264 if (bo
->type
== ttm_bo_type_device
||
1265 bo
->type
== ttm_bo_type_sg
)
1266 ret
= drm_vma_offset_add(&bdev
->vma_manager
, &bo
->vma_node
,
1269 /* passed reservation objects should already be locked,
1270 * since otherwise lockdep will be angered in radeon.
1273 locked
= reservation_object_trylock(bo
->resv
);
1278 ret
= ttm_bo_validate(bo
, placement
, ctx
);
1280 if (unlikely(ret
)) {
1282 ttm_bo_unreserve(bo
);
1288 if (resv
&& !(bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
)) {
1289 spin_lock(&bdev
->glob
->lru_lock
);
1290 ttm_bo_add_to_lru(bo
);
1291 spin_unlock(&bdev
->glob
->lru_lock
);
1296 EXPORT_SYMBOL(ttm_bo_init_reserved
);
1298 int ttm_bo_init(struct ttm_bo_device
*bdev
,
1299 struct ttm_buffer_object
*bo
,
1301 enum ttm_bo_type type
,
1302 struct ttm_placement
*placement
,
1303 uint32_t page_alignment
,
1306 struct sg_table
*sg
,
1307 struct reservation_object
*resv
,
1308 void (*destroy
) (struct ttm_buffer_object
*))
1310 struct ttm_operation_ctx ctx
= { interruptible
, false };
1313 ret
= ttm_bo_init_reserved(bdev
, bo
, size
, type
, placement
,
1314 page_alignment
, &ctx
, acc_size
,
1320 ttm_bo_unreserve(bo
);
1324 EXPORT_SYMBOL(ttm_bo_init
);
1326 size_t ttm_bo_acc_size(struct ttm_bo_device
*bdev
,
1327 unsigned long bo_size
,
1328 unsigned struct_size
)
1330 unsigned npages
= (PAGE_ALIGN(bo_size
)) >> PAGE_SHIFT
;
1333 size
+= ttm_round_pot(struct_size
);
1334 size
+= ttm_round_pot(npages
* sizeof(void *));
1335 size
+= ttm_round_pot(sizeof(struct ttm_tt
));
1338 EXPORT_SYMBOL(ttm_bo_acc_size
);
1340 size_t ttm_bo_dma_acc_size(struct ttm_bo_device
*bdev
,
1341 unsigned long bo_size
,
1342 unsigned struct_size
)
1344 unsigned npages
= (PAGE_ALIGN(bo_size
)) >> PAGE_SHIFT
;
1347 size
+= ttm_round_pot(struct_size
);
1348 size
+= ttm_round_pot(npages
* (2*sizeof(void *) + sizeof(dma_addr_t
)));
1349 size
+= ttm_round_pot(sizeof(struct ttm_dma_tt
));
1352 EXPORT_SYMBOL(ttm_bo_dma_acc_size
);
1354 int ttm_bo_create(struct ttm_bo_device
*bdev
,
1356 enum ttm_bo_type type
,
1357 struct ttm_placement
*placement
,
1358 uint32_t page_alignment
,
1360 struct ttm_buffer_object
**p_bo
)
1362 struct ttm_buffer_object
*bo
;
1366 bo
= kzalloc(sizeof(*bo
), GFP_KERNEL
);
1367 if (unlikely(bo
== NULL
))
1370 acc_size
= ttm_bo_acc_size(bdev
, size
, sizeof(struct ttm_buffer_object
));
1371 ret
= ttm_bo_init(bdev
, bo
, size
, type
, placement
, page_alignment
,
1372 interruptible
, acc_size
,
1374 if (likely(ret
== 0))
1379 EXPORT_SYMBOL(ttm_bo_create
);
1381 static int ttm_bo_force_list_clean(struct ttm_bo_device
*bdev
,
1384 struct ttm_operation_ctx ctx
= {
1385 .interruptible
= false,
1386 .no_wait_gpu
= false,
1387 .flags
= TTM_OPT_FLAG_FORCE_ALLOC
1389 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1390 struct ttm_bo_global
*glob
= bdev
->glob
;
1391 struct dma_fence
*fence
;
1396 * Can't use standard list traversal since we're unlocking.
1399 spin_lock(&glob
->lru_lock
);
1400 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
1401 while (!list_empty(&man
->lru
[i
])) {
1402 spin_unlock(&glob
->lru_lock
);
1403 ret
= ttm_mem_evict_first(bdev
, mem_type
, NULL
, &ctx
);
1406 spin_lock(&glob
->lru_lock
);
1409 spin_unlock(&glob
->lru_lock
);
1411 spin_lock(&man
->move_lock
);
1412 fence
= dma_fence_get(man
->move
);
1413 spin_unlock(&man
->move_lock
);
1416 ret
= dma_fence_wait(fence
, false);
1417 dma_fence_put(fence
);
1425 int ttm_bo_clean_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1427 struct ttm_mem_type_manager
*man
;
1430 if (mem_type
>= TTM_NUM_MEM_TYPES
) {
1431 pr_err("Illegal memory type %d\n", mem_type
);
1434 man
= &bdev
->man
[mem_type
];
1436 if (!man
->has_type
) {
1437 pr_err("Trying to take down uninitialized memory manager type %u\n",
1442 man
->use_type
= false;
1443 man
->has_type
= false;
1447 ret
= ttm_bo_force_list_clean(bdev
, mem_type
);
1449 pr_err("Cleanup eviction failed\n");
1453 ret
= (*man
->func
->takedown
)(man
);
1456 dma_fence_put(man
->move
);
1461 EXPORT_SYMBOL(ttm_bo_clean_mm
);
1463 int ttm_bo_evict_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1465 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1467 if (mem_type
== 0 || mem_type
>= TTM_NUM_MEM_TYPES
) {
1468 pr_err("Illegal memory manager memory type %u\n", mem_type
);
1472 if (!man
->has_type
) {
1473 pr_err("Memory type %u has not been initialized\n", mem_type
);
1477 return ttm_bo_force_list_clean(bdev
, mem_type
);
1479 EXPORT_SYMBOL(ttm_bo_evict_mm
);
1481 int ttm_bo_init_mm(struct ttm_bo_device
*bdev
, unsigned type
,
1482 unsigned long p_size
)
1485 struct ttm_mem_type_manager
*man
;
1488 BUG_ON(type
>= TTM_NUM_MEM_TYPES
);
1489 man
= &bdev
->man
[type
];
1490 BUG_ON(man
->has_type
);
1491 man
->io_reserve_fastpath
= true;
1492 man
->use_io_reserve_lru
= false;
1493 mutex_init(&man
->io_reserve_mutex
);
1494 spin_lock_init(&man
->move_lock
);
1495 INIT_LIST_HEAD(&man
->io_reserve_lru
);
1497 ret
= bdev
->driver
->init_mem_type(bdev
, type
, man
);
1502 if (type
!= TTM_PL_SYSTEM
) {
1503 ret
= (*man
->func
->init
)(man
, p_size
);
1507 man
->has_type
= true;
1508 man
->use_type
= true;
1511 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
)
1512 INIT_LIST_HEAD(&man
->lru
[i
]);
1517 EXPORT_SYMBOL(ttm_bo_init_mm
);
1519 static void ttm_bo_global_kobj_release(struct kobject
*kobj
)
1521 struct ttm_bo_global
*glob
=
1522 container_of(kobj
, struct ttm_bo_global
, kobj
);
1524 __free_page(glob
->dummy_read_page
);
1527 static void ttm_bo_global_release(void)
1529 struct ttm_bo_global
*glob
= &ttm_bo_glob
;
1531 mutex_lock(&ttm_global_mutex
);
1532 if (--glob
->use_count
> 0)
1535 kobject_del(&glob
->kobj
);
1536 kobject_put(&glob
->kobj
);
1537 ttm_mem_global_release(&ttm_mem_glob
);
1539 mutex_unlock(&ttm_global_mutex
);
1542 static int ttm_bo_global_init(void)
1544 struct ttm_bo_global
*glob
= &ttm_bo_glob
;
1548 mutex_lock(&ttm_global_mutex
);
1549 if (++glob
->use_count
> 1)
1552 ret
= ttm_mem_global_init(&ttm_mem_glob
);
1556 spin_lock_init(&glob
->lru_lock
);
1557 glob
->mem_glob
= &ttm_mem_glob
;
1558 glob
->mem_glob
->bo_glob
= glob
;
1559 glob
->dummy_read_page
= alloc_page(__GFP_ZERO
| GFP_DMA32
);
1561 if (unlikely(glob
->dummy_read_page
== NULL
)) {
1566 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
)
1567 INIT_LIST_HEAD(&glob
->swap_lru
[i
]);
1568 INIT_LIST_HEAD(&glob
->device_list
);
1569 atomic_set(&glob
->bo_count
, 0);
1571 ret
= kobject_init_and_add(
1572 &glob
->kobj
, &ttm_bo_glob_kobj_type
, ttm_get_kobj(), "buffer_objects");
1573 if (unlikely(ret
!= 0))
1574 kobject_put(&glob
->kobj
);
1576 mutex_unlock(&ttm_global_mutex
);
1580 int ttm_bo_device_release(struct ttm_bo_device
*bdev
)
1583 unsigned i
= TTM_NUM_MEM_TYPES
;
1584 struct ttm_mem_type_manager
*man
;
1585 struct ttm_bo_global
*glob
= bdev
->glob
;
1588 man
= &bdev
->man
[i
];
1589 if (man
->has_type
) {
1590 man
->use_type
= false;
1591 if ((i
!= TTM_PL_SYSTEM
) && ttm_bo_clean_mm(bdev
, i
)) {
1593 pr_err("DRM memory manager type %d is not clean\n",
1596 man
->has_type
= false;
1600 mutex_lock(&ttm_global_mutex
);
1601 list_del(&bdev
->device_list
);
1602 mutex_unlock(&ttm_global_mutex
);
1604 cancel_delayed_work_sync(&bdev
->wq
);
1606 if (ttm_bo_delayed_delete(bdev
, true))
1607 pr_debug("Delayed destroy list was clean\n");
1609 spin_lock(&glob
->lru_lock
);
1610 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
)
1611 if (list_empty(&bdev
->man
[0].lru
[0]))
1612 pr_debug("Swap list %d was clean\n", i
);
1613 spin_unlock(&glob
->lru_lock
);
1615 drm_vma_offset_manager_destroy(&bdev
->vma_manager
);
1618 ttm_bo_global_release();
1622 EXPORT_SYMBOL(ttm_bo_device_release
);
1624 int ttm_bo_device_init(struct ttm_bo_device
*bdev
,
1625 struct ttm_bo_driver
*driver
,
1626 struct address_space
*mapping
,
1627 uint64_t file_page_offset
,
1630 struct ttm_bo_global
*glob
= &ttm_bo_glob
;
1633 ret
= ttm_bo_global_init();
1637 bdev
->driver
= driver
;
1639 memset(bdev
->man
, 0, sizeof(bdev
->man
));
1642 * Initialize the system memory buffer type.
1643 * Other types need to be driver / IOCTL initialized.
1645 ret
= ttm_bo_init_mm(bdev
, TTM_PL_SYSTEM
, 0);
1646 if (unlikely(ret
!= 0))
1649 drm_vma_offset_manager_init(&bdev
->vma_manager
, file_page_offset
,
1651 INIT_DELAYED_WORK(&bdev
->wq
, ttm_bo_delayed_workqueue
);
1652 INIT_LIST_HEAD(&bdev
->ddestroy
);
1653 bdev
->dev_mapping
= mapping
;
1655 bdev
->need_dma32
= need_dma32
;
1656 mutex_lock(&ttm_global_mutex
);
1657 list_add_tail(&bdev
->device_list
, &glob
->device_list
);
1658 mutex_unlock(&ttm_global_mutex
);
1662 ttm_bo_global_release();
1665 EXPORT_SYMBOL(ttm_bo_device_init
);
1668 * buffer object vm functions.
1671 bool ttm_mem_reg_is_pci(struct ttm_bo_device
*bdev
, struct ttm_mem_reg
*mem
)
1673 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem
->mem_type
];
1675 if (!(man
->flags
& TTM_MEMTYPE_FLAG_FIXED
)) {
1676 if (mem
->mem_type
== TTM_PL_SYSTEM
)
1679 if (man
->flags
& TTM_MEMTYPE_FLAG_CMA
)
1682 if (mem
->placement
& TTM_PL_FLAG_CACHED
)
1688 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object
*bo
)
1690 struct ttm_bo_device
*bdev
= bo
->bdev
;
1692 drm_vma_node_unmap(&bo
->vma_node
, bdev
->dev_mapping
);
1693 ttm_mem_io_free_vm(bo
);
1696 void ttm_bo_unmap_virtual(struct ttm_buffer_object
*bo
)
1698 struct ttm_bo_device
*bdev
= bo
->bdev
;
1699 struct ttm_mem_type_manager
*man
= &bdev
->man
[bo
->mem
.mem_type
];
1701 ttm_mem_io_lock(man
, false);
1702 ttm_bo_unmap_virtual_locked(bo
);
1703 ttm_mem_io_unlock(man
);
1707 EXPORT_SYMBOL(ttm_bo_unmap_virtual
);
1709 int ttm_bo_wait(struct ttm_buffer_object
*bo
,
1710 bool interruptible
, bool no_wait
)
1712 long timeout
= 15 * HZ
;
1715 if (reservation_object_test_signaled_rcu(bo
->resv
, true))
1721 timeout
= reservation_object_wait_timeout_rcu(bo
->resv
, true,
1722 interruptible
, timeout
);
1729 reservation_object_add_excl_fence(bo
->resv
, NULL
);
1732 EXPORT_SYMBOL(ttm_bo_wait
);
1734 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object
*bo
, bool no_wait
)
1739 * Using ttm_bo_reserve makes sure the lru lists are updated.
1742 ret
= ttm_bo_reserve(bo
, true, no_wait
, NULL
);
1743 if (unlikely(ret
!= 0))
1745 ret
= ttm_bo_wait(bo
, true, no_wait
);
1746 if (likely(ret
== 0))
1747 atomic_inc(&bo
->cpu_writers
);
1748 ttm_bo_unreserve(bo
);
1751 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab
);
1753 void ttm_bo_synccpu_write_release(struct ttm_buffer_object
*bo
)
1755 atomic_dec(&bo
->cpu_writers
);
1757 EXPORT_SYMBOL(ttm_bo_synccpu_write_release
);
1760 * A buffer object shrink method that tries to swap out the first
1761 * buffer object on the bo_global::swap_lru list.
1763 int ttm_bo_swapout(struct ttm_bo_global
*glob
, struct ttm_operation_ctx
*ctx
)
1765 struct ttm_buffer_object
*bo
;
1770 spin_lock(&glob
->lru_lock
);
1771 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
1772 list_for_each_entry(bo
, &glob
->swap_lru
[i
], swap
) {
1773 if (ttm_bo_evict_swapout_allowable(bo
, ctx
, &locked
)) {
1783 spin_unlock(&glob
->lru_lock
);
1787 kref_get(&bo
->list_kref
);
1789 if (!list_empty(&bo
->ddestroy
)) {
1790 ret
= ttm_bo_cleanup_refs(bo
, false, false, locked
);
1791 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1795 ttm_bo_del_from_lru(bo
);
1796 spin_unlock(&glob
->lru_lock
);
1799 * Move to system cached
1802 if (bo
->mem
.mem_type
!= TTM_PL_SYSTEM
||
1803 bo
->ttm
->caching_state
!= tt_cached
) {
1804 struct ttm_operation_ctx ctx
= { false, false };
1805 struct ttm_mem_reg evict_mem
;
1807 evict_mem
= bo
->mem
;
1808 evict_mem
.mm_node
= NULL
;
1809 evict_mem
.placement
= TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
;
1810 evict_mem
.mem_type
= TTM_PL_SYSTEM
;
1812 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true, &ctx
);
1813 if (unlikely(ret
!= 0))
1818 * Make sure BO is idle.
1821 ret
= ttm_bo_wait(bo
, false, false);
1822 if (unlikely(ret
!= 0))
1825 ttm_bo_unmap_virtual(bo
);
1828 * Swap out. Buffer will be swapped in again as soon as
1829 * anyone tries to access a ttm page.
1832 if (bo
->bdev
->driver
->swap_notify
)
1833 bo
->bdev
->driver
->swap_notify(bo
);
1835 ret
= ttm_tt_swapout(bo
->ttm
, bo
->persistent_swap_storage
);
1840 * Unreserve without putting on LRU to avoid swapping out an
1841 * already swapped buffer.
1844 reservation_object_unlock(bo
->resv
);
1845 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1848 EXPORT_SYMBOL(ttm_bo_swapout
);
1850 void ttm_bo_swapout_all(struct ttm_bo_device
*bdev
)
1852 struct ttm_operation_ctx ctx
= {
1853 .interruptible
= false,
1854 .no_wait_gpu
= false
1857 while (ttm_bo_swapout(bdev
->glob
, &ctx
) == 0)
1860 EXPORT_SYMBOL(ttm_bo_swapout_all
);
1863 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1866 * @bo: Pointer to buffer
1868 int ttm_bo_wait_unreserved(struct ttm_buffer_object
*bo
)
1873 * In the absense of a wait_unlocked API,
1874 * Use the bo::wu_mutex to avoid triggering livelocks due to
1875 * concurrent use of this function. Note that this use of
1876 * bo::wu_mutex can go away if we change locking order to
1877 * mmap_sem -> bo::reserve.
1879 ret
= mutex_lock_interruptible(&bo
->wu_mutex
);
1880 if (unlikely(ret
!= 0))
1881 return -ERESTARTSYS
;
1882 if (!ww_mutex_is_locked(&bo
->resv
->lock
))
1884 ret
= reservation_object_lock_interruptible(bo
->resv
, NULL
);
1887 if (unlikely(ret
!= 0))
1889 reservation_object_unlock(bo
->resv
);
1892 mutex_unlock(&bo
->wu_mutex
);