atomic.h 5.1 KB

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