atomic.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. volatile 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. #define atomic_read(v) ((v)->counter)
  60. #define atomic_set(v,i) (((v)->counter) = (i))
  61. static __inline__ int atomic_add_return(int i, atomic_t * v)
  62. {
  63. return __CS_LOOP(v, i, "ar");
  64. }
  65. #define atomic_add(_i, _v) atomic_add_return(_i, _v)
  66. #define atomic_add_negative(_i, _v) (atomic_add_return(_i, _v) < 0)
  67. #define atomic_inc(_v) atomic_add_return(1, _v)
  68. #define atomic_inc_return(_v) atomic_add_return(1, _v)
  69. #define atomic_inc_and_test(_v) (atomic_add_return(1, _v) == 0)
  70. static __inline__ int atomic_sub_return(int i, atomic_t * v)
  71. {
  72. return __CS_LOOP(v, i, "sr");
  73. }
  74. #define atomic_sub(_i, _v) atomic_sub_return(_i, _v)
  75. #define atomic_sub_and_test(_i, _v) (atomic_sub_return(_i, _v) == 0)
  76. #define atomic_dec(_v) atomic_sub_return(1, _v)
  77. #define atomic_dec_return(_v) atomic_sub_return(1, _v)
  78. #define atomic_dec_and_test(_v) (atomic_sub_return(1, _v) == 0)
  79. static __inline__ void atomic_clear_mask(unsigned long mask, atomic_t * v)
  80. {
  81. __CS_LOOP(v, ~mask, "nr");
  82. }
  83. static __inline__ void atomic_set_mask(unsigned long mask, atomic_t * v)
  84. {
  85. __CS_LOOP(v, mask, "or");
  86. }
  87. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  88. static __inline__ int atomic_cmpxchg(atomic_t *v, int old, int new)
  89. {
  90. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
  91. asm volatile(
  92. " cs %0,%2,%1"
  93. : "+d" (old), "=Q" (v->counter)
  94. : "d" (new), "Q" (v->counter)
  95. : "cc", "memory");
  96. #else /* __GNUC__ */
  97. asm volatile(
  98. " cs %0,%3,0(%2)"
  99. : "+d" (old), "=m" (v->counter)
  100. : "a" (v), "d" (new), "m" (v->counter)
  101. : "cc", "memory");
  102. #endif /* __GNUC__ */
  103. return old;
  104. }
  105. static __inline__ int atomic_add_unless(atomic_t *v, int a, int u)
  106. {
  107. int c, old;
  108. c = atomic_read(v);
  109. for (;;) {
  110. if (unlikely(c == u))
  111. break;
  112. old = atomic_cmpxchg(v, c, c + a);
  113. if (likely(old == c))
  114. break;
  115. c = old;
  116. }
  117. return c != u;
  118. }
  119. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  120. #undef __CS_LOOP
  121. #ifdef __s390x__
  122. typedef struct {
  123. volatile long long counter;
  124. } __attribute__ ((aligned (8))) atomic64_t;
  125. #define ATOMIC64_INIT(i) { (i) }
  126. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
  127. #define __CSG_LOOP(ptr, op_val, op_string) ({ \
  128. typeof(ptr->counter) old_val, new_val; \
  129. asm volatile( \
  130. " lg %0,%2\n" \
  131. "0: lgr %1,%0\n" \
  132. op_string " %1,%3\n" \
  133. " csg %0,%1,%2\n" \
  134. " jl 0b" \
  135. : "=&d" (old_val), "=&d" (new_val), \
  136. "=Q" (((atomic_t *)(ptr))->counter) \
  137. : "d" (op_val), "Q" (((atomic_t *)(ptr))->counter) \
  138. : "cc", "memory" ); \
  139. new_val; \
  140. })
  141. #else /* __GNUC__ */
  142. #define __CSG_LOOP(ptr, op_val, op_string) ({ \
  143. typeof(ptr->counter) old_val, new_val; \
  144. asm volatile( \
  145. " lg %0,0(%3)\n" \
  146. "0: lgr %1,%0\n" \
  147. op_string " %1,%4\n" \
  148. " csg %0,%1,0(%3)\n" \
  149. " jl 0b" \
  150. : "=&d" (old_val), "=&d" (new_val), \
  151. "=m" (((atomic_t *)(ptr))->counter) \
  152. : "a" (ptr), "d" (op_val), \
  153. "m" (((atomic_t *)(ptr))->counter) \
  154. : "cc", "memory" ); \
  155. new_val; \
  156. })
  157. #endif /* __GNUC__ */
  158. #define atomic64_read(v) ((v)->counter)
  159. #define atomic64_set(v,i) (((v)->counter) = (i))
  160. static __inline__ long long atomic64_add_return(long long i, atomic64_t * v)
  161. {
  162. return __CSG_LOOP(v, i, "agr");
  163. }
  164. #define atomic64_add(_i, _v) atomic64_add_return(_i, _v)
  165. #define atomic64_add_negative(_i, _v) (atomic64_add_return(_i, _v) < 0)
  166. #define atomic64_inc(_v) atomic64_add_return(1, _v)
  167. #define atomic64_inc_return(_v) atomic64_add_return(1, _v)
  168. #define atomic64_inc_and_test(_v) (atomic64_add_return(1, _v) == 0)
  169. static __inline__ long long atomic64_sub_return(long long i, atomic64_t * v)
  170. {
  171. return __CSG_LOOP(v, i, "sgr");
  172. }
  173. #define atomic64_sub(_i, _v) atomic64_sub_return(_i, _v)
  174. #define atomic64_sub_and_test(_i, _v) (atomic64_sub_return(_i, _v) == 0)
  175. #define atomic64_dec(_v) atomic64_sub_return(1, _v)
  176. #define atomic64_dec_return(_v) atomic64_sub_return(1, _v)
  177. #define atomic64_dec_and_test(_v) (atomic64_sub_return(1, _v) == 0)
  178. static __inline__ void atomic64_clear_mask(unsigned long mask, atomic64_t * v)
  179. {
  180. __CSG_LOOP(v, ~mask, "ngr");
  181. }
  182. static __inline__ void atomic64_set_mask(unsigned long mask, atomic64_t * v)
  183. {
  184. __CSG_LOOP(v, mask, "ogr");
  185. }
  186. static __inline__ long long atomic64_cmpxchg(atomic64_t *v,
  187. long long old, long long new)
  188. {
  189. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
  190. asm volatile(
  191. " csg %0,%2,%1"
  192. : "+d" (old), "=Q" (v->counter)
  193. : "d" (new), "Q" (v->counter)
  194. : "cc", "memory");
  195. #else /* __GNUC__ */
  196. asm volatile(
  197. " csg %0,%3,0(%2)"
  198. : "+d" (old), "=m" (v->counter)
  199. : "a" (v), "d" (new), "m" (v->counter)
  200. : "cc", "memory");
  201. #endif /* __GNUC__ */
  202. return old;
  203. }
  204. static __inline__ int atomic64_add_unless(atomic64_t *v,
  205. long long a, long long u)
  206. {
  207. long long c, old;
  208. c = atomic64_read(v);
  209. for (;;) {
  210. if (unlikely(c == u))
  211. break;
  212. old = atomic64_cmpxchg(v, c, c + a);
  213. if (likely(old == c))
  214. break;
  215. c = old;
  216. }
  217. return c != u;
  218. }
  219. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
  220. #undef __CSG_LOOP
  221. #endif
  222. #define smp_mb__before_atomic_dec() smp_mb()
  223. #define smp_mb__after_atomic_dec() smp_mb()
  224. #define smp_mb__before_atomic_inc() smp_mb()
  225. #define smp_mb__after_atomic_inc() smp_mb()
  226. #include <asm-generic/atomic.h>
  227. #endif /* __KERNEL__ */
  228. #endif /* __ARCH_S390_ATOMIC__ */