4 * Copyright Red Hat, Inc. 2013-2014
7 * Dave Airlie <airlied@redhat.com>
8 * Gerd Hoffmann <kraxel@redhat.com>
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.
14 #include "qemu/osdep.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"
26 #include <virglrenderer.h>
28 static void virtio_gpu_gl_update_cursor_data(VirtIOGPU
*g
,
29 struct virtio_gpu_scanout
*s
,
32 uint32_t width
, height
;
33 uint32_t pixels
, *data
;
35 data
= virgl_renderer_get_cursor_data(resource_id
, &width
, &height
);
40 if (width
!= s
->current_cursor
->width
||
41 height
!= s
->current_cursor
->height
) {
46 pixels
= s
->current_cursor
->width
* s
->current_cursor
->height
;
47 memcpy(s
->current_cursor
->data
, data
, pixels
* sizeof(uint32_t));
51 static void virtio_gpu_gl_flushed(VirtIOGPUBase
*b
)
53 VirtIOGPU
*g
= VIRTIO_GPU(b
);
54 VirtIOGPUGL
*gl
= VIRTIO_GPU_GL(b
);
56 if (gl
->renderer_reset
) {
57 gl
->renderer_reset
= false;
58 virtio_gpu_virgl_reset(g
);
60 virtio_gpu_process_cmdq(g
);
63 static void virtio_gpu_gl_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
65 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
66 VirtIOGPUGL
*gl
= VIRTIO_GPU_GL(vdev
);
67 struct virtio_gpu_ctrl_command
*cmd
;
69 if (!virtio_queue_ready(vq
)) {
73 if (!gl
->renderer_inited
) {
74 virtio_gpu_virgl_init(g
);
75 gl
->renderer_inited
= true;
78 cmd
= virtqueue_pop(vq
, sizeof(struct virtio_gpu_ctrl_command
));
82 cmd
->finished
= false;
83 QTAILQ_INSERT_TAIL(&g
->cmdq
, cmd
, next
);
84 cmd
= virtqueue_pop(vq
, sizeof(struct virtio_gpu_ctrl_command
));
87 virtio_gpu_process_cmdq(g
);
88 virtio_gpu_virgl_fence_poll(g
);
91 static void virtio_gpu_gl_reset(VirtIODevice
*vdev
)
93 VirtIOGPU
*g
= VIRTIO_GPU(vdev
);
94 VirtIOGPUGL
*gl
= VIRTIO_GPU_GL(vdev
);
96 virtio_gpu_reset(vdev
);
98 if (gl
->renderer_inited
) {
99 if (g
->parent_obj
.renderer_blocked
) {
100 gl
->renderer_reset
= true;
102 virtio_gpu_virgl_reset(g
);
107 static void virtio_gpu_gl_device_realize(DeviceState
*qdev
, Error
**errp
)
109 VirtIOGPU
*g
= VIRTIO_GPU(qdev
);
111 #if defined(HOST_WORDS_BIGENDIAN)
112 error_setg(errp
, "virgl is not supported on bigendian platforms");
116 if (!display_opengl
) {
117 error_setg(errp
, "opengl is not available");
121 g
->parent_obj
.conf
.flags
|= (1 << VIRTIO_GPU_FLAG_VIRGL_ENABLED
);
122 VIRTIO_GPU_BASE(g
)->virtio_config
.num_capsets
=
123 virtio_gpu_virgl_get_num_capsets(g
);
125 virtio_gpu_device_realize(qdev
, errp
);
128 static Property virtio_gpu_gl_properties
[] = {
129 DEFINE_PROP_BIT("stats", VirtIOGPU
, parent_obj
.conf
.flags
,
130 VIRTIO_GPU_FLAG_STATS_ENABLED
, false),
131 DEFINE_PROP_END_OF_LIST(),
134 static void virtio_gpu_gl_class_init(ObjectClass
*klass
, void *data
)
136 DeviceClass
*dc
= DEVICE_CLASS(klass
);
137 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
138 VirtIOGPUBaseClass
*vbc
= VIRTIO_GPU_BASE_CLASS(klass
);
139 VirtIOGPUClass
*vgc
= VIRTIO_GPU_CLASS(klass
);
141 vbc
->gl_flushed
= virtio_gpu_gl_flushed
;
142 vgc
->handle_ctrl
= virtio_gpu_gl_handle_ctrl
;
143 vgc
->process_cmd
= virtio_gpu_virgl_process_cmd
;
144 vgc
->update_cursor_data
= virtio_gpu_gl_update_cursor_data
;
146 vdc
->realize
= virtio_gpu_gl_device_realize
;
147 vdc
->reset
= virtio_gpu_gl_reset
;
148 device_class_set_props(dc
, virtio_gpu_gl_properties
);
151 static const TypeInfo virtio_gpu_gl_info
= {
152 .name
= TYPE_VIRTIO_GPU_GL
,
153 .parent
= TYPE_VIRTIO_GPU
,
154 .instance_size
= sizeof(VirtIOGPUGL
),
155 .class_init
= virtio_gpu_gl_class_init
,
158 static void virtio_register_types(void)
160 type_register_static(&virtio_gpu_gl_info
);
163 type_init(virtio_register_types
)