1 #ifndef __ARCH_S390_ATOMIC__
2 #define __ARCH_S390_ATOMIC__
5 * include/asm-s390/atomic.h
8 * Copyright (C) 1999-2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
9 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
10 * Denis Joseph Barrow,
11 * Arnd Bergmann (arndb@de.ibm.com)
13 * Derived from "include/asm-i386/bitops.h"
14 * Copyright (C) 1992, Linus Torvalds
19 * Atomic operations that C can't guarantee us. Useful for
20 * resource counting etc..
21 * S390 uses 'Compare And Swap' for atomicity in SMP enviroment
26 } __attribute__ ((aligned (4))) atomic_t;
27 #define ATOMIC_INIT(i) { (i) }
31 #define __CS_LOOP(ptr, op_val, op_string) ({ \
32 typeof(ptr->counter) old_val, new_val; \
33 __asm__ __volatile__(" l %0,0(%3)\n" \
35 op_string " %1,%4\n" \
38 : "=&d" (old_val), "=&d" (new_val), \
39 "=m" (((atomic_t *)(ptr))->counter) \
40 : "a" (ptr), "d" (op_val), \
41 "m" (((atomic_t *)(ptr))->counter) \
45 #define atomic_read(v) ((v)->counter)
46 #define atomic_set(v,i) (((v)->counter) = (i))
48 static __inline__ void atomic_add(int i, atomic_t * v)
50 __CS_LOOP(v, i, "ar");
52 static __inline__ int atomic_add_return(int i, atomic_t * v)
54 return __CS_LOOP(v, i, "ar");
56 static __inline__ int atomic_add_negative(int i, atomic_t * v)
58 return __CS_LOOP(v, i, "ar") < 0;
60 static __inline__ void atomic_sub(int i, atomic_t * v)
62 __CS_LOOP(v, i, "sr");
64 static __inline__ int atomic_sub_return(int i, atomic_t * v)
66 return __CS_LOOP(v, i, "sr");
68 static __inline__ void atomic_inc(volatile atomic_t * v)
70 __CS_LOOP(v, 1, "ar");
72 static __inline__ int atomic_inc_return(volatile atomic_t * v)
74 return __CS_LOOP(v, 1, "ar");
77 static __inline__ int atomic_inc_and_test(volatile atomic_t * v)
79 return __CS_LOOP(v, 1, "ar") == 0;
81 static __inline__ void atomic_dec(volatile atomic_t * v)
83 __CS_LOOP(v, 1, "sr");
85 static __inline__ int atomic_dec_return(volatile atomic_t * v)
87 return __CS_LOOP(v, 1, "sr");
89 static __inline__ int atomic_dec_and_test(volatile atomic_t * v)
91 return __CS_LOOP(v, 1, "sr") == 0;
93 static __inline__ void atomic_clear_mask(unsigned long mask, atomic_t * v)
95 __CS_LOOP(v, ~mask, "nr");
97 static __inline__ void atomic_set_mask(unsigned long mask, atomic_t * v)
99 __CS_LOOP(v, mask, "or");
105 volatile long long counter;
106 } __attribute__ ((aligned (8))) atomic64_t;
107 #define ATOMIC64_INIT(i) { (i) }
109 #define __CSG_LOOP(ptr, op_val, op_string) ({ \
110 typeof(ptr->counter) old_val, new_val; \
111 __asm__ __volatile__(" lg %0,0(%3)\n" \
113 op_string " %1,%4\n" \
114 " csg %0,%1,0(%3)\n" \
116 : "=&d" (old_val), "=&d" (new_val), \
117 "=m" (((atomic_t *)(ptr))->counter) \
118 : "a" (ptr), "d" (op_val), \
119 "m" (((atomic_t *)(ptr))->counter) \
120 : "cc", "memory" ); \
123 #define atomic64_read(v) ((v)->counter)
124 #define atomic64_set(v,i) (((v)->counter) = (i))
126 static __inline__ void atomic64_add(long long i, atomic64_t * v)
128 __CSG_LOOP(v, i, "agr");
130 static __inline__ long long atomic64_add_return(long long i, atomic64_t * v)
132 return __CSG_LOOP(v, i, "agr");
134 static __inline__ long long atomic64_add_negative(long long i, atomic64_t * v)
136 return __CSG_LOOP(v, i, "agr") < 0;
138 static __inline__ void atomic64_sub(long long i, atomic64_t * v)
140 __CSG_LOOP(v, i, "sgr");
142 static __inline__ void atomic64_inc(volatile atomic64_t * v)
144 __CSG_LOOP(v, 1, "agr");
146 static __inline__ long long atomic64_inc_return(volatile atomic64_t * v)
148 return __CSG_LOOP(v, 1, "agr");
150 static __inline__ long long atomic64_inc_and_test(volatile atomic64_t * v)
152 return __CSG_LOOP(v, 1, "agr") == 0;
154 static __inline__ void atomic64_dec(volatile atomic64_t * v)
156 __CSG_LOOP(v, 1, "sgr");
158 static __inline__ long long atomic64_dec_return(volatile atomic64_t * v)
160 return __CSG_LOOP(v, 1, "sgr");
162 static __inline__ long long atomic64_dec_and_test(volatile atomic64_t * v)
164 return __CSG_LOOP(v, 1, "sgr") == 0;
166 static __inline__ void atomic64_clear_mask(unsigned long mask, atomic64_t * v)
168 __CSG_LOOP(v, ~mask, "ngr");
170 static __inline__ void atomic64_set_mask(unsigned long mask, atomic64_t * v)
172 __CSG_LOOP(v, mask, "ogr");
179 returns 0 if expected_oldval==value in *v ( swap was successful )
180 returns 1 if unsuccessful.
182 This is non-portable, use bitops or spinlocks instead!
184 static __inline__ int
185 atomic_compare_and_swap(int expected_oldval,int new_val,atomic_t *v)
189 __asm__ __volatile__(
195 : "=&d" (retval), "=m" (v->counter)
196 : "a" (v), "d" (expected_oldval) , "d" (new_val),
197 "m" (v->counter) : "cc", "memory" );
201 #define atomic_cmpxchg(v, o, n) (atomic_compare_and_swap((o), (n), &((v)->counter)))
203 #define atomic_add_unless(v, a, u) \
206 c = atomic_read(v); \
207 while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c) \
211 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
213 #define smp_mb__before_atomic_dec() smp_mb()
214 #define smp_mb__after_atomic_dec() smp_mb()
215 #define smp_mb__before_atomic_inc() smp_mb()
216 #define smp_mb__after_atomic_inc() smp_mb()
218 #endif /* __KERNEL__ */
219 #endif /* __ARCH_S390_ATOMIC__ */