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