1 /* $NetBSD: atomic.h,v 1.1 2002/10/19 12:22:34 bsh Exp $ */
4 * Copyright (C) 2003-2004 Olivier Houchard
5 * Copyright (C) 1994-1997 Mark Brinicombe
6 * Copyright (C) 1994 Brini
9 * This code is derived from software written for Brini by Mark Brinicombe
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by Brini.
22 * 4. The name of Brini may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL BRINI BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
31 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #ifndef _MACHINE_ATOMIC_V6_H_
40 #define _MACHINE_ATOMIC_V6_H_
42 #ifndef _MACHINE_ATOMIC_H_
43 #error Do not include this file directly, use <machine/atomic.h>
47 #define isb() __asm __volatile("isb" : : : "memory")
48 #define dsb() __asm __volatile("dsb" : : : "memory")
49 #define dmb() __asm __volatile("dmb" : : : "memory")
51 #define isb() __asm __volatile("mcr p15, 0, %0, c7, c5, 4" : : "r" (0) : "memory")
52 #define dsb() __asm __volatile("mcr p15, 0, %0, c7, c10, 4" : : "r" (0) : "memory")
53 #define dmb() __asm __volatile("mcr p15, 0, %0, c7, c10, 5" : : "r" (0) : "memory")
55 #error Only use this file with ARMv6 and later
62 #define ARM_HAVE_ATOMIC64
64 #define ATOMIC_ACQ_REL_LONG(NAME) \
65 static __inline void \
66 atomic_##NAME##_acq_long(__volatile u_long *p, u_long v) \
68 atomic_##NAME##_long(p, v); \
72 static __inline void \
73 atomic_##NAME##_rel_long(__volatile u_long *p, u_long v) \
76 atomic_##NAME##_long(p, v); \
79 #define ATOMIC_ACQ_REL(NAME, WIDTH) \
80 static __inline void \
81 atomic_##NAME##_acq_##WIDTH(__volatile uint##WIDTH##_t *p, uint##WIDTH##_t v)\
83 atomic_##NAME##_##WIDTH(p, v); \
87 static __inline void \
88 atomic_##NAME##_rel_##WIDTH(__volatile uint##WIDTH##_t *p, uint##WIDTH##_t v)\
91 atomic_##NAME##_##WIDTH(p, v); \
96 atomic_add_32(volatile uint32_t *p, uint32_t val)
98 uint32_t tmp = 0, tmp2 = 0;
101 "1: ldrex %0, [%2] \n"
103 " strex %1, %0, [%2] \n"
107 : "=&r" (tmp), "+r" (tmp2)
108 ,"+r" (p), "+r" (val) : : "cc", "memory");
112 atomic_add_64(volatile uint64_t *p, uint64_t val)
119 " ldrexd %Q[tmp], %R[tmp], [%[ptr]] \n"
120 " adds %Q[tmp], %Q[val] \n"
121 " adc %R[tmp], %R[tmp], %R[val] \n"
122 " strexd %[exf], %Q[tmp], %R[tmp], [%[ptr]] \n"
126 : [exf] "=&r" (exflag),
134 atomic_add_long(volatile u_long *p, u_long val)
137 atomic_add_32((volatile uint32_t *)p, val);
140 ATOMIC_ACQ_REL(add, 32)
141 ATOMIC_ACQ_REL(add, 64)
142 ATOMIC_ACQ_REL_LONG(add)
145 atomic_clear_32(volatile uint32_t *address, uint32_t setmask)
147 uint32_t tmp = 0, tmp2 = 0;
150 "1: ldrex %0, [%2] \n"
152 " strex %1, %0, [%2] \n"
156 : "=&r" (tmp), "+r" (tmp2), "+r" (address), "+r" (setmask)
161 atomic_clear_64(volatile uint64_t *p, uint64_t val)
168 " ldrexd %Q[tmp], %R[tmp], [%[ptr]] \n"
169 " bic %Q[tmp], %Q[val] \n"
170 " bic %R[tmp], %R[val] \n"
171 " strexd %[exf], %Q[tmp], %R[tmp], [%[ptr]] \n"
175 : [exf] "=&r" (exflag),
183 atomic_clear_long(volatile u_long *address, u_long setmask)
186 atomic_clear_32((volatile uint32_t *)address, setmask);
189 ATOMIC_ACQ_REL(clear, 32)
190 ATOMIC_ACQ_REL(clear, 64)
191 ATOMIC_ACQ_REL_LONG(clear)
193 #define ATOMIC_FCMPSET_CODE(RET, TYPE, SUF) \
198 "1: ldrex" SUF " %[tmp], [%[ptr]] \n" \
199 " ldr %[ret], [%[oldv]] \n" \
200 " teq %[tmp], %[ret] \n" \
202 " str" SUF "ne %[tmp], [%[oldv]] \n" \
203 " movne %[ret], #0 \n" \
204 " strex" SUF "eq %[ret], %[newv], [%[ptr]] \n" \
205 " eorseq %[ret], #1 \n" \
207 : [ret] "=&r" (RET), \
209 : [ptr] "r" (_ptr), \
215 #define ATOMIC_FCMPSET_CODE64(RET) \
220 "1: ldrexd %Q[tmp], %R[tmp], [%[ptr]] \n" \
221 " ldrd %Q[cmp], %R[cmp], [%[oldv]] \n" \
222 " teq %Q[tmp], %Q[cmp] \n" \
224 " teqeq %R[tmp], %R[cmp] \n" \
226 " movne %[ret], #0 \n" \
227 " strdne %[cmp], [%[oldv]] \n" \
228 " strexdeq %[ret], %Q[newv], %R[newv], [%[ptr]] \n" \
229 " eorseq %[ret], #1 \n" \
231 : [ret] "=&r" (RET), \
234 : [ptr] "r" (_ptr), \
241 atomic_fcmpset_8(volatile uint8_t *_ptr, uint8_t *_old, uint8_t _new)
245 ATOMIC_FCMPSET_CODE(ret, uint8_t, "b");
250 atomic_fcmpset_acq_8(volatile uint8_t *_ptr, uint8_t *_old, uint8_t _new)
254 ATOMIC_FCMPSET_CODE(ret, uint8_t, "b");
260 atomic_fcmpset_rel_8(volatile uint8_t *_ptr, uint8_t *_old, uint8_t _new)
265 ATOMIC_FCMPSET_CODE(ret, uint8_t, "b");
270 atomic_fcmpset_16(volatile uint16_t *_ptr, uint16_t *_old, uint16_t _new)
274 ATOMIC_FCMPSET_CODE(ret, uint16_t, "h");
279 atomic_fcmpset_acq_16(volatile uint16_t *_ptr, uint16_t *_old, uint16_t _new)
283 ATOMIC_FCMPSET_CODE(ret, uint16_t, "h");
289 atomic_fcmpset_rel_16(volatile uint16_t *_ptr, uint16_t *_old, uint16_t _new)
294 ATOMIC_FCMPSET_CODE(ret, uint16_t, "h");
299 atomic_fcmpset_32(volatile uint32_t *_ptr, uint32_t *_old, uint32_t _new)
303 ATOMIC_FCMPSET_CODE(ret, uint32_t, "");
308 atomic_fcmpset_acq_32(volatile uint32_t *_ptr, uint32_t *_old, uint32_t _new)
312 ATOMIC_FCMPSET_CODE(ret, uint32_t, "");
318 atomic_fcmpset_rel_32(volatile uint32_t *_ptr, uint32_t *_old, uint32_t _new)
323 ATOMIC_FCMPSET_CODE(ret, uint32_t, "");
328 atomic_fcmpset_long(volatile long *_ptr, long *_old, long _new)
332 ATOMIC_FCMPSET_CODE(ret, long, "");
337 atomic_fcmpset_acq_long(volatile long *_ptr, long *_old, long _new)
341 ATOMIC_FCMPSET_CODE(ret, long, "");
347 atomic_fcmpset_rel_long(volatile long *_ptr, long *_old, long _new)
352 ATOMIC_FCMPSET_CODE(ret, long, "");
357 atomic_fcmpset_64(volatile uint64_t *_ptr, uint64_t *_old, uint64_t _new)
361 ATOMIC_FCMPSET_CODE64(ret);
366 atomic_fcmpset_acq_64(volatile uint64_t *_ptr, uint64_t *_old, uint64_t _new)
370 ATOMIC_FCMPSET_CODE64(ret);
376 atomic_fcmpset_rel_64(volatile uint64_t *_ptr, uint64_t *_old, uint64_t _new)
381 ATOMIC_FCMPSET_CODE64(ret);
385 #define ATOMIC_CMPSET_CODE(RET, SUF) \
388 "1: ldrex" SUF " %[ret], [%[ptr]] \n" \
389 " teq %[ret], %[oldv] \n" \
391 " movne %[ret], #0 \n" \
392 " strex" SUF "eq %[ret], %[newv], [%[ptr]] \n" \
393 " eorseq %[ret], #1 \n" \
395 : [ret] "=&r" (RET) \
396 : [ptr] "r" (_ptr), \
402 #define ATOMIC_CMPSET_CODE64(RET) \
407 "1: ldrexd %Q[tmp], %R[tmp], [%[ptr]] \n" \
408 " teq %Q[tmp], %Q[oldv] \n" \
410 " teqeq %R[tmp], %R[oldv] \n" \
412 " movne %[ret], #0 \n" \
413 " strexdeq %[ret], %Q[newv], %R[newv], [%[ptr]] \n" \
414 " eorseq %[ret], #1 \n" \
416 : [ret] "=&r" (RET), \
418 : [ptr] "r" (_ptr), \
425 atomic_cmpset_8(volatile uint8_t *_ptr, uint8_t _old, uint8_t _new)
429 ATOMIC_CMPSET_CODE(ret, "b");
434 atomic_cmpset_acq_8(volatile uint8_t *_ptr, uint8_t _old, uint8_t _new)
438 ATOMIC_CMPSET_CODE(ret, "b");
444 atomic_cmpset_rel_8(volatile uint8_t *_ptr, uint8_t _old, uint8_t _new)
449 ATOMIC_CMPSET_CODE(ret, "b");
454 atomic_cmpset_16(volatile uint16_t *_ptr, uint16_t _old, uint16_t _new)
458 ATOMIC_CMPSET_CODE(ret, "h");
463 atomic_cmpset_acq_16(volatile uint16_t *_ptr, uint16_t _old, uint16_t _new)
467 ATOMIC_CMPSET_CODE(ret, "h");
473 atomic_cmpset_rel_16(volatile uint16_t *_ptr, uint16_t _old, uint16_t _new)
478 ATOMIC_CMPSET_CODE(ret, "h");
483 atomic_cmpset_32(volatile uint32_t *_ptr, uint32_t _old, uint32_t _new)
487 ATOMIC_CMPSET_CODE(ret, "");
492 atomic_cmpset_acq_32(volatile uint32_t *_ptr, uint32_t _old, uint32_t _new)
496 ATOMIC_CMPSET_CODE(ret, "");
502 atomic_cmpset_rel_32(volatile uint32_t *_ptr, uint32_t _old, uint32_t _new)
507 ATOMIC_CMPSET_CODE(ret, "");
512 atomic_cmpset_long(volatile long *_ptr, long _old, long _new)
516 ATOMIC_CMPSET_CODE(ret, "");
521 atomic_cmpset_acq_long(volatile long *_ptr, long _old, long _new)
525 ATOMIC_CMPSET_CODE(ret, "");
531 atomic_cmpset_rel_long(volatile long *_ptr, long _old, long _new)
536 ATOMIC_CMPSET_CODE(ret, "");
541 atomic_cmpset_64(volatile uint64_t *_ptr, uint64_t _old, uint64_t _new)
545 ATOMIC_CMPSET_CODE64(ret);
550 atomic_cmpset_acq_64(volatile uint64_t *_ptr, uint64_t _old, uint64_t _new)
554 ATOMIC_CMPSET_CODE64(ret);
560 atomic_cmpset_rel_64(volatile uint64_t *_ptr, uint64_t _old, uint64_t _new)
565 ATOMIC_CMPSET_CODE64(ret);
569 static __inline uint32_t
570 atomic_fetchadd_32(volatile uint32_t *p, uint32_t val)
572 uint32_t tmp = 0, tmp2 = 0, ret = 0;
575 "1: ldrex %0, [%3] \n"
577 " strex %2, %1, [%3] \n"
581 : "+r" (ret), "=&r" (tmp), "+r" (tmp2), "+r" (p), "+r" (val)
586 static __inline uint64_t
587 atomic_fetchadd_64(volatile uint64_t *p, uint64_t val)
594 " ldrexd %Q[ret], %R[ret], [%[ptr]] \n"
595 " adds %Q[tmp], %Q[ret], %Q[val] \n"
596 " adc %R[tmp], %R[ret], %R[val] \n"
597 " strexd %[exf], %Q[tmp], %R[tmp], [%[ptr]] \n"
602 [exf] "=&r" (exflag),
610 static __inline u_long
611 atomic_fetchadd_long(volatile u_long *p, u_long val)
614 return (atomic_fetchadd_32((volatile uint32_t *)p, val));
617 static __inline uint32_t
618 atomic_load_acq_32(volatile uint32_t *p)
627 static __inline uint64_t
628 atomic_load_64(volatile uint64_t *p)
633 * The only way to atomically load 64 bits is with LDREXD which puts the
634 * exclusive monitor into the exclusive state, so reset it to open state
635 * with CLREX because we don't actually need to store anything.
638 "ldrexd %Q[ret], %R[ret], [%[ptr]] \n"
646 static __inline uint64_t
647 atomic_load_acq_64(volatile uint64_t *p)
651 ret = atomic_load_64(p);
656 static __inline u_long
657 atomic_load_acq_long(volatile u_long *p)
666 static __inline uint32_t
667 atomic_readandclear_32(volatile uint32_t *p)
669 uint32_t ret, tmp = 0, tmp2 = 0;
672 "1: ldrex %0, [%3] \n"
674 " strex %2, %1, [%3] \n"
678 : "=r" (ret), "=&r" (tmp), "+r" (tmp2), "+r" (p)
683 static __inline uint64_t
684 atomic_readandclear_64(volatile uint64_t *p)
691 " ldrexd %Q[ret], %R[ret], [%[ptr]] \n"
692 " mov %Q[tmp], #0 \n"
693 " mov %R[tmp], #0 \n"
694 " strexd %[exf], %Q[tmp], %R[tmp], [%[ptr]] \n"
699 [exf] "=&r" (exflag),
706 static __inline u_long
707 atomic_readandclear_long(volatile u_long *p)
710 return (atomic_readandclear_32((volatile uint32_t *)p));
714 atomic_set_32(volatile uint32_t *address, uint32_t setmask)
716 uint32_t tmp = 0, tmp2 = 0;
719 "1: ldrex %0, [%2] \n"
721 " strex %1, %0, [%2] \n"
725 : "=&r" (tmp), "+r" (tmp2), "+r" (address), "+r" (setmask)
730 atomic_set_64(volatile uint64_t *p, uint64_t val)
737 " ldrexd %Q[tmp], %R[tmp], [%[ptr]] \n"
738 " orr %Q[tmp], %Q[val] \n"
739 " orr %R[tmp], %R[val] \n"
740 " strexd %[exf], %Q[tmp], %R[tmp], [%[ptr]] \n"
744 : [exf] "=&r" (exflag),
752 atomic_set_long(volatile u_long *address, u_long setmask)
755 atomic_set_32((volatile uint32_t *)address, setmask);
758 ATOMIC_ACQ_REL(set, 32)
759 ATOMIC_ACQ_REL(set, 64)
760 ATOMIC_ACQ_REL_LONG(set)
763 atomic_subtract_32(volatile uint32_t *p, uint32_t val)
765 uint32_t tmp = 0, tmp2 = 0;
768 "1: ldrex %0, [%2] \n"
770 " strex %1, %0, [%2] \n"
774 : "=&r" (tmp), "+r" (tmp2), "+r" (p), "+r" (val)
779 atomic_subtract_64(volatile uint64_t *p, uint64_t val)
786 " ldrexd %Q[tmp], %R[tmp], [%[ptr]] \n"
787 " subs %Q[tmp], %Q[val] \n"
788 " sbc %R[tmp], %R[tmp], %R[val] \n"
789 " strexd %[exf], %Q[tmp], %R[tmp], [%[ptr]] \n"
793 : [exf] "=&r" (exflag),
801 atomic_subtract_long(volatile u_long *p, u_long val)
804 atomic_subtract_32((volatile uint32_t *)p, val);
807 ATOMIC_ACQ_REL(subtract, 32)
808 ATOMIC_ACQ_REL(subtract, 64)
809 ATOMIC_ACQ_REL_LONG(subtract)
812 atomic_store_64(volatile uint64_t *p, uint64_t val)
818 * The only way to atomically store 64 bits is with STREXD, which will
819 * succeed only if paired up with a preceeding LDREXD using the same
820 * address, so we read and discard the existing value before storing.
824 " ldrexd %Q[tmp], %R[tmp], [%[ptr]] \n"
825 " strexd %[exf], %Q[val], %R[val], [%[ptr]] \n"
837 atomic_store_rel_32(volatile uint32_t *p, uint32_t v)
845 atomic_store_rel_64(volatile uint64_t *p, uint64_t val)
849 atomic_store_64(p, val);
853 atomic_store_rel_long(volatile u_long *p, u_long v)
861 atomic_testandset_32(volatile uint32_t *p, u_int v)
863 uint32_t tmp, tmp2, res, mask;
865 mask = 1u << (v & 0x1f);
868 "1: ldrex %0, [%4] \n"
870 " strex %2, %1, [%4] \n"
874 : "=&r" (res), "=&r" (tmp), "=&r" (tmp2)
875 : "r" (mask), "r" (p)
877 return ((res & mask) != 0);
881 atomic_testandset_int(volatile u_int *p, u_int v)
884 return (atomic_testandset_32((volatile uint32_t *)p, v));
888 atomic_testandset_long(volatile u_long *p, u_int v)
891 return (atomic_testandset_32((volatile uint32_t *)p, v));
895 atomic_testandset_64(volatile uint64_t *p, u_int v)
897 volatile uint32_t *p32;
899 p32 = (volatile uint32_t *)p;
900 /* Assume little-endian */
905 return (atomic_testandset_32(p32, v));
908 static __inline uint32_t
909 atomic_swap_32(volatile uint32_t *p, uint32_t v)
911 uint32_t ret, exflag;
914 "1: ldrex %[ret], [%[ptr]] \n"
915 " strex %[exf], %[val], [%[ptr]] \n"
927 static __inline uint64_t
928 atomic_swap_64(volatile uint64_t *p, uint64_t v)
934 "1: ldrexd %Q[ret], %R[ret], [%[ptr]] \n"
935 " strexd %[exf], %Q[val], %R[val], [%[ptr]] \n"
947 #undef ATOMIC_ACQ_REL
948 #undef ATOMIC_ACQ_REL_LONG
951 atomic_thread_fence_acq(void)
958 atomic_thread_fence_rel(void)
965 atomic_thread_fence_acq_rel(void)
972 atomic_thread_fence_seq_cst(void)
978 #endif /* _MACHINE_ATOMIC_V6_H_ */