spinlock.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright (C) 2012 ARM Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef __ASM_SPINLOCK_H
  17. #define __ASM_SPINLOCK_H
  18. #include <asm/lse.h>
  19. #include <asm/spinlock_types.h>
  20. #include <asm/processor.h>
  21. /*
  22. * Spinlock implementation.
  23. *
  24. * The memory barriers are implicit with the load-acquire and store-release
  25. * instructions.
  26. */
  27. #define arch_spin_unlock_wait(lock) \
  28. do { while (arch_spin_is_locked(lock)) cpu_relax(); } while (0)
  29. #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
  30. static inline void arch_spin_lock(arch_spinlock_t *lock)
  31. {
  32. unsigned int tmp;
  33. arch_spinlock_t lockval, newval;
  34. asm volatile(
  35. /* Atomically increment the next ticket. */
  36. ARM64_LSE_ATOMIC_INSN(
  37. /* LL/SC */
  38. " prfm pstl1strm, %3\n"
  39. "1: ldaxr %w0, %3\n"
  40. " add %w1, %w0, %w5\n"
  41. " stxr %w2, %w1, %3\n"
  42. " cbnz %w2, 1b\n",
  43. /* LSE atomics */
  44. " mov %w2, %w5\n"
  45. " ldadda %w2, %w0, %3\n"
  46. " nop\n"
  47. " nop\n"
  48. " nop\n"
  49. )
  50. /* Did we get the lock? */
  51. " eor %w1, %w0, %w0, ror #16\n"
  52. " cbz %w1, 3f\n"
  53. /*
  54. * No: spin on the owner. Send a local event to avoid missing an
  55. * unlock before the exclusive load.
  56. */
  57. " sevl\n"
  58. "2: wfe\n"
  59. " ldaxrh %w2, %4\n"
  60. " eor %w1, %w2, %w0, lsr #16\n"
  61. " cbnz %w1, 2b\n"
  62. /* We got the lock. Critical section starts here. */
  63. "3:"
  64. : "=&r" (lockval), "=&r" (newval), "=&r" (tmp), "+Q" (*lock)
  65. : "Q" (lock->owner), "I" (1 << TICKET_SHIFT)
  66. : "memory");
  67. }
  68. static inline int arch_spin_trylock(arch_spinlock_t *lock)
  69. {
  70. unsigned int tmp;
  71. arch_spinlock_t lockval;
  72. asm volatile(ARM64_LSE_ATOMIC_INSN(
  73. /* LL/SC */
  74. " prfm pstl1strm, %2\n"
  75. "1: ldaxr %w0, %2\n"
  76. " eor %w1, %w0, %w0, ror #16\n"
  77. " cbnz %w1, 2f\n"
  78. " add %w0, %w0, %3\n"
  79. " stxr %w1, %w0, %2\n"
  80. " cbnz %w1, 1b\n"
  81. "2:",
  82. /* LSE atomics */
  83. " ldr %w0, %2\n"
  84. " eor %w1, %w0, %w0, ror #16\n"
  85. " cbnz %w1, 1f\n"
  86. " add %w1, %w0, %3\n"
  87. " casa %w0, %w1, %2\n"
  88. " sub %w1, %w1, %3\n"
  89. " eor %w1, %w1, %w0\n"
  90. "1:")
  91. : "=&r" (lockval), "=&r" (tmp), "+Q" (*lock)
  92. : "I" (1 << TICKET_SHIFT)
  93. : "memory");
  94. return !tmp;
  95. }
  96. static inline void arch_spin_unlock(arch_spinlock_t *lock)
  97. {
  98. unsigned long tmp;
  99. asm volatile(ARM64_LSE_ATOMIC_INSN(
  100. /* LL/SC */
  101. " ldrh %w1, %0\n"
  102. " add %w1, %w1, #1\n"
  103. " stlrh %w1, %0",
  104. /* LSE atomics */
  105. " mov %w1, #1\n"
  106. " nop\n"
  107. " staddlh %w1, %0")
  108. : "=Q" (lock->owner), "=&r" (tmp)
  109. :
  110. : "memory");
  111. }
  112. static inline int arch_spin_value_unlocked(arch_spinlock_t lock)
  113. {
  114. return lock.owner == lock.next;
  115. }
  116. static inline int arch_spin_is_locked(arch_spinlock_t *lock)
  117. {
  118. return !arch_spin_value_unlocked(READ_ONCE(*lock));
  119. }
  120. static inline int arch_spin_is_contended(arch_spinlock_t *lock)
  121. {
  122. arch_spinlock_t lockval = READ_ONCE(*lock);
  123. return (lockval.next - lockval.owner) > 1;
  124. }
  125. #define arch_spin_is_contended arch_spin_is_contended
  126. /*
  127. * Write lock implementation.
  128. *
  129. * Write locks set bit 31. Unlocking, is done by writing 0 since the lock is
  130. * exclusively held.
  131. *
  132. * The memory barriers are implicit with the load-acquire and store-release
  133. * instructions.
  134. */
  135. static inline void arch_write_lock(arch_rwlock_t *rw)
  136. {
  137. unsigned int tmp;
  138. asm volatile(ARM64_LSE_ATOMIC_INSN(
  139. /* LL/SC */
  140. " sevl\n"
  141. "1: wfe\n"
  142. "2: ldaxr %w0, %1\n"
  143. " cbnz %w0, 1b\n"
  144. " stxr %w0, %w2, %1\n"
  145. " cbnz %w0, 2b\n"
  146. " nop",
  147. /* LSE atomics */
  148. "1: mov %w0, wzr\n"
  149. "2: casa %w0, %w2, %1\n"
  150. " cbz %w0, 3f\n"
  151. " ldxr %w0, %1\n"
  152. " cbz %w0, 2b\n"
  153. " wfe\n"
  154. " b 1b\n"
  155. "3:")
  156. : "=&r" (tmp), "+Q" (rw->lock)
  157. : "r" (0x80000000)
  158. : "memory");
  159. }
  160. static inline int arch_write_trylock(arch_rwlock_t *rw)
  161. {
  162. unsigned int tmp;
  163. asm volatile(ARM64_LSE_ATOMIC_INSN(
  164. /* LL/SC */
  165. "1: ldaxr %w0, %1\n"
  166. " cbnz %w0, 2f\n"
  167. " stxr %w0, %w2, %1\n"
  168. " cbnz %w0, 1b\n"
  169. "2:",
  170. /* LSE atomics */
  171. " mov %w0, wzr\n"
  172. " casa %w0, %w2, %1\n"
  173. " nop\n"
  174. " nop")
  175. : "=&r" (tmp), "+Q" (rw->lock)
  176. : "r" (0x80000000)
  177. : "memory");
  178. return !tmp;
  179. }
  180. static inline void arch_write_unlock(arch_rwlock_t *rw)
  181. {
  182. asm volatile(ARM64_LSE_ATOMIC_INSN(
  183. " stlr wzr, %0",
  184. " swpl wzr, wzr, %0")
  185. : "=Q" (rw->lock) :: "memory");
  186. }
  187. /* write_can_lock - would write_trylock() succeed? */
  188. #define arch_write_can_lock(x) ((x)->lock == 0)
  189. /*
  190. * Read lock implementation.
  191. *
  192. * It exclusively loads the lock value, increments it and stores the new value
  193. * back if positive and the CPU still exclusively owns the location. If the
  194. * value is negative, the lock is already held.
  195. *
  196. * During unlocking there may be multiple active read locks but no write lock.
  197. *
  198. * The memory barriers are implicit with the load-acquire and store-release
  199. * instructions.
  200. *
  201. * Note that in UNDEFINED cases, such as unlocking a lock twice, the LL/SC
  202. * and LSE implementations may exhibit different behaviour (although this
  203. * will have no effect on lockdep).
  204. */
  205. static inline void arch_read_lock(arch_rwlock_t *rw)
  206. {
  207. unsigned int tmp, tmp2;
  208. asm volatile(
  209. " sevl\n"
  210. ARM64_LSE_ATOMIC_INSN(
  211. /* LL/SC */
  212. "1: wfe\n"
  213. "2: ldaxr %w0, %2\n"
  214. " add %w0, %w0, #1\n"
  215. " tbnz %w0, #31, 1b\n"
  216. " stxr %w1, %w0, %2\n"
  217. " nop\n"
  218. " cbnz %w1, 2b",
  219. /* LSE atomics */
  220. "1: wfe\n"
  221. "2: ldxr %w0, %2\n"
  222. " adds %w1, %w0, #1\n"
  223. " tbnz %w1, #31, 1b\n"
  224. " casa %w0, %w1, %2\n"
  225. " sbc %w0, %w1, %w0\n"
  226. " cbnz %w0, 2b")
  227. : "=&r" (tmp), "=&r" (tmp2), "+Q" (rw->lock)
  228. :
  229. : "cc", "memory");
  230. }
  231. static inline void arch_read_unlock(arch_rwlock_t *rw)
  232. {
  233. unsigned int tmp, tmp2;
  234. asm volatile(ARM64_LSE_ATOMIC_INSN(
  235. /* LL/SC */
  236. "1: ldxr %w0, %2\n"
  237. " sub %w0, %w0, #1\n"
  238. " stlxr %w1, %w0, %2\n"
  239. " cbnz %w1, 1b",
  240. /* LSE atomics */
  241. " movn %w0, #0\n"
  242. " nop\n"
  243. " nop\n"
  244. " staddl %w0, %2")
  245. : "=&r" (tmp), "=&r" (tmp2), "+Q" (rw->lock)
  246. :
  247. : "memory");
  248. }
  249. static inline int arch_read_trylock(arch_rwlock_t *rw)
  250. {
  251. unsigned int tmp, tmp2;
  252. asm volatile(ARM64_LSE_ATOMIC_INSN(
  253. /* LL/SC */
  254. " mov %w1, #1\n"
  255. "1: ldaxr %w0, %2\n"
  256. " add %w0, %w0, #1\n"
  257. " tbnz %w0, #31, 2f\n"
  258. " stxr %w1, %w0, %2\n"
  259. " cbnz %w1, 1b\n"
  260. "2:",
  261. /* LSE atomics */
  262. " ldr %w0, %2\n"
  263. " adds %w1, %w0, #1\n"
  264. " tbnz %w1, #31, 1f\n"
  265. " casa %w0, %w1, %2\n"
  266. " sbc %w1, %w1, %w0\n"
  267. " nop\n"
  268. "1:")
  269. : "=&r" (tmp), "=&r" (tmp2), "+Q" (rw->lock)
  270. :
  271. : "cc", "memory");
  272. return !tmp2;
  273. }
  274. /* read_can_lock - would read_trylock() succeed? */
  275. #define arch_read_can_lock(x) ((x)->lock < 0x80000000)
  276. #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
  277. #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
  278. #define arch_spin_relax(lock) cpu_relax()
  279. #define arch_read_relax(lock) cpu_relax()
  280. #define arch_write_relax(lock) cpu_relax()
  281. /*
  282. * Accesses appearing in program order before a spin_lock() operation
  283. * can be reordered with accesses inside the critical section, by virtue
  284. * of arch_spin_lock being constructed using acquire semantics.
  285. *
  286. * In cases where this is problematic (e.g. try_to_wake_up), an
  287. * smp_mb__before_spinlock() can restore the required ordering.
  288. */
  289. #define smp_mb__before_spinlock() smp_mb()
  290. #endif /* __ASM_SPINLOCK_H */