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>
21 #define __atomic_acquire_fence() \
22 __asm__ __volatile__(RISCV_ACQUIRE_BARRIER "" ::: "memory")
24 #define __atomic_release_fence() \
25 __asm__ __volatile__(RISCV_RELEASE_BARRIER "" ::: "memory");
27 static __always_inline int arch_atomic_read(const atomic_t *v)
29 return READ_ONCE(v->counter);
31 static __always_inline void arch_atomic_set(atomic_t *v, int i)
33 WRITE_ONCE(v->counter, i);
36 #ifndef CONFIG_GENERIC_ATOMIC64
37 #define ATOMIC64_INIT(i) { (i) }
38 static __always_inline s64 arch_atomic64_read(const atomic64_t *v)
40 return READ_ONCE(v->counter);
42 static __always_inline void arch_atomic64_set(atomic64_t *v, s64 i)
44 WRITE_ONCE(v->counter, i);
49 * First, the atomic ops that have no ordering constraints and therefor don't
50 * have the AQ or RL bits set. These don't return anything, so there's only
51 * one version to worry about.
53 #define ATOMIC_OP(op, asm_op, I, asm_type, c_type, prefix) \
54 static __always_inline \
55 void arch_atomic##prefix##_##op(c_type i, atomic##prefix##_t *v) \
57 __asm__ __volatile__ ( \
58 " amo" #asm_op "." #asm_type " zero, %1, %0" \
64 #ifdef CONFIG_GENERIC_ATOMIC64
65 #define ATOMIC_OPS(op, asm_op, I) \
66 ATOMIC_OP (op, asm_op, I, w, int, )
68 #define ATOMIC_OPS(op, asm_op, I) \
69 ATOMIC_OP (op, asm_op, I, w, int, ) \
70 ATOMIC_OP (op, asm_op, I, d, s64, 64)
73 ATOMIC_OPS(add, add, i)
74 ATOMIC_OPS(sub, add, -i)
75 ATOMIC_OPS(and, and, i)
76 ATOMIC_OPS( or, or, i)
77 ATOMIC_OPS(xor, xor, i)
83 * Atomic ops that have ordered, relaxed, acquire, and release variants.
84 * There's two flavors of these: the arithmatic ops have both fetch and return
85 * versions, while the logical ops only have fetch versions.
87 #define ATOMIC_FETCH_OP(op, asm_op, I, asm_type, c_type, prefix) \
88 static __always_inline \
89 c_type arch_atomic##prefix##_fetch_##op##_relaxed(c_type i, \
90 atomic##prefix##_t *v) \
92 register c_type ret; \
93 __asm__ __volatile__ ( \
94 " amo" #asm_op "." #asm_type " %1, %2, %0" \
95 : "+A" (v->counter), "=r" (ret) \
100 static __always_inline \
101 c_type arch_atomic##prefix##_fetch_##op(c_type i, atomic##prefix##_t *v) \
103 register c_type ret; \
104 __asm__ __volatile__ ( \
105 " amo" #asm_op "." #asm_type ".aqrl %1, %2, %0" \
106 : "+A" (v->counter), "=r" (ret) \
112 #define ATOMIC_OP_RETURN(op, asm_op, c_op, I, asm_type, c_type, prefix) \
113 static __always_inline \
114 c_type arch_atomic##prefix##_##op##_return_relaxed(c_type i, \
115 atomic##prefix##_t *v) \
117 return arch_atomic##prefix##_fetch_##op##_relaxed(i, v) c_op I; \
119 static __always_inline \
120 c_type arch_atomic##prefix##_##op##_return(c_type i, atomic##prefix##_t *v) \
122 return arch_atomic##prefix##_fetch_##op(i, v) c_op I; \
125 #ifdef CONFIG_GENERIC_ATOMIC64
126 #define ATOMIC_OPS(op, asm_op, c_op, I) \
127 ATOMIC_FETCH_OP( op, asm_op, I, w, int, ) \
128 ATOMIC_OP_RETURN(op, asm_op, c_op, I, w, int, )
130 #define ATOMIC_OPS(op, asm_op, c_op, I) \
131 ATOMIC_FETCH_OP( op, asm_op, I, w, int, ) \
132 ATOMIC_OP_RETURN(op, asm_op, c_op, I, w, int, ) \
133 ATOMIC_FETCH_OP( op, asm_op, I, d, s64, 64) \
134 ATOMIC_OP_RETURN(op, asm_op, c_op, I, d, s64, 64)
137 ATOMIC_OPS(add, add, +, i)
138 ATOMIC_OPS(sub, add, +, -i)
140 #define arch_atomic_add_return_relaxed arch_atomic_add_return_relaxed
141 #define arch_atomic_sub_return_relaxed arch_atomic_sub_return_relaxed
142 #define arch_atomic_add_return arch_atomic_add_return
143 #define arch_atomic_sub_return arch_atomic_sub_return
145 #define arch_atomic_fetch_add_relaxed arch_atomic_fetch_add_relaxed
146 #define arch_atomic_fetch_sub_relaxed arch_atomic_fetch_sub_relaxed
147 #define arch_atomic_fetch_add arch_atomic_fetch_add
148 #define arch_atomic_fetch_sub arch_atomic_fetch_sub
150 #ifndef CONFIG_GENERIC_ATOMIC64
151 #define arch_atomic64_add_return_relaxed arch_atomic64_add_return_relaxed
152 #define arch_atomic64_sub_return_relaxed arch_atomic64_sub_return_relaxed
153 #define arch_atomic64_add_return arch_atomic64_add_return
154 #define arch_atomic64_sub_return arch_atomic64_sub_return
156 #define arch_atomic64_fetch_add_relaxed arch_atomic64_fetch_add_relaxed
157 #define arch_atomic64_fetch_sub_relaxed arch_atomic64_fetch_sub_relaxed
158 #define arch_atomic64_fetch_add arch_atomic64_fetch_add
159 #define arch_atomic64_fetch_sub arch_atomic64_fetch_sub
164 #ifdef CONFIG_GENERIC_ATOMIC64
165 #define ATOMIC_OPS(op, asm_op, I) \
166 ATOMIC_FETCH_OP(op, asm_op, I, w, int, )
168 #define ATOMIC_OPS(op, asm_op, I) \
169 ATOMIC_FETCH_OP(op, asm_op, I, w, int, ) \
170 ATOMIC_FETCH_OP(op, asm_op, I, d, s64, 64)
173 ATOMIC_OPS(and, and, i)
174 ATOMIC_OPS( or, or, i)
175 ATOMIC_OPS(xor, xor, i)
177 #define arch_atomic_fetch_and_relaxed arch_atomic_fetch_and_relaxed
178 #define arch_atomic_fetch_or_relaxed arch_atomic_fetch_or_relaxed
179 #define arch_atomic_fetch_xor_relaxed arch_atomic_fetch_xor_relaxed
180 #define arch_atomic_fetch_and arch_atomic_fetch_and
181 #define arch_atomic_fetch_or arch_atomic_fetch_or
182 #define arch_atomic_fetch_xor arch_atomic_fetch_xor
184 #ifndef CONFIG_GENERIC_ATOMIC64
185 #define arch_atomic64_fetch_and_relaxed arch_atomic64_fetch_and_relaxed
186 #define arch_atomic64_fetch_or_relaxed arch_atomic64_fetch_or_relaxed
187 #define arch_atomic64_fetch_xor_relaxed arch_atomic64_fetch_xor_relaxed
188 #define arch_atomic64_fetch_and arch_atomic64_fetch_and
189 #define arch_atomic64_fetch_or arch_atomic64_fetch_or
190 #define arch_atomic64_fetch_xor arch_atomic64_fetch_xor
195 #undef ATOMIC_FETCH_OP
196 #undef ATOMIC_OP_RETURN
198 /* This is required to provide a full barrier on success. */
199 static __always_inline int arch_atomic_fetch_add_unless(atomic_t *v, int a, int u)
203 __asm__ __volatile__ (
204 "0: lr.w %[p], %[c]\n"
205 " beq %[p], %[u], 1f\n"
206 " add %[rc], %[p], %[a]\n"
207 " sc.w.rl %[rc], %[rc], %[c]\n"
211 : [p]"=&r" (prev), [rc]"=&r" (rc), [c]"+A" (v->counter)
212 : [a]"r" (a), [u]"r" (u)
216 #define arch_atomic_fetch_add_unless arch_atomic_fetch_add_unless
218 #ifndef CONFIG_GENERIC_ATOMIC64
219 static __always_inline s64 arch_atomic64_fetch_add_unless(atomic64_t *v, s64 a, s64 u)
224 __asm__ __volatile__ (
225 "0: lr.d %[p], %[c]\n"
226 " beq %[p], %[u], 1f\n"
227 " add %[rc], %[p], %[a]\n"
228 " sc.d.rl %[rc], %[rc], %[c]\n"
232 : [p]"=&r" (prev), [rc]"=&r" (rc), [c]"+A" (v->counter)
233 : [a]"r" (a), [u]"r" (u)
237 #define arch_atomic64_fetch_add_unless arch_atomic64_fetch_add_unless
240 static __always_inline bool arch_atomic_inc_unless_negative(atomic_t *v)
244 __asm__ __volatile__ (
245 "0: lr.w %[p], %[c]\n"
247 " addi %[rc], %[p], 1\n"
248 " sc.w.rl %[rc], %[rc], %[c]\n"
252 : [p]"=&r" (prev), [rc]"=&r" (rc), [c]"+A" (v->counter)
258 #define arch_atomic_inc_unless_negative arch_atomic_inc_unless_negative
260 static __always_inline bool arch_atomic_dec_unless_positive(atomic_t *v)
264 __asm__ __volatile__ (
265 "0: lr.w %[p], %[c]\n"
267 " addi %[rc], %[p], -1\n"
268 " sc.w.rl %[rc], %[rc], %[c]\n"
272 : [p]"=&r" (prev), [rc]"=&r" (rc), [c]"+A" (v->counter)
278 #define arch_atomic_dec_unless_positive arch_atomic_dec_unless_positive
280 static __always_inline int arch_atomic_dec_if_positive(atomic_t *v)
284 __asm__ __volatile__ (
285 "0: lr.w %[p], %[c]\n"
286 " addi %[rc], %[p], -1\n"
288 " sc.w.rl %[rc], %[rc], %[c]\n"
292 : [p]"=&r" (prev), [rc]"=&r" (rc), [c]"+A" (v->counter)
298 #define arch_atomic_dec_if_positive arch_atomic_dec_if_positive
300 #ifndef CONFIG_GENERIC_ATOMIC64
301 static __always_inline bool arch_atomic64_inc_unless_negative(atomic64_t *v)
306 __asm__ __volatile__ (
307 "0: lr.d %[p], %[c]\n"
309 " addi %[rc], %[p], 1\n"
310 " sc.d.rl %[rc], %[rc], %[c]\n"
314 : [p]"=&r" (prev), [rc]"=&r" (rc), [c]"+A" (v->counter)
320 #define arch_atomic64_inc_unless_negative arch_atomic64_inc_unless_negative
322 static __always_inline bool arch_atomic64_dec_unless_positive(atomic64_t *v)
327 __asm__ __volatile__ (
328 "0: lr.d %[p], %[c]\n"
330 " addi %[rc], %[p], -1\n"
331 " sc.d.rl %[rc], %[rc], %[c]\n"
335 : [p]"=&r" (prev), [rc]"=&r" (rc), [c]"+A" (v->counter)
341 #define arch_atomic64_dec_unless_positive arch_atomic64_dec_unless_positive
343 static __always_inline s64 arch_atomic64_dec_if_positive(atomic64_t *v)
348 __asm__ __volatile__ (
349 "0: lr.d %[p], %[c]\n"
350 " addi %[rc], %[p], -1\n"
352 " sc.d.rl %[rc], %[rc], %[c]\n"
356 : [p]"=&r" (prev), [rc]"=&r" (rc), [c]"+A" (v->counter)
362 #define arch_atomic64_dec_if_positive arch_atomic64_dec_if_positive
365 #endif /* _ASM_RISCV_ATOMIC_H */