blob: 6c6defc24619fbab673b6296387c1dead503158b [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>
Paul Gortmaker9e5228c2012-04-01 16:38:42 -04009#include <asm/cmpxchg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11/*
12 * Atomic operations that C can't guarantee us. Useful for
13 * resource counting etc..
14 *
15 * And probably incredibly slow on parisc. OTOH, we don't
16 * have to write any serious assembly. prumpf
17 */
18
19#ifdef CONFIG_SMP
20#include <asm/spinlock.h>
21#include <asm/cache.h> /* we use L1_CACHE_BYTES */
22
23/* Use an array of spinlocks for our atomic_ts.
24 * Hash function to index into a different SPINLOCK.
25 * Since "a" is usually an address, use one spinlock per cacheline.
26 */
27# define ATOMIC_HASH_SIZE 4
James Bottomley47e669c2009-03-22 03:58:40 +000028# define ATOMIC_HASH(a) (&(__atomic_hash[ (((unsigned long) (a))/L1_CACHE_BYTES) & (ATOMIC_HASH_SIZE-1) ]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Thomas Gleixner445c8952009-12-02 19:49:50 +010030extern arch_spinlock_t __atomic_hash[ATOMIC_HASH_SIZE] __lock_aligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070032/* Can't use raw_spin_lock_irq because of #include problems, so
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 * this is the substitute */
34#define _atomic_spin_lock_irqsave(l,f) do { \
Thomas Gleixner445c8952009-12-02 19:49:50 +010035 arch_spinlock_t *s = ATOMIC_HASH(l); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 local_irq_save(f); \
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010037 arch_spin_lock(s); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070038} while(0)
39
40#define _atomic_spin_unlock_irqrestore(l,f) do { \
Thomas Gleixner445c8952009-12-02 19:49:50 +010041 arch_spinlock_t *s = ATOMIC_HASH(l); \
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010042 arch_spin_unlock(s); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 local_irq_restore(f); \
44} while(0)
45
46
47#else
48# define _atomic_spin_lock_irqsave(l,f) do { local_irq_save(f); } while (0)
49# define _atomic_spin_unlock_irqrestore(l,f) do { local_irq_restore(f); } while (0)
50#endif
51
Matthew Wilcoxea4354672009-01-06 14:40:39 -080052/*
53 * Note that we need not lock read accesses - aligned word writes/reads
54 * are atomic, so a reader never sees inconsistent values.
Kyle McMartin2e13b312006-01-17 08:33:01 -070055 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/* It's possible to reduce all atomic operations to either
58 * __atomic_add_return, atomic_set and atomic_read (the latter
59 * is there only for consistency).
60 */
61
62static __inline__ int __atomic_add_return(int i, atomic_t *v)
63{
64 int ret;
65 unsigned long flags;
66 _atomic_spin_lock_irqsave(v, flags);
67
68 ret = (v->counter += i);
69
70 _atomic_spin_unlock_irqrestore(v, flags);
71 return ret;
72}
73
74static __inline__ void atomic_set(atomic_t *v, int i)
75{
76 unsigned long flags;
77 _atomic_spin_lock_irqsave(v, flags);
78
79 v->counter = i;
80
81 _atomic_spin_unlock_irqrestore(v, flags);
82}
83
84static __inline__ int atomic_read(const atomic_t *v)
85{
Anton Blanchardf3d46f92010-05-17 14:33:53 +100086 return (*(volatile int *)&(v)->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89/* exported interface */
Mathieu Desnoyers8ffe9d02007-05-08 00:34:26 -070090#define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
Ingo Molnarffbf6702006-01-09 15:59:17 -080091#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Nick Piggin8426e1f2005-11-13 16:07:25 -080093/**
Arun Sharmaf24219b2011-07-26 16:09:07 -070094 * __atomic_add_unless - add unless the number is a given value
Nick Piggin8426e1f2005-11-13 16:07:25 -080095 * @v: pointer of type atomic_t
96 * @a: the amount to add to v...
97 * @u: ...unless v is equal to u.
98 *
99 * Atomically adds @a to @v, so long as it was not @u.
Arun Sharmaf24219b2011-07-26 16:09:07 -0700100 * Returns the old value of @v.
Nick Piggin8426e1f2005-11-13 16:07:25 -0800101 */
Arun Sharmaf24219b2011-07-26 16:09:07 -0700102static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -0700103{
104 int c, old;
105 c = atomic_read(v);
106 for (;;) {
107 if (unlikely(c == (u)))
108 break;
109 old = atomic_cmpxchg((v), c, c + (a));
110 if (likely(old == c))
111 break;
112 c = old;
113 }
Arun Sharmaf24219b2011-07-26 16:09:07 -0700114 return c;
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -0700115}
116
Nick Piggin8426e1f2005-11-13 16:07:25 -0800117
Bastian Blank692c14a2009-04-04 20:54:26 +0000118#define atomic_add(i,v) ((void)(__atomic_add_return( (i),(v))))
119#define atomic_sub(i,v) ((void)(__atomic_add_return(-(i),(v))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#define atomic_inc(v) ((void)(__atomic_add_return( 1,(v))))
121#define atomic_dec(v) ((void)(__atomic_add_return( -1,(v))))
122
Bastian Blank692c14a2009-04-04 20:54:26 +0000123#define atomic_add_return(i,v) (__atomic_add_return( (i),(v)))
124#define atomic_sub_return(i,v) (__atomic_add_return(-(i),(v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#define atomic_inc_return(v) (__atomic_add_return( 1,(v)))
126#define atomic_dec_return(v) (__atomic_add_return( -1,(v)))
127
128#define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
129
130/*
131 * atomic_inc_and_test - increment and test
132 * @v: pointer of type atomic_t
133 *
134 * Atomically increments @v by 1
135 * and returns true if the result is zero, or false for all
136 * other cases.
137 */
138#define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
139
140#define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
141
Kyle McMartin4da9f132006-03-29 19:47:32 -0500142#define atomic_sub_and_test(i,v) (atomic_sub_return((i),(v)) == 0)
143
Kyle McMartin2e13b312006-01-17 08:33:01 -0700144#define ATOMIC_INIT(i) ((atomic_t) { (i) })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146#define smp_mb__before_atomic_dec() smp_mb()
147#define smp_mb__after_atomic_dec() smp_mb()
148#define smp_mb__before_atomic_inc() smp_mb()
149#define smp_mb__after_atomic_inc() smp_mb()
150
Helge Deller513e7ec2007-01-28 15:09:20 +0100151#ifdef CONFIG_64BIT
Kyle McMartin2e13b312006-01-17 08:33:01 -0700152
Kyle McMartin2e13b312006-01-17 08:33:01 -0700153#define ATOMIC64_INIT(i) ((atomic64_t) { (i) })
154
John David Anglin548c2102011-06-11 14:42:06 -0400155static __inline__ s64
Kyle McMartin2e13b312006-01-17 08:33:01 -0700156__atomic64_add_return(s64 i, atomic64_t *v)
157{
John David Anglin548c2102011-06-11 14:42:06 -0400158 s64 ret;
Kyle McMartin2e13b312006-01-17 08:33:01 -0700159 unsigned long flags;
160 _atomic_spin_lock_irqsave(v, flags);
161
162 ret = (v->counter += i);
163
164 _atomic_spin_unlock_irqrestore(v, flags);
165 return ret;
166}
167
168static __inline__ void
169atomic64_set(atomic64_t *v, s64 i)
170{
171 unsigned long flags;
172 _atomic_spin_lock_irqsave(v, flags);
173
174 v->counter = i;
175
176 _atomic_spin_unlock_irqrestore(v, flags);
177}
178
179static __inline__ s64
180atomic64_read(const atomic64_t *v)
181{
Anton Blanchardf3d46f92010-05-17 14:33:53 +1000182 return (*(volatile long *)&(v)->counter);
Kyle McMartin2e13b312006-01-17 08:33:01 -0700183}
184
James Bottomley47e669c2009-03-22 03:58:40 +0000185#define atomic64_add(i,v) ((void)(__atomic64_add_return( ((s64)(i)),(v))))
186#define atomic64_sub(i,v) ((void)(__atomic64_add_return(-((s64)(i)),(v))))
Kyle McMartin2e13b312006-01-17 08:33:01 -0700187#define atomic64_inc(v) ((void)(__atomic64_add_return( 1,(v))))
188#define atomic64_dec(v) ((void)(__atomic64_add_return( -1,(v))))
189
James Bottomley47e669c2009-03-22 03:58:40 +0000190#define atomic64_add_return(i,v) (__atomic64_add_return( ((s64)(i)),(v)))
191#define atomic64_sub_return(i,v) (__atomic64_add_return(-((s64)(i)),(v)))
Kyle McMartin2e13b312006-01-17 08:33:01 -0700192#define atomic64_inc_return(v) (__atomic64_add_return( 1,(v)))
193#define atomic64_dec_return(v) (__atomic64_add_return( -1,(v)))
194
195#define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
196
197#define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
198#define atomic64_dec_and_test(v) (atomic64_dec_return(v) == 0)
Kyle McMartin4da9f132006-03-29 19:47:32 -0500199#define atomic64_sub_and_test(i,v) (atomic64_sub_return((i),(v)) == 0)
Kyle McMartin2e13b312006-01-17 08:33:01 -0700200
Mathieu Desnoyers8ffe9d02007-05-08 00:34:26 -0700201/* exported interface */
202#define atomic64_cmpxchg(v, o, n) \
203 ((__typeof__((v)->counter))cmpxchg(&((v)->counter), (o), (n)))
204#define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
205
206/**
207 * atomic64_add_unless - add unless the number is a given value
208 * @v: pointer of type atomic64_t
209 * @a: the amount to add to v...
210 * @u: ...unless v is equal to u.
211 *
212 * Atomically adds @a to @v, so long as it was not @u.
Arun Sharmaf24219b2011-07-26 16:09:07 -0700213 * Returns the old value of @v.
Mathieu Desnoyers8ffe9d02007-05-08 00:34:26 -0700214 */
Mathieu Desnoyers2856f5e2007-05-08 00:34:38 -0700215static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u)
216{
217 long c, old;
218 c = atomic64_read(v);
219 for (;;) {
220 if (unlikely(c == (u)))
221 break;
222 old = atomic64_cmpxchg((v), c, c + (a));
223 if (likely(old == c))
224 break;
225 c = old;
226 }
227 return c != (u);
228}
229
Mathieu Desnoyers8ffe9d02007-05-08 00:34:26 -0700230#define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
231
Kyle McMartin64daa442009-07-02 13:10:29 -0400232#endif /* !CONFIG_64BIT */
Kyle McMartin2e13b312006-01-17 08:33:01 -0700233
Kyle McMartin2e13b312006-01-17 08:33:01 -0700234
235#endif /* _ASM_PARISC_ATOMIC_H_ */