1 /* SPDX-License-Identifier: GPL-2.0 */
3 /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2018-2023 Linaro Ltd.
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>
16 #include "ipa_version.h"
18 /* Maximum number of channels and event rings supported by the driver */
19 #define GSI_CHANNEL_COUNT_MAX 28
20 #define GSI_EVT_RING_COUNT_MAX 28
22 /* Maximum TLV FIFO size for a channel; 64 here is arbitrary (and high) */
23 #define GSI_TLV_MAX 64
27 struct platform_device;
31 struct gsi_channel_data;
32 struct ipa_gsi_endpoint_data;
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 */
39 /* The ring index value indicates the next "open" entry in the ring.
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.
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).
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.
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.
63 * The result of a pool allocation of multiple elements is always
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) */
75 struct gsi_trans_info {
76 atomic_t tre_avail; /* TREs available for allocation */
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 */
87 struct gsi_trans_pool sg_pool; /* scatterlist pool */
88 struct gsi_trans_pool cmd_pool; /* command payload DMA pool */
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,
102 /* We only care about channels between IPA and AP */
106 bool command; /* AP command TX channel or not */
108 u8 trans_tre_max; /* max TREs in a transaction */
112 struct gsi_ring tre_ring;
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 */
123 struct gsi_trans_info trans_info;
125 struct napi_struct napi;
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,
135 struct gsi_evt_ring {
136 struct gsi_channel *channel;
137 struct gsi_ring ring;
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;
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 */
162 * gsi_setup() - Set up the GSI subsystem
163 * @gsi: Address of GSI structure embedded in an IPA structure
165 * Return: 0 if successful, or a negative error code
167 * Performs initialization that must wait until the GSI hardware is
168 * ready (including firmware loaded).
170 int gsi_setup(struct gsi *gsi);
173 * gsi_teardown() - Tear down GSI subsystem
174 * @gsi: GSI address previously passed to a successful gsi_setup() call
176 void gsi_teardown(struct gsi *gsi);
179 * gsi_channel_tre_max() - Channel maximum number of in-flight TREs
181 * @channel_id: Channel whose limit is to be returned
183 * Return: The maximum number of TREs outstanding on the channel
185 u32 gsi_channel_tre_max(struct gsi *gsi, u32 channel_id);
188 * gsi_channel_start() - Start an allocated GSI channel
190 * @channel_id: Channel to start
192 * Return: 0 if successful, or a negative error code
194 int gsi_channel_start(struct gsi *gsi, u32 channel_id);
197 * gsi_channel_stop() - Stop a started GSI channel
198 * @gsi: GSI pointer returned by gsi_setup()
199 * @channel_id: Channel to stop
201 * Return: 0 if successful, or a negative error code
203 int gsi_channel_stop(struct gsi *gsi, u32 channel_id);
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)
211 void gsi_modem_channel_flow_control(struct gsi *gsi, u32 channel_id,
215 * gsi_channel_reset() - Reset an allocated GSI channel
217 * @channel_id: Channel to be reset
218 * @doorbell: Whether to (possibly) enable the doorbell engine
220 * Reset a channel and reconfigure it. The @doorbell flag indicates
221 * that the doorbell engine should be enabled if needed.
223 * GSI hardware relinquishes ownership of all pending receive buffer
224 * transactions and they will complete with their cancelled flag set.
226 void gsi_channel_reset(struct gsi *gsi, u32 channel_id, bool doorbell);
229 * gsi_suspend() - Prepare the GSI subsystem for suspend
232 void gsi_suspend(struct gsi *gsi);
235 * gsi_resume() - Resume the GSI subsystem following suspend
238 void gsi_resume(struct gsi *gsi);
241 * gsi_channel_suspend() - Suspend a GSI channel
243 * @channel_id: Channel to suspend
245 * For IPA v4.0+, suspend is implemented by stopping the channel.
247 int gsi_channel_suspend(struct gsi *gsi, u32 channel_id);
250 * gsi_channel_resume() - Resume a suspended GSI channel
252 * @channel_id: Channel to resume
254 * For IPA v4.0+, the stopped channel is started again.
256 int gsi_channel_resume(struct gsi *gsi, u32 channel_id);
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
266 * Return: 0 if successful, or a negative error code
268 * Early stage initialization of the GSI subsystem, performing tasks
269 * that can be done before the GSI hardware is ready to use.
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);
276 * gsi_exit() - Exit the GSI subsystem
277 * @gsi: GSI address previously passed to a successful gsi_init() call
279 void gsi_exit(struct gsi *gsi);