spinlock.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. #if __LINUX_ARM_ARCH__ >= 7
  36. __asm__ __volatile__ (
  37. "dsb ishst\n"
  38. SEV
  39. );
  40. #else
  41. __asm__ __volatile__ (
  42. "mcr p15, 0, %0, c7, c10, 4\n"
  43. SEV
  44. : : "r" (0)
  45. );
  46. #endif
  47. }
  48. /*
  49. * ARMv6 ticket-based spin-locking.
  50. *
  51. * A memory barrier is required after we get a lock, and before we
  52. * release it, because V6 CPUs are assumed to have weakly ordered
  53. * memory.
  54. */
  55. #define arch_spin_unlock_wait(lock) \
  56. do { while (arch_spin_is_locked(lock)) cpu_relax(); } while (0)
  57. #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
  58. static inline void arch_spin_lock(arch_spinlock_t *lock)
  59. {
  60. unsigned long tmp;
  61. u32 newval;
  62. arch_spinlock_t lockval;
  63. prefetchw(&lock->slock);
  64. __asm__ __volatile__(
  65. "1: ldrex %0, [%3]\n"
  66. " add %1, %0, %4\n"
  67. " strex %2, %1, [%3]\n"
  68. " teq %2, #0\n"
  69. " bne 1b"
  70. : "=&r" (lockval), "=&r" (newval), "=&r" (tmp)
  71. : "r" (&lock->slock), "I" (1 << TICKET_SHIFT)
  72. : "cc");
  73. while (lockval.tickets.next != lockval.tickets.owner) {
  74. wfe();
  75. lockval.tickets.owner = ACCESS_ONCE(lock->tickets.owner);
  76. }
  77. smp_mb();
  78. }
  79. static inline int arch_spin_trylock(arch_spinlock_t *lock)
  80. {
  81. unsigned long contended, res;
  82. u32 slock;
  83. prefetchw(&lock->slock);
  84. do {
  85. __asm__ __volatile__(
  86. " ldrex %0, [%3]\n"
  87. " mov %2, #0\n"
  88. " subs %1, %0, %0, ror #16\n"
  89. " addeq %0, %0, %4\n"
  90. " strexeq %2, %0, [%3]"
  91. : "=&r" (slock), "=&r" (contended), "=&r" (res)
  92. : "r" (&lock->slock), "I" (1 << TICKET_SHIFT)
  93. : "cc");
  94. } while (res);
  95. if (!contended) {
  96. smp_mb();
  97. return 1;
  98. } else {
  99. return 0;
  100. }
  101. }
  102. static inline void arch_spin_unlock(arch_spinlock_t *lock)
  103. {
  104. smp_mb();
  105. lock->tickets.owner++;
  106. dsb_sev();
  107. }
  108. static inline int arch_spin_value_unlocked(arch_spinlock_t lock)
  109. {
  110. return lock.tickets.owner == lock.tickets.next;
  111. }
  112. static inline int arch_spin_is_locked(arch_spinlock_t *lock)
  113. {
  114. return !arch_spin_value_unlocked(ACCESS_ONCE(*lock));
  115. }
  116. static inline int arch_spin_is_contended(arch_spinlock_t *lock)
  117. {
  118. struct __raw_tickets tickets = ACCESS_ONCE(lock->tickets);
  119. return (tickets.next - tickets.owner) > 1;
  120. }
  121. #define arch_spin_is_contended arch_spin_is_contended
  122. /*
  123. * RWLOCKS
  124. *
  125. *
  126. * Write locks are easy - we just set bit 31. When unlocking, we can
  127. * just write zero since the lock is exclusively held.
  128. */
  129. static inline void arch_write_lock(arch_rwlock_t *rw)
  130. {
  131. unsigned long tmp;
  132. prefetchw(&rw->lock);
  133. __asm__ __volatile__(
  134. "1: ldrex %0, [%1]\n"
  135. " teq %0, #0\n"
  136. WFE("ne")
  137. " strexeq %0, %2, [%1]\n"
  138. " teq %0, #0\n"
  139. " bne 1b"
  140. : "=&r" (tmp)
  141. : "r" (&rw->lock), "r" (0x80000000)
  142. : "cc");
  143. smp_mb();
  144. }
  145. static inline int arch_write_trylock(arch_rwlock_t *rw)
  146. {
  147. unsigned long contended, res;
  148. prefetchw(&rw->lock);
  149. do {
  150. __asm__ __volatile__(
  151. " ldrex %0, [%2]\n"
  152. " mov %1, #0\n"
  153. " teq %0, #0\n"
  154. " strexeq %1, %3, [%2]"
  155. : "=&r" (contended), "=&r" (res)
  156. : "r" (&rw->lock), "r" (0x80000000)
  157. : "cc");
  158. } while (res);
  159. if (!contended) {
  160. smp_mb();
  161. return 1;
  162. } else {
  163. return 0;
  164. }
  165. }
  166. static inline void arch_write_unlock(arch_rwlock_t *rw)
  167. {
  168. smp_mb();
  169. __asm__ __volatile__(
  170. "str %1, [%0]\n"
  171. :
  172. : "r" (&rw->lock), "r" (0)
  173. : "cc");
  174. dsb_sev();
  175. }
  176. /* write_can_lock - would write_trylock() succeed? */
  177. #define arch_write_can_lock(x) (ACCESS_ONCE((x)->lock) == 0)
  178. /*
  179. * Read locks are a bit more hairy:
  180. * - Exclusively load the lock value.
  181. * - Increment it.
  182. * - Store new lock value if positive, and we still own this location.
  183. * If the value is negative, we've already failed.
  184. * - If we failed to store the value, we want a negative result.
  185. * - If we failed, try again.
  186. * Unlocking is similarly hairy. We may have multiple read locks
  187. * currently active. However, we know we won't have any write
  188. * locks.
  189. */
  190. static inline void arch_read_lock(arch_rwlock_t *rw)
  191. {
  192. unsigned long tmp, tmp2;
  193. prefetchw(&rw->lock);
  194. __asm__ __volatile__(
  195. "1: ldrex %0, [%2]\n"
  196. " adds %0, %0, #1\n"
  197. " strexpl %1, %0, [%2]\n"
  198. WFE("mi")
  199. " rsbpls %0, %1, #0\n"
  200. " bmi 1b"
  201. : "=&r" (tmp), "=&r" (tmp2)
  202. : "r" (&rw->lock)
  203. : "cc");
  204. smp_mb();
  205. }
  206. static inline void arch_read_unlock(arch_rwlock_t *rw)
  207. {
  208. unsigned long tmp, tmp2;
  209. smp_mb();
  210. prefetchw(&rw->lock);
  211. __asm__ __volatile__(
  212. "1: ldrex %0, [%2]\n"
  213. " sub %0, %0, #1\n"
  214. " strex %1, %0, [%2]\n"
  215. " teq %1, #0\n"
  216. " bne 1b"
  217. : "=&r" (tmp), "=&r" (tmp2)
  218. : "r" (&rw->lock)
  219. : "cc");
  220. if (tmp == 0)
  221. dsb_sev();
  222. }
  223. static inline int arch_read_trylock(arch_rwlock_t *rw)
  224. {
  225. unsigned long contended, res;
  226. prefetchw(&rw->lock);
  227. do {
  228. __asm__ __volatile__(
  229. " ldrex %0, [%2]\n"
  230. " mov %1, #0\n"
  231. " adds %0, %0, #1\n"
  232. " strexpl %1, %0, [%2]"
  233. : "=&r" (contended), "=&r" (res)
  234. : "r" (&rw->lock)
  235. : "cc");
  236. } while (res);
  237. /* If the lock is negative, then it is already held for write. */
  238. if (contended < 0x80000000) {
  239. smp_mb();
  240. return 1;
  241. } else {
  242. return 0;
  243. }
  244. }
  245. /* read_can_lock - would read_trylock() succeed? */
  246. #define arch_read_can_lock(x) (ACCESS_ONCE((x)->lock) < 0x80000000)
  247. #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
  248. #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
  249. #define arch_spin_relax(lock) cpu_relax()
  250. #define arch_read_relax(lock) cpu_relax()
  251. #define arch_write_relax(lock) cpu_relax()
  252. #endif /* __ASM_SPINLOCK_H */