atomic.h 7.1 KB

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