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_SH_ATOMIC_H |
| 3 | #define __ASM_SH_ATOMIC_H |
| 4 | |
Rich Felker | 2b47d54 | 2016-07-28 19:21:10 +0000 | [diff] [blame] | 5 | #if defined(CONFIG_CPU_J2) |
| 6 | |
| 7 | #include <asm-generic/atomic.h> |
| 8 | |
| 9 | #else |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | /* |
| 12 | * Atomic operations that C can't guarantee us. Useful for |
| 13 | * resource counting etc.. |
| 14 | * |
| 15 | */ |
| 16 | |
Matthew Wilcox | ea435467 | 2009-01-06 14:40:39 -0800 | [diff] [blame] | 17 | #include <linux/compiler.h> |
| 18 | #include <linux/types.h> |
David Howells | e839ca5 | 2012-03-28 18:30:03 +0100 | [diff] [blame] | 19 | #include <asm/cmpxchg.h> |
Peter Zijlstra | 603228b | 2014-03-13 19:00:35 +0100 | [diff] [blame] | 20 | #include <asm/barrier.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Nobuhiro Iwamatsu | ec2ccd8 | 2012-04-27 11:12:38 +0930 | [diff] [blame] | 22 | #define ATOMIC_INIT(i) { (i) } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Peter Zijlstra | 62e8a32 | 2015-09-18 11:13:10 +0200 | [diff] [blame] | 24 | #define atomic_read(v) READ_ONCE((v)->counter) |
| 25 | #define atomic_set(v,i) WRITE_ONCE((v)->counter, (i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Stuart Menefy | 1efe4ce | 2007-11-30 16:12:36 +0900 | [diff] [blame] | 27 | #if defined(CONFIG_GUSA_RB) |
| 28 | #include <asm/atomic-grb.h> |
| 29 | #elif defined(CONFIG_CPU_SH4A) |
Paul Mundt | ec723fbe | 2006-12-07 20:33:38 +0900 | [diff] [blame] | 30 | #include <asm/atomic-llsc.h> |
Paul Mundt | 781125c | 2006-09-27 17:52:19 +0900 | [diff] [blame] | 31 | #else |
Paul Mundt | ec723fbe | 2006-12-07 20:33:38 +0900 | [diff] [blame] | 32 | #include <asm/atomic-irq.h> |
Paul Mundt | 781125c | 2006-09-27 17:52:19 +0900 | [diff] [blame] | 33 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0) |
Paul Mundt | 8c0b813 | 2010-01-08 17:02:17 +0900 | [diff] [blame] | 36 | #define atomic_dec_return(v) atomic_sub_return(1, (v)) |
| 37 | #define atomic_inc_return(v) atomic_add_return(1, (v)) |
| 38 | #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0) |
| 39 | #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0) |
| 40 | #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Paul Mundt | 8c0b813 | 2010-01-08 17:02:17 +0900 | [diff] [blame] | 42 | #define atomic_inc(v) atomic_add(1, (v)) |
| 43 | #define atomic_dec(v) atomic_sub(1, (v)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Paul Mundt | 8c0b813 | 2010-01-08 17:02:17 +0900 | [diff] [blame] | 45 | #define atomic_xchg(v, new) (xchg(&((v)->counter), new)) |
| 46 | #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n))) |
| 47 | |
| 48 | /** |
Arun Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 49 | * __atomic_add_unless - add unless the number is a given value |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | * @v: pointer of type atomic_t |
Paul Mundt | 8c0b813 | 2010-01-08 17:02:17 +0900 | [diff] [blame] | 51 | * @a: the amount to add to v... |
| 52 | * @u: ...unless v is equal to u. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | * |
Paul Mundt | 8c0b813 | 2010-01-08 17:02:17 +0900 | [diff] [blame] | 54 | * Atomically adds @a to @v, so long as it was not @u. |
Arun Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 55 | * Returns the old value of @v. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | */ |
Arun Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 57 | static inline int __atomic_add_unless(atomic_t *v, int a, int u) |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 58 | { |
Paul Mundt | 8c0b813 | 2010-01-08 17:02:17 +0900 | [diff] [blame] | 59 | int c, old; |
| 60 | c = atomic_read(v); |
| 61 | for (;;) { |
| 62 | if (unlikely(c == (u))) |
| 63 | break; |
| 64 | old = atomic_cmpxchg((v), c, c + (a)); |
| 65 | if (likely(old == c)) |
| 66 | break; |
| 67 | c = old; |
| 68 | } |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 69 | |
Arun Sharma | f24219b | 2011-07-26 16:09:07 -0700 | [diff] [blame] | 70 | return c; |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 71 | } |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 72 | |
Rich Felker | 2b47d54 | 2016-07-28 19:21:10 +0000 | [diff] [blame] | 73 | #endif /* CONFIG_CPU_J2 */ |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | #endif /* __ASM_SH_ATOMIC_H */ |