spinlock.h 4.3 KB

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