1dff96888SDirk Hohndel (VMware) // SPDX-License-Identifier: GPL-2.0 OR MIT
2fb1d9738SJakob Bornecrantz /**************************************************************************
3fb1d9738SJakob Bornecrantz  *
4dff96888SDirk Hohndel (VMware)  * Copyright 2009-2011 VMware, Inc., Palo Alto, CA., USA
5fb1d9738SJakob Bornecrantz  *
6fb1d9738SJakob Bornecrantz  * Permission is hereby granted, free of charge, to any person obtaining a
7fb1d9738SJakob Bornecrantz  * copy of this software and associated documentation files (the
8fb1d9738SJakob Bornecrantz  * "Software"), to deal in the Software without restriction, including
9fb1d9738SJakob Bornecrantz  * without limitation the rights to use, copy, modify, merge, publish,
10fb1d9738SJakob Bornecrantz  * distribute, sub license, and/or sell copies of the Software, and to
11fb1d9738SJakob Bornecrantz  * permit persons to whom the Software is furnished to do so, subject to
12fb1d9738SJakob Bornecrantz  * the following conditions:
13fb1d9738SJakob Bornecrantz  *
14fb1d9738SJakob Bornecrantz  * The above copyright notice and this permission notice (including the
15fb1d9738SJakob Bornecrantz  * next paragraph) shall be included in all copies or substantial portions
16fb1d9738SJakob Bornecrantz  * of the Software.
17fb1d9738SJakob Bornecrantz  *
18fb1d9738SJakob Bornecrantz  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19fb1d9738SJakob Bornecrantz  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20fb1d9738SJakob Bornecrantz  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21fb1d9738SJakob Bornecrantz  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22fb1d9738SJakob Bornecrantz  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23fb1d9738SJakob Bornecrantz  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24fb1d9738SJakob Bornecrantz  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25fb1d9738SJakob Bornecrantz  *
26fb1d9738SJakob Bornecrantz  **************************************************************************/
27fb1d9738SJakob Bornecrantz 
28fb1d9738SJakob Bornecrantz #include "vmwgfx_drv.h"
29fb1d9738SJakob Bornecrantz 
vmw_bo_vm_lookup(struct ttm_device * bdev,struct drm_file * filp,unsigned long offset,unsigned long pages,struct ttm_buffer_object ** p_bo)308afa13a0SZack Rusin static int vmw_bo_vm_lookup(struct ttm_device *bdev,
318afa13a0SZack Rusin 				   struct drm_file *filp,
32e65d096fSThomas Zimmermann 				   unsigned long offset,
338afa13a0SZack Rusin 				   unsigned long pages,
348afa13a0SZack Rusin 				   struct ttm_buffer_object **p_bo)
35e65d096fSThomas Zimmermann {
36e65d096fSThomas Zimmermann 	struct vmw_private *dev_priv = container_of(bdev, struct vmw_private, bdev);
37e65d096fSThomas Zimmermann 	struct drm_device *drm = &dev_priv->drm;
38e65d096fSThomas Zimmermann 	struct drm_vma_offset_node *node;
398afa13a0SZack Rusin 	int ret;
408afa13a0SZack Rusin 
418afa13a0SZack Rusin 	*p_bo = NULL;
42e65d096fSThomas Zimmermann 
43e65d096fSThomas Zimmermann 	drm_vma_offset_lock_lookup(bdev->vma_manager);
44e65d096fSThomas Zimmermann 
45e65d096fSThomas Zimmermann 	node = drm_vma_offset_lookup_locked(bdev->vma_manager, offset, pages);
46e65d096fSThomas Zimmermann 	if (likely(node)) {
478afa13a0SZack Rusin 		*p_bo = container_of(node, struct ttm_buffer_object,
48e65d096fSThomas Zimmermann 				  base.vma_node);
498afa13a0SZack Rusin 		*p_bo = ttm_bo_get_unless_zero(*p_bo);
50e65d096fSThomas Zimmermann 	}
51e65d096fSThomas Zimmermann 
52e65d096fSThomas Zimmermann 	drm_vma_offset_unlock_lookup(bdev->vma_manager);
53e65d096fSThomas Zimmermann 
548afa13a0SZack Rusin 	if (!*p_bo) {
55e65d096fSThomas Zimmermann 		drm_err(drm, "Could not find buffer object to map\n");
568afa13a0SZack Rusin 		return -EINVAL;
578afa13a0SZack Rusin 	}
58e65d096fSThomas Zimmermann 
598afa13a0SZack Rusin 	if (!drm_vma_node_is_allowed(node, filp)) {
608afa13a0SZack Rusin 		ret = -EACCES;
618afa13a0SZack Rusin 		goto out_no_access;
628afa13a0SZack Rusin 	}
638afa13a0SZack Rusin 
648afa13a0SZack Rusin 	return 0;
658afa13a0SZack Rusin out_no_access:
668afa13a0SZack Rusin 	ttm_bo_put(*p_bo);
678afa13a0SZack Rusin 	return ret;
68e65d096fSThomas Zimmermann }
69e65d096fSThomas Zimmermann 
vmw_mmap(struct file * filp,struct vm_area_struct * vma)70fb1d9738SJakob Bornecrantz int vmw_mmap(struct file *filp, struct vm_area_struct *vma)
71fb1d9738SJakob Bornecrantz {
72b7468b15SThomas Hellstrom 	static const struct vm_operations_struct vmw_vm_ops = {
73b7468b15SThomas Hellstrom 		.pfn_mkwrite = vmw_bo_vm_mkwrite,
74b7468b15SThomas Hellstrom 		.page_mkwrite = vmw_bo_vm_mkwrite,
75b7468b15SThomas Hellstrom 		.fault = vmw_bo_vm_fault,
76b7468b15SThomas Hellstrom 		.open = ttm_bo_vm_open,
7775390281SThomas Hellstrom (VMware) 		.close = ttm_bo_vm_close,
78b7468b15SThomas Hellstrom 	};
79bed2dd84SThomas Zimmermann 	struct drm_file *file_priv = filp->private_data;
80bed2dd84SThomas Zimmermann 	struct vmw_private *dev_priv = vmw_priv(file_priv->minor->dev);
81e65d096fSThomas Zimmermann 	struct ttm_device *bdev = &dev_priv->bdev;
82e65d096fSThomas Zimmermann 	struct ttm_buffer_object *bo;
83e65d096fSThomas Zimmermann 	int ret;
84fb1d9738SJakob Bornecrantz 
85e65d096fSThomas Zimmermann 	if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET_START))
86e65d096fSThomas Zimmermann 		return -EINVAL;
87e65d096fSThomas Zimmermann 
888afa13a0SZack Rusin 	ret = vmw_bo_vm_lookup(bdev, file_priv, vma->vm_pgoff, vma_pages(vma), &bo);
89e65d096fSThomas Zimmermann 	if (unlikely(ret != 0))
908afa13a0SZack Rusin 		return ret;
91e65d096fSThomas Zimmermann 
92e65d096fSThomas Zimmermann 	ret = ttm_bo_mmap_obj(vma, bo);
93e65d096fSThomas Zimmermann 	if (unlikely(ret != 0))
94e65d096fSThomas Zimmermann 		goto out_unref;
95b7468b15SThomas Hellstrom 
96b7468b15SThomas Hellstrom 	vma->vm_ops = &vmw_vm_ops;
97b7468b15SThomas Hellstrom 
98b2041425SThomas Hellstrom 	/* Use VM_PFNMAP rather than VM_MIXEDMAP if not a COW mapping */
99ca6eb14dSPeter Xu 	if (!is_cow_mapping(vma->vm_flags))
100*1c71222eSSuren Baghdasaryan 		vm_flags_mod(vma, VM_PFNMAP, VM_MIXEDMAP);
101b2041425SThomas Hellstrom 
102e65d096fSThomas Zimmermann 	ttm_bo_put(bo); /* release extra ref taken by ttm_bo_mmap_obj() */
103e65d096fSThomas Zimmermann 
104b7468b15SThomas Hellstrom 	return 0;
105e65d096fSThomas Zimmermann 
106e65d096fSThomas Zimmermann out_unref:
107e65d096fSThomas Zimmermann 	ttm_bo_put(bo);
108e65d096fSThomas Zimmermann 	return ret;
109fb1d9738SJakob Bornecrantz }
110fb1d9738SJakob Bornecrantz 
111