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>
44 #define TTM_ASSERT_LOCKED(param)
45 #define TTM_DEBUG(fmt, arg...)
46 #define TTM_BO_HASH_ORDER 13
48 static int ttm_bo_swapout(struct ttm_mem_shrink
*shrink
);
49 static void ttm_bo_global_kobj_release(struct kobject
*kobj
);
51 static struct attribute ttm_bo_count
= {
56 static inline int ttm_mem_type_from_place(const struct ttm_place
*place
,
61 for (i
= 0; i
<= TTM_PL_PRIV5
; i
++)
62 if (place
->flags
& (1 << i
)) {
69 static void ttm_mem_type_debug(struct ttm_bo_device
*bdev
, int mem_type
)
71 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
73 pr_err(" has_type: %d\n", man
->has_type
);
74 pr_err(" use_type: %d\n", man
->use_type
);
75 pr_err(" flags: 0x%08X\n", man
->flags
);
76 pr_err(" gpu_offset: 0x%08lX\n", man
->gpu_offset
);
77 pr_err(" size: %llu\n", man
->size
);
78 pr_err(" available_caching: 0x%08X\n", man
->available_caching
);
79 pr_err(" default_caching: 0x%08X\n", man
->default_caching
);
80 if (mem_type
!= TTM_PL_SYSTEM
)
81 (*man
->func
->debug
)(man
, TTM_PFX
);
84 static void ttm_bo_mem_space_debug(struct ttm_buffer_object
*bo
,
85 struct ttm_placement
*placement
)
89 pr_err("No space for %p (%lu pages, %luK, %luM)\n",
90 bo
, bo
->mem
.num_pages
, bo
->mem
.size
>> 10,
92 for (i
= 0; i
< placement
->num_placement
; i
++) {
93 ret
= ttm_mem_type_from_place(&placement
->placement
[i
],
97 pr_err(" placement[%d]=0x%08X (%d)\n",
98 i
, placement
->placement
[i
].flags
, mem_type
);
99 ttm_mem_type_debug(bo
->bdev
, mem_type
);
103 static ssize_t
ttm_bo_global_show(struct kobject
*kobj
,
104 struct attribute
*attr
,
107 struct ttm_bo_global
*glob
=
108 container_of(kobj
, struct ttm_bo_global
, kobj
);
110 return snprintf(buffer
, PAGE_SIZE
, "%lu\n",
111 (unsigned long) atomic_read(&glob
->bo_count
));
114 static struct attribute
*ttm_bo_global_attrs
[] = {
119 static const struct sysfs_ops ttm_bo_global_ops
= {
120 .show
= &ttm_bo_global_show
123 static struct kobj_type ttm_bo_glob_kobj_type
= {
124 .release
= &ttm_bo_global_kobj_release
,
125 .sysfs_ops
= &ttm_bo_global_ops
,
126 .default_attrs
= ttm_bo_global_attrs
130 static inline uint32_t ttm_bo_type_flags(unsigned type
)
135 static void ttm_bo_release_list(struct kref
*list_kref
)
137 struct ttm_buffer_object
*bo
=
138 container_of(list_kref
, struct ttm_buffer_object
, list_kref
);
139 struct ttm_bo_device
*bdev
= bo
->bdev
;
140 size_t acc_size
= bo
->acc_size
;
142 BUG_ON(atomic_read(&bo
->list_kref
.refcount
));
143 BUG_ON(atomic_read(&bo
->kref
.refcount
));
144 BUG_ON(atomic_read(&bo
->cpu_writers
));
145 BUG_ON(bo
->sync_obj
!= NULL
);
146 BUG_ON(bo
->mem
.mm_node
!= NULL
);
147 BUG_ON(!list_empty(&bo
->lru
));
148 BUG_ON(!list_empty(&bo
->ddestroy
));
151 ttm_tt_destroy(bo
->ttm
);
152 atomic_dec(&bo
->glob
->bo_count
);
153 if (bo
->resv
== &bo
->ttm_resv
)
154 reservation_object_fini(&bo
->ttm_resv
);
155 mutex_destroy(&bo
->wu_mutex
);
161 ttm_mem_global_free(bdev
->glob
->mem_glob
, acc_size
);
164 void ttm_bo_add_to_lru(struct ttm_buffer_object
*bo
)
166 struct ttm_bo_device
*bdev
= bo
->bdev
;
167 struct ttm_mem_type_manager
*man
;
169 lockdep_assert_held(&bo
->resv
->lock
.base
);
171 if (!(bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
)) {
173 BUG_ON(!list_empty(&bo
->lru
));
175 man
= &bdev
->man
[bo
->mem
.mem_type
];
176 list_add_tail(&bo
->lru
, &man
->lru
);
177 kref_get(&bo
->list_kref
);
179 if (bo
->ttm
!= NULL
) {
180 list_add_tail(&bo
->swap
, &bo
->glob
->swap_lru
);
181 kref_get(&bo
->list_kref
);
185 EXPORT_SYMBOL(ttm_bo_add_to_lru
);
187 int ttm_bo_del_from_lru(struct ttm_buffer_object
*bo
)
191 if (!list_empty(&bo
->swap
)) {
192 list_del_init(&bo
->swap
);
195 if (!list_empty(&bo
->lru
)) {
196 list_del_init(&bo
->lru
);
201 * TODO: Add a driver hook to delete from
202 * driver-specific LRU's here.
208 static void ttm_bo_ref_bug(struct kref
*list_kref
)
213 void ttm_bo_list_ref_sub(struct ttm_buffer_object
*bo
, int count
,
216 kref_sub(&bo
->list_kref
, count
,
217 (never_free
) ? ttm_bo_ref_bug
: ttm_bo_release_list
);
220 void ttm_bo_del_sub_from_lru(struct ttm_buffer_object
*bo
)
224 spin_lock(&bo
->glob
->lru_lock
);
225 put_count
= ttm_bo_del_from_lru(bo
);
226 spin_unlock(&bo
->glob
->lru_lock
);
227 ttm_bo_list_ref_sub(bo
, put_count
, true);
229 EXPORT_SYMBOL(ttm_bo_del_sub_from_lru
);
232 * Call bo->mutex locked.
234 static int ttm_bo_add_ttm(struct ttm_buffer_object
*bo
, bool zero_alloc
)
236 struct ttm_bo_device
*bdev
= bo
->bdev
;
237 struct ttm_bo_global
*glob
= bo
->glob
;
239 uint32_t page_flags
= 0;
241 TTM_ASSERT_LOCKED(&bo
->mutex
);
244 if (bdev
->need_dma32
)
245 page_flags
|= TTM_PAGE_FLAG_DMA32
;
248 case ttm_bo_type_device
:
250 page_flags
|= TTM_PAGE_FLAG_ZERO_ALLOC
;
251 case ttm_bo_type_kernel
:
252 bo
->ttm
= bdev
->driver
->ttm_tt_create(bdev
, bo
->num_pages
<< PAGE_SHIFT
,
253 page_flags
, glob
->dummy_read_page
);
254 if (unlikely(bo
->ttm
== NULL
))
258 bo
->ttm
= bdev
->driver
->ttm_tt_create(bdev
, bo
->num_pages
<< PAGE_SHIFT
,
259 page_flags
| TTM_PAGE_FLAG_SG
,
260 glob
->dummy_read_page
);
261 if (unlikely(bo
->ttm
== NULL
)) {
265 bo
->ttm
->sg
= bo
->sg
;
268 pr_err("Illegal buffer object type\n");
276 static int ttm_bo_handle_move_mem(struct ttm_buffer_object
*bo
,
277 struct ttm_mem_reg
*mem
,
278 bool evict
, bool interruptible
,
281 struct ttm_bo_device
*bdev
= bo
->bdev
;
282 bool old_is_pci
= ttm_mem_reg_is_pci(bdev
, &bo
->mem
);
283 bool new_is_pci
= ttm_mem_reg_is_pci(bdev
, mem
);
284 struct ttm_mem_type_manager
*old_man
= &bdev
->man
[bo
->mem
.mem_type
];
285 struct ttm_mem_type_manager
*new_man
= &bdev
->man
[mem
->mem_type
];
288 if (old_is_pci
|| new_is_pci
||
289 ((mem
->placement
& bo
->mem
.placement
& TTM_PL_MASK_CACHING
) == 0)) {
290 ret
= ttm_mem_io_lock(old_man
, true);
291 if (unlikely(ret
!= 0))
293 ttm_bo_unmap_virtual_locked(bo
);
294 ttm_mem_io_unlock(old_man
);
298 * Create and bind a ttm if required.
301 if (!(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
)) {
302 if (bo
->ttm
== NULL
) {
303 bool zero
= !(old_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
);
304 ret
= ttm_bo_add_ttm(bo
, zero
);
309 ret
= ttm_tt_set_placement_caching(bo
->ttm
, mem
->placement
);
313 if (mem
->mem_type
!= TTM_PL_SYSTEM
) {
314 ret
= ttm_tt_bind(bo
->ttm
, mem
);
319 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
) {
320 if (bdev
->driver
->move_notify
)
321 bdev
->driver
->move_notify(bo
, mem
);
328 if (bdev
->driver
->move_notify
)
329 bdev
->driver
->move_notify(bo
, mem
);
331 if (!(old_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) &&
332 !(new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
))
333 ret
= ttm_bo_move_ttm(bo
, evict
, no_wait_gpu
, mem
);
334 else if (bdev
->driver
->move
)
335 ret
= bdev
->driver
->move(bo
, evict
, interruptible
,
338 ret
= ttm_bo_move_memcpy(bo
, evict
, no_wait_gpu
, mem
);
341 if (bdev
->driver
->move_notify
) {
342 struct ttm_mem_reg tmp_mem
= *mem
;
345 bdev
->driver
->move_notify(bo
, mem
);
355 if (bdev
->driver
->invalidate_caches
) {
356 ret
= bdev
->driver
->invalidate_caches(bdev
, bo
->mem
.placement
);
358 pr_err("Can not flush read caches\n");
363 if (bo
->mem
.mm_node
) {
364 bo
->offset
= (bo
->mem
.start
<< PAGE_SHIFT
) +
365 bdev
->man
[bo
->mem
.mem_type
].gpu_offset
;
366 bo
->cur_placement
= bo
->mem
.placement
;
373 new_man
= &bdev
->man
[bo
->mem
.mem_type
];
374 if ((new_man
->flags
& TTM_MEMTYPE_FLAG_FIXED
) && bo
->ttm
) {
375 ttm_tt_unbind(bo
->ttm
);
376 ttm_tt_destroy(bo
->ttm
);
385 * Will release GPU memory type usage on destruction.
386 * This is the place to put in driver specific hooks to release
387 * driver private resources.
388 * Will release the bo::reserved lock.
391 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object
*bo
)
393 if (bo
->bdev
->driver
->move_notify
)
394 bo
->bdev
->driver
->move_notify(bo
, NULL
);
397 ttm_tt_unbind(bo
->ttm
);
398 ttm_tt_destroy(bo
->ttm
);
401 ttm_bo_mem_put(bo
, &bo
->mem
);
403 ww_mutex_unlock (&bo
->resv
->lock
);
406 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object
*bo
)
408 struct ttm_bo_device
*bdev
= bo
->bdev
;
409 struct ttm_bo_global
*glob
= bo
->glob
;
410 struct ttm_bo_driver
*driver
= bdev
->driver
;
411 void *sync_obj
= NULL
;
415 spin_lock(&glob
->lru_lock
);
416 ret
= __ttm_bo_reserve(bo
, false, true, false, NULL
);
418 spin_lock(&bdev
->fence_lock
);
419 (void) ttm_bo_wait(bo
, false, false, true);
420 if (!ret
&& !bo
->sync_obj
) {
421 spin_unlock(&bdev
->fence_lock
);
422 put_count
= ttm_bo_del_from_lru(bo
);
424 spin_unlock(&glob
->lru_lock
);
425 ttm_bo_cleanup_memtype_use(bo
);
427 ttm_bo_list_ref_sub(bo
, put_count
, true);
432 sync_obj
= driver
->sync_obj_ref(bo
->sync_obj
);
433 spin_unlock(&bdev
->fence_lock
);
438 * Make NO_EVICT bos immediately available to
439 * shrinkers, now that they are queued for
442 if (bo
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
) {
443 bo
->mem
.placement
&= ~TTM_PL_FLAG_NO_EVICT
;
444 ttm_bo_add_to_lru(bo
);
447 __ttm_bo_unreserve(bo
);
450 kref_get(&bo
->list_kref
);
451 list_add_tail(&bo
->ddestroy
, &bdev
->ddestroy
);
452 spin_unlock(&glob
->lru_lock
);
455 driver
->sync_obj_flush(sync_obj
);
456 driver
->sync_obj_unref(&sync_obj
);
458 schedule_delayed_work(&bdev
->wq
,
459 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
463 * function ttm_bo_cleanup_refs_and_unlock
464 * If bo idle, remove from delayed- and lru lists, and unref.
465 * If not idle, do nothing.
467 * Must be called with lru_lock and reservation held, this function
468 * will drop both before returning.
470 * @interruptible Any sleeps should occur interruptibly.
471 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
474 static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object
*bo
,
478 struct ttm_bo_device
*bdev
= bo
->bdev
;
479 struct ttm_bo_driver
*driver
= bdev
->driver
;
480 struct ttm_bo_global
*glob
= bo
->glob
;
484 spin_lock(&bdev
->fence_lock
);
485 ret
= ttm_bo_wait(bo
, false, false, true);
487 if (ret
&& !no_wait_gpu
) {
491 * Take a reference to the fence and unreserve,
492 * at this point the buffer should be dead, so
493 * no new sync objects can be attached.
495 sync_obj
= driver
->sync_obj_ref(bo
->sync_obj
);
496 spin_unlock(&bdev
->fence_lock
);
498 __ttm_bo_unreserve(bo
);
499 spin_unlock(&glob
->lru_lock
);
501 ret
= driver
->sync_obj_wait(sync_obj
, false, interruptible
);
502 driver
->sync_obj_unref(&sync_obj
);
507 * remove sync_obj with ttm_bo_wait, the wait should be
508 * finished, and no new wait object should have been added.
510 spin_lock(&bdev
->fence_lock
);
511 ret
= ttm_bo_wait(bo
, false, false, true);
513 spin_unlock(&bdev
->fence_lock
);
517 spin_lock(&glob
->lru_lock
);
518 ret
= __ttm_bo_reserve(bo
, false, true, false, NULL
);
521 * We raced, and lost, someone else holds the reservation now,
522 * and is probably busy in ttm_bo_cleanup_memtype_use.
524 * Even if it's not the case, because we finished waiting any
525 * delayed destruction would succeed, so just return success
529 spin_unlock(&glob
->lru_lock
);
533 spin_unlock(&bdev
->fence_lock
);
535 if (ret
|| unlikely(list_empty(&bo
->ddestroy
))) {
536 __ttm_bo_unreserve(bo
);
537 spin_unlock(&glob
->lru_lock
);
541 put_count
= ttm_bo_del_from_lru(bo
);
542 list_del_init(&bo
->ddestroy
);
545 spin_unlock(&glob
->lru_lock
);
546 ttm_bo_cleanup_memtype_use(bo
);
548 ttm_bo_list_ref_sub(bo
, put_count
, true);
554 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
555 * encountered buffers.
558 static int ttm_bo_delayed_delete(struct ttm_bo_device
*bdev
, bool remove_all
)
560 struct ttm_bo_global
*glob
= bdev
->glob
;
561 struct ttm_buffer_object
*entry
= NULL
;
564 spin_lock(&glob
->lru_lock
);
565 if (list_empty(&bdev
->ddestroy
))
568 entry
= list_first_entry(&bdev
->ddestroy
,
569 struct ttm_buffer_object
, ddestroy
);
570 kref_get(&entry
->list_kref
);
573 struct ttm_buffer_object
*nentry
= NULL
;
575 if (entry
->ddestroy
.next
!= &bdev
->ddestroy
) {
576 nentry
= list_first_entry(&entry
->ddestroy
,
577 struct ttm_buffer_object
, ddestroy
);
578 kref_get(&nentry
->list_kref
);
581 ret
= __ttm_bo_reserve(entry
, false, true, false, NULL
);
582 if (remove_all
&& ret
) {
583 spin_unlock(&glob
->lru_lock
);
584 ret
= __ttm_bo_reserve(entry
, false, false,
586 spin_lock(&glob
->lru_lock
);
590 ret
= ttm_bo_cleanup_refs_and_unlock(entry
, false,
593 spin_unlock(&glob
->lru_lock
);
595 kref_put(&entry
->list_kref
, ttm_bo_release_list
);
601 spin_lock(&glob
->lru_lock
);
602 if (list_empty(&entry
->ddestroy
))
607 spin_unlock(&glob
->lru_lock
);
610 kref_put(&entry
->list_kref
, ttm_bo_release_list
);
614 static void ttm_bo_delayed_workqueue(struct work_struct
*work
)
616 struct ttm_bo_device
*bdev
=
617 container_of(work
, struct ttm_bo_device
, wq
.work
);
619 if (ttm_bo_delayed_delete(bdev
, false)) {
620 schedule_delayed_work(&bdev
->wq
,
621 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
625 static void ttm_bo_release(struct kref
*kref
)
627 struct ttm_buffer_object
*bo
=
628 container_of(kref
, struct ttm_buffer_object
, kref
);
629 struct ttm_bo_device
*bdev
= bo
->bdev
;
630 struct ttm_mem_type_manager
*man
= &bdev
->man
[bo
->mem
.mem_type
];
632 drm_vma_offset_remove(&bdev
->vma_manager
, &bo
->vma_node
);
633 ttm_mem_io_lock(man
, false);
634 ttm_mem_io_free_vm(bo
);
635 ttm_mem_io_unlock(man
);
636 ttm_bo_cleanup_refs_or_queue(bo
);
637 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
640 void ttm_bo_unref(struct ttm_buffer_object
**p_bo
)
642 struct ttm_buffer_object
*bo
= *p_bo
;
645 kref_put(&bo
->kref
, ttm_bo_release
);
647 EXPORT_SYMBOL(ttm_bo_unref
);
649 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device
*bdev
)
651 return cancel_delayed_work_sync(&bdev
->wq
);
653 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue
);
655 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device
*bdev
, int resched
)
658 schedule_delayed_work(&bdev
->wq
,
659 ((HZ
/ 100) < 1) ? 1 : HZ
/ 100);
661 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue
);
663 static int ttm_bo_evict(struct ttm_buffer_object
*bo
, bool interruptible
,
666 struct ttm_bo_device
*bdev
= bo
->bdev
;
667 struct ttm_mem_reg evict_mem
;
668 struct ttm_placement placement
;
671 spin_lock(&bdev
->fence_lock
);
672 ret
= ttm_bo_wait(bo
, false, interruptible
, no_wait_gpu
);
673 spin_unlock(&bdev
->fence_lock
);
675 if (unlikely(ret
!= 0)) {
676 if (ret
!= -ERESTARTSYS
) {
677 pr_err("Failed to expire sync object before buffer eviction\n");
682 lockdep_assert_held(&bo
->resv
->lock
.base
);
685 evict_mem
.mm_node
= NULL
;
686 evict_mem
.bus
.io_reserved_vm
= false;
687 evict_mem
.bus
.io_reserved_count
= 0;
689 placement
.num_placement
= 0;
690 placement
.num_busy_placement
= 0;
691 bdev
->driver
->evict_flags(bo
, &placement
);
692 ret
= ttm_bo_mem_space(bo
, &placement
, &evict_mem
, interruptible
,
695 if (ret
!= -ERESTARTSYS
) {
696 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
698 ttm_bo_mem_space_debug(bo
, &placement
);
703 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true, interruptible
,
706 if (ret
!= -ERESTARTSYS
)
707 pr_err("Buffer eviction failed\n");
708 ttm_bo_mem_put(bo
, &evict_mem
);
716 static int ttm_mem_evict_first(struct ttm_bo_device
*bdev
,
721 struct ttm_bo_global
*glob
= bdev
->glob
;
722 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
723 struct ttm_buffer_object
*bo
;
724 int ret
= -EBUSY
, put_count
;
726 spin_lock(&glob
->lru_lock
);
727 list_for_each_entry(bo
, &man
->lru
, lru
) {
728 ret
= __ttm_bo_reserve(bo
, false, true, false, NULL
);
734 spin_unlock(&glob
->lru_lock
);
738 kref_get(&bo
->list_kref
);
740 if (!list_empty(&bo
->ddestroy
)) {
741 ret
= ttm_bo_cleanup_refs_and_unlock(bo
, interruptible
,
743 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
747 put_count
= ttm_bo_del_from_lru(bo
);
748 spin_unlock(&glob
->lru_lock
);
752 ttm_bo_list_ref_sub(bo
, put_count
, true);
754 ret
= ttm_bo_evict(bo
, interruptible
, no_wait_gpu
);
755 ttm_bo_unreserve(bo
);
757 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
761 void ttm_bo_mem_put(struct ttm_buffer_object
*bo
, struct ttm_mem_reg
*mem
)
763 struct ttm_mem_type_manager
*man
= &bo
->bdev
->man
[mem
->mem_type
];
766 (*man
->func
->put_node
)(man
, mem
);
768 EXPORT_SYMBOL(ttm_bo_mem_put
);
771 * Repeatedly evict memory from the LRU for @mem_type until we create enough
772 * space, or we've evicted everything and there isn't enough space.
774 static int ttm_bo_mem_force_space(struct ttm_buffer_object
*bo
,
776 const struct ttm_place
*place
,
777 struct ttm_mem_reg
*mem
,
781 struct ttm_bo_device
*bdev
= bo
->bdev
;
782 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
786 ret
= (*man
->func
->get_node
)(man
, bo
, place
, mem
);
787 if (unlikely(ret
!= 0))
791 ret
= ttm_mem_evict_first(bdev
, mem_type
,
792 interruptible
, no_wait_gpu
);
793 if (unlikely(ret
!= 0))
796 if (mem
->mm_node
== NULL
)
798 mem
->mem_type
= mem_type
;
802 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager
*man
,
803 uint32_t cur_placement
,
804 uint32_t proposed_placement
)
806 uint32_t caching
= proposed_placement
& TTM_PL_MASK_CACHING
;
807 uint32_t result
= proposed_placement
& ~TTM_PL_MASK_CACHING
;
810 * Keep current caching if possible.
813 if ((cur_placement
& caching
) != 0)
814 result
|= (cur_placement
& caching
);
815 else if ((man
->default_caching
& caching
) != 0)
816 result
|= man
->default_caching
;
817 else if ((TTM_PL_FLAG_CACHED
& caching
) != 0)
818 result
|= TTM_PL_FLAG_CACHED
;
819 else if ((TTM_PL_FLAG_WC
& caching
) != 0)
820 result
|= TTM_PL_FLAG_WC
;
821 else if ((TTM_PL_FLAG_UNCACHED
& caching
) != 0)
822 result
|= TTM_PL_FLAG_UNCACHED
;
827 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager
*man
,
829 const struct ttm_place
*place
,
830 uint32_t *masked_placement
)
832 uint32_t cur_flags
= ttm_bo_type_flags(mem_type
);
834 if ((cur_flags
& place
->flags
& TTM_PL_MASK_MEM
) == 0)
837 if ((place
->flags
& man
->available_caching
) == 0)
840 cur_flags
|= (place
->flags
& man
->available_caching
);
842 *masked_placement
= cur_flags
;
847 * Creates space for memory region @mem according to its type.
849 * This function first searches for free space in compatible memory types in
850 * the priority order defined by the driver. If free space isn't found, then
851 * ttm_bo_mem_force_space is attempted in priority order to evict and find
854 int ttm_bo_mem_space(struct ttm_buffer_object
*bo
,
855 struct ttm_placement
*placement
,
856 struct ttm_mem_reg
*mem
,
860 struct ttm_bo_device
*bdev
= bo
->bdev
;
861 struct ttm_mem_type_manager
*man
;
862 uint32_t mem_type
= TTM_PL_SYSTEM
;
863 uint32_t cur_flags
= 0;
864 bool type_found
= false;
865 bool type_ok
= false;
866 bool has_erestartsys
= false;
870 for (i
= 0; i
< placement
->num_placement
; ++i
) {
871 const struct ttm_place
*place
= &placement
->placement
[i
];
873 ret
= ttm_mem_type_from_place(place
, &mem_type
);
876 man
= &bdev
->man
[mem_type
];
878 type_ok
= ttm_bo_mt_compatible(man
, mem_type
, place
,
884 cur_flags
= ttm_bo_select_caching(man
, bo
->mem
.placement
,
887 * Use the access and other non-mapping-related flag bits from
888 * the memory placement flags to the current flags
890 ttm_flag_masked(&cur_flags
, place
->flags
,
891 ~TTM_PL_MASK_MEMTYPE
);
893 if (mem_type
== TTM_PL_SYSTEM
)
896 if (man
->has_type
&& man
->use_type
) {
898 ret
= (*man
->func
->get_node
)(man
, bo
, place
, mem
);
906 if ((type_ok
&& (mem_type
== TTM_PL_SYSTEM
)) || mem
->mm_node
) {
907 mem
->mem_type
= mem_type
;
908 mem
->placement
= cur_flags
;
915 for (i
= 0; i
< placement
->num_busy_placement
; ++i
) {
916 const struct ttm_place
*place
= &placement
->busy_placement
[i
];
918 ret
= ttm_mem_type_from_place(place
, &mem_type
);
921 man
= &bdev
->man
[mem_type
];
924 if (!ttm_bo_mt_compatible(man
, mem_type
, place
, &cur_flags
))
927 cur_flags
= ttm_bo_select_caching(man
, bo
->mem
.placement
,
930 * Use the access and other non-mapping-related flag bits from
931 * the memory placement flags to the current flags
933 ttm_flag_masked(&cur_flags
, place
->flags
,
934 ~TTM_PL_MASK_MEMTYPE
);
936 if (mem_type
== TTM_PL_SYSTEM
) {
937 mem
->mem_type
= mem_type
;
938 mem
->placement
= cur_flags
;
943 ret
= ttm_bo_mem_force_space(bo
, mem_type
, place
, mem
,
944 interruptible
, no_wait_gpu
);
945 if (ret
== 0 && mem
->mm_node
) {
946 mem
->placement
= cur_flags
;
949 if (ret
== -ERESTARTSYS
)
950 has_erestartsys
= true;
952 ret
= (has_erestartsys
) ? -ERESTARTSYS
: -ENOMEM
;
955 EXPORT_SYMBOL(ttm_bo_mem_space
);
957 static int ttm_bo_move_buffer(struct ttm_buffer_object
*bo
,
958 struct ttm_placement
*placement
,
963 struct ttm_mem_reg mem
;
964 struct ttm_bo_device
*bdev
= bo
->bdev
;
966 lockdep_assert_held(&bo
->resv
->lock
.base
);
969 * FIXME: It's possible to pipeline buffer moves.
970 * Have the driver move function wait for idle when necessary,
971 * instead of doing it here.
973 spin_lock(&bdev
->fence_lock
);
974 ret
= ttm_bo_wait(bo
, false, interruptible
, no_wait_gpu
);
975 spin_unlock(&bdev
->fence_lock
);
978 mem
.num_pages
= bo
->num_pages
;
979 mem
.size
= mem
.num_pages
<< PAGE_SHIFT
;
980 mem
.page_alignment
= bo
->mem
.page_alignment
;
981 mem
.bus
.io_reserved_vm
= false;
982 mem
.bus
.io_reserved_count
= 0;
984 * Determine where to move the buffer.
986 ret
= ttm_bo_mem_space(bo
, placement
, &mem
,
987 interruptible
, no_wait_gpu
);
990 ret
= ttm_bo_handle_move_mem(bo
, &mem
, false,
991 interruptible
, no_wait_gpu
);
993 if (ret
&& mem
.mm_node
)
994 ttm_bo_mem_put(bo
, &mem
);
998 static bool ttm_bo_mem_compat(struct ttm_placement
*placement
,
999 struct ttm_mem_reg
*mem
,
1000 uint32_t *new_flags
)
1004 for (i
= 0; i
< placement
->num_placement
; i
++) {
1005 const struct ttm_place
*heap
= &placement
->placement
[i
];
1006 if (mem
->mm_node
&& heap
->lpfn
!= 0 &&
1007 (mem
->start
< heap
->fpfn
||
1008 mem
->start
+ mem
->num_pages
> heap
->lpfn
))
1011 *new_flags
= heap
->flags
;
1012 if ((*new_flags
& mem
->placement
& TTM_PL_MASK_CACHING
) &&
1013 (*new_flags
& mem
->placement
& TTM_PL_MASK_MEM
))
1017 for (i
= 0; i
< placement
->num_busy_placement
; i
++) {
1018 const struct ttm_place
*heap
= &placement
->busy_placement
[i
];
1019 if (mem
->mm_node
&& heap
->lpfn
!= 0 &&
1020 (mem
->start
< heap
->fpfn
||
1021 mem
->start
+ mem
->num_pages
> heap
->lpfn
))
1024 *new_flags
= heap
->flags
;
1025 if ((*new_flags
& mem
->placement
& TTM_PL_MASK_CACHING
) &&
1026 (*new_flags
& mem
->placement
& TTM_PL_MASK_MEM
))
1033 int ttm_bo_validate(struct ttm_buffer_object
*bo
,
1034 struct ttm_placement
*placement
,
1041 lockdep_assert_held(&bo
->resv
->lock
.base
);
1043 * Check whether we need to move buffer.
1045 if (!ttm_bo_mem_compat(placement
, &bo
->mem
, &new_flags
)) {
1046 ret
= ttm_bo_move_buffer(bo
, placement
, interruptible
,
1052 * Use the access and other non-mapping-related flag bits from
1053 * the compatible memory placement flags to the active flags
1055 ttm_flag_masked(&bo
->mem
.placement
, new_flags
,
1056 ~TTM_PL_MASK_MEMTYPE
);
1059 * We might need to add a TTM.
1061 if (bo
->mem
.mem_type
== TTM_PL_SYSTEM
&& bo
->ttm
== NULL
) {
1062 ret
= ttm_bo_add_ttm(bo
, true);
1068 EXPORT_SYMBOL(ttm_bo_validate
);
1070 int ttm_bo_init(struct ttm_bo_device
*bdev
,
1071 struct ttm_buffer_object
*bo
,
1073 enum ttm_bo_type type
,
1074 struct ttm_placement
*placement
,
1075 uint32_t page_alignment
,
1077 struct file
*persistent_swap_storage
,
1079 struct sg_table
*sg
,
1080 void (*destroy
) (struct ttm_buffer_object
*))
1083 unsigned long num_pages
;
1084 struct ttm_mem_global
*mem_glob
= bdev
->glob
->mem_glob
;
1087 ret
= ttm_mem_global_alloc(mem_glob
, acc_size
, false, false);
1089 pr_err("Out of kernel memory\n");
1097 num_pages
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1098 if (num_pages
== 0) {
1099 pr_err("Illegal buffer object size\n");
1104 ttm_mem_global_free(mem_glob
, acc_size
);
1107 bo
->destroy
= destroy
;
1109 kref_init(&bo
->kref
);
1110 kref_init(&bo
->list_kref
);
1111 atomic_set(&bo
->cpu_writers
, 0);
1112 INIT_LIST_HEAD(&bo
->lru
);
1113 INIT_LIST_HEAD(&bo
->ddestroy
);
1114 INIT_LIST_HEAD(&bo
->swap
);
1115 INIT_LIST_HEAD(&bo
->io_reserve_lru
);
1116 mutex_init(&bo
->wu_mutex
);
1118 bo
->glob
= bdev
->glob
;
1120 bo
->num_pages
= num_pages
;
1121 bo
->mem
.size
= num_pages
<< PAGE_SHIFT
;
1122 bo
->mem
.mem_type
= TTM_PL_SYSTEM
;
1123 bo
->mem
.num_pages
= bo
->num_pages
;
1124 bo
->mem
.mm_node
= NULL
;
1125 bo
->mem
.page_alignment
= page_alignment
;
1126 bo
->mem
.bus
.io_reserved_vm
= false;
1127 bo
->mem
.bus
.io_reserved_count
= 0;
1129 bo
->mem
.placement
= (TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
);
1130 bo
->persistent_swap_storage
= persistent_swap_storage
;
1131 bo
->acc_size
= acc_size
;
1133 bo
->resv
= &bo
->ttm_resv
;
1134 reservation_object_init(bo
->resv
);
1135 atomic_inc(&bo
->glob
->bo_count
);
1136 drm_vma_node_reset(&bo
->vma_node
);
1139 * For ttm_bo_type_device buffers, allocate
1140 * address space from the device.
1142 if (bo
->type
== ttm_bo_type_device
||
1143 bo
->type
== ttm_bo_type_sg
)
1144 ret
= drm_vma_offset_add(&bdev
->vma_manager
, &bo
->vma_node
,
1147 locked
= ww_mutex_trylock(&bo
->resv
->lock
);
1151 ret
= ttm_bo_validate(bo
, placement
, interruptible
, false);
1153 ttm_bo_unreserve(bo
);
1160 EXPORT_SYMBOL(ttm_bo_init
);
1162 size_t ttm_bo_acc_size(struct ttm_bo_device
*bdev
,
1163 unsigned long bo_size
,
1164 unsigned struct_size
)
1166 unsigned npages
= (PAGE_ALIGN(bo_size
)) >> PAGE_SHIFT
;
1169 size
+= ttm_round_pot(struct_size
);
1170 size
+= PAGE_ALIGN(npages
* sizeof(void *));
1171 size
+= ttm_round_pot(sizeof(struct ttm_tt
));
1174 EXPORT_SYMBOL(ttm_bo_acc_size
);
1176 size_t ttm_bo_dma_acc_size(struct ttm_bo_device
*bdev
,
1177 unsigned long bo_size
,
1178 unsigned struct_size
)
1180 unsigned npages
= (PAGE_ALIGN(bo_size
)) >> PAGE_SHIFT
;
1183 size
+= ttm_round_pot(struct_size
);
1184 size
+= PAGE_ALIGN(npages
* sizeof(void *));
1185 size
+= PAGE_ALIGN(npages
* sizeof(dma_addr_t
));
1186 size
+= ttm_round_pot(sizeof(struct ttm_dma_tt
));
1189 EXPORT_SYMBOL(ttm_bo_dma_acc_size
);
1191 int ttm_bo_create(struct ttm_bo_device
*bdev
,
1193 enum ttm_bo_type type
,
1194 struct ttm_placement
*placement
,
1195 uint32_t page_alignment
,
1197 struct file
*persistent_swap_storage
,
1198 struct ttm_buffer_object
**p_bo
)
1200 struct ttm_buffer_object
*bo
;
1204 bo
= kzalloc(sizeof(*bo
), GFP_KERNEL
);
1205 if (unlikely(bo
== NULL
))
1208 acc_size
= ttm_bo_acc_size(bdev
, size
, sizeof(struct ttm_buffer_object
));
1209 ret
= ttm_bo_init(bdev
, bo
, size
, type
, placement
, page_alignment
,
1210 interruptible
, persistent_swap_storage
, acc_size
,
1212 if (likely(ret
== 0))
1217 EXPORT_SYMBOL(ttm_bo_create
);
1219 static int ttm_bo_force_list_clean(struct ttm_bo_device
*bdev
,
1220 unsigned mem_type
, bool allow_errors
)
1222 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1223 struct ttm_bo_global
*glob
= bdev
->glob
;
1227 * Can't use standard list traversal since we're unlocking.
1230 spin_lock(&glob
->lru_lock
);
1231 while (!list_empty(&man
->lru
)) {
1232 spin_unlock(&glob
->lru_lock
);
1233 ret
= ttm_mem_evict_first(bdev
, mem_type
, false, false);
1238 pr_err("Cleanup eviction failed\n");
1241 spin_lock(&glob
->lru_lock
);
1243 spin_unlock(&glob
->lru_lock
);
1247 int ttm_bo_clean_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1249 struct ttm_mem_type_manager
*man
;
1252 if (mem_type
>= TTM_NUM_MEM_TYPES
) {
1253 pr_err("Illegal memory type %d\n", mem_type
);
1256 man
= &bdev
->man
[mem_type
];
1258 if (!man
->has_type
) {
1259 pr_err("Trying to take down uninitialized memory manager type %u\n",
1264 man
->use_type
= false;
1265 man
->has_type
= false;
1269 ttm_bo_force_list_clean(bdev
, mem_type
, false);
1271 ret
= (*man
->func
->takedown
)(man
);
1276 EXPORT_SYMBOL(ttm_bo_clean_mm
);
1278 int ttm_bo_evict_mm(struct ttm_bo_device
*bdev
, unsigned mem_type
)
1280 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem_type
];
1282 if (mem_type
== 0 || mem_type
>= TTM_NUM_MEM_TYPES
) {
1283 pr_err("Illegal memory manager memory type %u\n", mem_type
);
1287 if (!man
->has_type
) {
1288 pr_err("Memory type %u has not been initialized\n", mem_type
);
1292 return ttm_bo_force_list_clean(bdev
, mem_type
, true);
1294 EXPORT_SYMBOL(ttm_bo_evict_mm
);
1296 int ttm_bo_init_mm(struct ttm_bo_device
*bdev
, unsigned type
,
1297 unsigned long p_size
)
1300 struct ttm_mem_type_manager
*man
;
1302 BUG_ON(type
>= TTM_NUM_MEM_TYPES
);
1303 man
= &bdev
->man
[type
];
1304 BUG_ON(man
->has_type
);
1305 man
->io_reserve_fastpath
= true;
1306 man
->use_io_reserve_lru
= false;
1307 mutex_init(&man
->io_reserve_mutex
);
1308 INIT_LIST_HEAD(&man
->io_reserve_lru
);
1310 ret
= bdev
->driver
->init_mem_type(bdev
, type
, man
);
1316 if (type
!= TTM_PL_SYSTEM
) {
1317 ret
= (*man
->func
->init
)(man
, p_size
);
1321 man
->has_type
= true;
1322 man
->use_type
= true;
1325 INIT_LIST_HEAD(&man
->lru
);
1329 EXPORT_SYMBOL(ttm_bo_init_mm
);
1331 static void ttm_bo_global_kobj_release(struct kobject
*kobj
)
1333 struct ttm_bo_global
*glob
=
1334 container_of(kobj
, struct ttm_bo_global
, kobj
);
1336 ttm_mem_unregister_shrink(glob
->mem_glob
, &glob
->shrink
);
1337 __free_page(glob
->dummy_read_page
);
1341 void ttm_bo_global_release(struct drm_global_reference
*ref
)
1343 struct ttm_bo_global
*glob
= ref
->object
;
1345 kobject_del(&glob
->kobj
);
1346 kobject_put(&glob
->kobj
);
1348 EXPORT_SYMBOL(ttm_bo_global_release
);
1350 int ttm_bo_global_init(struct drm_global_reference
*ref
)
1352 struct ttm_bo_global_ref
*bo_ref
=
1353 container_of(ref
, struct ttm_bo_global_ref
, ref
);
1354 struct ttm_bo_global
*glob
= ref
->object
;
1357 mutex_init(&glob
->device_list_mutex
);
1358 spin_lock_init(&glob
->lru_lock
);
1359 glob
->mem_glob
= bo_ref
->mem_glob
;
1360 glob
->dummy_read_page
= alloc_page(__GFP_ZERO
| GFP_DMA32
);
1362 if (unlikely(glob
->dummy_read_page
== NULL
)) {
1367 INIT_LIST_HEAD(&glob
->swap_lru
);
1368 INIT_LIST_HEAD(&glob
->device_list
);
1370 ttm_mem_init_shrink(&glob
->shrink
, ttm_bo_swapout
);
1371 ret
= ttm_mem_register_shrink(glob
->mem_glob
, &glob
->shrink
);
1372 if (unlikely(ret
!= 0)) {
1373 pr_err("Could not register buffer object swapout\n");
1377 atomic_set(&glob
->bo_count
, 0);
1379 ret
= kobject_init_and_add(
1380 &glob
->kobj
, &ttm_bo_glob_kobj_type
, ttm_get_kobj(), "buffer_objects");
1381 if (unlikely(ret
!= 0))
1382 kobject_put(&glob
->kobj
);
1385 __free_page(glob
->dummy_read_page
);
1390 EXPORT_SYMBOL(ttm_bo_global_init
);
1393 int ttm_bo_device_release(struct ttm_bo_device
*bdev
)
1396 unsigned i
= TTM_NUM_MEM_TYPES
;
1397 struct ttm_mem_type_manager
*man
;
1398 struct ttm_bo_global
*glob
= bdev
->glob
;
1401 man
= &bdev
->man
[i
];
1402 if (man
->has_type
) {
1403 man
->use_type
= false;
1404 if ((i
!= TTM_PL_SYSTEM
) && ttm_bo_clean_mm(bdev
, i
)) {
1406 pr_err("DRM memory manager type %d is not clean\n",
1409 man
->has_type
= false;
1413 mutex_lock(&glob
->device_list_mutex
);
1414 list_del(&bdev
->device_list
);
1415 mutex_unlock(&glob
->device_list_mutex
);
1417 cancel_delayed_work_sync(&bdev
->wq
);
1419 while (ttm_bo_delayed_delete(bdev
, true))
1422 spin_lock(&glob
->lru_lock
);
1423 if (list_empty(&bdev
->ddestroy
))
1424 TTM_DEBUG("Delayed destroy list was clean\n");
1426 if (list_empty(&bdev
->man
[0].lru
))
1427 TTM_DEBUG("Swap list was clean\n");
1428 spin_unlock(&glob
->lru_lock
);
1430 drm_vma_offset_manager_destroy(&bdev
->vma_manager
);
1434 EXPORT_SYMBOL(ttm_bo_device_release
);
1436 int ttm_bo_device_init(struct ttm_bo_device
*bdev
,
1437 struct ttm_bo_global
*glob
,
1438 struct ttm_bo_driver
*driver
,
1439 struct address_space
*mapping
,
1440 uint64_t file_page_offset
,
1445 bdev
->driver
= driver
;
1447 memset(bdev
->man
, 0, sizeof(bdev
->man
));
1450 * Initialize the system memory buffer type.
1451 * Other types need to be driver / IOCTL initialized.
1453 ret
= ttm_bo_init_mm(bdev
, TTM_PL_SYSTEM
, 0);
1454 if (unlikely(ret
!= 0))
1457 drm_vma_offset_manager_init(&bdev
->vma_manager
, file_page_offset
,
1459 INIT_DELAYED_WORK(&bdev
->wq
, ttm_bo_delayed_workqueue
);
1460 INIT_LIST_HEAD(&bdev
->ddestroy
);
1461 bdev
->dev_mapping
= mapping
;
1463 bdev
->need_dma32
= need_dma32
;
1465 spin_lock_init(&bdev
->fence_lock
);
1466 mutex_lock(&glob
->device_list_mutex
);
1467 list_add_tail(&bdev
->device_list
, &glob
->device_list
);
1468 mutex_unlock(&glob
->device_list_mutex
);
1474 EXPORT_SYMBOL(ttm_bo_device_init
);
1477 * buffer object vm functions.
1480 bool ttm_mem_reg_is_pci(struct ttm_bo_device
*bdev
, struct ttm_mem_reg
*mem
)
1482 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem
->mem_type
];
1484 if (!(man
->flags
& TTM_MEMTYPE_FLAG_FIXED
)) {
1485 if (mem
->mem_type
== TTM_PL_SYSTEM
)
1488 if (man
->flags
& TTM_MEMTYPE_FLAG_CMA
)
1491 if (mem
->placement
& TTM_PL_FLAG_CACHED
)
1497 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object
*bo
)
1499 struct ttm_bo_device
*bdev
= bo
->bdev
;
1501 drm_vma_node_unmap(&bo
->vma_node
, bdev
->dev_mapping
);
1502 ttm_mem_io_free_vm(bo
);
1505 void ttm_bo_unmap_virtual(struct ttm_buffer_object
*bo
)
1507 struct ttm_bo_device
*bdev
= bo
->bdev
;
1508 struct ttm_mem_type_manager
*man
= &bdev
->man
[bo
->mem
.mem_type
];
1510 ttm_mem_io_lock(man
, false);
1511 ttm_bo_unmap_virtual_locked(bo
);
1512 ttm_mem_io_unlock(man
);
1516 EXPORT_SYMBOL(ttm_bo_unmap_virtual
);
1519 int ttm_bo_wait(struct ttm_buffer_object
*bo
,
1520 bool lazy
, bool interruptible
, bool no_wait
)
1522 struct ttm_bo_driver
*driver
= bo
->bdev
->driver
;
1523 struct ttm_bo_device
*bdev
= bo
->bdev
;
1527 if (likely(bo
->sync_obj
== NULL
))
1530 while (bo
->sync_obj
) {
1532 if (driver
->sync_obj_signaled(bo
->sync_obj
)) {
1533 void *tmp_obj
= bo
->sync_obj
;
1534 bo
->sync_obj
= NULL
;
1535 clear_bit(TTM_BO_PRIV_FLAG_MOVING
, &bo
->priv_flags
);
1536 spin_unlock(&bdev
->fence_lock
);
1537 driver
->sync_obj_unref(&tmp_obj
);
1538 spin_lock(&bdev
->fence_lock
);
1545 sync_obj
= driver
->sync_obj_ref(bo
->sync_obj
);
1546 spin_unlock(&bdev
->fence_lock
);
1547 ret
= driver
->sync_obj_wait(sync_obj
,
1548 lazy
, interruptible
);
1549 if (unlikely(ret
!= 0)) {
1550 driver
->sync_obj_unref(&sync_obj
);
1551 spin_lock(&bdev
->fence_lock
);
1554 spin_lock(&bdev
->fence_lock
);
1555 if (likely(bo
->sync_obj
== sync_obj
)) {
1556 void *tmp_obj
= bo
->sync_obj
;
1557 bo
->sync_obj
= NULL
;
1558 clear_bit(TTM_BO_PRIV_FLAG_MOVING
,
1560 spin_unlock(&bdev
->fence_lock
);
1561 driver
->sync_obj_unref(&sync_obj
);
1562 driver
->sync_obj_unref(&tmp_obj
);
1563 spin_lock(&bdev
->fence_lock
);
1565 spin_unlock(&bdev
->fence_lock
);
1566 driver
->sync_obj_unref(&sync_obj
);
1567 spin_lock(&bdev
->fence_lock
);
1572 EXPORT_SYMBOL(ttm_bo_wait
);
1574 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object
*bo
, bool no_wait
)
1576 struct ttm_bo_device
*bdev
= bo
->bdev
;
1580 * Using ttm_bo_reserve makes sure the lru lists are updated.
1583 ret
= ttm_bo_reserve(bo
, true, no_wait
, false, NULL
);
1584 if (unlikely(ret
!= 0))
1586 spin_lock(&bdev
->fence_lock
);
1587 ret
= ttm_bo_wait(bo
, false, true, no_wait
);
1588 spin_unlock(&bdev
->fence_lock
);
1589 if (likely(ret
== 0))
1590 atomic_inc(&bo
->cpu_writers
);
1591 ttm_bo_unreserve(bo
);
1594 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab
);
1596 void ttm_bo_synccpu_write_release(struct ttm_buffer_object
*bo
)
1598 atomic_dec(&bo
->cpu_writers
);
1600 EXPORT_SYMBOL(ttm_bo_synccpu_write_release
);
1603 * A buffer object shrink method that tries to swap out the first
1604 * buffer object on the bo_global::swap_lru list.
1607 static int ttm_bo_swapout(struct ttm_mem_shrink
*shrink
)
1609 struct ttm_bo_global
*glob
=
1610 container_of(shrink
, struct ttm_bo_global
, shrink
);
1611 struct ttm_buffer_object
*bo
;
1614 uint32_t swap_placement
= (TTM_PL_FLAG_CACHED
| TTM_PL_FLAG_SYSTEM
);
1616 spin_lock(&glob
->lru_lock
);
1617 list_for_each_entry(bo
, &glob
->swap_lru
, swap
) {
1618 ret
= __ttm_bo_reserve(bo
, false, true, false, NULL
);
1624 spin_unlock(&glob
->lru_lock
);
1628 kref_get(&bo
->list_kref
);
1630 if (!list_empty(&bo
->ddestroy
)) {
1631 ret
= ttm_bo_cleanup_refs_and_unlock(bo
, false, false);
1632 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1636 put_count
= ttm_bo_del_from_lru(bo
);
1637 spin_unlock(&glob
->lru_lock
);
1639 ttm_bo_list_ref_sub(bo
, put_count
, true);
1642 * Wait for GPU, then move to system cached.
1645 spin_lock(&bo
->bdev
->fence_lock
);
1646 ret
= ttm_bo_wait(bo
, false, false, false);
1647 spin_unlock(&bo
->bdev
->fence_lock
);
1649 if (unlikely(ret
!= 0))
1652 if ((bo
->mem
.placement
& swap_placement
) != swap_placement
) {
1653 struct ttm_mem_reg evict_mem
;
1655 evict_mem
= bo
->mem
;
1656 evict_mem
.mm_node
= NULL
;
1657 evict_mem
.placement
= TTM_PL_FLAG_SYSTEM
| TTM_PL_FLAG_CACHED
;
1658 evict_mem
.mem_type
= TTM_PL_SYSTEM
;
1660 ret
= ttm_bo_handle_move_mem(bo
, &evict_mem
, true,
1662 if (unlikely(ret
!= 0))
1666 ttm_bo_unmap_virtual(bo
);
1669 * Swap out. Buffer will be swapped in again as soon as
1670 * anyone tries to access a ttm page.
1673 if (bo
->bdev
->driver
->swap_notify
)
1674 bo
->bdev
->driver
->swap_notify(bo
);
1676 ret
= ttm_tt_swapout(bo
->ttm
, bo
->persistent_swap_storage
);
1681 * Unreserve without putting on LRU to avoid swapping out an
1682 * already swapped buffer.
1685 __ttm_bo_unreserve(bo
);
1686 kref_put(&bo
->list_kref
, ttm_bo_release_list
);
1690 void ttm_bo_swapout_all(struct ttm_bo_device
*bdev
)
1692 while (ttm_bo_swapout(&bdev
->glob
->shrink
) == 0)
1695 EXPORT_SYMBOL(ttm_bo_swapout_all
);
1698 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1701 * @bo: Pointer to buffer
1703 int ttm_bo_wait_unreserved(struct ttm_buffer_object
*bo
)
1708 * In the absense of a wait_unlocked API,
1709 * Use the bo::wu_mutex to avoid triggering livelocks due to
1710 * concurrent use of this function. Note that this use of
1711 * bo::wu_mutex can go away if we change locking order to
1712 * mmap_sem -> bo::reserve.
1714 ret
= mutex_lock_interruptible(&bo
->wu_mutex
);
1715 if (unlikely(ret
!= 0))
1716 return -ERESTARTSYS
;
1717 if (!ww_mutex_is_locked(&bo
->resv
->lock
))
1719 ret
= __ttm_bo_reserve(bo
, true, false, false, NULL
);
1720 if (unlikely(ret
!= 0))
1722 __ttm_bo_unreserve(bo
);
1725 mutex_unlock(&bo
->wu_mutex
);