xref: /linux/drivers/gpu/drm/vmwgfx/vmwgfx_prime.c (revision 29b4a6996c244f0d360537d6a4a0996468372c17)
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
3  *
4  * Copyright (c) 2013-2024 Broadcom. All Rights Reserved. The term
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sub license, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial portions
17  * of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25  * USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  **************************************************************************/
28 /*
29  * Authors:
30  *     Thomas Hellstrom <thellstrom@vmware.com>
31  *
32  */
33 
34 #include "vmwgfx_drv.h"
35 #include "vmwgfx_bo.h"
36 #include "ttm_object.h"
37 #include <linux/dma-buf.h>
38 
39 /*
40  * DMA-BUF attach- and mapping methods. No need to implement
41  * these until we have other virtual devices use them.
42  */
43 
vmw_prime_map_attach(struct dma_buf * dma_buf,struct dma_buf_attachment * attach)44 static int vmw_prime_map_attach(struct dma_buf *dma_buf,
45 				struct dma_buf_attachment *attach)
46 {
47 	return -ENOSYS;
48 }
49 
vmw_prime_map_detach(struct dma_buf * dma_buf,struct dma_buf_attachment * attach)50 static void vmw_prime_map_detach(struct dma_buf *dma_buf,
51 				 struct dma_buf_attachment *attach)
52 {
53 }
54 
vmw_prime_map_dma_buf(struct dma_buf_attachment * attach,enum dma_data_direction dir)55 static struct sg_table *vmw_prime_map_dma_buf(struct dma_buf_attachment *attach,
56 					      enum dma_data_direction dir)
57 {
58 	return ERR_PTR(-ENOSYS);
59 }
60 
vmw_prime_unmap_dma_buf(struct dma_buf_attachment * attach,struct sg_table * sgb,enum dma_data_direction dir)61 static void vmw_prime_unmap_dma_buf(struct dma_buf_attachment *attach,
62 				    struct sg_table *sgb,
63 				    enum dma_data_direction dir)
64 {
65 }
66 
67 const struct dma_buf_ops vmw_prime_dmabuf_ops =  {
68 	.attach = vmw_prime_map_attach,
69 	.detach = vmw_prime_map_detach,
70 	.map_dma_buf = vmw_prime_map_dma_buf,
71 	.unmap_dma_buf = vmw_prime_unmap_dma_buf,
72 	.release = NULL,
73 };
74 
vmw_prime_fd_to_handle(struct drm_device * dev,struct drm_file * file_priv,int fd,u32 * handle)75 int vmw_prime_fd_to_handle(struct drm_device *dev,
76 			   struct drm_file *file_priv,
77 			   int fd, u32 *handle)
78 {
79 	struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
80 	int ret = ttm_prime_fd_to_handle(tfile, fd, handle);
81 
82 	if (ret)
83 		ret = drm_gem_prime_fd_to_handle(dev, file_priv, fd, handle);
84 
85 	return ret;
86 }
87 
vmw_prime_handle_to_fd(struct drm_device * dev,struct drm_file * file_priv,uint32_t handle,uint32_t flags,int * prime_fd)88 int vmw_prime_handle_to_fd(struct drm_device *dev,
89 			   struct drm_file *file_priv,
90 			   uint32_t handle, uint32_t flags,
91 			   int *prime_fd)
92 {
93 	struct vmw_private *vmw = vmw_priv(dev);
94 	struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
95 	struct vmw_bo *vbo;
96 	int ret;
97 	int surf_handle;
98 
99 	if (handle > VMWGFX_NUM_MOB) {
100 		ret = ttm_prime_handle_to_fd(tfile, handle, flags, prime_fd);
101 	} else {
102 		ret = vmw_user_bo_lookup(file_priv, handle, &vbo);
103 		if (ret)
104 			return ret;
105 		if (vbo && vbo->is_dumb) {
106 			ret = drm_gem_prime_handle_to_fd(dev, file_priv, handle,
107 							 flags, prime_fd);
108 		} else {
109 			surf_handle = vmw_lookup_surface_handle_for_buffer(vmw,
110 									   vbo,
111 									   handle);
112 			if (surf_handle > 0)
113 				ret = ttm_prime_handle_to_fd(tfile, surf_handle,
114 							     flags, prime_fd);
115 			else
116 				ret = drm_gem_prime_handle_to_fd(dev, file_priv,
117 								 handle, flags,
118 								 prime_fd);
119 		}
120 		vmw_user_bo_unref(&vbo);
121 	}
122 
123 	return ret;
124 }
125