xref: /openbmc/qemu/hw/display/vhost-user-gpu.c (revision 243705aa)
1 /*
2  * vhost-user GPU Device
3  *
4  * Copyright Red Hat, Inc. 2018
5  *
6  * Authors:
7  *     Marc-André Lureau <marcandre.lureau@redhat.com>
8  *
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.
11  */
12 
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"
21 
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;
35 
36 typedef struct VhostUserGpuDisplayInfoReply {
37     struct virtio_gpu_resp_display_info info;
38 } VhostUserGpuDisplayInfoReply;
39 
40 typedef struct VhostUserGpuCursorPos {
41     uint32_t scanout_id;
42     uint32_t x;
43     uint32_t y;
44 } QEMU_PACKED VhostUserGpuCursorPos;
45 
46 typedef struct VhostUserGpuCursorUpdate {
47     VhostUserGpuCursorPos pos;
48     uint32_t hot_x;
49     uint32_t hot_y;
50     uint32_t data[64 * 64];
51 } QEMU_PACKED VhostUserGpuCursorUpdate;
52 
53 typedef struct VhostUserGpuScanout {
54     uint32_t scanout_id;
55     uint32_t width;
56     uint32_t height;
57 } QEMU_PACKED VhostUserGpuScanout;
58 
59 typedef struct VhostUserGpuUpdate {
60     uint32_t scanout_id;
61     uint32_t x;
62     uint32_t y;
63     uint32_t width;
64     uint32_t height;
65     uint8_t data[];
66 } QEMU_PACKED VhostUserGpuUpdate;
67 
68 typedef struct VhostUserGpuDMABUFScanout {
69     uint32_t scanout_id;
70     uint32_t x;
71     uint32_t y;
72     uint32_t width;
73     uint32_t height;
74     uint32_t fd_width;
75     uint32_t fd_height;
76     uint32_t fd_stride;
77     uint32_t fd_flags;
78     int fd_drm_fourcc;
79 } QEMU_PACKED VhostUserGpuDMABUFScanout;
80 
81 typedef struct VhostUserGpuMsg {
82     uint32_t request; /* VhostUserGpuRequest */
83     uint32_t flags;
84     uint32_t size; /* the following payload size */
85     union {
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;
92         uint64_t u64;
93     } payload;
94 } QEMU_PACKED VhostUserGpuMsg;
95 
96 static VhostUserGpuMsg m __attribute__ ((unused));
97 #define VHOST_USER_GPU_HDR_SIZE \
98     (sizeof(m.request) + sizeof(m.size) + sizeof(m.flags))
99 
100 #define VHOST_USER_GPU_MSG_FLAG_REPLY 0x4
101 
102 static void vhost_user_gpu_update_blocked(VhostUserGPU *g, bool blocked);
103 
104 static void
105 vhost_user_gpu_handle_cursor(VhostUserGPU *g, VhostUserGpuMsg *msg)
106 {
107     VhostUserGpuCursorPos *pos = &msg->payload.cursor_pos;
108     struct virtio_gpu_scanout *s;
109 
110     if (pos->scanout_id >= g->parent_obj.conf.max_outputs) {
111         return;
112     }
113     s = &g->parent_obj.scanout[pos->scanout_id];
114 
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);
119         }
120 
121         s->current_cursor->hot_x = up->hot_x;
122         s->current_cursor->hot_y = up->hot_y;
123 
124         memcpy(s->current_cursor->data, up->data,
125                64 * 64 * sizeof(uint32_t));
126 
127         dpy_cursor_define(s->con, s->current_cursor);
128     }
129 
130     dpy_mouse_set(s->con, pos->x, pos->y,
131                   msg->request != VHOST_USER_GPU_CURSOR_POS_HIDE);
132 }
133 
134 static void
135 vhost_user_gpu_send_msg(VhostUserGPU *g, const VhostUserGpuMsg *msg)
136 {
137     qemu_chr_fe_write(&g->vhost_chr, (uint8_t *)msg,
138                       VHOST_USER_GPU_HDR_SIZE + msg->size);
139 }
140 
141 static void
142 vhost_user_gpu_unblock(VhostUserGPU *g)
143 {
144     VhostUserGpuMsg msg = {
145         .request = VHOST_USER_GPU_DMABUF_UPDATE,
146         .flags = VHOST_USER_GPU_MSG_FLAG_REPLY,
147     };
148 
149     vhost_user_gpu_send_msg(g, &msg);
150 }
151 
152 static void
153 vhost_user_gpu_handle_display(VhostUserGPU *g, VhostUserGpuMsg *msg)
154 {
155     QemuConsole *con = NULL;
156     struct virtio_gpu_scanout *s;
157 
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),
164         };
165 
166         vhost_user_gpu_send_msg(g, &reply);
167         break;
168     }
169     case VHOST_USER_GPU_SET_PROTOCOL_FEATURES: {
170         break;
171     }
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),
178         };
179 
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);
185         break;
186     }
187     case VHOST_USER_GPU_SCANOUT: {
188         VhostUserGpuScanout *m = &msg->payload.scanout;
189 
190         if (m->scanout_id >= g->parent_obj.conf.max_outputs) {
191             return;
192         }
193 
194         g->parent_obj.enable = 1;
195         s = &g->parent_obj.scanout[m->scanout_id];
196         con = s->con;
197 
198         if (m->width == 0) {
199             dpy_gfx_replace_surface(con, NULL);
200         } else {
201             s->ds = qemu_create_displaysurface(m->width, m->height);
202             /* replace surface on next update */
203         }
204 
205         break;
206     }
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);
210         QemuDmaBuf *dmabuf;
211 
212         if (m->scanout_id >= g->parent_obj.conf.max_outputs) {
213             error_report("invalid scanout: %d", m->scanout_id);
214             if (fd >= 0) {
215                 close(fd);
216             }
217             break;
218         }
219 
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) {
224             close(dmabuf->fd);
225             dmabuf->fd = -1;
226         }
227         dpy_gl_release_dmabuf(con, dmabuf);
228         if (fd == -1) {
229             dpy_gl_scanout_disable(con);
230             break;
231         }
232         *dmabuf = (QemuDmaBuf) {
233             .fd = fd,
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,
239         };
240         dpy_gl_scanout_dmabuf(con, dmabuf);
241         break;
242     }
243     case VHOST_USER_GPU_DMABUF_UPDATE: {
244         VhostUserGpuUpdate *m = &msg->payload.update;
245 
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);
250             break;
251         }
252 
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);
257             break;
258         }
259         g->backend_blocked = true;
260         dpy_gl_update(con, m->x, m->y, m->width, m->height);
261         break;
262     }
263     case VHOST_USER_GPU_UPDATE: {
264         VhostUserGpuUpdate *m = &msg->payload.update;
265 
266         if (m->scanout_id >= g->parent_obj.conf.max_outputs) {
267             break;
268         }
269         s = &g->parent_obj.scanout[m->scanout_id];
270         con = s->con;
271         pixman_image_t *image =
272             pixman_image_create_bits(PIXMAN_x8r8g8b8,
273                                      m->width,
274                                      m->height,
275                                      (uint32_t *)m->data,
276                                      m->width * 4);
277 
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);
281 
282         pixman_image_unref(image);
283         if (qemu_console_surface(con) != s->ds) {
284             dpy_gfx_replace_surface(con, s->ds);
285         } else {
286             dpy_gfx_update(con, m->x, m->y, m->width, m->height);
287         }
288         break;
289     }
290     default:
291         g_warning("unhandled message %d %d", msg->request, msg->size);
292     }
293 
294     if (con && qemu_console_is_gl_blocked(con)) {
295         vhost_user_gpu_update_blocked(g, true);
296     }
297 }
298 
299 static void
300 vhost_user_gpu_chr_read(void *opaque)
301 {
302     VhostUserGPU *g = opaque;
303     VhostUserGpuMsg *msg = NULL;
304     VhostUserGpuRequest request;
305     uint32_t size, flags;
306     int r;
307 
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);
312         goto end;
313     }
314 
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");
319         goto end;
320     }
321 
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");
326         goto end;
327     }
328 
329     msg = g_malloc(VHOST_USER_GPU_HDR_SIZE + size);
330 
331     r = qemu_chr_fe_read_all(&g->vhost_chr,
332                              (uint8_t *)&msg->payload, size);
333     if (r != size) {
334         error_report("failed to read msg payload %d != %d", r, size);
335         goto end;
336     }
337 
338     msg->request = request;
339     msg->flags = size;
340     msg->size = size;
341 
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);
346     } else {
347         vhost_user_gpu_handle_display(g, msg);
348     }
349 
350 end:
351     g_free(msg);
352 }
353 
354 static void
355 vhost_user_gpu_update_blocked(VhostUserGPU *g, bool blocked)
356 {
357     qemu_set_fd_handler(g->vhost_gpu_fd,
358                         blocked ? NULL : vhost_user_gpu_chr_read, NULL, g);
359 }
360 
361 static void
362 vhost_user_gpu_gl_flushed(VirtIOGPUBase *b)
363 {
364     VhostUserGPU *g = VHOST_USER_GPU(b);
365 
366     if (g->backend_blocked) {
367         vhost_user_gpu_unblock(g);
368         g->backend_blocked = false;
369     }
370 
371     vhost_user_gpu_update_blocked(g, false);
372 }
373 
374 static bool
375 vhost_user_gpu_do_set_socket(VhostUserGPU *g, Error **errp)
376 {
377     Chardev *chr;
378     int sv[2];
379 
380     if (qemu_socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
381         error_setg_errno(errp, errno, "socketpair() failed");
382         return false;
383     }
384 
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");
388         goto err;
389     }
390     if (!qemu_chr_fe_init(&g->vhost_chr, chr, errp)) {
391         goto err;
392     }
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);
396         goto err;
397     }
398 
399     g->vhost_gpu_fd = sv[0];
400     vhost_user_gpu_update_blocked(g, false);
401     close(sv[1]);
402     return true;
403 
404 err:
405     close(sv[0]);
406     close(sv[1]);
407     if (chr) {
408         object_unref(OBJECT(chr));
409     }
410     return false;
411 }
412 
413 static void
414 vhost_user_gpu_get_config(VirtIODevice *vdev, uint8_t *config_data)
415 {
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;
421     int ret;
422 
423     memset(config_data, 0, sizeof(struct virtio_gpu_config));
424 
425     ret = vhost_dev_get_config(&g->vhost->dev,
426                                config_data, sizeof(struct virtio_gpu_config),
427                                &local_err);
428     if (ret) {
429         error_report_err(local_err);
430         return;
431     }
432 
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;
437 }
438 
439 static void
440 vhost_user_gpu_set_config(VirtIODevice *vdev,
441                           const uint8_t *config_data)
442 {
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;
447     int ret;
448 
449     if (vgconfig->events_clear) {
450         b->virtio_config.events_read &= ~vgconfig->events_clear;
451     }
452 
453     ret = vhost_dev_set_config(&g->vhost->dev, config_data,
454                                0, sizeof(struct virtio_gpu_config),
455                                VHOST_SET_CONFIG_TYPE_MASTER);
456     if (ret) {
457         error_report("vhost-user-gpu: set device config space failed");
458         return;
459     }
460 }
461 
462 static void
463 vhost_user_gpu_set_status(VirtIODevice *vdev, uint8_t val)
464 {
465     VhostUserGPU *g = VHOST_USER_GPU(vdev);
466     Error *err = NULL;
467 
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);
471             return;
472         }
473         vhost_user_backend_start(g->vhost);
474     } else {
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;
480         }
481         vhost_user_backend_stop(g->vhost);
482     }
483 }
484 
485 static bool
486 vhost_user_gpu_guest_notifier_pending(VirtIODevice *vdev, int idx)
487 {
488     VhostUserGPU *g = VHOST_USER_GPU(vdev);
489 
490     /*
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
494      */
495 
496     if (idx == VIRTIO_CONFIG_IRQ_IDX) {
497         return false;
498     }
499     return vhost_virtqueue_pending(&g->vhost->dev, idx);
500 }
501 
502 static void
503 vhost_user_gpu_guest_notifier_mask(VirtIODevice *vdev, int idx, bool mask)
504 {
505     VhostUserGPU *g = VHOST_USER_GPU(vdev);
506 
507     /*
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
511      */
512 
513     if (idx == VIRTIO_CONFIG_IRQ_IDX) {
514         return;
515     }
516     vhost_virtqueue_mask(&g->vhost->dev, vdev, idx, mask);
517 }
518 
519 static void
520 vhost_user_gpu_instance_init(Object *obj)
521 {
522     VhostUserGPU *g = VHOST_USER_GPU(obj);
523 
524     g->vhost = VHOST_USER_BACKEND(object_new(TYPE_VHOST_USER_BACKEND));
525     object_property_add_alias(obj, "chardev",
526                               OBJECT(g->vhost), "chardev");
527 }
528 
529 static void
530 vhost_user_gpu_instance_finalize(Object *obj)
531 {
532     VhostUserGPU *g = VHOST_USER_GPU(obj);
533 
534     object_unref(OBJECT(g->vhost));
535 }
536 
537 static void
538 vhost_user_gpu_reset(VirtIODevice *vdev)
539 {
540     VhostUserGPU *g = VHOST_USER_GPU(vdev);
541 
542     virtio_gpu_base_reset(VIRTIO_GPU_BASE(vdev));
543 
544     vhost_user_backend_stop(g->vhost);
545 }
546 
547 static int
548 vhost_user_gpu_config_change(struct vhost_dev *dev)
549 {
550     error_report("vhost-user-gpu: unhandled backend config change");
551     return -1;
552 }
553 
554 static const VhostDevConfigOps config_ops = {
555     .vhost_dev_config_notifier = vhost_user_gpu_config_change,
556 };
557 
558 static void
559 vhost_user_gpu_device_realize(DeviceState *qdev, Error **errp)
560 {
561     VhostUserGPU *g = VHOST_USER_GPU(qdev);
562     VirtIODevice *vdev = VIRTIO_DEVICE(g);
563 
564     vhost_dev_set_config_notifier(&g->vhost->dev, &config_ops);
565     if (vhost_user_backend_dev_init(g->vhost, vdev, 2, errp) < 0) {
566         return;
567     }
568 
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;
573     }
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;
576     } else {
577         error_report("EDID requested but the backend doesn't support it.");
578         g->parent_obj.conf.flags &= ~(1 << VIRTIO_GPU_FLAG_EDID_ENABLED);
579     }
580 
581     if (!virtio_gpu_base_device_realize(qdev, NULL, NULL, errp)) {
582         return;
583     }
584 
585     g->vhost_gpu_fd = -1;
586 }
587 
588 static struct vhost_dev *vhost_user_gpu_get_vhost(VirtIODevice *vdev)
589 {
590     VhostUserGPU *g = VHOST_USER_GPU(vdev);
591     return &g->vhost->dev;
592 }
593 
594 static Property vhost_user_gpu_properties[] = {
595     VIRTIO_GPU_BASE_PROPERTIES(VhostUserGPU, parent_obj.conf),
596     DEFINE_PROP_END_OF_LIST(),
597 };
598 
599 static void
600 vhost_user_gpu_class_init(ObjectClass *klass, void *data)
601 {
602     DeviceClass *dc = DEVICE_CLASS(klass);
603     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
604     VirtIOGPUBaseClass *vgc = VIRTIO_GPU_BASE_CLASS(klass);
605 
606     vgc->gl_flushed = vhost_user_gpu_gl_flushed;
607 
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;
616 
617     device_class_set_props(dc, vhost_user_gpu_properties);
618 }
619 
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,
627 };
628 module_obj(TYPE_VHOST_USER_GPU);
629 module_kconfig(VHOST_USER_GPU);
630 
631 static void vhost_user_gpu_register_types(void)
632 {
633     type_register_static(&vhost_user_gpu_info);
634 }
635 
636 type_init(vhost_user_gpu_register_types)
637