atomic.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 environment.
  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. 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. static inline int atomic_sub_return(int i, atomic_t *v)
  56. {
  57. return __CS_LOOP(v, i, "sr");
  58. }
  59. #define atomic_sub(_i, _v) atomic_sub_return(_i, _v)
  60. #define atomic_sub_and_test(_i, _v) (atomic_sub_return(_i, _v) == 0)
  61. #define atomic_dec(_v) atomic_sub_return(1, _v)
  62. #define atomic_dec_return(_v) atomic_sub_return(1, _v)
  63. #define atomic_dec_and_test(_v) (atomic_sub_return(1, _v) == 0)
  64. static inline void atomic_clear_mask(unsigned long mask, atomic_t *v)
  65. {
  66. __CS_LOOP(v, ~mask, "nr");
  67. }
  68. static inline void atomic_set_mask(unsigned long mask, atomic_t *v)
  69. {
  70. __CS_LOOP(v, mask, "or");
  71. }
  72. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  73. static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
  74. {
  75. asm volatile(
  76. " cs %0,%2,%1"
  77. : "+d" (old), "=Q" (v->counter)
  78. : "d" (new), "Q" (v->counter)
  79. : "cc", "memory");
  80. return old;
  81. }
  82. static inline int __atomic_add_unless(atomic_t *v, int a, int u)
  83. {
  84. int c, old;
  85. c = atomic_read(v);
  86. for (;;) {
  87. if (unlikely(c == u))
  88. break;
  89. old = atomic_cmpxchg(v, c, c + a);
  90. if (likely(old == c))
  91. break;
  92. c = old;
  93. }
  94. return c;
  95. }
  96. #undef __CS_LOOP
  97. #define ATOMIC64_INIT(i) { (i) }
  98. #ifdef CONFIG_64BIT
  99. #define __CSG_LOOP(ptr, op_val, op_string) ({ \
  100. long long old_val, new_val; \
  101. asm volatile( \
  102. " lg %0,%2\n" \
  103. "0: lgr %1,%0\n" \
  104. op_string " %1,%3\n" \
  105. " csg %0,%1,%2\n" \
  106. " jl 0b" \
  107. : "=&d" (old_val), "=&d" (new_val), \
  108. "=Q" (((atomic_t *)(ptr))->counter) \
  109. : "d" (op_val), "Q" (((atomic_t *)(ptr))->counter) \
  110. : "cc", "memory"); \
  111. new_val; \
  112. })
  113. static inline long long atomic64_read(const atomic64_t *v)
  114. {
  115. long long c;
  116. asm volatile(
  117. " lg %0,%1\n"
  118. : "=d" (c) : "Q" (v->counter));
  119. return c;
  120. }
  121. static inline void atomic64_set(atomic64_t *v, long long i)
  122. {
  123. asm volatile(
  124. " stg %1,%0\n"
  125. : "=Q" (v->counter) : "d" (i));
  126. }
  127. static inline long long atomic64_add_return(long long i, atomic64_t *v)
  128. {
  129. return __CSG_LOOP(v, i, "agr");
  130. }
  131. static inline long long atomic64_sub_return(long long i, atomic64_t *v)
  132. {
  133. return __CSG_LOOP(v, i, "sgr");
  134. }
  135. static inline void atomic64_clear_mask(unsigned long mask, atomic64_t *v)
  136. {
  137. __CSG_LOOP(v, ~mask, "ngr");
  138. }
  139. static inline void atomic64_set_mask(unsigned long mask, atomic64_t *v)
  140. {
  141. __CSG_LOOP(v, mask, "ogr");
  142. }
  143. #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
  144. static inline long long atomic64_cmpxchg(atomic64_t *v,
  145. long long old, long long new)
  146. {
  147. asm volatile(
  148. " csg %0,%2,%1"
  149. : "+d" (old), "=Q" (v->counter)
  150. : "d" (new), "Q" (v->counter)
  151. : "cc", "memory");
  152. return old;
  153. }
  154. #undef __CSG_LOOP
  155. #else /* CONFIG_64BIT */
  156. typedef struct {
  157. long long counter;
  158. } atomic64_t;
  159. static inline long long atomic64_read(const atomic64_t *v)
  160. {
  161. register_pair rp;
  162. asm volatile(
  163. " lm %0,%N0,%1"
  164. : "=&d" (rp) : "Q" (v->counter) );
  165. return rp.pair;
  166. }
  167. static inline void atomic64_set(atomic64_t *v, long long i)
  168. {
  169. register_pair rp = {.pair = i};
  170. asm volatile(
  171. " stm %1,%N1,%0"
  172. : "=Q" (v->counter) : "d" (rp) );
  173. }
  174. static inline long long atomic64_xchg(atomic64_t *v, long long new)
  175. {
  176. register_pair rp_new = {.pair = new};
  177. register_pair rp_old;
  178. asm volatile(
  179. " lm %0,%N0,%1\n"
  180. "0: cds %0,%2,%1\n"
  181. " jl 0b\n"
  182. : "=&d" (rp_old), "=Q" (v->counter)
  183. : "d" (rp_new), "Q" (v->counter)
  184. : "cc");
  185. return rp_old.pair;
  186. }
  187. static inline long long atomic64_cmpxchg(atomic64_t *v,
  188. long long old, long long new)
  189. {
  190. register_pair rp_old = {.pair = old};
  191. register_pair rp_new = {.pair = new};
  192. asm volatile(
  193. " cds %0,%2,%1"
  194. : "+&d" (rp_old), "=Q" (v->counter)
  195. : "d" (rp_new), "Q" (v->counter)
  196. : "cc");
  197. return rp_old.pair;
  198. }
  199. static inline long long atomic64_add_return(long long i, atomic64_t *v)
  200. {
  201. long long old, new;
  202. do {
  203. old = atomic64_read(v);
  204. new = old + i;
  205. } while (atomic64_cmpxchg(v, old, new) != old);
  206. return new;
  207. }
  208. static inline long long atomic64_sub_return(long long i, atomic64_t *v)
  209. {
  210. long long old, new;
  211. do {
  212. old = atomic64_read(v);
  213. new = old - i;
  214. } while (atomic64_cmpxchg(v, old, new) != old);
  215. return new;
  216. }
  217. static inline void atomic64_set_mask(unsigned long long mask, atomic64_t *v)
  218. {
  219. long long old, new;
  220. do {
  221. old = atomic64_read(v);
  222. new = old | mask;
  223. } while (atomic64_cmpxchg(v, old, new) != old);
  224. }
  225. static inline void atomic64_clear_mask(unsigned long long mask, atomic64_t *v)
  226. {
  227. long long old, new;
  228. do {
  229. old = atomic64_read(v);
  230. new = old & mask;
  231. } while (atomic64_cmpxchg(v, old, new) != old);
  232. }
  233. #endif /* CONFIG_64BIT */
  234. static inline int atomic64_add_unless(atomic64_t *v, long long a, long long u)
  235. {
  236. long long c, old;
  237. c = atomic64_read(v);
  238. for (;;) {
  239. if (unlikely(c == u))
  240. break;
  241. old = atomic64_cmpxchg(v, c, c + a);
  242. if (likely(old == c))
  243. break;
  244. c = old;
  245. }
  246. return c != u;
  247. }
  248. static inline long long atomic64_dec_if_positive(atomic64_t *v)
  249. {
  250. long long c, old, dec;
  251. c = atomic64_read(v);
  252. for (;;) {
  253. dec = c - 1;
  254. if (unlikely(dec < 0))
  255. break;
  256. old = atomic64_cmpxchg((v), c, dec);
  257. if (likely(old == c))
  258. break;
  259. c = old;
  260. }
  261. return dec;
  262. }
  263. #define atomic64_add(_i, _v) atomic64_add_return(_i, _v)
  264. #define atomic64_add_negative(_i, _v) (atomic64_add_return(_i, _v) < 0)
  265. #define atomic64_inc(_v) atomic64_add_return(1, _v)
  266. #define atomic64_inc_return(_v) atomic64_add_return(1, _v)
  267. #define atomic64_inc_and_test(_v) (atomic64_add_return(1, _v) == 0)
  268. #define atomic64_sub(_i, _v) atomic64_sub_return(_i, _v)
  269. #define atomic64_sub_and_test(_i, _v) (atomic64_sub_return(_i, _v) == 0)
  270. #define atomic64_dec(_v) atomic64_sub_return(1, _v)
  271. #define atomic64_dec_return(_v) atomic64_sub_return(1, _v)
  272. #define atomic64_dec_and_test(_v) (atomic64_sub_return(1, _v) == 0)
  273. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
  274. #define smp_mb__before_atomic_dec() smp_mb()
  275. #define smp_mb__after_atomic_dec() smp_mb()
  276. #define smp_mb__before_atomic_inc() smp_mb()
  277. #define smp_mb__after_atomic_inc() smp_mb()
  278. #endif /* __ARCH_S390_ATOMIC__ */