atomic.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_M32R_ATOMIC_H
  3. #define _ASM_M32R_ATOMIC_H
  4. /*
  5. * linux/include/asm-m32r/atomic.h
  6. *
  7. * M32R version:
  8. * Copyright (C) 2001, 2002 Hitoshi Yamamoto
  9. * Copyright (C) 2004 Hirokazu Takata <takata at linux-m32r.org>
  10. */
  11. #include <linux/types.h>
  12. #include <asm/assembler.h>
  13. #include <asm/cmpxchg.h>
  14. #include <asm/dcache_clear.h>
  15. #include <asm/barrier.h>
  16. /*
  17. * Atomic operations that C can't guarantee us. Useful for
  18. * resource counting etc..
  19. */
  20. #define ATOMIC_INIT(i) { (i) }
  21. /**
  22. * atomic_read - read atomic variable
  23. * @v: pointer of type atomic_t
  24. *
  25. * Atomically reads the value of @v.
  26. */
  27. #define atomic_read(v) READ_ONCE((v)->counter)
  28. /**
  29. * atomic_set - set atomic variable
  30. * @v: pointer of type atomic_t
  31. * @i: required value
  32. *
  33. * Atomically sets the value of @v to @i.
  34. */
  35. #define atomic_set(v,i) WRITE_ONCE(((v)->counter), (i))
  36. #ifdef CONFIG_CHIP_M32700_TS1
  37. #define __ATOMIC_CLOBBER , "r4"
  38. #else
  39. #define __ATOMIC_CLOBBER
  40. #endif
  41. #define ATOMIC_OP(op) \
  42. static __inline__ void atomic_##op(int i, atomic_t *v) \
  43. { \
  44. unsigned long flags; \
  45. int result; \
  46. \
  47. local_irq_save(flags); \
  48. __asm__ __volatile__ ( \
  49. "# atomic_" #op " \n\t" \
  50. DCACHE_CLEAR("%0", "r4", "%1") \
  51. M32R_LOCK" %0, @%1; \n\t" \
  52. #op " %0, %2; \n\t" \
  53. M32R_UNLOCK" %0, @%1; \n\t" \
  54. : "=&r" (result) \
  55. : "r" (&v->counter), "r" (i) \
  56. : "memory" \
  57. __ATOMIC_CLOBBER \
  58. ); \
  59. local_irq_restore(flags); \
  60. } \
  61. #define ATOMIC_OP_RETURN(op) \
  62. static __inline__ int atomic_##op##_return(int i, atomic_t *v) \
  63. { \
  64. unsigned long flags; \
  65. int result; \
  66. \
  67. local_irq_save(flags); \
  68. __asm__ __volatile__ ( \
  69. "# atomic_" #op "_return \n\t" \
  70. DCACHE_CLEAR("%0", "r4", "%1") \
  71. M32R_LOCK" %0, @%1; \n\t" \
  72. #op " %0, %2; \n\t" \
  73. M32R_UNLOCK" %0, @%1; \n\t" \
  74. : "=&r" (result) \
  75. : "r" (&v->counter), "r" (i) \
  76. : "memory" \
  77. __ATOMIC_CLOBBER \
  78. ); \
  79. local_irq_restore(flags); \
  80. \
  81. return result; \
  82. }
  83. #define ATOMIC_FETCH_OP(op) \
  84. static __inline__ int atomic_fetch_##op(int i, atomic_t *v) \
  85. { \
  86. unsigned long flags; \
  87. int result, val; \
  88. \
  89. local_irq_save(flags); \
  90. __asm__ __volatile__ ( \
  91. "# atomic_fetch_" #op " \n\t" \
  92. DCACHE_CLEAR("%0", "r4", "%2") \
  93. M32R_LOCK" %1, @%2; \n\t" \
  94. "mv %0, %1 \n\t" \
  95. #op " %1, %3; \n\t" \
  96. M32R_UNLOCK" %1, @%2; \n\t" \
  97. : "=&r" (result), "=&r" (val) \
  98. : "r" (&v->counter), "r" (i) \
  99. : "memory" \
  100. __ATOMIC_CLOBBER \
  101. ); \
  102. local_irq_restore(flags); \
  103. \
  104. return result; \
  105. }
  106. #define ATOMIC_OPS(op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op) ATOMIC_FETCH_OP(op)
  107. ATOMIC_OPS(add)
  108. ATOMIC_OPS(sub)
  109. #undef ATOMIC_OPS
  110. #define ATOMIC_OPS(op) ATOMIC_OP(op) ATOMIC_FETCH_OP(op)
  111. ATOMIC_OPS(and)
  112. ATOMIC_OPS(or)
  113. ATOMIC_OPS(xor)
  114. #undef ATOMIC_OPS
  115. #undef ATOMIC_FETCH_OP
  116. #undef ATOMIC_OP_RETURN
  117. #undef ATOMIC_OP
  118. /**
  119. * atomic_sub_and_test - subtract value from variable and test result
  120. * @i: integer value to subtract
  121. * @v: pointer of type atomic_t
  122. *
  123. * Atomically subtracts @i from @v and returns
  124. * true if the result is zero, or false for all
  125. * other cases.
  126. */
  127. #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
  128. /**
  129. * atomic_inc_return - increment atomic variable and return it
  130. * @v: pointer of type atomic_t
  131. *
  132. * Atomically increments @v by 1 and returns the result.
  133. */
  134. static __inline__ int atomic_inc_return(atomic_t *v)
  135. {
  136. unsigned long flags;
  137. int result;
  138. local_irq_save(flags);
  139. __asm__ __volatile__ (
  140. "# atomic_inc_return \n\t"
  141. DCACHE_CLEAR("%0", "r4", "%1")
  142. M32R_LOCK" %0, @%1; \n\t"
  143. "addi %0, #1; \n\t"
  144. M32R_UNLOCK" %0, @%1; \n\t"
  145. : "=&r" (result)
  146. : "r" (&v->counter)
  147. : "memory"
  148. __ATOMIC_CLOBBER
  149. );
  150. local_irq_restore(flags);
  151. return result;
  152. }
  153. /**
  154. * atomic_dec_return - decrement atomic variable and return it
  155. * @v: pointer of type atomic_t
  156. *
  157. * Atomically decrements @v by 1 and returns the result.
  158. */
  159. static __inline__ int atomic_dec_return(atomic_t *v)
  160. {
  161. unsigned long flags;
  162. int result;
  163. local_irq_save(flags);
  164. __asm__ __volatile__ (
  165. "# atomic_dec_return \n\t"
  166. DCACHE_CLEAR("%0", "r4", "%1")
  167. M32R_LOCK" %0, @%1; \n\t"
  168. "addi %0, #-1; \n\t"
  169. M32R_UNLOCK" %0, @%1; \n\t"
  170. : "=&r" (result)
  171. : "r" (&v->counter)
  172. : "memory"
  173. __ATOMIC_CLOBBER
  174. );
  175. local_irq_restore(flags);
  176. return result;
  177. }
  178. /**
  179. * atomic_inc - increment atomic variable
  180. * @v: pointer of type atomic_t
  181. *
  182. * Atomically increments @v by 1.
  183. */
  184. #define atomic_inc(v) ((void)atomic_inc_return(v))
  185. /**
  186. * atomic_dec - decrement atomic variable
  187. * @v: pointer of type atomic_t
  188. *
  189. * Atomically decrements @v by 1.
  190. */
  191. #define atomic_dec(v) ((void)atomic_dec_return(v))
  192. /**
  193. * atomic_inc_and_test - increment and test
  194. * @v: pointer of type atomic_t
  195. *
  196. * Atomically increments @v by 1
  197. * and returns true if the result is zero, or false for all
  198. * other cases.
  199. */
  200. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  201. /**
  202. * atomic_dec_and_test - decrement and test
  203. * @v: pointer of type atomic_t
  204. *
  205. * Atomically decrements @v by 1 and
  206. * returns true if the result is 0, or false for all
  207. * other cases.
  208. */
  209. #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
  210. /**
  211. * atomic_add_negative - add and test if negative
  212. * @v: pointer of type atomic_t
  213. * @i: integer value to add
  214. *
  215. * Atomically adds @i to @v and returns true
  216. * if the result is negative, or false when
  217. * result is greater than or equal to zero.
  218. */
  219. #define atomic_add_negative(i,v) (atomic_add_return((i), (v)) < 0)
  220. #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
  221. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  222. /**
  223. * __atomic_add_unless - add unless the number is a given value
  224. * @v: pointer of type atomic_t
  225. * @a: the amount to add to v...
  226. * @u: ...unless v is equal to u.
  227. *
  228. * Atomically adds @a to @v, so long as it was not @u.
  229. * Returns the old value of @v.
  230. */
  231. static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
  232. {
  233. int c, old;
  234. c = atomic_read(v);
  235. for (;;) {
  236. if (unlikely(c == (u)))
  237. break;
  238. old = atomic_cmpxchg((v), c, c + (a));
  239. if (likely(old == c))
  240. break;
  241. c = old;
  242. }
  243. return c;
  244. }
  245. #endif /* _ASM_M32R_ATOMIC_H */