bitops.h 8.6 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
/*
 * Copyright 1995, Russell King.
 * Various bits and pieces copyrights include:
 *  Linus Torvalds (test_bit).
 * Big endian support: Copyright 2001, Nicolas Pitre
 *  reworked by rmk.
 *
 * bit 0 is the LSB of an "unsigned long" quantity.
 *
 * Please note that the code in this file should never be included
 * from user space.  Many of these are not implemented in assembler
 * since they would be too costly.  Also, they require privileged
 * instructions (which are not available from user mode) to ensure
 * that they are atomic.
 */

#ifndef __ASM_ARM_BITOPS_H
#define __ASM_ARM_BITOPS_H

#ifdef __KERNEL__

J
Jiri Slaby 已提交
22 23 24 25
#ifndef _LINUX_BITOPS_H
#error only <linux/bitops.h> can be included directly
#endif

26
#include <linux/compiler.h>
27
#include <linux/irqflags.h>
L
Linus Torvalds 已提交
28

29 30
#define smp_mb__before_clear_bit()	smp_mb()
#define smp_mb__after_clear_bit()	smp_mb()
L
Linus Torvalds 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43

/*
 * These functions are the basis of our bit ops.
 *
 * First, the atomic bitops. These use native endian.
 */
static inline void ____atomic_set_bit(unsigned int bit, volatile unsigned long *p)
{
	unsigned long flags;
	unsigned long mask = 1UL << (bit & 31);

	p += bit >> 5;

44
	raw_local_irq_save(flags);
L
Linus Torvalds 已提交
45
	*p |= mask;
46
	raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
47 48 49 50 51 52 53 54 55
}

static inline void ____atomic_clear_bit(unsigned int bit, volatile unsigned long *p)
{
	unsigned long flags;
	unsigned long mask = 1UL << (bit & 31);

	p += bit >> 5;

56
	raw_local_irq_save(flags);
L
Linus Torvalds 已提交
57
	*p &= ~mask;
58
	raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
59 60 61 62 63 64 65 66 67
}

static inline void ____atomic_change_bit(unsigned int bit, volatile unsigned long *p)
{
	unsigned long flags;
	unsigned long mask = 1UL << (bit & 31);

	p += bit >> 5;

68
	raw_local_irq_save(flags);
L
Linus Torvalds 已提交
69
	*p ^= mask;
70
	raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
71 72 73 74 75 76 77 78 79 80 81
}

static inline int
____atomic_test_and_set_bit(unsigned int bit, volatile unsigned long *p)
{
	unsigned long flags;
	unsigned int res;
	unsigned long mask = 1UL << (bit & 31);

	p += bit >> 5;

82
	raw_local_irq_save(flags);
L
Linus Torvalds 已提交
83 84
	res = *p;
	*p = res | mask;
85
	raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
86

J
Johannes Weiner 已提交
87
	return (res & mask) != 0;
L
Linus Torvalds 已提交
88 89 90 91 92 93 94 95 96 97 98
}

static inline int
____atomic_test_and_clear_bit(unsigned int bit, volatile unsigned long *p)
{
	unsigned long flags;
	unsigned int res;
	unsigned long mask = 1UL << (bit & 31);

	p += bit >> 5;

99
	raw_local_irq_save(flags);
L
Linus Torvalds 已提交
100 101
	res = *p;
	*p = res & ~mask;
102
	raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
103

J
Johannes Weiner 已提交
104
	return (res & mask) != 0;
L
Linus Torvalds 已提交
105 106 107 108 109 110 111 112 113 114 115
}

static inline int
____atomic_test_and_change_bit(unsigned int bit, volatile unsigned long *p)
{
	unsigned long flags;
	unsigned int res;
	unsigned long mask = 1UL << (bit & 31);

	p += bit >> 5;

116
	raw_local_irq_save(flags);
L
Linus Torvalds 已提交
117 118
	res = *p;
	*p = res ^ mask;
119
	raw_local_irq_restore(flags);
L
Linus Torvalds 已提交
120

J
Johannes Weiner 已提交
121
	return (res & mask) != 0;
L
Linus Torvalds 已提交
122 123
}

124
#include <asm-generic/bitops/non-atomic.h>
L
Linus Torvalds 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

/*
 *  A note about Endian-ness.
 *  -------------------------
 *
 * When the ARM is put into big endian mode via CR15, the processor
 * merely swaps the order of bytes within words, thus:
 *
 *          ------------ physical data bus bits -----------
 *          D31 ... D24  D23 ... D16  D15 ... D8  D7 ... D0
 * little     byte 3       byte 2       byte 1      byte 0
 * big        byte 0       byte 1       byte 2      byte 3
 *
 * This means that reading a 32-bit word at address 0 returns the same
 * value irrespective of the endian mode bit.
 *
 * Peripheral devices should be connected with the data bus reversed in
 * "Big Endian" mode.  ARM Application Note 61 is applicable, and is
 * available from http://www.arm.com/.
 *
 * The following assumes that the data bus connectivity for big endian
 * mode has been followed.
 *
 * Note that bit 0 is defined to be 32-bit word bit 0, not byte 0 bit 0.
 */

151 152 153 154 155 156 157 158 159 160
/*
 * Native endian assembly bitops.  nr = 0 -> word 0 bit 0.
 */
extern void _set_bit(int nr, volatile unsigned long * p);
extern void _clear_bit(int nr, volatile unsigned long * p);
extern void _change_bit(int nr, volatile unsigned long * p);
extern int _test_and_set_bit(int nr, volatile unsigned long * p);
extern int _test_and_clear_bit(int nr, volatile unsigned long * p);
extern int _test_and_change_bit(int nr, volatile unsigned long * p);

L
Linus Torvalds 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
/*
 * Little endian assembly bitops.  nr = 0 -> byte 0 bit 0.
 */
extern int _find_first_zero_bit_le(const void * p, unsigned size);
extern int _find_next_zero_bit_le(const void * p, int size, int offset);
extern int _find_first_bit_le(const unsigned long *p, unsigned size);
extern int _find_next_bit_le(const unsigned long *p, int size, int offset);

/*
 * Big endian assembly bitops.  nr = 0 -> byte 3 bit 0.
 */
extern int _find_first_zero_bit_be(const void * p, unsigned size);
extern int _find_next_zero_bit_be(const void * p, int size, int offset);
extern int _find_first_bit_be(const unsigned long *p, unsigned size);
extern int _find_next_bit_be(const unsigned long *p, int size, int offset);

177
#ifndef CONFIG_SMP
L
Linus Torvalds 已提交
178 179 180
/*
 * The __* form of bitops are non-atomic and may be reordered.
 */
181 182
#define ATOMIC_BITOP(name,nr,p)			\
	(__builtin_constant_p(nr) ? ____atomic_##name(nr, p) : _##name(nr,p))
183
#else
184
#define ATOMIC_BITOP(name,nr,p)		_##name(nr,p)
185
#endif
L
Linus Torvalds 已提交
186

187 188 189 190 191 192 193 194 195
/*
 * Native endian atomic definitions.
 */
#define set_bit(nr,p)			ATOMIC_BITOP(set_bit,nr,p)
#define clear_bit(nr,p)			ATOMIC_BITOP(clear_bit,nr,p)
#define change_bit(nr,p)		ATOMIC_BITOP(change_bit,nr,p)
#define test_and_set_bit(nr,p)		ATOMIC_BITOP(test_and_set_bit,nr,p)
#define test_and_clear_bit(nr,p)	ATOMIC_BITOP(test_and_clear_bit,nr,p)
#define test_and_change_bit(nr,p)	ATOMIC_BITOP(test_and_change_bit,nr,p)
L
Linus Torvalds 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

#ifndef __ARMEB__
/*
 * These are the little endian, atomic definitions.
 */
#define find_first_zero_bit(p,sz)	_find_first_zero_bit_le(p,sz)
#define find_next_zero_bit(p,sz,off)	_find_next_zero_bit_le(p,sz,off)
#define find_first_bit(p,sz)		_find_first_bit_le(p,sz)
#define find_next_bit(p,sz,off)		_find_next_bit_le(p,sz,off)

#else
/*
 * These are the big endian, atomic definitions.
 */
#define find_first_zero_bit(p,sz)	_find_first_zero_bit_be(p,sz)
#define find_next_zero_bit(p,sz,off)	_find_next_zero_bit_be(p,sz,off)
#define find_first_bit(p,sz)		_find_first_bit_be(p,sz)
#define find_next_bit(p,sz,off)		_find_next_bit_be(p,sz,off)

#endif

#if __LINUX_ARM_ARCH__ < 5

219
#include <asm-generic/bitops/ffz.h>
N
Nicolas Pitre 已提交
220
#include <asm-generic/bitops/__fls.h>
221 222 223
#include <asm-generic/bitops/__ffs.h>
#include <asm-generic/bitops/fls.h>
#include <asm-generic/bitops/ffs.h>
L
Linus Torvalds 已提交
224 225 226

#else

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
static inline int constant_fls(int x)
{
	int r = 32;

	if (!x)
		return 0;
	if (!(x & 0xffff0000u)) {
		x <<= 16;
		r -= 16;
	}
	if (!(x & 0xff000000u)) {
		x <<= 8;
		r -= 8;
	}
	if (!(x & 0xf0000000u)) {
		x <<= 4;
		r -= 4;
	}
	if (!(x & 0xc0000000u)) {
		x <<= 2;
		r -= 2;
	}
	if (!(x & 0x80000000u)) {
		x <<= 1;
		r -= 1;
	}
	return r;
}

L
Linus Torvalds 已提交
256
/*
257 258 259 260
 * On ARMv5 and above those functions can be implemented around the
 * clz instruction for much better code efficiency.  __clz returns
 * the number of leading zeros, zero input will return 32, and
 * 0x80000000 will return 0.
L
Linus Torvalds 已提交
261
 */
262 263 264 265 266
static inline unsigned int __clz(unsigned int x)
{
	unsigned int ret;

	asm("clz\t%0, %1" : "=r" (ret) : "r" (x));
L
Linus Torvalds 已提交
267

268 269 270 271 272 273 274
	return ret;
}

/*
 * fls() returns zero if the input is zero, otherwise returns the bit
 * position of the last set bit, where the LSB is 1 and MSB is 32.
 */
275 276
static inline int fls(int x)
{
N
Nicolas Pitre 已提交
277 278 279
	if (__builtin_constant_p(x))
	       return constant_fls(x);

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
	return 32 - __clz(x);
}

/*
 * __fls() returns the bit position of the last bit set, where the
 * LSB is 0 and MSB is 31.  Zero input is undefined.
 */
static inline unsigned long __fls(unsigned long x)
{
	return fls(x) - 1;
}

/*
 * ffs() returns zero if the input was zero, otherwise returns the bit
 * position of the first set bit, where the LSB is 1 and MSB is 32.
 */
static inline int ffs(int x)
{
	return fls(x & -x);
}

/*
 * __ffs() returns the bit position of the first bit set, where the
 * LSB is 0 and MSB is 31.  Zero input is undefined.
 */
static inline unsigned long __ffs(unsigned long x)
{
	return ffs(x) - 1;
308 309
}

L
Linus Torvalds 已提交
310 311 312 313
#define ffz(x) __ffs( ~(x) )

#endif

314
#include <asm-generic/bitops/fls64.h>
L
Linus Torvalds 已提交
315

316 317
#include <asm-generic/bitops/sched.h>
#include <asm-generic/bitops/hweight.h>
N
Nick Piggin 已提交
318
#include <asm-generic/bitops/lock.h>
L
Linus Torvalds 已提交
319

A
Akinobu Mita 已提交
320
#ifdef __ARMEB__
321 322 323 324 325

static inline int find_first_zero_bit_le(const void *p, unsigned size)
{
	return _find_first_zero_bit_le(p, size);
}
326
#define find_first_zero_bit_le find_first_zero_bit_le
327 328 329 330 331

static inline int find_next_zero_bit_le(const void *p, int size, int offset)
{
	return _find_next_zero_bit_le(p, size, offset);
}
332
#define find_next_zero_bit_le find_next_zero_bit_le
333 334 335 336 337

static inline int find_next_bit_le(const void *p, int size, int offset)
{
	return _find_next_bit_le(p, size, offset);
}
338
#define find_next_bit_le find_next_bit_le
339

A
Akinobu Mita 已提交
340 341 342 343
#endif

#include <asm-generic/bitops/le.h>

L
Linus Torvalds 已提交
344 345 346
/*
 * Ext2 is defined to use little-endian byte ordering.
 */
347
#include <asm-generic/bitops/ext2-atomic-setbit.h>
L
Linus Torvalds 已提交
348 349 350 351

#endif /* __KERNEL__ */

#endif /* _ARM_BITOPS_H */