2 * Copyright (C) 2015 Red Hat, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
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 NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include <drm/drm_file.h>
27 #include <drm/drm_fourcc.h>
29 #include "virtgpu_drv.h"
31 void virtio_gpu_gem_free_object(struct drm_gem_object *gem_obj)
33 struct virtio_gpu_object *obj = gem_to_virtio_gpu_obj(gem_obj);
36 virtio_gpu_object_unref(&obj);
39 struct virtio_gpu_object*
40 virtio_gpu_alloc_object(struct drm_device *dev,
41 struct virtio_gpu_object_params *params,
42 struct virtio_gpu_fence *fence)
44 struct virtio_gpu_device *vgdev = dev->dev_private;
45 struct virtio_gpu_object *obj;
48 ret = virtio_gpu_object_create(vgdev, params, &obj, fence);
55 int virtio_gpu_gem_create(struct drm_file *file,
56 struct drm_device *dev,
57 struct virtio_gpu_object_params *params,
58 struct drm_gem_object **obj_p,
61 struct virtio_gpu_object *obj;
65 obj = virtio_gpu_alloc_object(dev, params, NULL);
69 ret = drm_gem_handle_create(file, &obj->gem_base, &handle);
71 drm_gem_object_release(&obj->gem_base);
75 *obj_p = &obj->gem_base;
77 /* drop reference from allocate - handle holds it now */
78 drm_gem_object_put_unlocked(&obj->gem_base);
84 int virtio_gpu_mode_dumb_create(struct drm_file *file_priv,
85 struct drm_device *dev,
86 struct drm_mode_create_dumb *args)
88 struct drm_gem_object *gobj;
89 struct virtio_gpu_object_params params = { 0 };
96 pitch = args->width * 4;
97 args->size = pitch * args->height;
98 args->size = ALIGN(args->size, PAGE_SIZE);
100 params.format = virtio_gpu_translate_format(DRM_FORMAT_HOST_XRGB8888);
101 params.width = args->width;
102 params.height = args->height;
103 params.size = args->size;
105 ret = virtio_gpu_gem_create(file_priv, dev, ¶ms, &gobj,
117 int virtio_gpu_mode_dumb_mmap(struct drm_file *file_priv,
118 struct drm_device *dev,
119 uint32_t handle, uint64_t *offset_p)
121 struct drm_gem_object *gobj;
122 struct virtio_gpu_object *obj;
125 gobj = drm_gem_object_lookup(file_priv, handle);
128 obj = gem_to_virtio_gpu_obj(gobj);
129 *offset_p = virtio_gpu_object_mmap_offset(obj);
130 drm_gem_object_put_unlocked(gobj);
134 int virtio_gpu_gem_object_open(struct drm_gem_object *obj,
135 struct drm_file *file)
137 struct virtio_gpu_device *vgdev = obj->dev->dev_private;
138 struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
139 struct virtio_gpu_object *qobj = gem_to_virtio_gpu_obj(obj);
142 if (!vgdev->has_virgl_3d)
145 r = virtio_gpu_object_reserve(qobj, false);
149 virtio_gpu_cmd_context_attach_resource(vgdev, vfpriv->ctx_id,
150 qobj->hw_res_handle);
151 virtio_gpu_object_unreserve(qobj);
155 void virtio_gpu_gem_object_close(struct drm_gem_object *obj,
156 struct drm_file *file)
158 struct virtio_gpu_device *vgdev = obj->dev->dev_private;
159 struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
160 struct virtio_gpu_object *qobj = gem_to_virtio_gpu_obj(obj);
163 if (!vgdev->has_virgl_3d)
166 r = virtio_gpu_object_reserve(qobj, false);
170 virtio_gpu_cmd_context_detach_resource(vgdev, vfpriv->ctx_id,
171 qobj->hw_res_handle);
172 virtio_gpu_object_unreserve(qobj);