spinlock.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 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 inline int __raw_spin_trylock(raw_spinlock_t *lock)
  91. {
  92. int tmp;
  93. short new;
  94. asm volatile(
  95. "movw %2,%w0\n\t"
  96. "cmpb %h0,%b0\n\t"
  97. "jne 1f\n\t"
  98. "movw %w0,%w1\n\t"
  99. "incb %h1\n\t"
  100. "lock ; cmpxchgw %w1,%2\n\t"
  101. "1:"
  102. "sete %b1\n\t"
  103. "movzbl %b1,%0\n\t"
  104. :"=&a" (tmp), "=Q" (new), "+m" (lock->slock)
  105. :
  106. : "memory", "cc");
  107. return tmp;
  108. }
  109. static inline void __raw_spin_unlock(raw_spinlock_t *lock)
  110. {
  111. __asm__ __volatile__(
  112. UNLOCK_LOCK_PREFIX "incb %0"
  113. :"+m" (lock->slock)
  114. :
  115. :"memory", "cc");
  116. }
  117. #else
  118. static inline int __raw_spin_is_locked(raw_spinlock_t *lock)
  119. {
  120. int tmp = *(volatile signed int *)(&(lock)->slock);
  121. return (((tmp >> 16) & 0xffff) != (tmp & 0xffff));
  122. }
  123. static inline int __raw_spin_is_contended(raw_spinlock_t *lock)
  124. {
  125. int tmp = *(volatile signed int *)(&(lock)->slock);
  126. return (((tmp >> 16) & 0xffff) - (tmp & 0xffff)) > 1;
  127. }
  128. static inline void __raw_spin_lock(raw_spinlock_t *lock)
  129. {
  130. int inc = 0x00010000;
  131. int tmp;
  132. __asm__ __volatile__ (
  133. "lock ; xaddl %0, %1\n"
  134. "movzwl %w0, %2\n\t"
  135. "shrl $16, %0\n\t"
  136. "1:\t"
  137. "cmpl %0, %2\n\t"
  138. "je 2f\n\t"
  139. "rep ; nop\n\t"
  140. "movzwl %1, %2\n\t"
  141. /* don't need lfence here, because loads are in-order */
  142. "jmp 1b\n"
  143. "2:"
  144. :"+Q" (inc), "+m" (lock->slock), "=r" (tmp)
  145. :
  146. :"memory", "cc");
  147. }
  148. #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
  149. static inline int __raw_spin_trylock(raw_spinlock_t *lock)
  150. {
  151. int tmp;
  152. int new;
  153. asm volatile(
  154. "movl %2,%0\n\t"
  155. "movl %0,%1\n\t"
  156. "roll $16, %0\n\t"
  157. "cmpl %0,%1\n\t"
  158. "jne 1f\n\t"
  159. "addl $0x00010000, %1\n\t"
  160. "lock ; cmpxchgl %1,%2\n\t"
  161. "1:"
  162. "sete %b1\n\t"
  163. "movzbl %b1,%0\n\t"
  164. :"=&a" (tmp), "=r" (new), "+m" (lock->slock)
  165. :
  166. : "memory", "cc");
  167. return tmp;
  168. }
  169. static inline void __raw_spin_unlock(raw_spinlock_t *lock)
  170. {
  171. __asm__ __volatile__(
  172. UNLOCK_LOCK_PREFIX "incw %0"
  173. :"+m" (lock->slock)
  174. :
  175. :"memory", "cc");
  176. }
  177. #endif
  178. static inline void __raw_spin_unlock_wait(raw_spinlock_t *lock)
  179. {
  180. while (__raw_spin_is_locked(lock))
  181. cpu_relax();
  182. }
  183. /*
  184. * Read-write spinlocks, allowing multiple readers
  185. * but only one writer.
  186. *
  187. * NOTE! it is quite common to have readers in interrupts
  188. * but no interrupt writers. For those circumstances we
  189. * can "mix" irq-safe locks - any writer needs to get a
  190. * irq-safe write-lock, but readers can get non-irqsafe
  191. * read-locks.
  192. *
  193. * On x86, we implement read-write locks as a 32-bit counter
  194. * with the high bit (sign) being the "contended" bit.
  195. */
  196. /**
  197. * read_can_lock - would read_trylock() succeed?
  198. * @lock: the rwlock in question.
  199. */
  200. static inline int __raw_read_can_lock(raw_rwlock_t *lock)
  201. {
  202. return (int)(lock)->lock > 0;
  203. }
  204. /**
  205. * write_can_lock - would write_trylock() succeed?
  206. * @lock: the rwlock in question.
  207. */
  208. static inline int __raw_write_can_lock(raw_rwlock_t *lock)
  209. {
  210. return (lock)->lock == RW_LOCK_BIAS;
  211. }
  212. static inline void __raw_read_lock(raw_rwlock_t *rw)
  213. {
  214. asm volatile(LOCK_PREFIX " subl $1,(%0)\n\t"
  215. "jns 1f\n"
  216. "call __read_lock_failed\n\t"
  217. "1:\n"
  218. ::LOCK_PTR_REG (rw) : "memory");
  219. }
  220. static inline void __raw_write_lock(raw_rwlock_t *rw)
  221. {
  222. asm volatile(LOCK_PREFIX " subl %1,(%0)\n\t"
  223. "jz 1f\n"
  224. "call __write_lock_failed\n\t"
  225. "1:\n"
  226. ::LOCK_PTR_REG (rw), "i" (RW_LOCK_BIAS) : "memory");
  227. }
  228. static inline int __raw_read_trylock(raw_rwlock_t *lock)
  229. {
  230. atomic_t *count = (atomic_t *)lock;
  231. atomic_dec(count);
  232. if (atomic_read(count) >= 0)
  233. return 1;
  234. atomic_inc(count);
  235. return 0;
  236. }
  237. static inline int __raw_write_trylock(raw_rwlock_t *lock)
  238. {
  239. atomic_t *count = (atomic_t *)lock;
  240. if (atomic_sub_and_test(RW_LOCK_BIAS, count))
  241. return 1;
  242. atomic_add(RW_LOCK_BIAS, count);
  243. return 0;
  244. }
  245. static inline void __raw_read_unlock(raw_rwlock_t *rw)
  246. {
  247. asm volatile(LOCK_PREFIX "incl %0" :"+m" (rw->lock) : : "memory");
  248. }
  249. static inline void __raw_write_unlock(raw_rwlock_t *rw)
  250. {
  251. asm volatile(LOCK_PREFIX "addl %1, %0"
  252. : "+m" (rw->lock) : "i" (RW_LOCK_BIAS) : "memory");
  253. }
  254. #define _raw_spin_relax(lock) cpu_relax()
  255. #define _raw_read_relax(lock) cpu_relax()
  256. #define _raw_write_relax(lock) cpu_relax()
  257. #endif