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
);
48 static struct attribute ttm_bo_count
= {
53 /* default destructor */
54 static void ttm_bo_default_destroy(struct ttm_buffer_object
*bo
)
59 static inline int ttm_mem_type_from_place(const struct ttm_place
*place
,
64 pos
= ffs(place
->flags
& TTM_PL_MASK_MEM
);
72 static void ttm_mem_type_debug(struct ttm_bo_device
*bdev
, int mem_type
)
74 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
75 struct drm_printer p
= drm_debug_printer(TTM_PFX
);
77 pr_err(" has_type: %d\n", man
->has_type
);
78 pr_err(" use_type: %d\n", man
->use_type
);
79 pr_err(" flags: 0x%08X\n", man
->flags
);
80 pr_err(" gpu_offset: 0x%08llX\n", man
->gpu_offset
);
81 pr_err(" size: %llu\n", man
->size
);
82 pr_err(" available_caching: 0x%08X\n", man
->available_caching
);
83 pr_err(" default_caching: 0x%08X\n", man
->default_caching
);
84 if (mem_type
!= TTM_PL_SYSTEM
)
85 (*man
->func
->debug
)(man
, &p
);
88 static void ttm_bo_mem_space_debug(struct ttm_buffer_object
*bo
,
89 struct ttm_placement
*placement
)
93 pr_err("No space for %p (%lu pages, %luK, %luM)\n",
94 bo
, bo
->mem
.num_pages
, bo
->mem
.size
>> 10,
96 for (i
= 0; i
< placement
->num_placement
; i
++) {
97 ret
= ttm_mem_type_from_place(&placement
->placement
[i
],
101 pr_err(" placement[%d]=0x%08X (%d)\n",
102 i
, placement
->placement
[i
].flags
, mem_type
);
103 ttm_mem_type_debug(bo
->bdev
, mem_type
);
107 static ssize_t
ttm_bo_global_show(struct kobject
*kobj
,
108 struct attribute
*attr
,
111 struct ttm_bo_global
*glob
=
112 container_of(kobj
, struct ttm_bo_global
, kobj
);
114 return snprintf(buffer
, PAGE_SIZE
, "%d\n",
115 atomic_read(&glob
->bo_count
));
118 static struct attribute
*ttm_bo_global_attrs
[] = {
123 static const struct sysfs_ops ttm_bo_global_ops
= {
124 .show
= &ttm_bo_global_show
127 static struct kobj_type ttm_bo_glob_kobj_type
= {
128 .release
= &ttm_bo_global_kobj_release
,
129 .sysfs_ops
= &ttm_bo_global_ops
,
130 .default_attrs
= ttm_bo_global_attrs
134 static inline uint32_t ttm_bo_type_flags(unsigned type
)
139 static void ttm_bo_release_list(struct kref
*list_kref
)
141 struct ttm_buffer_object
*bo
=
142 container_of(list_kref
, struct ttm_buffer_object
, list_kref
);
143 struct ttm_bo_device
*bdev
= bo
->bdev
;
144 size_t acc_size
= bo
->acc_size
;
146 BUG_ON(kref_read(&bo
->list_kref
));
147 BUG_ON(kref_read(&bo
->kref
));
148 BUG_ON(atomic_read(&bo
->cpu_writers
));
149 BUG_ON(bo
->mem
.mm_node
!= NULL
);
150 BUG_ON(!list_empty(&bo
->lru
));
151 BUG_ON(!list_empty(&bo
->ddestroy
));
152 ttm_tt_destroy(bo
->ttm
);
153 atomic_dec(&bo
->bdev
->glob
->bo_count
);
154 dma_fence_put(bo
->moving
);
155 reservation_object_fini(&bo
->ttm_resv
);
156 mutex_destroy(&bo
->wu_mutex
);
158 ttm_mem_global_free(bdev
->glob
->mem_glob
, acc_size
);
161 void ttm_bo_add_to_lru(struct ttm_buffer_object
*bo
)
163 struct ttm_bo_device
*bdev
= bo
->bdev
;
164 struct ttm_mem_type_manager
*man
;
166 reservation_object_assert_held(bo
->resv
);
168 if (!(bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
)) {
169 BUG_ON(!list_empty(&bo
->lru
));
171 man
= &bdev
->man
[bo
->mem
.mem_type
];
172 list_add_tail(&bo
->lru
, &man
->lru
[bo
->priority
]);
173 kref_get(&bo
->list_kref
);
175 if (bo
->ttm
&& !(bo
->ttm
->page_flags
&
176 (TTM_PAGE_FLAG_SG
| TTM_PAGE_FLAG_SWAPPED
))) {
177 list_add_tail(&bo
->swap
,
178 &bdev
->glob
->swap_lru
[bo
->priority
]);
179 kref_get(&bo
->list_kref
);
183 EXPORT_SYMBOL(ttm_bo_add_to_lru
);
185 static void ttm_bo_ref_bug(struct kref
*list_kref
)
190 void ttm_bo_del_from_lru(struct ttm_buffer_object
*bo
)
192 if (!list_empty(&bo
->swap
)) {
193 list_del_init(&bo
->swap
);
194 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
196 if (!list_empty(&bo
->lru
)) {
197 list_del_init(&bo
->lru
);
198 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
202 * TODO: Add a driver hook to delete from
203 * driver-specific LRU's here.
207 void ttm_bo_del_sub_from_lru(struct ttm_buffer_object
*bo
)
209 struct ttm_bo_global
*glob
= bo
->bdev
->glob
;
211 spin_lock(&glob
->lru_lock
);
212 ttm_bo_del_from_lru(bo
);
213 spin_unlock(&glob
->lru_lock
);
215 EXPORT_SYMBOL(ttm_bo_del_sub_from_lru
);
217 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object
*bo
)
219 reservation_object_assert_held(bo
->resv
);
221 ttm_bo_del_from_lru(bo
);
222 ttm_bo_add_to_lru(bo
);
224 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail
);
226 static int ttm_bo_handle_move_mem(struct ttm_buffer_object
*bo
,
227 struct ttm_mem_reg
*mem
, bool evict
,
228 struct ttm_operation_ctx
*ctx
)
230 struct ttm_bo_device
*bdev
= bo
->bdev
;
231 bool old_is_pci
= ttm_mem_reg_is_pci(bdev
, &bo
->mem
);
232 bool new_is_pci
= ttm_mem_reg_is_pci(bdev
, mem
);
233 struct ttm_mem_type_manager
*old_man
= &bdev
->man
[bo
->mem
.mem_type
];
234 struct ttm_mem_type_manager
*new_man
= &bdev
->man
[mem
->mem_type
];
237 if (old_is_pci
|| new_is_pci
||
238 ((mem
->placement
& bo
->mem
.placement
& TTM_PL_MASK_CACHING
) == 0)) {
239 ret
= ttm_mem_io_lock(old_man
, true);
240 if (unlikely(ret
!= 0))
242 ttm_bo_unmap_virtual_locked(bo
);
243 ttm_mem_io_unlock(old_man
);
247 * Create and bind a ttm if required.
250 if (!(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
)) {
251 if (bo
->ttm
== NULL
) {
252 bool zero
= !(old_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
);
253 ret
= ttm_tt_create(bo
, zero
);
258 ret
= ttm_tt_set_placement_caching(bo
->ttm
, mem
->placement
);
262 if (mem
->mem_type
!= TTM_PL_SYSTEM
) {
263 ret
= ttm_tt_bind(bo
->ttm
, mem
, ctx
);
268 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
) {
269 if (bdev
->driver
->move_notify
)
270 bdev
->driver
->move_notify(bo
, evict
, mem
);
277 if (bdev
->driver
->move_notify
)
278 bdev
->driver
->move_notify(bo
, evict
, mem
);
280 if (!(old_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) &&
281 !(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
))
282 ret
= ttm_bo_move_ttm(bo
, ctx
, mem
);
283 else if (bdev
->driver
->move
)
284 ret
= bdev
->driver
->move(bo
, evict
, ctx
, mem
);
286 ret
= ttm_bo_move_memcpy(bo
, ctx
, mem
);
289 if (bdev
->driver
->move_notify
) {
291 bdev
->driver
->move_notify(bo
, false, mem
);
300 if (bdev
->driver
->invalidate_caches
) {
301 ret
= bdev
->driver
->invalidate_caches(bdev
, bo
->mem
.placement
);
303 pr_err("Can not flush read caches\n");
309 bo
->offset
= (bo
->mem
.start
<< PAGE_SHIFT
) +
310 bdev
->man
[bo
->mem
.mem_type
].gpu_offset
;
314 ctx
->bytes_moved
+= bo
->num_pages
<< PAGE_SHIFT
;
318 new_man
= &bdev
->man
[bo
->mem
.mem_type
];
319 if (new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) {
320 ttm_tt_destroy(bo
->ttm
);
329 * Will release GPU memory type usage on destruction.
330 * This is the place to put in driver specific hooks to release
331 * driver private resources.
332 * Will release the bo::reserved lock.
335 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object
*bo
)
337 if (bo
->bdev
->driver
->move_notify
)
338 bo
->bdev
->driver
->move_notify(bo
, false, NULL
);
340 ttm_tt_destroy(bo
->ttm
);
342 ttm_bo_mem_put(bo
, &bo
->mem
);
345 static int ttm_bo_individualize_resv(struct ttm_buffer_object
*bo
)
349 if (bo
->resv
== &bo
->ttm_resv
)
352 BUG_ON(!reservation_object_trylock(&bo
->ttm_resv
));
354 r
= reservation_object_copy_fences(&bo
->ttm_resv
, bo
->resv
);
356 reservation_object_unlock(&bo
->ttm_resv
);
361 static void ttm_bo_flush_all_fences(struct ttm_buffer_object
*bo
)
363 struct reservation_object_list
*fobj
;
364 struct dma_fence
*fence
;
367 fobj
= reservation_object_get_list(&bo
->ttm_resv
);
368 fence
= reservation_object_get_excl(&bo
->ttm_resv
);
369 if (fence
&& !fence
->ops
->signaled
)
370 dma_fence_enable_sw_signaling(fence
);
372 for (i
= 0; fobj
&& i
< fobj
->shared_count
; ++i
) {
373 fence
= rcu_dereference_protected(fobj
->shared
[i
],
374 reservation_object_held(bo
->resv
));
376 if (!fence
->ops
->signaled
)
377 dma_fence_enable_sw_signaling(fence
);
381 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object
*bo
)
383 struct ttm_bo_device
*bdev
= bo
->bdev
;
384 struct ttm_bo_global
*glob
= bdev
->glob
;
387 ret
= ttm_bo_individualize_resv(bo
);
389 /* Last resort, if we fail to allocate memory for the
390 * fences block for the BO to become idle
392 reservation_object_wait_timeout_rcu(bo
->resv
, true, false,
394 spin_lock(&glob
->lru_lock
);
398 spin_lock(&glob
->lru_lock
);
399 ret
= reservation_object_trylock(bo
->resv
) ? 0 : -EBUSY
;
401 if (reservation_object_test_signaled_rcu(&bo
->ttm_resv
, true)) {
402 ttm_bo_del_from_lru(bo
);
403 spin_unlock(&glob
->lru_lock
);
404 if (bo
->resv
!= &bo
->ttm_resv
)
405 reservation_object_unlock(&bo
->ttm_resv
);
407 ttm_bo_cleanup_memtype_use(bo
);
408 reservation_object_unlock(bo
->resv
);
412 ttm_bo_flush_all_fences(bo
);
415 * Make NO_EVICT bos immediately available to
416 * shrinkers, now that they are queued for
419 if (bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
) {
420 bo
->mem
.placement
&= ~TTM_PL_FLAG_NO_EVICT
;
421 ttm_bo_add_to_lru(bo
);
424 reservation_object_unlock(bo
->resv
);
426 if (bo
->resv
!= &bo
->ttm_resv
)
427 reservation_object_unlock(&bo
->ttm_resv
);
430 kref_get(&bo
->list_kref
);
431 list_add_tail(&bo
->ddestroy
, &bdev
->ddestroy
);
432 spin_unlock(&glob
->lru_lock
);
434 schedule_delayed_work(&bdev
->wq
,
435 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
439 * function ttm_bo_cleanup_refs
440 * If bo idle, remove from delayed- and lru lists, and unref.
441 * If not idle, do nothing.
443 * Must be called with lru_lock and reservation held, this function
444 * will drop the lru lock and optionally the reservation lock before returning.
446 * @interruptible Any sleeps should occur interruptibly.
447 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
448 * @unlock_resv Unlock the reservation lock as well.
451 static int ttm_bo_cleanup_refs(struct ttm_buffer_object
*bo
,
452 bool interruptible
, bool no_wait_gpu
,
455 struct ttm_bo_global
*glob
= bo
->bdev
->glob
;
456 struct reservation_object
*resv
;
459 if (unlikely(list_empty(&bo
->ddestroy
)))
462 resv
= &bo
->ttm_resv
;
464 if (reservation_object_test_signaled_rcu(resv
, true))
469 if (ret
&& !no_wait_gpu
) {
473 reservation_object_unlock(bo
->resv
);
474 spin_unlock(&glob
->lru_lock
);
476 lret
= reservation_object_wait_timeout_rcu(resv
, true,
485 spin_lock(&glob
->lru_lock
);
486 if (unlock_resv
&& !reservation_object_trylock(bo
->resv
)) {
488 * We raced, and lost, someone else holds the reservation now,
489 * and is probably busy in ttm_bo_cleanup_memtype_use.
491 * Even if it's not the case, because we finished waiting any
492 * delayed destruction would succeed, so just return success
495 spin_unlock(&glob
->lru_lock
);
501 if (ret
|| unlikely(list_empty(&bo
->ddestroy
))) {
503 reservation_object_unlock(bo
->resv
);
504 spin_unlock(&glob
->lru_lock
);
508 ttm_bo_del_from_lru(bo
);
509 list_del_init(&bo
->ddestroy
);
510 kref_put(&bo
->list_kref
, ttm_bo_ref_bug
);
512 spin_unlock(&glob
->lru_lock
);
513 ttm_bo_cleanup_memtype_use(bo
);
516 reservation_object_unlock(bo
->resv
);
522 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
523 * encountered buffers.
525 static bool ttm_bo_delayed_delete(struct ttm_bo_device
*bdev
, bool remove_all
)
527 struct ttm_bo_global
*glob
= bdev
->glob
;
528 struct list_head removed
;
531 INIT_LIST_HEAD(&removed
);
533 spin_lock(&glob
->lru_lock
);
534 while (!list_empty(&bdev
->ddestroy
)) {
535 struct ttm_buffer_object
*bo
;
537 bo
= list_first_entry(&bdev
->ddestroy
, struct ttm_buffer_object
,
539 kref_get(&bo
->list_kref
);
540 list_move_tail(&bo
->ddestroy
, &removed
);
542 if (remove_all
|| bo
->resv
!= &bo
->ttm_resv
) {
543 spin_unlock(&glob
->lru_lock
);
544 reservation_object_lock(bo
->resv
, NULL
);
546 spin_lock(&glob
->lru_lock
);
547 ttm_bo_cleanup_refs(bo
, false, !remove_all
, true);
549 } else if (reservation_object_trylock(bo
->resv
)) {
550 ttm_bo_cleanup_refs(bo
, false, !remove_all
, true);
552 spin_unlock(&glob
->lru_lock
);
555 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
556 spin_lock(&glob
->lru_lock
);
558 list_splice_tail(&removed
, &bdev
->ddestroy
);
559 empty
= list_empty(&bdev
->ddestroy
);
560 spin_unlock(&glob
->lru_lock
);
565 static void ttm_bo_delayed_workqueue(struct work_struct
*work
)
567 struct ttm_bo_device
*bdev
=
568 container_of(work
, struct ttm_bo_device
, wq
.work
);
570 if (!ttm_bo_delayed_delete(bdev
, false))
571 schedule_delayed_work(&bdev
->wq
,
572 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
575 static void ttm_bo_release(struct kref
*kref
)
577 struct ttm_buffer_object
*bo
=
578 container_of(kref
, struct ttm_buffer_object
, kref
);
579 struct ttm_bo_device
*bdev
= bo
->bdev
;
580 struct ttm_mem_type_manager
*man
= &bdev
->man
[bo
->mem
.mem_type
];
582 drm_vma_offset_remove(&bdev
->vma_manager
, &bo
->vma_node
);
583 ttm_mem_io_lock(man
, false);
584 ttm_mem_io_free_vm(bo
);
585 ttm_mem_io_unlock(man
);
586 ttm_bo_cleanup_refs_or_queue(bo
);
587 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
590 void ttm_bo_put(struct ttm_buffer_object
*bo
)
592 kref_put(&bo
->kref
, ttm_bo_release
);
594 EXPORT_SYMBOL(ttm_bo_put
);
596 void ttm_bo_unref(struct ttm_buffer_object
**p_bo
)
598 struct ttm_buffer_object
*bo
= *p_bo
;
603 EXPORT_SYMBOL(ttm_bo_unref
);
605 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device
*bdev
)
607 return cancel_delayed_work_sync(&bdev
->wq
);
609 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue
);
611 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device
*bdev
, int resched
)
614 schedule_delayed_work(&bdev
->wq
,
615 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
617 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue
);
619 static int ttm_bo_evict(struct ttm_buffer_object
*bo
,
620 struct ttm_operation_ctx
*ctx
)
622 struct ttm_bo_device
*bdev
= bo
->bdev
;
623 struct ttm_mem_reg evict_mem
;
624 struct ttm_placement placement
;
627 reservation_object_assert_held(bo
->resv
);
629 placement
.num_placement
= 0;
630 placement
.num_busy_placement
= 0;
631 bdev
->driver
->evict_flags(bo
, &placement
);
633 if (!placement
.num_placement
&& !placement
.num_busy_placement
) {
634 ret
= ttm_bo_pipeline_gutting(bo
);
638 return ttm_tt_create(bo
, false);
642 evict_mem
.mm_node
= NULL
;
643 evict_mem
.bus
.io_reserved_vm
= false;
644 evict_mem
.bus
.io_reserved_count
= 0;
646 ret
= ttm_bo_mem_space(bo
, &placement
, &evict_mem
, ctx
);
648 if (ret
!= -ERESTARTSYS
) {
649 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
651 ttm_bo_mem_space_debug(bo
, &placement
);
656 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true, ctx
);
658 if (ret
!= -ERESTARTSYS
)
659 pr_err("Buffer eviction failed\n");
660 ttm_bo_mem_put(bo
, &evict_mem
);
668 bool ttm_bo_eviction_valuable(struct ttm_buffer_object
*bo
,
669 const struct ttm_place
*place
)
671 /* Don't evict this BO if it's outside of the
672 * requested placement range
674 if (place
->fpfn
>= (bo
->mem
.start
+ bo
->mem
.size
) ||
675 (place
->lpfn
&& place
->lpfn
<= bo
->mem
.start
))
680 EXPORT_SYMBOL(ttm_bo_eviction_valuable
);
683 * Check the target bo is allowable to be evicted or swapout, including cases:
685 * a. if share same reservation object with ctx->resv, have assumption
686 * reservation objects should already be locked, so not lock again and
687 * return true directly when either the opreation allow_reserved_eviction
688 * or the target bo already is in delayed free list;
690 * b. Otherwise, trylock it.
692 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object
*bo
,
693 struct ttm_operation_ctx
*ctx
, bool *locked
)
698 if (bo
->resv
== ctx
->resv
) {
699 reservation_object_assert_held(bo
->resv
);
700 if (ctx
->flags
& TTM_OPT_FLAG_ALLOW_RES_EVICT
701 || !list_empty(&bo
->ddestroy
))
704 *locked
= reservation_object_trylock(bo
->resv
);
711 static int ttm_mem_evict_first(struct ttm_bo_device
*bdev
,
713 const struct ttm_place
*place
,
714 struct ttm_operation_ctx
*ctx
)
716 struct ttm_bo_global
*glob
= bdev
->glob
;
717 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
718 struct ttm_buffer_object
*bo
= NULL
;
723 spin_lock(&glob
->lru_lock
);
724 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
725 list_for_each_entry(bo
, &man
->lru
[i
], lru
) {
726 if (!ttm_bo_evict_swapout_allowable(bo
, ctx
, &locked
))
729 if (place
&& !bdev
->driver
->eviction_valuable(bo
,
732 reservation_object_unlock(bo
->resv
);
738 /* If the inner loop terminated early, we have our candidate */
739 if (&bo
->lru
!= &man
->lru
[i
])
746 spin_unlock(&glob
->lru_lock
);
750 kref_get(&bo
->list_kref
);
752 if (!list_empty(&bo
->ddestroy
)) {
753 ret
= ttm_bo_cleanup_refs(bo
, ctx
->interruptible
,
754 ctx
->no_wait_gpu
, locked
);
755 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
759 ttm_bo_del_from_lru(bo
);
760 spin_unlock(&glob
->lru_lock
);
762 ret
= ttm_bo_evict(bo
, ctx
);
764 ttm_bo_unreserve(bo
);
766 spin_lock(&glob
->lru_lock
);
767 ttm_bo_add_to_lru(bo
);
768 spin_unlock(&glob
->lru_lock
);
771 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
775 void ttm_bo_mem_put(struct ttm_buffer_object
*bo
, struct ttm_mem_reg
*mem
)
777 struct ttm_mem_type_manager
*man
= &bo
->bdev
->man
[mem
->mem_type
];
780 (*man
->func
->put_node
)(man
, mem
);
782 EXPORT_SYMBOL(ttm_bo_mem_put
);
785 * Add the last move fence to the BO and reserve a new shared slot.
787 static int ttm_bo_add_move_fence(struct ttm_buffer_object
*bo
,
788 struct ttm_mem_type_manager
*man
,
789 struct ttm_mem_reg
*mem
)
791 struct dma_fence
*fence
;
794 spin_lock(&man
->move_lock
);
795 fence
= dma_fence_get(man
->move
);
796 spin_unlock(&man
->move_lock
);
799 reservation_object_add_shared_fence(bo
->resv
, fence
);
801 ret
= reservation_object_reserve_shared(bo
->resv
);
805 dma_fence_put(bo
->moving
);
813 * Repeatedly evict memory from the LRU for @mem_type until we create enough
814 * space, or we've evicted everything and there isn't enough space.
816 static int ttm_bo_mem_force_space(struct ttm_buffer_object
*bo
,
818 const struct ttm_place
*place
,
819 struct ttm_mem_reg
*mem
,
820 struct ttm_operation_ctx
*ctx
)
822 struct ttm_bo_device
*bdev
= bo
->bdev
;
823 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
827 ret
= (*man
->func
->get_node
)(man
, bo
, place
, mem
);
828 if (unlikely(ret
!= 0))
832 ret
= ttm_mem_evict_first(bdev
, mem_type
, place
, ctx
);
833 if (unlikely(ret
!= 0))
836 mem
->mem_type
= mem_type
;
837 return ttm_bo_add_move_fence(bo
, man
, mem
);
840 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager
*man
,
841 uint32_t cur_placement
,
842 uint32_t proposed_placement
)
844 uint32_t caching
= proposed_placement
& TTM_PL_MASK_CACHING
;
845 uint32_t result
= proposed_placement
& ~TTM_PL_MASK_CACHING
;
848 * Keep current caching if possible.
851 if ((cur_placement
& caching
) != 0)
852 result
|= (cur_placement
& caching
);
853 else if ((man
->default_caching
& caching
) != 0)
854 result
|= man
->default_caching
;
855 else if ((TTM_PL_FLAG_CACHED
& caching
) != 0)
856 result
|= TTM_PL_FLAG_CACHED
;
857 else if ((TTM_PL_FLAG_WC
& caching
) != 0)
858 result
|= TTM_PL_FLAG_WC
;
859 else if ((TTM_PL_FLAG_UNCACHED
& caching
) != 0)
860 result
|= TTM_PL_FLAG_UNCACHED
;
865 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager
*man
,
867 const struct ttm_place
*place
,
868 uint32_t *masked_placement
)
870 uint32_t cur_flags
= ttm_bo_type_flags(mem_type
);
872 if ((cur_flags
& place
->flags
& TTM_PL_MASK_MEM
) == 0)
875 if ((place
->flags
& man
->available_caching
) == 0)
878 cur_flags
|= (place
->flags
& man
->available_caching
);
880 *masked_placement
= cur_flags
;
885 * Creates space for memory region @mem according to its type.
887 * This function first searches for free space in compatible memory types in
888 * the priority order defined by the driver. If free space isn't found, then
889 * ttm_bo_mem_force_space is attempted in priority order to evict and find
892 int ttm_bo_mem_space(struct ttm_buffer_object
*bo
,
893 struct ttm_placement
*placement
,
894 struct ttm_mem_reg
*mem
,
895 struct ttm_operation_ctx
*ctx
)
897 struct ttm_bo_device
*bdev
= bo
->bdev
;
898 struct ttm_mem_type_manager
*man
;
899 uint32_t mem_type
= TTM_PL_SYSTEM
;
900 uint32_t cur_flags
= 0;
901 bool type_found
= false;
902 bool type_ok
= false;
903 bool has_erestartsys
= false;
906 ret
= reservation_object_reserve_shared(bo
->resv
);
911 for (i
= 0; i
< placement
->num_placement
; ++i
) {
912 const struct ttm_place
*place
= &placement
->placement
[i
];
914 ret
= ttm_mem_type_from_place(place
, &mem_type
);
917 man
= &bdev
->man
[mem_type
];
918 if (!man
->has_type
|| !man
->use_type
)
921 type_ok
= ttm_bo_mt_compatible(man
, mem_type
, place
,
928 cur_flags
= ttm_bo_select_caching(man
, bo
->mem
.placement
,
931 * Use the access and other non-mapping-related flag bits from
932 * the memory placement flags to the current flags
934 ttm_flag_masked(&cur_flags
, place
->flags
,
935 ~TTM_PL_MASK_MEMTYPE
);
937 if (mem_type
== TTM_PL_SYSTEM
)
940 ret
= (*man
->func
->get_node
)(man
, bo
, place
, mem
);
945 ret
= ttm_bo_add_move_fence(bo
, man
, mem
);
947 (*man
->func
->put_node
)(man
, mem
);
954 if ((type_ok
&& (mem_type
== TTM_PL_SYSTEM
)) || mem
->mm_node
) {
955 mem
->mem_type
= mem_type
;
956 mem
->placement
= cur_flags
;
960 for (i
= 0; i
< placement
->num_busy_placement
; ++i
) {
961 const struct ttm_place
*place
= &placement
->busy_placement
[i
];
963 ret
= ttm_mem_type_from_place(place
, &mem_type
);
966 man
= &bdev
->man
[mem_type
];
967 if (!man
->has_type
|| !man
->use_type
)
969 if (!ttm_bo_mt_compatible(man
, mem_type
, place
, &cur_flags
))
973 cur_flags
= ttm_bo_select_caching(man
, bo
->mem
.placement
,
976 * Use the access and other non-mapping-related flag bits from
977 * the memory placement flags to the current flags
979 ttm_flag_masked(&cur_flags
, place
->flags
,
980 ~TTM_PL_MASK_MEMTYPE
);
982 if (mem_type
== TTM_PL_SYSTEM
) {
983 mem
->mem_type
= mem_type
;
984 mem
->placement
= cur_flags
;
989 ret
= ttm_bo_mem_force_space(bo
, mem_type
, place
, mem
, ctx
);
990 if (ret
== 0 && mem
->mm_node
) {
991 mem
->placement
= cur_flags
;
994 if (ret
== -ERESTARTSYS
)
995 has_erestartsys
= true;
999 pr_err(TTM_PFX
"No compatible memory type found\n");
1003 return (has_erestartsys
) ? -ERESTARTSYS
: -ENOMEM
;
1005 EXPORT_SYMBOL(ttm_bo_mem_space
);
1007 static int ttm_bo_move_buffer(struct ttm_buffer_object
*bo
,
1008 struct ttm_placement
*placement
,
1009 struct ttm_operation_ctx
*ctx
)
1012 struct ttm_mem_reg mem
;
1014 reservation_object_assert_held(bo
->resv
);
1016 mem
.num_pages
= bo
->num_pages
;
1017 mem
.size
= mem
.num_pages
<< PAGE_SHIFT
;
1018 mem
.page_alignment
= bo
->mem
.page_alignment
;
1019 mem
.bus
.io_reserved_vm
= false;
1020 mem
.bus
.io_reserved_count
= 0;
1022 * Determine where to move the buffer.
1024 ret
= ttm_bo_mem_space(bo
, placement
, &mem
, ctx
);
1027 ret
= ttm_bo_handle_move_mem(bo
, &mem
, false, ctx
);
1029 if (ret
&& mem
.mm_node
)
1030 ttm_bo_mem_put(bo
, &mem
);
1034 static bool ttm_bo_places_compat(const struct ttm_place
*places
,
1035 unsigned num_placement
,
1036 struct ttm_mem_reg
*mem
,
1037 uint32_t *new_flags
)
1041 for (i
= 0; i
< num_placement
; i
++) {
1042 const struct ttm_place
*heap
= &places
[i
];
1044 if (mem
->mm_node
&& (mem
->start
< heap
->fpfn
||
1045 (heap
->lpfn
!= 0 && (mem
->start
+ mem
->num_pages
) > heap
->lpfn
)))
1048 *new_flags
= heap
->flags
;
1049 if ((*new_flags
& mem
->placement
& TTM_PL_MASK_CACHING
) &&
1050 (*new_flags
& mem
->placement
& TTM_PL_MASK_MEM
) &&
1051 (!(*new_flags
& TTM_PL_FLAG_CONTIGUOUS
) ||
1052 (mem
->placement
& TTM_PL_FLAG_CONTIGUOUS
)))
1058 bool ttm_bo_mem_compat(struct ttm_placement
*placement
,
1059 struct ttm_mem_reg
*mem
,
1060 uint32_t *new_flags
)
1062 if (ttm_bo_places_compat(placement
->placement
, placement
->num_placement
,
1066 if ((placement
->busy_placement
!= placement
->placement
||
1067 placement
->num_busy_placement
> placement
->num_placement
) &&
1068 ttm_bo_places_compat(placement
->busy_placement
,
1069 placement
->num_busy_placement
,
1075 EXPORT_SYMBOL(ttm_bo_mem_compat
);
1077 int ttm_bo_validate(struct ttm_buffer_object
*bo
,
1078 struct ttm_placement
*placement
,
1079 struct ttm_operation_ctx
*ctx
)
1084 reservation_object_assert_held(bo
->resv
);
1086 * Check whether we need to move buffer.
1088 if (!ttm_bo_mem_compat(placement
, &bo
->mem
, &new_flags
)) {
1089 ret
= ttm_bo_move_buffer(bo
, placement
, ctx
);
1094 * Use the access and other non-mapping-related flag bits from
1095 * the compatible memory placement flags to the active flags
1097 ttm_flag_masked(&bo
->mem
.placement
, new_flags
,
1098 ~TTM_PL_MASK_MEMTYPE
);
1101 * We might need to add a TTM.
1103 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
&& bo
->ttm
== NULL
) {
1104 ret
= ttm_tt_create(bo
, true);
1110 EXPORT_SYMBOL(ttm_bo_validate
);
1112 int ttm_bo_init_reserved(struct ttm_bo_device
*bdev
,
1113 struct ttm_buffer_object
*bo
,
1115 enum ttm_bo_type type
,
1116 struct ttm_placement
*placement
,
1117 uint32_t page_alignment
,
1118 struct ttm_operation_ctx
*ctx
,
1120 struct sg_table
*sg
,
1121 struct reservation_object
*resv
,
1122 void (*destroy
) (struct ttm_buffer_object
*))
1125 unsigned long num_pages
;
1126 struct ttm_mem_global
*mem_glob
= bdev
->glob
->mem_glob
;
1129 ret
= ttm_mem_global_alloc(mem_glob
, acc_size
, ctx
);
1131 pr_err("Out of kernel memory\n");
1139 num_pages
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1140 if (num_pages
== 0) {
1141 pr_err("Illegal buffer object size\n");
1146 ttm_mem_global_free(mem_glob
, acc_size
);
1149 bo
->destroy
= destroy
? destroy
: ttm_bo_default_destroy
;
1151 kref_init(&bo
->kref
);
1152 kref_init(&bo
->list_kref
);
1153 atomic_set(&bo
->cpu_writers
, 0);
1154 INIT_LIST_HEAD(&bo
->lru
);
1155 INIT_LIST_HEAD(&bo
->ddestroy
);
1156 INIT_LIST_HEAD(&bo
->swap
);
1157 INIT_LIST_HEAD(&bo
->io_reserve_lru
);
1158 mutex_init(&bo
->wu_mutex
);
1161 bo
->num_pages
= num_pages
;
1162 bo
->mem
.size
= num_pages
<< PAGE_SHIFT
;
1163 bo
->mem
.mem_type
= TTM_PL_SYSTEM
;
1164 bo
->mem
.num_pages
= bo
->num_pages
;
1165 bo
->mem
.mm_node
= NULL
;
1166 bo
->mem
.page_alignment
= page_alignment
;
1167 bo
->mem
.bus
.io_reserved_vm
= false;
1168 bo
->mem
.bus
.io_reserved_count
= 0;
1170 bo
->mem
.placement
= (TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
);
1171 bo
->acc_size
= acc_size
;
1175 reservation_object_assert_held(bo
->resv
);
1177 bo
->resv
= &bo
->ttm_resv
;
1179 reservation_object_init(&bo
->ttm_resv
);
1180 atomic_inc(&bo
->bdev
->glob
->bo_count
);
1181 drm_vma_node_reset(&bo
->vma_node
);
1184 * For ttm_bo_type_device buffers, allocate
1185 * address space from the device.
1187 if (bo
->type
== ttm_bo_type_device
||
1188 bo
->type
== ttm_bo_type_sg
)
1189 ret
= drm_vma_offset_add(&bdev
->vma_manager
, &bo
->vma_node
,
1192 /* passed reservation objects should already be locked,
1193 * since otherwise lockdep will be angered in radeon.
1196 locked
= reservation_object_trylock(bo
->resv
);
1201 ret
= ttm_bo_validate(bo
, placement
, ctx
);
1203 if (unlikely(ret
)) {
1205 ttm_bo_unreserve(bo
);
1211 if (resv
&& !(bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
)) {
1212 spin_lock(&bdev
->glob
->lru_lock
);
1213 ttm_bo_add_to_lru(bo
);
1214 spin_unlock(&bdev
->glob
->lru_lock
);
1219 EXPORT_SYMBOL(ttm_bo_init_reserved
);
1221 int ttm_bo_init(struct ttm_bo_device
*bdev
,
1222 struct ttm_buffer_object
*bo
,
1224 enum ttm_bo_type type
,
1225 struct ttm_placement
*placement
,
1226 uint32_t page_alignment
,
1229 struct sg_table
*sg
,
1230 struct reservation_object
*resv
,
1231 void (*destroy
) (struct ttm_buffer_object
*))
1233 struct ttm_operation_ctx ctx
= { interruptible
, false };
1236 ret
= ttm_bo_init_reserved(bdev
, bo
, size
, type
, placement
,
1237 page_alignment
, &ctx
, acc_size
,
1243 ttm_bo_unreserve(bo
);
1247 EXPORT_SYMBOL(ttm_bo_init
);
1249 size_t ttm_bo_acc_size(struct ttm_bo_device
*bdev
,
1250 unsigned long bo_size
,
1251 unsigned struct_size
)
1253 unsigned npages
= (PAGE_ALIGN(bo_size
)) >> PAGE_SHIFT
;
1256 size
+= ttm_round_pot(struct_size
);
1257 size
+= ttm_round_pot(npages
* sizeof(void *));
1258 size
+= ttm_round_pot(sizeof(struct ttm_tt
));
1261 EXPORT_SYMBOL(ttm_bo_acc_size
);
1263 size_t ttm_bo_dma_acc_size(struct ttm_bo_device
*bdev
,
1264 unsigned long bo_size
,
1265 unsigned struct_size
)
1267 unsigned npages
= (PAGE_ALIGN(bo_size
)) >> PAGE_SHIFT
;
1270 size
+= ttm_round_pot(struct_size
);
1271 size
+= ttm_round_pot(npages
* (2*sizeof(void *) + sizeof(dma_addr_t
)));
1272 size
+= ttm_round_pot(sizeof(struct ttm_dma_tt
));
1275 EXPORT_SYMBOL(ttm_bo_dma_acc_size
);
1277 int ttm_bo_create(struct ttm_bo_device
*bdev
,
1279 enum ttm_bo_type type
,
1280 struct ttm_placement
*placement
,
1281 uint32_t page_alignment
,
1283 struct ttm_buffer_object
**p_bo
)
1285 struct ttm_buffer_object
*bo
;
1289 bo
= kzalloc(sizeof(*bo
), GFP_KERNEL
);
1290 if (unlikely(bo
== NULL
))
1293 acc_size
= ttm_bo_acc_size(bdev
, size
, sizeof(struct ttm_buffer_object
));
1294 ret
= ttm_bo_init(bdev
, bo
, size
, type
, placement
, page_alignment
,
1295 interruptible
, acc_size
,
1297 if (likely(ret
== 0))
1302 EXPORT_SYMBOL(ttm_bo_create
);
1304 static int ttm_bo_force_list_clean(struct ttm_bo_device
*bdev
,
1307 struct ttm_operation_ctx ctx
= {
1308 .interruptible
= false,
1309 .no_wait_gpu
= false,
1310 .flags
= TTM_OPT_FLAG_FORCE_ALLOC
1312 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1313 struct ttm_bo_global
*glob
= bdev
->glob
;
1314 struct dma_fence
*fence
;
1319 * Can't use standard list traversal since we're unlocking.
1322 spin_lock(&glob
->lru_lock
);
1323 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
1324 while (!list_empty(&man
->lru
[i
])) {
1325 spin_unlock(&glob
->lru_lock
);
1326 ret
= ttm_mem_evict_first(bdev
, mem_type
, NULL
, &ctx
);
1329 spin_lock(&glob
->lru_lock
);
1332 spin_unlock(&glob
->lru_lock
);
1334 spin_lock(&man
->move_lock
);
1335 fence
= dma_fence_get(man
->move
);
1336 spin_unlock(&man
->move_lock
);
1339 ret
= dma_fence_wait(fence
, false);
1340 dma_fence_put(fence
);
1348 int ttm_bo_clean_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1350 struct ttm_mem_type_manager
*man
;
1353 if (mem_type
>= TTM_NUM_MEM_TYPES
) {
1354 pr_err("Illegal memory type %d\n", mem_type
);
1357 man
= &bdev
->man
[mem_type
];
1359 if (!man
->has_type
) {
1360 pr_err("Trying to take down uninitialized memory manager type %u\n",
1365 man
->use_type
= false;
1366 man
->has_type
= false;
1370 ret
= ttm_bo_force_list_clean(bdev
, mem_type
);
1372 pr_err("Cleanup eviction failed\n");
1376 ret
= (*man
->func
->takedown
)(man
);
1379 dma_fence_put(man
->move
);
1384 EXPORT_SYMBOL(ttm_bo_clean_mm
);
1386 int ttm_bo_evict_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1388 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1390 if (mem_type
== 0 || mem_type
>= TTM_NUM_MEM_TYPES
) {
1391 pr_err("Illegal memory manager memory type %u\n", mem_type
);
1395 if (!man
->has_type
) {
1396 pr_err("Memory type %u has not been initialized\n", mem_type
);
1400 return ttm_bo_force_list_clean(bdev
, mem_type
);
1402 EXPORT_SYMBOL(ttm_bo_evict_mm
);
1404 int ttm_bo_init_mm(struct ttm_bo_device
*bdev
, unsigned type
,
1405 unsigned long p_size
)
1408 struct ttm_mem_type_manager
*man
;
1411 BUG_ON(type
>= TTM_NUM_MEM_TYPES
);
1412 man
= &bdev
->man
[type
];
1413 BUG_ON(man
->has_type
);
1414 man
->io_reserve_fastpath
= true;
1415 man
->use_io_reserve_lru
= false;
1416 mutex_init(&man
->io_reserve_mutex
);
1417 spin_lock_init(&man
->move_lock
);
1418 INIT_LIST_HEAD(&man
->io_reserve_lru
);
1420 ret
= bdev
->driver
->init_mem_type(bdev
, type
, man
);
1425 if (type
!= TTM_PL_SYSTEM
) {
1426 ret
= (*man
->func
->init
)(man
, p_size
);
1430 man
->has_type
= true;
1431 man
->use_type
= true;
1434 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
)
1435 INIT_LIST_HEAD(&man
->lru
[i
]);
1440 EXPORT_SYMBOL(ttm_bo_init_mm
);
1442 static void ttm_bo_global_kobj_release(struct kobject
*kobj
)
1444 struct ttm_bo_global
*glob
=
1445 container_of(kobj
, struct ttm_bo_global
, kobj
);
1447 __free_page(glob
->dummy_read_page
);
1450 void ttm_bo_global_release(struct drm_global_reference
*ref
)
1452 struct ttm_bo_global
*glob
= ref
->object
;
1454 kobject_del(&glob
->kobj
);
1455 kobject_put(&glob
->kobj
);
1457 EXPORT_SYMBOL(ttm_bo_global_release
);
1459 int ttm_bo_global_init(struct drm_global_reference
*ref
)
1461 struct ttm_bo_global_ref
*bo_ref
=
1462 container_of(ref
, struct ttm_bo_global_ref
, ref
);
1463 struct ttm_bo_global
*glob
= ref
->object
;
1467 mutex_init(&glob
->device_list_mutex
);
1468 spin_lock_init(&glob
->lru_lock
);
1469 glob
->mem_glob
= bo_ref
->mem_glob
;
1470 glob
->mem_glob
->bo_glob
= glob
;
1471 glob
->dummy_read_page
= alloc_page(__GFP_ZERO
| GFP_DMA32
);
1473 if (unlikely(glob
->dummy_read_page
== NULL
)) {
1478 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
)
1479 INIT_LIST_HEAD(&glob
->swap_lru
[i
]);
1480 INIT_LIST_HEAD(&glob
->device_list
);
1481 atomic_set(&glob
->bo_count
, 0);
1483 ret
= kobject_init_and_add(
1484 &glob
->kobj
, &ttm_bo_glob_kobj_type
, ttm_get_kobj(), "buffer_objects");
1485 if (unlikely(ret
!= 0))
1486 kobject_put(&glob
->kobj
);
1492 EXPORT_SYMBOL(ttm_bo_global_init
);
1495 int ttm_bo_device_release(struct ttm_bo_device
*bdev
)
1498 unsigned i
= TTM_NUM_MEM_TYPES
;
1499 struct ttm_mem_type_manager
*man
;
1500 struct ttm_bo_global
*glob
= bdev
->glob
;
1503 man
= &bdev
->man
[i
];
1504 if (man
->has_type
) {
1505 man
->use_type
= false;
1506 if ((i
!= TTM_PL_SYSTEM
) && ttm_bo_clean_mm(bdev
, i
)) {
1508 pr_err("DRM memory manager type %d is not clean\n",
1511 man
->has_type
= false;
1515 mutex_lock(&glob
->device_list_mutex
);
1516 list_del(&bdev
->device_list
);
1517 mutex_unlock(&glob
->device_list_mutex
);
1519 cancel_delayed_work_sync(&bdev
->wq
);
1521 if (ttm_bo_delayed_delete(bdev
, true))
1522 pr_debug("Delayed destroy list was clean\n");
1524 spin_lock(&glob
->lru_lock
);
1525 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
)
1526 if (list_empty(&bdev
->man
[0].lru
[0]))
1527 pr_debug("Swap list %d was clean\n", i
);
1528 spin_unlock(&glob
->lru_lock
);
1530 drm_vma_offset_manager_destroy(&bdev
->vma_manager
);
1534 EXPORT_SYMBOL(ttm_bo_device_release
);
1536 int ttm_bo_device_init(struct ttm_bo_device
*bdev
,
1537 struct ttm_bo_global
*glob
,
1538 struct ttm_bo_driver
*driver
,
1539 struct address_space
*mapping
,
1540 uint64_t file_page_offset
,
1545 bdev
->driver
= driver
;
1547 memset(bdev
->man
, 0, sizeof(bdev
->man
));
1550 * Initialize the system memory buffer type.
1551 * Other types need to be driver / IOCTL initialized.
1553 ret
= ttm_bo_init_mm(bdev
, TTM_PL_SYSTEM
, 0);
1554 if (unlikely(ret
!= 0))
1557 drm_vma_offset_manager_init(&bdev
->vma_manager
, file_page_offset
,
1559 INIT_DELAYED_WORK(&bdev
->wq
, ttm_bo_delayed_workqueue
);
1560 INIT_LIST_HEAD(&bdev
->ddestroy
);
1561 bdev
->dev_mapping
= mapping
;
1563 bdev
->need_dma32
= need_dma32
;
1564 mutex_lock(&glob
->device_list_mutex
);
1565 list_add_tail(&bdev
->device_list
, &glob
->device_list
);
1566 mutex_unlock(&glob
->device_list_mutex
);
1572 EXPORT_SYMBOL(ttm_bo_device_init
);
1575 * buffer object vm functions.
1578 bool ttm_mem_reg_is_pci(struct ttm_bo_device
*bdev
, struct ttm_mem_reg
*mem
)
1580 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem
->mem_type
];
1582 if (!(man
->flags
& TTM_MEMTYPE_FLAG_FIXED
)) {
1583 if (mem
->mem_type
== TTM_PL_SYSTEM
)
1586 if (man
->flags
& TTM_MEMTYPE_FLAG_CMA
)
1589 if (mem
->placement
& TTM_PL_FLAG_CACHED
)
1595 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object
*bo
)
1597 struct ttm_bo_device
*bdev
= bo
->bdev
;
1599 drm_vma_node_unmap(&bo
->vma_node
, bdev
->dev_mapping
);
1600 ttm_mem_io_free_vm(bo
);
1603 void ttm_bo_unmap_virtual(struct ttm_buffer_object
*bo
)
1605 struct ttm_bo_device
*bdev
= bo
->bdev
;
1606 struct ttm_mem_type_manager
*man
= &bdev
->man
[bo
->mem
.mem_type
];
1608 ttm_mem_io_lock(man
, false);
1609 ttm_bo_unmap_virtual_locked(bo
);
1610 ttm_mem_io_unlock(man
);
1614 EXPORT_SYMBOL(ttm_bo_unmap_virtual
);
1616 int ttm_bo_wait(struct ttm_buffer_object
*bo
,
1617 bool interruptible
, bool no_wait
)
1619 long timeout
= 15 * HZ
;
1622 if (reservation_object_test_signaled_rcu(bo
->resv
, true))
1628 timeout
= reservation_object_wait_timeout_rcu(bo
->resv
, true,
1629 interruptible
, timeout
);
1636 reservation_object_add_excl_fence(bo
->resv
, NULL
);
1639 EXPORT_SYMBOL(ttm_bo_wait
);
1641 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object
*bo
, bool no_wait
)
1646 * Using ttm_bo_reserve makes sure the lru lists are updated.
1649 ret
= ttm_bo_reserve(bo
, true, no_wait
, NULL
);
1650 if (unlikely(ret
!= 0))
1652 ret
= ttm_bo_wait(bo
, true, no_wait
);
1653 if (likely(ret
== 0))
1654 atomic_inc(&bo
->cpu_writers
);
1655 ttm_bo_unreserve(bo
);
1658 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab
);
1660 void ttm_bo_synccpu_write_release(struct ttm_buffer_object
*bo
)
1662 atomic_dec(&bo
->cpu_writers
);
1664 EXPORT_SYMBOL(ttm_bo_synccpu_write_release
);
1667 * A buffer object shrink method that tries to swap out the first
1668 * buffer object on the bo_global::swap_lru list.
1670 int ttm_bo_swapout(struct ttm_bo_global
*glob
, struct ttm_operation_ctx
*ctx
)
1672 struct ttm_buffer_object
*bo
;
1677 spin_lock(&glob
->lru_lock
);
1678 for (i
= 0; i
< TTM_MAX_BO_PRIORITY
; ++i
) {
1679 list_for_each_entry(bo
, &glob
->swap_lru
[i
], swap
) {
1680 if (ttm_bo_evict_swapout_allowable(bo
, ctx
, &locked
)) {
1690 spin_unlock(&glob
->lru_lock
);
1694 kref_get(&bo
->list_kref
);
1696 if (!list_empty(&bo
->ddestroy
)) {
1697 ret
= ttm_bo_cleanup_refs(bo
, false, false, locked
);
1698 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1702 ttm_bo_del_from_lru(bo
);
1703 spin_unlock(&glob
->lru_lock
);
1706 * Move to system cached
1709 if (bo
->mem
.mem_type
!= TTM_PL_SYSTEM
||
1710 bo
->ttm
->caching_state
!= tt_cached
) {
1711 struct ttm_operation_ctx ctx
= { false, false };
1712 struct ttm_mem_reg evict_mem
;
1714 evict_mem
= bo
->mem
;
1715 evict_mem
.mm_node
= NULL
;
1716 evict_mem
.placement
= TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
;
1717 evict_mem
.mem_type
= TTM_PL_SYSTEM
;
1719 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true, &ctx
);
1720 if (unlikely(ret
!= 0))
1725 * Make sure BO is idle.
1728 ret
= ttm_bo_wait(bo
, false, false);
1729 if (unlikely(ret
!= 0))
1732 ttm_bo_unmap_virtual(bo
);
1735 * Swap out. Buffer will be swapped in again as soon as
1736 * anyone tries to access a ttm page.
1739 if (bo
->bdev
->driver
->swap_notify
)
1740 bo
->bdev
->driver
->swap_notify(bo
);
1742 ret
= ttm_tt_swapout(bo
->ttm
, bo
->persistent_swap_storage
);
1747 * Unreserve without putting on LRU to avoid swapping out an
1748 * already swapped buffer.
1751 reservation_object_unlock(bo
->resv
);
1752 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1755 EXPORT_SYMBOL(ttm_bo_swapout
);
1757 void ttm_bo_swapout_all(struct ttm_bo_device
*bdev
)
1759 struct ttm_operation_ctx ctx
= {
1760 .interruptible
= false,
1761 .no_wait_gpu
= false
1764 while (ttm_bo_swapout(bdev
->glob
, &ctx
) == 0)
1767 EXPORT_SYMBOL(ttm_bo_swapout_all
);
1770 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1773 * @bo: Pointer to buffer
1775 int ttm_bo_wait_unreserved(struct ttm_buffer_object
*bo
)
1780 * In the absense of a wait_unlocked API,
1781 * Use the bo::wu_mutex to avoid triggering livelocks due to
1782 * concurrent use of this function. Note that this use of
1783 * bo::wu_mutex can go away if we change locking order to
1784 * mmap_sem -> bo::reserve.
1786 ret
= mutex_lock_interruptible(&bo
->wu_mutex
);
1787 if (unlikely(ret
!= 0))
1788 return -ERESTARTSYS
;
1789 if (!ww_mutex_is_locked(&bo
->resv
->lock
))
1791 ret
= reservation_object_lock_interruptible(bo
->resv
, NULL
);
1794 if (unlikely(ret
!= 0))
1796 reservation_object_unlock(bo
->resv
);
1799 mutex_unlock(&bo
->wu_mutex
);