atomic.h 7.0 KB

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