eal: factorize x86 headers
[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 <emmintrin.h>
42 #include "generic/rte_atomic.h"
43
44 #if RTE_MAX_LCORE == 1
45 #define MPLOCKED                        /**< No need to insert MP lock prefix. */
46 #else
47 #define MPLOCKED        "lock ; "       /**< Insert MP lock prefix. */
48 #endif
49
50 #define rte_mb() _mm_mfence()
51
52 #define rte_wmb() _mm_sfence()
53
54 #define rte_rmb() _mm_lfence()
55
56 /*------------------------- 16 bit atomic operations -------------------------*/
57
58 #ifndef RTE_FORCE_INTRINSICS
59 static inline int
60 rte_atomic16_cmpset(volatile uint16_t *dst, uint16_t exp, uint16_t src)
61 {
62         uint8_t res;
63
64         asm volatile(
65                         MPLOCKED
66                         "cmpxchgw %[src], %[dst];"
67                         "sete %[res];"
68                         : [res] "=a" (res),     /* output */
69                           [dst] "=m" (*dst)
70                         : [src] "r" (src),      /* input */
71                           "a" (exp),
72                           "m" (*dst)
73                         : "memory");            /* no-clobber list */
74         return res;
75 }
76
77 static inline int rte_atomic16_test_and_set(rte_atomic16_t *v)
78 {
79         return rte_atomic16_cmpset((volatile uint16_t *)&v->cnt, 0, 1);
80 }
81
82 static inline void
83 rte_atomic16_inc(rte_atomic16_t *v)
84 {
85         asm volatile(
86                         MPLOCKED
87                         "incw %[cnt]"
88                         : [cnt] "=m" (v->cnt)   /* output */
89                         : "m" (v->cnt)          /* input */
90                         );
91 }
92
93 static inline void
94 rte_atomic16_dec(rte_atomic16_t *v)
95 {
96         asm volatile(
97                         MPLOCKED
98                         "decw %[cnt]"
99                         : [cnt] "=m" (v->cnt)   /* output */
100                         : "m" (v->cnt)          /* input */
101                         );
102 }
103
104 static inline int rte_atomic16_inc_and_test(rte_atomic16_t *v)
105 {
106         uint8_t ret;
107
108         asm volatile(
109                         MPLOCKED
110                         "incw %[cnt] ; "
111                         "sete %[ret]"
112                         : [cnt] "+m" (v->cnt),  /* output */
113                           [ret] "=qm" (ret)
114                         );
115         return (ret != 0);
116 }
117
118 static inline int rte_atomic16_dec_and_test(rte_atomic16_t *v)
119 {
120         uint8_t ret;
121
122         asm volatile(MPLOCKED
123                         "decw %[cnt] ; "
124                         "sete %[ret]"
125                         : [cnt] "+m" (v->cnt),  /* output */
126                           [ret] "=qm" (ret)
127                         );
128         return (ret != 0);
129 }
130
131 /*------------------------- 32 bit atomic operations -------------------------*/
132
133 static inline int
134 rte_atomic32_cmpset(volatile uint32_t *dst, uint32_t exp, uint32_t src)
135 {
136         uint8_t res;
137
138         asm volatile(
139                         MPLOCKED
140                         "cmpxchgl %[src], %[dst];"
141                         "sete %[res];"
142                         : [res] "=a" (res),     /* output */
143                           [dst] "=m" (*dst)
144                         : [src] "r" (src),      /* input */
145                           "a" (exp),
146                           "m" (*dst)
147                         : "memory");            /* no-clobber list */
148         return res;
149 }
150
151 static inline int rte_atomic32_test_and_set(rte_atomic32_t *v)
152 {
153         return rte_atomic32_cmpset((volatile uint32_t *)&v->cnt, 0, 1);
154 }
155
156 static inline void
157 rte_atomic32_inc(rte_atomic32_t *v)
158 {
159         asm volatile(
160                         MPLOCKED
161                         "incl %[cnt]"
162                         : [cnt] "=m" (v->cnt)   /* output */
163                         : "m" (v->cnt)          /* input */
164                         );
165 }
166
167 static inline void
168 rte_atomic32_dec(rte_atomic32_t *v)
169 {
170         asm volatile(
171                         MPLOCKED
172                         "decl %[cnt]"
173                         : [cnt] "=m" (v->cnt)   /* output */
174                         : "m" (v->cnt)          /* input */
175                         );
176 }
177
178 static inline int rte_atomic32_inc_and_test(rte_atomic32_t *v)
179 {
180         uint8_t ret;
181
182         asm volatile(
183                         MPLOCKED
184                         "incl %[cnt] ; "
185                         "sete %[ret]"
186                         : [cnt] "+m" (v->cnt),  /* output */
187                           [ret] "=qm" (ret)
188                         );
189         return (ret != 0);
190 }
191
192 static inline int rte_atomic32_dec_and_test(rte_atomic32_t *v)
193 {
194         uint8_t ret;
195
196         asm volatile(MPLOCKED
197                         "decl %[cnt] ; "
198                         "sete %[ret]"
199                         : [cnt] "+m" (v->cnt),  /* output */
200                           [ret] "=qm" (ret)
201                         );
202         return (ret != 0);
203 }
204 #endif
205
206 #ifdef RTE_ARCH_I686
207 #include "rte_atomic_32.h"
208 #else
209 #include "rte_atomic_64.h"
210 #endif
211
212 #ifdef __cplusplus
213 }
214 #endif
215
216 #endif /* _RTE_ATOMIC_X86_H_ */