atomic.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/config.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. u32 oldval, res;
  71. do {
  72. __asm__ __volatile__("@ atomic_cmpxchg\n"
  73. "ldrex %1, [%2]\n"
  74. "teq %1, %3\n"
  75. "strexeq %0, %4, [%2]\n"
  76. : "=&r" (res), "=&r" (oldval)
  77. : "r" (&ptr->counter), "Ir" (old), "r" (new)
  78. : "cc");
  79. } while (res);
  80. return oldval;
  81. }
  82. static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
  83. {
  84. unsigned long tmp, tmp2;
  85. __asm__ __volatile__("@ atomic_clear_mask\n"
  86. "1: ldrex %0, %2\n"
  87. " bic %0, %0, %3\n"
  88. " strex %1, %0, %2\n"
  89. " teq %1, #0\n"
  90. " bne 1b"
  91. : "=&r" (tmp), "=&r" (tmp2)
  92. : "r" (addr), "Ir" (mask)
  93. : "cc");
  94. }
  95. #else /* ARM_ARCH_6 */
  96. #include <asm/system.h>
  97. #ifdef CONFIG_SMP
  98. #error SMP not supported on pre-ARMv6 CPUs
  99. #endif
  100. #define atomic_set(v,i) (((v)->counter) = (i))
  101. static inline int atomic_add_return(int i, atomic_t *v)
  102. {
  103. unsigned long flags;
  104. int val;
  105. local_irq_save(flags);
  106. val = v->counter;
  107. v->counter = val += i;
  108. local_irq_restore(flags);
  109. return val;
  110. }
  111. static inline int atomic_sub_return(int i, atomic_t *v)
  112. {
  113. unsigned long flags;
  114. int val;
  115. local_irq_save(flags);
  116. val = v->counter;
  117. v->counter = val -= i;
  118. local_irq_restore(flags);
  119. return val;
  120. }
  121. static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
  122. {
  123. int ret;
  124. unsigned long flags;
  125. local_irq_save(flags);
  126. ret = v->counter;
  127. if (likely(ret == old))
  128. v->counter = new;
  129. local_irq_restore(flags);
  130. return ret;
  131. }
  132. static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
  133. {
  134. unsigned long flags;
  135. local_irq_save(flags);
  136. *addr &= ~mask;
  137. local_irq_restore(flags);
  138. }
  139. #endif /* __LINUX_ARM_ARCH__ */
  140. static inline int atomic_add_unless(atomic_t *v, int a, int u)
  141. {
  142. int c, old;
  143. c = atomic_read(v);
  144. while (c != u && (old = atomic_cmpxchg((v), c, c + a)) != c)
  145. c = old;
  146. return c != u;
  147. }
  148. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  149. #define atomic_add(i, v) (void) atomic_add_return(i, v)
  150. #define atomic_inc(v) (void) atomic_add_return(1, v)
  151. #define atomic_sub(i, v) (void) atomic_sub_return(i, v)
  152. #define atomic_dec(v) (void) atomic_sub_return(1, v)
  153. #define atomic_inc_and_test(v) (atomic_add_return(1, v) == 0)
  154. #define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0)
  155. #define atomic_inc_return(v) (atomic_add_return(1, v))
  156. #define atomic_dec_return(v) (atomic_sub_return(1, v))
  157. #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
  158. #define atomic_add_negative(i,v) (atomic_add_return(i, v) < 0)
  159. /* Atomic operations are already serializing on ARM */
  160. #define smp_mb__before_atomic_dec() barrier()
  161. #define smp_mb__after_atomic_dec() barrier()
  162. #define smp_mb__before_atomic_inc() barrier()
  163. #define smp_mb__after_atomic_inc() barrier()
  164. #endif
  165. #endif