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 /* A 64bit atomic type */
110 #define ATOMIC64_INIT(val) { (val) }
113 * atomic64_read - read atomic variable
114 * @v: pointer of type atomic64_t
116 * Atomically reads the value of @v.
118 static inline long long atomic64_read(const atomic64_t *v)
121 * Requires an atomic op to read both 32-bit parts consistently.
122 * Casting away const is safe since the atomic support routines
123 * do not write to memory if the value has not been modified.
125 return _atomic64_xchg_add((long long *)&v->counter, 0);
129 * atomic64_add - add integer to atomic variable
130 * @i: integer value to add
131 * @v: pointer of type atomic64_t
133 * Atomically adds @i to @v.
135 static inline void atomic64_add(long long i, atomic64_t *v)
137 _atomic64_xchg_add(&v->counter, i);
140 #define ATOMIC64_OPS(op) \
141 long long _atomic64_fetch_##op(long long *v, long long n); \
142 static inline void atomic64_##op(long long i, atomic64_t *v) \
144 _atomic64_fetch_##op(&v->counter, i); \
146 static inline long long atomic64_fetch_##op(long long i, atomic64_t *v) \
149 return _atomic64_fetch_##op(&v->counter, i); \
158 static inline long long atomic64_fetch_add(long long i, atomic64_t *v)
161 return _atomic64_xchg_add(&v->counter, i);
165 * atomic64_add_return - add integer and return
166 * @v: pointer of type atomic64_t
167 * @i: integer value to add
169 * Atomically adds @i to @v and returns @i + @v
171 static inline long long atomic64_add_return(long long i, atomic64_t *v)
173 smp_mb(); /* barrier for proper semantics */
174 return _atomic64_xchg_add(&v->counter, i) + i;
178 * atomic64_add_unless - add unless the number is already a given value
179 * @v: pointer of type atomic64_t
180 * @a: the amount to add to v...
181 * @u: ...unless v is equal to u.
183 * Atomically adds @a to @v, so long as @v was not already @u.
184 * Returns non-zero if @v was not @u, and zero otherwise.
186 static inline long long atomic64_add_unless(atomic64_t *v, long long a,
189 smp_mb(); /* barrier for proper semantics */
190 return _atomic64_xchg_add_unless(&v->counter, a, u) != u;
194 * atomic64_set - set atomic variable
195 * @v: pointer of type atomic64_t
198 * Atomically sets the value of @v to @i.
200 * atomic64_set() can't be just a raw store, since it would be lost if it
201 * fell between the load and store of one of the other atomic ops.
203 static inline void atomic64_set(atomic64_t *v, long long n)
205 _atomic64_xchg(&v->counter, n);
208 #define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
209 #define atomic64_inc(v) atomic64_add(1LL, (v))
210 #define atomic64_inc_return(v) atomic64_add_return(1LL, (v))
211 #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
212 #define atomic64_sub_return(i, v) atomic64_add_return(-(i), (v))
213 #define atomic64_fetch_sub(i, v) atomic64_fetch_add(-(i), (v))
214 #define atomic64_sub_and_test(a, v) (atomic64_sub_return((a), (v)) == 0)
215 #define atomic64_sub(i, v) atomic64_add(-(i), (v))
216 #define atomic64_dec(v) atomic64_sub(1LL, (v))
217 #define atomic64_dec_return(v) atomic64_sub_return(1LL, (v))
218 #define atomic64_dec_and_test(v) (atomic64_dec_return((v)) == 0)
219 #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1LL, 0LL)
221 #endif /* !__ASSEMBLY__ */
224 * Internal definitions only beyond this point.
228 * Number of atomic locks in atomic_locks[]. Must be a power of two.
229 * There is no reason for more than PAGE_SIZE / 8 entries, since that
230 * is the maximum number of pointer bits we can use to index this.
231 * And we cannot have more than PAGE_SIZE / 4, since this has to
232 * fit on a single page and each entry takes 4 bytes.
234 #define ATOMIC_HASH_SHIFT (PAGE_SHIFT - 3)
235 #define ATOMIC_HASH_SIZE (1 << ATOMIC_HASH_SHIFT)
238 extern int atomic_locks[];
242 * All the code that may fault while holding an atomic lock must
243 * place the pointer to the lock in ATOMIC_LOCK_REG so the fault code
244 * can correctly release and reacquire the lock. Note that we
245 * mention the register number in a comment in "lib/atomic_asm.S" to help
246 * assembly coders from using this register by mistake, so if it
247 * is changed here, change that comment as well.
249 #define ATOMIC_LOCK_REG 20
250 #define ATOMIC_LOCK_REG_NAME r20
253 /* Called from setup to initialize a hash table to point to per_cpu locks. */
254 void __init_atomic_per_cpu(void);
257 /* Support releasing the atomic lock in do_page_fault_ics(). */
258 void __atomic_fault_unlock(int *lock_ptr);
261 /* Return a pointer to the lock for the given address. */
262 int *__atomic_hashed_lock(volatile void *v);
264 /* Private helper routines in lib/atomic_asm_32.S */
269 extern struct __get_user __atomic32_cmpxchg(volatile int *p,
270 int *lock, int o, int n);
271 extern struct __get_user __atomic32_xchg(volatile int *p, int *lock, int n);
272 extern struct __get_user __atomic32_xchg_add(volatile int *p, int *lock, int n);
273 extern struct __get_user __atomic32_xchg_add_unless(volatile int *p,
274 int *lock, int o, int n);
275 extern struct __get_user __atomic32_fetch_or(volatile int *p, int *lock, int n);
276 extern struct __get_user __atomic32_fetch_and(volatile int *p, int *lock, int n);
277 extern struct __get_user __atomic32_fetch_andn(volatile int *p, int *lock, int n);
278 extern struct __get_user __atomic32_fetch_xor(volatile int *p, int *lock, int n);
279 extern long long __atomic64_cmpxchg(volatile long long *p, int *lock,
280 long long o, long long n);
281 extern long long __atomic64_xchg(volatile long long *p, int *lock, long long n);
282 extern long long __atomic64_xchg_add(volatile long long *p, int *lock,
284 extern long long __atomic64_xchg_add_unless(volatile long long *p,
285 int *lock, long long o, long long n);
286 extern long long __atomic64_fetch_and(volatile long long *p, int *lock, long long n);
287 extern long long __atomic64_fetch_or(volatile long long *p, int *lock, long long n);
288 extern long long __atomic64_fetch_xor(volatile long long *p, int *lock, long long n);
290 /* Return failure from the atomic wrappers. */
291 struct __get_user __atomic_bad_address(int __user *addr);
293 #endif /* !__ASSEMBLY__ */
295 #endif /* _ASM_TILE_ATOMIC_32_H */