]> git.ipfire.org Git - thirdparty/linux.git/blame - arch/sh/include/asm/atomic.h
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[thirdparty/linux.git] / arch / sh / include / asm / atomic.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2#ifndef __ASM_SH_ATOMIC_H
3#define __ASM_SH_ATOMIC_H
4
2b47d54e
RF
5#if defined(CONFIG_CPU_J2)
6
7#include <asm-generic/atomic.h>
8
9#else
10
1da177e4
LT
11/*
12 * Atomic operations that C can't guarantee us. Useful for
13 * resource counting etc..
14 *
15 */
16
ea435467
MW
17#include <linux/compiler.h>
18#include <linux/types.h>
e839ca52 19#include <asm/cmpxchg.h>
603228bc 20#include <asm/barrier.h>
1da177e4 21
ec2ccd88 22#define ATOMIC_INIT(i) { (i) }
1da177e4 23
62e8a325
PZ
24#define atomic_read(v) READ_ONCE((v)->counter)
25#define atomic_set(v,i) WRITE_ONCE((v)->counter, (i))
1da177e4 26
1efe4ce3
SM
27#if defined(CONFIG_GUSA_RB)
28#include <asm/atomic-grb.h>
29#elif defined(CONFIG_CPU_SH4A)
ec723fbe 30#include <asm/atomic-llsc.h>
781125ca 31#else
ec723fbe 32#include <asm/atomic-irq.h>
781125ca 33#endif
1da177e4
LT
34
35#define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
8c0b8139
PM
36#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)
1da177e4 41
8c0b8139
PM
42#define atomic_inc(v) atomic_add(1, (v))
43#define atomic_dec(v) atomic_sub(1, (v))
1da177e4 44
8c0b8139
PM
45#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
46#define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
47
48/**
f24219b4 49 * __atomic_add_unless - add unless the number is a given value
1da177e4 50 * @v: pointer of type atomic_t
8c0b8139
PM
51 * @a: the amount to add to v...
52 * @u: ...unless v is equal to u.
1da177e4 53 *
8c0b8139 54 * Atomically adds @a to @v, so long as it was not @u.
f24219b4 55 * Returns the old value of @v.
1da177e4 56 */
f24219b4 57static inline int __atomic_add_unless(atomic_t *v, int a, int u)
8426e1f6 58{
8c0b8139
PM
59 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 }
69
f24219b4 70 return c;
8426e1f6 71}
8426e1f6 72
2b47d54e
RF
73#endif /* CONFIG_CPU_J2 */
74
1da177e4 75#endif /* __ASM_SH_ATOMIC_H */