cmpxchg.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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: ldaxrb %w0, [%3]\n"
  29. " stlxrb %w1, %w2, [%3]\n"
  30. " cbnz %w1, 1b\n"
  31. : "=&r" (ret), "=&r" (tmp)
  32. : "r" (x), "r" (ptr)
  33. : "memory", "cc");
  34. break;
  35. case 2:
  36. asm volatile("// __xchg2\n"
  37. "1: ldaxrh %w0, [%3]\n"
  38. " stlxrh %w1, %w2, [%3]\n"
  39. " cbnz %w1, 1b\n"
  40. : "=&r" (ret), "=&r" (tmp)
  41. : "r" (x), "r" (ptr)
  42. : "memory", "cc");
  43. break;
  44. case 4:
  45. asm volatile("// __xchg4\n"
  46. "1: ldaxr %w0, [%3]\n"
  47. " stlxr %w1, %w2, [%3]\n"
  48. " cbnz %w1, 1b\n"
  49. : "=&r" (ret), "=&r" (tmp)
  50. : "r" (x), "r" (ptr)
  51. : "memory", "cc");
  52. break;
  53. case 8:
  54. asm volatile("// __xchg8\n"
  55. "1: ldaxr %0, [%3]\n"
  56. " stlxr %w1, %2, [%3]\n"
  57. " cbnz %w1, 1b\n"
  58. : "=&r" (ret), "=&r" (tmp)
  59. : "r" (x), "r" (ptr)
  60. : "memory", "cc");
  61. break;
  62. default:
  63. BUILD_BUG();
  64. }
  65. return ret;
  66. }
  67. #define xchg(ptr,x) \
  68. ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
  69. static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
  70. unsigned long new, int size)
  71. {
  72. unsigned long oldval = 0, res;
  73. switch (size) {
  74. case 1:
  75. do {
  76. asm volatile("// __cmpxchg1\n"
  77. " ldxrb %w1, [%2]\n"
  78. " mov %w0, #0\n"
  79. " cmp %w1, %w3\n"
  80. " b.ne 1f\n"
  81. " stxrb %w0, %w4, [%2]\n"
  82. "1:\n"
  83. : "=&r" (res), "=&r" (oldval)
  84. : "r" (ptr), "Ir" (old), "r" (new)
  85. : "cc");
  86. } while (res);
  87. break;
  88. case 2:
  89. do {
  90. asm volatile("// __cmpxchg2\n"
  91. " ldxrh %w1, [%2]\n"
  92. " mov %w0, #0\n"
  93. " cmp %w1, %w3\n"
  94. " b.ne 1f\n"
  95. " stxrh %w0, %w4, [%2]\n"
  96. "1:\n"
  97. : "=&r" (res), "=&r" (oldval)
  98. : "r" (ptr), "Ir" (old), "r" (new)
  99. : "memory", "cc");
  100. } while (res);
  101. break;
  102. case 4:
  103. do {
  104. asm volatile("// __cmpxchg4\n"
  105. " ldxr %w1, [%2]\n"
  106. " mov %w0, #0\n"
  107. " cmp %w1, %w3\n"
  108. " b.ne 1f\n"
  109. " stxr %w0, %w4, [%2]\n"
  110. "1:\n"
  111. : "=&r" (res), "=&r" (oldval)
  112. : "r" (ptr), "Ir" (old), "r" (new)
  113. : "cc");
  114. } while (res);
  115. break;
  116. case 8:
  117. do {
  118. asm volatile("// __cmpxchg8\n"
  119. " ldxr %1, [%2]\n"
  120. " mov %w0, #0\n"
  121. " cmp %1, %3\n"
  122. " b.ne 1f\n"
  123. " stxr %w0, %4, [%2]\n"
  124. "1:\n"
  125. : "=&r" (res), "=&r" (oldval)
  126. : "r" (ptr), "Ir" (old), "r" (new)
  127. : "cc");
  128. } while (res);
  129. break;
  130. default:
  131. BUILD_BUG();
  132. }
  133. return oldval;
  134. }
  135. static inline unsigned long __cmpxchg_mb(volatile void *ptr, unsigned long old,
  136. unsigned long new, int size)
  137. {
  138. unsigned long ret;
  139. smp_mb();
  140. ret = __cmpxchg(ptr, old, new, size);
  141. smp_mb();
  142. return ret;
  143. }
  144. #define cmpxchg(ptr,o,n) \
  145. ((__typeof__(*(ptr)))__cmpxchg_mb((ptr), \
  146. (unsigned long)(o), \
  147. (unsigned long)(n), \
  148. sizeof(*(ptr))))
  149. #define cmpxchg_local(ptr,o,n) \
  150. ((__typeof__(*(ptr)))__cmpxchg((ptr), \
  151. (unsigned long)(o), \
  152. (unsigned long)(n), \
  153. sizeof(*(ptr))))
  154. #endif /* __ASM_CMPXCHG_H */