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