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