blob: fde943d180e03fe725989b4ffbe8af167f3535f8 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Waiman Longa33fda32015-04-24 14:56:30 -04002/*
3 * Queued spinlock
4 *
Waiman Longa33fda32015-04-24 14:56:30 -04005 * (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.
Waiman Long64d816c2015-11-09 19:09:21 -05006 * (C) Copyright 2015 Hewlett-Packard Enterprise Development LP
Waiman Longa33fda32015-04-24 14:56:30 -04007 *
Waiman Long64d816c2015-11-09 19:09:21 -05008 * Authors: Waiman Long <waiman.long@hpe.com>
Waiman Longa33fda32015-04-24 14:56:30 -04009 */
10#ifndef __ASM_GENERIC_QSPINLOCK_H
11#define __ASM_GENERIC_QSPINLOCK_H
12
13#include <asm-generic/qspinlock_types.h>
14
15/**
16 * queued_spin_is_locked - is the spinlock locked?
17 * @lock: Pointer to queued spinlock structure
18 * Return: 1 if it is locked, 0 otherwise
19 */
20static __always_inline int queued_spin_is_locked(struct qspinlock *lock)
21{
Peter Zijlstra54cf8092016-05-20 18:04:36 +020022 /*
Peter Zijlstra2c610022016-06-08 10:19:51 +020023 * Any !0 state indicates it is locked, even if _Q_LOCKED_VAL
24 * isn't immediately observable.
Peter Zijlstra54cf8092016-05-20 18:04:36 +020025 */
Peter Zijlstra2c610022016-06-08 10:19:51 +020026 return atomic_read(&lock->val);
Waiman Longa33fda32015-04-24 14:56:30 -040027}
28
29/**
30 * queued_spin_value_unlocked - is the spinlock structure unlocked?
31 * @lock: queued spinlock structure
32 * Return: 1 if it is unlocked, 0 otherwise
33 *
34 * N.B. Whenever there are tasks waiting for the lock, it is considered
35 * locked wrt the lockref code to avoid lock stealing by the lockref
36 * code and change things underneath the lock. This also allows some
37 * optimizations to be applied without conflict with lockref.
38 */
39static __always_inline int queued_spin_value_unlocked(struct qspinlock lock)
40{
41 return !atomic_read(&lock.val);
42}
43
44/**
45 * queued_spin_is_contended - check if the lock is contended
46 * @lock : Pointer to queued spinlock structure
47 * Return: 1 if lock contended, 0 otherwise
48 */
49static __always_inline int queued_spin_is_contended(struct qspinlock *lock)
50{
51 return atomic_read(&lock->val) & ~_Q_LOCKED_MASK;
52}
53/**
54 * queued_spin_trylock - try to acquire the queued spinlock
55 * @lock : Pointer to queued spinlock structure
56 * Return: 1 if lock acquired, 0 if failed
57 */
58static __always_inline int queued_spin_trylock(struct qspinlock *lock)
59{
Matthew Wilcox27df8962018-08-20 10:19:14 -040060 u32 val = atomic_read(&lock->val);
61
62 if (unlikely(val))
63 return 0;
64
65 return likely(atomic_try_cmpxchg_acquire(&lock->val, &val, _Q_LOCKED_VAL));
Waiman Longa33fda32015-04-24 14:56:30 -040066}
67
68extern void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val);
69
70/**
71 * queued_spin_lock - acquire a queued spinlock
72 * @lock: Pointer to queued spinlock structure
73 */
74static __always_inline void queued_spin_lock(struct qspinlock *lock)
75{
Matthew Wilcox27df8962018-08-20 10:19:14 -040076 u32 val = 0;
Waiman Longa33fda32015-04-24 14:56:30 -040077
Matthew Wilcox27df8962018-08-20 10:19:14 -040078 if (likely(atomic_try_cmpxchg_acquire(&lock->val, &val, _Q_LOCKED_VAL)))
Waiman Longa33fda32015-04-24 14:56:30 -040079 return;
Matthew Wilcox27df8962018-08-20 10:19:14 -040080
Waiman Longa33fda32015-04-24 14:56:30 -040081 queued_spin_lock_slowpath(lock, val);
82}
83
84#ifndef queued_spin_unlock
85/**
86 * queued_spin_unlock - release a queued spinlock
87 * @lock : Pointer to queued spinlock structure
88 */
89static __always_inline void queued_spin_unlock(struct qspinlock *lock)
90{
91 /*
Pan Xinhuica50e422016-06-03 16:38:14 +080092 * unlock() needs release semantics:
Waiman Longa33fda32015-04-24 14:56:30 -040093 */
Will Deacon626e5fb2018-04-26 11:34:24 +010094 smp_store_release(&lock->locked, 0);
Waiman Longa33fda32015-04-24 14:56:30 -040095}
96#endif
97
Peter Zijlstra43b3f022015-09-04 17:25:23 +020098#ifndef virt_spin_lock
99static __always_inline bool virt_spin_lock(struct qspinlock *lock)
Peter Zijlstra (Intel)2aa79af2015-04-24 14:56:36 -0400100{
101 return false;
102}
103#endif
104
Waiman Longa33fda32015-04-24 14:56:30 -0400105/*
Waiman Longa33fda32015-04-24 14:56:30 -0400106 * Remapping spinlock architecture specific functions to the corresponding
107 * queued spinlock functions.
108 */
109#define arch_spin_is_locked(l) queued_spin_is_locked(l)
110#define arch_spin_is_contended(l) queued_spin_is_contended(l)
111#define arch_spin_value_unlocked(l) queued_spin_value_unlocked(l)
112#define arch_spin_lock(l) queued_spin_lock(l)
113#define arch_spin_trylock(l) queued_spin_trylock(l)
114#define arch_spin_unlock(l) queued_spin_unlock(l)
Waiman Longa33fda32015-04-24 14:56:30 -0400115
116#endif /* __ASM_GENERIC_QSPINLOCK_H */