1 /* SPDX-License-Identifier: GPL-2.0 */
4 * Copyright IBM Corp. 1999
5 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
7 * Derived from "include/asm-i386/spinlock.h"
10 #ifndef __ASM_SPINLOCK_H
11 #define __ASM_SPINLOCK_H
13 #include <linux/smp.h>
14 #include <asm/atomic_ops.h>
15 #include <asm/barrier.h>
16 #include <asm/processor.h>
18 #define SPINLOCK_LOCKVAL (S390_lowcore.spinlock_lockval)
20 extern int spin_retry
;
23 static inline bool arch_vcpu_is_preempted(int cpu
) { return false; }
25 bool arch_vcpu_is_preempted(int cpu
);
28 #define vcpu_is_preempted arch_vcpu_is_preempted
31 * Simple spin lock operations. There are two variants, one clears IRQ's
32 * on the local processor, one does not.
34 * We make no fairness assumptions. They have a cost.
36 * (the type definitions are in asm/spinlock_types.h)
39 void arch_lock_relax(int cpu
);
41 void arch_spin_lock_wait(arch_spinlock_t
*);
42 int arch_spin_trylock_retry(arch_spinlock_t
*);
43 void arch_spin_lock_wait_flags(arch_spinlock_t
*, unsigned long flags
);
45 static inline void arch_spin_relax(arch_spinlock_t
*lock
)
47 arch_lock_relax(lock
->lock
);
50 static inline u32
arch_spin_lockval(int cpu
)
55 static inline int arch_spin_value_unlocked(arch_spinlock_t lock
)
57 return lock
.lock
== 0;
60 static inline int arch_spin_is_locked(arch_spinlock_t
*lp
)
62 return READ_ONCE(lp
->lock
) != 0;
65 static inline int arch_spin_trylock_once(arch_spinlock_t
*lp
)
68 return likely(arch_spin_value_unlocked(*lp
) &&
69 __atomic_cmpxchg_bool(&lp
->lock
, 0, SPINLOCK_LOCKVAL
));
72 static inline void arch_spin_lock(arch_spinlock_t
*lp
)
74 if (!arch_spin_trylock_once(lp
))
75 arch_spin_lock_wait(lp
);
78 static inline void arch_spin_lock_flags(arch_spinlock_t
*lp
,
81 if (!arch_spin_trylock_once(lp
))
82 arch_spin_lock_wait_flags(lp
, flags
);
85 static inline int arch_spin_trylock(arch_spinlock_t
*lp
)
87 if (!arch_spin_trylock_once(lp
))
88 return arch_spin_trylock_retry(lp
);
92 static inline void arch_spin_unlock(arch_spinlock_t
*lp
)
94 typecheck(int, lp
->lock
);
96 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
97 " .long 0xb2fa0070\n" /* NIAI 7 */
100 : "=Q" (lp
->lock
) : "d" (0) : "cc", "memory");
104 * Read-write spinlocks, allowing multiple readers
105 * but only one writer.
107 * NOTE! it is quite common to have readers in interrupts
108 * but no interrupt writers. For those circumstances we
109 * can "mix" irq-safe locks - any writer needs to get a
110 * irq-safe write-lock, but readers can get non-irqsafe
115 * read_can_lock - would read_trylock() succeed?
116 * @lock: the rwlock in question.
118 #define arch_read_can_lock(x) ((int)(x)->lock >= 0)
121 * write_can_lock - would write_trylock() succeed?
122 * @lock: the rwlock in question.
124 #define arch_write_can_lock(x) ((x)->lock == 0)
126 extern int _raw_read_trylock_retry(arch_rwlock_t
*lp
);
127 extern int _raw_write_trylock_retry(arch_rwlock_t
*lp
);
129 #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
130 #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
132 static inline int arch_read_trylock_once(arch_rwlock_t
*rw
)
134 int old
= ACCESS_ONCE(rw
->lock
);
135 return likely(old
>= 0 &&
136 __atomic_cmpxchg_bool(&rw
->lock
, old
, old
+ 1));
139 static inline int arch_write_trylock_once(arch_rwlock_t
*rw
)
141 int old
= ACCESS_ONCE(rw
->lock
);
142 return likely(old
== 0 &&
143 __atomic_cmpxchg_bool(&rw
->lock
, 0, 0x80000000));
146 #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
148 #define __RAW_OP_OR "lao"
149 #define __RAW_OP_AND "lan"
150 #define __RAW_OP_ADD "laa"
152 #define __RAW_LOCK(ptr, op_val, op_string) \
156 typecheck(int *, ptr); \
158 op_string " %0,%2,%1\n" \
160 : "=d" (old_val), "+Q" (*ptr) \
166 #define __RAW_UNLOCK(ptr, op_val, op_string) \
170 typecheck(int *, ptr); \
172 op_string " %0,%2,%1\n" \
173 : "=d" (old_val), "+Q" (*ptr) \
179 extern void _raw_read_lock_wait(arch_rwlock_t
*lp
);
180 extern void _raw_write_lock_wait(arch_rwlock_t
*lp
, int prev
);
182 static inline void arch_read_lock(arch_rwlock_t
*rw
)
186 old
= __RAW_LOCK(&rw
->lock
, 1, __RAW_OP_ADD
);
188 _raw_read_lock_wait(rw
);
191 static inline void arch_read_unlock(arch_rwlock_t
*rw
)
193 __RAW_UNLOCK(&rw
->lock
, -1, __RAW_OP_ADD
);
196 static inline void arch_write_lock(arch_rwlock_t
*rw
)
200 old
= __RAW_LOCK(&rw
->lock
, 0x80000000, __RAW_OP_OR
);
202 _raw_write_lock_wait(rw
, old
);
203 rw
->owner
= SPINLOCK_LOCKVAL
;
206 static inline void arch_write_unlock(arch_rwlock_t
*rw
)
209 __RAW_UNLOCK(&rw
->lock
, 0x7fffffff, __RAW_OP_AND
);
212 #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
214 extern void _raw_read_lock_wait(arch_rwlock_t
*lp
);
215 extern void _raw_write_lock_wait(arch_rwlock_t
*lp
);
217 static inline void arch_read_lock(arch_rwlock_t
*rw
)
219 if (!arch_read_trylock_once(rw
))
220 _raw_read_lock_wait(rw
);
223 static inline void arch_read_unlock(arch_rwlock_t
*rw
)
228 old
= ACCESS_ONCE(rw
->lock
);
229 } while (!__atomic_cmpxchg_bool(&rw
->lock
, old
, old
- 1));
232 static inline void arch_write_lock(arch_rwlock_t
*rw
)
234 if (!arch_write_trylock_once(rw
))
235 _raw_write_lock_wait(rw
);
236 rw
->owner
= SPINLOCK_LOCKVAL
;
239 static inline void arch_write_unlock(arch_rwlock_t
*rw
)
241 typecheck(int, rw
->lock
);
251 #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
253 static inline int arch_read_trylock(arch_rwlock_t
*rw
)
255 if (!arch_read_trylock_once(rw
))
256 return _raw_read_trylock_retry(rw
);
260 static inline int arch_write_trylock(arch_rwlock_t
*rw
)
262 if (!arch_write_trylock_once(rw
) && !_raw_write_trylock_retry(rw
))
264 rw
->owner
= SPINLOCK_LOCKVAL
;
268 static inline void arch_read_relax(arch_rwlock_t
*rw
)
270 arch_lock_relax(rw
->owner
);
273 static inline void arch_write_relax(arch_rwlock_t
*rw
)
275 arch_lock_relax(rw
->owner
);
278 #endif /* __ASM_SPINLOCK_H */