spinlock.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ASM_SPINLOCK_H
  3. #define __ASM_SPINLOCK_H
  4. #include <asm/barrier.h>
  5. #include <asm/ldcw.h>
  6. #include <asm/processor.h>
  7. #include <asm/spinlock_types.h>
  8. static inline void arch_spin_val_check(int lock_val)
  9. {
  10. if (IS_ENABLED(CONFIG_LIGHTWEIGHT_SPINLOCK_CHECK))
  11. asm volatile( "andcm,= %0,%1,%%r0\n"
  12. ".word %2\n"
  13. : : "r" (lock_val), "r" (__ARCH_SPIN_LOCK_UNLOCKED_VAL),
  14. "i" (SPINLOCK_BREAK_INSN));
  15. }
  16. static inline int arch_spin_is_locked(arch_spinlock_t *x)
  17. {
  18. volatile unsigned int *a;
  19. int lock_val;
  20. a = __ldcw_align(x);
  21. lock_val = READ_ONCE(*a);
  22. arch_spin_val_check(lock_val);
  23. return (lock_val == 0);
  24. }
  25. static inline void arch_spin_lock(arch_spinlock_t *x)
  26. {
  27. volatile unsigned int *a;
  28. a = __ldcw_align(x);
  29. do {
  30. int lock_val_old;
  31. lock_val_old = __ldcw(a);
  32. arch_spin_val_check(lock_val_old);
  33. if (lock_val_old)
  34. return; /* got lock */
  35. /* wait until we should try to get lock again */
  36. while (*a == 0)
  37. continue;
  38. } while (1);
  39. }
  40. static inline void arch_spin_unlock(arch_spinlock_t *x)
  41. {
  42. volatile unsigned int *a;
  43. a = __ldcw_align(x);
  44. /* Release with ordered store. */
  45. __asm__ __volatile__("stw,ma %0,0(%1)"
  46. : : "r"(__ARCH_SPIN_LOCK_UNLOCKED_VAL), "r"(a) : "memory");
  47. }
  48. static inline int arch_spin_trylock(arch_spinlock_t *x)
  49. {
  50. volatile unsigned int *a;
  51. int lock_val;
  52. a = __ldcw_align(x);
  53. lock_val = __ldcw(a);
  54. arch_spin_val_check(lock_val);
  55. return lock_val != 0;
  56. }
  57. /*
  58. * Read-write spinlocks, allowing multiple readers but only one writer.
  59. * Unfair locking as Writers could be starved indefinitely by Reader(s)
  60. *
  61. * The spinlock itself is contained in @counter and access to it is
  62. * serialized with @lock_mutex.
  63. */
  64. /* 1 - lock taken successfully */
  65. static inline int arch_read_trylock(arch_rwlock_t *rw)
  66. {
  67. int ret = 0;
  68. unsigned long flags;
  69. local_irq_save(flags);
  70. arch_spin_lock(&(rw->lock_mutex));
  71. /*
  72. * zero means writer holds the lock exclusively, deny Reader.
  73. * Otherwise grant lock to first/subseq reader
  74. */
  75. if (rw->counter > 0) {
  76. rw->counter--;
  77. ret = 1;
  78. }
  79. arch_spin_unlock(&(rw->lock_mutex));
  80. local_irq_restore(flags);
  81. return ret;
  82. }
  83. /* 1 - lock taken successfully */
  84. static inline int arch_write_trylock(arch_rwlock_t *rw)
  85. {
  86. int ret = 0;
  87. unsigned long flags;
  88. local_irq_save(flags);
  89. arch_spin_lock(&(rw->lock_mutex));
  90. /*
  91. * If reader(s) hold lock (lock < __ARCH_RW_LOCK_UNLOCKED__),
  92. * deny writer. Otherwise if unlocked grant to writer
  93. * Hence the claim that Linux rwlocks are unfair to writers.
  94. * (can be starved for an indefinite time by readers).
  95. */
  96. if (rw->counter == __ARCH_RW_LOCK_UNLOCKED__) {
  97. rw->counter = 0;
  98. ret = 1;
  99. }
  100. arch_spin_unlock(&(rw->lock_mutex));
  101. local_irq_restore(flags);
  102. return ret;
  103. }
  104. static inline void arch_read_lock(arch_rwlock_t *rw)
  105. {
  106. while (!arch_read_trylock(rw))
  107. cpu_relax();
  108. }
  109. static inline void arch_write_lock(arch_rwlock_t *rw)
  110. {
  111. while (!arch_write_trylock(rw))
  112. cpu_relax();
  113. }
  114. static inline void arch_read_unlock(arch_rwlock_t *rw)
  115. {
  116. unsigned long flags;
  117. local_irq_save(flags);
  118. arch_spin_lock(&(rw->lock_mutex));
  119. rw->counter++;
  120. arch_spin_unlock(&(rw->lock_mutex));
  121. local_irq_restore(flags);
  122. }
  123. static inline void arch_write_unlock(arch_rwlock_t *rw)
  124. {
  125. unsigned long flags;
  126. local_irq_save(flags);
  127. arch_spin_lock(&(rw->lock_mutex));
  128. rw->counter = __ARCH_RW_LOCK_UNLOCKED__;
  129. arch_spin_unlock(&(rw->lock_mutex));
  130. local_irq_restore(flags);
  131. }
  132. #endif /* __ASM_SPINLOCK_H */