atomic.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * include/asm-xtensa/atomic.h
  3. *
  4. * Atomic operations that C can't guarantee us. Useful for resource counting..
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. *
  10. * Copyright (C) 2001 - 2005 Tensilica Inc.
  11. */
  12. #ifndef _XTENSA_ATOMIC_H
  13. #define _XTENSA_ATOMIC_H
  14. #include <linux/stringify.h>
  15. #include <linux/types.h>
  16. #ifdef __KERNEL__
  17. #include <asm/processor.h>
  18. #include <asm/cmpxchg.h>
  19. #define ATOMIC_INIT(i) { (i) }
  20. /*
  21. * This Xtensa implementation assumes that the right mechanism
  22. * for exclusion is for locking interrupts to level 1.
  23. *
  24. * Locking interrupts looks like this:
  25. *
  26. * rsil a15, 1
  27. * <code>
  28. * wsr a15, PS
  29. * rsync
  30. *
  31. * Note that a15 is used here because the register allocation
  32. * done by the compiler is not guaranteed and a window overflow
  33. * may not occur between the rsil and wsr instructions. By using
  34. * a15 in the rsil, the machine is guaranteed to be in a state
  35. * where no register reference will cause an overflow.
  36. */
  37. /**
  38. * atomic_read - read atomic variable
  39. * @v: pointer of type atomic_t
  40. *
  41. * Atomically reads the value of @v.
  42. */
  43. #define atomic_read(v) (*(volatile int *)&(v)->counter)
  44. /**
  45. * atomic_set - set atomic variable
  46. * @v: pointer of type atomic_t
  47. * @i: required value
  48. *
  49. * Atomically sets the value of @v to @i.
  50. */
  51. #define atomic_set(v,i) ((v)->counter = (i))
  52. /**
  53. * atomic_add - add integer to atomic variable
  54. * @i: integer value to add
  55. * @v: pointer of type atomic_t
  56. *
  57. * Atomically adds @i to @v.
  58. */
  59. static inline void atomic_add(int i, atomic_t * v)
  60. {
  61. unsigned int vval;
  62. __asm__ __volatile__(
  63. "rsil a15, "__stringify(LOCKLEVEL)"\n\t"
  64. "l32i %0, %2, 0 \n\t"
  65. "add %0, %0, %1 \n\t"
  66. "s32i %0, %2, 0 \n\t"
  67. "wsr a15, "__stringify(PS)" \n\t"
  68. "rsync \n"
  69. : "=&a" (vval)
  70. : "a" (i), "a" (v)
  71. : "a15", "memory"
  72. );
  73. }
  74. /**
  75. * atomic_sub - subtract the atomic variable
  76. * @i: integer value to subtract
  77. * @v: pointer of type atomic_t
  78. *
  79. * Atomically subtracts @i from @v.
  80. */
  81. static inline void atomic_sub(int i, atomic_t *v)
  82. {
  83. unsigned int vval;
  84. __asm__ __volatile__(
  85. "rsil a15, "__stringify(LOCKLEVEL)"\n\t"
  86. "l32i %0, %2, 0 \n\t"
  87. "sub %0, %0, %1 \n\t"
  88. "s32i %0, %2, 0 \n\t"
  89. "wsr a15, "__stringify(PS)" \n\t"
  90. "rsync \n"
  91. : "=&a" (vval)
  92. : "a" (i), "a" (v)
  93. : "a15", "memory"
  94. );
  95. }
  96. /*
  97. * We use atomic_{add|sub}_return to define other functions.
  98. */
  99. static inline int atomic_add_return(int i, atomic_t * v)
  100. {
  101. unsigned int vval;
  102. __asm__ __volatile__(
  103. "rsil a15,"__stringify(LOCKLEVEL)"\n\t"
  104. "l32i %0, %2, 0 \n\t"
  105. "add %0, %0, %1 \n\t"
  106. "s32i %0, %2, 0 \n\t"
  107. "wsr a15, "__stringify(PS)" \n\t"
  108. "rsync \n"
  109. : "=&a" (vval)
  110. : "a" (i), "a" (v)
  111. : "a15", "memory"
  112. );
  113. return vval;
  114. }
  115. static inline int atomic_sub_return(int i, atomic_t * v)
  116. {
  117. unsigned int vval;
  118. __asm__ __volatile__(
  119. "rsil a15,"__stringify(LOCKLEVEL)"\n\t"
  120. "l32i %0, %2, 0 \n\t"
  121. "sub %0, %0, %1 \n\t"
  122. "s32i %0, %2, 0 \n\t"
  123. "wsr a15, "__stringify(PS)" \n\t"
  124. "rsync \n"
  125. : "=&a" (vval)
  126. : "a" (i), "a" (v)
  127. : "a15", "memory"
  128. );
  129. return vval;
  130. }
  131. /**
  132. * atomic_sub_and_test - subtract value from variable and test result
  133. * @i: integer value to subtract
  134. * @v: pointer of type atomic_t
  135. *
  136. * Atomically subtracts @i from @v and returns
  137. * true if the result is zero, or false for all
  138. * other cases.
  139. */
  140. #define atomic_sub_and_test(i,v) (atomic_sub_return((i),(v)) == 0)
  141. /**
  142. * atomic_inc - increment atomic variable
  143. * @v: pointer of type atomic_t
  144. *
  145. * Atomically increments @v by 1.
  146. */
  147. #define atomic_inc(v) atomic_add(1,(v))
  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_return(v) atomic_add_return(1,(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) atomic_sub(1,(v))
  162. /**
  163. * atomic_dec_return - decrement atomic variable
  164. * @v: pointer of type atomic_t
  165. *
  166. * Atomically decrements @v by 1.
  167. */
  168. #define atomic_dec_return(v) atomic_sub_return(1,(v))
  169. /**
  170. * atomic_dec_and_test - decrement and test
  171. * @v: pointer of type atomic_t
  172. *
  173. * Atomically decrements @v by 1 and
  174. * returns true if the result is 0, or false for all other
  175. * cases.
  176. */
  177. #define atomic_dec_and_test(v) (atomic_sub_return(1,(v)) == 0)
  178. /**
  179. * atomic_inc_and_test - increment and test
  180. * @v: pointer of type atomic_t
  181. *
  182. * Atomically increments @v by 1
  183. * and returns true if the result is zero, or false for all
  184. * other cases.
  185. */
  186. #define atomic_inc_and_test(v) (atomic_add_return(1,(v)) == 0)
  187. /**
  188. * atomic_add_negative - add and test if negative
  189. * @v: pointer of type atomic_t
  190. * @i: integer value to add
  191. *
  192. * Atomically adds @i to @v and returns true
  193. * if the result is negative, or false when
  194. * result is greater than or equal to zero.
  195. */
  196. #define atomic_add_negative(i,v) (atomic_add_return((i),(v)) < 0)
  197. #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
  198. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  199. /**
  200. * __atomic_add_unless - add unless the number is a given value
  201. * @v: pointer of type atomic_t
  202. * @a: the amount to add to v...
  203. * @u: ...unless v is equal to u.
  204. *
  205. * Atomically adds @a to @v, so long as it was not @u.
  206. * Returns the old value of @v.
  207. */
  208. static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
  209. {
  210. int c, old;
  211. c = atomic_read(v);
  212. for (;;) {
  213. if (unlikely(c == (u)))
  214. break;
  215. old = atomic_cmpxchg((v), c, c + (a));
  216. if (likely(old == c))
  217. break;
  218. c = old;
  219. }
  220. return c;
  221. }
  222. static inline void atomic_clear_mask(unsigned int mask, atomic_t *v)
  223. {
  224. unsigned int all_f = -1;
  225. unsigned int vval;
  226. __asm__ __volatile__(
  227. "rsil a15,"__stringify(LOCKLEVEL)"\n\t"
  228. "l32i %0, %2, 0 \n\t"
  229. "xor %1, %4, %3 \n\t"
  230. "and %0, %0, %4 \n\t"
  231. "s32i %0, %2, 0 \n\t"
  232. "wsr a15, "__stringify(PS)" \n\t"
  233. "rsync \n"
  234. : "=&a" (vval), "=a" (mask)
  235. : "a" (v), "a" (all_f), "1" (mask)
  236. : "a15", "memory"
  237. );
  238. }
  239. static inline void atomic_set_mask(unsigned int mask, atomic_t *v)
  240. {
  241. unsigned int vval;
  242. __asm__ __volatile__(
  243. "rsil a15,"__stringify(LOCKLEVEL)"\n\t"
  244. "l32i %0, %2, 0 \n\t"
  245. "or %0, %0, %1 \n\t"
  246. "s32i %0, %2, 0 \n\t"
  247. "wsr a15, "__stringify(PS)" \n\t"
  248. "rsync \n"
  249. : "=&a" (vval)
  250. : "a" (mask), "a" (v)
  251. : "a15", "memory"
  252. );
  253. }
  254. /* Atomic operations are already serializing */
  255. #define smp_mb__before_atomic_dec() barrier()
  256. #define smp_mb__after_atomic_dec() barrier()
  257. #define smp_mb__before_atomic_inc() barrier()
  258. #define smp_mb__after_atomic_inc() barrier()
  259. #endif /* __KERNEL__ */
  260. #endif /* _XTENSA_ATOMIC_H */