rwsem.h 7.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#ifndef _S390_RWSEM_H
#define _S390_RWSEM_H

/*
 *  include/asm-s390/rwsem.h
 *
 *  S390 version
 *    Copyright (C) 2002 IBM Deutschland Entwicklung GmbH, IBM Corporation
 *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
 *
 *  Based on asm-alpha/semaphore.h and asm-i386/rwsem.h
 */

/*
 *
 * The MSW of the count is the negated number of active writers and waiting
 * lockers, and the LSW is the total number of active locks
 *
 * The lock count is initialized to 0 (no active and no waiting lockers).
 *
 * When a writer subtracts WRITE_BIAS, it'll get 0xffff0001 for the case of an
 * uncontended lock. This can be determined because XADD returns the old value.
 * Readers increment by 1 and see a positive value when uncontended, negative
 * if there are writers (and maybe) readers waiting (in which case it goes to
 * sleep).
 *
 * The value of WAITING_BIAS supports up to 32766 waiting processes. This can
 * be extended to 65534 by manually checking the whole MSW rather than relying
 * on the S flag.
 *
 * The value of ACTIVE_BIAS supports up to 65535 active processes.
 *
 * This should be totally fair - if anything is waiting, a process that wants a
 * lock will go to the back of the queue. When the currently active lock is
 * released, if there's a writer at the front of the queue, then that and only
 * that will be woken up; if there's a bunch of consequtive readers at the
 * front, then they'll all be woken up, but no other readers will be.
 */

#ifndef _LINUX_RWSEM_H
#error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
#endif

#ifdef __KERNEL__

extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *);
extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *);
extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *);
extern struct rw_semaphore *rwsem_downgrade_write(struct rw_semaphore *);

#ifndef __s390x__
#define RWSEM_UNLOCKED_VALUE	0x00000000
#define RWSEM_ACTIVE_BIAS	0x00000001
#define RWSEM_ACTIVE_MASK	0x0000ffff
#define RWSEM_WAITING_BIAS	(-0x00010000)
#else /* __s390x__ */
#define RWSEM_UNLOCKED_VALUE	0x0000000000000000L
#define RWSEM_ACTIVE_BIAS	0x0000000000000001L
#define RWSEM_ACTIVE_MASK	0x00000000ffffffffL
#define RWSEM_WAITING_BIAS	(-0x0000000100000000L)
#endif /* __s390x__ */
#define RWSEM_ACTIVE_READ_BIAS	RWSEM_ACTIVE_BIAS
#define RWSEM_ACTIVE_WRITE_BIAS	(RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)

/*
 * lock for reading
 */
static inline void __down_read(struct rw_semaphore *sem)
{
	signed long old, new;

73
	asm volatile(
L
Linus Torvalds 已提交
74
#ifndef __s390x__
75
		"	l	%0,%2\n"
76
		"0:	lr	%1,%0\n"
77 78
		"	ahi	%1,%4\n"
		"	cs	%0,%1,%2\n"
79
		"	jl	0b"
L
Linus Torvalds 已提交
80
#else /* __s390x__ */
81
		"	lg	%0,%2\n"
82
		"0:	lgr	%1,%0\n"
83 84
		"	aghi	%1,%4\n"
		"	csg	%0,%1,%2\n"
85
		"	jl	0b"
L
Linus Torvalds 已提交
86
#endif /* __s390x__ */
87 88 89
		: "=&d" (old), "=&d" (new), "=Q" (sem->count)
		: "Q" (sem->count), "i" (RWSEM_ACTIVE_READ_BIAS)
		: "cc", "memory");
L
Linus Torvalds 已提交
90 91 92 93 94 95 96 97 98 99 100
	if (old < 0)
		rwsem_down_read_failed(sem);
}

/*
 * trylock for reading -- returns 1 if successful, 0 if contention
 */
static inline int __down_read_trylock(struct rw_semaphore *sem)
{
	signed long old, new;

101
	asm volatile(
L
Linus Torvalds 已提交
102
#ifndef __s390x__
103
		"	l	%0,%2\n"
104 105
		"0:	ltr	%1,%0\n"
		"	jm	1f\n"
106 107
		"	ahi	%1,%4\n"
		"	cs	%0,%1,%2\n"
108
		"	jl	0b\n"
L
Linus Torvalds 已提交
109 110
		"1:"
#else /* __s390x__ */
111
		"	lg	%0,%2\n"
112 113
		"0:	ltgr	%1,%0\n"
		"	jm	1f\n"
114 115
		"	aghi	%1,%4\n"
		"	csg	%0,%1,%2\n"
116
		"	jl	0b\n"
L
Linus Torvalds 已提交
117 118
		"1:"
#endif /* __s390x__ */
119 120 121
		: "=&d" (old), "=&d" (new), "=Q" (sem->count)
		: "Q" (sem->count), "i" (RWSEM_ACTIVE_READ_BIAS)
		: "cc", "memory");
L
Linus Torvalds 已提交
122 123 124 125 126 127
	return old >= 0 ? 1 : 0;
}

/*
 * lock for writing
 */
128
static inline void __down_write_nested(struct rw_semaphore *sem, int subclass)
L
Linus Torvalds 已提交
129 130 131 132
{
	signed long old, new, tmp;

	tmp = RWSEM_ACTIVE_WRITE_BIAS;
133
	asm volatile(
L
Linus Torvalds 已提交
134
#ifndef __s390x__
135
		"	l	%0,%2\n"
136
		"0:	lr	%1,%0\n"
137 138
		"	a	%1,%4\n"
		"	cs	%0,%1,%2\n"
139
		"	jl	0b"
L
Linus Torvalds 已提交
140
#else /* __s390x__ */
141
		"	lg	%0,%2\n"
142
		"0:	lgr	%1,%0\n"
143 144
		"	ag	%1,%4\n"
		"	csg	%0,%1,%2\n"
145
		"	jl	0b"
L
Linus Torvalds 已提交
146
#endif /* __s390x__ */
147 148
		: "=&d" (old), "=&d" (new), "=Q" (sem->count)
		: "Q" (sem->count), "m" (tmp)
149
		: "cc", "memory");
L
Linus Torvalds 已提交
150 151 152 153
	if (old != 0)
		rwsem_down_write_failed(sem);
}

154 155 156 157 158
static inline void __down_write(struct rw_semaphore *sem)
{
	__down_write_nested(sem, 0);
}

L
Linus Torvalds 已提交
159 160 161 162 163 164 165
/*
 * trylock for writing -- returns 1 if successful, 0 if contention
 */
static inline int __down_write_trylock(struct rw_semaphore *sem)
{
	signed long old;

166
	asm volatile(
L
Linus Torvalds 已提交
167
#ifndef __s390x__
168
		"	l	%0,%1\n"
169 170
		"0:	ltr	%0,%0\n"
		"	jnz	1f\n"
171
		"	cs	%0,%3,%1\n"
172
		"	jl	0b\n"
L
Linus Torvalds 已提交
173
#else /* __s390x__ */
174
		"	lg	%0,%1\n"
175 176
		"0:	ltgr	%0,%0\n"
		"	jnz	1f\n"
177
		"	csg	%0,%3,%1\n"
178
		"	jl	0b\n"
L
Linus Torvalds 已提交
179 180
#endif /* __s390x__ */
		"1:"
181 182 183
		: "=&d" (old), "=Q" (sem->count)
		: "Q" (sem->count), "d" (RWSEM_ACTIVE_WRITE_BIAS)
		: "cc", "memory");
L
Linus Torvalds 已提交
184 185 186 187 188 189 190 191 192 193
	return (old == RWSEM_UNLOCKED_VALUE) ? 1 : 0;
}

/*
 * unlock after reading
 */
static inline void __up_read(struct rw_semaphore *sem)
{
	signed long old, new;

194
	asm volatile(
L
Linus Torvalds 已提交
195
#ifndef __s390x__
196
		"	l	%0,%2\n"
197
		"0:	lr	%1,%0\n"
198 199
		"	ahi	%1,%4\n"
		"	cs	%0,%1,%2\n"
200
		"	jl	0b"
L
Linus Torvalds 已提交
201
#else /* __s390x__ */
202
		"	lg	%0,%2\n"
203
		"0:	lgr	%1,%0\n"
204 205
		"	aghi	%1,%4\n"
		"	csg	%0,%1,%2\n"
206
		"	jl	0b"
L
Linus Torvalds 已提交
207
#endif /* __s390x__ */
208 209
		: "=&d" (old), "=&d" (new), "=Q" (sem->count)
		: "Q" (sem->count), "i" (-RWSEM_ACTIVE_READ_BIAS)
210
		: "cc", "memory");
L
Linus Torvalds 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223
	if (new < 0)
		if ((new & RWSEM_ACTIVE_MASK) == 0)
			rwsem_wake(sem);
}

/*
 * unlock after writing
 */
static inline void __up_write(struct rw_semaphore *sem)
{
	signed long old, new, tmp;

	tmp = -RWSEM_ACTIVE_WRITE_BIAS;
224
	asm volatile(
L
Linus Torvalds 已提交
225
#ifndef __s390x__
226
		"	l	%0,%2\n"
227
		"0:	lr	%1,%0\n"
228 229
		"	a	%1,%4\n"
		"	cs	%0,%1,%2\n"
230
		"	jl	0b"
L
Linus Torvalds 已提交
231
#else /* __s390x__ */
232
		"	lg	%0,%2\n"
233
		"0:	lgr	%1,%0\n"
234 235
		"	ag	%1,%4\n"
		"	csg	%0,%1,%2\n"
236
		"	jl	0b"
L
Linus Torvalds 已提交
237
#endif /* __s390x__ */
238 239
		: "=&d" (old), "=&d" (new), "=Q" (sem->count)
		: "Q" (sem->count), "m" (tmp)
240
		: "cc", "memory");
L
Linus Torvalds 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253
	if (new < 0)
		if ((new & RWSEM_ACTIVE_MASK) == 0)
			rwsem_wake(sem);
}

/*
 * downgrade write lock to read lock
 */
static inline void __downgrade_write(struct rw_semaphore *sem)
{
	signed long old, new, tmp;

	tmp = -RWSEM_WAITING_BIAS;
254
	asm volatile(
L
Linus Torvalds 已提交
255
#ifndef __s390x__
256
		"	l	%0,%2\n"
257
		"0:	lr	%1,%0\n"
258 259
		"	a	%1,%4\n"
		"	cs	%0,%1,%2\n"
260
		"	jl	0b"
L
Linus Torvalds 已提交
261
#else /* __s390x__ */
262
		"	lg	%0,%2\n"
263
		"0:	lgr	%1,%0\n"
264 265
		"	ag	%1,%4\n"
		"	csg	%0,%1,%2\n"
266
		"	jl	0b"
L
Linus Torvalds 已提交
267
#endif /* __s390x__ */
268 269
		: "=&d" (old), "=&d" (new), "=Q" (sem->count)
		: "Q" (sem->count), "m" (tmp)
270
		: "cc", "memory");
L
Linus Torvalds 已提交
271 272 273 274 275 276 277 278 279 280 281
	if (new > 1)
		rwsem_downgrade_wake(sem);
}

/*
 * implement atomic add functionality
 */
static inline void rwsem_atomic_add(long delta, struct rw_semaphore *sem)
{
	signed long old, new;

282
	asm volatile(
L
Linus Torvalds 已提交
283
#ifndef __s390x__
284
		"	l	%0,%2\n"
285
		"0:	lr	%1,%0\n"
286 287
		"	ar	%1,%4\n"
		"	cs	%0,%1,%2\n"
288
		"	jl	0b"
L
Linus Torvalds 已提交
289
#else /* __s390x__ */
290
		"	lg	%0,%2\n"
291
		"0:	lgr	%1,%0\n"
292 293
		"	agr	%1,%4\n"
		"	csg	%0,%1,%2\n"
294
		"	jl	0b"
L
Linus Torvalds 已提交
295
#endif /* __s390x__ */
296 297
		: "=&d" (old), "=&d" (new), "=Q" (sem->count)
		: "Q" (sem->count), "d" (delta)
298
		: "cc", "memory");
L
Linus Torvalds 已提交
299 300 301 302 303 304 305 306 307
}

/*
 * implement exchange and add functionality
 */
static inline long rwsem_atomic_update(long delta, struct rw_semaphore *sem)
{
	signed long old, new;

308
	asm volatile(
L
Linus Torvalds 已提交
309
#ifndef __s390x__
310
		"	l	%0,%2\n"
311
		"0:	lr	%1,%0\n"
312 313
		"	ar	%1,%4\n"
		"	cs	%0,%1,%2\n"
314
		"	jl	0b"
L
Linus Torvalds 已提交
315
#else /* __s390x__ */
316
		"	lg	%0,%2\n"
317
		"0:	lgr	%1,%0\n"
318 319
		"	agr	%1,%4\n"
		"	csg	%0,%1,%2\n"
320
		"	jl	0b"
L
Linus Torvalds 已提交
321
#endif /* __s390x__ */
322 323
		: "=&d" (old), "=&d" (new), "=Q" (sem->count)
		: "Q" (sem->count), "d" (delta)
324
		: "cc", "memory");
L
Linus Torvalds 已提交
325 326 327
	return new;
}

328 329 330 331 332
static inline int rwsem_is_locked(struct rw_semaphore *sem)
{
	return (sem->count != 0);
}

L
Linus Torvalds 已提交
333 334
#endif /* __KERNEL__ */
#endif /* _S390_RWSEM_H */