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 #define TTM_ASSERT_LOCKED(param)
46 #define TTM_DEBUG(fmt, arg...)
47 #define TTM_BO_HASH_ORDER 13
49 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
50 static void ttm_bo_global_kobj_release(struct kobject *kobj);
52 static struct attribute ttm_bo_count = {
57 static inline int ttm_mem_type_from_place(const struct ttm_place *place,
62 pos = ffs(place->flags & TTM_PL_MASK_MEM);
70 static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
72 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
74 pr_err(" has_type: %d\n", man->has_type);
75 pr_err(" use_type: %d\n", man->use_type);
76 pr_err(" flags: 0x%08X\n", man->flags);
77 pr_err(" gpu_offset: 0x%08llX\n", man->gpu_offset);
78 pr_err(" size: %llu\n", man->size);
79 pr_err(" available_caching: 0x%08X\n", man->available_caching);
80 pr_err(" default_caching: 0x%08X\n", man->default_caching);
81 if (mem_type != TTM_PL_SYSTEM)
82 (*man->func->debug)(man, TTM_PFX);
85 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
86 struct ttm_placement *placement)
90 pr_err("No space for %p (%lu pages, %luK, %luM)\n",
91 bo, bo->mem.num_pages, bo->mem.size >> 10,
93 for (i = 0; i < placement->num_placement; i++) {
94 ret = ttm_mem_type_from_place(&placement->placement[i],
98 pr_err(" placement[%d]=0x%08X (%d)\n",
99 i, placement->placement[i].flags, mem_type);
100 ttm_mem_type_debug(bo->bdev, mem_type);
104 static ssize_t ttm_bo_global_show(struct kobject *kobj,
105 struct attribute *attr,
108 struct ttm_bo_global *glob =
109 container_of(kobj, struct ttm_bo_global, kobj);
111 return snprintf(buffer, PAGE_SIZE, "%lu\n",
112 (unsigned long) atomic_read(&glob->bo_count));
115 static struct attribute *ttm_bo_global_attrs[] = {
120 static const struct sysfs_ops ttm_bo_global_ops = {
121 .show = &ttm_bo_global_show
124 static struct kobj_type ttm_bo_glob_kobj_type = {
125 .release = &ttm_bo_global_kobj_release,
126 .sysfs_ops = &ttm_bo_global_ops,
127 .default_attrs = ttm_bo_global_attrs
131 static inline uint32_t ttm_bo_type_flags(unsigned type)
136 static void ttm_bo_release_list(struct kref *list_kref)
138 struct ttm_buffer_object *bo =
139 container_of(list_kref, struct ttm_buffer_object, list_kref);
140 struct ttm_bo_device *bdev = bo->bdev;
141 size_t acc_size = bo->acc_size;
143 BUG_ON(kref_read(&bo->list_kref));
144 BUG_ON(kref_read(&bo->kref));
145 BUG_ON(atomic_read(&bo->cpu_writers));
146 BUG_ON(bo->mem.mm_node != NULL);
147 BUG_ON(!list_empty(&bo->lru));
148 BUG_ON(!list_empty(&bo->ddestroy));
149 ttm_tt_destroy(bo->ttm);
150 atomic_dec(&bo->glob->bo_count);
151 dma_fence_put(bo->moving);
152 if (bo->resv == &bo->ttm_resv)
153 reservation_object_fini(&bo->ttm_resv);
154 mutex_destroy(&bo->wu_mutex);
160 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
163 void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
165 struct ttm_bo_device *bdev = bo->bdev;
166 struct ttm_mem_type_manager *man;
168 lockdep_assert_held(&bo->resv->lock.base);
170 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
172 BUG_ON(!list_empty(&bo->lru));
174 man = &bdev->man[bo->mem.mem_type];
175 list_add_tail(&bo->lru, &man->lru[bo->priority]);
176 kref_get(&bo->list_kref);
178 if (bo->ttm && !(bo->ttm->page_flags & TTM_PAGE_FLAG_SG)) {
179 list_add_tail(&bo->swap,
180 &bo->glob->swap_lru[bo->priority]);
181 kref_get(&bo->list_kref);
185 EXPORT_SYMBOL(ttm_bo_add_to_lru);
187 static void ttm_bo_ref_bug(struct kref *list_kref)
192 void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
194 if (!list_empty(&bo->swap)) {
195 list_del_init(&bo->swap);
196 kref_put(&bo->list_kref, ttm_bo_ref_bug);
198 if (!list_empty(&bo->lru)) {
199 list_del_init(&bo->lru);
200 kref_put(&bo->list_kref, ttm_bo_ref_bug);
204 * TODO: Add a driver hook to delete from
205 * driver-specific LRU's here.
209 void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
211 spin_lock(&bo->glob->lru_lock);
212 ttm_bo_del_from_lru(bo);
213 spin_unlock(&bo->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 lockdep_assert_held(&bo->resv->lock.base);
221 ttm_bo_del_from_lru(bo);
222 ttm_bo_add_to_lru(bo);
224 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
227 * Call bo->mutex locked.
229 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
231 struct ttm_bo_device *bdev = bo->bdev;
232 struct ttm_bo_global *glob = bo->glob;
234 uint32_t page_flags = 0;
236 TTM_ASSERT_LOCKED(&bo->mutex);
239 if (bdev->need_dma32)
240 page_flags |= TTM_PAGE_FLAG_DMA32;
243 case ttm_bo_type_device:
245 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
246 case ttm_bo_type_kernel:
247 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
248 page_flags, glob->dummy_read_page);
249 if (unlikely(bo->ttm == NULL))
253 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
254 page_flags | TTM_PAGE_FLAG_SG,
255 glob->dummy_read_page);
256 if (unlikely(bo->ttm == NULL)) {
260 bo->ttm->sg = bo->sg;
263 pr_err("Illegal buffer object type\n");
271 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
272 struct ttm_mem_reg *mem,
273 bool evict, bool interruptible,
276 struct ttm_bo_device *bdev = bo->bdev;
277 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
278 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
279 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
280 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
283 if (old_is_pci || new_is_pci ||
284 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
285 ret = ttm_mem_io_lock(old_man, true);
286 if (unlikely(ret != 0))
288 ttm_bo_unmap_virtual_locked(bo);
289 ttm_mem_io_unlock(old_man);
293 * Create and bind a ttm if required.
296 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
297 if (bo->ttm == NULL) {
298 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
299 ret = ttm_bo_add_ttm(bo, zero);
304 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
308 if (mem->mem_type != TTM_PL_SYSTEM) {
309 ret = ttm_tt_bind(bo->ttm, mem);
314 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
315 if (bdev->driver->move_notify)
316 bdev->driver->move_notify(bo, evict, mem);
323 if (bdev->driver->move_notify)
324 bdev->driver->move_notify(bo, evict, mem);
326 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
327 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
328 ret = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, mem);
329 else if (bdev->driver->move)
330 ret = bdev->driver->move(bo, evict, interruptible,
333 ret = ttm_bo_move_memcpy(bo, interruptible, no_wait_gpu, mem);
336 if (bdev->driver->move_notify) {
337 struct ttm_mem_reg tmp_mem = *mem;
340 bdev->driver->move_notify(bo, false, mem);
350 if (bdev->driver->invalidate_caches) {
351 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
353 pr_err("Can not flush read caches\n");
358 if (bo->mem.mm_node) {
359 bo->offset = (bo->mem.start << PAGE_SHIFT) +
360 bdev->man[bo->mem.mem_type].gpu_offset;
361 bo->cur_placement = bo->mem.placement;
368 new_man = &bdev->man[bo->mem.mem_type];
369 if (new_man->flags & TTM_MEMTYPE_FLAG_FIXED) {
370 ttm_tt_destroy(bo->ttm);
379 * Will release GPU memory type usage on destruction.
380 * This is the place to put in driver specific hooks to release
381 * driver private resources.
382 * Will release the bo::reserved lock.
385 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
387 if (bo->bdev->driver->move_notify)
388 bo->bdev->driver->move_notify(bo, false, NULL);
390 ttm_tt_destroy(bo->ttm);
392 ttm_bo_mem_put(bo, &bo->mem);
394 ww_mutex_unlock (&bo->resv->lock);
397 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
399 struct reservation_object_list *fobj;
400 struct dma_fence *fence;
403 fobj = reservation_object_get_list(bo->resv);
404 fence = reservation_object_get_excl(bo->resv);
405 if (fence && !fence->ops->signaled)
406 dma_fence_enable_sw_signaling(fence);
408 for (i = 0; fobj && i < fobj->shared_count; ++i) {
409 fence = rcu_dereference_protected(fobj->shared[i],
410 reservation_object_held(bo->resv));
412 if (!fence->ops->signaled)
413 dma_fence_enable_sw_signaling(fence);
417 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
419 struct ttm_bo_device *bdev = bo->bdev;
420 struct ttm_bo_global *glob = bo->glob;
423 spin_lock(&glob->lru_lock);
424 ret = __ttm_bo_reserve(bo, false, true, NULL);
427 if (!ttm_bo_wait(bo, false, true)) {
428 ttm_bo_del_from_lru(bo);
429 spin_unlock(&glob->lru_lock);
430 ttm_bo_cleanup_memtype_use(bo);
434 ttm_bo_flush_all_fences(bo);
437 * Make NO_EVICT bos immediately available to
438 * shrinkers, now that they are queued for
441 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
442 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
443 ttm_bo_add_to_lru(bo);
446 __ttm_bo_unreserve(bo);
449 kref_get(&bo->list_kref);
450 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
451 spin_unlock(&glob->lru_lock);
453 schedule_delayed_work(&bdev->wq,
454 ((HZ / 100) < 1) ? 1 : HZ / 100);
458 * function ttm_bo_cleanup_refs_and_unlock
459 * If bo idle, remove from delayed- and lru lists, and unref.
460 * If not idle, do nothing.
462 * Must be called with lru_lock and reservation held, this function
463 * will drop both before returning.
465 * @interruptible Any sleeps should occur interruptibly.
466 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
469 static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
473 struct ttm_bo_global *glob = bo->glob;
476 ret = ttm_bo_wait(bo, false, true);
478 if (ret && !no_wait_gpu) {
480 ww_mutex_unlock(&bo->resv->lock);
481 spin_unlock(&glob->lru_lock);
483 lret = reservation_object_wait_timeout_rcu(bo->resv,
493 spin_lock(&glob->lru_lock);
494 ret = __ttm_bo_reserve(bo, false, true, NULL);
497 * We raced, and lost, someone else holds the reservation now,
498 * and is probably busy in ttm_bo_cleanup_memtype_use.
500 * Even if it's not the case, because we finished waiting any
501 * delayed destruction would succeed, so just return success
505 spin_unlock(&glob->lru_lock);
510 * remove sync_obj with ttm_bo_wait, the wait should be
511 * finished, and no new wait object should have been added.
513 ret = ttm_bo_wait(bo, false, true);
517 if (ret || unlikely(list_empty(&bo->ddestroy))) {
518 __ttm_bo_unreserve(bo);
519 spin_unlock(&glob->lru_lock);
523 ttm_bo_del_from_lru(bo);
524 list_del_init(&bo->ddestroy);
525 kref_put(&bo->list_kref, ttm_bo_ref_bug);
527 spin_unlock(&glob->lru_lock);
528 ttm_bo_cleanup_memtype_use(bo);
534 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
535 * encountered buffers.
538 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
540 struct ttm_bo_global *glob = bdev->glob;
541 struct ttm_buffer_object *entry = NULL;
544 spin_lock(&glob->lru_lock);
545 if (list_empty(&bdev->ddestroy))
548 entry = list_first_entry(&bdev->ddestroy,
549 struct ttm_buffer_object, ddestroy);
550 kref_get(&entry->list_kref);
553 struct ttm_buffer_object *nentry = NULL;
555 if (entry->ddestroy.next != &bdev->ddestroy) {
556 nentry = list_first_entry(&entry->ddestroy,
557 struct ttm_buffer_object, ddestroy);
558 kref_get(&nentry->list_kref);
561 ret = __ttm_bo_reserve(entry, false, true, NULL);
562 if (remove_all && ret) {
563 spin_unlock(&glob->lru_lock);
564 ret = __ttm_bo_reserve(entry, false, false, NULL);
565 spin_lock(&glob->lru_lock);
569 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
572 spin_unlock(&glob->lru_lock);
574 kref_put(&entry->list_kref, ttm_bo_release_list);
580 spin_lock(&glob->lru_lock);
581 if (list_empty(&entry->ddestroy))
586 spin_unlock(&glob->lru_lock);
589 kref_put(&entry->list_kref, ttm_bo_release_list);
593 static void ttm_bo_delayed_workqueue(struct work_struct *work)
595 struct ttm_bo_device *bdev =
596 container_of(work, struct ttm_bo_device, wq.work);
598 if (ttm_bo_delayed_delete(bdev, false)) {
599 schedule_delayed_work(&bdev->wq,
600 ((HZ / 100) < 1) ? 1 : HZ / 100);
604 static void ttm_bo_release(struct kref *kref)
606 struct ttm_buffer_object *bo =
607 container_of(kref, struct ttm_buffer_object, kref);
608 struct ttm_bo_device *bdev = bo->bdev;
609 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
611 drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
612 ttm_mem_io_lock(man, false);
613 ttm_mem_io_free_vm(bo);
614 ttm_mem_io_unlock(man);
615 ttm_bo_cleanup_refs_or_queue(bo);
616 kref_put(&bo->list_kref, ttm_bo_release_list);
619 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
621 struct ttm_buffer_object *bo = *p_bo;
624 kref_put(&bo->kref, ttm_bo_release);
626 EXPORT_SYMBOL(ttm_bo_unref);
628 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
630 return cancel_delayed_work_sync(&bdev->wq);
632 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
634 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
637 schedule_delayed_work(&bdev->wq,
638 ((HZ / 100) < 1) ? 1 : HZ / 100);
640 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
642 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
645 struct ttm_bo_device *bdev = bo->bdev;
646 struct ttm_mem_reg evict_mem;
647 struct ttm_placement placement;
650 lockdep_assert_held(&bo->resv->lock.base);
653 evict_mem.mm_node = NULL;
654 evict_mem.bus.io_reserved_vm = false;
655 evict_mem.bus.io_reserved_count = 0;
657 placement.num_placement = 0;
658 placement.num_busy_placement = 0;
659 bdev->driver->evict_flags(bo, &placement);
660 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
663 if (ret != -ERESTARTSYS) {
664 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
666 ttm_bo_mem_space_debug(bo, &placement);
671 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
674 if (ret != -ERESTARTSYS)
675 pr_err("Buffer eviction failed\n");
676 ttm_bo_mem_put(bo, &evict_mem);
684 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
685 const struct ttm_place *place)
687 /* Don't evict this BO if it's outside of the
688 * requested placement range
690 if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
691 (place->lpfn && place->lpfn <= bo->mem.start))
696 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
698 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
700 const struct ttm_place *place,
704 struct ttm_bo_global *glob = bdev->glob;
705 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
706 struct ttm_buffer_object *bo;
710 spin_lock(&glob->lru_lock);
711 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
712 list_for_each_entry(bo, &man->lru[i], lru) {
713 ret = __ttm_bo_reserve(bo, false, true, NULL);
717 if (place && !bdev->driver->eviction_valuable(bo,
719 __ttm_bo_unreserve(bo);
732 spin_unlock(&glob->lru_lock);
736 kref_get(&bo->list_kref);
738 if (!list_empty(&bo->ddestroy)) {
739 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
741 kref_put(&bo->list_kref, ttm_bo_release_list);
745 ttm_bo_del_from_lru(bo);
746 spin_unlock(&glob->lru_lock);
750 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
751 ttm_bo_unreserve(bo);
753 kref_put(&bo->list_kref, ttm_bo_release_list);
757 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
759 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
762 (*man->func->put_node)(man, mem);
764 EXPORT_SYMBOL(ttm_bo_mem_put);
767 * Add the last move fence to the BO and reserve a new shared slot.
769 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
770 struct ttm_mem_type_manager *man,
771 struct ttm_mem_reg *mem)
773 struct dma_fence *fence;
776 spin_lock(&man->move_lock);
777 fence = dma_fence_get(man->move);
778 spin_unlock(&man->move_lock);
781 reservation_object_add_shared_fence(bo->resv, fence);
783 ret = reservation_object_reserve_shared(bo->resv);
787 dma_fence_put(bo->moving);
795 * Repeatedly evict memory from the LRU for @mem_type until we create enough
796 * space, or we've evicted everything and there isn't enough space.
798 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
800 const struct ttm_place *place,
801 struct ttm_mem_reg *mem,
805 struct ttm_bo_device *bdev = bo->bdev;
806 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
810 ret = (*man->func->get_node)(man, bo, place, mem);
811 if (unlikely(ret != 0))
815 ret = ttm_mem_evict_first(bdev, mem_type, place,
816 interruptible, no_wait_gpu);
817 if (unlikely(ret != 0))
820 mem->mem_type = mem_type;
821 return ttm_bo_add_move_fence(bo, man, mem);
824 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
825 uint32_t cur_placement,
826 uint32_t proposed_placement)
828 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
829 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
832 * Keep current caching if possible.
835 if ((cur_placement & caching) != 0)
836 result |= (cur_placement & caching);
837 else if ((man->default_caching & caching) != 0)
838 result |= man->default_caching;
839 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
840 result |= TTM_PL_FLAG_CACHED;
841 else if ((TTM_PL_FLAG_WC & caching) != 0)
842 result |= TTM_PL_FLAG_WC;
843 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
844 result |= TTM_PL_FLAG_UNCACHED;
849 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
851 const struct ttm_place *place,
852 uint32_t *masked_placement)
854 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
856 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
859 if ((place->flags & man->available_caching) == 0)
862 cur_flags |= (place->flags & man->available_caching);
864 *masked_placement = cur_flags;
869 * Creates space for memory region @mem according to its type.
871 * This function first searches for free space in compatible memory types in
872 * the priority order defined by the driver. If free space isn't found, then
873 * ttm_bo_mem_force_space is attempted in priority order to evict and find
876 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
877 struct ttm_placement *placement,
878 struct ttm_mem_reg *mem,
882 struct ttm_bo_device *bdev = bo->bdev;
883 struct ttm_mem_type_manager *man;
884 uint32_t mem_type = TTM_PL_SYSTEM;
885 uint32_t cur_flags = 0;
886 bool type_found = false;
887 bool type_ok = false;
888 bool has_erestartsys = false;
891 ret = reservation_object_reserve_shared(bo->resv);
896 for (i = 0; i < placement->num_placement; ++i) {
897 const struct ttm_place *place = &placement->placement[i];
899 ret = ttm_mem_type_from_place(place, &mem_type);
902 man = &bdev->man[mem_type];
903 if (!man->has_type || !man->use_type)
906 type_ok = ttm_bo_mt_compatible(man, mem_type, place,
913 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
916 * Use the access and other non-mapping-related flag bits from
917 * the memory placement flags to the current flags
919 ttm_flag_masked(&cur_flags, place->flags,
920 ~TTM_PL_MASK_MEMTYPE);
922 if (mem_type == TTM_PL_SYSTEM)
925 ret = (*man->func->get_node)(man, bo, place, mem);
930 ret = ttm_bo_add_move_fence(bo, man, mem);
932 (*man->func->put_node)(man, mem);
939 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
940 mem->mem_type = mem_type;
941 mem->placement = cur_flags;
945 for (i = 0; i < placement->num_busy_placement; ++i) {
946 const struct ttm_place *place = &placement->busy_placement[i];
948 ret = ttm_mem_type_from_place(place, &mem_type);
951 man = &bdev->man[mem_type];
952 if (!man->has_type || !man->use_type)
954 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
958 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
961 * Use the access and other non-mapping-related flag bits from
962 * the memory placement flags to the current flags
964 ttm_flag_masked(&cur_flags, place->flags,
965 ~TTM_PL_MASK_MEMTYPE);
967 if (mem_type == TTM_PL_SYSTEM) {
968 mem->mem_type = mem_type;
969 mem->placement = cur_flags;
974 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
975 interruptible, no_wait_gpu);
976 if (ret == 0 && mem->mm_node) {
977 mem->placement = cur_flags;
980 if (ret == -ERESTARTSYS)
981 has_erestartsys = true;
985 printk(KERN_ERR TTM_PFX "No compatible memory type found.\n");
989 return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
991 EXPORT_SYMBOL(ttm_bo_mem_space);
993 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
994 struct ttm_placement *placement,
999 struct ttm_mem_reg mem;
1001 lockdep_assert_held(&bo->resv->lock.base);
1003 mem.num_pages = bo->num_pages;
1004 mem.size = mem.num_pages << PAGE_SHIFT;
1005 mem.page_alignment = bo->mem.page_alignment;
1006 mem.bus.io_reserved_vm = false;
1007 mem.bus.io_reserved_count = 0;
1009 * Determine where to move the buffer.
1011 ret = ttm_bo_mem_space(bo, placement, &mem,
1012 interruptible, no_wait_gpu);
1015 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1016 interruptible, no_wait_gpu);
1018 if (ret && mem.mm_node)
1019 ttm_bo_mem_put(bo, &mem);
1023 bool ttm_bo_mem_compat(struct ttm_placement *placement,
1024 struct ttm_mem_reg *mem,
1025 uint32_t *new_flags)
1029 for (i = 0; i < placement->num_placement; i++) {
1030 const struct ttm_place *heap = &placement->placement[i];
1032 (mem->start < heap->fpfn ||
1033 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1036 *new_flags = heap->flags;
1037 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1038 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1042 for (i = 0; i < placement->num_busy_placement; i++) {
1043 const struct ttm_place *heap = &placement->busy_placement[i];
1045 (mem->start < heap->fpfn ||
1046 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1049 *new_flags = heap->flags;
1050 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1051 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1057 EXPORT_SYMBOL(ttm_bo_mem_compat);
1059 int ttm_bo_validate(struct ttm_buffer_object *bo,
1060 struct ttm_placement *placement,
1067 lockdep_assert_held(&bo->resv->lock.base);
1069 * Check whether we need to move buffer.
1071 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1072 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1078 * Use the access and other non-mapping-related flag bits from
1079 * the compatible memory placement flags to the active flags
1081 ttm_flag_masked(&bo->mem.placement, new_flags,
1082 ~TTM_PL_MASK_MEMTYPE);
1085 * We might need to add a TTM.
1087 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1088 ret = ttm_bo_add_ttm(bo, true);
1094 EXPORT_SYMBOL(ttm_bo_validate);
1096 int ttm_bo_init(struct ttm_bo_device *bdev,
1097 struct ttm_buffer_object *bo,
1099 enum ttm_bo_type type,
1100 struct ttm_placement *placement,
1101 uint32_t page_alignment,
1103 struct file *persistent_swap_storage,
1105 struct sg_table *sg,
1106 struct reservation_object *resv,
1107 void (*destroy) (struct ttm_buffer_object *))
1110 unsigned long num_pages;
1111 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1114 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1116 pr_err("Out of kernel memory\n");
1124 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1125 if (num_pages == 0) {
1126 pr_err("Illegal buffer object size\n");
1131 ttm_mem_global_free(mem_glob, acc_size);
1134 bo->destroy = destroy;
1136 kref_init(&bo->kref);
1137 kref_init(&bo->list_kref);
1138 atomic_set(&bo->cpu_writers, 0);
1139 INIT_LIST_HEAD(&bo->lru);
1140 INIT_LIST_HEAD(&bo->ddestroy);
1141 INIT_LIST_HEAD(&bo->swap);
1142 INIT_LIST_HEAD(&bo->io_reserve_lru);
1143 mutex_init(&bo->wu_mutex);
1145 bo->glob = bdev->glob;
1147 bo->num_pages = num_pages;
1148 bo->mem.size = num_pages << PAGE_SHIFT;
1149 bo->mem.mem_type = TTM_PL_SYSTEM;
1150 bo->mem.num_pages = bo->num_pages;
1151 bo->mem.mm_node = NULL;
1152 bo->mem.page_alignment = page_alignment;
1153 bo->mem.bus.io_reserved_vm = false;
1154 bo->mem.bus.io_reserved_count = 0;
1156 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1157 bo->persistent_swap_storage = persistent_swap_storage;
1158 bo->acc_size = acc_size;
1162 lockdep_assert_held(&bo->resv->lock.base);
1164 bo->resv = &bo->ttm_resv;
1165 reservation_object_init(&bo->ttm_resv);
1167 atomic_inc(&bo->glob->bo_count);
1168 drm_vma_node_reset(&bo->vma_node);
1172 * For ttm_bo_type_device buffers, allocate
1173 * address space from the device.
1175 if (bo->type == ttm_bo_type_device ||
1176 bo->type == ttm_bo_type_sg)
1177 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
1180 /* passed reservation objects should already be locked,
1181 * since otherwise lockdep will be angered in radeon.
1184 locked = ww_mutex_trylock(&bo->resv->lock);
1189 ret = ttm_bo_validate(bo, placement, interruptible, false);
1192 ttm_bo_unreserve(bo);
1194 } else if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
1195 spin_lock(&bo->glob->lru_lock);
1196 ttm_bo_add_to_lru(bo);
1197 spin_unlock(&bo->glob->lru_lock);
1205 EXPORT_SYMBOL(ttm_bo_init);
1207 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1208 unsigned long bo_size,
1209 unsigned struct_size)
1211 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1214 size += ttm_round_pot(struct_size);
1215 size += ttm_round_pot(npages * sizeof(void *));
1216 size += ttm_round_pot(sizeof(struct ttm_tt));
1219 EXPORT_SYMBOL(ttm_bo_acc_size);
1221 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1222 unsigned long bo_size,
1223 unsigned struct_size)
1225 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1228 size += ttm_round_pot(struct_size);
1229 size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
1230 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1233 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1235 int ttm_bo_create(struct ttm_bo_device *bdev,
1237 enum ttm_bo_type type,
1238 struct ttm_placement *placement,
1239 uint32_t page_alignment,
1241 struct file *persistent_swap_storage,
1242 struct ttm_buffer_object **p_bo)
1244 struct ttm_buffer_object *bo;
1248 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1249 if (unlikely(bo == NULL))
1252 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1253 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1254 interruptible, persistent_swap_storage, acc_size,
1256 if (likely(ret == 0))
1261 EXPORT_SYMBOL(ttm_bo_create);
1263 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1266 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1267 struct ttm_bo_global *glob = bdev->glob;
1268 struct dma_fence *fence;
1273 * Can't use standard list traversal since we're unlocking.
1276 spin_lock(&glob->lru_lock);
1277 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1278 while (!list_empty(&man->lru[i])) {
1279 spin_unlock(&glob->lru_lock);
1280 ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
1283 spin_lock(&glob->lru_lock);
1286 spin_unlock(&glob->lru_lock);
1288 spin_lock(&man->move_lock);
1289 fence = dma_fence_get(man->move);
1290 spin_unlock(&man->move_lock);
1293 ret = dma_fence_wait(fence, false);
1294 dma_fence_put(fence);
1302 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1304 struct ttm_mem_type_manager *man;
1307 if (mem_type >= TTM_NUM_MEM_TYPES) {
1308 pr_err("Illegal memory type %d\n", mem_type);
1311 man = &bdev->man[mem_type];
1313 if (!man->has_type) {
1314 pr_err("Trying to take down uninitialized memory manager type %u\n",
1318 dma_fence_put(man->move);
1320 man->use_type = false;
1321 man->has_type = false;
1325 ret = ttm_bo_force_list_clean(bdev, mem_type);
1327 pr_err("Cleanup eviction failed\n");
1331 ret = (*man->func->takedown)(man);
1336 EXPORT_SYMBOL(ttm_bo_clean_mm);
1338 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1340 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1342 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1343 pr_err("Illegal memory manager memory type %u\n", mem_type);
1347 if (!man->has_type) {
1348 pr_err("Memory type %u has not been initialized\n", mem_type);
1352 return ttm_bo_force_list_clean(bdev, mem_type);
1354 EXPORT_SYMBOL(ttm_bo_evict_mm);
1356 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1357 unsigned long p_size)
1360 struct ttm_mem_type_manager *man;
1363 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1364 man = &bdev->man[type];
1365 BUG_ON(man->has_type);
1366 man->io_reserve_fastpath = true;
1367 man->use_io_reserve_lru = false;
1368 mutex_init(&man->io_reserve_mutex);
1369 spin_lock_init(&man->move_lock);
1370 INIT_LIST_HEAD(&man->io_reserve_lru);
1372 ret = bdev->driver->init_mem_type(bdev, type, man);
1378 if (type != TTM_PL_SYSTEM) {
1379 ret = (*man->func->init)(man, p_size);
1383 man->has_type = true;
1384 man->use_type = true;
1387 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1388 INIT_LIST_HEAD(&man->lru[i]);
1393 EXPORT_SYMBOL(ttm_bo_init_mm);
1395 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1397 struct ttm_bo_global *glob =
1398 container_of(kobj, struct ttm_bo_global, kobj);
1400 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1401 __free_page(glob->dummy_read_page);
1405 void ttm_bo_global_release(struct drm_global_reference *ref)
1407 struct ttm_bo_global *glob = ref->object;
1409 kobject_del(&glob->kobj);
1410 kobject_put(&glob->kobj);
1412 EXPORT_SYMBOL(ttm_bo_global_release);
1414 int ttm_bo_global_init(struct drm_global_reference *ref)
1416 struct ttm_bo_global_ref *bo_ref =
1417 container_of(ref, struct ttm_bo_global_ref, ref);
1418 struct ttm_bo_global *glob = ref->object;
1422 mutex_init(&glob->device_list_mutex);
1423 spin_lock_init(&glob->lru_lock);
1424 glob->mem_glob = bo_ref->mem_glob;
1425 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1427 if (unlikely(glob->dummy_read_page == NULL)) {
1432 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1433 INIT_LIST_HEAD(&glob->swap_lru[i]);
1434 INIT_LIST_HEAD(&glob->device_list);
1436 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1437 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1438 if (unlikely(ret != 0)) {
1439 pr_err("Could not register buffer object swapout\n");
1443 atomic_set(&glob->bo_count, 0);
1445 ret = kobject_init_and_add(
1446 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1447 if (unlikely(ret != 0))
1448 kobject_put(&glob->kobj);
1451 __free_page(glob->dummy_read_page);
1456 EXPORT_SYMBOL(ttm_bo_global_init);
1459 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1462 unsigned i = TTM_NUM_MEM_TYPES;
1463 struct ttm_mem_type_manager *man;
1464 struct ttm_bo_global *glob = bdev->glob;
1467 man = &bdev->man[i];
1468 if (man->has_type) {
1469 man->use_type = false;
1470 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1472 pr_err("DRM memory manager type %d is not clean\n",
1475 man->has_type = false;
1479 mutex_lock(&glob->device_list_mutex);
1480 list_del(&bdev->device_list);
1481 mutex_unlock(&glob->device_list_mutex);
1483 cancel_delayed_work_sync(&bdev->wq);
1485 while (ttm_bo_delayed_delete(bdev, true))
1488 spin_lock(&glob->lru_lock);
1489 if (list_empty(&bdev->ddestroy))
1490 TTM_DEBUG("Delayed destroy list was clean\n");
1492 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1493 if (list_empty(&bdev->man[0].lru[0]))
1494 TTM_DEBUG("Swap list %d was clean\n", i);
1495 spin_unlock(&glob->lru_lock);
1497 drm_vma_offset_manager_destroy(&bdev->vma_manager);
1501 EXPORT_SYMBOL(ttm_bo_device_release);
1503 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1504 struct ttm_bo_global *glob,
1505 struct ttm_bo_driver *driver,
1506 struct address_space *mapping,
1507 uint64_t file_page_offset,
1512 bdev->driver = driver;
1514 memset(bdev->man, 0, sizeof(bdev->man));
1517 * Initialize the system memory buffer type.
1518 * Other types need to be driver / IOCTL initialized.
1520 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1521 if (unlikely(ret != 0))
1524 drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
1526 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1527 INIT_LIST_HEAD(&bdev->ddestroy);
1528 bdev->dev_mapping = mapping;
1530 bdev->need_dma32 = need_dma32;
1531 mutex_lock(&glob->device_list_mutex);
1532 list_add_tail(&bdev->device_list, &glob->device_list);
1533 mutex_unlock(&glob->device_list_mutex);
1539 EXPORT_SYMBOL(ttm_bo_device_init);
1542 * buffer object vm functions.
1545 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1547 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1549 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1550 if (mem->mem_type == TTM_PL_SYSTEM)
1553 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1556 if (mem->placement & TTM_PL_FLAG_CACHED)
1562 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1564 struct ttm_bo_device *bdev = bo->bdev;
1566 drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
1567 ttm_mem_io_free_vm(bo);
1570 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1572 struct ttm_bo_device *bdev = bo->bdev;
1573 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1575 ttm_mem_io_lock(man, false);
1576 ttm_bo_unmap_virtual_locked(bo);
1577 ttm_mem_io_unlock(man);
1581 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1583 int ttm_bo_wait(struct ttm_buffer_object *bo,
1584 bool interruptible, bool no_wait)
1586 long timeout = 15 * HZ;
1589 if (reservation_object_test_signaled_rcu(bo->resv, true))
1595 timeout = reservation_object_wait_timeout_rcu(bo->resv, true,
1596 interruptible, timeout);
1603 reservation_object_add_excl_fence(bo->resv, NULL);
1606 EXPORT_SYMBOL(ttm_bo_wait);
1608 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1613 * Using ttm_bo_reserve makes sure the lru lists are updated.
1616 ret = ttm_bo_reserve(bo, true, no_wait, NULL);
1617 if (unlikely(ret != 0))
1619 ret = ttm_bo_wait(bo, true, no_wait);
1620 if (likely(ret == 0))
1621 atomic_inc(&bo->cpu_writers);
1622 ttm_bo_unreserve(bo);
1625 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1627 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1629 atomic_dec(&bo->cpu_writers);
1631 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1634 * A buffer object shrink method that tries to swap out the first
1635 * buffer object on the bo_global::swap_lru list.
1638 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1640 struct ttm_bo_global *glob =
1641 container_of(shrink, struct ttm_bo_global, shrink);
1642 struct ttm_buffer_object *bo;
1646 spin_lock(&glob->lru_lock);
1647 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1648 list_for_each_entry(bo, &glob->swap_lru[i], swap) {
1649 ret = __ttm_bo_reserve(bo, false, true, NULL);
1658 spin_unlock(&glob->lru_lock);
1662 kref_get(&bo->list_kref);
1664 if (!list_empty(&bo->ddestroy)) {
1665 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1666 kref_put(&bo->list_kref, ttm_bo_release_list);
1670 ttm_bo_del_from_lru(bo);
1671 spin_unlock(&glob->lru_lock);
1674 * Move to system cached
1677 if (bo->mem.mem_type != TTM_PL_SYSTEM ||
1678 bo->ttm->caching_state != tt_cached) {
1679 struct ttm_mem_reg evict_mem;
1681 evict_mem = bo->mem;
1682 evict_mem.mm_node = NULL;
1683 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1684 evict_mem.mem_type = TTM_PL_SYSTEM;
1686 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1688 if (unlikely(ret != 0))
1693 * Make sure BO is idle.
1696 ret = ttm_bo_wait(bo, false, false);
1697 if (unlikely(ret != 0))
1700 ttm_bo_unmap_virtual(bo);
1703 * Swap out. Buffer will be swapped in again as soon as
1704 * anyone tries to access a ttm page.
1707 if (bo->bdev->driver->swap_notify)
1708 bo->bdev->driver->swap_notify(bo);
1710 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1715 * Unreserve without putting on LRU to avoid swapping out an
1716 * already swapped buffer.
1719 __ttm_bo_unreserve(bo);
1720 kref_put(&bo->list_kref, ttm_bo_release_list);
1724 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1726 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
1729 EXPORT_SYMBOL(ttm_bo_swapout_all);
1732 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1735 * @bo: Pointer to buffer
1737 int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1742 * In the absense of a wait_unlocked API,
1743 * Use the bo::wu_mutex to avoid triggering livelocks due to
1744 * concurrent use of this function. Note that this use of
1745 * bo::wu_mutex can go away if we change locking order to
1746 * mmap_sem -> bo::reserve.
1748 ret = mutex_lock_interruptible(&bo->wu_mutex);
1749 if (unlikely(ret != 0))
1750 return -ERESTARTSYS;
1751 if (!ww_mutex_is_locked(&bo->resv->lock))
1753 ret = __ttm_bo_reserve(bo, true, false, NULL);
1754 if (unlikely(ret != 0))
1756 __ttm_bo_unreserve(bo);
1759 mutex_unlock(&bo->wu_mutex);