blob: d1020363c41ab744cebcb2ae4896849fbe10c6b2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/compiler.h>
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009/*
10 * Your basic SMP spinlocks, allowing only a single CPU anywhere
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Simple spin lock operations. There are two variants, one clears IRQ's
13 * on the local processor, one does not.
14 *
15 * We make no fairness assumptions. They have a cost.
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070016 *
17 * (the type definitions are in asm/spinlock_types.h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070020#define __raw_spin_is_locked(x) \
21 (*(volatile signed char *)(&(x)->slock) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070023#define __raw_spin_lock_string \
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 "\n1:\t" \
Andi Kleen8c749322006-08-30 19:37:14 +020025 LOCK_PREFIX " ; decb %0\n\t" \
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 "jns 3f\n" \
27 "2:\t" \
28 "rep;nop\n\t" \
29 "cmpb $0,%0\n\t" \
30 "jle 2b\n\t" \
31 "jmp 1b\n" \
32 "3:\n\t"
33
Ingo Molnar55f327f2006-07-03 00:24:43 -070034/*
35 * NOTE: there's an irqs-on section here, which normally would have to be
36 * irq-traced, but on CONFIG_TRACE_IRQFLAGS we never use
37 * __raw_spin_lock_string_flags().
38 */
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070039#define __raw_spin_lock_string_flags \
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 "\n1:\t" \
Andi Kleen8c749322006-08-30 19:37:14 +020041 LOCK_PREFIX " ; decb %0\n\t" \
Chuck Ebbert42c059e02006-03-23 02:59:55 -080042 "jns 5f\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 "2:\t" \
44 "testl $0x200, %1\n\t" \
Chuck Ebbert42c059e02006-03-23 02:59:55 -080045 "jz 4f\n\t" \
46 "sti\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 "3:\t" \
48 "rep;nop\n\t" \
49 "cmpb $0, %0\n\t" \
50 "jle 3b\n\t" \
51 "cli\n\t" \
52 "jmp 1b\n" \
Chuck Ebbert42c059e02006-03-23 02:59:55 -080053 "4:\t" \
54 "rep;nop\n\t" \
55 "cmpb $0, %0\n\t" \
56 "jg 1b\n\t" \
57 "jmp 4b\n" \
58 "5:\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070060static inline void __raw_spin_lock(raw_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Andi Kleen8c749322006-08-30 19:37:14 +020062 asm(__raw_spin_lock_string : "+m" (lock->slock) : : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070065/*
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
68 * so we turn it off:
69 */
70#ifndef CONFIG_PROVE_LOCKING
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070071static inline void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Andi Kleen8c749322006-08-30 19:37:14 +020073 asm(__raw_spin_lock_string_flags : "+m" (lock->slock) : "r" (flags) : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070075#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070077static inline int __raw_spin_trylock(raw_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 char oldval;
80 __asm__ __volatile__(
81 "xchgb %b0,%1"
Linus Torvaldsb862f3b2006-07-08 15:24:18 -070082 :"=q" (oldval), "+m" (lock->slock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 :"0" (0) : "memory");
84 return oldval > 0;
85}
86
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070087/*
88 * __raw_spin_unlock based on writing $1 to the low byte.
89 * This method works. Despite all the confusion.
90 * (except on PPro SMP or if we are using OOSTORE, so we use xchgb there)
91 * (PPro errata 66, 92)
92 */
93
94#if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE)
95
96#define __raw_spin_unlock_string \
97 "movb $1,%0" \
Linus Torvaldsb862f3b2006-07-08 15:24:18 -070098 :"+m" (lock->slock) : : "memory"
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070099
100
101static inline void __raw_spin_unlock(raw_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 __asm__ __volatile__(
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700104 __raw_spin_unlock_string
105 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700108#else
109
110#define __raw_spin_unlock_string \
111 "xchgb %b0, %1" \
Linus Torvaldsb862f3b2006-07-08 15:24:18 -0700112 :"=q" (oldval), "+m" (lock->slock) \
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700113 :"0" (oldval) : "memory"
114
115static inline void __raw_spin_unlock(raw_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700117 char oldval = 1;
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 __asm__ __volatile__(
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700120 __raw_spin_unlock_string
121 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700124#endif
125
126#define __raw_spin_unlock_wait(lock) \
127 do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0)
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129/*
130 * Read-write spinlocks, allowing multiple readers
131 * but only one writer.
132 *
133 * NOTE! it is quite common to have readers in interrupts
134 * but no interrupt writers. For those circumstances we
135 * can "mix" irq-safe locks - any writer needs to get a
136 * irq-safe write-lock, but readers can get non-irqsafe
137 * read-locks.
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700138 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 * On x86, we implement read-write locks as a 32-bit counter
140 * with the high bit (sign) being the "contended" bit.
141 *
142 * The inline assembly is non-obvious. Think about it.
143 *
144 * Changed to use the same technique as rw semaphores. See
145 * semaphore.h for details. -ben
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700146 *
147 * the helpers are in arch/i386/kernel/semaphore.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700150/**
151 * read_can_lock - would read_trylock() succeed?
152 * @lock: the rwlock in question.
153 */
154#define __raw_read_can_lock(x) ((int)(x)->lock > 0)
155
156/**
157 * write_can_lock - would write_trylock() succeed?
158 * @lock: the rwlock in question.
159 */
160#define __raw_write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
161
162static inline void __raw_read_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 __build_read_lock(rw, "__read_lock_failed");
165}
166
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700167static inline void __raw_write_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 __build_write_lock(rw, "__write_lock_failed");
170}
171
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700172static inline int __raw_read_trylock(raw_rwlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 atomic_t *count = (atomic_t *)lock;
175 atomic_dec(count);
176 if (atomic_read(count) >= 0)
177 return 1;
178 atomic_inc(count);
179 return 0;
180}
181
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700182static inline int __raw_write_trylock(raw_rwlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 atomic_t *count = (atomic_t *)lock;
185 if (atomic_sub_and_test(RW_LOCK_BIAS, count))
186 return 1;
187 atomic_add(RW_LOCK_BIAS, count);
188 return 0;
189}
190
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700191static inline void __raw_read_unlock(raw_rwlock_t *rw)
192{
Linus Torvaldsb862f3b2006-07-08 15:24:18 -0700193 asm volatile(LOCK_PREFIX "incl %0" :"+m" (rw->lock) : : "memory");
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700194}
195
196static inline void __raw_write_unlock(raw_rwlock_t *rw)
197{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800198 asm volatile(LOCK_PREFIX "addl $" RW_LOCK_BIAS_STR ", %0"
Linus Torvaldsb862f3b2006-07-08 15:24:18 -0700199 : "+m" (rw->lock) : : "memory");
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700200}
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202#endif /* __ASM_SPINLOCK_H */