spinlock.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #ifdef CONFIG_PARAVIRT
  9. #include <asm/paravirt.h>
  10. #else
  11. #define CLI_STRING "cli"
  12. #define STI_STRING "sti"
  13. #endif /* CONFIG_PARAVIRT */
  14. /*
  15. * Your basic SMP spinlocks, allowing only a single CPU anywhere
  16. *
  17. * Simple spin lock operations. There are two variants, one clears IRQ's
  18. * on the local processor, one does not.
  19. *
  20. * We make no fairness assumptions. They have a cost.
  21. *
  22. * (the type definitions are in asm/spinlock_types.h)
  23. */
  24. static inline int __raw_spin_is_locked(raw_spinlock_t *x)
  25. {
  26. return *(volatile signed char *)(&(x)->slock) <= 0;
  27. }
  28. static inline void __raw_spin_lock(raw_spinlock_t *lock)
  29. {
  30. asm volatile("\n1:\t"
  31. LOCK_PREFIX " ; decb %0\n\t"
  32. "jns 3f\n"
  33. "2:\t"
  34. "rep;nop\n\t"
  35. "cmpb $0,%0\n\t"
  36. "jle 2b\n\t"
  37. "jmp 1b\n"
  38. "3:\n\t"
  39. : "+m" (lock->slock) : : "memory");
  40. }
  41. /*
  42. * It is easier for the lock validator if interrupts are not re-enabled
  43. * in the middle of a lock-acquire. This is a performance feature anyway
  44. * so we turn it off:
  45. *
  46. * NOTE: there's an irqs-on section here, which normally would have to be
  47. * irq-traced, but on CONFIG_TRACE_IRQFLAGS we never use this variant.
  48. */
  49. #ifndef CONFIG_PROVE_LOCKING
  50. static inline void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long flags)
  51. {
  52. asm volatile(
  53. "\n1:\t"
  54. LOCK_PREFIX " ; decb %0\n\t"
  55. "jns 5f\n"
  56. "2:\t"
  57. "testl $0x200, %1\n\t"
  58. "jz 4f\n\t"
  59. STI_STRING "\n"
  60. "3:\t"
  61. "rep;nop\n\t"
  62. "cmpb $0, %0\n\t"
  63. "jle 3b\n\t"
  64. CLI_STRING "\n\t"
  65. "jmp 1b\n"
  66. "4:\t"
  67. "rep;nop\n\t"
  68. "cmpb $0, %0\n\t"
  69. "jg 1b\n\t"
  70. "jmp 4b\n"
  71. "5:\n\t"
  72. : "+m" (lock->slock) : "r" (flags) : "memory");
  73. }
  74. #endif
  75. static inline int __raw_spin_trylock(raw_spinlock_t *lock)
  76. {
  77. char oldval;
  78. asm volatile(
  79. "xchgb %b0,%1"
  80. :"=q" (oldval), "+m" (lock->slock)
  81. :"0" (0) : "memory");
  82. return oldval > 0;
  83. }
  84. /*
  85. * __raw_spin_unlock based on writing $1 to the low byte.
  86. * This method works. Despite all the confusion.
  87. * (except on PPro SMP or if we are using OOSTORE, so we use xchgb there)
  88. * (PPro errata 66, 92)
  89. */
  90. #if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE)
  91. static inline void __raw_spin_unlock(raw_spinlock_t *lock)
  92. {
  93. asm volatile("movb $1,%0" : "+m" (lock->slock) :: "memory");
  94. }
  95. #else
  96. static inline void __raw_spin_unlock(raw_spinlock_t *lock)
  97. {
  98. char oldval = 1;
  99. asm volatile("xchgb %b0, %1"
  100. : "=q" (oldval), "+m" (lock->slock)
  101. : "0" (oldval) : "memory");
  102. }
  103. #endif
  104. static inline void __raw_spin_unlock_wait(raw_spinlock_t *lock)
  105. {
  106. while (__raw_spin_is_locked(lock))
  107. cpu_relax();
  108. }
  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. static inline int __raw_read_can_lock(raw_rwlock_t *x)
  134. {
  135. return (int)(x)->lock > 0;
  136. }
  137. /**
  138. * write_can_lock - would write_trylock() succeed?
  139. * @lock: the rwlock in question.
  140. */
  141. static inline int __raw_write_can_lock(raw_rwlock_t *x)
  142. {
  143. return (x)->lock == RW_LOCK_BIAS;
  144. }
  145. static inline void __raw_read_lock(raw_rwlock_t *rw)
  146. {
  147. asm volatile(LOCK_PREFIX " subl $1,(%0)\n\t"
  148. "jns 1f\n"
  149. "call __read_lock_failed\n\t"
  150. "1:\n"
  151. ::"a" (rw) : "memory");
  152. }
  153. static inline void __raw_write_lock(raw_rwlock_t *rw)
  154. {
  155. asm volatile(LOCK_PREFIX " subl $" RW_LOCK_BIAS_STR ",(%0)\n\t"
  156. "jz 1f\n"
  157. "call __write_lock_failed\n\t"
  158. "1:\n"
  159. ::"a" (rw) : "memory");
  160. }
  161. static inline int __raw_read_trylock(raw_rwlock_t *lock)
  162. {
  163. atomic_t *count = (atomic_t *)lock;
  164. atomic_dec(count);
  165. if (atomic_read(count) >= 0)
  166. return 1;
  167. atomic_inc(count);
  168. return 0;
  169. }
  170. static inline int __raw_write_trylock(raw_rwlock_t *lock)
  171. {
  172. atomic_t *count = (atomic_t *)lock;
  173. if (atomic_sub_and_test(RW_LOCK_BIAS, count))
  174. return 1;
  175. atomic_add(RW_LOCK_BIAS, count);
  176. return 0;
  177. }
  178. static inline void __raw_read_unlock(raw_rwlock_t *rw)
  179. {
  180. asm volatile(LOCK_PREFIX "incl %0" :"+m" (rw->lock) : : "memory");
  181. }
  182. static inline void __raw_write_unlock(raw_rwlock_t *rw)
  183. {
  184. asm volatile(LOCK_PREFIX "addl $" RW_LOCK_BIAS_STR ", %0"
  185. : "+m" (rw->lock) : : "memory");
  186. }
  187. #define _raw_spin_relax(lock) cpu_relax()
  188. #define _raw_read_relax(lock) cpu_relax()
  189. #define _raw_write_relax(lock) cpu_relax()
  190. #endif /* __ASM_SPINLOCK_H */