[common] linux: added support for kvmfr dmabuf

This commit is contained in:
Geoffrey McRae 2020-10-29 21:51:16 +11:00
parent 5081c3ea88
commit 0efe7dc63c
2 changed files with 50 additions and 106 deletions

View File

@ -3,6 +3,7 @@ project(lg_common_platform_code LANGUAGES C)
include_directories( include_directories(
${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/include
${PROJECT_TOP}
) )
add_library(lg_common_platform_code STATIC add_library(lg_common_platform_code STATIC

View File

@ -25,6 +25,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/ioctl.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
@ -32,58 +33,19 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include "common/debug.h" #include "common/debug.h"
#include "common/option.h" #include "common/option.h"
#include "common/stringutils.h" #include "common/stringutils.h"
#include "module/kvmfr.h"
struct IVSHMEMInfo struct IVSHMEMInfo
{ {
int fd; int devFd;
int dmaFd;
int size; int size;
}; };
static int uioOpenFile(const char * shmDevice, const char * file)
{
char * path;
alloc_sprintf(&path, "/sys/class/uio/%s/%s", shmDevice, file);
int fd = open(path, O_RDONLY);
if (fd < 0)
{
free(path);
return -1;
}
free(path);
return fd;
}
static char * uioGetName(const char * shmDevice)
{
int fd = uioOpenFile(shmDevice, "name");
if (fd < 0)
return NULL;
char * name = malloc(32);
int len = read(fd, name, 31);
if (len <= 0)
{
free(name);
close(fd);
return NULL;
}
name[len] = '\0';
close(fd);
while(len > 0 && name[len-1] == '\n')
{
--len;
name[len] = '\0';
}
return name;
}
static bool ivshmemDeviceValidator(struct Option * opt, const char ** error) static bool ivshmemDeviceValidator(struct Option * opt, const char ** error)
{ {
// if it's not a uio device, it must be a file on disk // if it's not a kvmfr device, it must be a file on disk
if (strlen(opt->value.x_string) > 3 && memcmp(opt->value.x_string, "uio", 3) != 0) if (strlen(opt->value.x_string) > 3 && memcmp(opt->value.x_string, "kvmfr", 5) != 0)
{ {
struct stat st; struct stat st;
if (stat(opt->value.x_string, &st) != 0) if (stat(opt->value.x_string, &st) != 0)
@ -94,21 +56,6 @@ static bool ivshmemDeviceValidator(struct Option * opt, const char ** error)
return true; return true;
} }
char * name = uioGetName(opt->value.x_string);
if (!name)
{
*error = "Failed to get the uio device name";
return false;
}
if (strcmp(name, "KVMFR") != 0)
{
free(name);
*error = "Device is not a KVMFR device";
return false;
}
free(name);
return true; return true;
} }
@ -116,7 +63,7 @@ static StringList ivshmemDeviceGetValues(struct Option * option)
{ {
StringList sl = stringlist_new(true); StringList sl = stringlist_new(true);
DIR * d = opendir("/sys/class/uio"); DIR * d = opendir("/sys/class/kvmfr");
if (!d) if (!d)
return sl; return sl;
@ -126,20 +73,11 @@ static StringList ivshmemDeviceGetValues(struct Option * option)
if (dir->d_name[0] == '.') if (dir->d_name[0] == '.')
continue; continue;
char * name = uioGetName(dir->d_name);
if (!name)
continue;
if (strcmp(name, "KVMFR") == 0)
{
char * devName; char * devName;
alloc_sprintf(&devName, "/dev/%s", dir->d_name); alloc_sprintf(&devName, "/dev/%s", dir->d_name);
stringlist_push(sl, devName); stringlist_push(sl, devName);
} }
free(name);
}
closedir(d); closedir(d);
return sl; return sl;
} }
@ -152,7 +90,7 @@ void ivshmemOptionsInit()
.module = "app", .module = "app",
.name = "shmFile", .name = "shmFile",
.shortopt = 'f', .shortopt = 'f',
.description = "The path to the shared memory file, or the name of the uio device to use, ie: uio0", .description = "The path to the shared memory file, or the name of the kvmfr device to use, ie: kvmfr0",
.type = OPTION_TYPE_STRING, .type = OPTION_TYPE_STRING,
.value.x_string = "/dev/shm/looking-glass", .value.x_string = "/dev/shm/looking-glass",
.validator = ivshmemDeviceValidator, .validator = ivshmemDeviceValidator,
@ -174,44 +112,42 @@ bool ivshmemOpenDev(struct IVSHMEM * dev, const char * shmDevice)
assert(dev); assert(dev);
unsigned int devSize; unsigned int devSize;
int devFD; int devFd = -1;
int dmaFd = -1;
int mapFd = -1;
dev->opaque = NULL; dev->opaque = NULL;
DEBUG_INFO("KVMFR Device : %s", shmDevice); DEBUG_INFO("KVMFR Device : %s", shmDevice);
if (strlen(shmDevice) > 8 && memcmp(shmDevice, "/dev/uio", 8) == 0) if (strlen(shmDevice) > 8 && memcmp(shmDevice, "/dev/kvmfr", 10) == 0)
{ {
const char * uioDev = shmDevice + 5; devFd = open(shmDevice, O_RDWR, (mode_t)0600);
if (devFd < 0)
// get the device size
int fd = uioOpenFile(uioDev, "maps/map0/size");
if (fd < 0)
{
DEBUG_ERROR("Failed to open %s/size", uioDev);
return false;
}
char size[32];
int len = read(fd, size, sizeof(size) - 1);
if (len <= 0)
{
DEBUG_ERROR("Failed to read the device size");
DEBUG_ERROR("%s", strerror(errno));
close(fd);
return false;
}
size[len] = '\0';
close(fd);
devSize = strtoul(size, NULL, 16);
devFD = open(shmDevice, O_RDWR, (mode_t)0600);
if (devFD < 0)
{ {
DEBUG_ERROR("Failed to open: %s", shmDevice); DEBUG_ERROR("Failed to open: %s", shmDevice);
DEBUG_ERROR("%s", strerror(errno)); DEBUG_ERROR("%s", strerror(errno));
return false; return false;
} }
// get the device size
devSize = ioctl(devFd, KVMFR_DMABUF_GETSIZE, 0);
const struct kvmfr_dmabuf_create create =
{
.flags = KVMFR_DMABUF_FLAG_CLOEXEC,
.offset = 0x0,
.size = devSize
};
dmaFd = ioctl(devFd, KVMFR_DMABUF_CREATE, &create);
if (dmaFd < 0)
{
DEBUG_ERROR("Failed to create the dma buffer");
close(devFd);
return false;
}
mapFd = dmaFd;
} }
else else
{ {
@ -224,16 +160,18 @@ bool ivshmemOpenDev(struct IVSHMEM * dev, const char * shmDevice)
} }
devSize = st.st_size; devSize = st.st_size;
devFD = open(shmDevice, O_RDWR, (mode_t)0600); devFd = open(shmDevice, O_RDWR, (mode_t)0600);
if (devFD < 0) if (devFd < 0)
{ {
DEBUG_ERROR("Failed to open: %s", shmDevice); DEBUG_ERROR("Failed to open: %s", shmDevice);
DEBUG_ERROR("%s", strerror(errno)); DEBUG_ERROR("%s", strerror(errno));
return false; return false;
} }
mapFd = devFd;
} }
void * map = mmap(0, devSize, PROT_READ | PROT_WRITE, MAP_SHARED, devFD, 0); void * map = mmap(0, devSize, PROT_READ | PROT_WRITE, MAP_SHARED, mapFd, 0);
if (map == MAP_FAILED) if (map == MAP_FAILED)
{ {
DEBUG_ERROR("Failed to map the shared memory device: %s", shmDevice); DEBUG_ERROR("Failed to map the shared memory device: %s", shmDevice);
@ -244,7 +182,8 @@ bool ivshmemOpenDev(struct IVSHMEM * dev, const char * shmDevice)
struct IVSHMEMInfo * info = struct IVSHMEMInfo * info =
(struct IVSHMEMInfo *)malloc(sizeof(struct IVSHMEMInfo)); (struct IVSHMEMInfo *)malloc(sizeof(struct IVSHMEMInfo));
info->size = devSize; info->size = devSize;
info->fd = devFD; info->devFd = devFd;
info->dmaFd = dmaFd;
dev->opaque = info; dev->opaque = info;
dev->size = devSize; dev->size = devSize;
@ -263,7 +202,11 @@ void ivshmemClose(struct IVSHMEM * dev)
(struct IVSHMEMInfo *)dev->opaque; (struct IVSHMEMInfo *)dev->opaque;
munmap(dev->mem, info->size); munmap(dev->mem, info->size);
close(info->fd);
if (info->dmaFd >= 0)
close(info->dmaFd);
close(info->devFd);
free(info); free(info);
dev->mem = NULL; dev->mem = NULL;