spinlock.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #ifndef _X86_SPINLOCK_H_
  2. #define _X86_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. /*
  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. * These are fair FIFO ticket locks, which are currently limited to 256
  15. * CPUs.
  16. *
  17. * (the type definitions are in asm/spinlock_types.h)
  18. */
  19. #ifdef CONFIG_PARAVIRT
  20. #include <asm/paravirt.h>
  21. #else
  22. #define CLI_STRING "cli"
  23. #define STI_STRING "sti"
  24. #define CLI_STI_CLOBBERS
  25. #define CLI_STI_INPUT_ARGS
  26. #endif /* CONFIG_PARAVIRT */
  27. #ifdef CONFIG_X86_32
  28. typedef char _slock_t;
  29. # define LOCK_INS_DEC "decb"
  30. # define LOCK_INS_XCH "xchgb"
  31. # define LOCK_INS_MOV "movb"
  32. # define LOCK_INS_CMP "cmpb"
  33. # define LOCK_PTR_REG "a"
  34. #else
  35. typedef int _slock_t;
  36. # define LOCK_INS_DEC "decl"
  37. # define LOCK_INS_XCH "xchgl"
  38. # define LOCK_INS_MOV "movl"
  39. # define LOCK_INS_CMP "cmpl"
  40. # define LOCK_PTR_REG "D"
  41. #endif
  42. #if (NR_CPUS > 256)
  43. #error spinlock supports a maximum of 256 CPUs
  44. #endif
  45. static inline int __raw_spin_is_locked(raw_spinlock_t *lock)
  46. {
  47. int tmp = *(volatile signed int *)(&(lock)->slock);
  48. return (((tmp >> 8) & 0xff) != (tmp & 0xff));
  49. }
  50. static inline int __raw_spin_is_contended(raw_spinlock_t *lock)
  51. {
  52. int tmp = *(volatile signed int *)(&(lock)->slock);
  53. return (((tmp >> 8) & 0xff) - (tmp & 0xff)) > 1;
  54. }
  55. static inline void __raw_spin_lock(raw_spinlock_t *lock)
  56. {
  57. short inc = 0x0100;
  58. /*
  59. * Ticket locks are conceptually two bytes, one indicating the current
  60. * head of the queue, and the other indicating the current tail. The
  61. * lock is acquired by atomically noting the tail and incrementing it
  62. * by one (thus adding ourself to the queue and noting our position),
  63. * then waiting until the head becomes equal to the the initial value
  64. * of the tail.
  65. *
  66. * This uses a 16-bit xadd to increment the tail and also load the
  67. * position of the head, which takes care of memory ordering issues
  68. * and should be optimal for the uncontended case. Note the tail must
  69. * be in the high byte, otherwise the 16-bit wide increment of the low
  70. * byte would carry up and contaminate the high byte.
  71. */
  72. __asm__ __volatile__ (
  73. LOCK_PREFIX "xaddw %w0, %1\n"
  74. "1:\t"
  75. "cmpb %h0, %b0\n\t"
  76. "je 2f\n\t"
  77. "rep ; nop\n\t"
  78. "movb %1, %b0\n\t"
  79. /* don't need lfence here, because loads are in-order */
  80. "jmp 1b\n"
  81. "2:"
  82. :"+Q" (inc), "+m" (lock->slock)
  83. :
  84. :"memory", "cc");
  85. }
  86. #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
  87. static inline int __raw_spin_trylock(raw_spinlock_t *lock)
  88. {
  89. int tmp;
  90. short new;
  91. asm volatile(
  92. "movw %2,%w0\n\t"
  93. "cmpb %h0,%b0\n\t"
  94. "jne 1f\n\t"
  95. "movw %w0,%w1\n\t"
  96. "incb %h1\n\t"
  97. "lock ; cmpxchgw %w1,%2\n\t"
  98. "1:"
  99. "sete %b1\n\t"
  100. "movzbl %b1,%0\n\t"
  101. :"=&a" (tmp), "=Q" (new), "+m" (lock->slock)
  102. :
  103. : "memory", "cc");
  104. return tmp;
  105. }
  106. #if defined(CONFIG_X86_32) && \
  107. (defined(CONFIG_X86_OOSTORE) || defined(CONFIG_X86_PPRO_FENCE))
  108. /*
  109. * On PPro SMP or if we are using OOSTORE, we use a locked operation to unlock
  110. * (PPro errata 66, 92)
  111. */
  112. # define UNLOCK_LOCK_PREFIX LOCK_PREFIX
  113. #else
  114. # define UNLOCK_LOCK_PREFIX
  115. #endif
  116. static inline void __raw_spin_unlock(raw_spinlock_t *lock)
  117. {
  118. __asm__ __volatile__(
  119. UNLOCK_LOCK_PREFIX "incb %0"
  120. :"+m" (lock->slock)
  121. :
  122. :"memory", "cc");
  123. }
  124. static inline void __raw_spin_unlock_wait(raw_spinlock_t *lock)
  125. {
  126. while (__raw_spin_is_locked(lock))
  127. cpu_relax();
  128. }
  129. /*
  130. * Read-write spinlocks, allowing multiple readers
  131. * but only one writer.
  132. *
  133. * NOTE! it is quite common to have readers in interrupts
  134. * but no interrupt writers. For those circumstances we
  135. * can "mix" irq-safe locks - any writer needs to get a
  136. * irq-safe write-lock, but readers can get non-irqsafe
  137. * read-locks.
  138. *
  139. * On x86, we implement read-write locks as a 32-bit counter
  140. * with the high bit (sign) being the "contended" bit.
  141. */
  142. /**
  143. * read_can_lock - would read_trylock() succeed?
  144. * @lock: the rwlock in question.
  145. */
  146. static inline int __raw_read_can_lock(raw_rwlock_t *lock)
  147. {
  148. return (int)(lock)->lock > 0;
  149. }
  150. /**
  151. * write_can_lock - would write_trylock() succeed?
  152. * @lock: the rwlock in question.
  153. */
  154. static inline int __raw_write_can_lock(raw_rwlock_t *lock)
  155. {
  156. return (lock)->lock == RW_LOCK_BIAS;
  157. }
  158. static inline void __raw_read_lock(raw_rwlock_t *rw)
  159. {
  160. asm volatile(LOCK_PREFIX " subl $1,(%0)\n\t"
  161. "jns 1f\n"
  162. "call __read_lock_failed\n\t"
  163. "1:\n"
  164. ::LOCK_PTR_REG (rw) : "memory");
  165. }
  166. static inline void __raw_write_lock(raw_rwlock_t *rw)
  167. {
  168. asm volatile(LOCK_PREFIX " subl %1,(%0)\n\t"
  169. "jz 1f\n"
  170. "call __write_lock_failed\n\t"
  171. "1:\n"
  172. ::LOCK_PTR_REG (rw), "i" (RW_LOCK_BIAS) : "memory");
  173. }
  174. static inline int __raw_read_trylock(raw_rwlock_t *lock)
  175. {
  176. atomic_t *count = (atomic_t *)lock;
  177. atomic_dec(count);
  178. if (atomic_read(count) >= 0)
  179. return 1;
  180. atomic_inc(count);
  181. return 0;
  182. }
  183. static inline int __raw_write_trylock(raw_rwlock_t *lock)
  184. {
  185. atomic_t *count = (atomic_t *)lock;
  186. if (atomic_sub_and_test(RW_LOCK_BIAS, count))
  187. return 1;
  188. atomic_add(RW_LOCK_BIAS, count);
  189. return 0;
  190. }
  191. static inline void __raw_read_unlock(raw_rwlock_t *rw)
  192. {
  193. asm volatile(LOCK_PREFIX "incl %0" :"+m" (rw->lock) : : "memory");
  194. }
  195. static inline void __raw_write_unlock(raw_rwlock_t *rw)
  196. {
  197. asm volatile(LOCK_PREFIX "addl %1, %0"
  198. : "+m" (rw->lock) : "i" (RW_LOCK_BIAS) : "memory");
  199. }
  200. #define _raw_spin_relax(lock) cpu_relax()
  201. #define _raw_read_relax(lock) cpu_relax()
  202. #define _raw_write_relax(lock) cpu_relax()
  203. #endif