]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - rpmsg/rpmsg.git/blob - arch/x86/include/asm/atomic64_32.h
Merge tag 'xfs-fixes-for-4.19-rc7' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[rpmsg/rpmsg.git] / arch / x86 / include / asm / atomic64_32.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_ATOMIC64_32_H
3 #define _ASM_X86_ATOMIC64_32_H
5 #include <linux/compiler.h>
6 #include <linux/types.h>
7 //#include <asm/cmpxchg.h>
9 /* An 64bit atomic type */
11 typedef struct {
12         u64 __aligned(8) counter;
13 } atomic64_t;
15 #define ATOMIC64_INIT(val)      { (val) }
17 #define __ATOMIC64_DECL(sym) void atomic64_##sym(atomic64_t *, ...)
18 #ifndef ATOMIC64_EXPORT
19 #define ATOMIC64_DECL_ONE __ATOMIC64_DECL
20 #else
21 #define ATOMIC64_DECL_ONE(sym) __ATOMIC64_DECL(sym); \
22         ATOMIC64_EXPORT(atomic64_##sym)
23 #endif
25 #ifdef CONFIG_X86_CMPXCHG64
26 #define __alternative_atomic64(f, g, out, in...) \
27         asm volatile("call %P[func]" \
28                      : out : [func] "i" (atomic64_##g##_cx8), ## in)
30 #define ATOMIC64_DECL(sym) ATOMIC64_DECL_ONE(sym##_cx8)
31 #else
32 #define __alternative_atomic64(f, g, out, in...) \
33         alternative_call(atomic64_##f##_386, atomic64_##g##_cx8, \
34                          X86_FEATURE_CX8, ASM_OUTPUT2(out), ## in)
36 #define ATOMIC64_DECL(sym) ATOMIC64_DECL_ONE(sym##_cx8); \
37         ATOMIC64_DECL_ONE(sym##_386)
39 ATOMIC64_DECL_ONE(add_386);
40 ATOMIC64_DECL_ONE(sub_386);
41 ATOMIC64_DECL_ONE(inc_386);
42 ATOMIC64_DECL_ONE(dec_386);
43 #endif
45 #define alternative_atomic64(f, out, in...) \
46         __alternative_atomic64(f, f, ASM_OUTPUT2(out), ## in)
48 ATOMIC64_DECL(read);
49 ATOMIC64_DECL(set);
50 ATOMIC64_DECL(xchg);
51 ATOMIC64_DECL(add_return);
52 ATOMIC64_DECL(sub_return);
53 ATOMIC64_DECL(inc_return);
54 ATOMIC64_DECL(dec_return);
55 ATOMIC64_DECL(dec_if_positive);
56 ATOMIC64_DECL(inc_not_zero);
57 ATOMIC64_DECL(add_unless);
59 #undef ATOMIC64_DECL
60 #undef ATOMIC64_DECL_ONE
61 #undef __ATOMIC64_DECL
62 #undef ATOMIC64_EXPORT
64 /**
65  * arch_atomic64_cmpxchg - cmpxchg atomic64 variable
66  * @v: pointer to type atomic64_t
67  * @o: expected value
68  * @n: new value
69  *
70  * Atomically sets @v to @n if it was equal to @o and returns
71  * the old value.
72  */
74 static inline long long arch_atomic64_cmpxchg(atomic64_t *v, long long o,
75                                               long long n)
76 {
77         return arch_cmpxchg64(&v->counter, o, n);
78 }
80 /**
81  * arch_atomic64_xchg - xchg atomic64 variable
82  * @v: pointer to type atomic64_t
83  * @n: value to assign
84  *
85  * Atomically xchgs the value of @v to @n and returns
86  * the old value.
87  */
88 static inline long long arch_atomic64_xchg(atomic64_t *v, long long n)
89 {
90         long long o;
91         unsigned high = (unsigned)(n >> 32);
92         unsigned low = (unsigned)n;
93         alternative_atomic64(xchg, "=&A" (o),
94                              "S" (v), "b" (low), "c" (high)
95                              : "memory");
96         return o;
97 }
99 /**
100  * arch_atomic64_set - set atomic64 variable
101  * @v: pointer to type atomic64_t
102  * @i: value to assign
103  *
104  * Atomically sets the value of @v to @n.
105  */
106 static inline void arch_atomic64_set(atomic64_t *v, long long i)
108         unsigned high = (unsigned)(i >> 32);
109         unsigned low = (unsigned)i;
110         alternative_atomic64(set, /* no output */,
111                              "S" (v), "b" (low), "c" (high)
112                              : "eax", "edx", "memory");
115 /**
116  * arch_atomic64_read - read atomic64 variable
117  * @v: pointer to type atomic64_t
118  *
119  * Atomically reads the value of @v and returns it.
120  */
121 static inline long long arch_atomic64_read(const atomic64_t *v)
123         long long r;
124         alternative_atomic64(read, "=&A" (r), "c" (v) : "memory");
125         return r;
128 /**
129  * arch_atomic64_add_return - add and return
130  * @i: integer value to add
131  * @v: pointer to type atomic64_t
132  *
133  * Atomically adds @i to @v and returns @i + *@v
134  */
135 static inline long long arch_atomic64_add_return(long long i, atomic64_t *v)
137         alternative_atomic64(add_return,
138                              ASM_OUTPUT2("+A" (i), "+c" (v)),
139                              ASM_NO_INPUT_CLOBBER("memory"));
140         return i;
143 /*
144  * Other variants with different arithmetic operators:
145  */
146 static inline long long arch_atomic64_sub_return(long long i, atomic64_t *v)
148         alternative_atomic64(sub_return,
149                              ASM_OUTPUT2("+A" (i), "+c" (v)),
150                              ASM_NO_INPUT_CLOBBER("memory"));
151         return i;
154 static inline long long arch_atomic64_inc_return(atomic64_t *v)
156         long long a;
157         alternative_atomic64(inc_return, "=&A" (a),
158                              "S" (v) : "memory", "ecx");
159         return a;
161 #define arch_atomic64_inc_return arch_atomic64_inc_return
163 static inline long long arch_atomic64_dec_return(atomic64_t *v)
165         long long a;
166         alternative_atomic64(dec_return, "=&A" (a),
167                              "S" (v) : "memory", "ecx");
168         return a;
170 #define arch_atomic64_dec_return arch_atomic64_dec_return
172 /**
173  * arch_atomic64_add - add integer to atomic64 variable
174  * @i: integer value to add
175  * @v: pointer to type atomic64_t
176  *
177  * Atomically adds @i to @v.
178  */
179 static inline long long arch_atomic64_add(long long i, atomic64_t *v)
181         __alternative_atomic64(add, add_return,
182                                ASM_OUTPUT2("+A" (i), "+c" (v)),
183                                ASM_NO_INPUT_CLOBBER("memory"));
184         return i;
187 /**
188  * arch_atomic64_sub - subtract the atomic64 variable
189  * @i: integer value to subtract
190  * @v: pointer to type atomic64_t
191  *
192  * Atomically subtracts @i from @v.
193  */
194 static inline long long arch_atomic64_sub(long long i, atomic64_t *v)
196         __alternative_atomic64(sub, sub_return,
197                                ASM_OUTPUT2("+A" (i), "+c" (v)),
198                                ASM_NO_INPUT_CLOBBER("memory"));
199         return i;
202 /**
203  * arch_atomic64_inc - increment atomic64 variable
204  * @v: pointer to type atomic64_t
205  *
206  * Atomically increments @v by 1.
207  */
208 static inline void arch_atomic64_inc(atomic64_t *v)
210         __alternative_atomic64(inc, inc_return, /* no output */,
211                                "S" (v) : "memory", "eax", "ecx", "edx");
213 #define arch_atomic64_inc arch_atomic64_inc
215 /**
216  * arch_atomic64_dec - decrement atomic64 variable
217  * @v: pointer to type atomic64_t
218  *
219  * Atomically decrements @v by 1.
220  */
221 static inline void arch_atomic64_dec(atomic64_t *v)
223         __alternative_atomic64(dec, dec_return, /* no output */,
224                                "S" (v) : "memory", "eax", "ecx", "edx");
226 #define arch_atomic64_dec arch_atomic64_dec
228 /**
229  * arch_atomic64_add_unless - add unless the number is a given value
230  * @v: pointer of type atomic64_t
231  * @a: the amount to add to v...
232  * @u: ...unless v is equal to u.
233  *
234  * Atomically adds @a to @v, so long as it was not @u.
235  * Returns non-zero if the add was done, zero otherwise.
236  */
237 static inline int arch_atomic64_add_unless(atomic64_t *v, long long a,
238                                            long long u)
240         unsigned low = (unsigned)u;
241         unsigned high = (unsigned)(u >> 32);
242         alternative_atomic64(add_unless,
243                              ASM_OUTPUT2("+A" (a), "+c" (low), "+D" (high)),
244                              "S" (v) : "memory");
245         return (int)a;
248 static inline int arch_atomic64_inc_not_zero(atomic64_t *v)
250         int r;
251         alternative_atomic64(inc_not_zero, "=&a" (r),
252                              "S" (v) : "ecx", "edx", "memory");
253         return r;
255 #define arch_atomic64_inc_not_zero arch_atomic64_inc_not_zero
257 static inline long long arch_atomic64_dec_if_positive(atomic64_t *v)
259         long long r;
260         alternative_atomic64(dec_if_positive, "=&A" (r),
261                              "S" (v) : "ecx", "memory");
262         return r;
264 #define arch_atomic64_dec_if_positive arch_atomic64_dec_if_positive
266 #undef alternative_atomic64
267 #undef __alternative_atomic64
269 static inline void arch_atomic64_and(long long i, atomic64_t *v)
271         long long old, c = 0;
273         while ((old = arch_atomic64_cmpxchg(v, c, c & i)) != c)
274                 c = old;
277 static inline long long arch_atomic64_fetch_and(long long i, atomic64_t *v)
279         long long old, c = 0;
281         while ((old = arch_atomic64_cmpxchg(v, c, c & i)) != c)
282                 c = old;
284         return old;
287 static inline void arch_atomic64_or(long long i, atomic64_t *v)
289         long long old, c = 0;
291         while ((old = arch_atomic64_cmpxchg(v, c, c | i)) != c)
292                 c = old;
295 static inline long long arch_atomic64_fetch_or(long long i, atomic64_t *v)
297         long long old, c = 0;
299         while ((old = arch_atomic64_cmpxchg(v, c, c | i)) != c)
300                 c = old;
302         return old;
305 static inline void arch_atomic64_xor(long long i, atomic64_t *v)
307         long long old, c = 0;
309         while ((old = arch_atomic64_cmpxchg(v, c, c ^ i)) != c)
310                 c = old;
313 static inline long long arch_atomic64_fetch_xor(long long i, atomic64_t *v)
315         long long old, c = 0;
317         while ((old = arch_atomic64_cmpxchg(v, c, c ^ i)) != c)
318                 c = old;
320         return old;
323 static inline long long arch_atomic64_fetch_add(long long i, atomic64_t *v)
325         long long old, c = 0;
327         while ((old = arch_atomic64_cmpxchg(v, c, c + i)) != c)
328                 c = old;
330         return old;
333 #define arch_atomic64_fetch_sub(i, v)   arch_atomic64_fetch_add(-(i), (v))
335 #endif /* _ASM_X86_ATOMIC64_32_H */