atomic.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. #include <asm/processor.h>
  17. #include <asm/cmpxchg.h>
  18. #include <asm/barrier.h>
  19. /*
  20. * This Xtensa implementation assumes that the right mechanism
  21. * for exclusion is for locking interrupts to level EXCM_LEVEL.
  22. *
  23. * Locking interrupts looks like this:
  24. *
  25. * rsil a14, TOPLEVEL
  26. * <code>
  27. * wsr a14, PS
  28. * rsync
  29. *
  30. * Note that a14 is used here because the register allocation
  31. * done by the compiler is not guaranteed and a window overflow
  32. * may not occur between the rsil and wsr instructions. By using
  33. * a14 in the rsil, the machine is guaranteed to be in a state
  34. * where no register reference will cause an overflow.
  35. */
  36. /**
  37. * atomic_read - read atomic variable
  38. * @v: pointer of type atomic_t
  39. *
  40. * Atomically reads the value of @v.
  41. */
  42. #define arch_atomic_read(v) READ_ONCE((v)->counter)
  43. /**
  44. * atomic_set - set atomic variable
  45. * @v: pointer of type atomic_t
  46. * @i: required value
  47. *
  48. * Atomically sets the value of @v to @i.
  49. */
  50. #define arch_atomic_set(v,i) WRITE_ONCE((v)->counter, (i))
  51. #if XCHAL_HAVE_EXCLUSIVE
  52. #define ATOMIC_OP(op) \
  53. static inline void arch_atomic_##op(int i, atomic_t *v) \
  54. { \
  55. unsigned long tmp; \
  56. int result; \
  57. \
  58. __asm__ __volatile__( \
  59. "1: l32ex %[tmp], %[addr]\n" \
  60. " " #op " %[result], %[tmp], %[i]\n" \
  61. " s32ex %[result], %[addr]\n" \
  62. " getex %[result]\n" \
  63. " beqz %[result], 1b\n" \
  64. : [result] "=&a" (result), [tmp] "=&a" (tmp) \
  65. : [i] "a" (i), [addr] "a" (v) \
  66. : "memory" \
  67. ); \
  68. } \
  69. #define ATOMIC_OP_RETURN(op) \
  70. static inline int arch_atomic_##op##_return(int i, atomic_t *v) \
  71. { \
  72. unsigned long tmp; \
  73. int result; \
  74. \
  75. __asm__ __volatile__( \
  76. "1: l32ex %[tmp], %[addr]\n" \
  77. " " #op " %[result], %[tmp], %[i]\n" \
  78. " s32ex %[result], %[addr]\n" \
  79. " getex %[result]\n" \
  80. " beqz %[result], 1b\n" \
  81. " " #op " %[result], %[tmp], %[i]\n" \
  82. : [result] "=&a" (result), [tmp] "=&a" (tmp) \
  83. : [i] "a" (i), [addr] "a" (v) \
  84. : "memory" \
  85. ); \
  86. \
  87. return result; \
  88. }
  89. #define ATOMIC_FETCH_OP(op) \
  90. static inline int arch_atomic_fetch_##op(int i, atomic_t *v) \
  91. { \
  92. unsigned long tmp; \
  93. int result; \
  94. \
  95. __asm__ __volatile__( \
  96. "1: l32ex %[tmp], %[addr]\n" \
  97. " " #op " %[result], %[tmp], %[i]\n" \
  98. " s32ex %[result], %[addr]\n" \
  99. " getex %[result]\n" \
  100. " beqz %[result], 1b\n" \
  101. : [result] "=&a" (result), [tmp] "=&a" (tmp) \
  102. : [i] "a" (i), [addr] "a" (v) \
  103. : "memory" \
  104. ); \
  105. \
  106. return tmp; \
  107. }
  108. #elif XCHAL_HAVE_S32C1I
  109. #define ATOMIC_OP(op) \
  110. static inline void arch_atomic_##op(int i, atomic_t * v) \
  111. { \
  112. unsigned long tmp; \
  113. int result; \
  114. \
  115. __asm__ __volatile__( \
  116. "1: l32i %[tmp], %[mem]\n" \
  117. " wsr %[tmp], scompare1\n" \
  118. " " #op " %[result], %[tmp], %[i]\n" \
  119. " s32c1i %[result], %[mem]\n" \
  120. " bne %[result], %[tmp], 1b\n" \
  121. : [result] "=&a" (result), [tmp] "=&a" (tmp), \
  122. [mem] "+m" (*v) \
  123. : [i] "a" (i) \
  124. : "memory" \
  125. ); \
  126. } \
  127. #define ATOMIC_OP_RETURN(op) \
  128. static inline int arch_atomic_##op##_return(int i, atomic_t * v) \
  129. { \
  130. unsigned long tmp; \
  131. int result; \
  132. \
  133. __asm__ __volatile__( \
  134. "1: l32i %[tmp], %[mem]\n" \
  135. " wsr %[tmp], scompare1\n" \
  136. " " #op " %[result], %[tmp], %[i]\n" \
  137. " s32c1i %[result], %[mem]\n" \
  138. " bne %[result], %[tmp], 1b\n" \
  139. " " #op " %[result], %[result], %[i]\n" \
  140. : [result] "=&a" (result), [tmp] "=&a" (tmp), \
  141. [mem] "+m" (*v) \
  142. : [i] "a" (i) \
  143. : "memory" \
  144. ); \
  145. \
  146. return result; \
  147. }
  148. #define ATOMIC_FETCH_OP(op) \
  149. static inline int arch_atomic_fetch_##op(int i, atomic_t * v) \
  150. { \
  151. unsigned long tmp; \
  152. int result; \
  153. \
  154. __asm__ __volatile__( \
  155. "1: l32i %[tmp], %[mem]\n" \
  156. " wsr %[tmp], scompare1\n" \
  157. " " #op " %[result], %[tmp], %[i]\n" \
  158. " s32c1i %[result], %[mem]\n" \
  159. " bne %[result], %[tmp], 1b\n" \
  160. : [result] "=&a" (result), [tmp] "=&a" (tmp), \
  161. [mem] "+m" (*v) \
  162. : [i] "a" (i) \
  163. : "memory" \
  164. ); \
  165. \
  166. return result; \
  167. }
  168. #else /* XCHAL_HAVE_S32C1I */
  169. #define ATOMIC_OP(op) \
  170. static inline void arch_atomic_##op(int i, atomic_t * v) \
  171. { \
  172. unsigned int vval; \
  173. \
  174. __asm__ __volatile__( \
  175. " rsil a14, "__stringify(TOPLEVEL)"\n" \
  176. " l32i %[result], %[mem]\n" \
  177. " " #op " %[result], %[result], %[i]\n" \
  178. " s32i %[result], %[mem]\n" \
  179. " wsr a14, ps\n" \
  180. " rsync\n" \
  181. : [result] "=&a" (vval), [mem] "+m" (*v) \
  182. : [i] "a" (i) \
  183. : "a14", "memory" \
  184. ); \
  185. } \
  186. #define ATOMIC_OP_RETURN(op) \
  187. static inline int arch_atomic_##op##_return(int i, atomic_t * v) \
  188. { \
  189. unsigned int vval; \
  190. \
  191. __asm__ __volatile__( \
  192. " rsil a14,"__stringify(TOPLEVEL)"\n" \
  193. " l32i %[result], %[mem]\n" \
  194. " " #op " %[result], %[result], %[i]\n" \
  195. " s32i %[result], %[mem]\n" \
  196. " wsr a14, ps\n" \
  197. " rsync\n" \
  198. : [result] "=&a" (vval), [mem] "+m" (*v) \
  199. : [i] "a" (i) \
  200. : "a14", "memory" \
  201. ); \
  202. \
  203. return vval; \
  204. }
  205. #define ATOMIC_FETCH_OP(op) \
  206. static inline int arch_atomic_fetch_##op(int i, atomic_t * v) \
  207. { \
  208. unsigned int tmp, vval; \
  209. \
  210. __asm__ __volatile__( \
  211. " rsil a14,"__stringify(TOPLEVEL)"\n" \
  212. " l32i %[result], %[mem]\n" \
  213. " " #op " %[tmp], %[result], %[i]\n" \
  214. " s32i %[tmp], %[mem]\n" \
  215. " wsr a14, ps\n" \
  216. " rsync\n" \
  217. : [result] "=&a" (vval), [tmp] "=&a" (tmp), \
  218. [mem] "+m" (*v) \
  219. : [i] "a" (i) \
  220. : "a14", "memory" \
  221. ); \
  222. \
  223. return vval; \
  224. }
  225. #endif /* XCHAL_HAVE_S32C1I */
  226. #define ATOMIC_OPS(op) ATOMIC_OP(op) ATOMIC_FETCH_OP(op) ATOMIC_OP_RETURN(op)
  227. ATOMIC_OPS(add)
  228. ATOMIC_OPS(sub)
  229. #undef ATOMIC_OPS
  230. #define ATOMIC_OPS(op) ATOMIC_OP(op) ATOMIC_FETCH_OP(op)
  231. ATOMIC_OPS(and)
  232. ATOMIC_OPS(or)
  233. ATOMIC_OPS(xor)
  234. #undef ATOMIC_OPS
  235. #undef ATOMIC_FETCH_OP
  236. #undef ATOMIC_OP_RETURN
  237. #undef ATOMIC_OP
  238. #define arch_atomic_cmpxchg(v, o, n) ((int)arch_cmpxchg(&((v)->counter), (o), (n)))
  239. #define arch_atomic_xchg(v, new) (arch_xchg(&((v)->counter), new))
  240. #endif /* _XTENSA_ATOMIC_H */