1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2018-2024 Linaro Ltd.
7 #include <linux/bits.h>
9 #include <linux/completion.h>
10 #include <linux/interrupt.h>
11 #include <linux/mutex.h>
12 #include <linux/netdevice.h>
13 #include <linux/platform_device.h>
14 #include <linux/types.h>
17 #include "gsi_private.h"
19 #include "gsi_trans.h"
22 #include "ipa_version.h"
26 * DOC: The IPA Generic Software Interface
28 * The generic software interface (GSI) is an integral component of the IPA,
29 * providing a well-defined communication layer between the AP subsystem
30 * and the IPA core. The modem uses the GSI layer as well.
34 * | AP +<---. .----+ Modem |
37 * -------- | | | | ---------
47 * In the above diagram, the AP and Modem represent "execution environments"
48 * (EEs), which are independent operating environments that use the IPA for
51 * Each EE uses a set of unidirectional GSI "channels," which allow transfer
52 * of data to or from the IPA. A channel is implemented as a ring buffer,
53 * with a DRAM-resident array of "transfer elements" (TREs) available to
54 * describe transfers to or from other EEs through the IPA. A transfer
55 * element can also contain an immediate command, requesting the IPA perform
56 * actions other than data transfer.
58 * Each TRE refers to a block of data--also located in DRAM. After writing
59 * one or more TREs to a channel, the writer (either the IPA or an EE) writes
60 * a doorbell register to inform the receiving side how many elements have
63 * Each channel has a GSI "event ring" associated with it. An event ring
64 * is implemented very much like a channel ring, but is always directed from
65 * the IPA to an EE. The IPA notifies an EE (such as the AP) about channel
66 * events by adding an entry to the event ring associated with the channel.
67 * The GSI then writes its doorbell for the event ring, causing the target
68 * EE to be interrupted. Each entry in an event ring contains a pointer
69 * to the channel TRE whose completion the event represents.
71 * Each TRE in a channel ring has a set of flags. One flag indicates whether
72 * the completion of the transfer operation generates an entry (and possibly
73 * an interrupt) in the channel's event ring. Other flags allow transfer
74 * elements to be chained together, forming a single logical transaction.
75 * TRE flags are used to control whether and when interrupts are generated
76 * to signal completion of channel transfers.
78 * Elements in channel and event rings are completed (or consumed) strictly
79 * in order. Completion of one entry implies the completion of all preceding
80 * entries. A single completion interrupt can therefore communicate the
81 * completion of many transfers.
83 * Note that all GSI registers are little-endian, which is the assumed
84 * endianness of I/O space accesses. The accessor functions perform byte
85 * swapping if needed (i.e., for a big endian CPU).
88 /* Delay period for interrupt moderation (in 32KHz IPA internal timer ticks) */
89 #define GSI_EVT_RING_INT_MODT (32 * 1) /* 1ms under 32KHz clock */
91 #define GSI_CMD_TIMEOUT 50 /* milliseconds */
93 #define GSI_CHANNEL_STOP_RETRIES 10
94 #define GSI_CHANNEL_MODEM_HALT_RETRIES 10
95 #define GSI_CHANNEL_MODEM_FLOW_RETRIES 5 /* disable flow control only */
97 #define GSI_MHI_EVENT_ID_START 10 /* 1st reserved event id */
98 #define GSI_MHI_EVENT_ID_END 16 /* Last reserved event id */
100 #define GSI_ISR_MAX_ITER 50 /* Detect interrupt storms */
102 /* An entry in an event ring */
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.
124 struct gsi_channel_scratch_gpi
{
127 u16 max_outstanding_tre
;
129 u16 outstanding_threshold
;
132 /** gsi_channel_scratch - channel scratch configuration area
134 * The exact interpretation of this register is protocol-specific.
135 * We only use GPI channels; see struct gsi_channel_scratch_gpi, above.
137 union gsi_channel_scratch
{
138 struct gsi_channel_scratch_gpi gpi
;
147 /* Check things that can be validated at build time. */
148 static void gsi_validate_build(void)
150 /* This is used as a divisor */
151 BUILD_BUG_ON(!GSI_RING_ELEMENT_SIZE
);
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.
157 BUILD_BUG_ON(sizeof(struct gsi_event
) != GSI_RING_ELEMENT_SIZE
);
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.
163 BUILD_BUG_ON(!is_power_of_2(GSI_RING_ELEMENT_SIZE
));
166 /* Return the channel id associated with a given channel */
167 static u32
gsi_channel_id(struct gsi_channel
*channel
)
169 return channel
- &channel
->gsi
->channel
[0];
172 /* An initialized channel has a non-null GSI pointer */
173 static bool gsi_channel_initialized(struct gsi_channel
*channel
)
175 return !!channel
->gsi
;
178 /* Encode the channel protocol for the CH_C_CNTXT_0 register */
179 static u32
ch_c_cntxt_0_type_encode(enum ipa_version version
,
180 const struct reg
*reg
,
181 enum gsi_channel_type type
)
185 val
= reg_encode(reg
, CHTYPE_PROTOCOL
, type
);
186 if (version
< IPA_VERSION_4_5
|| version
>= IPA_VERSION_5_0
)
189 type
>>= hweight32(reg_fmask(reg
, CHTYPE_PROTOCOL
));
191 return val
| reg_encode(reg
, CHTYPE_PROTOCOL_MSB
, type
);
194 /* Update the GSI IRQ type register with the cached value */
195 static void gsi_irq_type_update(struct gsi
*gsi
, u32 val
)
197 const struct reg
*reg
= gsi_reg(gsi
, CNTXT_TYPE_IRQ_MSK
);
199 gsi
->type_enabled_bitmap
= val
;
200 iowrite32(val
, gsi
->virt
+ reg_offset(reg
));
203 static void gsi_irq_type_enable(struct gsi
*gsi
, enum gsi_irq_type_id type_id
)
205 gsi_irq_type_update(gsi
, gsi
->type_enabled_bitmap
| type_id
);
208 static void gsi_irq_type_disable(struct gsi
*gsi
, enum gsi_irq_type_id type_id
)
210 gsi_irq_type_update(gsi
, gsi
->type_enabled_bitmap
& ~type_id
);
213 /* Event ring commands are performed one at a time. Their completion
214 * is signaled by the event ring control GSI interrupt type, which is
215 * only enabled when we issue an event ring command. Only the event
216 * ring being operated on has this interrupt enabled.
218 static void gsi_irq_ev_ctrl_enable(struct gsi
*gsi
, u32 evt_ring_id
)
220 u32 val
= BIT(evt_ring_id
);
221 const struct reg
*reg
;
223 /* There's a small chance that a previous command completed
224 * after the interrupt was disabled, so make sure we have no
225 * pending interrupts before we enable them.
227 reg
= gsi_reg(gsi
, CNTXT_SRC_EV_CH_IRQ_CLR
);
228 iowrite32(~0, gsi
->virt
+ reg_offset(reg
));
230 reg
= gsi_reg(gsi
, CNTXT_SRC_EV_CH_IRQ_MSK
);
231 iowrite32(val
, gsi
->virt
+ reg_offset(reg
));
232 gsi_irq_type_enable(gsi
, GSI_EV_CTRL
);
235 /* Disable event ring control interrupts */
236 static void gsi_irq_ev_ctrl_disable(struct gsi
*gsi
)
238 const struct reg
*reg
;
240 gsi_irq_type_disable(gsi
, GSI_EV_CTRL
);
242 reg
= gsi_reg(gsi
, CNTXT_SRC_EV_CH_IRQ_MSK
);
243 iowrite32(0, gsi
->virt
+ reg_offset(reg
));
246 /* Channel commands are performed one at a time. Their completion is
247 * signaled by the channel control GSI interrupt type, which is only
248 * enabled when we issue a channel command. Only the channel being
249 * operated on has this interrupt enabled.
251 static void gsi_irq_ch_ctrl_enable(struct gsi
*gsi
, u32 channel_id
)
253 u32 val
= BIT(channel_id
);
254 const struct reg
*reg
;
256 /* There's a small chance that a previous command completed
257 * after the interrupt was disabled, so make sure we have no
258 * pending interrupts before we enable them.
260 reg
= gsi_reg(gsi
, CNTXT_SRC_CH_IRQ_CLR
);
261 iowrite32(~0, gsi
->virt
+ reg_offset(reg
));
263 reg
= gsi_reg(gsi
, CNTXT_SRC_CH_IRQ_MSK
);
264 iowrite32(val
, gsi
->virt
+ reg_offset(reg
));
266 gsi_irq_type_enable(gsi
, GSI_CH_CTRL
);
269 /* Disable channel control interrupts */
270 static void gsi_irq_ch_ctrl_disable(struct gsi
*gsi
)
272 const struct reg
*reg
;
274 gsi_irq_type_disable(gsi
, GSI_CH_CTRL
);
276 reg
= gsi_reg(gsi
, CNTXT_SRC_CH_IRQ_MSK
);
277 iowrite32(0, gsi
->virt
+ reg_offset(reg
));
280 static void gsi_irq_ieob_enable_one(struct gsi
*gsi
, u32 evt_ring_id
)
282 bool enable_ieob
= !gsi
->ieob_enabled_bitmap
;
283 const struct reg
*reg
;
286 gsi
->ieob_enabled_bitmap
|= BIT(evt_ring_id
);
288 reg
= gsi_reg(gsi
, CNTXT_SRC_IEOB_IRQ_MSK
);
289 val
= gsi
->ieob_enabled_bitmap
;
290 iowrite32(val
, gsi
->virt
+ reg_offset(reg
));
292 /* Enable the interrupt type if this is the first channel enabled */
294 gsi_irq_type_enable(gsi
, GSI_IEOB
);
297 static void gsi_irq_ieob_disable(struct gsi
*gsi
, u32 event_mask
)
299 const struct reg
*reg
;
302 gsi
->ieob_enabled_bitmap
&= ~event_mask
;
304 /* Disable the interrupt type if this was the last enabled channel */
305 if (!gsi
->ieob_enabled_bitmap
)
306 gsi_irq_type_disable(gsi
, GSI_IEOB
);
308 reg
= gsi_reg(gsi
, CNTXT_SRC_IEOB_IRQ_MSK
);
309 val
= gsi
->ieob_enabled_bitmap
;
310 iowrite32(val
, gsi
->virt
+ reg_offset(reg
));
313 static void gsi_irq_ieob_disable_one(struct gsi
*gsi
, u32 evt_ring_id
)
315 gsi_irq_ieob_disable(gsi
, BIT(evt_ring_id
));
318 /* Enable all GSI_interrupt types */
319 static void gsi_irq_enable(struct gsi
*gsi
)
321 const struct reg
*reg
;
324 /* Global interrupts include hardware error reports. Enable
325 * that so we can at least report the error should it occur.
327 reg
= gsi_reg(gsi
, CNTXT_GLOB_IRQ_EN
);
328 iowrite32(ERROR_INT
, gsi
->virt
+ reg_offset(reg
));
330 gsi_irq_type_update(gsi
, gsi
->type_enabled_bitmap
| GSI_GLOB_EE
);
332 /* General GSI interrupts are reported to all EEs; if they occur
333 * they are unrecoverable (without reset). A breakpoint interrupt
334 * also exists, but we don't support that. We want to be notified
335 * of errors so we can report them, even if they can't be handled.
337 reg
= gsi_reg(gsi
, CNTXT_GSI_IRQ_EN
);
339 val
|= CMD_FIFO_OVRFLOW
;
340 val
|= MCS_STACK_OVRFLOW
;
341 iowrite32(val
, gsi
->virt
+ reg_offset(reg
));
343 gsi_irq_type_update(gsi
, gsi
->type_enabled_bitmap
| GSI_GENERAL
);
346 /* Disable all GSI interrupt types */
347 static void gsi_irq_disable(struct gsi
*gsi
)
349 const struct reg
*reg
;
351 gsi_irq_type_update(gsi
, 0);
353 /* Clear the type-specific interrupt masks set by gsi_irq_enable() */
354 reg
= gsi_reg(gsi
, CNTXT_GSI_IRQ_EN
);
355 iowrite32(0, gsi
->virt
+ reg_offset(reg
));
357 reg
= gsi_reg(gsi
, CNTXT_GLOB_IRQ_EN
);
358 iowrite32(0, gsi
->virt
+ reg_offset(reg
));
361 /* Return the virtual address associated with a ring index */
362 void *gsi_ring_virt(struct gsi_ring
*ring
, u32 index
)
364 /* Note: index *must* be used modulo the ring count here */
365 return ring
->virt
+ (index
% ring
->count
) * GSI_RING_ELEMENT_SIZE
;
368 /* Return the 32-bit DMA address associated with a ring index */
369 static u32
gsi_ring_addr(struct gsi_ring
*ring
, u32 index
)
371 return lower_32_bits(ring
->addr
) + index
* GSI_RING_ELEMENT_SIZE
;
374 /* Return the ring index of a 32-bit ring offset */
375 static u32
gsi_ring_index(struct gsi_ring
*ring
, u32 offset
)
377 return (offset
- gsi_ring_addr(ring
, 0)) / GSI_RING_ELEMENT_SIZE
;
380 /* Issue a GSI command by writing a value to a register, then wait for
381 * completion to be signaled. Returns true if the command completes
382 * or false if it times out.
384 static bool gsi_command(struct gsi
*gsi
, u32 reg
, u32 val
)
386 unsigned long timeout
= msecs_to_jiffies(GSI_CMD_TIMEOUT
);
387 struct completion
*completion
= &gsi
->completion
;
389 reinit_completion(completion
);
391 iowrite32(val
, gsi
->virt
+ reg
);
393 return !!wait_for_completion_timeout(completion
, timeout
);
396 /* Return the hardware's notion of the current state of an event ring */
397 static enum gsi_evt_ring_state
398 gsi_evt_ring_state(struct gsi
*gsi
, u32 evt_ring_id
)
400 const struct reg
*reg
= gsi_reg(gsi
, EV_CH_E_CNTXT_0
);
403 val
= ioread32(gsi
->virt
+ reg_n_offset(reg
, evt_ring_id
));
405 return reg_decode(reg
, EV_CHSTATE
, val
);
408 /* Issue an event ring command and wait for it to complete */
409 static void gsi_evt_ring_command(struct gsi
*gsi
, u32 evt_ring_id
,
410 enum gsi_evt_cmd_opcode opcode
)
412 struct device
*dev
= gsi
->dev
;
413 const struct reg
*reg
;
417 /* Enable the completion interrupt for the command */
418 gsi_irq_ev_ctrl_enable(gsi
, evt_ring_id
);
420 reg
= gsi_reg(gsi
, EV_CH_CMD
);
421 val
= reg_encode(reg
, EV_CHID
, evt_ring_id
);
422 val
|= reg_encode(reg
, EV_OPCODE
, opcode
);
424 timeout
= !gsi_command(gsi
, reg_offset(reg
), val
);
426 gsi_irq_ev_ctrl_disable(gsi
);
431 dev_err(dev
, "GSI command %u for event ring %u timed out, state %u\n",
432 opcode
, evt_ring_id
, gsi_evt_ring_state(gsi
, evt_ring_id
));
435 /* Allocate an event ring in NOT_ALLOCATED state */
436 static int gsi_evt_ring_alloc_command(struct gsi
*gsi
, u32 evt_ring_id
)
438 enum gsi_evt_ring_state state
;
440 /* Get initial event ring state */
441 state
= gsi_evt_ring_state(gsi
, evt_ring_id
);
442 if (state
!= GSI_EVT_RING_STATE_NOT_ALLOCATED
) {
443 dev_err(gsi
->dev
, "event ring %u bad state %u before alloc\n",
448 gsi_evt_ring_command(gsi
, evt_ring_id
, GSI_EVT_ALLOCATE
);
450 /* If successful the event ring state will have changed */
451 state
= gsi_evt_ring_state(gsi
, evt_ring_id
);
452 if (state
== GSI_EVT_RING_STATE_ALLOCATED
)
455 dev_err(gsi
->dev
, "event ring %u bad state %u after alloc\n",
461 /* Reset a GSI event ring in ALLOCATED or ERROR state. */
462 static void gsi_evt_ring_reset_command(struct gsi
*gsi
, u32 evt_ring_id
)
464 enum gsi_evt_ring_state state
;
466 state
= gsi_evt_ring_state(gsi
, evt_ring_id
);
467 if (state
!= GSI_EVT_RING_STATE_ALLOCATED
&&
468 state
!= GSI_EVT_RING_STATE_ERROR
) {
469 dev_err(gsi
->dev
, "event ring %u bad state %u before reset\n",
474 gsi_evt_ring_command(gsi
, evt_ring_id
, GSI_EVT_RESET
);
476 /* If successful the event ring state will have changed */
477 state
= gsi_evt_ring_state(gsi
, evt_ring_id
);
478 if (state
== GSI_EVT_RING_STATE_ALLOCATED
)
481 dev_err(gsi
->dev
, "event ring %u bad state %u after reset\n",
485 /* Issue a hardware de-allocation request for an allocated event ring */
486 static void gsi_evt_ring_de_alloc_command(struct gsi
*gsi
, u32 evt_ring_id
)
488 enum gsi_evt_ring_state state
;
490 state
= gsi_evt_ring_state(gsi
, evt_ring_id
);
491 if (state
!= GSI_EVT_RING_STATE_ALLOCATED
) {
492 dev_err(gsi
->dev
, "event ring %u state %u before dealloc\n",
497 gsi_evt_ring_command(gsi
, evt_ring_id
, GSI_EVT_DE_ALLOC
);
499 /* If successful the event ring state will have changed */
500 state
= gsi_evt_ring_state(gsi
, evt_ring_id
);
501 if (state
== GSI_EVT_RING_STATE_NOT_ALLOCATED
)
504 dev_err(gsi
->dev
, "event ring %u bad state %u after dealloc\n",
508 /* Fetch the current state of a channel from hardware */
509 static enum gsi_channel_state
gsi_channel_state(struct gsi_channel
*channel
)
511 const struct reg
*reg
= gsi_reg(channel
->gsi
, CH_C_CNTXT_0
);
512 u32 channel_id
= gsi_channel_id(channel
);
513 struct gsi
*gsi
= channel
->gsi
;
514 void __iomem
*virt
= gsi
->virt
;
517 reg
= gsi_reg(gsi
, CH_C_CNTXT_0
);
518 val
= ioread32(virt
+ reg_n_offset(reg
, channel_id
));
520 return reg_decode(reg
, CHSTATE
, val
);
523 /* Issue a channel command and wait for it to complete */
525 gsi_channel_command(struct gsi_channel
*channel
, enum gsi_ch_cmd_opcode opcode
)
527 u32 channel_id
= gsi_channel_id(channel
);
528 struct gsi
*gsi
= channel
->gsi
;
529 struct device
*dev
= gsi
->dev
;
530 const struct reg
*reg
;
534 /* Enable the completion interrupt for the command */
535 gsi_irq_ch_ctrl_enable(gsi
, channel_id
);
537 reg
= gsi_reg(gsi
, CH_CMD
);
538 val
= reg_encode(reg
, CH_CHID
, channel_id
);
539 val
|= reg_encode(reg
, CH_OPCODE
, opcode
);
541 timeout
= !gsi_command(gsi
, reg_offset(reg
), val
);
543 gsi_irq_ch_ctrl_disable(gsi
);
548 dev_err(dev
, "GSI command %u for channel %u timed out, state %u\n",
549 opcode
, channel_id
, gsi_channel_state(channel
));
552 /* Allocate GSI channel in NOT_ALLOCATED state */
553 static int gsi_channel_alloc_command(struct gsi
*gsi
, u32 channel_id
)
555 struct gsi_channel
*channel
= &gsi
->channel
[channel_id
];
556 struct device
*dev
= gsi
->dev
;
557 enum gsi_channel_state state
;
559 /* Get initial channel state */
560 state
= gsi_channel_state(channel
);
561 if (state
!= GSI_CHANNEL_STATE_NOT_ALLOCATED
) {
562 dev_err(dev
, "channel %u bad state %u before alloc\n",
567 gsi_channel_command(channel
, GSI_CH_ALLOCATE
);
569 /* If successful the channel state will have changed */
570 state
= gsi_channel_state(channel
);
571 if (state
== GSI_CHANNEL_STATE_ALLOCATED
)
574 dev_err(dev
, "channel %u bad state %u after alloc\n",
580 /* Start an ALLOCATED channel */
581 static int gsi_channel_start_command(struct gsi_channel
*channel
)
583 struct device
*dev
= channel
->gsi
->dev
;
584 enum gsi_channel_state state
;
586 state
= gsi_channel_state(channel
);
587 if (state
!= GSI_CHANNEL_STATE_ALLOCATED
&&
588 state
!= GSI_CHANNEL_STATE_STOPPED
) {
589 dev_err(dev
, "channel %u bad state %u before start\n",
590 gsi_channel_id(channel
), state
);
594 gsi_channel_command(channel
, GSI_CH_START
);
596 /* If successful the channel state will have changed */
597 state
= gsi_channel_state(channel
);
598 if (state
== GSI_CHANNEL_STATE_STARTED
)
601 dev_err(dev
, "channel %u bad state %u after start\n",
602 gsi_channel_id(channel
), state
);
607 /* Stop a GSI channel in STARTED state */
608 static int gsi_channel_stop_command(struct gsi_channel
*channel
)
610 struct device
*dev
= channel
->gsi
->dev
;
611 enum gsi_channel_state state
;
613 state
= gsi_channel_state(channel
);
615 /* Channel could have entered STOPPED state since last call
616 * if it timed out. If so, we're done.
618 if (state
== GSI_CHANNEL_STATE_STOPPED
)
621 if (state
!= GSI_CHANNEL_STATE_STARTED
&&
622 state
!= GSI_CHANNEL_STATE_STOP_IN_PROC
) {
623 dev_err(dev
, "channel %u bad state %u before stop\n",
624 gsi_channel_id(channel
), state
);
628 gsi_channel_command(channel
, GSI_CH_STOP
);
630 /* If successful the channel state will have changed */
631 state
= gsi_channel_state(channel
);
632 if (state
== GSI_CHANNEL_STATE_STOPPED
)
635 /* We may have to try again if stop is in progress */
636 if (state
== GSI_CHANNEL_STATE_STOP_IN_PROC
)
639 dev_err(dev
, "channel %u bad state %u after stop\n",
640 gsi_channel_id(channel
), state
);
645 /* Reset a GSI channel in ALLOCATED or ERROR state. */
646 static void gsi_channel_reset_command(struct gsi_channel
*channel
)
648 struct device
*dev
= channel
->gsi
->dev
;
649 enum gsi_channel_state state
;
651 /* A short delay is required before a RESET command */
652 usleep_range(USEC_PER_MSEC
, 2 * USEC_PER_MSEC
);
654 state
= gsi_channel_state(channel
);
655 if (state
!= GSI_CHANNEL_STATE_STOPPED
&&
656 state
!= GSI_CHANNEL_STATE_ERROR
) {
657 /* No need to reset a channel already in ALLOCATED state */
658 if (state
!= GSI_CHANNEL_STATE_ALLOCATED
)
659 dev_err(dev
, "channel %u bad state %u before reset\n",
660 gsi_channel_id(channel
), state
);
664 gsi_channel_command(channel
, GSI_CH_RESET
);
666 /* If successful the channel state will have changed */
667 state
= gsi_channel_state(channel
);
668 if (state
!= GSI_CHANNEL_STATE_ALLOCATED
)
669 dev_err(dev
, "channel %u bad state %u after reset\n",
670 gsi_channel_id(channel
), state
);
673 /* Deallocate an ALLOCATED GSI channel */
674 static void gsi_channel_de_alloc_command(struct gsi
*gsi
, u32 channel_id
)
676 struct gsi_channel
*channel
= &gsi
->channel
[channel_id
];
677 struct device
*dev
= gsi
->dev
;
678 enum gsi_channel_state state
;
680 state
= gsi_channel_state(channel
);
681 if (state
!= GSI_CHANNEL_STATE_ALLOCATED
) {
682 dev_err(dev
, "channel %u bad state %u before dealloc\n",
687 gsi_channel_command(channel
, GSI_CH_DE_ALLOC
);
689 /* If successful the channel state will have changed */
690 state
= gsi_channel_state(channel
);
692 if (state
!= GSI_CHANNEL_STATE_NOT_ALLOCATED
)
693 dev_err(dev
, "channel %u bad state %u after dealloc\n",
697 /* Ring an event ring doorbell, reporting the last entry processed by the AP.
698 * The index argument (modulo the ring count) is the first unfilled entry, so
699 * we supply one less than that with the doorbell. Update the event ring
700 * index field with the value provided.
702 static void gsi_evt_ring_doorbell(struct gsi
*gsi
, u32 evt_ring_id
, u32 index
)
704 const struct reg
*reg
= gsi_reg(gsi
, EV_CH_E_DOORBELL_0
);
705 struct gsi_ring
*ring
= &gsi
->evt_ring
[evt_ring_id
].ring
;
708 ring
->index
= index
; /* Next unused entry */
710 /* Note: index *must* be used modulo the ring count here */
711 val
= gsi_ring_addr(ring
, (index
- 1) % ring
->count
);
712 iowrite32(val
, gsi
->virt
+ reg_n_offset(reg
, evt_ring_id
));
715 /* Program an event ring for use */
716 static void gsi_evt_ring_program(struct gsi
*gsi
, u32 evt_ring_id
)
718 struct gsi_evt_ring
*evt_ring
= &gsi
->evt_ring
[evt_ring_id
];
719 struct gsi_ring
*ring
= &evt_ring
->ring
;
720 const struct reg
*reg
;
723 reg
= gsi_reg(gsi
, EV_CH_E_CNTXT_0
);
724 /* We program all event rings as GPI type/protocol */
725 val
= reg_encode(reg
, EV_CHTYPE
, GSI_CHANNEL_TYPE_GPI
);
726 /* EV_EE field is 0 (GSI_EE_AP) */
727 val
|= reg_bit(reg
, EV_INTYPE
);
728 val
|= reg_encode(reg
, EV_ELEMENT_SIZE
, GSI_RING_ELEMENT_SIZE
);
729 iowrite32(val
, gsi
->virt
+ reg_n_offset(reg
, evt_ring_id
));
731 reg
= gsi_reg(gsi
, EV_CH_E_CNTXT_1
);
732 val
= reg_encode(reg
, R_LENGTH
, ring
->count
* GSI_RING_ELEMENT_SIZE
);
733 iowrite32(val
, gsi
->virt
+ reg_n_offset(reg
, evt_ring_id
));
735 /* The context 2 and 3 registers store the low-order and
736 * high-order 32 bits of the address of the event ring,
739 reg
= gsi_reg(gsi
, EV_CH_E_CNTXT_2
);
740 val
= lower_32_bits(ring
->addr
);
741 iowrite32(val
, gsi
->virt
+ reg_n_offset(reg
, evt_ring_id
));
743 reg
= gsi_reg(gsi
, EV_CH_E_CNTXT_3
);
744 val
= upper_32_bits(ring
->addr
);
745 iowrite32(val
, gsi
->virt
+ reg_n_offset(reg
, evt_ring_id
));
747 /* Enable interrupt moderation by setting the moderation delay */
748 reg
= gsi_reg(gsi
, EV_CH_E_CNTXT_8
);
749 val
= reg_encode(reg
, EV_MODT
, GSI_EVT_RING_INT_MODT
);
750 val
|= reg_encode(reg
, EV_MODC
, 1); /* comes from channel */
751 /* EV_MOD_CNT is 0 (no counter-based interrupt coalescing) */
752 iowrite32(val
, gsi
->virt
+ reg_n_offset(reg
, evt_ring_id
));
754 /* No MSI write data, and MSI high and low address is 0 */
755 reg
= gsi_reg(gsi
, EV_CH_E_CNTXT_9
);
756 iowrite32(0, gsi
->virt
+ reg_n_offset(reg
, evt_ring_id
));
758 reg
= gsi_reg(gsi
, EV_CH_E_CNTXT_10
);
759 iowrite32(0, gsi
->virt
+ reg_n_offset(reg
, evt_ring_id
));
761 reg
= gsi_reg(gsi
, EV_CH_E_CNTXT_11
);
762 iowrite32(0, gsi
->virt
+ reg_n_offset(reg
, evt_ring_id
));
764 /* We don't need to get event read pointer updates */
765 reg
= gsi_reg(gsi
, EV_CH_E_CNTXT_12
);
766 iowrite32(0, gsi
->virt
+ reg_n_offset(reg
, evt_ring_id
));
768 reg
= gsi_reg(gsi
, EV_CH_E_CNTXT_13
);
769 iowrite32(0, gsi
->virt
+ reg_n_offset(reg
, evt_ring_id
));
771 /* Finally, tell the hardware our "last processed" event (arbitrary) */
772 gsi_evt_ring_doorbell(gsi
, evt_ring_id
, ring
->index
);
775 /* Find the transaction whose completion indicates a channel is quiesced */
776 static struct gsi_trans
*gsi_channel_trans_last(struct gsi_channel
*channel
)
778 struct gsi_trans_info
*trans_info
= &channel
->trans_info
;
779 u32 pending_id
= trans_info
->pending_id
;
780 struct gsi_trans
*trans
;
783 if (channel
->toward_ipa
&& pending_id
!= trans_info
->free_id
) {
784 /* There is a small chance a TX transaction got allocated
785 * just before we disabled transmits, so check for that.
786 * The last allocated, committed, or pending transaction
787 * precedes the first free transaction.
789 trans_id
= trans_info
->free_id
- 1;
790 } else if (trans_info
->polled_id
!= pending_id
) {
791 /* Otherwise (TX or RX) we want to wait for anything that
792 * has completed, or has been polled but not released yet.
794 * The last completed or polled transaction precedes the
795 * first pending transaction.
797 trans_id
= pending_id
- 1;
802 /* Caller will wait for this, so take a reference */
803 trans
= &trans_info
->trans
[trans_id
% channel
->tre_count
];
804 refcount_inc(&trans
->refcount
);
809 /* Wait for transaction activity on a channel to complete */
810 static void gsi_channel_trans_quiesce(struct gsi_channel
*channel
)
812 struct gsi_trans
*trans
;
814 /* Get the last transaction, and wait for it to complete */
815 trans
= gsi_channel_trans_last(channel
);
817 wait_for_completion(&trans
->completion
);
818 gsi_trans_free(trans
);
822 /* Program a channel for use; there is no gsi_channel_deprogram() */
823 static void gsi_channel_program(struct gsi_channel
*channel
, bool doorbell
)
825 size_t size
= channel
->tre_ring
.count
* GSI_RING_ELEMENT_SIZE
;
826 u32 channel_id
= gsi_channel_id(channel
);
827 union gsi_channel_scratch scr
= { };
828 struct gsi_channel_scratch_gpi
*gpi
;
829 struct gsi
*gsi
= channel
->gsi
;
830 const struct reg
*reg
;
835 reg
= gsi_reg(gsi
, CH_C_CNTXT_0
);
837 /* We program all channels as GPI type/protocol */
838 val
= ch_c_cntxt_0_type_encode(gsi
->version
, reg
, GSI_CHANNEL_TYPE_GPI
);
839 if (channel
->toward_ipa
)
840 val
|= reg_bit(reg
, CHTYPE_DIR
);
841 if (gsi
->version
< IPA_VERSION_5_0
)
842 val
|= reg_encode(reg
, ERINDEX
, channel
->evt_ring_id
);
843 val
|= reg_encode(reg
, ELEMENT_SIZE
, GSI_RING_ELEMENT_SIZE
);
844 iowrite32(val
, gsi
->virt
+ reg_n_offset(reg
, channel_id
));
846 reg
= gsi_reg(gsi
, CH_C_CNTXT_1
);
847 val
= reg_encode(reg
, CH_R_LENGTH
, size
);
848 if (gsi
->version
>= IPA_VERSION_5_0
)
849 val
|= reg_encode(reg
, CH_ERINDEX
, channel
->evt_ring_id
);
850 iowrite32(val
, gsi
->virt
+ reg_n_offset(reg
, channel_id
));
852 /* The context 2 and 3 registers store the low-order and
853 * high-order 32 bits of the address of the channel ring,
856 reg
= gsi_reg(gsi
, CH_C_CNTXT_2
);
857 val
= lower_32_bits(channel
->tre_ring
.addr
);
858 iowrite32(val
, gsi
->virt
+ reg_n_offset(reg
, channel_id
));
860 reg
= gsi_reg(gsi
, CH_C_CNTXT_3
);
861 val
= upper_32_bits(channel
->tre_ring
.addr
);
862 iowrite32(val
, gsi
->virt
+ reg_n_offset(reg
, channel_id
));
864 reg
= gsi_reg(gsi
, CH_C_QOS
);
866 /* Command channel gets low weighted round-robin priority */
867 if (channel
->command
)
868 wrr_weight
= reg_field_max(reg
, WRR_WEIGHT
);
869 val
= reg_encode(reg
, WRR_WEIGHT
, wrr_weight
);
871 /* Max prefetch is 1 segment (do not set MAX_PREFETCH_FMASK) */
873 /* No need to use the doorbell engine starting at IPA v4.0 */
874 if (gsi
->version
< IPA_VERSION_4_0
&& doorbell
)
875 val
|= reg_bit(reg
, USE_DB_ENG
);
877 /* v4.0 introduces an escape buffer for prefetch. We use it
878 * on all but the AP command channel.
880 if (gsi
->version
>= IPA_VERSION_4_0
&& !channel
->command
) {
881 /* If not otherwise set, prefetch buffers are used */
882 if (gsi
->version
< IPA_VERSION_4_5
)
883 val
|= reg_bit(reg
, USE_ESCAPE_BUF_ONLY
);
885 val
|= reg_encode(reg
, PREFETCH_MODE
, ESCAPE_BUF_ONLY
);
887 /* All channels set DB_IN_BYTES */
888 if (gsi
->version
>= IPA_VERSION_4_9
)
889 val
|= reg_bit(reg
, DB_IN_BYTES
);
891 iowrite32(val
, gsi
->virt
+ reg_n_offset(reg
, channel_id
));
893 /* Now update the scratch registers for GPI protocol */
895 gpi
->max_outstanding_tre
= channel
->trans_tre_max
*
896 GSI_RING_ELEMENT_SIZE
;
897 gpi
->outstanding_threshold
= 2 * GSI_RING_ELEMENT_SIZE
;
899 reg
= gsi_reg(gsi
, CH_C_SCRATCH_0
);
900 val
= scr
.data
.word1
;
901 iowrite32(val
, gsi
->virt
+ reg_n_offset(reg
, channel_id
));
903 reg
= gsi_reg(gsi
, CH_C_SCRATCH_1
);
904 val
= scr
.data
.word2
;
905 iowrite32(val
, gsi
->virt
+ reg_n_offset(reg
, channel_id
));
907 reg
= gsi_reg(gsi
, CH_C_SCRATCH_2
);
908 val
= scr
.data
.word3
;
909 iowrite32(val
, gsi
->virt
+ reg_n_offset(reg
, channel_id
));
911 /* We must preserve the upper 16 bits of the last scratch register.
912 * The next sequence assumes those bits remain unchanged between the
913 * read and the write.
915 reg
= gsi_reg(gsi
, CH_C_SCRATCH_3
);
916 offset
= reg_n_offset(reg
, channel_id
);
917 val
= ioread32(gsi
->virt
+ offset
);
918 val
= (scr
.data
.word4
& GENMASK(31, 16)) | (val
& GENMASK(15, 0));
919 iowrite32(val
, gsi
->virt
+ offset
);
924 static int __gsi_channel_start(struct gsi_channel
*channel
, bool resume
)
926 struct gsi
*gsi
= channel
->gsi
;
929 /* Prior to IPA v4.0 suspend/resume is not implemented by GSI */
930 if (resume
&& gsi
->version
< IPA_VERSION_4_0
)
933 mutex_lock(&gsi
->mutex
);
935 ret
= gsi_channel_start_command(channel
);
937 mutex_unlock(&gsi
->mutex
);
942 /* Start an allocated GSI channel */
943 int gsi_channel_start(struct gsi
*gsi
, u32 channel_id
)
945 struct gsi_channel
*channel
= &gsi
->channel
[channel_id
];
948 /* Enable NAPI and the completion interrupt */
949 napi_enable(&channel
->napi
);
950 gsi_irq_ieob_enable_one(gsi
, channel
->evt_ring_id
);
952 ret
= __gsi_channel_start(channel
, false);
954 gsi_irq_ieob_disable_one(gsi
, channel
->evt_ring_id
);
955 napi_disable(&channel
->napi
);
961 static int gsi_channel_stop_retry(struct gsi_channel
*channel
)
963 u32 retries
= GSI_CHANNEL_STOP_RETRIES
;
967 ret
= gsi_channel_stop_command(channel
);
970 usleep_range(3 * USEC_PER_MSEC
, 5 * USEC_PER_MSEC
);
976 static int __gsi_channel_stop(struct gsi_channel
*channel
, bool suspend
)
978 struct gsi
*gsi
= channel
->gsi
;
981 /* Wait for any underway transactions to complete before stopping. */
982 gsi_channel_trans_quiesce(channel
);
984 /* Prior to IPA v4.0 suspend/resume is not implemented by GSI */
985 if (suspend
&& gsi
->version
< IPA_VERSION_4_0
)
988 mutex_lock(&gsi
->mutex
);
990 ret
= gsi_channel_stop_retry(channel
);
992 mutex_unlock(&gsi
->mutex
);
997 /* Stop a started channel */
998 int gsi_channel_stop(struct gsi
*gsi
, u32 channel_id
)
1000 struct gsi_channel
*channel
= &gsi
->channel
[channel_id
];
1003 ret
= __gsi_channel_stop(channel
, false);
1007 /* Disable the completion interrupt and NAPI if successful */
1008 gsi_irq_ieob_disable_one(gsi
, channel
->evt_ring_id
);
1009 napi_disable(&channel
->napi
);
1014 /* Reset and reconfigure a channel, (possibly) enabling the doorbell engine */
1015 void gsi_channel_reset(struct gsi
*gsi
, u32 channel_id
, bool doorbell
)
1017 struct gsi_channel
*channel
= &gsi
->channel
[channel_id
];
1019 mutex_lock(&gsi
->mutex
);
1021 gsi_channel_reset_command(channel
);
1022 /* Due to a hardware quirk we may need to reset RX channels twice. */
1023 if (gsi
->version
< IPA_VERSION_4_0
&& !channel
->toward_ipa
)
1024 gsi_channel_reset_command(channel
);
1026 /* Hardware assumes this is 0 following reset */
1027 channel
->tre_ring
.index
= 0;
1028 gsi_channel_program(channel
, doorbell
);
1029 gsi_channel_trans_cancel_pending(channel
);
1031 mutex_unlock(&gsi
->mutex
);
1034 /* Stop a started channel for suspend */
1035 int gsi_channel_suspend(struct gsi
*gsi
, u32 channel_id
)
1037 struct gsi_channel
*channel
= &gsi
->channel
[channel_id
];
1040 ret
= __gsi_channel_stop(channel
, true);
1044 /* Ensure NAPI polling has finished. */
1045 napi_synchronize(&channel
->napi
);
1050 /* Resume a suspended channel (starting if stopped) */
1051 int gsi_channel_resume(struct gsi
*gsi
, u32 channel_id
)
1053 struct gsi_channel
*channel
= &gsi
->channel
[channel_id
];
1055 return __gsi_channel_start(channel
, true);
1058 /* Prevent all GSI interrupts while suspended */
1059 void gsi_suspend(struct gsi
*gsi
)
1061 disable_irq(gsi
->irq
);
1064 /* Allow all GSI interrupts again when resuming */
1065 void gsi_resume(struct gsi
*gsi
)
1067 enable_irq(gsi
->irq
);
1070 void gsi_trans_tx_committed(struct gsi_trans
*trans
)
1072 struct gsi_channel
*channel
= &trans
->gsi
->channel
[trans
->channel_id
];
1074 channel
->trans_count
++;
1075 channel
->byte_count
+= trans
->len
;
1077 trans
->trans_count
= channel
->trans_count
;
1078 trans
->byte_count
= channel
->byte_count
;
1081 void gsi_trans_tx_queued(struct gsi_trans
*trans
)
1083 u32 channel_id
= trans
->channel_id
;
1084 struct gsi
*gsi
= trans
->gsi
;
1085 struct gsi_channel
*channel
;
1089 channel
= &gsi
->channel
[channel_id
];
1091 byte_count
= channel
->byte_count
- channel
->queued_byte_count
;
1092 trans_count
= channel
->trans_count
- channel
->queued_trans_count
;
1093 channel
->queued_byte_count
= channel
->byte_count
;
1094 channel
->queued_trans_count
= channel
->trans_count
;
1096 ipa_gsi_channel_tx_queued(gsi
, channel_id
, trans_count
, byte_count
);
1100 * gsi_trans_tx_completed() - Report completed TX transactions
1101 * @trans: TX channel transaction that has completed
1103 * Report that a transaction on a TX channel has completed. At the time a
1104 * transaction is committed, we record *in the transaction* its channel's
1105 * committed transaction and byte counts. Transactions are completed in
1106 * order, and the difference between the channel's byte/transaction count
1107 * when the transaction was committed and when it completes tells us
1108 * exactly how much data has been transferred while the transaction was
1111 * We report this information to the network stack, which uses it to manage
1112 * the rate at which data is sent to hardware.
1114 static void gsi_trans_tx_completed(struct gsi_trans
*trans
)
1116 u32 channel_id
= trans
->channel_id
;
1117 struct gsi
*gsi
= trans
->gsi
;
1118 struct gsi_channel
*channel
;
1122 channel
= &gsi
->channel
[channel_id
];
1123 trans_count
= trans
->trans_count
- channel
->compl_trans_count
;
1124 byte_count
= trans
->byte_count
- channel
->compl_byte_count
;
1126 channel
->compl_trans_count
+= trans_count
;
1127 channel
->compl_byte_count
+= byte_count
;
1129 ipa_gsi_channel_tx_completed(gsi
, channel_id
, trans_count
, byte_count
);
1132 /* Channel control interrupt handler */
1133 static void gsi_isr_chan_ctrl(struct gsi
*gsi
)
1135 const struct reg
*reg
;
1138 reg
= gsi_reg(gsi
, CNTXT_SRC_CH_IRQ
);
1139 channel_mask
= ioread32(gsi
->virt
+ reg_offset(reg
));
1141 reg
= gsi_reg(gsi
, CNTXT_SRC_CH_IRQ_CLR
);
1142 iowrite32(channel_mask
, gsi
->virt
+ reg_offset(reg
));
1144 while (channel_mask
) {
1145 u32 channel_id
= __ffs(channel_mask
);
1147 channel_mask
^= BIT(channel_id
);
1149 complete(&gsi
->completion
);
1153 /* Event ring control interrupt handler */
1154 static void gsi_isr_evt_ctrl(struct gsi
*gsi
)
1156 const struct reg
*reg
;
1159 reg
= gsi_reg(gsi
, CNTXT_SRC_EV_CH_IRQ
);
1160 event_mask
= ioread32(gsi
->virt
+ reg_offset(reg
));
1162 reg
= gsi_reg(gsi
, CNTXT_SRC_EV_CH_IRQ_CLR
);
1163 iowrite32(event_mask
, gsi
->virt
+ reg_offset(reg
));
1165 while (event_mask
) {
1166 u32 evt_ring_id
= __ffs(event_mask
);
1168 event_mask
^= BIT(evt_ring_id
);
1170 complete(&gsi
->completion
);
1174 /* Global channel error interrupt handler */
1176 gsi_isr_glob_chan_err(struct gsi
*gsi
, u32 err_ee
, u32 channel_id
, u32 code
)
1178 if (code
== GSI_OUT_OF_RESOURCES
) {
1179 dev_err(gsi
->dev
, "channel %u out of resources\n", channel_id
);
1180 complete(&gsi
->completion
);
1184 /* Report, but otherwise ignore all other error codes */
1185 dev_err(gsi
->dev
, "channel %u global error ee 0x%08x code 0x%08x\n",
1186 channel_id
, err_ee
, code
);
1189 /* Global event error interrupt handler */
1191 gsi_isr_glob_evt_err(struct gsi
*gsi
, u32 err_ee
, u32 evt_ring_id
, u32 code
)
1193 if (code
== GSI_OUT_OF_RESOURCES
) {
1194 struct gsi_evt_ring
*evt_ring
= &gsi
->evt_ring
[evt_ring_id
];
1195 u32 channel_id
= gsi_channel_id(evt_ring
->channel
);
1197 complete(&gsi
->completion
);
1198 dev_err(gsi
->dev
, "evt_ring for channel %u out of resources\n",
1203 /* Report, but otherwise ignore all other error codes */
1204 dev_err(gsi
->dev
, "event ring %u global error ee %u code 0x%08x\n",
1205 evt_ring_id
, err_ee
, code
);
1208 /* Global error interrupt handler */
1209 static void gsi_isr_glob_err(struct gsi
*gsi
)
1211 const struct reg
*log_reg
;
1212 const struct reg
*clr_reg
;
1213 enum gsi_err_type type
;
1214 enum gsi_err_code code
;
1220 /* Get the logged error, then reinitialize the log */
1221 log_reg
= gsi_reg(gsi
, ERROR_LOG
);
1222 offset
= reg_offset(log_reg
);
1223 val
= ioread32(gsi
->virt
+ offset
);
1224 iowrite32(0, gsi
->virt
+ offset
);
1226 clr_reg
= gsi_reg(gsi
, ERROR_LOG_CLR
);
1227 iowrite32(~0, gsi
->virt
+ reg_offset(clr_reg
));
1229 /* Parse the error value */
1230 ee
= reg_decode(log_reg
, ERR_EE
, val
);
1231 type
= reg_decode(log_reg
, ERR_TYPE
, val
);
1232 which
= reg_decode(log_reg
, ERR_VIRT_IDX
, val
);
1233 code
= reg_decode(log_reg
, ERR_CODE
, val
);
1235 if (type
== GSI_ERR_TYPE_CHAN
)
1236 gsi_isr_glob_chan_err(gsi
, ee
, which
, code
);
1237 else if (type
== GSI_ERR_TYPE_EVT
)
1238 gsi_isr_glob_evt_err(gsi
, ee
, which
, code
);
1239 else /* type GSI_ERR_TYPE_GLOB should be fatal */
1240 dev_err(gsi
->dev
, "unexpected global error 0x%08x\n", type
);
1243 /* Generic EE interrupt handler */
1244 static void gsi_isr_gp_int1(struct gsi
*gsi
)
1246 const struct reg
*reg
;
1250 /* This interrupt is used to handle completions of GENERIC GSI
1251 * commands. We use these to allocate and halt channels on the
1252 * modem's behalf due to a hardware quirk on IPA v4.2. The modem
1253 * "owns" channels even when the AP allocates them, and have no
1254 * way of knowing whether a modem channel's state has been changed.
1256 * We also use GENERIC commands to enable/disable channel flow
1257 * control for IPA v4.2+.
1259 * It is recommended that we halt the modem channels we allocated
1260 * when shutting down, but it's possible the channel isn't running
1261 * at the time we issue the HALT command. We'll get an error in
1262 * that case, but it's harmless (the channel is already halted).
1263 * Similarly, we could get an error back when updating flow control
1264 * on a channel because it's not in the proper state.
1266 * In either case, we silently ignore a INCORRECT_CHANNEL_STATE
1267 * error if we receive it.
1269 reg
= gsi_reg(gsi
, CNTXT_SCRATCH_0
);
1270 val
= ioread32(gsi
->virt
+ reg_offset(reg
));
1271 result
= reg_decode(reg
, GENERIC_EE_RESULT
, val
);
1274 case GENERIC_EE_SUCCESS
:
1275 case GENERIC_EE_INCORRECT_CHANNEL_STATE
:
1279 case GENERIC_EE_RETRY
:
1280 gsi
->result
= -EAGAIN
;
1284 dev_err(gsi
->dev
, "global INT1 generic result %u\n", result
);
1289 complete(&gsi
->completion
);
1292 /* Inter-EE interrupt handler */
1293 static void gsi_isr_glob_ee(struct gsi
*gsi
)
1295 const struct reg
*reg
;
1298 reg
= gsi_reg(gsi
, CNTXT_GLOB_IRQ_STTS
);
1299 val
= ioread32(gsi
->virt
+ reg_offset(reg
));
1301 if (val
& ERROR_INT
)
1302 gsi_isr_glob_err(gsi
);
1304 reg
= gsi_reg(gsi
, CNTXT_GLOB_IRQ_CLR
);
1305 iowrite32(val
, gsi
->virt
+ reg_offset(reg
));
1309 if (val
& GP_INT1
) {
1311 gsi_isr_gp_int1(gsi
);
1315 dev_err(gsi
->dev
, "unexpected global interrupt 0x%08x\n", val
);
1318 /* I/O completion interrupt event */
1319 static void gsi_isr_ieob(struct gsi
*gsi
)
1321 const struct reg
*reg
;
1324 reg
= gsi_reg(gsi
, CNTXT_SRC_IEOB_IRQ
);
1325 event_mask
= ioread32(gsi
->virt
+ reg_offset(reg
));
1327 gsi_irq_ieob_disable(gsi
, event_mask
);
1329 reg
= gsi_reg(gsi
, CNTXT_SRC_IEOB_IRQ_CLR
);
1330 iowrite32(event_mask
, gsi
->virt
+ reg_offset(reg
));
1332 while (event_mask
) {
1333 u32 evt_ring_id
= __ffs(event_mask
);
1335 event_mask
^= BIT(evt_ring_id
);
1337 napi_schedule(&gsi
->evt_ring
[evt_ring_id
].channel
->napi
);
1341 /* General event interrupts represent serious problems, so report them */
1342 static void gsi_isr_general(struct gsi
*gsi
)
1344 struct device
*dev
= gsi
->dev
;
1345 const struct reg
*reg
;
1348 reg
= gsi_reg(gsi
, CNTXT_GSI_IRQ_STTS
);
1349 val
= ioread32(gsi
->virt
+ reg_offset(reg
));
1351 reg
= gsi_reg(gsi
, CNTXT_GSI_IRQ_CLR
);
1352 iowrite32(val
, gsi
->virt
+ reg_offset(reg
));
1354 dev_err(dev
, "unexpected general interrupt 0x%08x\n", val
);
1358 * gsi_isr() - Top level GSI interrupt service routine
1359 * @irq: Interrupt number (ignored)
1360 * @dev_id: GSI pointer supplied to request_irq()
1362 * This is the main handler function registered for the GSI IRQ. Each type
1363 * of interrupt has a separate handler function that is called from here.
1365 static irqreturn_t
gsi_isr(int irq
, void *dev_id
)
1367 struct gsi
*gsi
= dev_id
;
1368 const struct reg
*reg
;
1373 reg
= gsi_reg(gsi
, CNTXT_TYPE_IRQ
);
1374 offset
= reg_offset(reg
);
1376 /* enum gsi_irq_type_id defines GSI interrupt types */
1377 while ((intr_mask
= ioread32(gsi
->virt
+ offset
))) {
1378 /* intr_mask contains bitmask of pending GSI interrupts */
1380 u32 gsi_intr
= BIT(__ffs(intr_mask
));
1382 intr_mask
^= gsi_intr
;
1384 /* Note: the IRQ condition for each type is cleared
1385 * when the type-specific register is updated.
1389 gsi_isr_chan_ctrl(gsi
);
1392 gsi_isr_evt_ctrl(gsi
);
1395 gsi_isr_glob_ee(gsi
);
1401 gsi_isr_general(gsi
);
1405 "unrecognized interrupt type 0x%08x\n",
1409 } while (intr_mask
);
1411 if (++cnt
> GSI_ISR_MAX_ITER
) {
1412 dev_err(gsi
->dev
, "interrupt flood\n");
1420 /* Init function for GSI IRQ lookup; there is no gsi_irq_exit() */
1421 static int gsi_irq_init(struct gsi
*gsi
, struct platform_device
*pdev
)
1425 ret
= platform_get_irq_byname(pdev
, "gsi");
1427 return ret
? : -EINVAL
;
1434 /* Return the transaction associated with a transfer completion event */
1435 static struct gsi_trans
*
1436 gsi_event_trans(struct gsi
*gsi
, struct gsi_event
*event
)
1438 u32 channel_id
= event
->chid
;
1439 struct gsi_channel
*channel
;
1440 struct gsi_trans
*trans
;
1444 channel
= &gsi
->channel
[channel_id
];
1445 if (WARN(!channel
->gsi
, "event has bad channel %u\n", channel_id
))
1448 /* Event xfer_ptr records the TRE it's associated with */
1449 tre_offset
= lower_32_bits(le64_to_cpu(event
->xfer_ptr
));
1450 tre_index
= gsi_ring_index(&channel
->tre_ring
, tre_offset
);
1452 trans
= gsi_channel_trans_mapped(channel
, tre_index
);
1454 if (WARN(!trans
, "channel %u event with no transaction\n", channel_id
))
1461 * gsi_evt_ring_update() - Update transaction state from hardware
1463 * @evt_ring_id: Event ring ID
1464 * @index: Event index in ring reported by hardware
1466 * Events for RX channels contain the actual number of bytes received into
1467 * the buffer. Every event has a transaction associated with it, and here
1468 * we update transactions to record their actual received lengths.
1470 * When an event for a TX channel arrives we use information in the
1471 * transaction to report the number of requests and bytes that have
1474 * This function is called whenever we learn that the GSI hardware has filled
1475 * new events since the last time we checked. The ring's index field tells
1476 * the first entry in need of processing. The index provided is the
1477 * first *unfilled* event in the ring (following the last filled one).
1479 * Events are sequential within the event ring, and transactions are
1480 * sequential within the transaction array.
1482 * Note that @index always refers to an element *within* the event ring.
1484 static void gsi_evt_ring_update(struct gsi
*gsi
, u32 evt_ring_id
, u32 index
)
1486 struct gsi_evt_ring
*evt_ring
= &gsi
->evt_ring
[evt_ring_id
];
1487 struct gsi_ring
*ring
= &evt_ring
->ring
;
1488 struct gsi_event
*event_done
;
1489 struct gsi_event
*event
;
1493 /* Starting with the oldest un-processed event, determine which
1494 * transaction (and which channel) is associated with the event.
1495 * For RX channels, update each completed transaction with the
1496 * number of bytes that were actually received. For TX channels
1497 * associated with a network device, report to the network stack
1498 * the number of transfers and bytes this completion represents.
1500 old_index
= ring
->index
;
1501 event
= gsi_ring_virt(ring
, old_index
);
1503 /* Compute the number of events to process before we wrap,
1504 * and determine when we'll be done processing events.
1506 event_avail
= ring
->count
- old_index
% ring
->count
;
1507 event_done
= gsi_ring_virt(ring
, index
);
1509 struct gsi_trans
*trans
;
1511 trans
= gsi_event_trans(gsi
, event
);
1515 if (trans
->direction
== DMA_FROM_DEVICE
)
1516 trans
->len
= __le16_to_cpu(event
->len
);
1518 gsi_trans_tx_completed(trans
);
1520 gsi_trans_move_complete(trans
);
1522 /* Move on to the next event and transaction */
1526 event
= gsi_ring_virt(ring
, 0);
1527 } while (event
!= event_done
);
1529 /* Tell the hardware we've handled these events */
1530 gsi_evt_ring_doorbell(gsi
, evt_ring_id
, index
);
1533 /* Initialize a ring, including allocating DMA memory for its entries */
1534 static int gsi_ring_alloc(struct gsi
*gsi
, struct gsi_ring
*ring
, u32 count
)
1536 u32 size
= count
* GSI_RING_ELEMENT_SIZE
;
1537 struct device
*dev
= gsi
->dev
;
1540 /* Hardware requires a 2^n ring size, with alignment equal to size.
1541 * The DMA address returned by dma_alloc_coherent() is guaranteed to
1542 * be a power-of-2 number of pages, which satisfies the requirement.
1544 ring
->virt
= dma_alloc_coherent(dev
, size
, &addr
, GFP_KERNEL
);
1549 ring
->count
= count
;
1555 /* Free a previously-allocated ring */
1556 static void gsi_ring_free(struct gsi
*gsi
, struct gsi_ring
*ring
)
1558 size_t size
= ring
->count
* GSI_RING_ELEMENT_SIZE
;
1560 dma_free_coherent(gsi
->dev
, size
, ring
->virt
, ring
->addr
);
1563 /* Allocate an available event ring id */
1564 static int gsi_evt_ring_id_alloc(struct gsi
*gsi
)
1568 if (gsi
->event_bitmap
== ~0U) {
1569 dev_err(gsi
->dev
, "event rings exhausted\n");
1573 evt_ring_id
= ffz(gsi
->event_bitmap
);
1574 gsi
->event_bitmap
|= BIT(evt_ring_id
);
1576 return (int)evt_ring_id
;
1579 /* Free a previously-allocated event ring id */
1580 static void gsi_evt_ring_id_free(struct gsi
*gsi
, u32 evt_ring_id
)
1582 gsi
->event_bitmap
&= ~BIT(evt_ring_id
);
1585 /* Ring a channel doorbell, reporting the first un-filled entry */
1586 void gsi_channel_doorbell(struct gsi_channel
*channel
)
1588 struct gsi_ring
*tre_ring
= &channel
->tre_ring
;
1589 u32 channel_id
= gsi_channel_id(channel
);
1590 struct gsi
*gsi
= channel
->gsi
;
1591 const struct reg
*reg
;
1594 reg
= gsi_reg(gsi
, CH_C_DOORBELL_0
);
1595 /* Note: index *must* be used modulo the ring count here */
1596 val
= gsi_ring_addr(tre_ring
, tre_ring
->index
% tre_ring
->count
);
1597 iowrite32(val
, gsi
->virt
+ reg_n_offset(reg
, channel_id
));
1600 /* Consult hardware, move newly completed transactions to completed state */
1601 void gsi_channel_update(struct gsi_channel
*channel
)
1603 u32 evt_ring_id
= channel
->evt_ring_id
;
1604 struct gsi
*gsi
= channel
->gsi
;
1605 struct gsi_evt_ring
*evt_ring
;
1606 struct gsi_trans
*trans
;
1607 struct gsi_ring
*ring
;
1608 const struct reg
*reg
;
1612 evt_ring
= &gsi
->evt_ring
[evt_ring_id
];
1613 ring
= &evt_ring
->ring
;
1615 /* See if there's anything new to process; if not, we're done. Note
1616 * that index always refers to an entry *within* the event ring.
1618 reg
= gsi_reg(gsi
, EV_CH_E_CNTXT_4
);
1619 offset
= reg_n_offset(reg
, evt_ring_id
);
1620 index
= gsi_ring_index(ring
, ioread32(gsi
->virt
+ offset
));
1621 if (index
== ring
->index
% ring
->count
)
1624 /* Get the transaction for the latest completed event. */
1625 trans
= gsi_event_trans(gsi
, gsi_ring_virt(ring
, index
- 1));
1629 /* For RX channels, update each completed transaction with the number
1630 * of bytes that were actually received. For TX channels, report
1631 * the number of transactions and bytes this completion represents
1632 * up the network stack.
1634 gsi_evt_ring_update(gsi
, evt_ring_id
, index
);
1638 * gsi_channel_poll_one() - Return a single completed transaction on a channel
1639 * @channel: Channel to be polled
1641 * Return: Transaction pointer, or null if none are available
1643 * This function returns the first of a channel's completed transactions.
1644 * If no transactions are in completed state, the hardware is consulted to
1645 * determine whether any new transactions have completed. If so, they're
1646 * moved to completed state and the first such transaction is returned.
1647 * If there are no more completed transactions, a null pointer is returned.
1649 static struct gsi_trans
*gsi_channel_poll_one(struct gsi_channel
*channel
)
1651 struct gsi_trans
*trans
;
1653 /* Get the first completed transaction */
1654 trans
= gsi_channel_trans_complete(channel
);
1656 gsi_trans_move_polled(trans
);
1662 * gsi_channel_poll() - NAPI poll function for a channel
1663 * @napi: NAPI structure for the channel
1664 * @budget: Budget supplied by NAPI core
1666 * Return: Number of items polled (<= budget)
1668 * Single transactions completed by hardware are polled until either
1669 * the budget is exhausted, or there are no more. Each transaction
1670 * polled is passed to gsi_trans_complete(), to perform remaining
1671 * completion processing and retire/free the transaction.
1673 static int gsi_channel_poll(struct napi_struct
*napi
, int budget
)
1675 struct gsi_channel
*channel
;
1678 channel
= container_of(napi
, struct gsi_channel
, napi
);
1679 for (count
= 0; count
< budget
; count
++) {
1680 struct gsi_trans
*trans
;
1682 trans
= gsi_channel_poll_one(channel
);
1685 gsi_trans_complete(trans
);
1688 if (count
< budget
&& napi_complete(napi
))
1689 gsi_irq_ieob_enable_one(channel
->gsi
, channel
->evt_ring_id
);
1694 /* The event bitmap represents which event ids are available for allocation.
1695 * Set bits are not available, clear bits can be used. This function
1696 * initializes the map so all events supported by the hardware are available,
1697 * then precludes any reserved events from being allocated.
1699 static u32
gsi_event_bitmap_init(u32 evt_ring_max
)
1701 u32 event_bitmap
= GENMASK(BITS_PER_LONG
- 1, evt_ring_max
);
1703 event_bitmap
|= GENMASK(GSI_MHI_EVENT_ID_END
, GSI_MHI_EVENT_ID_START
);
1705 return event_bitmap
;
1708 /* Setup function for a single channel */
1709 static int gsi_channel_setup_one(struct gsi
*gsi
, u32 channel_id
)
1711 struct gsi_channel
*channel
= &gsi
->channel
[channel_id
];
1712 u32 evt_ring_id
= channel
->evt_ring_id
;
1715 if (!gsi_channel_initialized(channel
))
1718 ret
= gsi_evt_ring_alloc_command(gsi
, evt_ring_id
);
1722 gsi_evt_ring_program(gsi
, evt_ring_id
);
1724 ret
= gsi_channel_alloc_command(gsi
, channel_id
);
1726 goto err_evt_ring_de_alloc
;
1728 gsi_channel_program(channel
, true);
1730 if (channel
->toward_ipa
)
1731 netif_napi_add_tx(gsi
->dummy_dev
, &channel
->napi
,
1734 netif_napi_add(gsi
->dummy_dev
, &channel
->napi
,
1739 err_evt_ring_de_alloc
:
1740 /* We've done nothing with the event ring yet so don't reset */
1741 gsi_evt_ring_de_alloc_command(gsi
, evt_ring_id
);
1746 /* Inverse of gsi_channel_setup_one() */
1747 static void gsi_channel_teardown_one(struct gsi
*gsi
, u32 channel_id
)
1749 struct gsi_channel
*channel
= &gsi
->channel
[channel_id
];
1750 u32 evt_ring_id
= channel
->evt_ring_id
;
1752 if (!gsi_channel_initialized(channel
))
1755 netif_napi_del(&channel
->napi
);
1757 gsi_channel_de_alloc_command(gsi
, channel_id
);
1758 gsi_evt_ring_reset_command(gsi
, evt_ring_id
);
1759 gsi_evt_ring_de_alloc_command(gsi
, evt_ring_id
);
1762 /* We use generic commands only to operate on modem channels. We don't have
1763 * the ability to determine channel state for a modem channel, so we simply
1764 * issue the command and wait for it to complete.
1766 static int gsi_generic_command(struct gsi
*gsi
, u32 channel_id
,
1767 enum gsi_generic_cmd_opcode opcode
,
1770 const struct reg
*reg
;
1775 /* The error global interrupt type is always enabled (until we tear
1776 * down), so we will keep it enabled.
1778 * A generic EE command completes with a GSI global interrupt of
1779 * type GP_INT1. We only perform one generic command at a time
1780 * (to allocate, halt, or enable/disable flow control on a modem
1781 * channel), and only from this function. So we enable the GP_INT1
1782 * IRQ type here, and disable it again after the command completes.
1784 reg
= gsi_reg(gsi
, CNTXT_GLOB_IRQ_EN
);
1785 val
= ERROR_INT
| GP_INT1
;
1786 iowrite32(val
, gsi
->virt
+ reg_offset(reg
));
1788 /* First zero the result code field */
1789 reg
= gsi_reg(gsi
, CNTXT_SCRATCH_0
);
1790 offset
= reg_offset(reg
);
1791 val
= ioread32(gsi
->virt
+ offset
);
1793 val
&= ~reg_fmask(reg
, GENERIC_EE_RESULT
);
1794 iowrite32(val
, gsi
->virt
+ offset
);
1796 /* Now issue the command */
1797 reg
= gsi_reg(gsi
, GENERIC_CMD
);
1798 val
= reg_encode(reg
, GENERIC_OPCODE
, opcode
);
1799 val
|= reg_encode(reg
, GENERIC_CHID
, channel_id
);
1800 val
|= reg_encode(reg
, GENERIC_EE
, GSI_EE_MODEM
);
1801 if (gsi
->version
>= IPA_VERSION_4_11
)
1802 val
|= reg_encode(reg
, GENERIC_PARAMS
, params
);
1804 timeout
= !gsi_command(gsi
, reg_offset(reg
), val
);
1806 /* Disable the GP_INT1 IRQ type again */
1807 reg
= gsi_reg(gsi
, CNTXT_GLOB_IRQ_EN
);
1808 iowrite32(ERROR_INT
, gsi
->virt
+ reg_offset(reg
));
1813 dev_err(gsi
->dev
, "GSI generic command %u to channel %u timed out\n",
1814 opcode
, channel_id
);
1819 static int gsi_modem_channel_alloc(struct gsi
*gsi
, u32 channel_id
)
1821 return gsi_generic_command(gsi
, channel_id
,
1822 GSI_GENERIC_ALLOCATE_CHANNEL
, 0);
1825 static void gsi_modem_channel_halt(struct gsi
*gsi
, u32 channel_id
)
1827 u32 retries
= GSI_CHANNEL_MODEM_HALT_RETRIES
;
1831 ret
= gsi_generic_command(gsi
, channel_id
,
1832 GSI_GENERIC_HALT_CHANNEL
, 0);
1833 while (ret
== -EAGAIN
&& retries
--);
1836 dev_err(gsi
->dev
, "error %d halting modem channel %u\n",
1840 /* Enable or disable flow control for a modem GSI TX channel (IPA v4.2+) */
1842 gsi_modem_channel_flow_control(struct gsi
*gsi
, u32 channel_id
, bool enable
)
1848 command
= enable
? GSI_GENERIC_ENABLE_FLOW_CONTROL
1849 : GSI_GENERIC_DISABLE_FLOW_CONTROL
;
1850 /* Disabling flow control on IPA v4.11+ can return -EAGAIN if enable
1851 * is underway. In this case we need to retry the command.
1853 if (!enable
&& gsi
->version
>= IPA_VERSION_4_11
)
1854 retries
= GSI_CHANNEL_MODEM_FLOW_RETRIES
;
1857 ret
= gsi_generic_command(gsi
, channel_id
, command
, 0);
1858 while (ret
== -EAGAIN
&& retries
--);
1862 "error %d %sabling mode channel %u flow control\n",
1863 ret
, enable
? "en" : "dis", channel_id
);
1866 /* Setup function for channels */
1867 static int gsi_channel_setup(struct gsi
*gsi
)
1873 gsi_irq_enable(gsi
);
1875 mutex_lock(&gsi
->mutex
);
1878 ret
= gsi_channel_setup_one(gsi
, channel_id
);
1881 } while (++channel_id
< gsi
->channel_count
);
1883 /* Make sure no channels were defined that hardware does not support */
1884 while (channel_id
< GSI_CHANNEL_COUNT_MAX
) {
1885 struct gsi_channel
*channel
= &gsi
->channel
[channel_id
++];
1887 if (!gsi_channel_initialized(channel
))
1891 dev_err(gsi
->dev
, "channel %u not supported by hardware\n",
1893 channel_id
= gsi
->channel_count
;
1897 /* Allocate modem channels if necessary */
1898 mask
= gsi
->modem_channel_bitmap
;
1900 u32 modem_channel_id
= __ffs(mask
);
1902 ret
= gsi_modem_channel_alloc(gsi
, modem_channel_id
);
1904 goto err_unwind_modem
;
1906 /* Clear bit from mask only after success (for unwind) */
1907 mask
^= BIT(modem_channel_id
);
1910 mutex_unlock(&gsi
->mutex
);
1915 /* Compute which modem channels need to be deallocated */
1916 mask
^= gsi
->modem_channel_bitmap
;
1918 channel_id
= __fls(mask
);
1920 mask
^= BIT(channel_id
);
1922 gsi_modem_channel_halt(gsi
, channel_id
);
1926 while (channel_id
--)
1927 gsi_channel_teardown_one(gsi
, channel_id
);
1929 mutex_unlock(&gsi
->mutex
);
1931 gsi_irq_disable(gsi
);
1936 /* Inverse of gsi_channel_setup() */
1937 static void gsi_channel_teardown(struct gsi
*gsi
)
1939 u32 mask
= gsi
->modem_channel_bitmap
;
1942 mutex_lock(&gsi
->mutex
);
1945 channel_id
= __fls(mask
);
1947 mask
^= BIT(channel_id
);
1949 gsi_modem_channel_halt(gsi
, channel_id
);
1952 channel_id
= gsi
->channel_count
- 1;
1954 gsi_channel_teardown_one(gsi
, channel_id
);
1955 while (channel_id
--);
1957 mutex_unlock(&gsi
->mutex
);
1959 gsi_irq_disable(gsi
);
1962 /* Turn off all GSI interrupts initially */
1963 static int gsi_irq_setup(struct gsi
*gsi
)
1965 const struct reg
*reg
;
1968 /* Writing 1 indicates IRQ interrupts; 0 would be MSI */
1969 reg
= gsi_reg(gsi
, CNTXT_INTSET
);
1970 iowrite32(reg_bit(reg
, INTYPE
), gsi
->virt
+ reg_offset(reg
));
1972 /* Disable all interrupt types */
1973 gsi_irq_type_update(gsi
, 0);
1975 /* Clear all type-specific interrupt masks */
1976 reg
= gsi_reg(gsi
, CNTXT_SRC_CH_IRQ_MSK
);
1977 iowrite32(0, gsi
->virt
+ reg_offset(reg
));
1979 reg
= gsi_reg(gsi
, CNTXT_SRC_EV_CH_IRQ_MSK
);
1980 iowrite32(0, gsi
->virt
+ reg_offset(reg
));
1982 reg
= gsi_reg(gsi
, CNTXT_GLOB_IRQ_EN
);
1983 iowrite32(0, gsi
->virt
+ reg_offset(reg
));
1985 reg
= gsi_reg(gsi
, CNTXT_SRC_IEOB_IRQ_MSK
);
1986 iowrite32(0, gsi
->virt
+ reg_offset(reg
));
1988 /* The inter-EE interrupts are not supported for IPA v3.0-v3.1 */
1989 if (gsi
->version
> IPA_VERSION_3_1
) {
1990 reg
= gsi_reg(gsi
, INTER_EE_SRC_CH_IRQ_MSK
);
1991 iowrite32(0, gsi
->virt
+ reg_offset(reg
));
1993 reg
= gsi_reg(gsi
, INTER_EE_SRC_EV_CH_IRQ_MSK
);
1994 iowrite32(0, gsi
->virt
+ reg_offset(reg
));
1997 reg
= gsi_reg(gsi
, CNTXT_GSI_IRQ_EN
);
1998 iowrite32(0, gsi
->virt
+ reg_offset(reg
));
2000 ret
= request_irq(gsi
->irq
, gsi_isr
, 0, "gsi", gsi
);
2002 dev_err(gsi
->dev
, "error %d requesting \"gsi\" IRQ\n", ret
);
2007 static void gsi_irq_teardown(struct gsi
*gsi
)
2009 free_irq(gsi
->irq
, gsi
);
2012 /* Get # supported channel and event rings; there is no gsi_ring_teardown() */
2013 static int gsi_ring_setup(struct gsi
*gsi
)
2015 struct device
*dev
= gsi
->dev
;
2016 const struct reg
*reg
;
2020 if (gsi
->version
< IPA_VERSION_3_5_1
) {
2021 /* No HW_PARAM_2 register prior to IPA v3.5.1, assume the max */
2022 gsi
->channel_count
= GSI_CHANNEL_COUNT_MAX
;
2023 gsi
->evt_ring_count
= GSI_EVT_RING_COUNT_MAX
;
2028 reg
= gsi_reg(gsi
, HW_PARAM_2
);
2029 val
= ioread32(gsi
->virt
+ reg_offset(reg
));
2031 count
= reg_decode(reg
, NUM_CH_PER_EE
, val
);
2033 dev_err(dev
, "GSI reports zero channels supported\n");
2036 if (count
> GSI_CHANNEL_COUNT_MAX
) {
2037 dev_warn(dev
, "limiting to %u channels; hardware supports %u\n",
2038 GSI_CHANNEL_COUNT_MAX
, count
);
2039 count
= GSI_CHANNEL_COUNT_MAX
;
2041 gsi
->channel_count
= count
;
2043 if (gsi
->version
< IPA_VERSION_5_0
) {
2044 count
= reg_decode(reg
, NUM_EV_PER_EE
, val
);
2046 reg
= gsi_reg(gsi
, HW_PARAM_4
);
2047 count
= reg_decode(reg
, EV_PER_EE
, val
);
2050 dev_err(dev
, "GSI reports zero event rings supported\n");
2053 if (count
> GSI_EVT_RING_COUNT_MAX
) {
2055 "limiting to %u event rings; hardware supports %u\n",
2056 GSI_EVT_RING_COUNT_MAX
, count
);
2057 count
= GSI_EVT_RING_COUNT_MAX
;
2059 gsi
->evt_ring_count
= count
;
2064 /* Setup function for GSI. GSI firmware must be loaded and initialized */
2065 int gsi_setup(struct gsi
*gsi
)
2067 const struct reg
*reg
;
2071 /* Here is where we first touch the GSI hardware */
2072 reg
= gsi_reg(gsi
, GSI_STATUS
);
2073 val
= ioread32(gsi
->virt
+ reg_offset(reg
));
2074 if (!(val
& reg_bit(reg
, ENABLED
))) {
2075 dev_err(gsi
->dev
, "GSI has not been enabled\n");
2079 ret
= gsi_irq_setup(gsi
);
2083 ret
= gsi_ring_setup(gsi
); /* No matching teardown required */
2085 goto err_irq_teardown
;
2087 /* Initialize the error log */
2088 reg
= gsi_reg(gsi
, ERROR_LOG
);
2089 iowrite32(0, gsi
->virt
+ reg_offset(reg
));
2091 ret
= gsi_channel_setup(gsi
);
2093 goto err_irq_teardown
;
2098 gsi_irq_teardown(gsi
);
2103 /* Inverse of gsi_setup() */
2104 void gsi_teardown(struct gsi
*gsi
)
2106 gsi_channel_teardown(gsi
);
2107 gsi_irq_teardown(gsi
);
2110 /* Initialize a channel's event ring */
2111 static int gsi_channel_evt_ring_init(struct gsi_channel
*channel
)
2113 struct gsi
*gsi
= channel
->gsi
;
2114 struct gsi_evt_ring
*evt_ring
;
2117 ret
= gsi_evt_ring_id_alloc(gsi
);
2120 channel
->evt_ring_id
= ret
;
2122 evt_ring
= &gsi
->evt_ring
[channel
->evt_ring_id
];
2123 evt_ring
->channel
= channel
;
2125 ret
= gsi_ring_alloc(gsi
, &evt_ring
->ring
, channel
->event_count
);
2127 return 0; /* Success! */
2129 dev_err(gsi
->dev
, "error %d allocating channel %u event ring\n",
2130 ret
, gsi_channel_id(channel
));
2132 gsi_evt_ring_id_free(gsi
, channel
->evt_ring_id
);
2137 /* Inverse of gsi_channel_evt_ring_init() */
2138 static void gsi_channel_evt_ring_exit(struct gsi_channel
*channel
)
2140 u32 evt_ring_id
= channel
->evt_ring_id
;
2141 struct gsi
*gsi
= channel
->gsi
;
2142 struct gsi_evt_ring
*evt_ring
;
2144 evt_ring
= &gsi
->evt_ring
[evt_ring_id
];
2145 gsi_ring_free(gsi
, &evt_ring
->ring
);
2146 gsi_evt_ring_id_free(gsi
, evt_ring_id
);
2149 static bool gsi_channel_data_valid(struct gsi
*gsi
, bool command
,
2150 const struct ipa_gsi_endpoint_data
*data
)
2152 const struct gsi_channel_data
*channel_data
;
2153 u32 channel_id
= data
->channel_id
;
2154 struct device
*dev
= gsi
->dev
;
2156 /* Make sure channel ids are in the range driver supports */
2157 if (channel_id
>= GSI_CHANNEL_COUNT_MAX
) {
2158 dev_err(dev
, "bad channel id %u; must be less than %u\n",
2159 channel_id
, GSI_CHANNEL_COUNT_MAX
);
2163 if (data
->ee_id
!= GSI_EE_AP
&& data
->ee_id
!= GSI_EE_MODEM
) {
2164 dev_err(dev
, "bad EE id %u; not AP or modem\n", data
->ee_id
);
2168 if (command
&& !data
->toward_ipa
) {
2169 dev_err(dev
, "command channel %u is not TX\n", channel_id
);
2173 channel_data
= &data
->channel
;
2175 if (!channel_data
->tlv_count
||
2176 channel_data
->tlv_count
> GSI_TLV_MAX
) {
2177 dev_err(dev
, "channel %u bad tlv_count %u; must be 1..%u\n",
2178 channel_id
, channel_data
->tlv_count
, GSI_TLV_MAX
);
2182 if (command
&& IPA_COMMAND_TRANS_TRE_MAX
> channel_data
->tlv_count
) {
2183 dev_err(dev
, "command TRE max too big for channel %u (%u > %u)\n",
2184 channel_id
, IPA_COMMAND_TRANS_TRE_MAX
,
2185 channel_data
->tlv_count
);
2189 /* We have to allow at least one maximally-sized transaction to
2190 * be outstanding (which would use tlv_count TREs). Given how
2191 * gsi_channel_tre_max() is computed, tre_count has to be almost
2192 * twice the TLV FIFO size to satisfy this requirement.
2194 if (channel_data
->tre_count
< 2 * channel_data
->tlv_count
- 1) {
2195 dev_err(dev
, "channel %u TLV count %u exceeds TRE count %u\n",
2196 channel_id
, channel_data
->tlv_count
,
2197 channel_data
->tre_count
);
2201 if (!is_power_of_2(channel_data
->tre_count
)) {
2202 dev_err(dev
, "channel %u bad tre_count %u; not power of 2\n",
2203 channel_id
, channel_data
->tre_count
);
2207 if (!is_power_of_2(channel_data
->event_count
)) {
2208 dev_err(dev
, "channel %u bad event_count %u; not power of 2\n",
2209 channel_id
, channel_data
->event_count
);
2216 /* Init function for a single channel */
2217 static int gsi_channel_init_one(struct gsi
*gsi
,
2218 const struct ipa_gsi_endpoint_data
*data
,
2221 struct gsi_channel
*channel
;
2225 if (!gsi_channel_data_valid(gsi
, command
, data
))
2228 /* Worst case we need an event for every outstanding TRE */
2229 if (data
->channel
.tre_count
> data
->channel
.event_count
) {
2230 tre_count
= data
->channel
.event_count
;
2231 dev_warn(gsi
->dev
, "channel %u limited to %u TREs\n",
2232 data
->channel_id
, tre_count
);
2234 tre_count
= data
->channel
.tre_count
;
2237 channel
= &gsi
->channel
[data
->channel_id
];
2238 memset(channel
, 0, sizeof(*channel
));
2241 channel
->toward_ipa
= data
->toward_ipa
;
2242 channel
->command
= command
;
2243 channel
->trans_tre_max
= data
->channel
.tlv_count
;
2244 channel
->tre_count
= tre_count
;
2245 channel
->event_count
= data
->channel
.event_count
;
2247 ret
= gsi_channel_evt_ring_init(channel
);
2251 ret
= gsi_ring_alloc(gsi
, &channel
->tre_ring
, data
->channel
.tre_count
);
2253 dev_err(gsi
->dev
, "error %d allocating channel %u ring\n",
2254 ret
, data
->channel_id
);
2255 goto err_channel_evt_ring_exit
;
2258 ret
= gsi_channel_trans_init(gsi
, data
->channel_id
);
2263 u32 tre_max
= gsi_channel_tre_max(gsi
, data
->channel_id
);
2265 ret
= ipa_cmd_pool_init(channel
, tre_max
);
2268 return 0; /* Success! */
2270 gsi_channel_trans_exit(channel
);
2272 gsi_ring_free(gsi
, &channel
->tre_ring
);
2273 err_channel_evt_ring_exit
:
2274 gsi_channel_evt_ring_exit(channel
);
2276 channel
->gsi
= NULL
; /* Mark it not (fully) initialized */
2281 /* Inverse of gsi_channel_init_one() */
2282 static void gsi_channel_exit_one(struct gsi_channel
*channel
)
2284 if (!gsi_channel_initialized(channel
))
2287 if (channel
->command
)
2288 ipa_cmd_pool_exit(channel
);
2289 gsi_channel_trans_exit(channel
);
2290 gsi_ring_free(channel
->gsi
, &channel
->tre_ring
);
2291 gsi_channel_evt_ring_exit(channel
);
2294 /* Init function for channels */
2295 static int gsi_channel_init(struct gsi
*gsi
, u32 count
,
2296 const struct ipa_gsi_endpoint_data
*data
)
2302 /* IPA v4.2 requires the AP to allocate channels for the modem */
2303 modem_alloc
= gsi
->version
== IPA_VERSION_4_2
;
2305 gsi
->event_bitmap
= gsi_event_bitmap_init(GSI_EVT_RING_COUNT_MAX
);
2306 gsi
->ieob_enabled_bitmap
= 0;
2308 /* The endpoint data array is indexed by endpoint name */
2309 for (i
= 0; i
< count
; i
++) {
2310 bool command
= i
== IPA_ENDPOINT_AP_COMMAND_TX
;
2312 if (ipa_gsi_endpoint_data_empty(&data
[i
]))
2313 continue; /* Skip over empty slots */
2315 /* Mark modem channels to be allocated (hardware workaround) */
2316 if (data
[i
].ee_id
== GSI_EE_MODEM
) {
2318 gsi
->modem_channel_bitmap
|=
2319 BIT(data
[i
].channel_id
);
2323 ret
= gsi_channel_init_one(gsi
, &data
[i
], command
);
2332 if (ipa_gsi_endpoint_data_empty(&data
[i
]))
2334 if (modem_alloc
&& data
[i
].ee_id
== GSI_EE_MODEM
) {
2335 gsi
->modem_channel_bitmap
&= ~BIT(data
[i
].channel_id
);
2338 gsi_channel_exit_one(&gsi
->channel
[data
->channel_id
]);
2344 /* Inverse of gsi_channel_init() */
2345 static void gsi_channel_exit(struct gsi
*gsi
)
2347 u32 channel_id
= GSI_CHANNEL_COUNT_MAX
- 1;
2350 gsi_channel_exit_one(&gsi
->channel
[channel_id
]);
2351 while (channel_id
--);
2352 gsi
->modem_channel_bitmap
= 0;
2355 /* Init function for GSI. GSI hardware does not need to be "ready" */
2356 int gsi_init(struct gsi
*gsi
, struct platform_device
*pdev
,
2357 enum ipa_version version
, u32 count
,
2358 const struct ipa_gsi_endpoint_data
*data
)
2362 gsi_validate_build();
2364 gsi
->dev
= &pdev
->dev
;
2365 gsi
->version
= version
;
2367 /* GSI uses NAPI on all channels. Create a dummy network device
2368 * for the channel NAPI contexts to be associated with.
2370 gsi
->dummy_dev
= alloc_netdev_dummy(0);
2371 if (!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
);
2392 free_netdev(gsi
->dummy_dev
);
2398 /* Inverse of gsi_init() */
2399 void gsi_exit(struct gsi
*gsi
)
2401 mutex_destroy(&gsi
->mutex
);
2402 gsi_channel_exit(gsi
);
2403 free_netdev(gsi
->dummy_dev
);
2407 /* The maximum number of outstanding TREs on a channel. This limits
2408 * a channel's maximum number of transactions outstanding (worst case
2409 * is one TRE per transaction).
2411 * The absolute limit is the number of TREs in the channel's TRE ring,
2412 * and in theory we should be able use all of them. But in practice,
2413 * doing that led to the hardware reporting exhaustion of event ring
2414 * slots for writing completion information. So the hardware limit
2415 * would be (tre_count - 1).
2417 * We reduce it a bit further though. Transaction resource pools are
2418 * sized to be a little larger than this maximum, to allow resource
2419 * allocations to always be contiguous. The number of entries in a
2420 * TRE ring buffer is a power of 2, and the extra resources in a pool
2421 * tends to nearly double the memory allocated for it. Reducing the
2422 * maximum number of outstanding TREs allows the number of entries in
2423 * a pool to avoid crossing that power-of-2 boundary, and this can
2424 * substantially reduce pool memory requirements. The number we
2425 * reduce it by matches the number added in gsi_trans_pool_init().
2427 u32
gsi_channel_tre_max(struct gsi
*gsi
, u32 channel_id
)
2429 struct gsi_channel
*channel
= &gsi
->channel
[channel_id
];
2431 /* Hardware limit is channel->tre_count - 1 */
2432 return channel
->tre_count
- (channel
->trans_tre_max
- 1);