spinlock.h 7.1 KB

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