1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
3  *
4  * Copyright 2009-2015 VMware, Inc., Palo Alto, CA., USA
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 #include "vmwgfx_drv.h"
29 #include <drm/ttm/ttm_placement.h>
30 
31 static const struct ttm_place vram_placement_flags = {
32 	.fpfn = 0,
33 	.lpfn = 0,
34 	.mem_type = TTM_PL_VRAM,
35 	.flags = 0
36 };
37 
38 static const struct ttm_place sys_placement_flags = {
39 	.fpfn = 0,
40 	.lpfn = 0,
41 	.mem_type = TTM_PL_SYSTEM,
42 	.flags = 0
43 };
44 
45 static const struct ttm_place gmr_placement_flags = {
46 	.fpfn = 0,
47 	.lpfn = 0,
48 	.mem_type = VMW_PL_GMR,
49 	.flags = 0
50 };
51 
52 static const struct ttm_place mob_placement_flags = {
53 	.fpfn = 0,
54 	.lpfn = 0,
55 	.mem_type = VMW_PL_MOB,
56 	.flags = 0
57 };
58 
59 struct ttm_placement vmw_vram_placement = {
60 	.num_placement = 1,
61 	.placement = &vram_placement_flags,
62 	.num_busy_placement = 1,
63 	.busy_placement = &vram_placement_flags
64 };
65 
66 static const struct ttm_place vram_gmr_placement_flags[] = {
67 	{
68 		.fpfn = 0,
69 		.lpfn = 0,
70 		.mem_type = TTM_PL_VRAM,
71 		.flags = 0
72 	}, {
73 		.fpfn = 0,
74 		.lpfn = 0,
75 		.mem_type = VMW_PL_GMR,
76 		.flags = 0
77 	}
78 };
79 
80 static const struct ttm_place gmr_vram_placement_flags[] = {
81 	{
82 		.fpfn = 0,
83 		.lpfn = 0,
84 		.mem_type = VMW_PL_GMR,
85 		.flags = 0
86 	}, {
87 		.fpfn = 0,
88 		.lpfn = 0,
89 		.mem_type = TTM_PL_VRAM,
90 		.flags = 0
91 	}
92 };
93 
94 static const struct ttm_place vmw_sys_placement_flags = {
95 	.fpfn = 0,
96 	.lpfn = 0,
97 	.mem_type = VMW_PL_SYSTEM,
98 	.flags = 0
99 };
100 
101 struct ttm_placement vmw_vram_gmr_placement = {
102 	.num_placement = 2,
103 	.placement = vram_gmr_placement_flags,
104 	.num_busy_placement = 1,
105 	.busy_placement = &gmr_placement_flags
106 };
107 
108 struct ttm_placement vmw_vram_sys_placement = {
109 	.num_placement = 1,
110 	.placement = &vram_placement_flags,
111 	.num_busy_placement = 1,
112 	.busy_placement = &sys_placement_flags
113 };
114 
115 struct ttm_placement vmw_sys_placement = {
116 	.num_placement = 1,
117 	.placement = &sys_placement_flags,
118 	.num_busy_placement = 1,
119 	.busy_placement = &sys_placement_flags
120 };
121 
122 struct ttm_placement vmw_pt_sys_placement = {
123 	.num_placement = 1,
124 	.placement = &vmw_sys_placement_flags,
125 	.num_busy_placement = 1,
126 	.busy_placement = &vmw_sys_placement_flags
127 };
128 
129 static const struct ttm_place nonfixed_placement_flags[] = {
130 	{
131 		.fpfn = 0,
132 		.lpfn = 0,
133 		.mem_type = TTM_PL_SYSTEM,
134 		.flags = 0
135 	}, {
136 		.fpfn = 0,
137 		.lpfn = 0,
138 		.mem_type = VMW_PL_GMR,
139 		.flags = 0
140 	}, {
141 		.fpfn = 0,
142 		.lpfn = 0,
143 		.mem_type = VMW_PL_MOB,
144 		.flags = 0
145 	}
146 };
147 
148 struct ttm_placement vmw_srf_placement = {
149 	.num_placement = 1,
150 	.num_busy_placement = 2,
151 	.placement = &gmr_placement_flags,
152 	.busy_placement = gmr_vram_placement_flags
153 };
154 
155 struct ttm_placement vmw_mob_placement = {
156 	.num_placement = 1,
157 	.num_busy_placement = 1,
158 	.placement = &mob_placement_flags,
159 	.busy_placement = &mob_placement_flags
160 };
161 
162 struct ttm_placement vmw_nonfixed_placement = {
163 	.num_placement = 3,
164 	.placement = nonfixed_placement_flags,
165 	.num_busy_placement = 1,
166 	.busy_placement = &sys_placement_flags
167 };
168 
169 const size_t vmw_tt_size = sizeof(struct vmw_ttm_tt);
170 
171 /**
172  * __vmw_piter_non_sg_next: Helper functions to advance
173  * a struct vmw_piter iterator.
174  *
175  * @viter: Pointer to the iterator.
176  *
177  * These functions return false if past the end of the list,
178  * true otherwise. Functions are selected depending on the current
179  * DMA mapping mode.
180  */
181 static bool __vmw_piter_non_sg_next(struct vmw_piter *viter)
182 {
183 	return ++(viter->i) < viter->num_pages;
184 }
185 
186 static bool __vmw_piter_sg_next(struct vmw_piter *viter)
187 {
188 	bool ret = __vmw_piter_non_sg_next(viter);
189 
190 	return __sg_page_iter_dma_next(&viter->iter) && ret;
191 }
192 
193 
194 static dma_addr_t __vmw_piter_dma_addr(struct vmw_piter *viter)
195 {
196 	return viter->addrs[viter->i];
197 }
198 
199 static dma_addr_t __vmw_piter_sg_addr(struct vmw_piter *viter)
200 {
201 	return sg_page_iter_dma_address(&viter->iter);
202 }
203 
204 
205 /**
206  * vmw_piter_start - Initialize a struct vmw_piter.
207  *
208  * @viter: Pointer to the iterator to initialize
209  * @vsgt: Pointer to a struct vmw_sg_table to initialize from
210  * @p_offset: Pointer offset used to update current array position
211  *
212  * Note that we're following the convention of __sg_page_iter_start, so that
213  * the iterator doesn't point to a valid page after initialization; it has
214  * to be advanced one step first.
215  */
216 void vmw_piter_start(struct vmw_piter *viter, const struct vmw_sg_table *vsgt,
217 		     unsigned long p_offset)
218 {
219 	viter->i = p_offset - 1;
220 	viter->num_pages = vsgt->num_pages;
221 	viter->pages = vsgt->pages;
222 	switch (vsgt->mode) {
223 	case vmw_dma_alloc_coherent:
224 		viter->next = &__vmw_piter_non_sg_next;
225 		viter->dma_address = &__vmw_piter_dma_addr;
226 		viter->addrs = vsgt->addrs;
227 		break;
228 	case vmw_dma_map_populate:
229 	case vmw_dma_map_bind:
230 		viter->next = &__vmw_piter_sg_next;
231 		viter->dma_address = &__vmw_piter_sg_addr;
232 		__sg_page_iter_start(&viter->iter.base, vsgt->sgt->sgl,
233 				     vsgt->sgt->orig_nents, p_offset);
234 		break;
235 	default:
236 		BUG();
237 	}
238 }
239 
240 /**
241  * vmw_ttm_unmap_from_dma - unmap  device addresses previsouly mapped for
242  * TTM pages
243  *
244  * @vmw_tt: Pointer to a struct vmw_ttm_backend
245  *
246  * Used to free dma mappings previously mapped by vmw_ttm_map_for_dma.
247  */
248 static void vmw_ttm_unmap_from_dma(struct vmw_ttm_tt *vmw_tt)
249 {
250 	struct device *dev = vmw_tt->dev_priv->drm.dev;
251 
252 	dma_unmap_sgtable(dev, &vmw_tt->sgt, DMA_BIDIRECTIONAL, 0);
253 	vmw_tt->sgt.nents = vmw_tt->sgt.orig_nents;
254 }
255 
256 /**
257  * vmw_ttm_map_for_dma - map TTM pages to get device addresses
258  *
259  * @vmw_tt: Pointer to a struct vmw_ttm_backend
260  *
261  * This function is used to get device addresses from the kernel DMA layer.
262  * However, it's violating the DMA API in that when this operation has been
263  * performed, it's illegal for the CPU to write to the pages without first
264  * unmapping the DMA mappings, or calling dma_sync_sg_for_cpu(). It is
265  * therefore only legal to call this function if we know that the function
266  * dma_sync_sg_for_cpu() is a NOP, and dma_sync_sg_for_device() is at most
267  * a CPU write buffer flush.
268  */
269 static int vmw_ttm_map_for_dma(struct vmw_ttm_tt *vmw_tt)
270 {
271 	struct device *dev = vmw_tt->dev_priv->drm.dev;
272 
273 	return dma_map_sgtable(dev, &vmw_tt->sgt, DMA_BIDIRECTIONAL, 0);
274 }
275 
276 /**
277  * vmw_ttm_map_dma - Make sure TTM pages are visible to the device
278  *
279  * @vmw_tt: Pointer to a struct vmw_ttm_tt
280  *
281  * Select the correct function for and make sure the TTM pages are
282  * visible to the device. Allocate storage for the device mappings.
283  * If a mapping has already been performed, indicated by the storage
284  * pointer being non NULL, the function returns success.
285  */
286 static int vmw_ttm_map_dma(struct vmw_ttm_tt *vmw_tt)
287 {
288 	struct vmw_private *dev_priv = vmw_tt->dev_priv;
289 	struct vmw_sg_table *vsgt = &vmw_tt->vsgt;
290 	int ret = 0;
291 
292 	if (vmw_tt->mapped)
293 		return 0;
294 
295 	vsgt->mode = dev_priv->map_mode;
296 	vsgt->pages = vmw_tt->dma_ttm.pages;
297 	vsgt->num_pages = vmw_tt->dma_ttm.num_pages;
298 	vsgt->addrs = vmw_tt->dma_ttm.dma_address;
299 	vsgt->sgt = NULL;
300 
301 	switch (dev_priv->map_mode) {
302 	case vmw_dma_map_bind:
303 	case vmw_dma_map_populate:
304 		vsgt->sgt = &vmw_tt->sgt;
305 		ret = sg_alloc_table_from_pages_segment(
306 			&vmw_tt->sgt, vsgt->pages, vsgt->num_pages, 0,
307 			(unsigned long)vsgt->num_pages << PAGE_SHIFT,
308 			dma_get_max_seg_size(dev_priv->drm.dev), GFP_KERNEL);
309 		if (ret)
310 			goto out_sg_alloc_fail;
311 
312 		ret = vmw_ttm_map_for_dma(vmw_tt);
313 		if (unlikely(ret != 0))
314 			goto out_map_fail;
315 
316 		break;
317 	default:
318 		break;
319 	}
320 
321 	vmw_tt->mapped = true;
322 	return 0;
323 
324 out_map_fail:
325 	sg_free_table(vmw_tt->vsgt.sgt);
326 	vmw_tt->vsgt.sgt = NULL;
327 out_sg_alloc_fail:
328 	return ret;
329 }
330 
331 /**
332  * vmw_ttm_unmap_dma - Tear down any TTM page device mappings
333  *
334  * @vmw_tt: Pointer to a struct vmw_ttm_tt
335  *
336  * Tear down any previously set up device DMA mappings and free
337  * any storage space allocated for them. If there are no mappings set up,
338  * this function is a NOP.
339  */
340 static void vmw_ttm_unmap_dma(struct vmw_ttm_tt *vmw_tt)
341 {
342 	struct vmw_private *dev_priv = vmw_tt->dev_priv;
343 
344 	if (!vmw_tt->vsgt.sgt)
345 		return;
346 
347 	switch (dev_priv->map_mode) {
348 	case vmw_dma_map_bind:
349 	case vmw_dma_map_populate:
350 		vmw_ttm_unmap_from_dma(vmw_tt);
351 		sg_free_table(vmw_tt->vsgt.sgt);
352 		vmw_tt->vsgt.sgt = NULL;
353 		break;
354 	default:
355 		break;
356 	}
357 	vmw_tt->mapped = false;
358 }
359 
360 /**
361  * vmw_bo_sg_table - Return a struct vmw_sg_table object for a
362  * TTM buffer object
363  *
364  * @bo: Pointer to a struct ttm_buffer_object
365  *
366  * Returns a pointer to a struct vmw_sg_table object. The object should
367  * not be freed after use.
368  * Note that for the device addresses to be valid, the buffer object must
369  * either be reserved or pinned.
370  */
371 const struct vmw_sg_table *vmw_bo_sg_table(struct ttm_buffer_object *bo)
372 {
373 	struct vmw_ttm_tt *vmw_tt =
374 		container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm);
375 
376 	return &vmw_tt->vsgt;
377 }
378 
379 
380 static int vmw_ttm_bind(struct ttm_device *bdev,
381 			struct ttm_tt *ttm, struct ttm_resource *bo_mem)
382 {
383 	struct vmw_ttm_tt *vmw_be =
384 		container_of(ttm, struct vmw_ttm_tt, dma_ttm);
385 	int ret = 0;
386 
387 	if (!bo_mem)
388 		return -EINVAL;
389 
390 	if (vmw_be->bound)
391 		return 0;
392 
393 	ret = vmw_ttm_map_dma(vmw_be);
394 	if (unlikely(ret != 0))
395 		return ret;
396 
397 	vmw_be->gmr_id = bo_mem->start;
398 	vmw_be->mem_type = bo_mem->mem_type;
399 
400 	switch (bo_mem->mem_type) {
401 	case VMW_PL_GMR:
402 		ret = vmw_gmr_bind(vmw_be->dev_priv, &vmw_be->vsgt,
403 				    ttm->num_pages, vmw_be->gmr_id);
404 		break;
405 	case VMW_PL_MOB:
406 		if (unlikely(vmw_be->mob == NULL)) {
407 			vmw_be->mob =
408 				vmw_mob_create(ttm->num_pages);
409 			if (unlikely(vmw_be->mob == NULL))
410 				return -ENOMEM;
411 		}
412 
413 		ret = vmw_mob_bind(vmw_be->dev_priv, vmw_be->mob,
414 				    &vmw_be->vsgt, ttm->num_pages,
415 				    vmw_be->gmr_id);
416 		break;
417 	case VMW_PL_SYSTEM:
418 		/* Nothing to be done for a system bind */
419 		break;
420 	default:
421 		BUG();
422 	}
423 	vmw_be->bound = true;
424 	return ret;
425 }
426 
427 static void vmw_ttm_unbind(struct ttm_device *bdev,
428 			   struct ttm_tt *ttm)
429 {
430 	struct vmw_ttm_tt *vmw_be =
431 		container_of(ttm, struct vmw_ttm_tt, dma_ttm);
432 
433 	if (!vmw_be->bound)
434 		return;
435 
436 	switch (vmw_be->mem_type) {
437 	case VMW_PL_GMR:
438 		vmw_gmr_unbind(vmw_be->dev_priv, vmw_be->gmr_id);
439 		break;
440 	case VMW_PL_MOB:
441 		vmw_mob_unbind(vmw_be->dev_priv, vmw_be->mob);
442 		break;
443 	case VMW_PL_SYSTEM:
444 		break;
445 	default:
446 		BUG();
447 	}
448 
449 	if (vmw_be->dev_priv->map_mode == vmw_dma_map_bind)
450 		vmw_ttm_unmap_dma(vmw_be);
451 	vmw_be->bound = false;
452 }
453 
454 
455 static void vmw_ttm_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
456 {
457 	struct vmw_ttm_tt *vmw_be =
458 		container_of(ttm, struct vmw_ttm_tt, dma_ttm);
459 
460 	vmw_ttm_unmap_dma(vmw_be);
461 	ttm_tt_fini(ttm);
462 	if (vmw_be->mob)
463 		vmw_mob_destroy(vmw_be->mob);
464 
465 	kfree(vmw_be);
466 }
467 
468 
469 static int vmw_ttm_populate(struct ttm_device *bdev,
470 			    struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
471 {
472 	int ret;
473 
474 	/* TODO: maybe completely drop this ? */
475 	if (ttm_tt_is_populated(ttm))
476 		return 0;
477 
478 	ret = ttm_pool_alloc(&bdev->pool, ttm, ctx);
479 
480 	return ret;
481 }
482 
483 static void vmw_ttm_unpopulate(struct ttm_device *bdev,
484 			       struct ttm_tt *ttm)
485 {
486 	struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt,
487 						 dma_ttm);
488 
489 	vmw_ttm_unbind(bdev, ttm);
490 
491 	if (vmw_tt->mob) {
492 		vmw_mob_destroy(vmw_tt->mob);
493 		vmw_tt->mob = NULL;
494 	}
495 
496 	vmw_ttm_unmap_dma(vmw_tt);
497 
498 	ttm_pool_free(&bdev->pool, ttm);
499 }
500 
501 static struct ttm_tt *vmw_ttm_tt_create(struct ttm_buffer_object *bo,
502 					uint32_t page_flags)
503 {
504 	struct vmw_ttm_tt *vmw_be;
505 	int ret;
506 
507 	vmw_be = kzalloc(sizeof(*vmw_be), GFP_KERNEL);
508 	if (!vmw_be)
509 		return NULL;
510 
511 	vmw_be->dev_priv = container_of(bo->bdev, struct vmw_private, bdev);
512 	vmw_be->mob = NULL;
513 
514 	if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent)
515 		ret = ttm_sg_tt_init(&vmw_be->dma_ttm, bo, page_flags,
516 				     ttm_cached);
517 	else
518 		ret = ttm_tt_init(&vmw_be->dma_ttm, bo, page_flags,
519 				  ttm_cached, 0);
520 	if (unlikely(ret != 0))
521 		goto out_no_init;
522 
523 	return &vmw_be->dma_ttm;
524 out_no_init:
525 	kfree(vmw_be);
526 	return NULL;
527 }
528 
529 static void vmw_evict_flags(struct ttm_buffer_object *bo,
530 		     struct ttm_placement *placement)
531 {
532 	*placement = vmw_sys_placement;
533 }
534 
535 static int vmw_ttm_io_mem_reserve(struct ttm_device *bdev, struct ttm_resource *mem)
536 {
537 	struct vmw_private *dev_priv = container_of(bdev, struct vmw_private, bdev);
538 
539 	switch (mem->mem_type) {
540 	case TTM_PL_SYSTEM:
541 	case VMW_PL_SYSTEM:
542 	case VMW_PL_GMR:
543 	case VMW_PL_MOB:
544 		return 0;
545 	case TTM_PL_VRAM:
546 		mem->bus.offset = (mem->start << PAGE_SHIFT) +
547 			dev_priv->vram_start;
548 		mem->bus.is_iomem = true;
549 		mem->bus.caching = ttm_cached;
550 		break;
551 	default:
552 		return -EINVAL;
553 	}
554 	return 0;
555 }
556 
557 /**
558  * vmw_move_notify - TTM move_notify_callback
559  *
560  * @bo: The TTM buffer object about to move.
561  * @old_mem: The old memory where we move from
562  * @new_mem: The struct ttm_resource indicating to what memory
563  *       region the move is taking place.
564  *
565  * Calls move_notify for all subsystems needing it.
566  * (currently only resources).
567  */
568 static void vmw_move_notify(struct ttm_buffer_object *bo,
569 			    struct ttm_resource *old_mem,
570 			    struct ttm_resource *new_mem)
571 {
572 	vmw_bo_move_notify(bo, new_mem);
573 	vmw_query_move_notify(bo, old_mem, new_mem);
574 }
575 
576 
577 /**
578  * vmw_swap_notify - TTM move_notify_callback
579  *
580  * @bo: The TTM buffer object about to be swapped out.
581  */
582 static void vmw_swap_notify(struct ttm_buffer_object *bo)
583 {
584 	vmw_bo_swap_notify(bo);
585 	(void) ttm_bo_wait(bo, false, false);
586 }
587 
588 static bool vmw_memtype_is_system(uint32_t mem_type)
589 {
590 	return mem_type == TTM_PL_SYSTEM || mem_type == VMW_PL_SYSTEM;
591 }
592 
593 static int vmw_move(struct ttm_buffer_object *bo,
594 		    bool evict,
595 		    struct ttm_operation_ctx *ctx,
596 		    struct ttm_resource *new_mem,
597 		    struct ttm_place *hop)
598 {
599 	struct ttm_resource_manager *old_man = ttm_manager_type(bo->bdev, bo->resource->mem_type);
600 	struct ttm_resource_manager *new_man = ttm_manager_type(bo->bdev, new_mem->mem_type);
601 	int ret;
602 
603 	if (new_man->use_tt && !vmw_memtype_is_system(new_mem->mem_type)) {
604 		ret = vmw_ttm_bind(bo->bdev, bo->ttm, new_mem);
605 		if (ret)
606 			return ret;
607 	}
608 
609 	vmw_move_notify(bo, bo->resource, new_mem);
610 
611 	if (old_man->use_tt && new_man->use_tt) {
612 		if (vmw_memtype_is_system(bo->resource->mem_type)) {
613 			ttm_bo_move_null(bo, new_mem);
614 			return 0;
615 		}
616 		ret = ttm_bo_wait_ctx(bo, ctx);
617 		if (ret)
618 			goto fail;
619 
620 		vmw_ttm_unbind(bo->bdev, bo->ttm);
621 		ttm_resource_free(bo, &bo->resource);
622 		ttm_bo_assign_mem(bo, new_mem);
623 		return 0;
624 	} else {
625 		ret = ttm_bo_move_memcpy(bo, ctx, new_mem);
626 		if (ret)
627 			goto fail;
628 	}
629 	return 0;
630 fail:
631 	vmw_move_notify(bo, new_mem, bo->resource);
632 	return ret;
633 }
634 
635 struct ttm_device_funcs vmw_bo_driver = {
636 	.ttm_tt_create = &vmw_ttm_tt_create,
637 	.ttm_tt_populate = &vmw_ttm_populate,
638 	.ttm_tt_unpopulate = &vmw_ttm_unpopulate,
639 	.ttm_tt_destroy = &vmw_ttm_destroy,
640 	.eviction_valuable = ttm_bo_eviction_valuable,
641 	.evict_flags = vmw_evict_flags,
642 	.move = vmw_move,
643 	.swap_notify = vmw_swap_notify,
644 	.io_mem_reserve = &vmw_ttm_io_mem_reserve,
645 };
646 
647 int vmw_bo_create_and_populate(struct vmw_private *dev_priv,
648 			       unsigned long bo_size,
649 			       struct ttm_buffer_object **bo_p)
650 {
651 	struct ttm_operation_ctx ctx = {
652 		.interruptible = false,
653 		.no_wait_gpu = false
654 	};
655 	struct ttm_buffer_object *bo;
656 	int ret;
657 
658 	ret = vmw_bo_create_kernel(dev_priv, bo_size,
659 				   &vmw_pt_sys_placement,
660 				   &bo);
661 	if (unlikely(ret != 0))
662 		return ret;
663 
664 	ret = ttm_bo_reserve(bo, false, true, NULL);
665 	BUG_ON(ret != 0);
666 	ret = vmw_ttm_populate(bo->bdev, bo->ttm, &ctx);
667 	if (likely(ret == 0)) {
668 		struct vmw_ttm_tt *vmw_tt =
669 			container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm);
670 		ret = vmw_ttm_map_dma(vmw_tt);
671 	}
672 
673 	ttm_bo_unreserve(bo);
674 
675 	if (likely(ret == 0))
676 		*bo_p = bo;
677 	return ret;
678 }
679