2 * vhost-user GPU Device
4 * Copyright Red Hat, Inc. 2018
7 * Marc-André Lureau <marcandre.lureau@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qemu/error-report.h"
15 #include "qemu/sockets.h"
16 #include "hw/qdev-properties.h"
17 #include "hw/virtio/virtio-gpu.h"
18 #include "chardev/char-fe.h"
19 #include "qapi/error.h"
20 #include "migration/blocker.h"
22 typedef enum VhostUserGpuRequest
{
23 VHOST_USER_GPU_NONE
= 0,
24 VHOST_USER_GPU_GET_PROTOCOL_FEATURES
,
25 VHOST_USER_GPU_SET_PROTOCOL_FEATURES
,
26 VHOST_USER_GPU_GET_DISPLAY_INFO
,
27 VHOST_USER_GPU_CURSOR_POS
,
28 VHOST_USER_GPU_CURSOR_POS_HIDE
,
29 VHOST_USER_GPU_CURSOR_UPDATE
,
30 VHOST_USER_GPU_SCANOUT
,
31 VHOST_USER_GPU_UPDATE
,
32 VHOST_USER_GPU_DMABUF_SCANOUT
,
33 VHOST_USER_GPU_DMABUF_UPDATE
,
34 } VhostUserGpuRequest
;
36 typedef struct VhostUserGpuDisplayInfoReply
{
37 struct virtio_gpu_resp_display_info info
;
38 } VhostUserGpuDisplayInfoReply
;
40 typedef struct VhostUserGpuCursorPos
{
44 } QEMU_PACKED VhostUserGpuCursorPos
;
46 typedef struct VhostUserGpuCursorUpdate
{
47 VhostUserGpuCursorPos pos
;
50 uint32_t data
[64 * 64];
51 } QEMU_PACKED VhostUserGpuCursorUpdate
;
53 typedef struct VhostUserGpuScanout
{
57 } QEMU_PACKED VhostUserGpuScanout
;
59 typedef struct VhostUserGpuUpdate
{
66 } QEMU_PACKED VhostUserGpuUpdate
;
68 typedef struct VhostUserGpuDMABUFScanout
{
79 } QEMU_PACKED VhostUserGpuDMABUFScanout
;
81 typedef struct VhostUserGpuMsg
{
82 uint32_t request
; /* VhostUserGpuRequest */
84 uint32_t size
; /* the following payload size */
86 VhostUserGpuCursorPos cursor_pos
;
87 VhostUserGpuCursorUpdate cursor_update
;
88 VhostUserGpuScanout scanout
;
89 VhostUserGpuUpdate update
;
90 VhostUserGpuDMABUFScanout dmabuf_scanout
;
91 struct virtio_gpu_resp_display_info display_info
;
94 } QEMU_PACKED VhostUserGpuMsg
;
96 static VhostUserGpuMsg m
__attribute__ ((unused
));
97 #define VHOST_USER_GPU_HDR_SIZE \
98 (sizeof(m.request) + sizeof(m.size) + sizeof(m.flags))
100 #define VHOST_USER_GPU_MSG_FLAG_REPLY 0x4
102 static void vhost_user_gpu_update_blocked(VhostUserGPU
*g
, bool blocked
);
105 vhost_user_gpu_handle_cursor(VhostUserGPU
*g
, VhostUserGpuMsg
*msg
)
107 VhostUserGpuCursorPos
*pos
= &msg
->payload
.cursor_pos
;
108 struct virtio_gpu_scanout
*s
;
110 if (pos
->scanout_id
>= g
->parent_obj
.conf
.max_outputs
) {
113 s
= &g
->parent_obj
.scanout
[pos
->scanout_id
];
115 if (msg
->request
== VHOST_USER_GPU_CURSOR_UPDATE
) {
116 VhostUserGpuCursorUpdate
*up
= &msg
->payload
.cursor_update
;
117 if (!s
->current_cursor
) {
118 s
->current_cursor
= cursor_alloc(64, 64);
121 s
->current_cursor
->hot_x
= up
->hot_x
;
122 s
->current_cursor
->hot_y
= up
->hot_y
;
124 memcpy(s
->current_cursor
->data
, up
->data
,
125 64 * 64 * sizeof(uint32_t));
127 dpy_cursor_define(s
->con
, s
->current_cursor
);
130 dpy_mouse_set(s
->con
, pos
->x
, pos
->y
,
131 msg
->request
!= VHOST_USER_GPU_CURSOR_POS_HIDE
);
135 vhost_user_gpu_send_msg(VhostUserGPU
*g
, const VhostUserGpuMsg
*msg
)
137 qemu_chr_fe_write(&g
->vhost_chr
, (uint8_t *)msg
,
138 VHOST_USER_GPU_HDR_SIZE
+ msg
->size
);
142 vhost_user_gpu_unblock(VhostUserGPU
*g
)
144 VhostUserGpuMsg msg
= {
145 .request
= VHOST_USER_GPU_DMABUF_UPDATE
,
146 .flags
= VHOST_USER_GPU_MSG_FLAG_REPLY
,
149 vhost_user_gpu_send_msg(g
, &msg
);
153 vhost_user_gpu_handle_display(VhostUserGPU
*g
, VhostUserGpuMsg
*msg
)
155 QemuConsole
*con
= NULL
;
156 struct virtio_gpu_scanout
*s
;
158 switch (msg
->request
) {
159 case VHOST_USER_GPU_GET_PROTOCOL_FEATURES
: {
160 VhostUserGpuMsg reply
= {
161 .request
= msg
->request
,
162 .flags
= VHOST_USER_GPU_MSG_FLAG_REPLY
,
163 .size
= sizeof(uint64_t),
166 vhost_user_gpu_send_msg(g
, &reply
);
169 case VHOST_USER_GPU_SET_PROTOCOL_FEATURES
: {
172 case VHOST_USER_GPU_GET_DISPLAY_INFO
: {
173 struct virtio_gpu_resp_display_info display_info
= { {} };
174 VhostUserGpuMsg reply
= {
175 .request
= msg
->request
,
176 .flags
= VHOST_USER_GPU_MSG_FLAG_REPLY
,
177 .size
= sizeof(struct virtio_gpu_resp_display_info
),
180 display_info
.hdr
.type
= VIRTIO_GPU_RESP_OK_DISPLAY_INFO
;
181 virtio_gpu_base_fill_display_info(VIRTIO_GPU_BASE(g
), &display_info
);
182 memcpy(&reply
.payload
.display_info
, &display_info
,
183 sizeof(display_info
));
184 vhost_user_gpu_send_msg(g
, &reply
);
187 case VHOST_USER_GPU_SCANOUT
: {
188 VhostUserGpuScanout
*m
= &msg
->payload
.scanout
;
190 if (m
->scanout_id
>= g
->parent_obj
.conf
.max_outputs
) {
194 g
->parent_obj
.enable
= 1;
195 s
= &g
->parent_obj
.scanout
[m
->scanout_id
];
199 dpy_gfx_replace_surface(con
, NULL
);
201 s
->ds
= qemu_create_displaysurface(m
->width
, m
->height
);
202 /* replace surface on next update */
207 case VHOST_USER_GPU_DMABUF_SCANOUT
: {
208 VhostUserGpuDMABUFScanout
*m
= &msg
->payload
.dmabuf_scanout
;
209 int fd
= qemu_chr_fe_get_msgfd(&g
->vhost_chr
);
212 if (m
->scanout_id
>= g
->parent_obj
.conf
.max_outputs
) {
213 error_report("invalid scanout: %d", m
->scanout_id
);
220 g
->parent_obj
.enable
= 1;
221 con
= g
->parent_obj
.scanout
[m
->scanout_id
].con
;
222 dmabuf
= &g
->dmabuf
[m
->scanout_id
];
223 if (dmabuf
->fd
>= 0) {
227 dpy_gl_release_dmabuf(con
, dmabuf
);
229 dpy_gl_scanout_disable(con
);
232 *dmabuf
= (QemuDmaBuf
) {
234 .width
= m
->fd_width
,
235 .height
= m
->fd_height
,
236 .stride
= m
->fd_stride
,
237 .fourcc
= m
->fd_drm_fourcc
,
238 .y0_top
= m
->fd_flags
& VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP
,
240 dpy_gl_scanout_dmabuf(con
, dmabuf
);
243 case VHOST_USER_GPU_DMABUF_UPDATE
: {
244 VhostUserGpuUpdate
*m
= &msg
->payload
.update
;
246 if (m
->scanout_id
>= g
->parent_obj
.conf
.max_outputs
||
247 !g
->parent_obj
.scanout
[m
->scanout_id
].con
) {
248 error_report("invalid scanout update: %d", m
->scanout_id
);
249 vhost_user_gpu_unblock(g
);
253 con
= g
->parent_obj
.scanout
[m
->scanout_id
].con
;
254 if (!console_has_gl(con
)) {
255 error_report("console doesn't support GL!");
256 vhost_user_gpu_unblock(g
);
259 g
->backend_blocked
= true;
260 dpy_gl_update(con
, m
->x
, m
->y
, m
->width
, m
->height
);
263 case VHOST_USER_GPU_UPDATE
: {
264 VhostUserGpuUpdate
*m
= &msg
->payload
.update
;
266 if (m
->scanout_id
>= g
->parent_obj
.conf
.max_outputs
) {
269 s
= &g
->parent_obj
.scanout
[m
->scanout_id
];
271 pixman_image_t
*image
=
272 pixman_image_create_bits(PIXMAN_x8r8g8b8
,
278 pixman_image_composite(PIXMAN_OP_SRC
,
279 image
, NULL
, s
->ds
->image
,
280 0, 0, 0, 0, m
->x
, m
->y
, m
->width
, m
->height
);
282 pixman_image_unref(image
);
283 if (qemu_console_surface(con
) != s
->ds
) {
284 dpy_gfx_replace_surface(con
, s
->ds
);
286 dpy_gfx_update(con
, m
->x
, m
->y
, m
->width
, m
->height
);
291 g_warning("unhandled message %d %d", msg
->request
, msg
->size
);
294 if (con
&& qemu_console_is_gl_blocked(con
)) {
295 vhost_user_gpu_update_blocked(g
, true);
300 vhost_user_gpu_chr_read(void *opaque
)
302 VhostUserGPU
*g
= opaque
;
303 VhostUserGpuMsg
*msg
= NULL
;
304 VhostUserGpuRequest request
;
305 uint32_t size
, flags
;
308 r
= qemu_chr_fe_read_all(&g
->vhost_chr
,
309 (uint8_t *)&request
, sizeof(uint32_t));
310 if (r
!= sizeof(uint32_t)) {
311 error_report("failed to read msg header: %d, %d", r
, errno
);
315 r
= qemu_chr_fe_read_all(&g
->vhost_chr
,
316 (uint8_t *)&flags
, sizeof(uint32_t));
317 if (r
!= sizeof(uint32_t)) {
318 error_report("failed to read msg flags");
322 r
= qemu_chr_fe_read_all(&g
->vhost_chr
,
323 (uint8_t *)&size
, sizeof(uint32_t));
324 if (r
!= sizeof(uint32_t)) {
325 error_report("failed to read msg size");
329 msg
= g_malloc(VHOST_USER_GPU_HDR_SIZE
+ size
);
331 r
= qemu_chr_fe_read_all(&g
->vhost_chr
,
332 (uint8_t *)&msg
->payload
, size
);
334 error_report("failed to read msg payload %d != %d", r
, size
);
338 msg
->request
= request
;
342 if (request
== VHOST_USER_GPU_CURSOR_UPDATE
||
343 request
== VHOST_USER_GPU_CURSOR_POS
||
344 request
== VHOST_USER_GPU_CURSOR_POS_HIDE
) {
345 vhost_user_gpu_handle_cursor(g
, msg
);
347 vhost_user_gpu_handle_display(g
, msg
);
355 vhost_user_gpu_update_blocked(VhostUserGPU
*g
, bool blocked
)
357 qemu_set_fd_handler(g
->vhost_gpu_fd
,
358 blocked
? NULL
: vhost_user_gpu_chr_read
, NULL
, g
);
362 vhost_user_gpu_gl_flushed(VirtIOGPUBase
*b
)
364 VhostUserGPU
*g
= VHOST_USER_GPU(b
);
366 if (g
->backend_blocked
) {
367 vhost_user_gpu_unblock(VHOST_USER_GPU(g
));
368 g
->backend_blocked
= false;
371 vhost_user_gpu_update_blocked(VHOST_USER_GPU(g
), false);
375 vhost_user_gpu_do_set_socket(VhostUserGPU
*g
, Error
**errp
)
380 if (qemu_socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
) == -1) {
381 error_setg_errno(errp
, errno
, "socketpair() failed");
385 chr
= CHARDEV(object_new(TYPE_CHARDEV_SOCKET
));
386 if (!chr
|| qemu_chr_add_client(chr
, sv
[0]) == -1) {
387 error_setg(errp
, "Failed to make socket chardev");
390 if (!qemu_chr_fe_init(&g
->vhost_chr
, chr
, errp
)) {
393 if (vhost_user_gpu_set_socket(&g
->vhost
->dev
, sv
[1]) < 0) {
394 error_setg(errp
, "Failed to set vhost-user-gpu socket");
395 qemu_chr_fe_deinit(&g
->vhost_chr
, false);
399 g
->vhost_gpu_fd
= sv
[0];
400 vhost_user_gpu_update_blocked(g
, false);
408 object_unref(OBJECT(chr
));
414 vhost_user_gpu_get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
416 VhostUserGPU
*g
= VHOST_USER_GPU(vdev
);
417 VirtIOGPUBase
*b
= VIRTIO_GPU_BASE(vdev
);
418 struct virtio_gpu_config
*vgconfig
=
419 (struct virtio_gpu_config
*)config_data
;
420 Error
*local_err
= NULL
;
423 memset(config_data
, 0, sizeof(struct virtio_gpu_config
));
425 ret
= vhost_dev_get_config(&g
->vhost
->dev
,
426 config_data
, sizeof(struct virtio_gpu_config
),
429 error_report_err(local_err
);
433 /* those fields are managed by qemu */
434 vgconfig
->num_scanouts
= b
->virtio_config
.num_scanouts
;
435 vgconfig
->events_read
= b
->virtio_config
.events_read
;
436 vgconfig
->events_clear
= b
->virtio_config
.events_clear
;
440 vhost_user_gpu_set_config(VirtIODevice
*vdev
,
441 const uint8_t *config_data
)
443 VhostUserGPU
*g
= VHOST_USER_GPU(vdev
);
444 VirtIOGPUBase
*b
= VIRTIO_GPU_BASE(vdev
);
445 const struct virtio_gpu_config
*vgconfig
=
446 (const struct virtio_gpu_config
*)config_data
;
449 if (vgconfig
->events_clear
) {
450 b
->virtio_config
.events_read
&= ~vgconfig
->events_clear
;
453 ret
= vhost_dev_set_config(&g
->vhost
->dev
, config_data
,
454 0, sizeof(struct virtio_gpu_config
),
455 VHOST_SET_CONFIG_TYPE_MASTER
);
457 error_report("vhost-user-gpu: set device config space failed");
463 vhost_user_gpu_set_status(VirtIODevice
*vdev
, uint8_t val
)
465 VhostUserGPU
*g
= VHOST_USER_GPU(vdev
);
468 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
&& vdev
->vm_running
) {
469 if (!vhost_user_gpu_do_set_socket(g
, &err
)) {
470 error_report_err(err
);
473 vhost_user_backend_start(g
->vhost
);
475 /* unblock any wait and stop processing */
476 if (g
->vhost_gpu_fd
!= -1) {
477 vhost_user_gpu_update_blocked(g
, true);
478 qemu_chr_fe_deinit(&g
->vhost_chr
, true);
479 g
->vhost_gpu_fd
= -1;
481 vhost_user_backend_stop(g
->vhost
);
486 vhost_user_gpu_guest_notifier_pending(VirtIODevice
*vdev
, int idx
)
488 VhostUserGPU
*g
= VHOST_USER_GPU(vdev
);
491 * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
492 * as the Marco of configure interrupt's IDX, If this driver does not
493 * support, the function will return
496 if (idx
== VIRTIO_CONFIG_IRQ_IDX
) {
499 return vhost_virtqueue_pending(&g
->vhost
->dev
, idx
);
503 vhost_user_gpu_guest_notifier_mask(VirtIODevice
*vdev
, int idx
, bool mask
)
505 VhostUserGPU
*g
= VHOST_USER_GPU(vdev
);
508 * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
509 * as the Marco of configure interrupt's IDX, If this driver does not
510 * support, the function will return
513 if (idx
== VIRTIO_CONFIG_IRQ_IDX
) {
516 vhost_virtqueue_mask(&g
->vhost
->dev
, vdev
, idx
, mask
);
520 vhost_user_gpu_instance_init(Object
*obj
)
522 VhostUserGPU
*g
= VHOST_USER_GPU(obj
);
524 g
->vhost
= VHOST_USER_BACKEND(object_new(TYPE_VHOST_USER_BACKEND
));
525 object_property_add_alias(obj
, "chardev",
526 OBJECT(g
->vhost
), "chardev");
530 vhost_user_gpu_instance_finalize(Object
*obj
)
532 VhostUserGPU
*g
= VHOST_USER_GPU(obj
);
534 object_unref(OBJECT(g
->vhost
));
538 vhost_user_gpu_reset(VirtIODevice
*vdev
)
540 VhostUserGPU
*g
= VHOST_USER_GPU(vdev
);
542 virtio_gpu_base_reset(VIRTIO_GPU_BASE(vdev
));
544 vhost_user_backend_stop(g
->vhost
);
548 vhost_user_gpu_config_change(struct vhost_dev
*dev
)
550 error_report("vhost-user-gpu: unhandled backend config change");
554 static const VhostDevConfigOps config_ops
= {
555 .vhost_dev_config_notifier
= vhost_user_gpu_config_change
,
559 vhost_user_gpu_device_realize(DeviceState
*qdev
, Error
**errp
)
561 VhostUserGPU
*g
= VHOST_USER_GPU(qdev
);
562 VirtIODevice
*vdev
= VIRTIO_DEVICE(g
);
564 vhost_dev_set_config_notifier(&g
->vhost
->dev
, &config_ops
);
565 if (vhost_user_backend_dev_init(g
->vhost
, vdev
, 2, errp
) < 0) {
569 /* existing backend may send DMABUF, so let's add that requirement */
570 g
->parent_obj
.conf
.flags
|= 1 << VIRTIO_GPU_FLAG_DMABUF_ENABLED
;
571 if (virtio_has_feature(g
->vhost
->dev
.features
, VIRTIO_GPU_F_VIRGL
)) {
572 g
->parent_obj
.conf
.flags
|= 1 << VIRTIO_GPU_FLAG_VIRGL_ENABLED
;
574 if (virtio_has_feature(g
->vhost
->dev
.features
, VIRTIO_GPU_F_EDID
)) {
575 g
->parent_obj
.conf
.flags
|= 1 << VIRTIO_GPU_FLAG_EDID_ENABLED
;
577 error_report("EDID requested but the backend doesn't support it.");
578 g
->parent_obj
.conf
.flags
&= ~(1 << VIRTIO_GPU_FLAG_EDID_ENABLED
);
581 if (!virtio_gpu_base_device_realize(qdev
, NULL
, NULL
, errp
)) {
585 g
->vhost_gpu_fd
= -1;
588 static struct vhost_dev
*vhost_user_gpu_get_vhost(VirtIODevice
*vdev
)
590 VhostUserGPU
*g
= VHOST_USER_GPU(vdev
);
591 return &g
->vhost
->dev
;
594 static Property vhost_user_gpu_properties
[] = {
595 VIRTIO_GPU_BASE_PROPERTIES(VhostUserGPU
, parent_obj
.conf
),
596 DEFINE_PROP_END_OF_LIST(),
600 vhost_user_gpu_class_init(ObjectClass
*klass
, void *data
)
602 DeviceClass
*dc
= DEVICE_CLASS(klass
);
603 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
604 VirtIOGPUBaseClass
*vgc
= VIRTIO_GPU_BASE_CLASS(klass
);
606 vgc
->gl_flushed
= vhost_user_gpu_gl_flushed
;
608 vdc
->realize
= vhost_user_gpu_device_realize
;
609 vdc
->reset
= vhost_user_gpu_reset
;
610 vdc
->set_status
= vhost_user_gpu_set_status
;
611 vdc
->guest_notifier_mask
= vhost_user_gpu_guest_notifier_mask
;
612 vdc
->guest_notifier_pending
= vhost_user_gpu_guest_notifier_pending
;
613 vdc
->get_config
= vhost_user_gpu_get_config
;
614 vdc
->set_config
= vhost_user_gpu_set_config
;
615 vdc
->get_vhost
= vhost_user_gpu_get_vhost
;
617 device_class_set_props(dc
, vhost_user_gpu_properties
);
620 static const TypeInfo vhost_user_gpu_info
= {
621 .name
= TYPE_VHOST_USER_GPU
,
622 .parent
= TYPE_VIRTIO_GPU_BASE
,
623 .instance_size
= sizeof(VhostUserGPU
),
624 .instance_init
= vhost_user_gpu_instance_init
,
625 .instance_finalize
= vhost_user_gpu_instance_finalize
,
626 .class_init
= vhost_user_gpu_class_init
,
628 module_obj(TYPE_VHOST_USER_GPU
);
629 module_kconfig(VHOST_USER_GPU
);
631 static void vhost_user_gpu_register_types(void)
633 type_register_static(&vhost_user_gpu_info
);
636 type_init(vhost_user_gpu_register_types
)