spinlock.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 ; 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 ; 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. #define __raw_spin_lock_string_up \
  55. "\n\tdecb %0"
  56. static inline void __raw_spin_lock(raw_spinlock_t *lock)
  57. {
  58. alternative_smp(
  59. __raw_spin_lock_string,
  60. __raw_spin_lock_string_up,
  61. "=m" (lock->slock) : : "memory");
  62. }
  63. /*
  64. * It is easier for the lock validator if interrupts are not re-enabled
  65. * in the middle of a lock-acquire. This is a performance feature anyway
  66. * so we turn it off:
  67. */
  68. #ifndef CONFIG_PROVE_LOCKING
  69. static inline void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long flags)
  70. {
  71. alternative_smp(
  72. __raw_spin_lock_string_flags,
  73. __raw_spin_lock_string_up,
  74. "=m" (lock->slock) : "r" (flags) : "memory");
  75. }
  76. #endif
  77. static inline int __raw_spin_trylock(raw_spinlock_t *lock)
  78. {
  79. char oldval;
  80. __asm__ __volatile__(
  81. "xchgb %b0,%1"
  82. :"=q" (oldval), "=m" (lock->slock)
  83. :"0" (0) : "memory");
  84. return oldval > 0;
  85. }
  86. /*
  87. * __raw_spin_unlock based on writing $1 to the low byte.
  88. * This method works. Despite all the confusion.
  89. * (except on PPro SMP or if we are using OOSTORE, so we use xchgb there)
  90. * (PPro errata 66, 92)
  91. */
  92. #if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE)
  93. #define __raw_spin_unlock_string \
  94. "movb $1,%0" \
  95. :"=m" (lock->slock) : : "memory"
  96. static inline void __raw_spin_unlock(raw_spinlock_t *lock)
  97. {
  98. __asm__ __volatile__(
  99. __raw_spin_unlock_string
  100. );
  101. }
  102. #else
  103. #define __raw_spin_unlock_string \
  104. "xchgb %b0, %1" \
  105. :"=q" (oldval), "=m" (lock->slock) \
  106. :"0" (oldval) : "memory"
  107. static inline void __raw_spin_unlock(raw_spinlock_t *lock)
  108. {
  109. char oldval = 1;
  110. __asm__ __volatile__(
  111. __raw_spin_unlock_string
  112. );
  113. }
  114. #endif
  115. #define __raw_spin_unlock_wait(lock) \
  116. do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0)
  117. /*
  118. * Read-write spinlocks, allowing multiple readers
  119. * but only one writer.
  120. *
  121. * NOTE! it is quite common to have readers in interrupts
  122. * but no interrupt writers. For those circumstances we
  123. * can "mix" irq-safe locks - any writer needs to get a
  124. * irq-safe write-lock, but readers can get non-irqsafe
  125. * read-locks.
  126. *
  127. * On x86, we implement read-write locks as a 32-bit counter
  128. * with the high bit (sign) being the "contended" bit.
  129. *
  130. * The inline assembly is non-obvious. Think about it.
  131. *
  132. * Changed to use the same technique as rw semaphores. See
  133. * semaphore.h for details. -ben
  134. *
  135. * the helpers are in arch/i386/kernel/semaphore.c
  136. */
  137. /**
  138. * read_can_lock - would read_trylock() succeed?
  139. * @lock: the rwlock in question.
  140. */
  141. #define __raw_read_can_lock(x) ((int)(x)->lock > 0)
  142. /**
  143. * write_can_lock - would write_trylock() succeed?
  144. * @lock: the rwlock in question.
  145. */
  146. #define __raw_write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
  147. static inline void __raw_read_lock(raw_rwlock_t *rw)
  148. {
  149. __build_read_lock(rw, "__read_lock_failed");
  150. }
  151. static inline void __raw_write_lock(raw_rwlock_t *rw)
  152. {
  153. __build_write_lock(rw, "__write_lock_failed");
  154. }
  155. static inline int __raw_read_trylock(raw_rwlock_t *lock)
  156. {
  157. atomic_t *count = (atomic_t *)lock;
  158. atomic_dec(count);
  159. if (atomic_read(count) >= 0)
  160. return 1;
  161. atomic_inc(count);
  162. return 0;
  163. }
  164. static inline int __raw_write_trylock(raw_rwlock_t *lock)
  165. {
  166. atomic_t *count = (atomic_t *)lock;
  167. if (atomic_sub_and_test(RW_LOCK_BIAS, count))
  168. return 1;
  169. atomic_add(RW_LOCK_BIAS, count);
  170. return 0;
  171. }
  172. static inline void __raw_read_unlock(raw_rwlock_t *rw)
  173. {
  174. asm volatile(LOCK_PREFIX "incl %0" :"=m" (rw->lock) : : "memory");
  175. }
  176. static inline void __raw_write_unlock(raw_rwlock_t *rw)
  177. {
  178. asm volatile(LOCK_PREFIX "addl $" RW_LOCK_BIAS_STR ", %0"
  179. : "=m" (rw->lock) : : "memory");
  180. }
  181. #endif /* __ASM_SPINLOCK_H */