gsi_trans.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2019-2024 Linaro Ltd.
  4. */
  5. #ifndef _GSI_TRANS_H_
  6. #define _GSI_TRANS_H_
  7. #include <linux/completion.h>
  8. #include <linux/dma-direction.h>
  9. #include <linux/refcount.h>
  10. #include <linux/types.h>
  11. #include "ipa_cmd.h"
  12. struct device;
  13. struct page;
  14. struct scatterlist;
  15. struct sk_buff;
  16. struct gsi;
  17. struct gsi_trans_pool;
  18. /* Maximum number of TREs in an IPA immediate command transaction */
  19. #define IPA_COMMAND_TRANS_TRE_MAX 8
  20. /**
  21. * struct gsi_trans - a GSI transaction
  22. *
  23. * Most fields in this structure for internal use by the transaction core code:
  24. * @gsi: GSI pointer
  25. * @channel_id: Channel number transaction is associated with
  26. * @cancelled: If set by the core code, transaction was cancelled
  27. * @rsvd_count: Number of TREs reserved for this transaction
  28. * @used_count: Number of TREs *used* (could be less than rsvd_count)
  29. * @len: Number of bytes sent or received by the transaction
  30. * @data: Preserved but not touched by the core transaction code
  31. * @cmd_opcode: Array of command opcodes (command channel only)
  32. * @sgl: An array of scatter/gather entries managed by core code
  33. * @direction: DMA transfer direction (DMA_NONE for commands)
  34. * @refcount: Reference count used for destruction
  35. * @completion: Completed when the transaction completes
  36. * @byte_count: TX channel byte count recorded when transaction committed
  37. * @trans_count: Channel transaction count when committed (for BQL accounting)
  38. *
  39. * The @len field is set when the transaction is committed. For RX
  40. * transactions it is updated later to reflect the actual number of bytes
  41. * received.
  42. */
  43. struct gsi_trans {
  44. struct gsi *gsi;
  45. u8 channel_id;
  46. bool cancelled; /* true if transaction was cancelled */
  47. u8 rsvd_count; /* # TREs requested */
  48. u8 used_count; /* # entries used in sgl[] */
  49. u32 len; /* total # bytes across sgl[] */
  50. union {
  51. void *data;
  52. u8 cmd_opcode[IPA_COMMAND_TRANS_TRE_MAX];
  53. };
  54. struct scatterlist *sgl;
  55. enum dma_data_direction direction;
  56. refcount_t refcount;
  57. struct completion completion;
  58. u64 byte_count; /* channel byte_count when committed */
  59. u64 trans_count; /* channel trans_count when committed */
  60. };
  61. /**
  62. * gsi_trans_pool_init() - Initialize a pool of structures for transactions
  63. * @pool: GSI transaction pool pointer
  64. * @size: Size of elements in the pool
  65. * @count: Minimum number of elements in the pool
  66. * @max_alloc: Maximum number of elements allocated at a time from pool
  67. *
  68. * Return: 0 if successful, or a negative error code
  69. */
  70. int gsi_trans_pool_init(struct gsi_trans_pool *pool, size_t size, u32 count,
  71. u32 max_alloc);
  72. /**
  73. * gsi_trans_pool_alloc() - Allocate one or more elements from a pool
  74. * @pool: Pool pointer
  75. * @count: Number of elements to allocate from the pool
  76. *
  77. * Return: Virtual address of element(s) allocated from the pool
  78. */
  79. void *gsi_trans_pool_alloc(struct gsi_trans_pool *pool, u32 count);
  80. /**
  81. * gsi_trans_pool_exit() - Inverse of gsi_trans_pool_init()
  82. * @pool: Pool pointer
  83. */
  84. void gsi_trans_pool_exit(struct gsi_trans_pool *pool);
  85. /**
  86. * gsi_trans_pool_init_dma() - Initialize a pool of DMA-able structures
  87. * @dev: Device used for DMA
  88. * @pool: Pool pointer
  89. * @size: Size of elements in the pool
  90. * @count: Minimum number of elements in the pool
  91. * @max_alloc: Maximum number of elements allocated at a time from pool
  92. *
  93. * Return: 0 if successful, or a negative error code
  94. *
  95. * Structures in this pool reside in DMA-coherent memory.
  96. */
  97. int gsi_trans_pool_init_dma(struct device *dev, struct gsi_trans_pool *pool,
  98. size_t size, u32 count, u32 max_alloc);
  99. /**
  100. * gsi_trans_pool_alloc_dma() - Allocate an element from a DMA pool
  101. * @pool: DMA pool pointer
  102. * @addr: DMA address "handle" associated with the allocation
  103. *
  104. * Return: Virtual address of element allocated from the pool
  105. *
  106. * Only one element at a time may be allocated from a DMA pool.
  107. */
  108. void *gsi_trans_pool_alloc_dma(struct gsi_trans_pool *pool, dma_addr_t *addr);
  109. /**
  110. * gsi_trans_pool_exit_dma() - Inverse of gsi_trans_pool_init_dma()
  111. * @dev: Device used for DMA
  112. * @pool: Pool pointer
  113. */
  114. void gsi_trans_pool_exit_dma(struct device *dev, struct gsi_trans_pool *pool);
  115. /**
  116. * gsi_channel_trans_idle() - Return whether no transactions are allocated
  117. * @gsi: GSI pointer
  118. * @channel_id: Channel the transaction is associated with
  119. *
  120. * Return: True if no transactions are allocated, false otherwise
  121. *
  122. */
  123. bool gsi_channel_trans_idle(struct gsi *gsi, u32 channel_id);
  124. /**
  125. * gsi_channel_trans_alloc() - Allocate a GSI transaction on a channel
  126. * @gsi: GSI pointer
  127. * @channel_id: Channel the transaction is associated with
  128. * @tre_count: Number of elements in the transaction
  129. * @direction: DMA direction for entire SGL (or DMA_NONE)
  130. *
  131. * Return: A GSI transaction structure, or a null pointer if all
  132. * available transactions are in use
  133. */
  134. struct gsi_trans *gsi_channel_trans_alloc(struct gsi *gsi, u32 channel_id,
  135. u32 tre_count,
  136. enum dma_data_direction direction);
  137. /**
  138. * gsi_trans_free() - Free a previously-allocated GSI transaction
  139. * @trans: Transaction to be freed
  140. */
  141. void gsi_trans_free(struct gsi_trans *trans);
  142. /**
  143. * gsi_trans_cmd_add() - Add an immediate command to a transaction
  144. * @trans: Transaction
  145. * @buf: Buffer pointer for command payload
  146. * @size: Number of bytes in buffer
  147. * @addr: DMA address for payload
  148. * @opcode: IPA immediate command opcode
  149. */
  150. void gsi_trans_cmd_add(struct gsi_trans *trans, void *buf, u32 size,
  151. dma_addr_t addr, enum ipa_cmd_opcode opcode);
  152. /**
  153. * gsi_trans_page_add() - Add a page transfer to a transaction
  154. * @trans: Transaction
  155. * @page: Page pointer
  156. * @size: Number of bytes (starting at offset) to transfer
  157. * @offset: Offset within page for start of transfer
  158. */
  159. int gsi_trans_page_add(struct gsi_trans *trans, struct page *page, u32 size,
  160. u32 offset);
  161. /**
  162. * gsi_trans_skb_add() - Add a socket transfer to a transaction
  163. * @trans: Transaction
  164. * @skb: Socket buffer for transfer (outbound)
  165. *
  166. * Return: 0, or -EMSGSIZE if socket data won't fit in transaction.
  167. */
  168. int gsi_trans_skb_add(struct gsi_trans *trans, struct sk_buff *skb);
  169. /**
  170. * gsi_trans_commit() - Commit a GSI transaction
  171. * @trans: Transaction to commit
  172. * @ring_db: Whether to tell the hardware about these queued transfers
  173. */
  174. void gsi_trans_commit(struct gsi_trans *trans, bool ring_db);
  175. /**
  176. * gsi_trans_commit_wait() - Commit a GSI transaction and wait for it
  177. * to complete
  178. * @trans: Transaction to commit
  179. */
  180. void gsi_trans_commit_wait(struct gsi_trans *trans);
  181. /**
  182. * gsi_trans_read_byte() - Issue a single byte read TRE on a channel
  183. * @gsi: GSI pointer
  184. * @channel_id: Channel on which to read a byte
  185. * @addr: DMA address into which to transfer the one byte
  186. *
  187. * This is not a transaction operation at all. It's defined here because
  188. * it needs to be done in coordination with other transaction activity.
  189. */
  190. int gsi_trans_read_byte(struct gsi *gsi, u32 channel_id, dma_addr_t addr);
  191. /**
  192. * gsi_trans_read_byte_done() - Clean up after a single byte read TRE
  193. * @gsi: GSI pointer
  194. * @channel_id: Channel on which byte was read
  195. *
  196. * This function needs to be called to signal that the work related
  197. * to reading a byte initiated by gsi_trans_read_byte() is complete.
  198. */
  199. void gsi_trans_read_byte_done(struct gsi *gsi, u32 channel_id);
  200. #endif /* _GSI_TRANS_H_ */