blob: 423fdda2322dd6ee3f51add1cea21ce96f0b63f0 [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
46#include <linux/list.h>
47#include <linux/spinlock.h>
48
49struct rwsem_waiter;
50
51extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *);
52extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *);
53extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
54extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *);
55extern struct rw_semaphore *rwsem_downgrade_write(struct rw_semaphore *);
56
57/*
58 * the semaphore definition
59 */
60struct rw_semaphore {
61 signed long count;
62 spinlock_t wait_lock;
63 struct list_head wait_list;
Ingo Molnar4ea21762006-07-03 00:24:53 -070064#ifdef CONFIG_DEBUG_LOCK_ALLOC
65 struct lockdep_map dep_map;
66#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
69#ifndef __s390x__
70#define RWSEM_UNLOCKED_VALUE 0x00000000
71#define RWSEM_ACTIVE_BIAS 0x00000001
72#define RWSEM_ACTIVE_MASK 0x0000ffff
73#define RWSEM_WAITING_BIAS (-0x00010000)
74#else /* __s390x__ */
75#define RWSEM_UNLOCKED_VALUE 0x0000000000000000L
76#define RWSEM_ACTIVE_BIAS 0x0000000000000001L
77#define RWSEM_ACTIVE_MASK 0x00000000ffffffffL
78#define RWSEM_WAITING_BIAS (-0x0000000100000000L)
79#endif /* __s390x__ */
80#define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
81#define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
82
83/*
84 * initialisation
85 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070086
87#ifdef CONFIG_DEBUG_LOCK_ALLOC
88# define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
89#else
90# define __RWSEM_DEP_MAP_INIT(lockname)
91#endif
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#define __RWSEM_INITIALIZER(name) \
Heiko Carstens519580f2008-01-26 14:11:04 +010094 { RWSEM_UNLOCKED_VALUE, __SPIN_LOCK_UNLOCKED((name).wait.lock), \
95 LIST_HEAD_INIT((name).wait_list) __RWSEM_DEP_MAP_INIT(name) }
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97#define DECLARE_RWSEM(name) \
98 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
99
100static inline void init_rwsem(struct rw_semaphore *sem)
101{
102 sem->count = RWSEM_UNLOCKED_VALUE;
103 spin_lock_init(&sem->wait_lock);
104 INIT_LIST_HEAD(&sem->wait_list);
105}
106
Ingo Molnar4ea21762006-07-03 00:24:53 -0700107extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
108 struct lock_class_key *key);
109
110#define init_rwsem(sem) \
111do { \
112 static struct lock_class_key __key; \
113 \
114 __init_rwsem((sem), #sem, &__key); \
115} while (0)
116
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/*
119 * lock for reading
120 */
121static inline void __down_read(struct rw_semaphore *sem)
122{
123 signed long old, new;
124
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200125 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#ifndef __s390x__
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 " ahi %1,%4\n"
130 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200131 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#else /* __s390x__ */
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 " aghi %1,%4\n"
136 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200137 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#endif /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100139 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
140 : "Q" (sem->count), "i" (RWSEM_ACTIVE_READ_BIAS)
141 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (old < 0)
143 rwsem_down_read_failed(sem);
144}
145
146/*
147 * trylock for reading -- returns 1 if successful, 0 if contention
148 */
149static inline int __down_read_trylock(struct rw_semaphore *sem)
150{
151 signed long old, new;
152
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200153 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#ifndef __s390x__
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100155 " l %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200156 "0: ltr %1,%0\n"
157 " jm 1f\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100158 " ahi %1,%4\n"
159 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200160 " jl 0b\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 "1:"
162#else /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100163 " lg %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200164 "0: ltgr %1,%0\n"
165 " jm 1f\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100166 " aghi %1,%4\n"
167 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200168 " jl 0b\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 "1:"
170#endif /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100171 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
172 : "Q" (sem->count), "i" (RWSEM_ACTIVE_READ_BIAS)
173 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return old >= 0 ? 1 : 0;
175}
176
177/*
178 * lock for writing
179 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700180static inline void __down_write_nested(struct rw_semaphore *sem, int subclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 signed long old, new, tmp;
183
184 tmp = RWSEM_ACTIVE_WRITE_BIAS;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200185 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#ifndef __s390x__
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100187 " l %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200188 "0: lr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100189 " a %1,%4\n"
190 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200191 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192#else /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100193 " lg %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200194 "0: lgr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100195 " ag %1,%4\n"
196 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200197 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198#endif /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100199 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
200 : "Q" (sem->count), "m" (tmp)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200201 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (old != 0)
203 rwsem_down_write_failed(sem);
204}
205
Ingo Molnar4ea21762006-07-03 00:24:53 -0700206static inline void __down_write(struct rw_semaphore *sem)
207{
208 __down_write_nested(sem, 0);
209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*
212 * trylock for writing -- returns 1 if successful, 0 if contention
213 */
214static inline int __down_write_trylock(struct rw_semaphore *sem)
215{
216 signed long old;
217
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,%1\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200221 "0: ltr %0,%0\n"
222 " jnz 1f\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100223 " cs %0,%3,%1\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200224 " jl 0b\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225#else /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100226 " lg %0,%1\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200227 "0: ltgr %0,%0\n"
228 " jnz 1f\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100229 " csg %0,%3,%1\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200230 " jl 0b\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231#endif /* __s390x__ */
232 "1:"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100233 : "=&d" (old), "=Q" (sem->count)
234 : "Q" (sem->count), "d" (RWSEM_ACTIVE_WRITE_BIAS)
235 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return (old == RWSEM_UNLOCKED_VALUE) ? 1 : 0;
237}
238
239/*
240 * unlock after reading
241 */
242static inline void __up_read(struct rw_semaphore *sem)
243{
244 signed long old, new;
245
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200246 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247#ifndef __s390x__
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 " ahi %1,%4\n"
251 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200252 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253#else /* __s390x__ */
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 " aghi %1,%4\n"
257 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200258 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259#endif /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100260 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
261 : "Q" (sem->count), "i" (-RWSEM_ACTIVE_READ_BIAS)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200262 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (new < 0)
264 if ((new & RWSEM_ACTIVE_MASK) == 0)
265 rwsem_wake(sem);
266}
267
268/*
269 * unlock after writing
270 */
271static inline void __up_write(struct rw_semaphore *sem)
272{
273 signed long old, new, tmp;
274
275 tmp = -RWSEM_ACTIVE_WRITE_BIAS;
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 " a %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 " ag %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), "m" (tmp)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200292 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (new < 0)
294 if ((new & RWSEM_ACTIVE_MASK) == 0)
295 rwsem_wake(sem);
296}
297
298/*
299 * downgrade write lock to read lock
300 */
301static inline void __downgrade_write(struct rw_semaphore *sem)
302{
303 signed long old, new, tmp;
304
305 tmp = -RWSEM_WAITING_BIAS;
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200306 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307#ifndef __s390x__
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100308 " l %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200309 "0: lr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100310 " a %1,%4\n"
311 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200312 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313#else /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100314 " lg %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200315 "0: lgr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100316 " ag %1,%4\n"
317 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200318 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319#endif /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100320 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
321 : "Q" (sem->count), "m" (tmp)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200322 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (new > 1)
324 rwsem_downgrade_wake(sem);
325}
326
327/*
328 * implement atomic add functionality
329 */
330static inline void rwsem_atomic_add(long delta, struct rw_semaphore *sem)
331{
332 signed long old, new;
333
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200334 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335#ifndef __s390x__
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100336 " l %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200337 "0: lr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100338 " ar %1,%4\n"
339 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200340 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341#else /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100342 " lg %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200343 "0: lgr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100344 " agr %1,%4\n"
345 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200346 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347#endif /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100348 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
349 : "Q" (sem->count), "d" (delta)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200350 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353/*
354 * implement exchange and add functionality
355 */
356static inline long rwsem_atomic_update(long delta, struct rw_semaphore *sem)
357{
358 signed long old, new;
359
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200360 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361#ifndef __s390x__
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100362 " l %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200363 "0: lr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100364 " ar %1,%4\n"
365 " cs %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200366 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367#else /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100368 " lg %0,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200369 "0: lgr %1,%0\n"
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100370 " agr %1,%4\n"
371 " csg %0,%1,%2\n"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200372 " jl 0b"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373#endif /* __s390x__ */
Martin Schwidefsky987bcda2010-02-26 22:37:31 +0100374 : "=&d" (old), "=&d" (new), "=Q" (sem->count)
375 : "Q" (sem->count), "d" (delta)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200376 : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return new;
378}
379
Rik Van Rieleb92f4e2005-10-29 18:15:44 -0700380static inline int rwsem_is_locked(struct rw_semaphore *sem)
381{
382 return (sem->count != 0);
383}
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385#endif /* __KERNEL__ */
386#endif /* _S390_RWSEM_H */