atomic.h 6.3 KB

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