spinlock_64.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright 2011 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. *
  14. * 64-bit SMP ticket spinlocks, allowing only a single CPU anywhere
  15. * (the type definitions are in asm/spinlock_types.h)
  16. */
  17. #ifndef _ASM_TILE_SPINLOCK_64_H
  18. #define _ASM_TILE_SPINLOCK_64_H
  19. #include <linux/compiler.h>
  20. /* Shifts and masks for the various fields in "lock". */
  21. #define __ARCH_SPIN_CURRENT_SHIFT 17
  22. #define __ARCH_SPIN_NEXT_MASK 0x7fff
  23. #define __ARCH_SPIN_NEXT_OVERFLOW 0x8000
  24. /*
  25. * Return the "current" portion of a ticket lock value,
  26. * i.e. the number that currently owns the lock.
  27. */
  28. static inline u32 arch_spin_current(u32 val)
  29. {
  30. return val >> __ARCH_SPIN_CURRENT_SHIFT;
  31. }
  32. /*
  33. * Return the "next" portion of a ticket lock value,
  34. * i.e. the number that the next task to try to acquire the lock will get.
  35. */
  36. static inline u32 arch_spin_next(u32 val)
  37. {
  38. return val & __ARCH_SPIN_NEXT_MASK;
  39. }
  40. /* The lock is locked if a task would have to wait to get it. */
  41. static inline int arch_spin_is_locked(arch_spinlock_t *lock)
  42. {
  43. /* Use READ_ONCE() to ensure that calling this in a loop is OK. */
  44. u32 val = READ_ONCE(lock->lock);
  45. return arch_spin_current(val) != arch_spin_next(val);
  46. }
  47. /* Bump the current ticket so the next task owns the lock. */
  48. static inline void arch_spin_unlock(arch_spinlock_t *lock)
  49. {
  50. wmb(); /* guarantee anything modified under the lock is visible */
  51. __insn_fetchadd4(&lock->lock, 1U << __ARCH_SPIN_CURRENT_SHIFT);
  52. }
  53. void arch_spin_unlock_wait(arch_spinlock_t *lock);
  54. void arch_spin_lock_slow(arch_spinlock_t *lock, u32 val);
  55. /* Grab the "next" ticket number and bump it atomically.
  56. * If the current ticket is not ours, go to the slow path.
  57. * We also take the slow path if the "next" value overflows.
  58. */
  59. static inline void arch_spin_lock(arch_spinlock_t *lock)
  60. {
  61. u32 val = __insn_fetchadd4(&lock->lock, 1);
  62. u32 ticket = val & (__ARCH_SPIN_NEXT_MASK | __ARCH_SPIN_NEXT_OVERFLOW);
  63. if (unlikely(arch_spin_current(val) != ticket))
  64. arch_spin_lock_slow(lock, ticket);
  65. }
  66. /* Try to get the lock, and return whether we succeeded. */
  67. int arch_spin_trylock(arch_spinlock_t *lock);
  68. /* We cannot take an interrupt after getting a ticket, so don't enable them. */
  69. #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
  70. /*
  71. * Read-write spinlocks, allowing multiple readers
  72. * but only one writer.
  73. *
  74. * We use fetchadd() for readers, and fetchor() with the sign bit
  75. * for writers.
  76. */
  77. #define __WRITE_LOCK_BIT (1 << 31)
  78. static inline int arch_write_val_locked(int val)
  79. {
  80. return val < 0; /* Optimize "val & __WRITE_LOCK_BIT". */
  81. }
  82. /**
  83. * read_can_lock - would read_trylock() succeed?
  84. * @lock: the rwlock in question.
  85. */
  86. static inline int arch_read_can_lock(arch_rwlock_t *rw)
  87. {
  88. return !arch_write_val_locked(rw->lock);
  89. }
  90. /**
  91. * write_can_lock - would write_trylock() succeed?
  92. * @lock: the rwlock in question.
  93. */
  94. static inline int arch_write_can_lock(arch_rwlock_t *rw)
  95. {
  96. return rw->lock == 0;
  97. }
  98. extern void __read_lock_failed(arch_rwlock_t *rw);
  99. static inline void arch_read_lock(arch_rwlock_t *rw)
  100. {
  101. u32 val = __insn_fetchaddgez4(&rw->lock, 1);
  102. if (unlikely(arch_write_val_locked(val)))
  103. __read_lock_failed(rw);
  104. }
  105. extern void __write_lock_failed(arch_rwlock_t *rw, u32 val);
  106. static inline void arch_write_lock(arch_rwlock_t *rw)
  107. {
  108. u32 val = __insn_fetchor4(&rw->lock, __WRITE_LOCK_BIT);
  109. if (unlikely(val != 0))
  110. __write_lock_failed(rw, val);
  111. }
  112. static inline void arch_read_unlock(arch_rwlock_t *rw)
  113. {
  114. __insn_mf();
  115. __insn_fetchadd4(&rw->lock, -1);
  116. }
  117. static inline void arch_write_unlock(arch_rwlock_t *rw)
  118. {
  119. __insn_mf();
  120. __insn_exch4(&rw->lock, 0); /* Avoid waiting in the write buffer. */
  121. }
  122. static inline int arch_read_trylock(arch_rwlock_t *rw)
  123. {
  124. return !arch_write_val_locked(__insn_fetchaddgez4(&rw->lock, 1));
  125. }
  126. static inline int arch_write_trylock(arch_rwlock_t *rw)
  127. {
  128. u32 val = __insn_fetchor4(&rw->lock, __WRITE_LOCK_BIT);
  129. if (likely(val == 0))
  130. return 1;
  131. if (!arch_write_val_locked(val))
  132. __insn_fetchand4(&rw->lock, ~__WRITE_LOCK_BIT);
  133. return 0;
  134. }
  135. #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
  136. #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
  137. #endif /* _ASM_TILE_SPINLOCK_64_H */