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
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 * private helper fn to determine if check is in open interval (lo,hi)
37 */
38static bool addr_check(unsigned check, unsigned lo, unsigned hi)
39{
40	return check - (lo + 1) < (hi - 1) - lo;
41}
42
43/**
44 * dma_fifo_init: initialize the fifo to a valid but inoperative state
45 * @fifo: address of in-place "struct dma_fifo" object
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 * 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),
58 *         must be power of 2
59 * @tx_limit: maximum # of bytes transmissable 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()
63 *
64 * The 'apparent' size will be rounded up to next greater aligned size.
65 * Returns 0 if no error, otherwise an error code
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 * dma_fifo_free - frees the fifo
99 * @fifo: address of in-place "struct dma_fifo" to free
100 *
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.
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 * dma_fifo_reset - dumps the fifo contents and reinits for reuse
120 * @fifo: address of in-place "struct dma_fifo" to reset
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 * 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
144 *
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.
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 * 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.
187 *
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.
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 * 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
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	/* 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);
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