cmpxchg.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright IBM Corp. 1999, 2011
  3. *
  4. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
  5. */
  6. #ifndef __ASM_CMPXCHG_H
  7. #define __ASM_CMPXCHG_H
  8. #include <linux/types.h>
  9. extern void __xchg_called_with_bad_pointer(void);
  10. static inline unsigned long __xchg(unsigned long x, void *ptr, int size)
  11. {
  12. unsigned long addr, old;
  13. int shift;
  14. switch (size) {
  15. case 1:
  16. addr = (unsigned long) ptr;
  17. shift = (3 ^ (addr & 3)) << 3;
  18. addr ^= addr & 3;
  19. asm volatile(
  20. " l %0,%4\n"
  21. "0: lr 0,%0\n"
  22. " nr 0,%3\n"
  23. " or 0,%2\n"
  24. " cs %0,0,%4\n"
  25. " jl 0b\n"
  26. : "=&d" (old), "=Q" (*(int *) addr)
  27. : "d" (x << shift), "d" (~(255 << shift)),
  28. "Q" (*(int *) addr) : "memory", "cc", "0");
  29. return old >> shift;
  30. case 2:
  31. addr = (unsigned long) ptr;
  32. shift = (2 ^ (addr & 2)) << 3;
  33. addr ^= addr & 2;
  34. asm volatile(
  35. " l %0,%4\n"
  36. "0: lr 0,%0\n"
  37. " nr 0,%3\n"
  38. " or 0,%2\n"
  39. " cs %0,0,%4\n"
  40. " jl 0b\n"
  41. : "=&d" (old), "=Q" (*(int *) addr)
  42. : "d" (x << shift), "d" (~(65535 << shift)),
  43. "Q" (*(int *) addr) : "memory", "cc", "0");
  44. return old >> shift;
  45. case 4:
  46. asm volatile(
  47. " l %0,%3\n"
  48. "0: cs %0,%2,%3\n"
  49. " jl 0b\n"
  50. : "=&d" (old), "=Q" (*(int *) ptr)
  51. : "d" (x), "Q" (*(int *) ptr)
  52. : "memory", "cc");
  53. return old;
  54. #ifdef CONFIG_64BIT
  55. case 8:
  56. asm volatile(
  57. " lg %0,%3\n"
  58. "0: csg %0,%2,%3\n"
  59. " jl 0b\n"
  60. : "=&d" (old), "=m" (*(long *) ptr)
  61. : "d" (x), "Q" (*(long *) ptr)
  62. : "memory", "cc");
  63. return old;
  64. #endif /* CONFIG_64BIT */
  65. }
  66. __xchg_called_with_bad_pointer();
  67. return x;
  68. }
  69. #define xchg(ptr, x) \
  70. ({ \
  71. __typeof__(*(ptr)) __ret; \
  72. __ret = (__typeof__(*(ptr))) \
  73. __xchg((unsigned long)(x), (void *)(ptr), sizeof(*(ptr)));\
  74. __ret; \
  75. })
  76. /*
  77. * Atomic compare and exchange. Compare OLD with MEM, if identical,
  78. * store NEW in MEM. Return the initial value in MEM. Success is
  79. * indicated by comparing RETURN with OLD.
  80. */
  81. #define __HAVE_ARCH_CMPXCHG
  82. extern void __cmpxchg_called_with_bad_pointer(void);
  83. static inline unsigned long __cmpxchg(void *ptr, unsigned long old,
  84. unsigned long new, int size)
  85. {
  86. unsigned long addr, prev, tmp;
  87. int shift;
  88. switch (size) {
  89. case 1:
  90. addr = (unsigned long) ptr;
  91. shift = (3 ^ (addr & 3)) << 3;
  92. addr ^= addr & 3;
  93. asm volatile(
  94. " l %0,%2\n"
  95. "0: nr %0,%5\n"
  96. " lr %1,%0\n"
  97. " or %0,%3\n"
  98. " or %1,%4\n"
  99. " cs %0,%1,%2\n"
  100. " jnl 1f\n"
  101. " xr %1,%0\n"
  102. " nr %1,%5\n"
  103. " jnz 0b\n"
  104. "1:"
  105. : "=&d" (prev), "=&d" (tmp), "=Q" (*(int *) ptr)
  106. : "d" (old << shift), "d" (new << shift),
  107. "d" (~(255 << shift)), "Q" (*(int *) ptr)
  108. : "memory", "cc");
  109. return prev >> shift;
  110. case 2:
  111. addr = (unsigned long) ptr;
  112. shift = (2 ^ (addr & 2)) << 3;
  113. addr ^= addr & 2;
  114. asm volatile(
  115. " l %0,%2\n"
  116. "0: nr %0,%5\n"
  117. " lr %1,%0\n"
  118. " or %0,%3\n"
  119. " or %1,%4\n"
  120. " cs %0,%1,%2\n"
  121. " jnl 1f\n"
  122. " xr %1,%0\n"
  123. " nr %1,%5\n"
  124. " jnz 0b\n"
  125. "1:"
  126. : "=&d" (prev), "=&d" (tmp), "=Q" (*(int *) ptr)
  127. : "d" (old << shift), "d" (new << shift),
  128. "d" (~(65535 << shift)), "Q" (*(int *) ptr)
  129. : "memory", "cc");
  130. return prev >> shift;
  131. case 4:
  132. asm volatile(
  133. " cs %0,%3,%1\n"
  134. : "=&d" (prev), "=Q" (*(int *) ptr)
  135. : "0" (old), "d" (new), "Q" (*(int *) ptr)
  136. : "memory", "cc");
  137. return prev;
  138. #ifdef CONFIG_64BIT
  139. case 8:
  140. asm volatile(
  141. " csg %0,%3,%1\n"
  142. : "=&d" (prev), "=Q" (*(long *) ptr)
  143. : "0" (old), "d" (new), "Q" (*(long *) ptr)
  144. : "memory", "cc");
  145. return prev;
  146. #endif /* CONFIG_64BIT */
  147. }
  148. __cmpxchg_called_with_bad_pointer();
  149. return old;
  150. }
  151. #define cmpxchg(ptr, o, n) \
  152. ((__typeof__(*(ptr)))__cmpxchg((ptr), (unsigned long)(o), \
  153. (unsigned long)(n), sizeof(*(ptr))))
  154. #include <asm-generic/cmpxchg-local.h>
  155. static inline unsigned long __cmpxchg_local(void *ptr,
  156. unsigned long old,
  157. unsigned long new, int size)
  158. {
  159. switch (size) {
  160. case 1:
  161. case 2:
  162. case 4:
  163. #ifdef CONFIG_64BIT
  164. case 8:
  165. #endif
  166. return __cmpxchg(ptr, old, new, size);
  167. default:
  168. return __cmpxchg_local_generic(ptr, old, new, size);
  169. }
  170. return old;
  171. }
  172. /*
  173. * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
  174. * them available.
  175. */
  176. #define cmpxchg_local(ptr, o, n) \
  177. ((__typeof__(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(o), \
  178. (unsigned long)(n), sizeof(*(ptr))))
  179. #ifdef CONFIG_64BIT
  180. #define cmpxchg64_local(ptr, o, n) \
  181. ({ \
  182. BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
  183. cmpxchg_local((ptr), (o), (n)); \
  184. })
  185. #else
  186. #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
  187. #endif
  188. #endif /* __ASM_CMPXCHG_H */