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
242 #define atomic_set_long_long atomic_set_64
243 #define atomic_clear_long_long atomic_clear_64
244 #define atomic_add_long_long atomic_add_64
245 #define atomic_subtract_long_long atomic_subtract_64
246 #define atomic_readandclear_long_long atomic_readandclear_64
250 #define ATOMIC_ACQ_REL(NAME, WIDTH, TYPE) \
251 static __inline void \
252 atomic_##NAME##_acq_##WIDTH(volatile u_int##WIDTH##_t *p, u_int##WIDTH##_t v) \
254 atomic_##NAME##_##WIDTH(p, v); \
258 static __inline void \
259 atomic_##NAME##_rel_##WIDTH(volatile u_int##WIDTH##_t *p, u_int##WIDTH##_t v) \
262 atomic_##NAME##_##WIDTH(p, v); \
265 static __inline void \
266 atomic_##NAME##_acq_##TYPE(volatile u_int##WIDTH##_t *p, u_int##WIDTH##_t v) \
268 atomic_##NAME##_##WIDTH(p, v); \
272 static __inline void \
273 atomic_##NAME##_rel_##TYPE(volatile u_int##WIDTH##_t *p, u_int##WIDTH##_t v) \
276 atomic_##NAME##_##WIDTH(p, v); \
279 ATOMIC_ACQ_REL(set, 8, char)
280 ATOMIC_ACQ_REL(clear, 8, char)
281 ATOMIC_ACQ_REL(add, 8, char)
282 ATOMIC_ACQ_REL(subtract, 8, char)
283 ATOMIC_ACQ_REL(set, 16, short)
284 ATOMIC_ACQ_REL(clear, 16, short)
285 ATOMIC_ACQ_REL(add, 16, short)
286 ATOMIC_ACQ_REL(subtract, 16, short)
287 ATOMIC_ACQ_REL(set, 32, int)
288 ATOMIC_ACQ_REL(clear, 32, int)
289 ATOMIC_ACQ_REL(add, 32, int)
290 ATOMIC_ACQ_REL(subtract, 32, int)
292 #define atomic_set_acq_long atomic_set_acq_32
293 #define atomic_set_rel_long atomic_set_rel_32
294 #define atomic_clear_acq_long atomic_clear_acq_32
295 #define atomic_clear_rel_long atomic_clear_rel_32
296 #define atomic_add_acq_long atomic_add_acq_32
297 #define atomic_add_rel_long atomic_add_rel_32
298 #define atomic_subtract_acq_long atomic_subtract_acq_32
299 #define atomic_subtract_rel_long atomic_subtract_rel_32
301 #define atomic_set_acq_ptr atomic_set_acq_32
302 #define atomic_set_rel_ptr atomic_set_rel_32
303 #define atomic_clear_acq_ptr atomic_clear_acq_32
304 #define atomic_clear_rel_ptr atomic_clear_rel_32
305 #define atomic_add_acq_ptr atomic_add_acq_32
306 #define atomic_add_rel_ptr atomic_add_rel_32
307 #define atomic_subtract_acq_ptr atomic_subtract_acq_32
308 #define atomic_subtract_rel_ptr atomic_subtract_rel_32
310 #undef ATOMIC_ACQ_REL
313 * We assume that a = b will do atomic loads and stores.
315 #define ATOMIC_STORE_LOAD(TYPE, WIDTH) \
316 static __inline u_##TYPE \
317 atomic_load_acq_##WIDTH(volatile u_##TYPE *p) \
326 static __inline void \
327 atomic_store_rel_##WIDTH(volatile u_##TYPE *p, u_##TYPE v) \
333 static __inline u_##TYPE \
334 atomic_load_acq_##TYPE(volatile u_##TYPE *p) \
343 static __inline void \
344 atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v) \
350 ATOMIC_STORE_LOAD(char, 8)
351 ATOMIC_STORE_LOAD(short, 16)
352 ATOMIC_STORE_LOAD(int, 32)
354 #define atomic_load_acq_long atomic_load_acq_32
355 #define atomic_store_rel_long atomic_store_rel_32
357 #define atomic_load_acq_ptr atomic_load_acq_32
358 #define atomic_store_rel_ptr atomic_store_rel_32
360 #undef ATOMIC_STORE_LOAD
363 * Atomically compare the value stored at *p with cmpval and if the
364 * two values are equal, update the value of *p with newval. Returns
365 * zero if the compare failed, nonzero otherwise.
367 static __inline uint32_t
368 atomic_cmpset_32(volatile uint32_t* p, uint32_t cmpval, uint32_t newval)
372 #ifdef __GNUCLIKE_ASM
374 "1:\tlwarx %0, 0, %2\n\t" /* load old value */
375 "cmplw %3, %0\n\t" /* compare */
376 "bne 2f\n\t" /* exit if not equal */
377 "stwcx. %4, 0, %2\n\t" /* attempt to store */
378 "bne- 1b\n\t" /* spin if failed */
379 "li %0, 1\n\t" /* success - retval = 1 */
380 "b 3f\n\t" /* we've succeeded */
382 "stwcx. %0, 0, %2\n\t" /* clear reservation (74xx) */
383 "li %0, 0\n\t" /* failure - retval = 0 */
385 : "=&r" (ret), "=m" (*p)
386 : "r" (p), "r" (cmpval), "r" (newval), "m" (*p)
396 * Atomically compare the value stored at *p with cmpval and if the
397 * two values are equal, update the value of *p with newval. Returns
398 * zero if the compare failed, nonzero otherwise.
400 static __inline u_int64_t
401 atomic_cmpset_64(volatile u_int64_t* p, u_int64_t cmpval, u_int64_t newval)
406 : "=&r" (ret), "=r" (*p)
407 : "r" (cmpval), "r" (newval), "r" (*p)
415 #define atomic_cmpset_int atomic_cmpset_32
416 #define atomic_cmpset_long atomic_cmpset_32
417 #define atomic_cmpset_ptr atomic_cmpset_32
420 #define atomic_cmpset_long_long atomic_cmpset_64
423 static __inline uint32_t
424 atomic_cmpset_acq_32(volatile uint32_t *p, uint32_t cmpval, uint32_t newval)
428 retval = atomic_cmpset_32(p, cmpval, newval);
433 static __inline uint32_t
434 atomic_cmpset_rel_32(volatile uint32_t *p, uint32_t cmpval, uint32_t newval)
437 return (atomic_cmpset_32(p, cmpval, newval));
440 #define atomic_cmpset_acq_int atomic_cmpset_acq_32
441 #define atomic_cmpset_rel_int atomic_cmpset_rel_32
442 #define atomic_cmpset_acq_long atomic_cmpset_acq_32
443 #define atomic_cmpset_rel_long atomic_cmpset_rel_32
444 #define atomic_cmpset_acq_ptr atomic_cmpset_acq_32
445 #define atomic_cmpset_rel_ptr atomic_cmpset_rel_32
447 static __inline uint32_t
448 atomic_fetchadd_32(volatile uint32_t *p, uint32_t v)
454 } while (!atomic_cmpset_32(p, value, value + v));
458 #define atomic_fetchadd_int atomic_fetchadd_32
460 #endif /* ! _MACHINE_ATOMIC_H_ */