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