yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
ddf19c
From fc2d0dfe60b14992a9b67e7a18394ba6365dc5ed Mon Sep 17 00:00:00 2001
ddf19c
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
ddf19c
Date: Wed, 18 Mar 2020 18:10:40 +0000
ddf19c
Subject: [PATCH 2/2] build-sys: do not make qemu-ga link with pixman
ddf19c
MIME-Version: 1.0
ddf19c
Content-Type: text/plain; charset=UTF-8
ddf19c
Content-Transfer-Encoding: 8bit
ddf19c
ddf19c
RH-Author: Marc-André Lureau <marcandre.lureau@redhat.com>
ddf19c
Message-id: <20200318181040.256425-1-marcandre.lureau@redhat.com>
ddf19c
Patchwork-id: 94381
ddf19c
O-Subject: [RHEL-AV-8.2.0 qemu-kvm PATCH] build-sys: do not make qemu-ga link with pixman
ddf19c
Bugzilla: 1811670
ddf19c
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
ddf19c
RH-Acked-by: John Snow <jsnow@redhat.com>
ddf19c
RH-Acked-by: Daniel P. Berrange <berrange@redhat.com>
ddf19c
ddf19c
Since commit d52c454aadcdae74506f315ebf8b58bb79a05573 ("contrib: add
ddf19c
vhost-user-gpu"), qemu-ga is linking with pixman.
ddf19c
ddf19c
This is because the Make-based build-system use a global namespace for
ddf19c
variables, and we rely on "main.o-libs" for different linking targets.
ddf19c
ddf19c
Note: this kind of variable clashing is hard to fix or prevent
ddf19c
currently.  meson should help, as declarations have a linear
ddf19c
dependency and doesn't rely so much on variables and clever tricks.
ddf19c
ddf19c
Note2: we have a lot of main.c (or other duplicated names!) in
ddf19c
tree. Imho, it would be annoying and a bad workaroud to rename all
ddf19c
those to avoid conflicts like I did here.
ddf19c
ddf19c
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1811670
ddf19c
ddf19c
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
ddf19c
Message-Id: <20200311160923.882474-1-marcandre.lureau@redhat.com>
ddf19c
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
ddf19c
ddf19c
BZ: https://bugzilla.redhat.com/show_bug.cgi?id=1811670
ddf19c
Brew: http://brewweb.devel.redhat.com/brew/taskinfo?taskID=27330493
ddf19c
ddf19c
(cherry picked from commit 5b42bc5ce9ab4a3171819feea5042931817211fd)
ddf19c
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
ddf19c
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
ddf19c
---
ddf19c
 contrib/vhost-user-gpu/Makefile.objs    |    6 +-
ddf19c
 contrib/vhost-user-gpu/main.c           | 1191 -------------------------------
ddf19c
 contrib/vhost-user-gpu/vhost-user-gpu.c | 1191 +++++++++++++++++++++++++++++++
ddf19c
 3 files changed, 1194 insertions(+), 1194 deletions(-)
ddf19c
 delete mode 100644 contrib/vhost-user-gpu/main.c
ddf19c
 create mode 100644 contrib/vhost-user-gpu/vhost-user-gpu.c
ddf19c
ddf19c
diff --git a/contrib/vhost-user-gpu/Makefile.objs b/contrib/vhost-user-gpu/Makefile.objs
ddf19c
index 6170c91..0929609 100644
ddf19c
--- a/contrib/vhost-user-gpu/Makefile.objs
ddf19c
+++ b/contrib/vhost-user-gpu/Makefile.objs
ddf19c
@@ -1,7 +1,7 @@
ddf19c
-vhost-user-gpu-obj-y = main.o virgl.o vugbm.o
ddf19c
+vhost-user-gpu-obj-y = vhost-user-gpu.o virgl.o vugbm.o
ddf19c
 
ddf19c
-main.o-cflags := $(PIXMAN_CFLAGS) $(GBM_CFLAGS)
ddf19c
-main.o-libs := $(PIXMAN_LIBS)
ddf19c
+vhost-user-gpu.o-cflags := $(PIXMAN_CFLAGS) $(GBM_CFLAGS)
ddf19c
+vhost-user-gpu.o-libs := $(PIXMAN_LIBS)
ddf19c
 
ddf19c
 virgl.o-cflags := $(VIRGL_CFLAGS) $(GBM_CFLAGS)
ddf19c
 virgl.o-libs := $(VIRGL_LIBS)
ddf19c
diff --git a/contrib/vhost-user-gpu/main.c b/contrib/vhost-user-gpu/main.c
ddf19c
deleted file mode 100644
ddf19c
index b45d201..0000000
ddf19c
--- a/contrib/vhost-user-gpu/main.c
ddf19c
+++ /dev/null
ddf19c
@@ -1,1191 +0,0 @@
ddf19c
-/*
ddf19c
- * Virtio vhost-user GPU Device
ddf19c
- *
ddf19c
- * Copyright Red Hat, Inc. 2013-2018
ddf19c
- *
ddf19c
- * Authors:
ddf19c
- *     Dave Airlie <airlied@redhat.com>
ddf19c
- *     Gerd Hoffmann <kraxel@redhat.com>
ddf19c
- *     Marc-André Lureau <marcandre.lureau@redhat.com>
ddf19c
- *
ddf19c
- * This work is licensed under the terms of the GNU GPL, version 2 or later.
ddf19c
- * See the COPYING file in the top-level directory.
ddf19c
- */
ddf19c
-#include "qemu/osdep.h"
ddf19c
-#include "qemu/drm.h"
ddf19c
-#include "qapi/error.h"
ddf19c
-#include "qemu/sockets.h"
ddf19c
-
ddf19c
-#include <pixman.h>
ddf19c
-#include <glib-unix.h>
ddf19c
-
ddf19c
-#include "vugpu.h"
ddf19c
-#include "hw/virtio/virtio-gpu-bswap.h"
ddf19c
-#include "hw/virtio/virtio-gpu-pixman.h"
ddf19c
-#include "virgl.h"
ddf19c
-#include "vugbm.h"
ddf19c
-
ddf19c
-enum {
ddf19c
-    VHOST_USER_GPU_MAX_QUEUES = 2,
ddf19c
-};
ddf19c
-
ddf19c
-struct virtio_gpu_simple_resource {
ddf19c
-    uint32_t resource_id;
ddf19c
-    uint32_t width;
ddf19c
-    uint32_t height;
ddf19c
-    uint32_t format;
ddf19c
-    struct iovec *iov;
ddf19c
-    unsigned int iov_cnt;
ddf19c
-    uint32_t scanout_bitmask;
ddf19c
-    pixman_image_t *image;
ddf19c
-    struct vugbm_buffer buffer;
ddf19c
-    QTAILQ_ENTRY(virtio_gpu_simple_resource) next;
ddf19c
-};
ddf19c
-
ddf19c
-static gboolean opt_print_caps;
ddf19c
-static int opt_fdnum = -1;
ddf19c
-static char *opt_socket_path;
ddf19c
-static char *opt_render_node;
ddf19c
-static gboolean opt_virgl;
ddf19c
-
ddf19c
-static void vg_handle_ctrl(VuDev *dev, int qidx);
ddf19c
-
ddf19c
-static const char *
ddf19c
-vg_cmd_to_string(int cmd)
ddf19c
-{
ddf19c
-#define CMD(cmd) [cmd] = #cmd
ddf19c
-    static const char *vg_cmd_str[] = {
ddf19c
-        CMD(VIRTIO_GPU_UNDEFINED),
ddf19c
-
ddf19c
-        /* 2d commands */
ddf19c
-        CMD(VIRTIO_GPU_CMD_GET_DISPLAY_INFO),
ddf19c
-        CMD(VIRTIO_GPU_CMD_RESOURCE_CREATE_2D),
ddf19c
-        CMD(VIRTIO_GPU_CMD_RESOURCE_UNREF),
ddf19c
-        CMD(VIRTIO_GPU_CMD_SET_SCANOUT),
ddf19c
-        CMD(VIRTIO_GPU_CMD_RESOURCE_FLUSH),
ddf19c
-        CMD(VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D),
ddf19c
-        CMD(VIRTIO_GPU_CMD_RESOURCE_ATTACH_BACKING),
ddf19c
-        CMD(VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING),
ddf19c
-        CMD(VIRTIO_GPU_CMD_GET_CAPSET_INFO),
ddf19c
-        CMD(VIRTIO_GPU_CMD_GET_CAPSET),
ddf19c
-
ddf19c
-        /* 3d commands */
ddf19c
-        CMD(VIRTIO_GPU_CMD_CTX_CREATE),
ddf19c
-        CMD(VIRTIO_GPU_CMD_CTX_DESTROY),
ddf19c
-        CMD(VIRTIO_GPU_CMD_CTX_ATTACH_RESOURCE),
ddf19c
-        CMD(VIRTIO_GPU_CMD_CTX_DETACH_RESOURCE),
ddf19c
-        CMD(VIRTIO_GPU_CMD_RESOURCE_CREATE_3D),
ddf19c
-        CMD(VIRTIO_GPU_CMD_TRANSFER_TO_HOST_3D),
ddf19c
-        CMD(VIRTIO_GPU_CMD_TRANSFER_FROM_HOST_3D),
ddf19c
-        CMD(VIRTIO_GPU_CMD_SUBMIT_3D),
ddf19c
-
ddf19c
-        /* cursor commands */
ddf19c
-        CMD(VIRTIO_GPU_CMD_UPDATE_CURSOR),
ddf19c
-        CMD(VIRTIO_GPU_CMD_MOVE_CURSOR),
ddf19c
-    };
ddf19c
-#undef REQ
ddf19c
-
ddf19c
-    if (cmd >= 0 && cmd < G_N_ELEMENTS(vg_cmd_str)) {
ddf19c
-        return vg_cmd_str[cmd];
ddf19c
-    } else {
ddf19c
-        return "unknown";
ddf19c
-    }
ddf19c
-}
ddf19c
-
ddf19c
-static int
ddf19c
-vg_sock_fd_read(int sock, void *buf, ssize_t buflen)
ddf19c
-{
ddf19c
-    int ret;
ddf19c
-
ddf19c
-    do {
ddf19c
-        ret = read(sock, buf, buflen);
ddf19c
-    } while (ret < 0 && (errno == EINTR || errno == EAGAIN));
ddf19c
-
ddf19c
-    g_warn_if_fail(ret == buflen);
ddf19c
-    return ret;
ddf19c
-}
ddf19c
-
ddf19c
-static void
ddf19c
-vg_sock_fd_close(VuGpu *g)
ddf19c
-{
ddf19c
-    if (g->sock_fd >= 0) {
ddf19c
-        close(g->sock_fd);
ddf19c
-        g->sock_fd = -1;
ddf19c
-    }
ddf19c
-}
ddf19c
-
ddf19c
-static gboolean
ddf19c
-source_wait_cb(gint fd, GIOCondition condition, gpointer user_data)
ddf19c
-{
ddf19c
-    VuGpu *g = user_data;
ddf19c
-
ddf19c
-    if (!vg_recv_msg(g, VHOST_USER_GPU_DMABUF_UPDATE, 0, NULL)) {
ddf19c
-        return G_SOURCE_CONTINUE;
ddf19c
-    }
ddf19c
-
ddf19c
-    /* resume */
ddf19c
-    g->wait_ok = 0;
ddf19c
-    vg_handle_ctrl(&g->dev.parent, 0);
ddf19c
-
ddf19c
-    return G_SOURCE_REMOVE;
ddf19c
-}
ddf19c
-
ddf19c
-void
ddf19c
-vg_wait_ok(VuGpu *g)
ddf19c
-{
ddf19c
-    assert(g->wait_ok == 0);
ddf19c
-    g->wait_ok = g_unix_fd_add(g->sock_fd, G_IO_IN | G_IO_HUP,
ddf19c
-                               source_wait_cb, g);
ddf19c
-}
ddf19c
-
ddf19c
-static int
ddf19c
-vg_sock_fd_write(int sock, const void *buf, ssize_t buflen, int fd)
ddf19c
-{
ddf19c
-    ssize_t ret;
ddf19c
-    struct iovec iov = {
ddf19c
-        .iov_base = (void *)buf,
ddf19c
-        .iov_len = buflen,
ddf19c
-    };
ddf19c
-    struct msghdr msg = {
ddf19c
-        .msg_iov = &iov,
ddf19c
-        .msg_iovlen = 1,
ddf19c
-    };
ddf19c
-    union {
ddf19c
-        struct cmsghdr cmsghdr;
ddf19c
-        char control[CMSG_SPACE(sizeof(int))];
ddf19c
-    } cmsgu;
ddf19c
-    struct cmsghdr *cmsg;
ddf19c
-
ddf19c
-    if (fd != -1) {
ddf19c
-        msg.msg_control = cmsgu.control;
ddf19c
-        msg.msg_controllen = sizeof(cmsgu.control);
ddf19c
-
ddf19c
-        cmsg = CMSG_FIRSTHDR(&msg;;
ddf19c
-        cmsg->cmsg_len = CMSG_LEN(sizeof(int));
ddf19c
-        cmsg->cmsg_level = SOL_SOCKET;
ddf19c
-        cmsg->cmsg_type = SCM_RIGHTS;
ddf19c
-
ddf19c
-        *((int *)CMSG_DATA(cmsg)) = fd;
ddf19c
-    }
ddf19c
-
ddf19c
-    do {
ddf19c
-        ret = sendmsg(sock, &msg, 0);
ddf19c
-    } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
ddf19c
-
ddf19c
-    g_warn_if_fail(ret == buflen);
ddf19c
-    return ret;
ddf19c
-}
ddf19c
-
ddf19c
-void
ddf19c
-vg_send_msg(VuGpu *vg, const VhostUserGpuMsg *msg, int fd)
ddf19c
-{
ddf19c
-    if (vg_sock_fd_write(vg->sock_fd, msg,
ddf19c
-                         VHOST_USER_GPU_HDR_SIZE + msg->size, fd) < 0) {
ddf19c
-        vg_sock_fd_close(vg);
ddf19c
-    }
ddf19c
-}
ddf19c
-
ddf19c
-bool
ddf19c
-vg_recv_msg(VuGpu *g, uint32_t expect_req, uint32_t expect_size,
ddf19c
-            gpointer payload)
ddf19c
-{
ddf19c
-    uint32_t req, flags, size;
ddf19c
-
ddf19c
-    if (vg_sock_fd_read(g->sock_fd, &req, sizeof(req)) < 0 ||
ddf19c
-        vg_sock_fd_read(g->sock_fd, &flags, sizeof(flags)) < 0 ||
ddf19c
-        vg_sock_fd_read(g->sock_fd, &size, sizeof(size)) < 0) {
ddf19c
-        goto err;
ddf19c
-    }
ddf19c
-
ddf19c
-    g_return_val_if_fail(req == expect_req, false);
ddf19c
-    g_return_val_if_fail(flags & VHOST_USER_GPU_MSG_FLAG_REPLY, false);
ddf19c
-    g_return_val_if_fail(size == expect_size, false);
ddf19c
-
ddf19c
-    if (size && vg_sock_fd_read(g->sock_fd, payload, size) != size) {
ddf19c
-        goto err;
ddf19c
-    }
ddf19c
-
ddf19c
-    return true;
ddf19c
-
ddf19c
-err:
ddf19c
-    vg_sock_fd_close(g);
ddf19c
-    return false;
ddf19c
-}
ddf19c
-
ddf19c
-static struct virtio_gpu_simple_resource *
ddf19c
-virtio_gpu_find_resource(VuGpu *g, uint32_t resource_id)
ddf19c
-{
ddf19c
-    struct virtio_gpu_simple_resource *res;
ddf19c
-
ddf19c
-    QTAILQ_FOREACH(res, &g->reslist, next) {
ddf19c
-        if (res->resource_id == resource_id) {
ddf19c
-            return res;
ddf19c
-        }
ddf19c
-    }
ddf19c
-    return NULL;
ddf19c
-}
ddf19c
-
ddf19c
-void
ddf19c
-vg_ctrl_response(VuGpu *g,
ddf19c
-                 struct virtio_gpu_ctrl_command *cmd,
ddf19c
-                 struct virtio_gpu_ctrl_hdr *resp,
ddf19c
-                 size_t resp_len)
ddf19c
-{
ddf19c
-    size_t s;
ddf19c
-
ddf19c
-    if (cmd->cmd_hdr.flags & VIRTIO_GPU_FLAG_FENCE) {
ddf19c
-        resp->flags |= VIRTIO_GPU_FLAG_FENCE;
ddf19c
-        resp->fence_id = cmd->cmd_hdr.fence_id;
ddf19c
-        resp->ctx_id = cmd->cmd_hdr.ctx_id;
ddf19c
-    }
ddf19c
-    virtio_gpu_ctrl_hdr_bswap(resp);
ddf19c
-    s = iov_from_buf(cmd->elem.in_sg, cmd->elem.in_num, 0, resp, resp_len);
ddf19c
-    if (s != resp_len) {
ddf19c
-        g_critical("%s: response size incorrect %zu vs %zu",
ddf19c
-                   __func__, s, resp_len);
ddf19c
-    }
ddf19c
-    vu_queue_push(&g->dev.parent, cmd->vq, &cmd->elem, s);
ddf19c
-    vu_queue_notify(&g->dev.parent, cmd->vq);
ddf19c
-    cmd->finished = true;
ddf19c
-}
ddf19c
-
ddf19c
-void
ddf19c
-vg_ctrl_response_nodata(VuGpu *g,
ddf19c
-                        struct virtio_gpu_ctrl_command *cmd,
ddf19c
-                        enum virtio_gpu_ctrl_type type)
ddf19c
-{
ddf19c
-    struct virtio_gpu_ctrl_hdr resp = {
ddf19c
-        .type = type,
ddf19c
-    };
ddf19c
-
ddf19c
-    vg_ctrl_response(g, cmd, &resp, sizeof(resp));
ddf19c
-}
ddf19c
-
ddf19c
-void
ddf19c
-vg_get_display_info(VuGpu *vg, struct virtio_gpu_ctrl_command *cmd)
ddf19c
-{
ddf19c
-    struct virtio_gpu_resp_display_info dpy_info = { {} };
ddf19c
-    VhostUserGpuMsg msg = {
ddf19c
-        .request = VHOST_USER_GPU_GET_DISPLAY_INFO,
ddf19c
-        .size = 0,
ddf19c
-    };
ddf19c
-
ddf19c
-    assert(vg->wait_ok == 0);
ddf19c
-
ddf19c
-    vg_send_msg(vg, &msg, -1);
ddf19c
-    if (!vg_recv_msg(vg, msg.request, sizeof(dpy_info), &dpy_info)) {
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    vg_ctrl_response(vg, cmd, &dpy_info.hdr, sizeof(dpy_info));
ddf19c
-}
ddf19c
-
ddf19c
-static void
ddf19c
-vg_resource_create_2d(VuGpu *g,
ddf19c
-                      struct virtio_gpu_ctrl_command *cmd)
ddf19c
-{
ddf19c
-    pixman_format_code_t pformat;
ddf19c
-    struct virtio_gpu_simple_resource *res;
ddf19c
-    struct virtio_gpu_resource_create_2d c2d;
ddf19c
-
ddf19c
-    VUGPU_FILL_CMD(c2d);
ddf19c
-    virtio_gpu_bswap_32(&c2d, sizeof(c2d));
ddf19c
-
ddf19c
-    if (c2d.resource_id == 0) {
ddf19c
-        g_critical("%s: resource id 0 is not allowed", __func__);
ddf19c
-        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    res = virtio_gpu_find_resource(g, c2d.resource_id);
ddf19c
-    if (res) {
ddf19c
-        g_critical("%s: resource already exists %d", __func__, c2d.resource_id);
ddf19c
-        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    res = g_new0(struct virtio_gpu_simple_resource, 1);
ddf19c
-    res->width = c2d.width;
ddf19c
-    res->height = c2d.height;
ddf19c
-    res->format = c2d.format;
ddf19c
-    res->resource_id = c2d.resource_id;
ddf19c
-
ddf19c
-    pformat = virtio_gpu_get_pixman_format(c2d.format);
ddf19c
-    if (!pformat) {
ddf19c
-        g_critical("%s: host couldn't handle guest format %d",
ddf19c
-                   __func__, c2d.format);
ddf19c
-        g_free(res);
ddf19c
-        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
ddf19c
-        return;
ddf19c
-    }
ddf19c
-    vugbm_buffer_create(&res->buffer, &g->gdev, c2d.width, c2d.height);
ddf19c
-    res->image = pixman_image_create_bits(pformat,
ddf19c
-                                          c2d.width,
ddf19c
-                                          c2d.height,
ddf19c
-                                          (uint32_t *)res->buffer.mmap,
ddf19c
-                                          res->buffer.stride);
ddf19c
-    if (!res->image) {
ddf19c
-        g_critical("%s: resource creation failed %d %d %d",
ddf19c
-                   __func__, c2d.resource_id, c2d.width, c2d.height);
ddf19c
-        g_free(res);
ddf19c
-        cmd->error = VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY;
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    QTAILQ_INSERT_HEAD(&g->reslist, res, next);
ddf19c
-}
ddf19c
-
ddf19c
-static void
ddf19c
-vg_disable_scanout(VuGpu *g, int scanout_id)
ddf19c
-{
ddf19c
-    struct virtio_gpu_scanout *scanout = &g->scanout[scanout_id];
ddf19c
-    struct virtio_gpu_simple_resource *res;
ddf19c
-
ddf19c
-    if (scanout->resource_id == 0) {
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    res = virtio_gpu_find_resource(g, scanout->resource_id);
ddf19c
-    if (res) {
ddf19c
-        res->scanout_bitmask &= ~(1 << scanout_id);
ddf19c
-    }
ddf19c
-
ddf19c
-    scanout->width = 0;
ddf19c
-    scanout->height = 0;
ddf19c
-
ddf19c
-    if (g->sock_fd >= 0) {
ddf19c
-        VhostUserGpuMsg msg = {
ddf19c
-            .request = VHOST_USER_GPU_SCANOUT,
ddf19c
-            .size = sizeof(VhostUserGpuScanout),
ddf19c
-            .payload.scanout.scanout_id = scanout_id,
ddf19c
-        };
ddf19c
-        vg_send_msg(g, &msg, -1);
ddf19c
-    }
ddf19c
-}
ddf19c
-
ddf19c
-static void
ddf19c
-vg_resource_destroy(VuGpu *g,
ddf19c
-                    struct virtio_gpu_simple_resource *res)
ddf19c
-{
ddf19c
-    int i;
ddf19c
-
ddf19c
-    if (res->scanout_bitmask) {
ddf19c
-        for (i = 0; i < VIRTIO_GPU_MAX_SCANOUTS; i++) {
ddf19c
-            if (res->scanout_bitmask & (1 << i)) {
ddf19c
-                vg_disable_scanout(g, i);
ddf19c
-            }
ddf19c
-        }
ddf19c
-    }
ddf19c
-
ddf19c
-    vugbm_buffer_destroy(&res->buffer);
ddf19c
-    pixman_image_unref(res->image);
ddf19c
-    QTAILQ_REMOVE(&g->reslist, res, next);
ddf19c
-    g_free(res);
ddf19c
-}
ddf19c
-
ddf19c
-static void
ddf19c
-vg_resource_unref(VuGpu *g,
ddf19c
-                  struct virtio_gpu_ctrl_command *cmd)
ddf19c
-{
ddf19c
-    struct virtio_gpu_simple_resource *res;
ddf19c
-    struct virtio_gpu_resource_unref unref;
ddf19c
-
ddf19c
-    VUGPU_FILL_CMD(unref);
ddf19c
-    virtio_gpu_bswap_32(&unref, sizeof(unref));
ddf19c
-
ddf19c
-    res = virtio_gpu_find_resource(g, unref.resource_id);
ddf19c
-    if (!res) {
ddf19c
-        g_critical("%s: illegal resource specified %d",
ddf19c
-                   __func__, unref.resource_id);
ddf19c
-        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
ddf19c
-        return;
ddf19c
-    }
ddf19c
-    vg_resource_destroy(g, res);
ddf19c
-}
ddf19c
-
ddf19c
-int
ddf19c
-vg_create_mapping_iov(VuGpu *g,
ddf19c
-                      struct virtio_gpu_resource_attach_backing *ab,
ddf19c
-                      struct virtio_gpu_ctrl_command *cmd,
ddf19c
-                      struct iovec **iov)
ddf19c
-{
ddf19c
-    struct virtio_gpu_mem_entry *ents;
ddf19c
-    size_t esize, s;
ddf19c
-    int i;
ddf19c
-
ddf19c
-    if (ab->nr_entries > 16384) {
ddf19c
-        g_critical("%s: nr_entries is too big (%d > 16384)",
ddf19c
-                   __func__, ab->nr_entries);
ddf19c
-        return -1;
ddf19c
-    }
ddf19c
-
ddf19c
-    esize = sizeof(*ents) * ab->nr_entries;
ddf19c
-    ents = g_malloc(esize);
ddf19c
-    s = iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num,
ddf19c
-                   sizeof(*ab), ents, esize);
ddf19c
-    if (s != esize) {
ddf19c
-        g_critical("%s: command data size incorrect %zu vs %zu",
ddf19c
-                   __func__, s, esize);
ddf19c
-        g_free(ents);
ddf19c
-        return -1;
ddf19c
-    }
ddf19c
-
ddf19c
-    *iov = g_malloc0(sizeof(struct iovec) * ab->nr_entries);
ddf19c
-    for (i = 0; i < ab->nr_entries; i++) {
ddf19c
-        uint64_t len = ents[i].length;
ddf19c
-        (*iov)[i].iov_len = ents[i].length;
ddf19c
-        (*iov)[i].iov_base = vu_gpa_to_va(&g->dev.parent, &len, ents[i].addr);
ddf19c
-        if (!(*iov)[i].iov_base || len != ents[i].length) {
ddf19c
-            g_critical("%s: resource %d element %d",
ddf19c
-                       __func__, ab->resource_id, i);
ddf19c
-            g_free(*iov);
ddf19c
-            g_free(ents);
ddf19c
-            *iov = NULL;
ddf19c
-            return -1;
ddf19c
-        }
ddf19c
-    }
ddf19c
-    g_free(ents);
ddf19c
-    return 0;
ddf19c
-}
ddf19c
-
ddf19c
-static void
ddf19c
-vg_resource_attach_backing(VuGpu *g,
ddf19c
-                           struct virtio_gpu_ctrl_command *cmd)
ddf19c
-{
ddf19c
-    struct virtio_gpu_simple_resource *res;
ddf19c
-    struct virtio_gpu_resource_attach_backing ab;
ddf19c
-    int ret;
ddf19c
-
ddf19c
-    VUGPU_FILL_CMD(ab);
ddf19c
-    virtio_gpu_bswap_32(&ab, sizeof(ab));
ddf19c
-
ddf19c
-    res = virtio_gpu_find_resource(g, ab.resource_id);
ddf19c
-    if (!res) {
ddf19c
-        g_critical("%s: illegal resource specified %d",
ddf19c
-                   __func__, ab.resource_id);
ddf19c
-        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    ret = vg_create_mapping_iov(g, &ab, cmd, &res->iov);
ddf19c
-    if (ret != 0) {
ddf19c
-        cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    res->iov_cnt = ab.nr_entries;
ddf19c
-}
ddf19c
-
ddf19c
-static void
ddf19c
-vg_resource_detach_backing(VuGpu *g,
ddf19c
-                           struct virtio_gpu_ctrl_command *cmd)
ddf19c
-{
ddf19c
-    struct virtio_gpu_simple_resource *res;
ddf19c
-    struct virtio_gpu_resource_detach_backing detach;
ddf19c
-
ddf19c
-    VUGPU_FILL_CMD(detach);
ddf19c
-    virtio_gpu_bswap_32(&detach, sizeof(detach));
ddf19c
-
ddf19c
-    res = virtio_gpu_find_resource(g, detach.resource_id);
ddf19c
-    if (!res || !res->iov) {
ddf19c
-        g_critical("%s: illegal resource specified %d",
ddf19c
-                   __func__, detach.resource_id);
ddf19c
-        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    g_free(res->iov);
ddf19c
-    res->iov = NULL;
ddf19c
-    res->iov_cnt = 0;
ddf19c
-}
ddf19c
-
ddf19c
-static void
ddf19c
-vg_transfer_to_host_2d(VuGpu *g,
ddf19c
-                       struct virtio_gpu_ctrl_command *cmd)
ddf19c
-{
ddf19c
-    struct virtio_gpu_simple_resource *res;
ddf19c
-    int h;
ddf19c
-    uint32_t src_offset, dst_offset, stride;
ddf19c
-    int bpp;
ddf19c
-    pixman_format_code_t format;
ddf19c
-    struct virtio_gpu_transfer_to_host_2d t2d;
ddf19c
-
ddf19c
-    VUGPU_FILL_CMD(t2d);
ddf19c
-    virtio_gpu_t2d_bswap(&t2d);
ddf19c
-
ddf19c
-    res = virtio_gpu_find_resource(g, t2d.resource_id);
ddf19c
-    if (!res || !res->iov) {
ddf19c
-        g_critical("%s: illegal resource specified %d",
ddf19c
-                   __func__, t2d.resource_id);
ddf19c
-        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    if (t2d.r.x > res->width ||
ddf19c
-        t2d.r.y > res->height ||
ddf19c
-        t2d.r.width > res->width ||
ddf19c
-        t2d.r.height > res->height ||
ddf19c
-        t2d.r.x + t2d.r.width > res->width ||
ddf19c
-        t2d.r.y + t2d.r.height > res->height) {
ddf19c
-        g_critical("%s: transfer bounds outside resource"
ddf19c
-                   " bounds for resource %d: %d %d %d %d vs %d %d",
ddf19c
-                   __func__, t2d.resource_id, t2d.r.x, t2d.r.y,
ddf19c
-                   t2d.r.width, t2d.r.height, res->width, res->height);
ddf19c
-        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    format = pixman_image_get_format(res->image);
ddf19c
-    bpp = (PIXMAN_FORMAT_BPP(format) + 7) / 8;
ddf19c
-    stride = pixman_image_get_stride(res->image);
ddf19c
-
ddf19c
-    if (t2d.offset || t2d.r.x || t2d.r.y ||
ddf19c
-        t2d.r.width != pixman_image_get_width(res->image)) {
ddf19c
-        void *img_data = pixman_image_get_data(res->image);
ddf19c
-        for (h = 0; h < t2d.r.height; h++) {
ddf19c
-            src_offset = t2d.offset + stride * h;
ddf19c
-            dst_offset = (t2d.r.y + h) * stride + (t2d.r.x * bpp);
ddf19c
-
ddf19c
-            iov_to_buf(res->iov, res->iov_cnt, src_offset,
ddf19c
-                       img_data
ddf19c
-                       + dst_offset, t2d.r.width * bpp);
ddf19c
-        }
ddf19c
-    } else {
ddf19c
-        iov_to_buf(res->iov, res->iov_cnt, 0,
ddf19c
-                   pixman_image_get_data(res->image),
ddf19c
-                   pixman_image_get_stride(res->image)
ddf19c
-                   * pixman_image_get_height(res->image));
ddf19c
-    }
ddf19c
-}
ddf19c
-
ddf19c
-static void
ddf19c
-vg_set_scanout(VuGpu *g,
ddf19c
-               struct virtio_gpu_ctrl_command *cmd)
ddf19c
-{
ddf19c
-    struct virtio_gpu_simple_resource *res, *ores;
ddf19c
-    struct virtio_gpu_scanout *scanout;
ddf19c
-    struct virtio_gpu_set_scanout ss;
ddf19c
-    int fd;
ddf19c
-
ddf19c
-    VUGPU_FILL_CMD(ss);
ddf19c
-    virtio_gpu_bswap_32(&ss, sizeof(ss));
ddf19c
-
ddf19c
-    if (ss.scanout_id >= VIRTIO_GPU_MAX_SCANOUTS) {
ddf19c
-        g_critical("%s: illegal scanout id specified %d",
ddf19c
-                   __func__, ss.scanout_id);
ddf19c
-        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_SCANOUT_ID;
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    if (ss.resource_id == 0) {
ddf19c
-        vg_disable_scanout(g, ss.scanout_id);
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    /* create a surface for this scanout */
ddf19c
-    res = virtio_gpu_find_resource(g, ss.resource_id);
ddf19c
-    if (!res) {
ddf19c
-        g_critical("%s: illegal resource specified %d",
ddf19c
-                      __func__, ss.resource_id);
ddf19c
-        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    if (ss.r.x > res->width ||
ddf19c
-        ss.r.y > res->height ||
ddf19c
-        ss.r.width > res->width ||
ddf19c
-        ss.r.height > res->height ||
ddf19c
-        ss.r.x + ss.r.width > res->width ||
ddf19c
-        ss.r.y + ss.r.height > res->height) {
ddf19c
-        g_critical("%s: illegal scanout %d bounds for"
ddf19c
-                   " resource %d, (%d,%d)+%d,%d vs %d %d",
ddf19c
-                   __func__, ss.scanout_id, ss.resource_id, ss.r.x, ss.r.y,
ddf19c
-                   ss.r.width, ss.r.height, res->width, res->height);
ddf19c
-        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    scanout = &g->scanout[ss.scanout_id];
ddf19c
-
ddf19c
-    ores = virtio_gpu_find_resource(g, scanout->resource_id);
ddf19c
-    if (ores) {
ddf19c
-        ores->scanout_bitmask &= ~(1 << ss.scanout_id);
ddf19c
-    }
ddf19c
-
ddf19c
-    res->scanout_bitmask |= (1 << ss.scanout_id);
ddf19c
-    scanout->resource_id = ss.resource_id;
ddf19c
-    scanout->x = ss.r.x;
ddf19c
-    scanout->y = ss.r.y;
ddf19c
-    scanout->width = ss.r.width;
ddf19c
-    scanout->height = ss.r.height;
ddf19c
-
ddf19c
-    struct vugbm_buffer *buffer = &res->buffer;
ddf19c
-
ddf19c
-    if (vugbm_buffer_can_get_dmabuf_fd(buffer)) {
ddf19c
-        VhostUserGpuMsg msg = {
ddf19c
-            .request = VHOST_USER_GPU_DMABUF_SCANOUT,
ddf19c
-            .size = sizeof(VhostUserGpuDMABUFScanout),
ddf19c
-            .payload.dmabuf_scanout = (VhostUserGpuDMABUFScanout) {
ddf19c
-                .scanout_id = ss.scanout_id,
ddf19c
-                .x = ss.r.x,
ddf19c
-                .y = ss.r.y,
ddf19c
-                .width = ss.r.width,
ddf19c
-                .height = ss.r.height,
ddf19c
-                .fd_width = buffer->width,
ddf19c
-                .fd_height = buffer->height,
ddf19c
-                .fd_stride = buffer->stride,
ddf19c
-                .fd_drm_fourcc = buffer->format
ddf19c
-            }
ddf19c
-        };
ddf19c
-
ddf19c
-        if (vugbm_buffer_get_dmabuf_fd(buffer, &fd)) {
ddf19c
-            vg_send_msg(g, &msg, fd);
ddf19c
-            close(fd);
ddf19c
-        }
ddf19c
-    } else {
ddf19c
-        VhostUserGpuMsg msg = {
ddf19c
-            .request = VHOST_USER_GPU_SCANOUT,
ddf19c
-            .size = sizeof(VhostUserGpuScanout),
ddf19c
-            .payload.scanout = (VhostUserGpuScanout) {
ddf19c
-                .scanout_id = ss.scanout_id,
ddf19c
-                .width = scanout->width,
ddf19c
-                .height = scanout->height
ddf19c
-            }
ddf19c
-        };
ddf19c
-        vg_send_msg(g, &msg, -1);
ddf19c
-    }
ddf19c
-}
ddf19c
-
ddf19c
-static void
ddf19c
-vg_resource_flush(VuGpu *g,
ddf19c
-                  struct virtio_gpu_ctrl_command *cmd)
ddf19c
-{
ddf19c
-    struct virtio_gpu_simple_resource *res;
ddf19c
-    struct virtio_gpu_resource_flush rf;
ddf19c
-    pixman_region16_t flush_region;
ddf19c
-    int i;
ddf19c
-
ddf19c
-    VUGPU_FILL_CMD(rf);
ddf19c
-    virtio_gpu_bswap_32(&rf, sizeof(rf));
ddf19c
-
ddf19c
-    res = virtio_gpu_find_resource(g, rf.resource_id);
ddf19c
-    if (!res) {
ddf19c
-        g_critical("%s: illegal resource specified %d\n",
ddf19c
-                   __func__, rf.resource_id);
ddf19c
-        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    if (rf.r.x > res->width ||
ddf19c
-        rf.r.y > res->height ||
ddf19c
-        rf.r.width > res->width ||
ddf19c
-        rf.r.height > res->height ||
ddf19c
-        rf.r.x + rf.r.width > res->width ||
ddf19c
-        rf.r.y + rf.r.height > res->height) {
ddf19c
-        g_critical("%s: flush bounds outside resource"
ddf19c
-                   " bounds for resource %d: %d %d %d %d vs %d %d\n",
ddf19c
-                   __func__, rf.resource_id, rf.r.x, rf.r.y,
ddf19c
-                   rf.r.width, rf.r.height, res->width, res->height);
ddf19c
-        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    pixman_region_init_rect(&flush_region,
ddf19c
-                            rf.r.x, rf.r.y, rf.r.width, rf.r.height);
ddf19c
-    for (i = 0; i < VIRTIO_GPU_MAX_SCANOUTS; i++) {
ddf19c
-        struct virtio_gpu_scanout *scanout;
ddf19c
-        pixman_region16_t region, finalregion;
ddf19c
-        pixman_box16_t *extents;
ddf19c
-
ddf19c
-        if (!(res->scanout_bitmask & (1 << i))) {
ddf19c
-            continue;
ddf19c
-        }
ddf19c
-        scanout = &g->scanout[i];
ddf19c
-
ddf19c
-        pixman_region_init(&finalregion);
ddf19c
-        pixman_region_init_rect(&region, scanout->x, scanout->y,
ddf19c
-                                scanout->width, scanout->height);
ddf19c
-
ddf19c
-        pixman_region_intersect(&finalregion, &flush_region, &region);
ddf19c
-
ddf19c
-        extents = pixman_region_extents(&finalregion);
ddf19c
-        size_t width = extents->x2 - extents->x1;
ddf19c
-        size_t height = extents->y2 - extents->y1;
ddf19c
-
ddf19c
-        if (vugbm_buffer_can_get_dmabuf_fd(&res->buffer)) {
ddf19c
-            VhostUserGpuMsg vmsg = {
ddf19c
-                .request = VHOST_USER_GPU_DMABUF_UPDATE,
ddf19c
-                .size = sizeof(VhostUserGpuUpdate),
ddf19c
-                .payload.update = (VhostUserGpuUpdate) {
ddf19c
-                    .scanout_id = i,
ddf19c
-                    .x = extents->x1,
ddf19c
-                    .y = extents->y1,
ddf19c
-                    .width = width,
ddf19c
-                    .height = height,
ddf19c
-                }
ddf19c
-            };
ddf19c
-            vg_send_msg(g, &vmsg, -1);
ddf19c
-            vg_wait_ok(g);
ddf19c
-        } else {
ddf19c
-            size_t bpp =
ddf19c
-                PIXMAN_FORMAT_BPP(pixman_image_get_format(res->image)) / 8;
ddf19c
-            size_t size = width * height * bpp;
ddf19c
-
ddf19c
-            void *p = g_malloc(VHOST_USER_GPU_HDR_SIZE +
ddf19c
-                               sizeof(VhostUserGpuUpdate) + size);
ddf19c
-            VhostUserGpuMsg *msg = p;
ddf19c
-            msg->request = VHOST_USER_GPU_UPDATE;
ddf19c
-            msg->size = sizeof(VhostUserGpuUpdate) + size;
ddf19c
-            msg->payload.update = (VhostUserGpuUpdate) {
ddf19c
-                .scanout_id = i,
ddf19c
-                .x = extents->x1,
ddf19c
-                .y = extents->y1,
ddf19c
-                .width = width,
ddf19c
-                .height = height,
ddf19c
-            };
ddf19c
-            pixman_image_t *i =
ddf19c
-                pixman_image_create_bits(pixman_image_get_format(res->image),
ddf19c
-                                         msg->payload.update.width,
ddf19c
-                                         msg->payload.update.height,
ddf19c
-                                         p + offsetof(VhostUserGpuMsg,
ddf19c
-                                                      payload.update.data),
ddf19c
-                                         width * bpp);
ddf19c
-            pixman_image_composite(PIXMAN_OP_SRC,
ddf19c
-                                   res->image, NULL, i,
ddf19c
-                                   extents->x1, extents->y1,
ddf19c
-                                   0, 0, 0, 0,
ddf19c
-                                   width, height);
ddf19c
-            pixman_image_unref(i);
ddf19c
-            vg_send_msg(g, msg, -1);
ddf19c
-            g_free(msg);
ddf19c
-        }
ddf19c
-        pixman_region_fini(&region);
ddf19c
-        pixman_region_fini(&finalregion);
ddf19c
-    }
ddf19c
-    pixman_region_fini(&flush_region);
ddf19c
-}
ddf19c
-
ddf19c
-static void
ddf19c
-vg_process_cmd(VuGpu *vg, struct virtio_gpu_ctrl_command *cmd)
ddf19c
-{
ddf19c
-    switch (cmd->cmd_hdr.type) {
ddf19c
-    case VIRTIO_GPU_CMD_GET_DISPLAY_INFO:
ddf19c
-        vg_get_display_info(vg, cmd);
ddf19c
-        break;
ddf19c
-    case VIRTIO_GPU_CMD_RESOURCE_CREATE_2D:
ddf19c
-        vg_resource_create_2d(vg, cmd);
ddf19c
-        break;
ddf19c
-    case VIRTIO_GPU_CMD_RESOURCE_UNREF:
ddf19c
-        vg_resource_unref(vg, cmd);
ddf19c
-        break;
ddf19c
-    case VIRTIO_GPU_CMD_RESOURCE_FLUSH:
ddf19c
-        vg_resource_flush(vg, cmd);
ddf19c
-        break;
ddf19c
-    case VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D:
ddf19c
-        vg_transfer_to_host_2d(vg, cmd);
ddf19c
-        break;
ddf19c
-    case VIRTIO_GPU_CMD_SET_SCANOUT:
ddf19c
-        vg_set_scanout(vg, cmd);
ddf19c
-        break;
ddf19c
-    case VIRTIO_GPU_CMD_RESOURCE_ATTACH_BACKING:
ddf19c
-        vg_resource_attach_backing(vg, cmd);
ddf19c
-        break;
ddf19c
-    case VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING:
ddf19c
-        vg_resource_detach_backing(vg, cmd);
ddf19c
-        break;
ddf19c
-    /* case VIRTIO_GPU_CMD_GET_EDID: */
ddf19c
-    /*     break */
ddf19c
-    default:
ddf19c
-        g_warning("TODO handle ctrl %x\n", cmd->cmd_hdr.type);
ddf19c
-        cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
ddf19c
-        break;
ddf19c
-    }
ddf19c
-    if (!cmd->finished) {
ddf19c
-        vg_ctrl_response_nodata(vg, cmd, cmd->error ? cmd->error :
ddf19c
-                                VIRTIO_GPU_RESP_OK_NODATA);
ddf19c
-    }
ddf19c
-}
ddf19c
-
ddf19c
-static void
ddf19c
-vg_handle_ctrl(VuDev *dev, int qidx)
ddf19c
-{
ddf19c
-    VuGpu *vg = container_of(dev, VuGpu, dev.parent);
ddf19c
-    VuVirtq *vq = vu_get_queue(dev, qidx);
ddf19c
-    struct virtio_gpu_ctrl_command *cmd = NULL;
ddf19c
-    size_t len;
ddf19c
-
ddf19c
-    for (;;) {
ddf19c
-        if (vg->wait_ok != 0) {
ddf19c
-            return;
ddf19c
-        }
ddf19c
-
ddf19c
-        cmd = vu_queue_pop(dev, vq, sizeof(struct virtio_gpu_ctrl_command));
ddf19c
-        if (!cmd) {
ddf19c
-            break;
ddf19c
-        }
ddf19c
-        cmd->vq = vq;
ddf19c
-        cmd->error = 0;
ddf19c
-        cmd->finished = false;
ddf19c
-
ddf19c
-        len = iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num,
ddf19c
-                         0, &cmd->cmd_hdr, sizeof(cmd->cmd_hdr));
ddf19c
-        if (len != sizeof(cmd->cmd_hdr)) {
ddf19c
-            g_warning("%s: command size incorrect %zu vs %zu\n",
ddf19c
-                      __func__, len, sizeof(cmd->cmd_hdr));
ddf19c
-        }
ddf19c
-
ddf19c
-        virtio_gpu_ctrl_hdr_bswap(&cmd->cmd_hdr);
ddf19c
-        g_debug("%d %s\n", cmd->cmd_hdr.type,
ddf19c
-                vg_cmd_to_string(cmd->cmd_hdr.type));
ddf19c
-
ddf19c
-        if (vg->virgl) {
ddf19c
-            vg_virgl_process_cmd(vg, cmd);
ddf19c
-        } else {
ddf19c
-            vg_process_cmd(vg, cmd);
ddf19c
-        }
ddf19c
-
ddf19c
-        if (!cmd->finished) {
ddf19c
-            QTAILQ_INSERT_TAIL(&vg->fenceq, cmd, next);
ddf19c
-            vg->inflight++;
ddf19c
-        } else {
ddf19c
-            g_free(cmd);
ddf19c
-        }
ddf19c
-    }
ddf19c
-}
ddf19c
-
ddf19c
-static void
ddf19c
-update_cursor_data_simple(VuGpu *g, uint32_t resource_id, gpointer data)
ddf19c
-{
ddf19c
-    struct virtio_gpu_simple_resource *res;
ddf19c
-
ddf19c
-    res = virtio_gpu_find_resource(g, resource_id);
ddf19c
-    g_return_if_fail(res != NULL);
ddf19c
-    g_return_if_fail(pixman_image_get_width(res->image) == 64);
ddf19c
-    g_return_if_fail(pixman_image_get_height(res->image) == 64);
ddf19c
-    g_return_if_fail(
ddf19c
-        PIXMAN_FORMAT_BPP(pixman_image_get_format(res->image)) == 32);
ddf19c
-
ddf19c
-    memcpy(data, pixman_image_get_data(res->image), 64 * 64 * sizeof(uint32_t));
ddf19c
-}
ddf19c
-
ddf19c
-static void
ddf19c
-vg_process_cursor_cmd(VuGpu *g, struct virtio_gpu_update_cursor *cursor)
ddf19c
-{
ddf19c
-    bool move = cursor->hdr.type != VIRTIO_GPU_CMD_MOVE_CURSOR;
ddf19c
-
ddf19c
-    g_debug("%s move:%d\n", G_STRFUNC, move);
ddf19c
-
ddf19c
-    if (move) {
ddf19c
-        VhostUserGpuMsg msg = {
ddf19c
-            .request = cursor->resource_id ?
ddf19c
-                VHOST_USER_GPU_CURSOR_POS : VHOST_USER_GPU_CURSOR_POS_HIDE,
ddf19c
-            .size = sizeof(VhostUserGpuCursorPos),
ddf19c
-            .payload.cursor_pos = {
ddf19c
-                .scanout_id = cursor->pos.scanout_id,
ddf19c
-                .x = cursor->pos.x,
ddf19c
-                .y = cursor->pos.y,
ddf19c
-            }
ddf19c
-        };
ddf19c
-        vg_send_msg(g, &msg, -1);
ddf19c
-    } else {
ddf19c
-        VhostUserGpuMsg msg = {
ddf19c
-            .request = VHOST_USER_GPU_CURSOR_UPDATE,
ddf19c
-            .size = sizeof(VhostUserGpuCursorUpdate),
ddf19c
-            .payload.cursor_update = {
ddf19c
-                .pos = {
ddf19c
-                    .scanout_id = cursor->pos.scanout_id,
ddf19c
-                    .x = cursor->pos.x,
ddf19c
-                    .y = cursor->pos.y,
ddf19c
-                },
ddf19c
-                .hot_x = cursor->hot_x,
ddf19c
-                .hot_y = cursor->hot_y,
ddf19c
-            }
ddf19c
-        };
ddf19c
-        if (g->virgl) {
ddf19c
-            vg_virgl_update_cursor_data(g, cursor->resource_id,
ddf19c
-                                        msg.payload.cursor_update.data);
ddf19c
-        } else {
ddf19c
-            update_cursor_data_simple(g, cursor->resource_id,
ddf19c
-                                      msg.payload.cursor_update.data);
ddf19c
-        }
ddf19c
-        vg_send_msg(g, &msg, -1);
ddf19c
-    }
ddf19c
-}
ddf19c
-
ddf19c
-static void
ddf19c
-vg_handle_cursor(VuDev *dev, int qidx)
ddf19c
-{
ddf19c
-    VuGpu *g = container_of(dev, VuGpu, dev.parent);
ddf19c
-    VuVirtq *vq = vu_get_queue(dev, qidx);
ddf19c
-    VuVirtqElement *elem;
ddf19c
-    size_t len;
ddf19c
-    struct virtio_gpu_update_cursor cursor;
ddf19c
-
ddf19c
-    for (;;) {
ddf19c
-        elem = vu_queue_pop(dev, vq, sizeof(VuVirtqElement));
ddf19c
-        if (!elem) {
ddf19c
-            break;
ddf19c
-        }
ddf19c
-        g_debug("cursor out:%d in:%d\n", elem->out_num, elem->in_num);
ddf19c
-
ddf19c
-        len = iov_to_buf(elem->out_sg, elem->out_num,
ddf19c
-                         0, &cursor, sizeof(cursor));
ddf19c
-        if (len != sizeof(cursor)) {
ddf19c
-            g_warning("%s: cursor size incorrect %zu vs %zu\n",
ddf19c
-                      __func__, len, sizeof(cursor));
ddf19c
-        } else {
ddf19c
-            virtio_gpu_bswap_32(&cursor, sizeof(cursor));
ddf19c
-            vg_process_cursor_cmd(g, &cursor);
ddf19c
-        }
ddf19c
-        vu_queue_push(dev, vq, elem, 0);
ddf19c
-        vu_queue_notify(dev, vq);
ddf19c
-        g_free(elem);
ddf19c
-    }
ddf19c
-}
ddf19c
-
ddf19c
-static void
ddf19c
-vg_panic(VuDev *dev, const char *msg)
ddf19c
-{
ddf19c
-    g_critical("%s\n", msg);
ddf19c
-    exit(1);
ddf19c
-}
ddf19c
-
ddf19c
-static void
ddf19c
-vg_queue_set_started(VuDev *dev, int qidx, bool started)
ddf19c
-{
ddf19c
-    VuVirtq *vq = vu_get_queue(dev, qidx);
ddf19c
-
ddf19c
-    g_debug("queue started %d:%d\n", qidx, started);
ddf19c
-
ddf19c
-    switch (qidx) {
ddf19c
-    case 0:
ddf19c
-        vu_set_queue_handler(dev, vq, started ? vg_handle_ctrl : NULL);
ddf19c
-        break;
ddf19c
-    case 1:
ddf19c
-        vu_set_queue_handler(dev, vq, started ? vg_handle_cursor : NULL);
ddf19c
-        break;
ddf19c
-    default:
ddf19c
-        break;
ddf19c
-    }
ddf19c
-}
ddf19c
-
ddf19c
-static void
ddf19c
-set_gpu_protocol_features(VuGpu *g)
ddf19c
-{
ddf19c
-    uint64_t u64;
ddf19c
-    VhostUserGpuMsg msg = {
ddf19c
-        .request = VHOST_USER_GPU_GET_PROTOCOL_FEATURES
ddf19c
-    };
ddf19c
-
ddf19c
-    assert(g->wait_ok == 0);
ddf19c
-    vg_send_msg(g, &msg, -1);
ddf19c
-    if (!vg_recv_msg(g, msg.request, sizeof(u64), &u64)) {
ddf19c
-        return;
ddf19c
-    }
ddf19c
-
ddf19c
-    msg = (VhostUserGpuMsg) {
ddf19c
-        .request = VHOST_USER_GPU_SET_PROTOCOL_FEATURES,
ddf19c
-        .size = sizeof(uint64_t),
ddf19c
-        .payload.u64 = 0
ddf19c
-    };
ddf19c
-    vg_send_msg(g, &msg, -1);
ddf19c
-}
ddf19c
-
ddf19c
-static int
ddf19c
-vg_process_msg(VuDev *dev, VhostUserMsg *msg, int *do_reply)
ddf19c
-{
ddf19c
-    VuGpu *g = container_of(dev, VuGpu, dev.parent);
ddf19c
-
ddf19c
-    switch (msg->request) {
ddf19c
-    case VHOST_USER_GPU_SET_SOCKET: {
ddf19c
-        g_return_val_if_fail(msg->fd_num == 1, 1);
ddf19c
-        g_return_val_if_fail(g->sock_fd == -1, 1);
ddf19c
-        g->sock_fd = msg->fds[0];
ddf19c
-        set_gpu_protocol_features(g);
ddf19c
-        return 1;
ddf19c
-    }
ddf19c
-    default:
ddf19c
-        return 0;
ddf19c
-    }
ddf19c
-
ddf19c
-    return 0;
ddf19c
-}
ddf19c
-
ddf19c
-static uint64_t
ddf19c
-vg_get_features(VuDev *dev)
ddf19c
-{
ddf19c
-    uint64_t features = 0;
ddf19c
-
ddf19c
-    if (opt_virgl) {
ddf19c
-        features |= 1 << VIRTIO_GPU_F_VIRGL;
ddf19c
-    }
ddf19c
-
ddf19c
-    return features;
ddf19c
-}
ddf19c
-
ddf19c
-static void
ddf19c
-vg_set_features(VuDev *dev, uint64_t features)
ddf19c
-{
ddf19c
-    VuGpu *g = container_of(dev, VuGpu, dev.parent);
ddf19c
-    bool virgl = features & (1 << VIRTIO_GPU_F_VIRGL);
ddf19c
-
ddf19c
-    if (virgl && !g->virgl_inited) {
ddf19c
-        if (!vg_virgl_init(g)) {
ddf19c
-            vg_panic(dev, "Failed to initialize virgl");
ddf19c
-        }
ddf19c
-        g->virgl_inited = true;
ddf19c
-    }
ddf19c
-
ddf19c
-    g->virgl = virgl;
ddf19c
-}
ddf19c
-
ddf19c
-static int
ddf19c
-vg_get_config(VuDev *dev, uint8_t *config, uint32_t len)
ddf19c
-{
ddf19c
-    VuGpu *g = container_of(dev, VuGpu, dev.parent);
ddf19c
-
ddf19c
-    g_return_val_if_fail(len <= sizeof(struct virtio_gpu_config), -1);
ddf19c
-
ddf19c
-    if (opt_virgl) {
ddf19c
-        g->virtio_config.num_capsets = vg_virgl_get_num_capsets();
ddf19c
-    }
ddf19c
-
ddf19c
-    memcpy(config, &g->virtio_config, len);
ddf19c
-
ddf19c
-    return 0;
ddf19c
-}
ddf19c
-
ddf19c
-static int
ddf19c
-vg_set_config(VuDev *dev, const uint8_t *data,
ddf19c
-              uint32_t offset, uint32_t size,
ddf19c
-              uint32_t flags)
ddf19c
-{
ddf19c
-    VuGpu *g = container_of(dev, VuGpu, dev.parent);
ddf19c
-    struct virtio_gpu_config *config = (struct virtio_gpu_config *)data;
ddf19c
-
ddf19c
-    if (config->events_clear) {
ddf19c
-        g->virtio_config.events_read &= ~config->events_clear;
ddf19c
-    }
ddf19c
-
ddf19c
-    return 0;
ddf19c
-}
ddf19c
-
ddf19c
-static const VuDevIface vuiface = {
ddf19c
-    .set_features = vg_set_features,
ddf19c
-    .get_features = vg_get_features,
ddf19c
-    .queue_set_started = vg_queue_set_started,
ddf19c
-    .process_msg = vg_process_msg,
ddf19c
-    .get_config = vg_get_config,
ddf19c
-    .set_config = vg_set_config,
ddf19c
-};
ddf19c
-
ddf19c
-static void
ddf19c
-vg_destroy(VuGpu *g)
ddf19c
-{
ddf19c
-    struct virtio_gpu_simple_resource *res, *tmp;
ddf19c
-
ddf19c
-    vug_deinit(&g->dev);
ddf19c
-
ddf19c
-    vg_sock_fd_close(g);
ddf19c
-
ddf19c
-    QTAILQ_FOREACH_SAFE(res, &g->reslist, next, tmp) {
ddf19c
-        vg_resource_destroy(g, res);
ddf19c
-    }
ddf19c
-
ddf19c
-    vugbm_device_destroy(&g->gdev);
ddf19c
-}
ddf19c
-
ddf19c
-static GOptionEntry entries[] = {
ddf19c
-    { "print-capabilities", 'c', 0, G_OPTION_ARG_NONE, &opt_print_caps,
ddf19c
-      "Print capabilities", NULL },
ddf19c
-    { "fd", 'f', 0, G_OPTION_ARG_INT, &opt_fdnum,
ddf19c
-      "Use inherited fd socket", "FDNUM" },
ddf19c
-    { "socket-path", 's', 0, G_OPTION_ARG_FILENAME, &opt_socket_path,
ddf19c
-      "Use UNIX socket path", "PATH" },
ddf19c
-    { "render-node", 'r', 0, G_OPTION_ARG_FILENAME, &opt_render_node,
ddf19c
-      "Specify DRM render node", "PATH" },
ddf19c
-    { "virgl", 'v', 0, G_OPTION_ARG_NONE, &opt_virgl,
ddf19c
-      "Turn virgl rendering on", NULL },
ddf19c
-    { NULL, }
ddf19c
-};
ddf19c
-
ddf19c
-int
ddf19c
-main(int argc, char *argv[])
ddf19c
-{
ddf19c
-    GOptionContext *context;
ddf19c
-    GError *error = NULL;
ddf19c
-    GMainLoop *loop = NULL;
ddf19c
-    int fd;
ddf19c
-    VuGpu g = { .sock_fd = -1, .drm_rnode_fd = -1 };
ddf19c
-
ddf19c
-    QTAILQ_INIT(&g.reslist);
ddf19c
-    QTAILQ_INIT(&g.fenceq);
ddf19c
-
ddf19c
-    context = g_option_context_new("QEMU vhost-user-gpu");
ddf19c
-    g_option_context_add_main_entries(context, entries, NULL);
ddf19c
-    if (!g_option_context_parse(context, &argc, &argv, &error)) {
ddf19c
-        g_printerr("Option parsing failed: %s\n", error->message);
ddf19c
-        exit(EXIT_FAILURE);
ddf19c
-    }
ddf19c
-    g_option_context_free(context);
ddf19c
-
ddf19c
-    if (opt_print_caps) {
ddf19c
-        g_print("{\n");
ddf19c
-        g_print("  \"type\": \"gpu\",\n");
ddf19c
-        g_print("  \"features\": [\n");
ddf19c
-        g_print("    \"render-node\",\n");
ddf19c
-        g_print("    \"virgl\"\n");
ddf19c
-        g_print("  ]\n");
ddf19c
-        g_print("}\n");
ddf19c
-        exit(EXIT_SUCCESS);
ddf19c
-    }
ddf19c
-
ddf19c
-    g.drm_rnode_fd = qemu_drm_rendernode_open(opt_render_node);
ddf19c
-    if (opt_render_node && g.drm_rnode_fd == -1) {
ddf19c
-        g_printerr("Failed to open DRM rendernode.\n");
ddf19c
-        exit(EXIT_FAILURE);
ddf19c
-    }
ddf19c
-
ddf19c
-    if (g.drm_rnode_fd >= 0) {
ddf19c
-        if (!vugbm_device_init(&g.gdev, g.drm_rnode_fd)) {
ddf19c
-            g_warning("Failed to init DRM device, using fallback path");
ddf19c
-        }
ddf19c
-    }
ddf19c
-
ddf19c
-    if ((!!opt_socket_path + (opt_fdnum != -1)) != 1) {
ddf19c
-        g_printerr("Please specify either --fd or --socket-path\n");
ddf19c
-        exit(EXIT_FAILURE);
ddf19c
-    }
ddf19c
-
ddf19c
-    if (opt_socket_path) {
ddf19c
-        int lsock = unix_listen(opt_socket_path, &error_fatal);
ddf19c
-        if (lsock < 0) {
ddf19c
-            g_printerr("Failed to listen on %s.\n", opt_socket_path);
ddf19c
-            exit(EXIT_FAILURE);
ddf19c
-        }
ddf19c
-        fd = accept(lsock, NULL, NULL);
ddf19c
-        close(lsock);
ddf19c
-    } else {
ddf19c
-        fd = opt_fdnum;
ddf19c
-    }
ddf19c
-    if (fd == -1) {
ddf19c
-        g_printerr("Invalid vhost-user socket.\n");
ddf19c
-        exit(EXIT_FAILURE);
ddf19c
-    }
ddf19c
-
ddf19c
-    if (!vug_init(&g.dev, VHOST_USER_GPU_MAX_QUEUES, fd, vg_panic, &vuiface)) {
ddf19c
-        g_printerr("Failed to initialize libvhost-user-glib.\n");
ddf19c
-        exit(EXIT_FAILURE);
ddf19c
-    }
ddf19c
-
ddf19c
-    loop = g_main_loop_new(NULL, FALSE);
ddf19c
-    g_main_loop_run(loop);
ddf19c
-    g_main_loop_unref(loop);
ddf19c
-
ddf19c
-    vg_destroy(&g);
ddf19c
-    if (g.drm_rnode_fd >= 0) {
ddf19c
-        close(g.drm_rnode_fd);
ddf19c
-    }
ddf19c
-
ddf19c
-    return 0;
ddf19c
-}
ddf19c
diff --git a/contrib/vhost-user-gpu/vhost-user-gpu.c b/contrib/vhost-user-gpu/vhost-user-gpu.c
ddf19c
new file mode 100644
ddf19c
index 0000000..b45d201
ddf19c
--- /dev/null
ddf19c
+++ b/contrib/vhost-user-gpu/vhost-user-gpu.c
ddf19c
@@ -0,0 +1,1191 @@
ddf19c
+/*
ddf19c
+ * Virtio vhost-user GPU Device
ddf19c
+ *
ddf19c
+ * Copyright Red Hat, Inc. 2013-2018
ddf19c
+ *
ddf19c
+ * Authors:
ddf19c
+ *     Dave Airlie <airlied@redhat.com>
ddf19c
+ *     Gerd Hoffmann <kraxel@redhat.com>
ddf19c
+ *     Marc-André Lureau <marcandre.lureau@redhat.com>
ddf19c
+ *
ddf19c
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
ddf19c
+ * See the COPYING file in the top-level directory.
ddf19c
+ */
ddf19c
+#include "qemu/osdep.h"
ddf19c
+#include "qemu/drm.h"
ddf19c
+#include "qapi/error.h"
ddf19c
+#include "qemu/sockets.h"
ddf19c
+
ddf19c
+#include <pixman.h>
ddf19c
+#include <glib-unix.h>
ddf19c
+
ddf19c
+#include "vugpu.h"
ddf19c
+#include "hw/virtio/virtio-gpu-bswap.h"
ddf19c
+#include "hw/virtio/virtio-gpu-pixman.h"
ddf19c
+#include "virgl.h"
ddf19c
+#include "vugbm.h"
ddf19c
+
ddf19c
+enum {
ddf19c
+    VHOST_USER_GPU_MAX_QUEUES = 2,
ddf19c
+};
ddf19c
+
ddf19c
+struct virtio_gpu_simple_resource {
ddf19c
+    uint32_t resource_id;
ddf19c
+    uint32_t width;
ddf19c
+    uint32_t height;
ddf19c
+    uint32_t format;
ddf19c
+    struct iovec *iov;
ddf19c
+    unsigned int iov_cnt;
ddf19c
+    uint32_t scanout_bitmask;
ddf19c
+    pixman_image_t *image;
ddf19c
+    struct vugbm_buffer buffer;
ddf19c
+    QTAILQ_ENTRY(virtio_gpu_simple_resource) next;
ddf19c
+};
ddf19c
+
ddf19c
+static gboolean opt_print_caps;
ddf19c
+static int opt_fdnum = -1;
ddf19c
+static char *opt_socket_path;
ddf19c
+static char *opt_render_node;
ddf19c
+static gboolean opt_virgl;
ddf19c
+
ddf19c
+static void vg_handle_ctrl(VuDev *dev, int qidx);
ddf19c
+
ddf19c
+static const char *
ddf19c
+vg_cmd_to_string(int cmd)
ddf19c
+{
ddf19c
+#define CMD(cmd) [cmd] = #cmd
ddf19c
+    static const char *vg_cmd_str[] = {
ddf19c
+        CMD(VIRTIO_GPU_UNDEFINED),
ddf19c
+
ddf19c
+        /* 2d commands */
ddf19c
+        CMD(VIRTIO_GPU_CMD_GET_DISPLAY_INFO),
ddf19c
+        CMD(VIRTIO_GPU_CMD_RESOURCE_CREATE_2D),
ddf19c
+        CMD(VIRTIO_GPU_CMD_RESOURCE_UNREF),
ddf19c
+        CMD(VIRTIO_GPU_CMD_SET_SCANOUT),
ddf19c
+        CMD(VIRTIO_GPU_CMD_RESOURCE_FLUSH),
ddf19c
+        CMD(VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D),
ddf19c
+        CMD(VIRTIO_GPU_CMD_RESOURCE_ATTACH_BACKING),
ddf19c
+        CMD(VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING),
ddf19c
+        CMD(VIRTIO_GPU_CMD_GET_CAPSET_INFO),
ddf19c
+        CMD(VIRTIO_GPU_CMD_GET_CAPSET),
ddf19c
+
ddf19c
+        /* 3d commands */
ddf19c
+        CMD(VIRTIO_GPU_CMD_CTX_CREATE),
ddf19c
+        CMD(VIRTIO_GPU_CMD_CTX_DESTROY),
ddf19c
+        CMD(VIRTIO_GPU_CMD_CTX_ATTACH_RESOURCE),
ddf19c
+        CMD(VIRTIO_GPU_CMD_CTX_DETACH_RESOURCE),
ddf19c
+        CMD(VIRTIO_GPU_CMD_RESOURCE_CREATE_3D),
ddf19c
+        CMD(VIRTIO_GPU_CMD_TRANSFER_TO_HOST_3D),
ddf19c
+        CMD(VIRTIO_GPU_CMD_TRANSFER_FROM_HOST_3D),
ddf19c
+        CMD(VIRTIO_GPU_CMD_SUBMIT_3D),
ddf19c
+
ddf19c
+        /* cursor commands */
ddf19c
+        CMD(VIRTIO_GPU_CMD_UPDATE_CURSOR),
ddf19c
+        CMD(VIRTIO_GPU_CMD_MOVE_CURSOR),
ddf19c
+    };
ddf19c
+#undef REQ
ddf19c
+
ddf19c
+    if (cmd >= 0 && cmd < G_N_ELEMENTS(vg_cmd_str)) {
ddf19c
+        return vg_cmd_str[cmd];
ddf19c
+    } else {
ddf19c
+        return "unknown";
ddf19c
+    }
ddf19c
+}
ddf19c
+
ddf19c
+static int
ddf19c
+vg_sock_fd_read(int sock, void *buf, ssize_t buflen)
ddf19c
+{
ddf19c
+    int ret;
ddf19c
+
ddf19c
+    do {
ddf19c
+        ret = read(sock, buf, buflen);
ddf19c
+    } while (ret < 0 && (errno == EINTR || errno == EAGAIN));
ddf19c
+
ddf19c
+    g_warn_if_fail(ret == buflen);
ddf19c
+    return ret;
ddf19c
+}
ddf19c
+
ddf19c
+static void
ddf19c
+vg_sock_fd_close(VuGpu *g)
ddf19c
+{
ddf19c
+    if (g->sock_fd >= 0) {
ddf19c
+        close(g->sock_fd);
ddf19c
+        g->sock_fd = -1;
ddf19c
+    }
ddf19c
+}
ddf19c
+
ddf19c
+static gboolean
ddf19c
+source_wait_cb(gint fd, GIOCondition condition, gpointer user_data)
ddf19c
+{
ddf19c
+    VuGpu *g = user_data;
ddf19c
+
ddf19c
+    if (!vg_recv_msg(g, VHOST_USER_GPU_DMABUF_UPDATE, 0, NULL)) {
ddf19c
+        return G_SOURCE_CONTINUE;
ddf19c
+    }
ddf19c
+
ddf19c
+    /* resume */
ddf19c
+    g->wait_ok = 0;
ddf19c
+    vg_handle_ctrl(&g->dev.parent, 0);
ddf19c
+
ddf19c
+    return G_SOURCE_REMOVE;
ddf19c
+}
ddf19c
+
ddf19c
+void
ddf19c
+vg_wait_ok(VuGpu *g)
ddf19c
+{
ddf19c
+    assert(g->wait_ok == 0);
ddf19c
+    g->wait_ok = g_unix_fd_add(g->sock_fd, G_IO_IN | G_IO_HUP,
ddf19c
+                               source_wait_cb, g);
ddf19c
+}
ddf19c
+
ddf19c
+static int
ddf19c
+vg_sock_fd_write(int sock, const void *buf, ssize_t buflen, int fd)
ddf19c
+{
ddf19c
+    ssize_t ret;
ddf19c
+    struct iovec iov = {
ddf19c
+        .iov_base = (void *)buf,
ddf19c
+        .iov_len = buflen,
ddf19c
+    };
ddf19c
+    struct msghdr msg = {
ddf19c
+        .msg_iov = &iov,
ddf19c
+        .msg_iovlen = 1,
ddf19c
+    };
ddf19c
+    union {
ddf19c
+        struct cmsghdr cmsghdr;
ddf19c
+        char control[CMSG_SPACE(sizeof(int))];
ddf19c
+    } cmsgu;
ddf19c
+    struct cmsghdr *cmsg;
ddf19c
+
ddf19c
+    if (fd != -1) {
ddf19c
+        msg.msg_control = cmsgu.control;
ddf19c
+        msg.msg_controllen = sizeof(cmsgu.control);
ddf19c
+
ddf19c
+        cmsg = CMSG_FIRSTHDR(&msg;;
ddf19c
+        cmsg->cmsg_len = CMSG_LEN(sizeof(int));
ddf19c
+        cmsg->cmsg_level = SOL_SOCKET;
ddf19c
+        cmsg->cmsg_type = SCM_RIGHTS;
ddf19c
+
ddf19c
+        *((int *)CMSG_DATA(cmsg)) = fd;
ddf19c
+    }
ddf19c
+
ddf19c
+    do {
ddf19c
+        ret = sendmsg(sock, &msg, 0);
ddf19c
+    } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
ddf19c
+
ddf19c
+    g_warn_if_fail(ret == buflen);
ddf19c
+    return ret;
ddf19c
+}
ddf19c
+
ddf19c
+void
ddf19c
+vg_send_msg(VuGpu *vg, const VhostUserGpuMsg *msg, int fd)
ddf19c
+{
ddf19c
+    if (vg_sock_fd_write(vg->sock_fd, msg,
ddf19c
+                         VHOST_USER_GPU_HDR_SIZE + msg->size, fd) < 0) {
ddf19c
+        vg_sock_fd_close(vg);
ddf19c
+    }
ddf19c
+}
ddf19c
+
ddf19c
+bool
ddf19c
+vg_recv_msg(VuGpu *g, uint32_t expect_req, uint32_t expect_size,
ddf19c
+            gpointer payload)
ddf19c
+{
ddf19c
+    uint32_t req, flags, size;
ddf19c
+
ddf19c
+    if (vg_sock_fd_read(g->sock_fd, &req, sizeof(req)) < 0 ||
ddf19c
+        vg_sock_fd_read(g->sock_fd, &flags, sizeof(flags)) < 0 ||
ddf19c
+        vg_sock_fd_read(g->sock_fd, &size, sizeof(size)) < 0) {
ddf19c
+        goto err;
ddf19c
+    }
ddf19c
+
ddf19c
+    g_return_val_if_fail(req == expect_req, false);
ddf19c
+    g_return_val_if_fail(flags & VHOST_USER_GPU_MSG_FLAG_REPLY, false);
ddf19c
+    g_return_val_if_fail(size == expect_size, false);
ddf19c
+
ddf19c
+    if (size && vg_sock_fd_read(g->sock_fd, payload, size) != size) {
ddf19c
+        goto err;
ddf19c
+    }
ddf19c
+
ddf19c
+    return true;
ddf19c
+
ddf19c
+err:
ddf19c
+    vg_sock_fd_close(g);
ddf19c
+    return false;
ddf19c
+}
ddf19c
+
ddf19c
+static struct virtio_gpu_simple_resource *
ddf19c
+virtio_gpu_find_resource(VuGpu *g, uint32_t resource_id)
ddf19c
+{
ddf19c
+    struct virtio_gpu_simple_resource *res;
ddf19c
+
ddf19c
+    QTAILQ_FOREACH(res, &g->reslist, next) {
ddf19c
+        if (res->resource_id == resource_id) {
ddf19c
+            return res;
ddf19c
+        }
ddf19c
+    }
ddf19c
+    return NULL;
ddf19c
+}
ddf19c
+
ddf19c
+void
ddf19c
+vg_ctrl_response(VuGpu *g,
ddf19c
+                 struct virtio_gpu_ctrl_command *cmd,
ddf19c
+                 struct virtio_gpu_ctrl_hdr *resp,
ddf19c
+                 size_t resp_len)
ddf19c
+{
ddf19c
+    size_t s;
ddf19c
+
ddf19c
+    if (cmd->cmd_hdr.flags & VIRTIO_GPU_FLAG_FENCE) {
ddf19c
+        resp->flags |= VIRTIO_GPU_FLAG_FENCE;
ddf19c
+        resp->fence_id = cmd->cmd_hdr.fence_id;
ddf19c
+        resp->ctx_id = cmd->cmd_hdr.ctx_id;
ddf19c
+    }
ddf19c
+    virtio_gpu_ctrl_hdr_bswap(resp);
ddf19c
+    s = iov_from_buf(cmd->elem.in_sg, cmd->elem.in_num, 0, resp, resp_len);
ddf19c
+    if (s != resp_len) {
ddf19c
+        g_critical("%s: response size incorrect %zu vs %zu",
ddf19c
+                   __func__, s, resp_len);
ddf19c
+    }
ddf19c
+    vu_queue_push(&g->dev.parent, cmd->vq, &cmd->elem, s);
ddf19c
+    vu_queue_notify(&g->dev.parent, cmd->vq);
ddf19c
+    cmd->finished = true;
ddf19c
+}
ddf19c
+
ddf19c
+void
ddf19c
+vg_ctrl_response_nodata(VuGpu *g,
ddf19c
+                        struct virtio_gpu_ctrl_command *cmd,
ddf19c
+                        enum virtio_gpu_ctrl_type type)
ddf19c
+{
ddf19c
+    struct virtio_gpu_ctrl_hdr resp = {
ddf19c
+        .type = type,
ddf19c
+    };
ddf19c
+
ddf19c
+    vg_ctrl_response(g, cmd, &resp, sizeof(resp));
ddf19c
+}
ddf19c
+
ddf19c
+void
ddf19c
+vg_get_display_info(VuGpu *vg, struct virtio_gpu_ctrl_command *cmd)
ddf19c
+{
ddf19c
+    struct virtio_gpu_resp_display_info dpy_info = { {} };
ddf19c
+    VhostUserGpuMsg msg = {
ddf19c
+        .request = VHOST_USER_GPU_GET_DISPLAY_INFO,
ddf19c
+        .size = 0,
ddf19c
+    };
ddf19c
+
ddf19c
+    assert(vg->wait_ok == 0);
ddf19c
+
ddf19c
+    vg_send_msg(vg, &msg, -1);
ddf19c
+    if (!vg_recv_msg(vg, msg.request, sizeof(dpy_info), &dpy_info)) {
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    vg_ctrl_response(vg, cmd, &dpy_info.hdr, sizeof(dpy_info));
ddf19c
+}
ddf19c
+
ddf19c
+static void
ddf19c
+vg_resource_create_2d(VuGpu *g,
ddf19c
+                      struct virtio_gpu_ctrl_command *cmd)
ddf19c
+{
ddf19c
+    pixman_format_code_t pformat;
ddf19c
+    struct virtio_gpu_simple_resource *res;
ddf19c
+    struct virtio_gpu_resource_create_2d c2d;
ddf19c
+
ddf19c
+    VUGPU_FILL_CMD(c2d);
ddf19c
+    virtio_gpu_bswap_32(&c2d, sizeof(c2d));
ddf19c
+
ddf19c
+    if (c2d.resource_id == 0) {
ddf19c
+        g_critical("%s: resource id 0 is not allowed", __func__);
ddf19c
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    res = virtio_gpu_find_resource(g, c2d.resource_id);
ddf19c
+    if (res) {
ddf19c
+        g_critical("%s: resource already exists %d", __func__, c2d.resource_id);
ddf19c
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    res = g_new0(struct virtio_gpu_simple_resource, 1);
ddf19c
+    res->width = c2d.width;
ddf19c
+    res->height = c2d.height;
ddf19c
+    res->format = c2d.format;
ddf19c
+    res->resource_id = c2d.resource_id;
ddf19c
+
ddf19c
+    pformat = virtio_gpu_get_pixman_format(c2d.format);
ddf19c
+    if (!pformat) {
ddf19c
+        g_critical("%s: host couldn't handle guest format %d",
ddf19c
+                   __func__, c2d.format);
ddf19c
+        g_free(res);
ddf19c
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
ddf19c
+        return;
ddf19c
+    }
ddf19c
+    vugbm_buffer_create(&res->buffer, &g->gdev, c2d.width, c2d.height);
ddf19c
+    res->image = pixman_image_create_bits(pformat,
ddf19c
+                                          c2d.width,
ddf19c
+                                          c2d.height,
ddf19c
+                                          (uint32_t *)res->buffer.mmap,
ddf19c
+                                          res->buffer.stride);
ddf19c
+    if (!res->image) {
ddf19c
+        g_critical("%s: resource creation failed %d %d %d",
ddf19c
+                   __func__, c2d.resource_id, c2d.width, c2d.height);
ddf19c
+        g_free(res);
ddf19c
+        cmd->error = VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY;
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    QTAILQ_INSERT_HEAD(&g->reslist, res, next);
ddf19c
+}
ddf19c
+
ddf19c
+static void
ddf19c
+vg_disable_scanout(VuGpu *g, int scanout_id)
ddf19c
+{
ddf19c
+    struct virtio_gpu_scanout *scanout = &g->scanout[scanout_id];
ddf19c
+    struct virtio_gpu_simple_resource *res;
ddf19c
+
ddf19c
+    if (scanout->resource_id == 0) {
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    res = virtio_gpu_find_resource(g, scanout->resource_id);
ddf19c
+    if (res) {
ddf19c
+        res->scanout_bitmask &= ~(1 << scanout_id);
ddf19c
+    }
ddf19c
+
ddf19c
+    scanout->width = 0;
ddf19c
+    scanout->height = 0;
ddf19c
+
ddf19c
+    if (g->sock_fd >= 0) {
ddf19c
+        VhostUserGpuMsg msg = {
ddf19c
+            .request = VHOST_USER_GPU_SCANOUT,
ddf19c
+            .size = sizeof(VhostUserGpuScanout),
ddf19c
+            .payload.scanout.scanout_id = scanout_id,
ddf19c
+        };
ddf19c
+        vg_send_msg(g, &msg, -1);
ddf19c
+    }
ddf19c
+}
ddf19c
+
ddf19c
+static void
ddf19c
+vg_resource_destroy(VuGpu *g,
ddf19c
+                    struct virtio_gpu_simple_resource *res)
ddf19c
+{
ddf19c
+    int i;
ddf19c
+
ddf19c
+    if (res->scanout_bitmask) {
ddf19c
+        for (i = 0; i < VIRTIO_GPU_MAX_SCANOUTS; i++) {
ddf19c
+            if (res->scanout_bitmask & (1 << i)) {
ddf19c
+                vg_disable_scanout(g, i);
ddf19c
+            }
ddf19c
+        }
ddf19c
+    }
ddf19c
+
ddf19c
+    vugbm_buffer_destroy(&res->buffer);
ddf19c
+    pixman_image_unref(res->image);
ddf19c
+    QTAILQ_REMOVE(&g->reslist, res, next);
ddf19c
+    g_free(res);
ddf19c
+}
ddf19c
+
ddf19c
+static void
ddf19c
+vg_resource_unref(VuGpu *g,
ddf19c
+                  struct virtio_gpu_ctrl_command *cmd)
ddf19c
+{
ddf19c
+    struct virtio_gpu_simple_resource *res;
ddf19c
+    struct virtio_gpu_resource_unref unref;
ddf19c
+
ddf19c
+    VUGPU_FILL_CMD(unref);
ddf19c
+    virtio_gpu_bswap_32(&unref, sizeof(unref));
ddf19c
+
ddf19c
+    res = virtio_gpu_find_resource(g, unref.resource_id);
ddf19c
+    if (!res) {
ddf19c
+        g_critical("%s: illegal resource specified %d",
ddf19c
+                   __func__, unref.resource_id);
ddf19c
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
ddf19c
+        return;
ddf19c
+    }
ddf19c
+    vg_resource_destroy(g, res);
ddf19c
+}
ddf19c
+
ddf19c
+int
ddf19c
+vg_create_mapping_iov(VuGpu *g,
ddf19c
+                      struct virtio_gpu_resource_attach_backing *ab,
ddf19c
+                      struct virtio_gpu_ctrl_command *cmd,
ddf19c
+                      struct iovec **iov)
ddf19c
+{
ddf19c
+    struct virtio_gpu_mem_entry *ents;
ddf19c
+    size_t esize, s;
ddf19c
+    int i;
ddf19c
+
ddf19c
+    if (ab->nr_entries > 16384) {
ddf19c
+        g_critical("%s: nr_entries is too big (%d > 16384)",
ddf19c
+                   __func__, ab->nr_entries);
ddf19c
+        return -1;
ddf19c
+    }
ddf19c
+
ddf19c
+    esize = sizeof(*ents) * ab->nr_entries;
ddf19c
+    ents = g_malloc(esize);
ddf19c
+    s = iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num,
ddf19c
+                   sizeof(*ab), ents, esize);
ddf19c
+    if (s != esize) {
ddf19c
+        g_critical("%s: command data size incorrect %zu vs %zu",
ddf19c
+                   __func__, s, esize);
ddf19c
+        g_free(ents);
ddf19c
+        return -1;
ddf19c
+    }
ddf19c
+
ddf19c
+    *iov = g_malloc0(sizeof(struct iovec) * ab->nr_entries);
ddf19c
+    for (i = 0; i < ab->nr_entries; i++) {
ddf19c
+        uint64_t len = ents[i].length;
ddf19c
+        (*iov)[i].iov_len = ents[i].length;
ddf19c
+        (*iov)[i].iov_base = vu_gpa_to_va(&g->dev.parent, &len, ents[i].addr);
ddf19c
+        if (!(*iov)[i].iov_base || len != ents[i].length) {
ddf19c
+            g_critical("%s: resource %d element %d",
ddf19c
+                       __func__, ab->resource_id, i);
ddf19c
+            g_free(*iov);
ddf19c
+            g_free(ents);
ddf19c
+            *iov = NULL;
ddf19c
+            return -1;
ddf19c
+        }
ddf19c
+    }
ddf19c
+    g_free(ents);
ddf19c
+    return 0;
ddf19c
+}
ddf19c
+
ddf19c
+static void
ddf19c
+vg_resource_attach_backing(VuGpu *g,
ddf19c
+                           struct virtio_gpu_ctrl_command *cmd)
ddf19c
+{
ddf19c
+    struct virtio_gpu_simple_resource *res;
ddf19c
+    struct virtio_gpu_resource_attach_backing ab;
ddf19c
+    int ret;
ddf19c
+
ddf19c
+    VUGPU_FILL_CMD(ab);
ddf19c
+    virtio_gpu_bswap_32(&ab, sizeof(ab));
ddf19c
+
ddf19c
+    res = virtio_gpu_find_resource(g, ab.resource_id);
ddf19c
+    if (!res) {
ddf19c
+        g_critical("%s: illegal resource specified %d",
ddf19c
+                   __func__, ab.resource_id);
ddf19c
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    ret = vg_create_mapping_iov(g, &ab, cmd, &res->iov);
ddf19c
+    if (ret != 0) {
ddf19c
+        cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    res->iov_cnt = ab.nr_entries;
ddf19c
+}
ddf19c
+
ddf19c
+static void
ddf19c
+vg_resource_detach_backing(VuGpu *g,
ddf19c
+                           struct virtio_gpu_ctrl_command *cmd)
ddf19c
+{
ddf19c
+    struct virtio_gpu_simple_resource *res;
ddf19c
+    struct virtio_gpu_resource_detach_backing detach;
ddf19c
+
ddf19c
+    VUGPU_FILL_CMD(detach);
ddf19c
+    virtio_gpu_bswap_32(&detach, sizeof(detach));
ddf19c
+
ddf19c
+    res = virtio_gpu_find_resource(g, detach.resource_id);
ddf19c
+    if (!res || !res->iov) {
ddf19c
+        g_critical("%s: illegal resource specified %d",
ddf19c
+                   __func__, detach.resource_id);
ddf19c
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    g_free(res->iov);
ddf19c
+    res->iov = NULL;
ddf19c
+    res->iov_cnt = 0;
ddf19c
+}
ddf19c
+
ddf19c
+static void
ddf19c
+vg_transfer_to_host_2d(VuGpu *g,
ddf19c
+                       struct virtio_gpu_ctrl_command *cmd)
ddf19c
+{
ddf19c
+    struct virtio_gpu_simple_resource *res;
ddf19c
+    int h;
ddf19c
+    uint32_t src_offset, dst_offset, stride;
ddf19c
+    int bpp;
ddf19c
+    pixman_format_code_t format;
ddf19c
+    struct virtio_gpu_transfer_to_host_2d t2d;
ddf19c
+
ddf19c
+    VUGPU_FILL_CMD(t2d);
ddf19c
+    virtio_gpu_t2d_bswap(&t2d);
ddf19c
+
ddf19c
+    res = virtio_gpu_find_resource(g, t2d.resource_id);
ddf19c
+    if (!res || !res->iov) {
ddf19c
+        g_critical("%s: illegal resource specified %d",
ddf19c
+                   __func__, t2d.resource_id);
ddf19c
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    if (t2d.r.x > res->width ||
ddf19c
+        t2d.r.y > res->height ||
ddf19c
+        t2d.r.width > res->width ||
ddf19c
+        t2d.r.height > res->height ||
ddf19c
+        t2d.r.x + t2d.r.width > res->width ||
ddf19c
+        t2d.r.y + t2d.r.height > res->height) {
ddf19c
+        g_critical("%s: transfer bounds outside resource"
ddf19c
+                   " bounds for resource %d: %d %d %d %d vs %d %d",
ddf19c
+                   __func__, t2d.resource_id, t2d.r.x, t2d.r.y,
ddf19c
+                   t2d.r.width, t2d.r.height, res->width, res->height);
ddf19c
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    format = pixman_image_get_format(res->image);
ddf19c
+    bpp = (PIXMAN_FORMAT_BPP(format) + 7) / 8;
ddf19c
+    stride = pixman_image_get_stride(res->image);
ddf19c
+
ddf19c
+    if (t2d.offset || t2d.r.x || t2d.r.y ||
ddf19c
+        t2d.r.width != pixman_image_get_width(res->image)) {
ddf19c
+        void *img_data = pixman_image_get_data(res->image);
ddf19c
+        for (h = 0; h < t2d.r.height; h++) {
ddf19c
+            src_offset = t2d.offset + stride * h;
ddf19c
+            dst_offset = (t2d.r.y + h) * stride + (t2d.r.x * bpp);
ddf19c
+
ddf19c
+            iov_to_buf(res->iov, res->iov_cnt, src_offset,
ddf19c
+                       img_data
ddf19c
+                       + dst_offset, t2d.r.width * bpp);
ddf19c
+        }
ddf19c
+    } else {
ddf19c
+        iov_to_buf(res->iov, res->iov_cnt, 0,
ddf19c
+                   pixman_image_get_data(res->image),
ddf19c
+                   pixman_image_get_stride(res->image)
ddf19c
+                   * pixman_image_get_height(res->image));
ddf19c
+    }
ddf19c
+}
ddf19c
+
ddf19c
+static void
ddf19c
+vg_set_scanout(VuGpu *g,
ddf19c
+               struct virtio_gpu_ctrl_command *cmd)
ddf19c
+{
ddf19c
+    struct virtio_gpu_simple_resource *res, *ores;
ddf19c
+    struct virtio_gpu_scanout *scanout;
ddf19c
+    struct virtio_gpu_set_scanout ss;
ddf19c
+    int fd;
ddf19c
+
ddf19c
+    VUGPU_FILL_CMD(ss);
ddf19c
+    virtio_gpu_bswap_32(&ss, sizeof(ss));
ddf19c
+
ddf19c
+    if (ss.scanout_id >= VIRTIO_GPU_MAX_SCANOUTS) {
ddf19c
+        g_critical("%s: illegal scanout id specified %d",
ddf19c
+                   __func__, ss.scanout_id);
ddf19c
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_SCANOUT_ID;
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    if (ss.resource_id == 0) {
ddf19c
+        vg_disable_scanout(g, ss.scanout_id);
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    /* create a surface for this scanout */
ddf19c
+    res = virtio_gpu_find_resource(g, ss.resource_id);
ddf19c
+    if (!res) {
ddf19c
+        g_critical("%s: illegal resource specified %d",
ddf19c
+                      __func__, ss.resource_id);
ddf19c
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    if (ss.r.x > res->width ||
ddf19c
+        ss.r.y > res->height ||
ddf19c
+        ss.r.width > res->width ||
ddf19c
+        ss.r.height > res->height ||
ddf19c
+        ss.r.x + ss.r.width > res->width ||
ddf19c
+        ss.r.y + ss.r.height > res->height) {
ddf19c
+        g_critical("%s: illegal scanout %d bounds for"
ddf19c
+                   " resource %d, (%d,%d)+%d,%d vs %d %d",
ddf19c
+                   __func__, ss.scanout_id, ss.resource_id, ss.r.x, ss.r.y,
ddf19c
+                   ss.r.width, ss.r.height, res->width, res->height);
ddf19c
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    scanout = &g->scanout[ss.scanout_id];
ddf19c
+
ddf19c
+    ores = virtio_gpu_find_resource(g, scanout->resource_id);
ddf19c
+    if (ores) {
ddf19c
+        ores->scanout_bitmask &= ~(1 << ss.scanout_id);
ddf19c
+    }
ddf19c
+
ddf19c
+    res->scanout_bitmask |= (1 << ss.scanout_id);
ddf19c
+    scanout->resource_id = ss.resource_id;
ddf19c
+    scanout->x = ss.r.x;
ddf19c
+    scanout->y = ss.r.y;
ddf19c
+    scanout->width = ss.r.width;
ddf19c
+    scanout->height = ss.r.height;
ddf19c
+
ddf19c
+    struct vugbm_buffer *buffer = &res->buffer;
ddf19c
+
ddf19c
+    if (vugbm_buffer_can_get_dmabuf_fd(buffer)) {
ddf19c
+        VhostUserGpuMsg msg = {
ddf19c
+            .request = VHOST_USER_GPU_DMABUF_SCANOUT,
ddf19c
+            .size = sizeof(VhostUserGpuDMABUFScanout),
ddf19c
+            .payload.dmabuf_scanout = (VhostUserGpuDMABUFScanout) {
ddf19c
+                .scanout_id = ss.scanout_id,
ddf19c
+                .x = ss.r.x,
ddf19c
+                .y = ss.r.y,
ddf19c
+                .width = ss.r.width,
ddf19c
+                .height = ss.r.height,
ddf19c
+                .fd_width = buffer->width,
ddf19c
+                .fd_height = buffer->height,
ddf19c
+                .fd_stride = buffer->stride,
ddf19c
+                .fd_drm_fourcc = buffer->format
ddf19c
+            }
ddf19c
+        };
ddf19c
+
ddf19c
+        if (vugbm_buffer_get_dmabuf_fd(buffer, &fd)) {
ddf19c
+            vg_send_msg(g, &msg, fd);
ddf19c
+            close(fd);
ddf19c
+        }
ddf19c
+    } else {
ddf19c
+        VhostUserGpuMsg msg = {
ddf19c
+            .request = VHOST_USER_GPU_SCANOUT,
ddf19c
+            .size = sizeof(VhostUserGpuScanout),
ddf19c
+            .payload.scanout = (VhostUserGpuScanout) {
ddf19c
+                .scanout_id = ss.scanout_id,
ddf19c
+                .width = scanout->width,
ddf19c
+                .height = scanout->height
ddf19c
+            }
ddf19c
+        };
ddf19c
+        vg_send_msg(g, &msg, -1);
ddf19c
+    }
ddf19c
+}
ddf19c
+
ddf19c
+static void
ddf19c
+vg_resource_flush(VuGpu *g,
ddf19c
+                  struct virtio_gpu_ctrl_command *cmd)
ddf19c
+{
ddf19c
+    struct virtio_gpu_simple_resource *res;
ddf19c
+    struct virtio_gpu_resource_flush rf;
ddf19c
+    pixman_region16_t flush_region;
ddf19c
+    int i;
ddf19c
+
ddf19c
+    VUGPU_FILL_CMD(rf);
ddf19c
+    virtio_gpu_bswap_32(&rf, sizeof(rf));
ddf19c
+
ddf19c
+    res = virtio_gpu_find_resource(g, rf.resource_id);
ddf19c
+    if (!res) {
ddf19c
+        g_critical("%s: illegal resource specified %d\n",
ddf19c
+                   __func__, rf.resource_id);
ddf19c
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID;
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    if (rf.r.x > res->width ||
ddf19c
+        rf.r.y > res->height ||
ddf19c
+        rf.r.width > res->width ||
ddf19c
+        rf.r.height > res->height ||
ddf19c
+        rf.r.x + rf.r.width > res->width ||
ddf19c
+        rf.r.y + rf.r.height > res->height) {
ddf19c
+        g_critical("%s: flush bounds outside resource"
ddf19c
+                   " bounds for resource %d: %d %d %d %d vs %d %d\n",
ddf19c
+                   __func__, rf.resource_id, rf.r.x, rf.r.y,
ddf19c
+                   rf.r.width, rf.r.height, res->width, res->height);
ddf19c
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    pixman_region_init_rect(&flush_region,
ddf19c
+                            rf.r.x, rf.r.y, rf.r.width, rf.r.height);
ddf19c
+    for (i = 0; i < VIRTIO_GPU_MAX_SCANOUTS; i++) {
ddf19c
+        struct virtio_gpu_scanout *scanout;
ddf19c
+        pixman_region16_t region, finalregion;
ddf19c
+        pixman_box16_t *extents;
ddf19c
+
ddf19c
+        if (!(res->scanout_bitmask & (1 << i))) {
ddf19c
+            continue;
ddf19c
+        }
ddf19c
+        scanout = &g->scanout[i];
ddf19c
+
ddf19c
+        pixman_region_init(&finalregion);
ddf19c
+        pixman_region_init_rect(&region, scanout->x, scanout->y,
ddf19c
+                                scanout->width, scanout->height);
ddf19c
+
ddf19c
+        pixman_region_intersect(&finalregion, &flush_region, &region);
ddf19c
+
ddf19c
+        extents = pixman_region_extents(&finalregion);
ddf19c
+        size_t width = extents->x2 - extents->x1;
ddf19c
+        size_t height = extents->y2 - extents->y1;
ddf19c
+
ddf19c
+        if (vugbm_buffer_can_get_dmabuf_fd(&res->buffer)) {
ddf19c
+            VhostUserGpuMsg vmsg = {
ddf19c
+                .request = VHOST_USER_GPU_DMABUF_UPDATE,
ddf19c
+                .size = sizeof(VhostUserGpuUpdate),
ddf19c
+                .payload.update = (VhostUserGpuUpdate) {
ddf19c
+                    .scanout_id = i,
ddf19c
+                    .x = extents->x1,
ddf19c
+                    .y = extents->y1,
ddf19c
+                    .width = width,
ddf19c
+                    .height = height,
ddf19c
+                }
ddf19c
+            };
ddf19c
+            vg_send_msg(g, &vmsg, -1);
ddf19c
+            vg_wait_ok(g);
ddf19c
+        } else {
ddf19c
+            size_t bpp =
ddf19c
+                PIXMAN_FORMAT_BPP(pixman_image_get_format(res->image)) / 8;
ddf19c
+            size_t size = width * height * bpp;
ddf19c
+
ddf19c
+            void *p = g_malloc(VHOST_USER_GPU_HDR_SIZE +
ddf19c
+                               sizeof(VhostUserGpuUpdate) + size);
ddf19c
+            VhostUserGpuMsg *msg = p;
ddf19c
+            msg->request = VHOST_USER_GPU_UPDATE;
ddf19c
+            msg->size = sizeof(VhostUserGpuUpdate) + size;
ddf19c
+            msg->payload.update = (VhostUserGpuUpdate) {
ddf19c
+                .scanout_id = i,
ddf19c
+                .x = extents->x1,
ddf19c
+                .y = extents->y1,
ddf19c
+                .width = width,
ddf19c
+                .height = height,
ddf19c
+            };
ddf19c
+            pixman_image_t *i =
ddf19c
+                pixman_image_create_bits(pixman_image_get_format(res->image),
ddf19c
+                                         msg->payload.update.width,
ddf19c
+                                         msg->payload.update.height,
ddf19c
+                                         p + offsetof(VhostUserGpuMsg,
ddf19c
+                                                      payload.update.data),
ddf19c
+                                         width * bpp);
ddf19c
+            pixman_image_composite(PIXMAN_OP_SRC,
ddf19c
+                                   res->image, NULL, i,
ddf19c
+                                   extents->x1, extents->y1,
ddf19c
+                                   0, 0, 0, 0,
ddf19c
+                                   width, height);
ddf19c
+            pixman_image_unref(i);
ddf19c
+            vg_send_msg(g, msg, -1);
ddf19c
+            g_free(msg);
ddf19c
+        }
ddf19c
+        pixman_region_fini(&region);
ddf19c
+        pixman_region_fini(&finalregion);
ddf19c
+    }
ddf19c
+    pixman_region_fini(&flush_region);
ddf19c
+}
ddf19c
+
ddf19c
+static void
ddf19c
+vg_process_cmd(VuGpu *vg, struct virtio_gpu_ctrl_command *cmd)
ddf19c
+{
ddf19c
+    switch (cmd->cmd_hdr.type) {
ddf19c
+    case VIRTIO_GPU_CMD_GET_DISPLAY_INFO:
ddf19c
+        vg_get_display_info(vg, cmd);
ddf19c
+        break;
ddf19c
+    case VIRTIO_GPU_CMD_RESOURCE_CREATE_2D:
ddf19c
+        vg_resource_create_2d(vg, cmd);
ddf19c
+        break;
ddf19c
+    case VIRTIO_GPU_CMD_RESOURCE_UNREF:
ddf19c
+        vg_resource_unref(vg, cmd);
ddf19c
+        break;
ddf19c
+    case VIRTIO_GPU_CMD_RESOURCE_FLUSH:
ddf19c
+        vg_resource_flush(vg, cmd);
ddf19c
+        break;
ddf19c
+    case VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D:
ddf19c
+        vg_transfer_to_host_2d(vg, cmd);
ddf19c
+        break;
ddf19c
+    case VIRTIO_GPU_CMD_SET_SCANOUT:
ddf19c
+        vg_set_scanout(vg, cmd);
ddf19c
+        break;
ddf19c
+    case VIRTIO_GPU_CMD_RESOURCE_ATTACH_BACKING:
ddf19c
+        vg_resource_attach_backing(vg, cmd);
ddf19c
+        break;
ddf19c
+    case VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING:
ddf19c
+        vg_resource_detach_backing(vg, cmd);
ddf19c
+        break;
ddf19c
+    /* case VIRTIO_GPU_CMD_GET_EDID: */
ddf19c
+    /*     break */
ddf19c
+    default:
ddf19c
+        g_warning("TODO handle ctrl %x\n", cmd->cmd_hdr.type);
ddf19c
+        cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
ddf19c
+        break;
ddf19c
+    }
ddf19c
+    if (!cmd->finished) {
ddf19c
+        vg_ctrl_response_nodata(vg, cmd, cmd->error ? cmd->error :
ddf19c
+                                VIRTIO_GPU_RESP_OK_NODATA);
ddf19c
+    }
ddf19c
+}
ddf19c
+
ddf19c
+static void
ddf19c
+vg_handle_ctrl(VuDev *dev, int qidx)
ddf19c
+{
ddf19c
+    VuGpu *vg = container_of(dev, VuGpu, dev.parent);
ddf19c
+    VuVirtq *vq = vu_get_queue(dev, qidx);
ddf19c
+    struct virtio_gpu_ctrl_command *cmd = NULL;
ddf19c
+    size_t len;
ddf19c
+
ddf19c
+    for (;;) {
ddf19c
+        if (vg->wait_ok != 0) {
ddf19c
+            return;
ddf19c
+        }
ddf19c
+
ddf19c
+        cmd = vu_queue_pop(dev, vq, sizeof(struct virtio_gpu_ctrl_command));
ddf19c
+        if (!cmd) {
ddf19c
+            break;
ddf19c
+        }
ddf19c
+        cmd->vq = vq;
ddf19c
+        cmd->error = 0;
ddf19c
+        cmd->finished = false;
ddf19c
+
ddf19c
+        len = iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num,
ddf19c
+                         0, &cmd->cmd_hdr, sizeof(cmd->cmd_hdr));
ddf19c
+        if (len != sizeof(cmd->cmd_hdr)) {
ddf19c
+            g_warning("%s: command size incorrect %zu vs %zu\n",
ddf19c
+                      __func__, len, sizeof(cmd->cmd_hdr));
ddf19c
+        }
ddf19c
+
ddf19c
+        virtio_gpu_ctrl_hdr_bswap(&cmd->cmd_hdr);
ddf19c
+        g_debug("%d %s\n", cmd->cmd_hdr.type,
ddf19c
+                vg_cmd_to_string(cmd->cmd_hdr.type));
ddf19c
+
ddf19c
+        if (vg->virgl) {
ddf19c
+            vg_virgl_process_cmd(vg, cmd);
ddf19c
+        } else {
ddf19c
+            vg_process_cmd(vg, cmd);
ddf19c
+        }
ddf19c
+
ddf19c
+        if (!cmd->finished) {
ddf19c
+            QTAILQ_INSERT_TAIL(&vg->fenceq, cmd, next);
ddf19c
+            vg->inflight++;
ddf19c
+        } else {
ddf19c
+            g_free(cmd);
ddf19c
+        }
ddf19c
+    }
ddf19c
+}
ddf19c
+
ddf19c
+static void
ddf19c
+update_cursor_data_simple(VuGpu *g, uint32_t resource_id, gpointer data)
ddf19c
+{
ddf19c
+    struct virtio_gpu_simple_resource *res;
ddf19c
+
ddf19c
+    res = virtio_gpu_find_resource(g, resource_id);
ddf19c
+    g_return_if_fail(res != NULL);
ddf19c
+    g_return_if_fail(pixman_image_get_width(res->image) == 64);
ddf19c
+    g_return_if_fail(pixman_image_get_height(res->image) == 64);
ddf19c
+    g_return_if_fail(
ddf19c
+        PIXMAN_FORMAT_BPP(pixman_image_get_format(res->image)) == 32);
ddf19c
+
ddf19c
+    memcpy(data, pixman_image_get_data(res->image), 64 * 64 * sizeof(uint32_t));
ddf19c
+}
ddf19c
+
ddf19c
+static void
ddf19c
+vg_process_cursor_cmd(VuGpu *g, struct virtio_gpu_update_cursor *cursor)
ddf19c
+{
ddf19c
+    bool move = cursor->hdr.type != VIRTIO_GPU_CMD_MOVE_CURSOR;
ddf19c
+
ddf19c
+    g_debug("%s move:%d\n", G_STRFUNC, move);
ddf19c
+
ddf19c
+    if (move) {
ddf19c
+        VhostUserGpuMsg msg = {
ddf19c
+            .request = cursor->resource_id ?
ddf19c
+                VHOST_USER_GPU_CURSOR_POS : VHOST_USER_GPU_CURSOR_POS_HIDE,
ddf19c
+            .size = sizeof(VhostUserGpuCursorPos),
ddf19c
+            .payload.cursor_pos = {
ddf19c
+                .scanout_id = cursor->pos.scanout_id,
ddf19c
+                .x = cursor->pos.x,
ddf19c
+                .y = cursor->pos.y,
ddf19c
+            }
ddf19c
+        };
ddf19c
+        vg_send_msg(g, &msg, -1);
ddf19c
+    } else {
ddf19c
+        VhostUserGpuMsg msg = {
ddf19c
+            .request = VHOST_USER_GPU_CURSOR_UPDATE,
ddf19c
+            .size = sizeof(VhostUserGpuCursorUpdate),
ddf19c
+            .payload.cursor_update = {
ddf19c
+                .pos = {
ddf19c
+                    .scanout_id = cursor->pos.scanout_id,
ddf19c
+                    .x = cursor->pos.x,
ddf19c
+                    .y = cursor->pos.y,
ddf19c
+                },
ddf19c
+                .hot_x = cursor->hot_x,
ddf19c
+                .hot_y = cursor->hot_y,
ddf19c
+            }
ddf19c
+        };
ddf19c
+        if (g->virgl) {
ddf19c
+            vg_virgl_update_cursor_data(g, cursor->resource_id,
ddf19c
+                                        msg.payload.cursor_update.data);
ddf19c
+        } else {
ddf19c
+            update_cursor_data_simple(g, cursor->resource_id,
ddf19c
+                                      msg.payload.cursor_update.data);
ddf19c
+        }
ddf19c
+        vg_send_msg(g, &msg, -1);
ddf19c
+    }
ddf19c
+}
ddf19c
+
ddf19c
+static void
ddf19c
+vg_handle_cursor(VuDev *dev, int qidx)
ddf19c
+{
ddf19c
+    VuGpu *g = container_of(dev, VuGpu, dev.parent);
ddf19c
+    VuVirtq *vq = vu_get_queue(dev, qidx);
ddf19c
+    VuVirtqElement *elem;
ddf19c
+    size_t len;
ddf19c
+    struct virtio_gpu_update_cursor cursor;
ddf19c
+
ddf19c
+    for (;;) {
ddf19c
+        elem = vu_queue_pop(dev, vq, sizeof(VuVirtqElement));
ddf19c
+        if (!elem) {
ddf19c
+            break;
ddf19c
+        }
ddf19c
+        g_debug("cursor out:%d in:%d\n", elem->out_num, elem->in_num);
ddf19c
+
ddf19c
+        len = iov_to_buf(elem->out_sg, elem->out_num,
ddf19c
+                         0, &cursor, sizeof(cursor));
ddf19c
+        if (len != sizeof(cursor)) {
ddf19c
+            g_warning("%s: cursor size incorrect %zu vs %zu\n",
ddf19c
+                      __func__, len, sizeof(cursor));
ddf19c
+        } else {
ddf19c
+            virtio_gpu_bswap_32(&cursor, sizeof(cursor));
ddf19c
+            vg_process_cursor_cmd(g, &cursor);
ddf19c
+        }
ddf19c
+        vu_queue_push(dev, vq, elem, 0);
ddf19c
+        vu_queue_notify(dev, vq);
ddf19c
+        g_free(elem);
ddf19c
+    }
ddf19c
+}
ddf19c
+
ddf19c
+static void
ddf19c
+vg_panic(VuDev *dev, const char *msg)
ddf19c
+{
ddf19c
+    g_critical("%s\n", msg);
ddf19c
+    exit(1);
ddf19c
+}
ddf19c
+
ddf19c
+static void
ddf19c
+vg_queue_set_started(VuDev *dev, int qidx, bool started)
ddf19c
+{
ddf19c
+    VuVirtq *vq = vu_get_queue(dev, qidx);
ddf19c
+
ddf19c
+    g_debug("queue started %d:%d\n", qidx, started);
ddf19c
+
ddf19c
+    switch (qidx) {
ddf19c
+    case 0:
ddf19c
+        vu_set_queue_handler(dev, vq, started ? vg_handle_ctrl : NULL);
ddf19c
+        break;
ddf19c
+    case 1:
ddf19c
+        vu_set_queue_handler(dev, vq, started ? vg_handle_cursor : NULL);
ddf19c
+        break;
ddf19c
+    default:
ddf19c
+        break;
ddf19c
+    }
ddf19c
+}
ddf19c
+
ddf19c
+static void
ddf19c
+set_gpu_protocol_features(VuGpu *g)
ddf19c
+{
ddf19c
+    uint64_t u64;
ddf19c
+    VhostUserGpuMsg msg = {
ddf19c
+        .request = VHOST_USER_GPU_GET_PROTOCOL_FEATURES
ddf19c
+    };
ddf19c
+
ddf19c
+    assert(g->wait_ok == 0);
ddf19c
+    vg_send_msg(g, &msg, -1);
ddf19c
+    if (!vg_recv_msg(g, msg.request, sizeof(u64), &u64)) {
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    msg = (VhostUserGpuMsg) {
ddf19c
+        .request = VHOST_USER_GPU_SET_PROTOCOL_FEATURES,
ddf19c
+        .size = sizeof(uint64_t),
ddf19c
+        .payload.u64 = 0
ddf19c
+    };
ddf19c
+    vg_send_msg(g, &msg, -1);
ddf19c
+}
ddf19c
+
ddf19c
+static int
ddf19c
+vg_process_msg(VuDev *dev, VhostUserMsg *msg, int *do_reply)
ddf19c
+{
ddf19c
+    VuGpu *g = container_of(dev, VuGpu, dev.parent);
ddf19c
+
ddf19c
+    switch (msg->request) {
ddf19c
+    case VHOST_USER_GPU_SET_SOCKET: {
ddf19c
+        g_return_val_if_fail(msg->fd_num == 1, 1);
ddf19c
+        g_return_val_if_fail(g->sock_fd == -1, 1);
ddf19c
+        g->sock_fd = msg->fds[0];
ddf19c
+        set_gpu_protocol_features(g);
ddf19c
+        return 1;
ddf19c
+    }
ddf19c
+    default:
ddf19c
+        return 0;
ddf19c
+    }
ddf19c
+
ddf19c
+    return 0;
ddf19c
+}
ddf19c
+
ddf19c
+static uint64_t
ddf19c
+vg_get_features(VuDev *dev)
ddf19c
+{
ddf19c
+    uint64_t features = 0;
ddf19c
+
ddf19c
+    if (opt_virgl) {
ddf19c
+        features |= 1 << VIRTIO_GPU_F_VIRGL;
ddf19c
+    }
ddf19c
+
ddf19c
+    return features;
ddf19c
+}
ddf19c
+
ddf19c
+static void
ddf19c
+vg_set_features(VuDev *dev, uint64_t features)
ddf19c
+{
ddf19c
+    VuGpu *g = container_of(dev, VuGpu, dev.parent);
ddf19c
+    bool virgl = features & (1 << VIRTIO_GPU_F_VIRGL);
ddf19c
+
ddf19c
+    if (virgl && !g->virgl_inited) {
ddf19c
+        if (!vg_virgl_init(g)) {
ddf19c
+            vg_panic(dev, "Failed to initialize virgl");
ddf19c
+        }
ddf19c
+        g->virgl_inited = true;
ddf19c
+    }
ddf19c
+
ddf19c
+    g->virgl = virgl;
ddf19c
+}
ddf19c
+
ddf19c
+static int
ddf19c
+vg_get_config(VuDev *dev, uint8_t *config, uint32_t len)
ddf19c
+{
ddf19c
+    VuGpu *g = container_of(dev, VuGpu, dev.parent);
ddf19c
+
ddf19c
+    g_return_val_if_fail(len <= sizeof(struct virtio_gpu_config), -1);
ddf19c
+
ddf19c
+    if (opt_virgl) {
ddf19c
+        g->virtio_config.num_capsets = vg_virgl_get_num_capsets();
ddf19c
+    }
ddf19c
+
ddf19c
+    memcpy(config, &g->virtio_config, len);
ddf19c
+
ddf19c
+    return 0;
ddf19c
+}
ddf19c
+
ddf19c
+static int
ddf19c
+vg_set_config(VuDev *dev, const uint8_t *data,
ddf19c
+              uint32_t offset, uint32_t size,
ddf19c
+              uint32_t flags)
ddf19c
+{
ddf19c
+    VuGpu *g = container_of(dev, VuGpu, dev.parent);
ddf19c
+    struct virtio_gpu_config *config = (struct virtio_gpu_config *)data;
ddf19c
+
ddf19c
+    if (config->events_clear) {
ddf19c
+        g->virtio_config.events_read &= ~config->events_clear;
ddf19c
+    }
ddf19c
+
ddf19c
+    return 0;
ddf19c
+}
ddf19c
+
ddf19c
+static const VuDevIface vuiface = {
ddf19c
+    .set_features = vg_set_features,
ddf19c
+    .get_features = vg_get_features,
ddf19c
+    .queue_set_started = vg_queue_set_started,
ddf19c
+    .process_msg = vg_process_msg,
ddf19c
+    .get_config = vg_get_config,
ddf19c
+    .set_config = vg_set_config,
ddf19c
+};
ddf19c
+
ddf19c
+static void
ddf19c
+vg_destroy(VuGpu *g)
ddf19c
+{
ddf19c
+    struct virtio_gpu_simple_resource *res, *tmp;
ddf19c
+
ddf19c
+    vug_deinit(&g->dev);
ddf19c
+
ddf19c
+    vg_sock_fd_close(g);
ddf19c
+
ddf19c
+    QTAILQ_FOREACH_SAFE(res, &g->reslist, next, tmp) {
ddf19c
+        vg_resource_destroy(g, res);
ddf19c
+    }
ddf19c
+
ddf19c
+    vugbm_device_destroy(&g->gdev);
ddf19c
+}
ddf19c
+
ddf19c
+static GOptionEntry entries[] = {
ddf19c
+    { "print-capabilities", 'c', 0, G_OPTION_ARG_NONE, &opt_print_caps,
ddf19c
+      "Print capabilities", NULL },
ddf19c
+    { "fd", 'f', 0, G_OPTION_ARG_INT, &opt_fdnum,
ddf19c
+      "Use inherited fd socket", "FDNUM" },
ddf19c
+    { "socket-path", 's', 0, G_OPTION_ARG_FILENAME, &opt_socket_path,
ddf19c
+      "Use UNIX socket path", "PATH" },
ddf19c
+    { "render-node", 'r', 0, G_OPTION_ARG_FILENAME, &opt_render_node,
ddf19c
+      "Specify DRM render node", "PATH" },
ddf19c
+    { "virgl", 'v', 0, G_OPTION_ARG_NONE, &opt_virgl,
ddf19c
+      "Turn virgl rendering on", NULL },
ddf19c
+    { NULL, }
ddf19c
+};
ddf19c
+
ddf19c
+int
ddf19c
+main(int argc, char *argv[])
ddf19c
+{
ddf19c
+    GOptionContext *context;
ddf19c
+    GError *error = NULL;
ddf19c
+    GMainLoop *loop = NULL;
ddf19c
+    int fd;
ddf19c
+    VuGpu g = { .sock_fd = -1, .drm_rnode_fd = -1 };
ddf19c
+
ddf19c
+    QTAILQ_INIT(&g.reslist);
ddf19c
+    QTAILQ_INIT(&g.fenceq);
ddf19c
+
ddf19c
+    context = g_option_context_new("QEMU vhost-user-gpu");
ddf19c
+    g_option_context_add_main_entries(context, entries, NULL);
ddf19c
+    if (!g_option_context_parse(context, &argc, &argv, &error)) {
ddf19c
+        g_printerr("Option parsing failed: %s\n", error->message);
ddf19c
+        exit(EXIT_FAILURE);
ddf19c
+    }
ddf19c
+    g_option_context_free(context);
ddf19c
+
ddf19c
+    if (opt_print_caps) {
ddf19c
+        g_print("{\n");
ddf19c
+        g_print("  \"type\": \"gpu\",\n");
ddf19c
+        g_print("  \"features\": [\n");
ddf19c
+        g_print("    \"render-node\",\n");
ddf19c
+        g_print("    \"virgl\"\n");
ddf19c
+        g_print("  ]\n");
ddf19c
+        g_print("}\n");
ddf19c
+        exit(EXIT_SUCCESS);
ddf19c
+    }
ddf19c
+
ddf19c
+    g.drm_rnode_fd = qemu_drm_rendernode_open(opt_render_node);
ddf19c
+    if (opt_render_node && g.drm_rnode_fd == -1) {
ddf19c
+        g_printerr("Failed to open DRM rendernode.\n");
ddf19c
+        exit(EXIT_FAILURE);
ddf19c
+    }
ddf19c
+
ddf19c
+    if (g.drm_rnode_fd >= 0) {
ddf19c
+        if (!vugbm_device_init(&g.gdev, g.drm_rnode_fd)) {
ddf19c
+            g_warning("Failed to init DRM device, using fallback path");
ddf19c
+        }
ddf19c
+    }
ddf19c
+
ddf19c
+    if ((!!opt_socket_path + (opt_fdnum != -1)) != 1) {
ddf19c
+        g_printerr("Please specify either --fd or --socket-path\n");
ddf19c
+        exit(EXIT_FAILURE);
ddf19c
+    }
ddf19c
+
ddf19c
+    if (opt_socket_path) {
ddf19c
+        int lsock = unix_listen(opt_socket_path, &error_fatal);
ddf19c
+        if (lsock < 0) {
ddf19c
+            g_printerr("Failed to listen on %s.\n", opt_socket_path);
ddf19c
+            exit(EXIT_FAILURE);
ddf19c
+        }
ddf19c
+        fd = accept(lsock, NULL, NULL);
ddf19c
+        close(lsock);
ddf19c
+    } else {
ddf19c
+        fd = opt_fdnum;
ddf19c
+    }
ddf19c
+    if (fd == -1) {
ddf19c
+        g_printerr("Invalid vhost-user socket.\n");
ddf19c
+        exit(EXIT_FAILURE);
ddf19c
+    }
ddf19c
+
ddf19c
+    if (!vug_init(&g.dev, VHOST_USER_GPU_MAX_QUEUES, fd, vg_panic, &vuiface)) {
ddf19c
+        g_printerr("Failed to initialize libvhost-user-glib.\n");
ddf19c
+        exit(EXIT_FAILURE);
ddf19c
+    }
ddf19c
+
ddf19c
+    loop = g_main_loop_new(NULL, FALSE);
ddf19c
+    g_main_loop_run(loop);
ddf19c
+    g_main_loop_unref(loop);
ddf19c
+
ddf19c
+    vg_destroy(&g);
ddf19c
+    if (g.drm_rnode_fd >= 0) {
ddf19c
+        close(g.drm_rnode_fd);
ddf19c
+    }
ddf19c
+
ddf19c
+    return 0;
ddf19c
+}
ddf19c
-- 
ddf19c
1.8.3.1
ddf19c