From 5522e93fb9426665e6317c8e699f72f63095bba7 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Thu, 29 Oct 2020 22:56:39 +1100 Subject: [PATCH] [common] linux: added helpers for ivshmem DMA usage --- common/include/common/ivshmem.h | 5 ++++ common/src/platform/linux/ivshmem.c | 36 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/common/include/common/ivshmem.h b/common/include/common/ivshmem.h index da6834da..91b27637 100644 --- a/common/include/common/ivshmem.h +++ b/common/include/common/ivshmem.h @@ -20,6 +20,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA #pragma once #include +#include struct IVSHMEM { @@ -36,3 +37,7 @@ bool ivshmemOpen(struct IVSHMEM * dev); bool ivshmemOpenDev(struct IVSHMEM * dev, const char * shmDev); void ivshmemClose(struct IVSHMEM * dev); void ivshmemFree(struct IVSHMEM * dev); + +/* Linux KVMFR support only for now (VM->VM) */ +bool ivshmemHasDMA (struct IVSHMEM * dev); +int ivshmemGetDMABuf(struct IVSHMEM * dev, uint64_t offset, uint64_t size); diff --git a/common/src/platform/linux/ivshmem.c b/common/src/platform/linux/ivshmem.c index 19c27aa7..55e56cdd 100644 --- a/common/src/platform/linux/ivshmem.c +++ b/common/src/platform/linux/ivshmem.c @@ -213,3 +213,39 @@ void ivshmemClose(struct IVSHMEM * dev) dev->size = 0; dev->opaque = NULL; } + +bool ivshmemHasDMA(struct IVSHMEM * dev) +{ + assert(dev && dev->opaque); + + struct IVSHMEMInfo * info = + (struct IVSHMEMInfo *)dev->opaque; + + return info->dmaFd >= 0; +} + +int ivshmemGetDMABuf(struct IVSHMEM * dev, uint64_t offset, uint64_t size) +{ + assert(ivshmemHasDMA(dev)); + assert(dev && dev->opaque); + assert(offset + size <= dev->size); + + struct IVSHMEMInfo * info = + (struct IVSHMEMInfo *)dev->opaque; + + // align to the page size + size = (size & ~(0x1000-1)) + 0x1000; + + const struct kvmfr_dmabuf_create create = + { + .flags = KVMFR_DMABUF_FLAG_CLOEXEC, + .offset = offset, + .size = size + }; + + int fd = ioctl(info->devFd, KVMFR_DMABUF_CREATE, &create); + if (fd < 0) + DEBUG_ERROR("Failed to create the dma buffer"); + + return fd; +}