| 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/mmap-alloc.h" |
| 17 | #include "qemu/module.h" |
| 18 | #include "qemu/error-report.h" |
| 19 | #include "qapi/error.h" |
| 20 | #include "system/system.h" |
| 21 | #include "hw/virtio/virtio.h" |
| 22 | #include "hw/virtio/virtio-gpu.h" |
| 23 | #include "hw/virtio/virtio-gpu-bswap.h" |
| 24 | #include "hw/virtio/virtio-gpu-pixman.h" |
| 25 | #include "hw/core/qdev-properties.h" |
| 26 | |
| 27 | #include <virglrenderer.h> |
| 28 | |
| 29 | static void virtio_gpu_gl_update_cursor_data(VirtIOGPU *g, |
| 30 | struct virtio_gpu_scanout *s, |
| 31 | uint32_t resource_id) |
| 32 | { |
| 33 | VirtIOGPUGL *gl = VIRTIO_GPU_GL(g); |
| 34 | uint32_t width, height; |
| 35 | uint32_t pixels, *data; |
| 36 | |
| 37 | if (gl->renderer_state != RS_INITED) { |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | data = virgl_renderer_get_cursor_data(resource_id, &width, &height); |
| 42 | if (!data) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | if (width != s->current_cursor->width || |
| 47 | height != s->current_cursor->height) { |
| 48 | free(data); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | pixels = s->current_cursor->width * s->current_cursor->height; |
| 53 | memcpy(s->current_cursor->data, data, pixels * sizeof(uint32_t)); |
| 54 | free(data); |
| 55 | } |
| 56 | |
| 57 | static void virtio_gpu_gl_flushed(VirtIOGPUBase *b) |
| 58 | { |
| 59 | VirtIOGPU *g = VIRTIO_GPU(b); |
| 60 | |
| 61 | virtio_gpu_process_cmdq(g); |
| 62 | } |
| 63 | |
| 64 | static void virtio_gpu_gl_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) |
| 65 | { |
| 66 | VirtIOGPU *g = VIRTIO_GPU(vdev); |
| 67 | struct virtio_gpu_ctrl_command *cmd; |
| 68 | |
| 69 | if (!virtio_queue_ready(vq)) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | if (!virtio_gpu_virgl_update_render_state(g)) { |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command)); |
| 78 | while (cmd) { |
| 79 | cmd->vq = vq; |
| 80 | cmd->error = 0; |
| 81 | cmd->finished = false; |
| 82 | QTAILQ_INSERT_TAIL(&g->cmdq, cmd, next); |
| 83 | cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command)); |
| 84 | } |
| 85 | |
| 86 | virtio_gpu_process_cmdq(g); |
| 87 | virtio_gpu_virgl_fence_poll(g); |
| 88 | } |
| 89 | |
| 90 | static void virtio_gpu_gl_reset(VirtIODevice *vdev) |
| 91 | { |
| 92 | VirtIOGPU *g = VIRTIO_GPU(vdev); |
| 93 | VirtIOGPUGL *gl = VIRTIO_GPU_GL(vdev); |
| 94 | |
| 95 | virtio_gpu_reset(vdev); |
| 96 | |
| 97 | /* |
| 98 | * GL functions must be called with the associated GL context in main |
| 99 | * thread, and when the renderer is unblocked. |
| 100 | */ |
| 101 | if (gl->renderer_state == RS_INITED) { |
| 102 | virtio_gpu_virgl_reset_scanout(g); |
| 103 | gl->renderer_state = RS_RESET; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | static void virtio_gpu_gl_device_realize(DeviceState *qdev, Error **errp) |
| 108 | { |
| 109 | ERRP_GUARD(); |
| 110 | VirtIOGPUBase *b = VIRTIO_GPU_BASE(qdev); |
| 111 | VirtIOGPU *g = VIRTIO_GPU(b); |
| 112 | #if !defined(CONFIG_WIN32) |
| 113 | VirtIOGPUGL *gl = VIRTIO_GPU_GL(g); |
| 114 | void *map; |
| 115 | #endif |
| 116 | |
| 117 | #if HOST_BIG_ENDIAN |
| 118 | error_setg(errp, "virgl is not supported on bigendian platforms"); |
| 119 | return; |
| 120 | #endif |
| 121 | |
| 122 | if (!object_resolve_path_type("", TYPE_VIRTIO_GPU_GL, NULL)) { |
| 123 | error_setg(errp, "at most one %s device is permitted", TYPE_VIRTIO_GPU_GL); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | if (!display_opengl) { |
| 128 | error_setg(errp, |
| 129 | "The display backend does not have OpenGL support enabled"); |
| 130 | error_append_hint(errp, |
| 131 | "It can be enabled with '-display BACKEND,gl=on' " |
| 132 | "where BACKEND is the name of the display backend " |
| 133 | "to use.\n"); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | g->parent_obj.conf.flags |= (1 << VIRTIO_GPU_FLAG_VIRGL_ENABLED); |
| 138 | g->capset_ids = virtio_gpu_virgl_get_capsets(g); |
| 139 | VIRTIO_GPU_BASE(g)->virtio_config.num_capsets = g->capset_ids->len; |
| 140 | |
| 141 | #if VIRGL_VERSION_MAJOR >= 1 |
| 142 | g->parent_obj.conf.flags |= 1 << VIRTIO_GPU_FLAG_CONTEXT_INIT_ENABLED; |
| 143 | #endif |
| 144 | |
| 145 | #if !defined(CONFIG_WIN32) |
| 146 | if (virtio_gpu_hostmem_enabled(b->conf)) { |
| 147 | map = qemu_ram_mmap(-1, b->conf.hostmem, qemu_real_host_page_size(), |
| 148 | 0, 0); |
| 149 | if (map == MAP_FAILED) { |
| 150 | error_setg_errno(errp, errno, |
| 151 | "virgl hostmem region could not be initialized"); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | gl->hostmem_mmap = map; |
| 156 | memory_region_init_ram_ptr(&gl->hostmem_background, NULL, |
| 157 | "hostmem-background", b->conf.hostmem, |
| 158 | gl->hostmem_mmap); |
| 159 | memory_region_add_subregion(&b->hostmem, 0, &gl->hostmem_background); |
| 160 | } |
| 161 | #endif |
| 162 | |
| 163 | virtio_gpu_device_realize(qdev, errp); |
| 164 | } |
| 165 | |
| 166 | static const Property virtio_gpu_gl_properties[] = { |
| 167 | DEFINE_PROP_BIT("stats", VirtIOGPU, parent_obj.conf.flags, |
| 168 | VIRTIO_GPU_FLAG_STATS_ENABLED, false), |
| 169 | DEFINE_PROP_BIT("venus", VirtIOGPU, parent_obj.conf.flags, |
| 170 | VIRTIO_GPU_FLAG_VENUS_ENABLED, false), |
| 171 | DEFINE_PROP_BIT("drm_native_context", VirtIOGPU, parent_obj.conf.flags, |
| 172 | VIRTIO_GPU_FLAG_DRM_ENABLED, false), |
| 173 | }; |
| 174 | |
| 175 | static void virtio_gpu_gl_device_unrealize(DeviceState *qdev) |
| 176 | { |
| 177 | VirtIOGPU *g = VIRTIO_GPU(qdev); |
| 178 | VirtIOGPUGL *gl = VIRTIO_GPU_GL(qdev); |
| 179 | |
| 180 | if (gl->renderer_state >= RS_INITED) { |
| 181 | #if VIRGL_VERSION_MAJOR >= 1 |
| 182 | qemu_bh_delete(gl->cmdq_resume_bh); |
| 183 | |
| 184 | if (gl->async_fence_bh) { |
| 185 | virtio_gpu_virgl_reset_async_fences(g); |
| 186 | qemu_bh_delete(gl->async_fence_bh); |
| 187 | } |
| 188 | #endif |
| 189 | if (virtio_gpu_stats_enabled(g->parent_obj.conf)) { |
| 190 | timer_free(gl->print_stats); |
| 191 | } |
| 192 | timer_free(gl->fence_poll); |
| 193 | virgl_renderer_cleanup(NULL); |
| 194 | } |
| 195 | |
| 196 | gl->renderer_state = RS_START; |
| 197 | |
| 198 | g_array_unref(g->capset_ids); |
| 199 | |
| 200 | /* |
| 201 | * It is not guaranteed that the memory region will be finalized |
| 202 | * immediately with memory_region_del_subregion(), there can be |
| 203 | * a remaining reference to gl->hostmem_mmap. VirtIO-GPU is not |
| 204 | * hotpluggable, hence no need to worry about the leaked mapping. |
| 205 | * |
| 206 | * The memory_region_del_subregion(gl->hostmem_background) is unnecessary |
| 207 | * because b->hostmem and gl->hostmem_background belong to the same |
| 208 | * device and will be gone at the same time. |
| 209 | */ |
| 210 | } |
| 211 | |
| 212 | static void virtio_gpu_gl_class_init(ObjectClass *klass, const void *data) |
| 213 | { |
| 214 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 215 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); |
| 216 | VirtIOGPUBaseClass *vbc = VIRTIO_GPU_BASE_CLASS(klass); |
| 217 | VirtIOGPUClass *vgc = VIRTIO_GPU_CLASS(klass); |
| 218 | |
| 219 | vbc->gl_flushed = virtio_gpu_gl_flushed; |
| 220 | vgc->handle_ctrl = virtio_gpu_gl_handle_ctrl; |
| 221 | vgc->process_cmd = virtio_gpu_virgl_process_cmd; |
| 222 | vgc->update_cursor_data = virtio_gpu_gl_update_cursor_data; |
| 223 | |
| 224 | vgc->resource_destroy = virtio_gpu_virgl_resource_destroy; |
| 225 | vdc->realize = virtio_gpu_gl_device_realize; |
| 226 | vdc->unrealize = virtio_gpu_gl_device_unrealize; |
| 227 | vdc->reset = virtio_gpu_gl_reset; |
| 228 | device_class_set_props(dc, virtio_gpu_gl_properties); |
| 229 | } |
| 230 | |
| 231 | static const TypeInfo virtio_gpu_gl_info = { |
| 232 | .name = TYPE_VIRTIO_GPU_GL, |
| 233 | .parent = TYPE_VIRTIO_GPU, |
| 234 | .instance_size = sizeof(VirtIOGPUGL), |
| 235 | .class_init = virtio_gpu_gl_class_init, |
| 236 | }; |
| 237 | module_obj(TYPE_VIRTIO_GPU_GL); |
| 238 | module_kconfig(VIRTIO_GPU); |
| 239 | |
| 240 | static void virtio_register_types(void) |
| 241 | { |
| 242 | type_register_static(&virtio_gpu_gl_info); |
| 243 | } |
| 244 | |
| 245 | type_init(virtio_register_types) |
| 246 | |
| 247 | module_dep("hw-display-virtio-gpu"); |
| 248 | module_dep("ui-opengl"); |