Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Waiman Long | d73a339 | 2015-04-24 14:56:31 -0400 | [diff] [blame] | 2 | #ifndef _ASM_X86_QSPINLOCK_H |
| 3 | #define _ASM_X86_QSPINLOCK_H |
| 4 | |
Juergen Gross | 9043442 | 2017-09-06 19:36:24 +0200 | [diff] [blame] | 5 | #include <linux/jump_label.h> |
Peter Zijlstra (Intel) | 2aa79af | 2015-04-24 14:56:36 -0400 | [diff] [blame] | 6 | #include <asm/cpufeature.h> |
Waiman Long | d73a339 | 2015-04-24 14:56:31 -0400 | [diff] [blame] | 7 | #include <asm-generic/qspinlock_types.h> |
Peter Zijlstra (Intel) | f233f7f | 2015-04-24 14:56:38 -0400 | [diff] [blame] | 8 | #include <asm/paravirt.h> |
Peter Zijlstra | 7aa54be | 2018-09-26 13:01:20 +0200 | [diff] [blame] | 9 | #include <asm/rmwcc.h> |
Waiman Long | d73a339 | 2015-04-24 14:56:31 -0400 | [diff] [blame] | 10 | |
Will Deacon | b247be3 | 2018-04-26 11:34:18 +0100 | [diff] [blame] | 11 | #define _Q_PENDING_LOOPS (1 << 9) |
| 12 | |
Peter Zijlstra | 7aa54be | 2018-09-26 13:01:20 +0200 | [diff] [blame] | 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) |
| 15 | { |
Peter Zijlstra | b987ffc | 2018-11-02 14:26:53 +0100 | [diff] [blame] | 16 | u32 val; |
Peter Zijlstra | 7aa54be | 2018-09-26 13:01:20 +0200 | [diff] [blame] | 17 | |
Peter Zijlstra | b987ffc | 2018-11-02 14:26:53 +0100 | [diff] [blame] | 18 | /* |
| 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 Zijlstra | 7aa54be | 2018-09-26 13:01:20 +0200 | [diff] [blame] | 25 | val |= atomic_read(&lock->val) & ~_Q_PENDING_MASK; |
| 26 | |
| 27 | return val; |
| 28 | } |
| 29 | |
Will Deacon | 626e5fb | 2018-04-26 11:34:24 +0100 | [diff] [blame] | 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); |
Zhenzhong Duan | 05eee61 | 2019-10-23 19:16:22 +0800 | [diff] [blame] | 35 | extern bool nopvspin; |
Will Deacon | 626e5fb | 2018-04-26 11:34:24 +0100 | [diff] [blame] | 36 | |
Waiman Long | d73a339 | 2015-04-24 14:56:31 -0400 | [diff] [blame] | 37 | #define queued_spin_unlock queued_spin_unlock |
| 38 | /** |
| 39 | * queued_spin_unlock - release a queued spinlock |
| 40 | * @lock : Pointer to queued spinlock structure |
| 41 | * |
| 42 | * A smp_store_release() on the least-significant byte. |
| 43 | */ |
Peter Zijlstra (Intel) | f233f7f | 2015-04-24 14:56:38 -0400 | [diff] [blame] | 44 | static inline void native_queued_spin_unlock(struct qspinlock *lock) |
Waiman Long | d73a339 | 2015-04-24 14:56:31 -0400 | [diff] [blame] | 45 | { |
Will Deacon | 625e88b | 2018-04-26 11:34:16 +0100 | [diff] [blame] | 46 | smp_store_release(&lock->locked, 0); |
Waiman Long | d73a339 | 2015-04-24 14:56:31 -0400 | [diff] [blame] | 47 | } |
| 48 | |
Peter Zijlstra (Intel) | f233f7f | 2015-04-24 14:56:38 -0400 | [diff] [blame] | 49 | static inline void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) |
| 50 | { |
| 51 | pv_queued_spin_lock_slowpath(lock, val); |
| 52 | } |
| 53 | |
| 54 | static inline void queued_spin_unlock(struct qspinlock *lock) |
| 55 | { |
| 56 | pv_queued_spin_unlock(lock); |
| 57 | } |
Peter Zijlstra | 3cded41 | 2016-11-15 16:47:06 +0100 | [diff] [blame] | 58 | |
| 59 | #define vcpu_is_preempted vcpu_is_preempted |
Waiman Long | 6c62985 | 2017-02-20 13:36:03 -0500 | [diff] [blame] | 60 | static inline bool vcpu_is_preempted(long cpu) |
Peter Zijlstra | 3cded41 | 2016-11-15 16:47:06 +0100 | [diff] [blame] | 61 | { |
| 62 | return pv_vcpu_is_preempted(cpu); |
| 63 | } |
Peter Zijlstra (Intel) | f233f7f | 2015-04-24 14:56:38 -0400 | [diff] [blame] | 64 | #endif |
| 65 | |
Peter Zijlstra | a6b2778 | 2015-09-05 16:55:05 +0200 | [diff] [blame] | 66 | #ifdef CONFIG_PARAVIRT |
Peter Zijlstra | 24a376d | 2019-08-01 15:30:28 +0200 | [diff] [blame] | 67 | /* |
| 68 | * virt_spin_lock_key - enables (by default) the virt_spin_lock() hijack. |
| 69 | * |
| 70 | * Native (and PV wanting native due to vCPU pinning) should disable this key. |
| 71 | * It is done in this backwards fashion to only have a single direction change, |
| 72 | * which removes ordering between native_pv_spin_init() and HV setup. |
| 73 | */ |
Juergen Gross | 9043442 | 2017-09-06 19:36:24 +0200 | [diff] [blame] | 74 | DECLARE_STATIC_KEY_TRUE(virt_spin_lock_key); |
| 75 | |
| 76 | void native_pv_lock_init(void) __init; |
| 77 | |
Peter Zijlstra | 24a376d | 2019-08-01 15:30:28 +0200 | [diff] [blame] | 78 | /* |
| 79 | * Shortcut for the queued_spin_lock_slowpath() function that allows |
| 80 | * virt to hijack it. |
| 81 | * |
| 82 | * Returns: |
| 83 | * true - lock has been negotiated, all done; |
| 84 | * false - queued_spin_lock_slowpath() will do its thing. |
| 85 | */ |
Peter Zijlstra | 43b3f02 | 2015-09-04 17:25:23 +0200 | [diff] [blame] | 86 | #define virt_spin_lock virt_spin_lock |
Peter Zijlstra | 43b3f02 | 2015-09-04 17:25:23 +0200 | [diff] [blame] | 87 | static inline bool virt_spin_lock(struct qspinlock *lock) |
Peter Zijlstra (Intel) | 2aa79af | 2015-04-24 14:56:36 -0400 | [diff] [blame] | 88 | { |
Juergen Gross | 9043442 | 2017-09-06 19:36:24 +0200 | [diff] [blame] | 89 | if (!static_branch_likely(&virt_spin_lock_key)) |
Peter Zijlstra (Intel) | 2aa79af | 2015-04-24 14:56:36 -0400 | [diff] [blame] | 90 | return false; |
| 91 | |
Peter Zijlstra | 43b3f02 | 2015-09-04 17:25:23 +0200 | [diff] [blame] | 92 | /* |
| 93 | * On hypervisors without PARAVIRT_SPINLOCKS support we fall |
| 94 | * back to a Test-and-Set spinlock, because fair locks have |
| 95 | * horrible lock 'holder' preemption issues. |
| 96 | */ |
| 97 | |
| 98 | do { |
| 99 | while (atomic_read(&lock->val) != 0) |
| 100 | cpu_relax(); |
| 101 | } while (atomic_cmpxchg(&lock->val, 0, _Q_LOCKED_VAL) != 0); |
Peter Zijlstra (Intel) | 2aa79af | 2015-04-24 14:56:36 -0400 | [diff] [blame] | 102 | |
| 103 | return true; |
| 104 | } |
Juergen Gross | 9043442 | 2017-09-06 19:36:24 +0200 | [diff] [blame] | 105 | #else |
| 106 | static inline void native_pv_lock_init(void) |
| 107 | { |
| 108 | } |
Peter Zijlstra | a6b2778 | 2015-09-05 16:55:05 +0200 | [diff] [blame] | 109 | #endif /* CONFIG_PARAVIRT */ |
Peter Zijlstra (Intel) | 2aa79af | 2015-04-24 14:56:36 -0400 | [diff] [blame] | 110 | |
Waiman Long | d73a339 | 2015-04-24 14:56:31 -0400 | [diff] [blame] | 111 | #include <asm-generic/qspinlock.h> |
| 112 | |
| 113 | #endif /* _ASM_X86_QSPINLOCK_H */ |