1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
3 * Copyright 2021 VMware, Inc.
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:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
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
27 #include "vmwgfx_drv.h"
29 #include "drm/drm_prime.h"
30 #include "drm/drm_gem_ttm_helper.h"
33 * vmw_buffer_object - Convert a struct ttm_buffer_object to a struct
36 * @bo: Pointer to the TTM buffer object.
37 * Return: Pointer to the struct vmw_buffer_object embedding the
40 static struct vmw_buffer_object *
41 vmw_buffer_object(struct ttm_buffer_object *bo)
43 return container_of(bo, struct vmw_buffer_object, base);
46 static void vmw_gem_object_free(struct drm_gem_object *gobj)
48 struct ttm_buffer_object *bo = drm_gem_ttm_of_gem(gobj);
54 static int vmw_gem_object_open(struct drm_gem_object *obj,
55 struct drm_file *file_priv)
60 static void vmw_gem_object_close(struct drm_gem_object *obj,
61 struct drm_file *file_priv)
65 static int vmw_gem_pin_private(struct drm_gem_object *obj, bool do_pin)
67 struct ttm_buffer_object *bo = drm_gem_ttm_of_gem(obj);
68 struct vmw_buffer_object *vbo = vmw_buffer_object(bo);
71 ret = ttm_bo_reserve(bo, false, false, NULL);
72 if (unlikely(ret != 0))
75 vmw_bo_pin_reserved(vbo, do_pin);
84 static int vmw_gem_object_pin(struct drm_gem_object *obj)
86 return vmw_gem_pin_private(obj, true);
89 static void vmw_gem_object_unpin(struct drm_gem_object *obj)
91 vmw_gem_pin_private(obj, false);
94 static struct sg_table *vmw_gem_object_get_sg_table(struct drm_gem_object *obj)
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);
100 if (vmw_tt->vsgt.sgt)
101 return vmw_tt->vsgt.sgt;
103 return drm_prime_pages_to_sg(obj->dev, vmw_tt->dma_ttm.pages, vmw_tt->dma_ttm.num_pages);
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,
121 * vmw_gem_destroy - vmw buffer object destructor
123 * @bo: Pointer to the embedded struct ttm_buffer_object
125 void vmw_gem_destroy(struct ttm_buffer_object *bo)
127 struct vmw_buffer_object *vbo = vmw_buffer_object(bo);
130 WARN_ON(!RB_EMPTY_ROOT(&vbo->res_tree));
132 drm_gem_object_release(&vbo->base.base);
136 int vmw_gem_object_create_with_handle(struct vmw_private *dev_priv,
137 struct drm_file *filp,
140 struct vmw_buffer_object **p_vbo)
144 ret = vmw_bo_create(dev_priv, size,
145 (dev_priv->has_mob) ?
147 &vmw_vram_sys_placement,
148 true, false, &vmw_gem_destroy, p_vbo);
150 (*p_vbo)->base.base.funcs = &vmw_gem_object_funcs;
154 ret = drm_gem_handle_create(filp, &(*p_vbo)->base.base, handle);
155 /* drop reference from allocate - handle holds it now */
156 drm_gem_object_put(&(*p_vbo)->base.base);
162 int vmw_gem_object_create_ioctl(struct drm_device *dev, void *data,
163 struct drm_file *filp)
165 struct vmw_private *dev_priv = vmw_priv(dev);
166 union drm_vmw_alloc_dmabuf_arg *arg =
167 (union drm_vmw_alloc_dmabuf_arg *)data;
168 struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
169 struct drm_vmw_dmabuf_rep *rep = &arg->rep;
170 struct vmw_buffer_object *vbo;
174 ret = vmw_gem_object_create_with_handle(dev_priv, filp,
175 req->size, &handle, &vbo);
179 rep->handle = handle;
180 rep->map_handle = drm_vma_node_offset_addr(&vbo->base.base.vma_node);
181 rep->cur_gmr_id = handle;
182 rep->cur_gmr_offset = 0;
187 #if defined(CONFIG_DEBUG_FS)
189 static void vmw_bo_print_info(int id, struct vmw_buffer_object *bo, struct seq_file *m)
191 const char *placement;
194 switch (bo->base.resource->mem_type) {
215 switch (bo->base.type) {
216 case ttm_bo_type_device:
219 case ttm_bo_type_kernel:
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",
235 kref_read(&bo->base.base.refcount),
236 kref_read(&bo->base.kref));
240 static int vmw_debugfs_gem_info_show(struct seq_file *m, void *unused)
242 struct vmw_private *vdev = (struct vmw_private *)m->private;
243 struct drm_device *dev = &vdev->drm;
244 struct drm_file *file;
247 r = mutex_lock_interruptible(&dev->filelist_mutex);
251 list_for_each_entry(file, &dev->filelist, lhead) {
252 struct task_struct *task;
253 struct drm_gem_object *gobj;
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.
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>");
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);
272 vmw_bo_print_info(id, bo, m);
274 spin_unlock(&file->table_lock);
277 mutex_unlock(&dev->filelist_mutex);
281 DEFINE_SHOW_ATTRIBUTE(vmw_debugfs_gem_info);
285 void vmw_debugfs_gem_init(struct vmw_private *vdev)
287 #if defined(CONFIG_DEBUG_FS)
288 struct drm_minor *minor = vdev->drm.primary;
289 struct dentry *root = minor->debugfs_root;
291 debugfs_create_file("vmwgfx_gem_info", 0444, root, vdev,
292 &vmw_debugfs_gem_info_fops);