1 #ifndef __ARCH_H8300_ATOMIC__
2 #define __ARCH_H8300_ATOMIC__
3
4 #include <linux/types.h>
5 #include <asm/cmpxchg.h>
6
7 /*
8 * Atomic operations that C can't guarantee us. Useful for
9 * resource counting etc..
10 */
11
12 #define ATOMIC_INIT(i) { (i) }
13
14 #define atomic_read(v) READ_ONCE((v)->counter)
15 #define atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
16
17 #include <linux/kernel.h>
18
19 #define ATOMIC_OP_RETURN(op, c_op) \
20 static inline int atomic_##op##_return(int i, atomic_t *v) \
21 { \
22 h8300flags flags; \
23 int ret; \
24 \
25 flags = arch_local_irq_save(); \
26 ret = v->counter c_op i; \
27 arch_local_irq_restore(flags); \
28 return ret; \
29 }
30
31 #define ATOMIC_FETCH_OP(op, c_op) \
32 static inline int atomic_fetch_##op(int i, atomic_t *v) \
33 { \
34 h8300flags flags; \
35 int ret; \
36 \
37 flags = arch_local_irq_save(); \
38 ret = v->counter; \
39 v->counter c_op i; \
40 arch_local_irq_restore(flags); \
41 return ret; \
42 }
43
44 #define ATOMIC_OP(op, c_op) \
45 static inline void atomic_##op(int i, atomic_t *v) \
46 { \
47 h8300flags flags; \
48 \
49 flags = arch_local_irq_save(); \
50 v->counter c_op i; \
51 arch_local_irq_restore(flags); \
52 }
53
54 ATOMIC_OP_RETURN(add, +=)
55 ATOMIC_OP_RETURN(sub, -=)
56
57 #define ATOMIC_OPS(op, c_op) \
58 ATOMIC_OP(op, c_op) \
59 ATOMIC_FETCH_OP(op, c_op)
60
61 ATOMIC_OPS(and, &=)
62 ATOMIC_OPS(or, |=)
63 ATOMIC_OPS(xor, ^=)
64 ATOMIC_OPS(add, +=)
65 ATOMIC_OPS(sub, -=)
66
67 #undef ATOMIC_OPS
68 #undef ATOMIC_OP_RETURN
69 #undef ATOMIC_OP
70
71 #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
72 #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
73
74 #define atomic_inc_return(v) atomic_add_return(1, v)
75 #define atomic_dec_return(v) atomic_sub_return(1, v)
76
77 #define atomic_inc(v) (void)atomic_inc_return(v)
78 #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
79
80 #define atomic_dec(v) (void)atomic_dec_return(v)
81 #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
82
atomic_cmpxchg(atomic_t * v,int old,int new)83 static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
84 {
85 int ret;
86 h8300flags flags;
87
88 flags = arch_local_irq_save();
89 ret = v->counter;
90 if (likely(ret == old))
91 v->counter = new;
92 arch_local_irq_restore(flags);
93 return ret;
94 }
95
__atomic_add_unless(atomic_t * v,int a,int u)96 static inline int __atomic_add_unless(atomic_t *v, int a, int u)
97 {
98 int ret;
99 h8300flags flags;
100
101 flags = arch_local_irq_save();
102 ret = v->counter;
103 if (ret != u)
104 v->counter += a;
105 arch_local_irq_restore(flags);
106 return ret;
107 }
108
109 #endif /* __ARCH_H8300_ATOMIC __ */
110