atomic64-arcv2.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #define arch_atomic64_cmpxchg arch_atomic64_cmpxchg
  138. static inline s64 arch_atomic64_xchg(atomic64_t *ptr, s64 new)
  139. {
  140. s64 prev;
  141. smp_mb();
  142. __asm__ __volatile__(
  143. "1: llockd %0, [%1] \n"
  144. " scondd %2, [%1] \n"
  145. " bnz 1b \n"
  146. "2: \n"
  147. : "=&r"(prev)
  148. : "r"(ptr), "r"(new)
  149. : "cc"); /* memory clobber comes from smp_mb() */
  150. smp_mb();
  151. return prev;
  152. }
  153. #define arch_atomic64_xchg arch_atomic64_xchg
  154. static inline s64 arch_atomic64_dec_if_positive(atomic64_t *v)
  155. {
  156. s64 val;
  157. smp_mb();
  158. __asm__ __volatile__(
  159. "1: llockd %0, [%1] \n"
  160. " sub.f %L0, %L0, 1 # w0 - 1, set C on borrow\n"
  161. " sub.c %H0, %H0, 1 # if C set, w1 - 1\n"
  162. " brlt %H0, 0, 2f \n"
  163. " scondd %0, [%1] \n"
  164. " bnz 1b \n"
  165. "2: \n"
  166. : "=&r"(val)
  167. : "r"(&v->counter)
  168. : "cc"); /* memory clobber comes from smp_mb() */
  169. smp_mb();
  170. return val;
  171. }
  172. #define arch_atomic64_dec_if_positive arch_atomic64_dec_if_positive
  173. static inline s64 arch_atomic64_fetch_add_unless(atomic64_t *v, s64 a, s64 u)
  174. {
  175. s64 old, temp;
  176. smp_mb();
  177. __asm__ __volatile__(
  178. "1: llockd %0, [%2] \n"
  179. " brne %L0, %L4, 2f # continue to add since v != u \n"
  180. " breq.d %H0, %H4, 3f # return since v == u \n"
  181. "2: \n"
  182. " add.f %L1, %L0, %L3 \n"
  183. " adc %H1, %H0, %H3 \n"
  184. " scondd %1, [%2] \n"
  185. " bnz 1b \n"
  186. "3: \n"
  187. : "=&r"(old), "=&r" (temp)
  188. : "r"(&v->counter), "r"(a), "r"(u)
  189. : "cc"); /* memory clobber comes from smp_mb() */
  190. smp_mb();
  191. return old;
  192. }
  193. #define arch_atomic64_fetch_add_unless arch_atomic64_fetch_add_unless
  194. #endif