atomic.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * arch/arm/include/asm/atomic.h
  3. *
  4. * Copyright (C) 1996 Russell King.
  5. * Copyright (C) 2002 Deep Blue Solutions Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #ifndef __ASM_ARM_ATOMIC_H
  12. #define __ASM_ARM_ATOMIC_H
  13. #include <linux/compiler.h>
  14. #include <linux/prefetch.h>
  15. #include <linux/types.h>
  16. #include <linux/irqflags.h>
  17. #include <asm/barrier.h>
  18. #include <asm/cmpxchg.h>
  19. #define ATOMIC_INIT(i) { (i) }
  20. #ifdef __KERNEL__
  21. /*
  22. * On ARM, ordinary assignment (str instruction) doesn't clear the local
  23. * strex/ldrex monitor on some implementations. The reason we can use it for
  24. * atomic_set() is the clrex or dummy strex done on every exception return.
  25. */
  26. #define atomic_read(v) ACCESS_ONCE((v)->counter)
  27. #define atomic_set(v,i) (((v)->counter) = (i))
  28. #if __LINUX_ARM_ARCH__ >= 6
  29. /*
  30. * ARMv6 UP and SMP safe atomic ops. We use load exclusive and
  31. * store exclusive to ensure that these are atomic. We may loop
  32. * to ensure that the update happens.
  33. */
  34. #define ATOMIC_OP(op, c_op, asm_op) \
  35. static inline void atomic_##op(int i, atomic_t *v) \
  36. { \
  37. unsigned long tmp; \
  38. int result; \
  39. \
  40. prefetchw(&v->counter); \
  41. __asm__ __volatile__("@ atomic_" #op "\n" \
  42. "1: ldrex %0, [%3]\n" \
  43. " " #asm_op " %0, %0, %4\n" \
  44. " strex %1, %0, [%3]\n" \
  45. " teq %1, #0\n" \
  46. " bne 1b" \
  47. : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \
  48. : "r" (&v->counter), "Ir" (i) \
  49. : "cc"); \
  50. } \
  51. #define ATOMIC_OP_RETURN(op, c_op, asm_op) \
  52. static inline int atomic_##op##_return(int i, atomic_t *v) \
  53. { \
  54. unsigned long tmp; \
  55. int result; \
  56. \
  57. smp_mb(); \
  58. prefetchw(&v->counter); \
  59. \
  60. __asm__ __volatile__("@ atomic_" #op "_return\n" \
  61. "1: ldrex %0, [%3]\n" \
  62. " " #asm_op " %0, %0, %4\n" \
  63. " strex %1, %0, [%3]\n" \
  64. " teq %1, #0\n" \
  65. " bne 1b" \
  66. : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \
  67. : "r" (&v->counter), "Ir" (i) \
  68. : "cc"); \
  69. \
  70. smp_mb(); \
  71. \
  72. return result; \
  73. }
  74. static inline int atomic_cmpxchg(atomic_t *ptr, int old, int new)
  75. {
  76. int oldval;
  77. unsigned long res;
  78. smp_mb();
  79. prefetchw(&ptr->counter);
  80. do {
  81. __asm__ __volatile__("@ atomic_cmpxchg\n"
  82. "ldrex %1, [%3]\n"
  83. "mov %0, #0\n"
  84. "teq %1, %4\n"
  85. "strexeq %0, %5, [%3]\n"
  86. : "=&r" (res), "=&r" (oldval), "+Qo" (ptr->counter)
  87. : "r" (&ptr->counter), "Ir" (old), "r" (new)
  88. : "cc");
  89. } while (res);
  90. smp_mb();
  91. return oldval;
  92. }
  93. static inline int __atomic_add_unless(atomic_t *v, int a, int u)
  94. {
  95. int oldval, newval;
  96. unsigned long tmp;
  97. smp_mb();
  98. prefetchw(&v->counter);
  99. __asm__ __volatile__ ("@ atomic_add_unless\n"
  100. "1: ldrex %0, [%4]\n"
  101. " teq %0, %5\n"
  102. " beq 2f\n"
  103. " add %1, %0, %6\n"
  104. " strex %2, %1, [%4]\n"
  105. " teq %2, #0\n"
  106. " bne 1b\n"
  107. "2:"
  108. : "=&r" (oldval), "=&r" (newval), "=&r" (tmp), "+Qo" (v->counter)
  109. : "r" (&v->counter), "r" (u), "r" (a)
  110. : "cc");
  111. if (oldval != u)
  112. smp_mb();
  113. return oldval;
  114. }
  115. #else /* ARM_ARCH_6 */
  116. #ifdef CONFIG_SMP
  117. #error SMP not supported on pre-ARMv6 CPUs
  118. #endif
  119. #define ATOMIC_OP(op, c_op, asm_op) \
  120. static inline void atomic_##op(int i, atomic_t *v) \
  121. { \
  122. unsigned long flags; \
  123. \
  124. raw_local_irq_save(flags); \
  125. v->counter c_op i; \
  126. raw_local_irq_restore(flags); \
  127. } \
  128. #define ATOMIC_OP_RETURN(op, c_op, asm_op) \
  129. static inline int atomic_##op##_return(int i, atomic_t *v) \
  130. { \
  131. unsigned long flags; \
  132. int val; \
  133. \
  134. raw_local_irq_save(flags); \
  135. v->counter c_op i; \
  136. val = v->counter; \
  137. raw_local_irq_restore(flags); \
  138. \
  139. return val; \
  140. }
  141. static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
  142. {
  143. int ret;
  144. unsigned long flags;
  145. raw_local_irq_save(flags);
  146. ret = v->counter;
  147. if (likely(ret == old))
  148. v->counter = new;
  149. raw_local_irq_restore(flags);
  150. return ret;
  151. }
  152. static inline int __atomic_add_unless(atomic_t *v, int a, int u)
  153. {
  154. int c, old;
  155. c = atomic_read(v);
  156. while (c != u && (old = atomic_cmpxchg((v), c, c + a)) != c)
  157. c = old;
  158. return c;
  159. }
  160. #endif /* __LINUX_ARM_ARCH__ */
  161. #define ATOMIC_OPS(op, c_op, asm_op) \
  162. ATOMIC_OP(op, c_op, asm_op) \
  163. ATOMIC_OP_RETURN(op, c_op, asm_op)
  164. ATOMIC_OPS(add, +=, add)
  165. ATOMIC_OPS(sub, -=, sub)
  166. #undef ATOMIC_OPS
  167. #undef ATOMIC_OP_RETURN
  168. #undef ATOMIC_OP
  169. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  170. #define atomic_inc(v) atomic_add(1, v)
  171. #define atomic_dec(v) atomic_sub(1, v)
  172. #define atomic_inc_and_test(v) (atomic_add_return(1, v) == 0)
  173. #define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0)
  174. #define atomic_inc_return(v) (atomic_add_return(1, v))
  175. #define atomic_dec_return(v) (atomic_sub_return(1, v))
  176. #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
  177. #define atomic_add_negative(i,v) (atomic_add_return(i, v) < 0)
  178. #ifndef CONFIG_GENERIC_ATOMIC64
  179. typedef struct {
  180. long long counter;
  181. } atomic64_t;
  182. #define ATOMIC64_INIT(i) { (i) }
  183. #ifdef CONFIG_ARM_LPAE
  184. static inline long long atomic64_read(const atomic64_t *v)
  185. {
  186. long long result;
  187. __asm__ __volatile__("@ atomic64_read\n"
  188. " ldrd %0, %H0, [%1]"
  189. : "=&r" (result)
  190. : "r" (&v->counter), "Qo" (v->counter)
  191. );
  192. return result;
  193. }
  194. static inline void atomic64_set(atomic64_t *v, long long i)
  195. {
  196. __asm__ __volatile__("@ atomic64_set\n"
  197. " strd %2, %H2, [%1]"
  198. : "=Qo" (v->counter)
  199. : "r" (&v->counter), "r" (i)
  200. );
  201. }
  202. #else
  203. static inline long long atomic64_read(const atomic64_t *v)
  204. {
  205. long long result;
  206. __asm__ __volatile__("@ atomic64_read\n"
  207. " ldrexd %0, %H0, [%1]"
  208. : "=&r" (result)
  209. : "r" (&v->counter), "Qo" (v->counter)
  210. );
  211. return result;
  212. }
  213. static inline void atomic64_set(atomic64_t *v, long long i)
  214. {
  215. long long tmp;
  216. prefetchw(&v->counter);
  217. __asm__ __volatile__("@ atomic64_set\n"
  218. "1: ldrexd %0, %H0, [%2]\n"
  219. " strexd %0, %3, %H3, [%2]\n"
  220. " teq %0, #0\n"
  221. " bne 1b"
  222. : "=&r" (tmp), "=Qo" (v->counter)
  223. : "r" (&v->counter), "r" (i)
  224. : "cc");
  225. }
  226. #endif
  227. #define ATOMIC64_OP(op, op1, op2) \
  228. static inline void atomic64_##op(long long i, atomic64_t *v) \
  229. { \
  230. long long result; \
  231. unsigned long tmp; \
  232. \
  233. prefetchw(&v->counter); \
  234. __asm__ __volatile__("@ atomic64_" #op "\n" \
  235. "1: ldrexd %0, %H0, [%3]\n" \
  236. " " #op1 " %Q0, %Q0, %Q4\n" \
  237. " " #op2 " %R0, %R0, %R4\n" \
  238. " strexd %1, %0, %H0, [%3]\n" \
  239. " teq %1, #0\n" \
  240. " bne 1b" \
  241. : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \
  242. : "r" (&v->counter), "r" (i) \
  243. : "cc"); \
  244. } \
  245. #define ATOMIC64_OP_RETURN(op, op1, op2) \
  246. static inline long long atomic64_##op##_return(long long i, atomic64_t *v) \
  247. { \
  248. long long result; \
  249. unsigned long tmp; \
  250. \
  251. smp_mb(); \
  252. prefetchw(&v->counter); \
  253. \
  254. __asm__ __volatile__("@ atomic64_" #op "_return\n" \
  255. "1: ldrexd %0, %H0, [%3]\n" \
  256. " " #op1 " %Q0, %Q0, %Q4\n" \
  257. " " #op2 " %R0, %R0, %R4\n" \
  258. " strexd %1, %0, %H0, [%3]\n" \
  259. " teq %1, #0\n" \
  260. " bne 1b" \
  261. : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter) \
  262. : "r" (&v->counter), "r" (i) \
  263. : "cc"); \
  264. \
  265. smp_mb(); \
  266. \
  267. return result; \
  268. }
  269. #define ATOMIC64_OPS(op, op1, op2) \
  270. ATOMIC64_OP(op, op1, op2) \
  271. ATOMIC64_OP_RETURN(op, op1, op2)
  272. ATOMIC64_OPS(add, adds, adc)
  273. ATOMIC64_OPS(sub, subs, sbc)
  274. #undef ATOMIC64_OPS
  275. #undef ATOMIC64_OP_RETURN
  276. #undef ATOMIC64_OP
  277. static inline long long atomic64_cmpxchg(atomic64_t *ptr, long long old,
  278. long long new)
  279. {
  280. long long oldval;
  281. unsigned long res;
  282. smp_mb();
  283. prefetchw(&ptr->counter);
  284. do {
  285. __asm__ __volatile__("@ atomic64_cmpxchg\n"
  286. "ldrexd %1, %H1, [%3]\n"
  287. "mov %0, #0\n"
  288. "teq %1, %4\n"
  289. "teqeq %H1, %H4\n"
  290. "strexdeq %0, %5, %H5, [%3]"
  291. : "=&r" (res), "=&r" (oldval), "+Qo" (ptr->counter)
  292. : "r" (&ptr->counter), "r" (old), "r" (new)
  293. : "cc");
  294. } while (res);
  295. smp_mb();
  296. return oldval;
  297. }
  298. static inline long long atomic64_xchg(atomic64_t *ptr, long long new)
  299. {
  300. long long result;
  301. unsigned long tmp;
  302. smp_mb();
  303. prefetchw(&ptr->counter);
  304. __asm__ __volatile__("@ atomic64_xchg\n"
  305. "1: ldrexd %0, %H0, [%3]\n"
  306. " strexd %1, %4, %H4, [%3]\n"
  307. " teq %1, #0\n"
  308. " bne 1b"
  309. : "=&r" (result), "=&r" (tmp), "+Qo" (ptr->counter)
  310. : "r" (&ptr->counter), "r" (new)
  311. : "cc");
  312. smp_mb();
  313. return result;
  314. }
  315. static inline long long atomic64_dec_if_positive(atomic64_t *v)
  316. {
  317. long long result;
  318. unsigned long tmp;
  319. smp_mb();
  320. prefetchw(&v->counter);
  321. __asm__ __volatile__("@ atomic64_dec_if_positive\n"
  322. "1: ldrexd %0, %H0, [%3]\n"
  323. " subs %Q0, %Q0, #1\n"
  324. " sbc %R0, %R0, #0\n"
  325. " teq %R0, #0\n"
  326. " bmi 2f\n"
  327. " strexd %1, %0, %H0, [%3]\n"
  328. " teq %1, #0\n"
  329. " bne 1b\n"
  330. "2:"
  331. : "=&r" (result), "=&r" (tmp), "+Qo" (v->counter)
  332. : "r" (&v->counter)
  333. : "cc");
  334. smp_mb();
  335. return result;
  336. }
  337. static inline int atomic64_add_unless(atomic64_t *v, long long a, long long u)
  338. {
  339. long long val;
  340. unsigned long tmp;
  341. int ret = 1;
  342. smp_mb();
  343. prefetchw(&v->counter);
  344. __asm__ __volatile__("@ atomic64_add_unless\n"
  345. "1: ldrexd %0, %H0, [%4]\n"
  346. " teq %0, %5\n"
  347. " teqeq %H0, %H5\n"
  348. " moveq %1, #0\n"
  349. " beq 2f\n"
  350. " adds %Q0, %Q0, %Q6\n"
  351. " adc %R0, %R0, %R6\n"
  352. " strexd %2, %0, %H0, [%4]\n"
  353. " teq %2, #0\n"
  354. " bne 1b\n"
  355. "2:"
  356. : "=&r" (val), "+r" (ret), "=&r" (tmp), "+Qo" (v->counter)
  357. : "r" (&v->counter), "r" (u), "r" (a)
  358. : "cc");
  359. if (ret)
  360. smp_mb();
  361. return ret;
  362. }
  363. #define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
  364. #define atomic64_inc(v) atomic64_add(1LL, (v))
  365. #define atomic64_inc_return(v) atomic64_add_return(1LL, (v))
  366. #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
  367. #define atomic64_sub_and_test(a, v) (atomic64_sub_return((a), (v)) == 0)
  368. #define atomic64_dec(v) atomic64_sub(1LL, (v))
  369. #define atomic64_dec_return(v) atomic64_sub_return(1LL, (v))
  370. #define atomic64_dec_and_test(v) (atomic64_dec_return((v)) == 0)
  371. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1LL, 0LL)
  372. #endif /* !CONFIG_GENERIC_ATOMIC64 */
  373. #endif
  374. #endif