VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / include / asm-sparc64 / spinlock.h
1 /* spinlock.h: 64-bit Sparc spinlock support.
2  *
3  * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
4  */
5
6 #ifndef __SPARC64_SPINLOCK_H
7 #define __SPARC64_SPINLOCK_H
8
9 #include <linux/config.h>
10 #include <linux/threads.h>      /* For NR_CPUS */
11
12 #ifndef __ASSEMBLY__
13
14 /* To get debugging spinlocks which detect and catch
15  * deadlock situations, set CONFIG_DEBUG_SPINLOCK
16  * and rebuild your kernel.
17  */
18
19 /* All of these locking primitives are expected to work properly
20  * even in an RMO memory model, which currently is what the kernel
21  * runs in.
22  *
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.
30  */
31
32 #ifndef CONFIG_DEBUG_SPINLOCK
33
34 typedef unsigned char spinlock_t;
35 #define SPIN_LOCK_UNLOCKED      0
36
37 #define spin_lock_init(lock)    (*((unsigned char *)(lock)) = 0)
38 #define spin_is_locked(lock)    (*((volatile unsigned char *)(lock)) != 0)
39
40 #define spin_unlock_wait(lock)  \
41 do {    membar("#LoadLoad");    \
42 } while(*((volatile unsigned char *)lock))
43
44 /* arch/sparc64/lib/spinlock.S */
45 extern void _raw_spin_lock(spinlock_t *lock);
46
47 static __inline__ int _raw_spin_trylock(spinlock_t *lock)
48 {
49         unsigned int result;
50         __asm__ __volatile__("ldstub [%1], %0\n\t"
51                              "membar #StoreLoad | #StoreStore"
52                              : "=r" (result)
53                              : "r" (lock)
54                              : "memory");
55         return (result == 0);
56 }
57
58 static __inline__ void _raw_spin_unlock(spinlock_t *lock)
59 {
60         __asm__ __volatile__("membar    #StoreStore | #LoadStore\n\t"
61                              "stb       %%g0, [%0]"
62                              : /* No outputs */
63                              : "r" (lock)
64                              : "memory");
65 }
66
67 extern void _raw_spin_lock_flags(spinlock_t *lock, unsigned long flags);
68
69 #else /* !(CONFIG_DEBUG_SPINLOCK) */
70
71 typedef struct {
72         unsigned char lock;
73         unsigned int owner_pc, owner_cpu;
74 } spinlock_t;
75 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0, 0, 0xff }
76 #define spin_lock_init(__lock)  \
77 do {    (__lock)->lock = 0; \
78         (__lock)->owner_pc = 0; \
79         (__lock)->owner_cpu = 0xff; \
80 } while(0)
81 #define spin_is_locked(__lock)  (*((volatile unsigned char *)(&((__lock)->lock))) != 0)
82 #define spin_unlock_wait(__lock)        \
83 do { \
84         membar("#LoadLoad"); \
85 } while(*((volatile unsigned char *)(&((__lock)->lock))))
86
87 extern void _do_spin_lock (spinlock_t *lock, char *str);
88 extern void _do_spin_unlock (spinlock_t *lock);
89 extern int _spin_trylock (spinlock_t *lock);
90
91 #define _raw_spin_trylock(lp)   _spin_trylock(lp)
92 #define _raw_spin_lock(lock)    _do_spin_lock(lock, "spin_lock")
93 #define _raw_spin_unlock(lock)  _do_spin_unlock(lock)
94 #define _raw_spin_lock_flags(lock, flags) _raw_spin_lock(lock)
95
96 #endif /* CONFIG_DEBUG_SPINLOCK */
97
98 /* Multi-reader locks, these are much saner than the 32-bit Sparc ones... */
99
100 #ifndef CONFIG_DEBUG_SPINLOCK
101
102 typedef unsigned int rwlock_t;
103 #define RW_LOCK_UNLOCKED        0
104 #define rwlock_init(lp) do { *(lp) = RW_LOCK_UNLOCKED; } while(0)
105 #define rwlock_is_locked(x) (*(x) != RW_LOCK_UNLOCKED)
106
107 extern void __read_lock(rwlock_t *);
108 extern void __read_unlock(rwlock_t *);
109 extern void __write_lock(rwlock_t *);
110 extern void __write_unlock(rwlock_t *);
111 extern int __write_trylock(rwlock_t *);
112
113 #define _raw_read_lock(p)       __read_lock(p)
114 #define _raw_read_unlock(p)     __read_unlock(p)
115 #define _raw_write_lock(p)      __write_lock(p)
116 #define _raw_write_unlock(p)    __write_unlock(p)
117 #define _raw_write_trylock(p)   __write_trylock(p)
118
119 #else /* !(CONFIG_DEBUG_SPINLOCK) */
120
121 typedef struct {
122         unsigned long lock;
123         unsigned int writer_pc, writer_cpu;
124         unsigned int reader_pc[NR_CPUS];
125 } rwlock_t;
126 #define RW_LOCK_UNLOCKED        (rwlock_t) { 0, 0, 0xff, { } }
127 #define rwlock_init(lp) do { *(lp) = RW_LOCK_UNLOCKED; } while(0)
128 #define rwlock_is_locked(x) ((x)->lock != 0)
129
130 extern void _do_read_lock(rwlock_t *rw, char *str);
131 extern void _do_read_unlock(rwlock_t *rw, char *str);
132 extern void _do_write_lock(rwlock_t *rw, char *str);
133 extern void _do_write_unlock(rwlock_t *rw);
134 extern int _do_write_trylock(rwlock_t *rw, char *str);
135
136 #define _raw_read_lock(lock) \
137 do {    unsigned long flags; \
138         local_irq_save(flags); \
139         _do_read_lock(lock, "read_lock"); \
140         local_irq_restore(flags); \
141 } while(0)
142
143 #define _raw_read_unlock(lock) \
144 do {    unsigned long flags; \
145         local_irq_save(flags); \
146         _do_read_unlock(lock, "read_unlock"); \
147         local_irq_restore(flags); \
148 } while(0)
149
150 #define _raw_write_lock(lock) \
151 do {    unsigned long flags; \
152         local_irq_save(flags); \
153         _do_write_lock(lock, "write_lock"); \
154         local_irq_restore(flags); \
155 } while(0)
156
157 #define _raw_write_unlock(lock) \
158 do {    unsigned long flags; \
159         local_irq_save(flags); \
160         _do_write_unlock(lock); \
161         local_irq_restore(flags); \
162 } while(0)
163
164 #define _raw_write_trylock(lock) \
165 ({      unsigned long flags; \
166         int val; \
167         local_irq_save(flags); \
168         val = _do_write_trylock(lock, "write_trylock"); \
169         local_irq_restore(flags); \
170         val; \
171 })
172
173 #endif /* CONFIG_DEBUG_SPINLOCK */
174
175 #endif /* !(__ASSEMBLY__) */
176
177 #endif /* !(__SPARC64_SPINLOCK_H) */