spinlock.h 6.3 KB

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