cmpxchg.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* xchg and cmpxchg operation emulation for FR-V
  2. *
  3. * For an explanation of how atomic ops work in this arch, see:
  4. * Documentation/frv/atomic-ops.txt
  5. *
  6. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  7. * Written by David Howells (dhowells@redhat.com)
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #ifndef _ASM_CMPXCHG_H
  15. #define _ASM_CMPXCHG_H
  16. #include <linux/types.h>
  17. /*****************************************************************************/
  18. /*
  19. * exchange value with memory
  20. */
  21. extern uint64_t __xchg_64(uint64_t i, volatile void *v);
  22. #ifndef CONFIG_FRV_OUTOFLINE_ATOMIC_OPS
  23. #define xchg(ptr, x) \
  24. ({ \
  25. __typeof__(ptr) __xg_ptr = (ptr); \
  26. __typeof__(*(ptr)) __xg_orig; \
  27. \
  28. switch (sizeof(__xg_orig)) { \
  29. case 4: \
  30. asm volatile( \
  31. "swap%I0 %M0,%1" \
  32. : "+m"(*__xg_ptr), "=r"(__xg_orig) \
  33. : "1"(x) \
  34. : "memory" \
  35. ); \
  36. break; \
  37. \
  38. default: \
  39. __xg_orig = (__typeof__(__xg_orig))0; \
  40. asm volatile("break"); \
  41. break; \
  42. } \
  43. \
  44. __xg_orig; \
  45. })
  46. #else
  47. extern uint32_t __xchg_32(uint32_t i, volatile void *v);
  48. #define xchg(ptr, x) \
  49. ({ \
  50. __typeof__(ptr) __xg_ptr = (ptr); \
  51. __typeof__(*(ptr)) __xg_orig; \
  52. \
  53. switch (sizeof(__xg_orig)) { \
  54. case 4: __xg_orig = (__typeof__(*(ptr))) __xchg_32((uint32_t) x, __xg_ptr); break; \
  55. default: \
  56. __xg_orig = (__typeof__(__xg_orig))0; \
  57. asm volatile("break"); \
  58. break; \
  59. } \
  60. __xg_orig; \
  61. })
  62. #endif
  63. #define tas(ptr) (xchg((ptr), 1))
  64. /*****************************************************************************/
  65. /*
  66. * compare and conditionally exchange value with memory
  67. * - if (*ptr == test) then orig = *ptr; *ptr = test;
  68. * - if (*ptr != test) then orig = *ptr;
  69. */
  70. extern uint64_t __cmpxchg_64(uint64_t test, uint64_t new, volatile uint64_t *v);
  71. #ifndef CONFIG_FRV_OUTOFLINE_ATOMIC_OPS
  72. #define cmpxchg(ptr, test, new) \
  73. ({ \
  74. __typeof__(ptr) __xg_ptr = (ptr); \
  75. __typeof__(*(ptr)) __xg_orig, __xg_tmp; \
  76. __typeof__(*(ptr)) __xg_test = (test); \
  77. __typeof__(*(ptr)) __xg_new = (new); \
  78. \
  79. switch (sizeof(__xg_orig)) { \
  80. case 4: \
  81. asm volatile( \
  82. "0: \n" \
  83. " orcc gr0,gr0,gr0,icc3 \n" \
  84. " ckeq icc3,cc7 \n" \
  85. " ld.p %M0,%1 \n" \
  86. " orcr cc7,cc7,cc3 \n" \
  87. " sub%I4cc %1,%4,%2,icc0 \n" \
  88. " bne icc0,#0,1f \n" \
  89. " cst.p %3,%M0 ,cc3,#1 \n" \
  90. " corcc gr29,gr29,gr0 ,cc3,#1 \n" \
  91. " beq icc3,#0,0b \n" \
  92. "1: \n" \
  93. : "+U"(*__xg_ptr), "=&r"(__xg_orig), "=&r"(__xg_tmp) \
  94. : "r"(__xg_new), "NPr"(__xg_test) \
  95. : "memory", "cc7", "cc3", "icc3", "icc0" \
  96. ); \
  97. break; \
  98. \
  99. default: \
  100. __xg_orig = (__typeof__(__xg_orig))0; \
  101. asm volatile("break"); \
  102. break; \
  103. } \
  104. \
  105. __xg_orig; \
  106. })
  107. #else
  108. extern uint32_t __cmpxchg_32(uint32_t *v, uint32_t test, uint32_t new);
  109. #define cmpxchg(ptr, test, new) \
  110. ({ \
  111. __typeof__(ptr) __xg_ptr = (ptr); \
  112. __typeof__(*(ptr)) __xg_orig; \
  113. __typeof__(*(ptr)) __xg_test = (test); \
  114. __typeof__(*(ptr)) __xg_new = (new); \
  115. \
  116. switch (sizeof(__xg_orig)) { \
  117. case 4: __xg_orig = (__force __typeof__(*ptr)) \
  118. __cmpxchg_32((__force uint32_t *)__xg_ptr, \
  119. (__force uint32_t)__xg_test, \
  120. (__force uint32_t)__xg_new); break; \
  121. default: \
  122. __xg_orig = (__typeof__(__xg_orig))0; \
  123. asm volatile("break"); \
  124. break; \
  125. } \
  126. \
  127. __xg_orig; \
  128. })
  129. #endif
  130. #include <asm-generic/cmpxchg-local.h>
  131. static inline unsigned long __cmpxchg_local(volatile void *ptr,
  132. unsigned long old,
  133. unsigned long new, int size)
  134. {
  135. switch (size) {
  136. case 4:
  137. return cmpxchg((unsigned long *)ptr, old, new);
  138. default:
  139. return __cmpxchg_local_generic(ptr, old, new, size);
  140. }
  141. return old;
  142. }
  143. /*
  144. * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
  145. * them available.
  146. */
  147. #define cmpxchg_local(ptr, o, n) \
  148. ((__typeof__(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(o), \
  149. (unsigned long)(n), sizeof(*(ptr))))
  150. #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
  151. #endif /* _ASM_CMPXCHG_H */