cmpxchg.h 6.1 KB

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