18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/* spinlock.h: 32-bit Sparc spinlock support.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#ifndef __SPARC_SPINLOCK_H
88c2ecf20Sopenharmony_ci#define __SPARC_SPINLOCK_H
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <asm/psr.h>
138c2ecf20Sopenharmony_ci#include <asm/barrier.h>
148c2ecf20Sopenharmony_ci#include <asm/processor.h> /* for cpu_relax */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define arch_spin_is_locked(lock) (*((volatile unsigned char *)(lock)) != 0)
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cistatic inline void arch_spin_lock(arch_spinlock_t *lock)
198c2ecf20Sopenharmony_ci{
208c2ecf20Sopenharmony_ci	__asm__ __volatile__(
218c2ecf20Sopenharmony_ci	"\n1:\n\t"
228c2ecf20Sopenharmony_ci	"ldstub	[%0], %%g2\n\t"
238c2ecf20Sopenharmony_ci	"orcc	%%g2, 0x0, %%g0\n\t"
248c2ecf20Sopenharmony_ci	"bne,a	2f\n\t"
258c2ecf20Sopenharmony_ci	" ldub	[%0], %%g2\n\t"
268c2ecf20Sopenharmony_ci	".subsection	2\n"
278c2ecf20Sopenharmony_ci	"2:\n\t"
288c2ecf20Sopenharmony_ci	"orcc	%%g2, 0x0, %%g0\n\t"
298c2ecf20Sopenharmony_ci	"bne,a	2b\n\t"
308c2ecf20Sopenharmony_ci	" ldub	[%0], %%g2\n\t"
318c2ecf20Sopenharmony_ci	"b,a	1b\n\t"
328c2ecf20Sopenharmony_ci	".previous\n"
338c2ecf20Sopenharmony_ci	: /* no outputs */
348c2ecf20Sopenharmony_ci	: "r" (lock)
358c2ecf20Sopenharmony_ci	: "g2", "memory", "cc");
368c2ecf20Sopenharmony_ci}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic inline int arch_spin_trylock(arch_spinlock_t *lock)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	unsigned int result;
418c2ecf20Sopenharmony_ci	__asm__ __volatile__("ldstub [%1], %0"
428c2ecf20Sopenharmony_ci			     : "=r" (result)
438c2ecf20Sopenharmony_ci			     : "r" (lock)
448c2ecf20Sopenharmony_ci			     : "memory");
458c2ecf20Sopenharmony_ci	return (result == 0);
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic inline void arch_spin_unlock(arch_spinlock_t *lock)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	__asm__ __volatile__("stb %%g0, [%0]" : : "r" (lock) : "memory");
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/* Read-write spinlocks, allowing multiple readers
548c2ecf20Sopenharmony_ci * but only one writer.
558c2ecf20Sopenharmony_ci *
568c2ecf20Sopenharmony_ci * NOTE! it is quite common to have readers in interrupts
578c2ecf20Sopenharmony_ci * but no interrupt writers. For those circumstances we
588c2ecf20Sopenharmony_ci * can "mix" irq-safe locks - any writer needs to get a
598c2ecf20Sopenharmony_ci * irq-safe write-lock, but readers can get non-irqsafe
608c2ecf20Sopenharmony_ci * read-locks.
618c2ecf20Sopenharmony_ci *
628c2ecf20Sopenharmony_ci * XXX This might create some problems with my dual spinlock
638c2ecf20Sopenharmony_ci * XXX scheme, deadlocks etc. -DaveM
648c2ecf20Sopenharmony_ci *
658c2ecf20Sopenharmony_ci * Sort of like atomic_t's on Sparc, but even more clever.
668c2ecf20Sopenharmony_ci *
678c2ecf20Sopenharmony_ci *	------------------------------------
688c2ecf20Sopenharmony_ci *	| 24-bit counter           | wlock |  arch_rwlock_t
698c2ecf20Sopenharmony_ci *	------------------------------------
708c2ecf20Sopenharmony_ci *	 31                       8 7     0
718c2ecf20Sopenharmony_ci *
728c2ecf20Sopenharmony_ci * wlock signifies the one writer is in or somebody is updating
738c2ecf20Sopenharmony_ci * counter. For a writer, if he successfully acquires the wlock,
748c2ecf20Sopenharmony_ci * but counter is non-zero, he has to release the lock and wait,
758c2ecf20Sopenharmony_ci * till both counter and wlock are zero.
768c2ecf20Sopenharmony_ci *
778c2ecf20Sopenharmony_ci * Unfortunately this scheme limits us to ~16,000,000 cpus.
788c2ecf20Sopenharmony_ci */
798c2ecf20Sopenharmony_cistatic inline void __arch_read_lock(arch_rwlock_t *rw)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	register arch_rwlock_t *lp asm("g1");
828c2ecf20Sopenharmony_ci	lp = rw;
838c2ecf20Sopenharmony_ci	__asm__ __volatile__(
848c2ecf20Sopenharmony_ci	"mov	%%o7, %%g4\n\t"
858c2ecf20Sopenharmony_ci	"call	___rw_read_enter\n\t"
868c2ecf20Sopenharmony_ci	" ldstub	[%%g1 + 3], %%g2\n"
878c2ecf20Sopenharmony_ci	: /* no outputs */
888c2ecf20Sopenharmony_ci	: "r" (lp)
898c2ecf20Sopenharmony_ci	: "g2", "g4", "memory", "cc");
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci#define arch_read_lock(lock) \
938c2ecf20Sopenharmony_cido {	unsigned long flags; \
948c2ecf20Sopenharmony_ci	local_irq_save(flags); \
958c2ecf20Sopenharmony_ci	__arch_read_lock(lock); \
968c2ecf20Sopenharmony_ci	local_irq_restore(flags); \
978c2ecf20Sopenharmony_ci} while(0)
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistatic inline void __arch_read_unlock(arch_rwlock_t *rw)
1008c2ecf20Sopenharmony_ci{
1018c2ecf20Sopenharmony_ci	register arch_rwlock_t *lp asm("g1");
1028c2ecf20Sopenharmony_ci	lp = rw;
1038c2ecf20Sopenharmony_ci	__asm__ __volatile__(
1048c2ecf20Sopenharmony_ci	"mov	%%o7, %%g4\n\t"
1058c2ecf20Sopenharmony_ci	"call	___rw_read_exit\n\t"
1068c2ecf20Sopenharmony_ci	" ldstub	[%%g1 + 3], %%g2\n"
1078c2ecf20Sopenharmony_ci	: /* no outputs */
1088c2ecf20Sopenharmony_ci	: "r" (lp)
1098c2ecf20Sopenharmony_ci	: "g2", "g4", "memory", "cc");
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci#define arch_read_unlock(lock) \
1138c2ecf20Sopenharmony_cido {	unsigned long flags; \
1148c2ecf20Sopenharmony_ci	local_irq_save(flags); \
1158c2ecf20Sopenharmony_ci	__arch_read_unlock(lock); \
1168c2ecf20Sopenharmony_ci	local_irq_restore(flags); \
1178c2ecf20Sopenharmony_ci} while(0)
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic inline void arch_write_lock(arch_rwlock_t *rw)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	register arch_rwlock_t *lp asm("g1");
1228c2ecf20Sopenharmony_ci	lp = rw;
1238c2ecf20Sopenharmony_ci	__asm__ __volatile__(
1248c2ecf20Sopenharmony_ci	"mov	%%o7, %%g4\n\t"
1258c2ecf20Sopenharmony_ci	"call	___rw_write_enter\n\t"
1268c2ecf20Sopenharmony_ci	" ldstub	[%%g1 + 3], %%g2\n"
1278c2ecf20Sopenharmony_ci	: /* no outputs */
1288c2ecf20Sopenharmony_ci	: "r" (lp)
1298c2ecf20Sopenharmony_ci	: "g2", "g4", "memory", "cc");
1308c2ecf20Sopenharmony_ci	*(volatile __u32 *)&lp->lock = ~0U;
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic inline void arch_write_unlock(arch_rwlock_t *lock)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	__asm__ __volatile__(
1368c2ecf20Sopenharmony_ci"	st		%%g0, [%0]"
1378c2ecf20Sopenharmony_ci	: /* no outputs */
1388c2ecf20Sopenharmony_ci	: "r" (lock)
1398c2ecf20Sopenharmony_ci	: "memory");
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic inline int arch_write_trylock(arch_rwlock_t *rw)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	unsigned int val;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	__asm__ __volatile__("ldstub [%1 + 3], %0"
1478c2ecf20Sopenharmony_ci			     : "=r" (val)
1488c2ecf20Sopenharmony_ci			     : "r" (&rw->lock)
1498c2ecf20Sopenharmony_ci			     : "memory");
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	if (val == 0) {
1528c2ecf20Sopenharmony_ci		val = rw->lock & ~0xff;
1538c2ecf20Sopenharmony_ci		if (val)
1548c2ecf20Sopenharmony_ci			((volatile u8*)&rw->lock)[3] = 0;
1558c2ecf20Sopenharmony_ci		else
1568c2ecf20Sopenharmony_ci			*(volatile u32*)&rw->lock = ~0U;
1578c2ecf20Sopenharmony_ci	}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	return (val == 0);
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic inline int __arch_read_trylock(arch_rwlock_t *rw)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	register arch_rwlock_t *lp asm("g1");
1658c2ecf20Sopenharmony_ci	register int res asm("o0");
1668c2ecf20Sopenharmony_ci	lp = rw;
1678c2ecf20Sopenharmony_ci	__asm__ __volatile__(
1688c2ecf20Sopenharmony_ci	"mov	%%o7, %%g4\n\t"
1698c2ecf20Sopenharmony_ci	"call	___rw_read_try\n\t"
1708c2ecf20Sopenharmony_ci	" ldstub	[%%g1 + 3], %%g2\n"
1718c2ecf20Sopenharmony_ci	: "=r" (res)
1728c2ecf20Sopenharmony_ci	: "r" (lp)
1738c2ecf20Sopenharmony_ci	: "g2", "g4", "memory", "cc");
1748c2ecf20Sopenharmony_ci	return res;
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci#define arch_read_trylock(lock) \
1788c2ecf20Sopenharmony_ci({	unsigned long flags; \
1798c2ecf20Sopenharmony_ci	int res; \
1808c2ecf20Sopenharmony_ci	local_irq_save(flags); \
1818c2ecf20Sopenharmony_ci	res = __arch_read_trylock(lock); \
1828c2ecf20Sopenharmony_ci	local_irq_restore(flags); \
1838c2ecf20Sopenharmony_ci	res; \
1848c2ecf20Sopenharmony_ci})
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci#endif /* !(__ASSEMBLY__) */
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci#endif /* __SPARC_SPINLOCK_H */
189