blob: 8a7bd80c8b331c8d6b4f93bab2d92f8c4c54fbab [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __ASM_SH_ATOMIC_H
2#define __ASM_SH_ATOMIC_H
3
Rich Felker2b47d542016-07-28 19:21:10 +00004#if defined(CONFIG_CPU_J2)
5
6#include <asm-generic/atomic.h>
7
8#else
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010/*
11 * Atomic operations that C can't guarantee us. Useful for
12 * resource counting etc..
13 *
14 */
15
Matthew Wilcoxea4354672009-01-06 14:40:39 -080016#include <linux/compiler.h>
17#include <linux/types.h>
David Howellse839ca52012-03-28 18:30:03 +010018#include <asm/cmpxchg.h>
Peter Zijlstra603228b2014-03-13 19:00:35 +010019#include <asm/barrier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Nobuhiro Iwamatsuec2ccd82012-04-27 11:12:38 +093021#define ATOMIC_INIT(i) { (i) }
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Peter Zijlstra62e8a322015-09-18 11:13:10 +020023#define atomic_read(v) READ_ONCE((v)->counter)
24#define atomic_set(v,i) WRITE_ONCE((v)->counter, (i))
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Stuart Menefy1efe4ce2007-11-30 16:12:36 +090026#if defined(CONFIG_GUSA_RB)
27#include <asm/atomic-grb.h>
28#elif defined(CONFIG_CPU_SH4A)
Paul Mundtec723fbe2006-12-07 20:33:38 +090029#include <asm/atomic-llsc.h>
Paul Mundt781125c2006-09-27 17:52:19 +090030#else
Paul Mundtec723fbe2006-12-07 20:33:38 +090031#include <asm/atomic-irq.h>
Paul Mundt781125c2006-09-27 17:52:19 +090032#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
Paul Mundt8c0b8132010-01-08 17:02:17 +090035#define atomic_dec_return(v) atomic_sub_return(1, (v))
36#define atomic_inc_return(v) atomic_add_return(1, (v))
37#define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
38#define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
39#define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Paul Mundt8c0b8132010-01-08 17:02:17 +090041#define atomic_inc(v) atomic_add(1, (v))
42#define atomic_dec(v) atomic_sub(1, (v))
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Paul Mundt8c0b8132010-01-08 17:02:17 +090044#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
45#define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
46
47/**
Arun Sharmaf24219b2011-07-26 16:09:07 -070048 * __atomic_add_unless - add unless the number is a given value
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * @v: pointer of type atomic_t
Paul Mundt8c0b8132010-01-08 17:02:17 +090050 * @a: the amount to add to v...
51 * @u: ...unless v is equal to u.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 *
Paul Mundt8c0b8132010-01-08 17:02:17 +090053 * Atomically adds @a to @v, so long as it was not @u.
Arun Sharmaf24219b2011-07-26 16:09:07 -070054 * Returns the old value of @v.
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 */
Arun Sharmaf24219b2011-07-26 16:09:07 -070056static inline int __atomic_add_unless(atomic_t *v, int a, int u)
Nick Piggin8426e1f2005-11-13 16:07:25 -080057{
Paul Mundt8c0b8132010-01-08 17:02:17 +090058 int c, old;
59 c = atomic_read(v);
60 for (;;) {
61 if (unlikely(c == (u)))
62 break;
63 old = atomic_cmpxchg((v), c, c + (a));
64 if (likely(old == c))
65 break;
66 c = old;
67 }
Nick Piggin8426e1f2005-11-13 16:07:25 -080068
Arun Sharmaf24219b2011-07-26 16:09:07 -070069 return c;
Nick Piggin8426e1f2005-11-13 16:07:25 -080070}
Nick Piggin8426e1f2005-11-13 16:07:25 -080071
Rich Felker2b47d542016-07-28 19:21:10 +000072#endif /* CONFIG_CPU_J2 */
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#endif /* __ASM_SH_ATOMIC_H */