pcilib.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #ifndef _PCITOOL_PCILIB_H
  2. #define _PCITOOL_PCILIB_H
  3. #include <sys/time.h>
  4. #include <stddef.h>
  5. #include <stdint.h>
  6. #include <stdarg.h>
  7. typedef struct pcilib_s pcilib_t;
  8. typedef struct pcilib_event_context_s pcilib_context_t;
  9. typedef uint32_t pcilib_version_t;
  10. typedef uint8_t pcilib_bar_t; /**< Type holding the PCI Bar number */
  11. typedef uint16_t pcilib_register_t; /**< Type holding the register position within the field listing registers in the model */
  12. typedef uint32_t pcilib_register_addr_t; /**< Type holding the register address within address-space of BARs */
  13. typedef uint8_t pcilib_register_size_t; /**< Type holding the size in bits of the register */
  14. typedef uint32_t pcilib_register_value_t; /**< Type holding the register value */
  15. typedef uint8_t pcilib_dma_engine_addr_t;
  16. typedef uint8_t pcilib_dma_engine_t;
  17. typedef uint64_t pcilib_event_id_t;
  18. typedef uint32_t pcilib_event_t;
  19. typedef uint64_t pcilib_timeout_t; /**< In microseconds */
  20. typedef unsigned int pcilib_irq_hw_source_t;
  21. typedef uint32_t pcilib_irq_source_t;
  22. typedef enum {
  23. PCILIB_LOG_DEBUG = 0, /**< Debug messages will be always printed as they should be filtered based on setting of corresponding environmental variable */
  24. PCILIB_LOG_INFO, /**< Informational message are suppresed by default */
  25. PCILIB_LOG_WARNING, /**< Warnings messages indicate that something unexpected happen, but application can continue */
  26. PCILIB_LOG_ERROR /**< The error which is impossible to handle on this level of library */
  27. } pcilib_log_priority_t;
  28. typedef enum {
  29. PCILIB_HOST_ENDIAN = 0,
  30. PCILIB_LITTLE_ENDIAN,
  31. PCILIB_BIG_ENDIAN
  32. } pcilib_endianess_t;
  33. typedef enum {
  34. PCILIB_DMA_IRQ = 1,
  35. PCILIB_EVENT_IRQ = 2
  36. } pcilib_irq_type_t;
  37. typedef enum { /**< 0x8000 and up are reserved for driver-specific types */
  38. PCILIB_EVENT_DATA = 0, /**< default data format */
  39. PCILIB_EVENT_RAW_DATA = 1 /**< raw data */
  40. } pcilib_event_data_type_t;
  41. typedef enum {
  42. PCILIB_DMA_TO_DEVICE = 1,
  43. PCILIB_DMA_FROM_DEVICE = 2,
  44. PCILIB_DMA_BIDIRECTIONAL = 3
  45. } pcilib_dma_direction_t;
  46. typedef enum {
  47. PCILIB_DMA_FLAGS_DEFAULT = 0,
  48. PCILIB_DMA_FLAG_EOP = 1, /**< last buffer of the packet */
  49. PCILIB_DMA_FLAG_WAIT = 2, /**< wait completion of write operation / wait for data during read operation */
  50. PCILIB_DMA_FLAG_MULTIPACKET = 4, /**< read multiple packets */
  51. PCILIB_DMA_FLAG_PERSISTENT = 8, /**< do not stop DMA engine on application termination / permanently close DMA engine on dma_stop */
  52. PCILIB_DMA_FLAG_IGNORE_ERRORS = 16 /**< do not crash on errors, but return appropriate error codes */
  53. } pcilib_dma_flags_t;
  54. typedef enum {
  55. PCILIB_EVENT_FLAGS_DEFAULT = 0,
  56. PCILIB_EVENT_FLAG_RAW_DATA_ONLY = 1, /**< Do not parse data, just read raw and pass it to rawdata callback. If passed to rawdata callback, idicates the data is not identified as event (most probably just padding) */
  57. PCILIB_EVENT_FLAG_STOP_ONLY = 1, /**< Do not cleanup, just stop acquiring new frames, the cleanup should be requested afterwards */
  58. PCILIB_EVENT_FLAG_EOF = 2, /**< Indicates that it is the last part of the frame (not required) */
  59. PCILIB_EVENT_FLAG_PREPROCESS = 4 /**< Enables preprocessing of the raw data (decoding frames, etc.) */
  60. } pcilib_event_flags_t;
  61. typedef enum {
  62. PCILIB_EVENT_INFO_FLAG_BROKEN = 1 /**< Indicates broken frames (if this flag is fales, the frame still can be broken) */
  63. } pcilib_event_info_flags_t;
  64. typedef struct {
  65. pcilib_event_t type;
  66. uint64_t seqnum; /**< we will add seqnum_overflow if required */
  67. uint64_t offset; /**< nanoseconds */
  68. struct timeval timestamp; /**< most accurate timestamp */
  69. pcilib_event_info_flags_t flags; /**< flags */
  70. } pcilib_event_info_t;
  71. #define PCILIB_BAR_DETECT ((pcilib_bar_t)-1)
  72. #define PCILIB_BAR_INVALID ((pcilib_bar_t)-1)
  73. #define PCILIB_BAR0 0
  74. #define PCILIB_BAR1 1
  75. #define PCILIB_DMA_ENGINE_INVALID ((pcilib_dma_engine_t)-1)
  76. #define PCILIB_DMA_ENGINE_ALL ((pcilib_dma_engine_t)-1)
  77. #define PCILIB_DMA_FLAGS_DEFAULT ((pcilib_dma_flags_t)0)
  78. #define PCILIB_DMA_ENGINE_ADDR_INVALID ((pcilib_dma_engine_addr_t)-1)
  79. #define PCILIB_REGISTER_INVALID ((pcilib_register_t)-1)
  80. #define PCILIB_ADDRESS_INVALID ((uintptr_t)-1)
  81. #define PCILIB_EVENT0 1
  82. #define PCILIB_EVENT1 2
  83. #define PCILIB_EVENT2 4
  84. #define PCILIB_EVENT3 8
  85. #define PCILIB_EVENTS_ALL ((pcilib_event_t)-1)
  86. #define PCILIB_EVENT_INVALID ((pcilib_event_t)-1)
  87. #define PCILIB_EVENT_DATA_TYPE_INVALID ((pcilib_event_data_type_t)-1)
  88. #define PCILIB_TIMEOUT_INFINITE ((pcilib_timeout_t)-1)
  89. #define PCILIB_TIMEOUT_IMMEDIATE 0
  90. #define PCILIB_IRQ_TYPE_ALL 0
  91. #define PCILIB_IRQ_SOURCE_DEFAULT 0
  92. #define PCILIB_MODEL_DETECT NULL
  93. typedef void (*pcilib_logger_t)(void *arg, const char *file, int line, pcilib_log_priority_t prio, const char *msg, va_list va);
  94. /**<
  95. * Callback function called when new data is read by DMA streaming function
  96. * @ctx - DMA Engine context
  97. * @flags - DMA Flags
  98. * @bufsize - size of data in bytes
  99. * @buf - data
  100. * @returns
  101. * <0 - error, stop streaming (the value is negative error code)
  102. * 0 - stop streaming (PCILIB_STREAMING_STOP)
  103. * 1 - wait DMA timeout and return gracefuly if no data (PCILIB_STREAMING_CONTINUE)
  104. * 2 - wait the specified timeout and return gracefuly if no data (PCILIB_STREAMING_WAIT)
  105. * 3 - check if more data is available without waiting, return gracefuly if not (PCILIB_STREAMING_CHECK)
  106. * 5 - wait DMA timeout and fail if no data (PCILIB_STREAMING_REQ_FRAGMENT)
  107. * 6 - wait the specified timeout and fail if no data (PCILIB_STREAMING_REQ_PACKET)
  108. */
  109. typedef int (*pcilib_dma_callback_t)(void *ctx, pcilib_dma_flags_t flags, size_t bufsize, void *buf);
  110. typedef int (*pcilib_event_callback_t)(pcilib_event_id_t event_id, pcilib_event_info_t *info, void *user);
  111. typedef int (*pcilib_event_rawdata_callback_t)(pcilib_event_id_t event_id, pcilib_event_info_t *info, pcilib_event_flags_t flags, size_t size, void *data, void *user);
  112. int pcilib_set_logger(pcilib_log_priority_t min_prio, pcilib_logger_t logger, void *arg);
  113. pcilib_t *pcilib_open(const char *device, const char *model);
  114. void pcilib_close(pcilib_t *ctx);
  115. int pcilib_start_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, pcilib_dma_flags_t flags);
  116. int pcilib_stop_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, pcilib_dma_flags_t flags);
  117. // Interrupt API is preliminary and can be significantly changed in future
  118. int pcilib_enable_irq(pcilib_t *ctx, pcilib_irq_type_t irq_type, pcilib_dma_flags_t flags);
  119. int pcilib_acknowledge_irq(pcilib_t *ctx, pcilib_irq_type_t irq_type, pcilib_irq_source_t irq_source);
  120. int pcilib_disable_irq(pcilib_t *ctx, pcilib_dma_flags_t flags);
  121. int pcilib_wait_irq(pcilib_t *ctx, pcilib_irq_hw_source_t source, pcilib_timeout_t timeout, size_t *count);
  122. int pcilib_clear_irq(pcilib_t *ctx, pcilib_irq_hw_source_t source);
  123. void *pcilib_map_bar(pcilib_t *ctx, pcilib_bar_t bar);
  124. void pcilib_unmap_bar(pcilib_t *ctx, pcilib_bar_t bar, void *data);
  125. char *pcilib_resolve_register_address(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr); // addr is offset if bar is specified
  126. char *pcilib_resolve_data_space(pcilib_t *ctx, uintptr_t addr, size_t *size);
  127. pcilib_register_t pcilib_find_register(pcilib_t *ctx, const char *bank, const char *reg);
  128. pcilib_event_t pcilib_find_event(pcilib_t *ctx, const char *event);
  129. pcilib_event_data_type_t pcilib_find_event_data_type(pcilib_t *ctx, pcilib_event_t event, const char *data_type);
  130. pcilib_dma_engine_t pcilib_find_dma_by_addr(pcilib_t *ctx, pcilib_dma_direction_t direction, pcilib_dma_engine_addr_t dma);
  131. int pcilib_read(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, size_t size, void *buf);
  132. int pcilib_write(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, size_t size, void *buf);
  133. int pcilib_write_fifo(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, uint8_t fifo_size, size_t n, void *buf);
  134. int pcilib_read_fifo(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, uint8_t fifo_size, size_t n, void *buf);
  135. int pcilib_skip_dma(pcilib_t *ctx, pcilib_dma_engine_t dma);
  136. int pcilib_stream_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, pcilib_dma_flags_t flags, pcilib_timeout_t timeout, pcilib_dma_callback_t cb, void *cbattr);
  137. int pcilib_push_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, pcilib_dma_flags_t flags, pcilib_timeout_t timeout, void *buf, size_t *written_bytes);
  138. int pcilib_read_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, void *buf, size_t *read_bytes);
  139. int pcilib_read_dma_custom(pcilib_t *ctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, pcilib_dma_flags_t flags, pcilib_timeout_t timeout, void *buf, size_t *read_bytes);
  140. int pcilib_write_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, void *buf, size_t *written_bytes);
  141. double pcilib_benchmark_dma(pcilib_t *ctx, pcilib_dma_engine_addr_t dma, uintptr_t addr, size_t size, size_t iterations, pcilib_dma_direction_t direction);
  142. int pcilib_read_register_space(pcilib_t *ctx, const char *bank, pcilib_register_addr_t addr, size_t n, pcilib_register_value_t *buf);
  143. int pcilib_write_register_space(pcilib_t *ctx, const char *bank, pcilib_register_addr_t addr, size_t n, pcilib_register_value_t *buf);
  144. int pcilib_read_register_by_id(pcilib_t *ctx, pcilib_register_t reg, pcilib_register_value_t *value);
  145. int pcilib_write_register_by_id(pcilib_t *ctx, pcilib_register_t reg, pcilib_register_value_t value);
  146. int pcilib_read_register(pcilib_t *ctx, const char *bank, const char *regname, pcilib_register_value_t *value);
  147. int pcilib_write_register(pcilib_t *ctx, const char *bank, const char *regname, pcilib_register_value_t value);
  148. int pcilib_reset(pcilib_t *ctx);
  149. int pcilib_trigger(pcilib_t *ctx, pcilib_event_t event, size_t trigger_size, void *trigger_data);
  150. /*
  151. * The recording of new events will be stopped after reaching max_events records
  152. * or when the specified amount of time is elapsed. However, the @pcilib_stop
  153. * function should be called still.
  154. * NOTE: This options may not be respected if the PCILIB_EVENT_FLAG_RAW_DATA_ONLY
  155. * is specified.
  156. */
  157. int pcilib_configure_autostop(pcilib_t *ctx, size_t max_events, pcilib_timeout_t duration);
  158. /*
  159. * Request auto-triggering while grabbing
  160. */
  161. int pcilib_configure_autotrigger(pcilib_t *ctx, pcilib_timeout_t interval, pcilib_event_t event, size_t trigger_size, void *trigger_data);
  162. /*
  163. * Request streaming the rawdata from the event engine. It is fastest way to acuqire data.
  164. * No memory copies will be performed and DMA buffers will be directly passed to the user
  165. * callback. However, to prevent data loss, no processing should be done on the data. The
  166. * user callback is only expected to copy data into the appropriate place and return control
  167. * to the event engine.
  168. * The performance can be boosted further by disabling any data processing within the event
  169. * engine. Just pass PCILIB_EVENT_FLAG_RAW_DATA_ONLY flag to the @pcilib_start function.
  170. */
  171. int pcilib_configure_rawdata_callback(pcilib_t *ctx, pcilib_event_rawdata_callback_t callback, void *user);
  172. /*
  173. * Configures maximal number of preprocessing threads. Actual amount of threads
  174. * may be bigger. For instance, additionaly a real-time reader thread will be
  175. * executed for most of hardware
  176. */
  177. int pcilib_configure_preprocessing_threads(pcilib_t *ctx, size_t max_threads);
  178. pcilib_context_t *pcilib_get_event_engine(pcilib_t *ctx);
  179. int pcilib_start(pcilib_t *ctx, pcilib_event_t event_mask, pcilib_event_flags_t flags);
  180. int pcilib_stop(pcilib_t *ctx, pcilib_event_flags_t flags);
  181. int pcilib_stream(pcilib_t *ctx, pcilib_event_callback_t callback, void *user);
  182. int pcilib_get_next_event(pcilib_t *ctx, pcilib_timeout_t timeout, pcilib_event_id_t *evid, size_t info_size, pcilib_event_info_t *info);
  183. int pcilib_copy_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t size, void *buf, size_t *retsize);
  184. int pcilib_copy_data_with_argument(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t arg_size, void *arg, size_t size, void *buf, size_t *retsize);
  185. void *pcilib_get_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t *size_or_err);
  186. void *pcilib_get_data_with_argument(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t arg_size, void *arg, size_t *size_or_err);
  187. /*
  188. * This function is provided to find potentially corrupted data. If the data is overwritten by
  189. * the time return_data is called it will return error.
  190. */
  191. int pcilib_return_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, void *data);
  192. /*
  193. * @param data - will be allocated and shuld be freed if NULL, otherwise used and size should contain correct size.
  194. * In case of failure the content of data is undefined.
  195. * @param timeout - will be autotriggered if NULL
  196. */
  197. int pcilib_grab(pcilib_t *ctx, pcilib_event_t event_mask, size_t *size, void **data, pcilib_timeout_t timeout);
  198. #endif /* _PCITOOL_PCILIB_H */