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 | |
Sean Anderson | 6babd84 | 2023-07-31 17:27:33 -0400 | [diff] [blame] | 18 | #if __LINUX_ARM_ARCH__ < 5 |
| 19 | |
Grygorii Strashko | ced66ce | 2018-06-28 14:25:52 -0500 | [diff] [blame] | 20 | #include <asm-generic/bitops/__ffs.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 21 | #include <asm-generic/bitops/__fls.h> |
| 22 | #include <asm-generic/bitops/fls.h> |
Sean Anderson | 6babd84 | 2023-07-31 17:27:33 -0400 | [diff] [blame] | 23 | |
| 24 | #else |
| 25 | |
| 26 | #define PLATFORM_FFS |
| 27 | #define PLATFORM_FLS |
| 28 | |
| 29 | #if !IS_ENABLED(CONFIG_HAS_THUMB2) && CONFIG_IS_ENABLED(SYS_THUMB_BUILD) |
| 30 | |
| 31 | unsigned long __fls(unsigned long word); |
| 32 | unsigned long __ffs(unsigned long word); |
| 33 | int fls(unsigned int x); |
| 34 | int ffs(int x); |
| 35 | |
| 36 | #else |
| 37 | |
| 38 | #include <asm-generic/bitops/builtin-__fls.h> |
| 39 | #include <asm-generic/bitops/builtin-__ffs.h> |
| 40 | #include <asm-generic/bitops/builtin-fls.h> |
| 41 | #include <asm-generic/bitops/builtin-ffs.h> |
| 42 | |
| 43 | #endif |
| 44 | #endif |
| 45 | |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 46 | #include <asm-generic/bitops/fls64.h> |
Grygorii Strashko | ced66ce | 2018-06-28 14:25:52 -0500 | [diff] [blame] | 47 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 48 | #ifdef __KERNEL__ |
| 49 | |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 50 | #ifndef __ASSEMBLY__ |
| 51 | #include <linux/bitops.h> |
| 52 | #endif |
Vasili Galka | 8e53b1e | 2014-06-10 16:16:14 +0300 | [diff] [blame] | 53 | #include <asm/proc-armv/system.h> |
Simon Kagstrom | 209a72e | 2009-08-24 09:10:16 +0200 | [diff] [blame] | 54 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 55 | #define smp_mb__before_clear_bit() do { } while (0) |
| 56 | #define smp_mb__after_clear_bit() do { } while (0) |
| 57 | |
| 58 | /* |
| 59 | * Function prototypes to keep gcc -Wall happy. |
| 60 | */ |
| 61 | extern void set_bit(int nr, volatile void * addr); |
| 62 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 63 | extern void clear_bit(int nr, volatile void * addr); |
| 64 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 65 | extern void change_bit(int nr, volatile void * addr); |
| 66 | |
| 67 | static inline void __change_bit(int nr, volatile void *addr) |
| 68 | { |
Simon Kagstrom | 6fc5822 | 2009-08-24 09:10:03 +0200 | [diff] [blame] | 69 | unsigned long mask = BIT_MASK(nr); |
| 70 | unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); |
| 71 | |
| 72 | *p ^= mask; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 73 | } |
| 74 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 75 | static inline int __test_and_set_bit(int nr, volatile void *addr) |
| 76 | { |
Simon Kagstrom | 6fc5822 | 2009-08-24 09:10:03 +0200 | [diff] [blame] | 77 | unsigned long mask = BIT_MASK(nr); |
| 78 | unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); |
| 79 | unsigned long old = *p; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 80 | |
Simon Kagstrom | 6fc5822 | 2009-08-24 09:10:03 +0200 | [diff] [blame] | 81 | *p = old | mask; |
| 82 | return (old & mask) != 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Simon Kagstrom | 209a72e | 2009-08-24 09:10:16 +0200 | [diff] [blame] | 85 | static inline int test_and_set_bit(int nr, volatile void * addr) |
| 86 | { |
Heiko Schocher | 3c6392f | 2015-11-30 08:47:42 +0100 | [diff] [blame] | 87 | unsigned long flags = 0; |
Simon Kagstrom | 209a72e | 2009-08-24 09:10:16 +0200 | [diff] [blame] | 88 | int out; |
| 89 | |
| 90 | local_irq_save(flags); |
| 91 | out = __test_and_set_bit(nr, addr); |
| 92 | local_irq_restore(flags); |
| 93 | |
| 94 | return out; |
| 95 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 96 | |
| 97 | static inline int __test_and_clear_bit(int nr, volatile void *addr) |
| 98 | { |
Simon Kagstrom | 6fc5822 | 2009-08-24 09:10:03 +0200 | [diff] [blame] | 99 | unsigned long mask = BIT_MASK(nr); |
| 100 | unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); |
| 101 | unsigned long old = *p; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 102 | |
Simon Kagstrom | 6fc5822 | 2009-08-24 09:10:03 +0200 | [diff] [blame] | 103 | *p = old & ~mask; |
| 104 | return (old & mask) != 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Simon Kagstrom | 209a72e | 2009-08-24 09:10:16 +0200 | [diff] [blame] | 107 | static inline int test_and_clear_bit(int nr, volatile void * addr) |
| 108 | { |
Heiko Schocher | 3c6392f | 2015-11-30 08:47:42 +0100 | [diff] [blame] | 109 | unsigned long flags = 0; |
Simon Kagstrom | 209a72e | 2009-08-24 09:10:16 +0200 | [diff] [blame] | 110 | int out; |
| 111 | |
| 112 | local_irq_save(flags); |
| 113 | out = __test_and_clear_bit(nr, addr); |
| 114 | local_irq_restore(flags); |
| 115 | |
| 116 | return out; |
| 117 | } |
| 118 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 119 | extern int test_and_change_bit(int nr, volatile void * addr); |
| 120 | |
| 121 | static inline int __test_and_change_bit(int nr, volatile void *addr) |
| 122 | { |
Simon Kagstrom | 6fc5822 | 2009-08-24 09:10:03 +0200 | [diff] [blame] | 123 | unsigned long mask = BIT_MASK(nr); |
| 124 | unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); |
| 125 | unsigned long old = *p; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 126 | |
Simon Kagstrom | 6fc5822 | 2009-08-24 09:10:03 +0200 | [diff] [blame] | 127 | *p = old ^ mask; |
| 128 | return (old & mask) != 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 129 | } |
| 130 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 131 | /* |
| 132 | * This routine doesn't need to be atomic. |
| 133 | */ |
| 134 | static inline int test_bit(int nr, const void * addr) |
| 135 | { |
| 136 | return ((unsigned char *) addr)[nr >> 3] & (1U << (nr & 7)); |
| 137 | } |
| 138 | |
Rob Herring | a6357cb | 2011-07-05 04:38:51 +0000 | [diff] [blame] | 139 | static inline int __ilog2(unsigned int x) |
| 140 | { |
Sean Anderson | 6babd84 | 2023-07-31 17:27:33 -0400 | [diff] [blame] | 141 | return fls(x) - 1; |
Rob Herring | a6357cb | 2011-07-05 04:38:51 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Grygorii Strashko | ced66ce | 2018-06-28 14:25:52 -0500 | [diff] [blame] | 144 | #define ffz(x) __ffs(~(x)) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 145 | |
Vitaly Andrianov | 3bd82c5 | 2015-02-05 11:24:46 -0500 | [diff] [blame] | 146 | static inline int find_next_zero_bit(void *addr, int size, int offset) |
| 147 | { |
Grygorii Strashko | ced66ce | 2018-06-28 14:25:52 -0500 | [diff] [blame] | 148 | unsigned long *p = ((unsigned long *)addr) + (offset / BITS_PER_LONG); |
| 149 | unsigned long result = offset & ~(BITS_PER_LONG - 1); |
Vitaly Andrianov | 3bd82c5 | 2015-02-05 11:24:46 -0500 | [diff] [blame] | 150 | unsigned long tmp; |
| 151 | |
| 152 | if (offset >= size) |
| 153 | return size; |
| 154 | size -= result; |
Grygorii Strashko | ced66ce | 2018-06-28 14:25:52 -0500 | [diff] [blame] | 155 | offset &= (BITS_PER_LONG - 1); |
Vitaly Andrianov | 3bd82c5 | 2015-02-05 11:24:46 -0500 | [diff] [blame] | 156 | if (offset) { |
| 157 | tmp = *(p++); |
Grygorii Strashko | ced66ce | 2018-06-28 14:25:52 -0500 | [diff] [blame] | 158 | tmp |= ~0UL >> (BITS_PER_LONG - offset); |
| 159 | if (size < BITS_PER_LONG) |
Vitaly Andrianov | 3bd82c5 | 2015-02-05 11:24:46 -0500 | [diff] [blame] | 160 | goto found_first; |
| 161 | if (~tmp) |
| 162 | goto found_middle; |
Grygorii Strashko | ced66ce | 2018-06-28 14:25:52 -0500 | [diff] [blame] | 163 | size -= BITS_PER_LONG; |
| 164 | result += BITS_PER_LONG; |
Vitaly Andrianov | 3bd82c5 | 2015-02-05 11:24:46 -0500 | [diff] [blame] | 165 | } |
Grygorii Strashko | ced66ce | 2018-06-28 14:25:52 -0500 | [diff] [blame] | 166 | while (size & ~(BITS_PER_LONG - 1)) { |
Vitaly Andrianov | 3bd82c5 | 2015-02-05 11:24:46 -0500 | [diff] [blame] | 167 | tmp = *(p++); |
| 168 | if (~tmp) |
| 169 | goto found_middle; |
Grygorii Strashko | ced66ce | 2018-06-28 14:25:52 -0500 | [diff] [blame] | 170 | result += BITS_PER_LONG; |
| 171 | size -= BITS_PER_LONG; |
Vitaly Andrianov | 3bd82c5 | 2015-02-05 11:24:46 -0500 | [diff] [blame] | 172 | } |
| 173 | if (!size) |
| 174 | return result; |
| 175 | tmp = *p; |
| 176 | |
| 177 | found_first: |
Grygorii Strashko | cdcda67 | 2018-04-27 19:58:49 -0500 | [diff] [blame] | 178 | tmp |= ~0UL << size; |
Vitaly Andrianov | 3bd82c5 | 2015-02-05 11:24:46 -0500 | [diff] [blame] | 179 | found_middle: |
| 180 | return result + ffz(tmp); |
| 181 | } |
| 182 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 183 | /* |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 184 | * hweightN: returns the hamming weight (i.e. the number |
| 185 | * of bits set) of a N-bit word |
| 186 | */ |
| 187 | |
| 188 | #define hweight32(x) generic_hweight32(x) |
| 189 | #define hweight16(x) generic_hweight16(x) |
| 190 | #define hweight8(x) generic_hweight8(x) |
| 191 | |
Vitaly Andrianov | 3bd82c5 | 2015-02-05 11:24:46 -0500 | [diff] [blame] | 192 | #define find_first_zero_bit(addr, size) \ |
| 193 | find_next_zero_bit((addr), (size), 0) |
| 194 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 195 | #define ext2_set_bit test_and_set_bit |
| 196 | #define ext2_clear_bit test_and_clear_bit |
| 197 | #define ext2_test_bit test_bit |
| 198 | #define ext2_find_first_zero_bit find_first_zero_bit |
| 199 | #define ext2_find_next_zero_bit find_next_zero_bit |
| 200 | |
| 201 | /* Bitmap functions for the minix filesystem. */ |
| 202 | #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr) |
| 203 | #define minix_set_bit(nr,addr) set_bit(nr,addr) |
| 204 | #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr) |
| 205 | #define minix_test_bit(nr,addr) test_bit(nr,addr) |
| 206 | #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size) |
| 207 | |
| 208 | #endif /* __KERNEL__ */ |
| 209 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 210 | #endif /* _ARM_BITOPS_H */ |