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
9 #include <linux/threads.h> /* For NR_CPUS */
15 #ifdef CONFIG_DEBUG_SPINLOCK
16 struct _spinlock_debug {
18 unsigned long owner_pc;
20 unsigned int break_lock;
23 typedef struct _spinlock_debug spinlock_t;
25 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0, 0 }
26 #define spin_lock_init(lp) do { *(lp)= SPIN_LOCK_UNLOCKED; } while(0)
27 #define spin_is_locked(lp) (*((volatile unsigned char *)(&((lp)->lock))) != 0)
28 #define spin_unlock_wait(lp) do { barrier(); } while(*(volatile unsigned char *)(&(lp)->lock))
30 extern void _do_spin_lock(spinlock_t *lock, char *str);
31 extern int _spin_trylock(spinlock_t *lock);
32 extern void _do_spin_unlock(spinlock_t *lock);
34 #define _raw_spin_trylock(lp) _spin_trylock(lp)
35 #define _raw_spin_lock(lock) _do_spin_lock(lock, "spin_lock")
36 #define _raw_spin_unlock(lock) _do_spin_unlock(lock)
38 struct _rwlock_debug {
39 volatile unsigned int lock;
40 unsigned long owner_pc;
41 unsigned long reader_pc[NR_CPUS];
43 unsigned int break_lock;
46 typedef struct _rwlock_debug rwlock_t;
48 #define RW_LOCK_UNLOCKED (rwlock_t) { 0, 0, {0} }
50 #define rwlock_init(lp) do { *(lp)= RW_LOCK_UNLOCKED; } while(0)
52 extern void _do_read_lock(rwlock_t *rw, char *str);
53 extern void _do_read_unlock(rwlock_t *rw, char *str);
54 extern void _do_write_lock(rwlock_t *rw, char *str);
55 extern void _do_write_unlock(rwlock_t *rw);
57 #define _raw_read_lock(lock) \
58 do { unsigned long flags; \
59 local_irq_save(flags); \
60 _do_read_lock(lock, "read_lock"); \
61 local_irq_restore(flags); \
64 #define _raw_read_unlock(lock) \
65 do { unsigned long flags; \
66 local_irq_save(flags); \
67 _do_read_unlock(lock, "read_unlock"); \
68 local_irq_restore(flags); \
71 #define _raw_write_lock(lock) \
72 do { unsigned long flags; \
73 local_irq_save(flags); \
74 _do_write_lock(lock, "write_lock"); \
75 local_irq_restore(flags); \
78 #define _raw_write_unlock(lock) \
79 do { unsigned long flags; \
80 local_irq_save(flags); \
81 _do_write_unlock(lock); \
82 local_irq_restore(flags); \
85 #else /* !CONFIG_DEBUG_SPINLOCK */
90 unsigned int break_lock;
94 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0 }
96 #define spin_lock_init(lock) (*((unsigned char *)(lock)) = 0)
97 #define spin_is_locked(lock) (*((volatile unsigned char *)(lock)) != 0)
99 #define spin_unlock_wait(lock) \
102 } while(*((volatile unsigned char *)lock))
104 extern __inline__ void _raw_spin_lock(spinlock_t *lock)
106 __asm__ __volatile__(
108 "ldstub [%0], %%g2\n\t"
109 "orcc %%g2, 0x0, %%g0\n\t"
111 " ldub [%0], %%g2\n\t"
114 "orcc %%g2, 0x0, %%g0\n\t"
116 " ldub [%0], %%g2\n\t"
121 : "g2", "memory", "cc");
124 extern __inline__ int _raw_spin_trylock(spinlock_t *lock)
127 __asm__ __volatile__("ldstub [%1], %0"
131 return (result == 0);
134 extern __inline__ void _raw_spin_unlock(spinlock_t *lock)
136 __asm__ __volatile__("stb %%g0, [%0]" : : "r" (lock) : "memory");
139 /* Read-write spinlocks, allowing multiple readers
140 * but only one writer.
142 * NOTE! it is quite common to have readers in interrupts
143 * but no interrupt writers. For those circumstances we
144 * can "mix" irq-safe locks - any writer needs to get a
145 * irq-safe write-lock, but readers can get non-irqsafe
148 * XXX This might create some problems with my dual spinlock
149 * XXX scheme, deadlocks etc. -DaveM
152 volatile unsigned int lock;
153 #ifdef CONFIG_PREEMPT
154 unsigned int break_lock;
158 #define RW_LOCK_UNLOCKED (rwlock_t) { 0 }
160 #define rwlock_init(lp) do { *(lp)= RW_LOCK_UNLOCKED; } while(0)
163 /* Sort of like atomic_t's on Sparc, but even more clever.
165 * ------------------------------------
166 * | 24-bit counter | wlock | rwlock_t
167 * ------------------------------------
170 * wlock signifies the one writer is in or somebody is updating
171 * counter. For a writer, if he successfully acquires the wlock,
172 * but counter is non-zero, he has to release the lock and wait,
173 * till both counter and wlock are zero.
175 * Unfortunately this scheme limits us to ~16,000,000 cpus.
177 extern __inline__ void _read_lock(rwlock_t *rw)
179 register rwlock_t *lp asm("g1");
181 __asm__ __volatile__(
183 "call ___rw_read_enter\n\t"
184 " ldstub [%%g1 + 3], %%g2\n"
187 : "g2", "g4", "memory", "cc");
190 #define _raw_read_lock(lock) \
191 do { unsigned long flags; \
192 local_irq_save(flags); \
194 local_irq_restore(flags); \
197 extern __inline__ void _read_unlock(rwlock_t *rw)
199 register rwlock_t *lp asm("g1");
201 __asm__ __volatile__(
203 "call ___rw_read_exit\n\t"
204 " ldstub [%%g1 + 3], %%g2\n"
207 : "g2", "g4", "memory", "cc");
210 #define _raw_read_unlock(lock) \
211 do { unsigned long flags; \
212 local_irq_save(flags); \
213 _read_unlock(lock); \
214 local_irq_restore(flags); \
217 extern __inline__ void _raw_write_lock(rwlock_t *rw)
219 register rwlock_t *lp asm("g1");
221 __asm__ __volatile__(
223 "call ___rw_write_enter\n\t"
224 " ldstub [%%g1 + 3], %%g2\n"
227 : "g2", "g4", "memory", "cc");
230 #define _raw_write_unlock(rw) do { (rw)->lock = 0; } while(0)
232 #endif /* CONFIG_DEBUG_SPINLOCK */
234 #define _raw_spin_lock_flags(lock, flags) _raw_spin_lock(lock)
236 #endif /* !(__ASSEMBLY__) */
238 #endif /* __SPARC_SPINLOCK_H */