atomic.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #ifndef __ARCH_S390_ATOMIC__
  2. #define __ARCH_S390_ATOMIC__
  3. /*
  4. * Copyright 1999,2009 IBM Corp.
  5. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
  6. * Denis Joseph Barrow,
  7. * Arnd Bergmann <arndb@de.ibm.com>,
  8. *
  9. * Atomic operations that C can't guarantee us.
  10. * Useful for resource counting etc.
  11. * s390 uses 'Compare And Swap' for atomicity in SMP enviroment.
  12. *
  13. */
  14. #include <linux/compiler.h>
  15. #include <linux/types.h>
  16. #include <asm/system.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. barrier();
  35. return v->counter;
  36. }
  37. static inline void atomic_set(atomic_t *v, int i)
  38. {
  39. v->counter = i;
  40. barrier();
  41. }
  42. static inline int atomic_add_return(int i, atomic_t *v)
  43. {
  44. return __CS_LOOP(v, i, "ar");
  45. }
  46. #define atomic_add(_i, _v) atomic_add_return(_i, _v)
  47. #define atomic_add_negative(_i, _v) (atomic_add_return(_i, _v) < 0)
  48. #define atomic_inc(_v) atomic_add_return(1, _v)
  49. #define atomic_inc_return(_v) atomic_add_return(1, _v)
  50. #define atomic_inc_and_test(_v) (atomic_add_return(1, _v) == 0)
  51. static inline int atomic_sub_return(int i, atomic_t *v)
  52. {
  53. return __CS_LOOP(v, i, "sr");
  54. }
  55. #define atomic_sub(_i, _v) atomic_sub_return(_i, _v)
  56. #define atomic_sub_and_test(_i, _v) (atomic_sub_return(_i, _v) == 0)
  57. #define atomic_dec(_v) atomic_sub_return(1, _v)
  58. #define atomic_dec_return(_v) atomic_sub_return(1, _v)
  59. #define atomic_dec_and_test(_v) (atomic_sub_return(1, _v) == 0)
  60. static inline void atomic_clear_mask(unsigned long mask, atomic_t *v)
  61. {
  62. __CS_LOOP(v, ~mask, "nr");
  63. }
  64. static inline void atomic_set_mask(unsigned long mask, atomic_t *v)
  65. {
  66. __CS_LOOP(v, mask, "or");
  67. }
  68. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  69. static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
  70. {
  71. asm volatile(
  72. " cs %0,%2,%1"
  73. : "+d" (old), "=Q" (v->counter)
  74. : "d" (new), "Q" (v->counter)
  75. : "cc", "memory");
  76. return old;
  77. }
  78. static inline int atomic_add_unless(atomic_t *v, int a, int u)
  79. {
  80. int c, old;
  81. c = atomic_read(v);
  82. for (;;) {
  83. if (unlikely(c == u))
  84. break;
  85. old = atomic_cmpxchg(v, c, c + a);
  86. if (likely(old == c))
  87. break;
  88. c = old;
  89. }
  90. return c != u;
  91. }
  92. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  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. barrier();
  113. return v->counter;
  114. }
  115. static inline void atomic64_set(atomic64_t *v, long long i)
  116. {
  117. v->counter = i;
  118. barrier();
  119. }
  120. static inline long long atomic64_add_return(long long i, atomic64_t *v)
  121. {
  122. return __CSG_LOOP(v, i, "agr");
  123. }
  124. static inline long long atomic64_sub_return(long long i, atomic64_t *v)
  125. {
  126. return __CSG_LOOP(v, i, "sgr");
  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 long long atomic64_sub_return(long long i, atomic64_t *v)
  202. {
  203. long long old, new;
  204. do {
  205. old = atomic64_read(v);
  206. new = old - i;
  207. } while (atomic64_cmpxchg(v, old, new) != old);
  208. return new;
  209. }
  210. static inline void atomic64_set_mask(unsigned long long mask, atomic64_t *v)
  211. {
  212. long long old, new;
  213. do {
  214. old = atomic64_read(v);
  215. new = old | mask;
  216. } while (atomic64_cmpxchg(v, old, new) != old);
  217. }
  218. static inline void atomic64_clear_mask(unsigned long long mask, atomic64_t *v)
  219. {
  220. long long old, new;
  221. do {
  222. old = atomic64_read(v);
  223. new = old & mask;
  224. } while (atomic64_cmpxchg(v, old, new) != old);
  225. }
  226. #endif /* CONFIG_64BIT */
  227. static inline int atomic64_add_unless(atomic64_t *v, long long a, long long u)
  228. {
  229. long long c, old;
  230. c = atomic64_read(v);
  231. for (;;) {
  232. if (unlikely(c == u))
  233. break;
  234. old = atomic64_cmpxchg(v, c, c + a);
  235. if (likely(old == c))
  236. break;
  237. c = old;
  238. }
  239. return c != u;
  240. }
  241. static inline long long atomic64_dec_if_positive(atomic64_t *v)
  242. {
  243. long long c, old, dec;
  244. c = atomic64_read(v);
  245. for (;;) {
  246. dec = c - 1;
  247. if (unlikely(dec < 0))
  248. break;
  249. old = atomic64_cmpxchg((v), c, dec);
  250. if (likely(old == c))
  251. break;
  252. c = old;
  253. }
  254. return dec;
  255. }
  256. #define atomic64_add(_i, _v) atomic64_add_return(_i, _v)
  257. #define atomic64_add_negative(_i, _v) (atomic64_add_return(_i, _v) < 0)
  258. #define atomic64_inc(_v) atomic64_add_return(1, _v)
  259. #define atomic64_inc_return(_v) atomic64_add_return(1, _v)
  260. #define atomic64_inc_and_test(_v) (atomic64_add_return(1, _v) == 0)
  261. #define atomic64_sub(_i, _v) atomic64_sub_return(_i, _v)
  262. #define atomic64_sub_and_test(_i, _v) (atomic64_sub_return(_i, _v) == 0)
  263. #define atomic64_dec(_v) atomic64_sub_return(1, _v)
  264. #define atomic64_dec_return(_v) atomic64_sub_return(1, _v)
  265. #define atomic64_dec_and_test(_v) (atomic64_sub_return(1, _v) == 0)
  266. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
  267. #define smp_mb__before_atomic_dec() smp_mb()
  268. #define smp_mb__after_atomic_dec() smp_mb()
  269. #define smp_mb__before_atomic_inc() smp_mb()
  270. #define smp_mb__after_atomic_inc() smp_mb()
  271. #include <asm-generic/atomic-long.h>
  272. #endif /* __ARCH_S390_ATOMIC__ */