spinlock.h 4.7 KB

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