gsi_trans.h 7.3 KB

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