New upstream version 17.11.1
[deb_dpdk.git] / lib / librte_eal / common / include / arch / x86 / rte_atomic.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #ifndef _RTE_ATOMIC_X86_H_
35 #define _RTE_ATOMIC_X86_H_
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 #include <stdint.h>
42 #include <rte_common.h>
43 #include <rte_config.h>
44 #include <emmintrin.h>
45 #include "generic/rte_atomic.h"
46
47 #if RTE_MAX_LCORE == 1
48 #define MPLOCKED                        /**< No need to insert MP lock prefix. */
49 #else
50 #define MPLOCKED        "lock ; "       /**< Insert MP lock prefix. */
51 #endif
52
53 #define rte_mb() _mm_mfence()
54
55 #define rte_wmb() _mm_sfence()
56
57 #define rte_rmb() _mm_lfence()
58
59 #define rte_smp_mb() rte_mb()
60
61 #define rte_smp_wmb() rte_compiler_barrier()
62
63 #define rte_smp_rmb() rte_compiler_barrier()
64
65 #define rte_io_mb() rte_mb()
66
67 #define rte_io_wmb() rte_compiler_barrier()
68
69 #define rte_io_rmb() rte_compiler_barrier()
70
71 /*------------------------- 16 bit atomic operations -------------------------*/
72
73 #ifndef RTE_FORCE_INTRINSICS
74 static inline int
75 rte_atomic16_cmpset(volatile uint16_t *dst, uint16_t exp, uint16_t src)
76 {
77         uint8_t res;
78
79         asm volatile(
80                         MPLOCKED
81                         "cmpxchgw %[src], %[dst];"
82                         "sete %[res];"
83                         : [res] "=a" (res),     /* output */
84                           [dst] "=m" (*dst)
85                         : [src] "r" (src),      /* input */
86                           "a" (exp),
87                           "m" (*dst)
88                         : "memory");            /* no-clobber list */
89         return res;
90 }
91
92 static inline int rte_atomic16_test_and_set(rte_atomic16_t *v)
93 {
94         return rte_atomic16_cmpset((volatile uint16_t *)&v->cnt, 0, 1);
95 }
96
97 static inline void
98 rte_atomic16_inc(rte_atomic16_t *v)
99 {
100         asm volatile(
101                         MPLOCKED
102                         "incw %[cnt]"
103                         : [cnt] "=m" (v->cnt)   /* output */
104                         : "m" (v->cnt)          /* input */
105                         );
106 }
107
108 static inline void
109 rte_atomic16_dec(rte_atomic16_t *v)
110 {
111         asm volatile(
112                         MPLOCKED
113                         "decw %[cnt]"
114                         : [cnt] "=m" (v->cnt)   /* output */
115                         : "m" (v->cnt)          /* input */
116                         );
117 }
118
119 static inline int rte_atomic16_inc_and_test(rte_atomic16_t *v)
120 {
121         uint8_t ret;
122
123         asm volatile(
124                         MPLOCKED
125                         "incw %[cnt] ; "
126                         "sete %[ret]"
127                         : [cnt] "+m" (v->cnt),  /* output */
128                           [ret] "=qm" (ret)
129                         );
130         return ret != 0;
131 }
132
133 static inline int rte_atomic16_dec_and_test(rte_atomic16_t *v)
134 {
135         uint8_t ret;
136
137         asm volatile(MPLOCKED
138                         "decw %[cnt] ; "
139                         "sete %[ret]"
140                         : [cnt] "+m" (v->cnt),  /* output */
141                           [ret] "=qm" (ret)
142                         );
143         return ret != 0;
144 }
145
146 /*------------------------- 32 bit atomic operations -------------------------*/
147
148 static inline int
149 rte_atomic32_cmpset(volatile uint32_t *dst, uint32_t exp, uint32_t src)
150 {
151         uint8_t res;
152
153         asm volatile(
154                         MPLOCKED
155                         "cmpxchgl %[src], %[dst];"
156                         "sete %[res];"
157                         : [res] "=a" (res),     /* output */
158                           [dst] "=m" (*dst)
159                         : [src] "r" (src),      /* input */
160                           "a" (exp),
161                           "m" (*dst)
162                         : "memory");            /* no-clobber list */
163         return res;
164 }
165
166 static inline int rte_atomic32_test_and_set(rte_atomic32_t *v)
167 {
168         return rte_atomic32_cmpset((volatile uint32_t *)&v->cnt, 0, 1);
169 }
170
171 static inline void
172 rte_atomic32_inc(rte_atomic32_t *v)
173 {
174         asm volatile(
175                         MPLOCKED
176                         "incl %[cnt]"
177                         : [cnt] "=m" (v->cnt)   /* output */
178                         : "m" (v->cnt)          /* input */
179                         );
180 }
181
182 static inline void
183 rte_atomic32_dec(rte_atomic32_t *v)
184 {
185         asm volatile(
186                         MPLOCKED
187                         "decl %[cnt]"
188                         : [cnt] "=m" (v->cnt)   /* output */
189                         : "m" (v->cnt)          /* input */
190                         );
191 }
192
193 static inline int rte_atomic32_inc_and_test(rte_atomic32_t *v)
194 {
195         uint8_t ret;
196
197         asm volatile(
198                         MPLOCKED
199                         "incl %[cnt] ; "
200                         "sete %[ret]"
201                         : [cnt] "+m" (v->cnt),  /* output */
202                           [ret] "=qm" (ret)
203                         );
204         return ret != 0;
205 }
206
207 static inline int rte_atomic32_dec_and_test(rte_atomic32_t *v)
208 {
209         uint8_t ret;
210
211         asm volatile(MPLOCKED
212                         "decl %[cnt] ; "
213                         "sete %[ret]"
214                         : [cnt] "+m" (v->cnt),  /* output */
215                           [ret] "=qm" (ret)
216                         );
217         return ret != 0;
218 }
219 #endif
220
221 #ifdef RTE_ARCH_I686
222 #include "rte_atomic_32.h"
223 #else
224 #include "rte_atomic_64.h"
225 #endif
226
227 #ifdef __cplusplus
228 }
229 #endif
230
231 #endif /* _RTE_ATOMIC_X86_H_ */