yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

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