cmpxchg.h 3.9 KB

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