Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | #ifndef _ASM_BITOPS_H |
| 10 | #define _ASM_BITOPS_H |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/compiler.h> |
| 13 | #include <linux/types.h> |
Ralf Baechle | ec917c2c | 2005-10-07 16:58:15 +0100 | [diff] [blame] | 14 | #include <asm/bug.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <asm/byteorder.h> /* sigh ... */ |
| 16 | #include <asm/cpu-features.h> |
| 17 | |
| 18 | #if (_MIPS_SZLONG == 32) |
| 19 | #define SZLONG_LOG 5 |
| 20 | #define SZLONG_MASK 31UL |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 21 | #define __LL "ll " |
| 22 | #define __SC "sc " |
Ralf Baechle | 42a3b4f | 2005-09-03 15:56:17 -0700 | [diff] [blame] | 23 | #define cpu_to_lelongp(x) cpu_to_le32p((__u32 *) (x)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #elif (_MIPS_SZLONG == 64) |
| 25 | #define SZLONG_LOG 6 |
| 26 | #define SZLONG_MASK 63UL |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 27 | #define __LL "lld " |
| 28 | #define __SC "scd " |
Ralf Baechle | 42a3b4f | 2005-09-03 15:56:17 -0700 | [diff] [blame] | 29 | #define cpu_to_lelongp(x) cpu_to_le64p((__u64 *) (x)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #endif |
| 31 | |
| 32 | #ifdef __KERNEL__ |
| 33 | |
Ralf Baechle | 192ef36 | 2006-07-07 14:07:18 +0100 | [diff] [blame] | 34 | #include <linux/irqflags.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <asm/sgidefs.h> |
| 36 | #include <asm/war.h> |
| 37 | |
| 38 | /* |
| 39 | * clear_bit() doesn't provide any barrier for the compiler. |
| 40 | */ |
| 41 | #define smp_mb__before_clear_bit() smp_mb() |
| 42 | #define smp_mb__after_clear_bit() smp_mb() |
| 43 | |
| 44 | /* |
| 45 | * Only disable interrupt for kernel mode stuff to keep usermode stuff |
| 46 | * that dares to use kernel include files alive. |
| 47 | */ |
| 48 | |
| 49 | #define __bi_flags unsigned long flags |
| 50 | #define __bi_local_irq_save(x) local_irq_save(x) |
| 51 | #define __bi_local_irq_restore(x) local_irq_restore(x) |
| 52 | #else |
| 53 | #define __bi_flags |
| 54 | #define __bi_local_irq_save(x) |
| 55 | #define __bi_local_irq_restore(x) |
| 56 | #endif /* __KERNEL__ */ |
| 57 | |
| 58 | /* |
| 59 | * set_bit - Atomically set a bit in memory |
| 60 | * @nr: the bit to set |
| 61 | * @addr: the address to start counting from |
| 62 | * |
| 63 | * This function is atomic and may not be reordered. See __set_bit() |
| 64 | * if you do not require the atomic guarantees. |
| 65 | * Note that @nr may be almost arbitrarily large; this function is not |
| 66 | * restricted to acting on a single-word quantity. |
| 67 | */ |
| 68 | static inline void set_bit(unsigned long nr, volatile unsigned long *addr) |
| 69 | { |
| 70 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
| 71 | unsigned long temp; |
| 72 | |
| 73 | if (cpu_has_llsc && R10000_LLSC_WAR) { |
| 74 | __asm__ __volatile__( |
Maciej W. Rozycki | c4559f6 | 2005-06-23 15:57:15 +0000 | [diff] [blame] | 75 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | "1: " __LL "%0, %1 # set_bit \n" |
| 77 | " or %0, %2 \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 78 | " " __SC "%0, %1 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | " beqzl %0, 1b \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 80 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | : "=&r" (temp), "=m" (*m) |
| 82 | : "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m)); |
| 83 | } else if (cpu_has_llsc) { |
| 84 | __asm__ __volatile__( |
Maciej W. Rozycki | c4559f6 | 2005-06-23 15:57:15 +0000 | [diff] [blame] | 85 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | "1: " __LL "%0, %1 # set_bit \n" |
| 87 | " or %0, %2 \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 88 | " " __SC "%0, %1 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | " beqz %0, 1b \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 90 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | : "=&r" (temp), "=m" (*m) |
| 92 | : "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m)); |
| 93 | } else { |
| 94 | volatile unsigned long *a = addr; |
| 95 | unsigned long mask; |
| 96 | __bi_flags; |
| 97 | |
| 98 | a += nr >> SZLONG_LOG; |
| 99 | mask = 1UL << (nr & SZLONG_MASK); |
| 100 | __bi_local_irq_save(flags); |
| 101 | *a |= mask; |
| 102 | __bi_local_irq_restore(flags); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | * clear_bit - Clears a bit in memory |
| 108 | * @nr: Bit to clear |
| 109 | * @addr: Address to start counting from |
| 110 | * |
| 111 | * clear_bit() is atomic and may not be reordered. However, it does |
| 112 | * not contain a memory barrier, so if it is used for locking purposes, |
| 113 | * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit() |
| 114 | * in order to ensure changes are visible on other processors. |
| 115 | */ |
| 116 | static inline void clear_bit(unsigned long nr, volatile unsigned long *addr) |
| 117 | { |
| 118 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
| 119 | unsigned long temp; |
| 120 | |
| 121 | if (cpu_has_llsc && R10000_LLSC_WAR) { |
| 122 | __asm__ __volatile__( |
Maciej W. Rozycki | c4559f6 | 2005-06-23 15:57:15 +0000 | [diff] [blame] | 123 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | "1: " __LL "%0, %1 # clear_bit \n" |
| 125 | " and %0, %2 \n" |
| 126 | " " __SC "%0, %1 \n" |
| 127 | " beqzl %0, 1b \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 128 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | : "=&r" (temp), "=m" (*m) |
| 130 | : "ir" (~(1UL << (nr & SZLONG_MASK))), "m" (*m)); |
| 131 | } else if (cpu_has_llsc) { |
| 132 | __asm__ __volatile__( |
Maciej W. Rozycki | c4559f6 | 2005-06-23 15:57:15 +0000 | [diff] [blame] | 133 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | "1: " __LL "%0, %1 # clear_bit \n" |
| 135 | " and %0, %2 \n" |
| 136 | " " __SC "%0, %1 \n" |
| 137 | " beqz %0, 1b \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 138 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | : "=&r" (temp), "=m" (*m) |
| 140 | : "ir" (~(1UL << (nr & SZLONG_MASK))), "m" (*m)); |
| 141 | } else { |
| 142 | volatile unsigned long *a = addr; |
| 143 | unsigned long mask; |
| 144 | __bi_flags; |
| 145 | |
| 146 | a += nr >> SZLONG_LOG; |
| 147 | mask = 1UL << (nr & SZLONG_MASK); |
| 148 | __bi_local_irq_save(flags); |
| 149 | *a &= ~mask; |
| 150 | __bi_local_irq_restore(flags); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | * change_bit - Toggle a bit in memory |
| 156 | * @nr: Bit to change |
| 157 | * @addr: Address to start counting from |
| 158 | * |
| 159 | * change_bit() is atomic and may not be reordered. |
| 160 | * Note that @nr may be almost arbitrarily large; this function is not |
| 161 | * restricted to acting on a single-word quantity. |
| 162 | */ |
| 163 | static inline void change_bit(unsigned long nr, volatile unsigned long *addr) |
| 164 | { |
| 165 | if (cpu_has_llsc && R10000_LLSC_WAR) { |
| 166 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
| 167 | unsigned long temp; |
| 168 | |
| 169 | __asm__ __volatile__( |
Maciej W. Rozycki | c4559f6 | 2005-06-23 15:57:15 +0000 | [diff] [blame] | 170 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | "1: " __LL "%0, %1 # change_bit \n" |
| 172 | " xor %0, %2 \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 173 | " " __SC "%0, %1 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | " beqzl %0, 1b \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 175 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | : "=&r" (temp), "=m" (*m) |
| 177 | : "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m)); |
| 178 | } else if (cpu_has_llsc) { |
| 179 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
| 180 | unsigned long temp; |
| 181 | |
| 182 | __asm__ __volatile__( |
Maciej W. Rozycki | c4559f6 | 2005-06-23 15:57:15 +0000 | [diff] [blame] | 183 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | "1: " __LL "%0, %1 # change_bit \n" |
| 185 | " xor %0, %2 \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 186 | " " __SC "%0, %1 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | " beqz %0, 1b \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 188 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | : "=&r" (temp), "=m" (*m) |
| 190 | : "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m)); |
| 191 | } else { |
| 192 | volatile unsigned long *a = addr; |
| 193 | unsigned long mask; |
| 194 | __bi_flags; |
| 195 | |
| 196 | a += nr >> SZLONG_LOG; |
| 197 | mask = 1UL << (nr & SZLONG_MASK); |
| 198 | __bi_local_irq_save(flags); |
| 199 | *a ^= mask; |
| 200 | __bi_local_irq_restore(flags); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | * test_and_set_bit - Set a bit and return its old value |
| 206 | * @nr: Bit to set |
| 207 | * @addr: Address to count from |
| 208 | * |
| 209 | * This operation is atomic and cannot be reordered. |
| 210 | * It also implies a memory barrier. |
| 211 | */ |
| 212 | static inline int test_and_set_bit(unsigned long nr, |
| 213 | volatile unsigned long *addr) |
| 214 | { |
| 215 | if (cpu_has_llsc && R10000_LLSC_WAR) { |
| 216 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
| 217 | unsigned long temp, res; |
| 218 | |
| 219 | __asm__ __volatile__( |
Maciej W. Rozycki | c4559f6 | 2005-06-23 15:57:15 +0000 | [diff] [blame] | 220 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | "1: " __LL "%0, %1 # test_and_set_bit \n" |
| 222 | " or %2, %0, %3 \n" |
| 223 | " " __SC "%2, %1 \n" |
| 224 | " beqzl %2, 1b \n" |
| 225 | " and %2, %0, %3 \n" |
| 226 | #ifdef CONFIG_SMP |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 227 | " sync \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | #endif |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 229 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | : "=&r" (temp), "=m" (*m), "=&r" (res) |
| 231 | : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m) |
| 232 | : "memory"); |
| 233 | |
| 234 | return res != 0; |
| 235 | } else if (cpu_has_llsc) { |
| 236 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
| 237 | unsigned long temp, res; |
| 238 | |
| 239 | __asm__ __volatile__( |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 240 | " .set push \n" |
| 241 | " .set noreorder \n" |
Maciej W. Rozycki | c4559f6 | 2005-06-23 15:57:15 +0000 | [diff] [blame] | 242 | " .set mips3 \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 243 | "1: " __LL "%0, %1 # test_and_set_bit \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | " or %2, %0, %3 \n" |
| 245 | " " __SC "%2, %1 \n" |
| 246 | " beqz %2, 1b \n" |
| 247 | " and %2, %0, %3 \n" |
| 248 | #ifdef CONFIG_SMP |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 249 | " sync \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | #endif |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 251 | " .set pop \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | : "=&r" (temp), "=m" (*m), "=&r" (res) |
| 253 | : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m) |
| 254 | : "memory"); |
| 255 | |
| 256 | return res != 0; |
| 257 | } else { |
| 258 | volatile unsigned long *a = addr; |
| 259 | unsigned long mask; |
| 260 | int retval; |
| 261 | __bi_flags; |
| 262 | |
| 263 | a += nr >> SZLONG_LOG; |
| 264 | mask = 1UL << (nr & SZLONG_MASK); |
| 265 | __bi_local_irq_save(flags); |
| 266 | retval = (mask & *a) != 0; |
| 267 | *a |= mask; |
| 268 | __bi_local_irq_restore(flags); |
| 269 | |
| 270 | return retval; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | * test_and_clear_bit - Clear a bit and return its old value |
| 276 | * @nr: Bit to clear |
| 277 | * @addr: Address to count from |
| 278 | * |
| 279 | * This operation is atomic and cannot be reordered. |
| 280 | * It also implies a memory barrier. |
| 281 | */ |
| 282 | static inline int test_and_clear_bit(unsigned long nr, |
| 283 | volatile unsigned long *addr) |
| 284 | { |
| 285 | if (cpu_has_llsc && R10000_LLSC_WAR) { |
| 286 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
| 287 | unsigned long temp, res; |
| 288 | |
| 289 | __asm__ __volatile__( |
Maciej W. Rozycki | c4559f6 | 2005-06-23 15:57:15 +0000 | [diff] [blame] | 290 | " .set mips3 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | "1: " __LL "%0, %1 # test_and_clear_bit \n" |
| 292 | " or %2, %0, %3 \n" |
| 293 | " xor %2, %3 \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 294 | " " __SC "%2, %1 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | " beqzl %2, 1b \n" |
| 296 | " and %2, %0, %3 \n" |
| 297 | #ifdef CONFIG_SMP |
| 298 | " sync \n" |
| 299 | #endif |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 300 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | : "=&r" (temp), "=m" (*m), "=&r" (res) |
| 302 | : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m) |
| 303 | : "memory"); |
| 304 | |
| 305 | return res != 0; |
| 306 | } else if (cpu_has_llsc) { |
| 307 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
| 308 | unsigned long temp, res; |
| 309 | |
| 310 | __asm__ __volatile__( |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 311 | " .set push \n" |
| 312 | " .set noreorder \n" |
Maciej W. Rozycki | c4559f6 | 2005-06-23 15:57:15 +0000 | [diff] [blame] | 313 | " .set mips3 \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 314 | "1: " __LL "%0, %1 # test_and_clear_bit \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | " or %2, %0, %3 \n" |
| 316 | " xor %2, %3 \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 317 | " " __SC "%2, %1 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | " beqz %2, 1b \n" |
| 319 | " and %2, %0, %3 \n" |
| 320 | #ifdef CONFIG_SMP |
| 321 | " sync \n" |
| 322 | #endif |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 323 | " .set pop \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | : "=&r" (temp), "=m" (*m), "=&r" (res) |
| 325 | : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m) |
| 326 | : "memory"); |
| 327 | |
| 328 | return res != 0; |
| 329 | } else { |
| 330 | volatile unsigned long *a = addr; |
| 331 | unsigned long mask; |
| 332 | int retval; |
| 333 | __bi_flags; |
| 334 | |
| 335 | a += nr >> SZLONG_LOG; |
| 336 | mask = 1UL << (nr & SZLONG_MASK); |
| 337 | __bi_local_irq_save(flags); |
| 338 | retval = (mask & *a) != 0; |
| 339 | *a &= ~mask; |
| 340 | __bi_local_irq_restore(flags); |
| 341 | |
| 342 | return retval; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | * test_and_change_bit - Change a bit and return its old value |
| 348 | * @nr: Bit to change |
| 349 | * @addr: Address to count from |
| 350 | * |
| 351 | * This operation is atomic and cannot be reordered. |
| 352 | * It also implies a memory barrier. |
| 353 | */ |
| 354 | static inline int test_and_change_bit(unsigned long nr, |
| 355 | volatile unsigned long *addr) |
| 356 | { |
| 357 | if (cpu_has_llsc && R10000_LLSC_WAR) { |
| 358 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
| 359 | unsigned long temp, res; |
| 360 | |
| 361 | __asm__ __volatile__( |
Maciej W. Rozycki | c4559f6 | 2005-06-23 15:57:15 +0000 | [diff] [blame] | 362 | " .set mips3 \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 363 | "1: " __LL "%0, %1 # test_and_change_bit \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | " xor %2, %0, %3 \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 365 | " " __SC "%2, %1 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | " beqzl %2, 1b \n" |
| 367 | " and %2, %0, %3 \n" |
| 368 | #ifdef CONFIG_SMP |
| 369 | " sync \n" |
| 370 | #endif |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 371 | " .set mips0 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | : "=&r" (temp), "=m" (*m), "=&r" (res) |
| 373 | : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m) |
| 374 | : "memory"); |
| 375 | |
| 376 | return res != 0; |
| 377 | } else if (cpu_has_llsc) { |
| 378 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
| 379 | unsigned long temp, res; |
| 380 | |
| 381 | __asm__ __volatile__( |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 382 | " .set push \n" |
| 383 | " .set noreorder \n" |
Maciej W. Rozycki | c4559f6 | 2005-06-23 15:57:15 +0000 | [diff] [blame] | 384 | " .set mips3 \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 385 | "1: " __LL "%0, %1 # test_and_change_bit \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | " xor %2, %0, %3 \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 387 | " " __SC "\t%2, %1 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | " beqz %2, 1b \n" |
| 389 | " and %2, %0, %3 \n" |
| 390 | #ifdef CONFIG_SMP |
| 391 | " sync \n" |
| 392 | #endif |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 393 | " .set pop \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | : "=&r" (temp), "=m" (*m), "=&r" (res) |
| 395 | : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m) |
| 396 | : "memory"); |
| 397 | |
| 398 | return res != 0; |
| 399 | } else { |
| 400 | volatile unsigned long *a = addr; |
| 401 | unsigned long mask, retval; |
| 402 | __bi_flags; |
| 403 | |
| 404 | a += nr >> SZLONG_LOG; |
| 405 | mask = 1UL << (nr & SZLONG_MASK); |
| 406 | __bi_local_irq_save(flags); |
| 407 | retval = (mask & *a) != 0; |
| 408 | *a ^= mask; |
| 409 | __bi_local_irq_restore(flags); |
| 410 | |
| 411 | return retval; |
| 412 | } |
| 413 | } |
| 414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | #undef __bi_flags |
| 416 | #undef __bi_local_irq_save |
| 417 | #undef __bi_local_irq_restore |
| 418 | |
Akinobu Mita | 3c9ee7e | 2006-03-26 01:39:30 -0800 | [diff] [blame] | 419 | #include <asm-generic/bitops/non-atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 421 | /* |
| 422 | * Return the bit position (0..63) of the most significant 1 bit in a word |
| 423 | * Returns -1 if no 1 bit exists |
| 424 | */ |
Ralf Baechle | ec917c2c | 2005-10-07 16:58:15 +0100 | [diff] [blame] | 425 | static inline int __ilog2(unsigned long x) |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 426 | { |
| 427 | int lz; |
| 428 | |
Ralf Baechle | ec917c2c | 2005-10-07 16:58:15 +0100 | [diff] [blame] | 429 | if (sizeof(x) == 4) { |
| 430 | __asm__ ( |
| 431 | " .set push \n" |
| 432 | " .set mips32 \n" |
| 433 | " clz %0, %1 \n" |
| 434 | " .set pop \n" |
| 435 | : "=r" (lz) |
| 436 | : "r" (x)); |
| 437 | |
| 438 | return 31 - lz; |
| 439 | } |
| 440 | |
| 441 | BUG_ON(sizeof(x) != 8); |
| 442 | |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 443 | __asm__ ( |
| 444 | " .set push \n" |
| 445 | " .set mips64 \n" |
| 446 | " dclz %0, %1 \n" |
| 447 | " .set pop \n" |
| 448 | : "=r" (lz) |
| 449 | : "r" (x)); |
| 450 | |
| 451 | return 63 - lz; |
| 452 | } |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 453 | |
Akinobu Mita | 3c9ee7e | 2006-03-26 01:39:30 -0800 | [diff] [blame] | 454 | #if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64) |
| 455 | |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 456 | /* |
| 457 | * __ffs - find first bit in word. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | * @word: The word to search |
| 459 | * |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 460 | * Returns 0..SZLONG-1 |
| 461 | * Undefined if no bit exists, so code should check against 0 first. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | */ |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 463 | static inline unsigned long __ffs(unsigned long word) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | { |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 465 | return __ilog2(word & -word); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /* |
Atsushi Nemoto | bc81824 | 2006-04-17 21:19:12 +0900 | [diff] [blame] | 469 | * fls - find last bit set. |
| 470 | * @word: The word to search |
| 471 | * |
| 472 | * This is defined the same way as ffs. |
| 473 | * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. |
| 474 | */ |
| 475 | static inline int fls(int word) |
| 476 | { |
| 477 | __asm__ ("clz %0, %1" : "=r" (word) : "r" (word)); |
| 478 | |
| 479 | return 32 - word; |
| 480 | } |
| 481 | |
| 482 | #if defined(CONFIG_64BIT) && defined(CONFIG_CPU_MIPS64) |
| 483 | static inline int fls64(__u64 word) |
| 484 | { |
| 485 | __asm__ ("dclz %0, %1" : "=r" (word) : "r" (word)); |
| 486 | |
| 487 | return 64 - word; |
| 488 | } |
| 489 | #else |
| 490 | #include <asm-generic/bitops/fls64.h> |
| 491 | #endif |
| 492 | |
| 493 | /* |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 494 | * ffs - find first bit set. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | * @word: The word to search |
| 496 | * |
Atsushi Nemoto | bc81824 | 2006-04-17 21:19:12 +0900 | [diff] [blame] | 497 | * This is defined the same way as |
| 498 | * the libc and compiler builtin ffs routines, therefore |
| 499 | * differs in spirit from the above ffz (man ffs). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | */ |
Atsushi Nemoto | bc81824 | 2006-04-17 21:19:12 +0900 | [diff] [blame] | 501 | static inline int ffs(int word) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | { |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 503 | if (!word) |
| 504 | return 0; |
| 505 | |
Atsushi Nemoto | bc81824 | 2006-04-17 21:19:12 +0900 | [diff] [blame] | 506 | return fls(word & -word); |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 507 | } |
Ralf Baechle | 2caf190 | 2006-01-30 17:14:41 +0000 | [diff] [blame] | 508 | |
Akinobu Mita | 3c9ee7e | 2006-03-26 01:39:30 -0800 | [diff] [blame] | 509 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
Akinobu Mita | 3c9ee7e | 2006-03-26 01:39:30 -0800 | [diff] [blame] | 511 | #include <asm-generic/bitops/__ffs.h> |
| 512 | #include <asm-generic/bitops/ffs.h> |
Akinobu Mita | 3c9ee7e | 2006-03-26 01:39:30 -0800 | [diff] [blame] | 513 | #include <asm-generic/bitops/fls.h> |
Atsushi Nemoto | bc81824 | 2006-04-17 21:19:12 +0900 | [diff] [blame] | 514 | #include <asm-generic/bitops/fls64.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | |
Akinobu Mita | 3c9ee7e | 2006-03-26 01:39:30 -0800 | [diff] [blame] | 516 | #endif /*defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
Atsushi Nemoto | bc81824 | 2006-04-17 21:19:12 +0900 | [diff] [blame] | 518 | #include <asm-generic/bitops/ffz.h> |
Akinobu Mita | 3c9ee7e | 2006-03-26 01:39:30 -0800 | [diff] [blame] | 519 | #include <asm-generic/bitops/find.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
| 521 | #ifdef __KERNEL__ |
| 522 | |
Akinobu Mita | 3c9ee7e | 2006-03-26 01:39:30 -0800 | [diff] [blame] | 523 | #include <asm-generic/bitops/sched.h> |
| 524 | #include <asm-generic/bitops/hweight.h> |
| 525 | #include <asm-generic/bitops/ext2-non-atomic.h> |
| 526 | #include <asm-generic/bitops/ext2-atomic.h> |
| 527 | #include <asm-generic/bitops/minix.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | |
| 529 | #endif /* __KERNEL__ */ |
| 530 | |
| 531 | #endif /* _ASM_BITOPS_H */ |