1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_POWERPC_QSPINLOCK_H
3 #define _ASM_POWERPC_QSPINLOCK_H
5 #include <linux/compiler.h>
6 #include <asm/qspinlock_types.h>
7 #include <asm/paravirt.h>
11 * Use the EH=1 hint for accesses that result in the lock being acquired.
12 * The hardware is supposed to optimise this pattern by holding the lock
13 * cacheline longer, and releasing when a store to the same memory (the
14 * unlock) is performed.
16 #define _Q_SPIN_EH_HINT 1
18 #define _Q_SPIN_EH_HINT 0
22 * The trylock itself may steal. This makes trylocks slightly stronger, and
23 * makes locks slightly more efficient when stealing.
25 * This is compile-time, so if true then there may always be stealers, so the
26 * nosteal paths become unused.
28 #define _Q_SPIN_TRY_LOCK_STEAL 1
31 * Put a speculation barrier after testing the lock/node and finding it
32 * busy. Try to prevent pointless speculation in slow paths.
34 * Slows down the lockstorm microbenchmark with no stealing, where locking
35 * is purely FIFO through the queue. May have more benefit in real workload
36 * where speculating into the wrong place could have a greater cost.
38 #define _Q_SPIN_SPEC_BARRIER 0
42 * Execute a miso instruction after passing the MCS lock ownership to the
43 * queue head. Miso is intended to make stores visible to other CPUs sooner.
45 * This seems to make the lockstorm microbenchmark nospin test go slightly
46 * faster on POWER10, but disable for now.
48 #define _Q_SPIN_MISO 0
50 #define _Q_SPIN_MISO 0
55 * This executes miso after an unlock of the lock word, having ownership
56 * pass to the next CPU sooner. This will slow the uncontended path to some
57 * degree. Not evidence it helps yet.
59 #define _Q_SPIN_MISO_UNLOCK 0
61 #define _Q_SPIN_MISO_UNLOCK 0
65 * Seems to slow down lockstorm microbenchmark, suspect queue node just
66 * has to become shared again right afterwards when its waiter spins on
69 #define _Q_SPIN_PREFETCH_NEXT 0
71 static __always_inline
int queued_spin_is_locked(struct qspinlock
*lock
)
73 return READ_ONCE(lock
->val
);
76 static __always_inline
int queued_spin_value_unlocked(struct qspinlock lock
)
81 static __always_inline
int queued_spin_is_contended(struct qspinlock
*lock
)
83 return !!(READ_ONCE(lock
->val
) & _Q_TAIL_CPU_MASK
);
86 static __always_inline u32
queued_spin_encode_locked_val(void)
88 /* XXX: make this use lock value in paca like simple spinlocks? */
89 return _Q_LOCKED_VAL
| (smp_processor_id() << _Q_OWNER_CPU_OFFSET
);
92 static __always_inline
int __queued_spin_trylock_nosteal(struct qspinlock
*lock
)
94 u32
new = queued_spin_encode_locked_val();
97 /* Trylock succeeds only when unlocked and no queued nodes */
99 "1: lwarx %0,0,%1,%3 # __queued_spin_trylock_nosteal \n"
104 "\t" PPC_ACQUIRE_BARRIER
" \n"
107 : "r" (&lock
->val
), "r" (new),
108 "i" (_Q_SPIN_EH_HINT
)
111 return likely(prev
== 0);
114 static __always_inline
int __queued_spin_trylock_steal(struct qspinlock
*lock
)
116 u32
new = queued_spin_encode_locked_val();
119 /* Trylock may get ahead of queued nodes if it finds unlocked */
121 "1: lwarx %0,0,%2,%5 # __queued_spin_trylock_steal \n"
128 "\t" PPC_ACQUIRE_BARRIER
" \n"
130 : "=&r" (prev
), "=&r" (tmp
)
131 : "r" (&lock
->val
), "r" (new), "r" (_Q_TAIL_CPU_MASK
),
132 "i" (_Q_SPIN_EH_HINT
)
135 return likely(!(prev
& ~_Q_TAIL_CPU_MASK
));
138 static __always_inline
int queued_spin_trylock(struct qspinlock
*lock
)
140 if (!_Q_SPIN_TRY_LOCK_STEAL
)
141 return __queued_spin_trylock_nosteal(lock
);
143 return __queued_spin_trylock_steal(lock
);
146 void queued_spin_lock_slowpath(struct qspinlock
*lock
);
148 static __always_inline
void queued_spin_lock(struct qspinlock
*lock
)
150 if (!queued_spin_trylock(lock
))
151 queued_spin_lock_slowpath(lock
);
154 static inline void queued_spin_unlock(struct qspinlock
*lock
)
156 smp_store_release(&lock
->locked
, 0);
157 if (_Q_SPIN_MISO_UNLOCK
)
158 asm volatile("miso" ::: "memory");
161 #define arch_spin_is_locked(l) queued_spin_is_locked(l)
162 #define arch_spin_is_contended(l) queued_spin_is_contended(l)
163 #define arch_spin_value_unlocked(l) queued_spin_value_unlocked(l)
164 #define arch_spin_lock(l) queued_spin_lock(l)
165 #define arch_spin_trylock(l) queued_spin_trylock(l)
166 #define arch_spin_unlock(l) queued_spin_unlock(l)
168 #ifdef CONFIG_PARAVIRT_SPINLOCKS
169 void pv_spinlocks_init(void);
171 static inline void pv_spinlocks_init(void) { }
174 #endif /* _ASM_POWERPC_QSPINLOCK_H */