1 // SPDX-License-Identifier: GPL-2.0
3 * Out of line spinlock code.
5 * Copyright IBM Corp. 2004, 2006
6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
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>
16 #include <asm/alternative.h>
21 static int __init
spin_retry_init(void)
27 early_initcall(spin_retry_init
);
30 * spin_retry= parameter
32 static int __init
spin_retry_setup(char *str
)
34 spin_retry
= simple_strtoul(str
, &str
, 0);
37 __setup("spin_retry=", spin_retry_setup
);
40 struct spin_wait
*next
, *prev
;
44 static DEFINE_PER_CPU_ALIGNED(struct spin_wait
, spin_wait
[4]);
46 #define _Q_LOCK_CPU_OFFSET 0
47 #define _Q_LOCK_STEAL_OFFSET 16
48 #define _Q_TAIL_IDX_OFFSET 18
49 #define _Q_TAIL_CPU_OFFSET 20
51 #define _Q_LOCK_CPU_MASK 0x0000ffff
52 #define _Q_LOCK_STEAL_ADD 0x00010000
53 #define _Q_LOCK_STEAL_MASK 0x00030000
54 #define _Q_TAIL_IDX_MASK 0x000c0000
55 #define _Q_TAIL_CPU_MASK 0xfff00000
57 #define _Q_LOCK_MASK (_Q_LOCK_CPU_MASK | _Q_LOCK_STEAL_MASK)
58 #define _Q_TAIL_MASK (_Q_TAIL_IDX_MASK | _Q_TAIL_CPU_MASK)
60 void arch_spin_lock_setup(int cpu
)
62 struct spin_wait
*node
;
65 node
= per_cpu_ptr(&spin_wait
[0], cpu
);
66 for (ix
= 0; ix
< 4; ix
++, node
++) {
67 memset(node
, 0, sizeof(*node
));
68 node
->node_id
= ((cpu
+ 1) << _Q_TAIL_CPU_OFFSET
) +
69 (ix
<< _Q_TAIL_IDX_OFFSET
);
73 static inline int arch_load_niai4(int *lock
)
78 ALTERNATIVE("", ".long 0xb2fa0040", 49) /* NIAI 4 */
80 : "=d" (owner
) : "Q" (*lock
) : "memory");
84 static inline int arch_cmpxchg_niai8(int *lock
, int old
, int new)
89 ALTERNATIVE("", ".long 0xb2fa0080", 49) /* NIAI 8 */
91 : "=d" (old
), "=Q" (*lock
)
92 : "0" (old
), "d" (new), "Q" (*lock
)
94 return expected
== old
;
97 static inline struct spin_wait
*arch_spin_decode_tail(int lock
)
101 ix
= (lock
& _Q_TAIL_IDX_MASK
) >> _Q_TAIL_IDX_OFFSET
;
102 cpu
= (lock
& _Q_TAIL_CPU_MASK
) >> _Q_TAIL_CPU_OFFSET
;
103 return per_cpu_ptr(&spin_wait
[ix
], cpu
- 1);
106 static inline int arch_spin_yield_target(int lock
, struct spin_wait
*node
)
108 if (lock
& _Q_LOCK_CPU_MASK
)
109 return lock
& _Q_LOCK_CPU_MASK
;
110 if (node
== NULL
|| node
->prev
== NULL
)
111 return 0; /* 0 -> no target cpu */
114 return node
->node_id
>> _Q_TAIL_CPU_OFFSET
;
117 static inline void arch_spin_lock_queued(arch_spinlock_t
*lp
)
119 struct spin_wait
*node
, *next
;
120 int lockval
, ix
, node_id
, tail_id
, old
, new, owner
, count
;
122 ix
= S390_lowcore
.spinlock_index
++;
124 lockval
= SPINLOCK_LOCKVAL
; /* cpu + 1 */
125 node
= this_cpu_ptr(&spin_wait
[ix
]);
126 node
->prev
= node
->next
= NULL
;
127 node_id
= node
->node_id
;
129 /* Enqueue the node for this CPU in the spinlock wait queue */
131 old
= READ_ONCE(lp
->lock
);
132 if ((old
& _Q_LOCK_CPU_MASK
) == 0 &&
133 (old
& _Q_LOCK_STEAL_MASK
) != _Q_LOCK_STEAL_MASK
) {
135 * The lock is free but there may be waiters.
136 * With no waiters simply take the lock, if there
137 * are waiters try to steal the lock. The lock may
138 * be stolen three times before the next queued
139 * waiter will get the lock.
141 new = (old
? (old
+ _Q_LOCK_STEAL_ADD
) : 0) | lockval
;
142 if (__atomic_cmpxchg_bool(&lp
->lock
, old
, new))
145 /* lock passing in progress */
148 /* Make the node of this CPU the new tail. */
149 new = node_id
| (old
& _Q_LOCK_MASK
);
150 if (__atomic_cmpxchg_bool(&lp
->lock
, old
, new))
153 /* Set the 'next' pointer of the tail node in the queue */
154 tail_id
= old
& _Q_TAIL_MASK
;
156 node
->prev
= arch_spin_decode_tail(tail_id
);
157 WRITE_ONCE(node
->prev
->next
, node
);
160 /* Pass the virtual CPU to the lock holder if it is not running */
161 owner
= arch_spin_yield_target(old
, node
);
162 if (owner
&& arch_vcpu_is_preempted(owner
- 1))
163 smp_yield_cpu(owner
- 1);
165 /* Spin on the CPU local node->prev pointer */
168 while (READ_ONCE(node
->prev
) != NULL
) {
172 /* Query running state of lock holder again. */
173 owner
= arch_spin_yield_target(old
, node
);
174 if (owner
&& arch_vcpu_is_preempted(owner
- 1))
175 smp_yield_cpu(owner
- 1);
179 /* Spin on the lock value in the spinlock_t */
182 old
= READ_ONCE(lp
->lock
);
183 owner
= old
& _Q_LOCK_CPU_MASK
;
185 tail_id
= old
& _Q_TAIL_MASK
;
186 new = ((tail_id
!= node_id
) ? tail_id
: 0) | lockval
;
187 if (__atomic_cmpxchg_bool(&lp
->lock
, old
, new))
195 if (!MACHINE_IS_LPAR
|| arch_vcpu_is_preempted(owner
- 1))
196 smp_yield_cpu(owner
- 1);
199 /* Pass lock_spin job to next CPU in the queue */
200 if (node_id
&& tail_id
!= node_id
) {
201 /* Wait until the next CPU has set up the 'next' pointer */
202 while ((next
= READ_ONCE(node
->next
)) == NULL
)
208 S390_lowcore
.spinlock_index
--;
211 static inline void arch_spin_lock_classic(arch_spinlock_t
*lp
)
213 int lockval
, old
, new, owner
, count
;
215 lockval
= SPINLOCK_LOCKVAL
; /* cpu + 1 */
217 /* Pass the virtual CPU to the lock holder if it is not running */
218 owner
= arch_spin_yield_target(READ_ONCE(lp
->lock
), NULL
);
219 if (owner
&& arch_vcpu_is_preempted(owner
- 1))
220 smp_yield_cpu(owner
- 1);
224 old
= arch_load_niai4(&lp
->lock
);
225 owner
= old
& _Q_LOCK_CPU_MASK
;
226 /* Try to get the lock if it is free. */
228 new = (old
& _Q_TAIL_MASK
) | lockval
;
229 if (arch_cmpxchg_niai8(&lp
->lock
, old
, new)) {
238 if (!MACHINE_IS_LPAR
|| arch_vcpu_is_preempted(owner
- 1))
239 smp_yield_cpu(owner
- 1);
243 void arch_spin_lock_wait(arch_spinlock_t
*lp
)
245 /* Use classic spinlocks + niai if the steal time is >= 10% */
246 if (test_cpu_flag(CIF_DEDICATED_CPU
))
247 arch_spin_lock_queued(lp
);
249 arch_spin_lock_classic(lp
);
251 EXPORT_SYMBOL(arch_spin_lock_wait
);
253 int arch_spin_trylock_retry(arch_spinlock_t
*lp
)
255 int cpu
= SPINLOCK_LOCKVAL
;
258 for (count
= spin_retry
; count
> 0; count
--) {
259 owner
= READ_ONCE(lp
->lock
);
260 /* Try to get the lock if it is free. */
262 if (__atomic_cmpxchg_bool(&lp
->lock
, 0, cpu
))
268 EXPORT_SYMBOL(arch_spin_trylock_retry
);
270 void arch_read_lock_wait(arch_rwlock_t
*rw
)
272 if (unlikely(in_interrupt())) {
273 while (READ_ONCE(rw
->cnts
) & 0x10000)
278 /* Remove this reader again to allow recursive read locking */
279 __atomic_add_const(-1, &rw
->cnts
);
280 /* Put the reader into the wait queue */
281 arch_spin_lock(&rw
->wait
);
282 /* Now add this reader to the count value again */
283 __atomic_add_const(1, &rw
->cnts
);
284 /* Loop until the writer is done */
285 while (READ_ONCE(rw
->cnts
) & 0x10000)
287 arch_spin_unlock(&rw
->wait
);
289 EXPORT_SYMBOL(arch_read_lock_wait
);
291 void arch_write_lock_wait(arch_rwlock_t
*rw
)
295 /* Add this CPU to the write waiters */
296 __atomic_add(0x20000, &rw
->cnts
);
298 /* Put the writer into the wait queue */
299 arch_spin_lock(&rw
->wait
);
302 old
= READ_ONCE(rw
->cnts
);
303 if ((old
& 0x1ffff) == 0 &&
304 __atomic_cmpxchg_bool(&rw
->cnts
, old
, old
| 0x10000))
310 arch_spin_unlock(&rw
->wait
);
312 EXPORT_SYMBOL(arch_write_lock_wait
);
314 void arch_spin_relax(arch_spinlock_t
*lp
)
318 cpu
= READ_ONCE(lp
->lock
) & _Q_LOCK_CPU_MASK
;
321 if (MACHINE_IS_LPAR
&& !arch_vcpu_is_preempted(cpu
- 1))
323 smp_yield_cpu(cpu
- 1);
325 EXPORT_SYMBOL(arch_spin_relax
);