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 <[email protected]> Cc: Cédric Le Goater <[email protected]> Signed-off-by: Vivek Kasireddy <[email protected]> --- include/hw/vfio/vfio-device.h | 16 ++++++++++ hw/vfio/device.c | 59 +++++++++++++++++++++++++++++++++++ hw/vfio/dmabuf-stubs.c | 7 +++++ 3 files changed, 82 insertions(+) diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h index d46640ff53..14909490ef 100644 --- a/include/hw/vfio/vfio-device.h +++ b/include/hw/vfio/vfio-device.h @@ -331,6 +331,22 @@ int vfio_device_get_irq_info(VFIODevice *vbasedev, int index, */ int vfio_device_create_dmabuf_fd(struct iovec *iov, unsigned int iov_cnt, size_t total_size, 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 + * @total_size: total size of the dmabuf + * @errp: pointer to Error*, to store an error if it happens. + * + * Returns the mapped pointer on success and NULL on error. + */ +void *vfio_device_mmap_dmabuf(struct iovec *iov, unsigned int iov_cnt, + size_t total_size, Error **errp); #endif /* Returns 0 on success, or a negative errno. */ diff --git a/hw/vfio/device.c b/hw/vfio/device.c index 542c378913..43223ceacd 100644 --- a/hw/vfio/device.c +++ b/hw/vfio/device.c @@ -758,3 +758,62 @@ int vfio_device_create_dmabuf_fd(struct iovec *iov, unsigned int iov_cnt, } return ret; } + +void *vfio_device_mmap_dmabuf(struct iovec *iov, unsigned int iov_cnt, + size_t total_size, Error **errp) +{ + struct vfio_region_info *info = NULL; + ram_addr_t offset, len = 0; + VFIODevice *vbasedev; + VFIORegion *region; + RAMBlock *first_rb; + void *map, *submap; + int i; + + if (iov_size(iov, iov_cnt) != total_size) { + error_setg(errp, "Total size of iov does not match dmabuf size\n"); + return NULL; + } + + if (!vfio_device_lookup(iov, &vbasedev, &first_rb, errp)) { + return NULL; + } + + /* + * 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 NULL; + } + + for (i = 0; i < iov_cnt; i++) { + if (!vfio_region_lookup(iov[i].iov_base, ®ion, + first_rb, &offset, errp)) { + goto err; + } + + if (vfio_device_get_region_info(vbasedev, region->nr, &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_errno(errp, errno, "Could not mmap dmabuf segment\n"); + goto err; + } + len += iov[i].iov_len; + } + return map; +err: + munmap(map, total_size); + error_prepend(errp, "VFIO dmabuf mmap failed: "); + return NULL; +} + diff --git a/hw/vfio/dmabuf-stubs.c b/hw/vfio/dmabuf-stubs.c index 374bd2a188..044d1ed058 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; } + +void *vfio_device_mmap_dmabuf(struct iovec *iov, unsigned int iov_cnt, + size_t total_size, Error **errp) +{ + error_setg(errp, "VFIO mmap dmabuf support is not enabled"); + return NULL; +} -- 2.53.0
