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")
42 #define mb() __ATOMIC_BARRIER
51 #define __ATOMIC_ADD_8(p, v, t) \
52 8-bit atomic_add not implemented
54 #define __ATOMIC_ADD_16(p, v, t) \
55 16-bit atomic_add not implemented
57 #define __ATOMIC_ADD_32(p, v, t) \
59 "1: lwarx %0, 0, %2\n" \
61 " stwcx. %0, 0, %2\n" \
63 : "=&r" (t), "=m" (*p) \
64 : "r" (p), "r" (v), "m" (*p) \
68 #define __ATOMIC_ADD_64(p, v, t) \
69 64-bit atomic_add not implemented
71 #define _ATOMIC_ADD(width, suffix, type) \
72 static __inline void \
73 atomic_add_##suffix(volatile type *p, type v) { \
75 __ATOMIC_ADD_##width(p, v, t); \
78 static __inline void \
79 atomic_add_acq_##suffix(volatile type *p, type v) { \
81 __ATOMIC_ADD_##width(p, v, t); \
85 static __inline void \
86 atomic_add_rel_##suffix(volatile type *p, type v) { \
89 __ATOMIC_ADD_##width(p, v, t); \
94 _ATOMIC_ADD(8, 8, uint8_t)
95 _ATOMIC_ADD(8, char, u_char)
96 _ATOMIC_ADD(16, 16, uint16_t)
97 _ATOMIC_ADD(16, short, u_short)
99 _ATOMIC_ADD(32, 32, uint32_t)
100 _ATOMIC_ADD(32, int, u_int)
101 _ATOMIC_ADD(32, long, u_long)
102 _ATOMIC_ADD(32, ptr, uintptr_t)
104 _ATOMIC_ADD(64, 64, uint64_t)
105 _ATOMIC_ADD(64, long_long, u_long_long)
109 #undef __ATOMIC_ADD_64
110 #undef __ATOMIC_ADD_32
111 #undef __ATOMIC_ADD_16
112 #undef __ATOMIC_ADD_8
119 #define __ATOMIC_CLEAR_8(p, v, t) \
120 8-bit atomic_clear not implemented
122 #define __ATOMIC_CLEAR_16(p, v, t) \
123 16-bit atomic_clear not implemented
125 #define __ATOMIC_CLEAR_32(p, v, t) \
127 "1: lwarx %0, 0, %2\n" \
128 " andc %0, %0, %3\n" \
129 " stwcx. %0, 0, %2\n" \
131 : "=&r" (t), "=m" (*p) \
132 : "r" (p), "r" (v), "m" (*p) \
134 /* __ATOMIC_CLEAR_32 */
136 #define __ATOMIC_CLEAR_64(p, v, t) \
137 64-bit atomic_clear not implemented
139 #define _ATOMIC_CLEAR(width, suffix, type) \
140 static __inline void \
141 atomic_clear_##suffix(volatile type *p, type v) { \
143 __ATOMIC_CLEAR_##width(p, v, t); \
146 static __inline void \
147 atomic_clear_acq_##suffix(volatile type *p, type v) { \
149 __ATOMIC_CLEAR_##width(p, v, t); \
153 static __inline void \
154 atomic_clear_rel_##suffix(volatile type *p, type v) { \
157 __ATOMIC_CLEAR_##width(p, v, t); \
162 _ATOMIC_CLEAR(8, 8, uint8_t)
163 _ATOMIC_CLEAR(8, char, u_char)
164 _ATOMIC_CLEAR(16, 16, uint16_t)
165 _ATOMIC_CLEAR(16, short, u_short)
167 _ATOMIC_CLEAR(32, 32, uint32_t)
168 _ATOMIC_CLEAR(32, int, u_int)
169 _ATOMIC_CLEAR(32, long, u_long)
170 _ATOMIC_CLEAR(32, ptr, uintptr_t)
172 _ATOMIC_CLEAR(64, 64, uint64_t)
173 _ATOMIC_CLEAR(64, long_long, u_long_long)
177 #undef __ATOMIC_CLEAR_64
178 #undef __ATOMIC_CLEAR_32
179 #undef __ATOMIC_CLEAR_16
180 #undef __ATOMIC_CLEAR_8
183 * atomic_cmpset(p, o, n)
185 /* TODO -- see below */
190 /* TODO -- see below */
193 * atomic_readandclear(p)
195 /* TODO -- see below */
202 #define __ATOMIC_SET_8(p, v, t) \
203 8-bit atomic_set not implemented
205 #define __ATOMIC_SET_16(p, v, t) \
206 16-bit atomic_set not implemented
208 #define __ATOMIC_SET_32(p, v, t) \
210 "1: lwarx %0, 0, %2\n" \
212 " stwcx. %0, 0, %2\n" \
214 : "=&r" (t), "=m" (*p) \
215 : "r" (p), "r" (v), "m" (*p) \
217 /* __ATOMIC_SET_32 */
219 #define __ATOMIC_SET_64(p, v, t) \
220 64-bit atomic_set not implemented
222 #define _ATOMIC_SET(width, suffix, type) \
223 static __inline void \
224 atomic_set_##suffix(volatile type *p, type v) { \
226 __ATOMIC_SET_##width(p, v, t); \
229 static __inline void \
230 atomic_set_acq_##suffix(volatile type *p, type v) { \
232 __ATOMIC_SET_##width(p, v, t); \
236 static __inline void \
237 atomic_set_rel_##suffix(volatile type *p, type v) { \
240 __ATOMIC_SET_##width(p, v, t); \
245 _ATOMIC_SET(8, 8, uint8_t)
246 _ATOMIC_SET(8, char, u_char)
247 _ATOMIC_SET(16, 16, uint16_t)
248 _ATOMIC_SET(16, short, u_short)
250 _ATOMIC_SET(32, 32, uint32_t)
251 _ATOMIC_SET(32, int, u_int)
252 _ATOMIC_SET(32, long, u_long)
253 _ATOMIC_SET(32, ptr, uintptr_t)
255 _ATOMIC_SET(64, 64, uint64_t)
256 _ATOMIC_SET(64, long_long, u_long_long)
260 #undef __ATOMIC_SET_64
261 #undef __ATOMIC_SET_32
262 #undef __ATOMIC_SET_16
263 #undef __ATOMIC_SET_8
266 * atomic_subtract(p, v)
270 #define __ATOMIC_SUBTRACT_8(p, v, t) \
271 8-bit atomic_subtract not implemented
273 #define __ATOMIC_SUBTRACT_16(p, v, t) \
274 16-bit atomic_subtract not implemented
276 #define __ATOMIC_SUBTRACT_32(p, v, t) \
278 "1: lwarx %0, 0, %2\n" \
279 " subf %0, %3, %0\n" \
280 " stwcx. %0, 0, %2\n" \
282 : "=&r" (t), "=m" (*p) \
283 : "r" (p), "r" (v), "m" (*p) \
285 /* __ATOMIC_SUBTRACT_32 */
287 #define __ATOMIC_SUBTRACT_64(p, v, t) \
288 64-bit atomic_subtract not implemented
290 #define _ATOMIC_SUBTRACT(width, suffix, type) \
291 static __inline void \
292 atomic_subtract_##suffix(volatile type *p, type v) { \
294 __ATOMIC_SUBTRACT_##width(p, v, t); \
297 static __inline void \
298 atomic_subtract_acq_##suffix(volatile type *p, type v) { \
300 __ATOMIC_SUBTRACT_##width(p, v, t); \
304 static __inline void \
305 atomic_subtract_rel_##suffix(volatile type *p, type v) { \
308 __ATOMIC_SUBTRACT_##width(p, v, t); \
310 /* _ATOMIC_SUBTRACT */
313 _ATOMIC_SUBTRACT(8, 8, uint8_t)
314 _ATOMIC_SUBTRACT(8, char, u_char)
315 _ATOMIC_SUBTRACT(16, 16, uint16_t)
316 _ATOMIC_SUBTRACT(16, short, u_short)
318 _ATOMIC_SUBTRACT(32, 32, uint32_t)
319 _ATOMIC_SUBTRACT(32, int, u_int)
320 _ATOMIC_SUBTRACT(32, long, u_long)
321 _ATOMIC_SUBTRACT(32, ptr, uintptr_t)
323 _ATOMIC_SUBTRACT(64, 64, uint64_t)
324 _ATOMIC_SUBTRACT(64, long_long, u_long_long)
327 #undef _ATOMIC_SUBTRACT
328 #undef __ATOMIC_SUBTRACT_64
329 #undef __ATOMIC_SUBTRACT_32
330 #undef __ATOMIC_SUBTRACT_16
331 #undef __ATOMIC_SUBTRACT_8
334 * atomic_store_rel(p, v)
336 /* TODO -- see below */
339 * Old/original implementations that still need revisiting.
342 static __inline uint32_t
343 atomic_readandclear_32(volatile uint32_t *addr)
345 uint32_t result,temp;
347 #ifdef __GNUCLIKE_ASM
349 "\tsync\n" /* drain writes */
350 "1:\tlwarx %0, 0, %3\n\t" /* load old value */
351 "li %1, 0\n\t" /* load new value */
352 "stwcx. %1, 0, %3\n\t" /* attempt to store */
353 "bne- 1b\n\t" /* spin if failed */
354 : "=&r"(result), "=&r"(temp), "=m" (*addr)
355 : "r" (addr), "m" (*addr)
362 #define atomic_readandclear_int atomic_readandclear_32
363 #define atomic_readandclear_long atomic_readandclear_32
364 #define atomic_readandclear_ptr atomic_readandclear_32
367 * We assume that a = b will do atomic loads and stores.
369 #define ATOMIC_STORE_LOAD(TYPE, WIDTH) \
370 static __inline u_##TYPE \
371 atomic_load_acq_##WIDTH(volatile u_##TYPE *p) \
380 static __inline void \
381 atomic_store_rel_##WIDTH(volatile u_##TYPE *p, u_##TYPE v) \
387 static __inline u_##TYPE \
388 atomic_load_acq_##TYPE(volatile u_##TYPE *p) \
397 static __inline void \
398 atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v) \
404 ATOMIC_STORE_LOAD(char, 8)
405 ATOMIC_STORE_LOAD(short, 16)
406 ATOMIC_STORE_LOAD(int, 32)
408 #define atomic_load_acq_long atomic_load_acq_32
409 #define atomic_store_rel_long atomic_store_rel_32
410 #define atomic_load_acq_ptr atomic_load_acq_32
411 #define atomic_store_rel_ptr atomic_store_rel_32
413 #undef ATOMIC_STORE_LOAD
416 * Atomically compare the value stored at *p with cmpval and if the
417 * two values are equal, update the value of *p with newval. Returns
418 * zero if the compare failed, nonzero otherwise.
420 static __inline uint32_t
421 atomic_cmpset_32(volatile uint32_t* p, uint32_t cmpval, uint32_t newval)
425 #ifdef __GNUCLIKE_ASM
427 "1:\tlwarx %0, 0, %2\n\t" /* load old value */
428 "cmplw %3, %0\n\t" /* compare */
429 "bne 2f\n\t" /* exit if not equal */
430 "stwcx. %4, 0, %2\n\t" /* attempt to store */
431 "bne- 1b\n\t" /* spin if failed */
432 "li %0, 1\n\t" /* success - retval = 1 */
433 "b 3f\n\t" /* we've succeeded */
435 "stwcx. %0, 0, %2\n\t" /* clear reservation (74xx) */
436 "li %0, 0\n\t" /* failure - retval = 0 */
438 : "=&r" (ret), "=m" (*p)
439 : "r" (p), "r" (cmpval), "r" (newval), "m" (*p)
446 static __inline u_long
447 atomic_cmpset_long(volatile u_long* p, u_long cmpval, u_long newval)
451 #ifdef __GNUCLIKE_ASM
453 "1:\tlwarx %0, 0, %2\n\t" /* load old value */
454 "cmplw %3, %0\n\t" /* compare */
455 "bne 2f\n\t" /* exit if not equal */
456 "stwcx. %4, 0, %2\n\t" /* attempt to store */
457 "bne- 1b\n\t" /* spin if failed */
458 "li %0, 1\n\t" /* success - retval = 1 */
459 "b 3f\n\t" /* we've succeeded */
461 "stwcx. %0, 0, %2\n\t" /* clear reservation (74xx) */
462 "li %0, 0\n\t" /* failure - retval = 0 */
464 : "=&r" (ret), "=m" (*p)
465 : "r" (p), "r" (cmpval), "r" (newval), "m" (*p)
472 #define atomic_cmpset_int atomic_cmpset_32
474 #define atomic_cmpset_ptr(dst, old, new) \
475 atomic_cmpset_32((volatile u_int *)(dst), (u_int)(old), (u_int)(new))
477 static __inline uint32_t
478 atomic_cmpset_acq_32(volatile uint32_t *p, uint32_t cmpval, uint32_t newval)
482 retval = atomic_cmpset_32(p, cmpval, newval);
487 static __inline uint32_t
488 atomic_cmpset_rel_32(volatile uint32_t *p, uint32_t cmpval, uint32_t newval)
491 return (atomic_cmpset_32(p, cmpval, newval));
494 static __inline u_long
495 atomic_cmpset_acq_long(volatile u_long *p, u_long cmpval, u_long newval)
499 retval = atomic_cmpset_long(p, cmpval, newval);
504 static __inline uint32_t
505 atomic_cmpset_rel_long(volatile u_long *p, u_long cmpval, u_long newval)
508 return (atomic_cmpset_long(p, cmpval, newval));
511 #define atomic_cmpset_acq_int atomic_cmpset_acq_32
512 #define atomic_cmpset_rel_int atomic_cmpset_rel_32
514 #define atomic_cmpset_acq_ptr(dst, old, new) \
515 atomic_cmpset_acq_32((volatile u_int *)(dst), (u_int)(old), (u_int)(new))
516 #define atomic_cmpset_rel_ptr(dst, old, new) \
517 atomic_cmpset_rel_32((volatile u_int *)(dst), (u_int)(old), (u_int)(new))
519 static __inline uint32_t
520 atomic_fetchadd_32(volatile uint32_t *p, uint32_t v)
526 } while (!atomic_cmpset_32(p, value, value + v));
530 #define atomic_fetchadd_int atomic_fetchadd_32
531 #define atomic_fetchadd_long(p, v) \
532 (u_long)atomic_fetchadd_32((volatile u_int *)(p), (u_int)(v))
534 #endif /* ! _MACHINE_ATOMIC_H_ */