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