atomic.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 arch_atomic_read(v) READ_ONCE((v)->counter)
  16. #define arch_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 arch_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 arch_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. : "di" (i), "2" (arch_atomic_read(v))); \
  44. return t; \
  45. }
  46. #define ATOMIC_FETCH_OP(op, c_op, asm_op) \
  47. static inline int arch_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. : "di" (i), "2" (arch_atomic_read(v))); \
  58. return tmp; \
  59. }
  60. #else
  61. #define ATOMIC_OP_RETURN(op, c_op, asm_op) \
  62. static inline int arch_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 arch_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. #define arch_atomic_add_return arch_atomic_add_return
  94. #define arch_atomic_sub_return arch_atomic_sub_return
  95. #define arch_atomic_fetch_add arch_atomic_fetch_add
  96. #define arch_atomic_fetch_sub arch_atomic_fetch_sub
  97. #undef ATOMIC_OPS
  98. #define ATOMIC_OPS(op, c_op, asm_op) \
  99. ATOMIC_OP(op, c_op, asm_op) \
  100. ATOMIC_FETCH_OP(op, c_op, asm_op)
  101. ATOMIC_OPS(and, &=, and)
  102. ATOMIC_OPS(or, |=, or)
  103. ATOMIC_OPS(xor, ^=, eor)
  104. #define arch_atomic_fetch_and arch_atomic_fetch_and
  105. #define arch_atomic_fetch_or arch_atomic_fetch_or
  106. #define arch_atomic_fetch_xor arch_atomic_fetch_xor
  107. #undef ATOMIC_OPS
  108. #undef ATOMIC_FETCH_OP
  109. #undef ATOMIC_OP_RETURN
  110. #undef ATOMIC_OP
  111. static inline void arch_atomic_inc(atomic_t *v)
  112. {
  113. __asm__ __volatile__("addql #1,%0" : "+m" (*v));
  114. }
  115. #define arch_atomic_inc arch_atomic_inc
  116. static inline void arch_atomic_dec(atomic_t *v)
  117. {
  118. __asm__ __volatile__("subql #1,%0" : "+m" (*v));
  119. }
  120. #define arch_atomic_dec arch_atomic_dec
  121. static inline int arch_atomic_dec_and_test(atomic_t *v)
  122. {
  123. char c;
  124. __asm__ __volatile__("subql #1,%1; seq %0" : "=d" (c), "+m" (*v));
  125. return c != 0;
  126. }
  127. #define arch_atomic_dec_and_test arch_atomic_dec_and_test
  128. static inline int arch_atomic_dec_and_test_lt(atomic_t *v)
  129. {
  130. char c;
  131. __asm__ __volatile__(
  132. "subql #1,%1; slt %0"
  133. : "=d" (c), "=m" (*v)
  134. : "m" (*v));
  135. return c != 0;
  136. }
  137. static inline int arch_atomic_inc_and_test(atomic_t *v)
  138. {
  139. char c;
  140. __asm__ __volatile__("addql #1,%1; seq %0" : "=d" (c), "+m" (*v));
  141. return c != 0;
  142. }
  143. #define arch_atomic_inc_and_test arch_atomic_inc_and_test
  144. #ifndef CONFIG_RMW_INSNS
  145. static inline int arch_atomic_cmpxchg(atomic_t *v, int old, int new)
  146. {
  147. unsigned long flags;
  148. int prev;
  149. local_irq_save(flags);
  150. prev = arch_atomic_read(v);
  151. if (prev == old)
  152. arch_atomic_set(v, new);
  153. local_irq_restore(flags);
  154. return prev;
  155. }
  156. #define arch_atomic_cmpxchg arch_atomic_cmpxchg
  157. static inline int arch_atomic_xchg(atomic_t *v, int new)
  158. {
  159. unsigned long flags;
  160. int prev;
  161. local_irq_save(flags);
  162. prev = arch_atomic_read(v);
  163. arch_atomic_set(v, new);
  164. local_irq_restore(flags);
  165. return prev;
  166. }
  167. #define arch_atomic_xchg arch_atomic_xchg
  168. #endif /* !CONFIG_RMW_INSNS */
  169. static inline int arch_atomic_sub_and_test(int i, atomic_t *v)
  170. {
  171. char c;
  172. __asm__ __volatile__("subl %2,%1; seq %0"
  173. : "=d" (c), "+m" (*v)
  174. : ASM_DI (i));
  175. return c != 0;
  176. }
  177. #define arch_atomic_sub_and_test arch_atomic_sub_and_test
  178. static inline int arch_atomic_add_negative(int i, atomic_t *v)
  179. {
  180. char c;
  181. __asm__ __volatile__("addl %2,%1; smi %0"
  182. : "=d" (c), "+m" (*v)
  183. : ASM_DI (i));
  184. return c != 0;
  185. }
  186. #define arch_atomic_add_negative arch_atomic_add_negative
  187. #endif /* __ARCH_M68K_ATOMIC __ */