xref: /openbmc/qemu/hw/display/vhost-user-gpu.c (revision bb5101aadc1675790983c7911092dd9abeec4651)
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     VHOST_USER_GPU_GET_EDID,
35     VHOST_USER_GPU_DMABUF_SCANOUT2,
36 } VhostUserGpuRequest;
37 
38 typedef struct VhostUserGpuDisplayInfoReply {
39     struct virtio_gpu_resp_display_info info;
40 } VhostUserGpuDisplayInfoReply;
41 
42 typedef struct VhostUserGpuCursorPos {
43     uint32_t scanout_id;
44     uint32_t x;
45     uint32_t y;
46 } QEMU_PACKED VhostUserGpuCursorPos;
47 
48 typedef struct VhostUserGpuCursorUpdate {
49     VhostUserGpuCursorPos pos;
50     uint32_t hot_x;
51     uint32_t hot_y;
52     uint32_t data[64 * 64];
53 } QEMU_PACKED VhostUserGpuCursorUpdate;
54 
55 typedef struct VhostUserGpuScanout {
56     uint32_t scanout_id;
57     uint32_t width;
58     uint32_t height;
59 } QEMU_PACKED VhostUserGpuScanout;
60 
61 typedef struct VhostUserGpuUpdate {
62     uint32_t scanout_id;
63     uint32_t x;
64     uint32_t y;
65     uint32_t width;
66     uint32_t height;
67     uint8_t data[];
68 } QEMU_PACKED VhostUserGpuUpdate;
69 
70 typedef struct VhostUserGpuDMABUFScanout {
71     uint32_t scanout_id;
72     uint32_t x;
73     uint32_t y;
74     uint32_t width;
75     uint32_t height;
76     uint32_t fd_width;
77     uint32_t fd_height;
78     uint32_t fd_stride;
79     uint32_t fd_flags;
80     int fd_drm_fourcc;
81 } QEMU_PACKED VhostUserGpuDMABUFScanout;
82 
83 typedef struct VhostUserGpuDMABUFScanout2 {
84     struct VhostUserGpuDMABUFScanout dmabuf_scanout;
85     uint64_t modifier;
86 } QEMU_PACKED VhostUserGpuDMABUFScanout2;
87 
88 typedef struct VhostUserGpuEdidRequest {
89     uint32_t scanout_id;
90 } QEMU_PACKED VhostUserGpuEdidRequest;
91 
92 typedef struct VhostUserGpuMsg {
93     uint32_t request; /* VhostUserGpuRequest */
94     uint32_t flags;
95     uint32_t size; /* the following payload size */
96     union {
97         VhostUserGpuCursorPos cursor_pos;
98         VhostUserGpuCursorUpdate cursor_update;
99         VhostUserGpuScanout scanout;
100         VhostUserGpuUpdate update;
101         VhostUserGpuDMABUFScanout dmabuf_scanout;
102         VhostUserGpuDMABUFScanout2 dmabuf_scanout2;
103         VhostUserGpuEdidRequest edid_req;
104         struct virtio_gpu_resp_edid resp_edid;
105         struct virtio_gpu_resp_display_info display_info;
106         uint64_t u64;
107     } payload;
108 } QEMU_PACKED VhostUserGpuMsg;
109 
110 static VhostUserGpuMsg m __attribute__ ((unused));
111 #define VHOST_USER_GPU_HDR_SIZE \
112     (sizeof(m.request) + sizeof(m.size) + sizeof(m.flags))
113 
114 #define VHOST_USER_GPU_MSG_FLAG_REPLY 0x4
115 
116 #define VHOST_USER_GPU_PROTOCOL_F_EDID 0
117 #define VHOST_USER_GPU_PROTOCOL_F_DMABUF2 1
118 
119 static void vhost_user_gpu_update_blocked(VhostUserGPU *g, bool blocked);
120 
121 static void
122 vhost_user_gpu_handle_cursor(VhostUserGPU *g, VhostUserGpuMsg *msg)
123 {
124     VhostUserGpuCursorPos *pos = &msg->payload.cursor_pos;
125     struct virtio_gpu_scanout *s;
126 
127     if (pos->scanout_id >= g->parent_obj.conf.max_outputs) {
128         return;
129     }
130     s = &g->parent_obj.scanout[pos->scanout_id];
131 
132     if (msg->request == VHOST_USER_GPU_CURSOR_UPDATE) {
133         VhostUserGpuCursorUpdate *up = &msg->payload.cursor_update;
134         if (!s->current_cursor) {
135             s->current_cursor = cursor_alloc(64, 64);
136         }
137 
138         s->current_cursor->hot_x = up->hot_x;
139         s->current_cursor->hot_y = up->hot_y;
140 
141         memcpy(s->current_cursor->data, up->data,
142                64 * 64 * sizeof(uint32_t));
143 
144         dpy_cursor_define(s->con, s->current_cursor);
145     }
146 
147     dpy_mouse_set(s->con, pos->x, pos->y,
148                   msg->request != VHOST_USER_GPU_CURSOR_POS_HIDE);
149 }
150 
151 static void
152 vhost_user_gpu_send_msg(VhostUserGPU *g, const VhostUserGpuMsg *msg)
153 {
154     qemu_chr_fe_write(&g->vhost_chr, (uint8_t *)msg,
155                       VHOST_USER_GPU_HDR_SIZE + msg->size);
156 }
157 
158 static void
159 vhost_user_gpu_unblock(VhostUserGPU *g)
160 {
161     VhostUserGpuMsg msg = {
162         .request = VHOST_USER_GPU_DMABUF_UPDATE,
163         .flags = VHOST_USER_GPU_MSG_FLAG_REPLY,
164     };
165 
166     vhost_user_gpu_send_msg(g, &msg);
167 }
168 
169 static void
170 vhost_user_gpu_handle_display(VhostUserGPU *g, VhostUserGpuMsg *msg)
171 {
172     QemuConsole *con = NULL;
173     struct virtio_gpu_scanout *s;
174 
175     switch (msg->request) {
176     case VHOST_USER_GPU_GET_PROTOCOL_FEATURES: {
177         VhostUserGpuMsg reply = {
178             .request = msg->request,
179             .flags = VHOST_USER_GPU_MSG_FLAG_REPLY,
180             .size = sizeof(uint64_t),
181             .payload = {
182                 .u64 = (1 << VHOST_USER_GPU_PROTOCOL_F_EDID) |
183                        (1 << VHOST_USER_GPU_PROTOCOL_F_DMABUF2)
184             }
185         };
186 
187         vhost_user_gpu_send_msg(g, &reply);
188         break;
189     }
190     case VHOST_USER_GPU_SET_PROTOCOL_FEATURES: {
191         break;
192     }
193     case VHOST_USER_GPU_GET_DISPLAY_INFO: {
194         struct virtio_gpu_resp_display_info display_info = { {} };
195         VhostUserGpuMsg reply = {
196             .request = msg->request,
197             .flags = VHOST_USER_GPU_MSG_FLAG_REPLY,
198             .size = sizeof(struct virtio_gpu_resp_display_info),
199         };
200 
201         display_info.hdr.type = VIRTIO_GPU_RESP_OK_DISPLAY_INFO;
202         virtio_gpu_base_fill_display_info(VIRTIO_GPU_BASE(g), &display_info);
203         memcpy(&reply.payload.display_info, &display_info,
204                sizeof(display_info));
205         vhost_user_gpu_send_msg(g, &reply);
206         break;
207     }
208     case VHOST_USER_GPU_GET_EDID: {
209         VhostUserGpuEdidRequest *m = &msg->payload.edid_req;
210         struct virtio_gpu_resp_edid resp = { {} };
211         VhostUserGpuMsg reply = {
212             .request = msg->request,
213             .flags = VHOST_USER_GPU_MSG_FLAG_REPLY,
214             .size = sizeof(reply.payload.resp_edid),
215         };
216 
217         if (m->scanout_id >= g->parent_obj.conf.max_outputs) {
218             error_report("invalid scanout: %d", m->scanout_id);
219             break;
220         }
221 
222         resp.hdr.type = VIRTIO_GPU_RESP_OK_EDID;
223         virtio_gpu_base_generate_edid(VIRTIO_GPU_BASE(g), m->scanout_id, &resp);
224         memcpy(&reply.payload.resp_edid, &resp, sizeof(resp));
225         vhost_user_gpu_send_msg(g, &reply);
226         break;
227     }
228     case VHOST_USER_GPU_SCANOUT: {
229         VhostUserGpuScanout *m = &msg->payload.scanout;
230 
231         if (m->scanout_id >= g->parent_obj.conf.max_outputs) {
232             return;
233         }
234 
235         g->parent_obj.enable = 1;
236         s = &g->parent_obj.scanout[m->scanout_id];
237         con = s->con;
238 
239         if (m->width == 0) {
240             dpy_gfx_replace_surface(con, NULL);
241         } else {
242             s->ds = qemu_create_displaysurface(m->width, m->height);
243             /* replace surface on next update */
244         }
245 
246         break;
247     }
248     case VHOST_USER_GPU_DMABUF_SCANOUT2:
249     case VHOST_USER_GPU_DMABUF_SCANOUT: {
250         VhostUserGpuDMABUFScanout *m = &msg->payload.dmabuf_scanout;
251         int fd = qemu_chr_fe_get_msgfd(&g->vhost_chr);
252         uint32_t offset = 0;
253         uint32_t stride = m->fd_stride;
254         uint64_t modifier = 0;
255         QemuDmaBuf *dmabuf;
256 
257         if (m->scanout_id >= g->parent_obj.conf.max_outputs) {
258             error_report("invalid scanout: %d", m->scanout_id);
259             if (fd >= 0) {
260                 close(fd);
261             }
262             break;
263         }
264 
265         g->parent_obj.enable = 1;
266         con = g->parent_obj.scanout[m->scanout_id].con;
267         dmabuf = g->dmabuf[m->scanout_id];
268 
269         if (dmabuf) {
270             qemu_dmabuf_close(dmabuf);
271             dpy_gl_release_dmabuf(con, dmabuf);
272             g_clear_pointer(&dmabuf, qemu_dmabuf_free);
273         }
274 
275         if (fd == -1) {
276             dpy_gl_scanout_disable(con);
277             g->dmabuf[m->scanout_id] = NULL;
278             break;
279         }
280 
281         if (msg->request == VHOST_USER_GPU_DMABUF_SCANOUT2) {
282             VhostUserGpuDMABUFScanout2 *m2 = &msg->payload.dmabuf_scanout2;
283             modifier = m2->modifier;
284         }
285 
286         dmabuf = qemu_dmabuf_new(m->width, m->height,
287                                  &offset, &stride, 0, 0,
288                                  m->fd_width, m->fd_height,
289                                  m->fd_drm_fourcc, modifier,
290                                  &fd, 1, false, m->fd_flags &
291                                  VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP);
292 
293         dpy_gl_scanout_dmabuf(con, dmabuf);
294         g->dmabuf[m->scanout_id] = dmabuf;
295         break;
296     }
297     case VHOST_USER_GPU_DMABUF_UPDATE: {
298         VhostUserGpuUpdate *m = &msg->payload.update;
299 
300         if (m->scanout_id >= g->parent_obj.conf.max_outputs ||
301             !g->parent_obj.scanout[m->scanout_id].con) {
302             error_report("invalid scanout update: %d", m->scanout_id);
303             vhost_user_gpu_unblock(g);
304             break;
305         }
306 
307         con = g->parent_obj.scanout[m->scanout_id].con;
308         if (!console_has_gl(con)) {
309             error_report("console doesn't support GL!");
310             vhost_user_gpu_unblock(g);
311             break;
312         }
313         g->backend_blocked = true;
314         dpy_gl_update(con, m->x, m->y, m->width, m->height);
315         break;
316     }
317 #ifdef CONFIG_PIXMAN
318     case VHOST_USER_GPU_UPDATE: {
319         VhostUserGpuUpdate *m = &msg->payload.update;
320 
321         if (m->scanout_id >= g->parent_obj.conf.max_outputs) {
322             break;
323         }
324         s = &g->parent_obj.scanout[m->scanout_id];
325         con = s->con;
326         pixman_image_t *image =
327             pixman_image_create_bits(PIXMAN_x8r8g8b8,
328                                      m->width,
329                                      m->height,
330                                      (uint32_t *)m->data,
331                                      m->width * 4);
332 
333         pixman_image_composite(PIXMAN_OP_SRC,
334                                image, NULL, s->ds->image,
335                                0, 0, 0, 0, m->x, m->y, m->width, m->height);
336 
337         pixman_image_unref(image);
338         if (qemu_console_surface(con) != s->ds) {
339             dpy_gfx_replace_surface(con, s->ds);
340         } else {
341             dpy_gfx_update(con, m->x, m->y, m->width, m->height);
342         }
343         break;
344     }
345 #endif
346     default:
347         g_warning("unhandled message %d %d", msg->request, msg->size);
348     }
349 
350     if (con && qemu_console_is_gl_blocked(con)) {
351         vhost_user_gpu_update_blocked(g, true);
352     }
353 }
354 
355 static void
356 vhost_user_gpu_chr_read(void *opaque)
357 {
358     VhostUserGPU *g = opaque;
359     VhostUserGpuMsg *msg = NULL;
360     VhostUserGpuRequest request;
361     uint32_t size, flags;
362     int r;
363 
364     r = qemu_chr_fe_read_all(&g->vhost_chr,
365                              (uint8_t *)&request, sizeof(uint32_t));
366     if (r != sizeof(uint32_t)) {
367         error_report("failed to read msg header: %d, %d", r, errno);
368         goto end;
369     }
370 
371     r = qemu_chr_fe_read_all(&g->vhost_chr,
372                              (uint8_t *)&flags, sizeof(uint32_t));
373     if (r != sizeof(uint32_t)) {
374         error_report("failed to read msg flags");
375         goto end;
376     }
377 
378     r = qemu_chr_fe_read_all(&g->vhost_chr,
379                              (uint8_t *)&size, sizeof(uint32_t));
380     if (r != sizeof(uint32_t)) {
381         error_report("failed to read msg size");
382         goto end;
383     }
384 
385     msg = g_malloc(VHOST_USER_GPU_HDR_SIZE + size);
386 
387     r = qemu_chr_fe_read_all(&g->vhost_chr,
388                              (uint8_t *)&msg->payload, size);
389     if (r != size) {
390         error_report("failed to read msg payload %d != %d", r, size);
391         goto end;
392     }
393 
394     msg->request = request;
395     msg->flags = flags;
396     msg->size = size;
397 
398     if (request == VHOST_USER_GPU_CURSOR_UPDATE ||
399         request == VHOST_USER_GPU_CURSOR_POS ||
400         request == VHOST_USER_GPU_CURSOR_POS_HIDE) {
401         vhost_user_gpu_handle_cursor(g, msg);
402     } else {
403         vhost_user_gpu_handle_display(g, msg);
404     }
405 
406 end:
407     g_free(msg);
408 }
409 
410 static void
411 vhost_user_gpu_update_blocked(VhostUserGPU *g, bool blocked)
412 {
413     qemu_set_fd_handler(g->vhost_gpu_fd,
414                         blocked ? NULL : vhost_user_gpu_chr_read, NULL, g);
415 }
416 
417 static void
418 vhost_user_gpu_gl_flushed(VirtIOGPUBase *b)
419 {
420     VhostUserGPU *g = VHOST_USER_GPU(b);
421 
422     if (g->backend_blocked) {
423         vhost_user_gpu_unblock(g);
424         g->backend_blocked = false;
425     }
426 
427     vhost_user_gpu_update_blocked(g, false);
428 }
429 
430 static bool
431 vhost_user_gpu_do_set_socket(VhostUserGPU *g, Error **errp)
432 {
433     Chardev *chr;
434     int sv[2];
435 
436     if (qemu_socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
437         error_setg_errno(errp, errno, "socketpair() failed");
438         return false;
439     }
440 
441     chr = CHARDEV(object_new(TYPE_CHARDEV_SOCKET));
442     if (!chr || qemu_chr_add_client(chr, sv[0]) == -1) {
443         error_setg(errp, "Failed to make socket chardev");
444         goto err;
445     }
446     if (!qemu_chr_fe_init(&g->vhost_chr, chr, errp)) {
447         goto err;
448     }
449     if (vhost_user_gpu_set_socket(&g->vhost->dev, sv[1]) < 0) {
450         error_setg(errp, "Failed to set vhost-user-gpu socket");
451         qemu_chr_fe_deinit(&g->vhost_chr, false);
452         goto err;
453     }
454 
455     g->vhost_gpu_fd = sv[0];
456     vhost_user_gpu_update_blocked(g, false);
457     close(sv[1]);
458     return true;
459 
460 err:
461     close(sv[0]);
462     close(sv[1]);
463     if (chr) {
464         object_unref(OBJECT(chr));
465     }
466     return false;
467 }
468 
469 static void
470 vhost_user_gpu_get_config(VirtIODevice *vdev, uint8_t *config_data)
471 {
472     VhostUserGPU *g = VHOST_USER_GPU(vdev);
473     VirtIOGPUBase *b = VIRTIO_GPU_BASE(vdev);
474     struct virtio_gpu_config *vgconfig =
475         (struct virtio_gpu_config *)config_data;
476     Error *local_err = NULL;
477     int ret;
478 
479     memset(config_data, 0, sizeof(struct virtio_gpu_config));
480 
481     ret = vhost_dev_get_config(&g->vhost->dev,
482                                config_data, sizeof(struct virtio_gpu_config),
483                                &local_err);
484     if (ret) {
485         error_report_err(local_err);
486         return;
487     }
488 
489     /* those fields are managed by qemu */
490     vgconfig->num_scanouts = b->virtio_config.num_scanouts;
491     vgconfig->events_read = b->virtio_config.events_read;
492     vgconfig->events_clear = b->virtio_config.events_clear;
493 }
494 
495 static void
496 vhost_user_gpu_set_config(VirtIODevice *vdev,
497                           const uint8_t *config_data)
498 {
499     VhostUserGPU *g = VHOST_USER_GPU(vdev);
500     VirtIOGPUBase *b = VIRTIO_GPU_BASE(vdev);
501     const struct virtio_gpu_config *vgconfig =
502         (const struct virtio_gpu_config *)config_data;
503     int ret;
504 
505     if (vgconfig->events_clear) {
506         b->virtio_config.events_read &= ~vgconfig->events_clear;
507     }
508 
509     ret = vhost_dev_set_config(&g->vhost->dev, config_data,
510                                0, sizeof(struct virtio_gpu_config),
511                                VHOST_SET_CONFIG_TYPE_FRONTEND);
512     if (ret) {
513         error_report("vhost-user-gpu: set device config space failed");
514         return;
515     }
516 }
517 
518 static void
519 vhost_user_gpu_set_status(VirtIODevice *vdev, uint8_t val)
520 {
521     VhostUserGPU *g = VHOST_USER_GPU(vdev);
522     Error *err = NULL;
523 
524     if (val & VIRTIO_CONFIG_S_DRIVER_OK && vdev->vm_running) {
525         if (!vhost_user_gpu_do_set_socket(g, &err)) {
526             error_report_err(err);
527             return;
528         }
529         vhost_user_backend_start(g->vhost);
530     } else {
531         /* unblock any wait and stop processing */
532         if (g->vhost_gpu_fd != -1) {
533             vhost_user_gpu_update_blocked(g, true);
534             qemu_chr_fe_deinit(&g->vhost_chr, true);
535             g->vhost_gpu_fd = -1;
536         }
537         vhost_user_backend_stop(g->vhost);
538     }
539 }
540 
541 static bool
542 vhost_user_gpu_guest_notifier_pending(VirtIODevice *vdev, int idx)
543 {
544     VhostUserGPU *g = VHOST_USER_GPU(vdev);
545 
546     /*
547      * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
548      * as the macro of configure interrupt's IDX, If this driver does not
549      * support, the function will return
550      */
551 
552     if (idx == VIRTIO_CONFIG_IRQ_IDX) {
553         return false;
554     }
555     return vhost_virtqueue_pending(&g->vhost->dev, idx);
556 }
557 
558 static void
559 vhost_user_gpu_guest_notifier_mask(VirtIODevice *vdev, int idx, bool mask)
560 {
561     VhostUserGPU *g = VHOST_USER_GPU(vdev);
562 
563     /*
564      * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
565      * as the macro of configure interrupt's IDX, If this driver does not
566      * support, the function will return
567      */
568 
569     if (idx == VIRTIO_CONFIG_IRQ_IDX) {
570         return;
571     }
572     vhost_virtqueue_mask(&g->vhost->dev, vdev, idx, mask);
573 }
574 
575 static void
576 vhost_user_gpu_instance_init(Object *obj)
577 {
578     VhostUserGPU *g = VHOST_USER_GPU(obj);
579 
580     g->vhost = VHOST_USER_BACKEND(object_new(TYPE_VHOST_USER_BACKEND));
581     object_property_add_alias(obj, "chardev",
582                               OBJECT(g->vhost), "chardev");
583 }
584 
585 static void
586 vhost_user_gpu_instance_finalize(Object *obj)
587 {
588     VhostUserGPU *g = VHOST_USER_GPU(obj);
589 
590     object_unref(OBJECT(g->vhost));
591 }
592 
593 static void
594 vhost_user_gpu_reset(VirtIODevice *vdev)
595 {
596     VhostUserGPU *g = VHOST_USER_GPU(vdev);
597 
598     virtio_gpu_base_reset(VIRTIO_GPU_BASE(vdev));
599 
600     vhost_user_backend_stop(g->vhost);
601 }
602 
603 static int
604 vhost_user_gpu_config_change(struct vhost_dev *dev)
605 {
606     error_report("vhost-user-gpu: unhandled backend config change");
607     return -1;
608 }
609 
610 static const VhostDevConfigOps config_ops = {
611     .vhost_dev_config_notifier = vhost_user_gpu_config_change,
612 };
613 
614 static void
615 vhost_user_gpu_device_realize(DeviceState *qdev, Error **errp)
616 {
617     VhostUserGPU *g = VHOST_USER_GPU(qdev);
618     VirtIODevice *vdev = VIRTIO_DEVICE(g);
619 
620     vhost_dev_set_config_notifier(&g->vhost->dev, &config_ops);
621     if (vhost_user_backend_dev_init(g->vhost, vdev, 2, errp) < 0) {
622         return;
623     }
624 
625     /* existing backend may send DMABUF, so let's add that requirement */
626     g->parent_obj.conf.flags |= 1 << VIRTIO_GPU_FLAG_DMABUF_ENABLED;
627     if (virtio_has_feature(g->vhost->dev.features, VIRTIO_GPU_F_VIRGL)) {
628         g->parent_obj.conf.flags |= 1 << VIRTIO_GPU_FLAG_VIRGL_ENABLED;
629     }
630     if (virtio_has_feature(g->vhost->dev.features, VIRTIO_GPU_F_EDID)) {
631         g->parent_obj.conf.flags |= 1 << VIRTIO_GPU_FLAG_EDID_ENABLED;
632     } else {
633         error_report("EDID requested but the backend doesn't support it.");
634         g->parent_obj.conf.flags &= ~(1 << VIRTIO_GPU_FLAG_EDID_ENABLED);
635     }
636     if (virtio_has_feature(g->vhost->dev.features,
637         VIRTIO_GPU_F_RESOURCE_UUID)) {
638         g->parent_obj.conf.flags |= 1 << VIRTIO_GPU_FLAG_RESOURCE_UUID_ENABLED;
639     }
640     if (virtio_has_feature(g->vhost->dev.features,
641         VIRTIO_GPU_F_RESOURCE_UUID)) {
642         g->parent_obj.conf.flags |= 1 << VIRTIO_GPU_FLAG_RESOURCE_UUID_ENABLED;
643     }
644 
645     if (!virtio_gpu_base_device_realize(qdev, NULL, NULL, errp)) {
646         return;
647     }
648 
649     g->vhost_gpu_fd = -1;
650 }
651 
652 static struct vhost_dev *vhost_user_gpu_get_vhost(VirtIODevice *vdev)
653 {
654     VhostUserGPU *g = VHOST_USER_GPU(vdev);
655     return g->vhost ? &g->vhost->dev : NULL;
656 }
657 
658 static const Property vhost_user_gpu_properties[] = {
659     VIRTIO_GPU_BASE_PROPERTIES(VhostUserGPU, parent_obj.conf),
660 };
661 
662 static void
663 vhost_user_gpu_class_init(ObjectClass *klass, const void *data)
664 {
665     DeviceClass *dc = DEVICE_CLASS(klass);
666     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
667     VirtIOGPUBaseClass *vgc = VIRTIO_GPU_BASE_CLASS(klass);
668 
669     vgc->gl_flushed = vhost_user_gpu_gl_flushed;
670 
671     vdc->realize = vhost_user_gpu_device_realize;
672     vdc->reset = vhost_user_gpu_reset;
673     vdc->set_status   = vhost_user_gpu_set_status;
674     vdc->guest_notifier_mask = vhost_user_gpu_guest_notifier_mask;
675     vdc->guest_notifier_pending = vhost_user_gpu_guest_notifier_pending;
676     vdc->get_config = vhost_user_gpu_get_config;
677     vdc->set_config = vhost_user_gpu_set_config;
678     vdc->get_vhost = vhost_user_gpu_get_vhost;
679 
680     device_class_set_props(dc, vhost_user_gpu_properties);
681 }
682 
683 static const TypeInfo vhost_user_gpu_info = {
684     .name = TYPE_VHOST_USER_GPU,
685     .parent = TYPE_VIRTIO_GPU_BASE,
686     .instance_size = sizeof(VhostUserGPU),
687     .instance_init = vhost_user_gpu_instance_init,
688     .instance_finalize = vhost_user_gpu_instance_finalize,
689     .class_init = vhost_user_gpu_class_init,
690 };
691 module_obj(TYPE_VHOST_USER_GPU);
692 module_kconfig(VHOST_USER_GPU);
693 
694 static void vhost_user_gpu_register_types(void)
695 {
696     type_register_static(&vhost_user_gpu_info);
697 }
698 
699 type_init(vhost_user_gpu_register_types)
700