spinlock_64.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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" : "=m" (lock->slock) : : "memory");
  33. }
  34. /*
  35. * Same as __raw_spin_lock, but reenable interrupts during spinning.
  36. */
  37. #ifndef CONFIG_PROVE_LOCKING
  38. static inline void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long flags)
  39. {
  40. asm volatile(
  41. "\n1:\t"
  42. LOCK_PREFIX " ; decl %0\n\t"
  43. "jns 5f\n"
  44. "testl $0x200, %1\n\t" /* interrupts were disabled? */
  45. "jz 4f\n\t"
  46. "sti\n"
  47. "3:\t"
  48. "rep;nop\n\t"
  49. "cmpl $0, %0\n\t"
  50. "jle 3b\n\t"
  51. "cli\n\t"
  52. "jmp 1b\n"
  53. "4:\t"
  54. "rep;nop\n\t"
  55. "cmpl $0, %0\n\t"
  56. "jg 1b\n\t"
  57. "jmp 4b\n"
  58. "5:\n\t"
  59. : "+m" (lock->slock) : "r" ((unsigned)flags) : "memory");
  60. }
  61. #endif
  62. static inline int __raw_spin_trylock(raw_spinlock_t *lock)
  63. {
  64. int oldval;
  65. asm volatile(
  66. "xchgl %0,%1"
  67. :"=q" (oldval), "=m" (lock->slock)
  68. :"0" (0) : "memory");
  69. return oldval > 0;
  70. }
  71. static inline void __raw_spin_unlock(raw_spinlock_t *lock)
  72. {
  73. asm volatile("movl $1,%0" :"=m" (lock->slock) :: "memory");
  74. }
  75. static inline void __raw_spin_unlock_wait(raw_spinlock_t *lock)
  76. {
  77. while (__raw_spin_is_locked(lock))
  78. cpu_relax();
  79. }
  80. /*
  81. * Read-write spinlocks, allowing multiple readers
  82. * but only one writer.
  83. *
  84. * NOTE! it is quite common to have readers in interrupts
  85. * but no interrupt writers. For those circumstances we
  86. * can "mix" irq-safe locks - any writer needs to get a
  87. * irq-safe write-lock, but readers can get non-irqsafe
  88. * read-locks.
  89. *
  90. * On x86, we implement read-write locks as a 32-bit counter
  91. * with the high bit (sign) being the "contended" bit.
  92. */
  93. static inline int __raw_read_can_lock(raw_rwlock_t *lock)
  94. {
  95. return (int)(lock)->lock > 0;
  96. }
  97. static inline int __raw_write_can_lock(raw_rwlock_t *lock)
  98. {
  99. return (lock)->lock == RW_LOCK_BIAS;
  100. }
  101. static inline void __raw_read_lock(raw_rwlock_t *rw)
  102. {
  103. asm volatile(LOCK_PREFIX "subl $1,(%0)\n\t"
  104. "jns 1f\n"
  105. "call __read_lock_failed\n"
  106. "1:\n"
  107. ::"D" (rw), "i" (RW_LOCK_BIAS) : "memory");
  108. }
  109. static inline void __raw_write_lock(raw_rwlock_t *rw)
  110. {
  111. asm volatile(LOCK_PREFIX "subl %1,(%0)\n\t"
  112. "jz 1f\n"
  113. "\tcall __write_lock_failed\n\t"
  114. "1:\n"
  115. ::"D" (rw), "i" (RW_LOCK_BIAS) : "memory");
  116. }
  117. static inline int __raw_read_trylock(raw_rwlock_t *lock)
  118. {
  119. atomic_t *count = (atomic_t *)lock;
  120. atomic_dec(count);
  121. if (atomic_read(count) >= 0)
  122. return 1;
  123. atomic_inc(count);
  124. return 0;
  125. }
  126. static inline int __raw_write_trylock(raw_rwlock_t *lock)
  127. {
  128. atomic_t *count = (atomic_t *)lock;
  129. if (atomic_sub_and_test(RW_LOCK_BIAS, count))
  130. return 1;
  131. atomic_add(RW_LOCK_BIAS, count);
  132. return 0;
  133. }
  134. static inline void __raw_read_unlock(raw_rwlock_t *rw)
  135. {
  136. asm volatile(LOCK_PREFIX " ; incl %0" :"=m" (rw->lock) : : "memory");
  137. }
  138. static inline void __raw_write_unlock(raw_rwlock_t *rw)
  139. {
  140. asm volatile(LOCK_PREFIX " ; addl $" RW_LOCK_BIAS_STR ",%0"
  141. : "=m" (rw->lock) : : "memory");
  142. }
  143. #define _raw_spin_relax(lock) cpu_relax()
  144. #define _raw_read_relax(lock) cpu_relax()
  145. #define _raw_write_relax(lock) cpu_relax()
  146. #endif /* __ASM_SPINLOCK_H */