atomic.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #ifndef __ARCH_M68K_ATOMIC__
  2. #define __ARCH_M68K_ATOMIC__
  3. #include <linux/types.h>
  4. #include <linux/irqflags.h>
  5. #include <asm/cmpxchg.h>
  6. #include <asm/barrier.h>
  7. /*
  8. * Atomic operations that C can't guarantee us. Useful for
  9. * resource counting etc..
  10. */
  11. /*
  12. * We do not have SMP m68k systems, so we don't have to deal with that.
  13. */
  14. #define ATOMIC_INIT(i) { (i) }
  15. #define atomic_read(v) READ_ONCE((v)->counter)
  16. #define atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
  17. /*
  18. * The ColdFire parts cannot do some immediate to memory operations,
  19. * so for them we do not specify the "i" asm constraint.
  20. */
  21. #ifdef CONFIG_COLDFIRE
  22. #define ASM_DI "d"
  23. #else
  24. #define ASM_DI "di"
  25. #endif
  26. #define ATOMIC_OP(op, c_op, asm_op) \
  27. static inline void atomic_##op(int i, atomic_t *v) \
  28. { \
  29. __asm__ __volatile__(#asm_op "l %1,%0" : "+m" (*v) : ASM_DI (i));\
  30. } \
  31. #ifdef CONFIG_RMW_INSNS
  32. #define ATOMIC_OP_RETURN(op, c_op, asm_op) \
  33. static inline int atomic_##op##_return(int i, atomic_t *v) \
  34. { \
  35. int t, tmp; \
  36. \
  37. __asm__ __volatile__( \
  38. "1: movel %2,%1\n" \
  39. " " #asm_op "l %3,%1\n" \
  40. " casl %2,%1,%0\n" \
  41. " jne 1b" \
  42. : "+m" (*v), "=&d" (t), "=&d" (tmp) \
  43. : "g" (i), "2" (atomic_read(v))); \
  44. return t; \
  45. }
  46. #define ATOMIC_FETCH_OP(op, c_op, asm_op) \
  47. static inline int atomic_fetch_##op(int i, atomic_t *v) \
  48. { \
  49. int t, tmp; \
  50. \
  51. __asm__ __volatile__( \
  52. "1: movel %2,%1\n" \
  53. " " #asm_op "l %3,%1\n" \
  54. " casl %2,%1,%0\n" \
  55. " jne 1b" \
  56. : "+m" (*v), "=&d" (t), "=&d" (tmp) \
  57. : "g" (i), "2" (atomic_read(v))); \
  58. return tmp; \
  59. }
  60. #else
  61. #define ATOMIC_OP_RETURN(op, c_op, asm_op) \
  62. static inline int atomic_##op##_return(int i, atomic_t * v) \
  63. { \
  64. unsigned long flags; \
  65. int t; \
  66. \
  67. local_irq_save(flags); \
  68. t = (v->counter c_op i); \
  69. local_irq_restore(flags); \
  70. \
  71. return t; \
  72. }
  73. #define ATOMIC_FETCH_OP(op, c_op, asm_op) \
  74. static inline int atomic_fetch_##op(int i, atomic_t * v) \
  75. { \
  76. unsigned long flags; \
  77. int t; \
  78. \
  79. local_irq_save(flags); \
  80. t = v->counter; \
  81. v->counter c_op i; \
  82. local_irq_restore(flags); \
  83. \
  84. return t; \
  85. }
  86. #endif /* CONFIG_RMW_INSNS */
  87. #define ATOMIC_OPS(op, c_op, asm_op) \
  88. ATOMIC_OP(op, c_op, asm_op) \
  89. ATOMIC_OP_RETURN(op, c_op, asm_op) \
  90. ATOMIC_FETCH_OP(op, c_op, asm_op)
  91. ATOMIC_OPS(add, +=, add)
  92. ATOMIC_OPS(sub, -=, sub)
  93. #undef ATOMIC_OPS
  94. #define ATOMIC_OPS(op, c_op, asm_op) \
  95. ATOMIC_OP(op, c_op, asm_op) \
  96. ATOMIC_FETCH_OP(op, c_op, asm_op)
  97. ATOMIC_OPS(and, &=, and)
  98. ATOMIC_OPS(or, |=, or)
  99. ATOMIC_OPS(xor, ^=, eor)
  100. #undef ATOMIC_OPS
  101. #undef ATOMIC_FETCH_OP
  102. #undef ATOMIC_OP_RETURN
  103. #undef ATOMIC_OP
  104. static inline void atomic_inc(atomic_t *v)
  105. {
  106. __asm__ __volatile__("addql #1,%0" : "+m" (*v));
  107. }
  108. static inline void atomic_dec(atomic_t *v)
  109. {
  110. __asm__ __volatile__("subql #1,%0" : "+m" (*v));
  111. }
  112. static inline int atomic_dec_and_test(atomic_t *v)
  113. {
  114. char c;
  115. __asm__ __volatile__("subql #1,%1; seq %0" : "=d" (c), "+m" (*v));
  116. return c != 0;
  117. }
  118. static inline int atomic_dec_and_test_lt(atomic_t *v)
  119. {
  120. char c;
  121. __asm__ __volatile__(
  122. "subql #1,%1; slt %0"
  123. : "=d" (c), "=m" (*v)
  124. : "m" (*v));
  125. return c != 0;
  126. }
  127. static inline int atomic_inc_and_test(atomic_t *v)
  128. {
  129. char c;
  130. __asm__ __volatile__("addql #1,%1; seq %0" : "=d" (c), "+m" (*v));
  131. return c != 0;
  132. }
  133. #ifdef CONFIG_RMW_INSNS
  134. #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
  135. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  136. #else /* !CONFIG_RMW_INSNS */
  137. static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
  138. {
  139. unsigned long flags;
  140. int prev;
  141. local_irq_save(flags);
  142. prev = atomic_read(v);
  143. if (prev == old)
  144. atomic_set(v, new);
  145. local_irq_restore(flags);
  146. return prev;
  147. }
  148. static inline int atomic_xchg(atomic_t *v, int new)
  149. {
  150. unsigned long flags;
  151. int prev;
  152. local_irq_save(flags);
  153. prev = atomic_read(v);
  154. atomic_set(v, new);
  155. local_irq_restore(flags);
  156. return prev;
  157. }
  158. #endif /* !CONFIG_RMW_INSNS */
  159. #define atomic_dec_return(v) atomic_sub_return(1, (v))
  160. #define atomic_inc_return(v) atomic_add_return(1, (v))
  161. static inline int atomic_sub_and_test(int i, atomic_t *v)
  162. {
  163. char c;
  164. __asm__ __volatile__("subl %2,%1; seq %0"
  165. : "=d" (c), "+m" (*v)
  166. : ASM_DI (i));
  167. return c != 0;
  168. }
  169. static inline int atomic_add_negative(int i, atomic_t *v)
  170. {
  171. char c;
  172. __asm__ __volatile__("addl %2,%1; smi %0"
  173. : "=d" (c), "+m" (*v)
  174. : ASM_DI (i));
  175. return c != 0;
  176. }
  177. static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
  178. {
  179. int c, old;
  180. c = atomic_read(v);
  181. for (;;) {
  182. if (unlikely(c == (u)))
  183. break;
  184. old = atomic_cmpxchg((v), c, c + (a));
  185. if (likely(old == c))
  186. break;
  187. c = old;
  188. }
  189. return c;
  190. }
  191. #endif /* __ARCH_M68K_ATOMIC __ */