xref: /linux/drivers/gpu/drm/ttm/ttm_bo.c (revision b6c0783ff278671e38fed978fefb732101ac8836)
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 
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  */
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  */
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 
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 
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 
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 
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 		dma_fence_enable_sw_signaling(fence);
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 (bdev->funcs->release_notify)
270 			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(&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(&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 /* TODO: remove! */
322 void ttm_bo_put(struct ttm_buffer_object *bo)
323 {
324 	kref_put(&bo->kref, ttm_bo_release);
325 }
326 
327 void ttm_bo_fini(struct ttm_buffer_object *bo)
328 {
329 	ttm_bo_put(bo);
330 }
331 EXPORT_SYMBOL(ttm_bo_fini);
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_resource *evict_mem;
361 	struct ttm_placement placement;
362 	struct ttm_place hop;
363 	int ret = 0;
364 
365 	memset(&hop, 0, sizeof(hop));
366 
367 	dma_resv_assert_held(bo->base.resv);
368 
369 	placement.num_placement = 0;
370 	bo->bdev->funcs->evict_flags(bo, &placement);
371 
372 	if (!placement.num_placement) {
373 		ret = ttm_bo_wait_ctx(bo, ctx);
374 		if (ret)
375 			return ret;
376 
377 		/*
378 		 * Since we've already synced, this frees backing store
379 		 * immediately.
380 		 */
381 		return ttm_bo_pipeline_gutting(bo);
382 	}
383 
384 	ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
385 	if (ret) {
386 		if (ret != -ERESTARTSYS) {
387 			pr_err("Failed to find memory space for buffer 0x%p eviction\n",
388 			       bo);
389 			ttm_bo_mem_space_debug(bo, &placement);
390 		}
391 		goto out;
392 	}
393 
394 	do {
395 		ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
396 		if (ret != -EMULTIHOP)
397 			break;
398 
399 		ret = ttm_bo_bounce_temp_buffer(bo, ctx, &hop);
400 	} while (!ret);
401 
402 	if (ret) {
403 		ttm_resource_free(bo, &evict_mem);
404 		if (ret != -ERESTARTSYS && ret != -EINTR)
405 			pr_err("Buffer eviction failed\n");
406 	}
407 out:
408 	return ret;
409 }
410 
411 /**
412  * ttm_bo_eviction_valuable
413  *
414  * @bo: The buffer object to evict
415  * @place: the placement we need to make room for
416  *
417  * Check if it is valuable to evict the BO to make room for the given placement.
418  */
419 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
420 			      const struct ttm_place *place)
421 {
422 	struct ttm_resource *res = bo->resource;
423 
424 	dma_resv_assert_held(bo->base.resv);
425 
426 	if (res->mem_type == TTM_PL_SYSTEM)
427 		return true;
428 
429 	/* Don't evict this BO if it's outside of the
430 	 * requested placement range
431 	 */
432 	return ttm_resource_intersects(bo->bdev, res, place, bo->base.size);
433 }
434 EXPORT_SYMBOL(ttm_bo_eviction_valuable);
435 
436 /**
437  * ttm_bo_evict_first() - Evict the first bo on the manager's LRU list.
438  * @bdev: The ttm device.
439  * @man: The manager whose bo to evict.
440  * @ctx: The TTM operation ctx governing the eviction.
441  *
442  * Return: 0 if successful or the resource disappeared. Negative error code on error.
443  */
444 int ttm_bo_evict_first(struct ttm_device *bdev, struct ttm_resource_manager *man,
445 		       struct ttm_operation_ctx *ctx)
446 {
447 	struct ttm_resource_cursor cursor;
448 	struct ttm_buffer_object *bo;
449 	struct ttm_resource *res;
450 	unsigned int mem_type;
451 	int ret = 0;
452 
453 	spin_lock(&bdev->lru_lock);
454 	ttm_resource_cursor_init(&cursor, man);
455 	res = ttm_resource_manager_first(&cursor);
456 	ttm_resource_cursor_fini(&cursor);
457 	if (!res) {
458 		ret = -ENOENT;
459 		goto out_no_ref;
460 	}
461 	bo = res->bo;
462 	if (!ttm_bo_get_unless_zero(bo))
463 		goto out_no_ref;
464 	mem_type = res->mem_type;
465 	spin_unlock(&bdev->lru_lock);
466 	ret = ttm_bo_reserve(bo, ctx->interruptible, ctx->no_wait_gpu, NULL);
467 	if (ret)
468 		goto out_no_lock;
469 	if (!bo->resource || bo->resource->mem_type != mem_type)
470 		goto out_bo_moved;
471 
472 	if (bo->deleted) {
473 		ret = ttm_bo_wait_ctx(bo, ctx);
474 		if (!ret)
475 			ttm_bo_cleanup_memtype_use(bo);
476 	} else {
477 		ret = ttm_bo_evict(bo, ctx);
478 	}
479 out_bo_moved:
480 	dma_resv_unlock(bo->base.resv);
481 out_no_lock:
482 	ttm_bo_put(bo);
483 	return ret;
484 
485 out_no_ref:
486 	spin_unlock(&bdev->lru_lock);
487 	return ret;
488 }
489 
490 /**
491  * struct ttm_bo_evict_walk - Parameters for the evict walk.
492  */
493 struct ttm_bo_evict_walk {
494 	/** @walk: The walk base parameters. */
495 	struct ttm_lru_walk walk;
496 	/** @place: The place passed to the resource allocation. */
497 	const struct ttm_place *place;
498 	/** @evictor: The buffer object we're trying to make room for. */
499 	struct ttm_buffer_object *evictor;
500 	/** @res: The allocated resource if any. */
501 	struct ttm_resource **res;
502 	/** @evicted: Number of successful evictions. */
503 	unsigned long evicted;
504 
505 	/** @limit_pool: Which pool limit we should test against */
506 	struct dmem_cgroup_pool_state *limit_pool;
507 	/** @try_low: Whether we should attempt to evict BO's with low watermark threshold */
508 	bool try_low;
509 	/** @hit_low: If we cannot evict a bo when @try_low is false (first pass) */
510 	bool hit_low;
511 };
512 
513 static s64 ttm_bo_evict_cb(struct ttm_lru_walk *walk, struct ttm_buffer_object *bo)
514 {
515 	struct ttm_bo_evict_walk *evict_walk =
516 		container_of(walk, typeof(*evict_walk), walk);
517 	s64 lret;
518 
519 	if (!dmem_cgroup_state_evict_valuable(evict_walk->limit_pool, bo->resource->css,
520 					      evict_walk->try_low, &evict_walk->hit_low))
521 		return 0;
522 
523 	if (bo->pin_count || !bo->bdev->funcs->eviction_valuable(bo, evict_walk->place))
524 		return 0;
525 
526 	if (bo->deleted) {
527 		lret = ttm_bo_wait_ctx(bo, walk->arg.ctx);
528 		if (!lret)
529 			ttm_bo_cleanup_memtype_use(bo);
530 	} else {
531 		lret = ttm_bo_evict(bo, walk->arg.ctx);
532 	}
533 
534 	if (lret)
535 		goto out;
536 
537 	evict_walk->evicted++;
538 	if (evict_walk->res)
539 		lret = ttm_resource_alloc(evict_walk->evictor, evict_walk->place,
540 					  evict_walk->res, NULL);
541 	if (lret == 0)
542 		return 1;
543 out:
544 	/* Errors that should terminate the walk. */
545 	if (lret == -ENOSPC)
546 		return -EBUSY;
547 
548 	return lret;
549 }
550 
551 static const struct ttm_lru_walk_ops ttm_evict_walk_ops = {
552 	.process_bo = ttm_bo_evict_cb,
553 };
554 
555 static int ttm_bo_evict_alloc(struct ttm_device *bdev,
556 			      struct ttm_resource_manager *man,
557 			      const struct ttm_place *place,
558 			      struct ttm_buffer_object *evictor,
559 			      struct ttm_operation_ctx *ctx,
560 			      struct ww_acquire_ctx *ticket,
561 			      struct ttm_resource **res,
562 			      struct dmem_cgroup_pool_state *limit_pool)
563 {
564 	struct ttm_bo_evict_walk evict_walk = {
565 		.walk = {
566 			.ops = &ttm_evict_walk_ops,
567 			.arg = {
568 				.ctx = ctx,
569 				.ticket = ticket,
570 			}
571 		},
572 		.place = place,
573 		.evictor = evictor,
574 		.res = res,
575 		.limit_pool = limit_pool,
576 	};
577 	s64 lret;
578 
579 	evict_walk.walk.arg.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.arg.trylock_only = false;
594 
595 retry:
596 	do {
597 		/* The walk may clear the evict_walk.walk.ticket field */
598 		evict_walk.walk.arg.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 pipelined eviction fencesto the BO as kernel dependency and reserve new
660  * fence slots.
661  */
662 static int ttm_bo_add_pipelined_eviction_fences(struct ttm_buffer_object *bo,
663 						struct ttm_resource_manager *man,
664 						bool no_wait_gpu)
665 {
666 	struct dma_fence *fence;
667 	int i;
668 
669 	spin_lock(&man->eviction_lock);
670 	for (i = 0; i < TTM_NUM_MOVE_FENCES; i++) {
671 		fence = man->eviction_fences[i];
672 		if (!fence)
673 			continue;
674 
675 		if (no_wait_gpu) {
676 			if (!dma_fence_is_signaled(fence)) {
677 				spin_unlock(&man->eviction_lock);
678 				return -EBUSY;
679 			}
680 		} else {
681 			dma_resv_add_fence(bo->base.resv, fence, DMA_RESV_USAGE_KERNEL);
682 		}
683 	}
684 	spin_unlock(&man->eviction_lock);
685 
686 	/* TODO: this call should be removed. */
687 	return dma_resv_reserve_fences(bo->base.resv, 1);
688 }
689 
690 /**
691  * ttm_bo_alloc_resource - Allocate backing store for a BO
692  *
693  * @bo: Pointer to a struct ttm_buffer_object of which we want a resource for
694  * @placement: Proposed new placement for the buffer object
695  * @ctx: if and how to sleep, lock buffers and alloc memory
696  * @force_space: If we should evict buffers to force space
697  * @res: The resulting struct ttm_resource.
698  *
699  * Allocates a resource for the buffer object pointed to by @bo, using the
700  * placement flags in @placement, potentially evicting other buffer objects when
701  * @force_space is true.
702  * This function may sleep while waiting for resources to become available.
703  * Returns:
704  * -EBUSY: No space available (only if no_wait == true).
705  * -ENOSPC: Could not allocate space for the buffer object, either due to
706  * fragmentation or concurrent allocators.
707  * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
708  */
709 static int ttm_bo_alloc_resource(struct ttm_buffer_object *bo,
710 				 struct ttm_placement *placement,
711 				 struct ttm_operation_ctx *ctx,
712 				 bool force_space,
713 				 struct ttm_resource **res)
714 {
715 	struct ttm_device *bdev = bo->bdev;
716 	struct ww_acquire_ctx *ticket;
717 	int i, ret;
718 
719 	ticket = dma_resv_locking_ctx(bo->base.resv);
720 	ret = dma_resv_reserve_fences(bo->base.resv, TTM_NUM_MOVE_FENCES);
721 	if (unlikely(ret))
722 		return ret;
723 
724 	for (i = 0; i < placement->num_placement; ++i) {
725 		const struct ttm_place *place = &placement->placement[i];
726 		struct dmem_cgroup_pool_state *limit_pool = NULL;
727 		struct ttm_resource_manager *man;
728 		bool may_evict;
729 
730 		man = ttm_manager_type(bdev, place->mem_type);
731 		if (!man || !ttm_resource_manager_used(man))
732 			continue;
733 
734 		if (place->flags & (force_space ? TTM_PL_FLAG_DESIRED :
735 				    TTM_PL_FLAG_FALLBACK))
736 			continue;
737 
738 		may_evict = (force_space && place->mem_type != TTM_PL_SYSTEM);
739 		ret = ttm_resource_alloc(bo, place, res, force_space ? &limit_pool : NULL);
740 		if (ret) {
741 			if (ret != -ENOSPC && ret != -EAGAIN) {
742 				dmem_cgroup_pool_state_put(limit_pool);
743 				return ret;
744 			}
745 			if (!may_evict) {
746 				dmem_cgroup_pool_state_put(limit_pool);
747 				continue;
748 			}
749 
750 			ret = ttm_bo_evict_alloc(bdev, man, place, bo, ctx,
751 						 ticket, res, limit_pool);
752 			dmem_cgroup_pool_state_put(limit_pool);
753 			if (ret == -EBUSY)
754 				continue;
755 			if (ret)
756 				return ret;
757 		}
758 
759 		ret = ttm_bo_add_pipelined_eviction_fences(bo, man, ctx->no_wait_gpu);
760 		if (unlikely(ret)) {
761 			ttm_resource_free(bo, res);
762 			if (ret == -EBUSY)
763 				continue;
764 
765 			return ret;
766 		}
767 		return 0;
768 	}
769 
770 	return -ENOSPC;
771 }
772 
773 /*
774  * ttm_bo_mem_space - Wrapper around ttm_bo_alloc_resource
775  *
776  * @bo: Pointer to a struct ttm_buffer_object of which we want a resource for
777  * @placement: Proposed new placement for the buffer object
778  * @res: The resulting struct ttm_resource.
779  * @ctx: if and how to sleep, lock buffers and alloc memory
780  *
781  * Tries both idle allocation and forcefully eviction of buffers. See
782  * ttm_bo_alloc_resource for details.
783  */
784 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
785 		     struct ttm_placement *placement,
786 		     struct ttm_resource **res,
787 		     struct ttm_operation_ctx *ctx)
788 {
789 	bool force_space = false;
790 	int ret;
791 
792 	do {
793 		ret = ttm_bo_alloc_resource(bo, placement, ctx,
794 					    force_space, res);
795 		force_space = !force_space;
796 	} while (ret == -ENOSPC && force_space);
797 
798 	return ret;
799 }
800 EXPORT_SYMBOL(ttm_bo_mem_space);
801 
802 /**
803  * ttm_bo_validate
804  *
805  * @bo: The buffer object.
806  * @placement: Proposed placement for the buffer object.
807  * @ctx: validation parameters.
808  *
809  * Changes placement and caching policy of the buffer object
810  * according proposed placement.
811  * Returns
812  * -EINVAL on invalid proposed placement.
813  * -ENOMEM on out-of-memory condition.
814  * -EBUSY if no_wait is true and buffer busy.
815  * -ERESTARTSYS if interrupted by a signal.
816  */
817 int ttm_bo_validate(struct ttm_buffer_object *bo,
818 		    struct ttm_placement *placement,
819 		    struct ttm_operation_ctx *ctx)
820 {
821 	struct ttm_resource *res;
822 	struct ttm_place hop;
823 	bool force_space;
824 	int ret;
825 
826 	dma_resv_assert_held(bo->base.resv);
827 
828 	/*
829 	 * Remove the backing store if no placement is given.
830 	 */
831 	if (!placement->num_placement)
832 		return ttm_bo_pipeline_gutting(bo);
833 
834 	force_space = false;
835 	do {
836 		/* Check whether we need to move buffer. */
837 		if (bo->resource &&
838 		    ttm_resource_compatible(bo->resource, placement,
839 					    force_space))
840 			return 0;
841 
842 		/* Moving of pinned BOs is forbidden */
843 		if (bo->pin_count)
844 			return -EINVAL;
845 
846 		/*
847 		 * Determine where to move the buffer.
848 		 *
849 		 * If driver determines move is going to need
850 		 * an extra step then it will return -EMULTIHOP
851 		 * and the buffer will be moved to the temporary
852 		 * stop and the driver will be called to make
853 		 * the second hop.
854 		 */
855 		ret = ttm_bo_alloc_resource(bo, placement, ctx, force_space,
856 					    &res);
857 		force_space = !force_space;
858 		if (ret == -ENOSPC)
859 			continue;
860 		if (ret)
861 			return ret;
862 
863 bounce:
864 		ret = ttm_bo_handle_move_mem(bo, res, false, ctx, &hop);
865 		if (ret == -EMULTIHOP) {
866 			ret = ttm_bo_bounce_temp_buffer(bo, ctx, &hop);
867 			/* try and move to final place now. */
868 			if (!ret)
869 				goto bounce;
870 		}
871 		if (ret) {
872 			ttm_resource_free(bo, &res);
873 			return ret;
874 		}
875 
876 	} while (ret && force_space);
877 
878 	/* For backward compatibility with userspace */
879 	if (ret == -ENOSPC)
880 		return bo->bdev->alloc_flags & TTM_ALLOCATION_PROPAGATE_ENOSPC ?
881 		       ret : -ENOMEM;
882 
883 	/*
884 	 * We might need to add a TTM.
885 	 */
886 	if (!bo->resource || bo->resource->mem_type == TTM_PL_SYSTEM) {
887 		ret = ttm_tt_create(bo, true);
888 		if (ret)
889 			return ret;
890 	}
891 	return 0;
892 }
893 EXPORT_SYMBOL(ttm_bo_validate);
894 
895 /**
896  * ttm_bo_init_reserved
897  *
898  * @bdev: Pointer to a ttm_device struct.
899  * @bo: Pointer to a ttm_buffer_object to be initialized.
900  * @type: Requested type of buffer object.
901  * @placement: Initial placement for buffer object.
902  * @alignment: Data alignment in pages.
903  * @ctx: TTM operation context for memory allocation.
904  * @sg: Scatter-gather table.
905  * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
906  * @destroy: Destroy function. Use NULL for kfree().
907  *
908  * This function initializes a pre-allocated struct ttm_buffer_object.
909  * As this object may be part of a larger structure, this function,
910  * together with the @destroy function, enables driver-specific objects
911  * derived from a ttm_buffer_object.
912  *
913  * On successful return, the caller owns an object kref to @bo. The kref and
914  * list_kref are usually set to 1, but note that in some situations, other
915  * tasks may already be holding references to @bo as well.
916  * Furthermore, if resv == NULL, the buffer's reservation lock will be held,
917  * and it is the caller's responsibility to call ttm_bo_unreserve.
918  *
919  * If a failure occurs, the function will call the @destroy function. Thus,
920  * after a failure, dereferencing @bo is illegal and will likely cause memory
921  * corruption.
922  *
923  * Returns
924  * -ENOMEM: Out of memory.
925  * -EINVAL: Invalid placement flags.
926  * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
927  */
928 int ttm_bo_init_reserved(struct ttm_device *bdev, struct ttm_buffer_object *bo,
929 			 enum ttm_bo_type type, struct ttm_placement *placement,
930 			 uint32_t alignment, struct ttm_operation_ctx *ctx,
931 			 struct sg_table *sg, struct dma_resv *resv,
932 			 void (*destroy) (struct ttm_buffer_object *))
933 {
934 	int ret;
935 
936 	kref_init(&bo->kref);
937 	bo->bdev = bdev;
938 	bo->type = type;
939 	bo->page_alignment = alignment;
940 	bo->destroy = destroy;
941 	bo->pin_count = 0;
942 	bo->sg = sg;
943 	bo->bulk_move = NULL;
944 	if (resv)
945 		bo->base.resv = resv;
946 	else
947 		bo->base.resv = &bo->base._resv;
948 	atomic_inc(&ttm_glob.bo_count);
949 
950 	/*
951 	 * For ttm_bo_type_device buffers, allocate
952 	 * address space from the device.
953 	 */
954 	if (bo->type == ttm_bo_type_device || bo->type == ttm_bo_type_sg) {
955 		ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
956 					 PFN_UP(bo->base.size));
957 		if (ret)
958 			goto err_put;
959 	}
960 
961 	/* passed reservation objects should already be locked,
962 	 * since otherwise lockdep will be angered in radeon.
963 	 */
964 	if (!resv)
965 		WARN_ON(!dma_resv_trylock(bo->base.resv));
966 	else
967 		dma_resv_assert_held(resv);
968 
969 	ret = ttm_bo_validate(bo, placement, ctx);
970 	if (unlikely(ret))
971 		goto err_unlock;
972 
973 	return 0;
974 
975 err_unlock:
976 	if (!resv)
977 		dma_resv_unlock(bo->base.resv);
978 
979 err_put:
980 	ttm_bo_put(bo);
981 	return ret;
982 }
983 EXPORT_SYMBOL(ttm_bo_init_reserved);
984 
985 /**
986  * ttm_bo_init_validate
987  *
988  * @bdev: Pointer to a ttm_device struct.
989  * @bo: Pointer to a ttm_buffer_object to be initialized.
990  * @type: Requested type of buffer object.
991  * @placement: Initial placement for buffer object.
992  * @alignment: Data alignment in pages.
993  * @interruptible: If needing to sleep to wait for GPU resources,
994  * sleep interruptible.
995  * pinned in physical memory. If this behaviour is not desired, this member
996  * holds a pointer to a persistent shmem object. Typically, this would
997  * point to the shmem object backing a GEM object if TTM is used to back a
998  * GEM user interface.
999  * @sg: Scatter-gather table.
1000  * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
1001  * @destroy: Destroy function. Use NULL for kfree().
1002  *
1003  * This function initializes a pre-allocated struct ttm_buffer_object.
1004  * As this object may be part of a larger structure, this function,
1005  * together with the @destroy function,
1006  * enables driver-specific objects derived from a ttm_buffer_object.
1007  *
1008  * On successful return, the caller owns an object kref to @bo. The kref and
1009  * list_kref are usually set to 1, but note that in some situations, other
1010  * tasks may already be holding references to @bo as well.
1011  *
1012  * If a failure occurs, the function will call the @destroy function, Thus,
1013  * after a failure, dereferencing @bo is illegal and will likely cause memory
1014  * corruption.
1015  *
1016  * Returns
1017  * -ENOMEM: Out of memory.
1018  * -EINVAL: Invalid placement flags.
1019  * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
1020  */
1021 int ttm_bo_init_validate(struct ttm_device *bdev, struct ttm_buffer_object *bo,
1022 			 enum ttm_bo_type type, struct ttm_placement *placement,
1023 			 uint32_t alignment, bool interruptible,
1024 			 struct sg_table *sg, struct dma_resv *resv,
1025 			 void (*destroy) (struct ttm_buffer_object *))
1026 {
1027 	struct ttm_operation_ctx ctx = { .interruptible = interruptible };
1028 	int ret;
1029 
1030 	ret = ttm_bo_init_reserved(bdev, bo, type, placement, alignment, &ctx,
1031 				   sg, resv, destroy);
1032 	if (ret)
1033 		return ret;
1034 
1035 	if (!resv)
1036 		ttm_bo_unreserve(bo);
1037 
1038 	return 0;
1039 }
1040 EXPORT_SYMBOL(ttm_bo_init_validate);
1041 
1042 /*
1043  * buffer object vm functions.
1044  */
1045 
1046 /**
1047  * ttm_bo_unmap_virtual
1048  *
1049  * @bo: tear down the virtual mappings for this BO
1050  */
1051 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
1052 {
1053 	struct ttm_device *bdev = bo->bdev;
1054 
1055 	drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
1056 	ttm_mem_io_free(bdev, bo->resource);
1057 }
1058 EXPORT_SYMBOL(ttm_bo_unmap_virtual);
1059 
1060 /**
1061  * ttm_bo_wait_ctx - wait for buffer idle.
1062  *
1063  * @bo:  The buffer object.
1064  * @ctx: defines how to wait
1065  *
1066  * Waits for the buffer to be idle. Used timeout depends on the context.
1067  * Returns -EBUSY if wait timed outt, -ERESTARTSYS if interrupted by a signal or
1068  * zero on success.
1069  */
1070 int ttm_bo_wait_ctx(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx)
1071 {
1072 	long ret;
1073 
1074 	if (ctx->no_wait_gpu) {
1075 		if (dma_resv_test_signaled(bo->base.resv,
1076 					   DMA_RESV_USAGE_BOOKKEEP))
1077 			return 0;
1078 		else
1079 			return -EBUSY;
1080 	}
1081 
1082 	ret = dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
1083 				    ctx->interruptible, 15 * HZ);
1084 	if (unlikely(ret < 0))
1085 		return ret;
1086 	if (unlikely(ret == 0))
1087 		return -EBUSY;
1088 	return 0;
1089 }
1090 EXPORT_SYMBOL(ttm_bo_wait_ctx);
1091 
1092 /**
1093  * struct ttm_bo_swapout_walk - Parameters for the swapout walk
1094  */
1095 struct ttm_bo_swapout_walk {
1096 	/** @walk: The walk base parameters. */
1097 	struct ttm_lru_walk walk;
1098 	/** @gfp_flags: The gfp flags to use for ttm_tt_swapout() */
1099 	gfp_t gfp_flags;
1100 	/** @hit_low: Whether we should attempt to swap BO's with low watermark threshold */
1101 	/** @evict_low: If we cannot swap a bo when @try_low is false (first pass) */
1102 	bool hit_low, evict_low;
1103 };
1104 
1105 static s64
1106 ttm_bo_swapout_cb(struct ttm_lru_walk *walk, struct ttm_buffer_object *bo)
1107 {
1108 	struct ttm_place place = { .mem_type = bo->resource->mem_type };
1109 	struct ttm_bo_swapout_walk *swapout_walk =
1110 		container_of(walk, typeof(*swapout_walk), walk);
1111 	struct ttm_operation_ctx *ctx = walk->arg.ctx;
1112 	struct ttm_device *bdev = bo->bdev;
1113 	struct ttm_tt *tt = bo->ttm;
1114 	s64 ret;
1115 
1116 	/*
1117 	 * While the bo may already reside in SYSTEM placement, set
1118 	 * SYSTEM as new placement to cover also the move further below.
1119 	 * The driver may use the fact that we're moving from SYSTEM
1120 	 * as an indication that we're about to swap out.
1121 	 */
1122 	if (bo->pin_count || !bdev->funcs->eviction_valuable(bo, &place)) {
1123 		ret = -EBUSY;
1124 		goto out;
1125 	}
1126 
1127 	if (!tt || !ttm_tt_is_populated(tt) ||
1128 	    tt->page_flags & (TTM_TT_FLAG_EXTERNAL | TTM_TT_FLAG_SWAPPED)) {
1129 		ret = -EBUSY;
1130 		goto out;
1131 	}
1132 
1133 	if (bo->deleted) {
1134 		pgoff_t num_pages = tt->num_pages;
1135 
1136 		ret = ttm_bo_wait_ctx(bo, ctx);
1137 		if (ret)
1138 			goto out;
1139 
1140 		ttm_bo_cleanup_memtype_use(bo);
1141 		ret = num_pages;
1142 		goto out;
1143 	}
1144 
1145 	/*
1146 	 * Move to system cached
1147 	 */
1148 	if (bo->resource->mem_type != TTM_PL_SYSTEM) {
1149 		struct ttm_resource *evict_mem;
1150 		struct ttm_place hop;
1151 
1152 		memset(&hop, 0, sizeof(hop));
1153 		place.mem_type = TTM_PL_SYSTEM;
1154 		ret = ttm_resource_alloc(bo, &place, &evict_mem, NULL);
1155 		if (ret)
1156 			goto out;
1157 
1158 		ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
1159 		if (ret) {
1160 			WARN(ret == -EMULTIHOP,
1161 			     "Unexpected multihop in swapout - likely driver bug.\n");
1162 			ttm_resource_free(bo, &evict_mem);
1163 			goto out;
1164 		}
1165 	}
1166 
1167 	/*
1168 	 * Make sure BO is idle.
1169 	 */
1170 	ret = ttm_bo_wait_ctx(bo, ctx);
1171 	if (ret)
1172 		goto out;
1173 
1174 	ttm_bo_unmap_virtual(bo);
1175 	if (bdev->funcs->swap_notify)
1176 		bdev->funcs->swap_notify(bo);
1177 
1178 	if (ttm_tt_is_populated(tt)) {
1179 		spin_lock(&bdev->lru_lock);
1180 		ttm_resource_del_bulk_move(bo->resource, bo);
1181 		spin_unlock(&bdev->lru_lock);
1182 
1183 		ret = ttm_tt_swapout(bdev, tt, swapout_walk->gfp_flags);
1184 
1185 		spin_lock(&bdev->lru_lock);
1186 		if (ret)
1187 			ttm_resource_add_bulk_move(bo->resource, bo);
1188 		ttm_resource_move_to_lru_tail(bo->resource);
1189 		spin_unlock(&bdev->lru_lock);
1190 	}
1191 
1192 out:
1193 	/* Consider -ENOMEM and -ENOSPC non-fatal. */
1194 	if (ret == -ENOMEM || ret == -ENOSPC)
1195 		ret = -EBUSY;
1196 
1197 	return ret;
1198 }
1199 
1200 const struct ttm_lru_walk_ops ttm_swap_ops = {
1201 	.process_bo = ttm_bo_swapout_cb,
1202 };
1203 
1204 /**
1205  * ttm_bo_swapout() - Swap out buffer objects on the LRU list to shmem.
1206  * @bdev: The ttm device.
1207  * @ctx: The ttm_operation_ctx governing the swapout operation.
1208  * @man: The resource manager whose resources / buffer objects are
1209  * goint to be swapped out.
1210  * @gfp_flags: The gfp flags used for shmem page allocations.
1211  * @target: The desired number of bytes to swap out.
1212  *
1213  * Return: The number of bytes actually swapped out, or negative error code
1214  * on error.
1215  */
1216 s64 ttm_bo_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
1217 		   struct ttm_resource_manager *man, gfp_t gfp_flags,
1218 		   s64 target)
1219 {
1220 	struct ttm_bo_swapout_walk swapout_walk = {
1221 		.walk = {
1222 			.ops = &ttm_swap_ops,
1223 			.arg = {
1224 				.ctx = ctx,
1225 				.trylock_only = true,
1226 			},
1227 		},
1228 		.gfp_flags = gfp_flags,
1229 	};
1230 
1231 	return ttm_lru_walk_for_evict(&swapout_walk.walk, bdev, man, target);
1232 }
1233 
1234 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
1235 {
1236 	if (bo->ttm == NULL)
1237 		return;
1238 
1239 	ttm_tt_unpopulate(bo->bdev, bo->ttm);
1240 	ttm_tt_destroy(bo->bdev, bo->ttm);
1241 	bo->ttm = NULL;
1242 }
1243 
1244 /**
1245  * ttm_bo_populate() - Ensure that a buffer object has backing pages
1246  * @bo: The buffer object
1247  * @ctx: The ttm_operation_ctx governing the operation.
1248  *
1249  * For buffer objects in a memory type whose manager uses
1250  * struct ttm_tt for backing pages, ensure those backing pages
1251  * are present and with valid content. The bo's resource is also
1252  * placed on the correct LRU list if it was previously swapped
1253  * out.
1254  *
1255  * Return: 0 if successful, negative error code on failure.
1256  * Note: May return -EINTR or -ERESTARTSYS if @ctx::interruptible
1257  * is set to true.
1258  */
1259 int ttm_bo_populate(struct ttm_buffer_object *bo,
1260 		    struct ttm_operation_ctx *ctx)
1261 {
1262 	struct ttm_device *bdev = bo->bdev;
1263 	struct ttm_tt *tt = bo->ttm;
1264 	bool swapped;
1265 	int ret;
1266 
1267 	dma_resv_assert_held(bo->base.resv);
1268 
1269 	if (!tt)
1270 		return 0;
1271 
1272 	swapped = ttm_tt_is_swapped(tt);
1273 	ret = ttm_tt_populate(bdev, tt, ctx);
1274 	if (ret)
1275 		return ret;
1276 
1277 	if (swapped && !ttm_tt_is_swapped(tt) && !bo->pin_count &&
1278 	    bo->resource) {
1279 		spin_lock(&bdev->lru_lock);
1280 		ttm_resource_add_bulk_move(bo->resource, bo);
1281 		ttm_resource_move_to_lru_tail(bo->resource);
1282 		spin_unlock(&bdev->lru_lock);
1283 	}
1284 
1285 	return 0;
1286 }
1287 EXPORT_SYMBOL(ttm_bo_populate);
1288 
1289 int ttm_bo_setup_export(struct ttm_buffer_object *bo,
1290 			struct ttm_operation_ctx *ctx)
1291 {
1292 	int ret;
1293 
1294 	ret = ttm_bo_reserve(bo, false, false, NULL);
1295 	if (ret != 0)
1296 		return ret;
1297 
1298 	ret = ttm_bo_populate(bo, ctx);
1299 	ttm_bo_unreserve(bo);
1300 	return ret;
1301 }
1302 EXPORT_SYMBOL(ttm_bo_setup_export);
1303