bank.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #ifndef _PCILIB_BANK_H
  2. #define _PCILIB_BANK_H
  3. #include <pcilib.h>
  4. #define PCILIB_REGISTER_BANK_INVALID ((pcilib_register_bank_t)-1)
  5. #define PCILIB_REGISTER_BANK0 0 /**< First BANK to be used in the event engine */
  6. #define PCILIB_REGISTER_BANK1 1
  7. #define PCILIB_REGISTER_BANK2 2
  8. #define PCILIB_REGISTER_BANK3 3
  9. #define PCILIB_REGISTER_BANK_CONF 64 /**< Configuration registers */
  10. #define PCILIB_REGISTER_BANK_PROPERTY 65 /**< Registers abstracting properties and other computed registers */
  11. #define PCILIB_REGISTER_BANK_DMACONF 96 /**< DMA configuration in the software registers */
  12. #define PCILIB_REGISTER_BANK_DMA 97 /**< First BANK address to be used by DMA engines */
  13. #define PCILIB_REGISTER_BANK_DMA0 PCILIB_REGISTER_BANK_DMA /**< First BANK address to be used by DMA engines */
  14. #define PCILIB_REGISTER_BANK_DMA1 (PCILIB_REGISTER_BANK_DMA + 1) /**< Second BANK address to be used by DMA engines */
  15. #define PCILIB_REGISTER_BANK_DMA2 (PCILIB_REGISTER_BANK_DMA + 2) /**< Third BANK address to be used by DMA engines */
  16. #define PCILIB_REGISTER_BANK_DMA3 (PCILIB_REGISTER_BANK_DMA + 3) /**< Fourth BANK address to be used by DMA engines */
  17. #define PCILIB_REGISTER_BANK_DYNAMIC 128 /**< First BANK address to map dynamic XML configuration */
  18. #define PCILIB_REGISTER_PROTOCOL_INVALID ((pcilib_register_protocol_t)-1)
  19. #define PCILIB_REGISTER_PROTOCOL0 0 /**< First PROTOCOL address to be used in the event engine */
  20. #define PCILIB_REGISTER_PROTOCOL_DEFAULT 64 /**< Default memmap based protocol */
  21. #define PCILIB_REGISTER_PROTOCOL_SOFTWARE 65 /**< Software registers */
  22. #define PCILIB_REGISTER_PROTOCOL_PROPERTY 66 /**< Protocol to access registers interfacing properties */
  23. #define PCILIB_REGISTER_PROTOCOL_DMA 96 /**< First PROTOCOL address to be used by DMA engines */
  24. #define PCILIB_REGISTER_PROTOCOL_DYNAMIC 128 /**< First PROTOCOL address to be used by plugins */
  25. typedef uint8_t pcilib_register_bank_t; /**< Type holding the bank position within the field listing register banks in the model */
  26. typedef uint8_t pcilib_register_bank_addr_t; /**< Type holding the bank address number */
  27. typedef uint8_t pcilib_register_protocol_t; /**< Type holding the protocol position within the field listing register protocols in the model */
  28. typedef uint8_t pcilib_register_protocol_addr_t; /**< Type holding the protocol address */
  29. typedef struct pcilib_register_bank_context_s pcilib_register_bank_context_t;
  30. typedef enum {
  31. PCILIB_MODEL_MODIFICATON_FLAGS_DEFAULT = 0,
  32. PCILIB_MODEL_MODIFICATION_FLAG_OVERRIDE = 1, /**< Instructs to override the existing registers/banks/etc... */
  33. PCILIB_MODEL_MODIFICATION_FLAG_SKIP_EXISTING = 2 /**< If flag is set, pcilib will just skip existing registers/banks/etc instead of reporting a error */
  34. } pcilib_model_modification_flags_t;
  35. typedef enum {
  36. PCILIB_ADDRESS_RESOLUTION_FLAGS_DEFAULT = 0,
  37. PCILIB_ADDRESS_RESOLUTION_FLAG_BUS_ADDRESS = 1, /**< Resolve bus address instead of VA */
  38. PCILIB_ADDRESS_RESOLUTION_FLAG_PHYS_ADDRESS = 2, /**< Resolve hardware address instead of VA */
  39. PCILIB_ADDRESS_RESOLUTION_MASK_ADDRESS_TYPE = 3,
  40. PCILIB_ADDRESS_RESOLUTION_FLAG_READ_ONLY = 4, /**< Request read-only memory, in case if read-write resolution is impossible */
  41. PCILIB_ADDRESS_RESOLUTION_FLAG_WRITE_ONLY = 8, /**< Request write-only resolution, in case if read-write resolution is impossible */
  42. PCILIB_ADDRESS_RESOLUTION_MASK_ACCESS_MODE = 12
  43. } pcilib_address_resolution_flags_t;
  44. typedef struct {
  45. pcilib_version_t version;
  46. pcilib_register_bank_context_t *(*init)(pcilib_t *ctx, pcilib_register_bank_t bank, const char *model, const void *args); /**< Optional API call to initialize bank context */
  47. void (*free)(pcilib_t *pcilib, pcilib_register_bank_context_t *ctx); /**< Optional API call to cleanup bank context */
  48. uintptr_t (*resolve)(pcilib_t *pcilib, pcilib_register_bank_context_t *ctx, pcilib_address_resolution_flags_t flags, pcilib_register_addr_t addr); /**< Resolves register virtual address (if supported) */
  49. int (*read)(pcilib_t *pcilib, pcilib_register_bank_context_t *ctx, pcilib_register_addr_t addr, pcilib_register_value_t *value); /**< Read from register, mandatory for RO/RW registers */
  50. int (*write)(pcilib_t *pcilib, pcilib_register_bank_context_t *ctx, pcilib_register_addr_t addr, pcilib_register_value_t value); /**< Write to register, mandatory for WO/RW registers */
  51. } pcilib_register_protocol_api_description_t;
  52. typedef struct {
  53. pcilib_register_protocol_addr_t addr; /**< Protocol address used in model for addressing the described protocol */
  54. const pcilib_register_protocol_api_description_t *api; /**< Defines all API functions for protocol */
  55. const char *model; /**< If NULL, the actually used model is used instead */
  56. const void *args; /**< Custom protocol-specific arguments. The actual structure may depend on the specified model */
  57. const char *name; /**< Short protocol name */
  58. const char *description; /**< A bit longer protocol description */
  59. } pcilib_register_protocol_description_t;
  60. typedef struct {
  61. pcilib_register_bank_addr_t addr; /**< Bank address used in model for addressing the described register bank */
  62. pcilib_register_protocol_addr_t protocol; /**< Defines a protocol to access registers */
  63. pcilib_bar_t bar; /**< Specifies the PCI BAR through which an access to the registers is provided (autodetcted if PCILIB_BAR_DETECT is specified) */
  64. uintptr_t read_addr; /**< protocol specific (normally offset in the BAR of the first address used to read registers) */
  65. uintptr_t write_addr; /**< protocol specific (normally offset in the BAR of the first address used to write registers) */
  66. uint8_t access; /**< Default register size in bits (or word-size in plain addressing mode) */
  67. size_t size; /**< The size of the bank in bytes (i.e. number of registers in plain addressing mode multiplied by access; this does not limit number of register names since indefinite number of names can be provided if bit-fields/views are used) */
  68. pcilib_endianess_t raw_endianess; /**< Specifies endianess in the plain-addressing mode, PCILIB_HOST_ENDIAN have to be specified if no conversion desired.
  69. Conversion applied after protocol. This value does not get into the account in register-access mode */
  70. pcilib_endianess_t endianess; /**< Specifies endianess in the register-access mode, this may differ from raw_endianess if multi-word registers are used.
  71. This is fully independent from raw_endianess. No double conversion is either performed */
  72. const char *format; /**< printf format for the registers, either %lu for decimal output or 0x%lx for hexdecimal */
  73. const char *name; /**< short bank name */
  74. const char *description; /**< longer bank description */
  75. } pcilib_register_bank_description_t;
  76. /**
  77. * Default mappings: defines virtual address to register mappings, i.e. how 0x9000 in the following command
  78. * will be mapped to the actual register. By comparing with start and end-addresses, we find to which range
  79. * 0x9000 belongs to and detect actual bank and offset in it.
  80. * Simple example: pci -r 0x9000
  81. * if we specify range { 0x9000, 0x9100, 10, -0x9000}, the example command we print the value of the first
  82. * register in the bank 10.
  83. */
  84. typedef struct {
  85. uintptr_t start; /**< The first virtual address of the register range */
  86. uintptr_t end; /**< The last virtual address of the register range */
  87. pcilib_register_bank_addr_t bank; /**< The bank mapped to the specified range */
  88. long addr_shift; /**< Address shift, i.e. how much we should add/substract to the virtual address to get address in the register bank */
  89. } pcilib_register_range_t;
  90. struct pcilib_register_bank_context_s {
  91. const pcilib_register_bank_description_t *bank; /**< Corresponding bank description */
  92. const pcilib_register_protocol_api_description_t *api; /**< API functions */
  93. pcilib_xml_node_t *xml; /**< Additional XML properties */
  94. };
  95. #ifdef __cplusplus
  96. extern "C" {
  97. #endif
  98. /**
  99. * Initalizes context of register banks. This is an internal function and will
  100. * be called automatically when new register banks are added. On error no new
  101. * banks are initalized
  102. * @param[in,out] ctx - pcilib context
  103. * @return - error or 0 on success
  104. */
  105. int pcilib_init_register_banks(pcilib_t *ctx);
  106. /**
  107. * Destroys contexts of register banks. This is an internal function and will
  108. * be called during clean-up.
  109. * @param[in,out] ctx - pcilib context
  110. * @param[in] start - specifies first bank to clean (used to clean only part of the banks to keep the defined state if pcilib_init_register_banks has failed)
  111. */
  112. void pcilib_free_register_banks(pcilib_t *ctx, pcilib_register_bank_t start);
  113. /**
  114. * Use this function to add new register banks into the model or override configuration
  115. * of the existing banks. The function will copy the context of banks structure, but name,
  116. * description, and other strings in the structure are considered to have static duration
  117. * and will not be copied. On error no new banks are initalized.
  118. * @param[in,out] ctx - pcilib context
  119. * @param[in] flags - instructs if existing banks should be reported as error (default), overriden or ignored
  120. * @param[in] n - number of banks to initialize. It is OK to pass 0 if banks variable is NULL terminated (last member of banks array have all members set to 0)
  121. * @param[in] banks - bank descriptions
  122. * @param[out] ids - if specified will contain the ids of the newly registered and overriden banks
  123. * @return - error or 0 on success
  124. */
  125. int pcilib_add_register_banks(pcilib_t *ctx, pcilib_model_modification_flags_t flags, size_t n, const pcilib_register_bank_description_t *banks, pcilib_register_bank_t *ids);
  126. /**
  127. * Use this function to add new register protocols into the model. It is error to re-register
  128. * already registered protocols. The function will copy the context of banks structure, but name,
  129. * description, and other strings in the structure are considered to have static duration
  130. * and will not be copied. On error no new protocols are initalized.
  131. * @param[in,out] ctx - pcilib context
  132. * @param[in] flags - not used
  133. * @param[in] n - number of protocols to initialize. It is OK to pass 0 if protocols variable is NULL terminated (last member of protocols array have all members set to 0)
  134. * @param[in] protocols - protocol descriptions
  135. * @param[out] ids - if specified will contain the ids of the newly registered protocols
  136. * @return - error or 0 on success
  137. */
  138. int pcilib_add_register_protocols(pcilib_t *ctx, pcilib_model_modification_flags_t flags, size_t n, const pcilib_register_protocol_description_t *protocols, pcilib_register_protocol_t *ids);
  139. /**
  140. * Use this function to add new register ranges into the model. It is error register
  141. * overlapping registered ranges. On error no new ranges are initalized.
  142. * @param[in,out] ctx - pcilib context
  143. * @param[in] flags - not used
  144. * @param[in] n - number of protocols to initialize. It is OK to pass 0 if protocols variable is NULL terminated.
  145. * @param[in] ranges - range descriptions
  146. * @return - error or 0 on success
  147. */
  148. int pcilib_add_register_ranges(pcilib_t *ctx, pcilib_model_modification_flags_t flags, size_t n, const pcilib_register_range_t *ranges);
  149. /**
  150. * Find the register bank id (offset in \a banks array) corresponding to the specified bank address
  151. * @param[in,out] ctx - pcilib context
  152. * @param[in] bank - the address of register bank
  153. * @return - bank id or PCILIB_REGISTER_BANK_INVALID if bank is not found
  154. */
  155. pcilib_register_bank_t pcilib_find_register_bank_by_addr(pcilib_t *ctx, pcilib_register_bank_addr_t bank);
  156. /**
  157. * Find the register bank id (offset in \a banks array) corresponding to the specified bank name
  158. * @param[in,out] ctx - pcilib context
  159. * @param[in] bankname - the name of register bank
  160. * @return - bank id or PCILIB_REGISTER_BANK_INVALID if bank is not found
  161. */
  162. pcilib_register_bank_t pcilib_find_register_bank_by_name(pcilib_t *ctx, const char *bankname);
  163. /**
  164. * Find the register bank id (offset in \a banks array) corresponding to the specified bank name or address
  165. * @param[in,out] ctx - pcilib context
  166. * @param[in] bank - either the name or the address of the required register bank
  167. * @return - bank id or PCILIB_REGISTER_BANK_INVALID if bank is not found
  168. */
  169. pcilib_register_bank_t pcilib_find_register_bank(pcilib_t *ctx, const char *bank);
  170. /**
  171. * Find the register protocol id (offset in \a protocols array) corresponding to the specified protocol address
  172. * @param[in,out] ctx - pcilib context
  173. * @param[in] protocol - the address of register protocol
  174. * @return - protocol id or PCILIB_REGISTER_PROTOCOL_INVALID if register protocol is not found
  175. */
  176. pcilib_register_protocol_t pcilib_find_register_protocol_by_addr(pcilib_t *ctx, pcilib_register_protocol_addr_t protocol);
  177. /**
  178. * Find the register protocol id (offset in \a protocols array) corresponding to the specified protocol name
  179. * @param[in,out] ctx - pcilib context
  180. * @param[in] name - the name of register protocol
  181. * @return - protocol id or PCILIB_REGISTER_PROTOCOL_INVALID if register protocol is not found
  182. */
  183. pcilib_register_protocol_t pcilib_find_register_protocol_by_name(pcilib_t *ctx, const char *name);
  184. /**
  185. * Find the register protocol id (offset in \a protocols array) corresponding to the specified protocol name or address
  186. * @param[in,out] ctx - pcilib context
  187. * @param[in] name - either the name or the address of the required register protocol
  188. * @return - protocol id or PCILIB_REGISTER_PROTOCOL_INVALID if register protocol is not found
  189. */
  190. pcilib_register_protocol_t pcilib_find_register_protocol(pcilib_t *ctx, const char *name);
  191. /**
  192. * Resolves the address of the specified register bank. The address of the register0 in the bank is returned.
  193. *
  194. * All address types (virtual address in process address space, physical address, or bus address) can be resolved.
  195. * The caller has also to specify the requested access mode (read, write, read/write) and the error will be returned
  196. * if the requested access type is not possible.
  197. *
  198. * @param[in,out] ctx - pcilib context
  199. * @param[in] flags - specifies the type of required address (virtual, physical, or bus) and the required access (ro/wo/rw)
  200. * @param[in] bank - the id of register bank
  201. * @return - the resolved address or PCILIB_ADDRESS_INVALID on error
  202. */
  203. uintptr_t pcilib_resolve_bank_address_by_id(pcilib_t *ctx, pcilib_address_resolution_flags_t flags, pcilib_register_bank_t bank);
  204. /**
  205. * Resolves the address of the specified register bank. The address of the register0 in the bank is returned.
  206. *
  207. * All address types (virtual address in process address space, physical address, or bus address) can be resolved.
  208. * The caller has also to specify the requested access mode (read, write, read/write) and the error will be returned
  209. * if the requested access type is not possible.
  210. *
  211. * @param[in,out] ctx - pcilib context
  212. * @param[in] flags - specifies the type of required address (virtual, physical, or bus) and the required access (ro/wo/rw)
  213. * @param[in] bank - the name of register bank
  214. * @return - the resolved address or PCILIB_ADDRESS_INVALID on error
  215. */
  216. uintptr_t pcilib_resolve_bank_address(pcilib_t *ctx, pcilib_address_resolution_flags_t flags, const char *bank);
  217. /**
  218. * Resolves the address of the specified register.
  219. *
  220. * All address types (virtual address in process address space, physical address, or bus address) can be resolved.
  221. * The caller has also to specify the requested access mode (read, write, read/write) and the error will be returned
  222. * if the requested access type is not possible.
  223. *
  224. * @param[in,out] ctx - pcilib context
  225. * @param[in] flags - specifies the type of required address (virtual, physical, or bus) and the required access (ro/wo/rw)
  226. * @param[in] reg - the id of register
  227. * @return - the resolved address or PCILIB_ADDRESS_INVALID on error
  228. */
  229. uintptr_t pcilib_resolve_register_address_by_id(pcilib_t *ctx, pcilib_address_resolution_flags_t flags, pcilib_register_t reg);
  230. /**
  231. * Resolves the address of the specified register.
  232. *
  233. * All address types (virtual address in process address space, physical address, or bus address) can be resolved.
  234. * The caller has also to specify the requested access mode (read, write, read/write) and the error will be returned
  235. * if the requested access type is not possible.
  236. *
  237. * @param[in,out] ctx - pcilib context
  238. * @param[in] flags - specifies the type of required address (virtual, physical, or bus) and the required access (ro/wo/rw)
  239. * @param[in] bank - should specify the bank name if register with the same name may occur in multiple banks, NULL otherwise
  240. * @param[in] regname - the name of the register
  241. * @return - the resolved address or PCILIB_ADDRESS_INVALID on error
  242. */
  243. uintptr_t pcilib_resolve_register_address(pcilib_t *ctx, pcilib_address_resolution_flags_t flags, const char *bank, const char *regname);
  244. /**
  245. * Extracts additional information about the specified register bank. The additional information
  246. * is model-specific and are provided as extra XML attributes in XML-described register bank.
  247. * The available attributes are only restricted by used XSD schema.
  248. * @param[in,out] ctx - pcilib context
  249. * @param[in] bank - register bank id
  250. * @param[in] attr - requested attribute name
  251. * @param[in,out] val - the value of attribute is returned here (see \ref public_api_value),
  252. * pcilib_clean_value() will be executed if \a val contains data. Therefore it should be always initialized to 0 before first use
  253. * @return - error code or 0 on success
  254. */
  255. int pcilib_get_register_bank_attr_by_id(pcilib_t *ctx, pcilib_register_bank_t bank, const char *attr, pcilib_value_t *val);
  256. /**
  257. * Extracts additional information about the specified register bank. The additional information
  258. * is model-specific and are provided as extra XML attributes in XML-described register bank.
  259. * The available attributes are only restricted by used XSD schema.
  260. * @param[in,out] ctx - pcilib context
  261. * @param[in] bankname - the name of register bank
  262. * @param[in] attr - requested attribute name
  263. * @param[in,out] val - the value of attribute is returned here (see \ref public_api_value),
  264. * pcilib_clean_value() will be executed if \a val contains data. Therefore it should be always initialized to 0 before first use
  265. * @return - error code or 0 on success
  266. */
  267. int pcilib_get_register_bank_attr(pcilib_t *ctx, const char *bankname, const char *attr, pcilib_value_t *val);
  268. #ifdef __cplusplus
  269. }
  270. #endif
  271. #endif /* _PCILIB_BANK_H */