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