1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __ASM_SH_ATOMIC_H 3 #define __ASM_SH_ATOMIC_H 4 5 #if defined(CONFIG_CPU_J2) 6 7 #include <asm-generic/atomic.h> 8 9 #else 10 11 /* 12 * Atomic operations that C can't guarantee us. Useful for 13 * resource counting etc.. 14 * 15 */ 16 17 #include <linux/compiler.h> 18 #include <linux/types.h> 19 #include <asm/cmpxchg.h> 20 #include <asm/barrier.h> 21 22 #define ATOMIC_INIT(i) { (i) } 23 24 #define atomic_read(v) READ_ONCE((v)->counter) 25 #define atomic_set(v,i) WRITE_ONCE((v)->counter, (i)) 26 27 #if defined(CONFIG_GUSA_RB) 28 #include <asm/atomic-grb.h> 29 #elif defined(CONFIG_CPU_SH4A) 30 #include <asm/atomic-llsc.h> 31 #else 32 #include <asm/atomic-irq.h> 33 #endif 34 35 #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0) 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) 41 42 #define atomic_inc(v) atomic_add(1, (v)) 43 #define atomic_dec(v) atomic_sub(1, (v)) 44 45 #define atomic_xchg(v, new) (xchg(&((v)->counter), new)) 46 #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n))) 47 48 /** 49 * __atomic_add_unless - add unless the number is a given value 50 * @v: pointer of type atomic_t 51 * @a: the amount to add to v... 52 * @u: ...unless v is equal to u. 53 * 54 * Atomically adds @a to @v, so long as it was not @u. 55 * Returns the old value of @v. 56 */ 57 static inline int __atomic_add_unless(atomic_t *v, int a, int u) 58 { 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 } 69 70 return c; 71 } 72 73 #endif /* CONFIG_CPU_J2 */ 74 75 #endif /* __ASM_SH_ATOMIC_H */ 76