c687bc
From e6147c5a23a75361b1374bfb4b96403d243b5c38 Mon Sep 17 00:00:00 2001
c687bc
From: Cornelia Huck <cohuck@redhat.com>
c687bc
Date: Tue, 19 Jan 2021 12:50:43 -0500
c687bc
Subject: [PATCH 4/7] vfio: Find DMA available capability
c687bc
c687bc
RH-Author: Cornelia Huck <cohuck@redhat.com>
c687bc
Message-id: <20210119125046.472811-5-cohuck@redhat.com>
c687bc
Patchwork-id: 100677
c687bc
O-Subject: [RHEL-8.4.0 qemu-kvm PATCH 4/7] vfio: Find DMA available capability
c687bc
Bugzilla: 1905391
c687bc
RH-Acked-by: David Hildenbrand <david@redhat.com>
c687bc
RH-Acked-by: Auger Eric <eric.auger@redhat.com>
c687bc
RH-Acked-by: Thomas Huth <thuth@redhat.com>
c687bc
c687bc
From: Matthew Rosato <mjrosato@linux.ibm.com>
c687bc
c687bc
The underlying host may be limiting the number of outstanding DMA
c687bc
requests for type 1 IOMMU.  Add helper functions to check for the
c687bc
DMA available capability and retrieve the current number of DMA
c687bc
mappings allowed.
c687bc
c687bc
Signed-off-by: Matthew Rosato <mjrosato@linux.ibm.com>
c687bc
Reviewed-by: Cornelia Huck <cohuck@redhat.com>
c687bc
[aw: vfio_get_info_dma_avail moved inside CONFIG_LINUX]
c687bc
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
c687bc
(cherry picked from commit 7486a62845b1e12011dd99973e4739f69d57cd38)
c687bc
Signed-off-by: Cornelia Huck <cohuck@redhat.com>
c687bc
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
c687bc
---
c687bc
 hw/vfio/common.c              | 31 +++++++++++++++++++++++++++++++
c687bc
 include/hw/vfio/vfio-common.h |  2 ++
c687bc
 2 files changed, 33 insertions(+)
c687bc
c687bc
diff --git a/hw/vfio/common.c b/hw/vfio/common.c
c687bc
index 77d62d2dcdf..23efdfadebd 100644
c687bc
--- a/hw/vfio/common.c
c687bc
+++ b/hw/vfio/common.c
c687bc
@@ -850,6 +850,37 @@ vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id)
c687bc
     return vfio_get_cap((void *)info, info->cap_offset, id);
c687bc
 }
c687bc
 
c687bc
+static struct vfio_info_cap_header *
c687bc
+vfio_get_iommu_type1_info_cap(struct vfio_iommu_type1_info *info, uint16_t id)
c687bc
+{
c687bc
+    if (!(info->flags & VFIO_IOMMU_INFO_CAPS)) {
c687bc
+        return NULL;
c687bc
+    }
c687bc
+
c687bc
+    return vfio_get_cap((void *)info, info->cap_offset, id);
c687bc
+}
c687bc
+
c687bc
+bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info,
c687bc
+                             unsigned int *avail)
c687bc
+{
c687bc
+    struct vfio_info_cap_header *hdr;
c687bc
+    struct vfio_iommu_type1_info_dma_avail *cap;
c687bc
+
c687bc
+    /* If the capability cannot be found, assume no DMA limiting */
c687bc
+    hdr = vfio_get_iommu_type1_info_cap(info,
c687bc
+                                        VFIO_IOMMU_TYPE1_INFO_DMA_AVAIL);
c687bc
+    if (hdr == NULL) {
c687bc
+        return false;
c687bc
+    }
c687bc
+
c687bc
+    if (avail != NULL) {
c687bc
+        cap = (void *) hdr;
c687bc
+        *avail = cap->avail;
c687bc
+    }
c687bc
+
c687bc
+    return true;
c687bc
+}
c687bc
+
c687bc
 static int vfio_setup_region_sparse_mmaps(VFIORegion *region,
c687bc
                                           struct vfio_region_info *info)
c687bc
 {
c687bc
diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h
c687bc
index fd564209ac7..aa6cbe4a998 100644
c687bc
--- a/include/hw/vfio/vfio-common.h
c687bc
+++ b/include/hw/vfio/vfio-common.h
c687bc
@@ -191,6 +191,8 @@ int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
c687bc
 bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type);
c687bc
 struct vfio_info_cap_header *
c687bc
 vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id);
c687bc
+bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info,
c687bc
+                             unsigned int *avail);
c687bc
 #endif
c687bc
 extern const MemoryListener vfio_prereg_listener;
c687bc
 
c687bc
-- 
c687bc
2.27.0
c687bc