spinlock.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 old value is read exclusively and the new one, if unlocked, is written
  24. * exclusively. In case of failure, the loop is restarted.
  25. *
  26. * The memory barriers are implicit with the load-acquire and store-release
  27. * instructions.
  28. *
  29. * Unlocked value: 0
  30. * Locked value: 1
  31. */
  32. #define arch_spin_is_locked(x) ((x)->lock != 0)
  33. #define arch_spin_unlock_wait(lock) \
  34. do { while (arch_spin_is_locked(lock)) cpu_relax(); } while (0)
  35. #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
  36. static inline void arch_spin_lock(arch_spinlock_t *lock)
  37. {
  38. unsigned int tmp;
  39. asm volatile(
  40. " sevl\n"
  41. "1: wfe\n"
  42. "2: ldaxr %w0, [%1]\n"
  43. " cbnz %w0, 1b\n"
  44. " stxr %w0, %w2, [%1]\n"
  45. " cbnz %w0, 2b\n"
  46. : "=&r" (tmp)
  47. : "r" (&lock->lock), "r" (1)
  48. : "memory");
  49. }
  50. static inline int arch_spin_trylock(arch_spinlock_t *lock)
  51. {
  52. unsigned int tmp;
  53. asm volatile(
  54. " ldaxr %w0, [%1]\n"
  55. " cbnz %w0, 1f\n"
  56. " stxr %w0, %w2, [%1]\n"
  57. "1:\n"
  58. : "=&r" (tmp)
  59. : "r" (&lock->lock), "r" (1)
  60. : "memory");
  61. return !tmp;
  62. }
  63. static inline void arch_spin_unlock(arch_spinlock_t *lock)
  64. {
  65. asm volatile(
  66. " stlr %w1, [%0]\n"
  67. : : "r" (&lock->lock), "r" (0) : "memory");
  68. }
  69. /*
  70. * Write lock implementation.
  71. *
  72. * Write locks set bit 31. Unlocking, is done by writing 0 since the lock is
  73. * exclusively held.
  74. *
  75. * The memory barriers are implicit with the load-acquire and store-release
  76. * instructions.
  77. */
  78. static inline void arch_write_lock(arch_rwlock_t *rw)
  79. {
  80. unsigned int tmp;
  81. asm volatile(
  82. " sevl\n"
  83. "1: wfe\n"
  84. "2: ldaxr %w0, [%1]\n"
  85. " cbnz %w0, 1b\n"
  86. " stxr %w0, %w2, [%1]\n"
  87. " cbnz %w0, 2b\n"
  88. : "=&r" (tmp)
  89. : "r" (&rw->lock), "r" (0x80000000)
  90. : "memory");
  91. }
  92. static inline int arch_write_trylock(arch_rwlock_t *rw)
  93. {
  94. unsigned int tmp;
  95. asm volatile(
  96. " ldaxr %w0, [%1]\n"
  97. " cbnz %w0, 1f\n"
  98. " stxr %w0, %w2, [%1]\n"
  99. "1:\n"
  100. : "=&r" (tmp)
  101. : "r" (&rw->lock), "r" (0x80000000)
  102. : "memory");
  103. return !tmp;
  104. }
  105. static inline void arch_write_unlock(arch_rwlock_t *rw)
  106. {
  107. asm volatile(
  108. " stlr %w1, [%0]\n"
  109. : : "r" (&rw->lock), "r" (0) : "memory");
  110. }
  111. /* write_can_lock - would write_trylock() succeed? */
  112. #define arch_write_can_lock(x) ((x)->lock == 0)
  113. /*
  114. * Read lock implementation.
  115. *
  116. * It exclusively loads the lock value, increments it and stores the new value
  117. * back if positive and the CPU still exclusively owns the location. If the
  118. * value is negative, the lock is already held.
  119. *
  120. * During unlocking there may be multiple active read locks but no write lock.
  121. *
  122. * The memory barriers are implicit with the load-acquire and store-release
  123. * instructions.
  124. */
  125. static inline void arch_read_lock(arch_rwlock_t *rw)
  126. {
  127. unsigned int tmp, tmp2;
  128. asm volatile(
  129. " sevl\n"
  130. "1: wfe\n"
  131. "2: ldaxr %w0, [%2]\n"
  132. " add %w0, %w0, #1\n"
  133. " tbnz %w0, #31, 1b\n"
  134. " stxr %w1, %w0, [%2]\n"
  135. " cbnz %w1, 2b\n"
  136. : "=&r" (tmp), "=&r" (tmp2)
  137. : "r" (&rw->lock)
  138. : "memory");
  139. }
  140. static inline void arch_read_unlock(arch_rwlock_t *rw)
  141. {
  142. unsigned int tmp, tmp2;
  143. asm volatile(
  144. "1: ldxr %w0, [%2]\n"
  145. " sub %w0, %w0, #1\n"
  146. " stlxr %w1, %w0, [%2]\n"
  147. " cbnz %w1, 1b\n"
  148. : "=&r" (tmp), "=&r" (tmp2)
  149. : "r" (&rw->lock)
  150. : "memory");
  151. }
  152. static inline int arch_read_trylock(arch_rwlock_t *rw)
  153. {
  154. unsigned int tmp, tmp2 = 1;
  155. asm volatile(
  156. " ldaxr %w0, [%2]\n"
  157. " add %w0, %w0, #1\n"
  158. " tbnz %w0, #31, 1f\n"
  159. " stxr %w1, %w0, [%2]\n"
  160. "1:\n"
  161. : "=&r" (tmp), "+r" (tmp2)
  162. : "r" (&rw->lock)
  163. : "memory");
  164. return !tmp2;
  165. }
  166. /* read_can_lock - would read_trylock() succeed? */
  167. #define arch_read_can_lock(x) ((x)->lock < 0x80000000)
  168. #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
  169. #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
  170. #define arch_spin_relax(lock) cpu_relax()
  171. #define arch_read_relax(lock) cpu_relax()
  172. #define arch_write_relax(lock) cpu_relax()
  173. #endif /* __ASM_SPINLOCK_H */