blob: 88bf981e73cf45c084c66d6d60c25e066fa53340 [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>
Andrew Morton2bd0cfb2006-09-27 01:49:42 -07007#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
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
Andi Kleen8b059d22006-09-26 10:52:32 +020020static inline int __raw_spin_is_locked(raw_spinlock_t *lock)
21{
22 return *(volatile signed int *)(&(lock)->slock) <= 0;
23}
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070025static inline void __raw_spin_lock(raw_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
Andi Kleen8b059d22006-09-26 10:52:32 +020027 asm volatile(
28 "\n1:\t"
29 LOCK_PREFIX " ; decl %0\n\t"
30 "jns 2f\n"
31 "3:\n"
32 "rep;nop\n\t"
33 "cmpl $0,%0\n\t"
34 "jle 3b\n\t"
35 "jmp 1b\n"
36 "2:\t" : "=m" (lock->slock) : : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38
Andi Kleen87e16522006-12-07 02:14:00 +010039/*
40 * Same as __raw_spin_lock, but reenable interrupts during spinning.
41 */
42#ifndef CONFIG_PROVE_LOCKING
43static inline void __raw_spin_lock_flags(raw_spinlock_t *lock, unsigned long flags)
44{
45 asm volatile(
46 "\n1:\t"
47 LOCK_PREFIX " ; decl %0\n\t"
48 "jns 5f\n"
49 "testl $0x200, %1\n\t" /* interrupts were disabled? */
50 "jz 4f\n\t"
51 "sti\n"
52 "3:\t"
53 "rep;nop\n\t"
54 "cmpl $0, %0\n\t"
55 "jle 3b\n\t"
56 "cli\n\t"
57 "jmp 1b\n"
58 "4:\t"
59 "rep;nop\n\t"
60 "cmpl $0, %0\n\t"
61 "jg 1b\n\t"
62 "jmp 4b\n"
63 "5:\n\t"
64 : "+m" (lock->slock) : "r" ((unsigned)flags) : "memory");
65}
66#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070068static inline int __raw_spin_trylock(raw_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Andi Kleen485832a2005-11-05 17:25:54 +010070 int oldval;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070071
Andi Kleen8b059d22006-09-26 10:52:32 +020072 asm volatile(
Andi Kleen485832a2005-11-05 17:25:54 +010073 "xchgl %0,%1"
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070074 :"=q" (oldval), "=m" (lock->slock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 :"0" (0) : "memory");
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return oldval > 0;
78}
79
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070080static inline void __raw_spin_unlock(raw_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Andi Kleen8b059d22006-09-26 10:52:32 +020082 asm volatile("movl $1,%0" :"=m" (lock->slock) :: "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Andi Kleen8b059d22006-09-26 10:52:32 +020085static inline void __raw_spin_unlock_wait(raw_spinlock_t *lock)
86{
87 while (__raw_spin_is_locked(lock))
88 cpu_relax();
89}
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*
92 * Read-write spinlocks, allowing multiple readers
93 * but only one writer.
94 *
95 * NOTE! it is quite common to have readers in interrupts
96 * but no interrupt writers. For those circumstances we
97 * can "mix" irq-safe locks - any writer needs to get a
98 * irq-safe write-lock, but readers can get non-irqsafe
99 * read-locks.
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700100 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * On x86, we implement read-write locks as a 32-bit counter
102 * with the high bit (sign) being the "contended" bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Andi Kleen8b059d22006-09-26 10:52:32 +0200105static inline int __raw_read_can_lock(raw_rwlock_t *lock)
106{
107 return (int)(lock)->lock > 0;
108}
109
110static inline int __raw_write_can_lock(raw_rwlock_t *lock)
111{
112 return (lock)->lock == RW_LOCK_BIAS;
113}
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700114
115static inline void __raw_read_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Andi Kleen8b059d22006-09-26 10:52:32 +0200117 asm volatile(LOCK_PREFIX "subl $1,(%0)\n\t"
118 "jns 1f\n"
119 "call __read_lock_failed\n"
120 "1:\n"
121 ::"D" (rw), "i" (RW_LOCK_BIAS) : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700124static inline void __raw_write_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Andi Kleen8b059d22006-09-26 10:52:32 +0200126 asm volatile(LOCK_PREFIX "subl %1,(%0)\n\t"
127 "jz 1f\n"
128 "\tcall __write_lock_failed\n\t"
129 "1:\n"
130 ::"D" (rw), "i" (RW_LOCK_BIAS) : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700133static inline int __raw_read_trylock(raw_rwlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 atomic_t *count = (atomic_t *)lock;
136 atomic_dec(count);
137 if (atomic_read(count) >= 0)
138 return 1;
139 atomic_inc(count);
140 return 0;
141}
142
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700143static inline int __raw_write_trylock(raw_rwlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 atomic_t *count = (atomic_t *)lock;
146 if (atomic_sub_and_test(RW_LOCK_BIAS, count))
147 return 1;
148 atomic_add(RW_LOCK_BIAS, count);
149 return 0;
150}
151
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700152static inline void __raw_read_unlock(raw_rwlock_t *rw)
153{
Andi Kleen841be8d2006-08-30 19:37:13 +0200154 asm volatile(LOCK_PREFIX " ; incl %0" :"=m" (rw->lock) : : "memory");
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700155}
156
157static inline void __raw_write_unlock(raw_rwlock_t *rw)
158{
Andi Kleen841be8d2006-08-30 19:37:13 +0200159 asm volatile(LOCK_PREFIX " ; addl $" RW_LOCK_BIAS_STR ",%0"
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700160 : "=m" (rw->lock) : : "memory");
161}
162
Martin Schwidefskyef6edc92006-09-30 23:27:43 -0700163#define _raw_spin_relax(lock) cpu_relax()
164#define _raw_read_relax(lock) cpu_relax()
165#define _raw_write_relax(lock) cpu_relax()
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#endif /* __ASM_SPINLOCK_H */