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];
886 if (!man->has_type || !man->use_type)
889 type_ok = ttm_bo_mt_compatible(man, mem_type, place,
896 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
899 * Use the access and other non-mapping-related flag bits from
900 * the memory placement flags to the current flags
902 ttm_flag_masked(&cur_flags, place->flags,
903 ~TTM_PL_MASK_MEMTYPE);
905 if (mem_type == TTM_PL_SYSTEM)
908 ret = (*man->func->get_node)(man, bo, place, mem);
916 if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
917 mem->mem_type = mem_type;
918 mem->placement = cur_flags;
922 for (i = 0; i < placement->num_busy_placement; ++i) {
923 const struct ttm_place *place = &placement->busy_placement[i];
925 ret = ttm_mem_type_from_place(place, &mem_type);
928 man = &bdev->man[mem_type];
929 if (!man->has_type || !man->use_type)
931 if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
935 cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
938 * Use the access and other non-mapping-related flag bits from
939 * the memory placement flags to the current flags
941 ttm_flag_masked(&cur_flags, place->flags,
942 ~TTM_PL_MASK_MEMTYPE);
944 if (mem_type == TTM_PL_SYSTEM) {
945 mem->mem_type = mem_type;
946 mem->placement = cur_flags;
951 ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
952 interruptible, no_wait_gpu);
953 if (ret == 0 && mem->mm_node) {
954 mem->placement = cur_flags;
957 if (ret == -ERESTARTSYS)
958 has_erestartsys = true;
962 printk(KERN_ERR TTM_PFX "No compatible memory type found.\n");
966 return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
968 EXPORT_SYMBOL(ttm_bo_mem_space);
970 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
971 struct ttm_placement *placement,
976 struct ttm_mem_reg mem;
978 lockdep_assert_held(&bo->resv->lock.base);
981 * FIXME: It's possible to pipeline buffer moves.
982 * Have the driver move function wait for idle when necessary,
983 * instead of doing it here.
985 ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
988 mem.num_pages = bo->num_pages;
989 mem.size = mem.num_pages << PAGE_SHIFT;
990 mem.page_alignment = bo->mem.page_alignment;
991 mem.bus.io_reserved_vm = false;
992 mem.bus.io_reserved_count = 0;
994 * Determine where to move the buffer.
996 ret = ttm_bo_mem_space(bo, placement, &mem,
997 interruptible, no_wait_gpu);
1000 ret = ttm_bo_handle_move_mem(bo, &mem, false,
1001 interruptible, no_wait_gpu);
1003 if (ret && mem.mm_node)
1004 ttm_bo_mem_put(bo, &mem);
1008 bool ttm_bo_mem_compat(struct ttm_placement *placement,
1009 struct ttm_mem_reg *mem,
1010 uint32_t *new_flags)
1014 for (i = 0; i < placement->num_placement; i++) {
1015 const struct ttm_place *heap = &placement->placement[i];
1017 (mem->start < heap->fpfn ||
1018 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1021 *new_flags = heap->flags;
1022 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1023 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1027 for (i = 0; i < placement->num_busy_placement; i++) {
1028 const struct ttm_place *heap = &placement->busy_placement[i];
1030 (mem->start < heap->fpfn ||
1031 (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1034 *new_flags = heap->flags;
1035 if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
1036 (*new_flags & mem->placement & TTM_PL_MASK_MEM))
1043 int ttm_bo_validate(struct ttm_buffer_object *bo,
1044 struct ttm_placement *placement,
1051 lockdep_assert_held(&bo->resv->lock.base);
1053 * Check whether we need to move buffer.
1055 if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1056 ret = ttm_bo_move_buffer(bo, placement, interruptible,
1062 * Use the access and other non-mapping-related flag bits from
1063 * the compatible memory placement flags to the active flags
1065 ttm_flag_masked(&bo->mem.placement, new_flags,
1066 ~TTM_PL_MASK_MEMTYPE);
1069 * We might need to add a TTM.
1071 if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
1072 ret = ttm_bo_add_ttm(bo, true);
1078 EXPORT_SYMBOL(ttm_bo_validate);
1080 int ttm_bo_init(struct ttm_bo_device *bdev,
1081 struct ttm_buffer_object *bo,
1083 enum ttm_bo_type type,
1084 struct ttm_placement *placement,
1085 uint32_t page_alignment,
1087 struct vm_object *persistent_swap_storage,
1089 struct sg_table *sg,
1090 struct reservation_object *resv,
1091 void (*destroy) (struct ttm_buffer_object *))
1094 unsigned long num_pages;
1095 struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
1098 ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
1100 pr_err("Out of kernel memory\n");
1108 num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1109 if (num_pages == 0) {
1110 pr_err("Illegal buffer object size\n");
1115 ttm_mem_global_free(mem_glob, acc_size);
1118 bo->destroy = destroy;
1120 kref_init(&bo->kref);
1121 kref_init(&bo->list_kref);
1122 atomic_set(&bo->cpu_writers, 0);
1123 INIT_LIST_HEAD(&bo->lru);
1124 INIT_LIST_HEAD(&bo->ddestroy);
1125 INIT_LIST_HEAD(&bo->swap);
1126 INIT_LIST_HEAD(&bo->io_reserve_lru);
1127 lockinit(&bo->wu_mutex, "ttmbwm", 0, LK_CANRECURSE);
1129 bo->glob = bdev->glob;
1131 bo->num_pages = num_pages;
1132 bo->mem.size = num_pages << PAGE_SHIFT;
1133 bo->mem.mem_type = TTM_PL_SYSTEM;
1134 bo->mem.num_pages = bo->num_pages;
1135 bo->mem.mm_node = NULL;
1136 bo->mem.page_alignment = page_alignment;
1137 bo->mem.bus.io_reserved_vm = false;
1138 bo->mem.bus.io_reserved_count = 0;
1140 bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
1141 bo->persistent_swap_storage = persistent_swap_storage;
1142 bo->acc_size = acc_size;
1146 lockdep_assert_held(&bo->resv->lock.base);
1148 bo->resv = &bo->ttm_resv;
1149 reservation_object_init(&bo->ttm_resv);
1151 atomic_inc(&bo->glob->bo_count);
1152 drm_vma_node_reset(&bo->vma_node);
1155 * For ttm_bo_type_device buffers, allocate
1156 * address space from the device.
1158 if (bo->type == ttm_bo_type_device ||
1159 bo->type == ttm_bo_type_sg)
1160 ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
1163 /* passed reservation objects should already be locked,
1164 * since otherwise lockdep will be angered in radeon.
1167 locked = ww_mutex_trylock(&bo->resv->lock);
1172 ret = ttm_bo_validate(bo, placement, interruptible, false);
1175 ttm_bo_unreserve(bo);
1182 EXPORT_SYMBOL(ttm_bo_init);
1184 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
1185 unsigned long bo_size,
1186 unsigned struct_size)
1188 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1191 size += ttm_round_pot(struct_size);
1192 size += PAGE_ALIGN(npages * sizeof(void *));
1193 size += ttm_round_pot(sizeof(struct ttm_tt));
1196 EXPORT_SYMBOL(ttm_bo_acc_size);
1198 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1199 unsigned long bo_size,
1200 unsigned struct_size)
1202 unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1205 size += ttm_round_pot(struct_size);
1206 size += PAGE_ALIGN(npages * sizeof(void *));
1207 size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
1208 size += ttm_round_pot(sizeof(struct ttm_dma_tt));
1211 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1213 int ttm_bo_create(struct ttm_bo_device *bdev,
1215 enum ttm_bo_type type,
1216 struct ttm_placement *placement,
1217 uint32_t page_alignment,
1219 struct vm_object *persistent_swap_storage,
1220 struct ttm_buffer_object **p_bo)
1222 struct ttm_buffer_object *bo;
1226 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
1227 if (unlikely(bo == NULL))
1230 acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
1231 ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
1232 interruptible, persistent_swap_storage, acc_size,
1234 if (likely(ret == 0))
1239 EXPORT_SYMBOL(ttm_bo_create);
1241 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
1242 unsigned mem_type, bool allow_errors)
1244 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1245 struct ttm_bo_global *glob = bdev->glob;
1249 * Can't use standard list traversal since we're unlocking.
1252 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1253 while (!list_empty(&man->lru)) {
1254 lockmgr(&glob->lru_lock, LK_RELEASE);
1255 ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
1260 pr_err("Cleanup eviction failed\n");
1263 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1265 lockmgr(&glob->lru_lock, LK_RELEASE);
1269 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1271 struct ttm_mem_type_manager *man;
1274 if (mem_type >= TTM_NUM_MEM_TYPES) {
1275 pr_err("Illegal memory type %d\n", mem_type);
1278 man = &bdev->man[mem_type];
1280 if (!man->has_type) {
1281 pr_err("Trying to take down uninitialized memory manager type %u\n",
1286 man->use_type = false;
1287 man->has_type = false;
1291 ttm_bo_force_list_clean(bdev, mem_type, false);
1293 ret = (*man->func->takedown)(man);
1298 EXPORT_SYMBOL(ttm_bo_clean_mm);
1300 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
1302 struct ttm_mem_type_manager *man = &bdev->man[mem_type];
1304 if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
1305 pr_err("Illegal memory manager memory type %u\n", mem_type);
1309 if (!man->has_type) {
1310 pr_err("Memory type %u has not been initialized\n", mem_type);
1314 return ttm_bo_force_list_clean(bdev, mem_type, true);
1316 EXPORT_SYMBOL(ttm_bo_evict_mm);
1318 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
1319 unsigned long p_size)
1322 struct ttm_mem_type_manager *man;
1324 BUG_ON(type >= TTM_NUM_MEM_TYPES);
1325 man = &bdev->man[type];
1326 BUG_ON(man->has_type);
1327 man->io_reserve_fastpath = true;
1328 man->use_io_reserve_lru = false;
1329 lockinit(&man->io_reserve_mutex, "ttmior", 0, 0);
1330 INIT_LIST_HEAD(&man->io_reserve_lru);
1332 ret = bdev->driver->init_mem_type(bdev, type, man);
1338 if (type != TTM_PL_SYSTEM) {
1339 ret = (*man->func->init)(man, p_size);
1343 man->has_type = true;
1344 man->use_type = true;
1347 INIT_LIST_HEAD(&man->lru);
1351 EXPORT_SYMBOL(ttm_bo_init_mm);
1353 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1355 struct ttm_bo_global *glob =
1356 container_of(kobj, struct ttm_bo_global, kobj);
1358 ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
1359 __free_page(glob->dummy_read_page);
1363 void ttm_bo_global_release(struct drm_global_reference *ref)
1365 struct ttm_bo_global *glob = ref->object;
1367 kobject_del(&glob->kobj);
1368 kobject_put(&glob->kobj);
1370 EXPORT_SYMBOL(ttm_bo_global_release);
1372 int ttm_bo_global_init(struct drm_global_reference *ref)
1374 struct ttm_bo_global_ref *bo_ref =
1375 container_of(ref, struct ttm_bo_global_ref, ref);
1376 struct ttm_bo_global *glob = ref->object;
1379 lockinit(&glob->device_list_mutex, "ttmdlm", 0, 0);
1380 lockinit(&glob->lru_lock, "ttmlru", 0, 0);
1381 glob->mem_glob = bo_ref->mem_glob;
1382 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1384 if (unlikely(glob->dummy_read_page == NULL)) {
1389 INIT_LIST_HEAD(&glob->swap_lru);
1390 INIT_LIST_HEAD(&glob->device_list);
1392 ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
1393 ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
1394 if (unlikely(ret != 0)) {
1395 pr_err("Could not register buffer object swapout\n");
1399 atomic_set(&glob->bo_count, 0);
1401 ret = kobject_init_and_add(
1402 &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1403 if (unlikely(ret != 0))
1404 kobject_put(&glob->kobj);
1407 __free_page(glob->dummy_read_page);
1412 EXPORT_SYMBOL(ttm_bo_global_init);
1415 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1418 unsigned i = TTM_NUM_MEM_TYPES;
1419 struct ttm_mem_type_manager *man;
1420 struct ttm_bo_global *glob = bdev->glob;
1423 man = &bdev->man[i];
1424 if (man->has_type) {
1425 man->use_type = false;
1426 if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
1428 pr_err("DRM memory manager type %d is not clean\n",
1431 man->has_type = false;
1435 mutex_lock(&glob->device_list_mutex);
1436 list_del(&bdev->device_list);
1437 mutex_unlock(&glob->device_list_mutex);
1439 cancel_delayed_work_sync(&bdev->wq);
1441 while (ttm_bo_delayed_delete(bdev, true))
1444 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1445 if (list_empty(&bdev->ddestroy))
1446 TTM_DEBUG("Delayed destroy list was clean\n");
1448 if (list_empty(&bdev->man[0].lru))
1449 TTM_DEBUG("Swap list was clean\n");
1450 lockmgr(&glob->lru_lock, LK_RELEASE);
1452 drm_vma_offset_manager_destroy(&bdev->vma_manager);
1456 EXPORT_SYMBOL(ttm_bo_device_release);
1458 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1459 struct ttm_bo_global *glob,
1460 struct ttm_bo_driver *driver,
1461 struct address_space *mapping,
1462 uint64_t file_page_offset,
1467 bdev->driver = driver;
1469 memset(bdev->man, 0, sizeof(bdev->man));
1472 * Initialize the system memory buffer type.
1473 * Other types need to be driver / IOCTL initialized.
1475 ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
1476 if (unlikely(ret != 0))
1479 drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
1481 INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1482 INIT_LIST_HEAD(&bdev->ddestroy);
1484 * XXX DRAGONFLY - dev_mapping NULL atm, find other XXX DRAGONFLY
1485 * lines and fix when it no longer is in later API change.
1487 bdev->dev_mapping = mapping;
1489 bdev->need_dma32 = need_dma32;
1491 mutex_lock(&glob->device_list_mutex);
1492 list_add_tail(&bdev->device_list, &glob->device_list);
1493 mutex_unlock(&glob->device_list_mutex);
1499 EXPORT_SYMBOL(ttm_bo_device_init);
1502 * buffer object vm functions.
1505 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1507 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1509 if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
1510 if (mem->mem_type == TTM_PL_SYSTEM)
1513 if (man->flags & TTM_MEMTYPE_FLAG_CMA)
1516 if (mem->placement & TTM_PL_FLAG_CACHED)
1522 #ifdef __DragonFly__
1525 * XXX DRAGONFLY - device_mapping not yet implemented so
1526 * file_mapping is basically always NULL. We have to properly
1527 * release the mmap, etc.
1529 void ttm_bo_release_mmap(struct ttm_buffer_object *bo);
1532 * drm_vma_node_unmap() - Unmap offset node
1533 * @node: Offset node
1534 * @file_mapping: Address space to unmap @node from
1536 * Unmap all userspace mappings for a given offset node. The mappings must be
1537 * associated with the @file_mapping address-space. If no offset exists or
1538 * the address-space is invalid, nothing is done.
1540 * This call is unlocked. The caller must guarantee that drm_vma_offset_remove()
1541 * is not called on this node concurrently.
1543 static inline void drm_vma_node_unmap(struct drm_vma_offset_node *node,
1544 struct address_space *file_mapping)
1546 struct ttm_buffer_object *bo = container_of(node, struct ttm_buffer_object, vma_node);
1548 if (drm_vma_node_has_offset(node))
1549 unmap_mapping_range(file_mapping,
1550 drm_vma_node_offset_addr(node),
1551 drm_vma_node_size(node) << PAGE_SHIFT, 1);
1552 ttm_bo_release_mmap(bo);
1556 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
1558 struct ttm_bo_device *bdev = bo->bdev;
1560 drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
1561 ttm_mem_io_free_vm(bo);
1564 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1566 struct ttm_bo_device *bdev = bo->bdev;
1567 struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
1569 ttm_mem_io_lock(man, false);
1570 ttm_bo_unmap_virtual_locked(bo);
1571 ttm_mem_io_unlock(man);
1575 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1577 int ttm_bo_wait(struct ttm_buffer_object *bo,
1578 bool lazy, bool interruptible, bool no_wait)
1580 struct reservation_object_list *fobj;
1581 struct reservation_object *resv;
1583 long timeout = 15 * HZ;
1587 fobj = reservation_object_get_list(resv);
1588 excl = reservation_object_get_excl(resv);
1590 if (!fence_is_signaled(excl)) {
1594 timeout = fence_wait_timeout(excl,
1595 interruptible, timeout);
1599 for (i = 0; fobj && timeout > 0 && i < fobj->shared_count; ++i) {
1600 struct fence *fence;
1601 fence = rcu_dereference_protected(fobj->shared[i],
1602 reservation_object_held(resv));
1604 if (!fence_is_signaled(fence)) {
1608 timeout = fence_wait_timeout(fence,
1609 interruptible, timeout);
1619 reservation_object_add_excl_fence(resv, NULL);
1620 clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
1623 EXPORT_SYMBOL(ttm_bo_wait);
1625 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
1630 * Using ttm_bo_reserve makes sure the lru lists are updated.
1633 ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
1634 if (unlikely(ret != 0))
1636 ret = ttm_bo_wait(bo, false, true, no_wait);
1637 if (likely(ret == 0))
1638 atomic_inc(&bo->cpu_writers);
1639 ttm_bo_unreserve(bo);
1642 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
1644 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
1646 atomic_dec(&bo->cpu_writers);
1648 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
1651 * A buffer object shrink method that tries to swap out the first
1652 * buffer object on the bo_global::swap_lru list.
1655 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
1657 struct ttm_bo_global *glob =
1658 container_of(shrink, struct ttm_bo_global, shrink);
1659 struct ttm_buffer_object *bo;
1662 uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
1664 lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
1665 list_for_each_entry(bo, &glob->swap_lru, swap) {
1666 ret = __ttm_bo_reserve(bo, false, true, false, 0);
1672 lockmgr(&glob->lru_lock, LK_RELEASE);
1676 kref_get(&bo->list_kref);
1678 if (!list_empty(&bo->ddestroy)) {
1679 ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
1680 kref_put(&bo->list_kref, ttm_bo_release_list);
1684 put_count = ttm_bo_del_from_lru(bo);
1685 lockmgr(&glob->lru_lock, LK_RELEASE);
1687 ttm_bo_list_ref_sub(bo, put_count, true);
1690 * Wait for GPU, then move to system cached.
1693 ret = ttm_bo_wait(bo, false, false, false);
1695 if (unlikely(ret != 0))
1698 if ((bo->mem.placement & swap_placement) != swap_placement) {
1699 struct ttm_mem_reg evict_mem;
1701 evict_mem = bo->mem;
1702 evict_mem.mm_node = NULL;
1703 evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
1704 evict_mem.mem_type = TTM_PL_SYSTEM;
1706 ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
1708 if (unlikely(ret != 0))
1712 ttm_bo_unmap_virtual(bo);
1715 * Swap out. Buffer will be swapped in again as soon as
1716 * anyone tries to access a ttm page.
1719 if (bo->bdev->driver->swap_notify)
1720 bo->bdev->driver->swap_notify(bo);
1722 ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
1727 * Unreserve without putting on LRU to avoid swapping out an
1728 * already swapped buffer.
1731 __ttm_bo_unreserve(bo);
1732 kref_put(&bo->list_kref, ttm_bo_release_list);
1736 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
1738 while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
1741 EXPORT_SYMBOL(ttm_bo_swapout_all);
1744 * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
1747 * @bo: Pointer to buffer
1749 int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
1754 * In the absense of a wait_unlocked API,
1755 * Use the bo::wu_mutex to avoid triggering livelocks due to
1756 * concurrent use of this function. Note that this use of
1757 * bo::wu_mutex can go away if we change locking order to
1758 * mmap_sem -> bo::reserve.
1760 ret = mutex_lock_interruptible(&bo->wu_mutex);
1761 if (unlikely(ret != 0))
1762 return -ERESTARTSYS;
1763 if (!ww_mutex_is_locked(&bo->resv->lock))
1765 ret = __ttm_bo_reserve(bo, true, false, false, NULL);
1766 if (unlikely(ret != 0))
1768 __ttm_bo_unreserve(bo);
1771 mutex_unlock(&bo->wu_mutex);