| 1 | /* |
|---|
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
|---|
| 3 | * License. See the file "COPYING" in the main directory of this archive |
|---|
| 4 | * for more details. |
|---|
| 5 | * |
|---|
| 6 | * Copyright (c) 1994 - 1997, 1999, 2000 Ralf Baechle (ralf@gnu.org) |
|---|
| 7 | * Copyright (c) 1999, 2000 Silicon Graphics, Inc. |
|---|
| 8 | */ |
|---|
| 9 | |
|---|
| 10 | /* non-atomic version which could be used in userspace (but shouldn't anyway) */ |
|---|
| 11 | |
|---|
| 12 | #ifndef _ASM_BITOPS_H |
|---|
| 13 | #define _ASM_BITOPS_H |
|---|
| 14 | |
|---|
| 15 | #include <linux/types.h> |
|---|
| 16 | #include <asm/byteorder.h> /* sigh ... */ |
|---|
| 17 | |
|---|
| 18 | #if (_MIPS_SZLONG == 32) |
|---|
| 19 | #define SZLONG_LOG 5 |
|---|
| 20 | #define SZLONG_MASK 31UL |
|---|
| 21 | #define __LL "ll" |
|---|
| 22 | #define __SC "sc" |
|---|
| 23 | #define cpu_to_lelongp(x) cpu_to_le32p((__u32 *) (x)) |
|---|
| 24 | #elif (_MIPS_SZLONG == 64) |
|---|
| 25 | #define SZLONG_LOG 6 |
|---|
| 26 | #define SZLONG_MASK 63UL |
|---|
| 27 | #define __LL "lld" |
|---|
| 28 | #define __SC "scd" |
|---|
| 29 | #define cpu_to_lelongp(x) cpu_to_le64p((__u64 *) (x)) |
|---|
| 30 | #endif |
|---|
| 31 | |
|---|
| 32 | /* |
|---|
| 33 | * set_bit - Atomically set a bit in memory |
|---|
| 34 | * @nr: the bit to set |
|---|
| 35 | * @addr: the address to start counting from |
|---|
| 36 | * |
|---|
| 37 | * This function is atomic and may not be reordered. See __set_bit() |
|---|
| 38 | * if you do not require the atomic guarantees. |
|---|
| 39 | * Note that @nr may be almost arbitrarily large; this function is not |
|---|
| 40 | * restricted to acting on a single-word quantity. |
|---|
| 41 | */ |
|---|
| 42 | static inline void set_bit(unsigned long nr, volatile unsigned long * addr) |
|---|
| 43 | { |
|---|
| 44 | volatile unsigned long *a = addr; |
|---|
| 45 | unsigned long mask; |
|---|
| 46 | |
|---|
| 47 | a += nr >> SZLONG_LOG; |
|---|
| 48 | mask = 1 << (nr & SZLONG_MASK); |
|---|
| 49 | *a |= mask; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | /* |
|---|
| 53 | * __set_bit - Set a bit in memory |
|---|
| 54 | * @nr: the bit to set |
|---|
| 55 | * @addr: the address to start counting from |
|---|
| 56 | * |
|---|
| 57 | * Unlike set_bit(), this function is non-atomic and may be reordered. |
|---|
| 58 | * If it's called on the same region of memory simultaneously, the effect |
|---|
| 59 | * may be that only one operation succeeds. |
|---|
| 60 | */ |
|---|
| 61 | static inline void __set_bit(unsigned long nr, volatile unsigned long * addr) |
|---|
| 62 | { |
|---|
| 63 | volatile unsigned long *a = addr; |
|---|
| 64 | unsigned long mask; |
|---|
| 65 | |
|---|
| 66 | a += nr >> SZLONG_LOG; |
|---|
| 67 | mask = 1 << (nr & SZLONG_MASK); |
|---|
| 68 | *a |= mask; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | /* |
|---|
| 72 | * clear_bit - Clears a bit in memory |
|---|
| 73 | * @nr: Bit to clear |
|---|
| 74 | * @addr: Address to start counting from |
|---|
| 75 | * |
|---|
| 76 | * clear_bit() is atomic and may not be reordered. However, it does |
|---|
| 77 | * not contain a memory barrier, so if it is used for locking purposes, |
|---|
| 78 | * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit() |
|---|
| 79 | * in order to ensure changes are visible on other processors. |
|---|
| 80 | */ |
|---|
| 81 | static inline void clear_bit(unsigned long nr, volatile unsigned long * addr) |
|---|
| 82 | { |
|---|
| 83 | volatile unsigned long *a = addr; |
|---|
| 84 | unsigned long mask; |
|---|
| 85 | |
|---|
| 86 | a += nr >> SZLONG_LOG; |
|---|
| 87 | mask = 1 << (nr & SZLONG_MASK); |
|---|
| 88 | *a &= ~mask; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | static inline void __clear_bit(unsigned long nr, volatile unsigned long * addr) |
|---|
| 92 | { |
|---|
| 93 | volatile unsigned long *a = addr; |
|---|
| 94 | unsigned long mask; |
|---|
| 95 | |
|---|
| 96 | a += nr >> SZLONG_LOG; |
|---|
| 97 | mask = 1 << (nr & SZLONG_MASK); |
|---|
| 98 | *a &= ~mask; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | /* |
|---|
| 102 | * change_bit - Toggle a bit in memory |
|---|
| 103 | * @nr: Bit to change |
|---|
| 104 | * @addr: Address to start counting from |
|---|
| 105 | * |
|---|
| 106 | * change_bit() is atomic and may not be reordered. |
|---|
| 107 | * Note that @nr may be almost arbitrarily large; this function is not |
|---|
| 108 | * restricted to acting on a single-word quantity. |
|---|
| 109 | */ |
|---|
| 110 | static inline void change_bit(unsigned long nr, volatile unsigned long * addr) |
|---|
| 111 | { |
|---|
| 112 | volatile unsigned long *a = addr; |
|---|
| 113 | unsigned long mask; |
|---|
| 114 | |
|---|
| 115 | a += nr >> SZLONG_LOG; |
|---|
| 116 | mask = 1 << (nr & SZLONG_MASK); |
|---|
| 117 | *a ^= mask; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | /* |
|---|
| 121 | * __change_bit - Toggle a bit in memory |
|---|
| 122 | * @nr: the bit to change |
|---|
| 123 | * @addr: the address to start counting from |
|---|
| 124 | * |
|---|
| 125 | * Unlike change_bit(), this function is non-atomic and may be reordered. |
|---|
| 126 | * If it's called on the same region of memory simultaneously, the effect |
|---|
| 127 | * may be that only one operation succeeds. |
|---|
| 128 | */ |
|---|
| 129 | static inline void __change_bit(unsigned long nr, volatile unsigned long * addr) |
|---|
| 130 | { |
|---|
| 131 | unsigned long * m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
|---|
| 132 | |
|---|
| 133 | *m ^= 1UL << (nr & SZLONG_MASK); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | /* |
|---|
| 137 | * test_and_set_bit - Set a bit and return its old value |
|---|
| 138 | * @nr: Bit to set |
|---|
| 139 | * @addr: Address to count from |
|---|
| 140 | * |
|---|
| 141 | * This operation is atomic and cannot be reordered. |
|---|
| 142 | * It also implies a memory barrier. |
|---|
| 143 | */ |
|---|
| 144 | static inline int test_and_set_bit(unsigned long nr, |
|---|
| 145 | volatile unsigned long * addr) |
|---|
| 146 | { |
|---|
| 147 | volatile unsigned long *a = addr; |
|---|
| 148 | unsigned long mask; |
|---|
| 149 | int retval; |
|---|
| 150 | |
|---|
| 151 | a += nr >> SZLONG_LOG; |
|---|
| 152 | mask = 1 << (nr & SZLONG_MASK); |
|---|
| 153 | retval = (mask & *a) != 0; |
|---|
| 154 | *a |= mask; |
|---|
| 155 | |
|---|
| 156 | return retval; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | /* |
|---|
| 160 | * __test_and_set_bit - Set a bit and return its old value |
|---|
| 161 | * @nr: Bit to set |
|---|
| 162 | * @addr: Address to count from |
|---|
| 163 | * |
|---|
| 164 | * This operation is non-atomic and can be reordered. |
|---|
| 165 | * If two examples of this operation race, one can appear to succeed |
|---|
| 166 | * but actually fail. You must protect multiple accesses with a lock. |
|---|
| 167 | */ |
|---|
| 168 | static inline int __test_and_set_bit(unsigned long nr, |
|---|
| 169 | volatile unsigned long *addr) |
|---|
| 170 | { |
|---|
| 171 | volatile unsigned long *a = addr; |
|---|
| 172 | unsigned long mask; |
|---|
| 173 | int retval; |
|---|
| 174 | |
|---|
| 175 | a += nr >> SZLONG_LOG; |
|---|
| 176 | mask = 1 << (nr & SZLONG_MASK); |
|---|
| 177 | retval = (mask & *a) != 0; |
|---|
| 178 | *a |= mask; |
|---|
| 179 | |
|---|
| 180 | return retval; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | /* |
|---|
| 184 | * test_and_clear_bit - Clear a bit and return its old value |
|---|
| 185 | * @nr: Bit to clear |
|---|
| 186 | * @addr: Address to count from |
|---|
| 187 | * |
|---|
| 188 | * This operation is atomic and cannot be reordered. |
|---|
| 189 | * It also implies a memory barrier. |
|---|
| 190 | */ |
|---|
| 191 | static inline int test_and_clear_bit(unsigned long nr, |
|---|
| 192 | volatile unsigned long * addr) |
|---|
| 193 | { |
|---|
| 194 | volatile unsigned long *a = addr; |
|---|
| 195 | unsigned long mask; |
|---|
| 196 | int retval; |
|---|
| 197 | |
|---|
| 198 | a += nr >> SZLONG_LOG; |
|---|
| 199 | mask = 1 << (nr & SZLONG_MASK); |
|---|
| 200 | retval = (mask & *a) != 0; |
|---|
| 201 | *a &= ~mask; |
|---|
| 202 | |
|---|
| 203 | return retval; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | /* |
|---|
| 207 | * __test_and_clear_bit - Clear a bit and return its old value |
|---|
| 208 | * @nr: Bit to clear |
|---|
| 209 | * @addr: Address to count from |
|---|
| 210 | * |
|---|
| 211 | * This operation is non-atomic and can be reordered. |
|---|
| 212 | * If two examples of this operation race, one can appear to succeed |
|---|
| 213 | * but actually fail. You must protect multiple accesses with a lock. |
|---|
| 214 | */ |
|---|
| 215 | static inline int __test_and_clear_bit(unsigned long nr, |
|---|
| 216 | volatile unsigned long * addr) |
|---|
| 217 | { |
|---|
| 218 | volatile unsigned long *a = addr; |
|---|
| 219 | unsigned long mask; |
|---|
| 220 | int retval; |
|---|
| 221 | |
|---|
| 222 | a += (nr >> SZLONG_LOG); |
|---|
| 223 | mask = 1UL << (nr & SZLONG_MASK); |
|---|
| 224 | retval = ((mask & *a) != 0); |
|---|
| 225 | *a &= ~mask; |
|---|
| 226 | |
|---|
| 227 | return retval; |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | /* |
|---|
| 231 | * test_and_change_bit - Change a bit and return its old value |
|---|
| 232 | * @nr: Bit to change |
|---|
| 233 | * @addr: Address to count from |
|---|
| 234 | * |
|---|
| 235 | * This operation is atomic and cannot be reordered. |
|---|
| 236 | * It also implies a memory barrier. |
|---|
| 237 | */ |
|---|
| 238 | static inline int test_and_change_bit(unsigned long nr, |
|---|
| 239 | volatile unsigned long * addr) |
|---|
| 240 | { |
|---|
| 241 | volatile unsigned long *a = addr; |
|---|
| 242 | unsigned long mask, retval; |
|---|
| 243 | |
|---|
| 244 | a += nr >> SZLONG_LOG; |
|---|
| 245 | mask = 1 << (nr & SZLONG_MASK); |
|---|
| 246 | retval = (mask & *a) != 0; |
|---|
| 247 | *a ^= mask; |
|---|
| 248 | |
|---|
| 249 | return retval; |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | /* |
|---|
| 253 | * __test_and_change_bit - Change a bit and return its old value |
|---|
| 254 | * @nr: Bit to change |
|---|
| 255 | * @addr: Address to count from |
|---|
| 256 | * |
|---|
| 257 | * This operation is non-atomic and can be reordered. |
|---|
| 258 | * If two examples of this operation race, one can appear to succeed |
|---|
| 259 | * but actually fail. You must protect multiple accesses with a lock. |
|---|
| 260 | */ |
|---|
| 261 | static inline int __test_and_change_bit(unsigned long nr, |
|---|
| 262 | volatile unsigned long * addr) |
|---|
| 263 | { |
|---|
| 264 | volatile unsigned long *a = addr; |
|---|
| 265 | unsigned long mask; |
|---|
| 266 | int retval; |
|---|
| 267 | |
|---|
| 268 | a += (nr >> SZLONG_LOG); |
|---|
| 269 | mask = 1 << (nr & SZLONG_MASK); |
|---|
| 270 | retval = (mask & *a) != 0; |
|---|
| 271 | *a ^= mask; |
|---|
| 272 | |
|---|
| 273 | return retval; |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | /* |
|---|
| 277 | * test_bit - Determine whether a bit is set |
|---|
| 278 | * @nr: bit number to test |
|---|
| 279 | * @addr: Address to start counting from |
|---|
| 280 | */ |
|---|
| 281 | static inline int test_bit(unsigned long nr, const volatile unsigned long *addr) |
|---|
| 282 | { |
|---|
| 283 | return 1UL & (addr[nr >> SZLONG_LOG] >> (nr & SZLONG_MASK)); |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | /* |
|---|
| 287 | * ffz - find first zero in word. |
|---|
| 288 | * @word: The word to search |
|---|
| 289 | * |
|---|
| 290 | * Undefined if no zero exists, so code should check against ~0UL first. |
|---|
| 291 | */ |
|---|
| 292 | static inline unsigned long ffz(unsigned long word) |
|---|
| 293 | { |
|---|
| 294 | int b = 0, s; |
|---|
| 295 | |
|---|
| 296 | word = ~word; |
|---|
| 297 | #if (_MIPS_SZLONG == 32) |
|---|
| 298 | s = 16; if (word << 16 != 0) s = 0; b += s; word >>= s; |
|---|
| 299 | s = 8; if (word << 24 != 0) s = 0; b += s; word >>= s; |
|---|
| 300 | s = 4; if (word << 28 != 0) s = 0; b += s; word >>= s; |
|---|
| 301 | s = 2; if (word << 30 != 0) s = 0; b += s; word >>= s; |
|---|
| 302 | s = 1; if (word << 31 != 0) s = 0; b += s; |
|---|
| 303 | #endif |
|---|
| 304 | #if (_MIPS_SZLONG == 64) |
|---|
| 305 | s = 32; if (word << 32 != 0) s = 0; b += s; word >>= s; |
|---|
| 306 | s = 16; if (word << 48 != 0) s = 0; b += s; word >>= s; |
|---|
| 307 | s = 8; if (word << 56 != 0) s = 0; b += s; word >>= s; |
|---|
| 308 | s = 4; if (word << 60 != 0) s = 0; b += s; word >>= s; |
|---|
| 309 | s = 2; if (word << 62 != 0) s = 0; b += s; word >>= s; |
|---|
| 310 | s = 1; if (word << 63 != 0) s = 0; b += s; |
|---|
| 311 | #endif |
|---|
| 312 | |
|---|
| 313 | return b; |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | /* |
|---|
| 317 | * __ffs - find first bit in word. |
|---|
| 318 | * @word: The word to search |
|---|
| 319 | * |
|---|
| 320 | * Undefined if no bit exists, so code should check against 0 first. |
|---|
| 321 | */ |
|---|
| 322 | static inline unsigned long __ffs(unsigned long word) |
|---|
| 323 | { |
|---|
| 324 | return ffz(~word); |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | /* |
|---|
| 328 | * fls: find last bit set. |
|---|
| 329 | */ |
|---|
| 330 | |
|---|
| 331 | #define fls(x) generic_fls(x) |
|---|
| 332 | |
|---|
| 333 | /* |
|---|
| 334 | * find_next_zero_bit - find the first zero bit in a memory region |
|---|
| 335 | * @addr: The address to base the search on |
|---|
| 336 | * @offset: The bitnumber to start searching at |
|---|
| 337 | * @size: The maximum size to search |
|---|
| 338 | */ |
|---|
| 339 | static inline unsigned long find_next_zero_bit(const unsigned long *addr, |
|---|
| 340 | unsigned long size, unsigned long offset) |
|---|
| 341 | { |
|---|
| 342 | const unsigned long *p = addr + (offset >> SZLONG_LOG); |
|---|
| 343 | unsigned long result = offset & ~SZLONG_MASK; |
|---|
| 344 | unsigned long tmp; |
|---|
| 345 | |
|---|
| 346 | if (offset >= size) |
|---|
| 347 | return size; |
|---|
| 348 | size -= result; |
|---|
| 349 | offset &= SZLONG_MASK; |
|---|
| 350 | if (offset) { |
|---|
| 351 | tmp = *(p++); |
|---|
| 352 | tmp |= ~0UL >> (_MIPS_SZLONG-offset); |
|---|
| 353 | if (size < _MIPS_SZLONG) |
|---|
| 354 | goto found_first; |
|---|
| 355 | if (~tmp) |
|---|
| 356 | goto found_middle; |
|---|
| 357 | size -= _MIPS_SZLONG; |
|---|
| 358 | result += _MIPS_SZLONG; |
|---|
| 359 | } |
|---|
| 360 | while (size & ~SZLONG_MASK) { |
|---|
| 361 | if (~(tmp = *(p++))) |
|---|
| 362 | goto found_middle; |
|---|
| 363 | result += _MIPS_SZLONG; |
|---|
| 364 | size -= _MIPS_SZLONG; |
|---|
| 365 | } |
|---|
| 366 | if (!size) |
|---|
| 367 | return result; |
|---|
| 368 | tmp = *p; |
|---|
| 369 | |
|---|
| 370 | found_first: |
|---|
| 371 | tmp |= ~0UL << size; |
|---|
| 372 | if (tmp == ~0UL) /* Are any bits zero? */ |
|---|
| 373 | return result + size; /* Nope. */ |
|---|
| 374 | found_middle: |
|---|
| 375 | return result + ffz(tmp); |
|---|
| 376 | } |
|---|
| 377 | |
|---|
| 378 | #define find_first_zero_bit(addr, size) \ |
|---|
| 379 | find_next_zero_bit((addr), (size), 0) |
|---|
| 380 | |
|---|
| 381 | /* |
|---|
| 382 | * find_next_bit - find the next set bit in a memory region |
|---|
| 383 | * @addr: The address to base the search on |
|---|
| 384 | * @offset: The bitnumber to start searching at |
|---|
| 385 | * @size: The maximum size to search |
|---|
| 386 | */ |
|---|
| 387 | static inline unsigned long find_next_bit(const unsigned long *addr, |
|---|
| 388 | unsigned long size, unsigned long offset) |
|---|
| 389 | { |
|---|
| 390 | const unsigned long *p = addr + (offset >> SZLONG_LOG); |
|---|
| 391 | unsigned long result = offset & ~SZLONG_MASK; |
|---|
| 392 | unsigned long tmp; |
|---|
| 393 | |
|---|
| 394 | if (offset >= size) |
|---|
| 395 | return size; |
|---|
| 396 | size -= result; |
|---|
| 397 | offset &= SZLONG_MASK; |
|---|
| 398 | if (offset) { |
|---|
| 399 | tmp = *(p++); |
|---|
| 400 | tmp &= ~0UL << offset; |
|---|
| 401 | if (size < _MIPS_SZLONG) |
|---|
| 402 | goto found_first; |
|---|
| 403 | if (tmp) |
|---|
| 404 | goto found_middle; |
|---|
| 405 | size -= _MIPS_SZLONG; |
|---|
| 406 | result += _MIPS_SZLONG; |
|---|
| 407 | } |
|---|
| 408 | while (size & ~SZLONG_MASK) { |
|---|
| 409 | if ((tmp = *(p++))) |
|---|
| 410 | goto found_middle; |
|---|
| 411 | result += _MIPS_SZLONG; |
|---|
| 412 | size -= _MIPS_SZLONG; |
|---|
| 413 | } |
|---|
| 414 | if (!size) |
|---|
| 415 | return result; |
|---|
| 416 | tmp = *p; |
|---|
| 417 | |
|---|
| 418 | found_first: |
|---|
| 419 | tmp &= ~0UL >> (_MIPS_SZLONG - size); |
|---|
| 420 | if (tmp == 0UL) /* Are any bits set? */ |
|---|
| 421 | return result + size; /* Nope. */ |
|---|
| 422 | found_middle: |
|---|
| 423 | return result + __ffs(tmp); |
|---|
| 424 | } |
|---|
| 425 | |
|---|
| 426 | /* |
|---|
| 427 | * find_first_bit - find the first set bit in a memory region |
|---|
| 428 | * @addr: The address to start the search at |
|---|
| 429 | * @size: The maximum size to search |
|---|
| 430 | * |
|---|
| 431 | * Returns the bit-number of the first set bit, not the number of the byte |
|---|
| 432 | * containing a bit. |
|---|
| 433 | */ |
|---|
| 434 | #define find_first_bit(addr, size) \ |
|---|
| 435 | find_next_bit((addr), (size), 0) |
|---|
| 436 | |
|---|
| 437 | #endif /* _ASM_BITOPS_H */ |
|---|