cmpxchg.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ASM_ARM_CMPXCHG_H
  3. #define __ASM_ARM_CMPXCHG_H
  4. #include <linux/irqflags.h>
  5. #include <linux/prefetch.h>
  6. #include <asm/barrier.h>
  7. #if defined(CONFIG_CPU_SA1100) || defined(CONFIG_CPU_SA110)
  8. /*
  9. * On the StrongARM, "swp" is terminally broken since it bypasses the
  10. * cache totally. This means that the cache becomes inconsistent, and,
  11. * since we use normal loads/stores as well, this is really bad.
  12. * Typically, this causes oopsen in filp_close, but could have other,
  13. * more disastrous effects. There are two work-arounds:
  14. * 1. Disable interrupts and emulate the atomic swap
  15. * 2. Clean the cache, perform atomic swap, flush the cache
  16. *
  17. * We choose (1) since its the "easiest" to achieve here and is not
  18. * dependent on the processor type.
  19. *
  20. * NOTE that this solution won't work on an SMP system, so explcitly
  21. * forbid it here.
  22. */
  23. #define swp_is_buggy
  24. #endif
  25. static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size)
  26. {
  27. extern void __bad_xchg(volatile void *, int);
  28. unsigned long ret;
  29. #ifdef swp_is_buggy
  30. unsigned long flags;
  31. #endif
  32. #if __LINUX_ARM_ARCH__ >= 6
  33. unsigned int tmp;
  34. #endif
  35. prefetchw((const void *)ptr);
  36. switch (size) {
  37. #if __LINUX_ARM_ARCH__ >= 6
  38. #ifndef CONFIG_CPU_V6 /* MIN ARCH >= V6K */
  39. case 1:
  40. asm volatile("@ __xchg1\n"
  41. "1: ldrexb %0, [%3]\n"
  42. " strexb %1, %2, [%3]\n"
  43. " teq %1, #0\n"
  44. " bne 1b"
  45. : "=&r" (ret), "=&r" (tmp)
  46. : "r" (x), "r" (ptr)
  47. : "memory", "cc");
  48. break;
  49. case 2:
  50. asm volatile("@ __xchg2\n"
  51. "1: ldrexh %0, [%3]\n"
  52. " strexh %1, %2, [%3]\n"
  53. " teq %1, #0\n"
  54. " bne 1b"
  55. : "=&r" (ret), "=&r" (tmp)
  56. : "r" (x), "r" (ptr)
  57. : "memory", "cc");
  58. break;
  59. #endif
  60. case 4:
  61. asm volatile("@ __xchg4\n"
  62. "1: ldrex %0, [%3]\n"
  63. " strex %1, %2, [%3]\n"
  64. " teq %1, #0\n"
  65. " bne 1b"
  66. : "=&r" (ret), "=&r" (tmp)
  67. : "r" (x), "r" (ptr)
  68. : "memory", "cc");
  69. break;
  70. #elif defined(swp_is_buggy)
  71. #ifdef CONFIG_SMP
  72. #error SMP is not supported on this platform
  73. #endif
  74. case 1:
  75. raw_local_irq_save(flags);
  76. ret = *(volatile unsigned char *)ptr;
  77. *(volatile unsigned char *)ptr = x;
  78. raw_local_irq_restore(flags);
  79. break;
  80. case 4:
  81. raw_local_irq_save(flags);
  82. ret = *(volatile unsigned long *)ptr;
  83. *(volatile unsigned long *)ptr = x;
  84. raw_local_irq_restore(flags);
  85. break;
  86. #else
  87. case 1:
  88. asm volatile("@ __xchg1\n"
  89. " swpb %0, %1, [%2]"
  90. : "=&r" (ret)
  91. : "r" (x), "r" (ptr)
  92. : "memory", "cc");
  93. break;
  94. case 4:
  95. asm volatile("@ __xchg4\n"
  96. " swp %0, %1, [%2]"
  97. : "=&r" (ret)
  98. : "r" (x), "r" (ptr)
  99. : "memory", "cc");
  100. break;
  101. #endif
  102. default:
  103. /* Cause a link-time error, the xchg() size is not supported */
  104. __bad_xchg(ptr, size), ret = 0;
  105. break;
  106. }
  107. return ret;
  108. }
  109. #define arch_xchg_relaxed(ptr, x) ({ \
  110. (__typeof__(*(ptr)))__xchg((unsigned long)(x), (ptr), \
  111. sizeof(*(ptr))); \
  112. })
  113. #include <asm-generic/cmpxchg-local.h>
  114. #if __LINUX_ARM_ARCH__ < 6
  115. /* min ARCH < ARMv6 */
  116. #ifdef CONFIG_SMP
  117. #error "SMP is not supported on this platform"
  118. #endif
  119. #define arch_xchg arch_xchg_relaxed
  120. /*
  121. * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
  122. * them available.
  123. */
  124. #define arch_cmpxchg_local(ptr, o, n) ({ \
  125. (__typeof(*ptr))__generic_cmpxchg_local((ptr), \
  126. (unsigned long)(o), \
  127. (unsigned long)(n), \
  128. sizeof(*(ptr))); \
  129. })
  130. #define arch_cmpxchg64_local(ptr, o, n) __generic_cmpxchg64_local((ptr), (o), (n))
  131. #include <asm-generic/cmpxchg.h>
  132. #else /* min ARCH >= ARMv6 */
  133. extern void __bad_cmpxchg(volatile void *ptr, int size);
  134. /*
  135. * cmpxchg only support 32-bits operands on ARMv6.
  136. */
  137. static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
  138. unsigned long new, int size)
  139. {
  140. unsigned long oldval, res;
  141. prefetchw((const void *)ptr);
  142. switch (size) {
  143. #ifndef CONFIG_CPU_V6 /* min ARCH >= ARMv6K */
  144. case 1:
  145. do {
  146. asm volatile("@ __cmpxchg1\n"
  147. " ldrexb %1, [%2]\n"
  148. " mov %0, #0\n"
  149. " teq %1, %3\n"
  150. " strexbeq %0, %4, [%2]\n"
  151. : "=&r" (res), "=&r" (oldval)
  152. : "r" (ptr), "Ir" (old), "r" (new)
  153. : "memory", "cc");
  154. } while (res);
  155. break;
  156. case 2:
  157. do {
  158. asm volatile("@ __cmpxchg1\n"
  159. " ldrexh %1, [%2]\n"
  160. " mov %0, #0\n"
  161. " teq %1, %3\n"
  162. " strexheq %0, %4, [%2]\n"
  163. : "=&r" (res), "=&r" (oldval)
  164. : "r" (ptr), "Ir" (old), "r" (new)
  165. : "memory", "cc");
  166. } while (res);
  167. break;
  168. #endif
  169. case 4:
  170. do {
  171. asm volatile("@ __cmpxchg4\n"
  172. " ldrex %1, [%2]\n"
  173. " mov %0, #0\n"
  174. " teq %1, %3\n"
  175. " strexeq %0, %4, [%2]\n"
  176. : "=&r" (res), "=&r" (oldval)
  177. : "r" (ptr), "Ir" (old), "r" (new)
  178. : "memory", "cc");
  179. } while (res);
  180. break;
  181. default:
  182. __bad_cmpxchg(ptr, size);
  183. oldval = 0;
  184. }
  185. return oldval;
  186. }
  187. #define arch_cmpxchg_relaxed(ptr,o,n) ({ \
  188. (__typeof__(*(ptr)))__cmpxchg((ptr), \
  189. (unsigned long)(o), \
  190. (unsigned long)(n), \
  191. sizeof(*(ptr))); \
  192. })
  193. static inline unsigned long __cmpxchg_local(volatile void *ptr,
  194. unsigned long old,
  195. unsigned long new, int size)
  196. {
  197. unsigned long ret;
  198. switch (size) {
  199. #ifdef CONFIG_CPU_V6 /* min ARCH == ARMv6 */
  200. case 1:
  201. case 2:
  202. ret = __generic_cmpxchg_local(ptr, old, new, size);
  203. break;
  204. #endif
  205. default:
  206. ret = __cmpxchg(ptr, old, new, size);
  207. }
  208. return ret;
  209. }
  210. #define arch_cmpxchg_local(ptr, o, n) ({ \
  211. (__typeof(*ptr))__cmpxchg_local((ptr), \
  212. (unsigned long)(o), \
  213. (unsigned long)(n), \
  214. sizeof(*(ptr))); \
  215. })
  216. static inline unsigned long long __cmpxchg64(unsigned long long *ptr,
  217. unsigned long long old,
  218. unsigned long long new)
  219. {
  220. unsigned long long oldval;
  221. unsigned long res;
  222. prefetchw(ptr);
  223. __asm__ __volatile__(
  224. "1: ldrexd %1, %H1, [%3]\n"
  225. " teq %1, %4\n"
  226. " teqeq %H1, %H4\n"
  227. " bne 2f\n"
  228. " strexd %0, %5, %H5, [%3]\n"
  229. " teq %0, #0\n"
  230. " bne 1b\n"
  231. "2:"
  232. : "=&r" (res), "=&r" (oldval), "+Qo" (*ptr)
  233. : "r" (ptr), "r" (old), "r" (new)
  234. : "cc");
  235. return oldval;
  236. }
  237. #define arch_cmpxchg64_relaxed(ptr, o, n) ({ \
  238. (__typeof__(*(ptr)))__cmpxchg64((ptr), \
  239. (unsigned long long)(o), \
  240. (unsigned long long)(n)); \
  241. })
  242. #define arch_cmpxchg64_local(ptr, o, n) arch_cmpxchg64_relaxed((ptr), (o), (n))
  243. #endif /* __LINUX_ARM_ARCH__ >= 6 */
  244. #endif /* __ASM_ARM_CMPXCHG_H */