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>
17 #include <asm/alternative.h>
19 #define SPINLOCK_LOCKVAL (S390_lowcore.spinlock_lockval)
21 extern int spin_retry
;
23 bool arch_vcpu_is_preempted(int cpu
);
25 #define vcpu_is_preempted arch_vcpu_is_preempted
28 * Simple spin lock operations. There are two variants, one clears IRQ's
29 * on the local processor, one does not.
31 * We make no fairness assumptions. They have a cost.
33 * (the type definitions are in asm/spinlock_types.h)
36 void arch_spin_relax(arch_spinlock_t
*lock
);
37 #define arch_spin_relax arch_spin_relax
39 void arch_spin_lock_wait(arch_spinlock_t
*);
40 int arch_spin_trylock_retry(arch_spinlock_t
*);
41 void arch_spin_lock_setup(int cpu
);
43 static inline u32
arch_spin_lockval(int cpu
)
48 static inline int arch_spin_value_unlocked(arch_spinlock_t lock
)
50 return lock
.lock
== 0;
53 static inline int arch_spin_is_locked(arch_spinlock_t
*lp
)
55 return READ_ONCE(lp
->lock
) != 0;
58 static inline int arch_spin_trylock_once(arch_spinlock_t
*lp
)
61 return likely(__atomic_cmpxchg_bool(&lp
->lock
, 0, SPINLOCK_LOCKVAL
));
64 static inline void arch_spin_lock(arch_spinlock_t
*lp
)
66 if (!arch_spin_trylock_once(lp
))
67 arch_spin_lock_wait(lp
);
70 static inline void arch_spin_lock_flags(arch_spinlock_t
*lp
,
73 if (!arch_spin_trylock_once(lp
))
74 arch_spin_lock_wait(lp
);
76 #define arch_spin_lock_flags arch_spin_lock_flags
78 static inline int arch_spin_trylock(arch_spinlock_t
*lp
)
80 if (!arch_spin_trylock_once(lp
))
81 return arch_spin_trylock_retry(lp
);
85 static inline void arch_spin_unlock(arch_spinlock_t
*lp
)
87 typecheck(int, lp
->lock
);
89 ALTERNATIVE("", ".long 0xb2fa0070", 49) /* NIAI 7 */
91 : "=Q" (((unsigned short *) &lp
->lock
)[1])
92 : "d" (0) : "cc", "memory");
96 * Read-write spinlocks, allowing multiple readers
97 * but only one writer.
99 * NOTE! it is quite common to have readers in interrupts
100 * but no interrupt writers. For those circumstances we
101 * can "mix" irq-safe locks - any writer needs to get a
102 * irq-safe write-lock, but readers can get non-irqsafe
106 #define arch_read_relax(rw) barrier()
107 #define arch_write_relax(rw) barrier()
109 void arch_read_lock_wait(arch_rwlock_t
*lp
);
110 void arch_write_lock_wait(arch_rwlock_t
*lp
);
112 static inline void arch_read_lock(arch_rwlock_t
*rw
)
116 old
= __atomic_add(1, &rw
->cnts
);
117 if (old
& 0xffff0000)
118 arch_read_lock_wait(rw
);
121 static inline void arch_read_unlock(arch_rwlock_t
*rw
)
123 __atomic_add_const_barrier(-1, &rw
->cnts
);
126 static inline void arch_write_lock(arch_rwlock_t
*rw
)
128 if (!__atomic_cmpxchg_bool(&rw
->cnts
, 0, 0x30000))
129 arch_write_lock_wait(rw
);
132 static inline void arch_write_unlock(arch_rwlock_t
*rw
)
134 __atomic_add_barrier(-0x30000, &rw
->cnts
);
138 static inline int arch_read_trylock(arch_rwlock_t
*rw
)
142 old
= READ_ONCE(rw
->cnts
);
143 return (!(old
& 0xffff0000) &&
144 __atomic_cmpxchg_bool(&rw
->cnts
, old
, old
+ 1));
147 static inline int arch_write_trylock(arch_rwlock_t
*rw
)
151 old
= READ_ONCE(rw
->cnts
);
152 return !old
&& __atomic_cmpxchg_bool(&rw
->cnts
, 0, 0x30000);
155 #endif /* __ASM_SPINLOCK_H */