cmpxchg.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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(LOCKLEVEL)"\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. /*
  84. * xchg_u32
  85. *
  86. * Note that a15 is used here because the register allocation
  87. * done by the compiler is not guaranteed and a window overflow
  88. * may not occur between the rsil and wsr instructions. By using
  89. * a15 in the rsil, the machine is guaranteed to be in a state
  90. * where no register reference will cause an overflow.
  91. */
  92. static inline unsigned long xchg_u32(volatile int * m, unsigned long val)
  93. {
  94. #if XCHAL_HAVE_S32C1I
  95. unsigned long tmp, result;
  96. __asm__ __volatile__(
  97. "1: l32i %1, %2, 0\n"
  98. " mov %0, %3\n"
  99. " wsr %1, scompare1\n"
  100. " s32c1i %0, %2, 0\n"
  101. " bne %0, %1, 1b\n"
  102. : "=&a" (result), "=&a" (tmp)
  103. : "a" (m), "a" (val)
  104. : "memory"
  105. );
  106. return result;
  107. #else
  108. unsigned long tmp;
  109. __asm__ __volatile__(
  110. " rsil a15, "__stringify(LOCKLEVEL)"\n"
  111. " l32i %0, %1, 0\n"
  112. " s32i %2, %1, 0\n"
  113. " wsr a15, ps\n"
  114. " rsync\n"
  115. : "=&a" (tmp)
  116. : "a" (m), "a" (val)
  117. : "a15", "memory");
  118. return tmp;
  119. #endif
  120. }
  121. #define xchg(ptr,x) \
  122. ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
  123. /*
  124. * This only works if the compiler isn't horribly bad at optimizing.
  125. * gcc-2.5.8 reportedly can't handle this, but I define that one to
  126. * be dead anyway.
  127. */
  128. extern void __xchg_called_with_bad_pointer(void);
  129. static __inline__ unsigned long
  130. __xchg(unsigned long x, volatile void * ptr, int size)
  131. {
  132. switch (size) {
  133. case 4:
  134. return xchg_u32(ptr, x);
  135. }
  136. __xchg_called_with_bad_pointer();
  137. return x;
  138. }
  139. #endif /* __ASSEMBLY__ */
  140. #endif /* _XTENSA_CMPXCHG_H */