In order to implement VFIO_DEVICE_FEATURE_DMA_BUF, we first need
to identify the VFIO region and index the buffer (represented by
iovec) belongs to and then translate its addresses to offsets
within that region.
The qemu_ram_block_from_host() API gives us both the region and the
offset info we need to populate the dma ranges in order to invoke
this feature.
Cc: Alex Williamson <alex.williamson@redhat.com>
Cc: Cédric Le Goater <clg@redhat.com>
Signed-off-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
---
hw/vfio/device.c | 40 +++++++++++++++++++++++++++++++++++
include/hw/vfio/vfio-device.h | 14 ++++++++++++
2 files changed, 54 insertions(+)
diff --git a/hw/vfio/device.c b/hw/vfio/device.c
index 9ff73f9941..5417142482 100644
--- a/hw/vfio/device.c
+++ b/hw/vfio/device.c
@@ -21,6 +21,7 @@
#include "qemu/osdep.h"
#include <sys/ioctl.h>
+#include "system/ramblock.h"
#include "hw/vfio/vfio-device.h"
#include "hw/vfio/pci.h"
#include "hw/hw.h"
@@ -615,3 +616,42 @@ VFIODevice *vfio_device_lookup(MemoryRegion *mr)
}
return NULL;
}
+
+int vfio_device_create_dmabuf_fd(VFIODevice *vbasedev,
+ struct iovec *iov, unsigned int iov_cnt)
+{
+ g_autofree struct vfio_device_feature *feature = NULL;
+ struct vfio_device_feature_dma_buf *dma_buf;
+ ram_addr_t offset;
+ RAMBlock *rb;
+ size_t argsz;
+ int i, index;
+
+ argsz = sizeof(*feature) + sizeof (*dma_buf) +
+ sizeof(struct vfio_region_dma_range) * iov_cnt;
+ feature = g_malloc0(argsz);
+ dma_buf = (struct vfio_device_feature_dma_buf *)feature->data;
+
+ for (i = 0; i < iov_cnt; i++) {
+ rb = qemu_ram_block_from_host(iov[i].iov_base, false, &offset);
+ if (!rb) {
+ return -1;
+ }
+
+ index = vfio_get_region_index_from_mr(rb->mr);
+ if (index < 0) {
+ return -1;
+ }
+
+ dma_buf->region_index = index;
+ dma_buf->dma_ranges[i].offset = offset;
+ dma_buf->dma_ranges[i].length = iov[i].iov_len;
+ }
+
+ dma_buf->nr_ranges = iov_cnt;
+ dma_buf->open_flags = O_RDONLY | O_CLOEXEC;
+ feature->argsz = argsz;
+ feature->flags = VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_DMA_BUF;
+
+ return vbasedev->io_ops->device_feature(vbasedev, feature);
+}
diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h
index 2f8087f133..7fc2912f15 100644
--- a/include/hw/vfio/vfio-device.h
+++ b/include/hw/vfio/vfio-device.h
@@ -309,6 +309,20 @@ int vfio_get_region_index_from_mr(MemoryRegion *mr);
* Returns the VFIO device if found or NULL.
*/
VFIODevice *vfio_device_lookup(MemoryRegion *mr);
+
+/**
+ * Create and return a dmabuf fd by first translating the addresses in the
+ * iovec array into VFIO region offsets and then invoking the
+ * VFIO_DEVICE_FEATURE_DMA_BUF feature.
+ *
+ * @vbasedev: #VFIODevice to use
+ * @iov: array of iovec entries associated with a buffer
+ * @iov_cnt: number of entries in the iovec array
+ *
+ * Returns the newly created dmabuf fd or -1 on error.
+ */
+int vfio_device_create_dmabuf_fd(VFIODevice *vbasedev,
+ struct iovec *iov, unsigned int iov_cnt);
#endif
/* Returns 0 on success, or a negative errno. */
--
2.50.1