2 * Copyright (c) 2008 Marcel Moolenaar
3 * Copyright (c) 2001 Benno Rice
4 * Copyright (c) 2001 David E. O'Brien
5 * Copyright (c) 1998 Doug Rabson
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
32 #ifndef _MACHINE_ATOMIC_H_
33 #define _MACHINE_ATOMIC_H_
36 #error this file needs sys/cdefs.h as a prerequisite
39 #define __ATOMIC_BARRIER \
40 __asm __volatile("sync" : : : "memory")
47 #define __ATOMIC_ADD_8(p, v, t) \
48 8-bit atomic_add not implemented
50 #define __ATOMIC_ADD_16(p, v, t) \
51 16-bit atomic_add not implemented
53 #define __ATOMIC_ADD_32(p, v, t) \
55 "1: lwarx %0, 0, %2\n" \
57 " stwcx. %0, 0, %2\n" \
59 : "=&r" (t), "=m" (*p) \
60 : "r" (p), "r" (v), "m" (*p) \
64 #define __ATOMIC_ADD_64(p, v, t) \
65 64-bit atomic_add not implemented
67 #define _ATOMIC_ADD(width, suffix, type) \
68 static __inline void \
69 atomic_add_##suffix(volatile type *p, type v) { \
71 __ATOMIC_ADD_##width(p, v, t); \
74 static __inline void \
75 atomic_add_acq_##suffix(volatile type *p, type v) { \
77 __ATOMIC_ADD_##width(p, v, t); \
81 static __inline void \
82 atomic_add_rel_##suffix(volatile type *p, type v) { \
85 __ATOMIC_ADD_##width(p, v, t); \
90 _ATOMIC_ADD(8, 8, uint8_t)
91 _ATOMIC_ADD(8, char, u_char)
92 _ATOMIC_ADD(16, 16, uint16_t)
93 _ATOMIC_ADD(16, short, u_short)
95 _ATOMIC_ADD(32, 32, uint32_t)
96 _ATOMIC_ADD(32, int, u_int)
97 _ATOMIC_ADD(32, long, u_long)
98 _ATOMIC_ADD(32, ptr, uintptr_t)
100 _ATOMIC_ADD(64, 64, uint64_t)
101 _ATOMIC_ADD(64, long_long, u_long_long)
105 #undef __ATOMIC_ADD_64
106 #undef __ATOMIC_ADD_32
107 #undef __ATOMIC_ADD_16
108 #undef __ATOMIC_ADD_8
115 #define __ATOMIC_CLEAR_8(p, v, t) \
116 8-bit atomic_add not implemented
118 #define __ATOMIC_CLEAR_16(p, v, t) \
119 16-bit atomic_add not implemented
121 #define __ATOMIC_CLEAR_32(p, v, t) \
123 "1: lwarx %0, 0, %2\n" \
124 " andc %0, %0, %3\n" \
125 " stwcx. %0, 0, %2\n" \
127 : "=&r" (t), "=m" (*p) \
128 : "r" (p), "r" (v), "m" (*p) \
130 /* __ATOMIC_CLEAR_32 */
132 #define __ATOMIC_CLEAR_64(p, v, t) \
133 64-bit atomic_add not implemented
135 #define _ATOMIC_CLEAR(width, suffix, type) \
136 static __inline void \
137 atomic_clear_##suffix(volatile type *p, type v) { \
139 __ATOMIC_CLEAR_##width(p, v, t); \
142 static __inline void \
143 atomic_clear_acq_##suffix(volatile type *p, type v) { \
145 __ATOMIC_CLEAR_##width(p, v, t); \
149 static __inline void \
150 atomic_clear_rel_##suffix(volatile type *p, type v) { \
153 __ATOMIC_CLEAR_##width(p, v, t); \
158 _ATOMIC_CLEAR(8, 8, uint8_t)
159 _ATOMIC_CLEAR(8, char, u_char)
160 _ATOMIC_CLEAR(16, 16, uint16_t)
161 _ATOMIC_CLEAR(16, short, u_short)
163 _ATOMIC_CLEAR(32, 32, uint32_t)
164 _ATOMIC_CLEAR(32, int, u_int)
165 _ATOMIC_CLEAR(32, long, u_long)
166 _ATOMIC_CLEAR(32, ptr, uintptr_t)
168 _ATOMIC_CLEAR(64, 64, uint64_t)
169 _ATOMIC_CLEAR(64, long_long, u_long_long)
173 #undef __ATOMIC_CLEAR_64
174 #undef __ATOMIC_CLEAR_32
175 #undef __ATOMIC_CLEAR_16
176 #undef __ATOMIC_CLEAR_8
179 * atomic_cmpset(p, o, n)
181 /* TODO -- see below */
186 /* TODO -- see below */
189 * atomic_readandclear(p)
191 /* TODO -- see below */
198 #define __ATOMIC_SET_8(p, v, t) \
199 8-bit atomic_set not implemented
201 #define __ATOMIC_SET_16(p, v, t) \
202 16-bit atomic_set not implemented
204 #define __ATOMIC_SET_32(p, v, t) \
206 "1: lwarx %0, 0, %2\n" \
208 " stwcx. %0, 0, %2\n" \
210 : "=&r" (t), "=m" (*p) \
211 : "r" (p), "r" (v), "m" (*p) \
213 /* __ATOMIC_SET_32 */
215 #define __ATOMIC_SET_64(p, v, t) \
216 64-bit atomic_set not implemented
218 #define _ATOMIC_SET(width, suffix, type) \
219 static __inline void \
220 atomic_set_##suffix(volatile type *p, type v) { \
222 __ATOMIC_SET_##width(p, v, t); \
225 static __inline void \
226 atomic_set_acq_##suffix(volatile type *p, type v) { \
228 __ATOMIC_SET_##width(p, v, t); \
232 static __inline void \
233 atomic_set_rel_##suffix(volatile type *p, type v) { \
236 __ATOMIC_SET_##width(p, v, t); \
241 _ATOMIC_SET(8, 8, uint8_t)
242 _ATOMIC_SET(8, char, u_char)
243 _ATOMIC_SET(16, 16, uint16_t)
244 _ATOMIC_SET(16, short, u_short)
246 _ATOMIC_SET(32, 32, uint32_t)
247 _ATOMIC_SET(32, int, u_int)
248 _ATOMIC_SET(32, long, u_long)
249 _ATOMIC_SET(32, ptr, uintptr_t)
251 _ATOMIC_SET(64, 64, uint64_t)
252 _ATOMIC_SET(64, long_long, u_long_long)
256 #undef __ATOMIC_SET_64
257 #undef __ATOMIC_SET_32
258 #undef __ATOMIC_SET_16
259 #undef __ATOMIC_SET_8
262 * atomic_subtract(p, v)
266 #define __ATOMIC_SUBTRACT_8(p, v, t) \
267 8-bit atomic_subtract not implemented
269 #define __ATOMIC_SUBTRACT_16(p, v, t) \
270 16-bit atomic_subtract not implemented
272 #define __ATOMIC_SUBTRACT_32(p, v, t) \
274 "1: lwarx %0, 0, %2\n" \
275 " subf %0, %3, %0\n" \
276 " stwcx. %0, 0, %2\n" \
278 : "=&r" (t), "=m" (*p) \
279 : "r" (p), "r" (v), "m" (*p) \
281 /* __ATOMIC_SUBTRACT_32 */
283 #define __ATOMIC_SUBTRACT_64(p, v, t) \
284 64-bit atomic_subtract not implemented
286 #define _ATOMIC_SUBTRACT(width, suffix, type) \
287 static __inline void \
288 atomic_subtract_##suffix(volatile type *p, type v) { \
290 __ATOMIC_SUBTRACT_##width(p, v, t); \
293 static __inline void \
294 atomic_subtract_acq_##suffix(volatile type *p, type v) { \
296 __ATOMIC_SUBTRACT_##width(p, v, t); \
300 static __inline void \
301 atomic_subtract_rel_##suffix(volatile type *p, type v) { \
304 __ATOMIC_SUBTRACT_##width(p, v, t); \
306 /* _ATOMIC_SUBTRACT */
309 _ATOMIC_SUBTRACT(8, 8, uint8_t)
310 _ATOMIC_SUBTRACT(8, char, u_char)
311 _ATOMIC_SUBTRACT(16, 16, uint16_t)
312 _ATOMIC_SUBTRACT(16, short, u_short)
314 _ATOMIC_SUBTRACT(32, 32, uint32_t)
315 _ATOMIC_SUBTRACT(32, int, u_int)
316 _ATOMIC_SUBTRACT(32, long, u_long)
317 _ATOMIC_SUBTRACT(32, ptr, uintptr_t)
319 _ATOMIC_SUBTRACT(64, 64, uint64_t)
320 _ATOMIC_SUBTRACT(64, long_long, u_long_long)
323 #undef _ATOMIC_SUBTRACT
324 #undef __ATOMIC_SUBTRACT_64
325 #undef __ATOMIC_SUBTRACT_32
326 #undef __ATOMIC_SUBTRACT_16
327 #undef __ATOMIC_SUBTRACT_8
330 * atomic_store_rel(p, v)
332 /* TODO -- see below */
335 * Old/original implementations that still need revisiting.
338 static __inline uint32_t
339 atomic_readandclear_32(volatile uint32_t *addr)
341 uint32_t result,temp;
343 #ifdef __GNUCLIKE_ASM
345 "\tsync\n" /* drain writes */
346 "1:\tlwarx %0, 0, %3\n\t" /* load old value */
347 "li %1, 0\n\t" /* load new value */
348 "stwcx. %1, 0, %3\n\t" /* attempt to store */
349 "bne- 1b\n\t" /* spin if failed */
350 : "=&r"(result), "=&r"(temp), "=m" (*addr)
351 : "r" (addr), "m" (*addr)
358 #define atomic_readandclear_int atomic_readandclear_32
359 #define atomic_readandclear_long atomic_readandclear_32
360 #define atomic_readandclear_ptr atomic_readandclear_32
363 * We assume that a = b will do atomic loads and stores.
365 #define ATOMIC_STORE_LOAD(TYPE, WIDTH) \
366 static __inline u_##TYPE \
367 atomic_load_acq_##WIDTH(volatile u_##TYPE *p) \
376 static __inline void \
377 atomic_store_rel_##WIDTH(volatile u_##TYPE *p, u_##TYPE v) \
383 static __inline u_##TYPE \
384 atomic_load_acq_##TYPE(volatile u_##TYPE *p) \
393 static __inline void \
394 atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v) \
400 ATOMIC_STORE_LOAD(char, 8)
401 ATOMIC_STORE_LOAD(short, 16)
402 ATOMIC_STORE_LOAD(int, 32)
404 #define atomic_load_acq_long atomic_load_acq_32
405 #define atomic_store_rel_long atomic_store_rel_32
406 #define atomic_load_acq_ptr atomic_load_acq_32
407 #define atomic_store_rel_ptr atomic_store_rel_32
409 #undef ATOMIC_STORE_LOAD
412 * Atomically compare the value stored at *p with cmpval and if the
413 * two values are equal, update the value of *p with newval. Returns
414 * zero if the compare failed, nonzero otherwise.
416 static __inline uint32_t
417 atomic_cmpset_32(volatile uint32_t* p, uint32_t cmpval, uint32_t newval)
421 #ifdef __GNUCLIKE_ASM
423 "1:\tlwarx %0, 0, %2\n\t" /* load old value */
424 "cmplw %3, %0\n\t" /* compare */
425 "bne 2f\n\t" /* exit if not equal */
426 "stwcx. %4, 0, %2\n\t" /* attempt to store */
427 "bne- 1b\n\t" /* spin if failed */
428 "li %0, 1\n\t" /* success - retval = 1 */
429 "b 3f\n\t" /* we've succeeded */
431 "stwcx. %0, 0, %2\n\t" /* clear reservation (74xx) */
432 "li %0, 0\n\t" /* failure - retval = 0 */
434 : "=&r" (ret), "=m" (*p)
435 : "r" (p), "r" (cmpval), "r" (newval), "m" (*p)
442 static __inline u_long
443 atomic_cmpset_long(volatile u_long* p, u_long cmpval, u_long newval)
447 #ifdef __GNUCLIKE_ASM
449 "1:\tlwarx %0, 0, %2\n\t" /* load old value */
450 "cmplw %3, %0\n\t" /* compare */
451 "bne 2f\n\t" /* exit if not equal */
452 "stwcx. %4, 0, %2\n\t" /* attempt to store */
453 "bne- 1b\n\t" /* spin if failed */
454 "li %0, 1\n\t" /* success - retval = 1 */
455 "b 3f\n\t" /* we've succeeded */
457 "stwcx. %0, 0, %2\n\t" /* clear reservation (74xx) */
458 "li %0, 0\n\t" /* failure - retval = 0 */
460 : "=&r" (ret), "=m" (*p)
461 : "r" (p), "r" (cmpval), "r" (newval), "m" (*p)
468 #define atomic_cmpset_int atomic_cmpset_32
470 #define atomic_cmpset_ptr(dst, old, new) \
471 atomic_cmpset_32((volatile u_int *)(dst), (u_int)(old), (u_int)(new))
473 static __inline uint32_t
474 atomic_cmpset_acq_32(volatile uint32_t *p, uint32_t cmpval, uint32_t newval)
478 retval = atomic_cmpset_32(p, cmpval, newval);
483 static __inline uint32_t
484 atomic_cmpset_rel_32(volatile uint32_t *p, uint32_t cmpval, uint32_t newval)
487 return (atomic_cmpset_32(p, cmpval, newval));
490 static __inline u_long
491 atomic_cmpset_acq_long(volatile u_long *p, u_long cmpval, u_long newval)
495 retval = atomic_cmpset_long(p, cmpval, newval);
500 static __inline uint32_t
501 atomic_cmpset_rel_long(volatile u_long *p, u_long cmpval, u_long newval)
504 return (atomic_cmpset_long(p, cmpval, newval));
507 #define atomic_cmpset_acq_int atomic_cmpset_acq_32
508 #define atomic_cmpset_rel_int atomic_cmpset_rel_32
510 #define atomic_cmpset_acq_ptr(dst, old, new) \
511 atomic_cmpset_acq_32((volatile u_int *)(dst), (u_int)(old), (u_int)(new))
512 #define atomic_cmpset_rel_ptr(dst, old, new) \
513 atomic_cmpset_rel_32((volatile u_int *)(dst), (u_int)(old), (u_int)(new))
515 static __inline uint32_t
516 atomic_fetchadd_32(volatile uint32_t *p, uint32_t v)
522 } while (!atomic_cmpset_32(p, value, value + v));
526 #define atomic_fetchadd_int atomic_fetchadd_32
527 #define atomic_fetchadd_long(p, v) \
528 (u_long)atomic_fetchadd_32((volatile u_int *)(p), (u_int)(v))
530 #undef __ATOMIC_BARRIER
532 #endif /* ! _MACHINE_ATOMIC_H_ */