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