Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation
3 : : */
4 : :
5 : : #ifndef _RTE_ATOMIC_X86_H_
6 : : #define _RTE_ATOMIC_X86_H_
7 : :
8 : : #ifdef __cplusplus
9 : : extern "C" {
10 : : #endif
11 : :
12 : : #include <stdint.h>
13 : : #include <rte_common.h>
14 : : #include <rte_config.h>
15 : : #include <emmintrin.h>
16 : : #include "generic/rte_atomic.h"
17 : :
18 : : #if RTE_MAX_LCORE == 1
19 : : #define MPLOCKED /**< No need to insert MP lock prefix. */
20 : : #else
21 : : #define MPLOCKED "lock ; " /**< Insert MP lock prefix. */
22 : : #endif
23 : :
24 : : #define rte_mb() _mm_mfence()
25 : :
26 : : #define rte_wmb() _mm_sfence()
27 : :
28 : : #define rte_rmb() _mm_lfence()
29 : :
30 : : #define rte_smp_wmb() rte_compiler_barrier()
31 : :
32 : : #define rte_smp_rmb() rte_compiler_barrier()
33 : :
34 : : /*
35 : : * From Intel Software Development Manual; Vol 3;
36 : : * 8.2.2 Memory Ordering in P6 and More Recent Processor Families:
37 : : * ...
38 : : * . Reads are not reordered with other reads.
39 : : * . Writes are not reordered with older reads.
40 : : * . Writes to memory are not reordered with other writes,
41 : : * with the following exceptions:
42 : : * . streaming stores (writes) executed with the non-temporal move
43 : : * instructions (MOVNTI, MOVNTQ, MOVNTDQ, MOVNTPS, and MOVNTPD); and
44 : : * . string operations (see Section 8.2.4.1).
45 : : * ...
46 : : * . Reads may be reordered with older writes to different locations but not
47 : : * with older writes to the same location.
48 : : * . Reads or writes cannot be reordered with I/O instructions,
49 : : * locked instructions, or serializing instructions.
50 : : * . Reads cannot pass earlier LFENCE and MFENCE instructions.
51 : : * . Writes ... cannot pass earlier LFENCE, SFENCE, and MFENCE instructions.
52 : : * . LFENCE instructions cannot pass earlier reads.
53 : : * . SFENCE instructions cannot pass earlier writes ...
54 : : * . MFENCE instructions cannot pass earlier reads, writes ...
55 : : *
56 : : * As pointed by Java guys, that makes possible to use lock-prefixed
57 : : * instructions to get the same effect as mfence and on most modern HW
58 : : * that gives a better performance then using mfence:
59 : : * https://shipilev.net/blog/2014/on-the-fence-with-dependencies/
60 : : * Basic idea is to use lock prefixed add with some dummy memory location
61 : : * as the destination. From their experiments 128B(2 cache lines) below
62 : : * current stack pointer looks like a good candidate.
63 : : * So below we use that technique for rte_smp_mb() implementation.
64 : : */
65 : :
66 : : static __rte_always_inline void
67 : : rte_smp_mb(void)
68 : : {
69 : : #ifdef RTE_TOOLCHAIN_MSVC
70 : : _mm_mfence();
71 : : #else
72 : : #ifdef RTE_ARCH_I686
73 : : asm volatile("lock addl $0, -128(%%esp); " ::: "memory");
74 : : #else
75 : 965374 : asm volatile("lock addl $0, -128(%%rsp); " ::: "memory");
76 : : #endif
77 : : #endif
78 : 0 : }
79 : :
80 : : #define rte_io_mb() rte_mb()
81 : :
82 : : #define rte_io_wmb() rte_compiler_barrier()
83 : :
84 : : #define rte_io_rmb() rte_compiler_barrier()
85 : :
86 : : /**
87 : : * Synchronization fence between threads based on the specified memory order.
88 : : *
89 : : * On x86 the __rte_atomic_thread_fence(rte_memory_order_seq_cst) generates full 'mfence'
90 : : * which is quite expensive. The optimized implementation of rte_smp_mb is
91 : : * used instead.
92 : : */
93 : : static __rte_always_inline void
94 : : rte_atomic_thread_fence(rte_memory_order memorder)
95 : : {
96 : : if (memorder == rte_memory_order_seq_cst)
97 : : rte_smp_mb();
98 : : else
99 [ # # # # : 0 : __rte_atomic_thread_fence(memorder);
# # # # #
# # # # #
# # # # #
# # # #
# ]
100 : 0 : }
101 : :
102 : : #ifndef RTE_TOOLCHAIN_MSVC
103 : :
104 : : /*------------------------- 16 bit atomic operations -------------------------*/
105 : :
106 : : #ifndef RTE_FORCE_INTRINSICS
107 : : static inline int
108 : : rte_atomic16_cmpset(volatile uint16_t *dst, uint16_t exp, uint16_t src)
109 : : {
110 : : uint8_t res;
111 : :
112 : 1 : asm volatile(
113 : : MPLOCKED
114 : : "cmpxchgw %[src], %[dst];"
115 : : "sete %[res];"
116 : : : [res] "=a" (res), /* output */
117 : : [dst] "=m" (*dst)
118 : : : [src] "r" (src), /* input */
119 : : "a" (exp),
120 : : "m" (*dst)
121 : : : "memory"); /* no-clobber list */
122 [ + - ]: 1 : return res;
123 : : }
124 : :
125 : : static inline uint16_t
126 : : rte_atomic16_exchange(volatile uint16_t *dst, uint16_t val)
127 : : {
128 : 1000000 : asm volatile(
129 : : MPLOCKED
130 : : "xchgw %0, %1;"
131 : : : "=r" (val), "=m" (*dst)
132 : : : "0" (val), "m" (*dst)
133 : : : "memory"); /* no-clobber list */
134 : : return val;
135 : : }
136 : :
137 : : static inline int rte_atomic16_test_and_set(rte_atomic16_t *v)
138 : : {
139 : 0 : return rte_atomic16_cmpset((volatile uint16_t *)&v->cnt, 0, 1);
140 : : }
141 : :
142 : : static inline void
143 : : rte_atomic16_inc(rte_atomic16_t *v)
144 : : {
145 : 1000000 : asm volatile(
146 : : MPLOCKED
147 : : "incw %[cnt]"
148 : : : [cnt] "=m" (v->cnt) /* output */
149 : : : "m" (v->cnt) /* input */
150 : : );
151 : : }
152 : :
153 : : static inline void
154 : : rte_atomic16_dec(rte_atomic16_t *v)
155 : : {
156 : 1000000 : asm volatile(
157 : : MPLOCKED
158 : : "decw %[cnt]"
159 : : : [cnt] "=m" (v->cnt) /* output */
160 : : : "m" (v->cnt) /* input */
161 : : );
162 : : }
163 : :
164 : : static inline int rte_atomic16_inc_and_test(rte_atomic16_t *v)
165 : : {
166 : : uint8_t ret;
167 : :
168 : 1 : asm volatile(
169 : : MPLOCKED
170 : : "incw %[cnt] ; "
171 : : "sete %[ret]"
172 : : : [cnt] "+m" (v->cnt), /* output */
173 : : [ret] "=qm" (ret)
174 : : );
175 : : return ret != 0;
176 : : }
177 : :
178 : : static inline int rte_atomic16_dec_and_test(rte_atomic16_t *v)
179 : : {
180 : : uint8_t ret;
181 : :
182 : 1 : asm volatile(MPLOCKED
183 : : "decw %[cnt] ; "
184 : : "sete %[ret]"
185 : : : [cnt] "+m" (v->cnt), /* output */
186 : : [ret] "=qm" (ret)
187 : : );
188 : : return ret != 0;
189 : : }
190 : :
191 : : /*------------------------- 32 bit atomic operations -------------------------*/
192 : :
193 : : static inline int
194 : 11040 : rte_atomic32_cmpset(volatile uint32_t *dst, uint32_t exp, uint32_t src)
195 : : {
196 : : uint8_t res;
197 : :
198 : 88870728 : asm volatile(
199 : : MPLOCKED
200 : : "cmpxchgl %[src], %[dst];"
201 : : "sete %[res];"
202 : : : [res] "=a" (res), /* output */
203 : : [dst] "=m" (*dst)
204 : : : [src] "r" (src), /* input */
205 : : "a" (exp),
206 : : "m" (*dst)
207 : : : "memory"); /* no-clobber list */
208 [ + + - + : 88870728 : return res;
- + # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
209 : : }
210 : :
211 : : static inline uint32_t
212 : : rte_atomic32_exchange(volatile uint32_t *dst, uint32_t val)
213 : : {
214 : 1000000 : asm volatile(
215 : : MPLOCKED
216 : : "xchgl %0, %1;"
217 : : : "=r" (val), "=m" (*dst)
218 : : : "0" (val), "m" (*dst)
219 : : : "memory"); /* no-clobber list */
220 : : return val;
221 : : }
222 : :
223 : : static inline int rte_atomic32_test_and_set(rte_atomic32_t *v)
224 : : {
225 : 0 : return rte_atomic32_cmpset((volatile uint32_t *)&v->cnt, 0, 1);
226 : : }
227 : :
228 : : static inline void
229 : : rte_atomic32_inc(rte_atomic32_t *v)
230 : : {
231 : 1000000 : asm volatile(
232 : : MPLOCKED
233 : : "incl %[cnt]"
234 : : : [cnt] "=m" (v->cnt) /* output */
235 : : : "m" (v->cnt) /* input */
236 : : );
237 : 0 : }
238 : :
239 : : static inline void
240 : : rte_atomic32_dec(rte_atomic32_t *v)
241 : : {
242 : 1000000 : asm volatile(
243 : : MPLOCKED
244 : : "decl %[cnt]"
245 : : : [cnt] "=m" (v->cnt) /* output */
246 : : : "m" (v->cnt) /* input */
247 : : );
248 : : }
249 : :
250 : : static inline int rte_atomic32_inc_and_test(rte_atomic32_t *v)
251 : : {
252 : : uint8_t ret;
253 : :
254 : 1 : asm volatile(
255 : : MPLOCKED
256 : : "incl %[cnt] ; "
257 : : "sete %[ret]"
258 : : : [cnt] "+m" (v->cnt), /* output */
259 : : [ret] "=qm" (ret)
260 : : );
261 : : return ret != 0;
262 : : }
263 : :
264 : : static inline int rte_atomic32_dec_and_test(rte_atomic32_t *v)
265 : : {
266 : : uint8_t ret;
267 : :
268 : 1 : asm volatile(MPLOCKED
269 : : "decl %[cnt] ; "
270 : : "sete %[ret]"
271 : : : [cnt] "+m" (v->cnt), /* output */
272 : : [ret] "=qm" (ret)
273 : : );
274 : : return ret != 0;
275 : : }
276 : : #endif
277 : :
278 : : #ifdef RTE_ARCH_I686
279 : : #include "rte_atomic_32.h"
280 : : #else
281 : : #include "rte_atomic_64.h"
282 : : #endif
283 : :
284 : : #endif
285 : :
286 : : #ifdef __cplusplus
287 : : }
288 : : #endif
289 : :
290 : : #endif /* _RTE_ATOMIC_X86_H_ */
|