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