xref: /openbmc/qemu/hw/display/virtio-gpu-gl.c (revision 7b72c7dd)
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 "sysemu/sysemu.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 
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 
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 
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 
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 
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 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     DEFINE_PROP_END_OF_LIST(),
163 };
164 
165 static void virtio_gpu_gl_device_unrealize(DeviceState *qdev)
166 {
167     VirtIOGPU *g = VIRTIO_GPU(qdev);
168     VirtIOGPUGL *gl = VIRTIO_GPU_GL(qdev);
169 
170     if (gl->renderer_state >= RS_INITED) {
171 #if VIRGL_VERSION_MAJOR >= 1
172         qemu_bh_delete(gl->cmdq_resume_bh);
173 #endif
174         if (virtio_gpu_stats_enabled(g->parent_obj.conf)) {
175             timer_free(gl->print_stats);
176         }
177         timer_free(gl->fence_poll);
178         virgl_renderer_cleanup(NULL);
179     }
180 
181     gl->renderer_state = RS_START;
182 
183     g_array_unref(g->capset_ids);
184 }
185 
186 static void virtio_gpu_gl_class_init(ObjectClass *klass, void *data)
187 {
188     DeviceClass *dc = DEVICE_CLASS(klass);
189     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
190     VirtIOGPUBaseClass *vbc = VIRTIO_GPU_BASE_CLASS(klass);
191     VirtIOGPUClass *vgc = VIRTIO_GPU_CLASS(klass);
192 
193     vbc->gl_flushed = virtio_gpu_gl_flushed;
194     vgc->handle_ctrl = virtio_gpu_gl_handle_ctrl;
195     vgc->process_cmd = virtio_gpu_virgl_process_cmd;
196     vgc->update_cursor_data = virtio_gpu_gl_update_cursor_data;
197 
198     vdc->realize = virtio_gpu_gl_device_realize;
199     vdc->unrealize = virtio_gpu_gl_device_unrealize;
200     vdc->reset = virtio_gpu_gl_reset;
201     device_class_set_props(dc, virtio_gpu_gl_properties);
202 }
203 
204 static const TypeInfo virtio_gpu_gl_info = {
205     .name = TYPE_VIRTIO_GPU_GL,
206     .parent = TYPE_VIRTIO_GPU,
207     .instance_size = sizeof(VirtIOGPUGL),
208     .class_init = virtio_gpu_gl_class_init,
209 };
210 module_obj(TYPE_VIRTIO_GPU_GL);
211 module_kconfig(VIRTIO_GPU);
212 
213 static void virtio_register_types(void)
214 {
215     type_register_static(&virtio_gpu_gl_info);
216 }
217 
218 type_init(virtio_register_types)
219 
220 module_dep("hw-display-virtio-gpu");
221 module_dep("ui-opengl");
222