xref: /dflybsd-src/sys/dev/drm/ttm/ttm_bo_util.c (revision 9ebbd47df7abd81e0803cf228d15b3c372ad85db)
1 /**************************************************************************
2  *
3  * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 /*
28  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29  */
30 
31 #include <drm/ttm/ttm_bo_driver.h>
32 #include <drm/ttm/ttm_placement.h>
33 #include <sys/sfbuf.h>
34 #include <linux/export.h>
35 #include <linux/wait.h>
36 
37 void ttm_bo_free_old_node(struct ttm_buffer_object *bo)
38 {
39 	ttm_bo_mem_put(bo, &bo->mem);
40 }
41 
42 int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
43 		    bool evict,
44 		    bool no_wait_gpu, struct ttm_mem_reg *new_mem)
45 {
46 	struct ttm_tt *ttm = bo->ttm;
47 	struct ttm_mem_reg *old_mem = &bo->mem;
48 	int ret;
49 
50 	if (old_mem->mem_type != TTM_PL_SYSTEM) {
51 		ttm_tt_unbind(ttm);
52 		ttm_bo_free_old_node(bo);
53 		ttm_flag_masked(&old_mem->placement, TTM_PL_FLAG_SYSTEM,
54 				TTM_PL_MASK_MEM);
55 		old_mem->mem_type = TTM_PL_SYSTEM;
56 	}
57 
58 	ret = ttm_tt_set_placement_caching(ttm, new_mem->placement);
59 	if (unlikely(ret != 0))
60 		return ret;
61 
62 	if (new_mem->mem_type != TTM_PL_SYSTEM) {
63 		ret = ttm_tt_bind(ttm, new_mem);
64 		if (unlikely(ret != 0))
65 			return ret;
66 	}
67 
68 	*old_mem = *new_mem;
69 	new_mem->mm_node = NULL;
70 
71 	return 0;
72 }
73 EXPORT_SYMBOL(ttm_bo_move_ttm);
74 
75 int ttm_mem_io_lock(struct ttm_mem_type_manager *man, bool interruptible)
76 {
77 	if (likely(man->io_reserve_fastpath))
78 		return 0;
79 
80 	if (interruptible) {
81 		if (lockmgr(&man->io_reserve_mutex,
82 			    LK_EXCLUSIVE | LK_SLEEPFAIL))
83 			return (-EINTR);
84 		else
85 			return (0);
86 	}
87 
88 	lockmgr(&man->io_reserve_mutex, LK_EXCLUSIVE);
89 	return 0;
90 }
91 EXPORT_SYMBOL(ttm_mem_io_lock);
92 
93 void ttm_mem_io_unlock(struct ttm_mem_type_manager *man)
94 {
95 	if (likely(man->io_reserve_fastpath))
96 		return;
97 
98 	lockmgr(&man->io_reserve_mutex, LK_RELEASE);
99 }
100 EXPORT_SYMBOL(ttm_mem_io_unlock);
101 
102 static int ttm_mem_io_evict(struct ttm_mem_type_manager *man)
103 {
104 	struct ttm_buffer_object *bo;
105 
106 	if (!man->use_io_reserve_lru || list_empty(&man->io_reserve_lru))
107 		return -EAGAIN;
108 
109 	bo = list_first_entry(&man->io_reserve_lru,
110 			      struct ttm_buffer_object,
111 			      io_reserve_lru);
112 	list_del_init(&bo->io_reserve_lru);
113 	ttm_bo_unmap_virtual_locked(bo);
114 
115 	return 0;
116 }
117 
118 
119 int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
120 		       struct ttm_mem_reg *mem)
121 {
122 	struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
123 	int ret = 0;
124 
125 	if (!bdev->driver->io_mem_reserve)
126 		return 0;
127 	if (likely(man->io_reserve_fastpath))
128 		return bdev->driver->io_mem_reserve(bdev, mem);
129 
130 	if (bdev->driver->io_mem_reserve &&
131 	    mem->bus.io_reserved_count++ == 0) {
132 retry:
133 		ret = bdev->driver->io_mem_reserve(bdev, mem);
134 		if (ret == -EAGAIN) {
135 			ret = ttm_mem_io_evict(man);
136 			if (ret == 0)
137 				goto retry;
138 		}
139 	}
140 	return ret;
141 }
142 EXPORT_SYMBOL(ttm_mem_io_reserve);
143 
144 void ttm_mem_io_free(struct ttm_bo_device *bdev,
145 		     struct ttm_mem_reg *mem)
146 {
147 	struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
148 
149 	if (likely(man->io_reserve_fastpath))
150 		return;
151 
152 	if (bdev->driver->io_mem_reserve &&
153 	    --mem->bus.io_reserved_count == 0 &&
154 	    bdev->driver->io_mem_free)
155 		bdev->driver->io_mem_free(bdev, mem);
156 
157 }
158 EXPORT_SYMBOL(ttm_mem_io_free);
159 
160 int ttm_mem_io_reserve_vm(struct ttm_buffer_object *bo)
161 {
162 	struct ttm_mem_reg *mem = &bo->mem;
163 	int ret;
164 
165 	if (!mem->bus.io_reserved_vm) {
166 		struct ttm_mem_type_manager *man =
167 			&bo->bdev->man[mem->mem_type];
168 
169 		ret = ttm_mem_io_reserve(bo->bdev, mem);
170 		if (unlikely(ret != 0))
171 			return ret;
172 		mem->bus.io_reserved_vm = true;
173 		if (man->use_io_reserve_lru)
174 			list_add_tail(&bo->io_reserve_lru,
175 				      &man->io_reserve_lru);
176 	}
177 	return 0;
178 }
179 
180 void ttm_mem_io_free_vm(struct ttm_buffer_object *bo)
181 {
182 	struct ttm_mem_reg *mem = &bo->mem;
183 
184 	if (mem->bus.io_reserved_vm) {
185 		mem->bus.io_reserved_vm = false;
186 		list_del_init(&bo->io_reserve_lru);
187 		ttm_mem_io_free(bo->bdev, mem);
188 	}
189 }
190 
191 static
192 int ttm_mem_reg_ioremap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
193 			void **virtual)
194 {
195 	struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
196 	int ret;
197 	void *addr;
198 
199 	*virtual = NULL;
200 	(void) ttm_mem_io_lock(man, false);
201 	ret = ttm_mem_io_reserve(bdev, mem);
202 	ttm_mem_io_unlock(man);
203 	if (ret || !mem->bus.is_iomem)
204 		return ret;
205 
206 	if (mem->bus.addr) {
207 		addr = mem->bus.addr;
208 	} else {
209 		addr = pmap_mapdev_attr(mem->bus.base + mem->bus.offset,
210 		    mem->bus.size, (mem->placement & TTM_PL_FLAG_WC) ?
211 		    VM_MEMATTR_WRITE_COMBINING : VM_MEMATTR_UNCACHEABLE);
212 		if (!addr) {
213 			(void) ttm_mem_io_lock(man, false);
214 			ttm_mem_io_free(bdev, mem);
215 			ttm_mem_io_unlock(man);
216 			return -ENOMEM;
217 		}
218 	}
219 	*virtual = addr;
220 	return 0;
221 }
222 
223 static
224 void ttm_mem_reg_iounmap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
225 			 void *virtual)
226 {
227 	struct ttm_mem_type_manager *man;
228 
229 	man = &bdev->man[mem->mem_type];
230 
231 	if (virtual && mem->bus.addr == NULL)
232 		pmap_unmapdev((vm_offset_t)virtual, mem->bus.size);
233 	(void) ttm_mem_io_lock(man, false);
234 	ttm_mem_io_free(bdev, mem);
235 	ttm_mem_io_unlock(man);
236 }
237 
238 static int ttm_copy_io_page(void *dst, void *src, unsigned long page)
239 {
240 	uint32_t *dstP =
241 	    (uint32_t *) ((unsigned long)dst + (page << PAGE_SHIFT));
242 	uint32_t *srcP =
243 	    (uint32_t *) ((unsigned long)src + (page << PAGE_SHIFT));
244 
245 	int i;
246 	for (i = 0; i < PAGE_SIZE / sizeof(uint32_t); ++i)
247 		/* iowrite32(ioread32(srcP++), dstP++); */
248 		*dstP++ = *srcP++;
249 	return 0;
250 }
251 
252 static int ttm_copy_io_ttm_page(struct ttm_tt *ttm, void *src,
253 				unsigned long page,
254 				vm_memattr_t prot)
255 {
256 	vm_page_t d = ttm->pages[page];
257 	void *dst;
258 
259 	if (!d)
260 		return -ENOMEM;
261 
262 	src = (void *)((unsigned long)src + (page << PAGE_SHIFT));
263 
264 	/* XXXKIB can't sleep ? */
265 	dst = pmap_mapdev_attr(VM_PAGE_TO_PHYS(d), PAGE_SIZE, prot);
266 	if (!dst)
267 		return -ENOMEM;
268 
269 	memcpy_fromio(dst, src, PAGE_SIZE);
270 
271 	pmap_unmapdev((vm_offset_t)dst, PAGE_SIZE);
272 
273 	return 0;
274 }
275 
276 static int ttm_copy_ttm_io_page(struct ttm_tt *ttm, void *dst,
277 				unsigned long page,
278 				vm_memattr_t prot)
279 {
280 	vm_page_t s = ttm->pages[page];
281 	void *src;
282 
283 	if (!s)
284 		return -ENOMEM;
285 
286 	dst = (void *)((unsigned long)dst + (page << PAGE_SHIFT));
287 	src = pmap_mapdev_attr(VM_PAGE_TO_PHYS(s), PAGE_SIZE, prot);
288 	if (!src)
289 		return -ENOMEM;
290 
291 	memcpy_toio(dst, src, PAGE_SIZE);
292 
293 	pmap_unmapdev((vm_offset_t)src, PAGE_SIZE);
294 
295 	return 0;
296 }
297 
298 int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
299 		       bool evict, bool no_wait_gpu,
300 		       struct ttm_mem_reg *new_mem)
301 {
302 	struct ttm_bo_device *bdev = bo->bdev;
303 	struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
304 	struct ttm_tt *ttm = bo->ttm;
305 	struct ttm_mem_reg *old_mem = &bo->mem;
306 	struct ttm_mem_reg old_copy = *old_mem;
307 	void *old_iomap;
308 	void *new_iomap;
309 	int ret;
310 	unsigned long i;
311 	unsigned long page;
312 	unsigned long add = 0;
313 	int dir;
314 
315 	ret = ttm_mem_reg_ioremap(bdev, old_mem, &old_iomap);
316 	if (ret)
317 		return ret;
318 	ret = ttm_mem_reg_ioremap(bdev, new_mem, &new_iomap);
319 	if (ret)
320 		goto out;
321 
322 	if (old_iomap == NULL && new_iomap == NULL)
323 		goto out2;
324 	if (old_iomap == NULL && ttm == NULL)
325 		goto out2;
326 
327 	if (ttm->state == tt_unpopulated) {
328 		ret = ttm->bdev->driver->ttm_tt_populate(ttm);
329 		if (ret) {
330 			/* if we fail here don't nuke the mm node
331 			 * as the bo still owns it */
332 			old_copy.mm_node = NULL;
333 			goto out1;
334 		}
335 	}
336 
337 	add = 0;
338 	dir = 1;
339 
340 	if ((old_mem->mem_type == new_mem->mem_type) &&
341 	    (new_mem->start < old_mem->start + old_mem->size)) {
342 		dir = -1;
343 		add = new_mem->num_pages - 1;
344 	}
345 
346 	for (i = 0; i < new_mem->num_pages; ++i) {
347 		page = i * dir + add;
348 		if (old_iomap == NULL) {
349 			vm_memattr_t prot = ttm_io_prot(old_mem->placement);
350 			ret = ttm_copy_ttm_io_page(ttm, new_iomap, page,
351 						   prot);
352 		} else if (new_iomap == NULL) {
353 			vm_memattr_t prot = ttm_io_prot(new_mem->placement);
354 			ret = ttm_copy_io_ttm_page(ttm, old_iomap, page,
355 						   prot);
356 		} else
357 			ret = ttm_copy_io_page(new_iomap, old_iomap, page);
358 		if (ret) {
359 			/* failing here, means keep old copy as-is */
360 			old_copy.mm_node = NULL;
361 			goto out1;
362 		}
363 	}
364 	cpu_mfence();
365 out2:
366 	old_copy = *old_mem;
367 	*old_mem = *new_mem;
368 	new_mem->mm_node = NULL;
369 
370 	if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && (ttm != NULL)) {
371 		ttm_tt_unbind(ttm);
372 		ttm_tt_destroy(ttm);
373 		bo->ttm = NULL;
374 	}
375 
376 out1:
377 	ttm_mem_reg_iounmap(bdev, old_mem, new_iomap);
378 out:
379 	ttm_mem_reg_iounmap(bdev, &old_copy, old_iomap);
380 	ttm_bo_mem_put(bo, &old_copy);
381 	return ret;
382 }
383 EXPORT_SYMBOL(ttm_bo_move_memcpy);
384 
385 static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
386 {
387 	kfree(bo);
388 }
389 
390 /**
391  * ttm_buffer_object_transfer
392  *
393  * @bo: A pointer to a struct ttm_buffer_object.
394  * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
395  * holding the data of @bo with the old placement.
396  *
397  * This is a utility function that may be called after an accelerated move
398  * has been scheduled. A new buffer object is created as a placeholder for
399  * the old data while it's being copied. When that buffer object is idle,
400  * it can be destroyed, releasing the space of the old placement.
401  * Returns:
402  * !0: Failure.
403  */
404 
405 static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
406 				      struct ttm_buffer_object **new_obj)
407 {
408 	struct ttm_buffer_object *fbo;
409 	struct ttm_bo_device *bdev = bo->bdev;
410 	struct ttm_bo_driver *driver = bdev->driver;
411 
412 	fbo = kmalloc(sizeof(*fbo), M_DRM, M_WAITOK | M_ZERO);
413 	if (!fbo)
414 		return -ENOMEM;
415 
416 	*fbo = *bo;
417 
418 	/**
419 	 * Fix up members that we shouldn't copy directly:
420 	 * TODO: Explicit member copy would probably be better here.
421 	 */
422 
423 	init_waitqueue_head(&fbo->event_queue);
424 	INIT_LIST_HEAD(&fbo->ddestroy);
425 	INIT_LIST_HEAD(&fbo->lru);
426 	INIT_LIST_HEAD(&fbo->swap);
427 	INIT_LIST_HEAD(&fbo->io_reserve_lru);
428 	fbo->vm_node = NULL;
429 	atomic_set(&fbo->cpu_writers, 0);
430 
431 	lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
432 	if (bo->sync_obj)
433 		fbo->sync_obj = driver->sync_obj_ref(bo->sync_obj);
434 	else
435 		fbo->sync_obj = NULL;
436 	lockmgr(&bdev->fence_lock, LK_RELEASE);
437 	kref_init(&fbo->list_kref);
438 	kref_init(&fbo->kref);
439 	fbo->destroy = &ttm_transfered_destroy;
440 	fbo->acc_size = 0;
441 
442         /*
443 	 * Mirror ref from kref_init() for list_kref.
444 	 */
445 	set_bit(TTM_BO_PRIV_FLAG_ACTIVE, &fbo->priv_flags);
446 
447 	*new_obj = fbo;
448 	return 0;
449 }
450 
451 vm_memattr_t
452 ttm_io_prot(uint32_t caching_flags)
453 {
454 #if defined(__i386__) || defined(__x86_64__)
455 	if (caching_flags & TTM_PL_FLAG_WC)
456 		return (VM_MEMATTR_WRITE_COMBINING);
457 	else
458 		/*
459 		 * We do not support i386, look at the linux source
460 		 * for the reason of the comment.
461 		 */
462 		return (VM_MEMATTR_UNCACHEABLE);
463 #else
464 #error Port me
465 #endif
466 }
467 EXPORT_SYMBOL(ttm_io_prot);
468 
469 static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
470 			  unsigned long offset,
471 			  unsigned long size,
472 			  struct ttm_bo_kmap_obj *map)
473 {
474 	struct ttm_mem_reg *mem = &bo->mem;
475 
476 	if (bo->mem.bus.addr) {
477 		map->bo_kmap_type = ttm_bo_map_premapped;
478 		map->virtual = (void *)(((u8 *)bo->mem.bus.addr) + offset);
479 	} else {
480 		map->bo_kmap_type = ttm_bo_map_iomap;
481 		map->virtual = pmap_mapdev_attr(bo->mem.bus.base +
482 		    bo->mem.bus.offset + offset, size,
483 		    (mem->placement & TTM_PL_FLAG_WC) ?
484 		    VM_MEMATTR_WRITE_COMBINING : VM_MEMATTR_UNCACHEABLE);
485 		map->size = size;
486 	}
487 	return (!map->virtual) ? -ENOMEM : 0;
488 }
489 
490 static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
491 			   unsigned long start_page,
492 			   unsigned long num_pages,
493 			   struct ttm_bo_kmap_obj *map)
494 {
495 	struct ttm_mem_reg *mem = &bo->mem;
496 	vm_memattr_t prot;
497 	struct ttm_tt *ttm = bo->ttm;
498 	int i, ret;
499 
500 	BUG_ON(!ttm);
501 
502 	if (ttm->state == tt_unpopulated) {
503 		ret = ttm->bdev->driver->ttm_tt_populate(ttm);
504 		if (ret)
505 			return ret;
506 	}
507 
508 	if (num_pages == 1 && (mem->placement & TTM_PL_FLAG_CACHED)) {
509 		/*
510 		 * We're mapping a single page, and the desired
511 		 * page protection is consistent with the bo.
512 		 */
513 
514 		map->bo_kmap_type = ttm_bo_map_kmap;
515 		map->page = ttm->pages[start_page];
516 		map->sf = sf_buf_alloc(map->page);
517 		map->virtual = (void *)sf_buf_kva(map->sf);
518 	} else {
519 		/*
520 		 * We need to use vmap to get the desired page protection
521 		 * or to make the buffer object look contiguous.
522 		 */
523 		prot = (mem->placement & TTM_PL_FLAG_CACHED) ?
524 			VM_MEMATTR_WRITE_COMBINING :
525 			ttm_io_prot(mem->placement);
526 		map->bo_kmap_type = ttm_bo_map_vmap;
527 		map->num_pages = num_pages;
528 		map->virtual = (void *)kmem_alloc_nofault(&kernel_map,
529 		    num_pages * PAGE_SIZE, PAGE_SIZE);
530 		if (map->virtual != NULL) {
531 			for (i = 0; i < num_pages; i++) {
532 				/* XXXKIB hack */
533 				pmap_page_set_memattr(ttm->pages[start_page +
534 				    i], prot);
535 			}
536 			pmap_qenter((vm_offset_t)map->virtual,
537 			    &ttm->pages[start_page], num_pages);
538 		}
539 	}
540 	return (!map->virtual) ? -ENOMEM : 0;
541 }
542 
543 int ttm_bo_kmap(struct ttm_buffer_object *bo,
544 		unsigned long start_page, unsigned long num_pages,
545 		struct ttm_bo_kmap_obj *map)
546 {
547 	struct ttm_mem_type_manager *man =
548 		&bo->bdev->man[bo->mem.mem_type];
549 	unsigned long offset, size;
550 	int ret;
551 
552 	BUG_ON(!list_empty(&bo->swap));
553 	map->virtual = NULL;
554 	map->bo = bo;
555 	if (num_pages > bo->num_pages)
556 		return -EINVAL;
557 	if (start_page > bo->num_pages)
558 		return -EINVAL;
559 #if 0
560 	if (num_pages > 1 && !DRM_SUSER(DRM_CURPROC))
561 		return -EPERM;
562 #endif
563 	(void) ttm_mem_io_lock(man, false);
564 	ret = ttm_mem_io_reserve(bo->bdev, &bo->mem);
565 	ttm_mem_io_unlock(man);
566 	if (ret)
567 		return ret;
568 	if (!bo->mem.bus.is_iomem) {
569 		return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
570 	} else {
571 		offset = start_page << PAGE_SHIFT;
572 		size = num_pages << PAGE_SHIFT;
573 		return ttm_bo_ioremap(bo, offset, size, map);
574 	}
575 }
576 EXPORT_SYMBOL(ttm_bo_kmap);
577 
578 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
579 {
580 	struct ttm_buffer_object *bo = map->bo;
581 	struct ttm_mem_type_manager *man =
582 		&bo->bdev->man[bo->mem.mem_type];
583 
584 	if (!map->virtual)
585 		return;
586 	switch (map->bo_kmap_type) {
587 	case ttm_bo_map_iomap:
588 		pmap_unmapdev((vm_offset_t)map->virtual, map->size);
589 		break;
590 	case ttm_bo_map_vmap:
591 		pmap_qremove((vm_offset_t)(map->virtual), map->num_pages);
592 		kmem_free(&kernel_map, (vm_offset_t)map->virtual,
593 		    map->num_pages * PAGE_SIZE);
594 		break;
595 	case ttm_bo_map_kmap:
596 		sf_buf_free(map->sf);
597 		break;
598 	case ttm_bo_map_premapped:
599 		break;
600 	default:
601 		BUG();
602 	}
603 	(void) ttm_mem_io_lock(man, false);
604 	ttm_mem_io_free(map->bo->bdev, &map->bo->mem);
605 	ttm_mem_io_unlock(man);
606 	map->virtual = NULL;
607 	map->page = NULL;
608 	map->sf = NULL;
609 }
610 EXPORT_SYMBOL(ttm_bo_kunmap);
611 
612 int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
613 			      void *sync_obj,
614 			      bool evict,
615 			      bool no_wait_gpu,
616 			      struct ttm_mem_reg *new_mem)
617 {
618 	struct ttm_bo_device *bdev = bo->bdev;
619 	struct ttm_bo_driver *driver = bdev->driver;
620 	struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
621 	struct ttm_mem_reg *old_mem = &bo->mem;
622 	int ret;
623 	struct ttm_buffer_object *ghost_obj;
624 	void *tmp_obj = NULL;
625 
626 	lockmgr(&bdev->fence_lock, LK_EXCLUSIVE);
627 	if (bo->sync_obj) {
628 		tmp_obj = bo->sync_obj;
629 		bo->sync_obj = NULL;
630 	}
631 	bo->sync_obj = driver->sync_obj_ref(sync_obj);
632 	if (evict) {
633 		ret = ttm_bo_wait(bo, false, false, false);
634 		lockmgr(&bdev->fence_lock, LK_RELEASE);
635 		if (tmp_obj)
636 			driver->sync_obj_unref(&tmp_obj);
637 		if (ret)
638 			return ret;
639 
640 		if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
641 		    (bo->ttm != NULL)) {
642 			ttm_tt_unbind(bo->ttm);
643 			ttm_tt_destroy(bo->ttm);
644 			bo->ttm = NULL;
645 		}
646 		ttm_bo_free_old_node(bo);
647 	} else {
648 		/**
649 		 * This should help pipeline ordinary buffer moves.
650 		 *
651 		 * Hang old buffer memory on a new buffer object,
652 		 * and leave it to be released when the GPU
653 		 * operation has completed.
654 		 */
655 
656 		set_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
657 		lockmgr(&bdev->fence_lock, LK_RELEASE);
658 		if (tmp_obj)
659 			driver->sync_obj_unref(&tmp_obj);
660 
661 		ret = ttm_buffer_object_transfer(bo, &ghost_obj);
662 		if (ret)
663 			return ret;
664 
665 		/**
666 		 * If we're not moving to fixed memory, the TTM object
667 		 * needs to stay alive. Otherwhise hang it on the ghost
668 		 * bo to be unbound and destroyed.
669 		 */
670 
671 		if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED))
672 			ghost_obj->ttm = NULL;
673 		else
674 			bo->ttm = NULL;
675 
676 		ttm_bo_unreserve(ghost_obj);
677 		ttm_bo_unref(&ghost_obj);
678 	}
679 
680 	*old_mem = *new_mem;
681 	new_mem->mm_node = NULL;
682 
683 	return 0;
684 }
685 EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);
686