4 * Copyright (C) IBM Corporation 2014.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of IBM Corporation nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * Inspired from FreeBSD src/sys/powerpc/include/atomic.h
35 * Copyright (c) 2008 Marcel Moolenaar
36 * Copyright (c) 2001 Benno Rice
37 * Copyright (c) 2001 David E. O'Brien
38 * Copyright (c) 1998 Doug Rabson
39 * All rights reserved.
42 #ifndef _RTE_ATOMIC_PPC_64_H_
43 #define _RTE_ATOMIC_PPC_64_H_
49 #include "generic/rte_atomic.h"
52 * General memory barrier.
54 * Guarantees that the LOAD and STORE operations generated before the
55 * barrier occur before the LOAD and STORE operations generated after.
57 #define rte_mb() {asm volatile("sync" : : : "memory"); }
60 * Write memory barrier.
62 * Guarantees that the STORE operations generated before the barrier
63 * occur before the STORE operations generated after.
65 #define rte_wmb() {asm volatile("sync" : : : "memory"); }
68 * Read memory barrier.
70 * Guarantees that the LOAD operations generated before the barrier
71 * occur before the LOAD operations generated after.
73 #define rte_rmb() {asm volatile("sync" : : : "memory"); }
75 #define rte_smp_mb() rte_mb()
77 #define rte_smp_wmb() rte_compiler_barrier()
79 #define rte_smp_rmb() rte_compiler_barrier()
81 /*------------------------- 16 bit atomic operations -------------------------*/
82 /* To be compatible with Power7, use GCC built-in functions for 16 bit
85 #ifndef RTE_FORCE_INTRINSICS
87 rte_atomic16_cmpset(volatile uint16_t *dst, uint16_t exp, uint16_t src)
89 return __atomic_compare_exchange(dst, &exp, &src, 0, __ATOMIC_ACQUIRE,
90 __ATOMIC_ACQUIRE) ? 1 : 0;
93 static inline int rte_atomic16_test_and_set(rte_atomic16_t *v)
95 return rte_atomic16_cmpset((volatile uint16_t *)&v->cnt, 0, 1);
99 rte_atomic16_inc(rte_atomic16_t *v)
101 __atomic_add_fetch(&v->cnt, 1, __ATOMIC_ACQUIRE);
105 rte_atomic16_dec(rte_atomic16_t *v)
107 __atomic_sub_fetch(&v->cnt, 1, __ATOMIC_ACQUIRE);
110 static inline int rte_atomic16_inc_and_test(rte_atomic16_t *v)
112 return (__atomic_add_fetch(&v->cnt, 1, __ATOMIC_ACQUIRE) == 0);
115 static inline int rte_atomic16_dec_and_test(rte_atomic16_t *v)
117 return (__atomic_sub_fetch(&v->cnt, 1, __ATOMIC_ACQUIRE) == 0);
120 /*------------------------- 32 bit atomic operations -------------------------*/
123 rte_atomic32_cmpset(volatile uint32_t *dst, uint32_t exp, uint32_t src)
125 unsigned int ret = 0;
129 "1:\tlwarx %[ret], 0, %[dst]\n"
130 "cmplw %[exp], %[ret]\n"
132 "stwcx. %[src], 0, %[dst]\n"
137 "stwcx. %[ret], 0, %[dst]\n"
141 : [ret] "=&r" (ret), "=m" (*dst)
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 "1: lwarx %[t],0,%[cnt]\n"
163 "addic %[t],%[t],1\n"
164 "stwcx. %[t],0,%[cnt]\n"
166 : [t] "=&r" (t), "=m" (v->cnt)
167 : [cnt] "r" (&v->cnt), "m" (v->cnt)
168 : "cc", "xer", "memory");
172 rte_atomic32_dec(rte_atomic32_t *v)
177 "1: lwarx %[t],0,%[cnt]\n"
178 "addic %[t],%[t],-1\n"
179 "stwcx. %[t],0,%[cnt]\n"
181 : [t] "=&r" (t), "=m" (v->cnt)
182 : [cnt] "r" (&v->cnt), "m" (v->cnt)
183 : "cc", "xer", "memory");
186 static inline int rte_atomic32_inc_and_test(rte_atomic32_t *v)
192 "1: lwarx %[ret],0,%[cnt]\n"
193 "addic %[ret],%[ret],1\n"
194 "stwcx. %[ret],0,%[cnt]\n"
198 : [cnt] "r" (&v->cnt)
199 : "cc", "xer", "memory");
204 static inline int rte_atomic32_dec_and_test(rte_atomic32_t *v)
210 "1: lwarx %[ret],0,%[cnt]\n"
211 "addic %[ret],%[ret],-1\n"
212 "stwcx. %[ret],0,%[cnt]\n"
216 : [cnt] "r" (&v->cnt)
217 : "cc", "xer", "memory");
221 /*------------------------- 64 bit atomic operations -------------------------*/
224 rte_atomic64_cmpset(volatile uint64_t *dst, uint64_t exp, uint64_t src)
226 unsigned int ret = 0;
230 "1: ldarx %[ret], 0, %[dst]\n"
231 "cmpld %[exp], %[ret]\n"
233 "stdcx. %[src], 0, %[dst]\n"
238 "stdcx. %[ret], 0, %[dst]\n"
242 : [ret] "=&r" (ret), "=m" (*dst)
252 rte_atomic64_init(rte_atomic64_t *v)
257 static inline int64_t
258 rte_atomic64_read(rte_atomic64_t *v)
262 asm volatile("ld%U1%X1 %[ret],%[cnt]"
264 : [cnt] "m"(v->cnt));
270 rte_atomic64_set(rte_atomic64_t *v, int64_t new_value)
272 asm volatile("std%U0%X0 %[new_value],%[cnt]"
274 : [new_value] "r"(new_value));
278 rte_atomic64_add(rte_atomic64_t *v, int64_t inc)
283 "1: ldarx %[t],0,%[cnt]\n"
284 "add %[t],%[inc],%[t]\n"
285 "stdcx. %[t],0,%[cnt]\n"
287 : [t] "=&r" (t), "=m" (v->cnt)
288 : [cnt] "r" (&v->cnt), [inc] "r" (inc), "m" (v->cnt)
293 rte_atomic64_sub(rte_atomic64_t *v, int64_t dec)
298 "1: ldarx %[t],0,%[cnt]\n"
299 "subf %[t],%[dec],%[t]\n"
300 "stdcx. %[t],0,%[cnt]\n"
302 : [t] "=&r" (t), "+m" (v->cnt)
303 : [cnt] "r" (&v->cnt), [dec] "r" (dec), "m" (v->cnt)
308 rte_atomic64_inc(rte_atomic64_t *v)
313 "1: ldarx %[t],0,%[cnt]\n"
314 "addic %[t],%[t],1\n"
315 "stdcx. %[t],0,%[cnt]\n"
317 : [t] "=&r" (t), "+m" (v->cnt)
318 : [cnt] "r" (&v->cnt), "m" (v->cnt)
319 : "cc", "xer", "memory");
323 rte_atomic64_dec(rte_atomic64_t *v)
328 "1: ldarx %[t],0,%[cnt]\n"
329 "addic %[t],%[t],-1\n"
330 "stdcx. %[t],0,%[cnt]\n"
332 : [t] "=&r" (t), "+m" (v->cnt)
333 : [cnt] "r" (&v->cnt), "m" (v->cnt)
334 : "cc", "xer", "memory");
337 static inline int64_t
338 rte_atomic64_add_return(rte_atomic64_t *v, int64_t inc)
344 "1: ldarx %[ret],0,%[cnt]\n"
345 "add %[ret],%[inc],%[ret]\n"
346 "stdcx. %[ret],0,%[cnt]\n"
350 : [inc] "r" (inc), [cnt] "r" (&v->cnt)
356 static inline int64_t
357 rte_atomic64_sub_return(rte_atomic64_t *v, int64_t dec)
363 "1: ldarx %[ret],0,%[cnt]\n"
364 "subf %[ret],%[dec],%[ret]\n"
365 "stdcx. %[ret],0,%[cnt]\n"
369 : [dec] "r" (dec), [cnt] "r" (&v->cnt)
375 static inline int rte_atomic64_inc_and_test(rte_atomic64_t *v)
381 "1: ldarx %[ret],0,%[cnt]\n"
382 "addic %[ret],%[ret],1\n"
383 "stdcx. %[ret],0,%[cnt]\n"
387 : [cnt] "r" (&v->cnt)
388 : "cc", "xer", "memory");
393 static inline int rte_atomic64_dec_and_test(rte_atomic64_t *v)
399 "1: ldarx %[ret],0,%[cnt]\n"
400 "addic %[ret],%[ret],-1\n"
401 "stdcx. %[ret],0,%[cnt]\n"
405 : [cnt] "r" (&v->cnt)
406 : "cc", "xer", "memory");
411 static inline int rte_atomic64_test_and_set(rte_atomic64_t *v)
413 return rte_atomic64_cmpset((volatile uint64_t *)&v->cnt, 0, 1);
417 * Atomically set a 64-bit counter to 0.
420 * A pointer to the atomic counter.
422 static inline void rte_atomic64_clear(rte_atomic64_t *v)
432 #endif /* _RTE_ATOMIC_PPC_64_H_ */