xref: /openbmc/linux/arch/s390/include/asm/cmpxchg.h (revision d7a3d85e)
1 /*
2  * Copyright IBM Corp. 1999, 2011
3  *
4  * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
5  */
6 
7 #ifndef __ASM_CMPXCHG_H
8 #define __ASM_CMPXCHG_H
9 
10 #include <linux/mmdebug.h>
11 #include <linux/types.h>
12 #include <linux/bug.h>
13 
14 #define cmpxchg(ptr, o, n)						\
15 ({									\
16 	__typeof__(*(ptr)) __o = (o);					\
17 	__typeof__(*(ptr)) __n = (n);					\
18 	(__typeof__(*(ptr))) __sync_val_compare_and_swap((ptr),__o,__n);\
19 })
20 
21 #define cmpxchg64	cmpxchg
22 #define cmpxchg_local	cmpxchg
23 #define cmpxchg64_local	cmpxchg
24 
25 #define xchg(ptr, x)							\
26 ({									\
27 	__typeof__(ptr) __ptr = (ptr);					\
28 	__typeof__(*(ptr)) __old;					\
29 	do {								\
30 		__old = *__ptr;						\
31 	} while (!__sync_bool_compare_and_swap(__ptr, __old, x));	\
32 	__old;								\
33 })
34 
35 #define __HAVE_ARCH_CMPXCHG
36 
37 #define __cmpxchg_double_op(p1, p2, o1, o2, n1, n2, insn)		\
38 ({									\
39 	register __typeof__(*(p1)) __old1 asm("2") = (o1);		\
40 	register __typeof__(*(p2)) __old2 asm("3") = (o2);		\
41 	register __typeof__(*(p1)) __new1 asm("4") = (n1);		\
42 	register __typeof__(*(p2)) __new2 asm("5") = (n2);		\
43 	int cc;								\
44 	asm volatile(							\
45 			insn   " %[old],%[new],%[ptr]\n"		\
46 		"	ipm	%[cc]\n"				\
47 		"	srl	%[cc],28"				\
48 		: [cc] "=d" (cc), [old] "+d" (__old1), "+d" (__old2)	\
49 		: [new] "d" (__new1), "d" (__new2),			\
50 		  [ptr] "Q" (*(p1)), "Q" (*(p2))			\
51 		: "memory", "cc");					\
52 	!cc;								\
53 })
54 
55 #define __cmpxchg_double_4(p1, p2, o1, o2, n1, n2) \
56 	__cmpxchg_double_op(p1, p2, o1, o2, n1, n2, "cds")
57 
58 #define __cmpxchg_double_8(p1, p2, o1, o2, n1, n2) \
59 	__cmpxchg_double_op(p1, p2, o1, o2, n1, n2, "cdsg")
60 
61 extern void __cmpxchg_double_called_with_bad_pointer(void);
62 
63 #define __cmpxchg_double(p1, p2, o1, o2, n1, n2)			\
64 ({									\
65 	int __ret;							\
66 	switch (sizeof(*(p1))) {					\
67 	case 4:								\
68 		__ret = __cmpxchg_double_4(p1, p2, o1, o2, n1, n2);	\
69 		break;							\
70 	case 8:								\
71 		__ret = __cmpxchg_double_8(p1, p2, o1, o2, n1, n2);	\
72 		break;							\
73 	default:							\
74 		__cmpxchg_double_called_with_bad_pointer();		\
75 	}								\
76 	__ret;								\
77 })
78 
79 #define cmpxchg_double(p1, p2, o1, o2, n1, n2)				\
80 ({									\
81 	__typeof__(p1) __p1 = (p1);					\
82 	__typeof__(p2) __p2 = (p2);					\
83 	BUILD_BUG_ON(sizeof(*(p1)) != sizeof(long));			\
84 	BUILD_BUG_ON(sizeof(*(p2)) != sizeof(long));			\
85 	VM_BUG_ON((unsigned long)((__p1) + 1) != (unsigned long)(__p2));\
86 	__cmpxchg_double_8(__p1, __p2, o1, o2, n1, n2);			\
87 })
88 
89 #define system_has_cmpxchg_double()	1
90 
91 #endif /* __ASM_CMPXCHG_H */
92