cmpxchg.h 6.6 KB

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