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