spinlock.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #ifndef _X86_SPINLOCK_H_
  2. #define _X86_SPINLOCK_H_
  3. #include <asm/atomic.h>
  4. #include <asm/rwlock.h>
  5. #include <asm/page.h>
  6. #include <asm/processor.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. #ifdef CONFIG_PARAVIRT
  18. #include <asm/paravirt.h>
  19. #else
  20. #define CLI_STRING "cli"
  21. #define STI_STRING "sti"
  22. #define CLI_STI_CLOBBERS
  23. #define CLI_STI_INPUT_ARGS
  24. #endif /* CONFIG_PARAVIRT */
  25. #ifdef CONFIG_X86_32
  26. typedef char _slock_t;
  27. # define LOCK_INS_DEC "decb"
  28. # define LOCK_INS_XCH "xchgb"
  29. # define LOCK_INS_MOV "movb"
  30. # define LOCK_INS_CMP "cmpb"
  31. # define LOCK_PTR_REG "a"
  32. #else
  33. typedef int _slock_t;
  34. # define LOCK_INS_DEC "decl"
  35. # define LOCK_INS_XCH "xchgl"
  36. # define LOCK_INS_MOV "movl"
  37. # define LOCK_INS_CMP "cmpl"
  38. # define LOCK_PTR_REG "D"
  39. #endif
  40. static inline int __raw_spin_is_locked(raw_spinlock_t *lock)
  41. {
  42. return *(volatile _slock_t *)(&(lock)->slock) <= 0;
  43. }
  44. static inline void __raw_spin_lock(raw_spinlock_t *lock)
  45. {
  46. asm volatile(
  47. "\n1:\t"
  48. LOCK_PREFIX " ; " LOCK_INS_DEC " %0\n\t"
  49. "jns 3f\n"
  50. "2:\t"
  51. "rep;nop\n\t"
  52. LOCK_INS_CMP " $0,%0\n\t"
  53. "jle 2b\n\t"
  54. "jmp 1b\n"
  55. "3:\n\t"
  56. : "+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. * NOTE: there's an irqs-on section here, which normally would have to be
  64. * irq-traced, but on CONFIG_TRACE_IRQFLAGS we never use this variant.
  65. */
  66. #ifndef CONFIG_PROVE_LOCKING
  67. static inline void __raw_spin_lock_flags(raw_spinlock_t *lock,
  68. unsigned long flags)
  69. {
  70. asm volatile(
  71. "\n1:\t"
  72. LOCK_PREFIX " ; " LOCK_INS_DEC " %[slock]\n\t"
  73. "jns 5f\n"
  74. "testl $0x200, %[flags]\n\t"
  75. "jz 4f\n\t"
  76. STI_STRING "\n"
  77. "3:\t"
  78. "rep;nop\n\t"
  79. LOCK_INS_CMP " $0, %[slock]\n\t"
  80. "jle 3b\n\t"
  81. CLI_STRING "\n\t"
  82. "jmp 1b\n"
  83. "4:\t"
  84. "rep;nop\n\t"
  85. LOCK_INS_CMP " $0, %[slock]\n\t"
  86. "jg 1b\n\t"
  87. "jmp 4b\n"
  88. "5:\n\t"
  89. : [slock] "+m" (lock->slock)
  90. : [flags] "r" ((u32)flags)
  91. CLI_STI_INPUT_ARGS
  92. : "memory" CLI_STI_CLOBBERS);
  93. }
  94. #endif
  95. static inline int __raw_spin_trylock(raw_spinlock_t *lock)
  96. {
  97. _slock_t oldval;
  98. asm volatile(
  99. LOCK_INS_XCH " %0,%1"
  100. :"=q" (oldval), "+m" (lock->slock)
  101. :"0" (0) : "memory");
  102. return oldval > 0;
  103. }
  104. /*
  105. * __raw_spin_unlock based on writing $1 to the low byte.
  106. * This method works. Despite all the confusion.
  107. * (except on PPro SMP or if we are using OOSTORE, so we use xchgb there)
  108. * (PPro errata 66, 92)
  109. */
  110. #if defined(X86_64) || \
  111. (!defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE))
  112. static inline void __raw_spin_unlock(raw_spinlock_t *lock)
  113. {
  114. asm volatile(LOCK_INS_MOV " $1,%0" : "=m" (lock->slock) :: "memory");
  115. }
  116. #else
  117. static inline void __raw_spin_unlock(raw_spinlock_t *lock)
  118. {
  119. unsigned char oldval = 1;
  120. asm volatile("xchgb %b0, %1"
  121. : "=q" (oldval), "+m" (lock->slock)
  122. : "0" (oldval) : "memory");
  123. }
  124. #endif
  125. static inline void __raw_spin_unlock_wait(raw_spinlock_t *lock)
  126. {
  127. while (__raw_spin_is_locked(lock))
  128. cpu_relax();
  129. }
  130. /*
  131. * Read-write spinlocks, allowing multiple readers
  132. * but only one writer.
  133. *
  134. * NOTE! it is quite common to have readers in interrupts
  135. * but no interrupt writers. For those circumstances we
  136. * can "mix" irq-safe locks - any writer needs to get a
  137. * irq-safe write-lock, but readers can get non-irqsafe
  138. * read-locks.
  139. *
  140. * On x86, we implement read-write locks as a 32-bit counter
  141. * with the high bit (sign) being the "contended" bit.
  142. */
  143. static inline int __raw_read_can_lock(raw_rwlock_t *lock)
  144. {
  145. return (int)(lock)->lock > 0;
  146. }
  147. static inline int __raw_write_can_lock(raw_rwlock_t *lock)
  148. {
  149. return (lock)->lock == RW_LOCK_BIAS;
  150. }
  151. static inline void __raw_read_lock(raw_rwlock_t *rw)
  152. {
  153. asm volatile(LOCK_PREFIX " subl $1,(%0)\n\t"
  154. "jns 1f\n"
  155. "call __read_lock_failed\n\t"
  156. "1:\n"
  157. ::LOCK_PTR_REG (rw) : "memory");
  158. }
  159. static inline void __raw_write_lock(raw_rwlock_t *rw)
  160. {
  161. asm volatile(LOCK_PREFIX " subl %1,(%0)\n\t"
  162. "jz 1f\n"
  163. "call __write_lock_failed\n\t"
  164. "1:\n"
  165. ::LOCK_PTR_REG (rw), "i" (RW_LOCK_BIAS) : "memory");
  166. }
  167. static inline int __raw_read_trylock(raw_rwlock_t *lock)
  168. {
  169. atomic_t *count = (atomic_t *)lock;
  170. atomic_dec(count);
  171. if (atomic_read(count) >= 0)
  172. return 1;
  173. atomic_inc(count);
  174. return 0;
  175. }
  176. static inline int __raw_write_trylock(raw_rwlock_t *lock)
  177. {
  178. atomic_t *count = (atomic_t *)lock;
  179. if (atomic_sub_and_test(RW_LOCK_BIAS, count))
  180. return 1;
  181. atomic_add(RW_LOCK_BIAS, count);
  182. return 0;
  183. }
  184. static inline void __raw_read_unlock(raw_rwlock_t *rw)
  185. {
  186. asm volatile(LOCK_PREFIX "incl %0" :"+m" (rw->lock) : : "memory");
  187. }
  188. static inline void __raw_write_unlock(raw_rwlock_t *rw)
  189. {
  190. asm volatile(LOCK_PREFIX "addl %1, %0"
  191. : "+m" (rw->lock) : "i" (RW_LOCK_BIAS) : "memory");
  192. }
  193. #define _raw_spin_relax(lock) cpu_relax()
  194. #define _raw_read_relax(lock) cpu_relax()
  195. #define _raw_write_relax(lock) cpu_relax()
  196. #endif