hw/sd/pl181: Rename pl181_send_command() as pl181_do_command()
[qemu/ar7.git] / hw / display / vhost-user-gpu.c
blob4cdaee1bde49385bf340533573f52834464b9c1d
1 /*
2 * vhost-user GPU Device
4 * Copyright Red Hat, Inc. 2018
6 * Authors:
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 "hw/qdev-properties.h"
15 #include "hw/virtio/virtio-gpu.h"
16 #include "chardev/char-fe.h"
17 #include "qapi/error.h"
18 #include "migration/blocker.h"
20 #define VHOST_USER_GPU(obj) \
21 OBJECT_CHECK(VhostUserGPU, (obj), TYPE_VHOST_USER_GPU)
23 typedef enum VhostUserGpuRequest {
24 VHOST_USER_GPU_NONE = 0,
25 VHOST_USER_GPU_GET_PROTOCOL_FEATURES,
26 VHOST_USER_GPU_SET_PROTOCOL_FEATURES,
27 VHOST_USER_GPU_GET_DISPLAY_INFO,
28 VHOST_USER_GPU_CURSOR_POS,
29 VHOST_USER_GPU_CURSOR_POS_HIDE,
30 VHOST_USER_GPU_CURSOR_UPDATE,
31 VHOST_USER_GPU_SCANOUT,
32 VHOST_USER_GPU_UPDATE,
33 VHOST_USER_GPU_DMABUF_SCANOUT,
34 VHOST_USER_GPU_DMABUF_UPDATE,
35 } VhostUserGpuRequest;
37 typedef struct VhostUserGpuDisplayInfoReply {
38 struct virtio_gpu_resp_display_info info;
39 } VhostUserGpuDisplayInfoReply;
41 typedef struct VhostUserGpuCursorPos {
42 uint32_t scanout_id;
43 uint32_t x;
44 uint32_t y;
45 } QEMU_PACKED VhostUserGpuCursorPos;
47 typedef struct VhostUserGpuCursorUpdate {
48 VhostUserGpuCursorPos pos;
49 uint32_t hot_x;
50 uint32_t hot_y;
51 uint32_t data[64 * 64];
52 } QEMU_PACKED VhostUserGpuCursorUpdate;
54 typedef struct VhostUserGpuScanout {
55 uint32_t scanout_id;
56 uint32_t width;
57 uint32_t height;
58 } QEMU_PACKED VhostUserGpuScanout;
60 typedef struct VhostUserGpuUpdate {
61 uint32_t scanout_id;
62 uint32_t x;
63 uint32_t y;
64 uint32_t width;
65 uint32_t height;
66 uint8_t data[];
67 } QEMU_PACKED VhostUserGpuUpdate;
69 typedef struct VhostUserGpuDMABUFScanout {
70 uint32_t scanout_id;
71 uint32_t x;
72 uint32_t y;
73 uint32_t width;
74 uint32_t height;
75 uint32_t fd_width;
76 uint32_t fd_height;
77 uint32_t fd_stride;
78 uint32_t fd_flags;
79 int fd_drm_fourcc;
80 } QEMU_PACKED VhostUserGpuDMABUFScanout;
82 typedef struct VhostUserGpuMsg {
83 uint32_t request; /* VhostUserGpuRequest */
84 uint32_t flags;
85 uint32_t size; /* the following payload size */
86 union {
87 VhostUserGpuCursorPos cursor_pos;
88 VhostUserGpuCursorUpdate cursor_update;
89 VhostUserGpuScanout scanout;
90 VhostUserGpuUpdate update;
91 VhostUserGpuDMABUFScanout dmabuf_scanout;
92 struct virtio_gpu_resp_display_info display_info;
93 uint64_t u64;
94 } payload;
95 } QEMU_PACKED VhostUserGpuMsg;
97 static VhostUserGpuMsg m __attribute__ ((unused));
98 #define VHOST_USER_GPU_HDR_SIZE \
99 (sizeof(m.request) + sizeof(m.size) + sizeof(m.flags))
101 #define VHOST_USER_GPU_MSG_FLAG_REPLY 0x4
103 static void vhost_user_gpu_update_blocked(VhostUserGPU *g, bool blocked);
105 static void
106 vhost_user_gpu_handle_cursor(VhostUserGPU *g, VhostUserGpuMsg *msg)
108 VhostUserGpuCursorPos *pos = &msg->payload.cursor_pos;
109 struct virtio_gpu_scanout *s;
111 if (pos->scanout_id >= g->parent_obj.conf.max_outputs) {
112 return;
114 s = &g->parent_obj.scanout[pos->scanout_id];
116 if (msg->request == VHOST_USER_GPU_CURSOR_UPDATE) {
117 VhostUserGpuCursorUpdate *up = &msg->payload.cursor_update;
118 if (!s->current_cursor) {
119 s->current_cursor = cursor_alloc(64, 64);
122 s->current_cursor->hot_x = up->hot_x;
123 s->current_cursor->hot_y = up->hot_y;
125 memcpy(s->current_cursor->data, up->data,
126 64 * 64 * sizeof(uint32_t));
128 dpy_cursor_define(s->con, s->current_cursor);
131 dpy_mouse_set(s->con, pos->x, pos->y,
132 msg->request != VHOST_USER_GPU_CURSOR_POS_HIDE);
135 static void
136 vhost_user_gpu_send_msg(VhostUserGPU *g, const VhostUserGpuMsg *msg)
138 qemu_chr_fe_write(&g->vhost_chr, (uint8_t *)msg,
139 VHOST_USER_GPU_HDR_SIZE + msg->size);
142 static void
143 vhost_user_gpu_unblock(VhostUserGPU *g)
145 VhostUserGpuMsg msg = {
146 .request = VHOST_USER_GPU_DMABUF_UPDATE,
147 .flags = VHOST_USER_GPU_MSG_FLAG_REPLY,
150 vhost_user_gpu_send_msg(g, &msg);
153 static void
154 vhost_user_gpu_handle_display(VhostUserGPU *g, VhostUserGpuMsg *msg)
156 QemuConsole *con = NULL;
157 struct virtio_gpu_scanout *s;
159 switch (msg->request) {
160 case VHOST_USER_GPU_GET_PROTOCOL_FEATURES: {
161 VhostUserGpuMsg reply = {
162 .request = msg->request,
163 .flags = VHOST_USER_GPU_MSG_FLAG_REPLY,
164 .size = sizeof(uint64_t),
167 vhost_user_gpu_send_msg(g, &reply);
168 break;
170 case VHOST_USER_GPU_SET_PROTOCOL_FEATURES: {
171 break;
173 case VHOST_USER_GPU_GET_DISPLAY_INFO: {
174 struct virtio_gpu_resp_display_info display_info = { {} };
175 VhostUserGpuMsg reply = {
176 .request = msg->request,
177 .flags = VHOST_USER_GPU_MSG_FLAG_REPLY,
178 .size = sizeof(struct virtio_gpu_resp_display_info),
181 display_info.hdr.type = VIRTIO_GPU_RESP_OK_DISPLAY_INFO;
182 virtio_gpu_base_fill_display_info(VIRTIO_GPU_BASE(g), &display_info);
183 memcpy(&reply.payload.display_info, &display_info,
184 sizeof(display_info));
185 vhost_user_gpu_send_msg(g, &reply);
186 break;
188 case VHOST_USER_GPU_SCANOUT: {
189 VhostUserGpuScanout *m = &msg->payload.scanout;
191 if (m->scanout_id >= g->parent_obj.conf.max_outputs) {
192 return;
195 g->parent_obj.enable = 1;
196 s = &g->parent_obj.scanout[m->scanout_id];
197 con = s->con;
199 if (m->scanout_id == 0 && m->width == 0) {
200 s->ds = qemu_create_message_surface(640, 480,
201 "Guest disabled display.");
202 dpy_gfx_replace_surface(con, s->ds);
203 } else {
204 s->ds = qemu_create_displaysurface(m->width, m->height);
205 /* replace surface on next update */
208 break;
210 case VHOST_USER_GPU_DMABUF_SCANOUT: {
211 VhostUserGpuDMABUFScanout *m = &msg->payload.dmabuf_scanout;
212 int fd = qemu_chr_fe_get_msgfd(&g->vhost_chr);
213 QemuDmaBuf *dmabuf;
215 if (m->scanout_id >= g->parent_obj.conf.max_outputs) {
216 error_report("invalid scanout: %d", m->scanout_id);
217 if (fd >= 0) {
218 close(fd);
220 break;
223 g->parent_obj.enable = 1;
224 con = g->parent_obj.scanout[m->scanout_id].con;
225 dmabuf = &g->dmabuf[m->scanout_id];
226 if (dmabuf->fd >= 0) {
227 close(dmabuf->fd);
228 dmabuf->fd = -1;
230 if (!console_has_gl_dmabuf(con)) {
231 /* it would be nice to report that error earlier */
232 error_report("console doesn't support dmabuf!");
233 break;
235 dpy_gl_release_dmabuf(con, dmabuf);
236 if (fd == -1) {
237 dpy_gl_scanout_disable(con);
238 break;
240 *dmabuf = (QemuDmaBuf) {
241 .fd = fd,
242 .width = m->fd_width,
243 .height = m->fd_height,
244 .stride = m->fd_stride,
245 .fourcc = m->fd_drm_fourcc,
246 .y0_top = m->fd_flags & VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP,
248 dpy_gl_scanout_dmabuf(con, dmabuf);
249 break;
251 case VHOST_USER_GPU_DMABUF_UPDATE: {
252 VhostUserGpuUpdate *m = &msg->payload.update;
254 if (m->scanout_id >= g->parent_obj.conf.max_outputs ||
255 !g->parent_obj.scanout[m->scanout_id].con) {
256 error_report("invalid scanout update: %d", m->scanout_id);
257 vhost_user_gpu_unblock(g);
258 break;
261 con = g->parent_obj.scanout[m->scanout_id].con;
262 if (!console_has_gl(con)) {
263 error_report("console doesn't support GL!");
264 vhost_user_gpu_unblock(g);
265 break;
267 dpy_gl_update(con, m->x, m->y, m->width, m->height);
268 g->backend_blocked = true;
269 break;
271 case VHOST_USER_GPU_UPDATE: {
272 VhostUserGpuUpdate *m = &msg->payload.update;
274 if (m->scanout_id >= g->parent_obj.conf.max_outputs) {
275 break;
277 s = &g->parent_obj.scanout[m->scanout_id];
278 con = s->con;
279 pixman_image_t *image =
280 pixman_image_create_bits(PIXMAN_x8r8g8b8,
281 m->width,
282 m->height,
283 (uint32_t *)m->data,
284 m->width * 4);
286 pixman_image_composite(PIXMAN_OP_SRC,
287 image, NULL, s->ds->image,
288 0, 0, 0, 0, m->x, m->y, m->width, m->height);
290 pixman_image_unref(image);
291 if (qemu_console_surface(con) != s->ds) {
292 dpy_gfx_replace_surface(con, s->ds);
293 } else {
294 dpy_gfx_update(con, m->x, m->y, m->width, m->height);
296 break;
298 default:
299 g_warning("unhandled message %d %d", msg->request, msg->size);
302 if (con && qemu_console_is_gl_blocked(con)) {
303 vhost_user_gpu_update_blocked(g, true);
307 static void
308 vhost_user_gpu_chr_read(void *opaque)
310 VhostUserGPU *g = opaque;
311 VhostUserGpuMsg *msg = NULL;
312 VhostUserGpuRequest request;
313 uint32_t size, flags;
314 int r;
316 r = qemu_chr_fe_read_all(&g->vhost_chr,
317 (uint8_t *)&request, sizeof(uint32_t));
318 if (r != sizeof(uint32_t)) {
319 error_report("failed to read msg header: %d, %d", r, errno);
320 goto end;
323 r = qemu_chr_fe_read_all(&g->vhost_chr,
324 (uint8_t *)&flags, sizeof(uint32_t));
325 if (r != sizeof(uint32_t)) {
326 error_report("failed to read msg flags");
327 goto end;
330 r = qemu_chr_fe_read_all(&g->vhost_chr,
331 (uint8_t *)&size, sizeof(uint32_t));
332 if (r != sizeof(uint32_t)) {
333 error_report("failed to read msg size");
334 goto end;
337 msg = g_malloc(VHOST_USER_GPU_HDR_SIZE + size);
338 g_return_if_fail(msg != NULL);
340 r = qemu_chr_fe_read_all(&g->vhost_chr,
341 (uint8_t *)&msg->payload, size);
342 if (r != size) {
343 error_report("failed to read msg payload %d != %d", r, size);
344 goto end;
347 msg->request = request;
348 msg->flags = size;
349 msg->size = size;
351 if (request == VHOST_USER_GPU_CURSOR_UPDATE ||
352 request == VHOST_USER_GPU_CURSOR_POS ||
353 request == VHOST_USER_GPU_CURSOR_POS_HIDE) {
354 vhost_user_gpu_handle_cursor(g, msg);
355 } else {
356 vhost_user_gpu_handle_display(g, msg);
359 end:
360 g_free(msg);
363 static void
364 vhost_user_gpu_update_blocked(VhostUserGPU *g, bool blocked)
366 qemu_set_fd_handler(g->vhost_gpu_fd,
367 blocked ? NULL : vhost_user_gpu_chr_read, NULL, g);
370 static void
371 vhost_user_gpu_gl_unblock(VirtIOGPUBase *b)
373 VhostUserGPU *g = VHOST_USER_GPU(b);
375 if (g->backend_blocked) {
376 vhost_user_gpu_unblock(VHOST_USER_GPU(g));
377 g->backend_blocked = false;
380 vhost_user_gpu_update_blocked(VHOST_USER_GPU(g), false);
383 static bool
384 vhost_user_gpu_do_set_socket(VhostUserGPU *g, Error **errp)
386 Chardev *chr;
387 int sv[2];
389 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
390 error_setg_errno(errp, errno, "socketpair() failed");
391 return false;
394 chr = CHARDEV(object_new(TYPE_CHARDEV_SOCKET));
395 if (!chr || qemu_chr_add_client(chr, sv[0]) == -1) {
396 error_setg(errp, "Failed to make socket chardev");
397 goto err;
399 if (!qemu_chr_fe_init(&g->vhost_chr, chr, errp)) {
400 goto err;
402 if (vhost_user_gpu_set_socket(&g->vhost->dev, sv[1]) < 0) {
403 error_setg(errp, "Failed to set vhost-user-gpu socket");
404 qemu_chr_fe_deinit(&g->vhost_chr, false);
405 goto err;
408 g->vhost_gpu_fd = sv[0];
409 vhost_user_gpu_update_blocked(g, false);
410 close(sv[1]);
411 return true;
413 err:
414 close(sv[0]);
415 close(sv[1]);
416 if (chr) {
417 object_unref(OBJECT(chr));
419 return false;
422 static void
423 vhost_user_gpu_get_config(VirtIODevice *vdev, uint8_t *config_data)
425 VhostUserGPU *g = VHOST_USER_GPU(vdev);
426 VirtIOGPUBase *b = VIRTIO_GPU_BASE(vdev);
427 struct virtio_gpu_config *vgconfig =
428 (struct virtio_gpu_config *)config_data;
429 int ret;
431 memset(config_data, 0, sizeof(struct virtio_gpu_config));
433 ret = vhost_dev_get_config(&g->vhost->dev,
434 config_data, sizeof(struct virtio_gpu_config));
435 if (ret) {
436 error_report("vhost-user-gpu: get device config space failed");
437 return;
440 /* those fields are managed by qemu */
441 vgconfig->num_scanouts = b->virtio_config.num_scanouts;
442 vgconfig->events_read = b->virtio_config.events_read;
443 vgconfig->events_clear = b->virtio_config.events_clear;
446 static void
447 vhost_user_gpu_set_config(VirtIODevice *vdev,
448 const uint8_t *config_data)
450 VhostUserGPU *g = VHOST_USER_GPU(vdev);
451 VirtIOGPUBase *b = VIRTIO_GPU_BASE(vdev);
452 const struct virtio_gpu_config *vgconfig =
453 (const struct virtio_gpu_config *)config_data;
454 int ret;
456 if (vgconfig->events_clear) {
457 b->virtio_config.events_read &= ~vgconfig->events_clear;
460 ret = vhost_dev_set_config(&g->vhost->dev, config_data,
461 0, sizeof(struct virtio_gpu_config),
462 VHOST_SET_CONFIG_TYPE_MASTER);
463 if (ret) {
464 error_report("vhost-user-gpu: set device config space failed");
465 return;
469 static void
470 vhost_user_gpu_set_status(VirtIODevice *vdev, uint8_t val)
472 VhostUserGPU *g = VHOST_USER_GPU(vdev);
473 Error *err = NULL;
475 if (val & VIRTIO_CONFIG_S_DRIVER_OK && vdev->vm_running) {
476 if (!vhost_user_gpu_do_set_socket(g, &err)) {
477 error_report_err(err);
478 return;
480 vhost_user_backend_start(g->vhost);
481 } else {
482 /* unblock any wait and stop processing */
483 if (g->vhost_gpu_fd != -1) {
484 vhost_user_gpu_update_blocked(g, true);
485 qemu_chr_fe_deinit(&g->vhost_chr, true);
486 g->vhost_gpu_fd = -1;
488 vhost_user_backend_stop(g->vhost);
492 static bool
493 vhost_user_gpu_guest_notifier_pending(VirtIODevice *vdev, int idx)
495 VhostUserGPU *g = VHOST_USER_GPU(vdev);
497 return vhost_virtqueue_pending(&g->vhost->dev, idx);
500 static void
501 vhost_user_gpu_guest_notifier_mask(VirtIODevice *vdev, int idx, bool mask)
503 VhostUserGPU *g = VHOST_USER_GPU(vdev);
505 vhost_virtqueue_mask(&g->vhost->dev, vdev, idx, mask);
508 static void
509 vhost_user_gpu_instance_init(Object *obj)
511 VhostUserGPU *g = VHOST_USER_GPU(obj);
513 g->vhost = VHOST_USER_BACKEND(object_new(TYPE_VHOST_USER_BACKEND));
514 object_property_add_alias(obj, "chardev",
515 OBJECT(g->vhost), "chardev");
518 static void
519 vhost_user_gpu_instance_finalize(Object *obj)
521 VhostUserGPU *g = VHOST_USER_GPU(obj);
523 object_unref(OBJECT(g->vhost));
526 static void
527 vhost_user_gpu_reset(VirtIODevice *vdev)
529 VhostUserGPU *g = VHOST_USER_GPU(vdev);
531 virtio_gpu_base_reset(VIRTIO_GPU_BASE(vdev));
533 vhost_user_backend_stop(g->vhost);
536 static int
537 vhost_user_gpu_config_change(struct vhost_dev *dev)
539 error_report("vhost-user-gpu: unhandled backend config change");
540 return -1;
543 static const VhostDevConfigOps config_ops = {
544 .vhost_dev_config_notifier = vhost_user_gpu_config_change,
547 static void
548 vhost_user_gpu_device_realize(DeviceState *qdev, Error **errp)
550 VhostUserGPU *g = VHOST_USER_GPU(qdev);
551 VirtIODevice *vdev = VIRTIO_DEVICE(g);
553 vhost_dev_set_config_notifier(&g->vhost->dev, &config_ops);
554 if (vhost_user_backend_dev_init(g->vhost, vdev, 2, errp) < 0) {
555 return;
558 if (virtio_has_feature(g->vhost->dev.features, VIRTIO_GPU_F_VIRGL)) {
559 g->parent_obj.conf.flags |= 1 << VIRTIO_GPU_FLAG_VIRGL_ENABLED;
562 if (!virtio_gpu_base_device_realize(qdev, NULL, NULL, errp)) {
563 return;
566 g->vhost_gpu_fd = -1;
569 static Property vhost_user_gpu_properties[] = {
570 VIRTIO_GPU_BASE_PROPERTIES(VhostUserGPU, parent_obj.conf),
571 DEFINE_PROP_END_OF_LIST(),
574 static void
575 vhost_user_gpu_class_init(ObjectClass *klass, void *data)
577 DeviceClass *dc = DEVICE_CLASS(klass);
578 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
579 VirtIOGPUBaseClass *vgc = VIRTIO_GPU_BASE_CLASS(klass);
581 vgc->gl_unblock = vhost_user_gpu_gl_unblock;
583 vdc->realize = vhost_user_gpu_device_realize;
584 vdc->reset = vhost_user_gpu_reset;
585 vdc->set_status = vhost_user_gpu_set_status;
586 vdc->guest_notifier_mask = vhost_user_gpu_guest_notifier_mask;
587 vdc->guest_notifier_pending = vhost_user_gpu_guest_notifier_pending;
588 vdc->get_config = vhost_user_gpu_get_config;
589 vdc->set_config = vhost_user_gpu_set_config;
591 device_class_set_props(dc, vhost_user_gpu_properties);
594 static const TypeInfo vhost_user_gpu_info = {
595 .name = TYPE_VHOST_USER_GPU,
596 .parent = TYPE_VIRTIO_GPU_BASE,
597 .instance_size = sizeof(VhostUserGPU),
598 .instance_init = vhost_user_gpu_instance_init,
599 .instance_finalize = vhost_user_gpu_instance_finalize,
600 .class_init = vhost_user_gpu_class_init,
603 static void vhost_user_gpu_register_types(void)
605 type_register_static(&vhost_user_gpu_info);
608 type_init(vhost_user_gpu_register_types)