From 18e672c04c6b12d51b13f4cf32b9c197245407d6 Mon Sep 17 00:00:00 2001 From: zilio nicolas Date: Fri, 12 Jun 2015 16:56:21 +0200 Subject: added soft_reg --- protocols/CMakeLists.txt | 4 +- protocols/software_registers.c | 90 ++++++++++++++++++++++++++++++++++++++++++ protocols/software_registers.h | 58 +++++++++++++++++++++++++++ 3 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 protocols/software_registers.c create mode 100644 protocols/software_registers.h (limited to 'protocols') diff --git a/protocols/CMakeLists.txt b/protocols/CMakeLists.txt index 3c1d75b..2299061 100644 --- a/protocols/CMakeLists.txt +++ b/protocols/CMakeLists.txt @@ -3,6 +3,6 @@ include_directories( ${CMAKE_SOURCE_DIR}/pcilib ) -set(HEADERS ${HEADERS} default.h) +set(HEADERS ${HEADERS} default.h software_registers.h) -add_library(protocols STATIC default.c) +add_library(protocols STATIC default.c software_registers.c) diff --git a/protocols/software_registers.c b/protocols/software_registers.c new file mode 100644 index 0000000..d3c390f --- /dev/null +++ b/protocols/software_registers.c @@ -0,0 +1,90 @@ +#include +#include +#include + +#include "model.h" +#include "error.h" +#include "kmem.h" +#include "pcilib.h" +#include "pci.h" + + +/** + * pcilib_software_registers_open + * this function initializes the kernel space memory and stores in it the default values of the registers of the given bank index, if it was not initialized by a concurrent process, and return a bank context containing the adress of this kernel space. It the kernel space memory was already initialized by a concurrent process, then this function just return the bank context with the adress of this kernel space already used + *@param[in] ctx the pcilib_t structure running + * @param[in] bank the bank index that will permits to get the bank we want registers from + * @param[in] model not used + * @param[in] args not used + *@return a bank context with the adress of the kernel space memory related to it + */ +pcilib_register_bank_context_t* pcilib_software_registers_open(pcilib_t *ctx, pcilib_register_bank_t bank,const char* model, const void *args){ + pcilib_register_bank_context_t* bank_ctx; + pcilib_register_value_t *init=NULL; + pcilib_kmem_handle_t *test; + int i; + int j; + + bank_ctx=calloc(1,sizeof(pcilib_register_bank_context_t)); + bank_ctx->bank=ctx->banks + bank; + + test=pcilib_alloc_kernel_memory(ctx, 0, 0, 0, 0,PCILIB_KMEM_USE_STANDARD,PCILIB_KMEM_FLAG_REUSE|PCILIB_KMEM_FLAG_PERSISTENT); + if (!test)pcilib_error("allocation of kernel memory for registers has failed"); + + if(pcilib_kmem_is_reused(ctx,test)== PCILIB_KMEM_REUSE_ALLOCATED){ + bank_ctx->bank_software_register_adress=test; + }else{ + bank_ctx->bank_software_register_adress=test; + init=test; + j=0; + while(ctx->model_info.registers[j].name!=NULL){ + if(ctx->model_info.registers[j].bank==(ctx->banks + bank).addr){ + pcilib_write_register_by_id(ctx,ctx->model_info.registers[j],ctx->model_info.registers[j].defvalue); + } + j++; + } + } + return bank_ctx; +} + + +/** + * pcilib_software_registers_close + * this function clear the kernel memory space that could have been allocated for software registers + * @param[in] bank_ctx the bank context running that we get from the initialisation function + */ +void pcilib_software_registers_close(pcilib_register_bank_context_t *bank_ctx){ + int err=1; + + err=pcilib_clean_kernel_memory(bank_ctx->ctx, 0, 0); + + if(err) pcilib_error("Error closing register kernel space"); +} + +/** + * pcilib_software_registers_read + * this function read the value of a said register in the kernel space + * @param[in] ctx the pcilib_t structure runnning + * @param[in] bank_ctx the bank context that was returned by the initialisation function + * @param[in] addr the adress of the register we want to read + *@param[out] value the value of the register + * @return 0 in case of success + */ +int pcilib_software_registers_read(pcilib_t *ctx, pcilib_register_bank_context_t *bank_ctx,pcilib_register_addr_t addr, pcilib_register_value_t *value){ + *value= *(pcilib_register_value_t*) bank_ctx->bank_software_register_adress+addr; + return 0; +} + +/** + * pcilib_software_registers_write + * this function write the said value to a said register in the kernel space + * @param[in] ctx the pcilib_t structure runnning + * @param[in] bank_ctx the bank context that was returned by the initialisation function + * @param[in] addr the adress of the register we want to write in + *@param[out] value the value we want to write in the register + * @return 0 in case of success + */ +int pcilib_software_registers_write(pcilib_t *ctx, pcilib_register_bank_context_t *bank_ctx, pcilib_register_addr_t addr, pcilib_register_value_t value){ + *(pcilib_register_value_t*)(bank_ctx->bank_software_register_adress+addr)=value; + return 0; +} diff --git a/protocols/software_registers.h b/protocols/software_registers.h new file mode 100644 index 0000000..f31e292 --- /dev/null +++ b/protocols/software_registers.h @@ -0,0 +1,58 @@ +/** + * @file software_registers.h + * @skip author nicolas zilio, nicolas.zilio@hotmail.fr + * @brief header file for implementation of the protocol with registers registered in the kernel space. + */ + +#ifndef _PCILIB_SOFTREG_PROT +#define _PCILIB_SOFTREG_PROT + +#include "pcilib.h" +#include "version.h" +#include "model.h" + +/** + * this function initialize the kernel space memory for the use of software register. it initializes the kernel space memory and stores in it the default values of the registers of the given bank index, if it was not initialized by a concurrent process, and return a bank context containing the adress of this kernel space. If the kernel space memory was already initialized by a concurrent process, then this function just return the bank context with the adress of this kernel space already used + *@param[in] ctx the pcilib_t structure running + * @param[in] bank the bank index that will permits to get the bank we want registers from + * @param[in] model not used + * @param[in] args not used + *@return a bank context with the adress of the kernel space memory related to it + */ +pcilib_register_bank_context_t* pcilib_software_registers_open(pcilib_t *ctx, pcilib_register_bank_t bank, const char* model, const void *args); + +/** + * this function clear the kernel memory space that could have been allocated for software registers. + * @param[in] bank_ctx the bank context running that we get from the initialisation function + */ +void pcilib_software_registers_close(pcilib_register_bank_context_t *bank_ctx); + +/** + * this function read the value of a said register in the kernel space. + * @param[in] ctx the pcilib_t structure runnning + * @param[in] bank_ctx the bank context that was returned by the initialisation function + * @param[in] addr the adress of the register we want to read + *@param[out] value the value of the register + * @return 0 in case of success + */ +int pcilib_software_registers_read(pcilib_t *ctx, pcilib_register_bank_context_t* bank_ctx,pcilib_register_addr_t addr, pcilib_register_value_t *value); + +/** + * this function write the said value to a said register in the kernel space + * @param[in] ctx the pcilib_t structure runnning + * @param[in] bank_ctx the bank context that was returned by the initialisation function + * @param[in] addr the adress of the register we want to write in + *@param[out] value the value we want to write in the register + * @return 0 in case of success + */ +int pcilib_software_registers_write(pcilib_t *ctx,pcilib_register_bank_context_t* bank_ctx, pcilib_register_addr_t addr, pcilib_register_value_t value); + +#ifdef _PCILIB_EXPORT_C +/** + * new protocol addition to the protocol api. + */ +const pcilib_register_protocol_api_description_t pcilib_register_software_register_protocol_api = + { PCILIB_VERSION, pcilib_software_registers_open, pcilib_software_registers_close,pcilib_software_registers_read, pcilib_software_registers_write }; /**< we add there the protocol to the list of possible protocols*/ +#endif /* _PCILIB_EXPORT_C */ + +#endif /* _PCILIB_SOFTREG_PROT*/ -- cgit v1.2.3