cmpxchg.h 5.6 KB

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