xref: /f-stack/dpdk/drivers/net/avp/rte_avp_fifo.h (revision d30ea906)
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 __cplusplus
12 extern "C" {
13 #endif
14 
15 #ifdef __KERNEL__
16 /* Write memory barrier for kernel compiles */
17 #define AVP_WMB() smp_wmb()
18 /* Read memory barrier for kernel compiles */
19 #define AVP_RMB() smp_rmb()
20 #else
21 /* Write memory barrier for userspace compiles */
22 #define AVP_WMB() rte_wmb()
23 /* Read memory barrier for userspace compiles */
24 #define AVP_RMB() rte_rmb()
25 #endif
26 
27 #ifndef __KERNEL__
28 #include <rte_debug.h>
29 
30 /**
31  * Initializes the avp fifo structure
32  */
33 static inline void
avp_fifo_init(struct rte_avp_fifo * fifo,unsigned int size)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
avp_fifo_put(struct rte_avp_fifo * fifo,void ** data,unsigned int num)51 avp_fifo_put(struct rte_avp_fifo *fifo, void **data, unsigned int num)
52 {
53 	unsigned int i = 0;
54 	unsigned int fifo_write = fifo->write;
55 	unsigned int fifo_read = fifo->read;
56 	unsigned int new_write = fifo_write;
57 
58 	for (i = 0; i < num; i++) {
59 		new_write = (new_write + 1) & (fifo->len - 1);
60 
61 		if (new_write == fifo_read)
62 			break;
63 		fifo->buffer[fifo_write] = data[i];
64 		fifo_write = new_write;
65 	}
66 	AVP_WMB();
67 	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
avp_fifo_get(struct rte_avp_fifo * fifo,void ** data,unsigned int num)75 avp_fifo_get(struct rte_avp_fifo *fifo, void **data, unsigned int num)
76 {
77 	unsigned int i = 0;
78 	unsigned int new_read = fifo->read;
79 	unsigned int fifo_write = fifo->write;
80 
81 	if (new_read == fifo_write)
82 		return 0; /* empty */
83 
84 	for (i = 0; i < num; i++) {
85 		if (new_read == fifo_write)
86 			break;
87 
88 		data[i] = fifo->buffer[new_read];
89 		new_read = (new_read + 1) & (fifo->len - 1);
90 	}
91 	AVP_RMB();
92 	fifo->read = new_read;
93 	return i;
94 }
95 
96 /**
97  * Get the num of elements in the fifo
98  */
99 static inline unsigned int
avp_fifo_count(struct rte_avp_fifo * fifo)100 avp_fifo_count(struct rte_avp_fifo *fifo)
101 {
102 	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
avp_fifo_free_count(struct rte_avp_fifo * fifo)109 avp_fifo_free_count(struct rte_avp_fifo *fifo)
110 {
111 	return (fifo->read - fifo->write - 1) & (fifo->len - 1);
112 }
113 
114 #ifdef __cplusplus
115 }
116 #endif
117 
118 #endif /* _RTE_AVP_FIFO_H_ */
119