spinlock.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #ifndef _ALPHA_SPINLOCK_H
  2. #define _ALPHA_SPINLOCK_H
  3. #include <linux/kernel.h>
  4. #include <asm/current.h>
  5. #include <asm/barrier.h>
  6. #include <asm/processor.h>
  7. /*
  8. * Simple spin lock operations. There are two variants, one clears IRQ's
  9. * on the local processor, one does not.
  10. *
  11. * We make no fairness assumptions. They have a cost.
  12. */
  13. #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
  14. #define arch_spin_is_locked(x) ((x)->lock != 0)
  15. static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
  16. {
  17. smp_cond_load_acquire(&lock->lock, !VAL);
  18. }
  19. static inline int arch_spin_value_unlocked(arch_spinlock_t lock)
  20. {
  21. return lock.lock == 0;
  22. }
  23. static inline void arch_spin_unlock(arch_spinlock_t * lock)
  24. {
  25. mb();
  26. lock->lock = 0;
  27. }
  28. static inline void arch_spin_lock(arch_spinlock_t * lock)
  29. {
  30. long tmp;
  31. __asm__ __volatile__(
  32. "1: ldl_l %0,%1\n"
  33. " bne %0,2f\n"
  34. " lda %0,1\n"
  35. " stl_c %0,%1\n"
  36. " beq %0,2f\n"
  37. " mb\n"
  38. ".subsection 2\n"
  39. "2: ldl %0,%1\n"
  40. " bne %0,2b\n"
  41. " br 1b\n"
  42. ".previous"
  43. : "=&r" (tmp), "=m" (lock->lock)
  44. : "m"(lock->lock) : "memory");
  45. }
  46. static inline int arch_spin_trylock(arch_spinlock_t *lock)
  47. {
  48. return !test_and_set_bit(0, &lock->lock);
  49. }
  50. /***********************************************************/
  51. static inline int arch_read_can_lock(arch_rwlock_t *lock)
  52. {
  53. return (lock->lock & 1) == 0;
  54. }
  55. static inline int arch_write_can_lock(arch_rwlock_t *lock)
  56. {
  57. return lock->lock == 0;
  58. }
  59. static inline void arch_read_lock(arch_rwlock_t *lock)
  60. {
  61. long regx;
  62. __asm__ __volatile__(
  63. "1: ldl_l %1,%0\n"
  64. " blbs %1,6f\n"
  65. " subl %1,2,%1\n"
  66. " stl_c %1,%0\n"
  67. " beq %1,6f\n"
  68. " mb\n"
  69. ".subsection 2\n"
  70. "6: ldl %1,%0\n"
  71. " blbs %1,6b\n"
  72. " br 1b\n"
  73. ".previous"
  74. : "=m" (*lock), "=&r" (regx)
  75. : "m" (*lock) : "memory");
  76. }
  77. static inline void arch_write_lock(arch_rwlock_t *lock)
  78. {
  79. long regx;
  80. __asm__ __volatile__(
  81. "1: ldl_l %1,%0\n"
  82. " bne %1,6f\n"
  83. " lda %1,1\n"
  84. " stl_c %1,%0\n"
  85. " beq %1,6f\n"
  86. " mb\n"
  87. ".subsection 2\n"
  88. "6: ldl %1,%0\n"
  89. " bne %1,6b\n"
  90. " br 1b\n"
  91. ".previous"
  92. : "=m" (*lock), "=&r" (regx)
  93. : "m" (*lock) : "memory");
  94. }
  95. static inline int arch_read_trylock(arch_rwlock_t * lock)
  96. {
  97. long regx;
  98. int success;
  99. __asm__ __volatile__(
  100. "1: ldl_l %1,%0\n"
  101. " lda %2,0\n"
  102. " blbs %1,2f\n"
  103. " subl %1,2,%2\n"
  104. " stl_c %2,%0\n"
  105. " beq %2,6f\n"
  106. "2: mb\n"
  107. ".subsection 2\n"
  108. "6: br 1b\n"
  109. ".previous"
  110. : "=m" (*lock), "=&r" (regx), "=&r" (success)
  111. : "m" (*lock) : "memory");
  112. return success;
  113. }
  114. static inline int arch_write_trylock(arch_rwlock_t * lock)
  115. {
  116. long regx;
  117. int success;
  118. __asm__ __volatile__(
  119. "1: ldl_l %1,%0\n"
  120. " lda %2,0\n"
  121. " bne %1,2f\n"
  122. " lda %2,1\n"
  123. " stl_c %2,%0\n"
  124. " beq %2,6f\n"
  125. "2: mb\n"
  126. ".subsection 2\n"
  127. "6: br 1b\n"
  128. ".previous"
  129. : "=m" (*lock), "=&r" (regx), "=&r" (success)
  130. : "m" (*lock) : "memory");
  131. return success;
  132. }
  133. static inline void arch_read_unlock(arch_rwlock_t * lock)
  134. {
  135. long regx;
  136. __asm__ __volatile__(
  137. " mb\n"
  138. "1: ldl_l %1,%0\n"
  139. " addl %1,2,%1\n"
  140. " stl_c %1,%0\n"
  141. " beq %1,6f\n"
  142. ".subsection 2\n"
  143. "6: br 1b\n"
  144. ".previous"
  145. : "=m" (*lock), "=&r" (regx)
  146. : "m" (*lock) : "memory");
  147. }
  148. static inline void arch_write_unlock(arch_rwlock_t * lock)
  149. {
  150. mb();
  151. lock->lock = 0;
  152. }
  153. #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
  154. #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
  155. #endif /* _ALPHA_SPINLOCK_H */