H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 1 | #ifndef _ASM_X86_BITOPS_H |
| 2 | #define _ASM_X86_BITOPS_H |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 3 | |
| 4 | /* |
| 5 | * Copyright 1992, Linus Torvalds. |
Andi Kleen | c8399943 | 2009-01-12 23:01:15 +0100 | [diff] [blame] | 6 | * |
| 7 | * Note: inlines with more than a single statement should be marked |
| 8 | * __always_inline to avoid problems with older gcc's inlining heuristics. |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #ifndef _LINUX_BITOPS_H |
| 12 | #error only <linux/bitops.h> can be included directly |
| 13 | #endif |
| 14 | |
| 15 | #include <linux/compiler.h> |
| 16 | #include <asm/alternative.h> |
Peter Zijlstra | 0c44c2d | 2013-09-11 15:19:24 +0200 | [diff] [blame^] | 17 | #include <asm/rmwcc.h> |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 18 | |
H. Peter Anvin | 9b71050 | 2013-07-16 15:20:14 -0700 | [diff] [blame] | 19 | #if BITS_PER_LONG == 32 |
| 20 | # define _BITOPS_LONG_SHIFT 5 |
| 21 | #elif BITS_PER_LONG == 64 |
| 22 | # define _BITOPS_LONG_SHIFT 6 |
| 23 | #else |
| 24 | # error "Unexpected BITS_PER_LONG" |
| 25 | #endif |
| 26 | |
Borislav Petkov | e8f380e | 2012-05-22 12:53:45 +0200 | [diff] [blame] | 27 | #define BIT_64(n) (U64_C(1) << (n)) |
| 28 | |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 29 | /* |
| 30 | * These have to be done with inline assembly: that way the bit-setting |
| 31 | * is guaranteed to be atomic. All bit operations return 0 if the bit |
| 32 | * was cleared before the operation and != 0 if it was not. |
| 33 | * |
| 34 | * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1). |
| 35 | */ |
| 36 | |
| 37 | #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1) |
| 38 | /* Technically wrong, but this avoids compilation errors on some gcc |
| 39 | versions. */ |
Linus Torvalds | 1a750e0 | 2008-06-18 21:03:26 -0700 | [diff] [blame] | 40 | #define BITOP_ADDR(x) "=m" (*(volatile long *) (x)) |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 41 | #else |
Linus Torvalds | 1a750e0 | 2008-06-18 21:03:26 -0700 | [diff] [blame] | 42 | #define BITOP_ADDR(x) "+m" (*(volatile long *) (x)) |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 43 | #endif |
| 44 | |
Ingo Molnar | 7dbceaf | 2008-06-20 07:28:24 +0200 | [diff] [blame] | 45 | #define ADDR BITOP_ADDR(addr) |
Linus Torvalds | 1a750e0 | 2008-06-18 21:03:26 -0700 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * We do the locked ops that don't return the old value as |
| 49 | * a mask operation on a byte. |
| 50 | */ |
Ingo Molnar | 7dbceaf | 2008-06-20 07:28:24 +0200 | [diff] [blame] | 51 | #define IS_IMMEDIATE(nr) (__builtin_constant_p(nr)) |
| 52 | #define CONST_MASK_ADDR(nr, addr) BITOP_ADDR((void *)(addr) + ((nr)>>3)) |
| 53 | #define CONST_MASK(nr) (1 << ((nr) & 7)) |
Linus Torvalds | 1a750e0 | 2008-06-18 21:03:26 -0700 | [diff] [blame] | 54 | |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 55 | /** |
| 56 | * set_bit - Atomically set a bit in memory |
| 57 | * @nr: the bit to set |
| 58 | * @addr: the address to start counting from |
| 59 | * |
| 60 | * This function is atomic and may not be reordered. See __set_bit() |
| 61 | * if you do not require the atomic guarantees. |
| 62 | * |
| 63 | * Note: there are no guarantees that this function will not be reordered |
| 64 | * on non x86 architectures, so if you are writing portable code, |
| 65 | * make sure not to rely on its reordering guarantees. |
| 66 | * |
| 67 | * Note that @nr may be almost arbitrarily large; this function is not |
| 68 | * restricted to acting on a single-word quantity. |
| 69 | */ |
Andi Kleen | c8399943 | 2009-01-12 23:01:15 +0100 | [diff] [blame] | 70 | static __always_inline void |
H. Peter Anvin | 9b71050 | 2013-07-16 15:20:14 -0700 | [diff] [blame] | 71 | set_bit(long nr, volatile unsigned long *addr) |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 72 | { |
Ingo Molnar | 7dbceaf | 2008-06-20 07:28:24 +0200 | [diff] [blame] | 73 | if (IS_IMMEDIATE(nr)) { |
| 74 | asm volatile(LOCK_PREFIX "orb %1,%0" |
| 75 | : CONST_MASK_ADDR(nr, addr) |
Ingo Molnar | 437a0a5 | 2008-06-20 21:50:20 +0200 | [diff] [blame] | 76 | : "iq" ((u8)CONST_MASK(nr)) |
Ingo Molnar | 7dbceaf | 2008-06-20 07:28:24 +0200 | [diff] [blame] | 77 | : "memory"); |
| 78 | } else { |
| 79 | asm volatile(LOCK_PREFIX "bts %1,%0" |
| 80 | : BITOP_ADDR(addr) : "Ir" (nr) : "memory"); |
| 81 | } |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
| 85 | * __set_bit - Set a bit in memory |
| 86 | * @nr: the bit to set |
| 87 | * @addr: the address to start counting from |
| 88 | * |
| 89 | * Unlike set_bit(), this function is non-atomic and may be reordered. |
| 90 | * If it's called on the same region of memory simultaneously, the effect |
| 91 | * may be that only one operation succeeds. |
| 92 | */ |
H. Peter Anvin | 9b71050 | 2013-07-16 15:20:14 -0700 | [diff] [blame] | 93 | static inline void __set_bit(long nr, volatile unsigned long *addr) |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 94 | { |
Joe Perches | f19dcf4 | 2008-03-23 01:03:07 -0700 | [diff] [blame] | 95 | asm volatile("bts %1,%0" : ADDR : "Ir" (nr) : "memory"); |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 98 | /** |
| 99 | * clear_bit - Clears a bit in memory |
| 100 | * @nr: Bit to clear |
| 101 | * @addr: Address to start counting from |
| 102 | * |
| 103 | * clear_bit() is atomic and may not be reordered. However, it does |
| 104 | * not contain a memory barrier, so if it is used for locking purposes, |
| 105 | * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit() |
| 106 | * in order to ensure changes are visible on other processors. |
| 107 | */ |
Andi Kleen | c8399943 | 2009-01-12 23:01:15 +0100 | [diff] [blame] | 108 | static __always_inline void |
H. Peter Anvin | 9b71050 | 2013-07-16 15:20:14 -0700 | [diff] [blame] | 109 | clear_bit(long nr, volatile unsigned long *addr) |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 110 | { |
Ingo Molnar | 7dbceaf | 2008-06-20 07:28:24 +0200 | [diff] [blame] | 111 | if (IS_IMMEDIATE(nr)) { |
| 112 | asm volatile(LOCK_PREFIX "andb %1,%0" |
| 113 | : CONST_MASK_ADDR(nr, addr) |
Ingo Molnar | 437a0a5 | 2008-06-20 21:50:20 +0200 | [diff] [blame] | 114 | : "iq" ((u8)~CONST_MASK(nr))); |
Ingo Molnar | 7dbceaf | 2008-06-20 07:28:24 +0200 | [diff] [blame] | 115 | } else { |
| 116 | asm volatile(LOCK_PREFIX "btr %1,%0" |
| 117 | : BITOP_ADDR(addr) |
| 118 | : "Ir" (nr)); |
| 119 | } |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* |
| 123 | * clear_bit_unlock - Clears a bit in memory |
| 124 | * @nr: Bit to clear |
| 125 | * @addr: Address to start counting from |
| 126 | * |
| 127 | * clear_bit() is atomic and implies release semantics before the memory |
| 128 | * operation. It can be used for an unlock. |
| 129 | */ |
H. Peter Anvin | 9b71050 | 2013-07-16 15:20:14 -0700 | [diff] [blame] | 130 | static inline void clear_bit_unlock(long nr, volatile unsigned long *addr) |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 131 | { |
| 132 | barrier(); |
| 133 | clear_bit(nr, addr); |
| 134 | } |
| 135 | |
H. Peter Anvin | 9b71050 | 2013-07-16 15:20:14 -0700 | [diff] [blame] | 136 | static inline void __clear_bit(long nr, volatile unsigned long *addr) |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 137 | { |
Simon Holm Thøgersen | eb2b4e6 | 2008-05-05 15:45:28 +0200 | [diff] [blame] | 138 | asm volatile("btr %1,%0" : ADDR : "Ir" (nr)); |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /* |
| 142 | * __clear_bit_unlock - Clears a bit in memory |
| 143 | * @nr: Bit to clear |
| 144 | * @addr: Address to start counting from |
| 145 | * |
| 146 | * __clear_bit() is non-atomic and implies release semantics before the memory |
| 147 | * operation. It can be used for an unlock if no other CPUs can concurrently |
| 148 | * modify other bits in the word. |
| 149 | * |
| 150 | * No memory barrier is required here, because x86 cannot reorder stores past |
| 151 | * older loads. Same principle as spin_unlock. |
| 152 | */ |
H. Peter Anvin | 9b71050 | 2013-07-16 15:20:14 -0700 | [diff] [blame] | 153 | static inline void __clear_bit_unlock(long nr, volatile unsigned long *addr) |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 154 | { |
| 155 | barrier(); |
| 156 | __clear_bit(nr, addr); |
| 157 | } |
| 158 | |
| 159 | #define smp_mb__before_clear_bit() barrier() |
| 160 | #define smp_mb__after_clear_bit() barrier() |
| 161 | |
| 162 | /** |
| 163 | * __change_bit - Toggle a bit in memory |
| 164 | * @nr: the bit to change |
| 165 | * @addr: the address to start counting from |
| 166 | * |
| 167 | * Unlike change_bit(), this function is non-atomic and may be reordered. |
| 168 | * If it's called on the same region of memory simultaneously, the effect |
| 169 | * may be that only one operation succeeds. |
| 170 | */ |
H. Peter Anvin | 9b71050 | 2013-07-16 15:20:14 -0700 | [diff] [blame] | 171 | static inline void __change_bit(long nr, volatile unsigned long *addr) |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 172 | { |
Simon Holm Thøgersen | eb2b4e6 | 2008-05-05 15:45:28 +0200 | [diff] [blame] | 173 | asm volatile("btc %1,%0" : ADDR : "Ir" (nr)); |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
| 177 | * change_bit - Toggle a bit in memory |
| 178 | * @nr: Bit to change |
| 179 | * @addr: Address to start counting from |
| 180 | * |
| 181 | * change_bit() is atomic and may not be reordered. |
| 182 | * Note that @nr may be almost arbitrarily large; this function is not |
| 183 | * restricted to acting on a single-word quantity. |
| 184 | */ |
H. Peter Anvin | 9b71050 | 2013-07-16 15:20:14 -0700 | [diff] [blame] | 185 | static inline void change_bit(long nr, volatile unsigned long *addr) |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 186 | { |
Uros Bizjak | 838e8bb | 2008-10-24 16:53:33 +0200 | [diff] [blame] | 187 | if (IS_IMMEDIATE(nr)) { |
| 188 | asm volatile(LOCK_PREFIX "xorb %1,%0" |
| 189 | : CONST_MASK_ADDR(nr, addr) |
| 190 | : "iq" ((u8)CONST_MASK(nr))); |
| 191 | } else { |
| 192 | asm volatile(LOCK_PREFIX "btc %1,%0" |
| 193 | : BITOP_ADDR(addr) |
| 194 | : "Ir" (nr)); |
| 195 | } |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /** |
| 199 | * test_and_set_bit - Set a bit and return its old value |
| 200 | * @nr: Bit to set |
| 201 | * @addr: Address to count from |
| 202 | * |
| 203 | * This operation is atomic and cannot be reordered. |
| 204 | * It also implies a memory barrier. |
| 205 | */ |
H. Peter Anvin | 9b71050 | 2013-07-16 15:20:14 -0700 | [diff] [blame] | 206 | static inline int test_and_set_bit(long nr, volatile unsigned long *addr) |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 207 | { |
Peter Zijlstra | 0c44c2d | 2013-09-11 15:19:24 +0200 | [diff] [blame^] | 208 | GEN_BINARY_RMWcc(LOCK_PREFIX "bts", *addr, nr, "%0", "c"); |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /** |
| 212 | * test_and_set_bit_lock - Set a bit and return its old value for lock |
| 213 | * @nr: Bit to set |
| 214 | * @addr: Address to count from |
| 215 | * |
| 216 | * This is the same as test_and_set_bit on x86. |
| 217 | */ |
Andi Kleen | c8399943 | 2009-01-12 23:01:15 +0100 | [diff] [blame] | 218 | static __always_inline int |
H. Peter Anvin | 9b71050 | 2013-07-16 15:20:14 -0700 | [diff] [blame] | 219 | test_and_set_bit_lock(long nr, volatile unsigned long *addr) |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 220 | { |
| 221 | return test_and_set_bit(nr, addr); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * __test_and_set_bit - Set a bit and return its old value |
| 226 | * @nr: Bit to set |
| 227 | * @addr: Address to count from |
| 228 | * |
| 229 | * This operation is non-atomic and can be reordered. |
| 230 | * If two examples of this operation race, one can appear to succeed |
| 231 | * but actually fail. You must protect multiple accesses with a lock. |
| 232 | */ |
H. Peter Anvin | 9b71050 | 2013-07-16 15:20:14 -0700 | [diff] [blame] | 233 | static inline int __test_and_set_bit(long nr, volatile unsigned long *addr) |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 234 | { |
| 235 | int oldbit; |
| 236 | |
Simon Holm Thøgersen | eb2b4e6 | 2008-05-05 15:45:28 +0200 | [diff] [blame] | 237 | asm("bts %2,%1\n\t" |
| 238 | "sbb %0,%0" |
| 239 | : "=r" (oldbit), ADDR |
| 240 | : "Ir" (nr)); |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 241 | return oldbit; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * test_and_clear_bit - Clear a bit and return its old value |
| 246 | * @nr: Bit to clear |
| 247 | * @addr: Address to count from |
| 248 | * |
| 249 | * This operation is atomic and cannot be reordered. |
| 250 | * It also implies a memory barrier. |
| 251 | */ |
H. Peter Anvin | 9b71050 | 2013-07-16 15:20:14 -0700 | [diff] [blame] | 252 | static inline int test_and_clear_bit(long nr, volatile unsigned long *addr) |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 253 | { |
Peter Zijlstra | 0c44c2d | 2013-09-11 15:19:24 +0200 | [diff] [blame^] | 254 | GEN_BINARY_RMWcc(LOCK_PREFIX "btr", *addr, nr, "%0", "c"); |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /** |
| 258 | * __test_and_clear_bit - Clear a bit and return its old value |
| 259 | * @nr: Bit to clear |
| 260 | * @addr: Address to count from |
| 261 | * |
| 262 | * This operation is non-atomic and can be reordered. |
| 263 | * If two examples of this operation race, one can appear to succeed |
| 264 | * but actually fail. You must protect multiple accesses with a lock. |
Michael S. Tsirkin | d0a69d6 | 2012-06-24 19:24:42 +0300 | [diff] [blame] | 265 | * |
| 266 | * Note: the operation is performed atomically with respect to |
| 267 | * the local CPU, but not other CPUs. Portable code should not |
| 268 | * rely on this behaviour. |
| 269 | * KVM relies on this behaviour on x86 for modifying memory that is also |
| 270 | * accessed from a hypervisor on the same CPU if running in a VM: don't change |
| 271 | * this without also updating arch/x86/kernel/kvm.c |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 272 | */ |
H. Peter Anvin | 9b71050 | 2013-07-16 15:20:14 -0700 | [diff] [blame] | 273 | static inline int __test_and_clear_bit(long nr, volatile unsigned long *addr) |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 274 | { |
| 275 | int oldbit; |
| 276 | |
Simon Holm Thøgersen | eb2b4e6 | 2008-05-05 15:45:28 +0200 | [diff] [blame] | 277 | asm volatile("btr %2,%1\n\t" |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 278 | "sbb %0,%0" |
Simon Holm Thøgersen | eb2b4e6 | 2008-05-05 15:45:28 +0200 | [diff] [blame] | 279 | : "=r" (oldbit), ADDR |
| 280 | : "Ir" (nr)); |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 281 | return oldbit; |
| 282 | } |
| 283 | |
| 284 | /* WARNING: non atomic and it can be reordered! */ |
H. Peter Anvin | 9b71050 | 2013-07-16 15:20:14 -0700 | [diff] [blame] | 285 | static inline int __test_and_change_bit(long nr, volatile unsigned long *addr) |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 286 | { |
| 287 | int oldbit; |
| 288 | |
Simon Holm Thøgersen | eb2b4e6 | 2008-05-05 15:45:28 +0200 | [diff] [blame] | 289 | asm volatile("btc %2,%1\n\t" |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 290 | "sbb %0,%0" |
Simon Holm Thøgersen | eb2b4e6 | 2008-05-05 15:45:28 +0200 | [diff] [blame] | 291 | : "=r" (oldbit), ADDR |
| 292 | : "Ir" (nr) : "memory"); |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 293 | |
| 294 | return oldbit; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * test_and_change_bit - Change a bit and return its old value |
| 299 | * @nr: Bit to change |
| 300 | * @addr: Address to count from |
| 301 | * |
| 302 | * This operation is atomic and cannot be reordered. |
| 303 | * It also implies a memory barrier. |
| 304 | */ |
H. Peter Anvin | 9b71050 | 2013-07-16 15:20:14 -0700 | [diff] [blame] | 305 | static inline int test_and_change_bit(long nr, volatile unsigned long *addr) |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 306 | { |
Peter Zijlstra | 0c44c2d | 2013-09-11 15:19:24 +0200 | [diff] [blame^] | 307 | GEN_BINARY_RMWcc(LOCK_PREFIX "btc", *addr, nr, "%0", "c"); |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 308 | } |
| 309 | |
H. Peter Anvin | 9b71050 | 2013-07-16 15:20:14 -0700 | [diff] [blame] | 310 | static __always_inline int constant_test_bit(long nr, const volatile unsigned long *addr) |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 311 | { |
H. Peter Anvin | 9b71050 | 2013-07-16 15:20:14 -0700 | [diff] [blame] | 312 | return ((1UL << (nr & (BITS_PER_LONG-1))) & |
| 313 | (addr[nr >> _BITOPS_LONG_SHIFT])) != 0; |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 314 | } |
| 315 | |
H. Peter Anvin | 9b71050 | 2013-07-16 15:20:14 -0700 | [diff] [blame] | 316 | static inline int variable_test_bit(long nr, volatile const unsigned long *addr) |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 317 | { |
| 318 | int oldbit; |
| 319 | |
Simon Holm Thøgersen | eb2b4e6 | 2008-05-05 15:45:28 +0200 | [diff] [blame] | 320 | asm volatile("bt %2,%1\n\t" |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 321 | "sbb %0,%0" |
| 322 | : "=r" (oldbit) |
Simon Holm Thøgersen | eb2b4e6 | 2008-05-05 15:45:28 +0200 | [diff] [blame] | 323 | : "m" (*(unsigned long *)addr), "Ir" (nr)); |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 324 | |
| 325 | return oldbit; |
| 326 | } |
| 327 | |
| 328 | #if 0 /* Fool kernel-doc since it doesn't do macros yet */ |
| 329 | /** |
| 330 | * test_bit - Determine whether a bit is set |
| 331 | * @nr: bit number to test |
| 332 | * @addr: Address to start counting from |
| 333 | */ |
| 334 | static int test_bit(int nr, const volatile unsigned long *addr); |
| 335 | #endif |
| 336 | |
Joe Perches | f19dcf4 | 2008-03-23 01:03:07 -0700 | [diff] [blame] | 337 | #define test_bit(nr, addr) \ |
| 338 | (__builtin_constant_p((nr)) \ |
| 339 | ? constant_test_bit((nr), (addr)) \ |
| 340 | : variable_test_bit((nr), (addr))) |
Jeremy Fitzhardinge | 1c54d77 | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 341 | |
Alexander van Heukelum | 12d9c84 | 2008-03-15 13:04:42 +0100 | [diff] [blame] | 342 | /** |
| 343 | * __ffs - find first set bit in word |
| 344 | * @word: The word to search |
| 345 | * |
| 346 | * Undefined if no bit exists, so code should check against 0 first. |
| 347 | */ |
| 348 | static inline unsigned long __ffs(unsigned long word) |
| 349 | { |
Jan Beulich | e26a44a | 2012-09-18 12:16:14 +0100 | [diff] [blame] | 350 | asm("rep; bsf %1,%0" |
Joe Perches | f19dcf4 | 2008-03-23 01:03:07 -0700 | [diff] [blame] | 351 | : "=r" (word) |
| 352 | : "rm" (word)); |
Alexander van Heukelum | 12d9c84 | 2008-03-15 13:04:42 +0100 | [diff] [blame] | 353 | return word; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * ffz - find first zero bit in word |
| 358 | * @word: The word to search |
| 359 | * |
| 360 | * Undefined if no zero exists, so code should check against ~0UL first. |
| 361 | */ |
| 362 | static inline unsigned long ffz(unsigned long word) |
| 363 | { |
Jan Beulich | e26a44a | 2012-09-18 12:16:14 +0100 | [diff] [blame] | 364 | asm("rep; bsf %1,%0" |
Joe Perches | f19dcf4 | 2008-03-23 01:03:07 -0700 | [diff] [blame] | 365 | : "=r" (word) |
| 366 | : "r" (~word)); |
Alexander van Heukelum | 12d9c84 | 2008-03-15 13:04:42 +0100 | [diff] [blame] | 367 | return word; |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * __fls: find last set bit in word |
| 372 | * @word: The word to search |
| 373 | * |
Alexander van Heukelum | 8450e85 | 2008-07-05 19:53:46 +0200 | [diff] [blame] | 374 | * Undefined if no set bit exists, so code should check against 0 first. |
Alexander van Heukelum | 12d9c84 | 2008-03-15 13:04:42 +0100 | [diff] [blame] | 375 | */ |
| 376 | static inline unsigned long __fls(unsigned long word) |
| 377 | { |
Joe Perches | f19dcf4 | 2008-03-23 01:03:07 -0700 | [diff] [blame] | 378 | asm("bsr %1,%0" |
| 379 | : "=r" (word) |
| 380 | : "rm" (word)); |
Alexander van Heukelum | 12d9c84 | 2008-03-15 13:04:42 +0100 | [diff] [blame] | 381 | return word; |
| 382 | } |
| 383 | |
H. Peter Anvin | 83d99df | 2011-12-15 14:55:53 -0800 | [diff] [blame] | 384 | #undef ADDR |
| 385 | |
Alexander van Heukelum | 12d9c84 | 2008-03-15 13:04:42 +0100 | [diff] [blame] | 386 | #ifdef __KERNEL__ |
| 387 | /** |
| 388 | * ffs - find first set bit in word |
| 389 | * @x: the word to search |
| 390 | * |
| 391 | * This is defined the same way as the libc and compiler builtin ffs |
| 392 | * routines, therefore differs in spirit from the other bitops. |
| 393 | * |
| 394 | * ffs(value) returns 0 if value is 0 or the position of the first |
| 395 | * set bit if value is nonzero. The first (least significant) bit |
| 396 | * is at position 1. |
| 397 | */ |
| 398 | static inline int ffs(int x) |
| 399 | { |
| 400 | int r; |
David Howells | ca3d30c | 2011-12-13 14:56:54 +0000 | [diff] [blame] | 401 | |
| 402 | #ifdef CONFIG_X86_64 |
| 403 | /* |
| 404 | * AMD64 says BSFL won't clobber the dest reg if x==0; Intel64 says the |
| 405 | * dest reg is undefined if x==0, but their CPU architect says its |
| 406 | * value is written to set it to the same as before, except that the |
| 407 | * top 32 bits will be cleared. |
| 408 | * |
| 409 | * We cannot do this on 32 bits because at the very least some |
| 410 | * 486 CPUs did not behave this way. |
| 411 | */ |
David Howells | ca3d30c | 2011-12-13 14:56:54 +0000 | [diff] [blame] | 412 | asm("bsfl %1,%0" |
| 413 | : "=r" (r) |
Jan Beulich | 1edfbb4 | 2012-09-10 12:04:16 +0100 | [diff] [blame] | 414 | : "rm" (x), "0" (-1)); |
David Howells | ca3d30c | 2011-12-13 14:56:54 +0000 | [diff] [blame] | 415 | #elif defined(CONFIG_X86_CMOV) |
Joe Perches | f19dcf4 | 2008-03-23 01:03:07 -0700 | [diff] [blame] | 416 | asm("bsfl %1,%0\n\t" |
| 417 | "cmovzl %2,%0" |
David Howells | ca3d30c | 2011-12-13 14:56:54 +0000 | [diff] [blame] | 418 | : "=&r" (r) : "rm" (x), "r" (-1)); |
Alexander van Heukelum | 12d9c84 | 2008-03-15 13:04:42 +0100 | [diff] [blame] | 419 | #else |
Joe Perches | f19dcf4 | 2008-03-23 01:03:07 -0700 | [diff] [blame] | 420 | asm("bsfl %1,%0\n\t" |
| 421 | "jnz 1f\n\t" |
| 422 | "movl $-1,%0\n" |
| 423 | "1:" : "=r" (r) : "rm" (x)); |
Alexander van Heukelum | 12d9c84 | 2008-03-15 13:04:42 +0100 | [diff] [blame] | 424 | #endif |
| 425 | return r + 1; |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * fls - find last set bit in word |
| 430 | * @x: the word to search |
| 431 | * |
| 432 | * This is defined in a similar way as the libc and compiler builtin |
| 433 | * ffs, but returns the position of the most significant set bit. |
| 434 | * |
| 435 | * fls(value) returns 0 if value is 0 or the position of the last |
| 436 | * set bit if value is nonzero. The last (most significant) bit is |
| 437 | * at position 32. |
| 438 | */ |
| 439 | static inline int fls(int x) |
| 440 | { |
| 441 | int r; |
David Howells | ca3d30c | 2011-12-13 14:56:54 +0000 | [diff] [blame] | 442 | |
| 443 | #ifdef CONFIG_X86_64 |
| 444 | /* |
| 445 | * AMD64 says BSRL won't clobber the dest reg if x==0; Intel64 says the |
| 446 | * dest reg is undefined if x==0, but their CPU architect says its |
| 447 | * value is written to set it to the same as before, except that the |
| 448 | * top 32 bits will be cleared. |
| 449 | * |
| 450 | * We cannot do this on 32 bits because at the very least some |
| 451 | * 486 CPUs did not behave this way. |
| 452 | */ |
David Howells | ca3d30c | 2011-12-13 14:56:54 +0000 | [diff] [blame] | 453 | asm("bsrl %1,%0" |
| 454 | : "=r" (r) |
Jan Beulich | 1edfbb4 | 2012-09-10 12:04:16 +0100 | [diff] [blame] | 455 | : "rm" (x), "0" (-1)); |
David Howells | ca3d30c | 2011-12-13 14:56:54 +0000 | [diff] [blame] | 456 | #elif defined(CONFIG_X86_CMOV) |
Joe Perches | f19dcf4 | 2008-03-23 01:03:07 -0700 | [diff] [blame] | 457 | asm("bsrl %1,%0\n\t" |
| 458 | "cmovzl %2,%0" |
| 459 | : "=&r" (r) : "rm" (x), "rm" (-1)); |
Alexander van Heukelum | 12d9c84 | 2008-03-15 13:04:42 +0100 | [diff] [blame] | 460 | #else |
Joe Perches | f19dcf4 | 2008-03-23 01:03:07 -0700 | [diff] [blame] | 461 | asm("bsrl %1,%0\n\t" |
| 462 | "jnz 1f\n\t" |
| 463 | "movl $-1,%0\n" |
| 464 | "1:" : "=r" (r) : "rm" (x)); |
Alexander van Heukelum | 12d9c84 | 2008-03-15 13:04:42 +0100 | [diff] [blame] | 465 | #endif |
| 466 | return r + 1; |
| 467 | } |
Alexander van Heukelum | d66462f | 2008-04-04 20:49:30 +0200 | [diff] [blame] | 468 | |
David Howells | ca3d30c | 2011-12-13 14:56:54 +0000 | [diff] [blame] | 469 | /** |
| 470 | * fls64 - find last set bit in a 64-bit word |
| 471 | * @x: the word to search |
| 472 | * |
| 473 | * This is defined in a similar way as the libc and compiler builtin |
| 474 | * ffsll, but returns the position of the most significant set bit. |
| 475 | * |
| 476 | * fls64(value) returns 0 if value is 0 or the position of the last |
| 477 | * set bit if value is nonzero. The last (most significant) bit is |
| 478 | * at position 64. |
| 479 | */ |
| 480 | #ifdef CONFIG_X86_64 |
| 481 | static __always_inline int fls64(__u64 x) |
| 482 | { |
Jan Beulich | 1edfbb4 | 2012-09-10 12:04:16 +0100 | [diff] [blame] | 483 | int bitpos = -1; |
David Howells | ca3d30c | 2011-12-13 14:56:54 +0000 | [diff] [blame] | 484 | /* |
| 485 | * AMD64 says BSRQ won't clobber the dest reg if x==0; Intel64 says the |
| 486 | * dest reg is undefined if x==0, but their CPU architect says its |
| 487 | * value is written to set it to the same as before. |
| 488 | */ |
Jan Beulich | 1edfbb4 | 2012-09-10 12:04:16 +0100 | [diff] [blame] | 489 | asm("bsrq %1,%q0" |
David Howells | ca3d30c | 2011-12-13 14:56:54 +0000 | [diff] [blame] | 490 | : "+r" (bitpos) |
| 491 | : "rm" (x)); |
| 492 | return bitpos + 1; |
| 493 | } |
| 494 | #else |
| 495 | #include <asm-generic/bitops/fls64.h> |
| 496 | #endif |
| 497 | |
Akinobu Mita | 708ff2a | 2010-09-29 18:08:50 +0900 | [diff] [blame] | 498 | #include <asm-generic/bitops/find.h> |
| 499 | |
Alexander van Heukelum | d66462f | 2008-04-04 20:49:30 +0200 | [diff] [blame] | 500 | #include <asm-generic/bitops/sched.h> |
| 501 | |
| 502 | #define ARCH_HAS_FAST_MULTIPLIER 1 |
| 503 | |
Borislav Petkov | d61931d | 2010-03-05 17:34:46 +0100 | [diff] [blame] | 504 | #include <asm/arch_hweight.h> |
| 505 | |
| 506 | #include <asm-generic/bitops/const_hweight.h> |
Alexander van Heukelum | d66462f | 2008-04-04 20:49:30 +0200 | [diff] [blame] | 507 | |
Akinobu Mita | 861b5ae | 2011-03-23 16:42:02 -0700 | [diff] [blame] | 508 | #include <asm-generic/bitops/le.h> |
Alexander van Heukelum | d66462f | 2008-04-04 20:49:30 +0200 | [diff] [blame] | 509 | |
Akinobu Mita | 148817b | 2011-07-26 16:09:04 -0700 | [diff] [blame] | 510 | #include <asm-generic/bitops/ext2-atomic-setbit.h> |
Alexander van Heukelum | d66462f | 2008-04-04 20:49:30 +0200 | [diff] [blame] | 511 | |
Alexander van Heukelum | d66462f | 2008-04-04 20:49:30 +0200 | [diff] [blame] | 512 | #endif /* __KERNEL__ */ |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 513 | #endif /* _ASM_X86_BITOPS_H */ |