cmpxchg.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * 1,2 and 4 byte cmpxchg and xchg implementations for OpenRISC.
  3. *
  4. * Copyright (C) 2014 Stefan Kristiansson <[email protected]>
  5. * Copyright (C) 2017 Stafford Horne <[email protected]>
  6. *
  7. * This file is licensed under the terms of the GNU General Public License
  8. * version 2. This program is licensed "as is" without any warranty of any
  9. * kind, whether express or implied.
  10. *
  11. * Note:
  12. * The portable implementations of 1 and 2 byte xchg and cmpxchg using a 4
  13. * byte cmpxchg is sourced heavily from the sh and mips implementations.
  14. */
  15. #ifndef __ASM_OPENRISC_CMPXCHG_H
  16. #define __ASM_OPENRISC_CMPXCHG_H
  17. #include <linux/bits.h>
  18. #include <linux/compiler.h>
  19. #include <linux/types.h>
  20. #define __HAVE_ARCH_CMPXCHG 1
  21. static inline unsigned long cmpxchg_u32(volatile void *ptr,
  22. unsigned long old, unsigned long new)
  23. {
  24. __asm__ __volatile__(
  25. "1: l.lwa %0, 0(%1) \n"
  26. " l.sfeq %0, %2 \n"
  27. " l.bnf 2f \n"
  28. " l.nop \n"
  29. " l.swa 0(%1), %3 \n"
  30. " l.bnf 1b \n"
  31. " l.nop \n"
  32. "2: \n"
  33. : "=&r"(old)
  34. : "r"(ptr), "r"(old), "r"(new)
  35. : "cc", "memory");
  36. return old;
  37. }
  38. static inline unsigned long xchg_u32(volatile void *ptr,
  39. unsigned long val)
  40. {
  41. __asm__ __volatile__(
  42. "1: l.lwa %0, 0(%1) \n"
  43. " l.swa 0(%1), %2 \n"
  44. " l.bnf 1b \n"
  45. " l.nop \n"
  46. : "=&r"(val)
  47. : "r"(ptr), "r"(val)
  48. : "cc", "memory");
  49. return val;
  50. }
  51. static inline u32 cmpxchg_small(volatile void *ptr, u32 old, u32 new,
  52. int size)
  53. {
  54. int off = (unsigned long)ptr % sizeof(u32);
  55. volatile u32 *p = ptr - off;
  56. #ifdef __BIG_ENDIAN
  57. int bitoff = (sizeof(u32) - size - off) * BITS_PER_BYTE;
  58. #else
  59. int bitoff = off * BITS_PER_BYTE;
  60. #endif
  61. u32 bitmask = ((0x1 << size * BITS_PER_BYTE) - 1) << bitoff;
  62. u32 load32, old32, new32;
  63. u32 ret;
  64. load32 = READ_ONCE(*p);
  65. while (true) {
  66. ret = (load32 & bitmask) >> bitoff;
  67. if (old != ret)
  68. return ret;
  69. old32 = (load32 & ~bitmask) | (old << bitoff);
  70. new32 = (load32 & ~bitmask) | (new << bitoff);
  71. /* Do 32 bit cmpxchg */
  72. load32 = cmpxchg_u32(p, old32, new32);
  73. if (load32 == old32)
  74. return old;
  75. }
  76. }
  77. /* xchg */
  78. static inline u32 xchg_small(volatile void *ptr, u32 x, int size)
  79. {
  80. int off = (unsigned long)ptr % sizeof(u32);
  81. volatile u32 *p = ptr - off;
  82. #ifdef __BIG_ENDIAN
  83. int bitoff = (sizeof(u32) - size - off) * BITS_PER_BYTE;
  84. #else
  85. int bitoff = off * BITS_PER_BYTE;
  86. #endif
  87. u32 bitmask = ((0x1 << size * BITS_PER_BYTE) - 1) << bitoff;
  88. u32 oldv, newv;
  89. u32 ret;
  90. do {
  91. oldv = READ_ONCE(*p);
  92. ret = (oldv & bitmask) >> bitoff;
  93. newv = (oldv & ~bitmask) | (x << bitoff);
  94. } while (cmpxchg_u32(p, oldv, newv) != oldv);
  95. return ret;
  96. }
  97. /*
  98. * This function doesn't exist, so you'll get a linker error
  99. * if something tries to do an invalid cmpxchg().
  100. */
  101. extern unsigned long __cmpxchg_called_with_bad_pointer(void)
  102. __compiletime_error("Bad argument size for cmpxchg");
  103. static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
  104. unsigned long new, int size)
  105. {
  106. switch (size) {
  107. case 1:
  108. case 2:
  109. return cmpxchg_small(ptr, old, new, size);
  110. case 4:
  111. return cmpxchg_u32(ptr, old, new);
  112. default:
  113. return __cmpxchg_called_with_bad_pointer();
  114. }
  115. }
  116. #define arch_cmpxchg(ptr, o, n) \
  117. ({ \
  118. (__typeof__(*(ptr))) __cmpxchg((ptr), \
  119. (unsigned long)(o), \
  120. (unsigned long)(n), \
  121. sizeof(*(ptr))); \
  122. })
  123. /*
  124. * This function doesn't exist, so you'll get a linker error if
  125. * something tries to do an invalidly-sized xchg().
  126. */
  127. extern unsigned long __xchg_called_with_bad_pointer(void)
  128. __compiletime_error("Bad argument size for xchg");
  129. static inline unsigned long __xchg(volatile void *ptr, unsigned long with,
  130. int size)
  131. {
  132. switch (size) {
  133. case 1:
  134. case 2:
  135. return xchg_small(ptr, with, size);
  136. case 4:
  137. return xchg_u32(ptr, with);
  138. default:
  139. return __xchg_called_with_bad_pointer();
  140. }
  141. }
  142. #define arch_xchg(ptr, with) \
  143. ({ \
  144. (__typeof__(*(ptr))) __xchg((ptr), \
  145. (unsigned long)(with), \
  146. sizeof(*(ptr))); \
  147. })
  148. #endif /* __ASM_OPENRISC_CMPXCHG_H */