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