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 <rte_config.h>
44 #include <emmintrin.h>
45 #include "generic/rte_atomic.h"
47 #if RTE_MAX_LCORE == 1
48 #define MPLOCKED /**< No need to insert MP lock prefix. */
50 #define MPLOCKED "lock ; " /**< Insert MP lock prefix. */
53 #define rte_mb() _mm_mfence()
55 #define rte_wmb() _mm_sfence()
57 #define rte_rmb() _mm_lfence()
59 #define rte_smp_mb() rte_mb()
61 #define rte_smp_wmb() rte_compiler_barrier()
63 #define rte_smp_rmb() rte_compiler_barrier()
65 #define rte_io_mb() rte_mb()
67 #define rte_io_wmb() rte_compiler_barrier()
69 #define rte_io_rmb() rte_compiler_barrier()
71 /*------------------------- 16 bit atomic operations -------------------------*/
73 #ifndef RTE_FORCE_INTRINSICS
75 rte_atomic16_cmpset(volatile uint16_t *dst, uint16_t exp, uint16_t src)
81 "cmpxchgw %[src], %[dst];"
83 : [res] "=a" (res), /* output */
85 : [src] "r" (src), /* input */
88 : "memory"); /* no-clobber list */
92 static inline int rte_atomic16_test_and_set(rte_atomic16_t *v)
94 return rte_atomic16_cmpset((volatile uint16_t *)&v->cnt, 0, 1);
98 rte_atomic16_inc(rte_atomic16_t *v)
103 : [cnt] "=m" (v->cnt) /* output */
104 : "m" (v->cnt) /* input */
109 rte_atomic16_dec(rte_atomic16_t *v)
114 : [cnt] "=m" (v->cnt) /* output */
115 : "m" (v->cnt) /* input */
119 static inline int rte_atomic16_inc_and_test(rte_atomic16_t *v)
127 : [cnt] "+m" (v->cnt), /* output */
133 static inline int rte_atomic16_dec_and_test(rte_atomic16_t *v)
137 asm volatile(MPLOCKED
140 : [cnt] "+m" (v->cnt), /* output */
146 /*------------------------- 32 bit atomic operations -------------------------*/
149 rte_atomic32_cmpset(volatile uint32_t *dst, uint32_t exp, uint32_t src)
155 "cmpxchgl %[src], %[dst];"
157 : [res] "=a" (res), /* output */
159 : [src] "r" (src), /* input */
162 : "memory"); /* no-clobber list */
166 static inline int rte_atomic32_test_and_set(rte_atomic32_t *v)
168 return rte_atomic32_cmpset((volatile uint32_t *)&v->cnt, 0, 1);
172 rte_atomic32_inc(rte_atomic32_t *v)
177 : [cnt] "=m" (v->cnt) /* output */
178 : "m" (v->cnt) /* input */
183 rte_atomic32_dec(rte_atomic32_t *v)
188 : [cnt] "=m" (v->cnt) /* output */
189 : "m" (v->cnt) /* input */
193 static inline int rte_atomic32_inc_and_test(rte_atomic32_t *v)
201 : [cnt] "+m" (v->cnt), /* output */
207 static inline int rte_atomic32_dec_and_test(rte_atomic32_t *v)
211 asm volatile(MPLOCKED
214 : [cnt] "+m" (v->cnt), /* output */
222 #include "rte_atomic_32.h"
224 #include "rte_atomic_64.h"
231 #endif /* _RTE_ATOMIC_X86_H_ */