1 // SPDX-License-Identifier: GPL-2.0+
3 * DMA-able FIFO implementation
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/list.h>
11 #include <linux/bug.h>
16 #define df_trace(s, args...) pr_debug(s, ##args)
18 #define df_trace(s, args...)
21 #define FAIL(fifo, condition, format...) ({ \
22 fifo->corrupt = !!(condition); \
23 WARN(fifo->corrupt, format); \
27 * private helper fn to determine if check is in open interval (lo,hi)
29 static bool addr_check(unsigned int check, unsigned int lo, unsigned int hi)
31 return check - (lo + 1) < (hi - 1) - lo;
35 * dma_fifo_init: initialize the fifo to a valid but inoperative state
36 * @fifo: address of in-place "struct dma_fifo" object
38 void dma_fifo_init(struct dma_fifo *fifo)
40 memset(fifo, 0, sizeof(*fifo));
41 INIT_LIST_HEAD(&fifo->pending);
45 * dma_fifo_alloc - initialize and allocate dma_fifo
46 * @fifo: address of in-place "struct dma_fifo" object
47 * @size: 'apparent' size, in bytes, of fifo
48 * @align: dma alignment to maintain (should be at least cpu cache alignment),
50 * @tx_limit: maximum # of bytes transmissible per dma (rounded down to
51 * multiple of alignment, but at least align size)
52 * @open_limit: maximum # of outstanding dma transactions allowed
53 * @gfp_mask: get_free_pages mask, passed to kmalloc()
55 * The 'apparent' size will be rounded up to next greater aligned size.
56 * Returns 0 if no error, otherwise an error code
58 int dma_fifo_alloc(struct dma_fifo *fifo, int size, unsigned int align,
59 int tx_limit, int open_limit, gfp_t gfp_mask)
63 if (!is_power_of_2(align) || size < 0)
66 size = round_up(size, align);
67 capacity = size + align * open_limit + align * DMA_FIFO_GUARD;
68 fifo->data = kmalloc(capacity, gfp_mask);
78 fifo->tx_limit = max_t(int, round_down(tx_limit, align), align);
80 fifo->open_limit = open_limit;
81 fifo->guard = size + align * open_limit;
82 fifo->capacity = capacity;
89 * dma_fifo_free - frees the fifo
90 * @fifo: address of in-place "struct dma_fifo" to free
92 * Also reinits the fifo to a valid but inoperative state. This
93 * allows the fifo to be reused with a different target requiring
94 * different fifo parameters.
96 void dma_fifo_free(struct dma_fifo *fifo)
98 struct dma_pending *pending, *next;
103 list_for_each_entry_safe(pending, next, &fifo->pending, link)
104 list_del_init(&pending->link);
110 * dma_fifo_reset - dumps the fifo contents and reinits for reuse
111 * @fifo: address of in-place "struct dma_fifo" to reset
113 void dma_fifo_reset(struct dma_fifo *fifo)
115 struct dma_pending *pending, *next;
120 list_for_each_entry_safe(pending, next, &fifo->pending, link)
121 list_del_init(&pending->link);
125 fifo->avail = fifo->size;
131 * dma_fifo_in - copies data into the fifo
132 * @fifo: address of in-place "struct dma_fifo" to write to
133 * @src: buffer to copy from
134 * @n: # of bytes to copy
136 * Returns the # of bytes actually copied, which can be less than requested if
137 * the fifo becomes full. If < 0, return is error code.
139 int dma_fifo_in(struct dma_fifo *fifo, const void *src, int n)
153 ofs = fifo->in % fifo->capacity;
154 l = min(n, fifo->capacity - ofs);
155 memcpy(fifo->data + ofs, src, l);
156 memcpy(fifo->data, src + l, n - l);
158 if (FAIL(fifo, addr_check(fifo->done, fifo->in, fifo->in + n) ||
160 "fifo corrupt: in:%u out:%u done:%u n:%d avail:%d",
161 fifo->in, fifo->out, fifo->done, n, fifo->avail))
167 df_trace("in:%u out:%u done:%u n:%d avail:%d", fifo->in, fifo->out,
168 fifo->done, n, fifo->avail);
174 * dma_fifo_out_pend - gets address/len of next avail read and marks as pended
175 * @fifo: address of in-place "struct dma_fifo" to read from
176 * @pended: address of structure to fill with read address/len
177 * The data/len fields will be NULL/0 if no dma is pended.
179 * Returns the # of used bytes remaining in fifo (ie, if > 0, more data
180 * remains in the fifo that was not pended). If < 0, return is error code.
182 int dma_fifo_out_pend(struct dma_fifo *fifo, struct dma_pending *pended)
184 unsigned int len, n, ofs, l, limit;
193 pended->out = fifo->out;
195 len = fifo->in - fifo->out;
198 if (fifo->open == fifo->open_limit)
202 ofs = fifo->out % fifo->capacity;
203 l = fifo->capacity - ofs;
204 limit = min_t(unsigned int, l, fifo->tx_limit);
208 } else if (ofs + n > fifo->guard) {
210 fifo->in = fifo->out;
212 fifo->out += round_up(n, fifo->align);
213 fifo->in = fifo->out;
216 df_trace("in: %u out: %u done: %u n: %d len: %u avail: %d", fifo->in,
217 fifo->out, fifo->done, n, len, fifo->avail);
220 pended->data = fifo->data + ofs;
221 pended->next = fifo->out;
222 list_add_tail(&pended->link, &fifo->pending);
225 if (FAIL(fifo, fifo->open > fifo->open_limit,
226 "past open limit:%d (limit:%d)",
227 fifo->open, fifo->open_limit))
229 if (FAIL(fifo, fifo->out & (fifo->align - 1),
230 "fifo out unaligned:%u (align:%u)",
231 fifo->out, fifo->align))
238 * dma_fifo_out_complete - marks pended dma as completed
239 * @fifo: address of in-place "struct dma_fifo" which was read from
240 * @complete: address of structure for previously pended dma to mark completed
242 int dma_fifo_out_complete(struct dma_fifo *fifo, struct dma_pending *complete)
244 struct dma_pending *pending, *next, *tmp;
250 if (list_empty(&fifo->pending) && fifo->open == 0)
253 if (FAIL(fifo, list_empty(&fifo->pending) != (fifo->open == 0),
254 "pending list disagrees with open count:%d",
258 tmp = complete->data;
260 list_replace(&complete->link, &tmp->link);
261 dp_mark_completed(tmp);
263 /* Only update the fifo in the original pended order */
264 list_for_each_entry_safe(pending, next, &fifo->pending, link) {
265 if (!dp_is_completed(pending)) {
266 df_trace("still pending: saved out: %u len: %d",
267 pending->out, pending->len);
271 if (FAIL(fifo, pending->out != fifo->done ||
272 addr_check(fifo->in, fifo->done, pending->next),
273 "in:%u out:%u done:%u saved:%u next:%u",
274 fifo->in, fifo->out, fifo->done, pending->out,
278 list_del_init(&pending->link);
279 fifo->done = pending->next;
280 fifo->avail += pending->len;
283 df_trace("in: %u out: %u done: %u len: %u avail: %d", fifo->in,
284 fifo->out, fifo->done, pending->len, fifo->avail);
287 if (FAIL(fifo, fifo->open < 0, "open dma:%d < 0", fifo->open))
289 if (FAIL(fifo, fifo->avail > fifo->size, "fifo avail:%d > size:%d",
290 fifo->avail, fifo->size))