spinlock.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. /*
  124. * Read locks are a bit more hairy:
  125. * - Exclusively load the lock value.
  126. * - Increment it.
  127. * - Store new lock value if positive, and we still own this location.
  128. * If the value is negative, we've already failed.
  129. * - If we failed to store the value, we want a negative result.
  130. * - If we failed, try again.
  131. * Unlocking is similarly hairy. We may have multiple read locks
  132. * currently active. However, we know we won't have any write
  133. * locks.
  134. */
  135. static inline void __raw_read_lock(raw_rwlock_t *rw)
  136. {
  137. unsigned long tmp, tmp2;
  138. __asm__ __volatile__(
  139. "1: ldrex %0, [%2]\n"
  140. " adds %0, %0, #1\n"
  141. " strexpl %1, %0, [%2]\n"
  142. #ifdef CONFIG_CPU_32v6K
  143. " wfemi\n"
  144. #endif
  145. " rsbpls %0, %1, #0\n"
  146. " bmi 1b"
  147. : "=&r" (tmp), "=&r" (tmp2)
  148. : "r" (&rw->lock)
  149. : "cc");
  150. smp_mb();
  151. }
  152. static inline void __raw_read_unlock(raw_rwlock_t *rw)
  153. {
  154. unsigned long tmp, tmp2;
  155. smp_mb();
  156. __asm__ __volatile__(
  157. "1: ldrex %0, [%2]\n"
  158. " sub %0, %0, #1\n"
  159. " strex %1, %0, [%2]\n"
  160. " teq %1, #0\n"
  161. " bne 1b"
  162. #ifdef CONFIG_CPU_32v6K
  163. "\n cmp %0, #0\n"
  164. " mcreq p15, 0, %0, c7, c10, 4\n"
  165. " seveq"
  166. #endif
  167. : "=&r" (tmp), "=&r" (tmp2)
  168. : "r" (&rw->lock)
  169. : "cc");
  170. }
  171. #define __raw_read_trylock(lock) generic__raw_read_trylock(lock)
  172. #endif /* __ASM_SPINLOCK_H */