atomic64-arcv2.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * ARCv2 supports 64-bit exclusive load (LLOCKD) / store (SCONDD)
  4. * - The address HAS to be 64-bit aligned
  5. */
  6. #ifndef _ASM_ARC_ATOMIC64_ARCV2_H
  7. #define _ASM_ARC_ATOMIC64_ARCV2_H
  8. typedef struct {
  9. s64 __aligned(8) counter;
  10. } atomic64_t;
  11. #define ATOMIC64_INIT(a) { (a) }
  12. static inline s64 arch_atomic64_read(const atomic64_t *v)
  13. {
  14. s64 val;
  15. __asm__ __volatile__(
  16. " ldd %0, [%1] \n"
  17. : "=r"(val)
  18. : "r"(&v->counter));
  19. return val;
  20. }
  21. static inline void arch_atomic64_set(atomic64_t *v, s64 a)
  22. {
  23. /*
  24. * This could have been a simple assignment in "C" but would need
  25. * explicit volatile. Otherwise gcc optimizers could elide the store
  26. * which borked atomic64 self-test
  27. * In the inline asm version, memory clobber needed for exact same
  28. * reason, to tell gcc about the store.
  29. *
  30. * This however is not needed for sibling atomic64_add() etc since both
  31. * load/store are explicitly done in inline asm. As long as API is used
  32. * for each access, gcc has no way to optimize away any load/store
  33. */
  34. __asm__ __volatile__(
  35. " std %0, [%1] \n"
  36. :
  37. : "r"(a), "r"(&v->counter)
  38. : "memory");
  39. }
  40. #define ATOMIC64_OP(op, op1, op2) \
  41. static inline void arch_atomic64_##op(s64 a, atomic64_t *v) \
  42. { \
  43. s64 val; \
  44. \
  45. __asm__ __volatile__( \
  46. "1: \n" \
  47. " llockd %0, [%1] \n" \
  48. " " #op1 " %L0, %L0, %L2 \n" \
  49. " " #op2 " %H0, %H0, %H2 \n" \
  50. " scondd %0, [%1] \n" \
  51. " bnz 1b \n" \
  52. : "=&r"(val) \
  53. : "r"(&v->counter), "ir"(a) \
  54. : "cc", "memory"); \
  55. } \
  56. #define ATOMIC64_OP_RETURN(op, op1, op2) \
  57. static inline s64 arch_atomic64_##op##_return_relaxed(s64 a, atomic64_t *v) \
  58. { \
  59. s64 val; \
  60. \
  61. __asm__ __volatile__( \
  62. "1: \n" \
  63. " llockd %0, [%1] \n" \
  64. " " #op1 " %L0, %L0, %L2 \n" \
  65. " " #op2 " %H0, %H0, %H2 \n" \
  66. " scondd %0, [%1] \n" \
  67. " bnz 1b \n" \
  68. : [val] "=&r"(val) \
  69. : "r"(&v->counter), "ir"(a) \
  70. : "cc", "memory"); \
  71. \
  72. return val; \
  73. }
  74. #define arch_atomic64_add_return_relaxed arch_atomic64_add_return_relaxed
  75. #define arch_atomic64_sub_return_relaxed arch_atomic64_sub_return_relaxed
  76. #define ATOMIC64_FETCH_OP(op, op1, op2) \
  77. static inline s64 arch_atomic64_fetch_##op##_relaxed(s64 a, atomic64_t *v) \
  78. { \
  79. s64 val, orig; \
  80. \
  81. __asm__ __volatile__( \
  82. "1: \n" \
  83. " llockd %0, [%2] \n" \
  84. " " #op1 " %L1, %L0, %L3 \n" \
  85. " " #op2 " %H1, %H0, %H3 \n" \
  86. " scondd %1, [%2] \n" \
  87. " bnz 1b \n" \
  88. : "=&r"(orig), "=&r"(val) \
  89. : "r"(&v->counter), "ir"(a) \
  90. : "cc", "memory"); \
  91. \
  92. return orig; \
  93. }
  94. #define arch_atomic64_fetch_add_relaxed arch_atomic64_fetch_add_relaxed
  95. #define arch_atomic64_fetch_sub_relaxed arch_atomic64_fetch_sub_relaxed
  96. #define arch_atomic64_fetch_and_relaxed arch_atomic64_fetch_and_relaxed
  97. #define arch_atomic64_fetch_andnot_relaxed arch_atomic64_fetch_andnot_relaxed
  98. #define arch_atomic64_fetch_or_relaxed arch_atomic64_fetch_or_relaxed
  99. #define arch_atomic64_fetch_xor_relaxed arch_atomic64_fetch_xor_relaxed
  100. #define ATOMIC64_OPS(op, op1, op2) \
  101. ATOMIC64_OP(op, op1, op2) \
  102. ATOMIC64_OP_RETURN(op, op1, op2) \
  103. ATOMIC64_FETCH_OP(op, op1, op2)
  104. ATOMIC64_OPS(add, add.f, adc)
  105. ATOMIC64_OPS(sub, sub.f, sbc)
  106. #undef ATOMIC64_OPS
  107. #define ATOMIC64_OPS(op, op1, op2) \
  108. ATOMIC64_OP(op, op1, op2) \
  109. ATOMIC64_FETCH_OP(op, op1, op2)
  110. ATOMIC64_OPS(and, and, and)
  111. ATOMIC64_OPS(andnot, bic, bic)
  112. ATOMIC64_OPS(or, or, or)
  113. ATOMIC64_OPS(xor, xor, xor)
  114. #define arch_atomic64_andnot arch_atomic64_andnot
  115. #undef ATOMIC64_OPS
  116. #undef ATOMIC64_FETCH_OP
  117. #undef ATOMIC64_OP_RETURN
  118. #undef ATOMIC64_OP
  119. static inline s64
  120. arch_atomic64_cmpxchg(atomic64_t *ptr, s64 expected, s64 new)
  121. {
  122. s64 prev;
  123. smp_mb();
  124. __asm__ __volatile__(
  125. "1: llockd %0, [%1] \n"
  126. " brne %L0, %L2, 2f \n"
  127. " brne %H0, %H2, 2f \n"
  128. " scondd %3, [%1] \n"
  129. " bnz 1b \n"
  130. "2: \n"
  131. : "=&r"(prev)
  132. : "r"(ptr), "ir"(expected), "r"(new)
  133. : "cc"); /* memory clobber comes from smp_mb() */
  134. smp_mb();
  135. return prev;
  136. }
  137. static inline s64 arch_atomic64_xchg(atomic64_t *ptr, s64 new)
  138. {
  139. s64 prev;
  140. smp_mb();
  141. __asm__ __volatile__(
  142. "1: llockd %0, [%1] \n"
  143. " scondd %2, [%1] \n"
  144. " bnz 1b \n"
  145. "2: \n"
  146. : "=&r"(prev)
  147. : "r"(ptr), "r"(new)
  148. : "cc"); /* memory clobber comes from smp_mb() */
  149. smp_mb();
  150. return prev;
  151. }
  152. /**
  153. * arch_atomic64_dec_if_positive - decrement by 1 if old value positive
  154. * @v: pointer of type atomic64_t
  155. *
  156. * The function returns the old value of *v minus 1, even if
  157. * the atomic variable, v, was not decremented.
  158. */
  159. static inline s64 arch_atomic64_dec_if_positive(atomic64_t *v)
  160. {
  161. s64 val;
  162. smp_mb();
  163. __asm__ __volatile__(
  164. "1: llockd %0, [%1] \n"
  165. " sub.f %L0, %L0, 1 # w0 - 1, set C on borrow\n"
  166. " sub.c %H0, %H0, 1 # if C set, w1 - 1\n"
  167. " brlt %H0, 0, 2f \n"
  168. " scondd %0, [%1] \n"
  169. " bnz 1b \n"
  170. "2: \n"
  171. : "=&r"(val)
  172. : "r"(&v->counter)
  173. : "cc"); /* memory clobber comes from smp_mb() */
  174. smp_mb();
  175. return val;
  176. }
  177. #define arch_atomic64_dec_if_positive arch_atomic64_dec_if_positive
  178. /**
  179. * arch_atomic64_fetch_add_unless - add unless the number is a given value
  180. * @v: pointer of type atomic64_t
  181. * @a: the amount to add to v...
  182. * @u: ...unless v is equal to u.
  183. *
  184. * Atomically adds @a to @v, if it was not @u.
  185. * Returns the old value of @v
  186. */
  187. static inline s64 arch_atomic64_fetch_add_unless(atomic64_t *v, s64 a, s64 u)
  188. {
  189. s64 old, temp;
  190. smp_mb();
  191. __asm__ __volatile__(
  192. "1: llockd %0, [%2] \n"
  193. " brne %L0, %L4, 2f # continue to add since v != u \n"
  194. " breq.d %H0, %H4, 3f # return since v == u \n"
  195. "2: \n"
  196. " add.f %L1, %L0, %L3 \n"
  197. " adc %H1, %H0, %H3 \n"
  198. " scondd %1, [%2] \n"
  199. " bnz 1b \n"
  200. "3: \n"
  201. : "=&r"(old), "=&r" (temp)
  202. : "r"(&v->counter), "r"(a), "r"(u)
  203. : "cc"); /* memory clobber comes from smp_mb() */
  204. smp_mb();
  205. return old;
  206. }
  207. #define arch_atomic64_fetch_add_unless arch_atomic64_fetch_add_unless
  208. #endif