blob: 3ae56073cc3d717a8ab82b78be379d6fe52fb4e3 [file] [log] [blame]
Kyle McMartin2e13b312006-01-17 08:33:01 -07001/* Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
2 * Copyright (C) 2006 Kyle McMartin <kyle@parisc-linux.org>
3 */
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#ifndef _ASM_PARISC_ATOMIC_H_
6#define _ASM_PARISC_ATOMIC_H_
7
Kyle McMartin2e13b312006-01-17 08:33:01 -07008#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
10/*
11 * Atomic operations that C can't guarantee us. Useful for
12 * resource counting etc..
13 *
14 * And probably incredibly slow on parisc. OTOH, we don't
15 * have to write any serious assembly. prumpf
16 */
17
18#ifdef CONFIG_SMP
19#include <asm/spinlock.h>
20#include <asm/cache.h> /* we use L1_CACHE_BYTES */
21
22/* Use an array of spinlocks for our atomic_ts.
23 * Hash function to index into a different SPINLOCK.
24 * Since "a" is usually an address, use one spinlock per cacheline.
25 */
26# define ATOMIC_HASH_SIZE 4
James Bottomley47e669c2009-03-22 03:58:40 +000027# define ATOMIC_HASH(a) (&(__atomic_hash[ (((unsigned long) (a))/L1_CACHE_BYTES) & (ATOMIC_HASH_SIZE-1) ]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Thomas Gleixner445c8952009-12-02 19:49:50 +010029extern arch_spinlock_t __atomic_hash[ATOMIC_HASH_SIZE] __lock_aligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070031/* Can't use raw_spin_lock_irq because of #include problems, so
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * this is the substitute */
33#define _atomic_spin_lock_irqsave(l,f) do { \
Thomas Gleixner445c8952009-12-02 19:49:50 +010034 arch_spinlock_t *s = ATOMIC_HASH(l); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 local_irq_save(f); \
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010036 arch_spin_lock(s); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070037} while(0)
38
39#define _atomic_spin_unlock_irqrestore(l,f) do { \
Thomas Gleixner445c8952009-12-02 19:49:50 +010040 arch_spinlock_t *s = ATOMIC_HASH(l); \
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010041 arch_spin_unlock(s); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 local_irq_restore(f); \
43} while(0)
44
45
46#else
47# define _atomic_spin_lock_irqsave(l,f) do { local_irq_save(f); } while (0)
48# define _atomic_spin_unlock_irqrestore(l,f) do { local_irq_restore(f); } while (0)
49#endif
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/* This should get optimized out since it's never called.
52** Or get a link error if xchg is used "wrong".
53*/
54extern void __xchg_called_with_bad_pointer(void);
55
56
57/* __xchg32/64 defined in arch/parisc/lib/bitops.c */
58extern unsigned long __xchg8(char, char *);
59extern unsigned long __xchg32(int, int *);
Helge Deller513e7ec2007-01-28 15:09:20 +010060#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070061extern unsigned long __xchg64(unsigned long, unsigned long *);
62#endif
63
64/* optimizer better get rid of switch since size is a constant */
Kyle McMartin2e13b312006-01-17 08:33:01 -070065static __inline__ unsigned long
66__xchg(unsigned long x, __volatile__ void * ptr, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 switch(size) {
Helge Deller513e7ec2007-01-28 15:09:20 +010069#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 case 8: return __xchg64(x,(unsigned long *) ptr);
71#endif
72 case 4: return __xchg32((int) x, (int *) ptr);
73 case 1: return __xchg8((char) x, (char *) ptr);
74 }
75 __xchg_called_with_bad_pointer();
76 return x;
77}
78
79
80/*
81** REVISIT - Abandoned use of LDCW in xchg() for now:
82** o need to test sizeof(*ptr) to avoid clearing adjacent bytes
Helge Deller513e7ec2007-01-28 15:09:20 +010083** o and while we are at it, could CONFIG_64BIT code use LDCD too?
Linus Torvalds1da177e2005-04-16 15:20:36 -070084**
85** if (__builtin_constant_p(x) && (x == NULL))
86** if (((unsigned long)p & 0xf) == 0)
87** return __ldcw(p);
88*/
89#define xchg(ptr,x) \
90 ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
91
92
93#define __HAVE_ARCH_CMPXCHG 1
94
95/* bug catcher for when unsupported size is used - won't link */
96extern void __cmpxchg_called_with_bad_pointer(void);
97
98/* __cmpxchg_u32/u64 defined in arch/parisc/lib/bitops.c */
99extern unsigned long __cmpxchg_u32(volatile unsigned int *m, unsigned int old, unsigned int new_);
100extern unsigned long __cmpxchg_u64(volatile unsigned long *ptr, unsigned long old, unsigned long new_);
101
102/* don't worry...optimizer will get rid of most of this */
103static __inline__ unsigned long
104__cmpxchg(volatile void *ptr, unsigned long old, unsigned long new_, int size)
105{
106 switch(size) {
Helge Deller513e7ec2007-01-28 15:09:20 +0100107#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 case 8: return __cmpxchg_u64((unsigned long *)ptr, old, new_);
109#endif
110 case 4: return __cmpxchg_u32((unsigned int *)ptr, (unsigned int) old, (unsigned int) new_);
111 }
112 __cmpxchg_called_with_bad_pointer();
113 return old;
114}
115
116#define cmpxchg(ptr,o,n) \
117 ({ \
118 __typeof__(*(ptr)) _o_ = (o); \
119 __typeof__(*(ptr)) _n_ = (n); \
120 (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
121 (unsigned long)_n_, sizeof(*(ptr))); \
122 })
123
Mathieu Desnoyersdf80c8c2008-02-07 00:16:21 -0800124#include <asm-generic/cmpxchg-local.h>
125
126static inline unsigned long __cmpxchg_local(volatile void *ptr,
127 unsigned long old,
128 unsigned long new_, int size)
129{
130 switch (size) {
131#ifdef CONFIG_64BIT
132 case 8: return __cmpxchg_u64((unsigned long *)ptr, old, new_);
133#endif
134 case 4: return __cmpxchg_u32(ptr, old, new_);
135 default:
136 return __cmpxchg_local_generic(ptr, old, new_, size);
137 }
138}
139
140/*
141 * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
142 * them available.
143 */
144#define cmpxchg_local(ptr, o, n) \
145 ((__typeof__(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(o), \
146 (unsigned long)(n), sizeof(*(ptr))))
147#ifdef CONFIG_64BIT
148#define cmpxchg64_local(ptr, o, n) \
149 ({ \
150 BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
151 cmpxchg_local((ptr), (o), (n)); \
152 })
153#else
154#define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
155#endif
156
Matthew Wilcoxea4354672009-01-06 14:40:39 -0800157/*
158 * Note that we need not lock read accesses - aligned word writes/reads
159 * are atomic, so a reader never sees inconsistent values.
Kyle McMartin2e13b312006-01-17 08:33:01 -0700160 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/* It's possible to reduce all atomic operations to either
163 * __atomic_add_return, atomic_set and atomic_read (the latter
164 * is there only for consistency).
165 */
166
167static __inline__ int __atomic_add_return(int i, atomic_t *v)
168{
169 int ret;
170 unsigned long flags;
171 _atomic_spin_lock_irqsave(v, flags);
172
173 ret = (v->counter += i);
174
175 _atomic_spin_unlock_irqrestore(v, flags);
176 return ret;
177}
178
179static __inline__ void atomic_set(atomic_t *v, int i)
180{
181 unsigned long flags;
182 _atomic_spin_lock_irqsave(v, flags);
183
184 v->counter = i;
185
186 _atomic_spin_unlock_irqrestore(v, flags);
187}
188
189static __inline__ int atomic_read(const atomic_t *v)
190{
Anton Blanchardf3d46f92010-05-17 14:33:53 +1000191 return (*(volatile int *)&(v)->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194/* exported interface */
Mathieu Desnoyers8ffe9d02007-05-08 00:34:26 -0700195#define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
Ingo Molnarffbf6702006-01-09 15:59:17 -0800196#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Nick Piggin8426e1f2005-11-13 16:07:25 -0800198/**
Arun Sharmaf24219b2011-07-26 16:09:07 -0700199 * __atomic_add_unless - add unless the number is a given value
Nick Piggin8426e1f2005-11-13 16:07:25 -0800200 * @v: pointer of type atomic_t
201 * @a: the amount to add to v...
202 * @u: ...unless v is equal to u.
203 *
204 * Atomically adds @a to @v, so long as it was not @u.
Arun Sharmaf24219b2011-07-26 16:09:07 -0700205 * Returns the old value of @v.
Nick Piggin8426e1f2005-11-13 16:07:25 -0800206 */
Arun Sharmaf24219b2011-07-26 16:09:07 -0700207static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -0700208{
209 int c, old;
210 c = atomic_read(v);
211 for (;;) {
212 if (unlikely(c == (u)))
213 break;
214 old = atomic_cmpxchg((v), c, c + (a));
215 if (likely(old == c))
216 break;
217 c = old;
218 }
Arun Sharmaf24219b2011-07-26 16:09:07 -0700219 return c;
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -0700220}
221
Nick Piggin8426e1f2005-11-13 16:07:25 -0800222
Bastian Blank692c14a2009-04-04 20:54:26 +0000223#define atomic_add(i,v) ((void)(__atomic_add_return( (i),(v))))
224#define atomic_sub(i,v) ((void)(__atomic_add_return(-(i),(v))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225#define atomic_inc(v) ((void)(__atomic_add_return( 1,(v))))
226#define atomic_dec(v) ((void)(__atomic_add_return( -1,(v))))
227
Bastian Blank692c14a2009-04-04 20:54:26 +0000228#define atomic_add_return(i,v) (__atomic_add_return( (i),(v)))
229#define atomic_sub_return(i,v) (__atomic_add_return(-(i),(v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230#define atomic_inc_return(v) (__atomic_add_return( 1,(v)))
231#define atomic_dec_return(v) (__atomic_add_return( -1,(v)))
232
233#define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
234
235/*
236 * atomic_inc_and_test - increment and test
237 * @v: pointer of type atomic_t
238 *
239 * Atomically increments @v by 1
240 * and returns true if the result is zero, or false for all
241 * other cases.
242 */
243#define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
244
245#define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
246
Kyle McMartin4da9f132006-03-29 19:47:32 -0500247#define atomic_sub_and_test(i,v) (atomic_sub_return((i),(v)) == 0)
248
Kyle McMartin2e13b312006-01-17 08:33:01 -0700249#define ATOMIC_INIT(i) ((atomic_t) { (i) })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251#define smp_mb__before_atomic_dec() smp_mb()
252#define smp_mb__after_atomic_dec() smp_mb()
253#define smp_mb__before_atomic_inc() smp_mb()
254#define smp_mb__after_atomic_inc() smp_mb()
255
Helge Deller513e7ec2007-01-28 15:09:20 +0100256#ifdef CONFIG_64BIT
Kyle McMartin2e13b312006-01-17 08:33:01 -0700257
Kyle McMartin2e13b312006-01-17 08:33:01 -0700258#define ATOMIC64_INIT(i) ((atomic64_t) { (i) })
259
John David Anglin548c2102011-06-11 14:42:06 -0400260static __inline__ s64
Kyle McMartin2e13b312006-01-17 08:33:01 -0700261__atomic64_add_return(s64 i, atomic64_t *v)
262{
John David Anglin548c2102011-06-11 14:42:06 -0400263 s64 ret;
Kyle McMartin2e13b312006-01-17 08:33:01 -0700264 unsigned long flags;
265 _atomic_spin_lock_irqsave(v, flags);
266
267 ret = (v->counter += i);
268
269 _atomic_spin_unlock_irqrestore(v, flags);
270 return ret;
271}
272
273static __inline__ void
274atomic64_set(atomic64_t *v, s64 i)
275{
276 unsigned long flags;
277 _atomic_spin_lock_irqsave(v, flags);
278
279 v->counter = i;
280
281 _atomic_spin_unlock_irqrestore(v, flags);
282}
283
284static __inline__ s64
285atomic64_read(const atomic64_t *v)
286{
Anton Blanchardf3d46f92010-05-17 14:33:53 +1000287 return (*(volatile long *)&(v)->counter);
Kyle McMartin2e13b312006-01-17 08:33:01 -0700288}
289
James Bottomley47e669c2009-03-22 03:58:40 +0000290#define atomic64_add(i,v) ((void)(__atomic64_add_return( ((s64)(i)),(v))))
291#define atomic64_sub(i,v) ((void)(__atomic64_add_return(-((s64)(i)),(v))))
Kyle McMartin2e13b312006-01-17 08:33:01 -0700292#define atomic64_inc(v) ((void)(__atomic64_add_return( 1,(v))))
293#define atomic64_dec(v) ((void)(__atomic64_add_return( -1,(v))))
294
James Bottomley47e669c2009-03-22 03:58:40 +0000295#define atomic64_add_return(i,v) (__atomic64_add_return( ((s64)(i)),(v)))
296#define atomic64_sub_return(i,v) (__atomic64_add_return(-((s64)(i)),(v)))
Kyle McMartin2e13b312006-01-17 08:33:01 -0700297#define atomic64_inc_return(v) (__atomic64_add_return( 1,(v)))
298#define atomic64_dec_return(v) (__atomic64_add_return( -1,(v)))
299
300#define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
301
302#define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
303#define atomic64_dec_and_test(v) (atomic64_dec_return(v) == 0)
Kyle McMartin4da9f132006-03-29 19:47:32 -0500304#define atomic64_sub_and_test(i,v) (atomic64_sub_return((i),(v)) == 0)
Kyle McMartin2e13b312006-01-17 08:33:01 -0700305
Mathieu Desnoyers8ffe9d02007-05-08 00:34:26 -0700306/* exported interface */
307#define atomic64_cmpxchg(v, o, n) \
308 ((__typeof__((v)->counter))cmpxchg(&((v)->counter), (o), (n)))
309#define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
310
311/**
312 * atomic64_add_unless - add unless the number is a given value
313 * @v: pointer of type atomic64_t
314 * @a: the amount to add to v...
315 * @u: ...unless v is equal to u.
316 *
317 * Atomically adds @a to @v, so long as it was not @u.
Arun Sharmaf24219b2011-07-26 16:09:07 -0700318 * Returns the old value of @v.
Mathieu Desnoyers8ffe9d02007-05-08 00:34:26 -0700319 */
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -0700320static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u)
321{
322 long c, old;
323 c = atomic64_read(v);
324 for (;;) {
325 if (unlikely(c == (u)))
326 break;
327 old = atomic64_cmpxchg((v), c, c + (a));
328 if (likely(old == c))
329 break;
330 c = old;
331 }
332 return c != (u);
333}
334
Mathieu Desnoyers8ffe9d02007-05-08 00:34:26 -0700335#define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
336
Kyle McMartin64daa442009-07-02 13:10:29 -0400337#endif /* !CONFIG_64BIT */
Kyle McMartin2e13b312006-01-17 08:33:01 -0700338
Kyle McMartin2e13b312006-01-17 08:33:01 -0700339
340#endif /* _ASM_PARISC_ATOMIC_H_ */