2 * Copyright (c) 2001 Benno Rice
3 * Copyright (c) 2001 David E. O'Brien
4 * Copyright (c) 1998 Doug Rabson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #ifndef _MACHINE_ATOMIC_H_
32 #define _MACHINE_ATOMIC_H_
34 #include <machine/cpufunc.h>
37 #error this file needs sys/cdefs.h as a prerequisite
41 * Various simple arithmetic on memory which is atomic in the presence
42 * of interrupts and SMP safe.
45 void atomic_set_8(volatile uint8_t *, uint8_t);
46 void atomic_clear_8(volatile uint8_t *, uint8_t);
47 void atomic_add_8(volatile uint8_t *, uint8_t);
48 void atomic_subtract_8(volatile uint8_t *, uint8_t);
50 void atomic_set_16(volatile uint16_t *, uint16_t);
51 void atomic_clear_16(volatile uint16_t *, uint16_t);
52 void atomic_add_16(volatile uint16_t *, uint16_t);
53 void atomic_subtract_16(volatile uint16_t *, uint16_t);
56 atomic_set_32(volatile uint32_t *p, uint32_t v)
62 "1:\tlwarx %0, 0, %2\n\t" /* load old value */
63 "or %0, %3, %0\n\t" /* calculate new value */
64 "stwcx. %0, 0, %2\n\t" /* attempt to store */
65 "bne- 1b\n\t" /* spin if failed */
66 : "=&r" (temp), "=m" (*p)
67 : "r" (p), "r" (v), "m" (*p)
73 atomic_clear_32(volatile uint32_t *p, uint32_t v)
79 "1:\tlwarx %0, 0, %2\n\t" /* load old value */
80 "andc %0, %0, %3\n\t" /* calculate new value */
81 "stwcx. %0, 0, %2\n\t" /* attempt to store */
82 "bne- 1b\n\t" /* spin if failed */
83 : "=&r" (temp), "=m" (*p)
84 : "r" (p), "r" (v), "m" (*p)
90 atomic_add_32(volatile uint32_t *p, uint32_t v)
96 "1:\tlwarx %0, 0, %2\n\t" /* load old value */
97 "add %0, %3, %0\n\t" /* calculate new value */
98 "stwcx. %0, 0, %2\n\t" /* attempt to store */
99 "bne- 1b\n\t" /* spin if failed */
100 : "=&r" (temp), "=m" (*p)
101 : "r" (p), "r" (v), "m" (*p)
107 atomic_subtract_32(volatile uint32_t *p, uint32_t v)
111 #ifdef __GNUCLIKE_ASM
113 "1:\tlwarx %0, 0, %2\n\t" /* load old value */
114 "subf %0, %3, %0\n\t" /* calculate new value */
115 "stwcx. %0, 0, %2\n\t" /* attempt to store */
116 "bne- 1b\n\t" /* spin if failed */
117 : "=&r" (temp), "=m" (*p)
118 : "r" (p), "r" (v), "m" (*p)
123 static __inline uint32_t
124 atomic_readandclear_32(volatile uint32_t *addr)
126 uint32_t result,temp;
128 #ifdef __GNUCLIKE_ASM
130 "\tsync\n" /* drain writes */
131 "1:\tlwarx %0, 0, %3\n\t" /* load old value */
132 "li %1, 0\n\t" /* load new value */
133 "stwcx. %1, 0, %3\n\t" /* attempt to store */
134 "bne- 1b\n\t" /* spin if failed */
135 : "=&r"(result), "=&r"(temp), "=m" (*addr)
136 : "r" (addr), "m" (*addr)
146 * So far I haven't found a way to implement atomic 64-bit ops on the
147 * 32-bit PowerPC without involving major headaches. If anyone has
148 * any ideas, please let me know. =)
149 * - benno@FreeBSD.org
153 atomic_set_64(volatile u_int64_t *p, u_int64_t v)
158 : "=&r" (temp), "=r" (*p)
164 atomic_clear_64(volatile u_int64_t *p, u_int64_t v)
169 : "=&r" (temp), "=r" (*p)
175 atomic_add_64(volatile u_int64_t *p, u_int64_t v)
180 : "=&r" (temp), "=r" (*p)
186 atomic_subtract_64(volatile u_int64_t *p, u_int64_t v)
191 : "=&r" (temp), "=r" (*p)
196 static __inline u_int64_t
197 atomic_readandclear_64(volatile u_int64_t *addr)
199 u_int64_t result,temp;
202 : "=&r"(result), "=&r"(temp), "=r" (*addr)
211 #define atomic_set_char atomic_set_8
212 #define atomic_clear_char atomic_clear_8
213 #define atomic_add_char atomic_add_8
214 #define atomic_subtract_char atomic_subtract_8
216 #define atomic_set_short atomic_set_16
217 #define atomic_clear_short atomic_clear_16
218 #define atomic_add_short atomic_add_16
219 #define atomic_subtract_short atomic_subtract_16
221 #define atomic_set_int atomic_set_32
222 #define atomic_clear_int atomic_clear_32
223 #define atomic_add_int atomic_add_32
224 #define atomic_subtract_int atomic_subtract_32
225 #define atomic_readandclear_int atomic_readandclear_32
227 #define atomic_set_long atomic_set_32
228 #define atomic_clear_long atomic_clear_32
229 #define atomic_add_long(p, v) atomic_add_32((uint32_t *)p, (uint32_t)v)
230 #define atomic_subtract_long(p, v) atomic_subtract_32((uint32_t *)p, (uint32_t)v)
231 #define atomic_readandclear_long atomic_readandclear_32
233 #define atomic_set_ptr atomic_set_32
234 #define atomic_clear_ptr atomic_clear_32
235 #define atomic_add_ptr atomic_add_32
236 #define atomic_subtract_ptr atomic_subtract_32
237 #define atomic_readandclear_ptr atomic_readandclear_32
243 #define atomic_set_long_long atomic_set_64
244 #define atomic_clear_long_long atomic_clear_64
245 #define atomic_add_long_long atomic_add_64
246 #define atomic_subtract_long_long atomic_subtract_64
247 #define atomic_readandclear_long_long atomic_readandclear_64
251 #define ATOMIC_ACQ_REL(NAME, WIDTH, TYPE) \
252 static __inline void \
253 atomic_##NAME##_acq_##WIDTH(volatile u_int##WIDTH##_t *p, u_int##WIDTH##_t v) \
255 atomic_##NAME##_##WIDTH(p, v); \
259 static __inline void \
260 atomic_##NAME##_rel_##WIDTH(volatile u_int##WIDTH##_t *p, u_int##WIDTH##_t v) \
263 atomic_##NAME##_##WIDTH(p, v); \
266 static __inline void \
267 atomic_##NAME##_acq_##TYPE(volatile u_int##WIDTH##_t *p, u_int##WIDTH##_t v) \
269 atomic_##NAME##_##WIDTH(p, v); \
273 static __inline void \
274 atomic_##NAME##_rel_##TYPE(volatile u_int##WIDTH##_t *p, u_int##WIDTH##_t v) \
277 atomic_##NAME##_##WIDTH(p, v); \
280 ATOMIC_ACQ_REL(set, 8, char)
281 ATOMIC_ACQ_REL(clear, 8, char)
282 ATOMIC_ACQ_REL(add, 8, char)
283 ATOMIC_ACQ_REL(subtract, 8, char)
284 ATOMIC_ACQ_REL(set, 16, short)
285 ATOMIC_ACQ_REL(clear, 16, short)
286 ATOMIC_ACQ_REL(add, 16, short)
287 ATOMIC_ACQ_REL(subtract, 16, short)
288 ATOMIC_ACQ_REL(set, 32, int)
289 ATOMIC_ACQ_REL(clear, 32, int)
290 ATOMIC_ACQ_REL(add, 32, int)
291 ATOMIC_ACQ_REL(subtract, 32, int)
293 #define atomic_set_acq_long atomic_set_acq_32
294 #define atomic_set_rel_long atomic_set_rel_32
295 #define atomic_clear_acq_long atomic_clear_acq_32
296 #define atomic_clear_rel_long atomic_clear_rel_32
297 #define atomic_add_acq_long atomic_add_acq_32
298 #define atomic_add_rel_long atomic_add_rel_32
299 #define atomic_subtract_acq_long atomic_subtract_acq_32
300 #define atomic_subtract_rel_long atomic_subtract_rel_32
302 #define atomic_set_acq_ptr atomic_set_acq_32
303 #define atomic_set_rel_ptr atomic_set_rel_32
304 #define atomic_clear_acq_ptr atomic_clear_acq_32
305 #define atomic_clear_rel_ptr atomic_clear_rel_32
306 #define atomic_add_acq_ptr atomic_add_acq_32
307 #define atomic_add_rel_ptr atomic_add_rel_32
308 #define atomic_subtract_acq_ptr atomic_subtract_acq_32
309 #define atomic_subtract_rel_ptr atomic_subtract_rel_32
311 #undef ATOMIC_ACQ_REL
314 * We assume that a = b will do atomic loads and stores.
316 #define ATOMIC_STORE_LOAD(TYPE, WIDTH) \
317 static __inline u_##TYPE \
318 atomic_load_acq_##WIDTH(volatile u_##TYPE *p) \
327 static __inline void \
328 atomic_store_rel_##WIDTH(volatile u_##TYPE *p, u_##TYPE v) \
334 static __inline u_##TYPE \
335 atomic_load_acq_##TYPE(volatile u_##TYPE *p) \
344 static __inline void \
345 atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v) \
351 ATOMIC_STORE_LOAD(char, 8)
352 ATOMIC_STORE_LOAD(short, 16)
353 ATOMIC_STORE_LOAD(int, 32)
355 #define atomic_load_acq_long atomic_load_acq_32
356 #define atomic_store_rel_long atomic_store_rel_32
358 #define atomic_load_acq_ptr atomic_load_acq_32
359 #define atomic_store_rel_ptr atomic_store_rel_32
361 #undef ATOMIC_STORE_LOAD
364 * Atomically compare the value stored at *p with cmpval and if the
365 * two values are equal, update the value of *p with newval. Returns
366 * zero if the compare failed, nonzero otherwise.
368 static __inline uint32_t
369 atomic_cmpset_32(volatile uint32_t* p, uint32_t cmpval, uint32_t newval)
373 #ifdef __GNUCLIKE_ASM
375 "1:\tlwarx %0, 0, %2\n\t" /* load old value */
376 "cmplw %3, %0\n\t" /* compare */
377 "bne 2f\n\t" /* exit if not equal */
378 "stwcx. %4, 0, %2\n\t" /* attempt to store */
379 "bne- 1b\n\t" /* spin if failed */
380 "li %0, 1\n\t" /* success - retval = 1 */
381 "b 3f\n\t" /* we've succeeded */
383 "stwcx. %0, 0, %2\n\t" /* clear reservation (74xx) */
384 "li %0, 0\n\t" /* failure - retval = 0 */
386 : "=&r" (ret), "=m" (*p)
387 : "r" (p), "r" (cmpval), "r" (newval), "m" (*p)
394 static __inline u_long
395 atomic_cmpset_long(volatile u_long* p, u_long cmpval, u_long newval)
399 #ifdef __GNUCLIKE_ASM
401 "1:\tlwarx %0, 0, %2\n\t" /* load old value */
402 "cmplw %3, %0\n\t" /* compare */
403 "bne 2f\n\t" /* exit if not equal */
404 "stwcx. %4, 0, %2\n\t" /* attempt to store */
405 "bne- 1b\n\t" /* spin if failed */
406 "li %0, 1\n\t" /* success - retval = 1 */
407 "b 3f\n\t" /* we've succeeded */
409 "stwcx. %0, 0, %2\n\t" /* clear reservation (74xx) */
410 "li %0, 0\n\t" /* failure - retval = 0 */
412 : "=&r" (ret), "=m" (*p)
413 : "r" (p), "r" (cmpval), "r" (newval), "m" (*p)
423 * Atomically compare the value stored at *p with cmpval and if the
424 * two values are equal, update the value of *p with newval. Returns
425 * zero if the compare failed, nonzero otherwise.
427 static __inline u_int64_t
428 atomic_cmpset_64(volatile u_int64_t* p, u_int64_t cmpval, u_int64_t newval)
433 : "=&r" (ret), "=r" (*p)
434 : "r" (cmpval), "r" (newval), "r" (*p)
442 #define atomic_cmpset_int atomic_cmpset_32
444 #define atomic_cmpset_ptr(dst, old, new) \
445 atomic_cmpset_32((volatile u_int *)(dst), (u_int)(old), (u_int)(new))
448 #define atomic_cmpset_long_long atomic_cmpset_64
451 static __inline uint32_t
452 atomic_cmpset_acq_32(volatile uint32_t *p, uint32_t cmpval, uint32_t newval)
456 retval = atomic_cmpset_32(p, cmpval, newval);
461 static __inline uint32_t
462 atomic_cmpset_rel_32(volatile uint32_t *p, uint32_t cmpval, uint32_t newval)
465 return (atomic_cmpset_32(p, cmpval, newval));
468 static __inline u_long
469 atomic_cmpset_acq_long(volatile u_long *p, u_long cmpval, u_long newval)
473 retval = atomic_cmpset_long(p, cmpval, newval);
478 static __inline uint32_t
479 atomic_cmpset_rel_long(volatile u_long *p, u_long cmpval, u_long newval)
482 return (atomic_cmpset_long(p, cmpval, newval));
485 #define atomic_cmpset_acq_int atomic_cmpset_acq_32
486 #define atomic_cmpset_rel_int atomic_cmpset_rel_32
488 #define atomic_cmpset_acq_ptr(dst, old, new) \
489 atomic_cmpset_acq_32((volatile u_int *)(dst), (u_int)(old), (u_int)(new))
490 #define atomic_cmpset_rel_ptr(dst, old, new) \
491 atomic_cmpset_rel_32((volatile u_int *)(dst), (u_int)(old), (u_int)(new))
493 static __inline uint32_t
494 atomic_fetchadd_32(volatile uint32_t *p, uint32_t v)
500 } while (!atomic_cmpset_32(p, value, value + v));
504 #define atomic_fetchadd_int atomic_fetchadd_32
505 #define atomic_fetchadd_long(p, v) \
506 (u_long)atomic_fetchadd_32((volatile u_int *)(p), (u_int)(v))
508 #endif /* ! _MACHINE_ATOMIC_H_ */