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