spinlock_32.h 5.1 KB

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