tile: Provide atomic_{or,xor,and}
[cascardo/linux.git] / arch / tile / include / asm / atomic_32.h
1 /*
2  * Copyright 2010 Tilera Corporation. All Rights Reserved.
3  *
4  *   This program is free software; you can redistribute it and/or
5  *   modify it under the terms of the GNU General Public License
6  *   as published by the Free Software Foundation, version 2.
7  *
8  *   This program is distributed in the hope that it will be useful, but
9  *   WITHOUT ANY WARRANTY; without even the implied warranty of
10  *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11  *   NON INFRINGEMENT.  See the GNU General Public License for
12  *   more details.
13  *
14  * Do not include directly; use <linux/atomic.h>.
15  */
16
17 #ifndef _ASM_TILE_ATOMIC_32_H
18 #define _ASM_TILE_ATOMIC_32_H
19
20 #include <asm/barrier.h>
21 #include <arch/chip.h>
22
23 #ifndef __ASSEMBLY__
24
25 /**
26  * atomic_add - add integer to atomic variable
27  * @i: integer value to add
28  * @v: pointer of type atomic_t
29  *
30  * Atomically adds @i to @v.
31  */
32 static inline void atomic_add(int i, atomic_t *v)
33 {
34         _atomic_xchg_add(&v->counter, i);
35 }
36
37 #define ATOMIC_OP(op)                                                   \
38 unsigned long _atomic_##op(volatile unsigned long *p, unsigned long mask); \
39 static inline void atomic_##op(int i, atomic_t *v)                      \
40 {                                                                       \
41         _atomic_##op((unsigned long *)&v->counter, i);                  \
42 }
43
44 #define CONFIG_ARCH_HAS_ATOMIC_OR
45
46 ATOMIC_OP(and)
47 ATOMIC_OP(or)
48 ATOMIC_OP(xor)
49
50 #undef ATOMIC_OP
51
52 /**
53  * atomic_add_return - add integer and return
54  * @v: pointer of type atomic_t
55  * @i: integer value to add
56  *
57  * Atomically adds @i to @v and returns @i + @v
58  */
59 static inline int atomic_add_return(int i, atomic_t *v)
60 {
61         smp_mb();  /* barrier for proper semantics */
62         return _atomic_xchg_add(&v->counter, i) + i;
63 }
64
65 /**
66  * __atomic_add_unless - add unless the number is already a given value
67  * @v: pointer of type atomic_t
68  * @a: the amount to add to v...
69  * @u: ...unless v is equal to u.
70  *
71  * Atomically adds @a to @v, so long as @v was not already @u.
72  * Returns the old value of @v.
73  */
74 static inline int __atomic_add_unless(atomic_t *v, int a, int u)
75 {
76         smp_mb();  /* barrier for proper semantics */
77         return _atomic_xchg_add_unless(&v->counter, a, u);
78 }
79
80 /**
81  * atomic_set - set atomic variable
82  * @v: pointer of type atomic_t
83  * @i: required value
84  *
85  * Atomically sets the value of @v to @i.
86  *
87  * atomic_set() can't be just a raw store, since it would be lost if it
88  * fell between the load and store of one of the other atomic ops.
89  */
90 static inline void atomic_set(atomic_t *v, int n)
91 {
92         _atomic_xchg(&v->counter, n);
93 }
94
95 /* A 64bit atomic type */
96
97 typedef struct {
98         long long counter;
99 } atomic64_t;
100
101 #define ATOMIC64_INIT(val) { (val) }
102
103 /**
104  * atomic64_read - read atomic variable
105  * @v: pointer of type atomic64_t
106  *
107  * Atomically reads the value of @v.
108  */
109 static inline long long atomic64_read(const atomic64_t *v)
110 {
111         /*
112          * Requires an atomic op to read both 32-bit parts consistently.
113          * Casting away const is safe since the atomic support routines
114          * do not write to memory if the value has not been modified.
115          */
116         return _atomic64_xchg_add((long long *)&v->counter, 0);
117 }
118
119 /**
120  * atomic64_add - add integer to atomic variable
121  * @i: integer value to add
122  * @v: pointer of type atomic64_t
123  *
124  * Atomically adds @i to @v.
125  */
126 static inline void atomic64_add(long long i, atomic64_t *v)
127 {
128         _atomic64_xchg_add(&v->counter, i);
129 }
130
131 #define ATOMIC64_OP(op)                                         \
132 long long _atomic64_##op(long long *v, long long n);            \
133 static inline void atomic64_##op(long long i, atomic64_t *v)    \
134 {                                                               \
135         _atomic64_##op(&v->counter, i);                         \
136 }
137
138 ATOMIC64_OP(and)
139 ATOMIC64_OP(or)
140 ATOMIC64_OP(xor)
141
142 /**
143  * atomic64_add_return - add integer and return
144  * @v: pointer of type atomic64_t
145  * @i: integer value to add
146  *
147  * Atomically adds @i to @v and returns @i + @v
148  */
149 static inline long long atomic64_add_return(long long i, atomic64_t *v)
150 {
151         smp_mb();  /* barrier for proper semantics */
152         return _atomic64_xchg_add(&v->counter, i) + i;
153 }
154
155 /**
156  * atomic64_add_unless - add unless the number is already a given value
157  * @v: pointer of type atomic64_t
158  * @a: the amount to add to v...
159  * @u: ...unless v is equal to u.
160  *
161  * Atomically adds @a to @v, so long as @v was not already @u.
162  * Returns non-zero if @v was not @u, and zero otherwise.
163  */
164 static inline long long atomic64_add_unless(atomic64_t *v, long long a,
165                                         long long u)
166 {
167         smp_mb();  /* barrier for proper semantics */
168         return _atomic64_xchg_add_unless(&v->counter, a, u) != u;
169 }
170
171 /**
172  * atomic64_set - set atomic variable
173  * @v: pointer of type atomic64_t
174  * @i: required value
175  *
176  * Atomically sets the value of @v to @i.
177  *
178  * atomic64_set() can't be just a raw store, since it would be lost if it
179  * fell between the load and store of one of the other atomic ops.
180  */
181 static inline void atomic64_set(atomic64_t *v, long long n)
182 {
183         _atomic64_xchg(&v->counter, n);
184 }
185
186 #define atomic64_add_negative(a, v)     (atomic64_add_return((a), (v)) < 0)
187 #define atomic64_inc(v)                 atomic64_add(1LL, (v))
188 #define atomic64_inc_return(v)          atomic64_add_return(1LL, (v))
189 #define atomic64_inc_and_test(v)        (atomic64_inc_return(v) == 0)
190 #define atomic64_sub_return(i, v)       atomic64_add_return(-(i), (v))
191 #define atomic64_sub_and_test(a, v)     (atomic64_sub_return((a), (v)) == 0)
192 #define atomic64_sub(i, v)              atomic64_add(-(i), (v))
193 #define atomic64_dec(v)                 atomic64_sub(1LL, (v))
194 #define atomic64_dec_return(v)          atomic64_sub_return(1LL, (v))
195 #define atomic64_dec_and_test(v)        (atomic64_dec_return((v)) == 0)
196 #define atomic64_inc_not_zero(v)        atomic64_add_unless((v), 1LL, 0LL)
197
198
199 #endif /* !__ASSEMBLY__ */
200
201 /*
202  * Internal definitions only beyond this point.
203  */
204
205 /*
206  * Number of atomic locks in atomic_locks[]. Must be a power of two.
207  * There is no reason for more than PAGE_SIZE / 8 entries, since that
208  * is the maximum number of pointer bits we can use to index this.
209  * And we cannot have more than PAGE_SIZE / 4, since this has to
210  * fit on a single page and each entry takes 4 bytes.
211  */
212 #define ATOMIC_HASH_SHIFT (PAGE_SHIFT - 3)
213 #define ATOMIC_HASH_SIZE (1 << ATOMIC_HASH_SHIFT)
214
215 #ifndef __ASSEMBLY__
216 extern int atomic_locks[];
217 #endif
218
219 /*
220  * All the code that may fault while holding an atomic lock must
221  * place the pointer to the lock in ATOMIC_LOCK_REG so the fault code
222  * can correctly release and reacquire the lock.  Note that we
223  * mention the register number in a comment in "lib/atomic_asm.S" to help
224  * assembly coders from using this register by mistake, so if it
225  * is changed here, change that comment as well.
226  */
227 #define ATOMIC_LOCK_REG 20
228 #define ATOMIC_LOCK_REG_NAME r20
229
230 #ifndef __ASSEMBLY__
231 /* Called from setup to initialize a hash table to point to per_cpu locks. */
232 void __init_atomic_per_cpu(void);
233
234 #ifdef CONFIG_SMP
235 /* Support releasing the atomic lock in do_page_fault_ics(). */
236 void __atomic_fault_unlock(int *lock_ptr);
237 #endif
238
239 /* Return a pointer to the lock for the given address. */
240 int *__atomic_hashed_lock(volatile void *v);
241
242 /* Private helper routines in lib/atomic_asm_32.S */
243 struct __get_user {
244         unsigned long val;
245         int err;
246 };
247 extern struct __get_user __atomic_cmpxchg(volatile int *p,
248                                           int *lock, int o, int n);
249 extern struct __get_user __atomic_xchg(volatile int *p, int *lock, int n);
250 extern struct __get_user __atomic_xchg_add(volatile int *p, int *lock, int n);
251 extern struct __get_user __atomic_xchg_add_unless(volatile int *p,
252                                                   int *lock, int o, int n);
253 extern struct __get_user __atomic_or(volatile int *p, int *lock, int n);
254 extern struct __get_user __atomic_and(volatile int *p, int *lock, int n);
255 extern struct __get_user __atomic_andn(volatile int *p, int *lock, int n);
256 extern struct __get_user __atomic_xor(volatile int *p, int *lock, int n);
257 extern long long __atomic64_cmpxchg(volatile long long *p, int *lock,
258                                         long long o, long long n);
259 extern long long __atomic64_xchg(volatile long long *p, int *lock, long long n);
260 extern long long __atomic64_xchg_add(volatile long long *p, int *lock,
261                                         long long n);
262 extern long long __atomic64_xchg_add_unless(volatile long long *p,
263                                         int *lock, long long o, long long n);
264 extern long long __atomic64_and(volatile long long *p, int *lock, long long n);
265 extern long long __atomic64_or(volatile long long *p, int *lock, long long n);
266 extern long long __atomic64_xor(volatile long long *p, int *lock, long long n);
267
268 /* Return failure from the atomic wrappers. */
269 struct __get_user __atomic_bad_address(int __user *addr);
270
271 #endif /* !__ASSEMBLY__ */
272
273 #endif /* _ASM_TILE_ATOMIC_32_H */