spinlock.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/config.h>
  7. #include <linux/compiler.h>
  8. /*
  9. * Your basic SMP spinlocks, allowing only a single CPU anywhere
  10. *
  11. * Simple spin lock operations. There are two variants, one clears IRQ's
  12. * on the local processor, one does not.
  13. *
  14. * We make no fairness assumptions. They have a cost.
  15. *
  16. * (the type definitions are in asm/spinlock_types.h)
  17. */
  18. #define __raw_spin_is_locked(x) \
  19. (*(volatile signed char *)(&(x)->slock) <= 0)
  20. #define __raw_spin_lock_string \
  21. "\n1:\t" \
  22. "lock ; decb %0\n\t" \
  23. "jns 3f\n" \
  24. "2:\t" \
  25. "rep;nop\n\t" \
  26. "cmpb $0,%0\n\t" \
  27. "jle 2b\n\t" \
  28. "jmp 1b\n" \
  29. "3:\n\t"
  30. #define __raw_spin_lock_string_flags \
  31. "\n1:\t" \
  32. "lock ; decb %0\n\t" \
  33. "jns 5f\n" \
  34. "2:\t" \
  35. "testl $0x200, %1\n\t" \
  36. "jz 4f\n\t" \
  37. "sti\n" \
  38. "3:\t" \
  39. "rep;nop\n\t" \
  40. "cmpb $0, %0\n\t" \
  41. "jle 3b\n\t" \
  42. "cli\n\t" \
  43. "jmp 1b\n" \
  44. "4:\t" \
  45. "rep;nop\n\t" \
  46. "cmpb $0, %0\n\t" \
  47. "jg 1b\n\t" \
  48. "jmp 4b\n" \
  49. "5:\n\t"
  50. #define __raw_spin_lock_string_up \
  51. "\n\tdecb %0"
  52. static inline void __raw_spin_lock(raw_spinlock_t *lock)
  53. {
  54. alternative_smp(
  55. __raw_spin_lock_string,
  56. __raw_spin_lock_string_up,
  57. "=m" (lock->slock) : : "memory");
  58. }
  59. static inline void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long flags)
  60. {
  61. alternative_smp(
  62. __raw_spin_lock_string_flags,
  63. __raw_spin_lock_string_up,
  64. "=m" (lock->slock) : "r" (flags) : "memory");
  65. }
  66. static inline int __raw_spin_trylock(raw_spinlock_t *lock)
  67. {
  68. char oldval;
  69. __asm__ __volatile__(
  70. "xchgb %b0,%1"
  71. :"=q" (oldval), "=m" (lock->slock)
  72. :"0" (0) : "memory");
  73. return oldval > 0;
  74. }
  75. /*
  76. * __raw_spin_unlock based on writing $1 to the low byte.
  77. * This method works. Despite all the confusion.
  78. * (except on PPro SMP or if we are using OOSTORE, so we use xchgb there)
  79. * (PPro errata 66, 92)
  80. */
  81. #if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE)
  82. #define __raw_spin_unlock_string \
  83. "movb $1,%0" \
  84. :"=m" (lock->slock) : : "memory"
  85. static inline void __raw_spin_unlock(raw_spinlock_t *lock)
  86. {
  87. __asm__ __volatile__(
  88. __raw_spin_unlock_string
  89. );
  90. }
  91. #else
  92. #define __raw_spin_unlock_string \
  93. "xchgb %b0, %1" \
  94. :"=q" (oldval), "=m" (lock->slock) \
  95. :"0" (oldval) : "memory"
  96. static inline void __raw_spin_unlock(raw_spinlock_t *lock)
  97. {
  98. char oldval = 1;
  99. __asm__ __volatile__(
  100. __raw_spin_unlock_string
  101. );
  102. }
  103. #endif
  104. #define __raw_spin_unlock_wait(lock) \
  105. do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0)
  106. /*
  107. * Read-write spinlocks, allowing multiple readers
  108. * but only one writer.
  109. *
  110. * NOTE! it is quite common to have readers in interrupts
  111. * but no interrupt writers. For those circumstances we
  112. * can "mix" irq-safe locks - any writer needs to get a
  113. * irq-safe write-lock, but readers can get non-irqsafe
  114. * read-locks.
  115. *
  116. * On x86, we implement read-write locks as a 32-bit counter
  117. * with the high bit (sign) being the "contended" bit.
  118. *
  119. * The inline assembly is non-obvious. Think about it.
  120. *
  121. * Changed to use the same technique as rw semaphores. See
  122. * semaphore.h for details. -ben
  123. *
  124. * the helpers are in arch/i386/kernel/semaphore.c
  125. */
  126. /**
  127. * read_can_lock - would read_trylock() succeed?
  128. * @lock: the rwlock in question.
  129. */
  130. #define __raw_read_can_lock(x) ((int)(x)->lock > 0)
  131. /**
  132. * write_can_lock - would write_trylock() succeed?
  133. * @lock: the rwlock in question.
  134. */
  135. #define __raw_write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
  136. static inline void __raw_read_lock(raw_rwlock_t *rw)
  137. {
  138. __build_read_lock(rw, "__read_lock_failed");
  139. }
  140. static inline void __raw_write_lock(raw_rwlock_t *rw)
  141. {
  142. __build_write_lock(rw, "__write_lock_failed");
  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. #endif /* __ASM_SPINLOCK_H */