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