xref: /openbmc/linux/drivers/net/ipa/gsi.c (revision 14dbf977dd45115a13a64ab480886598ee0ba755)
1650d1603SAlex Elder // SPDX-License-Identifier: GPL-2.0
2650d1603SAlex Elder 
3650d1603SAlex Elder /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
4650d1603SAlex Elder  * Copyright (C) 2018-2020 Linaro Ltd.
5650d1603SAlex Elder  */
6650d1603SAlex Elder 
7650d1603SAlex Elder #include <linux/types.h>
8650d1603SAlex Elder #include <linux/bits.h>
9650d1603SAlex Elder #include <linux/bitfield.h>
10650d1603SAlex Elder #include <linux/mutex.h>
11650d1603SAlex Elder #include <linux/completion.h>
12650d1603SAlex Elder #include <linux/io.h>
13650d1603SAlex Elder #include <linux/bug.h>
14650d1603SAlex Elder #include <linux/interrupt.h>
15650d1603SAlex Elder #include <linux/platform_device.h>
16650d1603SAlex Elder #include <linux/netdevice.h>
17650d1603SAlex Elder 
18650d1603SAlex Elder #include "gsi.h"
19650d1603SAlex Elder #include "gsi_reg.h"
20650d1603SAlex Elder #include "gsi_private.h"
21650d1603SAlex Elder #include "gsi_trans.h"
22650d1603SAlex Elder #include "ipa_gsi.h"
23650d1603SAlex Elder #include "ipa_data.h"
241d0c09deSAlex Elder #include "ipa_version.h"
25650d1603SAlex Elder 
26650d1603SAlex Elder /**
27650d1603SAlex Elder  * DOC: The IPA Generic Software Interface
28650d1603SAlex Elder  *
29650d1603SAlex Elder  * The generic software interface (GSI) is an integral component of the IPA,
30650d1603SAlex Elder  * providing a well-defined communication layer between the AP subsystem
31650d1603SAlex Elder  * and the IPA core.  The modem uses the GSI layer as well.
32650d1603SAlex Elder  *
33650d1603SAlex Elder  *	--------	     ---------
34650d1603SAlex Elder  *	|      |	     |	     |
35650d1603SAlex Elder  *	|  AP  +<---.	.----+ Modem |
36650d1603SAlex Elder  *	|      +--. |	| .->+	     |
37650d1603SAlex Elder  *	|      |  | |	| |  |	     |
38650d1603SAlex Elder  *	--------  | |	| |  ---------
39650d1603SAlex Elder  *		  v |	v |
40650d1603SAlex Elder  *		--+-+---+-+--
41650d1603SAlex Elder  *		|    GSI    |
42650d1603SAlex Elder  *		|-----------|
43650d1603SAlex Elder  *		|	    |
44650d1603SAlex Elder  *		|    IPA    |
45650d1603SAlex Elder  *		|	    |
46650d1603SAlex Elder  *		-------------
47650d1603SAlex Elder  *
48650d1603SAlex Elder  * In the above diagram, the AP and Modem represent "execution environments"
49650d1603SAlex Elder  * (EEs), which are independent operating environments that use the IPA for
50650d1603SAlex Elder  * data transfer.
51650d1603SAlex Elder  *
52650d1603SAlex Elder  * Each EE uses a set of unidirectional GSI "channels," which allow transfer
53650d1603SAlex Elder  * of data to or from the IPA.  A channel is implemented as a ring buffer,
54650d1603SAlex Elder  * with a DRAM-resident array of "transfer elements" (TREs) available to
55650d1603SAlex Elder  * describe transfers to or from other EEs through the IPA.  A transfer
56650d1603SAlex Elder  * element can also contain an immediate command, requesting the IPA perform
57650d1603SAlex Elder  * actions other than data transfer.
58650d1603SAlex Elder  *
59650d1603SAlex Elder  * Each TRE refers to a block of data--also located DRAM.  After writing one
60650d1603SAlex Elder  * or more TREs to a channel, the writer (either the IPA or an EE) writes a
61650d1603SAlex Elder  * doorbell register to inform the receiving side how many elements have
62650d1603SAlex Elder  * been written.
63650d1603SAlex Elder  *
64650d1603SAlex Elder  * Each channel has a GSI "event ring" associated with it.  An event ring
65650d1603SAlex Elder  * is implemented very much like a channel ring, but is always directed from
66650d1603SAlex Elder  * the IPA to an EE.  The IPA notifies an EE (such as the AP) about channel
67650d1603SAlex Elder  * events by adding an entry to the event ring associated with the channel.
68650d1603SAlex Elder  * The GSI then writes its doorbell for the event ring, causing the target
69650d1603SAlex Elder  * EE to be interrupted.  Each entry in an event ring contains a pointer
70650d1603SAlex Elder  * to the channel TRE whose completion the event represents.
71650d1603SAlex Elder  *
72650d1603SAlex Elder  * Each TRE in a channel ring has a set of flags.  One flag indicates whether
73650d1603SAlex Elder  * the completion of the transfer operation generates an entry (and possibly
74650d1603SAlex Elder  * an interrupt) in the channel's event ring.  Other flags allow transfer
75650d1603SAlex Elder  * elements to be chained together, forming a single logical transaction.
76650d1603SAlex Elder  * TRE flags are used to control whether and when interrupts are generated
77650d1603SAlex Elder  * to signal completion of channel transfers.
78650d1603SAlex Elder  *
79650d1603SAlex Elder  * Elements in channel and event rings are completed (or consumed) strictly
80650d1603SAlex Elder  * in order.  Completion of one entry implies the completion of all preceding
81650d1603SAlex Elder  * entries.  A single completion interrupt can therefore communicate the
82650d1603SAlex Elder  * completion of many transfers.
83650d1603SAlex Elder  *
84650d1603SAlex Elder  * Note that all GSI registers are little-endian, which is the assumed
85650d1603SAlex Elder  * endianness of I/O space accesses.  The accessor functions perform byte
86650d1603SAlex Elder  * swapping if needed (i.e., for a big endian CPU).
87650d1603SAlex Elder  */
88650d1603SAlex Elder 
89650d1603SAlex Elder /* Delay period for interrupt moderation (in 32KHz IPA internal timer ticks) */
90650d1603SAlex Elder #define GSI_EVT_RING_INT_MODT		(32 * 1) /* 1ms under 32KHz clock */
91650d1603SAlex Elder 
92650d1603SAlex Elder #define GSI_CMD_TIMEOUT			5	/* seconds */
93650d1603SAlex Elder 
94650d1603SAlex Elder #define GSI_CHANNEL_STOP_RX_RETRIES	10
95650d1603SAlex Elder 
96650d1603SAlex Elder #define GSI_MHI_EVENT_ID_START		10	/* 1st reserved event id */
97650d1603SAlex Elder #define GSI_MHI_EVENT_ID_END		16	/* Last reserved event id */
98650d1603SAlex Elder 
99650d1603SAlex Elder #define GSI_ISR_MAX_ITER		50	/* Detect interrupt storms */
100650d1603SAlex Elder 
101650d1603SAlex Elder /* An entry in an event ring */
102650d1603SAlex Elder struct gsi_event {
103650d1603SAlex Elder 	__le64 xfer_ptr;
104650d1603SAlex Elder 	__le16 len;
105650d1603SAlex Elder 	u8 reserved1;
106650d1603SAlex Elder 	u8 code;
107650d1603SAlex Elder 	__le16 reserved2;
108650d1603SAlex Elder 	u8 type;
109650d1603SAlex Elder 	u8 chid;
110650d1603SAlex Elder };
111650d1603SAlex Elder 
112650d1603SAlex Elder /* Hardware values from the error log register error code field */
113650d1603SAlex Elder enum gsi_err_code {
114650d1603SAlex Elder 	GSI_INVALID_TRE_ERR			= 0x1,
115650d1603SAlex Elder 	GSI_OUT_OF_BUFFERS_ERR			= 0x2,
116650d1603SAlex Elder 	GSI_OUT_OF_RESOURCES_ERR		= 0x3,
117650d1603SAlex Elder 	GSI_UNSUPPORTED_INTER_EE_OP_ERR		= 0x4,
118650d1603SAlex Elder 	GSI_EVT_RING_EMPTY_ERR			= 0x5,
119650d1603SAlex Elder 	GSI_NON_ALLOCATED_EVT_ACCESS_ERR	= 0x6,
120650d1603SAlex Elder 	GSI_HWO_1_ERR				= 0x8,
121650d1603SAlex Elder };
122650d1603SAlex Elder 
123650d1603SAlex Elder /* Hardware values from the error log register error type field */
124650d1603SAlex Elder enum gsi_err_type {
125650d1603SAlex Elder 	GSI_ERR_TYPE_GLOB	= 0x1,
126650d1603SAlex Elder 	GSI_ERR_TYPE_CHAN	= 0x2,
127650d1603SAlex Elder 	GSI_ERR_TYPE_EVT	= 0x3,
128650d1603SAlex Elder };
129650d1603SAlex Elder 
130650d1603SAlex Elder /* Hardware values used when programming an event ring */
131650d1603SAlex Elder enum gsi_evt_chtype {
132650d1603SAlex Elder 	GSI_EVT_CHTYPE_MHI_EV	= 0x0,
133650d1603SAlex Elder 	GSI_EVT_CHTYPE_XHCI_EV	= 0x1,
134650d1603SAlex Elder 	GSI_EVT_CHTYPE_GPI_EV	= 0x2,
135650d1603SAlex Elder 	GSI_EVT_CHTYPE_XDCI_EV	= 0x3,
136650d1603SAlex Elder };
137650d1603SAlex Elder 
138650d1603SAlex Elder /* Hardware values used when programming a channel */
139650d1603SAlex Elder enum gsi_channel_protocol {
140650d1603SAlex Elder 	GSI_CHANNEL_PROTOCOL_MHI	= 0x0,
141650d1603SAlex Elder 	GSI_CHANNEL_PROTOCOL_XHCI	= 0x1,
142650d1603SAlex Elder 	GSI_CHANNEL_PROTOCOL_GPI	= 0x2,
143650d1603SAlex Elder 	GSI_CHANNEL_PROTOCOL_XDCI	= 0x3,
144650d1603SAlex Elder };
145650d1603SAlex Elder 
146650d1603SAlex Elder /* Hardware values representing an event ring immediate command opcode */
147650d1603SAlex Elder enum gsi_evt_cmd_opcode {
148650d1603SAlex Elder 	GSI_EVT_ALLOCATE	= 0x0,
149650d1603SAlex Elder 	GSI_EVT_RESET		= 0x9,
150650d1603SAlex Elder 	GSI_EVT_DE_ALLOC	= 0xa,
151650d1603SAlex Elder };
152650d1603SAlex Elder 
153650d1603SAlex Elder /* Hardware values representing a generic immediate command opcode */
154650d1603SAlex Elder enum gsi_generic_cmd_opcode {
155650d1603SAlex Elder 	GSI_GENERIC_HALT_CHANNEL	= 0x1,
156650d1603SAlex Elder 	GSI_GENERIC_ALLOCATE_CHANNEL	= 0x2,
157650d1603SAlex Elder };
158650d1603SAlex Elder 
159650d1603SAlex Elder /* Hardware values representing a channel immediate command opcode */
160650d1603SAlex Elder enum gsi_ch_cmd_opcode {
161650d1603SAlex Elder 	GSI_CH_ALLOCATE	= 0x0,
162650d1603SAlex Elder 	GSI_CH_START	= 0x1,
163650d1603SAlex Elder 	GSI_CH_STOP	= 0x2,
164650d1603SAlex Elder 	GSI_CH_RESET	= 0x9,
165650d1603SAlex Elder 	GSI_CH_DE_ALLOC	= 0xa,
166650d1603SAlex Elder };
167650d1603SAlex Elder 
168650d1603SAlex Elder /** gsi_channel_scratch_gpi - GPI protocol scratch register
169650d1603SAlex Elder  * @max_outstanding_tre:
170650d1603SAlex Elder  *	Defines the maximum number of TREs allowed in a single transaction
171650d1603SAlex Elder  *	on a channel (in bytes).  This determines the amount of prefetch
172650d1603SAlex Elder  *	performed by the hardware.  We configure this to equal the size of
173650d1603SAlex Elder  *	the TLV FIFO for the channel.
174650d1603SAlex Elder  * @outstanding_threshold:
175650d1603SAlex Elder  *	Defines the threshold (in bytes) determining when the sequencer
176650d1603SAlex Elder  *	should update the channel doorbell.  We configure this to equal
177650d1603SAlex Elder  *	the size of two TREs.
178650d1603SAlex Elder  */
179650d1603SAlex Elder struct gsi_channel_scratch_gpi {
180650d1603SAlex Elder 	u64 reserved1;
181650d1603SAlex Elder 	u16 reserved2;
182650d1603SAlex Elder 	u16 max_outstanding_tre;
183650d1603SAlex Elder 	u16 reserved3;
184650d1603SAlex Elder 	u16 outstanding_threshold;
185650d1603SAlex Elder };
186650d1603SAlex Elder 
187650d1603SAlex Elder /** gsi_channel_scratch - channel scratch configuration area
188650d1603SAlex Elder  *
189650d1603SAlex Elder  * The exact interpretation of this register is protocol-specific.
190650d1603SAlex Elder  * We only use GPI channels; see struct gsi_channel_scratch_gpi, above.
191650d1603SAlex Elder  */
192650d1603SAlex Elder union gsi_channel_scratch {
193650d1603SAlex Elder 	struct gsi_channel_scratch_gpi gpi;
194650d1603SAlex Elder 	struct {
195650d1603SAlex Elder 		u32 word1;
196650d1603SAlex Elder 		u32 word2;
197650d1603SAlex Elder 		u32 word3;
198650d1603SAlex Elder 		u32 word4;
199650d1603SAlex Elder 	} data;
200650d1603SAlex Elder };
201650d1603SAlex Elder 
202650d1603SAlex Elder /* Check things that can be validated at build time. */
203650d1603SAlex Elder static void gsi_validate_build(void)
204650d1603SAlex Elder {
205650d1603SAlex Elder 	/* This is used as a divisor */
206650d1603SAlex Elder 	BUILD_BUG_ON(!GSI_RING_ELEMENT_SIZE);
207650d1603SAlex Elder 
208650d1603SAlex Elder 	/* Code assumes the size of channel and event ring element are
209650d1603SAlex Elder 	 * the same (and fixed).  Make sure the size of an event ring
210650d1603SAlex Elder 	 * element is what's expected.
211650d1603SAlex Elder 	 */
212650d1603SAlex Elder 	BUILD_BUG_ON(sizeof(struct gsi_event) != GSI_RING_ELEMENT_SIZE);
213650d1603SAlex Elder 
214650d1603SAlex Elder 	/* Hardware requires a 2^n ring size.  We ensure the number of
215650d1603SAlex Elder 	 * elements in an event ring is a power of 2 elsewhere; this
216650d1603SAlex Elder 	 * ensure the elements themselves meet the requirement.
217650d1603SAlex Elder 	 */
218650d1603SAlex Elder 	BUILD_BUG_ON(!is_power_of_2(GSI_RING_ELEMENT_SIZE));
219650d1603SAlex Elder 
220650d1603SAlex Elder 	/* The channel element size must fit in this field */
221650d1603SAlex Elder 	BUILD_BUG_ON(GSI_RING_ELEMENT_SIZE > field_max(ELEMENT_SIZE_FMASK));
222650d1603SAlex Elder 
223650d1603SAlex Elder 	/* The event ring element size must fit in this field */
224650d1603SAlex Elder 	BUILD_BUG_ON(GSI_RING_ELEMENT_SIZE > field_max(EV_ELEMENT_SIZE_FMASK));
225650d1603SAlex Elder }
226650d1603SAlex Elder 
227650d1603SAlex Elder /* Return the channel id associated with a given channel */
228650d1603SAlex Elder static u32 gsi_channel_id(struct gsi_channel *channel)
229650d1603SAlex Elder {
230650d1603SAlex Elder 	return channel - &channel->gsi->channel[0];
231650d1603SAlex Elder }
232650d1603SAlex Elder 
233650d1603SAlex Elder static void gsi_irq_ieob_enable(struct gsi *gsi, u32 evt_ring_id)
234650d1603SAlex Elder {
235650d1603SAlex Elder 	u32 val;
236650d1603SAlex Elder 
237650d1603SAlex Elder 	gsi->event_enable_bitmap |= BIT(evt_ring_id);
238650d1603SAlex Elder 	val = gsi->event_enable_bitmap;
239650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_MSK_OFFSET);
240650d1603SAlex Elder }
241650d1603SAlex Elder 
242650d1603SAlex Elder static void gsi_irq_ieob_disable(struct gsi *gsi, u32 evt_ring_id)
243650d1603SAlex Elder {
244650d1603SAlex Elder 	u32 val;
245650d1603SAlex Elder 
246650d1603SAlex Elder 	gsi->event_enable_bitmap &= ~BIT(evt_ring_id);
247650d1603SAlex Elder 	val = gsi->event_enable_bitmap;
248650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_MSK_OFFSET);
249650d1603SAlex Elder }
250650d1603SAlex Elder 
251650d1603SAlex Elder /* Enable all GSI_interrupt types */
252650d1603SAlex Elder static void gsi_irq_enable(struct gsi *gsi)
253650d1603SAlex Elder {
254650d1603SAlex Elder 	u32 val;
255650d1603SAlex Elder 
256650d1603SAlex Elder 	/* We don't use inter-EE channel or event interrupts */
257650d1603SAlex Elder 	val = GSI_CNTXT_TYPE_IRQ_MSK_ALL;
258e6580d0eSAlex Elder 	val &= ~INTER_EE_CH_CTRL_FMASK;
259e6580d0eSAlex Elder 	val &= ~INTER_EE_EV_CTRL_FMASK;
260650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CNTXT_TYPE_IRQ_MSK_OFFSET);
261650d1603SAlex Elder 
262650d1603SAlex Elder 	val = GENMASK(gsi->channel_count - 1, 0);
263650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CNTXT_SRC_CH_IRQ_MSK_OFFSET);
264650d1603SAlex Elder 
265650d1603SAlex Elder 	val = GENMASK(gsi->evt_ring_count - 1, 0);
266650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_MSK_OFFSET);
267650d1603SAlex Elder 
268650d1603SAlex Elder 	/* Each IEOB interrupt is enabled (later) as needed by channels */
269650d1603SAlex Elder 	iowrite32(0, gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_MSK_OFFSET);
270650d1603SAlex Elder 
271650d1603SAlex Elder 	val = GSI_CNTXT_GLOB_IRQ_ALL;
272650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CNTXT_GLOB_IRQ_EN_OFFSET);
273650d1603SAlex Elder 
274650d1603SAlex Elder 	/* Never enable GSI_BREAK_POINT */
275fb980ef7SAlex Elder 	val = GSI_CNTXT_GSI_IRQ_ALL & ~BREAK_POINT_FMASK;
276650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CNTXT_GSI_IRQ_EN_OFFSET);
277650d1603SAlex Elder }
278650d1603SAlex Elder 
279650d1603SAlex Elder /* Disable all GSI_interrupt types */
280650d1603SAlex Elder static void gsi_irq_disable(struct gsi *gsi)
281650d1603SAlex Elder {
282650d1603SAlex Elder 	iowrite32(0, gsi->virt + GSI_CNTXT_GSI_IRQ_EN_OFFSET);
283650d1603SAlex Elder 	iowrite32(0, gsi->virt + GSI_CNTXT_GLOB_IRQ_EN_OFFSET);
284650d1603SAlex Elder 	iowrite32(0, gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_MSK_OFFSET);
285650d1603SAlex Elder 	iowrite32(0, gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_MSK_OFFSET);
286650d1603SAlex Elder 	iowrite32(0, gsi->virt + GSI_CNTXT_SRC_CH_IRQ_MSK_OFFSET);
287650d1603SAlex Elder 	iowrite32(0, gsi->virt + GSI_CNTXT_TYPE_IRQ_MSK_OFFSET);
288650d1603SAlex Elder }
289650d1603SAlex Elder 
290650d1603SAlex Elder /* Return the virtual address associated with a ring index */
291650d1603SAlex Elder void *gsi_ring_virt(struct gsi_ring *ring, u32 index)
292650d1603SAlex Elder {
293650d1603SAlex Elder 	/* Note: index *must* be used modulo the ring count here */
294650d1603SAlex Elder 	return ring->virt + (index % ring->count) * GSI_RING_ELEMENT_SIZE;
295650d1603SAlex Elder }
296650d1603SAlex Elder 
297650d1603SAlex Elder /* Return the 32-bit DMA address associated with a ring index */
298650d1603SAlex Elder static u32 gsi_ring_addr(struct gsi_ring *ring, u32 index)
299650d1603SAlex Elder {
300650d1603SAlex Elder 	return (ring->addr & GENMASK(31, 0)) + index * GSI_RING_ELEMENT_SIZE;
301650d1603SAlex Elder }
302650d1603SAlex Elder 
303650d1603SAlex Elder /* Return the ring index of a 32-bit ring offset */
304650d1603SAlex Elder static u32 gsi_ring_index(struct gsi_ring *ring, u32 offset)
305650d1603SAlex Elder {
306650d1603SAlex Elder 	return (offset - gsi_ring_addr(ring, 0)) / GSI_RING_ELEMENT_SIZE;
307650d1603SAlex Elder }
308650d1603SAlex Elder 
309650d1603SAlex Elder /* Issue a GSI command by writing a value to a register, then wait for
310650d1603SAlex Elder  * completion to be signaled.  Returns true if the command completes
311650d1603SAlex Elder  * or false if it times out.
312650d1603SAlex Elder  */
313650d1603SAlex Elder static bool
314650d1603SAlex Elder gsi_command(struct gsi *gsi, u32 reg, u32 val, struct completion *completion)
315650d1603SAlex Elder {
316650d1603SAlex Elder 	reinit_completion(completion);
317650d1603SAlex Elder 
318650d1603SAlex Elder 	iowrite32(val, gsi->virt + reg);
319650d1603SAlex Elder 
320650d1603SAlex Elder 	return !!wait_for_completion_timeout(completion, GSI_CMD_TIMEOUT * HZ);
321650d1603SAlex Elder }
322650d1603SAlex Elder 
323650d1603SAlex Elder /* Return the hardware's notion of the current state of an event ring */
324650d1603SAlex Elder static enum gsi_evt_ring_state
325650d1603SAlex Elder gsi_evt_ring_state(struct gsi *gsi, u32 evt_ring_id)
326650d1603SAlex Elder {
327650d1603SAlex Elder 	u32 val;
328650d1603SAlex Elder 
329650d1603SAlex Elder 	val = ioread32(gsi->virt + GSI_EV_CH_E_CNTXT_0_OFFSET(evt_ring_id));
330650d1603SAlex Elder 
331650d1603SAlex Elder 	return u32_get_bits(val, EV_CHSTATE_FMASK);
332650d1603SAlex Elder }
333650d1603SAlex Elder 
334650d1603SAlex Elder /* Issue an event ring command and wait for it to complete */
335650d1603SAlex Elder static int evt_ring_command(struct gsi *gsi, u32 evt_ring_id,
336650d1603SAlex Elder 			    enum gsi_evt_cmd_opcode opcode)
337650d1603SAlex Elder {
338650d1603SAlex Elder 	struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
339650d1603SAlex Elder 	struct completion *completion = &evt_ring->completion;
3408463488aSAlex Elder 	struct device *dev = gsi->dev;
341650d1603SAlex Elder 	u32 val;
342650d1603SAlex Elder 
343650d1603SAlex Elder 	val = u32_encode_bits(evt_ring_id, EV_CHID_FMASK);
344650d1603SAlex Elder 	val |= u32_encode_bits(opcode, EV_OPCODE_FMASK);
345650d1603SAlex Elder 
346650d1603SAlex Elder 	if (gsi_command(gsi, GSI_EV_CH_CMD_OFFSET, val, completion))
347650d1603SAlex Elder 		return 0;	/* Success! */
348650d1603SAlex Elder 
3498463488aSAlex Elder 	dev_err(dev, "GSI command %u for event ring %u timed out, state %u\n",
3508463488aSAlex Elder 		opcode, evt_ring_id, evt_ring->state);
351650d1603SAlex Elder 
352650d1603SAlex Elder 	return -ETIMEDOUT;
353650d1603SAlex Elder }
354650d1603SAlex Elder 
355650d1603SAlex Elder /* Allocate an event ring in NOT_ALLOCATED state */
356650d1603SAlex Elder static int gsi_evt_ring_alloc_command(struct gsi *gsi, u32 evt_ring_id)
357650d1603SAlex Elder {
358650d1603SAlex Elder 	struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
359650d1603SAlex Elder 	int ret;
360650d1603SAlex Elder 
361650d1603SAlex Elder 	/* Get initial event ring state */
362650d1603SAlex Elder 	evt_ring->state = gsi_evt_ring_state(gsi, evt_ring_id);
363a442b3c7SAlex Elder 	if (evt_ring->state != GSI_EVT_RING_STATE_NOT_ALLOCATED) {
364a442b3c7SAlex Elder 		dev_err(gsi->dev, "bad event ring state %u before alloc\n",
365a442b3c7SAlex Elder 			evt_ring->state);
366650d1603SAlex Elder 		return -EINVAL;
367a442b3c7SAlex Elder 	}
368650d1603SAlex Elder 
369650d1603SAlex Elder 	ret = evt_ring_command(gsi, evt_ring_id, GSI_EVT_ALLOCATE);
370650d1603SAlex Elder 	if (!ret && evt_ring->state != GSI_EVT_RING_STATE_ALLOCATED) {
371a442b3c7SAlex Elder 		dev_err(gsi->dev, "bad event ring state %u after alloc\n",
372650d1603SAlex Elder 			evt_ring->state);
373650d1603SAlex Elder 		ret = -EIO;
374650d1603SAlex Elder 	}
375650d1603SAlex Elder 
376650d1603SAlex Elder 	return ret;
377650d1603SAlex Elder }
378650d1603SAlex Elder 
379650d1603SAlex Elder /* Reset a GSI event ring in ALLOCATED or ERROR state. */
380650d1603SAlex Elder static void gsi_evt_ring_reset_command(struct gsi *gsi, u32 evt_ring_id)
381650d1603SAlex Elder {
382650d1603SAlex Elder 	struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
383650d1603SAlex Elder 	enum gsi_evt_ring_state state = evt_ring->state;
384650d1603SAlex Elder 	int ret;
385650d1603SAlex Elder 
386650d1603SAlex Elder 	if (state != GSI_EVT_RING_STATE_ALLOCATED &&
387650d1603SAlex Elder 	    state != GSI_EVT_RING_STATE_ERROR) {
388a442b3c7SAlex Elder 		dev_err(gsi->dev, "bad event ring state %u before reset\n",
389650d1603SAlex Elder 			evt_ring->state);
390650d1603SAlex Elder 		return;
391650d1603SAlex Elder 	}
392650d1603SAlex Elder 
393650d1603SAlex Elder 	ret = evt_ring_command(gsi, evt_ring_id, GSI_EVT_RESET);
394650d1603SAlex Elder 	if (!ret && evt_ring->state != GSI_EVT_RING_STATE_ALLOCATED)
395a442b3c7SAlex Elder 		dev_err(gsi->dev, "bad event ring state %u after reset\n",
396650d1603SAlex Elder 			evt_ring->state);
397650d1603SAlex Elder }
398650d1603SAlex Elder 
399650d1603SAlex Elder /* Issue a hardware de-allocation request for an allocated event ring */
400650d1603SAlex Elder static void gsi_evt_ring_de_alloc_command(struct gsi *gsi, u32 evt_ring_id)
401650d1603SAlex Elder {
402650d1603SAlex Elder 	struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
403650d1603SAlex Elder 	int ret;
404650d1603SAlex Elder 
405650d1603SAlex Elder 	if (evt_ring->state != GSI_EVT_RING_STATE_ALLOCATED) {
406a442b3c7SAlex Elder 		dev_err(gsi->dev, "bad event ring state %u before dealloc\n",
407650d1603SAlex Elder 			evt_ring->state);
408650d1603SAlex Elder 		return;
409650d1603SAlex Elder 	}
410650d1603SAlex Elder 
411650d1603SAlex Elder 	ret = evt_ring_command(gsi, evt_ring_id, GSI_EVT_DE_ALLOC);
412650d1603SAlex Elder 	if (!ret && evt_ring->state != GSI_EVT_RING_STATE_NOT_ALLOCATED)
413a442b3c7SAlex Elder 		dev_err(gsi->dev, "bad event ring state %u after dealloc\n",
414650d1603SAlex Elder 			evt_ring->state);
415650d1603SAlex Elder }
416650d1603SAlex Elder 
417a2003b30SAlex Elder /* Fetch the current state of a channel from hardware */
418aba7924fSAlex Elder static enum gsi_channel_state gsi_channel_state(struct gsi_channel *channel)
419650d1603SAlex Elder {
420aba7924fSAlex Elder 	u32 channel_id = gsi_channel_id(channel);
421aba7924fSAlex Elder 	void *virt = channel->gsi->virt;
422650d1603SAlex Elder 	u32 val;
423650d1603SAlex Elder 
424aba7924fSAlex Elder 	val = ioread32(virt + GSI_CH_C_CNTXT_0_OFFSET(channel_id));
425650d1603SAlex Elder 
426650d1603SAlex Elder 	return u32_get_bits(val, CHSTATE_FMASK);
427650d1603SAlex Elder }
428650d1603SAlex Elder 
429650d1603SAlex Elder /* Issue a channel command and wait for it to complete */
430650d1603SAlex Elder static int
431650d1603SAlex Elder gsi_channel_command(struct gsi_channel *channel, enum gsi_ch_cmd_opcode opcode)
432650d1603SAlex Elder {
433650d1603SAlex Elder 	struct completion *completion = &channel->completion;
434650d1603SAlex Elder 	u32 channel_id = gsi_channel_id(channel);
435a2003b30SAlex Elder 	struct gsi *gsi = channel->gsi;
4368463488aSAlex Elder 	struct device *dev = gsi->dev;
437650d1603SAlex Elder 	u32 val;
438650d1603SAlex Elder 
439650d1603SAlex Elder 	val = u32_encode_bits(channel_id, CH_CHID_FMASK);
440650d1603SAlex Elder 	val |= u32_encode_bits(opcode, CH_OPCODE_FMASK);
441650d1603SAlex Elder 
442a2003b30SAlex Elder 	if (gsi_command(gsi, GSI_CH_CMD_OFFSET, val, completion))
443650d1603SAlex Elder 		return 0;	/* Success! */
444650d1603SAlex Elder 
4458463488aSAlex Elder 	dev_err(dev, "GSI command %u for channel %u timed out, state %u\n",
446a2003b30SAlex Elder 		opcode, channel_id, gsi_channel_state(channel));
447650d1603SAlex Elder 
448650d1603SAlex Elder 	return -ETIMEDOUT;
449650d1603SAlex Elder }
450650d1603SAlex Elder 
451650d1603SAlex Elder /* Allocate GSI channel in NOT_ALLOCATED state */
452650d1603SAlex Elder static int gsi_channel_alloc_command(struct gsi *gsi, u32 channel_id)
453650d1603SAlex Elder {
454650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
455a442b3c7SAlex Elder 	struct device *dev = gsi->dev;
456a2003b30SAlex Elder 	enum gsi_channel_state state;
457650d1603SAlex Elder 	int ret;
458650d1603SAlex Elder 
459650d1603SAlex Elder 	/* Get initial channel state */
460a2003b30SAlex Elder 	state = gsi_channel_state(channel);
461a442b3c7SAlex Elder 	if (state != GSI_CHANNEL_STATE_NOT_ALLOCATED) {
462a442b3c7SAlex Elder 		dev_err(dev, "bad channel state %u before alloc\n", state);
463650d1603SAlex Elder 		return -EINVAL;
464a442b3c7SAlex Elder 	}
465650d1603SAlex Elder 
466650d1603SAlex Elder 	ret = gsi_channel_command(channel, GSI_CH_ALLOCATE);
467a2003b30SAlex Elder 
468a2003b30SAlex Elder 	/* Channel state will normally have been updated */
469a2003b30SAlex Elder 	state = gsi_channel_state(channel);
470a2003b30SAlex Elder 	if (!ret && state != GSI_CHANNEL_STATE_ALLOCATED) {
471a442b3c7SAlex Elder 		dev_err(dev, "bad channel state %u after alloc\n", state);
472650d1603SAlex Elder 		ret = -EIO;
473650d1603SAlex Elder 	}
474650d1603SAlex Elder 
475650d1603SAlex Elder 	return ret;
476650d1603SAlex Elder }
477650d1603SAlex Elder 
478650d1603SAlex Elder /* Start an ALLOCATED channel */
479650d1603SAlex Elder static int gsi_channel_start_command(struct gsi_channel *channel)
480650d1603SAlex Elder {
481a442b3c7SAlex Elder 	struct device *dev = channel->gsi->dev;
482a2003b30SAlex Elder 	enum gsi_channel_state state;
483650d1603SAlex Elder 	int ret;
484650d1603SAlex Elder 
485a2003b30SAlex Elder 	state = gsi_channel_state(channel);
486650d1603SAlex Elder 	if (state != GSI_CHANNEL_STATE_ALLOCATED &&
487a442b3c7SAlex Elder 	    state != GSI_CHANNEL_STATE_STOPPED) {
488a442b3c7SAlex Elder 		dev_err(dev, "bad channel state %u before start\n", state);
489650d1603SAlex Elder 		return -EINVAL;
490a442b3c7SAlex Elder 	}
491650d1603SAlex Elder 
492650d1603SAlex Elder 	ret = gsi_channel_command(channel, GSI_CH_START);
493a2003b30SAlex Elder 
494a2003b30SAlex Elder 	/* Channel state will normally have been updated */
495a2003b30SAlex Elder 	state = gsi_channel_state(channel);
496a2003b30SAlex Elder 	if (!ret && state != GSI_CHANNEL_STATE_STARTED) {
497a442b3c7SAlex Elder 		dev_err(dev, "bad channel state %u after start\n", state);
498650d1603SAlex Elder 		ret = -EIO;
499650d1603SAlex Elder 	}
500650d1603SAlex Elder 
501650d1603SAlex Elder 	return ret;
502650d1603SAlex Elder }
503650d1603SAlex Elder 
504650d1603SAlex Elder /* Stop a GSI channel in STARTED state */
505650d1603SAlex Elder static int gsi_channel_stop_command(struct gsi_channel *channel)
506650d1603SAlex Elder {
507a442b3c7SAlex Elder 	struct device *dev = channel->gsi->dev;
508a2003b30SAlex Elder 	enum gsi_channel_state state;
509650d1603SAlex Elder 	int ret;
510650d1603SAlex Elder 
511a2003b30SAlex Elder 	state = gsi_channel_state(channel);
5125468cbcdSAlex Elder 
5135468cbcdSAlex Elder 	/* Channel could have entered STOPPED state since last call
5145468cbcdSAlex Elder 	 * if it timed out.  If so, we're done.
5155468cbcdSAlex Elder 	 */
5165468cbcdSAlex Elder 	if (state == GSI_CHANNEL_STATE_STOPPED)
5175468cbcdSAlex Elder 		return 0;
5185468cbcdSAlex Elder 
519650d1603SAlex Elder 	if (state != GSI_CHANNEL_STATE_STARTED &&
520a442b3c7SAlex Elder 	    state != GSI_CHANNEL_STATE_STOP_IN_PROC) {
521a442b3c7SAlex Elder 		dev_err(dev, "bad channel state %u before stop\n", state);
522650d1603SAlex Elder 		return -EINVAL;
523a442b3c7SAlex Elder 	}
524650d1603SAlex Elder 
525650d1603SAlex Elder 	ret = gsi_channel_command(channel, GSI_CH_STOP);
526a2003b30SAlex Elder 
527a2003b30SAlex Elder 	/* Channel state will normally have been updated */
528a2003b30SAlex Elder 	state = gsi_channel_state(channel);
529a2003b30SAlex Elder 	if (ret || state == GSI_CHANNEL_STATE_STOPPED)
530650d1603SAlex Elder 		return ret;
531650d1603SAlex Elder 
532650d1603SAlex Elder 	/* We may have to try again if stop is in progress */
533a2003b30SAlex Elder 	if (state == GSI_CHANNEL_STATE_STOP_IN_PROC)
534650d1603SAlex Elder 		return -EAGAIN;
535650d1603SAlex Elder 
536a442b3c7SAlex Elder 	dev_err(dev, "bad channel state %u after stop\n", state);
537650d1603SAlex Elder 
538650d1603SAlex Elder 	return -EIO;
539650d1603SAlex Elder }
540650d1603SAlex Elder 
541650d1603SAlex Elder /* Reset a GSI channel in ALLOCATED or ERROR state. */
542650d1603SAlex Elder static void gsi_channel_reset_command(struct gsi_channel *channel)
543650d1603SAlex Elder {
544a442b3c7SAlex Elder 	struct device *dev = channel->gsi->dev;
545a2003b30SAlex Elder 	enum gsi_channel_state state;
546650d1603SAlex Elder 	int ret;
547650d1603SAlex Elder 
548650d1603SAlex Elder 	msleep(1);	/* A short delay is required before a RESET command */
549650d1603SAlex Elder 
550a2003b30SAlex Elder 	state = gsi_channel_state(channel);
551a2003b30SAlex Elder 	if (state != GSI_CHANNEL_STATE_STOPPED &&
552a2003b30SAlex Elder 	    state != GSI_CHANNEL_STATE_ERROR) {
553a442b3c7SAlex Elder 		dev_err(dev, "bad channel state %u before reset\n", state);
554650d1603SAlex Elder 		return;
555650d1603SAlex Elder 	}
556650d1603SAlex Elder 
557650d1603SAlex Elder 	ret = gsi_channel_command(channel, GSI_CH_RESET);
558a2003b30SAlex Elder 
559a2003b30SAlex Elder 	/* Channel state will normally have been updated */
560a2003b30SAlex Elder 	state = gsi_channel_state(channel);
561a2003b30SAlex Elder 	if (!ret && state != GSI_CHANNEL_STATE_ALLOCATED)
562a442b3c7SAlex Elder 		dev_err(dev, "bad channel state %u after reset\n", state);
563650d1603SAlex Elder }
564650d1603SAlex Elder 
565650d1603SAlex Elder /* Deallocate an ALLOCATED GSI channel */
566650d1603SAlex Elder static void gsi_channel_de_alloc_command(struct gsi *gsi, u32 channel_id)
567650d1603SAlex Elder {
568650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
569a442b3c7SAlex Elder 	struct device *dev = gsi->dev;
570a2003b30SAlex Elder 	enum gsi_channel_state state;
571650d1603SAlex Elder 	int ret;
572650d1603SAlex Elder 
573a2003b30SAlex Elder 	state = gsi_channel_state(channel);
574a2003b30SAlex Elder 	if (state != GSI_CHANNEL_STATE_ALLOCATED) {
575a442b3c7SAlex Elder 		dev_err(dev, "bad channel state %u before dealloc\n", state);
576650d1603SAlex Elder 		return;
577650d1603SAlex Elder 	}
578650d1603SAlex Elder 
579650d1603SAlex Elder 	ret = gsi_channel_command(channel, GSI_CH_DE_ALLOC);
580a2003b30SAlex Elder 
581a2003b30SAlex Elder 	/* Channel state will normally have been updated */
582a2003b30SAlex Elder 	state = gsi_channel_state(channel);
583a2003b30SAlex Elder 	if (!ret && state != GSI_CHANNEL_STATE_NOT_ALLOCATED)
584a442b3c7SAlex Elder 		dev_err(dev, "bad channel state %u after dealloc\n", state);
585650d1603SAlex Elder }
586650d1603SAlex Elder 
587650d1603SAlex Elder /* Ring an event ring doorbell, reporting the last entry processed by the AP.
588650d1603SAlex Elder  * The index argument (modulo the ring count) is the first unfilled entry, so
589650d1603SAlex Elder  * we supply one less than that with the doorbell.  Update the event ring
590650d1603SAlex Elder  * index field with the value provided.
591650d1603SAlex Elder  */
592650d1603SAlex Elder static void gsi_evt_ring_doorbell(struct gsi *gsi, u32 evt_ring_id, u32 index)
593650d1603SAlex Elder {
594650d1603SAlex Elder 	struct gsi_ring *ring = &gsi->evt_ring[evt_ring_id].ring;
595650d1603SAlex Elder 	u32 val;
596650d1603SAlex Elder 
597650d1603SAlex Elder 	ring->index = index;	/* Next unused entry */
598650d1603SAlex Elder 
599650d1603SAlex Elder 	/* Note: index *must* be used modulo the ring count here */
600650d1603SAlex Elder 	val = gsi_ring_addr(ring, (index - 1) % ring->count);
601650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_EV_CH_E_DOORBELL_0_OFFSET(evt_ring_id));
602650d1603SAlex Elder }
603650d1603SAlex Elder 
604650d1603SAlex Elder /* Program an event ring for use */
605650d1603SAlex Elder static void gsi_evt_ring_program(struct gsi *gsi, u32 evt_ring_id)
606650d1603SAlex Elder {
607650d1603SAlex Elder 	struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
608650d1603SAlex Elder 	size_t size = evt_ring->ring.count * GSI_RING_ELEMENT_SIZE;
609650d1603SAlex Elder 	u32 val;
610650d1603SAlex Elder 
611650d1603SAlex Elder 	val = u32_encode_bits(GSI_EVT_CHTYPE_GPI_EV, EV_CHTYPE_FMASK);
612650d1603SAlex Elder 	val |= EV_INTYPE_FMASK;
613650d1603SAlex Elder 	val |= u32_encode_bits(GSI_RING_ELEMENT_SIZE, EV_ELEMENT_SIZE_FMASK);
614650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_0_OFFSET(evt_ring_id));
615650d1603SAlex Elder 
616650d1603SAlex Elder 	val = u32_encode_bits(size, EV_R_LENGTH_FMASK);
617650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_1_OFFSET(evt_ring_id));
618650d1603SAlex Elder 
619650d1603SAlex Elder 	/* The context 2 and 3 registers store the low-order and
620650d1603SAlex Elder 	 * high-order 32 bits of the address of the event ring,
621650d1603SAlex Elder 	 * respectively.
622650d1603SAlex Elder 	 */
623650d1603SAlex Elder 	val = evt_ring->ring.addr & GENMASK(31, 0);
624650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_2_OFFSET(evt_ring_id));
625650d1603SAlex Elder 
626650d1603SAlex Elder 	val = evt_ring->ring.addr >> 32;
627650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_3_OFFSET(evt_ring_id));
628650d1603SAlex Elder 
629650d1603SAlex Elder 	/* Enable interrupt moderation by setting the moderation delay */
630650d1603SAlex Elder 	val = u32_encode_bits(GSI_EVT_RING_INT_MODT, MODT_FMASK);
631650d1603SAlex Elder 	val |= u32_encode_bits(1, MODC_FMASK);	/* comes from channel */
632650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_8_OFFSET(evt_ring_id));
633650d1603SAlex Elder 
634650d1603SAlex Elder 	/* No MSI write data, and MSI address high and low address is 0 */
635650d1603SAlex Elder 	iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_9_OFFSET(evt_ring_id));
636650d1603SAlex Elder 	iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_10_OFFSET(evt_ring_id));
637650d1603SAlex Elder 	iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_11_OFFSET(evt_ring_id));
638650d1603SAlex Elder 
639650d1603SAlex Elder 	/* We don't need to get event read pointer updates */
640650d1603SAlex Elder 	iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_12_OFFSET(evt_ring_id));
641650d1603SAlex Elder 	iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_13_OFFSET(evt_ring_id));
642650d1603SAlex Elder 
643650d1603SAlex Elder 	/* Finally, tell the hardware we've completed event 0 (arbitrary) */
644650d1603SAlex Elder 	gsi_evt_ring_doorbell(gsi, evt_ring_id, 0);
645650d1603SAlex Elder }
646650d1603SAlex Elder 
647650d1603SAlex Elder /* Return the last (most recent) transaction completed on a channel. */
648650d1603SAlex Elder static struct gsi_trans *gsi_channel_trans_last(struct gsi_channel *channel)
649650d1603SAlex Elder {
650650d1603SAlex Elder 	struct gsi_trans_info *trans_info = &channel->trans_info;
651650d1603SAlex Elder 	struct gsi_trans *trans;
652650d1603SAlex Elder 
653650d1603SAlex Elder 	spin_lock_bh(&trans_info->spinlock);
654650d1603SAlex Elder 
655650d1603SAlex Elder 	if (!list_empty(&trans_info->complete))
656650d1603SAlex Elder 		trans = list_last_entry(&trans_info->complete,
657650d1603SAlex Elder 					struct gsi_trans, links);
658650d1603SAlex Elder 	else if (!list_empty(&trans_info->polled))
659650d1603SAlex Elder 		trans = list_last_entry(&trans_info->polled,
660650d1603SAlex Elder 					struct gsi_trans, links);
661650d1603SAlex Elder 	else
662650d1603SAlex Elder 		trans = NULL;
663650d1603SAlex Elder 
664650d1603SAlex Elder 	/* Caller will wait for this, so take a reference */
665650d1603SAlex Elder 	if (trans)
666650d1603SAlex Elder 		refcount_inc(&trans->refcount);
667650d1603SAlex Elder 
668650d1603SAlex Elder 	spin_unlock_bh(&trans_info->spinlock);
669650d1603SAlex Elder 
670650d1603SAlex Elder 	return trans;
671650d1603SAlex Elder }
672650d1603SAlex Elder 
673650d1603SAlex Elder /* Wait for transaction activity on a channel to complete */
674650d1603SAlex Elder static void gsi_channel_trans_quiesce(struct gsi_channel *channel)
675650d1603SAlex Elder {
676650d1603SAlex Elder 	struct gsi_trans *trans;
677650d1603SAlex Elder 
678650d1603SAlex Elder 	/* Get the last transaction, and wait for it to complete */
679650d1603SAlex Elder 	trans = gsi_channel_trans_last(channel);
680650d1603SAlex Elder 	if (trans) {
681650d1603SAlex Elder 		wait_for_completion(&trans->completion);
682650d1603SAlex Elder 		gsi_trans_free(trans);
683650d1603SAlex Elder 	}
684650d1603SAlex Elder }
685650d1603SAlex Elder 
686650d1603SAlex Elder /* Stop channel activity.  Transactions may not be allocated until thawed. */
687650d1603SAlex Elder static void gsi_channel_freeze(struct gsi_channel *channel)
688650d1603SAlex Elder {
689650d1603SAlex Elder 	gsi_channel_trans_quiesce(channel);
690650d1603SAlex Elder 
691650d1603SAlex Elder 	napi_disable(&channel->napi);
692650d1603SAlex Elder 
693650d1603SAlex Elder 	gsi_irq_ieob_disable(channel->gsi, channel->evt_ring_id);
694650d1603SAlex Elder }
695650d1603SAlex Elder 
696650d1603SAlex Elder /* Allow transactions to be used on the channel again. */
697650d1603SAlex Elder static void gsi_channel_thaw(struct gsi_channel *channel)
698650d1603SAlex Elder {
699650d1603SAlex Elder 	gsi_irq_ieob_enable(channel->gsi, channel->evt_ring_id);
700650d1603SAlex Elder 
701650d1603SAlex Elder 	napi_enable(&channel->napi);
702650d1603SAlex Elder }
703650d1603SAlex Elder 
704650d1603SAlex Elder /* Program a channel for use */
705650d1603SAlex Elder static void gsi_channel_program(struct gsi_channel *channel, bool doorbell)
706650d1603SAlex Elder {
707650d1603SAlex Elder 	size_t size = channel->tre_ring.count * GSI_RING_ELEMENT_SIZE;
708650d1603SAlex Elder 	u32 channel_id = gsi_channel_id(channel);
709650d1603SAlex Elder 	union gsi_channel_scratch scr = { };
710650d1603SAlex Elder 	struct gsi_channel_scratch_gpi *gpi;
711650d1603SAlex Elder 	struct gsi *gsi = channel->gsi;
712650d1603SAlex Elder 	u32 wrr_weight = 0;
713650d1603SAlex Elder 	u32 val;
714650d1603SAlex Elder 
715650d1603SAlex Elder 	/* Arbitrarily pick TRE 0 as the first channel element to use */
716650d1603SAlex Elder 	channel->tre_ring.index = 0;
717650d1603SAlex Elder 
718650d1603SAlex Elder 	/* We program all channels to use GPI protocol */
719650d1603SAlex Elder 	val = u32_encode_bits(GSI_CHANNEL_PROTOCOL_GPI, CHTYPE_PROTOCOL_FMASK);
720650d1603SAlex Elder 	if (channel->toward_ipa)
721650d1603SAlex Elder 		val |= CHTYPE_DIR_FMASK;
722650d1603SAlex Elder 	val |= u32_encode_bits(channel->evt_ring_id, ERINDEX_FMASK);
723650d1603SAlex Elder 	val |= u32_encode_bits(GSI_RING_ELEMENT_SIZE, ELEMENT_SIZE_FMASK);
724650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CH_C_CNTXT_0_OFFSET(channel_id));
725650d1603SAlex Elder 
726650d1603SAlex Elder 	val = u32_encode_bits(size, R_LENGTH_FMASK);
727650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CH_C_CNTXT_1_OFFSET(channel_id));
728650d1603SAlex Elder 
729650d1603SAlex Elder 	/* The context 2 and 3 registers store the low-order and
730650d1603SAlex Elder 	 * high-order 32 bits of the address of the channel ring,
731650d1603SAlex Elder 	 * respectively.
732650d1603SAlex Elder 	 */
733650d1603SAlex Elder 	val = channel->tre_ring.addr & GENMASK(31, 0);
734650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CH_C_CNTXT_2_OFFSET(channel_id));
735650d1603SAlex Elder 
736650d1603SAlex Elder 	val = channel->tre_ring.addr >> 32;
737650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CH_C_CNTXT_3_OFFSET(channel_id));
738650d1603SAlex Elder 
739650d1603SAlex Elder 	/* Command channel gets low weighted round-robin priority */
740650d1603SAlex Elder 	if (channel->command)
741650d1603SAlex Elder 		wrr_weight = field_max(WRR_WEIGHT_FMASK);
742650d1603SAlex Elder 	val = u32_encode_bits(wrr_weight, WRR_WEIGHT_FMASK);
743650d1603SAlex Elder 
744650d1603SAlex Elder 	/* Max prefetch is 1 segment (do not set MAX_PREFETCH_FMASK) */
745650d1603SAlex Elder 
746650d1603SAlex Elder 	/* Enable the doorbell engine if requested */
747650d1603SAlex Elder 	if (doorbell)
748650d1603SAlex Elder 		val |= USE_DB_ENG_FMASK;
749650d1603SAlex Elder 
750*14dbf977SAlex Elder 	/* Starting with IPA v4.0 the command channel uses the escape buffer */
751*14dbf977SAlex Elder 	if (gsi->version != IPA_VERSION_3_5_1 && channel->command)
752650d1603SAlex Elder 		val |= USE_ESCAPE_BUF_ONLY_FMASK;
753650d1603SAlex Elder 
754650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CH_C_QOS_OFFSET(channel_id));
755650d1603SAlex Elder 
756650d1603SAlex Elder 	/* Now update the scratch registers for GPI protocol */
757650d1603SAlex Elder 	gpi = &scr.gpi;
758650d1603SAlex Elder 	gpi->max_outstanding_tre = gsi_channel_trans_tre_max(gsi, channel_id) *
759650d1603SAlex Elder 					GSI_RING_ELEMENT_SIZE;
760650d1603SAlex Elder 	gpi->outstanding_threshold = 2 * GSI_RING_ELEMENT_SIZE;
761650d1603SAlex Elder 
762650d1603SAlex Elder 	val = scr.data.word1;
763650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CH_C_SCRATCH_0_OFFSET(channel_id));
764650d1603SAlex Elder 
765650d1603SAlex Elder 	val = scr.data.word2;
766650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CH_C_SCRATCH_1_OFFSET(channel_id));
767650d1603SAlex Elder 
768650d1603SAlex Elder 	val = scr.data.word3;
769650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CH_C_SCRATCH_2_OFFSET(channel_id));
770650d1603SAlex Elder 
771650d1603SAlex Elder 	/* We must preserve the upper 16 bits of the last scratch register.
772650d1603SAlex Elder 	 * The next sequence assumes those bits remain unchanged between the
773650d1603SAlex Elder 	 * read and the write.
774650d1603SAlex Elder 	 */
775650d1603SAlex Elder 	val = ioread32(gsi->virt + GSI_CH_C_SCRATCH_3_OFFSET(channel_id));
776650d1603SAlex Elder 	val = (scr.data.word4 & GENMASK(31, 16)) | (val & GENMASK(15, 0));
777650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CH_C_SCRATCH_3_OFFSET(channel_id));
778650d1603SAlex Elder 
779650d1603SAlex Elder 	/* All done! */
780650d1603SAlex Elder }
781650d1603SAlex Elder 
782650d1603SAlex Elder static void gsi_channel_deprogram(struct gsi_channel *channel)
783650d1603SAlex Elder {
784650d1603SAlex Elder 	/* Nothing to do */
785650d1603SAlex Elder }
786650d1603SAlex Elder 
787650d1603SAlex Elder /* Start an allocated GSI channel */
788650d1603SAlex Elder int gsi_channel_start(struct gsi *gsi, u32 channel_id)
789650d1603SAlex Elder {
790650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
791650d1603SAlex Elder 	int ret;
792650d1603SAlex Elder 
793650d1603SAlex Elder 	mutex_lock(&gsi->mutex);
794650d1603SAlex Elder 
795650d1603SAlex Elder 	ret = gsi_channel_start_command(channel);
796650d1603SAlex Elder 
797650d1603SAlex Elder 	mutex_unlock(&gsi->mutex);
798650d1603SAlex Elder 
799650d1603SAlex Elder 	gsi_channel_thaw(channel);
800650d1603SAlex Elder 
801650d1603SAlex Elder 	return ret;
802650d1603SAlex Elder }
803650d1603SAlex Elder 
804650d1603SAlex Elder /* Stop a started channel */
805650d1603SAlex Elder int gsi_channel_stop(struct gsi *gsi, u32 channel_id)
806650d1603SAlex Elder {
807650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
808650d1603SAlex Elder 	u32 retries;
809650d1603SAlex Elder 	int ret;
810650d1603SAlex Elder 
811650d1603SAlex Elder 	gsi_channel_freeze(channel);
812650d1603SAlex Elder 
813650d1603SAlex Elder 	/* RX channels might require a little time to enter STOPPED state */
814650d1603SAlex Elder 	retries = channel->toward_ipa ? 0 : GSI_CHANNEL_STOP_RX_RETRIES;
815650d1603SAlex Elder 
816650d1603SAlex Elder 	mutex_lock(&gsi->mutex);
817650d1603SAlex Elder 
818650d1603SAlex Elder 	do {
819650d1603SAlex Elder 		ret = gsi_channel_stop_command(channel);
820650d1603SAlex Elder 		if (ret != -EAGAIN)
821650d1603SAlex Elder 			break;
822650d1603SAlex Elder 		msleep(1);
823650d1603SAlex Elder 	} while (retries--);
824650d1603SAlex Elder 
825650d1603SAlex Elder 	mutex_unlock(&gsi->mutex);
826650d1603SAlex Elder 
827650d1603SAlex Elder 	/* Thaw the channel if we need to retry (or on error) */
828650d1603SAlex Elder 	if (ret)
829650d1603SAlex Elder 		gsi_channel_thaw(channel);
830650d1603SAlex Elder 
831650d1603SAlex Elder 	return ret;
832650d1603SAlex Elder }
833650d1603SAlex Elder 
834650d1603SAlex Elder /* Reset and reconfigure a channel (possibly leaving doorbell disabled) */
835f86a1909SAlex Elder void gsi_channel_reset(struct gsi *gsi, u32 channel_id, bool legacy)
836650d1603SAlex Elder {
837650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
838650d1603SAlex Elder 
839650d1603SAlex Elder 	mutex_lock(&gsi->mutex);
840650d1603SAlex Elder 
841650d1603SAlex Elder 	gsi_channel_reset_command(channel);
842a3f2405bSAlex Elder 	/* Due to a hardware quirk we may need to reset RX channels twice. */
843a3f2405bSAlex Elder 	if (legacy && !channel->toward_ipa)
844650d1603SAlex Elder 		gsi_channel_reset_command(channel);
845650d1603SAlex Elder 
846f86a1909SAlex Elder 	gsi_channel_program(channel, legacy);
847650d1603SAlex Elder 	gsi_channel_trans_cancel_pending(channel);
848650d1603SAlex Elder 
849650d1603SAlex Elder 	mutex_unlock(&gsi->mutex);
850650d1603SAlex Elder }
851650d1603SAlex Elder 
852650d1603SAlex Elder /* Stop a STARTED channel for suspend (using stop if requested) */
853650d1603SAlex Elder int gsi_channel_suspend(struct gsi *gsi, u32 channel_id, bool stop)
854650d1603SAlex Elder {
855650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
856650d1603SAlex Elder 
857650d1603SAlex Elder 	if (stop)
858650d1603SAlex Elder 		return gsi_channel_stop(gsi, channel_id);
859650d1603SAlex Elder 
860650d1603SAlex Elder 	gsi_channel_freeze(channel);
861650d1603SAlex Elder 
862650d1603SAlex Elder 	return 0;
863650d1603SAlex Elder }
864650d1603SAlex Elder 
865650d1603SAlex Elder /* Resume a suspended channel (starting will be requested if STOPPED) */
866650d1603SAlex Elder int gsi_channel_resume(struct gsi *gsi, u32 channel_id, bool start)
867650d1603SAlex Elder {
868650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
869650d1603SAlex Elder 
870650d1603SAlex Elder 	if (start)
871650d1603SAlex Elder 		return gsi_channel_start(gsi, channel_id);
872650d1603SAlex Elder 
873650d1603SAlex Elder 	gsi_channel_thaw(channel);
874650d1603SAlex Elder 
875650d1603SAlex Elder 	return 0;
876650d1603SAlex Elder }
877650d1603SAlex Elder 
878650d1603SAlex Elder /**
879650d1603SAlex Elder  * gsi_channel_tx_queued() - Report queued TX transfers for a channel
880650d1603SAlex Elder  * @channel:	Channel for which to report
881650d1603SAlex Elder  *
882650d1603SAlex Elder  * Report to the network stack the number of bytes and transactions that
883650d1603SAlex Elder  * have been queued to hardware since last call.  This and the next function
884650d1603SAlex Elder  * supply information used by the network stack for throttling.
885650d1603SAlex Elder  *
886650d1603SAlex Elder  * For each channel we track the number of transactions used and bytes of
887650d1603SAlex Elder  * data those transactions represent.  We also track what those values are
888650d1603SAlex Elder  * each time this function is called.  Subtracting the two tells us
889650d1603SAlex Elder  * the number of bytes and transactions that have been added between
890650d1603SAlex Elder  * successive calls.
891650d1603SAlex Elder  *
892650d1603SAlex Elder  * Calling this each time we ring the channel doorbell allows us to
893650d1603SAlex Elder  * provide accurate information to the network stack about how much
894650d1603SAlex Elder  * work we've given the hardware at any point in time.
895650d1603SAlex Elder  */
896650d1603SAlex Elder void gsi_channel_tx_queued(struct gsi_channel *channel)
897650d1603SAlex Elder {
898650d1603SAlex Elder 	u32 trans_count;
899650d1603SAlex Elder 	u32 byte_count;
900650d1603SAlex Elder 
901650d1603SAlex Elder 	byte_count = channel->byte_count - channel->queued_byte_count;
902650d1603SAlex Elder 	trans_count = channel->trans_count - channel->queued_trans_count;
903650d1603SAlex Elder 	channel->queued_byte_count = channel->byte_count;
904650d1603SAlex Elder 	channel->queued_trans_count = channel->trans_count;
905650d1603SAlex Elder 
906650d1603SAlex Elder 	ipa_gsi_channel_tx_queued(channel->gsi, gsi_channel_id(channel),
907650d1603SAlex Elder 				  trans_count, byte_count);
908650d1603SAlex Elder }
909650d1603SAlex Elder 
910650d1603SAlex Elder /**
911650d1603SAlex Elder  * gsi_channel_tx_update() - Report completed TX transfers
912650d1603SAlex Elder  * @channel:	Channel that has completed transmitting packets
913650d1603SAlex Elder  * @trans:	Last transation known to be complete
914650d1603SAlex Elder  *
915650d1603SAlex Elder  * Compute the number of transactions and bytes that have been transferred
916650d1603SAlex Elder  * over a TX channel since the given transaction was committed.  Report this
917650d1603SAlex Elder  * information to the network stack.
918650d1603SAlex Elder  *
919650d1603SAlex Elder  * At the time a transaction is committed, we record its channel's
920650d1603SAlex Elder  * committed transaction and byte counts *in the transaction*.
921650d1603SAlex Elder  * Completions are signaled by the hardware with an interrupt, and
922650d1603SAlex Elder  * we can determine the latest completed transaction at that time.
923650d1603SAlex Elder  *
924650d1603SAlex Elder  * The difference between the byte/transaction count recorded in
925650d1603SAlex Elder  * the transaction and the count last time we recorded a completion
926650d1603SAlex Elder  * tells us exactly how much data has been transferred between
927650d1603SAlex Elder  * completions.
928650d1603SAlex Elder  *
929650d1603SAlex Elder  * Calling this each time we learn of a newly-completed transaction
930650d1603SAlex Elder  * allows us to provide accurate information to the network stack
931650d1603SAlex Elder  * about how much work has been completed by the hardware at a given
932650d1603SAlex Elder  * point in time.
933650d1603SAlex Elder  */
934650d1603SAlex Elder static void
935650d1603SAlex Elder gsi_channel_tx_update(struct gsi_channel *channel, struct gsi_trans *trans)
936650d1603SAlex Elder {
937650d1603SAlex Elder 	u64 byte_count = trans->byte_count + trans->len;
938650d1603SAlex Elder 	u64 trans_count = trans->trans_count + 1;
939650d1603SAlex Elder 
940650d1603SAlex Elder 	byte_count -= channel->compl_byte_count;
941650d1603SAlex Elder 	channel->compl_byte_count += byte_count;
942650d1603SAlex Elder 	trans_count -= channel->compl_trans_count;
943650d1603SAlex Elder 	channel->compl_trans_count += trans_count;
944650d1603SAlex Elder 
945650d1603SAlex Elder 	ipa_gsi_channel_tx_completed(channel->gsi, gsi_channel_id(channel),
946650d1603SAlex Elder 				     trans_count, byte_count);
947650d1603SAlex Elder }
948650d1603SAlex Elder 
949650d1603SAlex Elder /* Channel control interrupt handler */
950650d1603SAlex Elder static void gsi_isr_chan_ctrl(struct gsi *gsi)
951650d1603SAlex Elder {
952650d1603SAlex Elder 	u32 channel_mask;
953650d1603SAlex Elder 
954650d1603SAlex Elder 	channel_mask = ioread32(gsi->virt + GSI_CNTXT_SRC_CH_IRQ_OFFSET);
955650d1603SAlex Elder 	iowrite32(channel_mask, gsi->virt + GSI_CNTXT_SRC_CH_IRQ_CLR_OFFSET);
956650d1603SAlex Elder 
957650d1603SAlex Elder 	while (channel_mask) {
958650d1603SAlex Elder 		u32 channel_id = __ffs(channel_mask);
959650d1603SAlex Elder 		struct gsi_channel *channel;
960650d1603SAlex Elder 
961650d1603SAlex Elder 		channel_mask ^= BIT(channel_id);
962650d1603SAlex Elder 
963650d1603SAlex Elder 		channel = &gsi->channel[channel_id];
964650d1603SAlex Elder 
965650d1603SAlex Elder 		complete(&channel->completion);
966650d1603SAlex Elder 	}
967650d1603SAlex Elder }
968650d1603SAlex Elder 
969650d1603SAlex Elder /* Event ring control interrupt handler */
970650d1603SAlex Elder static void gsi_isr_evt_ctrl(struct gsi *gsi)
971650d1603SAlex Elder {
972650d1603SAlex Elder 	u32 event_mask;
973650d1603SAlex Elder 
974650d1603SAlex Elder 	event_mask = ioread32(gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_OFFSET);
975650d1603SAlex Elder 	iowrite32(event_mask, gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_CLR_OFFSET);
976650d1603SAlex Elder 
977650d1603SAlex Elder 	while (event_mask) {
978650d1603SAlex Elder 		u32 evt_ring_id = __ffs(event_mask);
979650d1603SAlex Elder 		struct gsi_evt_ring *evt_ring;
980650d1603SAlex Elder 
981650d1603SAlex Elder 		event_mask ^= BIT(evt_ring_id);
982650d1603SAlex Elder 
983650d1603SAlex Elder 		evt_ring = &gsi->evt_ring[evt_ring_id];
984650d1603SAlex Elder 		evt_ring->state = gsi_evt_ring_state(gsi, evt_ring_id);
985650d1603SAlex Elder 
986650d1603SAlex Elder 		complete(&evt_ring->completion);
987650d1603SAlex Elder 	}
988650d1603SAlex Elder }
989650d1603SAlex Elder 
990650d1603SAlex Elder /* Global channel error interrupt handler */
991650d1603SAlex Elder static void
992650d1603SAlex Elder gsi_isr_glob_chan_err(struct gsi *gsi, u32 err_ee, u32 channel_id, u32 code)
993650d1603SAlex Elder {
994650d1603SAlex Elder 	if (code == GSI_OUT_OF_RESOURCES_ERR) {
995650d1603SAlex Elder 		dev_err(gsi->dev, "channel %u out of resources\n", channel_id);
996650d1603SAlex Elder 		complete(&gsi->channel[channel_id].completion);
997650d1603SAlex Elder 		return;
998650d1603SAlex Elder 	}
999650d1603SAlex Elder 
1000650d1603SAlex Elder 	/* Report, but otherwise ignore all other error codes */
1001650d1603SAlex Elder 	dev_err(gsi->dev, "channel %u global error ee 0x%08x code 0x%08x\n",
1002650d1603SAlex Elder 		channel_id, err_ee, code);
1003650d1603SAlex Elder }
1004650d1603SAlex Elder 
1005650d1603SAlex Elder /* Global event error interrupt handler */
1006650d1603SAlex Elder static void
1007650d1603SAlex Elder gsi_isr_glob_evt_err(struct gsi *gsi, u32 err_ee, u32 evt_ring_id, u32 code)
1008650d1603SAlex Elder {
1009650d1603SAlex Elder 	if (code == GSI_OUT_OF_RESOURCES_ERR) {
1010650d1603SAlex Elder 		struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
1011650d1603SAlex Elder 		u32 channel_id = gsi_channel_id(evt_ring->channel);
1012650d1603SAlex Elder 
1013650d1603SAlex Elder 		complete(&evt_ring->completion);
1014650d1603SAlex Elder 		dev_err(gsi->dev, "evt_ring for channel %u out of resources\n",
1015650d1603SAlex Elder 			channel_id);
1016650d1603SAlex Elder 		return;
1017650d1603SAlex Elder 	}
1018650d1603SAlex Elder 
1019650d1603SAlex Elder 	/* Report, but otherwise ignore all other error codes */
1020650d1603SAlex Elder 	dev_err(gsi->dev, "event ring %u global error ee %u code 0x%08x\n",
1021650d1603SAlex Elder 		evt_ring_id, err_ee, code);
1022650d1603SAlex Elder }
1023650d1603SAlex Elder 
1024650d1603SAlex Elder /* Global error interrupt handler */
1025650d1603SAlex Elder static void gsi_isr_glob_err(struct gsi *gsi)
1026650d1603SAlex Elder {
1027650d1603SAlex Elder 	enum gsi_err_type type;
1028650d1603SAlex Elder 	enum gsi_err_code code;
1029650d1603SAlex Elder 	u32 which;
1030650d1603SAlex Elder 	u32 val;
1031650d1603SAlex Elder 	u32 ee;
1032650d1603SAlex Elder 
1033650d1603SAlex Elder 	/* Get the logged error, then reinitialize the log */
1034650d1603SAlex Elder 	val = ioread32(gsi->virt + GSI_ERROR_LOG_OFFSET);
1035650d1603SAlex Elder 	iowrite32(0, gsi->virt + GSI_ERROR_LOG_OFFSET);
1036650d1603SAlex Elder 	iowrite32(~0, gsi->virt + GSI_ERROR_LOG_CLR_OFFSET);
1037650d1603SAlex Elder 
1038650d1603SAlex Elder 	ee = u32_get_bits(val, ERR_EE_FMASK);
1039650d1603SAlex Elder 	which = u32_get_bits(val, ERR_VIRT_IDX_FMASK);
1040650d1603SAlex Elder 	type = u32_get_bits(val, ERR_TYPE_FMASK);
1041650d1603SAlex Elder 	code = u32_get_bits(val, ERR_CODE_FMASK);
1042650d1603SAlex Elder 
1043650d1603SAlex Elder 	if (type == GSI_ERR_TYPE_CHAN)
1044650d1603SAlex Elder 		gsi_isr_glob_chan_err(gsi, ee, which, code);
1045650d1603SAlex Elder 	else if (type == GSI_ERR_TYPE_EVT)
1046650d1603SAlex Elder 		gsi_isr_glob_evt_err(gsi, ee, which, code);
1047650d1603SAlex Elder 	else	/* type GSI_ERR_TYPE_GLOB should be fatal */
1048650d1603SAlex Elder 		dev_err(gsi->dev, "unexpected global error 0x%08x\n", type);
1049650d1603SAlex Elder }
1050650d1603SAlex Elder 
1051650d1603SAlex Elder /* Generic EE interrupt handler */
1052650d1603SAlex Elder static void gsi_isr_gp_int1(struct gsi *gsi)
1053650d1603SAlex Elder {
1054650d1603SAlex Elder 	u32 result;
1055650d1603SAlex Elder 	u32 val;
1056650d1603SAlex Elder 
1057650d1603SAlex Elder 	val = ioread32(gsi->virt + GSI_CNTXT_SCRATCH_0_OFFSET);
1058650d1603SAlex Elder 	result = u32_get_bits(val, GENERIC_EE_RESULT_FMASK);
1059650d1603SAlex Elder 	if (result != GENERIC_EE_SUCCESS_FVAL)
1060650d1603SAlex Elder 		dev_err(gsi->dev, "global INT1 generic result %u\n", result);
1061650d1603SAlex Elder 
1062650d1603SAlex Elder 	complete(&gsi->completion);
1063650d1603SAlex Elder }
10640b1ba18aSAlex Elder 
1065650d1603SAlex Elder /* Inter-EE interrupt handler */
1066650d1603SAlex Elder static void gsi_isr_glob_ee(struct gsi *gsi)
1067650d1603SAlex Elder {
1068650d1603SAlex Elder 	u32 val;
1069650d1603SAlex Elder 
1070650d1603SAlex Elder 	val = ioread32(gsi->virt + GSI_CNTXT_GLOB_IRQ_STTS_OFFSET);
1071650d1603SAlex Elder 
1072650d1603SAlex Elder 	if (val & ERROR_INT_FMASK)
1073650d1603SAlex Elder 		gsi_isr_glob_err(gsi);
1074650d1603SAlex Elder 
1075650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CNTXT_GLOB_IRQ_CLR_OFFSET);
1076650d1603SAlex Elder 
1077650d1603SAlex Elder 	val &= ~ERROR_INT_FMASK;
1078650d1603SAlex Elder 
1079d61bb716SAlex Elder 	if (val & GP_INT1_FMASK) {
1080d61bb716SAlex Elder 		val ^= GP_INT1_FMASK;
1081650d1603SAlex Elder 		gsi_isr_gp_int1(gsi);
1082650d1603SAlex Elder 	}
1083650d1603SAlex Elder 
1084650d1603SAlex Elder 	if (val)
1085650d1603SAlex Elder 		dev_err(gsi->dev, "unexpected global interrupt 0x%08x\n", val);
1086650d1603SAlex Elder }
1087650d1603SAlex Elder 
1088650d1603SAlex Elder /* I/O completion interrupt event */
1089650d1603SAlex Elder static void gsi_isr_ieob(struct gsi *gsi)
1090650d1603SAlex Elder {
1091650d1603SAlex Elder 	u32 event_mask;
1092650d1603SAlex Elder 
1093650d1603SAlex Elder 	event_mask = ioread32(gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_OFFSET);
1094195ef57fSAlex Elder 	iowrite32(event_mask, gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_CLR_OFFSET);
1095650d1603SAlex Elder 
1096650d1603SAlex Elder 	while (event_mask) {
1097650d1603SAlex Elder 		u32 evt_ring_id = __ffs(event_mask);
1098650d1603SAlex Elder 
1099650d1603SAlex Elder 		event_mask ^= BIT(evt_ring_id);
1100650d1603SAlex Elder 
1101650d1603SAlex Elder 		gsi_irq_ieob_disable(gsi, evt_ring_id);
1102650d1603SAlex Elder 		napi_schedule(&gsi->evt_ring[evt_ring_id].channel->napi);
1103650d1603SAlex Elder 	}
1104650d1603SAlex Elder }
1105650d1603SAlex Elder 
1106650d1603SAlex Elder /* General event interrupts represent serious problems, so report them */
1107650d1603SAlex Elder static void gsi_isr_general(struct gsi *gsi)
1108650d1603SAlex Elder {
1109650d1603SAlex Elder 	struct device *dev = gsi->dev;
1110650d1603SAlex Elder 	u32 val;
1111650d1603SAlex Elder 
1112650d1603SAlex Elder 	val = ioread32(gsi->virt + GSI_CNTXT_GSI_IRQ_STTS_OFFSET);
1113650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CNTXT_GSI_IRQ_CLR_OFFSET);
1114650d1603SAlex Elder 
1115650d1603SAlex Elder 	if (val)
1116650d1603SAlex Elder 		dev_err(dev, "unexpected general interrupt 0x%08x\n", val);
1117650d1603SAlex Elder }
1118650d1603SAlex Elder 
1119650d1603SAlex Elder /**
1120650d1603SAlex Elder  * gsi_isr() - Top level GSI interrupt service routine
1121650d1603SAlex Elder  * @irq:	Interrupt number (ignored)
1122650d1603SAlex Elder  * @dev_id:	GSI pointer supplied to request_irq()
1123650d1603SAlex Elder  *
1124650d1603SAlex Elder  * This is the main handler function registered for the GSI IRQ. Each type
1125650d1603SAlex Elder  * of interrupt has a separate handler function that is called from here.
1126650d1603SAlex Elder  */
1127650d1603SAlex Elder static irqreturn_t gsi_isr(int irq, void *dev_id)
1128650d1603SAlex Elder {
1129650d1603SAlex Elder 	struct gsi *gsi = dev_id;
1130650d1603SAlex Elder 	u32 intr_mask;
1131650d1603SAlex Elder 	u32 cnt = 0;
1132650d1603SAlex Elder 
1133650d1603SAlex Elder 	while ((intr_mask = ioread32(gsi->virt + GSI_CNTXT_TYPE_IRQ_OFFSET))) {
1134650d1603SAlex Elder 		/* intr_mask contains bitmask of pending GSI interrupts */
1135650d1603SAlex Elder 		do {
1136650d1603SAlex Elder 			u32 gsi_intr = BIT(__ffs(intr_mask));
1137650d1603SAlex Elder 
1138650d1603SAlex Elder 			intr_mask ^= gsi_intr;
1139650d1603SAlex Elder 
1140650d1603SAlex Elder 			switch (gsi_intr) {
1141650d1603SAlex Elder 			case CH_CTRL_FMASK:
1142650d1603SAlex Elder 				gsi_isr_chan_ctrl(gsi);
1143650d1603SAlex Elder 				break;
1144650d1603SAlex Elder 			case EV_CTRL_FMASK:
1145650d1603SAlex Elder 				gsi_isr_evt_ctrl(gsi);
1146650d1603SAlex Elder 				break;
1147650d1603SAlex Elder 			case GLOB_EE_FMASK:
1148650d1603SAlex Elder 				gsi_isr_glob_ee(gsi);
1149650d1603SAlex Elder 				break;
1150650d1603SAlex Elder 			case IEOB_FMASK:
1151650d1603SAlex Elder 				gsi_isr_ieob(gsi);
1152650d1603SAlex Elder 				break;
1153650d1603SAlex Elder 			case GENERAL_FMASK:
1154650d1603SAlex Elder 				gsi_isr_general(gsi);
1155650d1603SAlex Elder 				break;
1156650d1603SAlex Elder 			default:
1157650d1603SAlex Elder 				dev_err(gsi->dev,
11588463488aSAlex Elder 					"unrecognized interrupt type 0x%08x\n",
11598463488aSAlex Elder 					gsi_intr);
1160650d1603SAlex Elder 				break;
1161650d1603SAlex Elder 			}
1162650d1603SAlex Elder 		} while (intr_mask);
1163650d1603SAlex Elder 
1164650d1603SAlex Elder 		if (++cnt > GSI_ISR_MAX_ITER) {
1165650d1603SAlex Elder 			dev_err(gsi->dev, "interrupt flood\n");
1166650d1603SAlex Elder 			break;
1167650d1603SAlex Elder 		}
1168650d1603SAlex Elder 	}
1169650d1603SAlex Elder 
1170650d1603SAlex Elder 	return IRQ_HANDLED;
1171650d1603SAlex Elder }
1172650d1603SAlex Elder 
1173650d1603SAlex Elder /* Return the transaction associated with a transfer completion event */
1174650d1603SAlex Elder static struct gsi_trans *gsi_event_trans(struct gsi_channel *channel,
1175650d1603SAlex Elder 					 struct gsi_event *event)
1176650d1603SAlex Elder {
1177650d1603SAlex Elder 	u32 tre_offset;
1178650d1603SAlex Elder 	u32 tre_index;
1179650d1603SAlex Elder 
1180650d1603SAlex Elder 	/* Event xfer_ptr records the TRE it's associated with */
1181650d1603SAlex Elder 	tre_offset = le64_to_cpu(event->xfer_ptr) & GENMASK(31, 0);
1182650d1603SAlex Elder 	tre_index = gsi_ring_index(&channel->tre_ring, tre_offset);
1183650d1603SAlex Elder 
1184650d1603SAlex Elder 	return gsi_channel_trans_mapped(channel, tre_index);
1185650d1603SAlex Elder }
1186650d1603SAlex Elder 
1187650d1603SAlex Elder /**
1188650d1603SAlex Elder  * gsi_evt_ring_rx_update() - Record lengths of received data
1189650d1603SAlex Elder  * @evt_ring:	Event ring associated with channel that received packets
1190650d1603SAlex Elder  * @index:	Event index in ring reported by hardware
1191650d1603SAlex Elder  *
1192650d1603SAlex Elder  * Events for RX channels contain the actual number of bytes received into
1193650d1603SAlex Elder  * the buffer.  Every event has a transaction associated with it, and here
1194650d1603SAlex Elder  * we update transactions to record their actual received lengths.
1195650d1603SAlex Elder  *
1196650d1603SAlex Elder  * This function is called whenever we learn that the GSI hardware has filled
1197650d1603SAlex Elder  * new events since the last time we checked.  The ring's index field tells
1198650d1603SAlex Elder  * the first entry in need of processing.  The index provided is the
1199650d1603SAlex Elder  * first *unfilled* event in the ring (following the last filled one).
1200650d1603SAlex Elder  *
1201650d1603SAlex Elder  * Events are sequential within the event ring, and transactions are
1202650d1603SAlex Elder  * sequential within the transaction pool.
1203650d1603SAlex Elder  *
1204650d1603SAlex Elder  * Note that @index always refers to an element *within* the event ring.
1205650d1603SAlex Elder  */
1206650d1603SAlex Elder static void gsi_evt_ring_rx_update(struct gsi_evt_ring *evt_ring, u32 index)
1207650d1603SAlex Elder {
1208650d1603SAlex Elder 	struct gsi_channel *channel = evt_ring->channel;
1209650d1603SAlex Elder 	struct gsi_ring *ring = &evt_ring->ring;
1210650d1603SAlex Elder 	struct gsi_trans_info *trans_info;
1211650d1603SAlex Elder 	struct gsi_event *event_done;
1212650d1603SAlex Elder 	struct gsi_event *event;
1213650d1603SAlex Elder 	struct gsi_trans *trans;
1214650d1603SAlex Elder 	u32 byte_count = 0;
1215650d1603SAlex Elder 	u32 old_index;
1216650d1603SAlex Elder 	u32 event_avail;
1217650d1603SAlex Elder 
1218650d1603SAlex Elder 	trans_info = &channel->trans_info;
1219650d1603SAlex Elder 
1220650d1603SAlex Elder 	/* We'll start with the oldest un-processed event.  RX channels
1221650d1603SAlex Elder 	 * replenish receive buffers in single-TRE transactions, so we
1222650d1603SAlex Elder 	 * can just map that event to its transaction.  Transactions
1223650d1603SAlex Elder 	 * associated with completion events are consecutive.
1224650d1603SAlex Elder 	 */
1225650d1603SAlex Elder 	old_index = ring->index;
1226650d1603SAlex Elder 	event = gsi_ring_virt(ring, old_index);
1227650d1603SAlex Elder 	trans = gsi_event_trans(channel, event);
1228650d1603SAlex Elder 
1229650d1603SAlex Elder 	/* Compute the number of events to process before we wrap,
1230650d1603SAlex Elder 	 * and determine when we'll be done processing events.
1231650d1603SAlex Elder 	 */
1232650d1603SAlex Elder 	event_avail = ring->count - old_index % ring->count;
1233650d1603SAlex Elder 	event_done = gsi_ring_virt(ring, index);
1234650d1603SAlex Elder 	do {
1235650d1603SAlex Elder 		trans->len = __le16_to_cpu(event->len);
1236650d1603SAlex Elder 		byte_count += trans->len;
1237650d1603SAlex Elder 
1238650d1603SAlex Elder 		/* Move on to the next event and transaction */
1239650d1603SAlex Elder 		if (--event_avail)
1240650d1603SAlex Elder 			event++;
1241650d1603SAlex Elder 		else
1242650d1603SAlex Elder 			event = gsi_ring_virt(ring, 0);
1243650d1603SAlex Elder 		trans = gsi_trans_pool_next(&trans_info->pool, trans);
1244650d1603SAlex Elder 	} while (event != event_done);
1245650d1603SAlex Elder 
1246650d1603SAlex Elder 	/* We record RX bytes when they are received */
1247650d1603SAlex Elder 	channel->byte_count += byte_count;
1248650d1603SAlex Elder 	channel->trans_count++;
1249650d1603SAlex Elder }
1250650d1603SAlex Elder 
1251650d1603SAlex Elder /* Initialize a ring, including allocating DMA memory for its entries */
1252650d1603SAlex Elder static int gsi_ring_alloc(struct gsi *gsi, struct gsi_ring *ring, u32 count)
1253650d1603SAlex Elder {
1254650d1603SAlex Elder 	size_t size = count * GSI_RING_ELEMENT_SIZE;
1255650d1603SAlex Elder 	struct device *dev = gsi->dev;
1256650d1603SAlex Elder 	dma_addr_t addr;
1257650d1603SAlex Elder 
1258650d1603SAlex Elder 	/* Hardware requires a 2^n ring size, with alignment equal to size */
1259650d1603SAlex Elder 	ring->virt = dma_alloc_coherent(dev, size, &addr, GFP_KERNEL);
1260650d1603SAlex Elder 	if (ring->virt && addr % size) {
1261650d1603SAlex Elder 		dma_free_coherent(dev, size, ring->virt, ring->addr);
1262650d1603SAlex Elder 		dev_err(dev, "unable to alloc 0x%zx-aligned ring buffer\n",
1263650d1603SAlex Elder 			size);
1264650d1603SAlex Elder 		return -EINVAL;	/* Not a good error value, but distinct */
1265650d1603SAlex Elder 	} else if (!ring->virt) {
1266650d1603SAlex Elder 		return -ENOMEM;
1267650d1603SAlex Elder 	}
1268650d1603SAlex Elder 	ring->addr = addr;
1269650d1603SAlex Elder 	ring->count = count;
1270650d1603SAlex Elder 
1271650d1603SAlex Elder 	return 0;
1272650d1603SAlex Elder }
1273650d1603SAlex Elder 
1274650d1603SAlex Elder /* Free a previously-allocated ring */
1275650d1603SAlex Elder static void gsi_ring_free(struct gsi *gsi, struct gsi_ring *ring)
1276650d1603SAlex Elder {
1277650d1603SAlex Elder 	size_t size = ring->count * GSI_RING_ELEMENT_SIZE;
1278650d1603SAlex Elder 
1279650d1603SAlex Elder 	dma_free_coherent(gsi->dev, size, ring->virt, ring->addr);
1280650d1603SAlex Elder }
1281650d1603SAlex Elder 
1282650d1603SAlex Elder /* Allocate an available event ring id */
1283650d1603SAlex Elder static int gsi_evt_ring_id_alloc(struct gsi *gsi)
1284650d1603SAlex Elder {
1285650d1603SAlex Elder 	u32 evt_ring_id;
1286650d1603SAlex Elder 
1287650d1603SAlex Elder 	if (gsi->event_bitmap == ~0U) {
1288650d1603SAlex Elder 		dev_err(gsi->dev, "event rings exhausted\n");
1289650d1603SAlex Elder 		return -ENOSPC;
1290650d1603SAlex Elder 	}
1291650d1603SAlex Elder 
1292650d1603SAlex Elder 	evt_ring_id = ffz(gsi->event_bitmap);
1293650d1603SAlex Elder 	gsi->event_bitmap |= BIT(evt_ring_id);
1294650d1603SAlex Elder 
1295650d1603SAlex Elder 	return (int)evt_ring_id;
1296650d1603SAlex Elder }
1297650d1603SAlex Elder 
1298650d1603SAlex Elder /* Free a previously-allocated event ring id */
1299650d1603SAlex Elder static void gsi_evt_ring_id_free(struct gsi *gsi, u32 evt_ring_id)
1300650d1603SAlex Elder {
1301650d1603SAlex Elder 	gsi->event_bitmap &= ~BIT(evt_ring_id);
1302650d1603SAlex Elder }
1303650d1603SAlex Elder 
1304650d1603SAlex Elder /* Ring a channel doorbell, reporting the first un-filled entry */
1305650d1603SAlex Elder void gsi_channel_doorbell(struct gsi_channel *channel)
1306650d1603SAlex Elder {
1307650d1603SAlex Elder 	struct gsi_ring *tre_ring = &channel->tre_ring;
1308650d1603SAlex Elder 	u32 channel_id = gsi_channel_id(channel);
1309650d1603SAlex Elder 	struct gsi *gsi = channel->gsi;
1310650d1603SAlex Elder 	u32 val;
1311650d1603SAlex Elder 
1312650d1603SAlex Elder 	/* Note: index *must* be used modulo the ring count here */
1313650d1603SAlex Elder 	val = gsi_ring_addr(tre_ring, tre_ring->index % tre_ring->count);
1314650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CH_C_DOORBELL_0_OFFSET(channel_id));
1315650d1603SAlex Elder }
1316650d1603SAlex Elder 
1317650d1603SAlex Elder /* Consult hardware, move any newly completed transactions to completed list */
1318650d1603SAlex Elder static void gsi_channel_update(struct gsi_channel *channel)
1319650d1603SAlex Elder {
1320650d1603SAlex Elder 	u32 evt_ring_id = channel->evt_ring_id;
1321650d1603SAlex Elder 	struct gsi *gsi = channel->gsi;
1322650d1603SAlex Elder 	struct gsi_evt_ring *evt_ring;
1323650d1603SAlex Elder 	struct gsi_trans *trans;
1324650d1603SAlex Elder 	struct gsi_ring *ring;
1325650d1603SAlex Elder 	u32 offset;
1326650d1603SAlex Elder 	u32 index;
1327650d1603SAlex Elder 
1328650d1603SAlex Elder 	evt_ring = &gsi->evt_ring[evt_ring_id];
1329650d1603SAlex Elder 	ring = &evt_ring->ring;
1330650d1603SAlex Elder 
1331650d1603SAlex Elder 	/* See if there's anything new to process; if not, we're done.  Note
1332650d1603SAlex Elder 	 * that index always refers to an entry *within* the event ring.
1333650d1603SAlex Elder 	 */
1334650d1603SAlex Elder 	offset = GSI_EV_CH_E_CNTXT_4_OFFSET(evt_ring_id);
1335650d1603SAlex Elder 	index = gsi_ring_index(ring, ioread32(gsi->virt + offset));
1336650d1603SAlex Elder 	if (index == ring->index % ring->count)
1337650d1603SAlex Elder 		return;
1338650d1603SAlex Elder 
1339650d1603SAlex Elder 	/* Get the transaction for the latest completed event.  Take a
1340650d1603SAlex Elder 	 * reference to keep it from completing before we give the events
1341650d1603SAlex Elder 	 * for this and previous transactions back to the hardware.
1342650d1603SAlex Elder 	 */
1343650d1603SAlex Elder 	trans = gsi_event_trans(channel, gsi_ring_virt(ring, index - 1));
1344650d1603SAlex Elder 	refcount_inc(&trans->refcount);
1345650d1603SAlex Elder 
1346650d1603SAlex Elder 	/* For RX channels, update each completed transaction with the number
1347650d1603SAlex Elder 	 * of bytes that were actually received.  For TX channels, report
1348650d1603SAlex Elder 	 * the number of transactions and bytes this completion represents
1349650d1603SAlex Elder 	 * up the network stack.
1350650d1603SAlex Elder 	 */
1351650d1603SAlex Elder 	if (channel->toward_ipa)
1352650d1603SAlex Elder 		gsi_channel_tx_update(channel, trans);
1353650d1603SAlex Elder 	else
1354650d1603SAlex Elder 		gsi_evt_ring_rx_update(evt_ring, index);
1355650d1603SAlex Elder 
1356650d1603SAlex Elder 	gsi_trans_move_complete(trans);
1357650d1603SAlex Elder 
1358650d1603SAlex Elder 	/* Tell the hardware we've handled these events */
1359650d1603SAlex Elder 	gsi_evt_ring_doorbell(channel->gsi, channel->evt_ring_id, index);
1360650d1603SAlex Elder 
1361650d1603SAlex Elder 	gsi_trans_free(trans);
1362650d1603SAlex Elder }
1363650d1603SAlex Elder 
1364650d1603SAlex Elder /**
1365650d1603SAlex Elder  * gsi_channel_poll_one() - Return a single completed transaction on a channel
1366650d1603SAlex Elder  * @channel:	Channel to be polled
1367650d1603SAlex Elder  *
1368e3eea08eSAlex Elder  * Return:	Transaction pointer, or null if none are available
1369650d1603SAlex Elder  *
1370650d1603SAlex Elder  * This function returns the first entry on a channel's completed transaction
1371650d1603SAlex Elder  * list.  If that list is empty, the hardware is consulted to determine
1372650d1603SAlex Elder  * whether any new transactions have completed.  If so, they're moved to the
1373650d1603SAlex Elder  * completed list and the new first entry is returned.  If there are no more
1374650d1603SAlex Elder  * completed transactions, a null pointer is returned.
1375650d1603SAlex Elder  */
1376650d1603SAlex Elder static struct gsi_trans *gsi_channel_poll_one(struct gsi_channel *channel)
1377650d1603SAlex Elder {
1378650d1603SAlex Elder 	struct gsi_trans *trans;
1379650d1603SAlex Elder 
1380650d1603SAlex Elder 	/* Get the first transaction from the completed list */
1381650d1603SAlex Elder 	trans = gsi_channel_trans_complete(channel);
1382650d1603SAlex Elder 	if (!trans) {
1383650d1603SAlex Elder 		/* List is empty; see if there's more to do */
1384650d1603SAlex Elder 		gsi_channel_update(channel);
1385650d1603SAlex Elder 		trans = gsi_channel_trans_complete(channel);
1386650d1603SAlex Elder 	}
1387650d1603SAlex Elder 
1388650d1603SAlex Elder 	if (trans)
1389650d1603SAlex Elder 		gsi_trans_move_polled(trans);
1390650d1603SAlex Elder 
1391650d1603SAlex Elder 	return trans;
1392650d1603SAlex Elder }
1393650d1603SAlex Elder 
1394650d1603SAlex Elder /**
1395650d1603SAlex Elder  * gsi_channel_poll() - NAPI poll function for a channel
1396650d1603SAlex Elder  * @napi:	NAPI structure for the channel
1397650d1603SAlex Elder  * @budget:	Budget supplied by NAPI core
1398e3eea08eSAlex Elder  *
1399e3eea08eSAlex Elder  * Return:	Number of items polled (<= budget)
1400650d1603SAlex Elder  *
1401650d1603SAlex Elder  * Single transactions completed by hardware are polled until either
1402650d1603SAlex Elder  * the budget is exhausted, or there are no more.  Each transaction
1403650d1603SAlex Elder  * polled is passed to gsi_trans_complete(), to perform remaining
1404650d1603SAlex Elder  * completion processing and retire/free the transaction.
1405650d1603SAlex Elder  */
1406650d1603SAlex Elder static int gsi_channel_poll(struct napi_struct *napi, int budget)
1407650d1603SAlex Elder {
1408650d1603SAlex Elder 	struct gsi_channel *channel;
1409650d1603SAlex Elder 	int count = 0;
1410650d1603SAlex Elder 
1411650d1603SAlex Elder 	channel = container_of(napi, struct gsi_channel, napi);
1412650d1603SAlex Elder 	while (count < budget) {
1413650d1603SAlex Elder 		struct gsi_trans *trans;
1414650d1603SAlex Elder 
1415f45a7bccSAlex Elder 		count++;
1416650d1603SAlex Elder 		trans = gsi_channel_poll_one(channel);
1417650d1603SAlex Elder 		if (!trans)
1418650d1603SAlex Elder 			break;
1419650d1603SAlex Elder 		gsi_trans_complete(trans);
1420650d1603SAlex Elder 	}
1421650d1603SAlex Elder 
1422650d1603SAlex Elder 	if (count < budget) {
1423650d1603SAlex Elder 		napi_complete(&channel->napi);
1424650d1603SAlex Elder 		gsi_irq_ieob_enable(channel->gsi, channel->evt_ring_id);
1425650d1603SAlex Elder 	}
1426650d1603SAlex Elder 
1427650d1603SAlex Elder 	return count;
1428650d1603SAlex Elder }
1429650d1603SAlex Elder 
1430650d1603SAlex Elder /* The event bitmap represents which event ids are available for allocation.
1431650d1603SAlex Elder  * Set bits are not available, clear bits can be used.  This function
1432650d1603SAlex Elder  * initializes the map so all events supported by the hardware are available,
1433650d1603SAlex Elder  * then precludes any reserved events from being allocated.
1434650d1603SAlex Elder  */
1435650d1603SAlex Elder static u32 gsi_event_bitmap_init(u32 evt_ring_max)
1436650d1603SAlex Elder {
1437650d1603SAlex Elder 	u32 event_bitmap = GENMASK(BITS_PER_LONG - 1, evt_ring_max);
1438650d1603SAlex Elder 
1439650d1603SAlex Elder 	event_bitmap |= GENMASK(GSI_MHI_EVENT_ID_END, GSI_MHI_EVENT_ID_START);
1440650d1603SAlex Elder 
1441650d1603SAlex Elder 	return event_bitmap;
1442650d1603SAlex Elder }
1443650d1603SAlex Elder 
1444650d1603SAlex Elder /* Setup function for event rings */
1445650d1603SAlex Elder static void gsi_evt_ring_setup(struct gsi *gsi)
1446650d1603SAlex Elder {
1447650d1603SAlex Elder 	/* Nothing to do */
1448650d1603SAlex Elder }
1449650d1603SAlex Elder 
1450650d1603SAlex Elder /* Inverse of gsi_evt_ring_setup() */
1451650d1603SAlex Elder static void gsi_evt_ring_teardown(struct gsi *gsi)
1452650d1603SAlex Elder {
1453650d1603SAlex Elder 	/* Nothing to do */
1454650d1603SAlex Elder }
1455650d1603SAlex Elder 
1456650d1603SAlex Elder /* Setup function for a single channel */
1457650d1603SAlex Elder static int gsi_channel_setup_one(struct gsi *gsi, u32 channel_id,
1458f86a1909SAlex Elder 				 bool legacy)
1459650d1603SAlex Elder {
1460650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
1461650d1603SAlex Elder 	u32 evt_ring_id = channel->evt_ring_id;
1462650d1603SAlex Elder 	int ret;
1463650d1603SAlex Elder 
1464650d1603SAlex Elder 	if (!channel->gsi)
1465650d1603SAlex Elder 		return 0;	/* Ignore uninitialized channels */
1466650d1603SAlex Elder 
1467650d1603SAlex Elder 	ret = gsi_evt_ring_alloc_command(gsi, evt_ring_id);
1468650d1603SAlex Elder 	if (ret)
1469650d1603SAlex Elder 		return ret;
1470650d1603SAlex Elder 
1471650d1603SAlex Elder 	gsi_evt_ring_program(gsi, evt_ring_id);
1472650d1603SAlex Elder 
1473650d1603SAlex Elder 	ret = gsi_channel_alloc_command(gsi, channel_id);
1474650d1603SAlex Elder 	if (ret)
1475650d1603SAlex Elder 		goto err_evt_ring_de_alloc;
1476650d1603SAlex Elder 
1477f86a1909SAlex Elder 	gsi_channel_program(channel, legacy);
1478650d1603SAlex Elder 
1479650d1603SAlex Elder 	if (channel->toward_ipa)
1480650d1603SAlex Elder 		netif_tx_napi_add(&gsi->dummy_dev, &channel->napi,
1481650d1603SAlex Elder 				  gsi_channel_poll, NAPI_POLL_WEIGHT);
1482650d1603SAlex Elder 	else
1483650d1603SAlex Elder 		netif_napi_add(&gsi->dummy_dev, &channel->napi,
1484650d1603SAlex Elder 			       gsi_channel_poll, NAPI_POLL_WEIGHT);
1485650d1603SAlex Elder 
1486650d1603SAlex Elder 	return 0;
1487650d1603SAlex Elder 
1488650d1603SAlex Elder err_evt_ring_de_alloc:
1489650d1603SAlex Elder 	/* We've done nothing with the event ring yet so don't reset */
1490650d1603SAlex Elder 	gsi_evt_ring_de_alloc_command(gsi, evt_ring_id);
1491650d1603SAlex Elder 
1492650d1603SAlex Elder 	return ret;
1493650d1603SAlex Elder }
1494650d1603SAlex Elder 
1495650d1603SAlex Elder /* Inverse of gsi_channel_setup_one() */
1496650d1603SAlex Elder static void gsi_channel_teardown_one(struct gsi *gsi, u32 channel_id)
1497650d1603SAlex Elder {
1498650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
1499650d1603SAlex Elder 	u32 evt_ring_id = channel->evt_ring_id;
1500650d1603SAlex Elder 
1501650d1603SAlex Elder 	if (!channel->gsi)
1502650d1603SAlex Elder 		return;		/* Ignore uninitialized channels */
1503650d1603SAlex Elder 
1504650d1603SAlex Elder 	netif_napi_del(&channel->napi);
1505650d1603SAlex Elder 
1506650d1603SAlex Elder 	gsi_channel_deprogram(channel);
1507650d1603SAlex Elder 	gsi_channel_de_alloc_command(gsi, channel_id);
1508650d1603SAlex Elder 	gsi_evt_ring_reset_command(gsi, evt_ring_id);
1509650d1603SAlex Elder 	gsi_evt_ring_de_alloc_command(gsi, evt_ring_id);
1510650d1603SAlex Elder }
1511650d1603SAlex Elder 
1512650d1603SAlex Elder static int gsi_generic_command(struct gsi *gsi, u32 channel_id,
1513650d1603SAlex Elder 			       enum gsi_generic_cmd_opcode opcode)
1514650d1603SAlex Elder {
1515650d1603SAlex Elder 	struct completion *completion = &gsi->completion;
1516650d1603SAlex Elder 	u32 val;
1517650d1603SAlex Elder 
15180b1ba18aSAlex Elder 	/* First zero the result code field */
15190b1ba18aSAlex Elder 	val = ioread32(gsi->virt + GSI_CNTXT_SCRATCH_0_OFFSET);
15200b1ba18aSAlex Elder 	val &= ~GENERIC_EE_RESULT_FMASK;
15210b1ba18aSAlex Elder 	iowrite32(val, gsi->virt + GSI_CNTXT_SCRATCH_0_OFFSET);
15220b1ba18aSAlex Elder 
15230b1ba18aSAlex Elder 	/* Now issue the command */
1524650d1603SAlex Elder 	val = u32_encode_bits(opcode, GENERIC_OPCODE_FMASK);
1525650d1603SAlex Elder 	val |= u32_encode_bits(channel_id, GENERIC_CHID_FMASK);
1526650d1603SAlex Elder 	val |= u32_encode_bits(GSI_EE_MODEM, GENERIC_EE_FMASK);
1527650d1603SAlex Elder 
1528650d1603SAlex Elder 	if (gsi_command(gsi, GSI_GENERIC_CMD_OFFSET, val, completion))
1529650d1603SAlex Elder 		return 0;	/* Success! */
1530650d1603SAlex Elder 
1531650d1603SAlex Elder 	dev_err(gsi->dev, "GSI generic command %u to channel %u timed out\n",
1532650d1603SAlex Elder 		opcode, channel_id);
1533650d1603SAlex Elder 
1534650d1603SAlex Elder 	return -ETIMEDOUT;
1535650d1603SAlex Elder }
1536650d1603SAlex Elder 
1537650d1603SAlex Elder static int gsi_modem_channel_alloc(struct gsi *gsi, u32 channel_id)
1538650d1603SAlex Elder {
1539650d1603SAlex Elder 	return gsi_generic_command(gsi, channel_id,
1540650d1603SAlex Elder 				   GSI_GENERIC_ALLOCATE_CHANNEL);
1541650d1603SAlex Elder }
1542650d1603SAlex Elder 
1543650d1603SAlex Elder static void gsi_modem_channel_halt(struct gsi *gsi, u32 channel_id)
1544650d1603SAlex Elder {
1545650d1603SAlex Elder 	int ret;
1546650d1603SAlex Elder 
1547650d1603SAlex Elder 	ret = gsi_generic_command(gsi, channel_id, GSI_GENERIC_HALT_CHANNEL);
1548650d1603SAlex Elder 	if (ret)
1549650d1603SAlex Elder 		dev_err(gsi->dev, "error %d halting modem channel %u\n",
1550650d1603SAlex Elder 			ret, channel_id);
1551650d1603SAlex Elder }
1552650d1603SAlex Elder 
1553650d1603SAlex Elder /* Setup function for channels */
1554f86a1909SAlex Elder static int gsi_channel_setup(struct gsi *gsi, bool legacy)
1555650d1603SAlex Elder {
1556650d1603SAlex Elder 	u32 channel_id = 0;
1557650d1603SAlex Elder 	u32 mask;
1558650d1603SAlex Elder 	int ret;
1559650d1603SAlex Elder 
1560650d1603SAlex Elder 	gsi_evt_ring_setup(gsi);
1561650d1603SAlex Elder 	gsi_irq_enable(gsi);
1562650d1603SAlex Elder 
1563650d1603SAlex Elder 	mutex_lock(&gsi->mutex);
1564650d1603SAlex Elder 
1565650d1603SAlex Elder 	do {
1566f86a1909SAlex Elder 		ret = gsi_channel_setup_one(gsi, channel_id, legacy);
1567650d1603SAlex Elder 		if (ret)
1568650d1603SAlex Elder 			goto err_unwind;
1569650d1603SAlex Elder 	} while (++channel_id < gsi->channel_count);
1570650d1603SAlex Elder 
1571650d1603SAlex Elder 	/* Make sure no channels were defined that hardware does not support */
1572650d1603SAlex Elder 	while (channel_id < GSI_CHANNEL_COUNT_MAX) {
1573650d1603SAlex Elder 		struct gsi_channel *channel = &gsi->channel[channel_id++];
1574650d1603SAlex Elder 
1575650d1603SAlex Elder 		if (!channel->gsi)
1576650d1603SAlex Elder 			continue;	/* Ignore uninitialized channels */
1577650d1603SAlex Elder 
1578650d1603SAlex Elder 		dev_err(gsi->dev, "channel %u not supported by hardware\n",
1579650d1603SAlex Elder 			channel_id - 1);
1580650d1603SAlex Elder 		channel_id = gsi->channel_count;
1581650d1603SAlex Elder 		goto err_unwind;
1582650d1603SAlex Elder 	}
1583650d1603SAlex Elder 
1584650d1603SAlex Elder 	/* Allocate modem channels if necessary */
1585650d1603SAlex Elder 	mask = gsi->modem_channel_bitmap;
1586650d1603SAlex Elder 	while (mask) {
1587650d1603SAlex Elder 		u32 modem_channel_id = __ffs(mask);
1588650d1603SAlex Elder 
1589650d1603SAlex Elder 		ret = gsi_modem_channel_alloc(gsi, modem_channel_id);
1590650d1603SAlex Elder 		if (ret)
1591650d1603SAlex Elder 			goto err_unwind_modem;
1592650d1603SAlex Elder 
1593650d1603SAlex Elder 		/* Clear bit from mask only after success (for unwind) */
1594650d1603SAlex Elder 		mask ^= BIT(modem_channel_id);
1595650d1603SAlex Elder 	}
1596650d1603SAlex Elder 
1597650d1603SAlex Elder 	mutex_unlock(&gsi->mutex);
1598650d1603SAlex Elder 
1599650d1603SAlex Elder 	return 0;
1600650d1603SAlex Elder 
1601650d1603SAlex Elder err_unwind_modem:
1602650d1603SAlex Elder 	/* Compute which modem channels need to be deallocated */
1603650d1603SAlex Elder 	mask ^= gsi->modem_channel_bitmap;
1604650d1603SAlex Elder 	while (mask) {
1605993cac15SAlex Elder 		channel_id = __fls(mask);
1606650d1603SAlex Elder 
1607650d1603SAlex Elder 		mask ^= BIT(channel_id);
1608650d1603SAlex Elder 
1609650d1603SAlex Elder 		gsi_modem_channel_halt(gsi, channel_id);
1610650d1603SAlex Elder 	}
1611650d1603SAlex Elder 
1612650d1603SAlex Elder err_unwind:
1613650d1603SAlex Elder 	while (channel_id--)
1614650d1603SAlex Elder 		gsi_channel_teardown_one(gsi, channel_id);
1615650d1603SAlex Elder 
1616650d1603SAlex Elder 	mutex_unlock(&gsi->mutex);
1617650d1603SAlex Elder 
1618650d1603SAlex Elder 	gsi_irq_disable(gsi);
1619650d1603SAlex Elder 	gsi_evt_ring_teardown(gsi);
1620650d1603SAlex Elder 
1621650d1603SAlex Elder 	return ret;
1622650d1603SAlex Elder }
1623650d1603SAlex Elder 
1624650d1603SAlex Elder /* Inverse of gsi_channel_setup() */
1625650d1603SAlex Elder static void gsi_channel_teardown(struct gsi *gsi)
1626650d1603SAlex Elder {
1627650d1603SAlex Elder 	u32 mask = gsi->modem_channel_bitmap;
1628650d1603SAlex Elder 	u32 channel_id;
1629650d1603SAlex Elder 
1630650d1603SAlex Elder 	mutex_lock(&gsi->mutex);
1631650d1603SAlex Elder 
1632650d1603SAlex Elder 	while (mask) {
1633993cac15SAlex Elder 		channel_id = __fls(mask);
1634650d1603SAlex Elder 
1635650d1603SAlex Elder 		mask ^= BIT(channel_id);
1636650d1603SAlex Elder 
1637650d1603SAlex Elder 		gsi_modem_channel_halt(gsi, channel_id);
1638650d1603SAlex Elder 	}
1639650d1603SAlex Elder 
1640650d1603SAlex Elder 	channel_id = gsi->channel_count - 1;
1641650d1603SAlex Elder 	do
1642650d1603SAlex Elder 		gsi_channel_teardown_one(gsi, channel_id);
1643650d1603SAlex Elder 	while (channel_id--);
1644650d1603SAlex Elder 
1645650d1603SAlex Elder 	mutex_unlock(&gsi->mutex);
1646650d1603SAlex Elder 
1647650d1603SAlex Elder 	gsi_irq_disable(gsi);
1648650d1603SAlex Elder 	gsi_evt_ring_teardown(gsi);
1649650d1603SAlex Elder }
1650650d1603SAlex Elder 
1651650d1603SAlex Elder /* Setup function for GSI.  GSI firmware must be loaded and initialized */
1652f86a1909SAlex Elder int gsi_setup(struct gsi *gsi, bool legacy)
1653650d1603SAlex Elder {
16548463488aSAlex Elder 	struct device *dev = gsi->dev;
1655650d1603SAlex Elder 	u32 val;
1656650d1603SAlex Elder 
1657650d1603SAlex Elder 	/* Here is where we first touch the GSI hardware */
1658650d1603SAlex Elder 	val = ioread32(gsi->virt + GSI_GSI_STATUS_OFFSET);
1659650d1603SAlex Elder 	if (!(val & ENABLED_FMASK)) {
16608463488aSAlex Elder 		dev_err(dev, "GSI has not been enabled\n");
1661650d1603SAlex Elder 		return -EIO;
1662650d1603SAlex Elder 	}
1663650d1603SAlex Elder 
1664650d1603SAlex Elder 	val = ioread32(gsi->virt + GSI_GSI_HW_PARAM_2_OFFSET);
1665650d1603SAlex Elder 
1666650d1603SAlex Elder 	gsi->channel_count = u32_get_bits(val, NUM_CH_PER_EE_FMASK);
1667650d1603SAlex Elder 	if (!gsi->channel_count) {
16688463488aSAlex Elder 		dev_err(dev, "GSI reports zero channels supported\n");
1669650d1603SAlex Elder 		return -EINVAL;
1670650d1603SAlex Elder 	}
1671650d1603SAlex Elder 	if (gsi->channel_count > GSI_CHANNEL_COUNT_MAX) {
16728463488aSAlex Elder 		dev_warn(dev,
16738463488aSAlex Elder 			 "limiting to %u channels; hardware supports %u\n",
1674650d1603SAlex Elder 			 GSI_CHANNEL_COUNT_MAX, gsi->channel_count);
1675650d1603SAlex Elder 		gsi->channel_count = GSI_CHANNEL_COUNT_MAX;
1676650d1603SAlex Elder 	}
1677650d1603SAlex Elder 
1678650d1603SAlex Elder 	gsi->evt_ring_count = u32_get_bits(val, NUM_EV_PER_EE_FMASK);
1679650d1603SAlex Elder 	if (!gsi->evt_ring_count) {
16808463488aSAlex Elder 		dev_err(dev, "GSI reports zero event rings supported\n");
1681650d1603SAlex Elder 		return -EINVAL;
1682650d1603SAlex Elder 	}
1683650d1603SAlex Elder 	if (gsi->evt_ring_count > GSI_EVT_RING_COUNT_MAX) {
16848463488aSAlex Elder 		dev_warn(dev,
16858463488aSAlex Elder 			 "limiting to %u event rings; hardware supports %u\n",
1686650d1603SAlex Elder 			 GSI_EVT_RING_COUNT_MAX, gsi->evt_ring_count);
1687650d1603SAlex Elder 		gsi->evt_ring_count = GSI_EVT_RING_COUNT_MAX;
1688650d1603SAlex Elder 	}
1689650d1603SAlex Elder 
1690650d1603SAlex Elder 	/* Initialize the error log */
1691650d1603SAlex Elder 	iowrite32(0, gsi->virt + GSI_ERROR_LOG_OFFSET);
1692650d1603SAlex Elder 
1693650d1603SAlex Elder 	/* Writing 1 indicates IRQ interrupts; 0 would be MSI */
1694650d1603SAlex Elder 	iowrite32(1, gsi->virt + GSI_CNTXT_INTSET_OFFSET);
1695650d1603SAlex Elder 
1696f86a1909SAlex Elder 	return gsi_channel_setup(gsi, legacy);
1697650d1603SAlex Elder }
1698650d1603SAlex Elder 
1699650d1603SAlex Elder /* Inverse of gsi_setup() */
1700650d1603SAlex Elder void gsi_teardown(struct gsi *gsi)
1701650d1603SAlex Elder {
1702650d1603SAlex Elder 	gsi_channel_teardown(gsi);
1703650d1603SAlex Elder }
1704650d1603SAlex Elder 
1705650d1603SAlex Elder /* Initialize a channel's event ring */
1706650d1603SAlex Elder static int gsi_channel_evt_ring_init(struct gsi_channel *channel)
1707650d1603SAlex Elder {
1708650d1603SAlex Elder 	struct gsi *gsi = channel->gsi;
1709650d1603SAlex Elder 	struct gsi_evt_ring *evt_ring;
1710650d1603SAlex Elder 	int ret;
1711650d1603SAlex Elder 
1712650d1603SAlex Elder 	ret = gsi_evt_ring_id_alloc(gsi);
1713650d1603SAlex Elder 	if (ret < 0)
1714650d1603SAlex Elder 		return ret;
1715650d1603SAlex Elder 	channel->evt_ring_id = ret;
1716650d1603SAlex Elder 
1717650d1603SAlex Elder 	evt_ring = &gsi->evt_ring[channel->evt_ring_id];
1718650d1603SAlex Elder 	evt_ring->channel = channel;
1719650d1603SAlex Elder 
1720650d1603SAlex Elder 	ret = gsi_ring_alloc(gsi, &evt_ring->ring, channel->event_count);
1721650d1603SAlex Elder 	if (!ret)
1722650d1603SAlex Elder 		return 0;	/* Success! */
1723650d1603SAlex Elder 
1724650d1603SAlex Elder 	dev_err(gsi->dev, "error %d allocating channel %u event ring\n",
1725650d1603SAlex Elder 		ret, gsi_channel_id(channel));
1726650d1603SAlex Elder 
1727650d1603SAlex Elder 	gsi_evt_ring_id_free(gsi, channel->evt_ring_id);
1728650d1603SAlex Elder 
1729650d1603SAlex Elder 	return ret;
1730650d1603SAlex Elder }
1731650d1603SAlex Elder 
1732650d1603SAlex Elder /* Inverse of gsi_channel_evt_ring_init() */
1733650d1603SAlex Elder static void gsi_channel_evt_ring_exit(struct gsi_channel *channel)
1734650d1603SAlex Elder {
1735650d1603SAlex Elder 	u32 evt_ring_id = channel->evt_ring_id;
1736650d1603SAlex Elder 	struct gsi *gsi = channel->gsi;
1737650d1603SAlex Elder 	struct gsi_evt_ring *evt_ring;
1738650d1603SAlex Elder 
1739650d1603SAlex Elder 	evt_ring = &gsi->evt_ring[evt_ring_id];
1740650d1603SAlex Elder 	gsi_ring_free(gsi, &evt_ring->ring);
1741650d1603SAlex Elder 	gsi_evt_ring_id_free(gsi, evt_ring_id);
1742650d1603SAlex Elder }
1743650d1603SAlex Elder 
1744650d1603SAlex Elder /* Init function for event rings */
1745650d1603SAlex Elder static void gsi_evt_ring_init(struct gsi *gsi)
1746650d1603SAlex Elder {
1747650d1603SAlex Elder 	u32 evt_ring_id = 0;
1748650d1603SAlex Elder 
1749650d1603SAlex Elder 	gsi->event_bitmap = gsi_event_bitmap_init(GSI_EVT_RING_COUNT_MAX);
1750650d1603SAlex Elder 	gsi->event_enable_bitmap = 0;
1751650d1603SAlex Elder 	do
1752650d1603SAlex Elder 		init_completion(&gsi->evt_ring[evt_ring_id].completion);
1753650d1603SAlex Elder 	while (++evt_ring_id < GSI_EVT_RING_COUNT_MAX);
1754650d1603SAlex Elder }
1755650d1603SAlex Elder 
1756650d1603SAlex Elder /* Inverse of gsi_evt_ring_init() */
1757650d1603SAlex Elder static void gsi_evt_ring_exit(struct gsi *gsi)
1758650d1603SAlex Elder {
1759650d1603SAlex Elder 	/* Nothing to do */
1760650d1603SAlex Elder }
1761650d1603SAlex Elder 
1762650d1603SAlex Elder static bool gsi_channel_data_valid(struct gsi *gsi,
1763650d1603SAlex Elder 				   const struct ipa_gsi_endpoint_data *data)
1764650d1603SAlex Elder {
1765650d1603SAlex Elder #ifdef IPA_VALIDATION
1766650d1603SAlex Elder 	u32 channel_id = data->channel_id;
1767650d1603SAlex Elder 	struct device *dev = gsi->dev;
1768650d1603SAlex Elder 
1769650d1603SAlex Elder 	/* Make sure channel ids are in the range driver supports */
1770650d1603SAlex Elder 	if (channel_id >= GSI_CHANNEL_COUNT_MAX) {
17718463488aSAlex Elder 		dev_err(dev, "bad channel id %u; must be less than %u\n",
1772650d1603SAlex Elder 			channel_id, GSI_CHANNEL_COUNT_MAX);
1773650d1603SAlex Elder 		return false;
1774650d1603SAlex Elder 	}
1775650d1603SAlex Elder 
1776650d1603SAlex Elder 	if (data->ee_id != GSI_EE_AP && data->ee_id != GSI_EE_MODEM) {
17778463488aSAlex Elder 		dev_err(dev, "bad EE id %u; not AP or modem\n", data->ee_id);
1778650d1603SAlex Elder 		return false;
1779650d1603SAlex Elder 	}
1780650d1603SAlex Elder 
1781650d1603SAlex Elder 	if (!data->channel.tlv_count ||
1782650d1603SAlex Elder 	    data->channel.tlv_count > GSI_TLV_MAX) {
17838463488aSAlex Elder 		dev_err(dev, "channel %u bad tlv_count %u; must be 1..%u\n",
1784650d1603SAlex Elder 			channel_id, data->channel.tlv_count, GSI_TLV_MAX);
1785650d1603SAlex Elder 		return false;
1786650d1603SAlex Elder 	}
1787650d1603SAlex Elder 
1788650d1603SAlex Elder 	/* We have to allow at least one maximally-sized transaction to
1789650d1603SAlex Elder 	 * be outstanding (which would use tlv_count TREs).  Given how
1790650d1603SAlex Elder 	 * gsi_channel_tre_max() is computed, tre_count has to be almost
1791650d1603SAlex Elder 	 * twice the TLV FIFO size to satisfy this requirement.
1792650d1603SAlex Elder 	 */
1793650d1603SAlex Elder 	if (data->channel.tre_count < 2 * data->channel.tlv_count - 1) {
1794650d1603SAlex Elder 		dev_err(dev, "channel %u TLV count %u exceeds TRE count %u\n",
1795650d1603SAlex Elder 			channel_id, data->channel.tlv_count,
1796650d1603SAlex Elder 			data->channel.tre_count);
1797650d1603SAlex Elder 		return false;
1798650d1603SAlex Elder 	}
1799650d1603SAlex Elder 
1800650d1603SAlex Elder 	if (!is_power_of_2(data->channel.tre_count)) {
18018463488aSAlex Elder 		dev_err(dev, "channel %u bad tre_count %u; not power of 2\n",
1802650d1603SAlex Elder 			channel_id, data->channel.tre_count);
1803650d1603SAlex Elder 		return false;
1804650d1603SAlex Elder 	}
1805650d1603SAlex Elder 
1806650d1603SAlex Elder 	if (!is_power_of_2(data->channel.event_count)) {
18078463488aSAlex Elder 		dev_err(dev, "channel %u bad event_count %u; not power of 2\n",
1808650d1603SAlex Elder 			channel_id, data->channel.event_count);
1809650d1603SAlex Elder 		return false;
1810650d1603SAlex Elder 	}
1811650d1603SAlex Elder #endif /* IPA_VALIDATION */
1812650d1603SAlex Elder 
1813650d1603SAlex Elder 	return true;
1814650d1603SAlex Elder }
1815650d1603SAlex Elder 
1816650d1603SAlex Elder /* Init function for a single channel */
1817650d1603SAlex Elder static int gsi_channel_init_one(struct gsi *gsi,
1818650d1603SAlex Elder 				const struct ipa_gsi_endpoint_data *data,
1819*14dbf977SAlex Elder 				bool command)
1820650d1603SAlex Elder {
1821650d1603SAlex Elder 	struct gsi_channel *channel;
1822650d1603SAlex Elder 	u32 tre_count;
1823650d1603SAlex Elder 	int ret;
1824650d1603SAlex Elder 
1825650d1603SAlex Elder 	if (!gsi_channel_data_valid(gsi, data))
1826650d1603SAlex Elder 		return -EINVAL;
1827650d1603SAlex Elder 
1828650d1603SAlex Elder 	/* Worst case we need an event for every outstanding TRE */
1829650d1603SAlex Elder 	if (data->channel.tre_count > data->channel.event_count) {
1830650d1603SAlex Elder 		tre_count = data->channel.event_count;
18310721999fSAlex Elder 		dev_warn(gsi->dev, "channel %u limited to %u TREs\n",
18320721999fSAlex Elder 			 data->channel_id, tre_count);
1833650d1603SAlex Elder 	} else {
1834650d1603SAlex Elder 		tre_count = data->channel.tre_count;
1835650d1603SAlex Elder 	}
1836650d1603SAlex Elder 
1837650d1603SAlex Elder 	channel = &gsi->channel[data->channel_id];
1838650d1603SAlex Elder 	memset(channel, 0, sizeof(*channel));
1839650d1603SAlex Elder 
1840650d1603SAlex Elder 	channel->gsi = gsi;
1841650d1603SAlex Elder 	channel->toward_ipa = data->toward_ipa;
1842650d1603SAlex Elder 	channel->command = command;
1843650d1603SAlex Elder 	channel->tlv_count = data->channel.tlv_count;
1844650d1603SAlex Elder 	channel->tre_count = tre_count;
1845650d1603SAlex Elder 	channel->event_count = data->channel.event_count;
1846650d1603SAlex Elder 	init_completion(&channel->completion);
1847650d1603SAlex Elder 
1848650d1603SAlex Elder 	ret = gsi_channel_evt_ring_init(channel);
1849650d1603SAlex Elder 	if (ret)
1850650d1603SAlex Elder 		goto err_clear_gsi;
1851650d1603SAlex Elder 
1852650d1603SAlex Elder 	ret = gsi_ring_alloc(gsi, &channel->tre_ring, data->channel.tre_count);
1853650d1603SAlex Elder 	if (ret) {
1854650d1603SAlex Elder 		dev_err(gsi->dev, "error %d allocating channel %u ring\n",
1855650d1603SAlex Elder 			ret, data->channel_id);
1856650d1603SAlex Elder 		goto err_channel_evt_ring_exit;
1857650d1603SAlex Elder 	}
1858650d1603SAlex Elder 
1859650d1603SAlex Elder 	ret = gsi_channel_trans_init(gsi, data->channel_id);
1860650d1603SAlex Elder 	if (ret)
1861650d1603SAlex Elder 		goto err_ring_free;
1862650d1603SAlex Elder 
1863650d1603SAlex Elder 	if (command) {
1864650d1603SAlex Elder 		u32 tre_max = gsi_channel_tre_max(gsi, data->channel_id);
1865650d1603SAlex Elder 
1866650d1603SAlex Elder 		ret = ipa_cmd_pool_init(channel, tre_max);
1867650d1603SAlex Elder 	}
1868650d1603SAlex Elder 	if (!ret)
1869650d1603SAlex Elder 		return 0;	/* Success! */
1870650d1603SAlex Elder 
1871650d1603SAlex Elder 	gsi_channel_trans_exit(channel);
1872650d1603SAlex Elder err_ring_free:
1873650d1603SAlex Elder 	gsi_ring_free(gsi, &channel->tre_ring);
1874650d1603SAlex Elder err_channel_evt_ring_exit:
1875650d1603SAlex Elder 	gsi_channel_evt_ring_exit(channel);
1876650d1603SAlex Elder err_clear_gsi:
1877650d1603SAlex Elder 	channel->gsi = NULL;	/* Mark it not (fully) initialized */
1878650d1603SAlex Elder 
1879650d1603SAlex Elder 	return ret;
1880650d1603SAlex Elder }
1881650d1603SAlex Elder 
1882650d1603SAlex Elder /* Inverse of gsi_channel_init_one() */
1883650d1603SAlex Elder static void gsi_channel_exit_one(struct gsi_channel *channel)
1884650d1603SAlex Elder {
1885650d1603SAlex Elder 	if (!channel->gsi)
1886650d1603SAlex Elder 		return;		/* Ignore uninitialized channels */
1887650d1603SAlex Elder 
1888650d1603SAlex Elder 	if (channel->command)
1889650d1603SAlex Elder 		ipa_cmd_pool_exit(channel);
1890650d1603SAlex Elder 	gsi_channel_trans_exit(channel);
1891650d1603SAlex Elder 	gsi_ring_free(channel->gsi, &channel->tre_ring);
1892650d1603SAlex Elder 	gsi_channel_evt_ring_exit(channel);
1893650d1603SAlex Elder }
1894650d1603SAlex Elder 
1895650d1603SAlex Elder /* Init function for channels */
1896*14dbf977SAlex Elder static int gsi_channel_init(struct gsi *gsi, u32 count,
1897650d1603SAlex Elder 			    const struct ipa_gsi_endpoint_data *data,
1898650d1603SAlex Elder 			    bool modem_alloc)
1899650d1603SAlex Elder {
1900650d1603SAlex Elder 	int ret = 0;
1901650d1603SAlex Elder 	u32 i;
1902650d1603SAlex Elder 
1903650d1603SAlex Elder 	gsi_evt_ring_init(gsi);
1904650d1603SAlex Elder 
1905650d1603SAlex Elder 	/* The endpoint data array is indexed by endpoint name */
1906650d1603SAlex Elder 	for (i = 0; i < count; i++) {
1907650d1603SAlex Elder 		bool command = i == IPA_ENDPOINT_AP_COMMAND_TX;
1908650d1603SAlex Elder 
1909650d1603SAlex Elder 		if (ipa_gsi_endpoint_data_empty(&data[i]))
1910650d1603SAlex Elder 			continue;	/* Skip over empty slots */
1911650d1603SAlex Elder 
1912650d1603SAlex Elder 		/* Mark modem channels to be allocated (hardware workaround) */
1913650d1603SAlex Elder 		if (data[i].ee_id == GSI_EE_MODEM) {
1914650d1603SAlex Elder 			if (modem_alloc)
1915650d1603SAlex Elder 				gsi->modem_channel_bitmap |=
1916650d1603SAlex Elder 						BIT(data[i].channel_id);
1917650d1603SAlex Elder 			continue;
1918650d1603SAlex Elder 		}
1919650d1603SAlex Elder 
1920*14dbf977SAlex Elder 		ret = gsi_channel_init_one(gsi, &data[i], command);
1921650d1603SAlex Elder 		if (ret)
1922650d1603SAlex Elder 			goto err_unwind;
1923650d1603SAlex Elder 	}
1924650d1603SAlex Elder 
1925650d1603SAlex Elder 	return ret;
1926650d1603SAlex Elder 
1927650d1603SAlex Elder err_unwind:
1928650d1603SAlex Elder 	while (i--) {
1929650d1603SAlex Elder 		if (ipa_gsi_endpoint_data_empty(&data[i]))
1930650d1603SAlex Elder 			continue;
1931650d1603SAlex Elder 		if (modem_alloc && data[i].ee_id == GSI_EE_MODEM) {
1932650d1603SAlex Elder 			gsi->modem_channel_bitmap &= ~BIT(data[i].channel_id);
1933650d1603SAlex Elder 			continue;
1934650d1603SAlex Elder 		}
1935650d1603SAlex Elder 		gsi_channel_exit_one(&gsi->channel[data->channel_id]);
1936650d1603SAlex Elder 	}
1937650d1603SAlex Elder 	gsi_evt_ring_exit(gsi);
1938650d1603SAlex Elder 
1939650d1603SAlex Elder 	return ret;
1940650d1603SAlex Elder }
1941650d1603SAlex Elder 
1942650d1603SAlex Elder /* Inverse of gsi_channel_init() */
1943650d1603SAlex Elder static void gsi_channel_exit(struct gsi *gsi)
1944650d1603SAlex Elder {
1945650d1603SAlex Elder 	u32 channel_id = GSI_CHANNEL_COUNT_MAX - 1;
1946650d1603SAlex Elder 
1947650d1603SAlex Elder 	do
1948650d1603SAlex Elder 		gsi_channel_exit_one(&gsi->channel[channel_id]);
1949650d1603SAlex Elder 	while (channel_id--);
1950650d1603SAlex Elder 	gsi->modem_channel_bitmap = 0;
1951650d1603SAlex Elder 
1952650d1603SAlex Elder 	gsi_evt_ring_exit(gsi);
1953650d1603SAlex Elder }
1954650d1603SAlex Elder 
1955650d1603SAlex Elder /* Init function for GSI.  GSI hardware does not need to be "ready" */
19561d0c09deSAlex Elder int gsi_init(struct gsi *gsi, struct platform_device *pdev,
19571d0c09deSAlex Elder 	     enum ipa_version version, u32 count,
19581d0c09deSAlex Elder 	     const struct ipa_gsi_endpoint_data *data)
1959650d1603SAlex Elder {
19608463488aSAlex Elder 	struct device *dev = &pdev->dev;
1961650d1603SAlex Elder 	struct resource *res;
1962650d1603SAlex Elder 	resource_size_t size;
1963650d1603SAlex Elder 	unsigned int irq;
19641d0c09deSAlex Elder 	bool modem_alloc;
1965650d1603SAlex Elder 	int ret;
1966650d1603SAlex Elder 
1967650d1603SAlex Elder 	gsi_validate_build();
1968650d1603SAlex Elder 
19691d0c09deSAlex Elder 	/* IPA v4.2 requires the AP to allocate channels for the modem */
19701d0c09deSAlex Elder 	modem_alloc = version == IPA_VERSION_4_2;
19711d0c09deSAlex Elder 
19728463488aSAlex Elder 	gsi->dev = dev;
1973*14dbf977SAlex Elder 	gsi->version = version;
1974650d1603SAlex Elder 
1975650d1603SAlex Elder 	/* The GSI layer performs NAPI on all endpoints.  NAPI requires a
1976650d1603SAlex Elder 	 * network device structure, but the GSI layer does not have one,
1977650d1603SAlex Elder 	 * so we must create a dummy network device for this purpose.
1978650d1603SAlex Elder 	 */
1979650d1603SAlex Elder 	init_dummy_netdev(&gsi->dummy_dev);
1980650d1603SAlex Elder 
1981650d1603SAlex Elder 	ret = platform_get_irq_byname(pdev, "gsi");
1982650d1603SAlex Elder 	if (ret <= 0) {
19838463488aSAlex Elder 		dev_err(dev, "DT error %d getting \"gsi\" IRQ property\n", ret);
1984650d1603SAlex Elder 		return ret ? : -EINVAL;
1985650d1603SAlex Elder 	}
1986650d1603SAlex Elder 	irq = ret;
1987650d1603SAlex Elder 
1988650d1603SAlex Elder 	ret = request_irq(irq, gsi_isr, 0, "gsi", gsi);
1989650d1603SAlex Elder 	if (ret) {
19908463488aSAlex Elder 		dev_err(dev, "error %d requesting \"gsi\" IRQ\n", ret);
1991650d1603SAlex Elder 		return ret;
1992650d1603SAlex Elder 	}
1993650d1603SAlex Elder 	gsi->irq = irq;
1994650d1603SAlex Elder 
1995650d1603SAlex Elder 	/* Get GSI memory range and map it */
1996650d1603SAlex Elder 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "gsi");
1997650d1603SAlex Elder 	if (!res) {
19988463488aSAlex Elder 		dev_err(dev, "DT error getting \"gsi\" memory property\n");
1999650d1603SAlex Elder 		ret = -ENODEV;
200054f7e443SAlex Elder 		goto err_free_irq;
2001650d1603SAlex Elder 	}
2002650d1603SAlex Elder 
2003650d1603SAlex Elder 	size = resource_size(res);
2004650d1603SAlex Elder 	if (res->start > U32_MAX || size > U32_MAX - res->start) {
20058463488aSAlex Elder 		dev_err(dev, "DT memory resource \"gsi\" out of range\n");
2006650d1603SAlex Elder 		ret = -EINVAL;
200754f7e443SAlex Elder 		goto err_free_irq;
2008650d1603SAlex Elder 	}
2009650d1603SAlex Elder 
2010650d1603SAlex Elder 	gsi->virt = ioremap(res->start, size);
2011650d1603SAlex Elder 	if (!gsi->virt) {
20128463488aSAlex Elder 		dev_err(dev, "unable to remap \"gsi\" memory\n");
2013650d1603SAlex Elder 		ret = -ENOMEM;
201454f7e443SAlex Elder 		goto err_free_irq;
2015650d1603SAlex Elder 	}
2016650d1603SAlex Elder 
2017*14dbf977SAlex Elder 	ret = gsi_channel_init(gsi, count, data, modem_alloc);
2018650d1603SAlex Elder 	if (ret)
2019650d1603SAlex Elder 		goto err_iounmap;
2020650d1603SAlex Elder 
2021650d1603SAlex Elder 	mutex_init(&gsi->mutex);
2022650d1603SAlex Elder 	init_completion(&gsi->completion);
2023650d1603SAlex Elder 
2024650d1603SAlex Elder 	return 0;
2025650d1603SAlex Elder 
2026650d1603SAlex Elder err_iounmap:
2027650d1603SAlex Elder 	iounmap(gsi->virt);
202854f7e443SAlex Elder err_free_irq:
2029650d1603SAlex Elder 	free_irq(gsi->irq, gsi);
2030650d1603SAlex Elder 
2031650d1603SAlex Elder 	return ret;
2032650d1603SAlex Elder }
2033650d1603SAlex Elder 
2034650d1603SAlex Elder /* Inverse of gsi_init() */
2035650d1603SAlex Elder void gsi_exit(struct gsi *gsi)
2036650d1603SAlex Elder {
2037650d1603SAlex Elder 	mutex_destroy(&gsi->mutex);
2038650d1603SAlex Elder 	gsi_channel_exit(gsi);
2039650d1603SAlex Elder 	free_irq(gsi->irq, gsi);
2040650d1603SAlex Elder 	iounmap(gsi->virt);
2041650d1603SAlex Elder }
2042650d1603SAlex Elder 
2043650d1603SAlex Elder /* The maximum number of outstanding TREs on a channel.  This limits
2044650d1603SAlex Elder  * a channel's maximum number of transactions outstanding (worst case
2045650d1603SAlex Elder  * is one TRE per transaction).
2046650d1603SAlex Elder  *
2047650d1603SAlex Elder  * The absolute limit is the number of TREs in the channel's TRE ring,
2048650d1603SAlex Elder  * and in theory we should be able use all of them.  But in practice,
2049650d1603SAlex Elder  * doing that led to the hardware reporting exhaustion of event ring
2050650d1603SAlex Elder  * slots for writing completion information.  So the hardware limit
2051650d1603SAlex Elder  * would be (tre_count - 1).
2052650d1603SAlex Elder  *
2053650d1603SAlex Elder  * We reduce it a bit further though.  Transaction resource pools are
2054650d1603SAlex Elder  * sized to be a little larger than this maximum, to allow resource
2055650d1603SAlex Elder  * allocations to always be contiguous.  The number of entries in a
2056650d1603SAlex Elder  * TRE ring buffer is a power of 2, and the extra resources in a pool
2057650d1603SAlex Elder  * tends to nearly double the memory allocated for it.  Reducing the
2058650d1603SAlex Elder  * maximum number of outstanding TREs allows the number of entries in
2059650d1603SAlex Elder  * a pool to avoid crossing that power-of-2 boundary, and this can
2060650d1603SAlex Elder  * substantially reduce pool memory requirements.  The number we
2061650d1603SAlex Elder  * reduce it by matches the number added in gsi_trans_pool_init().
2062650d1603SAlex Elder  */
2063650d1603SAlex Elder u32 gsi_channel_tre_max(struct gsi *gsi, u32 channel_id)
2064650d1603SAlex Elder {
2065650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
2066650d1603SAlex Elder 
2067650d1603SAlex Elder 	/* Hardware limit is channel->tre_count - 1 */
2068650d1603SAlex Elder 	return channel->tre_count - (channel->tlv_count - 1);
2069650d1603SAlex Elder }
2070650d1603SAlex Elder 
2071650d1603SAlex Elder /* Returns the maximum number of TREs in a single transaction for a channel */
2072650d1603SAlex Elder u32 gsi_channel_trans_tre_max(struct gsi *gsi, u32 channel_id)
2073650d1603SAlex Elder {
2074650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
2075650d1603SAlex Elder 
2076650d1603SAlex Elder 	return channel->tlv_count;
2077650d1603SAlex Elder }
2078