xref: /openbmc/linux/arch/ia64/include/uapi/asm/cmpxchg.h (revision b8265621)
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 #ifndef _ASM_IA64_CMPXCHG_H
3 #define _ASM_IA64_CMPXCHG_H
4 
5 /*
6  * Compare/Exchange, forked from asm/intrinsics.h
7  * which was:
8  *
9  *	Copyright (C) 2002-2003 Hewlett-Packard Co
10  *	David Mosberger-Tang <davidm@hpl.hp.com>
11  */
12 
13 #ifndef __ASSEMBLY__
14 
15 #include <linux/types.h>
16 /* include compiler specific intrinsics */
17 #include <asm/ia64regs.h>
18 #ifdef __INTEL_COMPILER
19 # include <asm/intel_intrin.h>
20 #else
21 # include <asm/gcc_intrin.h>
22 #endif
23 
24 /*
25  * This function doesn't exist, so you'll get a linker error if
26  * something tries to do an invalid xchg().
27  */
28 extern void ia64_xchg_called_with_bad_pointer(void);
29 
30 #define __xchg(x, ptr, size)						\
31 ({									\
32 	unsigned long __xchg_result;					\
33 									\
34 	switch (size) {							\
35 	case 1:								\
36 		__xchg_result = ia64_xchg1((__u8 *)ptr, x);		\
37 		break;							\
38 									\
39 	case 2:								\
40 		__xchg_result = ia64_xchg2((__u16 *)ptr, x);		\
41 		break;							\
42 									\
43 	case 4:								\
44 		__xchg_result = ia64_xchg4((__u32 *)ptr, x);		\
45 		break;							\
46 									\
47 	case 8:								\
48 		__xchg_result = ia64_xchg8((__u64 *)ptr, x);		\
49 		break;							\
50 	default:							\
51 		ia64_xchg_called_with_bad_pointer();			\
52 	}								\
53 	__xchg_result;							\
54 })
55 
56 #define xchg(ptr, x)							\
57 ((__typeof__(*(ptr))) __xchg((unsigned long) (x), (ptr), sizeof(*(ptr))))
58 
59 /*
60  * Atomic compare and exchange.  Compare OLD with MEM, if identical,
61  * store NEW in MEM.  Return the initial value in MEM.  Success is
62  * indicated by comparing RETURN with OLD.
63  */
64 
65 /*
66  * This function doesn't exist, so you'll get a linker error
67  * if something tries to do an invalid cmpxchg().
68  */
69 extern long ia64_cmpxchg_called_with_bad_pointer(void);
70 
71 #define ia64_cmpxchg(sem, ptr, old, new, size)				\
72 ({									\
73 	__u64 _o_, _r_;							\
74 									\
75 	switch (size) {							\
76 	case 1:								\
77 		_o_ = (__u8) (long) (old);				\
78 		break;							\
79 	case 2:								\
80 		_o_ = (__u16) (long) (old);				\
81 		break;							\
82 	case 4:								\
83 		_o_ = (__u32) (long) (old);				\
84 		break;							\
85 	case 8:								\
86 		_o_ = (__u64) (long) (old);				\
87 		break;							\
88 	default:							\
89 		break;							\
90 	}								\
91 	switch (size) {							\
92 	case 1:								\
93 		_r_ = ia64_cmpxchg1_##sem((__u8 *) ptr, new, _o_);	\
94 		break;							\
95 									\
96 	case 2:								\
97 		_r_ = ia64_cmpxchg2_##sem((__u16 *) ptr, new, _o_);	\
98 		break;							\
99 									\
100 	case 4:								\
101 		_r_ = ia64_cmpxchg4_##sem((__u32 *) ptr, new, _o_);	\
102 		break;							\
103 									\
104 	case 8:								\
105 		_r_ = ia64_cmpxchg8_##sem((__u64 *) ptr, new, _o_);	\
106 		break;							\
107 									\
108 	default:							\
109 		_r_ = ia64_cmpxchg_called_with_bad_pointer();		\
110 		break;							\
111 	}								\
112 	(__typeof__(old)) _r_;						\
113 })
114 
115 #define cmpxchg_acq(ptr, o, n)	\
116 	ia64_cmpxchg(acq, (ptr), (o), (n), sizeof(*(ptr)))
117 #define cmpxchg_rel(ptr, o, n)	\
118 	ia64_cmpxchg(rel, (ptr), (o), (n), sizeof(*(ptr)))
119 
120 /*
121  * Worse still - early processor implementations actually just ignored
122  * the acquire/release and did a full fence all the time.  Unfortunately
123  * this meant a lot of badly written code that used .acq when they really
124  * wanted .rel became legacy out in the wild - so when we made a cpu
125  * that strictly did the .acq or .rel ... all that code started breaking - so
126  * we had to back-pedal and keep the "legacy" behavior of a full fence :-(
127  */
128 
129 /* for compatibility with other platforms: */
130 #define cmpxchg(ptr, o, n)	cmpxchg_acq((ptr), (o), (n))
131 #define cmpxchg64(ptr, o, n)	cmpxchg_acq((ptr), (o), (n))
132 
133 #define cmpxchg_local		cmpxchg
134 #define cmpxchg64_local		cmpxchg64
135 
136 #ifdef CONFIG_IA64_DEBUG_CMPXCHG
137 # define CMPXCHG_BUGCHECK_DECL	int _cmpxchg_bugcheck_count = 128;
138 # define CMPXCHG_BUGCHECK(v)						\
139 do {									\
140 	if (_cmpxchg_bugcheck_count-- <= 0) {				\
141 		void *ip;						\
142 		extern int printk(const char *fmt, ...);		\
143 		ip = (void *) ia64_getreg(_IA64_REG_IP);		\
144 		printk("CMPXCHG_BUGCHECK: stuck at %p on word %p\n", ip, (v));\
145 		break;							\
146 	}								\
147 } while (0)
148 #else /* !CONFIG_IA64_DEBUG_CMPXCHG */
149 # define CMPXCHG_BUGCHECK_DECL
150 # define CMPXCHG_BUGCHECK(v)
151 #endif /* !CONFIG_IA64_DEBUG_CMPXCHG */
152 
153 #endif /* !__ASSEMBLY__ */
154 
155 #endif /* _ASM_IA64_CMPXCHG_H */
156