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/threads.h> /* For NR_CPUS */
13 /* To get debugging spinlocks which detect and catch
14 * deadlock situations, set CONFIG_DEBUG_SPINLOCK
15 * and rebuild your kernel.
18 /* All of these locking primitives are expected to work properly
19 * even in an RMO memory model, which currently is what the kernel
22 * There is another issue. Because we play games to save cycles
23 * in the non-contention case, we need to be extra careful about
24 * branch targets into the "spinning" code. They live in their
25 * own section, but the newer V9 branches have a shorter range
26 * than the traditional 32-bit sparc branch variants. The rule
27 * is that the branches that go into and out of the spinner sections
28 * must be pre-V9 branches.
31 #define __raw_spin_is_locked(lp) ((lp)->lock != 0)
33 #define __raw_spin_unlock_wait(lp) \
37 static inline void __raw_spin_lock(raw_spinlock_t *lock)
42 "1: ldstub [%1], %0\n"
43 " membar #StoreLoad | #StoreStore\n"
51 " ba,a,pt %%xcc, 1b\n"
58 static inline int __raw_spin_trylock(raw_spinlock_t *lock)
64 " membar #StoreLoad | #StoreStore"
69 return (result == 0UL);
72 static inline void __raw_spin_unlock(raw_spinlock_t *lock)
75 " membar #StoreStore | #LoadStore\n"
82 static inline void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long flags)
84 unsigned long tmp1, tmp2;
87 "1: ldstub [%2], %0\n"
88 " membar #StoreLoad | #StoreStore\n"
101 : "=&r" (tmp1), "=&r" (tmp2)
102 : "r"(lock), "r"(flags)
106 /* Multi-reader locks, these are much saner than the 32-bit Sparc ones... */
108 static void inline __read_lock(raw_rwlock_t *lock)
110 unsigned long tmp1, tmp2;
112 __asm__ __volatile__ (
116 " cas [%2], %0, %1\n"
118 " membar #StoreLoad | #StoreStore\n"
119 " bne,pn %%icc, 1b\n"
123 " membar #LoadLoad\n"
126 " ba,a,pt %%xcc, 4b\n"
128 : "=&r" (tmp1), "=&r" (tmp2)
133 static int inline __read_trylock(raw_rwlock_t *lock)
137 __asm__ __volatile__ (
139 " brlz,a,pn %0, 2f\n"
142 " cas [%2], %0, %1\n"
144 " membar #StoreLoad | #StoreStore\n"
145 " bne,pn %%icc, 1b\n"
148 : "=&r" (tmp1), "=&r" (tmp2)
155 static void inline __read_unlock(raw_rwlock_t *lock)
157 unsigned long tmp1, tmp2;
159 __asm__ __volatile__(
160 " membar #StoreLoad | #LoadLoad\n"
163 " cas [%2], %0, %1\n"
165 " bne,pn %%xcc, 1b\n"
167 : "=&r" (tmp1), "=&r" (tmp2)
172 static void inline __write_lock(raw_rwlock_t *lock)
174 unsigned long mask, tmp1, tmp2;
178 __asm__ __volatile__(
182 " cas [%2], %0, %1\n"
184 " membar #StoreLoad | #StoreStore\n"
185 " bne,pn %%icc, 1b\n"
189 " membar #LoadLoad\n"
192 " ba,a,pt %%xcc, 4b\n"
194 : "=&r" (tmp1), "=&r" (tmp2)
195 : "r" (lock), "r" (mask)
199 static void inline __write_unlock(raw_rwlock_t *lock)
201 __asm__ __volatile__(
202 " membar #LoadStore | #StoreStore\n"
209 static int inline __write_trylock(raw_rwlock_t *lock)
211 unsigned long mask, tmp1, tmp2, result;
215 __asm__ __volatile__(
220 " cas [%3], %0, %1\n"
222 " membar #StoreLoad | #StoreStore\n"
223 " bne,pn %%icc, 1b\n"
227 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (result)
228 : "r" (lock), "r" (mask)
234 #define __raw_read_lock(p) __read_lock(p)
235 #define __raw_read_trylock(p) __read_trylock(p)
236 #define __raw_read_unlock(p) __read_unlock(p)
237 #define __raw_write_lock(p) __write_lock(p)
238 #define __raw_write_unlock(p) __write_unlock(p)
239 #define __raw_write_trylock(p) __write_trylock(p)
241 #define __raw_read_can_lock(rw) (!((rw)->lock & 0x80000000UL))
242 #define __raw_write_can_lock(rw) (!(rw)->lock)
244 #endif /* !(__ASSEMBLY__) */
246 #endif /* !(__SPARC64_SPINLOCK_H) */