1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * A generic kernel FIFO implementation
8 #include <linux/dma-mapping.h>
10 #include <linux/export.h>
11 #include <linux/kfifo.h>
12 #include <linux/log2.h>
13 #include <linux/scatterlist.h>
14 #include <linux/slab.h>
15 #include <linux/uaccess.h>
18 * internal helper to calculate the unused elements in a fifo
20 static inline unsigned int kfifo_unused(struct __kfifo *fifo)
22 return (fifo->mask + 1) - (fifo->in - fifo->out);
25 int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
26 size_t esize, gfp_t gfp_mask)
29 * round up to the next power of 2, since our 'let the indices
30 * wrap' technique works only in this case.
32 size = roundup_pow_of_two(size);
44 fifo->data = kmalloc_array(esize, size, gfp_mask);
50 fifo->mask = size - 1;
54 EXPORT_SYMBOL(__kfifo_alloc);
56 void __kfifo_free(struct __kfifo *fifo)
65 EXPORT_SYMBOL(__kfifo_free);
67 int __kfifo_init(struct __kfifo *fifo, void *buffer,
68 unsigned int size, size_t esize)
72 if (!is_power_of_2(size))
73 size = rounddown_pow_of_two(size);
84 fifo->mask = size - 1;
88 EXPORT_SYMBOL(__kfifo_init);
90 static void kfifo_copy_in(struct __kfifo *fifo, const void *src,
91 unsigned int len, unsigned int off)
93 unsigned int size = fifo->mask + 1;
94 unsigned int esize = fifo->esize;
103 l = min(len, size - off);
105 memcpy(fifo->data + off, src, l);
106 memcpy(fifo->data, src + l, len - l);
108 * make sure that the data in the fifo is up to date before
109 * incrementing the fifo->in index counter
114 unsigned int __kfifo_in(struct __kfifo *fifo,
115 const void *buf, unsigned int len)
119 l = kfifo_unused(fifo);
123 kfifo_copy_in(fifo, buf, len, fifo->in);
127 EXPORT_SYMBOL(__kfifo_in);
129 static void kfifo_copy_out(struct __kfifo *fifo, void *dst,
130 unsigned int len, unsigned int off)
132 unsigned int size = fifo->mask + 1;
133 unsigned int esize = fifo->esize;
142 l = min(len, size - off);
144 memcpy(dst, fifo->data + off, l);
145 memcpy(dst + l, fifo->data, len - l);
147 * make sure that the data is copied before
148 * incrementing the fifo->out index counter
153 unsigned int __kfifo_out_peek(struct __kfifo *fifo,
154 void *buf, unsigned int len)
158 l = fifo->in - fifo->out;
162 kfifo_copy_out(fifo, buf, len, fifo->out);
165 EXPORT_SYMBOL(__kfifo_out_peek);
167 unsigned int __kfifo_out_linear(struct __kfifo *fifo,
168 unsigned int *tail, unsigned int n)
170 unsigned int size = fifo->mask + 1;
171 unsigned int off = fifo->out & fifo->mask;
176 return min3(n, fifo->in - fifo->out, size - off);
178 EXPORT_SYMBOL(__kfifo_out_linear);
180 unsigned int __kfifo_out(struct __kfifo *fifo,
181 void *buf, unsigned int len)
183 len = __kfifo_out_peek(fifo, buf, 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;
204 l = min(len, size - off);
206 ret = copy_from_user(fifo->data + off, from, l);
208 ret = DIV_ROUND_UP(ret + len - l, esize);
210 ret = copy_from_user(fifo->data, from + l, len - l);
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
219 *copied = len - ret * esize;
220 /* return the number of elements which are not copied */
224 int __kfifo_from_user(struct __kfifo *fifo, const void __user *from,
225 unsigned long len, unsigned int *copied)
229 unsigned int esize = fifo->esize;
235 l = kfifo_unused(fifo);
239 ret = kfifo_copy_from_user(fifo, from, len, fifo->in, copied);
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)
255 unsigned int size = fifo->mask + 1;
256 unsigned int esize = fifo->esize;
264 l = min(len, size - off);
266 ret = copy_to_user(to, fifo->data + off, l);
268 ret = DIV_ROUND_UP(ret + len - l, esize);
270 ret = copy_to_user(to + l, fifo->data, len - l);
272 ret = DIV_ROUND_UP(ret, esize);
275 * make sure that the data is copied before
276 * incrementing the fifo->out index counter
279 *copied = len - ret * esize;
280 /* return the number of elements which are not copied */
284 int __kfifo_to_user(struct __kfifo *fifo, void __user *to,
285 unsigned long len, unsigned int *copied)
289 unsigned int esize = fifo->esize;
295 l = fifo->in - fifo->out;
298 ret = kfifo_copy_to_user(fifo, to, len, fifo->out, copied);
307 EXPORT_SYMBOL(__kfifo_to_user);
309 static unsigned int setup_sgl_buf(struct __kfifo *fifo, struct scatterlist *sgl,
310 unsigned int data_offset, int nents,
311 unsigned int len, dma_addr_t dma)
313 const void *buf = fifo->data + data_offset;
318 sg_set_buf(sgl, buf, len);
320 if (dma != DMA_MAPPING_ERROR) {
321 sg_dma_address(sgl) = dma + data_offset;
322 sg_dma_len(sgl) = len;
328 static unsigned int setup_sgl(struct __kfifo *fifo, struct scatterlist *sgl,
329 int nents, unsigned int len, unsigned int off, dma_addr_t dma)
331 unsigned int size = fifo->mask + 1;
332 unsigned int esize = fifo->esize;
333 unsigned int len_to_end;
342 len_to_end = min(len, size - off);
344 n = setup_sgl_buf(fifo, sgl, off, nents, len_to_end, dma);
345 n += setup_sgl_buf(fifo, sgl + n, 0, nents - n, len - len_to_end, dma);
350 unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
351 struct scatterlist *sgl, int nents, unsigned int len,
356 l = kfifo_unused(fifo);
360 return setup_sgl(fifo, sgl, nents, len, fifo->in, dma);
362 EXPORT_SYMBOL(__kfifo_dma_in_prepare);
364 unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
365 struct scatterlist *sgl, int nents, unsigned int len,
370 l = fifo->in - fifo->out;
374 return setup_sgl(fifo, sgl, nents, len, fifo->out, dma);
376 EXPORT_SYMBOL(__kfifo_dma_out_prepare);
378 unsigned int __kfifo_max_r(unsigned int len, size_t recsize)
380 unsigned int max = (1 << (recsize << 3)) - 1;
386 EXPORT_SYMBOL(__kfifo_max_r);
388 #define __KFIFO_PEEK(data, out, mask) \
389 ((data)[(out) & (mask)])
391 * __kfifo_peek_n internal helper function for determinate the length of
392 * the next record in the fifo
394 static unsigned int __kfifo_peek_n(struct __kfifo *fifo, size_t recsize)
397 unsigned int mask = fifo->mask;
398 unsigned char *data = fifo->data;
400 l = __KFIFO_PEEK(data, fifo->out, mask);
403 l |= __KFIFO_PEEK(data, fifo->out + 1, mask) << 8;
408 #define __KFIFO_POKE(data, in, mask, val) \
410 (data)[(in) & (mask)] = (unsigned char)(val) \
414 * __kfifo_poke_n internal helper function for storing the length of
415 * the record into the fifo
417 static void __kfifo_poke_n(struct __kfifo *fifo, unsigned int n, size_t recsize)
419 unsigned int mask = fifo->mask;
420 unsigned char *data = fifo->data;
422 __KFIFO_POKE(data, fifo->in, mask, n);
425 __KFIFO_POKE(data, fifo->in + 1, mask, n >> 8);
428 unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize)
430 return __kfifo_peek_n(fifo, recsize);
432 EXPORT_SYMBOL(__kfifo_len_r);
434 unsigned int __kfifo_in_r(struct __kfifo *fifo, const void *buf,
435 unsigned int len, size_t recsize)
437 if (len + recsize > kfifo_unused(fifo))
440 __kfifo_poke_n(fifo, len, recsize);
442 kfifo_copy_in(fifo, buf, len, fifo->in + recsize);
443 fifo->in += len + recsize;
446 EXPORT_SYMBOL(__kfifo_in_r);
448 static unsigned int kfifo_out_copy_r(struct __kfifo *fifo,
449 void *buf, unsigned int len, size_t recsize, unsigned int *n)
451 *n = __kfifo_peek_n(fifo, recsize);
456 kfifo_copy_out(fifo, buf, len, fifo->out + recsize);
460 unsigned int __kfifo_out_peek_r(struct __kfifo *fifo, void *buf,
461 unsigned int len, size_t recsize)
465 if (fifo->in == fifo->out)
468 return kfifo_out_copy_r(fifo, buf, len, recsize, &n);
470 EXPORT_SYMBOL(__kfifo_out_peek_r);
472 unsigned int __kfifo_out_linear_r(struct __kfifo *fifo,
473 unsigned int *tail, unsigned int n, size_t recsize)
475 if (fifo->in == fifo->out)
479 *tail = fifo->out + recsize;
481 return min(n, __kfifo_peek_n(fifo, recsize));
483 EXPORT_SYMBOL(__kfifo_out_linear_r);
485 unsigned int __kfifo_out_r(struct __kfifo *fifo, void *buf,
486 unsigned int len, size_t recsize)
490 if (fifo->in == fifo->out)
493 len = kfifo_out_copy_r(fifo, buf, len, recsize, &n);
494 fifo->out += n + recsize;
497 EXPORT_SYMBOL(__kfifo_out_r);
499 void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize)
503 n = __kfifo_peek_n(fifo, recsize);
504 fifo->out += n + recsize;
506 EXPORT_SYMBOL(__kfifo_skip_r);
508 int __kfifo_from_user_r(struct __kfifo *fifo, const void __user *from,
509 unsigned long len, unsigned int *copied, size_t recsize)
513 len = __kfifo_max_r(len, recsize);
515 if (len + recsize > kfifo_unused(fifo)) {
520 __kfifo_poke_n(fifo, len, recsize);
522 ret = kfifo_copy_from_user(fifo, from, len, fifo->in + recsize, copied);
527 fifo->in += len + recsize;
530 EXPORT_SYMBOL(__kfifo_from_user_r);
532 int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
533 unsigned long len, unsigned int *copied, size_t recsize)
538 if (fifo->in == fifo->out) {
543 n = __kfifo_peek_n(fifo, recsize);
547 ret = kfifo_copy_to_user(fifo, to, len, fifo->out + recsize, copied);
552 fifo->out += n + recsize;
555 EXPORT_SYMBOL(__kfifo_to_user_r);
557 unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
558 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize,
563 len = __kfifo_max_r(len, recsize);
565 if (len + recsize > kfifo_unused(fifo))
568 return setup_sgl(fifo, sgl, nents, len, fifo->in + recsize, dma);
570 EXPORT_SYMBOL(__kfifo_dma_in_prepare_r);
572 void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
573 unsigned int len, size_t recsize)
575 len = __kfifo_max_r(len, recsize);
576 __kfifo_poke_n(fifo, len, recsize);
577 fifo->in += len + recsize;
579 EXPORT_SYMBOL(__kfifo_dma_in_finish_r);
581 unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
582 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize,
587 len = __kfifo_max_r(len, recsize);
589 if (len + recsize > fifo->in - fifo->out)
592 return setup_sgl(fifo, sgl, nents, len, fifo->out + recsize, dma);
594 EXPORT_SYMBOL(__kfifo_dma_out_prepare_r);