1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
212751058SHeiko Carstens /*
3126b30c3SMartin Schwidefsky * Copyright IBM Corp. 1999, 2016
412751058SHeiko Carstens * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
512751058SHeiko Carstens * Denis Joseph Barrow,
6126b30c3SMartin Schwidefsky * Arnd Bergmann,
712751058SHeiko Carstens */
812751058SHeiko Carstens
9a53c8fabSHeiko Carstens #ifndef __ARCH_S390_ATOMIC__
10a53c8fabSHeiko Carstens #define __ARCH_S390_ATOMIC__
11a53c8fabSHeiko Carstens
12c6557e7fSMartin Schwidefsky #include <linux/compiler.h>
13ea435467SMatthew Wilcox #include <linux/types.h>
14126b30c3SMartin Schwidefsky #include <asm/atomic_ops.h>
150ccc8b7aSHeiko Carstens #include <asm/barrier.h>
16a0616cdeSDavid Howells #include <asm/cmpxchg.h>
17c6557e7fSMartin Schwidefsky
arch_atomic_read(const atomic_t * v)18*01cac82aSIlya Leoshkevich static __always_inline int arch_atomic_read(const atomic_t *v)
19c6557e7fSMartin Schwidefsky {
20c8a91c28SHeiko Carstens return __atomic_read(v);
21c6557e7fSMartin Schwidefsky }
2200017423SHeiko Carstens #define arch_atomic_read arch_atomic_read
23c6557e7fSMartin Schwidefsky
arch_atomic_set(atomic_t * v,int i)24*01cac82aSIlya Leoshkevich static __always_inline void arch_atomic_set(atomic_t *v, int i)
25c6557e7fSMartin Schwidefsky {
26c8a91c28SHeiko Carstens __atomic_set(v, i);
27c6557e7fSMartin Schwidefsky }
2800017423SHeiko Carstens #define arch_atomic_set arch_atomic_set
29c6557e7fSMartin Schwidefsky
arch_atomic_add_return(int i,atomic_t * v)30*01cac82aSIlya Leoshkevich static __always_inline int arch_atomic_add_return(int i, atomic_t *v)
31c6557e7fSMartin Schwidefsky {
32126b30c3SMartin Schwidefsky return __atomic_add_barrier(i, &v->counter) + i;
33c6557e7fSMartin Schwidefsky }
3400017423SHeiko Carstens #define arch_atomic_add_return arch_atomic_add_return
3575287430SHeiko Carstens
arch_atomic_fetch_add(int i,atomic_t * v)36*01cac82aSIlya Leoshkevich static __always_inline int arch_atomic_fetch_add(int i, atomic_t *v)
3756fefbbcSPeter Zijlstra {
38126b30c3SMartin Schwidefsky return __atomic_add_barrier(i, &v->counter);
3956fefbbcSPeter Zijlstra }
4000017423SHeiko Carstens #define arch_atomic_fetch_add arch_atomic_fetch_add
4156fefbbcSPeter Zijlstra
arch_atomic_add(int i,atomic_t * v)42*01cac82aSIlya Leoshkevich static __always_inline void arch_atomic_add(int i, atomic_t *v)
435692e4d1SHeiko Carstens {
44126b30c3SMartin Schwidefsky __atomic_add(i, &v->counter);
455692e4d1SHeiko Carstens }
4600017423SHeiko Carstens #define arch_atomic_add arch_atomic_add
475692e4d1SHeiko Carstens
4800017423SHeiko Carstens #define arch_atomic_sub(_i, _v) arch_atomic_add(-(int)(_i), _v)
4900017423SHeiko Carstens #define arch_atomic_sub_return(_i, _v) arch_atomic_add_return(-(int)(_i), _v)
5000017423SHeiko Carstens #define arch_atomic_fetch_sub(_i, _v) arch_atomic_fetch_add(-(int)(_i), _v)
51c6557e7fSMartin Schwidefsky
52126b30c3SMartin Schwidefsky #define ATOMIC_OPS(op) \
53*01cac82aSIlya Leoshkevich static __always_inline void arch_atomic_##op(int i, atomic_t *v) \
54ae8c35c8SPeter Zijlstra { \
55126b30c3SMartin Schwidefsky __atomic_##op(i, &v->counter); \
5656fefbbcSPeter Zijlstra } \
57*01cac82aSIlya Leoshkevich static __always_inline int arch_atomic_fetch_##op(int i, atomic_t *v) \
5856fefbbcSPeter Zijlstra { \
59126b30c3SMartin Schwidefsky return __atomic_##op##_barrier(i, &v->counter); \
60c6557e7fSMartin Schwidefsky }
61c6557e7fSMartin Schwidefsky
62126b30c3SMartin Schwidefsky ATOMIC_OPS(and)
ATOMIC_OPS(or)63126b30c3SMartin Schwidefsky ATOMIC_OPS(or)
64126b30c3SMartin Schwidefsky ATOMIC_OPS(xor)
6556fefbbcSPeter Zijlstra
6656fefbbcSPeter Zijlstra #undef ATOMIC_OPS
67ae8c35c8SPeter Zijlstra
6800017423SHeiko Carstens #define arch_atomic_and arch_atomic_and
6900017423SHeiko Carstens #define arch_atomic_or arch_atomic_or
7000017423SHeiko Carstens #define arch_atomic_xor arch_atomic_xor
7100017423SHeiko Carstens #define arch_atomic_fetch_and arch_atomic_fetch_and
7200017423SHeiko Carstens #define arch_atomic_fetch_or arch_atomic_fetch_or
7300017423SHeiko Carstens #define arch_atomic_fetch_xor arch_atomic_fetch_xor
74c6557e7fSMartin Schwidefsky
7500017423SHeiko Carstens #define arch_atomic_xchg(v, new) (arch_xchg(&((v)->counter), new))
7600017423SHeiko Carstens
77*01cac82aSIlya Leoshkevich static __always_inline int arch_atomic_cmpxchg(atomic_t *v, int old, int new)
78c6557e7fSMartin Schwidefsky {
79126b30c3SMartin Schwidefsky return __atomic_cmpxchg(&v->counter, old, new);
80c6557e7fSMartin Schwidefsky }
8100017423SHeiko Carstens #define arch_atomic_cmpxchg arch_atomic_cmpxchg
82c6557e7fSMartin Schwidefsky
83c6557e7fSMartin Schwidefsky #define ATOMIC64_INIT(i) { (i) }
84c6557e7fSMartin Schwidefsky
arch_atomic64_read(const atomic64_t * v)85*01cac82aSIlya Leoshkevich static __always_inline s64 arch_atomic64_read(const atomic64_t *v)
86c6557e7fSMartin Schwidefsky {
87c8a91c28SHeiko Carstens return __atomic64_read(v);
88c6557e7fSMartin Schwidefsky }
8900017423SHeiko Carstens #define arch_atomic64_read arch_atomic64_read
90c6557e7fSMartin Schwidefsky
arch_atomic64_set(atomic64_t * v,s64 i)91*01cac82aSIlya Leoshkevich static __always_inline void arch_atomic64_set(atomic64_t *v, s64 i)
92c6557e7fSMartin Schwidefsky {
93c8a91c28SHeiko Carstens __atomic64_set(v, i);
94c6557e7fSMartin Schwidefsky }
9500017423SHeiko Carstens #define arch_atomic64_set arch_atomic64_set
96c6557e7fSMartin Schwidefsky
arch_atomic64_add_return(s64 i,atomic64_t * v)97*01cac82aSIlya Leoshkevich static __always_inline s64 arch_atomic64_add_return(s64 i, atomic64_t *v)
98c6557e7fSMartin Schwidefsky {
990ca94800SMark Rutland return __atomic64_add_barrier(i, (long *)&v->counter) + i;
1000ccc8b7aSHeiko Carstens }
10100017423SHeiko Carstens #define arch_atomic64_add_return arch_atomic64_add_return
1020ccc8b7aSHeiko Carstens
arch_atomic64_fetch_add(s64 i,atomic64_t * v)103*01cac82aSIlya Leoshkevich static __always_inline s64 arch_atomic64_fetch_add(s64 i, atomic64_t *v)
10456fefbbcSPeter Zijlstra {
1050ca94800SMark Rutland return __atomic64_add_barrier(i, (long *)&v->counter);
10656fefbbcSPeter Zijlstra }
10700017423SHeiko Carstens #define arch_atomic64_fetch_add arch_atomic64_fetch_add
10856fefbbcSPeter Zijlstra
arch_atomic64_add(s64 i,atomic64_t * v)109*01cac82aSIlya Leoshkevich static __always_inline void arch_atomic64_add(s64 i, atomic64_t *v)
1100ccc8b7aSHeiko Carstens {
1110ca94800SMark Rutland __atomic64_add(i, (long *)&v->counter);
112c6557e7fSMartin Schwidefsky }
11300017423SHeiko Carstens #define arch_atomic64_add arch_atomic64_add
114c6557e7fSMartin Schwidefsky
11500017423SHeiko Carstens #define arch_atomic64_xchg(v, new) (arch_xchg(&((v)->counter), new))
116c6557e7fSMartin Schwidefsky
arch_atomic64_cmpxchg(atomic64_t * v,s64 old,s64 new)117*01cac82aSIlya Leoshkevich static __always_inline s64 arch_atomic64_cmpxchg(atomic64_t *v, s64 old, s64 new)
118c6557e7fSMartin Schwidefsky {
1190ca94800SMark Rutland return __atomic64_cmpxchg((long *)&v->counter, old, new);
120c6557e7fSMartin Schwidefsky }
12100017423SHeiko Carstens #define arch_atomic64_cmpxchg arch_atomic64_cmpxchg
122c6557e7fSMartin Schwidefsky
123126b30c3SMartin Schwidefsky #define ATOMIC64_OPS(op) \
124*01cac82aSIlya Leoshkevich static __always_inline void arch_atomic64_##op(s64 i, atomic64_t *v) \
125ae8c35c8SPeter Zijlstra { \
1260ca94800SMark Rutland __atomic64_##op(i, (long *)&v->counter); \
12756fefbbcSPeter Zijlstra } \
128*01cac82aSIlya Leoshkevich static __always_inline long arch_atomic64_fetch_##op(s64 i, atomic64_t *v) \
12956fefbbcSPeter Zijlstra { \
1300ca94800SMark Rutland return __atomic64_##op##_barrier(i, (long *)&v->counter); \
131ae8c35c8SPeter Zijlstra }
132ae8c35c8SPeter Zijlstra
133126b30c3SMartin Schwidefsky ATOMIC64_OPS(and)
134126b30c3SMartin Schwidefsky ATOMIC64_OPS(or)
135126b30c3SMartin Schwidefsky ATOMIC64_OPS(xor)
136ae8c35c8SPeter Zijlstra
13756fefbbcSPeter Zijlstra #undef ATOMIC64_OPS
13812751058SHeiko Carstens
13900017423SHeiko Carstens #define arch_atomic64_and arch_atomic64_and
14000017423SHeiko Carstens #define arch_atomic64_or arch_atomic64_or
14100017423SHeiko Carstens #define arch_atomic64_xor arch_atomic64_xor
14200017423SHeiko Carstens #define arch_atomic64_fetch_and arch_atomic64_fetch_and
14300017423SHeiko Carstens #define arch_atomic64_fetch_or arch_atomic64_fetch_or
14400017423SHeiko Carstens #define arch_atomic64_fetch_xor arch_atomic64_fetch_xor
14500017423SHeiko Carstens
14600017423SHeiko Carstens #define arch_atomic64_sub_return(_i, _v) arch_atomic64_add_return(-(s64)(_i), _v)
14700017423SHeiko Carstens #define arch_atomic64_fetch_sub(_i, _v) arch_atomic64_fetch_add(-(s64)(_i), _v)
14800017423SHeiko Carstens #define arch_atomic64_sub(_i, _v) arch_atomic64_add(-(s64)(_i), _v)
14900017423SHeiko Carstens
150c6557e7fSMartin Schwidefsky #endif /* __ARCH_S390_ATOMIC__ */
151