Home | History | Annotate | Line # | Download | only in ttm
ttm_bo.c revision 1.7
      1 /**************************************************************************
      2  *
      3  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
      4  * All Rights Reserved.
      5  *
      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:
     13  *
     14  * The above copyright notice and this permission notice (including the
     15  * next paragraph) shall be included in all copies or substantial portions
     16  * of the Software.
     17  *
     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.
     25  *
     26  **************************************************************************/
     27 /*
     28  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
     29  */
     30 
     31 #define pr_fmt(fmt) "[TTM] " fmt
     32 
     33 #ifdef __NetBSD__
     34 #include <sys/types.h>
     35 #include <uvm/uvm_extern.h>
     36 #include <uvm/uvm_object.h>
     37 #endif
     38 
     39 #include <drm/ttm/ttm_module.h>
     40 #include <drm/ttm/ttm_bo_driver.h>
     41 #include <drm/ttm/ttm_placement.h>
     42 #include <linux/jiffies.h>
     43 #include <linux/slab.h>
     44 #include <linux/sched.h>
     45 #include <linux/mm.h>
     46 #include <linux/file.h>
     47 #include <linux/module.h>
     48 #include <linux/atomic.h>
     49 #include <linux/printk.h>
     50 #include <linux/export.h>
     51 
     52 #define TTM_ASSERT_LOCKED(param)
     53 #define TTM_DEBUG(fmt, arg...)	do {} while (0)
     54 #define TTM_BO_HASH_ORDER 13
     55 
     56 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
     57 #ifndef __NetBSD__
     58 static void ttm_bo_global_kobj_release(struct kobject *kobj);
     59 #endif
     60 
     61 #ifndef __NetBSD__		/* XXX sysfs */
     62 static struct attribute ttm_bo_count = {
     63 	.name = "bo_count",
     64 	.mode = S_IRUGO
     65 };
     66 #endif
     67 
     68 static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
     69 {
     70 	int i;
     71 
     72 	for (i = 0; i <= TTM_PL_PRIV5; i++)
     73 		if (flags & (1 << i)) {
     74 			*mem_type = i;
     75 			return 0;
     76 		}
     77 	return -EINVAL;
     78 }
     79 
     80 static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
     81 {
     82 	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
     83 
     84 	pr_err("    has_type: %d\n", man->has_type);
     85 	pr_err("    use_type: %d\n", man->use_type);
     86 	pr_err("    flags: 0x%08X\n", man->flags);
     87 	pr_err("    gpu_offset: 0x%08lX\n", man->gpu_offset);
     88 	pr_err("    size: %"PRIu64"\n", man->size);
     89 	pr_err("    available_caching: 0x%08X\n", man->available_caching);
     90 	pr_err("    default_caching: 0x%08X\n", man->default_caching);
     91 	if (mem_type != TTM_PL_SYSTEM)
     92 		(*man->func->debug)(man, TTM_PFX);
     93 }
     94 
     95 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
     96 					struct ttm_placement *placement)
     97 {
     98 	int i, ret, mem_type;
     99 
    100 	pr_err("No space for %p (%lu pages, %luK, %luM)\n",
    101 	       bo, bo->mem.num_pages, bo->mem.size >> 10,
    102 	       bo->mem.size >> 20);
    103 	for (i = 0; i < placement->num_placement; i++) {
    104 		ret = ttm_mem_type_from_flags(placement->placement[i],
    105 						&mem_type);
    106 		if (ret)
    107 			return;
    108 		pr_err("  placement[%d]=0x%08X (%d)\n",
    109 		       i, placement->placement[i], mem_type);
    110 		ttm_mem_type_debug(bo->bdev, mem_type);
    111 	}
    112 }
    113 
    114 #ifndef __NetBSD__		/* XXX sysfs */
    115 static ssize_t ttm_bo_global_show(struct kobject *kobj,
    116 				  struct attribute *attr,
    117 				  char *buffer)
    118 {
    119 	struct ttm_bo_global *glob =
    120 		container_of(kobj, struct ttm_bo_global, kobj);
    121 
    122 	return snprintf(buffer, PAGE_SIZE, "%lu\n",
    123 			(unsigned long) atomic_read(&glob->bo_count));
    124 }
    125 
    126 static struct attribute *ttm_bo_global_attrs[] = {
    127 	&ttm_bo_count,
    128 	NULL
    129 };
    130 
    131 static const struct sysfs_ops ttm_bo_global_ops = {
    132 	.show = &ttm_bo_global_show
    133 };
    134 
    135 static struct kobj_type ttm_bo_glob_kobj_type  = {
    136 	.release = &ttm_bo_global_kobj_release,
    137 	.sysfs_ops = &ttm_bo_global_ops,
    138 	.default_attrs = ttm_bo_global_attrs
    139 };
    140 #endif	/* __NetBSD__ */
    141 
    142 
    143 static inline uint32_t ttm_bo_type_flags(unsigned type)
    144 {
    145 	return 1 << (type);
    146 }
    147 
    148 static void ttm_bo_release_list(struct kref *list_kref)
    149 {
    150 	struct ttm_buffer_object *bo =
    151 	    container_of(list_kref, struct ttm_buffer_object, list_kref);
    152 	struct ttm_bo_device *bdev = bo->bdev;
    153 	size_t acc_size = bo->acc_size;
    154 
    155 	BUG_ON(kref_referenced_p(&bo->list_kref));
    156 	BUG_ON(kref_referenced_p(&bo->kref));
    157 	BUG_ON(atomic_read(&bo->cpu_writers));
    158 	BUG_ON(bo->sync_obj != NULL);
    159 	BUG_ON(bo->mem.mm_node != NULL);
    160 	BUG_ON(!list_empty(&bo->lru));
    161 	BUG_ON(!list_empty(&bo->ddestroy));
    162 
    163 	if (bo->ttm)
    164 		ttm_tt_destroy(bo->ttm);
    165 	atomic_dec(&bo->glob->bo_count);
    166 	if (bo->resv == &bo->ttm_resv)
    167 		reservation_object_fini(&bo->ttm_resv);
    168 #ifdef __NetBSD__
    169 	linux_mutex_destroy(&bo->wu_mutex);
    170 #else
    171 	mutex_destroy(&bo->wu_mutex);
    172 #endif
    173 	if (bo->destroy)
    174 		bo->destroy(bo);
    175 	else {
    176 		kfree(bo);
    177 	}
    178 	ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
    179 }
    180 
    181 void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
    182 {
    183 	struct ttm_bo_device *bdev = bo->bdev;
    184 	struct ttm_mem_type_manager *man;
    185 
    186 	lockdep_assert_held(&bo->resv->lock.base);
    187 
    188 	if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
    189 
    190 		BUG_ON(!list_empty(&bo->lru));
    191 
    192 		man = &bdev->man[bo->mem.mem_type];
    193 		list_add_tail(&bo->lru, &man->lru);
    194 		kref_get(&bo->list_kref);
    195 
    196 		if (bo->ttm != NULL) {
    197 			list_add_tail(&bo->swap, &bo->glob->swap_lru);
    198 			kref_get(&bo->list_kref);
    199 		}
    200 	}
    201 }
    202 EXPORT_SYMBOL(ttm_bo_add_to_lru);
    203 
    204 int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
    205 {
    206 	int put_count = 0;
    207 
    208 	if (!list_empty(&bo->swap)) {
    209 		list_del_init(&bo->swap);
    210 		++put_count;
    211 	}
    212 	if (!list_empty(&bo->lru)) {
    213 		list_del_init(&bo->lru);
    214 		++put_count;
    215 	}
    216 
    217 	/*
    218 	 * TODO: Add a driver hook to delete from
    219 	 * driver-specific LRU's here.
    220 	 */
    221 
    222 	return put_count;
    223 }
    224 
    225 static void ttm_bo_ref_bug(struct kref *list_kref)
    226 {
    227 	BUG();
    228 }
    229 
    230 void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
    231 			 bool never_free)
    232 {
    233 	kref_sub(&bo->list_kref, count,
    234 		 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
    235 }
    236 
    237 void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
    238 {
    239 	int put_count;
    240 
    241 	spin_lock(&bo->glob->lru_lock);
    242 	put_count = ttm_bo_del_from_lru(bo);
    243 	spin_unlock(&bo->glob->lru_lock);
    244 	ttm_bo_list_ref_sub(bo, put_count, true);
    245 }
    246 EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
    247 
    248 /*
    249  * Call bo->mutex locked.
    250  */
    251 static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
    252 {
    253 	struct ttm_bo_device *bdev = bo->bdev;
    254 	struct ttm_bo_global *glob = bo->glob;
    255 	int ret = 0;
    256 	uint32_t page_flags = 0;
    257 
    258 	TTM_ASSERT_LOCKED(&bo->mutex);
    259 	bo->ttm = NULL;
    260 
    261 	if (bdev->need_dma32)
    262 		page_flags |= TTM_PAGE_FLAG_DMA32;
    263 
    264 	switch (bo->type) {
    265 	case ttm_bo_type_device:
    266 		if (zero_alloc)
    267 			page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
    268 	case ttm_bo_type_kernel:
    269 		bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
    270 						      page_flags, glob->dummy_read_page);
    271 		if (unlikely(bo->ttm == NULL))
    272 			ret = -ENOMEM;
    273 		break;
    274 	case ttm_bo_type_sg:
    275 		bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
    276 						      page_flags | TTM_PAGE_FLAG_SG,
    277 						      glob->dummy_read_page);
    278 		if (unlikely(bo->ttm == NULL)) {
    279 			ret = -ENOMEM;
    280 			break;
    281 		}
    282 		bo->ttm->sg = bo->sg;
    283 		break;
    284 	default:
    285 		pr_err("Illegal buffer object type\n");
    286 		ret = -EINVAL;
    287 		break;
    288 	}
    289 
    290 #ifdef __NetBSD__
    291 	if (ret)
    292 		return ret;
    293 
    294 	/*
    295 	 * XXX This is gross.  We ought to do it the other way around:
    296 	 * set the uao to have the main uvm object's lock.  However,
    297 	 * uvm_obj_setlock is not safe on uvm_aobjs.
    298 	 */
    299 	mutex_obj_hold(bo->ttm->swap_storage->vmobjlock);
    300 	uvm_obj_setlock(&bo->uvmobj, bo->ttm->swap_storage->vmobjlock);
    301 	return 0;
    302 #else
    303 	return ret;
    304 #endif
    305 }
    306 
    307 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
    308 				  struct ttm_mem_reg *mem,
    309 				  bool evict, bool interruptible,
    310 				  bool no_wait_gpu)
    311 {
    312 	struct ttm_bo_device *bdev = bo->bdev;
    313 	bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
    314 	bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
    315 	struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
    316 	struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
    317 	int ret = 0;
    318 
    319 	if (old_is_pci || new_is_pci ||
    320 	    ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
    321 		ret = ttm_mem_io_lock(old_man, true);
    322 		if (unlikely(ret != 0))
    323 			goto out_err;
    324 		ttm_bo_unmap_virtual_locked(bo);
    325 		ttm_mem_io_unlock(old_man);
    326 	}
    327 
    328 	/*
    329 	 * Create and bind a ttm if required.
    330 	 */
    331 
    332 	if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
    333 		if (bo->ttm == NULL) {
    334 			bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
    335 			ret = ttm_bo_add_ttm(bo, zero);
    336 			if (ret)
    337 				goto out_err;
    338 		}
    339 
    340 		ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
    341 		if (ret)
    342 			goto out_err;
    343 
    344 		if (mem->mem_type != TTM_PL_SYSTEM) {
    345 			ret = ttm_tt_bind(bo->ttm, mem);
    346 			if (ret)
    347 				goto out_err;
    348 		}
    349 
    350 		if (bo->mem.mem_type == TTM_PL_SYSTEM) {
    351 			if (bdev->driver->move_notify)
    352 				bdev->driver->move_notify(bo, mem);
    353 			bo->mem = *mem;
    354 			mem->mm_node = NULL;
    355 			goto moved;
    356 		}
    357 	}
    358 
    359 	if (bdev->driver->move_notify)
    360 		bdev->driver->move_notify(bo, mem);
    361 
    362 	if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
    363 	    !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
    364 		ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
    365 	else if (bdev->driver->move)
    366 		ret = bdev->driver->move(bo, evict, interruptible,
    367 					 no_wait_gpu, mem);
    368 	else
    369 		ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
    370 
    371 	if (ret) {
    372 		if (bdev->driver->move_notify) {
    373 			struct ttm_mem_reg tmp_mem = *mem;
    374 			*mem = bo->mem;
    375 			bo->mem = tmp_mem;
    376 			bdev->driver->move_notify(bo, mem);
    377 			bo->mem = *mem;
    378 			*mem = tmp_mem;
    379 		}
    380 
    381 		goto out_err;
    382 	}
    383 
    384 moved:
    385 	if (bo->evicted) {
    386 		if (bdev->driver->invalidate_caches) {
    387 			ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
    388 			if (ret)
    389 				pr_err("Can not flush read caches\n");
    390 		}
    391 		bo->evicted = false;
    392 	}
    393 
    394 	if (bo->mem.mm_node) {
    395 		bo->offset = (bo->mem.start << PAGE_SHIFT) +
    396 		    bdev->man[bo->mem.mem_type].gpu_offset;
    397 		bo->cur_placement = bo->mem.placement;
    398 	} else
    399 		bo->offset = 0;
    400 
    401 	return 0;
    402 
    403 out_err:
    404 	new_man = &bdev->man[bo->mem.mem_type];
    405 	if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
    406 		ttm_tt_unbind(bo->ttm);
    407 		ttm_tt_destroy(bo->ttm);
    408 		bo->ttm = NULL;
    409 	}
    410 
    411 	return ret;
    412 }
    413 
    414 /**
    415  * Call bo::reserved.
    416  * Will release GPU memory type usage on destruction.
    417  * This is the place to put in driver specific hooks to release
    418  * driver private resources.
    419  * Will release the bo::reserved lock.
    420  */
    421 
    422 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
    423 {
    424 	if (bo->bdev->driver->move_notify)
    425 		bo->bdev->driver->move_notify(bo, NULL);
    426 
    427 	if (bo->ttm) {
    428 		ttm_tt_unbind(bo->ttm);
    429 		ttm_tt_destroy(bo->ttm);
    430 		bo->ttm = NULL;
    431 	}
    432 	ttm_bo_mem_put(bo, &bo->mem);
    433 
    434 	ww_mutex_unlock (&bo->resv->lock);
    435 }
    436 
    437 static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
    438 {
    439 	struct ttm_bo_device *bdev = bo->bdev;
    440 	struct ttm_bo_global *glob = bo->glob;
    441 	struct ttm_bo_driver *driver = bdev->driver;
    442 	void *sync_obj = NULL;
    443 	int put_count;
    444 	int ret;
    445 
    446 	spin_lock(&glob->lru_lock);
    447 	ret = __ttm_bo_reserve(bo, false, true, false, 0);
    448 
    449 	spin_lock(&bdev->fence_lock);
    450 	(void) ttm_bo_wait(bo, false, false, true);
    451 	if (!ret && !bo->sync_obj) {
    452 		spin_unlock(&bdev->fence_lock);
    453 		put_count = ttm_bo_del_from_lru(bo);
    454 
    455 		spin_unlock(&glob->lru_lock);
    456 		ttm_bo_cleanup_memtype_use(bo);
    457 
    458 		ttm_bo_list_ref_sub(bo, put_count, true);
    459 
    460 		return;
    461 	}
    462 	if (bo->sync_obj)
    463 		sync_obj = driver->sync_obj_ref(bo->sync_obj);
    464 	spin_unlock(&bdev->fence_lock);
    465 
    466 	if (!ret) {
    467 
    468 		/*
    469 		 * Make NO_EVICT bos immediately available to
    470 		 * shrinkers, now that they are queued for
    471 		 * destruction.
    472 		 */
    473 		if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
    474 			bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
    475 			ttm_bo_add_to_lru(bo);
    476 		}
    477 
    478 		__ttm_bo_unreserve(bo);
    479 	}
    480 
    481 	kref_get(&bo->list_kref);
    482 	list_add_tail(&bo->ddestroy, &bdev->ddestroy);
    483 	spin_unlock(&glob->lru_lock);
    484 
    485 	if (sync_obj) {
    486 		driver->sync_obj_flush(sync_obj);
    487 		driver->sync_obj_unref(&sync_obj);
    488 	}
    489 	schedule_delayed_work(&bdev->wq,
    490 			      ((DRM_HZ / 100) < 1) ? 1 : DRM_HZ / 100);
    491 }
    492 
    493 /**
    494  * function ttm_bo_cleanup_refs_and_unlock
    495  * If bo idle, remove from delayed- and lru lists, and unref.
    496  * If not idle, do nothing.
    497  *
    498  * Must be called with lru_lock and reservation held, this function
    499  * will drop both before returning.
    500  *
    501  * @interruptible         Any sleeps should occur interruptibly.
    502  * @no_wait_gpu           Never wait for gpu. Return -EBUSY instead.
    503  */
    504 
    505 static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
    506 					  bool interruptible,
    507 					  bool no_wait_gpu)
    508 {
    509 	struct ttm_bo_device *bdev = bo->bdev;
    510 	struct ttm_bo_driver *driver = bdev->driver;
    511 	struct ttm_bo_global *glob = bo->glob;
    512 	int put_count;
    513 	int ret;
    514 
    515 	spin_lock(&bdev->fence_lock);
    516 	ret = ttm_bo_wait(bo, false, false, true);
    517 
    518 	if (ret && !no_wait_gpu) {
    519 		void *sync_obj;
    520 
    521 		/*
    522 		 * Take a reference to the fence and unreserve,
    523 		 * at this point the buffer should be dead, so
    524 		 * no new sync objects can be attached.
    525 		 */
    526 		sync_obj = driver->sync_obj_ref(bo->sync_obj);
    527 		spin_unlock(&bdev->fence_lock);
    528 
    529 		__ttm_bo_unreserve(bo);
    530 		spin_unlock(&glob->lru_lock);
    531 
    532 		ret = driver->sync_obj_wait(sync_obj, false, interruptible);
    533 		driver->sync_obj_unref(&sync_obj);
    534 		if (ret)
    535 			return ret;
    536 
    537 		/*
    538 		 * remove sync_obj with ttm_bo_wait, the wait should be
    539 		 * finished, and no new wait object should have been added.
    540 		 */
    541 		spin_lock(&bdev->fence_lock);
    542 		ret = ttm_bo_wait(bo, false, false, true);
    543 		WARN_ON(ret);
    544 		spin_unlock(&bdev->fence_lock);
    545 		if (ret)
    546 			return ret;
    547 
    548 		spin_lock(&glob->lru_lock);
    549 		ret = __ttm_bo_reserve(bo, false, true, false, 0);
    550 
    551 		/*
    552 		 * We raced, and lost, someone else holds the reservation now,
    553 		 * and is probably busy in ttm_bo_cleanup_memtype_use.
    554 		 *
    555 		 * Even if it's not the case, because we finished waiting any
    556 		 * delayed destruction would succeed, so just return success
    557 		 * here.
    558 		 */
    559 		if (ret) {
    560 			spin_unlock(&glob->lru_lock);
    561 			return 0;
    562 		}
    563 	} else
    564 		spin_unlock(&bdev->fence_lock);
    565 
    566 	if (ret || unlikely(list_empty(&bo->ddestroy))) {
    567 		__ttm_bo_unreserve(bo);
    568 		spin_unlock(&glob->lru_lock);
    569 		return ret;
    570 	}
    571 
    572 	put_count = ttm_bo_del_from_lru(bo);
    573 	list_del_init(&bo->ddestroy);
    574 	++put_count;
    575 
    576 	spin_unlock(&glob->lru_lock);
    577 	ttm_bo_cleanup_memtype_use(bo);
    578 
    579 	ttm_bo_list_ref_sub(bo, put_count, true);
    580 
    581 	return 0;
    582 }
    583 
    584 /**
    585  * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
    586  * encountered buffers.
    587  */
    588 
    589 static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
    590 {
    591 	struct ttm_bo_global *glob = bdev->glob;
    592 	struct ttm_buffer_object *entry = NULL;
    593 	int ret = 0;
    594 
    595 	spin_lock(&glob->lru_lock);
    596 	if (list_empty(&bdev->ddestroy))
    597 		goto out_unlock;
    598 
    599 	entry = list_first_entry(&bdev->ddestroy,
    600 		struct ttm_buffer_object, ddestroy);
    601 	kref_get(&entry->list_kref);
    602 
    603 	for (;;) {
    604 		struct ttm_buffer_object *nentry = NULL;
    605 
    606 		if (entry->ddestroy.next != &bdev->ddestroy) {
    607 			nentry = list_first_entry(&entry->ddestroy,
    608 				struct ttm_buffer_object, ddestroy);
    609 			kref_get(&nentry->list_kref);
    610 		}
    611 
    612 		ret = __ttm_bo_reserve(entry, false, true, false, 0);
    613 		if (remove_all && ret) {
    614 			spin_unlock(&glob->lru_lock);
    615 			ret = __ttm_bo_reserve(entry, false, false,
    616 					       false, 0);
    617 			spin_lock(&glob->lru_lock);
    618 		}
    619 
    620 		if (!ret)
    621 			ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
    622 							     !remove_all);
    623 		else
    624 			spin_unlock(&glob->lru_lock);
    625 
    626 		kref_put(&entry->list_kref, ttm_bo_release_list);
    627 		entry = nentry;
    628 
    629 		if (ret || !entry)
    630 			goto out;
    631 
    632 		spin_lock(&glob->lru_lock);
    633 		if (list_empty(&entry->ddestroy))
    634 			break;
    635 	}
    636 
    637 out_unlock:
    638 	spin_unlock(&glob->lru_lock);
    639 out:
    640 	if (entry)
    641 		kref_put(&entry->list_kref, ttm_bo_release_list);
    642 	return ret;
    643 }
    644 
    645 static void ttm_bo_delayed_workqueue(struct work_struct *work)
    646 {
    647 	struct ttm_bo_device *bdev =
    648 	    container_of(work, struct ttm_bo_device, wq.work);
    649 
    650 	if (ttm_bo_delayed_delete(bdev, false)) {
    651 		schedule_delayed_work(&bdev->wq,
    652 				      ((DRM_HZ / 100) < 1) ? 1 : DRM_HZ / 100);
    653 	}
    654 }
    655 
    656 static void ttm_bo_release(struct kref *kref)
    657 {
    658 	struct ttm_buffer_object *bo =
    659 	    container_of(kref, struct ttm_buffer_object, kref);
    660 	struct ttm_bo_device *bdev = bo->bdev;
    661 	struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
    662 
    663 #ifdef __NetBSD__
    664 	uvm_obj_destroy(&bo->uvmobj, true);
    665 #endif
    666 	drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
    667 #ifdef __NetBSD__
    668 	drm_vma_node_destroy(&bo->vma_node);
    669 #endif
    670 	ttm_mem_io_lock(man, false);
    671 	ttm_mem_io_free_vm(bo);
    672 	ttm_mem_io_unlock(man);
    673 	ttm_bo_cleanup_refs_or_queue(bo);
    674 	kref_put(&bo->list_kref, ttm_bo_release_list);
    675 }
    676 
    677 void ttm_bo_unref(struct ttm_buffer_object **p_bo)
    678 {
    679 	struct ttm_buffer_object *bo = *p_bo;
    680 
    681 	*p_bo = NULL;
    682 	kref_put(&bo->kref, ttm_bo_release);
    683 }
    684 EXPORT_SYMBOL(ttm_bo_unref);
    685 
    686 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
    687 {
    688 	return cancel_delayed_work_sync(&bdev->wq);
    689 }
    690 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
    691 
    692 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
    693 {
    694 	if (resched)
    695 		schedule_delayed_work(&bdev->wq,
    696 				      ((DRM_HZ / 100) < 1) ? 1 : DRM_HZ / 100);
    697 }
    698 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
    699 
    700 static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
    701 			bool no_wait_gpu)
    702 {
    703 	struct ttm_bo_device *bdev = bo->bdev;
    704 	struct ttm_mem_reg evict_mem;
    705 	struct ttm_placement placement;
    706 	int ret = 0;
    707 
    708 	spin_lock(&bdev->fence_lock);
    709 	ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
    710 	spin_unlock(&bdev->fence_lock);
    711 
    712 	if (unlikely(ret != 0)) {
    713 		if (ret != -ERESTARTSYS) {
    714 			pr_err("Failed to expire sync object before buffer eviction\n");
    715 		}
    716 		goto out;
    717 	}
    718 
    719 	lockdep_assert_held(&bo->resv->lock.base);
    720 
    721 	evict_mem = bo->mem;
    722 	evict_mem.mm_node = NULL;
    723 	evict_mem.bus.io_reserved_vm = false;
    724 	evict_mem.bus.io_reserved_count = 0;
    725 
    726 	placement.fpfn = 0;
    727 	placement.lpfn = 0;
    728 	placement.num_placement = 0;
    729 	placement.num_busy_placement = 0;
    730 	bdev->driver->evict_flags(bo, &placement);
    731 	ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
    732 				no_wait_gpu);
    733 	if (ret) {
    734 		if (ret != -ERESTARTSYS) {
    735 			pr_err("Failed to find memory space for buffer 0x%p eviction\n",
    736 			       bo);
    737 			ttm_bo_mem_space_debug(bo, &placement);
    738 		}
    739 		goto out;
    740 	}
    741 
    742 	ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
    743 				     no_wait_gpu);
    744 	if (ret) {
    745 		if (ret != -ERESTARTSYS)
    746 			pr_err("Buffer eviction failed\n");
    747 		ttm_bo_mem_put(bo, &evict_mem);
    748 		goto out;
    749 	}
    750 	bo->evicted = true;
    751 out:
    752 	return ret;
    753 }
    754 
    755 static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
    756 				uint32_t mem_type,
    757 				bool interruptible,
    758 				bool no_wait_gpu)
    759 {
    760 	struct ttm_bo_global *glob = bdev->glob;
    761 	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
    762 	struct ttm_buffer_object *bo;
    763 	int ret = -EBUSY, put_count;
    764 
    765 	spin_lock(&glob->lru_lock);
    766 	list_for_each_entry(bo, &man->lru, lru) {
    767 		ret = __ttm_bo_reserve(bo, false, true, false, 0);
    768 		if (!ret)
    769 			break;
    770 	}
    771 
    772 	if (ret) {
    773 		spin_unlock(&glob->lru_lock);
    774 		return ret;
    775 	}
    776 
    777 	kref_get(&bo->list_kref);
    778 
    779 	if (!list_empty(&bo->ddestroy)) {
    780 		ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
    781 						     no_wait_gpu);
    782 		kref_put(&bo->list_kref, ttm_bo_release_list);
    783 		return ret;
    784 	}
    785 
    786 	put_count = ttm_bo_del_from_lru(bo);
    787 	spin_unlock(&glob->lru_lock);
    788 
    789 	BUG_ON(ret != 0);
    790 
    791 	ttm_bo_list_ref_sub(bo, put_count, true);
    792 
    793 	ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
    794 	ttm_bo_unreserve(bo);
    795 
    796 	kref_put(&bo->list_kref, ttm_bo_release_list);
    797 	return ret;
    798 }
    799 
    800 void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
    801 {
    802 	struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
    803 
    804 	if (mem->mm_node)
    805 		(*man->func->put_node)(man, mem);
    806 }
    807 EXPORT_SYMBOL(ttm_bo_mem_put);
    808 
    809 /**
    810  * Repeatedly evict memory from the LRU for @mem_type until we create enough
    811  * space, or we've evicted everything and there isn't enough space.
    812  */
    813 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
    814 					uint32_t mem_type,
    815 					struct ttm_placement *placement,
    816 					struct ttm_mem_reg *mem,
    817 					bool interruptible,
    818 					bool no_wait_gpu)
    819 {
    820 	struct ttm_bo_device *bdev = bo->bdev;
    821 	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
    822 	int ret;
    823 
    824 	do {
    825 		ret = (*man->func->get_node)(man, bo, placement, mem);
    826 		if (unlikely(ret != 0))
    827 			return ret;
    828 		if (mem->mm_node)
    829 			break;
    830 		ret = ttm_mem_evict_first(bdev, mem_type,
    831 					  interruptible, no_wait_gpu);
    832 		if (unlikely(ret != 0))
    833 			return ret;
    834 	} while (1);
    835 	if (mem->mm_node == NULL)
    836 		return -ENOMEM;
    837 	mem->mem_type = mem_type;
    838 	return 0;
    839 }
    840 
    841 static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
    842 				      uint32_t cur_placement,
    843 				      uint32_t proposed_placement)
    844 {
    845 	uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
    846 	uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
    847 
    848 	/**
    849 	 * Keep current caching if possible.
    850 	 */
    851 
    852 	if ((cur_placement & caching) != 0)
    853 		result |= (cur_placement & caching);
    854 	else if ((man->default_caching & caching) != 0)
    855 		result |= man->default_caching;
    856 	else if ((TTM_PL_FLAG_CACHED & caching) != 0)
    857 		result |= TTM_PL_FLAG_CACHED;
    858 	else if ((TTM_PL_FLAG_WC & caching) != 0)
    859 		result |= TTM_PL_FLAG_WC;
    860 	else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
    861 		result |= TTM_PL_FLAG_UNCACHED;
    862 
    863 	return result;
    864 }
    865 
    866 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
    867 				 uint32_t mem_type,
    868 				 uint32_t proposed_placement,
    869 				 uint32_t *masked_placement)
    870 {
    871 	uint32_t cur_flags = ttm_bo_type_flags(mem_type);
    872 
    873 	if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
    874 		return false;
    875 
    876 	if ((proposed_placement & man->available_caching) == 0)
    877 		return false;
    878 
    879 	cur_flags |= (proposed_placement & man->available_caching);
    880 
    881 	*masked_placement = cur_flags;
    882 	return true;
    883 }
    884 
    885 /**
    886  * Creates space for memory region @mem according to its type.
    887  *
    888  * This function first searches for free space in compatible memory types in
    889  * the priority order defined by the driver.  If free space isn't found, then
    890  * ttm_bo_mem_force_space is attempted in priority order to evict and find
    891  * space.
    892  */
    893 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
    894 			struct ttm_placement *placement,
    895 			struct ttm_mem_reg *mem,
    896 			bool interruptible,
    897 			bool no_wait_gpu)
    898 {
    899 	struct ttm_bo_device *bdev = bo->bdev;
    900 	struct ttm_mem_type_manager *man;
    901 	uint32_t mem_type = TTM_PL_SYSTEM;
    902 	uint32_t cur_flags = 0;
    903 	bool type_found = false;
    904 	bool type_ok = false;
    905 	bool has_erestartsys = false;
    906 	int i, ret;
    907 
    908 	mem->mm_node = NULL;
    909 	for (i = 0; i < placement->num_placement; ++i) {
    910 		ret = ttm_mem_type_from_flags(placement->placement[i],
    911 						&mem_type);
    912 		if (ret)
    913 			return ret;
    914 		man = &bdev->man[mem_type];
    915 
    916 		type_ok = ttm_bo_mt_compatible(man,
    917 						mem_type,
    918 						placement->placement[i],
    919 						&cur_flags);
    920 
    921 		if (!type_ok)
    922 			continue;
    923 
    924 		cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
    925 						  cur_flags);
    926 		/*
    927 		 * Use the access and other non-mapping-related flag bits from
    928 		 * the memory placement flags to the current flags
    929 		 */
    930 		ttm_flag_masked(&cur_flags, placement->placement[i],
    931 				~TTM_PL_MASK_MEMTYPE);
    932 
    933 		if (mem_type == TTM_PL_SYSTEM)
    934 			break;
    935 
    936 		if (man->has_type && man->use_type) {
    937 			type_found = true;
    938 			ret = (*man->func->get_node)(man, bo, placement, mem);
    939 			if (unlikely(ret))
    940 				return ret;
    941 		}
    942 		if (mem->mm_node)
    943 			break;
    944 	}
    945 
    946 	if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
    947 		mem->mem_type = mem_type;
    948 		mem->placement = cur_flags;
    949 		return 0;
    950 	}
    951 
    952 	if (!type_found)
    953 		return -EINVAL;
    954 
    955 	for (i = 0; i < placement->num_busy_placement; ++i) {
    956 		ret = ttm_mem_type_from_flags(placement->busy_placement[i],
    957 						&mem_type);
    958 		if (ret)
    959 			return ret;
    960 		man = &bdev->man[mem_type];
    961 		if (!man->has_type)
    962 			continue;
    963 		if (!ttm_bo_mt_compatible(man,
    964 						mem_type,
    965 						placement->busy_placement[i],
    966 						&cur_flags))
    967 			continue;
    968 
    969 		cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
    970 						  cur_flags);
    971 		/*
    972 		 * Use the access and other non-mapping-related flag bits from
    973 		 * the memory placement flags to the current flags
    974 		 */
    975 		ttm_flag_masked(&cur_flags, placement->busy_placement[i],
    976 				~TTM_PL_MASK_MEMTYPE);
    977 
    978 
    979 		if (mem_type == TTM_PL_SYSTEM) {
    980 			mem->mem_type = mem_type;
    981 			mem->placement = cur_flags;
    982 			mem->mm_node = NULL;
    983 			return 0;
    984 		}
    985 
    986 		ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
    987 						interruptible, no_wait_gpu);
    988 		if (ret == 0 && mem->mm_node) {
    989 			mem->placement = cur_flags;
    990 			return 0;
    991 		}
    992 		if (ret == -ERESTARTSYS)
    993 			has_erestartsys = true;
    994 	}
    995 	ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
    996 	return ret;
    997 }
    998 EXPORT_SYMBOL(ttm_bo_mem_space);
    999 
   1000 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
   1001 			struct ttm_placement *placement,
   1002 			bool interruptible,
   1003 			bool no_wait_gpu)
   1004 {
   1005 	int ret = 0;
   1006 	struct ttm_mem_reg mem;
   1007 	struct ttm_bo_device *bdev = bo->bdev;
   1008 
   1009 	lockdep_assert_held(&bo->resv->lock.base);
   1010 
   1011 	/*
   1012 	 * FIXME: It's possible to pipeline buffer moves.
   1013 	 * Have the driver move function wait for idle when necessary,
   1014 	 * instead of doing it here.
   1015 	 */
   1016 	spin_lock(&bdev->fence_lock);
   1017 	ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
   1018 	spin_unlock(&bdev->fence_lock);
   1019 	if (ret)
   1020 		return ret;
   1021 	mem.num_pages = bo->num_pages;
   1022 	mem.size = mem.num_pages << PAGE_SHIFT;
   1023 	mem.page_alignment = bo->mem.page_alignment;
   1024 	mem.bus.io_reserved_vm = false;
   1025 	mem.bus.io_reserved_count = 0;
   1026 	/*
   1027 	 * Determine where to move the buffer.
   1028 	 */
   1029 	ret = ttm_bo_mem_space(bo, placement, &mem,
   1030 			       interruptible, no_wait_gpu);
   1031 	if (ret)
   1032 		goto out_unlock;
   1033 	ret = ttm_bo_handle_move_mem(bo, &mem, false,
   1034 				     interruptible, no_wait_gpu);
   1035 out_unlock:
   1036 	if (ret && mem.mm_node)
   1037 		ttm_bo_mem_put(bo, &mem);
   1038 	return ret;
   1039 }
   1040 
   1041 static bool ttm_bo_mem_compat(struct ttm_placement *placement,
   1042 			      struct ttm_mem_reg *mem,
   1043 			      uint32_t *new_flags)
   1044 {
   1045 	int i;
   1046 
   1047 	if (mem->mm_node && placement->lpfn != 0 &&
   1048 	    (mem->start < placement->fpfn ||
   1049 	     mem->start + mem->num_pages > placement->lpfn))
   1050 		return false;
   1051 
   1052 	for (i = 0; i < placement->num_placement; i++) {
   1053 		*new_flags = placement->placement[i];
   1054 		if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
   1055 		    (*new_flags & mem->placement & TTM_PL_MASK_MEM))
   1056 			return true;
   1057 	}
   1058 
   1059 	for (i = 0; i < placement->num_busy_placement; i++) {
   1060 		*new_flags = placement->busy_placement[i];
   1061 		if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
   1062 		    (*new_flags & mem->placement & TTM_PL_MASK_MEM))
   1063 			return true;
   1064 	}
   1065 
   1066 	return false;
   1067 }
   1068 
   1069 int ttm_bo_validate(struct ttm_buffer_object *bo,
   1070 			struct ttm_placement *placement,
   1071 			bool interruptible,
   1072 			bool no_wait_gpu)
   1073 {
   1074 	int ret;
   1075 	uint32_t new_flags;
   1076 
   1077 	lockdep_assert_held(&bo->resv->lock.base);
   1078 	/* Check that range is valid */
   1079 	if (placement->lpfn || placement->fpfn)
   1080 		if (placement->fpfn > placement->lpfn ||
   1081 			(placement->lpfn - placement->fpfn) < bo->num_pages)
   1082 			return -EINVAL;
   1083 	/*
   1084 	 * Check whether we need to move buffer.
   1085 	 */
   1086 	if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
   1087 		ret = ttm_bo_move_buffer(bo, placement, interruptible,
   1088 					 no_wait_gpu);
   1089 		if (ret)
   1090 			return ret;
   1091 	} else {
   1092 		/*
   1093 		 * Use the access and other non-mapping-related flag bits from
   1094 		 * the compatible memory placement flags to the active flags
   1095 		 */
   1096 		ttm_flag_masked(&bo->mem.placement, new_flags,
   1097 				~TTM_PL_MASK_MEMTYPE);
   1098 	}
   1099 	/*
   1100 	 * We might need to add a TTM.
   1101 	 */
   1102 	if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
   1103 		ret = ttm_bo_add_ttm(bo, true);
   1104 		if (ret)
   1105 			return ret;
   1106 	}
   1107 	return 0;
   1108 }
   1109 EXPORT_SYMBOL(ttm_bo_validate);
   1110 
   1111 int ttm_bo_check_placement(struct ttm_buffer_object *bo,
   1112 				struct ttm_placement *placement)
   1113 {
   1114 	BUG_ON((placement->fpfn || placement->lpfn) &&
   1115 	       (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
   1116 
   1117 	return 0;
   1118 }
   1119 
   1120 int ttm_bo_init(struct ttm_bo_device *bdev,
   1121 		struct ttm_buffer_object *bo,
   1122 		unsigned long size,
   1123 		enum ttm_bo_type type,
   1124 		struct ttm_placement *placement,
   1125 		uint32_t page_alignment,
   1126 		bool interruptible,
   1127 		struct file *persistent_swap_storage,
   1128 		size_t acc_size,
   1129 		struct sg_table *sg,
   1130 		void (*destroy) (struct ttm_buffer_object *))
   1131 {
   1132 	int ret = 0;
   1133 	unsigned long num_pages;
   1134 	struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
   1135 	bool locked;
   1136 
   1137 	ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
   1138 	if (ret) {
   1139 		pr_err("Out of kernel memory\n");
   1140 		if (destroy)
   1141 			(*destroy)(bo);
   1142 		else
   1143 			kfree(bo);
   1144 		return -ENOMEM;
   1145 	}
   1146 
   1147 	num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
   1148 	if (num_pages == 0) {
   1149 		pr_err("Illegal buffer object size\n");
   1150 		if (destroy)
   1151 			(*destroy)(bo);
   1152 		else
   1153 			kfree(bo);
   1154 		ttm_mem_global_free(mem_glob, acc_size);
   1155 		return -EINVAL;
   1156 	}
   1157 	bo->destroy = destroy;
   1158 
   1159 	kref_init(&bo->kref);
   1160 	kref_init(&bo->list_kref);
   1161 	atomic_set(&bo->cpu_writers, 0);
   1162 	INIT_LIST_HEAD(&bo->lru);
   1163 	INIT_LIST_HEAD(&bo->ddestroy);
   1164 	INIT_LIST_HEAD(&bo->swap);
   1165 	INIT_LIST_HEAD(&bo->io_reserve_lru);
   1166 #ifdef __NetBSD__
   1167 	linux_mutex_init(&bo->wu_mutex);
   1168 #else
   1169 	mutex_init(&bo->wu_mutex);
   1170 #endif
   1171 	bo->bdev = bdev;
   1172 	bo->glob = bdev->glob;
   1173 	bo->type = type;
   1174 	bo->num_pages = num_pages;
   1175 	bo->mem.size = num_pages << PAGE_SHIFT;
   1176 	bo->mem.mem_type = TTM_PL_SYSTEM;
   1177 	bo->mem.num_pages = bo->num_pages;
   1178 	bo->mem.mm_node = NULL;
   1179 	bo->mem.page_alignment = page_alignment;
   1180 	bo->mem.bus.io_reserved_vm = false;
   1181 	bo->mem.bus.io_reserved_count = 0;
   1182 	bo->priv_flags = 0;
   1183 	bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
   1184 	bo->persistent_swap_storage = persistent_swap_storage;
   1185 	bo->acc_size = acc_size;
   1186 	bo->sg = sg;
   1187 	bo->resv = &bo->ttm_resv;
   1188 	reservation_object_init(bo->resv);
   1189 	atomic_inc(&bo->glob->bo_count);
   1190 #ifdef __NetBSD__
   1191 	drm_vma_node_init(&bo->vma_node);
   1192 	uvm_obj_init(&bo->uvmobj, bdev->driver->ttm_uvm_ops, true, 1);
   1193 #else
   1194 	drm_vma_node_reset(&bo->vma_node);
   1195 #endif
   1196 
   1197 	ret = ttm_bo_check_placement(bo, placement);
   1198 
   1199 	/*
   1200 	 * For ttm_bo_type_device buffers, allocate
   1201 	 * address space from the device.
   1202 	 */
   1203 	if (likely(!ret) &&
   1204 	    (bo->type == ttm_bo_type_device ||
   1205 	     bo->type == ttm_bo_type_sg))
   1206 		ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
   1207 					 bo->mem.num_pages);
   1208 
   1209 	locked = ww_mutex_trylock(&bo->resv->lock);
   1210 	WARN_ON(!locked);
   1211 
   1212 	if (likely(!ret))
   1213 		ret = ttm_bo_validate(bo, placement, interruptible, false);
   1214 
   1215 	ttm_bo_unreserve(bo);
   1216 
   1217 	if (unlikely(ret))
   1218 		ttm_bo_unref(&bo);
   1219 
   1220 	return ret;
   1221 }
   1222 EXPORT_SYMBOL(ttm_bo_init);
   1223 
   1224 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
   1225 		       unsigned long bo_size,
   1226 		       unsigned struct_size)
   1227 {
   1228 	unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
   1229 	size_t size = 0;
   1230 
   1231 	size += ttm_round_pot(struct_size);
   1232 	size += PAGE_ALIGN(npages * sizeof(void *));
   1233 	size += ttm_round_pot(sizeof(struct ttm_tt));
   1234 	return size;
   1235 }
   1236 EXPORT_SYMBOL(ttm_bo_acc_size);
   1237 
   1238 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
   1239 			   unsigned long bo_size,
   1240 			   unsigned struct_size)
   1241 {
   1242 	unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
   1243 	size_t size = 0;
   1244 
   1245 	size += ttm_round_pot(struct_size);
   1246 	size += PAGE_ALIGN(npages * sizeof(void *));
   1247 	size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
   1248 	size += ttm_round_pot(sizeof(struct ttm_dma_tt));
   1249 	return size;
   1250 }
   1251 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
   1252 
   1253 int ttm_bo_create(struct ttm_bo_device *bdev,
   1254 			unsigned long size,
   1255 			enum ttm_bo_type type,
   1256 			struct ttm_placement *placement,
   1257 			uint32_t page_alignment,
   1258 			bool interruptible,
   1259 			struct file *persistent_swap_storage,
   1260 			struct ttm_buffer_object **p_bo)
   1261 {
   1262 	struct ttm_buffer_object *bo;
   1263 	size_t acc_size;
   1264 	int ret;
   1265 
   1266 	bo = kzalloc(sizeof(*bo), GFP_KERNEL);
   1267 	if (unlikely(bo == NULL))
   1268 		return -ENOMEM;
   1269 
   1270 	acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
   1271 	ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
   1272 			  interruptible, persistent_swap_storage, acc_size,
   1273 			  NULL, NULL);
   1274 	if (likely(ret == 0))
   1275 		*p_bo = bo;
   1276 
   1277 	return ret;
   1278 }
   1279 EXPORT_SYMBOL(ttm_bo_create);
   1280 
   1281 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
   1282 					unsigned mem_type, bool allow_errors)
   1283 {
   1284 	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
   1285 	struct ttm_bo_global *glob = bdev->glob;
   1286 	int ret;
   1287 
   1288 	/*
   1289 	 * Can't use standard list traversal since we're unlocking.
   1290 	 */
   1291 
   1292 	spin_lock(&glob->lru_lock);
   1293 	while (!list_empty(&man->lru)) {
   1294 		spin_unlock(&glob->lru_lock);
   1295 		ret = ttm_mem_evict_first(bdev, mem_type, false, false);
   1296 		if (ret) {
   1297 			if (allow_errors) {
   1298 				return ret;
   1299 			} else {
   1300 				pr_err("Cleanup eviction failed\n");
   1301 			}
   1302 		}
   1303 		spin_lock(&glob->lru_lock);
   1304 	}
   1305 	spin_unlock(&glob->lru_lock);
   1306 	return 0;
   1307 }
   1308 
   1309 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
   1310 {
   1311 	struct ttm_mem_type_manager *man;
   1312 	int ret = -EINVAL;
   1313 
   1314 	if (mem_type >= TTM_NUM_MEM_TYPES) {
   1315 		pr_err("Illegal memory type %d\n", mem_type);
   1316 		return ret;
   1317 	}
   1318 	man = &bdev->man[mem_type];
   1319 
   1320 	if (!man->has_type) {
   1321 		pr_err("Trying to take down uninitialized memory manager type %u\n",
   1322 		       mem_type);
   1323 		return ret;
   1324 	}
   1325 
   1326 	man->use_type = false;
   1327 	man->has_type = false;
   1328 
   1329 	ret = 0;
   1330 	if (mem_type > 0) {
   1331 		ttm_bo_force_list_clean(bdev, mem_type, false);
   1332 
   1333 		ret = (*man->func->takedown)(man);
   1334 	}
   1335 
   1336 #ifdef __NetBSD__
   1337 	linux_mutex_destroy(&man->io_reserve_mutex);
   1338 #else
   1339 	mutex_destroy(&man->io_reserve_mutex);
   1340 #endif
   1341 
   1342 	return ret;
   1343 }
   1344 EXPORT_SYMBOL(ttm_bo_clean_mm);
   1345 
   1346 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
   1347 {
   1348 	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
   1349 
   1350 	if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
   1351 		pr_err("Illegal memory manager memory type %u\n", mem_type);
   1352 		return -EINVAL;
   1353 	}
   1354 
   1355 	if (!man->has_type) {
   1356 		pr_err("Memory type %u has not been initialized\n", mem_type);
   1357 		return 0;
   1358 	}
   1359 
   1360 	return ttm_bo_force_list_clean(bdev, mem_type, true);
   1361 }
   1362 EXPORT_SYMBOL(ttm_bo_evict_mm);
   1363 
   1364 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
   1365 			unsigned long p_size)
   1366 {
   1367 	int ret = -EINVAL;
   1368 	struct ttm_mem_type_manager *man;
   1369 
   1370 	BUG_ON(type >= TTM_NUM_MEM_TYPES);
   1371 	man = &bdev->man[type];
   1372 	BUG_ON(man->has_type);
   1373 	man->io_reserve_fastpath = true;
   1374 	man->use_io_reserve_lru = false;
   1375 #ifdef __NetBSD__
   1376 	linux_mutex_init(&man->io_reserve_mutex);
   1377 #else
   1378 	mutex_init(&man->io_reserve_mutex);
   1379 #endif
   1380 	INIT_LIST_HEAD(&man->io_reserve_lru);
   1381 
   1382 	ret = bdev->driver->init_mem_type(bdev, type, man);
   1383 	if (ret)
   1384 		return ret;
   1385 	man->bdev = bdev;
   1386 
   1387 	ret = 0;
   1388 	if (type != TTM_PL_SYSTEM) {
   1389 		ret = (*man->func->init)(man, p_size);
   1390 		if (ret)
   1391 			return ret;
   1392 	}
   1393 	man->has_type = true;
   1394 	man->use_type = true;
   1395 	man->size = p_size;
   1396 
   1397 	INIT_LIST_HEAD(&man->lru);
   1398 
   1399 	return 0;
   1400 }
   1401 EXPORT_SYMBOL(ttm_bo_init_mm);
   1402 
   1403 #ifndef __NetBSD__
   1404 static void ttm_bo_global_kobj_release(struct kobject *kobj)
   1405 {
   1406 	struct ttm_bo_global *glob =
   1407 		container_of(kobj, struct ttm_bo_global, kobj);
   1408 
   1409 	ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
   1410 	__free_page(glob->dummy_read_page);
   1411 	mutex_destroy(&glob->device_list_mutex);
   1412 	kfree(glob);
   1413 }
   1414 #endif
   1415 
   1416 void ttm_bo_global_release(struct drm_global_reference *ref)
   1417 {
   1418 	struct ttm_bo_global *glob = ref->object;
   1419 
   1420 #ifdef __NetBSD__
   1421 	ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
   1422 	BUG_ON(glob->dummy_read_page != NULL);
   1423 	spin_lock_destroy(&glob->lru_lock);
   1424 	linux_mutex_destroy(&glob->device_list_mutex);
   1425 	kfree(glob);
   1426 #else
   1427 	kobject_del(&glob->kobj);
   1428 	kobject_put(&glob->kobj);
   1429 #endif
   1430 }
   1431 EXPORT_SYMBOL(ttm_bo_global_release);
   1432 
   1433 int ttm_bo_global_init(struct drm_global_reference *ref)
   1434 {
   1435 	struct ttm_bo_global_ref *bo_ref =
   1436 		container_of(ref, struct ttm_bo_global_ref, ref);
   1437 	struct ttm_bo_global *glob = ref->object;
   1438 	int ret;
   1439 
   1440 #ifdef __NetBSD__
   1441 	linux_mutex_init(&glob->device_list_mutex);
   1442 #else
   1443 	mutex_init(&glob->device_list_mutex);
   1444 #endif
   1445 	spin_lock_init(&glob->lru_lock);
   1446 	glob->mem_glob = bo_ref->mem_glob;
   1447 #ifdef __NetBSD__
   1448 	/* Only used by agp back end, will fix there.  */
   1449 	/* XXX Fix agp back end to DTRT.  */
   1450 	glob->dummy_read_page = NULL;
   1451 #else
   1452 	glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
   1453 
   1454 	if (unlikely(glob->dummy_read_page == NULL)) {
   1455 		ret = -ENOMEM;
   1456 		goto out_no_drp;
   1457 	}
   1458 #endif
   1459 
   1460 	INIT_LIST_HEAD(&glob->swap_lru);
   1461 	INIT_LIST_HEAD(&glob->device_list);
   1462 
   1463 	ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
   1464 	ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
   1465 	if (unlikely(ret != 0)) {
   1466 		pr_err("Could not register buffer object swapout\n");
   1467 		goto out_no_shrink;
   1468 	}
   1469 
   1470 	atomic_set(&glob->bo_count, 0);
   1471 
   1472 #ifdef __NetBSD__
   1473 	ret = 0;
   1474 #else
   1475 	ret = kobject_init_and_add(
   1476 		&glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
   1477 	if (unlikely(ret != 0))
   1478 		kobject_put(&glob->kobj);
   1479 #endif
   1480 	return ret;
   1481 out_no_shrink:
   1482 #ifndef __NetBSD__
   1483 	__free_page(glob->dummy_read_page);
   1484 out_no_drp:
   1485 #endif
   1486 	kfree(glob);
   1487 	return ret;
   1488 }
   1489 EXPORT_SYMBOL(ttm_bo_global_init);
   1490 
   1491 
   1492 int ttm_bo_device_release(struct ttm_bo_device *bdev)
   1493 {
   1494 	int ret = 0;
   1495 	unsigned i = TTM_NUM_MEM_TYPES;
   1496 	struct ttm_mem_type_manager *man;
   1497 	struct ttm_bo_global *glob = bdev->glob;
   1498 
   1499 	while (i--) {
   1500 		man = &bdev->man[i];
   1501 		if (man->has_type) {
   1502 			man->use_type = false;
   1503 			if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
   1504 				ret = -EBUSY;
   1505 				pr_err("DRM memory manager type %d is not clean\n",
   1506 				       i);
   1507 			}
   1508 			man->has_type = false;
   1509 		}
   1510 	}
   1511 
   1512 	mutex_lock(&glob->device_list_mutex);
   1513 	list_del(&bdev->device_list);
   1514 	mutex_unlock(&glob->device_list_mutex);
   1515 
   1516 	cancel_delayed_work_sync(&bdev->wq);
   1517 
   1518 	while (ttm_bo_delayed_delete(bdev, true))
   1519 		;
   1520 
   1521 	spin_lock(&glob->lru_lock);
   1522 	if (list_empty(&bdev->ddestroy))
   1523 		TTM_DEBUG("Delayed destroy list was clean\n");
   1524 
   1525 	if (list_empty(&bdev->man[0].lru))
   1526 		TTM_DEBUG("Swap list was clean\n");
   1527 	spin_unlock(&glob->lru_lock);
   1528 
   1529 	drm_vma_offset_manager_destroy(&bdev->vma_manager);
   1530 
   1531 	return ret;
   1532 }
   1533 EXPORT_SYMBOL(ttm_bo_device_release);
   1534 
   1535 int ttm_bo_device_init(struct ttm_bo_device *bdev,
   1536 		       struct ttm_bo_global *glob,
   1537 		       struct ttm_bo_driver *driver,
   1538 #ifdef __NetBSD__
   1539 		       bus_space_tag_t memt,
   1540 		       bus_dma_tag_t dmat,
   1541 #else
   1542 		       struct address_space *mapping,
   1543 #endif
   1544 		       uint64_t file_page_offset,
   1545 		       bool need_dma32)
   1546 {
   1547 	int ret = -EINVAL;
   1548 
   1549 	bdev->driver = driver;
   1550 
   1551 	memset(bdev->man, 0, sizeof(bdev->man));
   1552 
   1553 	/*
   1554 	 * Initialize the system memory buffer type.
   1555 	 * Other types need to be driver / IOCTL initialized.
   1556 	 */
   1557 	ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
   1558 	if (unlikely(ret != 0))
   1559 		goto out_no_sys;
   1560 
   1561 	drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
   1562 				    0x10000000);
   1563 	INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
   1564 	INIT_LIST_HEAD(&bdev->ddestroy);
   1565 #ifdef __NetBSD__
   1566 	bdev->memt = memt;
   1567 	bdev->dmat = dmat;
   1568 #else
   1569 	bdev->dev_mapping = mapping;
   1570 #endif
   1571 	bdev->glob = glob;
   1572 	bdev->need_dma32 = need_dma32;
   1573 	bdev->val_seq = 0;
   1574 	spin_lock_init(&bdev->fence_lock);
   1575 	mutex_lock(&glob->device_list_mutex);
   1576 	list_add_tail(&bdev->device_list, &glob->device_list);
   1577 	mutex_unlock(&glob->device_list_mutex);
   1578 
   1579 	return 0;
   1580 out_no_sys:
   1581 	return ret;
   1582 }
   1583 EXPORT_SYMBOL(ttm_bo_device_init);
   1584 
   1585 /*
   1586  * buffer object vm functions.
   1587  */
   1588 
   1589 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
   1590 {
   1591 	struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
   1592 
   1593 	if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
   1594 		if (mem->mem_type == TTM_PL_SYSTEM)
   1595 			return false;
   1596 
   1597 		if (man->flags & TTM_MEMTYPE_FLAG_CMA)
   1598 			return false;
   1599 
   1600 		if (mem->placement & TTM_PL_FLAG_CACHED)
   1601 			return false;
   1602 	}
   1603 	return true;
   1604 }
   1605 
   1606 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
   1607 {
   1608 #ifndef __NetBSD__
   1609 	struct ttm_bo_device *bdev = bo->bdev;
   1610 #endif
   1611 
   1612 #ifdef __NetBSD__
   1613 	if (bo->mem.bus.is_iomem) {
   1614 		paddr_t start, end, pa;
   1615 
   1616 		KASSERT((bo->mem.bus.base & (PAGE_SIZE - 1)) == 0);
   1617 		KASSERT((bo->mem.bus.offset & (PAGE_SIZE - 1)) == 0);
   1618 		start = bo->mem.bus.base + bo->mem.bus.offset;
   1619 		KASSERT((bo->mem.bus.size & (PAGE_SIZE - 1)) == 0);
   1620 		end = start + bo->mem.bus.size;
   1621 
   1622 		for (pa = start; pa < end; pa += PAGE_SIZE)
   1623 			pmap_pv_protect(pa, VM_PROT_NONE);
   1624 	} else if (bo->ttm != NULL) {
   1625 		unsigned i;
   1626 
   1627 		for (i = 0; i < bo->ttm->num_pages; i++)
   1628 			pmap_page_protect(&bo->ttm->pages[i]->p_vmp,
   1629 			    VM_PROT_NONE);
   1630 	}
   1631 #else
   1632 	drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
   1633 #endif
   1634 	ttm_mem_io_free_vm(bo);
   1635 }
   1636 
   1637 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
   1638 {
   1639 	struct ttm_bo_device *bdev = bo->bdev;
   1640 	struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
   1641 
   1642 	ttm_mem_io_lock(man, false);
   1643 	ttm_bo_unmap_virtual_locked(bo);
   1644 	ttm_mem_io_unlock(man);
   1645 }
   1646 
   1647 
   1648 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
   1649 
   1650 
   1651 int ttm_bo_wait(struct ttm_buffer_object *bo,
   1652 		bool lazy, bool interruptible, bool no_wait)
   1653 {
   1654 	struct ttm_bo_driver *driver = bo->bdev->driver;
   1655 	struct ttm_bo_device *bdev = bo->bdev;
   1656 	void *sync_obj;
   1657 	int ret = 0;
   1658 
   1659 	if (likely(bo->sync_obj == NULL))
   1660 		return 0;
   1661 
   1662 	while (bo->sync_obj) {
   1663 
   1664 		if (driver->sync_obj_signaled(bo->sync_obj)) {
   1665 			void *tmp_obj = bo->sync_obj;
   1666 			bo->sync_obj = NULL;
   1667 			clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
   1668 			spin_unlock(&bdev->fence_lock);
   1669 			driver->sync_obj_unref(&tmp_obj);
   1670 			spin_lock(&bdev->fence_lock);
   1671 			continue;
   1672 		}
   1673 
   1674 		if (no_wait)
   1675 			return -EBUSY;
   1676 
   1677 		sync_obj = driver->sync_obj_ref(bo->sync_obj);
   1678 		spin_unlock(&bdev->fence_lock);
   1679 		ret = driver->sync_obj_wait(sync_obj,
   1680 					    lazy, interruptible);
   1681 		if (unlikely(ret != 0)) {
   1682 			driver->sync_obj_unref(&sync_obj);
   1683 			spin_lock(&bdev->fence_lock);
   1684 			return ret;
   1685 		}
   1686 		spin_lock(&bdev->fence_lock);
   1687 		if (likely(bo->sync_obj == sync_obj)) {
   1688 			void *tmp_obj = bo->sync_obj;
   1689 			bo->sync_obj = NULL;
   1690 			clear_bit(TTM_BO_PRIV_FLAG_MOVING,
   1691 				  &bo->priv_flags);
   1692 			spin_unlock(&bdev->fence_lock);
   1693 			driver->sync_obj_unref(&sync_obj);
   1694 			driver->sync_obj_unref(&tmp_obj);
   1695 			spin_lock(&bdev->fence_lock);
   1696 		} else {
   1697 			spin_unlock(&bdev->fence_lock);
   1698 			driver->sync_obj_unref(&sync_obj);
   1699 			spin_lock(&bdev->fence_lock);
   1700 		}
   1701 	}
   1702 	return 0;
   1703 }
   1704 EXPORT_SYMBOL(ttm_bo_wait);
   1705 
   1706 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
   1707 {
   1708 	struct ttm_bo_device *bdev = bo->bdev;
   1709 	int ret = 0;
   1710 
   1711 	/*
   1712 	 * Using ttm_bo_reserve makes sure the lru lists are updated.
   1713 	 */
   1714 
   1715 	ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
   1716 	if (unlikely(ret != 0))
   1717 		return ret;
   1718 	spin_lock(&bdev->fence_lock);
   1719 	ret = ttm_bo_wait(bo, false, true, no_wait);
   1720 	spin_unlock(&bdev->fence_lock);
   1721 	if (likely(ret == 0))
   1722 		atomic_inc(&bo->cpu_writers);
   1723 	ttm_bo_unreserve(bo);
   1724 	return ret;
   1725 }
   1726 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
   1727 
   1728 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
   1729 {
   1730 	atomic_dec(&bo->cpu_writers);
   1731 }
   1732 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
   1733 
   1734 /**
   1735  * A buffer object shrink method that tries to swap out the first
   1736  * buffer object on the bo_global::swap_lru list.
   1737  */
   1738 
   1739 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
   1740 {
   1741 	struct ttm_bo_global *glob =
   1742 	    container_of(shrink, struct ttm_bo_global, shrink);
   1743 	struct ttm_buffer_object *bo;
   1744 	int ret = -EBUSY;
   1745 	int put_count;
   1746 	uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
   1747 
   1748 	spin_lock(&glob->lru_lock);
   1749 	list_for_each_entry(bo, &glob->swap_lru, swap) {
   1750 		ret = __ttm_bo_reserve(bo, false, true, false, 0);
   1751 		if (!ret)
   1752 			break;
   1753 	}
   1754 
   1755 	if (ret) {
   1756 		spin_unlock(&glob->lru_lock);
   1757 		return ret;
   1758 	}
   1759 
   1760 	kref_get(&bo->list_kref);
   1761 
   1762 	if (!list_empty(&bo->ddestroy)) {
   1763 		ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
   1764 		kref_put(&bo->list_kref, ttm_bo_release_list);
   1765 		return ret;
   1766 	}
   1767 
   1768 	put_count = ttm_bo_del_from_lru(bo);
   1769 	spin_unlock(&glob->lru_lock);
   1770 
   1771 	ttm_bo_list_ref_sub(bo, put_count, true);
   1772 
   1773 	/**
   1774 	 * Wait for GPU, then move to system cached.
   1775 	 */
   1776 
   1777 	spin_lock(&bo->bdev->fence_lock);
   1778 	ret = ttm_bo_wait(bo, false, false, false);
   1779 	spin_unlock(&bo->bdev->fence_lock);
   1780 
   1781 	if (unlikely(ret != 0))
   1782 		goto out;
   1783 
   1784 	if ((bo->mem.placement & swap_placement) != swap_placement) {
   1785 		struct ttm_mem_reg evict_mem;
   1786 
   1787 		evict_mem = bo->mem;
   1788 		evict_mem.mm_node = NULL;
   1789 		evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
   1790 		evict_mem.mem_type = TTM_PL_SYSTEM;
   1791 
   1792 		ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
   1793 					     false, false);
   1794 		if (unlikely(ret != 0))
   1795 			goto out;
   1796 	}
   1797 
   1798 	ttm_bo_unmap_virtual(bo);
   1799 
   1800 	/**
   1801 	 * Swap out. Buffer will be swapped in again as soon as
   1802 	 * anyone tries to access a ttm page.
   1803 	 */
   1804 
   1805 	if (bo->bdev->driver->swap_notify)
   1806 		bo->bdev->driver->swap_notify(bo);
   1807 
   1808 	ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
   1809 out:
   1810 
   1811 	/**
   1812 	 *
   1813 	 * Unreserve without putting on LRU to avoid swapping out an
   1814 	 * already swapped buffer.
   1815 	 */
   1816 
   1817 	__ttm_bo_unreserve(bo);
   1818 	kref_put(&bo->list_kref, ttm_bo_release_list);
   1819 	return ret;
   1820 }
   1821 
   1822 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
   1823 {
   1824 	while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
   1825 		;
   1826 }
   1827 EXPORT_SYMBOL(ttm_bo_swapout_all);
   1828 
   1829 /**
   1830  * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
   1831  * unreserved
   1832  *
   1833  * @bo: Pointer to buffer
   1834  */
   1835 int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
   1836 {
   1837 	int ret;
   1838 
   1839 	/*
   1840 	 * In the absense of a wait_unlocked API,
   1841 	 * Use the bo::wu_mutex to avoid triggering livelocks due to
   1842 	 * concurrent use of this function. Note that this use of
   1843 	 * bo::wu_mutex can go away if we change locking order to
   1844 	 * mmap_sem -> bo::reserve.
   1845 	 */
   1846 	ret = mutex_lock_interruptible(&bo->wu_mutex);
   1847 	if (unlikely(ret != 0))
   1848 		return -ERESTARTSYS;
   1849 	if (!ww_mutex_is_locked(&bo->resv->lock))
   1850 		goto out_unlock;
   1851 	ret = __ttm_bo_reserve(bo, true, false, false, NULL);
   1852 	if (unlikely(ret != 0))
   1853 		goto out_unlock;
   1854 	__ttm_bo_unreserve(bo);
   1855 
   1856 out_unlock:
   1857 	mutex_unlock(&bo->wu_mutex);
   1858 	return ret;
   1859 }
   1860