1 /*
2 * Virtio GPU Device
3 *
4 * Copyright Red Hat, Inc. 2013-2014
5 *
6 * Authors:
7 * Dave Airlie <airlied@redhat.com>
8 * Gerd Hoffmann <kraxel@redhat.com>
9 *
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
12 */
13
14 #include "qemu/osdep.h"
15 #include "qemu/iov.h"
16 #include "qemu/module.h"
17 #include "qemu/error-report.h"
18 #include "qapi/error.h"
19 #include "system/system.h"
20 #include "hw/virtio/virtio.h"
21 #include "hw/virtio/virtio-gpu.h"
22 #include "hw/virtio/virtio-gpu-bswap.h"
23 #include "hw/virtio/virtio-gpu-pixman.h"
24 #include "hw/qdev-properties.h"
25
26 #include <virglrenderer.h>
27
virtio_gpu_gl_update_cursor_data(VirtIOGPU * g,struct virtio_gpu_scanout * s,uint32_t resource_id)28 static void virtio_gpu_gl_update_cursor_data(VirtIOGPU *g,
29 struct virtio_gpu_scanout *s,
30 uint32_t resource_id)
31 {
32 VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
33 uint32_t width, height;
34 uint32_t pixels, *data;
35
36 if (gl->renderer_state != RS_INITED) {
37 return;
38 }
39
40 data = virgl_renderer_get_cursor_data(resource_id, &width, &height);
41 if (!data) {
42 return;
43 }
44
45 if (width != s->current_cursor->width ||
46 height != s->current_cursor->height) {
47 free(data);
48 return;
49 }
50
51 pixels = s->current_cursor->width * s->current_cursor->height;
52 memcpy(s->current_cursor->data, data, pixels * sizeof(uint32_t));
53 free(data);
54 }
55
virtio_gpu_gl_flushed(VirtIOGPUBase * b)56 static void virtio_gpu_gl_flushed(VirtIOGPUBase *b)
57 {
58 VirtIOGPU *g = VIRTIO_GPU(b);
59
60 virtio_gpu_process_cmdq(g);
61 }
62
virtio_gpu_gl_handle_ctrl(VirtIODevice * vdev,VirtQueue * vq)63 static void virtio_gpu_gl_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
64 {
65 VirtIOGPU *g = VIRTIO_GPU(vdev);
66 VirtIOGPUGL *gl = VIRTIO_GPU_GL(vdev);
67 struct virtio_gpu_ctrl_command *cmd;
68
69 if (!virtio_queue_ready(vq)) {
70 return;
71 }
72
73 switch (gl->renderer_state) {
74 case RS_RESET:
75 virtio_gpu_virgl_reset(g);
76 /* fallthrough */
77 case RS_START:
78 if (virtio_gpu_virgl_init(g)) {
79 gl->renderer_state = RS_INIT_FAILED;
80 return;
81 }
82
83 gl->renderer_state = RS_INITED;
84 break;
85 case RS_INIT_FAILED:
86 return;
87 case RS_INITED:
88 break;
89 }
90
91 cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command));
92 while (cmd) {
93 cmd->vq = vq;
94 cmd->error = 0;
95 cmd->finished = false;
96 QTAILQ_INSERT_TAIL(&g->cmdq, cmd, next);
97 cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command));
98 }
99
100 virtio_gpu_process_cmdq(g);
101 virtio_gpu_virgl_fence_poll(g);
102 }
103
virtio_gpu_gl_reset(VirtIODevice * vdev)104 static void virtio_gpu_gl_reset(VirtIODevice *vdev)
105 {
106 VirtIOGPU *g = VIRTIO_GPU(vdev);
107 VirtIOGPUGL *gl = VIRTIO_GPU_GL(vdev);
108
109 virtio_gpu_reset(vdev);
110
111 /*
112 * GL functions must be called with the associated GL context in main
113 * thread, and when the renderer is unblocked.
114 */
115 if (gl->renderer_state == RS_INITED) {
116 virtio_gpu_virgl_reset_scanout(g);
117 gl->renderer_state = RS_RESET;
118 }
119 }
120
virtio_gpu_gl_device_realize(DeviceState * qdev,Error ** errp)121 static void virtio_gpu_gl_device_realize(DeviceState *qdev, Error **errp)
122 {
123 ERRP_GUARD();
124 VirtIOGPU *g = VIRTIO_GPU(qdev);
125
126 #if HOST_BIG_ENDIAN
127 error_setg(errp, "virgl is not supported on bigendian platforms");
128 return;
129 #endif
130
131 if (!object_resolve_path_type("", TYPE_VIRTIO_GPU_GL, NULL)) {
132 error_setg(errp, "at most one %s device is permitted", TYPE_VIRTIO_GPU_GL);
133 return;
134 }
135
136 if (!display_opengl) {
137 error_setg(errp,
138 "The display backend does not have OpenGL support enabled");
139 error_append_hint(errp,
140 "It can be enabled with '-display BACKEND,gl=on' "
141 "where BACKEND is the name of the display backend "
142 "to use.\n");
143 return;
144 }
145
146 g->parent_obj.conf.flags |= (1 << VIRTIO_GPU_FLAG_VIRGL_ENABLED);
147 g->capset_ids = virtio_gpu_virgl_get_capsets(g);
148 VIRTIO_GPU_BASE(g)->virtio_config.num_capsets = g->capset_ids->len;
149
150 #if VIRGL_VERSION_MAJOR >= 1
151 g->parent_obj.conf.flags |= 1 << VIRTIO_GPU_FLAG_CONTEXT_INIT_ENABLED;
152 #endif
153
154 virtio_gpu_device_realize(qdev, errp);
155 }
156
157 static const Property virtio_gpu_gl_properties[] = {
158 DEFINE_PROP_BIT("stats", VirtIOGPU, parent_obj.conf.flags,
159 VIRTIO_GPU_FLAG_STATS_ENABLED, false),
160 DEFINE_PROP_BIT("venus", VirtIOGPU, parent_obj.conf.flags,
161 VIRTIO_GPU_FLAG_VENUS_ENABLED, false),
162 };
163
virtio_gpu_gl_device_unrealize(DeviceState * qdev)164 static void virtio_gpu_gl_device_unrealize(DeviceState *qdev)
165 {
166 VirtIOGPU *g = VIRTIO_GPU(qdev);
167 VirtIOGPUGL *gl = VIRTIO_GPU_GL(qdev);
168
169 if (gl->renderer_state >= RS_INITED) {
170 #if VIRGL_VERSION_MAJOR >= 1
171 qemu_bh_delete(gl->cmdq_resume_bh);
172 #endif
173 if (virtio_gpu_stats_enabled(g->parent_obj.conf)) {
174 timer_free(gl->print_stats);
175 }
176 timer_free(gl->fence_poll);
177 virgl_renderer_cleanup(NULL);
178 }
179
180 gl->renderer_state = RS_START;
181
182 g_array_unref(g->capset_ids);
183 }
184
virtio_gpu_gl_class_init(ObjectClass * klass,const void * data)185 static void virtio_gpu_gl_class_init(ObjectClass *klass, const void *data)
186 {
187 DeviceClass *dc = DEVICE_CLASS(klass);
188 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
189 VirtIOGPUBaseClass *vbc = VIRTIO_GPU_BASE_CLASS(klass);
190 VirtIOGPUClass *vgc = VIRTIO_GPU_CLASS(klass);
191
192 vbc->gl_flushed = virtio_gpu_gl_flushed;
193 vgc->handle_ctrl = virtio_gpu_gl_handle_ctrl;
194 vgc->process_cmd = virtio_gpu_virgl_process_cmd;
195 vgc->update_cursor_data = virtio_gpu_gl_update_cursor_data;
196
197 vdc->realize = virtio_gpu_gl_device_realize;
198 vdc->unrealize = virtio_gpu_gl_device_unrealize;
199 vdc->reset = virtio_gpu_gl_reset;
200 device_class_set_props(dc, virtio_gpu_gl_properties);
201 }
202
203 static const TypeInfo virtio_gpu_gl_info = {
204 .name = TYPE_VIRTIO_GPU_GL,
205 .parent = TYPE_VIRTIO_GPU,
206 .instance_size = sizeof(VirtIOGPUGL),
207 .class_init = virtio_gpu_gl_class_init,
208 };
209 module_obj(TYPE_VIRTIO_GPU_GL);
210 module_kconfig(VIRTIO_GPU);
211
virtio_register_types(void)212 static void virtio_register_types(void)
213 {
214 type_register_static(&virtio_gpu_gl_info);
215 }
216
217 type_init(virtio_register_types)
218
219 module_dep("hw-display-virtio-gpu");
220 module_dep("ui-opengl");
221