spinlock.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. /*
  8. * sev and wfe are ARMv6K extensions. Uniprocessor ARMv6 may not have the K
  9. * extensions, so when running on UP, we have to patch these instructions away.
  10. */
  11. #ifdef CONFIG_THUMB2_KERNEL
  12. /*
  13. * For Thumb-2, special care is needed to ensure that the conditional WFE
  14. * instruction really does assemble to exactly 4 bytes (as required by
  15. * the SMP_ON_UP fixup code). By itself "wfene" might cause the
  16. * assembler to insert a extra (16-bit) IT instruction, depending on the
  17. * presence or absence of neighbouring conditional instructions.
  18. *
  19. * To avoid this unpredictableness, an approprite IT is inserted explicitly:
  20. * the assembler won't change IT instructions which are explicitly present
  21. * in the input.
  22. */
  23. #define WFE(cond) __ALT_SMP_ASM( \
  24. "it " cond "\n\t" \
  25. "wfe" cond ".n", \
  26. \
  27. "nop.w" \
  28. )
  29. #else
  30. #define WFE(cond) __ALT_SMP_ASM("wfe" cond, "nop")
  31. #endif
  32. #define SEV __ALT_SMP_ASM(WASM(sev), WASM(nop))
  33. static inline void dsb_sev(void)
  34. {
  35. dsb(ishst);
  36. __asm__(SEV);
  37. }
  38. /*
  39. * ARMv6 ticket-based spin-locking.
  40. *
  41. * A memory barrier is required after we get a lock, and before we
  42. * release it, because V6 CPUs are assumed to have weakly ordered
  43. * memory.
  44. */
  45. #define arch_spin_unlock_wait(lock) \
  46. do { while (arch_spin_is_locked(lock)) cpu_relax(); } while (0)
  47. #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
  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 = ACCESS_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. /* write_can_lock - would write_trylock() succeed? */
  167. #define arch_write_can_lock(x) (ACCESS_ONCE((x)->lock) == 0)
  168. /*
  169. * Read locks are a bit more hairy:
  170. * - Exclusively load the lock value.
  171. * - Increment it.
  172. * - Store new lock value if positive, and we still own this location.
  173. * If the value is negative, we've already failed.
  174. * - If we failed to store the value, we want a negative result.
  175. * - If we failed, try again.
  176. * Unlocking is similarly hairy. We may have multiple read locks
  177. * currently active. However, we know we won't have any write
  178. * locks.
  179. */
  180. static inline void arch_read_lock(arch_rwlock_t *rw)
  181. {
  182. unsigned long tmp, tmp2;
  183. prefetchw(&rw->lock);
  184. __asm__ __volatile__(
  185. "1: ldrex %0, [%2]\n"
  186. " adds %0, %0, #1\n"
  187. " strexpl %1, %0, [%2]\n"
  188. WFE("mi")
  189. " rsbpls %0, %1, #0\n"
  190. " bmi 1b"
  191. : "=&r" (tmp), "=&r" (tmp2)
  192. : "r" (&rw->lock)
  193. : "cc");
  194. smp_mb();
  195. }
  196. static inline void arch_read_unlock(arch_rwlock_t *rw)
  197. {
  198. unsigned long tmp, tmp2;
  199. smp_mb();
  200. prefetchw(&rw->lock);
  201. __asm__ __volatile__(
  202. "1: ldrex %0, [%2]\n"
  203. " sub %0, %0, #1\n"
  204. " strex %1, %0, [%2]\n"
  205. " teq %1, #0\n"
  206. " bne 1b"
  207. : "=&r" (tmp), "=&r" (tmp2)
  208. : "r" (&rw->lock)
  209. : "cc");
  210. if (tmp == 0)
  211. dsb_sev();
  212. }
  213. static inline int arch_read_trylock(arch_rwlock_t *rw)
  214. {
  215. unsigned long contended, res;
  216. prefetchw(&rw->lock);
  217. do {
  218. __asm__ __volatile__(
  219. " ldrex %0, [%2]\n"
  220. " mov %1, #0\n"
  221. " adds %0, %0, #1\n"
  222. " strexpl %1, %0, [%2]"
  223. : "=&r" (contended), "=&r" (res)
  224. : "r" (&rw->lock)
  225. : "cc");
  226. } while (res);
  227. /* If the lock is negative, then it is already held for write. */
  228. if (contended < 0x80000000) {
  229. smp_mb();
  230. return 1;
  231. } else {
  232. return 0;
  233. }
  234. }
  235. /* read_can_lock - would read_trylock() succeed? */
  236. #define arch_read_can_lock(x) (ACCESS_ONCE((x)->lock) < 0x80000000)
  237. #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
  238. #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
  239. #define arch_spin_relax(lock) cpu_relax()
  240. #define arch_read_relax(lock) cpu_relax()
  241. #define arch_write_relax(lock) cpu_relax()
  242. #endif /* __ASM_SPINLOCK_H */