spinlock_32.h 4.5 KB

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