atomic.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Atomic operations.
  4. *
  5. * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  6. */
  7. #ifndef _ASM_ATOMIC_H
  8. #define _ASM_ATOMIC_H
  9. #include <linux/types.h>
  10. #include <asm/barrier.h>
  11. #include <asm/cmpxchg.h>
  12. #if __SIZEOF_LONG__ == 4
  13. #define __LL "ll.w "
  14. #define __SC "sc.w "
  15. #define __AMADD "amadd.w "
  16. #define __AMAND_DB "amand_db.w "
  17. #define __AMOR_DB "amor_db.w "
  18. #define __AMXOR_DB "amxor_db.w "
  19. #elif __SIZEOF_LONG__ == 8
  20. #define __LL "ll.d "
  21. #define __SC "sc.d "
  22. #define __AMADD "amadd.d "
  23. #define __AMAND_DB "amand_db.d "
  24. #define __AMOR_DB "amor_db.d "
  25. #define __AMXOR_DB "amxor_db.d "
  26. #endif
  27. #define ATOMIC_INIT(i) { (i) }
  28. /*
  29. * arch_atomic_read - read atomic variable
  30. * @v: pointer of type atomic_t
  31. *
  32. * Atomically reads the value of @v.
  33. */
  34. #define arch_atomic_read(v) READ_ONCE((v)->counter)
  35. /*
  36. * arch_atomic_set - set atomic variable
  37. * @v: pointer of type atomic_t
  38. * @i: required value
  39. *
  40. * Atomically sets the value of @v to @i.
  41. */
  42. #define arch_atomic_set(v, i) WRITE_ONCE((v)->counter, (i))
  43. #define ATOMIC_OP(op, I, asm_op) \
  44. static inline void arch_atomic_##op(int i, atomic_t *v) \
  45. { \
  46. __asm__ __volatile__( \
  47. "am"#asm_op"_db.w" " $zero, %1, %0 \n" \
  48. : "+ZB" (v->counter) \
  49. : "r" (I) \
  50. : "memory"); \
  51. }
  52. #define ATOMIC_OP_RETURN(op, I, asm_op, c_op) \
  53. static inline int arch_atomic_##op##_return_relaxed(int i, atomic_t *v) \
  54. { \
  55. int result; \
  56. \
  57. __asm__ __volatile__( \
  58. "am"#asm_op"_db.w" " %1, %2, %0 \n" \
  59. : "+ZB" (v->counter), "=&r" (result) \
  60. : "r" (I) \
  61. : "memory"); \
  62. \
  63. return result c_op I; \
  64. }
  65. #define ATOMIC_FETCH_OP(op, I, asm_op) \
  66. static inline int arch_atomic_fetch_##op##_relaxed(int i, atomic_t *v) \
  67. { \
  68. int result; \
  69. \
  70. __asm__ __volatile__( \
  71. "am"#asm_op"_db.w" " %1, %2, %0 \n" \
  72. : "+ZB" (v->counter), "=&r" (result) \
  73. : "r" (I) \
  74. : "memory"); \
  75. \
  76. return result; \
  77. }
  78. #define ATOMIC_OPS(op, I, asm_op, c_op) \
  79. ATOMIC_OP(op, I, asm_op) \
  80. ATOMIC_OP_RETURN(op, I, asm_op, c_op) \
  81. ATOMIC_FETCH_OP(op, I, asm_op)
  82. ATOMIC_OPS(add, i, add, +)
  83. ATOMIC_OPS(sub, -i, add, +)
  84. #define arch_atomic_add_return_relaxed arch_atomic_add_return_relaxed
  85. #define arch_atomic_sub_return_relaxed arch_atomic_sub_return_relaxed
  86. #define arch_atomic_fetch_add_relaxed arch_atomic_fetch_add_relaxed
  87. #define arch_atomic_fetch_sub_relaxed arch_atomic_fetch_sub_relaxed
  88. #undef ATOMIC_OPS
  89. #define ATOMIC_OPS(op, I, asm_op) \
  90. ATOMIC_OP(op, I, asm_op) \
  91. ATOMIC_FETCH_OP(op, I, asm_op)
  92. ATOMIC_OPS(and, i, and)
  93. ATOMIC_OPS(or, i, or)
  94. ATOMIC_OPS(xor, i, xor)
  95. #define arch_atomic_fetch_and_relaxed arch_atomic_fetch_and_relaxed
  96. #define arch_atomic_fetch_or_relaxed arch_atomic_fetch_or_relaxed
  97. #define arch_atomic_fetch_xor_relaxed arch_atomic_fetch_xor_relaxed
  98. #undef ATOMIC_OPS
  99. #undef ATOMIC_FETCH_OP
  100. #undef ATOMIC_OP_RETURN
  101. #undef ATOMIC_OP
  102. static inline int arch_atomic_fetch_add_unless(atomic_t *v, int a, int u)
  103. {
  104. int prev, rc;
  105. __asm__ __volatile__ (
  106. "0: ll.w %[p], %[c]\n"
  107. " beq %[p], %[u], 1f\n"
  108. " add.w %[rc], %[p], %[a]\n"
  109. " sc.w %[rc], %[c]\n"
  110. " beqz %[rc], 0b\n"
  111. " b 2f\n"
  112. "1:\n"
  113. __WEAK_LLSC_MB
  114. "2:\n"
  115. : [p]"=&r" (prev), [rc]"=&r" (rc),
  116. [c]"=ZB" (v->counter)
  117. : [a]"r" (a), [u]"r" (u)
  118. : "memory");
  119. return prev;
  120. }
  121. #define arch_atomic_fetch_add_unless arch_atomic_fetch_add_unless
  122. /*
  123. * arch_atomic_sub_if_positive - conditionally subtract integer from atomic variable
  124. * @i: integer value to subtract
  125. * @v: pointer of type atomic_t
  126. *
  127. * Atomically test @v and subtract @i if @v is greater or equal than @i.
  128. * The function returns the old value of @v minus @i.
  129. */
  130. static inline int arch_atomic_sub_if_positive(int i, atomic_t *v)
  131. {
  132. int result;
  133. int temp;
  134. if (__builtin_constant_p(i)) {
  135. __asm__ __volatile__(
  136. "1: ll.w %1, %2 # atomic_sub_if_positive\n"
  137. " addi.w %0, %1, %3 \n"
  138. " move %1, %0 \n"
  139. " bltz %0, 2f \n"
  140. " sc.w %1, %2 \n"
  141. " beqz %1, 1b \n"
  142. "2: \n"
  143. __WEAK_LLSC_MB
  144. : "=&r" (result), "=&r" (temp), "+ZC" (v->counter)
  145. : "I" (-i));
  146. } else {
  147. __asm__ __volatile__(
  148. "1: ll.w %1, %2 # atomic_sub_if_positive\n"
  149. " sub.w %0, %1, %3 \n"
  150. " move %1, %0 \n"
  151. " bltz %0, 2f \n"
  152. " sc.w %1, %2 \n"
  153. " beqz %1, 1b \n"
  154. "2: \n"
  155. __WEAK_LLSC_MB
  156. : "=&r" (result), "=&r" (temp), "+ZC" (v->counter)
  157. : "r" (i));
  158. }
  159. return result;
  160. }
  161. #define arch_atomic_cmpxchg(v, o, n) (arch_cmpxchg(&((v)->counter), (o), (n)))
  162. #define arch_atomic_xchg(v, new) (arch_xchg(&((v)->counter), (new)))
  163. /*
  164. * arch_atomic_dec_if_positive - decrement by 1 if old value positive
  165. * @v: pointer of type atomic_t
  166. */
  167. #define arch_atomic_dec_if_positive(v) arch_atomic_sub_if_positive(1, v)
  168. #ifdef CONFIG_64BIT
  169. #define ATOMIC64_INIT(i) { (i) }
  170. /*
  171. * arch_atomic64_read - read atomic variable
  172. * @v: pointer of type atomic64_t
  173. *
  174. */
  175. #define arch_atomic64_read(v) READ_ONCE((v)->counter)
  176. /*
  177. * arch_atomic64_set - set atomic variable
  178. * @v: pointer of type atomic64_t
  179. * @i: required value
  180. */
  181. #define arch_atomic64_set(v, i) WRITE_ONCE((v)->counter, (i))
  182. #define ATOMIC64_OP(op, I, asm_op) \
  183. static inline void arch_atomic64_##op(long i, atomic64_t *v) \
  184. { \
  185. __asm__ __volatile__( \
  186. "am"#asm_op"_db.d " " $zero, %1, %0 \n" \
  187. : "+ZB" (v->counter) \
  188. : "r" (I) \
  189. : "memory"); \
  190. }
  191. #define ATOMIC64_OP_RETURN(op, I, asm_op, c_op) \
  192. static inline long arch_atomic64_##op##_return_relaxed(long i, atomic64_t *v) \
  193. { \
  194. long result; \
  195. __asm__ __volatile__( \
  196. "am"#asm_op"_db.d " " %1, %2, %0 \n" \
  197. : "+ZB" (v->counter), "=&r" (result) \
  198. : "r" (I) \
  199. : "memory"); \
  200. \
  201. return result c_op I; \
  202. }
  203. #define ATOMIC64_FETCH_OP(op, I, asm_op) \
  204. static inline long arch_atomic64_fetch_##op##_relaxed(long i, atomic64_t *v) \
  205. { \
  206. long result; \
  207. \
  208. __asm__ __volatile__( \
  209. "am"#asm_op"_db.d " " %1, %2, %0 \n" \
  210. : "+ZB" (v->counter), "=&r" (result) \
  211. : "r" (I) \
  212. : "memory"); \
  213. \
  214. return result; \
  215. }
  216. #define ATOMIC64_OPS(op, I, asm_op, c_op) \
  217. ATOMIC64_OP(op, I, asm_op) \
  218. ATOMIC64_OP_RETURN(op, I, asm_op, c_op) \
  219. ATOMIC64_FETCH_OP(op, I, asm_op)
  220. ATOMIC64_OPS(add, i, add, +)
  221. ATOMIC64_OPS(sub, -i, add, +)
  222. #define arch_atomic64_add_return_relaxed arch_atomic64_add_return_relaxed
  223. #define arch_atomic64_sub_return_relaxed arch_atomic64_sub_return_relaxed
  224. #define arch_atomic64_fetch_add_relaxed arch_atomic64_fetch_add_relaxed
  225. #define arch_atomic64_fetch_sub_relaxed arch_atomic64_fetch_sub_relaxed
  226. #undef ATOMIC64_OPS
  227. #define ATOMIC64_OPS(op, I, asm_op) \
  228. ATOMIC64_OP(op, I, asm_op) \
  229. ATOMIC64_FETCH_OP(op, I, asm_op)
  230. ATOMIC64_OPS(and, i, and)
  231. ATOMIC64_OPS(or, i, or)
  232. ATOMIC64_OPS(xor, i, xor)
  233. #define arch_atomic64_fetch_and_relaxed arch_atomic64_fetch_and_relaxed
  234. #define arch_atomic64_fetch_or_relaxed arch_atomic64_fetch_or_relaxed
  235. #define arch_atomic64_fetch_xor_relaxed arch_atomic64_fetch_xor_relaxed
  236. #undef ATOMIC64_OPS
  237. #undef ATOMIC64_FETCH_OP
  238. #undef ATOMIC64_OP_RETURN
  239. #undef ATOMIC64_OP
  240. static inline long arch_atomic64_fetch_add_unless(atomic64_t *v, long a, long u)
  241. {
  242. long prev, rc;
  243. __asm__ __volatile__ (
  244. "0: ll.d %[p], %[c]\n"
  245. " beq %[p], %[u], 1f\n"
  246. " add.d %[rc], %[p], %[a]\n"
  247. " sc.d %[rc], %[c]\n"
  248. " beqz %[rc], 0b\n"
  249. " b 2f\n"
  250. "1:\n"
  251. __WEAK_LLSC_MB
  252. "2:\n"
  253. : [p]"=&r" (prev), [rc]"=&r" (rc),
  254. [c] "=ZB" (v->counter)
  255. : [a]"r" (a), [u]"r" (u)
  256. : "memory");
  257. return prev;
  258. }
  259. #define arch_atomic64_fetch_add_unless arch_atomic64_fetch_add_unless
  260. /*
  261. * arch_atomic64_sub_if_positive - conditionally subtract integer from atomic variable
  262. * @i: integer value to subtract
  263. * @v: pointer of type atomic64_t
  264. *
  265. * Atomically test @v and subtract @i if @v is greater or equal than @i.
  266. * The function returns the old value of @v minus @i.
  267. */
  268. static inline long arch_atomic64_sub_if_positive(long i, atomic64_t *v)
  269. {
  270. long result;
  271. long temp;
  272. if (__builtin_constant_p(i)) {
  273. __asm__ __volatile__(
  274. "1: ll.d %1, %2 # atomic64_sub_if_positive \n"
  275. " addi.d %0, %1, %3 \n"
  276. " move %1, %0 \n"
  277. " bltz %0, 2f \n"
  278. " sc.d %1, %2 \n"
  279. " beqz %1, 1b \n"
  280. "2: \n"
  281. __WEAK_LLSC_MB
  282. : "=&r" (result), "=&r" (temp), "+ZC" (v->counter)
  283. : "I" (-i));
  284. } else {
  285. __asm__ __volatile__(
  286. "1: ll.d %1, %2 # atomic64_sub_if_positive \n"
  287. " sub.d %0, %1, %3 \n"
  288. " move %1, %0 \n"
  289. " bltz %0, 2f \n"
  290. " sc.d %1, %2 \n"
  291. " beqz %1, 1b \n"
  292. "2: \n"
  293. __WEAK_LLSC_MB
  294. : "=&r" (result), "=&r" (temp), "+ZC" (v->counter)
  295. : "r" (i));
  296. }
  297. return result;
  298. }
  299. #define arch_atomic64_cmpxchg(v, o, n) \
  300. ((__typeof__((v)->counter))arch_cmpxchg(&((v)->counter), (o), (n)))
  301. #define arch_atomic64_xchg(v, new) (arch_xchg(&((v)->counter), (new)))
  302. /*
  303. * arch_atomic64_dec_if_positive - decrement by 1 if old value positive
  304. * @v: pointer of type atomic64_t
  305. */
  306. #define arch_atomic64_dec_if_positive(v) arch_atomic64_sub_if_positive(1, v)
  307. #endif /* CONFIG_64BIT */
  308. #endif /* _ASM_ATOMIC_H */