atomic.h 9.4 KB

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