blob: 1ceee10264c3832bce52f2cae57070d549afc35c [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
Heiko Carstensf4815ac2012-05-23 16:24:51 +020044#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define RWSEM_UNLOCKED_VALUE 0x00000000
46#define RWSEM_ACTIVE_BIAS 0x00000001
47#define RWSEM_ACTIVE_MASK 0x0000ffff
48#define RWSEM_WAITING_BIAS (-0x00010000)
Heiko Carstensf4815ac2012-05-23 16:24:51 +020049#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define RWSEM_UNLOCKED_VALUE 0x0000000000000000L
51#define RWSEM_ACTIVE_BIAS 0x0000000000000001L
52#define RWSEM_ACTIVE_MASK 0x00000000ffffffffL
53#define RWSEM_WAITING_BIAS (-0x0000000100000000L)
Heiko Carstensf4815ac2012-05-23 16:24:51 +020054#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
56#define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
57
58/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * lock for reading
60 */
61static inline void __down_read(struct rw_semaphore *sem)
62{
63 signed long old, new;
64
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020065 asm volatile(
Heiko Carstensf4815ac2012-05-23 16:24:51 +020066#ifndef CONFIG_64BIT
Martin Schwidefsky987bcda2010-02-26 22:37:31 +010067 " l %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020068 "0: lr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +010069 " ahi %1,%4\n"
70 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020071 " jl 0b"
Heiko Carstensf4815ac2012-05-23 16:24:51 +020072#else /* CONFIG_64BIT */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +010073 " lg %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020074 "0: lgr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +010075 " aghi %1,%4\n"
76 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020077 " jl 0b"
Heiko Carstensf4815ac2012-05-23 16:24:51 +020078#endif /* CONFIG_64BIT */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +010079 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
80 : "Q" (sem->count), "i" (RWSEM_ACTIVE_READ_BIAS)
81 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (old < 0)
83 rwsem_down_read_failed(sem);
84}
85
86/*
87 * trylock for reading -- returns 1 if successful, 0 if contention
88 */
89static inline int __down_read_trylock(struct rw_semaphore *sem)
90{
91 signed long old, new;
92
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020093 asm volatile(
Heiko Carstensf4815ac2012-05-23 16:24:51 +020094#ifndef CONFIG_64BIT
Martin Schwidefsky987bcda2010-02-26 22:37:31 +010095 " l %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020096 "0: ltr %1,%0\n"
97 " jm 1f\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +010098 " ahi %1,%4\n"
99 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200100 " jl 0b\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 "1:"
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200102#else /* CONFIG_64BIT */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100103 " lg %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200104 "0: ltgr %1,%0\n"
105 " jm 1f\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100106 " aghi %1,%4\n"
107 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200108 " jl 0b\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 "1:"
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200110#endif /* CONFIG_64BIT */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100111 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
112 : "Q" (sem->count), "i" (RWSEM_ACTIVE_READ_BIAS)
113 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return old >= 0 ? 1 : 0;
115}
116
117/*
118 * lock for writing
119 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700120static inline void __down_write_nested(struct rw_semaphore *sem, int subclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 signed long old, new, tmp;
123
124 tmp = RWSEM_ACTIVE_WRITE_BIAS;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200125 asm volatile(
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200126#ifndef CONFIG_64BIT
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100127 " l %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200128 "0: lr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100129 " a %1,%4\n"
130 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200131 " jl 0b"
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200132#else /* CONFIG_64BIT */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100133 " lg %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200134 "0: lgr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100135 " ag %1,%4\n"
136 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200137 " jl 0b"
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200138#endif /* CONFIG_64BIT */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100139 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
140 : "Q" (sem->count), "m" (tmp)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200141 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (old != 0)
143 rwsem_down_write_failed(sem);
144}
145
Ingo Molnar4ea21762006-07-03 00:24:53 -0700146static inline void __down_write(struct rw_semaphore *sem)
147{
148 __down_write_nested(sem, 0);
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
152 * trylock for writing -- returns 1 if successful, 0 if contention
153 */
154static inline int __down_write_trylock(struct rw_semaphore *sem)
155{
156 signed long old;
157
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200158 asm volatile(
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200159#ifndef CONFIG_64BIT
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100160 " l %0,%1\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200161 "0: ltr %0,%0\n"
162 " jnz 1f\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100163 " cs %0,%3,%1\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200164 " jl 0b\n"
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200165#else /* CONFIG_64BIT */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100166 " lg %0,%1\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200167 "0: ltgr %0,%0\n"
168 " jnz 1f\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100169 " csg %0,%3,%1\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200170 " jl 0b\n"
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200171#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 "1:"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100173 : "=&d" (old), "=Q" (sem->count)
174 : "Q" (sem->count), "d" (RWSEM_ACTIVE_WRITE_BIAS)
175 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return (old == RWSEM_UNLOCKED_VALUE) ? 1 : 0;
177}
178
179/*
180 * unlock after reading
181 */
182static inline void __up_read(struct rw_semaphore *sem)
183{
184 signed long old, new;
185
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200186 asm volatile(
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200187#ifndef CONFIG_64BIT
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100188 " l %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200189 "0: lr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100190 " ahi %1,%4\n"
191 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200192 " jl 0b"
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200193#else /* CONFIG_64BIT */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100194 " lg %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200195 "0: lgr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100196 " aghi %1,%4\n"
197 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200198 " jl 0b"
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200199#endif /* CONFIG_64BIT */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100200 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
201 : "Q" (sem->count), "i" (-RWSEM_ACTIVE_READ_BIAS)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200202 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (new < 0)
204 if ((new & RWSEM_ACTIVE_MASK) == 0)
205 rwsem_wake(sem);
206}
207
208/*
209 * unlock after writing
210 */
211static inline void __up_write(struct rw_semaphore *sem)
212{
213 signed long old, new, tmp;
214
215 tmp = -RWSEM_ACTIVE_WRITE_BIAS;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200216 asm volatile(
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200217#ifndef CONFIG_64BIT
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100218 " l %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200219 "0: lr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100220 " a %1,%4\n"
221 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200222 " jl 0b"
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200223#else /* CONFIG_64BIT */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100224 " lg %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200225 "0: lgr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100226 " ag %1,%4\n"
227 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200228 " jl 0b"
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200229#endif /* CONFIG_64BIT */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100230 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
231 : "Q" (sem->count), "m" (tmp)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200232 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (new < 0)
234 if ((new & RWSEM_ACTIVE_MASK) == 0)
235 rwsem_wake(sem);
236}
237
238/*
239 * downgrade write lock to read lock
240 */
241static inline void __downgrade_write(struct rw_semaphore *sem)
242{
243 signed long old, new, tmp;
244
245 tmp = -RWSEM_WAITING_BIAS;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200246 asm volatile(
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200247#ifndef CONFIG_64BIT
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100248 " l %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200249 "0: lr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100250 " a %1,%4\n"
251 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200252 " jl 0b"
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200253#else /* CONFIG_64BIT */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100254 " lg %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200255 "0: lgr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100256 " ag %1,%4\n"
257 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200258 " jl 0b"
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200259#endif /* CONFIG_64BIT */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100260 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
261 : "Q" (sem->count), "m" (tmp)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200262 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (new > 1)
264 rwsem_downgrade_wake(sem);
265}
266
267/*
268 * implement atomic add functionality
269 */
270static inline void rwsem_atomic_add(long delta, struct rw_semaphore *sem)
271{
272 signed long old, new;
273
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200274 asm volatile(
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200275#ifndef CONFIG_64BIT
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100276 " l %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200277 "0: lr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100278 " ar %1,%4\n"
279 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200280 " jl 0b"
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200281#else /* CONFIG_64BIT */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100282 " lg %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200283 "0: lgr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100284 " agr %1,%4\n"
285 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200286 " jl 0b"
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200287#endif /* CONFIG_64BIT */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100288 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
289 : "Q" (sem->count), "d" (delta)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200290 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293/*
294 * implement exchange and add functionality
295 */
296static inline long rwsem_atomic_update(long delta, struct rw_semaphore *sem)
297{
298 signed long old, new;
299
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200300 asm volatile(
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200301#ifndef CONFIG_64BIT
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100302 " l %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200303 "0: lr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100304 " ar %1,%4\n"
305 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200306 " jl 0b"
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200307#else /* CONFIG_64BIT */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100308 " lg %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200309 "0: lgr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100310 " agr %1,%4\n"
311 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200312 " jl 0b"
Heiko Carstensf4815ac2012-05-23 16:24:51 +0200313#endif /* CONFIG_64BIT */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100314 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
315 : "Q" (sem->count), "d" (delta)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200316 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return new;
318}
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320#endif /* _S390_RWSEM_H */