Home | History | Annotate | Line # | Download | only in ttm
ttm_bo.c revision 1.11
      1 /*	$NetBSD: ttm_bo.c,v 1.11 2018/08/27 04:58:37 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.11 2018/08/27 04:58:37 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/ttm/ttm_module.h>
     45 #include <drm/ttm/ttm_bo_driver.h>
     46 #include <drm/ttm/ttm_placement.h>
     47 #include <linux/jiffies.h>
     48 #include <linux/slab.h>
     49 #include <linux/sched.h>
     50 #include <linux/mm.h>
     51 #include <linux/file.h>
     52 #include <linux/module.h>
     53 #include <linux/atomic.h>
     54 #include <linux/reservation.h>
     55 #include <linux/printk.h>
     56 #include <linux/export.h>
     57 
     58 #define TTM_ASSERT_LOCKED(param)
     59 #define TTM_DEBUG(fmt, arg...)	do {} while (0)
     60 #define TTM_BO_HASH_ORDER 13
     61 
     62 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
     63 #ifndef __NetBSD__
     64 static void ttm_bo_global_kobj_release(struct kobject *kobj);
     65 #endif
     66 
     67 #ifndef __NetBSD__		/* XXX sysfs */
     68 static struct attribute ttm_bo_count = {
     69 	.name = "bo_count",
     70 	.mode = S_IRUGO
     71 };
     72 #endif
     73 
     74 static inline int ttm_mem_type_from_place(const struct ttm_place *place,
     75 					  uint32_t *mem_type)
     76 {
     77 	int i;
     78 
     79 	for (i = 0; i <= TTM_PL_PRIV5; i++)
     80 		if (place->flags & (1 << i)) {
     81 			*mem_type = i;
     82 			return 0;
     83 		}
     84 	return -EINVAL;
     85 }
     86 
     87 static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
     88 {
     89 	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
     90 
     91 	pr_err("    has_type: %d\n", man->has_type);
     92 	pr_err("    use_type: %d\n", man->use_type);
     93 	pr_err("    flags: 0x%08X\n", man->flags);
     94 	pr_err("    gpu_offset: 0x%"PRIX64"\n", man->gpu_offset);
     95 	pr_err("    size: %"PRIu64"\n", man->size);
     96 	pr_err("    available_caching: 0x%08X\n", man->available_caching);
     97 	pr_err("    default_caching: 0x%08X\n", man->default_caching);
     98 	if (mem_type != TTM_PL_SYSTEM)
     99 		(*man->func->debug)(man, TTM_PFX);
    100 }
    101 
    102 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
    103 					struct ttm_placement *placement)
    104 {
    105 	int i, ret, mem_type;
    106 
    107 	pr_err("No space for %p (%lu pages, %luK, %luM)\n",
    108 	       bo, bo->mem.num_pages, bo->mem.size >> 10,
    109 	       bo->mem.size >> 20);
    110 	for (i = 0; i < placement->num_placement; i++) {
    111 		ret = ttm_mem_type_from_place(&placement->placement[i],
    112 						&mem_type);
    113 		if (ret)
    114 			return;
    115 		pr_err("  placement[%d]=0x%08X (%d)\n",
    116 		       i, placement->placement[i].flags, mem_type);
    117 		ttm_mem_type_debug(bo->bdev, mem_type);
    118 	}
    119 }
    120 
    121 #ifndef __NetBSD__		/* XXX sysfs */
    122 static ssize_t ttm_bo_global_show(struct kobject *kobj,
    123 				  struct attribute *attr,
    124 				  char *buffer)
    125 {
    126 	struct ttm_bo_global *glob =
    127 		container_of(kobj, struct ttm_bo_global, kobj);
    128 
    129 	return snprintf(buffer, PAGE_SIZE, "%lu\n",
    130 			(unsigned long) atomic_read(&glob->bo_count));
    131 }
    132 
    133 static struct attribute *ttm_bo_global_attrs[] = {
    134 	&ttm_bo_count,
    135 	NULL
    136 };
    137 
    138 static const struct sysfs_ops ttm_bo_global_ops = {
    139 	.show = &ttm_bo_global_show
    140 };
    141 
    142 static struct kobj_type ttm_bo_glob_kobj_type  = {
    143 	.release = &ttm_bo_global_kobj_release,
    144 	.sysfs_ops = &ttm_bo_global_ops,
    145 	.default_attrs = ttm_bo_global_attrs
    146 };
    147 #endif	/* __NetBSD__ */
    148 
    149 
    150 static inline uint32_t ttm_bo_type_flags(unsigned type)
    151 {
    152 	return 1 << (type);
    153 }
    154 
    155 static void ttm_bo_release_list(struct kref *list_kref)
    156 {
    157 	struct ttm_buffer_object *bo =
    158 	    container_of(list_kref, struct ttm_buffer_object, list_kref);
    159 	struct ttm_bo_device *bdev = bo->bdev;
    160 	size_t acc_size = bo->acc_size;
    161 
    162 	BUG_ON(kref_referenced_p(&bo->list_kref));
    163 	BUG_ON(kref_referenced_p(&bo->kref));
    164 	BUG_ON(atomic_read(&bo->cpu_writers));
    165 	BUG_ON(bo->mem.mm_node != NULL);
    166 	BUG_ON(!list_empty(&bo->lru));
    167 	BUG_ON(!list_empty(&bo->ddestroy));
    168 
    169 	if (bo->ttm)
    170 		ttm_tt_destroy(bo->ttm);
    171 	atomic_dec(&bo->glob->bo_count);
    172 	if (bo->resv == &bo->ttm_resv)
    173 		reservation_object_fini(&bo->ttm_resv);
    174 #ifdef __NetBSD__
    175 	linux_mutex_destroy(&bo->wu_mutex);
    176 #else
    177 	mutex_destroy(&bo->wu_mutex);
    178 #endif
    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 			      ((DRM_HZ / 100) < 1) ? 1 : DRM_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 				      ((DRM_HZ / 100) < 1) ? 1 : DRM_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 				      ((DRM_HZ / 100) < 1) ? 1 : DRM_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 	ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
   1143 	if (ret) {
   1144 		pr_err("Out of kernel memory\n");
   1145 		if (destroy)
   1146 			(*destroy)(bo);
   1147 		else
   1148 			kfree(bo);
   1149 		return -ENOMEM;
   1150 	}
   1151 
   1152 	num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
   1153 	if (num_pages == 0) {
   1154 		pr_err("Illegal buffer object size\n");
   1155 		if (destroy)
   1156 			(*destroy)(bo);
   1157 		else
   1158 			kfree(bo);
   1159 		ttm_mem_global_free(mem_glob, acc_size);
   1160 		return -EINVAL;
   1161 	}
   1162 	bo->destroy = destroy;
   1163 
   1164 	kref_init(&bo->kref);
   1165 	kref_init(&bo->list_kref);
   1166 	atomic_set(&bo->cpu_writers, 0);
   1167 	INIT_LIST_HEAD(&bo->lru);
   1168 	INIT_LIST_HEAD(&bo->ddestroy);
   1169 	INIT_LIST_HEAD(&bo->swap);
   1170 	INIT_LIST_HEAD(&bo->io_reserve_lru);
   1171 #ifdef __NetBSD__
   1172 	linux_mutex_init(&bo->wu_mutex);
   1173 #else
   1174 	mutex_init(&bo->wu_mutex);
   1175 #endif
   1176 	bo->bdev = bdev;
   1177 	bo->glob = bdev->glob;
   1178 	bo->type = type;
   1179 	bo->num_pages = num_pages;
   1180 	bo->mem.size = num_pages << PAGE_SHIFT;
   1181 	bo->mem.mem_type = TTM_PL_SYSTEM;
   1182 	bo->mem.num_pages = bo->num_pages;
   1183 	bo->mem.mm_node = NULL;
   1184 	bo->mem.page_alignment = page_alignment;
   1185 	bo->mem.bus.io_reserved_vm = false;
   1186 	bo->mem.bus.io_reserved_count = 0;
   1187 	bo->priv_flags = 0;
   1188 	bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
   1189 	bo->persistent_swap_storage = persistent_swap_storage;
   1190 	bo->acc_size = acc_size;
   1191 	bo->sg = sg;
   1192 	if (resv) {
   1193 		bo->resv = resv;
   1194 		lockdep_assert_held(&bo->resv->lock.base);
   1195 	} else {
   1196 		bo->resv = &bo->ttm_resv;
   1197 		reservation_object_init(&bo->ttm_resv);
   1198 	}
   1199 	atomic_inc(&bo->glob->bo_count);
   1200 #ifdef __NetBSD__
   1201 	drm_vma_node_init(&bo->vma_node);
   1202 	uvm_obj_init(&bo->uvmobj, bdev->driver->ttm_uvm_ops, true, 1);
   1203 #else
   1204 	drm_vma_node_reset(&bo->vma_node);
   1205 #endif
   1206 
   1207 	/*
   1208 	 * For ttm_bo_type_device buffers, allocate
   1209 	 * address space from the device.
   1210 	 */
   1211 	if (bo->type == ttm_bo_type_device ||
   1212 	    bo->type == ttm_bo_type_sg)
   1213 		ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
   1214 					 bo->mem.num_pages);
   1215 
   1216 	/* passed reservation objects should already be locked,
   1217 	 * since otherwise lockdep will be angered in radeon.
   1218 	 */
   1219 	if (!resv) {
   1220 		locked = ww_mutex_trylock(&bo->resv->lock);
   1221 		WARN_ON(!locked);
   1222 	}
   1223 
   1224 	if (likely(!ret))
   1225 		ret = ttm_bo_validate(bo, placement, interruptible, false);
   1226 
   1227 	if (!resv)
   1228 		ttm_bo_unreserve(bo);
   1229 
   1230 	if (unlikely(ret))
   1231 		ttm_bo_unref(&bo);
   1232 
   1233 	return ret;
   1234 }
   1235 EXPORT_SYMBOL(ttm_bo_init);
   1236 
   1237 size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
   1238 		       unsigned long bo_size,
   1239 		       unsigned struct_size)
   1240 {
   1241 	unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
   1242 	size_t size = 0;
   1243 
   1244 	size += ttm_round_pot(struct_size);
   1245 	size += PAGE_ALIGN(npages * sizeof(void *));
   1246 	size += ttm_round_pot(sizeof(struct ttm_tt));
   1247 	return size;
   1248 }
   1249 EXPORT_SYMBOL(ttm_bo_acc_size);
   1250 
   1251 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
   1252 			   unsigned long bo_size,
   1253 			   unsigned struct_size)
   1254 {
   1255 	unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
   1256 	size_t size = 0;
   1257 
   1258 	size += ttm_round_pot(struct_size);
   1259 	size += PAGE_ALIGN(npages * sizeof(void *));
   1260 	size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
   1261 	size += ttm_round_pot(sizeof(struct ttm_dma_tt));
   1262 	return size;
   1263 }
   1264 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
   1265 
   1266 int ttm_bo_create(struct ttm_bo_device *bdev,
   1267 			unsigned long size,
   1268 			enum ttm_bo_type type,
   1269 			struct ttm_placement *placement,
   1270 			uint32_t page_alignment,
   1271 			bool interruptible,
   1272 			struct file *persistent_swap_storage,
   1273 			struct ttm_buffer_object **p_bo)
   1274 {
   1275 	struct ttm_buffer_object *bo;
   1276 	size_t acc_size;
   1277 	int ret;
   1278 
   1279 	bo = kzalloc(sizeof(*bo), GFP_KERNEL);
   1280 	if (unlikely(bo == NULL))
   1281 		return -ENOMEM;
   1282 
   1283 	acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
   1284 	ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
   1285 			  interruptible, persistent_swap_storage, acc_size,
   1286 			  NULL, NULL, NULL);
   1287 	if (likely(ret == 0))
   1288 		*p_bo = bo;
   1289 
   1290 	return ret;
   1291 }
   1292 EXPORT_SYMBOL(ttm_bo_create);
   1293 
   1294 static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
   1295 					unsigned mem_type, bool allow_errors)
   1296 {
   1297 	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
   1298 	struct ttm_bo_global *glob = bdev->glob;
   1299 	int ret;
   1300 
   1301 	/*
   1302 	 * Can't use standard list traversal since we're unlocking.
   1303 	 */
   1304 
   1305 	spin_lock(&glob->lru_lock);
   1306 	while (!list_empty(&man->lru)) {
   1307 		spin_unlock(&glob->lru_lock);
   1308 		ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
   1309 		if (ret) {
   1310 			if (allow_errors) {
   1311 				return ret;
   1312 			} else {
   1313 				pr_err("Cleanup eviction failed\n");
   1314 			}
   1315 		}
   1316 		spin_lock(&glob->lru_lock);
   1317 	}
   1318 	spin_unlock(&glob->lru_lock);
   1319 	return 0;
   1320 }
   1321 
   1322 int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
   1323 {
   1324 	struct ttm_mem_type_manager *man;
   1325 	int ret = -EINVAL;
   1326 
   1327 	if (mem_type >= TTM_NUM_MEM_TYPES) {
   1328 		pr_err("Illegal memory type %d\n", mem_type);
   1329 		return ret;
   1330 	}
   1331 	man = &bdev->man[mem_type];
   1332 
   1333 	if (!man->has_type) {
   1334 		pr_err("Trying to take down uninitialized memory manager type %u\n",
   1335 		       mem_type);
   1336 		return ret;
   1337 	}
   1338 
   1339 	man->use_type = false;
   1340 	man->has_type = false;
   1341 
   1342 	ret = 0;
   1343 	if (mem_type > 0) {
   1344 		ttm_bo_force_list_clean(bdev, mem_type, false);
   1345 
   1346 		ret = (*man->func->takedown)(man);
   1347 	}
   1348 
   1349 #ifdef __NetBSD__
   1350 	linux_mutex_destroy(&man->io_reserve_mutex);
   1351 #else
   1352 	mutex_destroy(&man->io_reserve_mutex);
   1353 #endif
   1354 
   1355 	return ret;
   1356 }
   1357 EXPORT_SYMBOL(ttm_bo_clean_mm);
   1358 
   1359 int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
   1360 {
   1361 	struct ttm_mem_type_manager *man = &bdev->man[mem_type];
   1362 
   1363 	if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
   1364 		pr_err("Illegal memory manager memory type %u\n", mem_type);
   1365 		return -EINVAL;
   1366 	}
   1367 
   1368 	if (!man->has_type) {
   1369 		pr_err("Memory type %u has not been initialized\n", mem_type);
   1370 		return 0;
   1371 	}
   1372 
   1373 	return ttm_bo_force_list_clean(bdev, mem_type, true);
   1374 }
   1375 EXPORT_SYMBOL(ttm_bo_evict_mm);
   1376 
   1377 int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
   1378 			unsigned long p_size)
   1379 {
   1380 	int ret = -EINVAL;
   1381 	struct ttm_mem_type_manager *man;
   1382 
   1383 	BUG_ON(type >= TTM_NUM_MEM_TYPES);
   1384 	man = &bdev->man[type];
   1385 	BUG_ON(man->has_type);
   1386 	man->io_reserve_fastpath = true;
   1387 	man->use_io_reserve_lru = false;
   1388 #ifdef __NetBSD__
   1389 	linux_mutex_init(&man->io_reserve_mutex);
   1390 #else
   1391 	mutex_init(&man->io_reserve_mutex);
   1392 #endif
   1393 	INIT_LIST_HEAD(&man->io_reserve_lru);
   1394 
   1395 	ret = bdev->driver->init_mem_type(bdev, type, man);
   1396 	if (ret)
   1397 		return ret;
   1398 	man->bdev = bdev;
   1399 
   1400 	ret = 0;
   1401 	if (type != TTM_PL_SYSTEM) {
   1402 		ret = (*man->func->init)(man, p_size);
   1403 		if (ret)
   1404 			return ret;
   1405 	}
   1406 	man->has_type = true;
   1407 	man->use_type = true;
   1408 	man->size = p_size;
   1409 
   1410 	INIT_LIST_HEAD(&man->lru);
   1411 
   1412 	return 0;
   1413 }
   1414 EXPORT_SYMBOL(ttm_bo_init_mm);
   1415 
   1416 #ifndef __NetBSD__
   1417 static void ttm_bo_global_kobj_release(struct kobject *kobj)
   1418 {
   1419 	struct ttm_bo_global *glob =
   1420 		container_of(kobj, struct ttm_bo_global, kobj);
   1421 
   1422 	ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
   1423 	__free_page(glob->dummy_read_page);
   1424 	mutex_destroy(&glob->device_list_mutex);
   1425 	kfree(glob);
   1426 }
   1427 #endif
   1428 
   1429 void ttm_bo_global_release(struct drm_global_reference *ref)
   1430 {
   1431 	struct ttm_bo_global *glob = ref->object;
   1432 
   1433 #ifdef __NetBSD__
   1434 	ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
   1435 	BUG_ON(glob->dummy_read_page != NULL);
   1436 	spin_lock_destroy(&glob->lru_lock);
   1437 	linux_mutex_destroy(&glob->device_list_mutex);
   1438 	kfree(glob);
   1439 #else
   1440 	kobject_del(&glob->kobj);
   1441 	kobject_put(&glob->kobj);
   1442 #endif
   1443 }
   1444 EXPORT_SYMBOL(ttm_bo_global_release);
   1445 
   1446 int ttm_bo_global_init(struct drm_global_reference *ref)
   1447 {
   1448 	struct ttm_bo_global_ref *bo_ref =
   1449 		container_of(ref, struct ttm_bo_global_ref, ref);
   1450 	struct ttm_bo_global *glob = ref->object;
   1451 	int ret;
   1452 
   1453 #ifdef __NetBSD__
   1454 	linux_mutex_init(&glob->device_list_mutex);
   1455 #else
   1456 	mutex_init(&glob->device_list_mutex);
   1457 #endif
   1458 	spin_lock_init(&glob->lru_lock);
   1459 	glob->mem_glob = bo_ref->mem_glob;
   1460 #ifdef __NetBSD__
   1461 	/* Only used by agp back end, will fix there.  */
   1462 	/* XXX Fix agp back end to DTRT.  */
   1463 	glob->dummy_read_page = NULL;
   1464 #else
   1465 	glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
   1466 
   1467 	if (unlikely(glob->dummy_read_page == NULL)) {
   1468 		ret = -ENOMEM;
   1469 		goto out_no_drp;
   1470 	}
   1471 #endif
   1472 
   1473 	INIT_LIST_HEAD(&glob->swap_lru);
   1474 	INIT_LIST_HEAD(&glob->device_list);
   1475 
   1476 	ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
   1477 	ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
   1478 	if (unlikely(ret != 0)) {
   1479 		pr_err("Could not register buffer object swapout\n");
   1480 		goto out_no_shrink;
   1481 	}
   1482 
   1483 	atomic_set(&glob->bo_count, 0);
   1484 
   1485 #ifdef __NetBSD__
   1486 	ret = 0;
   1487 #else
   1488 	ret = kobject_init_and_add(
   1489 		&glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
   1490 	if (unlikely(ret != 0))
   1491 		kobject_put(&glob->kobj);
   1492 #endif
   1493 	return ret;
   1494 out_no_shrink:
   1495 #ifndef __NetBSD__
   1496 	__free_page(glob->dummy_read_page);
   1497 out_no_drp:
   1498 #endif
   1499 	kfree(glob);
   1500 	return ret;
   1501 }
   1502 EXPORT_SYMBOL(ttm_bo_global_init);
   1503 
   1504 
   1505 int ttm_bo_device_release(struct ttm_bo_device *bdev)
   1506 {
   1507 	int ret = 0;
   1508 	unsigned i = TTM_NUM_MEM_TYPES;
   1509 	struct ttm_mem_type_manager *man;
   1510 	struct ttm_bo_global *glob = bdev->glob;
   1511 
   1512 	while (i--) {
   1513 		man = &bdev->man[i];
   1514 		if (man->has_type) {
   1515 			man->use_type = false;
   1516 			if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
   1517 				ret = -EBUSY;
   1518 				pr_err("DRM memory manager type %d is not clean\n",
   1519 				       i);
   1520 			}
   1521 			man->has_type = false;
   1522 		}
   1523 	}
   1524 
   1525 	mutex_lock(&glob->device_list_mutex);
   1526 	list_del(&bdev->device_list);
   1527 	mutex_unlock(&glob->device_list_mutex);
   1528 
   1529 	cancel_delayed_work_sync(&bdev->wq);
   1530 
   1531 	while (ttm_bo_delayed_delete(bdev, true))
   1532 		;
   1533 
   1534 	spin_lock(&glob->lru_lock);
   1535 	if (list_empty(&bdev->ddestroy))
   1536 		TTM_DEBUG("Delayed destroy list was clean\n");
   1537 
   1538 	if (list_empty(&bdev->man[0].lru))
   1539 		TTM_DEBUG("Swap list was clean\n");
   1540 	spin_unlock(&glob->lru_lock);
   1541 
   1542 	drm_vma_offset_manager_destroy(&bdev->vma_manager);
   1543 
   1544 	return ret;
   1545 }
   1546 EXPORT_SYMBOL(ttm_bo_device_release);
   1547 
   1548 int ttm_bo_device_init(struct ttm_bo_device *bdev,
   1549 		       struct ttm_bo_global *glob,
   1550 		       struct ttm_bo_driver *driver,
   1551 #ifdef __NetBSD__
   1552 		       bus_space_tag_t memt,
   1553 		       bus_dma_tag_t dmat,
   1554 #else
   1555 		       struct address_space *mapping,
   1556 #endif
   1557 		       uint64_t file_page_offset,
   1558 		       bool need_dma32)
   1559 {
   1560 	int ret = -EINVAL;
   1561 
   1562 	bdev->driver = driver;
   1563 
   1564 	memset(bdev->man, 0, sizeof(bdev->man));
   1565 
   1566 	/*
   1567 	 * Initialize the system memory buffer type.
   1568 	 * Other types need to be driver / IOCTL initialized.
   1569 	 */
   1570 	ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
   1571 	if (unlikely(ret != 0))
   1572 		goto out_no_sys;
   1573 
   1574 	drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
   1575 				    0x10000000);
   1576 	INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
   1577 	INIT_LIST_HEAD(&bdev->ddestroy);
   1578 #ifdef __NetBSD__
   1579 	bdev->memt = memt;
   1580 	bdev->dmat = dmat;
   1581 #else
   1582 	bdev->dev_mapping = mapping;
   1583 #endif
   1584 	bdev->glob = glob;
   1585 	bdev->need_dma32 = need_dma32;
   1586 	bdev->val_seq = 0;
   1587 	mutex_lock(&glob->device_list_mutex);
   1588 	list_add_tail(&bdev->device_list, &glob->device_list);
   1589 	mutex_unlock(&glob->device_list_mutex);
   1590 
   1591 	return 0;
   1592 out_no_sys:
   1593 	return ret;
   1594 }
   1595 EXPORT_SYMBOL(ttm_bo_device_init);
   1596 
   1597 /*
   1598  * buffer object vm functions.
   1599  */
   1600 
   1601 bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
   1602 {
   1603 	struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
   1604 
   1605 	if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
   1606 		if (mem->mem_type == TTM_PL_SYSTEM)
   1607 			return false;
   1608 
   1609 		if (man->flags & TTM_MEMTYPE_FLAG_CMA)
   1610 			return false;
   1611 
   1612 		if (mem->placement & TTM_PL_FLAG_CACHED)
   1613 			return false;
   1614 	}
   1615 	return true;
   1616 }
   1617 
   1618 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
   1619 {
   1620 #ifndef __NetBSD__
   1621 	struct ttm_bo_device *bdev = bo->bdev;
   1622 #endif
   1623 
   1624 #ifdef __NetBSD__
   1625 	if (bo->mem.bus.is_iomem) {
   1626 		paddr_t start, end, pa;
   1627 
   1628 		KASSERTMSG((bo->mem.bus.base & (PAGE_SIZE - 1)) == 0,
   1629 		    "bo bus base addr not page-aligned: %lx",
   1630 		    bo->mem.bus.base);
   1631 		KASSERTMSG((bo->mem.bus.offset & (PAGE_SIZE - 1)) == 0,
   1632 		    "bo bus offset not page-aligned: %lx",
   1633 		    bo->mem.bus.offset);
   1634 		start = bo->mem.bus.base + bo->mem.bus.offset;
   1635 		KASSERT((bo->mem.bus.size & (PAGE_SIZE - 1)) == 0);
   1636 		end = start + bo->mem.bus.size;
   1637 
   1638 		for (pa = start; pa < end; pa += PAGE_SIZE)
   1639 			pmap_pv_protect(pa, VM_PROT_NONE);
   1640 	} else if (bo->ttm != NULL) {
   1641 		unsigned i;
   1642 
   1643 		mutex_enter(bo->uvmobj.vmobjlock);
   1644 		for (i = 0; i < bo->ttm->num_pages; i++)
   1645 			pmap_page_protect(&bo->ttm->pages[i]->p_vmp,
   1646 			    VM_PROT_NONE);
   1647 		mutex_exit(bo->uvmobj.vmobjlock);
   1648 	}
   1649 #else
   1650 	drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
   1651 #endif
   1652 	ttm_mem_io_free_vm(bo);
   1653 }
   1654 
   1655 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
   1656 {
   1657 	struct ttm_bo_device *bdev = bo->bdev;
   1658 	struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
   1659 
   1660 	ttm_mem_io_lock(man, false);
   1661 	ttm_bo_unmap_virtual_locked(bo);
   1662 	ttm_mem_io_unlock(man);
   1663 }
   1664 
   1665 
   1666 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
   1667 
   1668 int ttm_bo_wait(struct ttm_buffer_object *bo,
   1669 		bool lazy, bool interruptible, bool no_wait)
   1670 {
   1671 	struct reservation_object_list *fobj;
   1672 	struct reservation_object *resv;
   1673 	struct fence *excl;
   1674 	long timeout = 15 * HZ;
   1675 	int i;
   1676 
   1677 	resv = bo->resv;
   1678 	fobj = reservation_object_get_list(resv);
   1679 	excl = reservation_object_get_excl(resv);
   1680 	if (excl) {
   1681 		if (!fence_is_signaled(excl)) {
   1682 			if (no_wait)
   1683 				return -EBUSY;
   1684 
   1685 			timeout = fence_wait_timeout(excl,
   1686 						     interruptible, timeout);
   1687 		}
   1688 	}
   1689 
   1690 	for (i = 0; fobj && timeout > 0 && i < fobj->shared_count; ++i) {
   1691 		struct fence *fence;
   1692 		fence = rcu_dereference_protected(fobj->shared[i],
   1693 						reservation_object_held(resv));
   1694 
   1695 		if (!fence_is_signaled(fence)) {
   1696 			if (no_wait)
   1697 				return -EBUSY;
   1698 
   1699 			timeout = fence_wait_timeout(fence,
   1700 						     interruptible, timeout);
   1701 		}
   1702 	}
   1703 
   1704 	if (timeout < 0)
   1705 		return timeout;
   1706 
   1707 	if (timeout == 0)
   1708 		return -EBUSY;
   1709 
   1710 	reservation_object_add_excl_fence(resv, NULL);
   1711 	clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
   1712 	return 0;
   1713 }
   1714 EXPORT_SYMBOL(ttm_bo_wait);
   1715 
   1716 int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
   1717 {
   1718 	int ret = 0;
   1719 
   1720 	/*
   1721 	 * Using ttm_bo_reserve makes sure the lru lists are updated.
   1722 	 */
   1723 
   1724 	ret = ttm_bo_reserve(bo, true, no_wait, false, NULL);
   1725 	if (unlikely(ret != 0))
   1726 		return ret;
   1727 	ret = ttm_bo_wait(bo, false, true, no_wait);
   1728 	if (likely(ret == 0))
   1729 		atomic_inc(&bo->cpu_writers);
   1730 	ttm_bo_unreserve(bo);
   1731 	return ret;
   1732 }
   1733 EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
   1734 
   1735 void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
   1736 {
   1737 	atomic_dec(&bo->cpu_writers);
   1738 }
   1739 EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
   1740 
   1741 /**
   1742  * A buffer object shrink method that tries to swap out the first
   1743  * buffer object on the bo_global::swap_lru list.
   1744  */
   1745 
   1746 static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
   1747 {
   1748 	struct ttm_bo_global *glob =
   1749 	    container_of(shrink, struct ttm_bo_global, shrink);
   1750 	struct ttm_buffer_object *bo;
   1751 	int ret = -EBUSY;
   1752 	int put_count;
   1753 
   1754 	spin_lock(&glob->lru_lock);
   1755 	list_for_each_entry(bo, &glob->swap_lru, swap) {
   1756 		ret = __ttm_bo_reserve(bo, false, true, false, NULL);
   1757 		if (!ret)
   1758 			break;
   1759 	}
   1760 
   1761 	if (ret) {
   1762 		spin_unlock(&glob->lru_lock);
   1763 		return ret;
   1764 	}
   1765 
   1766 	kref_get(&bo->list_kref);
   1767 
   1768 	if (!list_empty(&bo->ddestroy)) {
   1769 		ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
   1770 		kref_put(&bo->list_kref, ttm_bo_release_list);
   1771 		return ret;
   1772 	}
   1773 
   1774 	put_count = ttm_bo_del_from_lru(bo);
   1775 	spin_unlock(&glob->lru_lock);
   1776 
   1777 	ttm_bo_list_ref_sub(bo, put_count, true);
   1778 
   1779 	/**
   1780 	 * Wait for GPU, then move to system cached.
   1781 	 */
   1782 
   1783 	ret = ttm_bo_wait(bo, false, false, false);
   1784 
   1785 	if (unlikely(ret != 0))
   1786 		goto out;
   1787 
   1788 	if (bo->mem.mem_type != TTM_PL_SYSTEM ||
   1789 	    bo->ttm->caching_state != tt_cached) {
   1790 		struct ttm_mem_reg evict_mem;
   1791 
   1792 		evict_mem = bo->mem;
   1793 		evict_mem.mm_node = NULL;
   1794 		evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
   1795 		evict_mem.mem_type = TTM_PL_SYSTEM;
   1796 
   1797 		ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
   1798 					     false, false);
   1799 		if (unlikely(ret != 0))
   1800 			goto out;
   1801 	}
   1802 
   1803 	ttm_bo_unmap_virtual(bo);
   1804 
   1805 	/**
   1806 	 * Swap out. Buffer will be swapped in again as soon as
   1807 	 * anyone tries to access a ttm page.
   1808 	 */
   1809 
   1810 	if (bo->bdev->driver->swap_notify)
   1811 		bo->bdev->driver->swap_notify(bo);
   1812 
   1813 	ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
   1814 out:
   1815 
   1816 	/**
   1817 	 *
   1818 	 * Unreserve without putting on LRU to avoid swapping out an
   1819 	 * already swapped buffer.
   1820 	 */
   1821 
   1822 	__ttm_bo_unreserve(bo);
   1823 	kref_put(&bo->list_kref, ttm_bo_release_list);
   1824 	return ret;
   1825 }
   1826 
   1827 void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
   1828 {
   1829 	while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
   1830 		;
   1831 }
   1832 EXPORT_SYMBOL(ttm_bo_swapout_all);
   1833 
   1834 /**
   1835  * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
   1836  * unreserved
   1837  *
   1838  * @bo: Pointer to buffer
   1839  */
   1840 int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
   1841 {
   1842 	int ret;
   1843 
   1844 	/*
   1845 	 * In the absense of a wait_unlocked API,
   1846 	 * Use the bo::wu_mutex to avoid triggering livelocks due to
   1847 	 * concurrent use of this function. Note that this use of
   1848 	 * bo::wu_mutex can go away if we change locking order to
   1849 	 * mmap_sem -> bo::reserve.
   1850 	 */
   1851 	ret = mutex_lock_interruptible(&bo->wu_mutex);
   1852 	if (unlikely(ret != 0))
   1853 		return -ERESTARTSYS;
   1854 	if (!ww_mutex_is_locked(&bo->resv->lock))
   1855 		goto out_unlock;
   1856 	ret = __ttm_bo_reserve(bo, true, false, false, NULL);
   1857 	if (unlikely(ret != 0))
   1858 		goto out_unlock;
   1859 	__ttm_bo_unreserve(bo);
   1860 
   1861 out_unlock:
   1862 	mutex_unlock(&bo->wu_mutex);
   1863 	return ret;
   1864 }
   1865