cmpxchg.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Atomic xchg and cmpxchg operations.
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 2001 - 2005 Tensilica Inc.
  9. */
  10. #ifndef _XTENSA_CMPXCHG_H
  11. #define _XTENSA_CMPXCHG_H
  12. #ifndef __ASSEMBLY__
  13. #include <linux/bits.h>
  14. #include <linux/stringify.h>
  15. /*
  16. * cmpxchg
  17. */
  18. static inline unsigned long
  19. __cmpxchg_u32(volatile int *p, int old, int new)
  20. {
  21. #if XCHAL_HAVE_EXCLUSIVE
  22. unsigned long tmp, result;
  23. __asm__ __volatile__(
  24. "1: l32ex %[result], %[addr]\n"
  25. " bne %[result], %[cmp], 2f\n"
  26. " mov %[tmp], %[new]\n"
  27. " s32ex %[tmp], %[addr]\n"
  28. " getex %[tmp]\n"
  29. " beqz %[tmp], 1b\n"
  30. "2:\n"
  31. : [result] "=&a" (result), [tmp] "=&a" (tmp)
  32. : [new] "a" (new), [addr] "a" (p), [cmp] "a" (old)
  33. : "memory"
  34. );
  35. return result;
  36. #elif XCHAL_HAVE_S32C1I
  37. __asm__ __volatile__(
  38. " wsr %[cmp], scompare1\n"
  39. " s32c1i %[new], %[mem]\n"
  40. : [new] "+a" (new), [mem] "+m" (*p)
  41. : [cmp] "a" (old)
  42. : "memory"
  43. );
  44. return new;
  45. #else
  46. __asm__ __volatile__(
  47. " rsil a14, "__stringify(TOPLEVEL)"\n"
  48. " l32i %[old], %[mem]\n"
  49. " bne %[old], %[cmp], 1f\n"
  50. " s32i %[new], %[mem]\n"
  51. "1:\n"
  52. " wsr a14, ps\n"
  53. " rsync\n"
  54. : [old] "=&a" (old), [mem] "+m" (*p)
  55. : [cmp] "a" (old), [new] "r" (new)
  56. : "a14", "memory");
  57. return old;
  58. #endif
  59. }
  60. /* This function doesn't exist, so you'll get a linker error
  61. * if something tries to do an invalid cmpxchg(). */
  62. extern void __cmpxchg_called_with_bad_pointer(void);
  63. static __inline__ unsigned long
  64. __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
  65. {
  66. switch (size) {
  67. case 4: return __cmpxchg_u32(ptr, old, new);
  68. default: __cmpxchg_called_with_bad_pointer();
  69. return old;
  70. }
  71. }
  72. #define arch_cmpxchg(ptr,o,n) \
  73. ({ __typeof__(*(ptr)) _o_ = (o); \
  74. __typeof__(*(ptr)) _n_ = (n); \
  75. (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
  76. (unsigned long)_n_, sizeof (*(ptr))); \
  77. })
  78. #include <asm-generic/cmpxchg-local.h>
  79. static inline unsigned long __cmpxchg_local(volatile void *ptr,
  80. unsigned long old,
  81. unsigned long new, int size)
  82. {
  83. switch (size) {
  84. case 4:
  85. return __cmpxchg_u32(ptr, old, new);
  86. default:
  87. return __generic_cmpxchg_local(ptr, old, new, size);
  88. }
  89. return old;
  90. }
  91. /*
  92. * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
  93. * them available.
  94. */
  95. #define arch_cmpxchg_local(ptr, o, n) \
  96. ((__typeof__(*(ptr)))__generic_cmpxchg_local((ptr), (unsigned long)(o),\
  97. (unsigned long)(n), sizeof(*(ptr))))
  98. #define arch_cmpxchg64_local(ptr, o, n) __generic_cmpxchg64_local((ptr), (o), (n))
  99. #define arch_cmpxchg64(ptr, o, n) arch_cmpxchg64_local((ptr), (o), (n))
  100. /*
  101. * xchg_u32
  102. *
  103. * Note that a14 is used here because the register allocation
  104. * done by the compiler is not guaranteed and a window overflow
  105. * may not occur between the rsil and wsr instructions. By using
  106. * a14 in the rsil, the machine is guaranteed to be in a state
  107. * where no register reference will cause an overflow.
  108. */
  109. static inline unsigned long xchg_u32(volatile int * m, unsigned long val)
  110. {
  111. #if XCHAL_HAVE_EXCLUSIVE
  112. unsigned long tmp, result;
  113. __asm__ __volatile__(
  114. "1: l32ex %[result], %[addr]\n"
  115. " mov %[tmp], %[val]\n"
  116. " s32ex %[tmp], %[addr]\n"
  117. " getex %[tmp]\n"
  118. " beqz %[tmp], 1b\n"
  119. : [result] "=&a" (result), [tmp] "=&a" (tmp)
  120. : [val] "a" (val), [addr] "a" (m)
  121. : "memory"
  122. );
  123. return result;
  124. #elif XCHAL_HAVE_S32C1I
  125. unsigned long tmp, result;
  126. __asm__ __volatile__(
  127. "1: l32i %[tmp], %[mem]\n"
  128. " mov %[result], %[val]\n"
  129. " wsr %[tmp], scompare1\n"
  130. " s32c1i %[result], %[mem]\n"
  131. " bne %[result], %[tmp], 1b\n"
  132. : [result] "=&a" (result), [tmp] "=&a" (tmp),
  133. [mem] "+m" (*m)
  134. : [val] "a" (val)
  135. : "memory"
  136. );
  137. return result;
  138. #else
  139. unsigned long tmp;
  140. __asm__ __volatile__(
  141. " rsil a14, "__stringify(TOPLEVEL)"\n"
  142. " l32i %[tmp], %[mem]\n"
  143. " s32i %[val], %[mem]\n"
  144. " wsr a14, ps\n"
  145. " rsync\n"
  146. : [tmp] "=&a" (tmp), [mem] "+m" (*m)
  147. : [val] "a" (val)
  148. : "a14", "memory");
  149. return tmp;
  150. #endif
  151. }
  152. #define arch_xchg(ptr,x) \
  153. ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
  154. static inline u32 xchg_small(volatile void *ptr, u32 x, int size)
  155. {
  156. int off = (unsigned long)ptr % sizeof(u32);
  157. volatile u32 *p = ptr - off;
  158. #ifdef __BIG_ENDIAN
  159. int bitoff = (sizeof(u32) - size - off) * BITS_PER_BYTE;
  160. #else
  161. int bitoff = off * BITS_PER_BYTE;
  162. #endif
  163. u32 bitmask = ((0x1 << size * BITS_PER_BYTE) - 1) << bitoff;
  164. u32 oldv, newv;
  165. u32 ret;
  166. do {
  167. oldv = READ_ONCE(*p);
  168. ret = (oldv & bitmask) >> bitoff;
  169. newv = (oldv & ~bitmask) | (x << bitoff);
  170. } while (__cmpxchg_u32(p, oldv, newv) != oldv);
  171. return ret;
  172. }
  173. /*
  174. * This only works if the compiler isn't horribly bad at optimizing.
  175. * gcc-2.5.8 reportedly can't handle this, but I define that one to
  176. * be dead anyway.
  177. */
  178. extern void __xchg_called_with_bad_pointer(void);
  179. static __inline__ unsigned long
  180. __xchg(unsigned long x, volatile void * ptr, int size)
  181. {
  182. switch (size) {
  183. case 1:
  184. return xchg_small(ptr, x, 1);
  185. case 2:
  186. return xchg_small(ptr, x, 2);
  187. case 4:
  188. return xchg_u32(ptr, x);
  189. default:
  190. __xchg_called_with_bad_pointer();
  191. return x;
  192. }
  193. }
  194. #endif /* __ASSEMBLY__ */
  195. #endif /* _XTENSA_CMPXCHG_H */