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