blob: 11c5952e1afa8a648eb3891615c982217aafed4f [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright IBM Corp. 1999, 2016
4 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
5 * Denis Joseph Barrow,
6 * Arnd Bergmann,
7 */
8
9#ifndef __ARCH_S390_ATOMIC__
10#define __ARCH_S390_ATOMIC__
11
12#include <linux/compiler.h>
13#include <linux/types.h>
14#include <asm/atomic_ops.h>
15#include <asm/barrier.h>
16#include <asm/cmpxchg.h>
17
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000018static inline int atomic_read(const atomic_t *v)
19{
20 int c;
21
22 asm volatile(
23 " l %0,%1\n"
24 : "=d" (c) : "Q" (v->counter));
25 return c;
26}
27
28static inline void atomic_set(atomic_t *v, int i)
29{
30 asm volatile(
31 " st %1,%0\n"
32 : "=Q" (v->counter) : "d" (i));
33}
34
35static inline int atomic_add_return(int i, atomic_t *v)
36{
37 return __atomic_add_barrier(i, &v->counter) + i;
38}
39
40static inline int atomic_fetch_add(int i, atomic_t *v)
41{
42 return __atomic_add_barrier(i, &v->counter);
43}
44
45static inline void atomic_add(int i, atomic_t *v)
46{
47#ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
Olivier Deprez157378f2022-04-04 15:47:50 +020048 /*
49 * Order of conditions is important to circumvent gcc 10 bug:
50 * https://gcc.gnu.org/pipermail/gcc-patches/2020-July/549318.html
51 */
52 if ((i > -129) && (i < 128) && __builtin_constant_p(i)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000053 __atomic_add_const(i, &v->counter);
54 return;
55 }
56#endif
57 __atomic_add(i, &v->counter);
58}
59
60#define atomic_sub(_i, _v) atomic_add(-(int)(_i), _v)
61#define atomic_sub_return(_i, _v) atomic_add_return(-(int)(_i), _v)
62#define atomic_fetch_sub(_i, _v) atomic_fetch_add(-(int)(_i), _v)
63
64#define ATOMIC_OPS(op) \
65static inline void atomic_##op(int i, atomic_t *v) \
66{ \
67 __atomic_##op(i, &v->counter); \
68} \
69static inline int atomic_fetch_##op(int i, atomic_t *v) \
70{ \
71 return __atomic_##op##_barrier(i, &v->counter); \
72}
73
74ATOMIC_OPS(and)
75ATOMIC_OPS(or)
76ATOMIC_OPS(xor)
77
78#undef ATOMIC_OPS
79
80#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
81
82static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
83{
84 return __atomic_cmpxchg(&v->counter, old, new);
85}
86
87#define ATOMIC64_INIT(i) { (i) }
88
David Brazdil0f672f62019-12-10 10:32:29 +000089static inline s64 atomic64_read(const atomic64_t *v)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000090{
David Brazdil0f672f62019-12-10 10:32:29 +000091 s64 c;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000092
93 asm volatile(
94 " lg %0,%1\n"
95 : "=d" (c) : "Q" (v->counter));
96 return c;
97}
98
David Brazdil0f672f62019-12-10 10:32:29 +000099static inline void atomic64_set(atomic64_t *v, s64 i)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000100{
101 asm volatile(
102 " stg %1,%0\n"
103 : "=Q" (v->counter) : "d" (i));
104}
105
David Brazdil0f672f62019-12-10 10:32:29 +0000106static inline s64 atomic64_add_return(s64 i, atomic64_t *v)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000107{
David Brazdil0f672f62019-12-10 10:32:29 +0000108 return __atomic64_add_barrier(i, (long *)&v->counter) + i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000109}
110
David Brazdil0f672f62019-12-10 10:32:29 +0000111static inline s64 atomic64_fetch_add(s64 i, atomic64_t *v)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000112{
David Brazdil0f672f62019-12-10 10:32:29 +0000113 return __atomic64_add_barrier(i, (long *)&v->counter);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000114}
115
David Brazdil0f672f62019-12-10 10:32:29 +0000116static inline void atomic64_add(s64 i, atomic64_t *v)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000117{
118#ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
Olivier Deprez157378f2022-04-04 15:47:50 +0200119 /*
120 * Order of conditions is important to circumvent gcc 10 bug:
121 * https://gcc.gnu.org/pipermail/gcc-patches/2020-July/549318.html
122 */
123 if ((i > -129) && (i < 128) && __builtin_constant_p(i)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000124 __atomic64_add_const(i, (long *)&v->counter);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000125 return;
126 }
127#endif
David Brazdil0f672f62019-12-10 10:32:29 +0000128 __atomic64_add(i, (long *)&v->counter);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000129}
130
131#define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
132
David Brazdil0f672f62019-12-10 10:32:29 +0000133static inline s64 atomic64_cmpxchg(atomic64_t *v, s64 old, s64 new)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000134{
David Brazdil0f672f62019-12-10 10:32:29 +0000135 return __atomic64_cmpxchg((long *)&v->counter, old, new);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000136}
137
138#define ATOMIC64_OPS(op) \
David Brazdil0f672f62019-12-10 10:32:29 +0000139static inline void atomic64_##op(s64 i, atomic64_t *v) \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000140{ \
David Brazdil0f672f62019-12-10 10:32:29 +0000141 __atomic64_##op(i, (long *)&v->counter); \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000142} \
David Brazdil0f672f62019-12-10 10:32:29 +0000143static inline long atomic64_fetch_##op(s64 i, atomic64_t *v) \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000144{ \
David Brazdil0f672f62019-12-10 10:32:29 +0000145 return __atomic64_##op##_barrier(i, (long *)&v->counter); \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000146}
147
148ATOMIC64_OPS(and)
149ATOMIC64_OPS(or)
150ATOMIC64_OPS(xor)
151
152#undef ATOMIC64_OPS
153
David Brazdil0f672f62019-12-10 10:32:29 +0000154#define atomic64_sub_return(_i, _v) atomic64_add_return(-(s64)(_i), _v)
155#define atomic64_fetch_sub(_i, _v) atomic64_fetch_add(-(s64)(_i), _v)
156#define atomic64_sub(_i, _v) atomic64_add(-(s64)(_i), _v)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000157
158#endif /* __ARCH_S390_ATOMIC__ */