sh: Various cosmetic cleanups.
[firefly-linux-kernel-4.4.55.git] / include / asm-sh / atomic.h
1 #ifndef __ASM_SH_ATOMIC_H
2 #define __ASM_SH_ATOMIC_H
3
4 /*
5  * Atomic operations that C can't guarantee us.  Useful for
6  * resource counting etc..
7  *
8  */
9
10 typedef struct { volatile int counter; } atomic_t;
11
12 #define ATOMIC_INIT(i)  ( (atomic_t) { (i) } )
13
14 #define atomic_read(v)          ((v)->counter)
15 #define atomic_set(v,i)         ((v)->counter = (i))
16
17 #include <linux/compiler.h>
18 #include <asm/system.h>
19
20 /*
21  * To get proper branch prediction for the main line, we must branch
22  * forward to code at the end of this object's .text section, then
23  * branch back to restart the operation.
24  */
25
26 static __inline__ void atomic_add(int i, atomic_t * v)
27 {
28         unsigned long flags;
29
30         local_irq_save(flags);
31         *(long *)v += i;
32         local_irq_restore(flags);
33 }
34
35 static __inline__ void atomic_sub(int i, atomic_t *v)
36 {
37         unsigned long flags;
38
39         local_irq_save(flags);
40         *(long *)v -= i;
41         local_irq_restore(flags);
42 }
43
44 static __inline__ int atomic_add_return(int i, atomic_t * v)
45 {
46         unsigned long temp, flags;
47
48         local_irq_save(flags);
49         temp = *(long *)v;
50         temp += i;
51         *(long *)v = temp;
52         local_irq_restore(flags);
53
54         return temp;
55 }
56
57 #define atomic_add_negative(a, v)       (atomic_add_return((a), (v)) < 0)
58
59 static __inline__ int atomic_sub_return(int i, atomic_t * v)
60 {
61         unsigned long temp, flags;
62
63         local_irq_save(flags);
64         temp = *(long *)v;
65         temp -= i;
66         *(long *)v = temp;
67         local_irq_restore(flags);
68
69         return temp;
70 }
71
72 #define atomic_dec_return(v) atomic_sub_return(1,(v))
73 #define atomic_inc_return(v) atomic_add_return(1,(v))
74
75 /*
76  * atomic_inc_and_test - increment and test
77  * @v: pointer of type atomic_t
78  *
79  * Atomically increments @v by 1
80  * and returns true if the result is zero, or false for all
81  * other cases.
82  */
83 #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
84
85 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
86 #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
87
88 #define atomic_inc(v) atomic_add(1,(v))
89 #define atomic_dec(v) atomic_sub(1,(v))
90
91 static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
92 {
93         int ret;
94         unsigned long flags;
95
96         local_irq_save(flags);
97         ret = v->counter;
98         if (likely(ret == old))
99                 v->counter = new;
100         local_irq_restore(flags);
101
102         return ret;
103 }
104
105 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
106
107 static inline int atomic_add_unless(atomic_t *v, int a, int u)
108 {
109         int ret;
110         unsigned long flags;
111
112         local_irq_save(flags);
113         ret = v->counter;
114         if (ret != u)
115                 v->counter += a;
116         local_irq_restore(flags);
117
118         return ret != u;
119 }
120 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
121
122 static __inline__ void atomic_clear_mask(unsigned int mask, atomic_t *v)
123 {
124         unsigned long flags;
125
126         local_irq_save(flags);
127         *(long *)v &= ~mask;
128         local_irq_restore(flags);
129 }
130
131 static __inline__ void atomic_set_mask(unsigned int mask, atomic_t *v)
132 {
133         unsigned long flags;
134
135         local_irq_save(flags);
136         *(long *)v |= mask;
137         local_irq_restore(flags);
138 }
139
140 /* Atomic operations are already serializing on SH */
141 #define smp_mb__before_atomic_dec()     barrier()
142 #define smp_mb__after_atomic_dec()      barrier()
143 #define smp_mb__before_atomic_inc()     barrier()
144 #define smp_mb__after_atomic_inc()      barrier()
145
146 #include <asm-generic/atomic.h>
147 #endif /* __ASM_SH_ATOMIC_H */