From 00541db077c93e500463b9bfd6c1b34f5a6a6239 Mon Sep 17 00:00:00 2001 From: Berin Martini Date: Fri, 9 Jan 2015 13:44:46 -0500 Subject: [PATCH] Add function to library to allocate CMA memory Arrays that will be used to pass data between the host and fpga need to be page aligned and be slices of the DMA array the driver has reserved. These new functions allocate slices from the DMA array and ensures that the starting address is page aligned. --- lib/interface.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ lib/interface.h | 6 ++++++ 2 files changed, 51 insertions(+) diff --git a/lib/interface.c b/lib/interface.c index c7ef85e..d24b462 100644 --- a/lib/interface.c +++ b/lib/interface.c @@ -58,3 +58,48 @@ int cfg_read(unsigned int addr) return *reg; } + +void *mem_alloc(const int length, const int byte_nb) +{ + assert(mem); + + // calculate start of next array + int next_offset = mem_offset + mem_alloc_size(length, byte_nb); + + if (MEM_SIZE < next_offset) { + fprintf(stderr, + "ERROR attempted total memory allocation: %i bytes\n", + next_offset); + assert(0); + } + // pointer to new array + void *ptr = &mem[mem_offset]; + + // update memory pointer to the next free area + mem_offset = next_offset; + + return ptr; +} + +int mem_alloc_size(const int length, const int byte_nb) +{ + assert(length); + assert(byte_nb); + + int size = length * byte_nb; + int remainder = size % sysconf(_SC_PAGESIZE); + + if (0 != remainder) { + size = size + sysconf(_SC_PAGESIZE) - remainder; + } + + return size; +} + +int mem_alloc_length(const int length, const int byte_nb) +{ + assert(length); + assert(byte_nb); + + return (mem_alloc_size(length, byte_nb) / byte_nb); +} diff --git a/lib/interface.h b/lib/interface.h index 4c87c1b..b19e582 100644 --- a/lib/interface.h +++ b/lib/interface.h @@ -18,6 +18,12 @@ extern "C" { int cfg_read(unsigned int addr); + // dma memory + void *mem_alloc(const int length, const int byte_nb); + + int mem_alloc_size(const int length, const int byte_nb); + + int mem_alloc_length(const int length, const int byte_nb); #ifdef __cplusplus }