cmpxchg.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/stringify.h>
  14. /*
  15. * cmpxchg
  16. */
  17. static inline unsigned long
  18. __cmpxchg_u32(volatile int *p, int old, int new)
  19. {
  20. #if XCHAL_HAVE_S32C1I
  21. __asm__ __volatile__(
  22. " wsr %2, scompare1\n"
  23. " s32c1i %0, %1, 0\n"
  24. : "+a" (new)
  25. : "a" (p), "a" (old)
  26. : "memory"
  27. );
  28. return new;
  29. #else
  30. __asm__ __volatile__(
  31. " rsil a15, "__stringify(TOPLEVEL)"\n"
  32. " l32i %0, %1, 0\n"
  33. " bne %0, %2, 1f\n"
  34. " s32i %3, %1, 0\n"
  35. "1:\n"
  36. " wsr a15, ps\n"
  37. " rsync\n"
  38. : "=&a" (old)
  39. : "a" (p), "a" (old), "r" (new)
  40. : "a15", "memory");
  41. return old;
  42. #endif
  43. }
  44. /* This function doesn't exist, so you'll get a linker error
  45. * if something tries to do an invalid cmpxchg(). */
  46. extern void __cmpxchg_called_with_bad_pointer(void);
  47. static __inline__ unsigned long
  48. __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
  49. {
  50. switch (size) {
  51. case 4: return __cmpxchg_u32(ptr, old, new);
  52. default: __cmpxchg_called_with_bad_pointer();
  53. return old;
  54. }
  55. }
  56. #define cmpxchg(ptr,o,n) \
  57. ({ __typeof__(*(ptr)) _o_ = (o); \
  58. __typeof__(*(ptr)) _n_ = (n); \
  59. (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
  60. (unsigned long)_n_, sizeof (*(ptr))); \
  61. })
  62. #include <asm-generic/cmpxchg-local.h>
  63. static inline unsigned long __cmpxchg_local(volatile void *ptr,
  64. unsigned long old,
  65. unsigned long new, int size)
  66. {
  67. switch (size) {
  68. case 4:
  69. return __cmpxchg_u32(ptr, old, new);
  70. default:
  71. return __cmpxchg_local_generic(ptr, old, new, size);
  72. }
  73. return old;
  74. }
  75. /*
  76. * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
  77. * them available.
  78. */
  79. #define cmpxchg_local(ptr, o, n) \
  80. ((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\
  81. (unsigned long)(n), sizeof(*(ptr))))
  82. #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
  83. #define cmpxchg64(ptr, o, n) cmpxchg64_local((ptr), (o), (n))
  84. /*
  85. * xchg_u32
  86. *
  87. * Note that a15 is used here because the register allocation
  88. * done by the compiler is not guaranteed and a window overflow
  89. * may not occur between the rsil and wsr instructions. By using
  90. * a15 in the rsil, the machine is guaranteed to be in a state
  91. * where no register reference will cause an overflow.
  92. */
  93. static inline unsigned long xchg_u32(volatile int * m, unsigned long val)
  94. {
  95. #if XCHAL_HAVE_S32C1I
  96. unsigned long tmp, result;
  97. __asm__ __volatile__(
  98. "1: l32i %1, %2, 0\n"
  99. " mov %0, %3\n"
  100. " wsr %1, scompare1\n"
  101. " s32c1i %0, %2, 0\n"
  102. " bne %0, %1, 1b\n"
  103. : "=&a" (result), "=&a" (tmp)
  104. : "a" (m), "a" (val)
  105. : "memory"
  106. );
  107. return result;
  108. #else
  109. unsigned long tmp;
  110. __asm__ __volatile__(
  111. " rsil a15, "__stringify(TOPLEVEL)"\n"
  112. " l32i %0, %1, 0\n"
  113. " s32i %2, %1, 0\n"
  114. " wsr a15, ps\n"
  115. " rsync\n"
  116. : "=&a" (tmp)
  117. : "a" (m), "a" (val)
  118. : "a15", "memory");
  119. return tmp;
  120. #endif
  121. }
  122. #define xchg(ptr,x) \
  123. ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
  124. /*
  125. * This only works if the compiler isn't horribly bad at optimizing.
  126. * gcc-2.5.8 reportedly can't handle this, but I define that one to
  127. * be dead anyway.
  128. */
  129. extern void __xchg_called_with_bad_pointer(void);
  130. static __inline__ unsigned long
  131. __xchg(unsigned long x, volatile void * ptr, int size)
  132. {
  133. switch (size) {
  134. case 4:
  135. return xchg_u32(ptr, x);
  136. }
  137. __xchg_called_with_bad_pointer();
  138. return x;
  139. }
  140. #endif /* __ASSEMBLY__ */
  141. #endif /* _XTENSA_CMPXCHG_H */