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