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