[common] linux: added helpers for ivshmem DMA usage

This commit is contained in:
Geoffrey McRae 2020-10-29 22:56:39 +11:00
parent 0efe7dc63c
commit 5522e93fb9
2 changed files with 41 additions and 0 deletions

View file

@ -20,6 +20,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#pragma once
#include <stdbool.h>
#include <stdint.h>
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);

View file

@ -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;
}