cmpxchg.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Based on arch/arm/include/asm/cmpxchg.h
  3. *
  4. * Copyright (C) 2012 ARM Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef __ASM_CMPXCHG_H
  19. #define __ASM_CMPXCHG_H
  20. #include <linux/bug.h>
  21. #include <asm/barrier.h>
  22. static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size)
  23. {
  24. unsigned long ret, tmp;
  25. switch (size) {
  26. case 1:
  27. asm volatile("// __xchg1\n"
  28. "1: ldxrb %w0, %2\n"
  29. " stlxrb %w1, %w3, %2\n"
  30. " cbnz %w1, 1b\n"
  31. : "=&r" (ret), "=&r" (tmp), "+Q" (*(u8 *)ptr)
  32. : "r" (x)
  33. : "memory");
  34. break;
  35. case 2:
  36. asm volatile("// __xchg2\n"
  37. "1: ldxrh %w0, %2\n"
  38. " stlxrh %w1, %w3, %2\n"
  39. " cbnz %w1, 1b\n"
  40. : "=&r" (ret), "=&r" (tmp), "+Q" (*(u16 *)ptr)
  41. : "r" (x)
  42. : "memory");
  43. break;
  44. case 4:
  45. asm volatile("// __xchg4\n"
  46. "1: ldxr %w0, %2\n"
  47. " stlxr %w1, %w3, %2\n"
  48. " cbnz %w1, 1b\n"
  49. : "=&r" (ret), "=&r" (tmp), "+Q" (*(u32 *)ptr)
  50. : "r" (x)
  51. : "memory");
  52. break;
  53. case 8:
  54. asm volatile("// __xchg8\n"
  55. "1: ldxr %0, %2\n"
  56. " stlxr %w1, %3, %2\n"
  57. " cbnz %w1, 1b\n"
  58. : "=&r" (ret), "=&r" (tmp), "+Q" (*(u64 *)ptr)
  59. : "r" (x)
  60. : "memory");
  61. break;
  62. default:
  63. BUILD_BUG();
  64. }
  65. smp_mb();
  66. return ret;
  67. }
  68. #define xchg(ptr,x) \
  69. ({ \
  70. __typeof__(*(ptr)) __ret; \
  71. __ret = (__typeof__(*(ptr))) \
  72. __xchg((unsigned long)(x), (ptr), sizeof(*(ptr))); \
  73. __ret; \
  74. })
  75. static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
  76. unsigned long new, int size)
  77. {
  78. unsigned long oldval = 0, res;
  79. switch (size) {
  80. case 1:
  81. do {
  82. asm volatile("// __cmpxchg1\n"
  83. " ldxrb %w1, %2\n"
  84. " mov %w0, #0\n"
  85. " cmp %w1, %w3\n"
  86. " b.ne 1f\n"
  87. " stxrb %w0, %w4, %2\n"
  88. "1:\n"
  89. : "=&r" (res), "=&r" (oldval), "+Q" (*(u8 *)ptr)
  90. : "Ir" (old), "r" (new)
  91. : "cc");
  92. } while (res);
  93. break;
  94. case 2:
  95. do {
  96. asm volatile("// __cmpxchg2\n"
  97. " ldxrh %w1, %2\n"
  98. " mov %w0, #0\n"
  99. " cmp %w1, %w3\n"
  100. " b.ne 1f\n"
  101. " stxrh %w0, %w4, %2\n"
  102. "1:\n"
  103. : "=&r" (res), "=&r" (oldval), "+Q" (*(u16 *)ptr)
  104. : "Ir" (old), "r" (new)
  105. : "cc");
  106. } while (res);
  107. break;
  108. case 4:
  109. do {
  110. asm volatile("// __cmpxchg4\n"
  111. " ldxr %w1, %2\n"
  112. " mov %w0, #0\n"
  113. " cmp %w1, %w3\n"
  114. " b.ne 1f\n"
  115. " stxr %w0, %w4, %2\n"
  116. "1:\n"
  117. : "=&r" (res), "=&r" (oldval), "+Q" (*(u32 *)ptr)
  118. : "Ir" (old), "r" (new)
  119. : "cc");
  120. } while (res);
  121. break;
  122. case 8:
  123. do {
  124. asm volatile("// __cmpxchg8\n"
  125. " ldxr %1, %2\n"
  126. " mov %w0, #0\n"
  127. " cmp %1, %3\n"
  128. " b.ne 1f\n"
  129. " stxr %w0, %4, %2\n"
  130. "1:\n"
  131. : "=&r" (res), "=&r" (oldval), "+Q" (*(u64 *)ptr)
  132. : "Ir" (old), "r" (new)
  133. : "cc");
  134. } while (res);
  135. break;
  136. default:
  137. BUILD_BUG();
  138. }
  139. return oldval;
  140. }
  141. static inline unsigned long __cmpxchg_mb(volatile void *ptr, unsigned long old,
  142. unsigned long new, int size)
  143. {
  144. unsigned long ret;
  145. smp_mb();
  146. ret = __cmpxchg(ptr, old, new, size);
  147. smp_mb();
  148. return ret;
  149. }
  150. #define cmpxchg(ptr, o, n) \
  151. ({ \
  152. __typeof__(*(ptr)) __ret; \
  153. __ret = (__typeof__(*(ptr))) \
  154. __cmpxchg_mb((ptr), (unsigned long)(o), (unsigned long)(n), \
  155. sizeof(*(ptr))); \
  156. __ret; \
  157. })
  158. #define cmpxchg_local(ptr, o, n) \
  159. ({ \
  160. __typeof__(*(ptr)) __ret; \
  161. __ret = (__typeof__(*(ptr))) \
  162. __cmpxchg((ptr), (unsigned long)(o), \
  163. (unsigned long)(n), sizeof(*(ptr))); \
  164. __ret; \
  165. })
  166. #define cmpxchg64(ptr,o,n) cmpxchg((ptr),(o),(n))
  167. #define cmpxchg64_local(ptr,o,n) cmpxchg_local((ptr),(o),(n))
  168. #define cmpxchg64_relaxed(ptr,o,n) cmpxchg_local((ptr),(o),(n))
  169. #endif /* __ASM_CMPXCHG_H */