dma_queue.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright 2012 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #ifndef _GXIO_DMA_QUEUE_H_
  15. #define _GXIO_DMA_QUEUE_H_
  16. /*
  17. * DMA queue management APIs shared between TRIO and mPIPE.
  18. */
  19. #include <gxio/common.h>
  20. /* The credit counter lives in the high 32 bits. */
  21. #define DMA_QUEUE_CREDIT_SHIFT 32
  22. /*
  23. * State object that tracks a DMA queue's head and tail indices, as
  24. * well as the number of commands posted and completed. The
  25. * structure is accessed via a thread-safe, lock-free algorithm.
  26. */
  27. typedef struct {
  28. /*
  29. * Address of a MPIPE_EDMA_POST_REGION_VAL_t,
  30. * TRIO_PUSH_DMA_REGION_VAL_t, or TRIO_PULL_DMA_REGION_VAL_t
  31. * register. These register have identical encodings and provide
  32. * information about how many commands have been processed.
  33. */
  34. void *post_region_addr;
  35. /*
  36. * A lazily-updated count of how many edescs the hardware has
  37. * completed.
  38. */
  39. uint64_t hw_complete_count __attribute__ ((aligned(64)));
  40. /*
  41. * High 32 bits are a count of available egress command credits,
  42. * low 24 bits are the next egress "slot".
  43. */
  44. int64_t credits_and_next_index;
  45. } __gxio_dma_queue_t;
  46. /* Initialize a dma queue. */
  47. extern void __gxio_dma_queue_init(__gxio_dma_queue_t *dma_queue,
  48. void *post_region_addr,
  49. unsigned int num_entries);
  50. /*
  51. * Update the "credits_and_next_index" and "hw_complete_count" fields
  52. * based on pending hardware completions. Note that some other thread
  53. * may have already done this and, importantly, may still be in the
  54. * process of updating "credits_and_next_index".
  55. */
  56. extern void __gxio_dma_queue_update_credits(__gxio_dma_queue_t *dma_queue);
  57. /* Wait for credits to become available. */
  58. extern int64_t __gxio_dma_queue_wait_for_credits(__gxio_dma_queue_t *dma_queue,
  59. int64_t modifier);
  60. /* Reserve slots in the queue, optionally waiting for slots to become
  61. * available, and optionally returning a "completion_slot" suitable for
  62. * direct comparison to "hw_complete_count".
  63. */
  64. static inline int64_t __gxio_dma_queue_reserve(__gxio_dma_queue_t *dma_queue,
  65. unsigned int num, bool wait,
  66. bool completion)
  67. {
  68. uint64_t slot;
  69. /*
  70. * Try to reserve 'num' egress command slots. We do this by
  71. * constructing a constant that subtracts N credits and adds N to
  72. * the index, and using fetchaddgez to only apply it if the credits
  73. * count doesn't go negative.
  74. */
  75. int64_t modifier = (((int64_t)(-num)) << DMA_QUEUE_CREDIT_SHIFT) | num;
  76. int64_t old =
  77. __insn_fetchaddgez(&dma_queue->credits_and_next_index,
  78. modifier);
  79. if (unlikely(old + modifier < 0)) {
  80. /*
  81. * We're out of credits. Try once to get more by checking for
  82. * completed egress commands. If that fails, wait or fail.
  83. */
  84. __gxio_dma_queue_update_credits(dma_queue);
  85. old = __insn_fetchaddgez(&dma_queue->credits_and_next_index,
  86. modifier);
  87. if (old + modifier < 0) {
  88. if (wait)
  89. old = __gxio_dma_queue_wait_for_credits
  90. (dma_queue, modifier);
  91. else
  92. return GXIO_ERR_DMA_CREDITS;
  93. }
  94. }
  95. /* The bottom 24 bits of old encode the "slot". */
  96. slot = (old & 0xffffff);
  97. if (completion) {
  98. /*
  99. * A "completion_slot" is a "slot" which can be compared to
  100. * "hw_complete_count" at any time in the future. To convert
  101. * "slot" into a "completion_slot", we access "hw_complete_count"
  102. * once (knowing that we have reserved a slot, and thus, it will
  103. * be "basically" accurate), and combine its high 40 bits with
  104. * the 24 bit "slot", and handle "wrapping" by adding "1 << 24"
  105. * if the result is LESS than "hw_complete_count".
  106. */
  107. uint64_t complete;
  108. complete = ACCESS_ONCE(dma_queue->hw_complete_count);
  109. slot |= (complete & 0xffffffffff000000);
  110. if (slot < complete)
  111. slot += 0x1000000;
  112. }
  113. /*
  114. * If any of our slots mod 256 were equivalent to 0, go ahead and
  115. * collect some egress credits, and update "hw_complete_count", and
  116. * make sure the index doesn't overflow into the credits.
  117. */
  118. if (unlikely(((old + num) & 0xff) < num)) {
  119. __gxio_dma_queue_update_credits(dma_queue);
  120. /* Make sure the index doesn't overflow into the credits. */
  121. #ifdef __BIG_ENDIAN__
  122. *(((uint8_t *)&dma_queue->credits_and_next_index) + 4) = 0;
  123. #else
  124. *(((uint8_t *)&dma_queue->credits_and_next_index) + 3) = 0;
  125. #endif
  126. }
  127. return slot;
  128. }
  129. /* Non-inlinable "__gxio_dma_queue_reserve(..., true)". */
  130. extern int64_t __gxio_dma_queue_reserve_aux(__gxio_dma_queue_t *dma_queue,
  131. unsigned int num, int wait);
  132. /* Check whether a particular "completion slot" has completed.
  133. *
  134. * Note that this function requires a "completion slot", and thus
  135. * cannot be used with the result of any "reserve_fast" function.
  136. */
  137. extern int __gxio_dma_queue_is_complete(__gxio_dma_queue_t *dma_queue,
  138. int64_t completion_slot, int update);
  139. #endif /* !_GXIO_DMA_QUEUE_H_ */