yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
8fced6
From 3927f54a56e29003b84e0e3726d3a0170681128b Mon Sep 17 00:00:00 2001
8fced6
From: Cornelia Huck <cohuck@redhat.com>
8fced6
Date: Tue, 19 Jan 2021 12:50:44 -0500
8fced6
Subject: [PATCH 5/7] s390x/pci: Add routine to get the vfio dma available
8fced6
 count
8fced6
8fced6
RH-Author: Cornelia Huck <cohuck@redhat.com>
8fced6
Message-id: <20210119125046.472811-6-cohuck@redhat.com>
8fced6
Patchwork-id: 100679
8fced6
O-Subject: [RHEL-8.4.0 qemu-kvm PATCH 5/7] s390x/pci: Add routine to get the vfio dma available count
8fced6
Bugzilla: 1905391
8fced6
RH-Acked-by: David Hildenbrand <david@redhat.com>
8fced6
RH-Acked-by: Auger Eric <eric.auger@redhat.com>
8fced6
RH-Acked-by: Thomas Huth <thuth@redhat.com>
8fced6
8fced6
From: Matthew Rosato <mjrosato@linux.ibm.com>
8fced6
8fced6
Create new files for separating out vfio-specific work for s390
8fced6
pci. Add the first such routine, which issues VFIO_IOMMU_GET_INFO
8fced6
ioctl to collect the current dma available count.
8fced6
8fced6
Signed-off-by: Matthew Rosato <mjrosato@linux.ibm.com>
8fced6
Reviewed-by: Cornelia Huck <cohuck@redhat.com>
8fced6
[aw: Fix non-Linux build with CONFIG_LINUX]
8fced6
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
8fced6
(cherry picked from commit cd7498d07fbb20fa04790ff7ee168a8a8d01cb30)
8fced6
Signed-off-by: Cornelia Huck <cohuck@redhat.com>
8fced6
8fced6
 Conflicts:
8fced6
	hw/s390x/meson.build
8fced6
        --> added the file in hw/s390x/Makefile.objs instead,
8fced6
            since we do not use Meson yet
8fced6
        hw/s390x/s390-pci-vfio.c
8fced6
        --> NULL-initialize "info" to avoid a downstream-only
8fced6
            compiler warning
8fced6
8fced6
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
8fced6
---
8fced6
 hw/s390x/Makefile.objs           |  1 +
8fced6
 hw/s390x/s390-pci-vfio.c         | 54 ++++++++++++++++++++++++++++++++
8fced6
 include/hw/s390x/s390-pci-vfio.h | 24 ++++++++++++++
8fced6
 3 files changed, 79 insertions(+)
8fced6
 create mode 100644 hw/s390x/s390-pci-vfio.c
8fced6
 create mode 100644 include/hw/s390x/s390-pci-vfio.h
8fced6
8fced6
diff --git a/hw/s390x/Makefile.objs b/hw/s390x/Makefile.objs
8fced6
index c4086ec3171..43756c9437d 100644
8fced6
--- a/hw/s390x/Makefile.objs
8fced6
+++ b/hw/s390x/Makefile.objs
8fced6
@@ -7,6 +7,7 @@ obj-y += ipl.o
8fced6
 obj-y += css.o
8fced6
 obj-$(CONFIG_S390_CCW_VIRTIO) += s390-virtio-ccw.o
8fced6
 obj-$(CONFIG_TERMINAL3270) += 3270-ccw.o
8fced6
+obj-$(CONFIG_LINUX) += s390-pci-vfio.o
8fced6
 ifeq ($(CONFIG_VIRTIO_CCW),y)
8fced6
 obj-y += virtio-ccw.o
8fced6
 obj-$(CONFIG_VIRTIO_SERIAL) += virtio-ccw-serial.o
8fced6
diff --git a/hw/s390x/s390-pci-vfio.c b/hw/s390x/s390-pci-vfio.c
8fced6
new file mode 100644
8fced6
index 00000000000..0eb22ffec4c
8fced6
--- /dev/null
8fced6
+++ b/hw/s390x/s390-pci-vfio.c
8fced6
@@ -0,0 +1,54 @@
8fced6
+/*
8fced6
+ * s390 vfio-pci interfaces
8fced6
+ *
8fced6
+ * Copyright 2020 IBM Corp.
8fced6
+ * Author(s): Matthew Rosato <mjrosato@linux.ibm.com>
8fced6
+ *
8fced6
+ * This work is licensed under the terms of the GNU GPL, version 2 or (at
8fced6
+ * your option) any later version. See the COPYING file in the top-level
8fced6
+ * directory.
8fced6
+ */
8fced6
+
8fced6
+#include <sys/ioctl.h>
8fced6
+
8fced6
+#include "qemu/osdep.h"
8fced6
+#include "hw/s390x/s390-pci-vfio.h"
8fced6
+#include "hw/vfio/vfio-common.h"
8fced6
+
8fced6
+/*
8fced6
+ * Get the current DMA available count from vfio.  Returns true if vfio is
8fced6
+ * limiting DMA requests, false otherwise.  The current available count read
8fced6
+ * from vfio is returned in avail.
8fced6
+ */
8fced6
+bool s390_pci_update_dma_avail(int fd, unsigned int *avail)
8fced6
+{
8fced6
+    g_autofree struct vfio_iommu_type1_info *info = NULL;
8fced6
+    uint32_t argsz;
8fced6
+
8fced6
+    assert(avail);
8fced6
+
8fced6
+    argsz = sizeof(struct vfio_iommu_type1_info);
8fced6
+    info = g_malloc0(argsz);
8fced6
+
8fced6
+    /*
8fced6
+     * If the specified argsz is not large enough to contain all capabilities
8fced6
+     * it will be updated upon return from the ioctl.  Retry until we have
8fced6
+     * a big enough buffer to hold the entire capability chain.
8fced6
+     */
8fced6
+retry:
8fced6
+    info->argsz = argsz;
8fced6
+
8fced6
+    if (ioctl(fd, VFIO_IOMMU_GET_INFO, info)) {
8fced6
+        return false;
8fced6
+    }
8fced6
+
8fced6
+    if (info->argsz > argsz) {
8fced6
+        argsz = info->argsz;
8fced6
+        info = g_realloc(info, argsz);
8fced6
+        goto retry;
8fced6
+    }
8fced6
+
8fced6
+    /* If the capability exists, update with the current value */
8fced6
+    return vfio_get_info_dma_avail(info, avail);
8fced6
+}
8fced6
+
8fced6
diff --git a/include/hw/s390x/s390-pci-vfio.h b/include/hw/s390x/s390-pci-vfio.h
8fced6
new file mode 100644
8fced6
index 00000000000..1727292e9b5
8fced6
--- /dev/null
8fced6
+++ b/include/hw/s390x/s390-pci-vfio.h
8fced6
@@ -0,0 +1,24 @@
8fced6
+/*
8fced6
+ * s390 vfio-pci interfaces
8fced6
+ *
8fced6
+ * Copyright 2020 IBM Corp.
8fced6
+ * Author(s): Matthew Rosato <mjrosato@linux.ibm.com>
8fced6
+ *
8fced6
+ * This work is licensed under the terms of the GNU GPL, version 2 or (at
8fced6
+ * your option) any later version. See the COPYING file in the top-level
8fced6
+ * directory.
8fced6
+ */
8fced6
+
8fced6
+#ifndef HW_S390_PCI_VFIO_H
8fced6
+#define HW_S390_PCI_VFIO_H
8fced6
+
8fced6
+#ifdef CONFIG_LINUX
8fced6
+bool s390_pci_update_dma_avail(int fd, unsigned int *avail);
8fced6
+#else
8fced6
+static inline bool s390_pci_update_dma_avail(int fd, unsigned int *avail)
8fced6
+{
8fced6
+    return false;
8fced6
+}
8fced6
+#endif
8fced6
+
8fced6
+#endif
8fced6
-- 
8fced6
2.27.0
8fced6