xref: /openbmc/linux/drivers/net/ipa/gsi.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1650d1603SAlex Elder // SPDX-License-Identifier: GPL-2.0
2650d1603SAlex Elder 
3650d1603SAlex Elder /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
43c506addSAlex Elder  * Copyright (C) 2018-2023 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"
19d2bb6e65SAlex Elder #include "reg.h"
20650d1603SAlex Elder #include "gsi_reg.h"
21650d1603SAlex Elder #include "gsi_private.h"
22650d1603SAlex Elder #include "gsi_trans.h"
23650d1603SAlex Elder #include "ipa_gsi.h"
24650d1603SAlex Elder #include "ipa_data.h"
251d0c09deSAlex Elder #include "ipa_version.h"
26650d1603SAlex Elder 
27650d1603SAlex Elder /**
28650d1603SAlex Elder  * DOC: The IPA Generic Software Interface
29650d1603SAlex Elder  *
30650d1603SAlex Elder  * The generic software interface (GSI) is an integral component of the IPA,
31650d1603SAlex Elder  * providing a well-defined communication layer between the AP subsystem
32650d1603SAlex Elder  * and the IPA core.  The modem uses the GSI layer as well.
33650d1603SAlex Elder  *
34650d1603SAlex Elder  *	--------	     ---------
35650d1603SAlex Elder  *	|      |	     |	     |
36650d1603SAlex Elder  *	|  AP  +<---.	.----+ Modem |
37650d1603SAlex Elder  *	|      +--. |	| .->+	     |
38650d1603SAlex Elder  *	|      |  | |	| |  |	     |
39650d1603SAlex Elder  *	--------  | |	| |  ---------
40650d1603SAlex Elder  *		  v |	v |
41650d1603SAlex Elder  *		--+-+---+-+--
42650d1603SAlex Elder  *		|    GSI    |
43650d1603SAlex Elder  *		|-----------|
44650d1603SAlex Elder  *		|	    |
45650d1603SAlex Elder  *		|    IPA    |
46650d1603SAlex Elder  *		|	    |
47650d1603SAlex Elder  *		-------------
48650d1603SAlex Elder  *
49650d1603SAlex Elder  * In the above diagram, the AP and Modem represent "execution environments"
50650d1603SAlex Elder  * (EEs), which are independent operating environments that use the IPA for
51650d1603SAlex Elder  * data transfer.
52650d1603SAlex Elder  *
53650d1603SAlex Elder  * Each EE uses a set of unidirectional GSI "channels," which allow transfer
54650d1603SAlex Elder  * of data to or from the IPA.  A channel is implemented as a ring buffer,
55650d1603SAlex Elder  * with a DRAM-resident array of "transfer elements" (TREs) available to
56650d1603SAlex Elder  * describe transfers to or from other EEs through the IPA.  A transfer
57650d1603SAlex Elder  * element can also contain an immediate command, requesting the IPA perform
58650d1603SAlex Elder  * actions other than data transfer.
59650d1603SAlex Elder  *
60ace5dc61SAlex Elder  * Each TRE refers to a block of data--also located in DRAM.  After writing
61ace5dc61SAlex Elder  * one or more TREs to a channel, the writer (either the IPA or an EE) writes
62ace5dc61SAlex Elder  * a doorbell register to inform the receiving side how many elements have
63650d1603SAlex Elder  * been written.
64650d1603SAlex Elder  *
65650d1603SAlex Elder  * Each channel has a GSI "event ring" associated with it.  An event ring
66650d1603SAlex Elder  * is implemented very much like a channel ring, but is always directed from
67650d1603SAlex Elder  * the IPA to an EE.  The IPA notifies an EE (such as the AP) about channel
68650d1603SAlex Elder  * events by adding an entry to the event ring associated with the channel.
69650d1603SAlex Elder  * The GSI then writes its doorbell for the event ring, causing the target
70650d1603SAlex Elder  * EE to be interrupted.  Each entry in an event ring contains a pointer
71650d1603SAlex Elder  * to the channel TRE whose completion the event represents.
72650d1603SAlex Elder  *
73650d1603SAlex Elder  * Each TRE in a channel ring has a set of flags.  One flag indicates whether
74650d1603SAlex Elder  * the completion of the transfer operation generates an entry (and possibly
75650d1603SAlex Elder  * an interrupt) in the channel's event ring.  Other flags allow transfer
76650d1603SAlex Elder  * elements to be chained together, forming a single logical transaction.
77650d1603SAlex Elder  * TRE flags are used to control whether and when interrupts are generated
78650d1603SAlex Elder  * to signal completion of channel transfers.
79650d1603SAlex Elder  *
80650d1603SAlex Elder  * Elements in channel and event rings are completed (or consumed) strictly
81650d1603SAlex Elder  * in order.  Completion of one entry implies the completion of all preceding
82650d1603SAlex Elder  * entries.  A single completion interrupt can therefore communicate the
83650d1603SAlex Elder  * completion of many transfers.
84650d1603SAlex Elder  *
85650d1603SAlex Elder  * Note that all GSI registers are little-endian, which is the assumed
86650d1603SAlex Elder  * endianness of I/O space accesses.  The accessor functions perform byte
87650d1603SAlex Elder  * swapping if needed (i.e., for a big endian CPU).
88650d1603SAlex Elder  */
89650d1603SAlex Elder 
90650d1603SAlex Elder /* Delay period for interrupt moderation (in 32KHz IPA internal timer ticks) */
91650d1603SAlex Elder #define GSI_EVT_RING_INT_MODT		(32 * 1) /* 1ms under 32KHz clock */
92650d1603SAlex Elder 
9359b5f454SAlex Elder #define GSI_CMD_TIMEOUT			50	/* milliseconds */
94650d1603SAlex Elder 
95057ef63fSAlex Elder #define GSI_CHANNEL_STOP_RETRIES	10
9611361456SAlex Elder #define GSI_CHANNEL_MODEM_HALT_RETRIES	10
97fe68c43cSAlex Elder #define GSI_CHANNEL_MODEM_FLOW_RETRIES	5	/* disable flow control only */
98650d1603SAlex Elder 
99650d1603SAlex Elder #define GSI_MHI_EVENT_ID_START		10	/* 1st reserved event id */
100650d1603SAlex Elder #define GSI_MHI_EVENT_ID_END		16	/* Last reserved event id */
101650d1603SAlex Elder 
102650d1603SAlex Elder #define GSI_ISR_MAX_ITER		50	/* Detect interrupt storms */
103650d1603SAlex Elder 
104650d1603SAlex Elder /* An entry in an event ring */
105650d1603SAlex Elder struct gsi_event {
106650d1603SAlex Elder 	__le64 xfer_ptr;
107650d1603SAlex Elder 	__le16 len;
108650d1603SAlex Elder 	u8 reserved1;
109650d1603SAlex Elder 	u8 code;
110650d1603SAlex Elder 	__le16 reserved2;
111650d1603SAlex Elder 	u8 type;
112650d1603SAlex Elder 	u8 chid;
113650d1603SAlex Elder };
114650d1603SAlex Elder 
115650d1603SAlex Elder /** gsi_channel_scratch_gpi - GPI protocol scratch register
116650d1603SAlex Elder  * @max_outstanding_tre:
117650d1603SAlex Elder  *	Defines the maximum number of TREs allowed in a single transaction
118650d1603SAlex Elder  *	on a channel (in bytes).  This determines the amount of prefetch
119650d1603SAlex Elder  *	performed by the hardware.  We configure this to equal the size of
120650d1603SAlex Elder  *	the TLV FIFO for the channel.
121650d1603SAlex Elder  * @outstanding_threshold:
122650d1603SAlex Elder  *	Defines the threshold (in bytes) determining when the sequencer
123650d1603SAlex Elder  *	should update the channel doorbell.  We configure this to equal
124650d1603SAlex Elder  *	the size of two TREs.
125650d1603SAlex Elder  */
126650d1603SAlex Elder struct gsi_channel_scratch_gpi {
127650d1603SAlex Elder 	u64 reserved1;
128650d1603SAlex Elder 	u16 reserved2;
129650d1603SAlex Elder 	u16 max_outstanding_tre;
130650d1603SAlex Elder 	u16 reserved3;
131650d1603SAlex Elder 	u16 outstanding_threshold;
132650d1603SAlex Elder };
133650d1603SAlex Elder 
134650d1603SAlex Elder /** gsi_channel_scratch - channel scratch configuration area
135650d1603SAlex Elder  *
136650d1603SAlex Elder  * The exact interpretation of this register is protocol-specific.
137650d1603SAlex Elder  * We only use GPI channels; see struct gsi_channel_scratch_gpi, above.
138650d1603SAlex Elder  */
139650d1603SAlex Elder union gsi_channel_scratch {
140650d1603SAlex Elder 	struct gsi_channel_scratch_gpi gpi;
141650d1603SAlex Elder 	struct {
142650d1603SAlex Elder 		u32 word1;
143650d1603SAlex Elder 		u32 word2;
144650d1603SAlex Elder 		u32 word3;
145650d1603SAlex Elder 		u32 word4;
146650d1603SAlex Elder 	} data;
147650d1603SAlex Elder };
148650d1603SAlex Elder 
149650d1603SAlex Elder /* Check things that can be validated at build time. */
gsi_validate_build(void)150650d1603SAlex Elder static void gsi_validate_build(void)
151650d1603SAlex Elder {
152650d1603SAlex Elder 	/* This is used as a divisor */
153650d1603SAlex Elder 	BUILD_BUG_ON(!GSI_RING_ELEMENT_SIZE);
154650d1603SAlex Elder 
155650d1603SAlex Elder 	/* Code assumes the size of channel and event ring element are
156650d1603SAlex Elder 	 * the same (and fixed).  Make sure the size of an event ring
157650d1603SAlex Elder 	 * element is what's expected.
158650d1603SAlex Elder 	 */
159650d1603SAlex Elder 	BUILD_BUG_ON(sizeof(struct gsi_event) != GSI_RING_ELEMENT_SIZE);
160650d1603SAlex Elder 
161650d1603SAlex Elder 	/* Hardware requires a 2^n ring size.  We ensure the number of
162650d1603SAlex Elder 	 * elements in an event ring is a power of 2 elsewhere; this
163650d1603SAlex Elder 	 * ensure the elements themselves meet the requirement.
164650d1603SAlex Elder 	 */
165650d1603SAlex Elder 	BUILD_BUG_ON(!is_power_of_2(GSI_RING_ELEMENT_SIZE));
166650d1603SAlex Elder }
167650d1603SAlex Elder 
168650d1603SAlex Elder /* Return the channel id associated with a given channel */
gsi_channel_id(struct gsi_channel * channel)169650d1603SAlex Elder static u32 gsi_channel_id(struct gsi_channel *channel)
170650d1603SAlex Elder {
171650d1603SAlex Elder 	return channel - &channel->gsi->channel[0];
172650d1603SAlex Elder }
173650d1603SAlex Elder 
1746170b6daSAlex Elder /* An initialized channel has a non-null GSI pointer */
gsi_channel_initialized(struct gsi_channel * channel)1756170b6daSAlex Elder static bool gsi_channel_initialized(struct gsi_channel *channel)
1766170b6daSAlex Elder {
1776170b6daSAlex Elder 	return !!channel->gsi;
1786170b6daSAlex Elder }
1796170b6daSAlex Elder 
1800ec573efSAlex Elder /* Encode the channel protocol for the CH_C_CNTXT_0 register */
ch_c_cntxt_0_type_encode(enum ipa_version version,const struct reg * reg,enum gsi_channel_type type)1810ec573efSAlex Elder static u32 ch_c_cntxt_0_type_encode(enum ipa_version version,
182330ce9d3SAlex Elder 				    const struct reg *reg,
1830ec573efSAlex Elder 				    enum gsi_channel_type type)
1840ec573efSAlex Elder {
1850ec573efSAlex Elder 	u32 val;
1860ec573efSAlex Elder 
187330ce9d3SAlex Elder 	val = reg_encode(reg, CHTYPE_PROTOCOL, type);
18862747512SAlex Elder 	if (version < IPA_VERSION_4_5 || version >= IPA_VERSION_5_0)
1890ec573efSAlex Elder 		return val;
1900ec573efSAlex Elder 
191330ce9d3SAlex Elder 	type >>= hweight32(reg_fmask(reg, CHTYPE_PROTOCOL));
1920ec573efSAlex Elder 
193330ce9d3SAlex Elder 	return val | reg_encode(reg, CHTYPE_PROTOCOL_MSB, type);
1940ec573efSAlex Elder }
1950ec573efSAlex Elder 
1963ca97ffdSAlex Elder /* Update the GSI IRQ type register with the cached value */
gsi_irq_type_update(struct gsi * gsi,u32 val)1978194be79SAlex Elder static void gsi_irq_type_update(struct gsi *gsi, u32 val)
1983ca97ffdSAlex Elder {
1997ba51aa2SAlex Elder 	const struct reg *reg = gsi_reg(gsi, CNTXT_TYPE_IRQ_MSK);
2007ba51aa2SAlex Elder 
2018194be79SAlex Elder 	gsi->type_enabled_bitmap = val;
2027ba51aa2SAlex Elder 	iowrite32(val, gsi->virt + reg_offset(reg));
2033ca97ffdSAlex Elder }
2043ca97ffdSAlex Elder 
gsi_irq_type_enable(struct gsi * gsi,enum gsi_irq_type_id type_id)205b054d4f9SAlex Elder static void gsi_irq_type_enable(struct gsi *gsi, enum gsi_irq_type_id type_id)
206b054d4f9SAlex Elder {
207c5ebba75SAlex Elder 	gsi_irq_type_update(gsi, gsi->type_enabled_bitmap | type_id);
208b054d4f9SAlex Elder }
209b054d4f9SAlex Elder 
gsi_irq_type_disable(struct gsi * gsi,enum gsi_irq_type_id type_id)210b054d4f9SAlex Elder static void gsi_irq_type_disable(struct gsi *gsi, enum gsi_irq_type_id type_id)
211b054d4f9SAlex Elder {
212c5ebba75SAlex Elder 	gsi_irq_type_update(gsi, gsi->type_enabled_bitmap & ~type_id);
213b054d4f9SAlex Elder }
214b054d4f9SAlex Elder 
215a60d0632SAlex Elder /* Event ring commands are performed one at a time.  Their completion
216a60d0632SAlex Elder  * is signaled by the event ring control GSI interrupt type, which is
217a60d0632SAlex Elder  * only enabled when we issue an event ring command.  Only the event
218a60d0632SAlex Elder  * ring being operated on has this interrupt enabled.
219a60d0632SAlex Elder  */
gsi_irq_ev_ctrl_enable(struct gsi * gsi,u32 evt_ring_id)220a60d0632SAlex Elder static void gsi_irq_ev_ctrl_enable(struct gsi *gsi, u32 evt_ring_id)
221a60d0632SAlex Elder {
222a60d0632SAlex Elder 	u32 val = BIT(evt_ring_id);
2237ba51aa2SAlex Elder 	const struct reg *reg;
224a60d0632SAlex Elder 
225a60d0632SAlex Elder 	/* There's a small chance that a previous command completed
226a60d0632SAlex Elder 	 * after the interrupt was disabled, so make sure we have no
227a60d0632SAlex Elder 	 * pending interrupts before we enable them.
228a60d0632SAlex Elder 	 */
2297ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_SRC_EV_CH_IRQ_CLR);
2307ba51aa2SAlex Elder 	iowrite32(~0, gsi->virt + reg_offset(reg));
231a60d0632SAlex Elder 
2327ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_SRC_EV_CH_IRQ_MSK);
2337ba51aa2SAlex Elder 	iowrite32(val, gsi->virt + reg_offset(reg));
234a60d0632SAlex Elder 	gsi_irq_type_enable(gsi, GSI_EV_CTRL);
235a60d0632SAlex Elder }
236a60d0632SAlex Elder 
237a60d0632SAlex Elder /* Disable event ring control interrupts */
gsi_irq_ev_ctrl_disable(struct gsi * gsi)238a60d0632SAlex Elder static void gsi_irq_ev_ctrl_disable(struct gsi *gsi)
239a60d0632SAlex Elder {
2407ba51aa2SAlex Elder 	const struct reg *reg;
2417ba51aa2SAlex Elder 
242a60d0632SAlex Elder 	gsi_irq_type_disable(gsi, GSI_EV_CTRL);
2437ba51aa2SAlex Elder 
2447ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_SRC_EV_CH_IRQ_MSK);
2457ba51aa2SAlex Elder 	iowrite32(0, gsi->virt + reg_offset(reg));
246a60d0632SAlex Elder }
247a60d0632SAlex Elder 
248a60d0632SAlex Elder /* Channel commands are performed one at a time.  Their completion is
249a60d0632SAlex Elder  * signaled by the channel control GSI interrupt type, which is only
250a60d0632SAlex Elder  * enabled when we issue a channel command.  Only the channel being
251a60d0632SAlex Elder  * operated on has this interrupt enabled.
252a60d0632SAlex Elder  */
gsi_irq_ch_ctrl_enable(struct gsi * gsi,u32 channel_id)253a60d0632SAlex Elder static void gsi_irq_ch_ctrl_enable(struct gsi *gsi, u32 channel_id)
254a60d0632SAlex Elder {
255a60d0632SAlex Elder 	u32 val = BIT(channel_id);
2567ba51aa2SAlex Elder 	const struct reg *reg;
257a60d0632SAlex Elder 
258a60d0632SAlex Elder 	/* There's a small chance that a previous command completed
259a60d0632SAlex Elder 	 * after the interrupt was disabled, so make sure we have no
260a60d0632SAlex Elder 	 * pending interrupts before we enable them.
261a60d0632SAlex Elder 	 */
2627ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_SRC_CH_IRQ_CLR);
2637ba51aa2SAlex Elder 	iowrite32(~0, gsi->virt + reg_offset(reg));
264a60d0632SAlex Elder 
2657ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_SRC_CH_IRQ_MSK);
2667ba51aa2SAlex Elder 	iowrite32(val, gsi->virt + reg_offset(reg));
2677ba51aa2SAlex Elder 
268a60d0632SAlex Elder 	gsi_irq_type_enable(gsi, GSI_CH_CTRL);
269a60d0632SAlex Elder }
270a60d0632SAlex Elder 
271a60d0632SAlex Elder /* Disable channel control interrupts */
gsi_irq_ch_ctrl_disable(struct gsi * gsi)272a60d0632SAlex Elder static void gsi_irq_ch_ctrl_disable(struct gsi *gsi)
273a60d0632SAlex Elder {
2747ba51aa2SAlex Elder 	const struct reg *reg;
2757ba51aa2SAlex Elder 
276a60d0632SAlex Elder 	gsi_irq_type_disable(gsi, GSI_CH_CTRL);
2777ba51aa2SAlex Elder 
2787ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_SRC_CH_IRQ_MSK);
2797ba51aa2SAlex Elder 	iowrite32(0, gsi->virt + reg_offset(reg));
280a60d0632SAlex Elder }
281a60d0632SAlex Elder 
gsi_irq_ieob_enable_one(struct gsi * gsi,u32 evt_ring_id)2825725593eSAlex Elder static void gsi_irq_ieob_enable_one(struct gsi *gsi, u32 evt_ring_id)
283650d1603SAlex Elder {
28406c86328SAlex Elder 	bool enable_ieob = !gsi->ieob_enabled_bitmap;
2857ba51aa2SAlex Elder 	const struct reg *reg;
286650d1603SAlex Elder 	u32 val;
287650d1603SAlex Elder 
288a054539dSAlex Elder 	gsi->ieob_enabled_bitmap |= BIT(evt_ring_id);
2897ba51aa2SAlex Elder 
2907ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_SRC_IEOB_IRQ_MSK);
291a054539dSAlex Elder 	val = gsi->ieob_enabled_bitmap;
2927ba51aa2SAlex Elder 	iowrite32(val, gsi->virt + reg_offset(reg));
29306c86328SAlex Elder 
29406c86328SAlex Elder 	/* Enable the interrupt type if this is the first channel enabled */
29506c86328SAlex Elder 	if (enable_ieob)
29606c86328SAlex Elder 		gsi_irq_type_enable(gsi, GSI_IEOB);
297650d1603SAlex Elder }
298650d1603SAlex Elder 
gsi_irq_ieob_disable(struct gsi * gsi,u32 event_mask)2995725593eSAlex Elder static void gsi_irq_ieob_disable(struct gsi *gsi, u32 event_mask)
300650d1603SAlex Elder {
3017ba51aa2SAlex Elder 	const struct reg *reg;
302650d1603SAlex Elder 	u32 val;
303650d1603SAlex Elder 
3045725593eSAlex Elder 	gsi->ieob_enabled_bitmap &= ~event_mask;
30506c86328SAlex Elder 
30606c86328SAlex Elder 	/* Disable the interrupt type if this was the last enabled channel */
30706c86328SAlex Elder 	if (!gsi->ieob_enabled_bitmap)
30806c86328SAlex Elder 		gsi_irq_type_disable(gsi, GSI_IEOB);
30906c86328SAlex Elder 
3107ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_SRC_IEOB_IRQ_MSK);
311a054539dSAlex Elder 	val = gsi->ieob_enabled_bitmap;
3127ba51aa2SAlex Elder 	iowrite32(val, gsi->virt + reg_offset(reg));
313650d1603SAlex Elder }
314650d1603SAlex Elder 
gsi_irq_ieob_disable_one(struct gsi * gsi,u32 evt_ring_id)3155725593eSAlex Elder static void gsi_irq_ieob_disable_one(struct gsi *gsi, u32 evt_ring_id)
3165725593eSAlex Elder {
3175725593eSAlex Elder 	gsi_irq_ieob_disable(gsi, BIT(evt_ring_id));
3185725593eSAlex Elder }
3195725593eSAlex Elder 
320650d1603SAlex Elder /* Enable all GSI_interrupt types */
gsi_irq_enable(struct gsi * gsi)321650d1603SAlex Elder static void gsi_irq_enable(struct gsi *gsi)
322650d1603SAlex Elder {
3237ba51aa2SAlex Elder 	const struct reg *reg;
324650d1603SAlex Elder 	u32 val;
325650d1603SAlex Elder 
326d6c9e3f5SAlex Elder 	/* Global interrupts include hardware error reports.  Enable
327d6c9e3f5SAlex Elder 	 * that so we can at least report the error should it occur.
328d6c9e3f5SAlex Elder 	 */
3297ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_GLOB_IRQ_EN);
3307ba51aa2SAlex Elder 	iowrite32(ERROR_INT, gsi->virt + reg_offset(reg));
3317ba51aa2SAlex Elder 
332c5ebba75SAlex Elder 	gsi_irq_type_update(gsi, gsi->type_enabled_bitmap | GSI_GLOB_EE);
333d6c9e3f5SAlex Elder 
334352f26a8SAlex Elder 	/* General GSI interrupts are reported to all EEs; if they occur
335352f26a8SAlex Elder 	 * they are unrecoverable (without reset).  A breakpoint interrupt
336352f26a8SAlex Elder 	 * also exists, but we don't support that.  We want to be notified
337352f26a8SAlex Elder 	 * of errors so we can report them, even if they can't be handled.
338352f26a8SAlex Elder 	 */
3397ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_GSI_IRQ_EN);
340c5ebba75SAlex Elder 	val = BUS_ERROR;
341c5ebba75SAlex Elder 	val |= CMD_FIFO_OVRFLOW;
342c5ebba75SAlex Elder 	val |= MCS_STACK_OVRFLOW;
3437ba51aa2SAlex Elder 	iowrite32(val, gsi->virt + reg_offset(reg));
3447ba51aa2SAlex Elder 
345c5ebba75SAlex Elder 	gsi_irq_type_update(gsi, gsi->type_enabled_bitmap | GSI_GENERAL);
346650d1603SAlex Elder }
347650d1603SAlex Elder 
3483ca97ffdSAlex Elder /* Disable all GSI interrupt types */
gsi_irq_disable(struct gsi * gsi)349650d1603SAlex Elder static void gsi_irq_disable(struct gsi *gsi)
350650d1603SAlex Elder {
3517ba51aa2SAlex Elder 	const struct reg *reg;
3527ba51aa2SAlex Elder 
3538194be79SAlex Elder 	gsi_irq_type_update(gsi, 0);
35497eb94c8SAlex Elder 
3558194be79SAlex Elder 	/* Clear the type-specific interrupt masks set by gsi_irq_enable() */
3567ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_GSI_IRQ_EN);
3577ba51aa2SAlex Elder 	iowrite32(0, gsi->virt + reg_offset(reg));
3587ba51aa2SAlex Elder 
3597ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_GLOB_IRQ_EN);
3607ba51aa2SAlex Elder 	iowrite32(0, gsi->virt + reg_offset(reg));
361650d1603SAlex Elder }
362650d1603SAlex Elder 
363650d1603SAlex Elder /* Return the virtual address associated with a ring index */
gsi_ring_virt(struct gsi_ring * ring,u32 index)364650d1603SAlex Elder void *gsi_ring_virt(struct gsi_ring *ring, u32 index)
365650d1603SAlex Elder {
366650d1603SAlex Elder 	/* Note: index *must* be used modulo the ring count here */
367650d1603SAlex Elder 	return ring->virt + (index % ring->count) * GSI_RING_ELEMENT_SIZE;
368650d1603SAlex Elder }
369650d1603SAlex Elder 
370650d1603SAlex Elder /* Return the 32-bit DMA address associated with a ring index */
gsi_ring_addr(struct gsi_ring * ring,u32 index)371650d1603SAlex Elder static u32 gsi_ring_addr(struct gsi_ring *ring, u32 index)
372650d1603SAlex Elder {
3733c54b7beSAlex Elder 	return lower_32_bits(ring->addr) + index * GSI_RING_ELEMENT_SIZE;
374650d1603SAlex Elder }
375650d1603SAlex Elder 
376650d1603SAlex Elder /* Return the ring index of a 32-bit ring offset */
gsi_ring_index(struct gsi_ring * ring,u32 offset)377650d1603SAlex Elder static u32 gsi_ring_index(struct gsi_ring *ring, u32 offset)
378650d1603SAlex Elder {
379650d1603SAlex Elder 	return (offset - gsi_ring_addr(ring, 0)) / GSI_RING_ELEMENT_SIZE;
380650d1603SAlex Elder }
381650d1603SAlex Elder 
382650d1603SAlex Elder /* Issue a GSI command by writing a value to a register, then wait for
383650d1603SAlex Elder  * completion to be signaled.  Returns true if the command completes
384650d1603SAlex Elder  * or false if it times out.
385650d1603SAlex Elder  */
gsi_command(struct gsi * gsi,u32 reg,u32 val)3867ece9eaaSAlex Elder static bool gsi_command(struct gsi *gsi, u32 reg, u32 val)
387650d1603SAlex Elder {
38859b5f454SAlex Elder 	unsigned long timeout = msecs_to_jiffies(GSI_CMD_TIMEOUT);
3897ece9eaaSAlex Elder 	struct completion *completion = &gsi->completion;
39059b5f454SAlex Elder 
391650d1603SAlex Elder 	reinit_completion(completion);
392650d1603SAlex Elder 
393650d1603SAlex Elder 	iowrite32(val, gsi->virt + reg);
394650d1603SAlex Elder 
39559b5f454SAlex Elder 	return !!wait_for_completion_timeout(completion, timeout);
396650d1603SAlex Elder }
397650d1603SAlex Elder 
398650d1603SAlex Elder /* Return the hardware's notion of the current state of an event ring */
399650d1603SAlex Elder static enum gsi_evt_ring_state
gsi_evt_ring_state(struct gsi * gsi,u32 evt_ring_id)400650d1603SAlex Elder gsi_evt_ring_state(struct gsi *gsi, u32 evt_ring_id)
401650d1603SAlex Elder {
402d1ce6395SAlex Elder 	const struct reg *reg = gsi_reg(gsi, EV_CH_E_CNTXT_0);
403650d1603SAlex Elder 	u32 val;
404650d1603SAlex Elder 
405d1ce6395SAlex Elder 	val = ioread32(gsi->virt + reg_n_offset(reg, evt_ring_id));
406650d1603SAlex Elder 
407edc6158bSAlex Elder 	return reg_decode(reg, EV_CHSTATE, val);
408650d1603SAlex Elder }
409650d1603SAlex Elder 
410650d1603SAlex Elder /* Issue an event ring command and wait for it to complete */
gsi_evt_ring_command(struct gsi * gsi,u32 evt_ring_id,enum gsi_evt_cmd_opcode opcode)411d9cbe818SAlex Elder static void gsi_evt_ring_command(struct gsi *gsi, u32 evt_ring_id,
412650d1603SAlex Elder 				 enum gsi_evt_cmd_opcode opcode)
413650d1603SAlex Elder {
4148463488aSAlex Elder 	struct device *dev = gsi->dev;
4155791a73cSAlex Elder 	const struct reg *reg;
416d9cbe818SAlex Elder 	bool timeout;
417650d1603SAlex Elder 	u32 val;
418650d1603SAlex Elder 
419a60d0632SAlex Elder 	/* Enable the completion interrupt for the command */
420a60d0632SAlex Elder 	gsi_irq_ev_ctrl_enable(gsi, evt_ring_id);
421b4175f87SAlex Elder 
4225791a73cSAlex Elder 	reg = gsi_reg(gsi, EV_CH_CMD);
4233f3741c9SAlex Elder 	val = reg_encode(reg, EV_CHID, evt_ring_id);
4243f3741c9SAlex Elder 	val |= reg_encode(reg, EV_OPCODE, opcode);
425650d1603SAlex Elder 
4265791a73cSAlex Elder 	timeout = !gsi_command(gsi, reg_offset(reg), val);
427b4175f87SAlex Elder 
428a60d0632SAlex Elder 	gsi_irq_ev_ctrl_disable(gsi);
429b4175f87SAlex Elder 
430d9cbe818SAlex Elder 	if (!timeout)
4311ddf776bSAlex Elder 		return;
432650d1603SAlex Elder 
4338463488aSAlex Elder 	dev_err(dev, "GSI command %u for event ring %u timed out, state %u\n",
4343f77c926SAlex Elder 		opcode, evt_ring_id, gsi_evt_ring_state(gsi, evt_ring_id));
435650d1603SAlex Elder }
436650d1603SAlex Elder 
437650d1603SAlex Elder /* Allocate an event ring in NOT_ALLOCATED state */
gsi_evt_ring_alloc_command(struct gsi * gsi,u32 evt_ring_id)438650d1603SAlex Elder static int gsi_evt_ring_alloc_command(struct gsi *gsi, u32 evt_ring_id)
439650d1603SAlex Elder {
4403f77c926SAlex Elder 	enum gsi_evt_ring_state state;
441650d1603SAlex Elder 
442650d1603SAlex Elder 	/* Get initial event ring state */
4433f77c926SAlex Elder 	state = gsi_evt_ring_state(gsi, evt_ring_id);
4443f77c926SAlex Elder 	if (state != GSI_EVT_RING_STATE_NOT_ALLOCATED) {
445f8d3bdd5SAlex Elder 		dev_err(gsi->dev, "event ring %u bad state %u before alloc\n",
4463f77c926SAlex Elder 			evt_ring_id, state);
447650d1603SAlex Elder 		return -EINVAL;
448a442b3c7SAlex Elder 	}
449650d1603SAlex Elder 
450d9cbe818SAlex Elder 	gsi_evt_ring_command(gsi, evt_ring_id, GSI_EVT_ALLOCATE);
451428b448eSAlex Elder 
452428b448eSAlex Elder 	/* If successful the event ring state will have changed */
4533f77c926SAlex Elder 	state = gsi_evt_ring_state(gsi, evt_ring_id);
4543f77c926SAlex Elder 	if (state == GSI_EVT_RING_STATE_ALLOCATED)
455428b448eSAlex Elder 		return 0;
456428b448eSAlex Elder 
457f8d3bdd5SAlex Elder 	dev_err(gsi->dev, "event ring %u bad state %u after alloc\n",
4583f77c926SAlex Elder 		evt_ring_id, state);
459650d1603SAlex Elder 
460428b448eSAlex Elder 	return -EIO;
461650d1603SAlex Elder }
462650d1603SAlex Elder 
463650d1603SAlex Elder /* Reset a GSI event ring in ALLOCATED or ERROR state. */
gsi_evt_ring_reset_command(struct gsi * gsi,u32 evt_ring_id)464650d1603SAlex Elder static void gsi_evt_ring_reset_command(struct gsi *gsi, u32 evt_ring_id)
465650d1603SAlex Elder {
4663f77c926SAlex Elder 	enum gsi_evt_ring_state state;
467650d1603SAlex Elder 
4683f77c926SAlex Elder 	state = gsi_evt_ring_state(gsi, evt_ring_id);
469650d1603SAlex Elder 	if (state != GSI_EVT_RING_STATE_ALLOCATED &&
470650d1603SAlex Elder 	    state != GSI_EVT_RING_STATE_ERROR) {
471f8d3bdd5SAlex Elder 		dev_err(gsi->dev, "event ring %u bad state %u before reset\n",
4723f77c926SAlex Elder 			evt_ring_id, state);
473650d1603SAlex Elder 		return;
474650d1603SAlex Elder 	}
475650d1603SAlex Elder 
476d9cbe818SAlex Elder 	gsi_evt_ring_command(gsi, evt_ring_id, GSI_EVT_RESET);
477428b448eSAlex Elder 
478428b448eSAlex Elder 	/* If successful the event ring state will have changed */
4793f77c926SAlex Elder 	state = gsi_evt_ring_state(gsi, evt_ring_id);
4803f77c926SAlex Elder 	if (state == GSI_EVT_RING_STATE_ALLOCATED)
481428b448eSAlex Elder 		return;
482428b448eSAlex Elder 
483f8d3bdd5SAlex Elder 	dev_err(gsi->dev, "event ring %u bad state %u after reset\n",
4843f77c926SAlex Elder 		evt_ring_id, state);
485650d1603SAlex Elder }
486650d1603SAlex Elder 
487650d1603SAlex Elder /* Issue a hardware de-allocation request for an allocated event ring */
gsi_evt_ring_de_alloc_command(struct gsi * gsi,u32 evt_ring_id)488650d1603SAlex Elder static void gsi_evt_ring_de_alloc_command(struct gsi *gsi, u32 evt_ring_id)
489650d1603SAlex Elder {
4903f77c926SAlex Elder 	enum gsi_evt_ring_state state;
491650d1603SAlex Elder 
4923f77c926SAlex Elder 	state = gsi_evt_ring_state(gsi, evt_ring_id);
4933f77c926SAlex Elder 	if (state != GSI_EVT_RING_STATE_ALLOCATED) {
494f8d3bdd5SAlex Elder 		dev_err(gsi->dev, "event ring %u state %u before dealloc\n",
4953f77c926SAlex Elder 			evt_ring_id, state);
496650d1603SAlex Elder 		return;
497650d1603SAlex Elder 	}
498650d1603SAlex Elder 
499d9cbe818SAlex Elder 	gsi_evt_ring_command(gsi, evt_ring_id, GSI_EVT_DE_ALLOC);
500428b448eSAlex Elder 
501428b448eSAlex Elder 	/* If successful the event ring state will have changed */
5023f77c926SAlex Elder 	state = gsi_evt_ring_state(gsi, evt_ring_id);
5033f77c926SAlex Elder 	if (state == GSI_EVT_RING_STATE_NOT_ALLOCATED)
504428b448eSAlex Elder 		return;
505428b448eSAlex Elder 
506f8d3bdd5SAlex Elder 	dev_err(gsi->dev, "event ring %u bad state %u after dealloc\n",
5073f77c926SAlex Elder 		evt_ring_id, state);
508650d1603SAlex Elder }
509650d1603SAlex Elder 
510a2003b30SAlex Elder /* Fetch the current state of a channel from hardware */
gsi_channel_state(struct gsi_channel * channel)511aba7924fSAlex Elder static enum gsi_channel_state gsi_channel_state(struct gsi_channel *channel)
512650d1603SAlex Elder {
51376924eb9SAlex Elder 	const struct reg *reg = gsi_reg(channel->gsi, CH_C_CNTXT_0);
514aba7924fSAlex Elder 	u32 channel_id = gsi_channel_id(channel);
51576924eb9SAlex Elder 	struct gsi *gsi = channel->gsi;
51676924eb9SAlex Elder 	void __iomem *virt = gsi->virt;
517650d1603SAlex Elder 	u32 val;
518650d1603SAlex Elder 
51976924eb9SAlex Elder 	reg = gsi_reg(gsi, CH_C_CNTXT_0);
52076924eb9SAlex Elder 	val = ioread32(virt + reg_n_offset(reg, channel_id));
521650d1603SAlex Elder 
522330ce9d3SAlex Elder 	return reg_decode(reg, CHSTATE, val);
523650d1603SAlex Elder }
524650d1603SAlex Elder 
525650d1603SAlex Elder /* Issue a channel command and wait for it to complete */
5261169318bSAlex Elder static void
gsi_channel_command(struct gsi_channel * channel,enum gsi_ch_cmd_opcode opcode)527650d1603SAlex Elder gsi_channel_command(struct gsi_channel *channel, enum gsi_ch_cmd_opcode opcode)
528650d1603SAlex Elder {
529650d1603SAlex Elder 	u32 channel_id = gsi_channel_id(channel);
530a2003b30SAlex Elder 	struct gsi *gsi = channel->gsi;
5318463488aSAlex Elder 	struct device *dev = gsi->dev;
5325791a73cSAlex Elder 	const struct reg *reg;
533d9cbe818SAlex Elder 	bool timeout;
534650d1603SAlex Elder 	u32 val;
535650d1603SAlex Elder 
536a60d0632SAlex Elder 	/* Enable the completion interrupt for the command */
537a60d0632SAlex Elder 	gsi_irq_ch_ctrl_enable(gsi, channel_id);
538b054d4f9SAlex Elder 
5395791a73cSAlex Elder 	reg = gsi_reg(gsi, CH_CMD);
5403f3741c9SAlex Elder 	val = reg_encode(reg, CH_CHID, channel_id);
5413f3741c9SAlex Elder 	val |= reg_encode(reg, CH_OPCODE, opcode);
5425791a73cSAlex Elder 
5435791a73cSAlex Elder 	timeout = !gsi_command(gsi, reg_offset(reg), val);
544650d1603SAlex Elder 
545a60d0632SAlex Elder 	gsi_irq_ch_ctrl_disable(gsi);
546b054d4f9SAlex Elder 
547d9cbe818SAlex Elder 	if (!timeout)
5481169318bSAlex Elder 		return;
549650d1603SAlex Elder 
5508463488aSAlex Elder 	dev_err(dev, "GSI command %u for channel %u timed out, state %u\n",
551a2003b30SAlex Elder 		opcode, channel_id, gsi_channel_state(channel));
552650d1603SAlex Elder }
553650d1603SAlex Elder 
554650d1603SAlex Elder /* Allocate GSI channel in NOT_ALLOCATED state */
gsi_channel_alloc_command(struct gsi * gsi,u32 channel_id)555650d1603SAlex Elder static int gsi_channel_alloc_command(struct gsi *gsi, u32 channel_id)
556650d1603SAlex Elder {
557650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
558a442b3c7SAlex Elder 	struct device *dev = gsi->dev;
559a2003b30SAlex Elder 	enum gsi_channel_state state;
560650d1603SAlex Elder 
561650d1603SAlex Elder 	/* Get initial channel state */
562a2003b30SAlex Elder 	state = gsi_channel_state(channel);
563a442b3c7SAlex Elder 	if (state != GSI_CHANNEL_STATE_NOT_ALLOCATED) {
564f8d3bdd5SAlex Elder 		dev_err(dev, "channel %u bad state %u before alloc\n",
565f8d3bdd5SAlex Elder 			channel_id, state);
566650d1603SAlex Elder 		return -EINVAL;
567a442b3c7SAlex Elder 	}
568650d1603SAlex Elder 
5691169318bSAlex Elder 	gsi_channel_command(channel, GSI_CH_ALLOCATE);
570a2003b30SAlex Elder 
5716ffddf3bSAlex Elder 	/* If successful the channel state will have changed */
572a2003b30SAlex Elder 	state = gsi_channel_state(channel);
5736ffddf3bSAlex Elder 	if (state == GSI_CHANNEL_STATE_ALLOCATED)
5746ffddf3bSAlex Elder 		return 0;
5756ffddf3bSAlex Elder 
576f8d3bdd5SAlex Elder 	dev_err(dev, "channel %u bad state %u after alloc\n",
577f8d3bdd5SAlex Elder 		channel_id, state);
578650d1603SAlex Elder 
5796ffddf3bSAlex Elder 	return -EIO;
580650d1603SAlex Elder }
581650d1603SAlex Elder 
582650d1603SAlex Elder /* Start an ALLOCATED channel */
gsi_channel_start_command(struct gsi_channel * channel)583650d1603SAlex Elder static int gsi_channel_start_command(struct gsi_channel *channel)
584650d1603SAlex Elder {
585a442b3c7SAlex Elder 	struct device *dev = channel->gsi->dev;
586a2003b30SAlex Elder 	enum gsi_channel_state state;
587650d1603SAlex Elder 
588a2003b30SAlex Elder 	state = gsi_channel_state(channel);
589650d1603SAlex Elder 	if (state != GSI_CHANNEL_STATE_ALLOCATED &&
590a442b3c7SAlex Elder 	    state != GSI_CHANNEL_STATE_STOPPED) {
591f8d3bdd5SAlex Elder 		dev_err(dev, "channel %u bad state %u before start\n",
592f8d3bdd5SAlex Elder 			gsi_channel_id(channel), state);
593650d1603SAlex Elder 		return -EINVAL;
594a442b3c7SAlex Elder 	}
595650d1603SAlex Elder 
5961169318bSAlex Elder 	gsi_channel_command(channel, GSI_CH_START);
597a2003b30SAlex Elder 
5986ffddf3bSAlex Elder 	/* If successful the channel state will have changed */
599a2003b30SAlex Elder 	state = gsi_channel_state(channel);
6006ffddf3bSAlex Elder 	if (state == GSI_CHANNEL_STATE_STARTED)
6016ffddf3bSAlex Elder 		return 0;
6026ffddf3bSAlex Elder 
603f8d3bdd5SAlex Elder 	dev_err(dev, "channel %u bad state %u after start\n",
604f8d3bdd5SAlex Elder 		gsi_channel_id(channel), state);
605650d1603SAlex Elder 
6066ffddf3bSAlex Elder 	return -EIO;
607650d1603SAlex Elder }
608650d1603SAlex Elder 
609650d1603SAlex Elder /* Stop a GSI channel in STARTED state */
gsi_channel_stop_command(struct gsi_channel * channel)610650d1603SAlex Elder static int gsi_channel_stop_command(struct gsi_channel *channel)
611650d1603SAlex Elder {
612a442b3c7SAlex Elder 	struct device *dev = channel->gsi->dev;
613a2003b30SAlex Elder 	enum gsi_channel_state state;
614650d1603SAlex Elder 
615a2003b30SAlex Elder 	state = gsi_channel_state(channel);
6165468cbcdSAlex Elder 
6175468cbcdSAlex Elder 	/* Channel could have entered STOPPED state since last call
6185468cbcdSAlex Elder 	 * if it timed out.  If so, we're done.
6195468cbcdSAlex Elder 	 */
6205468cbcdSAlex Elder 	if (state == GSI_CHANNEL_STATE_STOPPED)
6215468cbcdSAlex Elder 		return 0;
6225468cbcdSAlex Elder 
623650d1603SAlex Elder 	if (state != GSI_CHANNEL_STATE_STARTED &&
624a442b3c7SAlex Elder 	    state != GSI_CHANNEL_STATE_STOP_IN_PROC) {
625f8d3bdd5SAlex Elder 		dev_err(dev, "channel %u bad state %u before stop\n",
626f8d3bdd5SAlex Elder 			gsi_channel_id(channel), state);
627650d1603SAlex Elder 		return -EINVAL;
628a442b3c7SAlex Elder 	}
629650d1603SAlex Elder 
6301169318bSAlex Elder 	gsi_channel_command(channel, GSI_CH_STOP);
631a2003b30SAlex Elder 
6326ffddf3bSAlex Elder 	/* If successful the channel state will have changed */
633a2003b30SAlex Elder 	state = gsi_channel_state(channel);
6346ffddf3bSAlex Elder 	if (state == GSI_CHANNEL_STATE_STOPPED)
6356ffddf3bSAlex Elder 		return 0;
636650d1603SAlex Elder 
637650d1603SAlex Elder 	/* We may have to try again if stop is in progress */
638a2003b30SAlex Elder 	if (state == GSI_CHANNEL_STATE_STOP_IN_PROC)
639650d1603SAlex Elder 		return -EAGAIN;
640650d1603SAlex Elder 
641f8d3bdd5SAlex Elder 	dev_err(dev, "channel %u bad state %u after stop\n",
642f8d3bdd5SAlex Elder 		gsi_channel_id(channel), state);
643650d1603SAlex Elder 
644650d1603SAlex Elder 	return -EIO;
645650d1603SAlex Elder }
646650d1603SAlex Elder 
647650d1603SAlex Elder /* Reset a GSI channel in ALLOCATED or ERROR state. */
gsi_channel_reset_command(struct gsi_channel * channel)648650d1603SAlex Elder static void gsi_channel_reset_command(struct gsi_channel *channel)
649650d1603SAlex Elder {
650a442b3c7SAlex Elder 	struct device *dev = channel->gsi->dev;
651a2003b30SAlex Elder 	enum gsi_channel_state state;
652650d1603SAlex Elder 
65374401946SAlex Elder 	/* A short delay is required before a RESET command */
65474401946SAlex Elder 	usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
655650d1603SAlex Elder 
656a2003b30SAlex Elder 	state = gsi_channel_state(channel);
657a2003b30SAlex Elder 	if (state != GSI_CHANNEL_STATE_STOPPED &&
658a2003b30SAlex Elder 	    state != GSI_CHANNEL_STATE_ERROR) {
6595d28913dSAlex Elder 		/* No need to reset a channel already in ALLOCATED state */
6605d28913dSAlex Elder 		if (state != GSI_CHANNEL_STATE_ALLOCATED)
661f8d3bdd5SAlex Elder 			dev_err(dev, "channel %u bad state %u before reset\n",
662f8d3bdd5SAlex Elder 				gsi_channel_id(channel), state);
663650d1603SAlex Elder 		return;
664650d1603SAlex Elder 	}
665650d1603SAlex Elder 
6661169318bSAlex Elder 	gsi_channel_command(channel, GSI_CH_RESET);
667a2003b30SAlex Elder 
6686ffddf3bSAlex Elder 	/* If successful the channel state will have changed */
669a2003b30SAlex Elder 	state = gsi_channel_state(channel);
6706ffddf3bSAlex Elder 	if (state != GSI_CHANNEL_STATE_ALLOCATED)
671f8d3bdd5SAlex Elder 		dev_err(dev, "channel %u bad state %u after reset\n",
672f8d3bdd5SAlex Elder 			gsi_channel_id(channel), state);
673650d1603SAlex Elder }
674650d1603SAlex Elder 
675650d1603SAlex Elder /* Deallocate an ALLOCATED GSI channel */
gsi_channel_de_alloc_command(struct gsi * gsi,u32 channel_id)676650d1603SAlex Elder static void gsi_channel_de_alloc_command(struct gsi *gsi, u32 channel_id)
677650d1603SAlex Elder {
678650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
679a442b3c7SAlex Elder 	struct device *dev = gsi->dev;
680a2003b30SAlex Elder 	enum gsi_channel_state state;
681650d1603SAlex Elder 
682a2003b30SAlex Elder 	state = gsi_channel_state(channel);
683a2003b30SAlex Elder 	if (state != GSI_CHANNEL_STATE_ALLOCATED) {
684f8d3bdd5SAlex Elder 		dev_err(dev, "channel %u bad state %u before dealloc\n",
685f8d3bdd5SAlex Elder 			channel_id, state);
686650d1603SAlex Elder 		return;
687650d1603SAlex Elder 	}
688650d1603SAlex Elder 
6891169318bSAlex Elder 	gsi_channel_command(channel, GSI_CH_DE_ALLOC);
690a2003b30SAlex Elder 
6916ffddf3bSAlex Elder 	/* If successful the channel state will have changed */
692a2003b30SAlex Elder 	state = gsi_channel_state(channel);
6936ffddf3bSAlex Elder 
6946ffddf3bSAlex Elder 	if (state != GSI_CHANNEL_STATE_NOT_ALLOCATED)
695f8d3bdd5SAlex Elder 		dev_err(dev, "channel %u bad state %u after dealloc\n",
696f8d3bdd5SAlex Elder 			channel_id, state);
697650d1603SAlex Elder }
698650d1603SAlex Elder 
699650d1603SAlex Elder /* Ring an event ring doorbell, reporting the last entry processed by the AP.
700650d1603SAlex Elder  * The index argument (modulo the ring count) is the first unfilled entry, so
701650d1603SAlex Elder  * we supply one less than that with the doorbell.  Update the event ring
702650d1603SAlex Elder  * index field with the value provided.
703650d1603SAlex Elder  */
gsi_evt_ring_doorbell(struct gsi * gsi,u32 evt_ring_id,u32 index)704650d1603SAlex Elder static void gsi_evt_ring_doorbell(struct gsi *gsi, u32 evt_ring_id, u32 index)
705650d1603SAlex Elder {
706d1ce6395SAlex Elder 	const struct reg *reg = gsi_reg(gsi, EV_CH_E_DOORBELL_0);
707650d1603SAlex Elder 	struct gsi_ring *ring = &gsi->evt_ring[evt_ring_id].ring;
708650d1603SAlex Elder 	u32 val;
709650d1603SAlex Elder 
710650d1603SAlex Elder 	ring->index = index;	/* Next unused entry */
711650d1603SAlex Elder 
712650d1603SAlex Elder 	/* Note: index *must* be used modulo the ring count here */
713650d1603SAlex Elder 	val = gsi_ring_addr(ring, (index - 1) % ring->count);
714d1ce6395SAlex Elder 	iowrite32(val, gsi->virt + reg_n_offset(reg, evt_ring_id));
715650d1603SAlex Elder }
716650d1603SAlex Elder 
717650d1603SAlex Elder /* Program an event ring for use */
gsi_evt_ring_program(struct gsi * gsi,u32 evt_ring_id)718650d1603SAlex Elder static void gsi_evt_ring_program(struct gsi *gsi, u32 evt_ring_id)
719650d1603SAlex Elder {
720650d1603SAlex Elder 	struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
7215fb859f7SAlex Elder 	struct gsi_ring *ring = &evt_ring->ring;
722d1ce6395SAlex Elder 	const struct reg *reg;
723650d1603SAlex Elder 	u32 val;
724650d1603SAlex Elder 
725d1ce6395SAlex Elder 	reg = gsi_reg(gsi, EV_CH_E_CNTXT_0);
72646dda53eSAlex Elder 	/* We program all event rings as GPI type/protocol */
727edc6158bSAlex Elder 	val = reg_encode(reg, EV_CHTYPE, GSI_CHANNEL_TYPE_GPI);
728edc6158bSAlex Elder 	/* EV_EE field is 0 (GSI_EE_AP) */
729edc6158bSAlex Elder 	val |= reg_bit(reg, EV_INTYPE);
730edc6158bSAlex Elder 	val |= reg_encode(reg, EV_ELEMENT_SIZE, GSI_RING_ELEMENT_SIZE);
731d1ce6395SAlex Elder 	iowrite32(val, gsi->virt + reg_n_offset(reg, evt_ring_id));
732650d1603SAlex Elder 
733d1ce6395SAlex Elder 	reg = gsi_reg(gsi, EV_CH_E_CNTXT_1);
734f75f44ddSAlex Elder 	val = reg_encode(reg, R_LENGTH, ring->count * GSI_RING_ELEMENT_SIZE);
735d1ce6395SAlex Elder 	iowrite32(val, gsi->virt + reg_n_offset(reg, evt_ring_id));
736650d1603SAlex Elder 
737650d1603SAlex Elder 	/* The context 2 and 3 registers store the low-order and
738650d1603SAlex Elder 	 * high-order 32 bits of the address of the event ring,
739650d1603SAlex Elder 	 * respectively.
740650d1603SAlex Elder 	 */
741d1ce6395SAlex Elder 	reg = gsi_reg(gsi, EV_CH_E_CNTXT_2);
7425fb859f7SAlex Elder 	val = lower_32_bits(ring->addr);
743d1ce6395SAlex Elder 	iowrite32(val, gsi->virt + reg_n_offset(reg, evt_ring_id));
744d1ce6395SAlex Elder 
745d1ce6395SAlex Elder 	reg = gsi_reg(gsi, EV_CH_E_CNTXT_3);
7465fb859f7SAlex Elder 	val = upper_32_bits(ring->addr);
747d1ce6395SAlex Elder 	iowrite32(val, gsi->virt + reg_n_offset(reg, evt_ring_id));
748650d1603SAlex Elder 
749650d1603SAlex Elder 	/* Enable interrupt moderation by setting the moderation delay */
750d1ce6395SAlex Elder 	reg = gsi_reg(gsi, EV_CH_E_CNTXT_8);
751edc6158bSAlex Elder 	val = reg_encode(reg, EV_MODT, GSI_EVT_RING_INT_MODT);
752ecfa80ceSAlex Elder 	val |= reg_encode(reg, EV_MODC, 1);	/* comes from channel */
753edc6158bSAlex Elder 	/* EV_MOD_CNT is 0 (no counter-based interrupt coalescing) */
754d1ce6395SAlex Elder 	iowrite32(val, gsi->virt + reg_n_offset(reg, evt_ring_id));
755650d1603SAlex Elder 
756edc6158bSAlex Elder 	/* No MSI write data, and MSI high and low address is 0 */
757d1ce6395SAlex Elder 	reg = gsi_reg(gsi, EV_CH_E_CNTXT_9);
758d1ce6395SAlex Elder 	iowrite32(0, gsi->virt + reg_n_offset(reg, evt_ring_id));
759d1ce6395SAlex Elder 
760d1ce6395SAlex Elder 	reg = gsi_reg(gsi, EV_CH_E_CNTXT_10);
761d1ce6395SAlex Elder 	iowrite32(0, gsi->virt + reg_n_offset(reg, evt_ring_id));
762d1ce6395SAlex Elder 
763d1ce6395SAlex Elder 	reg = gsi_reg(gsi, EV_CH_E_CNTXT_11);
764d1ce6395SAlex Elder 	iowrite32(0, gsi->virt + reg_n_offset(reg, evt_ring_id));
765650d1603SAlex Elder 
766650d1603SAlex Elder 	/* We don't need to get event read pointer updates */
767d1ce6395SAlex Elder 	reg = gsi_reg(gsi, EV_CH_E_CNTXT_12);
768d1ce6395SAlex Elder 	iowrite32(0, gsi->virt + reg_n_offset(reg, evt_ring_id));
769d1ce6395SAlex Elder 
770d1ce6395SAlex Elder 	reg = gsi_reg(gsi, EV_CH_E_CNTXT_13);
771d1ce6395SAlex Elder 	iowrite32(0, gsi->virt + reg_n_offset(reg, evt_ring_id));
772650d1603SAlex Elder 
7735fb859f7SAlex Elder 	/* Finally, tell the hardware our "last processed" event (arbitrary) */
7745fb859f7SAlex Elder 	gsi_evt_ring_doorbell(gsi, evt_ring_id, ring->index);
775650d1603SAlex Elder }
776650d1603SAlex Elder 
777e6316920SAlex Elder /* Find the transaction whose completion indicates a channel is quiesced */
gsi_channel_trans_last(struct gsi_channel * channel)778650d1603SAlex Elder static struct gsi_trans *gsi_channel_trans_last(struct gsi_channel *channel)
779650d1603SAlex Elder {
780650d1603SAlex Elder 	struct gsi_trans_info *trans_info = &channel->trans_info;
7814601e755SAlex Elder 	u32 pending_id = trans_info->pending_id;
782650d1603SAlex Elder 	struct gsi_trans *trans;
783c30623eaSAlex Elder 	u16 trans_id;
784650d1603SAlex Elder 
7854601e755SAlex Elder 	if (channel->toward_ipa && pending_id != trans_info->free_id) {
7864601e755SAlex Elder 		/* There is a small chance a TX transaction got allocated
7874601e755SAlex Elder 		 * just before we disabled transmits, so check for that.
7884601e755SAlex Elder 		 * The last allocated, committed, or pending transaction
789e68d1d15SAlex Elder 		 * precedes the first free transaction.
790e68d1d15SAlex Elder 		 */
791c30623eaSAlex Elder 		trans_id = trans_info->free_id - 1;
7924601e755SAlex Elder 	} else if (trans_info->polled_id != pending_id) {
793e6316920SAlex Elder 		/* Otherwise (TX or RX) we want to wait for anything that
794e6316920SAlex Elder 		 * has completed, or has been polled but not released yet.
795897c0ce6SAlex Elder 		 *
796e68d1d15SAlex Elder 		 * The last completed or polled transaction precedes the
797e68d1d15SAlex Elder 		 * first pending transaction.
798e6316920SAlex Elder 		 */
7994601e755SAlex Elder 		trans_id = pending_id - 1;
800897c0ce6SAlex Elder 	} else {
8014601e755SAlex Elder 		return NULL;
802897c0ce6SAlex Elder 	}
8034601e755SAlex Elder 
804650d1603SAlex Elder 	/* Caller will wait for this, so take a reference */
8054601e755SAlex Elder 	trans = &trans_info->trans[trans_id % channel->tre_count];
806650d1603SAlex Elder 	refcount_inc(&trans->refcount);
807650d1603SAlex Elder 
808650d1603SAlex Elder 	return trans;
809650d1603SAlex Elder }
810650d1603SAlex Elder 
811650d1603SAlex Elder /* Wait for transaction activity on a channel to complete */
gsi_channel_trans_quiesce(struct gsi_channel * channel)812650d1603SAlex Elder static void gsi_channel_trans_quiesce(struct gsi_channel *channel)
813650d1603SAlex Elder {
814650d1603SAlex Elder 	struct gsi_trans *trans;
815650d1603SAlex Elder 
816650d1603SAlex Elder 	/* Get the last transaction, and wait for it to complete */
817650d1603SAlex Elder 	trans = gsi_channel_trans_last(channel);
818650d1603SAlex Elder 	if (trans) {
819650d1603SAlex Elder 		wait_for_completion(&trans->completion);
820650d1603SAlex Elder 		gsi_trans_free(trans);
821650d1603SAlex Elder 	}
822650d1603SAlex Elder }
823650d1603SAlex Elder 
82457ab8ca4SAlex Elder /* Program a channel for use; there is no gsi_channel_deprogram() */
gsi_channel_program(struct gsi_channel * channel,bool doorbell)825650d1603SAlex Elder static void gsi_channel_program(struct gsi_channel *channel, bool doorbell)
826650d1603SAlex Elder {
827650d1603SAlex Elder 	size_t size = channel->tre_ring.count * GSI_RING_ELEMENT_SIZE;
828650d1603SAlex Elder 	u32 channel_id = gsi_channel_id(channel);
829650d1603SAlex Elder 	union gsi_channel_scratch scr = { };
830650d1603SAlex Elder 	struct gsi_channel_scratch_gpi *gpi;
831650d1603SAlex Elder 	struct gsi *gsi = channel->gsi;
832d2bb6e65SAlex Elder 	const struct reg *reg;
833650d1603SAlex Elder 	u32 wrr_weight = 0;
83476924eb9SAlex Elder 	u32 offset;
835650d1603SAlex Elder 	u32 val;
836650d1603SAlex Elder 
83776924eb9SAlex Elder 	reg = gsi_reg(gsi, CH_C_CNTXT_0);
83876924eb9SAlex Elder 
83946dda53eSAlex Elder 	/* We program all channels as GPI type/protocol */
840330ce9d3SAlex Elder 	val = ch_c_cntxt_0_type_encode(gsi->version, reg, GSI_CHANNEL_TYPE_GPI);
841650d1603SAlex Elder 	if (channel->toward_ipa)
842330ce9d3SAlex Elder 		val |= reg_bit(reg, CHTYPE_DIR);
84337cd29ecSAlex Elder 	if (gsi->version < IPA_VERSION_5_0)
844330ce9d3SAlex Elder 		val |= reg_encode(reg, ERINDEX, channel->evt_ring_id);
845330ce9d3SAlex Elder 	val |= reg_encode(reg, ELEMENT_SIZE, GSI_RING_ELEMENT_SIZE);
84676924eb9SAlex Elder 	iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
847650d1603SAlex Elder 
84876924eb9SAlex Elder 	reg = gsi_reg(gsi, CH_C_CNTXT_1);
849330ce9d3SAlex Elder 	val = reg_encode(reg, CH_R_LENGTH, size);
85037cd29ecSAlex Elder 	if (gsi->version >= IPA_VERSION_5_0)
85137cd29ecSAlex Elder 		val |= reg_encode(reg, CH_ERINDEX, channel->evt_ring_id);
85276924eb9SAlex Elder 	iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
853650d1603SAlex Elder 
854650d1603SAlex Elder 	/* The context 2 and 3 registers store the low-order and
855650d1603SAlex Elder 	 * high-order 32 bits of the address of the channel ring,
856650d1603SAlex Elder 	 * respectively.
857650d1603SAlex Elder 	 */
85876924eb9SAlex Elder 	reg = gsi_reg(gsi, CH_C_CNTXT_2);
8593c54b7beSAlex Elder 	val = lower_32_bits(channel->tre_ring.addr);
86076924eb9SAlex Elder 	iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
86176924eb9SAlex Elder 
86276924eb9SAlex Elder 	reg = gsi_reg(gsi, CH_C_CNTXT_3);
8633c54b7beSAlex Elder 	val = upper_32_bits(channel->tre_ring.addr);
86476924eb9SAlex Elder 	iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
865650d1603SAlex Elder 
866d2bb6e65SAlex Elder 	reg = gsi_reg(gsi, CH_C_QOS);
867d2bb6e65SAlex Elder 
868650d1603SAlex Elder 	/* Command channel gets low weighted round-robin priority */
869650d1603SAlex Elder 	if (channel->command)
870f50ca7ceSAlex Elder 		wrr_weight = reg_field_max(reg, WRR_WEIGHT);
871f50ca7ceSAlex Elder 	val = reg_encode(reg, WRR_WEIGHT, wrr_weight);
872650d1603SAlex Elder 
873650d1603SAlex Elder 	/* Max prefetch is 1 segment (do not set MAX_PREFETCH_FMASK) */
874650d1603SAlex Elder 
875d7f3087bSAlex Elder 	/* No need to use the doorbell engine starting at IPA v4.0 */
876d7f3087bSAlex Elder 	if (gsi->version < IPA_VERSION_4_0 && doorbell)
877f50ca7ceSAlex Elder 		val |= reg_bit(reg, USE_DB_ENG);
878650d1603SAlex Elder 
8799f848198SAlex Elder 	/* v4.0 introduces an escape buffer for prefetch.  We use it
8809f848198SAlex Elder 	 * on all but the AP command channel.
8819f848198SAlex Elder 	 */
882d7f3087bSAlex Elder 	if (gsi->version >= IPA_VERSION_4_0 && !channel->command) {
883b0b6f0ddSAlex Elder 		/* If not otherwise set, prefetch buffers are used */
884b0b6f0ddSAlex Elder 		if (gsi->version < IPA_VERSION_4_5)
885f50ca7ceSAlex Elder 			val |= reg_bit(reg, USE_ESCAPE_BUF_ONLY);
886b0b6f0ddSAlex Elder 		else
887f50ca7ceSAlex Elder 			val |= reg_encode(reg, PREFETCH_MODE, ESCAPE_BUF_ONLY);
888b0b6f0ddSAlex Elder 	}
88942839f95SAlex Elder 	/* All channels set DB_IN_BYTES */
89042839f95SAlex Elder 	if (gsi->version >= IPA_VERSION_4_9)
891f50ca7ceSAlex Elder 		val |= reg_bit(reg, DB_IN_BYTES);
892650d1603SAlex Elder 
893d2bb6e65SAlex Elder 	iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
894650d1603SAlex Elder 
895650d1603SAlex Elder 	/* Now update the scratch registers for GPI protocol */
896650d1603SAlex Elder 	gpi = &scr.gpi;
89788e03057SAlex Elder 	gpi->max_outstanding_tre = channel->trans_tre_max *
898650d1603SAlex Elder 					GSI_RING_ELEMENT_SIZE;
899650d1603SAlex Elder 	gpi->outstanding_threshold = 2 * GSI_RING_ELEMENT_SIZE;
900650d1603SAlex Elder 
90176924eb9SAlex Elder 	reg = gsi_reg(gsi, CH_C_SCRATCH_0);
902650d1603SAlex Elder 	val = scr.data.word1;
90376924eb9SAlex Elder 	iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
904650d1603SAlex Elder 
90576924eb9SAlex Elder 	reg = gsi_reg(gsi, CH_C_SCRATCH_1);
906650d1603SAlex Elder 	val = scr.data.word2;
90776924eb9SAlex Elder 	iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
908650d1603SAlex Elder 
90976924eb9SAlex Elder 	reg = gsi_reg(gsi, CH_C_SCRATCH_2);
910650d1603SAlex Elder 	val = scr.data.word3;
91176924eb9SAlex Elder 	iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
912650d1603SAlex Elder 
913650d1603SAlex Elder 	/* We must preserve the upper 16 bits of the last scratch register.
914650d1603SAlex Elder 	 * The next sequence assumes those bits remain unchanged between the
915650d1603SAlex Elder 	 * read and the write.
916650d1603SAlex Elder 	 */
91776924eb9SAlex Elder 	reg = gsi_reg(gsi, CH_C_SCRATCH_3);
91876924eb9SAlex Elder 	offset = reg_n_offset(reg, channel_id);
91976924eb9SAlex Elder 	val = ioread32(gsi->virt + offset);
920650d1603SAlex Elder 	val = (scr.data.word4 & GENMASK(31, 16)) | (val & GENMASK(15, 0));
92176924eb9SAlex Elder 	iowrite32(val, gsi->virt + offset);
922650d1603SAlex Elder 
923650d1603SAlex Elder 	/* All done! */
924650d1603SAlex Elder }
925650d1603SAlex Elder 
__gsi_channel_start(struct gsi_channel * channel,bool resume)9264a4ba483SAlex Elder static int __gsi_channel_start(struct gsi_channel *channel, bool resume)
927650d1603SAlex Elder {
928893b838eSAlex Elder 	struct gsi *gsi = channel->gsi;
929650d1603SAlex Elder 	int ret;
930650d1603SAlex Elder 
9314a4ba483SAlex Elder 	/* Prior to IPA v4.0 suspend/resume is not implemented by GSI */
9324a4ba483SAlex Elder 	if (resume && gsi->version < IPA_VERSION_4_0)
933a65c0288SAlex Elder 		return 0;
9344fef691cSAlex Elder 
935650d1603SAlex Elder 	mutex_lock(&gsi->mutex);
936650d1603SAlex Elder 
937a65c0288SAlex Elder 	ret = gsi_channel_start_command(channel);
938650d1603SAlex Elder 
939650d1603SAlex Elder 	mutex_unlock(&gsi->mutex);
940650d1603SAlex Elder 
941650d1603SAlex Elder 	return ret;
942650d1603SAlex Elder }
943650d1603SAlex Elder 
944893b838eSAlex Elder /* Start an allocated GSI channel */
gsi_channel_start(struct gsi * gsi,u32 channel_id)945893b838eSAlex Elder int gsi_channel_start(struct gsi *gsi, u32 channel_id)
946893b838eSAlex Elder {
947893b838eSAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
948a65c0288SAlex Elder 	int ret;
949893b838eSAlex Elder 
950a65c0288SAlex Elder 	/* Enable NAPI and the completion interrupt */
951a65c0288SAlex Elder 	napi_enable(&channel->napi);
952a65c0288SAlex Elder 	gsi_irq_ieob_enable_one(gsi, channel->evt_ring_id);
953a65c0288SAlex Elder 
9544a4ba483SAlex Elder 	ret = __gsi_channel_start(channel, false);
955a65c0288SAlex Elder 	if (ret) {
956a65c0288SAlex Elder 		gsi_irq_ieob_disable_one(gsi, channel->evt_ring_id);
957a65c0288SAlex Elder 		napi_disable(&channel->napi);
958a65c0288SAlex Elder 	}
959a65c0288SAlex Elder 
960a65c0288SAlex Elder 	return ret;
961893b838eSAlex Elder }
962893b838eSAlex Elder 
gsi_channel_stop_retry(struct gsi_channel * channel)963697e834eSAlex Elder static int gsi_channel_stop_retry(struct gsi_channel *channel)
964650d1603SAlex Elder {
965057ef63fSAlex Elder 	u32 retries = GSI_CHANNEL_STOP_RETRIES;
966650d1603SAlex Elder 	int ret;
967650d1603SAlex Elder 
968650d1603SAlex Elder 	do {
969650d1603SAlex Elder 		ret = gsi_channel_stop_command(channel);
970650d1603SAlex Elder 		if (ret != -EAGAIN)
971650d1603SAlex Elder 			break;
9723d60e15fSAlex Elder 		usleep_range(3 * USEC_PER_MSEC, 5 * USEC_PER_MSEC);
973650d1603SAlex Elder 	} while (retries--);
974650d1603SAlex Elder 
975697e834eSAlex Elder 	return ret;
976697e834eSAlex Elder }
977697e834eSAlex Elder 
__gsi_channel_stop(struct gsi_channel * channel,bool suspend)9784a4ba483SAlex Elder static int __gsi_channel_stop(struct gsi_channel *channel, bool suspend)
979697e834eSAlex Elder {
98063ec9be1SAlex Elder 	struct gsi *gsi = channel->gsi;
981697e834eSAlex Elder 	int ret;
982697e834eSAlex Elder 
983a65c0288SAlex Elder 	/* Wait for any underway transactions to complete before stopping. */
984bd1ea1e4SAlex Elder 	gsi_channel_trans_quiesce(channel);
985697e834eSAlex Elder 
9864a4ba483SAlex Elder 	/* Prior to IPA v4.0 suspend/resume is not implemented by GSI */
9874a4ba483SAlex Elder 	if (suspend && gsi->version < IPA_VERSION_4_0)
98863ec9be1SAlex Elder 		return 0;
98963ec9be1SAlex Elder 
99063ec9be1SAlex Elder 	mutex_lock(&gsi->mutex);
99163ec9be1SAlex Elder 
99263ec9be1SAlex Elder 	ret = gsi_channel_stop_retry(channel);
99363ec9be1SAlex Elder 
99463ec9be1SAlex Elder 	mutex_unlock(&gsi->mutex);
99563ec9be1SAlex Elder 
99663ec9be1SAlex Elder 	return ret;
997650d1603SAlex Elder }
998650d1603SAlex Elder 
999893b838eSAlex Elder /* Stop a started channel */
gsi_channel_stop(struct gsi * gsi,u32 channel_id)1000893b838eSAlex Elder int gsi_channel_stop(struct gsi *gsi, u32 channel_id)
1001893b838eSAlex Elder {
1002893b838eSAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
1003a65c0288SAlex Elder 	int ret;
1004893b838eSAlex Elder 
10054a4ba483SAlex Elder 	ret = __gsi_channel_stop(channel, false);
1006a65c0288SAlex Elder 	if (ret)
1007a65c0288SAlex Elder 		return ret;
1008a65c0288SAlex Elder 
100963ec9be1SAlex Elder 	/* Disable the completion interrupt and NAPI if successful */
1010a65c0288SAlex Elder 	gsi_irq_ieob_disable_one(gsi, channel->evt_ring_id);
1011a65c0288SAlex Elder 	napi_disable(&channel->napi);
1012a65c0288SAlex Elder 
1013a65c0288SAlex Elder 	return 0;
1014893b838eSAlex Elder }
1015893b838eSAlex Elder 
1016ce54993dSAlex Elder /* Reset and reconfigure a channel, (possibly) enabling the doorbell engine */
gsi_channel_reset(struct gsi * gsi,u32 channel_id,bool doorbell)1017ce54993dSAlex Elder void gsi_channel_reset(struct gsi *gsi, u32 channel_id, bool doorbell)
1018650d1603SAlex Elder {
1019650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
1020650d1603SAlex Elder 
1021650d1603SAlex Elder 	mutex_lock(&gsi->mutex);
1022650d1603SAlex Elder 
1023650d1603SAlex Elder 	gsi_channel_reset_command(channel);
1024a3f2405bSAlex Elder 	/* Due to a hardware quirk we may need to reset RX channels twice. */
1025d7f3087bSAlex Elder 	if (gsi->version < IPA_VERSION_4_0 && !channel->toward_ipa)
1026650d1603SAlex Elder 		gsi_channel_reset_command(channel);
1027650d1603SAlex Elder 
10285fb859f7SAlex Elder 	/* Hardware assumes this is 0 following reset */
10295fb859f7SAlex Elder 	channel->tre_ring.index = 0;
1030ce54993dSAlex Elder 	gsi_channel_program(channel, doorbell);
1031650d1603SAlex Elder 	gsi_channel_trans_cancel_pending(channel);
1032650d1603SAlex Elder 
1033650d1603SAlex Elder 	mutex_unlock(&gsi->mutex);
1034650d1603SAlex Elder }
1035650d1603SAlex Elder 
1036decfef0fSAlex Elder /* Stop a started channel for suspend */
gsi_channel_suspend(struct gsi * gsi,u32 channel_id)1037decfef0fSAlex Elder int gsi_channel_suspend(struct gsi *gsi, u32 channel_id)
1038650d1603SAlex Elder {
1039650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
1040b1750723SAlex Elder 	int ret;
1041650d1603SAlex Elder 
10424a4ba483SAlex Elder 	ret = __gsi_channel_stop(channel, true);
1043b1750723SAlex Elder 	if (ret)
1044b1750723SAlex Elder 		return ret;
1045b1750723SAlex Elder 
1046b1750723SAlex Elder 	/* Ensure NAPI polling has finished. */
1047b1750723SAlex Elder 	napi_synchronize(&channel->napi);
1048b1750723SAlex Elder 
1049b1750723SAlex Elder 	return 0;
1050650d1603SAlex Elder }
1051650d1603SAlex Elder 
1052decfef0fSAlex Elder /* Resume a suspended channel (starting if stopped) */
gsi_channel_resume(struct gsi * gsi,u32 channel_id)1053decfef0fSAlex Elder int gsi_channel_resume(struct gsi *gsi, u32 channel_id)
1054650d1603SAlex Elder {
1055650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
1056650d1603SAlex Elder 
10574a4ba483SAlex Elder 	return __gsi_channel_start(channel, true);
1058650d1603SAlex Elder }
1059650d1603SAlex Elder 
106045a42a3cSAlex Elder /* Prevent all GSI interrupts while suspended */
gsi_suspend(struct gsi * gsi)106145a42a3cSAlex Elder void gsi_suspend(struct gsi *gsi)
106245a42a3cSAlex Elder {
106345a42a3cSAlex Elder 	disable_irq(gsi->irq);
106445a42a3cSAlex Elder }
106545a42a3cSAlex Elder 
106645a42a3cSAlex Elder /* Allow all GSI interrupts again when resuming */
gsi_resume(struct gsi * gsi)106745a42a3cSAlex Elder void gsi_resume(struct gsi *gsi)
106845a42a3cSAlex Elder {
106945a42a3cSAlex Elder 	enable_irq(gsi->irq);
107045a42a3cSAlex Elder }
107145a42a3cSAlex Elder 
gsi_trans_tx_committed(struct gsi_trans * trans)10724e0f28e9SAlex Elder void gsi_trans_tx_committed(struct gsi_trans *trans)
10734e0f28e9SAlex Elder {
10744e0f28e9SAlex Elder 	struct gsi_channel *channel = &trans->gsi->channel[trans->channel_id];
10754e0f28e9SAlex Elder 
10764e0f28e9SAlex Elder 	channel->trans_count++;
10774e0f28e9SAlex Elder 	channel->byte_count += trans->len;
107865d39497SAlex Elder 
107965d39497SAlex Elder 	trans->trans_count = channel->trans_count;
108065d39497SAlex Elder 	trans->byte_count = channel->byte_count;
10814e0f28e9SAlex Elder }
10824e0f28e9SAlex Elder 
gsi_trans_tx_queued(struct gsi_trans * trans)1083bcec9ecbSAlex Elder void gsi_trans_tx_queued(struct gsi_trans *trans)
1084650d1603SAlex Elder {
1085bcec9ecbSAlex Elder 	u32 channel_id = trans->channel_id;
1086bcec9ecbSAlex Elder 	struct gsi *gsi = trans->gsi;
1087bcec9ecbSAlex Elder 	struct gsi_channel *channel;
1088650d1603SAlex Elder 	u32 trans_count;
1089650d1603SAlex Elder 	u32 byte_count;
1090650d1603SAlex Elder 
1091bcec9ecbSAlex Elder 	channel = &gsi->channel[channel_id];
1092bcec9ecbSAlex Elder 
1093650d1603SAlex Elder 	byte_count = channel->byte_count - channel->queued_byte_count;
1094650d1603SAlex Elder 	trans_count = channel->trans_count - channel->queued_trans_count;
1095650d1603SAlex Elder 	channel->queued_byte_count = channel->byte_count;
1096650d1603SAlex Elder 	channel->queued_trans_count = channel->trans_count;
1097650d1603SAlex Elder 
1098bcec9ecbSAlex Elder 	ipa_gsi_channel_tx_queued(gsi, channel_id, trans_count, byte_count);
1099650d1603SAlex Elder }
1100650d1603SAlex Elder 
1101650d1603SAlex Elder /**
1102c5bddecbSAlex Elder  * gsi_trans_tx_completed() - Report completed TX transactions
1103c5bddecbSAlex Elder  * @trans:	TX channel transaction that has completed
1104650d1603SAlex Elder  *
1105c5bddecbSAlex Elder  * Report that a transaction on a TX channel has completed.  At the time a
1106c5bddecbSAlex Elder  * transaction is committed, we record *in the transaction* its channel's
1107c5bddecbSAlex Elder  * committed transaction and byte counts.  Transactions are completed in
1108c5bddecbSAlex Elder  * order, and the difference between the channel's byte/transaction count
1109c5bddecbSAlex Elder  * when the transaction was committed and when it completes tells us
1110c5bddecbSAlex Elder  * exactly how much data has been transferred while the transaction was
1111c5bddecbSAlex Elder  * pending.
1112650d1603SAlex Elder  *
1113c5bddecbSAlex Elder  * We report this information to the network stack, which uses it to manage
1114c5bddecbSAlex Elder  * the rate at which data is sent to hardware.
1115650d1603SAlex Elder  */
gsi_trans_tx_completed(struct gsi_trans * trans)1116c5bddecbSAlex Elder static void gsi_trans_tx_completed(struct gsi_trans *trans)
1117650d1603SAlex Elder {
1118c5bddecbSAlex Elder 	u32 channel_id = trans->channel_id;
1119c5bddecbSAlex Elder 	struct gsi *gsi = trans->gsi;
1120c5bddecbSAlex Elder 	struct gsi_channel *channel;
1121c5bddecbSAlex Elder 	u32 trans_count;
1122c5bddecbSAlex Elder 	u32 byte_count;
1123c5bddecbSAlex Elder 
1124c5bddecbSAlex Elder 	channel = &gsi->channel[channel_id];
1125c5bddecbSAlex Elder 	trans_count = trans->trans_count - channel->compl_trans_count;
1126c5bddecbSAlex Elder 	byte_count = trans->byte_count - channel->compl_byte_count;
1127650d1603SAlex Elder 
1128650d1603SAlex Elder 	channel->compl_trans_count += trans_count;
112965d39497SAlex Elder 	channel->compl_byte_count += byte_count;
1130650d1603SAlex Elder 
1131c5bddecbSAlex Elder 	ipa_gsi_channel_tx_completed(gsi, channel_id, trans_count, byte_count);
1132650d1603SAlex Elder }
1133650d1603SAlex Elder 
1134650d1603SAlex Elder /* Channel control interrupt handler */
gsi_isr_chan_ctrl(struct gsi * gsi)1135650d1603SAlex Elder static void gsi_isr_chan_ctrl(struct gsi *gsi)
1136650d1603SAlex Elder {
11377ba51aa2SAlex Elder 	const struct reg *reg;
1138650d1603SAlex Elder 	u32 channel_mask;
1139650d1603SAlex Elder 
11407ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_SRC_CH_IRQ);
11417ba51aa2SAlex Elder 	channel_mask = ioread32(gsi->virt + reg_offset(reg));
11427ba51aa2SAlex Elder 
11437ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_SRC_CH_IRQ_CLR);
11447ba51aa2SAlex Elder 	iowrite32(channel_mask, gsi->virt + reg_offset(reg));
1145650d1603SAlex Elder 
1146650d1603SAlex Elder 	while (channel_mask) {
1147650d1603SAlex Elder 		u32 channel_id = __ffs(channel_mask);
1148650d1603SAlex Elder 
1149650d1603SAlex Elder 		channel_mask ^= BIT(channel_id);
1150650d1603SAlex Elder 
11517ece9eaaSAlex Elder 		complete(&gsi->completion);
1152650d1603SAlex Elder 	}
1153650d1603SAlex Elder }
1154650d1603SAlex Elder 
1155650d1603SAlex Elder /* Event ring control interrupt handler */
gsi_isr_evt_ctrl(struct gsi * gsi)1156650d1603SAlex Elder static void gsi_isr_evt_ctrl(struct gsi *gsi)
1157650d1603SAlex Elder {
11587ba51aa2SAlex Elder 	const struct reg *reg;
1159650d1603SAlex Elder 	u32 event_mask;
1160650d1603SAlex Elder 
11617ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_SRC_EV_CH_IRQ);
11627ba51aa2SAlex Elder 	event_mask = ioread32(gsi->virt + reg_offset(reg));
11637ba51aa2SAlex Elder 
11647ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_SRC_EV_CH_IRQ_CLR);
11657ba51aa2SAlex Elder 	iowrite32(event_mask, gsi->virt + reg_offset(reg));
1166650d1603SAlex Elder 
1167650d1603SAlex Elder 	while (event_mask) {
1168650d1603SAlex Elder 		u32 evt_ring_id = __ffs(event_mask);
1169650d1603SAlex Elder 
1170650d1603SAlex Elder 		event_mask ^= BIT(evt_ring_id);
1171650d1603SAlex Elder 
11727ece9eaaSAlex Elder 		complete(&gsi->completion);
1173650d1603SAlex Elder 	}
1174650d1603SAlex Elder }
1175650d1603SAlex Elder 
1176650d1603SAlex Elder /* Global channel error interrupt handler */
1177650d1603SAlex Elder static void
gsi_isr_glob_chan_err(struct gsi * gsi,u32 err_ee,u32 channel_id,u32 code)1178650d1603SAlex Elder gsi_isr_glob_chan_err(struct gsi *gsi, u32 err_ee, u32 channel_id, u32 code)
1179650d1603SAlex Elder {
11807b0ac8f6SAlex Elder 	if (code == GSI_OUT_OF_RESOURCES) {
1181650d1603SAlex Elder 		dev_err(gsi->dev, "channel %u out of resources\n", channel_id);
11827ece9eaaSAlex Elder 		complete(&gsi->completion);
1183650d1603SAlex Elder 		return;
1184650d1603SAlex Elder 	}
1185650d1603SAlex Elder 
1186650d1603SAlex Elder 	/* Report, but otherwise ignore all other error codes */
1187650d1603SAlex Elder 	dev_err(gsi->dev, "channel %u global error ee 0x%08x code 0x%08x\n",
1188650d1603SAlex Elder 		channel_id, err_ee, code);
1189650d1603SAlex Elder }
1190650d1603SAlex Elder 
1191650d1603SAlex Elder /* Global event error interrupt handler */
1192650d1603SAlex Elder static void
gsi_isr_glob_evt_err(struct gsi * gsi,u32 err_ee,u32 evt_ring_id,u32 code)1193650d1603SAlex Elder gsi_isr_glob_evt_err(struct gsi *gsi, u32 err_ee, u32 evt_ring_id, u32 code)
1194650d1603SAlex Elder {
11957b0ac8f6SAlex Elder 	if (code == GSI_OUT_OF_RESOURCES) {
1196650d1603SAlex Elder 		struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
1197650d1603SAlex Elder 		u32 channel_id = gsi_channel_id(evt_ring->channel);
1198650d1603SAlex Elder 
11997ece9eaaSAlex Elder 		complete(&gsi->completion);
1200650d1603SAlex Elder 		dev_err(gsi->dev, "evt_ring for channel %u out of resources\n",
1201650d1603SAlex Elder 			channel_id);
1202650d1603SAlex Elder 		return;
1203650d1603SAlex Elder 	}
1204650d1603SAlex Elder 
1205650d1603SAlex Elder 	/* Report, but otherwise ignore all other error codes */
1206650d1603SAlex Elder 	dev_err(gsi->dev, "event ring %u global error ee %u code 0x%08x\n",
1207650d1603SAlex Elder 		evt_ring_id, err_ee, code);
1208650d1603SAlex Elder }
1209650d1603SAlex Elder 
1210650d1603SAlex Elder /* Global error interrupt handler */
gsi_isr_glob_err(struct gsi * gsi)1211650d1603SAlex Elder static void gsi_isr_glob_err(struct gsi *gsi)
1212650d1603SAlex Elder {
12133f3741c9SAlex Elder 	const struct reg *log_reg;
12143f3741c9SAlex Elder 	const struct reg *clr_reg;
1215650d1603SAlex Elder 	enum gsi_err_type type;
1216650d1603SAlex Elder 	enum gsi_err_code code;
12175791a73cSAlex Elder 	u32 offset;
1218650d1603SAlex Elder 	u32 which;
1219650d1603SAlex Elder 	u32 val;
1220650d1603SAlex Elder 	u32 ee;
1221650d1603SAlex Elder 
1222650d1603SAlex Elder 	/* Get the logged error, then reinitialize the log */
12233f3741c9SAlex Elder 	log_reg = gsi_reg(gsi, ERROR_LOG);
12243f3741c9SAlex Elder 	offset = reg_offset(log_reg);
12255791a73cSAlex Elder 	val = ioread32(gsi->virt + offset);
12265791a73cSAlex Elder 	iowrite32(0, gsi->virt + offset);
1227650d1603SAlex Elder 
12283f3741c9SAlex Elder 	clr_reg = gsi_reg(gsi, ERROR_LOG_CLR);
12293f3741c9SAlex Elder 	iowrite32(~0, gsi->virt + reg_offset(clr_reg));
12305791a73cSAlex Elder 
12315791a73cSAlex Elder 	/* Parse the error value */
12323f3741c9SAlex Elder 	ee = reg_decode(log_reg, ERR_EE, val);
12333f3741c9SAlex Elder 	type = reg_decode(log_reg, ERR_TYPE, val);
12343f3741c9SAlex Elder 	which = reg_decode(log_reg, ERR_VIRT_IDX, val);
12353f3741c9SAlex Elder 	code = reg_decode(log_reg, ERR_CODE, val);
1236650d1603SAlex Elder 
1237650d1603SAlex Elder 	if (type == GSI_ERR_TYPE_CHAN)
1238650d1603SAlex Elder 		gsi_isr_glob_chan_err(gsi, ee, which, code);
1239650d1603SAlex Elder 	else if (type == GSI_ERR_TYPE_EVT)
1240650d1603SAlex Elder 		gsi_isr_glob_evt_err(gsi, ee, which, code);
1241650d1603SAlex Elder 	else	/* type GSI_ERR_TYPE_GLOB should be fatal */
1242650d1603SAlex Elder 		dev_err(gsi->dev, "unexpected global error 0x%08x\n", type);
1243650d1603SAlex Elder }
1244650d1603SAlex Elder 
1245650d1603SAlex Elder /* Generic EE interrupt handler */
gsi_isr_gp_int1(struct gsi * gsi)1246650d1603SAlex Elder static void gsi_isr_gp_int1(struct gsi *gsi)
1247650d1603SAlex Elder {
12487ba51aa2SAlex Elder 	const struct reg *reg;
1249650d1603SAlex Elder 	u32 result;
1250650d1603SAlex Elder 	u32 val;
1251650d1603SAlex Elder 
12524c9d631aSAlex Elder 	/* This interrupt is used to handle completions of GENERIC GSI
12534c9d631aSAlex Elder 	 * commands.  We use these to allocate and halt channels on the
12544c9d631aSAlex Elder 	 * modem's behalf due to a hardware quirk on IPA v4.2.  The modem
12554c9d631aSAlex Elder 	 * "owns" channels even when the AP allocates them, and have no
12564c9d631aSAlex Elder 	 * way of knowing whether a modem channel's state has been changed.
12574c9d631aSAlex Elder 	 *
12584c9d631aSAlex Elder 	 * We also use GENERIC commands to enable/disable channel flow
12594c9d631aSAlex Elder 	 * control for IPA v4.2+.
1260f849afccSAlex Elder 	 *
1261f849afccSAlex Elder 	 * It is recommended that we halt the modem channels we allocated
1262f849afccSAlex Elder 	 * when shutting down, but it's possible the channel isn't running
1263f849afccSAlex Elder 	 * at the time we issue the HALT command.  We'll get an error in
1264f849afccSAlex Elder 	 * that case, but it's harmless (the channel is already halted).
12654c9d631aSAlex Elder 	 * Similarly, we could get an error back when updating flow control
12664c9d631aSAlex Elder 	 * on a channel because it's not in the proper state.
1267f849afccSAlex Elder 	 *
1268c9d92cf2SAlex Elder 	 * In either case, we silently ignore a INCORRECT_CHANNEL_STATE
1269c9d92cf2SAlex Elder 	 * error if we receive it.
1270f849afccSAlex Elder 	 */
12717ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_SCRATCH_0);
12727ba51aa2SAlex Elder 	val = ioread32(gsi->virt + reg_offset(reg));
12733f3741c9SAlex Elder 	result = reg_decode(reg, GENERIC_EE_RESULT, val);
1274f849afccSAlex Elder 
1275f849afccSAlex Elder 	switch (result) {
1276f849afccSAlex Elder 	case GENERIC_EE_SUCCESS:
1277c9d92cf2SAlex Elder 	case GENERIC_EE_INCORRECT_CHANNEL_STATE:
127811361456SAlex Elder 		gsi->result = 0;
127911361456SAlex Elder 		break;
128011361456SAlex Elder 
128111361456SAlex Elder 	case GENERIC_EE_RETRY:
128211361456SAlex Elder 		gsi->result = -EAGAIN;
1283f849afccSAlex Elder 		break;
1284f849afccSAlex Elder 
1285f849afccSAlex Elder 	default:
1286650d1603SAlex Elder 		dev_err(gsi->dev, "global INT1 generic result %u\n", result);
128711361456SAlex Elder 		gsi->result = -EIO;
1288f849afccSAlex Elder 		break;
1289f849afccSAlex Elder 	}
1290650d1603SAlex Elder 
1291650d1603SAlex Elder 	complete(&gsi->completion);
1292650d1603SAlex Elder }
12930b1ba18aSAlex Elder 
1294650d1603SAlex Elder /* Inter-EE interrupt handler */
gsi_isr_glob_ee(struct gsi * gsi)1295650d1603SAlex Elder static void gsi_isr_glob_ee(struct gsi *gsi)
1296650d1603SAlex Elder {
12977ba51aa2SAlex Elder 	const struct reg *reg;
1298650d1603SAlex Elder 	u32 val;
1299650d1603SAlex Elder 
13007ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_GLOB_IRQ_STTS);
13017ba51aa2SAlex Elder 	val = ioread32(gsi->virt + reg_offset(reg));
1302650d1603SAlex Elder 
1303c5ebba75SAlex Elder 	if (val & ERROR_INT)
1304650d1603SAlex Elder 		gsi_isr_glob_err(gsi);
1305650d1603SAlex Elder 
13067ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_GLOB_IRQ_CLR);
13077ba51aa2SAlex Elder 	iowrite32(val, gsi->virt + reg_offset(reg));
1308650d1603SAlex Elder 
1309c5ebba75SAlex Elder 	val &= ~ERROR_INT;
1310650d1603SAlex Elder 
1311c5ebba75SAlex Elder 	if (val & GP_INT1) {
1312c5ebba75SAlex Elder 		val ^= GP_INT1;
1313650d1603SAlex Elder 		gsi_isr_gp_int1(gsi);
1314650d1603SAlex Elder 	}
1315650d1603SAlex Elder 
1316650d1603SAlex Elder 	if (val)
1317650d1603SAlex Elder 		dev_err(gsi->dev, "unexpected global interrupt 0x%08x\n", val);
1318650d1603SAlex Elder }
1319650d1603SAlex Elder 
1320650d1603SAlex Elder /* I/O completion interrupt event */
gsi_isr_ieob(struct gsi * gsi)1321650d1603SAlex Elder static void gsi_isr_ieob(struct gsi *gsi)
1322650d1603SAlex Elder {
13237ba51aa2SAlex Elder 	const struct reg *reg;
1324650d1603SAlex Elder 	u32 event_mask;
1325650d1603SAlex Elder 
13267ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_SRC_IEOB_IRQ);
13277ba51aa2SAlex Elder 	event_mask = ioread32(gsi->virt + reg_offset(reg));
13287ba51aa2SAlex Elder 
13297bd9785fSAlex Elder 	gsi_irq_ieob_disable(gsi, event_mask);
13307ba51aa2SAlex Elder 
13317ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_SRC_IEOB_IRQ_CLR);
13327ba51aa2SAlex Elder 	iowrite32(event_mask, gsi->virt + reg_offset(reg));
1333650d1603SAlex Elder 
1334650d1603SAlex Elder 	while (event_mask) {
1335650d1603SAlex Elder 		u32 evt_ring_id = __ffs(event_mask);
1336650d1603SAlex Elder 
1337650d1603SAlex Elder 		event_mask ^= BIT(evt_ring_id);
1338650d1603SAlex Elder 
1339650d1603SAlex Elder 		napi_schedule(&gsi->evt_ring[evt_ring_id].channel->napi);
1340650d1603SAlex Elder 	}
1341650d1603SAlex Elder }
1342650d1603SAlex Elder 
1343650d1603SAlex Elder /* General event interrupts represent serious problems, so report them */
gsi_isr_general(struct gsi * gsi)1344650d1603SAlex Elder static void gsi_isr_general(struct gsi *gsi)
1345650d1603SAlex Elder {
1346650d1603SAlex Elder 	struct device *dev = gsi->dev;
13477ba51aa2SAlex Elder 	const struct reg *reg;
1348650d1603SAlex Elder 	u32 val;
1349650d1603SAlex Elder 
13507ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_GSI_IRQ_STTS);
13517ba51aa2SAlex Elder 	val = ioread32(gsi->virt + reg_offset(reg));
13527ba51aa2SAlex Elder 
13537ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_GSI_IRQ_CLR);
13547ba51aa2SAlex Elder 	iowrite32(val, gsi->virt + reg_offset(reg));
1355650d1603SAlex Elder 
1356650d1603SAlex Elder 	dev_err(dev, "unexpected general interrupt 0x%08x\n", val);
1357650d1603SAlex Elder }
1358650d1603SAlex Elder 
1359650d1603SAlex Elder /**
1360650d1603SAlex Elder  * gsi_isr() - Top level GSI interrupt service routine
1361650d1603SAlex Elder  * @irq:	Interrupt number (ignored)
1362650d1603SAlex Elder  * @dev_id:	GSI pointer supplied to request_irq()
1363650d1603SAlex Elder  *
1364650d1603SAlex Elder  * This is the main handler function registered for the GSI IRQ. Each type
1365650d1603SAlex Elder  * of interrupt has a separate handler function that is called from here.
1366650d1603SAlex Elder  */
gsi_isr(int irq,void * dev_id)1367650d1603SAlex Elder static irqreturn_t gsi_isr(int irq, void *dev_id)
1368650d1603SAlex Elder {
1369650d1603SAlex Elder 	struct gsi *gsi = dev_id;
13707ba51aa2SAlex Elder 	const struct reg *reg;
1371650d1603SAlex Elder 	u32 intr_mask;
1372650d1603SAlex Elder 	u32 cnt = 0;
13737ba51aa2SAlex Elder 	u32 offset;
13747ba51aa2SAlex Elder 
13757ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_TYPE_IRQ);
13767ba51aa2SAlex Elder 	offset = reg_offset(reg);
1377650d1603SAlex Elder 
1378f9b28804SAlex Elder 	/* enum gsi_irq_type_id defines GSI interrupt types */
13797ba51aa2SAlex Elder 	while ((intr_mask = ioread32(gsi->virt + offset))) {
1380650d1603SAlex Elder 		/* intr_mask contains bitmask of pending GSI interrupts */
1381650d1603SAlex Elder 		do {
1382650d1603SAlex Elder 			u32 gsi_intr = BIT(__ffs(intr_mask));
1383650d1603SAlex Elder 
1384650d1603SAlex Elder 			intr_mask ^= gsi_intr;
1385650d1603SAlex Elder 
13867ba51aa2SAlex Elder 			/* Note: the IRQ condition for each type is cleared
13877ba51aa2SAlex Elder 			 * when the type-specific register is updated.
13887ba51aa2SAlex Elder 			 */
1389650d1603SAlex Elder 			switch (gsi_intr) {
1390c5ebba75SAlex Elder 			case GSI_CH_CTRL:
1391650d1603SAlex Elder 				gsi_isr_chan_ctrl(gsi);
1392650d1603SAlex Elder 				break;
1393c5ebba75SAlex Elder 			case GSI_EV_CTRL:
1394650d1603SAlex Elder 				gsi_isr_evt_ctrl(gsi);
1395650d1603SAlex Elder 				break;
1396c5ebba75SAlex Elder 			case GSI_GLOB_EE:
1397650d1603SAlex Elder 				gsi_isr_glob_ee(gsi);
1398650d1603SAlex Elder 				break;
1399c5ebba75SAlex Elder 			case GSI_IEOB:
1400650d1603SAlex Elder 				gsi_isr_ieob(gsi);
1401650d1603SAlex Elder 				break;
1402c5ebba75SAlex Elder 			case GSI_GENERAL:
1403650d1603SAlex Elder 				gsi_isr_general(gsi);
1404650d1603SAlex Elder 				break;
1405650d1603SAlex Elder 			default:
1406650d1603SAlex Elder 				dev_err(gsi->dev,
14078463488aSAlex Elder 					"unrecognized interrupt type 0x%08x\n",
14088463488aSAlex Elder 					gsi_intr);
1409650d1603SAlex Elder 				break;
1410650d1603SAlex Elder 			}
1411650d1603SAlex Elder 		} while (intr_mask);
1412650d1603SAlex Elder 
1413650d1603SAlex Elder 		if (++cnt > GSI_ISR_MAX_ITER) {
1414650d1603SAlex Elder 			dev_err(gsi->dev, "interrupt flood\n");
1415650d1603SAlex Elder 			break;
1416650d1603SAlex Elder 		}
1417650d1603SAlex Elder 	}
1418650d1603SAlex Elder 
1419650d1603SAlex Elder 	return IRQ_HANDLED;
1420650d1603SAlex Elder }
1421650d1603SAlex Elder 
1422b176f95bSAlex Elder /* Init function for GSI IRQ lookup; there is no gsi_irq_exit() */
gsi_irq_init(struct gsi * gsi,struct platform_device * pdev)14230b8d6761SAlex Elder static int gsi_irq_init(struct gsi *gsi, struct platform_device *pdev)
14240b8d6761SAlex Elder {
14250b8d6761SAlex Elder 	int ret;
14260b8d6761SAlex Elder 
14270b8d6761SAlex Elder 	ret = platform_get_irq_byname(pdev, "gsi");
142891306d1dSZihao Tang 	if (ret <= 0)
14290b8d6761SAlex Elder 		return ret ? : -EINVAL;
143091306d1dSZihao Tang 
1431b176f95bSAlex Elder 	gsi->irq = ret;
14320b8d6761SAlex Elder 
14330b8d6761SAlex Elder 	return 0;
14340b8d6761SAlex Elder }
14350b8d6761SAlex Elder 
1436650d1603SAlex Elder /* Return the transaction associated with a transfer completion event */
14377dd9558fSAlex Elder static struct gsi_trans *
gsi_event_trans(struct gsi * gsi,struct gsi_event * event)14387dd9558fSAlex Elder gsi_event_trans(struct gsi *gsi, struct gsi_event *event)
1439650d1603SAlex Elder {
14407dd9558fSAlex Elder 	u32 channel_id = event->chid;
14417dd9558fSAlex Elder 	struct gsi_channel *channel;
14427dd9558fSAlex Elder 	struct gsi_trans *trans;
1443650d1603SAlex Elder 	u32 tre_offset;
1444650d1603SAlex Elder 	u32 tre_index;
1445650d1603SAlex Elder 
14467dd9558fSAlex Elder 	channel = &gsi->channel[channel_id];
14477dd9558fSAlex Elder 	if (WARN(!channel->gsi, "event has bad channel %u\n", channel_id))
14487dd9558fSAlex Elder 		return NULL;
14497dd9558fSAlex Elder 
1450650d1603SAlex Elder 	/* Event xfer_ptr records the TRE it's associated with */
14513c54b7beSAlex Elder 	tre_offset = lower_32_bits(le64_to_cpu(event->xfer_ptr));
1452650d1603SAlex Elder 	tre_index = gsi_ring_index(&channel->tre_ring, tre_offset);
1453650d1603SAlex Elder 
14547dd9558fSAlex Elder 	trans = gsi_channel_trans_mapped(channel, tre_index);
14557dd9558fSAlex Elder 
14567dd9558fSAlex Elder 	if (WARN(!trans, "channel %u event with no transaction\n", channel_id))
14577dd9558fSAlex Elder 		return NULL;
14587dd9558fSAlex Elder 
14597dd9558fSAlex Elder 	return trans;
1460650d1603SAlex Elder }
1461650d1603SAlex Elder 
1462650d1603SAlex Elder /**
146381765eeaSAlex Elder  * gsi_evt_ring_update() - Update transaction state from hardware
14642f48fb0eSAlex Elder  * @gsi:		GSI pointer
14652f48fb0eSAlex Elder  * @evt_ring_id:	Event ring ID
1466650d1603SAlex Elder  * @index:		Event index in ring reported by hardware
1467650d1603SAlex Elder  *
1468650d1603SAlex Elder  * Events for RX channels contain the actual number of bytes received into
1469650d1603SAlex Elder  * the buffer.  Every event has a transaction associated with it, and here
1470650d1603SAlex Elder  * we update transactions to record their actual received lengths.
1471650d1603SAlex Elder  *
147281765eeaSAlex Elder  * When an event for a TX channel arrives we use information in the
1473ace5dc61SAlex Elder  * transaction to report the number of requests and bytes that have
1474ace5dc61SAlex Elder  * been transferred.
147581765eeaSAlex Elder  *
1476650d1603SAlex Elder  * This function is called whenever we learn that the GSI hardware has filled
1477650d1603SAlex Elder  * new events since the last time we checked.  The ring's index field tells
1478650d1603SAlex Elder  * the first entry in need of processing.  The index provided is the
1479650d1603SAlex Elder  * first *unfilled* event in the ring (following the last filled one).
1480650d1603SAlex Elder  *
1481650d1603SAlex Elder  * Events are sequential within the event ring, and transactions are
1482b63f507cSAlex Elder  * sequential within the transaction array.
1483650d1603SAlex Elder  *
1484650d1603SAlex Elder  * Note that @index always refers to an element *within* the event ring.
1485650d1603SAlex Elder  */
gsi_evt_ring_update(struct gsi * gsi,u32 evt_ring_id,u32 index)148681765eeaSAlex Elder static void gsi_evt_ring_update(struct gsi *gsi, u32 evt_ring_id, u32 index)
1487650d1603SAlex Elder {
14882f48fb0eSAlex Elder 	struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
1489650d1603SAlex Elder 	struct gsi_ring *ring = &evt_ring->ring;
1490650d1603SAlex Elder 	struct gsi_event *event_done;
1491650d1603SAlex Elder 	struct gsi_event *event;
1492650d1603SAlex Elder 	u32 event_avail;
1493d8290cbeSAlex Elder 	u32 old_index;
1494650d1603SAlex Elder 
149581765eeaSAlex Elder 	/* Starting with the oldest un-processed event, determine which
149681765eeaSAlex Elder 	 * transaction (and which channel) is associated with the event.
149781765eeaSAlex Elder 	 * For RX channels, update each completed transaction with the
149881765eeaSAlex Elder 	 * number of bytes that were actually received.  For TX channels
149981765eeaSAlex Elder 	 * associated with a network device, report to the network stack
150081765eeaSAlex Elder 	 * the number of transfers and bytes this completion represents.
1501650d1603SAlex Elder 	 */
1502650d1603SAlex Elder 	old_index = ring->index;
1503650d1603SAlex Elder 	event = gsi_ring_virt(ring, old_index);
1504650d1603SAlex Elder 
1505650d1603SAlex Elder 	/* Compute the number of events to process before we wrap,
1506650d1603SAlex Elder 	 * and determine when we'll be done processing events.
1507650d1603SAlex Elder 	 */
1508650d1603SAlex Elder 	event_avail = ring->count - old_index % ring->count;
1509650d1603SAlex Elder 	event_done = gsi_ring_virt(ring, index);
1510650d1603SAlex Elder 	do {
1511dd5a046cSAlex Elder 		struct gsi_trans *trans;
1512dd5a046cSAlex Elder 
15132f48fb0eSAlex Elder 		trans = gsi_event_trans(gsi, event);
1514dd5a046cSAlex Elder 		if (!trans)
1515dd5a046cSAlex Elder 			return;
1516dd5a046cSAlex Elder 
15179f1c3ad6SAlex Elder 		if (trans->direction == DMA_FROM_DEVICE)
1518650d1603SAlex Elder 			trans->len = __le16_to_cpu(event->len);
151981765eeaSAlex Elder 		else
152081765eeaSAlex Elder 			gsi_trans_tx_completed(trans);
152181765eeaSAlex Elder 
152281765eeaSAlex Elder 		gsi_trans_move_complete(trans);
1523650d1603SAlex Elder 
1524650d1603SAlex Elder 		/* Move on to the next event and transaction */
1525650d1603SAlex Elder 		if (--event_avail)
1526650d1603SAlex Elder 			event++;
1527650d1603SAlex Elder 		else
1528650d1603SAlex Elder 			event = gsi_ring_virt(ring, 0);
1529650d1603SAlex Elder 	} while (event != event_done);
153081765eeaSAlex Elder 
153181765eeaSAlex Elder 	/* Tell the hardware we've handled these events */
153281765eeaSAlex Elder 	gsi_evt_ring_doorbell(gsi, evt_ring_id, index);
1533650d1603SAlex Elder }
1534650d1603SAlex Elder 
1535650d1603SAlex Elder /* Initialize a ring, including allocating DMA memory for its entries */
gsi_ring_alloc(struct gsi * gsi,struct gsi_ring * ring,u32 count)1536650d1603SAlex Elder static int gsi_ring_alloc(struct gsi *gsi, struct gsi_ring *ring, u32 count)
1537650d1603SAlex Elder {
1538437c78f9SAlex Elder 	u32 size = count * GSI_RING_ELEMENT_SIZE;
1539650d1603SAlex Elder 	struct device *dev = gsi->dev;
1540650d1603SAlex Elder 	dma_addr_t addr;
1541650d1603SAlex Elder 
1542437c78f9SAlex Elder 	/* Hardware requires a 2^n ring size, with alignment equal to size.
154319aaf72cSAlex Elder 	 * The DMA address returned by dma_alloc_coherent() is guaranteed to
154419aaf72cSAlex Elder 	 * be a power-of-2 number of pages, which satisfies the requirement.
1545437c78f9SAlex Elder 	 */
1546650d1603SAlex Elder 	ring->virt = dma_alloc_coherent(dev, size, &addr, GFP_KERNEL);
154719aaf72cSAlex Elder 	if (!ring->virt)
1548650d1603SAlex Elder 		return -ENOMEM;
154919aaf72cSAlex Elder 
1550650d1603SAlex Elder 	ring->addr = addr;
1551650d1603SAlex Elder 	ring->count = count;
15525fb859f7SAlex Elder 	ring->index = 0;
1553650d1603SAlex Elder 
1554650d1603SAlex Elder 	return 0;
1555650d1603SAlex Elder }
1556650d1603SAlex Elder 
1557650d1603SAlex Elder /* Free a previously-allocated ring */
gsi_ring_free(struct gsi * gsi,struct gsi_ring * ring)1558650d1603SAlex Elder static void gsi_ring_free(struct gsi *gsi, struct gsi_ring *ring)
1559650d1603SAlex Elder {
1560650d1603SAlex Elder 	size_t size = ring->count * GSI_RING_ELEMENT_SIZE;
1561650d1603SAlex Elder 
1562650d1603SAlex Elder 	dma_free_coherent(gsi->dev, size, ring->virt, ring->addr);
1563650d1603SAlex Elder }
1564650d1603SAlex Elder 
1565650d1603SAlex Elder /* Allocate an available event ring id */
gsi_evt_ring_id_alloc(struct gsi * gsi)1566650d1603SAlex Elder static int gsi_evt_ring_id_alloc(struct gsi *gsi)
1567650d1603SAlex Elder {
1568650d1603SAlex Elder 	u32 evt_ring_id;
1569650d1603SAlex Elder 
1570650d1603SAlex Elder 	if (gsi->event_bitmap == ~0U) {
1571650d1603SAlex Elder 		dev_err(gsi->dev, "event rings exhausted\n");
1572650d1603SAlex Elder 		return -ENOSPC;
1573650d1603SAlex Elder 	}
1574650d1603SAlex Elder 
1575650d1603SAlex Elder 	evt_ring_id = ffz(gsi->event_bitmap);
1576650d1603SAlex Elder 	gsi->event_bitmap |= BIT(evt_ring_id);
1577650d1603SAlex Elder 
1578650d1603SAlex Elder 	return (int)evt_ring_id;
1579650d1603SAlex Elder }
1580650d1603SAlex Elder 
1581650d1603SAlex Elder /* Free a previously-allocated event ring id */
gsi_evt_ring_id_free(struct gsi * gsi,u32 evt_ring_id)1582650d1603SAlex Elder static void gsi_evt_ring_id_free(struct gsi *gsi, u32 evt_ring_id)
1583650d1603SAlex Elder {
1584650d1603SAlex Elder 	gsi->event_bitmap &= ~BIT(evt_ring_id);
1585650d1603SAlex Elder }
1586650d1603SAlex Elder 
1587650d1603SAlex Elder /* Ring a channel doorbell, reporting the first un-filled entry */
gsi_channel_doorbell(struct gsi_channel * channel)1588650d1603SAlex Elder void gsi_channel_doorbell(struct gsi_channel *channel)
1589650d1603SAlex Elder {
1590650d1603SAlex Elder 	struct gsi_ring *tre_ring = &channel->tre_ring;
1591650d1603SAlex Elder 	u32 channel_id = gsi_channel_id(channel);
1592650d1603SAlex Elder 	struct gsi *gsi = channel->gsi;
159376924eb9SAlex Elder 	const struct reg *reg;
1594650d1603SAlex Elder 	u32 val;
1595650d1603SAlex Elder 
159676924eb9SAlex Elder 	reg = gsi_reg(gsi, CH_C_DOORBELL_0);
1597650d1603SAlex Elder 	/* Note: index *must* be used modulo the ring count here */
1598650d1603SAlex Elder 	val = gsi_ring_addr(tre_ring, tre_ring->index % tre_ring->count);
159976924eb9SAlex Elder 	iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
1600650d1603SAlex Elder }
1601650d1603SAlex Elder 
1602ace5dc61SAlex Elder /* Consult hardware, move newly completed transactions to completed state */
gsi_channel_update(struct gsi_channel * channel)1603019e37eaSAlex Elder void gsi_channel_update(struct gsi_channel *channel)
1604650d1603SAlex Elder {
1605650d1603SAlex Elder 	u32 evt_ring_id = channel->evt_ring_id;
1606650d1603SAlex Elder 	struct gsi *gsi = channel->gsi;
1607650d1603SAlex Elder 	struct gsi_evt_ring *evt_ring;
1608650d1603SAlex Elder 	struct gsi_trans *trans;
1609650d1603SAlex Elder 	struct gsi_ring *ring;
1610d1ce6395SAlex Elder 	const struct reg *reg;
1611650d1603SAlex Elder 	u32 offset;
1612650d1603SAlex Elder 	u32 index;
1613650d1603SAlex Elder 
1614650d1603SAlex Elder 	evt_ring = &gsi->evt_ring[evt_ring_id];
1615650d1603SAlex Elder 	ring = &evt_ring->ring;
1616650d1603SAlex Elder 
1617650d1603SAlex Elder 	/* See if there's anything new to process; if not, we're done.  Note
1618650d1603SAlex Elder 	 * that index always refers to an entry *within* the event ring.
1619650d1603SAlex Elder 	 */
1620d1ce6395SAlex Elder 	reg = gsi_reg(gsi, EV_CH_E_CNTXT_4);
1621d1ce6395SAlex Elder 	offset = reg_n_offset(reg, evt_ring_id);
1622650d1603SAlex Elder 	index = gsi_ring_index(ring, ioread32(gsi->virt + offset));
1623650d1603SAlex Elder 	if (index == ring->index % ring->count)
1624019e37eaSAlex Elder 		return;
1625650d1603SAlex Elder 
1626c15f950dSAlex Elder 	/* Get the transaction for the latest completed event. */
16277dd9558fSAlex Elder 	trans = gsi_event_trans(gsi, gsi_ring_virt(ring, index - 1));
16287dd9558fSAlex Elder 	if (!trans)
1629019e37eaSAlex Elder 		return;
1630650d1603SAlex Elder 
1631650d1603SAlex Elder 	/* For RX channels, update each completed transaction with the number
1632650d1603SAlex Elder 	 * of bytes that were actually received.  For TX channels, report
1633650d1603SAlex Elder 	 * the number of transactions and bytes this completion represents
1634650d1603SAlex Elder 	 * up the network stack.
1635650d1603SAlex Elder 	 */
163681765eeaSAlex Elder 	gsi_evt_ring_update(gsi, evt_ring_id, index);
1637650d1603SAlex Elder }
1638650d1603SAlex Elder 
1639650d1603SAlex Elder /**
1640650d1603SAlex Elder  * gsi_channel_poll_one() - Return a single completed transaction on a channel
1641650d1603SAlex Elder  * @channel:	Channel to be polled
1642650d1603SAlex Elder  *
1643e3eea08eSAlex Elder  * Return:	Transaction pointer, or null if none are available
1644650d1603SAlex Elder  *
1645ace5dc61SAlex Elder  * This function returns the first of a channel's completed transactions.
1646ace5dc61SAlex Elder  * If no transactions are in completed state, the hardware is consulted to
1647ace5dc61SAlex Elder  * determine whether any new transactions have completed.  If so, they're
1648ace5dc61SAlex Elder  * moved to completed state and the first such transaction is returned.
1649ace5dc61SAlex Elder  * If there are no more completed transactions, a null pointer is returned.
1650650d1603SAlex Elder  */
gsi_channel_poll_one(struct gsi_channel * channel)1651650d1603SAlex Elder static struct gsi_trans *gsi_channel_poll_one(struct gsi_channel *channel)
1652650d1603SAlex Elder {
1653650d1603SAlex Elder 	struct gsi_trans *trans;
1654650d1603SAlex Elder 
1655ace5dc61SAlex Elder 	/* Get the first completed transaction */
1656650d1603SAlex Elder 	trans = gsi_channel_trans_complete(channel);
1657650d1603SAlex Elder 	if (trans)
1658650d1603SAlex Elder 		gsi_trans_move_polled(trans);
1659650d1603SAlex Elder 
1660650d1603SAlex Elder 	return trans;
1661650d1603SAlex Elder }
1662650d1603SAlex Elder 
1663650d1603SAlex Elder /**
1664650d1603SAlex Elder  * gsi_channel_poll() - NAPI poll function for a channel
1665650d1603SAlex Elder  * @napi:	NAPI structure for the channel
1666650d1603SAlex Elder  * @budget:	Budget supplied by NAPI core
1667e3eea08eSAlex Elder  *
1668e3eea08eSAlex Elder  * Return:	Number of items polled (<= budget)
1669650d1603SAlex Elder  *
1670650d1603SAlex Elder  * Single transactions completed by hardware are polled until either
1671650d1603SAlex Elder  * the budget is exhausted, or there are no more.  Each transaction
1672650d1603SAlex Elder  * polled is passed to gsi_trans_complete(), to perform remaining
1673650d1603SAlex Elder  * completion processing and retire/free the transaction.
1674650d1603SAlex Elder  */
gsi_channel_poll(struct napi_struct * napi,int budget)1675650d1603SAlex Elder static int gsi_channel_poll(struct napi_struct *napi, int budget)
1676650d1603SAlex Elder {
1677650d1603SAlex Elder 	struct gsi_channel *channel;
1678c80c4a1eSAlex Elder 	int count;
1679650d1603SAlex Elder 
1680650d1603SAlex Elder 	channel = container_of(napi, struct gsi_channel, napi);
1681c80c4a1eSAlex Elder 	for (count = 0; count < budget; count++) {
1682650d1603SAlex Elder 		struct gsi_trans *trans;
1683650d1603SAlex Elder 
1684650d1603SAlex Elder 		trans = gsi_channel_poll_one(channel);
1685650d1603SAlex Elder 		if (!trans)
1686650d1603SAlex Elder 			break;
1687650d1603SAlex Elder 		gsi_trans_complete(trans);
1688650d1603SAlex Elder 	}
1689650d1603SAlex Elder 
1690148604e7SAlex Elder 	if (count < budget && napi_complete(napi))
16915725593eSAlex Elder 		gsi_irq_ieob_enable_one(channel->gsi, channel->evt_ring_id);
1692650d1603SAlex Elder 
1693650d1603SAlex Elder 	return count;
1694650d1603SAlex Elder }
1695650d1603SAlex Elder 
1696650d1603SAlex Elder /* The event bitmap represents which event ids are available for allocation.
1697650d1603SAlex Elder  * Set bits are not available, clear bits can be used.  This function
1698650d1603SAlex Elder  * initializes the map so all events supported by the hardware are available,
1699650d1603SAlex Elder  * then precludes any reserved events from being allocated.
1700650d1603SAlex Elder  */
gsi_event_bitmap_init(u32 evt_ring_max)1701650d1603SAlex Elder static u32 gsi_event_bitmap_init(u32 evt_ring_max)
1702650d1603SAlex Elder {
1703650d1603SAlex Elder 	u32 event_bitmap = GENMASK(BITS_PER_LONG - 1, evt_ring_max);
1704650d1603SAlex Elder 
1705650d1603SAlex Elder 	event_bitmap |= GENMASK(GSI_MHI_EVENT_ID_END, GSI_MHI_EVENT_ID_START);
1706650d1603SAlex Elder 
1707650d1603SAlex Elder 	return event_bitmap;
1708650d1603SAlex Elder }
1709650d1603SAlex Elder 
1710650d1603SAlex Elder /* Setup function for a single channel */
gsi_channel_setup_one(struct gsi * gsi,u32 channel_id)1711d387c761SAlex Elder static int gsi_channel_setup_one(struct gsi *gsi, u32 channel_id)
1712650d1603SAlex Elder {
1713650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
1714650d1603SAlex Elder 	u32 evt_ring_id = channel->evt_ring_id;
1715650d1603SAlex Elder 	int ret;
1716650d1603SAlex Elder 
17176170b6daSAlex Elder 	if (!gsi_channel_initialized(channel))
17186170b6daSAlex Elder 		return 0;
1719650d1603SAlex Elder 
1720650d1603SAlex Elder 	ret = gsi_evt_ring_alloc_command(gsi, evt_ring_id);
1721650d1603SAlex Elder 	if (ret)
1722650d1603SAlex Elder 		return ret;
1723650d1603SAlex Elder 
1724650d1603SAlex Elder 	gsi_evt_ring_program(gsi, evt_ring_id);
1725650d1603SAlex Elder 
1726650d1603SAlex Elder 	ret = gsi_channel_alloc_command(gsi, channel_id);
1727650d1603SAlex Elder 	if (ret)
1728650d1603SAlex Elder 		goto err_evt_ring_de_alloc;
1729650d1603SAlex Elder 
1730d387c761SAlex Elder 	gsi_channel_program(channel, true);
1731650d1603SAlex Elder 
1732650d1603SAlex Elder 	if (channel->toward_ipa)
173316d083e2SJakub Kicinski 		netif_napi_add_tx(&gsi->dummy_dev, &channel->napi,
173416d083e2SJakub Kicinski 				  gsi_channel_poll);
1735650d1603SAlex Elder 	else
1736650d1603SAlex Elder 		netif_napi_add(&gsi->dummy_dev, &channel->napi,
1737b48b89f9SJakub Kicinski 			       gsi_channel_poll);
1738650d1603SAlex Elder 
1739650d1603SAlex Elder 	return 0;
1740650d1603SAlex Elder 
1741650d1603SAlex Elder err_evt_ring_de_alloc:
1742650d1603SAlex Elder 	/* We've done nothing with the event ring yet so don't reset */
1743650d1603SAlex Elder 	gsi_evt_ring_de_alloc_command(gsi, evt_ring_id);
1744650d1603SAlex Elder 
1745650d1603SAlex Elder 	return ret;
1746650d1603SAlex Elder }
1747650d1603SAlex Elder 
1748650d1603SAlex Elder /* Inverse of gsi_channel_setup_one() */
gsi_channel_teardown_one(struct gsi * gsi,u32 channel_id)1749650d1603SAlex Elder static void gsi_channel_teardown_one(struct gsi *gsi, u32 channel_id)
1750650d1603SAlex Elder {
1751650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
1752650d1603SAlex Elder 	u32 evt_ring_id = channel->evt_ring_id;
1753650d1603SAlex Elder 
17546170b6daSAlex Elder 	if (!gsi_channel_initialized(channel))
17556170b6daSAlex Elder 		return;
1756650d1603SAlex Elder 
1757650d1603SAlex Elder 	netif_napi_del(&channel->napi);
1758650d1603SAlex Elder 
1759650d1603SAlex Elder 	gsi_channel_de_alloc_command(gsi, channel_id);
1760650d1603SAlex Elder 	gsi_evt_ring_reset_command(gsi, evt_ring_id);
1761650d1603SAlex Elder 	gsi_evt_ring_de_alloc_command(gsi, evt_ring_id);
1762650d1603SAlex Elder }
1763650d1603SAlex Elder 
17644c9d631aSAlex Elder /* We use generic commands only to operate on modem channels.  We don't have
17654c9d631aSAlex Elder  * the ability to determine channel state for a modem channel, so we simply
17664c9d631aSAlex Elder  * issue the command and wait for it to complete.
17674c9d631aSAlex Elder  */
gsi_generic_command(struct gsi * gsi,u32 channel_id,enum gsi_generic_cmd_opcode opcode,u8 params)1768650d1603SAlex Elder static int gsi_generic_command(struct gsi *gsi, u32 channel_id,
1769fe68c43cSAlex Elder 			       enum gsi_generic_cmd_opcode opcode,
1770fe68c43cSAlex Elder 			       u8 params)
1771650d1603SAlex Elder {
17727ba51aa2SAlex Elder 	const struct reg *reg;
1773d9cbe818SAlex Elder 	bool timeout;
17747ba51aa2SAlex Elder 	u32 offset;
1775650d1603SAlex Elder 	u32 val;
1776650d1603SAlex Elder 
17774c9d631aSAlex Elder 	/* The error global interrupt type is always enabled (until we tear
17784c9d631aSAlex Elder 	 * down), so we will keep it enabled.
17794c9d631aSAlex Elder 	 *
17804c9d631aSAlex Elder 	 * A generic EE command completes with a GSI global interrupt of
17814c9d631aSAlex Elder 	 * type GP_INT1.  We only perform one generic command at a time
17824c9d631aSAlex Elder 	 * (to allocate, halt, or enable/disable flow control on a modem
17834c9d631aSAlex Elder 	 * channel), and only from this function.  So we enable the GP_INT1
17844c9d631aSAlex Elder 	 * IRQ type here, and disable it again after the command completes.
1785d6c9e3f5SAlex Elder 	 */
17867ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_GLOB_IRQ_EN);
1787c5ebba75SAlex Elder 	val = ERROR_INT | GP_INT1;
17887ba51aa2SAlex Elder 	iowrite32(val, gsi->virt + reg_offset(reg));
1789d6c9e3f5SAlex Elder 
17900b1ba18aSAlex Elder 	/* First zero the result code field */
17917ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_SCRATCH_0);
17927ba51aa2SAlex Elder 	offset = reg_offset(reg);
17937ba51aa2SAlex Elder 	val = ioread32(gsi->virt + offset);
17947ba51aa2SAlex Elder 
17953f3741c9SAlex Elder 	val &= ~reg_fmask(reg, GENERIC_EE_RESULT);
17967ba51aa2SAlex Elder 	iowrite32(val, gsi->virt + offset);
17970b1ba18aSAlex Elder 
17980b1ba18aSAlex Elder 	/* Now issue the command */
17995791a73cSAlex Elder 	reg = gsi_reg(gsi, GENERIC_CMD);
18003f3741c9SAlex Elder 	val = reg_encode(reg, GENERIC_OPCODE, opcode);
18013f3741c9SAlex Elder 	val |= reg_encode(reg, GENERIC_CHID, channel_id);
18023f3741c9SAlex Elder 	val |= reg_encode(reg, GENERIC_EE, GSI_EE_MODEM);
18032df181f0SAlex Elder 	if (gsi->version >= IPA_VERSION_4_11)
18043f3741c9SAlex Elder 		val |= reg_encode(reg, GENERIC_PARAMS, params);
1805650d1603SAlex Elder 
18065791a73cSAlex Elder 	timeout = !gsi_command(gsi, reg_offset(reg), val);
1807d6c9e3f5SAlex Elder 
1808d6c9e3f5SAlex Elder 	/* Disable the GP_INT1 IRQ type again */
18097ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_GLOB_IRQ_EN);
18107ba51aa2SAlex Elder 	iowrite32(ERROR_INT, gsi->virt + reg_offset(reg));
1811d6c9e3f5SAlex Elder 
1812d9cbe818SAlex Elder 	if (!timeout)
181311361456SAlex Elder 		return gsi->result;
1814650d1603SAlex Elder 
1815650d1603SAlex Elder 	dev_err(gsi->dev, "GSI generic command %u to channel %u timed out\n",
1816650d1603SAlex Elder 		opcode, channel_id);
1817650d1603SAlex Elder 
1818650d1603SAlex Elder 	return -ETIMEDOUT;
1819650d1603SAlex Elder }
1820650d1603SAlex Elder 
gsi_modem_channel_alloc(struct gsi * gsi,u32 channel_id)1821650d1603SAlex Elder static int gsi_modem_channel_alloc(struct gsi *gsi, u32 channel_id)
1822650d1603SAlex Elder {
1823650d1603SAlex Elder 	return gsi_generic_command(gsi, channel_id,
1824fe68c43cSAlex Elder 				   GSI_GENERIC_ALLOCATE_CHANNEL, 0);
1825650d1603SAlex Elder }
1826650d1603SAlex Elder 
gsi_modem_channel_halt(struct gsi * gsi,u32 channel_id)1827650d1603SAlex Elder static void gsi_modem_channel_halt(struct gsi *gsi, u32 channel_id)
1828650d1603SAlex Elder {
182911361456SAlex Elder 	u32 retries = GSI_CHANNEL_MODEM_HALT_RETRIES;
183011361456SAlex Elder 	int ret;
183111361456SAlex Elder 
183211361456SAlex Elder 	do
183311361456SAlex Elder 		ret = gsi_generic_command(gsi, channel_id,
1834fe68c43cSAlex Elder 					  GSI_GENERIC_HALT_CHANNEL, 0);
183511361456SAlex Elder 	while (ret == -EAGAIN && retries--);
183611361456SAlex Elder 
183711361456SAlex Elder 	if (ret)
183811361456SAlex Elder 		dev_err(gsi->dev, "error %d halting modem channel %u\n",
183911361456SAlex Elder 			ret, channel_id);
1840650d1603SAlex Elder }
1841650d1603SAlex Elder 
18424c9d631aSAlex Elder /* Enable or disable flow control for a modem GSI TX channel (IPA v4.2+) */
18434c9d631aSAlex Elder void
gsi_modem_channel_flow_control(struct gsi * gsi,u32 channel_id,bool enable)18444c9d631aSAlex Elder gsi_modem_channel_flow_control(struct gsi *gsi, u32 channel_id, bool enable)
18454c9d631aSAlex Elder {
1846fe68c43cSAlex Elder 	u32 retries = 0;
18474c9d631aSAlex Elder 	u32 command;
18484c9d631aSAlex Elder 	int ret;
18494c9d631aSAlex Elder 
18504c9d631aSAlex Elder 	command = enable ? GSI_GENERIC_ENABLE_FLOW_CONTROL
18514c9d631aSAlex Elder 			 : GSI_GENERIC_DISABLE_FLOW_CONTROL;
1852fe68c43cSAlex Elder 	/* Disabling flow control on IPA v4.11+ can return -EAGAIN if enable
1853fe68c43cSAlex Elder 	 * is underway.  In this case we need to retry the command.
1854fe68c43cSAlex Elder 	 */
1855fe68c43cSAlex Elder 	if (!enable && gsi->version >= IPA_VERSION_4_11)
1856fe68c43cSAlex Elder 		retries = GSI_CHANNEL_MODEM_FLOW_RETRIES;
18574c9d631aSAlex Elder 
1858fe68c43cSAlex Elder 	do
1859fe68c43cSAlex Elder 		ret = gsi_generic_command(gsi, channel_id, command, 0);
1860fe68c43cSAlex Elder 	while (ret == -EAGAIN && retries--);
1861fe68c43cSAlex Elder 
18624c9d631aSAlex Elder 	if (ret)
18634c9d631aSAlex Elder 		dev_err(gsi->dev,
18644c9d631aSAlex Elder 			"error %d %sabling mode channel %u flow control\n",
18654c9d631aSAlex Elder 			ret, enable ? "en" : "dis", channel_id);
18664c9d631aSAlex Elder }
18674c9d631aSAlex Elder 
1868650d1603SAlex Elder /* Setup function for channels */
gsi_channel_setup(struct gsi * gsi)1869d387c761SAlex Elder static int gsi_channel_setup(struct gsi *gsi)
1870650d1603SAlex Elder {
1871650d1603SAlex Elder 	u32 channel_id = 0;
1872650d1603SAlex Elder 	u32 mask;
1873650d1603SAlex Elder 	int ret;
1874650d1603SAlex Elder 
1875650d1603SAlex Elder 	gsi_irq_enable(gsi);
1876650d1603SAlex Elder 
1877650d1603SAlex Elder 	mutex_lock(&gsi->mutex);
1878650d1603SAlex Elder 
1879650d1603SAlex Elder 	do {
1880d387c761SAlex Elder 		ret = gsi_channel_setup_one(gsi, channel_id);
1881650d1603SAlex Elder 		if (ret)
1882650d1603SAlex Elder 			goto err_unwind;
1883650d1603SAlex Elder 	} while (++channel_id < gsi->channel_count);
1884650d1603SAlex Elder 
1885650d1603SAlex Elder 	/* Make sure no channels were defined that hardware does not support */
1886650d1603SAlex Elder 	while (channel_id < GSI_CHANNEL_COUNT_MAX) {
1887650d1603SAlex Elder 		struct gsi_channel *channel = &gsi->channel[channel_id++];
1888650d1603SAlex Elder 
18896170b6daSAlex Elder 		if (!gsi_channel_initialized(channel))
18906170b6daSAlex Elder 			continue;
1891650d1603SAlex Elder 
18921d23a56bSAlex Elder 		ret = -EINVAL;
1893650d1603SAlex Elder 		dev_err(gsi->dev, "channel %u not supported by hardware\n",
1894650d1603SAlex Elder 			channel_id - 1);
1895650d1603SAlex Elder 		channel_id = gsi->channel_count;
1896650d1603SAlex Elder 		goto err_unwind;
1897650d1603SAlex Elder 	}
1898650d1603SAlex Elder 
1899650d1603SAlex Elder 	/* Allocate modem channels if necessary */
1900650d1603SAlex Elder 	mask = gsi->modem_channel_bitmap;
1901650d1603SAlex Elder 	while (mask) {
1902650d1603SAlex Elder 		u32 modem_channel_id = __ffs(mask);
1903650d1603SAlex Elder 
1904650d1603SAlex Elder 		ret = gsi_modem_channel_alloc(gsi, modem_channel_id);
1905650d1603SAlex Elder 		if (ret)
1906650d1603SAlex Elder 			goto err_unwind_modem;
1907650d1603SAlex Elder 
1908650d1603SAlex Elder 		/* Clear bit from mask only after success (for unwind) */
1909650d1603SAlex Elder 		mask ^= BIT(modem_channel_id);
1910650d1603SAlex Elder 	}
1911650d1603SAlex Elder 
1912650d1603SAlex Elder 	mutex_unlock(&gsi->mutex);
1913650d1603SAlex Elder 
1914650d1603SAlex Elder 	return 0;
1915650d1603SAlex Elder 
1916650d1603SAlex Elder err_unwind_modem:
1917650d1603SAlex Elder 	/* Compute which modem channels need to be deallocated */
1918650d1603SAlex Elder 	mask ^= gsi->modem_channel_bitmap;
1919650d1603SAlex Elder 	while (mask) {
1920993cac15SAlex Elder 		channel_id = __fls(mask);
1921650d1603SAlex Elder 
1922650d1603SAlex Elder 		mask ^= BIT(channel_id);
1923650d1603SAlex Elder 
1924650d1603SAlex Elder 		gsi_modem_channel_halt(gsi, channel_id);
1925650d1603SAlex Elder 	}
1926650d1603SAlex Elder 
1927650d1603SAlex Elder err_unwind:
1928650d1603SAlex Elder 	while (channel_id--)
1929650d1603SAlex Elder 		gsi_channel_teardown_one(gsi, channel_id);
1930650d1603SAlex Elder 
1931650d1603SAlex Elder 	mutex_unlock(&gsi->mutex);
1932650d1603SAlex Elder 
1933650d1603SAlex Elder 	gsi_irq_disable(gsi);
1934650d1603SAlex Elder 
1935650d1603SAlex Elder 	return ret;
1936650d1603SAlex Elder }
1937650d1603SAlex Elder 
1938650d1603SAlex Elder /* Inverse of gsi_channel_setup() */
gsi_channel_teardown(struct gsi * gsi)1939650d1603SAlex Elder static void gsi_channel_teardown(struct gsi *gsi)
1940650d1603SAlex Elder {
1941650d1603SAlex Elder 	u32 mask = gsi->modem_channel_bitmap;
1942650d1603SAlex Elder 	u32 channel_id;
1943650d1603SAlex Elder 
1944650d1603SAlex Elder 	mutex_lock(&gsi->mutex);
1945650d1603SAlex Elder 
1946650d1603SAlex Elder 	while (mask) {
1947993cac15SAlex Elder 		channel_id = __fls(mask);
1948650d1603SAlex Elder 
1949650d1603SAlex Elder 		mask ^= BIT(channel_id);
1950650d1603SAlex Elder 
1951650d1603SAlex Elder 		gsi_modem_channel_halt(gsi, channel_id);
1952650d1603SAlex Elder 	}
1953650d1603SAlex Elder 
1954650d1603SAlex Elder 	channel_id = gsi->channel_count - 1;
1955650d1603SAlex Elder 	do
1956650d1603SAlex Elder 		gsi_channel_teardown_one(gsi, channel_id);
1957650d1603SAlex Elder 	while (channel_id--);
1958650d1603SAlex Elder 
1959650d1603SAlex Elder 	mutex_unlock(&gsi->mutex);
1960650d1603SAlex Elder 
1961650d1603SAlex Elder 	gsi_irq_disable(gsi);
1962650d1603SAlex Elder }
1963650d1603SAlex Elder 
19641657d8a4SAlex Elder /* Turn off all GSI interrupts initially */
gsi_irq_setup(struct gsi * gsi)19651657d8a4SAlex Elder static int gsi_irq_setup(struct gsi *gsi)
1966a7860a5fSAlex Elder {
19677ba51aa2SAlex Elder 	const struct reg *reg;
1968b176f95bSAlex Elder 	int ret;
1969b176f95bSAlex Elder 
19701657d8a4SAlex Elder 	/* Writing 1 indicates IRQ interrupts; 0 would be MSI */
19717ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_INTSET);
19723f3741c9SAlex Elder 	iowrite32(reg_bit(reg, INTYPE), gsi->virt + reg_offset(reg));
19731657d8a4SAlex Elder 
1974a7860a5fSAlex Elder 	/* Disable all interrupt types */
1975a7860a5fSAlex Elder 	gsi_irq_type_update(gsi, 0);
1976a7860a5fSAlex Elder 
1977a7860a5fSAlex Elder 	/* Clear all type-specific interrupt masks */
19787ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_SRC_CH_IRQ_MSK);
19797ba51aa2SAlex Elder 	iowrite32(0, gsi->virt + reg_offset(reg));
19807ba51aa2SAlex Elder 
19817ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_SRC_EV_CH_IRQ_MSK);
19827ba51aa2SAlex Elder 	iowrite32(0, gsi->virt + reg_offset(reg));
19837ba51aa2SAlex Elder 
19847ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_GLOB_IRQ_EN);
19857ba51aa2SAlex Elder 	iowrite32(0, gsi->virt + reg_offset(reg));
19867ba51aa2SAlex Elder 
19877ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_SRC_IEOB_IRQ_MSK);
19887ba51aa2SAlex Elder 	iowrite32(0, gsi->virt + reg_offset(reg));
1989a7860a5fSAlex Elder 
1990a7860a5fSAlex Elder 	/* The inter-EE interrupts are not supported for IPA v3.0-v3.1 */
1991a7860a5fSAlex Elder 	if (gsi->version > IPA_VERSION_3_1) {
19927ba51aa2SAlex Elder 		reg = gsi_reg(gsi, INTER_EE_SRC_CH_IRQ_MSK);
199359b12b1dSAlex Elder 		iowrite32(0, gsi->virt + reg_offset(reg));
19947ba51aa2SAlex Elder 
19957ba51aa2SAlex Elder 		reg = gsi_reg(gsi, INTER_EE_SRC_EV_CH_IRQ_MSK);
199659b12b1dSAlex Elder 		iowrite32(0, gsi->virt + reg_offset(reg));
1997a7860a5fSAlex Elder 	}
1998a7860a5fSAlex Elder 
19997ba51aa2SAlex Elder 	reg = gsi_reg(gsi, CNTXT_GSI_IRQ_EN);
20007ba51aa2SAlex Elder 	iowrite32(0, gsi->virt + reg_offset(reg));
20011657d8a4SAlex Elder 
2002b176f95bSAlex Elder 	ret = request_irq(gsi->irq, gsi_isr, 0, "gsi", gsi);
2003b176f95bSAlex Elder 	if (ret)
2004b176f95bSAlex Elder 		dev_err(gsi->dev, "error %d requesting \"gsi\" IRQ\n", ret);
2005b176f95bSAlex Elder 
2006b176f95bSAlex Elder 	return ret;
20071657d8a4SAlex Elder }
20081657d8a4SAlex Elder 
gsi_irq_teardown(struct gsi * gsi)20091657d8a4SAlex Elder static void gsi_irq_teardown(struct gsi *gsi)
20101657d8a4SAlex Elder {
2011b176f95bSAlex Elder 	free_irq(gsi->irq, gsi);
2012a7860a5fSAlex Elder }
2013a7860a5fSAlex Elder 
2014a7860a5fSAlex Elder /* Get # supported channel and event rings; there is no gsi_ring_teardown() */
gsi_ring_setup(struct gsi * gsi)2015a7860a5fSAlex Elder static int gsi_ring_setup(struct gsi *gsi)
2016a7860a5fSAlex Elder {
2017a7860a5fSAlex Elder 	struct device *dev = gsi->dev;
20185791a73cSAlex Elder 	const struct reg *reg;
2019a7860a5fSAlex Elder 	u32 count;
2020a7860a5fSAlex Elder 	u32 val;
2021a7860a5fSAlex Elder 
2022a7860a5fSAlex Elder 	if (gsi->version < IPA_VERSION_3_5_1) {
2023a7860a5fSAlex Elder 		/* No HW_PARAM_2 register prior to IPA v3.5.1, assume the max */
2024a7860a5fSAlex Elder 		gsi->channel_count = GSI_CHANNEL_COUNT_MAX;
2025a7860a5fSAlex Elder 		gsi->evt_ring_count = GSI_EVT_RING_COUNT_MAX;
2026a7860a5fSAlex Elder 
2027a7860a5fSAlex Elder 		return 0;
2028a7860a5fSAlex Elder 	}
2029a7860a5fSAlex Elder 
20305791a73cSAlex Elder 	reg = gsi_reg(gsi, HW_PARAM_2);
20315791a73cSAlex Elder 	val = ioread32(gsi->virt + reg_offset(reg));
2032a7860a5fSAlex Elder 
20333f3741c9SAlex Elder 	count = reg_decode(reg, NUM_CH_PER_EE, val);
2034a7860a5fSAlex Elder 	if (!count) {
2035a7860a5fSAlex Elder 		dev_err(dev, "GSI reports zero channels supported\n");
2036a7860a5fSAlex Elder 		return -EINVAL;
2037a7860a5fSAlex Elder 	}
2038a7860a5fSAlex Elder 	if (count > GSI_CHANNEL_COUNT_MAX) {
2039a7860a5fSAlex Elder 		dev_warn(dev, "limiting to %u channels; hardware supports %u\n",
2040a7860a5fSAlex Elder 			 GSI_CHANNEL_COUNT_MAX, count);
2041a7860a5fSAlex Elder 		count = GSI_CHANNEL_COUNT_MAX;
2042a7860a5fSAlex Elder 	}
2043a7860a5fSAlex Elder 	gsi->channel_count = count;
2044a7860a5fSAlex Elder 
2045*f651334eSAlex Elder 	if (gsi->version < IPA_VERSION_5_0) {
20463f3741c9SAlex Elder 		count = reg_decode(reg, NUM_EV_PER_EE, val);
2047*f651334eSAlex Elder 	} else {
2048*f651334eSAlex Elder 		reg = gsi_reg(gsi, HW_PARAM_4);
2049*f651334eSAlex Elder 		count = reg_decode(reg, EV_PER_EE, val);
2050*f651334eSAlex Elder 	}
2051a7860a5fSAlex Elder 	if (!count) {
2052a7860a5fSAlex Elder 		dev_err(dev, "GSI reports zero event rings supported\n");
2053a7860a5fSAlex Elder 		return -EINVAL;
2054a7860a5fSAlex Elder 	}
2055a7860a5fSAlex Elder 	if (count > GSI_EVT_RING_COUNT_MAX) {
2056a7860a5fSAlex Elder 		dev_warn(dev,
2057a7860a5fSAlex Elder 			 "limiting to %u event rings; hardware supports %u\n",
2058a7860a5fSAlex Elder 			 GSI_EVT_RING_COUNT_MAX, count);
2059a7860a5fSAlex Elder 		count = GSI_EVT_RING_COUNT_MAX;
2060a7860a5fSAlex Elder 	}
2061a7860a5fSAlex Elder 	gsi->evt_ring_count = count;
2062a7860a5fSAlex Elder 
2063a7860a5fSAlex Elder 	return 0;
2064a7860a5fSAlex Elder }
2065a7860a5fSAlex Elder 
2066650d1603SAlex Elder /* Setup function for GSI.  GSI firmware must be loaded and initialized */
gsi_setup(struct gsi * gsi)2067d387c761SAlex Elder int gsi_setup(struct gsi *gsi)
2068650d1603SAlex Elder {
20695791a73cSAlex Elder 	const struct reg *reg;
2070650d1603SAlex Elder 	u32 val;
2071bae70a80SAlex Elder 	int ret;
2072650d1603SAlex Elder 
2073650d1603SAlex Elder 	/* Here is where we first touch the GSI hardware */
20745791a73cSAlex Elder 	reg = gsi_reg(gsi, GSI_STATUS);
20755791a73cSAlex Elder 	val = ioread32(gsi->virt + reg_offset(reg));
20763f3741c9SAlex Elder 	if (!(val & reg_bit(reg, ENABLED))) {
2077bae70a80SAlex Elder 		dev_err(gsi->dev, "GSI has not been enabled\n");
2078650d1603SAlex Elder 		return -EIO;
2079650d1603SAlex Elder 	}
2080650d1603SAlex Elder 
20811657d8a4SAlex Elder 	ret = gsi_irq_setup(gsi);
20821657d8a4SAlex Elder 	if (ret)
20831657d8a4SAlex Elder 		return ret;
208497eb94c8SAlex Elder 
2085bae70a80SAlex Elder 	ret = gsi_ring_setup(gsi);	/* No matching teardown required */
2086bae70a80SAlex Elder 	if (ret)
20871657d8a4SAlex Elder 		goto err_irq_teardown;
2088650d1603SAlex Elder 
2089650d1603SAlex Elder 	/* Initialize the error log */
20905791a73cSAlex Elder 	reg = gsi_reg(gsi, ERROR_LOG);
20915791a73cSAlex Elder 	iowrite32(0, gsi->virt + reg_offset(reg));
2092650d1603SAlex Elder 
20931657d8a4SAlex Elder 	ret = gsi_channel_setup(gsi);
20941657d8a4SAlex Elder 	if (ret)
20951657d8a4SAlex Elder 		goto err_irq_teardown;
2096650d1603SAlex Elder 
20971657d8a4SAlex Elder 	return 0;
20981657d8a4SAlex Elder 
20991657d8a4SAlex Elder err_irq_teardown:
21001657d8a4SAlex Elder 	gsi_irq_teardown(gsi);
21011657d8a4SAlex Elder 
21021657d8a4SAlex Elder 	return ret;
2103650d1603SAlex Elder }
2104650d1603SAlex Elder 
2105650d1603SAlex Elder /* Inverse of gsi_setup() */
gsi_teardown(struct gsi * gsi)2106650d1603SAlex Elder void gsi_teardown(struct gsi *gsi)
2107650d1603SAlex Elder {
2108650d1603SAlex Elder 	gsi_channel_teardown(gsi);
21091657d8a4SAlex Elder 	gsi_irq_teardown(gsi);
2110650d1603SAlex Elder }
2111650d1603SAlex Elder 
2112650d1603SAlex Elder /* Initialize a channel's event ring */
gsi_channel_evt_ring_init(struct gsi_channel * channel)2113650d1603SAlex Elder static int gsi_channel_evt_ring_init(struct gsi_channel *channel)
2114650d1603SAlex Elder {
2115650d1603SAlex Elder 	struct gsi *gsi = channel->gsi;
2116650d1603SAlex Elder 	struct gsi_evt_ring *evt_ring;
2117650d1603SAlex Elder 	int ret;
2118650d1603SAlex Elder 
2119650d1603SAlex Elder 	ret = gsi_evt_ring_id_alloc(gsi);
2120650d1603SAlex Elder 	if (ret < 0)
2121650d1603SAlex Elder 		return ret;
2122650d1603SAlex Elder 	channel->evt_ring_id = ret;
2123650d1603SAlex Elder 
2124650d1603SAlex Elder 	evt_ring = &gsi->evt_ring[channel->evt_ring_id];
2125650d1603SAlex Elder 	evt_ring->channel = channel;
2126650d1603SAlex Elder 
2127650d1603SAlex Elder 	ret = gsi_ring_alloc(gsi, &evt_ring->ring, channel->event_count);
2128650d1603SAlex Elder 	if (!ret)
2129650d1603SAlex Elder 		return 0;	/* Success! */
2130650d1603SAlex Elder 
2131650d1603SAlex Elder 	dev_err(gsi->dev, "error %d allocating channel %u event ring\n",
2132650d1603SAlex Elder 		ret, gsi_channel_id(channel));
2133650d1603SAlex Elder 
2134650d1603SAlex Elder 	gsi_evt_ring_id_free(gsi, channel->evt_ring_id);
2135650d1603SAlex Elder 
2136650d1603SAlex Elder 	return ret;
2137650d1603SAlex Elder }
2138650d1603SAlex Elder 
2139650d1603SAlex Elder /* Inverse of gsi_channel_evt_ring_init() */
gsi_channel_evt_ring_exit(struct gsi_channel * channel)2140650d1603SAlex Elder static void gsi_channel_evt_ring_exit(struct gsi_channel *channel)
2141650d1603SAlex Elder {
2142650d1603SAlex Elder 	u32 evt_ring_id = channel->evt_ring_id;
2143650d1603SAlex Elder 	struct gsi *gsi = channel->gsi;
2144650d1603SAlex Elder 	struct gsi_evt_ring *evt_ring;
2145650d1603SAlex Elder 
2146650d1603SAlex Elder 	evt_ring = &gsi->evt_ring[evt_ring_id];
2147650d1603SAlex Elder 	gsi_ring_free(gsi, &evt_ring->ring);
2148650d1603SAlex Elder 	gsi_evt_ring_id_free(gsi, evt_ring_id);
2149650d1603SAlex Elder }
2150650d1603SAlex Elder 
gsi_channel_data_valid(struct gsi * gsi,bool command,const struct ipa_gsi_endpoint_data * data)215192f78f81SAlex Elder static bool gsi_channel_data_valid(struct gsi *gsi, bool command,
2152650d1603SAlex Elder 				   const struct ipa_gsi_endpoint_data *data)
2153650d1603SAlex Elder {
215492f78f81SAlex Elder 	const struct gsi_channel_data *channel_data;
2155650d1603SAlex Elder 	u32 channel_id = data->channel_id;
2156650d1603SAlex Elder 	struct device *dev = gsi->dev;
2157650d1603SAlex Elder 
2158650d1603SAlex Elder 	/* Make sure channel ids are in the range driver supports */
2159650d1603SAlex Elder 	if (channel_id >= GSI_CHANNEL_COUNT_MAX) {
21608463488aSAlex Elder 		dev_err(dev, "bad channel id %u; must be less than %u\n",
2161650d1603SAlex Elder 			channel_id, GSI_CHANNEL_COUNT_MAX);
2162650d1603SAlex Elder 		return false;
2163650d1603SAlex Elder 	}
2164650d1603SAlex Elder 
2165650d1603SAlex Elder 	if (data->ee_id != GSI_EE_AP && data->ee_id != GSI_EE_MODEM) {
21668463488aSAlex Elder 		dev_err(dev, "bad EE id %u; not AP or modem\n", data->ee_id);
2167650d1603SAlex Elder 		return false;
2168650d1603SAlex Elder 	}
2169650d1603SAlex Elder 
217092f78f81SAlex Elder 	if (command && !data->toward_ipa) {
217192f78f81SAlex Elder 		dev_err(dev, "command channel %u is not TX\n", channel_id);
217292f78f81SAlex Elder 		return false;
217392f78f81SAlex Elder 	}
217492f78f81SAlex Elder 
217592f78f81SAlex Elder 	channel_data = &data->channel;
217692f78f81SAlex Elder 
217792f78f81SAlex Elder 	if (!channel_data->tlv_count ||
217892f78f81SAlex Elder 	    channel_data->tlv_count > GSI_TLV_MAX) {
21798463488aSAlex Elder 		dev_err(dev, "channel %u bad tlv_count %u; must be 1..%u\n",
218092f78f81SAlex Elder 			channel_id, channel_data->tlv_count, GSI_TLV_MAX);
218192f78f81SAlex Elder 		return false;
218292f78f81SAlex Elder 	}
218392f78f81SAlex Elder 
218492f78f81SAlex Elder 	if (command && IPA_COMMAND_TRANS_TRE_MAX > channel_data->tlv_count) {
218592f78f81SAlex Elder 		dev_err(dev, "command TRE max too big for channel %u (%u > %u)\n",
218692f78f81SAlex Elder 			channel_id, IPA_COMMAND_TRANS_TRE_MAX,
218792f78f81SAlex Elder 			channel_data->tlv_count);
2188650d1603SAlex Elder 		return false;
2189650d1603SAlex Elder 	}
2190650d1603SAlex Elder 
2191650d1603SAlex Elder 	/* We have to allow at least one maximally-sized transaction to
2192650d1603SAlex Elder 	 * be outstanding (which would use tlv_count TREs).  Given how
2193650d1603SAlex Elder 	 * gsi_channel_tre_max() is computed, tre_count has to be almost
2194650d1603SAlex Elder 	 * twice the TLV FIFO size to satisfy this requirement.
2195650d1603SAlex Elder 	 */
219692f78f81SAlex Elder 	if (channel_data->tre_count < 2 * channel_data->tlv_count - 1) {
2197650d1603SAlex Elder 		dev_err(dev, "channel %u TLV count %u exceeds TRE count %u\n",
219892f78f81SAlex Elder 			channel_id, channel_data->tlv_count,
219992f78f81SAlex Elder 			channel_data->tre_count);
2200650d1603SAlex Elder 		return false;
2201650d1603SAlex Elder 	}
2202650d1603SAlex Elder 
220392f78f81SAlex Elder 	if (!is_power_of_2(channel_data->tre_count)) {
22048463488aSAlex Elder 		dev_err(dev, "channel %u bad tre_count %u; not power of 2\n",
220592f78f81SAlex Elder 			channel_id, channel_data->tre_count);
2206650d1603SAlex Elder 		return false;
2207650d1603SAlex Elder 	}
2208650d1603SAlex Elder 
220992f78f81SAlex Elder 	if (!is_power_of_2(channel_data->event_count)) {
22108463488aSAlex Elder 		dev_err(dev, "channel %u bad event_count %u; not power of 2\n",
221192f78f81SAlex Elder 			channel_id, channel_data->event_count);
2212650d1603SAlex Elder 		return false;
2213650d1603SAlex Elder 	}
2214650d1603SAlex Elder 
2215650d1603SAlex Elder 	return true;
2216650d1603SAlex Elder }
2217650d1603SAlex Elder 
2218650d1603SAlex Elder /* Init function for a single channel */
gsi_channel_init_one(struct gsi * gsi,const struct ipa_gsi_endpoint_data * data,bool command)2219650d1603SAlex Elder static int gsi_channel_init_one(struct gsi *gsi,
2220650d1603SAlex Elder 				const struct ipa_gsi_endpoint_data *data,
222114dbf977SAlex Elder 				bool command)
2222650d1603SAlex Elder {
2223650d1603SAlex Elder 	struct gsi_channel *channel;
2224650d1603SAlex Elder 	u32 tre_count;
2225650d1603SAlex Elder 	int ret;
2226650d1603SAlex Elder 
222792f78f81SAlex Elder 	if (!gsi_channel_data_valid(gsi, command, data))
2228650d1603SAlex Elder 		return -EINVAL;
2229650d1603SAlex Elder 
2230650d1603SAlex Elder 	/* Worst case we need an event for every outstanding TRE */
2231650d1603SAlex Elder 	if (data->channel.tre_count > data->channel.event_count) {
2232650d1603SAlex Elder 		tre_count = data->channel.event_count;
22330721999fSAlex Elder 		dev_warn(gsi->dev, "channel %u limited to %u TREs\n",
22340721999fSAlex Elder 			 data->channel_id, tre_count);
2235650d1603SAlex Elder 	} else {
2236650d1603SAlex Elder 		tre_count = data->channel.tre_count;
2237650d1603SAlex Elder 	}
2238650d1603SAlex Elder 
2239650d1603SAlex Elder 	channel = &gsi->channel[data->channel_id];
2240650d1603SAlex Elder 	memset(channel, 0, sizeof(*channel));
2241650d1603SAlex Elder 
2242650d1603SAlex Elder 	channel->gsi = gsi;
2243650d1603SAlex Elder 	channel->toward_ipa = data->toward_ipa;
2244650d1603SAlex Elder 	channel->command = command;
224588e03057SAlex Elder 	channel->trans_tre_max = data->channel.tlv_count;
2246650d1603SAlex Elder 	channel->tre_count = tre_count;
2247650d1603SAlex Elder 	channel->event_count = data->channel.event_count;
2248650d1603SAlex Elder 
2249650d1603SAlex Elder 	ret = gsi_channel_evt_ring_init(channel);
2250650d1603SAlex Elder 	if (ret)
2251650d1603SAlex Elder 		goto err_clear_gsi;
2252650d1603SAlex Elder 
2253650d1603SAlex Elder 	ret = gsi_ring_alloc(gsi, &channel->tre_ring, data->channel.tre_count);
2254650d1603SAlex Elder 	if (ret) {
2255650d1603SAlex Elder 		dev_err(gsi->dev, "error %d allocating channel %u ring\n",
2256650d1603SAlex Elder 			ret, data->channel_id);
2257650d1603SAlex Elder 		goto err_channel_evt_ring_exit;
2258650d1603SAlex Elder 	}
2259650d1603SAlex Elder 
2260650d1603SAlex Elder 	ret = gsi_channel_trans_init(gsi, data->channel_id);
2261650d1603SAlex Elder 	if (ret)
2262650d1603SAlex Elder 		goto err_ring_free;
2263650d1603SAlex Elder 
2264650d1603SAlex Elder 	if (command) {
2265650d1603SAlex Elder 		u32 tre_max = gsi_channel_tre_max(gsi, data->channel_id);
2266650d1603SAlex Elder 
2267650d1603SAlex Elder 		ret = ipa_cmd_pool_init(channel, tre_max);
2268650d1603SAlex Elder 	}
2269650d1603SAlex Elder 	if (!ret)
2270650d1603SAlex Elder 		return 0;	/* Success! */
2271650d1603SAlex Elder 
2272650d1603SAlex Elder 	gsi_channel_trans_exit(channel);
2273650d1603SAlex Elder err_ring_free:
2274650d1603SAlex Elder 	gsi_ring_free(gsi, &channel->tre_ring);
2275650d1603SAlex Elder err_channel_evt_ring_exit:
2276650d1603SAlex Elder 	gsi_channel_evt_ring_exit(channel);
2277650d1603SAlex Elder err_clear_gsi:
2278650d1603SAlex Elder 	channel->gsi = NULL;	/* Mark it not (fully) initialized */
2279650d1603SAlex Elder 
2280650d1603SAlex Elder 	return ret;
2281650d1603SAlex Elder }
2282650d1603SAlex Elder 
2283650d1603SAlex Elder /* Inverse of gsi_channel_init_one() */
gsi_channel_exit_one(struct gsi_channel * channel)2284650d1603SAlex Elder static void gsi_channel_exit_one(struct gsi_channel *channel)
2285650d1603SAlex Elder {
22866170b6daSAlex Elder 	if (!gsi_channel_initialized(channel))
22876170b6daSAlex Elder 		return;
2288650d1603SAlex Elder 
2289650d1603SAlex Elder 	if (channel->command)
2290650d1603SAlex Elder 		ipa_cmd_pool_exit(channel);
2291650d1603SAlex Elder 	gsi_channel_trans_exit(channel);
2292650d1603SAlex Elder 	gsi_ring_free(channel->gsi, &channel->tre_ring);
2293650d1603SAlex Elder 	gsi_channel_evt_ring_exit(channel);
2294650d1603SAlex Elder }
2295650d1603SAlex Elder 
2296650d1603SAlex Elder /* Init function for channels */
gsi_channel_init(struct gsi * gsi,u32 count,const struct ipa_gsi_endpoint_data * data)229714dbf977SAlex Elder static int gsi_channel_init(struct gsi *gsi, u32 count,
229856dfe8deSAlex Elder 			    const struct ipa_gsi_endpoint_data *data)
2299650d1603SAlex Elder {
230056dfe8deSAlex Elder 	bool modem_alloc;
2301650d1603SAlex Elder 	int ret = 0;
2302650d1603SAlex Elder 	u32 i;
2303650d1603SAlex Elder 
230456dfe8deSAlex Elder 	/* IPA v4.2 requires the AP to allocate channels for the modem */
230556dfe8deSAlex Elder 	modem_alloc = gsi->version == IPA_VERSION_4_2;
230656dfe8deSAlex Elder 
23077ece9eaaSAlex Elder 	gsi->event_bitmap = gsi_event_bitmap_init(GSI_EVT_RING_COUNT_MAX);
23087ece9eaaSAlex Elder 	gsi->ieob_enabled_bitmap = 0;
2309650d1603SAlex Elder 
2310650d1603SAlex Elder 	/* The endpoint data array is indexed by endpoint name */
2311650d1603SAlex Elder 	for (i = 0; i < count; i++) {
2312650d1603SAlex Elder 		bool command = i == IPA_ENDPOINT_AP_COMMAND_TX;
2313650d1603SAlex Elder 
2314650d1603SAlex Elder 		if (ipa_gsi_endpoint_data_empty(&data[i]))
2315650d1603SAlex Elder 			continue;	/* Skip over empty slots */
2316650d1603SAlex Elder 
2317650d1603SAlex Elder 		/* Mark modem channels to be allocated (hardware workaround) */
2318650d1603SAlex Elder 		if (data[i].ee_id == GSI_EE_MODEM) {
2319650d1603SAlex Elder 			if (modem_alloc)
2320650d1603SAlex Elder 				gsi->modem_channel_bitmap |=
2321650d1603SAlex Elder 						BIT(data[i].channel_id);
2322650d1603SAlex Elder 			continue;
2323650d1603SAlex Elder 		}
2324650d1603SAlex Elder 
232514dbf977SAlex Elder 		ret = gsi_channel_init_one(gsi, &data[i], command);
2326650d1603SAlex Elder 		if (ret)
2327650d1603SAlex Elder 			goto err_unwind;
2328650d1603SAlex Elder 	}
2329650d1603SAlex Elder 
2330650d1603SAlex Elder 	return ret;
2331650d1603SAlex Elder 
2332650d1603SAlex Elder err_unwind:
2333650d1603SAlex Elder 	while (i--) {
2334650d1603SAlex Elder 		if (ipa_gsi_endpoint_data_empty(&data[i]))
2335650d1603SAlex Elder 			continue;
2336650d1603SAlex Elder 		if (modem_alloc && data[i].ee_id == GSI_EE_MODEM) {
2337650d1603SAlex Elder 			gsi->modem_channel_bitmap &= ~BIT(data[i].channel_id);
2338650d1603SAlex Elder 			continue;
2339650d1603SAlex Elder 		}
2340650d1603SAlex Elder 		gsi_channel_exit_one(&gsi->channel[data->channel_id]);
2341650d1603SAlex Elder 	}
2342650d1603SAlex Elder 
2343650d1603SAlex Elder 	return ret;
2344650d1603SAlex Elder }
2345650d1603SAlex Elder 
2346650d1603SAlex Elder /* Inverse of gsi_channel_init() */
gsi_channel_exit(struct gsi * gsi)2347650d1603SAlex Elder static void gsi_channel_exit(struct gsi *gsi)
2348650d1603SAlex Elder {
2349650d1603SAlex Elder 	u32 channel_id = GSI_CHANNEL_COUNT_MAX - 1;
2350650d1603SAlex Elder 
2351650d1603SAlex Elder 	do
2352650d1603SAlex Elder 		gsi_channel_exit_one(&gsi->channel[channel_id]);
2353650d1603SAlex Elder 	while (channel_id--);
2354650d1603SAlex Elder 	gsi->modem_channel_bitmap = 0;
2355650d1603SAlex Elder }
2356650d1603SAlex Elder 
2357650d1603SAlex Elder /* Init function for GSI.  GSI hardware does not need to be "ready" */
gsi_init(struct gsi * gsi,struct platform_device * pdev,enum ipa_version version,u32 count,const struct ipa_gsi_endpoint_data * data)23581d0c09deSAlex Elder int gsi_init(struct gsi *gsi, struct platform_device *pdev,
23591d0c09deSAlex Elder 	     enum ipa_version version, u32 count,
23601d0c09deSAlex Elder 	     const struct ipa_gsi_endpoint_data *data)
2361650d1603SAlex Elder {
2362650d1603SAlex Elder 	int ret;
2363650d1603SAlex Elder 
2364650d1603SAlex Elder 	gsi_validate_build();
2365650d1603SAlex Elder 
23663c506addSAlex Elder 	gsi->dev = &pdev->dev;
236714dbf977SAlex Elder 	gsi->version = version;
2368650d1603SAlex Elder 
2369571b1e7eSAlex Elder 	/* GSI uses NAPI on all channels.  Create a dummy network device
2370571b1e7eSAlex Elder 	 * for the channel NAPI contexts to be associated with.
2371650d1603SAlex Elder 	 */
2372650d1603SAlex Elder 	init_dummy_netdev(&gsi->dummy_dev);
23730b8d6761SAlex Elder 	init_completion(&gsi->completion);
23740b8d6761SAlex Elder 
23753c506addSAlex Elder 	ret = gsi_reg_init(gsi, pdev);
23763c506addSAlex Elder 	if (ret)
23773c506addSAlex Elder 		return ret;
23783c506addSAlex Elder 
2379b176f95bSAlex Elder 	ret = gsi_irq_init(gsi, pdev);	/* No matching exit required */
2380650d1603SAlex Elder 	if (ret)
23813c506addSAlex Elder 		goto err_reg_exit;
2382650d1603SAlex Elder 
23830b8d6761SAlex Elder 	ret = gsi_channel_init(gsi, count, data);
23840b8d6761SAlex Elder 	if (ret)
23853c506addSAlex Elder 		goto err_reg_exit;
23860b8d6761SAlex Elder 
2387650d1603SAlex Elder 	mutex_init(&gsi->mutex);
2388650d1603SAlex Elder 
2389650d1603SAlex Elder 	return 0;
2390650d1603SAlex Elder 
23913c506addSAlex Elder err_reg_exit:
23923c506addSAlex Elder 	gsi_reg_exit(gsi);
2393650d1603SAlex Elder 
2394650d1603SAlex Elder 	return ret;
2395650d1603SAlex Elder }
2396650d1603SAlex Elder 
2397650d1603SAlex Elder /* Inverse of gsi_init() */
gsi_exit(struct gsi * gsi)2398650d1603SAlex Elder void gsi_exit(struct gsi *gsi)
2399650d1603SAlex Elder {
2400650d1603SAlex Elder 	mutex_destroy(&gsi->mutex);
2401650d1603SAlex Elder 	gsi_channel_exit(gsi);
24023c506addSAlex Elder 	gsi_reg_exit(gsi);
2403650d1603SAlex Elder }
2404650d1603SAlex Elder 
2405650d1603SAlex Elder /* The maximum number of outstanding TREs on a channel.  This limits
2406650d1603SAlex Elder  * a channel's maximum number of transactions outstanding (worst case
2407650d1603SAlex Elder  * is one TRE per transaction).
2408650d1603SAlex Elder  *
2409650d1603SAlex Elder  * The absolute limit is the number of TREs in the channel's TRE ring,
2410650d1603SAlex Elder  * and in theory we should be able use all of them.  But in practice,
2411650d1603SAlex Elder  * doing that led to the hardware reporting exhaustion of event ring
2412650d1603SAlex Elder  * slots for writing completion information.  So the hardware limit
2413650d1603SAlex Elder  * would be (tre_count - 1).
2414650d1603SAlex Elder  *
2415650d1603SAlex Elder  * We reduce it a bit further though.  Transaction resource pools are
2416650d1603SAlex Elder  * sized to be a little larger than this maximum, to allow resource
2417650d1603SAlex Elder  * allocations to always be contiguous.  The number of entries in a
2418650d1603SAlex Elder  * TRE ring buffer is a power of 2, and the extra resources in a pool
2419650d1603SAlex Elder  * tends to nearly double the memory allocated for it.  Reducing the
2420650d1603SAlex Elder  * maximum number of outstanding TREs allows the number of entries in
2421650d1603SAlex Elder  * a pool to avoid crossing that power-of-2 boundary, and this can
2422650d1603SAlex Elder  * substantially reduce pool memory requirements.  The number we
2423650d1603SAlex Elder  * reduce it by matches the number added in gsi_trans_pool_init().
2424650d1603SAlex Elder  */
gsi_channel_tre_max(struct gsi * gsi,u32 channel_id)2425650d1603SAlex Elder u32 gsi_channel_tre_max(struct gsi *gsi, u32 channel_id)
2426650d1603SAlex Elder {
2427650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
2428650d1603SAlex Elder 
2429650d1603SAlex Elder 	/* Hardware limit is channel->tre_count - 1 */
243088e03057SAlex Elder 	return channel->tre_count - (channel->trans_tre_max - 1);
2431650d1603SAlex Elder }
2432