Linux 4.14.199
[linux/fpc-iii.git] / lib / kfifo.c
bloba94227c5555109bfcfc14ab9a750cd0761680470
1 /*
2 * A generic kernel FIFO implementation
4 * Copyright (C) 2009/2010 Stefani Seibold <stefani@seibold.net>
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
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/kernel.h>
23 #include <linux/export.h>
24 #include <linux/slab.h>
25 #include <linux/err.h>
26 #include <linux/log2.h>
27 #include <linux/uaccess.h>
28 #include <linux/kfifo.h>
31 * internal helper to calculate the unused elements in a fifo
33 static inline unsigned int kfifo_unused(struct __kfifo *fifo)
35 return (fifo->mask + 1) - (fifo->in - fifo->out);
38 int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
39 size_t esize, gfp_t gfp_mask)
42 * round down to the next power of 2, since our 'let the indices
43 * wrap' technique works only in this case.
45 size = roundup_pow_of_two(size);
47 fifo->in = 0;
48 fifo->out = 0;
49 fifo->esize = esize;
51 if (size < 2) {
52 fifo->data = NULL;
53 fifo->mask = 0;
54 return -EINVAL;
57 fifo->data = kmalloc(size * esize, gfp_mask);
59 if (!fifo->data) {
60 fifo->mask = 0;
61 return -ENOMEM;
63 fifo->mask = size - 1;
65 return 0;
67 EXPORT_SYMBOL(__kfifo_alloc);
69 void __kfifo_free(struct __kfifo *fifo)
71 kfree(fifo->data);
72 fifo->in = 0;
73 fifo->out = 0;
74 fifo->esize = 0;
75 fifo->data = NULL;
76 fifo->mask = 0;
78 EXPORT_SYMBOL(__kfifo_free);
80 int __kfifo_init(struct __kfifo *fifo, void *buffer,
81 unsigned int size, size_t esize)
83 size /= esize;
85 if (!is_power_of_2(size))
86 size = rounddown_pow_of_two(size);
88 fifo->in = 0;
89 fifo->out = 0;
90 fifo->esize = esize;
91 fifo->data = buffer;
93 if (size < 2) {
94 fifo->mask = 0;
95 return -EINVAL;
97 fifo->mask = size - 1;
99 return 0;
101 EXPORT_SYMBOL(__kfifo_init);
103 static void kfifo_copy_in(struct __kfifo *fifo, const void *src,
104 unsigned int len, unsigned int off)
106 unsigned int size = fifo->mask + 1;
107 unsigned int esize = fifo->esize;
108 unsigned int l;
110 off &= fifo->mask;
111 if (esize != 1) {
112 off *= esize;
113 size *= esize;
114 len *= esize;
116 l = min(len, size - off);
118 memcpy(fifo->data + off, src, l);
119 memcpy(fifo->data, src + l, len - l);
121 * make sure that the data in the fifo is up to date before
122 * incrementing the fifo->in index counter
124 smp_wmb();
127 unsigned int __kfifo_in(struct __kfifo *fifo,
128 const void *buf, unsigned int len)
130 unsigned int l;
132 l = kfifo_unused(fifo);
133 if (len > l)
134 len = l;
136 kfifo_copy_in(fifo, buf, len, fifo->in);
137 fifo->in += len;
138 return len;
140 EXPORT_SYMBOL(__kfifo_in);
142 static void kfifo_copy_out(struct __kfifo *fifo, void *dst,
143 unsigned int len, unsigned int off)
145 unsigned int size = fifo->mask + 1;
146 unsigned int esize = fifo->esize;
147 unsigned int l;
149 off &= fifo->mask;
150 if (esize != 1) {
151 off *= esize;
152 size *= esize;
153 len *= esize;
155 l = min(len, size - off);
157 memcpy(dst, fifo->data + off, l);
158 memcpy(dst + l, fifo->data, len - l);
160 * make sure that the data is copied before
161 * incrementing the fifo->out index counter
163 smp_wmb();
166 unsigned int __kfifo_out_peek(struct __kfifo *fifo,
167 void *buf, unsigned int len)
169 unsigned int l;
171 l = fifo->in - fifo->out;
172 if (len > l)
173 len = l;
175 kfifo_copy_out(fifo, buf, len, fifo->out);
176 return len;
178 EXPORT_SYMBOL(__kfifo_out_peek);
180 unsigned int __kfifo_out(struct __kfifo *fifo,
181 void *buf, unsigned int len)
183 len = __kfifo_out_peek(fifo, buf, len);
184 fifo->out += len;
185 return len;
187 EXPORT_SYMBOL(__kfifo_out);
189 static unsigned long kfifo_copy_from_user(struct __kfifo *fifo,
190 const void __user *from, unsigned int len, unsigned int off,
191 unsigned int *copied)
193 unsigned int size = fifo->mask + 1;
194 unsigned int esize = fifo->esize;
195 unsigned int l;
196 unsigned long ret;
198 off &= fifo->mask;
199 if (esize != 1) {
200 off *= esize;
201 size *= esize;
202 len *= esize;
204 l = min(len, size - off);
206 ret = copy_from_user(fifo->data + off, from, l);
207 if (unlikely(ret))
208 ret = DIV_ROUND_UP(ret + len - l, esize);
209 else {
210 ret = copy_from_user(fifo->data, from + l, len - l);
211 if (unlikely(ret))
212 ret = DIV_ROUND_UP(ret, esize);
215 * make sure that the data in the fifo is up to date before
216 * incrementing the fifo->in index counter
218 smp_wmb();
219 *copied = len - ret * esize;
220 /* return the number of elements which are not copied */
221 return ret;
224 int __kfifo_from_user(struct __kfifo *fifo, const void __user *from,
225 unsigned long len, unsigned int *copied)
227 unsigned int l;
228 unsigned long ret;
229 unsigned int esize = fifo->esize;
230 int err;
232 if (esize != 1)
233 len /= esize;
235 l = kfifo_unused(fifo);
236 if (len > l)
237 len = l;
239 ret = kfifo_copy_from_user(fifo, from, len, fifo->in, copied);
240 if (unlikely(ret)) {
241 len -= ret;
242 err = -EFAULT;
243 } else
244 err = 0;
245 fifo->in += len;
246 return err;
248 EXPORT_SYMBOL(__kfifo_from_user);
250 static unsigned long kfifo_copy_to_user(struct __kfifo *fifo, void __user *to,
251 unsigned int len, unsigned int off, unsigned int *copied)
253 unsigned int l;
254 unsigned long ret;
255 unsigned int size = fifo->mask + 1;
256 unsigned int esize = fifo->esize;
258 off &= fifo->mask;
259 if (esize != 1) {
260 off *= esize;
261 size *= esize;
262 len *= esize;
264 l = min(len, size - off);
266 ret = copy_to_user(to, fifo->data + off, l);
267 if (unlikely(ret))
268 ret = DIV_ROUND_UP(ret + len - l, esize);
269 else {
270 ret = copy_to_user(to + l, fifo->data, len - l);
271 if (unlikely(ret))
272 ret = DIV_ROUND_UP(ret, esize);
275 * make sure that the data is copied before
276 * incrementing the fifo->out index counter
278 smp_wmb();
279 *copied = len - ret * esize;
280 /* return the number of elements which are not copied */
281 return ret;
284 int __kfifo_to_user(struct __kfifo *fifo, void __user *to,
285 unsigned long len, unsigned int *copied)
287 unsigned int l;
288 unsigned long ret;
289 unsigned int esize = fifo->esize;
290 int err;
292 if (esize != 1)
293 len /= esize;
295 l = fifo->in - fifo->out;
296 if (len > l)
297 len = l;
298 ret = kfifo_copy_to_user(fifo, to, len, fifo->out, copied);
299 if (unlikely(ret)) {
300 len -= ret;
301 err = -EFAULT;
302 } else
303 err = 0;
304 fifo->out += len;
305 return err;
307 EXPORT_SYMBOL(__kfifo_to_user);
309 static int setup_sgl_buf(struct scatterlist *sgl, void *buf,
310 int nents, unsigned int len)
312 int n;
313 unsigned int l;
314 unsigned int off;
315 struct page *page;
317 if (!nents)
318 return 0;
320 if (!len)
321 return 0;
323 n = 0;
324 page = virt_to_page(buf);
325 off = offset_in_page(buf);
326 l = 0;
328 while (len >= l + PAGE_SIZE - off) {
329 struct page *npage;
331 l += PAGE_SIZE;
332 buf += PAGE_SIZE;
333 npage = virt_to_page(buf);
334 if (page_to_phys(page) != page_to_phys(npage) - l) {
335 sg_set_page(sgl, page, l - off, off);
336 sgl = sg_next(sgl);
337 if (++n == nents || sgl == NULL)
338 return n;
339 page = npage;
340 len -= l - off;
341 l = off = 0;
344 sg_set_page(sgl, page, len, off);
345 return n + 1;
348 static unsigned int setup_sgl(struct __kfifo *fifo, struct scatterlist *sgl,
349 int nents, unsigned int len, unsigned int off)
351 unsigned int size = fifo->mask + 1;
352 unsigned int esize = fifo->esize;
353 unsigned int l;
354 unsigned int n;
356 off &= fifo->mask;
357 if (esize != 1) {
358 off *= esize;
359 size *= esize;
360 len *= esize;
362 l = min(len, size - off);
364 n = setup_sgl_buf(sgl, fifo->data + off, nents, l);
365 n += setup_sgl_buf(sgl + n, fifo->data, nents - n, len - l);
367 return n;
370 unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
371 struct scatterlist *sgl, int nents, unsigned int len)
373 unsigned int l;
375 l = kfifo_unused(fifo);
376 if (len > l)
377 len = l;
379 return setup_sgl(fifo, sgl, nents, len, fifo->in);
381 EXPORT_SYMBOL(__kfifo_dma_in_prepare);
383 unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
384 struct scatterlist *sgl, int nents, unsigned int len)
386 unsigned int l;
388 l = fifo->in - fifo->out;
389 if (len > l)
390 len = l;
392 return setup_sgl(fifo, sgl, nents, len, fifo->out);
394 EXPORT_SYMBOL(__kfifo_dma_out_prepare);
396 unsigned int __kfifo_max_r(unsigned int len, size_t recsize)
398 unsigned int max = (1 << (recsize << 3)) - 1;
400 if (len > max)
401 return max;
402 return len;
404 EXPORT_SYMBOL(__kfifo_max_r);
406 #define __KFIFO_PEEK(data, out, mask) \
407 ((data)[(out) & (mask)])
409 * __kfifo_peek_n internal helper function for determinate the length of
410 * the next record in the fifo
412 static unsigned int __kfifo_peek_n(struct __kfifo *fifo, size_t recsize)
414 unsigned int l;
415 unsigned int mask = fifo->mask;
416 unsigned char *data = fifo->data;
418 l = __KFIFO_PEEK(data, fifo->out, mask);
420 if (--recsize)
421 l |= __KFIFO_PEEK(data, fifo->out + 1, mask) << 8;
423 return l;
426 #define __KFIFO_POKE(data, in, mask, val) \
428 (data)[(in) & (mask)] = (unsigned char)(val) \
432 * __kfifo_poke_n internal helper function for storeing the length of
433 * the record into the fifo
435 static void __kfifo_poke_n(struct __kfifo *fifo, unsigned int n, size_t recsize)
437 unsigned int mask = fifo->mask;
438 unsigned char *data = fifo->data;
440 __KFIFO_POKE(data, fifo->in, mask, n);
442 if (recsize > 1)
443 __KFIFO_POKE(data, fifo->in + 1, mask, n >> 8);
446 unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize)
448 return __kfifo_peek_n(fifo, recsize);
450 EXPORT_SYMBOL(__kfifo_len_r);
452 unsigned int __kfifo_in_r(struct __kfifo *fifo, const void *buf,
453 unsigned int len, size_t recsize)
455 if (len + recsize > kfifo_unused(fifo))
456 return 0;
458 __kfifo_poke_n(fifo, len, recsize);
460 kfifo_copy_in(fifo, buf, len, fifo->in + recsize);
461 fifo->in += len + recsize;
462 return len;
464 EXPORT_SYMBOL(__kfifo_in_r);
466 static unsigned int kfifo_out_copy_r(struct __kfifo *fifo,
467 void *buf, unsigned int len, size_t recsize, unsigned int *n)
469 *n = __kfifo_peek_n(fifo, recsize);
471 if (len > *n)
472 len = *n;
474 kfifo_copy_out(fifo, buf, len, fifo->out + recsize);
475 return len;
478 unsigned int __kfifo_out_peek_r(struct __kfifo *fifo, void *buf,
479 unsigned int len, size_t recsize)
481 unsigned int n;
483 if (fifo->in == fifo->out)
484 return 0;
486 return kfifo_out_copy_r(fifo, buf, len, recsize, &n);
488 EXPORT_SYMBOL(__kfifo_out_peek_r);
490 unsigned int __kfifo_out_r(struct __kfifo *fifo, void *buf,
491 unsigned int len, size_t recsize)
493 unsigned int n;
495 if (fifo->in == fifo->out)
496 return 0;
498 len = kfifo_out_copy_r(fifo, buf, len, recsize, &n);
499 fifo->out += n + recsize;
500 return len;
502 EXPORT_SYMBOL(__kfifo_out_r);
504 void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize)
506 unsigned int n;
508 n = __kfifo_peek_n(fifo, recsize);
509 fifo->out += n + recsize;
511 EXPORT_SYMBOL(__kfifo_skip_r);
513 int __kfifo_from_user_r(struct __kfifo *fifo, const void __user *from,
514 unsigned long len, unsigned int *copied, size_t recsize)
516 unsigned long ret;
518 len = __kfifo_max_r(len, recsize);
520 if (len + recsize > kfifo_unused(fifo)) {
521 *copied = 0;
522 return 0;
525 __kfifo_poke_n(fifo, len, recsize);
527 ret = kfifo_copy_from_user(fifo, from, len, fifo->in + recsize, copied);
528 if (unlikely(ret)) {
529 *copied = 0;
530 return -EFAULT;
532 fifo->in += len + recsize;
533 return 0;
535 EXPORT_SYMBOL(__kfifo_from_user_r);
537 int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
538 unsigned long len, unsigned int *copied, size_t recsize)
540 unsigned long ret;
541 unsigned int n;
543 if (fifo->in == fifo->out) {
544 *copied = 0;
545 return 0;
548 n = __kfifo_peek_n(fifo, recsize);
549 if (len > n)
550 len = n;
552 ret = kfifo_copy_to_user(fifo, to, len, fifo->out + recsize, copied);
553 if (unlikely(ret)) {
554 *copied = 0;
555 return -EFAULT;
557 fifo->out += n + recsize;
558 return 0;
560 EXPORT_SYMBOL(__kfifo_to_user_r);
562 unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
563 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize)
565 BUG_ON(!nents);
567 len = __kfifo_max_r(len, recsize);
569 if (len + recsize > kfifo_unused(fifo))
570 return 0;
572 return setup_sgl(fifo, sgl, nents, len, fifo->in + recsize);
574 EXPORT_SYMBOL(__kfifo_dma_in_prepare_r);
576 void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
577 unsigned int len, size_t recsize)
579 len = __kfifo_max_r(len, recsize);
580 __kfifo_poke_n(fifo, len, recsize);
581 fifo->in += len + recsize;
583 EXPORT_SYMBOL(__kfifo_dma_in_finish_r);
585 unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
586 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize)
588 BUG_ON(!nents);
590 len = __kfifo_max_r(len, recsize);
592 if (len + recsize > fifo->in - fifo->out)
593 return 0;
595 return setup_sgl(fifo, sgl, nents, len, fifo->out + recsize);
597 EXPORT_SYMBOL(__kfifo_dma_out_prepare_r);
599 void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize)
601 unsigned int len;
603 len = __kfifo_peek_n(fifo, recsize);
604 fifo->out += len + recsize;
606 EXPORT_SYMBOL(__kfifo_dma_out_finish_r);