1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_QSPINLOCK_H
3 #define _ASM_X86_QSPINLOCK_H
5 #include <linux/jump_label.h>
6 #include <asm/cpufeature.h>
7 #include <asm-generic/qspinlock_types.h>
8 #include <asm/paravirt.h>
11 #define _Q_PENDING_LOOPS (1 << 9)
13 #define queued_fetch_set_pending_acquire queued_fetch_set_pending_acquire
14 static __always_inline u32 queued_fetch_set_pending_acquire(struct qspinlock *lock)
19 * We can't use GEN_BINARY_RMWcc() inside an if() stmt because asm goto
20 * and CONFIG_PROFILE_ALL_BRANCHES=y results in a label inside a
21 * statement expression, which GCC doesn't like.
23 val = GEN_BINARY_RMWcc(LOCK_PREFIX "btsl", lock->val.counter, c,
24 "I", _Q_PENDING_OFFSET) * _Q_PENDING_VAL;
25 val |= atomic_read(&lock->val) & ~_Q_PENDING_MASK;
30 #ifdef CONFIG_PARAVIRT_SPINLOCKS
31 extern void native_queued_spin_lock_slowpath(struct qspinlock *lock, u32 val);
32 extern void __pv_init_lock_hash(void);
33 extern void __pv_queued_spin_lock_slowpath(struct qspinlock *lock, u32 val);
34 extern void __raw_callee_save___pv_queued_spin_unlock(struct qspinlock *lock);
37 #define queued_spin_unlock queued_spin_unlock
39 * queued_spin_unlock - release a queued spinlock
40 * @lock : Pointer to queued spinlock structure
42 * A smp_store_release() on the least-significant byte.
44 static inline void native_queued_spin_unlock(struct qspinlock *lock)
46 smp_store_release(&lock->locked, 0);
49 static inline void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
51 pv_queued_spin_lock_slowpath(lock, val);
54 static inline void queued_spin_unlock(struct qspinlock *lock)
57 pv_queued_spin_unlock(lock);
60 #define vcpu_is_preempted vcpu_is_preempted
61 static inline bool vcpu_is_preempted(long cpu)
63 return pv_vcpu_is_preempted(cpu);
67 #ifdef CONFIG_PARAVIRT
69 * virt_spin_lock_key - enables (by default) the virt_spin_lock() hijack.
71 * Native (and PV wanting native due to vCPU pinning) should disable this key.
72 * It is done in this backwards fashion to only have a single direction change,
73 * which removes ordering between native_pv_spin_init() and HV setup.
75 DECLARE_STATIC_KEY_TRUE(virt_spin_lock_key);
77 void native_pv_lock_init(void) __init;
80 * Shortcut for the queued_spin_lock_slowpath() function that allows
84 * true - lock has been negotiated, all done;
85 * false - queued_spin_lock_slowpath() will do its thing.
87 #define virt_spin_lock virt_spin_lock
88 static inline bool virt_spin_lock(struct qspinlock *lock)
90 if (!static_branch_likely(&virt_spin_lock_key))
94 * On hypervisors without PARAVIRT_SPINLOCKS support we fall
95 * back to a Test-and-Set spinlock, because fair locks have
96 * horrible lock 'holder' preemption issues.
100 while (atomic_read(&lock->val) != 0)
102 } while (atomic_cmpxchg(&lock->val, 0, _Q_LOCKED_VAL) != 0);
107 static inline void native_pv_lock_init(void)
110 #endif /* CONFIG_PARAVIRT */
112 #include <asm-generic/qspinlock.h>
114 #endif /* _ASM_X86_QSPINLOCK_H */