diff options
author | Suren A. Chilingaryan <csa@suren.me> | 2016-05-14 01:10:33 +0200 |
---|---|---|
committer | Suren A. Chilingaryan <csa@suren.me> | 2016-05-14 01:10:33 +0200 |
commit | a21dea7a0d06cbdfe9a94c88b1ba99425bf2e102 (patch) | |
tree | 1c08af911cef8869ed1eb90902f89db83b524c53 /pcilib | |
parent | 71f64d82453f5c86dd40104839d98fc12ae44ddf (diff) | |
download | pcitool-a21dea7a0d06cbdfe9a94c88b1ba99425bf2e102.tar.gz pcitool-a21dea7a0d06cbdfe9a94c88b1ba99425bf2e102.tar.bz2 pcitool-a21dea7a0d06cbdfe9a94c88b1ba99425bf2e102.tar.xz pcitool-a21dea7a0d06cbdfe9a94c88b1ba99425bf2e102.zip |
Allow mapping of arbitrary memory areas
Diffstat (limited to 'pcilib')
-rw-r--r-- | pcilib/CMakeLists.txt | 6 | ||||
-rw-r--r-- | pcilib/mem.c | 52 | ||||
-rw-r--r-- | pcilib/mem.h | 29 |
3 files changed, 84 insertions, 3 deletions
diff --git a/pcilib/CMakeLists.txt b/pcilib/CMakeLists.txt index 2af60b4..d90df49 100644 --- a/pcilib/CMakeLists.txt +++ b/pcilib/CMakeLists.txt @@ -8,8 +8,8 @@ include_directories( ${UTHASH_INCLUDE_DIRS} ) -set(HEADERS pcilib.h pci.h datacpy.h memcpy.h pagecpy.h cpu.h timing.h export.h value.h bar.h fifo.h model.h bank.h register.h view.h property.h unit.h xml.h py.h kmem.h irq.h locking.h lock.h dma.h event.h plugin.h tools.h error.h debug.h env.h config.h version.h build.h) -add_library(pcilib SHARED pci.c datacpy.c memcpy.c pagecpy.c cpu.c timing.c export.c value.c bar.c fifo.c model.c bank.c register.c view.c unit.c property.c xml.c py.c kmem.c irq.c locking.c lock.c dma.c event.c plugin.c tools.c error.c debug.c env.c) +set(HEADERS pcilib.h pci.h datacpy.h memcpy.h pagecpy.h cpu.h timing.h export.h value.h mem.h bar.h fifo.h model.h bank.h register.h view.h property.h unit.h xml.h py.h kmem.h irq.h locking.h lock.h dma.h event.h plugin.h tools.h error.h debug.h env.h config.h version.h build.h) +add_library(pcilib SHARED pci.c datacpy.c memcpy.c pagecpy.c cpu.c timing.c export.c value.c mem.c bar.c fifo.c model.c bank.c register.c view.c unit.c property.c xml.c py.c kmem.c irq.c locking.c lock.c dma.c event.c plugin.c tools.c error.c debug.c env.c) target_link_libraries(pcilib dma protocols views ${CMAKE_THREAD_LIBS_INIT} ${UFODECODE_LIBRARIES} ${CMAKE_DL_LIBS} ${EXTRA_SYSTEM_LIBS} ${LIBXML2_LIBRARIES} ${PYTHON_LIBRARIES}) add_dependencies(pcilib dma protocols views) @@ -21,7 +21,7 @@ install(FILES pcilib.h DESTINATION include ) -install(FILES bar.h kmem.h locking.h lock.h bank.h register.h xml.h dma.h event.h model.h error.h debug.h env.h tools.h timing.h cpu.h datacpy.h pagecpy.h memcpy.h export.h view.h unit.h +install(FILES mem.h bar.h kmem.h locking.h lock.h bank.h register.h xml.h dma.h event.h model.h error.h debug.h env.h tools.h timing.h cpu.h datacpy.h pagecpy.h memcpy.h export.h view.h unit.h DESTINATION include/pcilib ) diff --git a/pcilib/mem.c b/pcilib/mem.c new file mode 100644 index 0000000..d8f6391 --- /dev/null +++ b/pcilib/mem.c @@ -0,0 +1,52 @@ +#include <stdio.h> +#include <string.h> +#include <strings.h> +#include <stdlib.h> +#include <stdint.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/ioctl.h> +#include <sys/mman.h> +#include <errno.h> +#include <assert.h> + +#include "pcilib.h" +#include "locking.h" +#include "mem.h" +#include "error.h" +#include "pci.h" + + + +void *pcilib_map_area(pcilib_t *ctx, uintptr_t addr, size_t size) { + void *res; + int err, ret; + + err = pcilib_lock_global(ctx); + if (err) { + pcilib_error("Error (%i) acquiring mmap lock", err); + return NULL; + } + + ret = ioctl( ctx->handle, PCIDRIVER_IOC_MMAP_MODE, PCIDRIVER_MMAP_AREA ); + if (ret) { + pcilib_unlock_global(ctx); + pcilib_error("PCIDRIVER_IOC_MMAP_MODE ioctl have failed"); + return NULL; + } + + res = mmap( 0, size, PROT_WRITE | PROT_READ, MAP_SHARED, ctx->handle, addr ); + + pcilib_unlock_global(ctx); + + if ((!res)||(res == MAP_FAILED)) { + pcilib_error("Failed to mmap area 0x%lx of size %zu bytes", addr, size); + return NULL; + } + + return res; +} + +void pcilib_unmap_area(pcilib_t *ctx, void *addr, size_t size) { + munmap(addr, size); +} diff --git a/pcilib/mem.h b/pcilib/mem.h new file mode 100644 index 0000000..2e3ac02 --- /dev/null +++ b/pcilib/mem.h @@ -0,0 +1,29 @@ +#ifndef _PCILIB_MEM_H +#define _PCILIB_MEM_H + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Maps the specified memory area in the address space of the process. + * @param[in,out] ctx - pcilib context + * @param[in] addr - hardware address (should be page-aligned) + * @param[in] size - size (should be multiple of page size) + * return - the address where the memory area is mapped + */ +void *pcilib_map_area(pcilib_t *ctx, uintptr_t addr, size_t size); + +/** + * Unmaps the specified memory area in the address space of the process. + * @param[in,out] ctx - pcilib context + * @param[in] addr - pointer to the virtual address where the area is mapped + * @param[in] size - size (should be multiple of page size) + */ +void pcilib_unmap_area(pcilib_t *ctx, void *addr, size_t size); + +#ifdef __cplusplus +} +#endif + +#endif /* _PCILIB_MEM_H */ |