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_
41 #include <emmintrin.h>
42 #include "generic/rte_atomic.h"
44 #if RTE_MAX_LCORE == 1
45 #define MPLOCKED /**< No need to insert MP lock prefix. */
47 #define MPLOCKED "lock ; " /**< Insert MP lock prefix. */
50 #define rte_mb() _mm_mfence()
52 #define rte_wmb() _mm_sfence()
54 #define rte_rmb() _mm_lfence()
56 /*------------------------- 16 bit atomic operations -------------------------*/
58 #ifndef RTE_FORCE_INTRINSICS
60 rte_atomic16_cmpset(volatile uint16_t *dst, uint16_t exp, uint16_t src)
66 "cmpxchgw %[src], %[dst];"
68 : [res] "=a" (res), /* output */
70 : [src] "r" (src), /* input */
73 : "memory"); /* no-clobber list */
77 static inline int rte_atomic16_test_and_set(rte_atomic16_t *v)
79 return rte_atomic16_cmpset((volatile uint16_t *)&v->cnt, 0, 1);
83 rte_atomic16_inc(rte_atomic16_t *v)
88 : [cnt] "=m" (v->cnt) /* output */
89 : "m" (v->cnt) /* input */
94 rte_atomic16_dec(rte_atomic16_t *v)
99 : [cnt] "=m" (v->cnt) /* output */
100 : "m" (v->cnt) /* input */
104 static inline int rte_atomic16_inc_and_test(rte_atomic16_t *v)
112 : [cnt] "+m" (v->cnt), /* output */
118 static inline int rte_atomic16_dec_and_test(rte_atomic16_t *v)
122 asm volatile(MPLOCKED
125 : [cnt] "+m" (v->cnt), /* output */
131 /*------------------------- 32 bit atomic operations -------------------------*/
134 rte_atomic32_cmpset(volatile uint32_t *dst, uint32_t exp, uint32_t src)
140 "cmpxchgl %[src], %[dst];"
142 : [res] "=a" (res), /* output */
144 : [src] "r" (src), /* input */
147 : "memory"); /* no-clobber list */
151 static inline int rte_atomic32_test_and_set(rte_atomic32_t *v)
153 return rte_atomic32_cmpset((volatile uint32_t *)&v->cnt, 0, 1);
157 rte_atomic32_inc(rte_atomic32_t *v)
162 : [cnt] "=m" (v->cnt) /* output */
163 : "m" (v->cnt) /* input */
168 rte_atomic32_dec(rte_atomic32_t *v)
173 : [cnt] "=m" (v->cnt) /* output */
174 : "m" (v->cnt) /* input */
178 static inline int rte_atomic32_inc_and_test(rte_atomic32_t *v)
186 : [cnt] "+m" (v->cnt), /* output */
192 static inline int rte_atomic32_dec_and_test(rte_atomic32_t *v)
196 asm volatile(MPLOCKED
199 : [cnt] "+m" (v->cnt), /* output */
207 #include "rte_atomic_32.h"
209 #include "rte_atomic_64.h"
216 #endif /* _RTE_ATOMIC_X86_H_ */