software.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/file.h>
  6. #include "model.h"
  7. #include "error.h"
  8. #include "kmem.h"
  9. #include "pcilib.h"
  10. #include "pci.h"
  11. typedef struct pcilib_software_register_bank_context_s pcilib_software_register_bank_context_t;
  12. /**
  13. * structure defining the context of software registers
  14. */
  15. struct pcilib_software_register_bank_context_s {
  16. pcilib_register_bank_context_t bank_ctx; /**< the bank context associated with the software registers */
  17. pcilib_kmem_handle_t *kmem; /**< the kernel memory for software registers */
  18. void *addr; /**< the virtual adress of the allocated kernel memory*/
  19. };
  20. void pcilib_software_registers_close(pcilib_t *ctx, pcilib_register_bank_context_t *bank_ctx) {
  21. if (((pcilib_software_register_bank_context_t*)bank_ctx)->kmem)
  22. pcilib_free_kernel_memory(ctx, ((pcilib_software_register_bank_context_t*)bank_ctx)->kmem, PCILIB_KMEM_FLAG_REUSE);
  23. free(bank_ctx);
  24. }
  25. pcilib_register_bank_context_t* pcilib_software_registers_open(pcilib_t *ctx, pcilib_register_bank_t bank, const char* model, const void *args) {
  26. int err;
  27. pcilib_software_register_bank_context_t *bank_ctx;
  28. pcilib_kmem_handle_t *handle;
  29. pcilib_lock_t *lock;
  30. pcilib_kmem_reuse_state_t reused;
  31. const pcilib_register_bank_description_t *bank_desc = ctx->banks + bank;
  32. if (bank_desc->size > PCILIB_KMEM_PAGE_SIZE) {
  33. pcilib_error("Currently software register banks are limited to %lu bytes, but %lu requested", PCILIB_KMEM_PAGE_SIZE, bank_desc->size);
  34. return NULL;
  35. }
  36. bank_ctx = calloc(1, sizeof(pcilib_software_register_bank_context_t));
  37. if (!bank_ctx) {
  38. pcilib_error("Memory allocation for bank context has failed");
  39. return NULL;
  40. }
  41. /*we get a lock to protect the creation of the kernel space for software registers against multiple creations*/
  42. lock = pcilib_get_lock(ctx, PCILIB_LOCK_FLAGS_DEFAULT, "softreg/%s", bank_desc->name);
  43. if (!lock) {
  44. pcilib_software_registers_close(ctx, (pcilib_register_bank_context_t*)bank_ctx);
  45. pcilib_error("Failed to initialize a lock to protect bank %s with software registers", bank_desc->name);
  46. return NULL;
  47. }
  48. err = pcilib_lock(lock);
  49. if (err) {
  50. pcilib_return_lock(ctx, PCILIB_LOCK_FLAGS_DEFAULT, lock);
  51. pcilib_software_registers_close(ctx, (pcilib_register_bank_context_t*)bank_ctx);
  52. pcilib_error("Error (%i) obtaining lock on bank %s with software registers", err, bank_desc->name);
  53. return NULL;
  54. }
  55. /*creation of the kernel space*/
  56. handle = pcilib_alloc_kernel_memory(ctx, PCILIB_KMEM_TYPE_PAGE, 1, PCILIB_KMEM_PAGE_SIZE, 0, PCILIB_KMEM_USE(PCILIB_KMEM_USE_SOFTWARE_REGISTERS, bank_desc->addr), PCILIB_KMEM_FLAG_REUSE|PCILIB_KMEM_FLAG_PERSISTENT);
  57. if (!handle) {
  58. pcilib_unlock(lock);
  59. pcilib_return_lock(ctx, PCILIB_LOCK_FLAGS_DEFAULT, lock);
  60. pcilib_software_registers_close(ctx, (pcilib_register_bank_context_t*)bank_ctx);
  61. pcilib_error("Allocation of kernel memory for software registers has failed");
  62. return NULL;
  63. }
  64. bank_ctx->kmem = handle;
  65. bank_ctx->addr = pcilib_kmem_get_block_ua(ctx, handle, 0);
  66. reused = pcilib_kmem_is_reused(ctx, handle);
  67. if ((reused & PCILIB_KMEM_REUSE_REUSED) == 0) {
  68. pcilib_register_t i;
  69. if (reused & PCILIB_KMEM_REUSE_PARTIAL) {
  70. pcilib_unlock(lock);
  71. pcilib_return_lock(ctx, PCILIB_LOCK_FLAGS_DEFAULT, lock);
  72. pcilib_software_registers_close(ctx, (pcilib_register_bank_context_t*)bank_ctx);
  73. pcilib_error("Inconsistent software registers are found (only part of required buffers is available)");
  74. return NULL;
  75. }
  76. /* here we fill the software registers with their default value*/
  77. for (i = 0; ctx->model_info.registers[i].name != NULL; i++) {
  78. if ((ctx->model_info.registers[i].bank == ctx->banks[bank].addr)&&(ctx->model_info.registers[i].type == PCILIB_REGISTER_STANDARD)) {
  79. *(pcilib_register_value_t*)(bank_ctx->addr + ctx->model_info.registers[i].addr) = ctx->model_info.registers[i].defvalue;
  80. }
  81. }
  82. }
  83. pcilib_unlock(lock);
  84. pcilib_return_lock(ctx, PCILIB_LOCK_FLAGS_DEFAULT, lock);
  85. return (pcilib_register_bank_context_t*)bank_ctx;
  86. }
  87. 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){
  88. if ((addr + sizeof(pcilib_register_value_t)) > bank_ctx->bank->size) {
  89. pcilib_error("Trying to access space outside of the define register bank (bank: %s, addr: 0x%lx)", bank_ctx->bank->name, addr);
  90. return PCILIB_ERROR_INVALID_ADDRESS;
  91. }
  92. // we consider this atomic operation and, therefore, do no locking
  93. *value = *(pcilib_register_value_t*)(((pcilib_software_register_bank_context_t*)bank_ctx)->addr + addr);
  94. return 0;
  95. }
  96. 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) {
  97. if ((addr + sizeof(pcilib_register_value_t)) > bank_ctx->bank->size) {
  98. pcilib_error("Trying to access space outside of the define register bank (bank: %s, addr: 0x%lx)", bank_ctx->bank->name, addr);
  99. return PCILIB_ERROR_INVALID_ADDRESS;
  100. }
  101. // we consider this atomic operation and, therefore, do no locking
  102. *(pcilib_register_value_t*)(((pcilib_software_register_bank_context_t*)bank_ctx)->addr + addr) = value;
  103. return 0;
  104. }