xref: /openbmc/linux/drivers/net/ipa/gsi.c (revision d7f3087b396d8aa4b4751b61fa8cbab82113bd59)
1650d1603SAlex Elder // SPDX-License-Identifier: GPL-2.0
2650d1603SAlex Elder 
3650d1603SAlex Elder /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
4571b1e7eSAlex Elder  * Copyright (C) 2018-2021 Linaro Ltd.
5650d1603SAlex Elder  */
6650d1603SAlex Elder 
7650d1603SAlex Elder #include <linux/types.h>
8650d1603SAlex Elder #include <linux/bits.h>
9650d1603SAlex Elder #include <linux/bitfield.h>
10650d1603SAlex Elder #include <linux/mutex.h>
11650d1603SAlex Elder #include <linux/completion.h>
12650d1603SAlex Elder #include <linux/io.h>
13650d1603SAlex Elder #include <linux/bug.h>
14650d1603SAlex Elder #include <linux/interrupt.h>
15650d1603SAlex Elder #include <linux/platform_device.h>
16650d1603SAlex Elder #include <linux/netdevice.h>
17650d1603SAlex Elder 
18650d1603SAlex Elder #include "gsi.h"
19650d1603SAlex Elder #include "gsi_reg.h"
20650d1603SAlex Elder #include "gsi_private.h"
21650d1603SAlex Elder #include "gsi_trans.h"
22650d1603SAlex Elder #include "ipa_gsi.h"
23650d1603SAlex Elder #include "ipa_data.h"
241d0c09deSAlex Elder #include "ipa_version.h"
25650d1603SAlex Elder 
26650d1603SAlex Elder /**
27650d1603SAlex Elder  * DOC: The IPA Generic Software Interface
28650d1603SAlex Elder  *
29650d1603SAlex Elder  * The generic software interface (GSI) is an integral component of the IPA,
30650d1603SAlex Elder  * providing a well-defined communication layer between the AP subsystem
31650d1603SAlex Elder  * and the IPA core.  The modem uses the GSI layer as well.
32650d1603SAlex Elder  *
33650d1603SAlex Elder  *	--------	     ---------
34650d1603SAlex Elder  *	|      |	     |	     |
35650d1603SAlex Elder  *	|  AP  +<---.	.----+ Modem |
36650d1603SAlex Elder  *	|      +--. |	| .->+	     |
37650d1603SAlex Elder  *	|      |  | |	| |  |	     |
38650d1603SAlex Elder  *	--------  | |	| |  ---------
39650d1603SAlex Elder  *		  v |	v |
40650d1603SAlex Elder  *		--+-+---+-+--
41650d1603SAlex Elder  *		|    GSI    |
42650d1603SAlex Elder  *		|-----------|
43650d1603SAlex Elder  *		|	    |
44650d1603SAlex Elder  *		|    IPA    |
45650d1603SAlex Elder  *		|	    |
46650d1603SAlex Elder  *		-------------
47650d1603SAlex Elder  *
48650d1603SAlex Elder  * In the above diagram, the AP and Modem represent "execution environments"
49650d1603SAlex Elder  * (EEs), which are independent operating environments that use the IPA for
50650d1603SAlex Elder  * data transfer.
51650d1603SAlex Elder  *
52650d1603SAlex Elder  * Each EE uses a set of unidirectional GSI "channels," which allow transfer
53650d1603SAlex Elder  * of data to or from the IPA.  A channel is implemented as a ring buffer,
54650d1603SAlex Elder  * with a DRAM-resident array of "transfer elements" (TREs) available to
55650d1603SAlex Elder  * describe transfers to or from other EEs through the IPA.  A transfer
56650d1603SAlex Elder  * element can also contain an immediate command, requesting the IPA perform
57650d1603SAlex Elder  * actions other than data transfer.
58650d1603SAlex Elder  *
59650d1603SAlex Elder  * Each TRE refers to a block of data--also located DRAM.  After writing one
60650d1603SAlex Elder  * or more TREs to a channel, the writer (either the IPA or an EE) writes a
61650d1603SAlex Elder  * doorbell register to inform the receiving side how many elements have
62650d1603SAlex Elder  * been written.
63650d1603SAlex Elder  *
64650d1603SAlex Elder  * Each channel has a GSI "event ring" associated with it.  An event ring
65650d1603SAlex Elder  * is implemented very much like a channel ring, but is always directed from
66650d1603SAlex Elder  * the IPA to an EE.  The IPA notifies an EE (such as the AP) about channel
67650d1603SAlex Elder  * events by adding an entry to the event ring associated with the channel.
68650d1603SAlex Elder  * The GSI then writes its doorbell for the event ring, causing the target
69650d1603SAlex Elder  * EE to be interrupted.  Each entry in an event ring contains a pointer
70650d1603SAlex Elder  * to the channel TRE whose completion the event represents.
71650d1603SAlex Elder  *
72650d1603SAlex Elder  * Each TRE in a channel ring has a set of flags.  One flag indicates whether
73650d1603SAlex Elder  * the completion of the transfer operation generates an entry (and possibly
74650d1603SAlex Elder  * an interrupt) in the channel's event ring.  Other flags allow transfer
75650d1603SAlex Elder  * elements to be chained together, forming a single logical transaction.
76650d1603SAlex Elder  * TRE flags are used to control whether and when interrupts are generated
77650d1603SAlex Elder  * to signal completion of channel transfers.
78650d1603SAlex Elder  *
79650d1603SAlex Elder  * Elements in channel and event rings are completed (or consumed) strictly
80650d1603SAlex Elder  * in order.  Completion of one entry implies the completion of all preceding
81650d1603SAlex Elder  * entries.  A single completion interrupt can therefore communicate the
82650d1603SAlex Elder  * completion of many transfers.
83650d1603SAlex Elder  *
84650d1603SAlex Elder  * Note that all GSI registers are little-endian, which is the assumed
85650d1603SAlex Elder  * endianness of I/O space accesses.  The accessor functions perform byte
86650d1603SAlex Elder  * swapping if needed (i.e., for a big endian CPU).
87650d1603SAlex Elder  */
88650d1603SAlex Elder 
89650d1603SAlex Elder /* Delay period for interrupt moderation (in 32KHz IPA internal timer ticks) */
90650d1603SAlex Elder #define GSI_EVT_RING_INT_MODT		(32 * 1) /* 1ms under 32KHz clock */
91650d1603SAlex Elder 
9259b5f454SAlex Elder #define GSI_CMD_TIMEOUT			50	/* milliseconds */
93650d1603SAlex Elder 
94057ef63fSAlex Elder #define GSI_CHANNEL_STOP_RETRIES	10
9511361456SAlex Elder #define GSI_CHANNEL_MODEM_HALT_RETRIES	10
96650d1603SAlex Elder 
97650d1603SAlex Elder #define GSI_MHI_EVENT_ID_START		10	/* 1st reserved event id */
98650d1603SAlex Elder #define GSI_MHI_EVENT_ID_END		16	/* Last reserved event id */
99650d1603SAlex Elder 
100650d1603SAlex Elder #define GSI_ISR_MAX_ITER		50	/* Detect interrupt storms */
101650d1603SAlex Elder 
102650d1603SAlex Elder /* An entry in an event ring */
103650d1603SAlex Elder struct gsi_event {
104650d1603SAlex Elder 	__le64 xfer_ptr;
105650d1603SAlex Elder 	__le16 len;
106650d1603SAlex Elder 	u8 reserved1;
107650d1603SAlex Elder 	u8 code;
108650d1603SAlex Elder 	__le16 reserved2;
109650d1603SAlex Elder 	u8 type;
110650d1603SAlex Elder 	u8 chid;
111650d1603SAlex Elder };
112650d1603SAlex Elder 
113650d1603SAlex Elder /** gsi_channel_scratch_gpi - GPI protocol scratch register
114650d1603SAlex Elder  * @max_outstanding_tre:
115650d1603SAlex Elder  *	Defines the maximum number of TREs allowed in a single transaction
116650d1603SAlex Elder  *	on a channel (in bytes).  This determines the amount of prefetch
117650d1603SAlex Elder  *	performed by the hardware.  We configure this to equal the size of
118650d1603SAlex Elder  *	the TLV FIFO for the channel.
119650d1603SAlex Elder  * @outstanding_threshold:
120650d1603SAlex Elder  *	Defines the threshold (in bytes) determining when the sequencer
121650d1603SAlex Elder  *	should update the channel doorbell.  We configure this to equal
122650d1603SAlex Elder  *	the size of two TREs.
123650d1603SAlex Elder  */
124650d1603SAlex Elder struct gsi_channel_scratch_gpi {
125650d1603SAlex Elder 	u64 reserved1;
126650d1603SAlex Elder 	u16 reserved2;
127650d1603SAlex Elder 	u16 max_outstanding_tre;
128650d1603SAlex Elder 	u16 reserved3;
129650d1603SAlex Elder 	u16 outstanding_threshold;
130650d1603SAlex Elder };
131650d1603SAlex Elder 
132650d1603SAlex Elder /** gsi_channel_scratch - channel scratch configuration area
133650d1603SAlex Elder  *
134650d1603SAlex Elder  * The exact interpretation of this register is protocol-specific.
135650d1603SAlex Elder  * We only use GPI channels; see struct gsi_channel_scratch_gpi, above.
136650d1603SAlex Elder  */
137650d1603SAlex Elder union gsi_channel_scratch {
138650d1603SAlex Elder 	struct gsi_channel_scratch_gpi gpi;
139650d1603SAlex Elder 	struct {
140650d1603SAlex Elder 		u32 word1;
141650d1603SAlex Elder 		u32 word2;
142650d1603SAlex Elder 		u32 word3;
143650d1603SAlex Elder 		u32 word4;
144650d1603SAlex Elder 	} data;
145650d1603SAlex Elder };
146650d1603SAlex Elder 
147650d1603SAlex Elder /* Check things that can be validated at build time. */
148650d1603SAlex Elder static void gsi_validate_build(void)
149650d1603SAlex Elder {
150650d1603SAlex Elder 	/* This is used as a divisor */
151650d1603SAlex Elder 	BUILD_BUG_ON(!GSI_RING_ELEMENT_SIZE);
152650d1603SAlex Elder 
153650d1603SAlex Elder 	/* Code assumes the size of channel and event ring element are
154650d1603SAlex Elder 	 * the same (and fixed).  Make sure the size of an event ring
155650d1603SAlex Elder 	 * element is what's expected.
156650d1603SAlex Elder 	 */
157650d1603SAlex Elder 	BUILD_BUG_ON(sizeof(struct gsi_event) != GSI_RING_ELEMENT_SIZE);
158650d1603SAlex Elder 
159650d1603SAlex Elder 	/* Hardware requires a 2^n ring size.  We ensure the number of
160650d1603SAlex Elder 	 * elements in an event ring is a power of 2 elsewhere; this
161650d1603SAlex Elder 	 * ensure the elements themselves meet the requirement.
162650d1603SAlex Elder 	 */
163650d1603SAlex Elder 	BUILD_BUG_ON(!is_power_of_2(GSI_RING_ELEMENT_SIZE));
164650d1603SAlex Elder 
165650d1603SAlex Elder 	/* The channel element size must fit in this field */
166650d1603SAlex Elder 	BUILD_BUG_ON(GSI_RING_ELEMENT_SIZE > field_max(ELEMENT_SIZE_FMASK));
167650d1603SAlex Elder 
168650d1603SAlex Elder 	/* The event ring element size must fit in this field */
169650d1603SAlex Elder 	BUILD_BUG_ON(GSI_RING_ELEMENT_SIZE > field_max(EV_ELEMENT_SIZE_FMASK));
170650d1603SAlex Elder }
171650d1603SAlex Elder 
172650d1603SAlex Elder /* Return the channel id associated with a given channel */
173650d1603SAlex Elder static u32 gsi_channel_id(struct gsi_channel *channel)
174650d1603SAlex Elder {
175650d1603SAlex Elder 	return channel - &channel->gsi->channel[0];
176650d1603SAlex Elder }
177650d1603SAlex Elder 
1786170b6daSAlex Elder /* An initialized channel has a non-null GSI pointer */
1796170b6daSAlex Elder static bool gsi_channel_initialized(struct gsi_channel *channel)
1806170b6daSAlex Elder {
1816170b6daSAlex Elder 	return !!channel->gsi;
1826170b6daSAlex Elder }
1836170b6daSAlex Elder 
1843ca97ffdSAlex Elder /* Update the GSI IRQ type register with the cached value */
1858194be79SAlex Elder static void gsi_irq_type_update(struct gsi *gsi, u32 val)
1863ca97ffdSAlex Elder {
1878194be79SAlex Elder 	gsi->type_enabled_bitmap = val;
1888194be79SAlex Elder 	iowrite32(val, gsi->virt + GSI_CNTXT_TYPE_IRQ_MSK_OFFSET);
1893ca97ffdSAlex Elder }
1903ca97ffdSAlex Elder 
191b054d4f9SAlex Elder static void gsi_irq_type_enable(struct gsi *gsi, enum gsi_irq_type_id type_id)
192b054d4f9SAlex Elder {
1938194be79SAlex Elder 	gsi_irq_type_update(gsi, gsi->type_enabled_bitmap | BIT(type_id));
194b054d4f9SAlex Elder }
195b054d4f9SAlex Elder 
196b054d4f9SAlex Elder static void gsi_irq_type_disable(struct gsi *gsi, enum gsi_irq_type_id type_id)
197b054d4f9SAlex Elder {
1988194be79SAlex Elder 	gsi_irq_type_update(gsi, gsi->type_enabled_bitmap & ~BIT(type_id));
199b054d4f9SAlex Elder }
200b054d4f9SAlex Elder 
20197eb94c8SAlex Elder /* Turn off all GSI interrupts initially */
20297eb94c8SAlex Elder static void gsi_irq_setup(struct gsi *gsi)
20397eb94c8SAlex Elder {
2048194be79SAlex Elder 	/* Disable all interrupt types */
2058194be79SAlex Elder 	gsi_irq_type_update(gsi, 0);
206b054d4f9SAlex Elder 
2078194be79SAlex Elder 	/* Clear all type-specific interrupt masks */
208b054d4f9SAlex Elder 	iowrite32(0, gsi->virt + GSI_CNTXT_SRC_CH_IRQ_MSK_OFFSET);
209b4175f87SAlex Elder 	iowrite32(0, gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_MSK_OFFSET);
210d6c9e3f5SAlex Elder 	iowrite32(0, gsi->virt + GSI_CNTXT_GLOB_IRQ_EN_OFFSET);
21106c86328SAlex Elder 	iowrite32(0, gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_MSK_OFFSET);
212cdeee49fSAlex Elder 
213571b1e7eSAlex Elder 	/* The inter-EE registers are in the non-adjusted address range */
214571b1e7eSAlex Elder 	iowrite32(0, gsi->virt_raw + GSI_INTER_EE_SRC_CH_IRQ_OFFSET);
215571b1e7eSAlex Elder 	iowrite32(0, gsi->virt_raw + GSI_INTER_EE_SRC_EV_CH_IRQ_OFFSET);
216cdeee49fSAlex Elder 
217352f26a8SAlex Elder 	iowrite32(0, gsi->virt + GSI_CNTXT_GSI_IRQ_EN_OFFSET);
21897eb94c8SAlex Elder }
21997eb94c8SAlex Elder 
22097eb94c8SAlex Elder /* Turn off all GSI interrupts when we're all done */
22197eb94c8SAlex Elder static void gsi_irq_teardown(struct gsi *gsi)
22297eb94c8SAlex Elder {
2238194be79SAlex Elder 	/* Nothing to do */
22497eb94c8SAlex Elder }
22597eb94c8SAlex Elder 
226a60d0632SAlex Elder /* Event ring commands are performed one at a time.  Their completion
227a60d0632SAlex Elder  * is signaled by the event ring control GSI interrupt type, which is
228a60d0632SAlex Elder  * only enabled when we issue an event ring command.  Only the event
229a60d0632SAlex Elder  * ring being operated on has this interrupt enabled.
230a60d0632SAlex Elder  */
231a60d0632SAlex Elder static void gsi_irq_ev_ctrl_enable(struct gsi *gsi, u32 evt_ring_id)
232a60d0632SAlex Elder {
233a60d0632SAlex Elder 	u32 val = BIT(evt_ring_id);
234a60d0632SAlex Elder 
235a60d0632SAlex Elder 	/* There's a small chance that a previous command completed
236a60d0632SAlex Elder 	 * after the interrupt was disabled, so make sure we have no
237a60d0632SAlex Elder 	 * pending interrupts before we enable them.
238a60d0632SAlex Elder 	 */
239a60d0632SAlex Elder 	iowrite32(~0, gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_CLR_OFFSET);
240a60d0632SAlex Elder 
241a60d0632SAlex Elder 	iowrite32(val, gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_MSK_OFFSET);
242a60d0632SAlex Elder 	gsi_irq_type_enable(gsi, GSI_EV_CTRL);
243a60d0632SAlex Elder }
244a60d0632SAlex Elder 
245a60d0632SAlex Elder /* Disable event ring control interrupts */
246a60d0632SAlex Elder static void gsi_irq_ev_ctrl_disable(struct gsi *gsi)
247a60d0632SAlex Elder {
248a60d0632SAlex Elder 	gsi_irq_type_disable(gsi, GSI_EV_CTRL);
249a60d0632SAlex Elder 	iowrite32(0, gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_MSK_OFFSET);
250a60d0632SAlex Elder }
251a60d0632SAlex Elder 
252a60d0632SAlex Elder /* Channel commands are performed one at a time.  Their completion is
253a60d0632SAlex Elder  * signaled by the channel control GSI interrupt type, which is only
254a60d0632SAlex Elder  * enabled when we issue a channel command.  Only the channel being
255a60d0632SAlex Elder  * operated on has this interrupt enabled.
256a60d0632SAlex Elder  */
257a60d0632SAlex Elder static void gsi_irq_ch_ctrl_enable(struct gsi *gsi, u32 channel_id)
258a60d0632SAlex Elder {
259a60d0632SAlex Elder 	u32 val = BIT(channel_id);
260a60d0632SAlex Elder 
261a60d0632SAlex Elder 	/* There's a small chance that a previous command completed
262a60d0632SAlex Elder 	 * after the interrupt was disabled, so make sure we have no
263a60d0632SAlex Elder 	 * pending interrupts before we enable them.
264a60d0632SAlex Elder 	 */
265a60d0632SAlex Elder 	iowrite32(~0, gsi->virt + GSI_CNTXT_SRC_CH_IRQ_CLR_OFFSET);
266a60d0632SAlex Elder 
267a60d0632SAlex Elder 	iowrite32(val, gsi->virt + GSI_CNTXT_SRC_CH_IRQ_MSK_OFFSET);
268a60d0632SAlex Elder 	gsi_irq_type_enable(gsi, GSI_CH_CTRL);
269a60d0632SAlex Elder }
270a60d0632SAlex Elder 
271a60d0632SAlex Elder /* Disable channel control interrupts */
272a60d0632SAlex Elder static void gsi_irq_ch_ctrl_disable(struct gsi *gsi)
273a60d0632SAlex Elder {
274a60d0632SAlex Elder 	gsi_irq_type_disable(gsi, GSI_CH_CTRL);
275a60d0632SAlex Elder 	iowrite32(0, gsi->virt + GSI_CNTXT_SRC_CH_IRQ_MSK_OFFSET);
276a60d0632SAlex Elder }
277a60d0632SAlex Elder 
2785725593eSAlex Elder static void gsi_irq_ieob_enable_one(struct gsi *gsi, u32 evt_ring_id)
279650d1603SAlex Elder {
28006c86328SAlex Elder 	bool enable_ieob = !gsi->ieob_enabled_bitmap;
281650d1603SAlex Elder 	u32 val;
282650d1603SAlex Elder 
283a054539dSAlex Elder 	gsi->ieob_enabled_bitmap |= BIT(evt_ring_id);
284a054539dSAlex Elder 	val = gsi->ieob_enabled_bitmap;
285650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_MSK_OFFSET);
28606c86328SAlex Elder 
28706c86328SAlex Elder 	/* Enable the interrupt type if this is the first channel enabled */
28806c86328SAlex Elder 	if (enable_ieob)
28906c86328SAlex Elder 		gsi_irq_type_enable(gsi, GSI_IEOB);
290650d1603SAlex Elder }
291650d1603SAlex Elder 
2925725593eSAlex Elder static void gsi_irq_ieob_disable(struct gsi *gsi, u32 event_mask)
293650d1603SAlex Elder {
294650d1603SAlex Elder 	u32 val;
295650d1603SAlex Elder 
2965725593eSAlex Elder 	gsi->ieob_enabled_bitmap &= ~event_mask;
29706c86328SAlex Elder 
29806c86328SAlex Elder 	/* Disable the interrupt type if this was the last enabled channel */
29906c86328SAlex Elder 	if (!gsi->ieob_enabled_bitmap)
30006c86328SAlex Elder 		gsi_irq_type_disable(gsi, GSI_IEOB);
30106c86328SAlex Elder 
302a054539dSAlex Elder 	val = gsi->ieob_enabled_bitmap;
303650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_MSK_OFFSET);
304650d1603SAlex Elder }
305650d1603SAlex Elder 
3065725593eSAlex Elder static void gsi_irq_ieob_disable_one(struct gsi *gsi, u32 evt_ring_id)
3075725593eSAlex Elder {
3085725593eSAlex Elder 	gsi_irq_ieob_disable(gsi, BIT(evt_ring_id));
3095725593eSAlex Elder }
3105725593eSAlex Elder 
311650d1603SAlex Elder /* Enable all GSI_interrupt types */
312650d1603SAlex Elder static void gsi_irq_enable(struct gsi *gsi)
313650d1603SAlex Elder {
314650d1603SAlex Elder 	u32 val;
315650d1603SAlex Elder 
316d6c9e3f5SAlex Elder 	/* Global interrupts include hardware error reports.  Enable
317d6c9e3f5SAlex Elder 	 * that so we can at least report the error should it occur.
318d6c9e3f5SAlex Elder 	 */
3196c6358ccSAlex Elder 	iowrite32(BIT(ERROR_INT), gsi->virt + GSI_CNTXT_GLOB_IRQ_EN_OFFSET);
3208194be79SAlex Elder 	gsi_irq_type_update(gsi, gsi->type_enabled_bitmap | BIT(GSI_GLOB_EE));
321d6c9e3f5SAlex Elder 
322352f26a8SAlex Elder 	/* General GSI interrupts are reported to all EEs; if they occur
323352f26a8SAlex Elder 	 * they are unrecoverable (without reset).  A breakpoint interrupt
324352f26a8SAlex Elder 	 * also exists, but we don't support that.  We want to be notified
325352f26a8SAlex Elder 	 * of errors so we can report them, even if they can't be handled.
326352f26a8SAlex Elder 	 */
3276c6358ccSAlex Elder 	val = BIT(BUS_ERROR);
3286c6358ccSAlex Elder 	val |= BIT(CMD_FIFO_OVRFLOW);
3296c6358ccSAlex Elder 	val |= BIT(MCS_STACK_OVRFLOW);
330650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CNTXT_GSI_IRQ_EN_OFFSET);
3318194be79SAlex Elder 	gsi_irq_type_update(gsi, gsi->type_enabled_bitmap | BIT(GSI_GENERAL));
332650d1603SAlex Elder }
333650d1603SAlex Elder 
3343ca97ffdSAlex Elder /* Disable all GSI interrupt types */
335650d1603SAlex Elder static void gsi_irq_disable(struct gsi *gsi)
336650d1603SAlex Elder {
3378194be79SAlex Elder 	gsi_irq_type_update(gsi, 0);
33897eb94c8SAlex Elder 
3398194be79SAlex Elder 	/* Clear the type-specific interrupt masks set by gsi_irq_enable() */
340650d1603SAlex Elder 	iowrite32(0, gsi->virt + GSI_CNTXT_GSI_IRQ_EN_OFFSET);
341d6c9e3f5SAlex Elder 	iowrite32(0, gsi->virt + GSI_CNTXT_GLOB_IRQ_EN_OFFSET);
342650d1603SAlex Elder }
343650d1603SAlex Elder 
344650d1603SAlex Elder /* Return the virtual address associated with a ring index */
345650d1603SAlex Elder void *gsi_ring_virt(struct gsi_ring *ring, u32 index)
346650d1603SAlex Elder {
347650d1603SAlex Elder 	/* Note: index *must* be used modulo the ring count here */
348650d1603SAlex Elder 	return ring->virt + (index % ring->count) * GSI_RING_ELEMENT_SIZE;
349650d1603SAlex Elder }
350650d1603SAlex Elder 
351650d1603SAlex Elder /* Return the 32-bit DMA address associated with a ring index */
352650d1603SAlex Elder static u32 gsi_ring_addr(struct gsi_ring *ring, u32 index)
353650d1603SAlex Elder {
3543c54b7beSAlex Elder 	return lower_32_bits(ring->addr) + index * GSI_RING_ELEMENT_SIZE;
355650d1603SAlex Elder }
356650d1603SAlex Elder 
357650d1603SAlex Elder /* Return the ring index of a 32-bit ring offset */
358650d1603SAlex Elder static u32 gsi_ring_index(struct gsi_ring *ring, u32 offset)
359650d1603SAlex Elder {
360650d1603SAlex Elder 	return (offset - gsi_ring_addr(ring, 0)) / GSI_RING_ELEMENT_SIZE;
361650d1603SAlex Elder }
362650d1603SAlex Elder 
363650d1603SAlex Elder /* Issue a GSI command by writing a value to a register, then wait for
364650d1603SAlex Elder  * completion to be signaled.  Returns true if the command completes
365650d1603SAlex Elder  * or false if it times out.
366650d1603SAlex Elder  */
367650d1603SAlex Elder static bool
368650d1603SAlex Elder gsi_command(struct gsi *gsi, u32 reg, u32 val, struct completion *completion)
369650d1603SAlex Elder {
37059b5f454SAlex Elder 	unsigned long timeout = msecs_to_jiffies(GSI_CMD_TIMEOUT);
37159b5f454SAlex Elder 
372650d1603SAlex Elder 	reinit_completion(completion);
373650d1603SAlex Elder 
374650d1603SAlex Elder 	iowrite32(val, gsi->virt + reg);
375650d1603SAlex Elder 
37659b5f454SAlex Elder 	return !!wait_for_completion_timeout(completion, timeout);
377650d1603SAlex Elder }
378650d1603SAlex Elder 
379650d1603SAlex Elder /* Return the hardware's notion of the current state of an event ring */
380650d1603SAlex Elder static enum gsi_evt_ring_state
381650d1603SAlex Elder gsi_evt_ring_state(struct gsi *gsi, u32 evt_ring_id)
382650d1603SAlex Elder {
383650d1603SAlex Elder 	u32 val;
384650d1603SAlex Elder 
385650d1603SAlex Elder 	val = ioread32(gsi->virt + GSI_EV_CH_E_CNTXT_0_OFFSET(evt_ring_id));
386650d1603SAlex Elder 
387650d1603SAlex Elder 	return u32_get_bits(val, EV_CHSTATE_FMASK);
388650d1603SAlex Elder }
389650d1603SAlex Elder 
390650d1603SAlex Elder /* Issue an event ring command and wait for it to complete */
391d9cbe818SAlex Elder static void gsi_evt_ring_command(struct gsi *gsi, u32 evt_ring_id,
392650d1603SAlex Elder 				 enum gsi_evt_cmd_opcode opcode)
393650d1603SAlex Elder {
394650d1603SAlex Elder 	struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
395650d1603SAlex Elder 	struct completion *completion = &evt_ring->completion;
3968463488aSAlex Elder 	struct device *dev = gsi->dev;
397d9cbe818SAlex Elder 	bool timeout;
398650d1603SAlex Elder 	u32 val;
399650d1603SAlex Elder 
400a60d0632SAlex Elder 	/* Enable the completion interrupt for the command */
401a60d0632SAlex Elder 	gsi_irq_ev_ctrl_enable(gsi, evt_ring_id);
402b4175f87SAlex Elder 
403650d1603SAlex Elder 	val = u32_encode_bits(evt_ring_id, EV_CHID_FMASK);
404650d1603SAlex Elder 	val |= u32_encode_bits(opcode, EV_OPCODE_FMASK);
405650d1603SAlex Elder 
406d9cbe818SAlex Elder 	timeout = !gsi_command(gsi, GSI_EV_CH_CMD_OFFSET, val, completion);
407b4175f87SAlex Elder 
408a60d0632SAlex Elder 	gsi_irq_ev_ctrl_disable(gsi);
409b4175f87SAlex Elder 
410d9cbe818SAlex Elder 	if (!timeout)
4111ddf776bSAlex Elder 		return;
412650d1603SAlex Elder 
4138463488aSAlex Elder 	dev_err(dev, "GSI command %u for event ring %u timed out, state %u\n",
4143f77c926SAlex Elder 		opcode, evt_ring_id, gsi_evt_ring_state(gsi, evt_ring_id));
415650d1603SAlex Elder }
416650d1603SAlex Elder 
417650d1603SAlex Elder /* Allocate an event ring in NOT_ALLOCATED state */
418650d1603SAlex Elder static int gsi_evt_ring_alloc_command(struct gsi *gsi, u32 evt_ring_id)
419650d1603SAlex Elder {
4203f77c926SAlex Elder 	enum gsi_evt_ring_state state;
421650d1603SAlex Elder 
422650d1603SAlex Elder 	/* Get initial event ring state */
4233f77c926SAlex Elder 	state = gsi_evt_ring_state(gsi, evt_ring_id);
4243f77c926SAlex Elder 	if (state != GSI_EVT_RING_STATE_NOT_ALLOCATED) {
425f8d3bdd5SAlex Elder 		dev_err(gsi->dev, "event ring %u bad state %u before alloc\n",
4263f77c926SAlex Elder 			evt_ring_id, state);
427650d1603SAlex Elder 		return -EINVAL;
428a442b3c7SAlex Elder 	}
429650d1603SAlex Elder 
430d9cbe818SAlex Elder 	gsi_evt_ring_command(gsi, evt_ring_id, GSI_EVT_ALLOCATE);
431428b448eSAlex Elder 
432428b448eSAlex Elder 	/* If successful the event ring state will have changed */
4333f77c926SAlex Elder 	state = gsi_evt_ring_state(gsi, evt_ring_id);
4343f77c926SAlex Elder 	if (state == GSI_EVT_RING_STATE_ALLOCATED)
435428b448eSAlex Elder 		return 0;
436428b448eSAlex Elder 
437f8d3bdd5SAlex Elder 	dev_err(gsi->dev, "event ring %u bad state %u after alloc\n",
4383f77c926SAlex Elder 		evt_ring_id, state);
439650d1603SAlex Elder 
440428b448eSAlex Elder 	return -EIO;
441650d1603SAlex Elder }
442650d1603SAlex Elder 
443650d1603SAlex Elder /* Reset a GSI event ring in ALLOCATED or ERROR state. */
444650d1603SAlex Elder static void gsi_evt_ring_reset_command(struct gsi *gsi, u32 evt_ring_id)
445650d1603SAlex Elder {
4463f77c926SAlex Elder 	enum gsi_evt_ring_state state;
447650d1603SAlex Elder 
4483f77c926SAlex Elder 	state = gsi_evt_ring_state(gsi, evt_ring_id);
449650d1603SAlex Elder 	if (state != GSI_EVT_RING_STATE_ALLOCATED &&
450650d1603SAlex Elder 	    state != GSI_EVT_RING_STATE_ERROR) {
451f8d3bdd5SAlex Elder 		dev_err(gsi->dev, "event ring %u bad state %u before reset\n",
4523f77c926SAlex Elder 			evt_ring_id, state);
453650d1603SAlex Elder 		return;
454650d1603SAlex Elder 	}
455650d1603SAlex Elder 
456d9cbe818SAlex Elder 	gsi_evt_ring_command(gsi, evt_ring_id, GSI_EVT_RESET);
457428b448eSAlex Elder 
458428b448eSAlex Elder 	/* If successful the event ring state will have changed */
4593f77c926SAlex Elder 	state = gsi_evt_ring_state(gsi, evt_ring_id);
4603f77c926SAlex Elder 	if (state == GSI_EVT_RING_STATE_ALLOCATED)
461428b448eSAlex Elder 		return;
462428b448eSAlex Elder 
463f8d3bdd5SAlex Elder 	dev_err(gsi->dev, "event ring %u bad state %u after reset\n",
4643f77c926SAlex Elder 		evt_ring_id, state);
465650d1603SAlex Elder }
466650d1603SAlex Elder 
467650d1603SAlex Elder /* Issue a hardware de-allocation request for an allocated event ring */
468650d1603SAlex Elder static void gsi_evt_ring_de_alloc_command(struct gsi *gsi, u32 evt_ring_id)
469650d1603SAlex Elder {
4703f77c926SAlex Elder 	enum gsi_evt_ring_state state;
471650d1603SAlex Elder 
4723f77c926SAlex Elder 	state = gsi_evt_ring_state(gsi, evt_ring_id);
4733f77c926SAlex Elder 	if (state != GSI_EVT_RING_STATE_ALLOCATED) {
474f8d3bdd5SAlex Elder 		dev_err(gsi->dev, "event ring %u state %u before dealloc\n",
4753f77c926SAlex Elder 			evt_ring_id, state);
476650d1603SAlex Elder 		return;
477650d1603SAlex Elder 	}
478650d1603SAlex Elder 
479d9cbe818SAlex Elder 	gsi_evt_ring_command(gsi, evt_ring_id, GSI_EVT_DE_ALLOC);
480428b448eSAlex Elder 
481428b448eSAlex Elder 	/* If successful the event ring state will have changed */
4823f77c926SAlex Elder 	state = gsi_evt_ring_state(gsi, evt_ring_id);
4833f77c926SAlex Elder 	if (state == GSI_EVT_RING_STATE_NOT_ALLOCATED)
484428b448eSAlex Elder 		return;
485428b448eSAlex Elder 
486f8d3bdd5SAlex Elder 	dev_err(gsi->dev, "event ring %u bad state %u after dealloc\n",
4873f77c926SAlex Elder 		evt_ring_id, state);
488650d1603SAlex Elder }
489650d1603SAlex Elder 
490a2003b30SAlex Elder /* Fetch the current state of a channel from hardware */
491aba7924fSAlex Elder static enum gsi_channel_state gsi_channel_state(struct gsi_channel *channel)
492650d1603SAlex Elder {
493aba7924fSAlex Elder 	u32 channel_id = gsi_channel_id(channel);
494e6cdd6d8SAlex Elder 	void __iomem *virt = channel->gsi->virt;
495650d1603SAlex Elder 	u32 val;
496650d1603SAlex Elder 
497aba7924fSAlex Elder 	val = ioread32(virt + GSI_CH_C_CNTXT_0_OFFSET(channel_id));
498650d1603SAlex Elder 
499650d1603SAlex Elder 	return u32_get_bits(val, CHSTATE_FMASK);
500650d1603SAlex Elder }
501650d1603SAlex Elder 
502650d1603SAlex Elder /* Issue a channel command and wait for it to complete */
5031169318bSAlex Elder static void
504650d1603SAlex Elder gsi_channel_command(struct gsi_channel *channel, enum gsi_ch_cmd_opcode opcode)
505650d1603SAlex Elder {
506650d1603SAlex Elder 	struct completion *completion = &channel->completion;
507650d1603SAlex Elder 	u32 channel_id = gsi_channel_id(channel);
508a2003b30SAlex Elder 	struct gsi *gsi = channel->gsi;
5098463488aSAlex Elder 	struct device *dev = gsi->dev;
510d9cbe818SAlex Elder 	bool timeout;
511650d1603SAlex Elder 	u32 val;
512650d1603SAlex Elder 
513a60d0632SAlex Elder 	/* Enable the completion interrupt for the command */
514a60d0632SAlex Elder 	gsi_irq_ch_ctrl_enable(gsi, channel_id);
515b054d4f9SAlex Elder 
516650d1603SAlex Elder 	val = u32_encode_bits(channel_id, CH_CHID_FMASK);
517650d1603SAlex Elder 	val |= u32_encode_bits(opcode, CH_OPCODE_FMASK);
518d9cbe818SAlex Elder 	timeout = !gsi_command(gsi, GSI_CH_CMD_OFFSET, val, completion);
519650d1603SAlex Elder 
520a60d0632SAlex Elder 	gsi_irq_ch_ctrl_disable(gsi);
521b054d4f9SAlex Elder 
522d9cbe818SAlex Elder 	if (!timeout)
5231169318bSAlex Elder 		return;
524650d1603SAlex Elder 
5258463488aSAlex Elder 	dev_err(dev, "GSI command %u for channel %u timed out, state %u\n",
526a2003b30SAlex Elder 		opcode, channel_id, gsi_channel_state(channel));
527650d1603SAlex Elder }
528650d1603SAlex Elder 
529650d1603SAlex Elder /* Allocate GSI channel in NOT_ALLOCATED state */
530650d1603SAlex Elder static int gsi_channel_alloc_command(struct gsi *gsi, u32 channel_id)
531650d1603SAlex Elder {
532650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
533a442b3c7SAlex Elder 	struct device *dev = gsi->dev;
534a2003b30SAlex Elder 	enum gsi_channel_state state;
535650d1603SAlex Elder 
536650d1603SAlex Elder 	/* Get initial channel state */
537a2003b30SAlex Elder 	state = gsi_channel_state(channel);
538a442b3c7SAlex Elder 	if (state != GSI_CHANNEL_STATE_NOT_ALLOCATED) {
539f8d3bdd5SAlex Elder 		dev_err(dev, "channel %u bad state %u before alloc\n",
540f8d3bdd5SAlex Elder 			channel_id, state);
541650d1603SAlex Elder 		return -EINVAL;
542a442b3c7SAlex Elder 	}
543650d1603SAlex Elder 
5441169318bSAlex Elder 	gsi_channel_command(channel, GSI_CH_ALLOCATE);
545a2003b30SAlex Elder 
5466ffddf3bSAlex Elder 	/* If successful the channel state will have changed */
547a2003b30SAlex Elder 	state = gsi_channel_state(channel);
5486ffddf3bSAlex Elder 	if (state == GSI_CHANNEL_STATE_ALLOCATED)
5496ffddf3bSAlex Elder 		return 0;
5506ffddf3bSAlex Elder 
551f8d3bdd5SAlex Elder 	dev_err(dev, "channel %u bad state %u after alloc\n",
552f8d3bdd5SAlex Elder 		channel_id, state);
553650d1603SAlex Elder 
5546ffddf3bSAlex Elder 	return -EIO;
555650d1603SAlex Elder }
556650d1603SAlex Elder 
557650d1603SAlex Elder /* Start an ALLOCATED channel */
558650d1603SAlex Elder static int gsi_channel_start_command(struct gsi_channel *channel)
559650d1603SAlex Elder {
560a442b3c7SAlex Elder 	struct device *dev = channel->gsi->dev;
561a2003b30SAlex Elder 	enum gsi_channel_state state;
562650d1603SAlex Elder 
563a2003b30SAlex Elder 	state = gsi_channel_state(channel);
564650d1603SAlex Elder 	if (state != GSI_CHANNEL_STATE_ALLOCATED &&
565a442b3c7SAlex Elder 	    state != GSI_CHANNEL_STATE_STOPPED) {
566f8d3bdd5SAlex Elder 		dev_err(dev, "channel %u bad state %u before start\n",
567f8d3bdd5SAlex Elder 			gsi_channel_id(channel), state);
568650d1603SAlex Elder 		return -EINVAL;
569a442b3c7SAlex Elder 	}
570650d1603SAlex Elder 
5711169318bSAlex Elder 	gsi_channel_command(channel, GSI_CH_START);
572a2003b30SAlex Elder 
5736ffddf3bSAlex Elder 	/* If successful the channel state will have changed */
574a2003b30SAlex Elder 	state = gsi_channel_state(channel);
5756ffddf3bSAlex Elder 	if (state == GSI_CHANNEL_STATE_STARTED)
5766ffddf3bSAlex Elder 		return 0;
5776ffddf3bSAlex Elder 
578f8d3bdd5SAlex Elder 	dev_err(dev, "channel %u bad state %u after start\n",
579f8d3bdd5SAlex Elder 		gsi_channel_id(channel), state);
580650d1603SAlex Elder 
5816ffddf3bSAlex Elder 	return -EIO;
582650d1603SAlex Elder }
583650d1603SAlex Elder 
584650d1603SAlex Elder /* Stop a GSI channel in STARTED state */
585650d1603SAlex Elder static int gsi_channel_stop_command(struct gsi_channel *channel)
586650d1603SAlex Elder {
587a442b3c7SAlex Elder 	struct device *dev = channel->gsi->dev;
588a2003b30SAlex Elder 	enum gsi_channel_state state;
589650d1603SAlex Elder 
590a2003b30SAlex Elder 	state = gsi_channel_state(channel);
5915468cbcdSAlex Elder 
5925468cbcdSAlex Elder 	/* Channel could have entered STOPPED state since last call
5935468cbcdSAlex Elder 	 * if it timed out.  If so, we're done.
5945468cbcdSAlex Elder 	 */
5955468cbcdSAlex Elder 	if (state == GSI_CHANNEL_STATE_STOPPED)
5965468cbcdSAlex Elder 		return 0;
5975468cbcdSAlex Elder 
598650d1603SAlex Elder 	if (state != GSI_CHANNEL_STATE_STARTED &&
599a442b3c7SAlex Elder 	    state != GSI_CHANNEL_STATE_STOP_IN_PROC) {
600f8d3bdd5SAlex Elder 		dev_err(dev, "channel %u bad state %u before stop\n",
601f8d3bdd5SAlex Elder 			gsi_channel_id(channel), state);
602650d1603SAlex Elder 		return -EINVAL;
603a442b3c7SAlex Elder 	}
604650d1603SAlex Elder 
6051169318bSAlex Elder 	gsi_channel_command(channel, GSI_CH_STOP);
606a2003b30SAlex Elder 
6076ffddf3bSAlex Elder 	/* If successful the channel state will have changed */
608a2003b30SAlex Elder 	state = gsi_channel_state(channel);
6096ffddf3bSAlex Elder 	if (state == GSI_CHANNEL_STATE_STOPPED)
6106ffddf3bSAlex Elder 		return 0;
611650d1603SAlex Elder 
612650d1603SAlex Elder 	/* We may have to try again if stop is in progress */
613a2003b30SAlex Elder 	if (state == GSI_CHANNEL_STATE_STOP_IN_PROC)
614650d1603SAlex Elder 		return -EAGAIN;
615650d1603SAlex Elder 
616f8d3bdd5SAlex Elder 	dev_err(dev, "channel %u bad state %u after stop\n",
617f8d3bdd5SAlex Elder 		gsi_channel_id(channel), state);
618650d1603SAlex Elder 
619650d1603SAlex Elder 	return -EIO;
620650d1603SAlex Elder }
621650d1603SAlex Elder 
622650d1603SAlex Elder /* Reset a GSI channel in ALLOCATED or ERROR state. */
623650d1603SAlex Elder static void gsi_channel_reset_command(struct gsi_channel *channel)
624650d1603SAlex Elder {
625a442b3c7SAlex Elder 	struct device *dev = channel->gsi->dev;
626a2003b30SAlex Elder 	enum gsi_channel_state state;
627650d1603SAlex Elder 
62874401946SAlex Elder 	/* A short delay is required before a RESET command */
62974401946SAlex Elder 	usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
630650d1603SAlex Elder 
631a2003b30SAlex Elder 	state = gsi_channel_state(channel);
632a2003b30SAlex Elder 	if (state != GSI_CHANNEL_STATE_STOPPED &&
633a2003b30SAlex Elder 	    state != GSI_CHANNEL_STATE_ERROR) {
6345d28913dSAlex Elder 		/* No need to reset a channel already in ALLOCATED state */
6355d28913dSAlex Elder 		if (state != GSI_CHANNEL_STATE_ALLOCATED)
636f8d3bdd5SAlex Elder 			dev_err(dev, "channel %u bad state %u before reset\n",
637f8d3bdd5SAlex Elder 				gsi_channel_id(channel), state);
638650d1603SAlex Elder 		return;
639650d1603SAlex Elder 	}
640650d1603SAlex Elder 
6411169318bSAlex Elder 	gsi_channel_command(channel, GSI_CH_RESET);
642a2003b30SAlex Elder 
6436ffddf3bSAlex Elder 	/* If successful the channel state will have changed */
644a2003b30SAlex Elder 	state = gsi_channel_state(channel);
6456ffddf3bSAlex Elder 	if (state != GSI_CHANNEL_STATE_ALLOCATED)
646f8d3bdd5SAlex Elder 		dev_err(dev, "channel %u bad state %u after reset\n",
647f8d3bdd5SAlex Elder 			gsi_channel_id(channel), state);
648650d1603SAlex Elder }
649650d1603SAlex Elder 
650650d1603SAlex Elder /* Deallocate an ALLOCATED GSI channel */
651650d1603SAlex Elder static void gsi_channel_de_alloc_command(struct gsi *gsi, u32 channel_id)
652650d1603SAlex Elder {
653650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
654a442b3c7SAlex Elder 	struct device *dev = gsi->dev;
655a2003b30SAlex Elder 	enum gsi_channel_state state;
656650d1603SAlex Elder 
657a2003b30SAlex Elder 	state = gsi_channel_state(channel);
658a2003b30SAlex Elder 	if (state != GSI_CHANNEL_STATE_ALLOCATED) {
659f8d3bdd5SAlex Elder 		dev_err(dev, "channel %u bad state %u before dealloc\n",
660f8d3bdd5SAlex Elder 			channel_id, state);
661650d1603SAlex Elder 		return;
662650d1603SAlex Elder 	}
663650d1603SAlex Elder 
6641169318bSAlex Elder 	gsi_channel_command(channel, GSI_CH_DE_ALLOC);
665a2003b30SAlex Elder 
6666ffddf3bSAlex Elder 	/* If successful the channel state will have changed */
667a2003b30SAlex Elder 	state = gsi_channel_state(channel);
6686ffddf3bSAlex Elder 
6696ffddf3bSAlex Elder 	if (state != GSI_CHANNEL_STATE_NOT_ALLOCATED)
670f8d3bdd5SAlex Elder 		dev_err(dev, "channel %u bad state %u after dealloc\n",
671f8d3bdd5SAlex Elder 			channel_id, state);
672650d1603SAlex Elder }
673650d1603SAlex Elder 
674650d1603SAlex Elder /* Ring an event ring doorbell, reporting the last entry processed by the AP.
675650d1603SAlex Elder  * The index argument (modulo the ring count) is the first unfilled entry, so
676650d1603SAlex Elder  * we supply one less than that with the doorbell.  Update the event ring
677650d1603SAlex Elder  * index field with the value provided.
678650d1603SAlex Elder  */
679650d1603SAlex Elder static void gsi_evt_ring_doorbell(struct gsi *gsi, u32 evt_ring_id, u32 index)
680650d1603SAlex Elder {
681650d1603SAlex Elder 	struct gsi_ring *ring = &gsi->evt_ring[evt_ring_id].ring;
682650d1603SAlex Elder 	u32 val;
683650d1603SAlex Elder 
684650d1603SAlex Elder 	ring->index = index;	/* Next unused entry */
685650d1603SAlex Elder 
686650d1603SAlex Elder 	/* Note: index *must* be used modulo the ring count here */
687650d1603SAlex Elder 	val = gsi_ring_addr(ring, (index - 1) % ring->count);
688650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_EV_CH_E_DOORBELL_0_OFFSET(evt_ring_id));
689650d1603SAlex Elder }
690650d1603SAlex Elder 
691650d1603SAlex Elder /* Program an event ring for use */
692650d1603SAlex Elder static void gsi_evt_ring_program(struct gsi *gsi, u32 evt_ring_id)
693650d1603SAlex Elder {
694650d1603SAlex Elder 	struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
695650d1603SAlex Elder 	size_t size = evt_ring->ring.count * GSI_RING_ELEMENT_SIZE;
696650d1603SAlex Elder 	u32 val;
697650d1603SAlex Elder 
69846dda53eSAlex Elder 	/* We program all event rings as GPI type/protocol */
69946dda53eSAlex Elder 	val = u32_encode_bits(GSI_CHANNEL_TYPE_GPI, EV_CHTYPE_FMASK);
700650d1603SAlex Elder 	val |= EV_INTYPE_FMASK;
701650d1603SAlex Elder 	val |= u32_encode_bits(GSI_RING_ELEMENT_SIZE, EV_ELEMENT_SIZE_FMASK);
702650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_0_OFFSET(evt_ring_id));
703650d1603SAlex Elder 
704650d1603SAlex Elder 	val = u32_encode_bits(size, EV_R_LENGTH_FMASK);
705650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_1_OFFSET(evt_ring_id));
706650d1603SAlex Elder 
707650d1603SAlex Elder 	/* The context 2 and 3 registers store the low-order and
708650d1603SAlex Elder 	 * high-order 32 bits of the address of the event ring,
709650d1603SAlex Elder 	 * respectively.
710650d1603SAlex Elder 	 */
7113c54b7beSAlex Elder 	val = lower_32_bits(evt_ring->ring.addr);
712650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_2_OFFSET(evt_ring_id));
7133c54b7beSAlex Elder 	val = upper_32_bits(evt_ring->ring.addr);
714650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_3_OFFSET(evt_ring_id));
715650d1603SAlex Elder 
716650d1603SAlex Elder 	/* Enable interrupt moderation by setting the moderation delay */
717650d1603SAlex Elder 	val = u32_encode_bits(GSI_EVT_RING_INT_MODT, MODT_FMASK);
718650d1603SAlex Elder 	val |= u32_encode_bits(1, MODC_FMASK);	/* comes from channel */
719650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_8_OFFSET(evt_ring_id));
720650d1603SAlex Elder 
721650d1603SAlex Elder 	/* No MSI write data, and MSI address high and low address is 0 */
722650d1603SAlex Elder 	iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_9_OFFSET(evt_ring_id));
723650d1603SAlex Elder 	iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_10_OFFSET(evt_ring_id));
724650d1603SAlex Elder 	iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_11_OFFSET(evt_ring_id));
725650d1603SAlex Elder 
726650d1603SAlex Elder 	/* We don't need to get event read pointer updates */
727650d1603SAlex Elder 	iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_12_OFFSET(evt_ring_id));
728650d1603SAlex Elder 	iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_13_OFFSET(evt_ring_id));
729650d1603SAlex Elder 
730650d1603SAlex Elder 	/* Finally, tell the hardware we've completed event 0 (arbitrary) */
731650d1603SAlex Elder 	gsi_evt_ring_doorbell(gsi, evt_ring_id, 0);
732650d1603SAlex Elder }
733650d1603SAlex Elder 
734e6316920SAlex Elder /* Find the transaction whose completion indicates a channel is quiesced */
735650d1603SAlex Elder static struct gsi_trans *gsi_channel_trans_last(struct gsi_channel *channel)
736650d1603SAlex Elder {
737650d1603SAlex Elder 	struct gsi_trans_info *trans_info = &channel->trans_info;
738e6316920SAlex Elder 	const struct list_head *list;
739650d1603SAlex Elder 	struct gsi_trans *trans;
740650d1603SAlex Elder 
741650d1603SAlex Elder 	spin_lock_bh(&trans_info->spinlock);
742650d1603SAlex Elder 
743e6316920SAlex Elder 	/* There is a small chance a TX transaction got allocated just
744e6316920SAlex Elder 	 * before we disabled transmits, so check for that.
745e6316920SAlex Elder 	 */
746e6316920SAlex Elder 	if (channel->toward_ipa) {
747e6316920SAlex Elder 		list = &trans_info->alloc;
748e6316920SAlex Elder 		if (!list_empty(list))
749e6316920SAlex Elder 			goto done;
750e6316920SAlex Elder 		list = &trans_info->pending;
751e6316920SAlex Elder 		if (!list_empty(list))
752e6316920SAlex Elder 			goto done;
753e6316920SAlex Elder 	}
754e6316920SAlex Elder 
755e6316920SAlex Elder 	/* Otherwise (TX or RX) we want to wait for anything that
756e6316920SAlex Elder 	 * has completed, or has been polled but not released yet.
757e6316920SAlex Elder 	 */
758e6316920SAlex Elder 	list = &trans_info->complete;
759e6316920SAlex Elder 	if (!list_empty(list))
760e6316920SAlex Elder 		goto done;
761e6316920SAlex Elder 	list = &trans_info->polled;
762e6316920SAlex Elder 	if (list_empty(list))
763e6316920SAlex Elder 		list = NULL;
764e6316920SAlex Elder done:
765e6316920SAlex Elder 	trans = list ? list_last_entry(list, struct gsi_trans, links) : NULL;
766650d1603SAlex Elder 
767650d1603SAlex Elder 	/* Caller will wait for this, so take a reference */
768650d1603SAlex Elder 	if (trans)
769650d1603SAlex Elder 		refcount_inc(&trans->refcount);
770650d1603SAlex Elder 
771650d1603SAlex Elder 	spin_unlock_bh(&trans_info->spinlock);
772650d1603SAlex Elder 
773650d1603SAlex Elder 	return trans;
774650d1603SAlex Elder }
775650d1603SAlex Elder 
776650d1603SAlex Elder /* Wait for transaction activity on a channel to complete */
777650d1603SAlex Elder static void gsi_channel_trans_quiesce(struct gsi_channel *channel)
778650d1603SAlex Elder {
779650d1603SAlex Elder 	struct gsi_trans *trans;
780650d1603SAlex Elder 
781650d1603SAlex Elder 	/* Get the last transaction, and wait for it to complete */
782650d1603SAlex Elder 	trans = gsi_channel_trans_last(channel);
783650d1603SAlex Elder 	if (trans) {
784650d1603SAlex Elder 		wait_for_completion(&trans->completion);
785650d1603SAlex Elder 		gsi_trans_free(trans);
786650d1603SAlex Elder 	}
787650d1603SAlex Elder }
788650d1603SAlex Elder 
789650d1603SAlex Elder /* Program a channel for use */
790650d1603SAlex Elder static void gsi_channel_program(struct gsi_channel *channel, bool doorbell)
791650d1603SAlex Elder {
792650d1603SAlex Elder 	size_t size = channel->tre_ring.count * GSI_RING_ELEMENT_SIZE;
793650d1603SAlex Elder 	u32 channel_id = gsi_channel_id(channel);
794650d1603SAlex Elder 	union gsi_channel_scratch scr = { };
795650d1603SAlex Elder 	struct gsi_channel_scratch_gpi *gpi;
796650d1603SAlex Elder 	struct gsi *gsi = channel->gsi;
797650d1603SAlex Elder 	u32 wrr_weight = 0;
798650d1603SAlex Elder 	u32 val;
799650d1603SAlex Elder 
800650d1603SAlex Elder 	/* Arbitrarily pick TRE 0 as the first channel element to use */
801650d1603SAlex Elder 	channel->tre_ring.index = 0;
802650d1603SAlex Elder 
80346dda53eSAlex Elder 	/* We program all channels as GPI type/protocol */
80446dda53eSAlex Elder 	val = u32_encode_bits(GSI_CHANNEL_TYPE_GPI, CHTYPE_PROTOCOL_FMASK);
805650d1603SAlex Elder 	if (channel->toward_ipa)
806650d1603SAlex Elder 		val |= CHTYPE_DIR_FMASK;
807650d1603SAlex Elder 	val |= u32_encode_bits(channel->evt_ring_id, ERINDEX_FMASK);
808650d1603SAlex Elder 	val |= u32_encode_bits(GSI_RING_ELEMENT_SIZE, ELEMENT_SIZE_FMASK);
809650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CH_C_CNTXT_0_OFFSET(channel_id));
810650d1603SAlex Elder 
811650d1603SAlex Elder 	val = u32_encode_bits(size, R_LENGTH_FMASK);
812650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CH_C_CNTXT_1_OFFSET(channel_id));
813650d1603SAlex Elder 
814650d1603SAlex Elder 	/* The context 2 and 3 registers store the low-order and
815650d1603SAlex Elder 	 * high-order 32 bits of the address of the channel ring,
816650d1603SAlex Elder 	 * respectively.
817650d1603SAlex Elder 	 */
8183c54b7beSAlex Elder 	val = lower_32_bits(channel->tre_ring.addr);
819650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CH_C_CNTXT_2_OFFSET(channel_id));
8203c54b7beSAlex Elder 	val = upper_32_bits(channel->tre_ring.addr);
821650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CH_C_CNTXT_3_OFFSET(channel_id));
822650d1603SAlex Elder 
823650d1603SAlex Elder 	/* Command channel gets low weighted round-robin priority */
824650d1603SAlex Elder 	if (channel->command)
825650d1603SAlex Elder 		wrr_weight = field_max(WRR_WEIGHT_FMASK);
826650d1603SAlex Elder 	val = u32_encode_bits(wrr_weight, WRR_WEIGHT_FMASK);
827650d1603SAlex Elder 
828650d1603SAlex Elder 	/* Max prefetch is 1 segment (do not set MAX_PREFETCH_FMASK) */
829650d1603SAlex Elder 
830*d7f3087bSAlex Elder 	/* No need to use the doorbell engine starting at IPA v4.0 */
831*d7f3087bSAlex Elder 	if (gsi->version < IPA_VERSION_4_0 && doorbell)
832650d1603SAlex Elder 		val |= USE_DB_ENG_FMASK;
833650d1603SAlex Elder 
8349f848198SAlex Elder 	/* v4.0 introduces an escape buffer for prefetch.  We use it
8359f848198SAlex Elder 	 * on all but the AP command channel.
8369f848198SAlex Elder 	 */
837*d7f3087bSAlex Elder 	if (gsi->version >= IPA_VERSION_4_0 && !channel->command) {
838b0b6f0ddSAlex Elder 		/* If not otherwise set, prefetch buffers are used */
839b0b6f0ddSAlex Elder 		if (gsi->version < IPA_VERSION_4_5)
840650d1603SAlex Elder 			val |= USE_ESCAPE_BUF_ONLY_FMASK;
841b0b6f0ddSAlex Elder 		else
842b0b6f0ddSAlex Elder 			val |= u32_encode_bits(GSI_ESCAPE_BUF_ONLY,
843b0b6f0ddSAlex Elder 					       PREFETCH_MODE_FMASK);
844b0b6f0ddSAlex Elder 	}
845650d1603SAlex Elder 
846650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CH_C_QOS_OFFSET(channel_id));
847650d1603SAlex Elder 
848650d1603SAlex Elder 	/* Now update the scratch registers for GPI protocol */
849650d1603SAlex Elder 	gpi = &scr.gpi;
850650d1603SAlex Elder 	gpi->max_outstanding_tre = gsi_channel_trans_tre_max(gsi, channel_id) *
851650d1603SAlex Elder 					GSI_RING_ELEMENT_SIZE;
852650d1603SAlex Elder 	gpi->outstanding_threshold = 2 * GSI_RING_ELEMENT_SIZE;
853650d1603SAlex Elder 
854650d1603SAlex Elder 	val = scr.data.word1;
855650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CH_C_SCRATCH_0_OFFSET(channel_id));
856650d1603SAlex Elder 
857650d1603SAlex Elder 	val = scr.data.word2;
858650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CH_C_SCRATCH_1_OFFSET(channel_id));
859650d1603SAlex Elder 
860650d1603SAlex Elder 	val = scr.data.word3;
861650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CH_C_SCRATCH_2_OFFSET(channel_id));
862650d1603SAlex Elder 
863650d1603SAlex Elder 	/* We must preserve the upper 16 bits of the last scratch register.
864650d1603SAlex Elder 	 * The next sequence assumes those bits remain unchanged between the
865650d1603SAlex Elder 	 * read and the write.
866650d1603SAlex Elder 	 */
867650d1603SAlex Elder 	val = ioread32(gsi->virt + GSI_CH_C_SCRATCH_3_OFFSET(channel_id));
868650d1603SAlex Elder 	val = (scr.data.word4 & GENMASK(31, 16)) | (val & GENMASK(15, 0));
869650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CH_C_SCRATCH_3_OFFSET(channel_id));
870650d1603SAlex Elder 
871650d1603SAlex Elder 	/* All done! */
872650d1603SAlex Elder }
873650d1603SAlex Elder 
874650d1603SAlex Elder static void gsi_channel_deprogram(struct gsi_channel *channel)
875650d1603SAlex Elder {
876650d1603SAlex Elder 	/* Nothing to do */
877650d1603SAlex Elder }
878650d1603SAlex Elder 
879893b838eSAlex Elder static int __gsi_channel_start(struct gsi_channel *channel, bool start)
880650d1603SAlex Elder {
881893b838eSAlex Elder 	struct gsi *gsi = channel->gsi;
882650d1603SAlex Elder 	int ret;
883650d1603SAlex Elder 
884a65c0288SAlex Elder 	if (!start)
885a65c0288SAlex Elder 		return 0;
8864fef691cSAlex Elder 
887650d1603SAlex Elder 	mutex_lock(&gsi->mutex);
888650d1603SAlex Elder 
889a65c0288SAlex Elder 	ret = gsi_channel_start_command(channel);
890650d1603SAlex Elder 
891650d1603SAlex Elder 	mutex_unlock(&gsi->mutex);
892650d1603SAlex Elder 
893650d1603SAlex Elder 	return ret;
894650d1603SAlex Elder }
895650d1603SAlex Elder 
896893b838eSAlex Elder /* Start an allocated GSI channel */
897893b838eSAlex Elder int gsi_channel_start(struct gsi *gsi, u32 channel_id)
898893b838eSAlex Elder {
899893b838eSAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
900a65c0288SAlex Elder 	int ret;
901893b838eSAlex Elder 
902a65c0288SAlex Elder 	/* Enable NAPI and the completion interrupt */
903a65c0288SAlex Elder 	napi_enable(&channel->napi);
904a65c0288SAlex Elder 	gsi_irq_ieob_enable_one(gsi, channel->evt_ring_id);
905a65c0288SAlex Elder 
906a65c0288SAlex Elder 	ret = __gsi_channel_start(channel, true);
907a65c0288SAlex Elder 	if (ret) {
908a65c0288SAlex Elder 		gsi_irq_ieob_disable_one(gsi, channel->evt_ring_id);
909a65c0288SAlex Elder 		napi_disable(&channel->napi);
910a65c0288SAlex Elder 	}
911a65c0288SAlex Elder 
912a65c0288SAlex Elder 	return ret;
913893b838eSAlex Elder }
914893b838eSAlex Elder 
915697e834eSAlex Elder static int gsi_channel_stop_retry(struct gsi_channel *channel)
916650d1603SAlex Elder {
917057ef63fSAlex Elder 	u32 retries = GSI_CHANNEL_STOP_RETRIES;
918650d1603SAlex Elder 	int ret;
919650d1603SAlex Elder 
920650d1603SAlex Elder 	do {
921650d1603SAlex Elder 		ret = gsi_channel_stop_command(channel);
922650d1603SAlex Elder 		if (ret != -EAGAIN)
923650d1603SAlex Elder 			break;
9243d60e15fSAlex Elder 		usleep_range(3 * USEC_PER_MSEC, 5 * USEC_PER_MSEC);
925650d1603SAlex Elder 	} while (retries--);
926650d1603SAlex Elder 
927697e834eSAlex Elder 	return ret;
928697e834eSAlex Elder }
929697e834eSAlex Elder 
930893b838eSAlex Elder static int __gsi_channel_stop(struct gsi_channel *channel, bool stop)
931697e834eSAlex Elder {
93263ec9be1SAlex Elder 	struct gsi *gsi = channel->gsi;
933697e834eSAlex Elder 	int ret;
934697e834eSAlex Elder 
935a65c0288SAlex Elder 	/* Wait for any underway transactions to complete before stopping. */
936bd1ea1e4SAlex Elder 	gsi_channel_trans_quiesce(channel);
937697e834eSAlex Elder 
93863ec9be1SAlex Elder 	if (!stop)
93963ec9be1SAlex Elder 		return 0;
94063ec9be1SAlex Elder 
94163ec9be1SAlex Elder 	mutex_lock(&gsi->mutex);
94263ec9be1SAlex Elder 
94363ec9be1SAlex Elder 	ret = gsi_channel_stop_retry(channel);
94463ec9be1SAlex Elder 
94563ec9be1SAlex Elder 	mutex_unlock(&gsi->mutex);
94663ec9be1SAlex Elder 
94763ec9be1SAlex Elder 	return ret;
948650d1603SAlex Elder }
949650d1603SAlex Elder 
950893b838eSAlex Elder /* Stop a started channel */
951893b838eSAlex Elder int gsi_channel_stop(struct gsi *gsi, u32 channel_id)
952893b838eSAlex Elder {
953893b838eSAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
954a65c0288SAlex Elder 	int ret;
955893b838eSAlex Elder 
956a65c0288SAlex Elder 	ret = __gsi_channel_stop(channel, true);
957a65c0288SAlex Elder 	if (ret)
958a65c0288SAlex Elder 		return ret;
959a65c0288SAlex Elder 
96063ec9be1SAlex Elder 	/* Disable the completion interrupt and NAPI if successful */
961a65c0288SAlex Elder 	gsi_irq_ieob_disable_one(gsi, channel->evt_ring_id);
962a65c0288SAlex Elder 	napi_disable(&channel->napi);
963a65c0288SAlex Elder 
964a65c0288SAlex Elder 	return 0;
965893b838eSAlex Elder }
966893b838eSAlex Elder 
967ce54993dSAlex Elder /* Reset and reconfigure a channel, (possibly) enabling the doorbell engine */
968ce54993dSAlex Elder void gsi_channel_reset(struct gsi *gsi, u32 channel_id, bool doorbell)
969650d1603SAlex Elder {
970650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
971650d1603SAlex Elder 
972650d1603SAlex Elder 	mutex_lock(&gsi->mutex);
973650d1603SAlex Elder 
974650d1603SAlex Elder 	gsi_channel_reset_command(channel);
975a3f2405bSAlex Elder 	/* Due to a hardware quirk we may need to reset RX channels twice. */
976*d7f3087bSAlex Elder 	if (gsi->version < IPA_VERSION_4_0 && !channel->toward_ipa)
977650d1603SAlex Elder 		gsi_channel_reset_command(channel);
978650d1603SAlex Elder 
979ce54993dSAlex Elder 	gsi_channel_program(channel, doorbell);
980650d1603SAlex Elder 	gsi_channel_trans_cancel_pending(channel);
981650d1603SAlex Elder 
982650d1603SAlex Elder 	mutex_unlock(&gsi->mutex);
983650d1603SAlex Elder }
984650d1603SAlex Elder 
985650d1603SAlex Elder /* Stop a STARTED channel for suspend (using stop if requested) */
986650d1603SAlex Elder int gsi_channel_suspend(struct gsi *gsi, u32 channel_id, bool stop)
987650d1603SAlex Elder {
988650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
989b1750723SAlex Elder 	int ret;
990650d1603SAlex Elder 
991b1750723SAlex Elder 	ret = __gsi_channel_stop(channel, stop);
992b1750723SAlex Elder 	if (ret)
993b1750723SAlex Elder 		return ret;
994b1750723SAlex Elder 
995b1750723SAlex Elder 	/* Ensure NAPI polling has finished. */
996b1750723SAlex Elder 	napi_synchronize(&channel->napi);
997b1750723SAlex Elder 
998b1750723SAlex Elder 	return 0;
999650d1603SAlex Elder }
1000650d1603SAlex Elder 
1001650d1603SAlex Elder /* Resume a suspended channel (starting will be requested if STOPPED) */
1002650d1603SAlex Elder int gsi_channel_resume(struct gsi *gsi, u32 channel_id, bool start)
1003650d1603SAlex Elder {
1004650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
1005650d1603SAlex Elder 
1006893b838eSAlex Elder 	return __gsi_channel_start(channel, start);
1007650d1603SAlex Elder }
1008650d1603SAlex Elder 
1009650d1603SAlex Elder /**
1010650d1603SAlex Elder  * gsi_channel_tx_queued() - Report queued TX transfers for a channel
1011650d1603SAlex Elder  * @channel:	Channel for which to report
1012650d1603SAlex Elder  *
1013650d1603SAlex Elder  * Report to the network stack the number of bytes and transactions that
1014650d1603SAlex Elder  * have been queued to hardware since last call.  This and the next function
1015650d1603SAlex Elder  * supply information used by the network stack for throttling.
1016650d1603SAlex Elder  *
1017650d1603SAlex Elder  * For each channel we track the number of transactions used and bytes of
1018650d1603SAlex Elder  * data those transactions represent.  We also track what those values are
1019650d1603SAlex Elder  * each time this function is called.  Subtracting the two tells us
1020650d1603SAlex Elder  * the number of bytes and transactions that have been added between
1021650d1603SAlex Elder  * successive calls.
1022650d1603SAlex Elder  *
1023650d1603SAlex Elder  * Calling this each time we ring the channel doorbell allows us to
1024650d1603SAlex Elder  * provide accurate information to the network stack about how much
1025650d1603SAlex Elder  * work we've given the hardware at any point in time.
1026650d1603SAlex Elder  */
1027650d1603SAlex Elder void gsi_channel_tx_queued(struct gsi_channel *channel)
1028650d1603SAlex Elder {
1029650d1603SAlex Elder 	u32 trans_count;
1030650d1603SAlex Elder 	u32 byte_count;
1031650d1603SAlex Elder 
1032650d1603SAlex Elder 	byte_count = channel->byte_count - channel->queued_byte_count;
1033650d1603SAlex Elder 	trans_count = channel->trans_count - channel->queued_trans_count;
1034650d1603SAlex Elder 	channel->queued_byte_count = channel->byte_count;
1035650d1603SAlex Elder 	channel->queued_trans_count = channel->trans_count;
1036650d1603SAlex Elder 
1037650d1603SAlex Elder 	ipa_gsi_channel_tx_queued(channel->gsi, gsi_channel_id(channel),
1038650d1603SAlex Elder 				  trans_count, byte_count);
1039650d1603SAlex Elder }
1040650d1603SAlex Elder 
1041650d1603SAlex Elder /**
1042650d1603SAlex Elder  * gsi_channel_tx_update() - Report completed TX transfers
1043650d1603SAlex Elder  * @channel:	Channel that has completed transmitting packets
1044650d1603SAlex Elder  * @trans:	Last transation known to be complete
1045650d1603SAlex Elder  *
1046650d1603SAlex Elder  * Compute the number of transactions and bytes that have been transferred
1047650d1603SAlex Elder  * over a TX channel since the given transaction was committed.  Report this
1048650d1603SAlex Elder  * information to the network stack.
1049650d1603SAlex Elder  *
1050650d1603SAlex Elder  * At the time a transaction is committed, we record its channel's
1051650d1603SAlex Elder  * committed transaction and byte counts *in the transaction*.
1052650d1603SAlex Elder  * Completions are signaled by the hardware with an interrupt, and
1053650d1603SAlex Elder  * we can determine the latest completed transaction at that time.
1054650d1603SAlex Elder  *
1055650d1603SAlex Elder  * The difference between the byte/transaction count recorded in
1056650d1603SAlex Elder  * the transaction and the count last time we recorded a completion
1057650d1603SAlex Elder  * tells us exactly how much data has been transferred between
1058650d1603SAlex Elder  * completions.
1059650d1603SAlex Elder  *
1060650d1603SAlex Elder  * Calling this each time we learn of a newly-completed transaction
1061650d1603SAlex Elder  * allows us to provide accurate information to the network stack
1062650d1603SAlex Elder  * about how much work has been completed by the hardware at a given
1063650d1603SAlex Elder  * point in time.
1064650d1603SAlex Elder  */
1065650d1603SAlex Elder static void
1066650d1603SAlex Elder gsi_channel_tx_update(struct gsi_channel *channel, struct gsi_trans *trans)
1067650d1603SAlex Elder {
1068650d1603SAlex Elder 	u64 byte_count = trans->byte_count + trans->len;
1069650d1603SAlex Elder 	u64 trans_count = trans->trans_count + 1;
1070650d1603SAlex Elder 
1071650d1603SAlex Elder 	byte_count -= channel->compl_byte_count;
1072650d1603SAlex Elder 	channel->compl_byte_count += byte_count;
1073650d1603SAlex Elder 	trans_count -= channel->compl_trans_count;
1074650d1603SAlex Elder 	channel->compl_trans_count += trans_count;
1075650d1603SAlex Elder 
1076650d1603SAlex Elder 	ipa_gsi_channel_tx_completed(channel->gsi, gsi_channel_id(channel),
1077650d1603SAlex Elder 				     trans_count, byte_count);
1078650d1603SAlex Elder }
1079650d1603SAlex Elder 
1080650d1603SAlex Elder /* Channel control interrupt handler */
1081650d1603SAlex Elder static void gsi_isr_chan_ctrl(struct gsi *gsi)
1082650d1603SAlex Elder {
1083650d1603SAlex Elder 	u32 channel_mask;
1084650d1603SAlex Elder 
1085650d1603SAlex Elder 	channel_mask = ioread32(gsi->virt + GSI_CNTXT_SRC_CH_IRQ_OFFSET);
1086650d1603SAlex Elder 	iowrite32(channel_mask, gsi->virt + GSI_CNTXT_SRC_CH_IRQ_CLR_OFFSET);
1087650d1603SAlex Elder 
1088650d1603SAlex Elder 	while (channel_mask) {
1089650d1603SAlex Elder 		u32 channel_id = __ffs(channel_mask);
1090650d1603SAlex Elder 		struct gsi_channel *channel;
1091650d1603SAlex Elder 
1092650d1603SAlex Elder 		channel_mask ^= BIT(channel_id);
1093650d1603SAlex Elder 
1094650d1603SAlex Elder 		channel = &gsi->channel[channel_id];
1095650d1603SAlex Elder 
1096650d1603SAlex Elder 		complete(&channel->completion);
1097650d1603SAlex Elder 	}
1098650d1603SAlex Elder }
1099650d1603SAlex Elder 
1100650d1603SAlex Elder /* Event ring control interrupt handler */
1101650d1603SAlex Elder static void gsi_isr_evt_ctrl(struct gsi *gsi)
1102650d1603SAlex Elder {
1103650d1603SAlex Elder 	u32 event_mask;
1104650d1603SAlex Elder 
1105650d1603SAlex Elder 	event_mask = ioread32(gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_OFFSET);
1106650d1603SAlex Elder 	iowrite32(event_mask, gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_CLR_OFFSET);
1107650d1603SAlex Elder 
1108650d1603SAlex Elder 	while (event_mask) {
1109650d1603SAlex Elder 		u32 evt_ring_id = __ffs(event_mask);
1110650d1603SAlex Elder 		struct gsi_evt_ring *evt_ring;
1111650d1603SAlex Elder 
1112650d1603SAlex Elder 		event_mask ^= BIT(evt_ring_id);
1113650d1603SAlex Elder 
1114650d1603SAlex Elder 		evt_ring = &gsi->evt_ring[evt_ring_id];
1115650d1603SAlex Elder 
1116650d1603SAlex Elder 		complete(&evt_ring->completion);
1117650d1603SAlex Elder 	}
1118650d1603SAlex Elder }
1119650d1603SAlex Elder 
1120650d1603SAlex Elder /* Global channel error interrupt handler */
1121650d1603SAlex Elder static void
1122650d1603SAlex Elder gsi_isr_glob_chan_err(struct gsi *gsi, u32 err_ee, u32 channel_id, u32 code)
1123650d1603SAlex Elder {
11247b0ac8f6SAlex Elder 	if (code == GSI_OUT_OF_RESOURCES) {
1125650d1603SAlex Elder 		dev_err(gsi->dev, "channel %u out of resources\n", channel_id);
1126650d1603SAlex Elder 		complete(&gsi->channel[channel_id].completion);
1127650d1603SAlex Elder 		return;
1128650d1603SAlex Elder 	}
1129650d1603SAlex Elder 
1130650d1603SAlex Elder 	/* Report, but otherwise ignore all other error codes */
1131650d1603SAlex Elder 	dev_err(gsi->dev, "channel %u global error ee 0x%08x code 0x%08x\n",
1132650d1603SAlex Elder 		channel_id, err_ee, code);
1133650d1603SAlex Elder }
1134650d1603SAlex Elder 
1135650d1603SAlex Elder /* Global event error interrupt handler */
1136650d1603SAlex Elder static void
1137650d1603SAlex Elder gsi_isr_glob_evt_err(struct gsi *gsi, u32 err_ee, u32 evt_ring_id, u32 code)
1138650d1603SAlex Elder {
11397b0ac8f6SAlex Elder 	if (code == GSI_OUT_OF_RESOURCES) {
1140650d1603SAlex Elder 		struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
1141650d1603SAlex Elder 		u32 channel_id = gsi_channel_id(evt_ring->channel);
1142650d1603SAlex Elder 
1143650d1603SAlex Elder 		complete(&evt_ring->completion);
1144650d1603SAlex Elder 		dev_err(gsi->dev, "evt_ring for channel %u out of resources\n",
1145650d1603SAlex Elder 			channel_id);
1146650d1603SAlex Elder 		return;
1147650d1603SAlex Elder 	}
1148650d1603SAlex Elder 
1149650d1603SAlex Elder 	/* Report, but otherwise ignore all other error codes */
1150650d1603SAlex Elder 	dev_err(gsi->dev, "event ring %u global error ee %u code 0x%08x\n",
1151650d1603SAlex Elder 		evt_ring_id, err_ee, code);
1152650d1603SAlex Elder }
1153650d1603SAlex Elder 
1154650d1603SAlex Elder /* Global error interrupt handler */
1155650d1603SAlex Elder static void gsi_isr_glob_err(struct gsi *gsi)
1156650d1603SAlex Elder {
1157650d1603SAlex Elder 	enum gsi_err_type type;
1158650d1603SAlex Elder 	enum gsi_err_code code;
1159650d1603SAlex Elder 	u32 which;
1160650d1603SAlex Elder 	u32 val;
1161650d1603SAlex Elder 	u32 ee;
1162650d1603SAlex Elder 
1163650d1603SAlex Elder 	/* Get the logged error, then reinitialize the log */
1164650d1603SAlex Elder 	val = ioread32(gsi->virt + GSI_ERROR_LOG_OFFSET);
1165650d1603SAlex Elder 	iowrite32(0, gsi->virt + GSI_ERROR_LOG_OFFSET);
1166650d1603SAlex Elder 	iowrite32(~0, gsi->virt + GSI_ERROR_LOG_CLR_OFFSET);
1167650d1603SAlex Elder 
1168650d1603SAlex Elder 	ee = u32_get_bits(val, ERR_EE_FMASK);
1169650d1603SAlex Elder 	type = u32_get_bits(val, ERR_TYPE_FMASK);
1170d6c9e3f5SAlex Elder 	which = u32_get_bits(val, ERR_VIRT_IDX_FMASK);
1171650d1603SAlex Elder 	code = u32_get_bits(val, ERR_CODE_FMASK);
1172650d1603SAlex Elder 
1173650d1603SAlex Elder 	if (type == GSI_ERR_TYPE_CHAN)
1174650d1603SAlex Elder 		gsi_isr_glob_chan_err(gsi, ee, which, code);
1175650d1603SAlex Elder 	else if (type == GSI_ERR_TYPE_EVT)
1176650d1603SAlex Elder 		gsi_isr_glob_evt_err(gsi, ee, which, code);
1177650d1603SAlex Elder 	else	/* type GSI_ERR_TYPE_GLOB should be fatal */
1178650d1603SAlex Elder 		dev_err(gsi->dev, "unexpected global error 0x%08x\n", type);
1179650d1603SAlex Elder }
1180650d1603SAlex Elder 
1181650d1603SAlex Elder /* Generic EE interrupt handler */
1182650d1603SAlex Elder static void gsi_isr_gp_int1(struct gsi *gsi)
1183650d1603SAlex Elder {
1184650d1603SAlex Elder 	u32 result;
1185650d1603SAlex Elder 	u32 val;
1186650d1603SAlex Elder 
1187f849afccSAlex Elder 	/* This interrupt is used to handle completions of the two GENERIC
1188f849afccSAlex Elder 	 * GSI commands.  We use these to allocate and halt channels on
1189f849afccSAlex Elder 	 * the modem's behalf due to a hardware quirk on IPA v4.2.  Once
1190f849afccSAlex Elder 	 * allocated, the modem "owns" these channels, and as a result we
1191f849afccSAlex Elder 	 * have no way of knowing the channel's state at any given time.
1192f849afccSAlex Elder 	 *
1193f849afccSAlex Elder 	 * It is recommended that we halt the modem channels we allocated
1194f849afccSAlex Elder 	 * when shutting down, but it's possible the channel isn't running
1195f849afccSAlex Elder 	 * at the time we issue the HALT command.  We'll get an error in
1196f849afccSAlex Elder 	 * that case, but it's harmless (the channel is already halted).
1197f849afccSAlex Elder 	 *
1198f849afccSAlex Elder 	 * For this reason, we silently ignore a CHANNEL_NOT_RUNNING error
1199f849afccSAlex Elder 	 * if we receive it.
1200f849afccSAlex Elder 	 */
1201650d1603SAlex Elder 	val = ioread32(gsi->virt + GSI_CNTXT_SCRATCH_0_OFFSET);
1202650d1603SAlex Elder 	result = u32_get_bits(val, GENERIC_EE_RESULT_FMASK);
1203f849afccSAlex Elder 
1204f849afccSAlex Elder 	switch (result) {
1205f849afccSAlex Elder 	case GENERIC_EE_SUCCESS:
1206f849afccSAlex Elder 	case GENERIC_EE_CHANNEL_NOT_RUNNING:
120711361456SAlex Elder 		gsi->result = 0;
120811361456SAlex Elder 		break;
120911361456SAlex Elder 
121011361456SAlex Elder 	case GENERIC_EE_RETRY:
121111361456SAlex Elder 		gsi->result = -EAGAIN;
1212f849afccSAlex Elder 		break;
1213f849afccSAlex Elder 
1214f849afccSAlex Elder 	default:
1215650d1603SAlex Elder 		dev_err(gsi->dev, "global INT1 generic result %u\n", result);
121611361456SAlex Elder 		gsi->result = -EIO;
1217f849afccSAlex Elder 		break;
1218f849afccSAlex Elder 	}
1219650d1603SAlex Elder 
1220650d1603SAlex Elder 	complete(&gsi->completion);
1221650d1603SAlex Elder }
12220b1ba18aSAlex Elder 
1223650d1603SAlex Elder /* Inter-EE interrupt handler */
1224650d1603SAlex Elder static void gsi_isr_glob_ee(struct gsi *gsi)
1225650d1603SAlex Elder {
1226650d1603SAlex Elder 	u32 val;
1227650d1603SAlex Elder 
1228650d1603SAlex Elder 	val = ioread32(gsi->virt + GSI_CNTXT_GLOB_IRQ_STTS_OFFSET);
1229650d1603SAlex Elder 
12306c6358ccSAlex Elder 	if (val & BIT(ERROR_INT))
1231650d1603SAlex Elder 		gsi_isr_glob_err(gsi);
1232650d1603SAlex Elder 
1233650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CNTXT_GLOB_IRQ_CLR_OFFSET);
1234650d1603SAlex Elder 
12356c6358ccSAlex Elder 	val &= ~BIT(ERROR_INT);
1236650d1603SAlex Elder 
12376c6358ccSAlex Elder 	if (val & BIT(GP_INT1)) {
12386c6358ccSAlex Elder 		val ^= BIT(GP_INT1);
1239650d1603SAlex Elder 		gsi_isr_gp_int1(gsi);
1240650d1603SAlex Elder 	}
1241650d1603SAlex Elder 
1242650d1603SAlex Elder 	if (val)
1243650d1603SAlex Elder 		dev_err(gsi->dev, "unexpected global interrupt 0x%08x\n", val);
1244650d1603SAlex Elder }
1245650d1603SAlex Elder 
1246650d1603SAlex Elder /* I/O completion interrupt event */
1247650d1603SAlex Elder static void gsi_isr_ieob(struct gsi *gsi)
1248650d1603SAlex Elder {
1249650d1603SAlex Elder 	u32 event_mask;
1250650d1603SAlex Elder 
1251650d1603SAlex Elder 	event_mask = ioread32(gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_OFFSET);
12527bd9785fSAlex Elder 	gsi_irq_ieob_disable(gsi, event_mask);
1253195ef57fSAlex Elder 	iowrite32(event_mask, gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_CLR_OFFSET);
1254650d1603SAlex Elder 
1255650d1603SAlex Elder 	while (event_mask) {
1256650d1603SAlex Elder 		u32 evt_ring_id = __ffs(event_mask);
1257650d1603SAlex Elder 
1258650d1603SAlex Elder 		event_mask ^= BIT(evt_ring_id);
1259650d1603SAlex Elder 
1260650d1603SAlex Elder 		napi_schedule(&gsi->evt_ring[evt_ring_id].channel->napi);
1261650d1603SAlex Elder 	}
1262650d1603SAlex Elder }
1263650d1603SAlex Elder 
1264650d1603SAlex Elder /* General event interrupts represent serious problems, so report them */
1265650d1603SAlex Elder static void gsi_isr_general(struct gsi *gsi)
1266650d1603SAlex Elder {
1267650d1603SAlex Elder 	struct device *dev = gsi->dev;
1268650d1603SAlex Elder 	u32 val;
1269650d1603SAlex Elder 
1270650d1603SAlex Elder 	val = ioread32(gsi->virt + GSI_CNTXT_GSI_IRQ_STTS_OFFSET);
1271650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CNTXT_GSI_IRQ_CLR_OFFSET);
1272650d1603SAlex Elder 
1273650d1603SAlex Elder 	dev_err(dev, "unexpected general interrupt 0x%08x\n", val);
1274650d1603SAlex Elder }
1275650d1603SAlex Elder 
1276650d1603SAlex Elder /**
1277650d1603SAlex Elder  * gsi_isr() - Top level GSI interrupt service routine
1278650d1603SAlex Elder  * @irq:	Interrupt number (ignored)
1279650d1603SAlex Elder  * @dev_id:	GSI pointer supplied to request_irq()
1280650d1603SAlex Elder  *
1281650d1603SAlex Elder  * This is the main handler function registered for the GSI IRQ. Each type
1282650d1603SAlex Elder  * of interrupt has a separate handler function that is called from here.
1283650d1603SAlex Elder  */
1284650d1603SAlex Elder static irqreturn_t gsi_isr(int irq, void *dev_id)
1285650d1603SAlex Elder {
1286650d1603SAlex Elder 	struct gsi *gsi = dev_id;
1287650d1603SAlex Elder 	u32 intr_mask;
1288650d1603SAlex Elder 	u32 cnt = 0;
1289650d1603SAlex Elder 
1290f9b28804SAlex Elder 	/* enum gsi_irq_type_id defines GSI interrupt types */
1291650d1603SAlex Elder 	while ((intr_mask = ioread32(gsi->virt + GSI_CNTXT_TYPE_IRQ_OFFSET))) {
1292650d1603SAlex Elder 		/* intr_mask contains bitmask of pending GSI interrupts */
1293650d1603SAlex Elder 		do {
1294650d1603SAlex Elder 			u32 gsi_intr = BIT(__ffs(intr_mask));
1295650d1603SAlex Elder 
1296650d1603SAlex Elder 			intr_mask ^= gsi_intr;
1297650d1603SAlex Elder 
1298650d1603SAlex Elder 			switch (gsi_intr) {
1299f9b28804SAlex Elder 			case BIT(GSI_CH_CTRL):
1300650d1603SAlex Elder 				gsi_isr_chan_ctrl(gsi);
1301650d1603SAlex Elder 				break;
1302f9b28804SAlex Elder 			case BIT(GSI_EV_CTRL):
1303650d1603SAlex Elder 				gsi_isr_evt_ctrl(gsi);
1304650d1603SAlex Elder 				break;
1305f9b28804SAlex Elder 			case BIT(GSI_GLOB_EE):
1306650d1603SAlex Elder 				gsi_isr_glob_ee(gsi);
1307650d1603SAlex Elder 				break;
1308f9b28804SAlex Elder 			case BIT(GSI_IEOB):
1309650d1603SAlex Elder 				gsi_isr_ieob(gsi);
1310650d1603SAlex Elder 				break;
1311f9b28804SAlex Elder 			case BIT(GSI_GENERAL):
1312650d1603SAlex Elder 				gsi_isr_general(gsi);
1313650d1603SAlex Elder 				break;
1314650d1603SAlex Elder 			default:
1315650d1603SAlex Elder 				dev_err(gsi->dev,
13168463488aSAlex Elder 					"unrecognized interrupt type 0x%08x\n",
13178463488aSAlex Elder 					gsi_intr);
1318650d1603SAlex Elder 				break;
1319650d1603SAlex Elder 			}
1320650d1603SAlex Elder 		} while (intr_mask);
1321650d1603SAlex Elder 
1322650d1603SAlex Elder 		if (++cnt > GSI_ISR_MAX_ITER) {
1323650d1603SAlex Elder 			dev_err(gsi->dev, "interrupt flood\n");
1324650d1603SAlex Elder 			break;
1325650d1603SAlex Elder 		}
1326650d1603SAlex Elder 	}
1327650d1603SAlex Elder 
1328650d1603SAlex Elder 	return IRQ_HANDLED;
1329650d1603SAlex Elder }
1330650d1603SAlex Elder 
13310b8d6761SAlex Elder static int gsi_irq_init(struct gsi *gsi, struct platform_device *pdev)
13320b8d6761SAlex Elder {
13330b8d6761SAlex Elder 	struct device *dev = &pdev->dev;
13340b8d6761SAlex Elder 	unsigned int irq;
13350b8d6761SAlex Elder 	int ret;
13360b8d6761SAlex Elder 
13370b8d6761SAlex Elder 	ret = platform_get_irq_byname(pdev, "gsi");
133891306d1dSZihao Tang 	if (ret <= 0)
13390b8d6761SAlex Elder 		return ret ? : -EINVAL;
134091306d1dSZihao Tang 
13410b8d6761SAlex Elder 	irq = ret;
13420b8d6761SAlex Elder 
13430b8d6761SAlex Elder 	ret = request_irq(irq, gsi_isr, 0, "gsi", gsi);
13440b8d6761SAlex Elder 	if (ret) {
13450b8d6761SAlex Elder 		dev_err(dev, "error %d requesting \"gsi\" IRQ\n", ret);
13460b8d6761SAlex Elder 		return ret;
13470b8d6761SAlex Elder 	}
13480b8d6761SAlex Elder 	gsi->irq = irq;
13490b8d6761SAlex Elder 
13500b8d6761SAlex Elder 	return 0;
13510b8d6761SAlex Elder }
13520b8d6761SAlex Elder 
13530b8d6761SAlex Elder static void gsi_irq_exit(struct gsi *gsi)
13540b8d6761SAlex Elder {
13550b8d6761SAlex Elder 	free_irq(gsi->irq, gsi);
13560b8d6761SAlex Elder }
13570b8d6761SAlex Elder 
1358650d1603SAlex Elder /* Return the transaction associated with a transfer completion event */
1359650d1603SAlex Elder static struct gsi_trans *gsi_event_trans(struct gsi_channel *channel,
1360650d1603SAlex Elder 					 struct gsi_event *event)
1361650d1603SAlex Elder {
1362650d1603SAlex Elder 	u32 tre_offset;
1363650d1603SAlex Elder 	u32 tre_index;
1364650d1603SAlex Elder 
1365650d1603SAlex Elder 	/* Event xfer_ptr records the TRE it's associated with */
13663c54b7beSAlex Elder 	tre_offset = lower_32_bits(le64_to_cpu(event->xfer_ptr));
1367650d1603SAlex Elder 	tre_index = gsi_ring_index(&channel->tre_ring, tre_offset);
1368650d1603SAlex Elder 
1369650d1603SAlex Elder 	return gsi_channel_trans_mapped(channel, tre_index);
1370650d1603SAlex Elder }
1371650d1603SAlex Elder 
1372650d1603SAlex Elder /**
1373650d1603SAlex Elder  * gsi_evt_ring_rx_update() - Record lengths of received data
1374650d1603SAlex Elder  * @evt_ring:	Event ring associated with channel that received packets
1375650d1603SAlex Elder  * @index:	Event index in ring reported by hardware
1376650d1603SAlex Elder  *
1377650d1603SAlex Elder  * Events for RX channels contain the actual number of bytes received into
1378650d1603SAlex Elder  * the buffer.  Every event has a transaction associated with it, and here
1379650d1603SAlex Elder  * we update transactions to record their actual received lengths.
1380650d1603SAlex Elder  *
1381650d1603SAlex Elder  * This function is called whenever we learn that the GSI hardware has filled
1382650d1603SAlex Elder  * new events since the last time we checked.  The ring's index field tells
1383650d1603SAlex Elder  * the first entry in need of processing.  The index provided is the
1384650d1603SAlex Elder  * first *unfilled* event in the ring (following the last filled one).
1385650d1603SAlex Elder  *
1386650d1603SAlex Elder  * Events are sequential within the event ring, and transactions are
1387650d1603SAlex Elder  * sequential within the transaction pool.
1388650d1603SAlex Elder  *
1389650d1603SAlex Elder  * Note that @index always refers to an element *within* the event ring.
1390650d1603SAlex Elder  */
1391650d1603SAlex Elder static void gsi_evt_ring_rx_update(struct gsi_evt_ring *evt_ring, u32 index)
1392650d1603SAlex Elder {
1393650d1603SAlex Elder 	struct gsi_channel *channel = evt_ring->channel;
1394650d1603SAlex Elder 	struct gsi_ring *ring = &evt_ring->ring;
1395650d1603SAlex Elder 	struct gsi_trans_info *trans_info;
1396650d1603SAlex Elder 	struct gsi_event *event_done;
1397650d1603SAlex Elder 	struct gsi_event *event;
1398650d1603SAlex Elder 	struct gsi_trans *trans;
1399650d1603SAlex Elder 	u32 byte_count = 0;
1400650d1603SAlex Elder 	u32 old_index;
1401650d1603SAlex Elder 	u32 event_avail;
1402650d1603SAlex Elder 
1403650d1603SAlex Elder 	trans_info = &channel->trans_info;
1404650d1603SAlex Elder 
1405650d1603SAlex Elder 	/* We'll start with the oldest un-processed event.  RX channels
1406650d1603SAlex Elder 	 * replenish receive buffers in single-TRE transactions, so we
1407650d1603SAlex Elder 	 * can just map that event to its transaction.  Transactions
1408650d1603SAlex Elder 	 * associated with completion events are consecutive.
1409650d1603SAlex Elder 	 */
1410650d1603SAlex Elder 	old_index = ring->index;
1411650d1603SAlex Elder 	event = gsi_ring_virt(ring, old_index);
1412650d1603SAlex Elder 	trans = gsi_event_trans(channel, event);
1413650d1603SAlex Elder 
1414650d1603SAlex Elder 	/* Compute the number of events to process before we wrap,
1415650d1603SAlex Elder 	 * and determine when we'll be done processing events.
1416650d1603SAlex Elder 	 */
1417650d1603SAlex Elder 	event_avail = ring->count - old_index % ring->count;
1418650d1603SAlex Elder 	event_done = gsi_ring_virt(ring, index);
1419650d1603SAlex Elder 	do {
1420650d1603SAlex Elder 		trans->len = __le16_to_cpu(event->len);
1421650d1603SAlex Elder 		byte_count += trans->len;
1422650d1603SAlex Elder 
1423650d1603SAlex Elder 		/* Move on to the next event and transaction */
1424650d1603SAlex Elder 		if (--event_avail)
1425650d1603SAlex Elder 			event++;
1426650d1603SAlex Elder 		else
1427650d1603SAlex Elder 			event = gsi_ring_virt(ring, 0);
1428650d1603SAlex Elder 		trans = gsi_trans_pool_next(&trans_info->pool, trans);
1429650d1603SAlex Elder 	} while (event != event_done);
1430650d1603SAlex Elder 
1431650d1603SAlex Elder 	/* We record RX bytes when they are received */
1432650d1603SAlex Elder 	channel->byte_count += byte_count;
1433650d1603SAlex Elder 	channel->trans_count++;
1434650d1603SAlex Elder }
1435650d1603SAlex Elder 
1436650d1603SAlex Elder /* Initialize a ring, including allocating DMA memory for its entries */
1437650d1603SAlex Elder static int gsi_ring_alloc(struct gsi *gsi, struct gsi_ring *ring, u32 count)
1438650d1603SAlex Elder {
1439437c78f9SAlex Elder 	u32 size = count * GSI_RING_ELEMENT_SIZE;
1440650d1603SAlex Elder 	struct device *dev = gsi->dev;
1441650d1603SAlex Elder 	dma_addr_t addr;
1442650d1603SAlex Elder 
1443437c78f9SAlex Elder 	/* Hardware requires a 2^n ring size, with alignment equal to size.
1444437c78f9SAlex Elder 	 * The size is a power of 2, so we can check alignment using just
1445437c78f9SAlex Elder 	 * the bottom 32 bits for a DMA address of any size.
1446437c78f9SAlex Elder 	 */
1447650d1603SAlex Elder 	ring->virt = dma_alloc_coherent(dev, size, &addr, GFP_KERNEL);
1448437c78f9SAlex Elder 	if (ring->virt && lower_32_bits(addr) % size) {
14494ace7a6eSDan Carpenter 		dma_free_coherent(dev, size, ring->virt, addr);
1450437c78f9SAlex Elder 		dev_err(dev, "unable to alloc 0x%x-aligned ring buffer\n",
1451650d1603SAlex Elder 			size);
1452650d1603SAlex Elder 		return -EINVAL;	/* Not a good error value, but distinct */
1453650d1603SAlex Elder 	} else if (!ring->virt) {
1454650d1603SAlex Elder 		return -ENOMEM;
1455650d1603SAlex Elder 	}
1456650d1603SAlex Elder 	ring->addr = addr;
1457650d1603SAlex Elder 	ring->count = count;
1458650d1603SAlex Elder 
1459650d1603SAlex Elder 	return 0;
1460650d1603SAlex Elder }
1461650d1603SAlex Elder 
1462650d1603SAlex Elder /* Free a previously-allocated ring */
1463650d1603SAlex Elder static void gsi_ring_free(struct gsi *gsi, struct gsi_ring *ring)
1464650d1603SAlex Elder {
1465650d1603SAlex Elder 	size_t size = ring->count * GSI_RING_ELEMENT_SIZE;
1466650d1603SAlex Elder 
1467650d1603SAlex Elder 	dma_free_coherent(gsi->dev, size, ring->virt, ring->addr);
1468650d1603SAlex Elder }
1469650d1603SAlex Elder 
1470650d1603SAlex Elder /* Allocate an available event ring id */
1471650d1603SAlex Elder static int gsi_evt_ring_id_alloc(struct gsi *gsi)
1472650d1603SAlex Elder {
1473650d1603SAlex Elder 	u32 evt_ring_id;
1474650d1603SAlex Elder 
1475650d1603SAlex Elder 	if (gsi->event_bitmap == ~0U) {
1476650d1603SAlex Elder 		dev_err(gsi->dev, "event rings exhausted\n");
1477650d1603SAlex Elder 		return -ENOSPC;
1478650d1603SAlex Elder 	}
1479650d1603SAlex Elder 
1480650d1603SAlex Elder 	evt_ring_id = ffz(gsi->event_bitmap);
1481650d1603SAlex Elder 	gsi->event_bitmap |= BIT(evt_ring_id);
1482650d1603SAlex Elder 
1483650d1603SAlex Elder 	return (int)evt_ring_id;
1484650d1603SAlex Elder }
1485650d1603SAlex Elder 
1486650d1603SAlex Elder /* Free a previously-allocated event ring id */
1487650d1603SAlex Elder static void gsi_evt_ring_id_free(struct gsi *gsi, u32 evt_ring_id)
1488650d1603SAlex Elder {
1489650d1603SAlex Elder 	gsi->event_bitmap &= ~BIT(evt_ring_id);
1490650d1603SAlex Elder }
1491650d1603SAlex Elder 
1492650d1603SAlex Elder /* Ring a channel doorbell, reporting the first un-filled entry */
1493650d1603SAlex Elder void gsi_channel_doorbell(struct gsi_channel *channel)
1494650d1603SAlex Elder {
1495650d1603SAlex Elder 	struct gsi_ring *tre_ring = &channel->tre_ring;
1496650d1603SAlex Elder 	u32 channel_id = gsi_channel_id(channel);
1497650d1603SAlex Elder 	struct gsi *gsi = channel->gsi;
1498650d1603SAlex Elder 	u32 val;
1499650d1603SAlex Elder 
1500650d1603SAlex Elder 	/* Note: index *must* be used modulo the ring count here */
1501650d1603SAlex Elder 	val = gsi_ring_addr(tre_ring, tre_ring->index % tre_ring->count);
1502650d1603SAlex Elder 	iowrite32(val, gsi->virt + GSI_CH_C_DOORBELL_0_OFFSET(channel_id));
1503650d1603SAlex Elder }
1504650d1603SAlex Elder 
1505650d1603SAlex Elder /* Consult hardware, move any newly completed transactions to completed list */
1506223f5b34SAlex Elder static struct gsi_trans *gsi_channel_update(struct gsi_channel *channel)
1507650d1603SAlex Elder {
1508650d1603SAlex Elder 	u32 evt_ring_id = channel->evt_ring_id;
1509650d1603SAlex Elder 	struct gsi *gsi = channel->gsi;
1510650d1603SAlex Elder 	struct gsi_evt_ring *evt_ring;
1511650d1603SAlex Elder 	struct gsi_trans *trans;
1512650d1603SAlex Elder 	struct gsi_ring *ring;
1513650d1603SAlex Elder 	u32 offset;
1514650d1603SAlex Elder 	u32 index;
1515650d1603SAlex Elder 
1516650d1603SAlex Elder 	evt_ring = &gsi->evt_ring[evt_ring_id];
1517650d1603SAlex Elder 	ring = &evt_ring->ring;
1518650d1603SAlex Elder 
1519650d1603SAlex Elder 	/* See if there's anything new to process; if not, we're done.  Note
1520650d1603SAlex Elder 	 * that index always refers to an entry *within* the event ring.
1521650d1603SAlex Elder 	 */
1522650d1603SAlex Elder 	offset = GSI_EV_CH_E_CNTXT_4_OFFSET(evt_ring_id);
1523650d1603SAlex Elder 	index = gsi_ring_index(ring, ioread32(gsi->virt + offset));
1524650d1603SAlex Elder 	if (index == ring->index % ring->count)
1525223f5b34SAlex Elder 		return NULL;
1526650d1603SAlex Elder 
1527650d1603SAlex Elder 	/* Get the transaction for the latest completed event.  Take a
1528650d1603SAlex Elder 	 * reference to keep it from completing before we give the events
1529650d1603SAlex Elder 	 * for this and previous transactions back to the hardware.
1530650d1603SAlex Elder 	 */
1531650d1603SAlex Elder 	trans = gsi_event_trans(channel, gsi_ring_virt(ring, index - 1));
1532650d1603SAlex Elder 	refcount_inc(&trans->refcount);
1533650d1603SAlex Elder 
1534650d1603SAlex Elder 	/* For RX channels, update each completed transaction with the number
1535650d1603SAlex Elder 	 * of bytes that were actually received.  For TX channels, report
1536650d1603SAlex Elder 	 * the number of transactions and bytes this completion represents
1537650d1603SAlex Elder 	 * up the network stack.
1538650d1603SAlex Elder 	 */
1539650d1603SAlex Elder 	if (channel->toward_ipa)
1540650d1603SAlex Elder 		gsi_channel_tx_update(channel, trans);
1541650d1603SAlex Elder 	else
1542650d1603SAlex Elder 		gsi_evt_ring_rx_update(evt_ring, index);
1543650d1603SAlex Elder 
1544650d1603SAlex Elder 	gsi_trans_move_complete(trans);
1545650d1603SAlex Elder 
1546650d1603SAlex Elder 	/* Tell the hardware we've handled these events */
1547650d1603SAlex Elder 	gsi_evt_ring_doorbell(channel->gsi, channel->evt_ring_id, index);
1548650d1603SAlex Elder 
1549650d1603SAlex Elder 	gsi_trans_free(trans);
1550223f5b34SAlex Elder 
1551223f5b34SAlex Elder 	return gsi_channel_trans_complete(channel);
1552650d1603SAlex Elder }
1553650d1603SAlex Elder 
1554650d1603SAlex Elder /**
1555650d1603SAlex Elder  * gsi_channel_poll_one() - Return a single completed transaction on a channel
1556650d1603SAlex Elder  * @channel:	Channel to be polled
1557650d1603SAlex Elder  *
1558e3eea08eSAlex Elder  * Return:	Transaction pointer, or null if none are available
1559650d1603SAlex Elder  *
1560650d1603SAlex Elder  * This function returns the first entry on a channel's completed transaction
1561650d1603SAlex Elder  * list.  If that list is empty, the hardware is consulted to determine
1562650d1603SAlex Elder  * whether any new transactions have completed.  If so, they're moved to the
1563650d1603SAlex Elder  * completed list and the new first entry is returned.  If there are no more
1564650d1603SAlex Elder  * completed transactions, a null pointer is returned.
1565650d1603SAlex Elder  */
1566650d1603SAlex Elder static struct gsi_trans *gsi_channel_poll_one(struct gsi_channel *channel)
1567650d1603SAlex Elder {
1568650d1603SAlex Elder 	struct gsi_trans *trans;
1569650d1603SAlex Elder 
1570650d1603SAlex Elder 	/* Get the first transaction from the completed list */
1571650d1603SAlex Elder 	trans = gsi_channel_trans_complete(channel);
1572223f5b34SAlex Elder 	if (!trans)	/* List is empty; see if there's more to do */
1573223f5b34SAlex Elder 		trans = gsi_channel_update(channel);
1574650d1603SAlex Elder 
1575650d1603SAlex Elder 	if (trans)
1576650d1603SAlex Elder 		gsi_trans_move_polled(trans);
1577650d1603SAlex Elder 
1578650d1603SAlex Elder 	return trans;
1579650d1603SAlex Elder }
1580650d1603SAlex Elder 
1581650d1603SAlex Elder /**
1582650d1603SAlex Elder  * gsi_channel_poll() - NAPI poll function for a channel
1583650d1603SAlex Elder  * @napi:	NAPI structure for the channel
1584650d1603SAlex Elder  * @budget:	Budget supplied by NAPI core
1585e3eea08eSAlex Elder  *
1586e3eea08eSAlex Elder  * Return:	Number of items polled (<= budget)
1587650d1603SAlex Elder  *
1588650d1603SAlex Elder  * Single transactions completed by hardware are polled until either
1589650d1603SAlex Elder  * the budget is exhausted, or there are no more.  Each transaction
1590650d1603SAlex Elder  * polled is passed to gsi_trans_complete(), to perform remaining
1591650d1603SAlex Elder  * completion processing and retire/free the transaction.
1592650d1603SAlex Elder  */
1593650d1603SAlex Elder static int gsi_channel_poll(struct napi_struct *napi, int budget)
1594650d1603SAlex Elder {
1595650d1603SAlex Elder 	struct gsi_channel *channel;
1596c80c4a1eSAlex Elder 	int count;
1597650d1603SAlex Elder 
1598650d1603SAlex Elder 	channel = container_of(napi, struct gsi_channel, napi);
1599c80c4a1eSAlex Elder 	for (count = 0; count < budget; count++) {
1600650d1603SAlex Elder 		struct gsi_trans *trans;
1601650d1603SAlex Elder 
1602650d1603SAlex Elder 		trans = gsi_channel_poll_one(channel);
1603650d1603SAlex Elder 		if (!trans)
1604650d1603SAlex Elder 			break;
1605650d1603SAlex Elder 		gsi_trans_complete(trans);
1606650d1603SAlex Elder 	}
1607650d1603SAlex Elder 
1608148604e7SAlex Elder 	if (count < budget && napi_complete(napi))
16095725593eSAlex Elder 		gsi_irq_ieob_enable_one(channel->gsi, channel->evt_ring_id);
1610650d1603SAlex Elder 
1611650d1603SAlex Elder 	return count;
1612650d1603SAlex Elder }
1613650d1603SAlex Elder 
1614650d1603SAlex Elder /* The event bitmap represents which event ids are available for allocation.
1615650d1603SAlex Elder  * Set bits are not available, clear bits can be used.  This function
1616650d1603SAlex Elder  * initializes the map so all events supported by the hardware are available,
1617650d1603SAlex Elder  * then precludes any reserved events from being allocated.
1618650d1603SAlex Elder  */
1619650d1603SAlex Elder static u32 gsi_event_bitmap_init(u32 evt_ring_max)
1620650d1603SAlex Elder {
1621650d1603SAlex Elder 	u32 event_bitmap = GENMASK(BITS_PER_LONG - 1, evt_ring_max);
1622650d1603SAlex Elder 
1623650d1603SAlex Elder 	event_bitmap |= GENMASK(GSI_MHI_EVENT_ID_END, GSI_MHI_EVENT_ID_START);
1624650d1603SAlex Elder 
1625650d1603SAlex Elder 	return event_bitmap;
1626650d1603SAlex Elder }
1627650d1603SAlex Elder 
1628650d1603SAlex Elder /* Setup function for event rings */
1629650d1603SAlex Elder static void gsi_evt_ring_setup(struct gsi *gsi)
1630650d1603SAlex Elder {
1631650d1603SAlex Elder 	/* Nothing to do */
1632650d1603SAlex Elder }
1633650d1603SAlex Elder 
1634650d1603SAlex Elder /* Inverse of gsi_evt_ring_setup() */
1635650d1603SAlex Elder static void gsi_evt_ring_teardown(struct gsi *gsi)
1636650d1603SAlex Elder {
1637650d1603SAlex Elder 	/* Nothing to do */
1638650d1603SAlex Elder }
1639650d1603SAlex Elder 
1640650d1603SAlex Elder /* Setup function for a single channel */
1641d387c761SAlex Elder static int gsi_channel_setup_one(struct gsi *gsi, u32 channel_id)
1642650d1603SAlex Elder {
1643650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
1644650d1603SAlex Elder 	u32 evt_ring_id = channel->evt_ring_id;
1645650d1603SAlex Elder 	int ret;
1646650d1603SAlex Elder 
16476170b6daSAlex Elder 	if (!gsi_channel_initialized(channel))
16486170b6daSAlex Elder 		return 0;
1649650d1603SAlex Elder 
1650650d1603SAlex Elder 	ret = gsi_evt_ring_alloc_command(gsi, evt_ring_id);
1651650d1603SAlex Elder 	if (ret)
1652650d1603SAlex Elder 		return ret;
1653650d1603SAlex Elder 
1654650d1603SAlex Elder 	gsi_evt_ring_program(gsi, evt_ring_id);
1655650d1603SAlex Elder 
1656650d1603SAlex Elder 	ret = gsi_channel_alloc_command(gsi, channel_id);
1657650d1603SAlex Elder 	if (ret)
1658650d1603SAlex Elder 		goto err_evt_ring_de_alloc;
1659650d1603SAlex Elder 
1660d387c761SAlex Elder 	gsi_channel_program(channel, true);
1661650d1603SAlex Elder 
1662650d1603SAlex Elder 	if (channel->toward_ipa)
1663650d1603SAlex Elder 		netif_tx_napi_add(&gsi->dummy_dev, &channel->napi,
1664650d1603SAlex Elder 				  gsi_channel_poll, NAPI_POLL_WEIGHT);
1665650d1603SAlex Elder 	else
1666650d1603SAlex Elder 		netif_napi_add(&gsi->dummy_dev, &channel->napi,
1667650d1603SAlex Elder 			       gsi_channel_poll, NAPI_POLL_WEIGHT);
1668650d1603SAlex Elder 
1669650d1603SAlex Elder 	return 0;
1670650d1603SAlex Elder 
1671650d1603SAlex Elder err_evt_ring_de_alloc:
1672650d1603SAlex Elder 	/* We've done nothing with the event ring yet so don't reset */
1673650d1603SAlex Elder 	gsi_evt_ring_de_alloc_command(gsi, evt_ring_id);
1674650d1603SAlex Elder 
1675650d1603SAlex Elder 	return ret;
1676650d1603SAlex Elder }
1677650d1603SAlex Elder 
1678650d1603SAlex Elder /* Inverse of gsi_channel_setup_one() */
1679650d1603SAlex Elder static void gsi_channel_teardown_one(struct gsi *gsi, u32 channel_id)
1680650d1603SAlex Elder {
1681650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
1682650d1603SAlex Elder 	u32 evt_ring_id = channel->evt_ring_id;
1683650d1603SAlex Elder 
16846170b6daSAlex Elder 	if (!gsi_channel_initialized(channel))
16856170b6daSAlex Elder 		return;
1686650d1603SAlex Elder 
1687650d1603SAlex Elder 	netif_napi_del(&channel->napi);
1688650d1603SAlex Elder 
1689650d1603SAlex Elder 	gsi_channel_deprogram(channel);
1690650d1603SAlex Elder 	gsi_channel_de_alloc_command(gsi, channel_id);
1691650d1603SAlex Elder 	gsi_evt_ring_reset_command(gsi, evt_ring_id);
1692650d1603SAlex Elder 	gsi_evt_ring_de_alloc_command(gsi, evt_ring_id);
1693650d1603SAlex Elder }
1694650d1603SAlex Elder 
1695650d1603SAlex Elder static int gsi_generic_command(struct gsi *gsi, u32 channel_id,
1696650d1603SAlex Elder 			       enum gsi_generic_cmd_opcode opcode)
1697650d1603SAlex Elder {
1698650d1603SAlex Elder 	struct completion *completion = &gsi->completion;
1699d9cbe818SAlex Elder 	bool timeout;
1700650d1603SAlex Elder 	u32 val;
1701650d1603SAlex Elder 
1702d6c9e3f5SAlex Elder 	/* The error global interrupt type is always enabled (until we
1703d6c9e3f5SAlex Elder 	 * teardown), so we won't change that.  A generic EE command
1704d6c9e3f5SAlex Elder 	 * completes with a GSI global interrupt of type GP_INT1.  We
1705d6c9e3f5SAlex Elder 	 * only perform one generic command at a time (to allocate or
1706d6c9e3f5SAlex Elder 	 * halt a modem channel) and only from this function.  So we
1707d6c9e3f5SAlex Elder 	 * enable the GP_INT1 IRQ type here while we're expecting it.
1708d6c9e3f5SAlex Elder 	 */
17096c6358ccSAlex Elder 	val = BIT(ERROR_INT) | BIT(GP_INT1);
1710d6c9e3f5SAlex Elder 	iowrite32(val, gsi->virt + GSI_CNTXT_GLOB_IRQ_EN_OFFSET);
1711d6c9e3f5SAlex Elder 
17120b1ba18aSAlex Elder 	/* First zero the result code field */
17130b1ba18aSAlex Elder 	val = ioread32(gsi->virt + GSI_CNTXT_SCRATCH_0_OFFSET);
17140b1ba18aSAlex Elder 	val &= ~GENERIC_EE_RESULT_FMASK;
17150b1ba18aSAlex Elder 	iowrite32(val, gsi->virt + GSI_CNTXT_SCRATCH_0_OFFSET);
17160b1ba18aSAlex Elder 
17170b1ba18aSAlex Elder 	/* Now issue the command */
1718650d1603SAlex Elder 	val = u32_encode_bits(opcode, GENERIC_OPCODE_FMASK);
1719650d1603SAlex Elder 	val |= u32_encode_bits(channel_id, GENERIC_CHID_FMASK);
1720650d1603SAlex Elder 	val |= u32_encode_bits(GSI_EE_MODEM, GENERIC_EE_FMASK);
1721650d1603SAlex Elder 
1722d9cbe818SAlex Elder 	timeout = !gsi_command(gsi, GSI_GENERIC_CMD_OFFSET, val, completion);
1723d6c9e3f5SAlex Elder 
1724d6c9e3f5SAlex Elder 	/* Disable the GP_INT1 IRQ type again */
17256c6358ccSAlex Elder 	iowrite32(BIT(ERROR_INT), gsi->virt + GSI_CNTXT_GLOB_IRQ_EN_OFFSET);
1726d6c9e3f5SAlex Elder 
1727d9cbe818SAlex Elder 	if (!timeout)
172811361456SAlex Elder 		return gsi->result;
1729650d1603SAlex Elder 
1730650d1603SAlex Elder 	dev_err(gsi->dev, "GSI generic command %u to channel %u timed out\n",
1731650d1603SAlex Elder 		opcode, channel_id);
1732650d1603SAlex Elder 
1733650d1603SAlex Elder 	return -ETIMEDOUT;
1734650d1603SAlex Elder }
1735650d1603SAlex Elder 
1736650d1603SAlex Elder static int gsi_modem_channel_alloc(struct gsi *gsi, u32 channel_id)
1737650d1603SAlex Elder {
1738650d1603SAlex Elder 	return gsi_generic_command(gsi, channel_id,
1739650d1603SAlex Elder 				   GSI_GENERIC_ALLOCATE_CHANNEL);
1740650d1603SAlex Elder }
1741650d1603SAlex Elder 
1742650d1603SAlex Elder static void gsi_modem_channel_halt(struct gsi *gsi, u32 channel_id)
1743650d1603SAlex Elder {
174411361456SAlex Elder 	u32 retries = GSI_CHANNEL_MODEM_HALT_RETRIES;
174511361456SAlex Elder 	int ret;
174611361456SAlex Elder 
174711361456SAlex Elder 	do
174811361456SAlex Elder 		ret = gsi_generic_command(gsi, channel_id,
174911361456SAlex Elder 					  GSI_GENERIC_HALT_CHANNEL);
175011361456SAlex Elder 	while (ret == -EAGAIN && retries--);
175111361456SAlex Elder 
175211361456SAlex Elder 	if (ret)
175311361456SAlex Elder 		dev_err(gsi->dev, "error %d halting modem channel %u\n",
175411361456SAlex Elder 			ret, channel_id);
1755650d1603SAlex Elder }
1756650d1603SAlex Elder 
1757650d1603SAlex Elder /* Setup function for channels */
1758d387c761SAlex Elder static int gsi_channel_setup(struct gsi *gsi)
1759650d1603SAlex Elder {
1760650d1603SAlex Elder 	u32 channel_id = 0;
1761650d1603SAlex Elder 	u32 mask;
1762650d1603SAlex Elder 	int ret;
1763650d1603SAlex Elder 
1764650d1603SAlex Elder 	gsi_evt_ring_setup(gsi);
1765650d1603SAlex Elder 	gsi_irq_enable(gsi);
1766650d1603SAlex Elder 
1767650d1603SAlex Elder 	mutex_lock(&gsi->mutex);
1768650d1603SAlex Elder 
1769650d1603SAlex Elder 	do {
1770d387c761SAlex Elder 		ret = gsi_channel_setup_one(gsi, channel_id);
1771650d1603SAlex Elder 		if (ret)
1772650d1603SAlex Elder 			goto err_unwind;
1773650d1603SAlex Elder 	} while (++channel_id < gsi->channel_count);
1774650d1603SAlex Elder 
1775650d1603SAlex Elder 	/* Make sure no channels were defined that hardware does not support */
1776650d1603SAlex Elder 	while (channel_id < GSI_CHANNEL_COUNT_MAX) {
1777650d1603SAlex Elder 		struct gsi_channel *channel = &gsi->channel[channel_id++];
1778650d1603SAlex Elder 
17796170b6daSAlex Elder 		if (!gsi_channel_initialized(channel))
17806170b6daSAlex Elder 			continue;
1781650d1603SAlex Elder 
17821d23a56bSAlex Elder 		ret = -EINVAL;
1783650d1603SAlex Elder 		dev_err(gsi->dev, "channel %u not supported by hardware\n",
1784650d1603SAlex Elder 			channel_id - 1);
1785650d1603SAlex Elder 		channel_id = gsi->channel_count;
1786650d1603SAlex Elder 		goto err_unwind;
1787650d1603SAlex Elder 	}
1788650d1603SAlex Elder 
1789650d1603SAlex Elder 	/* Allocate modem channels if necessary */
1790650d1603SAlex Elder 	mask = gsi->modem_channel_bitmap;
1791650d1603SAlex Elder 	while (mask) {
1792650d1603SAlex Elder 		u32 modem_channel_id = __ffs(mask);
1793650d1603SAlex Elder 
1794650d1603SAlex Elder 		ret = gsi_modem_channel_alloc(gsi, modem_channel_id);
1795650d1603SAlex Elder 		if (ret)
1796650d1603SAlex Elder 			goto err_unwind_modem;
1797650d1603SAlex Elder 
1798650d1603SAlex Elder 		/* Clear bit from mask only after success (for unwind) */
1799650d1603SAlex Elder 		mask ^= BIT(modem_channel_id);
1800650d1603SAlex Elder 	}
1801650d1603SAlex Elder 
1802650d1603SAlex Elder 	mutex_unlock(&gsi->mutex);
1803650d1603SAlex Elder 
1804650d1603SAlex Elder 	return 0;
1805650d1603SAlex Elder 
1806650d1603SAlex Elder err_unwind_modem:
1807650d1603SAlex Elder 	/* Compute which modem channels need to be deallocated */
1808650d1603SAlex Elder 	mask ^= gsi->modem_channel_bitmap;
1809650d1603SAlex Elder 	while (mask) {
1810993cac15SAlex Elder 		channel_id = __fls(mask);
1811650d1603SAlex Elder 
1812650d1603SAlex Elder 		mask ^= BIT(channel_id);
1813650d1603SAlex Elder 
1814650d1603SAlex Elder 		gsi_modem_channel_halt(gsi, channel_id);
1815650d1603SAlex Elder 	}
1816650d1603SAlex Elder 
1817650d1603SAlex Elder err_unwind:
1818650d1603SAlex Elder 	while (channel_id--)
1819650d1603SAlex Elder 		gsi_channel_teardown_one(gsi, channel_id);
1820650d1603SAlex Elder 
1821650d1603SAlex Elder 	mutex_unlock(&gsi->mutex);
1822650d1603SAlex Elder 
1823650d1603SAlex Elder 	gsi_irq_disable(gsi);
1824650d1603SAlex Elder 	gsi_evt_ring_teardown(gsi);
1825650d1603SAlex Elder 
1826650d1603SAlex Elder 	return ret;
1827650d1603SAlex Elder }
1828650d1603SAlex Elder 
1829650d1603SAlex Elder /* Inverse of gsi_channel_setup() */
1830650d1603SAlex Elder static void gsi_channel_teardown(struct gsi *gsi)
1831650d1603SAlex Elder {
1832650d1603SAlex Elder 	u32 mask = gsi->modem_channel_bitmap;
1833650d1603SAlex Elder 	u32 channel_id;
1834650d1603SAlex Elder 
1835650d1603SAlex Elder 	mutex_lock(&gsi->mutex);
1836650d1603SAlex Elder 
1837650d1603SAlex Elder 	while (mask) {
1838993cac15SAlex Elder 		channel_id = __fls(mask);
1839650d1603SAlex Elder 
1840650d1603SAlex Elder 		mask ^= BIT(channel_id);
1841650d1603SAlex Elder 
1842650d1603SAlex Elder 		gsi_modem_channel_halt(gsi, channel_id);
1843650d1603SAlex Elder 	}
1844650d1603SAlex Elder 
1845650d1603SAlex Elder 	channel_id = gsi->channel_count - 1;
1846650d1603SAlex Elder 	do
1847650d1603SAlex Elder 		gsi_channel_teardown_one(gsi, channel_id);
1848650d1603SAlex Elder 	while (channel_id--);
1849650d1603SAlex Elder 
1850650d1603SAlex Elder 	mutex_unlock(&gsi->mutex);
1851650d1603SAlex Elder 
1852650d1603SAlex Elder 	gsi_irq_disable(gsi);
1853650d1603SAlex Elder 	gsi_evt_ring_teardown(gsi);
1854650d1603SAlex Elder }
1855650d1603SAlex Elder 
1856650d1603SAlex Elder /* Setup function for GSI.  GSI firmware must be loaded and initialized */
1857d387c761SAlex Elder int gsi_setup(struct gsi *gsi)
1858650d1603SAlex Elder {
18598463488aSAlex Elder 	struct device *dev = gsi->dev;
1860650d1603SAlex Elder 	u32 val;
186197eb94c8SAlex Elder 	int ret;
1862650d1603SAlex Elder 
1863650d1603SAlex Elder 	/* Here is where we first touch the GSI hardware */
1864650d1603SAlex Elder 	val = ioread32(gsi->virt + GSI_GSI_STATUS_OFFSET);
1865650d1603SAlex Elder 	if (!(val & ENABLED_FMASK)) {
18668463488aSAlex Elder 		dev_err(dev, "GSI has not been enabled\n");
1867650d1603SAlex Elder 		return -EIO;
1868650d1603SAlex Elder 	}
1869650d1603SAlex Elder 
187097eb94c8SAlex Elder 	gsi_irq_setup(gsi);
187197eb94c8SAlex Elder 
1872650d1603SAlex Elder 	val = ioread32(gsi->virt + GSI_GSI_HW_PARAM_2_OFFSET);
1873650d1603SAlex Elder 
1874650d1603SAlex Elder 	gsi->channel_count = u32_get_bits(val, NUM_CH_PER_EE_FMASK);
1875650d1603SAlex Elder 	if (!gsi->channel_count) {
18768463488aSAlex Elder 		dev_err(dev, "GSI reports zero channels supported\n");
1877650d1603SAlex Elder 		return -EINVAL;
1878650d1603SAlex Elder 	}
1879650d1603SAlex Elder 	if (gsi->channel_count > GSI_CHANNEL_COUNT_MAX) {
18808463488aSAlex Elder 		dev_warn(dev,
18818463488aSAlex Elder 			 "limiting to %u channels; hardware supports %u\n",
1882650d1603SAlex Elder 			 GSI_CHANNEL_COUNT_MAX, gsi->channel_count);
1883650d1603SAlex Elder 		gsi->channel_count = GSI_CHANNEL_COUNT_MAX;
1884650d1603SAlex Elder 	}
1885650d1603SAlex Elder 
1886650d1603SAlex Elder 	gsi->evt_ring_count = u32_get_bits(val, NUM_EV_PER_EE_FMASK);
1887650d1603SAlex Elder 	if (!gsi->evt_ring_count) {
18888463488aSAlex Elder 		dev_err(dev, "GSI reports zero event rings supported\n");
1889650d1603SAlex Elder 		return -EINVAL;
1890650d1603SAlex Elder 	}
1891650d1603SAlex Elder 	if (gsi->evt_ring_count > GSI_EVT_RING_COUNT_MAX) {
18928463488aSAlex Elder 		dev_warn(dev,
18938463488aSAlex Elder 			 "limiting to %u event rings; hardware supports %u\n",
1894650d1603SAlex Elder 			 GSI_EVT_RING_COUNT_MAX, gsi->evt_ring_count);
1895650d1603SAlex Elder 		gsi->evt_ring_count = GSI_EVT_RING_COUNT_MAX;
1896650d1603SAlex Elder 	}
1897650d1603SAlex Elder 
1898650d1603SAlex Elder 	/* Initialize the error log */
1899650d1603SAlex Elder 	iowrite32(0, gsi->virt + GSI_ERROR_LOG_OFFSET);
1900650d1603SAlex Elder 
1901650d1603SAlex Elder 	/* Writing 1 indicates IRQ interrupts; 0 would be MSI */
1902650d1603SAlex Elder 	iowrite32(1, gsi->virt + GSI_CNTXT_INTSET_OFFSET);
1903650d1603SAlex Elder 
190497eb94c8SAlex Elder 	ret = gsi_channel_setup(gsi);
190597eb94c8SAlex Elder 	if (ret)
190697eb94c8SAlex Elder 		gsi_irq_teardown(gsi);
190797eb94c8SAlex Elder 
190897eb94c8SAlex Elder 	return ret;
1909650d1603SAlex Elder }
1910650d1603SAlex Elder 
1911650d1603SAlex Elder /* Inverse of gsi_setup() */
1912650d1603SAlex Elder void gsi_teardown(struct gsi *gsi)
1913650d1603SAlex Elder {
1914650d1603SAlex Elder 	gsi_channel_teardown(gsi);
191597eb94c8SAlex Elder 	gsi_irq_teardown(gsi);
1916650d1603SAlex Elder }
1917650d1603SAlex Elder 
1918650d1603SAlex Elder /* Initialize a channel's event ring */
1919650d1603SAlex Elder static int gsi_channel_evt_ring_init(struct gsi_channel *channel)
1920650d1603SAlex Elder {
1921650d1603SAlex Elder 	struct gsi *gsi = channel->gsi;
1922650d1603SAlex Elder 	struct gsi_evt_ring *evt_ring;
1923650d1603SAlex Elder 	int ret;
1924650d1603SAlex Elder 
1925650d1603SAlex Elder 	ret = gsi_evt_ring_id_alloc(gsi);
1926650d1603SAlex Elder 	if (ret < 0)
1927650d1603SAlex Elder 		return ret;
1928650d1603SAlex Elder 	channel->evt_ring_id = ret;
1929650d1603SAlex Elder 
1930650d1603SAlex Elder 	evt_ring = &gsi->evt_ring[channel->evt_ring_id];
1931650d1603SAlex Elder 	evt_ring->channel = channel;
1932650d1603SAlex Elder 
1933650d1603SAlex Elder 	ret = gsi_ring_alloc(gsi, &evt_ring->ring, channel->event_count);
1934650d1603SAlex Elder 	if (!ret)
1935650d1603SAlex Elder 		return 0;	/* Success! */
1936650d1603SAlex Elder 
1937650d1603SAlex Elder 	dev_err(gsi->dev, "error %d allocating channel %u event ring\n",
1938650d1603SAlex Elder 		ret, gsi_channel_id(channel));
1939650d1603SAlex Elder 
1940650d1603SAlex Elder 	gsi_evt_ring_id_free(gsi, channel->evt_ring_id);
1941650d1603SAlex Elder 
1942650d1603SAlex Elder 	return ret;
1943650d1603SAlex Elder }
1944650d1603SAlex Elder 
1945650d1603SAlex Elder /* Inverse of gsi_channel_evt_ring_init() */
1946650d1603SAlex Elder static void gsi_channel_evt_ring_exit(struct gsi_channel *channel)
1947650d1603SAlex Elder {
1948650d1603SAlex Elder 	u32 evt_ring_id = channel->evt_ring_id;
1949650d1603SAlex Elder 	struct gsi *gsi = channel->gsi;
1950650d1603SAlex Elder 	struct gsi_evt_ring *evt_ring;
1951650d1603SAlex Elder 
1952650d1603SAlex Elder 	evt_ring = &gsi->evt_ring[evt_ring_id];
1953650d1603SAlex Elder 	gsi_ring_free(gsi, &evt_ring->ring);
1954650d1603SAlex Elder 	gsi_evt_ring_id_free(gsi, evt_ring_id);
1955650d1603SAlex Elder }
1956650d1603SAlex Elder 
1957650d1603SAlex Elder /* Init function for event rings */
1958650d1603SAlex Elder static void gsi_evt_ring_init(struct gsi *gsi)
1959650d1603SAlex Elder {
1960650d1603SAlex Elder 	u32 evt_ring_id = 0;
1961650d1603SAlex Elder 
1962650d1603SAlex Elder 	gsi->event_bitmap = gsi_event_bitmap_init(GSI_EVT_RING_COUNT_MAX);
1963a054539dSAlex Elder 	gsi->ieob_enabled_bitmap = 0;
1964650d1603SAlex Elder 	do
1965650d1603SAlex Elder 		init_completion(&gsi->evt_ring[evt_ring_id].completion);
1966650d1603SAlex Elder 	while (++evt_ring_id < GSI_EVT_RING_COUNT_MAX);
1967650d1603SAlex Elder }
1968650d1603SAlex Elder 
1969650d1603SAlex Elder /* Inverse of gsi_evt_ring_init() */
1970650d1603SAlex Elder static void gsi_evt_ring_exit(struct gsi *gsi)
1971650d1603SAlex Elder {
1972650d1603SAlex Elder 	/* Nothing to do */
1973650d1603SAlex Elder }
1974650d1603SAlex Elder 
1975650d1603SAlex Elder static bool gsi_channel_data_valid(struct gsi *gsi,
1976650d1603SAlex Elder 				   const struct ipa_gsi_endpoint_data *data)
1977650d1603SAlex Elder {
1978650d1603SAlex Elder #ifdef IPA_VALIDATION
1979650d1603SAlex Elder 	u32 channel_id = data->channel_id;
1980650d1603SAlex Elder 	struct device *dev = gsi->dev;
1981650d1603SAlex Elder 
1982650d1603SAlex Elder 	/* Make sure channel ids are in the range driver supports */
1983650d1603SAlex Elder 	if (channel_id >= GSI_CHANNEL_COUNT_MAX) {
19848463488aSAlex Elder 		dev_err(dev, "bad channel id %u; must be less than %u\n",
1985650d1603SAlex Elder 			channel_id, GSI_CHANNEL_COUNT_MAX);
1986650d1603SAlex Elder 		return false;
1987650d1603SAlex Elder 	}
1988650d1603SAlex Elder 
1989650d1603SAlex Elder 	if (data->ee_id != GSI_EE_AP && data->ee_id != GSI_EE_MODEM) {
19908463488aSAlex Elder 		dev_err(dev, "bad EE id %u; not AP or modem\n", data->ee_id);
1991650d1603SAlex Elder 		return false;
1992650d1603SAlex Elder 	}
1993650d1603SAlex Elder 
1994650d1603SAlex Elder 	if (!data->channel.tlv_count ||
1995650d1603SAlex Elder 	    data->channel.tlv_count > GSI_TLV_MAX) {
19968463488aSAlex Elder 		dev_err(dev, "channel %u bad tlv_count %u; must be 1..%u\n",
1997650d1603SAlex Elder 			channel_id, data->channel.tlv_count, GSI_TLV_MAX);
1998650d1603SAlex Elder 		return false;
1999650d1603SAlex Elder 	}
2000650d1603SAlex Elder 
2001650d1603SAlex Elder 	/* We have to allow at least one maximally-sized transaction to
2002650d1603SAlex Elder 	 * be outstanding (which would use tlv_count TREs).  Given how
2003650d1603SAlex Elder 	 * gsi_channel_tre_max() is computed, tre_count has to be almost
2004650d1603SAlex Elder 	 * twice the TLV FIFO size to satisfy this requirement.
2005650d1603SAlex Elder 	 */
2006650d1603SAlex Elder 	if (data->channel.tre_count < 2 * data->channel.tlv_count - 1) {
2007650d1603SAlex Elder 		dev_err(dev, "channel %u TLV count %u exceeds TRE count %u\n",
2008650d1603SAlex Elder 			channel_id, data->channel.tlv_count,
2009650d1603SAlex Elder 			data->channel.tre_count);
2010650d1603SAlex Elder 		return false;
2011650d1603SAlex Elder 	}
2012650d1603SAlex Elder 
2013650d1603SAlex Elder 	if (!is_power_of_2(data->channel.tre_count)) {
20148463488aSAlex Elder 		dev_err(dev, "channel %u bad tre_count %u; not power of 2\n",
2015650d1603SAlex Elder 			channel_id, data->channel.tre_count);
2016650d1603SAlex Elder 		return false;
2017650d1603SAlex Elder 	}
2018650d1603SAlex Elder 
2019650d1603SAlex Elder 	if (!is_power_of_2(data->channel.event_count)) {
20208463488aSAlex Elder 		dev_err(dev, "channel %u bad event_count %u; not power of 2\n",
2021650d1603SAlex Elder 			channel_id, data->channel.event_count);
2022650d1603SAlex Elder 		return false;
2023650d1603SAlex Elder 	}
2024650d1603SAlex Elder #endif /* IPA_VALIDATION */
2025650d1603SAlex Elder 
2026650d1603SAlex Elder 	return true;
2027650d1603SAlex Elder }
2028650d1603SAlex Elder 
2029650d1603SAlex Elder /* Init function for a single channel */
2030650d1603SAlex Elder static int gsi_channel_init_one(struct gsi *gsi,
2031650d1603SAlex Elder 				const struct ipa_gsi_endpoint_data *data,
203214dbf977SAlex Elder 				bool command)
2033650d1603SAlex Elder {
2034650d1603SAlex Elder 	struct gsi_channel *channel;
2035650d1603SAlex Elder 	u32 tre_count;
2036650d1603SAlex Elder 	int ret;
2037650d1603SAlex Elder 
2038650d1603SAlex Elder 	if (!gsi_channel_data_valid(gsi, data))
2039650d1603SAlex Elder 		return -EINVAL;
2040650d1603SAlex Elder 
2041650d1603SAlex Elder 	/* Worst case we need an event for every outstanding TRE */
2042650d1603SAlex Elder 	if (data->channel.tre_count > data->channel.event_count) {
2043650d1603SAlex Elder 		tre_count = data->channel.event_count;
20440721999fSAlex Elder 		dev_warn(gsi->dev, "channel %u limited to %u TREs\n",
20450721999fSAlex Elder 			 data->channel_id, tre_count);
2046650d1603SAlex Elder 	} else {
2047650d1603SAlex Elder 		tre_count = data->channel.tre_count;
2048650d1603SAlex Elder 	}
2049650d1603SAlex Elder 
2050650d1603SAlex Elder 	channel = &gsi->channel[data->channel_id];
2051650d1603SAlex Elder 	memset(channel, 0, sizeof(*channel));
2052650d1603SAlex Elder 
2053650d1603SAlex Elder 	channel->gsi = gsi;
2054650d1603SAlex Elder 	channel->toward_ipa = data->toward_ipa;
2055650d1603SAlex Elder 	channel->command = command;
2056650d1603SAlex Elder 	channel->tlv_count = data->channel.tlv_count;
2057650d1603SAlex Elder 	channel->tre_count = tre_count;
2058650d1603SAlex Elder 	channel->event_count = data->channel.event_count;
2059650d1603SAlex Elder 	init_completion(&channel->completion);
2060650d1603SAlex Elder 
2061650d1603SAlex Elder 	ret = gsi_channel_evt_ring_init(channel);
2062650d1603SAlex Elder 	if (ret)
2063650d1603SAlex Elder 		goto err_clear_gsi;
2064650d1603SAlex Elder 
2065650d1603SAlex Elder 	ret = gsi_ring_alloc(gsi, &channel->tre_ring, data->channel.tre_count);
2066650d1603SAlex Elder 	if (ret) {
2067650d1603SAlex Elder 		dev_err(gsi->dev, "error %d allocating channel %u ring\n",
2068650d1603SAlex Elder 			ret, data->channel_id);
2069650d1603SAlex Elder 		goto err_channel_evt_ring_exit;
2070650d1603SAlex Elder 	}
2071650d1603SAlex Elder 
2072650d1603SAlex Elder 	ret = gsi_channel_trans_init(gsi, data->channel_id);
2073650d1603SAlex Elder 	if (ret)
2074650d1603SAlex Elder 		goto err_ring_free;
2075650d1603SAlex Elder 
2076650d1603SAlex Elder 	if (command) {
2077650d1603SAlex Elder 		u32 tre_max = gsi_channel_tre_max(gsi, data->channel_id);
2078650d1603SAlex Elder 
2079650d1603SAlex Elder 		ret = ipa_cmd_pool_init(channel, tre_max);
2080650d1603SAlex Elder 	}
2081650d1603SAlex Elder 	if (!ret)
2082650d1603SAlex Elder 		return 0;	/* Success! */
2083650d1603SAlex Elder 
2084650d1603SAlex Elder 	gsi_channel_trans_exit(channel);
2085650d1603SAlex Elder err_ring_free:
2086650d1603SAlex Elder 	gsi_ring_free(gsi, &channel->tre_ring);
2087650d1603SAlex Elder err_channel_evt_ring_exit:
2088650d1603SAlex Elder 	gsi_channel_evt_ring_exit(channel);
2089650d1603SAlex Elder err_clear_gsi:
2090650d1603SAlex Elder 	channel->gsi = NULL;	/* Mark it not (fully) initialized */
2091650d1603SAlex Elder 
2092650d1603SAlex Elder 	return ret;
2093650d1603SAlex Elder }
2094650d1603SAlex Elder 
2095650d1603SAlex Elder /* Inverse of gsi_channel_init_one() */
2096650d1603SAlex Elder static void gsi_channel_exit_one(struct gsi_channel *channel)
2097650d1603SAlex Elder {
20986170b6daSAlex Elder 	if (!gsi_channel_initialized(channel))
20996170b6daSAlex Elder 		return;
2100650d1603SAlex Elder 
2101650d1603SAlex Elder 	if (channel->command)
2102650d1603SAlex Elder 		ipa_cmd_pool_exit(channel);
2103650d1603SAlex Elder 	gsi_channel_trans_exit(channel);
2104650d1603SAlex Elder 	gsi_ring_free(channel->gsi, &channel->tre_ring);
2105650d1603SAlex Elder 	gsi_channel_evt_ring_exit(channel);
2106650d1603SAlex Elder }
2107650d1603SAlex Elder 
2108650d1603SAlex Elder /* Init function for channels */
210914dbf977SAlex Elder static int gsi_channel_init(struct gsi *gsi, u32 count,
211056dfe8deSAlex Elder 			    const struct ipa_gsi_endpoint_data *data)
2111650d1603SAlex Elder {
211256dfe8deSAlex Elder 	bool modem_alloc;
2113650d1603SAlex Elder 	int ret = 0;
2114650d1603SAlex Elder 	u32 i;
2115650d1603SAlex Elder 
211656dfe8deSAlex Elder 	/* IPA v4.2 requires the AP to allocate channels for the modem */
211756dfe8deSAlex Elder 	modem_alloc = gsi->version == IPA_VERSION_4_2;
211856dfe8deSAlex Elder 
2119650d1603SAlex Elder 	gsi_evt_ring_init(gsi);
2120650d1603SAlex Elder 
2121650d1603SAlex Elder 	/* The endpoint data array is indexed by endpoint name */
2122650d1603SAlex Elder 	for (i = 0; i < count; i++) {
2123650d1603SAlex Elder 		bool command = i == IPA_ENDPOINT_AP_COMMAND_TX;
2124650d1603SAlex Elder 
2125650d1603SAlex Elder 		if (ipa_gsi_endpoint_data_empty(&data[i]))
2126650d1603SAlex Elder 			continue;	/* Skip over empty slots */
2127650d1603SAlex Elder 
2128650d1603SAlex Elder 		/* Mark modem channels to be allocated (hardware workaround) */
2129650d1603SAlex Elder 		if (data[i].ee_id == GSI_EE_MODEM) {
2130650d1603SAlex Elder 			if (modem_alloc)
2131650d1603SAlex Elder 				gsi->modem_channel_bitmap |=
2132650d1603SAlex Elder 						BIT(data[i].channel_id);
2133650d1603SAlex Elder 			continue;
2134650d1603SAlex Elder 		}
2135650d1603SAlex Elder 
213614dbf977SAlex Elder 		ret = gsi_channel_init_one(gsi, &data[i], command);
2137650d1603SAlex Elder 		if (ret)
2138650d1603SAlex Elder 			goto err_unwind;
2139650d1603SAlex Elder 	}
2140650d1603SAlex Elder 
2141650d1603SAlex Elder 	return ret;
2142650d1603SAlex Elder 
2143650d1603SAlex Elder err_unwind:
2144650d1603SAlex Elder 	while (i--) {
2145650d1603SAlex Elder 		if (ipa_gsi_endpoint_data_empty(&data[i]))
2146650d1603SAlex Elder 			continue;
2147650d1603SAlex Elder 		if (modem_alloc && data[i].ee_id == GSI_EE_MODEM) {
2148650d1603SAlex Elder 			gsi->modem_channel_bitmap &= ~BIT(data[i].channel_id);
2149650d1603SAlex Elder 			continue;
2150650d1603SAlex Elder 		}
2151650d1603SAlex Elder 		gsi_channel_exit_one(&gsi->channel[data->channel_id]);
2152650d1603SAlex Elder 	}
2153650d1603SAlex Elder 	gsi_evt_ring_exit(gsi);
2154650d1603SAlex Elder 
2155650d1603SAlex Elder 	return ret;
2156650d1603SAlex Elder }
2157650d1603SAlex Elder 
2158650d1603SAlex Elder /* Inverse of gsi_channel_init() */
2159650d1603SAlex Elder static void gsi_channel_exit(struct gsi *gsi)
2160650d1603SAlex Elder {
2161650d1603SAlex Elder 	u32 channel_id = GSI_CHANNEL_COUNT_MAX - 1;
2162650d1603SAlex Elder 
2163650d1603SAlex Elder 	do
2164650d1603SAlex Elder 		gsi_channel_exit_one(&gsi->channel[channel_id]);
2165650d1603SAlex Elder 	while (channel_id--);
2166650d1603SAlex Elder 	gsi->modem_channel_bitmap = 0;
2167650d1603SAlex Elder 
2168650d1603SAlex Elder 	gsi_evt_ring_exit(gsi);
2169650d1603SAlex Elder }
2170650d1603SAlex Elder 
2171650d1603SAlex Elder /* Init function for GSI.  GSI hardware does not need to be "ready" */
21721d0c09deSAlex Elder int gsi_init(struct gsi *gsi, struct platform_device *pdev,
21731d0c09deSAlex Elder 	     enum ipa_version version, u32 count,
21741d0c09deSAlex Elder 	     const struct ipa_gsi_endpoint_data *data)
2175650d1603SAlex Elder {
21768463488aSAlex Elder 	struct device *dev = &pdev->dev;
2177650d1603SAlex Elder 	struct resource *res;
2178650d1603SAlex Elder 	resource_size_t size;
2179cdeee49fSAlex Elder 	u32 adjust;
2180650d1603SAlex Elder 	int ret;
2181650d1603SAlex Elder 
2182650d1603SAlex Elder 	gsi_validate_build();
2183650d1603SAlex Elder 
21848463488aSAlex Elder 	gsi->dev = dev;
218514dbf977SAlex Elder 	gsi->version = version;
2186650d1603SAlex Elder 
2187571b1e7eSAlex Elder 	/* GSI uses NAPI on all channels.  Create a dummy network device
2188571b1e7eSAlex Elder 	 * for the channel NAPI contexts to be associated with.
2189650d1603SAlex Elder 	 */
2190650d1603SAlex Elder 	init_dummy_netdev(&gsi->dummy_dev);
2191650d1603SAlex Elder 
2192650d1603SAlex Elder 	/* Get GSI memory range and map it */
2193650d1603SAlex Elder 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "gsi");
2194650d1603SAlex Elder 	if (!res) {
21958463488aSAlex Elder 		dev_err(dev, "DT error getting \"gsi\" memory property\n");
21960b8d6761SAlex Elder 		return -ENODEV;
2197650d1603SAlex Elder 	}
2198650d1603SAlex Elder 
2199650d1603SAlex Elder 	size = resource_size(res);
2200650d1603SAlex Elder 	if (res->start > U32_MAX || size > U32_MAX - res->start) {
22018463488aSAlex Elder 		dev_err(dev, "DT memory resource \"gsi\" out of range\n");
22020b8d6761SAlex Elder 		return -EINVAL;
2203650d1603SAlex Elder 	}
2204650d1603SAlex Elder 
2205cdeee49fSAlex Elder 	/* Make sure we can make our pointer adjustment if necessary */
2206cdeee49fSAlex Elder 	adjust = gsi->version < IPA_VERSION_4_5 ? 0 : GSI_EE_REG_ADJUST;
2207cdeee49fSAlex Elder 	if (res->start < adjust) {
2208cdeee49fSAlex Elder 		dev_err(dev, "DT memory resource \"gsi\" too low (< %u)\n",
2209cdeee49fSAlex Elder 			adjust);
2210cdeee49fSAlex Elder 		return -EINVAL;
2211cdeee49fSAlex Elder 	}
2212cdeee49fSAlex Elder 
2213571b1e7eSAlex Elder 	gsi->virt_raw = ioremap(res->start, size);
2214571b1e7eSAlex Elder 	if (!gsi->virt_raw) {
22158463488aSAlex Elder 		dev_err(dev, "unable to remap \"gsi\" memory\n");
22160b8d6761SAlex Elder 		return -ENOMEM;
2217650d1603SAlex Elder 	}
2218571b1e7eSAlex Elder 	/* Most registers are accessed using an adjusted register range */
2219571b1e7eSAlex Elder 	gsi->virt = gsi->virt_raw - adjust;
2220650d1603SAlex Elder 
22210b8d6761SAlex Elder 	init_completion(&gsi->completion);
22220b8d6761SAlex Elder 
22230b8d6761SAlex Elder 	ret = gsi_irq_init(gsi, pdev);
2224650d1603SAlex Elder 	if (ret)
2225650d1603SAlex Elder 		goto err_iounmap;
2226650d1603SAlex Elder 
22270b8d6761SAlex Elder 	ret = gsi_channel_init(gsi, count, data);
22280b8d6761SAlex Elder 	if (ret)
22290b8d6761SAlex Elder 		goto err_irq_exit;
22300b8d6761SAlex Elder 
2231650d1603SAlex Elder 	mutex_init(&gsi->mutex);
2232650d1603SAlex Elder 
2233650d1603SAlex Elder 	return 0;
2234650d1603SAlex Elder 
22350b8d6761SAlex Elder err_irq_exit:
22360b8d6761SAlex Elder 	gsi_irq_exit(gsi);
2237650d1603SAlex Elder err_iounmap:
2238571b1e7eSAlex Elder 	iounmap(gsi->virt_raw);
2239650d1603SAlex Elder 
2240650d1603SAlex Elder 	return ret;
2241650d1603SAlex Elder }
2242650d1603SAlex Elder 
2243650d1603SAlex Elder /* Inverse of gsi_init() */
2244650d1603SAlex Elder void gsi_exit(struct gsi *gsi)
2245650d1603SAlex Elder {
2246650d1603SAlex Elder 	mutex_destroy(&gsi->mutex);
2247650d1603SAlex Elder 	gsi_channel_exit(gsi);
22480b8d6761SAlex Elder 	gsi_irq_exit(gsi);
2249571b1e7eSAlex Elder 	iounmap(gsi->virt_raw);
2250650d1603SAlex Elder }
2251650d1603SAlex Elder 
2252650d1603SAlex Elder /* The maximum number of outstanding TREs on a channel.  This limits
2253650d1603SAlex Elder  * a channel's maximum number of transactions outstanding (worst case
2254650d1603SAlex Elder  * is one TRE per transaction).
2255650d1603SAlex Elder  *
2256650d1603SAlex Elder  * The absolute limit is the number of TREs in the channel's TRE ring,
2257650d1603SAlex Elder  * and in theory we should be able use all of them.  But in practice,
2258650d1603SAlex Elder  * doing that led to the hardware reporting exhaustion of event ring
2259650d1603SAlex Elder  * slots for writing completion information.  So the hardware limit
2260650d1603SAlex Elder  * would be (tre_count - 1).
2261650d1603SAlex Elder  *
2262650d1603SAlex Elder  * We reduce it a bit further though.  Transaction resource pools are
2263650d1603SAlex Elder  * sized to be a little larger than this maximum, to allow resource
2264650d1603SAlex Elder  * allocations to always be contiguous.  The number of entries in a
2265650d1603SAlex Elder  * TRE ring buffer is a power of 2, and the extra resources in a pool
2266650d1603SAlex Elder  * tends to nearly double the memory allocated for it.  Reducing the
2267650d1603SAlex Elder  * maximum number of outstanding TREs allows the number of entries in
2268650d1603SAlex Elder  * a pool to avoid crossing that power-of-2 boundary, and this can
2269650d1603SAlex Elder  * substantially reduce pool memory requirements.  The number we
2270650d1603SAlex Elder  * reduce it by matches the number added in gsi_trans_pool_init().
2271650d1603SAlex Elder  */
2272650d1603SAlex Elder u32 gsi_channel_tre_max(struct gsi *gsi, u32 channel_id)
2273650d1603SAlex Elder {
2274650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
2275650d1603SAlex Elder 
2276650d1603SAlex Elder 	/* Hardware limit is channel->tre_count - 1 */
2277650d1603SAlex Elder 	return channel->tre_count - (channel->tlv_count - 1);
2278650d1603SAlex Elder }
2279650d1603SAlex Elder 
2280650d1603SAlex Elder /* Returns the maximum number of TREs in a single transaction for a channel */
2281650d1603SAlex Elder u32 gsi_channel_trans_tre_max(struct gsi *gsi, u32 channel_id)
2282650d1603SAlex Elder {
2283650d1603SAlex Elder 	struct gsi_channel *channel = &gsi->channel[channel_id];
2284650d1603SAlex Elder 
2285650d1603SAlex Elder 	return channel->tlv_count;
2286650d1603SAlex Elder }
2287