1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Copyright (C) 2012 Regents of the University of California
5 * Copyright (C) 2017 SiFive
8 #ifndef _ASM_RISCV_ATOMIC_H
9 #define _ASM_RISCV_ATOMIC_H
11 #ifdef CONFIG_GENERIC_ATOMIC64
12 # include <asm-generic/atomic64.h>
14 # if (__riscv_xlen < 64)
15 # error "64-bit atomics require XLEN to be at least 64"
19 #include <asm/cmpxchg.h>
20 #include <asm/barrier.h>
22 #define ATOMIC_INIT(i) { (i) }
24 #define __atomic_acquire_fence() \
25 __asm__ __volatile__(RISCV_ACQUIRE_BARRIER "" ::: "memory")
27 #define __atomic_release_fence() \
28 __asm__ __volatile__(RISCV_RELEASE_BARRIER "" ::: "memory");
30 static __always_inline int atomic_read(const atomic_t *v)
32 return READ_ONCE(v->counter);
34 static __always_inline void atomic_set(atomic_t *v, int i)
36 WRITE_ONCE(v->counter, i);
39 #ifndef CONFIG_GENERIC_ATOMIC64
40 #define ATOMIC64_INIT(i) { (i) }
41 static __always_inline s64 atomic64_read(const atomic64_t *v)
43 return READ_ONCE(v->counter);
45 static __always_inline void atomic64_set(atomic64_t *v, s64 i)
47 WRITE_ONCE(v->counter, i);
52 * First, the atomic ops that have no ordering constraints and therefor don't
53 * have the AQ or RL bits set. These don't return anything, so there's only
54 * one version to worry about.
56 #define ATOMIC_OP(op, asm_op, I, asm_type, c_type, prefix) \
57 static __always_inline \
58 void atomic##prefix##_##op(c_type i, atomic##prefix##_t *v) \
60 __asm__ __volatile__ ( \
61 " amo" #asm_op "." #asm_type " zero, %1, %0" \
67 #ifdef CONFIG_GENERIC_ATOMIC64
68 #define ATOMIC_OPS(op, asm_op, I) \
69 ATOMIC_OP (op, asm_op, I, w, int, )
71 #define ATOMIC_OPS(op, asm_op, I) \
72 ATOMIC_OP (op, asm_op, I, w, int, ) \
73 ATOMIC_OP (op, asm_op, I, d, s64, 64)
76 ATOMIC_OPS(add, add, i)
77 ATOMIC_OPS(sub, add, -i)
78 ATOMIC_OPS(and, and, i)
79 ATOMIC_OPS( or, or, i)
80 ATOMIC_OPS(xor, xor, i)
86 * Atomic ops that have ordered, relaxed, acquire, and release variants.
87 * There's two flavors of these: the arithmatic ops have both fetch and return
88 * versions, while the logical ops only have fetch versions.
90 #define ATOMIC_FETCH_OP(op, asm_op, I, asm_type, c_type, prefix) \
91 static __always_inline \
92 c_type atomic##prefix##_fetch_##op##_relaxed(c_type i, \
93 atomic##prefix##_t *v) \
95 register c_type ret; \
96 __asm__ __volatile__ ( \
97 " amo" #asm_op "." #asm_type " %1, %2, %0" \
98 : "+A" (v->counter), "=r" (ret) \
103 static __always_inline \
104 c_type atomic##prefix##_fetch_##op(c_type i, atomic##prefix##_t *v) \
106 register c_type ret; \
107 __asm__ __volatile__ ( \
108 " amo" #asm_op "." #asm_type ".aqrl %1, %2, %0" \
109 : "+A" (v->counter), "=r" (ret) \
115 #define ATOMIC_OP_RETURN(op, asm_op, c_op, I, asm_type, c_type, prefix) \
116 static __always_inline \
117 c_type atomic##prefix##_##op##_return_relaxed(c_type i, \
118 atomic##prefix##_t *v) \
120 return atomic##prefix##_fetch_##op##_relaxed(i, v) c_op I; \
122 static __always_inline \
123 c_type atomic##prefix##_##op##_return(c_type i, atomic##prefix##_t *v) \
125 return atomic##prefix##_fetch_##op(i, v) c_op I; \
128 #ifdef CONFIG_GENERIC_ATOMIC64
129 #define ATOMIC_OPS(op, asm_op, c_op, I) \
130 ATOMIC_FETCH_OP( op, asm_op, I, w, int, ) \
131 ATOMIC_OP_RETURN(op, asm_op, c_op, I, w, int, )
133 #define ATOMIC_OPS(op, asm_op, c_op, I) \
134 ATOMIC_FETCH_OP( op, asm_op, I, w, int, ) \
135 ATOMIC_OP_RETURN(op, asm_op, c_op, I, w, int, ) \
136 ATOMIC_FETCH_OP( op, asm_op, I, d, s64, 64) \
137 ATOMIC_OP_RETURN(op, asm_op, c_op, I, d, s64, 64)
140 ATOMIC_OPS(add, add, +, i)
141 ATOMIC_OPS(sub, add, +, -i)
143 #define atomic_add_return_relaxed atomic_add_return_relaxed
144 #define atomic_sub_return_relaxed atomic_sub_return_relaxed
145 #define atomic_add_return atomic_add_return
146 #define atomic_sub_return atomic_sub_return
148 #define atomic_fetch_add_relaxed atomic_fetch_add_relaxed
149 #define atomic_fetch_sub_relaxed atomic_fetch_sub_relaxed
150 #define atomic_fetch_add atomic_fetch_add
151 #define atomic_fetch_sub atomic_fetch_sub
153 #ifndef CONFIG_GENERIC_ATOMIC64
154 #define atomic64_add_return_relaxed atomic64_add_return_relaxed
155 #define atomic64_sub_return_relaxed atomic64_sub_return_relaxed
156 #define atomic64_add_return atomic64_add_return
157 #define atomic64_sub_return atomic64_sub_return
159 #define atomic64_fetch_add_relaxed atomic64_fetch_add_relaxed
160 #define atomic64_fetch_sub_relaxed atomic64_fetch_sub_relaxed
161 #define atomic64_fetch_add atomic64_fetch_add
162 #define atomic64_fetch_sub atomic64_fetch_sub
167 #ifdef CONFIG_GENERIC_ATOMIC64
168 #define ATOMIC_OPS(op, asm_op, I) \
169 ATOMIC_FETCH_OP(op, asm_op, I, w, int, )
171 #define ATOMIC_OPS(op, asm_op, I) \
172 ATOMIC_FETCH_OP(op, asm_op, I, w, int, ) \
173 ATOMIC_FETCH_OP(op, asm_op, I, d, s64, 64)
176 ATOMIC_OPS(and, and, i)
177 ATOMIC_OPS( or, or, i)
178 ATOMIC_OPS(xor, xor, i)
180 #define atomic_fetch_and_relaxed atomic_fetch_and_relaxed
181 #define atomic_fetch_or_relaxed atomic_fetch_or_relaxed
182 #define atomic_fetch_xor_relaxed atomic_fetch_xor_relaxed
183 #define atomic_fetch_and atomic_fetch_and
184 #define atomic_fetch_or atomic_fetch_or
185 #define atomic_fetch_xor atomic_fetch_xor
187 #ifndef CONFIG_GENERIC_ATOMIC64
188 #define atomic64_fetch_and_relaxed atomic64_fetch_and_relaxed
189 #define atomic64_fetch_or_relaxed atomic64_fetch_or_relaxed
190 #define atomic64_fetch_xor_relaxed atomic64_fetch_xor_relaxed
191 #define atomic64_fetch_and atomic64_fetch_and
192 #define atomic64_fetch_or atomic64_fetch_or
193 #define atomic64_fetch_xor atomic64_fetch_xor
198 #undef ATOMIC_FETCH_OP
199 #undef ATOMIC_OP_RETURN
201 /* This is required to provide a full barrier on success. */
202 static __always_inline int atomic_fetch_add_unless(atomic_t *v, int a, int u)
206 __asm__ __volatile__ (
207 "0: lr.w %[p], %[c]\n"
208 " beq %[p], %[u], 1f\n"
209 " add %[rc], %[p], %[a]\n"
210 " sc.w.rl %[rc], %[rc], %[c]\n"
214 : [p]"=&r" (prev), [rc]"=&r" (rc), [c]"+A" (v->counter)
215 : [a]"r" (a), [u]"r" (u)
219 #define atomic_fetch_add_unless atomic_fetch_add_unless
221 #ifndef CONFIG_GENERIC_ATOMIC64
222 static __always_inline s64 atomic64_fetch_add_unless(atomic64_t *v, s64 a, s64 u)
227 __asm__ __volatile__ (
228 "0: lr.d %[p], %[c]\n"
229 " beq %[p], %[u], 1f\n"
230 " add %[rc], %[p], %[a]\n"
231 " sc.d.rl %[rc], %[rc], %[c]\n"
235 : [p]"=&r" (prev), [rc]"=&r" (rc), [c]"+A" (v->counter)
236 : [a]"r" (a), [u]"r" (u)
240 #define atomic64_fetch_add_unless atomic64_fetch_add_unless
244 * atomic_{cmp,}xchg is required to have exactly the same ordering semantics as
245 * {cmp,}xchg and the operations that return, so they need a full barrier.
247 #define ATOMIC_OP(c_t, prefix, size) \
248 static __always_inline \
249 c_t atomic##prefix##_xchg_relaxed(atomic##prefix##_t *v, c_t n) \
251 return __xchg_relaxed(&(v->counter), n, size); \
253 static __always_inline \
254 c_t atomic##prefix##_xchg_acquire(atomic##prefix##_t *v, c_t n) \
256 return __xchg_acquire(&(v->counter), n, size); \
258 static __always_inline \
259 c_t atomic##prefix##_xchg_release(atomic##prefix##_t *v, c_t n) \
261 return __xchg_release(&(v->counter), n, size); \
263 static __always_inline \
264 c_t atomic##prefix##_xchg(atomic##prefix##_t *v, c_t n) \
266 return __xchg(&(v->counter), n, size); \
268 static __always_inline \
269 c_t atomic##prefix##_cmpxchg_relaxed(atomic##prefix##_t *v, \
272 return __cmpxchg_relaxed(&(v->counter), o, n, size); \
274 static __always_inline \
275 c_t atomic##prefix##_cmpxchg_acquire(atomic##prefix##_t *v, \
278 return __cmpxchg_acquire(&(v->counter), o, n, size); \
280 static __always_inline \
281 c_t atomic##prefix##_cmpxchg_release(atomic##prefix##_t *v, \
284 return __cmpxchg_release(&(v->counter), o, n, size); \
286 static __always_inline \
287 c_t atomic##prefix##_cmpxchg(atomic##prefix##_t *v, c_t o, c_t n) \
289 return __cmpxchg(&(v->counter), o, n, size); \
292 #ifdef CONFIG_GENERIC_ATOMIC64
293 #define ATOMIC_OPS() \
296 #define ATOMIC_OPS() \
297 ATOMIC_OP(int, , 4) \
298 ATOMIC_OP(s64, 64, 8)
303 #define atomic_xchg_relaxed atomic_xchg_relaxed
304 #define atomic_xchg_acquire atomic_xchg_acquire
305 #define atomic_xchg_release atomic_xchg_release
306 #define atomic_xchg atomic_xchg
307 #define atomic_cmpxchg_relaxed atomic_cmpxchg_relaxed
308 #define atomic_cmpxchg_acquire atomic_cmpxchg_acquire
309 #define atomic_cmpxchg_release atomic_cmpxchg_release
310 #define atomic_cmpxchg atomic_cmpxchg
315 static __always_inline int atomic_sub_if_positive(atomic_t *v, int offset)
319 __asm__ __volatile__ (
320 "0: lr.w %[p], %[c]\n"
321 " sub %[rc], %[p], %[o]\n"
323 " sc.w.rl %[rc], %[rc], %[c]\n"
327 : [p]"=&r" (prev), [rc]"=&r" (rc), [c]"+A" (v->counter)
330 return prev - offset;
333 #define atomic_dec_if_positive(v) atomic_sub_if_positive(v, 1)
335 #ifndef CONFIG_GENERIC_ATOMIC64
336 static __always_inline s64 atomic64_sub_if_positive(atomic64_t *v, s64 offset)
341 __asm__ __volatile__ (
342 "0: lr.d %[p], %[c]\n"
343 " sub %[rc], %[p], %[o]\n"
345 " sc.d.rl %[rc], %[rc], %[c]\n"
349 : [p]"=&r" (prev), [rc]"=&r" (rc), [c]"+A" (v->counter)
352 return prev - offset;
355 #define atomic64_dec_if_positive(v) atomic64_sub_if_positive(v, 1)
358 #endif /* _ASM_RISCV_ATOMIC_H */