wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1995, Russell King. |
| 3 | * Various bits and pieces copyrights include: |
| 4 | * Linus Torvalds (test_bit). |
| 5 | * |
| 6 | * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1). |
| 7 | * |
| 8 | * Please note that the code in this file should never be included |
| 9 | * from user space. Many of these are not implemented in assembler |
| 10 | * since they would be too costly. Also, they require priviledged |
| 11 | * instructions (which are not available from user mode) to ensure |
| 12 | * that they are atomic. |
| 13 | */ |
| 14 | |
| 15 | #ifndef __ASM_ARM_BITOPS_H |
| 16 | #define __ASM_ARM_BITOPS_H |
| 17 | |
| 18 | #ifdef __KERNEL__ |
| 19 | |
Vasili Galka | 8e53b1e | 2014-06-10 16:16:14 +0300 | [diff] [blame] | 20 | #include <asm/proc-armv/system.h> |
Simon Kagstrom | 209a72e | 2009-08-24 09:10:16 +0200 | [diff] [blame] | 21 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 22 | #define smp_mb__before_clear_bit() do { } while (0) |
| 23 | #define smp_mb__after_clear_bit() do { } while (0) |
| 24 | |
| 25 | /* |
| 26 | * Function prototypes to keep gcc -Wall happy. |
| 27 | */ |
| 28 | extern void set_bit(int nr, volatile void * addr); |
| 29 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 30 | extern void clear_bit(int nr, volatile void * addr); |
| 31 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 32 | extern void change_bit(int nr, volatile void * addr); |
| 33 | |
| 34 | static inline void __change_bit(int nr, volatile void *addr) |
| 35 | { |
Simon Kagstrom | 6fc5822 | 2009-08-24 09:10:03 +0200 | [diff] [blame] | 36 | unsigned long mask = BIT_MASK(nr); |
| 37 | unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); |
| 38 | |
| 39 | *p ^= mask; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 40 | } |
| 41 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 42 | static inline int __test_and_set_bit(int nr, volatile void *addr) |
| 43 | { |
Simon Kagstrom | 6fc5822 | 2009-08-24 09:10:03 +0200 | [diff] [blame] | 44 | unsigned long mask = BIT_MASK(nr); |
| 45 | unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); |
| 46 | unsigned long old = *p; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 47 | |
Simon Kagstrom | 6fc5822 | 2009-08-24 09:10:03 +0200 | [diff] [blame] | 48 | *p = old | mask; |
| 49 | return (old & mask) != 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Simon Kagstrom | 209a72e | 2009-08-24 09:10:16 +0200 | [diff] [blame] | 52 | static inline int test_and_set_bit(int nr, volatile void * addr) |
| 53 | { |
Heiko Schocher | 3c6392f | 2015-11-30 08:47:42 +0100 | [diff] [blame] | 54 | unsigned long flags = 0; |
Simon Kagstrom | 209a72e | 2009-08-24 09:10:16 +0200 | [diff] [blame] | 55 | int out; |
| 56 | |
| 57 | local_irq_save(flags); |
| 58 | out = __test_and_set_bit(nr, addr); |
| 59 | local_irq_restore(flags); |
| 60 | |
| 61 | return out; |
| 62 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 63 | |
| 64 | static inline int __test_and_clear_bit(int nr, volatile void *addr) |
| 65 | { |
Simon Kagstrom | 6fc5822 | 2009-08-24 09:10:03 +0200 | [diff] [blame] | 66 | unsigned long mask = BIT_MASK(nr); |
| 67 | unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); |
| 68 | unsigned long old = *p; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 69 | |
Simon Kagstrom | 6fc5822 | 2009-08-24 09:10:03 +0200 | [diff] [blame] | 70 | *p = old & ~mask; |
| 71 | return (old & mask) != 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Simon Kagstrom | 209a72e | 2009-08-24 09:10:16 +0200 | [diff] [blame] | 74 | static inline int test_and_clear_bit(int nr, volatile void * addr) |
| 75 | { |
Heiko Schocher | 3c6392f | 2015-11-30 08:47:42 +0100 | [diff] [blame] | 76 | unsigned long flags = 0; |
Simon Kagstrom | 209a72e | 2009-08-24 09:10:16 +0200 | [diff] [blame] | 77 | int out; |
| 78 | |
| 79 | local_irq_save(flags); |
| 80 | out = __test_and_clear_bit(nr, addr); |
| 81 | local_irq_restore(flags); |
| 82 | |
| 83 | return out; |
| 84 | } |
| 85 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 86 | extern int test_and_change_bit(int nr, volatile void * addr); |
| 87 | |
| 88 | static inline int __test_and_change_bit(int nr, volatile void *addr) |
| 89 | { |
Simon Kagstrom | 6fc5822 | 2009-08-24 09:10:03 +0200 | [diff] [blame] | 90 | unsigned long mask = BIT_MASK(nr); |
| 91 | unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); |
| 92 | unsigned long old = *p; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 93 | |
Simon Kagstrom | 6fc5822 | 2009-08-24 09:10:03 +0200 | [diff] [blame] | 94 | *p = old ^ mask; |
| 95 | return (old & mask) != 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 96 | } |
| 97 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 98 | /* |
| 99 | * This routine doesn't need to be atomic. |
| 100 | */ |
| 101 | static inline int test_bit(int nr, const void * addr) |
| 102 | { |
| 103 | return ((unsigned char *) addr)[nr >> 3] & (1U << (nr & 7)); |
| 104 | } |
| 105 | |
Rob Herring | a6357cb | 2011-07-05 04:38:51 +0000 | [diff] [blame] | 106 | static inline int __ilog2(unsigned int x) |
| 107 | { |
| 108 | return generic_fls(x) - 1; |
| 109 | } |
| 110 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 111 | /* |
| 112 | * ffz = Find First Zero in word. Undefined if no zero exists, |
| 113 | * so code should check against ~0UL first.. |
| 114 | */ |
| 115 | static inline unsigned long ffz(unsigned long word) |
| 116 | { |
| 117 | int k; |
| 118 | |
| 119 | word = ~word; |
| 120 | k = 31; |
| 121 | if (word & 0x0000ffff) { k -= 16; word <<= 16; } |
| 122 | if (word & 0x00ff0000) { k -= 8; word <<= 8; } |
| 123 | if (word & 0x0f000000) { k -= 4; word <<= 4; } |
| 124 | if (word & 0x30000000) { k -= 2; word <<= 2; } |
| 125 | if (word & 0x40000000) { k -= 1; } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 126 | return k; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Vitaly Andrianov | 3bd82c5 | 2015-02-05 11:24:46 -0500 | [diff] [blame] | 129 | static inline int find_next_zero_bit(void *addr, int size, int offset) |
| 130 | { |
| 131 | unsigned long *p = ((unsigned long *)addr) + (offset >> 5); |
| 132 | unsigned long result = offset & ~31UL; |
| 133 | unsigned long tmp; |
| 134 | |
| 135 | if (offset >= size) |
| 136 | return size; |
| 137 | size -= result; |
| 138 | offset &= 31UL; |
| 139 | if (offset) { |
| 140 | tmp = *(p++); |
| 141 | tmp |= ~0UL >> (32-offset); |
| 142 | if (size < 32) |
| 143 | goto found_first; |
| 144 | if (~tmp) |
| 145 | goto found_middle; |
| 146 | size -= 32; |
| 147 | result += 32; |
| 148 | } |
| 149 | while (size & ~31UL) { |
| 150 | tmp = *(p++); |
| 151 | if (~tmp) |
| 152 | goto found_middle; |
| 153 | result += 32; |
| 154 | size -= 32; |
| 155 | } |
| 156 | if (!size) |
| 157 | return result; |
| 158 | tmp = *p; |
| 159 | |
| 160 | found_first: |
Grygorii Strashko | cdcda67 | 2018-04-27 19:58:49 -0500 | [diff] [blame] | 161 | tmp |= ~0UL << size; |
Vitaly Andrianov | 3bd82c5 | 2015-02-05 11:24:46 -0500 | [diff] [blame] | 162 | found_middle: |
| 163 | return result + ffz(tmp); |
| 164 | } |
| 165 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 166 | /* |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 167 | * hweightN: returns the hamming weight (i.e. the number |
| 168 | * of bits set) of a N-bit word |
| 169 | */ |
| 170 | |
| 171 | #define hweight32(x) generic_hweight32(x) |
| 172 | #define hweight16(x) generic_hweight16(x) |
| 173 | #define hweight8(x) generic_hweight8(x) |
| 174 | |
Vitaly Andrianov | 3bd82c5 | 2015-02-05 11:24:46 -0500 | [diff] [blame] | 175 | #define find_first_zero_bit(addr, size) \ |
| 176 | find_next_zero_bit((addr), (size), 0) |
| 177 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 178 | #define ext2_set_bit test_and_set_bit |
| 179 | #define ext2_clear_bit test_and_clear_bit |
| 180 | #define ext2_test_bit test_bit |
| 181 | #define ext2_find_first_zero_bit find_first_zero_bit |
| 182 | #define ext2_find_next_zero_bit find_next_zero_bit |
| 183 | |
| 184 | /* Bitmap functions for the minix filesystem. */ |
| 185 | #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr) |
| 186 | #define minix_set_bit(nr,addr) set_bit(nr,addr) |
| 187 | #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr) |
| 188 | #define minix_test_bit(nr,addr) test_bit(nr,addr) |
| 189 | #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size) |
| 190 | |
| 191 | #endif /* __KERNEL__ */ |
| 192 | |
Fabio Estevam | d24ab14 | 2015-11-05 12:43:24 -0200 | [diff] [blame] | 193 | #include <asm-generic/bitops/__fls.h> |
| 194 | #include <asm-generic/bitops/__ffs.h> |
| 195 | #include <asm-generic/bitops/fls.h> |
| 196 | #include <asm-generic/bitops/fls64.h> |
| 197 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 198 | #endif /* _ARM_BITOPS_H */ |