Branch data Line data Source code
1 : : /* SPDX-License-Identifier: (BSD-3-Clause OR LGPL-2.1) 2 : : * Copyright(c) 2010-2013 Intel Corporation. 3 : : * Copyright(c) 2013-2017 Wind River Systems, Inc. 4 : : */ 5 : : 6 : : #ifndef _RTE_AVP_FIFO_H_ 7 : : #define _RTE_AVP_FIFO_H_ 8 : : 9 : : #include "rte_avp_common.h" 10 : : 11 : : #ifdef __KERNEL__ 12 : : /* Write memory barrier for kernel compiles */ 13 : : #define AVP_WMB() smp_wmb() 14 : : /* Read memory barrier for kernel compiles */ 15 : : #define AVP_RMB() smp_rmb() 16 : : #else 17 : : /* Write memory barrier for userspace compiles */ 18 : : #define AVP_WMB() rte_wmb() 19 : : /* Read memory barrier for userspace compiles */ 20 : : #define AVP_RMB() rte_rmb() 21 : : #endif 22 : : 23 : : #ifndef __KERNEL__ 24 : : #include <rte_debug.h> 25 : : 26 : : #ifdef __cplusplus 27 : : extern "C" { 28 : : #endif 29 : : 30 : : /** 31 : : * Initializes the avp fifo structure 32 : : */ 33 : : static inline void 34 : : avp_fifo_init(struct rte_avp_fifo *fifo, unsigned int size) 35 : : { 36 : : /* Ensure size is power of 2 */ 37 : : if (size & (size - 1)) 38 : : rte_panic("AVP fifo size must be power of 2\n"); 39 : : 40 : : fifo->write = 0; 41 : : fifo->read = 0; 42 : : fifo->len = size; 43 : : fifo->elem_size = sizeof(void *); 44 : : } 45 : : #endif 46 : : 47 : : /** 48 : : * Adds num elements into the fifo. Return the number actually written 49 : : */ 50 : : static inline unsigned 51 : : avp_fifo_put(struct rte_avp_fifo *fifo, void **data, unsigned int num) 52 : : { 53 : : unsigned int i = 0; 54 : 0 : unsigned int fifo_write = fifo->write; 55 : 0 : unsigned int fifo_read = fifo->read; 56 : : unsigned int new_write = fifo_write; 57 : : 58 [ # # # # : 0 : for (i = 0; i < num; i++) { # # # # # # ] 59 : 0 : new_write = (new_write + 1) & (fifo->len - 1); 60 : : 61 [ # # # # : 0 : if (new_write == fifo_read) # # # # # # ] 62 : : break; 63 : 0 : fifo->buffer[fifo_write] = data[i]; 64 : : fifo_write = new_write; 65 : : } 66 : : AVP_WMB(); 67 [ # # # # ]: 0 : fifo->write = fifo_write; 68 : : return i; 69 : : } 70 : : 71 : : /** 72 : : * Get up to num elements from the fifo. Return the number actually read 73 : : */ 74 : : static inline unsigned int 75 : : avp_fifo_get(struct rte_avp_fifo *fifo, void **data, unsigned int num) 76 : : { 77 : : unsigned int i = 0; 78 : 0 : unsigned int new_read = fifo->read; 79 : 0 : unsigned int fifo_write = fifo->write; 80 : : 81 [ # # # # : 0 : if (new_read == fifo_write) # # # # # # # # ] 82 : : return 0; /* empty */ 83 : : 84 [ # # # # : 0 : for (i = 0; i < num; i++) { # # # # # # # # ] 85 [ # # # # : 0 : if (new_read == fifo_write) # # # # # # # # ] 86 : : break; 87 : : 88 : 0 : data[i] = fifo->buffer[new_read]; 89 : 0 : new_read = (new_read + 1) & (fifo->len - 1); 90 : : } 91 : : AVP_RMB(); 92 [ # # # # ]: 0 : fifo->read = new_read; 93 : 0 : return i; 94 : : } 95 : : 96 : : /** 97 : : * Get the num of elements in the fifo 98 : : */ 99 : : static inline unsigned int 100 : : avp_fifo_count(struct rte_avp_fifo *fifo) 101 : : { 102 [ # # # # ]: 0 : return (fifo->len + fifo->write - fifo->read) & (fifo->len - 1); 103 : : } 104 : : 105 : : /** 106 : : * Get the num of available elements in the fifo 107 : : */ 108 : : static inline unsigned int 109 : : avp_fifo_free_count(struct rte_avp_fifo *fifo) 110 : : { 111 [ # # # # : 0 : return (fifo->read - fifo->write - 1) & (fifo->len - 1); # # ] 112 : : } 113 : : 114 : : #ifdef __cplusplus 115 : : } 116 : : #endif 117 : : 118 : : #endif /* _RTE_AVP_FIFO_H_ */