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
40 * The __ATOMIC_REL/ACQ() macros provide memory barriers only in conjunction
41 * with the atomic lXarx/stXcx. sequences below. They are not exposed outside
42 * of this file. See also Appendix B.2 of Book II of the architecture manual.
44 * Note that not all Book-E processors accept the light-weight sync variant.
45 * In particular, early models of E500 cores are known to wedge. Bank on all
46 * 64-bit capable CPUs to accept lwsync properly and pressimize 32-bit CPUs
47 * to use the heavier-weight sync.
51 #define mb() __asm __volatile("lwsync" : : : "memory")
52 #define rmb() __asm __volatile("lwsync" : : : "memory")
53 #define wmb() __asm __volatile("lwsync" : : : "memory")
54 #define __ATOMIC_REL() __asm __volatile("lwsync" : : : "memory")
55 #define __ATOMIC_ACQ() __asm __volatile("isync" : : : "memory")
57 #define mb() __asm __volatile("sync" : : : "memory")
58 #define rmb() __asm __volatile("sync" : : : "memory")
59 #define wmb() __asm __volatile("sync" : : : "memory")
60 #define __ATOMIC_REL() __asm __volatile("sync" : : : "memory")
61 #define __ATOMIC_ACQ() __asm __volatile("isync" : : : "memory")
69 #define __atomic_add_int(p, v, t) \
71 "1: lwarx %0, 0, %2\n" \
73 " stwcx. %0, 0, %2\n" \
75 : "=&r" (t), "=m" (*p) \
76 : "r" (p), "r" (v), "m" (*p) \
78 /* __atomic_add_int */
81 #define __atomic_add_long(p, v, t) \
83 "1: ldarx %0, 0, %2\n" \
85 " stdcx. %0, 0, %2\n" \
87 : "=&r" (t), "=m" (*p) \
88 : "r" (p), "r" (v), "m" (*p) \
90 /* __atomic_add_long */
92 #define __atomic_add_long(p, v, t) \
94 "1: lwarx %0, 0, %2\n" \
96 " stwcx. %0, 0, %2\n" \
98 : "=&r" (t), "=m" (*p) \
99 : "r" (p), "r" (v), "m" (*p) \
101 /* __atomic_add_long */
104 #define _ATOMIC_ADD(type) \
105 static __inline void \
106 atomic_add_##type(volatile u_##type *p, u_##type v) { \
108 __atomic_add_##type(p, v, t); \
111 static __inline void \
112 atomic_add_acq_##type(volatile u_##type *p, u_##type v) { \
114 __atomic_add_##type(p, v, t); \
118 static __inline void \
119 atomic_add_rel_##type(volatile u_##type *p, u_##type v) { \
122 __atomic_add_##type(p, v, t); \
129 #define atomic_add_32 atomic_add_int
130 #define atomic_add_acq_32 atomic_add_acq_int
131 #define atomic_add_rel_32 atomic_add_rel_int
134 #define atomic_add_64 atomic_add_long
135 #define atomic_add_acq_64 atomic_add_acq_long
136 #define atomic_add_rel_64 atomic_add_rel_long
138 #define atomic_add_ptr atomic_add_long
139 #define atomic_add_acq_ptr atomic_add_acq_long
140 #define atomic_add_rel_ptr atomic_add_rel_long
142 #define atomic_add_ptr atomic_add_int
143 #define atomic_add_acq_ptr atomic_add_acq_int
144 #define atomic_add_rel_ptr atomic_add_rel_int
147 #undef __atomic_add_long
148 #undef __atomic_add_int
155 #define __atomic_clear_int(p, v, t) \
157 "1: lwarx %0, 0, %2\n" \
158 " andc %0, %0, %3\n" \
159 " stwcx. %0, 0, %2\n" \
161 : "=&r" (t), "=m" (*p) \
162 : "r" (p), "r" (v), "m" (*p) \
164 /* __atomic_clear_int */
167 #define __atomic_clear_long(p, v, t) \
169 "1: ldarx %0, 0, %2\n" \
170 " andc %0, %0, %3\n" \
171 " stdcx. %0, 0, %2\n" \
173 : "=&r" (t), "=m" (*p) \
174 : "r" (p), "r" (v), "m" (*p) \
176 /* __atomic_clear_long */
178 #define __atomic_clear_long(p, v, t) \
180 "1: lwarx %0, 0, %2\n" \
181 " andc %0, %0, %3\n" \
182 " stwcx. %0, 0, %2\n" \
184 : "=&r" (t), "=m" (*p) \
185 : "r" (p), "r" (v), "m" (*p) \
187 /* __atomic_clear_long */
190 #define _ATOMIC_CLEAR(type) \
191 static __inline void \
192 atomic_clear_##type(volatile u_##type *p, u_##type v) { \
194 __atomic_clear_##type(p, v, t); \
197 static __inline void \
198 atomic_clear_acq_##type(volatile u_##type *p, u_##type v) { \
200 __atomic_clear_##type(p, v, t); \
204 static __inline void \
205 atomic_clear_rel_##type(volatile u_##type *p, u_##type v) { \
208 __atomic_clear_##type(p, v, t); \
216 #define atomic_clear_32 atomic_clear_int
217 #define atomic_clear_acq_32 atomic_clear_acq_int
218 #define atomic_clear_rel_32 atomic_clear_rel_int
221 #define atomic_clear_64 atomic_clear_long
222 #define atomic_clear_acq_64 atomic_clear_acq_long
223 #define atomic_clear_rel_64 atomic_clear_rel_long
225 #define atomic_clear_ptr atomic_clear_long
226 #define atomic_clear_acq_ptr atomic_clear_acq_long
227 #define atomic_clear_rel_ptr atomic_clear_rel_long
229 #define atomic_clear_ptr atomic_clear_int
230 #define atomic_clear_acq_ptr atomic_clear_acq_int
231 #define atomic_clear_rel_ptr atomic_clear_rel_int
234 #undef __atomic_clear_long
235 #undef __atomic_clear_int
238 * atomic_cmpset(p, o, n)
240 /* TODO -- see below */
245 /* TODO -- see below */
248 * atomic_readandclear(p)
250 /* TODO -- see below */
257 #define __atomic_set_int(p, v, t) \
259 "1: lwarx %0, 0, %2\n" \
261 " stwcx. %0, 0, %2\n" \
263 : "=&r" (t), "=m" (*p) \
264 : "r" (p), "r" (v), "m" (*p) \
266 /* __atomic_set_int */
269 #define __atomic_set_long(p, v, t) \
271 "1: ldarx %0, 0, %2\n" \
273 " stdcx. %0, 0, %2\n" \
275 : "=&r" (t), "=m" (*p) \
276 : "r" (p), "r" (v), "m" (*p) \
278 /* __atomic_set_long */
280 #define __atomic_set_long(p, v, t) \
282 "1: lwarx %0, 0, %2\n" \
284 " stwcx. %0, 0, %2\n" \
286 : "=&r" (t), "=m" (*p) \
287 : "r" (p), "r" (v), "m" (*p) \
289 /* __atomic_set_long */
292 #define _ATOMIC_SET(type) \
293 static __inline void \
294 atomic_set_##type(volatile u_##type *p, u_##type v) { \
296 __atomic_set_##type(p, v, t); \
299 static __inline void \
300 atomic_set_acq_##type(volatile u_##type *p, u_##type v) { \
302 __atomic_set_##type(p, v, t); \
306 static __inline void \
307 atomic_set_rel_##type(volatile u_##type *p, u_##type v) { \
310 __atomic_set_##type(p, v, t); \
317 #define atomic_set_32 atomic_set_int
318 #define atomic_set_acq_32 atomic_set_acq_int
319 #define atomic_set_rel_32 atomic_set_rel_int
322 #define atomic_set_64 atomic_set_long
323 #define atomic_set_acq_64 atomic_set_acq_long
324 #define atomic_set_rel_64 atomic_set_rel_long
326 #define atomic_set_ptr atomic_set_long
327 #define atomic_set_acq_ptr atomic_set_acq_long
328 #define atomic_set_rel_ptr atomic_set_rel_long
330 #define atomic_set_ptr atomic_set_int
331 #define atomic_set_acq_ptr atomic_set_acq_int
332 #define atomic_set_rel_ptr atomic_set_rel_int
335 #undef __atomic_set_long
336 #undef __atomic_set_int
339 * atomic_subtract(p, v)
343 #define __atomic_subtract_int(p, v, t) \
345 "1: lwarx %0, 0, %2\n" \
346 " subf %0, %3, %0\n" \
347 " stwcx. %0, 0, %2\n" \
349 : "=&r" (t), "=m" (*p) \
350 : "r" (p), "r" (v), "m" (*p) \
352 /* __atomic_subtract_int */
355 #define __atomic_subtract_long(p, v, t) \
357 "1: ldarx %0, 0, %2\n" \
358 " subf %0, %3, %0\n" \
359 " stdcx. %0, 0, %2\n" \
361 : "=&r" (t), "=m" (*p) \
362 : "r" (p), "r" (v), "m" (*p) \
364 /* __atomic_subtract_long */
366 #define __atomic_subtract_long(p, v, t) \
368 "1: lwarx %0, 0, %2\n" \
369 " subf %0, %3, %0\n" \
370 " stwcx. %0, 0, %2\n" \
372 : "=&r" (t), "=m" (*p) \
373 : "r" (p), "r" (v), "m" (*p) \
375 /* __atomic_subtract_long */
378 #define _ATOMIC_SUBTRACT(type) \
379 static __inline void \
380 atomic_subtract_##type(volatile u_##type *p, u_##type v) { \
382 __atomic_subtract_##type(p, v, t); \
385 static __inline void \
386 atomic_subtract_acq_##type(volatile u_##type *p, u_##type v) { \
388 __atomic_subtract_##type(p, v, t); \
392 static __inline void \
393 atomic_subtract_rel_##type(volatile u_##type *p, u_##type v) { \
396 __atomic_subtract_##type(p, v, t); \
398 /* _ATOMIC_SUBTRACT */
400 _ATOMIC_SUBTRACT(int)
401 _ATOMIC_SUBTRACT(long)
403 #define atomic_subtract_32 atomic_subtract_int
404 #define atomic_subtract_acq_32 atomic_subtract_acq_int
405 #define atomic_subtract_rel_32 atomic_subtract_rel_int
408 #define atomic_subtract_64 atomic_subtract_long
409 #define atomic_subtract_acq_64 atomic_subract_acq_long
410 #define atomic_subtract_rel_64 atomic_subtract_rel_long
412 #define atomic_subtract_ptr atomic_subtract_long
413 #define atomic_subtract_acq_ptr atomic_subtract_acq_long
414 #define atomic_subtract_rel_ptr atomic_subtract_rel_long
416 #define atomic_subtract_ptr atomic_subtract_int
417 #define atomic_subtract_acq_ptr atomic_subtract_acq_int
418 #define atomic_subtract_rel_ptr atomic_subtract_rel_int
420 #undef _ATOMIC_SUBTRACT
421 #undef __atomic_subtract_long
422 #undef __atomic_subtract_int
425 * atomic_store_rel(p, v)
427 /* TODO -- see below */
430 * Old/original implementations that still need revisiting.
433 static __inline u_int
434 atomic_readandclear_int(volatile u_int *addr)
438 #ifdef __GNUCLIKE_ASM
440 "\tsync\n" /* drain writes */
441 "1:\tlwarx %0, 0, %3\n\t" /* load old value */
442 "li %1, 0\n\t" /* load new value */
443 "stwcx. %1, 0, %3\n\t" /* attempt to store */
444 "bne- 1b\n\t" /* spin if failed */
445 : "=&r"(result), "=&r"(temp), "=m" (*addr)
446 : "r" (addr), "m" (*addr)
454 static __inline u_long
455 atomic_readandclear_long(volatile u_long *addr)
459 #ifdef __GNUCLIKE_ASM
461 "\tsync\n" /* drain writes */
462 "1:\tldarx %0, 0, %3\n\t" /* load old value */
463 "li %1, 0\n\t" /* load new value */
464 "stdcx. %1, 0, %3\n\t" /* attempt to store */
465 "bne- 1b\n\t" /* spin if failed */
466 : "=&r"(result), "=&r"(temp), "=m" (*addr)
467 : "r" (addr), "m" (*addr)
475 #define atomic_readandclear_32 atomic_readandclear_int
478 #define atomic_readandclear_64 atomic_readandclear_long
480 #define atomic_readandclear_ptr atomic_readandclear_long
482 static __inline u_long
483 atomic_readandclear_long(volatile u_long *addr)
486 return ((u_long)atomic_readandclear_int((volatile u_int *)addr));
489 #define atomic_readandclear_ptr atomic_readandclear_int
493 * We assume that a = b will do atomic loads and stores.
495 #define ATOMIC_STORE_LOAD(TYPE) \
496 static __inline u_##TYPE \
497 atomic_load_acq_##TYPE(volatile u_##TYPE *p) \
506 static __inline void \
507 atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v) \
513 ATOMIC_STORE_LOAD(int)
515 #define atomic_load_acq_32 atomic_load_acq_int
516 #define atomic_store_rel_32 atomic_store_rel_int
519 ATOMIC_STORE_LOAD(long)
521 #define atomic_load_acq_64 atomic_load_acq_long
522 #define atomic_store_rel_64 atomic_store_rel_long
524 #define atomic_load_acq_ptr atomic_load_acq_long
525 #define atomic_store_rel_ptr atomic_store_rel_long
527 static __inline u_long
528 atomic_load_acq_long(volatile u_long *addr)
531 return ((u_long)atomic_load_acq_int((volatile u_int *)addr));
535 atomic_store_rel_long(volatile u_long *addr, u_long val)
538 atomic_store_rel_int((volatile u_int *)addr, (u_int)val);
541 #define atomic_load_acq_ptr atomic_load_acq_int
542 #define atomic_store_rel_ptr atomic_store_rel_int
544 #undef ATOMIC_STORE_LOAD
547 * Atomically compare the value stored at *p with cmpval and if the
548 * two values are equal, update the value of *p with newval. Returns
549 * zero if the compare failed, nonzero otherwise.
552 atomic_cmpset_int(volatile u_int* p, u_int cmpval, u_int newval)
556 #ifdef __GNUCLIKE_ASM
558 "1:\tlwarx %0, 0, %2\n\t" /* load old value */
559 "cmplw %3, %0\n\t" /* compare */
560 "bne 2f\n\t" /* exit if not equal */
561 "stwcx. %4, 0, %2\n\t" /* attempt to store */
562 "bne- 1b\n\t" /* spin if failed */
563 "li %0, 1\n\t" /* success - retval = 1 */
564 "b 3f\n\t" /* we've succeeded */
566 "stwcx. %0, 0, %2\n\t" /* clear reservation (74xx) */
567 "li %0, 0\n\t" /* failure - retval = 0 */
569 : "=&r" (ret), "=m" (*p)
570 : "r" (p), "r" (cmpval), "r" (newval), "m" (*p)
577 atomic_cmpset_long(volatile u_long* p, u_long cmpval, u_long newval)
581 #ifdef __GNUCLIKE_ASM
584 "1:\tldarx %0, 0, %2\n\t" /* load old value */
585 "cmpld %3, %0\n\t" /* compare */
586 "bne 2f\n\t" /* exit if not equal */
587 "stdcx. %4, 0, %2\n\t" /* attempt to store */
589 "1:\tlwarx %0, 0, %2\n\t" /* load old value */
590 "cmplw %3, %0\n\t" /* compare */
591 "bne 2f\n\t" /* exit if not equal */
592 "stwcx. %4, 0, %2\n\t" /* attempt to store */
594 "bne- 1b\n\t" /* spin if failed */
595 "li %0, 1\n\t" /* success - retval = 1 */
596 "b 3f\n\t" /* we've succeeded */
599 "stdcx. %0, 0, %2\n\t" /* clear reservation (74xx) */
601 "stwcx. %0, 0, %2\n\t" /* clear reservation (74xx) */
603 "li %0, 0\n\t" /* failure - retval = 0 */
605 : "=&r" (ret), "=m" (*p)
606 : "r" (p), "r" (cmpval), "r" (newval), "m" (*p)
614 atomic_cmpset_acq_int(volatile u_int *p, u_int cmpval, u_int newval)
618 retval = atomic_cmpset_int(p, cmpval, newval);
624 atomic_cmpset_rel_int(volatile u_int *p, u_int cmpval, u_int newval)
627 return (atomic_cmpset_int(p, cmpval, newval));
631 atomic_cmpset_acq_long(volatile u_long *p, u_long cmpval, u_long newval)
635 retval = atomic_cmpset_long(p, cmpval, newval);
641 atomic_cmpset_rel_long(volatile u_long *p, u_long cmpval, u_long newval)
644 return (atomic_cmpset_long(p, cmpval, newval));
647 #define atomic_cmpset_32 atomic_cmpset_int
648 #define atomic_cmpset_acq_32 atomic_cmpset_acq_int
649 #define atomic_cmpset_rel_32 atomic_cmpset_rel_int
652 #define atomic_cmpset_64 atomic_cmpset_long
653 #define atomic_cmpset_acq_64 atomic_cmpset_acq_long
654 #define atomic_cmpset_rel_64 atomic_cmpset_rel_long
656 #define atomic_cmpset_ptr atomic_cmpset_long
657 #define atomic_cmpset_acq_ptr atomic_cmpset_acq_long
658 #define atomic_cmpset_rel_ptr atomic_cmpset_rel_long
660 #define atomic_cmpset_ptr atomic_cmpset_int
661 #define atomic_cmpset_acq_ptr atomic_cmpset_acq_int
662 #define atomic_cmpset_rel_ptr atomic_cmpset_rel_int
665 static __inline u_int
666 atomic_fetchadd_int(volatile u_int *p, u_int v)
672 } while (!atomic_cmpset_int(p, value, value + v));
676 static __inline u_long
677 atomic_fetchadd_long(volatile u_long *p, u_long v)
683 } while (!atomic_cmpset_long(p, value, value + v));
687 #define atomic_fetchadd_32 atomic_fetchadd_int
690 #define atomic_fetchadd_64 atomic_fetchadd_long
696 #endif /* ! _MACHINE_ATOMIC_H_ */