blob: d76b7693cf1da5674a8b10ea616a4498b54a8c76 [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>
7#include <linux/config.h>
8#include <linux/compiler.h>
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010/*
11 * Your basic SMP spinlocks, allowing only a single CPU anywhere
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Simple spin lock operations. There are two variants, one clears IRQ's
14 * on the local processor, one does not.
15 *
16 * We make no fairness assumptions. They have a cost.
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070017 *
18 * (the type definitions are in asm/spinlock_types.h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 */
20
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070021#define __raw_spin_is_locked(x) \
22 (*(volatile signed char *)(&(x)->slock) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070024#define __raw_spin_lock_string \
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 "\n1:\t" \
26 "lock ; decb %0\n\t" \
27 "jns 3f\n" \
28 "2:\t" \
29 "rep;nop\n\t" \
30 "cmpb $0,%0\n\t" \
31 "jle 2b\n\t" \
32 "jmp 1b\n" \
33 "3:\n\t"
34
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070035#define __raw_spin_lock_string_flags \
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 "\n1:\t" \
37 "lock ; decb %0\n\t" \
Chuck Ebbert42c059e02006-03-23 02:59:55 -080038 "jns 5f\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 "2:\t" \
40 "testl $0x200, %1\n\t" \
Chuck Ebbert42c059e02006-03-23 02:59:55 -080041 "jz 4f\n\t" \
42 "sti\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 "3:\t" \
44 "rep;nop\n\t" \
45 "cmpb $0, %0\n\t" \
46 "jle 3b\n\t" \
47 "cli\n\t" \
48 "jmp 1b\n" \
Chuck Ebbert42c059e02006-03-23 02:59:55 -080049 "4:\t" \
50 "rep;nop\n\t" \
51 "cmpb $0, %0\n\t" \
52 "jg 1b\n\t" \
53 "jmp 4b\n" \
54 "5:\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080056#define __raw_spin_lock_string_up \
57 "\n\tdecb %0"
58
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070059static inline void __raw_spin_lock(raw_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080061 alternative_smp(
62 __raw_spin_lock_string,
63 __raw_spin_lock_string_up,
64 "=m" (lock->slock) : : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070067static inline void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -080069 alternative_smp(
70 __raw_spin_lock_string_flags,
71 __raw_spin_lock_string_up,
72 "=m" (lock->slock) : "r" (flags) : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070075static inline int __raw_spin_trylock(raw_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 char oldval;
78 __asm__ __volatile__(
79 "xchgb %b0,%1"
80 :"=q" (oldval), "=m" (lock->slock)
81 :"0" (0) : "memory");
82 return oldval > 0;
83}
84
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070085/*
86 * __raw_spin_unlock based on writing $1 to the low byte.
87 * This method works. Despite all the confusion.
88 * (except on PPro SMP or if we are using OOSTORE, so we use xchgb there)
89 * (PPro errata 66, 92)
90 */
91
92#if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE)
93
94#define __raw_spin_unlock_string \
95 "movb $1,%0" \
96 :"=m" (lock->slock) : : "memory"
97
98
99static inline void __raw_spin_unlock(raw_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 __asm__ __volatile__(
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700102 __raw_spin_unlock_string
103 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700106#else
107
108#define __raw_spin_unlock_string \
109 "xchgb %b0, %1" \
110 :"=q" (oldval), "=m" (lock->slock) \
111 :"0" (oldval) : "memory"
112
113static inline void __raw_spin_unlock(raw_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700115 char oldval = 1;
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 __asm__ __volatile__(
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700118 __raw_spin_unlock_string
119 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700122#endif
123
124#define __raw_spin_unlock_wait(lock) \
125 do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0)
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/*
128 * Read-write spinlocks, allowing multiple readers
129 * but only one writer.
130 *
131 * NOTE! it is quite common to have readers in interrupts
132 * but no interrupt writers. For those circumstances we
133 * can "mix" irq-safe locks - any writer needs to get a
134 * irq-safe write-lock, but readers can get non-irqsafe
135 * read-locks.
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700136 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 * On x86, we implement read-write locks as a 32-bit counter
138 * with the high bit (sign) being the "contended" bit.
139 *
140 * The inline assembly is non-obvious. Think about it.
141 *
142 * Changed to use the same technique as rw semaphores. See
143 * semaphore.h for details. -ben
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700144 *
145 * the helpers are in arch/i386/kernel/semaphore.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700148/**
149 * read_can_lock - would read_trylock() succeed?
150 * @lock: the rwlock in question.
151 */
152#define __raw_read_can_lock(x) ((int)(x)->lock > 0)
153
154/**
155 * write_can_lock - would write_trylock() succeed?
156 * @lock: the rwlock in question.
157 */
158#define __raw_write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
159
160static inline void __raw_read_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 __build_read_lock(rw, "__read_lock_failed");
163}
164
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700165static inline void __raw_write_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 __build_write_lock(rw, "__write_lock_failed");
168}
169
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700170static inline int __raw_read_trylock(raw_rwlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
172 atomic_t *count = (atomic_t *)lock;
173 atomic_dec(count);
174 if (atomic_read(count) >= 0)
175 return 1;
176 atomic_inc(count);
177 return 0;
178}
179
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700180static inline int __raw_write_trylock(raw_rwlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 atomic_t *count = (atomic_t *)lock;
183 if (atomic_sub_and_test(RW_LOCK_BIAS, count))
184 return 1;
185 atomic_add(RW_LOCK_BIAS, count);
186 return 0;
187}
188
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700189static inline void __raw_read_unlock(raw_rwlock_t *rw)
190{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800191 asm volatile(LOCK_PREFIX "incl %0" :"=m" (rw->lock) : : "memory");
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700192}
193
194static inline void __raw_write_unlock(raw_rwlock_t *rw)
195{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800196 asm volatile(LOCK_PREFIX "addl $" RW_LOCK_BIAS_STR ", %0"
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700197 : "=m" (rw->lock) : : "memory");
198}
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200#endif /* __ASM_SPINLOCK_H */