xref: /openbmc/linux/drivers/net/ipa/gsi.c (revision b4e18b29)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
4  * Copyright (C) 2018-2020 Linaro Ltd.
5  */
6 
7 #include <linux/types.h>
8 #include <linux/bits.h>
9 #include <linux/bitfield.h>
10 #include <linux/mutex.h>
11 #include <linux/completion.h>
12 #include <linux/io.h>
13 #include <linux/bug.h>
14 #include <linux/interrupt.h>
15 #include <linux/platform_device.h>
16 #include <linux/netdevice.h>
17 
18 #include "gsi.h"
19 #include "gsi_reg.h"
20 #include "gsi_private.h"
21 #include "gsi_trans.h"
22 #include "ipa_gsi.h"
23 #include "ipa_data.h"
24 #include "ipa_version.h"
25 
26 /**
27  * DOC: The IPA Generic Software Interface
28  *
29  * The generic software interface (GSI) is an integral component of the IPA,
30  * providing a well-defined communication layer between the AP subsystem
31  * and the IPA core.  The modem uses the GSI layer as well.
32  *
33  *	--------	     ---------
34  *	|      |	     |	     |
35  *	|  AP  +<---.	.----+ Modem |
36  *	|      +--. |	| .->+	     |
37  *	|      |  | |	| |  |	     |
38  *	--------  | |	| |  ---------
39  *		  v |	v |
40  *		--+-+---+-+--
41  *		|    GSI    |
42  *		|-----------|
43  *		|	    |
44  *		|    IPA    |
45  *		|	    |
46  *		-------------
47  *
48  * In the above diagram, the AP and Modem represent "execution environments"
49  * (EEs), which are independent operating environments that use the IPA for
50  * data transfer.
51  *
52  * Each EE uses a set of unidirectional GSI "channels," which allow transfer
53  * of data to or from the IPA.  A channel is implemented as a ring buffer,
54  * with a DRAM-resident array of "transfer elements" (TREs) available to
55  * describe transfers to or from other EEs through the IPA.  A transfer
56  * element can also contain an immediate command, requesting the IPA perform
57  * actions other than data transfer.
58  *
59  * Each TRE refers to a block of data--also located DRAM.  After writing one
60  * or more TREs to a channel, the writer (either the IPA or an EE) writes a
61  * doorbell register to inform the receiving side how many elements have
62  * been written.
63  *
64  * Each channel has a GSI "event ring" associated with it.  An event ring
65  * is implemented very much like a channel ring, but is always directed from
66  * the IPA to an EE.  The IPA notifies an EE (such as the AP) about channel
67  * events by adding an entry to the event ring associated with the channel.
68  * The GSI then writes its doorbell for the event ring, causing the target
69  * EE to be interrupted.  Each entry in an event ring contains a pointer
70  * to the channel TRE whose completion the event represents.
71  *
72  * Each TRE in a channel ring has a set of flags.  One flag indicates whether
73  * the completion of the transfer operation generates an entry (and possibly
74  * an interrupt) in the channel's event ring.  Other flags allow transfer
75  * elements to be chained together, forming a single logical transaction.
76  * TRE flags are used to control whether and when interrupts are generated
77  * to signal completion of channel transfers.
78  *
79  * Elements in channel and event rings are completed (or consumed) strictly
80  * in order.  Completion of one entry implies the completion of all preceding
81  * entries.  A single completion interrupt can therefore communicate the
82  * completion of many transfers.
83  *
84  * Note that all GSI registers are little-endian, which is the assumed
85  * endianness of I/O space accesses.  The accessor functions perform byte
86  * swapping if needed (i.e., for a big endian CPU).
87  */
88 
89 /* Delay period for interrupt moderation (in 32KHz IPA internal timer ticks) */
90 #define GSI_EVT_RING_INT_MODT		(32 * 1) /* 1ms under 32KHz clock */
91 
92 #define GSI_CMD_TIMEOUT			50	/* milliseconds */
93 
94 #define GSI_CHANNEL_STOP_RETRIES	10
95 #define GSI_CHANNEL_MODEM_HALT_RETRIES	10
96 
97 #define GSI_MHI_EVENT_ID_START		10	/* 1st reserved event id */
98 #define GSI_MHI_EVENT_ID_END		16	/* Last reserved event id */
99 
100 #define GSI_ISR_MAX_ITER		50	/* Detect interrupt storms */
101 
102 /* An entry in an event ring */
103 struct gsi_event {
104 	__le64 xfer_ptr;
105 	__le16 len;
106 	u8 reserved1;
107 	u8 code;
108 	__le16 reserved2;
109 	u8 type;
110 	u8 chid;
111 };
112 
113 /** gsi_channel_scratch_gpi - GPI protocol scratch register
114  * @max_outstanding_tre:
115  *	Defines the maximum number of TREs allowed in a single transaction
116  *	on a channel (in bytes).  This determines the amount of prefetch
117  *	performed by the hardware.  We configure this to equal the size of
118  *	the TLV FIFO for the channel.
119  * @outstanding_threshold:
120  *	Defines the threshold (in bytes) determining when the sequencer
121  *	should update the channel doorbell.  We configure this to equal
122  *	the size of two TREs.
123  */
124 struct gsi_channel_scratch_gpi {
125 	u64 reserved1;
126 	u16 reserved2;
127 	u16 max_outstanding_tre;
128 	u16 reserved3;
129 	u16 outstanding_threshold;
130 };
131 
132 /** gsi_channel_scratch - channel scratch configuration area
133  *
134  * The exact interpretation of this register is protocol-specific.
135  * We only use GPI channels; see struct gsi_channel_scratch_gpi, above.
136  */
137 union gsi_channel_scratch {
138 	struct gsi_channel_scratch_gpi gpi;
139 	struct {
140 		u32 word1;
141 		u32 word2;
142 		u32 word3;
143 		u32 word4;
144 	} data;
145 };
146 
147 /* Check things that can be validated at build time. */
148 static void gsi_validate_build(void)
149 {
150 	/* This is used as a divisor */
151 	BUILD_BUG_ON(!GSI_RING_ELEMENT_SIZE);
152 
153 	/* Code assumes the size of channel and event ring element are
154 	 * the same (and fixed).  Make sure the size of an event ring
155 	 * element is what's expected.
156 	 */
157 	BUILD_BUG_ON(sizeof(struct gsi_event) != GSI_RING_ELEMENT_SIZE);
158 
159 	/* Hardware requires a 2^n ring size.  We ensure the number of
160 	 * elements in an event ring is a power of 2 elsewhere; this
161 	 * ensure the elements themselves meet the requirement.
162 	 */
163 	BUILD_BUG_ON(!is_power_of_2(GSI_RING_ELEMENT_SIZE));
164 
165 	/* The channel element size must fit in this field */
166 	BUILD_BUG_ON(GSI_RING_ELEMENT_SIZE > field_max(ELEMENT_SIZE_FMASK));
167 
168 	/* The event ring element size must fit in this field */
169 	BUILD_BUG_ON(GSI_RING_ELEMENT_SIZE > field_max(EV_ELEMENT_SIZE_FMASK));
170 }
171 
172 /* Return the channel id associated with a given channel */
173 static u32 gsi_channel_id(struct gsi_channel *channel)
174 {
175 	return channel - &channel->gsi->channel[0];
176 }
177 
178 /* Update the GSI IRQ type register with the cached value */
179 static void gsi_irq_type_update(struct gsi *gsi, u32 val)
180 {
181 	gsi->type_enabled_bitmap = val;
182 	iowrite32(val, gsi->virt + GSI_CNTXT_TYPE_IRQ_MSK_OFFSET);
183 }
184 
185 static void gsi_irq_type_enable(struct gsi *gsi, enum gsi_irq_type_id type_id)
186 {
187 	gsi_irq_type_update(gsi, gsi->type_enabled_bitmap | BIT(type_id));
188 }
189 
190 static void gsi_irq_type_disable(struct gsi *gsi, enum gsi_irq_type_id type_id)
191 {
192 	gsi_irq_type_update(gsi, gsi->type_enabled_bitmap & ~BIT(type_id));
193 }
194 
195 /* Turn off all GSI interrupts initially */
196 static void gsi_irq_setup(struct gsi *gsi)
197 {
198 	u32 adjust;
199 
200 	/* Disable all interrupt types */
201 	gsi_irq_type_update(gsi, 0);
202 
203 	/* Clear all type-specific interrupt masks */
204 	iowrite32(0, gsi->virt + GSI_CNTXT_SRC_CH_IRQ_MSK_OFFSET);
205 	iowrite32(0, gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_MSK_OFFSET);
206 	iowrite32(0, gsi->virt + GSI_CNTXT_GLOB_IRQ_EN_OFFSET);
207 	iowrite32(0, gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_MSK_OFFSET);
208 
209 	/* Reverse the offset adjustment for inter-EE register offsets */
210 	adjust = gsi->version < IPA_VERSION_4_5 ? 0 : GSI_EE_REG_ADJUST;
211 	iowrite32(0, gsi->virt + adjust + GSI_INTER_EE_SRC_CH_IRQ_OFFSET);
212 	iowrite32(0, gsi->virt + adjust + GSI_INTER_EE_SRC_EV_CH_IRQ_OFFSET);
213 
214 	iowrite32(0, gsi->virt + GSI_CNTXT_GSI_IRQ_EN_OFFSET);
215 }
216 
217 /* Turn off all GSI interrupts when we're all done */
218 static void gsi_irq_teardown(struct gsi *gsi)
219 {
220 	/* Nothing to do */
221 }
222 
223 /* Event ring commands are performed one at a time.  Their completion
224  * is signaled by the event ring control GSI interrupt type, which is
225  * only enabled when we issue an event ring command.  Only the event
226  * ring being operated on has this interrupt enabled.
227  */
228 static void gsi_irq_ev_ctrl_enable(struct gsi *gsi, u32 evt_ring_id)
229 {
230 	u32 val = BIT(evt_ring_id);
231 
232 	/* There's a small chance that a previous command completed
233 	 * after the interrupt was disabled, so make sure we have no
234 	 * pending interrupts before we enable them.
235 	 */
236 	iowrite32(~0, gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_CLR_OFFSET);
237 
238 	iowrite32(val, gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_MSK_OFFSET);
239 	gsi_irq_type_enable(gsi, GSI_EV_CTRL);
240 }
241 
242 /* Disable event ring control interrupts */
243 static void gsi_irq_ev_ctrl_disable(struct gsi *gsi)
244 {
245 	gsi_irq_type_disable(gsi, GSI_EV_CTRL);
246 	iowrite32(0, gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_MSK_OFFSET);
247 }
248 
249 /* Channel commands are performed one at a time.  Their completion is
250  * signaled by the channel control GSI interrupt type, which is only
251  * enabled when we issue a channel command.  Only the channel being
252  * operated on has this interrupt enabled.
253  */
254 static void gsi_irq_ch_ctrl_enable(struct gsi *gsi, u32 channel_id)
255 {
256 	u32 val = BIT(channel_id);
257 
258 	/* There's a small chance that a previous command completed
259 	 * after the interrupt was disabled, so make sure we have no
260 	 * pending interrupts before we enable them.
261 	 */
262 	iowrite32(~0, gsi->virt + GSI_CNTXT_SRC_CH_IRQ_CLR_OFFSET);
263 
264 	iowrite32(val, gsi->virt + GSI_CNTXT_SRC_CH_IRQ_MSK_OFFSET);
265 	gsi_irq_type_enable(gsi, GSI_CH_CTRL);
266 }
267 
268 /* Disable channel control interrupts */
269 static void gsi_irq_ch_ctrl_disable(struct gsi *gsi)
270 {
271 	gsi_irq_type_disable(gsi, GSI_CH_CTRL);
272 	iowrite32(0, gsi->virt + GSI_CNTXT_SRC_CH_IRQ_MSK_OFFSET);
273 }
274 
275 static void gsi_irq_ieob_enable_one(struct gsi *gsi, u32 evt_ring_id)
276 {
277 	bool enable_ieob = !gsi->ieob_enabled_bitmap;
278 	u32 val;
279 
280 	gsi->ieob_enabled_bitmap |= BIT(evt_ring_id);
281 	val = gsi->ieob_enabled_bitmap;
282 	iowrite32(val, gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_MSK_OFFSET);
283 
284 	/* Enable the interrupt type if this is the first channel enabled */
285 	if (enable_ieob)
286 		gsi_irq_type_enable(gsi, GSI_IEOB);
287 }
288 
289 static void gsi_irq_ieob_disable(struct gsi *gsi, u32 event_mask)
290 {
291 	u32 val;
292 
293 	gsi->ieob_enabled_bitmap &= ~event_mask;
294 
295 	/* Disable the interrupt type if this was the last enabled channel */
296 	if (!gsi->ieob_enabled_bitmap)
297 		gsi_irq_type_disable(gsi, GSI_IEOB);
298 
299 	val = gsi->ieob_enabled_bitmap;
300 	iowrite32(val, gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_MSK_OFFSET);
301 }
302 
303 static void gsi_irq_ieob_disable_one(struct gsi *gsi, u32 evt_ring_id)
304 {
305 	gsi_irq_ieob_disable(gsi, BIT(evt_ring_id));
306 }
307 
308 /* Enable all GSI_interrupt types */
309 static void gsi_irq_enable(struct gsi *gsi)
310 {
311 	u32 val;
312 
313 	/* Global interrupts include hardware error reports.  Enable
314 	 * that so we can at least report the error should it occur.
315 	 */
316 	iowrite32(BIT(ERROR_INT), gsi->virt + GSI_CNTXT_GLOB_IRQ_EN_OFFSET);
317 	gsi_irq_type_update(gsi, gsi->type_enabled_bitmap | BIT(GSI_GLOB_EE));
318 
319 	/* General GSI interrupts are reported to all EEs; if they occur
320 	 * they are unrecoverable (without reset).  A breakpoint interrupt
321 	 * also exists, but we don't support that.  We want to be notified
322 	 * of errors so we can report them, even if they can't be handled.
323 	 */
324 	val = BIT(BUS_ERROR);
325 	val |= BIT(CMD_FIFO_OVRFLOW);
326 	val |= BIT(MCS_STACK_OVRFLOW);
327 	iowrite32(val, gsi->virt + GSI_CNTXT_GSI_IRQ_EN_OFFSET);
328 	gsi_irq_type_update(gsi, gsi->type_enabled_bitmap | BIT(GSI_GENERAL));
329 }
330 
331 /* Disable all GSI interrupt types */
332 static void gsi_irq_disable(struct gsi *gsi)
333 {
334 	gsi_irq_type_update(gsi, 0);
335 
336 	/* Clear the type-specific interrupt masks set by gsi_irq_enable() */
337 	iowrite32(0, gsi->virt + GSI_CNTXT_GSI_IRQ_EN_OFFSET);
338 	iowrite32(0, gsi->virt + GSI_CNTXT_GLOB_IRQ_EN_OFFSET);
339 }
340 
341 /* Return the virtual address associated with a ring index */
342 void *gsi_ring_virt(struct gsi_ring *ring, u32 index)
343 {
344 	/* Note: index *must* be used modulo the ring count here */
345 	return ring->virt + (index % ring->count) * GSI_RING_ELEMENT_SIZE;
346 }
347 
348 /* Return the 32-bit DMA address associated with a ring index */
349 static u32 gsi_ring_addr(struct gsi_ring *ring, u32 index)
350 {
351 	return (ring->addr & GENMASK(31, 0)) + index * GSI_RING_ELEMENT_SIZE;
352 }
353 
354 /* Return the ring index of a 32-bit ring offset */
355 static u32 gsi_ring_index(struct gsi_ring *ring, u32 offset)
356 {
357 	return (offset - gsi_ring_addr(ring, 0)) / GSI_RING_ELEMENT_SIZE;
358 }
359 
360 /* Issue a GSI command by writing a value to a register, then wait for
361  * completion to be signaled.  Returns true if the command completes
362  * or false if it times out.
363  */
364 static bool
365 gsi_command(struct gsi *gsi, u32 reg, u32 val, struct completion *completion)
366 {
367 	unsigned long timeout = msecs_to_jiffies(GSI_CMD_TIMEOUT);
368 
369 	reinit_completion(completion);
370 
371 	iowrite32(val, gsi->virt + reg);
372 
373 	return !!wait_for_completion_timeout(completion, timeout);
374 }
375 
376 /* Return the hardware's notion of the current state of an event ring */
377 static enum gsi_evt_ring_state
378 gsi_evt_ring_state(struct gsi *gsi, u32 evt_ring_id)
379 {
380 	u32 val;
381 
382 	val = ioread32(gsi->virt + GSI_EV_CH_E_CNTXT_0_OFFSET(evt_ring_id));
383 
384 	return u32_get_bits(val, EV_CHSTATE_FMASK);
385 }
386 
387 /* Issue an event ring command and wait for it to complete */
388 static void gsi_evt_ring_command(struct gsi *gsi, u32 evt_ring_id,
389 				 enum gsi_evt_cmd_opcode opcode)
390 {
391 	struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
392 	struct completion *completion = &evt_ring->completion;
393 	struct device *dev = gsi->dev;
394 	bool timeout;
395 	u32 val;
396 
397 	/* Enable the completion interrupt for the command */
398 	gsi_irq_ev_ctrl_enable(gsi, evt_ring_id);
399 
400 	val = u32_encode_bits(evt_ring_id, EV_CHID_FMASK);
401 	val |= u32_encode_bits(opcode, EV_OPCODE_FMASK);
402 
403 	timeout = !gsi_command(gsi, GSI_EV_CH_CMD_OFFSET, val, completion);
404 
405 	gsi_irq_ev_ctrl_disable(gsi);
406 
407 	if (!timeout)
408 		return;
409 
410 	dev_err(dev, "GSI command %u for event ring %u timed out, state %u\n",
411 		opcode, evt_ring_id, gsi_evt_ring_state(gsi, evt_ring_id));
412 }
413 
414 /* Allocate an event ring in NOT_ALLOCATED state */
415 static int gsi_evt_ring_alloc_command(struct gsi *gsi, u32 evt_ring_id)
416 {
417 	enum gsi_evt_ring_state state;
418 
419 	/* Get initial event ring state */
420 	state = gsi_evt_ring_state(gsi, evt_ring_id);
421 	if (state != GSI_EVT_RING_STATE_NOT_ALLOCATED) {
422 		dev_err(gsi->dev, "event ring %u bad state %u before alloc\n",
423 			evt_ring_id, state);
424 		return -EINVAL;
425 	}
426 
427 	gsi_evt_ring_command(gsi, evt_ring_id, GSI_EVT_ALLOCATE);
428 
429 	/* If successful the event ring state will have changed */
430 	state = gsi_evt_ring_state(gsi, evt_ring_id);
431 	if (state == GSI_EVT_RING_STATE_ALLOCATED)
432 		return 0;
433 
434 	dev_err(gsi->dev, "event ring %u bad state %u after alloc\n",
435 		evt_ring_id, state);
436 
437 	return -EIO;
438 }
439 
440 /* Reset a GSI event ring in ALLOCATED or ERROR state. */
441 static void gsi_evt_ring_reset_command(struct gsi *gsi, u32 evt_ring_id)
442 {
443 	enum gsi_evt_ring_state state;
444 
445 	state = gsi_evt_ring_state(gsi, evt_ring_id);
446 	if (state != GSI_EVT_RING_STATE_ALLOCATED &&
447 	    state != GSI_EVT_RING_STATE_ERROR) {
448 		dev_err(gsi->dev, "event ring %u bad state %u before reset\n",
449 			evt_ring_id, state);
450 		return;
451 	}
452 
453 	gsi_evt_ring_command(gsi, evt_ring_id, GSI_EVT_RESET);
454 
455 	/* If successful the event ring state will have changed */
456 	state = gsi_evt_ring_state(gsi, evt_ring_id);
457 	if (state == GSI_EVT_RING_STATE_ALLOCATED)
458 		return;
459 
460 	dev_err(gsi->dev, "event ring %u bad state %u after reset\n",
461 		evt_ring_id, state);
462 }
463 
464 /* Issue a hardware de-allocation request for an allocated event ring */
465 static void gsi_evt_ring_de_alloc_command(struct gsi *gsi, u32 evt_ring_id)
466 {
467 	enum gsi_evt_ring_state state;
468 
469 	state = gsi_evt_ring_state(gsi, evt_ring_id);
470 	if (state != GSI_EVT_RING_STATE_ALLOCATED) {
471 		dev_err(gsi->dev, "event ring %u state %u before dealloc\n",
472 			evt_ring_id, state);
473 		return;
474 	}
475 
476 	gsi_evt_ring_command(gsi, evt_ring_id, GSI_EVT_DE_ALLOC);
477 
478 	/* If successful the event ring state will have changed */
479 	state = gsi_evt_ring_state(gsi, evt_ring_id);
480 	if (state == GSI_EVT_RING_STATE_NOT_ALLOCATED)
481 		return;
482 
483 	dev_err(gsi->dev, "event ring %u bad state %u after dealloc\n",
484 		evt_ring_id, state);
485 }
486 
487 /* Fetch the current state of a channel from hardware */
488 static enum gsi_channel_state gsi_channel_state(struct gsi_channel *channel)
489 {
490 	u32 channel_id = gsi_channel_id(channel);
491 	void __iomem *virt = channel->gsi->virt;
492 	u32 val;
493 
494 	val = ioread32(virt + GSI_CH_C_CNTXT_0_OFFSET(channel_id));
495 
496 	return u32_get_bits(val, CHSTATE_FMASK);
497 }
498 
499 /* Issue a channel command and wait for it to complete */
500 static void
501 gsi_channel_command(struct gsi_channel *channel, enum gsi_ch_cmd_opcode opcode)
502 {
503 	struct completion *completion = &channel->completion;
504 	u32 channel_id = gsi_channel_id(channel);
505 	struct gsi *gsi = channel->gsi;
506 	struct device *dev = gsi->dev;
507 	bool timeout;
508 	u32 val;
509 
510 	/* Enable the completion interrupt for the command */
511 	gsi_irq_ch_ctrl_enable(gsi, channel_id);
512 
513 	val = u32_encode_bits(channel_id, CH_CHID_FMASK);
514 	val |= u32_encode_bits(opcode, CH_OPCODE_FMASK);
515 	timeout = !gsi_command(gsi, GSI_CH_CMD_OFFSET, val, completion);
516 
517 	gsi_irq_ch_ctrl_disable(gsi);
518 
519 	if (!timeout)
520 		return;
521 
522 	dev_err(dev, "GSI command %u for channel %u timed out, state %u\n",
523 		opcode, channel_id, gsi_channel_state(channel));
524 }
525 
526 /* Allocate GSI channel in NOT_ALLOCATED state */
527 static int gsi_channel_alloc_command(struct gsi *gsi, u32 channel_id)
528 {
529 	struct gsi_channel *channel = &gsi->channel[channel_id];
530 	struct device *dev = gsi->dev;
531 	enum gsi_channel_state state;
532 
533 	/* Get initial channel state */
534 	state = gsi_channel_state(channel);
535 	if (state != GSI_CHANNEL_STATE_NOT_ALLOCATED) {
536 		dev_err(dev, "channel %u bad state %u before alloc\n",
537 			channel_id, state);
538 		return -EINVAL;
539 	}
540 
541 	gsi_channel_command(channel, GSI_CH_ALLOCATE);
542 
543 	/* If successful the channel state will have changed */
544 	state = gsi_channel_state(channel);
545 	if (state == GSI_CHANNEL_STATE_ALLOCATED)
546 		return 0;
547 
548 	dev_err(dev, "channel %u bad state %u after alloc\n",
549 		channel_id, state);
550 
551 	return -EIO;
552 }
553 
554 /* Start an ALLOCATED channel */
555 static int gsi_channel_start_command(struct gsi_channel *channel)
556 {
557 	struct device *dev = channel->gsi->dev;
558 	enum gsi_channel_state state;
559 
560 	state = gsi_channel_state(channel);
561 	if (state != GSI_CHANNEL_STATE_ALLOCATED &&
562 	    state != GSI_CHANNEL_STATE_STOPPED) {
563 		dev_err(dev, "channel %u bad state %u before start\n",
564 			gsi_channel_id(channel), state);
565 		return -EINVAL;
566 	}
567 
568 	gsi_channel_command(channel, GSI_CH_START);
569 
570 	/* If successful the channel state will have changed */
571 	state = gsi_channel_state(channel);
572 	if (state == GSI_CHANNEL_STATE_STARTED)
573 		return 0;
574 
575 	dev_err(dev, "channel %u bad state %u after start\n",
576 		gsi_channel_id(channel), state);
577 
578 	return -EIO;
579 }
580 
581 /* Stop a GSI channel in STARTED state */
582 static int gsi_channel_stop_command(struct gsi_channel *channel)
583 {
584 	struct device *dev = channel->gsi->dev;
585 	enum gsi_channel_state state;
586 
587 	state = gsi_channel_state(channel);
588 
589 	/* Channel could have entered STOPPED state since last call
590 	 * if it timed out.  If so, we're done.
591 	 */
592 	if (state == GSI_CHANNEL_STATE_STOPPED)
593 		return 0;
594 
595 	if (state != GSI_CHANNEL_STATE_STARTED &&
596 	    state != GSI_CHANNEL_STATE_STOP_IN_PROC) {
597 		dev_err(dev, "channel %u bad state %u before stop\n",
598 			gsi_channel_id(channel), state);
599 		return -EINVAL;
600 	}
601 
602 	gsi_channel_command(channel, GSI_CH_STOP);
603 
604 	/* If successful the channel state will have changed */
605 	state = gsi_channel_state(channel);
606 	if (state == GSI_CHANNEL_STATE_STOPPED)
607 		return 0;
608 
609 	/* We may have to try again if stop is in progress */
610 	if (state == GSI_CHANNEL_STATE_STOP_IN_PROC)
611 		return -EAGAIN;
612 
613 	dev_err(dev, "channel %u bad state %u after stop\n",
614 		gsi_channel_id(channel), state);
615 
616 	return -EIO;
617 }
618 
619 /* Reset a GSI channel in ALLOCATED or ERROR state. */
620 static void gsi_channel_reset_command(struct gsi_channel *channel)
621 {
622 	struct device *dev = channel->gsi->dev;
623 	enum gsi_channel_state state;
624 
625 	/* A short delay is required before a RESET command */
626 	usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
627 
628 	state = gsi_channel_state(channel);
629 	if (state != GSI_CHANNEL_STATE_STOPPED &&
630 	    state != GSI_CHANNEL_STATE_ERROR) {
631 		/* No need to reset a channel already in ALLOCATED state */
632 		if (state != GSI_CHANNEL_STATE_ALLOCATED)
633 			dev_err(dev, "channel %u bad state %u before reset\n",
634 				gsi_channel_id(channel), state);
635 		return;
636 	}
637 
638 	gsi_channel_command(channel, GSI_CH_RESET);
639 
640 	/* If successful the channel state will have changed */
641 	state = gsi_channel_state(channel);
642 	if (state != GSI_CHANNEL_STATE_ALLOCATED)
643 		dev_err(dev, "channel %u bad state %u after reset\n",
644 			gsi_channel_id(channel), state);
645 }
646 
647 /* Deallocate an ALLOCATED GSI channel */
648 static void gsi_channel_de_alloc_command(struct gsi *gsi, u32 channel_id)
649 {
650 	struct gsi_channel *channel = &gsi->channel[channel_id];
651 	struct device *dev = gsi->dev;
652 	enum gsi_channel_state state;
653 
654 	state = gsi_channel_state(channel);
655 	if (state != GSI_CHANNEL_STATE_ALLOCATED) {
656 		dev_err(dev, "channel %u bad state %u before dealloc\n",
657 			channel_id, state);
658 		return;
659 	}
660 
661 	gsi_channel_command(channel, GSI_CH_DE_ALLOC);
662 
663 	/* If successful the channel state will have changed */
664 	state = gsi_channel_state(channel);
665 
666 	if (state != GSI_CHANNEL_STATE_NOT_ALLOCATED)
667 		dev_err(dev, "channel %u bad state %u after dealloc\n",
668 			channel_id, state);
669 }
670 
671 /* Ring an event ring doorbell, reporting the last entry processed by the AP.
672  * The index argument (modulo the ring count) is the first unfilled entry, so
673  * we supply one less than that with the doorbell.  Update the event ring
674  * index field with the value provided.
675  */
676 static void gsi_evt_ring_doorbell(struct gsi *gsi, u32 evt_ring_id, u32 index)
677 {
678 	struct gsi_ring *ring = &gsi->evt_ring[evt_ring_id].ring;
679 	u32 val;
680 
681 	ring->index = index;	/* Next unused entry */
682 
683 	/* Note: index *must* be used modulo the ring count here */
684 	val = gsi_ring_addr(ring, (index - 1) % ring->count);
685 	iowrite32(val, gsi->virt + GSI_EV_CH_E_DOORBELL_0_OFFSET(evt_ring_id));
686 }
687 
688 /* Program an event ring for use */
689 static void gsi_evt_ring_program(struct gsi *gsi, u32 evt_ring_id)
690 {
691 	struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
692 	size_t size = evt_ring->ring.count * GSI_RING_ELEMENT_SIZE;
693 	u32 val;
694 
695 	/* We program all event rings as GPI type/protocol */
696 	val = u32_encode_bits(GSI_CHANNEL_TYPE_GPI, EV_CHTYPE_FMASK);
697 	val |= EV_INTYPE_FMASK;
698 	val |= u32_encode_bits(GSI_RING_ELEMENT_SIZE, EV_ELEMENT_SIZE_FMASK);
699 	iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_0_OFFSET(evt_ring_id));
700 
701 	val = u32_encode_bits(size, EV_R_LENGTH_FMASK);
702 	iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_1_OFFSET(evt_ring_id));
703 
704 	/* The context 2 and 3 registers store the low-order and
705 	 * high-order 32 bits of the address of the event ring,
706 	 * respectively.
707 	 */
708 	val = evt_ring->ring.addr & GENMASK(31, 0);
709 	iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_2_OFFSET(evt_ring_id));
710 
711 	val = evt_ring->ring.addr >> 32;
712 	iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_3_OFFSET(evt_ring_id));
713 
714 	/* Enable interrupt moderation by setting the moderation delay */
715 	val = u32_encode_bits(GSI_EVT_RING_INT_MODT, MODT_FMASK);
716 	val |= u32_encode_bits(1, MODC_FMASK);	/* comes from channel */
717 	iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_8_OFFSET(evt_ring_id));
718 
719 	/* No MSI write data, and MSI address high and low address is 0 */
720 	iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_9_OFFSET(evt_ring_id));
721 	iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_10_OFFSET(evt_ring_id));
722 	iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_11_OFFSET(evt_ring_id));
723 
724 	/* We don't need to get event read pointer updates */
725 	iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_12_OFFSET(evt_ring_id));
726 	iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_13_OFFSET(evt_ring_id));
727 
728 	/* Finally, tell the hardware we've completed event 0 (arbitrary) */
729 	gsi_evt_ring_doorbell(gsi, evt_ring_id, 0);
730 }
731 
732 /* Find the transaction whose completion indicates a channel is quiesced */
733 static struct gsi_trans *gsi_channel_trans_last(struct gsi_channel *channel)
734 {
735 	struct gsi_trans_info *trans_info = &channel->trans_info;
736 	const struct list_head *list;
737 	struct gsi_trans *trans;
738 
739 	spin_lock_bh(&trans_info->spinlock);
740 
741 	/* There is a small chance a TX transaction got allocated just
742 	 * before we disabled transmits, so check for that.
743 	 */
744 	if (channel->toward_ipa) {
745 		list = &trans_info->alloc;
746 		if (!list_empty(list))
747 			goto done;
748 		list = &trans_info->pending;
749 		if (!list_empty(list))
750 			goto done;
751 	}
752 
753 	/* Otherwise (TX or RX) we want to wait for anything that
754 	 * has completed, or has been polled but not released yet.
755 	 */
756 	list = &trans_info->complete;
757 	if (!list_empty(list))
758 		goto done;
759 	list = &trans_info->polled;
760 	if (list_empty(list))
761 		list = NULL;
762 done:
763 	trans = list ? list_last_entry(list, struct gsi_trans, links) : NULL;
764 
765 	/* Caller will wait for this, so take a reference */
766 	if (trans)
767 		refcount_inc(&trans->refcount);
768 
769 	spin_unlock_bh(&trans_info->spinlock);
770 
771 	return trans;
772 }
773 
774 /* Wait for transaction activity on a channel to complete */
775 static void gsi_channel_trans_quiesce(struct gsi_channel *channel)
776 {
777 	struct gsi_trans *trans;
778 
779 	/* Get the last transaction, and wait for it to complete */
780 	trans = gsi_channel_trans_last(channel);
781 	if (trans) {
782 		wait_for_completion(&trans->completion);
783 		gsi_trans_free(trans);
784 	}
785 }
786 
787 /* Program a channel for use */
788 static void gsi_channel_program(struct gsi_channel *channel, bool doorbell)
789 {
790 	size_t size = channel->tre_ring.count * GSI_RING_ELEMENT_SIZE;
791 	u32 channel_id = gsi_channel_id(channel);
792 	union gsi_channel_scratch scr = { };
793 	struct gsi_channel_scratch_gpi *gpi;
794 	struct gsi *gsi = channel->gsi;
795 	u32 wrr_weight = 0;
796 	u32 val;
797 
798 	/* Arbitrarily pick TRE 0 as the first channel element to use */
799 	channel->tre_ring.index = 0;
800 
801 	/* We program all channels as GPI type/protocol */
802 	val = u32_encode_bits(GSI_CHANNEL_TYPE_GPI, CHTYPE_PROTOCOL_FMASK);
803 	if (channel->toward_ipa)
804 		val |= CHTYPE_DIR_FMASK;
805 	val |= u32_encode_bits(channel->evt_ring_id, ERINDEX_FMASK);
806 	val |= u32_encode_bits(GSI_RING_ELEMENT_SIZE, ELEMENT_SIZE_FMASK);
807 	iowrite32(val, gsi->virt + GSI_CH_C_CNTXT_0_OFFSET(channel_id));
808 
809 	val = u32_encode_bits(size, R_LENGTH_FMASK);
810 	iowrite32(val, gsi->virt + GSI_CH_C_CNTXT_1_OFFSET(channel_id));
811 
812 	/* The context 2 and 3 registers store the low-order and
813 	 * high-order 32 bits of the address of the channel ring,
814 	 * respectively.
815 	 */
816 	val = channel->tre_ring.addr & GENMASK(31, 0);
817 	iowrite32(val, gsi->virt + GSI_CH_C_CNTXT_2_OFFSET(channel_id));
818 
819 	val = channel->tre_ring.addr >> 32;
820 	iowrite32(val, gsi->virt + GSI_CH_C_CNTXT_3_OFFSET(channel_id));
821 
822 	/* Command channel gets low weighted round-robin priority */
823 	if (channel->command)
824 		wrr_weight = field_max(WRR_WEIGHT_FMASK);
825 	val = u32_encode_bits(wrr_weight, WRR_WEIGHT_FMASK);
826 
827 	/* Max prefetch is 1 segment (do not set MAX_PREFETCH_FMASK) */
828 
829 	/* We enable the doorbell engine for IPA v3.5.1 */
830 	if (gsi->version == IPA_VERSION_3_5_1 && doorbell)
831 		val |= USE_DB_ENG_FMASK;
832 
833 	/* v4.0 introduces an escape buffer for prefetch.  We use it
834 	 * on all but the AP command channel.
835 	 */
836 	if (gsi->version != IPA_VERSION_3_5_1 && !channel->command) {
837 		/* If not otherwise set, prefetch buffers are used */
838 		if (gsi->version < IPA_VERSION_4_5)
839 			val |= USE_ESCAPE_BUF_ONLY_FMASK;
840 		else
841 			val |= u32_encode_bits(GSI_ESCAPE_BUF_ONLY,
842 					       PREFETCH_MODE_FMASK);
843 	}
844 
845 	iowrite32(val, gsi->virt + GSI_CH_C_QOS_OFFSET(channel_id));
846 
847 	/* Now update the scratch registers for GPI protocol */
848 	gpi = &scr.gpi;
849 	gpi->max_outstanding_tre = gsi_channel_trans_tre_max(gsi, channel_id) *
850 					GSI_RING_ELEMENT_SIZE;
851 	gpi->outstanding_threshold = 2 * GSI_RING_ELEMENT_SIZE;
852 
853 	val = scr.data.word1;
854 	iowrite32(val, gsi->virt + GSI_CH_C_SCRATCH_0_OFFSET(channel_id));
855 
856 	val = scr.data.word2;
857 	iowrite32(val, gsi->virt + GSI_CH_C_SCRATCH_1_OFFSET(channel_id));
858 
859 	val = scr.data.word3;
860 	iowrite32(val, gsi->virt + GSI_CH_C_SCRATCH_2_OFFSET(channel_id));
861 
862 	/* We must preserve the upper 16 bits of the last scratch register.
863 	 * The next sequence assumes those bits remain unchanged between the
864 	 * read and the write.
865 	 */
866 	val = ioread32(gsi->virt + GSI_CH_C_SCRATCH_3_OFFSET(channel_id));
867 	val = (scr.data.word4 & GENMASK(31, 16)) | (val & GENMASK(15, 0));
868 	iowrite32(val, gsi->virt + GSI_CH_C_SCRATCH_3_OFFSET(channel_id));
869 
870 	/* All done! */
871 }
872 
873 static void gsi_channel_deprogram(struct gsi_channel *channel)
874 {
875 	/* Nothing to do */
876 }
877 
878 static int __gsi_channel_start(struct gsi_channel *channel, bool start)
879 {
880 	struct gsi *gsi = channel->gsi;
881 	int ret;
882 
883 	if (!start)
884 		return 0;
885 
886 	mutex_lock(&gsi->mutex);
887 
888 	ret = gsi_channel_start_command(channel);
889 
890 	mutex_unlock(&gsi->mutex);
891 
892 	return ret;
893 }
894 
895 /* Start an allocated GSI channel */
896 int gsi_channel_start(struct gsi *gsi, u32 channel_id)
897 {
898 	struct gsi_channel *channel = &gsi->channel[channel_id];
899 	int ret;
900 
901 	/* Enable NAPI and the completion interrupt */
902 	napi_enable(&channel->napi);
903 	gsi_irq_ieob_enable_one(gsi, channel->evt_ring_id);
904 
905 	ret = __gsi_channel_start(channel, true);
906 	if (ret) {
907 		gsi_irq_ieob_disable_one(gsi, channel->evt_ring_id);
908 		napi_disable(&channel->napi);
909 	}
910 
911 	return ret;
912 }
913 
914 static int gsi_channel_stop_retry(struct gsi_channel *channel)
915 {
916 	u32 retries = GSI_CHANNEL_STOP_RETRIES;
917 	int ret;
918 
919 	do {
920 		ret = gsi_channel_stop_command(channel);
921 		if (ret != -EAGAIN)
922 			break;
923 		usleep_range(3 * USEC_PER_MSEC, 5 * USEC_PER_MSEC);
924 	} while (retries--);
925 
926 	return ret;
927 }
928 
929 static int __gsi_channel_stop(struct gsi_channel *channel, bool stop)
930 {
931 	struct gsi *gsi = channel->gsi;
932 	int ret;
933 
934 	/* Wait for any underway transactions to complete before stopping. */
935 	gsi_channel_trans_quiesce(channel);
936 
937 	if (!stop)
938 		return 0;
939 
940 	mutex_lock(&gsi->mutex);
941 
942 	ret = gsi_channel_stop_retry(channel);
943 
944 	mutex_unlock(&gsi->mutex);
945 
946 	return ret;
947 }
948 
949 /* Stop a started channel */
950 int gsi_channel_stop(struct gsi *gsi, u32 channel_id)
951 {
952 	struct gsi_channel *channel = &gsi->channel[channel_id];
953 	int ret;
954 
955 	ret = __gsi_channel_stop(channel, true);
956 	if (ret)
957 		return ret;
958 
959 	/* Disable the completion interrupt and NAPI if successful */
960 	gsi_irq_ieob_disable_one(gsi, channel->evt_ring_id);
961 	napi_disable(&channel->napi);
962 
963 	return 0;
964 }
965 
966 /* Reset and reconfigure a channel, (possibly) enabling the doorbell engine */
967 void gsi_channel_reset(struct gsi *gsi, u32 channel_id, bool doorbell)
968 {
969 	struct gsi_channel *channel = &gsi->channel[channel_id];
970 
971 	mutex_lock(&gsi->mutex);
972 
973 	gsi_channel_reset_command(channel);
974 	/* Due to a hardware quirk we may need to reset RX channels twice. */
975 	if (gsi->version == IPA_VERSION_3_5_1 && !channel->toward_ipa)
976 		gsi_channel_reset_command(channel);
977 
978 	gsi_channel_program(channel, doorbell);
979 	gsi_channel_trans_cancel_pending(channel);
980 
981 	mutex_unlock(&gsi->mutex);
982 }
983 
984 /* Stop a STARTED channel for suspend (using stop if requested) */
985 int gsi_channel_suspend(struct gsi *gsi, u32 channel_id, bool stop)
986 {
987 	struct gsi_channel *channel = &gsi->channel[channel_id];
988 	int ret;
989 
990 	ret = __gsi_channel_stop(channel, stop);
991 	if (ret)
992 		return ret;
993 
994 	/* Ensure NAPI polling has finished. */
995 	napi_synchronize(&channel->napi);
996 
997 	return 0;
998 }
999 
1000 /* Resume a suspended channel (starting will be requested if STOPPED) */
1001 int gsi_channel_resume(struct gsi *gsi, u32 channel_id, bool start)
1002 {
1003 	struct gsi_channel *channel = &gsi->channel[channel_id];
1004 
1005 	return __gsi_channel_start(channel, start);
1006 }
1007 
1008 /**
1009  * gsi_channel_tx_queued() - Report queued TX transfers for a channel
1010  * @channel:	Channel for which to report
1011  *
1012  * Report to the network stack the number of bytes and transactions that
1013  * have been queued to hardware since last call.  This and the next function
1014  * supply information used by the network stack for throttling.
1015  *
1016  * For each channel we track the number of transactions used and bytes of
1017  * data those transactions represent.  We also track what those values are
1018  * each time this function is called.  Subtracting the two tells us
1019  * the number of bytes and transactions that have been added between
1020  * successive calls.
1021  *
1022  * Calling this each time we ring the channel doorbell allows us to
1023  * provide accurate information to the network stack about how much
1024  * work we've given the hardware at any point in time.
1025  */
1026 void gsi_channel_tx_queued(struct gsi_channel *channel)
1027 {
1028 	u32 trans_count;
1029 	u32 byte_count;
1030 
1031 	byte_count = channel->byte_count - channel->queued_byte_count;
1032 	trans_count = channel->trans_count - channel->queued_trans_count;
1033 	channel->queued_byte_count = channel->byte_count;
1034 	channel->queued_trans_count = channel->trans_count;
1035 
1036 	ipa_gsi_channel_tx_queued(channel->gsi, gsi_channel_id(channel),
1037 				  trans_count, byte_count);
1038 }
1039 
1040 /**
1041  * gsi_channel_tx_update() - Report completed TX transfers
1042  * @channel:	Channel that has completed transmitting packets
1043  * @trans:	Last transation known to be complete
1044  *
1045  * Compute the number of transactions and bytes that have been transferred
1046  * over a TX channel since the given transaction was committed.  Report this
1047  * information to the network stack.
1048  *
1049  * At the time a transaction is committed, we record its channel's
1050  * committed transaction and byte counts *in the transaction*.
1051  * Completions are signaled by the hardware with an interrupt, and
1052  * we can determine the latest completed transaction at that time.
1053  *
1054  * The difference between the byte/transaction count recorded in
1055  * the transaction and the count last time we recorded a completion
1056  * tells us exactly how much data has been transferred between
1057  * completions.
1058  *
1059  * Calling this each time we learn of a newly-completed transaction
1060  * allows us to provide accurate information to the network stack
1061  * about how much work has been completed by the hardware at a given
1062  * point in time.
1063  */
1064 static void
1065 gsi_channel_tx_update(struct gsi_channel *channel, struct gsi_trans *trans)
1066 {
1067 	u64 byte_count = trans->byte_count + trans->len;
1068 	u64 trans_count = trans->trans_count + 1;
1069 
1070 	byte_count -= channel->compl_byte_count;
1071 	channel->compl_byte_count += byte_count;
1072 	trans_count -= channel->compl_trans_count;
1073 	channel->compl_trans_count += trans_count;
1074 
1075 	ipa_gsi_channel_tx_completed(channel->gsi, gsi_channel_id(channel),
1076 				     trans_count, byte_count);
1077 }
1078 
1079 /* Channel control interrupt handler */
1080 static void gsi_isr_chan_ctrl(struct gsi *gsi)
1081 {
1082 	u32 channel_mask;
1083 
1084 	channel_mask = ioread32(gsi->virt + GSI_CNTXT_SRC_CH_IRQ_OFFSET);
1085 	iowrite32(channel_mask, gsi->virt + GSI_CNTXT_SRC_CH_IRQ_CLR_OFFSET);
1086 
1087 	while (channel_mask) {
1088 		u32 channel_id = __ffs(channel_mask);
1089 		struct gsi_channel *channel;
1090 
1091 		channel_mask ^= BIT(channel_id);
1092 
1093 		channel = &gsi->channel[channel_id];
1094 
1095 		complete(&channel->completion);
1096 	}
1097 }
1098 
1099 /* Event ring control interrupt handler */
1100 static void gsi_isr_evt_ctrl(struct gsi *gsi)
1101 {
1102 	u32 event_mask;
1103 
1104 	event_mask = ioread32(gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_OFFSET);
1105 	iowrite32(event_mask, gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_CLR_OFFSET);
1106 
1107 	while (event_mask) {
1108 		u32 evt_ring_id = __ffs(event_mask);
1109 		struct gsi_evt_ring *evt_ring;
1110 
1111 		event_mask ^= BIT(evt_ring_id);
1112 
1113 		evt_ring = &gsi->evt_ring[evt_ring_id];
1114 
1115 		complete(&evt_ring->completion);
1116 	}
1117 }
1118 
1119 /* Global channel error interrupt handler */
1120 static void
1121 gsi_isr_glob_chan_err(struct gsi *gsi, u32 err_ee, u32 channel_id, u32 code)
1122 {
1123 	if (code == GSI_OUT_OF_RESOURCES) {
1124 		dev_err(gsi->dev, "channel %u out of resources\n", channel_id);
1125 		complete(&gsi->channel[channel_id].completion);
1126 		return;
1127 	}
1128 
1129 	/* Report, but otherwise ignore all other error codes */
1130 	dev_err(gsi->dev, "channel %u global error ee 0x%08x code 0x%08x\n",
1131 		channel_id, err_ee, code);
1132 }
1133 
1134 /* Global event error interrupt handler */
1135 static void
1136 gsi_isr_glob_evt_err(struct gsi *gsi, u32 err_ee, u32 evt_ring_id, u32 code)
1137 {
1138 	if (code == GSI_OUT_OF_RESOURCES) {
1139 		struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
1140 		u32 channel_id = gsi_channel_id(evt_ring->channel);
1141 
1142 		complete(&evt_ring->completion);
1143 		dev_err(gsi->dev, "evt_ring for channel %u out of resources\n",
1144 			channel_id);
1145 		return;
1146 	}
1147 
1148 	/* Report, but otherwise ignore all other error codes */
1149 	dev_err(gsi->dev, "event ring %u global error ee %u code 0x%08x\n",
1150 		evt_ring_id, err_ee, code);
1151 }
1152 
1153 /* Global error interrupt handler */
1154 static void gsi_isr_glob_err(struct gsi *gsi)
1155 {
1156 	enum gsi_err_type type;
1157 	enum gsi_err_code code;
1158 	u32 which;
1159 	u32 val;
1160 	u32 ee;
1161 
1162 	/* Get the logged error, then reinitialize the log */
1163 	val = ioread32(gsi->virt + GSI_ERROR_LOG_OFFSET);
1164 	iowrite32(0, gsi->virt + GSI_ERROR_LOG_OFFSET);
1165 	iowrite32(~0, gsi->virt + GSI_ERROR_LOG_CLR_OFFSET);
1166 
1167 	ee = u32_get_bits(val, ERR_EE_FMASK);
1168 	type = u32_get_bits(val, ERR_TYPE_FMASK);
1169 	which = u32_get_bits(val, ERR_VIRT_IDX_FMASK);
1170 	code = u32_get_bits(val, ERR_CODE_FMASK);
1171 
1172 	if (type == GSI_ERR_TYPE_CHAN)
1173 		gsi_isr_glob_chan_err(gsi, ee, which, code);
1174 	else if (type == GSI_ERR_TYPE_EVT)
1175 		gsi_isr_glob_evt_err(gsi, ee, which, code);
1176 	else	/* type GSI_ERR_TYPE_GLOB should be fatal */
1177 		dev_err(gsi->dev, "unexpected global error 0x%08x\n", type);
1178 }
1179 
1180 /* Generic EE interrupt handler */
1181 static void gsi_isr_gp_int1(struct gsi *gsi)
1182 {
1183 	u32 result;
1184 	u32 val;
1185 
1186 	/* This interrupt is used to handle completions of the two GENERIC
1187 	 * GSI commands.  We use these to allocate and halt channels on
1188 	 * the modem's behalf due to a hardware quirk on IPA v4.2.  Once
1189 	 * allocated, the modem "owns" these channels, and as a result we
1190 	 * have no way of knowing the channel's state at any given time.
1191 	 *
1192 	 * It is recommended that we halt the modem channels we allocated
1193 	 * when shutting down, but it's possible the channel isn't running
1194 	 * at the time we issue the HALT command.  We'll get an error in
1195 	 * that case, but it's harmless (the channel is already halted).
1196 	 *
1197 	 * For this reason, we silently ignore a CHANNEL_NOT_RUNNING error
1198 	 * if we receive it.
1199 	 */
1200 	val = ioread32(gsi->virt + GSI_CNTXT_SCRATCH_0_OFFSET);
1201 	result = u32_get_bits(val, GENERIC_EE_RESULT_FMASK);
1202 
1203 	switch (result) {
1204 	case GENERIC_EE_SUCCESS:
1205 	case GENERIC_EE_CHANNEL_NOT_RUNNING:
1206 		gsi->result = 0;
1207 		break;
1208 
1209 	case GENERIC_EE_RETRY:
1210 		gsi->result = -EAGAIN;
1211 		break;
1212 
1213 	default:
1214 		dev_err(gsi->dev, "global INT1 generic result %u\n", result);
1215 		gsi->result = -EIO;
1216 		break;
1217 	}
1218 
1219 	complete(&gsi->completion);
1220 }
1221 
1222 /* Inter-EE interrupt handler */
1223 static void gsi_isr_glob_ee(struct gsi *gsi)
1224 {
1225 	u32 val;
1226 
1227 	val = ioread32(gsi->virt + GSI_CNTXT_GLOB_IRQ_STTS_OFFSET);
1228 
1229 	if (val & BIT(ERROR_INT))
1230 		gsi_isr_glob_err(gsi);
1231 
1232 	iowrite32(val, gsi->virt + GSI_CNTXT_GLOB_IRQ_CLR_OFFSET);
1233 
1234 	val &= ~BIT(ERROR_INT);
1235 
1236 	if (val & BIT(GP_INT1)) {
1237 		val ^= BIT(GP_INT1);
1238 		gsi_isr_gp_int1(gsi);
1239 	}
1240 
1241 	if (val)
1242 		dev_err(gsi->dev, "unexpected global interrupt 0x%08x\n", val);
1243 }
1244 
1245 /* I/O completion interrupt event */
1246 static void gsi_isr_ieob(struct gsi *gsi)
1247 {
1248 	u32 event_mask;
1249 
1250 	event_mask = ioread32(gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_OFFSET);
1251 	gsi_irq_ieob_disable(gsi, event_mask);
1252 	iowrite32(event_mask, gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_CLR_OFFSET);
1253 
1254 	while (event_mask) {
1255 		u32 evt_ring_id = __ffs(event_mask);
1256 
1257 		event_mask ^= BIT(evt_ring_id);
1258 
1259 		napi_schedule(&gsi->evt_ring[evt_ring_id].channel->napi);
1260 	}
1261 }
1262 
1263 /* General event interrupts represent serious problems, so report them */
1264 static void gsi_isr_general(struct gsi *gsi)
1265 {
1266 	struct device *dev = gsi->dev;
1267 	u32 val;
1268 
1269 	val = ioread32(gsi->virt + GSI_CNTXT_GSI_IRQ_STTS_OFFSET);
1270 	iowrite32(val, gsi->virt + GSI_CNTXT_GSI_IRQ_CLR_OFFSET);
1271 
1272 	dev_err(dev, "unexpected general interrupt 0x%08x\n", val);
1273 }
1274 
1275 /**
1276  * gsi_isr() - Top level GSI interrupt service routine
1277  * @irq:	Interrupt number (ignored)
1278  * @dev_id:	GSI pointer supplied to request_irq()
1279  *
1280  * This is the main handler function registered for the GSI IRQ. Each type
1281  * of interrupt has a separate handler function that is called from here.
1282  */
1283 static irqreturn_t gsi_isr(int irq, void *dev_id)
1284 {
1285 	struct gsi *gsi = dev_id;
1286 	u32 intr_mask;
1287 	u32 cnt = 0;
1288 
1289 	/* enum gsi_irq_type_id defines GSI interrupt types */
1290 	while ((intr_mask = ioread32(gsi->virt + GSI_CNTXT_TYPE_IRQ_OFFSET))) {
1291 		/* intr_mask contains bitmask of pending GSI interrupts */
1292 		do {
1293 			u32 gsi_intr = BIT(__ffs(intr_mask));
1294 
1295 			intr_mask ^= gsi_intr;
1296 
1297 			switch (gsi_intr) {
1298 			case BIT(GSI_CH_CTRL):
1299 				gsi_isr_chan_ctrl(gsi);
1300 				break;
1301 			case BIT(GSI_EV_CTRL):
1302 				gsi_isr_evt_ctrl(gsi);
1303 				break;
1304 			case BIT(GSI_GLOB_EE):
1305 				gsi_isr_glob_ee(gsi);
1306 				break;
1307 			case BIT(GSI_IEOB):
1308 				gsi_isr_ieob(gsi);
1309 				break;
1310 			case BIT(GSI_GENERAL):
1311 				gsi_isr_general(gsi);
1312 				break;
1313 			default:
1314 				dev_err(gsi->dev,
1315 					"unrecognized interrupt type 0x%08x\n",
1316 					gsi_intr);
1317 				break;
1318 			}
1319 		} while (intr_mask);
1320 
1321 		if (++cnt > GSI_ISR_MAX_ITER) {
1322 			dev_err(gsi->dev, "interrupt flood\n");
1323 			break;
1324 		}
1325 	}
1326 
1327 	return IRQ_HANDLED;
1328 }
1329 
1330 static int gsi_irq_init(struct gsi *gsi, struct platform_device *pdev)
1331 {
1332 	struct device *dev = &pdev->dev;
1333 	unsigned int irq;
1334 	int ret;
1335 
1336 	ret = platform_get_irq_byname(pdev, "gsi");
1337 	if (ret <= 0) {
1338 		dev_err(dev, "DT error %d getting \"gsi\" IRQ property\n", ret);
1339 		return ret ? : -EINVAL;
1340 	}
1341 	irq = ret;
1342 
1343 	ret = request_irq(irq, gsi_isr, 0, "gsi", gsi);
1344 	if (ret) {
1345 		dev_err(dev, "error %d requesting \"gsi\" IRQ\n", ret);
1346 		return ret;
1347 	}
1348 	gsi->irq = irq;
1349 
1350 	return 0;
1351 }
1352 
1353 static void gsi_irq_exit(struct gsi *gsi)
1354 {
1355 	free_irq(gsi->irq, gsi);
1356 }
1357 
1358 /* Return the transaction associated with a transfer completion event */
1359 static struct gsi_trans *gsi_event_trans(struct gsi_channel *channel,
1360 					 struct gsi_event *event)
1361 {
1362 	u32 tre_offset;
1363 	u32 tre_index;
1364 
1365 	/* Event xfer_ptr records the TRE it's associated with */
1366 	tre_offset = le64_to_cpu(event->xfer_ptr) & GENMASK(31, 0);
1367 	tre_index = gsi_ring_index(&channel->tre_ring, tre_offset);
1368 
1369 	return gsi_channel_trans_mapped(channel, tre_index);
1370 }
1371 
1372 /**
1373  * gsi_evt_ring_rx_update() - Record lengths of received data
1374  * @evt_ring:	Event ring associated with channel that received packets
1375  * @index:	Event index in ring reported by hardware
1376  *
1377  * Events for RX channels contain the actual number of bytes received into
1378  * the buffer.  Every event has a transaction associated with it, and here
1379  * we update transactions to record their actual received lengths.
1380  *
1381  * This function is called whenever we learn that the GSI hardware has filled
1382  * new events since the last time we checked.  The ring's index field tells
1383  * the first entry in need of processing.  The index provided is the
1384  * first *unfilled* event in the ring (following the last filled one).
1385  *
1386  * Events are sequential within the event ring, and transactions are
1387  * sequential within the transaction pool.
1388  *
1389  * Note that @index always refers to an element *within* the event ring.
1390  */
1391 static void gsi_evt_ring_rx_update(struct gsi_evt_ring *evt_ring, u32 index)
1392 {
1393 	struct gsi_channel *channel = evt_ring->channel;
1394 	struct gsi_ring *ring = &evt_ring->ring;
1395 	struct gsi_trans_info *trans_info;
1396 	struct gsi_event *event_done;
1397 	struct gsi_event *event;
1398 	struct gsi_trans *trans;
1399 	u32 byte_count = 0;
1400 	u32 old_index;
1401 	u32 event_avail;
1402 
1403 	trans_info = &channel->trans_info;
1404 
1405 	/* We'll start with the oldest un-processed event.  RX channels
1406 	 * replenish receive buffers in single-TRE transactions, so we
1407 	 * can just map that event to its transaction.  Transactions
1408 	 * associated with completion events are consecutive.
1409 	 */
1410 	old_index = ring->index;
1411 	event = gsi_ring_virt(ring, old_index);
1412 	trans = gsi_event_trans(channel, event);
1413 
1414 	/* Compute the number of events to process before we wrap,
1415 	 * and determine when we'll be done processing events.
1416 	 */
1417 	event_avail = ring->count - old_index % ring->count;
1418 	event_done = gsi_ring_virt(ring, index);
1419 	do {
1420 		trans->len = __le16_to_cpu(event->len);
1421 		byte_count += trans->len;
1422 
1423 		/* Move on to the next event and transaction */
1424 		if (--event_avail)
1425 			event++;
1426 		else
1427 			event = gsi_ring_virt(ring, 0);
1428 		trans = gsi_trans_pool_next(&trans_info->pool, trans);
1429 	} while (event != event_done);
1430 
1431 	/* We record RX bytes when they are received */
1432 	channel->byte_count += byte_count;
1433 	channel->trans_count++;
1434 }
1435 
1436 /* Initialize a ring, including allocating DMA memory for its entries */
1437 static int gsi_ring_alloc(struct gsi *gsi, struct gsi_ring *ring, u32 count)
1438 {
1439 	size_t size = count * GSI_RING_ELEMENT_SIZE;
1440 	struct device *dev = gsi->dev;
1441 	dma_addr_t addr;
1442 
1443 	/* Hardware requires a 2^n ring size, with alignment equal to size */
1444 	ring->virt = dma_alloc_coherent(dev, size, &addr, GFP_KERNEL);
1445 	if (ring->virt && addr % size) {
1446 		dma_free_coherent(dev, size, ring->virt, addr);
1447 		dev_err(dev, "unable to alloc 0x%zx-aligned ring buffer\n",
1448 			size);
1449 		return -EINVAL;	/* Not a good error value, but distinct */
1450 	} else if (!ring->virt) {
1451 		return -ENOMEM;
1452 	}
1453 	ring->addr = addr;
1454 	ring->count = count;
1455 
1456 	return 0;
1457 }
1458 
1459 /* Free a previously-allocated ring */
1460 static void gsi_ring_free(struct gsi *gsi, struct gsi_ring *ring)
1461 {
1462 	size_t size = ring->count * GSI_RING_ELEMENT_SIZE;
1463 
1464 	dma_free_coherent(gsi->dev, size, ring->virt, ring->addr);
1465 }
1466 
1467 /* Allocate an available event ring id */
1468 static int gsi_evt_ring_id_alloc(struct gsi *gsi)
1469 {
1470 	u32 evt_ring_id;
1471 
1472 	if (gsi->event_bitmap == ~0U) {
1473 		dev_err(gsi->dev, "event rings exhausted\n");
1474 		return -ENOSPC;
1475 	}
1476 
1477 	evt_ring_id = ffz(gsi->event_bitmap);
1478 	gsi->event_bitmap |= BIT(evt_ring_id);
1479 
1480 	return (int)evt_ring_id;
1481 }
1482 
1483 /* Free a previously-allocated event ring id */
1484 static void gsi_evt_ring_id_free(struct gsi *gsi, u32 evt_ring_id)
1485 {
1486 	gsi->event_bitmap &= ~BIT(evt_ring_id);
1487 }
1488 
1489 /* Ring a channel doorbell, reporting the first un-filled entry */
1490 void gsi_channel_doorbell(struct gsi_channel *channel)
1491 {
1492 	struct gsi_ring *tre_ring = &channel->tre_ring;
1493 	u32 channel_id = gsi_channel_id(channel);
1494 	struct gsi *gsi = channel->gsi;
1495 	u32 val;
1496 
1497 	/* Note: index *must* be used modulo the ring count here */
1498 	val = gsi_ring_addr(tre_ring, tre_ring->index % tre_ring->count);
1499 	iowrite32(val, gsi->virt + GSI_CH_C_DOORBELL_0_OFFSET(channel_id));
1500 }
1501 
1502 /* Consult hardware, move any newly completed transactions to completed list */
1503 static struct gsi_trans *gsi_channel_update(struct gsi_channel *channel)
1504 {
1505 	u32 evt_ring_id = channel->evt_ring_id;
1506 	struct gsi *gsi = channel->gsi;
1507 	struct gsi_evt_ring *evt_ring;
1508 	struct gsi_trans *trans;
1509 	struct gsi_ring *ring;
1510 	u32 offset;
1511 	u32 index;
1512 
1513 	evt_ring = &gsi->evt_ring[evt_ring_id];
1514 	ring = &evt_ring->ring;
1515 
1516 	/* See if there's anything new to process; if not, we're done.  Note
1517 	 * that index always refers to an entry *within* the event ring.
1518 	 */
1519 	offset = GSI_EV_CH_E_CNTXT_4_OFFSET(evt_ring_id);
1520 	index = gsi_ring_index(ring, ioread32(gsi->virt + offset));
1521 	if (index == ring->index % ring->count)
1522 		return NULL;
1523 
1524 	/* Get the transaction for the latest completed event.  Take a
1525 	 * reference to keep it from completing before we give the events
1526 	 * for this and previous transactions back to the hardware.
1527 	 */
1528 	trans = gsi_event_trans(channel, gsi_ring_virt(ring, index - 1));
1529 	refcount_inc(&trans->refcount);
1530 
1531 	/* For RX channels, update each completed transaction with the number
1532 	 * of bytes that were actually received.  For TX channels, report
1533 	 * the number of transactions and bytes this completion represents
1534 	 * up the network stack.
1535 	 */
1536 	if (channel->toward_ipa)
1537 		gsi_channel_tx_update(channel, trans);
1538 	else
1539 		gsi_evt_ring_rx_update(evt_ring, index);
1540 
1541 	gsi_trans_move_complete(trans);
1542 
1543 	/* Tell the hardware we've handled these events */
1544 	gsi_evt_ring_doorbell(channel->gsi, channel->evt_ring_id, index);
1545 
1546 	gsi_trans_free(trans);
1547 
1548 	return gsi_channel_trans_complete(channel);
1549 }
1550 
1551 /**
1552  * gsi_channel_poll_one() - Return a single completed transaction on a channel
1553  * @channel:	Channel to be polled
1554  *
1555  * Return:	Transaction pointer, or null if none are available
1556  *
1557  * This function returns the first entry on a channel's completed transaction
1558  * list.  If that list is empty, the hardware is consulted to determine
1559  * whether any new transactions have completed.  If so, they're moved to the
1560  * completed list and the new first entry is returned.  If there are no more
1561  * completed transactions, a null pointer is returned.
1562  */
1563 static struct gsi_trans *gsi_channel_poll_one(struct gsi_channel *channel)
1564 {
1565 	struct gsi_trans *trans;
1566 
1567 	/* Get the first transaction from the completed list */
1568 	trans = gsi_channel_trans_complete(channel);
1569 	if (!trans)	/* List is empty; see if there's more to do */
1570 		trans = gsi_channel_update(channel);
1571 
1572 	if (trans)
1573 		gsi_trans_move_polled(trans);
1574 
1575 	return trans;
1576 }
1577 
1578 /**
1579  * gsi_channel_poll() - NAPI poll function for a channel
1580  * @napi:	NAPI structure for the channel
1581  * @budget:	Budget supplied by NAPI core
1582  *
1583  * Return:	Number of items polled (<= budget)
1584  *
1585  * Single transactions completed by hardware are polled until either
1586  * the budget is exhausted, or there are no more.  Each transaction
1587  * polled is passed to gsi_trans_complete(), to perform remaining
1588  * completion processing and retire/free the transaction.
1589  */
1590 static int gsi_channel_poll(struct napi_struct *napi, int budget)
1591 {
1592 	struct gsi_channel *channel;
1593 	int count;
1594 
1595 	channel = container_of(napi, struct gsi_channel, napi);
1596 	for (count = 0; count < budget; count++) {
1597 		struct gsi_trans *trans;
1598 
1599 		trans = gsi_channel_poll_one(channel);
1600 		if (!trans)
1601 			break;
1602 		gsi_trans_complete(trans);
1603 	}
1604 
1605 	if (count < budget && napi_complete(napi))
1606 		gsi_irq_ieob_enable_one(channel->gsi, channel->evt_ring_id);
1607 
1608 	return count;
1609 }
1610 
1611 /* The event bitmap represents which event ids are available for allocation.
1612  * Set bits are not available, clear bits can be used.  This function
1613  * initializes the map so all events supported by the hardware are available,
1614  * then precludes any reserved events from being allocated.
1615  */
1616 static u32 gsi_event_bitmap_init(u32 evt_ring_max)
1617 {
1618 	u32 event_bitmap = GENMASK(BITS_PER_LONG - 1, evt_ring_max);
1619 
1620 	event_bitmap |= GENMASK(GSI_MHI_EVENT_ID_END, GSI_MHI_EVENT_ID_START);
1621 
1622 	return event_bitmap;
1623 }
1624 
1625 /* Setup function for event rings */
1626 static void gsi_evt_ring_setup(struct gsi *gsi)
1627 {
1628 	/* Nothing to do */
1629 }
1630 
1631 /* Inverse of gsi_evt_ring_setup() */
1632 static void gsi_evt_ring_teardown(struct gsi *gsi)
1633 {
1634 	/* Nothing to do */
1635 }
1636 
1637 /* Setup function for a single channel */
1638 static int gsi_channel_setup_one(struct gsi *gsi, u32 channel_id)
1639 {
1640 	struct gsi_channel *channel = &gsi->channel[channel_id];
1641 	u32 evt_ring_id = channel->evt_ring_id;
1642 	int ret;
1643 
1644 	if (!channel->gsi)
1645 		return 0;	/* Ignore uninitialized channels */
1646 
1647 	ret = gsi_evt_ring_alloc_command(gsi, evt_ring_id);
1648 	if (ret)
1649 		return ret;
1650 
1651 	gsi_evt_ring_program(gsi, evt_ring_id);
1652 
1653 	ret = gsi_channel_alloc_command(gsi, channel_id);
1654 	if (ret)
1655 		goto err_evt_ring_de_alloc;
1656 
1657 	gsi_channel_program(channel, true);
1658 
1659 	if (channel->toward_ipa)
1660 		netif_tx_napi_add(&gsi->dummy_dev, &channel->napi,
1661 				  gsi_channel_poll, NAPI_POLL_WEIGHT);
1662 	else
1663 		netif_napi_add(&gsi->dummy_dev, &channel->napi,
1664 			       gsi_channel_poll, NAPI_POLL_WEIGHT);
1665 
1666 	return 0;
1667 
1668 err_evt_ring_de_alloc:
1669 	/* We've done nothing with the event ring yet so don't reset */
1670 	gsi_evt_ring_de_alloc_command(gsi, evt_ring_id);
1671 
1672 	return ret;
1673 }
1674 
1675 /* Inverse of gsi_channel_setup_one() */
1676 static void gsi_channel_teardown_one(struct gsi *gsi, u32 channel_id)
1677 {
1678 	struct gsi_channel *channel = &gsi->channel[channel_id];
1679 	u32 evt_ring_id = channel->evt_ring_id;
1680 
1681 	if (!channel->gsi)
1682 		return;		/* Ignore uninitialized channels */
1683 
1684 	netif_napi_del(&channel->napi);
1685 
1686 	gsi_channel_deprogram(channel);
1687 	gsi_channel_de_alloc_command(gsi, channel_id);
1688 	gsi_evt_ring_reset_command(gsi, evt_ring_id);
1689 	gsi_evt_ring_de_alloc_command(gsi, evt_ring_id);
1690 }
1691 
1692 static int gsi_generic_command(struct gsi *gsi, u32 channel_id,
1693 			       enum gsi_generic_cmd_opcode opcode)
1694 {
1695 	struct completion *completion = &gsi->completion;
1696 	bool timeout;
1697 	u32 val;
1698 
1699 	/* The error global interrupt type is always enabled (until we
1700 	 * teardown), so we won't change that.  A generic EE command
1701 	 * completes with a GSI global interrupt of type GP_INT1.  We
1702 	 * only perform one generic command at a time (to allocate or
1703 	 * halt a modem channel) and only from this function.  So we
1704 	 * enable the GP_INT1 IRQ type here while we're expecting it.
1705 	 */
1706 	val = BIT(ERROR_INT) | BIT(GP_INT1);
1707 	iowrite32(val, gsi->virt + GSI_CNTXT_GLOB_IRQ_EN_OFFSET);
1708 
1709 	/* First zero the result code field */
1710 	val = ioread32(gsi->virt + GSI_CNTXT_SCRATCH_0_OFFSET);
1711 	val &= ~GENERIC_EE_RESULT_FMASK;
1712 	iowrite32(val, gsi->virt + GSI_CNTXT_SCRATCH_0_OFFSET);
1713 
1714 	/* Now issue the command */
1715 	val = u32_encode_bits(opcode, GENERIC_OPCODE_FMASK);
1716 	val |= u32_encode_bits(channel_id, GENERIC_CHID_FMASK);
1717 	val |= u32_encode_bits(GSI_EE_MODEM, GENERIC_EE_FMASK);
1718 
1719 	timeout = !gsi_command(gsi, GSI_GENERIC_CMD_OFFSET, val, completion);
1720 
1721 	/* Disable the GP_INT1 IRQ type again */
1722 	iowrite32(BIT(ERROR_INT), gsi->virt + GSI_CNTXT_GLOB_IRQ_EN_OFFSET);
1723 
1724 	if (!timeout)
1725 		return gsi->result;
1726 
1727 	dev_err(gsi->dev, "GSI generic command %u to channel %u timed out\n",
1728 		opcode, channel_id);
1729 
1730 	return -ETIMEDOUT;
1731 }
1732 
1733 static int gsi_modem_channel_alloc(struct gsi *gsi, u32 channel_id)
1734 {
1735 	return gsi_generic_command(gsi, channel_id,
1736 				   GSI_GENERIC_ALLOCATE_CHANNEL);
1737 }
1738 
1739 static void gsi_modem_channel_halt(struct gsi *gsi, u32 channel_id)
1740 {
1741 	u32 retries = GSI_CHANNEL_MODEM_HALT_RETRIES;
1742 	int ret;
1743 
1744 	do
1745 		ret = gsi_generic_command(gsi, channel_id,
1746 					  GSI_GENERIC_HALT_CHANNEL);
1747 	while (ret == -EAGAIN && retries--);
1748 
1749 	if (ret)
1750 		dev_err(gsi->dev, "error %d halting modem channel %u\n",
1751 			ret, channel_id);
1752 }
1753 
1754 /* Setup function for channels */
1755 static int gsi_channel_setup(struct gsi *gsi)
1756 {
1757 	u32 channel_id = 0;
1758 	u32 mask;
1759 	int ret;
1760 
1761 	gsi_evt_ring_setup(gsi);
1762 	gsi_irq_enable(gsi);
1763 
1764 	mutex_lock(&gsi->mutex);
1765 
1766 	do {
1767 		ret = gsi_channel_setup_one(gsi, channel_id);
1768 		if (ret)
1769 			goto err_unwind;
1770 	} while (++channel_id < gsi->channel_count);
1771 
1772 	/* Make sure no channels were defined that hardware does not support */
1773 	while (channel_id < GSI_CHANNEL_COUNT_MAX) {
1774 		struct gsi_channel *channel = &gsi->channel[channel_id++];
1775 
1776 		if (!channel->gsi)
1777 			continue;	/* Ignore uninitialized channels */
1778 
1779 		ret = -EINVAL;
1780 		dev_err(gsi->dev, "channel %u not supported by hardware\n",
1781 			channel_id - 1);
1782 		channel_id = gsi->channel_count;
1783 		goto err_unwind;
1784 	}
1785 
1786 	/* Allocate modem channels if necessary */
1787 	mask = gsi->modem_channel_bitmap;
1788 	while (mask) {
1789 		u32 modem_channel_id = __ffs(mask);
1790 
1791 		ret = gsi_modem_channel_alloc(gsi, modem_channel_id);
1792 		if (ret)
1793 			goto err_unwind_modem;
1794 
1795 		/* Clear bit from mask only after success (for unwind) */
1796 		mask ^= BIT(modem_channel_id);
1797 	}
1798 
1799 	mutex_unlock(&gsi->mutex);
1800 
1801 	return 0;
1802 
1803 err_unwind_modem:
1804 	/* Compute which modem channels need to be deallocated */
1805 	mask ^= gsi->modem_channel_bitmap;
1806 	while (mask) {
1807 		channel_id = __fls(mask);
1808 
1809 		mask ^= BIT(channel_id);
1810 
1811 		gsi_modem_channel_halt(gsi, channel_id);
1812 	}
1813 
1814 err_unwind:
1815 	while (channel_id--)
1816 		gsi_channel_teardown_one(gsi, channel_id);
1817 
1818 	mutex_unlock(&gsi->mutex);
1819 
1820 	gsi_irq_disable(gsi);
1821 	gsi_evt_ring_teardown(gsi);
1822 
1823 	return ret;
1824 }
1825 
1826 /* Inverse of gsi_channel_setup() */
1827 static void gsi_channel_teardown(struct gsi *gsi)
1828 {
1829 	u32 mask = gsi->modem_channel_bitmap;
1830 	u32 channel_id;
1831 
1832 	mutex_lock(&gsi->mutex);
1833 
1834 	while (mask) {
1835 		channel_id = __fls(mask);
1836 
1837 		mask ^= BIT(channel_id);
1838 
1839 		gsi_modem_channel_halt(gsi, channel_id);
1840 	}
1841 
1842 	channel_id = gsi->channel_count - 1;
1843 	do
1844 		gsi_channel_teardown_one(gsi, channel_id);
1845 	while (channel_id--);
1846 
1847 	mutex_unlock(&gsi->mutex);
1848 
1849 	gsi_irq_disable(gsi);
1850 	gsi_evt_ring_teardown(gsi);
1851 }
1852 
1853 /* Setup function for GSI.  GSI firmware must be loaded and initialized */
1854 int gsi_setup(struct gsi *gsi)
1855 {
1856 	struct device *dev = gsi->dev;
1857 	u32 val;
1858 	int ret;
1859 
1860 	/* Here is where we first touch the GSI hardware */
1861 	val = ioread32(gsi->virt + GSI_GSI_STATUS_OFFSET);
1862 	if (!(val & ENABLED_FMASK)) {
1863 		dev_err(dev, "GSI has not been enabled\n");
1864 		return -EIO;
1865 	}
1866 
1867 	gsi_irq_setup(gsi);
1868 
1869 	val = ioread32(gsi->virt + GSI_GSI_HW_PARAM_2_OFFSET);
1870 
1871 	gsi->channel_count = u32_get_bits(val, NUM_CH_PER_EE_FMASK);
1872 	if (!gsi->channel_count) {
1873 		dev_err(dev, "GSI reports zero channels supported\n");
1874 		return -EINVAL;
1875 	}
1876 	if (gsi->channel_count > GSI_CHANNEL_COUNT_MAX) {
1877 		dev_warn(dev,
1878 			 "limiting to %u channels; hardware supports %u\n",
1879 			 GSI_CHANNEL_COUNT_MAX, gsi->channel_count);
1880 		gsi->channel_count = GSI_CHANNEL_COUNT_MAX;
1881 	}
1882 
1883 	gsi->evt_ring_count = u32_get_bits(val, NUM_EV_PER_EE_FMASK);
1884 	if (!gsi->evt_ring_count) {
1885 		dev_err(dev, "GSI reports zero event rings supported\n");
1886 		return -EINVAL;
1887 	}
1888 	if (gsi->evt_ring_count > GSI_EVT_RING_COUNT_MAX) {
1889 		dev_warn(dev,
1890 			 "limiting to %u event rings; hardware supports %u\n",
1891 			 GSI_EVT_RING_COUNT_MAX, gsi->evt_ring_count);
1892 		gsi->evt_ring_count = GSI_EVT_RING_COUNT_MAX;
1893 	}
1894 
1895 	/* Initialize the error log */
1896 	iowrite32(0, gsi->virt + GSI_ERROR_LOG_OFFSET);
1897 
1898 	/* Writing 1 indicates IRQ interrupts; 0 would be MSI */
1899 	iowrite32(1, gsi->virt + GSI_CNTXT_INTSET_OFFSET);
1900 
1901 	ret = gsi_channel_setup(gsi);
1902 	if (ret)
1903 		gsi_irq_teardown(gsi);
1904 
1905 	return ret;
1906 }
1907 
1908 /* Inverse of gsi_setup() */
1909 void gsi_teardown(struct gsi *gsi)
1910 {
1911 	gsi_channel_teardown(gsi);
1912 	gsi_irq_teardown(gsi);
1913 }
1914 
1915 /* Initialize a channel's event ring */
1916 static int gsi_channel_evt_ring_init(struct gsi_channel *channel)
1917 {
1918 	struct gsi *gsi = channel->gsi;
1919 	struct gsi_evt_ring *evt_ring;
1920 	int ret;
1921 
1922 	ret = gsi_evt_ring_id_alloc(gsi);
1923 	if (ret < 0)
1924 		return ret;
1925 	channel->evt_ring_id = ret;
1926 
1927 	evt_ring = &gsi->evt_ring[channel->evt_ring_id];
1928 	evt_ring->channel = channel;
1929 
1930 	ret = gsi_ring_alloc(gsi, &evt_ring->ring, channel->event_count);
1931 	if (!ret)
1932 		return 0;	/* Success! */
1933 
1934 	dev_err(gsi->dev, "error %d allocating channel %u event ring\n",
1935 		ret, gsi_channel_id(channel));
1936 
1937 	gsi_evt_ring_id_free(gsi, channel->evt_ring_id);
1938 
1939 	return ret;
1940 }
1941 
1942 /* Inverse of gsi_channel_evt_ring_init() */
1943 static void gsi_channel_evt_ring_exit(struct gsi_channel *channel)
1944 {
1945 	u32 evt_ring_id = channel->evt_ring_id;
1946 	struct gsi *gsi = channel->gsi;
1947 	struct gsi_evt_ring *evt_ring;
1948 
1949 	evt_ring = &gsi->evt_ring[evt_ring_id];
1950 	gsi_ring_free(gsi, &evt_ring->ring);
1951 	gsi_evt_ring_id_free(gsi, evt_ring_id);
1952 }
1953 
1954 /* Init function for event rings */
1955 static void gsi_evt_ring_init(struct gsi *gsi)
1956 {
1957 	u32 evt_ring_id = 0;
1958 
1959 	gsi->event_bitmap = gsi_event_bitmap_init(GSI_EVT_RING_COUNT_MAX);
1960 	gsi->ieob_enabled_bitmap = 0;
1961 	do
1962 		init_completion(&gsi->evt_ring[evt_ring_id].completion);
1963 	while (++evt_ring_id < GSI_EVT_RING_COUNT_MAX);
1964 }
1965 
1966 /* Inverse of gsi_evt_ring_init() */
1967 static void gsi_evt_ring_exit(struct gsi *gsi)
1968 {
1969 	/* Nothing to do */
1970 }
1971 
1972 static bool gsi_channel_data_valid(struct gsi *gsi,
1973 				   const struct ipa_gsi_endpoint_data *data)
1974 {
1975 #ifdef IPA_VALIDATION
1976 	u32 channel_id = data->channel_id;
1977 	struct device *dev = gsi->dev;
1978 
1979 	/* Make sure channel ids are in the range driver supports */
1980 	if (channel_id >= GSI_CHANNEL_COUNT_MAX) {
1981 		dev_err(dev, "bad channel id %u; must be less than %u\n",
1982 			channel_id, GSI_CHANNEL_COUNT_MAX);
1983 		return false;
1984 	}
1985 
1986 	if (data->ee_id != GSI_EE_AP && data->ee_id != GSI_EE_MODEM) {
1987 		dev_err(dev, "bad EE id %u; not AP or modem\n", data->ee_id);
1988 		return false;
1989 	}
1990 
1991 	if (!data->channel.tlv_count ||
1992 	    data->channel.tlv_count > GSI_TLV_MAX) {
1993 		dev_err(dev, "channel %u bad tlv_count %u; must be 1..%u\n",
1994 			channel_id, data->channel.tlv_count, GSI_TLV_MAX);
1995 		return false;
1996 	}
1997 
1998 	/* We have to allow at least one maximally-sized transaction to
1999 	 * be outstanding (which would use tlv_count TREs).  Given how
2000 	 * gsi_channel_tre_max() is computed, tre_count has to be almost
2001 	 * twice the TLV FIFO size to satisfy this requirement.
2002 	 */
2003 	if (data->channel.tre_count < 2 * data->channel.tlv_count - 1) {
2004 		dev_err(dev, "channel %u TLV count %u exceeds TRE count %u\n",
2005 			channel_id, data->channel.tlv_count,
2006 			data->channel.tre_count);
2007 		return false;
2008 	}
2009 
2010 	if (!is_power_of_2(data->channel.tre_count)) {
2011 		dev_err(dev, "channel %u bad tre_count %u; not power of 2\n",
2012 			channel_id, data->channel.tre_count);
2013 		return false;
2014 	}
2015 
2016 	if (!is_power_of_2(data->channel.event_count)) {
2017 		dev_err(dev, "channel %u bad event_count %u; not power of 2\n",
2018 			channel_id, data->channel.event_count);
2019 		return false;
2020 	}
2021 #endif /* IPA_VALIDATION */
2022 
2023 	return true;
2024 }
2025 
2026 /* Init function for a single channel */
2027 static int gsi_channel_init_one(struct gsi *gsi,
2028 				const struct ipa_gsi_endpoint_data *data,
2029 				bool command)
2030 {
2031 	struct gsi_channel *channel;
2032 	u32 tre_count;
2033 	int ret;
2034 
2035 	if (!gsi_channel_data_valid(gsi, data))
2036 		return -EINVAL;
2037 
2038 	/* Worst case we need an event for every outstanding TRE */
2039 	if (data->channel.tre_count > data->channel.event_count) {
2040 		tre_count = data->channel.event_count;
2041 		dev_warn(gsi->dev, "channel %u limited to %u TREs\n",
2042 			 data->channel_id, tre_count);
2043 	} else {
2044 		tre_count = data->channel.tre_count;
2045 	}
2046 
2047 	channel = &gsi->channel[data->channel_id];
2048 	memset(channel, 0, sizeof(*channel));
2049 
2050 	channel->gsi = gsi;
2051 	channel->toward_ipa = data->toward_ipa;
2052 	channel->command = command;
2053 	channel->tlv_count = data->channel.tlv_count;
2054 	channel->tre_count = tre_count;
2055 	channel->event_count = data->channel.event_count;
2056 	init_completion(&channel->completion);
2057 
2058 	ret = gsi_channel_evt_ring_init(channel);
2059 	if (ret)
2060 		goto err_clear_gsi;
2061 
2062 	ret = gsi_ring_alloc(gsi, &channel->tre_ring, data->channel.tre_count);
2063 	if (ret) {
2064 		dev_err(gsi->dev, "error %d allocating channel %u ring\n",
2065 			ret, data->channel_id);
2066 		goto err_channel_evt_ring_exit;
2067 	}
2068 
2069 	ret = gsi_channel_trans_init(gsi, data->channel_id);
2070 	if (ret)
2071 		goto err_ring_free;
2072 
2073 	if (command) {
2074 		u32 tre_max = gsi_channel_tre_max(gsi, data->channel_id);
2075 
2076 		ret = ipa_cmd_pool_init(channel, tre_max);
2077 	}
2078 	if (!ret)
2079 		return 0;	/* Success! */
2080 
2081 	gsi_channel_trans_exit(channel);
2082 err_ring_free:
2083 	gsi_ring_free(gsi, &channel->tre_ring);
2084 err_channel_evt_ring_exit:
2085 	gsi_channel_evt_ring_exit(channel);
2086 err_clear_gsi:
2087 	channel->gsi = NULL;	/* Mark it not (fully) initialized */
2088 
2089 	return ret;
2090 }
2091 
2092 /* Inverse of gsi_channel_init_one() */
2093 static void gsi_channel_exit_one(struct gsi_channel *channel)
2094 {
2095 	if (!channel->gsi)
2096 		return;		/* Ignore uninitialized channels */
2097 
2098 	if (channel->command)
2099 		ipa_cmd_pool_exit(channel);
2100 	gsi_channel_trans_exit(channel);
2101 	gsi_ring_free(channel->gsi, &channel->tre_ring);
2102 	gsi_channel_evt_ring_exit(channel);
2103 }
2104 
2105 /* Init function for channels */
2106 static int gsi_channel_init(struct gsi *gsi, u32 count,
2107 			    const struct ipa_gsi_endpoint_data *data)
2108 {
2109 	bool modem_alloc;
2110 	int ret = 0;
2111 	u32 i;
2112 
2113 	/* IPA v4.2 requires the AP to allocate channels for the modem */
2114 	modem_alloc = gsi->version == IPA_VERSION_4_2;
2115 
2116 	gsi_evt_ring_init(gsi);
2117 
2118 	/* The endpoint data array is indexed by endpoint name */
2119 	for (i = 0; i < count; i++) {
2120 		bool command = i == IPA_ENDPOINT_AP_COMMAND_TX;
2121 
2122 		if (ipa_gsi_endpoint_data_empty(&data[i]))
2123 			continue;	/* Skip over empty slots */
2124 
2125 		/* Mark modem channels to be allocated (hardware workaround) */
2126 		if (data[i].ee_id == GSI_EE_MODEM) {
2127 			if (modem_alloc)
2128 				gsi->modem_channel_bitmap |=
2129 						BIT(data[i].channel_id);
2130 			continue;
2131 		}
2132 
2133 		ret = gsi_channel_init_one(gsi, &data[i], command);
2134 		if (ret)
2135 			goto err_unwind;
2136 	}
2137 
2138 	return ret;
2139 
2140 err_unwind:
2141 	while (i--) {
2142 		if (ipa_gsi_endpoint_data_empty(&data[i]))
2143 			continue;
2144 		if (modem_alloc && data[i].ee_id == GSI_EE_MODEM) {
2145 			gsi->modem_channel_bitmap &= ~BIT(data[i].channel_id);
2146 			continue;
2147 		}
2148 		gsi_channel_exit_one(&gsi->channel[data->channel_id]);
2149 	}
2150 	gsi_evt_ring_exit(gsi);
2151 
2152 	return ret;
2153 }
2154 
2155 /* Inverse of gsi_channel_init() */
2156 static void gsi_channel_exit(struct gsi *gsi)
2157 {
2158 	u32 channel_id = GSI_CHANNEL_COUNT_MAX - 1;
2159 
2160 	do
2161 		gsi_channel_exit_one(&gsi->channel[channel_id]);
2162 	while (channel_id--);
2163 	gsi->modem_channel_bitmap = 0;
2164 
2165 	gsi_evt_ring_exit(gsi);
2166 }
2167 
2168 /* Init function for GSI.  GSI hardware does not need to be "ready" */
2169 int gsi_init(struct gsi *gsi, struct platform_device *pdev,
2170 	     enum ipa_version version, u32 count,
2171 	     const struct ipa_gsi_endpoint_data *data)
2172 {
2173 	struct device *dev = &pdev->dev;
2174 	struct resource *res;
2175 	resource_size_t size;
2176 	u32 adjust;
2177 	int ret;
2178 
2179 	gsi_validate_build();
2180 
2181 	gsi->dev = dev;
2182 	gsi->version = version;
2183 
2184 	/* The GSI layer performs NAPI on all endpoints.  NAPI requires a
2185 	 * network device structure, but the GSI layer does not have one,
2186 	 * so we must create a dummy network device for this purpose.
2187 	 */
2188 	init_dummy_netdev(&gsi->dummy_dev);
2189 
2190 	/* Get GSI memory range and map it */
2191 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "gsi");
2192 	if (!res) {
2193 		dev_err(dev, "DT error getting \"gsi\" memory property\n");
2194 		return -ENODEV;
2195 	}
2196 
2197 	size = resource_size(res);
2198 	if (res->start > U32_MAX || size > U32_MAX - res->start) {
2199 		dev_err(dev, "DT memory resource \"gsi\" out of range\n");
2200 		return -EINVAL;
2201 	}
2202 
2203 	/* Make sure we can make our pointer adjustment if necessary */
2204 	adjust = gsi->version < IPA_VERSION_4_5 ? 0 : GSI_EE_REG_ADJUST;
2205 	if (res->start < adjust) {
2206 		dev_err(dev, "DT memory resource \"gsi\" too low (< %u)\n",
2207 			adjust);
2208 		return -EINVAL;
2209 	}
2210 
2211 	gsi->virt = ioremap(res->start, size);
2212 	if (!gsi->virt) {
2213 		dev_err(dev, "unable to remap \"gsi\" memory\n");
2214 		return -ENOMEM;
2215 	}
2216 	/* Adjust register range pointer downward for newer IPA versions */
2217 	gsi->virt -= adjust;
2218 
2219 	init_completion(&gsi->completion);
2220 
2221 	ret = gsi_irq_init(gsi, pdev);
2222 	if (ret)
2223 		goto err_iounmap;
2224 
2225 	ret = gsi_channel_init(gsi, count, data);
2226 	if (ret)
2227 		goto err_irq_exit;
2228 
2229 	mutex_init(&gsi->mutex);
2230 
2231 	return 0;
2232 
2233 err_irq_exit:
2234 	gsi_irq_exit(gsi);
2235 err_iounmap:
2236 	iounmap(gsi->virt);
2237 
2238 	return ret;
2239 }
2240 
2241 /* Inverse of gsi_init() */
2242 void gsi_exit(struct gsi *gsi)
2243 {
2244 	mutex_destroy(&gsi->mutex);
2245 	gsi_channel_exit(gsi);
2246 	gsi_irq_exit(gsi);
2247 	iounmap(gsi->virt);
2248 }
2249 
2250 /* The maximum number of outstanding TREs on a channel.  This limits
2251  * a channel's maximum number of transactions outstanding (worst case
2252  * is one TRE per transaction).
2253  *
2254  * The absolute limit is the number of TREs in the channel's TRE ring,
2255  * and in theory we should be able use all of them.  But in practice,
2256  * doing that led to the hardware reporting exhaustion of event ring
2257  * slots for writing completion information.  So the hardware limit
2258  * would be (tre_count - 1).
2259  *
2260  * We reduce it a bit further though.  Transaction resource pools are
2261  * sized to be a little larger than this maximum, to allow resource
2262  * allocations to always be contiguous.  The number of entries in a
2263  * TRE ring buffer is a power of 2, and the extra resources in a pool
2264  * tends to nearly double the memory allocated for it.  Reducing the
2265  * maximum number of outstanding TREs allows the number of entries in
2266  * a pool to avoid crossing that power-of-2 boundary, and this can
2267  * substantially reduce pool memory requirements.  The number we
2268  * reduce it by matches the number added in gsi_trans_pool_init().
2269  */
2270 u32 gsi_channel_tre_max(struct gsi *gsi, u32 channel_id)
2271 {
2272 	struct gsi_channel *channel = &gsi->channel[channel_id];
2273 
2274 	/* Hardware limit is channel->tre_count - 1 */
2275 	return channel->tre_count - (channel->tlv_count - 1);
2276 }
2277 
2278 /* Returns the maximum number of TREs in a single transaction for a channel */
2279 u32 gsi_channel_trans_tre_max(struct gsi *gsi, u32 channel_id)
2280 {
2281 	struct gsi_channel *channel = &gsi->channel[channel_id];
2282 
2283 	return channel->tlv_count;
2284 }
2285