spinlock.h 10.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
6
 * Copyright (C) 1999, 2000, 06 Ralf Baechle (ralf@linux-mips.org)
L
Linus Torvalds 已提交
7 8 9 10 11
 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
 */
#ifndef _ASM_SPINLOCK_H
#define _ASM_SPINLOCK_H

12 13
#include <linux/compiler.h>

14
#include <asm/barrier.h>
15
#include <asm/compiler.h>
L
Linus Torvalds 已提交
16 17 18 19
#include <asm/war.h>

/*
 * Your basic SMP spinlocks, allowing only a single CPU anywhere
20
 *
R
Ralf Baechle 已提交
21
 * Simple spin lock operations.	 There are two variants, one clears IRQ's
22 23 24 25 26
 * on the local processor, one does not.
 *
 * These are fair FIFO ticket locks
 *
 * (the type definitions are in asm/spinlock_types.h)
L
Linus Torvalds 已提交
27 28 29 30
 */


/*
31 32 33 34 35
 * Ticket locks are conceptually two parts, one indicating the current head of
 * the queue, and the other indicating the current tail. The lock is acquired
 * by atomically noting the tail and incrementing it by one (thus adding
 * ourself to the queue and noting our position), then waiting until the head
 * becomes equal to the the initial value of the tail.
L
Linus Torvalds 已提交
36 37
 */

38
static inline int arch_spin_is_locked(arch_spinlock_t *lock)
39
{
D
David Daney 已提交
40
	u32 counters = ACCESS_ONCE(lock->lock);
41

D
David Daney 已提交
42
	return ((counters >> 16) ^ counters) & 0xffff;
43 44
}

45 46 47
#define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
#define arch_spin_unlock_wait(x) \
	while (arch_spin_is_locked(x)) { cpu_relax(); }
48

49
static inline int arch_spin_is_contended(arch_spinlock_t *lock)
50
{
D
David Daney 已提交
51
	u32 counters = ACCESS_ONCE(lock->lock);
52

D
David Daney 已提交
53
	return (((counters >> 16) - counters) & 0xffff) > 1;
54
}
55
#define arch_spin_is_contended	arch_spin_is_contended
56

57
static inline void arch_spin_lock(arch_spinlock_t *lock)
L
Linus Torvalds 已提交
58
{
59 60
	int my_ticket;
	int tmp;
D
David Daney 已提交
61
	int inc = 0x10000;
L
Linus Torvalds 已提交
62 63

	if (R10000_LLSC_WAR) {
64
		__asm__ __volatile__ (
65
		"	.set push		# arch_spin_lock	\n"
66 67 68
		"	.set noreorder					\n"
		"							\n"
		"1:	ll	%[ticket], %[ticket_ptr]		\n"
D
David Daney 已提交
69
		"	addu	%[my_ticket], %[ticket], %[inc]		\n"
70 71
		"	sc	%[my_ticket], %[ticket_ptr]		\n"
		"	beqzl	%[my_ticket], 1b			\n"
L
Linus Torvalds 已提交
72
		"	 nop						\n"
D
David Daney 已提交
73 74
		"	srl	%[my_ticket], %[ticket], 16		\n"
		"	andi	%[ticket], %[ticket], 0xffff		\n"
75 76 77 78
		"	bne	%[ticket], %[my_ticket], 4f		\n"
		"	 subu	%[ticket], %[my_ticket], %[ticket]	\n"
		"2:							\n"
		"	.subsection 2					\n"
D
David Daney 已提交
79
		"4:	andi	%[ticket], %[ticket], 0xffff		\n"
80
		"	sll	%[ticket], 5				\n"
81 82 83 84
		"							\n"
		"6:	bnez	%[ticket], 6b				\n"
		"	 subu	%[ticket], 1				\n"
		"							\n"
D
David Daney 已提交
85
		"	lhu	%[ticket], %[serving_now_ptr]		\n"
86 87
		"	beq	%[ticket], %[my_ticket], 2b		\n"
		"	 subu	%[ticket], %[my_ticket], %[ticket]	\n"
88
		"	b	4b					\n"
89 90 91
		"	 subu	%[ticket], %[ticket], 1			\n"
		"	.previous					\n"
		"	.set pop					\n"
92
		: [ticket_ptr] "+" GCC_OFF_SMALL_ASM() (lock->lock),
D
David Daney 已提交
93
		  [serving_now_ptr] "+m" (lock->h.serving_now),
94
		  [ticket] "=&r" (tmp),
D
David Daney 已提交
95 96
		  [my_ticket] "=&r" (my_ticket)
		: [inc] "r" (inc));
L
Linus Torvalds 已提交
97
	} else {
98
		__asm__ __volatile__ (
99
		"	.set push		# arch_spin_lock	\n"
100 101
		"	.set noreorder					\n"
		"							\n"
D
David Daney 已提交
102 103
		"1:	ll	%[ticket], %[ticket_ptr]		\n"
		"	addu	%[my_ticket], %[ticket], %[inc]		\n"
104
		"	sc	%[my_ticket], %[ticket_ptr]		\n"
D
David Daney 已提交
105 106 107
		"	beqz	%[my_ticket], 1b			\n"
		"	 srl	%[my_ticket], %[ticket], 16		\n"
		"	andi	%[ticket], %[ticket], 0xffff		\n"
108 109 110
		"	bne	%[ticket], %[my_ticket], 4f		\n"
		"	 subu	%[ticket], %[my_ticket], %[ticket]	\n"
		"2:							\n"
111
		"	.subsection 2					\n"
112
		"4:	andi	%[ticket], %[ticket], 0x1fff		\n"
113
		"	sll	%[ticket], 5				\n"
114 115 116 117
		"							\n"
		"6:	bnez	%[ticket], 6b				\n"
		"	 subu	%[ticket], 1				\n"
		"							\n"
D
David Daney 已提交
118
		"	lhu	%[ticket], %[serving_now_ptr]		\n"
119 120
		"	beq	%[ticket], %[my_ticket], 2b		\n"
		"	 subu	%[ticket], %[my_ticket], %[ticket]	\n"
121
		"	b	4b					\n"
122
		"	 subu	%[ticket], %[ticket], 1			\n"
123
		"	.previous					\n"
124
		"	.set pop					\n"
125
		: [ticket_ptr] "+" GCC_OFF_SMALL_ASM() (lock->lock),
D
David Daney 已提交
126
		  [serving_now_ptr] "+m" (lock->h.serving_now),
127
		  [ticket] "=&r" (tmp),
D
David Daney 已提交
128 129
		  [my_ticket] "=&r" (my_ticket)
		: [inc] "r" (inc));
L
Linus Torvalds 已提交
130
	}
131

132
	smp_llsc_mb();
L
Linus Torvalds 已提交
133 134
}

135
static inline void arch_spin_unlock(arch_spinlock_t *lock)
L
Linus Torvalds 已提交
136
{
D
David Daney 已提交
137 138 139 140
	unsigned int serving_now = lock->h.serving_now + 1;
	wmb();
	lock->h.serving_now = (u16)serving_now;
	nudge_writes();
L
Linus Torvalds 已提交
141 142
}

143
static inline unsigned int arch_spin_trylock(arch_spinlock_t *lock)
L
Linus Torvalds 已提交
144
{
145
	int tmp, tmp2, tmp3;
D
David Daney 已提交
146
	int inc = 0x10000;
L
Linus Torvalds 已提交
147 148

	if (R10000_LLSC_WAR) {
149
		__asm__ __volatile__ (
150
		"	.set push		# arch_spin_trylock	\n"
151 152 153
		"	.set noreorder					\n"
		"							\n"
		"1:	ll	%[ticket], %[ticket_ptr]		\n"
D
David Daney 已提交
154 155
		"	srl	%[my_ticket], %[ticket], 16		\n"
		"	andi	%[now_serving], %[ticket], 0xffff	\n"
156
		"	bne	%[my_ticket], %[now_serving], 3f	\n"
D
David Daney 已提交
157
		"	 addu	%[ticket], %[ticket], %[inc]		\n"
158 159 160 161 162 163 164 165 166
		"	sc	%[ticket], %[ticket_ptr]		\n"
		"	beqzl	%[ticket], 1b				\n"
		"	 li	%[ticket], 1				\n"
		"2:							\n"
		"	.subsection 2					\n"
		"3:	b	2b					\n"
		"	 li	%[ticket], 0				\n"
		"	.previous					\n"
		"	.set pop					\n"
167
		: [ticket_ptr] "+" GCC_OFF_SMALL_ASM() (lock->lock),
168 169
		  [ticket] "=&r" (tmp),
		  [my_ticket] "=&r" (tmp2),
D
David Daney 已提交
170 171
		  [now_serving] "=&r" (tmp3)
		: [inc] "r" (inc));
L
Linus Torvalds 已提交
172
	} else {
173
		__asm__ __volatile__ (
174
		"	.set push		# arch_spin_trylock	\n"
175 176
		"	.set noreorder					\n"
		"							\n"
D
David Daney 已提交
177 178 179
		"1:	ll	%[ticket], %[ticket_ptr]		\n"
		"	srl	%[my_ticket], %[ticket], 16		\n"
		"	andi	%[now_serving], %[ticket], 0xffff	\n"
180
		"	bne	%[my_ticket], %[now_serving], 3f	\n"
D
David Daney 已提交
181
		"	 addu	%[ticket], %[ticket], %[inc]		\n"
182
		"	sc	%[ticket], %[ticket_ptr]		\n"
D
David Daney 已提交
183
		"	beqz	%[ticket], 1b				\n"
184 185
		"	 li	%[ticket], 1				\n"
		"2:							\n"
186
		"	.subsection 2					\n"
187 188
		"3:	b	2b					\n"
		"	 li	%[ticket], 0				\n"
189
		"	.previous					\n"
190
		"	.set pop					\n"
191
		: [ticket_ptr] "+" GCC_OFF_SMALL_ASM() (lock->lock),
192 193
		  [ticket] "=&r" (tmp),
		  [my_ticket] "=&r" (tmp2),
D
David Daney 已提交
194 195
		  [now_serving] "=&r" (tmp3)
		: [inc] "r" (inc));
L
Linus Torvalds 已提交
196 197
	}

198
	smp_llsc_mb();
199

200
	return tmp;
L
Linus Torvalds 已提交
201 202 203 204 205 206 207 208 209 210 211
}

/*
 * Read-write spinlocks, allowing multiple readers but only one writer.
 *
 * NOTE! it is quite common to have readers in interrupts but no interrupt
 * writers. For those circumstances we can "mix" irq-safe locks - any writer
 * needs to get a irq-safe write-lock, but readers can get non-irqsafe
 * read-locks.
 */

212 213 214 215
/*
 * read_can_lock - would read_trylock() succeed?
 * @lock: the rwlock in question.
 */
216
#define arch_read_can_lock(rw)	((rw)->lock >= 0)
217 218 219 220 221

/*
 * write_can_lock - would write_trylock() succeed?
 * @lock: the rwlock in question.
 */
R
Ralf Baechle 已提交
222
#define arch_write_can_lock(rw) (!(rw)->lock)
223

224
static inline void arch_read_lock(arch_rwlock_t *rw)
L
Linus Torvalds 已提交
225 226 227 228 229
{
	unsigned int tmp;

	if (R10000_LLSC_WAR) {
		__asm__ __volatile__(
230
		"	.set	noreorder	# arch_read_lock	\n"
L
Linus Torvalds 已提交
231 232 233 234 235 236 237
		"1:	ll	%1, %2					\n"
		"	bltz	%1, 1b					\n"
		"	 addu	%1, 1					\n"
		"	sc	%1, %0					\n"
		"	beqzl	%1, 1b					\n"
		"	 nop						\n"
		"	.set	reorder					\n"
238 239
		: "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp)
		: GCC_OFF_SMALL_ASM() (rw->lock)
L
Linus Torvalds 已提交
240 241
		: "memory");
	} else {
242 243 244 245 246 247
		do {
			__asm__ __volatile__(
			"1:	ll	%1, %2	# arch_read_lock	\n"
			"	bltz	%1, 1b				\n"
			"	 addu	%1, 1				\n"
			"2:	sc	%1, %0				\n"
248 249
			: "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp)
			: GCC_OFF_SMALL_ASM() (rw->lock)
250 251
			: "memory");
		} while (unlikely(!tmp));
L
Linus Torvalds 已提交
252
	}
253

254
	smp_llsc_mb();
L
Linus Torvalds 已提交
255 256
}

257
static inline void arch_read_unlock(arch_rwlock_t *rw)
L
Linus Torvalds 已提交
258 259 260
{
	unsigned int tmp;

261
	smp_mb__before_llsc();
262

L
Linus Torvalds 已提交
263 264
	if (R10000_LLSC_WAR) {
		__asm__ __volatile__(
265
		"1:	ll	%1, %2		# arch_read_unlock	\n"
266
		"	addiu	%1, -1					\n"
L
Linus Torvalds 已提交
267 268
		"	sc	%1, %0					\n"
		"	beqzl	%1, 1b					\n"
269 270
		: "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp)
		: GCC_OFF_SMALL_ASM() (rw->lock)
L
Linus Torvalds 已提交
271 272
		: "memory");
	} else {
273 274 275
		do {
			__asm__ __volatile__(
			"1:	ll	%1, %2	# arch_read_unlock	\n"
276
			"	addiu	%1, -1				\n"
277
			"	sc	%1, %0				\n"
278 279
			: "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp)
			: GCC_OFF_SMALL_ASM() (rw->lock)
280 281
			: "memory");
		} while (unlikely(!tmp));
L
Linus Torvalds 已提交
282 283 284
	}
}

285
static inline void arch_write_lock(arch_rwlock_t *rw)
L
Linus Torvalds 已提交
286 287 288 289 290
{
	unsigned int tmp;

	if (R10000_LLSC_WAR) {
		__asm__ __volatile__(
291
		"	.set	noreorder	# arch_write_lock	\n"
L
Linus Torvalds 已提交
292 293 294 295 296
		"1:	ll	%1, %2					\n"
		"	bnez	%1, 1b					\n"
		"	 lui	%1, 0x8000				\n"
		"	sc	%1, %0					\n"
		"	beqzl	%1, 1b					\n"
297
		"	 nop						\n"
L
Linus Torvalds 已提交
298
		"	.set	reorder					\n"
299 300
		: "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp)
		: GCC_OFF_SMALL_ASM() (rw->lock)
L
Linus Torvalds 已提交
301 302
		: "memory");
	} else {
303 304 305 306 307 308
		do {
			__asm__ __volatile__(
			"1:	ll	%1, %2	# arch_write_lock	\n"
			"	bnez	%1, 1b				\n"
			"	 lui	%1, 0x8000			\n"
			"2:	sc	%1, %0				\n"
309 310
			: "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp)
			: GCC_OFF_SMALL_ASM() (rw->lock)
311 312
			: "memory");
		} while (unlikely(!tmp));
L
Linus Torvalds 已提交
313
	}
314

315
	smp_llsc_mb();
L
Linus Torvalds 已提交
316 317
}

318
static inline void arch_write_unlock(arch_rwlock_t *rw)
L
Linus Torvalds 已提交
319
{
320
	smp_mb__before_llsc();
321

L
Linus Torvalds 已提交
322
	__asm__ __volatile__(
323
	"				# arch_write_unlock	\n"
L
Linus Torvalds 已提交
324 325 326 327 328 329
	"	sw	$0, %0					\n"
	: "=m" (rw->lock)
	: "m" (rw->lock)
	: "memory");
}

330
static inline int arch_read_trylock(arch_rwlock_t *rw)
331 332 333 334 335 336
{
	unsigned int tmp;
	int ret;

	if (R10000_LLSC_WAR) {
		__asm__ __volatile__(
337
		"	.set	noreorder	# arch_read_trylock	\n"
338 339
		"	li	%2, 0					\n"
		"1:	ll	%1, %3					\n"
340
		"	bltz	%1, 2f					\n"
341 342 343
		"	 addu	%1, 1					\n"
		"	sc	%1, %0					\n"
		"	.set	reorder					\n"
344 345
		"	beqzl	%1, 1b					\n"
		"	 nop						\n"
346
		__WEAK_LLSC_MB
347 348
		"	li	%2, 1					\n"
		"2:							\n"
349 350
		: "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp), "=&r" (ret)
		: GCC_OFF_SMALL_ASM() (rw->lock)
351 352 353
		: "memory");
	} else {
		__asm__ __volatile__(
354
		"	.set	noreorder	# arch_read_trylock	\n"
355 356
		"	li	%2, 0					\n"
		"1:	ll	%1, %3					\n"
357
		"	bltz	%1, 2f					\n"
358 359 360
		"	 addu	%1, 1					\n"
		"	sc	%1, %0					\n"
		"	beqz	%1, 1b					\n"
361
		"	 nop						\n"
362
		"	.set	reorder					\n"
363
		__WEAK_LLSC_MB
364 365
		"	li	%2, 1					\n"
		"2:							\n"
366 367
		: "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp), "=&r" (ret)
		: GCC_OFF_SMALL_ASM() (rw->lock)
368 369 370 371 372
		: "memory");
	}

	return ret;
}
L
Linus Torvalds 已提交
373

374
static inline int arch_write_trylock(arch_rwlock_t *rw)
L
Linus Torvalds 已提交
375 376 377 378 379 380
{
	unsigned int tmp;
	int ret;

	if (R10000_LLSC_WAR) {
		__asm__ __volatile__(
381
		"	.set	noreorder	# arch_write_trylock	\n"
L
Linus Torvalds 已提交
382 383 384 385 386 387
		"	li	%2, 0					\n"
		"1:	ll	%1, %3					\n"
		"	bnez	%1, 2f					\n"
		"	 lui	%1, 0x8000				\n"
		"	sc	%1, %0					\n"
		"	beqzl	%1, 1b					\n"
388
		"	 nop						\n"
389
		__WEAK_LLSC_MB
L
Linus Torvalds 已提交
390 391 392
		"	li	%2, 1					\n"
		"	.set	reorder					\n"
		"2:							\n"
393 394
		: "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp), "=&r" (ret)
		: GCC_OFF_SMALL_ASM() (rw->lock)
L
Linus Torvalds 已提交
395 396
		: "memory");
	} else {
397 398 399 400 401 402 403 404 405
		do {
			__asm__ __volatile__(
			"	ll	%1, %3	# arch_write_trylock	\n"
			"	li	%2, 0				\n"
			"	bnez	%1, 2f				\n"
			"	lui	%1, 0x8000			\n"
			"	sc	%1, %0				\n"
			"	li	%2, 1				\n"
			"2:						\n"
406
			: "=" GCC_OFF_SMALL_ASM() (rw->lock), "=&r" (tmp),
407
			  "=&r" (ret)
408
			: GCC_OFF_SMALL_ASM() (rw->lock)
409 410 411 412
			: "memory");
		} while (unlikely(!tmp));

		smp_llsc_mb();
L
Linus Torvalds 已提交
413 414 415 416 417
	}

	return ret;
}

418 419
#define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
#define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
420

421 422 423
#define arch_spin_relax(lock)	cpu_relax()
#define arch_read_relax(lock)	cpu_relax()
#define arch_write_relax(lock)	cpu_relax()
424

L
Linus Torvalds 已提交
425
#endif /* _ASM_SPINLOCK_H */