dma_fifo.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * DMA-able FIFO implementation
  3. *
  4. * Copyright (C) 2012 Peter Hurley <peter@hurleysoftware.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/list.h>
  19. #include <linux/bug.h>
  20. #include "dma_fifo.h"
  21. #ifdef DEBUG_TRACING
  22. #define df_trace(s, args...) pr_debug(s, ##args)
  23. #else
  24. #define df_trace(s, args...)
  25. #endif
  26. #define FAIL(fifo, condition, format...) ({ \
  27. fifo->corrupt = !!(condition); \
  28. WARN(fifo->corrupt, format); \
  29. })
  30. /*
  31. * private helper fn to determine if check is in open interval (lo,hi)
  32. */
  33. static bool addr_check(unsigned check, unsigned lo, unsigned hi)
  34. {
  35. return check - (lo + 1) < (hi - 1) - lo;
  36. }
  37. /**
  38. * dma_fifo_init: initialize the fifo to a valid but inoperative state
  39. * @fifo: address of in-place "struct dma_fifo" object
  40. */
  41. void dma_fifo_init(struct dma_fifo *fifo)
  42. {
  43. memset(fifo, 0, sizeof(*fifo));
  44. INIT_LIST_HEAD(&fifo->pending);
  45. }
  46. /**
  47. * dma_fifo_alloc - initialize and allocate dma_fifo
  48. * @fifo: address of in-place "struct dma_fifo" object
  49. * @size: 'apparent' size, in bytes, of fifo
  50. * @align: dma alignment to maintain (should be at least cpu cache alignment),
  51. * must be power of 2
  52. * @tx_limit: maximum # of bytes transmissible per dma (rounded down to
  53. * multiple of alignment, but at least align size)
  54. * @open_limit: maximum # of outstanding dma transactions allowed
  55. * @gfp_mask: get_free_pages mask, passed to kmalloc()
  56. *
  57. * The 'apparent' size will be rounded up to next greater aligned size.
  58. * Returns 0 if no error, otherwise an error code
  59. */
  60. int dma_fifo_alloc(struct dma_fifo *fifo, int size, unsigned align,
  61. int tx_limit, int open_limit, gfp_t gfp_mask)
  62. {
  63. int capacity;
  64. if (!is_power_of_2(align) || size < 0)
  65. return -EINVAL;
  66. size = round_up(size, align);
  67. capacity = size + align * open_limit + align * DMA_FIFO_GUARD;
  68. fifo->data = kmalloc(capacity, gfp_mask);
  69. if (!fifo->data)
  70. return -ENOMEM;
  71. fifo->in = 0;
  72. fifo->out = 0;
  73. fifo->done = 0;
  74. fifo->size = size;
  75. fifo->avail = size;
  76. fifo->align = align;
  77. fifo->tx_limit = max_t(int, round_down(tx_limit, align), align);
  78. fifo->open = 0;
  79. fifo->open_limit = open_limit;
  80. fifo->guard = size + align * open_limit;
  81. fifo->capacity = capacity;
  82. fifo->corrupt = 0;
  83. return 0;
  84. }
  85. /**
  86. * dma_fifo_free - frees the fifo
  87. * @fifo: address of in-place "struct dma_fifo" to free
  88. *
  89. * Also reinits the fifo to a valid but inoperative state. This
  90. * allows the fifo to be reused with a different target requiring
  91. * different fifo parameters.
  92. */
  93. void dma_fifo_free(struct dma_fifo *fifo)
  94. {
  95. struct dma_pending *pending, *next;
  96. if (fifo->data == NULL)
  97. return;
  98. list_for_each_entry_safe(pending, next, &fifo->pending, link)
  99. list_del_init(&pending->link);
  100. kfree(fifo->data);
  101. fifo->data = NULL;
  102. }
  103. /**
  104. * dma_fifo_reset - dumps the fifo contents and reinits for reuse
  105. * @fifo: address of in-place "struct dma_fifo" to reset
  106. */
  107. void dma_fifo_reset(struct dma_fifo *fifo)
  108. {
  109. struct dma_pending *pending, *next;
  110. if (fifo->data == NULL)
  111. return;
  112. list_for_each_entry_safe(pending, next, &fifo->pending, link)
  113. list_del_init(&pending->link);
  114. fifo->in = 0;
  115. fifo->out = 0;
  116. fifo->done = 0;
  117. fifo->avail = fifo->size;
  118. fifo->open = 0;
  119. fifo->corrupt = 0;
  120. }
  121. /**
  122. * dma_fifo_in - copies data into the fifo
  123. * @fifo: address of in-place "struct dma_fifo" to write to
  124. * @src: buffer to copy from
  125. * @n: # of bytes to copy
  126. *
  127. * Returns the # of bytes actually copied, which can be less than requested if
  128. * the fifo becomes full. If < 0, return is error code.
  129. */
  130. int dma_fifo_in(struct dma_fifo *fifo, const void *src, int n)
  131. {
  132. int ofs, l;
  133. if (fifo->data == NULL)
  134. return -ENOENT;
  135. if (fifo->corrupt)
  136. return -ENXIO;
  137. if (n > fifo->avail)
  138. n = fifo->avail;
  139. if (n <= 0)
  140. return 0;
  141. ofs = fifo->in % fifo->capacity;
  142. l = min(n, fifo->capacity - ofs);
  143. memcpy(fifo->data + ofs, src, l);
  144. memcpy(fifo->data, src + l, n - l);
  145. if (FAIL(fifo, addr_check(fifo->done, fifo->in, fifo->in + n) ||
  146. fifo->avail < n,
  147. "fifo corrupt: in:%u out:%u done:%u n:%d avail:%d",
  148. fifo->in, fifo->out, fifo->done, n, fifo->avail))
  149. return -ENXIO;
  150. fifo->in += n;
  151. fifo->avail -= n;
  152. df_trace("in:%u out:%u done:%u n:%d avail:%d", fifo->in, fifo->out,
  153. fifo->done, n, fifo->avail);
  154. return n;
  155. }
  156. /**
  157. * dma_fifo_out_pend - gets address/len of next avail read and marks as pended
  158. * @fifo: address of in-place "struct dma_fifo" to read from
  159. * @pended: address of structure to fill with read address/len
  160. * The data/len fields will be NULL/0 if no dma is pended.
  161. *
  162. * Returns the # of used bytes remaining in fifo (ie, if > 0, more data
  163. * remains in the fifo that was not pended). If < 0, return is error code.
  164. */
  165. int dma_fifo_out_pend(struct dma_fifo *fifo, struct dma_pending *pended)
  166. {
  167. unsigned len, n, ofs, l, limit;
  168. if (fifo->data == NULL)
  169. return -ENOENT;
  170. if (fifo->corrupt)
  171. return -ENXIO;
  172. pended->len = 0;
  173. pended->data = NULL;
  174. pended->out = fifo->out;
  175. len = fifo->in - fifo->out;
  176. if (!len)
  177. return -ENODATA;
  178. if (fifo->open == fifo->open_limit)
  179. return -EAGAIN;
  180. n = len;
  181. ofs = fifo->out % fifo->capacity;
  182. l = fifo->capacity - ofs;
  183. limit = min_t(unsigned, l, fifo->tx_limit);
  184. if (n > limit) {
  185. n = limit;
  186. fifo->out += limit;
  187. } else if (ofs + n > fifo->guard) {
  188. fifo->out += l;
  189. fifo->in = fifo->out;
  190. } else {
  191. fifo->out += round_up(n, fifo->align);
  192. fifo->in = fifo->out;
  193. }
  194. df_trace("in: %u out: %u done: %u n: %d len: %u avail: %d", fifo->in,
  195. fifo->out, fifo->done, n, len, fifo->avail);
  196. pended->len = n;
  197. pended->data = fifo->data + ofs;
  198. pended->next = fifo->out;
  199. list_add_tail(&pended->link, &fifo->pending);
  200. ++fifo->open;
  201. if (FAIL(fifo, fifo->open > fifo->open_limit,
  202. "past open limit:%d (limit:%d)",
  203. fifo->open, fifo->open_limit))
  204. return -ENXIO;
  205. if (FAIL(fifo, fifo->out & (fifo->align - 1),
  206. "fifo out unaligned:%u (align:%u)",
  207. fifo->out, fifo->align))
  208. return -ENXIO;
  209. return len - n;
  210. }
  211. /**
  212. * dma_fifo_out_complete - marks pended dma as completed
  213. * @fifo: address of in-place "struct dma_fifo" which was read from
  214. * @complete: address of structure for previously pended dma to mark completed
  215. */
  216. int dma_fifo_out_complete(struct dma_fifo *fifo, struct dma_pending *complete)
  217. {
  218. struct dma_pending *pending, *next, *tmp;
  219. if (fifo->data == NULL)
  220. return -ENOENT;
  221. if (fifo->corrupt)
  222. return -ENXIO;
  223. if (list_empty(&fifo->pending) && fifo->open == 0)
  224. return -EINVAL;
  225. if (FAIL(fifo, list_empty(&fifo->pending) != (fifo->open == 0),
  226. "pending list disagrees with open count:%d",
  227. fifo->open))
  228. return -ENXIO;
  229. tmp = complete->data;
  230. *tmp = *complete;
  231. list_replace(&complete->link, &tmp->link);
  232. dp_mark_completed(tmp);
  233. /* Only update the fifo in the original pended order */
  234. list_for_each_entry_safe(pending, next, &fifo->pending, link) {
  235. if (!dp_is_completed(pending)) {
  236. df_trace("still pending: saved out: %u len: %d",
  237. pending->out, pending->len);
  238. break;
  239. }
  240. if (FAIL(fifo, pending->out != fifo->done ||
  241. addr_check(fifo->in, fifo->done, pending->next),
  242. "in:%u out:%u done:%u saved:%u next:%u",
  243. fifo->in, fifo->out, fifo->done, pending->out,
  244. pending->next))
  245. return -ENXIO;
  246. list_del_init(&pending->link);
  247. fifo->done = pending->next;
  248. fifo->avail += pending->len;
  249. --fifo->open;
  250. df_trace("in: %u out: %u done: %u len: %u avail: %d", fifo->in,
  251. fifo->out, fifo->done, pending->len, fifo->avail);
  252. }
  253. if (FAIL(fifo, fifo->open < 0, "open dma:%d < 0", fifo->open))
  254. return -ENXIO;
  255. if (FAIL(fifo, fifo->avail > fifo->size, "fifo avail:%d > size:%d",
  256. fifo->avail, fifo->size))
  257. return -ENXIO;
  258. return 0;
  259. }