18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 OR MIT
28c2ecf20Sopenharmony_ci/**************************************************************************
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright 2009-2011 VMware, Inc., Palo Alto, CA., USA
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
78c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the
88c2ecf20Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
98c2ecf20Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
108c2ecf20Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
118c2ecf20Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
128c2ecf20Sopenharmony_ci * the following conditions:
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice (including the
158c2ecf20Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
168c2ecf20Sopenharmony_ci * of the Software.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
198c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
208c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
218c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
228c2ecf20Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
238c2ecf20Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
248c2ecf20Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci **************************************************************************/
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include "vmwgfx_drv.h"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ciint vmw_mmap(struct file *filp, struct vm_area_struct *vma)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	static const struct vm_operations_struct vmw_vm_ops = {
338c2ecf20Sopenharmony_ci		.pfn_mkwrite = vmw_bo_vm_mkwrite,
348c2ecf20Sopenharmony_ci		.page_mkwrite = vmw_bo_vm_mkwrite,
358c2ecf20Sopenharmony_ci		.fault = vmw_bo_vm_fault,
368c2ecf20Sopenharmony_ci		.open = ttm_bo_vm_open,
378c2ecf20Sopenharmony_ci		.close = ttm_bo_vm_close,
388c2ecf20Sopenharmony_ci#ifdef CONFIG_TRANSPARENT_HUGEPAGE
398c2ecf20Sopenharmony_ci		.huge_fault = vmw_bo_vm_huge_fault,
408c2ecf20Sopenharmony_ci#endif
418c2ecf20Sopenharmony_ci	};
428c2ecf20Sopenharmony_ci	struct drm_file *file_priv = filp->private_data;
438c2ecf20Sopenharmony_ci	struct vmw_private *dev_priv = vmw_priv(file_priv->minor->dev);
448c2ecf20Sopenharmony_ci	int ret = ttm_bo_mmap(filp, vma, &dev_priv->bdev);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	if (ret)
478c2ecf20Sopenharmony_ci		return ret;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	vma->vm_ops = &vmw_vm_ops;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	/* Use VM_PFNMAP rather than VM_MIXEDMAP if not a COW mapping */
528c2ecf20Sopenharmony_ci	if ((vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) != VM_MAYWRITE)
538c2ecf20Sopenharmony_ci		vma->vm_flags = (vma->vm_flags & ~VM_MIXEDMAP) | VM_PFNMAP;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	return 0;
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/* struct vmw_validation_mem callback */
598c2ecf20Sopenharmony_cistatic int vmw_vmt_reserve(struct vmw_validation_mem *m, size_t size)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	static struct ttm_operation_ctx ctx = {.interruptible = false,
628c2ecf20Sopenharmony_ci					       .no_wait_gpu = false};
638c2ecf20Sopenharmony_ci	struct vmw_private *dev_priv = container_of(m, struct vmw_private, vvm);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	return ttm_mem_global_alloc(vmw_mem_glob(dev_priv), size, &ctx);
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/* struct vmw_validation_mem callback */
698c2ecf20Sopenharmony_cistatic void vmw_vmt_unreserve(struct vmw_validation_mem *m, size_t size)
708c2ecf20Sopenharmony_ci{
718c2ecf20Sopenharmony_ci	struct vmw_private *dev_priv = container_of(m, struct vmw_private, vvm);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	return ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci/**
778c2ecf20Sopenharmony_ci * vmw_validation_mem_init_ttm - Interface the validation memory tracker
788c2ecf20Sopenharmony_ci * to ttm.
798c2ecf20Sopenharmony_ci * @dev_priv: Pointer to struct vmw_private. The reason we choose a vmw private
808c2ecf20Sopenharmony_ci * rather than a struct vmw_validation_mem is to make sure assumption in the
818c2ecf20Sopenharmony_ci * callbacks that struct vmw_private derives from struct vmw_validation_mem
828c2ecf20Sopenharmony_ci * holds true.
838c2ecf20Sopenharmony_ci * @gran: The recommended allocation granularity
848c2ecf20Sopenharmony_ci */
858c2ecf20Sopenharmony_civoid vmw_validation_mem_init_ttm(struct vmw_private *dev_priv, size_t gran)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	struct vmw_validation_mem *vvm = &dev_priv->vvm;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	vvm->reserve_mem = vmw_vmt_reserve;
908c2ecf20Sopenharmony_ci	vvm->unreserve_mem = vmw_vmt_unreserve;
918c2ecf20Sopenharmony_ci	vvm->gran = gran;
928c2ecf20Sopenharmony_ci}
93