1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/kernel.h>
18#include <linux/slab.h>
19#include <linux/list.h>
20#include <linux/bug.h>
21
22#include "dma_fifo.h"
23
24#ifdef DEBUG_TRACING
25#define df_trace(s, args...) pr_debug(s, ##args)
26#else
27#define df_trace(s, args...)
28#endif
29
30#define FAIL(fifo, condition, format...) ({ \
31 fifo->corrupt = !!(condition); \
32 WARN(fifo->corrupt, format); \
33})
34
35
36
37
38static bool addr_check(unsigned check, unsigned lo, unsigned hi)
39{
40 return check - (lo + 1) < (hi - 1) - lo;
41}
42
43
44
45
46
47void dma_fifo_init(struct dma_fifo *fifo)
48{
49 memset(fifo, 0, sizeof(*fifo));
50 INIT_LIST_HEAD(&fifo->pending);
51}
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67int dma_fifo_alloc(struct dma_fifo *fifo, int size, unsigned align,
68 int tx_limit, int open_limit, gfp_t gfp_mask)
69{
70 int capacity;
71
72 if (!is_power_of_2(align) || size < 0)
73 return -EINVAL;
74
75 size = round_up(size, align);
76 capacity = size + align * open_limit + align * DMA_FIFO_GUARD;
77 fifo->data = kmalloc(capacity, gfp_mask);
78 if (!fifo->data)
79 return -ENOMEM;
80
81 fifo->in = 0;
82 fifo->out = 0;
83 fifo->done = 0;
84 fifo->size = size;
85 fifo->avail = size;
86 fifo->align = align;
87 fifo->tx_limit = max_t(int, round_down(tx_limit, align), align);
88 fifo->open = 0;
89 fifo->open_limit = open_limit;
90 fifo->guard = size + align * open_limit;
91 fifo->capacity = capacity;
92 fifo->corrupt = 0;
93
94 return 0;
95}
96
97
98
99
100
101
102
103
104
105void dma_fifo_free(struct dma_fifo *fifo)
106{
107 struct dma_pending *pending, *next;
108
109 if (fifo->data == NULL)
110 return;
111
112 list_for_each_entry_safe(pending, next, &fifo->pending, link)
113 list_del_init(&pending->link);
114 kfree(fifo->data);
115 fifo->data = NULL;
116}
117
118
119
120
121
122void dma_fifo_reset(struct dma_fifo *fifo)
123{
124 struct dma_pending *pending, *next;
125
126 if (fifo->data == NULL)
127 return;
128
129 list_for_each_entry_safe(pending, next, &fifo->pending, link)
130 list_del_init(&pending->link);
131 fifo->in = 0;
132 fifo->out = 0;
133 fifo->done = 0;
134 fifo->avail = fifo->size;
135 fifo->open = 0;
136 fifo->corrupt = 0;
137}
138
139
140
141
142
143
144
145
146
147
148int dma_fifo_in(struct dma_fifo *fifo, const void *src, int n)
149{
150 int ofs, l;
151
152 if (fifo->data == NULL)
153 return -ENOENT;
154 if (fifo->corrupt)
155 return -ENXIO;
156
157 if (n > fifo->avail)
158 n = fifo->avail;
159 if (n <= 0)
160 return 0;
161
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);
166
167 if (FAIL(fifo, addr_check(fifo->done, fifo->in, fifo->in + n) ||
168 fifo->avail < n,
169 "fifo corrupt: in:%u out:%u done:%u n:%d avail:%d",
170 fifo->in, fifo->out, fifo->done, n, fifo->avail))
171 return -ENXIO;
172
173 fifo->in += n;
174 fifo->avail -= n;
175
176 df_trace("in:%u out:%u done:%u n:%d avail:%d", fifo->in, fifo->out,
177 fifo->done, n, fifo->avail);
178
179 return n;
180}
181
182
183
184
185
186
187
188
189
190
191int dma_fifo_out_pend(struct dma_fifo *fifo, struct dma_pending *pended)
192{
193 unsigned len, n, ofs, l, limit;
194
195 if (fifo->data == NULL)
196 return -ENOENT;
197 if (fifo->corrupt)
198 return -ENXIO;
199
200 pended->len = 0;
201 pended->data = NULL;
202 pended->out = fifo->out;
203
204 len = fifo->in - fifo->out;
205 if (!len)
206 return -ENODATA;
207 if (fifo->open == fifo->open_limit)
208 return -EAGAIN;
209
210 n = len;
211 ofs = fifo->out % fifo->capacity;
212 l = fifo->capacity - ofs;
213 limit = min_t(unsigned, l, fifo->tx_limit);
214 if (n > limit) {
215 n = limit;
216 fifo->out += limit;
217 } else if (ofs + n > fifo->guard) {
218 fifo->out += l;
219 fifo->in = fifo->out;
220 } else {
221 fifo->out += round_up(n, fifo->align);
222 fifo->in = fifo->out;
223 }
224
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);
227
228 pended->len = n;
229 pended->data = fifo->data + ofs;
230 pended->next = fifo->out;
231 list_add_tail(&pended->link, &fifo->pending);
232 ++fifo->open;
233
234 if (FAIL(fifo, fifo->open > fifo->open_limit,
235 "past open limit:%d (limit:%d)",
236 fifo->open, fifo->open_limit))
237 return -ENXIO;
238 if (FAIL(fifo, fifo->out & (fifo->align - 1),
239 "fifo out unaligned:%u (align:%u)",
240 fifo->out, fifo->align))
241 return -ENXIO;
242
243 return len - n;
244}
245
246
247
248
249
250
251int dma_fifo_out_complete(struct dma_fifo *fifo, struct dma_pending *complete)
252{
253 struct dma_pending *pending, *next, *tmp;
254
255 if (fifo->data == NULL)
256 return -ENOENT;
257 if (fifo->corrupt)
258 return -ENXIO;
259 if (list_empty(&fifo->pending) && fifo->open == 0)
260 return -EINVAL;
261
262 if (FAIL(fifo, list_empty(&fifo->pending) != (fifo->open == 0),
263 "pending list disagrees with open count:%d",
264 fifo->open))
265 return -ENXIO;
266
267 tmp = complete->data;
268 *tmp = *complete;
269 list_replace(&complete->link, &tmp->link);
270 dp_mark_completed(tmp);
271
272
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);
277 break;
278 }
279
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,
284 pending->next))
285 return -ENXIO;
286
287 list_del_init(&pending->link);
288 fifo->done = pending->next;
289 fifo->avail += pending->len;
290 --fifo->open;
291
292 df_trace("in: %u out: %u done: %u len: %u avail: %d", fifo->in,
293 fifo->out, fifo->done, pending->len, fifo->avail);
294 }
295
296 if (FAIL(fifo, fifo->open < 0, "open dma:%d < 0", fifo->open))
297 return -ENXIO;
298 if (FAIL(fifo, fifo->avail > fifo->size, "fifo avail:%d > size:%d",
299 fifo->avail, fifo->size))
300 return -ENXIO;
301
302 return 0;
303}
304