spinlock.h 6.8 KB

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