spinlock.h 4.5 KB

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