blob: 3ccea238be2d42bfcd227e02fc2de76b1428f8d5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Maciej W. Rozyckiedf7b932013-11-01 23:47:05 +00002 * Atomic operations that C can't guarantee us. Useful for
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * resource counting etc..
4 *
5 * But use these as seldom as possible since they are much more slower
6 * than regular operations.
7 *
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file "COPYING" in the main directory of this archive
10 * for more details.
11 *
Ralf Baechlee303e082006-11-30 01:14:50 +000012 * Copyright (C) 1996, 97, 99, 2000, 03, 04, 06 by Ralf Baechle
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#ifndef _ASM_ATOMIC_H
15#define _ASM_ATOMIC_H
16
Ralf Baechle192ef362006-07-07 14:07:18 +010017#include <linux/irqflags.h>
Matthew Wilcoxea4354672009-01-06 14:40:39 -080018#include <linux/types.h>
Ralf Baechle0004a9d2006-10-31 03:45:07 +000019#include <asm/barrier.h>
Maciej W. Rozyckib0984c42014-11-15 22:08:48 +000020#include <asm/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/cpu-features.h>
David Howellsb81947c2012-03-28 18:30:02 +010022#include <asm/cmpxchg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/war.h>
24
Joshua Kinard49360842017-11-18 23:00:51 -050025/*
26 * Using a branch-likely instruction to check the result of an sc instruction
27 * works around a bug present in R10000 CPUs prior to revision 3.0 that could
28 * cause ll-sc sequences to execute non-atomically.
29 */
30#if R10000_LLSC_WAR
31# define __scbeqz "beqzl"
32#else
33# define __scbeqz "beqz"
34#endif
35
Ralf Baechle70342282013-01-22 12:59:30 +010036#define ATOMIC_INIT(i) { (i) }
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * atomic_read - read atomic variable
40 * @v: pointer of type atomic_t
41 *
42 * Atomically reads the value of @v.
43 */
Peter Zijlstra62e8a322015-09-18 11:13:10 +020044#define atomic_read(v) READ_ONCE((v)->counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/*
47 * atomic_set - set atomic variable
48 * @v: pointer of type atomic_t
49 * @i: required value
50 *
51 * Atomically sets the value of @v to @i.
52 */
Peter Zijlstra62e8a322015-09-18 11:13:10 +020053#define atomic_set(v, i) WRITE_ONCE((v)->counter, (i))
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +000055#define ATOMIC_OP(op, c_op, asm_op) \
56static __inline__ void atomic_##op(int i, atomic_t * v) \
57{ \
Joshua Kinard49360842017-11-18 23:00:51 -050058 if (kernel_uses_llsc) { \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +000059 int temp; \
60 \
61 __asm__ __volatile__( \
Joshua Kinard49360842017-11-18 23:00:51 -050062 " .set "MIPS_ISA_LEVEL" \n" \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +000063 "1: ll %0, %1 # atomic_" #op " \n" \
64 " " #asm_op " %0, %2 \n" \
65 " sc %0, %1 \n" \
Joshua Kinard49360842017-11-18 23:00:51 -050066 "\t" __scbeqz " %0, 1b \n" \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +000067 " .set mips0 \n" \
Markos Chandras94bfb752015-01-26 12:44:11 +000068 : "=&r" (temp), "+" GCC_OFF_SMALL_ASM() (v->counter) \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +000069 : "Ir" (i)); \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +000070 } else { \
71 unsigned long flags; \
72 \
73 raw_local_irq_save(flags); \
74 v->counter c_op i; \
75 raw_local_irq_restore(flags); \
76 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +000079#define ATOMIC_OP_RETURN(op, c_op, asm_op) \
Peter Zijlstra4ec45852016-04-18 01:15:25 +020080static __inline__ int atomic_##op##_return_relaxed(int i, atomic_t * v) \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +000081{ \
82 int result; \
83 \
Joshua Kinard49360842017-11-18 23:00:51 -050084 if (kernel_uses_llsc) { \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +000085 int temp; \
86 \
87 __asm__ __volatile__( \
Joshua Kinard49360842017-11-18 23:00:51 -050088 " .set "MIPS_ISA_LEVEL" \n" \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +000089 "1: ll %1, %2 # atomic_" #op "_return \n" \
90 " " #asm_op " %0, %1, %3 \n" \
91 " sc %0, %2 \n" \
Joshua Kinard49360842017-11-18 23:00:51 -050092 "\t" __scbeqz " %0, 1b \n" \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +000093 " " #asm_op " %0, %1, %3 \n" \
94 " .set mips0 \n" \
95 : "=&r" (result), "=&r" (temp), \
Markos Chandras94bfb752015-01-26 12:44:11 +000096 "+" GCC_OFF_SMALL_ASM() (v->counter) \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +000097 : "Ir" (i)); \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +000098 } else { \
99 unsigned long flags; \
100 \
101 raw_local_irq_save(flags); \
102 result = v->counter; \
103 result c_op i; \
104 v->counter = result; \
105 raw_local_irq_restore(flags); \
106 } \
107 \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +0000108 return result; \
109}
110
Peter Zijlstra4edac522016-04-18 01:16:06 +0200111#define ATOMIC_FETCH_OP(op, c_op, asm_op) \
Peter Zijlstra4ec45852016-04-18 01:15:25 +0200112static __inline__ int atomic_fetch_##op##_relaxed(int i, atomic_t * v) \
Peter Zijlstra4edac522016-04-18 01:16:06 +0200113{ \
114 int result; \
115 \
Joshua Kinard49360842017-11-18 23:00:51 -0500116 if (kernel_uses_llsc) { \
Peter Zijlstra4edac522016-04-18 01:16:06 +0200117 int temp; \
118 \
119 __asm__ __volatile__( \
Joshua Kinard49360842017-11-18 23:00:51 -0500120 " .set "MIPS_ISA_LEVEL" \n" \
Peter Zijlstra4edac522016-04-18 01:16:06 +0200121 "1: ll %1, %2 # atomic_fetch_" #op " \n" \
122 " " #asm_op " %0, %1, %3 \n" \
123 " sc %0, %2 \n" \
Joshua Kinard49360842017-11-18 23:00:51 -0500124 "\t" __scbeqz " %0, 1b \n" \
Peter Zijlstra4edac522016-04-18 01:16:06 +0200125 " move %0, %1 \n" \
126 " .set mips0 \n" \
127 : "=&r" (result), "=&r" (temp), \
128 "+" GCC_OFF_SMALL_ASM() (v->counter) \
129 : "Ir" (i)); \
Peter Zijlstra4edac522016-04-18 01:16:06 +0200130 } else { \
131 unsigned long flags; \
132 \
133 raw_local_irq_save(flags); \
134 result = v->counter; \
135 v->counter c_op i; \
136 raw_local_irq_restore(flags); \
137 } \
138 \
Peter Zijlstra4edac522016-04-18 01:16:06 +0200139 return result; \
140}
141
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +0000142#define ATOMIC_OPS(op, c_op, asm_op) \
143 ATOMIC_OP(op, c_op, asm_op) \
Peter Zijlstra4edac522016-04-18 01:16:06 +0200144 ATOMIC_OP_RETURN(op, c_op, asm_op) \
145 ATOMIC_FETCH_OP(op, c_op, asm_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Peter Zijlstraef315632014-03-26 17:56:43 +0100147ATOMIC_OPS(add, +=, addu)
148ATOMIC_OPS(sub, -=, subu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Peter Zijlstra4ec45852016-04-18 01:15:25 +0200150#define atomic_add_return_relaxed atomic_add_return_relaxed
151#define atomic_sub_return_relaxed atomic_sub_return_relaxed
152#define atomic_fetch_add_relaxed atomic_fetch_add_relaxed
153#define atomic_fetch_sub_relaxed atomic_fetch_sub_relaxed
154
Peter Zijlstra4edac522016-04-18 01:16:06 +0200155#undef ATOMIC_OPS
156#define ATOMIC_OPS(op, c_op, asm_op) \
157 ATOMIC_OP(op, c_op, asm_op) \
158 ATOMIC_FETCH_OP(op, c_op, asm_op)
159
Peter Zijlstra4edac522016-04-18 01:16:06 +0200160ATOMIC_OPS(and, &=, and)
161ATOMIC_OPS(or, |=, or)
162ATOMIC_OPS(xor, ^=, xor)
Peter Zijlstra27782f22014-04-23 19:51:36 +0200163
Peter Zijlstra4ec45852016-04-18 01:15:25 +0200164#define atomic_fetch_and_relaxed atomic_fetch_and_relaxed
165#define atomic_fetch_or_relaxed atomic_fetch_or_relaxed
166#define atomic_fetch_xor_relaxed atomic_fetch_xor_relaxed
167
Peter Zijlstraef315632014-03-26 17:56:43 +0100168#undef ATOMIC_OPS
Peter Zijlstra4edac522016-04-18 01:16:06 +0200169#undef ATOMIC_FETCH_OP
Peter Zijlstraef315632014-03-26 17:56:43 +0100170#undef ATOMIC_OP_RETURN
171#undef ATOMIC_OP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173/*
Arnaud Gierschf10d14d2005-11-13 00:38:18 +0100174 * atomic_sub_if_positive - conditionally subtract integer from atomic variable
175 * @i: integer value to subtract
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * @v: pointer of type atomic_t
177 *
Arnaud Gierschf10d14d2005-11-13 00:38:18 +0100178 * Atomically test @v and subtract @i if @v is greater or equal than @i.
179 * The function returns the old value of @v minus @i.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 */
181static __inline__ int atomic_sub_if_positive(int i, atomic_t * v)
182{
Ralf Baechle915ec1e2009-01-12 00:52:18 +0000183 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
David Daneyf252ffd2010-01-08 17:17:43 -0800185 smp_mb__before_llsc();
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000186
Joshua Kinard49360842017-11-18 23:00:51 -0500187 if (kernel_uses_llsc) {
Ralf Baechle915ec1e2009-01-12 00:52:18 +0000188 int temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 __asm__ __volatile__(
Markos Chandras0038df22015-01-06 11:09:24 +0000191 " .set "MIPS_ISA_LEVEL" \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 "1: ll %1, %2 # atomic_sub_if_positive\n"
193 " subu %0, %1, %3 \n"
Joshua Kinarda0a5ac32017-11-18 22:29:56 -0500194 " move %1, %0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 " bltz %0, 1f \n"
Joshua Kinarda0a5ac32017-11-18 22:29:56 -0500196 " sc %1, %2 \n"
Joshua Kinard49360842017-11-18 23:00:51 -0500197 "\t" __scbeqz " %1, 1b \n"
Ralf Baechle50952022008-07-03 23:28:35 +0100198 "1: \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000199 " .set mips0 \n"
Maciej W. Rozyckib0984c42014-11-15 22:08:48 +0000200 : "=&r" (result), "=&r" (temp),
Markos Chandras94bfb752015-01-26 12:44:11 +0000201 "+" GCC_OFF_SMALL_ASM() (v->counter)
Joshua Kinardb4f2a172012-06-24 21:01:34 -0400202 : "Ir" (i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 } else {
204 unsigned long flags;
205
Ralf Baechle49edd092007-03-16 16:10:36 +0000206 raw_local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 result = v->counter;
208 result -= i;
209 if (result >= 0)
210 v->counter = result;
Ralf Baechle49edd092007-03-16 16:10:36 +0000211 raw_local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213
Ralf Baechle17099b12007-07-14 13:24:05 +0100214 smp_llsc_mb();
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return result;
217}
218
Mathieu Desnoyerse12f6442007-05-08 00:34:24 -0700219#define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
220#define atomic_xchg(v, new) (xchg(&((v)->counter), (new)))
Nick Piggin4a6dae62005-11-13 16:07:24 -0800221
Nick Piggin8426e1f2005-11-13 16:07:25 -0800222/**
Arun Sharmaf24219b2011-07-26 16:09:07 -0700223 * __atomic_add_unless - add unless the number is a given value
Nick Piggin8426e1f2005-11-13 16:07:25 -0800224 * @v: pointer of type atomic_t
225 * @a: the amount to add to v...
226 * @u: ...unless v is equal to u.
227 *
228 * Atomically adds @a to @v, so long as it was not @u.
Arun Sharmaf24219b2011-07-26 16:09:07 -0700229 * Returns the old value of @v.
Nick Piggin8426e1f2005-11-13 16:07:25 -0800230 */
Arun Sharmaf24219b2011-07-26 16:09:07 -0700231static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -0700232{
233 int c, old;
234 c = atomic_read(v);
235 for (;;) {
236 if (unlikely(c == (u)))
237 break;
238 old = atomic_cmpxchg((v), c, c + (a));
239 if (likely(old == c))
240 break;
241 c = old;
242 }
Arun Sharmaf24219b2011-07-26 16:09:07 -0700243 return c;
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -0700244}
Nick Piggin8426e1f2005-11-13 16:07:25 -0800245
Ralf Baechle21a151d2007-10-11 23:46:15 +0100246#define atomic_dec_return(v) atomic_sub_return(1, (v))
247#define atomic_inc_return(v) atomic_add_return(1, (v))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249/*
250 * atomic_sub_and_test - subtract value from variable and test result
251 * @i: integer value to subtract
252 * @v: pointer of type atomic_t
253 *
254 * Atomically subtracts @i from @v and returns
255 * true if the result is zero, or false for all
256 * other cases.
257 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100258#define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260/*
261 * atomic_inc_and_test - increment and test
262 * @v: pointer of type atomic_t
263 *
264 * Atomically increments @v by 1
265 * and returns true if the result is zero, or false for all
266 * other cases.
267 */
268#define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
269
270/*
271 * atomic_dec_and_test - decrement by 1 and test
272 * @v: pointer of type atomic_t
273 *
274 * Atomically decrements @v by 1 and
275 * returns true if the result is 0, or false for all other
276 * cases.
277 */
278#define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
279
280/*
281 * atomic_dec_if_positive - decrement by 1 if old value positive
282 * @v: pointer of type atomic_t
283 */
284#define atomic_dec_if_positive(v) atomic_sub_if_positive(1, v)
285
286/*
287 * atomic_inc - increment atomic variable
288 * @v: pointer of type atomic_t
289 *
290 * Atomically increments @v by 1.
291 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100292#define atomic_inc(v) atomic_add(1, (v))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294/*
295 * atomic_dec - decrement and test
296 * @v: pointer of type atomic_t
297 *
298 * Atomically decrements @v by 1.
299 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100300#define atomic_dec(v) atomic_sub(1, (v))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302/*
303 * atomic_add_negative - add and test if negative
304 * @v: pointer of type atomic_t
305 * @i: integer value to add
306 *
307 * Atomically adds @i to @v and returns true
308 * if the result is negative, or false when
309 * result is greater than or equal to zero.
310 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100311#define atomic_add_negative(i, v) (atomic_add_return(i, (v)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Ralf Baechle875d43e2005-09-03 15:56:16 -0700313#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315#define ATOMIC64_INIT(i) { (i) }
316
317/*
318 * atomic64_read - read atomic variable
319 * @v: pointer of type atomic64_t
320 *
321 */
Peter Zijlstra62e8a322015-09-18 11:13:10 +0200322#define atomic64_read(v) READ_ONCE((v)->counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324/*
325 * atomic64_set - set atomic variable
326 * @v: pointer of type atomic64_t
327 * @i: required value
328 */
Peter Zijlstra62e8a322015-09-18 11:13:10 +0200329#define atomic64_set(v, i) WRITE_ONCE((v)->counter, (i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +0000331#define ATOMIC64_OP(op, c_op, asm_op) \
332static __inline__ void atomic64_##op(long i, atomic64_t * v) \
333{ \
Joshua Kinard49360842017-11-18 23:00:51 -0500334 if (kernel_uses_llsc) { \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +0000335 long temp; \
336 \
337 __asm__ __volatile__( \
Joshua Kinard49360842017-11-18 23:00:51 -0500338 " .set "MIPS_ISA_LEVEL" \n" \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +0000339 "1: lld %0, %1 # atomic64_" #op " \n" \
340 " " #asm_op " %0, %2 \n" \
341 " scd %0, %1 \n" \
Joshua Kinard49360842017-11-18 23:00:51 -0500342 "\t" __scbeqz " %0, 1b \n" \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +0000343 " .set mips0 \n" \
Markos Chandras94bfb752015-01-26 12:44:11 +0000344 : "=&r" (temp), "+" GCC_OFF_SMALL_ASM() (v->counter) \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +0000345 : "Ir" (i)); \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +0000346 } else { \
347 unsigned long flags; \
348 \
349 raw_local_irq_save(flags); \
350 v->counter c_op i; \
351 raw_local_irq_restore(flags); \
352 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +0000355#define ATOMIC64_OP_RETURN(op, c_op, asm_op) \
Peter Zijlstra4ec45852016-04-18 01:15:25 +0200356static __inline__ long atomic64_##op##_return_relaxed(long i, atomic64_t * v) \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +0000357{ \
358 long result; \
359 \
Joshua Kinard49360842017-11-18 23:00:51 -0500360 if (kernel_uses_llsc) { \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +0000361 long temp; \
362 \
363 __asm__ __volatile__( \
Joshua Kinard49360842017-11-18 23:00:51 -0500364 " .set "MIPS_ISA_LEVEL" \n" \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +0000365 "1: lld %1, %2 # atomic64_" #op "_return\n" \
366 " " #asm_op " %0, %1, %3 \n" \
367 " scd %0, %2 \n" \
Joshua Kinard49360842017-11-18 23:00:51 -0500368 "\t" __scbeqz " %0, 1b \n" \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +0000369 " " #asm_op " %0, %1, %3 \n" \
370 " .set mips0 \n" \
371 : "=&r" (result), "=&r" (temp), \
Markos Chandras94bfb752015-01-26 12:44:11 +0000372 "+" GCC_OFF_SMALL_ASM() (v->counter) \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +0000373 : "Ir" (i)); \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +0000374 } else { \
375 unsigned long flags; \
376 \
377 raw_local_irq_save(flags); \
378 result = v->counter; \
379 result c_op i; \
380 v->counter = result; \
381 raw_local_irq_restore(flags); \
382 } \
383 \
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +0000384 return result; \
385}
386
Peter Zijlstra4edac522016-04-18 01:16:06 +0200387#define ATOMIC64_FETCH_OP(op, c_op, asm_op) \
Peter Zijlstra4ec45852016-04-18 01:15:25 +0200388static __inline__ long atomic64_fetch_##op##_relaxed(long i, atomic64_t * v) \
Peter Zijlstra4edac522016-04-18 01:16:06 +0200389{ \
390 long result; \
391 \
Peter Zijlstra4edac522016-04-18 01:16:06 +0200392 if (kernel_uses_llsc && R10000_LLSC_WAR) { \
393 long temp; \
394 \
395 __asm__ __volatile__( \
Joshua Kinard49360842017-11-18 23:00:51 -0500396 " .set "MIPS_ISA_LEVEL" \n" \
Peter Zijlstra4edac522016-04-18 01:16:06 +0200397 "1: lld %1, %2 # atomic64_fetch_" #op "\n" \
398 " " #asm_op " %0, %1, %3 \n" \
399 " scd %0, %2 \n" \
Joshua Kinard49360842017-11-18 23:00:51 -0500400 "\t" __scbeqz " %0, 1b \n" \
Peter Zijlstra4edac522016-04-18 01:16:06 +0200401 " move %0, %1 \n" \
402 " .set mips0 \n" \
403 : "=&r" (result), "=&r" (temp), \
404 "+" GCC_OFF_SMALL_ASM() (v->counter) \
405 : "Ir" (i)); \
Peter Zijlstra4edac522016-04-18 01:16:06 +0200406 } else { \
407 unsigned long flags; \
408 \
409 raw_local_irq_save(flags); \
410 result = v->counter; \
411 v->counter c_op i; \
412 raw_local_irq_restore(flags); \
413 } \
414 \
Peter Zijlstra4edac522016-04-18 01:16:06 +0200415 return result; \
416}
417
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +0000418#define ATOMIC64_OPS(op, c_op, asm_op) \
419 ATOMIC64_OP(op, c_op, asm_op) \
Peter Zijlstra4edac522016-04-18 01:16:06 +0200420 ATOMIC64_OP_RETURN(op, c_op, asm_op) \
421 ATOMIC64_FETCH_OP(op, c_op, asm_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Peter Zijlstraef315632014-03-26 17:56:43 +0100423ATOMIC64_OPS(add, +=, daddu)
424ATOMIC64_OPS(sub, -=, dsubu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Peter Zijlstra4ec45852016-04-18 01:15:25 +0200426#define atomic64_add_return_relaxed atomic64_add_return_relaxed
427#define atomic64_sub_return_relaxed atomic64_sub_return_relaxed
428#define atomic64_fetch_add_relaxed atomic64_fetch_add_relaxed
429#define atomic64_fetch_sub_relaxed atomic64_fetch_sub_relaxed
430
Peter Zijlstraef315632014-03-26 17:56:43 +0100431#undef ATOMIC64_OPS
Peter Zijlstra4edac522016-04-18 01:16:06 +0200432#define ATOMIC64_OPS(op, c_op, asm_op) \
433 ATOMIC64_OP(op, c_op, asm_op) \
434 ATOMIC64_FETCH_OP(op, c_op, asm_op)
435
436ATOMIC64_OPS(and, &=, and)
437ATOMIC64_OPS(or, |=, or)
438ATOMIC64_OPS(xor, ^=, xor)
439
Peter Zijlstra4ec45852016-04-18 01:15:25 +0200440#define atomic64_fetch_and_relaxed atomic64_fetch_and_relaxed
441#define atomic64_fetch_or_relaxed atomic64_fetch_or_relaxed
442#define atomic64_fetch_xor_relaxed atomic64_fetch_xor_relaxed
443
Peter Zijlstra4edac522016-04-18 01:16:06 +0200444#undef ATOMIC64_OPS
445#undef ATOMIC64_FETCH_OP
Peter Zijlstraef315632014-03-26 17:56:43 +0100446#undef ATOMIC64_OP_RETURN
447#undef ATOMIC64_OP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449/*
Maciej W. Rozyckiddb31082014-11-15 22:09:54 +0000450 * atomic64_sub_if_positive - conditionally subtract integer from atomic
451 * variable
Arnaud Gierschf10d14d2005-11-13 00:38:18 +0100452 * @i: integer value to subtract
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 * @v: pointer of type atomic64_t
454 *
Arnaud Gierschf10d14d2005-11-13 00:38:18 +0100455 * Atomically test @v and subtract @i if @v is greater or equal than @i.
456 * The function returns the old value of @v minus @i.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 */
458static __inline__ long atomic64_sub_if_positive(long i, atomic64_t * v)
459{
Ralf Baechle915ec1e2009-01-12 00:52:18 +0000460 long result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
David Daneyf252ffd2010-01-08 17:17:43 -0800462 smp_mb__before_llsc();
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000463
Joshua Kinard49360842017-11-18 23:00:51 -0500464 if (kernel_uses_llsc) {
Ralf Baechle915ec1e2009-01-12 00:52:18 +0000465 long temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 __asm__ __volatile__(
Markos Chandras0038df22015-01-06 11:09:24 +0000468 " .set "MIPS_ISA_LEVEL" \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 "1: lld %1, %2 # atomic64_sub_if_positive\n"
470 " dsubu %0, %1, %3 \n"
Joshua Kinarda0a5ac32017-11-18 22:29:56 -0500471 " move %1, %0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 " bltz %0, 1f \n"
Joshua Kinarda0a5ac32017-11-18 22:29:56 -0500473 " scd %1, %2 \n"
Joshua Kinard49360842017-11-18 23:00:51 -0500474 "\t" __scbeqz " %1, 1b \n"
Ralf Baechle50952022008-07-03 23:28:35 +0100475 "1: \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000476 " .set mips0 \n"
Maciej W. Rozyckib0984c42014-11-15 22:08:48 +0000477 : "=&r" (result), "=&r" (temp),
Markos Chandras94bfb752015-01-26 12:44:11 +0000478 "+" GCC_OFF_SMALL_ASM() (v->counter)
Joshua Kinardb4f2a172012-06-24 21:01:34 -0400479 : "Ir" (i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 } else {
481 unsigned long flags;
482
Ralf Baechle49edd092007-03-16 16:10:36 +0000483 raw_local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 result = v->counter;
485 result -= i;
486 if (result >= 0)
487 v->counter = result;
Ralf Baechle49edd092007-03-16 16:10:36 +0000488 raw_local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490
Ralf Baechle17099b12007-07-14 13:24:05 +0100491 smp_llsc_mb();
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return result;
494}
495
Mathieu Desnoyerse12f6442007-05-08 00:34:24 -0700496#define atomic64_cmpxchg(v, o, n) \
Atsushi Nemoto7b239bb2007-05-10 23:47:45 +0900497 ((__typeof__((v)->counter))cmpxchg(&((v)->counter), (o), (n)))
Mathieu Desnoyerse12f6442007-05-08 00:34:24 -0700498#define atomic64_xchg(v, new) (xchg(&((v)->counter), (new)))
499
500/**
501 * atomic64_add_unless - add unless the number is a given value
502 * @v: pointer of type atomic64_t
503 * @a: the amount to add to v...
504 * @u: ...unless v is equal to u.
505 *
506 * Atomically adds @a to @v, so long as it was not @u.
Ralf Baechlef25319d2015-10-16 23:09:57 +0200507 * Returns true iff @v was not @u.
Mathieu Desnoyerse12f6442007-05-08 00:34:24 -0700508 */
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -0700509static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u)
510{
511 long c, old;
512 c = atomic64_read(v);
513 for (;;) {
514 if (unlikely(c == (u)))
515 break;
516 old = atomic64_cmpxchg((v), c, c + (a));
517 if (likely(old == c))
518 break;
519 c = old;
520 }
521 return c != (u);
522}
523
Mathieu Desnoyerse12f6442007-05-08 00:34:24 -0700524#define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
525
Ralf Baechle21a151d2007-10-11 23:46:15 +0100526#define atomic64_dec_return(v) atomic64_sub_return(1, (v))
527#define atomic64_inc_return(v) atomic64_add_return(1, (v))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529/*
530 * atomic64_sub_and_test - subtract value from variable and test result
531 * @i: integer value to subtract
532 * @v: pointer of type atomic64_t
533 *
534 * Atomically subtracts @i from @v and returns
535 * true if the result is zero, or false for all
536 * other cases.
537 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100538#define atomic64_sub_and_test(i, v) (atomic64_sub_return((i), (v)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540/*
541 * atomic64_inc_and_test - increment and test
542 * @v: pointer of type atomic64_t
543 *
544 * Atomically increments @v by 1
545 * and returns true if the result is zero, or false for all
546 * other cases.
547 */
548#define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
549
550/*
551 * atomic64_dec_and_test - decrement by 1 and test
552 * @v: pointer of type atomic64_t
553 *
554 * Atomically decrements @v by 1 and
555 * returns true if the result is 0, or false for all other
556 * cases.
557 */
558#define atomic64_dec_and_test(v) (atomic64_sub_return(1, (v)) == 0)
559
560/*
561 * atomic64_dec_if_positive - decrement by 1 if old value positive
562 * @v: pointer of type atomic64_t
563 */
564#define atomic64_dec_if_positive(v) atomic64_sub_if_positive(1, v)
565
566/*
567 * atomic64_inc - increment atomic variable
568 * @v: pointer of type atomic64_t
569 *
570 * Atomically increments @v by 1.
571 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100572#define atomic64_inc(v) atomic64_add(1, (v))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574/*
575 * atomic64_dec - decrement and test
576 * @v: pointer of type atomic64_t
577 *
578 * Atomically decrements @v by 1.
579 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100580#define atomic64_dec(v) atomic64_sub(1, (v))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582/*
583 * atomic64_add_negative - add and test if negative
584 * @v: pointer of type atomic64_t
585 * @i: integer value to add
586 *
587 * Atomically adds @i to @v and returns true
588 * if the result is negative, or false when
589 * result is greater than or equal to zero.
590 */
Ralf Baechle21a151d2007-10-11 23:46:15 +0100591#define atomic64_add_negative(i, v) (atomic64_add_return(i, (v)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Ralf Baechle875d43e2005-09-03 15:56:16 -0700593#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595#endif /* _ASM_ATOMIC_H */