1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_SPINLOCK_H
3 #define __ASM_SPINLOCK_H
5 #include <asm/barrier.h>
7 #include <asm/processor.h>
8 #include <asm/spinlock_types.h>
10 static inline int arch_spin_is_locked(arch_spinlock_t *x)
12 volatile unsigned int *a = __ldcw_align(x);
16 #define arch_spin_lock(lock) arch_spin_lock_flags(lock, 0)
18 static inline void arch_spin_lock_flags(arch_spinlock_t *x,
21 volatile unsigned int *a;
24 while (__ldcw(a) == 0)
26 if (flags & PSW_SM_I) {
33 #define arch_spin_lock_flags arch_spin_lock_flags
35 static inline void arch_spin_unlock(arch_spinlock_t *x)
37 volatile unsigned int *a;
40 /* Release with ordered store. */
41 __asm__ __volatile__("stw,ma %0,0(%1)" : : "r"(1), "r"(a) : "memory");
44 static inline int arch_spin_trylock(arch_spinlock_t *x)
46 volatile unsigned int *a;
56 * Read-write spinlocks, allowing multiple readers but only one writer.
57 * Linux rwlocks are unfair to writers; they can be starved for an indefinite
58 * time by readers. With care, they can also be taken in interrupt context.
60 * In the PA-RISC implementation, we have a spinlock and a counter.
61 * Readers use the lock to serialise their access to the counter (which
62 * records how many readers currently hold the lock).
63 * Writers hold the spinlock, preventing any readers or other writers from
64 * grabbing the rwlock.
67 /* Note that we have to ensure interrupts are disabled in case we're
68 * interrupted by some other code that wants to grab the same read lock */
69 static __inline__ void arch_read_lock(arch_rwlock_t *rw)
72 local_irq_save(flags);
73 arch_spin_lock_flags(&rw->lock, flags);
75 arch_spin_unlock(&rw->lock);
76 local_irq_restore(flags);
79 /* Note that we have to ensure interrupts are disabled in case we're
80 * interrupted by some other code that wants to grab the same read lock */
81 static __inline__ void arch_read_unlock(arch_rwlock_t *rw)
84 local_irq_save(flags);
85 arch_spin_lock_flags(&rw->lock, flags);
87 arch_spin_unlock(&rw->lock);
88 local_irq_restore(flags);
91 /* Note that we have to ensure interrupts are disabled in case we're
92 * interrupted by some other code that wants to grab the same read lock */
93 static __inline__ int arch_read_trylock(arch_rwlock_t *rw)
97 local_irq_save(flags);
98 if (arch_spin_trylock(&rw->lock)) {
100 arch_spin_unlock(&rw->lock);
101 local_irq_restore(flags);
105 local_irq_restore(flags);
106 /* If write-locked, we fail to acquire the lock */
110 /* Wait until we have a realistic chance at the lock */
111 while (arch_spin_is_locked(&rw->lock) && rw->counter >= 0)
117 /* Note that we have to ensure interrupts are disabled in case we're
118 * interrupted by some other code that wants to read_trylock() this lock */
119 static __inline__ void arch_write_lock(arch_rwlock_t *rw)
123 local_irq_save(flags);
124 arch_spin_lock_flags(&rw->lock, flags);
126 if (rw->counter != 0) {
127 arch_spin_unlock(&rw->lock);
128 local_irq_restore(flags);
130 while (rw->counter != 0)
136 rw->counter = -1; /* mark as write-locked */
138 local_irq_restore(flags);
141 static __inline__ void arch_write_unlock(arch_rwlock_t *rw)
144 arch_spin_unlock(&rw->lock);
147 /* Note that we have to ensure interrupts are disabled in case we're
148 * interrupted by some other code that wants to read_trylock() this lock */
149 static __inline__ int arch_write_trylock(arch_rwlock_t *rw)
154 local_irq_save(flags);
155 if (arch_spin_trylock(&rw->lock)) {
156 if (rw->counter == 0) {
160 /* Read-locked. Oh well. */
161 arch_spin_unlock(&rw->lock);
164 local_irq_restore(flags);
169 #endif /* __ASM_SPINLOCK_H */