blob: d0eb4653cebdb0d7bf0eab014cfb142904ad89b7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _S390_RWSEM_H
2#define _S390_RWSEM_H
3
4/*
5 * include/asm-s390/rwsem.h
6 *
7 * S390 version
8 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH, IBM Corporation
9 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
10 *
11 * Based on asm-alpha/semaphore.h and asm-i386/rwsem.h
12 */
13
14/*
15 *
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
18 *
19 * The lock count is initialized to 0 (no active and no waiting lockers).
20 *
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).
26 *
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.
30 *
31 * The value of ACTIVE_BIAS supports up to 65535 active processes.
32 *
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.
38 */
39
40#ifndef _LINUX_RWSEM_H
41#error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
42#endif
43
44#ifdef __KERNEL__
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#ifndef __s390x__
47#define RWSEM_UNLOCKED_VALUE 0x00000000
48#define RWSEM_ACTIVE_BIAS 0x00000001
49#define RWSEM_ACTIVE_MASK 0x0000ffff
50#define RWSEM_WAITING_BIAS (-0x00010000)
51#else /* __s390x__ */
52#define RWSEM_UNLOCKED_VALUE 0x0000000000000000L
53#define RWSEM_ACTIVE_BIAS 0x0000000000000001L
54#define RWSEM_ACTIVE_MASK 0x00000000ffffffffL
55#define RWSEM_WAITING_BIAS (-0x0000000100000000L)
56#endif /* __s390x__ */
57#define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
58#define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
59
60/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * lock for reading
62 */
63static inline void __down_read(struct rw_semaphore *sem)
64{
65 signed long old, new;
66
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020067 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#ifndef __s390x__
Martin Schwidefsky987bcda2010-02-26 22:37:31 +010069 " l %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020070 "0: lr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +010071 " ahi %1,%4\n"
72 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020073 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#else /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +010075 " lg %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020076 "0: lgr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +010077 " aghi %1,%4\n"
78 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020079 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#endif /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +010081 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
82 : "Q" (sem->count), "i" (RWSEM_ACTIVE_READ_BIAS)
83 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (old < 0)
85 rwsem_down_read_failed(sem);
86}
87
88/*
89 * trylock for reading -- returns 1 if successful, 0 if contention
90 */
91static inline int __down_read_trylock(struct rw_semaphore *sem)
92{
93 signed long old, new;
94
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020095 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#ifndef __s390x__
Martin Schwidefsky987bcda2010-02-26 22:37:31 +010097 " l %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020098 "0: ltr %1,%0\n"
99 " jm 1f\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100100 " ahi %1,%4\n"
101 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200102 " jl 0b\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 "1:"
104#else /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100105 " lg %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200106 "0: ltgr %1,%0\n"
107 " jm 1f\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100108 " aghi %1,%4\n"
109 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200110 " jl 0b\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 "1:"
112#endif /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100113 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
114 : "Q" (sem->count), "i" (RWSEM_ACTIVE_READ_BIAS)
115 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return old >= 0 ? 1 : 0;
117}
118
119/*
120 * lock for writing
121 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700122static inline void __down_write_nested(struct rw_semaphore *sem, int subclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 signed long old, new, tmp;
125
126 tmp = RWSEM_ACTIVE_WRITE_BIAS;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200127 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#ifndef __s390x__
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100129 " l %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200130 "0: lr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100131 " a %1,%4\n"
132 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200133 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#else /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100135 " lg %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200136 "0: lgr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100137 " ag %1,%4\n"
138 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200139 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#endif /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100141 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
142 : "Q" (sem->count), "m" (tmp)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200143 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (old != 0)
145 rwsem_down_write_failed(sem);
146}
147
Ingo Molnar4ea21762006-07-03 00:24:53 -0700148static inline void __down_write(struct rw_semaphore *sem)
149{
150 __down_write_nested(sem, 0);
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*
154 * trylock for writing -- returns 1 if successful, 0 if contention
155 */
156static inline int __down_write_trylock(struct rw_semaphore *sem)
157{
158 signed long old;
159
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200160 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#ifndef __s390x__
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100162 " l %0,%1\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200163 "0: ltr %0,%0\n"
164 " jnz 1f\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100165 " cs %0,%3,%1\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200166 " jl 0b\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#else /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100168 " lg %0,%1\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200169 "0: ltgr %0,%0\n"
170 " jnz 1f\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100171 " csg %0,%3,%1\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200172 " jl 0b\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#endif /* __s390x__ */
174 "1:"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100175 : "=&d" (old), "=Q" (sem->count)
176 : "Q" (sem->count), "d" (RWSEM_ACTIVE_WRITE_BIAS)
177 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return (old == RWSEM_UNLOCKED_VALUE) ? 1 : 0;
179}
180
181/*
182 * unlock after reading
183 */
184static inline void __up_read(struct rw_semaphore *sem)
185{
186 signed long old, new;
187
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200188 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#ifndef __s390x__
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100190 " l %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200191 "0: lr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100192 " ahi %1,%4\n"
193 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200194 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195#else /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100196 " lg %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200197 "0: lgr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100198 " aghi %1,%4\n"
199 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200200 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201#endif /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100202 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
203 : "Q" (sem->count), "i" (-RWSEM_ACTIVE_READ_BIAS)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200204 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (new < 0)
206 if ((new & RWSEM_ACTIVE_MASK) == 0)
207 rwsem_wake(sem);
208}
209
210/*
211 * unlock after writing
212 */
213static inline void __up_write(struct rw_semaphore *sem)
214{
215 signed long old, new, tmp;
216
217 tmp = -RWSEM_ACTIVE_WRITE_BIAS;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200218 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219#ifndef __s390x__
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100220 " l %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200221 "0: lr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100222 " a %1,%4\n"
223 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200224 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225#else /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100226 " lg %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200227 "0: lgr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100228 " ag %1,%4\n"
229 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200230 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231#endif /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100232 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
233 : "Q" (sem->count), "m" (tmp)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200234 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (new < 0)
236 if ((new & RWSEM_ACTIVE_MASK) == 0)
237 rwsem_wake(sem);
238}
239
240/*
241 * downgrade write lock to read lock
242 */
243static inline void __downgrade_write(struct rw_semaphore *sem)
244{
245 signed long old, new, tmp;
246
247 tmp = -RWSEM_WAITING_BIAS;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200248 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249#ifndef __s390x__
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100250 " l %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200251 "0: lr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100252 " a %1,%4\n"
253 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200254 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255#else /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100256 " lg %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200257 "0: lgr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100258 " ag %1,%4\n"
259 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200260 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261#endif /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100262 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
263 : "Q" (sem->count), "m" (tmp)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200264 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (new > 1)
266 rwsem_downgrade_wake(sem);
267}
268
269/*
270 * implement atomic add functionality
271 */
272static inline void rwsem_atomic_add(long delta, struct rw_semaphore *sem)
273{
274 signed long old, new;
275
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200276 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277#ifndef __s390x__
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100278 " l %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200279 "0: lr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100280 " ar %1,%4\n"
281 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200282 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283#else /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100284 " lg %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200285 "0: lgr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100286 " agr %1,%4\n"
287 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200288 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289#endif /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100290 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
291 : "Q" (sem->count), "d" (delta)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200292 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
295/*
296 * implement exchange and add functionality
297 */
298static inline long rwsem_atomic_update(long delta, struct rw_semaphore *sem)
299{
300 signed long old, new;
301
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200302 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303#ifndef __s390x__
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100304 " l %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200305 "0: lr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100306 " ar %1,%4\n"
307 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200308 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309#else /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100310 " lg %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200311 "0: lgr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100312 " agr %1,%4\n"
313 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200314 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315#endif /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100316 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
317 : "Q" (sem->count), "d" (delta)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200318 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return new;
320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322#endif /* __KERNEL__ */
323#endif /* _S390_RWSEM_H */