atomic.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 <asm/system.h>
  16. #define ATOMIC_INIT(i) { (i) }
  17. #ifdef __KERNEL__
  18. /*
  19. * On ARM, ordinary assignment (str instruction) doesn't clear the local
  20. * strex/ldrex monitor on some implementations. The reason we can use it for
  21. * atomic_set() is the clrex or dummy strex done on every exception return.
  22. */
  23. #define atomic_read(v) ((v)->counter)
  24. #define atomic_set(v,i) (((v)->counter) = (i))
  25. #if __LINUX_ARM_ARCH__ >= 6
  26. /*
  27. * ARMv6 UP and SMP safe atomic ops. We use load exclusive and
  28. * store exclusive to ensure that these are atomic. We may loop
  29. * to ensure that the update happens.
  30. */
  31. static inline void atomic_add(int i, atomic_t *v)
  32. {
  33. unsigned long tmp;
  34. int result;
  35. __asm__ __volatile__("@ atomic_add\n"
  36. "1: ldrex %0, [%2]\n"
  37. " add %0, %0, %3\n"
  38. " strex %1, %0, [%2]\n"
  39. " teq %1, #0\n"
  40. " bne 1b"
  41. : "=&r" (result), "=&r" (tmp)
  42. : "r" (&v->counter), "Ir" (i)
  43. : "cc");
  44. }
  45. static inline int atomic_add_return(int i, atomic_t *v)
  46. {
  47. unsigned long tmp;
  48. int result;
  49. smp_mb();
  50. __asm__ __volatile__("@ atomic_add_return\n"
  51. "1: ldrex %0, [%2]\n"
  52. " add %0, %0, %3\n"
  53. " strex %1, %0, [%2]\n"
  54. " teq %1, #0\n"
  55. " bne 1b"
  56. : "=&r" (result), "=&r" (tmp)
  57. : "r" (&v->counter), "Ir" (i)
  58. : "cc");
  59. smp_mb();
  60. return result;
  61. }
  62. static inline void atomic_sub(int i, atomic_t *v)
  63. {
  64. unsigned long tmp;
  65. int result;
  66. __asm__ __volatile__("@ atomic_sub\n"
  67. "1: ldrex %0, [%2]\n"
  68. " sub %0, %0, %3\n"
  69. " strex %1, %0, [%2]\n"
  70. " teq %1, #0\n"
  71. " bne 1b"
  72. : "=&r" (result), "=&r" (tmp)
  73. : "r" (&v->counter), "Ir" (i)
  74. : "cc");
  75. }
  76. static inline int atomic_sub_return(int i, atomic_t *v)
  77. {
  78. unsigned long tmp;
  79. int result;
  80. smp_mb();
  81. __asm__ __volatile__("@ atomic_sub_return\n"
  82. "1: ldrex %0, [%2]\n"
  83. " sub %0, %0, %3\n"
  84. " strex %1, %0, [%2]\n"
  85. " teq %1, #0\n"
  86. " bne 1b"
  87. : "=&r" (result), "=&r" (tmp)
  88. : "r" (&v->counter), "Ir" (i)
  89. : "cc");
  90. smp_mb();
  91. return result;
  92. }
  93. static inline int atomic_cmpxchg(atomic_t *ptr, int old, int new)
  94. {
  95. unsigned long oldval, res;
  96. smp_mb();
  97. do {
  98. __asm__ __volatile__("@ atomic_cmpxchg\n"
  99. "ldrex %1, [%2]\n"
  100. "mov %0, #0\n"
  101. "teq %1, %3\n"
  102. "strexeq %0, %4, [%2]\n"
  103. : "=&r" (res), "=&r" (oldval)
  104. : "r" (&ptr->counter), "Ir" (old), "r" (new)
  105. : "cc");
  106. } while (res);
  107. smp_mb();
  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: ldrex %0, [%2]\n"
  115. " bic %0, %0, %3\n"
  116. " strex %1, %0, [%2]\n"
  117. " teq %1, #0\n"
  118. " bne 1b"
  119. : "=&r" (tmp), "=&r" (tmp2)
  120. : "r" (addr), "Ir" (mask)
  121. : "cc");
  122. }
  123. #else /* ARM_ARCH_6 */
  124. #ifdef CONFIG_SMP
  125. #error SMP not supported on pre-ARMv6 CPUs
  126. #endif
  127. static inline int atomic_add_return(int i, atomic_t *v)
  128. {
  129. unsigned long flags;
  130. int val;
  131. raw_local_irq_save(flags);
  132. val = v->counter;
  133. v->counter = val += i;
  134. raw_local_irq_restore(flags);
  135. return val;
  136. }
  137. #define atomic_add(i, v) (void) atomic_add_return(i, v)
  138. static inline int atomic_sub_return(int i, atomic_t *v)
  139. {
  140. unsigned long flags;
  141. int val;
  142. raw_local_irq_save(flags);
  143. val = v->counter;
  144. v->counter = val -= i;
  145. raw_local_irq_restore(flags);
  146. return val;
  147. }
  148. #define atomic_sub(i, v) (void) atomic_sub_return(i, v)
  149. static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
  150. {
  151. int ret;
  152. unsigned long flags;
  153. raw_local_irq_save(flags);
  154. ret = v->counter;
  155. if (likely(ret == old))
  156. v->counter = new;
  157. raw_local_irq_restore(flags);
  158. return ret;
  159. }
  160. static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
  161. {
  162. unsigned long flags;
  163. raw_local_irq_save(flags);
  164. *addr &= ~mask;
  165. raw_local_irq_restore(flags);
  166. }
  167. #endif /* __LINUX_ARM_ARCH__ */
  168. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  169. static inline int atomic_add_unless(atomic_t *v, int a, int u)
  170. {
  171. int c, old;
  172. c = atomic_read(v);
  173. while (c != u && (old = atomic_cmpxchg((v), c, c + a)) != c)
  174. c = old;
  175. return c != u;
  176. }
  177. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  178. #define atomic_inc(v) atomic_add(1, v)
  179. #define atomic_dec(v) atomic_sub(1, v)
  180. #define atomic_inc_and_test(v) (atomic_add_return(1, v) == 0)
  181. #define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0)
  182. #define atomic_inc_return(v) (atomic_add_return(1, v))
  183. #define atomic_dec_return(v) (atomic_sub_return(1, v))
  184. #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
  185. #define atomic_add_negative(i,v) (atomic_add_return(i, v) < 0)
  186. #define smp_mb__before_atomic_dec() smp_mb()
  187. #define smp_mb__after_atomic_dec() smp_mb()
  188. #define smp_mb__before_atomic_inc() smp_mb()
  189. #define smp_mb__after_atomic_inc() smp_mb()
  190. #include <asm-generic/atomic-long.h>
  191. #endif
  192. #endif