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