xref: /openbmc/linux/drivers/gpu/drm/vmwgfx/vmwgfx_gem.c (revision aad29a73199b7fbccfbabea3f1ee627ad1924f52)
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 
vmw_gem_object_free(struct drm_gem_object * gobj)33 static void vmw_gem_object_free(struct drm_gem_object *gobj)
34 {
35 	struct ttm_buffer_object *bo = drm_gem_ttm_of_gem(gobj);
36 	if (bo)
37 		ttm_bo_put(bo);
38 }
39 
vmw_gem_object_open(struct drm_gem_object * obj,struct drm_file * file_priv)40 static int vmw_gem_object_open(struct drm_gem_object *obj,
41 			       struct drm_file *file_priv)
42 {
43 	return 0;
44 }
45 
vmw_gem_object_close(struct drm_gem_object * obj,struct drm_file * file_priv)46 static void vmw_gem_object_close(struct drm_gem_object *obj,
47 				 struct drm_file *file_priv)
48 {
49 }
50 
vmw_gem_pin_private(struct drm_gem_object * obj,bool do_pin)51 static int vmw_gem_pin_private(struct drm_gem_object *obj, bool do_pin)
52 {
53 	struct ttm_buffer_object *bo = drm_gem_ttm_of_gem(obj);
54 	struct vmw_bo *vbo = to_vmw_bo(obj);
55 	int ret;
56 
57 	ret = ttm_bo_reserve(bo, false, false, NULL);
58 	if (unlikely(ret != 0))
59 		goto err;
60 
61 	vmw_bo_pin_reserved(vbo, do_pin);
62 
63 	ttm_bo_unreserve(bo);
64 
65 err:
66 	return ret;
67 }
68 
69 
vmw_gem_object_pin(struct drm_gem_object * obj)70 static int vmw_gem_object_pin(struct drm_gem_object *obj)
71 {
72 	return vmw_gem_pin_private(obj, true);
73 }
74 
vmw_gem_object_unpin(struct drm_gem_object * obj)75 static void vmw_gem_object_unpin(struct drm_gem_object *obj)
76 {
77 	vmw_gem_pin_private(obj, false);
78 }
79 
vmw_gem_object_get_sg_table(struct drm_gem_object * obj)80 static struct sg_table *vmw_gem_object_get_sg_table(struct drm_gem_object *obj)
81 {
82 	struct ttm_buffer_object *bo = drm_gem_ttm_of_gem(obj);
83 	struct vmw_ttm_tt *vmw_tt =
84 		container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm);
85 
86 	if (vmw_tt->vsgt.sgt)
87 		return vmw_tt->vsgt.sgt;
88 
89 	return drm_prime_pages_to_sg(obj->dev, vmw_tt->dma_ttm.pages, vmw_tt->dma_ttm.num_pages);
90 }
91 
92 static const struct vm_operations_struct vmw_vm_ops = {
93 	.pfn_mkwrite = vmw_bo_vm_mkwrite,
94 	.page_mkwrite = vmw_bo_vm_mkwrite,
95 	.fault = vmw_bo_vm_fault,
96 	.open = ttm_bo_vm_open,
97 	.close = ttm_bo_vm_close,
98 };
99 
100 static const struct drm_gem_object_funcs vmw_gem_object_funcs = {
101 	.free = vmw_gem_object_free,
102 	.open = vmw_gem_object_open,
103 	.close = vmw_gem_object_close,
104 	.print_info = drm_gem_ttm_print_info,
105 	.pin = vmw_gem_object_pin,
106 	.unpin = vmw_gem_object_unpin,
107 	.get_sg_table = vmw_gem_object_get_sg_table,
108 	.vmap = drm_gem_ttm_vmap,
109 	.vunmap = drm_gem_ttm_vunmap,
110 	.mmap = drm_gem_ttm_mmap,
111 	.vm_ops = &vmw_vm_ops,
112 };
113 
vmw_gem_object_create(struct vmw_private * vmw,struct vmw_bo_params * params,struct vmw_bo ** p_vbo)114 int vmw_gem_object_create(struct vmw_private *vmw,
115 			  struct vmw_bo_params *params,
116 			  struct vmw_bo **p_vbo)
117 {
118 	int ret = vmw_bo_create(vmw, params, p_vbo);
119 
120 	if (ret != 0)
121 		goto out_no_bo;
122 
123 	(*p_vbo)->tbo.base.funcs = &vmw_gem_object_funcs;
124 out_no_bo:
125 	return ret;
126 }
127 
vmw_gem_object_create_with_handle(struct vmw_private * dev_priv,struct drm_file * filp,uint32_t size,uint32_t * handle,struct vmw_bo ** p_vbo)128 int vmw_gem_object_create_with_handle(struct vmw_private *dev_priv,
129 				      struct drm_file *filp,
130 				      uint32_t size,
131 				      uint32_t *handle,
132 				      struct vmw_bo **p_vbo)
133 {
134 	int ret;
135 	struct vmw_bo_params params = {
136 		.domain = (dev_priv->has_mob) ? VMW_BO_DOMAIN_SYS : VMW_BO_DOMAIN_VRAM,
137 		.busy_domain = VMW_BO_DOMAIN_SYS,
138 		.bo_type = ttm_bo_type_device,
139 		.size = size,
140 		.pin = false
141 	};
142 
143 	ret = vmw_gem_object_create(dev_priv, &params, p_vbo);
144 	if (ret != 0)
145 		goto out_no_bo;
146 
147 	ret = drm_gem_handle_create(filp, &(*p_vbo)->tbo.base, handle);
148 out_no_bo:
149 	return ret;
150 }
151 
vmw_prime_import_sg_table(struct drm_device * dev,struct dma_buf_attachment * attach,struct sg_table * table)152 struct drm_gem_object *vmw_prime_import_sg_table(struct drm_device *dev,
153 						 struct dma_buf_attachment *attach,
154 						 struct sg_table *table)
155 {
156 	int ret;
157 	struct vmw_private *dev_priv = vmw_priv(dev);
158 	struct drm_gem_object *gem = NULL;
159 	struct vmw_bo *vbo;
160 	struct vmw_bo_params params = {
161 		.domain = (dev_priv->has_mob) ? VMW_BO_DOMAIN_SYS : VMW_BO_DOMAIN_VRAM,
162 		.busy_domain = VMW_BO_DOMAIN_SYS,
163 		.bo_type = ttm_bo_type_sg,
164 		.size = attach->dmabuf->size,
165 		.pin = false,
166 		.keep_resv = true,
167 		.resv = attach->dmabuf->resv,
168 		.sg = table,
169 
170 	};
171 
172 	dma_resv_lock(params.resv, NULL);
173 
174 	ret = vmw_bo_create(dev_priv, &params, &vbo);
175 	if (ret != 0)
176 		goto out_no_bo;
177 
178 	vbo->tbo.base.funcs = &vmw_gem_object_funcs;
179 
180 	gem = &vbo->tbo.base;
181 out_no_bo:
182 	dma_resv_unlock(params.resv);
183 	return gem;
184 }
185 
vmw_gem_object_create_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)186 int vmw_gem_object_create_ioctl(struct drm_device *dev, void *data,
187 				struct drm_file *filp)
188 {
189 	struct vmw_private *dev_priv = vmw_priv(dev);
190 	union drm_vmw_alloc_dmabuf_arg *arg =
191 	    (union drm_vmw_alloc_dmabuf_arg *)data;
192 	struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
193 	struct drm_vmw_dmabuf_rep *rep = &arg->rep;
194 	struct vmw_bo *vbo;
195 	uint32_t handle;
196 	int ret;
197 
198 	ret = vmw_gem_object_create_with_handle(dev_priv, filp,
199 						req->size, &handle, &vbo);
200 	if (ret)
201 		goto out_no_bo;
202 
203 	rep->handle = handle;
204 	rep->map_handle = drm_vma_node_offset_addr(&vbo->tbo.base.vma_node);
205 	rep->cur_gmr_id = handle;
206 	rep->cur_gmr_offset = 0;
207 	/* drop reference from allocate - handle holds it now */
208 	drm_gem_object_put(&vbo->tbo.base);
209 out_no_bo:
210 	return ret;
211 }
212 
213 #if defined(CONFIG_DEBUG_FS)
214 
vmw_bo_print_info(int id,struct vmw_bo * bo,struct seq_file * m)215 static void vmw_bo_print_info(int id, struct vmw_bo *bo, struct seq_file *m)
216 {
217 	const char *placement;
218 	const char *type;
219 
220 	switch (bo->tbo.resource->mem_type) {
221 	case TTM_PL_SYSTEM:
222 		placement = " CPU";
223 		break;
224 	case VMW_PL_GMR:
225 		placement = " GMR";
226 		break;
227 	case VMW_PL_MOB:
228 		placement = " MOB";
229 		break;
230 	case VMW_PL_SYSTEM:
231 		placement = "VCPU";
232 		break;
233 	case TTM_PL_VRAM:
234 		placement = "VRAM";
235 		break;
236 	default:
237 		placement = "None";
238 		break;
239 	}
240 
241 	switch (bo->tbo.type) {
242 	case ttm_bo_type_device:
243 		type = "device";
244 		break;
245 	case ttm_bo_type_kernel:
246 		type = "kernel";
247 		break;
248 	case ttm_bo_type_sg:
249 		type = "sg    ";
250 		break;
251 	default:
252 		type = "none  ";
253 		break;
254 	}
255 
256 	seq_printf(m, "\t\t0x%08x: %12zu bytes %s, type = %s",
257 		   id, bo->tbo.base.size, placement, type);
258 	seq_printf(m, ", priority = %u, pin_count = %u, GEM refs = %d, TTM refs = %d",
259 		   bo->tbo.priority,
260 		   bo->tbo.pin_count,
261 		   kref_read(&bo->tbo.base.refcount),
262 		   kref_read(&bo->tbo.kref));
263 	seq_puts(m, "\n");
264 }
265 
vmw_debugfs_gem_info_show(struct seq_file * m,void * unused)266 static int vmw_debugfs_gem_info_show(struct seq_file *m, void *unused)
267 {
268 	struct vmw_private *vdev = (struct vmw_private *)m->private;
269 	struct drm_device *dev = &vdev->drm;
270 	struct drm_file *file;
271 	int r;
272 
273 	r = mutex_lock_interruptible(&dev->filelist_mutex);
274 	if (r)
275 		return r;
276 
277 	list_for_each_entry(file, &dev->filelist, lhead) {
278 		struct task_struct *task;
279 		struct drm_gem_object *gobj;
280 		struct pid *pid;
281 		int id;
282 
283 		/*
284 		 * Although we have a valid reference on file->pid, that does
285 		 * not guarantee that the task_struct who called get_pid() is
286 		 * still alive (e.g. get_pid(current) => fork() => exit()).
287 		 * Therefore, we need to protect this ->comm access using RCU.
288 		 */
289 		rcu_read_lock();
290 		pid = rcu_dereference(file->pid);
291 		task = pid_task(pid, PIDTYPE_TGID);
292 		seq_printf(m, "pid %8d command %s:\n", pid_nr(pid),
293 			   task ? task->comm : "<unknown>");
294 		rcu_read_unlock();
295 
296 		spin_lock(&file->table_lock);
297 		idr_for_each_entry(&file->object_idr, gobj, id) {
298 			struct vmw_bo *bo = to_vmw_bo(gobj);
299 
300 			vmw_bo_print_info(id, bo, m);
301 		}
302 		spin_unlock(&file->table_lock);
303 	}
304 
305 	mutex_unlock(&dev->filelist_mutex);
306 	return 0;
307 }
308 
309 DEFINE_SHOW_ATTRIBUTE(vmw_debugfs_gem_info);
310 
311 #endif
312 
vmw_debugfs_gem_init(struct vmw_private * vdev)313 void vmw_debugfs_gem_init(struct vmw_private *vdev)
314 {
315 #if defined(CONFIG_DEBUG_FS)
316 	struct drm_minor *minor = vdev->drm.primary;
317 	struct dentry *root = minor->debugfs_root;
318 
319 	debugfs_create_file("vmwgfx_gem_info", 0444, root, vdev,
320 			    &vmw_debugfs_gem_info_fops);
321 #endif
322 }
323