atomic.h 4.5 KB

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