1 /* atomic.h: atomic operation emulation for FR-V
3 * For an explanation of how atomic ops work in this arch, see:
4 * Documentation/frv/atomic-ops.txt
6 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
7 * Written by David Howells (dhowells@redhat.com)
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
17 #include <linux/types.h>
18 #include <asm/cmpxchg.h>
19 #include <asm/barrier.h>
25 #include <asm/atomic_defs.h>
28 * Atomic operations that C can't guarantee us. Useful for
29 * resource counting etc..
31 * We do not have SMP systems, so we don't have to deal with that.
34 #define ATOMIC_INIT(i) { (i) }
35 #define atomic_read(v) READ_ONCE((v)->counter)
36 #define atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
38 static inline int atomic_inc_return(atomic_t *v)
40 return __atomic_add_return(1, &v->counter);
43 static inline int atomic_dec_return(atomic_t *v)
45 return __atomic_sub_return(1, &v->counter);
48 static inline int atomic_add_return(int i, atomic_t *v)
50 return __atomic_add_return(i, &v->counter);
53 static inline int atomic_sub_return(int i, atomic_t *v)
55 return __atomic_sub_return(i, &v->counter);
58 static inline int atomic_add_negative(int i, atomic_t *v)
60 return atomic_add_return(i, v) < 0;
63 static inline void atomic_inc(atomic_t *v)
68 static inline void atomic_dec(atomic_t *v)
73 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
74 #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
75 #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
84 #define ATOMIC64_INIT(i) { (i) }
86 static inline long long atomic64_read(const atomic64_t *v)
97 static inline void atomic64_set(atomic64_t *v, long long i)
99 asm volatile("std%I0 %1,%M0"
104 static inline long long atomic64_inc_return(atomic64_t *v)
106 return __atomic64_add_return(1, &v->counter);
109 static inline long long atomic64_dec_return(atomic64_t *v)
111 return __atomic64_sub_return(1, &v->counter);
114 static inline long long atomic64_add_return(long long i, atomic64_t *v)
116 return __atomic64_add_return(i, &v->counter);
119 static inline long long atomic64_sub_return(long long i, atomic64_t *v)
121 return __atomic64_sub_return(i, &v->counter);
124 static inline long long atomic64_add_negative(long long i, atomic64_t *v)
126 return atomic64_add_return(i, v) < 0;
129 static inline void atomic64_inc(atomic64_t *v)
131 atomic64_inc_return(v);
134 static inline void atomic64_dec(atomic64_t *v)
136 atomic64_dec_return(v);
139 #define atomic64_sub_and_test(i,v) (atomic64_sub_return((i), (v)) == 0)
140 #define atomic64_dec_and_test(v) (atomic64_dec_return((v)) == 0)
141 #define atomic64_inc_and_test(v) (atomic64_inc_return((v)) == 0)
142 #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
144 #define atomic_cmpxchg(v, old, new) (cmpxchg(&(v)->counter, old, new))
145 #define atomic_xchg(v, new) (xchg(&(v)->counter, new))
146 #define atomic64_cmpxchg(v, old, new) (__cmpxchg_64(old, new, &(v)->counter))
147 #define atomic64_xchg(v, new) (__xchg_64(new, &(v)->counter))
149 static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
154 if (unlikely(c == (u)))
156 old = atomic_cmpxchg((v), c, c + (a));
157 if (likely(old == c))
164 static inline int atomic64_add_unless(atomic64_t *v, long long i, long long u)
168 c = atomic64_read(v);
170 if (unlikely(c == u))
172 old = atomic64_cmpxchg(v, c, c + i);
173 if (likely(old == c))
180 static inline long long atomic64_dec_if_positive(atomic64_t *v)
182 long long c, old, dec;
184 c = atomic64_read(v);
187 if (unlikely(dec < 0))
189 old = atomic64_cmpxchg((v), c, dec);
190 if (likely(old == c))
197 #define ATOMIC_OP(op) \
198 static inline int atomic_fetch_##op(int i, atomic_t *v) \
200 return __atomic32_fetch_##op(i, &v->counter); \
202 static inline void atomic_##op(int i, atomic_t *v) \
204 (void)__atomic32_fetch_##op(i, &v->counter); \
207 static inline long long atomic64_fetch_##op(long long i, atomic64_t *v) \
209 return __atomic64_fetch_##op(i, &v->counter); \
211 static inline void atomic64_##op(long long i, atomic64_t *v) \
213 (void)__atomic64_fetch_##op(i, &v->counter); \
224 #endif /* _ASM_ATOMIC_H */