atomic.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 - 2008 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. #include <asm/barrier.h>
  20. #define ATOMIC_INIT(i) { (i) }
  21. /*
  22. * This Xtensa implementation assumes that the right mechanism
  23. * for exclusion is for locking interrupts to level EXCM_LEVEL.
  24. *
  25. * Locking interrupts looks like this:
  26. *
  27. * rsil a15, LOCKLEVEL
  28. * <code>
  29. * wsr a15, PS
  30. * rsync
  31. *
  32. * Note that a15 is used here because the register allocation
  33. * done by the compiler is not guaranteed and a window overflow
  34. * may not occur between the rsil and wsr instructions. By using
  35. * a15 in the rsil, the machine is guaranteed to be in a state
  36. * where no register reference will cause an overflow.
  37. */
  38. /**
  39. * atomic_read - read atomic variable
  40. * @v: pointer of type atomic_t
  41. *
  42. * Atomically reads the value of @v.
  43. */
  44. #define atomic_read(v) ACCESS_ONCE((v)->counter)
  45. /**
  46. * atomic_set - set atomic variable
  47. * @v: pointer of type atomic_t
  48. * @i: required value
  49. *
  50. * Atomically sets the value of @v to @i.
  51. */
  52. #define atomic_set(v,i) ((v)->counter = (i))
  53. #if XCHAL_HAVE_S32C1I
  54. #define ATOMIC_OP(op) \
  55. static inline void atomic_##op(int i, atomic_t * v) \
  56. { \
  57. unsigned long tmp; \
  58. int result; \
  59. \
  60. __asm__ __volatile__( \
  61. "1: l32i %1, %3, 0\n" \
  62. " wsr %1, scompare1\n" \
  63. " " #op " %0, %1, %2\n" \
  64. " s32c1i %0, %3, 0\n" \
  65. " bne %0, %1, 1b\n" \
  66. : "=&a" (result), "=&a" (tmp) \
  67. : "a" (i), "a" (v) \
  68. : "memory" \
  69. ); \
  70. } \
  71. #define ATOMIC_OP_RETURN(op) \
  72. static inline int atomic_##op##_return(int i, atomic_t * v) \
  73. { \
  74. unsigned long tmp; \
  75. int result; \
  76. \
  77. __asm__ __volatile__( \
  78. "1: l32i %1, %3, 0\n" \
  79. " wsr %1, scompare1\n" \
  80. " " #op " %0, %1, %2\n" \
  81. " s32c1i %0, %3, 0\n" \
  82. " bne %0, %1, 1b\n" \
  83. " " #op " %0, %0, %2\n" \
  84. : "=&a" (result), "=&a" (tmp) \
  85. : "a" (i), "a" (v) \
  86. : "memory" \
  87. ); \
  88. \
  89. return result; \
  90. }
  91. #else /* XCHAL_HAVE_S32C1I */
  92. #define ATOMIC_OP(op) \
  93. static inline void atomic_##op(int i, atomic_t * v) \
  94. { \
  95. unsigned int vval; \
  96. \
  97. __asm__ __volatile__( \
  98. " rsil a15, "__stringify(LOCKLEVEL)"\n"\
  99. " l32i %0, %2, 0\n" \
  100. " " #op " %0, %0, %1\n" \
  101. " s32i %0, %2, 0\n" \
  102. " wsr a15, ps\n" \
  103. " rsync\n" \
  104. : "=&a" (vval) \
  105. : "a" (i), "a" (v) \
  106. : "a15", "memory" \
  107. ); \
  108. } \
  109. #define ATOMIC_OP_RETURN(op) \
  110. static inline int atomic_##op##_return(int i, atomic_t * v) \
  111. { \
  112. unsigned int vval; \
  113. \
  114. __asm__ __volatile__( \
  115. " rsil a15,"__stringify(LOCKLEVEL)"\n" \
  116. " l32i %0, %2, 0\n" \
  117. " " #op " %0, %0, %1\n" \
  118. " s32i %0, %2, 0\n" \
  119. " wsr a15, ps\n" \
  120. " rsync\n" \
  121. : "=&a" (vval) \
  122. : "a" (i), "a" (v) \
  123. : "a15", "memory" \
  124. ); \
  125. \
  126. return vval; \
  127. }
  128. #endif /* XCHAL_HAVE_S32C1I */
  129. #define ATOMIC_OPS(op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op)
  130. ATOMIC_OPS(add)
  131. ATOMIC_OPS(sub)
  132. #undef ATOMIC_OPS
  133. #undef ATOMIC_OP_RETURN
  134. #undef ATOMIC_OP
  135. /**
  136. * atomic_sub_and_test - subtract value from variable and test result
  137. * @i: integer value to subtract
  138. * @v: pointer of type atomic_t
  139. *
  140. * Atomically subtracts @i from @v and returns
  141. * true if the result is zero, or false for all
  142. * other cases.
  143. */
  144. #define atomic_sub_and_test(i,v) (atomic_sub_return((i),(v)) == 0)
  145. /**
  146. * atomic_inc - increment atomic variable
  147. * @v: pointer of type atomic_t
  148. *
  149. * Atomically increments @v by 1.
  150. */
  151. #define atomic_inc(v) atomic_add(1,(v))
  152. /**
  153. * atomic_inc - increment atomic variable
  154. * @v: pointer of type atomic_t
  155. *
  156. * Atomically increments @v by 1.
  157. */
  158. #define atomic_inc_return(v) atomic_add_return(1,(v))
  159. /**
  160. * atomic_dec - decrement atomic variable
  161. * @v: pointer of type atomic_t
  162. *
  163. * Atomically decrements @v by 1.
  164. */
  165. #define atomic_dec(v) atomic_sub(1,(v))
  166. /**
  167. * atomic_dec_return - decrement atomic variable
  168. * @v: pointer of type atomic_t
  169. *
  170. * Atomically decrements @v by 1.
  171. */
  172. #define atomic_dec_return(v) atomic_sub_return(1,(v))
  173. /**
  174. * atomic_dec_and_test - decrement and test
  175. * @v: pointer of type atomic_t
  176. *
  177. * Atomically decrements @v by 1 and
  178. * returns true if the result is 0, or false for all other
  179. * cases.
  180. */
  181. #define atomic_dec_and_test(v) (atomic_sub_return(1,(v)) == 0)
  182. /**
  183. * atomic_inc_and_test - increment and test
  184. * @v: pointer of type atomic_t
  185. *
  186. * Atomically increments @v by 1
  187. * and returns true if the result is zero, or false for all
  188. * other cases.
  189. */
  190. #define atomic_inc_and_test(v) (atomic_add_return(1,(v)) == 0)
  191. /**
  192. * atomic_add_negative - add and test if negative
  193. * @v: pointer of type atomic_t
  194. * @i: integer value to add
  195. *
  196. * Atomically adds @i to @v and returns true
  197. * if the result is negative, or false when
  198. * result is greater than or equal to zero.
  199. */
  200. #define atomic_add_negative(i,v) (atomic_add_return((i),(v)) < 0)
  201. #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
  202. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  203. /**
  204. * __atomic_add_unless - add unless the number is a given value
  205. * @v: pointer of type atomic_t
  206. * @a: the amount to add to v...
  207. * @u: ...unless v is equal to u.
  208. *
  209. * Atomically adds @a to @v, so long as it was not @u.
  210. * Returns the old value of @v.
  211. */
  212. static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
  213. {
  214. int c, old;
  215. c = atomic_read(v);
  216. for (;;) {
  217. if (unlikely(c == (u)))
  218. break;
  219. old = atomic_cmpxchg((v), c, c + (a));
  220. if (likely(old == c))
  221. break;
  222. c = old;
  223. }
  224. return c;
  225. }
  226. static inline void atomic_clear_mask(unsigned int mask, atomic_t *v)
  227. {
  228. #if XCHAL_HAVE_S32C1I
  229. unsigned long tmp;
  230. int result;
  231. __asm__ __volatile__(
  232. "1: l32i %1, %3, 0\n"
  233. " wsr %1, scompare1\n"
  234. " and %0, %1, %2\n"
  235. " s32c1i %0, %3, 0\n"
  236. " bne %0, %1, 1b\n"
  237. : "=&a" (result), "=&a" (tmp)
  238. : "a" (~mask), "a" (v)
  239. : "memory"
  240. );
  241. #else
  242. unsigned int all_f = -1;
  243. unsigned int vval;
  244. __asm__ __volatile__(
  245. " rsil a15,"__stringify(LOCKLEVEL)"\n"
  246. " l32i %0, %2, 0\n"
  247. " xor %1, %4, %3\n"
  248. " and %0, %0, %4\n"
  249. " s32i %0, %2, 0\n"
  250. " wsr a15, ps\n"
  251. " rsync\n"
  252. : "=&a" (vval), "=a" (mask)
  253. : "a" (v), "a" (all_f), "1" (mask)
  254. : "a15", "memory"
  255. );
  256. #endif
  257. }
  258. static inline void atomic_set_mask(unsigned int mask, atomic_t *v)
  259. {
  260. #if XCHAL_HAVE_S32C1I
  261. unsigned long tmp;
  262. int result;
  263. __asm__ __volatile__(
  264. "1: l32i %1, %3, 0\n"
  265. " wsr %1, scompare1\n"
  266. " or %0, %1, %2\n"
  267. " s32c1i %0, %3, 0\n"
  268. " bne %0, %1, 1b\n"
  269. : "=&a" (result), "=&a" (tmp)
  270. : "a" (mask), "a" (v)
  271. : "memory"
  272. );
  273. #else
  274. unsigned int vval;
  275. __asm__ __volatile__(
  276. " rsil a15,"__stringify(LOCKLEVEL)"\n"
  277. " l32i %0, %2, 0\n"
  278. " or %0, %0, %1\n"
  279. " s32i %0, %2, 0\n"
  280. " wsr a15, ps\n"
  281. " rsync\n"
  282. : "=&a" (vval)
  283. : "a" (mask), "a" (v)
  284. : "a15", "memory"
  285. );
  286. #endif
  287. }
  288. #endif /* __KERNEL__ */
  289. #endif /* _XTENSA_ATOMIC_H */