1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
4  * Copyright (C) 2018-2024 Linaro Ltd.
5  */
6 #ifndef _GSI_H_
7 #define _GSI_H_
8 
9 #include <linux/completion.h>
10 #include <linux/mutex.h>
11 #include <linux/netdevice.h>
12 #include <linux/types.h>
13 
14 #include "ipa_version.h"
15 
16 /* Maximum number of channels and event rings supported by the driver */
17 #define GSI_CHANNEL_COUNT_MAX	28
18 #define GSI_EVT_RING_COUNT_MAX	28
19 
20 /* Maximum TLV FIFO size for a channel; 64 here is arbitrary (and high) */
21 #define GSI_TLV_MAX		64
22 
23 struct device;
24 struct platform_device;
25 
26 struct gsi;
27 struct gsi_trans;
28 struct ipa_gsi_endpoint_data;
29 
30 struct gsi_ring {
31 	void *virt;			/* ring array base address */
32 	dma_addr_t addr;		/* primarily low 32 bits used */
33 	u32 count;			/* number of elements in ring */
34 
35 	/* The ring index value indicates the next "open" entry in the ring.
36 	 *
37 	 * A channel ring consists of TRE entries filled by the AP and passed
38 	 * to the hardware for processing.  For a channel ring, the ring index
39 	 * identifies the next unused entry to be filled by the AP.  In this
40 	 * case the initial value is assumed by hardware to be 0.
41 	 *
42 	 * An event ring consists of event structures filled by the hardware
43 	 * and passed to the AP.  For event rings, the ring index identifies
44 	 * the next ring entry that is not known to have been filled by the
45 	 * hardware.  The initial value used is arbitrary (so we use 0).
46 	 */
47 	u32 index;
48 };
49 
50 /* Transactions use several resources that can be allocated dynamically
51  * but taken from a fixed-size pool.  The number of elements required for
52  * the pool is limited by the total number of TREs that can be outstanding.
53  *
54  * If sufficient TREs are available to reserve for a transaction,
55  * allocation from these pools is guaranteed to succeed.  Furthermore,
56  * these resources are implicitly freed whenever the TREs in the
57  * transaction they're associated with are released.
58  *
59  * The result of a pool allocation of multiple elements is always
60  * contiguous.
61  */
62 struct gsi_trans_pool {
63 	void *base;			/* base address of element pool */
64 	u32 count;			/* # elements in the pool */
65 	u32 free;			/* next free element in pool (modulo) */
66 	u32 size;			/* size (bytes) of an element */
67 	u32 max_alloc;			/* max allocation request */
68 	dma_addr_t addr;		/* DMA address if DMA pool (or 0) */
69 };
70 
71 struct gsi_trans_info {
72 	atomic_t tre_avail;		/* TREs available for allocation */
73 
74 	u16 free_id;			/* first free trans in array */
75 	u16 allocated_id;		/* first allocated transaction */
76 	u16 committed_id;		/* first committed transaction */
77 	u16 pending_id;			/* first pending transaction */
78 	u16 completed_id;		/* first completed transaction */
79 	u16 polled_id;			/* first polled transaction */
80 	struct gsi_trans *trans;	/* transaction array */
81 	struct gsi_trans **map;		/* TRE -> transaction map */
82 
83 	struct gsi_trans_pool sg_pool;	/* scatterlist pool */
84 	struct gsi_trans_pool cmd_pool;	/* command payload DMA pool */
85 };
86 
87 /* Hardware values signifying the state of a channel */
88 enum gsi_channel_state {
89 	GSI_CHANNEL_STATE_NOT_ALLOCATED		= 0x0,
90 	GSI_CHANNEL_STATE_ALLOCATED		= 0x1,
91 	GSI_CHANNEL_STATE_STARTED		= 0x2,
92 	GSI_CHANNEL_STATE_STOPPED		= 0x3,
93 	GSI_CHANNEL_STATE_STOP_IN_PROC		= 0x4,
94 	GSI_CHANNEL_STATE_FLOW_CONTROLLED	= 0x5,	/* IPA v4.2-v4.9 */
95 	GSI_CHANNEL_STATE_ERROR			= 0xf,
96 };
97 
98 /* We only care about channels between IPA and AP */
99 struct gsi_channel {
100 	struct gsi *gsi;
101 	bool toward_ipa;
102 	bool command;			/* AP command TX channel or not */
103 
104 	u8 trans_tre_max;		/* max TREs in a transaction */
105 	u16 tre_count;
106 	u16 event_count;
107 
108 	struct gsi_ring tre_ring;
109 	u32 evt_ring_id;
110 
111 	/* The following counts are used only for TX endpoints */
112 	u64 byte_count;			/* total # bytes transferred */
113 	u64 trans_count;		/* total # transactions */
114 	u64 queued_byte_count;		/* last reported queued byte count */
115 	u64 queued_trans_count;		/* ...and queued trans count */
116 	u64 compl_byte_count;		/* last reported completed byte count */
117 	u64 compl_trans_count;		/* ...and completed trans count */
118 
119 	struct gsi_trans_info trans_info;
120 
121 	struct napi_struct napi;
122 };
123 
124 /* Hardware values signifying the state of an event ring */
125 enum gsi_evt_ring_state {
126 	GSI_EVT_RING_STATE_NOT_ALLOCATED	= 0x0,
127 	GSI_EVT_RING_STATE_ALLOCATED		= 0x1,
128 	GSI_EVT_RING_STATE_ERROR		= 0xf,
129 };
130 
131 struct gsi_evt_ring {
132 	struct gsi_channel *channel;
133 	struct gsi_ring ring;
134 };
135 
136 struct gsi {
137 	struct device *dev;		/* Same as IPA device */
138 	enum ipa_version version;
139 	void __iomem *virt;		/* I/O mapped registers */
140 	const struct regs *regs;
141 
142 	u32 irq;
143 	u32 channel_count;
144 	u32 evt_ring_count;
145 	u32 event_bitmap;		/* allocated event rings */
146 	u32 modem_channel_bitmap;	/* modem channels to allocate */
147 	u32 type_enabled_bitmap;	/* GSI IRQ types enabled */
148 	u32 ieob_enabled_bitmap;	/* IEOB IRQ enabled (event rings) */
149 	int result;			/* Negative errno (generic commands) */
150 	struct completion completion;	/* Signals GSI command completion */
151 	struct mutex mutex;		/* protects commands, programming */
152 	struct gsi_channel channel[GSI_CHANNEL_COUNT_MAX];
153 	struct gsi_evt_ring evt_ring[GSI_EVT_RING_COUNT_MAX];
154 	struct net_device *dummy_dev;	/* needed for NAPI */
155 };
156 
157 /**
158  * gsi_setup() - Set up the GSI subsystem
159  * @gsi:	Address of GSI structure embedded in an IPA structure
160  *
161  * Return:	0 if successful, or a negative error code
162  *
163  * Performs initialization that must wait until the GSI hardware is
164  * ready (including firmware loaded).
165  */
166 int gsi_setup(struct gsi *gsi);
167 
168 /**
169  * gsi_teardown() - Tear down GSI subsystem
170  * @gsi:	GSI address previously passed to a successful gsi_setup() call
171  */
172 void gsi_teardown(struct gsi *gsi);
173 
174 /**
175  * gsi_channel_tre_max() - Channel maximum number of in-flight TREs
176  * @gsi:	GSI pointer
177  * @channel_id:	Channel whose limit is to be returned
178  *
179  * Return:	 The maximum number of TREs outstanding on the channel
180  */
181 u32 gsi_channel_tre_max(struct gsi *gsi, u32 channel_id);
182 
183 /**
184  * gsi_channel_start() - Start an allocated GSI channel
185  * @gsi:	GSI pointer
186  * @channel_id:	Channel to start
187  *
188  * Return:	0 if successful, or a negative error code
189  */
190 int gsi_channel_start(struct gsi *gsi, u32 channel_id);
191 
192 /**
193  * gsi_channel_stop() - Stop a started GSI channel
194  * @gsi:	GSI pointer returned by gsi_setup()
195  * @channel_id:	Channel to stop
196  *
197  * Return:	0 if successful, or a negative error code
198  */
199 int gsi_channel_stop(struct gsi *gsi, u32 channel_id);
200 
201 /**
202  * gsi_modem_channel_flow_control() - Set channel flow control state (IPA v4.2+)
203  * @gsi:	GSI pointer returned by gsi_setup()
204  * @channel_id:	Modem TX channel to control
205  * @enable:	Whether to enable flow control (i.e., prevent flow)
206  */
207 void gsi_modem_channel_flow_control(struct gsi *gsi, u32 channel_id,
208 				    bool enable);
209 
210 /**
211  * gsi_channel_reset() - Reset an allocated GSI channel
212  * @gsi:	GSI pointer
213  * @channel_id:	Channel to be reset
214  * @doorbell:	Whether to (possibly) enable the doorbell engine
215  *
216  * Reset a channel and reconfigure it.  The @doorbell flag indicates
217  * that the doorbell engine should be enabled if needed.
218  *
219  * GSI hardware relinquishes ownership of all pending receive buffer
220  * transactions and they will complete with their cancelled flag set.
221  */
222 void gsi_channel_reset(struct gsi *gsi, u32 channel_id, bool doorbell);
223 
224 /**
225  * gsi_suspend() - Prepare the GSI subsystem for suspend
226  * @gsi:	GSI pointer
227  */
228 void gsi_suspend(struct gsi *gsi);
229 
230 /**
231  * gsi_resume() - Resume the GSI subsystem following suspend
232  * @gsi:	GSI pointer
233  */
234 void gsi_resume(struct gsi *gsi);
235 
236 /**
237  * gsi_channel_suspend() - Suspend a GSI channel
238  * @gsi:	GSI pointer
239  * @channel_id:	Channel to suspend
240  *
241  * For IPA v4.0+, suspend is implemented by stopping the channel.
242  */
243 int gsi_channel_suspend(struct gsi *gsi, u32 channel_id);
244 
245 /**
246  * gsi_channel_resume() - Resume a suspended GSI channel
247  * @gsi:	GSI pointer
248  * @channel_id:	Channel to resume
249  *
250  * For IPA v4.0+, the stopped channel is started again.
251  */
252 int gsi_channel_resume(struct gsi *gsi, u32 channel_id);
253 
254 /**
255  * gsi_init() - Initialize the GSI subsystem
256  * @gsi:	Address of GSI structure embedded in an IPA structure
257  * @pdev:	IPA platform device
258  * @version:	IPA hardware version (implies GSI version)
259  * @count:	Number of entries in the configuration data array
260  * @data:	Endpoint and channel configuration data
261  *
262  * Return:	0 if successful, or a negative error code
263  *
264  * Early stage initialization of the GSI subsystem, performing tasks
265  * that can be done before the GSI hardware is ready to use.
266  */
267 int gsi_init(struct gsi *gsi, struct platform_device *pdev,
268 	     enum ipa_version version, u32 count,
269 	     const struct ipa_gsi_endpoint_data *data);
270 
271 /**
272  * gsi_exit() - Exit the GSI subsystem
273  * @gsi:	GSI address previously passed to a successful gsi_init() call
274  */
275 void gsi_exit(struct gsi *gsi);
276 
277 #endif /* _GSI_H_ */
278