xref: /openbmc/linux/drivers/gpu/drm/ttm/ttm_bo.c (revision e8263235)
1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
3  *
4  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sub license, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial portions
17  * of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25  * USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  **************************************************************************/
28 /*
29  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
30  */
31 
32 #define pr_fmt(fmt) "[TTM] " fmt
33 
34 #include <drm/ttm/ttm_module.h>
35 #include <drm/ttm/ttm_bo_driver.h>
36 #include <drm/ttm/ttm_placement.h>
37 #include <linux/jiffies.h>
38 #include <linux/slab.h>
39 #include <linux/sched.h>
40 #include <linux/mm.h>
41 #include <linux/file.h>
42 #include <linux/module.h>
43 #include <linux/atomic.h>
44 #include <linux/dma-resv.h>
45 
46 static void ttm_bo_global_kobj_release(struct kobject *kobj);
47 
48 /*
49  * ttm_global_mutex - protecting the global BO state
50  */
51 DEFINE_MUTEX(ttm_global_mutex);
52 unsigned ttm_bo_glob_use_count;
53 struct ttm_bo_global ttm_bo_glob;
54 EXPORT_SYMBOL(ttm_bo_glob);
55 
56 static struct attribute ttm_bo_count = {
57 	.name = "bo_count",
58 	.mode = S_IRUGO
59 };
60 
61 /* default destructor */
62 static void ttm_bo_default_destroy(struct ttm_buffer_object *bo)
63 {
64 	kfree(bo);
65 }
66 
67 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
68 					struct ttm_placement *placement)
69 {
70 	struct drm_printer p = drm_debug_printer(TTM_PFX);
71 	struct ttm_resource_manager *man;
72 	int i, mem_type;
73 
74 	drm_printf(&p, "No space for %p (%lu pages, %luK, %luM)\n",
75 		   bo, bo->mem.num_pages, bo->mem.size >> 10,
76 		   bo->mem.size >> 20);
77 	for (i = 0; i < placement->num_placement; i++) {
78 		mem_type = placement->placement[i].mem_type;
79 		drm_printf(&p, "  placement[%d]=0x%08X (%d)\n",
80 			   i, placement->placement[i].flags, mem_type);
81 		man = ttm_manager_type(bo->bdev, mem_type);
82 		ttm_resource_manager_debug(man, &p);
83 	}
84 }
85 
86 static ssize_t ttm_bo_global_show(struct kobject *kobj,
87 				  struct attribute *attr,
88 				  char *buffer)
89 {
90 	struct ttm_bo_global *glob =
91 		container_of(kobj, struct ttm_bo_global, kobj);
92 
93 	return snprintf(buffer, PAGE_SIZE, "%d\n",
94 				atomic_read(&glob->bo_count));
95 }
96 
97 static struct attribute *ttm_bo_global_attrs[] = {
98 	&ttm_bo_count,
99 	NULL
100 };
101 
102 static const struct sysfs_ops ttm_bo_global_ops = {
103 	.show = &ttm_bo_global_show
104 };
105 
106 static struct kobj_type ttm_bo_glob_kobj_type  = {
107 	.release = &ttm_bo_global_kobj_release,
108 	.sysfs_ops = &ttm_bo_global_ops,
109 	.default_attrs = ttm_bo_global_attrs
110 };
111 
112 static void ttm_bo_add_mem_to_lru(struct ttm_buffer_object *bo,
113 				  struct ttm_resource *mem)
114 {
115 	struct ttm_bo_device *bdev = bo->bdev;
116 	struct ttm_resource_manager *man;
117 
118 	if (!list_empty(&bo->lru) || bo->pin_count)
119 		return;
120 
121 	man = ttm_manager_type(bdev, mem->mem_type);
122 	list_add_tail(&bo->lru, &man->lru[bo->priority]);
123 
124 	if (man->use_tt && bo->ttm &&
125 	    !(bo->ttm->page_flags & (TTM_PAGE_FLAG_SG |
126 				     TTM_PAGE_FLAG_SWAPPED))) {
127 		list_add_tail(&bo->swap, &ttm_bo_glob.swap_lru[bo->priority]);
128 	}
129 }
130 
131 static void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
132 {
133 	struct ttm_bo_device *bdev = bo->bdev;
134 	bool notify = false;
135 
136 	if (!list_empty(&bo->swap)) {
137 		list_del_init(&bo->swap);
138 		notify = true;
139 	}
140 	if (!list_empty(&bo->lru)) {
141 		list_del_init(&bo->lru);
142 		notify = true;
143 	}
144 
145 	if (notify && bdev->driver->del_from_lru_notify)
146 		bdev->driver->del_from_lru_notify(bo);
147 }
148 
149 static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
150 				     struct ttm_buffer_object *bo)
151 {
152 	if (!pos->first)
153 		pos->first = bo;
154 	pos->last = bo;
155 }
156 
157 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
158 			     struct ttm_lru_bulk_move *bulk)
159 {
160 	dma_resv_assert_held(bo->base.resv);
161 
162 	ttm_bo_del_from_lru(bo);
163 	ttm_bo_add_mem_to_lru(bo, &bo->mem);
164 
165 	if (bulk && !bo->pin_count) {
166 		switch (bo->mem.mem_type) {
167 		case TTM_PL_TT:
168 			ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
169 			break;
170 
171 		case TTM_PL_VRAM:
172 			ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
173 			break;
174 		}
175 		if (bo->ttm && !(bo->ttm->page_flags &
176 				 (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED)))
177 			ttm_bo_bulk_move_set_pos(&bulk->swap[bo->priority], bo);
178 	}
179 }
180 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
181 
182 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
183 {
184 	unsigned i;
185 
186 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
187 		struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
188 		struct ttm_resource_manager *man;
189 
190 		if (!pos->first)
191 			continue;
192 
193 		dma_resv_assert_held(pos->first->base.resv);
194 		dma_resv_assert_held(pos->last->base.resv);
195 
196 		man = ttm_manager_type(pos->first->bdev, TTM_PL_TT);
197 		list_bulk_move_tail(&man->lru[i], &pos->first->lru,
198 				    &pos->last->lru);
199 	}
200 
201 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
202 		struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
203 		struct ttm_resource_manager *man;
204 
205 		if (!pos->first)
206 			continue;
207 
208 		dma_resv_assert_held(pos->first->base.resv);
209 		dma_resv_assert_held(pos->last->base.resv);
210 
211 		man = ttm_manager_type(pos->first->bdev, TTM_PL_VRAM);
212 		list_bulk_move_tail(&man->lru[i], &pos->first->lru,
213 				    &pos->last->lru);
214 	}
215 
216 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
217 		struct ttm_lru_bulk_move_pos *pos = &bulk->swap[i];
218 		struct list_head *lru;
219 
220 		if (!pos->first)
221 			continue;
222 
223 		dma_resv_assert_held(pos->first->base.resv);
224 		dma_resv_assert_held(pos->last->base.resv);
225 
226 		lru = &ttm_bo_glob.swap_lru[i];
227 		list_bulk_move_tail(lru, &pos->first->swap, &pos->last->swap);
228 	}
229 }
230 EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
231 
232 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
233 				  struct ttm_resource *mem, bool evict,
234 				  struct ttm_operation_ctx *ctx,
235 				  struct ttm_place *hop)
236 {
237 	struct ttm_bo_device *bdev = bo->bdev;
238 	struct ttm_resource_manager *old_man = ttm_manager_type(bdev, bo->mem.mem_type);
239 	struct ttm_resource_manager *new_man = ttm_manager_type(bdev, mem->mem_type);
240 	int ret;
241 
242 	ttm_bo_unmap_virtual(bo);
243 
244 	/*
245 	 * Create and bind a ttm if required.
246 	 */
247 
248 	if (new_man->use_tt) {
249 		/* Zero init the new TTM structure if the old location should
250 		 * have used one as well.
251 		 */
252 		ret = ttm_tt_create(bo, old_man->use_tt);
253 		if (ret)
254 			goto out_err;
255 
256 		if (mem->mem_type != TTM_PL_SYSTEM) {
257 			ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
258 			if (ret)
259 				goto out_err;
260 		}
261 	}
262 
263 	ret = bdev->driver->move(bo, evict, ctx, mem, hop);
264 	if (ret) {
265 		if (ret == -EMULTIHOP)
266 			return ret;
267 		goto out_err;
268 	}
269 
270 	ctx->bytes_moved += bo->num_pages << PAGE_SHIFT;
271 	return 0;
272 
273 out_err:
274 	new_man = ttm_manager_type(bdev, bo->mem.mem_type);
275 	if (!new_man->use_tt)
276 		ttm_bo_tt_destroy(bo);
277 
278 	return ret;
279 }
280 
281 /*
282  * Call bo::reserved.
283  * Will release GPU memory type usage on destruction.
284  * This is the place to put in driver specific hooks to release
285  * driver private resources.
286  * Will release the bo::reserved lock.
287  */
288 
289 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
290 {
291 	if (bo->bdev->driver->delete_mem_notify)
292 		bo->bdev->driver->delete_mem_notify(bo);
293 
294 	ttm_bo_tt_destroy(bo);
295 	ttm_resource_free(bo, &bo->mem);
296 }
297 
298 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
299 {
300 	int r;
301 
302 	if (bo->base.resv == &bo->base._resv)
303 		return 0;
304 
305 	BUG_ON(!dma_resv_trylock(&bo->base._resv));
306 
307 	r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
308 	dma_resv_unlock(&bo->base._resv);
309 	if (r)
310 		return r;
311 
312 	if (bo->type != ttm_bo_type_sg) {
313 		/* This works because the BO is about to be destroyed and nobody
314 		 * reference it any more. The only tricky case is the trylock on
315 		 * the resv object while holding the lru_lock.
316 		 */
317 		spin_lock(&ttm_bo_glob.lru_lock);
318 		bo->base.resv = &bo->base._resv;
319 		spin_unlock(&ttm_bo_glob.lru_lock);
320 	}
321 
322 	return r;
323 }
324 
325 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
326 {
327 	struct dma_resv *resv = &bo->base._resv;
328 	struct dma_resv_list *fobj;
329 	struct dma_fence *fence;
330 	int i;
331 
332 	rcu_read_lock();
333 	fobj = rcu_dereference(resv->fence);
334 	fence = rcu_dereference(resv->fence_excl);
335 	if (fence && !fence->ops->signaled)
336 		dma_fence_enable_sw_signaling(fence);
337 
338 	for (i = 0; fobj && i < fobj->shared_count; ++i) {
339 		fence = rcu_dereference(fobj->shared[i]);
340 
341 		if (!fence->ops->signaled)
342 			dma_fence_enable_sw_signaling(fence);
343 	}
344 	rcu_read_unlock();
345 }
346 
347 /**
348  * function ttm_bo_cleanup_refs
349  * If bo idle, remove from lru lists, and unref.
350  * If not idle, block if possible.
351  *
352  * Must be called with lru_lock and reservation held, this function
353  * will drop the lru lock and optionally the reservation lock before returning.
354  *
355  * @bo:                    The buffer object to clean-up
356  * @interruptible:         Any sleeps should occur interruptibly.
357  * @no_wait_gpu:           Never wait for gpu. Return -EBUSY instead.
358  * @unlock_resv:           Unlock the reservation lock as well.
359  */
360 
361 static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
362 			       bool interruptible, bool no_wait_gpu,
363 			       bool unlock_resv)
364 {
365 	struct dma_resv *resv = &bo->base._resv;
366 	int ret;
367 
368 	if (dma_resv_test_signaled_rcu(resv, true))
369 		ret = 0;
370 	else
371 		ret = -EBUSY;
372 
373 	if (ret && !no_wait_gpu) {
374 		long lret;
375 
376 		if (unlock_resv)
377 			dma_resv_unlock(bo->base.resv);
378 		spin_unlock(&ttm_bo_glob.lru_lock);
379 
380 		lret = dma_resv_wait_timeout_rcu(resv, true, interruptible,
381 						 30 * HZ);
382 
383 		if (lret < 0)
384 			return lret;
385 		else if (lret == 0)
386 			return -EBUSY;
387 
388 		spin_lock(&ttm_bo_glob.lru_lock);
389 		if (unlock_resv && !dma_resv_trylock(bo->base.resv)) {
390 			/*
391 			 * We raced, and lost, someone else holds the reservation now,
392 			 * and is probably busy in ttm_bo_cleanup_memtype_use.
393 			 *
394 			 * Even if it's not the case, because we finished waiting any
395 			 * delayed destruction would succeed, so just return success
396 			 * here.
397 			 */
398 			spin_unlock(&ttm_bo_glob.lru_lock);
399 			return 0;
400 		}
401 		ret = 0;
402 	}
403 
404 	if (ret || unlikely(list_empty(&bo->ddestroy))) {
405 		if (unlock_resv)
406 			dma_resv_unlock(bo->base.resv);
407 		spin_unlock(&ttm_bo_glob.lru_lock);
408 		return ret;
409 	}
410 
411 	ttm_bo_del_from_lru(bo);
412 	list_del_init(&bo->ddestroy);
413 	spin_unlock(&ttm_bo_glob.lru_lock);
414 	ttm_bo_cleanup_memtype_use(bo);
415 
416 	if (unlock_resv)
417 		dma_resv_unlock(bo->base.resv);
418 
419 	ttm_bo_put(bo);
420 
421 	return 0;
422 }
423 
424 /*
425  * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
426  * encountered buffers.
427  */
428 static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
429 {
430 	struct ttm_bo_global *glob = &ttm_bo_glob;
431 	struct list_head removed;
432 	bool empty;
433 
434 	INIT_LIST_HEAD(&removed);
435 
436 	spin_lock(&glob->lru_lock);
437 	while (!list_empty(&bdev->ddestroy)) {
438 		struct ttm_buffer_object *bo;
439 
440 		bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
441 				      ddestroy);
442 		list_move_tail(&bo->ddestroy, &removed);
443 		if (!ttm_bo_get_unless_zero(bo))
444 			continue;
445 
446 		if (remove_all || bo->base.resv != &bo->base._resv) {
447 			spin_unlock(&glob->lru_lock);
448 			dma_resv_lock(bo->base.resv, NULL);
449 
450 			spin_lock(&glob->lru_lock);
451 			ttm_bo_cleanup_refs(bo, false, !remove_all, true);
452 
453 		} else if (dma_resv_trylock(bo->base.resv)) {
454 			ttm_bo_cleanup_refs(bo, false, !remove_all, true);
455 		} else {
456 			spin_unlock(&glob->lru_lock);
457 		}
458 
459 		ttm_bo_put(bo);
460 		spin_lock(&glob->lru_lock);
461 	}
462 	list_splice_tail(&removed, &bdev->ddestroy);
463 	empty = list_empty(&bdev->ddestroy);
464 	spin_unlock(&glob->lru_lock);
465 
466 	return empty;
467 }
468 
469 static void ttm_bo_delayed_workqueue(struct work_struct *work)
470 {
471 	struct ttm_bo_device *bdev =
472 	    container_of(work, struct ttm_bo_device, wq.work);
473 
474 	if (!ttm_bo_delayed_delete(bdev, false))
475 		schedule_delayed_work(&bdev->wq,
476 				      ((HZ / 100) < 1) ? 1 : HZ / 100);
477 }
478 
479 static void ttm_bo_release(struct kref *kref)
480 {
481 	struct ttm_buffer_object *bo =
482 	    container_of(kref, struct ttm_buffer_object, kref);
483 	struct ttm_bo_device *bdev = bo->bdev;
484 	size_t acc_size = bo->acc_size;
485 	int ret;
486 
487 	if (!bo->deleted) {
488 		ret = ttm_bo_individualize_resv(bo);
489 		if (ret) {
490 			/* Last resort, if we fail to allocate memory for the
491 			 * fences block for the BO to become idle
492 			 */
493 			dma_resv_wait_timeout_rcu(bo->base.resv, true, false,
494 						  30 * HZ);
495 		}
496 
497 		if (bo->bdev->driver->release_notify)
498 			bo->bdev->driver->release_notify(bo);
499 
500 		drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
501 		ttm_mem_io_free(bdev, &bo->mem);
502 	}
503 
504 	if (!dma_resv_test_signaled_rcu(bo->base.resv, true) ||
505 	    !dma_resv_trylock(bo->base.resv)) {
506 		/* The BO is not idle, resurrect it for delayed destroy */
507 		ttm_bo_flush_all_fences(bo);
508 		bo->deleted = true;
509 
510 		spin_lock(&ttm_bo_glob.lru_lock);
511 
512 		/*
513 		 * Make pinned bos immediately available to
514 		 * shrinkers, now that they are queued for
515 		 * destruction.
516 		 */
517 		if (bo->pin_count) {
518 			bo->pin_count = 0;
519 			ttm_bo_del_from_lru(bo);
520 			ttm_bo_add_mem_to_lru(bo, &bo->mem);
521 		}
522 
523 		kref_init(&bo->kref);
524 		list_add_tail(&bo->ddestroy, &bdev->ddestroy);
525 		spin_unlock(&ttm_bo_glob.lru_lock);
526 
527 		schedule_delayed_work(&bdev->wq,
528 				      ((HZ / 100) < 1) ? 1 : HZ / 100);
529 		return;
530 	}
531 
532 	spin_lock(&ttm_bo_glob.lru_lock);
533 	ttm_bo_del_from_lru(bo);
534 	list_del(&bo->ddestroy);
535 	spin_unlock(&ttm_bo_glob.lru_lock);
536 
537 	ttm_bo_cleanup_memtype_use(bo);
538 	dma_resv_unlock(bo->base.resv);
539 
540 	atomic_dec(&ttm_bo_glob.bo_count);
541 	dma_fence_put(bo->moving);
542 	if (!ttm_bo_uses_embedded_gem_object(bo))
543 		dma_resv_fini(&bo->base._resv);
544 	bo->destroy(bo);
545 	ttm_mem_global_free(&ttm_mem_glob, acc_size);
546 }
547 
548 void ttm_bo_put(struct ttm_buffer_object *bo)
549 {
550 	kref_put(&bo->kref, ttm_bo_release);
551 }
552 EXPORT_SYMBOL(ttm_bo_put);
553 
554 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
555 {
556 	return cancel_delayed_work_sync(&bdev->wq);
557 }
558 EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
559 
560 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
561 {
562 	if (resched)
563 		schedule_delayed_work(&bdev->wq,
564 				      ((HZ / 100) < 1) ? 1 : HZ / 100);
565 }
566 EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
567 
568 static int ttm_bo_evict(struct ttm_buffer_object *bo,
569 			struct ttm_operation_ctx *ctx)
570 {
571 	struct ttm_bo_device *bdev = bo->bdev;
572 	struct ttm_resource evict_mem;
573 	struct ttm_placement placement;
574 	struct ttm_place hop;
575 	int ret = 0;
576 
577 	memset(&hop, 0, sizeof(hop));
578 
579 	dma_resv_assert_held(bo->base.resv);
580 
581 	placement.num_placement = 0;
582 	placement.num_busy_placement = 0;
583 	bdev->driver->evict_flags(bo, &placement);
584 
585 	if (!placement.num_placement && !placement.num_busy_placement) {
586 		ttm_bo_wait(bo, false, false);
587 
588 		ttm_bo_cleanup_memtype_use(bo);
589 		return ttm_tt_create(bo, false);
590 	}
591 
592 	evict_mem = bo->mem;
593 	evict_mem.mm_node = NULL;
594 	evict_mem.bus.offset = 0;
595 	evict_mem.bus.addr = NULL;
596 
597 	ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
598 	if (ret) {
599 		if (ret != -ERESTARTSYS) {
600 			pr_err("Failed to find memory space for buffer 0x%p eviction\n",
601 			       bo);
602 			ttm_bo_mem_space_debug(bo, &placement);
603 		}
604 		goto out;
605 	}
606 
607 	ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx, &hop);
608 	if (unlikely(ret)) {
609 		WARN(ret == -EMULTIHOP, "Unexpected multihop in eviction - likely driver bug\n");
610 		if (ret != -ERESTARTSYS)
611 			pr_err("Buffer eviction failed\n");
612 		ttm_resource_free(bo, &evict_mem);
613 	}
614 out:
615 	return ret;
616 }
617 
618 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
619 			      const struct ttm_place *place)
620 {
621 	/* Don't evict this BO if it's outside of the
622 	 * requested placement range
623 	 */
624 	if (place->fpfn >= (bo->mem.start + bo->mem.num_pages) ||
625 	    (place->lpfn && place->lpfn <= bo->mem.start))
626 		return false;
627 
628 	return true;
629 }
630 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
631 
632 /*
633  * Check the target bo is allowable to be evicted or swapout, including cases:
634  *
635  * a. if share same reservation object with ctx->resv, have assumption
636  * reservation objects should already be locked, so not lock again and
637  * return true directly when either the opreation allow_reserved_eviction
638  * or the target bo already is in delayed free list;
639  *
640  * b. Otherwise, trylock it.
641  */
642 static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
643 			struct ttm_operation_ctx *ctx, bool *locked, bool *busy)
644 {
645 	bool ret = false;
646 
647 	if (bo->base.resv == ctx->resv) {
648 		dma_resv_assert_held(bo->base.resv);
649 		if (ctx->allow_res_evict)
650 			ret = true;
651 		*locked = false;
652 		if (busy)
653 			*busy = false;
654 	} else {
655 		ret = dma_resv_trylock(bo->base.resv);
656 		*locked = ret;
657 		if (busy)
658 			*busy = !ret;
659 	}
660 
661 	return ret;
662 }
663 
664 /**
665  * ttm_mem_evict_wait_busy - wait for a busy BO to become available
666  *
667  * @busy_bo: BO which couldn't be locked with trylock
668  * @ctx: operation context
669  * @ticket: acquire ticket
670  *
671  * Try to lock a busy buffer object to avoid failing eviction.
672  */
673 static int ttm_mem_evict_wait_busy(struct ttm_buffer_object *busy_bo,
674 				   struct ttm_operation_ctx *ctx,
675 				   struct ww_acquire_ctx *ticket)
676 {
677 	int r;
678 
679 	if (!busy_bo || !ticket)
680 		return -EBUSY;
681 
682 	if (ctx->interruptible)
683 		r = dma_resv_lock_interruptible(busy_bo->base.resv,
684 							  ticket);
685 	else
686 		r = dma_resv_lock(busy_bo->base.resv, ticket);
687 
688 	/*
689 	 * TODO: It would be better to keep the BO locked until allocation is at
690 	 * least tried one more time, but that would mean a much larger rework
691 	 * of TTM.
692 	 */
693 	if (!r)
694 		dma_resv_unlock(busy_bo->base.resv);
695 
696 	return r == -EDEADLK ? -EBUSY : r;
697 }
698 
699 int ttm_mem_evict_first(struct ttm_bo_device *bdev,
700 			struct ttm_resource_manager *man,
701 			const struct ttm_place *place,
702 			struct ttm_operation_ctx *ctx,
703 			struct ww_acquire_ctx *ticket)
704 {
705 	struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
706 	bool locked = false;
707 	unsigned i;
708 	int ret;
709 
710 	spin_lock(&ttm_bo_glob.lru_lock);
711 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
712 		list_for_each_entry(bo, &man->lru[i], lru) {
713 			bool busy;
714 
715 			if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
716 							    &busy)) {
717 				if (busy && !busy_bo && ticket !=
718 				    dma_resv_locking_ctx(bo->base.resv))
719 					busy_bo = bo;
720 				continue;
721 			}
722 
723 			if (place && !bdev->driver->eviction_valuable(bo,
724 								      place)) {
725 				if (locked)
726 					dma_resv_unlock(bo->base.resv);
727 				continue;
728 			}
729 			if (!ttm_bo_get_unless_zero(bo)) {
730 				if (locked)
731 					dma_resv_unlock(bo->base.resv);
732 				continue;
733 			}
734 			break;
735 		}
736 
737 		/* If the inner loop terminated early, we have our candidate */
738 		if (&bo->lru != &man->lru[i])
739 			break;
740 
741 		bo = NULL;
742 	}
743 
744 	if (!bo) {
745 		if (busy_bo && !ttm_bo_get_unless_zero(busy_bo))
746 			busy_bo = NULL;
747 		spin_unlock(&ttm_bo_glob.lru_lock);
748 		ret = ttm_mem_evict_wait_busy(busy_bo, ctx, ticket);
749 		if (busy_bo)
750 			ttm_bo_put(busy_bo);
751 		return ret;
752 	}
753 
754 	if (bo->deleted) {
755 		ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
756 					  ctx->no_wait_gpu, locked);
757 		ttm_bo_put(bo);
758 		return ret;
759 	}
760 
761 	spin_unlock(&ttm_bo_glob.lru_lock);
762 
763 	ret = ttm_bo_evict(bo, ctx);
764 	if (locked)
765 		ttm_bo_unreserve(bo);
766 
767 	ttm_bo_put(bo);
768 	return ret;
769 }
770 
771 /*
772  * Add the last move fence to the BO and reserve a new shared slot.
773  */
774 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
775 				 struct ttm_resource_manager *man,
776 				 struct ttm_resource *mem,
777 				 bool no_wait_gpu)
778 {
779 	struct dma_fence *fence;
780 	int ret;
781 
782 	spin_lock(&man->move_lock);
783 	fence = dma_fence_get(man->move);
784 	spin_unlock(&man->move_lock);
785 
786 	if (!fence)
787 		return 0;
788 
789 	if (no_wait_gpu) {
790 		dma_fence_put(fence);
791 		return -EBUSY;
792 	}
793 
794 	dma_resv_add_shared_fence(bo->base.resv, fence);
795 
796 	ret = dma_resv_reserve_shared(bo->base.resv, 1);
797 	if (unlikely(ret)) {
798 		dma_fence_put(fence);
799 		return ret;
800 	}
801 
802 	dma_fence_put(bo->moving);
803 	bo->moving = fence;
804 	return 0;
805 }
806 
807 /*
808  * Repeatedly evict memory from the LRU for @mem_type until we create enough
809  * space, or we've evicted everything and there isn't enough space.
810  */
811 static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
812 				  const struct ttm_place *place,
813 				  struct ttm_resource *mem,
814 				  struct ttm_operation_ctx *ctx)
815 {
816 	struct ttm_bo_device *bdev = bo->bdev;
817 	struct ttm_resource_manager *man = ttm_manager_type(bdev, mem->mem_type);
818 	struct ww_acquire_ctx *ticket;
819 	int ret;
820 
821 	ticket = dma_resv_locking_ctx(bo->base.resv);
822 	do {
823 		ret = ttm_resource_alloc(bo, place, mem);
824 		if (likely(!ret))
825 			break;
826 		if (unlikely(ret != -ENOSPC))
827 			return ret;
828 		ret = ttm_mem_evict_first(bdev, man, place, ctx,
829 					  ticket);
830 		if (unlikely(ret != 0))
831 			return ret;
832 	} while (1);
833 
834 	return ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
835 }
836 
837 /**
838  * ttm_bo_mem_placement - check if placement is compatible
839  * @bo: BO to find memory for
840  * @place: where to search
841  * @mem: the memory object to fill in
842  *
843  * Check if placement is compatible and fill in mem structure.
844  * Returns -EBUSY if placement won't work or negative error code.
845  * 0 when placement can be used.
846  */
847 static int ttm_bo_mem_placement(struct ttm_buffer_object *bo,
848 				const struct ttm_place *place,
849 				struct ttm_resource *mem)
850 {
851 	struct ttm_bo_device *bdev = bo->bdev;
852 	struct ttm_resource_manager *man;
853 
854 	man = ttm_manager_type(bdev, place->mem_type);
855 	if (!man || !ttm_resource_manager_used(man))
856 		return -EBUSY;
857 
858 	mem->mem_type = place->mem_type;
859 	mem->placement = place->flags;
860 
861 	spin_lock(&ttm_bo_glob.lru_lock);
862 	ttm_bo_del_from_lru(bo);
863 	ttm_bo_add_mem_to_lru(bo, mem);
864 	spin_unlock(&ttm_bo_glob.lru_lock);
865 
866 	return 0;
867 }
868 
869 /*
870  * Creates space for memory region @mem according to its type.
871  *
872  * This function first searches for free space in compatible memory types in
873  * the priority order defined by the driver.  If free space isn't found, then
874  * ttm_bo_mem_force_space is attempted in priority order to evict and find
875  * space.
876  */
877 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
878 			struct ttm_placement *placement,
879 			struct ttm_resource *mem,
880 			struct ttm_operation_ctx *ctx)
881 {
882 	struct ttm_bo_device *bdev = bo->bdev;
883 	bool type_found = false;
884 	int i, ret;
885 
886 	ret = dma_resv_reserve_shared(bo->base.resv, 1);
887 	if (unlikely(ret))
888 		return ret;
889 
890 	for (i = 0; i < placement->num_placement; ++i) {
891 		const struct ttm_place *place = &placement->placement[i];
892 		struct ttm_resource_manager *man;
893 
894 		ret = ttm_bo_mem_placement(bo, place, mem);
895 		if (ret)
896 			continue;
897 
898 		type_found = true;
899 		ret = ttm_resource_alloc(bo, place, mem);
900 		if (ret == -ENOSPC)
901 			continue;
902 		if (unlikely(ret))
903 			goto error;
904 
905 		man = ttm_manager_type(bdev, mem->mem_type);
906 		ret = ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
907 		if (unlikely(ret)) {
908 			ttm_resource_free(bo, mem);
909 			if (ret == -EBUSY)
910 				continue;
911 
912 			goto error;
913 		}
914 		return 0;
915 	}
916 
917 	for (i = 0; i < placement->num_busy_placement; ++i) {
918 		const struct ttm_place *place = &placement->busy_placement[i];
919 
920 		ret = ttm_bo_mem_placement(bo, place, mem);
921 		if (ret)
922 			continue;
923 
924 		type_found = true;
925 		ret = ttm_bo_mem_force_space(bo, place, mem, ctx);
926 		if (likely(!ret))
927 			return 0;
928 
929 		if (ret && ret != -EBUSY)
930 			goto error;
931 	}
932 
933 	ret = -ENOMEM;
934 	if (!type_found) {
935 		pr_err(TTM_PFX "No compatible memory type found\n");
936 		ret = -EINVAL;
937 	}
938 
939 error:
940 	if (bo->mem.mem_type == TTM_PL_SYSTEM && !list_empty(&bo->lru)) {
941 		ttm_bo_move_to_lru_tail_unlocked(bo);
942 	}
943 
944 	return ret;
945 }
946 EXPORT_SYMBOL(ttm_bo_mem_space);
947 
948 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
949 				     struct ttm_resource *mem,
950 				     struct ttm_operation_ctx *ctx,
951 				     struct ttm_place *hop)
952 {
953 	struct ttm_placement hop_placement;
954 	int ret;
955 	struct ttm_resource hop_mem = *mem;
956 
957 	hop_mem.mm_node = NULL;
958 	hop_mem.mem_type = TTM_PL_SYSTEM;
959 	hop_mem.placement = 0;
960 
961 	hop_placement.num_placement = hop_placement.num_busy_placement = 1;
962 	hop_placement.placement = hop_placement.busy_placement = hop;
963 
964 	/* find space in the bounce domain */
965 	ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
966 	if (ret)
967 		return ret;
968 	/* move to the bounce domain */
969 	ret = ttm_bo_handle_move_mem(bo, &hop_mem, false, ctx, NULL);
970 	if (ret)
971 		return ret;
972 	return 0;
973 }
974 
975 static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
976 			      struct ttm_placement *placement,
977 			      struct ttm_operation_ctx *ctx)
978 {
979 	int ret = 0;
980 	struct ttm_place hop;
981 	struct ttm_resource mem;
982 
983 	dma_resv_assert_held(bo->base.resv);
984 
985 	memset(&hop, 0, sizeof(hop));
986 
987 	mem.num_pages = bo->num_pages;
988 	mem.size = mem.num_pages << PAGE_SHIFT;
989 	mem.page_alignment = bo->mem.page_alignment;
990 	mem.bus.offset = 0;
991 	mem.bus.addr = NULL;
992 	mem.mm_node = NULL;
993 
994 	/*
995 	 * Determine where to move the buffer.
996 	 *
997 	 * If driver determines move is going to need
998 	 * an extra step then it will return -EMULTIHOP
999 	 * and the buffer will be moved to the temporary
1000 	 * stop and the driver will be called to make
1001 	 * the second hop.
1002 	 */
1003 bounce:
1004 	ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
1005 	if (ret)
1006 		return ret;
1007 	ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx, &hop);
1008 	if (ret == -EMULTIHOP) {
1009 		ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
1010 		if (ret)
1011 			return ret;
1012 		/* try and move to final place now. */
1013 		goto bounce;
1014 	}
1015 	if (ret)
1016 		ttm_resource_free(bo, &mem);
1017 	return ret;
1018 }
1019 
1020 static bool ttm_bo_places_compat(const struct ttm_place *places,
1021 				 unsigned num_placement,
1022 				 struct ttm_resource *mem,
1023 				 uint32_t *new_flags)
1024 {
1025 	unsigned i;
1026 
1027 	for (i = 0; i < num_placement; i++) {
1028 		const struct ttm_place *heap = &places[i];
1029 
1030 		if ((mem->start < heap->fpfn ||
1031 		     (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
1032 			continue;
1033 
1034 		*new_flags = heap->flags;
1035 		if ((mem->mem_type == heap->mem_type) &&
1036 		    (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
1037 		     (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
1038 			return true;
1039 	}
1040 	return false;
1041 }
1042 
1043 bool ttm_bo_mem_compat(struct ttm_placement *placement,
1044 		       struct ttm_resource *mem,
1045 		       uint32_t *new_flags)
1046 {
1047 	if (ttm_bo_places_compat(placement->placement, placement->num_placement,
1048 				 mem, new_flags))
1049 		return true;
1050 
1051 	if ((placement->busy_placement != placement->placement ||
1052 	     placement->num_busy_placement > placement->num_placement) &&
1053 	    ttm_bo_places_compat(placement->busy_placement,
1054 				 placement->num_busy_placement,
1055 				 mem, new_flags))
1056 		return true;
1057 
1058 	return false;
1059 }
1060 EXPORT_SYMBOL(ttm_bo_mem_compat);
1061 
1062 int ttm_bo_validate(struct ttm_buffer_object *bo,
1063 		    struct ttm_placement *placement,
1064 		    struct ttm_operation_ctx *ctx)
1065 {
1066 	int ret;
1067 	uint32_t new_flags;
1068 
1069 	dma_resv_assert_held(bo->base.resv);
1070 
1071 	/*
1072 	 * Remove the backing store if no placement is given.
1073 	 */
1074 	if (!placement->num_placement && !placement->num_busy_placement) {
1075 		ret = ttm_bo_pipeline_gutting(bo);
1076 		if (ret)
1077 			return ret;
1078 
1079 		return ttm_tt_create(bo, false);
1080 	}
1081 
1082 	/*
1083 	 * Check whether we need to move buffer.
1084 	 */
1085 	if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
1086 		ret = ttm_bo_move_buffer(bo, placement, ctx);
1087 		if (ret)
1088 			return ret;
1089 	}
1090 	/*
1091 	 * We might need to add a TTM.
1092 	 */
1093 	if (bo->mem.mem_type == TTM_PL_SYSTEM) {
1094 		ret = ttm_tt_create(bo, true);
1095 		if (ret)
1096 			return ret;
1097 	}
1098 	return 0;
1099 }
1100 EXPORT_SYMBOL(ttm_bo_validate);
1101 
1102 int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
1103 			 struct ttm_buffer_object *bo,
1104 			 unsigned long size,
1105 			 enum ttm_bo_type type,
1106 			 struct ttm_placement *placement,
1107 			 uint32_t page_alignment,
1108 			 struct ttm_operation_ctx *ctx,
1109 			 size_t acc_size,
1110 			 struct sg_table *sg,
1111 			 struct dma_resv *resv,
1112 			 void (*destroy) (struct ttm_buffer_object *))
1113 {
1114 	struct ttm_mem_global *mem_glob = &ttm_mem_glob;
1115 	int ret = 0;
1116 	unsigned long num_pages;
1117 	bool locked;
1118 
1119 	ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
1120 	if (ret) {
1121 		pr_err("Out of kernel memory\n");
1122 		if (destroy)
1123 			(*destroy)(bo);
1124 		else
1125 			kfree(bo);
1126 		return -ENOMEM;
1127 	}
1128 
1129 	num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1130 	if (num_pages == 0) {
1131 		pr_err("Illegal buffer object size\n");
1132 		if (destroy)
1133 			(*destroy)(bo);
1134 		else
1135 			kfree(bo);
1136 		ttm_mem_global_free(mem_glob, acc_size);
1137 		return -EINVAL;
1138 	}
1139 	bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1140 
1141 	kref_init(&bo->kref);
1142 	INIT_LIST_HEAD(&bo->lru);
1143 	INIT_LIST_HEAD(&bo->ddestroy);
1144 	INIT_LIST_HEAD(&bo->swap);
1145 	bo->bdev = bdev;
1146 	bo->type = type;
1147 	bo->num_pages = num_pages;
1148 	bo->mem.size = num_pages << PAGE_SHIFT;
1149 	bo->mem.mem_type = TTM_PL_SYSTEM;
1150 	bo->mem.num_pages = bo->num_pages;
1151 	bo->mem.mm_node = NULL;
1152 	bo->mem.page_alignment = page_alignment;
1153 	bo->mem.bus.offset = 0;
1154 	bo->mem.bus.addr = NULL;
1155 	bo->moving = NULL;
1156 	bo->mem.placement = 0;
1157 	bo->acc_size = acc_size;
1158 	bo->pin_count = 0;
1159 	bo->sg = sg;
1160 	if (resv) {
1161 		bo->base.resv = resv;
1162 		dma_resv_assert_held(bo->base.resv);
1163 	} else {
1164 		bo->base.resv = &bo->base._resv;
1165 	}
1166 	if (!ttm_bo_uses_embedded_gem_object(bo)) {
1167 		/*
1168 		 * bo.gem is not initialized, so we have to setup the
1169 		 * struct elements we want use regardless.
1170 		 */
1171 		dma_resv_init(&bo->base._resv);
1172 		drm_vma_node_reset(&bo->base.vma_node);
1173 	}
1174 	atomic_inc(&ttm_bo_glob.bo_count);
1175 
1176 	/*
1177 	 * For ttm_bo_type_device buffers, allocate
1178 	 * address space from the device.
1179 	 */
1180 	if (bo->type == ttm_bo_type_device ||
1181 	    bo->type == ttm_bo_type_sg)
1182 		ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
1183 					 bo->mem.num_pages);
1184 
1185 	/* passed reservation objects should already be locked,
1186 	 * since otherwise lockdep will be angered in radeon.
1187 	 */
1188 	if (!resv) {
1189 		locked = dma_resv_trylock(bo->base.resv);
1190 		WARN_ON(!locked);
1191 	}
1192 
1193 	if (likely(!ret))
1194 		ret = ttm_bo_validate(bo, placement, ctx);
1195 
1196 	if (unlikely(ret)) {
1197 		if (!resv)
1198 			ttm_bo_unreserve(bo);
1199 
1200 		ttm_bo_put(bo);
1201 		return ret;
1202 	}
1203 
1204 	ttm_bo_move_to_lru_tail_unlocked(bo);
1205 
1206 	return ret;
1207 }
1208 EXPORT_SYMBOL(ttm_bo_init_reserved);
1209 
1210 int ttm_bo_init(struct ttm_bo_device *bdev,
1211 		struct ttm_buffer_object *bo,
1212 		unsigned long size,
1213 		enum ttm_bo_type type,
1214 		struct ttm_placement *placement,
1215 		uint32_t page_alignment,
1216 		bool interruptible,
1217 		size_t acc_size,
1218 		struct sg_table *sg,
1219 		struct dma_resv *resv,
1220 		void (*destroy) (struct ttm_buffer_object *))
1221 {
1222 	struct ttm_operation_ctx ctx = { interruptible, false };
1223 	int ret;
1224 
1225 	ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
1226 				   page_alignment, &ctx, acc_size,
1227 				   sg, resv, destroy);
1228 	if (ret)
1229 		return ret;
1230 
1231 	if (!resv)
1232 		ttm_bo_unreserve(bo);
1233 
1234 	return 0;
1235 }
1236 EXPORT_SYMBOL(ttm_bo_init);
1237 
1238 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
1239 			   unsigned long bo_size,
1240 			   unsigned struct_size)
1241 {
1242 	unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
1243 	size_t size = 0;
1244 
1245 	size += ttm_round_pot(struct_size);
1246 	size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
1247 	size += ttm_round_pot(sizeof(struct ttm_tt));
1248 	return size;
1249 }
1250 EXPORT_SYMBOL(ttm_bo_dma_acc_size);
1251 
1252 static void ttm_bo_global_kobj_release(struct kobject *kobj)
1253 {
1254 	struct ttm_bo_global *glob =
1255 		container_of(kobj, struct ttm_bo_global, kobj);
1256 
1257 	__free_page(glob->dummy_read_page);
1258 }
1259 
1260 static void ttm_bo_global_release(void)
1261 {
1262 	struct ttm_bo_global *glob = &ttm_bo_glob;
1263 
1264 	mutex_lock(&ttm_global_mutex);
1265 	if (--ttm_bo_glob_use_count > 0)
1266 		goto out;
1267 
1268 	kobject_del(&glob->kobj);
1269 	kobject_put(&glob->kobj);
1270 	ttm_mem_global_release(&ttm_mem_glob);
1271 	memset(glob, 0, sizeof(*glob));
1272 out:
1273 	mutex_unlock(&ttm_global_mutex);
1274 }
1275 
1276 static int ttm_bo_global_init(void)
1277 {
1278 	struct ttm_bo_global *glob = &ttm_bo_glob;
1279 	int ret = 0;
1280 	unsigned i;
1281 
1282 	mutex_lock(&ttm_global_mutex);
1283 	if (++ttm_bo_glob_use_count > 1)
1284 		goto out;
1285 
1286 	ret = ttm_mem_global_init(&ttm_mem_glob);
1287 	if (ret)
1288 		goto out;
1289 
1290 	spin_lock_init(&glob->lru_lock);
1291 	glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
1292 
1293 	if (unlikely(glob->dummy_read_page == NULL)) {
1294 		ret = -ENOMEM;
1295 		goto out;
1296 	}
1297 
1298 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1299 		INIT_LIST_HEAD(&glob->swap_lru[i]);
1300 	INIT_LIST_HEAD(&glob->device_list);
1301 	atomic_set(&glob->bo_count, 0);
1302 
1303 	ret = kobject_init_and_add(
1304 		&glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
1305 	if (unlikely(ret != 0))
1306 		kobject_put(&glob->kobj);
1307 out:
1308 	mutex_unlock(&ttm_global_mutex);
1309 	return ret;
1310 }
1311 
1312 int ttm_bo_device_release(struct ttm_bo_device *bdev)
1313 {
1314 	struct ttm_bo_global *glob = &ttm_bo_glob;
1315 	int ret = 0;
1316 	unsigned i;
1317 	struct ttm_resource_manager *man;
1318 
1319 	man = ttm_manager_type(bdev, TTM_PL_SYSTEM);
1320 	ttm_resource_manager_set_used(man, false);
1321 	ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL);
1322 
1323 	mutex_lock(&ttm_global_mutex);
1324 	list_del(&bdev->device_list);
1325 	mutex_unlock(&ttm_global_mutex);
1326 
1327 	cancel_delayed_work_sync(&bdev->wq);
1328 
1329 	if (ttm_bo_delayed_delete(bdev, true))
1330 		pr_debug("Delayed destroy list was clean\n");
1331 
1332 	spin_lock(&glob->lru_lock);
1333 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
1334 		if (list_empty(&man->lru[0]))
1335 			pr_debug("Swap list %d was clean\n", i);
1336 	spin_unlock(&glob->lru_lock);
1337 
1338 	ttm_pool_fini(&bdev->pool);
1339 
1340 	if (!ret)
1341 		ttm_bo_global_release();
1342 
1343 	return ret;
1344 }
1345 EXPORT_SYMBOL(ttm_bo_device_release);
1346 
1347 static void ttm_bo_init_sysman(struct ttm_bo_device *bdev)
1348 {
1349 	struct ttm_resource_manager *man = &bdev->sysman;
1350 
1351 	/*
1352 	 * Initialize the system memory buffer type.
1353 	 * Other types need to be driver / IOCTL initialized.
1354 	 */
1355 	man->use_tt = true;
1356 
1357 	ttm_resource_manager_init(man, 0);
1358 	ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, man);
1359 	ttm_resource_manager_set_used(man, true);
1360 }
1361 
1362 int ttm_bo_device_init(struct ttm_bo_device *bdev,
1363 		       struct ttm_bo_driver *driver,
1364 		       struct device *dev,
1365 		       struct address_space *mapping,
1366 		       struct drm_vma_offset_manager *vma_manager,
1367 		       bool use_dma_alloc, bool use_dma32)
1368 {
1369 	struct ttm_bo_global *glob = &ttm_bo_glob;
1370 	int ret;
1371 
1372 	if (WARN_ON(vma_manager == NULL))
1373 		return -EINVAL;
1374 
1375 	ret = ttm_bo_global_init();
1376 	if (ret)
1377 		return ret;
1378 
1379 	bdev->driver = driver;
1380 
1381 	ttm_bo_init_sysman(bdev);
1382 	ttm_pool_init(&bdev->pool, dev, use_dma_alloc, use_dma32);
1383 
1384 	bdev->vma_manager = vma_manager;
1385 	INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
1386 	INIT_LIST_HEAD(&bdev->ddestroy);
1387 	bdev->dev_mapping = mapping;
1388 	mutex_lock(&ttm_global_mutex);
1389 	list_add_tail(&bdev->device_list, &glob->device_list);
1390 	mutex_unlock(&ttm_global_mutex);
1391 
1392 	return 0;
1393 }
1394 EXPORT_SYMBOL(ttm_bo_device_init);
1395 
1396 /*
1397  * buffer object vm functions.
1398  */
1399 
1400 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1401 {
1402 	struct ttm_bo_device *bdev = bo->bdev;
1403 
1404 	drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1405 	ttm_mem_io_free(bdev, &bo->mem);
1406 }
1407 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1408 
1409 int ttm_bo_wait(struct ttm_buffer_object *bo,
1410 		bool interruptible, bool no_wait)
1411 {
1412 	long timeout = 15 * HZ;
1413 
1414 	if (no_wait) {
1415 		if (dma_resv_test_signaled_rcu(bo->base.resv, true))
1416 			return 0;
1417 		else
1418 			return -EBUSY;
1419 	}
1420 
1421 	timeout = dma_resv_wait_timeout_rcu(bo->base.resv, true,
1422 						      interruptible, timeout);
1423 	if (timeout < 0)
1424 		return timeout;
1425 
1426 	if (timeout == 0)
1427 		return -EBUSY;
1428 
1429 	dma_resv_add_excl_fence(bo->base.resv, NULL);
1430 	return 0;
1431 }
1432 EXPORT_SYMBOL(ttm_bo_wait);
1433 
1434 /*
1435  * A buffer object shrink method that tries to swap out the first
1436  * buffer object on the bo_global::swap_lru list.
1437  */
1438 int ttm_bo_swapout(struct ttm_operation_ctx *ctx)
1439 {
1440 	struct ttm_bo_global *glob = &ttm_bo_glob;
1441 	struct ttm_buffer_object *bo;
1442 	int ret = -EBUSY;
1443 	bool locked;
1444 	unsigned i;
1445 
1446 	spin_lock(&glob->lru_lock);
1447 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
1448 		list_for_each_entry(bo, &glob->swap_lru[i], swap) {
1449 			if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked,
1450 							    NULL))
1451 				continue;
1452 
1453 			if (!ttm_bo_get_unless_zero(bo)) {
1454 				if (locked)
1455 					dma_resv_unlock(bo->base.resv);
1456 				continue;
1457 			}
1458 
1459 			ret = 0;
1460 			break;
1461 		}
1462 		if (!ret)
1463 			break;
1464 	}
1465 
1466 	if (ret) {
1467 		spin_unlock(&glob->lru_lock);
1468 		return ret;
1469 	}
1470 
1471 	if (bo->deleted) {
1472 		ret = ttm_bo_cleanup_refs(bo, false, false, locked);
1473 		ttm_bo_put(bo);
1474 		return ret;
1475 	}
1476 
1477 	ttm_bo_del_from_lru(bo);
1478 	spin_unlock(&glob->lru_lock);
1479 
1480 	/**
1481 	 * Move to system cached
1482 	 */
1483 
1484 	if (bo->mem.mem_type != TTM_PL_SYSTEM) {
1485 		struct ttm_operation_ctx ctx = { false, false };
1486 		struct ttm_resource evict_mem;
1487 		struct ttm_place hop;
1488 
1489 		memset(&hop, 0, sizeof(hop));
1490 
1491 		evict_mem = bo->mem;
1492 		evict_mem.mm_node = NULL;
1493 		evict_mem.placement = 0;
1494 		evict_mem.mem_type = TTM_PL_SYSTEM;
1495 
1496 		ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx, &hop);
1497 		if (unlikely(ret != 0)) {
1498 			WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
1499 			goto out;
1500 		}
1501 	}
1502 
1503 	/**
1504 	 * Make sure BO is idle.
1505 	 */
1506 
1507 	ret = ttm_bo_wait(bo, false, false);
1508 	if (unlikely(ret != 0))
1509 		goto out;
1510 
1511 	ttm_bo_unmap_virtual(bo);
1512 
1513 	/**
1514 	 * Swap out. Buffer will be swapped in again as soon as
1515 	 * anyone tries to access a ttm page.
1516 	 */
1517 
1518 	if (bo->bdev->driver->swap_notify)
1519 		bo->bdev->driver->swap_notify(bo);
1520 
1521 	ret = ttm_tt_swapout(bo->bdev, bo->ttm);
1522 out:
1523 
1524 	/**
1525 	 *
1526 	 * Unreserve without putting on LRU to avoid swapping out an
1527 	 * already swapped buffer.
1528 	 */
1529 	if (locked)
1530 		dma_resv_unlock(bo->base.resv);
1531 	ttm_bo_put(bo);
1532 	return ret;
1533 }
1534 EXPORT_SYMBOL(ttm_bo_swapout);
1535 
1536 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1537 {
1538 	if (bo->ttm == NULL)
1539 		return;
1540 
1541 	ttm_tt_destroy(bo->bdev, bo->ttm);
1542 	bo->ttm = NULL;
1543 }
1544 
1545