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