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__ void atomic_inc(volatile atomic_t * v)
66 __CS_LOOP(v, 1, "ar");
68 static __inline__ int atomic_inc_return(volatile atomic_t * v)
70 return __CS_LOOP(v, 1, "ar");
72 static __inline__ int atomic_inc_and_test(volatile atomic_t * v)
74 return __CS_LOOP(v, 1, "ar") != 0;
76 static __inline__ void atomic_dec(volatile atomic_t * v)
78 __CS_LOOP(v, 1, "sr");
80 static __inline__ int atomic_dec_return(volatile atomic_t * v)
82 return __CS_LOOP(v, 1, "sr");
84 static __inline__ int atomic_dec_and_test(volatile atomic_t * v)
86 return __CS_LOOP(v, 1, "sr") == 0;
88 static __inline__ void atomic_clear_mask(unsigned long mask, atomic_t * v)
90 __CS_LOOP(v, ~mask, "nr");
92 static __inline__ void atomic_set_mask(unsigned long mask, atomic_t * v)
94 __CS_LOOP(v, mask, "or");
100 volatile long long counter;
101 } __attribute__ ((aligned (8))) atomic64_t;
102 #define ATOMIC64_INIT(i) { (i) }
104 #define __CSG_LOOP(ptr, op_val, op_string) ({ \
105 typeof(ptr->counter) old_val, new_val; \
106 __asm__ __volatile__(" lg %0,0(%3)\n" \
108 op_string " %1,%4\n" \
109 " csg %0,%1,0(%3)\n" \
111 : "=&d" (old_val), "=&d" (new_val), \
112 "=m" (((atomic_t *)(ptr))->counter) \
113 : "a" (ptr), "d" (op_val), \
114 "m" (((atomic_t *)(ptr))->counter) \
115 : "cc", "memory" ); \
118 #define atomic64_read(v) ((v)->counter)
119 #define atomic64_set(v,i) (((v)->counter) = (i))
121 static __inline__ void atomic64_add(int i, atomic64_t * v)
123 __CSG_LOOP(v, i, "agr");
125 static __inline__ long long atomic64_add_return(int i, atomic64_t * v)
127 return __CSG_LOOP(v, i, "agr");
129 static __inline__ long long atomic64_add_negative(int i, atomic64_t * v)
131 return __CSG_LOOP(v, i, "agr") < 0;
133 static __inline__ void atomic64_sub(int i, atomic64_t * v)
135 __CSG_LOOP(v, i, "sgr");
137 static __inline__ void atomic64_inc(volatile atomic64_t * v)
139 __CSG_LOOP(v, 1, "agr");
141 static __inline__ long long atomic64_inc_return(volatile atomic64_t * v)
143 return __CSG_LOOP(v, 1, "agr");
145 static __inline__ long long atomic64_inc_and_test(volatile atomic64_t * v)
147 return __CSG_LOOP(v, 1, "agr") != 0;
149 static __inline__ void atomic64_dec(volatile atomic64_t * v)
151 __CSG_LOOP(v, 1, "sgr");
153 static __inline__ long long atomic64_dec_return(volatile atomic64_t * v)
155 return __CSG_LOOP(v, 1, "sgr");
157 static __inline__ long long atomic64_dec_and_test(volatile atomic64_t * v)
159 return __CSG_LOOP(v, 1, "sgr") == 0;
161 static __inline__ void atomic64_clear_mask(unsigned long mask, atomic64_t * v)
163 __CSG_LOOP(v, ~mask, "ngr");
165 static __inline__ void atomic64_set_mask(unsigned long mask, atomic64_t * v)
167 __CSG_LOOP(v, mask, "ogr");
174 returns 0 if expected_oldval==value in *v ( swap was successful )
175 returns 1 if unsuccessful.
177 This is non-portable, use bitops or spinlocks instead!
179 static __inline__ int
180 atomic_compare_and_swap(int expected_oldval,int new_val,atomic_t *v)
184 __asm__ __volatile__(
190 : "=&d" (retval), "=m" (v->counter)
191 : "a" (v), "d" (expected_oldval) , "d" (new_val),
192 "m" (v->counter) : "cc", "memory" );
196 #define smp_mb__before_atomic_dec() smp_mb()
197 #define smp_mb__after_atomic_dec() smp_mb()
198 #define smp_mb__before_atomic_inc() smp_mb()
199 #define smp_mb__after_atomic_inc() smp_mb()
201 #endif /* __KERNEL__ */
202 #endif /* __ARCH_S390_ATOMIC__ */