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/module.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 if (!is_power_of_2(size))
46 size = rounddown_pow_of_two(size);
58 fifo->data = kmalloc(size * esize, gfp_mask);
64 fifo->mask = size - 1;
68 EXPORT_SYMBOL(__kfifo_alloc);
70 void __kfifo_free(struct __kfifo *fifo)
79 EXPORT_SYMBOL(__kfifo_free);
81 int __kfifo_init(struct __kfifo *fifo, void *buffer,
82 unsigned int size, size_t esize)
86 if (!is_power_of_2(size))
87 size = rounddown_pow_of_two(size);
98 fifo->mask = size - 1;
102 EXPORT_SYMBOL(__kfifo_init);
104 static void kfifo_copy_in(struct __kfifo *fifo, const void *src,
105 unsigned int len, unsigned int off)
107 unsigned int size = fifo->mask + 1;
108 unsigned int esize = fifo->esize;
117 l = min(len, size - off);
119 memcpy(fifo->data + off, src, l);
120 memcpy(fifo->data, src + l, len - l);
122 * make sure that the data in the fifo is up to date before
123 * incrementing the fifo->in index counter
128 unsigned int __kfifo_in(struct __kfifo *fifo,
129 const void *buf, unsigned int len)
133 l = kfifo_unused(fifo);
137 kfifo_copy_in(fifo, buf, len, fifo->in);
141 EXPORT_SYMBOL(__kfifo_in);
143 static void kfifo_copy_out(struct __kfifo *fifo, void *dst,
144 unsigned int len, unsigned int off)
146 unsigned int size = fifo->mask + 1;
147 unsigned int esize = fifo->esize;
156 l = min(len, size - off);
158 memcpy(dst, fifo->data + off, l);
159 memcpy(dst + l, fifo->data, len - l);
161 * make sure that the data is copied before
162 * incrementing the fifo->out index counter
167 unsigned int __kfifo_out_peek(struct __kfifo *fifo,
168 void *buf, unsigned int len)
172 l = fifo->in - fifo->out;
176 kfifo_copy_out(fifo, buf, len, fifo->out);
179 EXPORT_SYMBOL(__kfifo_out_peek);
181 unsigned int __kfifo_out(struct __kfifo *fifo,
182 void *buf, unsigned int len)
184 len = __kfifo_out_peek(fifo, buf, len);
188 EXPORT_SYMBOL(__kfifo_out);
190 static unsigned long kfifo_copy_from_user(struct __kfifo *fifo,
191 const void __user *from, unsigned int len, unsigned int off,
192 unsigned int *copied)
194 unsigned int size = fifo->mask + 1;
195 unsigned int esize = fifo->esize;
205 l = min(len, size - off);
207 ret = copy_from_user(fifo->data + off, from, l);
209 ret = DIV_ROUND_UP(ret + len - l, esize);
211 ret = copy_from_user(fifo->data, from + l, len - l);
213 ret = DIV_ROUND_UP(ret, esize);
216 * make sure that the data in the fifo is up to date before
217 * incrementing the fifo->in index counter
221 /* return the number of elements which are not copied */
225 int __kfifo_from_user(struct __kfifo *fifo, const void __user *from,
226 unsigned long len, unsigned int *copied)
230 unsigned int esize = fifo->esize;
236 l = kfifo_unused(fifo);
240 ret = kfifo_copy_from_user(fifo, from, len, fifo->in, copied);
249 EXPORT_SYMBOL(__kfifo_from_user);
251 static unsigned long kfifo_copy_to_user(struct __kfifo *fifo, void __user *to,
252 unsigned int len, unsigned int off, unsigned int *copied)
256 unsigned int size = fifo->mask + 1;
257 unsigned int esize = fifo->esize;
265 l = min(len, size - off);
267 ret = copy_to_user(to, fifo->data + off, l);
269 ret = DIV_ROUND_UP(ret + len - l, esize);
271 ret = copy_to_user(to + l, fifo->data, len - l);
273 ret = DIV_ROUND_UP(ret, esize);
276 * make sure that the data is copied before
277 * incrementing the fifo->out index counter
281 /* return the number of elements which are not copied */
285 int __kfifo_to_user(struct __kfifo *fifo, void __user *to,
286 unsigned long len, unsigned int *copied)
290 unsigned int esize = fifo->esize;
296 l = fifo->in - fifo->out;
299 ret = kfifo_copy_to_user(fifo, to, len, fifo->out, copied);
308 EXPORT_SYMBOL(__kfifo_to_user);
310 static int setup_sgl_buf(struct scatterlist *sgl, void *buf,
311 int nents, unsigned int len)
325 page = virt_to_page(buf);
326 off = offset_in_page(buf);
329 while (len >= l + PAGE_SIZE - off) {
334 npage = virt_to_page(buf);
335 if (page_to_phys(page) != page_to_phys(npage) - l) {
337 sg_set_page(sgl++, page, l - off, off);
346 sg_set_page(sgl++, page, len, off);
350 static unsigned int setup_sgl(struct __kfifo *fifo, struct scatterlist *sgl,
351 int nents, unsigned int len, unsigned int off)
353 unsigned int size = fifo->mask + 1;
354 unsigned int esize = fifo->esize;
364 l = min(len, size - off);
366 n = setup_sgl_buf(sgl, fifo->data + off, nents, l);
367 n += setup_sgl_buf(sgl + n, fifo->data, nents - n, len - l);
370 sg_mark_end(sgl + n - 1);
374 unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
375 struct scatterlist *sgl, int nents, unsigned int len)
379 l = kfifo_unused(fifo);
383 return setup_sgl(fifo, sgl, nents, len, fifo->in);
385 EXPORT_SYMBOL(__kfifo_dma_in_prepare);
387 unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
388 struct scatterlist *sgl, int nents, unsigned int len)
392 l = fifo->in - fifo->out;
396 return setup_sgl(fifo, sgl, nents, len, fifo->out);
398 EXPORT_SYMBOL(__kfifo_dma_out_prepare);
400 unsigned int __kfifo_max_r(unsigned int len, size_t recsize)
402 unsigned int max = (1 << (recsize << 3)) - 1;
409 #define __KFIFO_PEEK(data, out, mask) \
410 ((data)[(out) & (mask)])
412 * __kfifo_peek_n internal helper function for determinate the length of
413 * the next record in the fifo
415 static unsigned int __kfifo_peek_n(struct __kfifo *fifo, size_t recsize)
418 unsigned int mask = fifo->mask;
419 unsigned char *data = fifo->data;
421 l = __KFIFO_PEEK(data, fifo->out, mask);
424 l |= __KFIFO_PEEK(data, fifo->out + 1, mask) << 8;
429 #define __KFIFO_POKE(data, in, mask, val) \
431 (data)[(in) & (mask)] = (unsigned char)(val) \
435 * __kfifo_poke_n internal helper function for storeing the length of
436 * the record into the fifo
438 static void __kfifo_poke_n(struct __kfifo *fifo, unsigned int n, size_t recsize)
440 unsigned int mask = fifo->mask;
441 unsigned char *data = fifo->data;
443 __KFIFO_POKE(data, fifo->in, mask, n);
446 __KFIFO_POKE(data, fifo->in + 1, mask, n >> 8);
449 unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize)
451 return __kfifo_peek_n(fifo, recsize);
453 EXPORT_SYMBOL(__kfifo_len_r);
455 unsigned int __kfifo_in_r(struct __kfifo *fifo, const void *buf,
456 unsigned int len, size_t recsize)
458 if (len + recsize > kfifo_unused(fifo))
461 __kfifo_poke_n(fifo, len, recsize);
463 kfifo_copy_in(fifo, buf, len, fifo->in + recsize);
464 fifo->in += len + recsize;
467 EXPORT_SYMBOL(__kfifo_in_r);
469 static unsigned int kfifo_out_copy_r(struct __kfifo *fifo,
470 void *buf, unsigned int len, size_t recsize, unsigned int *n)
472 *n = __kfifo_peek_n(fifo, recsize);
477 kfifo_copy_out(fifo, buf, len, fifo->out + recsize);
481 unsigned int __kfifo_out_peek_r(struct __kfifo *fifo, void *buf,
482 unsigned int len, size_t recsize)
486 if (fifo->in == fifo->out)
489 return kfifo_out_copy_r(fifo, buf, len, recsize, &n);
491 EXPORT_SYMBOL(__kfifo_out_peek_r);
493 unsigned int __kfifo_out_r(struct __kfifo *fifo, void *buf,
494 unsigned int len, size_t recsize)
498 if (fifo->in == fifo->out)
501 len = kfifo_out_copy_r(fifo, buf, len, recsize, &n);
502 fifo->out += n + recsize;
505 EXPORT_SYMBOL(__kfifo_out_r);
507 int __kfifo_from_user_r(struct __kfifo *fifo, const void __user *from,
508 unsigned long len, unsigned int *copied, size_t recsize)
512 len = __kfifo_max_r(len, recsize);
514 if (len + recsize > kfifo_unused(fifo)) {
519 __kfifo_poke_n(fifo, len, recsize);
521 ret = kfifo_copy_from_user(fifo, from, len, fifo->in + recsize, copied);
526 fifo->in += len + recsize;
529 EXPORT_SYMBOL(__kfifo_from_user_r);
531 int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
532 unsigned long len, unsigned int *copied, size_t recsize)
537 if (fifo->in == fifo->out) {
542 n = __kfifo_peek_n(fifo, recsize);
546 ret = kfifo_copy_to_user(fifo, to, len, fifo->out + recsize, copied);
551 fifo->out += n + recsize;
554 EXPORT_SYMBOL(__kfifo_to_user_r);
556 unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
557 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize)
562 len = __kfifo_max_r(len, recsize);
564 if (len + recsize > kfifo_unused(fifo))
567 return setup_sgl(fifo, sgl, nents, len, fifo->in + recsize);
569 EXPORT_SYMBOL(__kfifo_dma_in_prepare_r);
571 void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
572 unsigned int len, size_t recsize)
574 len = __kfifo_max_r(len, recsize);
575 __kfifo_poke_n(fifo, len, recsize);
576 fifo->in += len + recsize;
578 EXPORT_SYMBOL(__kfifo_dma_in_finish_r);
580 unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
581 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize)
586 len = __kfifo_max_r(len, recsize);
588 if (len + recsize > fifo->in - fifo->out)
591 return setup_sgl(fifo, sgl, nents, len, fifo->out + recsize);
593 EXPORT_SYMBOL(__kfifo_dma_out_prepare_r);
595 void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize)
599 len = __kfifo_peek_n(fifo, recsize);
600 fifo->out += len + recsize;
602 EXPORT_SYMBOL(__kfifo_dma_out_finish_r);