atomic.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
  19. #define __ATOMIC_OR "lao"
  20. #define __ATOMIC_AND "lan"
  21. #define __ATOMIC_ADD "laa"
  22. #define __ATOMIC_LOOP(ptr, op_val, op_string) \
  23. ({ \
  24. int old_val; \
  25. asm volatile( \
  26. op_string " %0,%2,%1\n" \
  27. : "=d" (old_val), "+Q" (((atomic_t *)(ptr))->counter) \
  28. : "d" (op_val) \
  29. : "cc", "memory"); \
  30. old_val; \
  31. })
  32. #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
  33. #define __ATOMIC_OR "or"
  34. #define __ATOMIC_AND "nr"
  35. #define __ATOMIC_ADD "ar"
  36. #define __ATOMIC_LOOP(ptr, op_val, op_string) \
  37. ({ \
  38. int old_val, new_val; \
  39. asm volatile( \
  40. " l %0,%2\n" \
  41. "0: lr %1,%0\n" \
  42. op_string " %1,%3\n" \
  43. " cs %0,%1,%2\n" \
  44. " jl 0b" \
  45. : "=&d" (old_val), "=&d" (new_val), \
  46. "=Q" (((atomic_t *)(ptr))->counter) \
  47. : "d" (op_val), "Q" (((atomic_t *)(ptr))->counter) \
  48. : "cc", "memory"); \
  49. old_val; \
  50. })
  51. #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
  52. static inline int atomic_read(const atomic_t *v)
  53. {
  54. int c;
  55. asm volatile(
  56. " l %0,%1\n"
  57. : "=d" (c) : "Q" (v->counter));
  58. return c;
  59. }
  60. static inline void atomic_set(atomic_t *v, int i)
  61. {
  62. asm volatile(
  63. " st %1,%0\n"
  64. : "=Q" (v->counter) : "d" (i));
  65. }
  66. static inline int atomic_add_return(int i, atomic_t *v)
  67. {
  68. return __ATOMIC_LOOP(v, i, __ATOMIC_ADD) + i;
  69. }
  70. #define atomic_add(_i, _v) atomic_add_return(_i, _v)
  71. #define atomic_add_negative(_i, _v) (atomic_add_return(_i, _v) < 0)
  72. #define atomic_inc(_v) atomic_add_return(1, _v)
  73. #define atomic_inc_return(_v) atomic_add_return(1, _v)
  74. #define atomic_inc_and_test(_v) (atomic_add_return(1, _v) == 0)
  75. #define atomic_sub_return(_i, _v) atomic_add_return(-(int)(_i), _v)
  76. #define atomic_sub(_i, _v) atomic_sub_return(_i, _v)
  77. #define atomic_sub_and_test(_i, _v) (atomic_sub_return(_i, _v) == 0)
  78. #define atomic_dec(_v) atomic_sub_return(1, _v)
  79. #define atomic_dec_return(_v) atomic_sub_return(1, _v)
  80. #define atomic_dec_and_test(_v) (atomic_sub_return(1, _v) == 0)
  81. static inline void atomic_clear_mask(unsigned long mask, atomic_t *v)
  82. {
  83. __ATOMIC_LOOP(v, ~mask, __ATOMIC_AND);
  84. }
  85. static inline void atomic_set_mask(unsigned long mask, atomic_t *v)
  86. {
  87. __ATOMIC_LOOP(v, mask, __ATOMIC_OR);
  88. }
  89. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  90. static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
  91. {
  92. asm volatile(
  93. " cs %0,%2,%1"
  94. : "+d" (old), "=Q" (v->counter)
  95. : "d" (new), "Q" (v->counter)
  96. : "cc", "memory");
  97. return old;
  98. }
  99. static inline int __atomic_add_unless(atomic_t *v, int a, int u)
  100. {
  101. int c, old;
  102. c = atomic_read(v);
  103. for (;;) {
  104. if (unlikely(c == u))
  105. break;
  106. old = atomic_cmpxchg(v, c, c + a);
  107. if (likely(old == c))
  108. break;
  109. c = old;
  110. }
  111. return c;
  112. }
  113. #undef __ATOMIC_LOOP
  114. #define ATOMIC64_INIT(i) { (i) }
  115. #ifdef CONFIG_64BIT
  116. #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
  117. #define __ATOMIC64_OR "laog"
  118. #define __ATOMIC64_AND "lang"
  119. #define __ATOMIC64_ADD "laag"
  120. #define __ATOMIC64_LOOP(ptr, op_val, op_string) \
  121. ({ \
  122. long long old_val; \
  123. asm volatile( \
  124. op_string " %0,%2,%1\n" \
  125. : "=d" (old_val), "+Q" (((atomic_t *)(ptr))->counter) \
  126. : "d" (op_val) \
  127. : "cc", "memory"); \
  128. old_val; \
  129. })
  130. #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
  131. #define __ATOMIC64_OR "ogr"
  132. #define __ATOMIC64_AND "ngr"
  133. #define __ATOMIC64_ADD "agr"
  134. #define __ATOMIC64_LOOP(ptr, op_val, op_string) \
  135. ({ \
  136. long long old_val, new_val; \
  137. asm volatile( \
  138. " lg %0,%2\n" \
  139. "0: lgr %1,%0\n" \
  140. op_string " %1,%3\n" \
  141. " csg %0,%1,%2\n" \
  142. " jl 0b" \
  143. : "=&d" (old_val), "=&d" (new_val), \
  144. "=Q" (((atomic_t *)(ptr))->counter) \
  145. : "d" (op_val), "Q" (((atomic_t *)(ptr))->counter) \
  146. : "cc", "memory"); \
  147. old_val; \
  148. })
  149. #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
  150. static inline long long atomic64_read(const atomic64_t *v)
  151. {
  152. long long c;
  153. asm volatile(
  154. " lg %0,%1\n"
  155. : "=d" (c) : "Q" (v->counter));
  156. return c;
  157. }
  158. static inline void atomic64_set(atomic64_t *v, long long i)
  159. {
  160. asm volatile(
  161. " stg %1,%0\n"
  162. : "=Q" (v->counter) : "d" (i));
  163. }
  164. static inline long long atomic64_add_return(long long i, atomic64_t *v)
  165. {
  166. return __ATOMIC64_LOOP(v, i, __ATOMIC64_ADD) + i;
  167. }
  168. static inline void atomic64_clear_mask(unsigned long mask, atomic64_t *v)
  169. {
  170. __ATOMIC64_LOOP(v, ~mask, __ATOMIC64_AND);
  171. }
  172. static inline void atomic64_set_mask(unsigned long mask, atomic64_t *v)
  173. {
  174. __ATOMIC64_LOOP(v, mask, __ATOMIC64_OR);
  175. }
  176. #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
  177. static inline long long atomic64_cmpxchg(atomic64_t *v,
  178. long long old, long long new)
  179. {
  180. asm volatile(
  181. " csg %0,%2,%1"
  182. : "+d" (old), "=Q" (v->counter)
  183. : "d" (new), "Q" (v->counter)
  184. : "cc", "memory");
  185. return old;
  186. }
  187. #undef __ATOMIC64_LOOP
  188. #else /* CONFIG_64BIT */
  189. typedef struct {
  190. long long counter;
  191. } atomic64_t;
  192. static inline long long atomic64_read(const atomic64_t *v)
  193. {
  194. register_pair rp;
  195. asm volatile(
  196. " lm %0,%N0,%1"
  197. : "=&d" (rp) : "Q" (v->counter) );
  198. return rp.pair;
  199. }
  200. static inline void atomic64_set(atomic64_t *v, long long i)
  201. {
  202. register_pair rp = {.pair = i};
  203. asm volatile(
  204. " stm %1,%N1,%0"
  205. : "=Q" (v->counter) : "d" (rp) );
  206. }
  207. static inline long long atomic64_xchg(atomic64_t *v, long long new)
  208. {
  209. register_pair rp_new = {.pair = new};
  210. register_pair rp_old;
  211. asm volatile(
  212. " lm %0,%N0,%1\n"
  213. "0: cds %0,%2,%1\n"
  214. " jl 0b\n"
  215. : "=&d" (rp_old), "=Q" (v->counter)
  216. : "d" (rp_new), "Q" (v->counter)
  217. : "cc");
  218. return rp_old.pair;
  219. }
  220. static inline long long atomic64_cmpxchg(atomic64_t *v,
  221. long long old, long long new)
  222. {
  223. register_pair rp_old = {.pair = old};
  224. register_pair rp_new = {.pair = new};
  225. asm volatile(
  226. " cds %0,%2,%1"
  227. : "+&d" (rp_old), "=Q" (v->counter)
  228. : "d" (rp_new), "Q" (v->counter)
  229. : "cc");
  230. return rp_old.pair;
  231. }
  232. static inline long long atomic64_add_return(long long i, atomic64_t *v)
  233. {
  234. long long old, new;
  235. do {
  236. old = atomic64_read(v);
  237. new = old + i;
  238. } while (atomic64_cmpxchg(v, old, new) != old);
  239. return new;
  240. }
  241. static inline void atomic64_set_mask(unsigned long long mask, atomic64_t *v)
  242. {
  243. long long old, new;
  244. do {
  245. old = atomic64_read(v);
  246. new = old | mask;
  247. } while (atomic64_cmpxchg(v, old, new) != old);
  248. }
  249. static inline void atomic64_clear_mask(unsigned long long mask, atomic64_t *v)
  250. {
  251. long long old, new;
  252. do {
  253. old = atomic64_read(v);
  254. new = old & mask;
  255. } while (atomic64_cmpxchg(v, old, new) != old);
  256. }
  257. #endif /* CONFIG_64BIT */
  258. static inline int atomic64_add_unless(atomic64_t *v, long long a, long long u)
  259. {
  260. long long c, old;
  261. c = atomic64_read(v);
  262. for (;;) {
  263. if (unlikely(c == u))
  264. break;
  265. old = atomic64_cmpxchg(v, c, c + a);
  266. if (likely(old == c))
  267. break;
  268. c = old;
  269. }
  270. return c != u;
  271. }
  272. static inline long long atomic64_dec_if_positive(atomic64_t *v)
  273. {
  274. long long c, old, dec;
  275. c = atomic64_read(v);
  276. for (;;) {
  277. dec = c - 1;
  278. if (unlikely(dec < 0))
  279. break;
  280. old = atomic64_cmpxchg((v), c, dec);
  281. if (likely(old == c))
  282. break;
  283. c = old;
  284. }
  285. return dec;
  286. }
  287. #define atomic64_add(_i, _v) atomic64_add_return(_i, _v)
  288. #define atomic64_add_negative(_i, _v) (atomic64_add_return(_i, _v) < 0)
  289. #define atomic64_inc(_v) atomic64_add_return(1, _v)
  290. #define atomic64_inc_return(_v) atomic64_add_return(1, _v)
  291. #define atomic64_inc_and_test(_v) (atomic64_add_return(1, _v) == 0)
  292. #define atomic64_sub_return(_i, _v) atomic64_add_return(-(long long)(_i), _v)
  293. #define atomic64_sub(_i, _v) atomic64_sub_return(_i, _v)
  294. #define atomic64_sub_and_test(_i, _v) (atomic64_sub_return(_i, _v) == 0)
  295. #define atomic64_dec(_v) atomic64_sub_return(1, _v)
  296. #define atomic64_dec_return(_v) atomic64_sub_return(1, _v)
  297. #define atomic64_dec_and_test(_v) (atomic64_sub_return(1, _v) == 0)
  298. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
  299. #define smp_mb__before_atomic_dec() smp_mb()
  300. #define smp_mb__after_atomic_dec() smp_mb()
  301. #define smp_mb__before_atomic_inc() smp_mb()
  302. #define smp_mb__after_atomic_inc() smp_mb()
  303. #endif /* __ARCH_S390_ATOMIC__ */