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