atomic.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. #if XCHAL_HAVE_S32C1I
  62. unsigned long tmp;
  63. int result;
  64. __asm__ __volatile__(
  65. "1: l32i %1, %3, 0\n"
  66. " wsr %1, scompare1\n"
  67. " add %0, %1, %2\n"
  68. " s32c1i %0, %3, 0\n"
  69. " bne %0, %1, 1b\n"
  70. : "=&a" (result), "=&a" (tmp)
  71. : "a" (i), "a" (v)
  72. : "memory"
  73. );
  74. #else
  75. unsigned int vval;
  76. __asm__ __volatile__(
  77. " rsil a15, "__stringify(LOCKLEVEL)"\n"
  78. " l32i %0, %2, 0\n"
  79. " add %0, %0, %1\n"
  80. " s32i %0, %2, 0\n"
  81. " wsr a15, ps\n"
  82. " rsync\n"
  83. : "=&a" (vval)
  84. : "a" (i), "a" (v)
  85. : "a15", "memory"
  86. );
  87. #endif
  88. }
  89. /**
  90. * atomic_sub - subtract the atomic variable
  91. * @i: integer value to subtract
  92. * @v: pointer of type atomic_t
  93. *
  94. * Atomically subtracts @i from @v.
  95. */
  96. static inline void atomic_sub(int i, atomic_t *v)
  97. {
  98. #if XCHAL_HAVE_S32C1I
  99. unsigned long tmp;
  100. int result;
  101. __asm__ __volatile__(
  102. "1: l32i %1, %3, 0\n"
  103. " wsr %1, scompare1\n"
  104. " sub %0, %1, %2\n"
  105. " s32c1i %0, %3, 0\n"
  106. " bne %0, %1, 1b\n"
  107. : "=&a" (result), "=&a" (tmp)
  108. : "a" (i), "a" (v)
  109. : "memory"
  110. );
  111. #else
  112. unsigned int vval;
  113. __asm__ __volatile__(
  114. " rsil a15, "__stringify(LOCKLEVEL)"\n"
  115. " l32i %0, %2, 0\n"
  116. " sub %0, %0, %1\n"
  117. " s32i %0, %2, 0\n"
  118. " wsr a15, ps\n"
  119. " rsync\n"
  120. : "=&a" (vval)
  121. : "a" (i), "a" (v)
  122. : "a15", "memory"
  123. );
  124. #endif
  125. }
  126. /*
  127. * We use atomic_{add|sub}_return to define other functions.
  128. */
  129. static inline int atomic_add_return(int i, atomic_t * v)
  130. {
  131. #if XCHAL_HAVE_S32C1I
  132. unsigned long tmp;
  133. int result;
  134. __asm__ __volatile__(
  135. "1: l32i %1, %3, 0\n"
  136. " wsr %1, scompare1\n"
  137. " add %0, %1, %2\n"
  138. " s32c1i %0, %3, 0\n"
  139. " bne %0, %1, 1b\n"
  140. " add %0, %0, %2\n"
  141. : "=&a" (result), "=&a" (tmp)
  142. : "a" (i), "a" (v)
  143. : "memory"
  144. );
  145. return result;
  146. #else
  147. unsigned int vval;
  148. __asm__ __volatile__(
  149. " rsil a15,"__stringify(LOCKLEVEL)"\n"
  150. " l32i %0, %2, 0\n"
  151. " add %0, %0, %1\n"
  152. " s32i %0, %2, 0\n"
  153. " wsr a15, ps\n"
  154. " rsync\n"
  155. : "=&a" (vval)
  156. : "a" (i), "a" (v)
  157. : "a15", "memory"
  158. );
  159. return vval;
  160. #endif
  161. }
  162. static inline int atomic_sub_return(int i, atomic_t * v)
  163. {
  164. #if XCHAL_HAVE_S32C1I
  165. unsigned long tmp;
  166. int result;
  167. __asm__ __volatile__(
  168. "1: l32i %1, %3, 0\n"
  169. " wsr %1, scompare1\n"
  170. " sub %0, %1, %2\n"
  171. " s32c1i %0, %3, 0\n"
  172. " bne %0, %1, 1b\n"
  173. " sub %0, %0, %2\n"
  174. : "=&a" (result), "=&a" (tmp)
  175. : "a" (i), "a" (v)
  176. : "memory"
  177. );
  178. return result;
  179. #else
  180. unsigned int vval;
  181. __asm__ __volatile__(
  182. " rsil a15,"__stringify(LOCKLEVEL)"\n"
  183. " l32i %0, %2, 0\n"
  184. " sub %0, %0, %1\n"
  185. " s32i %0, %2, 0\n"
  186. " wsr a15, ps\n"
  187. " rsync\n"
  188. : "=&a" (vval)
  189. : "a" (i), "a" (v)
  190. : "a15", "memory"
  191. );
  192. return vval;
  193. #endif
  194. }
  195. /**
  196. * atomic_sub_and_test - subtract value from variable and test result
  197. * @i: integer value to subtract
  198. * @v: pointer of type atomic_t
  199. *
  200. * Atomically subtracts @i from @v and returns
  201. * true if the result is zero, or false for all
  202. * other cases.
  203. */
  204. #define atomic_sub_and_test(i,v) (atomic_sub_return((i),(v)) == 0)
  205. /**
  206. * atomic_inc - increment atomic variable
  207. * @v: pointer of type atomic_t
  208. *
  209. * Atomically increments @v by 1.
  210. */
  211. #define atomic_inc(v) atomic_add(1,(v))
  212. /**
  213. * atomic_inc - increment atomic variable
  214. * @v: pointer of type atomic_t
  215. *
  216. * Atomically increments @v by 1.
  217. */
  218. #define atomic_inc_return(v) atomic_add_return(1,(v))
  219. /**
  220. * atomic_dec - decrement atomic variable
  221. * @v: pointer of type atomic_t
  222. *
  223. * Atomically decrements @v by 1.
  224. */
  225. #define atomic_dec(v) atomic_sub(1,(v))
  226. /**
  227. * atomic_dec_return - decrement atomic variable
  228. * @v: pointer of type atomic_t
  229. *
  230. * Atomically decrements @v by 1.
  231. */
  232. #define atomic_dec_return(v) atomic_sub_return(1,(v))
  233. /**
  234. * atomic_dec_and_test - decrement and test
  235. * @v: pointer of type atomic_t
  236. *
  237. * Atomically decrements @v by 1 and
  238. * returns true if the result is 0, or false for all other
  239. * cases.
  240. */
  241. #define atomic_dec_and_test(v) (atomic_sub_return(1,(v)) == 0)
  242. /**
  243. * atomic_inc_and_test - increment and test
  244. * @v: pointer of type atomic_t
  245. *
  246. * Atomically increments @v by 1
  247. * and returns true if the result is zero, or false for all
  248. * other cases.
  249. */
  250. #define atomic_inc_and_test(v) (atomic_add_return(1,(v)) == 0)
  251. /**
  252. * atomic_add_negative - add and test if negative
  253. * @v: pointer of type atomic_t
  254. * @i: integer value to add
  255. *
  256. * Atomically adds @i to @v and returns true
  257. * if the result is negative, or false when
  258. * result is greater than or equal to zero.
  259. */
  260. #define atomic_add_negative(i,v) (atomic_add_return((i),(v)) < 0)
  261. #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
  262. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  263. /**
  264. * __atomic_add_unless - add unless the number is a given value
  265. * @v: pointer of type atomic_t
  266. * @a: the amount to add to v...
  267. * @u: ...unless v is equal to u.
  268. *
  269. * Atomically adds @a to @v, so long as it was not @u.
  270. * Returns the old value of @v.
  271. */
  272. static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
  273. {
  274. int c, old;
  275. c = atomic_read(v);
  276. for (;;) {
  277. if (unlikely(c == (u)))
  278. break;
  279. old = atomic_cmpxchg((v), c, c + (a));
  280. if (likely(old == c))
  281. break;
  282. c = old;
  283. }
  284. return c;
  285. }
  286. static inline void atomic_clear_mask(unsigned int mask, atomic_t *v)
  287. {
  288. #if XCHAL_HAVE_S32C1I
  289. unsigned long tmp;
  290. int result;
  291. __asm__ __volatile__(
  292. "1: l32i %1, %3, 0\n"
  293. " wsr %1, scompare1\n"
  294. " and %0, %1, %2\n"
  295. " s32c1i %0, %3, 0\n"
  296. " bne %0, %1, 1b\n"
  297. : "=&a" (result), "=&a" (tmp)
  298. : "a" (~mask), "a" (v)
  299. : "memory"
  300. );
  301. #else
  302. unsigned int all_f = -1;
  303. unsigned int vval;
  304. __asm__ __volatile__(
  305. " rsil a15,"__stringify(LOCKLEVEL)"\n"
  306. " l32i %0, %2, 0\n"
  307. " xor %1, %4, %3\n"
  308. " and %0, %0, %4\n"
  309. " s32i %0, %2, 0\n"
  310. " wsr a15, ps\n"
  311. " rsync\n"
  312. : "=&a" (vval), "=a" (mask)
  313. : "a" (v), "a" (all_f), "1" (mask)
  314. : "a15", "memory"
  315. );
  316. #endif
  317. }
  318. static inline void atomic_set_mask(unsigned int mask, atomic_t *v)
  319. {
  320. #if XCHAL_HAVE_S32C1I
  321. unsigned long tmp;
  322. int result;
  323. __asm__ __volatile__(
  324. "1: l32i %1, %3, 0\n"
  325. " wsr %1, scompare1\n"
  326. " or %0, %1, %2\n"
  327. " s32c1i %0, %3, 0\n"
  328. " bne %0, %1, 1b\n"
  329. : "=&a" (result), "=&a" (tmp)
  330. : "a" (mask), "a" (v)
  331. : "memory"
  332. );
  333. #else
  334. unsigned int vval;
  335. __asm__ __volatile__(
  336. " rsil a15,"__stringify(LOCKLEVEL)"\n"
  337. " l32i %0, %2, 0\n"
  338. " or %0, %0, %1\n"
  339. " s32i %0, %2, 0\n"
  340. " wsr a15, ps\n"
  341. " rsync\n"
  342. : "=&a" (vval)
  343. : "a" (mask), "a" (v)
  344. : "a15", "memory"
  345. );
  346. #endif
  347. }
  348. /* Atomic operations are already serializing */
  349. #define smp_mb__before_atomic_dec() barrier()
  350. #define smp_mb__after_atomic_dec() barrier()
  351. #define smp_mb__before_atomic_inc() barrier()
  352. #define smp_mb__after_atomic_inc() barrier()
  353. #endif /* __KERNEL__ */
  354. #endif /* _XTENSA_ATOMIC_H */