1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Low level function for atomic operations
5 * Copyright IBM Corp. 1999, 2016
8 #ifndef __ARCH_S390_ATOMIC_OPS__
9 #define __ARCH_S390_ATOMIC_OPS__
11 static inline int __atomic_read(const atomic_t *v)
17 : "=d" (c) : "R" (v->counter));
21 static inline void __atomic_set(atomic_t *v, int i)
25 : "=R" (v->counter) : "d" (i));
28 static inline s64 __atomic64_read(const atomic64_t *v)
34 : "=d" (c) : "RT" (v->counter));
38 static inline void __atomic64_set(atomic64_t *v, s64 i)
42 : "=RT" (v->counter) : "d" (i));
45 #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
47 #define __ATOMIC_OP(op_name, op_type, op_string, op_barrier) \
48 static inline op_type op_name(op_type val, op_type *ptr) \
53 op_string " %[old],%[val],%[ptr]\n" \
55 : [old] "=d" (old), [ptr] "+QS" (*ptr) \
56 : [val] "d" (val) : "cc", "memory"); \
60 #define __ATOMIC_OPS(op_name, op_type, op_string) \
61 __ATOMIC_OP(op_name, op_type, op_string, "\n") \
62 __ATOMIC_OP(op_name##_barrier, op_type, op_string, "bcr 14,0\n")
64 __ATOMIC_OPS(__atomic_add, int, "laa")
65 __ATOMIC_OPS(__atomic_and, int, "lan")
66 __ATOMIC_OPS(__atomic_or, int, "lao")
67 __ATOMIC_OPS(__atomic_xor, int, "lax")
69 __ATOMIC_OPS(__atomic64_add, long, "laag")
70 __ATOMIC_OPS(__atomic64_and, long, "lang")
71 __ATOMIC_OPS(__atomic64_or, long, "laog")
72 __ATOMIC_OPS(__atomic64_xor, long, "laxg")
77 #define __ATOMIC_CONST_OP(op_name, op_type, op_string, op_barrier) \
78 static __always_inline void op_name(op_type val, op_type *ptr) \
81 op_string " %[ptr],%[val]\n" \
83 : [ptr] "+QS" (*ptr) : [val] "i" (val) : "cc", "memory");\
86 #define __ATOMIC_CONST_OPS(op_name, op_type, op_string) \
87 __ATOMIC_CONST_OP(op_name, op_type, op_string, "\n") \
88 __ATOMIC_CONST_OP(op_name##_barrier, op_type, op_string, "bcr 14,0\n")
90 __ATOMIC_CONST_OPS(__atomic_add_const, int, "asi")
91 __ATOMIC_CONST_OPS(__atomic64_add_const, long, "agsi")
93 #undef __ATOMIC_CONST_OPS
94 #undef __ATOMIC_CONST_OP
96 #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
98 #define __ATOMIC_OP(op_name, op_string) \
99 static inline int op_name(int val, int *ptr) \
104 "0: lr %[new],%[old]\n" \
105 op_string " %[new],%[val]\n" \
106 " cs %[old],%[new],%[ptr]\n" \
108 : [old] "=d" (old), [new] "=&d" (new), [ptr] "+Q" (*ptr)\
109 : [val] "d" (val), "0" (*ptr) : "cc", "memory"); \
113 #define __ATOMIC_OPS(op_name, op_string) \
114 __ATOMIC_OP(op_name, op_string) \
115 __ATOMIC_OP(op_name##_barrier, op_string)
117 __ATOMIC_OPS(__atomic_add, "ar")
118 __ATOMIC_OPS(__atomic_and, "nr")
119 __ATOMIC_OPS(__atomic_or, "or")
120 __ATOMIC_OPS(__atomic_xor, "xr")
124 #define __ATOMIC64_OP(op_name, op_string) \
125 static inline long op_name(long val, long *ptr) \
130 "0: lgr %[new],%[old]\n" \
131 op_string " %[new],%[val]\n" \
132 " csg %[old],%[new],%[ptr]\n" \
134 : [old] "=d" (old), [new] "=&d" (new), [ptr] "+QS" (*ptr)\
135 : [val] "d" (val), "0" (*ptr) : "cc", "memory"); \
139 #define __ATOMIC64_OPS(op_name, op_string) \
140 __ATOMIC64_OP(op_name, op_string) \
141 __ATOMIC64_OP(op_name##_barrier, op_string)
143 __ATOMIC64_OPS(__atomic64_add, "agr")
144 __ATOMIC64_OPS(__atomic64_and, "ngr")
145 __ATOMIC64_OPS(__atomic64_or, "ogr")
146 __ATOMIC64_OPS(__atomic64_xor, "xgr")
148 #undef __ATOMIC64_OPS
150 #define __atomic_add_const(val, ptr) __atomic_add(val, ptr)
151 #define __atomic_add_const_barrier(val, ptr) __atomic_add(val, ptr)
152 #define __atomic64_add_const(val, ptr) __atomic64_add(val, ptr)
153 #define __atomic64_add_const_barrier(val, ptr) __atomic64_add(val, ptr)
155 #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
157 static inline int __atomic_cmpxchg(int *ptr, int old, int new)
160 " cs %[old],%[new],%[ptr]"
161 : [old] "+d" (old), [ptr] "+Q" (*ptr)
167 static inline bool __atomic_cmpxchg_bool(int *ptr, int old, int new)
169 int old_expected = old;
172 " cs %[old],%[new],%[ptr]"
173 : [old] "+d" (old), [ptr] "+Q" (*ptr)
176 return old == old_expected;
179 static inline long __atomic64_cmpxchg(long *ptr, long old, long new)
182 " csg %[old],%[new],%[ptr]"
183 : [old] "+d" (old), [ptr] "+QS" (*ptr)
189 static inline bool __atomic64_cmpxchg_bool(long *ptr, long old, long new)
191 long old_expected = old;
194 " csg %[old],%[new],%[ptr]"
195 : [old] "+d" (old), [ptr] "+QS" (*ptr)
198 return old == old_expected;
201 #endif /* __ARCH_S390_ATOMIC_OPS__ */