spinlock_64.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. /* Shifts and masks for the various fields in "lock". */
  20. #define __ARCH_SPIN_CURRENT_SHIFT 17
  21. #define __ARCH_SPIN_NEXT_MASK 0x7fff
  22. #define __ARCH_SPIN_NEXT_OVERFLOW 0x8000
  23. /*
  24. * Return the "current" portion of a ticket lock value,
  25. * i.e. the number that currently owns the lock.
  26. */
  27. static inline int arch_spin_current(u32 val)
  28. {
  29. return val >> __ARCH_SPIN_CURRENT_SHIFT;
  30. }
  31. /*
  32. * Return the "next" portion of a ticket lock value,
  33. * i.e. the number that the next task to try to acquire the lock will get.
  34. */
  35. static inline int arch_spin_next(u32 val)
  36. {
  37. return val & __ARCH_SPIN_NEXT_MASK;
  38. }
  39. /* The lock is locked if a task would have to wait to get it. */
  40. static inline int arch_spin_is_locked(arch_spinlock_t *lock)
  41. {
  42. u32 val = lock->lock;
  43. return arch_spin_current(val) != arch_spin_next(val);
  44. }
  45. /* Bump the current ticket so the next task owns the lock. */
  46. static inline void arch_spin_unlock(arch_spinlock_t *lock)
  47. {
  48. wmb(); /* guarantee anything modified under the lock is visible */
  49. __insn_fetchadd4(&lock->lock, 1U << __ARCH_SPIN_CURRENT_SHIFT);
  50. }
  51. void arch_spin_unlock_wait(arch_spinlock_t *lock);
  52. void arch_spin_lock_slow(arch_spinlock_t *lock, u32 val);
  53. /* Grab the "next" ticket number and bump it atomically.
  54. * If the current ticket is not ours, go to the slow path.
  55. * We also take the slow path if the "next" value overflows.
  56. */
  57. static inline void arch_spin_lock(arch_spinlock_t *lock)
  58. {
  59. u32 val = __insn_fetchadd4(&lock->lock, 1);
  60. u32 ticket = val & (__ARCH_SPIN_NEXT_MASK | __ARCH_SPIN_NEXT_OVERFLOW);
  61. if (unlikely(arch_spin_current(val) != ticket))
  62. arch_spin_lock_slow(lock, ticket);
  63. }
  64. /* Try to get the lock, and return whether we succeeded. */
  65. int arch_spin_trylock(arch_spinlock_t *lock);
  66. /* We cannot take an interrupt after getting a ticket, so don't enable them. */
  67. #define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
  68. /*
  69. * Read-write spinlocks, allowing multiple readers
  70. * but only one writer.
  71. *
  72. * We use fetchadd() for readers, and fetchor() with the sign bit
  73. * for writers.
  74. */
  75. #define __WRITE_LOCK_BIT (1 << 31)
  76. static inline int arch_write_val_locked(int val)
  77. {
  78. return val < 0; /* Optimize "val & __WRITE_LOCK_BIT". */
  79. }
  80. /**
  81. * read_can_lock - would read_trylock() succeed?
  82. * @lock: the rwlock in question.
  83. */
  84. static inline int arch_read_can_lock(arch_rwlock_t *rw)
  85. {
  86. return !arch_write_val_locked(rw->lock);
  87. }
  88. /**
  89. * write_can_lock - would write_trylock() succeed?
  90. * @lock: the rwlock in question.
  91. */
  92. static inline int arch_write_can_lock(arch_rwlock_t *rw)
  93. {
  94. return rw->lock == 0;
  95. }
  96. extern void __read_lock_failed(arch_rwlock_t *rw);
  97. static inline void arch_read_lock(arch_rwlock_t *rw)
  98. {
  99. u32 val = __insn_fetchaddgez4(&rw->lock, 1);
  100. if (unlikely(arch_write_val_locked(val)))
  101. __read_lock_failed(rw);
  102. }
  103. extern void __write_lock_failed(arch_rwlock_t *rw, u32 val);
  104. static inline void arch_write_lock(arch_rwlock_t *rw)
  105. {
  106. u32 val = __insn_fetchor4(&rw->lock, __WRITE_LOCK_BIT);
  107. if (unlikely(val != 0))
  108. __write_lock_failed(rw, val);
  109. }
  110. static inline void arch_read_unlock(arch_rwlock_t *rw)
  111. {
  112. __insn_mf();
  113. __insn_fetchadd4(&rw->lock, -1);
  114. }
  115. static inline void arch_write_unlock(arch_rwlock_t *rw)
  116. {
  117. __insn_mf();
  118. __insn_exch4(&rw->lock, 0); /* Avoid waiting in the write buffer. */
  119. }
  120. static inline int arch_read_trylock(arch_rwlock_t *rw)
  121. {
  122. return !arch_write_val_locked(__insn_fetchaddgez4(&rw->lock, 1));
  123. }
  124. static inline int arch_write_trylock(arch_rwlock_t *rw)
  125. {
  126. u32 val = __insn_fetchor4(&rw->lock, __WRITE_LOCK_BIT);
  127. if (likely(val == 0))
  128. return 1;
  129. if (!arch_write_val_locked(val))
  130. __insn_fetchand4(&rw->lock, ~__WRITE_LOCK_BIT);
  131. return 0;
  132. }
  133. #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
  134. #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
  135. #endif /* _ASM_TILE_SPINLOCK_64_H */