atomic.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ALPHA_ATOMIC_H
  3. #define _ALPHA_ATOMIC_H
  4. #include <linux/types.h>
  5. #include <asm/barrier.h>
  6. #include <asm/cmpxchg.h>
  7. /*
  8. * Atomic operations that C can't guarantee us. Useful for
  9. * resource counting etc...
  10. *
  11. * But use these as seldom as possible since they are much slower
  12. * than regular operations.
  13. */
  14. /*
  15. * To ensure dependency ordering is preserved for the _relaxed and
  16. * _release atomics, an smp_read_barrier_depends() is unconditionally
  17. * inserted into the _relaxed variants, which are used to build the
  18. * barriered versions. Avoid redundant back-to-back fences in the
  19. * _acquire and _fence versions.
  20. */
  21. #define __atomic_acquire_fence()
  22. #define __atomic_post_full_fence()
  23. #define ATOMIC_INIT(i) { (i) }
  24. #define ATOMIC64_INIT(i) { (i) }
  25. #define atomic_read(v) READ_ONCE((v)->counter)
  26. #define atomic64_read(v) READ_ONCE((v)->counter)
  27. #define atomic_set(v,i) WRITE_ONCE((v)->counter, (i))
  28. #define atomic64_set(v,i) WRITE_ONCE((v)->counter, (i))
  29. /*
  30. * To get proper branch prediction for the main line, we must branch
  31. * forward to code at the end of this object's .text section, then
  32. * branch back to restart the operation.
  33. */
  34. #define ATOMIC_OP(op, asm_op) \
  35. static __inline__ void atomic_##op(int i, atomic_t * v) \
  36. { \
  37. unsigned long temp; \
  38. __asm__ __volatile__( \
  39. "1: ldl_l %0,%1\n" \
  40. " " #asm_op " %0,%2,%0\n" \
  41. " stl_c %0,%1\n" \
  42. " beq %0,2f\n" \
  43. ".subsection 2\n" \
  44. "2: br 1b\n" \
  45. ".previous" \
  46. :"=&r" (temp), "=m" (v->counter) \
  47. :"Ir" (i), "m" (v->counter)); \
  48. } \
  49. #define ATOMIC_OP_RETURN(op, asm_op) \
  50. static inline int atomic_##op##_return_relaxed(int i, atomic_t *v) \
  51. { \
  52. long temp, result; \
  53. __asm__ __volatile__( \
  54. "1: ldl_l %0,%1\n" \
  55. " " #asm_op " %0,%3,%2\n" \
  56. " " #asm_op " %0,%3,%0\n" \
  57. " stl_c %0,%1\n" \
  58. " beq %0,2f\n" \
  59. ".subsection 2\n" \
  60. "2: br 1b\n" \
  61. ".previous" \
  62. :"=&r" (temp), "=m" (v->counter), "=&r" (result) \
  63. :"Ir" (i), "m" (v->counter) : "memory"); \
  64. smp_read_barrier_depends(); \
  65. return result; \
  66. }
  67. #define ATOMIC_FETCH_OP(op, asm_op) \
  68. static inline int atomic_fetch_##op##_relaxed(int i, atomic_t *v) \
  69. { \
  70. long temp, result; \
  71. __asm__ __volatile__( \
  72. "1: ldl_l %2,%1\n" \
  73. " " #asm_op " %2,%3,%0\n" \
  74. " stl_c %0,%1\n" \
  75. " beq %0,2f\n" \
  76. ".subsection 2\n" \
  77. "2: br 1b\n" \
  78. ".previous" \
  79. :"=&r" (temp), "=m" (v->counter), "=&r" (result) \
  80. :"Ir" (i), "m" (v->counter) : "memory"); \
  81. smp_read_barrier_depends(); \
  82. return result; \
  83. }
  84. #define ATOMIC64_OP(op, asm_op) \
  85. static __inline__ void atomic64_##op(long i, atomic64_t * v) \
  86. { \
  87. unsigned long temp; \
  88. __asm__ __volatile__( \
  89. "1: ldq_l %0,%1\n" \
  90. " " #asm_op " %0,%2,%0\n" \
  91. " stq_c %0,%1\n" \
  92. " beq %0,2f\n" \
  93. ".subsection 2\n" \
  94. "2: br 1b\n" \
  95. ".previous" \
  96. :"=&r" (temp), "=m" (v->counter) \
  97. :"Ir" (i), "m" (v->counter)); \
  98. } \
  99. #define ATOMIC64_OP_RETURN(op, asm_op) \
  100. static __inline__ long atomic64_##op##_return_relaxed(long i, atomic64_t * v) \
  101. { \
  102. long temp, result; \
  103. __asm__ __volatile__( \
  104. "1: ldq_l %0,%1\n" \
  105. " " #asm_op " %0,%3,%2\n" \
  106. " " #asm_op " %0,%3,%0\n" \
  107. " stq_c %0,%1\n" \
  108. " beq %0,2f\n" \
  109. ".subsection 2\n" \
  110. "2: br 1b\n" \
  111. ".previous" \
  112. :"=&r" (temp), "=m" (v->counter), "=&r" (result) \
  113. :"Ir" (i), "m" (v->counter) : "memory"); \
  114. smp_read_barrier_depends(); \
  115. return result; \
  116. }
  117. #define ATOMIC64_FETCH_OP(op, asm_op) \
  118. static __inline__ long atomic64_fetch_##op##_relaxed(long i, atomic64_t * v) \
  119. { \
  120. long temp, result; \
  121. __asm__ __volatile__( \
  122. "1: ldq_l %2,%1\n" \
  123. " " #asm_op " %2,%3,%0\n" \
  124. " stq_c %0,%1\n" \
  125. " beq %0,2f\n" \
  126. ".subsection 2\n" \
  127. "2: br 1b\n" \
  128. ".previous" \
  129. :"=&r" (temp), "=m" (v->counter), "=&r" (result) \
  130. :"Ir" (i), "m" (v->counter) : "memory"); \
  131. smp_read_barrier_depends(); \
  132. return result; \
  133. }
  134. #define ATOMIC_OPS(op) \
  135. ATOMIC_OP(op, op##l) \
  136. ATOMIC_OP_RETURN(op, op##l) \
  137. ATOMIC_FETCH_OP(op, op##l) \
  138. ATOMIC64_OP(op, op##q) \
  139. ATOMIC64_OP_RETURN(op, op##q) \
  140. ATOMIC64_FETCH_OP(op, op##q)
  141. ATOMIC_OPS(add)
  142. ATOMIC_OPS(sub)
  143. #define atomic_add_return_relaxed atomic_add_return_relaxed
  144. #define atomic_sub_return_relaxed atomic_sub_return_relaxed
  145. #define atomic_fetch_add_relaxed atomic_fetch_add_relaxed
  146. #define atomic_fetch_sub_relaxed atomic_fetch_sub_relaxed
  147. #define atomic64_add_return_relaxed atomic64_add_return_relaxed
  148. #define atomic64_sub_return_relaxed atomic64_sub_return_relaxed
  149. #define atomic64_fetch_add_relaxed atomic64_fetch_add_relaxed
  150. #define atomic64_fetch_sub_relaxed atomic64_fetch_sub_relaxed
  151. #define atomic_andnot atomic_andnot
  152. #define atomic64_andnot atomic64_andnot
  153. #undef ATOMIC_OPS
  154. #define ATOMIC_OPS(op, asm) \
  155. ATOMIC_OP(op, asm) \
  156. ATOMIC_FETCH_OP(op, asm) \
  157. ATOMIC64_OP(op, asm) \
  158. ATOMIC64_FETCH_OP(op, asm)
  159. ATOMIC_OPS(and, and)
  160. ATOMIC_OPS(andnot, bic)
  161. ATOMIC_OPS(or, bis)
  162. ATOMIC_OPS(xor, xor)
  163. #define atomic_fetch_and_relaxed atomic_fetch_and_relaxed
  164. #define atomic_fetch_andnot_relaxed atomic_fetch_andnot_relaxed
  165. #define atomic_fetch_or_relaxed atomic_fetch_or_relaxed
  166. #define atomic_fetch_xor_relaxed atomic_fetch_xor_relaxed
  167. #define atomic64_fetch_and_relaxed atomic64_fetch_and_relaxed
  168. #define atomic64_fetch_andnot_relaxed atomic64_fetch_andnot_relaxed
  169. #define atomic64_fetch_or_relaxed atomic64_fetch_or_relaxed
  170. #define atomic64_fetch_xor_relaxed atomic64_fetch_xor_relaxed
  171. #undef ATOMIC_OPS
  172. #undef ATOMIC64_FETCH_OP
  173. #undef ATOMIC64_OP_RETURN
  174. #undef ATOMIC64_OP
  175. #undef ATOMIC_FETCH_OP
  176. #undef ATOMIC_OP_RETURN
  177. #undef ATOMIC_OP
  178. #define atomic64_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), old, new))
  179. #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
  180. #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), old, new))
  181. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  182. /**
  183. * atomic_fetch_add_unless - add unless the number is a given value
  184. * @v: pointer of type atomic_t
  185. * @a: the amount to add to v...
  186. * @u: ...unless v is equal to u.
  187. *
  188. * Atomically adds @a to @v, so long as it was not @u.
  189. * Returns the old value of @v.
  190. */
  191. static __inline__ int atomic_fetch_add_unless(atomic_t *v, int a, int u)
  192. {
  193. int c, new, old;
  194. smp_mb();
  195. __asm__ __volatile__(
  196. "1: ldl_l %[old],%[mem]\n"
  197. " cmpeq %[old],%[u],%[c]\n"
  198. " addl %[old],%[a],%[new]\n"
  199. " bne %[c],2f\n"
  200. " stl_c %[new],%[mem]\n"
  201. " beq %[new],3f\n"
  202. "2:\n"
  203. ".subsection 2\n"
  204. "3: br 1b\n"
  205. ".previous"
  206. : [old] "=&r"(old), [new] "=&r"(new), [c] "=&r"(c)
  207. : [mem] "m"(*v), [a] "rI"(a), [u] "rI"((long)u)
  208. : "memory");
  209. smp_mb();
  210. return old;
  211. }
  212. #define atomic_fetch_add_unless atomic_fetch_add_unless
  213. /**
  214. * atomic64_fetch_add_unless - add unless the number is a given value
  215. * @v: pointer of type atomic64_t
  216. * @a: the amount to add to v...
  217. * @u: ...unless v is equal to u.
  218. *
  219. * Atomically adds @a to @v, so long as it was not @u.
  220. * Returns the old value of @v.
  221. */
  222. static __inline__ long atomic64_fetch_add_unless(atomic64_t *v, long a, long u)
  223. {
  224. long c, new, old;
  225. smp_mb();
  226. __asm__ __volatile__(
  227. "1: ldq_l %[old],%[mem]\n"
  228. " cmpeq %[old],%[u],%[c]\n"
  229. " addq %[old],%[a],%[new]\n"
  230. " bne %[c],2f\n"
  231. " stq_c %[new],%[mem]\n"
  232. " beq %[new],3f\n"
  233. "2:\n"
  234. ".subsection 2\n"
  235. "3: br 1b\n"
  236. ".previous"
  237. : [old] "=&r"(old), [new] "=&r"(new), [c] "=&r"(c)
  238. : [mem] "m"(*v), [a] "rI"(a), [u] "rI"(u)
  239. : "memory");
  240. smp_mb();
  241. return old;
  242. }
  243. #define atomic64_fetch_add_unless atomic64_fetch_add_unless
  244. /*
  245. * atomic64_dec_if_positive - decrement by 1 if old value positive
  246. * @v: pointer of type atomic_t
  247. *
  248. * The function returns the old value of *v minus 1, even if
  249. * the atomic variable, v, was not decremented.
  250. */
  251. static inline long atomic64_dec_if_positive(atomic64_t *v)
  252. {
  253. long old, tmp;
  254. smp_mb();
  255. __asm__ __volatile__(
  256. "1: ldq_l %[old],%[mem]\n"
  257. " subq %[old],1,%[tmp]\n"
  258. " ble %[old],2f\n"
  259. " stq_c %[tmp],%[mem]\n"
  260. " beq %[tmp],3f\n"
  261. "2:\n"
  262. ".subsection 2\n"
  263. "3: br 1b\n"
  264. ".previous"
  265. : [old] "=&r"(old), [tmp] "=&r"(tmp)
  266. : [mem] "m"(*v)
  267. : "memory");
  268. smp_mb();
  269. return old - 1;
  270. }
  271. #define atomic64_dec_if_positive atomic64_dec_if_positive
  272. #endif /* _ALPHA_ATOMIC_H */