Loading...
1/*
2 * Copyright (C) 2015 Red Hat, Inc.
3 * All Rights Reserved.
4 *
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:
12 *
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.
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 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.
24 */
25
26#include <drm/drmP.h>
27#include "virtgpu_drv.h"
28
29void virtio_gpu_gem_free_object(struct drm_gem_object *gem_obj)
30{
31 struct virtio_gpu_object *obj = gem_to_virtio_gpu_obj(gem_obj);
32
33 if (obj)
34 virtio_gpu_object_unref(&obj);
35}
36
37struct virtio_gpu_object *virtio_gpu_alloc_object(struct drm_device *dev,
38 size_t size, bool kernel,
39 bool pinned)
40{
41 struct virtio_gpu_device *vgdev = dev->dev_private;
42 struct virtio_gpu_object *obj;
43 int ret;
44
45 ret = virtio_gpu_object_create(vgdev, size, kernel, pinned, &obj);
46 if (ret)
47 return ERR_PTR(ret);
48
49 return obj;
50}
51
52int virtio_gpu_gem_create(struct drm_file *file,
53 struct drm_device *dev,
54 uint64_t size,
55 struct drm_gem_object **obj_p,
56 uint32_t *handle_p)
57{
58 struct virtio_gpu_object *obj;
59 int ret;
60 u32 handle;
61
62 obj = virtio_gpu_alloc_object(dev, size, false, false);
63 if (IS_ERR(obj))
64 return PTR_ERR(obj);
65
66 ret = drm_gem_handle_create(file, &obj->gem_base, &handle);
67 if (ret) {
68 drm_gem_object_release(&obj->gem_base);
69 return ret;
70 }
71
72 *obj_p = &obj->gem_base;
73
74 /* drop reference from allocate - handle holds it now */
75 drm_gem_object_unreference_unlocked(&obj->gem_base);
76
77 *handle_p = handle;
78 return 0;
79}
80
81int virtio_gpu_mode_dumb_create(struct drm_file *file_priv,
82 struct drm_device *dev,
83 struct drm_mode_create_dumb *args)
84{
85 struct virtio_gpu_device *vgdev = dev->dev_private;
86 struct drm_gem_object *gobj;
87 struct virtio_gpu_object *obj;
88 int ret;
89 uint32_t pitch;
90 uint32_t resid;
91
92 pitch = args->width * ((args->bpp + 1) / 8);
93 args->size = pitch * args->height;
94 args->size = ALIGN(args->size, PAGE_SIZE);
95
96 ret = virtio_gpu_gem_create(file_priv, dev, args->size, &gobj,
97 &args->handle);
98 if (ret)
99 goto fail;
100
101 virtio_gpu_resource_id_get(vgdev, &resid);
102 virtio_gpu_cmd_create_resource(vgdev, resid,
103 2, args->width, args->height);
104
105 /* attach the object to the resource */
106 obj = gem_to_virtio_gpu_obj(gobj);
107 ret = virtio_gpu_object_attach(vgdev, obj, resid, NULL);
108 if (ret)
109 goto fail;
110
111 obj->dumb = true;
112 args->pitch = pitch;
113 return ret;
114
115fail:
116 return ret;
117}
118
119int virtio_gpu_mode_dumb_destroy(struct drm_file *file_priv,
120 struct drm_device *dev,
121 uint32_t handle)
122{
123 return drm_gem_handle_delete(file_priv, handle);
124}
125
126int virtio_gpu_mode_dumb_mmap(struct drm_file *file_priv,
127 struct drm_device *dev,
128 uint32_t handle, uint64_t *offset_p)
129{
130 struct drm_gem_object *gobj;
131 struct virtio_gpu_object *obj;
132 BUG_ON(!offset_p);
133 gobj = drm_gem_object_lookup(dev, file_priv, handle);
134 if (gobj == NULL)
135 return -ENOENT;
136 obj = gem_to_virtio_gpu_obj(gobj);
137 *offset_p = virtio_gpu_object_mmap_offset(obj);
138 drm_gem_object_unreference_unlocked(gobj);
139 return 0;
140}
141
142int virtio_gpu_gem_object_open(struct drm_gem_object *obj,
143 struct drm_file *file)
144{
145 struct virtio_gpu_device *vgdev = obj->dev->dev_private;
146 struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
147 struct virtio_gpu_object *qobj = gem_to_virtio_gpu_obj(obj);
148 int r;
149
150 if (!vgdev->has_virgl_3d)
151 return 0;
152
153 r = virtio_gpu_object_reserve(qobj, false);
154 if (r)
155 return r;
156
157 virtio_gpu_cmd_context_attach_resource(vgdev, vfpriv->ctx_id,
158 qobj->hw_res_handle);
159 virtio_gpu_object_unreserve(qobj);
160 return 0;
161}
162
163void virtio_gpu_gem_object_close(struct drm_gem_object *obj,
164 struct drm_file *file)
165{
166 struct virtio_gpu_device *vgdev = obj->dev->dev_private;
167 struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
168 struct virtio_gpu_object *qobj = gem_to_virtio_gpu_obj(obj);
169 int r;
170
171 if (!vgdev->has_virgl_3d)
172 return;
173
174 r = virtio_gpu_object_reserve(qobj, false);
175 if (r)
176 return;
177
178 virtio_gpu_cmd_context_detach_resource(vgdev, vfpriv->ctx_id,
179 qobj->hw_res_handle);
180 virtio_gpu_object_unreserve(qobj);
181}
1/*
2 * Copyright (C) 2015 Red Hat, Inc.
3 * All Rights Reserved.
4 *
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:
12 *
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.
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 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.
24 */
25
26#include <drm/drm_file.h>
27#include <drm/drm_fourcc.h>
28
29#include "virtgpu_drv.h"
30
31void virtio_gpu_gem_free_object(struct drm_gem_object *gem_obj)
32{
33 struct virtio_gpu_object *obj = gem_to_virtio_gpu_obj(gem_obj);
34
35 if (obj)
36 virtio_gpu_object_unref(&obj);
37}
38
39struct virtio_gpu_object*
40virtio_gpu_alloc_object(struct drm_device *dev,
41 struct virtio_gpu_object_params *params,
42 struct virtio_gpu_fence *fence)
43{
44 struct virtio_gpu_device *vgdev = dev->dev_private;
45 struct virtio_gpu_object *obj;
46 int ret;
47
48 ret = virtio_gpu_object_create(vgdev, params, &obj, fence);
49 if (ret)
50 return ERR_PTR(ret);
51
52 return obj;
53}
54
55int 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,
59 uint32_t *handle_p)
60{
61 struct virtio_gpu_object *obj;
62 int ret;
63 u32 handle;
64
65 obj = virtio_gpu_alloc_object(dev, params, NULL);
66 if (IS_ERR(obj))
67 return PTR_ERR(obj);
68
69 ret = drm_gem_handle_create(file, &obj->gem_base, &handle);
70 if (ret) {
71 drm_gem_object_release(&obj->gem_base);
72 return ret;
73 }
74
75 *obj_p = &obj->gem_base;
76
77 /* drop reference from allocate - handle holds it now */
78 drm_gem_object_put_unlocked(&obj->gem_base);
79
80 *handle_p = handle;
81 return 0;
82}
83
84int virtio_gpu_mode_dumb_create(struct drm_file *file_priv,
85 struct drm_device *dev,
86 struct drm_mode_create_dumb *args)
87{
88 struct drm_gem_object *gobj;
89 struct virtio_gpu_object_params params = { 0 };
90 int ret;
91 uint32_t pitch;
92
93 if (args->bpp != 32)
94 return -EINVAL;
95
96 pitch = args->width * 4;
97 args->size = pitch * args->height;
98 args->size = ALIGN(args->size, PAGE_SIZE);
99
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;
104 params.dumb = true;
105 ret = virtio_gpu_gem_create(file_priv, dev, ¶ms, &gobj,
106 &args->handle);
107 if (ret)
108 goto fail;
109
110 args->pitch = pitch;
111 return ret;
112
113fail:
114 return ret;
115}
116
117int virtio_gpu_mode_dumb_mmap(struct drm_file *file_priv,
118 struct drm_device *dev,
119 uint32_t handle, uint64_t *offset_p)
120{
121 struct drm_gem_object *gobj;
122 struct virtio_gpu_object *obj;
123
124 BUG_ON(!offset_p);
125 gobj = drm_gem_object_lookup(file_priv, handle);
126 if (gobj == NULL)
127 return -ENOENT;
128 obj = gem_to_virtio_gpu_obj(gobj);
129 *offset_p = virtio_gpu_object_mmap_offset(obj);
130 drm_gem_object_put_unlocked(gobj);
131 return 0;
132}
133
134int virtio_gpu_gem_object_open(struct drm_gem_object *obj,
135 struct drm_file *file)
136{
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);
140 int r;
141
142 if (!vgdev->has_virgl_3d)
143 return 0;
144
145 r = virtio_gpu_object_reserve(qobj, false);
146 if (r)
147 return r;
148
149 virtio_gpu_cmd_context_attach_resource(vgdev, vfpriv->ctx_id,
150 qobj->hw_res_handle);
151 virtio_gpu_object_unreserve(qobj);
152 return 0;
153}
154
155void virtio_gpu_gem_object_close(struct drm_gem_object *obj,
156 struct drm_file *file)
157{
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);
161 int r;
162
163 if (!vgdev->has_virgl_3d)
164 return;
165
166 r = virtio_gpu_object_reserve(qobj, false);
167 if (r)
168 return;
169
170 virtio_gpu_cmd_context_detach_resource(vgdev, vfpriv->ctx_id,
171 qobj->hw_res_handle);
172 virtio_gpu_object_unreserve(qobj);
173}