pcilib.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #ifndef _PCITOOL_PCILIB_H
  2. #define _PCITOOL_PCILIB_H
  3. #define PCILIB_MAX_BANKS 6
  4. #define PCILIB_MAX_DMA_ENGINES 32
  5. #include <sys/time.h>
  6. #include <stdint.h>
  7. #define pcilib_memcpy pcilib_memcpy32
  8. #define pcilib_datacpy pcilib_datacpy32
  9. typedef struct pcilib_s pcilib_t;
  10. typedef struct pcilib_event_context_s pcilib_context_t;
  11. typedef struct pcilib_dma_context_s pcilib_dma_context_t;
  12. typedef struct pcilib_dma_api_description_s pcilib_dma_api_description_t;
  13. typedef struct pcilib_event_api_description_s pcilib_event_api_description_t;
  14. typedef struct pcilib_protocol_description_s pcilib_protocol_description_t;
  15. typedef unsigned int pcilib_irq_hw_source_t;
  16. typedef uint32_t pcilib_irq_source_t;
  17. typedef uint8_t pcilib_bar_t; /**< Type holding the PCI Bar number */
  18. typedef uint8_t pcilib_register_t; /**< Type holding the register ID within the Bank */
  19. typedef uint32_t pcilib_register_addr_t; /**< Type holding the register ID within the Bank */
  20. typedef uint8_t pcilib_register_bank_t; /**< Type holding the register bank number */
  21. typedef uint8_t pcilib_register_bank_addr_t; /**< Type holding the register bank number */
  22. typedef uint8_t pcilib_register_size_t; /**< Type holding the size in bits of the register */
  23. typedef uint32_t pcilib_register_value_t; /**< Type holding the register value */
  24. typedef uint8_t pcilib_dma_engine_addr_t;
  25. typedef uint8_t pcilib_dma_engine_t;
  26. typedef uint64_t pcilib_event_id_t;
  27. typedef uint32_t pcilib_event_t;
  28. typedef uint64_t pcilib_timeout_t;
  29. typedef enum {
  30. PCILIB_HOST_ENDIAN = 0,
  31. PCILIB_LITTLE_ENDIAN,
  32. PCILIB_BIG_ENDIAN
  33. } pcilib_endianess_t;
  34. typedef enum {
  35. PCILIB_MODEL_DETECT,
  36. PCILIB_MODEL_PCI,
  37. PCILIB_MODEL_IPECAMERA
  38. } pcilib_model_t;
  39. typedef enum {
  40. PCILIB_REGISTER_R = 1,
  41. PCILIB_REGISTER_W = 2,
  42. PCILIB_REGISTER_RW = 3,
  43. PCILIB_REGISTER_W1C = 4, /**< writting 1 resets the flag */
  44. PCILIB_REGISTER_RW1C = 5
  45. } pcilib_register_mode_t;
  46. typedef enum {
  47. PCILIB_DEFAULT_PROTOCOL,
  48. IPECAMERA_REGISTER_PROTOCOL
  49. } pcilib_register_protocol_t;
  50. typedef enum {
  51. PCILIB_EVENT_DATA = 0, /**< default data format */
  52. PCILIB_EVENT_RAW_DATA = 1 /**< raw data */
  53. } pcilib_event_data_type_t;
  54. typedef enum {
  55. PCILIB_DMA_FLAGS_DEFAULT = 0,
  56. PCILIB_DMA_FLAG_EOP = 1, /**< last buffer of the packet */
  57. PCILIB_DMA_FLAG_WAIT = 2, /**< wait completion of write operation / wait for data during read operation */
  58. PCILIB_DMA_FLAG_MULTIPACKET = 4, /**< read multiple packets */
  59. PCILIB_DMA_FLAG_PERSISTENT = 8, /**< do not stop DMA engine on application termination / permanently close DMA engine on dma_stop */
  60. PCILIB_DMA_FLAG_IGNORE_ERRORS = 16 /**< do not crash on errors, but return appropriate error codes */
  61. } pcilib_dma_flags_t;
  62. typedef enum {
  63. PCILIB_STREAMING_STOP = 0, /**< stop streaming */
  64. PCILIB_STREAMING_CONTINUE = 1, /**< wait the default DMA timeout for a new data */
  65. PCILIB_STREAMING_WAIT = 2, /**< wait the specified timeout for a new data */
  66. PCILIB_STREAMING_CHECK = 3, /**< do not wait for the data, bail out imideatly if no data ready */
  67. PCILIB_STREAMING_FAIL = 4, /**< fail if data is not available on timeout */
  68. PCILIB_STREAMING_REQ_FRAGMENT = 5, /**< only fragment of a packet is read, wait for next fragment and fail if no data during DMA timeout */
  69. PCILIB_STREAMING_REQ_PACKET = 6, /**< wait for next packet and fail if no data during the specified timeout */
  70. PCILIB_STREAMING_TIMEOUT_MASK = 3 /**< mask specifying all timeout modes */
  71. } pcilib_streaming_action_t;
  72. typedef enum {
  73. PCILIB_EVENT_FLAGS_DEFAULT = 0,
  74. PCILIB_EVENT_FLAG_RAW_DATA_ONLY = 1, /**< Do not parse data, just read raw and pass it to rawdata callback */
  75. PCILIB_EVENT_FLAG_STOP_ONLY = 1, /**< Do not cleanup, just stop acquiring new frames, the cleanup should be requested afterwards */
  76. PCILIB_EVENT_FLAG_EOF = 2, /**< Indicates that it is the last part of the frame (not required) */
  77. PCILIB_EVENT_FLAG_PREPROCESS = 4 /**< Enables preprocessing of the raw data (decoding frames, etc.) */
  78. } pcilib_event_flags_t;
  79. typedef enum {
  80. PCILIB_EVENT_INFO_FLAG_BROKEN = 1 /**< Indicates broken frames (if this flag is fales, the frame still can be broken) */
  81. } pcilib_event_info_flags_t;
  82. typedef enum {
  83. PCILIB_REGISTER_STANDARD = 0,
  84. PCILIB_REGISTER_FIFO,
  85. PCILIB_REGISTER_BITS
  86. } pcilib_register_type_t;
  87. #define PCILIB_BAR_DETECT ((pcilib_bar_t)-1)
  88. #define PCILIB_BAR_INVALID ((pcilib_bar_t)-1)
  89. #define PCILIB_BAR0 0
  90. #define PCILIB_BAR1 1
  91. #define PCILIB_DMA_ENGINE_INVALID ((pcilib_dma_engine_t)-1)
  92. #define PCILIB_DMA_ENGINE_ALL ((pcilib_dma_engine_t)-1)
  93. #define PCILIB_DMA_FLAGS_DEFAULT ((pcilib_dma_flags_t)0)
  94. #define PCILIB_DMA_ENGINE_ADDR_INVALID ((pcilib_dma_engine_addr_t)-1)
  95. #define PCILIB_REGISTER_INVALID ((pcilib_register_t)-1)
  96. #define PCILIB_ADDRESS_INVALID ((uintptr_t)-1)
  97. #define PCILIB_REGISTER_BANK_INVALID ((pcilib_register_bank_t)-1)
  98. #define PCILIB_REGISTER_BANK0 0
  99. #define PCILIB_REGISTER_BANK1 1
  100. #define PCILIB_REGISTER_BANK2 2
  101. #define PCILIB_REGISTER_BANK3 3
  102. #define PCILIB_REGISTER_BANK_DMA 128
  103. #define PCILIB_EVENT0 1
  104. #define PCILIB_EVENT1 2
  105. #define PCILIB_EVENT2 4
  106. #define PCILIB_EVENT3 8
  107. #define PCILIB_EVENTS_ALL ((pcilib_event_t)-1)
  108. #define PCILIB_EVENT_INVALID ((pcilib_event_t)-1)
  109. #define PCILIB_EVENT_DATA_TYPE_INVALID ((pcilib_event_data_type_t)-1)
  110. #define PCILIB_TIMEOUT_INFINITE ((pcilib_timeout_t)-1)
  111. #define PCILIB_TIMEOUT_IMMEDIATE 0
  112. #define PCILIB_IRQ_SOURCE_DEFAULT 0
  113. typedef struct {
  114. pcilib_event_t type;
  115. uint64_t seqnum; /**< we will add seqnum_overflow if required */
  116. uint64_t offset; /**< nanoseconds */
  117. struct timeval timestamp; /**< most accurate timestamp */
  118. pcilib_event_info_flags_t flags; /**< flags */
  119. } pcilib_event_info_t;
  120. /**<
  121. * Callback function called when new data is read by DMA streaming function
  122. * @ctx - DMA Engine context
  123. * @flags - DMA Flags
  124. * @bufsize - size of data in bytes
  125. * @buf - data
  126. * @returns
  127. * <0 - error, stop streaming (the value is negative error code)
  128. * 0 - stop streaming (PCILIB_STREAMING_STOP)
  129. * 1 - wait DMA timeout and return gracefuly if no data (PCILIB_STREAMING_CONTINUE)
  130. * 2 - wait the specified timeout and return gracefuly if no data (PCILIB_STREAMING_WAIT)
  131. * 3 - check if more data is available without waiting, return gracefuly if not (PCILIB_STREAMING_CHECK)
  132. * 5 - wait DMA timeout and fail if no data (PCILIB_STREAMING_REQ_FRAGMENT)
  133. * 6 - wait the specified timeout and fail if no data (PCILIB_STREAMING_REQ_PACKET)
  134. */
  135. typedef int (*pcilib_dma_callback_t)(void *ctx, pcilib_dma_flags_t flags, size_t bufsize, void *buf);
  136. typedef int (*pcilib_event_callback_t)(pcilib_event_id_t event_id, pcilib_event_info_t *info, void *user);
  137. 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);
  138. typedef struct {
  139. pcilib_register_bank_addr_t addr;
  140. pcilib_bar_t bar; // optional
  141. size_t size;
  142. pcilib_register_protocol_t protocol;
  143. uintptr_t read_addr; // or offset if bar specified
  144. uintptr_t write_addr; // or offset if bar specified
  145. uint8_t raw_endianess;
  146. uint8_t access;
  147. uint8_t endianess;
  148. const char *format;
  149. const char *name;
  150. const char *description;
  151. } pcilib_register_bank_description_t;
  152. typedef struct {
  153. pcilib_register_addr_t addr;
  154. pcilib_register_size_t offset;
  155. pcilib_register_size_t bits;
  156. pcilib_register_value_t defvalue;
  157. pcilib_register_value_t rwmask; /**< 1 - read before write bits, 0 - zero should be written to preserve value */
  158. pcilib_register_mode_t mode;
  159. pcilib_register_type_t type;
  160. pcilib_register_bank_t bank;
  161. const char *name;
  162. const char *description;
  163. } pcilib_register_description_t;
  164. /**
  165. * Default mappings
  166. */
  167. typedef struct {
  168. uintptr_t start;
  169. uintptr_t end;
  170. pcilib_register_bank_addr_t bank;
  171. long addr_shift;
  172. } pcilib_register_range_t;
  173. typedef struct {
  174. pcilib_event_t evid;
  175. const char *name;
  176. const char *description;
  177. } pcilib_event_description_t;
  178. typedef struct {
  179. pcilib_event_data_type_t data_type;
  180. pcilib_event_t evid;
  181. const char *name;
  182. const char *description;
  183. } pcilib_event_data_type_description_t;
  184. typedef enum {
  185. PCILIB_DMA_IRQ = 1,
  186. PCILIB_EVENT_IRQ = 2
  187. } pcilib_irq_type_t;
  188. typedef enum {
  189. PCILIB_DMA_TO_DEVICE = 1,
  190. PCILIB_DMA_FROM_DEVICE = 2,
  191. PCILIB_DMA_BIDIRECTIONAL = 3
  192. } pcilib_dma_direction_t;
  193. typedef enum {
  194. PCILIB_DMA_TYPE_BLOCK,
  195. PCILIB_DMA_TYPE_PACKET,
  196. PCILIB_DMA_TYPE_UNKNOWN
  197. } pcilib_dma_engine_type_t;
  198. typedef struct {
  199. pcilib_dma_engine_addr_t addr;
  200. pcilib_dma_engine_type_t type;
  201. pcilib_dma_direction_t direction;
  202. size_t addr_bits;
  203. } pcilib_dma_engine_description_t;
  204. typedef struct {
  205. pcilib_dma_engine_description_t *engines[PCILIB_MAX_DMA_ENGINES + 1];
  206. } pcilib_dma_info_t;
  207. typedef struct {
  208. uint8_t access;
  209. uint8_t endianess;
  210. pcilib_register_description_t *registers;
  211. pcilib_register_bank_description_t *banks;
  212. pcilib_register_range_t *ranges;
  213. pcilib_event_description_t *events;
  214. pcilib_event_data_type_description_t *data_types;
  215. pcilib_dma_api_description_t *dma_api;
  216. pcilib_event_api_description_t *event_api;
  217. } pcilib_model_description_t;
  218. int pcilib_set_error_handler(void (*err)(const char *msg, ...), void (*warn)(const char *msg, ...));
  219. pcilib_model_t pcilib_get_model(pcilib_t *ctx);
  220. pcilib_model_description_t *pcilib_get_model_description(pcilib_t *ctx);
  221. pcilib_context_t *pcilib_get_implementation_context(pcilib_t *ctx);
  222. pcilib_t *pcilib_open(const char *device, pcilib_model_t model);
  223. void pcilib_close(pcilib_t *ctx);
  224. int pcilib_start_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, pcilib_dma_flags_t flags);
  225. int pcilib_stop_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, pcilib_dma_flags_t flags);
  226. // Interrupt API is preliminary and can be significantly changed in future
  227. int pcilib_enable_irq(pcilib_t *ctx, pcilib_irq_type_t irq_type, pcilib_dma_flags_t flags);
  228. int pcilib_acknowledge_irq(pcilib_t *ctx, pcilib_irq_type_t irq_type, pcilib_irq_source_t irq_source);
  229. int pcilib_disable_irq(pcilib_t *ctx, pcilib_dma_flags_t flags);
  230. int pcilib_wait_irq(pcilib_t *ctx, pcilib_irq_hw_source_t source, pcilib_timeout_t timeout, size_t *count);
  231. int pcilib_clear_irq(pcilib_t *ctx, pcilib_irq_hw_source_t source);
  232. void *pcilib_map_bar(pcilib_t *ctx, pcilib_bar_t bar);
  233. void pcilib_unmap_bar(pcilib_t *ctx, pcilib_bar_t bar, void *data);
  234. char *pcilib_resolve_register_address(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr); // addr is offset if bar is specified
  235. char *pcilib_resolve_data_space(pcilib_t *ctx, uintptr_t addr, size_t *size);
  236. pcilib_register_bank_t pcilib_find_bank_by_addr(pcilib_t *ctx, pcilib_register_bank_addr_t bank);
  237. pcilib_register_bank_t pcilib_find_bank_by_name(pcilib_t *ctx, const char *bankname);
  238. pcilib_register_bank_t pcilib_find_bank(pcilib_t *ctx, const char *bank);
  239. pcilib_register_t pcilib_find_register(pcilib_t *ctx, const char *bank, const char *reg);
  240. pcilib_event_t pcilib_find_event(pcilib_t *ctx, const char *event);
  241. pcilib_event_data_type_t pcilib_find_event_data_type(pcilib_t *ctx, pcilib_event_t event, const char *data_type);
  242. pcilib_dma_engine_t pcilib_find_dma_by_addr(pcilib_t *ctx, pcilib_dma_direction_t direction, pcilib_dma_engine_addr_t dma);
  243. int pcilib_read(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, size_t size, void *buf);
  244. int pcilib_write(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, size_t size, void *buf);
  245. int pcilib_write_fifo(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, uint8_t fifo_size, size_t n, void *buf);
  246. int pcilib_read_fifo(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, uint8_t fifo_size, size_t n, void *buf);
  247. int pcilib_skip_dma(pcilib_t *ctx, pcilib_dma_engine_t dma);
  248. 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);
  249. 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);
  250. int pcilib_read_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, void *buf, size_t *read_bytes);
  251. 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);
  252. int pcilib_write_dma(pcilib_t *ctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, void *buf, size_t *written_bytes);
  253. 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);
  254. int pcilib_read_register_space(pcilib_t *ctx, const char *bank, pcilib_register_addr_t addr, size_t n, pcilib_register_value_t *buf);
  255. int pcilib_write_register_space(pcilib_t *ctx, const char *bank, pcilib_register_addr_t addr, size_t n, pcilib_register_value_t *buf);
  256. int pcilib_read_register_by_id(pcilib_t *ctx, pcilib_register_t reg, pcilib_register_value_t *value);
  257. int pcilib_write_register_by_id(pcilib_t *ctx, pcilib_register_t reg, pcilib_register_value_t value);
  258. int pcilib_read_register(pcilib_t *ctx, const char *bank, const char *regname, pcilib_register_value_t *value);
  259. int pcilib_write_register(pcilib_t *ctx, const char *bank, const char *regname, pcilib_register_value_t value);
  260. int pcilib_reset(pcilib_t *ctx);
  261. int pcilib_trigger(pcilib_t *ctx, pcilib_event_t event, size_t trigger_size, void *trigger_data);
  262. /*
  263. * The recording of new events will be stopped after reaching max_events records
  264. * or when the specified amount of time is elapsed. However, the @pcilib_stop
  265. * function should be called still.
  266. * NOTE: This options may not be respected if the PCILIB_EVENT_FLAG_RAW_DATA_ONLY
  267. * is specified.
  268. */
  269. int pcilib_configure_autostop(pcilib_t *ctx, size_t max_events, pcilib_timeout_t duration);
  270. /*
  271. * Request streaming the rawdata from the event engine. It is fastest way to acuqire data.
  272. * No memory copies will be performed and DMA buffers will be directly passed to the user
  273. * callback. However, to prevent data loss, no processing should be done on the data. The
  274. * user callback is only expected to copy data into the appropriate place and return control
  275. * to the event engine.
  276. * The performance can be boosted further by disabling any data processing within the event
  277. * engine. Just pass PCILIB_EVENT_FLAG_RAW_DATA_ONLY flag to the @pcilib_start function.
  278. */
  279. int pcilib_configure_rawdata_callback(pcilib_t *ctx, pcilib_event_rawdata_callback_t callback, void *user);
  280. int pcilib_start(pcilib_t *ctx, pcilib_event_t event_mask, pcilib_event_flags_t flags);
  281. int pcilib_stop(pcilib_t *ctx, pcilib_event_flags_t flags);
  282. int pcilib_stream(pcilib_t *ctx, pcilib_event_callback_t callback, void *user);
  283. 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);
  284. 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);
  285. 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);
  286. 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);
  287. 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);
  288. /*
  289. * This function is provided to find potentially corrupted data. If the data is overwritten by
  290. * the time return_data is called it will return error.
  291. */
  292. int pcilib_return_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, void *data);
  293. /*
  294. * @param data - will be allocated and shuld be freed if NULL, otherwise used and size should contain correct size.
  295. * In case of failure the content of data is undefined.
  296. * @param timeout - will be autotriggered if NULL
  297. */
  298. int pcilib_grab(pcilib_t *ctx, pcilib_event_t event_mask, size_t *size, void **data, pcilib_timeout_t timeout);
  299. #endif /* _PCITOOL_PCILIB_H */