blob: 444d6fd9a6d8991bc152721720bb47fc3b258e46 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Waiman Longd73a3392015-04-24 14:56:31 -04002#ifndef _ASM_X86_QSPINLOCK_H
3#define _ASM_X86_QSPINLOCK_H
4
Juergen Gross90434422017-09-06 19:36:24 +02005#include <linux/jump_label.h>
Peter Zijlstra (Intel)2aa79af2015-04-24 14:56:36 -04006#include <asm/cpufeature.h>
Waiman Longd73a3392015-04-24 14:56:31 -04007#include <asm-generic/qspinlock_types.h>
Peter Zijlstra (Intel)f233f7f2015-04-24 14:56:38 -04008#include <asm/paravirt.h>
Peter Zijlstra7aa54be2018-09-26 13:01:20 +02009#include <asm/rmwcc.h>
Waiman Longd73a3392015-04-24 14:56:31 -040010
Will Deaconb247be32018-04-26 11:34:18 +010011#define _Q_PENDING_LOOPS (1 << 9)
12
Peter Zijlstra7aa54be2018-09-26 13:01:20 +020013#define queued_fetch_set_pending_acquire queued_fetch_set_pending_acquire
14static __always_inline u32 queued_fetch_set_pending_acquire(struct qspinlock *lock)
15{
Peter Zijlstrab987ffc2018-11-02 14:26:53 +010016 u32 val;
Peter Zijlstra7aa54be2018-09-26 13:01:20 +020017
Peter Zijlstrab987ffc2018-11-02 14:26:53 +010018 /*
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.
22 */
23 val = GEN_BINARY_RMWcc(LOCK_PREFIX "btsl", lock->val.counter, c,
24 "I", _Q_PENDING_OFFSET) * _Q_PENDING_VAL;
Peter Zijlstra7aa54be2018-09-26 13:01:20 +020025 val |= atomic_read(&lock->val) & ~_Q_PENDING_MASK;
26
27 return val;
28}
29
Will Deacon626e5fb2018-04-26 11:34:24 +010030#ifdef CONFIG_PARAVIRT_SPINLOCKS
31extern void native_queued_spin_lock_slowpath(struct qspinlock *lock, u32 val);
32extern void __pv_init_lock_hash(void);
33extern void __pv_queued_spin_lock_slowpath(struct qspinlock *lock, u32 val);
34extern void __raw_callee_save___pv_queued_spin_unlock(struct qspinlock *lock);
35
Waiman Longd73a3392015-04-24 14:56:31 -040036#define queued_spin_unlock queued_spin_unlock
37/**
38 * queued_spin_unlock - release a queued spinlock
39 * @lock : Pointer to queued spinlock structure
40 *
41 * A smp_store_release() on the least-significant byte.
42 */
Peter Zijlstra (Intel)f233f7f2015-04-24 14:56:38 -040043static inline void native_queued_spin_unlock(struct qspinlock *lock)
Waiman Longd73a3392015-04-24 14:56:31 -040044{
Will Deacon625e88b2018-04-26 11:34:16 +010045 smp_store_release(&lock->locked, 0);
Waiman Longd73a3392015-04-24 14:56:31 -040046}
47
Peter Zijlstra (Intel)f233f7f2015-04-24 14:56:38 -040048static inline void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
49{
50 pv_queued_spin_lock_slowpath(lock, val);
51}
52
53static inline void queued_spin_unlock(struct qspinlock *lock)
54{
55 pv_queued_spin_unlock(lock);
56}
Peter Zijlstra3cded412016-11-15 16:47:06 +010057
58#define vcpu_is_preempted vcpu_is_preempted
Waiman Long6c629852017-02-20 13:36:03 -050059static inline bool vcpu_is_preempted(long cpu)
Peter Zijlstra3cded412016-11-15 16:47:06 +010060{
61 return pv_vcpu_is_preempted(cpu);
62}
Peter Zijlstra (Intel)f233f7f2015-04-24 14:56:38 -040063#endif
64
Peter Zijlstraa6b27782015-09-05 16:55:05 +020065#ifdef CONFIG_PARAVIRT
Peter Zijlstra24a376d2019-08-01 15:30:28 +020066/*
67 * virt_spin_lock_key - enables (by default) the virt_spin_lock() hijack.
68 *
69 * Native (and PV wanting native due to vCPU pinning) should disable this key.
70 * It is done in this backwards fashion to only have a single direction change,
71 * which removes ordering between native_pv_spin_init() and HV setup.
72 */
Juergen Gross90434422017-09-06 19:36:24 +020073DECLARE_STATIC_KEY_TRUE(virt_spin_lock_key);
74
75void native_pv_lock_init(void) __init;
76
Peter Zijlstra24a376d2019-08-01 15:30:28 +020077/*
78 * Shortcut for the queued_spin_lock_slowpath() function that allows
79 * virt to hijack it.
80 *
81 * Returns:
82 * true - lock has been negotiated, all done;
83 * false - queued_spin_lock_slowpath() will do its thing.
84 */
Peter Zijlstra43b3f022015-09-04 17:25:23 +020085#define virt_spin_lock virt_spin_lock
Peter Zijlstra43b3f022015-09-04 17:25:23 +020086static inline bool virt_spin_lock(struct qspinlock *lock)
Peter Zijlstra (Intel)2aa79af2015-04-24 14:56:36 -040087{
Juergen Gross90434422017-09-06 19:36:24 +020088 if (!static_branch_likely(&virt_spin_lock_key))
Peter Zijlstra (Intel)2aa79af2015-04-24 14:56:36 -040089 return false;
90
Peter Zijlstra43b3f022015-09-04 17:25:23 +020091 /*
92 * On hypervisors without PARAVIRT_SPINLOCKS support we fall
93 * back to a Test-and-Set spinlock, because fair locks have
94 * horrible lock 'holder' preemption issues.
95 */
96
97 do {
98 while (atomic_read(&lock->val) != 0)
99 cpu_relax();
100 } while (atomic_cmpxchg(&lock->val, 0, _Q_LOCKED_VAL) != 0);
Peter Zijlstra (Intel)2aa79af2015-04-24 14:56:36 -0400101
102 return true;
103}
Juergen Gross90434422017-09-06 19:36:24 +0200104#else
105static inline void native_pv_lock_init(void)
106{
107}
Peter Zijlstraa6b27782015-09-05 16:55:05 +0200108#endif /* CONFIG_PARAVIRT */
Peter Zijlstra (Intel)2aa79af2015-04-24 14:56:36 -0400109
Waiman Longd73a3392015-04-24 14:56:31 -0400110#include <asm-generic/qspinlock.h>
111
112#endif /* _ASM_X86_QSPINLOCK_H */