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