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 | |
Jiri Slaby | 0624517 | 2007-10-18 23:40:26 -0700 | [diff] [blame] | 12 | #ifndef _LINUX_BITOPS_H |
| 13 | #error only <linux/bitops.h> can be included directly |
| 14 | #endif |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/compiler.h> |
| 17 | #include <linux/types.h> |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 18 | #include <asm/barrier.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/byteorder.h> /* sigh ... */ |
| 20 | #include <asm/cpu-features.h> |
Ralf Baechle | 4ffd8b3 | 2006-11-30 01:14:50 +0000 | [diff] [blame] | 21 | #include <asm/sgidefs.h> |
| 22 | #include <asm/war.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Ralf Baechle | 49a89ef | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 24 | #if _MIPS_SZLONG == 32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #define SZLONG_LOG 5 |
| 26 | #define SZLONG_MASK 31UL |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 27 | #define __LL "ll " |
| 28 | #define __SC "sc " |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 29 | #define __INS "ins " |
| 30 | #define __EXT "ext " |
Ralf Baechle | 49a89ef | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 31 | #elif _MIPS_SZLONG == 64 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #define SZLONG_LOG 6 |
| 33 | #define SZLONG_MASK 63UL |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 34 | #define __LL "lld " |
| 35 | #define __SC "scd " |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 36 | #define __INS "dins " |
| 37 | #define __EXT "dext " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #endif |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | /* |
Jim Quinlan | 92d1159 | 2012-09-06 11:36:55 -0400 | [diff] [blame] | 41 | * These are the "slower" versions of the functions and are in bitops.c. |
| 42 | * These functions call raw_local_irq_{save,restore}(). |
| 43 | */ |
| 44 | void __mips_set_bit(unsigned long nr, volatile unsigned long *addr); |
| 45 | void __mips_clear_bit(unsigned long nr, volatile unsigned long *addr); |
| 46 | void __mips_change_bit(unsigned long nr, volatile unsigned long *addr); |
| 47 | int __mips_test_and_set_bit(unsigned long nr, |
| 48 | volatile unsigned long *addr); |
| 49 | int __mips_test_and_set_bit_lock(unsigned long nr, |
| 50 | volatile unsigned long *addr); |
| 51 | int __mips_test_and_clear_bit(unsigned long nr, |
| 52 | volatile unsigned long *addr); |
| 53 | int __mips_test_and_change_bit(unsigned long nr, |
| 54 | volatile unsigned long *addr); |
| 55 | |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | * set_bit - Atomically set a bit in memory |
| 59 | * @nr: the bit to set |
| 60 | * @addr: the address to start counting from |
| 61 | * |
| 62 | * This function is atomic and may not be reordered. See __set_bit() |
| 63 | * if you do not require the atomic guarantees. |
| 64 | * Note that @nr may be almost arbitrarily large; this function is not |
| 65 | * restricted to acting on a single-word quantity. |
| 66 | */ |
| 67 | static inline void set_bit(unsigned long nr, volatile unsigned long *addr) |
| 68 | { |
| 69 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
Jim Quinlan | 9de79c5 | 2012-09-06 11:36:54 -0400 | [diff] [blame] | 70 | int bit = nr & SZLONG_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | unsigned long temp; |
| 72 | |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 73 | if (kernel_uses_llsc && R10000_LLSC_WAR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | __asm__ __volatile__( |
Ralf Baechle | a809d46 | 2014-03-30 13:20:10 +0200 | [diff] [blame] | 75 | " .set arch=r4000 \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) |
Ralf Baechle | b961153 | 2007-03-05 00:56:15 +0000 | [diff] [blame] | 82 | : "ir" (1UL << bit), "m" (*m)); |
Ralf Baechle | 102fa15 | 2007-02-16 17:18:50 +0000 | [diff] [blame] | 83 | #ifdef CONFIG_CPU_MIPSR2 |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 84 | } else if (kernel_uses_llsc && __builtin_constant_p(bit)) { |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 85 | do { |
| 86 | __asm__ __volatile__( |
| 87 | " " __LL "%0, %1 # set_bit \n" |
| 88 | " " __INS "%0, %3, %2, 1 \n" |
| 89 | " " __SC "%0, %1 \n" |
| 90 | : "=&r" (temp), "+m" (*m) |
| 91 | : "ir" (bit), "r" (~0)); |
| 92 | } while (unlikely(!temp)); |
Ralf Baechle | 102fa15 | 2007-02-16 17:18:50 +0000 | [diff] [blame] | 93 | #endif /* CONFIG_CPU_MIPSR2 */ |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 94 | } else if (kernel_uses_llsc) { |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 95 | do { |
| 96 | __asm__ __volatile__( |
Ralf Baechle | a809d46 | 2014-03-30 13:20:10 +0200 | [diff] [blame] | 97 | " .set arch=r4000 \n" |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 98 | " " __LL "%0, %1 # set_bit \n" |
| 99 | " or %0, %2 \n" |
| 100 | " " __SC "%0, %1 \n" |
| 101 | " .set mips0 \n" |
| 102 | : "=&r" (temp), "+m" (*m) |
| 103 | : "ir" (1UL << bit)); |
| 104 | } while (unlikely(!temp)); |
Jim Quinlan | 92d1159 | 2012-09-06 11:36:55 -0400 | [diff] [blame] | 105 | } else |
| 106 | __mips_set_bit(nr, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | * clear_bit - Clears a bit in memory |
| 111 | * @nr: Bit to clear |
| 112 | * @addr: Address to start counting from |
| 113 | * |
| 114 | * clear_bit() is atomic and may not be reordered. However, it does |
| 115 | * not contain a memory barrier, so if it is used for locking purposes, |
Peter Zijlstra | 91bbefe | 2014-03-13 19:00:36 +0100 | [diff] [blame] | 116 | * you should call smp_mb__before_atomic() and/or smp_mb__after_atomic() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | * in order to ensure changes are visible on other processors. |
| 118 | */ |
| 119 | static inline void clear_bit(unsigned long nr, volatile unsigned long *addr) |
| 120 | { |
| 121 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
Jim Quinlan | 9de79c5 | 2012-09-06 11:36:54 -0400 | [diff] [blame] | 122 | int bit = nr & SZLONG_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | unsigned long temp; |
| 124 | |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 125 | if (kernel_uses_llsc && R10000_LLSC_WAR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | __asm__ __volatile__( |
Ralf Baechle | a809d46 | 2014-03-30 13:20:10 +0200 | [diff] [blame] | 127 | " .set arch=r4000 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | "1: " __LL "%0, %1 # clear_bit \n" |
| 129 | " and %0, %2 \n" |
| 130 | " " __SC "%0, %1 \n" |
| 131 | " beqzl %0, 1b \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 132 | " .set mips0 \n" |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 133 | : "=&r" (temp), "+m" (*m) |
| 134 | : "ir" (~(1UL << bit))); |
Ralf Baechle | 102fa15 | 2007-02-16 17:18:50 +0000 | [diff] [blame] | 135 | #ifdef CONFIG_CPU_MIPSR2 |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 136 | } else if (kernel_uses_llsc && __builtin_constant_p(bit)) { |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 137 | do { |
| 138 | __asm__ __volatile__( |
| 139 | " " __LL "%0, %1 # clear_bit \n" |
| 140 | " " __INS "%0, $0, %2, 1 \n" |
| 141 | " " __SC "%0, %1 \n" |
| 142 | : "=&r" (temp), "+m" (*m) |
| 143 | : "ir" (bit)); |
| 144 | } while (unlikely(!temp)); |
Ralf Baechle | 102fa15 | 2007-02-16 17:18:50 +0000 | [diff] [blame] | 145 | #endif /* CONFIG_CPU_MIPSR2 */ |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 146 | } else if (kernel_uses_llsc) { |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 147 | do { |
| 148 | __asm__ __volatile__( |
Ralf Baechle | a809d46 | 2014-03-30 13:20:10 +0200 | [diff] [blame] | 149 | " .set arch=r4000 \n" |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 150 | " " __LL "%0, %1 # clear_bit \n" |
| 151 | " and %0, %2 \n" |
| 152 | " " __SC "%0, %1 \n" |
| 153 | " .set mips0 \n" |
| 154 | : "=&r" (temp), "+m" (*m) |
| 155 | : "ir" (~(1UL << bit))); |
| 156 | } while (unlikely(!temp)); |
Jim Quinlan | 92d1159 | 2012-09-06 11:36:55 -0400 | [diff] [blame] | 157 | } else |
| 158 | __mips_clear_bit(nr, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* |
Nick Piggin | 728697c | 2007-10-18 03:06:53 -0700 | [diff] [blame] | 162 | * clear_bit_unlock - Clears a bit in memory |
| 163 | * @nr: Bit to clear |
| 164 | * @addr: Address to start counting from |
| 165 | * |
| 166 | * clear_bit() is atomic and implies release semantics before the memory |
| 167 | * operation. It can be used for an unlock. |
| 168 | */ |
| 169 | static inline void clear_bit_unlock(unsigned long nr, volatile unsigned long *addr) |
| 170 | { |
Peter Zijlstra | 91bbefe | 2014-03-13 19:00:36 +0100 | [diff] [blame] | 171 | smp_mb__before_atomic(); |
Nick Piggin | 728697c | 2007-10-18 03:06:53 -0700 | [diff] [blame] | 172 | clear_bit(nr, addr); |
| 173 | } |
| 174 | |
| 175 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | * change_bit - Toggle a bit in memory |
| 177 | * @nr: Bit to change |
| 178 | * @addr: Address to start counting from |
| 179 | * |
| 180 | * change_bit() is atomic and may not be reordered. |
| 181 | * Note that @nr may be almost arbitrarily large; this function is not |
| 182 | * restricted to acting on a single-word quantity. |
| 183 | */ |
| 184 | static inline void change_bit(unsigned long nr, volatile unsigned long *addr) |
| 185 | { |
Jim Quinlan | 9de79c5 | 2012-09-06 11:36:54 -0400 | [diff] [blame] | 186 | int bit = nr & SZLONG_MASK; |
Ralf Baechle | b961153 | 2007-03-05 00:56:15 +0000 | [diff] [blame] | 187 | |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 188 | if (kernel_uses_llsc && R10000_LLSC_WAR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
| 190 | unsigned long temp; |
| 191 | |
| 192 | __asm__ __volatile__( |
Ralf Baechle | a809d46 | 2014-03-30 13:20:10 +0200 | [diff] [blame] | 193 | " .set arch=r4000 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | "1: " __LL "%0, %1 # change_bit \n" |
| 195 | " xor %0, %2 \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 196 | " " __SC "%0, %1 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | " beqzl %0, 1b \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 198 | " .set mips0 \n" |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 199 | : "=&r" (temp), "+m" (*m) |
| 200 | : "ir" (1UL << bit)); |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 201 | } else if (kernel_uses_llsc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
| 203 | unsigned long temp; |
| 204 | |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 205 | do { |
| 206 | __asm__ __volatile__( |
Ralf Baechle | a809d46 | 2014-03-30 13:20:10 +0200 | [diff] [blame] | 207 | " .set arch=r4000 \n" |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 208 | " " __LL "%0, %1 # change_bit \n" |
| 209 | " xor %0, %2 \n" |
| 210 | " " __SC "%0, %1 \n" |
| 211 | " .set mips0 \n" |
| 212 | : "=&r" (temp), "+m" (*m) |
| 213 | : "ir" (1UL << bit)); |
| 214 | } while (unlikely(!temp)); |
Jim Quinlan | 92d1159 | 2012-09-06 11:36:55 -0400 | [diff] [blame] | 215 | } else |
| 216 | __mips_change_bit(nr, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | * test_and_set_bit - Set a bit and return its old value |
| 221 | * @nr: Bit to set |
| 222 | * @addr: Address to count from |
| 223 | * |
| 224 | * This operation is atomic and cannot be reordered. |
| 225 | * It also implies a memory barrier. |
| 226 | */ |
| 227 | static inline int test_and_set_bit(unsigned long nr, |
| 228 | volatile unsigned long *addr) |
| 229 | { |
Jim Quinlan | 9de79c5 | 2012-09-06 11:36:54 -0400 | [diff] [blame] | 230 | int bit = nr & SZLONG_MASK; |
Ralf Baechle | ff72b7a | 2007-06-07 13:17:30 +0100 | [diff] [blame] | 231 | unsigned long res; |
Ralf Baechle | b961153 | 2007-03-05 00:56:15 +0000 | [diff] [blame] | 232 | |
David Daney | f252ffd | 2010-01-08 17:17:43 -0800 | [diff] [blame] | 233 | smp_mb__before_llsc(); |
Nick Piggin | c8f30ae | 2007-10-18 03:06:52 -0700 | [diff] [blame] | 234 | |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 235 | if (kernel_uses_llsc && R10000_LLSC_WAR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
Ralf Baechle | ff72b7a | 2007-06-07 13:17:30 +0100 | [diff] [blame] | 237 | unsigned long temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
| 239 | __asm__ __volatile__( |
Ralf Baechle | a809d46 | 2014-03-30 13:20:10 +0200 | [diff] [blame] | 240 | " .set arch=r4000 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | "1: " __LL "%0, %1 # test_and_set_bit \n" |
| 242 | " or %2, %0, %3 \n" |
| 243 | " " __SC "%2, %1 \n" |
| 244 | " beqzl %2, 1b \n" |
| 245 | " and %2, %0, %3 \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 246 | " .set mips0 \n" |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 247 | : "=&r" (temp), "+m" (*m), "=&r" (res) |
| 248 | : "r" (1UL << bit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | : "memory"); |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 250 | } else if (kernel_uses_llsc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
Ralf Baechle | ff72b7a | 2007-06-07 13:17:30 +0100 | [diff] [blame] | 252 | unsigned long temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 254 | do { |
| 255 | __asm__ __volatile__( |
Ralf Baechle | a809d46 | 2014-03-30 13:20:10 +0200 | [diff] [blame] | 256 | " .set arch=r4000 \n" |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 257 | " " __LL "%0, %1 # test_and_set_bit \n" |
| 258 | " or %2, %0, %3 \n" |
| 259 | " " __SC "%2, %1 \n" |
| 260 | " .set mips0 \n" |
| 261 | : "=&r" (temp), "+m" (*m), "=&r" (res) |
| 262 | : "r" (1UL << bit) |
| 263 | : "memory"); |
| 264 | } while (unlikely(!res)); |
| 265 | |
| 266 | res = temp & (1UL << bit); |
Jim Quinlan | 92d1159 | 2012-09-06 11:36:55 -0400 | [diff] [blame] | 267 | } else |
| 268 | res = __mips_test_and_set_bit(nr, addr); |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 269 | |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 270 | smp_llsc_mb(); |
Ralf Baechle | ff72b7a | 2007-06-07 13:17:30 +0100 | [diff] [blame] | 271 | |
| 272 | return res != 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* |
Nick Piggin | 728697c | 2007-10-18 03:06:53 -0700 | [diff] [blame] | 276 | * test_and_set_bit_lock - Set a bit and return its old value |
| 277 | * @nr: Bit to set |
| 278 | * @addr: Address to count from |
| 279 | * |
| 280 | * This operation is atomic and implies acquire ordering semantics |
| 281 | * after the memory operation. |
| 282 | */ |
| 283 | static inline int test_and_set_bit_lock(unsigned long nr, |
| 284 | volatile unsigned long *addr) |
| 285 | { |
Jim Quinlan | 9de79c5 | 2012-09-06 11:36:54 -0400 | [diff] [blame] | 286 | int bit = nr & SZLONG_MASK; |
Nick Piggin | 728697c | 2007-10-18 03:06:53 -0700 | [diff] [blame] | 287 | unsigned long res; |
| 288 | |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 289 | if (kernel_uses_llsc && R10000_LLSC_WAR) { |
Nick Piggin | 728697c | 2007-10-18 03:06:53 -0700 | [diff] [blame] | 290 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
| 291 | unsigned long temp; |
| 292 | |
| 293 | __asm__ __volatile__( |
Ralf Baechle | a809d46 | 2014-03-30 13:20:10 +0200 | [diff] [blame] | 294 | " .set arch=r4000 \n" |
Nick Piggin | 728697c | 2007-10-18 03:06:53 -0700 | [diff] [blame] | 295 | "1: " __LL "%0, %1 # test_and_set_bit \n" |
| 296 | " or %2, %0, %3 \n" |
| 297 | " " __SC "%2, %1 \n" |
| 298 | " beqzl %2, 1b \n" |
| 299 | " and %2, %0, %3 \n" |
| 300 | " .set mips0 \n" |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 301 | : "=&r" (temp), "+m" (*m), "=&r" (res) |
| 302 | : "r" (1UL << bit) |
Nick Piggin | 728697c | 2007-10-18 03:06:53 -0700 | [diff] [blame] | 303 | : "memory"); |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 304 | } else if (kernel_uses_llsc) { |
Nick Piggin | 728697c | 2007-10-18 03:06:53 -0700 | [diff] [blame] | 305 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
| 306 | unsigned long temp; |
| 307 | |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 308 | do { |
| 309 | __asm__ __volatile__( |
Ralf Baechle | a809d46 | 2014-03-30 13:20:10 +0200 | [diff] [blame] | 310 | " .set arch=r4000 \n" |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 311 | " " __LL "%0, %1 # test_and_set_bit \n" |
| 312 | " or %2, %0, %3 \n" |
| 313 | " " __SC "%2, %1 \n" |
| 314 | " .set mips0 \n" |
| 315 | : "=&r" (temp), "+m" (*m), "=&r" (res) |
| 316 | : "r" (1UL << bit) |
| 317 | : "memory"); |
| 318 | } while (unlikely(!res)); |
| 319 | |
| 320 | res = temp & (1UL << bit); |
Jim Quinlan | 92d1159 | 2012-09-06 11:36:55 -0400 | [diff] [blame] | 321 | } else |
| 322 | res = __mips_test_and_set_bit_lock(nr, addr); |
Nick Piggin | 728697c | 2007-10-18 03:06:53 -0700 | [diff] [blame] | 323 | |
| 324 | smp_llsc_mb(); |
| 325 | |
| 326 | return res != 0; |
| 327 | } |
| 328 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | * test_and_clear_bit - Clear a bit and return its old value |
| 330 | * @nr: Bit to clear |
| 331 | * @addr: Address to count from |
| 332 | * |
| 333 | * This operation is atomic and cannot be reordered. |
| 334 | * It also implies a memory barrier. |
| 335 | */ |
| 336 | static inline int test_and_clear_bit(unsigned long nr, |
| 337 | volatile unsigned long *addr) |
| 338 | { |
Jim Quinlan | 9de79c5 | 2012-09-06 11:36:54 -0400 | [diff] [blame] | 339 | int bit = nr & SZLONG_MASK; |
Ralf Baechle | ff72b7a | 2007-06-07 13:17:30 +0100 | [diff] [blame] | 340 | unsigned long res; |
Ralf Baechle | b961153 | 2007-03-05 00:56:15 +0000 | [diff] [blame] | 341 | |
David Daney | f252ffd | 2010-01-08 17:17:43 -0800 | [diff] [blame] | 342 | smp_mb__before_llsc(); |
Nick Piggin | c8f30ae | 2007-10-18 03:06:52 -0700 | [diff] [blame] | 343 | |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 344 | if (kernel_uses_llsc && R10000_LLSC_WAR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
Atsushi Nemoto | 8e09ffb | 2007-06-14 00:56:31 +0900 | [diff] [blame] | 346 | unsigned long temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 348 | __asm__ __volatile__( |
Ralf Baechle | a809d46 | 2014-03-30 13:20:10 +0200 | [diff] [blame] | 349 | " .set arch=r4000 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | "1: " __LL "%0, %1 # test_and_clear_bit \n" |
| 351 | " or %2, %0, %3 \n" |
| 352 | " xor %2, %3 \n" |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 353 | " " __SC "%2, %1 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | " beqzl %2, 1b \n" |
| 355 | " and %2, %0, %3 \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 356 | " .set mips0 \n" |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 357 | : "=&r" (temp), "+m" (*m), "=&r" (res) |
| 358 | : "r" (1UL << bit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | : "memory"); |
Ralf Baechle | 102fa15 | 2007-02-16 17:18:50 +0000 | [diff] [blame] | 360 | #ifdef CONFIG_CPU_MIPSR2 |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 361 | } else if (kernel_uses_llsc && __builtin_constant_p(nr)) { |
Ralf Baechle | 102fa15 | 2007-02-16 17:18:50 +0000 | [diff] [blame] | 362 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
Ralf Baechle | ff72b7a | 2007-06-07 13:17:30 +0100 | [diff] [blame] | 363 | unsigned long temp; |
Ralf Baechle | 102fa15 | 2007-02-16 17:18:50 +0000 | [diff] [blame] | 364 | |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 365 | do { |
| 366 | __asm__ __volatile__( |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 367 | " " __LL "%0, %1 # test_and_clear_bit \n" |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 368 | " " __EXT "%2, %0, %3, 1 \n" |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 369 | " " __INS "%0, $0, %3, 1 \n" |
| 370 | " " __SC "%0, %1 \n" |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 371 | : "=&r" (temp), "+m" (*m), "=&r" (res) |
| 372 | : "ir" (bit) |
| 373 | : "memory"); |
| 374 | } while (unlikely(!temp)); |
Ralf Baechle | 102fa15 | 2007-02-16 17:18:50 +0000 | [diff] [blame] | 375 | #endif |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 376 | } else if (kernel_uses_llsc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
Ralf Baechle | ff72b7a | 2007-06-07 13:17:30 +0100 | [diff] [blame] | 378 | unsigned long temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 380 | do { |
| 381 | __asm__ __volatile__( |
Ralf Baechle | a809d46 | 2014-03-30 13:20:10 +0200 | [diff] [blame] | 382 | " .set arch=r4000 \n" |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 383 | " " __LL "%0, %1 # test_and_clear_bit \n" |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 384 | " or %2, %0, %3 \n" |
| 385 | " xor %2, %3 \n" |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 386 | " " __SC "%2, %1 \n" |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 387 | " .set mips0 \n" |
| 388 | : "=&r" (temp), "+m" (*m), "=&r" (res) |
| 389 | : "r" (1UL << bit) |
| 390 | : "memory"); |
| 391 | } while (unlikely(!res)); |
| 392 | |
| 393 | res = temp & (1UL << bit); |
Jim Quinlan | 92d1159 | 2012-09-06 11:36:55 -0400 | [diff] [blame] | 394 | } else |
| 395 | res = __mips_test_and_clear_bit(nr, addr); |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 396 | |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 397 | smp_llsc_mb(); |
Ralf Baechle | ff72b7a | 2007-06-07 13:17:30 +0100 | [diff] [blame] | 398 | |
| 399 | return res != 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | * test_and_change_bit - Change a bit and return its old value |
| 404 | * @nr: Bit to change |
| 405 | * @addr: Address to count from |
| 406 | * |
| 407 | * This operation is atomic and cannot be reordered. |
| 408 | * It also implies a memory barrier. |
| 409 | */ |
| 410 | static inline int test_and_change_bit(unsigned long nr, |
| 411 | volatile unsigned long *addr) |
| 412 | { |
Jim Quinlan | 9de79c5 | 2012-09-06 11:36:54 -0400 | [diff] [blame] | 413 | int bit = nr & SZLONG_MASK; |
Ralf Baechle | ff72b7a | 2007-06-07 13:17:30 +0100 | [diff] [blame] | 414 | unsigned long res; |
Ralf Baechle | b961153 | 2007-03-05 00:56:15 +0000 | [diff] [blame] | 415 | |
David Daney | f252ffd | 2010-01-08 17:17:43 -0800 | [diff] [blame] | 416 | smp_mb__before_llsc(); |
Nick Piggin | c8f30ae | 2007-10-18 03:06:52 -0700 | [diff] [blame] | 417 | |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 418 | if (kernel_uses_llsc && R10000_LLSC_WAR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
Ralf Baechle | ff72b7a | 2007-06-07 13:17:30 +0100 | [diff] [blame] | 420 | unsigned long temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
| 422 | __asm__ __volatile__( |
Ralf Baechle | a809d46 | 2014-03-30 13:20:10 +0200 | [diff] [blame] | 423 | " .set arch=r4000 \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 424 | "1: " __LL "%0, %1 # test_and_change_bit \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | " xor %2, %0, %3 \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 426 | " " __SC "%2, %1 \n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | " beqzl %2, 1b \n" |
| 428 | " and %2, %0, %3 \n" |
Maciej W. Rozycki | aac8aa7 | 2005-06-14 17:35:03 +0000 | [diff] [blame] | 429 | " .set mips0 \n" |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 430 | : "=&r" (temp), "+m" (*m), "=&r" (res) |
| 431 | : "r" (1UL << bit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | : "memory"); |
David Daney | b791d11 | 2009-07-13 11:15:19 -0700 | [diff] [blame] | 433 | } else if (kernel_uses_llsc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG); |
Ralf Baechle | ff72b7a | 2007-06-07 13:17:30 +0100 | [diff] [blame] | 435 | unsigned long temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 437 | do { |
| 438 | __asm__ __volatile__( |
Ralf Baechle | a809d46 | 2014-03-30 13:20:10 +0200 | [diff] [blame] | 439 | " .set arch=r4000 \n" |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 440 | " " __LL "%0, %1 # test_and_change_bit \n" |
Ralf Baechle | 7837314 | 2010-10-29 19:08:24 +0100 | [diff] [blame] | 441 | " xor %2, %0, %3 \n" |
| 442 | " " __SC "\t%2, %1 \n" |
| 443 | " .set mips0 \n" |
| 444 | : "=&r" (temp), "+m" (*m), "=&r" (res) |
| 445 | : "r" (1UL << bit) |
| 446 | : "memory"); |
| 447 | } while (unlikely(!res)); |
| 448 | |
| 449 | res = temp & (1UL << bit); |
Jim Quinlan | 92d1159 | 2012-09-06 11:36:55 -0400 | [diff] [blame] | 450 | } else |
| 451 | res = __mips_test_and_change_bit(nr, addr); |
Ralf Baechle | 0004a9d | 2006-10-31 03:45:07 +0000 | [diff] [blame] | 452 | |
Ralf Baechle | 17099b1 | 2007-07-14 13:24:05 +0100 | [diff] [blame] | 453 | smp_llsc_mb(); |
Ralf Baechle | ff72b7a | 2007-06-07 13:17:30 +0100 | [diff] [blame] | 454 | |
| 455 | return res != 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Akinobu Mita | 3c9ee7e | 2006-03-26 01:39:30 -0800 | [diff] [blame] | 458 | #include <asm-generic/bitops/non-atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 460 | /* |
Nick Piggin | 728697c | 2007-10-18 03:06:53 -0700 | [diff] [blame] | 461 | * __clear_bit_unlock - Clears a bit in memory |
| 462 | * @nr: Bit to clear |
| 463 | * @addr: Address to start counting from |
| 464 | * |
| 465 | * __clear_bit() is non-atomic and implies release semantics before the memory |
| 466 | * operation. It can be used for an unlock if no other CPUs can concurrently |
| 467 | * modify other bits in the word. |
| 468 | */ |
| 469 | static inline void __clear_bit_unlock(unsigned long nr, volatile unsigned long *addr) |
| 470 | { |
| 471 | smp_mb(); |
| 472 | __clear_bit(nr, addr); |
| 473 | } |
| 474 | |
| 475 | /* |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 476 | * Return the bit position (0..63) of the most significant 1 bit in a word |
| 477 | * Returns -1 if no 1 bit exists |
| 478 | */ |
Ralf Baechle | 4816227 | 2008-10-28 09:40:35 +0000 | [diff] [blame] | 479 | static inline unsigned long __fls(unsigned long word) |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 480 | { |
Ralf Baechle | 4816227 | 2008-10-28 09:40:35 +0000 | [diff] [blame] | 481 | int num; |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 482 | |
Ralf Baechle | 4816227 | 2008-10-28 09:40:35 +0000 | [diff] [blame] | 483 | if (BITS_PER_LONG == 32 && |
Ralf Baechle | 47740eb | 2009-04-19 03:21:22 +0200 | [diff] [blame] | 484 | __builtin_constant_p(cpu_has_clo_clz) && cpu_has_clo_clz) { |
Ralf Baechle | 49a89ef | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 485 | __asm__( |
Ralf Baechle | ec917c2c | 2005-10-07 16:58:15 +0100 | [diff] [blame] | 486 | " .set push \n" |
| 487 | " .set mips32 \n" |
| 488 | " clz %0, %1 \n" |
| 489 | " .set pop \n" |
Ralf Baechle | 4816227 | 2008-10-28 09:40:35 +0000 | [diff] [blame] | 490 | : "=r" (num) |
| 491 | : "r" (word)); |
Ralf Baechle | ec917c2c | 2005-10-07 16:58:15 +0100 | [diff] [blame] | 492 | |
Ralf Baechle | 4816227 | 2008-10-28 09:40:35 +0000 | [diff] [blame] | 493 | return 31 - num; |
Ralf Baechle | ec917c2c | 2005-10-07 16:58:15 +0100 | [diff] [blame] | 494 | } |
| 495 | |
Ralf Baechle | 4816227 | 2008-10-28 09:40:35 +0000 | [diff] [blame] | 496 | if (BITS_PER_LONG == 64 && |
| 497 | __builtin_constant_p(cpu_has_mips64) && cpu_has_mips64) { |
| 498 | __asm__( |
| 499 | " .set push \n" |
| 500 | " .set mips64 \n" |
| 501 | " dclz %0, %1 \n" |
| 502 | " .set pop \n" |
| 503 | : "=r" (num) |
| 504 | : "r" (word)); |
Ralf Baechle | ec917c2c | 2005-10-07 16:58:15 +0100 | [diff] [blame] | 505 | |
Ralf Baechle | 4816227 | 2008-10-28 09:40:35 +0000 | [diff] [blame] | 506 | return 63 - num; |
| 507 | } |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 508 | |
Ralf Baechle | 4816227 | 2008-10-28 09:40:35 +0000 | [diff] [blame] | 509 | num = BITS_PER_LONG - 1; |
| 510 | |
| 511 | #if BITS_PER_LONG == 64 |
| 512 | if (!(word & (~0ul << 32))) { |
| 513 | num -= 32; |
| 514 | word <<= 32; |
| 515 | } |
| 516 | #endif |
| 517 | if (!(word & (~0ul << (BITS_PER_LONG-16)))) { |
| 518 | num -= 16; |
| 519 | word <<= 16; |
| 520 | } |
| 521 | if (!(word & (~0ul << (BITS_PER_LONG-8)))) { |
| 522 | num -= 8; |
| 523 | word <<= 8; |
| 524 | } |
| 525 | if (!(word & (~0ul << (BITS_PER_LONG-4)))) { |
| 526 | num -= 4; |
| 527 | word <<= 4; |
| 528 | } |
| 529 | if (!(word & (~0ul << (BITS_PER_LONG-2)))) { |
| 530 | num -= 2; |
| 531 | word <<= 2; |
| 532 | } |
| 533 | if (!(word & (~0ul << (BITS_PER_LONG-1)))) |
| 534 | num -= 1; |
| 535 | return num; |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 536 | } |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 537 | |
| 538 | /* |
| 539 | * __ffs - find first bit in word. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | * @word: The word to search |
| 541 | * |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 542 | * Returns 0..SZLONG-1 |
| 543 | * Undefined if no bit exists, so code should check against 0 first. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | */ |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 545 | static inline unsigned long __ffs(unsigned long word) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | { |
Ralf Baechle | ddc0d00 | 2008-05-04 14:53:53 +0100 | [diff] [blame] | 547 | return __fls(word & -word); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | /* |
Atsushi Nemoto | bc81824 | 2006-04-17 21:19:12 +0900 | [diff] [blame] | 551 | * fls - find last bit set. |
| 552 | * @word: The word to search |
| 553 | * |
| 554 | * This is defined the same way as ffs. |
| 555 | * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. |
| 556 | */ |
Ralf Baechle | 4816227 | 2008-10-28 09:40:35 +0000 | [diff] [blame] | 557 | static inline int fls(int x) |
Atsushi Nemoto | bc81824 | 2006-04-17 21:19:12 +0900 | [diff] [blame] | 558 | { |
Ralf Baechle | 4816227 | 2008-10-28 09:40:35 +0000 | [diff] [blame] | 559 | int r; |
Atsushi Nemoto | bc81824 | 2006-04-17 21:19:12 +0900 | [diff] [blame] | 560 | |
Ralf Baechle | 47740eb | 2009-04-19 03:21:22 +0200 | [diff] [blame] | 561 | if (__builtin_constant_p(cpu_has_clo_clz) && cpu_has_clo_clz) { |
Ralf Baechle | 4816227 | 2008-10-28 09:40:35 +0000 | [diff] [blame] | 562 | __asm__("clz %0, %1" : "=r" (x) : "r" (x)); |
| 563 | |
| 564 | return 32 - x; |
| 565 | } |
| 566 | |
| 567 | r = 32; |
| 568 | if (!x) |
| 569 | return 0; |
| 570 | if (!(x & 0xffff0000u)) { |
| 571 | x <<= 16; |
| 572 | r -= 16; |
| 573 | } |
| 574 | if (!(x & 0xff000000u)) { |
| 575 | x <<= 8; |
| 576 | r -= 8; |
| 577 | } |
| 578 | if (!(x & 0xf0000000u)) { |
| 579 | x <<= 4; |
| 580 | r -= 4; |
| 581 | } |
| 582 | if (!(x & 0xc0000000u)) { |
| 583 | x <<= 2; |
| 584 | r -= 2; |
| 585 | } |
| 586 | if (!(x & 0x80000000u)) { |
| 587 | x <<= 1; |
| 588 | r -= 1; |
| 589 | } |
| 590 | return r; |
Atsushi Nemoto | bc81824 | 2006-04-17 21:19:12 +0900 | [diff] [blame] | 591 | } |
| 592 | |
Atsushi Nemoto | bc81824 | 2006-04-17 21:19:12 +0900 | [diff] [blame] | 593 | #include <asm-generic/bitops/fls64.h> |
Atsushi Nemoto | bc81824 | 2006-04-17 21:19:12 +0900 | [diff] [blame] | 594 | |
| 595 | /* |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 596 | * ffs - find first bit set. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | * @word: The word to search |
| 598 | * |
Atsushi Nemoto | bc81824 | 2006-04-17 21:19:12 +0900 | [diff] [blame] | 599 | * This is defined the same way as |
| 600 | * the libc and compiler builtin ffs routines, therefore |
| 601 | * differs in spirit from the above ffz (man ffs). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | */ |
Atsushi Nemoto | bc81824 | 2006-04-17 21:19:12 +0900 | [diff] [blame] | 603 | static inline int ffs(int word) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | { |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 605 | if (!word) |
| 606 | return 0; |
| 607 | |
Atsushi Nemoto | bc81824 | 2006-04-17 21:19:12 +0900 | [diff] [blame] | 608 | return fls(word & -word); |
Ralf Baechle | 6590326 | 2005-07-12 12:50:30 +0000 | [diff] [blame] | 609 | } |
Ralf Baechle | 2caf190 | 2006-01-30 17:14:41 +0000 | [diff] [blame] | 610 | |
Atsushi Nemoto | bc81824 | 2006-04-17 21:19:12 +0900 | [diff] [blame] | 611 | #include <asm-generic/bitops/ffz.h> |
Akinobu Mita | 3c9ee7e | 2006-03-26 01:39:30 -0800 | [diff] [blame] | 612 | #include <asm-generic/bitops/find.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
| 614 | #ifdef __KERNEL__ |
| 615 | |
Akinobu Mita | 3c9ee7e | 2006-03-26 01:39:30 -0800 | [diff] [blame] | 616 | #include <asm-generic/bitops/sched.h> |
David Daney | 1a403d1 | 2010-06-25 16:46:07 -0700 | [diff] [blame] | 617 | |
| 618 | #include <asm/arch_hweight.h> |
| 619 | #include <asm-generic/bitops/const_hweight.h> |
| 620 | |
Akinobu Mita | 861b5ae | 2011-03-23 16:42:02 -0700 | [diff] [blame] | 621 | #include <asm-generic/bitops/le.h> |
Akinobu Mita | 3c9ee7e | 2006-03-26 01:39:30 -0800 | [diff] [blame] | 622 | #include <asm-generic/bitops/ext2-atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
| 624 | #endif /* __KERNEL__ */ |
| 625 | |
| 626 | #endif /* _ASM_BITOPS_H */ |