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