2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Marcel Moolenaar
5 * Copyright (c) 2001 Benno Rice
6 * Copyright (c) 2001 David E. O'Brien
7 * Copyright (c) 1998 Doug Rabson
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #ifndef _MACHINE_ATOMIC_H_
35 #define _MACHINE_ATOMIC_H_
38 #error this file needs sys/cdefs.h as a prerequisite
42 * The __ATOMIC_REL/ACQ() macros provide memory barriers only in conjunction
43 * with the atomic lXarx/stXcx. sequences below. They are not exposed outside
44 * of this file. See also Appendix B.2 of Book II of the architecture manual.
46 * Note that not all Book-E processors accept the light-weight sync variant.
47 * In particular, early models of E500 cores are known to wedge. Bank on all
48 * 64-bit capable CPUs to accept lwsync properly and pressimize 32-bit CPUs
49 * to use the heavier-weight sync.
53 #define mb() __asm __volatile("sync" : : : "memory")
54 #define rmb() __asm __volatile("lwsync" : : : "memory")
55 #define wmb() __asm __volatile("lwsync" : : : "memory")
56 #define __ATOMIC_REL() __asm __volatile("lwsync" : : : "memory")
57 #define __ATOMIC_ACQ() __asm __volatile("isync" : : : "memory")
59 #define mb() __asm __volatile("sync" : : : "memory")
60 #define rmb() __asm __volatile("sync" : : : "memory")
61 #define wmb() __asm __volatile("sync" : : : "memory")
62 #define __ATOMIC_REL() __asm __volatile("sync" : : : "memory")
63 #define __ATOMIC_ACQ() __asm __volatile("isync" : : : "memory")
71 __asm __volatile("lwsync" : : : "memory");
73 __asm __volatile("sync" : : : "memory");
82 #define __atomic_add_int(p, v, t) \
84 "1: lwarx %0, 0, %2\n" \
86 " stwcx. %0, 0, %2\n" \
88 : "=&r" (t), "=m" (*p) \
89 : "r" (p), "r" (v), "m" (*p) \
91 /* __atomic_add_int */
94 #define __atomic_add_long(p, v, t) \
96 "1: ldarx %0, 0, %2\n" \
98 " stdcx. %0, 0, %2\n" \
100 : "=&r" (t), "=m" (*p) \
101 : "r" (p), "r" (v), "m" (*p) \
103 /* __atomic_add_long */
105 #define __atomic_add_long(p, v, t) \
107 "1: lwarx %0, 0, %2\n" \
108 " add %0, %3, %0\n" \
109 " stwcx. %0, 0, %2\n" \
111 : "=&r" (t), "=m" (*p) \
112 : "r" (p), "r" (v), "m" (*p) \
114 /* __atomic_add_long */
117 #define _ATOMIC_ADD(type) \
118 static __inline void \
119 atomic_add_##type(volatile u_##type *p, u_##type v) { \
121 __atomic_add_##type(p, v, t); \
124 static __inline void \
125 atomic_add_acq_##type(volatile u_##type *p, u_##type v) { \
127 __atomic_add_##type(p, v, t); \
131 static __inline void \
132 atomic_add_rel_##type(volatile u_##type *p, u_##type v) { \
135 __atomic_add_##type(p, v, t); \
142 #define atomic_add_32 atomic_add_int
143 #define atomic_add_acq_32 atomic_add_acq_int
144 #define atomic_add_rel_32 atomic_add_rel_int
147 #define atomic_add_64 atomic_add_long
148 #define atomic_add_acq_64 atomic_add_acq_long
149 #define atomic_add_rel_64 atomic_add_rel_long
151 #define atomic_add_ptr atomic_add_long
152 #define atomic_add_acq_ptr atomic_add_acq_long
153 #define atomic_add_rel_ptr atomic_add_rel_long
155 #define atomic_add_ptr atomic_add_int
156 #define atomic_add_acq_ptr atomic_add_acq_int
157 #define atomic_add_rel_ptr atomic_add_rel_int
160 #undef __atomic_add_long
161 #undef __atomic_add_int
168 #define __atomic_clear_int(p, v, t) \
170 "1: lwarx %0, 0, %2\n" \
171 " andc %0, %0, %3\n" \
172 " stwcx. %0, 0, %2\n" \
174 : "=&r" (t), "=m" (*p) \
175 : "r" (p), "r" (v), "m" (*p) \
177 /* __atomic_clear_int */
180 #define __atomic_clear_long(p, v, t) \
182 "1: ldarx %0, 0, %2\n" \
183 " andc %0, %0, %3\n" \
184 " stdcx. %0, 0, %2\n" \
186 : "=&r" (t), "=m" (*p) \
187 : "r" (p), "r" (v), "m" (*p) \
189 /* __atomic_clear_long */
191 #define __atomic_clear_long(p, v, t) \
193 "1: lwarx %0, 0, %2\n" \
194 " andc %0, %0, %3\n" \
195 " stwcx. %0, 0, %2\n" \
197 : "=&r" (t), "=m" (*p) \
198 : "r" (p), "r" (v), "m" (*p) \
200 /* __atomic_clear_long */
203 #define _ATOMIC_CLEAR(type) \
204 static __inline void \
205 atomic_clear_##type(volatile u_##type *p, u_##type v) { \
207 __atomic_clear_##type(p, v, t); \
210 static __inline void \
211 atomic_clear_acq_##type(volatile u_##type *p, u_##type v) { \
213 __atomic_clear_##type(p, v, t); \
217 static __inline void \
218 atomic_clear_rel_##type(volatile u_##type *p, u_##type v) { \
221 __atomic_clear_##type(p, v, t); \
229 #define atomic_clear_32 atomic_clear_int
230 #define atomic_clear_acq_32 atomic_clear_acq_int
231 #define atomic_clear_rel_32 atomic_clear_rel_int
234 #define atomic_clear_64 atomic_clear_long
235 #define atomic_clear_acq_64 atomic_clear_acq_long
236 #define atomic_clear_rel_64 atomic_clear_rel_long
238 #define atomic_clear_ptr atomic_clear_long
239 #define atomic_clear_acq_ptr atomic_clear_acq_long
240 #define atomic_clear_rel_ptr atomic_clear_rel_long
242 #define atomic_clear_ptr atomic_clear_int
243 #define atomic_clear_acq_ptr atomic_clear_acq_int
244 #define atomic_clear_rel_ptr atomic_clear_rel_int
247 #undef __atomic_clear_long
248 #undef __atomic_clear_int
251 * atomic_cmpset(p, o, n)
253 /* TODO -- see below */
258 /* TODO -- see below */
261 * atomic_readandclear(p)
263 /* TODO -- see below */
270 #define __atomic_set_int(p, v, t) \
272 "1: lwarx %0, 0, %2\n" \
274 " stwcx. %0, 0, %2\n" \
276 : "=&r" (t), "=m" (*p) \
277 : "r" (p), "r" (v), "m" (*p) \
279 /* __atomic_set_int */
282 #define __atomic_set_long(p, v, t) \
284 "1: ldarx %0, 0, %2\n" \
286 " stdcx. %0, 0, %2\n" \
288 : "=&r" (t), "=m" (*p) \
289 : "r" (p), "r" (v), "m" (*p) \
291 /* __atomic_set_long */
293 #define __atomic_set_long(p, v, t) \
295 "1: lwarx %0, 0, %2\n" \
297 " stwcx. %0, 0, %2\n" \
299 : "=&r" (t), "=m" (*p) \
300 : "r" (p), "r" (v), "m" (*p) \
302 /* __atomic_set_long */
305 #define _ATOMIC_SET(type) \
306 static __inline void \
307 atomic_set_##type(volatile u_##type *p, u_##type v) { \
309 __atomic_set_##type(p, v, t); \
312 static __inline void \
313 atomic_set_acq_##type(volatile u_##type *p, u_##type v) { \
315 __atomic_set_##type(p, v, t); \
319 static __inline void \
320 atomic_set_rel_##type(volatile u_##type *p, u_##type v) { \
323 __atomic_set_##type(p, v, t); \
330 #define atomic_set_32 atomic_set_int
331 #define atomic_set_acq_32 atomic_set_acq_int
332 #define atomic_set_rel_32 atomic_set_rel_int
335 #define atomic_set_64 atomic_set_long
336 #define atomic_set_acq_64 atomic_set_acq_long
337 #define atomic_set_rel_64 atomic_set_rel_long
339 #define atomic_set_ptr atomic_set_long
340 #define atomic_set_acq_ptr atomic_set_acq_long
341 #define atomic_set_rel_ptr atomic_set_rel_long
343 #define atomic_set_ptr atomic_set_int
344 #define atomic_set_acq_ptr atomic_set_acq_int
345 #define atomic_set_rel_ptr atomic_set_rel_int
348 #undef __atomic_set_long
349 #undef __atomic_set_int
352 * atomic_subtract(p, v)
356 #define __atomic_subtract_int(p, v, t) \
358 "1: lwarx %0, 0, %2\n" \
359 " subf %0, %3, %0\n" \
360 " stwcx. %0, 0, %2\n" \
362 : "=&r" (t), "=m" (*p) \
363 : "r" (p), "r" (v), "m" (*p) \
365 /* __atomic_subtract_int */
368 #define __atomic_subtract_long(p, v, t) \
370 "1: ldarx %0, 0, %2\n" \
371 " subf %0, %3, %0\n" \
372 " stdcx. %0, 0, %2\n" \
374 : "=&r" (t), "=m" (*p) \
375 : "r" (p), "r" (v), "m" (*p) \
377 /* __atomic_subtract_long */
379 #define __atomic_subtract_long(p, v, t) \
381 "1: lwarx %0, 0, %2\n" \
382 " subf %0, %3, %0\n" \
383 " stwcx. %0, 0, %2\n" \
385 : "=&r" (t), "=m" (*p) \
386 : "r" (p), "r" (v), "m" (*p) \
388 /* __atomic_subtract_long */
391 #define _ATOMIC_SUBTRACT(type) \
392 static __inline void \
393 atomic_subtract_##type(volatile u_##type *p, u_##type v) { \
395 __atomic_subtract_##type(p, v, t); \
398 static __inline void \
399 atomic_subtract_acq_##type(volatile u_##type *p, u_##type v) { \
401 __atomic_subtract_##type(p, v, t); \
405 static __inline void \
406 atomic_subtract_rel_##type(volatile u_##type *p, u_##type v) { \
409 __atomic_subtract_##type(p, v, t); \
411 /* _ATOMIC_SUBTRACT */
413 _ATOMIC_SUBTRACT(int)
414 _ATOMIC_SUBTRACT(long)
416 #define atomic_subtract_32 atomic_subtract_int
417 #define atomic_subtract_acq_32 atomic_subtract_acq_int
418 #define atomic_subtract_rel_32 atomic_subtract_rel_int
421 #define atomic_subtract_64 atomic_subtract_long
422 #define atomic_subtract_acq_64 atomic_subract_acq_long
423 #define atomic_subtract_rel_64 atomic_subtract_rel_long
425 #define atomic_subtract_ptr atomic_subtract_long
426 #define atomic_subtract_acq_ptr atomic_subtract_acq_long
427 #define atomic_subtract_rel_ptr atomic_subtract_rel_long
429 #define atomic_subtract_ptr atomic_subtract_int
430 #define atomic_subtract_acq_ptr atomic_subtract_acq_int
431 #define atomic_subtract_rel_ptr atomic_subtract_rel_int
433 #undef _ATOMIC_SUBTRACT
434 #undef __atomic_subtract_long
435 #undef __atomic_subtract_int
438 * atomic_store_rel(p, v)
440 /* TODO -- see below */
443 * Old/original implementations that still need revisiting.
446 static __inline u_int
447 atomic_readandclear_int(volatile u_int *addr)
452 "\tsync\n" /* drain writes */
453 "1:\tlwarx %0, 0, %3\n\t" /* load old value */
454 "li %1, 0\n\t" /* load new value */
455 "stwcx. %1, 0, %3\n\t" /* attempt to store */
456 "bne- 1b\n\t" /* spin if failed */
457 : "=&r"(result), "=&r"(temp), "=m" (*addr)
458 : "r" (addr), "m" (*addr)
465 static __inline u_long
466 atomic_readandclear_long(volatile u_long *addr)
471 "\tsync\n" /* drain writes */
472 "1:\tldarx %0, 0, %3\n\t" /* load old value */
473 "li %1, 0\n\t" /* load new value */
474 "stdcx. %1, 0, %3\n\t" /* attempt to store */
475 "bne- 1b\n\t" /* spin if failed */
476 : "=&r"(result), "=&r"(temp), "=m" (*addr)
477 : "r" (addr), "m" (*addr)
484 #define atomic_readandclear_32 atomic_readandclear_int
487 #define atomic_readandclear_64 atomic_readandclear_long
489 #define atomic_readandclear_ptr atomic_readandclear_long
491 static __inline u_long
492 atomic_readandclear_long(volatile u_long *addr)
495 return ((u_long)atomic_readandclear_int((volatile u_int *)addr));
498 #define atomic_readandclear_ptr atomic_readandclear_int
502 * We assume that a = b will do atomic loads and stores.
504 #define ATOMIC_STORE_LOAD(TYPE) \
505 static __inline u_##TYPE \
506 atomic_load_acq_##TYPE(volatile u_##TYPE *p) \
515 static __inline void \
516 atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v) \
523 ATOMIC_STORE_LOAD(int)
525 #define atomic_load_acq_32 atomic_load_acq_int
526 #define atomic_store_rel_32 atomic_store_rel_int
529 ATOMIC_STORE_LOAD(long)
531 #define atomic_load_acq_64 atomic_load_acq_long
532 #define atomic_store_rel_64 atomic_store_rel_long
534 #define atomic_load_acq_ptr atomic_load_acq_long
535 #define atomic_store_rel_ptr atomic_store_rel_long
537 static __inline u_long
538 atomic_load_acq_long(volatile u_long *addr)
541 return ((u_long)atomic_load_acq_int((volatile u_int *)addr));
545 atomic_store_rel_long(volatile u_long *addr, u_long val)
548 atomic_store_rel_int((volatile u_int *)addr, (u_int)val);
551 #define atomic_load_acq_ptr atomic_load_acq_int
552 #define atomic_store_rel_ptr atomic_store_rel_int
554 #undef ATOMIC_STORE_LOAD
557 * Atomically compare the value stored at *p with cmpval and if the
558 * two values are equal, update the value of *p with newval. Returns
559 * zero if the compare failed, nonzero otherwise.
562 atomic_cmpset_int(volatile u_int* p, u_int cmpval, u_int newval)
567 "1:\tlwarx %0, 0, %2\n\t" /* load old value */
568 "cmplw %3, %0\n\t" /* compare */
569 "bne 2f\n\t" /* exit if not equal */
570 "stwcx. %4, 0, %2\n\t" /* attempt to store */
571 "bne- 1b\n\t" /* spin if failed */
572 "li %0, 1\n\t" /* success - retval = 1 */
573 "b 3f\n\t" /* we've succeeded */
575 "stwcx. %0, 0, %2\n\t" /* clear reservation (74xx) */
576 "li %0, 0\n\t" /* failure - retval = 0 */
578 : "=&r" (ret), "=m" (*p)
579 : "r" (p), "r" (cmpval), "r" (newval), "m" (*p)
585 atomic_cmpset_long(volatile u_long* p, u_long cmpval, u_long newval)
591 "1:\tldarx %0, 0, %2\n\t" /* load old value */
592 "cmpld %3, %0\n\t" /* compare */
593 "bne 2f\n\t" /* exit if not equal */
594 "stdcx. %4, 0, %2\n\t" /* attempt to store */
596 "1:\tlwarx %0, 0, %2\n\t" /* load old value */
597 "cmplw %3, %0\n\t" /* compare */
598 "bne 2f\n\t" /* exit if not equal */
599 "stwcx. %4, 0, %2\n\t" /* attempt to store */
601 "bne- 1b\n\t" /* spin if failed */
602 "li %0, 1\n\t" /* success - retval = 1 */
603 "b 3f\n\t" /* we've succeeded */
606 "stdcx. %0, 0, %2\n\t" /* clear reservation (74xx) */
608 "stwcx. %0, 0, %2\n\t" /* clear reservation (74xx) */
610 "li %0, 0\n\t" /* failure - retval = 0 */
612 : "=&r" (ret), "=m" (*p)
613 : "r" (p), "r" (cmpval), "r" (newval), "m" (*p)
620 atomic_cmpset_acq_int(volatile u_int *p, u_int cmpval, u_int newval)
624 retval = atomic_cmpset_int(p, cmpval, newval);
630 atomic_cmpset_rel_int(volatile u_int *p, u_int cmpval, u_int newval)
633 return (atomic_cmpset_int(p, cmpval, newval));
637 atomic_cmpset_acq_long(volatile u_long *p, u_long cmpval, u_long newval)
641 retval = atomic_cmpset_long(p, cmpval, newval);
647 atomic_cmpset_rel_long(volatile u_long *p, u_long cmpval, u_long newval)
650 return (atomic_cmpset_long(p, cmpval, newval));
653 #define atomic_cmpset_32 atomic_cmpset_int
654 #define atomic_cmpset_acq_32 atomic_cmpset_acq_int
655 #define atomic_cmpset_rel_32 atomic_cmpset_rel_int
658 #define atomic_cmpset_64 atomic_cmpset_long
659 #define atomic_cmpset_acq_64 atomic_cmpset_acq_long
660 #define atomic_cmpset_rel_64 atomic_cmpset_rel_long
662 #define atomic_cmpset_ptr atomic_cmpset_long
663 #define atomic_cmpset_acq_ptr atomic_cmpset_acq_long
664 #define atomic_cmpset_rel_ptr atomic_cmpset_rel_long
666 #define atomic_cmpset_ptr atomic_cmpset_int
667 #define atomic_cmpset_acq_ptr atomic_cmpset_acq_int
668 #define atomic_cmpset_rel_ptr atomic_cmpset_rel_int
672 * Atomically compare the value stored at *p with *cmpval and if the
673 * two values are equal, update the value of *p with newval. Returns
674 * zero if the compare failed and sets *cmpval to the read value from *p,
678 atomic_fcmpset_int(volatile u_int *p, u_int *cmpval, u_int newval)
683 "lwarx %0, 0, %3\n\t" /* load old value */
684 "cmplw %4, %0\n\t" /* compare */
685 "bne 1f\n\t" /* exit if not equal */
686 "stwcx. %5, 0, %3\n\t" /* attempt to store */
687 "bne- 1f\n\t" /* exit if failed */
688 "li %0, 1\n\t" /* success - retval = 1 */
689 "b 2f\n\t" /* we've succeeded */
691 "stwcx. %0, 0, %3\n\t" /* clear reservation (74xx) */
693 "li %0, 0\n\t" /* failure - retval = 0 */
695 : "=&r" (ret), "=m" (*p), "=m" (*cmpval)
696 : "r" (p), "r" (*cmpval), "r" (newval), "m" (*p), "r"(cmpval)
702 atomic_fcmpset_long(volatile u_long *p, u_long *cmpval, u_long newval)
708 "ldarx %0, 0, %3\n\t" /* load old value */
709 "cmpld %4, %0\n\t" /* compare */
710 "bne 1f\n\t" /* exit if not equal */
711 "stdcx. %5, 0, %3\n\t" /* attempt to store */
713 "lwarx %0, 0, %3\n\t" /* load old value */
714 "cmplw %4, %0\n\t" /* compare */
715 "bne 1f\n\t" /* exit if not equal */
716 "stwcx. %5, 0, %3\n\t" /* attempt to store */
718 "bne- 1f\n\t" /* exit if failed */
719 "li %0, 1\n\t" /* success - retval = 1 */
720 "b 2f\n\t" /* we've succeeded */
723 "stdcx. %0, 0, %3\n\t" /* clear reservation (74xx) */
726 "stwcx. %0, 0, %3\n\t" /* clear reservation (74xx) */
729 "li %0, 0\n\t" /* failure - retval = 0 */
731 : "=&r" (ret), "=m" (*p), "=m" (*cmpval)
732 : "r" (p), "r" (*cmpval), "r" (newval), "m" (*p), "r"(cmpval)
739 atomic_fcmpset_acq_int(volatile u_int *p, u_int *cmpval, u_int newval)
743 retval = atomic_fcmpset_int(p, cmpval, newval);
749 atomic_fcmpset_rel_int(volatile u_int *p, u_int *cmpval, u_int newval)
752 return (atomic_fcmpset_int(p, cmpval, newval));
756 atomic_fcmpset_acq_long(volatile u_long *p, u_long *cmpval, u_long newval)
760 retval = atomic_fcmpset_long(p, cmpval, newval);
766 atomic_fcmpset_rel_long(volatile u_long *p, u_long *cmpval, u_long newval)
769 return (atomic_fcmpset_long(p, cmpval, newval));
772 #define atomic_fcmpset_32 atomic_fcmpset_int
773 #define atomic_fcmpset_acq_32 atomic_fcmpset_acq_int
774 #define atomic_fcmpset_rel_32 atomic_fcmpset_rel_int
777 #define atomic_fcmpset_64 atomic_fcmpset_long
778 #define atomic_fcmpset_acq_64 atomic_fcmpset_acq_long
779 #define atomic_fcmpset_rel_64 atomic_fcmpset_rel_long
781 #define atomic_fcmpset_ptr atomic_fcmpset_long
782 #define atomic_fcmpset_acq_ptr atomic_fcmpset_acq_long
783 #define atomic_fcmpset_rel_ptr atomic_fcmpset_rel_long
785 #define atomic_fcmpset_ptr atomic_fcmpset_int
786 #define atomic_fcmpset_acq_ptr atomic_fcmpset_acq_int
787 #define atomic_fcmpset_rel_ptr atomic_fcmpset_rel_int
790 static __inline u_int
791 atomic_fetchadd_int(volatile u_int *p, u_int v)
797 } while (!atomic_cmpset_int(p, value, value + v));
801 static __inline u_long
802 atomic_fetchadd_long(volatile u_long *p, u_long v)
808 } while (!atomic_cmpset_long(p, value, value + v));
812 static __inline u_int
813 atomic_swap_32(volatile u_int *p, u_int v)
821 : "=&r" (prev), "+m" (*(volatile u_int *)p)
829 static __inline u_long
830 atomic_swap_64(volatile u_long *p, u_long v)
838 : "=&r" (prev), "+m" (*(volatile u_long *)p)
846 #define atomic_fetchadd_32 atomic_fetchadd_int
847 #define atomic_swap_int atomic_swap_32
850 #define atomic_fetchadd_64 atomic_fetchadd_long
851 #define atomic_swap_long atomic_swap_64
852 #define atomic_swap_ptr atomic_swap_64
859 atomic_thread_fence_acq(void)
866 atomic_thread_fence_rel(void)
873 atomic_thread_fence_acq_rel(void)
880 atomic_thread_fence_seq_cst(void)
883 __asm __volatile("sync" : : : "memory");
886 #endif /* ! _MACHINE_ATOMIC_H_ */