169b9a
From 8635eaec9dd5152d94e2cd98056b80879357cf56 Mon Sep 17 00:00:00 2001
169b9a
From: Tarun Gupta <tgupta@redhat.com>
169b9a
Date: Wed, 20 Jun 2018 18:54:24 +0200
169b9a
Subject: [PATCH 16/17] vfio/display: adding region support
169b9a
169b9a
RH-Author: Tarun Gupta <tgupta@redhat.com>
169b9a
Message-id: <1529520865-18127-11-git-send-email-tgupta@redhat.com>
169b9a
Patchwork-id: 80912
169b9a
O-Subject: [RHEL7.6 qemu-kvm PATCH v3 10/11] vfio/display: adding region support
169b9a
Bugzilla: 1555246
169b9a
RH-Acked-by: Alex Williamson <alex.williamson@redhat.com>
169b9a
RH-Acked-by: Gerd Hoffmann <kraxel@redhat.com>
169b9a
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
169b9a
169b9a
Wire up region-based display.
169b9a
169b9a
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
169b9a
Reviewed By: Kirti Wankhede <kwankhede@nvidia.com>
169b9a
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
169b9a
169b9a
(cherry picked from 00195ba710a004af02a711239324d7137f0b189a)
169b9a
169b9a
Bugzilla: https://bugzilla.redhat.com/1555246
169b9a
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
169b9a
169b9a
Conflicts:
169b9a
    qemu_create_displaysurface_from() function in qemu-kvm does not
169b9a
    have "format" argument. It instead has the "bpp" and "byteswap"
169b9a
    argument.
169b9a
169b9a
    graphic_console_init() function in qemu-kvm does not have the
169b9a
    "head" argument
169b9a
---
169b9a
 hw/misc/vfio.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
169b9a
 1 file changed, 125 insertions(+), 2 deletions(-)
169b9a
169b9a
diff --git a/hw/misc/vfio.c b/hw/misc/vfio.c
169b9a
index 22d5cac..dd3efb3 100644
169b9a
--- a/hw/misc/vfio.c
169b9a
+++ b/hw/misc/vfio.c
169b9a
@@ -211,6 +211,14 @@ struct VFIODeviceOps {
169b9a
     void (*vfio_eoi)(VFIODevice *vdev);
169b9a
 };
169b9a
 
169b9a
+typedef struct VFIODisplay {
169b9a
+    QemuConsole *con;
169b9a
+    struct {
169b9a
+        VFIORegion buffer;
169b9a
+        DisplaySurface *surface;
169b9a
+    } region;
169b9a
+} VFIODisplay;
169b9a
+
169b9a
 typedef struct VFIOPCIDevice {
169b9a
     PCIDevice pdev;
169b9a
     VFIODevice vbasedev;
169b9a
@@ -245,6 +253,7 @@ typedef struct VFIOPCIDevice {
169b9a
     bool has_flr;
169b9a
     bool has_pm_reset;
169b9a
     bool rom_read_failed;
169b9a
+    VFIODisplay *dpy;
169b9a
 } VFIOPCIDevice;
169b9a
 
169b9a
 typedef struct VFIOGroup {
169b9a
@@ -2762,6 +2771,114 @@ static int vfio_region_mmap(VFIORegion *region)
169b9a
     return 0;
169b9a
 }
169b9a
 
169b9a
+/* ---------------------------------------------------------------------- */
169b9a
+
169b9a
+static void vfio_display_region_update(void *opaque)
169b9a
+{
169b9a
+    VFIOPCIDevice *vdev = opaque;
169b9a
+    VFIODisplay *dpy = vdev->dpy;
169b9a
+    struct vfio_device_gfx_plane_info plane = {
169b9a
+        .argsz = sizeof(plane),
169b9a
+        .flags = VFIO_GFX_PLANE_TYPE_REGION
169b9a
+    };
169b9a
+    pixman_format_code_t format;
169b9a
+    int ret;
169b9a
+
169b9a
+    ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &plane;;
169b9a
+    if (ret < 0) {
169b9a
+        error_report("ioctl VFIO_DEVICE_QUERY_GFX_PLANE: %s",
169b9a
+                     strerror(errno));
169b9a
+        return;
169b9a
+    }
169b9a
+    if (!plane.drm_format || !plane.size) {
169b9a
+        return;
169b9a
+    }
169b9a
+    format = qemu_drm_format_to_pixman(plane.drm_format);
169b9a
+    if (!format) {
169b9a
+        return;
169b9a
+    }
169b9a
+
169b9a
+    if (dpy->region.buffer.size &&
169b9a
+        dpy->region.buffer.nr != plane.region_index) {
169b9a
+        /* region changed */
169b9a
+        vfio_region_exit(&dpy->region.buffer);
169b9a
+        vfio_region_finalize(&dpy->region.buffer);
169b9a
+        dpy->region.surface = NULL;
169b9a
+    }
169b9a
+
169b9a
+    if (dpy->region.surface &&
169b9a
+        (surface_width(dpy->region.surface) != plane.width ||
169b9a
+         surface_height(dpy->region.surface) != plane.height ||
169b9a
+         dpy->region.surface->format != format)) {
169b9a
+        /* size changed */
169b9a
+        dpy->region.surface = NULL;
169b9a
+    }
169b9a
+
169b9a
+    if (!dpy->region.buffer.size) {
169b9a
+        /* mmap region */
169b9a
+        ret = vfio_region_setup(OBJECT(vdev), &vdev->vbasedev,
169b9a
+                                &dpy->region.buffer,
169b9a
+                                plane.region_index,
169b9a
+                                "display");
169b9a
+        if (ret != 0) {
169b9a
+            error_report("%s: vfio_region_setup(%d): %s",
169b9a
+                         __func__, plane.region_index, strerror(-ret));
169b9a
+            goto err;
169b9a
+        }
169b9a
+        ret = vfio_region_mmap(&dpy->region.buffer);
169b9a
+        if (ret != 0) {
169b9a
+            error_report("%s: vfio_region_mmap(%d): %s", __func__,
169b9a
+                         plane.region_index, strerror(-ret));
169b9a
+            goto err;
169b9a
+        }
169b9a
+        assert(dpy->region.buffer.mmaps[0].mmap != NULL);
169b9a
+    }
169b9a
+
169b9a
+    if (dpy->region.surface == NULL) {
169b9a
+        int bpp = PIXMAN_FORMAT_BPP(format);
169b9a
+        /* create surface */
169b9a
+        dpy->region.surface = qemu_create_displaysurface_from
169b9a
+            (plane.width, plane.height, bpp,
169b9a
+             plane.stride, dpy->region.buffer.mmaps[0].mmap, false);
169b9a
+        dpy_gfx_replace_surface(dpy->con, dpy->region.surface);
169b9a
+    }
169b9a
+
169b9a
+    /* full screen update */
169b9a
+    dpy_gfx_update(dpy->con, 0, 0,
169b9a
+                   surface_width(dpy->region.surface),
169b9a
+                   surface_height(dpy->region.surface));
169b9a
+    return;
169b9a
+
169b9a
+err:
169b9a
+    vfio_region_exit(&dpy->region.buffer);
169b9a
+    vfio_region_finalize(&dpy->region.buffer);
169b9a
+}
169b9a
+
169b9a
+static const GraphicHwOps vfio_display_region_ops = {
169b9a
+    .gfx_update = vfio_display_region_update,
169b9a
+};
169b9a
+
169b9a
+static int vfio_display_region_init(VFIOPCIDevice *vdev)
169b9a
+{
169b9a
+    vdev->dpy = g_new0(VFIODisplay, 1);
169b9a
+    vdev->dpy->con = graphic_console_init(DEVICE(vdev),
169b9a
+                                          &vfio_display_region_ops,
169b9a
+                                          vdev);
169b9a
+    return 0;
169b9a
+}
169b9a
+
169b9a
+static void vfio_display_region_exit(VFIODisplay *dpy)
169b9a
+{
169b9a
+    if (!dpy->region.buffer.size) {
169b9a
+        return;
169b9a
+    }
169b9a
+
169b9a
+    vfio_region_exit(&dpy->region.buffer);
169b9a
+    vfio_region_finalize(&dpy->region.buffer);
169b9a
+}
169b9a
+
169b9a
+/* ---------------------------------------------------------------------- */
169b9a
+
169b9a
 static int vfio_display_probe(VFIOPCIDevice *vdev)
169b9a
 {
169b9a
     struct vfio_device_gfx_plane_info probe;
169b9a
@@ -2772,8 +2889,7 @@ static int vfio_display_probe(VFIOPCIDevice *vdev)
169b9a
     probe.flags = VFIO_GFX_PLANE_TYPE_PROBE | VFIO_GFX_PLANE_TYPE_REGION;
169b9a
     ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_QUERY_GFX_PLANE, &probe);
169b9a
     if (ret == 0) {
169b9a
-        error_report("vfio-display: region support not implemented yet");
169b9a
-        return -1;
169b9a
+        return vfio_display_region_init(vdev);
169b9a
     }
169b9a
 
169b9a
     if (vdev->display == ON_OFF_AUTO_AUTO) {
169b9a
@@ -2787,6 +2903,13 @@ static int vfio_display_probe(VFIOPCIDevice *vdev)
169b9a
 
169b9a
 static void vfio_display_finalize(VFIOPCIDevice *vdev)
169b9a
 {
169b9a
+    if (!vdev->dpy) {
169b9a
+        return;
169b9a
+    }
169b9a
+
169b9a
+    graphic_console_close(vdev->dpy->con);
169b9a
+    vfio_display_region_exit(vdev->dpy);
169b9a
+    g_free(vdev->dpy);
169b9a
 }
169b9a
 
169b9a
 static void vfio_region_exit(VFIORegion *region)
169b9a
-- 
169b9a
1.8.3.1
169b9a