atomic.h 8.6 KB

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