xref: /minix3/common/lib/libc/arch/aarch64/atomic/atomic_op_asm.h (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* $NetBSD: atomic_op_asm.h,v 1.1 2014/08/10 05:47:35 matt Exp $ */
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc  * Copyright (c) 2014 The NetBSD Foundation, Inc.
5*0a6a1f1dSLionel Sambuc  * All rights reserved.
6*0a6a1f1dSLionel Sambuc  *
7*0a6a1f1dSLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
8*0a6a1f1dSLionel Sambuc  * by Matt Thomas of 3am Software Foundry.
9*0a6a1f1dSLionel Sambuc  *
10*0a6a1f1dSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11*0a6a1f1dSLionel Sambuc  * modification, are permitted provided that the following conditions
12*0a6a1f1dSLionel Sambuc  * are met:
13*0a6a1f1dSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15*0a6a1f1dSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*0a6a1f1dSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*0a6a1f1dSLionel Sambuc  *
19*0a6a1f1dSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*0a6a1f1dSLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*0a6a1f1dSLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*0a6a1f1dSLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*0a6a1f1dSLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*0a6a1f1dSLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*0a6a1f1dSLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*0a6a1f1dSLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*0a6a1f1dSLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*0a6a1f1dSLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*0a6a1f1dSLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
30*0a6a1f1dSLionel Sambuc  */
31*0a6a1f1dSLionel Sambuc 
32*0a6a1f1dSLionel Sambuc #ifndef _ATOMIC_OP_ASM_H_
33*0a6a1f1dSLionel Sambuc #define	_ATOMIC_OP_ASM_H_
34*0a6a1f1dSLionel Sambuc 
35*0a6a1f1dSLionel Sambuc #include <machine/asm.h>
36*0a6a1f1dSLionel Sambuc 
37*0a6a1f1dSLionel Sambuc #define	ATOMIC_OP8(OP, INSN)						\
38*0a6a1f1dSLionel Sambuc ENTRY_NP(_atomic_##OP##_8)						;\
39*0a6a1f1dSLionel Sambuc 	mov	x4, x0							;\
40*0a6a1f1dSLionel Sambuc 1:	ldxrb	w0, [x4]		/* load old value */		;\
41*0a6a1f1dSLionel Sambuc 	INSN	w2, w1, w0		/* calculate new value */	;\
42*0a6a1f1dSLionel Sambuc 	stxrb	w3, w2, [x4]		/* try to store */		;\
43*0a6a1f1dSLionel Sambuc 	cbnz	w3, 1b			/*   succeed? no, try again */	;\
44*0a6a1f1dSLionel Sambuc 	dmb	st							;\
45*0a6a1f1dSLionel Sambuc 	ret				/* return old value */		;\
46*0a6a1f1dSLionel Sambuc END(_atomic_##OP##_8)
47*0a6a1f1dSLionel Sambuc 
48*0a6a1f1dSLionel Sambuc #define	ATOMIC_OP8_NV(OP, INSN)						\
49*0a6a1f1dSLionel Sambuc ENTRY_NP(_atomic_##OP##_8_nv)						;\
50*0a6a1f1dSLionel Sambuc 	mov	x4, x0			/* need r0 for return value */	;\
51*0a6a1f1dSLionel Sambuc 1:	ldxrb	w0, [x4]		/* load old value */		;\
52*0a6a1f1dSLionel Sambuc 	INSN	w2, w1, w0		/* calc new (return) value */	;\
53*0a6a1f1dSLionel Sambuc 	stxrb	w3, w2, [x4]		/* try to store */		;\
54*0a6a1f1dSLionel Sambuc 	cbnz	w3, 1b			/*   succeed? no, try again */	;\
55*0a6a1f1dSLionel Sambuc 	dmb	st							;\
56*0a6a1f1dSLionel Sambuc 	ret				/* return new value */		;\
57*0a6a1f1dSLionel Sambuc END(_atomic_##OP##_8_nv)
58*0a6a1f1dSLionel Sambuc 
59*0a6a1f1dSLionel Sambuc #define	ATOMIC_OP16(OP, INSN)						\
60*0a6a1f1dSLionel Sambuc ENTRY_NP(_atomic_##OP##_16)						;\
61*0a6a1f1dSLionel Sambuc 	mov	x4, x0							;\
62*0a6a1f1dSLionel Sambuc 1:	ldxrh	w0, [x4]		/* load old value */		;\
63*0a6a1f1dSLionel Sambuc 	INSN	w2, w1, w0		/* calculate new value */	;\
64*0a6a1f1dSLionel Sambuc 	stxrh	w3, w2, [x4]		/* try to store */		;\
65*0a6a1f1dSLionel Sambuc 	cbnz	w3, 1b			/*   succeed? no, try again */	;\
66*0a6a1f1dSLionel Sambuc 	dmb	st							;\
67*0a6a1f1dSLionel Sambuc 	ret				/* return old value */		;\
68*0a6a1f1dSLionel Sambuc END(_atomic_##OP##_16)
69*0a6a1f1dSLionel Sambuc 
70*0a6a1f1dSLionel Sambuc #define	ATOMIC_OP16_NV(OP, INSN)					\
71*0a6a1f1dSLionel Sambuc ENTRY_NP(_atomic_##OP##_16_nv)						;\
72*0a6a1f1dSLionel Sambuc 	mov	x4, x0			/* need r0 for return value */	;\
73*0a6a1f1dSLionel Sambuc 1:	ldxrh	w0, [x4]		/* load old value */		;\
74*0a6a1f1dSLionel Sambuc 	INSN	w2, w1, w0		/* calc new (return) value */	;\
75*0a6a1f1dSLionel Sambuc 	stxrh	w3, w2, [x4]		/* try to store */		;\
76*0a6a1f1dSLionel Sambuc 	cbnz	w3, 1b			/*   succeed? no, try again */	;\
77*0a6a1f1dSLionel Sambuc 	dmb	st							;\
78*0a6a1f1dSLionel Sambuc 	ret				/* return new value */		;\
79*0a6a1f1dSLionel Sambuc END(_atomic_##OP##_16_nv)
80*0a6a1f1dSLionel Sambuc 
81*0a6a1f1dSLionel Sambuc #define	ATOMIC_OP32(OP, INSN)						\
82*0a6a1f1dSLionel Sambuc ENTRY_NP(_atomic_##OP##_32)						;\
83*0a6a1f1dSLionel Sambuc 	mov	x4, x0							;\
84*0a6a1f1dSLionel Sambuc 1:	ldxr	w0, [x4]		/* load old value */		;\
85*0a6a1f1dSLionel Sambuc 	INSN	w2, w1, w0		/* calculate new value */	;\
86*0a6a1f1dSLionel Sambuc 	stxr	w3, w2, [x4]		/* try to store */		;\
87*0a6a1f1dSLionel Sambuc 	cbnz	w3, 1b			/*   succeed? no, try again */	;\
88*0a6a1f1dSLionel Sambuc 	dmb	st							;\
89*0a6a1f1dSLionel Sambuc 	ret				/* return old value */		;\
90*0a6a1f1dSLionel Sambuc END(_atomic_##OP##_32)
91*0a6a1f1dSLionel Sambuc 
92*0a6a1f1dSLionel Sambuc #define	ATOMIC_OP32_NV(OP, INSN)					\
93*0a6a1f1dSLionel Sambuc ENTRY_NP(_atomic_##OP##_32_nv)						;\
94*0a6a1f1dSLionel Sambuc 	mov	x4, x0			/* need r0 for return value */	;\
95*0a6a1f1dSLionel Sambuc 1:	ldxr	w0, [x4]		/* load old value */		;\
96*0a6a1f1dSLionel Sambuc 	INSN	w0, w0, w1		/* calc new (return) value */	;\
97*0a6a1f1dSLionel Sambuc 	stxr	w3, w0, [x4]		/* try to store */		;\
98*0a6a1f1dSLionel Sambuc 	cbnz	w3, 1b			/*   succeed? no, try again? */	;\
99*0a6a1f1dSLionel Sambuc 	dmb	sy							;\
100*0a6a1f1dSLionel Sambuc 	ret				/* return new value */		;\
101*0a6a1f1dSLionel Sambuc END(_atomic_##OP##_32_nv)
102*0a6a1f1dSLionel Sambuc 
103*0a6a1f1dSLionel Sambuc #define	ATOMIC_OP64(OP, INSN)						\
104*0a6a1f1dSLionel Sambuc ENTRY_NP(_atomic_##OP##_64)						;\
105*0a6a1f1dSLionel Sambuc 	mov	x4, x0							;\
106*0a6a1f1dSLionel Sambuc 1:	ldxr	x0, [x4]		/* load old value */		;\
107*0a6a1f1dSLionel Sambuc 	INSN	x2, x1, x0		/* calculate new value */	;\
108*0a6a1f1dSLionel Sambuc 	stxr	w3, x2, [x4]		/* try to store */		;\
109*0a6a1f1dSLionel Sambuc 	cbnz	w3, 1b			/*   succeed? no, try again */	;\
110*0a6a1f1dSLionel Sambuc 	dmb	st							;\
111*0a6a1f1dSLionel Sambuc 	ret				/* return old value */		;\
112*0a6a1f1dSLionel Sambuc END(_atomic_##OP##_64)
113*0a6a1f1dSLionel Sambuc 
114*0a6a1f1dSLionel Sambuc #define	ATOMIC_OP64_NV(OP, INSN)					\
115*0a6a1f1dSLionel Sambuc ENTRY_NP(_atomic_##OP##_64_nv)						;\
116*0a6a1f1dSLionel Sambuc 	mov	x4, x0			/* need r0 for return value */	;\
117*0a6a1f1dSLionel Sambuc 1:	ldxr	x0, [x4]		/* load old value */		;\
118*0a6a1f1dSLionel Sambuc 	INSN	x0, x0, x1		/* calc new (return) value */	;\
119*0a6a1f1dSLionel Sambuc 	stxr	w3, x0, [x4]		/* try to store */		;\
120*0a6a1f1dSLionel Sambuc 	cbnz	w3, 1b			/*   succeed? no, try again? */	;\
121*0a6a1f1dSLionel Sambuc 	dmb	sy							;\
122*0a6a1f1dSLionel Sambuc 	ret				/* return new value */		;\
123*0a6a1f1dSLionel Sambuc END(_atomic_##OP##_64_nv)
124*0a6a1f1dSLionel Sambuc 
125*0a6a1f1dSLionel Sambuc #if defined(_KERNEL)
126*0a6a1f1dSLionel Sambuc 
127*0a6a1f1dSLionel Sambuc #define	ATOMIC_OP_ALIAS(a,s)	STRONG_ALIAS(a,s)
128*0a6a1f1dSLionel Sambuc 
129*0a6a1f1dSLionel Sambuc #else /* _KERNEL */
130*0a6a1f1dSLionel Sambuc 
131*0a6a1f1dSLionel Sambuc #define	ATOMIC_OP_ALIAS(a,s)	WEAK_ALIAS(a,s)
132*0a6a1f1dSLionel Sambuc 
133*0a6a1f1dSLionel Sambuc #endif /* _KERNEL */
134*0a6a1f1dSLionel Sambuc 
135*0a6a1f1dSLionel Sambuc #endif /* _ATOMIC_OP_ASM_H_ */
136