cmpxchg_64.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* 64-bit atomic xchg() and cmpxchg() definitions.
  3. *
  4. * Copyright (C) 1996, 1997, 2000 David S. Miller ([email protected])
  5. */
  6. #ifndef __ARCH_SPARC64_CMPXCHG__
  7. #define __ARCH_SPARC64_CMPXCHG__
  8. static inline unsigned long
  9. __cmpxchg_u32(volatile int *m, int old, int new)
  10. {
  11. __asm__ __volatile__("cas [%2], %3, %0"
  12. : "=&r" (new)
  13. : "0" (new), "r" (m), "r" (old)
  14. : "memory");
  15. return new;
  16. }
  17. static inline unsigned long xchg32(__volatile__ unsigned int *m, unsigned int val)
  18. {
  19. unsigned long tmp1, tmp2;
  20. __asm__ __volatile__(
  21. " mov %0, %1\n"
  22. "1: lduw [%4], %2\n"
  23. " cas [%4], %2, %0\n"
  24. " cmp %2, %0\n"
  25. " bne,a,pn %%icc, 1b\n"
  26. " mov %1, %0\n"
  27. : "=&r" (val), "=&r" (tmp1), "=&r" (tmp2)
  28. : "0" (val), "r" (m)
  29. : "cc", "memory");
  30. return val;
  31. }
  32. static inline unsigned long xchg64(__volatile__ unsigned long *m, unsigned long val)
  33. {
  34. unsigned long tmp1, tmp2;
  35. __asm__ __volatile__(
  36. " mov %0, %1\n"
  37. "1: ldx [%4], %2\n"
  38. " casx [%4], %2, %0\n"
  39. " cmp %2, %0\n"
  40. " bne,a,pn %%xcc, 1b\n"
  41. " mov %1, %0\n"
  42. : "=&r" (val), "=&r" (tmp1), "=&r" (tmp2)
  43. : "0" (val), "r" (m)
  44. : "cc", "memory");
  45. return val;
  46. }
  47. #define arch_xchg(ptr,x) \
  48. ({ __typeof__(*(ptr)) __ret; \
  49. __ret = (__typeof__(*(ptr))) \
  50. __xchg((unsigned long)(x), (ptr), sizeof(*(ptr))); \
  51. __ret; \
  52. })
  53. void __xchg_called_with_bad_pointer(void);
  54. /*
  55. * Use 4 byte cas instruction to achieve 2 byte xchg. Main logic
  56. * here is to get the bit shift of the byte we are interested in.
  57. * The XOR is handy for reversing the bits for big-endian byte order.
  58. */
  59. static inline unsigned long
  60. xchg16(__volatile__ unsigned short *m, unsigned short val)
  61. {
  62. unsigned long maddr = (unsigned long)m;
  63. int bit_shift = (((unsigned long)m & 2) ^ 2) << 3;
  64. unsigned int mask = 0xffff << bit_shift;
  65. unsigned int *ptr = (unsigned int *) (maddr & ~2);
  66. unsigned int old32, new32, load32;
  67. /* Read the old value */
  68. load32 = *ptr;
  69. do {
  70. old32 = load32;
  71. new32 = (load32 & (~mask)) | val << bit_shift;
  72. load32 = __cmpxchg_u32(ptr, old32, new32);
  73. } while (load32 != old32);
  74. return (load32 & mask) >> bit_shift;
  75. }
  76. static inline unsigned long __xchg(unsigned long x, __volatile__ void * ptr,
  77. int size)
  78. {
  79. switch (size) {
  80. case 2:
  81. return xchg16(ptr, x);
  82. case 4:
  83. return xchg32(ptr, x);
  84. case 8:
  85. return xchg64(ptr, x);
  86. }
  87. __xchg_called_with_bad_pointer();
  88. return x;
  89. }
  90. /*
  91. * Atomic compare and exchange. Compare OLD with MEM, if identical,
  92. * store NEW in MEM. Return the initial value in MEM. Success is
  93. * indicated by comparing RETURN with OLD.
  94. */
  95. #include <asm-generic/cmpxchg-local.h>
  96. static inline unsigned long
  97. __cmpxchg_u64(volatile long *m, unsigned long old, unsigned long new)
  98. {
  99. __asm__ __volatile__("casx [%2], %3, %0"
  100. : "=&r" (new)
  101. : "0" (new), "r" (m), "r" (old)
  102. : "memory");
  103. return new;
  104. }
  105. /*
  106. * Use 4 byte cas instruction to achieve 1 byte cmpxchg. Main logic
  107. * here is to get the bit shift of the byte we are interested in.
  108. * The XOR is handy for reversing the bits for big-endian byte order
  109. */
  110. static inline unsigned long
  111. __cmpxchg_u8(volatile unsigned char *m, unsigned char old, unsigned char new)
  112. {
  113. unsigned long maddr = (unsigned long)m;
  114. int bit_shift = (((unsigned long)m & 3) ^ 3) << 3;
  115. unsigned int mask = 0xff << bit_shift;
  116. unsigned int *ptr = (unsigned int *) (maddr & ~3);
  117. unsigned int old32, new32, load;
  118. unsigned int load32 = *ptr;
  119. do {
  120. new32 = (load32 & ~mask) | (new << bit_shift);
  121. old32 = (load32 & ~mask) | (old << bit_shift);
  122. load32 = __cmpxchg_u32(ptr, old32, new32);
  123. if (load32 == old32)
  124. return old;
  125. load = (load32 & mask) >> bit_shift;
  126. } while (load == old);
  127. return load;
  128. }
  129. /* This function doesn't exist, so you'll get a linker error
  130. if something tries to do an invalid cmpxchg(). */
  131. void __cmpxchg_called_with_bad_pointer(void);
  132. static inline unsigned long
  133. __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
  134. {
  135. switch (size) {
  136. case 1:
  137. return __cmpxchg_u8(ptr, old, new);
  138. case 4:
  139. return __cmpxchg_u32(ptr, old, new);
  140. case 8:
  141. return __cmpxchg_u64(ptr, old, new);
  142. }
  143. __cmpxchg_called_with_bad_pointer();
  144. return old;
  145. }
  146. #define arch_cmpxchg(ptr,o,n) \
  147. ({ \
  148. __typeof__(*(ptr)) _o_ = (o); \
  149. __typeof__(*(ptr)) _n_ = (n); \
  150. (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
  151. (unsigned long)_n_, sizeof(*(ptr))); \
  152. })
  153. /*
  154. * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
  155. * them available.
  156. */
  157. static inline unsigned long __cmpxchg_local(volatile void *ptr,
  158. unsigned long old,
  159. unsigned long new, int size)
  160. {
  161. switch (size) {
  162. case 4:
  163. case 8: return __cmpxchg(ptr, old, new, size);
  164. default:
  165. return __generic_cmpxchg_local(ptr, old, new, size);
  166. }
  167. return old;
  168. }
  169. #define arch_cmpxchg_local(ptr, o, n) \
  170. ((__typeof__(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(o), \
  171. (unsigned long)(n), sizeof(*(ptr))))
  172. #define arch_cmpxchg64_local(ptr, o, n) \
  173. ({ \
  174. BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
  175. arch_cmpxchg_local((ptr), (o), (n)); \
  176. })
  177. #define arch_cmpxchg64(ptr, o, n) arch_cmpxchg64_local((ptr), (o), (n))
  178. #endif /* __ARCH_SPARC64_CMPXCHG__ */