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_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) \
41 _atomic_##op((unsigned long *)&v->counter, i); \
44 #define CONFIG_ARCH_HAS_ATOMIC_OR
53 * atomic_add_return - add integer and return
54 * @v: pointer of type atomic_t
55 * @i: integer value to add
57 * Atomically adds @i to @v and returns @i + @v
59 static inline int atomic_add_return(int i, atomic_t *v)
61 smp_mb(); /* barrier for proper semantics */
62 return _atomic_xchg_add(&v->counter, i) + i;
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.
71 * Atomically adds @a to @v, so long as @v was not already @u.
72 * Returns the old value of @v.
74 static inline int __atomic_add_unless(atomic_t *v, int a, int u)
76 smp_mb(); /* barrier for proper semantics */
77 return _atomic_xchg_add_unless(&v->counter, a, u);
81 * atomic_set - set atomic variable
82 * @v: pointer of type atomic_t
85 * Atomically sets the value of @v to @i.
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.
90 static inline void atomic_set(atomic_t *v, int n)
92 _atomic_xchg(&v->counter, n);
95 /* A 64bit atomic type */
101 #define ATOMIC64_INIT(val) { (val) }
104 * atomic64_read - read atomic variable
105 * @v: pointer of type atomic64_t
107 * Atomically reads the value of @v.
109 static inline long long atomic64_read(const atomic64_t *v)
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.
116 return _atomic64_xchg_add((long long *)&v->counter, 0);
120 * atomic64_add - add integer to atomic variable
121 * @i: integer value to add
122 * @v: pointer of type atomic64_t
124 * Atomically adds @i to @v.
126 static inline void atomic64_add(long long i, atomic64_t *v)
128 _atomic64_xchg_add(&v->counter, i);
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) \
135 _atomic64_##op(&v->counter, i); \
143 * atomic64_add_return - add integer and return
144 * @v: pointer of type atomic64_t
145 * @i: integer value to add
147 * Atomically adds @i to @v and returns @i + @v
149 static inline long long atomic64_add_return(long long i, atomic64_t *v)
151 smp_mb(); /* barrier for proper semantics */
152 return _atomic64_xchg_add(&v->counter, i) + i;
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.
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.
164 static inline long long atomic64_add_unless(atomic64_t *v, long long a,
167 smp_mb(); /* barrier for proper semantics */
168 return _atomic64_xchg_add_unless(&v->counter, a, u) != u;
172 * atomic64_set - set atomic variable
173 * @v: pointer of type atomic64_t
176 * Atomically sets the value of @v to @i.
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.
181 static inline void atomic64_set(atomic64_t *v, long long n)
183 _atomic64_xchg(&v->counter, n);
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)
199 #endif /* !__ASSEMBLY__ */
202 * Internal definitions only beyond this point.
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.
212 #define ATOMIC_HASH_SHIFT (PAGE_SHIFT - 3)
213 #define ATOMIC_HASH_SIZE (1 << ATOMIC_HASH_SHIFT)
216 extern int atomic_locks[];
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.
227 #define ATOMIC_LOCK_REG 20
228 #define ATOMIC_LOCK_REG_NAME r20
231 /* Called from setup to initialize a hash table to point to per_cpu locks. */
232 void __init_atomic_per_cpu(void);
235 /* Support releasing the atomic lock in do_page_fault_ics(). */
236 void __atomic_fault_unlock(int *lock_ptr);
239 /* Return a pointer to the lock for the given address. */
240 int *__atomic_hashed_lock(volatile void *v);
242 /* Private helper routines in lib/atomic_asm_32.S */
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,
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);
268 /* Return failure from the atomic wrappers. */
269 struct __get_user __atomic_bad_address(int __user *addr);
271 #endif /* !__ASSEMBLY__ */
273 #endif /* _ASM_TILE_ATOMIC_32_H */