1 /* atomic.h: atomic operation emulation for FR-V
3 * For an explanation of how atomic ops work in this arch, see:
4 * Documentation/fujitsu/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/config.h>
18 #include <linux/types.h>
19 #include <asm/spr-regs.h>
26 * Atomic operations that C can't guarantee us. Useful for
27 * resource counting etc..
29 * We do not have SMP systems, so we don't have to deal with that.
32 /* Atomic operations are already serializing */
33 #define smp_mb__before_atomic_dec() barrier()
34 #define smp_mb__after_atomic_dec() barrier()
35 #define smp_mb__before_atomic_inc() barrier()
36 #define smp_mb__after_atomic_inc() barrier()
42 #define ATOMIC_INIT(i) { (i) }
43 #define atomic_read(v) ((v)->counter)
44 #define atomic_set(v, i) (((v)->counter) = (i))
46 #ifndef CONFIG_FRV_OUTOFLINE_ATOMIC_OPS
47 static inline int atomic_add_return(int i, atomic_t *v)
52 " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
54 " ld.p %M0,%1 \n" /* LD.P/ORCR must be atomic */
55 " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
57 " cst.p %1,%M0 ,cc3,#1 \n"
58 " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* clear ICC3.Z if store happens */
60 : "+U"(v->counter), "=&r"(val)
62 : "memory", "cc7", "cc3", "icc3"
68 static inline int atomic_sub_return(int i, atomic_t *v)
73 " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
75 " ld.p %M0,%1 \n" /* LD.P/ORCR must be atomic */
76 " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
78 " cst.p %1,%M0 ,cc3,#1 \n"
79 " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* clear ICC3.Z if store happens */
81 : "+U"(v->counter), "=&r"(val)
83 : "memory", "cc7", "cc3", "icc3"
91 extern int atomic_add_return(int i, atomic_t *v);
92 extern int atomic_sub_return(int i, atomic_t *v);
96 static inline int atomic_add_negative(int i, atomic_t *v)
98 return atomic_add_return(i, v) < 0;
101 static inline void atomic_add(int i, atomic_t *v)
103 atomic_add_return(i, v);
106 static inline void atomic_sub(int i, atomic_t *v)
108 atomic_sub_return(i, v);
111 static inline void atomic_inc(atomic_t *v)
113 atomic_add_return(1, v);
116 static inline void atomic_dec(atomic_t *v)
118 atomic_sub_return(1, v);
121 #define atomic_dec_return(v) atomic_sub_return(1, (v))
122 #define atomic_inc_return(v) atomic_add_return(1, (v))
124 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
125 #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
126 #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
128 #ifndef CONFIG_FRV_OUTOFLINE_ATOMIC_OPS
130 unsigned long atomic_test_and_ANDNOT_mask(unsigned long mask, volatile unsigned long *v)
132 unsigned long old, tmp;
136 " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
138 " ld.p %M0,%1 \n" /* LD.P/ORCR are atomic */
139 " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
140 " and%I3 %1,%3,%2 \n"
141 " cst.p %2,%M0 ,cc3,#1 \n" /* if store happens... */
142 " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* ... clear ICC3.Z */
144 : "+U"(*v), "=&r"(old), "=r"(tmp)
146 : "memory", "cc7", "cc3", "icc3"
153 unsigned long atomic_test_and_OR_mask(unsigned long mask, volatile unsigned long *v)
155 unsigned long old, tmp;
159 " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
161 " ld.p %M0,%1 \n" /* LD.P/ORCR are atomic */
162 " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
164 " cst.p %2,%M0 ,cc3,#1 \n" /* if store happens... */
165 " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* ... clear ICC3.Z */
167 : "+U"(*v), "=&r"(old), "=r"(tmp)
169 : "memory", "cc7", "cc3", "icc3"
176 unsigned long atomic_test_and_XOR_mask(unsigned long mask, volatile unsigned long *v)
178 unsigned long old, tmp;
182 " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
184 " ld.p %M0,%1 \n" /* LD.P/ORCR are atomic */
185 " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
186 " xor%I3 %1,%3,%2 \n"
187 " cst.p %2,%M0 ,cc3,#1 \n" /* if store happens... */
188 " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* ... clear ICC3.Z */
190 : "+U"(*v), "=&r"(old), "=r"(tmp)
192 : "memory", "cc7", "cc3", "icc3"
200 extern unsigned long atomic_test_and_ANDNOT_mask(unsigned long mask, volatile unsigned long *v);
201 extern unsigned long atomic_test_and_OR_mask(unsigned long mask, volatile unsigned long *v);
202 extern unsigned long atomic_test_and_XOR_mask(unsigned long mask, volatile unsigned long *v);
206 #define atomic_clear_mask(mask, v) atomic_test_and_ANDNOT_mask((mask), (v))
207 #define atomic_set_mask(mask, v) atomic_test_and_OR_mask((mask), (v))
209 /*****************************************************************************/
211 * exchange value with memory
213 #ifndef CONFIG_FRV_OUTOFLINE_ATOMIC_OPS
215 #define xchg(ptr, x) \
217 __typeof__(ptr) __xg_ptr = (ptr); \
218 __typeof__(*(ptr)) __xg_orig; \
220 switch (sizeof(__xg_orig)) { \
224 : "+m"(*__xg_ptr), "=&r"(__xg_orig) \
232 asm volatile("break"); \
241 extern uint32_t __xchg_32(uint32_t i, volatile void *v);
243 #define xchg(ptr, x) \
245 __typeof__(ptr) __xg_ptr = (ptr); \
246 __typeof__(*(ptr)) __xg_orig; \
248 switch (sizeof(__xg_orig)) { \
249 case 4: __xg_orig = (__typeof__(*(ptr))) __xchg_32((uint32_t) x, __xg_ptr); break; \
252 asm volatile("break"); \
260 #define tas(ptr) (xchg((ptr), 1))
262 /*****************************************************************************/
264 * compare and conditionally exchange value with memory
265 * - if (*ptr == test) then orig = *ptr; *ptr = test;
266 * - if (*ptr != test) then orig = *ptr;
268 #ifndef CONFIG_FRV_OUTOFLINE_ATOMIC_OPS
270 #define cmpxchg(ptr, test, new) \
272 __typeof__(ptr) __xg_ptr = (ptr); \
273 __typeof__(*(ptr)) __xg_orig, __xg_tmp; \
274 __typeof__(*(ptr)) __xg_test = (test); \
275 __typeof__(*(ptr)) __xg_new = (new); \
277 switch (sizeof(__xg_orig)) { \
281 " orcc gr0,gr0,gr0,icc3 \n" \
282 " ckeq icc3,cc7 \n" \
284 " orcr cc7,cc7,cc3 \n" \
285 " sub%I4cc %1,%4,%2,icc0 \n" \
286 " bne icc0,#0,1f \n" \
287 " cst.p %3,%M0 ,cc3,#1 \n" \
288 " corcc gr29,gr29,gr0 ,cc3,#1 \n" \
289 " beq icc3,#0,0b \n" \
291 : "+U"(*__xg_ptr), "=&r"(__xg_orig), "=&r"(__xg_tmp) \
292 : "r"(__xg_new), "NPr"(__xg_test) \
293 : "memory", "cc7", "cc3", "icc3", "icc0" \
299 asm volatile("break"); \
308 extern uint32_t __cmpxchg_32(uint32_t *v, uint32_t test, uint32_t new);
310 #define cmpxchg(ptr, test, new) \
312 __typeof__(ptr) __xg_ptr = (ptr); \
313 __typeof__(*(ptr)) __xg_orig; \
314 __typeof__(*(ptr)) __xg_test = (test); \
315 __typeof__(*(ptr)) __xg_new = (new); \
317 switch (sizeof(__xg_orig)) { \
318 case 4: __xg_orig = __cmpxchg_32(__xg_ptr, __xg_test, __xg_new); break; \
321 asm volatile("break"); \
330 #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), old, new))
331 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
333 #define atomic_add_unless(v, a, u) \
336 c = atomic_read(v); \
337 while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c) \
342 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
344 #include <asm-generic/atomic.h>
345 #endif /* _ASM_ATOMIC_H */