spinlock.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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/spinlock_types.h>
  19. #include <asm/processor.h>
  20. /*
  21. * Spinlock implementation.
  22. *
  23. * The memory barriers are implicit with the load-acquire and store-release
  24. * instructions.
  25. */
  26. #define arch_spin_unlock_wait(lock) \
  27. do { while (arch_spin_is_locked(lock)) cpu_relax(); } while (0)
  28. #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
  29. static inline void arch_spin_lock(arch_spinlock_t *lock)
  30. {
  31. unsigned int tmp;
  32. arch_spinlock_t lockval, newval;
  33. asm volatile(
  34. /* Atomically increment the next ticket. */
  35. " prfm pstl1strm, %3\n"
  36. "1: ldaxr %w0, %3\n"
  37. " add %w1, %w0, %w5\n"
  38. " stxr %w2, %w1, %3\n"
  39. " cbnz %w2, 1b\n"
  40. /* Did we get the lock? */
  41. " eor %w1, %w0, %w0, ror #16\n"
  42. " cbz %w1, 3f\n"
  43. /*
  44. * No: spin on the owner. Send a local event to avoid missing an
  45. * unlock before the exclusive load.
  46. */
  47. " sevl\n"
  48. "2: wfe\n"
  49. " ldaxrh %w2, %4\n"
  50. " eor %w1, %w2, %w0, lsr #16\n"
  51. " cbnz %w1, 2b\n"
  52. /* We got the lock. Critical section starts here. */
  53. "3:"
  54. : "=&r" (lockval), "=&r" (newval), "=&r" (tmp), "+Q" (*lock)
  55. : "Q" (lock->owner), "I" (1 << TICKET_SHIFT)
  56. : "memory");
  57. }
  58. static inline int arch_spin_trylock(arch_spinlock_t *lock)
  59. {
  60. unsigned int tmp;
  61. arch_spinlock_t lockval;
  62. asm volatile(
  63. " prfm pstl1strm, %2\n"
  64. "1: ldaxr %w0, %2\n"
  65. " eor %w1, %w0, %w0, ror #16\n"
  66. " cbnz %w1, 2f\n"
  67. " add %w0, %w0, %3\n"
  68. " stxr %w1, %w0, %2\n"
  69. " cbnz %w1, 1b\n"
  70. "2:"
  71. : "=&r" (lockval), "=&r" (tmp), "+Q" (*lock)
  72. : "I" (1 << TICKET_SHIFT)
  73. : "memory");
  74. return !tmp;
  75. }
  76. static inline void arch_spin_unlock(arch_spinlock_t *lock)
  77. {
  78. asm volatile(
  79. " stlrh %w1, %0\n"
  80. : "=Q" (lock->owner)
  81. : "r" (lock->owner + 1)
  82. : "memory");
  83. }
  84. static inline int arch_spin_is_locked(arch_spinlock_t *lock)
  85. {
  86. arch_spinlock_t lockval = ACCESS_ONCE(*lock);
  87. return lockval.owner != lockval.next;
  88. }
  89. static inline int arch_spin_is_contended(arch_spinlock_t *lock)
  90. {
  91. arch_spinlock_t lockval = ACCESS_ONCE(*lock);
  92. return (lockval.next - lockval.owner) > 1;
  93. }
  94. #define arch_spin_is_contended arch_spin_is_contended
  95. /*
  96. * Write lock implementation.
  97. *
  98. * Write locks set bit 31. Unlocking, is done by writing 0 since the lock is
  99. * exclusively held.
  100. *
  101. * The memory barriers are implicit with the load-acquire and store-release
  102. * instructions.
  103. */
  104. static inline void arch_write_lock(arch_rwlock_t *rw)
  105. {
  106. unsigned int tmp;
  107. asm volatile(
  108. " sevl\n"
  109. "1: wfe\n"
  110. "2: ldaxr %w0, %1\n"
  111. " cbnz %w0, 1b\n"
  112. " stxr %w0, %w2, %1\n"
  113. " cbnz %w0, 2b\n"
  114. : "=&r" (tmp), "+Q" (rw->lock)
  115. : "r" (0x80000000)
  116. : "cc", "memory");
  117. }
  118. static inline int arch_write_trylock(arch_rwlock_t *rw)
  119. {
  120. unsigned int tmp;
  121. asm volatile(
  122. " ldaxr %w0, %1\n"
  123. " cbnz %w0, 1f\n"
  124. " stxr %w0, %w2, %1\n"
  125. "1:\n"
  126. : "=&r" (tmp), "+Q" (rw->lock)
  127. : "r" (0x80000000)
  128. : "cc", "memory");
  129. return !tmp;
  130. }
  131. static inline void arch_write_unlock(arch_rwlock_t *rw)
  132. {
  133. asm volatile(
  134. " stlr %w1, %0\n"
  135. : "=Q" (rw->lock) : "r" (0) : "memory");
  136. }
  137. /* write_can_lock - would write_trylock() succeed? */
  138. #define arch_write_can_lock(x) ((x)->lock == 0)
  139. /*
  140. * Read lock implementation.
  141. *
  142. * It exclusively loads the lock value, increments it and stores the new value
  143. * back if positive and the CPU still exclusively owns the location. If the
  144. * value is negative, the lock is already held.
  145. *
  146. * During unlocking there may be multiple active read locks but no write lock.
  147. *
  148. * The memory barriers are implicit with the load-acquire and store-release
  149. * instructions.
  150. */
  151. static inline void arch_read_lock(arch_rwlock_t *rw)
  152. {
  153. unsigned int tmp, tmp2;
  154. asm volatile(
  155. " sevl\n"
  156. "1: wfe\n"
  157. "2: ldaxr %w0, %2\n"
  158. " add %w0, %w0, #1\n"
  159. " tbnz %w0, #31, 1b\n"
  160. " stxr %w1, %w0, %2\n"
  161. " cbnz %w1, 2b\n"
  162. : "=&r" (tmp), "=&r" (tmp2), "+Q" (rw->lock)
  163. :
  164. : "cc", "memory");
  165. }
  166. static inline void arch_read_unlock(arch_rwlock_t *rw)
  167. {
  168. unsigned int tmp, tmp2;
  169. asm volatile(
  170. "1: ldxr %w0, %2\n"
  171. " sub %w0, %w0, #1\n"
  172. " stlxr %w1, %w0, %2\n"
  173. " cbnz %w1, 1b\n"
  174. : "=&r" (tmp), "=&r" (tmp2), "+Q" (rw->lock)
  175. :
  176. : "cc", "memory");
  177. }
  178. static inline int arch_read_trylock(arch_rwlock_t *rw)
  179. {
  180. unsigned int tmp, tmp2 = 1;
  181. asm volatile(
  182. " ldaxr %w0, %2\n"
  183. " add %w0, %w0, #1\n"
  184. " tbnz %w0, #31, 1f\n"
  185. " stxr %w1, %w0, %2\n"
  186. "1:\n"
  187. : "=&r" (tmp), "+r" (tmp2), "+Q" (rw->lock)
  188. :
  189. : "cc", "memory");
  190. return !tmp2;
  191. }
  192. /* read_can_lock - would read_trylock() succeed? */
  193. #define arch_read_can_lock(x) ((x)->lock < 0x80000000)
  194. #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
  195. #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
  196. #define arch_spin_relax(lock) cpu_relax()
  197. #define arch_read_relax(lock) cpu_relax()
  198. #define arch_write_relax(lock) cpu_relax()
  199. #endif /* __ASM_SPINLOCK_H */