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