1 /* spinlock.h: 32-bit Sparc spinlock support.
3 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
6 #ifndef __SPARC_SPINLOCK_H
7 #define __SPARC_SPINLOCK_H
13 #define __raw_spin_is_locked(lock) (*((volatile unsigned char *)(lock)) != 0)
15 #define __raw_spin_unlock_wait(lock) \
16 do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0)
18 static inline void __raw_spin_lock(raw_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 __raw_spin_trylock(raw_spinlock_t *lock)
41 __asm__ __volatile__("ldstub [%1], %0"
48 static inline void __raw_spin_unlock(raw_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 | raw_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 __read_lock(raw_rwlock_t *rw)
81 register raw_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 __raw_read_lock(lock) \
93 do { unsigned long flags; \
94 local_irq_save(flags); \
96 local_irq_restore(flags); \
99 static inline void __read_unlock(raw_rwlock_t *rw)
101 register raw_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 __raw_read_unlock(lock) \
113 do { unsigned long flags; \
114 local_irq_save(flags); \
115 __read_unlock(lock); \
116 local_irq_restore(flags); \
119 static inline void __raw_write_lock(raw_rwlock_t *rw)
121 register raw_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 int __raw_write_trylock(raw_rwlock_t *rw)
137 __asm__ __volatile__("ldstub [%1 + 3], %0"
143 val = rw->lock & ~0xff;
145 ((volatile u8*)&rw->lock)[3] = 0;
147 *(volatile u32*)&rw->lock = ~0U;
153 static inline int __read_trylock(raw_rwlock_t *rw)
155 register raw_rwlock_t *lp asm("g1");
156 register int res asm("o0");
158 __asm__ __volatile__(
160 "call ___rw_read_try\n\t"
161 " ldstub [%%g1 + 3], %%g2\n"
164 : "g2", "g4", "memory", "cc");
168 #define __raw_read_trylock(lock) \
169 ({ unsigned long flags; \
171 local_irq_save(flags); \
172 res = __read_trylock(lock); \
173 local_irq_restore(flags); \
177 #define __raw_write_unlock(rw) do { (rw)->lock = 0; } while(0)
179 #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
181 #define _raw_spin_relax(lock) cpu_relax()
182 #define _raw_read_relax(lock) cpu_relax()
183 #define _raw_write_relax(lock) cpu_relax()
185 #define __raw_read_can_lock(rw) (!((rw)->lock & 0xff))
186 #define __raw_write_can_lock(rw) (!(rw)->lock)
188 #endif /* !(__ASSEMBLY__) */
190 #endif /* __SPARC_SPINLOCK_H */