1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* spinlock.h: 32-bit Sparc spinlock support.
7 #ifndef __SPARC_SPINLOCK_H
8 #define __SPARC_SPINLOCK_H
13 #include <asm/barrier.h>
14 #include <asm/processor.h> /* for cpu_relax */
16 #define arch_spin_is_locked(lock) (*((volatile unsigned char *)(lock)) != 0)
18 static inline void arch_spin_lock(arch_spinlock_t *lock)
22 "ldstub [%0], %%g2\n\t"
23 "orcc %%g2, 0x0, %%g0\n\t"
25 " ldub [%0], %%g2\n\t"
28 "orcc %%g2, 0x0, %%g0\n\t"
30 " ldub [%0], %%g2\n\t"
35 : "g2", "memory", "cc");
38 static inline int arch_spin_trylock(arch_spinlock_t *lock)
41 __asm__ __volatile__("ldstub [%1], %0"
48 static inline void arch_spin_unlock(arch_spinlock_t *lock)
50 __asm__ __volatile__("stb %%g0, [%0]" : : "r" (lock) : "memory");
53 /* Read-write spinlocks, allowing multiple readers
54 * but only one writer.
56 * NOTE! it is quite common to have readers in interrupts
57 * but no interrupt writers. For those circumstances we
58 * can "mix" irq-safe locks - any writer needs to get a
59 * irq-safe write-lock, but readers can get non-irqsafe
62 * XXX This might create some problems with my dual spinlock
63 * XXX scheme, deadlocks etc. -DaveM
65 * Sort of like atomic_t's on Sparc, but even more clever.
67 * ------------------------------------
68 * | 24-bit counter | wlock | arch_rwlock_t
69 * ------------------------------------
72 * wlock signifies the one writer is in or somebody is updating
73 * counter. For a writer, if he successfully acquires the wlock,
74 * but counter is non-zero, he has to release the lock and wait,
75 * till both counter and wlock are zero.
77 * Unfortunately this scheme limits us to ~16,000,000 cpus.
79 static inline void __arch_read_lock(arch_rwlock_t *rw)
81 register arch_rwlock_t *lp asm("g1");
85 "call ___rw_read_enter\n\t"
86 " ldstub [%%g1 + 3], %%g2\n"
89 : "g2", "g4", "memory", "cc");
92 #define arch_read_lock(lock) \
93 do { unsigned long flags; \
94 local_irq_save(flags); \
95 __arch_read_lock(lock); \
96 local_irq_restore(flags); \
99 static inline void __arch_read_unlock(arch_rwlock_t *rw)
101 register arch_rwlock_t *lp asm("g1");
103 __asm__ __volatile__(
105 "call ___rw_read_exit\n\t"
106 " ldstub [%%g1 + 3], %%g2\n"
109 : "g2", "g4", "memory", "cc");
112 #define arch_read_unlock(lock) \
113 do { unsigned long flags; \
114 local_irq_save(flags); \
115 __arch_read_unlock(lock); \
116 local_irq_restore(flags); \
119 static inline void arch_write_lock(arch_rwlock_t *rw)
121 register arch_rwlock_t *lp asm("g1");
123 __asm__ __volatile__(
125 "call ___rw_write_enter\n\t"
126 " ldstub [%%g1 + 3], %%g2\n"
129 : "g2", "g4", "memory", "cc");
130 *(volatile __u32 *)&lp->lock = ~0U;
133 static inline void arch_write_unlock(arch_rwlock_t *lock)
135 __asm__ __volatile__(
142 static inline int arch_write_trylock(arch_rwlock_t *rw)
146 __asm__ __volatile__("ldstub [%1 + 3], %0"
152 val = rw->lock & ~0xff;
154 ((volatile u8*)&rw->lock)[3] = 0;
156 *(volatile u32*)&rw->lock = ~0U;
162 static inline int __arch_read_trylock(arch_rwlock_t *rw)
164 register arch_rwlock_t *lp asm("g1");
165 register int res asm("o0");
167 __asm__ __volatile__(
169 "call ___rw_read_try\n\t"
170 " ldstub [%%g1 + 3], %%g2\n"
173 : "g2", "g4", "memory", "cc");
177 #define arch_read_trylock(lock) \
178 ({ unsigned long flags; \
180 local_irq_save(flags); \
181 res = __arch_read_trylock(lock); \
182 local_irq_restore(flags); \
186 #endif /* !(__ASSEMBLY__) */
188 #endif /* __SPARC_SPINLOCK_H */