18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  S390 version
48c2ecf20Sopenharmony_ci *    Copyright IBM Corp. 1999
58c2ecf20Sopenharmony_ci *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *  Derived from "include/asm-i386/spinlock.h"
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#ifndef __ASM_SPINLOCK_H
118c2ecf20Sopenharmony_ci#define __ASM_SPINLOCK_H
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/smp.h>
148c2ecf20Sopenharmony_ci#include <asm/atomic_ops.h>
158c2ecf20Sopenharmony_ci#include <asm/barrier.h>
168c2ecf20Sopenharmony_ci#include <asm/processor.h>
178c2ecf20Sopenharmony_ci#include <asm/alternative.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define SPINLOCK_LOCKVAL (S390_lowcore.spinlock_lockval)
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ciextern int spin_retry;
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cibool arch_vcpu_is_preempted(int cpu);
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define vcpu_is_preempted arch_vcpu_is_preempted
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/*
288c2ecf20Sopenharmony_ci * Simple spin lock operations.  There are two variants, one clears IRQ's
298c2ecf20Sopenharmony_ci * on the local processor, one does not.
308c2ecf20Sopenharmony_ci *
318c2ecf20Sopenharmony_ci * We make no fairness assumptions. They have a cost.
328c2ecf20Sopenharmony_ci *
338c2ecf20Sopenharmony_ci * (the type definitions are in asm/spinlock_types.h)
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_civoid arch_spin_relax(arch_spinlock_t *lock);
378c2ecf20Sopenharmony_ci#define arch_spin_relax	arch_spin_relax
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_civoid arch_spin_lock_wait(arch_spinlock_t *);
408c2ecf20Sopenharmony_ciint arch_spin_trylock_retry(arch_spinlock_t *);
418c2ecf20Sopenharmony_civoid arch_spin_lock_setup(int cpu);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic inline u32 arch_spin_lockval(int cpu)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	return cpu + 1;
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic inline int arch_spin_value_unlocked(arch_spinlock_t lock)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	return lock.lock == 0;
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic inline int arch_spin_is_locked(arch_spinlock_t *lp)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	return READ_ONCE(lp->lock) != 0;
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic inline int arch_spin_trylock_once(arch_spinlock_t *lp)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	barrier();
618c2ecf20Sopenharmony_ci	return likely(__atomic_cmpxchg_bool(&lp->lock, 0, SPINLOCK_LOCKVAL));
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic inline void arch_spin_lock(arch_spinlock_t *lp)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	if (!arch_spin_trylock_once(lp))
678c2ecf20Sopenharmony_ci		arch_spin_lock_wait(lp);
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic inline void arch_spin_lock_flags(arch_spinlock_t *lp,
718c2ecf20Sopenharmony_ci					unsigned long flags)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	if (!arch_spin_trylock_once(lp))
748c2ecf20Sopenharmony_ci		arch_spin_lock_wait(lp);
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci#define arch_spin_lock_flags	arch_spin_lock_flags
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic inline int arch_spin_trylock(arch_spinlock_t *lp)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	if (!arch_spin_trylock_once(lp))
818c2ecf20Sopenharmony_ci		return arch_spin_trylock_retry(lp);
828c2ecf20Sopenharmony_ci	return 1;
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic inline void arch_spin_unlock(arch_spinlock_t *lp)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	typecheck(int, lp->lock);
888c2ecf20Sopenharmony_ci	asm_inline volatile(
898c2ecf20Sopenharmony_ci		ALTERNATIVE("", ".long 0xb2fa0070", 49)	/* NIAI 7 */
908c2ecf20Sopenharmony_ci		"	sth	%1,%0\n"
918c2ecf20Sopenharmony_ci		: "=Q" (((unsigned short *) &lp->lock)[1])
928c2ecf20Sopenharmony_ci		: "d" (0) : "cc", "memory");
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci/*
968c2ecf20Sopenharmony_ci * Read-write spinlocks, allowing multiple readers
978c2ecf20Sopenharmony_ci * but only one writer.
988c2ecf20Sopenharmony_ci *
998c2ecf20Sopenharmony_ci * NOTE! it is quite common to have readers in interrupts
1008c2ecf20Sopenharmony_ci * but no interrupt writers. For those circumstances we
1018c2ecf20Sopenharmony_ci * can "mix" irq-safe locks - any writer needs to get a
1028c2ecf20Sopenharmony_ci * irq-safe write-lock, but readers can get non-irqsafe
1038c2ecf20Sopenharmony_ci * read-locks.
1048c2ecf20Sopenharmony_ci */
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci#define arch_read_relax(rw) barrier()
1078c2ecf20Sopenharmony_ci#define arch_write_relax(rw) barrier()
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_civoid arch_read_lock_wait(arch_rwlock_t *lp);
1108c2ecf20Sopenharmony_civoid arch_write_lock_wait(arch_rwlock_t *lp);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic inline void arch_read_lock(arch_rwlock_t *rw)
1138c2ecf20Sopenharmony_ci{
1148c2ecf20Sopenharmony_ci	int old;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	old = __atomic_add(1, &rw->cnts);
1178c2ecf20Sopenharmony_ci	if (old & 0xffff0000)
1188c2ecf20Sopenharmony_ci		arch_read_lock_wait(rw);
1198c2ecf20Sopenharmony_ci}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic inline void arch_read_unlock(arch_rwlock_t *rw)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	__atomic_add_const_barrier(-1, &rw->cnts);
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic inline void arch_write_lock(arch_rwlock_t *rw)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	if (!__atomic_cmpxchg_bool(&rw->cnts, 0, 0x30000))
1298c2ecf20Sopenharmony_ci		arch_write_lock_wait(rw);
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic inline void arch_write_unlock(arch_rwlock_t *rw)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	__atomic_add_barrier(-0x30000, &rw->cnts);
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic inline int arch_read_trylock(arch_rwlock_t *rw)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	int old;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	old = READ_ONCE(rw->cnts);
1438c2ecf20Sopenharmony_ci	return (!(old & 0xffff0000) &&
1448c2ecf20Sopenharmony_ci		__atomic_cmpxchg_bool(&rw->cnts, old, old + 1));
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cistatic inline int arch_write_trylock(arch_rwlock_t *rw)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	int old;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	old = READ_ONCE(rw->cnts);
1528c2ecf20Sopenharmony_ci	return !old && __atomic_cmpxchg_bool(&rw->cnts, 0, 0x30000);
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci#endif /* __ASM_SPINLOCK_H */
156