| 
									
										
										
										
											2021-04-30 13:35:33 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Virtio GPU Device | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright Red Hat, Inc. 2013-2014 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Authors: | 
					
						
							|  |  |  |  *     Dave Airlie <airlied@redhat.com> | 
					
						
							|  |  |  |  *     Gerd Hoffmann <kraxel@redhat.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This work is licensed under the terms of the GNU GPL, version 2 or later. | 
					
						
							|  |  |  |  * See the COPYING file in the top-level directory. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "qemu/osdep.h"
 | 
					
						
							|  |  |  | #include "qemu/iov.h"
 | 
					
						
							|  |  |  | #include "qemu/module.h"
 | 
					
						
							|  |  |  | #include "qemu/error-report.h"
 | 
					
						
							|  |  |  | #include "qapi/error.h"
 | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:34 +02:00
										 |  |  | #include "sysemu/sysemu.h"
 | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:33 +02:00
										 |  |  | #include "hw/virtio/virtio.h"
 | 
					
						
							|  |  |  | #include "hw/virtio/virtio-gpu.h"
 | 
					
						
							|  |  |  | #include "hw/virtio/virtio-gpu-bswap.h"
 | 
					
						
							|  |  |  | #include "hw/virtio/virtio-gpu-pixman.h"
 | 
					
						
							|  |  |  | #include "hw/qdev-properties.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:40 +02:00
										 |  |  | #include <virglrenderer.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void virtio_gpu_gl_update_cursor_data(VirtIOGPU *g, | 
					
						
							|  |  |  |                                              struct virtio_gpu_scanout *s, | 
					
						
							|  |  |  |                                              uint32_t resource_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     uint32_t width, height; | 
					
						
							|  |  |  |     uint32_t pixels, *data; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:43 +02:00
										 |  |  |     data = virgl_renderer_get_cursor_data(resource_id, &width, &height); | 
					
						
							|  |  |  |     if (!data) { | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:40 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:43 +02:00
										 |  |  |     if (width != s->current_cursor->width || | 
					
						
							|  |  |  |         height != s->current_cursor->height) { | 
					
						
							|  |  |  |         free(data); | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:39 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     pixels = s->current_cursor->width * s->current_cursor->height; | 
					
						
							|  |  |  |     memcpy(s->current_cursor->data, data, pixels * sizeof(uint32_t)); | 
					
						
							|  |  |  |     free(data); | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:39 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:38 +02:00
										 |  |  | static void virtio_gpu_gl_flushed(VirtIOGPUBase *b) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     VirtIOGPU *g = VIRTIO_GPU(b); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtio_gpu_process_cmdq(g); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:37 +02:00
										 |  |  | static void virtio_gpu_gl_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     VirtIOGPU *g = VIRTIO_GPU(vdev); | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:44 +02:00
										 |  |  |     VirtIOGPUGL *gl = VIRTIO_GPU_GL(vdev); | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:37 +02:00
										 |  |  |     struct virtio_gpu_ctrl_command *cmd; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!virtio_queue_ready(vq)) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:44 +02:00
										 |  |  |     if (!gl->renderer_inited) { | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:37 +02:00
										 |  |  |         virtio_gpu_virgl_init(g); | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:44 +02:00
										 |  |  |         gl->renderer_inited = true; | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:37 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-07-02 16:32:21 +04:00
										 |  |  |     if (gl->renderer_reset) { | 
					
						
							|  |  |  |         gl->renderer_reset = false; | 
					
						
							|  |  |  |         virtio_gpu_virgl_reset(g); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command)); | 
					
						
							|  |  |  |     while (cmd) { | 
					
						
							|  |  |  |         cmd->vq = vq; | 
					
						
							|  |  |  |         cmd->error = 0; | 
					
						
							|  |  |  |         cmd->finished = false; | 
					
						
							|  |  |  |         QTAILQ_INSERT_TAIL(&g->cmdq, cmd, next); | 
					
						
							|  |  |  |         cmd = virtqueue_pop(vq, sizeof(struct virtio_gpu_ctrl_command)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtio_gpu_process_cmdq(g); | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:43 +02:00
										 |  |  |     virtio_gpu_virgl_fence_poll(g); | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:37 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:35 +02:00
										 |  |  | static void virtio_gpu_gl_reset(VirtIODevice *vdev) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     VirtIOGPU *g = VIRTIO_GPU(vdev); | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:44 +02:00
										 |  |  |     VirtIOGPUGL *gl = VIRTIO_GPU_GL(vdev); | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     virtio_gpu_reset(vdev); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-02 16:32:21 +04:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * GL functions must be called with the associated GL context in main | 
					
						
							|  |  |  |      * thread, and when the renderer is unblocked. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     if (gl->renderer_inited && !gl->renderer_reset) { | 
					
						
							|  |  |  |         virtio_gpu_virgl_reset_scanout(g); | 
					
						
							|  |  |  |         gl->renderer_reset = true; | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:35 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:34 +02:00
										 |  |  | static void virtio_gpu_gl_device_realize(DeviceState *qdev, Error **errp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     VirtIOGPU *g = VIRTIO_GPU(qdev); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-23 19:57:17 +04:00
										 |  |  | #if HOST_BIG_ENDIAN
 | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:34 +02:00
										 |  |  |     error_setg(errp, "virgl is not supported on bigendian platforms"); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-05 14:42:18 +04:00
										 |  |  |     if (!object_resolve_path_type("", TYPE_VIRTIO_GPU_GL, NULL)) { | 
					
						
							|  |  |  |         error_setg(errp, "at most one %s device is permitted", TYPE_VIRTIO_GPU_GL); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:34 +02:00
										 |  |  |     if (!display_opengl) { | 
					
						
							|  |  |  |         error_setg(errp, "opengl is not available"); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     g->parent_obj.conf.flags |= (1 << VIRTIO_GPU_FLAG_VIRGL_ENABLED); | 
					
						
							|  |  |  |     VIRTIO_GPU_BASE(g)->virtio_config.num_capsets = | 
					
						
							|  |  |  |         virtio_gpu_virgl_get_num_capsets(g); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtio_gpu_device_realize(qdev, errp); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static Property virtio_gpu_gl_properties[] = { | 
					
						
							|  |  |  |     DEFINE_PROP_BIT("stats", VirtIOGPU, parent_obj.conf.flags, | 
					
						
							|  |  |  |                     VIRTIO_GPU_FLAG_STATS_ENABLED, false), | 
					
						
							|  |  |  |     DEFINE_PROP_END_OF_LIST(), | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:33 +02:00
										 |  |  | static void virtio_gpu_gl_class_init(ObjectClass *klass, void *data) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:34 +02:00
										 |  |  |     DeviceClass *dc = DEVICE_CLASS(klass); | 
					
						
							|  |  |  |     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:38 +02:00
										 |  |  |     VirtIOGPUBaseClass *vbc = VIRTIO_GPU_BASE_CLASS(klass); | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:37 +02:00
										 |  |  |     VirtIOGPUClass *vgc = VIRTIO_GPU_CLASS(klass); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:38 +02:00
										 |  |  |     vbc->gl_flushed = virtio_gpu_gl_flushed; | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:37 +02:00
										 |  |  |     vgc->handle_ctrl = virtio_gpu_gl_handle_ctrl; | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:43 +02:00
										 |  |  |     vgc->process_cmd = virtio_gpu_virgl_process_cmd; | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:40 +02:00
										 |  |  |     vgc->update_cursor_data = virtio_gpu_gl_update_cursor_data; | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     vdc->realize = virtio_gpu_gl_device_realize; | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:35 +02:00
										 |  |  |     vdc->reset = virtio_gpu_gl_reset; | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:34 +02:00
										 |  |  |     device_class_set_props(dc, virtio_gpu_gl_properties); | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:33 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const TypeInfo virtio_gpu_gl_info = { | 
					
						
							|  |  |  |     .name = TYPE_VIRTIO_GPU_GL, | 
					
						
							|  |  |  |     .parent = TYPE_VIRTIO_GPU, | 
					
						
							|  |  |  |     .instance_size = sizeof(VirtIOGPUGL), | 
					
						
							|  |  |  |     .class_init = virtio_gpu_gl_class_init, | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2021-06-24 12:38:08 +02:00
										 |  |  | module_obj(TYPE_VIRTIO_GPU_GL); | 
					
						
							| 
									
										
										
										
											2022-05-28 00:20:23 +02:00
										 |  |  | module_kconfig(VIRTIO_GPU); | 
					
						
							| 
									
										
										
										
											2021-04-30 13:35:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void virtio_register_types(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     type_register_static(&virtio_gpu_gl_info); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type_init(virtio_register_types) | 
					
						
							| 
									
										
										
										
											2021-06-24 12:38:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | module_dep("hw-display-virtio-gpu"); |