2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998 Doug Rabson.
5 * Copyright (c) 2001 Jake Burkholder.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * from: FreeBSD: src/sys/i386/include/atomic.h,v 1.20 2001/02/11
33 #ifndef _MACHINE_ATOMIC_H_
34 #define _MACHINE_ATOMIC_H_
36 #include <machine/cpufunc.h>
38 #define mb() __asm__ __volatile__ ("membar #MemIssue": : :"memory")
42 #include <sys/atomic_common.h>
44 /* Userland needs different ASI's. */
46 #define __ASI_ATOMIC ASI_N
48 #define __ASI_ATOMIC ASI_P
52 * Various simple arithmetic on memory which is atomic in the presence
53 * of interrupts and multiple processors. See atomic(9) for details.
54 * Note that efficient hardware support exists only for the 32 and 64
55 * bit variants; the 8 and 16 bit versions are not provided and should
56 * not be used in MI code.
58 * This implementation takes advantage of the fact that the sparc64
59 * cas instruction is both a load and a store. The loop is often coded
65 * } while (cas(p, expect, new) != expect);
67 * which performs an unnnecessary load on each iteration that the cas
68 * operation fails. Modified as follows:
73 * result = cas(p, expect, new);
74 * if (result == expect)
79 * the return value of cas is used to avoid the extra reload.
81 * We only include a memory barrier in the rel variants as in total store
82 * order which we use for running the kernel and all of the userland atomic
83 * loads and stores behave as if the were followed by a membar with a mask
84 * of #LoadLoad | #LoadStore | #StoreStore. In order to be also sufficient
85 * for use of relaxed memory ordering, the atomic_cas() in the acq variants
86 * additionally would have to be followed by a membar #LoadLoad | #LoadStore.
87 * Due to the suggested assembly syntax of the membar operands containing a
88 * # character, they cannot be used in macros. The cmask and mmask bits thus
89 * are hard coded in machine/cpufunc.h and used here through macros.
90 * Hopefully the bit numbers won't change in the future.
93 #define itype(sz) uint ## sz ## _t
95 #define atomic_cas_32(p, e, s) casa((p), (e), (s), __ASI_ATOMIC)
96 #define atomic_cas_64(p, e, s) casxa((p), (e), (s), __ASI_ATOMIC)
98 #define atomic_cas(p, e, s, sz) \
99 atomic_cas_ ## sz((p), (e), (s))
101 #define atomic_cas_acq(p, e, s, sz) ({ \
103 v = atomic_cas((p), (e), (s), sz); \
104 __compiler_membar(); \
108 #define atomic_cas_rel(p, e, s, sz) ({ \
110 membar(LoadStore | StoreStore); \
111 v = atomic_cas((p), (e), (s), sz); \
115 #define atomic_op(p, op, v, sz) ({ \
117 for (e = *(volatile itype(sz) *)(p);; e = r) { \
119 r = atomic_cas_ ## sz((p), e, s); \
126 #define atomic_op_acq(p, op, v, sz) ({ \
128 t = atomic_op((p), op, (v), sz); \
129 __compiler_membar(); \
133 #define atomic_op_rel(p, op, v, sz) ({ \
135 membar(LoadStore | StoreStore); \
136 t = atomic_op((p), op, (v), sz); \
140 #define atomic_ld_acq(p, sz) ({ \
142 v = atomic_cas((p), 0, 0, sz); \
143 __compiler_membar(); \
147 #define atomic_ld_clear(p, sz) ({ \
149 for (e = *(volatile itype(sz) *)(p);; e = r) { \
150 r = atomic_cas((p), e, 0, sz); \
157 #define atomic_st(p, v, sz) ({ \
159 for (e = *(volatile itype(sz) *)(p);; e = r) { \
160 r = atomic_cas((p), e, (v), sz); \
167 #define atomic_st_acq(p, v, sz) do { \
168 atomic_st((p), (v), sz); \
169 __compiler_membar(); \
172 #define atomic_st_rel(p, v, sz) do { \
173 membar(LoadStore | StoreStore); \
174 atomic_st((p), (v), sz); \
177 #define ATOMIC_GEN(name, ptype, vtype, atype, sz) \
179 static __inline vtype \
180 atomic_add_ ## name(volatile ptype p, atype v) \
182 return ((vtype)atomic_op((p), +, (v), sz)); \
184 static __inline vtype \
185 atomic_add_acq_ ## name(volatile ptype p, atype v) \
187 return ((vtype)atomic_op_acq((p), +, (v), sz)); \
189 static __inline vtype \
190 atomic_add_rel_ ## name(volatile ptype p, atype v) \
192 return ((vtype)atomic_op_rel((p), +, (v), sz)); \
195 static __inline vtype \
196 atomic_clear_ ## name(volatile ptype p, atype v) \
198 return ((vtype)atomic_op((p), &, ~(v), sz)); \
200 static __inline vtype \
201 atomic_clear_acq_ ## name(volatile ptype p, atype v) \
203 return ((vtype)atomic_op_acq((p), &, ~(v), sz)); \
205 static __inline vtype \
206 atomic_clear_rel_ ## name(volatile ptype p, atype v) \
208 return ((vtype)atomic_op_rel((p), &, ~(v), sz)); \
211 static __inline int \
212 atomic_cmpset_ ## name(volatile ptype p, vtype e, vtype s) \
214 return (((vtype)atomic_cas((p), (e), (s), sz)) == (e)); \
216 static __inline int \
217 atomic_cmpset_acq_ ## name(volatile ptype p, vtype e, vtype s) \
219 return (((vtype)atomic_cas_acq((p), (e), (s), sz)) == (e)); \
221 static __inline int \
222 atomic_cmpset_rel_ ## name(volatile ptype p, vtype e, vtype s) \
224 return (((vtype)atomic_cas_rel((p), (e), (s), sz)) == (e)); \
227 static __inline int \
228 atomic_fcmpset_ ## name(volatile ptype p, vtype *ep, vtype s) \
232 t = (vtype)atomic_cas((p), (*ep), (s), sz); \
238 static __inline int \
239 atomic_fcmpset_acq_ ## name(volatile ptype p, vtype *ep, vtype s) \
243 t = (vtype)atomic_cas_acq((p), (*ep), (s), sz); \
249 static __inline int \
250 atomic_fcmpset_rel_ ## name(volatile ptype p, vtype *ep, vtype s) \
254 t = (vtype)atomic_cas_rel((p), (*ep), (s), sz); \
261 static __inline vtype \
262 atomic_load_acq_ ## name(volatile ptype p) \
264 return ((vtype)atomic_cas_acq((p), 0, 0, sz)); \
267 static __inline vtype \
268 atomic_readandclear_ ## name(volatile ptype p) \
270 return ((vtype)atomic_ld_clear((p), sz)); \
273 static __inline vtype \
274 atomic_set_ ## name(volatile ptype p, atype v) \
276 return ((vtype)atomic_op((p), |, (v), sz)); \
278 static __inline vtype \
279 atomic_set_acq_ ## name(volatile ptype p, atype v) \
281 return ((vtype)atomic_op_acq((p), |, (v), sz)); \
283 static __inline vtype \
284 atomic_set_rel_ ## name(volatile ptype p, atype v) \
286 return ((vtype)atomic_op_rel((p), |, (v), sz)); \
289 static __inline vtype \
290 atomic_subtract_ ## name(volatile ptype p, atype v) \
292 return ((vtype)atomic_op((p), -, (v), sz)); \
294 static __inline vtype \
295 atomic_subtract_acq_ ## name(volatile ptype p, atype v) \
297 return ((vtype)atomic_op_acq((p), -, (v), sz)); \
299 static __inline vtype \
300 atomic_subtract_rel_ ## name(volatile ptype p, atype v) \
302 return ((vtype)atomic_op_rel((p), -, (v), sz)); \
305 static __inline void \
306 atomic_store_acq_ ## name(volatile ptype p, vtype v) \
308 atomic_st_acq((p), (v), sz); \
310 static __inline void \
311 atomic_store_rel_ ## name(volatile ptype p, vtype v) \
313 atomic_st_rel((p), (v), sz); \
316 static __inline vtype \
317 atomic_swap_ ## name(volatile ptype p, vtype v) \
319 return ((vtype)atomic_st((p), (v), sz)); \
323 atomic_thread_fence_acq(void)
330 atomic_thread_fence_rel(void)
337 atomic_thread_fence_acq_rel(void)
344 atomic_thread_fence_seq_cst(void)
347 membar(LoadLoad | LoadStore | StoreStore | StoreLoad);
351 ATOMIC_GEN(int, u_int *, u_int, u_int, 32);
352 ATOMIC_GEN(32, uint32_t *, uint32_t, uint32_t, 32);
354 ATOMIC_GEN(long, u_long *, u_long, u_long, 64);
355 ATOMIC_GEN(64, uint64_t *, uint64_t, uint64_t, 64);
357 ATOMIC_GEN(ptr, uintptr_t *, uintptr_t, uintptr_t, 64);
359 #define atomic_fetchadd_int atomic_add_int
360 #define atomic_fetchadd_32 atomic_add_32
361 #define atomic_fetchadd_long atomic_add_long
362 #define atomic_fetchadd_64 atomic_add_64
366 #undef atomic_cas_acq
367 #undef atomic_cas_rel
372 #undef atomic_ld_clear
377 #endif /* !_MACHINE_ATOMIC_H_ */