atomic.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #ifndef __ARCH_S390_ATOMIC__
  2. #define __ARCH_S390_ATOMIC__
  3. #include <linux/compiler.h>
  4. /*
  5. * include/asm-s390/atomic.h
  6. *
  7. * S390 version
  8. * Copyright (C) 1999-2005 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)
  12. *
  13. * Derived from "include/asm-i386/bitops.h"
  14. * Copyright (C) 1992, Linus Torvalds
  15. *
  16. */
  17. /*
  18. * Atomic operations that C can't guarantee us. Useful for
  19. * resource counting etc..
  20. * S390 uses 'Compare And Swap' for atomicity in SMP enviroment
  21. */
  22. typedef struct {
  23. int counter;
  24. } __attribute__ ((aligned (4))) atomic_t;
  25. #define ATOMIC_INIT(i) { (i) }
  26. #ifdef __KERNEL__
  27. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
  28. #define __CS_LOOP(ptr, op_val, op_string) ({ \
  29. typeof(ptr->counter) old_val, new_val; \
  30. asm volatile( \
  31. " l %0,%2\n" \
  32. "0: lr %1,%0\n" \
  33. op_string " %1,%3\n" \
  34. " cs %0,%1,%2\n" \
  35. " jl 0b" \
  36. : "=&d" (old_val), "=&d" (new_val), \
  37. "=Q" (((atomic_t *)(ptr))->counter) \
  38. : "d" (op_val), "Q" (((atomic_t *)(ptr))->counter) \
  39. : "cc", "memory"); \
  40. new_val; \
  41. })
  42. #else /* __GNUC__ */
  43. #define __CS_LOOP(ptr, op_val, op_string) ({ \
  44. typeof(ptr->counter) old_val, new_val; \
  45. asm volatile( \
  46. " l %0,0(%3)\n" \
  47. "0: lr %1,%0\n" \
  48. op_string " %1,%4\n" \
  49. " cs %0,%1,0(%3)\n" \
  50. " jl 0b" \
  51. : "=&d" (old_val), "=&d" (new_val), \
  52. "=m" (((atomic_t *)(ptr))->counter) \
  53. : "a" (ptr), "d" (op_val), \
  54. "m" (((atomic_t *)(ptr))->counter) \
  55. : "cc", "memory"); \
  56. new_val; \
  57. })
  58. #endif /* __GNUC__ */
  59. static inline int atomic_read(const atomic_t *v)
  60. {
  61. barrier();
  62. return v->counter;
  63. }
  64. static inline void atomic_set(atomic_t *v, int i)
  65. {
  66. v->counter = i;
  67. barrier();
  68. }
  69. static __inline__ int atomic_add_return(int i, atomic_t * v)
  70. {
  71. return __CS_LOOP(v, i, "ar");
  72. }
  73. #define atomic_add(_i, _v) atomic_add_return(_i, _v)
  74. #define atomic_add_negative(_i, _v) (atomic_add_return(_i, _v) < 0)
  75. #define atomic_inc(_v) atomic_add_return(1, _v)
  76. #define atomic_inc_return(_v) atomic_add_return(1, _v)
  77. #define atomic_inc_and_test(_v) (atomic_add_return(1, _v) == 0)
  78. static __inline__ int atomic_sub_return(int i, atomic_t * v)
  79. {
  80. return __CS_LOOP(v, i, "sr");
  81. }
  82. #define atomic_sub(_i, _v) atomic_sub_return(_i, _v)
  83. #define atomic_sub_and_test(_i, _v) (atomic_sub_return(_i, _v) == 0)
  84. #define atomic_dec(_v) atomic_sub_return(1, _v)
  85. #define atomic_dec_return(_v) atomic_sub_return(1, _v)
  86. #define atomic_dec_and_test(_v) (atomic_sub_return(1, _v) == 0)
  87. static __inline__ void atomic_clear_mask(unsigned long mask, atomic_t * v)
  88. {
  89. __CS_LOOP(v, ~mask, "nr");
  90. }
  91. static __inline__ void atomic_set_mask(unsigned long mask, atomic_t * v)
  92. {
  93. __CS_LOOP(v, mask, "or");
  94. }
  95. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  96. static __inline__ int atomic_cmpxchg(atomic_t *v, int old, int new)
  97. {
  98. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
  99. asm volatile(
  100. " cs %0,%2,%1"
  101. : "+d" (old), "=Q" (v->counter)
  102. : "d" (new), "Q" (v->counter)
  103. : "cc", "memory");
  104. #else /* __GNUC__ */
  105. asm volatile(
  106. " cs %0,%3,0(%2)"
  107. : "+d" (old), "=m" (v->counter)
  108. : "a" (v), "d" (new), "m" (v->counter)
  109. : "cc", "memory");
  110. #endif /* __GNUC__ */
  111. return old;
  112. }
  113. static __inline__ int atomic_add_unless(atomic_t *v, int a, int u)
  114. {
  115. int c, old;
  116. c = atomic_read(v);
  117. for (;;) {
  118. if (unlikely(c == u))
  119. break;
  120. old = atomic_cmpxchg(v, c, c + a);
  121. if (likely(old == c))
  122. break;
  123. c = old;
  124. }
  125. return c != u;
  126. }
  127. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  128. #undef __CS_LOOP
  129. #ifdef __s390x__
  130. typedef struct {
  131. long long counter;
  132. } __attribute__ ((aligned (8))) atomic64_t;
  133. #define ATOMIC64_INIT(i) { (i) }
  134. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
  135. #define __CSG_LOOP(ptr, op_val, op_string) ({ \
  136. typeof(ptr->counter) old_val, new_val; \
  137. asm volatile( \
  138. " lg %0,%2\n" \
  139. "0: lgr %1,%0\n" \
  140. op_string " %1,%3\n" \
  141. " csg %0,%1,%2\n" \
  142. " jl 0b" \
  143. : "=&d" (old_val), "=&d" (new_val), \
  144. "=Q" (((atomic_t *)(ptr))->counter) \
  145. : "d" (op_val), "Q" (((atomic_t *)(ptr))->counter) \
  146. : "cc", "memory" ); \
  147. new_val; \
  148. })
  149. #else /* __GNUC__ */
  150. #define __CSG_LOOP(ptr, op_val, op_string) ({ \
  151. typeof(ptr->counter) old_val, new_val; \
  152. asm volatile( \
  153. " lg %0,0(%3)\n" \
  154. "0: lgr %1,%0\n" \
  155. op_string " %1,%4\n" \
  156. " csg %0,%1,0(%3)\n" \
  157. " jl 0b" \
  158. : "=&d" (old_val), "=&d" (new_val), \
  159. "=m" (((atomic_t *)(ptr))->counter) \
  160. : "a" (ptr), "d" (op_val), \
  161. "m" (((atomic_t *)(ptr))->counter) \
  162. : "cc", "memory" ); \
  163. new_val; \
  164. })
  165. #endif /* __GNUC__ */
  166. static inline long long atomic64_read(const atomic64_t *v)
  167. {
  168. barrier();
  169. return v->counter;
  170. }
  171. static inline void atomic64_set(atomic64_t *v, long long i)
  172. {
  173. v->counter = i;
  174. barrier();
  175. }
  176. static __inline__ long long atomic64_add_return(long long i, atomic64_t * v)
  177. {
  178. return __CSG_LOOP(v, i, "agr");
  179. }
  180. #define atomic64_add(_i, _v) atomic64_add_return(_i, _v)
  181. #define atomic64_add_negative(_i, _v) (atomic64_add_return(_i, _v) < 0)
  182. #define atomic64_inc(_v) atomic64_add_return(1, _v)
  183. #define atomic64_inc_return(_v) atomic64_add_return(1, _v)
  184. #define atomic64_inc_and_test(_v) (atomic64_add_return(1, _v) == 0)
  185. static __inline__ long long atomic64_sub_return(long long i, atomic64_t * v)
  186. {
  187. return __CSG_LOOP(v, i, "sgr");
  188. }
  189. #define atomic64_sub(_i, _v) atomic64_sub_return(_i, _v)
  190. #define atomic64_sub_and_test(_i, _v) (atomic64_sub_return(_i, _v) == 0)
  191. #define atomic64_dec(_v) atomic64_sub_return(1, _v)
  192. #define atomic64_dec_return(_v) atomic64_sub_return(1, _v)
  193. #define atomic64_dec_and_test(_v) (atomic64_sub_return(1, _v) == 0)
  194. static __inline__ void atomic64_clear_mask(unsigned long mask, atomic64_t * v)
  195. {
  196. __CSG_LOOP(v, ~mask, "ngr");
  197. }
  198. static __inline__ void atomic64_set_mask(unsigned long mask, atomic64_t * v)
  199. {
  200. __CSG_LOOP(v, mask, "ogr");
  201. }
  202. #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
  203. static __inline__ long long atomic64_cmpxchg(atomic64_t *v,
  204. long long old, long long new)
  205. {
  206. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
  207. asm volatile(
  208. " csg %0,%2,%1"
  209. : "+d" (old), "=Q" (v->counter)
  210. : "d" (new), "Q" (v->counter)
  211. : "cc", "memory");
  212. #else /* __GNUC__ */
  213. asm volatile(
  214. " csg %0,%3,0(%2)"
  215. : "+d" (old), "=m" (v->counter)
  216. : "a" (v), "d" (new), "m" (v->counter)
  217. : "cc", "memory");
  218. #endif /* __GNUC__ */
  219. return old;
  220. }
  221. static __inline__ int atomic64_add_unless(atomic64_t *v,
  222. long long a, long long u)
  223. {
  224. long long c, old;
  225. c = atomic64_read(v);
  226. for (;;) {
  227. if (unlikely(c == u))
  228. break;
  229. old = atomic64_cmpxchg(v, c, c + a);
  230. if (likely(old == c))
  231. break;
  232. c = old;
  233. }
  234. return c != u;
  235. }
  236. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
  237. #undef __CSG_LOOP
  238. #endif
  239. #define smp_mb__before_atomic_dec() smp_mb()
  240. #define smp_mb__after_atomic_dec() smp_mb()
  241. #define smp_mb__before_atomic_inc() smp_mb()
  242. #define smp_mb__after_atomic_inc() smp_mb()
  243. #include <asm-generic/atomic.h>
  244. #endif /* __KERNEL__ */
  245. #endif /* __ARCH_S390_ATOMIC__ */