1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * Queued spinlock 4 * 5 * (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P. 6 * (C) Copyright 2015 Hewlett-Packard Enterprise Development LP 7 * 8 * Authors: Waiman Long <waiman.long@hpe.com> 9 */ 10 #ifndef __ASM_GENERIC_QSPINLOCK_H 11 #define __ASM_GENERIC_QSPINLOCK_H 12 13 #include <asm-generic/qspinlock_types.h> 14 15 #ifndef queued_spin_is_locked 16 /** 17 * queued_spin_is_locked - is the spinlock locked? 18 * @lock: Pointer to queued spinlock structure 19 * Return: 1 if it is locked, 0 otherwise 20 */ 21 static __always_inline int queued_spin_is_locked(struct qspinlock *lock) 22 { 23 /* 24 * Any !0 state indicates it is locked, even if _Q_LOCKED_VAL 25 * isn't immediately observable. 26 */ 27 return atomic_read(&lock->val); 28 } 29 #endif 30 31 /** 32 * queued_spin_value_unlocked - is the spinlock structure unlocked? 33 * @lock: queued spinlock structure 34 * Return: 1 if it is unlocked, 0 otherwise 35 * 36 * N.B. Whenever there are tasks waiting for the lock, it is considered 37 * locked wrt the lockref code to avoid lock stealing by the lockref 38 * code and change things underneath the lock. This also allows some 39 * optimizations to be applied without conflict with lockref. 40 */ 41 static __always_inline int queued_spin_value_unlocked(struct qspinlock lock) 42 { 43 return !atomic_read(&lock.val); 44 } 45 46 /** 47 * queued_spin_is_contended - check if the lock is contended 48 * @lock : Pointer to queued spinlock structure 49 * Return: 1 if lock contended, 0 otherwise 50 */ 51 static __always_inline int queued_spin_is_contended(struct qspinlock *lock) 52 { 53 return atomic_read(&lock->val) & ~_Q_LOCKED_MASK; 54 } 55 /** 56 * queued_spin_trylock - try to acquire the queued spinlock 57 * @lock : Pointer to queued spinlock structure 58 * Return: 1 if lock acquired, 0 if failed 59 */ 60 static __always_inline int queued_spin_trylock(struct qspinlock *lock) 61 { 62 u32 val = atomic_read(&lock->val); 63 64 if (unlikely(val)) 65 return 0; 66 67 return likely(atomic_try_cmpxchg_acquire(&lock->val, &val, _Q_LOCKED_VAL)); 68 } 69 70 extern void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val); 71 72 #ifndef queued_spin_lock 73 /** 74 * queued_spin_lock - acquire a queued spinlock 75 * @lock: Pointer to queued spinlock structure 76 */ 77 static __always_inline void queued_spin_lock(struct qspinlock *lock) 78 { 79 u32 val = 0; 80 81 if (likely(atomic_try_cmpxchg_acquire(&lock->val, &val, _Q_LOCKED_VAL))) 82 return; 83 84 queued_spin_lock_slowpath(lock, val); 85 } 86 #endif 87 88 #ifndef queued_spin_unlock 89 /** 90 * queued_spin_unlock - release a queued spinlock 91 * @lock : Pointer to queued spinlock structure 92 */ 93 static __always_inline void queued_spin_unlock(struct qspinlock *lock) 94 { 95 /* 96 * unlock() needs release semantics: 97 */ 98 smp_store_release(&lock->locked, 0); 99 } 100 #endif 101 102 #ifndef virt_spin_lock 103 static __always_inline bool virt_spin_lock(struct qspinlock *lock) 104 { 105 return false; 106 } 107 #endif 108 109 /* 110 * Remapping spinlock architecture specific functions to the corresponding 111 * queued spinlock functions. 112 */ 113 #define arch_spin_is_locked(l) queued_spin_is_locked(l) 114 #define arch_spin_is_contended(l) queued_spin_is_contended(l) 115 #define arch_spin_value_unlocked(l) queued_spin_value_unlocked(l) 116 #define arch_spin_lock(l) queued_spin_lock(l) 117 #define arch_spin_trylock(l) queued_spin_trylock(l) 118 #define arch_spin_unlock(l) queued_spin_unlock(l) 119 120 #endif /* __ASM_GENERIC_QSPINLOCK_H */ 121