atomic.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_POWERPC_ATOMIC_H_
  3. #define _ASM_POWERPC_ATOMIC_H_
  4. /*
  5. * PowerPC atomic operations
  6. */
  7. #ifdef __KERNEL__
  8. #include <linux/types.h>
  9. #include <asm/cmpxchg.h>
  10. #include <asm/barrier.h>
  11. #include <asm/asm-const.h>
  12. /*
  13. * Since *_return_relaxed and {cmp}xchg_relaxed are implemented with
  14. * a "bne-" instruction at the end, so an isync is enough as a acquire barrier
  15. * on the platform without lwsync.
  16. */
  17. #define __atomic_acquire_fence() \
  18. __asm__ __volatile__(PPC_ACQUIRE_BARRIER "" : : : "memory")
  19. #define __atomic_release_fence() \
  20. __asm__ __volatile__(PPC_RELEASE_BARRIER "" : : : "memory")
  21. static __inline__ int arch_atomic_read(const atomic_t *v)
  22. {
  23. int t;
  24. __asm__ __volatile__("lwz%U1%X1 %0,%1" : "=r"(t) : "m<>"(v->counter));
  25. return t;
  26. }
  27. static __inline__ void arch_atomic_set(atomic_t *v, int i)
  28. {
  29. __asm__ __volatile__("stw%U0%X0 %1,%0" : "=m<>"(v->counter) : "r"(i));
  30. }
  31. #define ATOMIC_OP(op, asm_op, suffix, sign, ...) \
  32. static __inline__ void arch_atomic_##op(int a, atomic_t *v) \
  33. { \
  34. int t; \
  35. \
  36. __asm__ __volatile__( \
  37. "1: lwarx %0,0,%3 # atomic_" #op "\n" \
  38. #asm_op "%I2" suffix " %0,%0,%2\n" \
  39. " stwcx. %0,0,%3 \n" \
  40. " bne- 1b\n" \
  41. : "=&r" (t), "+m" (v->counter) \
  42. : "r"#sign (a), "r" (&v->counter) \
  43. : "cc", ##__VA_ARGS__); \
  44. } \
  45. #define ATOMIC_OP_RETURN_RELAXED(op, asm_op, suffix, sign, ...) \
  46. static inline int arch_atomic_##op##_return_relaxed(int a, atomic_t *v) \
  47. { \
  48. int t; \
  49. \
  50. __asm__ __volatile__( \
  51. "1: lwarx %0,0,%3 # atomic_" #op "_return_relaxed\n" \
  52. #asm_op "%I2" suffix " %0,%0,%2\n" \
  53. " stwcx. %0,0,%3\n" \
  54. " bne- 1b\n" \
  55. : "=&r" (t), "+m" (v->counter) \
  56. : "r"#sign (a), "r" (&v->counter) \
  57. : "cc", ##__VA_ARGS__); \
  58. \
  59. return t; \
  60. }
  61. #define ATOMIC_FETCH_OP_RELAXED(op, asm_op, suffix, sign, ...) \
  62. static inline int arch_atomic_fetch_##op##_relaxed(int a, atomic_t *v) \
  63. { \
  64. int res, t; \
  65. \
  66. __asm__ __volatile__( \
  67. "1: lwarx %0,0,%4 # atomic_fetch_" #op "_relaxed\n" \
  68. #asm_op "%I3" suffix " %1,%0,%3\n" \
  69. " stwcx. %1,0,%4\n" \
  70. " bne- 1b\n" \
  71. : "=&r" (res), "=&r" (t), "+m" (v->counter) \
  72. : "r"#sign (a), "r" (&v->counter) \
  73. : "cc", ##__VA_ARGS__); \
  74. \
  75. return res; \
  76. }
  77. #define ATOMIC_OPS(op, asm_op, suffix, sign, ...) \
  78. ATOMIC_OP(op, asm_op, suffix, sign, ##__VA_ARGS__) \
  79. ATOMIC_OP_RETURN_RELAXED(op, asm_op, suffix, sign, ##__VA_ARGS__)\
  80. ATOMIC_FETCH_OP_RELAXED(op, asm_op, suffix, sign, ##__VA_ARGS__)
  81. ATOMIC_OPS(add, add, "c", I, "xer")
  82. ATOMIC_OPS(sub, sub, "c", I, "xer")
  83. #define arch_atomic_add_return_relaxed arch_atomic_add_return_relaxed
  84. #define arch_atomic_sub_return_relaxed arch_atomic_sub_return_relaxed
  85. #define arch_atomic_fetch_add_relaxed arch_atomic_fetch_add_relaxed
  86. #define arch_atomic_fetch_sub_relaxed arch_atomic_fetch_sub_relaxed
  87. #undef ATOMIC_OPS
  88. #define ATOMIC_OPS(op, asm_op, suffix, sign) \
  89. ATOMIC_OP(op, asm_op, suffix, sign) \
  90. ATOMIC_FETCH_OP_RELAXED(op, asm_op, suffix, sign)
  91. ATOMIC_OPS(and, and, ".", K)
  92. ATOMIC_OPS(or, or, "", K)
  93. ATOMIC_OPS(xor, xor, "", K)
  94. #define arch_atomic_fetch_and_relaxed arch_atomic_fetch_and_relaxed
  95. #define arch_atomic_fetch_or_relaxed arch_atomic_fetch_or_relaxed
  96. #define arch_atomic_fetch_xor_relaxed arch_atomic_fetch_xor_relaxed
  97. #undef ATOMIC_OPS
  98. #undef ATOMIC_FETCH_OP_RELAXED
  99. #undef ATOMIC_OP_RETURN_RELAXED
  100. #undef ATOMIC_OP
  101. #define arch_atomic_cmpxchg(v, o, n) \
  102. (arch_cmpxchg(&((v)->counter), (o), (n)))
  103. #define arch_atomic_cmpxchg_relaxed(v, o, n) \
  104. arch_cmpxchg_relaxed(&((v)->counter), (o), (n))
  105. #define arch_atomic_cmpxchg_acquire(v, o, n) \
  106. arch_cmpxchg_acquire(&((v)->counter), (o), (n))
  107. #define arch_atomic_xchg(v, new) \
  108. (arch_xchg(&((v)->counter), new))
  109. #define arch_atomic_xchg_relaxed(v, new) \
  110. arch_xchg_relaxed(&((v)->counter), (new))
  111. /*
  112. * Don't want to override the generic atomic_try_cmpxchg_acquire, because
  113. * we add a lock hint to the lwarx, which may not be wanted for the
  114. * _acquire case (and is not used by the other _acquire variants so it
  115. * would be a surprise).
  116. */
  117. static __always_inline bool
  118. arch_atomic_try_cmpxchg_lock(atomic_t *v, int *old, int new)
  119. {
  120. int r, o = *old;
  121. unsigned int eh = IS_ENABLED(CONFIG_PPC64);
  122. __asm__ __volatile__ (
  123. "1: lwarx %0,0,%2,%[eh] # atomic_try_cmpxchg_acquire \n"
  124. " cmpw 0,%0,%3 \n"
  125. " bne- 2f \n"
  126. " stwcx. %4,0,%2 \n"
  127. " bne- 1b \n"
  128. "\t" PPC_ACQUIRE_BARRIER " \n"
  129. "2: \n"
  130. : "=&r" (r), "+m" (v->counter)
  131. : "r" (&v->counter), "r" (o), "r" (new), [eh] "n" (eh)
  132. : "cr0", "memory");
  133. if (unlikely(r != o))
  134. *old = r;
  135. return likely(r == o);
  136. }
  137. /**
  138. * atomic_fetch_add_unless - add unless the number is a given value
  139. * @v: pointer of type atomic_t
  140. * @a: the amount to add to v...
  141. * @u: ...unless v is equal to u.
  142. *
  143. * Atomically adds @a to @v, so long as it was not @u.
  144. * Returns the old value of @v.
  145. */
  146. static __inline__ int arch_atomic_fetch_add_unless(atomic_t *v, int a, int u)
  147. {
  148. int t;
  149. __asm__ __volatile__ (
  150. PPC_ATOMIC_ENTRY_BARRIER
  151. "1: lwarx %0,0,%1 # atomic_fetch_add_unless\n\
  152. cmpw 0,%0,%3 \n\
  153. beq 2f \n\
  154. add%I2c %0,%0,%2 \n"
  155. " stwcx. %0,0,%1 \n\
  156. bne- 1b \n"
  157. PPC_ATOMIC_EXIT_BARRIER
  158. " sub%I2c %0,%0,%2 \n\
  159. 2:"
  160. : "=&r" (t)
  161. : "r" (&v->counter), "rI" (a), "r" (u)
  162. : "cc", "memory", "xer");
  163. return t;
  164. }
  165. #define arch_atomic_fetch_add_unless arch_atomic_fetch_add_unless
  166. /*
  167. * Atomically test *v and decrement if it is greater than 0.
  168. * The function returns the old value of *v minus 1, even if
  169. * the atomic variable, v, was not decremented.
  170. */
  171. static __inline__ int arch_atomic_dec_if_positive(atomic_t *v)
  172. {
  173. int t;
  174. __asm__ __volatile__(
  175. PPC_ATOMIC_ENTRY_BARRIER
  176. "1: lwarx %0,0,%1 # atomic_dec_if_positive\n\
  177. cmpwi %0,1\n\
  178. addi %0,%0,-1\n\
  179. blt- 2f\n"
  180. " stwcx. %0,0,%1\n\
  181. bne- 1b"
  182. PPC_ATOMIC_EXIT_BARRIER
  183. "\n\
  184. 2:" : "=&b" (t)
  185. : "r" (&v->counter)
  186. : "cc", "memory");
  187. return t;
  188. }
  189. #define arch_atomic_dec_if_positive arch_atomic_dec_if_positive
  190. #ifdef __powerpc64__
  191. #define ATOMIC64_INIT(i) { (i) }
  192. static __inline__ s64 arch_atomic64_read(const atomic64_t *v)
  193. {
  194. s64 t;
  195. __asm__ __volatile__("ld%U1%X1 %0,%1" : "=r"(t) : "m<>"(v->counter));
  196. return t;
  197. }
  198. static __inline__ void arch_atomic64_set(atomic64_t *v, s64 i)
  199. {
  200. __asm__ __volatile__("std%U0%X0 %1,%0" : "=m<>"(v->counter) : "r"(i));
  201. }
  202. #define ATOMIC64_OP(op, asm_op) \
  203. static __inline__ void arch_atomic64_##op(s64 a, atomic64_t *v) \
  204. { \
  205. s64 t; \
  206. \
  207. __asm__ __volatile__( \
  208. "1: ldarx %0,0,%3 # atomic64_" #op "\n" \
  209. #asm_op " %0,%2,%0\n" \
  210. " stdcx. %0,0,%3 \n" \
  211. " bne- 1b\n" \
  212. : "=&r" (t), "+m" (v->counter) \
  213. : "r" (a), "r" (&v->counter) \
  214. : "cc"); \
  215. }
  216. #define ATOMIC64_OP_RETURN_RELAXED(op, asm_op) \
  217. static inline s64 \
  218. arch_atomic64_##op##_return_relaxed(s64 a, atomic64_t *v) \
  219. { \
  220. s64 t; \
  221. \
  222. __asm__ __volatile__( \
  223. "1: ldarx %0,0,%3 # atomic64_" #op "_return_relaxed\n" \
  224. #asm_op " %0,%2,%0\n" \
  225. " stdcx. %0,0,%3\n" \
  226. " bne- 1b\n" \
  227. : "=&r" (t), "+m" (v->counter) \
  228. : "r" (a), "r" (&v->counter) \
  229. : "cc"); \
  230. \
  231. return t; \
  232. }
  233. #define ATOMIC64_FETCH_OP_RELAXED(op, asm_op) \
  234. static inline s64 \
  235. arch_atomic64_fetch_##op##_relaxed(s64 a, atomic64_t *v) \
  236. { \
  237. s64 res, t; \
  238. \
  239. __asm__ __volatile__( \
  240. "1: ldarx %0,0,%4 # atomic64_fetch_" #op "_relaxed\n" \
  241. #asm_op " %1,%3,%0\n" \
  242. " stdcx. %1,0,%4\n" \
  243. " bne- 1b\n" \
  244. : "=&r" (res), "=&r" (t), "+m" (v->counter) \
  245. : "r" (a), "r" (&v->counter) \
  246. : "cc"); \
  247. \
  248. return res; \
  249. }
  250. #define ATOMIC64_OPS(op, asm_op) \
  251. ATOMIC64_OP(op, asm_op) \
  252. ATOMIC64_OP_RETURN_RELAXED(op, asm_op) \
  253. ATOMIC64_FETCH_OP_RELAXED(op, asm_op)
  254. ATOMIC64_OPS(add, add)
  255. ATOMIC64_OPS(sub, subf)
  256. #define arch_atomic64_add_return_relaxed arch_atomic64_add_return_relaxed
  257. #define arch_atomic64_sub_return_relaxed arch_atomic64_sub_return_relaxed
  258. #define arch_atomic64_fetch_add_relaxed arch_atomic64_fetch_add_relaxed
  259. #define arch_atomic64_fetch_sub_relaxed arch_atomic64_fetch_sub_relaxed
  260. #undef ATOMIC64_OPS
  261. #define ATOMIC64_OPS(op, asm_op) \
  262. ATOMIC64_OP(op, asm_op) \
  263. ATOMIC64_FETCH_OP_RELAXED(op, asm_op)
  264. ATOMIC64_OPS(and, and)
  265. ATOMIC64_OPS(or, or)
  266. ATOMIC64_OPS(xor, xor)
  267. #define arch_atomic64_fetch_and_relaxed arch_atomic64_fetch_and_relaxed
  268. #define arch_atomic64_fetch_or_relaxed arch_atomic64_fetch_or_relaxed
  269. #define arch_atomic64_fetch_xor_relaxed arch_atomic64_fetch_xor_relaxed
  270. #undef ATOPIC64_OPS
  271. #undef ATOMIC64_FETCH_OP_RELAXED
  272. #undef ATOMIC64_OP_RETURN_RELAXED
  273. #undef ATOMIC64_OP
  274. static __inline__ void arch_atomic64_inc(atomic64_t *v)
  275. {
  276. s64 t;
  277. __asm__ __volatile__(
  278. "1: ldarx %0,0,%2 # atomic64_inc\n\
  279. addic %0,%0,1\n\
  280. stdcx. %0,0,%2 \n\
  281. bne- 1b"
  282. : "=&r" (t), "+m" (v->counter)
  283. : "r" (&v->counter)
  284. : "cc", "xer");
  285. }
  286. #define arch_atomic64_inc arch_atomic64_inc
  287. static __inline__ s64 arch_atomic64_inc_return_relaxed(atomic64_t *v)
  288. {
  289. s64 t;
  290. __asm__ __volatile__(
  291. "1: ldarx %0,0,%2 # atomic64_inc_return_relaxed\n"
  292. " addic %0,%0,1\n"
  293. " stdcx. %0,0,%2\n"
  294. " bne- 1b"
  295. : "=&r" (t), "+m" (v->counter)
  296. : "r" (&v->counter)
  297. : "cc", "xer");
  298. return t;
  299. }
  300. static __inline__ void arch_atomic64_dec(atomic64_t *v)
  301. {
  302. s64 t;
  303. __asm__ __volatile__(
  304. "1: ldarx %0,0,%2 # atomic64_dec\n\
  305. addic %0,%0,-1\n\
  306. stdcx. %0,0,%2\n\
  307. bne- 1b"
  308. : "=&r" (t), "+m" (v->counter)
  309. : "r" (&v->counter)
  310. : "cc", "xer");
  311. }
  312. #define arch_atomic64_dec arch_atomic64_dec
  313. static __inline__ s64 arch_atomic64_dec_return_relaxed(atomic64_t *v)
  314. {
  315. s64 t;
  316. __asm__ __volatile__(
  317. "1: ldarx %0,0,%2 # atomic64_dec_return_relaxed\n"
  318. " addic %0,%0,-1\n"
  319. " stdcx. %0,0,%2\n"
  320. " bne- 1b"
  321. : "=&r" (t), "+m" (v->counter)
  322. : "r" (&v->counter)
  323. : "cc", "xer");
  324. return t;
  325. }
  326. #define arch_atomic64_inc_return_relaxed arch_atomic64_inc_return_relaxed
  327. #define arch_atomic64_dec_return_relaxed arch_atomic64_dec_return_relaxed
  328. /*
  329. * Atomically test *v and decrement if it is greater than 0.
  330. * The function returns the old value of *v minus 1.
  331. */
  332. static __inline__ s64 arch_atomic64_dec_if_positive(atomic64_t *v)
  333. {
  334. s64 t;
  335. __asm__ __volatile__(
  336. PPC_ATOMIC_ENTRY_BARRIER
  337. "1: ldarx %0,0,%1 # atomic64_dec_if_positive\n\
  338. addic. %0,%0,-1\n\
  339. blt- 2f\n\
  340. stdcx. %0,0,%1\n\
  341. bne- 1b"
  342. PPC_ATOMIC_EXIT_BARRIER
  343. "\n\
  344. 2:" : "=&r" (t)
  345. : "r" (&v->counter)
  346. : "cc", "xer", "memory");
  347. return t;
  348. }
  349. #define arch_atomic64_dec_if_positive arch_atomic64_dec_if_positive
  350. #define arch_atomic64_cmpxchg(v, o, n) \
  351. (arch_cmpxchg(&((v)->counter), (o), (n)))
  352. #define arch_atomic64_cmpxchg_relaxed(v, o, n) \
  353. arch_cmpxchg_relaxed(&((v)->counter), (o), (n))
  354. #define arch_atomic64_cmpxchg_acquire(v, o, n) \
  355. arch_cmpxchg_acquire(&((v)->counter), (o), (n))
  356. #define arch_atomic64_xchg(v, new) \
  357. (arch_xchg(&((v)->counter), new))
  358. #define arch_atomic64_xchg_relaxed(v, new) \
  359. arch_xchg_relaxed(&((v)->counter), (new))
  360. /**
  361. * atomic64_fetch_add_unless - add unless the number is a given value
  362. * @v: pointer of type atomic64_t
  363. * @a: the amount to add to v...
  364. * @u: ...unless v is equal to u.
  365. *
  366. * Atomically adds @a to @v, so long as it was not @u.
  367. * Returns the old value of @v.
  368. */
  369. static __inline__ s64 arch_atomic64_fetch_add_unless(atomic64_t *v, s64 a, s64 u)
  370. {
  371. s64 t;
  372. __asm__ __volatile__ (
  373. PPC_ATOMIC_ENTRY_BARRIER
  374. "1: ldarx %0,0,%1 # atomic64_fetch_add_unless\n\
  375. cmpd 0,%0,%3 \n\
  376. beq 2f \n\
  377. add %0,%2,%0 \n"
  378. " stdcx. %0,0,%1 \n\
  379. bne- 1b \n"
  380. PPC_ATOMIC_EXIT_BARRIER
  381. " subf %0,%2,%0 \n\
  382. 2:"
  383. : "=&r" (t)
  384. : "r" (&v->counter), "r" (a), "r" (u)
  385. : "cc", "memory");
  386. return t;
  387. }
  388. #define arch_atomic64_fetch_add_unless arch_atomic64_fetch_add_unless
  389. /**
  390. * atomic_inc64_not_zero - increment unless the number is zero
  391. * @v: pointer of type atomic64_t
  392. *
  393. * Atomically increments @v by 1, so long as @v is non-zero.
  394. * Returns non-zero if @v was non-zero, and zero otherwise.
  395. */
  396. static __inline__ int arch_atomic64_inc_not_zero(atomic64_t *v)
  397. {
  398. s64 t1, t2;
  399. __asm__ __volatile__ (
  400. PPC_ATOMIC_ENTRY_BARRIER
  401. "1: ldarx %0,0,%2 # atomic64_inc_not_zero\n\
  402. cmpdi 0,%0,0\n\
  403. beq- 2f\n\
  404. addic %1,%0,1\n\
  405. stdcx. %1,0,%2\n\
  406. bne- 1b\n"
  407. PPC_ATOMIC_EXIT_BARRIER
  408. "\n\
  409. 2:"
  410. : "=&r" (t1), "=&r" (t2)
  411. : "r" (&v->counter)
  412. : "cc", "xer", "memory");
  413. return t1 != 0;
  414. }
  415. #define arch_atomic64_inc_not_zero(v) arch_atomic64_inc_not_zero((v))
  416. #endif /* __powerpc64__ */
  417. #endif /* __KERNEL__ */
  418. #endif /* _ASM_POWERPC_ATOMIC_H_ */