1 // SPDX-License-Identifier: GPL-2.0
3 * Out of line spinlock code.
5 * Copyright IBM Corp. 2004, 2006
9 #include <linux/types.h>
10 #include <linux/export.h>
11 #include <linux/spinlock.h>
12 #include <linux/jiffies.h>
13 #include <linux/init.h>
14 #include <linux/smp.h>
15 #include <linux/percpu.h>
17 #include <asm/alternative.h>
22 static int __init spin_retry_init(void)
28 early_initcall(spin_retry_init);
31 * spin_retry= parameter
33 static int __init spin_retry_setup(char *str)
35 spin_retry = simple_strtoul(str, &str, 0);
38 __setup("spin_retry=", spin_retry_setup);
41 struct spin_wait *next, *prev;
45 static DEFINE_PER_CPU_ALIGNED(struct spin_wait, spin_wait[4]);
47 #define _Q_LOCK_CPU_OFFSET 0
48 #define _Q_LOCK_STEAL_OFFSET 16
49 #define _Q_TAIL_IDX_OFFSET 18
50 #define _Q_TAIL_CPU_OFFSET 20
52 #define _Q_LOCK_CPU_MASK 0x0000ffff
53 #define _Q_LOCK_STEAL_ADD 0x00010000
54 #define _Q_LOCK_STEAL_MASK 0x00030000
55 #define _Q_TAIL_IDX_MASK 0x000c0000
56 #define _Q_TAIL_CPU_MASK 0xfff00000
58 #define _Q_LOCK_MASK (_Q_LOCK_CPU_MASK | _Q_LOCK_STEAL_MASK)
59 #define _Q_TAIL_MASK (_Q_TAIL_IDX_MASK | _Q_TAIL_CPU_MASK)
61 void arch_spin_lock_setup(int cpu)
63 struct spin_wait *node;
66 node = per_cpu_ptr(&spin_wait[0], cpu);
67 for (ix = 0; ix < 4; ix++, node++) {
68 memset(node, 0, sizeof(*node));
69 node->node_id = ((cpu + 1) << _Q_TAIL_CPU_OFFSET) +
70 (ix << _Q_TAIL_IDX_OFFSET);
74 static inline int arch_load_niai4(int *lock)
79 ALTERNATIVE("nop", ".insn rre,0xb2fa0000,4,0", ALT_FACILITY(49)) /* NIAI 4 */
80 " l %[owner],%[lock]\n"
81 : [owner] "=d" (owner) : [lock] "R" (*lock) : "memory");
85 #ifdef __HAVE_ASM_FLAG_OUTPUTS__
87 static inline int arch_try_cmpxchg_niai8(int *lock, int old, int new)
92 ALTERNATIVE("nop", ".insn rre,0xb2fa0000,8,0", ALT_FACILITY(49)) /* NIAI 8 */
93 " cs %[old],%[new],%[lock]\n"
94 : [old] "+d" (old), [lock] "+Q" (*lock), "=@cc" (cc)
100 #else /* __HAVE_ASM_FLAG_OUTPUTS__ */
102 static inline int arch_try_cmpxchg_niai8(int *lock, int old, int new)
107 ALTERNATIVE("nop", ".insn rre,0xb2fa0000,8,0", ALT_FACILITY(49)) /* NIAI 8 */
108 " cs %[old],%[new],%[lock]\n"
109 : [old] "+d" (old), [lock] "+Q" (*lock)
112 return expected == old;
115 #endif /* __HAVE_ASM_FLAG_OUTPUTS__ */
117 static inline struct spin_wait *arch_spin_decode_tail(int lock)
121 ix = (lock & _Q_TAIL_IDX_MASK) >> _Q_TAIL_IDX_OFFSET;
122 cpu = (lock & _Q_TAIL_CPU_MASK) >> _Q_TAIL_CPU_OFFSET;
123 return per_cpu_ptr(&spin_wait[ix], cpu - 1);
126 static inline int arch_spin_yield_target(int lock, struct spin_wait *node)
128 if (lock & _Q_LOCK_CPU_MASK)
129 return lock & _Q_LOCK_CPU_MASK;
130 if (node == NULL || node->prev == NULL)
131 return 0; /* 0 -> no target cpu */
134 return node->node_id >> _Q_TAIL_CPU_OFFSET;
137 static inline void arch_spin_lock_queued(arch_spinlock_t *lp)
139 struct spin_wait *node, *next;
140 int lockval, ix, node_id, tail_id, old, new, owner, count;
142 ix = get_lowcore()->spinlock_index++;
144 lockval = SPINLOCK_LOCKVAL; /* cpu + 1 */
145 node = this_cpu_ptr(&spin_wait[ix]);
146 node->prev = node->next = NULL;
147 node_id = node->node_id;
149 /* Enqueue the node for this CPU in the spinlock wait queue */
150 old = READ_ONCE(lp->lock);
152 if ((old & _Q_LOCK_CPU_MASK) == 0 &&
153 (old & _Q_LOCK_STEAL_MASK) != _Q_LOCK_STEAL_MASK) {
155 * The lock is free but there may be waiters.
156 * With no waiters simply take the lock, if there
157 * are waiters try to steal the lock. The lock may
158 * be stolen three times before the next queued
159 * waiter will get the lock.
161 new = (old ? (old + _Q_LOCK_STEAL_ADD) : 0) | lockval;
162 if (arch_try_cmpxchg(&lp->lock, &old, new))
165 /* lock passing in progress */
168 /* Make the node of this CPU the new tail. */
169 new = node_id | (old & _Q_LOCK_MASK);
170 if (arch_try_cmpxchg(&lp->lock, &old, new))
173 /* Set the 'next' pointer of the tail node in the queue */
174 tail_id = old & _Q_TAIL_MASK;
176 node->prev = arch_spin_decode_tail(tail_id);
177 WRITE_ONCE(node->prev->next, node);
180 /* Pass the virtual CPU to the lock holder if it is not running */
181 owner = arch_spin_yield_target(old, node);
182 if (owner && arch_vcpu_is_preempted(owner - 1))
183 smp_yield_cpu(owner - 1);
185 /* Spin on the CPU local node->prev pointer */
188 while (READ_ONCE(node->prev) != NULL) {
192 /* Query running state of lock holder again. */
193 owner = arch_spin_yield_target(old, node);
194 if (owner && arch_vcpu_is_preempted(owner - 1))
195 smp_yield_cpu(owner - 1);
199 /* Spin on the lock value in the spinlock_t */
202 old = READ_ONCE(lp->lock);
203 owner = old & _Q_LOCK_CPU_MASK;
205 tail_id = old & _Q_TAIL_MASK;
206 new = ((tail_id != node_id) ? tail_id : 0) | lockval;
207 if (arch_try_cmpxchg(&lp->lock, &old, new))
215 if (!MACHINE_IS_LPAR || arch_vcpu_is_preempted(owner - 1))
216 smp_yield_cpu(owner - 1);
219 /* Pass lock_spin job to next CPU in the queue */
220 if (node_id && tail_id != node_id) {
221 /* Wait until the next CPU has set up the 'next' pointer */
222 while ((next = READ_ONCE(node->next)) == NULL)
228 get_lowcore()->spinlock_index--;
231 static inline void arch_spin_lock_classic(arch_spinlock_t *lp)
233 int lockval, old, new, owner, count;
235 lockval = SPINLOCK_LOCKVAL; /* cpu + 1 */
237 /* Pass the virtual CPU to the lock holder if it is not running */
238 owner = arch_spin_yield_target(READ_ONCE(lp->lock), NULL);
239 if (owner && arch_vcpu_is_preempted(owner - 1))
240 smp_yield_cpu(owner - 1);
244 old = arch_load_niai4(&lp->lock);
245 owner = old & _Q_LOCK_CPU_MASK;
246 /* Try to get the lock if it is free. */
248 new = (old & _Q_TAIL_MASK) | lockval;
249 if (arch_try_cmpxchg_niai8(&lp->lock, old, new)) {
258 if (!MACHINE_IS_LPAR || arch_vcpu_is_preempted(owner - 1))
259 smp_yield_cpu(owner - 1);
263 void arch_spin_lock_wait(arch_spinlock_t *lp)
265 if (test_cpu_flag(CIF_DEDICATED_CPU))
266 arch_spin_lock_queued(lp);
268 arch_spin_lock_classic(lp);
270 EXPORT_SYMBOL(arch_spin_lock_wait);
272 int arch_spin_trylock_retry(arch_spinlock_t *lp)
274 int cpu = SPINLOCK_LOCKVAL;
277 for (count = spin_retry; count > 0; count--) {
278 owner = READ_ONCE(lp->lock);
279 /* Try to get the lock if it is free. */
281 if (arch_try_cmpxchg(&lp->lock, &owner, cpu))
287 EXPORT_SYMBOL(arch_spin_trylock_retry);
289 void arch_read_lock_wait(arch_rwlock_t *rw)
291 if (unlikely(in_interrupt())) {
292 while (READ_ONCE(rw->cnts) & 0x10000)
297 /* Remove this reader again to allow recursive read locking */
298 __atomic_add_const(-1, &rw->cnts);
299 /* Put the reader into the wait queue */
300 arch_spin_lock(&rw->wait);
301 /* Now add this reader to the count value again */
302 __atomic_add_const(1, &rw->cnts);
303 /* Loop until the writer is done */
304 while (READ_ONCE(rw->cnts) & 0x10000)
306 arch_spin_unlock(&rw->wait);
308 EXPORT_SYMBOL(arch_read_lock_wait);
310 void arch_write_lock_wait(arch_rwlock_t *rw)
314 /* Add this CPU to the write waiters */
315 __atomic_add(0x20000, &rw->cnts);
317 /* Put the writer into the wait queue */
318 arch_spin_lock(&rw->wait);
321 old = READ_ONCE(rw->cnts);
322 if ((old & 0x1ffff) == 0 &&
323 arch_try_cmpxchg(&rw->cnts, &old, old | 0x10000))
329 arch_spin_unlock(&rw->wait);
331 EXPORT_SYMBOL(arch_write_lock_wait);
333 void arch_spin_relax(arch_spinlock_t *lp)
337 cpu = READ_ONCE(lp->lock) & _Q_LOCK_CPU_MASK;
340 if (MACHINE_IS_LPAR && !arch_vcpu_is_preempted(cpu - 1))
342 smp_yield_cpu(cpu - 1);
344 EXPORT_SYMBOL(arch_spin_relax);