2 * DMA-able FIFO implementation
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/list.h>
24 #include <linux/bug.h>
29 #define df_trace(s, args...) pr_debug(s, ##args)
31 #define df_trace(s, args...)
34 #define FAIL(fifo, condition, format...) ({ \
35 fifo->corrupt = !!(condition); \
36 WARN(fifo->corrupt, format); \
40 * private helper fn to determine if check is in open interval (lo,hi)
42 static bool addr_check(unsigned check, unsigned lo, unsigned hi)
44 return check - (lo + 1) < (hi - 1) - lo;
48 * dma_fifo_init: initialize the fifo to a valid but inoperative state
49 * @fifo: address of in-place "struct dma_fifo" object
51 void dma_fifo_init(struct dma_fifo *fifo)
53 memset(fifo, 0, sizeof(*fifo));
54 INIT_LIST_HEAD(&fifo->pending);
58 * dma_fifo_alloc - initialize and allocate dma_fifo
59 * @fifo: address of in-place "struct dma_fifo" object
60 * @size: 'apparent' size, in bytes, of fifo
61 * @align: dma alignment to maintain (should be at least cpu cache alignment),
63 * @tx_limit: maximum # of bytes transmissable per dma (rounded down to
64 * multiple of alignment, but at least align size)
65 * @open_limit: maximum # of outstanding dma transactions allowed
66 * @gfp_mask: get_free_pages mask, passed to kmalloc()
68 * The 'apparent' size will be rounded up to next greater aligned size.
69 * Returns 0 if no error, otherwise an error code
71 int dma_fifo_alloc(struct dma_fifo *fifo, int size, unsigned align,
72 int tx_limit, int open_limit, gfp_t gfp_mask)
76 if (!is_power_of_2(align) || size < 0)
79 size = round_up(size, align);
80 capacity = size + align * open_limit + align * DMA_FIFO_GUARD;
81 fifo->data = kmalloc(capacity, gfp_mask);
91 fifo->tx_limit = max_t(int, round_down(tx_limit, align), align);
93 fifo->open_limit = open_limit;
94 fifo->guard = size + align * open_limit;
95 fifo->capacity = capacity;
102 * dma_fifo_free - frees the fifo
103 * @fifo: address of in-place "struct dma_fifo" to free
105 * Also reinits the fifo to a valid but inoperative state. This
106 * allows the fifo to be reused with a different target requiring
107 * different fifo parameters.
109 void dma_fifo_free(struct dma_fifo *fifo)
111 struct dma_pending *pending, *next;
113 if (fifo->data == NULL)
116 list_for_each_entry_safe(pending, next, &fifo->pending, link)
117 list_del_init(&pending->link);
123 * dma_fifo_reset - dumps the fifo contents and reinits for reuse
124 * @fifo: address of in-place "struct dma_fifo" to reset
126 void dma_fifo_reset(struct dma_fifo *fifo)
128 struct dma_pending *pending, *next;
130 if (fifo->data == NULL)
133 list_for_each_entry_safe(pending, next, &fifo->pending, link)
134 list_del_init(&pending->link);
138 fifo->avail = fifo->size;
144 * dma_fifo_in - copies data into the fifo
145 * @fifo: address of in-place "struct dma_fifo" to write to
146 * @src: buffer to copy from
147 * @n: # of bytes to copy
149 * Returns the # of bytes actually copied, which can be less than requested if
150 * the fifo becomes full. If < 0, return is error code.
152 int dma_fifo_in(struct dma_fifo *fifo, const void *src, int n)
156 if (fifo->data == NULL)
166 ofs = fifo->in % fifo->capacity;
167 l = min(n, fifo->capacity - ofs);
168 memcpy(fifo->data + ofs, src, l);
169 memcpy(fifo->data, src + l, n - l);
171 if (FAIL(fifo, addr_check(fifo->done, fifo->in, fifo->in + n) ||
173 "fifo corrupt: in:%u out:%u done:%u n:%d avail:%d",
174 fifo->in, fifo->out, fifo->done, n, fifo->avail))
180 df_trace("in:%u out:%u done:%u n:%d avail:%d", fifo->in, fifo->out,
181 fifo->done, n, fifo->avail);
187 * dma_fifo_out_pend - gets address/len of next avail read and marks as pended
188 * @fifo: address of in-place "struct dma_fifo" to read from
189 * @pended: address of structure to fill with read address/len
190 * The data/len fields will be NULL/0 if no dma is pended.
192 * Returns the # of used bytes remaining in fifo (ie, if > 0, more data
193 * remains in the fifo that was not pended). If < 0, return is error code.
195 int dma_fifo_out_pend(struct dma_fifo *fifo, struct dma_pending *pended)
197 unsigned len, n, ofs, l, limit;
199 if (fifo->data == NULL)
206 pended->out = fifo->out;
208 len = fifo->in - fifo->out;
211 if (fifo->open == fifo->open_limit)
215 ofs = fifo->out % fifo->capacity;
216 l = fifo->capacity - ofs;
217 limit = min_t(unsigned, l, fifo->tx_limit);
221 } else if (ofs + n > fifo->guard) {
223 fifo->in = fifo->out;
225 fifo->out += round_up(n, fifo->align);
226 fifo->in = fifo->out;
229 df_trace("in: %u out: %u done: %u n: %d len: %u avail: %d", fifo->in,
230 fifo->out, fifo->done, n, len, fifo->avail);
233 pended->data = fifo->data + ofs;
234 pended->next = fifo->out;
235 list_add_tail(&pended->link, &fifo->pending);
238 if (FAIL(fifo, fifo->open > fifo->open_limit,
239 "past open limit:%d (limit:%d)",
240 fifo->open, fifo->open_limit))
242 if (FAIL(fifo, fifo->out & (fifo->align - 1),
243 "fifo out unaligned:%u (align:%u)",
244 fifo->out, fifo->align))
251 * dma_fifo_out_complete - marks pended dma as completed
252 * @fifo: address of in-place "struct dma_fifo" which was read from
253 * @complete: address of structure for previously pended dma to mark completed
255 int dma_fifo_out_complete(struct dma_fifo *fifo, struct dma_pending *complete)
257 struct dma_pending *pending, *next, *tmp;
259 if (fifo->data == NULL)
263 if (list_empty(&fifo->pending) && fifo->open == 0)
266 if (FAIL(fifo, list_empty(&fifo->pending) != (fifo->open == 0),
267 "pending list disagrees with open count:%d",
271 tmp = complete->data;
273 list_replace(&complete->link, &tmp->link);
274 dp_mark_completed(tmp);
276 /* Only update the fifo in the original pended order */
277 list_for_each_entry_safe(pending, next, &fifo->pending, link) {
278 if (!dp_is_completed(pending)) {
279 df_trace("still pending: saved out: %u len: %d",
280 pending->out, pending->len);
284 if (FAIL(fifo, pending->out != fifo->done ||
285 addr_check(fifo->in, fifo->done, pending->next),
286 "in:%u out:%u done:%u saved:%u next:%u",
287 fifo->in, fifo->out, fifo->done, pending->out,
291 list_del_init(&pending->link);
292 fifo->done = pending->next;
293 fifo->avail += pending->len;
296 df_trace("in: %u out: %u done: %u len: %u avail: %d", fifo->in,
297 fifo->out, fifo->done, pending->len, fifo->avail);
300 if (FAIL(fifo, fifo->open < 0, "open dma:%d < 0", fifo->open))
302 if (FAIL(fifo, fifo->avail > fifo->size, "fifo avail:%d > size:%d",
303 fifo->avail, fifo->size))