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 for (i = 0; i <= TTM_PL_PRIV5; i++) {
63 if (place->flags & (1 << i)) {
71 static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
73 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
75 pr_err(" has_type: %d\n", man->has_type);
76 pr_err(" use_type: %d\n", man->use_type);
77 pr_err(" flags: 0x%08X\n", man->flags);
78 pr_err(" gpu_offset: 0x%08lX\n", man->gpu_offset);
79 pr_err(" size: %ju\n", man->size);
80 pr_err(" available_caching: 0x%08X\n", man->available_caching);
81 pr_err(" default_caching: 0x%08X\n", man->default_caching);
82 if (mem_type != TTM_PL_SYSTEM)
83 (*man->func->debug)(man, TTM_PFX);
86 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
87 struct ttm_placement *placement)
91 pr_err("No space for %p (%lu pages, %luK, %luM)\n",
92 bo, bo->mem.num_pages, bo->mem.size >> 10,
94 for (i = 0; i < placement->num_placement; i++) {
95 ret = ttm_mem_type_from_place(&placement->placement[i],
99 pr_err(" placement[%d]=0x%08X (%d)\n",
100 i, placement->placement[i].flags, mem_type);
101 ttm_mem_type_debug(bo->bdev, mem_type);
105 static ssize_t ttm_bo_global_show(struct kobject *kobj,
106 struct attribute *attr,
109 struct ttm_bo_global *glob =
110 container_of(kobj, struct ttm_bo_global, kobj);
112 return snprintf(buffer, PAGE_SIZE, "%lu\n",
113 (unsigned long) atomic_read(&glob->bo_count));
116 static struct attribute *ttm_bo_global_attrs[] = {
121 static const struct sysfs_ops ttm_bo_global_ops = {
122 .show = &ttm_bo_global_show
125 static struct kobj_type ttm_bo_glob_kobj_type = {
126 .release = &ttm_bo_global_kobj_release,
127 .sysfs_ops = &ttm_bo_global_ops,
128 .default_attrs = ttm_bo_global_attrs
132 static inline uint32_t ttm_bo_type_flags(unsigned type)
137 static void ttm_bo_release_list(struct kref *list_kref)
139 struct ttm_buffer_object *bo =
140 container_of(list_kref, struct ttm_buffer_object, list_kref);
141 struct ttm_bo_device *bdev = bo->bdev;
142 size_t acc_size = bo->acc_size;
144 BUG_ON(atomic_read(&bo->list_kref.refcount));
145 BUG_ON(atomic_read(&bo->kref.refcount));
146 BUG_ON(atomic_read(&bo->cpu_writers));
147 BUG_ON(bo->mem.mm_node != NULL);
148 BUG_ON(!list_empty(&bo->lru));
149 BUG_ON(!list_empty(&bo->ddestroy));
152 ttm_tt_destroy(bo->ttm);
153 atomic_dec(&bo->glob->bo_count);
154 if (bo->resv == &bo->ttm_resv)
155 reservation_object_fini(&bo->ttm_resv);
156 mutex_destroy(&bo->wu_mutex);
162 ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
165 void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
167 struct ttm_bo_device *bdev = bo->bdev;
168 struct ttm_mem_type_manager *man;
170 lockdep_assert_held(&bo->resv->lock.base);
172 if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
174 BUG_ON(!list_empty(&bo->lru));
176 man = &bdev->man[bo->mem.mem_type];
177 list_add_tail(&bo->lru, &man->lru);
178 kref_get(&bo->list_kref);
180 if (bo->ttm != NULL) {
181 list_add_tail(&bo->swap, &bo->glob->swap_lru);
182 kref_get(&bo->list_kref);
186 EXPORT_SYMBOL(ttm_bo_add_to_lru);
188 int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
192 if (!list_empty(&bo->swap)) {
193 list_del_init(&bo->swap);
196 if (!list_empty(&bo->lru)) {
197 list_del_init(&bo->lru);
202 * TODO: Add a driver hook to delete from
203 * driver-specific LRU's here.
209 static void ttm_bo_ref_bug(struct kref *list_kref)
214 void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
217 kref_sub(&bo->list_kref, count,
218 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
221 void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
225 lockmgr(&bo->glob->lru_lock, LK_EXCLUSIVE);
226 put_count = ttm_bo_del_from_lru(bo);
227 lockmgr(&bo->glob->lru_lock, LK_RELEASE);
228 ttm_bo_list_ref_sub(bo, put_count, true);
230 EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
233 * Call bo->mutex locked.
235 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
237 struct ttm_bo_device *bdev = bo->bdev;
238 struct ttm_bo_global *glob = bo->glob;
240 uint32_t page_flags = 0;
242 TTM_ASSERT_LOCKED(&bo->mutex);
245 if (bdev->need_dma32)
246 page_flags |= TTM_PAGE_FLAG_DMA32;
249 case ttm_bo_type_device:
251 page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
252 case ttm_bo_type_kernel:
253 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
254 page_flags, glob->dummy_read_page);
255 if (unlikely(bo->ttm == NULL))
259 bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
260 page_flags | TTM_PAGE_FLAG_SG,
261 glob->dummy_read_page);
262 if (unlikely(bo->ttm == NULL)) {
266 bo->ttm->sg = bo->sg;
269 pr_err("Illegal buffer object type\n");
277 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
278 struct ttm_mem_reg *mem,
279 bool evict, bool interruptible,
282 struct ttm_bo_device *bdev = bo->bdev;
283 bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
284 bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
285 struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
286 struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
289 if (old_is_pci || new_is_pci ||
290 ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
291 ret = ttm_mem_io_lock(old_man, true);
292 if (unlikely(ret != 0))
294 ttm_bo_unmap_virtual_locked(bo);
295 ttm_mem_io_unlock(old_man);
299 * Create and bind a ttm if required.
302 if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
303 if (bo->ttm == NULL) {
304 bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
305 ret = ttm_bo_add_ttm(bo, zero);
310 ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
314 if (mem->mem_type != TTM_PL_SYSTEM) {
315 ret = ttm_tt_bind(bo->ttm, mem);
320 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
321 if (bdev->driver->move_notify)
322 bdev->driver->move_notify(bo, mem);
329 if (bdev->driver->move_notify)
330 bdev->driver->move_notify(bo, mem);
332 if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
333 !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
334 ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
335 else if (bdev->driver->move)
336 ret = bdev->driver->move(bo, evict, interruptible,
339 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
342 if (bdev->driver->move_notify) {
343 struct ttm_mem_reg tmp_mem = *mem;
346 bdev->driver->move_notify(bo, mem);
356 if (bdev->driver->invalidate_caches) {
357 ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
359 pr_err("Can not flush read caches\n");
364 if (bo->mem.mm_node) {
365 bo->offset = (bo->mem.start << PAGE_SHIFT) +
366 bdev->man[bo->mem.mem_type].gpu_offset;
367 bo->cur_placement = bo->mem.placement;
374 new_man = &bdev->man[bo->mem.mem_type];
375 if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
376 ttm_tt_unbind(bo->ttm);
377 ttm_tt_destroy(bo->ttm);
386 * Will release GPU memory type usage on destruction.
387 * This is the place to put in driver specific hooks to release
388 * driver private resources.
389 * Will release the bo::reserved lock.
392 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
394 if (bo->bdev->driver->move_notify)
395 bo->bdev->driver->move_notify(bo, NULL);
398 ttm_tt_unbind(bo->ttm);
399 ttm_tt_destroy(bo->ttm);
402 ttm_bo_mem_put(bo, &bo->mem);
404 ww_mutex_unlock (&bo->resv->lock);
407 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
409 struct reservation_object_list *fobj;
413 fobj = reservation_object_get_list(bo->resv);
414 fence = reservation_object_get_excl(bo->resv);
415 if (fence && !fence->ops->signaled)
416 fence_enable_sw_signaling(fence);
418 for (i = 0; fobj && i < fobj->shared_count; ++i) {
419 fence = rcu_dereference_protected(fobj->shared[i],
420 reservation_object_held(bo->resv));
422 if (!fence->ops->signaled)
423 fence_enable_sw_signaling(fence);
427 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
429 struct ttm_bo_device *bdev = bo->bdev;
430 struct ttm_bo_global *glob = bo->glob;
434 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
435 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
438 if (!ttm_bo_wait(bo, false, false, true)) {
439 put_count = ttm_bo_del_from_lru(bo);
441 lockmgr(&glob->lru_lock, LK_RELEASE);
442 ttm_bo_cleanup_memtype_use(bo);
444 ttm_bo_list_ref_sub(bo, put_count, true);
448 ttm_bo_flush_all_fences(bo);
451 * Make NO_EVICT bos immediately available to
452 * shrinkers, now that they are queued for
455 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
456 bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
457 ttm_bo_add_to_lru(bo);
460 __ttm_bo_unreserve(bo);
463 kref_get(&bo->list_kref);
464 list_add_tail(&bo->ddestroy, &bdev->ddestroy);
465 lockmgr(&glob->lru_lock, LK_RELEASE);
467 schedule_delayed_work(&bdev->wq,
468 ((HZ / 100) < 1) ? 1 : HZ / 100);
472 * function ttm_bo_cleanup_refs_and_unlock
473 * If bo idle, remove from delayed- and lru lists, and unref.
474 * If not idle, do nothing.
476 * Must be called with lru_lock and reservation held, this function
477 * will drop both before returning.
479 * @interruptible Any sleeps should occur interruptibly.
480 * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
483 static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
487 struct ttm_bo_global *glob = bo->glob;
491 ret = ttm_bo_wait(bo, false, false, true);
493 if (ret && !no_wait_gpu) {
495 ww_mutex_unlock(&bo->resv->lock);
496 lockmgr(&glob->lru_lock, LK_RELEASE);
498 lret = reservation_object_wait_timeout_rcu(bo->resv,
508 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
509 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
512 * We raced, and lost, someone else holds the reservation now,
513 * and is probably busy in ttm_bo_cleanup_memtype_use.
515 * Even if it's not the case, because we finished waiting any
516 * delayed destruction would succeed, so just return success
520 lockmgr(&glob->lru_lock, LK_RELEASE);
525 * remove sync_obj with ttm_bo_wait, the wait should be
526 * finished, and no new wait object should have been added.
528 ret = ttm_bo_wait(bo, false, false, true);
532 if (ret || unlikely(list_empty(&bo->ddestroy))) {
533 __ttm_bo_unreserve(bo);
534 lockmgr(&glob->lru_lock, LK_RELEASE);
538 put_count = ttm_bo_del_from_lru(bo);
539 list_del_init(&bo->ddestroy);
542 lockmgr(&glob->lru_lock, LK_RELEASE);
543 ttm_bo_cleanup_memtype_use(bo);
545 ttm_bo_list_ref_sub(bo, put_count, true);
551 * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
552 * encountered buffers.
555 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
557 struct ttm_bo_global *glob = bdev->glob;
558 struct ttm_buffer_object *entry = NULL;
561 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
562 if (list_empty(&bdev->ddestroy))
565 entry = list_first_entry(&bdev->ddestroy,
566 struct ttm_buffer_object, ddestroy);
567 kref_get(&entry->list_kref);
570 struct ttm_buffer_object *nentry = NULL;
572 if (entry->ddestroy.next != &bdev->ddestroy) {
573 nentry = list_first_entry(&entry->ddestroy,
574 struct ttm_buffer_object, ddestroy);
575 kref_get(&nentry->list_kref);
578 ret = __ttm_bo_reserve(entry, false, true, false, 0);
579 if (remove_all && ret) {
580 lockmgr(&glob->lru_lock, LK_RELEASE);
581 ret = __ttm_bo_reserve(entry, false, false,
583 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
587 ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
590 lockmgr(&glob->lru_lock, LK_RELEASE);
592 kref_put(&entry->list_kref, ttm_bo_release_list);
598 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
599 if (list_empty(&entry->ddestroy))
604 lockmgr(&glob->lru_lock, LK_RELEASE);
607 kref_put(&entry->list_kref, ttm_bo_release_list);
611 static void ttm_bo_delayed_workqueue(struct work_struct *work)
613 struct ttm_bo_device *bdev =
614 container_of(work, struct ttm_bo_device, wq.work);
616 if (ttm_bo_delayed_delete(bdev, false)) {
617 schedule_delayed_work(&bdev->wq,
618 ((HZ / 100) < 1) ? 1 : HZ / 100);
622 static void ttm_bo_release(struct kref *kref)
624 struct ttm_buffer_object *bo =
625 container_of(kref, struct ttm_buffer_object, kref);
626 struct ttm_bo_device *bdev = bo->bdev;
627 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
629 drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
630 ttm_mem_io_lock(man, false);
631 ttm_mem_io_free_vm(bo);
632 ttm_mem_io_unlock(man);
633 ttm_bo_cleanup_refs_or_queue(bo);
634 kref_put(&bo->list_kref, ttm_bo_release_list);
637 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
639 struct ttm_buffer_object *bo = *p_bo;
642 kref_put(&bo->kref, ttm_bo_release);
644 EXPORT_SYMBOL(ttm_bo_unref);
646 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
648 return cancel_delayed_work_sync(&bdev->wq);
650 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
652 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
655 schedule_delayed_work(&bdev->wq,
656 ((HZ / 100) < 1) ? 1 : HZ / 100);
658 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
660 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
663 struct ttm_bo_device *bdev = bo->bdev;
664 struct ttm_mem_reg evict_mem;
665 struct ttm_placement placement;
668 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
670 if (unlikely(ret != 0)) {
671 if (ret != -ERESTARTSYS) {
672 pr_err("Failed to expire sync object before buffer eviction\n");
677 lockdep_assert_held(&bo->resv->lock.base);
680 evict_mem.mm_node = NULL;
681 evict_mem.bus.io_reserved_vm = false;
682 evict_mem.bus.io_reserved_count = 0;
684 placement.num_placement = 0;
685 placement.num_busy_placement = 0;
686 bdev->driver->evict_flags(bo, &placement);
687 ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
690 if (ret != -ERESTARTSYS) {
691 pr_err("Failed to find memory space for buffer 0x%p eviction\n",
693 ttm_bo_mem_space_debug(bo, &placement);
698 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
701 if (ret != -ERESTARTSYS)
702 pr_err("Buffer eviction failed\n");
703 ttm_bo_mem_put(bo, &evict_mem);
711 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
713 const struct ttm_place *place,
717 struct ttm_bo_global *glob = bdev->glob;
718 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
719 struct ttm_buffer_object *bo;
720 int ret = -EBUSY, put_count;
722 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
723 list_for_each_entry(bo, &man->lru, lru) {
724 ret = __ttm_bo_reserve(bo, false, true, false, NULL);
726 if (place && (place->fpfn || place->lpfn)) {
727 /* Don't evict this BO if it's outside of the
728 * requested placement range
730 if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
731 (place->lpfn && place->lpfn <= bo->mem.start)) {
732 __ttm_bo_unreserve(bo);
743 lockmgr(&glob->lru_lock, LK_RELEASE);
747 kref_get(&bo->list_kref);
749 if (!list_empty(&bo->ddestroy)) {
750 ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
752 kref_put(&bo->list_kref, ttm_bo_release_list);
756 put_count = ttm_bo_del_from_lru(bo);
757 lockmgr(&glob->lru_lock, LK_RELEASE);
761 ttm_bo_list_ref_sub(bo, put_count, true);
763 ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
764 ttm_bo_unreserve(bo);
766 kref_put(&bo->list_kref, ttm_bo_release_list);
770 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
772 struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
775 (*man->func->put_node)(man, mem);
777 EXPORT_SYMBOL(ttm_bo_mem_put);
780 * Repeatedly evict memory from the LRU for @mem_type until we create enough
781 * space, or we've evicted everything and there isn't enough space.
783 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
785 const struct ttm_place *place,
786 struct ttm_mem_reg *mem,
790 struct ttm_bo_device *bdev = bo->bdev;
791 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
795 ret = (*man->func->get_node)(man, bo, place, mem);
796 if (unlikely(ret != 0))
800 ret = ttm_mem_evict_first(bdev, mem_type, place,
801 interruptible, no_wait_gpu);
802 if (unlikely(ret != 0))
805 if (mem->mm_node == NULL)
807 mem->mem_type = mem_type;
811 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
812 uint32_t cur_placement,
813 uint32_t proposed_placement)
815 uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
816 uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
819 * Keep current caching if possible.
822 if ((cur_placement & caching) != 0)
823 result |= (cur_placement & caching);
824 else if ((man->default_caching & caching) != 0)
825 result |= man->default_caching;
826 else if ((TTM_PL_FLAG_CACHED & caching) != 0)
827 result |= TTM_PL_FLAG_CACHED;
828 else if ((TTM_PL_FLAG_WC & caching) != 0)
829 result |= TTM_PL_FLAG_WC;
830 else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
831 result |= TTM_PL_FLAG_UNCACHED;
836 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
838 const struct ttm_place *place,
839 uint32_t *masked_placement)
841 uint32_t cur_flags = ttm_bo_type_flags(mem_type);
843 if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
846 if ((place->flags & man->available_caching) == 0)
849 cur_flags |= (place->flags & man->available_caching);
851 *masked_placement = cur_flags;
856 * Creates space for memory region @mem according to its type.
858 * This function first searches for free space in compatible memory types in
859 * the priority order defined by the driver. If free space isn't found, then
860 * ttm_bo_mem_force_space is attempted in priority order to evict and find
863 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
864 struct ttm_placement *placement,
865 struct ttm_mem_reg *mem,
869 struct ttm_bo_device *bdev = bo->bdev;
870 struct ttm_mem_type_manager *man;
871 uint32_t mem_type = TTM_PL_SYSTEM;
872 uint32_t cur_flags = 0;
873 bool type_found = false;
874 bool type_ok = false;
875 bool has_erestartsys = false;
879 for (i = 0; i < placement->num_placement; ++i) {
880 const struct ttm_place *place = &placement->placement[i];
882 ret = ttm_mem_type_from_place(place, &mem_type);
885 man = &bdev->man[mem_type];
887 type_ok = ttm_bo_mt_compatible(man, mem_type, place,
893 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
896 * Use the access and other non-mapping-related flag bits from
897 * the memory placement flags to the current flags
899 ttm_flag_masked(&cur_flags, place->flags,
900 ~TTM_PL_MASK_MEMTYPE);
902 if (mem_type == TTM_PL_SYSTEM)
905 if (man->has_type && man->use_type) {
907 ret = (*man->func->get_node)(man, bo, place, mem);
915 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
916 mem->mem_type = mem_type;
917 mem->placement = cur_flags;
924 for (i = 0; i < placement->num_busy_placement; ++i) {
925 const struct ttm_place *place = &placement->busy_placement[i];
927 ret = ttm_mem_type_from_place(place, &mem_type);
930 man = &bdev->man[mem_type];
933 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
936 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
939 * Use the access and other non-mapping-related flag bits from
940 * the memory placement flags to the current flags
942 ttm_flag_masked(&cur_flags, place->flags,
943 ~TTM_PL_MASK_MEMTYPE);
945 if (mem_type == TTM_PL_SYSTEM) {
946 mem->mem_type = mem_type;
947 mem->placement = cur_flags;
952 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
953 interruptible, no_wait_gpu);
954 if (ret == 0 && mem->mm_node) {
955 mem->placement = cur_flags;
958 if (ret == -ERESTARTSYS)
959 has_erestartsys = true;
961 ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
964 EXPORT_SYMBOL(ttm_bo_mem_space);
966 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
967 struct ttm_placement *placement,
972 struct ttm_mem_reg mem;
974 lockdep_assert_held(&bo->resv->lock.base);
977 * FIXME: It's possible to pipeline buffer moves.
978 * Have the driver move function wait for idle when necessary,
979 * instead of doing it here.
981 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
984 mem.num_pages = bo->num_pages;
985 mem.size = mem.num_pages << PAGE_SHIFT;
986 mem.page_alignment = bo->mem.page_alignment;
987 mem.bus.io_reserved_vm = false;
988 mem.bus.io_reserved_count = 0;
990 * Determine where to move the buffer.
992 ret = ttm_bo_mem_space(bo, placement, &mem,
993 interruptible, no_wait_gpu);
996 ret = ttm_bo_handle_move_mem(bo, &mem, false,
997 interruptible, no_wait_gpu);
999 if (ret && mem.mm_node)
1000 ttm_bo_mem_put(bo, &mem);
1004 static bool ttm_bo_mem_compat(struct ttm_placement *placement,
1005 struct ttm_mem_reg *mem,
1006 uint32_t *new_flags)
1010 for (i = 0; i < placement->num_placement; i++) {
1011 const struct ttm_place *heap = &placement->placement[i];
1013 (mem->start < heap->fpfn ||
1014 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1017 *new_flags = heap->flags;
1018 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1019 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1023 for (i = 0; i < placement->num_busy_placement; i++) {
1024 const struct ttm_place *heap = &placement->busy_placement[i];
1026 (mem->start < heap->fpfn ||
1027 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1030 *new_flags = heap->flags;
1031 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1032 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1039 int ttm_bo_validate(struct ttm_buffer_object *bo,
1040 struct ttm_placement *placement,
1047 lockdep_assert_held(&bo->resv->lock.base);
1049 * Check whether we need to move buffer.
1051 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1052 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1058 * Use the access and other non-mapping-related flag bits from
1059 * the compatible memory placement flags to the active flags
1061 ttm_flag_masked(&bo->mem.placement, new_flags,
1062 ~TTM_PL_MASK_MEMTYPE);
1065 * We might need to add a TTM.
1067 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1068 ret = ttm_bo_add_ttm(bo, true);
1074 EXPORT_SYMBOL(ttm_bo_validate);
1076 int ttm_bo_init(struct ttm_bo_device *bdev,
1077 struct ttm_buffer_object *bo,
1079 enum ttm_bo_type type,
1080 struct ttm_placement *placement,
1081 uint32_t page_alignment,
1083 struct vm_object *persistent_swap_storage,
1085 struct sg_table *sg,
1086 struct reservation_object *resv,
1087 void (*destroy) (struct ttm_buffer_object *))
1090 unsigned long num_pages;
1091 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1094 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1096 pr_err("Out of kernel memory\n");
1104 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1105 if (num_pages == 0) {
1106 pr_err("Illegal buffer object size\n");
1111 ttm_mem_global_free(mem_glob, acc_size);
1114 bo->destroy = destroy;
1116 kref_init(&bo->kref);
1117 kref_init(&bo->list_kref);
1118 atomic_set(&bo->cpu_writers, 0);
1119 INIT_LIST_HEAD(&bo->lru);
1120 INIT_LIST_HEAD(&bo->ddestroy);
1121 INIT_LIST_HEAD(&bo->swap);
1122 INIT_LIST_HEAD(&bo->io_reserve_lru);
1123 lockinit(&bo->wu_mutex, "ttmbwm", 0, LK_CANRECURSE);
1125 bo->glob = bdev->glob;
1127 bo->num_pages = num_pages;
1128 bo->mem.size = num_pages << PAGE_SHIFT;
1129 bo->mem.mem_type = TTM_PL_SYSTEM;
1130 bo->mem.num_pages = bo->num_pages;
1131 bo->mem.mm_node = NULL;
1132 bo->mem.page_alignment = page_alignment;
1133 bo->mem.bus.io_reserved_vm = false;
1134 bo->mem.bus.io_reserved_count = 0;
1136 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1137 bo->persistent_swap_storage = persistent_swap_storage;
1138 bo->acc_size = acc_size;
1142 lockdep_assert_held(&bo->resv->lock.base);
1144 bo->resv = &bo->ttm_resv;
1145 reservation_object_init(&bo->ttm_resv);
1147 atomic_inc(&bo->glob->bo_count);
1148 drm_vma_node_reset(&bo->vma_node);
1151 * For ttm_bo_type_device buffers, allocate
1152 * address space from the device.
1154 if (bo->type == ttm_bo_type_device ||
1155 bo->type == ttm_bo_type_sg)
1156 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
1159 /* passed reservation objects should already be locked,
1160 * since otherwise lockdep will be angered in radeon.
1163 locked = ww_mutex_trylock(&bo->resv->lock);
1168 ret = ttm_bo_validate(bo, placement, interruptible, false);
1171 ttm_bo_unreserve(bo);
1178 EXPORT_SYMBOL(ttm_bo_init);
1180 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1181 unsigned long bo_size,
1182 unsigned struct_size)
1184 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1187 size += ttm_round_pot(struct_size);
1188 size += PAGE_ALIGN(npages * sizeof(void *));
1189 size += ttm_round_pot(sizeof(struct ttm_tt));
1192 EXPORT_SYMBOL(ttm_bo_acc_size);
1194 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1195 unsigned long bo_size,
1196 unsigned struct_size)
1198 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1201 size += ttm_round_pot(struct_size);
1202 size += PAGE_ALIGN(npages * sizeof(void *));
1203 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1204 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1207 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1209 int ttm_bo_create(struct ttm_bo_device *bdev,
1211 enum ttm_bo_type type,
1212 struct ttm_placement *placement,
1213 uint32_t page_alignment,
1215 struct vm_object *persistent_swap_storage,
1216 struct ttm_buffer_object **p_bo)
1218 struct ttm_buffer_object *bo;
1222 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1223 if (unlikely(bo == NULL))
1226 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1227 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1228 interruptible, persistent_swap_storage, acc_size,
1230 if (likely(ret == 0))
1235 EXPORT_SYMBOL(ttm_bo_create);
1237 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1238 unsigned mem_type, bool allow_errors)
1240 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1241 struct ttm_bo_global *glob = bdev->glob;
1245 * Can't use standard list traversal since we're unlocking.
1248 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1249 while (!list_empty(&man->lru)) {
1250 lockmgr(&glob->lru_lock, LK_RELEASE);
1251 ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
1256 pr_err("Cleanup eviction failed\n");
1259 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1261 lockmgr(&glob->lru_lock, LK_RELEASE);
1265 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1267 struct ttm_mem_type_manager *man;
1270 if (mem_type >= TTM_NUM_MEM_TYPES) {
1271 pr_err("Illegal memory type %d\n", mem_type);
1274 man = &bdev->man[mem_type];
1276 if (!man->has_type) {
1277 pr_err("Trying to take down uninitialized memory manager type %u\n",
1282 man->use_type = false;
1283 man->has_type = false;
1287 ttm_bo_force_list_clean(bdev, mem_type, false);
1289 ret = (*man->func->takedown)(man);
1294 EXPORT_SYMBOL(ttm_bo_clean_mm);
1296 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1298 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1300 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1301 pr_err("Illegal memory manager memory type %u\n", mem_type);
1305 if (!man->has_type) {
1306 pr_err("Memory type %u has not been initialized\n", mem_type);
1310 return ttm_bo_force_list_clean(bdev, mem_type, true);
1312 EXPORT_SYMBOL(ttm_bo_evict_mm);
1314 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1315 unsigned long p_size)
1318 struct ttm_mem_type_manager *man;
1320 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1321 man = &bdev->man[type];
1322 BUG_ON(man->has_type);
1323 man->io_reserve_fastpath = true;
1324 man->use_io_reserve_lru = false;
1325 lockinit(&man->io_reserve_mutex, "ttmior", 0, 0);
1326 INIT_LIST_HEAD(&man->io_reserve_lru);
1328 ret = bdev->driver->init_mem_type(bdev, type, man);
1334 if (type != TTM_PL_SYSTEM) {
1335 ret = (*man->func->init)(man, p_size);
1339 man->has_type = true;
1340 man->use_type = true;
1343 INIT_LIST_HEAD(&man->lru);
1347 EXPORT_SYMBOL(ttm_bo_init_mm);
1349 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1351 struct ttm_bo_global *glob =
1352 container_of(kobj, struct ttm_bo_global, kobj);
1354 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1355 __free_page(glob->dummy_read_page);
1359 void ttm_bo_global_release(struct drm_global_reference *ref)
1361 struct ttm_bo_global *glob = ref->object;
1363 kobject_del(&glob->kobj);
1364 kobject_put(&glob->kobj);
1366 EXPORT_SYMBOL(ttm_bo_global_release);
1368 int ttm_bo_global_init(struct drm_global_reference *ref)
1370 struct ttm_bo_global_ref *bo_ref =
1371 container_of(ref, struct ttm_bo_global_ref, ref);
1372 struct ttm_bo_global *glob = ref->object;
1375 lockinit(&glob->device_list_mutex, "ttmdlm", 0, 0);
1376 lockinit(&glob->lru_lock, "ttmlru", 0, 0);
1377 glob->mem_glob = bo_ref->mem_glob;
1378 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1380 if (unlikely(glob->dummy_read_page == NULL)) {
1385 INIT_LIST_HEAD(&glob->swap_lru);
1386 INIT_LIST_HEAD(&glob->device_list);
1388 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1389 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1390 if (unlikely(ret != 0)) {
1391 pr_err("Could not register buffer object swapout\n");
1395 atomic_set(&glob->bo_count, 0);
1397 ret = kobject_init_and_add(
1398 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1399 if (unlikely(ret != 0))
1400 kobject_put(&glob->kobj);
1403 __free_page(glob->dummy_read_page);
1408 EXPORT_SYMBOL(ttm_bo_global_init);
1411 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1414 unsigned i = TTM_NUM_MEM_TYPES;
1415 struct ttm_mem_type_manager *man;
1416 struct ttm_bo_global *glob = bdev->glob;
1419 man = &bdev->man[i];
1420 if (man->has_type) {
1421 man->use_type = false;
1422 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1424 pr_err("DRM memory manager type %d is not clean\n",
1427 man->has_type = false;
1431 mutex_lock(&glob->device_list_mutex);
1432 list_del(&bdev->device_list);
1433 mutex_unlock(&glob->device_list_mutex);
1435 cancel_delayed_work_sync(&bdev->wq);
1437 while (ttm_bo_delayed_delete(bdev, true))
1440 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1441 if (list_empty(&bdev->ddestroy))
1442 TTM_DEBUG("Delayed destroy list was clean\n");
1444 if (list_empty(&bdev->man[0].lru))
1445 TTM_DEBUG("Swap list was clean\n");
1446 lockmgr(&glob->lru_lock, LK_RELEASE);
1448 drm_vma_offset_manager_destroy(&bdev->vma_manager);
1452 EXPORT_SYMBOL(ttm_bo_device_release);
1454 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1455 struct ttm_bo_global *glob,
1456 struct ttm_bo_driver *driver,
1457 struct address_space *mapping,
1458 uint64_t file_page_offset,
1463 bdev->driver = driver;
1465 memset(bdev->man, 0, sizeof(bdev->man));
1468 * Initialize the system memory buffer type.
1469 * Other types need to be driver / IOCTL initialized.
1471 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1472 if (unlikely(ret != 0))
1475 drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
1477 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1478 INIT_LIST_HEAD(&bdev->ddestroy);
1480 * XXX DRAGONFLY - dev_mapping NULL atm, find other XXX DRAGONFLY
1481 * lines and fix when it no longer is in later API change.
1483 bdev->dev_mapping = mapping;
1485 bdev->need_dma32 = need_dma32;
1487 mutex_lock(&glob->device_list_mutex);
1488 list_add_tail(&bdev->device_list, &glob->device_list);
1489 mutex_unlock(&glob->device_list_mutex);
1495 EXPORT_SYMBOL(ttm_bo_device_init);
1498 * buffer object vm functions.
1501 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1503 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1505 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1506 if (mem->mem_type == TTM_PL_SYSTEM)
1509 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1512 if (mem->placement & TTM_PL_FLAG_CACHED)
1518 #ifdef __DragonFly__
1521 * XXX DRAGONFLY - device_mapping not yet implemented so
1522 * file_mapping is basically always NULL. We have to properly
1523 * release the mmap, etc.
1525 void ttm_bo_release_mmap(struct ttm_buffer_object *bo);
1528 * drm_vma_node_unmap() - Unmap offset node
1529 * @node: Offset node
1530 * @file_mapping: Address space to unmap @node from
1532 * Unmap all userspace mappings for a given offset node. The mappings must be
1533 * associated with the @file_mapping address-space. If no offset exists or
1534 * the address-space is invalid, nothing is done.
1536 * This call is unlocked. The caller must guarantee that drm_vma_offset_remove()
1537 * is not called on this node concurrently.
1539 static inline void drm_vma_node_unmap(struct drm_vma_offset_node *node,
1540 struct address_space *file_mapping)
1542 struct ttm_buffer_object *bo = container_of(node, struct ttm_buffer_object, vma_node);
1544 if (drm_vma_node_has_offset(node))
1545 unmap_mapping_range(file_mapping,
1546 drm_vma_node_offset_addr(node),
1547 drm_vma_node_size(node) << PAGE_SHIFT, 1);
1548 ttm_bo_release_mmap(bo);
1552 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1554 struct ttm_bo_device *bdev = bo->bdev;
1556 drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
1557 ttm_mem_io_free_vm(bo);
1560 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1562 struct ttm_bo_device *bdev = bo->bdev;
1563 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1565 ttm_mem_io_lock(man, false);
1566 ttm_bo_unmap_virtual_locked(bo);
1567 ttm_mem_io_unlock(man);
1571 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1573 int ttm_bo_wait(struct ttm_buffer_object *bo,
1574 bool lazy, bool interruptible, bool no_wait)
1576 struct reservation_object_list *fobj;
1577 struct reservation_object *resv;
1579 long timeout = 15 * HZ;
1583 fobj = reservation_object_get_list(resv);
1584 excl = reservation_object_get_excl(resv);
1586 if (!fence_is_signaled(excl)) {
1590 timeout = fence_wait_timeout(excl,
1591 interruptible, timeout);
1595 for (i = 0; fobj && timeout > 0 && i < fobj->shared_count; ++i) {
1596 struct fence *fence;
1597 fence = rcu_dereference_protected(fobj->shared[i],
1598 reservation_object_held(resv));
1600 if (!fence_is_signaled(fence)) {
1604 timeout = fence_wait_timeout(fence,
1605 interruptible, timeout);
1615 reservation_object_add_excl_fence(resv, NULL);
1616 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1619 EXPORT_SYMBOL(ttm_bo_wait);
1621 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1626 * Using ttm_bo_reserve makes sure the lru lists are updated.
1629 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1630 if (unlikely(ret != 0))
1632 ret = ttm_bo_wait(bo, false, true, no_wait);
1633 if (likely(ret == 0))
1634 atomic_inc(&bo->cpu_writers);
1635 ttm_bo_unreserve(bo);
1638 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1640 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1642 atomic_dec(&bo->cpu_writers);
1644 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1647 * A buffer object shrink method that tries to swap out the first
1648 * buffer object on the bo_global::swap_lru list.
1651 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1653 struct ttm_bo_global *glob =
1654 container_of(shrink, struct ttm_bo_global, shrink);
1655 struct ttm_buffer_object *bo;
1658 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1660 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1661 list_for_each_entry(bo, &glob->swap_lru, swap) {
1662 ret = __ttm_bo_reserve(bo, false, true, false, 0);
1668 lockmgr(&glob->lru_lock, LK_RELEASE);
1672 kref_get(&bo->list_kref);
1674 if (!list_empty(&bo->ddestroy)) {
1675 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1676 kref_put(&bo->list_kref, ttm_bo_release_list);
1680 put_count = ttm_bo_del_from_lru(bo);
1681 lockmgr(&glob->lru_lock, LK_RELEASE);
1683 ttm_bo_list_ref_sub(bo, put_count, true);
1686 * Wait for GPU, then move to system cached.
1689 ret = ttm_bo_wait(bo, false, false, false);
1691 if (unlikely(ret != 0))
1694 if ((bo->mem.placement & swap_placement) != swap_placement) {
1695 struct ttm_mem_reg evict_mem;
1697 evict_mem = bo->mem;
1698 evict_mem.mm_node = NULL;
1699 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1700 evict_mem.mem_type = TTM_PL_SYSTEM;
1702 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1704 if (unlikely(ret != 0))
1708 ttm_bo_unmap_virtual(bo);
1711 * Swap out. Buffer will be swapped in again as soon as
1712 * anyone tries to access a ttm page.
1715 if (bo->bdev->driver->swap_notify)
1716 bo->bdev->driver->swap_notify(bo);
1718 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1723 * Unreserve without putting on LRU to avoid swapping out an
1724 * already swapped buffer.
1727 __ttm_bo_unreserve(bo);
1728 kref_put(&bo->list_kref, ttm_bo_release_list);
1732 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1734 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
1737 EXPORT_SYMBOL(ttm_bo_swapout_all);
1740 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1743 * @bo: Pointer to buffer
1745 int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1750 * In the absense of a wait_unlocked API,
1751 * Use the bo::wu_mutex to avoid triggering livelocks due to
1752 * concurrent use of this function. Note that this use of
1753 * bo::wu_mutex can go away if we change locking order to
1754 * mmap_sem -> bo::reserve.
1756 ret = mutex_lock_interruptible(&bo->wu_mutex);
1757 if (unlikely(ret != 0))
1758 return -ERESTARTSYS;
1759 if (!ww_mutex_is_locked(&bo->resv->lock))
1761 ret = __ttm_bo_reserve(bo, true, false, false, NULL);
1762 if (unlikely(ret != 0))
1764 __ttm_bo_unreserve(bo);
1767 mutex_unlock(&bo->wu_mutex);