xref: /linux/drivers/gpu/drm/vmwgfx/vmwgfx_gem.c (revision 2697b79a469b68e3ad3640f55284359c1396278d)
1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /*
3  * Copyright 2021-2023 VMware, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without
8  * restriction, including without limitation the rights to use, copy,
9  * modify, merge, publish, distribute, sublicense, and/or sell copies
10  * of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  *
25  */
26 
27 #include "vmwgfx_bo.h"
28 #include "vmwgfx_drv.h"
29 
30 #include "drm/drm_prime.h"
31 #include "drm/drm_gem_ttm_helper.h"
32 
33 #include <linux/debugfs.h>
34 
35 static void vmw_gem_object_free(struct drm_gem_object *gobj)
36 {
37 	struct ttm_buffer_object *bo = drm_gem_ttm_of_gem(gobj);
38 	if (bo)
39 		ttm_bo_put(bo);
40 }
41 
42 static int vmw_gem_object_open(struct drm_gem_object *obj,
43 			       struct drm_file *file_priv)
44 {
45 	return 0;
46 }
47 
48 static void vmw_gem_object_close(struct drm_gem_object *obj,
49 				 struct drm_file *file_priv)
50 {
51 }
52 
53 static int vmw_gem_object_pin(struct drm_gem_object *obj)
54 {
55 	struct vmw_bo *vbo = to_vmw_bo(obj);
56 
57 	vmw_bo_pin_reserved(vbo, true);
58 
59 	return 0;
60 }
61 
62 static void vmw_gem_object_unpin(struct drm_gem_object *obj)
63 {
64 	struct vmw_bo *vbo = to_vmw_bo(obj);
65 
66 	vmw_bo_pin_reserved(vbo, false);
67 }
68 
69 static struct sg_table *vmw_gem_object_get_sg_table(struct drm_gem_object *obj)
70 {
71 	struct ttm_buffer_object *bo = drm_gem_ttm_of_gem(obj);
72 	struct vmw_ttm_tt *vmw_tt =
73 		container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm);
74 
75 	if (vmw_tt->vsgt.sgt)
76 		return vmw_tt->vsgt.sgt;
77 
78 	return drm_prime_pages_to_sg(obj->dev, vmw_tt->dma_ttm.pages, vmw_tt->dma_ttm.num_pages);
79 }
80 
81 static const struct vm_operations_struct vmw_vm_ops = {
82 	.pfn_mkwrite = vmw_bo_vm_mkwrite,
83 	.page_mkwrite = vmw_bo_vm_mkwrite,
84 	.fault = vmw_bo_vm_fault,
85 	.open = ttm_bo_vm_open,
86 	.close = ttm_bo_vm_close,
87 };
88 
89 static const struct drm_gem_object_funcs vmw_gem_object_funcs = {
90 	.free = vmw_gem_object_free,
91 	.open = vmw_gem_object_open,
92 	.close = vmw_gem_object_close,
93 	.print_info = drm_gem_ttm_print_info,
94 	.pin = vmw_gem_object_pin,
95 	.unpin = vmw_gem_object_unpin,
96 	.get_sg_table = vmw_gem_object_get_sg_table,
97 	.vmap = drm_gem_ttm_vmap,
98 	.vunmap = drm_gem_ttm_vunmap,
99 	.mmap = drm_gem_ttm_mmap,
100 	.vm_ops = &vmw_vm_ops,
101 };
102 
103 int vmw_gem_object_create(struct vmw_private *vmw,
104 			  struct vmw_bo_params *params,
105 			  struct vmw_bo **p_vbo)
106 {
107 	int ret = vmw_bo_create(vmw, params, p_vbo);
108 
109 	if (ret != 0)
110 		goto out_no_bo;
111 
112 	(*p_vbo)->tbo.base.funcs = &vmw_gem_object_funcs;
113 out_no_bo:
114 	return ret;
115 }
116 
117 int vmw_gem_object_create_with_handle(struct vmw_private *dev_priv,
118 				      struct drm_file *filp,
119 				      uint32_t size,
120 				      uint32_t *handle,
121 				      struct vmw_bo **p_vbo)
122 {
123 	int ret;
124 	struct vmw_bo_params params = {
125 		.domain = (dev_priv->has_mob) ? VMW_BO_DOMAIN_SYS : VMW_BO_DOMAIN_VRAM,
126 		.busy_domain = VMW_BO_DOMAIN_SYS,
127 		.bo_type = ttm_bo_type_device,
128 		.size = size,
129 		.pin = false
130 	};
131 
132 	ret = vmw_gem_object_create(dev_priv, &params, p_vbo);
133 	if (ret != 0)
134 		goto out_no_bo;
135 
136 	ret = drm_gem_handle_create(filp, &(*p_vbo)->tbo.base, handle);
137 out_no_bo:
138 	return ret;
139 }
140 
141 struct drm_gem_object *vmw_prime_import_sg_table(struct drm_device *dev,
142 						 struct dma_buf_attachment *attach,
143 						 struct sg_table *table)
144 {
145 	int ret;
146 	struct vmw_private *dev_priv = vmw_priv(dev);
147 	struct drm_gem_object *gem = NULL;
148 	struct vmw_bo *vbo;
149 	struct vmw_bo_params params = {
150 		.domain = (dev_priv->has_mob) ? VMW_BO_DOMAIN_SYS : VMW_BO_DOMAIN_VRAM,
151 		.busy_domain = VMW_BO_DOMAIN_SYS,
152 		.bo_type = ttm_bo_type_sg,
153 		.size = attach->dmabuf->size,
154 		.pin = false,
155 		.resv = attach->dmabuf->resv,
156 		.sg = table,
157 
158 	};
159 
160 	dma_resv_lock(params.resv, NULL);
161 
162 	ret = vmw_bo_create(dev_priv, &params, &vbo);
163 	if (ret != 0)
164 		goto out_no_bo;
165 
166 	vbo->tbo.base.funcs = &vmw_gem_object_funcs;
167 
168 	gem = &vbo->tbo.base;
169 out_no_bo:
170 	dma_resv_unlock(params.resv);
171 	return gem;
172 }
173 
174 int vmw_gem_object_create_ioctl(struct drm_device *dev, void *data,
175 				struct drm_file *filp)
176 {
177 	struct vmw_private *dev_priv = vmw_priv(dev);
178 	union drm_vmw_alloc_dmabuf_arg *arg =
179 	    (union drm_vmw_alloc_dmabuf_arg *)data;
180 	struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
181 	struct drm_vmw_dmabuf_rep *rep = &arg->rep;
182 	struct vmw_bo *vbo;
183 	uint32_t handle;
184 	int ret;
185 
186 	ret = vmw_gem_object_create_with_handle(dev_priv, filp,
187 						req->size, &handle, &vbo);
188 	if (ret)
189 		goto out_no_bo;
190 
191 	rep->handle = handle;
192 	rep->map_handle = drm_vma_node_offset_addr(&vbo->tbo.base.vma_node);
193 	rep->cur_gmr_id = handle;
194 	rep->cur_gmr_offset = 0;
195 	/* drop reference from allocate - handle holds it now */
196 	drm_gem_object_put(&vbo->tbo.base);
197 out_no_bo:
198 	return ret;
199 }
200 
201 #if defined(CONFIG_DEBUG_FS)
202 
203 static void vmw_bo_print_info(int id, struct vmw_bo *bo, struct seq_file *m)
204 {
205 	const char *placement;
206 	const char *type;
207 
208 	switch (bo->tbo.resource->mem_type) {
209 	case TTM_PL_SYSTEM:
210 		placement = " CPU";
211 		break;
212 	case VMW_PL_GMR:
213 		placement = " GMR";
214 		break;
215 	case VMW_PL_MOB:
216 		placement = " MOB";
217 		break;
218 	case VMW_PL_SYSTEM:
219 		placement = "VCPU";
220 		break;
221 	case TTM_PL_VRAM:
222 		placement = "VRAM";
223 		break;
224 	default:
225 		placement = "None";
226 		break;
227 	}
228 
229 	switch (bo->tbo.type) {
230 	case ttm_bo_type_device:
231 		type = "device";
232 		break;
233 	case ttm_bo_type_kernel:
234 		type = "kernel";
235 		break;
236 	case ttm_bo_type_sg:
237 		type = "sg    ";
238 		break;
239 	default:
240 		type = "none  ";
241 		break;
242 	}
243 
244 	seq_printf(m, "\t\t0x%08x: %12zu bytes %s, type = %s",
245 		   id, bo->tbo.base.size, placement, type);
246 	seq_printf(m, ", priority = %u, pin_count = %u, GEM refs = %d, TTM refs = %d",
247 		   bo->tbo.priority,
248 		   bo->tbo.pin_count,
249 		   kref_read(&bo->tbo.base.refcount),
250 		   kref_read(&bo->tbo.kref));
251 	seq_puts(m, "\n");
252 }
253 
254 static int vmw_debugfs_gem_info_show(struct seq_file *m, void *unused)
255 {
256 	struct vmw_private *vdev = (struct vmw_private *)m->private;
257 	struct drm_device *dev = &vdev->drm;
258 	struct drm_file *file;
259 	int r;
260 
261 	r = mutex_lock_interruptible(&dev->filelist_mutex);
262 	if (r)
263 		return r;
264 
265 	list_for_each_entry(file, &dev->filelist, lhead) {
266 		struct task_struct *task;
267 		struct drm_gem_object *gobj;
268 		struct pid *pid;
269 		int id;
270 
271 		/*
272 		 * Although we have a valid reference on file->pid, that does
273 		 * not guarantee that the task_struct who called get_pid() is
274 		 * still alive (e.g. get_pid(current) => fork() => exit()).
275 		 * Therefore, we need to protect this ->comm access using RCU.
276 		 */
277 		rcu_read_lock();
278 		pid = rcu_dereference(file->pid);
279 		task = pid_task(pid, PIDTYPE_TGID);
280 		seq_printf(m, "pid %8d command %s:\n", pid_nr(pid),
281 			   task ? task->comm : "<unknown>");
282 		rcu_read_unlock();
283 
284 		spin_lock(&file->table_lock);
285 		idr_for_each_entry(&file->object_idr, gobj, id) {
286 			struct vmw_bo *bo = to_vmw_bo(gobj);
287 
288 			vmw_bo_print_info(id, bo, m);
289 		}
290 		spin_unlock(&file->table_lock);
291 	}
292 
293 	mutex_unlock(&dev->filelist_mutex);
294 	return 0;
295 }
296 
297 DEFINE_SHOW_ATTRIBUTE(vmw_debugfs_gem_info);
298 
299 #endif
300 
301 void vmw_debugfs_gem_init(struct vmw_private *vdev)
302 {
303 #if defined(CONFIG_DEBUG_FS)
304 	struct drm_minor *minor = vdev->drm.primary;
305 	struct dentry *root = minor->debugfs_root;
306 
307 	debugfs_create_file("vmwgfx_gem_info", 0444, root, vdev,
308 			    &vmw_debugfs_gem_info_fops);
309 #endif
310 }
311