xref: /dflybsd-src/sys/dev/drm/include/asm/cmpxchg.h (revision ab413216e0477d18360cae566110a40c280c86b8)
1*ab413216SFrançois Tigeot /*
2*ab413216SFrançois Tigeot  * Copyright (c) 2020 François Tigeot <ftigeot@wolfpond.org>
3*ab413216SFrançois Tigeot  * All rights reserved.
4*ab413216SFrançois Tigeot  *
5*ab413216SFrançois Tigeot  * Redistribution and use in source and binary forms, with or without
6*ab413216SFrançois Tigeot  * modification, are permitted provided that the following conditions
7*ab413216SFrançois Tigeot  * are met:
8*ab413216SFrançois Tigeot  * 1. Redistributions of source code must retain the above copyright
9*ab413216SFrançois Tigeot  *    notice unmodified, this list of conditions, and the following
10*ab413216SFrançois Tigeot  *    disclaimer.
11*ab413216SFrançois Tigeot  * 2. Redistributions in binary form must reproduce the above copyright
12*ab413216SFrançois Tigeot  *    notice, this list of conditions and the following disclaimer in the
13*ab413216SFrançois Tigeot  *    documentation and/or other materials provided with the distribution.
14*ab413216SFrançois Tigeot  *
15*ab413216SFrançois Tigeot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*ab413216SFrançois Tigeot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*ab413216SFrançois Tigeot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*ab413216SFrançois Tigeot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*ab413216SFrançois Tigeot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*ab413216SFrançois Tigeot  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*ab413216SFrançois Tigeot  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*ab413216SFrançois Tigeot  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*ab413216SFrançois Tigeot  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*ab413216SFrançois Tigeot  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*ab413216SFrançois Tigeot  */
26*ab413216SFrançois Tigeot 
27*ab413216SFrançois Tigeot #ifndef	_ASM_CMPXCHG_H_
28*ab413216SFrançois Tigeot #define	_ASM_CMPXCHG_H_
29*ab413216SFrançois Tigeot 
30*ab413216SFrançois Tigeot #define xchg(ptr, value)				\
31*ab413216SFrançois Tigeot ({							\
32*ab413216SFrançois Tigeot 	__typeof(value) __ret = (value);		\
33*ab413216SFrançois Tigeot 							\
34*ab413216SFrançois Tigeot 	switch (sizeof(value)) {			\
35*ab413216SFrançois Tigeot 	case 8:						\
36*ab413216SFrançois Tigeot 		__asm __volatile("xchgq %0, %1"		\
37*ab413216SFrançois Tigeot 		    : "+r" (__ret), "+m" (*(ptr))	\
38*ab413216SFrançois Tigeot 		    : : "memory");			\
39*ab413216SFrançois Tigeot 		break;					\
40*ab413216SFrançois Tigeot 	case 4:						\
41*ab413216SFrançois Tigeot 		__asm __volatile("xchgl %0, %1"		\
42*ab413216SFrançois Tigeot 		    : "+r" (__ret), "+m" (*(ptr))	\
43*ab413216SFrançois Tigeot 		    : : "memory");			\
44*ab413216SFrançois Tigeot 		break;					\
45*ab413216SFrançois Tigeot 	case 2:						\
46*ab413216SFrançois Tigeot 		__asm __volatile("xchgw %0, %1"		\
47*ab413216SFrançois Tigeot 		    : "+r" (__ret), "+m" (*(ptr))	\
48*ab413216SFrançois Tigeot 		    : : "memory");			\
49*ab413216SFrançois Tigeot 		break;					\
50*ab413216SFrançois Tigeot 	case 1:						\
51*ab413216SFrançois Tigeot 		__asm __volatile("xchgb %0, %1"		\
52*ab413216SFrançois Tigeot 		    : "+r" (__ret), "+m" (*(ptr))	\
53*ab413216SFrançois Tigeot 		    : : "memory");			\
54*ab413216SFrançois Tigeot 		break;					\
55*ab413216SFrançois Tigeot 	default:					\
56*ab413216SFrançois Tigeot 		panic("xchg(): invalid size %ld\n", sizeof(value)); \
57*ab413216SFrançois Tigeot 	}						\
58*ab413216SFrançois Tigeot 							\
59*ab413216SFrançois Tigeot 	__ret;						\
60*ab413216SFrançois Tigeot })
61*ab413216SFrançois Tigeot 
62*ab413216SFrançois Tigeot #endif	/* _ASM_CMPXCHG_H_ */
63