bitops.h 13.7 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) 1994 - 1997, 99, 2000, 06, 07  Ralf Baechle (ralf@linux-mips.org)
L
Linus Torvalds 已提交
7 8 9 10 11 12
 * Copyright (c) 1999, 2000  Silicon Graphics, Inc.
 */
#ifndef _ASM_BITOPS_H
#define _ASM_BITOPS_H

#include <linux/compiler.h>
13
#include <linux/irqflags.h>
L
Linus Torvalds 已提交
14
#include <linux/types.h>
15
#include <asm/barrier.h>
16
#include <asm/bug.h>
L
Linus Torvalds 已提交
17 18
#include <asm/byteorder.h>		/* sigh ... */
#include <asm/cpu-features.h>
19 20
#include <asm/sgidefs.h>
#include <asm/war.h>
L
Linus Torvalds 已提交
21 22 23 24

#if (_MIPS_SZLONG == 32)
#define SZLONG_LOG 5
#define SZLONG_MASK 31UL
25 26
#define __LL		"ll	"
#define __SC		"sc	"
27 28
#define __INS		"ins    "
#define __EXT		"ext    "
L
Linus Torvalds 已提交
29 30 31
#elif (_MIPS_SZLONG == 64)
#define SZLONG_LOG 6
#define SZLONG_MASK 63UL
32 33
#define __LL		"lld	"
#define __SC		"scd	"
34 35
#define __INS		"dins    "
#define __EXT		"dext    "
L
Linus Torvalds 已提交
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
#endif

/*
 * clear_bit() doesn't provide any barrier for the compiler.
 */
#define smp_mb__before_clear_bit()	smp_mb()
#define smp_mb__after_clear_bit()	smp_mb()

/*
 * set_bit - Atomically set a bit in memory
 * @nr: the bit to set
 * @addr: the address to start counting from
 *
 * This function is atomic and may not be reordered.  See __set_bit()
 * if you do not require the atomic guarantees.
 * Note that @nr may be almost arbitrarily large; this function is not
 * restricted to acting on a single-word quantity.
 */
static inline void set_bit(unsigned long nr, volatile unsigned long *addr)
{
	unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
	unsigned long temp;

	if (cpu_has_llsc && R10000_LLSC_WAR) {
		__asm__ __volatile__(
61
		"	.set	mips3					\n"
L
Linus Torvalds 已提交
62 63
		"1:	" __LL "%0, %1			# set_bit	\n"
		"	or	%0, %2					\n"
64
		"	" __SC	"%0, %1					\n"
L
Linus Torvalds 已提交
65
		"	beqzl	%0, 1b					\n"
66
		"	.set	mips0					\n"
L
Linus Torvalds 已提交
67 68
		: "=&r" (temp), "=m" (*m)
		: "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m));
69 70 71 72 73 74 75 76 77 78 79 80 81
#ifdef CONFIG_CPU_MIPSR2
	} else if (__builtin_constant_p(nr)) {
		__asm__ __volatile__(
		"1:	" __LL "%0, %1			# set_bit	\n"
		"	" __INS "%0, %4, %2, 1				\n"
		"	" __SC "%0, %1					\n"
		"	beqz	%0, 2f					\n"
		"	.subsection 2					\n"
		"2:	b	1b					\n"
		"	.previous					\n"
		: "=&r" (temp), "=m" (*m)
		: "ir" (nr & SZLONG_MASK), "m" (*m), "r" (~0));
#endif /* CONFIG_CPU_MIPSR2 */
L
Linus Torvalds 已提交
82 83
	} else if (cpu_has_llsc) {
		__asm__ __volatile__(
84
		"	.set	mips3					\n"
L
Linus Torvalds 已提交
85 86
		"1:	" __LL "%0, %1			# set_bit	\n"
		"	or	%0, %2					\n"
87
		"	" __SC	"%0, %1					\n"
88 89 90 91
		"	beqz	%0, 2f					\n"
		"	.subsection 2					\n"
		"2:	b	1b					\n"
		"	.previous					\n"
92
		"	.set	mips0					\n"
L
Linus Torvalds 已提交
93 94 95 96 97
		: "=&r" (temp), "=m" (*m)
		: "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m));
	} else {
		volatile unsigned long *a = addr;
		unsigned long mask;
98
		unsigned long flags;
L
Linus Torvalds 已提交
99 100 101

		a += nr >> SZLONG_LOG;
		mask = 1UL << (nr & SZLONG_MASK);
102
		local_irq_save(flags);
L
Linus Torvalds 已提交
103
		*a |= mask;
104
		local_irq_restore(flags);
L
Linus Torvalds 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	}
}

/*
 * clear_bit - Clears a bit in memory
 * @nr: Bit to clear
 * @addr: Address to start counting from
 *
 * clear_bit() is atomic and may not be reordered.  However, it does
 * not contain a memory barrier, so if it is used for locking purposes,
 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
 * in order to ensure changes are visible on other processors.
 */
static inline void clear_bit(unsigned long nr, volatile unsigned long *addr)
{
	unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
	unsigned long temp;

	if (cpu_has_llsc && R10000_LLSC_WAR) {
		__asm__ __volatile__(
125
		"	.set	mips3					\n"
L
Linus Torvalds 已提交
126 127 128 129
		"1:	" __LL "%0, %1			# clear_bit	\n"
		"	and	%0, %2					\n"
		"	" __SC "%0, %1					\n"
		"	beqzl	%0, 1b					\n"
130
		"	.set	mips0					\n"
L
Linus Torvalds 已提交
131 132
		: "=&r" (temp), "=m" (*m)
		: "ir" (~(1UL << (nr & SZLONG_MASK))), "m" (*m));
133 134 135 136 137 138 139 140 141 142 143 144 145
#ifdef CONFIG_CPU_MIPSR2
	} else if (__builtin_constant_p(nr)) {
		__asm__ __volatile__(
		"1:	" __LL "%0, %1			# clear_bit	\n"
		"	" __INS "%0, $0, %2, 1				\n"
		"	" __SC "%0, %1					\n"
		"	beqz	%0, 2f					\n"
		"	.subsection 2					\n"
		"2:	b	1b					\n"
		"	.previous					\n"
		: "=&r" (temp), "=m" (*m)
		: "ir" (nr & SZLONG_MASK), "m" (*m));
#endif /* CONFIG_CPU_MIPSR2 */
L
Linus Torvalds 已提交
146 147
	} else if (cpu_has_llsc) {
		__asm__ __volatile__(
148
		"	.set	mips3					\n"
L
Linus Torvalds 已提交
149 150 151
		"1:	" __LL "%0, %1			# clear_bit	\n"
		"	and	%0, %2					\n"
		"	" __SC "%0, %1					\n"
152 153 154 155
		"	beqz	%0, 2f					\n"
		"	.subsection 2					\n"
		"2:	b	1b					\n"
		"	.previous					\n"
156
		"	.set	mips0					\n"
L
Linus Torvalds 已提交
157 158 159 160 161
		: "=&r" (temp), "=m" (*m)
		: "ir" (~(1UL << (nr & SZLONG_MASK))), "m" (*m));
	} else {
		volatile unsigned long *a = addr;
		unsigned long mask;
162
		unsigned long flags;
L
Linus Torvalds 已提交
163 164 165

		a += nr >> SZLONG_LOG;
		mask = 1UL << (nr & SZLONG_MASK);
166
		local_irq_save(flags);
L
Linus Torvalds 已提交
167
		*a &= ~mask;
168
		local_irq_restore(flags);
L
Linus Torvalds 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
	}
}

/*
 * change_bit - Toggle a bit in memory
 * @nr: Bit to change
 * @addr: Address to start counting from
 *
 * change_bit() is atomic and may not be reordered.
 * Note that @nr may be almost arbitrarily large; this function is not
 * restricted to acting on a single-word quantity.
 */
static inline void change_bit(unsigned long nr, volatile unsigned long *addr)
{
	if (cpu_has_llsc && R10000_LLSC_WAR) {
		unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
		unsigned long temp;

		__asm__ __volatile__(
188
		"	.set	mips3				\n"
L
Linus Torvalds 已提交
189 190
		"1:	" __LL "%0, %1		# change_bit	\n"
		"	xor	%0, %2				\n"
191
		"	" __SC	"%0, %1				\n"
L
Linus Torvalds 已提交
192
		"	beqzl	%0, 1b				\n"
193
		"	.set	mips0				\n"
L
Linus Torvalds 已提交
194 195 196 197 198 199 200
		: "=&r" (temp), "=m" (*m)
		: "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m));
	} else if (cpu_has_llsc) {
		unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
		unsigned long temp;

		__asm__ __volatile__(
201
		"	.set	mips3				\n"
L
Linus Torvalds 已提交
202 203
		"1:	" __LL "%0, %1		# change_bit	\n"
		"	xor	%0, %2				\n"
204
		"	" __SC	"%0, %1				\n"
205 206 207 208
		"	beqz	%0, 2f				\n"
		"	.subsection 2				\n"
		"2:	b	1b				\n"
		"	.previous				\n"
209
		"	.set	mips0				\n"
L
Linus Torvalds 已提交
210 211 212 213 214
		: "=&r" (temp), "=m" (*m)
		: "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m));
	} else {
		volatile unsigned long *a = addr;
		unsigned long mask;
215
		unsigned long flags;
L
Linus Torvalds 已提交
216 217 218

		a += nr >> SZLONG_LOG;
		mask = 1UL << (nr & SZLONG_MASK);
219
		local_irq_save(flags);
L
Linus Torvalds 已提交
220
		*a ^= mask;
221
		local_irq_restore(flags);
L
Linus Torvalds 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
	}
}

/*
 * test_and_set_bit - Set a bit and return its old value
 * @nr: Bit to set
 * @addr: Address to count from
 *
 * This operation is atomic and cannot be reordered.
 * It also implies a memory barrier.
 */
static inline int test_and_set_bit(unsigned long nr,
	volatile unsigned long *addr)
{
	if (cpu_has_llsc && R10000_LLSC_WAR) {
		unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
		unsigned long temp, res;

		__asm__ __volatile__(
241
		"	.set	mips3					\n"
L
Linus Torvalds 已提交
242 243 244 245 246
		"1:	" __LL "%0, %1		# test_and_set_bit	\n"
		"	or	%2, %0, %3				\n"
		"	" __SC	"%2, %1					\n"
		"	beqzl	%2, 1b					\n"
		"	and	%2, %0, %3				\n"
247
		"	.set	mips0					\n"
L
Linus Torvalds 已提交
248 249 250 251 252 253 254 255 256 257
		: "=&r" (temp), "=m" (*m), "=&r" (res)
		: "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
		: "memory");

		return res != 0;
	} else if (cpu_has_llsc) {
		unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
		unsigned long temp, res;

		__asm__ __volatile__(
258 259
		"	.set	push					\n"
		"	.set	noreorder				\n"
260
		"	.set	mips3					\n"
261
		"1:	" __LL "%0, %1		# test_and_set_bit	\n"
L
Linus Torvalds 已提交
262 263
		"	or	%2, %0, %3				\n"
		"	" __SC	"%2, %1					\n"
264
		"	beqz	%2, 2f					\n"
L
Linus Torvalds 已提交
265
		"	 and	%2, %0, %3				\n"
266 267 268 269
		"	.subsection 2					\n"
		"2:	b	1b					\n"
		"	 nop						\n"
		"	.previous					\n"
270
		"	.set	pop					\n"
L
Linus Torvalds 已提交
271 272 273 274 275 276 277 278 279
		: "=&r" (temp), "=m" (*m), "=&r" (res)
		: "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
		: "memory");

		return res != 0;
	} else {
		volatile unsigned long *a = addr;
		unsigned long mask;
		int retval;
280
		unsigned long flags;
L
Linus Torvalds 已提交
281 282 283

		a += nr >> SZLONG_LOG;
		mask = 1UL << (nr & SZLONG_MASK);
284
		local_irq_save(flags);
L
Linus Torvalds 已提交
285 286
		retval = (mask & *a) != 0;
		*a |= mask;
287
		local_irq_restore(flags);
L
Linus Torvalds 已提交
288 289 290

		return retval;
	}
291 292

	smp_mb();
L
Linus Torvalds 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
}

/*
 * test_and_clear_bit - Clear a bit and return its old value
 * @nr: Bit to clear
 * @addr: Address to count from
 *
 * This operation is atomic and cannot be reordered.
 * It also implies a memory barrier.
 */
static inline int test_and_clear_bit(unsigned long nr,
	volatile unsigned long *addr)
{
	if (cpu_has_llsc && R10000_LLSC_WAR) {
		unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
		unsigned long temp, res;

		__asm__ __volatile__(
311
		"	.set	mips3					\n"
L
Linus Torvalds 已提交
312 313 314
		"1:	" __LL	"%0, %1		# test_and_clear_bit	\n"
		"	or	%2, %0, %3				\n"
		"	xor	%2, %3					\n"
315
		"	" __SC 	"%2, %1					\n"
L
Linus Torvalds 已提交
316 317
		"	beqzl	%2, 1b					\n"
		"	and	%2, %0, %3				\n"
318
		"	.set	mips0					\n"
L
Linus Torvalds 已提交
319 320 321 322 323
		: "=&r" (temp), "=m" (*m), "=&r" (res)
		: "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
		: "memory");

		return res != 0;
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
#ifdef CONFIG_CPU_MIPSR2
	} else if (__builtin_constant_p(nr)) {
		unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
		unsigned long temp, res;

		__asm__ __volatile__(
		"1:	" __LL	"%0, %1		# test_and_clear_bit	\n"
		"	" __EXT "%2, %0, %3, 1				\n"
		"	" __INS	"%0, $0, %3, 1				\n"
		"	" __SC 	"%0, %1					\n"
		"	beqz	%0, 2f					\n"
		"	.subsection 2					\n"
		"2:	b	1b					\n"
		"	.previous					\n"
		: "=&r" (temp), "=m" (*m), "=&r" (res)
		: "ri" (nr & SZLONG_MASK), "m" (*m)
		: "memory");

		return res;
#endif
L
Linus Torvalds 已提交
344 345 346 347 348
	} else if (cpu_has_llsc) {
		unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
		unsigned long temp, res;

		__asm__ __volatile__(
349 350
		"	.set	push					\n"
		"	.set	noreorder				\n"
351
		"	.set	mips3					\n"
352
		"1:	" __LL	"%0, %1		# test_and_clear_bit	\n"
L
Linus Torvalds 已提交
353 354
		"	or	%2, %0, %3				\n"
		"	xor	%2, %3					\n"
355
		"	" __SC 	"%2, %1					\n"
356
		"	beqz	%2, 2f					\n"
L
Linus Torvalds 已提交
357
		"	 and	%2, %0, %3				\n"
358 359 360 361
		"	.subsection 2					\n"
		"2:	b	1b					\n"
		"	 nop						\n"
		"	.previous					\n"
362
		"	.set	pop					\n"
L
Linus Torvalds 已提交
363 364 365 366 367 368 369 370 371
		: "=&r" (temp), "=m" (*m), "=&r" (res)
		: "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
		: "memory");

		return res != 0;
	} else {
		volatile unsigned long *a = addr;
		unsigned long mask;
		int retval;
372
		unsigned long flags;
L
Linus Torvalds 已提交
373 374 375

		a += nr >> SZLONG_LOG;
		mask = 1UL << (nr & SZLONG_MASK);
376
		local_irq_save(flags);
L
Linus Torvalds 已提交
377 378
		retval = (mask & *a) != 0;
		*a &= ~mask;
379
		local_irq_restore(flags);
L
Linus Torvalds 已提交
380 381 382

		return retval;
	}
383 384

	smp_mb();
L
Linus Torvalds 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
}

/*
 * test_and_change_bit - Change a bit and return its old value
 * @nr: Bit to change
 * @addr: Address to count from
 *
 * This operation is atomic and cannot be reordered.
 * It also implies a memory barrier.
 */
static inline int test_and_change_bit(unsigned long nr,
	volatile unsigned long *addr)
{
	if (cpu_has_llsc && R10000_LLSC_WAR) {
		unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
		unsigned long temp, res;

		__asm__ __volatile__(
403
		"	.set	mips3					\n"
404
		"1:	" __LL	"%0, %1		# test_and_change_bit	\n"
L
Linus Torvalds 已提交
405
		"	xor	%2, %0, %3				\n"
406
		"	" __SC	"%2, %1					\n"
L
Linus Torvalds 已提交
407 408
		"	beqzl	%2, 1b					\n"
		"	and	%2, %0, %3				\n"
409
		"	.set	mips0					\n"
L
Linus Torvalds 已提交
410 411 412 413 414 415 416 417 418 419
		: "=&r" (temp), "=m" (*m), "=&r" (res)
		: "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
		: "memory");

		return res != 0;
	} else if (cpu_has_llsc) {
		unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
		unsigned long temp, res;

		__asm__ __volatile__(
420 421
		"	.set	push					\n"
		"	.set	noreorder				\n"
422
		"	.set	mips3					\n"
423
		"1:	" __LL	"%0, %1		# test_and_change_bit	\n"
L
Linus Torvalds 已提交
424
		"	xor	%2, %0, %3				\n"
425
		"	" __SC	"\t%2, %1				\n"
426
		"	beqz	%2, 2f					\n"
L
Linus Torvalds 已提交
427
		"	 and	%2, %0, %3				\n"
428 429 430 431
		"	.subsection 2					\n"
		"2:	b	1b					\n"
		"	 nop						\n"
		"	.previous					\n"
432
		"	.set	pop					\n"
L
Linus Torvalds 已提交
433 434 435 436 437 438 439 440
		: "=&r" (temp), "=m" (*m), "=&r" (res)
		: "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
		: "memory");

		return res != 0;
	} else {
		volatile unsigned long *a = addr;
		unsigned long mask, retval;
441
		unsigned long flags;
L
Linus Torvalds 已提交
442 443 444

		a += nr >> SZLONG_LOG;
		mask = 1UL << (nr & SZLONG_MASK);
445
		local_irq_save(flags);
L
Linus Torvalds 已提交
446 447
		retval = (mask & *a) != 0;
		*a ^= mask;
448
		local_irq_restore(flags);
L
Linus Torvalds 已提交
449 450 451

		return retval;
	}
452 453

	smp_mb();
L
Linus Torvalds 已提交
454 455
}

456
#include <asm-generic/bitops/non-atomic.h>
L
Linus Torvalds 已提交
457 458

/*
459
 * Return the bit position (0..63) of the most significant 1 bit in a word
460 461
 * Returns -1 if no 1 bit exists
 */
462
static inline int __ilog2(unsigned long x)
463 464 465
{
	int lz;

466 467 468 469 470 471 472 473
	if (sizeof(x) == 4) {
		__asm__ (
		"	.set	push					\n"
		"	.set	mips32					\n"
		"	clz	%0, %1					\n"
		"	.set	pop					\n"
		: "=r" (lz)
		: "r" (x));
474

475 476 477 478
		return 31 - lz;
	}

	BUG_ON(sizeof(x) != 8);
479 480 481 482 483 484 485 486 487 488 489 490

	__asm__ (
	"	.set	push						\n"
	"	.set	mips64						\n"
	"	dclz	%0, %1						\n"
	"	.set	pop						\n"
	: "=r" (lz)
	: "r" (x));

	return 63 - lz;
}

491 492
#if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)

493 494
/*
 * __ffs - find first bit in word.
L
Linus Torvalds 已提交
495 496
 * @word: The word to search
 *
497 498
 * Returns 0..SZLONG-1
 * Undefined if no bit exists, so code should check against 0 first.
L
Linus Torvalds 已提交
499
 */
500
static inline unsigned long __ffs(unsigned long word)
L
Linus Torvalds 已提交
501
{
502
	return __ilog2(word & -word);
L
Linus Torvalds 已提交
503 504 505
}

/*
506
 * fls - find last bit set.
L
Linus Torvalds 已提交
507 508
 * @word: The word to search
 *
509 510
 * This is defined the same way as ffs.
 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
L
Linus Torvalds 已提交
511
 */
512
static inline int fls(int word)
L
Linus Torvalds 已提交
513
{
514
	__asm__ ("clz %0, %1" : "=r" (word) : "r" (word));
515

516
	return 32 - word;
L
Linus Torvalds 已提交
517 518
}

519 520
#if defined(CONFIG_64BIT) && defined(CONFIG_CPU_MIPS64)
static inline int fls64(__u64 word)
521
{
522 523 524
	__asm__ ("dclz %0, %1" : "=r" (word) : "r" (word));

	return 64 - word;
525
}
526 527 528
#else
#include <asm-generic/bitops/fls64.h>
#endif
529 530

/*
531
 * ffs - find first bit set.
532 533
 * @word: The word to search
 *
534 535 536
 * This is defined the same way as
 * the libc and compiler builtin ffs routines, therefore
 * differs in spirit from the above ffz (man ffs).
537
 */
538
static inline int ffs(int word)
539
{
540 541
	if (!word)
		return 0;
542

543
	return fls(word & -word);
544 545
}

546
#else
L
Linus Torvalds 已提交
547

548 549 550
#include <asm-generic/bitops/__ffs.h>
#include <asm-generic/bitops/ffs.h>
#include <asm-generic/bitops/fls.h>
551
#include <asm-generic/bitops/fls64.h>
L
Linus Torvalds 已提交
552

553
#endif /*defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64) */
L
Linus Torvalds 已提交
554

555
#include <asm-generic/bitops/ffz.h>
556
#include <asm-generic/bitops/find.h>
L
Linus Torvalds 已提交
557 558 559

#ifdef __KERNEL__

560 561 562 563 564
#include <asm-generic/bitops/sched.h>
#include <asm-generic/bitops/hweight.h>
#include <asm-generic/bitops/ext2-non-atomic.h>
#include <asm-generic/bitops/ext2-atomic.h>
#include <asm-generic/bitops/minix.h>
L
Linus Torvalds 已提交
565 566 567 568

#endif /* __KERNEL__ */

#endif /* _ASM_BITOPS_H */