1 // SPDX-License-Identifier: GPL-2.0 2 3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved. 4 * Copyright (C) 2019-2020 Linaro Ltd. 5 */ 6 7 #include <linux/types.h> 8 #include <linux/bits.h> 9 #include <linux/bitfield.h> 10 #include <linux/refcount.h> 11 #include <linux/scatterlist.h> 12 #include <linux/dma-direction.h> 13 14 #include "gsi.h" 15 #include "gsi_private.h" 16 #include "gsi_trans.h" 17 #include "ipa_gsi.h" 18 #include "ipa_data.h" 19 #include "ipa_cmd.h" 20 21 /** 22 * DOC: GSI Transactions 23 * 24 * A GSI transaction abstracts the behavior of a GSI channel by representing 25 * everything about a related group of IPA commands in a single structure. 26 * (A "command" in this sense is either a data transfer or an IPA immediate 27 * command.) Most details of interaction with the GSI hardware are managed 28 * by the GSI transaction core, allowing users to simply describe commands 29 * to be performed. When a transaction has completed a callback function 30 * (dependent on the type of endpoint associated with the channel) allows 31 * cleanup of resources associated with the transaction. 32 * 33 * To perform a command (or set of them), a user of the GSI transaction 34 * interface allocates a transaction, indicating the number of TREs required 35 * (one per command). If sufficient TREs are available, they are reserved 36 * for use in the transaction and the allocation succeeds. This way 37 * exhaustion of the available TREs in a channel ring is detected 38 * as early as possible. All resources required to complete a transaction 39 * are allocated at transaction allocation time. 40 * 41 * Commands performed as part of a transaction are represented in an array 42 * of Linux scatterlist structures. This array is allocated with the 43 * transaction, and its entries are initialized using standard scatterlist 44 * functions (such as sg_set_buf() or skb_to_sgvec()). 45 * 46 * Once a transaction's scatterlist structures have been initialized, the 47 * transaction is committed. The caller is responsible for mapping buffers 48 * for DMA if necessary, and this should be done *before* allocating 49 * the transaction. Between a successful allocation and commit of a 50 * transaction no errors should occur. 51 * 52 * Committing transfers ownership of the entire transaction to the GSI 53 * transaction core. The GSI transaction code formats the content of 54 * the scatterlist array into the channel ring buffer and informs the 55 * hardware that new TREs are available to process. 56 * 57 * The last TRE in each transaction is marked to interrupt the AP when the 58 * GSI hardware has completed it. Because transfers described by TREs are 59 * performed strictly in order, signaling the completion of just the last 60 * TRE in the transaction is sufficient to indicate the full transaction 61 * is complete. 62 * 63 * When a transaction is complete, ipa_gsi_trans_complete() is called by the 64 * GSI code into the IPA layer, allowing it to perform any final cleanup 65 * required before the transaction is freed. 66 */ 67 68 /* Hardware values representing a transfer element type */ 69 enum gsi_tre_type { 70 GSI_RE_XFER = 0x2, 71 GSI_RE_IMMD_CMD = 0x3, 72 }; 73 74 /* An entry in a channel ring */ 75 struct gsi_tre { 76 __le64 addr; /* DMA address */ 77 __le16 len_opcode; /* length in bytes or enum IPA_CMD_* */ 78 __le16 reserved; 79 __le32 flags; /* TRE_FLAGS_* */ 80 }; 81 82 /* gsi_tre->flags mask values (in CPU byte order) */ 83 #define TRE_FLAGS_CHAIN_FMASK GENMASK(0, 0) 84 #define TRE_FLAGS_IEOT_FMASK GENMASK(9, 9) 85 #define TRE_FLAGS_BEI_FMASK GENMASK(10, 10) 86 #define TRE_FLAGS_TYPE_FMASK GENMASK(23, 16) 87 88 int gsi_trans_pool_init(struct gsi_trans_pool *pool, size_t size, u32 count, 89 u32 max_alloc) 90 { 91 void *virt; 92 93 if (!size) 94 return -EINVAL; 95 if (count < max_alloc) 96 return -EINVAL; 97 if (!max_alloc) 98 return -EINVAL; 99 100 /* By allocating a few extra entries in our pool (one less 101 * than the maximum number that will be requested in a 102 * single allocation), we can always satisfy requests without 103 * ever worrying about straddling the end of the pool array. 104 * If there aren't enough entries starting at the free index, 105 * we just allocate free entries from the beginning of the pool. 106 */ 107 virt = kcalloc(count + max_alloc - 1, size, GFP_KERNEL); 108 if (!virt) 109 return -ENOMEM; 110 111 pool->base = virt; 112 /* If the allocator gave us any extra memory, use it */ 113 pool->count = ksize(pool->base) / size; 114 pool->free = 0; 115 pool->max_alloc = max_alloc; 116 pool->size = size; 117 pool->addr = 0; /* Only used for DMA pools */ 118 119 return 0; 120 } 121 122 void gsi_trans_pool_exit(struct gsi_trans_pool *pool) 123 { 124 kfree(pool->base); 125 memset(pool, 0, sizeof(*pool)); 126 } 127 128 /* Allocate the requested number of (zeroed) entries from the pool */ 129 /* Home-grown DMA pool. This way we can preallocate and use the tre_count 130 * to guarantee allocations will succeed. Even though we specify max_alloc 131 * (and it can be more than one), we only allow allocation of a single 132 * element from a DMA pool. 133 */ 134 int gsi_trans_pool_init_dma(struct device *dev, struct gsi_trans_pool *pool, 135 size_t size, u32 count, u32 max_alloc) 136 { 137 size_t total_size; 138 dma_addr_t addr; 139 void *virt; 140 141 if (!size) 142 return -EINVAL; 143 if (count < max_alloc) 144 return -EINVAL; 145 if (!max_alloc) 146 return -EINVAL; 147 148 /* Don't let allocations cross a power-of-two boundary */ 149 size = __roundup_pow_of_two(size); 150 total_size = (count + max_alloc - 1) * size; 151 152 /* The allocator will give us a power-of-2 number of pages 153 * sufficient to satisfy our request. Round up our requested 154 * size to avoid any unused space in the allocation. This way 155 * gsi_trans_pool_exit_dma() can assume the total allocated 156 * size is exactly (count * size). 157 */ 158 total_size = get_order(total_size) << PAGE_SHIFT; 159 160 virt = dma_alloc_coherent(dev, total_size, &addr, GFP_KERNEL); 161 if (!virt) 162 return -ENOMEM; 163 164 pool->base = virt; 165 pool->count = total_size / size; 166 pool->free = 0; 167 pool->size = size; 168 pool->max_alloc = max_alloc; 169 pool->addr = addr; 170 171 return 0; 172 } 173 174 void gsi_trans_pool_exit_dma(struct device *dev, struct gsi_trans_pool *pool) 175 { 176 size_t total_size = pool->count * pool->size; 177 178 dma_free_coherent(dev, total_size, pool->base, pool->addr); 179 memset(pool, 0, sizeof(*pool)); 180 } 181 182 /* Return the byte offset of the next free entry in the pool */ 183 static u32 gsi_trans_pool_alloc_common(struct gsi_trans_pool *pool, u32 count) 184 { 185 u32 offset; 186 187 WARN_ON(!count); 188 WARN_ON(count > pool->max_alloc); 189 190 /* Allocate from beginning if wrap would occur */ 191 if (count > pool->count - pool->free) 192 pool->free = 0; 193 194 offset = pool->free * pool->size; 195 pool->free += count; 196 memset(pool->base + offset, 0, count * pool->size); 197 198 return offset; 199 } 200 201 /* Allocate a contiguous block of zeroed entries from a pool */ 202 void *gsi_trans_pool_alloc(struct gsi_trans_pool *pool, u32 count) 203 { 204 return pool->base + gsi_trans_pool_alloc_common(pool, count); 205 } 206 207 /* Allocate a single zeroed entry from a DMA pool */ 208 void *gsi_trans_pool_alloc_dma(struct gsi_trans_pool *pool, dma_addr_t *addr) 209 { 210 u32 offset = gsi_trans_pool_alloc_common(pool, 1); 211 212 *addr = pool->addr + offset; 213 214 return pool->base + offset; 215 } 216 217 /* Map a TRE ring entry index to the transaction it is associated with */ 218 static void gsi_trans_map(struct gsi_trans *trans, u32 index) 219 { 220 struct gsi_channel *channel = &trans->gsi->channel[trans->channel_id]; 221 222 /* The completion event will indicate the last TRE used */ 223 index += trans->used_count - 1; 224 225 /* Note: index *must* be used modulo the ring count here */ 226 channel->trans_info.map[index % channel->tre_ring.count] = trans; 227 } 228 229 /* Return the transaction mapped to a given ring entry */ 230 struct gsi_trans * 231 gsi_channel_trans_mapped(struct gsi_channel *channel, u32 index) 232 { 233 /* Note: index *must* be used modulo the ring count here */ 234 return channel->trans_info.map[index % channel->tre_ring.count]; 235 } 236 237 /* Return the oldest completed transaction for a channel (or null) */ 238 struct gsi_trans *gsi_channel_trans_complete(struct gsi_channel *channel) 239 { 240 return list_first_entry_or_null(&channel->trans_info.complete, 241 struct gsi_trans, links); 242 } 243 244 /* Move a transaction from the allocated list to the committed list */ 245 static void gsi_trans_move_committed(struct gsi_trans *trans) 246 { 247 struct gsi_channel *channel = &trans->gsi->channel[trans->channel_id]; 248 struct gsi_trans_info *trans_info = &channel->trans_info; 249 u16 trans_index; 250 251 spin_lock_bh(&trans_info->spinlock); 252 253 list_move_tail(&trans->links, &trans_info->committed); 254 255 trans = list_first_entry_or_null(&trans_info->alloc, 256 struct gsi_trans, links); 257 258 spin_unlock_bh(&trans_info->spinlock); 259 260 /* This allocated transaction is now committed */ 261 trans_info->allocated_id++; 262 263 if (trans) { 264 trans_index = trans_info->allocated_id % channel->tre_count; 265 WARN_ON(trans != &trans_info->trans[trans_index]); 266 } else { 267 WARN_ON(trans_info->allocated_id != trans_info->free_id); 268 } 269 } 270 271 /* Move transactions from the committed list to the pending list */ 272 static void gsi_trans_move_pending(struct gsi_trans *trans) 273 { 274 struct gsi_channel *channel = &trans->gsi->channel[trans->channel_id]; 275 struct gsi_trans_info *trans_info = &channel->trans_info; 276 struct list_head list; 277 278 spin_lock_bh(&trans_info->spinlock); 279 280 /* Move this transaction and all predecessors to the pending list */ 281 list_cut_position(&list, &trans_info->committed, &trans->links); 282 list_splice_tail(&list, &trans_info->pending); 283 284 spin_unlock_bh(&trans_info->spinlock); 285 } 286 287 /* Move a transaction and all of its predecessors from the pending list 288 * to the completed list. 289 */ 290 void gsi_trans_move_complete(struct gsi_trans *trans) 291 { 292 struct gsi_channel *channel = &trans->gsi->channel[trans->channel_id]; 293 struct gsi_trans_info *trans_info = &channel->trans_info; 294 struct list_head list; 295 296 spin_lock_bh(&trans_info->spinlock); 297 298 /* Move this transaction and all predecessors to completed list */ 299 list_cut_position(&list, &trans_info->pending, &trans->links); 300 list_splice_tail(&list, &trans_info->complete); 301 302 spin_unlock_bh(&trans_info->spinlock); 303 } 304 305 /* Move a transaction from the completed list to the polled list */ 306 void gsi_trans_move_polled(struct gsi_trans *trans) 307 { 308 struct gsi_channel *channel = &trans->gsi->channel[trans->channel_id]; 309 struct gsi_trans_info *trans_info = &channel->trans_info; 310 311 spin_lock_bh(&trans_info->spinlock); 312 313 list_move_tail(&trans->links, &trans_info->polled); 314 315 spin_unlock_bh(&trans_info->spinlock); 316 } 317 318 /* Reserve some number of TREs on a channel. Returns true if successful */ 319 static bool 320 gsi_trans_tre_reserve(struct gsi_trans_info *trans_info, u32 tre_count) 321 { 322 int avail = atomic_read(&trans_info->tre_avail); 323 int new; 324 325 do { 326 new = avail - (int)tre_count; 327 if (unlikely(new < 0)) 328 return false; 329 } while (!atomic_try_cmpxchg(&trans_info->tre_avail, &avail, new)); 330 331 return true; 332 } 333 334 /* Release previously-reserved TRE entries to a channel */ 335 static void 336 gsi_trans_tre_release(struct gsi_trans_info *trans_info, u32 tre_count) 337 { 338 atomic_add(tre_count, &trans_info->tre_avail); 339 } 340 341 /* Return true if no transactions are allocated, false otherwise */ 342 bool gsi_channel_trans_idle(struct gsi *gsi, u32 channel_id) 343 { 344 u32 tre_max = gsi_channel_tre_max(gsi, channel_id); 345 struct gsi_trans_info *trans_info; 346 347 trans_info = &gsi->channel[channel_id].trans_info; 348 349 return atomic_read(&trans_info->tre_avail) == tre_max; 350 } 351 352 /* Allocate a GSI transaction on a channel */ 353 struct gsi_trans *gsi_channel_trans_alloc(struct gsi *gsi, u32 channel_id, 354 u32 tre_count, 355 enum dma_data_direction direction) 356 { 357 struct gsi_channel *channel = &gsi->channel[channel_id]; 358 struct gsi_trans_info *trans_info; 359 struct gsi_trans *trans; 360 u16 trans_index; 361 362 if (WARN_ON(tre_count > channel->trans_tre_max)) 363 return NULL; 364 365 trans_info = &channel->trans_info; 366 367 /* If we can't reserve the TREs for the transaction, we're done */ 368 if (!gsi_trans_tre_reserve(trans_info, tre_count)) 369 return NULL; 370 371 trans_index = trans_info->free_id % channel->tre_count; 372 trans = &trans_info->trans[trans_index]; 373 memset(trans, 0, sizeof(*trans)); 374 375 /* Initialize non-zero fields in the transaction */ 376 trans->gsi = gsi; 377 trans->channel_id = channel_id; 378 trans->rsvd_count = tre_count; 379 init_completion(&trans->completion); 380 381 /* Allocate the scatterlist */ 382 trans->sgl = gsi_trans_pool_alloc(&trans_info->sg_pool, tre_count); 383 sg_init_marker(trans->sgl, tre_count); 384 385 trans->direction = direction; 386 refcount_set(&trans->refcount, 1); 387 388 /* This free transaction will now be allocated */ 389 trans_info->free_id++; 390 391 spin_lock_bh(&trans_info->spinlock); 392 393 list_add_tail(&trans->links, &trans_info->alloc); 394 395 trans = list_first_entry(&trans_info->alloc, struct gsi_trans, links); 396 397 spin_unlock_bh(&trans_info->spinlock); 398 399 WARN_ON(trans_info->allocated_id == trans_info->free_id); 400 trans_index = trans_info->allocated_id % channel->tre_count; 401 WARN_ON(trans != &trans_info->trans[trans_index]); 402 403 return trans; 404 } 405 406 /* Free a previously-allocated transaction */ 407 void gsi_trans_free(struct gsi_trans *trans) 408 { 409 refcount_t *refcount = &trans->refcount; 410 struct gsi_trans_info *trans_info; 411 bool last; 412 413 /* We must hold the lock to release the last reference */ 414 if (refcount_dec_not_one(refcount)) 415 return; 416 417 trans_info = &trans->gsi->channel[trans->channel_id].trans_info; 418 419 spin_lock_bh(&trans_info->spinlock); 420 421 /* Reference might have been added before we got the lock */ 422 last = refcount_dec_and_test(refcount); 423 if (last) 424 list_del(&trans->links); 425 426 spin_unlock_bh(&trans_info->spinlock); 427 428 if (!last) 429 return; 430 431 /* Unused transactions are allocated but never committed */ 432 if (!trans->used_count) 433 trans_info->allocated_id++; 434 else 435 ipa_gsi_trans_release(trans); 436 437 /* Releasing the reserved TREs implicitly frees the sgl[] and 438 * (if present) info[] arrays, plus the transaction itself. 439 */ 440 gsi_trans_tre_release(trans_info, trans->rsvd_count); 441 } 442 443 /* Add an immediate command to a transaction */ 444 void gsi_trans_cmd_add(struct gsi_trans *trans, void *buf, u32 size, 445 dma_addr_t addr, enum ipa_cmd_opcode opcode) 446 { 447 u32 which = trans->used_count++; 448 struct scatterlist *sg; 449 450 WARN_ON(which >= trans->rsvd_count); 451 452 /* Commands are quite different from data transfer requests. 453 * Their payloads come from a pool whose memory is allocated 454 * using dma_alloc_coherent(). We therefore do *not* map them 455 * for DMA (unlike what we do for pages and skbs). 456 * 457 * When a transaction completes, the SGL is normally unmapped. 458 * A command transaction has direction DMA_NONE, which tells 459 * gsi_trans_complete() to skip the unmapping step. 460 * 461 * The only things we use directly in a command scatter/gather 462 * entry are the DMA address and length. We still need the SG 463 * table flags to be maintained though, so assign a NULL page 464 * pointer for that purpose. 465 */ 466 sg = &trans->sgl[which]; 467 sg_assign_page(sg, NULL); 468 sg_dma_address(sg) = addr; 469 sg_dma_len(sg) = size; 470 471 trans->cmd_opcode[which] = opcode; 472 } 473 474 /* Add a page transfer to a transaction. It will fill the only TRE. */ 475 int gsi_trans_page_add(struct gsi_trans *trans, struct page *page, u32 size, 476 u32 offset) 477 { 478 struct scatterlist *sg = &trans->sgl[0]; 479 int ret; 480 481 if (WARN_ON(trans->rsvd_count != 1)) 482 return -EINVAL; 483 if (WARN_ON(trans->used_count)) 484 return -EINVAL; 485 486 sg_set_page(sg, page, size, offset); 487 ret = dma_map_sg(trans->gsi->dev, sg, 1, trans->direction); 488 if (!ret) 489 return -ENOMEM; 490 491 trans->used_count++; /* Transaction now owns the (DMA mapped) page */ 492 493 return 0; 494 } 495 496 /* Add an SKB transfer to a transaction. No other TREs will be used. */ 497 int gsi_trans_skb_add(struct gsi_trans *trans, struct sk_buff *skb) 498 { 499 struct scatterlist *sg = &trans->sgl[0]; 500 u32 used_count; 501 int ret; 502 503 if (WARN_ON(trans->rsvd_count != 1)) 504 return -EINVAL; 505 if (WARN_ON(trans->used_count)) 506 return -EINVAL; 507 508 /* skb->len will not be 0 (checked early) */ 509 ret = skb_to_sgvec(skb, sg, 0, skb->len); 510 if (ret < 0) 511 return ret; 512 used_count = ret; 513 514 ret = dma_map_sg(trans->gsi->dev, sg, used_count, trans->direction); 515 if (!ret) 516 return -ENOMEM; 517 518 /* Transaction now owns the (DMA mapped) skb */ 519 trans->used_count += used_count; 520 521 return 0; 522 } 523 524 /* Compute the length/opcode value to use for a TRE */ 525 static __le16 gsi_tre_len_opcode(enum ipa_cmd_opcode opcode, u32 len) 526 { 527 return opcode == IPA_CMD_NONE ? cpu_to_le16((u16)len) 528 : cpu_to_le16((u16)opcode); 529 } 530 531 /* Compute the flags value to use for a given TRE */ 532 static __le32 gsi_tre_flags(bool last_tre, bool bei, enum ipa_cmd_opcode opcode) 533 { 534 enum gsi_tre_type tre_type; 535 u32 tre_flags; 536 537 tre_type = opcode == IPA_CMD_NONE ? GSI_RE_XFER : GSI_RE_IMMD_CMD; 538 tre_flags = u32_encode_bits(tre_type, TRE_FLAGS_TYPE_FMASK); 539 540 /* Last TRE contains interrupt flags */ 541 if (last_tre) { 542 /* All transactions end in a transfer completion interrupt */ 543 tre_flags |= TRE_FLAGS_IEOT_FMASK; 544 /* Don't interrupt when outbound commands are acknowledged */ 545 if (bei) 546 tre_flags |= TRE_FLAGS_BEI_FMASK; 547 } else { /* All others indicate there's more to come */ 548 tre_flags |= TRE_FLAGS_CHAIN_FMASK; 549 } 550 551 return cpu_to_le32(tre_flags); 552 } 553 554 static void gsi_trans_tre_fill(struct gsi_tre *dest_tre, dma_addr_t addr, 555 u32 len, bool last_tre, bool bei, 556 enum ipa_cmd_opcode opcode) 557 { 558 struct gsi_tre tre; 559 560 tre.addr = cpu_to_le64(addr); 561 tre.len_opcode = gsi_tre_len_opcode(opcode, len); 562 tre.reserved = 0; 563 tre.flags = gsi_tre_flags(last_tre, bei, opcode); 564 565 /* ARM64 can write 16 bytes as a unit with a single instruction. 566 * Doing the assignment this way is an attempt to make that happen. 567 */ 568 *dest_tre = tre; 569 } 570 571 /** 572 * __gsi_trans_commit() - Common GSI transaction commit code 573 * @trans: Transaction to commit 574 * @ring_db: Whether to tell the hardware about these queued transfers 575 * 576 * Formats channel ring TRE entries based on the content of the scatterlist. 577 * Maps a transaction pointer to the last ring entry used for the transaction, 578 * so it can be recovered when it completes. Moves the transaction to the 579 * pending list. Finally, updates the channel ring pointer and optionally 580 * rings the doorbell. 581 */ 582 static void __gsi_trans_commit(struct gsi_trans *trans, bool ring_db) 583 { 584 struct gsi_channel *channel = &trans->gsi->channel[trans->channel_id]; 585 struct gsi_ring *tre_ring = &channel->tre_ring; 586 enum ipa_cmd_opcode opcode = IPA_CMD_NONE; 587 bool bei = channel->toward_ipa; 588 struct gsi_tre *dest_tre; 589 struct scatterlist *sg; 590 u32 byte_count = 0; 591 u8 *cmd_opcode; 592 u32 avail; 593 u32 i; 594 595 WARN_ON(!trans->used_count); 596 597 /* Consume the entries. If we cross the end of the ring while 598 * filling them we'll switch to the beginning to finish. 599 * If there is no info array we're doing a simple data 600 * transfer request, whose opcode is IPA_CMD_NONE. 601 */ 602 cmd_opcode = channel->command ? &trans->cmd_opcode[0] : NULL; 603 avail = tre_ring->count - tre_ring->index % tre_ring->count; 604 dest_tre = gsi_ring_virt(tre_ring, tre_ring->index); 605 for_each_sg(trans->sgl, sg, trans->used_count, i) { 606 bool last_tre = i == trans->used_count - 1; 607 dma_addr_t addr = sg_dma_address(sg); 608 u32 len = sg_dma_len(sg); 609 610 byte_count += len; 611 if (!avail--) 612 dest_tre = gsi_ring_virt(tre_ring, 0); 613 if (cmd_opcode) 614 opcode = *cmd_opcode++; 615 616 gsi_trans_tre_fill(dest_tre, addr, len, last_tre, bei, opcode); 617 dest_tre++; 618 } 619 /* Associate the TRE with the transaction */ 620 gsi_trans_map(trans, tre_ring->index); 621 622 tre_ring->index += trans->used_count; 623 624 trans->len = byte_count; 625 if (channel->toward_ipa) 626 gsi_trans_tx_committed(trans); 627 628 gsi_trans_move_committed(trans); 629 630 /* Ring doorbell if requested, or if all TREs are allocated */ 631 if (ring_db || !atomic_read(&channel->trans_info.tre_avail)) { 632 /* Report what we're handing off to hardware for TX channels */ 633 if (channel->toward_ipa) 634 gsi_trans_tx_queued(trans); 635 gsi_trans_move_pending(trans); 636 gsi_channel_doorbell(channel); 637 } 638 } 639 640 /* Commit a GSI transaction */ 641 void gsi_trans_commit(struct gsi_trans *trans, bool ring_db) 642 { 643 if (trans->used_count) 644 __gsi_trans_commit(trans, ring_db); 645 else 646 gsi_trans_free(trans); 647 } 648 649 /* Commit a GSI transaction and wait for it to complete */ 650 void gsi_trans_commit_wait(struct gsi_trans *trans) 651 { 652 if (!trans->used_count) 653 goto out_trans_free; 654 655 refcount_inc(&trans->refcount); 656 657 __gsi_trans_commit(trans, true); 658 659 wait_for_completion(&trans->completion); 660 661 out_trans_free: 662 gsi_trans_free(trans); 663 } 664 665 /* Process the completion of a transaction; called while polling */ 666 void gsi_trans_complete(struct gsi_trans *trans) 667 { 668 /* If the entire SGL was mapped when added, unmap it now */ 669 if (trans->direction != DMA_NONE) 670 dma_unmap_sg(trans->gsi->dev, trans->sgl, trans->used_count, 671 trans->direction); 672 673 ipa_gsi_trans_complete(trans); 674 675 complete(&trans->completion); 676 677 gsi_trans_free(trans); 678 } 679 680 /* Cancel a channel's pending transactions */ 681 void gsi_channel_trans_cancel_pending(struct gsi_channel *channel) 682 { 683 struct gsi_trans_info *trans_info = &channel->trans_info; 684 struct gsi_trans *trans; 685 bool cancelled; 686 687 /* channel->gsi->mutex is held by caller */ 688 spin_lock_bh(&trans_info->spinlock); 689 690 cancelled = !list_empty(&trans_info->pending); 691 list_for_each_entry(trans, &trans_info->pending, links) 692 trans->cancelled = true; 693 694 list_splice_tail_init(&trans_info->pending, &trans_info->complete); 695 696 spin_unlock_bh(&trans_info->spinlock); 697 698 /* Schedule NAPI polling to complete the cancelled transactions */ 699 if (cancelled) 700 napi_schedule(&channel->napi); 701 } 702 703 /* Issue a command to read a single byte from a channel */ 704 int gsi_trans_read_byte(struct gsi *gsi, u32 channel_id, dma_addr_t addr) 705 { 706 struct gsi_channel *channel = &gsi->channel[channel_id]; 707 struct gsi_ring *tre_ring = &channel->tre_ring; 708 struct gsi_trans_info *trans_info; 709 struct gsi_tre *dest_tre; 710 711 trans_info = &channel->trans_info; 712 713 /* First reserve the TRE, if possible */ 714 if (!gsi_trans_tre_reserve(trans_info, 1)) 715 return -EBUSY; 716 717 /* Now fill the reserved TRE and tell the hardware */ 718 719 dest_tre = gsi_ring_virt(tre_ring, tre_ring->index); 720 gsi_trans_tre_fill(dest_tre, addr, 1, true, false, IPA_CMD_NONE); 721 722 tre_ring->index++; 723 gsi_channel_doorbell(channel); 724 725 return 0; 726 } 727 728 /* Mark a gsi_trans_read_byte() request done */ 729 void gsi_trans_read_byte_done(struct gsi *gsi, u32 channel_id) 730 { 731 struct gsi_channel *channel = &gsi->channel[channel_id]; 732 733 gsi_trans_tre_release(&channel->trans_info, 1); 734 } 735 736 /* Initialize a channel's GSI transaction info */ 737 int gsi_channel_trans_init(struct gsi *gsi, u32 channel_id) 738 { 739 struct gsi_channel *channel = &gsi->channel[channel_id]; 740 u32 tre_count = channel->tre_count; 741 struct gsi_trans_info *trans_info; 742 u32 tre_max; 743 int ret; 744 745 /* Ensure the size of a channel element is what's expected */ 746 BUILD_BUG_ON(sizeof(struct gsi_tre) != GSI_RING_ELEMENT_SIZE); 747 748 trans_info = &channel->trans_info; 749 750 /* The tre_avail field is what ultimately limits the number of 751 * outstanding transactions and their resources. A transaction 752 * allocation succeeds only if the TREs available are sufficient 753 * for what the transaction might need. 754 */ 755 tre_max = gsi_channel_tre_max(channel->gsi, channel_id); 756 atomic_set(&trans_info->tre_avail, tre_max); 757 758 /* We can't use more TREs than the number available in the ring. 759 * This limits the number of transactions that can be outstanding. 760 * Worst case is one TRE per transaction (but we actually limit 761 * it to something a little less than that). By allocating a 762 * power-of-two number of transactions we can use an index 763 * modulo that number to determine the next one that's free. 764 * Transactions are allocated one at a time. 765 */ 766 trans_info->trans = kcalloc(tre_count, sizeof(*trans_info->trans), 767 GFP_KERNEL); 768 if (!trans_info->trans) 769 return -ENOMEM; 770 trans_info->free_id = 0; /* all modulo channel->tre_count */ 771 trans_info->allocated_id = 0; 772 773 /* A completion event contains a pointer to the TRE that caused 774 * the event (which will be the last one used by the transaction). 775 * Each entry in this map records the transaction associated 776 * with a corresponding completed TRE. 777 */ 778 trans_info->map = kcalloc(tre_count, sizeof(*trans_info->map), 779 GFP_KERNEL); 780 if (!trans_info->map) { 781 ret = -ENOMEM; 782 goto err_trans_free; 783 } 784 785 /* A transaction uses a scatterlist array to represent the data 786 * transfers implemented by the transaction. Each scatterlist 787 * element is used to fill a single TRE when the transaction is 788 * committed. So we need as many scatterlist elements as the 789 * maximum number of TREs that can be outstanding. 790 */ 791 ret = gsi_trans_pool_init(&trans_info->sg_pool, 792 sizeof(struct scatterlist), 793 tre_max, channel->trans_tre_max); 794 if (ret) 795 goto err_map_free; 796 797 spin_lock_init(&trans_info->spinlock); 798 INIT_LIST_HEAD(&trans_info->alloc); 799 INIT_LIST_HEAD(&trans_info->committed); 800 INIT_LIST_HEAD(&trans_info->pending); 801 INIT_LIST_HEAD(&trans_info->complete); 802 INIT_LIST_HEAD(&trans_info->polled); 803 804 return 0; 805 806 err_map_free: 807 kfree(trans_info->map); 808 err_trans_free: 809 kfree(trans_info->trans); 810 811 dev_err(gsi->dev, "error %d initializing channel %u transactions\n", 812 ret, channel_id); 813 814 return ret; 815 } 816 817 /* Inverse of gsi_channel_trans_init() */ 818 void gsi_channel_trans_exit(struct gsi_channel *channel) 819 { 820 struct gsi_trans_info *trans_info = &channel->trans_info; 821 822 gsi_trans_pool_exit(&trans_info->sg_pool); 823 kfree(trans_info->trans); 824 kfree(trans_info->map); 825 } 826