spinlock.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. /*
  7. * ARMv6 Spin-locking.
  8. *
  9. * We exclusively read the old value. If it is zero, we may have
  10. * won the lock, so we try exclusively storing it. A memory barrier
  11. * is required after we get a lock, and before we release it, because
  12. * V6 CPUs are assumed to have weakly ordered memory.
  13. *
  14. * Unlocked value: 0
  15. * Locked value: 1
  16. */
  17. #define __raw_spin_is_locked(x) ((x)->lock != 0)
  18. #define __raw_spin_unlock_wait(lock) \
  19. do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0)
  20. #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
  21. static inline void __raw_spin_lock(raw_spinlock_t *lock)
  22. {
  23. unsigned long tmp;
  24. __asm__ __volatile__(
  25. "1: ldrex %0, [%1]\n"
  26. " teq %0, #0\n"
  27. #ifdef CONFIG_CPU_32v6K
  28. " wfene\n"
  29. #endif
  30. " strexeq %0, %2, [%1]\n"
  31. " teqeq %0, #0\n"
  32. " bne 1b"
  33. : "=&r" (tmp)
  34. : "r" (&lock->lock), "r" (1)
  35. : "cc");
  36. smp_mb();
  37. }
  38. static inline int __raw_spin_trylock(raw_spinlock_t *lock)
  39. {
  40. unsigned long tmp;
  41. __asm__ __volatile__(
  42. " ldrex %0, [%1]\n"
  43. " teq %0, #0\n"
  44. " strexeq %0, %2, [%1]"
  45. : "=&r" (tmp)
  46. : "r" (&lock->lock), "r" (1)
  47. : "cc");
  48. if (tmp == 0) {
  49. smp_mb();
  50. return 1;
  51. } else {
  52. return 0;
  53. }
  54. }
  55. static inline void __raw_spin_unlock(raw_spinlock_t *lock)
  56. {
  57. smp_mb();
  58. __asm__ __volatile__(
  59. " str %1, [%0]\n"
  60. #ifdef CONFIG_CPU_32v6K
  61. " mcr p15, 0, %1, c7, c10, 4\n" /* DSB */
  62. " sev"
  63. #endif
  64. :
  65. : "r" (&lock->lock), "r" (0)
  66. : "cc");
  67. }
  68. /*
  69. * RWLOCKS
  70. *
  71. *
  72. * Write locks are easy - we just set bit 31. When unlocking, we can
  73. * just write zero since the lock is exclusively held.
  74. */
  75. #define rwlock_is_locked(x) (*((volatile unsigned int *)(x)) != 0)
  76. static inline void __raw_write_lock(raw_rwlock_t *rw)
  77. {
  78. unsigned long tmp;
  79. __asm__ __volatile__(
  80. "1: ldrex %0, [%1]\n"
  81. " teq %0, #0\n"
  82. #ifdef CONFIG_CPU_32v6K
  83. " wfene\n"
  84. #endif
  85. " strexeq %0, %2, [%1]\n"
  86. " teq %0, #0\n"
  87. " bne 1b"
  88. : "=&r" (tmp)
  89. : "r" (&rw->lock), "r" (0x80000000)
  90. : "cc");
  91. smp_mb();
  92. }
  93. static inline int __raw_write_trylock(raw_rwlock_t *rw)
  94. {
  95. unsigned long tmp;
  96. __asm__ __volatile__(
  97. "1: ldrex %0, [%1]\n"
  98. " teq %0, #0\n"
  99. " strexeq %0, %2, [%1]"
  100. : "=&r" (tmp)
  101. : "r" (&rw->lock), "r" (0x80000000)
  102. : "cc");
  103. if (tmp == 0) {
  104. smp_mb();
  105. return 1;
  106. } else {
  107. return 0;
  108. }
  109. }
  110. static inline void __raw_write_unlock(raw_rwlock_t *rw)
  111. {
  112. smp_mb();
  113. __asm__ __volatile__(
  114. "str %1, [%0]\n"
  115. #ifdef CONFIG_CPU_32v6K
  116. " mcr p15, 0, %1, c7, c10, 4\n" /* DSB */
  117. " sev\n"
  118. #endif
  119. :
  120. : "r" (&rw->lock), "r" (0)
  121. : "cc");
  122. }
  123. /* write_can_lock - would write_trylock() succeed? */
  124. #define __raw_write_can_lock(x) ((x)->lock == 0x80000000)
  125. /*
  126. * Read locks are a bit more hairy:
  127. * - Exclusively load the lock value.
  128. * - Increment it.
  129. * - Store new lock value if positive, and we still own this location.
  130. * If the value is negative, we've already failed.
  131. * - If we failed to store the value, we want a negative result.
  132. * - If we failed, try again.
  133. * Unlocking is similarly hairy. We may have multiple read locks
  134. * currently active. However, we know we won't have any write
  135. * locks.
  136. */
  137. static inline void __raw_read_lock(raw_rwlock_t *rw)
  138. {
  139. unsigned long tmp, tmp2;
  140. __asm__ __volatile__(
  141. "1: ldrex %0, [%2]\n"
  142. " adds %0, %0, #1\n"
  143. " strexpl %1, %0, [%2]\n"
  144. #ifdef CONFIG_CPU_32v6K
  145. " wfemi\n"
  146. #endif
  147. " rsbpls %0, %1, #0\n"
  148. " bmi 1b"
  149. : "=&r" (tmp), "=&r" (tmp2)
  150. : "r" (&rw->lock)
  151. : "cc");
  152. smp_mb();
  153. }
  154. static inline void __raw_read_unlock(raw_rwlock_t *rw)
  155. {
  156. unsigned long tmp, tmp2;
  157. smp_mb();
  158. __asm__ __volatile__(
  159. "1: ldrex %0, [%2]\n"
  160. " sub %0, %0, #1\n"
  161. " strex %1, %0, [%2]\n"
  162. " teq %1, #0\n"
  163. " bne 1b"
  164. #ifdef CONFIG_CPU_32v6K
  165. "\n cmp %0, #0\n"
  166. " mcreq p15, 0, %0, c7, c10, 4\n"
  167. " seveq"
  168. #endif
  169. : "=&r" (tmp), "=&r" (tmp2)
  170. : "r" (&rw->lock)
  171. : "cc");
  172. }
  173. static inline int __raw_read_trylock(raw_rwlock_t *rw)
  174. {
  175. unsigned long tmp, tmp2 = 1;
  176. __asm__ __volatile__(
  177. "1: ldrex %0, [%2]\n"
  178. " adds %0, %0, #1\n"
  179. " strexpl %1, %0, [%2]\n"
  180. : "=&r" (tmp), "+r" (tmp2)
  181. : "r" (&rw->lock)
  182. : "cc");
  183. smp_mb();
  184. return tmp2 == 0;
  185. }
  186. /* read_can_lock - would read_trylock() succeed? */
  187. #define __raw_read_can_lock(x) ((x)->lock < 0x80000000)
  188. #endif /* __ASM_SPINLOCK_H */