patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / include / asm-i386 / spinlock.h
1 #ifndef __ASM_SPINLOCK_H
2 #define __ASM_SPINLOCK_H
3
4 #include <asm/atomic.h>
5 #include <asm/rwlock.h>
6 #include <asm/page.h>
7 #include <linux/config.h>
8 #include <linux/compiler.h>
9
10 asmlinkage int printk(const char * fmt, ...)
11         __attribute__ ((format (printf, 1, 2)));
12
13 /*
14  * Your basic SMP spinlocks, allowing only a single CPU anywhere
15  */
16
17 typedef struct {
18         volatile unsigned int lock;
19 #ifdef CONFIG_DEBUG_SPINLOCK
20         unsigned magic;
21 #endif
22 } spinlock_t;
23
24 #define SPINLOCK_MAGIC  0xdead4ead
25
26 #ifdef CONFIG_DEBUG_SPINLOCK
27 #define SPINLOCK_MAGIC_INIT     , SPINLOCK_MAGIC
28 #else
29 #define SPINLOCK_MAGIC_INIT     /* */
30 #endif
31
32 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 1 SPINLOCK_MAGIC_INIT }
33
34 #define spin_lock_init(x)       do { *(x) = SPIN_LOCK_UNLOCKED; } while(0)
35
36 /*
37  * Simple spin lock operations.  There are two variants, one clears IRQ's
38  * on the local processor, one does not.
39  *
40  * We make no fairness assumptions. They have a cost.
41  */
42
43 #define spin_is_locked(x)       (*(volatile signed char *)(&(x)->lock) <= 0)
44 #define spin_unlock_wait(x)     do { barrier(); } while(spin_is_locked(x))
45 #define _raw_spin_lock_flags(lock, flags) _raw_spin_lock(lock)
46
47 #define spin_lock_string \
48         "\n1:\t" \
49         "lock ; decb %0\n\t" \
50         "js 2f\n" \
51         LOCK_SECTION_START("") \
52         "2:\t" \
53         "rep;nop\n\t" \
54         "cmpb $0,%0\n\t" \
55         "jle 2b\n\t" \
56         "jmp 1b\n" \
57         LOCK_SECTION_END
58
59 /*
60  * This works. Despite all the confusion.
61  * (except on PPro SMP or if we are using OOSTORE)
62  * (PPro errata 66, 92)
63  */
64  
65 #if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE)
66
67 #define spin_unlock_string \
68         "movb $1,%0" \
69                 :"=m" (lock->lock) : : "memory"
70
71
72 static inline void _raw_spin_unlock(spinlock_t *lock)
73 {
74 #ifdef CONFIG_DEBUG_SPINLOCK
75         BUG_ON(lock->magic != SPINLOCK_MAGIC);
76         BUG_ON(!spin_is_locked(lock));
77 #endif
78         __asm__ __volatile__(
79                 spin_unlock_string
80         );
81 }
82
83 #else
84
85 #define spin_unlock_string \
86         "xchgb %b0, %1" \
87                 :"=q" (oldval), "=m" (lock->lock) \
88                 :"0" (oldval) : "memory"
89
90 static inline void _raw_spin_unlock(spinlock_t *lock)
91 {
92         char oldval = 1;
93 #ifdef CONFIG_DEBUG_SPINLOCK
94         BUG_ON(lock->magic != SPINLOCK_MAGIC);
95         BUG_ON(!spin_is_locked(lock));
96 #endif
97         __asm__ __volatile__(
98                 spin_unlock_string
99         );
100 }
101
102 #endif
103
104 static inline int _raw_spin_trylock(spinlock_t *lock)
105 {
106         char oldval;
107         __asm__ __volatile__(
108                 "xchgb %b0,%1"
109                 :"=q" (oldval), "=m" (lock->lock)
110                 :"0" (0) : "memory");
111         return oldval > 0;
112 }
113
114 static inline void _raw_spin_lock(spinlock_t *lock)
115 {
116 #ifdef CONFIG_DEBUG_SPINLOCK
117         __label__ here;
118 here:
119         if (unlikely(lock->magic != SPINLOCK_MAGIC)) {
120                 printk("eip: %p\n", &&here);
121                 BUG();
122         }
123 #endif
124         __asm__ __volatile__(
125                 spin_lock_string
126                 :"=m" (lock->lock) : : "memory");
127 }
128
129
130 /*
131  * Read-write spinlocks, allowing multiple readers
132  * but only one writer.
133  *
134  * NOTE! it is quite common to have readers in interrupts
135  * but no interrupt writers. For those circumstances we
136  * can "mix" irq-safe locks - any writer needs to get a
137  * irq-safe write-lock, but readers can get non-irqsafe
138  * read-locks.
139  */
140 typedef struct {
141         volatile unsigned int lock;
142 #ifdef CONFIG_DEBUG_SPINLOCK
143         unsigned magic;
144 #endif
145 } rwlock_t;
146
147 #define RWLOCK_MAGIC    0xdeaf1eed
148
149 #ifdef CONFIG_DEBUG_SPINLOCK
150 #define RWLOCK_MAGIC_INIT       , RWLOCK_MAGIC
151 #else
152 #define RWLOCK_MAGIC_INIT       /* */
153 #endif
154
155 #define RW_LOCK_UNLOCKED (rwlock_t) { RW_LOCK_BIAS RWLOCK_MAGIC_INIT }
156
157 #define rwlock_init(x)  do { *(x) = RW_LOCK_UNLOCKED; } while(0)
158
159 #define rwlock_is_locked(x) ((x)->lock != RW_LOCK_BIAS)
160
161 /*
162  * On x86, we implement read-write locks as a 32-bit counter
163  * with the high bit (sign) being the "contended" bit.
164  *
165  * The inline assembly is non-obvious. Think about it.
166  *
167  * Changed to use the same technique as rw semaphores.  See
168  * semaphore.h for details.  -ben
169  */
170 /* the spinlock helpers are in arch/i386/kernel/semaphore.c */
171
172 static inline void _raw_read_lock(rwlock_t *rw)
173 {
174 #ifdef CONFIG_DEBUG_SPINLOCK
175         BUG_ON(rw->magic != RWLOCK_MAGIC);
176 #endif
177         __build_read_lock(rw, "__read_lock_failed");
178 }
179
180 static inline void _raw_write_lock(rwlock_t *rw)
181 {
182 #ifdef CONFIG_DEBUG_SPINLOCK
183         BUG_ON(rw->magic != RWLOCK_MAGIC);
184 #endif
185         __build_write_lock(rw, "__write_lock_failed");
186 }
187
188 #define _raw_read_unlock(rw)            asm volatile("lock ; incl %0" :"=m" ((rw)->lock) : : "memory")
189 #define _raw_write_unlock(rw)   asm volatile("lock ; addl $" RW_LOCK_BIAS_STR ",%0":"=m" ((rw)->lock) : : "memory")
190
191 static inline int _raw_write_trylock(rwlock_t *lock)
192 {
193         atomic_t *count = (atomic_t *)lock;
194         if (atomic_sub_and_test(RW_LOCK_BIAS, count))
195                 return 1;
196         atomic_add(RW_LOCK_BIAS, count);
197         return 0;
198 }
199
200 #endif /* __ASM_SPINLOCK_H */