blob: d3bcebed60ca6ef4895b7a22ea139023f4a6d49f [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>
Andi Kleenfb2e2842006-09-26 10:52:32 +02007#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/compiler.h>
9
Rusty Russelld3561b72006-12-07 02:14:07 +010010#ifdef CONFIG_PARAVIRT
11#include <asm/paravirt.h>
12#else
Rusty Russell0da5db32006-09-26 10:52:39 +020013#define CLI_STRING "cli"
14#define STI_STRING "sti"
Rusty Russell139ec7c2006-12-07 02:14:08 +010015#define CLI_STI_CLOBBERS
16#define CLI_STI_INPUT_ARGS
Rusty Russelld3561b72006-12-07 02:14:07 +010017#endif /* CONFIG_PARAVIRT */
Rusty Russell0da5db32006-09-26 10:52:39 +020018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019/*
20 * Your basic SMP spinlocks, allowing only a single CPU anywhere
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070021 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * Simple spin lock operations. There are two variants, one clears IRQ's
23 * on the local processor, one does not.
24 *
25 * We make no fairness assumptions. They have a cost.
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070026 *
27 * (the type definitions are in asm/spinlock_types.h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 */
29
Andi Kleenfb2e2842006-09-26 10:52:32 +020030static inline int __raw_spin_is_locked(raw_spinlock_t *x)
31{
32 return *(volatile signed char *)(&(x)->slock) <= 0;
33}
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070035static inline void __raw_spin_lock(raw_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Andi Kleenfb2e2842006-09-26 10:52:32 +020037 asm volatile("\n1:\t"
38 LOCK_PREFIX " ; decb %0\n\t"
39 "jns 3f\n"
40 "2:\t"
41 "rep;nop\n\t"
42 "cmpb $0,%0\n\t"
43 "jle 2b\n\t"
44 "jmp 1b\n"
45 "3:\n\t"
46 : "+m" (lock->slock) : : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070049/*
50 * It is easier for the lock validator if interrupts are not re-enabled
51 * in the middle of a lock-acquire. This is a performance feature anyway
52 * so we turn it off:
Andi Kleenfb2e2842006-09-26 10:52:32 +020053 *
54 * NOTE: there's an irqs-on section here, which normally would have to be
55 * irq-traced, but on CONFIG_TRACE_IRQFLAGS we never use this variant.
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070056 */
57#ifndef CONFIG_PROVE_LOCKING
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070058static inline void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Andi Kleenfb2e2842006-09-26 10:52:32 +020060 asm volatile(
61 "\n1:\t"
Rusty Russell139ec7c2006-12-07 02:14:08 +010062 LOCK_PREFIX " ; decb %[slock]\n\t"
Andi Kleenfb2e2842006-09-26 10:52:32 +020063 "jns 5f\n"
64 "2:\t"
Rusty Russell139ec7c2006-12-07 02:14:08 +010065 "testl $0x200, %[flags]\n\t"
Andi Kleenfb2e2842006-09-26 10:52:32 +020066 "jz 4f\n\t"
Rusty Russell0da5db32006-09-26 10:52:39 +020067 STI_STRING "\n"
Andi Kleenfb2e2842006-09-26 10:52:32 +020068 "3:\t"
69 "rep;nop\n\t"
Rusty Russell139ec7c2006-12-07 02:14:08 +010070 "cmpb $0, %[slock]\n\t"
Andi Kleenfb2e2842006-09-26 10:52:32 +020071 "jle 3b\n\t"
Rusty Russell0da5db32006-09-26 10:52:39 +020072 CLI_STRING "\n\t"
Andi Kleenfb2e2842006-09-26 10:52:32 +020073 "jmp 1b\n"
74 "4:\t"
75 "rep;nop\n\t"
Rusty Russell139ec7c2006-12-07 02:14:08 +010076 "cmpb $0, %[slock]\n\t"
Andi Kleenfb2e2842006-09-26 10:52:32 +020077 "jg 1b\n\t"
78 "jmp 4b\n"
79 "5:\n\t"
Rusty Russell139ec7c2006-12-07 02:14:08 +010080 : [slock] "+m" (lock->slock)
81 : [flags] "r" (flags)
82 CLI_STI_INPUT_ARGS
83 : "memory" CLI_STI_CLOBBERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
Ingo Molnar8a25d5d2006-07-03 00:24:54 -070085#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070087static inline int __raw_spin_trylock(raw_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 char oldval;
Andi Kleenfb2e2842006-09-26 10:52:32 +020090 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 "xchgb %b0,%1"
Linus Torvaldsb862f3b2006-07-08 15:24:18 -070092 :"=q" (oldval), "+m" (lock->slock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 :"0" (0) : "memory");
94 return oldval > 0;
95}
96
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070097/*
98 * __raw_spin_unlock based on writing $1 to the low byte.
99 * This method works. Despite all the confusion.
100 * (except on PPro SMP or if we are using OOSTORE, so we use xchgb there)
101 * (PPro errata 66, 92)
102 */
103
104#if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE)
105
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700106static inline void __raw_spin_unlock(raw_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Andi Kleenfb2e2842006-09-26 10:52:32 +0200108 asm volatile("movb $1,%0" : "+m" (lock->slock) :: "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700111#else
112
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700113static 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
Andi Kleenfb2e2842006-09-26 10:52:32 +0200117 asm volatile("xchgb %b0, %1"
118 : "=q" (oldval), "+m" (lock->slock)
119 : "0" (oldval) : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700122#endif
123
Andi Kleenfb2e2842006-09-26 10:52:32 +0200124static inline void __raw_spin_unlock_wait(raw_spinlock_t *lock)
125{
126 while (__raw_spin_is_locked(lock))
127 cpu_relax();
128}
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/*
131 * Read-write spinlocks, allowing multiple readers
132 * but only one writer.
133 *
134 * NOTE! it is quite common to have readers in interrupts
135 * but no interrupt writers. For those circumstances we
136 * can "mix" irq-safe locks - any writer needs to get a
137 * irq-safe write-lock, but readers can get non-irqsafe
138 * read-locks.
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700139 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 * On x86, we implement read-write locks as a 32-bit counter
141 * with the high bit (sign) being the "contended" bit.
142 *
143 * The inline assembly is non-obvious. Think about it.
144 *
145 * Changed to use the same technique as rw semaphores. See
146 * semaphore.h for details. -ben
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700147 *
148 * the helpers are in arch/i386/kernel/semaphore.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700151/**
152 * read_can_lock - would read_trylock() succeed?
153 * @lock: the rwlock in question.
154 */
Andi Kleenfb2e2842006-09-26 10:52:32 +0200155static inline int __raw_read_can_lock(raw_rwlock_t *x)
156{
157 return (int)(x)->lock > 0;
158}
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700159
160/**
161 * write_can_lock - would write_trylock() succeed?
162 * @lock: the rwlock in question.
163 */
Andi Kleenfb2e2842006-09-26 10:52:32 +0200164static inline int __raw_write_can_lock(raw_rwlock_t *x)
165{
166 return (x)->lock == RW_LOCK_BIAS;
167}
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700168
169static inline void __raw_read_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Andi Kleenfb2e2842006-09-26 10:52:32 +0200171 asm volatile(LOCK_PREFIX " subl $1,(%0)\n\t"
172 "jns 1f\n"
173 "call __read_lock_failed\n\t"
174 "1:\n"
175 ::"a" (rw) : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700178static inline void __raw_write_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Andi Kleenfb2e2842006-09-26 10:52:32 +0200180 asm volatile(LOCK_PREFIX " subl $" RW_LOCK_BIAS_STR ",(%0)\n\t"
181 "jz 1f\n"
182 "call __write_lock_failed\n\t"
183 "1:\n"
184 ::"a" (rw) : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700187static inline int __raw_read_trylock(raw_rwlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 atomic_t *count = (atomic_t *)lock;
190 atomic_dec(count);
191 if (atomic_read(count) >= 0)
192 return 1;
193 atomic_inc(count);
194 return 0;
195}
196
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700197static inline int __raw_write_trylock(raw_rwlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 atomic_t *count = (atomic_t *)lock;
200 if (atomic_sub_and_test(RW_LOCK_BIAS, count))
201 return 1;
202 atomic_add(RW_LOCK_BIAS, count);
203 return 0;
204}
205
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700206static inline void __raw_read_unlock(raw_rwlock_t *rw)
207{
Linus Torvaldsb862f3b2006-07-08 15:24:18 -0700208 asm volatile(LOCK_PREFIX "incl %0" :"+m" (rw->lock) : : "memory");
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700209}
210
211static inline void __raw_write_unlock(raw_rwlock_t *rw)
212{
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -0800213 asm volatile(LOCK_PREFIX "addl $" RW_LOCK_BIAS_STR ", %0"
Linus Torvaldsb862f3b2006-07-08 15:24:18 -0700214 : "+m" (rw->lock) : : "memory");
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700215}
216
Martin Schwidefskyef6edc92006-09-30 23:27:43 -0700217#define _raw_spin_relax(lock) cpu_relax()
218#define _raw_read_relax(lock) cpu_relax()
219#define _raw_write_relax(lock) cpu_relax()
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221#endif /* __ASM_SPINLOCK_H */