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>
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 #ifndef CONFIG_DEBUG_SPINLOCK
33 typedef unsigned char spinlock_t;
34 #define SPIN_LOCK_UNLOCKED 0
36 #define spin_lock_init(lock) (*((unsigned char *)(lock)) = 0)
37 #define spin_is_locked(lock) (*((volatile unsigned char *)(lock)) != 0)
39 #define spin_unlock_wait(lock) \
40 do { membar("#LoadLoad"); \
41 } while(*((volatile unsigned char *)lock))
43 extern __inline__ void spin_lock(spinlock_t *lock)
46 "1: ldstub [%0], %%g7\n"
48 " membar #StoreLoad | #StoreStore\n"
50 "2: ldub [%0], %%g7\n"
60 extern __inline__ int spin_trylock(spinlock_t *lock)
63 __asm__ __volatile__("ldstub [%1], %0\n\t"
64 "membar #StoreLoad | #StoreStore"
71 extern __inline__ void spin_unlock(spinlock_t *lock)
73 __asm__ __volatile__("membar #StoreStore | #LoadStore\n\t"
80 #else /* !(CONFIG_DEBUG_SPINLOCK) */
84 unsigned int owner_pc, owner_cpu;
86 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0, 0, 0xff }
87 #define spin_lock_init(__lock) \
88 do { (__lock)->lock = 0; \
89 (__lock)->owner_pc = 0; \
90 (__lock)->owner_cpu = 0xff; \
92 #define spin_is_locked(__lock) (*((volatile unsigned char *)(&((__lock)->lock))) != 0)
93 #define spin_unlock_wait(__lock) \
95 membar("#LoadLoad"); \
96 } while(*((volatile unsigned char *)(&((__lock)->lock))))
98 extern void _do_spin_lock (spinlock_t *lock, char *str);
99 extern void _do_spin_unlock (spinlock_t *lock);
100 extern int _spin_trylock (spinlock_t *lock);
102 #define spin_trylock(lp) _spin_trylock(lp)
103 #define spin_lock(lock) _do_spin_lock(lock, "spin_lock")
104 #define spin_unlock(lock) _do_spin_unlock(lock)
106 #endif /* CONFIG_DEBUG_SPINLOCK */
108 /* Multi-reader locks, these are much saner than the 32-bit Sparc ones... */
110 #ifndef CONFIG_DEBUG_SPINLOCK
112 typedef unsigned int rwlock_t;
113 #define RW_LOCK_UNLOCKED 0
114 #define rwlock_init(lp) do { *(lp) = RW_LOCK_UNLOCKED; } while(0)
116 extern void __read_lock(rwlock_t *);
117 extern void __read_unlock(rwlock_t *);
118 extern void __write_lock(rwlock_t *);
119 extern void __write_unlock(rwlock_t *);
121 #define read_lock(p) __read_lock(p)
122 #define read_unlock(p) __read_unlock(p)
123 #define write_lock(p) __write_lock(p)
124 #define write_unlock(p) __write_unlock(p)
126 #else /* !(CONFIG_DEBUG_SPINLOCK) */
130 unsigned int writer_pc, writer_cpu;
131 unsigned int reader_pc[4];
133 #define RW_LOCK_UNLOCKED (rwlock_t) { 0, 0, 0xff, { 0, 0, 0, 0 } }
134 #define rwlock_init(lp) do { *(lp) = RW_LOCK_UNLOCKED; } while(0)
136 extern void _do_read_lock(rwlock_t *rw, char *str);
137 extern void _do_read_unlock(rwlock_t *rw, char *str);
138 extern void _do_write_lock(rwlock_t *rw, char *str);
139 extern void _do_write_unlock(rwlock_t *rw);
141 #define read_lock(lock) \
142 do { unsigned long flags; \
143 __save_and_cli(flags); \
144 _do_read_lock(lock, "read_lock"); \
145 __restore_flags(flags); \
148 #define read_unlock(lock) \
149 do { unsigned long flags; \
150 __save_and_cli(flags); \
151 _do_read_unlock(lock, "read_unlock"); \
152 __restore_flags(flags); \
155 #define write_lock(lock) \
156 do { unsigned long flags; \
157 __save_and_cli(flags); \
158 _do_write_lock(lock, "write_lock"); \
159 __restore_flags(flags); \
162 #define write_unlock(lock) \
163 do { unsigned long flags; \
164 __save_and_cli(flags); \
165 _do_write_unlock(lock); \
166 __restore_flags(flags); \
169 #endif /* CONFIG_DEBUG_SPINLOCK */
171 #endif /* !(__ASSEMBLY__) */
173 #endif /* !(__SPARC64_SPINLOCK_H) */