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/spr-regs.h>
19 #include <asm/cmpxchg.h>
20 #include <asm/barrier.h>
27 * Atomic operations that C can't guarantee us. Useful for
28 * resource counting etc..
30 * We do not have SMP systems, so we don't have to deal with that.
33 #define ATOMIC_INIT(i) { (i) }
34 #define atomic_read(v) ACCESS_ONCE((v)->counter)
35 #define atomic_set(v, i) (((v)->counter) = (i))
37 #ifndef CONFIG_FRV_OUTOFLINE_ATOMIC_OPS
38 static inline int atomic_add_return(int i, atomic_t *v)
43 " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
45 " ld.p %M0,%1 \n" /* LD.P/ORCR must be atomic */
46 " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
48 " cst.p %1,%M0 ,cc3,#1 \n"
49 " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* clear ICC3.Z if store happens */
51 : "+U"(v->counter), "=&r"(val)
53 : "memory", "cc7", "cc3", "icc3"
59 static inline int atomic_sub_return(int i, atomic_t *v)
64 " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
66 " ld.p %M0,%1 \n" /* LD.P/ORCR must be atomic */
67 " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
69 " cst.p %1,%M0 ,cc3,#1 \n"
70 " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* clear ICC3.Z if store happens */
72 : "+U"(v->counter), "=&r"(val)
74 : "memory", "cc7", "cc3", "icc3"
82 extern int atomic_add_return(int i, atomic_t *v);
83 extern int atomic_sub_return(int i, atomic_t *v);
87 static inline int atomic_add_negative(int i, atomic_t *v)
89 return atomic_add_return(i, v) < 0;
92 static inline void atomic_add(int i, atomic_t *v)
94 atomic_add_return(i, v);
97 static inline void atomic_sub(int i, atomic_t *v)
99 atomic_sub_return(i, v);
102 static inline void atomic_inc(atomic_t *v)
104 atomic_add_return(1, v);
107 static inline void atomic_dec(atomic_t *v)
109 atomic_sub_return(1, v);
112 #define atomic_dec_return(v) atomic_sub_return(1, (v))
113 #define atomic_inc_return(v) atomic_add_return(1, (v))
115 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
116 #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
117 #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
123 volatile long long counter;
126 #define ATOMIC64_INIT(i) { (i) }
128 static inline long long atomic64_read(atomic64_t *v)
138 static inline void atomic64_set(atomic64_t *v, long long i)
140 asm volatile("std%I0 %1,%M0"
145 extern long long atomic64_inc_return(atomic64_t *v);
146 extern long long atomic64_dec_return(atomic64_t *v);
147 extern long long atomic64_add_return(long long i, atomic64_t *v);
148 extern long long atomic64_sub_return(long long i, atomic64_t *v);
150 static inline long long atomic64_add_negative(long long i, atomic64_t *v)
152 return atomic64_add_return(i, v) < 0;
155 static inline void atomic64_add(long long i, atomic64_t *v)
157 atomic64_add_return(i, v);
160 static inline void atomic64_sub(long long i, atomic64_t *v)
162 atomic64_sub_return(i, v);
165 static inline void atomic64_inc(atomic64_t *v)
167 atomic64_inc_return(v);
170 static inline void atomic64_dec(atomic64_t *v)
172 atomic64_dec_return(v);
175 #define atomic64_sub_and_test(i,v) (atomic64_sub_return((i), (v)) == 0)
176 #define atomic64_dec_and_test(v) (atomic64_dec_return((v)) == 0)
177 #define atomic64_inc_and_test(v) (atomic64_inc_return((v)) == 0)
179 #define atomic_cmpxchg(v, old, new) (cmpxchg(&(v)->counter, old, new))
180 #define atomic_xchg(v, new) (xchg(&(v)->counter, new))
181 #define atomic64_cmpxchg(v, old, new) (__cmpxchg_64(old, new, &(v)->counter))
182 #define atomic64_xchg(v, new) (__xchg_64(new, &(v)->counter))
184 static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
189 if (unlikely(c == (u)))
191 old = atomic_cmpxchg((v), c, c + (a));
192 if (likely(old == c))
200 #endif /* _ASM_ATOMIC_H */