1 #ifndef _X86_SPINLOCK_H_
2 #define _X86_SPINLOCK_H_
4 #include <asm/atomic.h>
5 #include <asm/rwlock.h>
7 #include <asm/processor.h>
8 #include <linux/compiler.h>
11 * Your basic SMP spinlocks, allowing only a single CPU anywhere
13 * Simple spin lock operations. There are two variants, one clears IRQ's
14 * on the local processor, one does not.
16 * These are fair FIFO ticket locks, which are currently limited to 256
19 * (the type definitions are in asm/spinlock_types.h)
23 typedef char _slock_t
;
24 # define LOCK_INS_DEC "decb"
25 # define LOCK_INS_XCH "xchgb"
26 # define LOCK_INS_MOV "movb"
27 # define LOCK_INS_CMP "cmpb"
28 # define LOCK_PTR_REG "a"
31 # define LOCK_INS_DEC "decl"
32 # define LOCK_INS_XCH "xchgl"
33 # define LOCK_INS_MOV "movl"
34 # define LOCK_INS_CMP "cmpl"
35 # define LOCK_PTR_REG "D"
38 #if defined(CONFIG_X86_32) && \
39 (defined(CONFIG_X86_OOSTORE) || defined(CONFIG_X86_PPRO_FENCE))
41 * On PPro SMP or if we are using OOSTORE, we use a locked operation to unlock
42 * (PPro errata 66, 92)
44 # define UNLOCK_LOCK_PREFIX LOCK_PREFIX
46 # define UNLOCK_LOCK_PREFIX
50 * Ticket locks are conceptually two parts, one indicating the current head of
51 * the queue, and the other indicating the current tail. The lock is acquired
52 * by atomically noting the tail and incrementing it by one (thus adding
53 * ourself to the queue and noting our position), then waiting until the head
54 * becomes equal to the the initial value of the tail.
56 * We use an xadd covering *both* parts of the lock, to increment the tail and
57 * also load the position of the head, which takes care of memory ordering
58 * issues and should be optimal for the uncontended case. Note the tail must be
59 * in the high part, because a wide xadd increment of the low part would carry
60 * up and contaminate the high part.
62 * With fewer than 2^8 possible CPUs, we can use x86's partial registers to
63 * save some instructions and make the code more elegant. There really isn't
64 * much between them in performance though, especially as locks are out of line.
67 static inline int __raw_spin_is_locked(raw_spinlock_t
*lock
)
69 int tmp
= *(volatile signed int *)(&(lock
)->slock
);
71 return (((tmp
>> 8) & 0xff) != (tmp
& 0xff));
74 static inline int __raw_spin_is_contended(raw_spinlock_t
*lock
)
76 int tmp
= *(volatile signed int *)(&(lock
)->slock
);
78 return (((tmp
>> 8) & 0xff) - (tmp
& 0xff)) > 1;
81 static inline void __raw_spin_lock(raw_spinlock_t
*lock
)
85 __asm__
__volatile__ (
86 LOCK_PREFIX
"xaddw %w0, %1\n"
92 /* don't need lfence here, because loads are in-order */
95 :"+Q" (inc
), "+m" (lock
->slock
)
100 #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
102 static inline int __raw_spin_trylock(raw_spinlock_t
*lock
)
113 "lock ; cmpxchgw %w1,%2\n\t"
117 :"=&a" (tmp
), "=Q" (new), "+m" (lock
->slock
)
124 static inline void __raw_spin_unlock(raw_spinlock_t
*lock
)
126 __asm__
__volatile__(
127 UNLOCK_LOCK_PREFIX
"incb %0"
133 static inline int __raw_spin_is_locked(raw_spinlock_t
*lock
)
135 int tmp
= *(volatile signed int *)(&(lock
)->slock
);
137 return (((tmp
>> 16) & 0xffff) != (tmp
& 0xffff));
140 static inline int __raw_spin_is_contended(raw_spinlock_t
*lock
)
142 int tmp
= *(volatile signed int *)(&(lock
)->slock
);
144 return (((tmp
>> 16) & 0xffff) - (tmp
& 0xffff)) > 1;
147 static inline void __raw_spin_lock(raw_spinlock_t
*lock
)
149 int inc
= 0x00010000;
152 __asm__
__volatile__ (
153 "lock ; xaddl %0, %1\n"
161 /* don't need lfence here, because loads are in-order */
164 :"+Q" (inc
), "+m" (lock
->slock
), "=r" (tmp
)
169 #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
171 static inline int __raw_spin_trylock(raw_spinlock_t
*lock
)
182 "addl $0x00010000, %1\n\t"
183 "lock ; cmpxchgl %1,%2\n\t"
187 :"=&a" (tmp
), "=r" (new), "+m" (lock
->slock
)
194 static inline void __raw_spin_unlock(raw_spinlock_t
*lock
)
196 __asm__
__volatile__(
197 UNLOCK_LOCK_PREFIX
"incw %0"
204 static inline void __raw_spin_unlock_wait(raw_spinlock_t
*lock
)
206 while (__raw_spin_is_locked(lock
))
211 * Read-write spinlocks, allowing multiple readers
212 * but only one writer.
214 * NOTE! it is quite common to have readers in interrupts
215 * but no interrupt writers. For those circumstances we
216 * can "mix" irq-safe locks - any writer needs to get a
217 * irq-safe write-lock, but readers can get non-irqsafe
220 * On x86, we implement read-write locks as a 32-bit counter
221 * with the high bit (sign) being the "contended" bit.
225 * read_can_lock - would read_trylock() succeed?
226 * @lock: the rwlock in question.
228 static inline int __raw_read_can_lock(raw_rwlock_t
*lock
)
230 return (int)(lock
)->lock
> 0;
234 * write_can_lock - would write_trylock() succeed?
235 * @lock: the rwlock in question.
237 static inline int __raw_write_can_lock(raw_rwlock_t
*lock
)
239 return (lock
)->lock
== RW_LOCK_BIAS
;
242 static inline void __raw_read_lock(raw_rwlock_t
*rw
)
244 asm volatile(LOCK_PREFIX
" subl $1,(%0)\n\t"
246 "call __read_lock_failed\n\t"
248 ::LOCK_PTR_REG (rw
) : "memory");
251 static inline void __raw_write_lock(raw_rwlock_t
*rw
)
253 asm volatile(LOCK_PREFIX
" subl %1,(%0)\n\t"
255 "call __write_lock_failed\n\t"
257 ::LOCK_PTR_REG (rw
), "i" (RW_LOCK_BIAS
) : "memory");
260 static inline int __raw_read_trylock(raw_rwlock_t
*lock
)
262 atomic_t
*count
= (atomic_t
*)lock
;
265 if (atomic_read(count
) >= 0)
271 static inline int __raw_write_trylock(raw_rwlock_t
*lock
)
273 atomic_t
*count
= (atomic_t
*)lock
;
275 if (atomic_sub_and_test(RW_LOCK_BIAS
, count
))
277 atomic_add(RW_LOCK_BIAS
, count
);
281 static inline void __raw_read_unlock(raw_rwlock_t
*rw
)
283 asm volatile(LOCK_PREFIX
"incl %0" :"+m" (rw
->lock
) : : "memory");
286 static inline void __raw_write_unlock(raw_rwlock_t
*rw
)
288 asm volatile(LOCK_PREFIX
"addl %1, %0"
289 : "+m" (rw
->lock
) : "i" (RW_LOCK_BIAS
) : "memory");
292 #define _raw_spin_relax(lock) cpu_relax()
293 #define _raw_read_relax(lock) cpu_relax()
294 #define _raw_write_relax(lock) cpu_relax()