xref: /openbsd/sys/dev/pci/drm/ttm/ttm_bo_util.c (revision d415bd75)
1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
3  *
4  * Copyright (c) 2007-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 #include <drm/ttm/ttm_bo_driver.h>
33 #include <drm/ttm/ttm_placement.h>
34 #include <drm/drm_cache.h>
35 #include <drm/drm_vma_manager.h>
36 #include <linux/iosys-map.h>
37 #include <linux/io.h>
38 #include <linux/highmem.h>
39 #include <linux/wait.h>
40 #include <linux/slab.h>
41 #include <linux/vmalloc.h>
42 #include <linux/module.h>
43 #include <linux/dma-resv.h>
44 
45 struct ttm_transfer_obj {
46 	struct ttm_buffer_object base;
47 	struct ttm_buffer_object *bo;
48 };
49 
50 int ttm_mem_io_reserve(struct ttm_device *bdev,
51 		       struct ttm_resource *mem)
52 {
53 	if (mem->bus.offset || mem->bus.addr)
54 		return 0;
55 
56 	mem->bus.is_iomem = false;
57 	if (!bdev->funcs->io_mem_reserve)
58 		return 0;
59 
60 	return bdev->funcs->io_mem_reserve(bdev, mem);
61 }
62 
63 void ttm_mem_io_free(struct ttm_device *bdev,
64 		     struct ttm_resource *mem)
65 {
66 	if (!mem)
67 		return;
68 
69 	if (!mem->bus.offset && !mem->bus.addr)
70 		return;
71 
72 	if (bdev->funcs->io_mem_free)
73 		bdev->funcs->io_mem_free(bdev, mem);
74 
75 	mem->bus.offset = 0;
76 	mem->bus.addr = NULL;
77 }
78 
79 /**
80  * ttm_move_memcpy - Helper to perform a memcpy ttm move operation.
81  * @clear: Whether to clear rather than copy.
82  * @num_pages: Number of pages of the operation.
83  * @dst_iter: A struct ttm_kmap_iter representing the destination resource.
84  * @src_iter: A struct ttm_kmap_iter representing the source resource.
85  *
86  * This function is intended to be able to move out async under a
87  * dma-fence if desired.
88  */
89 void ttm_move_memcpy(bool clear,
90 		     u32 num_pages,
91 		     struct ttm_kmap_iter *dst_iter,
92 		     struct ttm_kmap_iter *src_iter,
93 		     bus_space_tag_t memt)
94 {
95 	const struct ttm_kmap_iter_ops *dst_ops = dst_iter->ops;
96 	const struct ttm_kmap_iter_ops *src_ops = src_iter->ops;
97 	struct iosys_map src_map, dst_map;
98 	pgoff_t i;
99 
100 	/* Single TTM move. NOP */
101 	if (dst_ops->maps_tt && src_ops->maps_tt)
102 		return;
103 
104 	/* Don't move nonexistent data. Clear destination instead. */
105 	if (clear) {
106 		for (i = 0; i < num_pages; ++i) {
107 			dst_ops->map_local(dst_iter, &dst_map, i, memt);
108 			if (dst_map.is_iomem)
109 				memset_io(dst_map.vaddr_iomem, 0, PAGE_SIZE);
110 			else
111 				memset(dst_map.vaddr, 0, PAGE_SIZE);
112 			if (dst_ops->unmap_local)
113 				dst_ops->unmap_local(dst_iter, &dst_map, memt);
114 		}
115 		return;
116 	}
117 
118 	for (i = 0; i < num_pages; ++i) {
119 		dst_ops->map_local(dst_iter, &dst_map, i, memt);
120 		src_ops->map_local(src_iter, &src_map, i, memt);
121 
122 		drm_memcpy_from_wc(&dst_map, &src_map, PAGE_SIZE);
123 
124 		if (src_ops->unmap_local)
125 			src_ops->unmap_local(src_iter, &src_map, memt);
126 		if (dst_ops->unmap_local)
127 			dst_ops->unmap_local(dst_iter, &dst_map, memt);
128 	}
129 }
130 EXPORT_SYMBOL(ttm_move_memcpy);
131 
132 int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
133 		       struct ttm_operation_ctx *ctx,
134 		       struct ttm_resource *dst_mem)
135 {
136 	struct ttm_device *bdev = bo->bdev;
137 	struct ttm_resource_manager *dst_man =
138 		ttm_manager_type(bo->bdev, dst_mem->mem_type);
139 	struct ttm_tt *ttm = bo->ttm;
140 	struct ttm_resource *src_mem = bo->resource;
141 	struct ttm_resource_manager *src_man;
142 	union {
143 		struct ttm_kmap_iter_tt tt;
144 		struct ttm_kmap_iter_linear_io io;
145 	} _dst_iter, _src_iter;
146 	struct ttm_kmap_iter *dst_iter, *src_iter;
147 	bool clear;
148 	int ret = 0;
149 
150 	if (!src_mem)
151 		return 0;
152 
153 	src_man = ttm_manager_type(bdev, src_mem->mem_type);
154 	if (ttm && ((ttm->page_flags & TTM_TT_FLAG_SWAPPED) ||
155 		    dst_man->use_tt)) {
156 		ret = ttm_tt_populate(bdev, ttm, ctx);
157 		if (ret)
158 			return ret;
159 	}
160 
161 	dst_iter = ttm_kmap_iter_linear_io_init(&_dst_iter.io, bdev, dst_mem);
162 	if (PTR_ERR(dst_iter) == -EINVAL && dst_man->use_tt)
163 		dst_iter = ttm_kmap_iter_tt_init(&_dst_iter.tt, bo->ttm);
164 	if (IS_ERR(dst_iter))
165 		return PTR_ERR(dst_iter);
166 
167 	src_iter = ttm_kmap_iter_linear_io_init(&_src_iter.io, bdev, src_mem);
168 	if (PTR_ERR(src_iter) == -EINVAL && src_man->use_tt)
169 		src_iter = ttm_kmap_iter_tt_init(&_src_iter.tt, bo->ttm);
170 	if (IS_ERR(src_iter)) {
171 		ret = PTR_ERR(src_iter);
172 		goto out_src_iter;
173 	}
174 
175 	clear = src_iter->ops->maps_tt && (!ttm || !ttm_tt_is_populated(ttm));
176 	if (!(clear && ttm && !(ttm->page_flags & TTM_TT_FLAG_ZERO_ALLOC)))
177 		ttm_move_memcpy(clear, dst_mem->num_pages, dst_iter, src_iter,
178 		    bdev->memt);
179 
180 	if (!src_iter->ops->maps_tt)
181 		ttm_kmap_iter_linear_io_fini(&_src_iter.io, bdev, src_mem);
182 	ttm_bo_move_sync_cleanup(bo, dst_mem);
183 
184 out_src_iter:
185 	if (!dst_iter->ops->maps_tt)
186 		ttm_kmap_iter_linear_io_fini(&_dst_iter.io, bdev, dst_mem);
187 
188 	return ret;
189 }
190 EXPORT_SYMBOL(ttm_bo_move_memcpy);
191 
192 static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
193 {
194 	struct ttm_transfer_obj *fbo;
195 
196 	fbo = container_of(bo, struct ttm_transfer_obj, base);
197 	dma_resv_fini(&fbo->base.base._resv);
198 	ttm_bo_put(fbo->bo);
199 	kfree(fbo);
200 }
201 
202 /**
203  * ttm_buffer_object_transfer
204  *
205  * @bo: A pointer to a struct ttm_buffer_object.
206  * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
207  * holding the data of @bo with the old placement.
208  *
209  * This is a utility function that may be called after an accelerated move
210  * has been scheduled. A new buffer object is created as a placeholder for
211  * the old data while it's being copied. When that buffer object is idle,
212  * it can be destroyed, releasing the space of the old placement.
213  * Returns:
214  * !0: Failure.
215  */
216 
217 static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
218 				      struct ttm_buffer_object **new_obj)
219 {
220 	struct ttm_transfer_obj *fbo;
221 	int ret;
222 
223 	fbo = kmalloc(sizeof(*fbo), GFP_KERNEL);
224 	if (!fbo)
225 		return -ENOMEM;
226 
227 	fbo->base = *bo;
228 
229 	/**
230 	 * Fix up members that we shouldn't copy directly:
231 	 * TODO: Explicit member copy would probably be better here.
232 	 */
233 
234 	atomic_inc(&ttm_glob.bo_count);
235 	INIT_LIST_HEAD(&fbo->base.ddestroy);
236 	drm_vma_node_reset(&fbo->base.base.vma_node);
237 
238 	kref_init(&fbo->base.kref);
239 	fbo->base.destroy = &ttm_transfered_destroy;
240 	fbo->base.pin_count = 0;
241 	if (bo->type != ttm_bo_type_sg)
242 		fbo->base.base.resv = &fbo->base.base._resv;
243 
244 	dma_resv_init(&fbo->base.base._resv);
245 	fbo->base.base.dev = NULL;
246 	ret = dma_resv_trylock(&fbo->base.base._resv);
247 	WARN_ON(!ret);
248 
249 	if (fbo->base.resource) {
250 		ttm_resource_set_bo(fbo->base.resource, &fbo->base);
251 		bo->resource = NULL;
252 		ttm_bo_set_bulk_move(&fbo->base, NULL);
253 	} else {
254 		fbo->base.bulk_move = NULL;
255 	}
256 
257 	ret = dma_resv_reserve_fences(&fbo->base.base._resv, 1);
258 	if (ret) {
259 		kfree(fbo);
260 		return ret;
261 	}
262 
263 	ttm_bo_get(bo);
264 	fbo->bo = bo;
265 
266 	ttm_bo_move_to_lru_tail_unlocked(&fbo->base);
267 
268 	*new_obj = &fbo->base;
269 	return 0;
270 }
271 
272 pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res,
273 		     pgprot_t tmp)
274 {
275 	struct ttm_resource_manager *man;
276 	enum ttm_caching caching;
277 
278 	man = ttm_manager_type(bo->bdev, res->mem_type);
279 	caching = man->use_tt ? bo->ttm->caching : res->bus.caching;
280 
281 	return ttm_prot_from_caching(caching, tmp);
282 }
283 EXPORT_SYMBOL(ttm_io_prot);
284 
285 static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
286 			  unsigned long offset,
287 			  unsigned long size,
288 			  struct ttm_bo_kmap_obj *map)
289 {
290 	int flags;
291 	struct ttm_resource *mem = bo->resource;
292 
293 	if (bo->resource->bus.addr) {
294 		map->bo_kmap_type = ttm_bo_map_premapped;
295 		map->virtual = ((u8 *)bo->resource->bus.addr) + offset;
296 	} else {
297 		map->bo_kmap_type = ttm_bo_map_iomap;
298 		if (mem->bus.caching == ttm_write_combined)
299 			flags = BUS_SPACE_MAP_PREFETCHABLE;
300 #ifdef CONFIG_X86
301 		else if (mem->bus.caching == ttm_cached)
302 			flags = BUS_SPACE_MAP_CACHEABLE;
303 #endif
304 		else
305 			flags = 0;
306 		if (bus_space_map(bo->bdev->memt,
307 		    bo->resource->bus.offset + offset,
308 		    size, BUS_SPACE_MAP_LINEAR | flags,
309 		    &bo->resource->bus.bsh)) {
310 			printf("%s bus_space_map failed\n", __func__);
311 			map->virtual = 0;
312 		} else {
313 			map->virtual = bus_space_vaddr(bo->bdev->memt,
314 			    bo->resource->bus.bsh);
315 		}
316 	}
317 	return (!map->virtual) ? -ENOMEM : 0;
318 }
319 
320 static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
321 			   unsigned long start_page,
322 			   unsigned long num_pages,
323 			   struct ttm_bo_kmap_obj *map)
324 {
325 	struct ttm_resource *mem = bo->resource;
326 	struct ttm_operation_ctx ctx = {
327 		.interruptible = false,
328 		.no_wait_gpu = false
329 	};
330 	struct ttm_tt *ttm = bo->ttm;
331 	pgprot_t prot;
332 	int ret;
333 
334 	BUG_ON(!ttm);
335 
336 	ret = ttm_tt_populate(bo->bdev, ttm, &ctx);
337 	if (ret)
338 		return ret;
339 
340 	if (num_pages == 1 && ttm->caching == ttm_cached) {
341 		/*
342 		 * We're mapping a single page, and the desired
343 		 * page protection is consistent with the bo.
344 		 */
345 
346 		map->bo_kmap_type = ttm_bo_map_kmap;
347 		map->page = ttm->pages[start_page];
348 		map->virtual = kmap(map->page);
349 	} else {
350 		/*
351 		 * We need to use vmap to get the desired page protection
352 		 * or to make the buffer object look contiguous.
353 		 */
354 		prot = ttm_io_prot(bo, mem, PAGE_KERNEL);
355 		map->bo_kmap_type = ttm_bo_map_vmap;
356 		map->virtual = vmap(ttm->pages + start_page, num_pages,
357 				    0, prot);
358 	}
359 	return (!map->virtual) ? -ENOMEM : 0;
360 }
361 
362 int ttm_bo_kmap(struct ttm_buffer_object *bo,
363 		unsigned long start_page, unsigned long num_pages,
364 		struct ttm_bo_kmap_obj *map)
365 {
366 	unsigned long offset, size;
367 	int ret;
368 
369 	map->virtual = NULL;
370 	map->bo = bo;
371 	if (num_pages > bo->resource->num_pages)
372 		return -EINVAL;
373 	if ((start_page + num_pages) > bo->resource->num_pages)
374 		return -EINVAL;
375 
376 	ret = ttm_mem_io_reserve(bo->bdev, bo->resource);
377 	if (ret)
378 		return ret;
379 	if (!bo->resource->bus.is_iomem) {
380 		return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
381 	} else {
382 		offset = start_page << PAGE_SHIFT;
383 		size = num_pages << PAGE_SHIFT;
384 		return ttm_bo_ioremap(bo, offset, size, map);
385 	}
386 }
387 EXPORT_SYMBOL(ttm_bo_kmap);
388 
389 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
390 {
391 	if (!map->virtual)
392 		return;
393 	switch (map->bo_kmap_type) {
394 	case ttm_bo_map_iomap:
395 		bus_space_unmap(map->bo->bdev->memt, map->bo->resource->bus.bsh,
396 		    (size_t)map->bo->resource->num_pages << PAGE_SHIFT);
397 		break;
398 	case ttm_bo_map_vmap:
399 		vunmap(map->virtual,
400 		    (size_t)map->bo->resource->num_pages << PAGE_SHIFT);
401 		break;
402 	case ttm_bo_map_kmap:
403 		kunmap_va(map->virtual);
404 		break;
405 	case ttm_bo_map_premapped:
406 		break;
407 	default:
408 		BUG();
409 	}
410 	ttm_mem_io_free(map->bo->bdev, map->bo->resource);
411 	map->virtual = NULL;
412 	map->page = NULL;
413 }
414 EXPORT_SYMBOL(ttm_bo_kunmap);
415 
416 int ttm_bo_vmap(struct ttm_buffer_object *bo, struct iosys_map *map)
417 {
418 	int flags;
419 	struct ttm_resource *mem = bo->resource;
420 	int ret;
421 
422 	dma_resv_assert_held(bo->base.resv);
423 
424 	ret = ttm_mem_io_reserve(bo->bdev, mem);
425 	if (ret)
426 		return ret;
427 
428 	if (mem->bus.is_iomem) {
429 		void __iomem *vaddr_iomem;
430 
431 		if (mem->bus.addr)
432 			vaddr_iomem = (void __iomem *)mem->bus.addr;
433 		else {
434 			if (mem->bus.caching == ttm_write_combined)
435 				flags = BUS_SPACE_MAP_PREFETCHABLE;
436 #ifdef CONFIG_X86
437 			else if (mem->bus.caching == ttm_cached)
438 				flags = BUS_SPACE_MAP_CACHEABLE;
439 #endif
440 			else
441 				flags = 0;
442 			if (bus_space_map(bo->bdev->memt, mem->bus.offset,
443 			    bo->base.size, BUS_SPACE_MAP_LINEAR | flags,
444 			    &mem->bus.bsh)) {
445 				printf("%s bus_space_map failed\n", __func__);
446 				return -ENOMEM;
447 			}
448 			vaddr_iomem = bus_space_vaddr(bo->bdev->memt,
449 			    mem->bus.bsh);
450 		}
451 
452 		if (!vaddr_iomem)
453 			return -ENOMEM;
454 
455 		iosys_map_set_vaddr_iomem(map, vaddr_iomem);
456 
457 	} else {
458 		struct ttm_operation_ctx ctx = {
459 			.interruptible = false,
460 			.no_wait_gpu = false
461 		};
462 		struct ttm_tt *ttm = bo->ttm;
463 		pgprot_t prot;
464 		void *vaddr;
465 
466 		ret = ttm_tt_populate(bo->bdev, ttm, &ctx);
467 		if (ret)
468 			return ret;
469 
470 		/*
471 		 * We need to use vmap to get the desired page protection
472 		 * or to make the buffer object look contiguous.
473 		 */
474 		prot = ttm_io_prot(bo, mem, PAGE_KERNEL);
475 		vaddr = vmap(ttm->pages, ttm->num_pages, 0, prot);
476 		if (!vaddr)
477 			return -ENOMEM;
478 
479 		iosys_map_set_vaddr(map, vaddr);
480 	}
481 
482 	return 0;
483 }
484 EXPORT_SYMBOL(ttm_bo_vmap);
485 
486 void ttm_bo_vunmap(struct ttm_buffer_object *bo, struct iosys_map *map)
487 {
488 	struct ttm_resource *mem = bo->resource;
489 
490 	dma_resv_assert_held(bo->base.resv);
491 
492 	if (iosys_map_is_null(map))
493 		return;
494 
495 	if (!map->is_iomem)
496 		vunmap(map->vaddr,
497 		    (size_t)mem->num_pages << PAGE_SHIFT);
498 	else if (!mem->bus.addr)
499 		bus_space_unmap(bo->bdev->memt, mem->bus.bsh,
500 		    (size_t)mem->num_pages << PAGE_SHIFT);
501 	iosys_map_clear(map);
502 
503 	ttm_mem_io_free(bo->bdev, bo->resource);
504 }
505 EXPORT_SYMBOL(ttm_bo_vunmap);
506 
507 static int ttm_bo_wait_free_node(struct ttm_buffer_object *bo,
508 				 bool dst_use_tt)
509 {
510 	int ret;
511 	ret = ttm_bo_wait(bo, false, false);
512 	if (ret)
513 		return ret;
514 
515 	if (!dst_use_tt)
516 		ttm_bo_tt_destroy(bo);
517 	ttm_resource_free(bo, &bo->resource);
518 	return 0;
519 }
520 
521 static int ttm_bo_move_to_ghost(struct ttm_buffer_object *bo,
522 				struct dma_fence *fence,
523 				bool dst_use_tt)
524 {
525 	struct ttm_buffer_object *ghost_obj;
526 	int ret;
527 
528 	/**
529 	 * This should help pipeline ordinary buffer moves.
530 	 *
531 	 * Hang old buffer memory on a new buffer object,
532 	 * and leave it to be released when the GPU
533 	 * operation has completed.
534 	 */
535 
536 	ret = ttm_buffer_object_transfer(bo, &ghost_obj);
537 	if (ret)
538 		return ret;
539 
540 	dma_resv_add_fence(&ghost_obj->base._resv, fence,
541 			   DMA_RESV_USAGE_KERNEL);
542 
543 	/**
544 	 * If we're not moving to fixed memory, the TTM object
545 	 * needs to stay alive. Otherwhise hang it on the ghost
546 	 * bo to be unbound and destroyed.
547 	 */
548 
549 	if (dst_use_tt)
550 		ghost_obj->ttm = NULL;
551 	else
552 		bo->ttm = NULL;
553 
554 	dma_resv_unlock(&ghost_obj->base._resv);
555 	ttm_bo_put(ghost_obj);
556 	return 0;
557 }
558 
559 static void ttm_bo_move_pipeline_evict(struct ttm_buffer_object *bo,
560 				       struct dma_fence *fence)
561 {
562 	struct ttm_device *bdev = bo->bdev;
563 	struct ttm_resource_manager *from;
564 
565 	from = ttm_manager_type(bdev, bo->resource->mem_type);
566 
567 	/**
568 	 * BO doesn't have a TTM we need to bind/unbind. Just remember
569 	 * this eviction and free up the allocation
570 	 */
571 	spin_lock(&from->move_lock);
572 	if (!from->move || dma_fence_is_later(fence, from->move)) {
573 		dma_fence_put(from->move);
574 		from->move = dma_fence_get(fence);
575 	}
576 	spin_unlock(&from->move_lock);
577 
578 	ttm_resource_free(bo, &bo->resource);
579 }
580 
581 int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
582 			      struct dma_fence *fence,
583 			      bool evict,
584 			      bool pipeline,
585 			      struct ttm_resource *new_mem)
586 {
587 	struct ttm_device *bdev = bo->bdev;
588 	struct ttm_resource_manager *from = ttm_manager_type(bdev, bo->resource->mem_type);
589 	struct ttm_resource_manager *man = ttm_manager_type(bdev, new_mem->mem_type);
590 	int ret = 0;
591 
592 	dma_resv_add_fence(bo->base.resv, fence, DMA_RESV_USAGE_KERNEL);
593 	if (!evict)
594 		ret = ttm_bo_move_to_ghost(bo, fence, man->use_tt);
595 	else if (!from->use_tt && pipeline)
596 		ttm_bo_move_pipeline_evict(bo, fence);
597 	else
598 		ret = ttm_bo_wait_free_node(bo, man->use_tt);
599 
600 	if (ret)
601 		return ret;
602 
603 	ttm_bo_assign_mem(bo, new_mem);
604 
605 	return 0;
606 }
607 EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);
608 
609 void ttm_bo_move_sync_cleanup(struct ttm_buffer_object *bo,
610 			      struct ttm_resource *new_mem)
611 {
612 	struct ttm_device *bdev = bo->bdev;
613 	struct ttm_resource_manager *man = ttm_manager_type(bdev, new_mem->mem_type);
614 	int ret;
615 
616 	ret = ttm_bo_wait_free_node(bo, man->use_tt);
617 	if (WARN_ON(ret))
618 		return;
619 
620 	ttm_bo_assign_mem(bo, new_mem);
621 }
622 EXPORT_SYMBOL(ttm_bo_move_sync_cleanup);
623 
624 /**
625  * ttm_bo_pipeline_gutting - purge the contents of a bo
626  * @bo: The buffer object
627  *
628  * Purge the contents of a bo, async if the bo is not idle.
629  * After a successful call, the bo is left unpopulated in
630  * system placement. The function may wait uninterruptible
631  * for idle on OOM.
632  *
633  * Return: 0 if successful, negative error code on failure.
634  */
635 int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo)
636 {
637 	static const struct ttm_place sys_mem = { .mem_type = TTM_PL_SYSTEM };
638 	struct ttm_buffer_object *ghost;
639 	struct ttm_resource *sys_res;
640 	struct ttm_tt *ttm;
641 	int ret;
642 
643 	ret = ttm_resource_alloc(bo, &sys_mem, &sys_res);
644 	if (ret)
645 		return ret;
646 
647 	/* If already idle, no need for ghost object dance. */
648 	ret = ttm_bo_wait(bo, false, true);
649 	if (ret != -EBUSY) {
650 		if (!bo->ttm) {
651 			/* See comment below about clearing. */
652 			ret = ttm_tt_create(bo, true);
653 			if (ret)
654 				goto error_free_sys_mem;
655 		} else {
656 			ttm_tt_unpopulate(bo->bdev, bo->ttm);
657 			if (bo->type == ttm_bo_type_device)
658 				ttm_tt_mark_for_clear(bo->ttm);
659 		}
660 		ttm_resource_free(bo, &bo->resource);
661 		ttm_bo_assign_mem(bo, sys_res);
662 		return 0;
663 	}
664 
665 	/*
666 	 * We need an unpopulated ttm_tt after giving our current one,
667 	 * if any, to the ghost object. And we can't afford to fail
668 	 * creating one *after* the operation. If the bo subsequently gets
669 	 * resurrected, make sure it's cleared (if ttm_bo_type_device)
670 	 * to avoid leaking sensitive information to user-space.
671 	 */
672 
673 	ttm = bo->ttm;
674 	bo->ttm = NULL;
675 	ret = ttm_tt_create(bo, true);
676 	swap(bo->ttm, ttm);
677 	if (ret)
678 		goto error_free_sys_mem;
679 
680 	ret = ttm_buffer_object_transfer(bo, &ghost);
681 	if (ret)
682 		goto error_destroy_tt;
683 
684 	ret = dma_resv_copy_fences(&ghost->base._resv, bo->base.resv);
685 	/* Last resort, wait for the BO to be idle when we are OOM */
686 	if (ret)
687 		ttm_bo_wait(bo, false, false);
688 
689 	dma_resv_unlock(&ghost->base._resv);
690 	ttm_bo_put(ghost);
691 	bo->ttm = ttm;
692 	ttm_bo_assign_mem(bo, sys_res);
693 	return 0;
694 
695 error_destroy_tt:
696 	ttm_tt_destroy(bo->bdev, ttm);
697 
698 error_free_sys_mem:
699 	ttm_resource_free(bo, &sys_res);
700 	return ret;
701 }
702