blob: 055c60a057567ff655be0157c4756e4dd36dbccc [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _ASM_X86_QSPINLOCK_H
3#define _ASM_X86_QSPINLOCK_H
4
5#include <linux/jump_label.h>
6#include <asm/cpufeature.h>
7#include <asm-generic/qspinlock_types.h>
8#include <asm/paravirt.h>
9#include <asm/rmwcc.h>
10
11#define _Q_PENDING_LOOPS (1 << 9)
12
13#define queued_fetch_set_pending_acquire queued_fetch_set_pending_acquire
14
15static __always_inline bool __queued_RMW_btsl(struct qspinlock *lock)
16{
17 GEN_BINARY_RMWcc(LOCK_PREFIX "btsl", lock->val.counter,
18 "I", _Q_PENDING_OFFSET, "%0", c);
19}
20
21static __always_inline u32 queued_fetch_set_pending_acquire(struct qspinlock *lock)
22{
23 u32 val = 0;
24
25 if (__queued_RMW_btsl(lock))
26 val |= _Q_PENDING_VAL;
27
28 val |= atomic_read(&lock->val) & ~_Q_PENDING_MASK;
29
30 return val;
31}
32
33#ifdef CONFIG_PARAVIRT_SPINLOCKS
34extern void native_queued_spin_lock_slowpath(struct qspinlock *lock, u32 val);
35extern void __pv_init_lock_hash(void);
36extern void __pv_queued_spin_lock_slowpath(struct qspinlock *lock, u32 val);
37extern void __raw_callee_save___pv_queued_spin_unlock(struct qspinlock *lock);
38
39#define queued_spin_unlock queued_spin_unlock
40/**
41 * queued_spin_unlock - release a queued spinlock
42 * @lock : Pointer to queued spinlock structure
43 *
44 * A smp_store_release() on the least-significant byte.
45 */
46static inline void native_queued_spin_unlock(struct qspinlock *lock)
47{
48 smp_store_release(&lock->locked, 0);
49}
50
51static inline void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
52{
53 pv_queued_spin_lock_slowpath(lock, val);
54}
55
56static inline void queued_spin_unlock(struct qspinlock *lock)
57{
58 pv_queued_spin_unlock(lock);
59}
60
61#define vcpu_is_preempted vcpu_is_preempted
62static inline bool vcpu_is_preempted(long cpu)
63{
64 return pv_vcpu_is_preempted(cpu);
65}
66#endif
67
68#ifdef CONFIG_PARAVIRT
69DECLARE_STATIC_KEY_TRUE(virt_spin_lock_key);
70
71void native_pv_lock_init(void) __init;
72
73#define virt_spin_lock virt_spin_lock
74static inline bool virt_spin_lock(struct qspinlock *lock)
75{
76 if (!static_branch_likely(&virt_spin_lock_key))
77 return false;
78
79 /*
80 * On hypervisors without PARAVIRT_SPINLOCKS support we fall
81 * back to a Test-and-Set spinlock, because fair locks have
82 * horrible lock 'holder' preemption issues.
83 */
84
85 do {
86 while (atomic_read(&lock->val) != 0)
87 cpu_relax();
88 } while (atomic_cmpxchg(&lock->val, 0, _Q_LOCKED_VAL) != 0);
89
90 return true;
91}
92#else
93static inline void native_pv_lock_init(void)
94{
95}
96#endif /* CONFIG_PARAVIRT */
97
98#include <asm-generic/qspinlock.h>
99
100#endif /* _ASM_X86_QSPINLOCK_H */