spinlock.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * A stand-alone ticket spinlock implementation for use by the non-VHE
  4. * KVM hypervisor code running at EL2.
  5. *
  6. * Copyright (C) 2020 Google LLC
  7. * Author: Will Deacon <will@kernel.org>
  8. *
  9. * Heavily based on the implementation removed by c11090474d70 which was:
  10. * Copyright (C) 2012 ARM Ltd.
  11. */
  12. #ifndef __ARM64_KVM_NVHE_SPINLOCK_H__
  13. #define __ARM64_KVM_NVHE_SPINLOCK_H__
  14. #include <asm/alternative.h>
  15. #include <asm/lse.h>
  16. #include <asm/rwonce.h>
  17. typedef union hyp_spinlock {
  18. u32 __val;
  19. struct {
  20. #ifdef __AARCH64EB__
  21. u16 next, owner;
  22. #else
  23. u16 owner, next;
  24. #endif
  25. };
  26. } hyp_spinlock_t;
  27. #define __HYP_SPIN_LOCK_INITIALIZER \
  28. { .__val = 0 }
  29. #define __HYP_SPIN_LOCK_UNLOCKED \
  30. ((hyp_spinlock_t) __HYP_SPIN_LOCK_INITIALIZER)
  31. #define DEFINE_HYP_SPINLOCK(x) hyp_spinlock_t x = __HYP_SPIN_LOCK_UNLOCKED
  32. #define hyp_spin_lock_init(l) \
  33. do { \
  34. *(l) = __HYP_SPIN_LOCK_UNLOCKED; \
  35. } while (0)
  36. static inline void hyp_spin_lock(hyp_spinlock_t *lock)
  37. {
  38. u32 tmp;
  39. hyp_spinlock_t lockval, newval;
  40. asm volatile(
  41. /* Atomically increment the next ticket. */
  42. ARM64_LSE_ATOMIC_INSN(
  43. /* LL/SC */
  44. " prfm pstl1strm, %3\n"
  45. "1: ldaxr %w0, %3\n"
  46. " add %w1, %w0, #(1 << 16)\n"
  47. " stxr %w2, %w1, %3\n"
  48. " cbnz %w2, 1b\n",
  49. /* LSE atomics */
  50. " mov %w2, #(1 << 16)\n"
  51. " ldadda %w2, %w0, %3\n"
  52. __nops(3))
  53. /* Did we get the lock? */
  54. " eor %w1, %w0, %w0, ror #16\n"
  55. " cbz %w1, 3f\n"
  56. /*
  57. * No: spin on the owner. Send a local event to avoid missing an
  58. * unlock before the exclusive load.
  59. */
  60. " sevl\n"
  61. "2: wfe\n"
  62. " ldaxrh %w2, %4\n"
  63. " eor %w1, %w2, %w0, lsr #16\n"
  64. " cbnz %w1, 2b\n"
  65. /* We got the lock. Critical section starts here. */
  66. "3:"
  67. : "=&r" (lockval), "=&r" (newval), "=&r" (tmp), "+Q" (*lock)
  68. : "Q" (lock->owner)
  69. : "memory");
  70. }
  71. static inline void hyp_spin_unlock(hyp_spinlock_t *lock)
  72. {
  73. u64 tmp;
  74. asm volatile(
  75. ARM64_LSE_ATOMIC_INSN(
  76. /* LL/SC */
  77. " ldrh %w1, %0\n"
  78. " add %w1, %w1, #1\n"
  79. " stlrh %w1, %0",
  80. /* LSE atomics */
  81. " mov %w1, #1\n"
  82. " staddlh %w1, %0\n"
  83. __nops(1))
  84. : "=Q" (lock->owner), "=&r" (tmp)
  85. :
  86. : "memory");
  87. }
  88. static inline bool hyp_spin_is_locked(hyp_spinlock_t *lock)
  89. {
  90. hyp_spinlock_t lockval = READ_ONCE(*lock);
  91. return lockval.owner != lockval.next;
  92. }
  93. #ifdef CONFIG_NVHE_EL2_DEBUG
  94. static inline void hyp_assert_lock_held(hyp_spinlock_t *lock)
  95. {
  96. /*
  97. * The __pkvm_init() path accesses protected data-structures without
  98. * holding locks as the other CPUs are guaranteed to not enter EL2
  99. * concurrently at this point in time. The point by which EL2 is
  100. * initialized on all CPUs is reflected in the pkvm static key, so
  101. * wait until it is set before checking the lock state.
  102. */
  103. if (static_branch_likely(&kvm_protected_mode_initialized))
  104. BUG_ON(!hyp_spin_is_locked(lock));
  105. }
  106. #else
  107. static inline void hyp_assert_lock_held(hyp_spinlock_t *lock) { }
  108. #endif
  109. #endif /* __ARM64_KVM_NVHE_SPINLOCK_H__ */