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 250 spin_lock_bh(&trans_info->spinlock); 251 252 list_move_tail(&trans->links, &trans_info->committed); 253 254 spin_unlock_bh(&trans_info->spinlock); 255 256 /* This allocated transaction is now committed */ 257 trans_info->allocated_id++; 258 } 259 260 /* Move transactions from the committed list to the pending list */ 261 static void gsi_trans_move_pending(struct gsi_trans *trans) 262 { 263 struct gsi_channel *channel = &trans->gsi->channel[trans->channel_id]; 264 struct gsi_trans_info *trans_info = &channel->trans_info; 265 u16 trans_index = trans - &trans_info->trans[0]; 266 struct list_head list; 267 u16 delta; 268 269 spin_lock_bh(&trans_info->spinlock); 270 271 /* Move this transaction and all predecessors to the pending list */ 272 list_cut_position(&list, &trans_info->committed, &trans->links); 273 list_splice_tail(&list, &trans_info->pending); 274 275 spin_unlock_bh(&trans_info->spinlock); 276 277 /* These committed transactions are now pending */ 278 delta = trans_index - trans_info->committed_id + 1; 279 trans_info->committed_id += delta % channel->tre_count; 280 } 281 282 /* Move a transaction and all of its predecessors from the pending list 283 * to the completed list. 284 */ 285 void gsi_trans_move_complete(struct gsi_trans *trans) 286 { 287 struct gsi_channel *channel = &trans->gsi->channel[trans->channel_id]; 288 struct gsi_trans_info *trans_info = &channel->trans_info; 289 u16 trans_index = trans - trans_info->trans; 290 struct list_head list; 291 u16 delta; 292 293 spin_lock_bh(&trans_info->spinlock); 294 295 /* Move this transaction and all predecessors to completed list */ 296 list_cut_position(&list, &trans_info->pending, &trans->links); 297 list_splice_tail(&list, &trans_info->complete); 298 299 trans = list_first_entry(&trans_info->complete, 300 struct gsi_trans, links); 301 302 spin_unlock_bh(&trans_info->spinlock); 303 304 /* These pending transactions are now completed */ 305 delta = trans_index - trans_info->pending_id + 1; 306 delta %= channel->tre_count; 307 trans_info->pending_id += delta; 308 309 WARN_ON(trans_info->completed_id == trans_info->pending_id); 310 trans_index = trans_info->completed_id % channel->tre_count; 311 WARN_ON(trans != &trans_info->trans[trans_index]); 312 } 313 314 /* Move a transaction from the completed list to the polled list */ 315 void gsi_trans_move_polled(struct gsi_trans *trans) 316 { 317 struct gsi_channel *channel = &trans->gsi->channel[trans->channel_id]; 318 struct gsi_trans_info *trans_info = &channel->trans_info; 319 u16 trans_index; 320 321 spin_lock_bh(&trans_info->spinlock); 322 323 list_move_tail(&trans->links, &trans_info->polled); 324 325 trans = list_first_entry_or_null(&trans_info->complete, 326 struct gsi_trans, links); 327 328 spin_unlock_bh(&trans_info->spinlock); 329 330 /* This completed transaction is now polled */ 331 trans_info->completed_id++; 332 333 if (trans) { 334 trans_index = trans_info->completed_id % channel->tre_count; 335 WARN_ON(trans != &trans_info->trans[trans_index]); 336 } else { 337 WARN_ON(trans_info->completed_id != 338 trans_info->pending_id); 339 } 340 } 341 342 /* Reserve some number of TREs on a channel. Returns true if successful */ 343 static bool 344 gsi_trans_tre_reserve(struct gsi_trans_info *trans_info, u32 tre_count) 345 { 346 int avail = atomic_read(&trans_info->tre_avail); 347 int new; 348 349 do { 350 new = avail - (int)tre_count; 351 if (unlikely(new < 0)) 352 return false; 353 } while (!atomic_try_cmpxchg(&trans_info->tre_avail, &avail, new)); 354 355 return true; 356 } 357 358 /* Release previously-reserved TRE entries to a channel */ 359 static void 360 gsi_trans_tre_release(struct gsi_trans_info *trans_info, u32 tre_count) 361 { 362 atomic_add(tre_count, &trans_info->tre_avail); 363 } 364 365 /* Return true if no transactions are allocated, false otherwise */ 366 bool gsi_channel_trans_idle(struct gsi *gsi, u32 channel_id) 367 { 368 u32 tre_max = gsi_channel_tre_max(gsi, channel_id); 369 struct gsi_trans_info *trans_info; 370 371 trans_info = &gsi->channel[channel_id].trans_info; 372 373 return atomic_read(&trans_info->tre_avail) == tre_max; 374 } 375 376 /* Allocate a GSI transaction on a channel */ 377 struct gsi_trans *gsi_channel_trans_alloc(struct gsi *gsi, u32 channel_id, 378 u32 tre_count, 379 enum dma_data_direction direction) 380 { 381 struct gsi_channel *channel = &gsi->channel[channel_id]; 382 struct gsi_trans_info *trans_info; 383 struct gsi_trans *trans; 384 u16 trans_index; 385 386 if (WARN_ON(tre_count > channel->trans_tre_max)) 387 return NULL; 388 389 trans_info = &channel->trans_info; 390 391 /* If we can't reserve the TREs for the transaction, we're done */ 392 if (!gsi_trans_tre_reserve(trans_info, tre_count)) 393 return NULL; 394 395 trans_index = trans_info->free_id % channel->tre_count; 396 trans = &trans_info->trans[trans_index]; 397 memset(trans, 0, sizeof(*trans)); 398 399 /* Initialize non-zero fields in the transaction */ 400 trans->gsi = gsi; 401 trans->channel_id = channel_id; 402 trans->rsvd_count = tre_count; 403 init_completion(&trans->completion); 404 405 /* Allocate the scatterlist */ 406 trans->sgl = gsi_trans_pool_alloc(&trans_info->sg_pool, tre_count); 407 sg_init_marker(trans->sgl, tre_count); 408 409 trans->direction = direction; 410 refcount_set(&trans->refcount, 1); 411 412 /* This free transaction will now be allocated */ 413 trans_info->free_id++; 414 415 spin_lock_bh(&trans_info->spinlock); 416 417 list_add_tail(&trans->links, &trans_info->alloc); 418 419 spin_unlock_bh(&trans_info->spinlock); 420 421 return trans; 422 } 423 424 /* Free a previously-allocated transaction */ 425 void gsi_trans_free(struct gsi_trans *trans) 426 { 427 refcount_t *refcount = &trans->refcount; 428 struct gsi_trans_info *trans_info; 429 bool last; 430 431 /* We must hold the lock to release the last reference */ 432 if (refcount_dec_not_one(refcount)) 433 return; 434 435 trans_info = &trans->gsi->channel[trans->channel_id].trans_info; 436 437 spin_lock_bh(&trans_info->spinlock); 438 439 /* Reference might have been added before we got the lock */ 440 last = refcount_dec_and_test(refcount); 441 if (last) 442 list_del(&trans->links); 443 444 spin_unlock_bh(&trans_info->spinlock); 445 446 if (!last) 447 return; 448 449 /* Unused transactions are allocated but never committed, pending, 450 * completed, or polled. 451 */ 452 if (!trans->used_count) { 453 trans_info->allocated_id++; 454 trans_info->committed_id++; 455 trans_info->pending_id++; 456 trans_info->completed_id++; 457 } else { 458 ipa_gsi_trans_release(trans); 459 } 460 461 /* Releasing the reserved TREs implicitly frees the sgl[] and 462 * (if present) info[] arrays, plus the transaction itself. 463 */ 464 gsi_trans_tre_release(trans_info, trans->rsvd_count); 465 } 466 467 /* Add an immediate command to a transaction */ 468 void gsi_trans_cmd_add(struct gsi_trans *trans, void *buf, u32 size, 469 dma_addr_t addr, enum ipa_cmd_opcode opcode) 470 { 471 u32 which = trans->used_count++; 472 struct scatterlist *sg; 473 474 WARN_ON(which >= trans->rsvd_count); 475 476 /* Commands are quite different from data transfer requests. 477 * Their payloads come from a pool whose memory is allocated 478 * using dma_alloc_coherent(). We therefore do *not* map them 479 * for DMA (unlike what we do for pages and skbs). 480 * 481 * When a transaction completes, the SGL is normally unmapped. 482 * A command transaction has direction DMA_NONE, which tells 483 * gsi_trans_complete() to skip the unmapping step. 484 * 485 * The only things we use directly in a command scatter/gather 486 * entry are the DMA address and length. We still need the SG 487 * table flags to be maintained though, so assign a NULL page 488 * pointer for that purpose. 489 */ 490 sg = &trans->sgl[which]; 491 sg_assign_page(sg, NULL); 492 sg_dma_address(sg) = addr; 493 sg_dma_len(sg) = size; 494 495 trans->cmd_opcode[which] = opcode; 496 } 497 498 /* Add a page transfer to a transaction. It will fill the only TRE. */ 499 int gsi_trans_page_add(struct gsi_trans *trans, struct page *page, u32 size, 500 u32 offset) 501 { 502 struct scatterlist *sg = &trans->sgl[0]; 503 int ret; 504 505 if (WARN_ON(trans->rsvd_count != 1)) 506 return -EINVAL; 507 if (WARN_ON(trans->used_count)) 508 return -EINVAL; 509 510 sg_set_page(sg, page, size, offset); 511 ret = dma_map_sg(trans->gsi->dev, sg, 1, trans->direction); 512 if (!ret) 513 return -ENOMEM; 514 515 trans->used_count++; /* Transaction now owns the (DMA mapped) page */ 516 517 return 0; 518 } 519 520 /* Add an SKB transfer to a transaction. No other TREs will be used. */ 521 int gsi_trans_skb_add(struct gsi_trans *trans, struct sk_buff *skb) 522 { 523 struct scatterlist *sg = &trans->sgl[0]; 524 u32 used_count; 525 int ret; 526 527 if (WARN_ON(trans->rsvd_count != 1)) 528 return -EINVAL; 529 if (WARN_ON(trans->used_count)) 530 return -EINVAL; 531 532 /* skb->len will not be 0 (checked early) */ 533 ret = skb_to_sgvec(skb, sg, 0, skb->len); 534 if (ret < 0) 535 return ret; 536 used_count = ret; 537 538 ret = dma_map_sg(trans->gsi->dev, sg, used_count, trans->direction); 539 if (!ret) 540 return -ENOMEM; 541 542 /* Transaction now owns the (DMA mapped) skb */ 543 trans->used_count += used_count; 544 545 return 0; 546 } 547 548 /* Compute the length/opcode value to use for a TRE */ 549 static __le16 gsi_tre_len_opcode(enum ipa_cmd_opcode opcode, u32 len) 550 { 551 return opcode == IPA_CMD_NONE ? cpu_to_le16((u16)len) 552 : cpu_to_le16((u16)opcode); 553 } 554 555 /* Compute the flags value to use for a given TRE */ 556 static __le32 gsi_tre_flags(bool last_tre, bool bei, enum ipa_cmd_opcode opcode) 557 { 558 enum gsi_tre_type tre_type; 559 u32 tre_flags; 560 561 tre_type = opcode == IPA_CMD_NONE ? GSI_RE_XFER : GSI_RE_IMMD_CMD; 562 tre_flags = u32_encode_bits(tre_type, TRE_FLAGS_TYPE_FMASK); 563 564 /* Last TRE contains interrupt flags */ 565 if (last_tre) { 566 /* All transactions end in a transfer completion interrupt */ 567 tre_flags |= TRE_FLAGS_IEOT_FMASK; 568 /* Don't interrupt when outbound commands are acknowledged */ 569 if (bei) 570 tre_flags |= TRE_FLAGS_BEI_FMASK; 571 } else { /* All others indicate there's more to come */ 572 tre_flags |= TRE_FLAGS_CHAIN_FMASK; 573 } 574 575 return cpu_to_le32(tre_flags); 576 } 577 578 static void gsi_trans_tre_fill(struct gsi_tre *dest_tre, dma_addr_t addr, 579 u32 len, bool last_tre, bool bei, 580 enum ipa_cmd_opcode opcode) 581 { 582 struct gsi_tre tre; 583 584 tre.addr = cpu_to_le64(addr); 585 tre.len_opcode = gsi_tre_len_opcode(opcode, len); 586 tre.reserved = 0; 587 tre.flags = gsi_tre_flags(last_tre, bei, opcode); 588 589 /* ARM64 can write 16 bytes as a unit with a single instruction. 590 * Doing the assignment this way is an attempt to make that happen. 591 */ 592 *dest_tre = tre; 593 } 594 595 /** 596 * __gsi_trans_commit() - Common GSI transaction commit code 597 * @trans: Transaction to commit 598 * @ring_db: Whether to tell the hardware about these queued transfers 599 * 600 * Formats channel ring TRE entries based on the content of the scatterlist. 601 * Maps a transaction pointer to the last ring entry used for the transaction, 602 * so it can be recovered when it completes. Moves the transaction to the 603 * pending list. Finally, updates the channel ring pointer and optionally 604 * rings the doorbell. 605 */ 606 static void __gsi_trans_commit(struct gsi_trans *trans, bool ring_db) 607 { 608 struct gsi_channel *channel = &trans->gsi->channel[trans->channel_id]; 609 struct gsi_ring *tre_ring = &channel->tre_ring; 610 enum ipa_cmd_opcode opcode = IPA_CMD_NONE; 611 bool bei = channel->toward_ipa; 612 struct gsi_tre *dest_tre; 613 struct scatterlist *sg; 614 u32 byte_count = 0; 615 u8 *cmd_opcode; 616 u32 avail; 617 u32 i; 618 619 WARN_ON(!trans->used_count); 620 621 /* Consume the entries. If we cross the end of the ring while 622 * filling them we'll switch to the beginning to finish. 623 * If there is no info array we're doing a simple data 624 * transfer request, whose opcode is IPA_CMD_NONE. 625 */ 626 cmd_opcode = channel->command ? &trans->cmd_opcode[0] : NULL; 627 avail = tre_ring->count - tre_ring->index % tre_ring->count; 628 dest_tre = gsi_ring_virt(tre_ring, tre_ring->index); 629 for_each_sg(trans->sgl, sg, trans->used_count, i) { 630 bool last_tre = i == trans->used_count - 1; 631 dma_addr_t addr = sg_dma_address(sg); 632 u32 len = sg_dma_len(sg); 633 634 byte_count += len; 635 if (!avail--) 636 dest_tre = gsi_ring_virt(tre_ring, 0); 637 if (cmd_opcode) 638 opcode = *cmd_opcode++; 639 640 gsi_trans_tre_fill(dest_tre, addr, len, last_tre, bei, opcode); 641 dest_tre++; 642 } 643 /* Associate the TRE with the transaction */ 644 gsi_trans_map(trans, tre_ring->index); 645 646 tre_ring->index += trans->used_count; 647 648 trans->len = byte_count; 649 if (channel->toward_ipa) 650 gsi_trans_tx_committed(trans); 651 652 gsi_trans_move_committed(trans); 653 654 /* Ring doorbell if requested, or if all TREs are allocated */ 655 if (ring_db || !atomic_read(&channel->trans_info.tre_avail)) { 656 /* Report what we're handing off to hardware for TX channels */ 657 if (channel->toward_ipa) 658 gsi_trans_tx_queued(trans); 659 gsi_trans_move_pending(trans); 660 gsi_channel_doorbell(channel); 661 } 662 } 663 664 /* Commit a GSI transaction */ 665 void gsi_trans_commit(struct gsi_trans *trans, bool ring_db) 666 { 667 if (trans->used_count) 668 __gsi_trans_commit(trans, ring_db); 669 else 670 gsi_trans_free(trans); 671 } 672 673 /* Commit a GSI transaction and wait for it to complete */ 674 void gsi_trans_commit_wait(struct gsi_trans *trans) 675 { 676 if (!trans->used_count) 677 goto out_trans_free; 678 679 refcount_inc(&trans->refcount); 680 681 __gsi_trans_commit(trans, true); 682 683 wait_for_completion(&trans->completion); 684 685 out_trans_free: 686 gsi_trans_free(trans); 687 } 688 689 /* Process the completion of a transaction; called while polling */ 690 void gsi_trans_complete(struct gsi_trans *trans) 691 { 692 /* If the entire SGL was mapped when added, unmap it now */ 693 if (trans->direction != DMA_NONE) 694 dma_unmap_sg(trans->gsi->dev, trans->sgl, trans->used_count, 695 trans->direction); 696 697 ipa_gsi_trans_complete(trans); 698 699 complete(&trans->completion); 700 701 gsi_trans_free(trans); 702 } 703 704 /* Cancel a channel's pending transactions */ 705 void gsi_channel_trans_cancel_pending(struct gsi_channel *channel) 706 { 707 struct gsi_trans_info *trans_info = &channel->trans_info; 708 struct gsi_trans *trans; 709 bool cancelled; 710 711 /* channel->gsi->mutex is held by caller */ 712 spin_lock_bh(&trans_info->spinlock); 713 714 cancelled = !list_empty(&trans_info->pending); 715 list_for_each_entry(trans, &trans_info->pending, links) 716 trans->cancelled = true; 717 718 list_splice_tail_init(&trans_info->pending, &trans_info->complete); 719 720 spin_unlock_bh(&trans_info->spinlock); 721 722 /* Schedule NAPI polling to complete the cancelled transactions */ 723 if (cancelled) 724 napi_schedule(&channel->napi); 725 } 726 727 /* Issue a command to read a single byte from a channel */ 728 int gsi_trans_read_byte(struct gsi *gsi, u32 channel_id, dma_addr_t addr) 729 { 730 struct gsi_channel *channel = &gsi->channel[channel_id]; 731 struct gsi_ring *tre_ring = &channel->tre_ring; 732 struct gsi_trans_info *trans_info; 733 struct gsi_tre *dest_tre; 734 735 trans_info = &channel->trans_info; 736 737 /* First reserve the TRE, if possible */ 738 if (!gsi_trans_tre_reserve(trans_info, 1)) 739 return -EBUSY; 740 741 /* Now fill the reserved TRE and tell the hardware */ 742 743 dest_tre = gsi_ring_virt(tre_ring, tre_ring->index); 744 gsi_trans_tre_fill(dest_tre, addr, 1, true, false, IPA_CMD_NONE); 745 746 tre_ring->index++; 747 gsi_channel_doorbell(channel); 748 749 return 0; 750 } 751 752 /* Mark a gsi_trans_read_byte() request done */ 753 void gsi_trans_read_byte_done(struct gsi *gsi, u32 channel_id) 754 { 755 struct gsi_channel *channel = &gsi->channel[channel_id]; 756 757 gsi_trans_tre_release(&channel->trans_info, 1); 758 } 759 760 /* Initialize a channel's GSI transaction info */ 761 int gsi_channel_trans_init(struct gsi *gsi, u32 channel_id) 762 { 763 struct gsi_channel *channel = &gsi->channel[channel_id]; 764 u32 tre_count = channel->tre_count; 765 struct gsi_trans_info *trans_info; 766 u32 tre_max; 767 int ret; 768 769 /* Ensure the size of a channel element is what's expected */ 770 BUILD_BUG_ON(sizeof(struct gsi_tre) != GSI_RING_ELEMENT_SIZE); 771 772 trans_info = &channel->trans_info; 773 774 /* The tre_avail field is what ultimately limits the number of 775 * outstanding transactions and their resources. A transaction 776 * allocation succeeds only if the TREs available are sufficient 777 * for what the transaction might need. 778 */ 779 tre_max = gsi_channel_tre_max(channel->gsi, channel_id); 780 atomic_set(&trans_info->tre_avail, tre_max); 781 782 /* We can't use more TREs than the number available in the ring. 783 * This limits the number of transactions that can be outstanding. 784 * Worst case is one TRE per transaction (but we actually limit 785 * it to something a little less than that). By allocating a 786 * power-of-two number of transactions we can use an index 787 * modulo that number to determine the next one that's free. 788 * Transactions are allocated one at a time. 789 */ 790 trans_info->trans = kcalloc(tre_count, sizeof(*trans_info->trans), 791 GFP_KERNEL); 792 if (!trans_info->trans) 793 return -ENOMEM; 794 trans_info->free_id = 0; /* all modulo channel->tre_count */ 795 trans_info->allocated_id = 0; 796 trans_info->committed_id = 0; 797 trans_info->pending_id = 0; 798 trans_info->completed_id = 0; 799 800 /* A completion event contains a pointer to the TRE that caused 801 * the event (which will be the last one used by the transaction). 802 * Each entry in this map records the transaction associated 803 * with a corresponding completed TRE. 804 */ 805 trans_info->map = kcalloc(tre_count, sizeof(*trans_info->map), 806 GFP_KERNEL); 807 if (!trans_info->map) { 808 ret = -ENOMEM; 809 goto err_trans_free; 810 } 811 812 /* A transaction uses a scatterlist array to represent the data 813 * transfers implemented by the transaction. Each scatterlist 814 * element is used to fill a single TRE when the transaction is 815 * committed. So we need as many scatterlist elements as the 816 * maximum number of TREs that can be outstanding. 817 */ 818 ret = gsi_trans_pool_init(&trans_info->sg_pool, 819 sizeof(struct scatterlist), 820 tre_max, channel->trans_tre_max); 821 if (ret) 822 goto err_map_free; 823 824 spin_lock_init(&trans_info->spinlock); 825 INIT_LIST_HEAD(&trans_info->alloc); 826 INIT_LIST_HEAD(&trans_info->committed); 827 INIT_LIST_HEAD(&trans_info->pending); 828 INIT_LIST_HEAD(&trans_info->complete); 829 INIT_LIST_HEAD(&trans_info->polled); 830 831 return 0; 832 833 err_map_free: 834 kfree(trans_info->map); 835 err_trans_free: 836 kfree(trans_info->trans); 837 838 dev_err(gsi->dev, "error %d initializing channel %u transactions\n", 839 ret, channel_id); 840 841 return ret; 842 } 843 844 /* Inverse of gsi_channel_trans_init() */ 845 void gsi_channel_trans_exit(struct gsi_channel *channel) 846 { 847 struct gsi_trans_info *trans_info = &channel->trans_info; 848 849 gsi_trans_pool_exit(&trans_info->sg_pool); 850 kfree(trans_info->trans); 851 kfree(trans_info->map); 852 } 853