9ae3a8
From d9bcfe28139d190ed9847217dd309296580c2b7e Mon Sep 17 00:00:00 2001
9ae3a8
From: Alex Williamson <alex.williamson@redhat.com>
9ae3a8
Date: Fri, 10 Apr 2015 16:34:13 +0200
9ae3a8
Subject: [PATCH 07/14] vfio: Use vfio type1 v2 IOMMU interface
9ae3a8
9ae3a8
Message-id: <20150410163413.15324.90697.stgit@gimli.home>
9ae3a8
Patchwork-id: 64787
9ae3a8
O-Subject: [RHEL7.2 qemu-kvm PATCH 7/8] vfio: Use vfio type1 v2 IOMMU interface
9ae3a8
Bugzilla: 1210508
9ae3a8
RH-Acked-by: Thomas Huth <thuth@redhat.com>
9ae3a8
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
RH-Acked-by: Bandan Das <bsd@redhat.com>
9ae3a8
9ae3a8
Upstream: 2e6e697e166568fdd09ceaa8c7c8c8c53a5e345b
9ae3a8
9ae3a8
The difference between v1 and v2 is fairly subtle, simply more
9ae3a8
deterministic behavior for unmaps.  The v1 interface allows the user
9ae3a8
to attempt to unmap sub-regions of previous mappings, returning
9ae3a8
success with zero size if unable to comply.  This was a reflection of
9ae3a8
the underlying IOMMU API.  The v2 interface requires that the user
9ae3a8
may only unmap fully contained mappings, ie. an unmap cannot intersect
9ae3a8
or bisect a previous mapping, but may cover multiple mappings.  QEMU
9ae3a8
never made use of the sub-region v1 support anyway, so we can support
9ae3a8
either v1 or v2.  We'll favor v2 since it's newer.
9ae3a8
9ae3a8
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 hw/misc/vfio.c             | 8 ++++++--
9ae3a8
 linux-headers/linux/vfio.h | 1 +
9ae3a8
 2 files changed, 7 insertions(+), 2 deletions(-)
9ae3a8
9ae3a8
diff --git a/hw/misc/vfio.c b/hw/misc/vfio.c
9ae3a8
index 607dbf4..d06b485 100644
9ae3a8
--- a/hw/misc/vfio.c
9ae3a8
+++ b/hw/misc/vfio.c
9ae3a8
@@ -3312,7 +3312,10 @@ static int vfio_connect_container(VFIOGroup *group)
9ae3a8
     container = g_malloc0(sizeof(*container));
9ae3a8
     container->fd = fd;
9ae3a8
 
9ae3a8
-    if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU)) {
9ae3a8
+    if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) ||
9ae3a8
+        ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1v2_IOMMU)) {
9ae3a8
+        bool v2 = !!ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1v2_IOMMU);
9ae3a8
+
9ae3a8
         ret = ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &fd;;
9ae3a8
         if (ret) {
9ae3a8
             error_report("vfio: failed to set group container: %m");
9ae3a8
@@ -3321,7 +3324,8 @@ static int vfio_connect_container(VFIOGroup *group)
9ae3a8
             return -errno;
9ae3a8
         }
9ae3a8
 
9ae3a8
-        ret = ioctl(fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU);
9ae3a8
+        ret = ioctl(fd, VFIO_SET_IOMMU,
9ae3a8
+                    v2 ? VFIO_TYPE1v2_IOMMU : VFIO_TYPE1_IOMMU);
9ae3a8
         if (ret) {
9ae3a8
             error_report("vfio: failed to set iommu for container: %m");
9ae3a8
             g_free(container);
9ae3a8
diff --git a/linux-headers/linux/vfio.h b/linux-headers/linux/vfio.h
9ae3a8
index d341312..7919c76 100644
9ae3a8
--- a/linux-headers/linux/vfio.h
9ae3a8
+++ b/linux-headers/linux/vfio.h
9ae3a8
@@ -22,6 +22,7 @@
9ae3a8
 /* Extensions */
9ae3a8
 
9ae3a8
 #define VFIO_TYPE1_IOMMU		1
9ae3a8
+#define VFIO_TYPE1v2_IOMMU		3
9ae3a8
 
9ae3a8
 /*
9ae3a8
  * The IOCTL interface is designed for extensibility by embedding the
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8