2 * DMA-able FIFO implementation
4 * Copyright (C) 2012 Peter Hurley <peter@hurleysoftware.com>
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.
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/list.h>
20 #include <linux/bug.h>
25 #define df_trace(s, args...) pr_debug(s, ##args)
27 #define df_trace(s, args...)
30 #define FAIL(fifo, condition, format...) ({ \
31 fifo->corrupt = !!(condition); \
32 WARN(fifo->corrupt, format); \
36 * private helper fn to determine if check is in open interval (lo,hi)
38 static bool addr_check(unsigned check
, unsigned lo
, unsigned hi
)
40 return check
- (lo
+ 1) < (hi
- 1) - lo
;
44 * dma_fifo_init: initialize the fifo to a valid but inoperative state
45 * @fifo: address of in-place "struct dma_fifo" object
47 void dma_fifo_init(struct dma_fifo
*fifo
)
49 memset(fifo
, 0, sizeof(*fifo
));
50 INIT_LIST_HEAD(&fifo
->pending
);
54 * dma_fifo_alloc - initialize and allocate dma_fifo
55 * @fifo: address of in-place "struct dma_fifo" object
56 * @size: 'apparent' size, in bytes, of fifo
57 * @align: dma alignment to maintain (should be at least cpu cache alignment),
59 * @tx_limit: maximum # of bytes transmissible per dma (rounded down to
60 * multiple of alignment, but at least align size)
61 * @open_limit: maximum # of outstanding dma transactions allowed
62 * @gfp_mask: get_free_pages mask, passed to kmalloc()
64 * The 'apparent' size will be rounded up to next greater aligned size.
65 * Returns 0 if no error, otherwise an error code
67 int dma_fifo_alloc(struct dma_fifo
*fifo
, int size
, unsigned align
,
68 int tx_limit
, int open_limit
, gfp_t gfp_mask
)
72 if (!is_power_of_2(align
) || size
< 0)
75 size
= round_up(size
, align
);
76 capacity
= size
+ align
* open_limit
+ align
* DMA_FIFO_GUARD
;
77 fifo
->data
= kmalloc(capacity
, gfp_mask
);
87 fifo
->tx_limit
= max_t(int, round_down(tx_limit
, align
), align
);
89 fifo
->open_limit
= open_limit
;
90 fifo
->guard
= size
+ align
* open_limit
;
91 fifo
->capacity
= capacity
;
98 * dma_fifo_free - frees the fifo
99 * @fifo: address of in-place "struct dma_fifo" to free
101 * Also reinits the fifo to a valid but inoperative state. This
102 * allows the fifo to be reused with a different target requiring
103 * different fifo parameters.
105 void dma_fifo_free(struct dma_fifo
*fifo
)
107 struct dma_pending
*pending
, *next
;
109 if (fifo
->data
== NULL
)
112 list_for_each_entry_safe(pending
, next
, &fifo
->pending
, link
)
113 list_del_init(&pending
->link
);
119 * dma_fifo_reset - dumps the fifo contents and reinits for reuse
120 * @fifo: address of in-place "struct dma_fifo" to reset
122 void dma_fifo_reset(struct dma_fifo
*fifo
)
124 struct dma_pending
*pending
, *next
;
126 if (fifo
->data
== NULL
)
129 list_for_each_entry_safe(pending
, next
, &fifo
->pending
, link
)
130 list_del_init(&pending
->link
);
134 fifo
->avail
= fifo
->size
;
140 * dma_fifo_in - copies data into the fifo
141 * @fifo: address of in-place "struct dma_fifo" to write to
142 * @src: buffer to copy from
143 * @n: # of bytes to copy
145 * Returns the # of bytes actually copied, which can be less than requested if
146 * the fifo becomes full. If < 0, return is error code.
148 int dma_fifo_in(struct dma_fifo
*fifo
, const void *src
, int n
)
152 if (fifo
->data
== NULL
)
162 ofs
= fifo
->in
% fifo
->capacity
;
163 l
= min(n
, fifo
->capacity
- ofs
);
164 memcpy(fifo
->data
+ ofs
, src
, l
);
165 memcpy(fifo
->data
, src
+ l
, n
- l
);
167 if (FAIL(fifo
, addr_check(fifo
->done
, fifo
->in
, fifo
->in
+ n
) ||
169 "fifo corrupt: in:%u out:%u done:%u n:%d avail:%d",
170 fifo
->in
, fifo
->out
, fifo
->done
, n
, fifo
->avail
))
176 df_trace("in:%u out:%u done:%u n:%d avail:%d", fifo
->in
, fifo
->out
,
177 fifo
->done
, n
, fifo
->avail
);
183 * dma_fifo_out_pend - gets address/len of next avail read and marks as pended
184 * @fifo: address of in-place "struct dma_fifo" to read from
185 * @pended: address of structure to fill with read address/len
186 * The data/len fields will be NULL/0 if no dma is pended.
188 * Returns the # of used bytes remaining in fifo (ie, if > 0, more data
189 * remains in the fifo that was not pended). If < 0, return is error code.
191 int dma_fifo_out_pend(struct dma_fifo
*fifo
, struct dma_pending
*pended
)
193 unsigned len
, n
, ofs
, l
, limit
;
195 if (fifo
->data
== NULL
)
202 pended
->out
= fifo
->out
;
204 len
= fifo
->in
- fifo
->out
;
207 if (fifo
->open
== fifo
->open_limit
)
211 ofs
= fifo
->out
% fifo
->capacity
;
212 l
= fifo
->capacity
- ofs
;
213 limit
= min_t(unsigned, l
, fifo
->tx_limit
);
217 } else if (ofs
+ n
> fifo
->guard
) {
219 fifo
->in
= fifo
->out
;
221 fifo
->out
+= round_up(n
, fifo
->align
);
222 fifo
->in
= fifo
->out
;
225 df_trace("in: %u out: %u done: %u n: %d len: %u avail: %d", fifo
->in
,
226 fifo
->out
, fifo
->done
, n
, len
, fifo
->avail
);
229 pended
->data
= fifo
->data
+ ofs
;
230 pended
->next
= fifo
->out
;
231 list_add_tail(&pended
->link
, &fifo
->pending
);
234 if (FAIL(fifo
, fifo
->open
> fifo
->open_limit
,
235 "past open limit:%d (limit:%d)",
236 fifo
->open
, fifo
->open_limit
))
238 if (FAIL(fifo
, fifo
->out
& (fifo
->align
- 1),
239 "fifo out unaligned:%u (align:%u)",
240 fifo
->out
, fifo
->align
))
247 * dma_fifo_out_complete - marks pended dma as completed
248 * @fifo: address of in-place "struct dma_fifo" which was read from
249 * @complete: address of structure for previously pended dma to mark completed
251 int dma_fifo_out_complete(struct dma_fifo
*fifo
, struct dma_pending
*complete
)
253 struct dma_pending
*pending
, *next
, *tmp
;
255 if (fifo
->data
== NULL
)
259 if (list_empty(&fifo
->pending
) && fifo
->open
== 0)
262 if (FAIL(fifo
, list_empty(&fifo
->pending
) != (fifo
->open
== 0),
263 "pending list disagrees with open count:%d",
267 tmp
= complete
->data
;
269 list_replace(&complete
->link
, &tmp
->link
);
270 dp_mark_completed(tmp
);
272 /* Only update the fifo in the original pended order */
273 list_for_each_entry_safe(pending
, next
, &fifo
->pending
, link
) {
274 if (!dp_is_completed(pending
)) {
275 df_trace("still pending: saved out: %u len: %d",
276 pending
->out
, pending
->len
);
280 if (FAIL(fifo
, pending
->out
!= fifo
->done
||
281 addr_check(fifo
->in
, fifo
->done
, pending
->next
),
282 "in:%u out:%u done:%u saved:%u next:%u",
283 fifo
->in
, fifo
->out
, fifo
->done
, pending
->out
,
287 list_del_init(&pending
->link
);
288 fifo
->done
= pending
->next
;
289 fifo
->avail
+= pending
->len
;
292 df_trace("in: %u out: %u done: %u len: %u avail: %d", fifo
->in
,
293 fifo
->out
, fifo
->done
, pending
->len
, fifo
->avail
);
296 if (FAIL(fifo
, fifo
->open
< 0, "open dma:%d < 0", fifo
->open
))
298 if (FAIL(fifo
, fifo
->avail
> fifo
->size
, "fifo avail:%d > size:%d",
299 fifo
->avail
, fifo
->size
))
This page took 0.039673 seconds and 6 git commands to generate.