1 /* SPDX-License-Identifier: GPL-2.0 */
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2019-2020 Linaro Ltd.
9 #include <linux/types.h>
10 #include <linux/refcount.h>
11 #include <linux/completion.h>
12 #include <linux/dma-direction.h>
23 struct gsi_trans_pool;
25 /* Maximum number of TREs in an IPA immediate command transaction */
26 #define IPA_COMMAND_TRANS_TRE_MAX 8
29 * struct gsi_trans - a GSI transaction
31 * Most fields in this structure for internal use by the transaction core code:
32 * @links: Links for channel transaction lists by state
34 * @channel_id: Channel number transaction is associated with
35 * @cancelled: If set by the core code, transaction was cancelled
36 * @rsvd_count: Number of TREs reserved for this transaction
37 * @used_count: Number of TREs *used* (could be less than rsvd_count)
38 * @len: Number of bytes sent or received by the transaction
39 * @data: Preserved but not touched by the core transaction code
40 * @cmd_opcode: Array of command opcodes (command channel only)
41 * @sgl: An array of scatter/gather entries managed by core code
42 * @direction: DMA transfer direction (DMA_NONE for commands)
43 * @refcount: Reference count used for destruction
44 * @completion: Completed when the transaction completes
45 * @byte_count: TX channel byte count recorded when transaction committed
46 * @trans_count: Channel transaction count when committed (for BQL accounting)
48 * The @len field is set when the transaction is committed. For RX
49 * transactions it is updated later to reflect the actual number of bytes
53 struct list_head links; /* gsi_channel lists */
58 bool cancelled; /* true if transaction was cancelled */
60 u8 rsvd_count; /* # TREs requested */
61 u8 used_count; /* # entries used in sgl[] */
62 u32 len; /* total # bytes across sgl[] */
66 u8 cmd_opcode[IPA_COMMAND_TRANS_TRE_MAX];
68 struct scatterlist *sgl;
69 enum dma_data_direction direction;
72 struct completion completion;
74 u64 byte_count; /* channel byte_count when committed */
75 u64 trans_count; /* channel trans_count when committed */
79 * gsi_trans_pool_init() - Initialize a pool of structures for transactions
80 * @pool: GSI transaction poll pointer
81 * @size: Size of elements in the pool
82 * @count: Minimum number of elements in the pool
83 * @max_alloc: Maximum number of elements allocated at a time from pool
85 * Return: 0 if successful, or a negative error code
87 int gsi_trans_pool_init(struct gsi_trans_pool *pool, size_t size, u32 count,
91 * gsi_trans_pool_alloc() - Allocate one or more elements from a pool
93 * @count: Number of elements to allocate from the pool
95 * Return: Virtual address of element(s) allocated from the pool
97 void *gsi_trans_pool_alloc(struct gsi_trans_pool *pool, u32 count);
100 * gsi_trans_pool_exit() - Inverse of gsi_trans_pool_init()
101 * @pool: Pool pointer
103 void gsi_trans_pool_exit(struct gsi_trans_pool *pool);
106 * gsi_trans_pool_init_dma() - Initialize a pool of DMA-able structures
107 * @dev: Device used for DMA
108 * @pool: Pool pointer
109 * @size: Size of elements in the pool
110 * @count: Minimum number of elements in the pool
111 * @max_alloc: Maximum number of elements allocated at a time from pool
113 * Return: 0 if successful, or a negative error code
115 * Structures in this pool reside in DMA-coherent memory.
117 int gsi_trans_pool_init_dma(struct device *dev, struct gsi_trans_pool *pool,
118 size_t size, u32 count, u32 max_alloc);
121 * gsi_trans_pool_alloc_dma() - Allocate an element from a DMA pool
122 * @pool: DMA pool pointer
123 * @addr: DMA address "handle" associated with the allocation
125 * Return: Virtual address of element allocated from the pool
127 * Only one element at a time may be allocated from a DMA pool.
129 void *gsi_trans_pool_alloc_dma(struct gsi_trans_pool *pool, dma_addr_t *addr);
132 * gsi_trans_pool_exit_dma() - Inverse of gsi_trans_pool_init_dma()
133 * @dev: Device used for DMA
134 * @pool: Pool pointer
136 void gsi_trans_pool_exit_dma(struct device *dev, struct gsi_trans_pool *pool);
139 * gsi_channel_trans_idle() - Return whether no transactions are allocated
141 * @channel_id: Channel the transaction is associated with
143 * Return: True if no transactions are allocated, false otherwise
146 bool gsi_channel_trans_idle(struct gsi *gsi, u32 channel_id);
149 * gsi_channel_trans_alloc() - Allocate a GSI transaction on a channel
151 * @channel_id: Channel the transaction is associated with
152 * @tre_count: Number of elements in the transaction
153 * @direction: DMA direction for entire SGL (or DMA_NONE)
155 * Return: A GSI transaction structure, or a null pointer if all
156 * available transactions are in use
158 struct gsi_trans *gsi_channel_trans_alloc(struct gsi *gsi, u32 channel_id,
160 enum dma_data_direction direction);
163 * gsi_trans_free() - Free a previously-allocated GSI transaction
164 * @trans: Transaction to be freed
166 void gsi_trans_free(struct gsi_trans *trans);
169 * gsi_trans_cmd_add() - Add an immediate command to a transaction
170 * @trans: Transaction
171 * @buf: Buffer pointer for command payload
172 * @size: Number of bytes in buffer
173 * @addr: DMA address for payload
174 * @opcode: IPA immediate command opcode
176 void gsi_trans_cmd_add(struct gsi_trans *trans, void *buf, u32 size,
177 dma_addr_t addr, enum ipa_cmd_opcode opcode);
180 * gsi_trans_page_add() - Add a page transfer to a transaction
181 * @trans: Transaction
182 * @page: Page pointer
183 * @size: Number of bytes (starting at offset) to transfer
184 * @offset: Offset within page for start of transfer
186 int gsi_trans_page_add(struct gsi_trans *trans, struct page *page, u32 size,
190 * gsi_trans_skb_add() - Add a socket transfer to a transaction
191 * @trans: Transaction
192 * @skb: Socket buffer for transfer (outbound)
194 * Return: 0, or -EMSGSIZE if socket data won't fit in transaction.
196 int gsi_trans_skb_add(struct gsi_trans *trans, struct sk_buff *skb);
199 * gsi_trans_commit() - Commit a GSI transaction
200 * @trans: Transaction to commit
201 * @ring_db: Whether to tell the hardware about these queued transfers
203 void gsi_trans_commit(struct gsi_trans *trans, bool ring_db);
206 * gsi_trans_commit_wait() - Commit a GSI transaction and wait for it
208 * @trans: Transaction to commit
210 void gsi_trans_commit_wait(struct gsi_trans *trans);
213 * gsi_trans_read_byte() - Issue a single byte read TRE on a channel
215 * @channel_id: Channel on which to read a byte
216 * @addr: DMA address into which to transfer the one byte
218 * This is not a transaction operation at all. It's defined here because
219 * it needs to be done in coordination with other transaction activity.
221 int gsi_trans_read_byte(struct gsi *gsi, u32 channel_id, dma_addr_t addr);
224 * gsi_trans_read_byte_done() - Clean up after a single byte read TRE
226 * @channel_id: Channel on which byte was read
228 * This function needs to be called to signal that the work related
229 * to reading a byte initiated by gsi_trans_read_byte() is complete.
231 void gsi_trans_read_byte_done(struct gsi *gsi, u32 channel_id);
233 #endif /* _GSI_TRANS_H_ */