atomic.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 <asm/assembler.h>
  11. #include <asm/system.h>
  12. /*
  13. * Atomic operations that C can't guarantee us. Useful for
  14. * resource counting etc..
  15. */
  16. /*
  17. * Make sure gcc doesn't try to be clever and move things around
  18. * on us. We need to use _exactly_ the address the user gave us,
  19. * not some alias that contains the same information.
  20. */
  21. typedef struct { volatile int counter; } atomic_t;
  22. #define ATOMIC_INIT(i) { (i) }
  23. /**
  24. * atomic_read - read atomic variable
  25. * @v: pointer of type atomic_t
  26. *
  27. * Atomically reads the value of @v.
  28. */
  29. #define atomic_read(v) ((v)->counter)
  30. /**
  31. * atomic_set - set atomic variable
  32. * @v: pointer of type atomic_t
  33. * @i: required value
  34. *
  35. * Atomically sets the value of @v to @i.
  36. */
  37. #define atomic_set(v,i) (((v)->counter) = (i))
  38. /**
  39. * atomic_add_return - add integer to atomic variable and return it
  40. * @i: integer value to add
  41. * @v: pointer of type atomic_t
  42. *
  43. * Atomically adds @i to @v and return (@i + @v).
  44. */
  45. static __inline__ int atomic_add_return(int i, atomic_t *v)
  46. {
  47. unsigned long flags;
  48. int result;
  49. local_irq_save(flags);
  50. __asm__ __volatile__ (
  51. "# atomic_add_return \n\t"
  52. DCACHE_CLEAR("%0", "r4", "%1")
  53. M32R_LOCK" %0, @%1; \n\t"
  54. "add %0, %2; \n\t"
  55. M32R_UNLOCK" %0, @%1; \n\t"
  56. : "=&r" (result)
  57. : "r" (&v->counter), "r" (i)
  58. : "memory"
  59. #ifdef CONFIG_CHIP_M32700_TS1
  60. , "r4"
  61. #endif /* CONFIG_CHIP_M32700_TS1 */
  62. );
  63. local_irq_restore(flags);
  64. return result;
  65. }
  66. /**
  67. * atomic_sub_return - subtract integer from atomic variable and return it
  68. * @i: integer value to subtract
  69. * @v: pointer of type atomic_t
  70. *
  71. * Atomically subtracts @i from @v and return (@v - @i).
  72. */
  73. static __inline__ int atomic_sub_return(int i, atomic_t *v)
  74. {
  75. unsigned long flags;
  76. int result;
  77. local_irq_save(flags);
  78. __asm__ __volatile__ (
  79. "# atomic_sub_return \n\t"
  80. DCACHE_CLEAR("%0", "r4", "%1")
  81. M32R_LOCK" %0, @%1; \n\t"
  82. "sub %0, %2; \n\t"
  83. M32R_UNLOCK" %0, @%1; \n\t"
  84. : "=&r" (result)
  85. : "r" (&v->counter), "r" (i)
  86. : "memory"
  87. #ifdef CONFIG_CHIP_M32700_TS1
  88. , "r4"
  89. #endif /* CONFIG_CHIP_M32700_TS1 */
  90. );
  91. local_irq_restore(flags);
  92. return result;
  93. }
  94. /**
  95. * atomic_add - add integer to atomic variable
  96. * @i: integer value to add
  97. * @v: pointer of type atomic_t
  98. *
  99. * Atomically adds @i to @v.
  100. */
  101. #define atomic_add(i,v) ((void) atomic_add_return((i), (v)))
  102. /**
  103. * atomic_sub - subtract the atomic variable
  104. * @i: integer value to subtract
  105. * @v: pointer of type atomic_t
  106. *
  107. * Atomically subtracts @i from @v.
  108. */
  109. #define atomic_sub(i,v) ((void) atomic_sub_return((i), (v)))
  110. /**
  111. * atomic_sub_and_test - subtract value from variable and test result
  112. * @i: integer value to subtract
  113. * @v: pointer of type atomic_t
  114. *
  115. * Atomically subtracts @i from @v and returns
  116. * true if the result is zero, or false for all
  117. * other cases.
  118. */
  119. #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
  120. /**
  121. * atomic_inc_return - increment atomic variable and return it
  122. * @v: pointer of type atomic_t
  123. *
  124. * Atomically increments @v by 1 and returns the result.
  125. */
  126. static __inline__ int atomic_inc_return(atomic_t *v)
  127. {
  128. unsigned long flags;
  129. int result;
  130. local_irq_save(flags);
  131. __asm__ __volatile__ (
  132. "# atomic_inc_return \n\t"
  133. DCACHE_CLEAR("%0", "r4", "%1")
  134. M32R_LOCK" %0, @%1; \n\t"
  135. "addi %0, #1; \n\t"
  136. M32R_UNLOCK" %0, @%1; \n\t"
  137. : "=&r" (result)
  138. : "r" (&v->counter)
  139. : "memory"
  140. #ifdef CONFIG_CHIP_M32700_TS1
  141. , "r4"
  142. #endif /* CONFIG_CHIP_M32700_TS1 */
  143. );
  144. local_irq_restore(flags);
  145. return result;
  146. }
  147. /**
  148. * atomic_dec_return - decrement atomic variable and return it
  149. * @v: pointer of type atomic_t
  150. *
  151. * Atomically decrements @v by 1 and returns the result.
  152. */
  153. static __inline__ int atomic_dec_return(atomic_t *v)
  154. {
  155. unsigned long flags;
  156. int result;
  157. local_irq_save(flags);
  158. __asm__ __volatile__ (
  159. "# atomic_dec_return \n\t"
  160. DCACHE_CLEAR("%0", "r4", "%1")
  161. M32R_LOCK" %0, @%1; \n\t"
  162. "addi %0, #-1; \n\t"
  163. M32R_UNLOCK" %0, @%1; \n\t"
  164. : "=&r" (result)
  165. : "r" (&v->counter)
  166. : "memory"
  167. #ifdef CONFIG_CHIP_M32700_TS1
  168. , "r4"
  169. #endif /* CONFIG_CHIP_M32700_TS1 */
  170. );
  171. local_irq_restore(flags);
  172. return result;
  173. }
  174. /**
  175. * atomic_inc - increment atomic variable
  176. * @v: pointer of type atomic_t
  177. *
  178. * Atomically increments @v by 1.
  179. */
  180. #define atomic_inc(v) ((void)atomic_inc_return(v))
  181. /**
  182. * atomic_dec - decrement atomic variable
  183. * @v: pointer of type atomic_t
  184. *
  185. * Atomically decrements @v by 1.
  186. */
  187. #define atomic_dec(v) ((void)atomic_dec_return(v))
  188. /**
  189. * atomic_inc_and_test - increment and test
  190. * @v: pointer of type atomic_t
  191. *
  192. * Atomically increments @v by 1
  193. * and returns true if the result is zero, or false for all
  194. * other cases.
  195. */
  196. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  197. /**
  198. * atomic_dec_and_test - decrement and test
  199. * @v: pointer of type atomic_t
  200. *
  201. * Atomically decrements @v by 1 and
  202. * returns true if the result is 0, or false for all
  203. * other cases.
  204. */
  205. #define atomic_dec_and_test(v) (atomic_dec_return(v) == 0)
  206. /**
  207. * atomic_add_negative - add and test if negative
  208. * @v: pointer of type atomic_t
  209. * @i: integer value to add
  210. *
  211. * Atomically adds @i to @v and returns true
  212. * if the result is negative, or false when
  213. * result is greater than or equal to zero.
  214. */
  215. #define atomic_add_negative(i,v) (atomic_add_return((i), (v)) < 0)
  216. #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
  217. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  218. /**
  219. * atomic_add_unless - add unless the number is a given value
  220. * @v: pointer of type atomic_t
  221. * @a: the amount to add to v...
  222. * @u: ...unless v is equal to u.
  223. *
  224. * Atomically adds @a to @v, so long as it was not @u.
  225. * Returns non-zero if @v was not @u, and zero otherwise.
  226. */
  227. #define atomic_add_unless(v, a, u) \
  228. ({ \
  229. int c, old; \
  230. c = atomic_read(v); \
  231. while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c) \
  232. c = old; \
  233. c != (u); \
  234. })
  235. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  236. static __inline__ void atomic_clear_mask(unsigned long mask, atomic_t *addr)
  237. {
  238. unsigned long flags;
  239. unsigned long tmp;
  240. local_irq_save(flags);
  241. __asm__ __volatile__ (
  242. "# atomic_clear_mask \n\t"
  243. DCACHE_CLEAR("%0", "r5", "%1")
  244. M32R_LOCK" %0, @%1; \n\t"
  245. "and %0, %2; \n\t"
  246. M32R_UNLOCK" %0, @%1; \n\t"
  247. : "=&r" (tmp)
  248. : "r" (addr), "r" (~mask)
  249. : "memory"
  250. #ifdef CONFIG_CHIP_M32700_TS1
  251. , "r5"
  252. #endif /* CONFIG_CHIP_M32700_TS1 */
  253. );
  254. local_irq_restore(flags);
  255. }
  256. static __inline__ void atomic_set_mask(unsigned long mask, atomic_t *addr)
  257. {
  258. unsigned long flags;
  259. unsigned long tmp;
  260. local_irq_save(flags);
  261. __asm__ __volatile__ (
  262. "# atomic_set_mask \n\t"
  263. DCACHE_CLEAR("%0", "r5", "%1")
  264. M32R_LOCK" %0, @%1; \n\t"
  265. "or %0, %2; \n\t"
  266. M32R_UNLOCK" %0, @%1; \n\t"
  267. : "=&r" (tmp)
  268. : "r" (addr), "r" (mask)
  269. : "memory"
  270. #ifdef CONFIG_CHIP_M32700_TS1
  271. , "r5"
  272. #endif /* CONFIG_CHIP_M32700_TS1 */
  273. );
  274. local_irq_restore(flags);
  275. }
  276. /* Atomic operations are already serializing on m32r */
  277. #define smp_mb__before_atomic_dec() barrier()
  278. #define smp_mb__after_atomic_dec() barrier()
  279. #define smp_mb__before_atomic_inc() barrier()
  280. #define smp_mb__after_atomic_inc() barrier()
  281. #include <asm-generic/atomic.h>
  282. #endif /* _ASM_M32R_ATOMIC_H */