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