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