2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
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.
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
14 * Do not include directly; use <linux/atomic.h>.
17 #ifndef _ASM_TILE_ATOMIC_32_H
18 #define _ASM_TILE_ATOMIC_32_H
20 #include <asm/barrier.h>
21 #include <arch/chip.h>
26 * atomic_add - add integer to atomic variable
27 * @i: integer value to add
28 * @v: pointer of type atomic_t
30 * Atomically adds @i to @v.
32 static inline void atomic_add(int i, atomic_t *v)
34 _atomic_xchg_add(&v->counter, i);
37 #define ATOMIC_OPS(op) \
38 unsigned long _atomic_fetch_##op(volatile unsigned long *p, unsigned long mask); \
39 static inline void atomic_##op(int i, atomic_t *v) \
41 _atomic_fetch_##op((unsigned long *)&v->counter, i); \
43 static inline int atomic_fetch_##op(int i, atomic_t *v) \
46 return _atomic_fetch_##op((unsigned long *)&v->counter, i); \
55 static inline int atomic_fetch_add(int i, atomic_t *v)
58 return _atomic_xchg_add(&v->counter, i);
62 * atomic_add_return - add integer and return
63 * @v: pointer of type atomic_t
64 * @i: integer value to add
66 * Atomically adds @i to @v and returns @i + @v
68 static inline int atomic_add_return(int i, atomic_t *v)
70 smp_mb(); /* barrier for proper semantics */
71 return _atomic_xchg_add(&v->counter, i) + i;
75 * __atomic_add_unless - add unless the number is already a given value
76 * @v: pointer of type atomic_t
77 * @a: the amount to add to v...
78 * @u: ...unless v is equal to u.
80 * Atomically adds @a to @v, so long as @v was not already @u.
81 * Returns the old value of @v.
83 static inline int __atomic_add_unless(atomic_t *v, int a, int u)
85 smp_mb(); /* barrier for proper semantics */
86 return _atomic_xchg_add_unless(&v->counter, a, u);
90 * atomic_set - set atomic variable
91 * @v: pointer of type atomic_t
94 * Atomically sets the value of @v to @i.
96 * atomic_set() can't be just a raw store, since it would be lost if it
97 * fell between the load and store of one of the other atomic ops.
99 static inline void atomic_set(atomic_t *v, int n)
101 _atomic_xchg(&v->counter, n);
104 #define atomic_set_release(v, i) atomic_set((v), (i))
106 /* A 64bit atomic type */
112 #define ATOMIC64_INIT(val) { (val) }
115 * atomic64_read - read atomic variable
116 * @v: pointer of type atomic64_t
118 * Atomically reads the value of @v.
120 static inline long long atomic64_read(const atomic64_t *v)
123 * Requires an atomic op to read both 32-bit parts consistently.
124 * Casting away const is safe since the atomic support routines
125 * do not write to memory if the value has not been modified.
127 return _atomic64_xchg_add((long long *)&v->counter, 0);
131 * atomic64_add - add integer to atomic variable
132 * @i: integer value to add
133 * @v: pointer of type atomic64_t
135 * Atomically adds @i to @v.
137 static inline void atomic64_add(long long i, atomic64_t *v)
139 _atomic64_xchg_add(&v->counter, i);
142 #define ATOMIC64_OPS(op) \
143 long long _atomic64_fetch_##op(long long *v, long long n); \
144 static inline void atomic64_##op(long long i, atomic64_t *v) \
146 _atomic64_fetch_##op(&v->counter, i); \
148 static inline long long atomic64_fetch_##op(long long i, atomic64_t *v) \
151 return _atomic64_fetch_##op(&v->counter, i); \
160 static inline long long atomic64_fetch_add(long long i, atomic64_t *v)
163 return _atomic64_xchg_add(&v->counter, i);
167 * atomic64_add_return - add integer and return
168 * @v: pointer of type atomic64_t
169 * @i: integer value to add
171 * Atomically adds @i to @v and returns @i + @v
173 static inline long long atomic64_add_return(long long i, atomic64_t *v)
175 smp_mb(); /* barrier for proper semantics */
176 return _atomic64_xchg_add(&v->counter, i) + i;
180 * atomic64_add_unless - add unless the number is already a given value
181 * @v: pointer of type atomic64_t
182 * @a: the amount to add to v...
183 * @u: ...unless v is equal to u.
185 * Atomically adds @a to @v, so long as @v was not already @u.
186 * Returns non-zero if @v was not @u, and zero otherwise.
188 static inline long long atomic64_add_unless(atomic64_t *v, long long a,
191 smp_mb(); /* barrier for proper semantics */
192 return _atomic64_xchg_add_unless(&v->counter, a, u) != u;
196 * atomic64_set - set atomic variable
197 * @v: pointer of type atomic64_t
200 * Atomically sets the value of @v to @i.
202 * atomic64_set() can't be just a raw store, since it would be lost if it
203 * fell between the load and store of one of the other atomic ops.
205 static inline void atomic64_set(atomic64_t *v, long long n)
207 _atomic64_xchg(&v->counter, n);
210 #define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
211 #define atomic64_inc(v) atomic64_add(1LL, (v))
212 #define atomic64_inc_return(v) atomic64_add_return(1LL, (v))
213 #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
214 #define atomic64_sub_return(i, v) atomic64_add_return(-(i), (v))
215 #define atomic64_fetch_sub(i, v) atomic64_fetch_add(-(i), (v))
216 #define atomic64_sub_and_test(a, v) (atomic64_sub_return((a), (v)) == 0)
217 #define atomic64_sub(i, v) atomic64_add(-(i), (v))
218 #define atomic64_dec(v) atomic64_sub(1LL, (v))
219 #define atomic64_dec_return(v) atomic64_sub_return(1LL, (v))
220 #define atomic64_dec_and_test(v) (atomic64_dec_return((v)) == 0)
221 #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1LL, 0LL)
223 #endif /* !__ASSEMBLY__ */
226 * Internal definitions only beyond this point.
230 * Number of atomic locks in atomic_locks[]. Must be a power of two.
231 * There is no reason for more than PAGE_SIZE / 8 entries, since that
232 * is the maximum number of pointer bits we can use to index this.
233 * And we cannot have more than PAGE_SIZE / 4, since this has to
234 * fit on a single page and each entry takes 4 bytes.
236 #define ATOMIC_HASH_SHIFT (PAGE_SHIFT - 3)
237 #define ATOMIC_HASH_SIZE (1 << ATOMIC_HASH_SHIFT)
240 extern int atomic_locks[];
244 * All the code that may fault while holding an atomic lock must
245 * place the pointer to the lock in ATOMIC_LOCK_REG so the fault code
246 * can correctly release and reacquire the lock. Note that we
247 * mention the register number in a comment in "lib/atomic_asm.S" to help
248 * assembly coders from using this register by mistake, so if it
249 * is changed here, change that comment as well.
251 #define ATOMIC_LOCK_REG 20
252 #define ATOMIC_LOCK_REG_NAME r20
255 /* Called from setup to initialize a hash table to point to per_cpu locks. */
256 void __init_atomic_per_cpu(void);
259 /* Support releasing the atomic lock in do_page_fault_ics(). */
260 void __atomic_fault_unlock(int *lock_ptr);
263 /* Return a pointer to the lock for the given address. */
264 int *__atomic_hashed_lock(volatile void *v);
266 /* Private helper routines in lib/atomic_asm_32.S */
271 extern struct __get_user __atomic32_cmpxchg(volatile int *p,
272 int *lock, int o, int n);
273 extern struct __get_user __atomic32_xchg(volatile int *p, int *lock, int n);
274 extern struct __get_user __atomic32_xchg_add(volatile int *p, int *lock, int n);
275 extern struct __get_user __atomic32_xchg_add_unless(volatile int *p,
276 int *lock, int o, int n);
277 extern struct __get_user __atomic32_fetch_or(volatile int *p, int *lock, int n);
278 extern struct __get_user __atomic32_fetch_and(volatile int *p, int *lock, int n);
279 extern struct __get_user __atomic32_fetch_andn(volatile int *p, int *lock, int n);
280 extern struct __get_user __atomic32_fetch_xor(volatile int *p, int *lock, int n);
281 extern long long __atomic64_cmpxchg(volatile long long *p, int *lock,
282 long long o, long long n);
283 extern long long __atomic64_xchg(volatile long long *p, int *lock, long long n);
284 extern long long __atomic64_xchg_add(volatile long long *p, int *lock,
286 extern long long __atomic64_xchg_add_unless(volatile long long *p,
287 int *lock, long long o, long long n);
288 extern long long __atomic64_fetch_and(volatile long long *p, int *lock, long long n);
289 extern long long __atomic64_fetch_or(volatile long long *p, int *lock, long long n);
290 extern long long __atomic64_fetch_xor(volatile long long *p, int *lock, long long n);
292 /* Return failure from the atomic wrappers. */
293 struct __get_user __atomic_bad_address(int __user *addr);
295 #endif /* !__ASSEMBLY__ */
297 #endif /* _ASM_TILE_ATOMIC_32_H */