From nobody Tue Feb 10 19:14:21 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1507644580328155.90348370071467; Tue, 10 Oct 2017 07:09:40 -0700 (PDT) Received: from localhost ([::1]:35266 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e1vDy-0005t4-8P for importer@patchew.org; Tue, 10 Oct 2017 10:09:34 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41585) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e1v8R-0001lE-Ho for qemu-devel@nongnu.org; Tue, 10 Oct 2017 10:03:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e1v8L-00049y-Gk for qemu-devel@nongnu.org; Tue, 10 Oct 2017 10:03:51 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40192) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e1v8L-000499-8m for qemu-devel@nongnu.org; Tue, 10 Oct 2017 10:03:45 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 31BDA9091F; Tue, 10 Oct 2017 14:03:44 +0000 (UTC) Received: from sirius.home.kraxel.org (ovpn-116-239.ams2.redhat.com [10.36.116.239]) by smtp.corp.redhat.com (Postfix) with ESMTP id DE200675FA; Tue, 10 Oct 2017 14:03:40 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id 62E894FD42; Tue, 10 Oct 2017 16:03:34 +0200 (CEST) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 31BDA9091F Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=kraxel@redhat.com From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Tue, 10 Oct 2017 16:03:34 +0200 Message-Id: <20171010140334.8231-7-kraxel@redhat.com> In-Reply-To: <20171010140334.8231-1-kraxel@redhat.com> References: <20171010140334.8231-1-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 10 Oct 2017 14:03:44 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RfC PATCH 6/6] vfio/display: add dmabuf support (v15) 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: Alex Williamson , intel-gvt-dev@lists.freedesktop.org, Gerd Hoffmann , Tina Zhang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Wire up dma-buf based display. TODO: drop debug code and messages. Signed-off-by: Gerd Hoffmann --- hw/vfio/pci.h | 12 ++++ hw/vfio/display.c | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++= +++- 2 files changed, 193 insertions(+), 2 deletions(-) diff --git a/hw/vfio/pci.h b/hw/vfio/pci.h index c03c4b3eb0..1ab6f6abde 100644 --- a/hw/vfio/pci.h +++ b/hw/vfio/pci.h @@ -19,6 +19,7 @@ #include "qemu/event_notifier.h" #include "qemu/queue.h" #include "qemu/timer.h" +#include "ui/console.h" =20 #define PCI_ANY_ID (~0) =20 @@ -98,6 +99,14 @@ typedef struct VFIOMSIXInfo { unsigned long *pending; } VFIOMSIXInfo; =20 +typedef struct VFIODMABuf VFIODMABuf; +struct VFIODMABuf { + QemuDmaBuf buf; + uint32_t pos_x, pos_y; + int dmabuf_id; + QTAILQ_ENTRY(VFIODMABuf) next; +}; + typedef struct VFIOPCIDevice { PCIDevice pdev; VFIODevice vbasedev; @@ -152,6 +161,9 @@ typedef struct VFIOPCIDevice { uint32_t region_size; void *region_mmap; DisplaySurface *region_surface; + QTAILQ_HEAD(, VFIODMABuf) dmabufs; + VFIODMABuf *primary; + VFIODMABuf *cursor; } VFIOPCIDevice; =20 uint32_t vfio_pci_read_config(PCIDevice *pdev, uint32_t addr, int len); diff --git a/hw/vfio/display.c b/hw/vfio/display.c index 8211bcc6d4..f45fd1fb98 100644 --- a/hw/vfio/display.c +++ b/hw/vfio/display.c @@ -18,6 +18,186 @@ #include "ui/console.h" #include "pci.h" =20 +/* FIXME */ +#ifndef DRM_PLANE_TYPE_PRIMARY +# define DRM_PLANE_TYPE_PRIMARY 1 +# define DRM_PLANE_TYPE_CURSOR 2 +#endif + +static VFIODMABuf *vfio_display_get_dmabuf(VFIOPCIDevice *vdev, + uint32_t plane_type) +{ + struct vfio_device_gfx_plane_info plane; + struct vfio_device_gfx_dmabuf_fd gfd; + VFIODMABuf *dmabuf; + static int errcnt; + int ret; + + memset(&plane, 0, sizeof(plane)); + plane.argsz =3D sizeof(plane); + plane.flags =3D VFIO_GFX_PLANE_TYPE_DMABUF; + plane.drm_plane_type =3D plane_type; + ret =3D ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &plane); + if (ret < 0) { + fprintf(stderr, "(%d) ioctl VFIO_DEVICE_QUERY_GFX_PLANE(%s): %s\r", + ++errcnt, + (plane_type =3D=3D DRM_PLANE_TYPE_PRIMARY) ? "primary" : "= cursor", + strerror(errno)); + fflush(stderr); + return NULL; + } + if (!plane.drm_format || !plane.size) { + fprintf(stderr, "(%d) %s plane not initialized by guest\r", + ++errcnt, + (plane_type =3D=3D DRM_PLANE_TYPE_PRIMARY) ? "primary" : "= cursor"); + fflush(stderr); + return NULL; + } + + QTAILQ_FOREACH(dmabuf, &vdev->dmabufs, next) { + if (dmabuf->dmabuf_id =3D=3D plane.dmabuf_id) { + /* found in list, move to head, return it */ + QTAILQ_REMOVE(&vdev->dmabufs, dmabuf, next); + QTAILQ_INSERT_HEAD(&vdev->dmabufs, dmabuf, next); + if (plane_type =3D=3D DRM_PLANE_TYPE_CURSOR) { + dmabuf->pos_x =3D plane.x_pos; + dmabuf->pos_y =3D plane.y_pos; + } +#if 1 + if (plane.width !=3D dmabuf->buf.width || + plane.height !=3D dmabuf->buf.height) { + fprintf(stderr, "%s: cached dmabuf mismatch: id %d, " + "kernel %dx%d, cached %dx%d, plane %s\n", + __func__, plane.dmabuf_id, + plane.width, plane.height, + dmabuf->buf.width, dmabuf->buf.height, + (plane_type =3D=3D DRM_PLANE_TYPE_PRIMARY) + ? "primary" : "cursor"); + abort(); + } +#endif + return dmabuf; + } + } + + memset(&gfd, 0, sizeof(gfd)); + gfd.argsz =3D sizeof(gfd); + gfd.dmabuf_id =3D plane.dmabuf_id; + ret =3D ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_GFX_DMABUF, &gfd); + if (ret < 0) { + fprintf(stderr, "(%d) ioctl VFIO_DEVICE_GET_GFX_DMABUF: %s\r", + ++errcnt, strerror(errno)); + return NULL; + } + + fprintf(stderr, "%s: new dmabuf: id %d, res %dx%d, " + "format %c%c%c%c, plane %s, fd %d, hot +%d+%d\n", + __func__, plane.dmabuf_id, + plane.width, plane.height, + (plane.drm_format >> 0) & 0xff, + (plane.drm_format >> 8) & 0xff, + (plane.drm_format >> 16) & 0xff, + (plane.drm_format >> 24) & 0xff, + (plane_type =3D=3D DRM_PLANE_TYPE_PRIMARY) ? "primary" : "curs= or", + gfd.dmabuf_fd, + plane.x_pos, plane.y_pos); + + dmabuf =3D g_new0(VFIODMABuf, 1); + dmabuf->dmabuf_id =3D plane.dmabuf_id; + dmabuf->buf.width =3D plane.width; + dmabuf->buf.height =3D plane.height; + dmabuf->buf.stride =3D plane.stride; + dmabuf->buf.fourcc =3D plane.drm_format; + dmabuf->buf.fd =3D gfd.dmabuf_fd; + if (plane_type =3D=3D DRM_PLANE_TYPE_CURSOR) { + dmabuf->pos_x =3D plane.x_pos; + dmabuf->pos_y =3D plane.y_pos; + } + + QTAILQ_INSERT_HEAD(&vdev->dmabufs, dmabuf, next); + return dmabuf; +} + +static void vfio_display_free_dmabufs(VFIOPCIDevice *vdev) +{ + char log[128]; int pos =3D 0; + VFIODMABuf *dmabuf, *tmp; + uint32_t keep =3D 8; + + QTAILQ_FOREACH_SAFE(dmabuf, &vdev->dmabufs, next, tmp) { + if (keep > 0) { + pos +=3D sprintf(log + pos, " %d", dmabuf->buf.fd); + keep--; + continue; + } + assert(dmabuf !=3D vdev->primary); + QTAILQ_REMOVE(&vdev->dmabufs, dmabuf, next); + fprintf(stderr, "%s: free dmabuf: fd %d (keep%s)\n", + __func__, dmabuf->buf.fd, log); + dpy_gl_release_dmabuf(vdev->display_con, &dmabuf->buf); + close(dmabuf->buf.fd); + g_free(dmabuf); + } +} + +static void vfio_display_dmabuf_update(void *opaque) +{ + VFIOPCIDevice *vdev =3D opaque; + VFIODMABuf *primary, *cursor; + bool free_bufs =3D false; + + primary =3D vfio_display_get_dmabuf(vdev, DRM_PLANE_TYPE_PRIMARY); + if (primary =3D=3D NULL) { + return; + } + + if (vdev->primary !=3D primary) { + vdev->primary =3D primary; + qemu_console_resize(vdev->display_con, + primary->buf.width, primary->buf.height); + dpy_gl_scanout_dmabuf(vdev->display_con, + &primary->buf); + free_bufs =3D true; + } + + cursor =3D vfio_display_get_dmabuf(vdev, DRM_PLANE_TYPE_CURSOR); + if (vdev->cursor !=3D cursor) { + vdev->cursor =3D cursor; + free_bufs =3D true; + } + if (cursor !=3D NULL) { + dpy_gl_cursor_dmabuf(vdev->display_con, + &cursor->buf, + cursor->pos_x, + cursor->pos_y); + } + + dpy_gl_update(vdev->display_con, 0, 0, + primary->buf.width, primary->buf.height); + + if (free_bufs) { + vfio_display_free_dmabufs(vdev); + } +} + +static const GraphicHwOps vfio_display_dmabuf_ops =3D { + .gfx_update =3D vfio_display_dmabuf_update, +}; + +static int vfio_display_dmabuf_init(VFIOPCIDevice *vdev, Error **errp) +{ + if (!display_opengl) { + error_setg(errp, "vfio-display-dmabuf: opengl not available"); + return -1; + } + + vdev->display_con =3D graphic_console_init(DEVICE(vdev), 0, + &vfio_display_dmabuf_ops, + vdev); + /* TODO: disable hotplug (there is no graphic_console_close) */ + return 0; +} + /* ---------------------------------------------------------------------- = */ =20 static void vfio_display_region_update(void *opaque) @@ -121,8 +301,7 @@ int vfio_display_probe(VFIOPCIDevice *vdev, Error **err= p) probe.flags =3D VFIO_GFX_PLANE_TYPE_PROBE | VFIO_GFX_PLANE_TYPE_DMABUF; ret =3D ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &probe); if (ret =3D=3D 0) { - error_setg(errp, "vfio-display: dmabuf support not implemented yet= "); - return -1; + return vfio_display_dmabuf_init(vdev, errp); } =20 memset(&probe, 0, sizeof(probe)); --=20 2.9.3