spinlock.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * S390 version
  4. * Copyright IBM Corp. 1999
  5. * Author(s): Martin Schwidefsky ([email protected])
  6. *
  7. * Derived from "include/asm-i386/spinlock.h"
  8. */
  9. #ifndef __ASM_SPINLOCK_H
  10. #define __ASM_SPINLOCK_H
  11. #include <linux/smp.h>
  12. #include <asm/atomic_ops.h>
  13. #include <asm/barrier.h>
  14. #include <asm/processor.h>
  15. #include <asm/alternative.h>
  16. #define SPINLOCK_LOCKVAL (S390_lowcore.spinlock_lockval)
  17. extern int spin_retry;
  18. bool arch_vcpu_is_preempted(int cpu);
  19. #define vcpu_is_preempted arch_vcpu_is_preempted
  20. /*
  21. * Simple spin lock operations. There are two variants, one clears IRQ's
  22. * on the local processor, one does not.
  23. *
  24. * We make no fairness assumptions. They have a cost.
  25. *
  26. * (the type definitions are in asm/spinlock_types.h)
  27. */
  28. void arch_spin_relax(arch_spinlock_t *lock);
  29. #define arch_spin_relax arch_spin_relax
  30. void arch_spin_lock_wait(arch_spinlock_t *);
  31. int arch_spin_trylock_retry(arch_spinlock_t *);
  32. void arch_spin_lock_setup(int cpu);
  33. static inline u32 arch_spin_lockval(int cpu)
  34. {
  35. return cpu + 1;
  36. }
  37. static inline int arch_spin_value_unlocked(arch_spinlock_t lock)
  38. {
  39. return lock.lock == 0;
  40. }
  41. static inline int arch_spin_is_locked(arch_spinlock_t *lp)
  42. {
  43. return READ_ONCE(lp->lock) != 0;
  44. }
  45. static inline int arch_spin_trylock_once(arch_spinlock_t *lp)
  46. {
  47. barrier();
  48. return likely(__atomic_cmpxchg_bool(&lp->lock, 0, SPINLOCK_LOCKVAL));
  49. }
  50. static inline void arch_spin_lock(arch_spinlock_t *lp)
  51. {
  52. if (!arch_spin_trylock_once(lp))
  53. arch_spin_lock_wait(lp);
  54. }
  55. static inline int arch_spin_trylock(arch_spinlock_t *lp)
  56. {
  57. if (!arch_spin_trylock_once(lp))
  58. return arch_spin_trylock_retry(lp);
  59. return 1;
  60. }
  61. static inline void arch_spin_unlock(arch_spinlock_t *lp)
  62. {
  63. typecheck(int, lp->lock);
  64. kcsan_release();
  65. asm_inline volatile(
  66. ALTERNATIVE("nop", ".insn rre,0xb2fa0000,7,0", 49) /* NIAI 7 */
  67. " sth %1,%0\n"
  68. : "=R" (((unsigned short *) &lp->lock)[1])
  69. : "d" (0) : "cc", "memory");
  70. }
  71. /*
  72. * Read-write spinlocks, allowing multiple readers
  73. * but only one writer.
  74. *
  75. * NOTE! it is quite common to have readers in interrupts
  76. * but no interrupt writers. For those circumstances we
  77. * can "mix" irq-safe locks - any writer needs to get a
  78. * irq-safe write-lock, but readers can get non-irqsafe
  79. * read-locks.
  80. */
  81. #define arch_read_relax(rw) barrier()
  82. #define arch_write_relax(rw) barrier()
  83. void arch_read_lock_wait(arch_rwlock_t *lp);
  84. void arch_write_lock_wait(arch_rwlock_t *lp);
  85. static inline void arch_read_lock(arch_rwlock_t *rw)
  86. {
  87. int old;
  88. old = __atomic_add(1, &rw->cnts);
  89. if (old & 0xffff0000)
  90. arch_read_lock_wait(rw);
  91. }
  92. static inline void arch_read_unlock(arch_rwlock_t *rw)
  93. {
  94. __atomic_add_const_barrier(-1, &rw->cnts);
  95. }
  96. static inline void arch_write_lock(arch_rwlock_t *rw)
  97. {
  98. if (!__atomic_cmpxchg_bool(&rw->cnts, 0, 0x30000))
  99. arch_write_lock_wait(rw);
  100. }
  101. static inline void arch_write_unlock(arch_rwlock_t *rw)
  102. {
  103. __atomic_add_barrier(-0x30000, &rw->cnts);
  104. }
  105. static inline int arch_read_trylock(arch_rwlock_t *rw)
  106. {
  107. int old;
  108. old = READ_ONCE(rw->cnts);
  109. return (!(old & 0xffff0000) &&
  110. __atomic_cmpxchg_bool(&rw->cnts, old, old + 1));
  111. }
  112. static inline int arch_write_trylock(arch_rwlock_t *rw)
  113. {
  114. int old;
  115. old = READ_ONCE(rw->cnts);
  116. return !old && __atomic_cmpxchg_bool(&rw->cnts, 0, 0x30000);
  117. }
  118. #endif /* __ASM_SPINLOCK_H */