spinlock.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. #else
  23. # define LOCK_PTR_REG "D"
  24. #endif
  25. #if defined(CONFIG_X86_32) && \
  26. (defined(CONFIG_X86_OOSTORE) || defined(CONFIG_X86_PPRO_FENCE))
  27. /*
  28. * On PPro SMP or if we are using OOSTORE, we use a locked operation to unlock
  29. * (PPro errata 66, 92)
  30. */
  31. # define UNLOCK_LOCK_PREFIX LOCK_PREFIX
  32. #else
  33. # define UNLOCK_LOCK_PREFIX
  34. #endif
  35. /*
  36. * Ticket locks are conceptually two parts, one indicating the current head of
  37. * the queue, and the other indicating the current tail. The lock is acquired
  38. * by atomically noting the tail and incrementing it by one (thus adding
  39. * ourself to the queue and noting our position), then waiting until the head
  40. * becomes equal to the the initial value of the tail.
  41. *
  42. * We use an xadd covering *both* parts of the lock, to increment the tail and
  43. * also load the position of the head, which takes care of memory ordering
  44. * issues and should be optimal for the uncontended case. Note the tail must be
  45. * in the high part, because a wide xadd increment of the low part would carry
  46. * up and contaminate the high part.
  47. *
  48. * With fewer than 2^8 possible CPUs, we can use x86's partial registers to
  49. * save some instructions and make the code more elegant. There really isn't
  50. * much between them in performance though, especially as locks are out of line.
  51. */
  52. #if (NR_CPUS < 256)
  53. static inline int __ticket_spin_is_locked(raw_spinlock_t *lock)
  54. {
  55. int tmp = ACCESS_ONCE(lock->slock);
  56. return (((tmp >> 8) & 0xff) != (tmp & 0xff));
  57. }
  58. static inline int __ticket_spin_is_contended(raw_spinlock_t *lock)
  59. {
  60. int tmp = ACCESS_ONCE(lock->slock);
  61. return (((tmp >> 8) - tmp) & 0xff) > 1;
  62. }
  63. static __always_inline void __ticket_spin_lock(raw_spinlock_t *lock)
  64. {
  65. short inc = 0x0100;
  66. asm volatile (
  67. LOCK_PREFIX "xaddw %w0, %1\n"
  68. "1:\t"
  69. "cmpb %h0, %b0\n\t"
  70. "je 2f\n\t"
  71. "rep ; nop\n\t"
  72. "movb %1, %b0\n\t"
  73. /* don't need lfence here, because loads are in-order */
  74. "jmp 1b\n"
  75. "2:"
  76. : "+Q" (inc), "+m" (lock->slock)
  77. :
  78. : "memory", "cc");
  79. }
  80. static __always_inline int __ticket_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_PREFIX "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 __ticket_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 __ticket_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 __ticket_spin_is_contended(raw_spinlock_t *lock)
  112. {
  113. int tmp = ACCESS_ONCE(lock->slock);
  114. return (((tmp >> 16) - tmp) & 0xffff) > 1;
  115. }
  116. static __always_inline void __ticket_spin_lock(raw_spinlock_t *lock)
  117. {
  118. int inc = 0x00010000;
  119. int tmp;
  120. asm volatile(LOCK_PREFIX "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. static __always_inline int __ticket_spin_trylock(raw_spinlock_t *lock)
  136. {
  137. int tmp;
  138. int new;
  139. asm volatile("movl %2,%0\n\t"
  140. "movl %0,%1\n\t"
  141. "roll $16, %0\n\t"
  142. "cmpl %0,%1\n\t"
  143. "jne 1f\n\t"
  144. "addl $0x00010000, %1\n\t"
  145. LOCK_PREFIX "cmpxchgl %1,%2\n\t"
  146. "1:"
  147. "sete %b1\n\t"
  148. "movzbl %b1,%0\n\t"
  149. : "=&a" (tmp), "=r" (new), "+m" (lock->slock)
  150. :
  151. : "memory", "cc");
  152. return tmp;
  153. }
  154. static __always_inline void __ticket_spin_unlock(raw_spinlock_t *lock)
  155. {
  156. asm volatile(UNLOCK_LOCK_PREFIX "incw %0"
  157. : "+m" (lock->slock)
  158. :
  159. : "memory", "cc");
  160. }
  161. #endif
  162. #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
  163. #ifdef CONFIG_PARAVIRT
  164. /*
  165. * Define virtualization-friendly old-style lock byte lock, for use in
  166. * pv_lock_ops if desired.
  167. *
  168. * This differs from the pre-2.6.24 spinlock by always using xchgb
  169. * rather than decb to take the lock; this allows it to use a
  170. * zero-initialized lock structure. It also maintains a 1-byte
  171. * contention counter, so that we can implement
  172. * __byte_spin_is_contended.
  173. */
  174. struct __byte_spinlock {
  175. s8 lock;
  176. s8 spinners;
  177. };
  178. static inline int __byte_spin_is_locked(raw_spinlock_t *lock)
  179. {
  180. struct __byte_spinlock *bl = (struct __byte_spinlock *)lock;
  181. return bl->lock != 0;
  182. }
  183. static inline int __byte_spin_is_contended(raw_spinlock_t *lock)
  184. {
  185. struct __byte_spinlock *bl = (struct __byte_spinlock *)lock;
  186. return bl->spinners != 0;
  187. }
  188. static inline void __byte_spin_lock(raw_spinlock_t *lock)
  189. {
  190. struct __byte_spinlock *bl = (struct __byte_spinlock *)lock;
  191. s8 val = 1;
  192. asm("1: xchgb %1, %0\n"
  193. " test %1,%1\n"
  194. " jz 3f\n"
  195. " " LOCK_PREFIX "incb %2\n"
  196. "2: rep;nop\n"
  197. " cmpb $1, %0\n"
  198. " je 2b\n"
  199. " " LOCK_PREFIX "decb %2\n"
  200. " jmp 1b\n"
  201. "3:"
  202. : "+m" (bl->lock), "+q" (val), "+m" (bl->spinners): : "memory");
  203. }
  204. static inline int __byte_spin_trylock(raw_spinlock_t *lock)
  205. {
  206. struct __byte_spinlock *bl = (struct __byte_spinlock *)lock;
  207. u8 old = 1;
  208. asm("xchgb %1,%0"
  209. : "+m" (bl->lock), "+q" (old) : : "memory");
  210. return old == 0;
  211. }
  212. static inline void __byte_spin_unlock(raw_spinlock_t *lock)
  213. {
  214. struct __byte_spinlock *bl = (struct __byte_spinlock *)lock;
  215. smp_wmb();
  216. bl->lock = 0;
  217. }
  218. #else /* !CONFIG_PARAVIRT */
  219. static inline int __raw_spin_is_locked(raw_spinlock_t *lock)
  220. {
  221. return __ticket_spin_is_locked(lock);
  222. }
  223. static inline int __raw_spin_is_contended(raw_spinlock_t *lock)
  224. {
  225. return __ticket_spin_is_contended(lock);
  226. }
  227. static __always_inline void __raw_spin_lock(raw_spinlock_t *lock)
  228. {
  229. __ticket_spin_lock(lock);
  230. }
  231. static __always_inline int __raw_spin_trylock(raw_spinlock_t *lock)
  232. {
  233. return __ticket_spin_trylock(lock);
  234. }
  235. static __always_inline void __raw_spin_unlock(raw_spinlock_t *lock)
  236. {
  237. __ticket_spin_unlock(lock);
  238. }
  239. #endif /* CONFIG_PARAVIRT */
  240. static inline void __raw_spin_unlock_wait(raw_spinlock_t *lock)
  241. {
  242. while (__raw_spin_is_locked(lock))
  243. cpu_relax();
  244. }
  245. /*
  246. * Read-write spinlocks, allowing multiple readers
  247. * but only one writer.
  248. *
  249. * NOTE! it is quite common to have readers in interrupts
  250. * but no interrupt writers. For those circumstances we
  251. * can "mix" irq-safe locks - any writer needs to get a
  252. * irq-safe write-lock, but readers can get non-irqsafe
  253. * read-locks.
  254. *
  255. * On x86, we implement read-write locks as a 32-bit counter
  256. * with the high bit (sign) being the "contended" bit.
  257. */
  258. /**
  259. * read_can_lock - would read_trylock() succeed?
  260. * @lock: the rwlock in question.
  261. */
  262. static inline int __raw_read_can_lock(raw_rwlock_t *lock)
  263. {
  264. return (int)(lock)->lock > 0;
  265. }
  266. /**
  267. * write_can_lock - would write_trylock() succeed?
  268. * @lock: the rwlock in question.
  269. */
  270. static inline int __raw_write_can_lock(raw_rwlock_t *lock)
  271. {
  272. return (lock)->lock == RW_LOCK_BIAS;
  273. }
  274. static inline void __raw_read_lock(raw_rwlock_t *rw)
  275. {
  276. asm volatile(LOCK_PREFIX " subl $1,(%0)\n\t"
  277. "jns 1f\n"
  278. "call __read_lock_failed\n\t"
  279. "1:\n"
  280. ::LOCK_PTR_REG (rw) : "memory");
  281. }
  282. static inline void __raw_write_lock(raw_rwlock_t *rw)
  283. {
  284. asm volatile(LOCK_PREFIX " subl %1,(%0)\n\t"
  285. "jz 1f\n"
  286. "call __write_lock_failed\n\t"
  287. "1:\n"
  288. ::LOCK_PTR_REG (rw), "i" (RW_LOCK_BIAS) : "memory");
  289. }
  290. static inline int __raw_read_trylock(raw_rwlock_t *lock)
  291. {
  292. atomic_t *count = (atomic_t *)lock;
  293. atomic_dec(count);
  294. if (atomic_read(count) >= 0)
  295. return 1;
  296. atomic_inc(count);
  297. return 0;
  298. }
  299. static inline int __raw_write_trylock(raw_rwlock_t *lock)
  300. {
  301. atomic_t *count = (atomic_t *)lock;
  302. if (atomic_sub_and_test(RW_LOCK_BIAS, count))
  303. return 1;
  304. atomic_add(RW_LOCK_BIAS, count);
  305. return 0;
  306. }
  307. static inline void __raw_read_unlock(raw_rwlock_t *rw)
  308. {
  309. asm volatile(LOCK_PREFIX "incl %0" :"+m" (rw->lock) : : "memory");
  310. }
  311. static inline void __raw_write_unlock(raw_rwlock_t *rw)
  312. {
  313. asm volatile(LOCK_PREFIX "addl %1, %0"
  314. : "+m" (rw->lock) : "i" (RW_LOCK_BIAS) : "memory");
  315. }
  316. #define _raw_spin_relax(lock) cpu_relax()
  317. #define _raw_read_relax(lock) cpu_relax()
  318. #define _raw_write_relax(lock) cpu_relax()
  319. #endif /* ASM_X86__SPINLOCK_H */