spinlock.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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_is_locked(arch_spinlock_t *lock)
  109. {
  110. struct __raw_tickets tickets = ACCESS_ONCE(lock->tickets);
  111. return tickets.owner != tickets.next;
  112. }
  113. static inline int arch_spin_is_contended(arch_spinlock_t *lock)
  114. {
  115. struct __raw_tickets tickets = ACCESS_ONCE(lock->tickets);
  116. return (tickets.next - tickets.owner) > 1;
  117. }
  118. #define arch_spin_is_contended arch_spin_is_contended
  119. /*
  120. * RWLOCKS
  121. *
  122. *
  123. * Write locks are easy - we just set bit 31. When unlocking, we can
  124. * just write zero since the lock is exclusively held.
  125. */
  126. static inline void arch_write_lock(arch_rwlock_t *rw)
  127. {
  128. unsigned long tmp;
  129. prefetchw(&rw->lock);
  130. __asm__ __volatile__(
  131. "1: ldrex %0, [%1]\n"
  132. " teq %0, #0\n"
  133. WFE("ne")
  134. " strexeq %0, %2, [%1]\n"
  135. " teq %0, #0\n"
  136. " bne 1b"
  137. : "=&r" (tmp)
  138. : "r" (&rw->lock), "r" (0x80000000)
  139. : "cc");
  140. smp_mb();
  141. }
  142. static inline int arch_write_trylock(arch_rwlock_t *rw)
  143. {
  144. unsigned long contended, res;
  145. prefetchw(&rw->lock);
  146. do {
  147. __asm__ __volatile__(
  148. " ldrex %0, [%2]\n"
  149. " mov %1, #0\n"
  150. " teq %0, #0\n"
  151. " strexeq %1, %3, [%2]"
  152. : "=&r" (contended), "=&r" (res)
  153. : "r" (&rw->lock), "r" (0x80000000)
  154. : "cc");
  155. } while (res);
  156. if (!contended) {
  157. smp_mb();
  158. return 1;
  159. } else {
  160. return 0;
  161. }
  162. }
  163. static inline void arch_write_unlock(arch_rwlock_t *rw)
  164. {
  165. smp_mb();
  166. __asm__ __volatile__(
  167. "str %1, [%0]\n"
  168. :
  169. : "r" (&rw->lock), "r" (0)
  170. : "cc");
  171. dsb_sev();
  172. }
  173. /* write_can_lock - would write_trylock() succeed? */
  174. #define arch_write_can_lock(x) (ACCESS_ONCE((x)->lock) == 0)
  175. /*
  176. * Read locks are a bit more hairy:
  177. * - Exclusively load the lock value.
  178. * - Increment it.
  179. * - Store new lock value if positive, and we still own this location.
  180. * If the value is negative, we've already failed.
  181. * - If we failed to store the value, we want a negative result.
  182. * - If we failed, try again.
  183. * Unlocking is similarly hairy. We may have multiple read locks
  184. * currently active. However, we know we won't have any write
  185. * locks.
  186. */
  187. static inline void arch_read_lock(arch_rwlock_t *rw)
  188. {
  189. unsigned long tmp, tmp2;
  190. prefetchw(&rw->lock);
  191. __asm__ __volatile__(
  192. "1: ldrex %0, [%2]\n"
  193. " adds %0, %0, #1\n"
  194. " strexpl %1, %0, [%2]\n"
  195. WFE("mi")
  196. " rsbpls %0, %1, #0\n"
  197. " bmi 1b"
  198. : "=&r" (tmp), "=&r" (tmp2)
  199. : "r" (&rw->lock)
  200. : "cc");
  201. smp_mb();
  202. }
  203. static inline void arch_read_unlock(arch_rwlock_t *rw)
  204. {
  205. unsigned long tmp, tmp2;
  206. smp_mb();
  207. prefetchw(&rw->lock);
  208. __asm__ __volatile__(
  209. "1: ldrex %0, [%2]\n"
  210. " sub %0, %0, #1\n"
  211. " strex %1, %0, [%2]\n"
  212. " teq %1, #0\n"
  213. " bne 1b"
  214. : "=&r" (tmp), "=&r" (tmp2)
  215. : "r" (&rw->lock)
  216. : "cc");
  217. if (tmp == 0)
  218. dsb_sev();
  219. }
  220. static inline int arch_read_trylock(arch_rwlock_t *rw)
  221. {
  222. unsigned long contended, res;
  223. prefetchw(&rw->lock);
  224. do {
  225. __asm__ __volatile__(
  226. " ldrex %0, [%2]\n"
  227. " mov %1, #0\n"
  228. " adds %0, %0, #1\n"
  229. " strexpl %1, %0, [%2]"
  230. : "=&r" (contended), "=&r" (res)
  231. : "r" (&rw->lock)
  232. : "cc");
  233. } while (res);
  234. /* If the lock is negative, then it is already held for write. */
  235. if (contended < 0x80000000) {
  236. smp_mb();
  237. return 1;
  238. } else {
  239. return 0;
  240. }
  241. }
  242. /* read_can_lock - would read_trylock() succeed? */
  243. #define arch_read_can_lock(x) (ACCESS_ONCE((x)->lock) < 0x80000000)
  244. #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
  245. #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
  246. #define arch_spin_relax(lock) cpu_relax()
  247. #define arch_read_relax(lock) cpu_relax()
  248. #define arch_write_relax(lock) cpu_relax()
  249. #endif /* __ASM_SPINLOCK_H */