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