atomic.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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_H
  21. #define __ASM_ATOMIC_H
  22. #include <linux/compiler.h>
  23. #include <linux/types.h>
  24. #include <asm/barrier.h>
  25. #include <asm/cmpxchg.h>
  26. #define ATOMIC_INIT(i) { (i) }
  27. #ifdef __KERNEL__
  28. /*
  29. * On ARM, ordinary assignment (str instruction) doesn't clear the local
  30. * strex/ldrex monitor on some implementations. The reason we can use it for
  31. * atomic_set() is the clrex or dummy strex done on every exception return.
  32. */
  33. #define atomic_read(v) (*(volatile int *)&(v)->counter)
  34. #define atomic_set(v,i) (((v)->counter) = (i))
  35. /*
  36. * AArch64 UP and SMP safe atomic ops. We use load exclusive and
  37. * store exclusive to ensure that these are atomic. We may loop
  38. * to ensure that the update happens.
  39. */
  40. static inline void atomic_add(int i, atomic_t *v)
  41. {
  42. unsigned long tmp;
  43. int result;
  44. asm volatile("// atomic_add\n"
  45. "1: ldxr %w0, [%3]\n"
  46. " add %w0, %w0, %w4\n"
  47. " stxr %w1, %w0, [%3]\n"
  48. " cbnz %w1, 1b"
  49. : "=&r" (result), "=&r" (tmp), "+o" (v->counter)
  50. : "r" (&v->counter), "Ir" (i)
  51. : "cc");
  52. }
  53. static inline int atomic_add_return(int i, atomic_t *v)
  54. {
  55. unsigned long tmp;
  56. int result;
  57. asm volatile("// atomic_add_return\n"
  58. "1: ldaxr %w0, [%3]\n"
  59. " add %w0, %w0, %w4\n"
  60. " stlxr %w1, %w0, [%3]\n"
  61. " cbnz %w1, 1b"
  62. : "=&r" (result), "=&r" (tmp), "+o" (v->counter)
  63. : "r" (&v->counter), "Ir" (i)
  64. : "cc");
  65. return result;
  66. }
  67. static inline void atomic_sub(int i, atomic_t *v)
  68. {
  69. unsigned long tmp;
  70. int result;
  71. asm volatile("// atomic_sub\n"
  72. "1: ldxr %w0, [%3]\n"
  73. " sub %w0, %w0, %w4\n"
  74. " stxr %w1, %w0, [%3]\n"
  75. " cbnz %w1, 1b"
  76. : "=&r" (result), "=&r" (tmp), "+o" (v->counter)
  77. : "r" (&v->counter), "Ir" (i)
  78. : "cc");
  79. }
  80. static inline int atomic_sub_return(int i, atomic_t *v)
  81. {
  82. unsigned long tmp;
  83. int result;
  84. asm volatile("// atomic_sub_return\n"
  85. "1: ldaxr %w0, [%3]\n"
  86. " sub %w0, %w0, %w4\n"
  87. " stlxr %w1, %w0, [%3]\n"
  88. " cbnz %w1, 1b"
  89. : "=&r" (result), "=&r" (tmp), "+o" (v->counter)
  90. : "r" (&v->counter), "Ir" (i)
  91. : "cc");
  92. return result;
  93. }
  94. static inline int atomic_cmpxchg(atomic_t *ptr, int old, int new)
  95. {
  96. unsigned long tmp;
  97. int oldval;
  98. asm volatile("// atomic_cmpxchg\n"
  99. "1: ldaxr %w1, [%3]\n"
  100. " cmp %w1, %w4\n"
  101. " b.ne 2f\n"
  102. " stlxr %w0, %w5, [%3]\n"
  103. " cbnz %w0, 1b\n"
  104. "2:"
  105. : "=&r" (tmp), "=&r" (oldval), "+o" (ptr->counter)
  106. : "r" (&ptr->counter), "Ir" (old), "r" (new)
  107. : "cc");
  108. return oldval;
  109. }
  110. static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
  111. {
  112. unsigned long tmp, tmp2;
  113. asm volatile("// atomic_clear_mask\n"
  114. "1: ldxr %0, [%3]\n"
  115. " bic %0, %0, %4\n"
  116. " stxr %w1, %0, [%3]\n"
  117. " cbnz %w1, 1b"
  118. : "=&r" (tmp), "=&r" (tmp2), "+o" (*addr)
  119. : "r" (addr), "Ir" (mask)
  120. : "cc");
  121. }
  122. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  123. static inline int __atomic_add_unless(atomic_t *v, int a, int u)
  124. {
  125. int c, old;
  126. c = atomic_read(v);
  127. while (c != u && (old = atomic_cmpxchg((v), c, c + a)) != c)
  128. c = old;
  129. return c;
  130. }
  131. #define atomic_inc(v) atomic_add(1, v)
  132. #define atomic_dec(v) atomic_sub(1, v)
  133. #define atomic_inc_and_test(v) (atomic_add_return(1, v) == 0)
  134. #define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0)
  135. #define atomic_inc_return(v) (atomic_add_return(1, v))
  136. #define atomic_dec_return(v) (atomic_sub_return(1, v))
  137. #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
  138. #define atomic_add_negative(i,v) (atomic_add_return(i, v) < 0)
  139. #define smp_mb__before_atomic_dec() smp_mb()
  140. #define smp_mb__after_atomic_dec() smp_mb()
  141. #define smp_mb__before_atomic_inc() smp_mb()
  142. #define smp_mb__after_atomic_inc() smp_mb()
  143. /*
  144. * 64-bit atomic operations.
  145. */
  146. #define ATOMIC64_INIT(i) { (i) }
  147. #define atomic64_read(v) (*(volatile long long *)&(v)->counter)
  148. #define atomic64_set(v,i) (((v)->counter) = (i))
  149. static inline void atomic64_add(u64 i, atomic64_t *v)
  150. {
  151. long result;
  152. unsigned long tmp;
  153. asm volatile("// atomic64_add\n"
  154. "1: ldxr %0, [%3]\n"
  155. " add %0, %0, %4\n"
  156. " stxr %w1, %0, [%3]\n"
  157. " cbnz %w1, 1b"
  158. : "=&r" (result), "=&r" (tmp), "+o" (v->counter)
  159. : "r" (&v->counter), "Ir" (i)
  160. : "cc");
  161. }
  162. static inline long atomic64_add_return(long i, atomic64_t *v)
  163. {
  164. long result;
  165. unsigned long tmp;
  166. asm volatile("// atomic64_add_return\n"
  167. "1: ldaxr %0, [%3]\n"
  168. " add %0, %0, %4\n"
  169. " stlxr %w1, %0, [%3]\n"
  170. " cbnz %w1, 1b"
  171. : "=&r" (result), "=&r" (tmp), "+o" (v->counter)
  172. : "r" (&v->counter), "Ir" (i)
  173. : "cc");
  174. return result;
  175. }
  176. static inline void atomic64_sub(u64 i, atomic64_t *v)
  177. {
  178. long result;
  179. unsigned long tmp;
  180. asm volatile("// atomic64_sub\n"
  181. "1: ldxr %0, [%3]\n"
  182. " sub %0, %0, %4\n"
  183. " stxr %w1, %0, [%3]\n"
  184. " cbnz %w1, 1b"
  185. : "=&r" (result), "=&r" (tmp), "+o" (v->counter)
  186. : "r" (&v->counter), "Ir" (i)
  187. : "cc");
  188. }
  189. static inline long atomic64_sub_return(long i, atomic64_t *v)
  190. {
  191. long result;
  192. unsigned long tmp;
  193. asm volatile("// atomic64_sub_return\n"
  194. "1: ldaxr %0, [%3]\n"
  195. " sub %0, %0, %4\n"
  196. " stlxr %w1, %0, [%3]\n"
  197. " cbnz %w1, 1b"
  198. : "=&r" (result), "=&r" (tmp), "+o" (v->counter)
  199. : "r" (&v->counter), "Ir" (i)
  200. : "cc");
  201. return result;
  202. }
  203. static inline long atomic64_cmpxchg(atomic64_t *ptr, long old, long new)
  204. {
  205. long oldval;
  206. unsigned long res;
  207. asm volatile("// atomic64_cmpxchg\n"
  208. "1: ldaxr %1, [%3]\n"
  209. " cmp %1, %4\n"
  210. " b.ne 2f\n"
  211. " stlxr %w0, %5, [%3]\n"
  212. " cbnz %w0, 1b\n"
  213. "2:"
  214. : "=&r" (res), "=&r" (oldval), "+o" (ptr->counter)
  215. : "r" (&ptr->counter), "Ir" (old), "r" (new)
  216. : "cc");
  217. return oldval;
  218. }
  219. #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
  220. static inline long atomic64_dec_if_positive(atomic64_t *v)
  221. {
  222. long result;
  223. unsigned long tmp;
  224. asm volatile("// atomic64_dec_if_positive\n"
  225. "1: ldaxr %0, [%3]\n"
  226. " subs %0, %0, #1\n"
  227. " b.mi 2f\n"
  228. " stlxr %w1, %0, [%3]\n"
  229. " cbnz %w1, 1b\n"
  230. "2:"
  231. : "=&r" (result), "=&r" (tmp), "+o" (v->counter)
  232. : "r" (&v->counter)
  233. : "cc");
  234. return result;
  235. }
  236. static inline int atomic64_add_unless(atomic64_t *v, long a, long u)
  237. {
  238. long c, old;
  239. c = atomic64_read(v);
  240. while (c != u && (old = atomic64_cmpxchg((v), c, c + a)) != c)
  241. c = old;
  242. return c != u;
  243. }
  244. #define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
  245. #define atomic64_inc(v) atomic64_add(1LL, (v))
  246. #define atomic64_inc_return(v) atomic64_add_return(1LL, (v))
  247. #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
  248. #define atomic64_sub_and_test(a, v) (atomic64_sub_return((a), (v)) == 0)
  249. #define atomic64_dec(v) atomic64_sub(1LL, (v))
  250. #define atomic64_dec_return(v) atomic64_sub_return(1LL, (v))
  251. #define atomic64_dec_and_test(v) (atomic64_dec_return((v)) == 0)
  252. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1LL, 0LL)
  253. #endif
  254. #endif