1 /*	$NetBSD: vmwgfx_ttm_glue.c,v 1.3 2021/12/18 23:45:45 riastradh Exp $	*/
2 
3 // SPDX-License-Identifier: GPL-2.0 OR MIT
4 /**************************************************************************
5  *
6  * Copyright 2009-2011 VMware, Inc., Palo Alto, CA., USA
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sub license, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the
17  * next paragraph) shall be included in all copies or substantial portions
18  * of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
23  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
24  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26  * USE OR OTHER DEALINGS IN THE SOFTWARE.
27  *
28  **************************************************************************/
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: vmwgfx_ttm_glue.c,v 1.3 2021/12/18 23:45:45 riastradh Exp $");
32 
33 #include "vmwgfx_drv.h"
34 
vmw_mmap(struct file * filp,struct vm_area_struct * vma)35 int vmw_mmap(struct file *filp, struct vm_area_struct *vma)
36 {
37 	static const struct vm_operations_struct vmw_vm_ops = {
38 		.pfn_mkwrite = vmw_bo_vm_mkwrite,
39 		.page_mkwrite = vmw_bo_vm_mkwrite,
40 		.fault = vmw_bo_vm_fault,
41 		.open = ttm_bo_vm_open,
42 		.close = ttm_bo_vm_close
43 	};
44 	struct drm_file *file_priv = filp->private_data;
45 	struct vmw_private *dev_priv = vmw_priv(file_priv->minor->dev);
46 	int ret = ttm_bo_mmap(filp, vma, &dev_priv->bdev);
47 
48 	if (ret)
49 		return ret;
50 
51 	vma->vm_ops = &vmw_vm_ops;
52 
53 	/* Use VM_PFNMAP rather than VM_MIXEDMAP if not a COW mapping */
54 	if ((vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) != VM_MAYWRITE)
55 		vma->vm_flags = (vma->vm_flags & ~VM_MIXEDMAP) | VM_PFNMAP;
56 
57 	return 0;
58 }
59 
60 /* struct vmw_validation_mem callback */
vmw_vmt_reserve(struct vmw_validation_mem * m,size_t size)61 static int vmw_vmt_reserve(struct vmw_validation_mem *m, size_t size)
62 {
63 	static struct ttm_operation_ctx ctx = {.interruptible = false,
64 					       .no_wait_gpu = false};
65 	struct vmw_private *dev_priv = container_of(m, struct vmw_private, vvm);
66 
67 	return ttm_mem_global_alloc(vmw_mem_glob(dev_priv), size, &ctx);
68 }
69 
70 /* struct vmw_validation_mem callback */
vmw_vmt_unreserve(struct vmw_validation_mem * m,size_t size)71 static void vmw_vmt_unreserve(struct vmw_validation_mem *m, size_t size)
72 {
73 	struct vmw_private *dev_priv = container_of(m, struct vmw_private, vvm);
74 
75 	return ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
76 }
77 
78 /**
79  * vmw_validation_mem_init_ttm - Interface the validation memory tracker
80  * to ttm.
81  * @dev_priv: Pointer to struct vmw_private. The reason we choose a vmw private
82  * rather than a struct vmw_validation_mem is to make sure assumption in the
83  * callbacks that struct vmw_private derives from struct vmw_validation_mem
84  * holds true.
85  * @gran: The recommended allocation granularity
86  */
vmw_validation_mem_init_ttm(struct vmw_private * dev_priv,size_t gran)87 void vmw_validation_mem_init_ttm(struct vmw_private *dev_priv, size_t gran)
88 {
89 	struct vmw_validation_mem *vvm = &dev_priv->vvm;
90 
91 	vvm->reserve_mem = vmw_vmt_reserve;
92 	vvm->unreserve_mem = vmw_vmt_unreserve;
93 	vvm->gran = gran;
94 }
95