Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / arch / metag / include / asm / atomic_lock1.h
1 #ifndef __ASM_METAG_ATOMIC_LOCK1_H
2 #define __ASM_METAG_ATOMIC_LOCK1_H
3
4 #define ATOMIC_INIT(i)  { (i) }
5
6 #include <linux/compiler.h>
7
8 #include <asm/barrier.h>
9 #include <asm/global_lock.h>
10
11 static inline int atomic_read(const atomic_t *v)
12 {
13         return (v)->counter;
14 }
15
16 /*
17  * atomic_set needs to be take the lock to protect atomic_add_unless from a
18  * possible race, as it reads the counter twice:
19  *
20  *  CPU0                               CPU1
21  *  atomic_add_unless(1, 0)
22  *    ret = v->counter (non-zero)
23  *    if (ret != u)                    v->counter = 0
24  *      v->counter += 1 (counter set to 1)
25  *
26  * Making atomic_set take the lock ensures that ordering and logical
27  * consistency is preserved.
28  */
29 static inline int atomic_set(atomic_t *v, int i)
30 {
31         unsigned long flags;
32
33         __global_lock1(flags);
34         fence();
35         v->counter = i;
36         __global_unlock1(flags);
37         return i;
38 }
39
40 #define ATOMIC_OP(op, c_op)                                             \
41 static inline void atomic_##op(int i, atomic_t *v)                      \
42 {                                                                       \
43         unsigned long flags;                                            \
44                                                                         \
45         __global_lock1(flags);                                          \
46         fence();                                                        \
47         v->counter c_op i;                                              \
48         __global_unlock1(flags);                                        \
49 }                                                                       \
50
51 #define ATOMIC_OP_RETURN(op, c_op)                                      \
52 static inline int atomic_##op##_return(int i, atomic_t *v)              \
53 {                                                                       \
54         unsigned long result;                                           \
55         unsigned long flags;                                            \
56                                                                         \
57         __global_lock1(flags);                                          \
58         result = v->counter;                                            \
59         result c_op i;                                                  \
60         fence();                                                        \
61         v->counter = result;                                            \
62         __global_unlock1(flags);                                        \
63                                                                         \
64         return result;                                                  \
65 }
66
67 #define ATOMIC_OPS(op, c_op) ATOMIC_OP(op, c_op) ATOMIC_OP_RETURN(op, c_op)
68
69 ATOMIC_OPS(add, +=)
70 ATOMIC_OPS(sub, -=)
71
72 #undef ATOMIC_OPS
73 #undef ATOMIC_OP_RETURN
74 #undef ATOMIC_OP
75
76 static inline void atomic_clear_mask(unsigned int mask, atomic_t *v)
77 {
78         unsigned long flags;
79
80         __global_lock1(flags);
81         fence();
82         v->counter &= ~mask;
83         __global_unlock1(flags);
84 }
85
86 static inline void atomic_set_mask(unsigned int mask, atomic_t *v)
87 {
88         unsigned long flags;
89
90         __global_lock1(flags);
91         fence();
92         v->counter |= mask;
93         __global_unlock1(flags);
94 }
95
96 static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
97 {
98         int ret;
99         unsigned long flags;
100
101         __global_lock1(flags);
102         ret = v->counter;
103         if (ret == old) {
104                 fence();
105                 v->counter = new;
106         }
107         __global_unlock1(flags);
108
109         return ret;
110 }
111
112 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
113
114 static inline int __atomic_add_unless(atomic_t *v, int a, int u)
115 {
116         int ret;
117         unsigned long flags;
118
119         __global_lock1(flags);
120         ret = v->counter;
121         if (ret != u) {
122                 fence();
123                 v->counter += a;
124         }
125         __global_unlock1(flags);
126
127         return ret;
128 }
129
130 static inline int atomic_sub_if_positive(int i, atomic_t *v)
131 {
132         int ret;
133         unsigned long flags;
134
135         __global_lock1(flags);
136         ret = v->counter - 1;
137         if (ret >= 0) {
138                 fence();
139                 v->counter = ret;
140         }
141         __global_unlock1(flags);
142
143         return ret;
144 }
145
146 #endif /* __ASM_METAG_ATOMIC_LOCK1_H */