Blame SOURCES/kvm-build-sys-do-not-make-qemu-ga-link-with-pixman.patch

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