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 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 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(bo
->mem
.mm_node
!= NULL
);
157 BUG_ON(!list_empty(&bo
->lru
));
158 BUG_ON(!list_empty(&bo
->ddestroy
));
159 ttm_tt_destroy(bo
->ttm
);
160 atomic_dec(&ttm_bo_glob
.bo_count
);
161 dma_fence_put(bo
->moving
);
162 if (!ttm_bo_uses_embedded_gem_object(bo
))
163 dma_resv_fini(&bo
->base
._resv
);
165 ttm_mem_global_free(&ttm_mem_glob
, acc_size
);
168 static void ttm_bo_add_mem_to_lru(struct ttm_buffer_object
*bo
,
169 struct ttm_mem_reg
*mem
)
171 struct ttm_bo_device
*bdev
= bo
->bdev
;
172 struct ttm_mem_type_manager
*man
;
174 dma_resv_assert_held(bo
->base
.resv
);
176 if (!list_empty(&bo
->lru
))
179 if (mem
->placement
& TTM_PL_FLAG_NO_EVICT
)
182 man
= &bdev
->man
[mem
->mem_type
];
183 list_add_tail(&bo
->lru
, &man
->lru
[bo
->priority
]);
184 kref_get(&bo
->list_kref
);
186 if (!(man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) && bo
->ttm
&&
187 !(bo
->ttm
->page_flags
& (TTM_PAGE_FLAG_SG
|
188 TTM_PAGE_FLAG_SWAPPED
))) {
189 list_add_tail(&bo
->swap
, &ttm_bo_glob
.swap_lru
[bo
->priority
]);
190 kref_get(&bo
->list_kref
);
194 static void ttm_bo_ref_bug(struct kref
*list_kref
)
199 static 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 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos
*pos
,
220 struct ttm_buffer_object
*bo
)
227 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object
*bo
,
228 struct ttm_lru_bulk_move
*bulk
)
230 dma_resv_assert_held(bo
->base
.resv
);
232 ttm_bo_del_from_lru(bo
);
233 ttm_bo_add_mem_to_lru(bo
, &bo
->mem
);
235 if (bulk
&& !(bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
)) {
236 switch (bo
->mem
.mem_type
) {
238 ttm_bo_bulk_move_set_pos(&bulk
->tt
[bo
->priority
], bo
);
242 ttm_bo_bulk_move_set_pos(&bulk
->vram
[bo
->priority
], bo
);
245 if (bo
->ttm
&& !(bo
->ttm
->page_flags
&
246 (TTM_PAGE_FLAG_SG
| TTM_PAGE_FLAG_SWAPPED
)))
247 ttm_bo_bulk_move_set_pos(&bulk
->swap
[bo
->priority
], bo
);
250 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail
);
252 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move
*bulk
)
256 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
257 struct ttm_lru_bulk_move_pos
*pos
= &bulk
->tt
[i
];
258 struct ttm_mem_type_manager
*man
;
263 dma_resv_assert_held(pos
->first
->base
.resv
);
264 dma_resv_assert_held(pos
->last
->base
.resv
);
266 man
= &pos
->first
->bdev
->man
[TTM_PL_TT
];
267 list_bulk_move_tail(&man
->lru
[i
], &pos
->first
->lru
,
271 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
272 struct ttm_lru_bulk_move_pos
*pos
= &bulk
->vram
[i
];
273 struct ttm_mem_type_manager
*man
;
278 dma_resv_assert_held(pos
->first
->base
.resv
);
279 dma_resv_assert_held(pos
->last
->base
.resv
);
281 man
= &pos
->first
->bdev
->man
[TTM_PL_VRAM
];
282 list_bulk_move_tail(&man
->lru
[i
], &pos
->first
->lru
,
286 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
287 struct ttm_lru_bulk_move_pos
*pos
= &bulk
->swap
[i
];
288 struct list_head
*lru
;
293 dma_resv_assert_held(pos
->first
->base
.resv
);
294 dma_resv_assert_held(pos
->last
->base
.resv
);
296 lru
= &ttm_bo_glob
.swap_lru
[i
];
297 list_bulk_move_tail(lru
, &pos
->first
->swap
, &pos
->last
->swap
);
300 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail
);
302 static int ttm_bo_handle_move_mem(struct ttm_buffer_object
*bo
,
303 struct ttm_mem_reg
*mem
, bool evict
,
304 struct ttm_operation_ctx
*ctx
)
306 struct ttm_bo_device
*bdev
= bo
->bdev
;
307 bool old_is_pci
= ttm_mem_reg_is_pci(bdev
, &bo
->mem
);
308 bool new_is_pci
= ttm_mem_reg_is_pci(bdev
, mem
);
309 struct ttm_mem_type_manager
*old_man
= &bdev
->man
[bo
->mem
.mem_type
];
310 struct ttm_mem_type_manager
*new_man
= &bdev
->man
[mem
->mem_type
];
313 if (old_is_pci
|| new_is_pci
||
314 ((mem
->placement
& bo
->mem
.placement
& TTM_PL_MASK_CACHING
) == 0)) {
315 ret
= ttm_mem_io_lock(old_man
, true);
316 if (unlikely(ret
!= 0))
318 ttm_bo_unmap_virtual_locked(bo
);
319 ttm_mem_io_unlock(old_man
);
323 * Create and bind a ttm if required.
326 if (!(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
)) {
327 if (bo
->ttm
== NULL
) {
328 bool zero
= !(old_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
);
329 ret
= ttm_tt_create(bo
, zero
);
334 ret
= ttm_tt_set_placement_caching(bo
->ttm
, mem
->placement
);
338 if (mem
->mem_type
!= TTM_PL_SYSTEM
) {
339 ret
= ttm_tt_bind(bo
->ttm
, mem
, ctx
);
344 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
) {
345 if (bdev
->driver
->move_notify
)
346 bdev
->driver
->move_notify(bo
, evict
, mem
);
353 if (bdev
->driver
->move_notify
)
354 bdev
->driver
->move_notify(bo
, evict
, mem
);
356 if (!(old_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) &&
357 !(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
))
358 ret
= ttm_bo_move_ttm(bo
, ctx
, mem
);
359 else if (bdev
->driver
->move
)
360 ret
= bdev
->driver
->move(bo
, evict
, ctx
, mem
);
362 ret
= ttm_bo_move_memcpy(bo
, ctx
, mem
);
365 if (bdev
->driver
->move_notify
) {
367 bdev
->driver
->move_notify(bo
, false, mem
);
376 if (bdev
->driver
->invalidate_caches
) {
377 ret
= bdev
->driver
->invalidate_caches(bdev
, bo
->mem
.placement
);
379 pr_err("Can not flush read caches\n");
385 bo
->offset
= (bo
->mem
.start
<< PAGE_SHIFT
) +
386 bdev
->man
[bo
->mem
.mem_type
].gpu_offset
;
390 ctx
->bytes_moved
+= bo
->num_pages
<< PAGE_SHIFT
;
394 new_man
= &bdev
->man
[bo
->mem
.mem_type
];
395 if (new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) {
396 ttm_tt_destroy(bo
->ttm
);
405 * Will release GPU memory type usage on destruction.
406 * This is the place to put in driver specific hooks to release
407 * driver private resources.
408 * Will release the bo::reserved lock.
411 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object
*bo
)
413 if (bo
->bdev
->driver
->move_notify
)
414 bo
->bdev
->driver
->move_notify(bo
, false, NULL
);
416 ttm_tt_destroy(bo
->ttm
);
418 ttm_bo_mem_put(bo
, &bo
->mem
);
421 static int ttm_bo_individualize_resv(struct ttm_buffer_object
*bo
)
425 if (bo
->base
.resv
== &bo
->base
._resv
)
428 BUG_ON(!dma_resv_trylock(&bo
->base
._resv
));
430 r
= dma_resv_copy_fences(&bo
->base
._resv
, bo
->base
.resv
);
432 dma_resv_unlock(&bo
->base
._resv
);
437 static void ttm_bo_flush_all_fences(struct ttm_buffer_object
*bo
)
439 struct dma_resv_list
*fobj
;
440 struct dma_fence
*fence
;
443 fobj
= dma_resv_get_list(&bo
->base
._resv
);
444 fence
= dma_resv_get_excl(&bo
->base
._resv
);
445 if (fence
&& !fence
->ops
->signaled
)
446 dma_fence_enable_sw_signaling(fence
);
448 for (i
= 0; fobj
&& i
< fobj
->shared_count
; ++i
) {
449 fence
= rcu_dereference_protected(fobj
->shared
[i
],
450 dma_resv_held(bo
->base
.resv
));
452 if (!fence
->ops
->signaled
)
453 dma_fence_enable_sw_signaling(fence
);
457 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object
*bo
)
459 struct ttm_bo_device
*bdev
= bo
->bdev
;
462 ret
= ttm_bo_individualize_resv(bo
);
464 /* Last resort, if we fail to allocate memory for the
465 * fences block for the BO to become idle
467 dma_resv_wait_timeout_rcu(bo
->base
.resv
, true, false,
469 spin_lock(&ttm_bo_glob
.lru_lock
);
473 spin_lock(&ttm_bo_glob
.lru_lock
);
474 ret
= dma_resv_trylock(bo
->base
.resv
) ? 0 : -EBUSY
;
476 if (dma_resv_test_signaled_rcu(&bo
->base
._resv
, true)) {
477 ttm_bo_del_from_lru(bo
);
478 spin_unlock(&ttm_bo_glob
.lru_lock
);
479 if (bo
->base
.resv
!= &bo
->base
._resv
)
480 dma_resv_unlock(&bo
->base
._resv
);
482 ttm_bo_cleanup_memtype_use(bo
);
483 dma_resv_unlock(bo
->base
.resv
);
487 ttm_bo_flush_all_fences(bo
);
490 * Make NO_EVICT bos immediately available to
491 * shrinkers, now that they are queued for
494 if (bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
) {
495 bo
->mem
.placement
&= ~TTM_PL_FLAG_NO_EVICT
;
496 ttm_bo_move_to_lru_tail(bo
, NULL
);
499 dma_resv_unlock(bo
->base
.resv
);
501 if (bo
->base
.resv
!= &bo
->base
._resv
)
502 dma_resv_unlock(&bo
->base
._resv
);
505 kref_get(&bo
->list_kref
);
506 list_add_tail(&bo
->ddestroy
, &bdev
->ddestroy
);
507 spin_unlock(&ttm_bo_glob
.lru_lock
);
509 schedule_delayed_work(&bdev
->wq
,
510 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
514 * function ttm_bo_cleanup_refs
515 * If bo idle, remove from delayed- and lru lists, and unref.
516 * If not idle, do nothing.
518 * Must be called with lru_lock and reservation held, this function
519 * will drop the lru lock and optionally the reservation lock before returning.
521 * @interruptible Any sleeps should occur interruptibly.
522 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
523 * @unlock_resv Unlock the reservation lock as well.
526 static int ttm_bo_cleanup_refs(struct ttm_buffer_object
*bo
,
527 bool interruptible
, bool no_wait_gpu
,
530 struct dma_resv
*resv
;
533 if (unlikely(list_empty(&bo
->ddestroy
)))
534 resv
= bo
->base
.resv
;
536 resv
= &bo
->base
._resv
;
538 if (dma_resv_test_signaled_rcu(resv
, true))
543 if (ret
&& !no_wait_gpu
) {
547 dma_resv_unlock(bo
->base
.resv
);
548 spin_unlock(&ttm_bo_glob
.lru_lock
);
550 lret
= dma_resv_wait_timeout_rcu(resv
, true,
559 spin_lock(&ttm_bo_glob
.lru_lock
);
560 if (unlock_resv
&& !dma_resv_trylock(bo
->base
.resv
)) {
562 * We raced, and lost, someone else holds the reservation now,
563 * and is probably busy in ttm_bo_cleanup_memtype_use.
565 * Even if it's not the case, because we finished waiting any
566 * delayed destruction would succeed, so just return success
569 spin_unlock(&ttm_bo_glob
.lru_lock
);
575 if (ret
|| unlikely(list_empty(&bo
->ddestroy
))) {
577 dma_resv_unlock(bo
->base
.resv
);
578 spin_unlock(&ttm_bo_glob
.lru_lock
);
582 ttm_bo_del_from_lru(bo
);
583 list_del_init(&bo
->ddestroy
);
584 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
586 spin_unlock(&ttm_bo_glob
.lru_lock
);
587 ttm_bo_cleanup_memtype_use(bo
);
590 dma_resv_unlock(bo
->base
.resv
);
596 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
597 * encountered buffers.
599 static bool ttm_bo_delayed_delete(struct ttm_bo_device
*bdev
, bool remove_all
)
601 struct ttm_bo_global
*glob
= &ttm_bo_glob
;
602 struct list_head removed
;
605 INIT_LIST_HEAD(&removed
);
607 spin_lock(&glob
->lru_lock
);
608 while (!list_empty(&bdev
->ddestroy
)) {
609 struct ttm_buffer_object
*bo
;
611 bo
= list_first_entry(&bdev
->ddestroy
, struct ttm_buffer_object
,
613 kref_get(&bo
->list_kref
);
614 list_move_tail(&bo
->ddestroy
, &removed
);
616 if (remove_all
|| bo
->base
.resv
!= &bo
->base
._resv
) {
617 spin_unlock(&glob
->lru_lock
);
618 dma_resv_lock(bo
->base
.resv
, NULL
);
620 spin_lock(&glob
->lru_lock
);
621 ttm_bo_cleanup_refs(bo
, false, !remove_all
, true);
623 } else if (dma_resv_trylock(bo
->base
.resv
)) {
624 ttm_bo_cleanup_refs(bo
, false, !remove_all
, true);
626 spin_unlock(&glob
->lru_lock
);
629 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
630 spin_lock(&glob
->lru_lock
);
632 list_splice_tail(&removed
, &bdev
->ddestroy
);
633 empty
= list_empty(&bdev
->ddestroy
);
634 spin_unlock(&glob
->lru_lock
);
639 static void ttm_bo_delayed_workqueue(struct work_struct
*work
)
641 struct ttm_bo_device
*bdev
=
642 container_of(work
, struct ttm_bo_device
, wq
.work
);
644 if (!ttm_bo_delayed_delete(bdev
, false))
645 schedule_delayed_work(&bdev
->wq
,
646 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
649 static void ttm_bo_release(struct kref
*kref
)
651 struct ttm_buffer_object
*bo
=
652 container_of(kref
, struct ttm_buffer_object
, kref
);
653 struct ttm_bo_device
*bdev
= bo
->bdev
;
654 struct ttm_mem_type_manager
*man
= &bdev
->man
[bo
->mem
.mem_type
];
656 if (bo
->bdev
->driver
->release_notify
)
657 bo
->bdev
->driver
->release_notify(bo
);
659 drm_vma_offset_remove(bdev
->vma_manager
, &bo
->base
.vma_node
);
660 ttm_mem_io_lock(man
, false);
661 ttm_mem_io_free_vm(bo
);
662 ttm_mem_io_unlock(man
);
663 ttm_bo_cleanup_refs_or_queue(bo
);
664 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
667 void ttm_bo_put(struct ttm_buffer_object
*bo
)
669 kref_put(&bo
->kref
, ttm_bo_release
);
671 EXPORT_SYMBOL(ttm_bo_put
);
673 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device
*bdev
)
675 return cancel_delayed_work_sync(&bdev
->wq
);
677 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue
);
679 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device
*bdev
, int resched
)
682 schedule_delayed_work(&bdev
->wq
,
683 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
685 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue
);
687 static int ttm_bo_evict(struct ttm_buffer_object
*bo
,
688 struct ttm_operation_ctx
*ctx
)
690 struct ttm_bo_device
*bdev
= bo
->bdev
;
691 struct ttm_mem_reg evict_mem
;
692 struct ttm_placement placement
;
695 dma_resv_assert_held(bo
->base
.resv
);
697 placement
.num_placement
= 0;
698 placement
.num_busy_placement
= 0;
699 bdev
->driver
->evict_flags(bo
, &placement
);
701 if (!placement
.num_placement
&& !placement
.num_busy_placement
) {
702 ret
= ttm_bo_pipeline_gutting(bo
);
706 return ttm_tt_create(bo
, false);
710 evict_mem
.mm_node
= NULL
;
711 evict_mem
.bus
.io_reserved_vm
= false;
712 evict_mem
.bus
.io_reserved_count
= 0;
714 ret
= ttm_bo_mem_space(bo
, &placement
, &evict_mem
, ctx
);
716 if (ret
!= -ERESTARTSYS
) {
717 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
719 ttm_bo_mem_space_debug(bo
, &placement
);
724 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true, ctx
);
726 if (ret
!= -ERESTARTSYS
)
727 pr_err("Buffer eviction failed\n");
728 ttm_bo_mem_put(bo
, &evict_mem
);
736 bool ttm_bo_eviction_valuable(struct ttm_buffer_object
*bo
,
737 const struct ttm_place
*place
)
739 /* Don't evict this BO if it's outside of the
740 * requested placement range
742 if (place
->fpfn
>= (bo
->mem
.start
+ bo
->mem
.size
) ||
743 (place
->lpfn
&& place
->lpfn
<= bo
->mem
.start
))
748 EXPORT_SYMBOL(ttm_bo_eviction_valuable
);
751 * Check the target bo is allowable to be evicted or swapout, including cases:
753 * a. if share same reservation object with ctx->resv, have assumption
754 * reservation objects should already be locked, so not lock again and
755 * return true directly when either the opreation allow_reserved_eviction
756 * or the target bo already is in delayed free list;
758 * b. Otherwise, trylock it.
760 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object
*bo
,
761 struct ttm_operation_ctx
*ctx
, bool *locked
, bool *busy
)
765 if (bo
->base
.resv
== ctx
->resv
) {
766 dma_resv_assert_held(bo
->base
.resv
);
767 if (ctx
->flags
& TTM_OPT_FLAG_ALLOW_RES_EVICT
768 || !list_empty(&bo
->ddestroy
))
774 ret
= dma_resv_trylock(bo
->base
.resv
);
784 * ttm_mem_evict_wait_busy - wait for a busy BO to become available
786 * @busy_bo: BO which couldn't be locked with trylock
787 * @ctx: operation context
788 * @ticket: acquire ticket
790 * Try to lock a busy buffer object to avoid failing eviction.
792 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object
*busy_bo
,
793 struct ttm_operation_ctx
*ctx
,
794 struct ww_acquire_ctx
*ticket
)
798 if (!busy_bo
|| !ticket
)
801 if (ctx
->interruptible
)
802 r
= dma_resv_lock_interruptible(busy_bo
->base
.resv
,
805 r
= dma_resv_lock(busy_bo
->base
.resv
, ticket
);
808 * TODO: It would be better to keep the BO locked until allocation is at
809 * least tried one more time, but that would mean a much larger rework
813 dma_resv_unlock(busy_bo
->base
.resv
);
815 return r
== -EDEADLK
? -EBUSY
: r
;
818 static int ttm_mem_evict_first(struct ttm_bo_device
*bdev
,
820 const struct ttm_place
*place
,
821 struct ttm_operation_ctx
*ctx
,
822 struct ww_acquire_ctx
*ticket
)
824 struct ttm_buffer_object
*bo
= NULL
, *busy_bo
= NULL
;
825 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
830 spin_lock(&ttm_bo_glob
.lru_lock
);
831 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
832 list_for_each_entry(bo
, &man
->lru
[i
], lru
) {
835 if (!ttm_bo_evict_swapout_allowable(bo
, ctx
, &locked
,
837 if (busy
&& !busy_bo
&& ticket
!=
838 dma_resv_locking_ctx(bo
->base
.resv
))
843 if (place
&& !bdev
->driver
->eviction_valuable(bo
,
846 dma_resv_unlock(bo
->base
.resv
);
852 /* If the inner loop terminated early, we have our candidate */
853 if (&bo
->lru
!= &man
->lru
[i
])
861 kref_get(&busy_bo
->list_kref
);
862 spin_unlock(&ttm_bo_glob
.lru_lock
);
863 ret
= ttm_mem_evict_wait_busy(busy_bo
, ctx
, ticket
);
865 kref_put(&busy_bo
->list_kref
, ttm_bo_release_list
);
869 kref_get(&bo
->list_kref
);
871 if (!list_empty(&bo
->ddestroy
)) {
872 ret
= ttm_bo_cleanup_refs(bo
, ctx
->interruptible
,
873 ctx
->no_wait_gpu
, locked
);
874 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
878 spin_unlock(&ttm_bo_glob
.lru_lock
);
880 ret
= ttm_bo_evict(bo
, ctx
);
882 ttm_bo_unreserve(bo
);
884 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
888 void ttm_bo_mem_put(struct ttm_buffer_object
*bo
, struct ttm_mem_reg
*mem
)
890 struct ttm_mem_type_manager
*man
= &bo
->bdev
->man
[mem
->mem_type
];
893 (*man
->func
->put_node
)(man
, mem
);
895 EXPORT_SYMBOL(ttm_bo_mem_put
);
898 * Add the last move fence to the BO and reserve a new shared slot.
900 static int ttm_bo_add_move_fence(struct ttm_buffer_object
*bo
,
901 struct ttm_mem_type_manager
*man
,
902 struct ttm_mem_reg
*mem
,
905 struct dma_fence
*fence
;
908 spin_lock(&man
->move_lock
);
909 fence
= dma_fence_get(man
->move
);
910 spin_unlock(&man
->move_lock
);
918 dma_resv_add_shared_fence(bo
->base
.resv
, fence
);
920 ret
= dma_resv_reserve_shared(bo
->base
.resv
, 1);
922 dma_fence_put(fence
);
926 dma_fence_put(bo
->moving
);
932 * Repeatedly evict memory from the LRU for @mem_type until we create enough
933 * space, or we've evicted everything and there isn't enough space.
935 static int ttm_bo_mem_force_space(struct ttm_buffer_object
*bo
,
936 const struct ttm_place
*place
,
937 struct ttm_mem_reg
*mem
,
938 struct ttm_operation_ctx
*ctx
)
940 struct ttm_bo_device
*bdev
= bo
->bdev
;
941 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem
->mem_type
];
942 struct ww_acquire_ctx
*ticket
;
945 ticket
= dma_resv_locking_ctx(bo
->base
.resv
);
947 ret
= (*man
->func
->get_node
)(man
, bo
, place
, mem
);
948 if (unlikely(ret
!= 0))
952 ret
= ttm_mem_evict_first(bdev
, mem
->mem_type
, place
, ctx
,
954 if (unlikely(ret
!= 0))
958 return ttm_bo_add_move_fence(bo
, man
, mem
, ctx
->no_wait_gpu
);
961 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager
*man
,
962 uint32_t cur_placement
,
963 uint32_t proposed_placement
)
965 uint32_t caching
= proposed_placement
& TTM_PL_MASK_CACHING
;
966 uint32_t result
= proposed_placement
& ~TTM_PL_MASK_CACHING
;
969 * Keep current caching if possible.
972 if ((cur_placement
& caching
) != 0)
973 result
|= (cur_placement
& caching
);
974 else if ((man
->default_caching
& caching
) != 0)
975 result
|= man
->default_caching
;
976 else if ((TTM_PL_FLAG_CACHED
& caching
) != 0)
977 result
|= TTM_PL_FLAG_CACHED
;
978 else if ((TTM_PL_FLAG_WC
& caching
) != 0)
979 result
|= TTM_PL_FLAG_WC
;
980 else if ((TTM_PL_FLAG_UNCACHED
& caching
) != 0)
981 result
|= TTM_PL_FLAG_UNCACHED
;
986 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager
*man
,
988 const struct ttm_place
*place
,
989 uint32_t *masked_placement
)
991 uint32_t cur_flags
= ttm_bo_type_flags(mem_type
);
993 if ((cur_flags
& place
->flags
& TTM_PL_MASK_MEM
) == 0)
996 if ((place
->flags
& man
->available_caching
) == 0)
999 cur_flags
|= (place
->flags
& man
->available_caching
);
1001 *masked_placement
= cur_flags
;
1006 * ttm_bo_mem_placement - check if placement is compatible
1007 * @bo: BO to find memory for
1008 * @place: where to search
1009 * @mem: the memory object to fill in
1010 * @ctx: operation context
1012 * Check if placement is compatible and fill in mem structure.
1013 * Returns -EBUSY if placement won't work or negative error code.
1014 * 0 when placement can be used.
1016 static int ttm_bo_mem_placement(struct ttm_buffer_object
*bo
,
1017 const struct ttm_place
*place
,
1018 struct ttm_mem_reg
*mem
,
1019 struct ttm_operation_ctx
*ctx
)
1021 struct ttm_bo_device
*bdev
= bo
->bdev
;
1022 uint32_t mem_type
= TTM_PL_SYSTEM
;
1023 struct ttm_mem_type_manager
*man
;
1024 uint32_t cur_flags
= 0;
1027 ret
= ttm_mem_type_from_place(place
, &mem_type
);
1031 man
= &bdev
->man
[mem_type
];
1032 if (!man
->has_type
|| !man
->use_type
)
1035 if (!ttm_bo_mt_compatible(man
, mem_type
, place
, &cur_flags
))
1038 cur_flags
= ttm_bo_select_caching(man
, bo
->mem
.placement
, cur_flags
);
1040 * Use the access and other non-mapping-related flag bits from
1041 * the memory placement flags to the current flags
1043 ttm_flag_masked(&cur_flags
, place
->flags
, ~TTM_PL_MASK_MEMTYPE
);
1045 mem
->mem_type
= mem_type
;
1046 mem
->placement
= cur_flags
;
1048 spin_lock(&ttm_bo_glob
.lru_lock
);
1049 ttm_bo_del_from_lru(bo
);
1050 ttm_bo_add_mem_to_lru(bo
, mem
);
1051 spin_unlock(&ttm_bo_glob
.lru_lock
);
1057 * Creates space for memory region @mem according to its type.
1059 * This function first searches for free space in compatible memory types in
1060 * the priority order defined by the driver. If free space isn't found, then
1061 * ttm_bo_mem_force_space is attempted in priority order to evict and find
1064 int ttm_bo_mem_space(struct ttm_buffer_object
*bo
,
1065 struct ttm_placement
*placement
,
1066 struct ttm_mem_reg
*mem
,
1067 struct ttm_operation_ctx
*ctx
)
1069 struct ttm_bo_device
*bdev
= bo
->bdev
;
1070 bool type_found
= false;
1073 ret
= dma_resv_reserve_shared(bo
->base
.resv
, 1);
1077 mem
->mm_node
= NULL
;
1078 for (i
= 0; i
< placement
->num_placement
; ++i
) {
1079 const struct ttm_place
*place
= &placement
->placement
[i
];
1080 struct ttm_mem_type_manager
*man
;
1082 ret
= ttm_bo_mem_placement(bo
, place
, mem
, ctx
);
1089 mem
->mm_node
= NULL
;
1090 if (mem
->mem_type
== TTM_PL_SYSTEM
)
1093 man
= &bdev
->man
[mem
->mem_type
];
1094 ret
= (*man
->func
->get_node
)(man
, bo
, place
, mem
);
1101 ret
= ttm_bo_add_move_fence(bo
, man
, mem
, ctx
->no_wait_gpu
);
1102 if (unlikely(ret
)) {
1103 (*man
->func
->put_node
)(man
, mem
);
1112 for (i
= 0; i
< placement
->num_busy_placement
; ++i
) {
1113 const struct ttm_place
*place
= &placement
->busy_placement
[i
];
1115 ret
= ttm_bo_mem_placement(bo
, place
, mem
, ctx
);
1122 mem
->mm_node
= NULL
;
1123 if (mem
->mem_type
== TTM_PL_SYSTEM
)
1126 ret
= ttm_bo_mem_force_space(bo
, place
, mem
, ctx
);
1127 if (ret
== 0 && mem
->mm_node
)
1130 if (ret
&& ret
!= -EBUSY
)
1136 pr_err(TTM_PFX
"No compatible memory type found\n");
1141 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
&& !list_empty(&bo
->lru
)) {
1142 spin_lock(&ttm_bo_glob
.lru_lock
);
1143 ttm_bo_move_to_lru_tail(bo
, NULL
);
1144 spin_unlock(&ttm_bo_glob
.lru_lock
);
1149 EXPORT_SYMBOL(ttm_bo_mem_space
);
1151 static int ttm_bo_move_buffer(struct ttm_buffer_object
*bo
,
1152 struct ttm_placement
*placement
,
1153 struct ttm_operation_ctx
*ctx
)
1156 struct ttm_mem_reg mem
;
1158 dma_resv_assert_held(bo
->base
.resv
);
1160 mem
.num_pages
= bo
->num_pages
;
1161 mem
.size
= mem
.num_pages
<< PAGE_SHIFT
;
1162 mem
.page_alignment
= bo
->mem
.page_alignment
;
1163 mem
.bus
.io_reserved_vm
= false;
1164 mem
.bus
.io_reserved_count
= 0;
1166 * Determine where to move the buffer.
1168 ret
= ttm_bo_mem_space(bo
, placement
, &mem
, ctx
);
1171 ret
= ttm_bo_handle_move_mem(bo
, &mem
, false, ctx
);
1173 if (ret
&& mem
.mm_node
)
1174 ttm_bo_mem_put(bo
, &mem
);
1178 static bool ttm_bo_places_compat(const struct ttm_place
*places
,
1179 unsigned num_placement
,
1180 struct ttm_mem_reg
*mem
,
1181 uint32_t *new_flags
)
1185 for (i
= 0; i
< num_placement
; i
++) {
1186 const struct ttm_place
*heap
= &places
[i
];
1188 if (mem
->mm_node
&& (mem
->start
< heap
->fpfn
||
1189 (heap
->lpfn
!= 0 && (mem
->start
+ mem
->num_pages
) > heap
->lpfn
)))
1192 *new_flags
= heap
->flags
;
1193 if ((*new_flags
& mem
->placement
& TTM_PL_MASK_CACHING
) &&
1194 (*new_flags
& mem
->placement
& TTM_PL_MASK_MEM
) &&
1195 (!(*new_flags
& TTM_PL_FLAG_CONTIGUOUS
) ||
1196 (mem
->placement
& TTM_PL_FLAG_CONTIGUOUS
)))
1202 bool ttm_bo_mem_compat(struct ttm_placement
*placement
,
1203 struct ttm_mem_reg
*mem
,
1204 uint32_t *new_flags
)
1206 if (ttm_bo_places_compat(placement
->placement
, placement
->num_placement
,
1210 if ((placement
->busy_placement
!= placement
->placement
||
1211 placement
->num_busy_placement
> placement
->num_placement
) &&
1212 ttm_bo_places_compat(placement
->busy_placement
,
1213 placement
->num_busy_placement
,
1219 EXPORT_SYMBOL(ttm_bo_mem_compat
);
1221 int ttm_bo_validate(struct ttm_buffer_object
*bo
,
1222 struct ttm_placement
*placement
,
1223 struct ttm_operation_ctx
*ctx
)
1228 dma_resv_assert_held(bo
->base
.resv
);
1230 * Check whether we need to move buffer.
1232 if (!ttm_bo_mem_compat(placement
, &bo
->mem
, &new_flags
)) {
1233 ret
= ttm_bo_move_buffer(bo
, placement
, ctx
);
1238 * Use the access and other non-mapping-related flag bits from
1239 * the compatible memory placement flags to the active flags
1241 ttm_flag_masked(&bo
->mem
.placement
, new_flags
,
1242 ~TTM_PL_MASK_MEMTYPE
);
1245 * We might need to add a TTM.
1247 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
&& bo
->ttm
== NULL
) {
1248 ret
= ttm_tt_create(bo
, true);
1254 EXPORT_SYMBOL(ttm_bo_validate
);
1256 int ttm_bo_init_reserved(struct ttm_bo_device
*bdev
,
1257 struct ttm_buffer_object
*bo
,
1259 enum ttm_bo_type type
,
1260 struct ttm_placement
*placement
,
1261 uint32_t page_alignment
,
1262 struct ttm_operation_ctx
*ctx
,
1264 struct sg_table
*sg
,
1265 struct dma_resv
*resv
,
1266 void (*destroy
) (struct ttm_buffer_object
*))
1268 struct ttm_mem_global
*mem_glob
= &ttm_mem_glob
;
1270 unsigned long num_pages
;
1273 ret
= ttm_mem_global_alloc(mem_glob
, acc_size
, ctx
);
1275 pr_err("Out of kernel memory\n");
1283 num_pages
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1284 if (num_pages
== 0) {
1285 pr_err("Illegal buffer object size\n");
1290 ttm_mem_global_free(mem_glob
, acc_size
);
1293 bo
->destroy
= destroy
? destroy
: ttm_bo_default_destroy
;
1295 kref_init(&bo
->kref
);
1296 kref_init(&bo
->list_kref
);
1297 INIT_LIST_HEAD(&bo
->lru
);
1298 INIT_LIST_HEAD(&bo
->ddestroy
);
1299 INIT_LIST_HEAD(&bo
->swap
);
1300 INIT_LIST_HEAD(&bo
->io_reserve_lru
);
1303 bo
->num_pages
= num_pages
;
1304 bo
->mem
.size
= num_pages
<< PAGE_SHIFT
;
1305 bo
->mem
.mem_type
= TTM_PL_SYSTEM
;
1306 bo
->mem
.num_pages
= bo
->num_pages
;
1307 bo
->mem
.mm_node
= NULL
;
1308 bo
->mem
.page_alignment
= page_alignment
;
1309 bo
->mem
.bus
.io_reserved_vm
= false;
1310 bo
->mem
.bus
.io_reserved_count
= 0;
1312 bo
->mem
.placement
= (TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
);
1313 bo
->acc_size
= acc_size
;
1316 bo
->base
.resv
= resv
;
1317 dma_resv_assert_held(bo
->base
.resv
);
1319 bo
->base
.resv
= &bo
->base
._resv
;
1321 if (!ttm_bo_uses_embedded_gem_object(bo
)) {
1323 * bo.gem is not initialized, so we have to setup the
1324 * struct elements we want use regardless.
1326 dma_resv_init(&bo
->base
._resv
);
1327 drm_vma_node_reset(&bo
->base
.vma_node
);
1329 atomic_inc(&ttm_bo_glob
.bo_count
);
1332 * For ttm_bo_type_device buffers, allocate
1333 * address space from the device.
1335 if (bo
->type
== ttm_bo_type_device
||
1336 bo
->type
== ttm_bo_type_sg
)
1337 ret
= drm_vma_offset_add(bdev
->vma_manager
, &bo
->base
.vma_node
,
1340 /* passed reservation objects should already be locked,
1341 * since otherwise lockdep will be angered in radeon.
1344 locked
= dma_resv_trylock(bo
->base
.resv
);
1349 ret
= ttm_bo_validate(bo
, placement
, ctx
);
1351 if (unlikely(ret
)) {
1353 ttm_bo_unreserve(bo
);
1359 spin_lock(&ttm_bo_glob
.lru_lock
);
1360 ttm_bo_move_to_lru_tail(bo
, NULL
);
1361 spin_unlock(&ttm_bo_glob
.lru_lock
);
1365 EXPORT_SYMBOL(ttm_bo_init_reserved
);
1367 int ttm_bo_init(struct ttm_bo_device
*bdev
,
1368 struct ttm_buffer_object
*bo
,
1370 enum ttm_bo_type type
,
1371 struct ttm_placement
*placement
,
1372 uint32_t page_alignment
,
1375 struct sg_table
*sg
,
1376 struct dma_resv
*resv
,
1377 void (*destroy
) (struct ttm_buffer_object
*))
1379 struct ttm_operation_ctx ctx
= { interruptible
, false };
1382 ret
= ttm_bo_init_reserved(bdev
, bo
, size
, type
, placement
,
1383 page_alignment
, &ctx
, acc_size
,
1389 ttm_bo_unreserve(bo
);
1393 EXPORT_SYMBOL(ttm_bo_init
);
1395 size_t ttm_bo_acc_size(struct ttm_bo_device
*bdev
,
1396 unsigned long bo_size
,
1397 unsigned struct_size
)
1399 unsigned npages
= (PAGE_ALIGN(bo_size
)) >> PAGE_SHIFT
;
1402 size
+= ttm_round_pot(struct_size
);
1403 size
+= ttm_round_pot(npages
* sizeof(void *));
1404 size
+= ttm_round_pot(sizeof(struct ttm_tt
));
1407 EXPORT_SYMBOL(ttm_bo_acc_size
);
1409 size_t ttm_bo_dma_acc_size(struct ttm_bo_device
*bdev
,
1410 unsigned long bo_size
,
1411 unsigned struct_size
)
1413 unsigned npages
= (PAGE_ALIGN(bo_size
)) >> PAGE_SHIFT
;
1416 size
+= ttm_round_pot(struct_size
);
1417 size
+= ttm_round_pot(npages
* (2*sizeof(void *) + sizeof(dma_addr_t
)));
1418 size
+= ttm_round_pot(sizeof(struct ttm_dma_tt
));
1421 EXPORT_SYMBOL(ttm_bo_dma_acc_size
);
1423 int ttm_bo_create(struct ttm_bo_device
*bdev
,
1425 enum ttm_bo_type type
,
1426 struct ttm_placement
*placement
,
1427 uint32_t page_alignment
,
1429 struct ttm_buffer_object
**p_bo
)
1431 struct ttm_buffer_object
*bo
;
1435 bo
= kzalloc(sizeof(*bo
), GFP_KERNEL
);
1436 if (unlikely(bo
== NULL
))
1439 acc_size
= ttm_bo_acc_size(bdev
, size
, sizeof(struct ttm_buffer_object
));
1440 ret
= ttm_bo_init(bdev
, bo
, size
, type
, placement
, page_alignment
,
1441 interruptible
, acc_size
,
1443 if (likely(ret
== 0))
1448 EXPORT_SYMBOL(ttm_bo_create
);
1450 static int ttm_bo_force_list_clean(struct ttm_bo_device
*bdev
,
1453 struct ttm_operation_ctx ctx
= {
1454 .interruptible
= false,
1455 .no_wait_gpu
= false,
1456 .flags
= TTM_OPT_FLAG_FORCE_ALLOC
1458 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1459 struct ttm_bo_global
*glob
= &ttm_bo_glob
;
1460 struct dma_fence
*fence
;
1465 * Can't use standard list traversal since we're unlocking.
1468 spin_lock(&glob
->lru_lock
);
1469 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
1470 while (!list_empty(&man
->lru
[i
])) {
1471 spin_unlock(&glob
->lru_lock
);
1472 ret
= ttm_mem_evict_first(bdev
, mem_type
, NULL
, &ctx
,
1476 spin_lock(&glob
->lru_lock
);
1479 spin_unlock(&glob
->lru_lock
);
1481 spin_lock(&man
->move_lock
);
1482 fence
= dma_fence_get(man
->move
);
1483 spin_unlock(&man
->move_lock
);
1486 ret
= dma_fence_wait(fence
, false);
1487 dma_fence_put(fence
);
1495 int ttm_bo_clean_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1497 struct ttm_mem_type_manager
*man
;
1500 if (mem_type
>= TTM_NUM_MEM_TYPES
) {
1501 pr_err("Illegal memory type %d\n", mem_type
);
1504 man
= &bdev
->man
[mem_type
];
1506 if (!man
->has_type
) {
1507 pr_err("Trying to take down uninitialized memory manager type %u\n",
1512 man
->use_type
= false;
1513 man
->has_type
= false;
1517 ret
= ttm_bo_force_list_clean(bdev
, mem_type
);
1519 pr_err("Cleanup eviction failed\n");
1523 ret
= (*man
->func
->takedown
)(man
);
1526 dma_fence_put(man
->move
);
1531 EXPORT_SYMBOL(ttm_bo_clean_mm
);
1533 int ttm_bo_evict_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1535 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1537 if (mem_type
== 0 || mem_type
>= TTM_NUM_MEM_TYPES
) {
1538 pr_err("Illegal memory manager memory type %u\n", mem_type
);
1542 if (!man
->has_type
) {
1543 pr_err("Memory type %u has not been initialized\n", mem_type
);
1547 return ttm_bo_force_list_clean(bdev
, mem_type
);
1549 EXPORT_SYMBOL(ttm_bo_evict_mm
);
1551 int ttm_bo_init_mm(struct ttm_bo_device
*bdev
, unsigned type
,
1552 unsigned long p_size
)
1555 struct ttm_mem_type_manager
*man
;
1558 BUG_ON(type
>= TTM_NUM_MEM_TYPES
);
1559 man
= &bdev
->man
[type
];
1560 BUG_ON(man
->has_type
);
1561 man
->io_reserve_fastpath
= true;
1562 man
->use_io_reserve_lru
= false;
1563 mutex_init(&man
->io_reserve_mutex
);
1564 spin_lock_init(&man
->move_lock
);
1565 INIT_LIST_HEAD(&man
->io_reserve_lru
);
1567 ret
= bdev
->driver
->init_mem_type(bdev
, type
, man
);
1572 if (type
!= TTM_PL_SYSTEM
) {
1573 ret
= (*man
->func
->init
)(man
, p_size
);
1577 man
->has_type
= true;
1578 man
->use_type
= true;
1581 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
)
1582 INIT_LIST_HEAD(&man
->lru
[i
]);
1587 EXPORT_SYMBOL(ttm_bo_init_mm
);
1589 static void ttm_bo_global_kobj_release(struct kobject
*kobj
)
1591 struct ttm_bo_global
*glob
=
1592 container_of(kobj
, struct ttm_bo_global
, kobj
);
1594 __free_page(glob
->dummy_read_page
);
1597 static void ttm_bo_global_release(void)
1599 struct ttm_bo_global
*glob
= &ttm_bo_glob
;
1601 mutex_lock(&ttm_global_mutex
);
1602 if (--ttm_bo_glob_use_count
> 0)
1605 kobject_del(&glob
->kobj
);
1606 kobject_put(&glob
->kobj
);
1607 ttm_mem_global_release(&ttm_mem_glob
);
1608 memset(glob
, 0, sizeof(*glob
));
1610 mutex_unlock(&ttm_global_mutex
);
1613 static int ttm_bo_global_init(void)
1615 struct ttm_bo_global
*glob
= &ttm_bo_glob
;
1619 mutex_lock(&ttm_global_mutex
);
1620 if (++ttm_bo_glob_use_count
> 1)
1623 ret
= ttm_mem_global_init(&ttm_mem_glob
);
1627 spin_lock_init(&glob
->lru_lock
);
1628 glob
->dummy_read_page
= alloc_page(__GFP_ZERO
| GFP_DMA32
);
1630 if (unlikely(glob
->dummy_read_page
== NULL
)) {
1635 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
)
1636 INIT_LIST_HEAD(&glob
->swap_lru
[i
]);
1637 INIT_LIST_HEAD(&glob
->device_list
);
1638 atomic_set(&glob
->bo_count
, 0);
1640 ret
= kobject_init_and_add(
1641 &glob
->kobj
, &ttm_bo_glob_kobj_type
, ttm_get_kobj(), "buffer_objects");
1642 if (unlikely(ret
!= 0))
1643 kobject_put(&glob
->kobj
);
1645 mutex_unlock(&ttm_global_mutex
);
1649 int ttm_bo_device_release(struct ttm_bo_device
*bdev
)
1651 struct ttm_bo_global
*glob
= &ttm_bo_glob
;
1653 unsigned i
= TTM_NUM_MEM_TYPES
;
1654 struct ttm_mem_type_manager
*man
;
1657 man
= &bdev
->man
[i
];
1658 if (man
->has_type
) {
1659 man
->use_type
= false;
1660 if ((i
!= TTM_PL_SYSTEM
) && ttm_bo_clean_mm(bdev
, i
)) {
1662 pr_err("DRM memory manager type %d is not clean\n",
1665 man
->has_type
= false;
1669 mutex_lock(&ttm_global_mutex
);
1670 list_del(&bdev
->device_list
);
1671 mutex_unlock(&ttm_global_mutex
);
1673 cancel_delayed_work_sync(&bdev
->wq
);
1675 if (ttm_bo_delayed_delete(bdev
, true))
1676 pr_debug("Delayed destroy list was clean\n");
1678 spin_lock(&glob
->lru_lock
);
1679 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
)
1680 if (list_empty(&bdev
->man
[0].lru
[0]))
1681 pr_debug("Swap list %d was clean\n", i
);
1682 spin_unlock(&glob
->lru_lock
);
1685 ttm_bo_global_release();
1689 EXPORT_SYMBOL(ttm_bo_device_release
);
1691 int ttm_bo_device_init(struct ttm_bo_device
*bdev
,
1692 struct ttm_bo_driver
*driver
,
1693 struct address_space
*mapping
,
1694 struct drm_vma_offset_manager
*vma_manager
,
1697 struct ttm_bo_global
*glob
= &ttm_bo_glob
;
1700 if (WARN_ON(vma_manager
== NULL
))
1703 ret
= ttm_bo_global_init();
1707 bdev
->driver
= driver
;
1709 memset(bdev
->man
, 0, sizeof(bdev
->man
));
1712 * Initialize the system memory buffer type.
1713 * Other types need to be driver / IOCTL initialized.
1715 ret
= ttm_bo_init_mm(bdev
, TTM_PL_SYSTEM
, 0);
1716 if (unlikely(ret
!= 0))
1719 bdev
->vma_manager
= vma_manager
;
1720 INIT_DELAYED_WORK(&bdev
->wq
, ttm_bo_delayed_workqueue
);
1721 INIT_LIST_HEAD(&bdev
->ddestroy
);
1722 bdev
->dev_mapping
= mapping
;
1723 bdev
->need_dma32
= need_dma32
;
1724 mutex_lock(&ttm_global_mutex
);
1725 list_add_tail(&bdev
->device_list
, &glob
->device_list
);
1726 mutex_unlock(&ttm_global_mutex
);
1730 ttm_bo_global_release();
1733 EXPORT_SYMBOL(ttm_bo_device_init
);
1736 * buffer object vm functions.
1739 bool ttm_mem_reg_is_pci(struct ttm_bo_device
*bdev
, struct ttm_mem_reg
*mem
)
1741 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem
->mem_type
];
1743 if (!(man
->flags
& TTM_MEMTYPE_FLAG_FIXED
)) {
1744 if (mem
->mem_type
== TTM_PL_SYSTEM
)
1747 if (man
->flags
& TTM_MEMTYPE_FLAG_CMA
)
1750 if (mem
->placement
& TTM_PL_FLAG_CACHED
)
1756 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object
*bo
)
1758 struct ttm_bo_device
*bdev
= bo
->bdev
;
1760 drm_vma_node_unmap(&bo
->base
.vma_node
, bdev
->dev_mapping
);
1761 ttm_mem_io_free_vm(bo
);
1764 void ttm_bo_unmap_virtual(struct ttm_buffer_object
*bo
)
1766 struct ttm_bo_device
*bdev
= bo
->bdev
;
1767 struct ttm_mem_type_manager
*man
= &bdev
->man
[bo
->mem
.mem_type
];
1769 ttm_mem_io_lock(man
, false);
1770 ttm_bo_unmap_virtual_locked(bo
);
1771 ttm_mem_io_unlock(man
);
1775 EXPORT_SYMBOL(ttm_bo_unmap_virtual
);
1777 int ttm_bo_wait(struct ttm_buffer_object
*bo
,
1778 bool interruptible
, bool no_wait
)
1780 long timeout
= 15 * HZ
;
1783 if (dma_resv_test_signaled_rcu(bo
->base
.resv
, true))
1789 timeout
= dma_resv_wait_timeout_rcu(bo
->base
.resv
, true,
1790 interruptible
, timeout
);
1797 dma_resv_add_excl_fence(bo
->base
.resv
, NULL
);
1800 EXPORT_SYMBOL(ttm_bo_wait
);
1803 * A buffer object shrink method that tries to swap out the first
1804 * buffer object on the bo_global::swap_lru list.
1806 int ttm_bo_swapout(struct ttm_bo_global
*glob
, struct ttm_operation_ctx
*ctx
)
1808 struct ttm_buffer_object
*bo
;
1813 spin_lock(&glob
->lru_lock
);
1814 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
1815 list_for_each_entry(bo
, &glob
->swap_lru
[i
], swap
) {
1816 if (ttm_bo_evict_swapout_allowable(bo
, ctx
, &locked
,
1827 spin_unlock(&glob
->lru_lock
);
1831 kref_get(&bo
->list_kref
);
1833 if (!list_empty(&bo
->ddestroy
)) {
1834 ret
= ttm_bo_cleanup_refs(bo
, false, false, locked
);
1835 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1839 ttm_bo_del_from_lru(bo
);
1840 spin_unlock(&glob
->lru_lock
);
1843 * Move to system cached
1846 if (bo
->mem
.mem_type
!= TTM_PL_SYSTEM
||
1847 bo
->ttm
->caching_state
!= tt_cached
) {
1848 struct ttm_operation_ctx ctx
= { false, false };
1849 struct ttm_mem_reg evict_mem
;
1851 evict_mem
= bo
->mem
;
1852 evict_mem
.mm_node
= NULL
;
1853 evict_mem
.placement
= TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
;
1854 evict_mem
.mem_type
= TTM_PL_SYSTEM
;
1856 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true, &ctx
);
1857 if (unlikely(ret
!= 0))
1862 * Make sure BO is idle.
1865 ret
= ttm_bo_wait(bo
, false, false);
1866 if (unlikely(ret
!= 0))
1869 ttm_bo_unmap_virtual(bo
);
1872 * Swap out. Buffer will be swapped in again as soon as
1873 * anyone tries to access a ttm page.
1876 if (bo
->bdev
->driver
->swap_notify
)
1877 bo
->bdev
->driver
->swap_notify(bo
);
1879 ret
= ttm_tt_swapout(bo
->ttm
, bo
->persistent_swap_storage
);
1884 * Unreserve without putting on LRU to avoid swapping out an
1885 * already swapped buffer.
1888 dma_resv_unlock(bo
->base
.resv
);
1889 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1892 EXPORT_SYMBOL(ttm_bo_swapout
);
1894 void ttm_bo_swapout_all(struct ttm_bo_device
*bdev
)
1896 struct ttm_operation_ctx ctx
= {
1897 .interruptible
= false,
1898 .no_wait_gpu
= false
1901 while (ttm_bo_swapout(&ttm_bo_glob
, &ctx
) == 0);
1903 EXPORT_SYMBOL(ttm_bo_swapout_all
);