blob: 248a79f0eaffcd306968be3e29980e95aece7940 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -07008/*
9 * Your basic SMP spinlocks, allowing only a single CPU anywhere
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Simple spin lock operations. There are two variants, one clears IRQ's
12 * on the local processor, one does not.
13 *
14 * We make no fairness assumptions. They have a cost.
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070015 *
16 * (the type definitions are in asm/spinlock_types.h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070019#define __raw_spin_is_locked(x) \
Andi Kleen485832a2005-11-05 17:25:54 +010020 (*(volatile signed int *)(&(x)->slock) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070022#define __raw_spin_lock_string \
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 "\n1:\t" \
Andi Kleen841be8d2006-08-30 19:37:13 +020024 LOCK_PREFIX " ; decl %0\n\t" \
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 "js 2f\n" \
26 LOCK_SECTION_START("") \
27 "2:\t" \
28 "rep;nop\n\t" \
Andi Kleen485832a2005-11-05 17:25:54 +010029 "cmpl $0,%0\n\t" \
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 "jle 2b\n\t" \
31 "jmp 1b\n" \
32 LOCK_SECTION_END
33
Gerd Hoffmannd167a512006-06-26 13:56:16 +020034#define __raw_spin_lock_string_up \
35 "\n\tdecl %0"
36
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070037#define __raw_spin_unlock_string \
Andi Kleen485832a2005-11-05 17:25:54 +010038 "movl $1,%0" \
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070039 :"=m" (lock->slock) : : "memory"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070041static inline void __raw_spin_lock(raw_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Andi Kleen841be8d2006-08-30 19:37:13 +020043 asm volatile(__raw_spin_lock_string : "=m" (lock->slock) : : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070046#define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070048static inline int __raw_spin_trylock(raw_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Andi Kleen485832a2005-11-05 17:25:54 +010050 int oldval;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 __asm__ __volatile__(
Andi Kleen485832a2005-11-05 17:25:54 +010053 "xchgl %0,%1"
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070054 :"=q" (oldval), "=m" (lock->slock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 :"0" (0) : "memory");
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return oldval > 0;
58}
59
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070060static inline void __raw_spin_unlock(raw_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 __asm__ __volatile__(
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070063 __raw_spin_unlock_string
64 );
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070067#define __raw_spin_unlock_wait(lock) \
68 do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/*
71 * Read-write spinlocks, allowing multiple readers
72 * but only one writer.
73 *
74 * NOTE! it is quite common to have readers in interrupts
75 * but no interrupt writers. For those circumstances we
76 * can "mix" irq-safe locks - any writer needs to get a
77 * irq-safe write-lock, but readers can get non-irqsafe
78 * read-locks.
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070079 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * On x86, we implement read-write locks as a 32-bit counter
81 * with the high bit (sign) being the "contended" bit.
82 *
83 * The inline assembly is non-obvious. Think about it.
84 *
85 * Changed to use the same technique as rw semaphores. See
86 * semaphore.h for details. -ben
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070087 *
88 * the helpers are in arch/i386/kernel/semaphore.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070091#define __raw_read_can_lock(x) ((int)(x)->lock > 0)
92#define __raw_write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
93
94static inline void __raw_read_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 __build_read_lock(rw, "__read_lock_failed");
97}
98
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070099static inline void __raw_write_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 __build_write_lock(rw, "__write_lock_failed");
102}
103
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700104static inline int __raw_read_trylock(raw_rwlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 atomic_t *count = (atomic_t *)lock;
107 atomic_dec(count);
108 if (atomic_read(count) >= 0)
109 return 1;
110 atomic_inc(count);
111 return 0;
112}
113
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700114static inline int __raw_write_trylock(raw_rwlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 atomic_t *count = (atomic_t *)lock;
117 if (atomic_sub_and_test(RW_LOCK_BIAS, count))
118 return 1;
119 atomic_add(RW_LOCK_BIAS, count);
120 return 0;
121}
122
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700123static inline void __raw_read_unlock(raw_rwlock_t *rw)
124{
Andi Kleen841be8d2006-08-30 19:37:13 +0200125 asm volatile(LOCK_PREFIX " ; incl %0" :"=m" (rw->lock) : : "memory");
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700126}
127
128static inline void __raw_write_unlock(raw_rwlock_t *rw)
129{
Andi Kleen841be8d2006-08-30 19:37:13 +0200130 asm volatile(LOCK_PREFIX " ; addl $" RW_LOCK_BIAS_STR ",%0"
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700131 : "=m" (rw->lock) : : "memory");
132}
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#endif /* __ASM_SPINLOCK_H */