From patchwork Sat Jun 4 21:33:23 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= X-Patchwork-Id: 9154989 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 626A060574 for ; Sat, 4 Jun 2016 21:47:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 35DD727F2B for ; Sat, 4 Jun 2016 21:47:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2AA2028327; Sat, 4 Jun 2016 21:47:18 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 2357027F2B for ; Sat, 4 Jun 2016 21:47:17 +0000 (UTC) Received: from localhost ([::1]:34268 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b9JPY-0001iC-9x for patchwork-qemu-devel@patchwork.kernel.org; Sat, 04 Jun 2016 17:47:16 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44862) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b9JCv-0006Qv-O7 for qemu-devel@nongnu.org; Sat, 04 Jun 2016 17:34:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b9JCm-0005wI-KT for qemu-devel@nongnu.org; Sat, 04 Jun 2016 17:34:13 -0400 Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]:33814) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b9JCm-0005wA-5A for qemu-devel@nongnu.org; Sat, 04 Jun 2016 17:34:04 -0400 Received: by mail-wm0-x241.google.com with SMTP id n184so7288804wmn.1 for ; Sat, 04 Jun 2016 14:34:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mwU/iu0wT34UCIE8D40ABm+F2rE8geAiEeuSxX9gKKw=; b=V80CEQtfvpVb3yaoMu5nfK+fQ9qS9LpgS3lK81s+h07erORc+UtiwgZsfJhcFBuYhZ ZmFyLAna2Xkpw+UuDHEmub0OqwTxTrFZfyYxjCd3B+jDStPgBoQzHKIUPMBhXSGmU5DR 4RuY2bCPxTQxhZcb3HZOTIF25/HAEuDK9PUqaJgnEM7iWJiw3d5I3gKrW6ti4MumCq4r FGv/EX4+XTllPuJILNc9tzM4CrYo7yxAPCv1tvsMseID6jaGQ2X5iJloMHqEnrzIJ1NB L93rEwHyicuMxd4D4pP3L6SXIgc9VIYJV3HBwqCazFrCQuZDT+2heNKOFenjHgs8BRmL qzkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=mwU/iu0wT34UCIE8D40ABm+F2rE8geAiEeuSxX9gKKw=; b=JlmydtLQ1Z0VQPz8y4X0yBnHDPj/SJexM12y4QWOK5nK4tjthmuprIpEYSo+ag26UQ vRdUW496+bfwuldHE8sFwED0r1334QaEJB0TrU4rQFt8t2QxyfWcsvgy/uTvEh10vDrH /waFumLd+H6mHFVL0KuKf9LWEjJYdbLtueZFqbYD7h0XjuHTtoe/b4r5Vw6WIpPsHTfE E5cYy5VQUAbMtb1izjyXpyCVXX6ch+ER1Ji8bu3HhG730XDBFaiudpEcwNLzkxxkjAPi N2r1lk4NEiK8XvFrYXPWnOdXWZCsQF3LTyOff109wcItCtE91wDmrEfX6hl3Q6cyzzNc tIWA== X-Gm-Message-State: ALyK8tJ0i4gfuCv/4jFjghbxIuBxKPGPnu+lKKh5DX3Y0PgVtnQs+I4KsA1WWJs4WhJFDA== X-Received: by 10.28.178.135 with SMTP id b129mr896031wmf.60.1465076043408; Sat, 04 Jun 2016 14:34:03 -0700 (PDT) Received: from localhost (APoitiers-110-1-1-235.w193-248.abo.wanadoo.fr. [193.248.103.235]) by smtp.gmail.com with ESMTPSA id kd7sm12329634wjc.33.2016.06.04.14.34.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 04 Jun 2016 14:34:02 -0700 (PDT) From: marcandre.lureau@redhat.com To: qemu-devel@nongnu.org Date: Sat, 4 Jun 2016 23:33:23 +0200 Message-Id: <1465076003-26291-15-git-send-email-marcandre.lureau@redhat.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1465076003-26291-1-git-send-email-marcandre.lureau@redhat.com> References: <1465076003-26291-1-git-send-email-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::241 Subject: [Qemu-devel] [RFC 14/14] Add virtio-gpu vhost-user backend X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , kraxel@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Marc-André Lureau Add to virtio-gpu devices a "vhost-user" property. When set, the associated vhost-user backend is used to handle the virtio rings. For now, a socketpair is created for the backend to share the rendering results with qemu via a simple VHOST_GPU protocol. Example usage: -object vhost-user-backend,id=vug,cmd="./vhost-user-gpu" -device virtio-vga,virgl=true,vhost-user=vug Signed-off-by: Marc-André Lureau --- hw/display/Makefile.objs | 2 +- hw/display/vhost-gpu.c | 264 +++++++++++++++++++++++++++++++++++++++++ hw/display/virtio-gpu-pci.c | 6 + hw/display/virtio-gpu.c | 75 +++++++++++- hw/display/virtio-vga.c | 5 + include/hw/virtio/virtio-gpu.h | 7 ++ 6 files changed, 356 insertions(+), 3 deletions(-) create mode 100644 hw/display/vhost-gpu.c diff --git a/hw/display/Makefile.objs b/hw/display/Makefile.objs index d99780e..f889730 100644 --- a/hw/display/Makefile.objs +++ b/hw/display/Makefile.objs @@ -36,7 +36,7 @@ obj-$(CONFIG_VGA) += vga.o common-obj-$(CONFIG_QXL) += qxl.o qxl-logger.o qxl-render.o -obj-$(CONFIG_VIRTIO) += virtio-gpu.o virtio-gpu-3d.o +obj-$(CONFIG_VIRTIO) += virtio-gpu.o virtio-gpu-3d.o vhost-gpu.o obj-$(CONFIG_VIRTIO_PCI) += virtio-gpu-pci.o obj-$(CONFIG_VIRTIO_VGA) += virtio-vga.o virtio-gpu.o-cflags := $(VIRGL_CFLAGS) diff --git a/hw/display/vhost-gpu.c b/hw/display/vhost-gpu.c new file mode 100644 index 0000000..9dc8b13 --- /dev/null +++ b/hw/display/vhost-gpu.c @@ -0,0 +1,264 @@ +/* + * Virtio vhost GPU Device + * + * Copyright Red Hat, Inc. 2016 + * + * Authors: + * Marc-André Lureau + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "hw/virtio/virtio-gpu.h" +#include "sysemu/char.h" + +typedef enum VhostGpuRequest { + VHOST_GPU_NONE = 0, + VHOST_GPU_CURSOR_POS, + VHOST_GPU_CURSOR_POS_HIDE, + VHOST_GPU_CURSOR_UPDATE, + VHOST_GPU_SCANOUT, + VHOST_GPU_UPDATE, + VHOST_GPU_GL_SCANOUT, + VHOST_GPU_GL_UPDATE, +} VhostGpuRequest; + +typedef struct VhostGpuCursorPos { + uint32_t scanout_id; + uint32_t x; + uint32_t y; +} VhostGpuCursorPos; + +typedef struct VhostGpuCursorUpdate { + VhostGpuCursorPos pos; + uint32_t hot_x; + uint32_t hot_y; + uint32_t data[64 * 64]; +} VhostGpuCursorUpdate; + +typedef struct VhostGpuScanout { + uint32_t scanout_id; + uint32_t width; + uint32_t height; +} VhostGpuScanout; + +typedef struct VhostGpuGlScanout { + uint32_t scanout_id; + uint32_t x; + uint32_t y; + uint32_t width; + uint32_t height; + uint32_t fd_width; + uint32_t fd_height; + uint32_t fd_stride; + uint32_t fd_flags; + int fd_drm_fourcc; +} VhostGpuGlScanout; + +typedef struct VhostGpuUpdate { + uint32_t scanout_id; + uint32_t x; + uint32_t y; + uint32_t width; + uint32_t height; + uint8_t data[]; +} VhostGpuUpdate; + +typedef struct VhostGpuMsg { + VhostGpuRequest request; + uint32_t size; /* the following payload size */ + union { + VhostGpuCursorPos cursor_pos; + VhostGpuCursorUpdate cursor_update; + VhostGpuScanout scanout; + VhostGpuUpdate update; + VhostGpuGlScanout gl_scanout; + } payload; +} QEMU_PACKED VhostGpuMsg; + +static VhostGpuMsg m __attribute__ ((unused)); +#define VHOST_GPU_HDR_SIZE (sizeof(m.request) + sizeof(m.size)) + +static void vhost_gpu_handle_cursor(VirtIOGPU *g, VhostGpuMsg *msg) +{ + VhostGpuCursorPos *pos = &msg->payload.cursor_pos; + struct virtio_gpu_scanout *s; + + if (pos->scanout_id >= g->conf.max_outputs) { + return; + } + s = &g->scanout[pos->scanout_id]; + + if (msg->request == VHOST_GPU_CURSOR_UPDATE) { + VhostGpuCursorUpdate *up = &msg->payload.cursor_update; + if (!s->current_cursor) { + s->current_cursor = cursor_alloc(64, 64); + } + + s->current_cursor->hot_x = up->hot_x; + s->current_cursor->hot_y = up->hot_y; + + memcpy(s->current_cursor->data, up->data, + 64 * 64 * sizeof(uint32_t)); + + dpy_cursor_define(s->con, s->current_cursor); + } + + dpy_mouse_set(s->con, pos->x, pos->y, + msg->request != VHOST_GPU_CURSOR_POS_HIDE); +} + +static void vhost_gpu_handle_display(VirtIOGPU *g, VhostGpuMsg *msg) +{ + struct virtio_gpu_scanout *s; + + switch (msg->request) { + case VHOST_GPU_SCANOUT: { + VhostGpuScanout *m = &msg->payload.scanout; + + if (m->scanout_id >= g->conf.max_outputs) { + return; + } + s = &g->scanout[m->scanout_id]; + + s->ds = qemu_create_displaysurface(m->width, m->height); + if (!s->ds) { + return; + } + + dpy_gfx_replace_surface(s->con, s->ds); + break; + } + case VHOST_GPU_GL_SCANOUT: { + VhostGpuGlScanout *m = &msg->payload.gl_scanout; + int fd = qemu_chr_fe_get_msgfd(g->vhost_chr); + + if (m->scanout_id >= g->conf.max_outputs) { + close(fd); + break; + } + + g->enable = 1; + dpy_gl_scanout2(g->scanout[m->scanout_id].con, fd, + m->fd_flags & 1 /* FIXME: Y_0_TOP */, + m->x, m->y, m->width, m->height, + m->fd_width, m->fd_height, m->fd_stride, + m->fd_drm_fourcc); + break; + } + case VHOST_GPU_GL_UPDATE: { + VhostGpuUpdate *m = &msg->payload.update; + + if (m->scanout_id >= g->conf.max_outputs || + !g->scanout[m->scanout_id].con) { + break; + } + + dpy_gl_update(g->scanout[m->scanout_id].con, + m->x, m->y, m->width, m->height); + break; + } + case VHOST_GPU_UPDATE: { + VhostGpuUpdate *m = &msg->payload.update; + + if (m->scanout_id >= g->conf.max_outputs) { + break; + } + s = &g->scanout[m->scanout_id]; + + pixman_image_t *image = + pixman_image_create_bits(PIXMAN_x8r8g8b8, + m->width, + m->height, + (uint32_t *)m->data, + m->width * 4); + + pixman_image_composite(PIXMAN_OP_SRC, + image, NULL, s->ds->image, + 0, 0, 0, 0, m->x, m->y, m->width, m->height); + + pixman_image_unref(image); + dpy_gfx_update(s->con, m->x, m->y, m->width, m->height); + break; + } + default: + g_warn_if_reached(); + } +} + +static void vhost_gpu_chr_read(void *opaque) +{ + VirtIOGPU *g = opaque; + VhostGpuMsg *msg = NULL; + VhostGpuRequest request; + uint32_t size; + int r; + + r = qemu_chr_fe_read_all(g->vhost_chr, + (uint8_t *)&request, sizeof(uint32_t)); + if (r != sizeof(uint32_t)) { + error_report("failed to read msg header"); + goto end; + } + + r = qemu_chr_fe_read_all(g->vhost_chr, + (uint8_t *)&size, sizeof(uint32_t)); + if (r != sizeof(uint32_t)) { + error_report("failed to read msg size"); + goto end; + } + + msg = g_malloc(VHOST_GPU_HDR_SIZE + size); + g_return_if_fail(msg != NULL); + + r = qemu_chr_fe_read_all(g->vhost_chr, + (uint8_t *)&msg->payload, size); + if (r != size) { + error_report("failed to read msg payload %d != %d", r, size); + goto end; + } + + msg->request = request; + msg->size = size; + + if (request == VHOST_GPU_CURSOR_UPDATE || + request == VHOST_GPU_CURSOR_POS || + request == VHOST_GPU_CURSOR_POS_HIDE) { + vhost_gpu_handle_cursor(g, msg); + } else { + vhost_gpu_handle_display(g, msg); + } + +end: + g_free(msg); +} + +int vhost_gpu_init(VirtIOGPU *g, Error **errp) +{ + VirtIODevice *vdev = VIRTIO_DEVICE(g); + int sv[2]; + + if (vhost_user_backend_dev_init(g->vhost, vdev, 2, errp) < 0) { + return -1; + } + + if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) { + error_setg_errno(errp, errno, "socketpair() failed"); + return -1; + } + + g->vhost_chr = qemu_chr_open_socket(sv[0], errp); + if (!g->vhost_chr) { + return -1; + } + + qemu_set_fd_handler(sv[0], vhost_gpu_chr_read, NULL, g); + + vhost_user_gpu_set_socket(&g->vhost->dev, sv[1]); + + close(sv[1]); + + return 0; +} diff --git a/hw/display/virtio-gpu-pci.c b/hw/display/virtio-gpu-pci.c index a71b230..2331d87 100644 --- a/hw/display/virtio-gpu-pci.c +++ b/hw/display/virtio-gpu-pci.c @@ -16,6 +16,7 @@ #include "hw/virtio/virtio-bus.h" #include "hw/virtio/virtio-pci.h" #include "hw/virtio/virtio-gpu.h" +#include "qapi/error.h" static Property virtio_gpu_pci_properties[] = { DEFINE_VIRTIO_GPU_PCI_PROPERTIES(VirtIOPCIProxy), @@ -60,6 +61,11 @@ static void virtio_gpu_initfn(Object *obj) virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_GPU); + + /* could eventually be included in qdev_alias_all_properties? */ + object_property_add_alias(obj, "vhost-user", + OBJECT(&dev->vdev), "vhost-user", + &error_abort); } static const TypeInfo virtio_gpu_pci_info = { diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c index f3b0f14..b92f493 100644 --- a/hw/display/virtio-gpu.c +++ b/hw/display/virtio-gpu.c @@ -21,6 +21,8 @@ #include "hw/virtio/virtio-bus.h" #include "qemu/log.h" #include "qapi/error.h" +#include "sysemu/char.h" +#include "qemu/error-report.h" static struct virtio_gpu_simple_resource* virtio_gpu_find_resource(VirtIOGPU *g, uint32_t resource_id); @@ -905,7 +907,12 @@ static void virtio_gpu_gl_block(void *opaque, bool block) g->renderer_blocked = block; if (!block) { - virtio_gpu_process_cmdq(g); + if (g->vhost_chr) { + uint32_t ok; + qemu_chr_fe_write(g->vhost_chr, (uint8_t *)&ok, sizeof(ok)); + } else { + virtio_gpu_process_cmdq(g); + } } } @@ -962,6 +969,10 @@ static void virtio_gpu_device_realize(DeviceState *qdev, Error **errp) g->cursor_vq = virtio_add_queue(vdev, 16, virtio_gpu_handle_cursor_cb); } + if (g->vhost && vhost_gpu_init(g, errp) < 0) { + return; + } + g->ctrl_bh = qemu_bh_new(virtio_gpu_ctrl_bh, g); g->cursor_bh = qemu_bh_new(virtio_gpu_cursor_bh, g); QTAILQ_INIT(&g->reslist); @@ -982,8 +993,27 @@ static void virtio_gpu_device_realize(DeviceState *qdev, Error **errp) vmstate_register(qdev, -1, &vmstate_virtio_gpu_unmigratable, g); } +static void virtio_gpu_host_user_is_busy(Object *obj, const char *name, + Object *val, Error **errp) +{ + VirtIOGPU *g = VIRTIO_GPU(obj); + + if (g->vhost) { + error_setg(errp, "can't use already busy vhost-user"); + } else { + qdev_prop_allow_set_link_before_realize(obj, name, val, errp); + } +} + static void virtio_gpu_instance_init(Object *obj) { + VirtIOGPU *g = VIRTIO_GPU(obj); + + object_property_add_link(obj, "vhost-user", TYPE_VHOST_USER_BACKEND, + (Object **)&g->vhost, + virtio_gpu_host_user_is_busy, + OBJ_PROP_LINK_UNREF_ON_RELEASE, + &error_abort); } static void virtio_gpu_reset(VirtIODevice *vdev) @@ -993,7 +1023,9 @@ static void virtio_gpu_reset(VirtIODevice *vdev) int i; g->enable = 0; - + if (g->vhost) { + vhost_user_backend_stop(g->vhost); + } QTAILQ_FOREACH_SAFE(res, &g->reslist, next, tmp) { virtio_gpu_resource_destroy(g, res); } @@ -1026,6 +1058,42 @@ static void virtio_gpu_reset(VirtIODevice *vdev) #endif } +static void virtio_gpu_set_status(VirtIODevice *vdev, uint8_t val) +{ + VirtIOGPU *g = VIRTIO_GPU(vdev); + + if (g->vhost) { + if (val & VIRTIO_CONFIG_S_DRIVER_OK) { + vhost_user_backend_start(g->vhost); + } else { + vhost_user_backend_stop(g->vhost); + } + } +} + +static bool virtio_gpu_guest_notifier_pending(VirtIODevice *vdev, int idx) +{ + VirtIOGPU *g = VIRTIO_GPU(vdev); + + if (!g->vhost) { + return false; + } + + return vhost_virtqueue_pending(&g->vhost->dev, idx); +} + +static void virtio_gpu_guest_notifier_mask(VirtIODevice *vdev, int idx, + bool mask) +{ + VirtIOGPU *g = VIRTIO_GPU(vdev); + + if (!g->vhost) { + return; + } + + vhost_virtqueue_mask(&g->vhost->dev, vdev, idx, mask); +} + static Property virtio_gpu_properties[] = { DEFINE_PROP_UINT32("max_outputs", VirtIOGPU, conf.max_outputs, 1), #ifdef CONFIG_VIRGL @@ -1047,6 +1115,9 @@ static void virtio_gpu_class_init(ObjectClass *klass, void *data) vdc->set_config = virtio_gpu_set_config; vdc->get_features = virtio_gpu_get_features; vdc->set_features = virtio_gpu_set_features; + vdc->set_status = virtio_gpu_set_status; + vdc->guest_notifier_mask = virtio_gpu_guest_notifier_mask; + vdc->guest_notifier_pending = virtio_gpu_guest_notifier_pending; vdc->reset = virtio_gpu_reset; diff --git a/hw/display/virtio-vga.c b/hw/display/virtio-vga.c index f49f8de..6b233bb 100644 --- a/hw/display/virtio-vga.c +++ b/hw/display/virtio-vga.c @@ -181,6 +181,11 @@ static void virtio_vga_inst_initfn(Object *obj) virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_GPU); + + /* could eventually be included in qdev_alias_all_properties? */ + object_property_add_alias(obj, "vhost-user", + OBJECT(&dev->vdev), "vhost-user", + &error_abort); } static TypeInfo virtio_vga_info = { diff --git a/include/hw/virtio/virtio-gpu.h b/include/hw/virtio/virtio-gpu.h index 0cc8e67..a1e9fe5 100644 --- a/include/hw/virtio/virtio-gpu.h +++ b/include/hw/virtio/virtio-gpu.h @@ -19,6 +19,7 @@ #include "ui/console.h" #include "hw/virtio/virtio.h" #include "hw/pci/pci.h" +#include "sysemu/vhost-user-backend.h" #include "standard-headers/linux/virtio_gpu.h" #define TYPE_VIRTIO_GPU "virtio-gpu-device" @@ -82,6 +83,9 @@ struct virtio_gpu_ctrl_command { typedef struct VirtIOGPU { VirtIODevice parent_obj; + VhostUserBackend *vhost; + CharDriverState *vhost_chr; + QEMUBH *ctrl_bh; QEMUBH *cursor_bh; VirtQueue *ctrl_vq; @@ -161,4 +165,7 @@ void virtio_gpu_virgl_fence_poll(VirtIOGPU *g); void virtio_gpu_virgl_reset(VirtIOGPU *g); int virtio_gpu_virgl_init(VirtIOGPU *g); +/* vhost-gpu.c */ +int vhost_gpu_init(VirtIOGPU *g, Error **errp); + #endif