1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015 Cavium, Inc
3 * Copyright(c) 2020 Arm Limited
6 #ifndef _RTE_ATOMIC_ARM64_H_
7 #define _RTE_ATOMIC_ARM64_H_
9 #ifndef RTE_FORCE_INTRINSICS
10 # error Platform must be built with RTE_FORCE_INTRINSICS
17 #include "generic/rte_atomic.h"
18 #include <rte_branch_prediction.h>
19 #include <rte_compat.h>
20 #include <rte_debug.h>
22 #define rte_mb() asm volatile("dmb osh" : : : "memory")
24 #define rte_wmb() asm volatile("dmb oshst" : : : "memory")
26 #define rte_rmb() asm volatile("dmb oshld" : : : "memory")
28 #define rte_smp_mb() asm volatile("dmb ish" : : : "memory")
30 #define rte_smp_wmb() asm volatile("dmb ishst" : : : "memory")
32 #define rte_smp_rmb() asm volatile("dmb ishld" : : : "memory")
34 #define rte_io_mb() rte_mb()
36 #define rte_io_wmb() rte_wmb()
38 #define rte_io_rmb() rte_rmb()
40 static __rte_always_inline void
41 rte_atomic_thread_fence(int memorder)
43 __atomic_thread_fence(memorder);
46 /*------------------------ 128 bit atomic operations -------------------------*/
48 #if defined(__ARM_FEATURE_ATOMICS) || defined(RTE_ARM_FEATURE_ATOMICS)
49 #define __LSE_PREAMBLE ".arch armv8-a+lse\n"
51 #define __ATOMIC128_CAS_OP(cas_op_name, op_string) \
52 static __rte_noinline rte_int128_t \
53 cas_op_name(rte_int128_t *dst, rte_int128_t old, rte_int128_t updated) \
55 /* caspX instructions register pair must start from even-numbered
56 * register at operand 1.
57 * So, specify registers for local variables here.
59 register uint64_t x0 __asm("x0") = (uint64_t)old.val[0]; \
60 register uint64_t x1 __asm("x1") = (uint64_t)old.val[1]; \
61 register uint64_t x2 __asm("x2") = (uint64_t)updated.val[0]; \
62 register uint64_t x3 __asm("x3") = (uint64_t)updated.val[1]; \
65 op_string " %[old0], %[old1], %[upd0], %[upd1], [%[dst]]" \
77 __ATOMIC128_CAS_OP(__cas_128_relaxed, "casp")
78 __ATOMIC128_CAS_OP(__cas_128_acquire, "caspa")
79 __ATOMIC128_CAS_OP(__cas_128_release, "caspl")
80 __ATOMIC128_CAS_OP(__cas_128_acq_rel, "caspal")
83 #undef __ATOMIC128_CAS_OP
89 rte_atomic128_cmp_exchange(rte_int128_t *dst, rte_int128_t *exp,
90 const rte_int128_t *src, unsigned int weak, int success,
93 /* Always do strong CAS */
95 /* Ignore memory ordering for failure, memory order for
96 * success must be stronger or equal
98 RTE_SET_USED(failure);
99 /* Find invalid memory order */
100 RTE_ASSERT(success == __ATOMIC_RELAXED ||
101 success == __ATOMIC_ACQUIRE ||
102 success == __ATOMIC_RELEASE ||
103 success == __ATOMIC_ACQ_REL ||
104 success == __ATOMIC_SEQ_CST);
106 rte_int128_t expected = *exp;
107 rte_int128_t desired = *src;
110 #if defined(__ARM_FEATURE_ATOMICS) || defined(RTE_ARM_FEATURE_ATOMICS)
111 if (success == __ATOMIC_RELAXED)
112 old = __cas_128_relaxed(dst, expected, desired);
113 else if (success == __ATOMIC_ACQUIRE)
114 old = __cas_128_acquire(dst, expected, desired);
115 else if (success == __ATOMIC_RELEASE)
116 old = __cas_128_release(dst, expected, desired);
118 old = __cas_128_acq_rel(dst, expected, desired);
120 #define __HAS_ACQ(mo) ((mo) != __ATOMIC_RELAXED && (mo) != __ATOMIC_RELEASE)
121 #define __HAS_RLS(mo) ((mo) == __ATOMIC_RELEASE || (mo) == __ATOMIC_ACQ_REL || \
122 (mo) == __ATOMIC_SEQ_CST)
124 int ldx_mo = __HAS_ACQ(success) ? __ATOMIC_ACQUIRE : __ATOMIC_RELAXED;
125 int stx_mo = __HAS_RLS(success) ? __ATOMIC_RELEASE : __ATOMIC_RELAXED;
132 /* ldx128 can not guarantee atomic,
133 * Must write back src or old to verify atomicity of ldx128;
137 #define __LOAD_128(op_string, src, dst) { \
139 op_string " %0, %1, %2" \
140 : "=&r" (dst.val[0]), \
142 : "Q" (src->val[0]) \
145 if (ldx_mo == __ATOMIC_RELAXED)
146 __LOAD_128("ldxp", dst, old)
148 __LOAD_128("ldaxp", dst, old)
152 #define __STORE_128(op_string, dst, src, ret) { \
154 op_string " %w0, %1, %2, %3" \
156 : "r" (src.val[0]), \
161 if (likely(old.int128 == expected.int128)) {
162 if (stx_mo == __ATOMIC_RELAXED)
163 __STORE_128("stxp", dst, desired, ret)
165 __STORE_128("stlxp", dst, desired, ret)
167 /* In the failure case (since 'weak' is ignored and only
168 * weak == 0 is implemented), expected should contain
169 * the atomically read value of dst. This means, 'old'
170 * needs to be stored back to ensure it was read
173 if (stx_mo == __ATOMIC_RELAXED)
174 __STORE_128("stxp", dst, old, ret)
176 __STORE_128("stlxp", dst, old, ret)
181 } while (unlikely(ret));
184 /* Unconditionally updating expected removes an 'if' statement.
185 * expected should already be in register if not in the cache.
189 return (old.int128 == expected.int128);
196 #endif /* _RTE_ATOMIC_ARM64_H_ */