spinlock_64.h 4.0 KB

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