4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #ifndef _RTE_ATOMIC_X86_H_
35 #define _RTE_ATOMIC_X86_H_
42 #include <rte_common.h>
43 #include <emmintrin.h>
44 #include "generic/rte_atomic.h"
46 #if RTE_MAX_LCORE == 1
47 #define MPLOCKED /**< No need to insert MP lock prefix. */
49 #define MPLOCKED "lock ; " /**< Insert MP lock prefix. */
52 #define rte_mb() _mm_mfence()
54 #define rte_wmb() _mm_sfence()
56 #define rte_rmb() _mm_lfence()
58 #define rte_smp_wmb() rte_compiler_barrier()
60 #define rte_smp_rmb() rte_compiler_barrier()
63 * From Intel Software Development Manual; Vol 3;
64 * 8.2.2 Memory Ordering in P6 and More Recent Processor Families:
66 * . Reads are not reordered with other reads.
67 * . Writes are not reordered with older reads.
68 * . Writes to memory are not reordered with other writes,
69 * with the following exceptions:
70 * . streaming stores (writes) executed with the non-temporal move
71 * instructions (MOVNTI, MOVNTQ, MOVNTDQ, MOVNTPS, and MOVNTPD); and
72 * . string operations (see Section 8.2.4.1).
74 * . Reads may be reordered with older writes to different locations but not
75 * with older writes to the same location.
76 * . Reads or writes cannot be reordered with I/O instructions,
77 * locked instructions, or serializing instructions.
78 * . Reads cannot pass earlier LFENCE and MFENCE instructions.
79 * . Writes ... cannot pass earlier LFENCE, SFENCE, and MFENCE instructions.
80 * . LFENCE instructions cannot pass earlier reads.
81 * . SFENCE instructions cannot pass earlier writes ...
82 * . MFENCE instructions cannot pass earlier reads, writes ...
84 * As pointed by Java guys, that makes possible to use lock-prefixed
85 * instructions to get the same effect as mfence and on most modern HW
86 * that gives a better perfomance then using mfence:
87 * https://shipilev.net/blog/2014/on-the-fence-with-dependencies/
88 * Basic idea is to use lock prefixed add with some dummy memory location
89 * as the destination. From their experiments 128B(2 cache lines) below
90 * current stack pointer looks like a good candidate.
91 * So below we use that techinque for rte_smp_mb() implementation.
94 static inline void __attribute__((always_inline))
98 asm volatile("lock addl $0, -128(%%esp); " ::: "memory");
100 asm volatile("lock addl $0, -128(%%rsp); " ::: "memory");
104 /*------------------------- 16 bit atomic operations -------------------------*/
106 #ifndef RTE_FORCE_INTRINSICS
108 rte_atomic16_cmpset(volatile uint16_t *dst, uint16_t exp, uint16_t src)
114 "cmpxchgw %[src], %[dst];"
116 : [res] "=a" (res), /* output */
118 : [src] "r" (src), /* input */
121 : "memory"); /* no-clobber list */
125 static inline int rte_atomic16_test_and_set(rte_atomic16_t *v)
127 return rte_atomic16_cmpset((volatile uint16_t *)&v->cnt, 0, 1);
131 rte_atomic16_inc(rte_atomic16_t *v)
136 : [cnt] "=m" (v->cnt) /* output */
137 : "m" (v->cnt) /* input */
142 rte_atomic16_dec(rte_atomic16_t *v)
147 : [cnt] "=m" (v->cnt) /* output */
148 : "m" (v->cnt) /* input */
152 static inline int rte_atomic16_inc_and_test(rte_atomic16_t *v)
160 : [cnt] "+m" (v->cnt), /* output */
166 static inline int rte_atomic16_dec_and_test(rte_atomic16_t *v)
170 asm volatile(MPLOCKED
173 : [cnt] "+m" (v->cnt), /* output */
179 /*------------------------- 32 bit atomic operations -------------------------*/
182 rte_atomic32_cmpset(volatile uint32_t *dst, uint32_t exp, uint32_t src)
188 "cmpxchgl %[src], %[dst];"
190 : [res] "=a" (res), /* output */
192 : [src] "r" (src), /* input */
195 : "memory"); /* no-clobber list */
199 static inline int rte_atomic32_test_and_set(rte_atomic32_t *v)
201 return rte_atomic32_cmpset((volatile uint32_t *)&v->cnt, 0, 1);
205 rte_atomic32_inc(rte_atomic32_t *v)
210 : [cnt] "=m" (v->cnt) /* output */
211 : "m" (v->cnt) /* input */
216 rte_atomic32_dec(rte_atomic32_t *v)
221 : [cnt] "=m" (v->cnt) /* output */
222 : "m" (v->cnt) /* input */
226 static inline int rte_atomic32_inc_and_test(rte_atomic32_t *v)
234 : [cnt] "+m" (v->cnt), /* output */
240 static inline int rte_atomic32_dec_and_test(rte_atomic32_t *v)
244 asm volatile(MPLOCKED
247 : [cnt] "+m" (v->cnt), /* output */
255 #include "rte_atomic_32.h"
257 #include "rte_atomic_64.h"
264 #endif /* _RTE_ATOMIC_X86_H_ */