1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2018-2023 Linaro Ltd.
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>
13 #include <linux/bug.h>
14 #include <linux/interrupt.h>
15 #include <linux/platform_device.h>
16 #include <linux/netdevice.h>
21 #include "gsi_private.h"
22 #include "gsi_trans.h"
25 #include "ipa_version.h"
28 * DOC: The IPA Generic Software Interface
30 * The generic software interface (GSI) is an integral component of the IPA,
31 * providing a well-defined communication layer between the AP subsystem
32 * and the IPA core. The modem uses the GSI layer as well.
36 * | AP +<---. .----+ Modem |
39 * -------- | | | | ---------
49 * In the above diagram, the AP and Modem represent "execution environments"
50 * (EEs), which are independent operating environments that use the IPA for
53 * Each EE uses a set of unidirectional GSI "channels," which allow transfer
54 * of data to or from the IPA. A channel is implemented as a ring buffer,
55 * with a DRAM-resident array of "transfer elements" (TREs) available to
56 * describe transfers to or from other EEs through the IPA. A transfer
57 * element can also contain an immediate command, requesting the IPA perform
58 * actions other than data transfer.
60 * Each TRE refers to a block of data--also located in DRAM. After writing
61 * one or more TREs to a channel, the writer (either the IPA or an EE) writes
62 * a doorbell register to inform the receiving side how many elements have
65 * Each channel has a GSI "event ring" associated with it. An event ring
66 * is implemented very much like a channel ring, but is always directed from
67 * the IPA to an EE. The IPA notifies an EE (such as the AP) about channel
68 * events by adding an entry to the event ring associated with the channel.
69 * The GSI then writes its doorbell for the event ring, causing the target
70 * EE to be interrupted. Each entry in an event ring contains a pointer
71 * to the channel TRE whose completion the event represents.
73 * Each TRE in a channel ring has a set of flags. One flag indicates whether
74 * the completion of the transfer operation generates an entry (and possibly
75 * an interrupt) in the channel's event ring. Other flags allow transfer
76 * elements to be chained together, forming a single logical transaction.
77 * TRE flags are used to control whether and when interrupts are generated
78 * to signal completion of channel transfers.
80 * Elements in channel and event rings are completed (or consumed) strictly
81 * in order. Completion of one entry implies the completion of all preceding
82 * entries. A single completion interrupt can therefore communicate the
83 * completion of many transfers.
85 * Note that all GSI registers are little-endian, which is the assumed
86 * endianness of I/O space accesses. The accessor functions perform byte
87 * swapping if needed (i.e., for a big endian CPU).
90 /* Delay period for interrupt moderation (in 32KHz IPA internal timer ticks) */
91 #define GSI_EVT_RING_INT_MODT (32 * 1) /* 1ms under 32KHz clock */
93 #define GSI_CMD_TIMEOUT 50 /* milliseconds */
95 #define GSI_CHANNEL_STOP_RETRIES 10
96 #define GSI_CHANNEL_MODEM_HALT_RETRIES 10
97 #define GSI_CHANNEL_MODEM_FLOW_RETRIES 5 /* disable flow control only */
99 #define GSI_MHI_EVENT_ID_START 10 /* 1st reserved event id */
100 #define GSI_MHI_EVENT_ID_END 16 /* Last reserved event id */
102 #define GSI_ISR_MAX_ITER 50 /* Detect interrupt storms */
104 /* An entry in an event ring */
115 /** gsi_channel_scratch_gpi - GPI protocol scratch register
116 * @max_outstanding_tre:
117 * Defines the maximum number of TREs allowed in a single transaction
118 * on a channel (in bytes). This determines the amount of prefetch
119 * performed by the hardware. We configure this to equal the size of
120 * the TLV FIFO for the channel.
121 * @outstanding_threshold:
122 * Defines the threshold (in bytes) determining when the sequencer
123 * should update the channel doorbell. We configure this to equal
124 * the size of two TREs.
126 struct gsi_channel_scratch_gpi {
129 u16 max_outstanding_tre;
131 u16 outstanding_threshold;
134 /** gsi_channel_scratch - channel scratch configuration area
136 * The exact interpretation of this register is protocol-specific.
137 * We only use GPI channels; see struct gsi_channel_scratch_gpi, above.
139 union gsi_channel_scratch {
140 struct gsi_channel_scratch_gpi gpi;
149 /* Check things that can be validated at build time. */
150 static void gsi_validate_build(void)
152 /* This is used as a divisor */
153 BUILD_BUG_ON(!GSI_RING_ELEMENT_SIZE);
155 /* Code assumes the size of channel and event ring element are
156 * the same (and fixed). Make sure the size of an event ring
157 * element is what's expected.
159 BUILD_BUG_ON(sizeof(struct gsi_event) != GSI_RING_ELEMENT_SIZE);
161 /* Hardware requires a 2^n ring size. We ensure the number of
162 * elements in an event ring is a power of 2 elsewhere; this
163 * ensure the elements themselves meet the requirement.
165 BUILD_BUG_ON(!is_power_of_2(GSI_RING_ELEMENT_SIZE));
168 /* Return the channel id associated with a given channel */
169 static u32 gsi_channel_id(struct gsi_channel *channel)
171 return channel - &channel->gsi->channel[0];
174 /* An initialized channel has a non-null GSI pointer */
175 static bool gsi_channel_initialized(struct gsi_channel *channel)
177 return !!channel->gsi;
180 /* Encode the channel protocol for the CH_C_CNTXT_0 register */
181 static u32 ch_c_cntxt_0_type_encode(enum ipa_version version,
182 const struct reg *reg,
183 enum gsi_channel_type type)
187 val = reg_encode(reg, CHTYPE_PROTOCOL, type);
188 if (version < IPA_VERSION_4_5 || version >= IPA_VERSION_5_0)
191 type >>= hweight32(reg_fmask(reg, CHTYPE_PROTOCOL));
193 return val | reg_encode(reg, CHTYPE_PROTOCOL_MSB, type);
196 /* Update the GSI IRQ type register with the cached value */
197 static void gsi_irq_type_update(struct gsi *gsi, u32 val)
199 const struct reg *reg = gsi_reg(gsi, CNTXT_TYPE_IRQ_MSK);
201 gsi->type_enabled_bitmap = val;
202 iowrite32(val, gsi->virt + reg_offset(reg));
205 static void gsi_irq_type_enable(struct gsi *gsi, enum gsi_irq_type_id type_id)
207 gsi_irq_type_update(gsi, gsi->type_enabled_bitmap | type_id);
210 static void gsi_irq_type_disable(struct gsi *gsi, enum gsi_irq_type_id type_id)
212 gsi_irq_type_update(gsi, gsi->type_enabled_bitmap & ~type_id);
215 /* Event ring commands are performed one at a time. Their completion
216 * is signaled by the event ring control GSI interrupt type, which is
217 * only enabled when we issue an event ring command. Only the event
218 * ring being operated on has this interrupt enabled.
220 static void gsi_irq_ev_ctrl_enable(struct gsi *gsi, u32 evt_ring_id)
222 u32 val = BIT(evt_ring_id);
223 const struct reg *reg;
225 /* There's a small chance that a previous command completed
226 * after the interrupt was disabled, so make sure we have no
227 * pending interrupts before we enable them.
229 reg = gsi_reg(gsi, CNTXT_SRC_EV_CH_IRQ_CLR);
230 iowrite32(~0, gsi->virt + reg_offset(reg));
232 reg = gsi_reg(gsi, CNTXT_SRC_EV_CH_IRQ_MSK);
233 iowrite32(val, gsi->virt + reg_offset(reg));
234 gsi_irq_type_enable(gsi, GSI_EV_CTRL);
237 /* Disable event ring control interrupts */
238 static void gsi_irq_ev_ctrl_disable(struct gsi *gsi)
240 const struct reg *reg;
242 gsi_irq_type_disable(gsi, GSI_EV_CTRL);
244 reg = gsi_reg(gsi, CNTXT_SRC_EV_CH_IRQ_MSK);
245 iowrite32(0, gsi->virt + reg_offset(reg));
248 /* Channel commands are performed one at a time. Their completion is
249 * signaled by the channel control GSI interrupt type, which is only
250 * enabled when we issue a channel command. Only the channel being
251 * operated on has this interrupt enabled.
253 static void gsi_irq_ch_ctrl_enable(struct gsi *gsi, u32 channel_id)
255 u32 val = BIT(channel_id);
256 const struct reg *reg;
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.
262 reg = gsi_reg(gsi, CNTXT_SRC_CH_IRQ_CLR);
263 iowrite32(~0, gsi->virt + reg_offset(reg));
265 reg = gsi_reg(gsi, CNTXT_SRC_CH_IRQ_MSK);
266 iowrite32(val, gsi->virt + reg_offset(reg));
268 gsi_irq_type_enable(gsi, GSI_CH_CTRL);
271 /* Disable channel control interrupts */
272 static void gsi_irq_ch_ctrl_disable(struct gsi *gsi)
274 const struct reg *reg;
276 gsi_irq_type_disable(gsi, GSI_CH_CTRL);
278 reg = gsi_reg(gsi, CNTXT_SRC_CH_IRQ_MSK);
279 iowrite32(0, gsi->virt + reg_offset(reg));
282 static void gsi_irq_ieob_enable_one(struct gsi *gsi, u32 evt_ring_id)
284 bool enable_ieob = !gsi->ieob_enabled_bitmap;
285 const struct reg *reg;
288 gsi->ieob_enabled_bitmap |= BIT(evt_ring_id);
290 reg = gsi_reg(gsi, CNTXT_SRC_IEOB_IRQ_MSK);
291 val = gsi->ieob_enabled_bitmap;
292 iowrite32(val, gsi->virt + reg_offset(reg));
294 /* Enable the interrupt type if this is the first channel enabled */
296 gsi_irq_type_enable(gsi, GSI_IEOB);
299 static void gsi_irq_ieob_disable(struct gsi *gsi, u32 event_mask)
301 const struct reg *reg;
304 gsi->ieob_enabled_bitmap &= ~event_mask;
306 /* Disable the interrupt type if this was the last enabled channel */
307 if (!gsi->ieob_enabled_bitmap)
308 gsi_irq_type_disable(gsi, GSI_IEOB);
310 reg = gsi_reg(gsi, CNTXT_SRC_IEOB_IRQ_MSK);
311 val = gsi->ieob_enabled_bitmap;
312 iowrite32(val, gsi->virt + reg_offset(reg));
315 static void gsi_irq_ieob_disable_one(struct gsi *gsi, u32 evt_ring_id)
317 gsi_irq_ieob_disable(gsi, BIT(evt_ring_id));
320 /* Enable all GSI_interrupt types */
321 static void gsi_irq_enable(struct gsi *gsi)
323 const struct reg *reg;
326 /* Global interrupts include hardware error reports. Enable
327 * that so we can at least report the error should it occur.
329 reg = gsi_reg(gsi, CNTXT_GLOB_IRQ_EN);
330 iowrite32(ERROR_INT, gsi->virt + reg_offset(reg));
332 gsi_irq_type_update(gsi, gsi->type_enabled_bitmap | GSI_GLOB_EE);
334 /* General GSI interrupts are reported to all EEs; if they occur
335 * they are unrecoverable (without reset). A breakpoint interrupt
336 * also exists, but we don't support that. We want to be notified
337 * of errors so we can report them, even if they can't be handled.
339 reg = gsi_reg(gsi, CNTXT_GSI_IRQ_EN);
341 val |= CMD_FIFO_OVRFLOW;
342 val |= MCS_STACK_OVRFLOW;
343 iowrite32(val, gsi->virt + reg_offset(reg));
345 gsi_irq_type_update(gsi, gsi->type_enabled_bitmap | GSI_GENERAL);
348 /* Disable all GSI interrupt types */
349 static void gsi_irq_disable(struct gsi *gsi)
351 const struct reg *reg;
353 gsi_irq_type_update(gsi, 0);
355 /* Clear the type-specific interrupt masks set by gsi_irq_enable() */
356 reg = gsi_reg(gsi, CNTXT_GSI_IRQ_EN);
357 iowrite32(0, gsi->virt + reg_offset(reg));
359 reg = gsi_reg(gsi, CNTXT_GLOB_IRQ_EN);
360 iowrite32(0, gsi->virt + reg_offset(reg));
363 /* Return the virtual address associated with a ring index */
364 void *gsi_ring_virt(struct gsi_ring *ring, u32 index)
366 /* Note: index *must* be used modulo the ring count here */
367 return ring->virt + (index % ring->count) * GSI_RING_ELEMENT_SIZE;
370 /* Return the 32-bit DMA address associated with a ring index */
371 static u32 gsi_ring_addr(struct gsi_ring *ring, u32 index)
373 return lower_32_bits(ring->addr) + index * GSI_RING_ELEMENT_SIZE;
376 /* Return the ring index of a 32-bit ring offset */
377 static u32 gsi_ring_index(struct gsi_ring *ring, u32 offset)
379 return (offset - gsi_ring_addr(ring, 0)) / GSI_RING_ELEMENT_SIZE;
382 /* Issue a GSI command by writing a value to a register, then wait for
383 * completion to be signaled. Returns true if the command completes
384 * or false if it times out.
386 static bool gsi_command(struct gsi *gsi, u32 reg, u32 val)
388 unsigned long timeout = msecs_to_jiffies(GSI_CMD_TIMEOUT);
389 struct completion *completion = &gsi->completion;
391 reinit_completion(completion);
393 iowrite32(val, gsi->virt + reg);
395 return !!wait_for_completion_timeout(completion, timeout);
398 /* Return the hardware's notion of the current state of an event ring */
399 static enum gsi_evt_ring_state
400 gsi_evt_ring_state(struct gsi *gsi, u32 evt_ring_id)
402 const struct reg *reg = gsi_reg(gsi, EV_CH_E_CNTXT_0);
405 val = ioread32(gsi->virt + reg_n_offset(reg, evt_ring_id));
407 return reg_decode(reg, EV_CHSTATE, val);
410 /* Issue an event ring command and wait for it to complete */
411 static void gsi_evt_ring_command(struct gsi *gsi, u32 evt_ring_id,
412 enum gsi_evt_cmd_opcode opcode)
414 struct device *dev = gsi->dev;
415 const struct reg *reg;
419 /* Enable the completion interrupt for the command */
420 gsi_irq_ev_ctrl_enable(gsi, evt_ring_id);
422 reg = gsi_reg(gsi, EV_CH_CMD);
423 val = reg_encode(reg, EV_CHID, evt_ring_id);
424 val |= reg_encode(reg, EV_OPCODE, opcode);
426 timeout = !gsi_command(gsi, reg_offset(reg), val);
428 gsi_irq_ev_ctrl_disable(gsi);
433 dev_err(dev, "GSI command %u for event ring %u timed out, state %u\n",
434 opcode, evt_ring_id, gsi_evt_ring_state(gsi, evt_ring_id));
437 /* Allocate an event ring in NOT_ALLOCATED state */
438 static int gsi_evt_ring_alloc_command(struct gsi *gsi, u32 evt_ring_id)
440 enum gsi_evt_ring_state state;
442 /* Get initial event ring state */
443 state = gsi_evt_ring_state(gsi, evt_ring_id);
444 if (state != GSI_EVT_RING_STATE_NOT_ALLOCATED) {
445 dev_err(gsi->dev, "event ring %u bad state %u before alloc\n",
450 gsi_evt_ring_command(gsi, evt_ring_id, GSI_EVT_ALLOCATE);
452 /* If successful the event ring state will have changed */
453 state = gsi_evt_ring_state(gsi, evt_ring_id);
454 if (state == GSI_EVT_RING_STATE_ALLOCATED)
457 dev_err(gsi->dev, "event ring %u bad state %u after alloc\n",
463 /* Reset a GSI event ring in ALLOCATED or ERROR state. */
464 static void gsi_evt_ring_reset_command(struct gsi *gsi, u32 evt_ring_id)
466 enum gsi_evt_ring_state state;
468 state = gsi_evt_ring_state(gsi, evt_ring_id);
469 if (state != GSI_EVT_RING_STATE_ALLOCATED &&
470 state != GSI_EVT_RING_STATE_ERROR) {
471 dev_err(gsi->dev, "event ring %u bad state %u before reset\n",
476 gsi_evt_ring_command(gsi, evt_ring_id, GSI_EVT_RESET);
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_ALLOCATED)
483 dev_err(gsi->dev, "event ring %u bad state %u after reset\n",
487 /* Issue a hardware de-allocation request for an allocated event ring */
488 static void gsi_evt_ring_de_alloc_command(struct gsi *gsi, u32 evt_ring_id)
490 enum gsi_evt_ring_state state;
492 state = gsi_evt_ring_state(gsi, evt_ring_id);
493 if (state != GSI_EVT_RING_STATE_ALLOCATED) {
494 dev_err(gsi->dev, "event ring %u state %u before dealloc\n",
499 gsi_evt_ring_command(gsi, evt_ring_id, GSI_EVT_DE_ALLOC);
501 /* If successful the event ring state will have changed */
502 state = gsi_evt_ring_state(gsi, evt_ring_id);
503 if (state == GSI_EVT_RING_STATE_NOT_ALLOCATED)
506 dev_err(gsi->dev, "event ring %u bad state %u after dealloc\n",
510 /* Fetch the current state of a channel from hardware */
511 static enum gsi_channel_state gsi_channel_state(struct gsi_channel *channel)
513 const struct reg *reg = gsi_reg(channel->gsi, CH_C_CNTXT_0);
514 u32 channel_id = gsi_channel_id(channel);
515 struct gsi *gsi = channel->gsi;
516 void __iomem *virt = gsi->virt;
519 reg = gsi_reg(gsi, CH_C_CNTXT_0);
520 val = ioread32(virt + reg_n_offset(reg, channel_id));
522 return reg_decode(reg, CHSTATE, val);
525 /* Issue a channel command and wait for it to complete */
527 gsi_channel_command(struct gsi_channel *channel, enum gsi_ch_cmd_opcode opcode)
529 u32 channel_id = gsi_channel_id(channel);
530 struct gsi *gsi = channel->gsi;
531 struct device *dev = gsi->dev;
532 const struct reg *reg;
536 /* Enable the completion interrupt for the command */
537 gsi_irq_ch_ctrl_enable(gsi, channel_id);
539 reg = gsi_reg(gsi, CH_CMD);
540 val = reg_encode(reg, CH_CHID, channel_id);
541 val |= reg_encode(reg, CH_OPCODE, opcode);
543 timeout = !gsi_command(gsi, reg_offset(reg), val);
545 gsi_irq_ch_ctrl_disable(gsi);
550 dev_err(dev, "GSI command %u for channel %u timed out, state %u\n",
551 opcode, channel_id, gsi_channel_state(channel));
554 /* Allocate GSI channel in NOT_ALLOCATED state */
555 static int gsi_channel_alloc_command(struct gsi *gsi, u32 channel_id)
557 struct gsi_channel *channel = &gsi->channel[channel_id];
558 struct device *dev = gsi->dev;
559 enum gsi_channel_state state;
561 /* Get initial channel state */
562 state = gsi_channel_state(channel);
563 if (state != GSI_CHANNEL_STATE_NOT_ALLOCATED) {
564 dev_err(dev, "channel %u bad state %u before alloc\n",
569 gsi_channel_command(channel, GSI_CH_ALLOCATE);
571 /* If successful the channel state will have changed */
572 state = gsi_channel_state(channel);
573 if (state == GSI_CHANNEL_STATE_ALLOCATED)
576 dev_err(dev, "channel %u bad state %u after alloc\n",
582 /* Start an ALLOCATED channel */
583 static int gsi_channel_start_command(struct gsi_channel *channel)
585 struct device *dev = channel->gsi->dev;
586 enum gsi_channel_state state;
588 state = gsi_channel_state(channel);
589 if (state != GSI_CHANNEL_STATE_ALLOCATED &&
590 state != GSI_CHANNEL_STATE_STOPPED) {
591 dev_err(dev, "channel %u bad state %u before start\n",
592 gsi_channel_id(channel), state);
596 gsi_channel_command(channel, GSI_CH_START);
598 /* If successful the channel state will have changed */
599 state = gsi_channel_state(channel);
600 if (state == GSI_CHANNEL_STATE_STARTED)
603 dev_err(dev, "channel %u bad state %u after start\n",
604 gsi_channel_id(channel), state);
609 /* Stop a GSI channel in STARTED state */
610 static int gsi_channel_stop_command(struct gsi_channel *channel)
612 struct device *dev = channel->gsi->dev;
613 enum gsi_channel_state state;
615 state = gsi_channel_state(channel);
617 /* Channel could have entered STOPPED state since last call
618 * if it timed out. If so, we're done.
620 if (state == GSI_CHANNEL_STATE_STOPPED)
623 if (state != GSI_CHANNEL_STATE_STARTED &&
624 state != GSI_CHANNEL_STATE_STOP_IN_PROC) {
625 dev_err(dev, "channel %u bad state %u before stop\n",
626 gsi_channel_id(channel), state);
630 gsi_channel_command(channel, GSI_CH_STOP);
632 /* If successful the channel state will have changed */
633 state = gsi_channel_state(channel);
634 if (state == GSI_CHANNEL_STATE_STOPPED)
637 /* We may have to try again if stop is in progress */
638 if (state == GSI_CHANNEL_STATE_STOP_IN_PROC)
641 dev_err(dev, "channel %u bad state %u after stop\n",
642 gsi_channel_id(channel), state);
647 /* Reset a GSI channel in ALLOCATED or ERROR state. */
648 static void gsi_channel_reset_command(struct gsi_channel *channel)
650 struct device *dev = channel->gsi->dev;
651 enum gsi_channel_state state;
653 /* A short delay is required before a RESET command */
654 usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
656 state = gsi_channel_state(channel);
657 if (state != GSI_CHANNEL_STATE_STOPPED &&
658 state != GSI_CHANNEL_STATE_ERROR) {
659 /* No need to reset a channel already in ALLOCATED state */
660 if (state != GSI_CHANNEL_STATE_ALLOCATED)
661 dev_err(dev, "channel %u bad state %u before reset\n",
662 gsi_channel_id(channel), state);
666 gsi_channel_command(channel, GSI_CH_RESET);
668 /* If successful the channel state will have changed */
669 state = gsi_channel_state(channel);
670 if (state != GSI_CHANNEL_STATE_ALLOCATED)
671 dev_err(dev, "channel %u bad state %u after reset\n",
672 gsi_channel_id(channel), state);
675 /* Deallocate an ALLOCATED GSI channel */
676 static void gsi_channel_de_alloc_command(struct gsi *gsi, u32 channel_id)
678 struct gsi_channel *channel = &gsi->channel[channel_id];
679 struct device *dev = gsi->dev;
680 enum gsi_channel_state state;
682 state = gsi_channel_state(channel);
683 if (state != GSI_CHANNEL_STATE_ALLOCATED) {
684 dev_err(dev, "channel %u bad state %u before dealloc\n",
689 gsi_channel_command(channel, GSI_CH_DE_ALLOC);
691 /* If successful the channel state will have changed */
692 state = gsi_channel_state(channel);
694 if (state != GSI_CHANNEL_STATE_NOT_ALLOCATED)
695 dev_err(dev, "channel %u bad state %u after dealloc\n",
699 /* Ring an event ring doorbell, reporting the last entry processed by the AP.
700 * The index argument (modulo the ring count) is the first unfilled entry, so
701 * we supply one less than that with the doorbell. Update the event ring
702 * index field with the value provided.
704 static void gsi_evt_ring_doorbell(struct gsi *gsi, u32 evt_ring_id, u32 index)
706 const struct reg *reg = gsi_reg(gsi, EV_CH_E_DOORBELL_0);
707 struct gsi_ring *ring = &gsi->evt_ring[evt_ring_id].ring;
710 ring->index = index; /* Next unused entry */
712 /* Note: index *must* be used modulo the ring count here */
713 val = gsi_ring_addr(ring, (index - 1) % ring->count);
714 iowrite32(val, gsi->virt + reg_n_offset(reg, evt_ring_id));
717 /* Program an event ring for use */
718 static void gsi_evt_ring_program(struct gsi *gsi, u32 evt_ring_id)
720 struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
721 struct gsi_ring *ring = &evt_ring->ring;
722 const struct reg *reg;
725 reg = gsi_reg(gsi, EV_CH_E_CNTXT_0);
726 /* We program all event rings as GPI type/protocol */
727 val = reg_encode(reg, EV_CHTYPE, GSI_CHANNEL_TYPE_GPI);
728 /* EV_EE field is 0 (GSI_EE_AP) */
729 val |= reg_bit(reg, EV_INTYPE);
730 val |= reg_encode(reg, EV_ELEMENT_SIZE, GSI_RING_ELEMENT_SIZE);
731 iowrite32(val, gsi->virt + reg_n_offset(reg, evt_ring_id));
733 reg = gsi_reg(gsi, EV_CH_E_CNTXT_1);
734 val = reg_encode(reg, R_LENGTH, ring->count * GSI_RING_ELEMENT_SIZE);
735 iowrite32(val, gsi->virt + reg_n_offset(reg, evt_ring_id));
737 /* The context 2 and 3 registers store the low-order and
738 * high-order 32 bits of the address of the event ring,
741 reg = gsi_reg(gsi, EV_CH_E_CNTXT_2);
742 val = lower_32_bits(ring->addr);
743 iowrite32(val, gsi->virt + reg_n_offset(reg, evt_ring_id));
745 reg = gsi_reg(gsi, EV_CH_E_CNTXT_3);
746 val = upper_32_bits(ring->addr);
747 iowrite32(val, gsi->virt + reg_n_offset(reg, evt_ring_id));
749 /* Enable interrupt moderation by setting the moderation delay */
750 reg = gsi_reg(gsi, EV_CH_E_CNTXT_8);
751 val = reg_encode(reg, EV_MODT, GSI_EVT_RING_INT_MODT);
752 val |= reg_encode(reg, EV_MODC, 1); /* comes from channel */
753 /* EV_MOD_CNT is 0 (no counter-based interrupt coalescing) */
754 iowrite32(val, gsi->virt + reg_n_offset(reg, evt_ring_id));
756 /* No MSI write data, and MSI high and low address is 0 */
757 reg = gsi_reg(gsi, EV_CH_E_CNTXT_9);
758 iowrite32(0, gsi->virt + reg_n_offset(reg, evt_ring_id));
760 reg = gsi_reg(gsi, EV_CH_E_CNTXT_10);
761 iowrite32(0, gsi->virt + reg_n_offset(reg, evt_ring_id));
763 reg = gsi_reg(gsi, EV_CH_E_CNTXT_11);
764 iowrite32(0, gsi->virt + reg_n_offset(reg, evt_ring_id));
766 /* We don't need to get event read pointer updates */
767 reg = gsi_reg(gsi, EV_CH_E_CNTXT_12);
768 iowrite32(0, gsi->virt + reg_n_offset(reg, evt_ring_id));
770 reg = gsi_reg(gsi, EV_CH_E_CNTXT_13);
771 iowrite32(0, gsi->virt + reg_n_offset(reg, evt_ring_id));
773 /* Finally, tell the hardware our "last processed" event (arbitrary) */
774 gsi_evt_ring_doorbell(gsi, evt_ring_id, ring->index);
777 /* Find the transaction whose completion indicates a channel is quiesced */
778 static struct gsi_trans *gsi_channel_trans_last(struct gsi_channel *channel)
780 struct gsi_trans_info *trans_info = &channel->trans_info;
781 u32 pending_id = trans_info->pending_id;
782 struct gsi_trans *trans;
785 if (channel->toward_ipa && pending_id != trans_info->free_id) {
786 /* There is a small chance a TX transaction got allocated
787 * just before we disabled transmits, so check for that.
788 * The last allocated, committed, or pending transaction
789 * precedes the first free transaction.
791 trans_id = trans_info->free_id - 1;
792 } else if (trans_info->polled_id != pending_id) {
793 /* Otherwise (TX or RX) we want to wait for anything that
794 * has completed, or has been polled but not released yet.
796 * The last completed or polled transaction precedes the
797 * first pending transaction.
799 trans_id = pending_id - 1;
804 /* Caller will wait for this, so take a reference */
805 trans = &trans_info->trans[trans_id % channel->tre_count];
806 refcount_inc(&trans->refcount);
811 /* Wait for transaction activity on a channel to complete */
812 static void gsi_channel_trans_quiesce(struct gsi_channel *channel)
814 struct gsi_trans *trans;
816 /* Get the last transaction, and wait for it to complete */
817 trans = gsi_channel_trans_last(channel);
819 wait_for_completion(&trans->completion);
820 gsi_trans_free(trans);
824 /* Program a channel for use; there is no gsi_channel_deprogram() */
825 static void gsi_channel_program(struct gsi_channel *channel, bool doorbell)
827 size_t size = channel->tre_ring.count * GSI_RING_ELEMENT_SIZE;
828 u32 channel_id = gsi_channel_id(channel);
829 union gsi_channel_scratch scr = { };
830 struct gsi_channel_scratch_gpi *gpi;
831 struct gsi *gsi = channel->gsi;
832 const struct reg *reg;
837 reg = gsi_reg(gsi, CH_C_CNTXT_0);
839 /* We program all channels as GPI type/protocol */
840 val = ch_c_cntxt_0_type_encode(gsi->version, reg, GSI_CHANNEL_TYPE_GPI);
841 if (channel->toward_ipa)
842 val |= reg_bit(reg, CHTYPE_DIR);
843 if (gsi->version < IPA_VERSION_5_0)
844 val |= reg_encode(reg, ERINDEX, channel->evt_ring_id);
845 val |= reg_encode(reg, ELEMENT_SIZE, GSI_RING_ELEMENT_SIZE);
846 iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
848 reg = gsi_reg(gsi, CH_C_CNTXT_1);
849 val = reg_encode(reg, CH_R_LENGTH, size);
850 if (gsi->version >= IPA_VERSION_5_0)
851 val |= reg_encode(reg, CH_ERINDEX, channel->evt_ring_id);
852 iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
854 /* The context 2 and 3 registers store the low-order and
855 * high-order 32 bits of the address of the channel ring,
858 reg = gsi_reg(gsi, CH_C_CNTXT_2);
859 val = lower_32_bits(channel->tre_ring.addr);
860 iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
862 reg = gsi_reg(gsi, CH_C_CNTXT_3);
863 val = upper_32_bits(channel->tre_ring.addr);
864 iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
866 reg = gsi_reg(gsi, CH_C_QOS);
868 /* Command channel gets low weighted round-robin priority */
869 if (channel->command)
870 wrr_weight = reg_field_max(reg, WRR_WEIGHT);
871 val = reg_encode(reg, WRR_WEIGHT, wrr_weight);
873 /* Max prefetch is 1 segment (do not set MAX_PREFETCH_FMASK) */
875 /* No need to use the doorbell engine starting at IPA v4.0 */
876 if (gsi->version < IPA_VERSION_4_0 && doorbell)
877 val |= reg_bit(reg, USE_DB_ENG);
879 /* v4.0 introduces an escape buffer for prefetch. We use it
880 * on all but the AP command channel.
882 if (gsi->version >= IPA_VERSION_4_0 && !channel->command) {
883 /* If not otherwise set, prefetch buffers are used */
884 if (gsi->version < IPA_VERSION_4_5)
885 val |= reg_bit(reg, USE_ESCAPE_BUF_ONLY);
887 val |= reg_encode(reg, PREFETCH_MODE, ESCAPE_BUF_ONLY);
889 /* All channels set DB_IN_BYTES */
890 if (gsi->version >= IPA_VERSION_4_9)
891 val |= reg_bit(reg, DB_IN_BYTES);
893 iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
895 /* Now update the scratch registers for GPI protocol */
897 gpi->max_outstanding_tre = channel->trans_tre_max *
898 GSI_RING_ELEMENT_SIZE;
899 gpi->outstanding_threshold = 2 * GSI_RING_ELEMENT_SIZE;
901 reg = gsi_reg(gsi, CH_C_SCRATCH_0);
902 val = scr.data.word1;
903 iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
905 reg = gsi_reg(gsi, CH_C_SCRATCH_1);
906 val = scr.data.word2;
907 iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
909 reg = gsi_reg(gsi, CH_C_SCRATCH_2);
910 val = scr.data.word3;
911 iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
913 /* We must preserve the upper 16 bits of the last scratch register.
914 * The next sequence assumes those bits remain unchanged between the
915 * read and the write.
917 reg = gsi_reg(gsi, CH_C_SCRATCH_3);
918 offset = reg_n_offset(reg, channel_id);
919 val = ioread32(gsi->virt + offset);
920 val = (scr.data.word4 & GENMASK(31, 16)) | (val & GENMASK(15, 0));
921 iowrite32(val, gsi->virt + offset);
926 static int __gsi_channel_start(struct gsi_channel *channel, bool resume)
928 struct gsi *gsi = channel->gsi;
931 /* Prior to IPA v4.0 suspend/resume is not implemented by GSI */
932 if (resume && gsi->version < IPA_VERSION_4_0)
935 mutex_lock(&gsi->mutex);
937 ret = gsi_channel_start_command(channel);
939 mutex_unlock(&gsi->mutex);
944 /* Start an allocated GSI channel */
945 int gsi_channel_start(struct gsi *gsi, u32 channel_id)
947 struct gsi_channel *channel = &gsi->channel[channel_id];
950 /* Enable NAPI and the completion interrupt */
951 napi_enable(&channel->napi);
952 gsi_irq_ieob_enable_one(gsi, channel->evt_ring_id);
954 ret = __gsi_channel_start(channel, false);
956 gsi_irq_ieob_disable_one(gsi, channel->evt_ring_id);
957 napi_disable(&channel->napi);
963 static int gsi_channel_stop_retry(struct gsi_channel *channel)
965 u32 retries = GSI_CHANNEL_STOP_RETRIES;
969 ret = gsi_channel_stop_command(channel);
972 usleep_range(3 * USEC_PER_MSEC, 5 * USEC_PER_MSEC);
978 static int __gsi_channel_stop(struct gsi_channel *channel, bool suspend)
980 struct gsi *gsi = channel->gsi;
983 /* Wait for any underway transactions to complete before stopping. */
984 gsi_channel_trans_quiesce(channel);
986 /* Prior to IPA v4.0 suspend/resume is not implemented by GSI */
987 if (suspend && gsi->version < IPA_VERSION_4_0)
990 mutex_lock(&gsi->mutex);
992 ret = gsi_channel_stop_retry(channel);
994 mutex_unlock(&gsi->mutex);
999 /* Stop a started channel */
1000 int gsi_channel_stop(struct gsi *gsi, u32 channel_id)
1002 struct gsi_channel *channel = &gsi->channel[channel_id];
1005 ret = __gsi_channel_stop(channel, false);
1009 /* Disable the completion interrupt and NAPI if successful */
1010 gsi_irq_ieob_disable_one(gsi, channel->evt_ring_id);
1011 napi_disable(&channel->napi);
1016 /* Reset and reconfigure a channel, (possibly) enabling the doorbell engine */
1017 void gsi_channel_reset(struct gsi *gsi, u32 channel_id, bool doorbell)
1019 struct gsi_channel *channel = &gsi->channel[channel_id];
1021 mutex_lock(&gsi->mutex);
1023 gsi_channel_reset_command(channel);
1024 /* Due to a hardware quirk we may need to reset RX channels twice. */
1025 if (gsi->version < IPA_VERSION_4_0 && !channel->toward_ipa)
1026 gsi_channel_reset_command(channel);
1028 /* Hardware assumes this is 0 following reset */
1029 channel->tre_ring.index = 0;
1030 gsi_channel_program(channel, doorbell);
1031 gsi_channel_trans_cancel_pending(channel);
1033 mutex_unlock(&gsi->mutex);
1036 /* Stop a started channel for suspend */
1037 int gsi_channel_suspend(struct gsi *gsi, u32 channel_id)
1039 struct gsi_channel *channel = &gsi->channel[channel_id];
1042 ret = __gsi_channel_stop(channel, true);
1046 /* Ensure NAPI polling has finished. */
1047 napi_synchronize(&channel->napi);
1052 /* Resume a suspended channel (starting if stopped) */
1053 int gsi_channel_resume(struct gsi *gsi, u32 channel_id)
1055 struct gsi_channel *channel = &gsi->channel[channel_id];
1057 return __gsi_channel_start(channel, true);
1060 /* Prevent all GSI interrupts while suspended */
1061 void gsi_suspend(struct gsi *gsi)
1063 disable_irq(gsi->irq);
1066 /* Allow all GSI interrupts again when resuming */
1067 void gsi_resume(struct gsi *gsi)
1069 enable_irq(gsi->irq);
1072 void gsi_trans_tx_committed(struct gsi_trans *trans)
1074 struct gsi_channel *channel = &trans->gsi->channel[trans->channel_id];
1076 channel->trans_count++;
1077 channel->byte_count += trans->len;
1079 trans->trans_count = channel->trans_count;
1080 trans->byte_count = channel->byte_count;
1083 void gsi_trans_tx_queued(struct gsi_trans *trans)
1085 u32 channel_id = trans->channel_id;
1086 struct gsi *gsi = trans->gsi;
1087 struct gsi_channel *channel;
1091 channel = &gsi->channel[channel_id];
1093 byte_count = channel->byte_count - channel->queued_byte_count;
1094 trans_count = channel->trans_count - channel->queued_trans_count;
1095 channel->queued_byte_count = channel->byte_count;
1096 channel->queued_trans_count = channel->trans_count;
1098 ipa_gsi_channel_tx_queued(gsi, channel_id, trans_count, byte_count);
1102 * gsi_trans_tx_completed() - Report completed TX transactions
1103 * @trans: TX channel transaction that has completed
1105 * Report that a transaction on a TX channel has completed. At the time a
1106 * transaction is committed, we record *in the transaction* its channel's
1107 * committed transaction and byte counts. Transactions are completed in
1108 * order, and the difference between the channel's byte/transaction count
1109 * when the transaction was committed and when it completes tells us
1110 * exactly how much data has been transferred while the transaction was
1113 * We report this information to the network stack, which uses it to manage
1114 * the rate at which data is sent to hardware.
1116 static void gsi_trans_tx_completed(struct gsi_trans *trans)
1118 u32 channel_id = trans->channel_id;
1119 struct gsi *gsi = trans->gsi;
1120 struct gsi_channel *channel;
1124 channel = &gsi->channel[channel_id];
1125 trans_count = trans->trans_count - channel->compl_trans_count;
1126 byte_count = trans->byte_count - channel->compl_byte_count;
1128 channel->compl_trans_count += trans_count;
1129 channel->compl_byte_count += byte_count;
1131 ipa_gsi_channel_tx_completed(gsi, channel_id, trans_count, byte_count);
1134 /* Channel control interrupt handler */
1135 static void gsi_isr_chan_ctrl(struct gsi *gsi)
1137 const struct reg *reg;
1140 reg = gsi_reg(gsi, CNTXT_SRC_CH_IRQ);
1141 channel_mask = ioread32(gsi->virt + reg_offset(reg));
1143 reg = gsi_reg(gsi, CNTXT_SRC_CH_IRQ_CLR);
1144 iowrite32(channel_mask, gsi->virt + reg_offset(reg));
1146 while (channel_mask) {
1147 u32 channel_id = __ffs(channel_mask);
1149 channel_mask ^= BIT(channel_id);
1151 complete(&gsi->completion);
1155 /* Event ring control interrupt handler */
1156 static void gsi_isr_evt_ctrl(struct gsi *gsi)
1158 const struct reg *reg;
1161 reg = gsi_reg(gsi, CNTXT_SRC_EV_CH_IRQ);
1162 event_mask = ioread32(gsi->virt + reg_offset(reg));
1164 reg = gsi_reg(gsi, CNTXT_SRC_EV_CH_IRQ_CLR);
1165 iowrite32(event_mask, gsi->virt + reg_offset(reg));
1167 while (event_mask) {
1168 u32 evt_ring_id = __ffs(event_mask);
1170 event_mask ^= BIT(evt_ring_id);
1172 complete(&gsi->completion);
1176 /* Global channel error interrupt handler */
1178 gsi_isr_glob_chan_err(struct gsi *gsi, u32 err_ee, u32 channel_id, u32 code)
1180 if (code == GSI_OUT_OF_RESOURCES) {
1181 dev_err(gsi->dev, "channel %u out of resources\n", channel_id);
1182 complete(&gsi->completion);
1186 /* Report, but otherwise ignore all other error codes */
1187 dev_err(gsi->dev, "channel %u global error ee 0x%08x code 0x%08x\n",
1188 channel_id, err_ee, code);
1191 /* Global event error interrupt handler */
1193 gsi_isr_glob_evt_err(struct gsi *gsi, u32 err_ee, u32 evt_ring_id, u32 code)
1195 if (code == GSI_OUT_OF_RESOURCES) {
1196 struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
1197 u32 channel_id = gsi_channel_id(evt_ring->channel);
1199 complete(&gsi->completion);
1200 dev_err(gsi->dev, "evt_ring for channel %u out of resources\n",
1205 /* Report, but otherwise ignore all other error codes */
1206 dev_err(gsi->dev, "event ring %u global error ee %u code 0x%08x\n",
1207 evt_ring_id, err_ee, code);
1210 /* Global error interrupt handler */
1211 static void gsi_isr_glob_err(struct gsi *gsi)
1213 const struct reg *log_reg;
1214 const struct reg *clr_reg;
1215 enum gsi_err_type type;
1216 enum gsi_err_code code;
1222 /* Get the logged error, then reinitialize the log */
1223 log_reg = gsi_reg(gsi, ERROR_LOG);
1224 offset = reg_offset(log_reg);
1225 val = ioread32(gsi->virt + offset);
1226 iowrite32(0, gsi->virt + offset);
1228 clr_reg = gsi_reg(gsi, ERROR_LOG_CLR);
1229 iowrite32(~0, gsi->virt + reg_offset(clr_reg));
1231 /* Parse the error value */
1232 ee = reg_decode(log_reg, ERR_EE, val);
1233 type = reg_decode(log_reg, ERR_TYPE, val);
1234 which = reg_decode(log_reg, ERR_VIRT_IDX, val);
1235 code = reg_decode(log_reg, ERR_CODE, val);
1237 if (type == GSI_ERR_TYPE_CHAN)
1238 gsi_isr_glob_chan_err(gsi, ee, which, code);
1239 else if (type == GSI_ERR_TYPE_EVT)
1240 gsi_isr_glob_evt_err(gsi, ee, which, code);
1241 else /* type GSI_ERR_TYPE_GLOB should be fatal */
1242 dev_err(gsi->dev, "unexpected global error 0x%08x\n", type);
1245 /* Generic EE interrupt handler */
1246 static void gsi_isr_gp_int1(struct gsi *gsi)
1248 const struct reg *reg;
1252 /* This interrupt is used to handle completions of GENERIC GSI
1253 * commands. We use these to allocate and halt channels on the
1254 * modem's behalf due to a hardware quirk on IPA v4.2. The modem
1255 * "owns" channels even when the AP allocates them, and have no
1256 * way of knowing whether a modem channel's state has been changed.
1258 * We also use GENERIC commands to enable/disable channel flow
1259 * control for IPA v4.2+.
1261 * It is recommended that we halt the modem channels we allocated
1262 * when shutting down, but it's possible the channel isn't running
1263 * at the time we issue the HALT command. We'll get an error in
1264 * that case, but it's harmless (the channel is already halted).
1265 * Similarly, we could get an error back when updating flow control
1266 * on a channel because it's not in the proper state.
1268 * In either case, we silently ignore a INCORRECT_CHANNEL_STATE
1269 * error if we receive it.
1271 reg = gsi_reg(gsi, CNTXT_SCRATCH_0);
1272 val = ioread32(gsi->virt + reg_offset(reg));
1273 result = reg_decode(reg, GENERIC_EE_RESULT, val);
1276 case GENERIC_EE_SUCCESS:
1277 case GENERIC_EE_INCORRECT_CHANNEL_STATE:
1281 case GENERIC_EE_RETRY:
1282 gsi->result = -EAGAIN;
1286 dev_err(gsi->dev, "global INT1 generic result %u\n", result);
1291 complete(&gsi->completion);
1294 /* Inter-EE interrupt handler */
1295 static void gsi_isr_glob_ee(struct gsi *gsi)
1297 const struct reg *reg;
1300 reg = gsi_reg(gsi, CNTXT_GLOB_IRQ_STTS);
1301 val = ioread32(gsi->virt + reg_offset(reg));
1303 if (val & ERROR_INT)
1304 gsi_isr_glob_err(gsi);
1306 reg = gsi_reg(gsi, CNTXT_GLOB_IRQ_CLR);
1307 iowrite32(val, gsi->virt + reg_offset(reg));
1311 if (val & GP_INT1) {
1313 gsi_isr_gp_int1(gsi);
1317 dev_err(gsi->dev, "unexpected global interrupt 0x%08x\n", val);
1320 /* I/O completion interrupt event */
1321 static void gsi_isr_ieob(struct gsi *gsi)
1323 const struct reg *reg;
1326 reg = gsi_reg(gsi, CNTXT_SRC_IEOB_IRQ);
1327 event_mask = ioread32(gsi->virt + reg_offset(reg));
1329 gsi_irq_ieob_disable(gsi, event_mask);
1331 reg = gsi_reg(gsi, CNTXT_SRC_IEOB_IRQ_CLR);
1332 iowrite32(event_mask, gsi->virt + reg_offset(reg));
1334 while (event_mask) {
1335 u32 evt_ring_id = __ffs(event_mask);
1337 event_mask ^= BIT(evt_ring_id);
1339 napi_schedule(&gsi->evt_ring[evt_ring_id].channel->napi);
1343 /* General event interrupts represent serious problems, so report them */
1344 static void gsi_isr_general(struct gsi *gsi)
1346 struct device *dev = gsi->dev;
1347 const struct reg *reg;
1350 reg = gsi_reg(gsi, CNTXT_GSI_IRQ_STTS);
1351 val = ioread32(gsi->virt + reg_offset(reg));
1353 reg = gsi_reg(gsi, CNTXT_GSI_IRQ_CLR);
1354 iowrite32(val, gsi->virt + reg_offset(reg));
1356 dev_err(dev, "unexpected general interrupt 0x%08x\n", val);
1360 * gsi_isr() - Top level GSI interrupt service routine
1361 * @irq: Interrupt number (ignored)
1362 * @dev_id: GSI pointer supplied to request_irq()
1364 * This is the main handler function registered for the GSI IRQ. Each type
1365 * of interrupt has a separate handler function that is called from here.
1367 static irqreturn_t gsi_isr(int irq, void *dev_id)
1369 struct gsi *gsi = dev_id;
1370 const struct reg *reg;
1375 reg = gsi_reg(gsi, CNTXT_TYPE_IRQ);
1376 offset = reg_offset(reg);
1378 /* enum gsi_irq_type_id defines GSI interrupt types */
1379 while ((intr_mask = ioread32(gsi->virt + offset))) {
1380 /* intr_mask contains bitmask of pending GSI interrupts */
1382 u32 gsi_intr = BIT(__ffs(intr_mask));
1384 intr_mask ^= gsi_intr;
1386 /* Note: the IRQ condition for each type is cleared
1387 * when the type-specific register is updated.
1391 gsi_isr_chan_ctrl(gsi);
1394 gsi_isr_evt_ctrl(gsi);
1397 gsi_isr_glob_ee(gsi);
1403 gsi_isr_general(gsi);
1407 "unrecognized interrupt type 0x%08x\n",
1411 } while (intr_mask);
1413 if (++cnt > GSI_ISR_MAX_ITER) {
1414 dev_err(gsi->dev, "interrupt flood\n");
1422 /* Init function for GSI IRQ lookup; there is no gsi_irq_exit() */
1423 static int gsi_irq_init(struct gsi *gsi, struct platform_device *pdev)
1427 ret = platform_get_irq_byname(pdev, "gsi");
1429 return ret ? : -EINVAL;
1436 /* Return the transaction associated with a transfer completion event */
1437 static struct gsi_trans *
1438 gsi_event_trans(struct gsi *gsi, struct gsi_event *event)
1440 u32 channel_id = event->chid;
1441 struct gsi_channel *channel;
1442 struct gsi_trans *trans;
1446 channel = &gsi->channel[channel_id];
1447 if (WARN(!channel->gsi, "event has bad channel %u\n", channel_id))
1450 /* Event xfer_ptr records the TRE it's associated with */
1451 tre_offset = lower_32_bits(le64_to_cpu(event->xfer_ptr));
1452 tre_index = gsi_ring_index(&channel->tre_ring, tre_offset);
1454 trans = gsi_channel_trans_mapped(channel, tre_index);
1456 if (WARN(!trans, "channel %u event with no transaction\n", channel_id))
1463 * gsi_evt_ring_update() - Update transaction state from hardware
1465 * @evt_ring_id: Event ring ID
1466 * @index: Event index in ring reported by hardware
1468 * Events for RX channels contain the actual number of bytes received into
1469 * the buffer. Every event has a transaction associated with it, and here
1470 * we update transactions to record their actual received lengths.
1472 * When an event for a TX channel arrives we use information in the
1473 * transaction to report the number of requests and bytes that have
1476 * This function is called whenever we learn that the GSI hardware has filled
1477 * new events since the last time we checked. The ring's index field tells
1478 * the first entry in need of processing. The index provided is the
1479 * first *unfilled* event in the ring (following the last filled one).
1481 * Events are sequential within the event ring, and transactions are
1482 * sequential within the transaction array.
1484 * Note that @index always refers to an element *within* the event ring.
1486 static void gsi_evt_ring_update(struct gsi *gsi, u32 evt_ring_id, u32 index)
1488 struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
1489 struct gsi_ring *ring = &evt_ring->ring;
1490 struct gsi_event *event_done;
1491 struct gsi_event *event;
1495 /* Starting with the oldest un-processed event, determine which
1496 * transaction (and which channel) is associated with the event.
1497 * For RX channels, update each completed transaction with the
1498 * number of bytes that were actually received. For TX channels
1499 * associated with a network device, report to the network stack
1500 * the number of transfers and bytes this completion represents.
1502 old_index = ring->index;
1503 event = gsi_ring_virt(ring, old_index);
1505 /* Compute the number of events to process before we wrap,
1506 * and determine when we'll be done processing events.
1508 event_avail = ring->count - old_index % ring->count;
1509 event_done = gsi_ring_virt(ring, index);
1511 struct gsi_trans *trans;
1513 trans = gsi_event_trans(gsi, event);
1517 if (trans->direction == DMA_FROM_DEVICE)
1518 trans->len = __le16_to_cpu(event->len);
1520 gsi_trans_tx_completed(trans);
1522 gsi_trans_move_complete(trans);
1524 /* Move on to the next event and transaction */
1528 event = gsi_ring_virt(ring, 0);
1529 } while (event != event_done);
1531 /* Tell the hardware we've handled these events */
1532 gsi_evt_ring_doorbell(gsi, evt_ring_id, index);
1535 /* Initialize a ring, including allocating DMA memory for its entries */
1536 static int gsi_ring_alloc(struct gsi *gsi, struct gsi_ring *ring, u32 count)
1538 u32 size = count * GSI_RING_ELEMENT_SIZE;
1539 struct device *dev = gsi->dev;
1542 /* Hardware requires a 2^n ring size, with alignment equal to size.
1543 * The DMA address returned by dma_alloc_coherent() is guaranteed to
1544 * be a power-of-2 number of pages, which satisfies the requirement.
1546 ring->virt = dma_alloc_coherent(dev, size, &addr, GFP_KERNEL);
1551 ring->count = count;
1557 /* Free a previously-allocated ring */
1558 static void gsi_ring_free(struct gsi *gsi, struct gsi_ring *ring)
1560 size_t size = ring->count * GSI_RING_ELEMENT_SIZE;
1562 dma_free_coherent(gsi->dev, size, ring->virt, ring->addr);
1565 /* Allocate an available event ring id */
1566 static int gsi_evt_ring_id_alloc(struct gsi *gsi)
1570 if (gsi->event_bitmap == ~0U) {
1571 dev_err(gsi->dev, "event rings exhausted\n");
1575 evt_ring_id = ffz(gsi->event_bitmap);
1576 gsi->event_bitmap |= BIT(evt_ring_id);
1578 return (int)evt_ring_id;
1581 /* Free a previously-allocated event ring id */
1582 static void gsi_evt_ring_id_free(struct gsi *gsi, u32 evt_ring_id)
1584 gsi->event_bitmap &= ~BIT(evt_ring_id);
1587 /* Ring a channel doorbell, reporting the first un-filled entry */
1588 void gsi_channel_doorbell(struct gsi_channel *channel)
1590 struct gsi_ring *tre_ring = &channel->tre_ring;
1591 u32 channel_id = gsi_channel_id(channel);
1592 struct gsi *gsi = channel->gsi;
1593 const struct reg *reg;
1596 reg = gsi_reg(gsi, CH_C_DOORBELL_0);
1597 /* Note: index *must* be used modulo the ring count here */
1598 val = gsi_ring_addr(tre_ring, tre_ring->index % tre_ring->count);
1599 iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
1602 /* Consult hardware, move newly completed transactions to completed state */
1603 void gsi_channel_update(struct gsi_channel *channel)
1605 u32 evt_ring_id = channel->evt_ring_id;
1606 struct gsi *gsi = channel->gsi;
1607 struct gsi_evt_ring *evt_ring;
1608 struct gsi_trans *trans;
1609 struct gsi_ring *ring;
1610 const struct reg *reg;
1614 evt_ring = &gsi->evt_ring[evt_ring_id];
1615 ring = &evt_ring->ring;
1617 /* See if there's anything new to process; if not, we're done. Note
1618 * that index always refers to an entry *within* the event ring.
1620 reg = gsi_reg(gsi, EV_CH_E_CNTXT_4);
1621 offset = reg_n_offset(reg, evt_ring_id);
1622 index = gsi_ring_index(ring, ioread32(gsi->virt + offset));
1623 if (index == ring->index % ring->count)
1626 /* Get the transaction for the latest completed event. */
1627 trans = gsi_event_trans(gsi, gsi_ring_virt(ring, index - 1));
1631 /* For RX channels, update each completed transaction with the number
1632 * of bytes that were actually received. For TX channels, report
1633 * the number of transactions and bytes this completion represents
1634 * up the network stack.
1636 gsi_evt_ring_update(gsi, evt_ring_id, index);
1640 * gsi_channel_poll_one() - Return a single completed transaction on a channel
1641 * @channel: Channel to be polled
1643 * Return: Transaction pointer, or null if none are available
1645 * This function returns the first of a channel's completed transactions.
1646 * If no transactions are in completed state, the hardware is consulted to
1647 * determine whether any new transactions have completed. If so, they're
1648 * moved to completed state and the first such transaction is returned.
1649 * If there are no more completed transactions, a null pointer is returned.
1651 static struct gsi_trans *gsi_channel_poll_one(struct gsi_channel *channel)
1653 struct gsi_trans *trans;
1655 /* Get the first completed transaction */
1656 trans = gsi_channel_trans_complete(channel);
1658 gsi_trans_move_polled(trans);
1664 * gsi_channel_poll() - NAPI poll function for a channel
1665 * @napi: NAPI structure for the channel
1666 * @budget: Budget supplied by NAPI core
1668 * Return: Number of items polled (<= budget)
1670 * Single transactions completed by hardware are polled until either
1671 * the budget is exhausted, or there are no more. Each transaction
1672 * polled is passed to gsi_trans_complete(), to perform remaining
1673 * completion processing and retire/free the transaction.
1675 static int gsi_channel_poll(struct napi_struct *napi, int budget)
1677 struct gsi_channel *channel;
1680 channel = container_of(napi, struct gsi_channel, napi);
1681 for (count = 0; count < budget; count++) {
1682 struct gsi_trans *trans;
1684 trans = gsi_channel_poll_one(channel);
1687 gsi_trans_complete(trans);
1690 if (count < budget && napi_complete(napi))
1691 gsi_irq_ieob_enable_one(channel->gsi, channel->evt_ring_id);
1696 /* The event bitmap represents which event ids are available for allocation.
1697 * Set bits are not available, clear bits can be used. This function
1698 * initializes the map so all events supported by the hardware are available,
1699 * then precludes any reserved events from being allocated.
1701 static u32 gsi_event_bitmap_init(u32 evt_ring_max)
1703 u32 event_bitmap = GENMASK(BITS_PER_LONG - 1, evt_ring_max);
1705 event_bitmap |= GENMASK(GSI_MHI_EVENT_ID_END, GSI_MHI_EVENT_ID_START);
1707 return event_bitmap;
1710 /* Setup function for a single channel */
1711 static int gsi_channel_setup_one(struct gsi *gsi, u32 channel_id)
1713 struct gsi_channel *channel = &gsi->channel[channel_id];
1714 u32 evt_ring_id = channel->evt_ring_id;
1717 if (!gsi_channel_initialized(channel))
1720 ret = gsi_evt_ring_alloc_command(gsi, evt_ring_id);
1724 gsi_evt_ring_program(gsi, evt_ring_id);
1726 ret = gsi_channel_alloc_command(gsi, channel_id);
1728 goto err_evt_ring_de_alloc;
1730 gsi_channel_program(channel, true);
1732 if (channel->toward_ipa)
1733 netif_napi_add_tx(&gsi->dummy_dev, &channel->napi,
1736 netif_napi_add(&gsi->dummy_dev, &channel->napi,
1741 err_evt_ring_de_alloc:
1742 /* We've done nothing with the event ring yet so don't reset */
1743 gsi_evt_ring_de_alloc_command(gsi, evt_ring_id);
1748 /* Inverse of gsi_channel_setup_one() */
1749 static void gsi_channel_teardown_one(struct gsi *gsi, u32 channel_id)
1751 struct gsi_channel *channel = &gsi->channel[channel_id];
1752 u32 evt_ring_id = channel->evt_ring_id;
1754 if (!gsi_channel_initialized(channel))
1757 netif_napi_del(&channel->napi);
1759 gsi_channel_de_alloc_command(gsi, channel_id);
1760 gsi_evt_ring_reset_command(gsi, evt_ring_id);
1761 gsi_evt_ring_de_alloc_command(gsi, evt_ring_id);
1764 /* We use generic commands only to operate on modem channels. We don't have
1765 * the ability to determine channel state for a modem channel, so we simply
1766 * issue the command and wait for it to complete.
1768 static int gsi_generic_command(struct gsi *gsi, u32 channel_id,
1769 enum gsi_generic_cmd_opcode opcode,
1772 const struct reg *reg;
1777 /* The error global interrupt type is always enabled (until we tear
1778 * down), so we will keep it enabled.
1780 * A generic EE command completes with a GSI global interrupt of
1781 * type GP_INT1. We only perform one generic command at a time
1782 * (to allocate, halt, or enable/disable flow control on a modem
1783 * channel), and only from this function. So we enable the GP_INT1
1784 * IRQ type here, and disable it again after the command completes.
1786 reg = gsi_reg(gsi, CNTXT_GLOB_IRQ_EN);
1787 val = ERROR_INT | GP_INT1;
1788 iowrite32(val, gsi->virt + reg_offset(reg));
1790 /* First zero the result code field */
1791 reg = gsi_reg(gsi, CNTXT_SCRATCH_0);
1792 offset = reg_offset(reg);
1793 val = ioread32(gsi->virt + offset);
1795 val &= ~reg_fmask(reg, GENERIC_EE_RESULT);
1796 iowrite32(val, gsi->virt + offset);
1798 /* Now issue the command */
1799 reg = gsi_reg(gsi, GENERIC_CMD);
1800 val = reg_encode(reg, GENERIC_OPCODE, opcode);
1801 val |= reg_encode(reg, GENERIC_CHID, channel_id);
1802 val |= reg_encode(reg, GENERIC_EE, GSI_EE_MODEM);
1803 if (gsi->version >= IPA_VERSION_4_11)
1804 val |= reg_encode(reg, GENERIC_PARAMS, params);
1806 timeout = !gsi_command(gsi, reg_offset(reg), val);
1808 /* Disable the GP_INT1 IRQ type again */
1809 reg = gsi_reg(gsi, CNTXT_GLOB_IRQ_EN);
1810 iowrite32(ERROR_INT, gsi->virt + reg_offset(reg));
1815 dev_err(gsi->dev, "GSI generic command %u to channel %u timed out\n",
1816 opcode, channel_id);
1821 static int gsi_modem_channel_alloc(struct gsi *gsi, u32 channel_id)
1823 return gsi_generic_command(gsi, channel_id,
1824 GSI_GENERIC_ALLOCATE_CHANNEL, 0);
1827 static void gsi_modem_channel_halt(struct gsi *gsi, u32 channel_id)
1829 u32 retries = GSI_CHANNEL_MODEM_HALT_RETRIES;
1833 ret = gsi_generic_command(gsi, channel_id,
1834 GSI_GENERIC_HALT_CHANNEL, 0);
1835 while (ret == -EAGAIN && retries--);
1838 dev_err(gsi->dev, "error %d halting modem channel %u\n",
1842 /* Enable or disable flow control for a modem GSI TX channel (IPA v4.2+) */
1844 gsi_modem_channel_flow_control(struct gsi *gsi, u32 channel_id, bool enable)
1850 command = enable ? GSI_GENERIC_ENABLE_FLOW_CONTROL
1851 : GSI_GENERIC_DISABLE_FLOW_CONTROL;
1852 /* Disabling flow control on IPA v4.11+ can return -EAGAIN if enable
1853 * is underway. In this case we need to retry the command.
1855 if (!enable && gsi->version >= IPA_VERSION_4_11)
1856 retries = GSI_CHANNEL_MODEM_FLOW_RETRIES;
1859 ret = gsi_generic_command(gsi, channel_id, command, 0);
1860 while (ret == -EAGAIN && retries--);
1864 "error %d %sabling mode channel %u flow control\n",
1865 ret, enable ? "en" : "dis", channel_id);
1868 /* Setup function for channels */
1869 static int gsi_channel_setup(struct gsi *gsi)
1875 gsi_irq_enable(gsi);
1877 mutex_lock(&gsi->mutex);
1880 ret = gsi_channel_setup_one(gsi, channel_id);
1883 } while (++channel_id < gsi->channel_count);
1885 /* Make sure no channels were defined that hardware does not support */
1886 while (channel_id < GSI_CHANNEL_COUNT_MAX) {
1887 struct gsi_channel *channel = &gsi->channel[channel_id++];
1889 if (!gsi_channel_initialized(channel))
1893 dev_err(gsi->dev, "channel %u not supported by hardware\n",
1895 channel_id = gsi->channel_count;
1899 /* Allocate modem channels if necessary */
1900 mask = gsi->modem_channel_bitmap;
1902 u32 modem_channel_id = __ffs(mask);
1904 ret = gsi_modem_channel_alloc(gsi, modem_channel_id);
1906 goto err_unwind_modem;
1908 /* Clear bit from mask only after success (for unwind) */
1909 mask ^= BIT(modem_channel_id);
1912 mutex_unlock(&gsi->mutex);
1917 /* Compute which modem channels need to be deallocated */
1918 mask ^= gsi->modem_channel_bitmap;
1920 channel_id = __fls(mask);
1922 mask ^= BIT(channel_id);
1924 gsi_modem_channel_halt(gsi, channel_id);
1928 while (channel_id--)
1929 gsi_channel_teardown_one(gsi, channel_id);
1931 mutex_unlock(&gsi->mutex);
1933 gsi_irq_disable(gsi);
1938 /* Inverse of gsi_channel_setup() */
1939 static void gsi_channel_teardown(struct gsi *gsi)
1941 u32 mask = gsi->modem_channel_bitmap;
1944 mutex_lock(&gsi->mutex);
1947 channel_id = __fls(mask);
1949 mask ^= BIT(channel_id);
1951 gsi_modem_channel_halt(gsi, channel_id);
1954 channel_id = gsi->channel_count - 1;
1956 gsi_channel_teardown_one(gsi, channel_id);
1957 while (channel_id--);
1959 mutex_unlock(&gsi->mutex);
1961 gsi_irq_disable(gsi);
1964 /* Turn off all GSI interrupts initially */
1965 static int gsi_irq_setup(struct gsi *gsi)
1967 const struct reg *reg;
1970 /* Writing 1 indicates IRQ interrupts; 0 would be MSI */
1971 reg = gsi_reg(gsi, CNTXT_INTSET);
1972 iowrite32(reg_bit(reg, INTYPE), gsi->virt + reg_offset(reg));
1974 /* Disable all interrupt types */
1975 gsi_irq_type_update(gsi, 0);
1977 /* Clear all type-specific interrupt masks */
1978 reg = gsi_reg(gsi, CNTXT_SRC_CH_IRQ_MSK);
1979 iowrite32(0, gsi->virt + reg_offset(reg));
1981 reg = gsi_reg(gsi, CNTXT_SRC_EV_CH_IRQ_MSK);
1982 iowrite32(0, gsi->virt + reg_offset(reg));
1984 reg = gsi_reg(gsi, CNTXT_GLOB_IRQ_EN);
1985 iowrite32(0, gsi->virt + reg_offset(reg));
1987 reg = gsi_reg(gsi, CNTXT_SRC_IEOB_IRQ_MSK);
1988 iowrite32(0, gsi->virt + reg_offset(reg));
1990 /* The inter-EE interrupts are not supported for IPA v3.0-v3.1 */
1991 if (gsi->version > IPA_VERSION_3_1) {
1992 reg = gsi_reg(gsi, INTER_EE_SRC_CH_IRQ_MSK);
1993 iowrite32(0, gsi->virt + reg_offset(reg));
1995 reg = gsi_reg(gsi, INTER_EE_SRC_EV_CH_IRQ_MSK);
1996 iowrite32(0, gsi->virt + reg_offset(reg));
1999 reg = gsi_reg(gsi, CNTXT_GSI_IRQ_EN);
2000 iowrite32(0, gsi->virt + reg_offset(reg));
2002 ret = request_irq(gsi->irq, gsi_isr, 0, "gsi", gsi);
2004 dev_err(gsi->dev, "error %d requesting \"gsi\" IRQ\n", ret);
2009 static void gsi_irq_teardown(struct gsi *gsi)
2011 free_irq(gsi->irq, gsi);
2014 /* Get # supported channel and event rings; there is no gsi_ring_teardown() */
2015 static int gsi_ring_setup(struct gsi *gsi)
2017 struct device *dev = gsi->dev;
2018 const struct reg *reg;
2022 if (gsi->version < IPA_VERSION_3_5_1) {
2023 /* No HW_PARAM_2 register prior to IPA v3.5.1, assume the max */
2024 gsi->channel_count = GSI_CHANNEL_COUNT_MAX;
2025 gsi->evt_ring_count = GSI_EVT_RING_COUNT_MAX;
2030 reg = gsi_reg(gsi, HW_PARAM_2);
2031 val = ioread32(gsi->virt + reg_offset(reg));
2033 count = reg_decode(reg, NUM_CH_PER_EE, val);
2035 dev_err(dev, "GSI reports zero channels supported\n");
2038 if (count > GSI_CHANNEL_COUNT_MAX) {
2039 dev_warn(dev, "limiting to %u channels; hardware supports %u\n",
2040 GSI_CHANNEL_COUNT_MAX, count);
2041 count = GSI_CHANNEL_COUNT_MAX;
2043 gsi->channel_count = count;
2045 if (gsi->version < IPA_VERSION_5_0) {
2046 count = reg_decode(reg, NUM_EV_PER_EE, val);
2048 reg = gsi_reg(gsi, HW_PARAM_4);
2049 count = reg_decode(reg, EV_PER_EE, val);
2052 dev_err(dev, "GSI reports zero event rings supported\n");
2055 if (count > GSI_EVT_RING_COUNT_MAX) {
2057 "limiting to %u event rings; hardware supports %u\n",
2058 GSI_EVT_RING_COUNT_MAX, count);
2059 count = GSI_EVT_RING_COUNT_MAX;
2061 gsi->evt_ring_count = count;
2066 /* Setup function for GSI. GSI firmware must be loaded and initialized */
2067 int gsi_setup(struct gsi *gsi)
2069 const struct reg *reg;
2073 /* Here is where we first touch the GSI hardware */
2074 reg = gsi_reg(gsi, GSI_STATUS);
2075 val = ioread32(gsi->virt + reg_offset(reg));
2076 if (!(val & reg_bit(reg, ENABLED))) {
2077 dev_err(gsi->dev, "GSI has not been enabled\n");
2081 ret = gsi_irq_setup(gsi);
2085 ret = gsi_ring_setup(gsi); /* No matching teardown required */
2087 goto err_irq_teardown;
2089 /* Initialize the error log */
2090 reg = gsi_reg(gsi, ERROR_LOG);
2091 iowrite32(0, gsi->virt + reg_offset(reg));
2093 ret = gsi_channel_setup(gsi);
2095 goto err_irq_teardown;
2100 gsi_irq_teardown(gsi);
2105 /* Inverse of gsi_setup() */
2106 void gsi_teardown(struct gsi *gsi)
2108 gsi_channel_teardown(gsi);
2109 gsi_irq_teardown(gsi);
2112 /* Initialize a channel's event ring */
2113 static int gsi_channel_evt_ring_init(struct gsi_channel *channel)
2115 struct gsi *gsi = channel->gsi;
2116 struct gsi_evt_ring *evt_ring;
2119 ret = gsi_evt_ring_id_alloc(gsi);
2122 channel->evt_ring_id = ret;
2124 evt_ring = &gsi->evt_ring[channel->evt_ring_id];
2125 evt_ring->channel = channel;
2127 ret = gsi_ring_alloc(gsi, &evt_ring->ring, channel->event_count);
2129 return 0; /* Success! */
2131 dev_err(gsi->dev, "error %d allocating channel %u event ring\n",
2132 ret, gsi_channel_id(channel));
2134 gsi_evt_ring_id_free(gsi, channel->evt_ring_id);
2139 /* Inverse of gsi_channel_evt_ring_init() */
2140 static void gsi_channel_evt_ring_exit(struct gsi_channel *channel)
2142 u32 evt_ring_id = channel->evt_ring_id;
2143 struct gsi *gsi = channel->gsi;
2144 struct gsi_evt_ring *evt_ring;
2146 evt_ring = &gsi->evt_ring[evt_ring_id];
2147 gsi_ring_free(gsi, &evt_ring->ring);
2148 gsi_evt_ring_id_free(gsi, evt_ring_id);
2151 static bool gsi_channel_data_valid(struct gsi *gsi, bool command,
2152 const struct ipa_gsi_endpoint_data *data)
2154 const struct gsi_channel_data *channel_data;
2155 u32 channel_id = data->channel_id;
2156 struct device *dev = gsi->dev;
2158 /* Make sure channel ids are in the range driver supports */
2159 if (channel_id >= GSI_CHANNEL_COUNT_MAX) {
2160 dev_err(dev, "bad channel id %u; must be less than %u\n",
2161 channel_id, GSI_CHANNEL_COUNT_MAX);
2165 if (data->ee_id != GSI_EE_AP && data->ee_id != GSI_EE_MODEM) {
2166 dev_err(dev, "bad EE id %u; not AP or modem\n", data->ee_id);
2170 if (command && !data->toward_ipa) {
2171 dev_err(dev, "command channel %u is not TX\n", channel_id);
2175 channel_data = &data->channel;
2177 if (!channel_data->tlv_count ||
2178 channel_data->tlv_count > GSI_TLV_MAX) {
2179 dev_err(dev, "channel %u bad tlv_count %u; must be 1..%u\n",
2180 channel_id, channel_data->tlv_count, GSI_TLV_MAX);
2184 if (command && IPA_COMMAND_TRANS_TRE_MAX > channel_data->tlv_count) {
2185 dev_err(dev, "command TRE max too big for channel %u (%u > %u)\n",
2186 channel_id, IPA_COMMAND_TRANS_TRE_MAX,
2187 channel_data->tlv_count);
2191 /* We have to allow at least one maximally-sized transaction to
2192 * be outstanding (which would use tlv_count TREs). Given how
2193 * gsi_channel_tre_max() is computed, tre_count has to be almost
2194 * twice the TLV FIFO size to satisfy this requirement.
2196 if (channel_data->tre_count < 2 * channel_data->tlv_count - 1) {
2197 dev_err(dev, "channel %u TLV count %u exceeds TRE count %u\n",
2198 channel_id, channel_data->tlv_count,
2199 channel_data->tre_count);
2203 if (!is_power_of_2(channel_data->tre_count)) {
2204 dev_err(dev, "channel %u bad tre_count %u; not power of 2\n",
2205 channel_id, channel_data->tre_count);
2209 if (!is_power_of_2(channel_data->event_count)) {
2210 dev_err(dev, "channel %u bad event_count %u; not power of 2\n",
2211 channel_id, channel_data->event_count);
2218 /* Init function for a single channel */
2219 static int gsi_channel_init_one(struct gsi *gsi,
2220 const struct ipa_gsi_endpoint_data *data,
2223 struct gsi_channel *channel;
2227 if (!gsi_channel_data_valid(gsi, command, data))
2230 /* Worst case we need an event for every outstanding TRE */
2231 if (data->channel.tre_count > data->channel.event_count) {
2232 tre_count = data->channel.event_count;
2233 dev_warn(gsi->dev, "channel %u limited to %u TREs\n",
2234 data->channel_id, tre_count);
2236 tre_count = data->channel.tre_count;
2239 channel = &gsi->channel[data->channel_id];
2240 memset(channel, 0, sizeof(*channel));
2243 channel->toward_ipa = data->toward_ipa;
2244 channel->command = command;
2245 channel->trans_tre_max = data->channel.tlv_count;
2246 channel->tre_count = tre_count;
2247 channel->event_count = data->channel.event_count;
2249 ret = gsi_channel_evt_ring_init(channel);
2253 ret = gsi_ring_alloc(gsi, &channel->tre_ring, data->channel.tre_count);
2255 dev_err(gsi->dev, "error %d allocating channel %u ring\n",
2256 ret, data->channel_id);
2257 goto err_channel_evt_ring_exit;
2260 ret = gsi_channel_trans_init(gsi, data->channel_id);
2265 u32 tre_max = gsi_channel_tre_max(gsi, data->channel_id);
2267 ret = ipa_cmd_pool_init(channel, tre_max);
2270 return 0; /* Success! */
2272 gsi_channel_trans_exit(channel);
2274 gsi_ring_free(gsi, &channel->tre_ring);
2275 err_channel_evt_ring_exit:
2276 gsi_channel_evt_ring_exit(channel);
2278 channel->gsi = NULL; /* Mark it not (fully) initialized */
2283 /* Inverse of gsi_channel_init_one() */
2284 static void gsi_channel_exit_one(struct gsi_channel *channel)
2286 if (!gsi_channel_initialized(channel))
2289 if (channel->command)
2290 ipa_cmd_pool_exit(channel);
2291 gsi_channel_trans_exit(channel);
2292 gsi_ring_free(channel->gsi, &channel->tre_ring);
2293 gsi_channel_evt_ring_exit(channel);
2296 /* Init function for channels */
2297 static int gsi_channel_init(struct gsi *gsi, u32 count,
2298 const struct ipa_gsi_endpoint_data *data)
2304 /* IPA v4.2 requires the AP to allocate channels for the modem */
2305 modem_alloc = gsi->version == IPA_VERSION_4_2;
2307 gsi->event_bitmap = gsi_event_bitmap_init(GSI_EVT_RING_COUNT_MAX);
2308 gsi->ieob_enabled_bitmap = 0;
2310 /* The endpoint data array is indexed by endpoint name */
2311 for (i = 0; i < count; i++) {
2312 bool command = i == IPA_ENDPOINT_AP_COMMAND_TX;
2314 if (ipa_gsi_endpoint_data_empty(&data[i]))
2315 continue; /* Skip over empty slots */
2317 /* Mark modem channels to be allocated (hardware workaround) */
2318 if (data[i].ee_id == GSI_EE_MODEM) {
2320 gsi->modem_channel_bitmap |=
2321 BIT(data[i].channel_id);
2325 ret = gsi_channel_init_one(gsi, &data[i], command);
2334 if (ipa_gsi_endpoint_data_empty(&data[i]))
2336 if (modem_alloc && data[i].ee_id == GSI_EE_MODEM) {
2337 gsi->modem_channel_bitmap &= ~BIT(data[i].channel_id);
2340 gsi_channel_exit_one(&gsi->channel[data->channel_id]);
2346 /* Inverse of gsi_channel_init() */
2347 static void gsi_channel_exit(struct gsi *gsi)
2349 u32 channel_id = GSI_CHANNEL_COUNT_MAX - 1;
2352 gsi_channel_exit_one(&gsi->channel[channel_id]);
2353 while (channel_id--);
2354 gsi->modem_channel_bitmap = 0;
2357 /* Init function for GSI. GSI hardware does not need to be "ready" */
2358 int gsi_init(struct gsi *gsi, struct platform_device *pdev,
2359 enum ipa_version version, u32 count,
2360 const struct ipa_gsi_endpoint_data *data)
2364 gsi_validate_build();
2366 gsi->dev = &pdev->dev;
2367 gsi->version = version;
2369 /* GSI uses NAPI on all channels. Create a dummy network device
2370 * for the channel NAPI contexts to be associated with.
2372 init_dummy_netdev(&gsi->dummy_dev);
2373 init_completion(&gsi->completion);
2375 ret = gsi_reg_init(gsi, pdev);
2379 ret = gsi_irq_init(gsi, pdev); /* No matching exit required */
2383 ret = gsi_channel_init(gsi, count, data);
2387 mutex_init(&gsi->mutex);
2397 /* Inverse of gsi_init() */
2398 void gsi_exit(struct gsi *gsi)
2400 mutex_destroy(&gsi->mutex);
2401 gsi_channel_exit(gsi);
2405 /* The maximum number of outstanding TREs on a channel. This limits
2406 * a channel's maximum number of transactions outstanding (worst case
2407 * is one TRE per transaction).
2409 * The absolute limit is the number of TREs in the channel's TRE ring,
2410 * and in theory we should be able use all of them. But in practice,
2411 * doing that led to the hardware reporting exhaustion of event ring
2412 * slots for writing completion information. So the hardware limit
2413 * would be (tre_count - 1).
2415 * We reduce it a bit further though. Transaction resource pools are
2416 * sized to be a little larger than this maximum, to allow resource
2417 * allocations to always be contiguous. The number of entries in a
2418 * TRE ring buffer is a power of 2, and the extra resources in a pool
2419 * tends to nearly double the memory allocated for it. Reducing the
2420 * maximum number of outstanding TREs allows the number of entries in
2421 * a pool to avoid crossing that power-of-2 boundary, and this can
2422 * substantially reduce pool memory requirements. The number we
2423 * reduce it by matches the number added in gsi_trans_pool_init().
2425 u32 gsi_channel_tre_max(struct gsi *gsi, u32 channel_id)
2427 struct gsi_channel *channel = &gsi->channel[channel_id];
2429 /* Hardware limit is channel->tre_count - 1 */
2430 return channel->tre_count - (channel->trans_tre_max - 1);