atomic.h 4.7 KB

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