1 /**************************************************************************
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
31 #define pr_fmt(fmt) "[TTM] " fmt
33 #include <drm/ttm/ttm_module.h>
34 #include <drm/ttm/ttm_bo_driver.h>
35 #include <drm/ttm/ttm_placement.h>
36 #include <linux/jiffies.h>
37 #include <linux/slab.h>
38 #include <linux/sched.h>
40 #include <linux/file.h>
41 #include <linux/module.h>
42 #include <linux/atomic.h>
43 #include <linux/reservation.h>
45 static void ttm_bo_global_kobj_release(struct kobject
*kobj
);
47 static struct attribute ttm_bo_count
= {
52 static inline int ttm_mem_type_from_place(const struct ttm_place
*place
,
57 pos
= ffs(place
->flags
& TTM_PL_MASK_MEM
);
65 static void ttm_mem_type_debug(struct ttm_bo_device
*bdev
, int mem_type
)
67 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
68 struct drm_printer p
= drm_debug_printer(TTM_PFX
);
70 pr_err(" has_type: %d\n", man
->has_type
);
71 pr_err(" use_type: %d\n", man
->use_type
);
72 pr_err(" flags: 0x%08X\n", man
->flags
);
73 pr_err(" gpu_offset: 0x%08llX\n", man
->gpu_offset
);
74 pr_err(" size: %llu\n", man
->size
);
75 pr_err(" available_caching: 0x%08X\n", man
->available_caching
);
76 pr_err(" default_caching: 0x%08X\n", man
->default_caching
);
77 if (mem_type
!= TTM_PL_SYSTEM
)
78 (*man
->func
->debug
)(man
, &p
);
81 static void ttm_bo_mem_space_debug(struct ttm_buffer_object
*bo
,
82 struct ttm_placement
*placement
)
86 pr_err("No space for %p (%lu pages, %luK, %luM)\n",
87 bo
, bo
->mem
.num_pages
, bo
->mem
.size
>> 10,
89 for (i
= 0; i
< placement
->num_placement
; i
++) {
90 ret
= ttm_mem_type_from_place(&placement
->placement
[i
],
94 pr_err(" placement[%d]=0x%08X (%d)\n",
95 i
, placement
->placement
[i
].flags
, mem_type
);
96 ttm_mem_type_debug(bo
->bdev
, mem_type
);
100 static ssize_t
ttm_bo_global_show(struct kobject
*kobj
,
101 struct attribute
*attr
,
104 struct ttm_bo_global
*glob
=
105 container_of(kobj
, struct ttm_bo_global
, kobj
);
107 return snprintf(buffer
, PAGE_SIZE
, "%d\n",
108 atomic_read(&glob
->bo_count
));
111 static struct attribute
*ttm_bo_global_attrs
[] = {
116 static const struct sysfs_ops ttm_bo_global_ops
= {
117 .show
= &ttm_bo_global_show
120 static struct kobj_type ttm_bo_glob_kobj_type
= {
121 .release
= &ttm_bo_global_kobj_release
,
122 .sysfs_ops
= &ttm_bo_global_ops
,
123 .default_attrs
= ttm_bo_global_attrs
127 static inline uint32_t ttm_bo_type_flags(unsigned type
)
132 static void ttm_bo_release_list(struct kref
*list_kref
)
134 struct ttm_buffer_object
*bo
=
135 container_of(list_kref
, struct ttm_buffer_object
, list_kref
);
136 struct ttm_bo_device
*bdev
= bo
->bdev
;
137 size_t acc_size
= bo
->acc_size
;
139 BUG_ON(kref_read(&bo
->list_kref
));
140 BUG_ON(kref_read(&bo
->kref
));
141 BUG_ON(atomic_read(&bo
->cpu_writers
));
142 BUG_ON(bo
->mem
.mm_node
!= NULL
);
143 BUG_ON(!list_empty(&bo
->lru
));
144 BUG_ON(!list_empty(&bo
->ddestroy
));
145 ttm_tt_destroy(bo
->ttm
);
146 atomic_dec(&bo
->glob
->bo_count
);
147 dma_fence_put(bo
->moving
);
148 reservation_object_fini(&bo
->ttm_resv
);
149 mutex_destroy(&bo
->wu_mutex
);
155 ttm_mem_global_free(bdev
->glob
->mem_glob
, acc_size
);
158 void ttm_bo_add_to_lru(struct ttm_buffer_object
*bo
)
160 struct ttm_bo_device
*bdev
= bo
->bdev
;
161 struct ttm_mem_type_manager
*man
;
163 reservation_object_assert_held(bo
->resv
);
165 if (!(bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
)) {
167 BUG_ON(!list_empty(&bo
->lru
));
169 man
= &bdev
->man
[bo
->mem
.mem_type
];
170 list_add_tail(&bo
->lru
, &man
->lru
[bo
->priority
]);
171 kref_get(&bo
->list_kref
);
173 if (bo
->ttm
&& !(bo
->ttm
->page_flags
&
174 (TTM_PAGE_FLAG_SG
| TTM_PAGE_FLAG_SWAPPED
))) {
175 list_add_tail(&bo
->swap
,
176 &bo
->glob
->swap_lru
[bo
->priority
]);
177 kref_get(&bo
->list_kref
);
181 EXPORT_SYMBOL(ttm_bo_add_to_lru
);
183 static void ttm_bo_ref_bug(struct kref
*list_kref
)
188 void ttm_bo_del_from_lru(struct ttm_buffer_object
*bo
)
190 if (!list_empty(&bo
->swap
)) {
191 list_del_init(&bo
->swap
);
192 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
194 if (!list_empty(&bo
->lru
)) {
195 list_del_init(&bo
->lru
);
196 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
200 * TODO: Add a driver hook to delete from
201 * driver-specific LRU's here.
205 void ttm_bo_del_sub_from_lru(struct ttm_buffer_object
*bo
)
207 spin_lock(&bo
->glob
->lru_lock
);
208 ttm_bo_del_from_lru(bo
);
209 spin_unlock(&bo
->glob
->lru_lock
);
211 EXPORT_SYMBOL(ttm_bo_del_sub_from_lru
);
213 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object
*bo
)
215 reservation_object_assert_held(bo
->resv
);
217 ttm_bo_del_from_lru(bo
);
218 ttm_bo_add_to_lru(bo
);
220 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail
);
223 * Call bo->mutex locked.
225 static int ttm_bo_add_ttm(struct ttm_buffer_object
*bo
, bool zero_alloc
)
227 struct ttm_bo_device
*bdev
= bo
->bdev
;
228 struct ttm_bo_global
*glob
= bo
->glob
;
230 uint32_t page_flags
= 0;
232 reservation_object_assert_held(bo
->resv
);
235 if (bdev
->need_dma32
)
236 page_flags
|= TTM_PAGE_FLAG_DMA32
;
239 case ttm_bo_type_device
:
241 page_flags
|= TTM_PAGE_FLAG_ZERO_ALLOC
;
242 case ttm_bo_type_kernel
:
243 bo
->ttm
= bdev
->driver
->ttm_tt_create(bdev
, bo
->num_pages
<< PAGE_SHIFT
,
244 page_flags
, glob
->dummy_read_page
);
245 if (unlikely(bo
->ttm
== NULL
))
249 bo
->ttm
= bdev
->driver
->ttm_tt_create(bdev
, bo
->num_pages
<< PAGE_SHIFT
,
250 page_flags
| TTM_PAGE_FLAG_SG
,
251 glob
->dummy_read_page
);
252 if (unlikely(bo
->ttm
== NULL
)) {
256 bo
->ttm
->sg
= bo
->sg
;
259 pr_err("Illegal buffer object type\n");
267 static int ttm_bo_handle_move_mem(struct ttm_buffer_object
*bo
,
268 struct ttm_mem_reg
*mem
, bool evict
,
269 struct ttm_operation_ctx
*ctx
)
271 struct ttm_bo_device
*bdev
= bo
->bdev
;
272 bool old_is_pci
= ttm_mem_reg_is_pci(bdev
, &bo
->mem
);
273 bool new_is_pci
= ttm_mem_reg_is_pci(bdev
, mem
);
274 struct ttm_mem_type_manager
*old_man
= &bdev
->man
[bo
->mem
.mem_type
];
275 struct ttm_mem_type_manager
*new_man
= &bdev
->man
[mem
->mem_type
];
278 if (old_is_pci
|| new_is_pci
||
279 ((mem
->placement
& bo
->mem
.placement
& TTM_PL_MASK_CACHING
) == 0)) {
280 ret
= ttm_mem_io_lock(old_man
, true);
281 if (unlikely(ret
!= 0))
283 ttm_bo_unmap_virtual_locked(bo
);
284 ttm_mem_io_unlock(old_man
);
288 * Create and bind a ttm if required.
291 if (!(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
)) {
292 if (bo
->ttm
== NULL
) {
293 bool zero
= !(old_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
);
294 ret
= ttm_bo_add_ttm(bo
, zero
);
299 ret
= ttm_tt_set_placement_caching(bo
->ttm
, mem
->placement
);
303 if (mem
->mem_type
!= TTM_PL_SYSTEM
) {
304 ret
= ttm_tt_bind(bo
->ttm
, mem
, ctx
);
309 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
) {
310 if (bdev
->driver
->move_notify
)
311 bdev
->driver
->move_notify(bo
, evict
, mem
);
318 if (bdev
->driver
->move_notify
)
319 bdev
->driver
->move_notify(bo
, evict
, mem
);
321 if (!(old_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) &&
322 !(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
))
323 ret
= ttm_bo_move_ttm(bo
, ctx
, mem
);
324 else if (bdev
->driver
->move
)
325 ret
= bdev
->driver
->move(bo
, evict
, ctx
, mem
);
327 ret
= ttm_bo_move_memcpy(bo
, ctx
, mem
);
330 if (bdev
->driver
->move_notify
) {
331 struct ttm_mem_reg tmp_mem
= *mem
;
334 bdev
->driver
->move_notify(bo
, false, mem
);
344 if (bdev
->driver
->invalidate_caches
) {
345 ret
= bdev
->driver
->invalidate_caches(bdev
, bo
->mem
.placement
);
347 pr_err("Can not flush read caches\n");
353 bo
->offset
= (bo
->mem
.start
<< PAGE_SHIFT
) +
354 bdev
->man
[bo
->mem
.mem_type
].gpu_offset
;
358 ctx
->bytes_moved
+= bo
->num_pages
<< PAGE_SHIFT
;
362 new_man
= &bdev
->man
[bo
->mem
.mem_type
];
363 if (new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) {
364 ttm_tt_destroy(bo
->ttm
);
373 * Will release GPU memory type usage on destruction.
374 * This is the place to put in driver specific hooks to release
375 * driver private resources.
376 * Will release the bo::reserved lock.
379 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object
*bo
)
381 if (bo
->bdev
->driver
->move_notify
)
382 bo
->bdev
->driver
->move_notify(bo
, false, NULL
);
384 ttm_tt_destroy(bo
->ttm
);
386 ttm_bo_mem_put(bo
, &bo
->mem
);
389 static int ttm_bo_individualize_resv(struct ttm_buffer_object
*bo
)
393 if (bo
->resv
== &bo
->ttm_resv
)
396 BUG_ON(!reservation_object_trylock(&bo
->ttm_resv
));
398 r
= reservation_object_copy_fences(&bo
->ttm_resv
, bo
->resv
);
400 reservation_object_unlock(&bo
->ttm_resv
);
405 static void ttm_bo_flush_all_fences(struct ttm_buffer_object
*bo
)
407 struct reservation_object_list
*fobj
;
408 struct dma_fence
*fence
;
411 fobj
= reservation_object_get_list(&bo
->ttm_resv
);
412 fence
= reservation_object_get_excl(&bo
->ttm_resv
);
413 if (fence
&& !fence
->ops
->signaled
)
414 dma_fence_enable_sw_signaling(fence
);
416 for (i
= 0; fobj
&& i
< fobj
->shared_count
; ++i
) {
417 fence
= rcu_dereference_protected(fobj
->shared
[i
],
418 reservation_object_held(bo
->resv
));
420 if (!fence
->ops
->signaled
)
421 dma_fence_enable_sw_signaling(fence
);
425 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object
*bo
)
427 struct ttm_bo_device
*bdev
= bo
->bdev
;
428 struct ttm_bo_global
*glob
= bo
->glob
;
431 ret
= ttm_bo_individualize_resv(bo
);
433 /* Last resort, if we fail to allocate memory for the
434 * fences block for the BO to become idle
436 reservation_object_wait_timeout_rcu(bo
->resv
, true, false,
438 spin_lock(&glob
->lru_lock
);
442 spin_lock(&glob
->lru_lock
);
443 ret
= reservation_object_trylock(bo
->resv
) ? 0 : -EBUSY
;
445 if (reservation_object_test_signaled_rcu(&bo
->ttm_resv
, true)) {
446 ttm_bo_del_from_lru(bo
);
447 spin_unlock(&glob
->lru_lock
);
448 if (bo
->resv
!= &bo
->ttm_resv
)
449 reservation_object_unlock(&bo
->ttm_resv
);
451 ttm_bo_cleanup_memtype_use(bo
);
452 reservation_object_unlock(bo
->resv
);
456 ttm_bo_flush_all_fences(bo
);
459 * Make NO_EVICT bos immediately available to
460 * shrinkers, now that they are queued for
463 if (bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
) {
464 bo
->mem
.placement
&= ~TTM_PL_FLAG_NO_EVICT
;
465 ttm_bo_add_to_lru(bo
);
468 reservation_object_unlock(bo
->resv
);
470 if (bo
->resv
!= &bo
->ttm_resv
)
471 reservation_object_unlock(&bo
->ttm_resv
);
474 kref_get(&bo
->list_kref
);
475 list_add_tail(&bo
->ddestroy
, &bdev
->ddestroy
);
476 spin_unlock(&glob
->lru_lock
);
478 schedule_delayed_work(&bdev
->wq
,
479 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
483 * function ttm_bo_cleanup_refs
484 * If bo idle, remove from delayed- and lru lists, and unref.
485 * If not idle, do nothing.
487 * Must be called with lru_lock and reservation held, this function
488 * will drop the lru lock and optionally the reservation lock before returning.
490 * @interruptible Any sleeps should occur interruptibly.
491 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
492 * @unlock_resv Unlock the reservation lock as well.
495 static int ttm_bo_cleanup_refs(struct ttm_buffer_object
*bo
,
496 bool interruptible
, bool no_wait_gpu
,
499 struct ttm_bo_global
*glob
= bo
->glob
;
500 struct reservation_object
*resv
;
503 if (unlikely(list_empty(&bo
->ddestroy
)))
506 resv
= &bo
->ttm_resv
;
508 if (reservation_object_test_signaled_rcu(resv
, true))
513 if (ret
&& !no_wait_gpu
) {
517 reservation_object_unlock(bo
->resv
);
518 spin_unlock(&glob
->lru_lock
);
520 lret
= reservation_object_wait_timeout_rcu(resv
, true,
529 spin_lock(&glob
->lru_lock
);
530 if (unlock_resv
&& !reservation_object_trylock(bo
->resv
)) {
532 * We raced, and lost, someone else holds the reservation now,
533 * and is probably busy in ttm_bo_cleanup_memtype_use.
535 * Even if it's not the case, because we finished waiting any
536 * delayed destruction would succeed, so just return success
539 spin_unlock(&glob
->lru_lock
);
545 if (ret
|| unlikely(list_empty(&bo
->ddestroy
))) {
547 reservation_object_unlock(bo
->resv
);
548 spin_unlock(&glob
->lru_lock
);
552 ttm_bo_del_from_lru(bo
);
553 list_del_init(&bo
->ddestroy
);
554 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
556 spin_unlock(&glob
->lru_lock
);
557 ttm_bo_cleanup_memtype_use(bo
);
560 reservation_object_unlock(bo
->resv
);
566 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
567 * encountered buffers.
569 static bool ttm_bo_delayed_delete(struct ttm_bo_device
*bdev
, bool remove_all
)
571 struct ttm_bo_global
*glob
= bdev
->glob
;
572 struct list_head removed
;
575 INIT_LIST_HEAD(&removed
);
577 spin_lock(&glob
->lru_lock
);
578 while (!list_empty(&bdev
->ddestroy
)) {
579 struct ttm_buffer_object
*bo
;
581 bo
= list_first_entry(&bdev
->ddestroy
, struct ttm_buffer_object
,
583 kref_get(&bo
->list_kref
);
584 list_move_tail(&bo
->ddestroy
, &removed
);
586 if (remove_all
|| bo
->resv
!= &bo
->ttm_resv
) {
587 spin_unlock(&glob
->lru_lock
);
588 reservation_object_lock(bo
->resv
, NULL
);
590 spin_lock(&glob
->lru_lock
);
591 ttm_bo_cleanup_refs(bo
, false, !remove_all
, true);
593 } else if (reservation_object_trylock(bo
->resv
)) {
594 ttm_bo_cleanup_refs(bo
, false, !remove_all
, true);
596 spin_unlock(&glob
->lru_lock
);
599 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
600 spin_lock(&glob
->lru_lock
);
602 list_splice_tail(&removed
, &bdev
->ddestroy
);
603 empty
= list_empty(&bdev
->ddestroy
);
604 spin_unlock(&glob
->lru_lock
);
609 static void ttm_bo_delayed_workqueue(struct work_struct
*work
)
611 struct ttm_bo_device
*bdev
=
612 container_of(work
, struct ttm_bo_device
, wq
.work
);
614 if (!ttm_bo_delayed_delete(bdev
, false)) {
615 schedule_delayed_work(&bdev
->wq
,
616 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
620 static void ttm_bo_release(struct kref
*kref
)
622 struct ttm_buffer_object
*bo
=
623 container_of(kref
, struct ttm_buffer_object
, kref
);
624 struct ttm_bo_device
*bdev
= bo
->bdev
;
625 struct ttm_mem_type_manager
*man
= &bdev
->man
[bo
->mem
.mem_type
];
627 drm_vma_offset_remove(&bdev
->vma_manager
, &bo
->vma_node
);
628 ttm_mem_io_lock(man
, false);
629 ttm_mem_io_free_vm(bo
);
630 ttm_mem_io_unlock(man
);
631 ttm_bo_cleanup_refs_or_queue(bo
);
632 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
635 void ttm_bo_unref(struct ttm_buffer_object
**p_bo
)
637 struct ttm_buffer_object
*bo
= *p_bo
;
640 kref_put(&bo
->kref
, ttm_bo_release
);
642 EXPORT_SYMBOL(ttm_bo_unref
);
644 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device
*bdev
)
646 return cancel_delayed_work_sync(&bdev
->wq
);
648 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue
);
650 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device
*bdev
, int resched
)
653 schedule_delayed_work(&bdev
->wq
,
654 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
656 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue
);
658 static int ttm_bo_evict(struct ttm_buffer_object
*bo
,
659 struct ttm_operation_ctx
*ctx
)
661 struct ttm_bo_device
*bdev
= bo
->bdev
;
662 struct ttm_mem_reg evict_mem
;
663 struct ttm_placement placement
;
666 reservation_object_assert_held(bo
->resv
);
669 evict_mem
.mm_node
= NULL
;
670 evict_mem
.bus
.io_reserved_vm
= false;
671 evict_mem
.bus
.io_reserved_count
= 0;
673 placement
.num_placement
= 0;
674 placement
.num_busy_placement
= 0;
675 bdev
->driver
->evict_flags(bo
, &placement
);
676 ret
= ttm_bo_mem_space(bo
, &placement
, &evict_mem
, ctx
);
678 if (ret
!= -ERESTARTSYS
) {
679 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
681 ttm_bo_mem_space_debug(bo
, &placement
);
686 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true, ctx
);
688 if (ret
!= -ERESTARTSYS
)
689 pr_err("Buffer eviction failed\n");
690 ttm_bo_mem_put(bo
, &evict_mem
);
698 bool ttm_bo_eviction_valuable(struct ttm_buffer_object
*bo
,
699 const struct ttm_place
*place
)
701 /* Don't evict this BO if it's outside of the
702 * requested placement range
704 if (place
->fpfn
>= (bo
->mem
.start
+ bo
->mem
.size
) ||
705 (place
->lpfn
&& place
->lpfn
<= bo
->mem
.start
))
710 EXPORT_SYMBOL(ttm_bo_eviction_valuable
);
713 * Check the target bo is allowable to be evicted or swapout, including cases:
715 * a. if share same reservation object with ctx->resv, have assumption
716 * reservation objects should already be locked, so not lock again and
717 * return true directly when either the opreation allow_reserved_eviction
718 * or the target bo already is in delayed free list;
720 * b. Otherwise, trylock it.
722 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object
*bo
,
723 struct ttm_operation_ctx
*ctx
, bool *locked
)
728 if (bo
->resv
== ctx
->resv
) {
729 reservation_object_assert_held(bo
->resv
);
730 if (ctx
->allow_reserved_eviction
|| !list_empty(&bo
->ddestroy
))
733 *locked
= reservation_object_trylock(bo
->resv
);
740 static int ttm_mem_evict_first(struct ttm_bo_device
*bdev
,
742 const struct ttm_place
*place
,
743 struct ttm_operation_ctx
*ctx
)
745 struct ttm_bo_global
*glob
= bdev
->glob
;
746 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
747 struct ttm_buffer_object
*bo
= NULL
;
752 spin_lock(&glob
->lru_lock
);
753 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
754 list_for_each_entry(bo
, &man
->lru
[i
], lru
) {
755 if (!ttm_bo_evict_swapout_allowable(bo
, ctx
, &locked
))
758 if (place
&& !bdev
->driver
->eviction_valuable(bo
,
761 reservation_object_unlock(bo
->resv
);
767 /* If the inner loop terminated early, we have our candidate */
768 if (&bo
->lru
!= &man
->lru
[i
])
775 spin_unlock(&glob
->lru_lock
);
779 kref_get(&bo
->list_kref
);
781 if (!list_empty(&bo
->ddestroy
)) {
782 ret
= ttm_bo_cleanup_refs(bo
, ctx
->interruptible
,
783 ctx
->no_wait_gpu
, locked
);
784 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
788 ttm_bo_del_from_lru(bo
);
789 spin_unlock(&glob
->lru_lock
);
791 ret
= ttm_bo_evict(bo
, ctx
);
793 ttm_bo_unreserve(bo
);
795 spin_lock(&glob
->lru_lock
);
796 ttm_bo_add_to_lru(bo
);
797 spin_unlock(&glob
->lru_lock
);
800 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
804 void ttm_bo_mem_put(struct ttm_buffer_object
*bo
, struct ttm_mem_reg
*mem
)
806 struct ttm_mem_type_manager
*man
= &bo
->bdev
->man
[mem
->mem_type
];
809 (*man
->func
->put_node
)(man
, mem
);
811 EXPORT_SYMBOL(ttm_bo_mem_put
);
814 * Add the last move fence to the BO and reserve a new shared slot.
816 static int ttm_bo_add_move_fence(struct ttm_buffer_object
*bo
,
817 struct ttm_mem_type_manager
*man
,
818 struct ttm_mem_reg
*mem
)
820 struct dma_fence
*fence
;
823 spin_lock(&man
->move_lock
);
824 fence
= dma_fence_get(man
->move
);
825 spin_unlock(&man
->move_lock
);
828 reservation_object_add_shared_fence(bo
->resv
, fence
);
830 ret
= reservation_object_reserve_shared(bo
->resv
);
834 dma_fence_put(bo
->moving
);
842 * Repeatedly evict memory from the LRU for @mem_type until we create enough
843 * space, or we've evicted everything and there isn't enough space.
845 static int ttm_bo_mem_force_space(struct ttm_buffer_object
*bo
,
847 const struct ttm_place
*place
,
848 struct ttm_mem_reg
*mem
,
849 struct ttm_operation_ctx
*ctx
)
851 struct ttm_bo_device
*bdev
= bo
->bdev
;
852 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
856 ret
= (*man
->func
->get_node
)(man
, bo
, place
, mem
);
857 if (unlikely(ret
!= 0))
861 ret
= ttm_mem_evict_first(bdev
, mem_type
, place
, ctx
);
862 if (unlikely(ret
!= 0))
865 mem
->mem_type
= mem_type
;
866 return ttm_bo_add_move_fence(bo
, man
, mem
);
869 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager
*man
,
870 uint32_t cur_placement
,
871 uint32_t proposed_placement
)
873 uint32_t caching
= proposed_placement
& TTM_PL_MASK_CACHING
;
874 uint32_t result
= proposed_placement
& ~TTM_PL_MASK_CACHING
;
877 * Keep current caching if possible.
880 if ((cur_placement
& caching
) != 0)
881 result
|= (cur_placement
& caching
);
882 else if ((man
->default_caching
& caching
) != 0)
883 result
|= man
->default_caching
;
884 else if ((TTM_PL_FLAG_CACHED
& caching
) != 0)
885 result
|= TTM_PL_FLAG_CACHED
;
886 else if ((TTM_PL_FLAG_WC
& caching
) != 0)
887 result
|= TTM_PL_FLAG_WC
;
888 else if ((TTM_PL_FLAG_UNCACHED
& caching
) != 0)
889 result
|= TTM_PL_FLAG_UNCACHED
;
894 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager
*man
,
896 const struct ttm_place
*place
,
897 uint32_t *masked_placement
)
899 uint32_t cur_flags
= ttm_bo_type_flags(mem_type
);
901 if ((cur_flags
& place
->flags
& TTM_PL_MASK_MEM
) == 0)
904 if ((place
->flags
& man
->available_caching
) == 0)
907 cur_flags
|= (place
->flags
& man
->available_caching
);
909 *masked_placement
= cur_flags
;
914 * Creates space for memory region @mem according to its type.
916 * This function first searches for free space in compatible memory types in
917 * the priority order defined by the driver. If free space isn't found, then
918 * ttm_bo_mem_force_space is attempted in priority order to evict and find
921 int ttm_bo_mem_space(struct ttm_buffer_object
*bo
,
922 struct ttm_placement
*placement
,
923 struct ttm_mem_reg
*mem
,
924 struct ttm_operation_ctx
*ctx
)
926 struct ttm_bo_device
*bdev
= bo
->bdev
;
927 struct ttm_mem_type_manager
*man
;
928 uint32_t mem_type
= TTM_PL_SYSTEM
;
929 uint32_t cur_flags
= 0;
930 bool type_found
= false;
931 bool type_ok
= false;
932 bool has_erestartsys
= false;
935 ret
= reservation_object_reserve_shared(bo
->resv
);
940 for (i
= 0; i
< placement
->num_placement
; ++i
) {
941 const struct ttm_place
*place
= &placement
->placement
[i
];
943 ret
= ttm_mem_type_from_place(place
, &mem_type
);
946 man
= &bdev
->man
[mem_type
];
947 if (!man
->has_type
|| !man
->use_type
)
950 type_ok
= ttm_bo_mt_compatible(man
, mem_type
, place
,
957 cur_flags
= ttm_bo_select_caching(man
, bo
->mem
.placement
,
960 * Use the access and other non-mapping-related flag bits from
961 * the memory placement flags to the current flags
963 ttm_flag_masked(&cur_flags
, place
->flags
,
964 ~TTM_PL_MASK_MEMTYPE
);
966 if (mem_type
== TTM_PL_SYSTEM
)
969 ret
= (*man
->func
->get_node
)(man
, bo
, place
, mem
);
974 ret
= ttm_bo_add_move_fence(bo
, man
, mem
);
976 (*man
->func
->put_node
)(man
, mem
);
983 if ((type_ok
&& (mem_type
== TTM_PL_SYSTEM
)) || mem
->mm_node
) {
984 mem
->mem_type
= mem_type
;
985 mem
->placement
= cur_flags
;
989 for (i
= 0; i
< placement
->num_busy_placement
; ++i
) {
990 const struct ttm_place
*place
= &placement
->busy_placement
[i
];
992 ret
= ttm_mem_type_from_place(place
, &mem_type
);
995 man
= &bdev
->man
[mem_type
];
996 if (!man
->has_type
|| !man
->use_type
)
998 if (!ttm_bo_mt_compatible(man
, mem_type
, place
, &cur_flags
))
1002 cur_flags
= ttm_bo_select_caching(man
, bo
->mem
.placement
,
1005 * Use the access and other non-mapping-related flag bits from
1006 * the memory placement flags to the current flags
1008 ttm_flag_masked(&cur_flags
, place
->flags
,
1009 ~TTM_PL_MASK_MEMTYPE
);
1011 if (mem_type
== TTM_PL_SYSTEM
) {
1012 mem
->mem_type
= mem_type
;
1013 mem
->placement
= cur_flags
;
1014 mem
->mm_node
= NULL
;
1018 ret
= ttm_bo_mem_force_space(bo
, mem_type
, place
, mem
, ctx
);
1019 if (ret
== 0 && mem
->mm_node
) {
1020 mem
->placement
= cur_flags
;
1023 if (ret
== -ERESTARTSYS
)
1024 has_erestartsys
= true;
1028 pr_err(TTM_PFX
"No compatible memory type found\n");
1032 return (has_erestartsys
) ? -ERESTARTSYS
: -ENOMEM
;
1034 EXPORT_SYMBOL(ttm_bo_mem_space
);
1036 static int ttm_bo_move_buffer(struct ttm_buffer_object
*bo
,
1037 struct ttm_placement
*placement
,
1038 struct ttm_operation_ctx
*ctx
)
1041 struct ttm_mem_reg mem
;
1043 reservation_object_assert_held(bo
->resv
);
1045 mem
.num_pages
= bo
->num_pages
;
1046 mem
.size
= mem
.num_pages
<< PAGE_SHIFT
;
1047 mem
.page_alignment
= bo
->mem
.page_alignment
;
1048 mem
.bus
.io_reserved_vm
= false;
1049 mem
.bus
.io_reserved_count
= 0;
1051 * Determine where to move the buffer.
1053 ret
= ttm_bo_mem_space(bo
, placement
, &mem
, ctx
);
1056 ret
= ttm_bo_handle_move_mem(bo
, &mem
, false, ctx
);
1058 if (ret
&& mem
.mm_node
)
1059 ttm_bo_mem_put(bo
, &mem
);
1063 static bool ttm_bo_places_compat(const struct ttm_place
*places
,
1064 unsigned num_placement
,
1065 struct ttm_mem_reg
*mem
,
1066 uint32_t *new_flags
)
1070 for (i
= 0; i
< num_placement
; i
++) {
1071 const struct ttm_place
*heap
= &places
[i
];
1073 if (mem
->mm_node
&& (mem
->start
< heap
->fpfn
||
1074 (heap
->lpfn
!= 0 && (mem
->start
+ mem
->num_pages
) > heap
->lpfn
)))
1077 *new_flags
= heap
->flags
;
1078 if ((*new_flags
& mem
->placement
& TTM_PL_MASK_CACHING
) &&
1079 (*new_flags
& mem
->placement
& TTM_PL_MASK_MEM
) &&
1080 (!(*new_flags
& TTM_PL_FLAG_CONTIGUOUS
) ||
1081 (mem
->placement
& TTM_PL_FLAG_CONTIGUOUS
)))
1087 bool ttm_bo_mem_compat(struct ttm_placement
*placement
,
1088 struct ttm_mem_reg
*mem
,
1089 uint32_t *new_flags
)
1091 if (ttm_bo_places_compat(placement
->placement
, placement
->num_placement
,
1095 if ((placement
->busy_placement
!= placement
->placement
||
1096 placement
->num_busy_placement
> placement
->num_placement
) &&
1097 ttm_bo_places_compat(placement
->busy_placement
,
1098 placement
->num_busy_placement
,
1104 EXPORT_SYMBOL(ttm_bo_mem_compat
);
1106 int ttm_bo_validate(struct ttm_buffer_object
*bo
,
1107 struct ttm_placement
*placement
,
1108 struct ttm_operation_ctx
*ctx
)
1113 reservation_object_assert_held(bo
->resv
);
1115 * Check whether we need to move buffer.
1117 if (!ttm_bo_mem_compat(placement
, &bo
->mem
, &new_flags
)) {
1118 ret
= ttm_bo_move_buffer(bo
, placement
, ctx
);
1123 * Use the access and other non-mapping-related flag bits from
1124 * the compatible memory placement flags to the active flags
1126 ttm_flag_masked(&bo
->mem
.placement
, new_flags
,
1127 ~TTM_PL_MASK_MEMTYPE
);
1130 * We might need to add a TTM.
1132 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
&& bo
->ttm
== NULL
) {
1133 ret
= ttm_bo_add_ttm(bo
, true);
1139 EXPORT_SYMBOL(ttm_bo_validate
);
1141 int ttm_bo_init_reserved(struct ttm_bo_device
*bdev
,
1142 struct ttm_buffer_object
*bo
,
1144 enum ttm_bo_type type
,
1145 struct ttm_placement
*placement
,
1146 uint32_t page_alignment
,
1147 struct ttm_operation_ctx
*ctx
,
1148 struct file
*persistent_swap_storage
,
1150 struct sg_table
*sg
,
1151 struct reservation_object
*resv
,
1152 void (*destroy
) (struct ttm_buffer_object
*))
1155 unsigned long num_pages
;
1156 struct ttm_mem_global
*mem_glob
= bdev
->glob
->mem_glob
;
1159 ret
= ttm_mem_global_alloc(mem_glob
, acc_size
, ctx
);
1161 pr_err("Out of kernel memory\n");
1169 num_pages
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1170 if (num_pages
== 0) {
1171 pr_err("Illegal buffer object size\n");
1176 ttm_mem_global_free(mem_glob
, acc_size
);
1179 bo
->destroy
= destroy
;
1181 kref_init(&bo
->kref
);
1182 kref_init(&bo
->list_kref
);
1183 atomic_set(&bo
->cpu_writers
, 0);
1184 INIT_LIST_HEAD(&bo
->lru
);
1185 INIT_LIST_HEAD(&bo
->ddestroy
);
1186 INIT_LIST_HEAD(&bo
->swap
);
1187 INIT_LIST_HEAD(&bo
->io_reserve_lru
);
1188 mutex_init(&bo
->wu_mutex
);
1190 bo
->glob
= bdev
->glob
;
1192 bo
->num_pages
= num_pages
;
1193 bo
->mem
.size
= num_pages
<< PAGE_SHIFT
;
1194 bo
->mem
.mem_type
= TTM_PL_SYSTEM
;
1195 bo
->mem
.num_pages
= bo
->num_pages
;
1196 bo
->mem
.mm_node
= NULL
;
1197 bo
->mem
.page_alignment
= page_alignment
;
1198 bo
->mem
.bus
.io_reserved_vm
= false;
1199 bo
->mem
.bus
.io_reserved_count
= 0;
1201 bo
->mem
.placement
= (TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
);
1202 bo
->persistent_swap_storage
= persistent_swap_storage
;
1203 bo
->acc_size
= acc_size
;
1207 reservation_object_assert_held(bo
->resv
);
1209 bo
->resv
= &bo
->ttm_resv
;
1211 reservation_object_init(&bo
->ttm_resv
);
1212 atomic_inc(&bo
->glob
->bo_count
);
1213 drm_vma_node_reset(&bo
->vma_node
);
1217 * For ttm_bo_type_device buffers, allocate
1218 * address space from the device.
1220 if (bo
->type
== ttm_bo_type_device
||
1221 bo
->type
== ttm_bo_type_sg
)
1222 ret
= drm_vma_offset_add(&bdev
->vma_manager
, &bo
->vma_node
,
1225 /* passed reservation objects should already be locked,
1226 * since otherwise lockdep will be angered in radeon.
1229 locked
= reservation_object_trylock(bo
->resv
);
1234 ret
= ttm_bo_validate(bo
, placement
, ctx
);
1236 if (unlikely(ret
)) {
1238 ttm_bo_unreserve(bo
);
1244 if (resv
&& !(bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
)) {
1245 spin_lock(&bo
->glob
->lru_lock
);
1246 ttm_bo_add_to_lru(bo
);
1247 spin_unlock(&bo
->glob
->lru_lock
);
1252 EXPORT_SYMBOL(ttm_bo_init_reserved
);
1254 int ttm_bo_init(struct ttm_bo_device
*bdev
,
1255 struct ttm_buffer_object
*bo
,
1257 enum ttm_bo_type type
,
1258 struct ttm_placement
*placement
,
1259 uint32_t page_alignment
,
1261 struct file
*persistent_swap_storage
,
1263 struct sg_table
*sg
,
1264 struct reservation_object
*resv
,
1265 void (*destroy
) (struct ttm_buffer_object
*))
1267 struct ttm_operation_ctx ctx
= { interruptible
, false };
1270 ret
= ttm_bo_init_reserved(bdev
, bo
, size
, type
, placement
,
1271 page_alignment
, &ctx
,
1272 persistent_swap_storage
, acc_size
,
1278 ttm_bo_unreserve(bo
);
1282 EXPORT_SYMBOL(ttm_bo_init
);
1284 size_t ttm_bo_acc_size(struct ttm_bo_device
*bdev
,
1285 unsigned long bo_size
,
1286 unsigned struct_size
)
1288 unsigned npages
= (PAGE_ALIGN(bo_size
)) >> PAGE_SHIFT
;
1291 size
+= ttm_round_pot(struct_size
);
1292 size
+= ttm_round_pot(npages
* sizeof(void *));
1293 size
+= ttm_round_pot(sizeof(struct ttm_tt
));
1296 EXPORT_SYMBOL(ttm_bo_acc_size
);
1298 size_t ttm_bo_dma_acc_size(struct ttm_bo_device
*bdev
,
1299 unsigned long bo_size
,
1300 unsigned struct_size
)
1302 unsigned npages
= (PAGE_ALIGN(bo_size
)) >> PAGE_SHIFT
;
1305 size
+= ttm_round_pot(struct_size
);
1306 size
+= ttm_round_pot(npages
* (2*sizeof(void *) + sizeof(dma_addr_t
)));
1307 size
+= ttm_round_pot(sizeof(struct ttm_dma_tt
));
1310 EXPORT_SYMBOL(ttm_bo_dma_acc_size
);
1312 int ttm_bo_create(struct ttm_bo_device
*bdev
,
1314 enum ttm_bo_type type
,
1315 struct ttm_placement
*placement
,
1316 uint32_t page_alignment
,
1318 struct file
*persistent_swap_storage
,
1319 struct ttm_buffer_object
**p_bo
)
1321 struct ttm_buffer_object
*bo
;
1325 bo
= kzalloc(sizeof(*bo
), GFP_KERNEL
);
1326 if (unlikely(bo
== NULL
))
1329 acc_size
= ttm_bo_acc_size(bdev
, size
, sizeof(struct ttm_buffer_object
));
1330 ret
= ttm_bo_init(bdev
, bo
, size
, type
, placement
, page_alignment
,
1331 interruptible
, persistent_swap_storage
, acc_size
,
1333 if (likely(ret
== 0))
1338 EXPORT_SYMBOL(ttm_bo_create
);
1340 static int ttm_bo_force_list_clean(struct ttm_bo_device
*bdev
,
1343 struct ttm_operation_ctx ctx
= { false, false };
1344 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1345 struct ttm_bo_global
*glob
= bdev
->glob
;
1346 struct dma_fence
*fence
;
1351 * Can't use standard list traversal since we're unlocking.
1354 spin_lock(&glob
->lru_lock
);
1355 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
1356 while (!list_empty(&man
->lru
[i
])) {
1357 spin_unlock(&glob
->lru_lock
);
1358 ret
= ttm_mem_evict_first(bdev
, mem_type
, NULL
, &ctx
);
1361 spin_lock(&glob
->lru_lock
);
1364 spin_unlock(&glob
->lru_lock
);
1366 spin_lock(&man
->move_lock
);
1367 fence
= dma_fence_get(man
->move
);
1368 spin_unlock(&man
->move_lock
);
1371 ret
= dma_fence_wait(fence
, false);
1372 dma_fence_put(fence
);
1380 int ttm_bo_clean_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1382 struct ttm_mem_type_manager
*man
;
1385 if (mem_type
>= TTM_NUM_MEM_TYPES
) {
1386 pr_err("Illegal memory type %d\n", mem_type
);
1389 man
= &bdev
->man
[mem_type
];
1391 if (!man
->has_type
) {
1392 pr_err("Trying to take down uninitialized memory manager type %u\n",
1397 man
->use_type
= false;
1398 man
->has_type
= false;
1402 ret
= ttm_bo_force_list_clean(bdev
, mem_type
);
1404 pr_err("Cleanup eviction failed\n");
1408 ret
= (*man
->func
->takedown
)(man
);
1411 dma_fence_put(man
->move
);
1416 EXPORT_SYMBOL(ttm_bo_clean_mm
);
1418 int ttm_bo_evict_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1420 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1422 if (mem_type
== 0 || mem_type
>= TTM_NUM_MEM_TYPES
) {
1423 pr_err("Illegal memory manager memory type %u\n", mem_type
);
1427 if (!man
->has_type
) {
1428 pr_err("Memory type %u has not been initialized\n", mem_type
);
1432 return ttm_bo_force_list_clean(bdev
, mem_type
);
1434 EXPORT_SYMBOL(ttm_bo_evict_mm
);
1436 int ttm_bo_init_mm(struct ttm_bo_device
*bdev
, unsigned type
,
1437 unsigned long p_size
)
1440 struct ttm_mem_type_manager
*man
;
1443 BUG_ON(type
>= TTM_NUM_MEM_TYPES
);
1444 man
= &bdev
->man
[type
];
1445 BUG_ON(man
->has_type
);
1446 man
->io_reserve_fastpath
= true;
1447 man
->use_io_reserve_lru
= false;
1448 mutex_init(&man
->io_reserve_mutex
);
1449 spin_lock_init(&man
->move_lock
);
1450 INIT_LIST_HEAD(&man
->io_reserve_lru
);
1452 ret
= bdev
->driver
->init_mem_type(bdev
, type
, man
);
1457 if (type
!= TTM_PL_SYSTEM
) {
1458 ret
= (*man
->func
->init
)(man
, p_size
);
1462 man
->has_type
= true;
1463 man
->use_type
= true;
1466 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
)
1467 INIT_LIST_HEAD(&man
->lru
[i
]);
1472 EXPORT_SYMBOL(ttm_bo_init_mm
);
1474 static void ttm_bo_global_kobj_release(struct kobject
*kobj
)
1476 struct ttm_bo_global
*glob
=
1477 container_of(kobj
, struct ttm_bo_global
, kobj
);
1479 __free_page(glob
->dummy_read_page
);
1483 void ttm_bo_global_release(struct drm_global_reference
*ref
)
1485 struct ttm_bo_global
*glob
= ref
->object
;
1487 kobject_del(&glob
->kobj
);
1488 kobject_put(&glob
->kobj
);
1490 EXPORT_SYMBOL(ttm_bo_global_release
);
1492 int ttm_bo_global_init(struct drm_global_reference
*ref
)
1494 struct ttm_bo_global_ref
*bo_ref
=
1495 container_of(ref
, struct ttm_bo_global_ref
, ref
);
1496 struct ttm_bo_global
*glob
= ref
->object
;
1500 mutex_init(&glob
->device_list_mutex
);
1501 spin_lock_init(&glob
->lru_lock
);
1502 glob
->mem_glob
= bo_ref
->mem_glob
;
1503 glob
->mem_glob
->bo_glob
= glob
;
1504 glob
->dummy_read_page
= alloc_page(__GFP_ZERO
| GFP_DMA32
);
1506 if (unlikely(glob
->dummy_read_page
== NULL
)) {
1511 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
)
1512 INIT_LIST_HEAD(&glob
->swap_lru
[i
]);
1513 INIT_LIST_HEAD(&glob
->device_list
);
1514 atomic_set(&glob
->bo_count
, 0);
1516 ret
= kobject_init_and_add(
1517 &glob
->kobj
, &ttm_bo_glob_kobj_type
, ttm_get_kobj(), "buffer_objects");
1518 if (unlikely(ret
!= 0))
1519 kobject_put(&glob
->kobj
);
1525 EXPORT_SYMBOL(ttm_bo_global_init
);
1528 int ttm_bo_device_release(struct ttm_bo_device
*bdev
)
1531 unsigned i
= TTM_NUM_MEM_TYPES
;
1532 struct ttm_mem_type_manager
*man
;
1533 struct ttm_bo_global
*glob
= bdev
->glob
;
1536 man
= &bdev
->man
[i
];
1537 if (man
->has_type
) {
1538 man
->use_type
= false;
1539 if ((i
!= TTM_PL_SYSTEM
) && ttm_bo_clean_mm(bdev
, i
)) {
1541 pr_err("DRM memory manager type %d is not clean\n",
1544 man
->has_type
= false;
1548 mutex_lock(&glob
->device_list_mutex
);
1549 list_del(&bdev
->device_list
);
1550 mutex_unlock(&glob
->device_list_mutex
);
1552 cancel_delayed_work_sync(&bdev
->wq
);
1554 if (ttm_bo_delayed_delete(bdev
, true))
1555 pr_debug("Delayed destroy list was clean\n");
1557 spin_lock(&glob
->lru_lock
);
1558 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
)
1559 if (list_empty(&bdev
->man
[0].lru
[0]))
1560 pr_debug("Swap list %d was clean\n", i
);
1561 spin_unlock(&glob
->lru_lock
);
1563 drm_vma_offset_manager_destroy(&bdev
->vma_manager
);
1567 EXPORT_SYMBOL(ttm_bo_device_release
);
1569 int ttm_bo_device_init(struct ttm_bo_device
*bdev
,
1570 struct ttm_bo_global
*glob
,
1571 struct ttm_bo_driver
*driver
,
1572 struct address_space
*mapping
,
1573 uint64_t file_page_offset
,
1578 bdev
->driver
= driver
;
1580 memset(bdev
->man
, 0, sizeof(bdev
->man
));
1583 * Initialize the system memory buffer type.
1584 * Other types need to be driver / IOCTL initialized.
1586 ret
= ttm_bo_init_mm(bdev
, TTM_PL_SYSTEM
, 0);
1587 if (unlikely(ret
!= 0))
1590 drm_vma_offset_manager_init(&bdev
->vma_manager
, file_page_offset
,
1592 INIT_DELAYED_WORK(&bdev
->wq
, ttm_bo_delayed_workqueue
);
1593 INIT_LIST_HEAD(&bdev
->ddestroy
);
1594 bdev
->dev_mapping
= mapping
;
1596 bdev
->need_dma32
= need_dma32
;
1597 mutex_lock(&glob
->device_list_mutex
);
1598 list_add_tail(&bdev
->device_list
, &glob
->device_list
);
1599 mutex_unlock(&glob
->device_list_mutex
);
1605 EXPORT_SYMBOL(ttm_bo_device_init
);
1608 * buffer object vm functions.
1611 bool ttm_mem_reg_is_pci(struct ttm_bo_device
*bdev
, struct ttm_mem_reg
*mem
)
1613 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem
->mem_type
];
1615 if (!(man
->flags
& TTM_MEMTYPE_FLAG_FIXED
)) {
1616 if (mem
->mem_type
== TTM_PL_SYSTEM
)
1619 if (man
->flags
& TTM_MEMTYPE_FLAG_CMA
)
1622 if (mem
->placement
& TTM_PL_FLAG_CACHED
)
1628 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object
*bo
)
1630 struct ttm_bo_device
*bdev
= bo
->bdev
;
1632 drm_vma_node_unmap(&bo
->vma_node
, bdev
->dev_mapping
);
1633 ttm_mem_io_free_vm(bo
);
1636 void ttm_bo_unmap_virtual(struct ttm_buffer_object
*bo
)
1638 struct ttm_bo_device
*bdev
= bo
->bdev
;
1639 struct ttm_mem_type_manager
*man
= &bdev
->man
[bo
->mem
.mem_type
];
1641 ttm_mem_io_lock(man
, false);
1642 ttm_bo_unmap_virtual_locked(bo
);
1643 ttm_mem_io_unlock(man
);
1647 EXPORT_SYMBOL(ttm_bo_unmap_virtual
);
1649 int ttm_bo_wait(struct ttm_buffer_object
*bo
,
1650 bool interruptible
, bool no_wait
)
1652 long timeout
= 15 * HZ
;
1655 if (reservation_object_test_signaled_rcu(bo
->resv
, true))
1661 timeout
= reservation_object_wait_timeout_rcu(bo
->resv
, true,
1662 interruptible
, timeout
);
1669 reservation_object_add_excl_fence(bo
->resv
, NULL
);
1672 EXPORT_SYMBOL(ttm_bo_wait
);
1674 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object
*bo
, bool no_wait
)
1679 * Using ttm_bo_reserve makes sure the lru lists are updated.
1682 ret
= ttm_bo_reserve(bo
, true, no_wait
, NULL
);
1683 if (unlikely(ret
!= 0))
1685 ret
= ttm_bo_wait(bo
, true, no_wait
);
1686 if (likely(ret
== 0))
1687 atomic_inc(&bo
->cpu_writers
);
1688 ttm_bo_unreserve(bo
);
1691 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab
);
1693 void ttm_bo_synccpu_write_release(struct ttm_buffer_object
*bo
)
1695 atomic_dec(&bo
->cpu_writers
);
1697 EXPORT_SYMBOL(ttm_bo_synccpu_write_release
);
1700 * A buffer object shrink method that tries to swap out the first
1701 * buffer object on the bo_global::swap_lru list.
1703 int ttm_bo_swapout(struct ttm_bo_global
*glob
, struct ttm_operation_ctx
*ctx
)
1705 struct ttm_buffer_object
*bo
;
1710 spin_lock(&glob
->lru_lock
);
1711 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
1712 list_for_each_entry(bo
, &glob
->swap_lru
[i
], swap
) {
1713 if (ttm_bo_evict_swapout_allowable(bo
, ctx
, &locked
)) {
1723 spin_unlock(&glob
->lru_lock
);
1727 kref_get(&bo
->list_kref
);
1729 if (!list_empty(&bo
->ddestroy
)) {
1730 ret
= ttm_bo_cleanup_refs(bo
, false, false, locked
);
1731 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1735 ttm_bo_del_from_lru(bo
);
1736 spin_unlock(&glob
->lru_lock
);
1739 * Move to system cached
1742 if (bo
->mem
.mem_type
!= TTM_PL_SYSTEM
||
1743 bo
->ttm
->caching_state
!= tt_cached
) {
1744 struct ttm_operation_ctx ctx
= { false, false };
1745 struct ttm_mem_reg evict_mem
;
1747 evict_mem
= bo
->mem
;
1748 evict_mem
.mm_node
= NULL
;
1749 evict_mem
.placement
= TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
;
1750 evict_mem
.mem_type
= TTM_PL_SYSTEM
;
1752 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true, &ctx
);
1753 if (unlikely(ret
!= 0))
1758 * Make sure BO is idle.
1761 ret
= ttm_bo_wait(bo
, false, false);
1762 if (unlikely(ret
!= 0))
1765 ttm_bo_unmap_virtual(bo
);
1768 * Swap out. Buffer will be swapped in again as soon as
1769 * anyone tries to access a ttm page.
1772 if (bo
->bdev
->driver
->swap_notify
)
1773 bo
->bdev
->driver
->swap_notify(bo
);
1775 ret
= ttm_tt_swapout(bo
->ttm
, bo
->persistent_swap_storage
);
1780 * Unreserve without putting on LRU to avoid swapping out an
1781 * already swapped buffer.
1784 reservation_object_unlock(bo
->resv
);
1785 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1788 EXPORT_SYMBOL(ttm_bo_swapout
);
1790 void ttm_bo_swapout_all(struct ttm_bo_device
*bdev
)
1792 struct ttm_operation_ctx ctx
= {
1793 .interruptible
= false,
1794 .no_wait_gpu
= false
1797 while (ttm_bo_swapout(bdev
->glob
, &ctx
) == 0)
1800 EXPORT_SYMBOL(ttm_bo_swapout_all
);
1803 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1806 * @bo: Pointer to buffer
1808 int ttm_bo_wait_unreserved(struct ttm_buffer_object
*bo
)
1813 * In the absense of a wait_unlocked API,
1814 * Use the bo::wu_mutex to avoid triggering livelocks due to
1815 * concurrent use of this function. Note that this use of
1816 * bo::wu_mutex can go away if we change locking order to
1817 * mmap_sem -> bo::reserve.
1819 ret
= mutex_lock_interruptible(&bo
->wu_mutex
);
1820 if (unlikely(ret
!= 0))
1821 return -ERESTARTSYS
;
1822 if (!ww_mutex_is_locked(&bo
->resv
->lock
))
1824 ret
= reservation_object_lock_interruptible(bo
->resv
, NULL
);
1827 if (unlikely(ret
!= 0))
1829 reservation_object_unlock(bo
->resv
);
1832 mutex_unlock(&bo
->wu_mutex
);