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>
10 * Your basic SMP spinlocks, allowing only a single CPU anywhere
12 * Simple spin lock operations. There are two variants, one clears IRQ's
13 * on the local processor, one does not.
15 * We make no fairness assumptions. They have a cost.
17 * (the type definitions are in asm/spinlock_types.h)
20 #ifdef CONFIG_PARAVIRT
21 #include <asm/paravirt.h>
23 #define CLI_STRING "cli"
24 #define STI_STRING "sti"
25 #define CLI_STI_CLOBBERS
26 #define CLI_STI_INPUT_ARGS
27 #endif /* CONFIG_PARAVIRT */
30 typedef char _slock_t;
31 # define LOCK_INS_DEC "decb"
32 # define LOCK_INS_XCH "xchgb"
33 # define LOCK_INS_MOV "movb"
34 # define LOCK_INS_CMP "cmpb"
35 # define LOCK_PTR_REG "a"
38 # define LOCK_INS_DEC "decl"
39 # define LOCK_INS_XCH "xchgl"
40 # define LOCK_INS_MOV "movl"
41 # define LOCK_INS_CMP "cmpl"
42 # define LOCK_PTR_REG "D"
45 static inline int __raw_spin_is_locked(raw_spinlock_t *lock)
47 return *(volatile _slock_t *)(&(lock)->slock) <= 0;
50 static inline void __raw_spin_lock(raw_spinlock_t *lock)
54 LOCK_PREFIX " ; " LOCK_INS_DEC " %0\n\t"
58 LOCK_INS_CMP " $0,%0\n\t"
62 : "+m" (lock->slock) : : "memory");
66 * It is easier for the lock validator if interrupts are not re-enabled
67 * in the middle of a lock-acquire. This is a performance feature anyway
70 * NOTE: there's an irqs-on section here, which normally would have to be
71 * irq-traced, but on CONFIG_TRACE_IRQFLAGS we never use this variant.
73 #ifndef CONFIG_PROVE_LOCKING
74 static inline void __raw_spin_lock_flags(raw_spinlock_t *lock,
79 LOCK_PREFIX " ; " LOCK_INS_DEC " %[slock]\n\t"
81 "testl $0x200, %[flags]\n\t"
86 LOCK_INS_CMP " $0, %[slock]\n\t"
92 LOCK_INS_CMP " $0, %[slock]\n\t"
96 : [slock] "+m" (lock->slock)
97 : [flags] "r" ((u32)flags)
99 : "memory" CLI_STI_CLOBBERS);
103 static inline int __raw_spin_trylock(raw_spinlock_t *lock)
108 LOCK_INS_XCH " %0,%1"
109 :"=q" (oldval), "+m" (lock->slock)
110 :"0" (0) : "memory");
116 * __raw_spin_unlock based on writing $1 to the low byte.
117 * This method works. Despite all the confusion.
118 * (except on PPro SMP or if we are using OOSTORE, so we use xchgb there)
119 * (PPro errata 66, 92)
121 #if defined(X86_64) || \
122 (!defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE))
124 static inline void __raw_spin_unlock(raw_spinlock_t *lock)
126 asm volatile(LOCK_INS_MOV " $1,%0" : "=m" (lock->slock) :: "memory");
131 static inline void __raw_spin_unlock(raw_spinlock_t *lock)
133 unsigned char oldval = 1;
135 asm volatile("xchgb %b0, %1"
136 : "=q" (oldval), "+m" (lock->slock)
137 : "0" (oldval) : "memory");
142 static inline void __raw_spin_unlock_wait(raw_spinlock_t *lock)
144 while (__raw_spin_is_locked(lock))
149 * Read-write spinlocks, allowing multiple readers
150 * but only one writer.
152 * NOTE! it is quite common to have readers in interrupts
153 * but no interrupt writers. For those circumstances we
154 * can "mix" irq-safe locks - any writer needs to get a
155 * irq-safe write-lock, but readers can get non-irqsafe
158 * On x86, we implement read-write locks as a 32-bit counter
159 * with the high bit (sign) being the "contended" bit.
162 static inline int __raw_read_can_lock(raw_rwlock_t *lock)
164 return (int)(lock)->lock > 0;
167 static inline int __raw_write_can_lock(raw_rwlock_t *lock)
169 return (lock)->lock == RW_LOCK_BIAS;
172 static inline void __raw_read_lock(raw_rwlock_t *rw)
174 asm volatile(LOCK_PREFIX " subl $1,(%0)\n\t"
176 "call __read_lock_failed\n\t"
178 ::LOCK_PTR_REG (rw) : "memory");
181 static inline void __raw_write_lock(raw_rwlock_t *rw)
183 asm volatile(LOCK_PREFIX " subl %1,(%0)\n\t"
185 "call __write_lock_failed\n\t"
187 ::LOCK_PTR_REG (rw), "i" (RW_LOCK_BIAS) : "memory");
190 static inline int __raw_read_trylock(raw_rwlock_t *lock)
192 atomic_t *count = (atomic_t *)lock;
195 if (atomic_read(count) >= 0)
201 static inline int __raw_write_trylock(raw_rwlock_t *lock)
203 atomic_t *count = (atomic_t *)lock;
205 if (atomic_sub_and_test(RW_LOCK_BIAS, count))
207 atomic_add(RW_LOCK_BIAS, count);
211 static inline void __raw_read_unlock(raw_rwlock_t *rw)
213 asm volatile(LOCK_PREFIX "incl %0" :"+m" (rw->lock) : : "memory");
216 static inline void __raw_write_unlock(raw_rwlock_t *rw)
218 asm volatile(LOCK_PREFIX "addl %1, %0"
219 : "+m" (rw->lock) : "i" (RW_LOCK_BIAS) : "memory");
222 #define _raw_spin_relax(lock) cpu_relax()
223 #define _raw_read_relax(lock) cpu_relax()
224 #define _raw_write_relax(lock) cpu_relax()