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