xref: /openbmc/linux/drivers/net/ipa/gsi.h (revision c4a7b9b5)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
4  * Copyright (C) 2018-2022 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_raw;		/* I/O mapped address range */
144 	void __iomem *virt;		/* Adjusted for most registers */
145 	u32 irq;
146 	u32 channel_count;
147 	u32 evt_ring_count;
148 	u32 event_bitmap;		/* allocated event rings */
149 	u32 modem_channel_bitmap;	/* modem channels to allocate */
150 	u32 type_enabled_bitmap;	/* GSI IRQ types enabled */
151 	u32 ieob_enabled_bitmap;	/* IEOB IRQ enabled (event rings) */
152 	int result;			/* Negative errno (generic commands) */
153 	struct completion completion;	/* Signals GSI command completion */
154 	struct mutex mutex;		/* protects commands, programming */
155 	struct gsi_channel channel[GSI_CHANNEL_COUNT_MAX];
156 	struct gsi_evt_ring evt_ring[GSI_EVT_RING_COUNT_MAX];
157 	struct net_device dummy_dev;	/* needed for NAPI */
158 };
159 
160 /**
161  * gsi_setup() - Set up the GSI subsystem
162  * @gsi:	Address of GSI structure embedded in an IPA structure
163  *
164  * Return:	0 if successful, or a negative error code
165  *
166  * Performs initialization that must wait until the GSI hardware is
167  * ready (including firmware loaded).
168  */
169 int gsi_setup(struct gsi *gsi);
170 
171 /**
172  * gsi_teardown() - Tear down GSI subsystem
173  * @gsi:	GSI address previously passed to a successful gsi_setup() call
174  */
175 void gsi_teardown(struct gsi *gsi);
176 
177 /**
178  * gsi_channel_tre_max() - Channel maximum number of in-flight TREs
179  * @gsi:	GSI pointer
180  * @channel_id:	Channel whose limit is to be returned
181  *
182  * Return:	 The maximum number of TREs outstanding on the channel
183  */
184 u32 gsi_channel_tre_max(struct gsi *gsi, u32 channel_id);
185 
186 /**
187  * gsi_channel_start() - Start an allocated GSI channel
188  * @gsi:	GSI pointer
189  * @channel_id:	Channel to start
190  *
191  * Return:	0 if successful, or a negative error code
192  */
193 int gsi_channel_start(struct gsi *gsi, u32 channel_id);
194 
195 /**
196  * gsi_channel_stop() - Stop a started GSI channel
197  * @gsi:	GSI pointer returned by gsi_setup()
198  * @channel_id:	Channel to stop
199  *
200  * Return:	0 if successful, or a negative error code
201  */
202 int gsi_channel_stop(struct gsi *gsi, u32 channel_id);
203 
204 /**
205  * gsi_modem_channel_flow_control() - Set channel flow control state (IPA v4.2+)
206  * @gsi:	GSI pointer returned by gsi_setup()
207  * @channel_id:	Modem TX channel to control
208  * @enable:	Whether to enable flow control (i.e., prevent flow)
209  */
210 void gsi_modem_channel_flow_control(struct gsi *gsi, u32 channel_id,
211 				    bool enable);
212 
213 /**
214  * gsi_channel_reset() - Reset an allocated GSI channel
215  * @gsi:	GSI pointer
216  * @channel_id:	Channel to be reset
217  * @doorbell:	Whether to (possibly) enable the doorbell engine
218  *
219  * Reset a channel and reconfigure it.  The @doorbell flag indicates
220  * that the doorbell engine should be enabled if needed.
221  *
222  * GSI hardware relinquishes ownership of all pending receive buffer
223  * transactions and they will complete with their cancelled flag set.
224  */
225 void gsi_channel_reset(struct gsi *gsi, u32 channel_id, bool doorbell);
226 
227 /**
228  * gsi_suspend() - Prepare the GSI subsystem for suspend
229  * @gsi:	GSI pointer
230  */
231 void gsi_suspend(struct gsi *gsi);
232 
233 /**
234  * gsi_resume() - Resume the GSI subsystem following suspend
235  * @gsi:	GSI pointer
236  */
237 void gsi_resume(struct gsi *gsi);
238 
239 /**
240  * gsi_channel_suspend() - Suspend a GSI channel
241  * @gsi:	GSI pointer
242  * @channel_id:	Channel to suspend
243  *
244  * For IPA v4.0+, suspend is implemented by stopping the channel.
245  */
246 int gsi_channel_suspend(struct gsi *gsi, u32 channel_id);
247 
248 /**
249  * gsi_channel_resume() - Resume a suspended GSI channel
250  * @gsi:	GSI pointer
251  * @channel_id:	Channel to resume
252  *
253  * For IPA v4.0+, the stopped channel is started again.
254  */
255 int gsi_channel_resume(struct gsi *gsi, u32 channel_id);
256 
257 /**
258  * gsi_init() - Initialize the GSI subsystem
259  * @gsi:	Address of GSI structure embedded in an IPA structure
260  * @pdev:	IPA platform device
261  * @version:	IPA hardware version (implies GSI version)
262  * @count:	Number of entries in the configuration data array
263  * @data:	Endpoint and channel configuration data
264  *
265  * Return:	0 if successful, or a negative error code
266  *
267  * Early stage initialization of the GSI subsystem, performing tasks
268  * that can be done before the GSI hardware is ready to use.
269  */
270 int gsi_init(struct gsi *gsi, struct platform_device *pdev,
271 	     enum ipa_version version, u32 count,
272 	     const struct ipa_gsi_endpoint_data *data);
273 
274 /**
275  * gsi_exit() - Exit the GSI subsystem
276  * @gsi:	GSI address previously passed to a successful gsi_init() call
277  */
278 void gsi_exit(struct gsi *gsi);
279 
280 #endif /* _GSI_H_ */
281