spinlock.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #ifndef __ASM_SPINLOCK_H
  2. #define __ASM_SPINLOCK_H
  3. #include <asm/atomic.h>
  4. #include <asm/rwlock.h>
  5. #include <asm/page.h>
  6. #include <linux/compiler.h>
  7. /*
  8. * Your basic SMP spinlocks, allowing only a single CPU anywhere
  9. *
  10. * Simple spin lock operations. There are two variants, one clears IRQ's
  11. * on the local processor, one does not.
  12. *
  13. * We make no fairness assumptions. They have a cost.
  14. *
  15. * (the type definitions are in asm/spinlock_types.h)
  16. */
  17. #define __raw_spin_is_locked(x) \
  18. (*(volatile signed char *)(&(x)->slock) <= 0)
  19. #define __raw_spin_lock_string \
  20. "\n1:\t" \
  21. LOCK_PREFIX " ; decb %0\n\t" \
  22. "jns 3f\n" \
  23. "2:\t" \
  24. "rep;nop\n\t" \
  25. "cmpb $0,%0\n\t" \
  26. "jle 2b\n\t" \
  27. "jmp 1b\n" \
  28. "3:\n\t"
  29. /*
  30. * NOTE: there's an irqs-on section here, which normally would have to be
  31. * irq-traced, but on CONFIG_TRACE_IRQFLAGS we never use
  32. * __raw_spin_lock_string_flags().
  33. */
  34. #define __raw_spin_lock_string_flags \
  35. "\n1:\t" \
  36. LOCK_PREFIX " ; decb %0\n\t" \
  37. "jns 5f\n" \
  38. "2:\t" \
  39. "testl $0x200, %1\n\t" \
  40. "jz 4f\n\t" \
  41. "sti\n" \
  42. "3:\t" \
  43. "rep;nop\n\t" \
  44. "cmpb $0, %0\n\t" \
  45. "jle 3b\n\t" \
  46. "cli\n\t" \
  47. "jmp 1b\n" \
  48. "4:\t" \
  49. "rep;nop\n\t" \
  50. "cmpb $0, %0\n\t" \
  51. "jg 1b\n\t" \
  52. "jmp 4b\n" \
  53. "5:\n\t"
  54. static inline void __raw_spin_lock(raw_spinlock_t *lock)
  55. {
  56. asm(__raw_spin_lock_string : "+m" (lock->slock) : : "memory");
  57. }
  58. /*
  59. * It is easier for the lock validator if interrupts are not re-enabled
  60. * in the middle of a lock-acquire. This is a performance feature anyway
  61. * so we turn it off:
  62. */
  63. #ifndef CONFIG_PROVE_LOCKING
  64. static inline void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long flags)
  65. {
  66. asm(__raw_spin_lock_string_flags : "+m" (lock->slock) : "r" (flags) : "memory");
  67. }
  68. #endif
  69. static inline int __raw_spin_trylock(raw_spinlock_t *lock)
  70. {
  71. char oldval;
  72. __asm__ __volatile__(
  73. "xchgb %b0,%1"
  74. :"=q" (oldval), "+m" (lock->slock)
  75. :"0" (0) : "memory");
  76. return oldval > 0;
  77. }
  78. /*
  79. * __raw_spin_unlock based on writing $1 to the low byte.
  80. * This method works. Despite all the confusion.
  81. * (except on PPro SMP or if we are using OOSTORE, so we use xchgb there)
  82. * (PPro errata 66, 92)
  83. */
  84. #if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE)
  85. #define __raw_spin_unlock_string \
  86. "movb $1,%0" \
  87. :"+m" (lock->slock) : : "memory"
  88. static inline void __raw_spin_unlock(raw_spinlock_t *lock)
  89. {
  90. __asm__ __volatile__(
  91. __raw_spin_unlock_string
  92. );
  93. }
  94. #else
  95. #define __raw_spin_unlock_string \
  96. "xchgb %b0, %1" \
  97. :"=q" (oldval), "+m" (lock->slock) \
  98. :"0" (oldval) : "memory"
  99. static inline void __raw_spin_unlock(raw_spinlock_t *lock)
  100. {
  101. char oldval = 1;
  102. __asm__ __volatile__(
  103. __raw_spin_unlock_string
  104. );
  105. }
  106. #endif
  107. #define __raw_spin_unlock_wait(lock) \
  108. do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0)
  109. /*
  110. * Read-write spinlocks, allowing multiple readers
  111. * but only one writer.
  112. *
  113. * NOTE! it is quite common to have readers in interrupts
  114. * but no interrupt writers. For those circumstances we
  115. * can "mix" irq-safe locks - any writer needs to get a
  116. * irq-safe write-lock, but readers can get non-irqsafe
  117. * read-locks.
  118. *
  119. * On x86, we implement read-write locks as a 32-bit counter
  120. * with the high bit (sign) being the "contended" bit.
  121. *
  122. * The inline assembly is non-obvious. Think about it.
  123. *
  124. * Changed to use the same technique as rw semaphores. See
  125. * semaphore.h for details. -ben
  126. *
  127. * the helpers are in arch/i386/kernel/semaphore.c
  128. */
  129. /**
  130. * read_can_lock - would read_trylock() succeed?
  131. * @lock: the rwlock in question.
  132. */
  133. #define __raw_read_can_lock(x) ((int)(x)->lock > 0)
  134. /**
  135. * write_can_lock - would write_trylock() succeed?
  136. * @lock: the rwlock in question.
  137. */
  138. #define __raw_write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
  139. static inline void __raw_read_lock(raw_rwlock_t *rw)
  140. {
  141. __build_read_lock(rw, "__read_lock_failed");
  142. }
  143. static inline void __raw_write_lock(raw_rwlock_t *rw)
  144. {
  145. __build_write_lock(rw, "__write_lock_failed");
  146. }
  147. static inline int __raw_read_trylock(raw_rwlock_t *lock)
  148. {
  149. atomic_t *count = (atomic_t *)lock;
  150. atomic_dec(count);
  151. if (atomic_read(count) >= 0)
  152. return 1;
  153. atomic_inc(count);
  154. return 0;
  155. }
  156. static inline int __raw_write_trylock(raw_rwlock_t *lock)
  157. {
  158. atomic_t *count = (atomic_t *)lock;
  159. if (atomic_sub_and_test(RW_LOCK_BIAS, count))
  160. return 1;
  161. atomic_add(RW_LOCK_BIAS, count);
  162. return 0;
  163. }
  164. static inline void __raw_read_unlock(raw_rwlock_t *rw)
  165. {
  166. asm volatile(LOCK_PREFIX "incl %0" :"+m" (rw->lock) : : "memory");
  167. }
  168. static inline void __raw_write_unlock(raw_rwlock_t *rw)
  169. {
  170. asm volatile(LOCK_PREFIX "addl $" RW_LOCK_BIAS_STR ", %0"
  171. : "+m" (rw->lock) : : "memory");
  172. }
  173. #endif /* __ASM_SPINLOCK_H */