ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-mips / spinlock.h
1 /*
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
4  * for more details.
5  *
6  * Copyright (C) 1999, 2000 by Ralf Baechle
7  * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
8  */
9 #ifndef _ASM_SPINLOCK_H
10 #define _ASM_SPINLOCK_H
11
12 /*
13  * Your basic SMP spinlocks, allowing only a single CPU anywhere
14  */
15
16 typedef struct {
17         volatile unsigned int lock;
18 } spinlock_t;
19
20 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0 }
21
22 #define spin_lock_init(x)       do { (x)->lock = 0; } while(0)
23
24 #define spin_is_locked(x)       ((x)->lock != 0)
25 #define spin_unlock_wait(x)     do { barrier(); } while ((x)->lock)
26
27 /*
28  * Simple spin lock operations.  There are two variants, one clears IRQ's
29  * on the local processor, one does not.
30  *
31  * We make no fairness assumptions.  They have a cost.
32  */
33
34 static inline void _raw_spin_lock(spinlock_t *lock)
35 {
36         unsigned int tmp;
37
38         __asm__ __volatile__(
39         ".set\tnoreorder\t\t\t# _raw_spin_lock\n"
40         "1:\tll\t%1, %2\n\t"
41         "bnez\t%1, 1b\n\t"
42         " li\t%1, 1\n\t"
43         "sc\t%1, %0\n\t"
44         "beqz\t%1, 1b\n\t"
45         " sync\n\t"
46         ".set\treorder"
47         : "=m" (lock->lock), "=&r" (tmp)
48         : "m" (lock->lock)
49         : "memory");
50 }
51
52 static inline void _raw_spin_unlock(spinlock_t *lock)
53 {
54         __asm__ __volatile__(
55         ".set\tnoreorder\t\t\t# _raw_spin_unlock\n\t"
56         "sync\n\t"
57         "sw\t$0, %0\n\t"
58         ".set\treorder"
59         : "=m" (lock->lock)
60         : "m" (lock->lock)
61         : "memory");
62 }
63
64 static inline unsigned int _raw_spin_trylock(spinlock_t *lock)
65 {
66         unsigned int temp, res;
67
68         __asm__ __volatile__(
69         ".set\tnoreorder\t\t\t# _raw_spin_trylock\n\t"
70         "1:\tll\t%0, %3\n\t"
71         "ori\t%2, %0, 1\n\t"
72         "sc\t%2, %1\n\t"
73         "beqz\t%2, 1b\n\t"
74         " andi\t%2, %0, 1\n\t"
75         ".set\treorder"
76         : "=&r" (temp), "=m" (lock->lock), "=&r" (res)
77         : "m" (lock->lock)
78         : "memory");
79
80         return res == 0;
81 }
82
83 /*
84  * Read-write spinlocks, allowing multiple readers but only one writer.
85  *
86  * NOTE! it is quite common to have readers in interrupts but no interrupt
87  * writers. For those circumstances we can "mix" irq-safe locks - any writer
88  * needs to get a irq-safe write-lock, but readers can get non-irqsafe
89  * read-locks.
90  */
91
92 typedef struct {
93         volatile unsigned int lock;
94 } rwlock_t;
95
96 #define RW_LOCK_UNLOCKED (rwlock_t) { 0 }
97
98 #define rwlock_init(x)  do { *(x) = RW_LOCK_UNLOCKED; } while(0)
99
100 #define rwlock_is_locked(x) ((x)->lock)
101
102 static inline void _raw_read_lock(rwlock_t *rw)
103 {
104         unsigned int tmp;
105
106         __asm__ __volatile__(
107         ".set\tnoreorder\t\t\t# _raw_read_lock\n"
108         "1:\tll\t%1, %2\n\t"
109         "bltz\t%1, 1b\n\t"
110         " addu\t%1, 1\n\t"
111         "sc\t%1, %0\n\t"
112         "beqz\t%1, 1b\n\t"
113         " sync\n\t"
114         ".set\treorder"
115         : "=m" (rw->lock), "=&r" (tmp)
116         : "m" (rw->lock)
117         : "memory");
118 }
119
120 /* Note the use of sub, not subu which will make the kernel die with an
121    overflow exception if we ever try to unlock an rwlock that is already
122    unlocked or is being held by a writer.  */
123 static inline void _raw_read_unlock(rwlock_t *rw)
124 {
125         unsigned int tmp;
126
127         __asm__ __volatile__(
128         ".set\tnoreorder\t\t\t# _raw_read_unlock\n"
129         "1:\tll\t%1, %2\n\t"
130         "sub\t%1, 1\n\t"
131         "sc\t%1, %0\n\t"
132         "beqz\t%1, 1b\n\t"
133         " sync\n\t"
134         ".set\treorder"
135         : "=m" (rw->lock), "=&r" (tmp)
136         : "m" (rw->lock)
137         : "memory");
138 }
139
140 static inline void _raw_write_lock(rwlock_t *rw)
141 {
142         unsigned int tmp;
143
144         __asm__ __volatile__(
145         ".set\tnoreorder\t\t\t# _raw_write_lock\n"
146         "1:\tll\t%1, %2\n\t"
147         "bnez\t%1, 1b\n\t"
148         " lui\t%1, 0x8000\n\t"
149         "sc\t%1, %0\n\t"
150         "beqz\t%1, 1b\n\t"
151         " sync\n\t"
152         ".set\treorder"
153         : "=m" (rw->lock), "=&r" (tmp)
154         : "m" (rw->lock)
155         : "memory");
156 }
157
158 static inline void _raw_write_unlock(rwlock_t *rw)
159 {
160         __asm__ __volatile__(
161         ".set\tnoreorder\t\t\t# _raw_write_unlock\n\t"
162         "sync\n\t"
163         "sw\t$0, %0\n\t"
164         ".set\treorder"
165         : "=m" (rw->lock)
166         : "m" (rw->lock)
167         : "memory");
168 }
169
170 static inline int _raw_write_trylock(rwlock_t *rw)
171 {
172         unsigned int tmp;
173         int ret;
174
175         __asm__ __volatile__(
176         ".set\tnoreorder\t\t\t# _raw_write_trylock\n"
177         "li\t%2, 0\n\t"
178         "1:\tll\t%1, %3\n\t"
179         "bnez\t%1, 2f\n\t"
180         "lui\t%1, 0x8000\n\t"
181         "sc\t%1, %0\n\t"
182         "beqz\t%1, 1b\n\t"
183         "sync\n\t"
184         "li\t%2, 1\n\t"
185         ".set\treorder\n"
186         "2:"
187         : "=m" (rw->lock), "=&r" (tmp), "=&r" (ret)
188         : "m" (rw->lock)
189         : "memory");
190
191         return ret;
192 }
193
194 #endif /* _ASM_SPINLOCK_H */