1650d1603SAlex Elder // SPDX-License-Identifier: GPL-2.0 2650d1603SAlex Elder 3650d1603SAlex Elder /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved. 4650d1603SAlex Elder * Copyright (C) 2018-2020 Linaro Ltd. 5650d1603SAlex Elder */ 6650d1603SAlex Elder 7650d1603SAlex Elder #include <linux/types.h> 8650d1603SAlex Elder #include <linux/bits.h> 9650d1603SAlex Elder #include <linux/bitfield.h> 10650d1603SAlex Elder #include <linux/mutex.h> 11650d1603SAlex Elder #include <linux/completion.h> 12650d1603SAlex Elder #include <linux/io.h> 13650d1603SAlex Elder #include <linux/bug.h> 14650d1603SAlex Elder #include <linux/interrupt.h> 15650d1603SAlex Elder #include <linux/platform_device.h> 16650d1603SAlex Elder #include <linux/netdevice.h> 17650d1603SAlex Elder 18650d1603SAlex Elder #include "gsi.h" 19650d1603SAlex Elder #include "gsi_reg.h" 20650d1603SAlex Elder #include "gsi_private.h" 21650d1603SAlex Elder #include "gsi_trans.h" 22650d1603SAlex Elder #include "ipa_gsi.h" 23650d1603SAlex Elder #include "ipa_data.h" 241d0c09deSAlex Elder #include "ipa_version.h" 25650d1603SAlex Elder 26650d1603SAlex Elder /** 27650d1603SAlex Elder * DOC: The IPA Generic Software Interface 28650d1603SAlex Elder * 29650d1603SAlex Elder * The generic software interface (GSI) is an integral component of the IPA, 30650d1603SAlex Elder * providing a well-defined communication layer between the AP subsystem 31650d1603SAlex Elder * and the IPA core. The modem uses the GSI layer as well. 32650d1603SAlex Elder * 33650d1603SAlex Elder * -------- --------- 34650d1603SAlex Elder * | | | | 35650d1603SAlex Elder * | AP +<---. .----+ Modem | 36650d1603SAlex Elder * | +--. | | .->+ | 37650d1603SAlex Elder * | | | | | | | | 38650d1603SAlex Elder * -------- | | | | --------- 39650d1603SAlex Elder * v | v | 40650d1603SAlex Elder * --+-+---+-+-- 41650d1603SAlex Elder * | GSI | 42650d1603SAlex Elder * |-----------| 43650d1603SAlex Elder * | | 44650d1603SAlex Elder * | IPA | 45650d1603SAlex Elder * | | 46650d1603SAlex Elder * ------------- 47650d1603SAlex Elder * 48650d1603SAlex Elder * In the above diagram, the AP and Modem represent "execution environments" 49650d1603SAlex Elder * (EEs), which are independent operating environments that use the IPA for 50650d1603SAlex Elder * data transfer. 51650d1603SAlex Elder * 52650d1603SAlex Elder * Each EE uses a set of unidirectional GSI "channels," which allow transfer 53650d1603SAlex Elder * of data to or from the IPA. A channel is implemented as a ring buffer, 54650d1603SAlex Elder * with a DRAM-resident array of "transfer elements" (TREs) available to 55650d1603SAlex Elder * describe transfers to or from other EEs through the IPA. A transfer 56650d1603SAlex Elder * element can also contain an immediate command, requesting the IPA perform 57650d1603SAlex Elder * actions other than data transfer. 58650d1603SAlex Elder * 59650d1603SAlex Elder * Each TRE refers to a block of data--also located DRAM. After writing one 60650d1603SAlex Elder * or more TREs to a channel, the writer (either the IPA or an EE) writes a 61650d1603SAlex Elder * doorbell register to inform the receiving side how many elements have 62650d1603SAlex Elder * been written. 63650d1603SAlex Elder * 64650d1603SAlex Elder * Each channel has a GSI "event ring" associated with it. An event ring 65650d1603SAlex Elder * is implemented very much like a channel ring, but is always directed from 66650d1603SAlex Elder * the IPA to an EE. The IPA notifies an EE (such as the AP) about channel 67650d1603SAlex Elder * events by adding an entry to the event ring associated with the channel. 68650d1603SAlex Elder * The GSI then writes its doorbell for the event ring, causing the target 69650d1603SAlex Elder * EE to be interrupted. Each entry in an event ring contains a pointer 70650d1603SAlex Elder * to the channel TRE whose completion the event represents. 71650d1603SAlex Elder * 72650d1603SAlex Elder * Each TRE in a channel ring has a set of flags. One flag indicates whether 73650d1603SAlex Elder * the completion of the transfer operation generates an entry (and possibly 74650d1603SAlex Elder * an interrupt) in the channel's event ring. Other flags allow transfer 75650d1603SAlex Elder * elements to be chained together, forming a single logical transaction. 76650d1603SAlex Elder * TRE flags are used to control whether and when interrupts are generated 77650d1603SAlex Elder * to signal completion of channel transfers. 78650d1603SAlex Elder * 79650d1603SAlex Elder * Elements in channel and event rings are completed (or consumed) strictly 80650d1603SAlex Elder * in order. Completion of one entry implies the completion of all preceding 81650d1603SAlex Elder * entries. A single completion interrupt can therefore communicate the 82650d1603SAlex Elder * completion of many transfers. 83650d1603SAlex Elder * 84650d1603SAlex Elder * Note that all GSI registers are little-endian, which is the assumed 85650d1603SAlex Elder * endianness of I/O space accesses. The accessor functions perform byte 86650d1603SAlex Elder * swapping if needed (i.e., for a big endian CPU). 87650d1603SAlex Elder */ 88650d1603SAlex Elder 89650d1603SAlex Elder /* Delay period for interrupt moderation (in 32KHz IPA internal timer ticks) */ 90650d1603SAlex Elder #define GSI_EVT_RING_INT_MODT (32 * 1) /* 1ms under 32KHz clock */ 91650d1603SAlex Elder 92650d1603SAlex Elder #define GSI_CMD_TIMEOUT 5 /* seconds */ 93650d1603SAlex Elder 94650d1603SAlex Elder #define GSI_CHANNEL_STOP_RX_RETRIES 10 9511361456SAlex Elder #define GSI_CHANNEL_MODEM_HALT_RETRIES 10 96650d1603SAlex Elder 97650d1603SAlex Elder #define GSI_MHI_EVENT_ID_START 10 /* 1st reserved event id */ 98650d1603SAlex Elder #define GSI_MHI_EVENT_ID_END 16 /* Last reserved event id */ 99650d1603SAlex Elder 100650d1603SAlex Elder #define GSI_ISR_MAX_ITER 50 /* Detect interrupt storms */ 101650d1603SAlex Elder 102650d1603SAlex Elder /* An entry in an event ring */ 103650d1603SAlex Elder struct gsi_event { 104650d1603SAlex Elder __le64 xfer_ptr; 105650d1603SAlex Elder __le16 len; 106650d1603SAlex Elder u8 reserved1; 107650d1603SAlex Elder u8 code; 108650d1603SAlex Elder __le16 reserved2; 109650d1603SAlex Elder u8 type; 110650d1603SAlex Elder u8 chid; 111650d1603SAlex Elder }; 112650d1603SAlex Elder 113650d1603SAlex Elder /** gsi_channel_scratch_gpi - GPI protocol scratch register 114650d1603SAlex Elder * @max_outstanding_tre: 115650d1603SAlex Elder * Defines the maximum number of TREs allowed in a single transaction 116650d1603SAlex Elder * on a channel (in bytes). This determines the amount of prefetch 117650d1603SAlex Elder * performed by the hardware. We configure this to equal the size of 118650d1603SAlex Elder * the TLV FIFO for the channel. 119650d1603SAlex Elder * @outstanding_threshold: 120650d1603SAlex Elder * Defines the threshold (in bytes) determining when the sequencer 121650d1603SAlex Elder * should update the channel doorbell. We configure this to equal 122650d1603SAlex Elder * the size of two TREs. 123650d1603SAlex Elder */ 124650d1603SAlex Elder struct gsi_channel_scratch_gpi { 125650d1603SAlex Elder u64 reserved1; 126650d1603SAlex Elder u16 reserved2; 127650d1603SAlex Elder u16 max_outstanding_tre; 128650d1603SAlex Elder u16 reserved3; 129650d1603SAlex Elder u16 outstanding_threshold; 130650d1603SAlex Elder }; 131650d1603SAlex Elder 132650d1603SAlex Elder /** gsi_channel_scratch - channel scratch configuration area 133650d1603SAlex Elder * 134650d1603SAlex Elder * The exact interpretation of this register is protocol-specific. 135650d1603SAlex Elder * We only use GPI channels; see struct gsi_channel_scratch_gpi, above. 136650d1603SAlex Elder */ 137650d1603SAlex Elder union gsi_channel_scratch { 138650d1603SAlex Elder struct gsi_channel_scratch_gpi gpi; 139650d1603SAlex Elder struct { 140650d1603SAlex Elder u32 word1; 141650d1603SAlex Elder u32 word2; 142650d1603SAlex Elder u32 word3; 143650d1603SAlex Elder u32 word4; 144650d1603SAlex Elder } data; 145650d1603SAlex Elder }; 146650d1603SAlex Elder 147650d1603SAlex Elder /* Check things that can be validated at build time. */ 148650d1603SAlex Elder static void gsi_validate_build(void) 149650d1603SAlex Elder { 150650d1603SAlex Elder /* This is used as a divisor */ 151650d1603SAlex Elder BUILD_BUG_ON(!GSI_RING_ELEMENT_SIZE); 152650d1603SAlex Elder 153650d1603SAlex Elder /* Code assumes the size of channel and event ring element are 154650d1603SAlex Elder * the same (and fixed). Make sure the size of an event ring 155650d1603SAlex Elder * element is what's expected. 156650d1603SAlex Elder */ 157650d1603SAlex Elder BUILD_BUG_ON(sizeof(struct gsi_event) != GSI_RING_ELEMENT_SIZE); 158650d1603SAlex Elder 159650d1603SAlex Elder /* Hardware requires a 2^n ring size. We ensure the number of 160650d1603SAlex Elder * elements in an event ring is a power of 2 elsewhere; this 161650d1603SAlex Elder * ensure the elements themselves meet the requirement. 162650d1603SAlex Elder */ 163650d1603SAlex Elder BUILD_BUG_ON(!is_power_of_2(GSI_RING_ELEMENT_SIZE)); 164650d1603SAlex Elder 165650d1603SAlex Elder /* The channel element size must fit in this field */ 166650d1603SAlex Elder BUILD_BUG_ON(GSI_RING_ELEMENT_SIZE > field_max(ELEMENT_SIZE_FMASK)); 167650d1603SAlex Elder 168650d1603SAlex Elder /* The event ring element size must fit in this field */ 169650d1603SAlex Elder BUILD_BUG_ON(GSI_RING_ELEMENT_SIZE > field_max(EV_ELEMENT_SIZE_FMASK)); 170650d1603SAlex Elder } 171650d1603SAlex Elder 172650d1603SAlex Elder /* Return the channel id associated with a given channel */ 173650d1603SAlex Elder static u32 gsi_channel_id(struct gsi_channel *channel) 174650d1603SAlex Elder { 175650d1603SAlex Elder return channel - &channel->gsi->channel[0]; 176650d1603SAlex Elder } 177650d1603SAlex Elder 1783ca97ffdSAlex Elder /* Update the GSI IRQ type register with the cached value */ 1798194be79SAlex Elder static void gsi_irq_type_update(struct gsi *gsi, u32 val) 1803ca97ffdSAlex Elder { 1818194be79SAlex Elder gsi->type_enabled_bitmap = val; 1828194be79SAlex Elder iowrite32(val, gsi->virt + GSI_CNTXT_TYPE_IRQ_MSK_OFFSET); 1833ca97ffdSAlex Elder } 1843ca97ffdSAlex Elder 185b054d4f9SAlex Elder static void gsi_irq_type_enable(struct gsi *gsi, enum gsi_irq_type_id type_id) 186b054d4f9SAlex Elder { 1878194be79SAlex Elder gsi_irq_type_update(gsi, gsi->type_enabled_bitmap | BIT(type_id)); 188b054d4f9SAlex Elder } 189b054d4f9SAlex Elder 190b054d4f9SAlex Elder static void gsi_irq_type_disable(struct gsi *gsi, enum gsi_irq_type_id type_id) 191b054d4f9SAlex Elder { 1928194be79SAlex Elder gsi_irq_type_update(gsi, gsi->type_enabled_bitmap & ~BIT(type_id)); 193b054d4f9SAlex Elder } 194b054d4f9SAlex Elder 19597eb94c8SAlex Elder /* Turn off all GSI interrupts initially */ 19697eb94c8SAlex Elder static void gsi_irq_setup(struct gsi *gsi) 19797eb94c8SAlex Elder { 198cdeee49fSAlex Elder u32 adjust; 199cdeee49fSAlex Elder 2008194be79SAlex Elder /* Disable all interrupt types */ 2018194be79SAlex Elder gsi_irq_type_update(gsi, 0); 202b054d4f9SAlex Elder 2038194be79SAlex Elder /* Clear all type-specific interrupt masks */ 204b054d4f9SAlex Elder iowrite32(0, gsi->virt + GSI_CNTXT_SRC_CH_IRQ_MSK_OFFSET); 205b4175f87SAlex Elder iowrite32(0, gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_MSK_OFFSET); 206d6c9e3f5SAlex Elder iowrite32(0, gsi->virt + GSI_CNTXT_GLOB_IRQ_EN_OFFSET); 20706c86328SAlex Elder iowrite32(0, gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_MSK_OFFSET); 208cdeee49fSAlex Elder 209cdeee49fSAlex Elder /* Reverse the offset adjustment for inter-EE register offsets */ 210cdeee49fSAlex Elder adjust = gsi->version < IPA_VERSION_4_5 ? 0 : GSI_EE_REG_ADJUST; 211cdeee49fSAlex Elder iowrite32(0, gsi->virt + adjust + GSI_INTER_EE_SRC_CH_IRQ_OFFSET); 212cdeee49fSAlex Elder iowrite32(0, gsi->virt + adjust + GSI_INTER_EE_SRC_EV_CH_IRQ_OFFSET); 213cdeee49fSAlex Elder 214352f26a8SAlex Elder iowrite32(0, gsi->virt + GSI_CNTXT_GSI_IRQ_EN_OFFSET); 21597eb94c8SAlex Elder } 21697eb94c8SAlex Elder 21797eb94c8SAlex Elder /* Turn off all GSI interrupts when we're all done */ 21897eb94c8SAlex Elder static void gsi_irq_teardown(struct gsi *gsi) 21997eb94c8SAlex Elder { 2208194be79SAlex Elder /* Nothing to do */ 22197eb94c8SAlex Elder } 22297eb94c8SAlex Elder 223a60d0632SAlex Elder /* Event ring commands are performed one at a time. Their completion 224a60d0632SAlex Elder * is signaled by the event ring control GSI interrupt type, which is 225a60d0632SAlex Elder * only enabled when we issue an event ring command. Only the event 226a60d0632SAlex Elder * ring being operated on has this interrupt enabled. 227a60d0632SAlex Elder */ 228a60d0632SAlex Elder static void gsi_irq_ev_ctrl_enable(struct gsi *gsi, u32 evt_ring_id) 229a60d0632SAlex Elder { 230a60d0632SAlex Elder u32 val = BIT(evt_ring_id); 231a60d0632SAlex Elder 232a60d0632SAlex Elder /* There's a small chance that a previous command completed 233a60d0632SAlex Elder * after the interrupt was disabled, so make sure we have no 234a60d0632SAlex Elder * pending interrupts before we enable them. 235a60d0632SAlex Elder */ 236a60d0632SAlex Elder iowrite32(~0, gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_CLR_OFFSET); 237a60d0632SAlex Elder 238a60d0632SAlex Elder iowrite32(val, gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_MSK_OFFSET); 239a60d0632SAlex Elder gsi_irq_type_enable(gsi, GSI_EV_CTRL); 240a60d0632SAlex Elder } 241a60d0632SAlex Elder 242a60d0632SAlex Elder /* Disable event ring control interrupts */ 243a60d0632SAlex Elder static void gsi_irq_ev_ctrl_disable(struct gsi *gsi) 244a60d0632SAlex Elder { 245a60d0632SAlex Elder gsi_irq_type_disable(gsi, GSI_EV_CTRL); 246a60d0632SAlex Elder iowrite32(0, gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_MSK_OFFSET); 247a60d0632SAlex Elder } 248a60d0632SAlex Elder 249a60d0632SAlex Elder /* Channel commands are performed one at a time. Their completion is 250a60d0632SAlex Elder * signaled by the channel control GSI interrupt type, which is only 251a60d0632SAlex Elder * enabled when we issue a channel command. Only the channel being 252a60d0632SAlex Elder * operated on has this interrupt enabled. 253a60d0632SAlex Elder */ 254a60d0632SAlex Elder static void gsi_irq_ch_ctrl_enable(struct gsi *gsi, u32 channel_id) 255a60d0632SAlex Elder { 256a60d0632SAlex Elder u32 val = BIT(channel_id); 257a60d0632SAlex Elder 258a60d0632SAlex Elder /* There's a small chance that a previous command completed 259a60d0632SAlex Elder * after the interrupt was disabled, so make sure we have no 260a60d0632SAlex Elder * pending interrupts before we enable them. 261a60d0632SAlex Elder */ 262a60d0632SAlex Elder iowrite32(~0, gsi->virt + GSI_CNTXT_SRC_CH_IRQ_CLR_OFFSET); 263a60d0632SAlex Elder 264a60d0632SAlex Elder iowrite32(val, gsi->virt + GSI_CNTXT_SRC_CH_IRQ_MSK_OFFSET); 265a60d0632SAlex Elder gsi_irq_type_enable(gsi, GSI_CH_CTRL); 266a60d0632SAlex Elder } 267a60d0632SAlex Elder 268a60d0632SAlex Elder /* Disable channel control interrupts */ 269a60d0632SAlex Elder static void gsi_irq_ch_ctrl_disable(struct gsi *gsi) 270a60d0632SAlex Elder { 271a60d0632SAlex Elder gsi_irq_type_disable(gsi, GSI_CH_CTRL); 272a60d0632SAlex Elder iowrite32(0, gsi->virt + GSI_CNTXT_SRC_CH_IRQ_MSK_OFFSET); 273a60d0632SAlex Elder } 274a60d0632SAlex Elder 275650d1603SAlex Elder static void gsi_irq_ieob_enable(struct gsi *gsi, u32 evt_ring_id) 276650d1603SAlex Elder { 27706c86328SAlex Elder bool enable_ieob = !gsi->ieob_enabled_bitmap; 278650d1603SAlex Elder u32 val; 279650d1603SAlex Elder 280a054539dSAlex Elder gsi->ieob_enabled_bitmap |= BIT(evt_ring_id); 281a054539dSAlex Elder val = gsi->ieob_enabled_bitmap; 282650d1603SAlex Elder iowrite32(val, gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_MSK_OFFSET); 28306c86328SAlex Elder 28406c86328SAlex Elder /* Enable the interrupt type if this is the first channel enabled */ 28506c86328SAlex Elder if (enable_ieob) 28606c86328SAlex Elder gsi_irq_type_enable(gsi, GSI_IEOB); 287650d1603SAlex Elder } 288650d1603SAlex Elder 289650d1603SAlex Elder static void gsi_irq_ieob_disable(struct gsi *gsi, u32 evt_ring_id) 290650d1603SAlex Elder { 291650d1603SAlex Elder u32 val; 292650d1603SAlex Elder 293a054539dSAlex Elder gsi->ieob_enabled_bitmap &= ~BIT(evt_ring_id); 29406c86328SAlex Elder 29506c86328SAlex Elder /* Disable the interrupt type if this was the last enabled channel */ 29606c86328SAlex Elder if (!gsi->ieob_enabled_bitmap) 29706c86328SAlex Elder gsi_irq_type_disable(gsi, GSI_IEOB); 29806c86328SAlex Elder 299a054539dSAlex Elder val = gsi->ieob_enabled_bitmap; 300650d1603SAlex Elder iowrite32(val, gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_MSK_OFFSET); 301650d1603SAlex Elder } 302650d1603SAlex Elder 303650d1603SAlex Elder /* Enable all GSI_interrupt types */ 304650d1603SAlex Elder static void gsi_irq_enable(struct gsi *gsi) 305650d1603SAlex Elder { 306650d1603SAlex Elder u32 val; 307650d1603SAlex Elder 308d6c9e3f5SAlex Elder /* Global interrupts include hardware error reports. Enable 309d6c9e3f5SAlex Elder * that so we can at least report the error should it occur. 310d6c9e3f5SAlex Elder */ 3116c6358ccSAlex Elder iowrite32(BIT(ERROR_INT), gsi->virt + GSI_CNTXT_GLOB_IRQ_EN_OFFSET); 3128194be79SAlex Elder gsi_irq_type_update(gsi, gsi->type_enabled_bitmap | BIT(GSI_GLOB_EE)); 313d6c9e3f5SAlex Elder 314352f26a8SAlex Elder /* General GSI interrupts are reported to all EEs; if they occur 315352f26a8SAlex Elder * they are unrecoverable (without reset). A breakpoint interrupt 316352f26a8SAlex Elder * also exists, but we don't support that. We want to be notified 317352f26a8SAlex Elder * of errors so we can report them, even if they can't be handled. 318352f26a8SAlex Elder */ 3196c6358ccSAlex Elder val = BIT(BUS_ERROR); 3206c6358ccSAlex Elder val |= BIT(CMD_FIFO_OVRFLOW); 3216c6358ccSAlex Elder val |= BIT(MCS_STACK_OVRFLOW); 322650d1603SAlex Elder iowrite32(val, gsi->virt + GSI_CNTXT_GSI_IRQ_EN_OFFSET); 3238194be79SAlex Elder gsi_irq_type_update(gsi, gsi->type_enabled_bitmap | BIT(GSI_GENERAL)); 324650d1603SAlex Elder } 325650d1603SAlex Elder 3263ca97ffdSAlex Elder /* Disable all GSI interrupt types */ 327650d1603SAlex Elder static void gsi_irq_disable(struct gsi *gsi) 328650d1603SAlex Elder { 3298194be79SAlex Elder gsi_irq_type_update(gsi, 0); 33097eb94c8SAlex Elder 3318194be79SAlex Elder /* Clear the type-specific interrupt masks set by gsi_irq_enable() */ 332650d1603SAlex Elder iowrite32(0, gsi->virt + GSI_CNTXT_GSI_IRQ_EN_OFFSET); 333d6c9e3f5SAlex Elder iowrite32(0, gsi->virt + GSI_CNTXT_GLOB_IRQ_EN_OFFSET); 334650d1603SAlex Elder } 335650d1603SAlex Elder 336650d1603SAlex Elder /* Return the virtual address associated with a ring index */ 337650d1603SAlex Elder void *gsi_ring_virt(struct gsi_ring *ring, u32 index) 338650d1603SAlex Elder { 339650d1603SAlex Elder /* Note: index *must* be used modulo the ring count here */ 340650d1603SAlex Elder return ring->virt + (index % ring->count) * GSI_RING_ELEMENT_SIZE; 341650d1603SAlex Elder } 342650d1603SAlex Elder 343650d1603SAlex Elder /* Return the 32-bit DMA address associated with a ring index */ 344650d1603SAlex Elder static u32 gsi_ring_addr(struct gsi_ring *ring, u32 index) 345650d1603SAlex Elder { 346650d1603SAlex Elder return (ring->addr & GENMASK(31, 0)) + index * GSI_RING_ELEMENT_SIZE; 347650d1603SAlex Elder } 348650d1603SAlex Elder 349650d1603SAlex Elder /* Return the ring index of a 32-bit ring offset */ 350650d1603SAlex Elder static u32 gsi_ring_index(struct gsi_ring *ring, u32 offset) 351650d1603SAlex Elder { 352650d1603SAlex Elder return (offset - gsi_ring_addr(ring, 0)) / GSI_RING_ELEMENT_SIZE; 353650d1603SAlex Elder } 354650d1603SAlex Elder 355650d1603SAlex Elder /* Issue a GSI command by writing a value to a register, then wait for 356650d1603SAlex Elder * completion to be signaled. Returns true if the command completes 357650d1603SAlex Elder * or false if it times out. 358650d1603SAlex Elder */ 359650d1603SAlex Elder static bool 360650d1603SAlex Elder gsi_command(struct gsi *gsi, u32 reg, u32 val, struct completion *completion) 361650d1603SAlex Elder { 362650d1603SAlex Elder reinit_completion(completion); 363650d1603SAlex Elder 364650d1603SAlex Elder iowrite32(val, gsi->virt + reg); 365650d1603SAlex Elder 366650d1603SAlex Elder return !!wait_for_completion_timeout(completion, GSI_CMD_TIMEOUT * HZ); 367650d1603SAlex Elder } 368650d1603SAlex Elder 369650d1603SAlex Elder /* Return the hardware's notion of the current state of an event ring */ 370650d1603SAlex Elder static enum gsi_evt_ring_state 371650d1603SAlex Elder gsi_evt_ring_state(struct gsi *gsi, u32 evt_ring_id) 372650d1603SAlex Elder { 373650d1603SAlex Elder u32 val; 374650d1603SAlex Elder 375650d1603SAlex Elder val = ioread32(gsi->virt + GSI_EV_CH_E_CNTXT_0_OFFSET(evt_ring_id)); 376650d1603SAlex Elder 377650d1603SAlex Elder return u32_get_bits(val, EV_CHSTATE_FMASK); 378650d1603SAlex Elder } 379650d1603SAlex Elder 380650d1603SAlex Elder /* Issue an event ring command and wait for it to complete */ 381d9cbe818SAlex Elder static void gsi_evt_ring_command(struct gsi *gsi, u32 evt_ring_id, 382650d1603SAlex Elder enum gsi_evt_cmd_opcode opcode) 383650d1603SAlex Elder { 384650d1603SAlex Elder struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id]; 385650d1603SAlex Elder struct completion *completion = &evt_ring->completion; 3868463488aSAlex Elder struct device *dev = gsi->dev; 387d9cbe818SAlex Elder bool timeout; 388650d1603SAlex Elder u32 val; 389650d1603SAlex Elder 390a60d0632SAlex Elder /* Enable the completion interrupt for the command */ 391a60d0632SAlex Elder gsi_irq_ev_ctrl_enable(gsi, evt_ring_id); 392b4175f87SAlex Elder 393650d1603SAlex Elder val = u32_encode_bits(evt_ring_id, EV_CHID_FMASK); 394650d1603SAlex Elder val |= u32_encode_bits(opcode, EV_OPCODE_FMASK); 395650d1603SAlex Elder 396d9cbe818SAlex Elder timeout = !gsi_command(gsi, GSI_EV_CH_CMD_OFFSET, val, completion); 397b4175f87SAlex Elder 398a60d0632SAlex Elder gsi_irq_ev_ctrl_disable(gsi); 399b4175f87SAlex Elder 400d9cbe818SAlex Elder if (!timeout) 4011ddf776bSAlex Elder return; 402650d1603SAlex Elder 4038463488aSAlex Elder dev_err(dev, "GSI command %u for event ring %u timed out, state %u\n", 4048463488aSAlex Elder opcode, evt_ring_id, evt_ring->state); 405650d1603SAlex Elder } 406650d1603SAlex Elder 407650d1603SAlex Elder /* Allocate an event ring in NOT_ALLOCATED state */ 408650d1603SAlex Elder static int gsi_evt_ring_alloc_command(struct gsi *gsi, u32 evt_ring_id) 409650d1603SAlex Elder { 410650d1603SAlex Elder struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id]; 411650d1603SAlex Elder 412650d1603SAlex Elder /* Get initial event ring state */ 413650d1603SAlex Elder evt_ring->state = gsi_evt_ring_state(gsi, evt_ring_id); 414a442b3c7SAlex Elder if (evt_ring->state != GSI_EVT_RING_STATE_NOT_ALLOCATED) { 415f8d3bdd5SAlex Elder dev_err(gsi->dev, "event ring %u bad state %u before alloc\n", 416f8d3bdd5SAlex Elder evt_ring_id, evt_ring->state); 417650d1603SAlex Elder return -EINVAL; 418a442b3c7SAlex Elder } 419650d1603SAlex Elder 420d9cbe818SAlex Elder gsi_evt_ring_command(gsi, evt_ring_id, GSI_EVT_ALLOCATE); 421428b448eSAlex Elder 422428b448eSAlex Elder /* If successful the event ring state will have changed */ 423428b448eSAlex Elder if (evt_ring->state == GSI_EVT_RING_STATE_ALLOCATED) 424428b448eSAlex Elder return 0; 425428b448eSAlex Elder 426f8d3bdd5SAlex Elder dev_err(gsi->dev, "event ring %u bad state %u after alloc\n", 427f8d3bdd5SAlex Elder evt_ring_id, evt_ring->state); 428650d1603SAlex Elder 429428b448eSAlex Elder return -EIO; 430650d1603SAlex Elder } 431650d1603SAlex Elder 432650d1603SAlex Elder /* Reset a GSI event ring in ALLOCATED or ERROR state. */ 433650d1603SAlex Elder static void gsi_evt_ring_reset_command(struct gsi *gsi, u32 evt_ring_id) 434650d1603SAlex Elder { 435650d1603SAlex Elder struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id]; 436650d1603SAlex Elder enum gsi_evt_ring_state state = evt_ring->state; 437650d1603SAlex Elder 438650d1603SAlex Elder if (state != GSI_EVT_RING_STATE_ALLOCATED && 439650d1603SAlex Elder state != GSI_EVT_RING_STATE_ERROR) { 440f8d3bdd5SAlex Elder dev_err(gsi->dev, "event ring %u bad state %u before reset\n", 441f8d3bdd5SAlex Elder evt_ring_id, evt_ring->state); 442650d1603SAlex Elder return; 443650d1603SAlex Elder } 444650d1603SAlex Elder 445d9cbe818SAlex Elder gsi_evt_ring_command(gsi, evt_ring_id, GSI_EVT_RESET); 446428b448eSAlex Elder 447428b448eSAlex Elder /* If successful the event ring state will have changed */ 448428b448eSAlex Elder if (evt_ring->state == GSI_EVT_RING_STATE_ALLOCATED) 449428b448eSAlex Elder return; 450428b448eSAlex Elder 451f8d3bdd5SAlex Elder dev_err(gsi->dev, "event ring %u bad state %u after reset\n", 452f8d3bdd5SAlex Elder evt_ring_id, evt_ring->state); 453650d1603SAlex Elder } 454650d1603SAlex Elder 455650d1603SAlex Elder /* Issue a hardware de-allocation request for an allocated event ring */ 456650d1603SAlex Elder static void gsi_evt_ring_de_alloc_command(struct gsi *gsi, u32 evt_ring_id) 457650d1603SAlex Elder { 458650d1603SAlex Elder struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id]; 459650d1603SAlex Elder 460650d1603SAlex Elder if (evt_ring->state != GSI_EVT_RING_STATE_ALLOCATED) { 461f8d3bdd5SAlex Elder dev_err(gsi->dev, "event ring %u state %u before dealloc\n", 462f8d3bdd5SAlex Elder evt_ring_id, evt_ring->state); 463650d1603SAlex Elder return; 464650d1603SAlex Elder } 465650d1603SAlex Elder 466d9cbe818SAlex Elder gsi_evt_ring_command(gsi, evt_ring_id, GSI_EVT_DE_ALLOC); 467428b448eSAlex Elder 468428b448eSAlex Elder /* If successful the event ring state will have changed */ 469428b448eSAlex Elder if (evt_ring->state == GSI_EVT_RING_STATE_NOT_ALLOCATED) 470428b448eSAlex Elder return; 471428b448eSAlex Elder 472f8d3bdd5SAlex Elder dev_err(gsi->dev, "event ring %u bad state %u after dealloc\n", 473f8d3bdd5SAlex Elder evt_ring_id, evt_ring->state); 474650d1603SAlex Elder } 475650d1603SAlex Elder 476a2003b30SAlex Elder /* Fetch the current state of a channel from hardware */ 477aba7924fSAlex Elder static enum gsi_channel_state gsi_channel_state(struct gsi_channel *channel) 478650d1603SAlex Elder { 479aba7924fSAlex Elder u32 channel_id = gsi_channel_id(channel); 480aba7924fSAlex Elder void *virt = channel->gsi->virt; 481650d1603SAlex Elder u32 val; 482650d1603SAlex Elder 483aba7924fSAlex Elder val = ioread32(virt + GSI_CH_C_CNTXT_0_OFFSET(channel_id)); 484650d1603SAlex Elder 485650d1603SAlex Elder return u32_get_bits(val, CHSTATE_FMASK); 486650d1603SAlex Elder } 487650d1603SAlex Elder 488650d1603SAlex Elder /* Issue a channel command and wait for it to complete */ 4891169318bSAlex Elder static void 490650d1603SAlex Elder gsi_channel_command(struct gsi_channel *channel, enum gsi_ch_cmd_opcode opcode) 491650d1603SAlex Elder { 492650d1603SAlex Elder struct completion *completion = &channel->completion; 493650d1603SAlex Elder u32 channel_id = gsi_channel_id(channel); 494a2003b30SAlex Elder struct gsi *gsi = channel->gsi; 4958463488aSAlex Elder struct device *dev = gsi->dev; 496d9cbe818SAlex Elder bool timeout; 497650d1603SAlex Elder u32 val; 498650d1603SAlex Elder 499a60d0632SAlex Elder /* Enable the completion interrupt for the command */ 500a60d0632SAlex Elder gsi_irq_ch_ctrl_enable(gsi, channel_id); 501b054d4f9SAlex Elder 502650d1603SAlex Elder val = u32_encode_bits(channel_id, CH_CHID_FMASK); 503650d1603SAlex Elder val |= u32_encode_bits(opcode, CH_OPCODE_FMASK); 504d9cbe818SAlex Elder timeout = !gsi_command(gsi, GSI_CH_CMD_OFFSET, val, completion); 505650d1603SAlex Elder 506a60d0632SAlex Elder gsi_irq_ch_ctrl_disable(gsi); 507b054d4f9SAlex Elder 508d9cbe818SAlex Elder if (!timeout) 5091169318bSAlex Elder return; 510650d1603SAlex Elder 5118463488aSAlex Elder dev_err(dev, "GSI command %u for channel %u timed out, state %u\n", 512a2003b30SAlex Elder opcode, channel_id, gsi_channel_state(channel)); 513650d1603SAlex Elder } 514650d1603SAlex Elder 515650d1603SAlex Elder /* Allocate GSI channel in NOT_ALLOCATED state */ 516650d1603SAlex Elder static int gsi_channel_alloc_command(struct gsi *gsi, u32 channel_id) 517650d1603SAlex Elder { 518650d1603SAlex Elder struct gsi_channel *channel = &gsi->channel[channel_id]; 519a442b3c7SAlex Elder struct device *dev = gsi->dev; 520a2003b30SAlex Elder enum gsi_channel_state state; 521650d1603SAlex Elder 522650d1603SAlex Elder /* Get initial channel state */ 523a2003b30SAlex Elder state = gsi_channel_state(channel); 524a442b3c7SAlex Elder if (state != GSI_CHANNEL_STATE_NOT_ALLOCATED) { 525f8d3bdd5SAlex Elder dev_err(dev, "channel %u bad state %u before alloc\n", 526f8d3bdd5SAlex Elder channel_id, state); 527650d1603SAlex Elder return -EINVAL; 528a442b3c7SAlex Elder } 529650d1603SAlex Elder 5301169318bSAlex Elder gsi_channel_command(channel, GSI_CH_ALLOCATE); 531a2003b30SAlex Elder 5326ffddf3bSAlex Elder /* If successful the channel state will have changed */ 533a2003b30SAlex Elder state = gsi_channel_state(channel); 5346ffddf3bSAlex Elder if (state == GSI_CHANNEL_STATE_ALLOCATED) 5356ffddf3bSAlex Elder return 0; 5366ffddf3bSAlex Elder 537f8d3bdd5SAlex Elder dev_err(dev, "channel %u bad state %u after alloc\n", 538f8d3bdd5SAlex Elder channel_id, state); 539650d1603SAlex Elder 5406ffddf3bSAlex Elder return -EIO; 541650d1603SAlex Elder } 542650d1603SAlex Elder 543650d1603SAlex Elder /* Start an ALLOCATED channel */ 544650d1603SAlex Elder static int gsi_channel_start_command(struct gsi_channel *channel) 545650d1603SAlex Elder { 546a442b3c7SAlex Elder struct device *dev = channel->gsi->dev; 547a2003b30SAlex Elder enum gsi_channel_state state; 548650d1603SAlex Elder 549a2003b30SAlex Elder state = gsi_channel_state(channel); 550650d1603SAlex Elder if (state != GSI_CHANNEL_STATE_ALLOCATED && 551a442b3c7SAlex Elder state != GSI_CHANNEL_STATE_STOPPED) { 552f8d3bdd5SAlex Elder dev_err(dev, "channel %u bad state %u before start\n", 553f8d3bdd5SAlex Elder gsi_channel_id(channel), state); 554650d1603SAlex Elder return -EINVAL; 555a442b3c7SAlex Elder } 556650d1603SAlex Elder 5571169318bSAlex Elder gsi_channel_command(channel, GSI_CH_START); 558a2003b30SAlex Elder 5596ffddf3bSAlex Elder /* If successful the channel state will have changed */ 560a2003b30SAlex Elder state = gsi_channel_state(channel); 5616ffddf3bSAlex Elder if (state == GSI_CHANNEL_STATE_STARTED) 5626ffddf3bSAlex Elder return 0; 5636ffddf3bSAlex Elder 564f8d3bdd5SAlex Elder dev_err(dev, "channel %u bad state %u after start\n", 565f8d3bdd5SAlex Elder gsi_channel_id(channel), state); 566650d1603SAlex Elder 5676ffddf3bSAlex Elder return -EIO; 568650d1603SAlex Elder } 569650d1603SAlex Elder 570650d1603SAlex Elder /* Stop a GSI channel in STARTED state */ 571650d1603SAlex Elder static int gsi_channel_stop_command(struct gsi_channel *channel) 572650d1603SAlex Elder { 573a442b3c7SAlex Elder struct device *dev = channel->gsi->dev; 574a2003b30SAlex Elder enum gsi_channel_state state; 575650d1603SAlex Elder 576a2003b30SAlex Elder state = gsi_channel_state(channel); 5775468cbcdSAlex Elder 5785468cbcdSAlex Elder /* Channel could have entered STOPPED state since last call 5795468cbcdSAlex Elder * if it timed out. If so, we're done. 5805468cbcdSAlex Elder */ 5815468cbcdSAlex Elder if (state == GSI_CHANNEL_STATE_STOPPED) 5825468cbcdSAlex Elder return 0; 5835468cbcdSAlex Elder 584650d1603SAlex Elder if (state != GSI_CHANNEL_STATE_STARTED && 585a442b3c7SAlex Elder state != GSI_CHANNEL_STATE_STOP_IN_PROC) { 586f8d3bdd5SAlex Elder dev_err(dev, "channel %u bad state %u before stop\n", 587f8d3bdd5SAlex Elder gsi_channel_id(channel), state); 588650d1603SAlex Elder return -EINVAL; 589a442b3c7SAlex Elder } 590650d1603SAlex Elder 5911169318bSAlex Elder gsi_channel_command(channel, GSI_CH_STOP); 592a2003b30SAlex Elder 5936ffddf3bSAlex Elder /* If successful the channel state will have changed */ 594a2003b30SAlex Elder state = gsi_channel_state(channel); 5956ffddf3bSAlex Elder if (state == GSI_CHANNEL_STATE_STOPPED) 5966ffddf3bSAlex Elder return 0; 597650d1603SAlex Elder 598650d1603SAlex Elder /* We may have to try again if stop is in progress */ 599a2003b30SAlex Elder if (state == GSI_CHANNEL_STATE_STOP_IN_PROC) 600650d1603SAlex Elder return -EAGAIN; 601650d1603SAlex Elder 602f8d3bdd5SAlex Elder dev_err(dev, "channel %u bad state %u after stop\n", 603f8d3bdd5SAlex Elder gsi_channel_id(channel), state); 604650d1603SAlex Elder 605650d1603SAlex Elder return -EIO; 606650d1603SAlex Elder } 607650d1603SAlex Elder 608650d1603SAlex Elder /* Reset a GSI channel in ALLOCATED or ERROR state. */ 609650d1603SAlex Elder static void gsi_channel_reset_command(struct gsi_channel *channel) 610650d1603SAlex Elder { 611a442b3c7SAlex Elder struct device *dev = channel->gsi->dev; 612a2003b30SAlex Elder enum gsi_channel_state state; 613650d1603SAlex Elder 614*74401946SAlex Elder /* A short delay is required before a RESET command */ 615*74401946SAlex Elder usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC); 616650d1603SAlex Elder 617a2003b30SAlex Elder state = gsi_channel_state(channel); 618a2003b30SAlex Elder if (state != GSI_CHANNEL_STATE_STOPPED && 619a2003b30SAlex Elder state != GSI_CHANNEL_STATE_ERROR) { 6205d28913dSAlex Elder /* No need to reset a channel already in ALLOCATED state */ 6215d28913dSAlex Elder if (state != GSI_CHANNEL_STATE_ALLOCATED) 622f8d3bdd5SAlex Elder dev_err(dev, "channel %u bad state %u before reset\n", 623f8d3bdd5SAlex Elder gsi_channel_id(channel), state); 624650d1603SAlex Elder return; 625650d1603SAlex Elder } 626650d1603SAlex Elder 6271169318bSAlex Elder gsi_channel_command(channel, GSI_CH_RESET); 628a2003b30SAlex Elder 6296ffddf3bSAlex Elder /* If successful the channel state will have changed */ 630a2003b30SAlex Elder state = gsi_channel_state(channel); 6316ffddf3bSAlex Elder if (state != GSI_CHANNEL_STATE_ALLOCATED) 632f8d3bdd5SAlex Elder dev_err(dev, "channel %u bad state %u after reset\n", 633f8d3bdd5SAlex Elder gsi_channel_id(channel), state); 634650d1603SAlex Elder } 635650d1603SAlex Elder 636650d1603SAlex Elder /* Deallocate an ALLOCATED GSI channel */ 637650d1603SAlex Elder static void gsi_channel_de_alloc_command(struct gsi *gsi, u32 channel_id) 638650d1603SAlex Elder { 639650d1603SAlex Elder struct gsi_channel *channel = &gsi->channel[channel_id]; 640a442b3c7SAlex Elder struct device *dev = gsi->dev; 641a2003b30SAlex Elder enum gsi_channel_state state; 642650d1603SAlex Elder 643a2003b30SAlex Elder state = gsi_channel_state(channel); 644a2003b30SAlex Elder if (state != GSI_CHANNEL_STATE_ALLOCATED) { 645f8d3bdd5SAlex Elder dev_err(dev, "channel %u bad state %u before dealloc\n", 646f8d3bdd5SAlex Elder channel_id, state); 647650d1603SAlex Elder return; 648650d1603SAlex Elder } 649650d1603SAlex Elder 6501169318bSAlex Elder gsi_channel_command(channel, GSI_CH_DE_ALLOC); 651a2003b30SAlex Elder 6526ffddf3bSAlex Elder /* If successful the channel state will have changed */ 653a2003b30SAlex Elder state = gsi_channel_state(channel); 6546ffddf3bSAlex Elder 6556ffddf3bSAlex Elder if (state != GSI_CHANNEL_STATE_NOT_ALLOCATED) 656f8d3bdd5SAlex Elder dev_err(dev, "channel %u bad state %u after dealloc\n", 657f8d3bdd5SAlex Elder channel_id, state); 658650d1603SAlex Elder } 659650d1603SAlex Elder 660650d1603SAlex Elder /* Ring an event ring doorbell, reporting the last entry processed by the AP. 661650d1603SAlex Elder * The index argument (modulo the ring count) is the first unfilled entry, so 662650d1603SAlex Elder * we supply one less than that with the doorbell. Update the event ring 663650d1603SAlex Elder * index field with the value provided. 664650d1603SAlex Elder */ 665650d1603SAlex Elder static void gsi_evt_ring_doorbell(struct gsi *gsi, u32 evt_ring_id, u32 index) 666650d1603SAlex Elder { 667650d1603SAlex Elder struct gsi_ring *ring = &gsi->evt_ring[evt_ring_id].ring; 668650d1603SAlex Elder u32 val; 669650d1603SAlex Elder 670650d1603SAlex Elder ring->index = index; /* Next unused entry */ 671650d1603SAlex Elder 672650d1603SAlex Elder /* Note: index *must* be used modulo the ring count here */ 673650d1603SAlex Elder val = gsi_ring_addr(ring, (index - 1) % ring->count); 674650d1603SAlex Elder iowrite32(val, gsi->virt + GSI_EV_CH_E_DOORBELL_0_OFFSET(evt_ring_id)); 675650d1603SAlex Elder } 676650d1603SAlex Elder 677650d1603SAlex Elder /* Program an event ring for use */ 678650d1603SAlex Elder static void gsi_evt_ring_program(struct gsi *gsi, u32 evt_ring_id) 679650d1603SAlex Elder { 680650d1603SAlex Elder struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id]; 681650d1603SAlex Elder size_t size = evt_ring->ring.count * GSI_RING_ELEMENT_SIZE; 682650d1603SAlex Elder u32 val; 683650d1603SAlex Elder 68446dda53eSAlex Elder /* We program all event rings as GPI type/protocol */ 68546dda53eSAlex Elder val = u32_encode_bits(GSI_CHANNEL_TYPE_GPI, EV_CHTYPE_FMASK); 686650d1603SAlex Elder val |= EV_INTYPE_FMASK; 687650d1603SAlex Elder val |= u32_encode_bits(GSI_RING_ELEMENT_SIZE, EV_ELEMENT_SIZE_FMASK); 688650d1603SAlex Elder iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_0_OFFSET(evt_ring_id)); 689650d1603SAlex Elder 690650d1603SAlex Elder val = u32_encode_bits(size, EV_R_LENGTH_FMASK); 691650d1603SAlex Elder iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_1_OFFSET(evt_ring_id)); 692650d1603SAlex Elder 693650d1603SAlex Elder /* The context 2 and 3 registers store the low-order and 694650d1603SAlex Elder * high-order 32 bits of the address of the event ring, 695650d1603SAlex Elder * respectively. 696650d1603SAlex Elder */ 697650d1603SAlex Elder val = evt_ring->ring.addr & GENMASK(31, 0); 698650d1603SAlex Elder iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_2_OFFSET(evt_ring_id)); 699650d1603SAlex Elder 700650d1603SAlex Elder val = evt_ring->ring.addr >> 32; 701650d1603SAlex Elder iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_3_OFFSET(evt_ring_id)); 702650d1603SAlex Elder 703650d1603SAlex Elder /* Enable interrupt moderation by setting the moderation delay */ 704650d1603SAlex Elder val = u32_encode_bits(GSI_EVT_RING_INT_MODT, MODT_FMASK); 705650d1603SAlex Elder val |= u32_encode_bits(1, MODC_FMASK); /* comes from channel */ 706650d1603SAlex Elder iowrite32(val, gsi->virt + GSI_EV_CH_E_CNTXT_8_OFFSET(evt_ring_id)); 707650d1603SAlex Elder 708650d1603SAlex Elder /* No MSI write data, and MSI address high and low address is 0 */ 709650d1603SAlex Elder iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_9_OFFSET(evt_ring_id)); 710650d1603SAlex Elder iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_10_OFFSET(evt_ring_id)); 711650d1603SAlex Elder iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_11_OFFSET(evt_ring_id)); 712650d1603SAlex Elder 713650d1603SAlex Elder /* We don't need to get event read pointer updates */ 714650d1603SAlex Elder iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_12_OFFSET(evt_ring_id)); 715650d1603SAlex Elder iowrite32(0, gsi->virt + GSI_EV_CH_E_CNTXT_13_OFFSET(evt_ring_id)); 716650d1603SAlex Elder 717650d1603SAlex Elder /* Finally, tell the hardware we've completed event 0 (arbitrary) */ 718650d1603SAlex Elder gsi_evt_ring_doorbell(gsi, evt_ring_id, 0); 719650d1603SAlex Elder } 720650d1603SAlex Elder 721650d1603SAlex Elder /* Return the last (most recent) transaction completed on a channel. */ 722650d1603SAlex Elder static struct gsi_trans *gsi_channel_trans_last(struct gsi_channel *channel) 723650d1603SAlex Elder { 724650d1603SAlex Elder struct gsi_trans_info *trans_info = &channel->trans_info; 725650d1603SAlex Elder struct gsi_trans *trans; 726650d1603SAlex Elder 727650d1603SAlex Elder spin_lock_bh(&trans_info->spinlock); 728650d1603SAlex Elder 729650d1603SAlex Elder if (!list_empty(&trans_info->complete)) 730650d1603SAlex Elder trans = list_last_entry(&trans_info->complete, 731650d1603SAlex Elder struct gsi_trans, links); 732650d1603SAlex Elder else if (!list_empty(&trans_info->polled)) 733650d1603SAlex Elder trans = list_last_entry(&trans_info->polled, 734650d1603SAlex Elder struct gsi_trans, links); 735650d1603SAlex Elder else 736650d1603SAlex Elder trans = NULL; 737650d1603SAlex Elder 738650d1603SAlex Elder /* Caller will wait for this, so take a reference */ 739650d1603SAlex Elder if (trans) 740650d1603SAlex Elder refcount_inc(&trans->refcount); 741650d1603SAlex Elder 742650d1603SAlex Elder spin_unlock_bh(&trans_info->spinlock); 743650d1603SAlex Elder 744650d1603SAlex Elder return trans; 745650d1603SAlex Elder } 746650d1603SAlex Elder 747650d1603SAlex Elder /* Wait for transaction activity on a channel to complete */ 748650d1603SAlex Elder static void gsi_channel_trans_quiesce(struct gsi_channel *channel) 749650d1603SAlex Elder { 750650d1603SAlex Elder struct gsi_trans *trans; 751650d1603SAlex Elder 752650d1603SAlex Elder /* Get the last transaction, and wait for it to complete */ 753650d1603SAlex Elder trans = gsi_channel_trans_last(channel); 754650d1603SAlex Elder if (trans) { 755650d1603SAlex Elder wait_for_completion(&trans->completion); 756650d1603SAlex Elder gsi_trans_free(trans); 757650d1603SAlex Elder } 758650d1603SAlex Elder } 759650d1603SAlex Elder 760650d1603SAlex Elder /* Stop channel activity. Transactions may not be allocated until thawed. */ 761650d1603SAlex Elder static void gsi_channel_freeze(struct gsi_channel *channel) 762650d1603SAlex Elder { 763650d1603SAlex Elder gsi_channel_trans_quiesce(channel); 764650d1603SAlex Elder 765650d1603SAlex Elder napi_disable(&channel->napi); 766650d1603SAlex Elder 767650d1603SAlex Elder gsi_irq_ieob_disable(channel->gsi, channel->evt_ring_id); 768650d1603SAlex Elder } 769650d1603SAlex Elder 770650d1603SAlex Elder /* Allow transactions to be used on the channel again. */ 771650d1603SAlex Elder static void gsi_channel_thaw(struct gsi_channel *channel) 772650d1603SAlex Elder { 773650d1603SAlex Elder gsi_irq_ieob_enable(channel->gsi, channel->evt_ring_id); 774650d1603SAlex Elder 775650d1603SAlex Elder napi_enable(&channel->napi); 776650d1603SAlex Elder } 777650d1603SAlex Elder 778650d1603SAlex Elder /* Program a channel for use */ 779650d1603SAlex Elder static void gsi_channel_program(struct gsi_channel *channel, bool doorbell) 780650d1603SAlex Elder { 781650d1603SAlex Elder size_t size = channel->tre_ring.count * GSI_RING_ELEMENT_SIZE; 782650d1603SAlex Elder u32 channel_id = gsi_channel_id(channel); 783650d1603SAlex Elder union gsi_channel_scratch scr = { }; 784650d1603SAlex Elder struct gsi_channel_scratch_gpi *gpi; 785650d1603SAlex Elder struct gsi *gsi = channel->gsi; 786650d1603SAlex Elder u32 wrr_weight = 0; 787650d1603SAlex Elder u32 val; 788650d1603SAlex Elder 789650d1603SAlex Elder /* Arbitrarily pick TRE 0 as the first channel element to use */ 790650d1603SAlex Elder channel->tre_ring.index = 0; 791650d1603SAlex Elder 79246dda53eSAlex Elder /* We program all channels as GPI type/protocol */ 79346dda53eSAlex Elder val = u32_encode_bits(GSI_CHANNEL_TYPE_GPI, CHTYPE_PROTOCOL_FMASK); 794650d1603SAlex Elder if (channel->toward_ipa) 795650d1603SAlex Elder val |= CHTYPE_DIR_FMASK; 796650d1603SAlex Elder val |= u32_encode_bits(channel->evt_ring_id, ERINDEX_FMASK); 797650d1603SAlex Elder val |= u32_encode_bits(GSI_RING_ELEMENT_SIZE, ELEMENT_SIZE_FMASK); 798650d1603SAlex Elder iowrite32(val, gsi->virt + GSI_CH_C_CNTXT_0_OFFSET(channel_id)); 799650d1603SAlex Elder 800650d1603SAlex Elder val = u32_encode_bits(size, R_LENGTH_FMASK); 801650d1603SAlex Elder iowrite32(val, gsi->virt + GSI_CH_C_CNTXT_1_OFFSET(channel_id)); 802650d1603SAlex Elder 803650d1603SAlex Elder /* The context 2 and 3 registers store the low-order and 804650d1603SAlex Elder * high-order 32 bits of the address of the channel ring, 805650d1603SAlex Elder * respectively. 806650d1603SAlex Elder */ 807650d1603SAlex Elder val = channel->tre_ring.addr & GENMASK(31, 0); 808650d1603SAlex Elder iowrite32(val, gsi->virt + GSI_CH_C_CNTXT_2_OFFSET(channel_id)); 809650d1603SAlex Elder 810650d1603SAlex Elder val = channel->tre_ring.addr >> 32; 811650d1603SAlex Elder iowrite32(val, gsi->virt + GSI_CH_C_CNTXT_3_OFFSET(channel_id)); 812650d1603SAlex Elder 813650d1603SAlex Elder /* Command channel gets low weighted round-robin priority */ 814650d1603SAlex Elder if (channel->command) 815650d1603SAlex Elder wrr_weight = field_max(WRR_WEIGHT_FMASK); 816650d1603SAlex Elder val = u32_encode_bits(wrr_weight, WRR_WEIGHT_FMASK); 817650d1603SAlex Elder 818650d1603SAlex Elder /* Max prefetch is 1 segment (do not set MAX_PREFETCH_FMASK) */ 819650d1603SAlex Elder 820ce54993dSAlex Elder /* We enable the doorbell engine for IPA v3.5.1 */ 821ce54993dSAlex Elder if (gsi->version == IPA_VERSION_3_5_1 && doorbell) 822650d1603SAlex Elder val |= USE_DB_ENG_FMASK; 823650d1603SAlex Elder 8249f848198SAlex Elder /* v4.0 introduces an escape buffer for prefetch. We use it 8259f848198SAlex Elder * on all but the AP command channel. 8269f848198SAlex Elder */ 827b0b6f0ddSAlex Elder if (gsi->version != IPA_VERSION_3_5_1 && !channel->command) { 828b0b6f0ddSAlex Elder /* If not otherwise set, prefetch buffers are used */ 829b0b6f0ddSAlex Elder if (gsi->version < IPA_VERSION_4_5) 830650d1603SAlex Elder val |= USE_ESCAPE_BUF_ONLY_FMASK; 831b0b6f0ddSAlex Elder else 832b0b6f0ddSAlex Elder val |= u32_encode_bits(GSI_ESCAPE_BUF_ONLY, 833b0b6f0ddSAlex Elder PREFETCH_MODE_FMASK); 834b0b6f0ddSAlex Elder } 835650d1603SAlex Elder 836650d1603SAlex Elder iowrite32(val, gsi->virt + GSI_CH_C_QOS_OFFSET(channel_id)); 837650d1603SAlex Elder 838650d1603SAlex Elder /* Now update the scratch registers for GPI protocol */ 839650d1603SAlex Elder gpi = &scr.gpi; 840650d1603SAlex Elder gpi->max_outstanding_tre = gsi_channel_trans_tre_max(gsi, channel_id) * 841650d1603SAlex Elder GSI_RING_ELEMENT_SIZE; 842650d1603SAlex Elder gpi->outstanding_threshold = 2 * GSI_RING_ELEMENT_SIZE; 843650d1603SAlex Elder 844650d1603SAlex Elder val = scr.data.word1; 845650d1603SAlex Elder iowrite32(val, gsi->virt + GSI_CH_C_SCRATCH_0_OFFSET(channel_id)); 846650d1603SAlex Elder 847650d1603SAlex Elder val = scr.data.word2; 848650d1603SAlex Elder iowrite32(val, gsi->virt + GSI_CH_C_SCRATCH_1_OFFSET(channel_id)); 849650d1603SAlex Elder 850650d1603SAlex Elder val = scr.data.word3; 851650d1603SAlex Elder iowrite32(val, gsi->virt + GSI_CH_C_SCRATCH_2_OFFSET(channel_id)); 852650d1603SAlex Elder 853650d1603SAlex Elder /* We must preserve the upper 16 bits of the last scratch register. 854650d1603SAlex Elder * The next sequence assumes those bits remain unchanged between the 855650d1603SAlex Elder * read and the write. 856650d1603SAlex Elder */ 857650d1603SAlex Elder val = ioread32(gsi->virt + GSI_CH_C_SCRATCH_3_OFFSET(channel_id)); 858650d1603SAlex Elder val = (scr.data.word4 & GENMASK(31, 16)) | (val & GENMASK(15, 0)); 859650d1603SAlex Elder iowrite32(val, gsi->virt + GSI_CH_C_SCRATCH_3_OFFSET(channel_id)); 860650d1603SAlex Elder 861650d1603SAlex Elder /* All done! */ 862650d1603SAlex Elder } 863650d1603SAlex Elder 864650d1603SAlex Elder static void gsi_channel_deprogram(struct gsi_channel *channel) 865650d1603SAlex Elder { 866650d1603SAlex Elder /* Nothing to do */ 867650d1603SAlex Elder } 868650d1603SAlex Elder 869650d1603SAlex Elder /* Start an allocated GSI channel */ 870650d1603SAlex Elder int gsi_channel_start(struct gsi *gsi, u32 channel_id) 871650d1603SAlex Elder { 872650d1603SAlex Elder struct gsi_channel *channel = &gsi->channel[channel_id]; 873650d1603SAlex Elder int ret; 874650d1603SAlex Elder 875650d1603SAlex Elder mutex_lock(&gsi->mutex); 876650d1603SAlex Elder 877650d1603SAlex Elder ret = gsi_channel_start_command(channel); 878650d1603SAlex Elder 879650d1603SAlex Elder mutex_unlock(&gsi->mutex); 880650d1603SAlex Elder 881650d1603SAlex Elder gsi_channel_thaw(channel); 882650d1603SAlex Elder 883650d1603SAlex Elder return ret; 884650d1603SAlex Elder } 885650d1603SAlex Elder 886650d1603SAlex Elder /* Stop a started channel */ 887650d1603SAlex Elder int gsi_channel_stop(struct gsi *gsi, u32 channel_id) 888650d1603SAlex Elder { 889650d1603SAlex Elder struct gsi_channel *channel = &gsi->channel[channel_id]; 890650d1603SAlex Elder u32 retries; 891650d1603SAlex Elder int ret; 892650d1603SAlex Elder 893650d1603SAlex Elder gsi_channel_freeze(channel); 894650d1603SAlex Elder 895650d1603SAlex Elder /* RX channels might require a little time to enter STOPPED state */ 896650d1603SAlex Elder retries = channel->toward_ipa ? 0 : GSI_CHANNEL_STOP_RX_RETRIES; 897650d1603SAlex Elder 898650d1603SAlex Elder mutex_lock(&gsi->mutex); 899650d1603SAlex Elder 900650d1603SAlex Elder do { 901650d1603SAlex Elder ret = gsi_channel_stop_command(channel); 902650d1603SAlex Elder if (ret != -EAGAIN) 903650d1603SAlex Elder break; 904*74401946SAlex Elder usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC); 905650d1603SAlex Elder } while (retries--); 906650d1603SAlex Elder 907650d1603SAlex Elder mutex_unlock(&gsi->mutex); 908650d1603SAlex Elder 909650d1603SAlex Elder /* Thaw the channel if we need to retry (or on error) */ 910650d1603SAlex Elder if (ret) 911650d1603SAlex Elder gsi_channel_thaw(channel); 912650d1603SAlex Elder 913650d1603SAlex Elder return ret; 914650d1603SAlex Elder } 915650d1603SAlex Elder 916ce54993dSAlex Elder /* Reset and reconfigure a channel, (possibly) enabling the doorbell engine */ 917ce54993dSAlex Elder void gsi_channel_reset(struct gsi *gsi, u32 channel_id, bool doorbell) 918650d1603SAlex Elder { 919650d1603SAlex Elder struct gsi_channel *channel = &gsi->channel[channel_id]; 920650d1603SAlex Elder 921650d1603SAlex Elder mutex_lock(&gsi->mutex); 922650d1603SAlex Elder 923650d1603SAlex Elder gsi_channel_reset_command(channel); 924a3f2405bSAlex Elder /* Due to a hardware quirk we may need to reset RX channels twice. */ 9259de4a4ccSAlex Elder if (gsi->version == IPA_VERSION_3_5_1 && !channel->toward_ipa) 926650d1603SAlex Elder gsi_channel_reset_command(channel); 927650d1603SAlex Elder 928ce54993dSAlex Elder gsi_channel_program(channel, doorbell); 929650d1603SAlex Elder gsi_channel_trans_cancel_pending(channel); 930650d1603SAlex Elder 931650d1603SAlex Elder mutex_unlock(&gsi->mutex); 932650d1603SAlex Elder } 933650d1603SAlex Elder 934650d1603SAlex Elder /* Stop a STARTED channel for suspend (using stop if requested) */ 935650d1603SAlex Elder int gsi_channel_suspend(struct gsi *gsi, u32 channel_id, bool stop) 936650d1603SAlex Elder { 937650d1603SAlex Elder struct gsi_channel *channel = &gsi->channel[channel_id]; 938650d1603SAlex Elder 939650d1603SAlex Elder if (stop) 940650d1603SAlex Elder return gsi_channel_stop(gsi, channel_id); 941650d1603SAlex Elder 942650d1603SAlex Elder gsi_channel_freeze(channel); 943650d1603SAlex Elder 944650d1603SAlex Elder return 0; 945650d1603SAlex Elder } 946650d1603SAlex Elder 947650d1603SAlex Elder /* Resume a suspended channel (starting will be requested if STOPPED) */ 948650d1603SAlex Elder int gsi_channel_resume(struct gsi *gsi, u32 channel_id, bool start) 949650d1603SAlex Elder { 950650d1603SAlex Elder struct gsi_channel *channel = &gsi->channel[channel_id]; 951650d1603SAlex Elder 952650d1603SAlex Elder if (start) 953650d1603SAlex Elder return gsi_channel_start(gsi, channel_id); 954650d1603SAlex Elder 955650d1603SAlex Elder gsi_channel_thaw(channel); 956650d1603SAlex Elder 957650d1603SAlex Elder return 0; 958650d1603SAlex Elder } 959650d1603SAlex Elder 960650d1603SAlex Elder /** 961650d1603SAlex Elder * gsi_channel_tx_queued() - Report queued TX transfers for a channel 962650d1603SAlex Elder * @channel: Channel for which to report 963650d1603SAlex Elder * 964650d1603SAlex Elder * Report to the network stack the number of bytes and transactions that 965650d1603SAlex Elder * have been queued to hardware since last call. This and the next function 966650d1603SAlex Elder * supply information used by the network stack for throttling. 967650d1603SAlex Elder * 968650d1603SAlex Elder * For each channel we track the number of transactions used and bytes of 969650d1603SAlex Elder * data those transactions represent. We also track what those values are 970650d1603SAlex Elder * each time this function is called. Subtracting the two tells us 971650d1603SAlex Elder * the number of bytes and transactions that have been added between 972650d1603SAlex Elder * successive calls. 973650d1603SAlex Elder * 974650d1603SAlex Elder * Calling this each time we ring the channel doorbell allows us to 975650d1603SAlex Elder * provide accurate information to the network stack about how much 976650d1603SAlex Elder * work we've given the hardware at any point in time. 977650d1603SAlex Elder */ 978650d1603SAlex Elder void gsi_channel_tx_queued(struct gsi_channel *channel) 979650d1603SAlex Elder { 980650d1603SAlex Elder u32 trans_count; 981650d1603SAlex Elder u32 byte_count; 982650d1603SAlex Elder 983650d1603SAlex Elder byte_count = channel->byte_count - channel->queued_byte_count; 984650d1603SAlex Elder trans_count = channel->trans_count - channel->queued_trans_count; 985650d1603SAlex Elder channel->queued_byte_count = channel->byte_count; 986650d1603SAlex Elder channel->queued_trans_count = channel->trans_count; 987650d1603SAlex Elder 988650d1603SAlex Elder ipa_gsi_channel_tx_queued(channel->gsi, gsi_channel_id(channel), 989650d1603SAlex Elder trans_count, byte_count); 990650d1603SAlex Elder } 991650d1603SAlex Elder 992650d1603SAlex Elder /** 993650d1603SAlex Elder * gsi_channel_tx_update() - Report completed TX transfers 994650d1603SAlex Elder * @channel: Channel that has completed transmitting packets 995650d1603SAlex Elder * @trans: Last transation known to be complete 996650d1603SAlex Elder * 997650d1603SAlex Elder * Compute the number of transactions and bytes that have been transferred 998650d1603SAlex Elder * over a TX channel since the given transaction was committed. Report this 999650d1603SAlex Elder * information to the network stack. 1000650d1603SAlex Elder * 1001650d1603SAlex Elder * At the time a transaction is committed, we record its channel's 1002650d1603SAlex Elder * committed transaction and byte counts *in the transaction*. 1003650d1603SAlex Elder * Completions are signaled by the hardware with an interrupt, and 1004650d1603SAlex Elder * we can determine the latest completed transaction at that time. 1005650d1603SAlex Elder * 1006650d1603SAlex Elder * The difference between the byte/transaction count recorded in 1007650d1603SAlex Elder * the transaction and the count last time we recorded a completion 1008650d1603SAlex Elder * tells us exactly how much data has been transferred between 1009650d1603SAlex Elder * completions. 1010650d1603SAlex Elder * 1011650d1603SAlex Elder * Calling this each time we learn of a newly-completed transaction 1012650d1603SAlex Elder * allows us to provide accurate information to the network stack 1013650d1603SAlex Elder * about how much work has been completed by the hardware at a given 1014650d1603SAlex Elder * point in time. 1015650d1603SAlex Elder */ 1016650d1603SAlex Elder static void 1017650d1603SAlex Elder gsi_channel_tx_update(struct gsi_channel *channel, struct gsi_trans *trans) 1018650d1603SAlex Elder { 1019650d1603SAlex Elder u64 byte_count = trans->byte_count + trans->len; 1020650d1603SAlex Elder u64 trans_count = trans->trans_count + 1; 1021650d1603SAlex Elder 1022650d1603SAlex Elder byte_count -= channel->compl_byte_count; 1023650d1603SAlex Elder channel->compl_byte_count += byte_count; 1024650d1603SAlex Elder trans_count -= channel->compl_trans_count; 1025650d1603SAlex Elder channel->compl_trans_count += trans_count; 1026650d1603SAlex Elder 1027650d1603SAlex Elder ipa_gsi_channel_tx_completed(channel->gsi, gsi_channel_id(channel), 1028650d1603SAlex Elder trans_count, byte_count); 1029650d1603SAlex Elder } 1030650d1603SAlex Elder 1031650d1603SAlex Elder /* Channel control interrupt handler */ 1032650d1603SAlex Elder static void gsi_isr_chan_ctrl(struct gsi *gsi) 1033650d1603SAlex Elder { 1034650d1603SAlex Elder u32 channel_mask; 1035650d1603SAlex Elder 1036650d1603SAlex Elder channel_mask = ioread32(gsi->virt + GSI_CNTXT_SRC_CH_IRQ_OFFSET); 1037650d1603SAlex Elder iowrite32(channel_mask, gsi->virt + GSI_CNTXT_SRC_CH_IRQ_CLR_OFFSET); 1038650d1603SAlex Elder 1039650d1603SAlex Elder while (channel_mask) { 1040650d1603SAlex Elder u32 channel_id = __ffs(channel_mask); 1041650d1603SAlex Elder struct gsi_channel *channel; 1042650d1603SAlex Elder 1043650d1603SAlex Elder channel_mask ^= BIT(channel_id); 1044650d1603SAlex Elder 1045650d1603SAlex Elder channel = &gsi->channel[channel_id]; 1046650d1603SAlex Elder 1047650d1603SAlex Elder complete(&channel->completion); 1048650d1603SAlex Elder } 1049650d1603SAlex Elder } 1050650d1603SAlex Elder 1051650d1603SAlex Elder /* Event ring control interrupt handler */ 1052650d1603SAlex Elder static void gsi_isr_evt_ctrl(struct gsi *gsi) 1053650d1603SAlex Elder { 1054650d1603SAlex Elder u32 event_mask; 1055650d1603SAlex Elder 1056650d1603SAlex Elder event_mask = ioread32(gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_OFFSET); 1057650d1603SAlex Elder iowrite32(event_mask, gsi->virt + GSI_CNTXT_SRC_EV_CH_IRQ_CLR_OFFSET); 1058650d1603SAlex Elder 1059650d1603SAlex Elder while (event_mask) { 1060650d1603SAlex Elder u32 evt_ring_id = __ffs(event_mask); 1061650d1603SAlex Elder struct gsi_evt_ring *evt_ring; 1062650d1603SAlex Elder 1063650d1603SAlex Elder event_mask ^= BIT(evt_ring_id); 1064650d1603SAlex Elder 1065650d1603SAlex Elder evt_ring = &gsi->evt_ring[evt_ring_id]; 1066650d1603SAlex Elder evt_ring->state = gsi_evt_ring_state(gsi, evt_ring_id); 1067650d1603SAlex Elder 1068650d1603SAlex Elder complete(&evt_ring->completion); 1069650d1603SAlex Elder } 1070650d1603SAlex Elder } 1071650d1603SAlex Elder 1072650d1603SAlex Elder /* Global channel error interrupt handler */ 1073650d1603SAlex Elder static void 1074650d1603SAlex Elder gsi_isr_glob_chan_err(struct gsi *gsi, u32 err_ee, u32 channel_id, u32 code) 1075650d1603SAlex Elder { 10767b0ac8f6SAlex Elder if (code == GSI_OUT_OF_RESOURCES) { 1077650d1603SAlex Elder dev_err(gsi->dev, "channel %u out of resources\n", channel_id); 1078650d1603SAlex Elder complete(&gsi->channel[channel_id].completion); 1079650d1603SAlex Elder return; 1080650d1603SAlex Elder } 1081650d1603SAlex Elder 1082650d1603SAlex Elder /* Report, but otherwise ignore all other error codes */ 1083650d1603SAlex Elder dev_err(gsi->dev, "channel %u global error ee 0x%08x code 0x%08x\n", 1084650d1603SAlex Elder channel_id, err_ee, code); 1085650d1603SAlex Elder } 1086650d1603SAlex Elder 1087650d1603SAlex Elder /* Global event error interrupt handler */ 1088650d1603SAlex Elder static void 1089650d1603SAlex Elder gsi_isr_glob_evt_err(struct gsi *gsi, u32 err_ee, u32 evt_ring_id, u32 code) 1090650d1603SAlex Elder { 10917b0ac8f6SAlex Elder if (code == GSI_OUT_OF_RESOURCES) { 1092650d1603SAlex Elder struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id]; 1093650d1603SAlex Elder u32 channel_id = gsi_channel_id(evt_ring->channel); 1094650d1603SAlex Elder 1095650d1603SAlex Elder complete(&evt_ring->completion); 1096650d1603SAlex Elder dev_err(gsi->dev, "evt_ring for channel %u out of resources\n", 1097650d1603SAlex Elder channel_id); 1098650d1603SAlex Elder return; 1099650d1603SAlex Elder } 1100650d1603SAlex Elder 1101650d1603SAlex Elder /* Report, but otherwise ignore all other error codes */ 1102650d1603SAlex Elder dev_err(gsi->dev, "event ring %u global error ee %u code 0x%08x\n", 1103650d1603SAlex Elder evt_ring_id, err_ee, code); 1104650d1603SAlex Elder } 1105650d1603SAlex Elder 1106650d1603SAlex Elder /* Global error interrupt handler */ 1107650d1603SAlex Elder static void gsi_isr_glob_err(struct gsi *gsi) 1108650d1603SAlex Elder { 1109650d1603SAlex Elder enum gsi_err_type type; 1110650d1603SAlex Elder enum gsi_err_code code; 1111650d1603SAlex Elder u32 which; 1112650d1603SAlex Elder u32 val; 1113650d1603SAlex Elder u32 ee; 1114650d1603SAlex Elder 1115650d1603SAlex Elder /* Get the logged error, then reinitialize the log */ 1116650d1603SAlex Elder val = ioread32(gsi->virt + GSI_ERROR_LOG_OFFSET); 1117650d1603SAlex Elder iowrite32(0, gsi->virt + GSI_ERROR_LOG_OFFSET); 1118650d1603SAlex Elder iowrite32(~0, gsi->virt + GSI_ERROR_LOG_CLR_OFFSET); 1119650d1603SAlex Elder 1120650d1603SAlex Elder ee = u32_get_bits(val, ERR_EE_FMASK); 1121650d1603SAlex Elder type = u32_get_bits(val, ERR_TYPE_FMASK); 1122d6c9e3f5SAlex Elder which = u32_get_bits(val, ERR_VIRT_IDX_FMASK); 1123650d1603SAlex Elder code = u32_get_bits(val, ERR_CODE_FMASK); 1124650d1603SAlex Elder 1125650d1603SAlex Elder if (type == GSI_ERR_TYPE_CHAN) 1126650d1603SAlex Elder gsi_isr_glob_chan_err(gsi, ee, which, code); 1127650d1603SAlex Elder else if (type == GSI_ERR_TYPE_EVT) 1128650d1603SAlex Elder gsi_isr_glob_evt_err(gsi, ee, which, code); 1129650d1603SAlex Elder else /* type GSI_ERR_TYPE_GLOB should be fatal */ 1130650d1603SAlex Elder dev_err(gsi->dev, "unexpected global error 0x%08x\n", type); 1131650d1603SAlex Elder } 1132650d1603SAlex Elder 1133650d1603SAlex Elder /* Generic EE interrupt handler */ 1134650d1603SAlex Elder static void gsi_isr_gp_int1(struct gsi *gsi) 1135650d1603SAlex Elder { 1136650d1603SAlex Elder u32 result; 1137650d1603SAlex Elder u32 val; 1138650d1603SAlex Elder 1139f849afccSAlex Elder /* This interrupt is used to handle completions of the two GENERIC 1140f849afccSAlex Elder * GSI commands. We use these to allocate and halt channels on 1141f849afccSAlex Elder * the modem's behalf due to a hardware quirk on IPA v4.2. Once 1142f849afccSAlex Elder * allocated, the modem "owns" these channels, and as a result we 1143f849afccSAlex Elder * have no way of knowing the channel's state at any given time. 1144f849afccSAlex Elder * 1145f849afccSAlex Elder * It is recommended that we halt the modem channels we allocated 1146f849afccSAlex Elder * when shutting down, but it's possible the channel isn't running 1147f849afccSAlex Elder * at the time we issue the HALT command. We'll get an error in 1148f849afccSAlex Elder * that case, but it's harmless (the channel is already halted). 1149f849afccSAlex Elder * 1150f849afccSAlex Elder * For this reason, we silently ignore a CHANNEL_NOT_RUNNING error 1151f849afccSAlex Elder * if we receive it. 1152f849afccSAlex Elder */ 1153650d1603SAlex Elder val = ioread32(gsi->virt + GSI_CNTXT_SCRATCH_0_OFFSET); 1154650d1603SAlex Elder result = u32_get_bits(val, GENERIC_EE_RESULT_FMASK); 1155f849afccSAlex Elder 1156f849afccSAlex Elder switch (result) { 1157f849afccSAlex Elder case GENERIC_EE_SUCCESS: 1158f849afccSAlex Elder case GENERIC_EE_CHANNEL_NOT_RUNNING: 115911361456SAlex Elder gsi->result = 0; 116011361456SAlex Elder break; 116111361456SAlex Elder 116211361456SAlex Elder case GENERIC_EE_RETRY: 116311361456SAlex Elder gsi->result = -EAGAIN; 1164f849afccSAlex Elder break; 1165f849afccSAlex Elder 1166f849afccSAlex Elder default: 1167650d1603SAlex Elder dev_err(gsi->dev, "global INT1 generic result %u\n", result); 116811361456SAlex Elder gsi->result = -EIO; 1169f849afccSAlex Elder break; 1170f849afccSAlex Elder } 1171650d1603SAlex Elder 1172650d1603SAlex Elder complete(&gsi->completion); 1173650d1603SAlex Elder } 11740b1ba18aSAlex Elder 1175650d1603SAlex Elder /* Inter-EE interrupt handler */ 1176650d1603SAlex Elder static void gsi_isr_glob_ee(struct gsi *gsi) 1177650d1603SAlex Elder { 1178650d1603SAlex Elder u32 val; 1179650d1603SAlex Elder 1180650d1603SAlex Elder val = ioread32(gsi->virt + GSI_CNTXT_GLOB_IRQ_STTS_OFFSET); 1181650d1603SAlex Elder 11826c6358ccSAlex Elder if (val & BIT(ERROR_INT)) 1183650d1603SAlex Elder gsi_isr_glob_err(gsi); 1184650d1603SAlex Elder 1185650d1603SAlex Elder iowrite32(val, gsi->virt + GSI_CNTXT_GLOB_IRQ_CLR_OFFSET); 1186650d1603SAlex Elder 11876c6358ccSAlex Elder val &= ~BIT(ERROR_INT); 1188650d1603SAlex Elder 11896c6358ccSAlex Elder if (val & BIT(GP_INT1)) { 11906c6358ccSAlex Elder val ^= BIT(GP_INT1); 1191650d1603SAlex Elder gsi_isr_gp_int1(gsi); 1192650d1603SAlex Elder } 1193650d1603SAlex Elder 1194650d1603SAlex Elder if (val) 1195650d1603SAlex Elder dev_err(gsi->dev, "unexpected global interrupt 0x%08x\n", val); 1196650d1603SAlex Elder } 1197650d1603SAlex Elder 1198650d1603SAlex Elder /* I/O completion interrupt event */ 1199650d1603SAlex Elder static void gsi_isr_ieob(struct gsi *gsi) 1200650d1603SAlex Elder { 1201650d1603SAlex Elder u32 event_mask; 1202650d1603SAlex Elder 1203650d1603SAlex Elder event_mask = ioread32(gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_OFFSET); 1204195ef57fSAlex Elder iowrite32(event_mask, gsi->virt + GSI_CNTXT_SRC_IEOB_IRQ_CLR_OFFSET); 1205650d1603SAlex Elder 1206650d1603SAlex Elder while (event_mask) { 1207650d1603SAlex Elder u32 evt_ring_id = __ffs(event_mask); 1208650d1603SAlex Elder 1209650d1603SAlex Elder event_mask ^= BIT(evt_ring_id); 1210650d1603SAlex Elder 1211650d1603SAlex Elder gsi_irq_ieob_disable(gsi, evt_ring_id); 1212650d1603SAlex Elder napi_schedule(&gsi->evt_ring[evt_ring_id].channel->napi); 1213650d1603SAlex Elder } 1214650d1603SAlex Elder } 1215650d1603SAlex Elder 1216650d1603SAlex Elder /* General event interrupts represent serious problems, so report them */ 1217650d1603SAlex Elder static void gsi_isr_general(struct gsi *gsi) 1218650d1603SAlex Elder { 1219650d1603SAlex Elder struct device *dev = gsi->dev; 1220650d1603SAlex Elder u32 val; 1221650d1603SAlex Elder 1222650d1603SAlex Elder val = ioread32(gsi->virt + GSI_CNTXT_GSI_IRQ_STTS_OFFSET); 1223650d1603SAlex Elder iowrite32(val, gsi->virt + GSI_CNTXT_GSI_IRQ_CLR_OFFSET); 1224650d1603SAlex Elder 1225650d1603SAlex Elder dev_err(dev, "unexpected general interrupt 0x%08x\n", val); 1226650d1603SAlex Elder } 1227650d1603SAlex Elder 1228650d1603SAlex Elder /** 1229650d1603SAlex Elder * gsi_isr() - Top level GSI interrupt service routine 1230650d1603SAlex Elder * @irq: Interrupt number (ignored) 1231650d1603SAlex Elder * @dev_id: GSI pointer supplied to request_irq() 1232650d1603SAlex Elder * 1233650d1603SAlex Elder * This is the main handler function registered for the GSI IRQ. Each type 1234650d1603SAlex Elder * of interrupt has a separate handler function that is called from here. 1235650d1603SAlex Elder */ 1236650d1603SAlex Elder static irqreturn_t gsi_isr(int irq, void *dev_id) 1237650d1603SAlex Elder { 1238650d1603SAlex Elder struct gsi *gsi = dev_id; 1239650d1603SAlex Elder u32 intr_mask; 1240650d1603SAlex Elder u32 cnt = 0; 1241650d1603SAlex Elder 1242f9b28804SAlex Elder /* enum gsi_irq_type_id defines GSI interrupt types */ 1243650d1603SAlex Elder while ((intr_mask = ioread32(gsi->virt + GSI_CNTXT_TYPE_IRQ_OFFSET))) { 1244650d1603SAlex Elder /* intr_mask contains bitmask of pending GSI interrupts */ 1245650d1603SAlex Elder do { 1246650d1603SAlex Elder u32 gsi_intr = BIT(__ffs(intr_mask)); 1247650d1603SAlex Elder 1248650d1603SAlex Elder intr_mask ^= gsi_intr; 1249650d1603SAlex Elder 1250650d1603SAlex Elder switch (gsi_intr) { 1251f9b28804SAlex Elder case BIT(GSI_CH_CTRL): 1252650d1603SAlex Elder gsi_isr_chan_ctrl(gsi); 1253650d1603SAlex Elder break; 1254f9b28804SAlex Elder case BIT(GSI_EV_CTRL): 1255650d1603SAlex Elder gsi_isr_evt_ctrl(gsi); 1256650d1603SAlex Elder break; 1257f9b28804SAlex Elder case BIT(GSI_GLOB_EE): 1258650d1603SAlex Elder gsi_isr_glob_ee(gsi); 1259650d1603SAlex Elder break; 1260f9b28804SAlex Elder case BIT(GSI_IEOB): 1261650d1603SAlex Elder gsi_isr_ieob(gsi); 1262650d1603SAlex Elder break; 1263f9b28804SAlex Elder case BIT(GSI_GENERAL): 1264650d1603SAlex Elder gsi_isr_general(gsi); 1265650d1603SAlex Elder break; 1266650d1603SAlex Elder default: 1267650d1603SAlex Elder dev_err(gsi->dev, 12688463488aSAlex Elder "unrecognized interrupt type 0x%08x\n", 12698463488aSAlex Elder gsi_intr); 1270650d1603SAlex Elder break; 1271650d1603SAlex Elder } 1272650d1603SAlex Elder } while (intr_mask); 1273650d1603SAlex Elder 1274650d1603SAlex Elder if (++cnt > GSI_ISR_MAX_ITER) { 1275650d1603SAlex Elder dev_err(gsi->dev, "interrupt flood\n"); 1276650d1603SAlex Elder break; 1277650d1603SAlex Elder } 1278650d1603SAlex Elder } 1279650d1603SAlex Elder 1280650d1603SAlex Elder return IRQ_HANDLED; 1281650d1603SAlex Elder } 1282650d1603SAlex Elder 12830b8d6761SAlex Elder static int gsi_irq_init(struct gsi *gsi, struct platform_device *pdev) 12840b8d6761SAlex Elder { 12850b8d6761SAlex Elder struct device *dev = &pdev->dev; 12860b8d6761SAlex Elder unsigned int irq; 12870b8d6761SAlex Elder int ret; 12880b8d6761SAlex Elder 12890b8d6761SAlex Elder ret = platform_get_irq_byname(pdev, "gsi"); 12900b8d6761SAlex Elder if (ret <= 0) { 12910b8d6761SAlex Elder dev_err(dev, "DT error %d getting \"gsi\" IRQ property\n", ret); 12920b8d6761SAlex Elder return ret ? : -EINVAL; 12930b8d6761SAlex Elder } 12940b8d6761SAlex Elder irq = ret; 12950b8d6761SAlex Elder 12960b8d6761SAlex Elder ret = request_irq(irq, gsi_isr, 0, "gsi", gsi); 12970b8d6761SAlex Elder if (ret) { 12980b8d6761SAlex Elder dev_err(dev, "error %d requesting \"gsi\" IRQ\n", ret); 12990b8d6761SAlex Elder return ret; 13000b8d6761SAlex Elder } 13010b8d6761SAlex Elder gsi->irq = irq; 13020b8d6761SAlex Elder 13030b8d6761SAlex Elder return 0; 13040b8d6761SAlex Elder } 13050b8d6761SAlex Elder 13060b8d6761SAlex Elder static void gsi_irq_exit(struct gsi *gsi) 13070b8d6761SAlex Elder { 13080b8d6761SAlex Elder free_irq(gsi->irq, gsi); 13090b8d6761SAlex Elder } 13100b8d6761SAlex Elder 1311650d1603SAlex Elder /* Return the transaction associated with a transfer completion event */ 1312650d1603SAlex Elder static struct gsi_trans *gsi_event_trans(struct gsi_channel *channel, 1313650d1603SAlex Elder struct gsi_event *event) 1314650d1603SAlex Elder { 1315650d1603SAlex Elder u32 tre_offset; 1316650d1603SAlex Elder u32 tre_index; 1317650d1603SAlex Elder 1318650d1603SAlex Elder /* Event xfer_ptr records the TRE it's associated with */ 1319650d1603SAlex Elder tre_offset = le64_to_cpu(event->xfer_ptr) & GENMASK(31, 0); 1320650d1603SAlex Elder tre_index = gsi_ring_index(&channel->tre_ring, tre_offset); 1321650d1603SAlex Elder 1322650d1603SAlex Elder return gsi_channel_trans_mapped(channel, tre_index); 1323650d1603SAlex Elder } 1324650d1603SAlex Elder 1325650d1603SAlex Elder /** 1326650d1603SAlex Elder * gsi_evt_ring_rx_update() - Record lengths of received data 1327650d1603SAlex Elder * @evt_ring: Event ring associated with channel that received packets 1328650d1603SAlex Elder * @index: Event index in ring reported by hardware 1329650d1603SAlex Elder * 1330650d1603SAlex Elder * Events for RX channels contain the actual number of bytes received into 1331650d1603SAlex Elder * the buffer. Every event has a transaction associated with it, and here 1332650d1603SAlex Elder * we update transactions to record their actual received lengths. 1333650d1603SAlex Elder * 1334650d1603SAlex Elder * This function is called whenever we learn that the GSI hardware has filled 1335650d1603SAlex Elder * new events since the last time we checked. The ring's index field tells 1336650d1603SAlex Elder * the first entry in need of processing. The index provided is the 1337650d1603SAlex Elder * first *unfilled* event in the ring (following the last filled one). 1338650d1603SAlex Elder * 1339650d1603SAlex Elder * Events are sequential within the event ring, and transactions are 1340650d1603SAlex Elder * sequential within the transaction pool. 1341650d1603SAlex Elder * 1342650d1603SAlex Elder * Note that @index always refers to an element *within* the event ring. 1343650d1603SAlex Elder */ 1344650d1603SAlex Elder static void gsi_evt_ring_rx_update(struct gsi_evt_ring *evt_ring, u32 index) 1345650d1603SAlex Elder { 1346650d1603SAlex Elder struct gsi_channel *channel = evt_ring->channel; 1347650d1603SAlex Elder struct gsi_ring *ring = &evt_ring->ring; 1348650d1603SAlex Elder struct gsi_trans_info *trans_info; 1349650d1603SAlex Elder struct gsi_event *event_done; 1350650d1603SAlex Elder struct gsi_event *event; 1351650d1603SAlex Elder struct gsi_trans *trans; 1352650d1603SAlex Elder u32 byte_count = 0; 1353650d1603SAlex Elder u32 old_index; 1354650d1603SAlex Elder u32 event_avail; 1355650d1603SAlex Elder 1356650d1603SAlex Elder trans_info = &channel->trans_info; 1357650d1603SAlex Elder 1358650d1603SAlex Elder /* We'll start with the oldest un-processed event. RX channels 1359650d1603SAlex Elder * replenish receive buffers in single-TRE transactions, so we 1360650d1603SAlex Elder * can just map that event to its transaction. Transactions 1361650d1603SAlex Elder * associated with completion events are consecutive. 1362650d1603SAlex Elder */ 1363650d1603SAlex Elder old_index = ring->index; 1364650d1603SAlex Elder event = gsi_ring_virt(ring, old_index); 1365650d1603SAlex Elder trans = gsi_event_trans(channel, event); 1366650d1603SAlex Elder 1367650d1603SAlex Elder /* Compute the number of events to process before we wrap, 1368650d1603SAlex Elder * and determine when we'll be done processing events. 1369650d1603SAlex Elder */ 1370650d1603SAlex Elder event_avail = ring->count - old_index % ring->count; 1371650d1603SAlex Elder event_done = gsi_ring_virt(ring, index); 1372650d1603SAlex Elder do { 1373650d1603SAlex Elder trans->len = __le16_to_cpu(event->len); 1374650d1603SAlex Elder byte_count += trans->len; 1375650d1603SAlex Elder 1376650d1603SAlex Elder /* Move on to the next event and transaction */ 1377650d1603SAlex Elder if (--event_avail) 1378650d1603SAlex Elder event++; 1379650d1603SAlex Elder else 1380650d1603SAlex Elder event = gsi_ring_virt(ring, 0); 1381650d1603SAlex Elder trans = gsi_trans_pool_next(&trans_info->pool, trans); 1382650d1603SAlex Elder } while (event != event_done); 1383650d1603SAlex Elder 1384650d1603SAlex Elder /* We record RX bytes when they are received */ 1385650d1603SAlex Elder channel->byte_count += byte_count; 1386650d1603SAlex Elder channel->trans_count++; 1387650d1603SAlex Elder } 1388650d1603SAlex Elder 1389650d1603SAlex Elder /* Initialize a ring, including allocating DMA memory for its entries */ 1390650d1603SAlex Elder static int gsi_ring_alloc(struct gsi *gsi, struct gsi_ring *ring, u32 count) 1391650d1603SAlex Elder { 1392650d1603SAlex Elder size_t size = count * GSI_RING_ELEMENT_SIZE; 1393650d1603SAlex Elder struct device *dev = gsi->dev; 1394650d1603SAlex Elder dma_addr_t addr; 1395650d1603SAlex Elder 1396650d1603SAlex Elder /* Hardware requires a 2^n ring size, with alignment equal to size */ 1397650d1603SAlex Elder ring->virt = dma_alloc_coherent(dev, size, &addr, GFP_KERNEL); 1398650d1603SAlex Elder if (ring->virt && addr % size) { 1399650d1603SAlex Elder dma_free_coherent(dev, size, ring->virt, ring->addr); 1400650d1603SAlex Elder dev_err(dev, "unable to alloc 0x%zx-aligned ring buffer\n", 1401650d1603SAlex Elder size); 1402650d1603SAlex Elder return -EINVAL; /* Not a good error value, but distinct */ 1403650d1603SAlex Elder } else if (!ring->virt) { 1404650d1603SAlex Elder return -ENOMEM; 1405650d1603SAlex Elder } 1406650d1603SAlex Elder ring->addr = addr; 1407650d1603SAlex Elder ring->count = count; 1408650d1603SAlex Elder 1409650d1603SAlex Elder return 0; 1410650d1603SAlex Elder } 1411650d1603SAlex Elder 1412650d1603SAlex Elder /* Free a previously-allocated ring */ 1413650d1603SAlex Elder static void gsi_ring_free(struct gsi *gsi, struct gsi_ring *ring) 1414650d1603SAlex Elder { 1415650d1603SAlex Elder size_t size = ring->count * GSI_RING_ELEMENT_SIZE; 1416650d1603SAlex Elder 1417650d1603SAlex Elder dma_free_coherent(gsi->dev, size, ring->virt, ring->addr); 1418650d1603SAlex Elder } 1419650d1603SAlex Elder 1420650d1603SAlex Elder /* Allocate an available event ring id */ 1421650d1603SAlex Elder static int gsi_evt_ring_id_alloc(struct gsi *gsi) 1422650d1603SAlex Elder { 1423650d1603SAlex Elder u32 evt_ring_id; 1424650d1603SAlex Elder 1425650d1603SAlex Elder if (gsi->event_bitmap == ~0U) { 1426650d1603SAlex Elder dev_err(gsi->dev, "event rings exhausted\n"); 1427650d1603SAlex Elder return -ENOSPC; 1428650d1603SAlex Elder } 1429650d1603SAlex Elder 1430650d1603SAlex Elder evt_ring_id = ffz(gsi->event_bitmap); 1431650d1603SAlex Elder gsi->event_bitmap |= BIT(evt_ring_id); 1432650d1603SAlex Elder 1433650d1603SAlex Elder return (int)evt_ring_id; 1434650d1603SAlex Elder } 1435650d1603SAlex Elder 1436650d1603SAlex Elder /* Free a previously-allocated event ring id */ 1437650d1603SAlex Elder static void gsi_evt_ring_id_free(struct gsi *gsi, u32 evt_ring_id) 1438650d1603SAlex Elder { 1439650d1603SAlex Elder gsi->event_bitmap &= ~BIT(evt_ring_id); 1440650d1603SAlex Elder } 1441650d1603SAlex Elder 1442650d1603SAlex Elder /* Ring a channel doorbell, reporting the first un-filled entry */ 1443650d1603SAlex Elder void gsi_channel_doorbell(struct gsi_channel *channel) 1444650d1603SAlex Elder { 1445650d1603SAlex Elder struct gsi_ring *tre_ring = &channel->tre_ring; 1446650d1603SAlex Elder u32 channel_id = gsi_channel_id(channel); 1447650d1603SAlex Elder struct gsi *gsi = channel->gsi; 1448650d1603SAlex Elder u32 val; 1449650d1603SAlex Elder 1450650d1603SAlex Elder /* Note: index *must* be used modulo the ring count here */ 1451650d1603SAlex Elder val = gsi_ring_addr(tre_ring, tre_ring->index % tre_ring->count); 1452650d1603SAlex Elder iowrite32(val, gsi->virt + GSI_CH_C_DOORBELL_0_OFFSET(channel_id)); 1453650d1603SAlex Elder } 1454650d1603SAlex Elder 1455650d1603SAlex Elder /* Consult hardware, move any newly completed transactions to completed list */ 1456650d1603SAlex Elder static void gsi_channel_update(struct gsi_channel *channel) 1457650d1603SAlex Elder { 1458650d1603SAlex Elder u32 evt_ring_id = channel->evt_ring_id; 1459650d1603SAlex Elder struct gsi *gsi = channel->gsi; 1460650d1603SAlex Elder struct gsi_evt_ring *evt_ring; 1461650d1603SAlex Elder struct gsi_trans *trans; 1462650d1603SAlex Elder struct gsi_ring *ring; 1463650d1603SAlex Elder u32 offset; 1464650d1603SAlex Elder u32 index; 1465650d1603SAlex Elder 1466650d1603SAlex Elder evt_ring = &gsi->evt_ring[evt_ring_id]; 1467650d1603SAlex Elder ring = &evt_ring->ring; 1468650d1603SAlex Elder 1469650d1603SAlex Elder /* See if there's anything new to process; if not, we're done. Note 1470650d1603SAlex Elder * that index always refers to an entry *within* the event ring. 1471650d1603SAlex Elder */ 1472650d1603SAlex Elder offset = GSI_EV_CH_E_CNTXT_4_OFFSET(evt_ring_id); 1473650d1603SAlex Elder index = gsi_ring_index(ring, ioread32(gsi->virt + offset)); 1474650d1603SAlex Elder if (index == ring->index % ring->count) 1475650d1603SAlex Elder return; 1476650d1603SAlex Elder 1477650d1603SAlex Elder /* Get the transaction for the latest completed event. Take a 1478650d1603SAlex Elder * reference to keep it from completing before we give the events 1479650d1603SAlex Elder * for this and previous transactions back to the hardware. 1480650d1603SAlex Elder */ 1481650d1603SAlex Elder trans = gsi_event_trans(channel, gsi_ring_virt(ring, index - 1)); 1482650d1603SAlex Elder refcount_inc(&trans->refcount); 1483650d1603SAlex Elder 1484650d1603SAlex Elder /* For RX channels, update each completed transaction with the number 1485650d1603SAlex Elder * of bytes that were actually received. For TX channels, report 1486650d1603SAlex Elder * the number of transactions and bytes this completion represents 1487650d1603SAlex Elder * up the network stack. 1488650d1603SAlex Elder */ 1489650d1603SAlex Elder if (channel->toward_ipa) 1490650d1603SAlex Elder gsi_channel_tx_update(channel, trans); 1491650d1603SAlex Elder else 1492650d1603SAlex Elder gsi_evt_ring_rx_update(evt_ring, index); 1493650d1603SAlex Elder 1494650d1603SAlex Elder gsi_trans_move_complete(trans); 1495650d1603SAlex Elder 1496650d1603SAlex Elder /* Tell the hardware we've handled these events */ 1497650d1603SAlex Elder gsi_evt_ring_doorbell(channel->gsi, channel->evt_ring_id, index); 1498650d1603SAlex Elder 1499650d1603SAlex Elder gsi_trans_free(trans); 1500650d1603SAlex Elder } 1501650d1603SAlex Elder 1502650d1603SAlex Elder /** 1503650d1603SAlex Elder * gsi_channel_poll_one() - Return a single completed transaction on a channel 1504650d1603SAlex Elder * @channel: Channel to be polled 1505650d1603SAlex Elder * 1506e3eea08eSAlex Elder * Return: Transaction pointer, or null if none are available 1507650d1603SAlex Elder * 1508650d1603SAlex Elder * This function returns the first entry on a channel's completed transaction 1509650d1603SAlex Elder * list. If that list is empty, the hardware is consulted to determine 1510650d1603SAlex Elder * whether any new transactions have completed. If so, they're moved to the 1511650d1603SAlex Elder * completed list and the new first entry is returned. If there are no more 1512650d1603SAlex Elder * completed transactions, a null pointer is returned. 1513650d1603SAlex Elder */ 1514650d1603SAlex Elder static struct gsi_trans *gsi_channel_poll_one(struct gsi_channel *channel) 1515650d1603SAlex Elder { 1516650d1603SAlex Elder struct gsi_trans *trans; 1517650d1603SAlex Elder 1518650d1603SAlex Elder /* Get the first transaction from the completed list */ 1519650d1603SAlex Elder trans = gsi_channel_trans_complete(channel); 1520650d1603SAlex Elder if (!trans) { 1521650d1603SAlex Elder /* List is empty; see if there's more to do */ 1522650d1603SAlex Elder gsi_channel_update(channel); 1523650d1603SAlex Elder trans = gsi_channel_trans_complete(channel); 1524650d1603SAlex Elder } 1525650d1603SAlex Elder 1526650d1603SAlex Elder if (trans) 1527650d1603SAlex Elder gsi_trans_move_polled(trans); 1528650d1603SAlex Elder 1529650d1603SAlex Elder return trans; 1530650d1603SAlex Elder } 1531650d1603SAlex Elder 1532650d1603SAlex Elder /** 1533650d1603SAlex Elder * gsi_channel_poll() - NAPI poll function for a channel 1534650d1603SAlex Elder * @napi: NAPI structure for the channel 1535650d1603SAlex Elder * @budget: Budget supplied by NAPI core 1536e3eea08eSAlex Elder * 1537e3eea08eSAlex Elder * Return: Number of items polled (<= budget) 1538650d1603SAlex Elder * 1539650d1603SAlex Elder * Single transactions completed by hardware are polled until either 1540650d1603SAlex Elder * the budget is exhausted, or there are no more. Each transaction 1541650d1603SAlex Elder * polled is passed to gsi_trans_complete(), to perform remaining 1542650d1603SAlex Elder * completion processing and retire/free the transaction. 1543650d1603SAlex Elder */ 1544650d1603SAlex Elder static int gsi_channel_poll(struct napi_struct *napi, int budget) 1545650d1603SAlex Elder { 1546650d1603SAlex Elder struct gsi_channel *channel; 1547650d1603SAlex Elder int count = 0; 1548650d1603SAlex Elder 1549650d1603SAlex Elder channel = container_of(napi, struct gsi_channel, napi); 1550650d1603SAlex Elder while (count < budget) { 1551650d1603SAlex Elder struct gsi_trans *trans; 1552650d1603SAlex Elder 1553f45a7bccSAlex Elder count++; 1554650d1603SAlex Elder trans = gsi_channel_poll_one(channel); 1555650d1603SAlex Elder if (!trans) 1556650d1603SAlex Elder break; 1557650d1603SAlex Elder gsi_trans_complete(trans); 1558650d1603SAlex Elder } 1559650d1603SAlex Elder 1560650d1603SAlex Elder if (count < budget) { 1561650d1603SAlex Elder napi_complete(&channel->napi); 1562650d1603SAlex Elder gsi_irq_ieob_enable(channel->gsi, channel->evt_ring_id); 1563650d1603SAlex Elder } 1564650d1603SAlex Elder 1565650d1603SAlex Elder return count; 1566650d1603SAlex Elder } 1567650d1603SAlex Elder 1568650d1603SAlex Elder /* The event bitmap represents which event ids are available for allocation. 1569650d1603SAlex Elder * Set bits are not available, clear bits can be used. This function 1570650d1603SAlex Elder * initializes the map so all events supported by the hardware are available, 1571650d1603SAlex Elder * then precludes any reserved events from being allocated. 1572650d1603SAlex Elder */ 1573650d1603SAlex Elder static u32 gsi_event_bitmap_init(u32 evt_ring_max) 1574650d1603SAlex Elder { 1575650d1603SAlex Elder u32 event_bitmap = GENMASK(BITS_PER_LONG - 1, evt_ring_max); 1576650d1603SAlex Elder 1577650d1603SAlex Elder event_bitmap |= GENMASK(GSI_MHI_EVENT_ID_END, GSI_MHI_EVENT_ID_START); 1578650d1603SAlex Elder 1579650d1603SAlex Elder return event_bitmap; 1580650d1603SAlex Elder } 1581650d1603SAlex Elder 1582650d1603SAlex Elder /* Setup function for event rings */ 1583650d1603SAlex Elder static void gsi_evt_ring_setup(struct gsi *gsi) 1584650d1603SAlex Elder { 1585650d1603SAlex Elder /* Nothing to do */ 1586650d1603SAlex Elder } 1587650d1603SAlex Elder 1588650d1603SAlex Elder /* Inverse of gsi_evt_ring_setup() */ 1589650d1603SAlex Elder static void gsi_evt_ring_teardown(struct gsi *gsi) 1590650d1603SAlex Elder { 1591650d1603SAlex Elder /* Nothing to do */ 1592650d1603SAlex Elder } 1593650d1603SAlex Elder 1594650d1603SAlex Elder /* Setup function for a single channel */ 1595d387c761SAlex Elder static int gsi_channel_setup_one(struct gsi *gsi, u32 channel_id) 1596650d1603SAlex Elder { 1597650d1603SAlex Elder struct gsi_channel *channel = &gsi->channel[channel_id]; 1598650d1603SAlex Elder u32 evt_ring_id = channel->evt_ring_id; 1599650d1603SAlex Elder int ret; 1600650d1603SAlex Elder 1601650d1603SAlex Elder if (!channel->gsi) 1602650d1603SAlex Elder return 0; /* Ignore uninitialized channels */ 1603650d1603SAlex Elder 1604650d1603SAlex Elder ret = gsi_evt_ring_alloc_command(gsi, evt_ring_id); 1605650d1603SAlex Elder if (ret) 1606650d1603SAlex Elder return ret; 1607650d1603SAlex Elder 1608650d1603SAlex Elder gsi_evt_ring_program(gsi, evt_ring_id); 1609650d1603SAlex Elder 1610650d1603SAlex Elder ret = gsi_channel_alloc_command(gsi, channel_id); 1611650d1603SAlex Elder if (ret) 1612650d1603SAlex Elder goto err_evt_ring_de_alloc; 1613650d1603SAlex Elder 1614d387c761SAlex Elder gsi_channel_program(channel, true); 1615650d1603SAlex Elder 1616650d1603SAlex Elder if (channel->toward_ipa) 1617650d1603SAlex Elder netif_tx_napi_add(&gsi->dummy_dev, &channel->napi, 1618650d1603SAlex Elder gsi_channel_poll, NAPI_POLL_WEIGHT); 1619650d1603SAlex Elder else 1620650d1603SAlex Elder netif_napi_add(&gsi->dummy_dev, &channel->napi, 1621650d1603SAlex Elder gsi_channel_poll, NAPI_POLL_WEIGHT); 1622650d1603SAlex Elder 1623650d1603SAlex Elder return 0; 1624650d1603SAlex Elder 1625650d1603SAlex Elder err_evt_ring_de_alloc: 1626650d1603SAlex Elder /* We've done nothing with the event ring yet so don't reset */ 1627650d1603SAlex Elder gsi_evt_ring_de_alloc_command(gsi, evt_ring_id); 1628650d1603SAlex Elder 1629650d1603SAlex Elder return ret; 1630650d1603SAlex Elder } 1631650d1603SAlex Elder 1632650d1603SAlex Elder /* Inverse of gsi_channel_setup_one() */ 1633650d1603SAlex Elder static void gsi_channel_teardown_one(struct gsi *gsi, u32 channel_id) 1634650d1603SAlex Elder { 1635650d1603SAlex Elder struct gsi_channel *channel = &gsi->channel[channel_id]; 1636650d1603SAlex Elder u32 evt_ring_id = channel->evt_ring_id; 1637650d1603SAlex Elder 1638650d1603SAlex Elder if (!channel->gsi) 1639650d1603SAlex Elder return; /* Ignore uninitialized channels */ 1640650d1603SAlex Elder 1641650d1603SAlex Elder netif_napi_del(&channel->napi); 1642650d1603SAlex Elder 1643650d1603SAlex Elder gsi_channel_deprogram(channel); 1644650d1603SAlex Elder gsi_channel_de_alloc_command(gsi, channel_id); 1645650d1603SAlex Elder gsi_evt_ring_reset_command(gsi, evt_ring_id); 1646650d1603SAlex Elder gsi_evt_ring_de_alloc_command(gsi, evt_ring_id); 1647650d1603SAlex Elder } 1648650d1603SAlex Elder 1649650d1603SAlex Elder static int gsi_generic_command(struct gsi *gsi, u32 channel_id, 1650650d1603SAlex Elder enum gsi_generic_cmd_opcode opcode) 1651650d1603SAlex Elder { 1652650d1603SAlex Elder struct completion *completion = &gsi->completion; 1653d9cbe818SAlex Elder bool timeout; 1654650d1603SAlex Elder u32 val; 1655650d1603SAlex Elder 1656d6c9e3f5SAlex Elder /* The error global interrupt type is always enabled (until we 1657d6c9e3f5SAlex Elder * teardown), so we won't change that. A generic EE command 1658d6c9e3f5SAlex Elder * completes with a GSI global interrupt of type GP_INT1. We 1659d6c9e3f5SAlex Elder * only perform one generic command at a time (to allocate or 1660d6c9e3f5SAlex Elder * halt a modem channel) and only from this function. So we 1661d6c9e3f5SAlex Elder * enable the GP_INT1 IRQ type here while we're expecting it. 1662d6c9e3f5SAlex Elder */ 16636c6358ccSAlex Elder val = BIT(ERROR_INT) | BIT(GP_INT1); 1664d6c9e3f5SAlex Elder iowrite32(val, gsi->virt + GSI_CNTXT_GLOB_IRQ_EN_OFFSET); 1665d6c9e3f5SAlex Elder 16660b1ba18aSAlex Elder /* First zero the result code field */ 16670b1ba18aSAlex Elder val = ioread32(gsi->virt + GSI_CNTXT_SCRATCH_0_OFFSET); 16680b1ba18aSAlex Elder val &= ~GENERIC_EE_RESULT_FMASK; 16690b1ba18aSAlex Elder iowrite32(val, gsi->virt + GSI_CNTXT_SCRATCH_0_OFFSET); 16700b1ba18aSAlex Elder 16710b1ba18aSAlex Elder /* Now issue the command */ 1672650d1603SAlex Elder val = u32_encode_bits(opcode, GENERIC_OPCODE_FMASK); 1673650d1603SAlex Elder val |= u32_encode_bits(channel_id, GENERIC_CHID_FMASK); 1674650d1603SAlex Elder val |= u32_encode_bits(GSI_EE_MODEM, GENERIC_EE_FMASK); 1675650d1603SAlex Elder 1676d9cbe818SAlex Elder timeout = !gsi_command(gsi, GSI_GENERIC_CMD_OFFSET, val, completion); 1677d6c9e3f5SAlex Elder 1678d6c9e3f5SAlex Elder /* Disable the GP_INT1 IRQ type again */ 16796c6358ccSAlex Elder iowrite32(BIT(ERROR_INT), gsi->virt + GSI_CNTXT_GLOB_IRQ_EN_OFFSET); 1680d6c9e3f5SAlex Elder 1681d9cbe818SAlex Elder if (!timeout) 168211361456SAlex Elder return gsi->result; 1683650d1603SAlex Elder 1684650d1603SAlex Elder dev_err(gsi->dev, "GSI generic command %u to channel %u timed out\n", 1685650d1603SAlex Elder opcode, channel_id); 1686650d1603SAlex Elder 1687650d1603SAlex Elder return -ETIMEDOUT; 1688650d1603SAlex Elder } 1689650d1603SAlex Elder 1690650d1603SAlex Elder static int gsi_modem_channel_alloc(struct gsi *gsi, u32 channel_id) 1691650d1603SAlex Elder { 1692650d1603SAlex Elder return gsi_generic_command(gsi, channel_id, 1693650d1603SAlex Elder GSI_GENERIC_ALLOCATE_CHANNEL); 1694650d1603SAlex Elder } 1695650d1603SAlex Elder 1696650d1603SAlex Elder static void gsi_modem_channel_halt(struct gsi *gsi, u32 channel_id) 1697650d1603SAlex Elder { 169811361456SAlex Elder u32 retries = GSI_CHANNEL_MODEM_HALT_RETRIES; 169911361456SAlex Elder int ret; 170011361456SAlex Elder 170111361456SAlex Elder do 170211361456SAlex Elder ret = gsi_generic_command(gsi, channel_id, 170311361456SAlex Elder GSI_GENERIC_HALT_CHANNEL); 170411361456SAlex Elder while (ret == -EAGAIN && retries--); 170511361456SAlex Elder 170611361456SAlex Elder if (ret) 170711361456SAlex Elder dev_err(gsi->dev, "error %d halting modem channel %u\n", 170811361456SAlex Elder ret, channel_id); 1709650d1603SAlex Elder } 1710650d1603SAlex Elder 1711650d1603SAlex Elder /* Setup function for channels */ 1712d387c761SAlex Elder static int gsi_channel_setup(struct gsi *gsi) 1713650d1603SAlex Elder { 1714650d1603SAlex Elder u32 channel_id = 0; 1715650d1603SAlex Elder u32 mask; 1716650d1603SAlex Elder int ret; 1717650d1603SAlex Elder 1718650d1603SAlex Elder gsi_evt_ring_setup(gsi); 1719650d1603SAlex Elder gsi_irq_enable(gsi); 1720650d1603SAlex Elder 1721650d1603SAlex Elder mutex_lock(&gsi->mutex); 1722650d1603SAlex Elder 1723650d1603SAlex Elder do { 1724d387c761SAlex Elder ret = gsi_channel_setup_one(gsi, channel_id); 1725650d1603SAlex Elder if (ret) 1726650d1603SAlex Elder goto err_unwind; 1727650d1603SAlex Elder } while (++channel_id < gsi->channel_count); 1728650d1603SAlex Elder 1729650d1603SAlex Elder /* Make sure no channels were defined that hardware does not support */ 1730650d1603SAlex Elder while (channel_id < GSI_CHANNEL_COUNT_MAX) { 1731650d1603SAlex Elder struct gsi_channel *channel = &gsi->channel[channel_id++]; 1732650d1603SAlex Elder 1733650d1603SAlex Elder if (!channel->gsi) 1734650d1603SAlex Elder continue; /* Ignore uninitialized channels */ 1735650d1603SAlex Elder 1736650d1603SAlex Elder dev_err(gsi->dev, "channel %u not supported by hardware\n", 1737650d1603SAlex Elder channel_id - 1); 1738650d1603SAlex Elder channel_id = gsi->channel_count; 1739650d1603SAlex Elder goto err_unwind; 1740650d1603SAlex Elder } 1741650d1603SAlex Elder 1742650d1603SAlex Elder /* Allocate modem channels if necessary */ 1743650d1603SAlex Elder mask = gsi->modem_channel_bitmap; 1744650d1603SAlex Elder while (mask) { 1745650d1603SAlex Elder u32 modem_channel_id = __ffs(mask); 1746650d1603SAlex Elder 1747650d1603SAlex Elder ret = gsi_modem_channel_alloc(gsi, modem_channel_id); 1748650d1603SAlex Elder if (ret) 1749650d1603SAlex Elder goto err_unwind_modem; 1750650d1603SAlex Elder 1751650d1603SAlex Elder /* Clear bit from mask only after success (for unwind) */ 1752650d1603SAlex Elder mask ^= BIT(modem_channel_id); 1753650d1603SAlex Elder } 1754650d1603SAlex Elder 1755650d1603SAlex Elder mutex_unlock(&gsi->mutex); 1756650d1603SAlex Elder 1757650d1603SAlex Elder return 0; 1758650d1603SAlex Elder 1759650d1603SAlex Elder err_unwind_modem: 1760650d1603SAlex Elder /* Compute which modem channels need to be deallocated */ 1761650d1603SAlex Elder mask ^= gsi->modem_channel_bitmap; 1762650d1603SAlex Elder while (mask) { 1763993cac15SAlex Elder channel_id = __fls(mask); 1764650d1603SAlex Elder 1765650d1603SAlex Elder mask ^= BIT(channel_id); 1766650d1603SAlex Elder 1767650d1603SAlex Elder gsi_modem_channel_halt(gsi, channel_id); 1768650d1603SAlex Elder } 1769650d1603SAlex Elder 1770650d1603SAlex Elder err_unwind: 1771650d1603SAlex Elder while (channel_id--) 1772650d1603SAlex Elder gsi_channel_teardown_one(gsi, channel_id); 1773650d1603SAlex Elder 1774650d1603SAlex Elder mutex_unlock(&gsi->mutex); 1775650d1603SAlex Elder 1776650d1603SAlex Elder gsi_irq_disable(gsi); 1777650d1603SAlex Elder gsi_evt_ring_teardown(gsi); 1778650d1603SAlex Elder 1779650d1603SAlex Elder return ret; 1780650d1603SAlex Elder } 1781650d1603SAlex Elder 1782650d1603SAlex Elder /* Inverse of gsi_channel_setup() */ 1783650d1603SAlex Elder static void gsi_channel_teardown(struct gsi *gsi) 1784650d1603SAlex Elder { 1785650d1603SAlex Elder u32 mask = gsi->modem_channel_bitmap; 1786650d1603SAlex Elder u32 channel_id; 1787650d1603SAlex Elder 1788650d1603SAlex Elder mutex_lock(&gsi->mutex); 1789650d1603SAlex Elder 1790650d1603SAlex Elder while (mask) { 1791993cac15SAlex Elder channel_id = __fls(mask); 1792650d1603SAlex Elder 1793650d1603SAlex Elder mask ^= BIT(channel_id); 1794650d1603SAlex Elder 1795650d1603SAlex Elder gsi_modem_channel_halt(gsi, channel_id); 1796650d1603SAlex Elder } 1797650d1603SAlex Elder 1798650d1603SAlex Elder channel_id = gsi->channel_count - 1; 1799650d1603SAlex Elder do 1800650d1603SAlex Elder gsi_channel_teardown_one(gsi, channel_id); 1801650d1603SAlex Elder while (channel_id--); 1802650d1603SAlex Elder 1803650d1603SAlex Elder mutex_unlock(&gsi->mutex); 1804650d1603SAlex Elder 1805650d1603SAlex Elder gsi_irq_disable(gsi); 1806650d1603SAlex Elder gsi_evt_ring_teardown(gsi); 1807650d1603SAlex Elder } 1808650d1603SAlex Elder 1809650d1603SAlex Elder /* Setup function for GSI. GSI firmware must be loaded and initialized */ 1810d387c761SAlex Elder int gsi_setup(struct gsi *gsi) 1811650d1603SAlex Elder { 18128463488aSAlex Elder struct device *dev = gsi->dev; 1813650d1603SAlex Elder u32 val; 181497eb94c8SAlex Elder int ret; 1815650d1603SAlex Elder 1816650d1603SAlex Elder /* Here is where we first touch the GSI hardware */ 1817650d1603SAlex Elder val = ioread32(gsi->virt + GSI_GSI_STATUS_OFFSET); 1818650d1603SAlex Elder if (!(val & ENABLED_FMASK)) { 18198463488aSAlex Elder dev_err(dev, "GSI has not been enabled\n"); 1820650d1603SAlex Elder return -EIO; 1821650d1603SAlex Elder } 1822650d1603SAlex Elder 182397eb94c8SAlex Elder gsi_irq_setup(gsi); 182497eb94c8SAlex Elder 1825650d1603SAlex Elder val = ioread32(gsi->virt + GSI_GSI_HW_PARAM_2_OFFSET); 1826650d1603SAlex Elder 1827650d1603SAlex Elder gsi->channel_count = u32_get_bits(val, NUM_CH_PER_EE_FMASK); 1828650d1603SAlex Elder if (!gsi->channel_count) { 18298463488aSAlex Elder dev_err(dev, "GSI reports zero channels supported\n"); 1830650d1603SAlex Elder return -EINVAL; 1831650d1603SAlex Elder } 1832650d1603SAlex Elder if (gsi->channel_count > GSI_CHANNEL_COUNT_MAX) { 18338463488aSAlex Elder dev_warn(dev, 18348463488aSAlex Elder "limiting to %u channels; hardware supports %u\n", 1835650d1603SAlex Elder GSI_CHANNEL_COUNT_MAX, gsi->channel_count); 1836650d1603SAlex Elder gsi->channel_count = GSI_CHANNEL_COUNT_MAX; 1837650d1603SAlex Elder } 1838650d1603SAlex Elder 1839650d1603SAlex Elder gsi->evt_ring_count = u32_get_bits(val, NUM_EV_PER_EE_FMASK); 1840650d1603SAlex Elder if (!gsi->evt_ring_count) { 18418463488aSAlex Elder dev_err(dev, "GSI reports zero event rings supported\n"); 1842650d1603SAlex Elder return -EINVAL; 1843650d1603SAlex Elder } 1844650d1603SAlex Elder if (gsi->evt_ring_count > GSI_EVT_RING_COUNT_MAX) { 18458463488aSAlex Elder dev_warn(dev, 18468463488aSAlex Elder "limiting to %u event rings; hardware supports %u\n", 1847650d1603SAlex Elder GSI_EVT_RING_COUNT_MAX, gsi->evt_ring_count); 1848650d1603SAlex Elder gsi->evt_ring_count = GSI_EVT_RING_COUNT_MAX; 1849650d1603SAlex Elder } 1850650d1603SAlex Elder 1851650d1603SAlex Elder /* Initialize the error log */ 1852650d1603SAlex Elder iowrite32(0, gsi->virt + GSI_ERROR_LOG_OFFSET); 1853650d1603SAlex Elder 1854650d1603SAlex Elder /* Writing 1 indicates IRQ interrupts; 0 would be MSI */ 1855650d1603SAlex Elder iowrite32(1, gsi->virt + GSI_CNTXT_INTSET_OFFSET); 1856650d1603SAlex Elder 185797eb94c8SAlex Elder ret = gsi_channel_setup(gsi); 185897eb94c8SAlex Elder if (ret) 185997eb94c8SAlex Elder gsi_irq_teardown(gsi); 186097eb94c8SAlex Elder 186197eb94c8SAlex Elder return ret; 1862650d1603SAlex Elder } 1863650d1603SAlex Elder 1864650d1603SAlex Elder /* Inverse of gsi_setup() */ 1865650d1603SAlex Elder void gsi_teardown(struct gsi *gsi) 1866650d1603SAlex Elder { 1867650d1603SAlex Elder gsi_channel_teardown(gsi); 186897eb94c8SAlex Elder gsi_irq_teardown(gsi); 1869650d1603SAlex Elder } 1870650d1603SAlex Elder 1871650d1603SAlex Elder /* Initialize a channel's event ring */ 1872650d1603SAlex Elder static int gsi_channel_evt_ring_init(struct gsi_channel *channel) 1873650d1603SAlex Elder { 1874650d1603SAlex Elder struct gsi *gsi = channel->gsi; 1875650d1603SAlex Elder struct gsi_evt_ring *evt_ring; 1876650d1603SAlex Elder int ret; 1877650d1603SAlex Elder 1878650d1603SAlex Elder ret = gsi_evt_ring_id_alloc(gsi); 1879650d1603SAlex Elder if (ret < 0) 1880650d1603SAlex Elder return ret; 1881650d1603SAlex Elder channel->evt_ring_id = ret; 1882650d1603SAlex Elder 1883650d1603SAlex Elder evt_ring = &gsi->evt_ring[channel->evt_ring_id]; 1884650d1603SAlex Elder evt_ring->channel = channel; 1885650d1603SAlex Elder 1886650d1603SAlex Elder ret = gsi_ring_alloc(gsi, &evt_ring->ring, channel->event_count); 1887650d1603SAlex Elder if (!ret) 1888650d1603SAlex Elder return 0; /* Success! */ 1889650d1603SAlex Elder 1890650d1603SAlex Elder dev_err(gsi->dev, "error %d allocating channel %u event ring\n", 1891650d1603SAlex Elder ret, gsi_channel_id(channel)); 1892650d1603SAlex Elder 1893650d1603SAlex Elder gsi_evt_ring_id_free(gsi, channel->evt_ring_id); 1894650d1603SAlex Elder 1895650d1603SAlex Elder return ret; 1896650d1603SAlex Elder } 1897650d1603SAlex Elder 1898650d1603SAlex Elder /* Inverse of gsi_channel_evt_ring_init() */ 1899650d1603SAlex Elder static void gsi_channel_evt_ring_exit(struct gsi_channel *channel) 1900650d1603SAlex Elder { 1901650d1603SAlex Elder u32 evt_ring_id = channel->evt_ring_id; 1902650d1603SAlex Elder struct gsi *gsi = channel->gsi; 1903650d1603SAlex Elder struct gsi_evt_ring *evt_ring; 1904650d1603SAlex Elder 1905650d1603SAlex Elder evt_ring = &gsi->evt_ring[evt_ring_id]; 1906650d1603SAlex Elder gsi_ring_free(gsi, &evt_ring->ring); 1907650d1603SAlex Elder gsi_evt_ring_id_free(gsi, evt_ring_id); 1908650d1603SAlex Elder } 1909650d1603SAlex Elder 1910650d1603SAlex Elder /* Init function for event rings */ 1911650d1603SAlex Elder static void gsi_evt_ring_init(struct gsi *gsi) 1912650d1603SAlex Elder { 1913650d1603SAlex Elder u32 evt_ring_id = 0; 1914650d1603SAlex Elder 1915650d1603SAlex Elder gsi->event_bitmap = gsi_event_bitmap_init(GSI_EVT_RING_COUNT_MAX); 1916a054539dSAlex Elder gsi->ieob_enabled_bitmap = 0; 1917650d1603SAlex Elder do 1918650d1603SAlex Elder init_completion(&gsi->evt_ring[evt_ring_id].completion); 1919650d1603SAlex Elder while (++evt_ring_id < GSI_EVT_RING_COUNT_MAX); 1920650d1603SAlex Elder } 1921650d1603SAlex Elder 1922650d1603SAlex Elder /* Inverse of gsi_evt_ring_init() */ 1923650d1603SAlex Elder static void gsi_evt_ring_exit(struct gsi *gsi) 1924650d1603SAlex Elder { 1925650d1603SAlex Elder /* Nothing to do */ 1926650d1603SAlex Elder } 1927650d1603SAlex Elder 1928650d1603SAlex Elder static bool gsi_channel_data_valid(struct gsi *gsi, 1929650d1603SAlex Elder const struct ipa_gsi_endpoint_data *data) 1930650d1603SAlex Elder { 1931650d1603SAlex Elder #ifdef IPA_VALIDATION 1932650d1603SAlex Elder u32 channel_id = data->channel_id; 1933650d1603SAlex Elder struct device *dev = gsi->dev; 1934650d1603SAlex Elder 1935650d1603SAlex Elder /* Make sure channel ids are in the range driver supports */ 1936650d1603SAlex Elder if (channel_id >= GSI_CHANNEL_COUNT_MAX) { 19378463488aSAlex Elder dev_err(dev, "bad channel id %u; must be less than %u\n", 1938650d1603SAlex Elder channel_id, GSI_CHANNEL_COUNT_MAX); 1939650d1603SAlex Elder return false; 1940650d1603SAlex Elder } 1941650d1603SAlex Elder 1942650d1603SAlex Elder if (data->ee_id != GSI_EE_AP && data->ee_id != GSI_EE_MODEM) { 19438463488aSAlex Elder dev_err(dev, "bad EE id %u; not AP or modem\n", data->ee_id); 1944650d1603SAlex Elder return false; 1945650d1603SAlex Elder } 1946650d1603SAlex Elder 1947650d1603SAlex Elder if (!data->channel.tlv_count || 1948650d1603SAlex Elder data->channel.tlv_count > GSI_TLV_MAX) { 19498463488aSAlex Elder dev_err(dev, "channel %u bad tlv_count %u; must be 1..%u\n", 1950650d1603SAlex Elder channel_id, data->channel.tlv_count, GSI_TLV_MAX); 1951650d1603SAlex Elder return false; 1952650d1603SAlex Elder } 1953650d1603SAlex Elder 1954650d1603SAlex Elder /* We have to allow at least one maximally-sized transaction to 1955650d1603SAlex Elder * be outstanding (which would use tlv_count TREs). Given how 1956650d1603SAlex Elder * gsi_channel_tre_max() is computed, tre_count has to be almost 1957650d1603SAlex Elder * twice the TLV FIFO size to satisfy this requirement. 1958650d1603SAlex Elder */ 1959650d1603SAlex Elder if (data->channel.tre_count < 2 * data->channel.tlv_count - 1) { 1960650d1603SAlex Elder dev_err(dev, "channel %u TLV count %u exceeds TRE count %u\n", 1961650d1603SAlex Elder channel_id, data->channel.tlv_count, 1962650d1603SAlex Elder data->channel.tre_count); 1963650d1603SAlex Elder return false; 1964650d1603SAlex Elder } 1965650d1603SAlex Elder 1966650d1603SAlex Elder if (!is_power_of_2(data->channel.tre_count)) { 19678463488aSAlex Elder dev_err(dev, "channel %u bad tre_count %u; not power of 2\n", 1968650d1603SAlex Elder channel_id, data->channel.tre_count); 1969650d1603SAlex Elder return false; 1970650d1603SAlex Elder } 1971650d1603SAlex Elder 1972650d1603SAlex Elder if (!is_power_of_2(data->channel.event_count)) { 19738463488aSAlex Elder dev_err(dev, "channel %u bad event_count %u; not power of 2\n", 1974650d1603SAlex Elder channel_id, data->channel.event_count); 1975650d1603SAlex Elder return false; 1976650d1603SAlex Elder } 1977650d1603SAlex Elder #endif /* IPA_VALIDATION */ 1978650d1603SAlex Elder 1979650d1603SAlex Elder return true; 1980650d1603SAlex Elder } 1981650d1603SAlex Elder 1982650d1603SAlex Elder /* Init function for a single channel */ 1983650d1603SAlex Elder static int gsi_channel_init_one(struct gsi *gsi, 1984650d1603SAlex Elder const struct ipa_gsi_endpoint_data *data, 198514dbf977SAlex Elder bool command) 1986650d1603SAlex Elder { 1987650d1603SAlex Elder struct gsi_channel *channel; 1988650d1603SAlex Elder u32 tre_count; 1989650d1603SAlex Elder int ret; 1990650d1603SAlex Elder 1991650d1603SAlex Elder if (!gsi_channel_data_valid(gsi, data)) 1992650d1603SAlex Elder return -EINVAL; 1993650d1603SAlex Elder 1994650d1603SAlex Elder /* Worst case we need an event for every outstanding TRE */ 1995650d1603SAlex Elder if (data->channel.tre_count > data->channel.event_count) { 1996650d1603SAlex Elder tre_count = data->channel.event_count; 19970721999fSAlex Elder dev_warn(gsi->dev, "channel %u limited to %u TREs\n", 19980721999fSAlex Elder data->channel_id, tre_count); 1999650d1603SAlex Elder } else { 2000650d1603SAlex Elder tre_count = data->channel.tre_count; 2001650d1603SAlex Elder } 2002650d1603SAlex Elder 2003650d1603SAlex Elder channel = &gsi->channel[data->channel_id]; 2004650d1603SAlex Elder memset(channel, 0, sizeof(*channel)); 2005650d1603SAlex Elder 2006650d1603SAlex Elder channel->gsi = gsi; 2007650d1603SAlex Elder channel->toward_ipa = data->toward_ipa; 2008650d1603SAlex Elder channel->command = command; 2009650d1603SAlex Elder channel->tlv_count = data->channel.tlv_count; 2010650d1603SAlex Elder channel->tre_count = tre_count; 2011650d1603SAlex Elder channel->event_count = data->channel.event_count; 2012650d1603SAlex Elder init_completion(&channel->completion); 2013650d1603SAlex Elder 2014650d1603SAlex Elder ret = gsi_channel_evt_ring_init(channel); 2015650d1603SAlex Elder if (ret) 2016650d1603SAlex Elder goto err_clear_gsi; 2017650d1603SAlex Elder 2018650d1603SAlex Elder ret = gsi_ring_alloc(gsi, &channel->tre_ring, data->channel.tre_count); 2019650d1603SAlex Elder if (ret) { 2020650d1603SAlex Elder dev_err(gsi->dev, "error %d allocating channel %u ring\n", 2021650d1603SAlex Elder ret, data->channel_id); 2022650d1603SAlex Elder goto err_channel_evt_ring_exit; 2023650d1603SAlex Elder } 2024650d1603SAlex Elder 2025650d1603SAlex Elder ret = gsi_channel_trans_init(gsi, data->channel_id); 2026650d1603SAlex Elder if (ret) 2027650d1603SAlex Elder goto err_ring_free; 2028650d1603SAlex Elder 2029650d1603SAlex Elder if (command) { 2030650d1603SAlex Elder u32 tre_max = gsi_channel_tre_max(gsi, data->channel_id); 2031650d1603SAlex Elder 2032650d1603SAlex Elder ret = ipa_cmd_pool_init(channel, tre_max); 2033650d1603SAlex Elder } 2034650d1603SAlex Elder if (!ret) 2035650d1603SAlex Elder return 0; /* Success! */ 2036650d1603SAlex Elder 2037650d1603SAlex Elder gsi_channel_trans_exit(channel); 2038650d1603SAlex Elder err_ring_free: 2039650d1603SAlex Elder gsi_ring_free(gsi, &channel->tre_ring); 2040650d1603SAlex Elder err_channel_evt_ring_exit: 2041650d1603SAlex Elder gsi_channel_evt_ring_exit(channel); 2042650d1603SAlex Elder err_clear_gsi: 2043650d1603SAlex Elder channel->gsi = NULL; /* Mark it not (fully) initialized */ 2044650d1603SAlex Elder 2045650d1603SAlex Elder return ret; 2046650d1603SAlex Elder } 2047650d1603SAlex Elder 2048650d1603SAlex Elder /* Inverse of gsi_channel_init_one() */ 2049650d1603SAlex Elder static void gsi_channel_exit_one(struct gsi_channel *channel) 2050650d1603SAlex Elder { 2051650d1603SAlex Elder if (!channel->gsi) 2052650d1603SAlex Elder return; /* Ignore uninitialized channels */ 2053650d1603SAlex Elder 2054650d1603SAlex Elder if (channel->command) 2055650d1603SAlex Elder ipa_cmd_pool_exit(channel); 2056650d1603SAlex Elder gsi_channel_trans_exit(channel); 2057650d1603SAlex Elder gsi_ring_free(channel->gsi, &channel->tre_ring); 2058650d1603SAlex Elder gsi_channel_evt_ring_exit(channel); 2059650d1603SAlex Elder } 2060650d1603SAlex Elder 2061650d1603SAlex Elder /* Init function for channels */ 206214dbf977SAlex Elder static int gsi_channel_init(struct gsi *gsi, u32 count, 206356dfe8deSAlex Elder const struct ipa_gsi_endpoint_data *data) 2064650d1603SAlex Elder { 206556dfe8deSAlex Elder bool modem_alloc; 2066650d1603SAlex Elder int ret = 0; 2067650d1603SAlex Elder u32 i; 2068650d1603SAlex Elder 206956dfe8deSAlex Elder /* IPA v4.2 requires the AP to allocate channels for the modem */ 207056dfe8deSAlex Elder modem_alloc = gsi->version == IPA_VERSION_4_2; 207156dfe8deSAlex Elder 2072650d1603SAlex Elder gsi_evt_ring_init(gsi); 2073650d1603SAlex Elder 2074650d1603SAlex Elder /* The endpoint data array is indexed by endpoint name */ 2075650d1603SAlex Elder for (i = 0; i < count; i++) { 2076650d1603SAlex Elder bool command = i == IPA_ENDPOINT_AP_COMMAND_TX; 2077650d1603SAlex Elder 2078650d1603SAlex Elder if (ipa_gsi_endpoint_data_empty(&data[i])) 2079650d1603SAlex Elder continue; /* Skip over empty slots */ 2080650d1603SAlex Elder 2081650d1603SAlex Elder /* Mark modem channels to be allocated (hardware workaround) */ 2082650d1603SAlex Elder if (data[i].ee_id == GSI_EE_MODEM) { 2083650d1603SAlex Elder if (modem_alloc) 2084650d1603SAlex Elder gsi->modem_channel_bitmap |= 2085650d1603SAlex Elder BIT(data[i].channel_id); 2086650d1603SAlex Elder continue; 2087650d1603SAlex Elder } 2088650d1603SAlex Elder 208914dbf977SAlex Elder ret = gsi_channel_init_one(gsi, &data[i], command); 2090650d1603SAlex Elder if (ret) 2091650d1603SAlex Elder goto err_unwind; 2092650d1603SAlex Elder } 2093650d1603SAlex Elder 2094650d1603SAlex Elder return ret; 2095650d1603SAlex Elder 2096650d1603SAlex Elder err_unwind: 2097650d1603SAlex Elder while (i--) { 2098650d1603SAlex Elder if (ipa_gsi_endpoint_data_empty(&data[i])) 2099650d1603SAlex Elder continue; 2100650d1603SAlex Elder if (modem_alloc && data[i].ee_id == GSI_EE_MODEM) { 2101650d1603SAlex Elder gsi->modem_channel_bitmap &= ~BIT(data[i].channel_id); 2102650d1603SAlex Elder continue; 2103650d1603SAlex Elder } 2104650d1603SAlex Elder gsi_channel_exit_one(&gsi->channel[data->channel_id]); 2105650d1603SAlex Elder } 2106650d1603SAlex Elder gsi_evt_ring_exit(gsi); 2107650d1603SAlex Elder 2108650d1603SAlex Elder return ret; 2109650d1603SAlex Elder } 2110650d1603SAlex Elder 2111650d1603SAlex Elder /* Inverse of gsi_channel_init() */ 2112650d1603SAlex Elder static void gsi_channel_exit(struct gsi *gsi) 2113650d1603SAlex Elder { 2114650d1603SAlex Elder u32 channel_id = GSI_CHANNEL_COUNT_MAX - 1; 2115650d1603SAlex Elder 2116650d1603SAlex Elder do 2117650d1603SAlex Elder gsi_channel_exit_one(&gsi->channel[channel_id]); 2118650d1603SAlex Elder while (channel_id--); 2119650d1603SAlex Elder gsi->modem_channel_bitmap = 0; 2120650d1603SAlex Elder 2121650d1603SAlex Elder gsi_evt_ring_exit(gsi); 2122650d1603SAlex Elder } 2123650d1603SAlex Elder 2124650d1603SAlex Elder /* Init function for GSI. GSI hardware does not need to be "ready" */ 21251d0c09deSAlex Elder int gsi_init(struct gsi *gsi, struct platform_device *pdev, 21261d0c09deSAlex Elder enum ipa_version version, u32 count, 21271d0c09deSAlex Elder const struct ipa_gsi_endpoint_data *data) 2128650d1603SAlex Elder { 21298463488aSAlex Elder struct device *dev = &pdev->dev; 2130650d1603SAlex Elder struct resource *res; 2131650d1603SAlex Elder resource_size_t size; 2132cdeee49fSAlex Elder u32 adjust; 2133650d1603SAlex Elder int ret; 2134650d1603SAlex Elder 2135650d1603SAlex Elder gsi_validate_build(); 2136650d1603SAlex Elder 21378463488aSAlex Elder gsi->dev = dev; 213814dbf977SAlex Elder gsi->version = version; 2139650d1603SAlex Elder 2140650d1603SAlex Elder /* The GSI layer performs NAPI on all endpoints. NAPI requires a 2141650d1603SAlex Elder * network device structure, but the GSI layer does not have one, 2142650d1603SAlex Elder * so we must create a dummy network device for this purpose. 2143650d1603SAlex Elder */ 2144650d1603SAlex Elder init_dummy_netdev(&gsi->dummy_dev); 2145650d1603SAlex Elder 2146650d1603SAlex Elder /* Get GSI memory range and map it */ 2147650d1603SAlex Elder res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "gsi"); 2148650d1603SAlex Elder if (!res) { 21498463488aSAlex Elder dev_err(dev, "DT error getting \"gsi\" memory property\n"); 21500b8d6761SAlex Elder return -ENODEV; 2151650d1603SAlex Elder } 2152650d1603SAlex Elder 2153650d1603SAlex Elder size = resource_size(res); 2154650d1603SAlex Elder if (res->start > U32_MAX || size > U32_MAX - res->start) { 21558463488aSAlex Elder dev_err(dev, "DT memory resource \"gsi\" out of range\n"); 21560b8d6761SAlex Elder return -EINVAL; 2157650d1603SAlex Elder } 2158650d1603SAlex Elder 2159cdeee49fSAlex Elder /* Make sure we can make our pointer adjustment if necessary */ 2160cdeee49fSAlex Elder adjust = gsi->version < IPA_VERSION_4_5 ? 0 : GSI_EE_REG_ADJUST; 2161cdeee49fSAlex Elder if (res->start < adjust) { 2162cdeee49fSAlex Elder dev_err(dev, "DT memory resource \"gsi\" too low (< %u)\n", 2163cdeee49fSAlex Elder adjust); 2164cdeee49fSAlex Elder return -EINVAL; 2165cdeee49fSAlex Elder } 2166cdeee49fSAlex Elder 2167650d1603SAlex Elder gsi->virt = ioremap(res->start, size); 2168650d1603SAlex Elder if (!gsi->virt) { 21698463488aSAlex Elder dev_err(dev, "unable to remap \"gsi\" memory\n"); 21700b8d6761SAlex Elder return -ENOMEM; 2171650d1603SAlex Elder } 2172cdeee49fSAlex Elder /* Adjust register range pointer downward for newer IPA versions */ 2173cdeee49fSAlex Elder gsi->virt -= adjust; 2174650d1603SAlex Elder 21750b8d6761SAlex Elder init_completion(&gsi->completion); 21760b8d6761SAlex Elder 21770b8d6761SAlex Elder ret = gsi_irq_init(gsi, pdev); 2178650d1603SAlex Elder if (ret) 2179650d1603SAlex Elder goto err_iounmap; 2180650d1603SAlex Elder 21810b8d6761SAlex Elder ret = gsi_channel_init(gsi, count, data); 21820b8d6761SAlex Elder if (ret) 21830b8d6761SAlex Elder goto err_irq_exit; 21840b8d6761SAlex Elder 2185650d1603SAlex Elder mutex_init(&gsi->mutex); 2186650d1603SAlex Elder 2187650d1603SAlex Elder return 0; 2188650d1603SAlex Elder 21890b8d6761SAlex Elder err_irq_exit: 21900b8d6761SAlex Elder gsi_irq_exit(gsi); 2191650d1603SAlex Elder err_iounmap: 2192650d1603SAlex Elder iounmap(gsi->virt); 2193650d1603SAlex Elder 2194650d1603SAlex Elder return ret; 2195650d1603SAlex Elder } 2196650d1603SAlex Elder 2197650d1603SAlex Elder /* Inverse of gsi_init() */ 2198650d1603SAlex Elder void gsi_exit(struct gsi *gsi) 2199650d1603SAlex Elder { 2200650d1603SAlex Elder mutex_destroy(&gsi->mutex); 2201650d1603SAlex Elder gsi_channel_exit(gsi); 22020b8d6761SAlex Elder gsi_irq_exit(gsi); 2203650d1603SAlex Elder iounmap(gsi->virt); 2204650d1603SAlex Elder } 2205650d1603SAlex Elder 2206650d1603SAlex Elder /* The maximum number of outstanding TREs on a channel. This limits 2207650d1603SAlex Elder * a channel's maximum number of transactions outstanding (worst case 2208650d1603SAlex Elder * is one TRE per transaction). 2209650d1603SAlex Elder * 2210650d1603SAlex Elder * The absolute limit is the number of TREs in the channel's TRE ring, 2211650d1603SAlex Elder * and in theory we should be able use all of them. But in practice, 2212650d1603SAlex Elder * doing that led to the hardware reporting exhaustion of event ring 2213650d1603SAlex Elder * slots for writing completion information. So the hardware limit 2214650d1603SAlex Elder * would be (tre_count - 1). 2215650d1603SAlex Elder * 2216650d1603SAlex Elder * We reduce it a bit further though. Transaction resource pools are 2217650d1603SAlex Elder * sized to be a little larger than this maximum, to allow resource 2218650d1603SAlex Elder * allocations to always be contiguous. The number of entries in a 2219650d1603SAlex Elder * TRE ring buffer is a power of 2, and the extra resources in a pool 2220650d1603SAlex Elder * tends to nearly double the memory allocated for it. Reducing the 2221650d1603SAlex Elder * maximum number of outstanding TREs allows the number of entries in 2222650d1603SAlex Elder * a pool to avoid crossing that power-of-2 boundary, and this can 2223650d1603SAlex Elder * substantially reduce pool memory requirements. The number we 2224650d1603SAlex Elder * reduce it by matches the number added in gsi_trans_pool_init(). 2225650d1603SAlex Elder */ 2226650d1603SAlex Elder u32 gsi_channel_tre_max(struct gsi *gsi, u32 channel_id) 2227650d1603SAlex Elder { 2228650d1603SAlex Elder struct gsi_channel *channel = &gsi->channel[channel_id]; 2229650d1603SAlex Elder 2230650d1603SAlex Elder /* Hardware limit is channel->tre_count - 1 */ 2231650d1603SAlex Elder return channel->tre_count - (channel->tlv_count - 1); 2232650d1603SAlex Elder } 2233650d1603SAlex Elder 2234650d1603SAlex Elder /* Returns the maximum number of TREs in a single transaction for a channel */ 2235650d1603SAlex Elder u32 gsi_channel_trans_tre_max(struct gsi *gsi, u32 channel_id) 2236650d1603SAlex Elder { 2237650d1603SAlex Elder struct gsi_channel *channel = &gsi->channel[channel_id]; 2238650d1603SAlex Elder 2239650d1603SAlex Elder return channel->tlv_count; 2240650d1603SAlex Elder } 2241