1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
8 * @internal when c11 memory model enabled use c11 atomic memory barrier.
9 * when under non c11 memory model use rte_smp_* memory barrier.
12 * Pointer to the source data.
14 * Pointer to the destination data.
18 #ifdef RTE_USE_C11_MEM_MODEL
19 #define __KNI_LOAD_ACQUIRE(src) ({ \
20 __atomic_load_n((src), __ATOMIC_ACQUIRE); \
22 #define __KNI_STORE_RELEASE(dst, value) do { \
23 __atomic_store_n((dst), value, __ATOMIC_RELEASE); \
26 #define __KNI_LOAD_ACQUIRE(src) ({ \
27 typeof (*(src)) val = *(src); \
31 #define __KNI_STORE_RELEASE(dst, value) do { \
38 * Initializes the kni fifo structure
41 kni_fifo_init(struct rte_kni_fifo *fifo, unsigned size)
43 /* Ensure size is power of 2 */
44 if (size & (size - 1))
45 rte_panic("KNI fifo size must be power of 2\n");
50 fifo->elem_size = sizeof(void *);
54 * Adds num elements into the fifo. Return the number actually written
56 static inline unsigned
57 kni_fifo_put(struct rte_kni_fifo *fifo, void **data, unsigned num)
60 unsigned fifo_write = fifo->write;
61 unsigned new_write = fifo_write;
62 unsigned fifo_read = __KNI_LOAD_ACQUIRE(&fifo->read);
64 for (i = 0; i < num; i++) {
65 new_write = (new_write + 1) & (fifo->len - 1);
67 if (new_write == fifo_read)
69 fifo->buffer[fifo_write] = data[i];
70 fifo_write = new_write;
72 __KNI_STORE_RELEASE(&fifo->write, fifo_write);
77 * Get up to num elements from the fifo. Return the number actually read
79 static inline unsigned
80 kni_fifo_get(struct rte_kni_fifo *fifo, void **data, unsigned num)
83 unsigned new_read = fifo->read;
84 unsigned fifo_write = __KNI_LOAD_ACQUIRE(&fifo->write);
86 for (i = 0; i < num; i++) {
87 if (new_read == fifo_write)
90 data[i] = fifo->buffer[new_read];
91 new_read = (new_read + 1) & (fifo->len - 1);
93 __KNI_STORE_RELEASE(&fifo->read, new_read);
98 * Get the num of elements in the fifo
100 static inline uint32_t
101 kni_fifo_count(struct rte_kni_fifo *fifo)
103 unsigned fifo_write = __KNI_LOAD_ACQUIRE(&fifo->write);
104 unsigned fifo_read = __KNI_LOAD_ACQUIRE(&fifo->read);
105 return (fifo->len + fifo_write - fifo_read) & (fifo->len - 1);
109 * Get the num of available elements in the fifo
111 static inline uint32_t
112 kni_fifo_free_count(struct rte_kni_fifo *fifo)
114 uint32_t fifo_write = __KNI_LOAD_ACQUIRE(&fifo->write);
115 uint32_t fifo_read = __KNI_LOAD_ACQUIRE(&fifo->read);
116 return (fifo_read - fifo_write - 1) & (fifo->len - 1);