rmtfs: Fetch rmtfs address from device-tree

The rmtfs address differes from platform to platform, so fetch these
from the device-tree node in /proc instead of hard coding them.

Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
This commit is contained in:
Bjorn Andersson 2016-02-08 16:30:25 -08:00
parent 6e467200c5
commit 2150e2ae74

View File

@ -1,4 +1,6 @@
#include <sys/mman.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
@ -8,11 +10,10 @@
#include <unistd.h>
#include "rmtfs.h"
#define SHAREDMEM_BASE 0x0fd80000
#define SHAREDMEM_SIZE 0x180000
static int rmtfs_mem_enumerate(void);
static uint64_t rmtfs_mem_address = SHAREDMEM_BASE;
static uint64_t rmtfs_mem_size = SHAREDMEM_SIZE;
static uint64_t rmtfs_mem_address;
static uint64_t rmtfs_mem_size;
static void *rmtfs_mem_base;
static bool rmtfs_mem_busy;
static int rmtfs_mem_fd;
@ -20,15 +21,20 @@ static int rmtfs_mem_fd;
int rmtfs_mem_open(void)
{
void *base;
int ret;
int fd;
ret = rmtfs_mem_enumerate();
if (ret < 0)
return ret;
fd = open("/dev/mem", O_RDWR|O_SYNC);
if (fd < 0) {
fprintf(stderr, "failed to open /dev/mem\n");
return fd;
}
base = mmap(0, SHAREDMEM_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, SHAREDMEM_BASE);
base = mmap(0, rmtfs_mem_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, rmtfs_mem_address);
if (base == MAP_FAILED) {
fprintf(stderr, "failed to mmap: %s\n", strerror(errno));
return -errno;
@ -86,3 +92,71 @@ void rmtfs_mem_close(void)
rmtfs_mem_fd = -1;
rmtfs_mem_base = MAP_FAILED;
}
static int rmtfs_mem_enumerate(void)
{
union {
uint32_t dw[2];
uint64_t qw[2];
} reg;
struct dirent *de;
int basefd;
int dirfd;
int regfd;
DIR *dir;
int ret = 0;
int n;
basefd = open("/proc/device-tree/reserved-memory/", O_DIRECTORY);
dir = fdopendir(basefd);
if (!dir) {
fprintf(stderr,
"Unable to open reserved-memory device tree node: %s\n",
strerror(-errno));
close(basefd);
return -1;
}
while ((de = readdir(dir)) != NULL) {
if (strncmp(de->d_name, "rmtfs", 5) != 0)
continue;
dirfd = openat(basefd, de->d_name, O_DIRECTORY);
if (dirfd < 0) {
fprintf(stderr, "failed to open %s: %s\n",
de->d_name, strerror(-errno));
ret = -1;
goto out;
}
regfd = openat(dirfd, "reg", O_RDONLY);
if (regfd < 0) {
fprintf(stderr, "failed to open reg of %s: %s\n",
de->d_name, strerror(-errno));
ret = -1;
goto out;
}
n = read(regfd, &reg, sizeof(reg));
if (n == 2 * sizeof(uint32_t)) {
rmtfs_mem_address = be32toh(reg.dw[0]);
rmtfs_mem_size = be32toh(reg.dw[1]);
} else if (n == 2 * sizeof(uint64_t)) {
rmtfs_mem_address = be64toh(reg.qw[0]);
rmtfs_mem_size = be64toh(reg.qw[1]);
} else {
fprintf(stderr, "failed to read reg of %s: %s\n",
de->d_name, strerror(-errno));
ret = -1;
}
close(regfd);
close(dirfd);
break;
}
out:
closedir(dir);
close(basefd);
return ret;
}