spinlock.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ASM_SPINLOCK_H
  3. #define __ASM_SPINLOCK_H
  4. #if __LINUX_ARM_ARCH__ < 6
  5. #error SMP not supported on pre-ARMv6 CPUs
  6. #endif
  7. #include <linux/prefetch.h>
  8. #include <asm/barrier.h>
  9. #include <asm/processor.h>
  10. /*
  11. * sev and wfe are ARMv6K extensions. Uniprocessor ARMv6 may not have the K
  12. * extensions, so when running on UP, we have to patch these instructions away.
  13. */
  14. #ifdef CONFIG_THUMB2_KERNEL
  15. /*
  16. * For Thumb-2, special care is needed to ensure that the conditional WFE
  17. * instruction really does assemble to exactly 4 bytes (as required by
  18. * the SMP_ON_UP fixup code). By itself "wfene" might cause the
  19. * assembler to insert a extra (16-bit) IT instruction, depending on the
  20. * presence or absence of neighbouring conditional instructions.
  21. *
  22. * To avoid this unpredictability, an appropriate IT is inserted explicitly:
  23. * the assembler won't change IT instructions which are explicitly present
  24. * in the input.
  25. */
  26. #define WFE(cond) __ALT_SMP_ASM( \
  27. "it " cond "\n\t" \
  28. "wfe" cond ".n", \
  29. \
  30. "nop.w" \
  31. )
  32. #else
  33. #define WFE(cond) __ALT_SMP_ASM("wfe" cond, "nop")
  34. #endif
  35. #define SEV __ALT_SMP_ASM(WASM(sev), WASM(nop))
  36. static inline void dsb_sev(void)
  37. {
  38. dsb(ishst);
  39. __asm__(SEV);
  40. }
  41. /*
  42. * ARMv6 ticket-based spin-locking.
  43. *
  44. * A memory barrier is required after we get a lock, and before we
  45. * release it, because V6 CPUs are assumed to have weakly ordered
  46. * memory.
  47. */
  48. static inline void arch_spin_lock(arch_spinlock_t *lock)
  49. {
  50. unsigned long tmp;
  51. u32 newval;
  52. arch_spinlock_t lockval;
  53. prefetchw(&lock->slock);
  54. __asm__ __volatile__(
  55. "1: ldrex %0, [%3]\n"
  56. " add %1, %0, %4\n"
  57. " strex %2, %1, [%3]\n"
  58. " teq %2, #0\n"
  59. " bne 1b"
  60. : "=&r" (lockval), "=&r" (newval), "=&r" (tmp)
  61. : "r" (&lock->slock), "I" (1 << TICKET_SHIFT)
  62. : "cc");
  63. while (lockval.tickets.next != lockval.tickets.owner) {
  64. wfe();
  65. lockval.tickets.owner = READ_ONCE(lock->tickets.owner);
  66. }
  67. smp_mb();
  68. }
  69. static inline int arch_spin_trylock(arch_spinlock_t *lock)
  70. {
  71. unsigned long contended, res;
  72. u32 slock;
  73. prefetchw(&lock->slock);
  74. do {
  75. __asm__ __volatile__(
  76. " ldrex %0, [%3]\n"
  77. " mov %2, #0\n"
  78. " subs %1, %0, %0, ror #16\n"
  79. " addeq %0, %0, %4\n"
  80. " strexeq %2, %0, [%3]"
  81. : "=&r" (slock), "=&r" (contended), "=&r" (res)
  82. : "r" (&lock->slock), "I" (1 << TICKET_SHIFT)
  83. : "cc");
  84. } while (res);
  85. if (!contended) {
  86. smp_mb();
  87. return 1;
  88. } else {
  89. return 0;
  90. }
  91. }
  92. static inline void arch_spin_unlock(arch_spinlock_t *lock)
  93. {
  94. smp_mb();
  95. lock->tickets.owner++;
  96. dsb_sev();
  97. }
  98. static inline int arch_spin_value_unlocked(arch_spinlock_t lock)
  99. {
  100. return lock.tickets.owner == lock.tickets.next;
  101. }
  102. static inline int arch_spin_is_locked(arch_spinlock_t *lock)
  103. {
  104. return !arch_spin_value_unlocked(READ_ONCE(*lock));
  105. }
  106. static inline int arch_spin_is_contended(arch_spinlock_t *lock)
  107. {
  108. struct __raw_tickets tickets = READ_ONCE(lock->tickets);
  109. return (tickets.next - tickets.owner) > 1;
  110. }
  111. #define arch_spin_is_contended arch_spin_is_contended
  112. /*
  113. * RWLOCKS
  114. *
  115. *
  116. * Write locks are easy - we just set bit 31. When unlocking, we can
  117. * just write zero since the lock is exclusively held.
  118. */
  119. static inline void arch_write_lock(arch_rwlock_t *rw)
  120. {
  121. unsigned long tmp;
  122. prefetchw(&rw->lock);
  123. __asm__ __volatile__(
  124. "1: ldrex %0, [%1]\n"
  125. " teq %0, #0\n"
  126. WFE("ne")
  127. " strexeq %0, %2, [%1]\n"
  128. " teq %0, #0\n"
  129. " bne 1b"
  130. : "=&r" (tmp)
  131. : "r" (&rw->lock), "r" (0x80000000)
  132. : "cc");
  133. smp_mb();
  134. }
  135. static inline int arch_write_trylock(arch_rwlock_t *rw)
  136. {
  137. unsigned long contended, res;
  138. prefetchw(&rw->lock);
  139. do {
  140. __asm__ __volatile__(
  141. " ldrex %0, [%2]\n"
  142. " mov %1, #0\n"
  143. " teq %0, #0\n"
  144. " strexeq %1, %3, [%2]"
  145. : "=&r" (contended), "=&r" (res)
  146. : "r" (&rw->lock), "r" (0x80000000)
  147. : "cc");
  148. } while (res);
  149. if (!contended) {
  150. smp_mb();
  151. return 1;
  152. } else {
  153. return 0;
  154. }
  155. }
  156. static inline void arch_write_unlock(arch_rwlock_t *rw)
  157. {
  158. smp_mb();
  159. __asm__ __volatile__(
  160. "str %1, [%0]\n"
  161. :
  162. : "r" (&rw->lock), "r" (0)
  163. : "cc");
  164. dsb_sev();
  165. }
  166. /*
  167. * Read locks are a bit more hairy:
  168. * - Exclusively load the lock value.
  169. * - Increment it.
  170. * - Store new lock value if positive, and we still own this location.
  171. * If the value is negative, we've already failed.
  172. * - If we failed to store the value, we want a negative result.
  173. * - If we failed, try again.
  174. * Unlocking is similarly hairy. We may have multiple read locks
  175. * currently active. However, we know we won't have any write
  176. * locks.
  177. */
  178. static inline void arch_read_lock(arch_rwlock_t *rw)
  179. {
  180. unsigned long tmp, tmp2;
  181. prefetchw(&rw->lock);
  182. __asm__ __volatile__(
  183. " .syntax unified\n"
  184. "1: ldrex %0, [%2]\n"
  185. " adds %0, %0, #1\n"
  186. " strexpl %1, %0, [%2]\n"
  187. WFE("mi")
  188. " rsbspl %0, %1, #0\n"
  189. " bmi 1b"
  190. : "=&r" (tmp), "=&r" (tmp2)
  191. : "r" (&rw->lock)
  192. : "cc");
  193. smp_mb();
  194. }
  195. static inline void arch_read_unlock(arch_rwlock_t *rw)
  196. {
  197. unsigned long tmp, tmp2;
  198. smp_mb();
  199. prefetchw(&rw->lock);
  200. __asm__ __volatile__(
  201. "1: ldrex %0, [%2]\n"
  202. " sub %0, %0, #1\n"
  203. " strex %1, %0, [%2]\n"
  204. " teq %1, #0\n"
  205. " bne 1b"
  206. : "=&r" (tmp), "=&r" (tmp2)
  207. : "r" (&rw->lock)
  208. : "cc");
  209. if (tmp == 0)
  210. dsb_sev();
  211. }
  212. static inline int arch_read_trylock(arch_rwlock_t *rw)
  213. {
  214. unsigned long contended, res;
  215. prefetchw(&rw->lock);
  216. do {
  217. __asm__ __volatile__(
  218. " ldrex %0, [%2]\n"
  219. " mov %1, #0\n"
  220. " adds %0, %0, #1\n"
  221. " strexpl %1, %0, [%2]"
  222. : "=&r" (contended), "=&r" (res)
  223. : "r" (&rw->lock)
  224. : "cc");
  225. } while (res);
  226. /* If the lock is negative, then it is already held for write. */
  227. if (contended < 0x80000000) {
  228. smp_mb();
  229. return 1;
  230. } else {
  231. return 0;
  232. }
  233. }
  234. #endif /* __ASM_SPINLOCK_H */