Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | #ifndef _ASM_IA64_ATOMIC_H |
| 3 | #define _ASM_IA64_ATOMIC_H |
| 4 | |
| 5 | /* |
| 6 | * Atomic operations that C can't guarantee us. Useful for |
| 7 | * resource counting etc.. |
| 8 | * |
| 9 | * NOTE: don't mess with the types below! The "unsigned long" and |
| 10 | * "int" types were carefully placed so as to ensure proper operation |
| 11 | * of the macros. |
| 12 | * |
| 13 | * Copyright (C) 1998, 1999, 2002-2003 Hewlett-Packard Co |
| 14 | * David Mosberger-Tang <davidm@hpl.hp.com> |
| 15 | */ |
| 16 | #include <linux/types.h> |
| 17 | |
| 18 | #include <asm/intrinsics.h> |
Peter Zijlstra | 0cd64ef | 2014-03-13 19:00:36 +0100 | [diff] [blame] | 19 | #include <asm/barrier.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Tony Luck | a119365 | 2012-07-26 10:55:26 -0700 | [diff] [blame] | 22 | #define ATOMIC_INIT(i) { (i) } |
| 23 | #define ATOMIC64_INIT(i) { (i) } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Peter Zijlstra | 62e8a32 | 2015-09-18 11:13:10 +0200 | [diff] [blame] | 25 | #define atomic_read(v) READ_ONCE((v)->counter) |
| 26 | #define atomic64_read(v) READ_ONCE((v)->counter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Peter Zijlstra | 62e8a32 | 2015-09-18 11:13:10 +0200 | [diff] [blame] | 28 | #define atomic_set(v,i) WRITE_ONCE(((v)->counter), (i)) |
| 29 | #define atomic64_set(v,i) WRITE_ONCE(((v)->counter), (i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Peter Zijlstra | 08be2da | 2014-03-23 18:20:30 +0100 | [diff] [blame] | 31 | #define ATOMIC_OP(op, c_op) \ |
| 32 | static __inline__ int \ |
| 33 | ia64_atomic_##op (int i, atomic_t *v) \ |
| 34 | { \ |
| 35 | __s32 old, new; \ |
| 36 | CMPXCHG_BUGCHECK_DECL \ |
| 37 | \ |
| 38 | do { \ |
| 39 | CMPXCHG_BUGCHECK(v); \ |
| 40 | old = atomic_read(v); \ |
| 41 | new = old c_op i; \ |
| 42 | } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic_t)) != old); \ |
| 43 | return new; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Peter Zijlstra | cc102507 | 2016-04-18 01:16:07 +0200 | [diff] [blame] | 46 | #define ATOMIC_FETCH_OP(op, c_op) \ |
| 47 | static __inline__ int \ |
| 48 | ia64_atomic_fetch_##op (int i, atomic_t *v) \ |
| 49 | { \ |
| 50 | __s32 old, new; \ |
| 51 | CMPXCHG_BUGCHECK_DECL \ |
| 52 | \ |
| 53 | do { \ |
| 54 | CMPXCHG_BUGCHECK(v); \ |
| 55 | old = atomic_read(v); \ |
| 56 | new = old c_op i; \ |
| 57 | } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic_t)) != old); \ |
| 58 | return old; \ |
| 59 | } |
| 60 | |
| 61 | #define ATOMIC_OPS(op, c_op) \ |
| 62 | ATOMIC_OP(op, c_op) \ |
| 63 | ATOMIC_FETCH_OP(op, c_op) |
| 64 | |
| 65 | ATOMIC_OPS(add, +) |
| 66 | ATOMIC_OPS(sub, -) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Matthew Wilcox | 4b664e7 | 2018-01-18 13:52:17 -0800 | [diff] [blame] | 68 | #ifdef __OPTIMIZE__ |
Matthew Wilcox | 2879b65 | 2018-02-19 09:41:26 -0800 | [diff] [blame] | 69 | #define __ia64_atomic_const(i) \ |
| 70 | static const int __ia64_atomic_p = __builtin_constant_p(i) ? \ |
Matthew Wilcox | 4b664e7 | 2018-01-18 13:52:17 -0800 | [diff] [blame] | 71 | ((i) == 1 || (i) == 4 || (i) == 8 || (i) == 16 || \ |
Matthew Wilcox | 2879b65 | 2018-02-19 09:41:26 -0800 | [diff] [blame] | 72 | (i) == -1 || (i) == -4 || (i) == -8 || (i) == -16) : 0;\ |
| 73 | __ia64_atomic_p |
Matthew Wilcox | 4b664e7 | 2018-01-18 13:52:17 -0800 | [diff] [blame] | 74 | #else |
Matthew Wilcox | 2879b65 | 2018-02-19 09:41:26 -0800 | [diff] [blame] | 75 | #define __ia64_atomic_const(i) 0 |
Matthew Wilcox | 4b664e7 | 2018-01-18 13:52:17 -0800 | [diff] [blame] | 76 | #endif |
Peter Zijlstra | 08be2da | 2014-03-23 18:20:30 +0100 | [diff] [blame] | 77 | |
Matthew Wilcox | 2879b65 | 2018-02-19 09:41:26 -0800 | [diff] [blame] | 78 | #define atomic_add_return(i,v) \ |
| 79 | ({ \ |
| 80 | int __ia64_aar_i = (i); \ |
| 81 | __ia64_atomic_const(i) \ |
| 82 | ? ia64_fetch_and_add(__ia64_aar_i, &(v)->counter) \ |
| 83 | : ia64_atomic_add(__ia64_aar_i, v); \ |
| 84 | }) |
| 85 | |
| 86 | #define atomic_sub_return(i,v) \ |
| 87 | ({ \ |
| 88 | int __ia64_asr_i = (i); \ |
| 89 | __ia64_atomic_const(i) \ |
| 90 | ? ia64_fetch_and_add(-__ia64_asr_i, &(v)->counter) \ |
| 91 | : ia64_atomic_sub(__ia64_asr_i, v); \ |
| 92 | }) |
| 93 | |
Peter Zijlstra | cc102507 | 2016-04-18 01:16:07 +0200 | [diff] [blame] | 94 | #define atomic_fetch_add(i,v) \ |
| 95 | ({ \ |
| 96 | int __ia64_aar_i = (i); \ |
Matthew Wilcox | 2879b65 | 2018-02-19 09:41:26 -0800 | [diff] [blame] | 97 | __ia64_atomic_const(i) \ |
Peter Zijlstra | cc102507 | 2016-04-18 01:16:07 +0200 | [diff] [blame] | 98 | ? ia64_fetchadd(__ia64_aar_i, &(v)->counter, acq) \ |
| 99 | : ia64_atomic_fetch_add(__ia64_aar_i, v); \ |
| 100 | }) |
Peter Zijlstra | 70ed473 | 2014-04-23 20:00:01 +0200 | [diff] [blame] | 101 | |
Peter Zijlstra | cc102507 | 2016-04-18 01:16:07 +0200 | [diff] [blame] | 102 | #define atomic_fetch_sub(i,v) \ |
| 103 | ({ \ |
| 104 | int __ia64_asr_i = (i); \ |
Matthew Wilcox | 2879b65 | 2018-02-19 09:41:26 -0800 | [diff] [blame] | 105 | __ia64_atomic_const(i) \ |
Peter Zijlstra | cc102507 | 2016-04-18 01:16:07 +0200 | [diff] [blame] | 106 | ? ia64_fetchadd(-__ia64_asr_i, &(v)->counter, acq) \ |
| 107 | : ia64_atomic_fetch_sub(__ia64_asr_i, v); \ |
| 108 | }) |
Peter Zijlstra | 70ed473 | 2014-04-23 20:00:01 +0200 | [diff] [blame] | 109 | |
Peter Zijlstra | cc102507 | 2016-04-18 01:16:07 +0200 | [diff] [blame] | 110 | ATOMIC_FETCH_OP(and, &) |
| 111 | ATOMIC_FETCH_OP(or, |) |
| 112 | ATOMIC_FETCH_OP(xor, ^) |
| 113 | |
| 114 | #define atomic_and(i,v) (void)ia64_atomic_fetch_and(i,v) |
| 115 | #define atomic_or(i,v) (void)ia64_atomic_fetch_or(i,v) |
| 116 | #define atomic_xor(i,v) (void)ia64_atomic_fetch_xor(i,v) |
| 117 | |
| 118 | #define atomic_fetch_and(i,v) ia64_atomic_fetch_and(i,v) |
| 119 | #define atomic_fetch_or(i,v) ia64_atomic_fetch_or(i,v) |
| 120 | #define atomic_fetch_xor(i,v) ia64_atomic_fetch_xor(i,v) |
| 121 | |
| 122 | #undef ATOMIC_OPS |
| 123 | #undef ATOMIC_FETCH_OP |
Peter Zijlstra | 70ed473 | 2014-04-23 20:00:01 +0200 | [diff] [blame] | 124 | #undef ATOMIC_OP |
| 125 | |
Peter Zijlstra | 08be2da | 2014-03-23 18:20:30 +0100 | [diff] [blame] | 126 | #define ATOMIC64_OP(op, c_op) \ |
| 127 | static __inline__ long \ |
| 128 | ia64_atomic64_##op (__s64 i, atomic64_t *v) \ |
| 129 | { \ |
| 130 | __s64 old, new; \ |
| 131 | CMPXCHG_BUGCHECK_DECL \ |
| 132 | \ |
| 133 | do { \ |
| 134 | CMPXCHG_BUGCHECK(v); \ |
| 135 | old = atomic64_read(v); \ |
| 136 | new = old c_op i; \ |
| 137 | } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic64_t)) != old); \ |
| 138 | return new; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Peter Zijlstra | cc102507 | 2016-04-18 01:16:07 +0200 | [diff] [blame] | 141 | #define ATOMIC64_FETCH_OP(op, c_op) \ |
| 142 | static __inline__ long \ |
| 143 | ia64_atomic64_fetch_##op (__s64 i, atomic64_t *v) \ |
| 144 | { \ |
| 145 | __s64 old, new; \ |
| 146 | CMPXCHG_BUGCHECK_DECL \ |
| 147 | \ |
| 148 | do { \ |
| 149 | CMPXCHG_BUGCHECK(v); \ |
| 150 | old = atomic64_read(v); \ |
| 151 | new = old c_op i; \ |
| 152 | } while (ia64_cmpxchg(acq, v, old, new, sizeof(atomic64_t)) != old); \ |
| 153 | return old; \ |
| 154 | } |
| 155 | |
| 156 | #define ATOMIC64_OPS(op, c_op) \ |
| 157 | ATOMIC64_OP(op, c_op) \ |
| 158 | ATOMIC64_FETCH_OP(op, c_op) |
| 159 | |
| 160 | ATOMIC64_OPS(add, +) |
| 161 | ATOMIC64_OPS(sub, -) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
Peter Zijlstra | 08be2da | 2014-03-23 18:20:30 +0100 | [diff] [blame] | 163 | #define atomic64_add_return(i,v) \ |
| 164 | ({ \ |
| 165 | long __ia64_aar_i = (i); \ |
Matthew Wilcox | 2879b65 | 2018-02-19 09:41:26 -0800 | [diff] [blame] | 166 | __ia64_atomic_const(i) \ |
Peter Zijlstra | 08be2da | 2014-03-23 18:20:30 +0100 | [diff] [blame] | 167 | ? ia64_fetch_and_add(__ia64_aar_i, &(v)->counter) \ |
| 168 | : ia64_atomic64_add(__ia64_aar_i, v); \ |
| 169 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Peter Zijlstra | 08be2da | 2014-03-23 18:20:30 +0100 | [diff] [blame] | 171 | #define atomic64_sub_return(i,v) \ |
| 172 | ({ \ |
| 173 | long __ia64_asr_i = (i); \ |
Matthew Wilcox | 2879b65 | 2018-02-19 09:41:26 -0800 | [diff] [blame] | 174 | __ia64_atomic_const(i) \ |
Peter Zijlstra | 08be2da | 2014-03-23 18:20:30 +0100 | [diff] [blame] | 175 | ? ia64_fetch_and_add(-__ia64_asr_i, &(v)->counter) \ |
| 176 | : ia64_atomic64_sub(__ia64_asr_i, v); \ |
| 177 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
Peter Zijlstra | cc102507 | 2016-04-18 01:16:07 +0200 | [diff] [blame] | 179 | #define atomic64_fetch_add(i,v) \ |
| 180 | ({ \ |
| 181 | long __ia64_aar_i = (i); \ |
Matthew Wilcox | 2879b65 | 2018-02-19 09:41:26 -0800 | [diff] [blame] | 182 | __ia64_atomic_const(i) \ |
Peter Zijlstra | cc102507 | 2016-04-18 01:16:07 +0200 | [diff] [blame] | 183 | ? ia64_fetchadd(__ia64_aar_i, &(v)->counter, acq) \ |
| 184 | : ia64_atomic64_fetch_add(__ia64_aar_i, v); \ |
| 185 | }) |
Peter Zijlstra | 70ed473 | 2014-04-23 20:00:01 +0200 | [diff] [blame] | 186 | |
Peter Zijlstra | cc102507 | 2016-04-18 01:16:07 +0200 | [diff] [blame] | 187 | #define atomic64_fetch_sub(i,v) \ |
| 188 | ({ \ |
| 189 | long __ia64_asr_i = (i); \ |
Matthew Wilcox | 2879b65 | 2018-02-19 09:41:26 -0800 | [diff] [blame] | 190 | __ia64_atomic_const(i) \ |
Peter Zijlstra | cc102507 | 2016-04-18 01:16:07 +0200 | [diff] [blame] | 191 | ? ia64_fetchadd(-__ia64_asr_i, &(v)->counter, acq) \ |
| 192 | : ia64_atomic64_fetch_sub(__ia64_asr_i, v); \ |
| 193 | }) |
Peter Zijlstra | 70ed473 | 2014-04-23 20:00:01 +0200 | [diff] [blame] | 194 | |
Peter Zijlstra | cc102507 | 2016-04-18 01:16:07 +0200 | [diff] [blame] | 195 | ATOMIC64_FETCH_OP(and, &) |
| 196 | ATOMIC64_FETCH_OP(or, |) |
| 197 | ATOMIC64_FETCH_OP(xor, ^) |
| 198 | |
| 199 | #define atomic64_and(i,v) (void)ia64_atomic64_fetch_and(i,v) |
| 200 | #define atomic64_or(i,v) (void)ia64_atomic64_fetch_or(i,v) |
| 201 | #define atomic64_xor(i,v) (void)ia64_atomic64_fetch_xor(i,v) |
| 202 | |
| 203 | #define atomic64_fetch_and(i,v) ia64_atomic64_fetch_and(i,v) |
| 204 | #define atomic64_fetch_or(i,v) ia64_atomic64_fetch_or(i,v) |
| 205 | #define atomic64_fetch_xor(i,v) ia64_atomic64_fetch_xor(i,v) |
| 206 | |
| 207 | #undef ATOMIC64_OPS |
| 208 | #undef ATOMIC64_FETCH_OP |
Peter Zijlstra | 70ed473 | 2014-04-23 20:00:01 +0200 | [diff] [blame] | 209 | #undef ATOMIC64_OP |
| 210 | |
Mathieu Desnoyers | 8197913 | 2007-05-08 00:34:22 -0700 | [diff] [blame] | 211 | #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), old, new)) |
Ingo Molnar | ffbf670 | 2006-01-09 15:59:17 -0800 | [diff] [blame] | 212 | #define atomic_xchg(v, new) (xchg(&((v)->counter), new)) |
Nick Piggin | 4a6dae6 | 2005-11-13 16:07:24 -0800 | [diff] [blame] | 213 | |
Mathieu Desnoyers | 8197913 | 2007-05-08 00:34:22 -0700 | [diff] [blame] | 214 | #define atomic64_cmpxchg(v, old, new) \ |
| 215 | (cmpxchg(&((v)->counter), old, new)) |
| 216 | #define atomic64_xchg(v, new) (xchg(&((v)->counter), new)) |
| 217 | |
Arun Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 218 | static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u) |
Mathieu Desnoyers | 2856f5e | 2007-05-08 00:34:38 -0700 | [diff] [blame] | 219 | { |
| 220 | int c, old; |
| 221 | c = atomic_read(v); |
| 222 | for (;;) { |
| 223 | if (unlikely(c == (u))) |
| 224 | break; |
| 225 | old = atomic_cmpxchg((v), c, c + (a)); |
| 226 | if (likely(old == c)) |
| 227 | break; |
| 228 | c = old; |
| 229 | } |
Arun Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 230 | return c; |
Mathieu Desnoyers | 2856f5e | 2007-05-08 00:34:38 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 233 | |
Tony Luck | 01d69a8 | 2010-08-13 16:41:07 -0700 | [diff] [blame] | 234 | static __inline__ long atomic64_add_unless(atomic64_t *v, long a, long u) |
Mathieu Desnoyers | 2856f5e | 2007-05-08 00:34:38 -0700 | [diff] [blame] | 235 | { |
| 236 | long c, old; |
| 237 | c = atomic64_read(v); |
| 238 | for (;;) { |
| 239 | if (unlikely(c == (u))) |
| 240 | break; |
| 241 | old = atomic64_cmpxchg((v), c, c + (a)); |
| 242 | if (likely(old == c)) |
| 243 | break; |
| 244 | c = old; |
| 245 | } |
| 246 | return c != (u); |
| 247 | } |
| 248 | |
Mathieu Desnoyers | 8197913 | 2007-05-08 00:34:22 -0700 | [diff] [blame] | 249 | #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0) |
| 250 | |
Vineet Gupta | 445ed0a | 2016-10-07 17:02:07 -0700 | [diff] [blame] | 251 | static __inline__ long atomic64_dec_if_positive(atomic64_t *v) |
| 252 | { |
| 253 | long c, old, dec; |
| 254 | c = atomic64_read(v); |
| 255 | for (;;) { |
| 256 | dec = c - 1; |
| 257 | if (unlikely(dec < 0)) |
| 258 | break; |
| 259 | old = atomic64_cmpxchg((v), c, dec); |
| 260 | if (likely(old == c)) |
| 261 | break; |
| 262 | c = old; |
| 263 | } |
| 264 | return dec; |
| 265 | } |
| 266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | /* |
| 268 | * Atomically add I to V and return TRUE if the resulting value is |
| 269 | * negative. |
| 270 | */ |
| 271 | static __inline__ int |
| 272 | atomic_add_negative (int i, atomic_t *v) |
| 273 | { |
| 274 | return atomic_add_return(i, v) < 0; |
| 275 | } |
| 276 | |
Tony Luck | 01d69a8 | 2010-08-13 16:41:07 -0700 | [diff] [blame] | 277 | static __inline__ long |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | atomic64_add_negative (__s64 i, atomic64_t *v) |
| 279 | { |
| 280 | return atomic64_add_return(i, v) < 0; |
| 281 | } |
| 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | #define atomic_dec_return(v) atomic_sub_return(1, (v)) |
| 284 | #define atomic_inc_return(v) atomic_add_return(1, (v)) |
| 285 | #define atomic64_dec_return(v) atomic64_sub_return(1, (v)) |
| 286 | #define atomic64_inc_return(v) atomic64_add_return(1, (v)) |
| 287 | |
| 288 | #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0) |
| 289 | #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0) |
| 290 | #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0) |
| 291 | #define atomic64_sub_and_test(i,v) (atomic64_sub_return((i), (v)) == 0) |
| 292 | #define atomic64_dec_and_test(v) (atomic64_sub_return(1, (v)) == 0) |
| 293 | #define atomic64_inc_and_test(v) (atomic64_add_return(1, (v)) == 0) |
| 294 | |
Peter Zijlstra | 08be2da | 2014-03-23 18:20:30 +0100 | [diff] [blame] | 295 | #define atomic_add(i,v) (void)atomic_add_return((i), (v)) |
| 296 | #define atomic_sub(i,v) (void)atomic_sub_return((i), (v)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | #define atomic_inc(v) atomic_add(1, (v)) |
| 298 | #define atomic_dec(v) atomic_sub(1, (v)) |
| 299 | |
Peter Zijlstra | 08be2da | 2014-03-23 18:20:30 +0100 | [diff] [blame] | 300 | #define atomic64_add(i,v) (void)atomic64_add_return((i), (v)) |
| 301 | #define atomic64_sub(i,v) (void)atomic64_sub_return((i), (v)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | #define atomic64_inc(v) atomic64_add(1, (v)) |
| 303 | #define atomic64_dec(v) atomic64_sub(1, (v)) |
| 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | #endif /* _ASM_IA64_ATOMIC_H */ |