atomic_lse.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * Based on arch/arm/include/asm/atomic.h
  3. *
  4. * Copyright (C) 1996 Russell King.
  5. * Copyright (C) 2002 Deep Blue Solutions Ltd.
  6. * Copyright (C) 2012 ARM Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef __ASM_ATOMIC_LSE_H
  21. #define __ASM_ATOMIC_LSE_H
  22. #ifndef __ARM64_IN_ATOMIC_IMPL
  23. #error "please don't include this file directly"
  24. #endif
  25. #define __LL_SC_ATOMIC(op) __LL_SC_CALL(atomic_##op)
  26. static inline void atomic_andnot(int i, atomic_t *v)
  27. {
  28. register int w0 asm ("w0") = i;
  29. register atomic_t *x1 asm ("x1") = v;
  30. asm volatile(ARM64_LSE_ATOMIC_INSN(__LL_SC_ATOMIC(andnot),
  31. " stclr %w[i], %[v]\n")
  32. : [i] "+r" (w0), [v] "+Q" (v->counter)
  33. : "r" (x1)
  34. : "x30");
  35. }
  36. static inline void atomic_or(int i, atomic_t *v)
  37. {
  38. register int w0 asm ("w0") = i;
  39. register atomic_t *x1 asm ("x1") = v;
  40. asm volatile(ARM64_LSE_ATOMIC_INSN(__LL_SC_ATOMIC(or),
  41. " stset %w[i], %[v]\n")
  42. : [i] "+r" (w0), [v] "+Q" (v->counter)
  43. : "r" (x1)
  44. : "x30");
  45. }
  46. static inline void atomic_xor(int i, atomic_t *v)
  47. {
  48. register int w0 asm ("w0") = i;
  49. register atomic_t *x1 asm ("x1") = v;
  50. asm volatile(ARM64_LSE_ATOMIC_INSN(__LL_SC_ATOMIC(xor),
  51. " steor %w[i], %[v]\n")
  52. : [i] "+r" (w0), [v] "+Q" (v->counter)
  53. : "r" (x1)
  54. : "x30");
  55. }
  56. static inline void atomic_add(int i, atomic_t *v)
  57. {
  58. register int w0 asm ("w0") = i;
  59. register atomic_t *x1 asm ("x1") = v;
  60. asm volatile(ARM64_LSE_ATOMIC_INSN(__LL_SC_ATOMIC(add),
  61. " stadd %w[i], %[v]\n")
  62. : [i] "+r" (w0), [v] "+Q" (v->counter)
  63. : "r" (x1)
  64. : "x30");
  65. }
  66. #define ATOMIC_OP_ADD_RETURN(name, mb, cl...) \
  67. static inline int atomic_add_return##name(int i, atomic_t *v) \
  68. { \
  69. register int w0 asm ("w0") = i; \
  70. register atomic_t *x1 asm ("x1") = v; \
  71. \
  72. asm volatile(ARM64_LSE_ATOMIC_INSN( \
  73. /* LL/SC */ \
  74. " nop\n" \
  75. __LL_SC_ATOMIC(add_return##name), \
  76. /* LSE atomics */ \
  77. " ldadd" #mb " %w[i], w30, %[v]\n" \
  78. " add %w[i], %w[i], w30") \
  79. : [i] "+r" (w0), [v] "+Q" (v->counter) \
  80. : "r" (x1) \
  81. : "x30" , ##cl); \
  82. \
  83. return w0; \
  84. }
  85. ATOMIC_OP_ADD_RETURN(_relaxed, )
  86. ATOMIC_OP_ADD_RETURN(_acquire, a, "memory")
  87. ATOMIC_OP_ADD_RETURN(_release, l, "memory")
  88. ATOMIC_OP_ADD_RETURN( , al, "memory")
  89. #undef ATOMIC_OP_ADD_RETURN
  90. static inline void atomic_and(int i, atomic_t *v)
  91. {
  92. register int w0 asm ("w0") = i;
  93. register atomic_t *x1 asm ("x1") = v;
  94. asm volatile(ARM64_LSE_ATOMIC_INSN(
  95. /* LL/SC */
  96. " nop\n"
  97. __LL_SC_ATOMIC(and),
  98. /* LSE atomics */
  99. " mvn %w[i], %w[i]\n"
  100. " stclr %w[i], %[v]")
  101. : [i] "+&r" (w0), [v] "+Q" (v->counter)
  102. : "r" (x1)
  103. : "x30");
  104. }
  105. static inline void atomic_sub(int i, atomic_t *v)
  106. {
  107. register int w0 asm ("w0") = i;
  108. register atomic_t *x1 asm ("x1") = v;
  109. asm volatile(ARM64_LSE_ATOMIC_INSN(
  110. /* LL/SC */
  111. " nop\n"
  112. __LL_SC_ATOMIC(sub),
  113. /* LSE atomics */
  114. " neg %w[i], %w[i]\n"
  115. " stadd %w[i], %[v]")
  116. : [i] "+&r" (w0), [v] "+Q" (v->counter)
  117. : "r" (x1)
  118. : "x30");
  119. }
  120. #define ATOMIC_OP_SUB_RETURN(name, mb, cl...) \
  121. static inline int atomic_sub_return##name(int i, atomic_t *v) \
  122. { \
  123. register int w0 asm ("w0") = i; \
  124. register atomic_t *x1 asm ("x1") = v; \
  125. \
  126. asm volatile(ARM64_LSE_ATOMIC_INSN( \
  127. /* LL/SC */ \
  128. " nop\n" \
  129. __LL_SC_ATOMIC(sub_return##name) \
  130. " nop", \
  131. /* LSE atomics */ \
  132. " neg %w[i], %w[i]\n" \
  133. " ldadd" #mb " %w[i], w30, %[v]\n" \
  134. " add %w[i], %w[i], w30") \
  135. : [i] "+&r" (w0), [v] "+Q" (v->counter) \
  136. : "r" (x1) \
  137. : "x30" , ##cl); \
  138. \
  139. return w0; \
  140. }
  141. ATOMIC_OP_SUB_RETURN(_relaxed, )
  142. ATOMIC_OP_SUB_RETURN(_acquire, a, "memory")
  143. ATOMIC_OP_SUB_RETURN(_release, l, "memory")
  144. ATOMIC_OP_SUB_RETURN( , al, "memory")
  145. #undef ATOMIC_OP_SUB_RETURN
  146. #undef __LL_SC_ATOMIC
  147. #define __LL_SC_ATOMIC64(op) __LL_SC_CALL(atomic64_##op)
  148. static inline void atomic64_andnot(long i, atomic64_t *v)
  149. {
  150. register long x0 asm ("x0") = i;
  151. register atomic64_t *x1 asm ("x1") = v;
  152. asm volatile(ARM64_LSE_ATOMIC_INSN(__LL_SC_ATOMIC64(andnot),
  153. " stclr %[i], %[v]\n")
  154. : [i] "+r" (x0), [v] "+Q" (v->counter)
  155. : "r" (x1)
  156. : "x30");
  157. }
  158. static inline void atomic64_or(long i, atomic64_t *v)
  159. {
  160. register long x0 asm ("x0") = i;
  161. register atomic64_t *x1 asm ("x1") = v;
  162. asm volatile(ARM64_LSE_ATOMIC_INSN(__LL_SC_ATOMIC64(or),
  163. " stset %[i], %[v]\n")
  164. : [i] "+r" (x0), [v] "+Q" (v->counter)
  165. : "r" (x1)
  166. : "x30");
  167. }
  168. static inline void atomic64_xor(long i, atomic64_t *v)
  169. {
  170. register long x0 asm ("x0") = i;
  171. register atomic64_t *x1 asm ("x1") = v;
  172. asm volatile(ARM64_LSE_ATOMIC_INSN(__LL_SC_ATOMIC64(xor),
  173. " steor %[i], %[v]\n")
  174. : [i] "+r" (x0), [v] "+Q" (v->counter)
  175. : "r" (x1)
  176. : "x30");
  177. }
  178. static inline void atomic64_add(long i, atomic64_t *v)
  179. {
  180. register long x0 asm ("x0") = i;
  181. register atomic64_t *x1 asm ("x1") = v;
  182. asm volatile(ARM64_LSE_ATOMIC_INSN(__LL_SC_ATOMIC64(add),
  183. " stadd %[i], %[v]\n")
  184. : [i] "+r" (x0), [v] "+Q" (v->counter)
  185. : "r" (x1)
  186. : "x30");
  187. }
  188. #define ATOMIC64_OP_ADD_RETURN(name, mb, cl...) \
  189. static inline long atomic64_add_return##name(long i, atomic64_t *v) \
  190. { \
  191. register long x0 asm ("x0") = i; \
  192. register atomic64_t *x1 asm ("x1") = v; \
  193. \
  194. asm volatile(ARM64_LSE_ATOMIC_INSN( \
  195. /* LL/SC */ \
  196. " nop\n" \
  197. __LL_SC_ATOMIC64(add_return##name), \
  198. /* LSE atomics */ \
  199. " ldadd" #mb " %[i], x30, %[v]\n" \
  200. " add %[i], %[i], x30") \
  201. : [i] "+r" (x0), [v] "+Q" (v->counter) \
  202. : "r" (x1) \
  203. : "x30" , ##cl); \
  204. \
  205. return x0; \
  206. }
  207. ATOMIC64_OP_ADD_RETURN(_relaxed, )
  208. ATOMIC64_OP_ADD_RETURN(_acquire, a, "memory")
  209. ATOMIC64_OP_ADD_RETURN(_release, l, "memory")
  210. ATOMIC64_OP_ADD_RETURN( , al, "memory")
  211. #undef ATOMIC64_OP_ADD_RETURN
  212. static inline void atomic64_and(long i, atomic64_t *v)
  213. {
  214. register long x0 asm ("x0") = i;
  215. register atomic64_t *x1 asm ("x1") = v;
  216. asm volatile(ARM64_LSE_ATOMIC_INSN(
  217. /* LL/SC */
  218. " nop\n"
  219. __LL_SC_ATOMIC64(and),
  220. /* LSE atomics */
  221. " mvn %[i], %[i]\n"
  222. " stclr %[i], %[v]")
  223. : [i] "+&r" (x0), [v] "+Q" (v->counter)
  224. : "r" (x1)
  225. : "x30");
  226. }
  227. static inline void atomic64_sub(long i, atomic64_t *v)
  228. {
  229. register long x0 asm ("x0") = i;
  230. register atomic64_t *x1 asm ("x1") = v;
  231. asm volatile(ARM64_LSE_ATOMIC_INSN(
  232. /* LL/SC */
  233. " nop\n"
  234. __LL_SC_ATOMIC64(sub),
  235. /* LSE atomics */
  236. " neg %[i], %[i]\n"
  237. " stadd %[i], %[v]")
  238. : [i] "+&r" (x0), [v] "+Q" (v->counter)
  239. : "r" (x1)
  240. : "x30");
  241. }
  242. #define ATOMIC64_OP_SUB_RETURN(name, mb, cl...) \
  243. static inline long atomic64_sub_return##name(long i, atomic64_t *v) \
  244. { \
  245. register long x0 asm ("x0") = i; \
  246. register atomic64_t *x1 asm ("x1") = v; \
  247. \
  248. asm volatile(ARM64_LSE_ATOMIC_INSN( \
  249. /* LL/SC */ \
  250. " nop\n" \
  251. __LL_SC_ATOMIC64(sub_return##name) \
  252. " nop", \
  253. /* LSE atomics */ \
  254. " neg %[i], %[i]\n" \
  255. " ldadd" #mb " %[i], x30, %[v]\n" \
  256. " add %[i], %[i], x30") \
  257. : [i] "+&r" (x0), [v] "+Q" (v->counter) \
  258. : "r" (x1) \
  259. : "x30" , ##cl); \
  260. \
  261. return x0; \
  262. }
  263. ATOMIC64_OP_SUB_RETURN(_relaxed, )
  264. ATOMIC64_OP_SUB_RETURN(_acquire, a, "memory")
  265. ATOMIC64_OP_SUB_RETURN(_release, l, "memory")
  266. ATOMIC64_OP_SUB_RETURN( , al, "memory")
  267. #undef ATOMIC64_OP_SUB_RETURN
  268. static inline long atomic64_dec_if_positive(atomic64_t *v)
  269. {
  270. register long x0 asm ("x0") = (long)v;
  271. asm volatile(ARM64_LSE_ATOMIC_INSN(
  272. /* LL/SC */
  273. " nop\n"
  274. __LL_SC_ATOMIC64(dec_if_positive)
  275. " nop\n"
  276. " nop\n"
  277. " nop\n"
  278. " nop\n"
  279. " nop",
  280. /* LSE atomics */
  281. "1: ldr x30, %[v]\n"
  282. " subs %[ret], x30, #1\n"
  283. " b.lt 2f\n"
  284. " casal x30, %[ret], %[v]\n"
  285. " sub x30, x30, #1\n"
  286. " sub x30, x30, %[ret]\n"
  287. " cbnz x30, 1b\n"
  288. "2:")
  289. : [ret] "+&r" (x0), [v] "+Q" (v->counter)
  290. :
  291. : "x30", "cc", "memory");
  292. return x0;
  293. }
  294. #undef __LL_SC_ATOMIC64
  295. #define __LL_SC_CMPXCHG(op) __LL_SC_CALL(__cmpxchg_case_##op)
  296. #define __CMPXCHG_CASE(w, sz, name, mb, cl...) \
  297. static inline unsigned long __cmpxchg_case_##name(volatile void *ptr, \
  298. unsigned long old, \
  299. unsigned long new) \
  300. { \
  301. register unsigned long x0 asm ("x0") = (unsigned long)ptr; \
  302. register unsigned long x1 asm ("x1") = old; \
  303. register unsigned long x2 asm ("x2") = new; \
  304. \
  305. asm volatile(ARM64_LSE_ATOMIC_INSN( \
  306. /* LL/SC */ \
  307. " nop\n" \
  308. __LL_SC_CMPXCHG(name) \
  309. " nop", \
  310. /* LSE atomics */ \
  311. " mov " #w "30, %" #w "[old]\n" \
  312. " cas" #mb #sz "\t" #w "30, %" #w "[new], %[v]\n" \
  313. " mov %" #w "[ret], " #w "30") \
  314. : [ret] "+r" (x0), [v] "+Q" (*(unsigned long *)ptr) \
  315. : [old] "r" (x1), [new] "r" (x2) \
  316. : "x30" , ##cl); \
  317. \
  318. return x0; \
  319. }
  320. __CMPXCHG_CASE(w, b, 1, )
  321. __CMPXCHG_CASE(w, h, 2, )
  322. __CMPXCHG_CASE(w, , 4, )
  323. __CMPXCHG_CASE(x, , 8, )
  324. __CMPXCHG_CASE(w, b, acq_1, a, "memory")
  325. __CMPXCHG_CASE(w, h, acq_2, a, "memory")
  326. __CMPXCHG_CASE(w, , acq_4, a, "memory")
  327. __CMPXCHG_CASE(x, , acq_8, a, "memory")
  328. __CMPXCHG_CASE(w, b, rel_1, l, "memory")
  329. __CMPXCHG_CASE(w, h, rel_2, l, "memory")
  330. __CMPXCHG_CASE(w, , rel_4, l, "memory")
  331. __CMPXCHG_CASE(x, , rel_8, l, "memory")
  332. __CMPXCHG_CASE(w, b, mb_1, al, "memory")
  333. __CMPXCHG_CASE(w, h, mb_2, al, "memory")
  334. __CMPXCHG_CASE(w, , mb_4, al, "memory")
  335. __CMPXCHG_CASE(x, , mb_8, al, "memory")
  336. #undef __LL_SC_CMPXCHG
  337. #undef __CMPXCHG_CASE
  338. #define __LL_SC_CMPXCHG_DBL(op) __LL_SC_CALL(__cmpxchg_double##op)
  339. #define __CMPXCHG_DBL(name, mb, cl...) \
  340. static inline long __cmpxchg_double##name(unsigned long old1, \
  341. unsigned long old2, \
  342. unsigned long new1, \
  343. unsigned long new2, \
  344. volatile void *ptr) \
  345. { \
  346. unsigned long oldval1 = old1; \
  347. unsigned long oldval2 = old2; \
  348. register unsigned long x0 asm ("x0") = old1; \
  349. register unsigned long x1 asm ("x1") = old2; \
  350. register unsigned long x2 asm ("x2") = new1; \
  351. register unsigned long x3 asm ("x3") = new2; \
  352. register unsigned long x4 asm ("x4") = (unsigned long)ptr; \
  353. \
  354. asm volatile(ARM64_LSE_ATOMIC_INSN( \
  355. /* LL/SC */ \
  356. " nop\n" \
  357. " nop\n" \
  358. " nop\n" \
  359. __LL_SC_CMPXCHG_DBL(name), \
  360. /* LSE atomics */ \
  361. " casp" #mb "\t%[old1], %[old2], %[new1], %[new2], %[v]\n"\
  362. " eor %[old1], %[old1], %[oldval1]\n" \
  363. " eor %[old2], %[old2], %[oldval2]\n" \
  364. " orr %[old1], %[old1], %[old2]") \
  365. : [old1] "+&r" (x0), [old2] "+&r" (x1), \
  366. [v] "+Q" (*(unsigned long *)ptr) \
  367. : [new1] "r" (x2), [new2] "r" (x3), [ptr] "r" (x4), \
  368. [oldval1] "r" (oldval1), [oldval2] "r" (oldval2) \
  369. : "x30" , ##cl); \
  370. \
  371. return x0; \
  372. }
  373. __CMPXCHG_DBL( , )
  374. __CMPXCHG_DBL(_mb, al, "memory")
  375. #undef __LL_SC_CMPXCHG_DBL
  376. #undef __CMPXCHG_DBL
  377. #endif /* __ASM_ATOMIC_LSE_H */