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