In order to mmap a dmabuf, we first need to create a placeholder
mapping that would fit the entire size of the dmabuf. This mapping
would then be replaced with smaller mappings of individual dmabuf
segments.
Cc: Alex Williamson <alex@shazbot.org>
Cc: Cédric Le Goater <clg@redhat.com>
Signed-off-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
---
hw/vfio/device.c | 65 +++++++++++++++++++++++++++++++++++
hw/vfio/dmabuf-stubs.c | 7 ++++
include/hw/vfio/vfio-device.h | 17 +++++++++
3 files changed, 89 insertions(+)
diff --git a/hw/vfio/device.c b/hw/vfio/device.c
index a25ecc3697..596fbceb68 100644
--- a/hw/vfio/device.c
+++ b/hw/vfio/device.c
@@ -728,3 +728,68 @@ int vfio_device_create_dmabuf_fd(struct iovec *iov, unsigned int iov_cnt,
}
return ret;
}
+
+bool vfio_device_mmap_dmabuf(struct iovec *iov, unsigned int iov_cnt,
+ void **addr, size_t total_size, Error **errp)
+{
+ struct vfio_region_info *info = NULL;
+ VFIODevice *vbasedev = NULL;
+ ram_addr_t offset, len = 0;
+ RAMBlock *first_rb, *rb;
+ void *map, *submap;
+ int i, index;
+
+ if (!vfio_device_lookup(iov, &vbasedev, errp)) {
+ return false;
+ }
+
+ /*
+ * We first reserve a contiguous chunk of address space for the entire
+ * dmabuf, then replace it with smaller mappings that correspond to the
+ * individual segments of the dmabuf.
+ */
+ map = mmap(NULL, total_size, PROT_NONE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+ if (map == MAP_FAILED) {
+ error_setg_errno(errp, errno, "Could not reserve placeholder mapping");
+ return false;
+ }
+
+ for (i = 0; i < iov_cnt; i++) {
+ rb = qemu_ram_block_from_host(iov[i].iov_base, false, &offset);
+ if (i == 0) {
+ first_rb = rb;
+ }
+ if (!rb || rb != first_rb) {
+ error_setg(errp, "Cannot mmap dmabuf with different regions\n");
+ goto err;
+ }
+
+ index = vfio_get_region_index_from_mr(rb->mr);
+ if (index < 0) {
+ error_setg(errp, "Cannot find region index for dmabuf segment\n");
+ goto err;
+ }
+
+ if (vfio_device_get_region_info(vbasedev, index, &info)) {
+ error_setg(errp, "Cannot find region info for dmabuf segment\n");
+ goto err;
+ }
+
+ submap = mmap(map + len, iov[i].iov_len, PROT_READ,
+ MAP_SHARED | MAP_FIXED, vbasedev->fd,
+ info->offset + offset);
+ if (submap == MAP_FAILED) {
+ error_setg(errp, "Could not mmap dmabuf segment\n");
+ goto err;
+ }
+
+ len += iov[i].iov_len;
+ }
+ *addr = map;
+ return true;
+err:
+ munmap(map, total_size);
+ error_prepend(errp, "VFIO dmabuf mmap failed: ");
+ return false;
+}
+
diff --git a/hw/vfio/dmabuf-stubs.c b/hw/vfio/dmabuf-stubs.c
index e71e2b68dd..c3aa37233d 100644
--- a/hw/vfio/dmabuf-stubs.c
+++ b/hw/vfio/dmabuf-stubs.c
@@ -15,3 +15,10 @@ int vfio_device_create_dmabuf_fd(struct iovec *iov, unsigned int iov_cnt,
error_setg(errp, "VFIO dmabuf support is not enabled");
return VFIO_DMABUF_CREATE_HOST_ERROR;
}
+
+bool vfio_device_mmap_dmabuf(struct iovec *iov, unsigned int iov_cnt,
+ void **addr, size_t total_size, Error **errp)
+{
+ error_setg(errp, "VFIO mmap dmabuf support is not enabled");
+ return false;
+}
diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h
index 9dcf3ea57d..a1381f5f30 100644
--- a/include/hw/vfio/vfio-device.h
+++ b/include/hw/vfio/vfio-device.h
@@ -340,6 +340,23 @@ int vfio_get_region_index_from_mr(MemoryRegion *mr);
*/
int vfio_device_create_dmabuf_fd(struct iovec *iov, unsigned int iov_cnt,
Error **errp);
+
+/**
+ * Mmap a dmabuf by first translating the addresses in the iovec
+ * array into VFIO region offsets and then creating a placeholder
+ * mapping that would be replaced later with mappings that
+ * correspond to the dmabuf segments.
+ *
+ * @iov: array of iovec entries associated with the dmabuf
+ * @iov_cnt: number of entries in the iovec array
+ * @addr: pointer to store the resulting mmap address
+ * @total_size: total size of the dmabuf
+ * @errp: pointer to Error*, to store an error if it happens.
+ *
+ * Returns true on success and false in case of an error.
+ */
+bool vfio_device_mmap_dmabuf(struct iovec *iov, unsigned int iov_cnt,
+ void **addr, size_t total_size, Error **errp);
#endif
/* Returns 0 on success, or a negative errno. */
--
2.53.0