blob: 0fd0099f43cca4fe830ef0664554280fe7d24055 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef __ASM_SH_ATOMIC_H
3#define __ASM_SH_ATOMIC_H
4
Rich Felker2b47d542016-07-28 19:21:10 +00005#if defined(CONFIG_CPU_J2)
6
7#include <asm-generic/atomic.h>
8
9#else
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011/*
12 * Atomic operations that C can't guarantee us. Useful for
13 * resource counting etc..
14 *
15 */
16
Matthew Wilcoxea4354672009-01-06 14:40:39 -080017#include <linux/compiler.h>
18#include <linux/types.h>
David Howellse839ca52012-03-28 18:30:03 +010019#include <asm/cmpxchg.h>
Peter Zijlstra603228b2014-03-13 19:00:35 +010020#include <asm/barrier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Nobuhiro Iwamatsuec2ccd82012-04-27 11:12:38 +093022#define ATOMIC_INIT(i) { (i) }
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Peter Zijlstra62e8a322015-09-18 11:13:10 +020024#define atomic_read(v) READ_ONCE((v)->counter)
25#define atomic_set(v,i) WRITE_ONCE((v)->counter, (i))
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Stuart Menefy1efe4ce2007-11-30 16:12:36 +090027#if defined(CONFIG_GUSA_RB)
28#include <asm/atomic-grb.h>
29#elif defined(CONFIG_CPU_SH4A)
Paul Mundtec723fbe2006-12-07 20:33:38 +090030#include <asm/atomic-llsc.h>
Paul Mundt781125c2006-09-27 17:52:19 +090031#else
Paul Mundtec723fbe2006-12-07 20:33:38 +090032#include <asm/atomic-irq.h>
Paul Mundt781125c2006-09-27 17:52:19 +090033#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
Paul Mundt8c0b8132010-01-08 17:02:17 +090036#define atomic_dec_return(v) atomic_sub_return(1, (v))
37#define atomic_inc_return(v) atomic_add_return(1, (v))
38#define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
39#define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
40#define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Paul Mundt8c0b8132010-01-08 17:02:17 +090042#define atomic_inc(v) atomic_add(1, (v))
43#define atomic_dec(v) atomic_sub(1, (v))
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Paul Mundt8c0b8132010-01-08 17:02:17 +090045#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
46#define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
47
48/**
Arun Sharmaf24219b2011-07-26 16:09:07 -070049 * __atomic_add_unless - add unless the number is a given value
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * @v: pointer of type atomic_t
Paul Mundt8c0b8132010-01-08 17:02:17 +090051 * @a: the amount to add to v...
52 * @u: ...unless v is equal to u.
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 *
Paul Mundt8c0b8132010-01-08 17:02:17 +090054 * Atomically adds @a to @v, so long as it was not @u.
Arun Sharmaf24219b2011-07-26 16:09:07 -070055 * Returns the old value of @v.
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 */
Arun Sharmaf24219b2011-07-26 16:09:07 -070057static inline int __atomic_add_unless(atomic_t *v, int a, int u)
Nick Piggin8426e1f2005-11-13 16:07:25 -080058{
Paul Mundt8c0b8132010-01-08 17:02:17 +090059 int c, old;
60 c = atomic_read(v);
61 for (;;) {
62 if (unlikely(c == (u)))
63 break;
64 old = atomic_cmpxchg((v), c, c + (a));
65 if (likely(old == c))
66 break;
67 c = old;
68 }
Nick Piggin8426e1f2005-11-13 16:07:25 -080069
Arun Sharmaf24219b2011-07-26 16:09:07 -070070 return c;
Nick Piggin8426e1f2005-11-13 16:07:25 -080071}
Nick Piggin8426e1f2005-11-13 16:07:25 -080072
Rich Felker2b47d542016-07-28 19:21:10 +000073#endif /* CONFIG_CPU_J2 */
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#endif /* __ASM_SH_ATOMIC_H */