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
34 typedef unsigned char spinlock_t
;
35 #define SPIN_LOCK_UNLOCKED 0
37 #define spin_lock_init(lock) (*((unsigned char *)(lock)) = 0)
38 #define spin_is_locked(lock) (*((volatile unsigned char *)(lock)) != 0)
40 #define spin_unlock_wait(lock) \
41 do { membar("#LoadLoad"); \
42 } while(*((volatile unsigned char *)lock))
44 static inline void _raw_spin_lock(spinlock_t
*lock
)
49 "1: ldstub [%1], %0\n"
51 " membar #StoreLoad | #StoreStore\n"
56 " ba,a,pt %%xcc, 1b\n"
63 static inline int _raw_spin_trylock(spinlock_t
*lock
)
69 " membar #StoreLoad | #StoreStore"
74 return (result
== 0UL);
77 static inline void _raw_spin_unlock(spinlock_t
*lock
)
80 " membar #StoreStore | #LoadStore\n"
87 static inline void _raw_spin_lock_flags(spinlock_t
*lock
, unsigned long flags
)
89 unsigned long tmp1
, tmp2
;
92 "1: ldstub [%2], %0\n"
94 " membar #StoreLoad | #StoreStore\n"
100 " membar #LoadLoad\n"
104 : "=&r" (tmp1
), "=&r" (tmp2
)
105 : "r"(lock
), "r"(flags
)
109 #else /* !(CONFIG_DEBUG_SPINLOCK) */
113 unsigned int owner_pc
, owner_cpu
;
115 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0, 0, 0xff }
116 #define spin_lock_init(__lock) \
117 do { (__lock)->lock = 0; \
118 (__lock)->owner_pc = 0; \
119 (__lock)->owner_cpu = 0xff; \
121 #define spin_is_locked(__lock) (*((volatile unsigned char *)(&((__lock)->lock))) != 0)
122 #define spin_unlock_wait(__lock) \
124 membar("#LoadLoad"); \
125 } while(*((volatile unsigned char *)(&((__lock)->lock))))
127 extern void _do_spin_lock (spinlock_t
*lock
, char *str
);
128 extern void _do_spin_unlock (spinlock_t
*lock
);
129 extern int _do_spin_trylock (spinlock_t
*lock
);
131 #define _raw_spin_trylock(lp) _do_spin_trylock(lp)
132 #define _raw_spin_lock(lock) _do_spin_lock(lock, "spin_lock")
133 #define _raw_spin_unlock(lock) _do_spin_unlock(lock)
134 #define _raw_spin_lock_flags(lock, flags) _raw_spin_lock(lock)
136 #endif /* CONFIG_DEBUG_SPINLOCK */
138 /* Multi-reader locks, these are much saner than the 32-bit Sparc ones... */
140 #ifndef CONFIG_DEBUG_SPINLOCK
142 typedef unsigned int rwlock_t
;
143 #define RW_LOCK_UNLOCKED 0
144 #define rwlock_init(lp) do { *(lp) = RW_LOCK_UNLOCKED; } while(0)
146 static void inline __read_lock(rwlock_t
*lock
)
148 unsigned long tmp1
, tmp2
;
150 __asm__
__volatile__ (
154 " cas [%2], %0, %1\n"
156 " bne,pn %%icc, 1b\n"
157 " membar #StoreLoad | #StoreStore\n"
161 " membar #LoadLoad\n"
162 " ba,a,pt %%xcc, 4b\n"
164 : "=&r" (tmp1
), "=&r" (tmp2
)
169 static void inline __read_unlock(rwlock_t
*lock
)
171 unsigned long tmp1
, tmp2
;
173 __asm__
__volatile__(
174 " membar #StoreLoad | #LoadLoad\n"
177 " cas [%2], %0, %1\n"
179 " bne,pn %%xcc, 1b\n"
181 : "=&r" (tmp1
), "=&r" (tmp2
)
186 static void inline __write_lock(rwlock_t
*lock
)
188 unsigned long mask
, tmp1
, tmp2
;
192 __asm__
__volatile__(
196 " cas [%2], %0, %1\n"
198 " bne,pn %%icc, 1b\n"
199 " membar #StoreLoad | #StoreStore\n"
203 " membar #LoadLoad\n"
204 " ba,a,pt %%xcc, 4b\n"
206 : "=&r" (tmp1
), "=&r" (tmp2
)
207 : "r" (lock
), "r" (mask
)
211 static void inline __write_unlock(rwlock_t
*lock
)
213 __asm__
__volatile__(
214 " membar #LoadStore | #StoreStore\n"
221 static int inline __write_trylock(rwlock_t
*lock
)
223 unsigned long mask
, tmp1
, tmp2
, result
;
227 __asm__
__volatile__(
232 " cas [%3], %0, %1\n"
234 " bne,pn %%icc, 1b\n"
235 " membar #StoreLoad | #StoreStore\n"
238 : "=&r" (tmp1
), "=&r" (tmp2
), "=&r" (result
)
239 : "r" (lock
), "r" (mask
)
245 #define _raw_read_lock(p) __read_lock(p)
246 #define _raw_read_unlock(p) __read_unlock(p)
247 #define _raw_write_lock(p) __write_lock(p)
248 #define _raw_write_unlock(p) __write_unlock(p)
249 #define _raw_write_trylock(p) __write_trylock(p)
251 #else /* !(CONFIG_DEBUG_SPINLOCK) */
255 unsigned int writer_pc
, writer_cpu
;
256 unsigned int reader_pc
[NR_CPUS
];
258 #define RW_LOCK_UNLOCKED (rwlock_t) { 0, 0, 0xff, { } }
259 #define rwlock_init(lp) do { *(lp) = RW_LOCK_UNLOCKED; } while(0)
261 extern void _do_read_lock(rwlock_t
*rw
, char *str
);
262 extern void _do_read_unlock(rwlock_t
*rw
, char *str
);
263 extern void _do_write_lock(rwlock_t
*rw
, char *str
);
264 extern void _do_write_unlock(rwlock_t
*rw
);
265 extern int _do_write_trylock(rwlock_t
*rw
, char *str
);
267 #define _raw_read_lock(lock) \
268 do { unsigned long flags; \
269 local_irq_save(flags); \
270 _do_read_lock(lock, "read_lock"); \
271 local_irq_restore(flags); \
274 #define _raw_read_unlock(lock) \
275 do { unsigned long flags; \
276 local_irq_save(flags); \
277 _do_read_unlock(lock, "read_unlock"); \
278 local_irq_restore(flags); \
281 #define _raw_write_lock(lock) \
282 do { unsigned long flags; \
283 local_irq_save(flags); \
284 _do_write_lock(lock, "write_lock"); \
285 local_irq_restore(flags); \
288 #define _raw_write_unlock(lock) \
289 do { unsigned long flags; \
290 local_irq_save(flags); \
291 _do_write_unlock(lock); \
292 local_irq_restore(flags); \
295 #define _raw_write_trylock(lock) \
296 ({ unsigned long flags; \
298 local_irq_save(flags); \
299 val = _do_write_trylock(lock, "write_trylock"); \
300 local_irq_restore(flags); \
304 #endif /* CONFIG_DEBUG_SPINLOCK */
306 #define _raw_read_trylock(lock) generic_raw_read_trylock(lock)
308 #endif /* !(__ASSEMBLY__) */
310 #endif /* !(__SPARC64_SPINLOCK_H) */