Imported Upstream version 16.04
[deb_dpdk.git] / lib / librte_eal / common / include / arch / x86 / rte_atomic_64.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 /*
35  * Inspired from FreeBSD src/sys/amd64/include/atomic.h
36  * Copyright (c) 1998 Doug Rabson
37  * All rights reserved.
38  */
39
40 #ifndef _RTE_ATOMIC_X86_64_H_
41 #define _RTE_ATOMIC_X86_64_H_
42
43 /*------------------------- 64 bit atomic operations -------------------------*/
44
45 #ifndef RTE_FORCE_INTRINSICS
46 static inline int
47 rte_atomic64_cmpset(volatile uint64_t *dst, uint64_t exp, uint64_t src)
48 {
49         uint8_t res;
50
51
52         asm volatile(
53                         MPLOCKED
54                         "cmpxchgq %[src], %[dst];"
55                         "sete %[res];"
56                         : [res] "=a" (res),     /* output */
57                           [dst] "=m" (*dst)
58                         : [src] "r" (src),      /* input */
59                           "a" (exp),
60                           "m" (*dst)
61                         : "memory");            /* no-clobber list */
62
63         return res;
64 }
65
66 static inline void
67 rte_atomic64_init(rte_atomic64_t *v)
68 {
69         v->cnt = 0;
70 }
71
72 static inline int64_t
73 rte_atomic64_read(rte_atomic64_t *v)
74 {
75         return v->cnt;
76 }
77
78 static inline void
79 rte_atomic64_set(rte_atomic64_t *v, int64_t new_value)
80 {
81         v->cnt = new_value;
82 }
83
84 static inline void
85 rte_atomic64_add(rte_atomic64_t *v, int64_t inc)
86 {
87         asm volatile(
88                         MPLOCKED
89                         "addq %[inc], %[cnt]"
90                         : [cnt] "=m" (v->cnt)   /* output */
91                         : [inc] "ir" (inc),     /* input */
92                           "m" (v->cnt)
93                         );
94 }
95
96 static inline void
97 rte_atomic64_sub(rte_atomic64_t *v, int64_t dec)
98 {
99         asm volatile(
100                         MPLOCKED
101                         "subq %[dec], %[cnt]"
102                         : [cnt] "=m" (v->cnt)   /* output */
103                         : [dec] "ir" (dec),     /* input */
104                           "m" (v->cnt)
105                         );
106 }
107
108 static inline void
109 rte_atomic64_inc(rte_atomic64_t *v)
110 {
111         asm volatile(
112                         MPLOCKED
113                         "incq %[cnt]"
114                         : [cnt] "=m" (v->cnt)   /* output */
115                         : "m" (v->cnt)          /* input */
116                         );
117 }
118
119 static inline void
120 rte_atomic64_dec(rte_atomic64_t *v)
121 {
122         asm volatile(
123                         MPLOCKED
124                         "decq %[cnt]"
125                         : [cnt] "=m" (v->cnt)   /* output */
126                         : "m" (v->cnt)          /* input */
127                         );
128 }
129
130 static inline int64_t
131 rte_atomic64_add_return(rte_atomic64_t *v, int64_t inc)
132 {
133         int64_t prev = inc;
134
135         asm volatile(
136                         MPLOCKED
137                         "xaddq %[prev], %[cnt]"
138                         : [prev] "+r" (prev),   /* output */
139                           [cnt] "=m" (v->cnt)
140                         : "m" (v->cnt)          /* input */
141                         );
142         return prev + inc;
143 }
144
145 static inline int64_t
146 rte_atomic64_sub_return(rte_atomic64_t *v, int64_t dec)
147 {
148         return rte_atomic64_add_return(v, -dec);
149 }
150
151 static inline int rte_atomic64_inc_and_test(rte_atomic64_t *v)
152 {
153         uint8_t ret;
154
155         asm volatile(
156                         MPLOCKED
157                         "incq %[cnt] ; "
158                         "sete %[ret]"
159                         : [cnt] "+m" (v->cnt), /* output */
160                           [ret] "=qm" (ret)
161                         );
162
163         return ret != 0;
164 }
165
166 static inline int rte_atomic64_dec_and_test(rte_atomic64_t *v)
167 {
168         uint8_t ret;
169
170         asm volatile(
171                         MPLOCKED
172                         "decq %[cnt] ; "
173                         "sete %[ret]"
174                         : [cnt] "+m" (v->cnt),  /* output */
175                           [ret] "=qm" (ret)
176                         );
177         return ret != 0;
178 }
179
180 static inline int rte_atomic64_test_and_set(rte_atomic64_t *v)
181 {
182         return rte_atomic64_cmpset((volatile uint64_t *)&v->cnt, 0, 1);
183 }
184
185 static inline void rte_atomic64_clear(rte_atomic64_t *v)
186 {
187         v->cnt = 0;
188 }
189 #endif
190
191 #endif /* _RTE_ATOMIC_X86_64_H_ */