spinlock.h 12.0 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>
L
Linus Torvalds 已提交
15 16 17 18
#include <asm/war.h>

/*
 * Your basic SMP spinlocks, allowing only a single CPU anywhere
19 20 21 22 23 24 25
 *
 * Simple spin lock operations.  There are two variants, one clears IRQ's
 * 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 已提交
26 27 28 29
 */


/*
30 31 32 33 34
 * 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 已提交
35 36
 */

37
static inline int __raw_spin_is_locked(arch_spinlock_t *lock)
38 39 40 41 42 43 44 45 46 47
{
	unsigned int counters = ACCESS_ONCE(lock->lock);

	return ((counters >> 14) ^ counters) & 0x1fff;
}

#define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
#define __raw_spin_unlock_wait(x) \
	while (__raw_spin_is_locked(x)) { cpu_relax(); }

48
static inline int __raw_spin_is_contended(arch_spinlock_t *lock)
49 50 51 52 53
{
	unsigned int counters = ACCESS_ONCE(lock->lock);

	return (((counters >> 14) - counters) & 0x1fff) > 1;
}
54
#define __raw_spin_is_contended	__raw_spin_is_contended
55

56
static inline void __raw_spin_lock(arch_spinlock_t *lock)
L
Linus Torvalds 已提交
57
{
58 59
	int my_ticket;
	int tmp;
L
Linus Torvalds 已提交
60 61

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

134
	smp_llsc_mb();
L
Linus Torvalds 已提交
135 136
}

137
static inline void __raw_spin_unlock(arch_spinlock_t *lock)
L
Linus Torvalds 已提交
138
{
139 140 141 142 143 144 145 146 147 148 149 150
	int tmp;

	smp_llsc_mb();

	if (R10000_LLSC_WAR) {
		__asm__ __volatile__ (
		"				# __raw_spin_unlock	\n"
		"1:	ll	%[ticket], %[ticket_ptr]		\n"
		"	addiu	%[ticket], %[ticket], 1			\n"
		"	ori	%[ticket], %[ticket], 0x2000		\n"
		"	xori	%[ticket], %[ticket], 0x2000		\n"
		"	sc	%[ticket], %[ticket_ptr]		\n"
151
		"	beqzl	%[ticket], 1b				\n"
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
		: [ticket_ptr] "+m" (lock->lock),
		  [ticket] "=&r" (tmp));
	} else {
		__asm__ __volatile__ (
		"	.set push		# __raw_spin_unlock	\n"
		"	.set noreorder					\n"
		"							\n"
		"	ll	%[ticket], %[ticket_ptr]		\n"
		"1:	addiu	%[ticket], %[ticket], 1			\n"
		"	ori	%[ticket], %[ticket], 0x2000		\n"
		"	xori	%[ticket], %[ticket], 0x2000		\n"
		"	sc	%[ticket], %[ticket_ptr]		\n"
		"	beqz	%[ticket], 2f				\n"
		"	 nop						\n"
		"							\n"
		"	.subsection 2					\n"
		"2:	b	1b					\n"
		"	 ll	%[ticket], %[ticket_ptr]		\n"
		"	.previous					\n"
		"	.set pop					\n"
		: [ticket_ptr] "+m" (lock->lock),
		  [ticket] "=&r" (tmp));
	}
L
Linus Torvalds 已提交
175 176
}

177
static inline unsigned int __raw_spin_trylock(arch_spinlock_t *lock)
L
Linus Torvalds 已提交
178
{
179
	int tmp, tmp2, tmp3;
L
Linus Torvalds 已提交
180 181

	if (R10000_LLSC_WAR) {
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
		__asm__ __volatile__ (
		"	.set push		# __raw_spin_trylock	\n"
		"	.set noreorder					\n"
		"							\n"
		"1:	ll	%[ticket], %[ticket_ptr]		\n"
		"	srl	%[my_ticket], %[ticket], 14		\n"
		"	andi	%[my_ticket], %[my_ticket], 0x1fff	\n"
		"	andi	%[now_serving], %[ticket], 0x1fff	\n"
		"	bne	%[my_ticket], %[now_serving], 3f	\n"
		"	 addiu	%[ticket], %[ticket], 0x4000		\n"
		"	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"
		: [ticket_ptr] "+m" (lock->lock),
		  [ticket] "=&r" (tmp),
		  [my_ticket] "=&r" (tmp2),
		  [now_serving] "=&r" (tmp3));
L
Linus Torvalds 已提交
205
	} else {
206 207 208 209 210 211 212 213 214 215 216 217 218 219
		__asm__ __volatile__ (
		"	.set push		# __raw_spin_trylock	\n"
		"	.set noreorder					\n"
		"							\n"
		"	ll	%[ticket], %[ticket_ptr]		\n"
		"1:	srl	%[my_ticket], %[ticket], 14		\n"
		"	andi	%[my_ticket], %[my_ticket], 0x1fff	\n"
		"	andi	%[now_serving], %[ticket], 0x1fff	\n"
		"	bne	%[my_ticket], %[now_serving], 3f	\n"
		"	 addiu	%[ticket], %[ticket], 0x4000		\n"
		"	sc	%[ticket], %[ticket_ptr]		\n"
		"	beqz	%[ticket], 4f				\n"
		"	 li	%[ticket], 1				\n"
		"2:							\n"
220
		"	.subsection 2					\n"
221 222 223 224
		"3:	b	2b					\n"
		"	 li	%[ticket], 0				\n"
		"4:	b	1b					\n"
		"	 ll	%[ticket], %[ticket_ptr]		\n"
225
		"	.previous					\n"
226 227 228 229 230
		"	.set pop					\n"
		: [ticket_ptr] "+m" (lock->lock),
		  [ticket] "=&r" (tmp),
		  [my_ticket] "=&r" (tmp2),
		  [now_serving] "=&r" (tmp3));
L
Linus Torvalds 已提交
231 232
	}

233
	smp_llsc_mb();
234

235
	return tmp;
L
Linus Torvalds 已提交
236 237 238 239 240 241 242 243 244 245 246
}

/*
 * 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.
 */

247 248 249 250 251 252 253 254 255 256 257 258
/*
 * read_can_lock - would read_trylock() succeed?
 * @lock: the rwlock in question.
 */
#define __raw_read_can_lock(rw)	((rw)->lock >= 0)

/*
 * write_can_lock - would write_trylock() succeed?
 * @lock: the rwlock in question.
 */
#define __raw_write_can_lock(rw)	(!(rw)->lock)

I
Ingo Molnar 已提交
259
static inline void __raw_read_lock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
260 261 262 263 264
{
	unsigned int tmp;

	if (R10000_LLSC_WAR) {
		__asm__ __volatile__(
I
Ingo Molnar 已提交
265
		"	.set	noreorder	# __raw_read_lock	\n"
L
Linus Torvalds 已提交
266 267 268 269 270 271 272 273 274 275 276 277
		"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"
		: "=m" (rw->lock), "=&r" (tmp)
		: "m" (rw->lock)
		: "memory");
	} else {
		__asm__ __volatile__(
I
Ingo Molnar 已提交
278
		"	.set	noreorder	# __raw_read_lock	\n"
L
Linus Torvalds 已提交
279
		"1:	ll	%1, %2					\n"
280
		"	bltz	%1, 2f					\n"
L
Linus Torvalds 已提交
281 282 283
		"	 addu	%1, 1					\n"
		"	sc	%1, %0					\n"
		"	beqz	%1, 1b					\n"
284
		"	 nop						\n"
285 286 287 288 289 290 291
		"	.subsection 2					\n"
		"2:	ll	%1, %2					\n"
		"	bltz	%1, 2b					\n"
		"	 addu	%1, 1					\n"
		"	b	1b					\n"
		"	 nop						\n"
		"	.previous					\n"
L
Linus Torvalds 已提交
292 293 294 295 296
		"	.set	reorder					\n"
		: "=m" (rw->lock), "=&r" (tmp)
		: "m" (rw->lock)
		: "memory");
	}
297

298
	smp_llsc_mb();
L
Linus Torvalds 已提交
299 300 301 302 303
}

/* Note the use of sub, not subu which will make the kernel die with an
   overflow exception if we ever try to unlock an rwlock that is already
   unlocked or is being held by a writer.  */
I
Ingo Molnar 已提交
304
static inline void __raw_read_unlock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
305 306 307
{
	unsigned int tmp;

308
	smp_llsc_mb();
309

L
Linus Torvalds 已提交
310 311
	if (R10000_LLSC_WAR) {
		__asm__ __volatile__(
I
Ingo Molnar 已提交
312
		"1:	ll	%1, %2		# __raw_read_unlock	\n"
L
Linus Torvalds 已提交
313 314 315 316 317 318 319 320
		"	sub	%1, 1					\n"
		"	sc	%1, %0					\n"
		"	beqzl	%1, 1b					\n"
		: "=m" (rw->lock), "=&r" (tmp)
		: "m" (rw->lock)
		: "memory");
	} else {
		__asm__ __volatile__(
I
Ingo Molnar 已提交
321
		"	.set	noreorder	# __raw_read_unlock	\n"
L
Linus Torvalds 已提交
322 323 324
		"1:	ll	%1, %2					\n"
		"	sub	%1, 1					\n"
		"	sc	%1, %0					\n"
325 326 327 328
		"	beqz	%1, 2f					\n"
		"	 nop						\n"
		"	.subsection 2					\n"
		"2:	b	1b					\n"
329
		"	 nop						\n"
330
		"	.previous					\n"
L
Linus Torvalds 已提交
331 332 333 334 335 336 337
		"	.set	reorder					\n"
		: "=m" (rw->lock), "=&r" (tmp)
		: "m" (rw->lock)
		: "memory");
	}
}

I
Ingo Molnar 已提交
338
static inline void __raw_write_lock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
339 340 341 342 343
{
	unsigned int tmp;

	if (R10000_LLSC_WAR) {
		__asm__ __volatile__(
I
Ingo Molnar 已提交
344
		"	.set	noreorder	# __raw_write_lock	\n"
L
Linus Torvalds 已提交
345 346 347 348 349
		"1:	ll	%1, %2					\n"
		"	bnez	%1, 1b					\n"
		"	 lui	%1, 0x8000				\n"
		"	sc	%1, %0					\n"
		"	beqzl	%1, 1b					\n"
350
		"	 nop						\n"
L
Linus Torvalds 已提交
351 352 353 354 355 356
		"	.set	reorder					\n"
		: "=m" (rw->lock), "=&r" (tmp)
		: "m" (rw->lock)
		: "memory");
	} else {
		__asm__ __volatile__(
I
Ingo Molnar 已提交
357
		"	.set	noreorder	# __raw_write_lock	\n"
L
Linus Torvalds 已提交
358
		"1:	ll	%1, %2					\n"
359
		"	bnez	%1, 2f					\n"
L
Linus Torvalds 已提交
360 361
		"	 lui	%1, 0x8000				\n"
		"	sc	%1, %0					\n"
362 363 364 365 366 367 368
		"	beqz	%1, 2f					\n"
		"	 nop						\n"
		"	.subsection 2					\n"
		"2:	ll	%1, %2					\n"
		"	bnez	%1, 2b					\n"
		"	 lui	%1, 0x8000				\n"
		"	b	1b					\n"
369
		"	 nop						\n"
370
		"	.previous					\n"
L
Linus Torvalds 已提交
371 372 373 374 375
		"	.set	reorder					\n"
		: "=m" (rw->lock), "=&r" (tmp)
		: "m" (rw->lock)
		: "memory");
	}
376

377
	smp_llsc_mb();
L
Linus Torvalds 已提交
378 379
}

I
Ingo Molnar 已提交
380
static inline void __raw_write_unlock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
381
{
382 383
	smp_mb();

L
Linus Torvalds 已提交
384
	__asm__ __volatile__(
385
	"				# __raw_write_unlock	\n"
L
Linus Torvalds 已提交
386 387 388 389 390 391
	"	sw	$0, %0					\n"
	: "=m" (rw->lock)
	: "m" (rw->lock)
	: "memory");
}

392 393 394 395 396 397 398 399 400 401
static inline int __raw_read_trylock(raw_rwlock_t *rw)
{
	unsigned int tmp;
	int ret;

	if (R10000_LLSC_WAR) {
		__asm__ __volatile__(
		"	.set	noreorder	# __raw_read_trylock	\n"
		"	li	%2, 0					\n"
		"1:	ll	%1, %3					\n"
402
		"	bltz	%1, 2f					\n"
403 404 405
		"	 addu	%1, 1					\n"
		"	sc	%1, %0					\n"
		"	.set	reorder					\n"
406 407
		"	beqzl	%1, 1b					\n"
		"	 nop						\n"
408
		__WEAK_LLSC_MB
409 410 411 412 413 414 415 416 417 418
		"	li	%2, 1					\n"
		"2:							\n"
		: "=m" (rw->lock), "=&r" (tmp), "=&r" (ret)
		: "m" (rw->lock)
		: "memory");
	} else {
		__asm__ __volatile__(
		"	.set	noreorder	# __raw_read_trylock	\n"
		"	li	%2, 0					\n"
		"1:	ll	%1, %3					\n"
419
		"	bltz	%1, 2f					\n"
420 421 422
		"	 addu	%1, 1					\n"
		"	sc	%1, %0					\n"
		"	beqz	%1, 1b					\n"
423
		"	 nop						\n"
424
		"	.set	reorder					\n"
425
		__WEAK_LLSC_MB
426 427 428 429 430 431 432 433 434
		"	li	%2, 1					\n"
		"2:							\n"
		: "=m" (rw->lock), "=&r" (tmp), "=&r" (ret)
		: "m" (rw->lock)
		: "memory");
	}

	return ret;
}
L
Linus Torvalds 已提交
435

I
Ingo Molnar 已提交
436
static inline int __raw_write_trylock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
437 438 439 440 441 442
{
	unsigned int tmp;
	int ret;

	if (R10000_LLSC_WAR) {
		__asm__ __volatile__(
I
Ingo Molnar 已提交
443
		"	.set	noreorder	# __raw_write_trylock	\n"
L
Linus Torvalds 已提交
444 445 446 447 448 449
		"	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"
450
		"	 nop						\n"
451
		__WEAK_LLSC_MB
L
Linus Torvalds 已提交
452 453 454 455 456 457 458 459
		"	li	%2, 1					\n"
		"	.set	reorder					\n"
		"2:							\n"
		: "=m" (rw->lock), "=&r" (tmp), "=&r" (ret)
		: "m" (rw->lock)
		: "memory");
	} else {
		__asm__ __volatile__(
I
Ingo Molnar 已提交
460
		"	.set	noreorder	# __raw_write_trylock	\n"
L
Linus Torvalds 已提交
461 462 463 464 465
		"	li	%2, 0					\n"
		"1:	ll	%1, %3					\n"
		"	bnez	%1, 2f					\n"
		"	lui	%1, 0x8000				\n"
		"	sc	%1, %0					\n"
466 467 468
		"	beqz	%1, 3f					\n"
		"	 li	%2, 1					\n"
		"2:							\n"
469
		__WEAK_LLSC_MB
470 471 472 473
		"	.subsection 2					\n"
		"3:	b	1b					\n"
		"	 li	%2, 0					\n"
		"	.previous					\n"
L
Linus Torvalds 已提交
474 475 476 477 478 479 480 481 482
		"	.set	reorder					\n"
		: "=m" (rw->lock), "=&r" (tmp), "=&r" (ret)
		: "m" (rw->lock)
		: "memory");
	}

	return ret;
}

483 484
#define __raw_read_lock_flags(lock, flags) __raw_read_lock(lock)
#define __raw_write_lock_flags(lock, flags) __raw_write_lock(lock)
485

486 487 488 489
#define _raw_spin_relax(lock)	cpu_relax()
#define _raw_read_relax(lock)	cpu_relax()
#define _raw_write_relax(lock)	cpu_relax()

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