spinlock.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
  28. {
  29. unsigned int tmp;
  30. arch_spinlock_t lockval;
  31. u32 owner;
  32. /*
  33. * Ensure prior spin_lock operations to other locks have completed
  34. * on this CPU before we test whether "lock" is locked.
  35. */
  36. smp_mb();
  37. owner = READ_ONCE(lock->owner) << 16;
  38. asm volatile(
  39. " sevl\n"
  40. "1: wfe\n"
  41. "2: ldaxr %w0, %2\n"
  42. /* Is the lock free? */
  43. " eor %w1, %w0, %w0, ror #16\n"
  44. " cbz %w1, 3f\n"
  45. /* Lock taken -- has there been a subsequent unlock->lock transition? */
  46. " eor %w1, %w3, %w0, lsl #16\n"
  47. " cbz %w1, 1b\n"
  48. /*
  49. * The owner has been updated, so there was an unlock->lock
  50. * transition that we missed. That means we can rely on the
  51. * store-release of the unlock operation paired with the
  52. * load-acquire of the lock operation to publish any of our
  53. * previous stores to the new lock owner and therefore don't
  54. * need to bother with the writeback below.
  55. */
  56. " b 4f\n"
  57. "3:\n"
  58. /*
  59. * Serialise against any concurrent lockers by writing back the
  60. * unlocked lock value
  61. */
  62. ARM64_LSE_ATOMIC_INSN(
  63. /* LL/SC */
  64. " stxr %w1, %w0, %2\n"
  65. __nops(2),
  66. /* LSE atomics */
  67. " mov %w1, %w0\n"
  68. " cas %w0, %w0, %2\n"
  69. " eor %w1, %w1, %w0\n")
  70. /* Somebody else wrote to the lock, GOTO 10 and reload the value */
  71. " cbnz %w1, 2b\n"
  72. "4:"
  73. : "=&r" (lockval), "=&r" (tmp), "+Q" (*lock)
  74. : "r" (owner)
  75. : "memory");
  76. }
  77. #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
  78. static inline void arch_spin_lock(arch_spinlock_t *lock)
  79. {
  80. unsigned int tmp;
  81. arch_spinlock_t lockval, newval;
  82. asm volatile(
  83. /* Atomically increment the next ticket. */
  84. ARM64_LSE_ATOMIC_INSN(
  85. /* LL/SC */
  86. " prfm pstl1strm, %3\n"
  87. "1: ldaxr %w0, %3\n"
  88. " add %w1, %w0, %w5\n"
  89. " stxr %w2, %w1, %3\n"
  90. " cbnz %w2, 1b\n",
  91. /* LSE atomics */
  92. " mov %w2, %w5\n"
  93. " ldadda %w2, %w0, %3\n"
  94. __nops(3)
  95. )
  96. /* Did we get the lock? */
  97. " eor %w1, %w0, %w0, ror #16\n"
  98. " cbz %w1, 3f\n"
  99. /*
  100. * No: spin on the owner. Send a local event to avoid missing an
  101. * unlock before the exclusive load.
  102. */
  103. " sevl\n"
  104. "2: wfe\n"
  105. " ldaxrh %w2, %4\n"
  106. " eor %w1, %w2, %w0, lsr #16\n"
  107. " cbnz %w1, 2b\n"
  108. /* We got the lock. Critical section starts here. */
  109. "3:"
  110. : "=&r" (lockval), "=&r" (newval), "=&r" (tmp), "+Q" (*lock)
  111. : "Q" (lock->owner), "I" (1 << TICKET_SHIFT)
  112. : "memory");
  113. }
  114. static inline int arch_spin_trylock(arch_spinlock_t *lock)
  115. {
  116. unsigned int tmp;
  117. arch_spinlock_t lockval;
  118. asm volatile(ARM64_LSE_ATOMIC_INSN(
  119. /* LL/SC */
  120. " prfm pstl1strm, %2\n"
  121. "1: ldaxr %w0, %2\n"
  122. " eor %w1, %w0, %w0, ror #16\n"
  123. " cbnz %w1, 2f\n"
  124. " add %w0, %w0, %3\n"
  125. " stxr %w1, %w0, %2\n"
  126. " cbnz %w1, 1b\n"
  127. "2:",
  128. /* LSE atomics */
  129. " ldr %w0, %2\n"
  130. " eor %w1, %w0, %w0, ror #16\n"
  131. " cbnz %w1, 1f\n"
  132. " add %w1, %w0, %3\n"
  133. " casa %w0, %w1, %2\n"
  134. " and %w1, %w1, #0xffff\n"
  135. " eor %w1, %w1, %w0, lsr #16\n"
  136. "1:")
  137. : "=&r" (lockval), "=&r" (tmp), "+Q" (*lock)
  138. : "I" (1 << TICKET_SHIFT)
  139. : "memory");
  140. return !tmp;
  141. }
  142. static inline void arch_spin_unlock(arch_spinlock_t *lock)
  143. {
  144. unsigned long tmp;
  145. asm volatile(ARM64_LSE_ATOMIC_INSN(
  146. /* LL/SC */
  147. " ldrh %w1, %0\n"
  148. " add %w1, %w1, #1\n"
  149. " stlrh %w1, %0",
  150. /* LSE atomics */
  151. " mov %w1, #1\n"
  152. " staddlh %w1, %0\n"
  153. __nops(1))
  154. : "=Q" (lock->owner), "=&r" (tmp)
  155. :
  156. : "memory");
  157. }
  158. static inline int arch_spin_value_unlocked(arch_spinlock_t lock)
  159. {
  160. return lock.owner == lock.next;
  161. }
  162. static inline int arch_spin_is_locked(arch_spinlock_t *lock)
  163. {
  164. smp_mb(); /* See arch_spin_unlock_wait */
  165. return !arch_spin_value_unlocked(READ_ONCE(*lock));
  166. }
  167. static inline int arch_spin_is_contended(arch_spinlock_t *lock)
  168. {
  169. arch_spinlock_t lockval = READ_ONCE(*lock);
  170. return (lockval.next - lockval.owner) > 1;
  171. }
  172. #define arch_spin_is_contended arch_spin_is_contended
  173. /*
  174. * Write lock implementation.
  175. *
  176. * Write locks set bit 31. Unlocking, is done by writing 0 since the lock is
  177. * exclusively held.
  178. *
  179. * The memory barriers are implicit with the load-acquire and store-release
  180. * instructions.
  181. */
  182. static inline void arch_write_lock(arch_rwlock_t *rw)
  183. {
  184. unsigned int tmp;
  185. asm volatile(ARM64_LSE_ATOMIC_INSN(
  186. /* LL/SC */
  187. " sevl\n"
  188. "1: wfe\n"
  189. "2: ldaxr %w0, %1\n"
  190. " cbnz %w0, 1b\n"
  191. " stxr %w0, %w2, %1\n"
  192. " cbnz %w0, 2b\n"
  193. __nops(1),
  194. /* LSE atomics */
  195. "1: mov %w0, wzr\n"
  196. "2: casa %w0, %w2, %1\n"
  197. " cbz %w0, 3f\n"
  198. " ldxr %w0, %1\n"
  199. " cbz %w0, 2b\n"
  200. " wfe\n"
  201. " b 1b\n"
  202. "3:")
  203. : "=&r" (tmp), "+Q" (rw->lock)
  204. : "r" (0x80000000)
  205. : "memory");
  206. }
  207. static inline int arch_write_trylock(arch_rwlock_t *rw)
  208. {
  209. unsigned int tmp;
  210. asm volatile(ARM64_LSE_ATOMIC_INSN(
  211. /* LL/SC */
  212. "1: ldaxr %w0, %1\n"
  213. " cbnz %w0, 2f\n"
  214. " stxr %w0, %w2, %1\n"
  215. " cbnz %w0, 1b\n"
  216. "2:",
  217. /* LSE atomics */
  218. " mov %w0, wzr\n"
  219. " casa %w0, %w2, %1\n"
  220. __nops(2))
  221. : "=&r" (tmp), "+Q" (rw->lock)
  222. : "r" (0x80000000)
  223. : "memory");
  224. return !tmp;
  225. }
  226. static inline void arch_write_unlock(arch_rwlock_t *rw)
  227. {
  228. asm volatile(ARM64_LSE_ATOMIC_INSN(
  229. " stlr wzr, %0",
  230. " swpl wzr, wzr, %0")
  231. : "=Q" (rw->lock) :: "memory");
  232. }
  233. /* write_can_lock - would write_trylock() succeed? */
  234. #define arch_write_can_lock(x) ((x)->lock == 0)
  235. /*
  236. * Read lock implementation.
  237. *
  238. * It exclusively loads the lock value, increments it and stores the new value
  239. * back if positive and the CPU still exclusively owns the location. If the
  240. * value is negative, the lock is already held.
  241. *
  242. * During unlocking there may be multiple active read locks but no write lock.
  243. *
  244. * The memory barriers are implicit with the load-acquire and store-release
  245. * instructions.
  246. *
  247. * Note that in UNDEFINED cases, such as unlocking a lock twice, the LL/SC
  248. * and LSE implementations may exhibit different behaviour (although this
  249. * will have no effect on lockdep).
  250. */
  251. static inline void arch_read_lock(arch_rwlock_t *rw)
  252. {
  253. unsigned int tmp, tmp2;
  254. asm volatile(
  255. " sevl\n"
  256. ARM64_LSE_ATOMIC_INSN(
  257. /* LL/SC */
  258. "1: wfe\n"
  259. "2: ldaxr %w0, %2\n"
  260. " add %w0, %w0, #1\n"
  261. " tbnz %w0, #31, 1b\n"
  262. " stxr %w1, %w0, %2\n"
  263. " cbnz %w1, 2b\n"
  264. __nops(1),
  265. /* LSE atomics */
  266. "1: wfe\n"
  267. "2: ldxr %w0, %2\n"
  268. " adds %w1, %w0, #1\n"
  269. " tbnz %w1, #31, 1b\n"
  270. " casa %w0, %w1, %2\n"
  271. " sbc %w0, %w1, %w0\n"
  272. " cbnz %w0, 2b")
  273. : "=&r" (tmp), "=&r" (tmp2), "+Q" (rw->lock)
  274. :
  275. : "cc", "memory");
  276. }
  277. static inline void arch_read_unlock(arch_rwlock_t *rw)
  278. {
  279. unsigned int tmp, tmp2;
  280. asm volatile(ARM64_LSE_ATOMIC_INSN(
  281. /* LL/SC */
  282. "1: ldxr %w0, %2\n"
  283. " sub %w0, %w0, #1\n"
  284. " stlxr %w1, %w0, %2\n"
  285. " cbnz %w1, 1b",
  286. /* LSE atomics */
  287. " movn %w0, #0\n"
  288. " staddl %w0, %2\n"
  289. __nops(2))
  290. : "=&r" (tmp), "=&r" (tmp2), "+Q" (rw->lock)
  291. :
  292. : "memory");
  293. }
  294. static inline int arch_read_trylock(arch_rwlock_t *rw)
  295. {
  296. unsigned int tmp, tmp2;
  297. asm volatile(ARM64_LSE_ATOMIC_INSN(
  298. /* LL/SC */
  299. " mov %w1, #1\n"
  300. "1: ldaxr %w0, %2\n"
  301. " add %w0, %w0, #1\n"
  302. " tbnz %w0, #31, 2f\n"
  303. " stxr %w1, %w0, %2\n"
  304. " cbnz %w1, 1b\n"
  305. "2:",
  306. /* LSE atomics */
  307. " ldr %w0, %2\n"
  308. " adds %w1, %w0, #1\n"
  309. " tbnz %w1, #31, 1f\n"
  310. " casa %w0, %w1, %2\n"
  311. " sbc %w1, %w1, %w0\n"
  312. __nops(1)
  313. "1:")
  314. : "=&r" (tmp), "=&r" (tmp2), "+Q" (rw->lock)
  315. :
  316. : "cc", "memory");
  317. return !tmp2;
  318. }
  319. /* read_can_lock - would read_trylock() succeed? */
  320. #define arch_read_can_lock(x) ((x)->lock < 0x80000000)
  321. #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
  322. #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
  323. #define arch_spin_relax(lock) cpu_relax()
  324. #define arch_read_relax(lock) cpu_relax()
  325. #define arch_write_relax(lock) cpu_relax()
  326. /*
  327. * Accesses appearing in program order before a spin_lock() operation
  328. * can be reordered with accesses inside the critical section, by virtue
  329. * of arch_spin_lock being constructed using acquire semantics.
  330. *
  331. * In cases where this is problematic (e.g. try_to_wake_up), an
  332. * smp_mb__before_spinlock() can restore the required ordering.
  333. */
  334. #define smp_mb__before_spinlock() smp_mb()
  335. #endif /* __ASM_SPINLOCK_H */