Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C), 2025, Wuxi Stars Micro System Technologies Co., Ltd.
3 : : */
4 : :
5 : : #ifndef SXE2_OSAL_H
6 : : #define SXE2_OSAL_H
7 : :
8 : : #include <string.h>
9 : : #include <stdint.h>
10 : : #include <stdarg.h>
11 : : #include <inttypes.h>
12 : : #include <stdbool.h>
13 : :
14 : : #include <rte_common.h>
15 : : #include <rte_cycles.h>
16 : : #include <rte_malloc.h>
17 : : #include <rte_ether.h>
18 : : #include <rte_version.h>
19 : : #include <rte_bitops.h>
20 : :
21 : : #ifndef __BITS_PER_LONG
22 : : #define __BITS_PER_LONG (__SIZEOF_LONG__ * 8)
23 : : #endif
24 : : #define BIT_WORD(nr) ((nr) / __BITS_PER_LONG)
25 : : #define BIT_MASK(nr) (1UL << ((nr) % __BITS_PER_LONG))
26 : :
27 : : #ifndef TAILQ_FOREACH_SAFE
28 : : #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
29 : : for ((var) = TAILQ_FIRST((head)); \
30 : : (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
31 : : (var) = (tvar))
32 : : #endif
33 : :
34 : : #define upper_32_bits(n) ((uint32_t)(((n) >> 16) >> 16))
35 : : #define lower_32_bits(n) ((uint32_t)((n) & 0xffffffff))
36 : :
37 : : #ifndef SXE2_DIV_ROUND_UP
38 : : #define SXE2_DIV_ROUND_UP(n, d) \
39 : : (((n) + (typeof(n))(d) - (typeof(n))1) / (typeof(n))(d))
40 : : #endif
41 : :
42 : : enum sxe2_itr_idx {
43 : : SXE2_ITR_IDX_0 = 0,
44 : : SXE2_ITR_IDX_1,
45 : : SXE2_ITR_IDX_2,
46 : : SXE2_ITR_IDX_NONE,
47 : : };
48 : :
49 : : #define SXE2_ETH_ALEN 6
50 : :
51 : : #define SXE2_BITS_PER_BYTE 8
52 : : #define BITS_TO_LONGS(nr) SXE2_DIV_ROUND_UP(nr, SXE2_BITS_PER_BYTE * sizeof(unsigned long))
53 : : #define BITS_TO_U32(nr) SXE2_DIV_ROUND_UP(nr, 32)
54 : :
55 : : #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (__BITS_PER_LONG - 1)))
56 : :
57 : : #define DECLARE_BITMAP(name, bits) \
58 : : unsigned long name[BITS_TO_LONGS(bits)]
59 : : #define BITMAP_TYPE unsigned long
60 : :
61 : 0 : static inline void sxe2_set_bit(uint32_t nr, unsigned long *addr)
62 : : {
63 [ # # # # : 0 : addr[nr / __BITS_PER_LONG] |= 1UL << (nr % __BITS_PER_LONG);
# # # # #
# # # # #
# # # # #
# ]
64 : 0 : }
65 : :
66 : 0 : static inline void sxe2_clear_bit(uint32_t nr, unsigned long *addr)
67 : : {
68 : 0 : addr[nr / __BITS_PER_LONG] &= ~(1UL << (nr % __BITS_PER_LONG));
69 : : }
70 : :
71 : 0 : static inline uint32_t sxe2_test_bit(uint32_t nr, const unsigned long *addr)
72 : : {
73 [ # # # # : 0 : return 1UL & (addr[BIT_WORD(nr)] >> (nr & (__BITS_PER_LONG-1)));
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
74 : : }
75 : :
76 : 0 : static inline uint32_t sxe2_bitmap_weight(const unsigned long *src, uint32_t nbits)
77 : : {
78 : 0 : uint32_t cnt = 0;
79 : 0 : uint16_t i;
80 : :
81 [ # # # # : 0 : for (i = 0; i < nbits; i++) {
# # # # #
# ]
82 [ # # # # : 0 : if (sxe2_test_bit(i, src))
# # # # #
# ]
83 : 0 : cnt++;
84 : : }
85 [ # # # # : 0 : return cnt;
# # # # #
# ]
86 : : }
87 : :
88 : : static inline bool sxe2_bitmap_empty(const unsigned long *src, uint32_t nbits)
89 : : {
90 : : uint16_t i;
91 : :
92 : : for (i = 0; i < nbits; i++) {
93 : : if (sxe2_test_bit(i, src))
94 : : return false;
95 : : }
96 : : return true;
97 : : }
98 : :
99 : 0 : static inline void sxe2_bitmap_zero(unsigned long *dst, uint32_t nbits)
100 : : {
101 : 0 : uint32_t len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
102 [ # # ]: 0 : memset(dst, 0, len);
103 : : }
104 : :
105 : 0 : static inline void sxe2_bitmap_copy(unsigned long *dst, const unsigned long *src,
106 : : uint32_t nbits)
107 : : {
108 : 0 : uint32_t len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
109 [ # # ]: 0 : memcpy(dst, src, len);
110 : 0 : }
111 : :
112 : : #define sxe2_small_const_nbits(nbits) \
113 : : ((nbits) && (nbits) <= __BITS_PER_LONG && (nbits) > 0)
114 : :
115 : : #define SXE2_BITMAP_LAST_WORD_MASK(nbits) \
116 : : (((nbits) % __BITS_PER_LONG) ? \
117 : : ((1UL << ((nbits) % __BITS_PER_LONG)) - 1UL) : ~0UL)
118 : :
119 : : static inline bool
120 : : __sxe2_bitmap_equal_generic(const unsigned long *src1,
121 : : const unsigned long *src2, uint32_t nbits)
122 : : {
123 : : uint32_t i;
124 : : uint32_t words = nbits / __BITS_PER_LONG;
125 : : uint32_t trailing = nbits % __BITS_PER_LONG;
126 : :
127 : : for (i = 0; i < words; i++) {
128 : : if (src1[i] != src2[i])
129 : : return false;
130 : : }
131 : :
132 : : if (trailing) {
133 : : unsigned long mask = (1UL << trailing) - 1UL;
134 : : if ((src1[words] & mask) != (src2[words] & mask))
135 : : return false;
136 : : }
137 : :
138 : : return true;
139 : : }
140 : :
141 : : static inline bool
142 : 0 : sxe2_bitmap_equal(const unsigned long *src1,
143 : : const unsigned long *src2, uint32_t nbits)
144 : : {
145 : 0 : if (sxe2_small_const_nbits(nbits))
146 [ # # ]: 0 : return !((*src1 ^ *src2) & SXE2_BITMAP_LAST_WORD_MASK(nbits));
147 : :
148 : : if (__rte_constant(nbits & 7) && (nbits % 8 == 0))
149 : : return !memcmp(src1, src2, nbits / 8);
150 : :
151 : : return __sxe2_bitmap_equal_generic(src1, src2, nbits);
152 : : }
153 : :
154 : : #endif /* SXE2_OSAL_H */
|