xref: /linux/drivers/gpu/drm/ttm/ttm_bo.c (revision 3671f37777589194c44bb9351568c13eee43da3c)
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_bo.h>
35 #include <drm/ttm/ttm_placement.h>
36 #include <drm/ttm/ttm_tt.h>
37 
38 #include <linux/jiffies.h>
39 #include <linux/slab.h>
40 #include <linux/sched.h>
41 #include <linux/mm.h>
42 #include <linux/file.h>
43 #include <linux/module.h>
44 #include <linux/atomic.h>
45 #include <linux/cgroup_dmem.h>
46 #include <linux/dma-resv.h>
47 
48 #include "ttm_module.h"
49 #include "ttm_bo_internal.h"
50 
51 static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
52 					struct ttm_placement *placement)
53 {
54 	struct drm_printer p = drm_dbg_printer(NULL, DRM_UT_CORE, TTM_PFX);
55 	struct ttm_resource_manager *man;
56 	int i, mem_type;
57 
58 	for (i = 0; i < placement->num_placement; i++) {
59 		mem_type = placement->placement[i].mem_type;
60 		drm_printf(&p, "  placement[%d]=0x%08X (%d)\n",
61 			   i, placement->placement[i].flags, mem_type);
62 		man = ttm_manager_type(bo->bdev, mem_type);
63 		ttm_resource_manager_debug(man, &p);
64 	}
65 }
66 
67 /**
68  * ttm_bo_move_to_lru_tail
69  *
70  * @bo: The buffer object.
71  *
72  * Move this BO to the tail of all lru lists used to lookup and reserve an
73  * object. This function must be called with struct ttm_global::lru_lock
74  * held, and is used to make a BO less likely to be considered for eviction.
75  */
76 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo)
77 {
78 	dma_resv_assert_held(bo->base.resv);
79 
80 	if (bo->resource)
81 		ttm_resource_move_to_lru_tail(bo->resource);
82 }
83 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
84 
85 /**
86  * ttm_bo_set_bulk_move - update BOs bulk move object
87  *
88  * @bo: The buffer object.
89  * @bulk: bulk move structure
90  *
91  * Update the BOs bulk move object, making sure that resources are added/removed
92  * as well. A bulk move allows to move many resource on the LRU at once,
93  * resulting in much less overhead of maintaining the LRU.
94  * The only requirement is that the resources stay together on the LRU and are
95  * never separated. This is enforces by setting the bulk_move structure on a BO.
96  * ttm_lru_bulk_move_tail() should be used to move all resources to the tail of
97  * their LRU list.
98  */
99 void ttm_bo_set_bulk_move(struct ttm_buffer_object *bo,
100 			  struct ttm_lru_bulk_move *bulk)
101 {
102 	dma_resv_assert_held(bo->base.resv);
103 
104 	if (bo->bulk_move == bulk)
105 		return;
106 
107 	spin_lock(&bo->bdev->lru_lock);
108 	if (bo->resource)
109 		ttm_resource_del_bulk_move(bo->resource, bo);
110 	bo->bulk_move = bulk;
111 	if (bo->resource)
112 		ttm_resource_add_bulk_move(bo->resource, bo);
113 	spin_unlock(&bo->bdev->lru_lock);
114 }
115 EXPORT_SYMBOL(ttm_bo_set_bulk_move);
116 
117 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
118 				  struct ttm_resource *mem, bool evict,
119 				  struct ttm_operation_ctx *ctx,
120 				  struct ttm_place *hop)
121 {
122 	struct ttm_device *bdev = bo->bdev;
123 	bool old_use_tt, new_use_tt;
124 	int ret;
125 
126 	old_use_tt = !bo->resource || ttm_manager_type(bdev, bo->resource->mem_type)->use_tt;
127 	new_use_tt = ttm_manager_type(bdev, mem->mem_type)->use_tt;
128 
129 	ttm_bo_unmap_virtual(bo);
130 
131 	/*
132 	 * Create and bind a ttm if required.
133 	 */
134 
135 	if (new_use_tt) {
136 		/* Zero init the new TTM structure if the old location should
137 		 * have used one as well.
138 		 */
139 		ret = ttm_tt_create(bo, old_use_tt);
140 		if (ret)
141 			goto out_err;
142 
143 		if (mem->mem_type != TTM_PL_SYSTEM) {
144 			ret = ttm_bo_populate(bo, ctx);
145 			if (ret)
146 				goto out_err;
147 		}
148 	}
149 
150 	ret = dma_resv_reserve_fences(bo->base.resv, 1);
151 	if (ret)
152 		goto out_err;
153 
154 	ret = bdev->funcs->move(bo, evict, ctx, mem, hop);
155 	if (ret) {
156 		if (ret == -EMULTIHOP)
157 			return ret;
158 		goto out_err;
159 	}
160 
161 	ctx->bytes_moved += bo->base.size;
162 	return 0;
163 
164 out_err:
165 	if (!old_use_tt)
166 		ttm_bo_tt_destroy(bo);
167 
168 	return ret;
169 }
170 
171 /*
172  * Call bo::reserved.
173  * Will release GPU memory type usage on destruction.
174  * This is the place to put in driver specific hooks to release
175  * driver private resources.
176  * Will release the bo::reserved lock.
177  */
178 
179 static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
180 {
181 	if (bo->bdev->funcs->delete_mem_notify)
182 		bo->bdev->funcs->delete_mem_notify(bo);
183 
184 	ttm_bo_tt_destroy(bo);
185 	ttm_resource_free(bo, &bo->resource);
186 }
187 
188 static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
189 {
190 	int r;
191 
192 	if (bo->base.resv == &bo->base._resv)
193 		return 0;
194 
195 	BUG_ON(!dma_resv_trylock(&bo->base._resv));
196 
197 	r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
198 	dma_resv_unlock(&bo->base._resv);
199 	if (r)
200 		return r;
201 
202 	if (bo->type != ttm_bo_type_sg) {
203 		/* This works because the BO is about to be destroyed and nobody
204 		 * reference it any more. The only tricky case is the trylock on
205 		 * the resv object while holding the lru_lock.
206 		 */
207 		spin_lock(&bo->bdev->lru_lock);
208 		bo->base.resv = &bo->base._resv;
209 		spin_unlock(&bo->bdev->lru_lock);
210 	}
211 
212 	return r;
213 }
214 
215 static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
216 {
217 	struct dma_resv *resv = &bo->base._resv;
218 	struct dma_resv_iter cursor;
219 	struct dma_fence *fence;
220 
221 	dma_resv_iter_begin(&cursor, resv, DMA_RESV_USAGE_BOOKKEEP);
222 	dma_resv_for_each_fence_unlocked(&cursor, fence) {
223 		if (!fence->ops->signaled)
224 			dma_fence_enable_sw_signaling(fence);
225 	}
226 	dma_resv_iter_end(&cursor);
227 }
228 
229 /*
230  * Block for the dma_resv object to become idle, lock the buffer and clean up
231  * the resource and tt object.
232  */
233 static void ttm_bo_delayed_delete(struct work_struct *work)
234 {
235 	struct ttm_buffer_object *bo;
236 
237 	bo = container_of(work, typeof(*bo), delayed_delete);
238 
239 	dma_resv_wait_timeout(&bo->base._resv, DMA_RESV_USAGE_BOOKKEEP, false,
240 			      MAX_SCHEDULE_TIMEOUT);
241 	dma_resv_lock(bo->base.resv, NULL);
242 	ttm_bo_cleanup_memtype_use(bo);
243 	dma_resv_unlock(bo->base.resv);
244 	ttm_bo_put(bo);
245 }
246 
247 static void ttm_bo_release(struct kref *kref)
248 {
249 	struct ttm_buffer_object *bo =
250 	    container_of(kref, struct ttm_buffer_object, kref);
251 	struct ttm_device *bdev = bo->bdev;
252 	int ret;
253 
254 	WARN_ON_ONCE(bo->pin_count);
255 	WARN_ON_ONCE(bo->bulk_move);
256 
257 	if (!bo->deleted) {
258 		ret = ttm_bo_individualize_resv(bo);
259 		if (ret) {
260 			/* Last resort, if we fail to allocate memory for the
261 			 * fences block for the BO to become idle
262 			 */
263 			dma_resv_wait_timeout(bo->base.resv,
264 					      DMA_RESV_USAGE_BOOKKEEP, false,
265 					      30 * HZ);
266 		}
267 
268 		if (bo->bdev->funcs->release_notify)
269 			bo->bdev->funcs->release_notify(bo);
270 
271 		drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
272 		ttm_mem_io_free(bdev, bo->resource);
273 
274 		if (!dma_resv_test_signaled(&bo->base._resv,
275 					    DMA_RESV_USAGE_BOOKKEEP) ||
276 		    (want_init_on_free() && (bo->ttm != NULL)) ||
277 		    bo->type == ttm_bo_type_sg ||
278 		    !dma_resv_trylock(bo->base.resv)) {
279 			/* The BO is not idle, resurrect it for delayed destroy */
280 			ttm_bo_flush_all_fences(bo);
281 			bo->deleted = true;
282 
283 			spin_lock(&bo->bdev->lru_lock);
284 
285 			/*
286 			 * Make pinned bos immediately available to
287 			 * shrinkers, now that they are queued for
288 			 * destruction.
289 			 *
290 			 * FIXME: QXL is triggering this. Can be removed when the
291 			 * driver is fixed.
292 			 */
293 			if (bo->pin_count) {
294 				bo->pin_count = 0;
295 				ttm_resource_move_to_lru_tail(bo->resource);
296 			}
297 
298 			kref_init(&bo->kref);
299 			spin_unlock(&bo->bdev->lru_lock);
300 
301 			INIT_WORK(&bo->delayed_delete, ttm_bo_delayed_delete);
302 
303 			/* Schedule the worker on the closest NUMA node. This
304 			 * improves performance since system memory might be
305 			 * cleared on free and that is best done on a CPU core
306 			 * close to it.
307 			 */
308 			queue_work_node(bdev->pool.nid, bdev->wq, &bo->delayed_delete);
309 			return;
310 		}
311 
312 		ttm_bo_cleanup_memtype_use(bo);
313 		dma_resv_unlock(bo->base.resv);
314 	}
315 
316 	atomic_dec(&ttm_glob.bo_count);
317 	bo->destroy(bo);
318 }
319 
320 /**
321  * ttm_bo_put
322  *
323  * @bo: The buffer object.
324  *
325  * Unreference a buffer object.
326  */
327 void ttm_bo_put(struct ttm_buffer_object *bo)
328 {
329 	kref_put(&bo->kref, ttm_bo_release);
330 }
331 EXPORT_SYMBOL(ttm_bo_put);
332 
333 static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
334 				     struct ttm_operation_ctx *ctx,
335 				     struct ttm_place *hop)
336 {
337 	struct ttm_placement hop_placement;
338 	struct ttm_resource *hop_mem;
339 	int ret;
340 
341 	hop_placement.num_placement = 1;
342 	hop_placement.placement = hop;
343 
344 	/* find space in the bounce domain */
345 	ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
346 	if (ret)
347 		return ret;
348 	/* move to the bounce domain */
349 	ret = ttm_bo_handle_move_mem(bo, hop_mem, false, ctx, NULL);
350 	if (ret) {
351 		ttm_resource_free(bo, &hop_mem);
352 		return ret;
353 	}
354 	return 0;
355 }
356 
357 static int ttm_bo_evict(struct ttm_buffer_object *bo,
358 			struct ttm_operation_ctx *ctx)
359 {
360 	struct ttm_device *bdev = bo->bdev;
361 	struct ttm_resource *evict_mem;
362 	struct ttm_placement placement;
363 	struct ttm_place hop;
364 	int ret = 0;
365 
366 	memset(&hop, 0, sizeof(hop));
367 
368 	dma_resv_assert_held(bo->base.resv);
369 
370 	placement.num_placement = 0;
371 	bdev->funcs->evict_flags(bo, &placement);
372 
373 	if (!placement.num_placement) {
374 		ret = ttm_bo_wait_ctx(bo, ctx);
375 		if (ret)
376 			return ret;
377 
378 		/*
379 		 * Since we've already synced, this frees backing store
380 		 * immediately.
381 		 */
382 		return ttm_bo_pipeline_gutting(bo);
383 	}
384 
385 	ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
386 	if (ret) {
387 		if (ret != -ERESTARTSYS) {
388 			pr_err("Failed to find memory space for buffer 0x%p eviction\n",
389 			       bo);
390 			ttm_bo_mem_space_debug(bo, &placement);
391 		}
392 		goto out;
393 	}
394 
395 	do {
396 		ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
397 		if (ret != -EMULTIHOP)
398 			break;
399 
400 		ret = ttm_bo_bounce_temp_buffer(bo, ctx, &hop);
401 	} while (!ret);
402 
403 	if (ret) {
404 		ttm_resource_free(bo, &evict_mem);
405 		if (ret != -ERESTARTSYS && ret != -EINTR)
406 			pr_err("Buffer eviction failed\n");
407 	}
408 out:
409 	return ret;
410 }
411 
412 /**
413  * ttm_bo_eviction_valuable
414  *
415  * @bo: The buffer object to evict
416  * @place: the placement we need to make room for
417  *
418  * Check if it is valuable to evict the BO to make room for the given placement.
419  */
420 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
421 			      const struct ttm_place *place)
422 {
423 	struct ttm_resource *res = bo->resource;
424 	struct ttm_device *bdev = bo->bdev;
425 
426 	dma_resv_assert_held(bo->base.resv);
427 	if (bo->resource->mem_type == TTM_PL_SYSTEM)
428 		return true;
429 
430 	/* Don't evict this BO if it's outside of the
431 	 * requested placement range
432 	 */
433 	return ttm_resource_intersects(bdev, res, place, bo->base.size);
434 }
435 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
436 
437 /**
438  * ttm_bo_evict_first() - Evict the first bo on the manager's LRU list.
439  * @bdev: The ttm device.
440  * @man: The manager whose bo to evict.
441  * @ctx: The TTM operation ctx governing the eviction.
442  *
443  * Return: 0 if successful or the resource disappeared. Negative error code on error.
444  */
445 int ttm_bo_evict_first(struct ttm_device *bdev, struct ttm_resource_manager *man,
446 		       struct ttm_operation_ctx *ctx)
447 {
448 	struct ttm_resource_cursor cursor;
449 	struct ttm_buffer_object *bo;
450 	struct ttm_resource *res;
451 	unsigned int mem_type;
452 	int ret = 0;
453 
454 	spin_lock(&bdev->lru_lock);
455 	ttm_resource_cursor_init(&cursor, man);
456 	res = ttm_resource_manager_first(&cursor);
457 	ttm_resource_cursor_fini(&cursor);
458 	if (!res) {
459 		ret = -ENOENT;
460 		goto out_no_ref;
461 	}
462 	bo = res->bo;
463 	if (!ttm_bo_get_unless_zero(bo))
464 		goto out_no_ref;
465 	mem_type = res->mem_type;
466 	spin_unlock(&bdev->lru_lock);
467 	ret = ttm_bo_reserve(bo, ctx->interruptible, ctx->no_wait_gpu, NULL);
468 	if (ret)
469 		goto out_no_lock;
470 	if (!bo->resource || bo->resource->mem_type != mem_type)
471 		goto out_bo_moved;
472 
473 	if (bo->deleted) {
474 		ret = ttm_bo_wait_ctx(bo, ctx);
475 		if (!ret)
476 			ttm_bo_cleanup_memtype_use(bo);
477 	} else {
478 		ret = ttm_bo_evict(bo, ctx);
479 	}
480 out_bo_moved:
481 	dma_resv_unlock(bo->base.resv);
482 out_no_lock:
483 	ttm_bo_put(bo);
484 	return ret;
485 
486 out_no_ref:
487 	spin_unlock(&bdev->lru_lock);
488 	return ret;
489 }
490 
491 /**
492  * struct ttm_bo_evict_walk - Parameters for the evict walk.
493  */
494 struct ttm_bo_evict_walk {
495 	/** @walk: The walk base parameters. */
496 	struct ttm_lru_walk walk;
497 	/** @place: The place passed to the resource allocation. */
498 	const struct ttm_place *place;
499 	/** @evictor: The buffer object we're trying to make room for. */
500 	struct ttm_buffer_object *evictor;
501 	/** @res: The allocated resource if any. */
502 	struct ttm_resource **res;
503 	/** @evicted: Number of successful evictions. */
504 	unsigned long evicted;
505 
506 	/** @limit_pool: Which pool limit we should test against */
507 	struct dmem_cgroup_pool_state *limit_pool;
508 	/** @try_low: Whether we should attempt to evict BO's with low watermark threshold */
509 	bool try_low;
510 	/** @hit_low: If we cannot evict a bo when @try_low is false (first pass) */
511 	bool hit_low;
512 };
513 
514 static s64 ttm_bo_evict_cb(struct ttm_lru_walk *walk, struct ttm_buffer_object *bo)
515 {
516 	struct ttm_bo_evict_walk *evict_walk =
517 		container_of(walk, typeof(*evict_walk), walk);
518 	s64 lret;
519 
520 	if (!dmem_cgroup_state_evict_valuable(evict_walk->limit_pool, bo->resource->css,
521 					      evict_walk->try_low, &evict_walk->hit_low))
522 		return 0;
523 
524 	if (bo->pin_count || !bo->bdev->funcs->eviction_valuable(bo, evict_walk->place))
525 		return 0;
526 
527 	if (bo->deleted) {
528 		lret = ttm_bo_wait_ctx(bo, walk->ctx);
529 		if (!lret)
530 			ttm_bo_cleanup_memtype_use(bo);
531 	} else {
532 		lret = ttm_bo_evict(bo, walk->ctx);
533 	}
534 
535 	if (lret)
536 		goto out;
537 
538 	evict_walk->evicted++;
539 	if (evict_walk->res)
540 		lret = ttm_resource_alloc(evict_walk->evictor, evict_walk->place,
541 					  evict_walk->res, NULL);
542 	if (lret == 0)
543 		return 1;
544 out:
545 	/* Errors that should terminate the walk. */
546 	if (lret == -ENOSPC)
547 		return -EBUSY;
548 
549 	return lret;
550 }
551 
552 static const struct ttm_lru_walk_ops ttm_evict_walk_ops = {
553 	.process_bo = ttm_bo_evict_cb,
554 };
555 
556 static int ttm_bo_evict_alloc(struct ttm_device *bdev,
557 			      struct ttm_resource_manager *man,
558 			      const struct ttm_place *place,
559 			      struct ttm_buffer_object *evictor,
560 			      struct ttm_operation_ctx *ctx,
561 			      struct ww_acquire_ctx *ticket,
562 			      struct ttm_resource **res,
563 			      struct dmem_cgroup_pool_state *limit_pool)
564 {
565 	struct ttm_bo_evict_walk evict_walk = {
566 		.walk = {
567 			.ops = &ttm_evict_walk_ops,
568 			.ctx = ctx,
569 			.ticket = ticket,
570 		},
571 		.place = place,
572 		.evictor = evictor,
573 		.res = res,
574 		.limit_pool = limit_pool,
575 	};
576 	s64 lret;
577 
578 	evict_walk.walk.trylock_only = true;
579 	lret = ttm_lru_walk_for_evict(&evict_walk.walk, bdev, man, 1);
580 
581 	/* One more attempt if we hit low limit? */
582 	if (!lret && evict_walk.hit_low) {
583 		evict_walk.try_low = true;
584 		lret = ttm_lru_walk_for_evict(&evict_walk.walk, bdev, man, 1);
585 	}
586 	if (lret || !ticket)
587 		goto out;
588 
589 	/* Reset low limit */
590 	evict_walk.try_low = evict_walk.hit_low = false;
591 	/* If ticket-locking, repeat while making progress. */
592 	evict_walk.walk.trylock_only = false;
593 
594 retry:
595 	do {
596 		/* The walk may clear the evict_walk.walk.ticket field */
597 		evict_walk.walk.ticket = ticket;
598 		evict_walk.evicted = 0;
599 		lret = ttm_lru_walk_for_evict(&evict_walk.walk, bdev, man, 1);
600 	} while (!lret && evict_walk.evicted);
601 
602 	/* We hit the low limit? Try once more */
603 	if (!lret && evict_walk.hit_low && !evict_walk.try_low) {
604 		evict_walk.try_low = true;
605 		goto retry;
606 	}
607 out:
608 	if (lret < 0)
609 		return lret;
610 	if (lret == 0)
611 		return -EBUSY;
612 	return 0;
613 }
614 
615 /**
616  * ttm_bo_pin - Pin the buffer object.
617  * @bo: The buffer object to pin
618  *
619  * Make sure the buffer is not evicted any more during memory pressure.
620  * @bo must be unpinned again by calling ttm_bo_unpin().
621  */
622 void ttm_bo_pin(struct ttm_buffer_object *bo)
623 {
624 	dma_resv_assert_held(bo->base.resv);
625 	WARN_ON_ONCE(!kref_read(&bo->kref));
626 	spin_lock(&bo->bdev->lru_lock);
627 	if (bo->resource)
628 		ttm_resource_del_bulk_move(bo->resource, bo);
629 	if (!bo->pin_count++ && bo->resource)
630 		ttm_resource_move_to_lru_tail(bo->resource);
631 	spin_unlock(&bo->bdev->lru_lock);
632 }
633 EXPORT_SYMBOL(ttm_bo_pin);
634 
635 /**
636  * ttm_bo_unpin - Unpin the buffer object.
637  * @bo: The buffer object to unpin
638  *
639  * Allows the buffer object to be evicted again during memory pressure.
640  */
641 void ttm_bo_unpin(struct ttm_buffer_object *bo)
642 {
643 	dma_resv_assert_held(bo->base.resv);
644 	WARN_ON_ONCE(!kref_read(&bo->kref));
645 	if (WARN_ON_ONCE(!bo->pin_count))
646 		return;
647 
648 	spin_lock(&bo->bdev->lru_lock);
649 	if (!--bo->pin_count && bo->resource) {
650 		ttm_resource_add_bulk_move(bo->resource, bo);
651 		ttm_resource_move_to_lru_tail(bo->resource);
652 	}
653 	spin_unlock(&bo->bdev->lru_lock);
654 }
655 EXPORT_SYMBOL(ttm_bo_unpin);
656 
657 /*
658  * Add the last move fence to the BO as kernel dependency and reserve a new
659  * fence slot.
660  */
661 static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
662 				 struct ttm_resource_manager *man,
663 				 bool no_wait_gpu)
664 {
665 	struct dma_fence *fence;
666 	int ret;
667 
668 	spin_lock(&man->move_lock);
669 	fence = dma_fence_get(man->move);
670 	spin_unlock(&man->move_lock);
671 
672 	if (!fence)
673 		return 0;
674 
675 	if (no_wait_gpu) {
676 		ret = dma_fence_is_signaled(fence) ? 0 : -EBUSY;
677 		dma_fence_put(fence);
678 		return ret;
679 	}
680 
681 	dma_resv_add_fence(bo->base.resv, fence, DMA_RESV_USAGE_KERNEL);
682 
683 	ret = dma_resv_reserve_fences(bo->base.resv, 1);
684 	dma_fence_put(fence);
685 	return ret;
686 }
687 
688 /**
689  * ttm_bo_alloc_resource - Allocate backing store for a BO
690  *
691  * @bo: Pointer to a struct ttm_buffer_object of which we want a resource for
692  * @placement: Proposed new placement for the buffer object
693  * @ctx: if and how to sleep, lock buffers and alloc memory
694  * @force_space: If we should evict buffers to force space
695  * @res: The resulting struct ttm_resource.
696  *
697  * Allocates a resource for the buffer object pointed to by @bo, using the
698  * placement flags in @placement, potentially evicting other buffer objects when
699  * @force_space is true.
700  * This function may sleep while waiting for resources to become available.
701  * Returns:
702  * -EBUSY: No space available (only if no_wait == true).
703  * -ENOSPC: Could not allocate space for the buffer object, either due to
704  * fragmentation or concurrent allocators.
705  * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
706  */
707 static int ttm_bo_alloc_resource(struct ttm_buffer_object *bo,
708 				 struct ttm_placement *placement,
709 				 struct ttm_operation_ctx *ctx,
710 				 bool force_space,
711 				 struct ttm_resource **res)
712 {
713 	struct ttm_device *bdev = bo->bdev;
714 	struct ww_acquire_ctx *ticket;
715 	int i, ret;
716 
717 	ticket = dma_resv_locking_ctx(bo->base.resv);
718 	ret = dma_resv_reserve_fences(bo->base.resv, 1);
719 	if (unlikely(ret))
720 		return ret;
721 
722 	for (i = 0; i < placement->num_placement; ++i) {
723 		const struct ttm_place *place = &placement->placement[i];
724 		struct dmem_cgroup_pool_state *limit_pool = NULL;
725 		struct ttm_resource_manager *man;
726 		bool may_evict;
727 
728 		man = ttm_manager_type(bdev, place->mem_type);
729 		if (!man || !ttm_resource_manager_used(man))
730 			continue;
731 
732 		if (place->flags & (force_space ? TTM_PL_FLAG_DESIRED :
733 				    TTM_PL_FLAG_FALLBACK))
734 			continue;
735 
736 		may_evict = (force_space && place->mem_type != TTM_PL_SYSTEM);
737 		ret = ttm_resource_alloc(bo, place, res, force_space ? &limit_pool : NULL);
738 		if (ret) {
739 			if (ret != -ENOSPC && ret != -EAGAIN) {
740 				dmem_cgroup_pool_state_put(limit_pool);
741 				return ret;
742 			}
743 			if (!may_evict) {
744 				dmem_cgroup_pool_state_put(limit_pool);
745 				continue;
746 			}
747 
748 			ret = ttm_bo_evict_alloc(bdev, man, place, bo, ctx,
749 						 ticket, res, limit_pool);
750 			dmem_cgroup_pool_state_put(limit_pool);
751 			if (ret == -EBUSY)
752 				continue;
753 			if (ret)
754 				return ret;
755 		}
756 
757 		ret = ttm_bo_add_move_fence(bo, man, ctx->no_wait_gpu);
758 		if (unlikely(ret)) {
759 			ttm_resource_free(bo, res);
760 			if (ret == -EBUSY)
761 				continue;
762 
763 			return ret;
764 		}
765 		return 0;
766 	}
767 
768 	return -ENOSPC;
769 }
770 
771 /*
772  * ttm_bo_mem_space - Wrapper around ttm_bo_alloc_resource
773  *
774  * @bo: Pointer to a struct ttm_buffer_object of which we want a resource for
775  * @placement: Proposed new placement for the buffer object
776  * @res: The resulting struct ttm_resource.
777  * @ctx: if and how to sleep, lock buffers and alloc memory
778  *
779  * Tries both idle allocation and forcefully eviction of buffers. See
780  * ttm_bo_alloc_resource for details.
781  */
782 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
783 		     struct ttm_placement *placement,
784 		     struct ttm_resource **res,
785 		     struct ttm_operation_ctx *ctx)
786 {
787 	bool force_space = false;
788 	int ret;
789 
790 	do {
791 		ret = ttm_bo_alloc_resource(bo, placement, ctx,
792 					    force_space, res);
793 		force_space = !force_space;
794 	} while (ret == -ENOSPC && force_space);
795 
796 	return ret;
797 }
798 EXPORT_SYMBOL(ttm_bo_mem_space);
799 
800 /**
801  * ttm_bo_validate
802  *
803  * @bo: The buffer object.
804  * @placement: Proposed placement for the buffer object.
805  * @ctx: validation parameters.
806  *
807  * Changes placement and caching policy of the buffer object
808  * according proposed placement.
809  * Returns
810  * -EINVAL on invalid proposed placement.
811  * -ENOMEM on out-of-memory condition.
812  * -EBUSY if no_wait is true and buffer busy.
813  * -ERESTARTSYS if interrupted by a signal.
814  */
815 int ttm_bo_validate(struct ttm_buffer_object *bo,
816 		    struct ttm_placement *placement,
817 		    struct ttm_operation_ctx *ctx)
818 {
819 	struct ttm_resource *res;
820 	struct ttm_place hop;
821 	bool force_space;
822 	int ret;
823 
824 	dma_resv_assert_held(bo->base.resv);
825 
826 	/*
827 	 * Remove the backing store if no placement is given.
828 	 */
829 	if (!placement->num_placement)
830 		return ttm_bo_pipeline_gutting(bo);
831 
832 	force_space = false;
833 	do {
834 		/* Check whether we need to move buffer. */
835 		if (bo->resource &&
836 		    ttm_resource_compatible(bo->resource, placement,
837 					    force_space))
838 			return 0;
839 
840 		/* Moving of pinned BOs is forbidden */
841 		if (bo->pin_count)
842 			return -EINVAL;
843 
844 		/*
845 		 * Determine where to move the buffer.
846 		 *
847 		 * If driver determines move is going to need
848 		 * an extra step then it will return -EMULTIHOP
849 		 * and the buffer will be moved to the temporary
850 		 * stop and the driver will be called to make
851 		 * the second hop.
852 		 */
853 		ret = ttm_bo_alloc_resource(bo, placement, ctx, force_space,
854 					    &res);
855 		force_space = !force_space;
856 		if (ret == -ENOSPC)
857 			continue;
858 		if (ret)
859 			return ret;
860 
861 bounce:
862 		ret = ttm_bo_handle_move_mem(bo, res, false, ctx, &hop);
863 		if (ret == -EMULTIHOP) {
864 			ret = ttm_bo_bounce_temp_buffer(bo, ctx, &hop);
865 			/* try and move to final place now. */
866 			if (!ret)
867 				goto bounce;
868 		}
869 		if (ret) {
870 			ttm_resource_free(bo, &res);
871 			return ret;
872 		}
873 
874 	} while (ret && force_space);
875 
876 	/* For backward compatibility with userspace */
877 	if (ret == -ENOSPC)
878 		return -ENOMEM;
879 
880 	/*
881 	 * We might need to add a TTM.
882 	 */
883 	if (!bo->resource || bo->resource->mem_type == TTM_PL_SYSTEM) {
884 		ret = ttm_tt_create(bo, true);
885 		if (ret)
886 			return ret;
887 	}
888 	return 0;
889 }
890 EXPORT_SYMBOL(ttm_bo_validate);
891 
892 /**
893  * ttm_bo_init_reserved
894  *
895  * @bdev: Pointer to a ttm_device struct.
896  * @bo: Pointer to a ttm_buffer_object to be initialized.
897  * @type: Requested type of buffer object.
898  * @placement: Initial placement for buffer object.
899  * @alignment: Data alignment in pages.
900  * @ctx: TTM operation context for memory allocation.
901  * @sg: Scatter-gather table.
902  * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
903  * @destroy: Destroy function. Use NULL for kfree().
904  *
905  * This function initializes a pre-allocated struct ttm_buffer_object.
906  * As this object may be part of a larger structure, this function,
907  * together with the @destroy function, enables driver-specific objects
908  * derived from a ttm_buffer_object.
909  *
910  * On successful return, the caller owns an object kref to @bo. The kref and
911  * list_kref are usually set to 1, but note that in some situations, other
912  * tasks may already be holding references to @bo as well.
913  * Furthermore, if resv == NULL, the buffer's reservation lock will be held,
914  * and it is the caller's responsibility to call ttm_bo_unreserve.
915  *
916  * If a failure occurs, the function will call the @destroy function. Thus,
917  * after a failure, dereferencing @bo is illegal and will likely cause memory
918  * corruption.
919  *
920  * Returns
921  * -ENOMEM: Out of memory.
922  * -EINVAL: Invalid placement flags.
923  * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
924  */
925 int ttm_bo_init_reserved(struct ttm_device *bdev, struct ttm_buffer_object *bo,
926 			 enum ttm_bo_type type, struct ttm_placement *placement,
927 			 uint32_t alignment, struct ttm_operation_ctx *ctx,
928 			 struct sg_table *sg, struct dma_resv *resv,
929 			 void (*destroy) (struct ttm_buffer_object *))
930 {
931 	int ret;
932 
933 	kref_init(&bo->kref);
934 	bo->bdev = bdev;
935 	bo->type = type;
936 	bo->page_alignment = alignment;
937 	bo->destroy = destroy;
938 	bo->pin_count = 0;
939 	bo->sg = sg;
940 	bo->bulk_move = NULL;
941 	if (resv)
942 		bo->base.resv = resv;
943 	else
944 		bo->base.resv = &bo->base._resv;
945 	atomic_inc(&ttm_glob.bo_count);
946 
947 	/*
948 	 * For ttm_bo_type_device buffers, allocate
949 	 * address space from the device.
950 	 */
951 	if (bo->type == ttm_bo_type_device || bo->type == ttm_bo_type_sg) {
952 		ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
953 					 PFN_UP(bo->base.size));
954 		if (ret)
955 			goto err_put;
956 	}
957 
958 	/* passed reservation objects should already be locked,
959 	 * since otherwise lockdep will be angered in radeon.
960 	 */
961 	if (!resv)
962 		WARN_ON(!dma_resv_trylock(bo->base.resv));
963 	else
964 		dma_resv_assert_held(resv);
965 
966 	ret = ttm_bo_validate(bo, placement, ctx);
967 	if (unlikely(ret))
968 		goto err_unlock;
969 
970 	return 0;
971 
972 err_unlock:
973 	if (!resv)
974 		dma_resv_unlock(bo->base.resv);
975 
976 err_put:
977 	ttm_bo_put(bo);
978 	return ret;
979 }
980 EXPORT_SYMBOL(ttm_bo_init_reserved);
981 
982 /**
983  * ttm_bo_init_validate
984  *
985  * @bdev: Pointer to a ttm_device struct.
986  * @bo: Pointer to a ttm_buffer_object to be initialized.
987  * @type: Requested type of buffer object.
988  * @placement: Initial placement for buffer object.
989  * @alignment: Data alignment in pages.
990  * @interruptible: If needing to sleep to wait for GPU resources,
991  * sleep interruptible.
992  * pinned in physical memory. If this behaviour is not desired, this member
993  * holds a pointer to a persistent shmem object. Typically, this would
994  * point to the shmem object backing a GEM object if TTM is used to back a
995  * GEM user interface.
996  * @sg: Scatter-gather table.
997  * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
998  * @destroy: Destroy function. Use NULL for kfree().
999  *
1000  * This function initializes a pre-allocated struct ttm_buffer_object.
1001  * As this object may be part of a larger structure, this function,
1002  * together with the @destroy function,
1003  * enables driver-specific objects derived from a ttm_buffer_object.
1004  *
1005  * On successful return, the caller owns an object kref to @bo. The kref and
1006  * list_kref are usually set to 1, but note that in some situations, other
1007  * tasks may already be holding references to @bo as well.
1008  *
1009  * If a failure occurs, the function will call the @destroy function, Thus,
1010  * after a failure, dereferencing @bo is illegal and will likely cause memory
1011  * corruption.
1012  *
1013  * Returns
1014  * -ENOMEM: Out of memory.
1015  * -EINVAL: Invalid placement flags.
1016  * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
1017  */
1018 int ttm_bo_init_validate(struct ttm_device *bdev, struct ttm_buffer_object *bo,
1019 			 enum ttm_bo_type type, struct ttm_placement *placement,
1020 			 uint32_t alignment, bool interruptible,
1021 			 struct sg_table *sg, struct dma_resv *resv,
1022 			 void (*destroy) (struct ttm_buffer_object *))
1023 {
1024 	struct ttm_operation_ctx ctx = { interruptible, false };
1025 	int ret;
1026 
1027 	ret = ttm_bo_init_reserved(bdev, bo, type, placement, alignment, &ctx,
1028 				   sg, resv, destroy);
1029 	if (ret)
1030 		return ret;
1031 
1032 	if (!resv)
1033 		ttm_bo_unreserve(bo);
1034 
1035 	return 0;
1036 }
1037 EXPORT_SYMBOL(ttm_bo_init_validate);
1038 
1039 /*
1040  * buffer object vm functions.
1041  */
1042 
1043 /**
1044  * ttm_bo_unmap_virtual
1045  *
1046  * @bo: tear down the virtual mappings for this BO
1047  */
1048 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1049 {
1050 	struct ttm_device *bdev = bo->bdev;
1051 
1052 	drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1053 	ttm_mem_io_free(bdev, bo->resource);
1054 }
1055 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1056 
1057 /**
1058  * ttm_bo_wait_ctx - wait for buffer idle.
1059  *
1060  * @bo:  The buffer object.
1061  * @ctx: defines how to wait
1062  *
1063  * Waits for the buffer to be idle. Used timeout depends on the context.
1064  * Returns -EBUSY if wait timed outt, -ERESTARTSYS if interrupted by a signal or
1065  * zero on success.
1066  */
1067 int ttm_bo_wait_ctx(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx)
1068 {
1069 	long ret;
1070 
1071 	if (ctx->no_wait_gpu) {
1072 		if (dma_resv_test_signaled(bo->base.resv,
1073 					   DMA_RESV_USAGE_BOOKKEEP))
1074 			return 0;
1075 		else
1076 			return -EBUSY;
1077 	}
1078 
1079 	ret = dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
1080 				    ctx->interruptible, 15 * HZ);
1081 	if (unlikely(ret < 0))
1082 		return ret;
1083 	if (unlikely(ret == 0))
1084 		return -EBUSY;
1085 	return 0;
1086 }
1087 EXPORT_SYMBOL(ttm_bo_wait_ctx);
1088 
1089 /**
1090  * struct ttm_bo_swapout_walk - Parameters for the swapout walk
1091  */
1092 struct ttm_bo_swapout_walk {
1093 	/** @walk: The walk base parameters. */
1094 	struct ttm_lru_walk walk;
1095 	/** @gfp_flags: The gfp flags to use for ttm_tt_swapout() */
1096 	gfp_t gfp_flags;
1097 	/** @hit_low: Whether we should attempt to swap BO's with low watermark threshold */
1098 	/** @evict_low: If we cannot swap a bo when @try_low is false (first pass) */
1099 	bool hit_low, evict_low;
1100 };
1101 
1102 static s64
1103 ttm_bo_swapout_cb(struct ttm_lru_walk *walk, struct ttm_buffer_object *bo)
1104 {
1105 	struct ttm_place place = {.mem_type = bo->resource->mem_type};
1106 	struct ttm_bo_swapout_walk *swapout_walk =
1107 		container_of(walk, typeof(*swapout_walk), walk);
1108 	struct ttm_operation_ctx *ctx = walk->ctx;
1109 	s64 ret;
1110 
1111 	/*
1112 	 * While the bo may already reside in SYSTEM placement, set
1113 	 * SYSTEM as new placement to cover also the move further below.
1114 	 * The driver may use the fact that we're moving from SYSTEM
1115 	 * as an indication that we're about to swap out.
1116 	 */
1117 	if (bo->pin_count || !bo->bdev->funcs->eviction_valuable(bo, &place)) {
1118 		ret = -EBUSY;
1119 		goto out;
1120 	}
1121 
1122 	if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
1123 	    bo->ttm->page_flags & TTM_TT_FLAG_EXTERNAL ||
1124 	    bo->ttm->page_flags & TTM_TT_FLAG_SWAPPED) {
1125 		ret = -EBUSY;
1126 		goto out;
1127 	}
1128 
1129 	if (bo->deleted) {
1130 		pgoff_t num_pages = bo->ttm->num_pages;
1131 
1132 		ret = ttm_bo_wait_ctx(bo, ctx);
1133 		if (ret)
1134 			goto out;
1135 
1136 		ttm_bo_cleanup_memtype_use(bo);
1137 		ret = num_pages;
1138 		goto out;
1139 	}
1140 
1141 	/*
1142 	 * Move to system cached
1143 	 */
1144 	if (bo->resource->mem_type != TTM_PL_SYSTEM) {
1145 		struct ttm_resource *evict_mem;
1146 		struct ttm_place hop;
1147 
1148 		memset(&hop, 0, sizeof(hop));
1149 		place.mem_type = TTM_PL_SYSTEM;
1150 		ret = ttm_resource_alloc(bo, &place, &evict_mem, NULL);
1151 		if (ret)
1152 			goto out;
1153 
1154 		ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
1155 		if (ret) {
1156 			WARN(ret == -EMULTIHOP,
1157 			     "Unexpected multihop in swapout - likely driver bug.\n");
1158 			ttm_resource_free(bo, &evict_mem);
1159 			goto out;
1160 		}
1161 	}
1162 
1163 	/*
1164 	 * Make sure BO is idle.
1165 	 */
1166 	ret = ttm_bo_wait_ctx(bo, ctx);
1167 	if (ret)
1168 		goto out;
1169 
1170 	ttm_bo_unmap_virtual(bo);
1171 	if (bo->bdev->funcs->swap_notify)
1172 		bo->bdev->funcs->swap_notify(bo);
1173 
1174 	if (ttm_tt_is_populated(bo->ttm)) {
1175 		spin_lock(&bo->bdev->lru_lock);
1176 		ttm_resource_del_bulk_move(bo->resource, bo);
1177 		spin_unlock(&bo->bdev->lru_lock);
1178 
1179 		ret = ttm_tt_swapout(bo->bdev, bo->ttm, swapout_walk->gfp_flags);
1180 
1181 		spin_lock(&bo->bdev->lru_lock);
1182 		if (ret)
1183 			ttm_resource_add_bulk_move(bo->resource, bo);
1184 		ttm_resource_move_to_lru_tail(bo->resource);
1185 		spin_unlock(&bo->bdev->lru_lock);
1186 	}
1187 
1188 out:
1189 	/* Consider -ENOMEM and -ENOSPC non-fatal. */
1190 	if (ret == -ENOMEM || ret == -ENOSPC)
1191 		ret = -EBUSY;
1192 
1193 	return ret;
1194 }
1195 
1196 const struct ttm_lru_walk_ops ttm_swap_ops = {
1197 	.process_bo = ttm_bo_swapout_cb,
1198 };
1199 
1200 /**
1201  * ttm_bo_swapout() - Swap out buffer objects on the LRU list to shmem.
1202  * @bdev: The ttm device.
1203  * @ctx: The ttm_operation_ctx governing the swapout operation.
1204  * @man: The resource manager whose resources / buffer objects are
1205  * goint to be swapped out.
1206  * @gfp_flags: The gfp flags used for shmem page allocations.
1207  * @target: The desired number of bytes to swap out.
1208  *
1209  * Return: The number of bytes actually swapped out, or negative error code
1210  * on error.
1211  */
1212 s64 ttm_bo_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
1213 		   struct ttm_resource_manager *man, gfp_t gfp_flags,
1214 		   s64 target)
1215 {
1216 	struct ttm_bo_swapout_walk swapout_walk = {
1217 		.walk = {
1218 			.ops = &ttm_swap_ops,
1219 			.ctx = ctx,
1220 			.trylock_only = true,
1221 		},
1222 		.gfp_flags = gfp_flags,
1223 	};
1224 
1225 	return ttm_lru_walk_for_evict(&swapout_walk.walk, bdev, man, target);
1226 }
1227 
1228 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1229 {
1230 	if (bo->ttm == NULL)
1231 		return;
1232 
1233 	ttm_tt_unpopulate(bo->bdev, bo->ttm);
1234 	ttm_tt_destroy(bo->bdev, bo->ttm);
1235 	bo->ttm = NULL;
1236 }
1237 
1238 /**
1239  * ttm_bo_populate() - Ensure that a buffer object has backing pages
1240  * @bo: The buffer object
1241  * @ctx: The ttm_operation_ctx governing the operation.
1242  *
1243  * For buffer objects in a memory type whose manager uses
1244  * struct ttm_tt for backing pages, ensure those backing pages
1245  * are present and with valid content. The bo's resource is also
1246  * placed on the correct LRU list if it was previously swapped
1247  * out.
1248  *
1249  * Return: 0 if successful, negative error code on failure.
1250  * Note: May return -EINTR or -ERESTARTSYS if @ctx::interruptible
1251  * is set to true.
1252  */
1253 int ttm_bo_populate(struct ttm_buffer_object *bo,
1254 		    struct ttm_operation_ctx *ctx)
1255 {
1256 	struct ttm_tt *tt = bo->ttm;
1257 	bool swapped;
1258 	int ret;
1259 
1260 	dma_resv_assert_held(bo->base.resv);
1261 
1262 	if (!tt)
1263 		return 0;
1264 
1265 	swapped = ttm_tt_is_swapped(tt);
1266 	ret = ttm_tt_populate(bo->bdev, tt, ctx);
1267 	if (ret)
1268 		return ret;
1269 
1270 	if (swapped && !ttm_tt_is_swapped(tt) && !bo->pin_count &&
1271 	    bo->resource) {
1272 		spin_lock(&bo->bdev->lru_lock);
1273 		ttm_resource_add_bulk_move(bo->resource, bo);
1274 		ttm_resource_move_to_lru_tail(bo->resource);
1275 		spin_unlock(&bo->bdev->lru_lock);
1276 	}
1277 
1278 	return 0;
1279 }
1280 EXPORT_SYMBOL(ttm_bo_populate);
1281