2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 1999, 2000, 06 by Ralf Baechle
7 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
9 #ifndef _ASM_SPINLOCK_H
10 #define _ASM_SPINLOCK_H
12 #include <asm/barrier.h>
16 * Your basic SMP spinlocks, allowing only a single CPU anywhere
19 #define __raw_spin_is_locked(x) ((x)->lock != 0)
20 #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
21 #define __raw_spin_unlock_wait(x) \
22 do { cpu_relax(); } while ((x)->lock)
25 * Simple spin lock operations. There are two variants, one clears IRQ's
26 * on the local processor, one does not.
28 * We make no fairness assumptions. They have a cost.
31 static inline void __raw_spin_lock(raw_spinlock_t *lock)
35 if (R10000_LLSC_WAR) {
37 " .set noreorder # __raw_spin_lock \n"
45 : "=m" (lock->lock), "=&r" (tmp)
50 " .set noreorder # __raw_spin_lock \n"
58 : "=m" (lock->lock), "=&r" (tmp)
66 static inline void __raw_spin_unlock(raw_spinlock_t *lock)
71 " .set noreorder # __raw_spin_unlock \n"
79 static inline unsigned int __raw_spin_trylock(raw_spinlock_t *lock)
81 unsigned int temp, res;
83 if (R10000_LLSC_WAR) {
85 " .set noreorder # __raw_spin_trylock \n"
93 : "=&r" (temp), "=m" (lock->lock), "=&r" (res)
98 " .set noreorder # __raw_spin_trylock \n"
105 : "=&r" (temp), "=m" (lock->lock), "=&r" (res)
116 * Read-write spinlocks, allowing multiple readers but only one writer.
118 * NOTE! it is quite common to have readers in interrupts but no interrupt
119 * writers. For those circumstances we can "mix" irq-safe locks - any writer
120 * needs to get a irq-safe write-lock, but readers can get non-irqsafe
125 * read_can_lock - would read_trylock() succeed?
126 * @lock: the rwlock in question.
128 #define __raw_read_can_lock(rw) ((rw)->lock >= 0)
131 * write_can_lock - would write_trylock() succeed?
132 * @lock: the rwlock in question.
134 #define __raw_write_can_lock(rw) (!(rw)->lock)
136 static inline void __raw_read_lock(raw_rwlock_t *rw)
140 if (R10000_LLSC_WAR) {
141 __asm__ __volatile__(
142 " .set noreorder # __raw_read_lock \n"
150 : "=m" (rw->lock), "=&r" (tmp)
154 __asm__ __volatile__(
155 " .set noreorder # __raw_read_lock \n"
163 : "=m" (rw->lock), "=&r" (tmp)
171 /* Note the use of sub, not subu which will make the kernel die with an
172 overflow exception if we ever try to unlock an rwlock that is already
173 unlocked or is being held by a writer. */
174 static inline void __raw_read_unlock(raw_rwlock_t *rw)
180 if (R10000_LLSC_WAR) {
181 __asm__ __volatile__(
182 "1: ll %1, %2 # __raw_read_unlock \n"
186 : "=m" (rw->lock), "=&r" (tmp)
190 __asm__ __volatile__(
191 " .set noreorder # __raw_read_unlock \n"
198 : "=m" (rw->lock), "=&r" (tmp)
204 static inline void __raw_write_lock(raw_rwlock_t *rw)
208 if (R10000_LLSC_WAR) {
209 __asm__ __volatile__(
210 " .set noreorder # __raw_write_lock \n"
218 : "=m" (rw->lock), "=&r" (tmp)
222 __asm__ __volatile__(
223 " .set noreorder # __raw_write_lock \n"
231 : "=m" (rw->lock), "=&r" (tmp)
239 static inline void __raw_write_unlock(raw_rwlock_t *rw)
243 __asm__ __volatile__(
244 " # __raw_write_unlock \n"
251 static inline int __raw_read_trylock(raw_rwlock_t *rw)
256 if (R10000_LLSC_WAR) {
257 __asm__ __volatile__(
258 " .set noreorder # __raw_read_trylock \n"
270 : "=m" (rw->lock), "=&r" (tmp), "=&r" (ret)
274 __asm__ __volatile__(
275 " .set noreorder # __raw_read_trylock \n"
287 : "=m" (rw->lock), "=&r" (tmp), "=&r" (ret)
295 static inline int __raw_write_trylock(raw_rwlock_t *rw)
300 if (R10000_LLSC_WAR) {
301 __asm__ __volatile__(
302 " .set noreorder # __raw_write_trylock \n"
314 : "=m" (rw->lock), "=&r" (tmp), "=&r" (ret)
318 __asm__ __volatile__(
319 " .set noreorder # __raw_write_trylock \n"
331 : "=m" (rw->lock), "=&r" (tmp), "=&r" (ret)
340 #define _raw_spin_relax(lock) cpu_relax()
341 #define _raw_read_relax(lock) cpu_relax()
342 #define _raw_write_relax(lock) cpu_relax()
344 #endif /* _ASM_SPINLOCK_H */