Lines Matching +full:fifo +full:- +full:size

1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * A generic kernel FIFO implementation
12 * How to porting drivers to the new generic FIFO API:
14 * - Modify the declaration of the "struct kfifo *" object into a
15 * in-place "struct kfifo" object
16 * - Init the in-place object with kfifo_alloc() or kfifo_init()
17 * Note: The address of the in-place "struct kfifo" object must be
19 * - Replace the use of __kfifo_put into kfifo_in and __kfifo_get
21 * - Replace the use of kfifo_put into kfifo_in_spinlocked and kfifo_get
26 * - The formerly __kfifo_* functions are renamed into kfifo_*
31 * and one writer is using the fifo and no kfifo_reset() will be called.
62 #define __STRUCT_KFIFO(type, size, recsize, ptrtype) \ argument
65 type buf[((size < 2) || (size & (size - 1))) ? -1 : size]; \
68 #define STRUCT_KFIFO(type, size) \ argument
69 struct __STRUCT_KFIFO(type, size, 0, type)
85 #define STRUCT_KFIFO_REC_1(size) \ argument
86 struct __STRUCT_KFIFO(unsigned char, size, 1, void)
88 #define STRUCT_KFIFO_REC_2(size) \ argument
89 struct __STRUCT_KFIFO(unsigned char, size, 2, void)
98 * helper macro to distinguish between real in place fifo where the fifo
99 * array is a part of the structure and the fifo type where the array is
100 * outside of the fifo structure.
102 #define __is_kfifo_ptr(fifo) \ argument
103 (sizeof(*fifo) == sizeof(STRUCT_KFIFO_PTR(typeof(*(fifo)->type))))
106 * DECLARE_KFIFO_PTR - macro to declare a fifo pointer object
107 * @fifo: name of the declared fifo
108 * @type: type of the fifo elements
110 #define DECLARE_KFIFO_PTR(fifo, type) STRUCT_KFIFO_PTR(type) fifo argument
113 * DECLARE_KFIFO - macro to declare a fifo object
114 * @fifo: name of the declared fifo
115 * @type: type of the fifo elements
116 * @size: the number of elements in the fifo, this must be a power of 2
118 #define DECLARE_KFIFO(fifo, type, size) STRUCT_KFIFO(type, size) fifo argument
121 * INIT_KFIFO - Initialize a fifo declared by DECLARE_KFIFO
122 * @fifo: name of the declared fifo datatype
124 #define INIT_KFIFO(fifo) \ argument
126 typeof(&(fifo)) __tmp = &(fifo); \
127 struct __kfifo *__kfifo = &__tmp->kfifo; \
128 __kfifo->in = 0; \
129 __kfifo->out = 0; \
130 __kfifo->mask = __is_kfifo_ptr(__tmp) ? 0 : ARRAY_SIZE(__tmp->buf) - 1;\
131 __kfifo->esize = sizeof(*__tmp->buf); \
132 __kfifo->data = __is_kfifo_ptr(__tmp) ? NULL : __tmp->buf; \
136 * DEFINE_KFIFO - macro to define and initialize a fifo
137 * @fifo: name of the declared fifo datatype
138 * @type: type of the fifo elements
139 * @size: the number of elements in the fifo, this must be a power of 2
141 * Note: the macro can be used for global and local fifo data type variables.
143 #define DEFINE_KFIFO(fifo, type, size) \ argument
144 DECLARE_KFIFO(fifo, type, size) = \
145 (typeof(fifo)) { \
150 .mask = __is_kfifo_ptr(&(fifo)) ? \
152 ARRAY_SIZE((fifo).buf) - 1, \
153 .esize = sizeof(*(fifo).buf), \
154 .data = __is_kfifo_ptr(&(fifo)) ? \
156 (fifo).buf, \
175 * kfifo_initialized - Check if the fifo is initialized
176 * @fifo: address of the fifo to check
178 * Return %true if fifo is initialized, otherwise %false.
179 * Assumes the fifo was 0 before.
181 #define kfifo_initialized(fifo) ((fifo)->kfifo.mask) argument
184 * kfifo_esize - returns the size of the element managed by the fifo
185 * @fifo: address of the fifo to be used
187 #define kfifo_esize(fifo) ((fifo)->kfifo.esize) argument
190 * kfifo_recsize - returns the size of the record length field
191 * @fifo: address of the fifo to be used
193 #define kfifo_recsize(fifo) (sizeof(*(fifo)->rectype)) argument
196 * kfifo_size - returns the size of the fifo in elements
197 * @fifo: address of the fifo to be used
199 #define kfifo_size(fifo) ((fifo)->kfifo.mask + 1) argument
202 * kfifo_reset - removes the entire fifo content
203 * @fifo: address of the fifo to be used
206 * fifo is exclusived locked or when it is secured that no other thread is
207 * accessing the fifo.
209 #define kfifo_reset(fifo) \ argument
211 typeof((fifo) + 1) __tmp = (fifo); \
212 __tmp->kfifo.in = __tmp->kfifo.out = 0; \
216 * kfifo_reset_out - skip fifo content
217 * @fifo: address of the fifo to be used
223 #define kfifo_reset_out(fifo) \ argument
225 typeof((fifo) + 1) __tmp = (fifo); \
226 __tmp->kfifo.out = __tmp->kfifo.in; \
230 * kfifo_len - returns the number of used elements in the fifo
231 * @fifo: address of the fifo to be used
233 #define kfifo_len(fifo) \ argument
235 typeof((fifo) + 1) __tmpl = (fifo); \
236 __tmpl->kfifo.in - __tmpl->kfifo.out; \
240 * kfifo_is_empty - returns true if the fifo is empty
241 * @fifo: address of the fifo to be used
243 #define kfifo_is_empty(fifo) \ argument
245 typeof((fifo) + 1) __tmpq = (fifo); \
246 __tmpq->kfifo.in == __tmpq->kfifo.out; \
250 * kfifo_is_empty_spinlocked - returns true if the fifo is empty using
252 * @fifo: address of the fifo to be used
255 #define kfifo_is_empty_spinlocked(fifo, lock) \ argument
260 __ret = kfifo_is_empty(fifo); \
266 * kfifo_is_empty_spinlocked_noirqsave - returns true if the fifo is empty
268 * @fifo: address of the fifo to be used
271 #define kfifo_is_empty_spinlocked_noirqsave(fifo, lock) \ argument
275 __ret = kfifo_is_empty(fifo); \
281 * kfifo_is_full - returns true if the fifo is full
282 * @fifo: address of the fifo to be used
284 #define kfifo_is_full(fifo) \ argument
286 typeof((fifo) + 1) __tmpq = (fifo); \
287 kfifo_len(__tmpq) > __tmpq->kfifo.mask; \
291 * kfifo_avail - returns the number of unused elements in the fifo
292 * @fifo: address of the fifo to be used
294 #define kfifo_avail(fifo) \ argument
297 typeof((fifo) + 1) __tmpq = (fifo); \
298 const size_t __recsize = sizeof(*__tmpq->rectype); \
299 unsigned int __avail = kfifo_size(__tmpq) - kfifo_len(__tmpq); \
301 __kfifo_max_r(__avail - __recsize, __recsize)) : \
307 * kfifo_skip - skip output data
308 * @fifo: address of the fifo to be used
310 #define kfifo_skip(fifo) \ argument
312 typeof((fifo) + 1) __tmp = (fifo); \
313 const size_t __recsize = sizeof(*__tmp->rectype); \
314 struct __kfifo *__kfifo = &__tmp->kfifo; \
318 __kfifo->out++; \
322 * kfifo_peek_len - gets the size of the next fifo record
323 * @fifo: address of the fifo to be used
325 * This function returns the size of the next fifo record in number of bytes.
327 #define kfifo_peek_len(fifo) \ argument
330 typeof((fifo) + 1) __tmp = (fifo); \
331 const size_t __recsize = sizeof(*__tmp->rectype); \
332 struct __kfifo *__kfifo = &__tmp->kfifo; \
333 (!__recsize) ? kfifo_len(__tmp) * sizeof(*__tmp->type) : \
339 * kfifo_alloc - dynamically allocates a new fifo buffer
340 * @fifo: pointer to the fifo
341 * @size: the number of elements in the fifo, this must be a power of 2
344 * This macro dynamically allocates a new fifo buffer.
346 * The number of elements will be rounded-up to a power of 2.
347 * The fifo will be release with kfifo_free().
350 #define kfifo_alloc(fifo, size, gfp_mask) \ argument
353 typeof((fifo) + 1) __tmp = (fifo); \
354 struct __kfifo *__kfifo = &__tmp->kfifo; \
356 __kfifo_alloc(__kfifo, size, sizeof(*__tmp->type), gfp_mask) : \
357 -EINVAL; \
362 * kfifo_free - frees the fifo
363 * @fifo: the fifo to be freed
365 #define kfifo_free(fifo) \ argument
367 typeof((fifo) + 1) __tmp = (fifo); \
368 struct __kfifo *__kfifo = &__tmp->kfifo; \
374 * kfifo_init - initialize a fifo using a preallocated buffer
375 * @fifo: the fifo to assign the buffer
377 * @size: the size of the internal buffer, this have to be a power of 2
379 * This macro initializes a fifo using a preallocated buffer.
381 * The number of elements will be rounded-up to a power of 2.
384 #define kfifo_init(fifo, buffer, size) \ argument
386 typeof((fifo) + 1) __tmp = (fifo); \
387 struct __kfifo *__kfifo = &__tmp->kfifo; \
389 __kfifo_init(__kfifo, buffer, size, sizeof(*__tmp->type)) : \
390 -EINVAL; \
394 * kfifo_put - put data into the fifo
395 * @fifo: address of the fifo to be used
398 * This macro copies the given value into the fifo.
399 * It returns 0 if the fifo was full. Otherwise it returns the number
405 #define kfifo_put(fifo, val) \ argument
407 typeof((fifo) + 1) __tmp = (fifo); \
408 typeof(*__tmp->const_type) __val = (val); \
410 size_t __recsize = sizeof(*__tmp->rectype); \
411 struct __kfifo *__kfifo = &__tmp->kfifo; \
419 ((typeof(__tmp->type))__kfifo->data) : \
420 (__tmp->buf) \
421 )[__kfifo->in & __tmp->kfifo.mask] = \
422 *(typeof(__tmp->type))&__val; \
424 __kfifo->in++; \
431 * kfifo_get - get data from the fifo
432 * @fifo: address of the fifo to be used
435 * This macro reads the data from the fifo.
436 * It returns 0 if the fifo was empty. Otherwise it returns the number
442 #define kfifo_get(fifo, val) \ argument
445 typeof((fifo) + 1) __tmp = (fifo); \
446 typeof(__tmp->ptr) __val = (val); \
448 const size_t __recsize = sizeof(*__tmp->rectype); \
449 struct __kfifo *__kfifo = &__tmp->kfifo; \
456 *(typeof(__tmp->type))__val = \
458 ((typeof(__tmp->type))__kfifo->data) : \
459 (__tmp->buf) \
460 )[__kfifo->out & __tmp->kfifo.mask]; \
462 __kfifo->out++; \
470 * kfifo_peek - get data from the fifo without removing
471 * @fifo: address of the fifo to be used
474 * This reads the data from the fifo without removing it from the fifo.
475 * It returns 0 if the fifo was empty. Otherwise it returns the number
481 #define kfifo_peek(fifo, val) \ argument
484 typeof((fifo) + 1) __tmp = (fifo); \
485 typeof(__tmp->ptr) __val = (val); \
487 const size_t __recsize = sizeof(*__tmp->rectype); \
488 struct __kfifo *__kfifo = &__tmp->kfifo; \
495 *(typeof(__tmp->type))__val = \
497 ((typeof(__tmp->type))__kfifo->data) : \
498 (__tmp->buf) \
499 )[__kfifo->out & __tmp->kfifo.mask]; \
508 * kfifo_in - put data into the fifo
509 * @fifo: address of the fifo to be used
513 * This macro copies the given buffer into the fifo and returns the
519 #define kfifo_in(fifo, buf, n) \ argument
521 typeof((fifo) + 1) __tmp = (fifo); \
522 typeof(__tmp->ptr_const) __buf = (buf); \
524 const size_t __recsize = sizeof(*__tmp->rectype); \
525 struct __kfifo *__kfifo = &__tmp->kfifo; \
532 * kfifo_in_spinlocked - put data into the fifo using a spinlock for locking
533 * @fifo: address of the fifo to be used
538 * This macro copies the given values buffer into the fifo and returns the
541 #define kfifo_in_spinlocked(fifo, buf, n, lock) \ argument
546 __ret = kfifo_in(fifo, buf, n); \
552 * kfifo_in_spinlocked_noirqsave - put data into fifo using a spinlock for
554 * @fifo: address of the fifo to be used
562 #define kfifo_in_spinlocked_noirqsave(fifo, buf, n, lock) \ argument
566 __ret = kfifo_in(fifo, buf, n); \
572 #define kfifo_in_locked(fifo, buf, n, lock) \ argument
573 kfifo_in_spinlocked(fifo, buf, n, lock)
576 * kfifo_out - get data from the fifo
577 * @fifo: address of the fifo to be used
581 * This macro get some data from the fifo and return the numbers of elements
587 #define kfifo_out(fifo, buf, n) \ argument
590 typeof((fifo) + 1) __tmp = (fifo); \
591 typeof(__tmp->ptr) __buf = (buf); \
593 const size_t __recsize = sizeof(*__tmp->rectype); \
594 struct __kfifo *__kfifo = &__tmp->kfifo; \
602 * kfifo_out_spinlocked - get data from the fifo using a spinlock for locking
603 * @fifo: address of the fifo to be used
608 * This macro get the data from the fifo and return the numbers of elements
611 #define kfifo_out_spinlocked(fifo, buf, n, lock) \ argument
617 __ret = kfifo_out(fifo, buf, n); \
624 * kfifo_out_spinlocked_noirqsave - get data from the fifo using a spinlock
626 * @fifo: address of the fifo to be used
634 #define kfifo_out_spinlocked_noirqsave(fifo, buf, n, lock) \ argument
639 __ret = kfifo_out(fifo, buf, n); \
646 #define kfifo_out_locked(fifo, buf, n, lock) \ argument
647 kfifo_out_spinlocked(fifo, buf, n, lock)
650 * kfifo_from_user - puts some data from user space into the fifo
651 * @fifo: address of the fifo to be used
657 * fifo, depending of the available space and returns -EFAULT/0.
662 #define kfifo_from_user(fifo, from, len, copied) \ argument
665 typeof((fifo) + 1) __tmp = (fifo); \
669 const size_t __recsize = sizeof(*__tmp->rectype); \
670 struct __kfifo *__kfifo = &__tmp->kfifo; \
678 * kfifo_to_user - copies data from the fifo into user space
679 * @fifo: address of the fifo to be used
681 * @len: the size of the destination buffer
684 * This macro copies at most @len bytes from the fifo into the
685 * @to buffer and returns -EFAULT/0.
690 #define kfifo_to_user(fifo, to, len, copied) \ argument
693 typeof((fifo) + 1) __tmp = (fifo); \
697 const size_t __recsize = sizeof(*__tmp->rectype); \
698 struct __kfifo *__kfifo = &__tmp->kfifo; \
706 * kfifo_dma_in_prepare - setup a scatterlist for DMA input
707 * @fifo: address of the fifo to be used
718 #define kfifo_dma_in_prepare(fifo, sgl, nents, len) \ argument
720 typeof((fifo) + 1) __tmp = (fifo); \
724 const size_t __recsize = sizeof(*__tmp->rectype); \
725 struct __kfifo *__kfifo = &__tmp->kfifo; \
732 * kfifo_dma_in_finish - finish a DMA IN operation
733 * @fifo: address of the fifo to be used
742 #define kfifo_dma_in_finish(fifo, len) \ argument
744 typeof((fifo) + 1) __tmp = (fifo); \
746 const size_t __recsize = sizeof(*__tmp->rectype); \
747 struct __kfifo *__kfifo = &__tmp->kfifo; \
751 __kfifo->in += __len / sizeof(*__tmp->type); \
755 * kfifo_dma_out_prepare - setup a scatterlist for DMA output
756 * @fifo: address of the fifo to be used
769 #define kfifo_dma_out_prepare(fifo, sgl, nents, len) \ argument
771 typeof((fifo) + 1) __tmp = (fifo); \
775 const size_t __recsize = sizeof(*__tmp->rectype); \
776 struct __kfifo *__kfifo = &__tmp->kfifo; \
783 * kfifo_dma_out_finish - finish a DMA OUT operation
784 * @fifo: address of the fifo to be used
793 #define kfifo_dma_out_finish(fifo, len) \ argument
795 typeof((fifo) + 1) __tmp = (fifo); \
797 const size_t __recsize = sizeof(*__tmp->rectype); \
798 struct __kfifo *__kfifo = &__tmp->kfifo; \
802 __kfifo->out += __len / sizeof(*__tmp->type); \
806 * kfifo_out_peek - gets some data from the fifo
807 * @fifo: address of the fifo to be used
811 * This macro get the data from the fifo and return the numbers of elements
812 * copied. The data is not removed from the fifo.
817 #define kfifo_out_peek(fifo, buf, n) \ argument
820 typeof((fifo) + 1) __tmp = (fifo); \
821 typeof(__tmp->ptr) __buf = (buf); \
823 const size_t __recsize = sizeof(*__tmp->rectype); \
824 struct __kfifo *__kfifo = &__tmp->kfifo; \
831 extern int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
834 extern void __kfifo_free(struct __kfifo *fifo);
836 extern int __kfifo_init(struct __kfifo *fifo, void *buffer,
837 unsigned int size, size_t esize);
839 extern unsigned int __kfifo_in(struct __kfifo *fifo,
842 extern unsigned int __kfifo_out(struct __kfifo *fifo,
845 extern int __kfifo_from_user(struct __kfifo *fifo,
848 extern int __kfifo_to_user(struct __kfifo *fifo,
851 extern unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
854 extern unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
857 extern unsigned int __kfifo_out_peek(struct __kfifo *fifo,
860 extern unsigned int __kfifo_in_r(struct __kfifo *fifo,
863 extern unsigned int __kfifo_out_r(struct __kfifo *fifo,
866 extern int __kfifo_from_user_r(struct __kfifo *fifo,
870 extern int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
873 extern unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
876 extern void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
879 extern unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
882 extern void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize);
884 extern unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize);
886 extern void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize);
888 extern unsigned int __kfifo_out_peek_r(struct __kfifo *fifo,