blob: 37f2f4a55231f9c09854708b9f0b07c4f4ad2016 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __ASM_SH_ATOMIC_H
2#define __ASM_SH_ATOMIC_H
3
4/*
5 * Atomic operations that C can't guarantee us. Useful for
6 * resource counting etc..
7 *
8 */
9
Matthew Wilcoxea4354672009-01-06 14:40:39 -080010#include <linux/compiler.h>
11#include <linux/types.h>
David Howellse839ca52012-03-28 18:30:03 +010012#include <asm/cmpxchg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#define ATOMIC_INIT(i) ( (atomic_t) { (i) } )
15
Anton Blanchardf3d46f92010-05-17 14:33:53 +100016#define atomic_read(v) (*(volatile int *)&(v)->counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#define atomic_set(v,i) ((v)->counter = (i))
18
Stuart Menefy1efe4ce2007-11-30 16:12:36 +090019#if defined(CONFIG_GUSA_RB)
20#include <asm/atomic-grb.h>
21#elif defined(CONFIG_CPU_SH4A)
Paul Mundtec723fbe2006-12-07 20:33:38 +090022#include <asm/atomic-llsc.h>
Paul Mundt781125c2006-09-27 17:52:19 +090023#else
Paul Mundtec723fbe2006-12-07 20:33:38 +090024#include <asm/atomic-irq.h>
Paul Mundt781125c2006-09-27 17:52:19 +090025#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
Paul Mundt8c0b8132010-01-08 17:02:17 +090028#define atomic_dec_return(v) atomic_sub_return(1, (v))
29#define atomic_inc_return(v) atomic_add_return(1, (v))
30#define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
31#define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
32#define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Paul Mundt8c0b8132010-01-08 17:02:17 +090034#define atomic_inc(v) atomic_add(1, (v))
35#define atomic_dec(v) atomic_sub(1, (v))
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Paul Mundt8c0b8132010-01-08 17:02:17 +090037#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
38#define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
39
40/**
Arun Sharmaf24219b2011-07-26 16:09:07 -070041 * __atomic_add_unless - add unless the number is a given value
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * @v: pointer of type atomic_t
Paul Mundt8c0b8132010-01-08 17:02:17 +090043 * @a: the amount to add to v...
44 * @u: ...unless v is equal to u.
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 *
Paul Mundt8c0b8132010-01-08 17:02:17 +090046 * Atomically adds @a to @v, so long as it was not @u.
Arun Sharmaf24219b2011-07-26 16:09:07 -070047 * Returns the old value of @v.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
Arun Sharmaf24219b2011-07-26 16:09:07 -070049static inline int __atomic_add_unless(atomic_t *v, int a, int u)
Nick Piggin8426e1f2005-11-13 16:07:25 -080050{
Paul Mundt8c0b8132010-01-08 17:02:17 +090051 int c, old;
52 c = atomic_read(v);
53 for (;;) {
54 if (unlikely(c == (u)))
55 break;
56 old = atomic_cmpxchg((v), c, c + (a));
57 if (likely(old == c))
58 break;
59 c = old;
60 }
Nick Piggin8426e1f2005-11-13 16:07:25 -080061
Arun Sharmaf24219b2011-07-26 16:09:07 -070062 return c;
Nick Piggin8426e1f2005-11-13 16:07:25 -080063}
Nick Piggin8426e1f2005-11-13 16:07:25 -080064
Paul Mundt1c8db712009-10-18 15:36:02 +090065#define smp_mb__before_atomic_dec() smp_mb()
66#define smp_mb__after_atomic_dec() smp_mb()
67#define smp_mb__before_atomic_inc() smp_mb()
68#define smp_mb__after_atomic_inc() smp_mb()
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#endif /* __ASM_SH_ATOMIC_H */