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> |
Waiman Long | d73a339 | 2015-04-24 14:56:31 -0400 | [diff] [blame] | 9 | |
Will Deacon | b247be3 | 2018-04-26 11:34:18 +0100 | [diff] [blame] | 10 | #define _Q_PENDING_LOOPS (1 << 9) |
| 11 | |
Will Deacon | 626e5fb | 2018-04-26 11:34:24 +0100 | [diff] [blame] | 12 | #ifdef CONFIG_PARAVIRT_SPINLOCKS |
| 13 | extern void native_queued_spin_lock_slowpath(struct qspinlock *lock, u32 val); |
| 14 | extern void __pv_init_lock_hash(void); |
| 15 | extern void __pv_queued_spin_lock_slowpath(struct qspinlock *lock, u32 val); |
| 16 | extern void __raw_callee_save___pv_queued_spin_unlock(struct qspinlock *lock); |
| 17 | |
Waiman Long | d73a339 | 2015-04-24 14:56:31 -0400 | [diff] [blame] | 18 | #define queued_spin_unlock queued_spin_unlock |
| 19 | /** |
| 20 | * queued_spin_unlock - release a queued spinlock |
| 21 | * @lock : Pointer to queued spinlock structure |
| 22 | * |
| 23 | * A smp_store_release() on the least-significant byte. |
| 24 | */ |
Peter Zijlstra (Intel) | f233f7f | 2015-04-24 14:56:38 -0400 | [diff] [blame] | 25 | static inline void native_queued_spin_unlock(struct qspinlock *lock) |
Waiman Long | d73a339 | 2015-04-24 14:56:31 -0400 | [diff] [blame] | 26 | { |
Will Deacon | 625e88b | 2018-04-26 11:34:16 +0100 | [diff] [blame] | 27 | smp_store_release(&lock->locked, 0); |
Waiman Long | d73a339 | 2015-04-24 14:56:31 -0400 | [diff] [blame] | 28 | } |
| 29 | |
Peter Zijlstra (Intel) | f233f7f | 2015-04-24 14:56:38 -0400 | [diff] [blame] | 30 | static inline void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) |
| 31 | { |
| 32 | pv_queued_spin_lock_slowpath(lock, val); |
| 33 | } |
| 34 | |
| 35 | static inline void queued_spin_unlock(struct qspinlock *lock) |
| 36 | { |
| 37 | pv_queued_spin_unlock(lock); |
| 38 | } |
Peter Zijlstra | 3cded41 | 2016-11-15 16:47:06 +0100 | [diff] [blame] | 39 | |
| 40 | #define vcpu_is_preempted vcpu_is_preempted |
Waiman Long | 6c62985 | 2017-02-20 13:36:03 -0500 | [diff] [blame] | 41 | static inline bool vcpu_is_preempted(long cpu) |
Peter Zijlstra | 3cded41 | 2016-11-15 16:47:06 +0100 | [diff] [blame] | 42 | { |
| 43 | return pv_vcpu_is_preempted(cpu); |
| 44 | } |
Peter Zijlstra (Intel) | f233f7f | 2015-04-24 14:56:38 -0400 | [diff] [blame] | 45 | #endif |
| 46 | |
Peter Zijlstra | a6b2778 | 2015-09-05 16:55:05 +0200 | [diff] [blame] | 47 | #ifdef CONFIG_PARAVIRT |
Juergen Gross | 9043442 | 2017-09-06 19:36:24 +0200 | [diff] [blame] | 48 | DECLARE_STATIC_KEY_TRUE(virt_spin_lock_key); |
| 49 | |
| 50 | void native_pv_lock_init(void) __init; |
| 51 | |
Peter Zijlstra | 43b3f02 | 2015-09-04 17:25:23 +0200 | [diff] [blame] | 52 | #define virt_spin_lock virt_spin_lock |
Peter Zijlstra | 43b3f02 | 2015-09-04 17:25:23 +0200 | [diff] [blame] | 53 | static inline bool virt_spin_lock(struct qspinlock *lock) |
Peter Zijlstra (Intel) | 2aa79af | 2015-04-24 14:56:36 -0400 | [diff] [blame] | 54 | { |
Juergen Gross | 9043442 | 2017-09-06 19:36:24 +0200 | [diff] [blame] | 55 | if (!static_branch_likely(&virt_spin_lock_key)) |
Peter Zijlstra (Intel) | 2aa79af | 2015-04-24 14:56:36 -0400 | [diff] [blame] | 56 | return false; |
| 57 | |
Peter Zijlstra | 43b3f02 | 2015-09-04 17:25:23 +0200 | [diff] [blame] | 58 | /* |
| 59 | * On hypervisors without PARAVIRT_SPINLOCKS support we fall |
| 60 | * back to a Test-and-Set spinlock, because fair locks have |
| 61 | * horrible lock 'holder' preemption issues. |
| 62 | */ |
| 63 | |
| 64 | do { |
| 65 | while (atomic_read(&lock->val) != 0) |
| 66 | cpu_relax(); |
| 67 | } while (atomic_cmpxchg(&lock->val, 0, _Q_LOCKED_VAL) != 0); |
Peter Zijlstra (Intel) | 2aa79af | 2015-04-24 14:56:36 -0400 | [diff] [blame] | 68 | |
| 69 | return true; |
| 70 | } |
Juergen Gross | 9043442 | 2017-09-06 19:36:24 +0200 | [diff] [blame] | 71 | #else |
| 72 | static inline void native_pv_lock_init(void) |
| 73 | { |
| 74 | } |
Peter Zijlstra | a6b2778 | 2015-09-05 16:55:05 +0200 | [diff] [blame] | 75 | #endif /* CONFIG_PARAVIRT */ |
Peter Zijlstra (Intel) | 2aa79af | 2015-04-24 14:56:36 -0400 | [diff] [blame] | 76 | |
Waiman Long | d73a339 | 2015-04-24 14:56:31 -0400 | [diff] [blame] | 77 | #include <asm-generic/qspinlock.h> |
| 78 | |
| 79 | #endif /* _ASM_X86_QSPINLOCK_H */ |