spinlock_64.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #ifndef __ASM_SPINLOCK_H
  2. #define __ASM_SPINLOCK_H
  3. #include <asm/atomic.h>
  4. #include <asm/rwlock.h>
  5. #include <asm/page.h>
  6. #include <asm/processor.h>
  7. /*
  8. * Your basic SMP spinlocks, allowing only a single CPU anywhere
  9. *
  10. * Simple spin lock operations. There are two variants, one clears IRQ's
  11. * on the local processor, one does not.
  12. *
  13. * We make no fairness assumptions. They have a cost.
  14. *
  15. * (the type definitions are in asm/spinlock_types.h)
  16. */
  17. typedef int _slock_t;
  18. #define LOCK_INS_DEC "decl"
  19. #define LOCK_INS_XCH "xchgl"
  20. #define LOCK_INS_MOV "movl"
  21. #define LOCK_INS_CMP "cmpl"
  22. #define LOCK_PTR_REG "D"
  23. static inline int __raw_spin_is_locked(raw_spinlock_t *lock)
  24. {
  25. return *(volatile _slock_t *)(&(lock)->slock) <= 0;
  26. }
  27. static inline void __raw_spin_lock(raw_spinlock_t *lock)
  28. {
  29. asm volatile(
  30. "\n1:\t"
  31. LOCK_PREFIX " ; " LOCK_INS_DEC " %0\n\t"
  32. "jns 3f\n"
  33. "2:\t"
  34. "rep;nop\n\t"
  35. LOCK_INS_CMP " $0,%0\n\t"
  36. "jle 2b\n\t"
  37. "jmp 1b\n"
  38. "3:\n\t"
  39. : "+m" (lock->slock) : : "memory");
  40. }
  41. /*
  42. * It is easier for the lock validator if interrupts are not re-enabled
  43. * in the middle of a lock-acquire. This is a performance feature anyway
  44. * so we turn it off:
  45. *
  46. * NOTE: there's an irqs-on section here, which normally would have to be
  47. * irq-traced, but on CONFIG_TRACE_IRQFLAGS we never use this variant.
  48. */
  49. #ifndef CONFIG_PROVE_LOCKING
  50. static inline void __raw_spin_lock_flags(raw_spinlock_t *lock,
  51. unsigned long flags)
  52. {
  53. asm volatile(
  54. "\n1:\t"
  55. LOCK_PREFIX " ; " LOCK_INS_DEC " %[slock]\n\t"
  56. "jns 5f\n"
  57. "testl $0x200, %[flags]\n\t"
  58. "jz 4f\n\t"
  59. STI_STRING "\n"
  60. "3:\t"
  61. "rep;nop\n\t"
  62. LOCK_INS_CMP " $0, %[slock]\n\t"
  63. "jle 3b\n\t"
  64. CLI_STRING "\n\t"
  65. "jmp 1b\n"
  66. "4:\t"
  67. "rep;nop\n\t"
  68. LOCK_INS_CMP " $0, %[slock]\n\t"
  69. "jg 1b\n\t"
  70. "jmp 4b\n"
  71. "5:\n\t"
  72. : [slock] "+m" (lock->slock)
  73. : [flags] "r" ((u32)flags)
  74. CLI_STI_INPUT_ARGS
  75. : "memory" CLI_STI_CLOBBERS);
  76. }
  77. #endif
  78. static inline int __raw_spin_trylock(raw_spinlock_t *lock)
  79. {
  80. _slock_t oldval;
  81. asm volatile(
  82. LOCK_INS_XCH " %0,%1"
  83. :"=q" (oldval), "+m" (lock->slock)
  84. :"0" (0) : "memory");
  85. return oldval > 0;
  86. }
  87. static inline void __raw_spin_unlock(raw_spinlock_t *lock)
  88. {
  89. asm volatile(LOCK_INS_MOV " $1,%0" : "=m" (lock->slock) :: "memory");
  90. }
  91. static inline void __raw_spin_unlock_wait(raw_spinlock_t *lock)
  92. {
  93. while (__raw_spin_is_locked(lock))
  94. cpu_relax();
  95. }
  96. /*
  97. * Read-write spinlocks, allowing multiple readers
  98. * but only one writer.
  99. *
  100. * NOTE! it is quite common to have readers in interrupts
  101. * but no interrupt writers. For those circumstances we
  102. * can "mix" irq-safe locks - any writer needs to get a
  103. * irq-safe write-lock, but readers can get non-irqsafe
  104. * read-locks.
  105. *
  106. * On x86, we implement read-write locks as a 32-bit counter
  107. * with the high bit (sign) being the "contended" bit.
  108. */
  109. static inline int __raw_read_can_lock(raw_rwlock_t *lock)
  110. {
  111. return (int)(lock)->lock > 0;
  112. }
  113. static inline int __raw_write_can_lock(raw_rwlock_t *lock)
  114. {
  115. return (lock)->lock == RW_LOCK_BIAS;
  116. }
  117. static inline void __raw_read_lock(raw_rwlock_t *rw)
  118. {
  119. asm volatile(LOCK_PREFIX " subl $1,(%0)\n\t"
  120. "jns 1f\n"
  121. "call __read_lock_failed\n\t"
  122. "1:\n"
  123. ::LOCK_PTR_REG (rw) : "memory");
  124. }
  125. static inline void __raw_write_lock(raw_rwlock_t *rw)
  126. {
  127. asm volatile(LOCK_PREFIX " subl %1,(%0)\n\t"
  128. "jz 1f\n"
  129. "call __write_lock_failed\n\t"
  130. "1:\n"
  131. ::LOCK_PTR_REG (rw), "i" (RW_LOCK_BIAS) : "memory");
  132. }
  133. static inline int __raw_read_trylock(raw_rwlock_t *lock)
  134. {
  135. atomic_t *count = (atomic_t *)lock;
  136. atomic_dec(count);
  137. if (atomic_read(count) >= 0)
  138. return 1;
  139. atomic_inc(count);
  140. return 0;
  141. }
  142. static inline int __raw_write_trylock(raw_rwlock_t *lock)
  143. {
  144. atomic_t *count = (atomic_t *)lock;
  145. if (atomic_sub_and_test(RW_LOCK_BIAS, count))
  146. return 1;
  147. atomic_add(RW_LOCK_BIAS, count);
  148. return 0;
  149. }
  150. static inline void __raw_read_unlock(raw_rwlock_t *rw)
  151. {
  152. asm volatile(LOCK_PREFIX "incl %0" :"+m" (rw->lock) : : "memory");
  153. }
  154. static inline void __raw_write_unlock(raw_rwlock_t *rw)
  155. {
  156. asm volatile(LOCK_PREFIX "addl %1, %0"
  157. : "+m" (rw->lock) : "i" (RW_LOCK_BIAS) : "memory");
  158. }
  159. #define _raw_spin_relax(lock) cpu_relax()
  160. #define _raw_read_relax(lock) cpu_relax()
  161. #define _raw_write_relax(lock) cpu_relax()
  162. #endif /* __ASM_SPINLOCK_H */