kmem.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #ifndef _PCILIB_KMEM_H
  2. #define _PCILIB_KMEM_H
  3. typedef struct pcilib_s pcilib_t;
  4. typedef struct pcilib_kmem_list_s pcilib_kmem_list_t;
  5. #define PCILIB_KMEM_PAGE_SIZE 0x1000 /**< Default pages size is 4096 bytes */
  6. typedef enum {
  7. PCILIB_TRISTATE_NO = 0, /**< All values are evaluated as false */
  8. PCILIB_TRISTATE_PARTIAL = 1, /**< Some values are evaluated as false and others as true */
  9. PCILIB_TRISTATE_YES = 2 /**< All values are evaluated as true */
  10. } pcilib_tristate_t;
  11. #define PCILIB_KMEM_TYPE_MASK 0xFFFF0000 /**< Mask selecting the general type in \ref pcilib_kmem_type_t. The subtype is may specify DMA mapping and other minor details of allocation */
  12. #define PCILIB_KMEM_USE_TYPE(use) (use >> 16) /**< Returns general use of the kernel buffers, for instance DMA buffer (see \ref pcilib_kmem_use_t) */
  13. #define PCILIB_KMEM_USE_SUBTYPE(use) (use & 0xFFFF) /**< Additional information about the use of the kernel buffers. The actual meaning depends on general use, for instance in case of DMA buffers the subtype specifies DMA engine */
  14. #define PCILIB_KMEM_USE(type, subtype) ((pcilib_kmem_use_t)(((type) << 16)|(subtype))) /**< Constructs full use from use type and use subtype */
  15. typedef enum {
  16. PCILIB_KMEM_TYPE_CONSISTENT = 0x00000, /**< Consistent memory can be simultaneously accessed by the device and applications and is normally used for DMA descriptors */
  17. PCILIB_KMEM_TYPE_PAGE = 0x10000, /**< A number of physically consequitive pages of memory */
  18. PCILIB_KMEM_TYPE_DMA_S2C_PAGE = 0x10001, /**< Memory pages mapped for S2C DMA operation (device reads) */
  19. PCILIB_KMEM_TYPE_DMA_C2S_PAGE = 0x10002, /**< Memory pages mapped for C2S DMA operation (device writes) */
  20. PCILIB_KMEM_TYPE_REGION = 0x20000, /**< Just map buffers to the contiguous user-supplied memory region (normally reserved during the boot with option memmap=512M$2G) */
  21. PCILIB_KMEM_TYPE_REGION_S2C = 0x20001, /**< Memory region mapped for S2C DMA operation (device reads) */
  22. PCILIB_KMEM_TYPE_REGION_C2S = 0x20002 /**< Memory region mapped for C2S DMA operation (device writes */
  23. } pcilib_kmem_type_t;
  24. typedef enum {
  25. PCILIB_KMEM_USE_STANDARD = 0, /**< Undefined use, buffers of standard use are not grouped together and can be used individually */
  26. PCILIB_KMEM_USE_DMA_RING = 1, /**< The kmem used for DMA descriptor, the sub type specifies the id of considered DMA engine */
  27. PCILIB_KMEM_USE_DMA_PAGES = 2, /**< The kmem used for DMA buffers, the sub type specifies the address of considered DMA engine (can be engine specific) */
  28. PCILIB_KMEM_USE_SOFTWARE_REGISTERS = 3, /**< The kmem used to store software registers, the sub type holds the bank address */
  29. PCILIB_KMEM_USE_LOCKS = 4, /**< The kmem used to hold locks, the sub type is not used */
  30. PCILIB_KMEM_USE_USER = 0x10 /**< Further uses can be defined by event engines */
  31. } pcilib_kmem_use_t;
  32. typedef enum {
  33. PCILIB_KMEM_SYNC_BIDIRECTIONAL = 0, /**< Two-way synchronization (should not be used) */
  34. PCILIB_KMEM_SYNC_TODEVICE = 1, /**< Allow device to read the data in the DMA-able buffer */
  35. PCILIB_KMEM_SYNC_FROMDEVICE = 2 /**< Allow application to read the data written by device in the DMA-able buffer */
  36. } pcilib_kmem_sync_direction_t;
  37. typedef enum {
  38. PCILIB_KMEM_FLAG_REUSE = 1, /**< Try to reuse existing buffer with the same use & item */
  39. PCILIB_KMEM_FLAG_EXCLUSIVE = 2, /**< Allow only a single application accessing a specified use & item */
  40. PCILIB_KMEM_FLAG_PERSISTENT = 4, /**< Sets persistent mode */
  41. PCILIB_KMEM_FLAG_HARDWARE = 8, /**< The buffer may be accessed by hardware, the hardware access will not occur any more if passed to _free function */
  42. PCILIB_KMEM_FLAG_FORCE = 16, /**< Force memory cleanup even if references are present */
  43. PCILIB_KMEM_FLAG_MASS = 32, /**< Apply to all buffers of selected use */
  44. PCILIB_KMEM_FLAG_TRY = 64 /**< Do not allocate buffers, try to reuse and fail if not possible */
  45. } pcilib_kmem_flags_t;
  46. typedef enum {
  47. PCILIB_KMEM_REUSE_ALLOCATED = PCILIB_TRISTATE_NO, /**< The kernel memory was allocated anew */
  48. PCILIB_KMEM_REUSE_REUSED = PCILIB_TRISTATE_YES, /**< Already allocated kernel memory was re-used */
  49. PCILIB_KMEM_REUSE_PARTIAL = PCILIB_TRISTATE_PARTIAL,/**< The kernel memory was partially re-used and partially allocated */
  50. PCILIB_KMEM_REUSE_PERSISTENT = 0x100, /**< Flag indicating that persistent kmem was allocated/reused (will not be cleaned on pcilib_free_kernel_memory() unless forced with PCILIB_KMEM_FLAG_PERSISTENT */
  51. PCILIB_KMEM_REUSE_HARDWARE = 0x200 /**< Flag indicating that kmem may be used by hardware device */
  52. } pcilib_kmem_reuse_state_t;
  53. typedef struct {
  54. int handle_id; /**< handled id is used to indentify buffer to kernel module */
  55. pcilib_kmem_reuse_state_t reused; /**< indicates if the buffer was allocated or used */
  56. uintptr_t pa; /**< physical address of buffer */
  57. uintptr_t ba; /**< bus address of buffer (if it is mapped for DMA operations) */
  58. volatile void *ua; /**< pointer to buffer in the process address space */
  59. size_t size; /**< size of the buffer in bytes */
  60. size_t alignment_offset; /**< we may request alignment of allocated buffers. To enusre proper alignment the larger buffer will be allocated and the offset will specify the first position in the buffer fullfilling alignment request */
  61. size_t mmap_offset; /**< mmap always maps pages, if physical address is not aligned to page boundary, this is the offset of the buffer relative to the pointer returned by mmap (and stored in \a ua) */
  62. } pcilib_kmem_addr_t;
  63. /**
  64. * single allocation - we set only addr, n_blocks = 0
  65. * multiple allocation - addr is not set, blocks are set, n_blocks > 0
  66. * sgmap allocation - addr contains ua, but pa's are set in blocks, n_blocks > 0
  67. */
  68. typedef struct {
  69. pcilib_kmem_type_t type; /**< The type of kernel memory (how it is allocated) */
  70. pcilib_kmem_use_t use; /**< The purpose of kernel memory (how it will be used) */
  71. pcilib_kmem_reuse_state_t reused; /**< Indicates if kernel memory was allocated anew, reused, or partially re-used. The additional flags will provide information about persistance and the hardware access */
  72. size_t n_blocks; /**< Number of allocated/re-used buffers in kmem */
  73. pcilib_kmem_addr_t addr; /**< Information about the buffer of single-buffer kmem */
  74. pcilib_kmem_addr_t blocks[]; /**< Information about all the buffers in kmem (variable size) */
  75. } pcilib_kmem_buffer_t;
  76. typedef void pcilib_kmem_handle_t;
  77. struct pcilib_kmem_list_s {
  78. pcilib_kmem_list_t *next, *prev; /**< List storring all allocated/re-used kmem's in application */
  79. pcilib_kmem_buffer_t buf; /**< The actual kmem context (it is of variable size due to \a blocks and, hence, should be last item in this struct */
  80. };
  81. #ifdef __cplusplus
  82. extern "C" {
  83. #endif
  84. /**
  85. * This function either allocates new buffers in the kernel space or just re-uses existing buffers.
  86. *
  87. * Sometimes kernel memory is allocated for the specific use-case, for example to provide buffers for DMA engine.
  88. * It is possible to specify intended use using \p use parameter.
  89. * The kernel memory with the specified use (i.e. \p use != 0) can be declared persistent (\p flags include
  90. * ::PCILIB_KMEM_FLAG_PERSISTENT). Such memory will not be de-allocated on clean-up and will be maintained by
  91. * the kernel module across the runs of various pcilib applications. To re-use persistent kernel memory,
  92. * ::PCILIB_KMEM_FLAG_REUSE should be set. In this case, the pcilib will either allocate new kernel memory
  93. * or re-use the already allocated kernel buffers with the specified \p use. If ::PCILIB_KMEM_FLAG_REUSE
  94. * is not set, but the kernel memory with the specified use is already allocated, an error will be returned.
  95. *
  96. * It is also possible to allocate persistent kernel memory which can be execlusively used by a single process.
  97. * The ::PCILIB_KMEM_FLAG_EXCLUSIVE flag have to be provided to pcilib_alloc_kernel_memory() function in this
  98. * case. Only a single process may call hold a reference to kernel memory. Before next process would be able
  99. * to obtain it, the process holding the reference should return it using pcilib_free_kernel_memory()
  100. * call. For example, DMA engine uses exclusive kernel memory to guarantee that exactly one process is
  101. * controlling DMA operations.
  102. *
  103. * To clean up allocated persistent kernel memory, pcilib_free_kernel_memory() have to be called with
  104. * ::PCILIB_KMEM_FLAG_PERSISTENT flag set.
  105. *
  106. * @param[in,out] ctx - pcilib context
  107. * @param[in] type - specifies type of allocation (simple pages, DMA pages, consistent memory, etc.)
  108. * @param[in] nmemb - specifies number of buffers to allocated
  109. * @param[in] size - specifies the size of each buffer in bytes
  110. * @param[in] alignment - specifies required alignment, the bus addresses are aligned for PCI-mapped buffers and physical addresses are aligned for the rest (if bounce buffers are used, the physical and bus addresses are not always have the same alignment)
  111. * @param[in] use - specifies how the memory will be used (to store software registers, locks, DMA buffers, etc.)
  112. * @param[in] flags - various flags defining how buffers should be re-used/allocated and, that, should happen on pcilib_free_kernel_memory()
  113. * @return - pointer on context with allocated kernel memory or NULL in case of error
  114. */
  115. pcilib_kmem_handle_t *pcilib_alloc_kernel_memory(pcilib_t *ctx, pcilib_kmem_type_t type, size_t nmemb, size_t size, size_t alignment, pcilib_kmem_use_t use, pcilib_kmem_flags_t flags);
  116. /**
  117. * This function either frees the allocated kernel memory or just releases some of the references.
  118. *
  119. * Only reference tracking is performed If the ::PCILIB_KMEM_FLAG_REUSE flag is passed to the
  120. * function. Otherwise, non-persistent memory is released when pcilib_free_kernel_memory() called.
  121. * The persistent memory is released if ::PCILIB_KMEM_FLAG_PERSISTENT is passed in \p flags parameter
  122. * unless the hold references are preventing us from releasing this memory. The error is reported
  123. * in this case. The kernel memory can be freed irrespective of hold references if ::PCILIB_KMEM_FLAG_FORCE
  124. * flag is specified.
  125. *
  126. * There are several types of references:
  127. * - The hardware reference - indicates that the memory may be used by DMA engine of the device. It is set
  128. * if pcilib_alloc_kernel_memory() is executed with ::PCILIB_KMEM_FLAG_HARDWARE flag. The reference can
  129. * be released if the same flag is passed to pcilib_free_kernel_memory()
  130. * - The software references - are obtained when the memory is reused with pcilib_alloc_kernel_memory().
  131. * They are released when corresponding pcilib_free_kernel_memory() is called.
  132. * - The mmap references - are obtained when the kernel memory is mapped to the virtual memory of application
  133. * which is normally happen on pcilib_alloc_kernel_memory() and fork of the process. The references are
  134. * returned when memory is unmapped. This happens on pcilib_free_kernel_memory() and termination of the
  135. * process with still mmaped regions.
  136. *
  137. * The software references are in a way replicate functionality of the mmap references. Currently, they are
  138. * used to keep track of non-persistent memory allocated and re-used within the same application (which is
  139. * actually discouraged). In this case the number of pcilib_alloc_kernel_memory() should be matched by
  140. * number of pcilib_free_kernel_memory() calls and only on a last call the memory will be really released.
  141. * Normally, all but last calls have to be accompanied by ::PCILIB_KMEM_FLAG_REUSE flag or the error
  142. * is reported. But to keep code simpler, the pcilib will not complain until there are software references
  143. * on hold. When the last software reference is released, we try actually clean up the memory and the
  144. * error is reported if other types of references are still present.
  145. * The software references may stuck if application crashes before calling pcilib_free_kernel_memory().
  146. * pcilib will prevent us from releasing the kernel memory with stuck references. Such memory can
  147. * be cleaned only with ::PCILIB_KMEM_FLAG_FORCE flag or using pcilib_clean_kernel_memory() call.
  148. *
  149. * @param[in,out] ctx - pcilib context
  150. * @param[in,out] k - kernel memory handle returned from pcilib_alloc_kernel_memory() call
  151. * @param[in] flags - various flags defining which referenes to hold and return and if the memory should be preserved or freed
  152. */
  153. void pcilib_free_kernel_memory(pcilib_t *ctx, pcilib_kmem_handle_t *k, pcilib_kmem_flags_t flags);
  154. /**
  155. * Free / dereference all kernel memory buffers associated with the specified \p use
  156. *
  157. * @param[in,out] ctx - pcilib context
  158. * @param[in] use - use-number to clean
  159. * @param[in] flags - various flags defining which referenes to hold and return and if the memory should be dereferenced or freed
  160. * @return - error or 0 on success
  161. */
  162. int pcilib_clean_kernel_memory(pcilib_t *ctx, pcilib_kmem_use_t use, pcilib_kmem_flags_t flags);
  163. /**
  164. * Synchronizes usage of the specified buffer between hardware and the system.
  165. *
  166. * @param[in,out] ctx - pcilib context
  167. * @param[in] k - kernel memory handle returned from pcilib_alloc_kernel_memory() call
  168. * @param[in] dir - synchronization direction (allows either device or system access)
  169. * @param[in] block - specifies the buffer within the kernel memory (buffers are numbered from 0)
  170. * @return - error or 0 on success
  171. */
  172. int pcilib_kmem_sync_block(pcilib_t *ctx, pcilib_kmem_handle_t *k, pcilib_kmem_sync_direction_t dir, size_t block);
  173. /**
  174. * Get a valid pointer on the user-space mapping of a single-buffer kernel memory
  175. *
  176. * @param[in,out] ctx - pcilib context
  177. * @param[in] k - kernel memory handle returned from pcilib_alloc_kernel_memory() call
  178. * @return - user-space pointer
  179. */
  180. volatile void *pcilib_kmem_get_ua(pcilib_t *ctx, pcilib_kmem_handle_t *k);
  181. /**
  182. * Get a physical address of a single-buffer kernel memory
  183. *
  184. * @param[in,out] ctx - pcilib context
  185. * @param[in] k - kernel memory handle returned from pcilib_alloc_kernel_memory() call
  186. * @return - physical address
  187. */
  188. uintptr_t pcilib_kmem_get_pa(pcilib_t *ctx, pcilib_kmem_handle_t *k);
  189. /**
  190. * Get a bus address of a single-buffer kernel memory
  191. *
  192. * @param[in,out] ctx - pcilib context
  193. * @param[in] k - kernel memory handle returned from pcilib_alloc_kernel_memory() call
  194. * @return - bus address or 0 if no bus mapping
  195. */
  196. uintptr_t pcilib_kmem_get_ba(pcilib_t *ctx, pcilib_kmem_handle_t *k);
  197. /**
  198. * Get a valid pointer on the user-space mapping of the specified kernel memory buffer
  199. *
  200. * @param[in,out] ctx - pcilib context
  201. * @param[in] k - kernel memory handle returned from pcilib_alloc_kernel_memory() call
  202. * @param[in] block - specifies the buffer within the kernel memory (buffers are numbered from 0)
  203. * @return - user-space pointer
  204. */
  205. volatile void *pcilib_kmem_get_block_ua(pcilib_t *ctx, pcilib_kmem_handle_t *k, size_t block);
  206. /**
  207. * Get a physical address of the specified kernel memory buffer
  208. *
  209. * @param[in,out] ctx - pcilib context
  210. * @param[in] k - kernel memory handle returned from pcilib_alloc_kernel_memory() call
  211. * @param[in] block - specifies the buffer within the kernel memory (buffers are numbered from 0)
  212. * @return - physical address
  213. */
  214. uintptr_t pcilib_kmem_get_block_pa(pcilib_t *ctx, pcilib_kmem_handle_t *k, size_t block);
  215. /**
  216. * Get a bus address of the specified kernel memory buffer
  217. *
  218. * @param[in,out] ctx - pcilib context
  219. * @param[in] k - kernel memory handle returned from pcilib_alloc_kernel_memory() call
  220. * @param[in] block - specifies the buffer within the kernel memory (buffers are numbered from 0)
  221. * @return - bus address or 0 if no bus mapping
  222. */
  223. uintptr_t pcilib_kmem_get_block_ba(pcilib_t *ctx, pcilib_kmem_handle_t *k, size_t block);
  224. /**
  225. * Get size of the specified kernel memory buffer
  226. *
  227. * @param[in,out] ctx - pcilib context
  228. * @param[in] k - kernel memory handle returned from pcilib_alloc_kernel_memory() call
  229. * @param[in] block - specifies the buffer within the kernel memory (buffers are numbered from 0)
  230. * @return - size in bytes
  231. */
  232. size_t pcilib_kmem_get_block_size(pcilib_t *ctx, pcilib_kmem_handle_t *k, size_t block);
  233. /**
  234. * Reports if the kernel memory was reused fully, partially, or allocated completely anew
  235. *
  236. * @param[in,out] ctx - pcilib context
  237. * @param[in] k - kernel memory handle returned from pcilib_alloc_kernel_memory() call
  238. * @return - re-use information
  239. */
  240. pcilib_kmem_reuse_state_t pcilib_kmem_is_reused(pcilib_t *ctx, pcilib_kmem_handle_t *k);
  241. #ifdef __cplusplus
  242. }
  243. #endif
  244. #endif /* _PCILIB_KMEM_H */