Goodbye mips64. 31704 lines of code bite the dust.
[linux-2.6/linux-mips.git] / include / asm-s390 / rwsem.h
blobef612dd92ca56e1d76345a152afc87e8e5f1583d
1 #ifndef _S390_RWSEM_H
2 #define _S390_RWSEM_H
4 /*
5 * include/asm-s390/rwsem.h
7 * S390 version
8 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH, IBM Corporation
9 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
11 * Based on asm-alpha/semaphore.h and asm-i386/rwsem.h
16 * The MSW of the count is the negated number of active writers and waiting
17 * lockers, and the LSW is the total number of active locks
19 * The lock count is initialized to 0 (no active and no waiting lockers).
21 * When a writer subtracts WRITE_BIAS, it'll get 0xffff0001 for the case of an
22 * uncontended lock. This can be determined because XADD returns the old value.
23 * Readers increment by 1 and see a positive value when uncontended, negative
24 * if there are writers (and maybe) readers waiting (in which case it goes to
25 * sleep).
27 * The value of WAITING_BIAS supports up to 32766 waiting processes. This can
28 * be extended to 65534 by manually checking the whole MSW rather than relying
29 * on the S flag.
31 * The value of ACTIVE_BIAS supports up to 65535 active processes.
33 * This should be totally fair - if anything is waiting, a process that wants a
34 * lock will go to the back of the queue. When the currently active lock is
35 * released, if there's a writer at the front of the queue, then that and only
36 * that will be woken up; if there's a bunch of consequtive readers at the
37 * front, then they'll all be woken up, but no other readers will be.
40 #ifndef _LINUX_RWSEM_H
41 #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
42 #endif
44 #ifdef __KERNEL__
46 #include <linux/list.h>
47 #include <linux/spinlock.h>
49 struct rwsem_waiter;
51 extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *);
52 extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *);
53 extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
54 extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *);
55 extern struct rw_semaphore *rwsem_downgrade_write(struct rw_semaphore *);
58 * the semaphore definition
60 struct rw_semaphore {
61 signed long count;
62 spinlock_t wait_lock;
63 struct list_head wait_list;
66 #ifndef __s390x__
67 #define RWSEM_UNLOCKED_VALUE 0x00000000
68 #define RWSEM_ACTIVE_BIAS 0x00000001
69 #define RWSEM_ACTIVE_MASK 0x0000ffff
70 #define RWSEM_WAITING_BIAS (-0x00010000)
71 #else /* __s390x__ */
72 #define RWSEM_UNLOCKED_VALUE 0x0000000000000000L
73 #define RWSEM_ACTIVE_BIAS 0x0000000000000001L
74 #define RWSEM_ACTIVE_MASK 0x00000000ffffffffL
75 #define RWSEM_WAITING_BIAS (-0x0000000100000000L)
76 #endif /* __s390x__ */
77 #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
78 #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
81 * initialisation
83 #define __RWSEM_INITIALIZER(name) \
84 { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, LIST_HEAD_INIT((name).wait_list) }
86 #define DECLARE_RWSEM(name) \
87 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
89 static inline void init_rwsem(struct rw_semaphore *sem)
91 sem->count = RWSEM_UNLOCKED_VALUE;
92 spin_lock_init(&sem->wait_lock);
93 INIT_LIST_HEAD(&sem->wait_list);
97 * lock for reading
99 static inline void __down_read(struct rw_semaphore *sem)
101 signed long old, new;
103 __asm__ __volatile__(
104 #ifndef __s390x__
105 " l %0,0(%2)\n"
106 "0: lr %1,%0\n"
107 " ahi %1,%3\n"
108 " cs %0,%1,0(%2)\n"
109 " jl 0b"
110 #else /* __s390x__ */
111 " lg %0,0(%2)\n"
112 "0: lgr %1,%0\n"
113 " aghi %1,%3\n"
114 " csg %0,%1,0(%2)\n"
115 " jl 0b"
116 #endif /* __s390x__ */
117 : "=&d" (old), "=&d" (new)
118 : "a" (&sem->count), "i" (RWSEM_ACTIVE_READ_BIAS)
119 : "cc", "memory" );
120 if (old < 0)
121 rwsem_down_read_failed(sem);
125 * trylock for reading -- returns 1 if successful, 0 if contention
127 static inline int __down_read_trylock(struct rw_semaphore *sem)
129 signed long old, new;
131 __asm__ __volatile__(
132 #ifndef __s390x__
133 " l %0,0(%2)\n"
134 "0: ltr %1,%0\n"
135 " jm 1f\n"
136 " ahi %1,%3\n"
137 " cs %0,%1,0(%2)\n"
138 " jl 0b\n"
139 "1:"
140 #else /* __s390x__ */
141 " lg %0,0(%2)\n"
142 "0: ltgr %1,%0\n"
143 " jm 1f\n"
144 " aghi %1,%3\n"
145 " csg %0,%1,0(%2)\n"
146 " jl 0b\n"
147 "1:"
148 #endif /* __s390x__ */
149 : "=&d" (old), "=&d" (new)
150 : "a" (&sem->count), "i" (RWSEM_ACTIVE_READ_BIAS)
151 : "cc", "memory" );
152 return old >= 0 ? 1 : 0;
156 * lock for writing
158 static inline void __down_write(struct rw_semaphore *sem)
160 signed long old, new, tmp;
162 tmp = RWSEM_ACTIVE_WRITE_BIAS;
163 __asm__ __volatile__(
164 #ifndef __s390x__
165 " l %0,0(%2)\n"
166 "0: lr %1,%0\n"
167 " a %1,%3\n"
168 " cs %0,%1,0(%2)\n"
169 " jl 0b"
170 #else /* __s390x__ */
171 " lg %0,0(%2)\n"
172 "0: lgr %1,%0\n"
173 " ag %1,%3\n"
174 " csg %0,%1,0(%2)\n"
175 " jl 0b"
176 #endif /* __s390x__ */
177 : "=&d" (old), "=&d" (new)
178 : "a" (&sem->count), "m" (tmp)
179 : "cc", "memory" );
180 if (old != 0)
181 rwsem_down_write_failed(sem);
185 * trylock for writing -- returns 1 if successful, 0 if contention
187 static inline int __down_write_trylock(struct rw_semaphore *sem)
189 signed long old;
191 __asm__ __volatile__(
192 #ifndef __s390x__
193 " l %0,0(%1)\n"
194 "0: ltr %0,%0\n"
195 " jnz 1f\n"
196 " cs %0,%2,0(%1)\n"
197 " jl 0b\n"
198 #else /* __s390x__ */
199 " lg %0,0(%1)\n"
200 "0: ltgr %0,%0\n"
201 " jnz 1f\n"
202 " csg %0,%2,0(%1)\n"
203 " jl 0b\n"
204 #endif /* __s390x__ */
205 "1:"
206 : "=&d" (old)
207 : "a" (&sem->count), "d" (RWSEM_ACTIVE_WRITE_BIAS)
208 : "cc", "memory" );
209 return (old == RWSEM_UNLOCKED_VALUE) ? 1 : 0;
213 * unlock after reading
215 static inline void __up_read(struct rw_semaphore *sem)
217 signed long old, new;
219 __asm__ __volatile__(
220 #ifndef __s390x__
221 " l %0,0(%2)\n"
222 "0: lr %1,%0\n"
223 " ahi %1,%3\n"
224 " cs %0,%1,0(%2)\n"
225 " jl 0b"
226 #else /* __s390x__ */
227 " lg %0,0(%2)\n"
228 "0: lgr %1,%0\n"
229 " aghi %1,%3\n"
230 " csg %0,%1,0(%2)\n"
231 " jl 0b"
232 #endif /* __s390x__ */
233 : "=&d" (old), "=&d" (new)
234 : "a" (&sem->count), "i" (-RWSEM_ACTIVE_READ_BIAS)
235 : "cc", "memory" );
236 if (new < 0)
237 if ((new & RWSEM_ACTIVE_MASK) == 0)
238 rwsem_wake(sem);
242 * unlock after writing
244 static inline void __up_write(struct rw_semaphore *sem)
246 signed long old, new, tmp;
248 tmp = -RWSEM_ACTIVE_WRITE_BIAS;
249 __asm__ __volatile__(
250 #ifndef __s390x__
251 " l %0,0(%2)\n"
252 "0: lr %1,%0\n"
253 " a %1,%3\n"
254 " cs %0,%1,0(%2)\n"
255 " jl 0b"
256 #else /* __s390x__ */
257 " lg %0,0(%2)\n"
258 "0: lgr %1,%0\n"
259 " ag %1,%3\n"
260 " csg %0,%1,0(%2)\n"
261 " jl 0b"
262 #endif /* __s390x__ */
263 : "=&d" (old), "=&d" (new)
264 : "a" (&sem->count), "m" (tmp)
265 : "cc", "memory" );
266 if (new < 0)
267 if ((new & RWSEM_ACTIVE_MASK) == 0)
268 rwsem_wake(sem);
272 * downgrade write lock to read lock
274 static inline void __downgrade_write(struct rw_semaphore *sem)
276 signed long old, new, tmp;
278 tmp = -RWSEM_WAITING_BIAS;
279 __asm__ __volatile__(
280 #ifndef __s390x__
281 " l %0,0(%2)\n"
282 "0: lr %1,%0\n"
283 " a %1,%3\n"
284 " cs %0,%1,0(%2)\n"
285 " jl 0b"
286 #else /* __s390x__ */
287 " lg %0,0(%2)\n"
288 "0: lgr %1,%0\n"
289 " ag %1,%3\n"
290 " csg %0,%1,0(%2)\n"
291 " jl 0b"
292 #endif /* __s390x__ */
293 : "=&d" (old), "=&d" (new)
294 : "a" (&sem->count), "m" (tmp)
295 : "cc", "memory" );
296 if (new > 1)
297 rwsem_downgrade_wake(sem);
301 * implement atomic add functionality
303 static inline void rwsem_atomic_add(long delta, struct rw_semaphore *sem)
305 signed long old, new;
307 __asm__ __volatile__(
308 #ifndef __s390x__
309 " l %0,0(%2)\n"
310 "0: lr %1,%0\n"
311 " ar %1,%3\n"
312 " cs %0,%1,0(%2)\n"
313 " jl 0b"
314 #else /* __s390x__ */
315 " lg %0,0(%2)\n"
316 "0: lgr %1,%0\n"
317 " agr %1,%3\n"
318 " csg %0,%1,0(%2)\n"
319 " jl 0b"
320 #endif /* __s390x__ */
321 : "=&d" (old), "=&d" (new)
322 : "a" (&sem->count), "d" (delta)
323 : "cc", "memory" );
327 * implement exchange and add functionality
329 static inline long rwsem_atomic_update(long delta, struct rw_semaphore *sem)
331 signed long old, new;
333 __asm__ __volatile__(
334 #ifndef __s390x__
335 " l %0,0(%2)\n"
336 "0: lr %1,%0\n"
337 " ar %1,%3\n"
338 " cs %0,%1,0(%2)\n"
339 " jl 0b"
340 #else /* __s390x__ */
341 " lg %0,0(%2)\n"
342 "0: lgr %1,%0\n"
343 " agr %1,%3\n"
344 " csg %0,%1,0(%2)\n"
345 " jl 0b"
346 #endif /* __s390x__ */
347 : "=&d" (old), "=&d" (new)
348 : "a" (&sem->count), "d" (delta)
349 : "cc", "memory" );
350 return new;
353 #endif /* __KERNEL__ */
354 #endif /* _S390_RWSEM_H */