1 /* spinlock.h: 64-bit Sparc spinlock support.
3 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
6 #ifndef __SPARC64_SPINLOCK_H
7 #define __SPARC64_SPINLOCK_H
9 #include <linux/config.h>
10 #include <linux/threads.h> /* For NR_CPUS */
14 /* To get debugging spinlocks which detect and catch
15 * deadlock situations, set CONFIG_DEBUG_SPINLOCK
16 * and rebuild your kernel.
19 /* All of these locking primitives are expected to work properly
20 * even in an RMO memory model, which currently is what the kernel
23 * There is another issue. Because we play games to save cycles
24 * in the non-contention case, we need to be extra careful about
25 * branch targets into the "spinning" code. They live in their
26 * own section, but the newer V9 branches have a shorter range
27 * than the traditional 32-bit sparc branch variants. The rule
28 * is that the branches that go into and out of the spinner sections
29 * must be pre-V9 branches.
32 #ifndef CONFIG_DEBUG_SPINLOCK
35 volatile unsigned char lock
;
37 unsigned int break_lock
;
40 #define SPIN_LOCK_UNLOCKED (spinlock_t) {0,}
42 #define spin_lock_init(lp) do { *(lp)= SPIN_LOCK_UNLOCKED; } while(0)
43 #define spin_is_locked(lp) ((lp)->lock != 0)
45 #define spin_unlock_wait(lp) \
46 do { membar("#LoadLoad"); \
49 static inline void _raw_spin_lock(spinlock_t
*lock
)
54 "1: ldstub [%1], %0\n"
56 " membar #StoreLoad | #StoreStore\n"
61 " ba,a,pt %%xcc, 1b\n"
68 static inline int _raw_spin_trylock(spinlock_t
*lock
)
74 " membar #StoreLoad | #StoreStore"
79 return (result
== 0UL);
82 static inline void _raw_spin_unlock(spinlock_t
*lock
)
85 " membar #StoreStore | #LoadStore\n"
92 static inline void _raw_spin_lock_flags(spinlock_t
*lock
, unsigned long flags
)
94 unsigned long tmp1
, tmp2
;
97 "1: ldstub [%2], %0\n"
99 " membar #StoreLoad | #StoreStore\n"
101 "2: rdpr %%pil, %1\n"
105 " membar #LoadLoad\n"
109 : "=&r" (tmp1
), "=&r" (tmp2
)
110 : "r"(lock
), "r"(flags
)
114 #else /* !(CONFIG_DEBUG_SPINLOCK) */
117 volatile unsigned char lock
;
118 unsigned int owner_pc
, owner_cpu
;
119 #ifdef CONFIG_PREEMPT
120 unsigned int break_lock
;
123 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0, 0, 0xff }
124 #define spin_lock_init(lp) do { *(lp)= SPIN_LOCK_UNLOCKED; } while(0)
125 #define spin_is_locked(__lock) ((__lock)->lock != 0)
126 #define spin_unlock_wait(__lock) \
128 membar("#LoadLoad"); \
129 } while((__lock)->lock)
131 extern void _do_spin_lock (spinlock_t
*lock
, char *str
);
132 extern void _do_spin_unlock (spinlock_t
*lock
);
133 extern int _do_spin_trylock (spinlock_t
*lock
);
135 #define _raw_spin_trylock(lp) _do_spin_trylock(lp)
136 #define _raw_spin_lock(lock) _do_spin_lock(lock, "spin_lock")
137 #define _raw_spin_unlock(lock) _do_spin_unlock(lock)
138 #define _raw_spin_lock_flags(lock, flags) _raw_spin_lock(lock)
140 #endif /* CONFIG_DEBUG_SPINLOCK */
142 /* Multi-reader locks, these are much saner than the 32-bit Sparc ones... */
144 #ifndef CONFIG_DEBUG_SPINLOCK
147 volatile unsigned int lock
;
148 #ifdef CONFIG_PREEMPT
149 unsigned int break_lock
;
152 #define RW_LOCK_UNLOCKED (rwlock_t) {0,}
153 #define rwlock_init(lp) do { *(lp) = RW_LOCK_UNLOCKED; } while(0)
155 static void inline __read_lock(rwlock_t
*lock
)
157 unsigned long tmp1
, tmp2
;
159 __asm__
__volatile__ (
163 " cas [%2], %0, %1\n"
165 " bne,pn %%icc, 1b\n"
166 " membar #StoreLoad | #StoreStore\n"
170 " membar #LoadLoad\n"
171 " ba,a,pt %%xcc, 4b\n"
173 : "=&r" (tmp1
), "=&r" (tmp2
)
178 static void inline __read_unlock(rwlock_t
*lock
)
180 unsigned long tmp1
, tmp2
;
182 __asm__
__volatile__(
183 " membar #StoreLoad | #LoadLoad\n"
186 " cas [%2], %0, %1\n"
188 " bne,pn %%xcc, 1b\n"
190 : "=&r" (tmp1
), "=&r" (tmp2
)
195 static void inline __write_lock(rwlock_t
*lock
)
197 unsigned long mask
, tmp1
, tmp2
;
201 __asm__
__volatile__(
205 " cas [%2], %0, %1\n"
207 " bne,pn %%icc, 1b\n"
208 " membar #StoreLoad | #StoreStore\n"
212 " membar #LoadLoad\n"
213 " ba,a,pt %%xcc, 4b\n"
215 : "=&r" (tmp1
), "=&r" (tmp2
)
216 : "r" (lock
), "r" (mask
)
220 static void inline __write_unlock(rwlock_t
*lock
)
222 __asm__
__volatile__(
223 " membar #LoadStore | #StoreStore\n"
230 static int inline __write_trylock(rwlock_t
*lock
)
232 unsigned long mask
, tmp1
, tmp2
, result
;
236 __asm__
__volatile__(
241 " cas [%3], %0, %1\n"
243 " bne,pn %%icc, 1b\n"
244 " membar #StoreLoad | #StoreStore\n"
247 : "=&r" (tmp1
), "=&r" (tmp2
), "=&r" (result
)
248 : "r" (lock
), "r" (mask
)
254 #define _raw_read_lock(p) __read_lock(p)
255 #define _raw_read_unlock(p) __read_unlock(p)
256 #define _raw_write_lock(p) __write_lock(p)
257 #define _raw_write_unlock(p) __write_unlock(p)
258 #define _raw_write_trylock(p) __write_trylock(p)
260 #else /* !(CONFIG_DEBUG_SPINLOCK) */
263 volatile unsigned long lock
;
264 unsigned int writer_pc
, writer_cpu
;
265 unsigned int reader_pc
[NR_CPUS
];
266 #ifdef CONFIG_PREEMPT
267 unsigned int break_lock
;
270 #define RW_LOCK_UNLOCKED (rwlock_t) { 0, 0, 0xff, { } }
271 #define rwlock_init(lp) do { *(lp) = RW_LOCK_UNLOCKED; } while(0)
273 extern void _do_read_lock(rwlock_t
*rw
, char *str
);
274 extern void _do_read_unlock(rwlock_t
*rw
, char *str
);
275 extern void _do_write_lock(rwlock_t
*rw
, char *str
);
276 extern void _do_write_unlock(rwlock_t
*rw
);
277 extern int _do_write_trylock(rwlock_t
*rw
, char *str
);
279 #define _raw_read_lock(lock) \
280 do { unsigned long flags; \
281 local_irq_save(flags); \
282 _do_read_lock(lock, "read_lock"); \
283 local_irq_restore(flags); \
286 #define _raw_read_unlock(lock) \
287 do { unsigned long flags; \
288 local_irq_save(flags); \
289 _do_read_unlock(lock, "read_unlock"); \
290 local_irq_restore(flags); \
293 #define _raw_write_lock(lock) \
294 do { unsigned long flags; \
295 local_irq_save(flags); \
296 _do_write_lock(lock, "write_lock"); \
297 local_irq_restore(flags); \
300 #define _raw_write_unlock(lock) \
301 do { unsigned long flags; \
302 local_irq_save(flags); \
303 _do_write_unlock(lock); \
304 local_irq_restore(flags); \
307 #define _raw_write_trylock(lock) \
308 ({ unsigned long flags; \
310 local_irq_save(flags); \
311 val = _do_write_trylock(lock, "write_trylock"); \
312 local_irq_restore(flags); \
316 #endif /* CONFIG_DEBUG_SPINLOCK */
318 #define _raw_read_trylock(lock) generic_raw_read_trylock(lock)
319 #define read_can_lock(rw) (!((rw)->lock & 0x80000000UL))
320 #define write_can_lock(rw) (!(rw)->lock)
322 #endif /* !(__ASSEMBLY__) */
324 #endif /* !(__SPARC64_SPINLOCK_H) */