spinlock.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #ifndef _ASM_X86_SPINLOCK_H
  2. #define _ASM_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. #include <asm/paravirt.h>
  9. /*
  10. * Your basic SMP spinlocks, allowing only a single CPU anywhere
  11. *
  12. * Simple spin lock operations. There are two variants, one clears IRQ's
  13. * on the local processor, one does not.
  14. *
  15. * These are fair FIFO ticket locks, which are currently limited to 256
  16. * CPUs.
  17. *
  18. * (the type definitions are in asm/spinlock_types.h)
  19. */
  20. #ifdef CONFIG_X86_32
  21. # define LOCK_PTR_REG "a"
  22. # define REG_PTR_MODE "k"
  23. #else
  24. # define LOCK_PTR_REG "D"
  25. # define REG_PTR_MODE "q"
  26. #endif
  27. #if defined(CONFIG_X86_32) && \
  28. (defined(CONFIG_X86_OOSTORE) || defined(CONFIG_X86_PPRO_FENCE))
  29. /*
  30. * On PPro SMP or if we are using OOSTORE, we use a locked operation to unlock
  31. * (PPro errata 66, 92)
  32. */
  33. # define UNLOCK_LOCK_PREFIX LOCK_PREFIX
  34. #else
  35. # define UNLOCK_LOCK_PREFIX
  36. #endif
  37. /*
  38. * Ticket locks are conceptually two parts, one indicating the current head of
  39. * the queue, and the other indicating the current tail. The lock is acquired
  40. * by atomically noting the tail and incrementing it by one (thus adding
  41. * ourself to the queue and noting our position), then waiting until the head
  42. * becomes equal to the the initial value of the tail.
  43. *
  44. * We use an xadd covering *both* parts of the lock, to increment the tail and
  45. * also load the position of the head, which takes care of memory ordering
  46. * issues and should be optimal for the uncontended case. Note the tail must be
  47. * in the high part, because a wide xadd increment of the low part would carry
  48. * up and contaminate the high part.
  49. *
  50. * With fewer than 2^8 possible CPUs, we can use x86's partial registers to
  51. * save some instructions and make the code more elegant. There really isn't
  52. * much between them in performance though, especially as locks are out of line.
  53. */
  54. #if (NR_CPUS < 256)
  55. #define TICKET_SHIFT 8
  56. static __always_inline void __ticket_spin_lock(raw_spinlock_t *lock)
  57. {
  58. short inc = 0x0100;
  59. asm volatile (
  60. LOCK_PREFIX "xaddw %w0, %1\n"
  61. "1:\t"
  62. "cmpb %h0, %b0\n\t"
  63. "je 2f\n\t"
  64. "rep ; nop\n\t"
  65. "movb %1, %b0\n\t"
  66. /* don't need lfence here, because loads are in-order */
  67. "jmp 1b\n"
  68. "2:"
  69. : "+Q" (inc), "+m" (lock->slock)
  70. :
  71. : "memory", "cc");
  72. }
  73. static __always_inline int __ticket_spin_trylock(raw_spinlock_t *lock)
  74. {
  75. int tmp, new;
  76. asm volatile("movzwl %2, %0\n\t"
  77. "cmpb %h0,%b0\n\t"
  78. "leal 0x100(%" REG_PTR_MODE "0), %1\n\t"
  79. "jne 1f\n\t"
  80. LOCK_PREFIX "cmpxchgw %w1,%2\n\t"
  81. "1:"
  82. "sete %b1\n\t"
  83. "movzbl %b1,%0\n\t"
  84. : "=&a" (tmp), "=&q" (new), "+m" (lock->slock)
  85. :
  86. : "memory", "cc");
  87. return tmp;
  88. }
  89. static __always_inline void __ticket_spin_unlock(raw_spinlock_t *lock)
  90. {
  91. asm volatile(UNLOCK_LOCK_PREFIX "incb %0"
  92. : "+m" (lock->slock)
  93. :
  94. : "memory", "cc");
  95. }
  96. #else
  97. #define TICKET_SHIFT 16
  98. static __always_inline void __ticket_spin_lock(raw_spinlock_t *lock)
  99. {
  100. int inc = 0x00010000;
  101. int tmp;
  102. asm volatile(LOCK_PREFIX "xaddl %0, %1\n"
  103. "movzwl %w0, %2\n\t"
  104. "shrl $16, %0\n\t"
  105. "1:\t"
  106. "cmpl %0, %2\n\t"
  107. "je 2f\n\t"
  108. "rep ; nop\n\t"
  109. "movzwl %1, %2\n\t"
  110. /* don't need lfence here, because loads are in-order */
  111. "jmp 1b\n"
  112. "2:"
  113. : "+r" (inc), "+m" (lock->slock), "=&r" (tmp)
  114. :
  115. : "memory", "cc");
  116. }
  117. static __always_inline int __ticket_spin_trylock(raw_spinlock_t *lock)
  118. {
  119. int tmp;
  120. int new;
  121. asm volatile("movl %2,%0\n\t"
  122. "movl %0,%1\n\t"
  123. "roll $16, %0\n\t"
  124. "cmpl %0,%1\n\t"
  125. "leal 0x00010000(%" REG_PTR_MODE "0), %1\n\t"
  126. "jne 1f\n\t"
  127. LOCK_PREFIX "cmpxchgl %1,%2\n\t"
  128. "1:"
  129. "sete %b1\n\t"
  130. "movzbl %b1,%0\n\t"
  131. : "=&a" (tmp), "=&q" (new), "+m" (lock->slock)
  132. :
  133. : "memory", "cc");
  134. return tmp;
  135. }
  136. static __always_inline void __ticket_spin_unlock(raw_spinlock_t *lock)
  137. {
  138. asm volatile(UNLOCK_LOCK_PREFIX "incw %0"
  139. : "+m" (lock->slock)
  140. :
  141. : "memory", "cc");
  142. }
  143. #endif
  144. static inline int __ticket_spin_is_locked(raw_spinlock_t *lock)
  145. {
  146. int tmp = ACCESS_ONCE(lock->slock);
  147. return !!(((tmp >> TICKET_SHIFT) ^ tmp) & ((1 << TICKET_SHIFT) - 1));
  148. }
  149. static inline int __ticket_spin_is_contended(raw_spinlock_t *lock)
  150. {
  151. int tmp = ACCESS_ONCE(lock->slock);
  152. return (((tmp >> TICKET_SHIFT) - tmp) & ((1 << TICKET_SHIFT) - 1)) > 1;
  153. }
  154. #ifndef CONFIG_PARAVIRT
  155. static inline int __raw_spin_is_locked(raw_spinlock_t *lock)
  156. {
  157. return __ticket_spin_is_locked(lock);
  158. }
  159. static inline int __raw_spin_is_contended(raw_spinlock_t *lock)
  160. {
  161. return __ticket_spin_is_contended(lock);
  162. }
  163. #define __raw_spin_is_contended __raw_spin_is_contended
  164. static __always_inline void __raw_spin_lock(raw_spinlock_t *lock)
  165. {
  166. __ticket_spin_lock(lock);
  167. }
  168. static __always_inline int __raw_spin_trylock(raw_spinlock_t *lock)
  169. {
  170. return __ticket_spin_trylock(lock);
  171. }
  172. static __always_inline void __raw_spin_unlock(raw_spinlock_t *lock)
  173. {
  174. __ticket_spin_unlock(lock);
  175. }
  176. static __always_inline void __raw_spin_lock_flags(raw_spinlock_t *lock,
  177. unsigned long flags)
  178. {
  179. __raw_spin_lock(lock);
  180. }
  181. #endif
  182. static inline void __raw_spin_unlock_wait(raw_spinlock_t *lock)
  183. {
  184. while (__raw_spin_is_locked(lock))
  185. cpu_relax();
  186. }
  187. /*
  188. * Read-write spinlocks, allowing multiple readers
  189. * but only one writer.
  190. *
  191. * NOTE! it is quite common to have readers in interrupts
  192. * but no interrupt writers. For those circumstances we
  193. * can "mix" irq-safe locks - any writer needs to get a
  194. * irq-safe write-lock, but readers can get non-irqsafe
  195. * read-locks.
  196. *
  197. * On x86, we implement read-write locks as a 32-bit counter
  198. * with the high bit (sign) being the "contended" bit.
  199. */
  200. /**
  201. * read_can_lock - would read_trylock() succeed?
  202. * @lock: the rwlock in question.
  203. */
  204. static inline int __raw_read_can_lock(raw_rwlock_t *lock)
  205. {
  206. return (int)(lock)->lock > 0;
  207. }
  208. /**
  209. * write_can_lock - would write_trylock() succeed?
  210. * @lock: the rwlock in question.
  211. */
  212. static inline int __raw_write_can_lock(raw_rwlock_t *lock)
  213. {
  214. return (lock)->lock == RW_LOCK_BIAS;
  215. }
  216. static inline void __raw_read_lock(raw_rwlock_t *rw)
  217. {
  218. asm volatile(LOCK_PREFIX " subl $1,(%0)\n\t"
  219. "jns 1f\n"
  220. "call __read_lock_failed\n\t"
  221. "1:\n"
  222. ::LOCK_PTR_REG (rw) : "memory");
  223. }
  224. static inline void __raw_write_lock(raw_rwlock_t *rw)
  225. {
  226. asm volatile(LOCK_PREFIX " subl %1,(%0)\n\t"
  227. "jz 1f\n"
  228. "call __write_lock_failed\n\t"
  229. "1:\n"
  230. ::LOCK_PTR_REG (rw), "i" (RW_LOCK_BIAS) : "memory");
  231. }
  232. static inline int __raw_read_trylock(raw_rwlock_t *lock)
  233. {
  234. atomic_t *count = (atomic_t *)lock;
  235. if (atomic_dec_return(count) >= 0)
  236. return 1;
  237. atomic_inc(count);
  238. return 0;
  239. }
  240. static inline int __raw_write_trylock(raw_rwlock_t *lock)
  241. {
  242. atomic_t *count = (atomic_t *)lock;
  243. if (atomic_sub_and_test(RW_LOCK_BIAS, count))
  244. return 1;
  245. atomic_add(RW_LOCK_BIAS, count);
  246. return 0;
  247. }
  248. static inline void __raw_read_unlock(raw_rwlock_t *rw)
  249. {
  250. asm volatile(LOCK_PREFIX "incl %0" :"+m" (rw->lock) : : "memory");
  251. }
  252. static inline void __raw_write_unlock(raw_rwlock_t *rw)
  253. {
  254. asm volatile(LOCK_PREFIX "addl %1, %0"
  255. : "+m" (rw->lock) : "i" (RW_LOCK_BIAS) : "memory");
  256. }
  257. #define __raw_read_lock_flags(lock, flags) __raw_read_lock(lock)
  258. #define __raw_write_lock_flags(lock, flags) __raw_write_lock(lock)
  259. #define _raw_spin_relax(lock) cpu_relax()
  260. #define _raw_read_relax(lock) cpu_relax()
  261. #define _raw_write_relax(lock) cpu_relax()
  262. #endif /* _ASM_X86_SPINLOCK_H */