cmpxchg.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. smp_mb();
  35. prefetchw((const void *)ptr);
  36. switch (size) {
  37. #if __LINUX_ARM_ARCH__ >= 6
  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 4:
  49. asm volatile("@ __xchg4\n"
  50. "1: ldrex %0, [%3]\n"
  51. " strex %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. #elif defined(swp_is_buggy)
  59. #ifdef CONFIG_SMP
  60. #error SMP is not supported on this platform
  61. #endif
  62. case 1:
  63. raw_local_irq_save(flags);
  64. ret = *(volatile unsigned char *)ptr;
  65. *(volatile unsigned char *)ptr = x;
  66. raw_local_irq_restore(flags);
  67. break;
  68. case 4:
  69. raw_local_irq_save(flags);
  70. ret = *(volatile unsigned long *)ptr;
  71. *(volatile unsigned long *)ptr = x;
  72. raw_local_irq_restore(flags);
  73. break;
  74. #else
  75. case 1:
  76. asm volatile("@ __xchg1\n"
  77. " swpb %0, %1, [%2]"
  78. : "=&r" (ret)
  79. : "r" (x), "r" (ptr)
  80. : "memory", "cc");
  81. break;
  82. case 4:
  83. asm volatile("@ __xchg4\n"
  84. " swp %0, %1, [%2]"
  85. : "=&r" (ret)
  86. : "r" (x), "r" (ptr)
  87. : "memory", "cc");
  88. break;
  89. #endif
  90. default:
  91. __bad_xchg(ptr, size), ret = 0;
  92. break;
  93. }
  94. smp_mb();
  95. return ret;
  96. }
  97. #define xchg(ptr,x) \
  98. ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
  99. #include <asm-generic/cmpxchg-local.h>
  100. #if __LINUX_ARM_ARCH__ < 6
  101. /* min ARCH < ARMv6 */
  102. #ifdef CONFIG_SMP
  103. #error "SMP is not supported on this platform"
  104. #endif
  105. /*
  106. * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
  107. * them available.
  108. */
  109. #define cmpxchg_local(ptr, o, n) \
  110. ((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\
  111. (unsigned long)(n), sizeof(*(ptr))))
  112. #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
  113. #ifndef CONFIG_SMP
  114. #include <asm-generic/cmpxchg.h>
  115. #endif
  116. #else /* min ARCH >= ARMv6 */
  117. extern void __bad_cmpxchg(volatile void *ptr, int size);
  118. /*
  119. * cmpxchg only support 32-bits operands on ARMv6.
  120. */
  121. static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
  122. unsigned long new, int size)
  123. {
  124. unsigned long oldval, res;
  125. prefetchw((const void *)ptr);
  126. switch (size) {
  127. #ifndef CONFIG_CPU_V6 /* min ARCH >= ARMv6K */
  128. case 1:
  129. do {
  130. asm volatile("@ __cmpxchg1\n"
  131. " ldrexb %1, [%2]\n"
  132. " mov %0, #0\n"
  133. " teq %1, %3\n"
  134. " strexbeq %0, %4, [%2]\n"
  135. : "=&r" (res), "=&r" (oldval)
  136. : "r" (ptr), "Ir" (old), "r" (new)
  137. : "memory", "cc");
  138. } while (res);
  139. break;
  140. case 2:
  141. do {
  142. asm volatile("@ __cmpxchg1\n"
  143. " ldrexh %1, [%2]\n"
  144. " mov %0, #0\n"
  145. " teq %1, %3\n"
  146. " strexheq %0, %4, [%2]\n"
  147. : "=&r" (res), "=&r" (oldval)
  148. : "r" (ptr), "Ir" (old), "r" (new)
  149. : "memory", "cc");
  150. } while (res);
  151. break;
  152. #endif
  153. case 4:
  154. do {
  155. asm volatile("@ __cmpxchg4\n"
  156. " ldrex %1, [%2]\n"
  157. " mov %0, #0\n"
  158. " teq %1, %3\n"
  159. " strexeq %0, %4, [%2]\n"
  160. : "=&r" (res), "=&r" (oldval)
  161. : "r" (ptr), "Ir" (old), "r" (new)
  162. : "memory", "cc");
  163. } while (res);
  164. break;
  165. default:
  166. __bad_cmpxchg(ptr, size);
  167. oldval = 0;
  168. }
  169. return oldval;
  170. }
  171. static inline unsigned long __cmpxchg_mb(volatile void *ptr, unsigned long old,
  172. unsigned long new, int size)
  173. {
  174. unsigned long ret;
  175. smp_mb();
  176. ret = __cmpxchg(ptr, old, new, size);
  177. smp_mb();
  178. return ret;
  179. }
  180. #define cmpxchg(ptr,o,n) \
  181. ((__typeof__(*(ptr)))__cmpxchg_mb((ptr), \
  182. (unsigned long)(o), \
  183. (unsigned long)(n), \
  184. sizeof(*(ptr))))
  185. static inline unsigned long __cmpxchg_local(volatile void *ptr,
  186. unsigned long old,
  187. unsigned long new, int size)
  188. {
  189. unsigned long ret;
  190. switch (size) {
  191. #ifdef CONFIG_CPU_V6 /* min ARCH == ARMv6 */
  192. case 1:
  193. case 2:
  194. ret = __cmpxchg_local_generic(ptr, old, new, size);
  195. break;
  196. #endif
  197. default:
  198. ret = __cmpxchg(ptr, old, new, size);
  199. }
  200. return ret;
  201. }
  202. static inline unsigned long long __cmpxchg64(unsigned long long *ptr,
  203. unsigned long long old,
  204. unsigned long long new)
  205. {
  206. unsigned long long oldval;
  207. unsigned long res;
  208. prefetchw(ptr);
  209. __asm__ __volatile__(
  210. "1: ldrexd %1, %H1, [%3]\n"
  211. " teq %1, %4\n"
  212. " teqeq %H1, %H4\n"
  213. " bne 2f\n"
  214. " strexd %0, %5, %H5, [%3]\n"
  215. " teq %0, #0\n"
  216. " bne 1b\n"
  217. "2:"
  218. : "=&r" (res), "=&r" (oldval), "+Qo" (*ptr)
  219. : "r" (ptr), "r" (old), "r" (new)
  220. : "cc");
  221. return oldval;
  222. }
  223. static inline unsigned long long __cmpxchg64_mb(unsigned long long *ptr,
  224. unsigned long long old,
  225. unsigned long long new)
  226. {
  227. unsigned long long ret;
  228. smp_mb();
  229. ret = __cmpxchg64(ptr, old, new);
  230. smp_mb();
  231. return ret;
  232. }
  233. #define cmpxchg_local(ptr,o,n) \
  234. ((__typeof__(*(ptr)))__cmpxchg_local((ptr), \
  235. (unsigned long)(o), \
  236. (unsigned long)(n), \
  237. sizeof(*(ptr))))
  238. #define cmpxchg64(ptr, o, n) \
  239. ((__typeof__(*(ptr)))__cmpxchg64_mb((ptr), \
  240. (unsigned long long)(o), \
  241. (unsigned long long)(n)))
  242. #define cmpxchg64_relaxed(ptr, o, n) \
  243. ((__typeof__(*(ptr)))__cmpxchg64((ptr), \
  244. (unsigned long long)(o), \
  245. (unsigned long long)(n)))
  246. #define cmpxchg64_local(ptr, o, n) cmpxchg64_relaxed((ptr), (o), (n))
  247. #endif /* __LINUX_ARM_ARCH__ >= 6 */
  248. #endif /* __ASM_ARM_CMPXCHG_H */