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 #include <asm-generic/bitops/__ffs.h>
19 #include <asm-generic/bitops/__fls.h>
20 #include <asm-generic/bitops/fls.h>
21 #include <asm-generic/bitops/fls64.h>
22
23 #ifdef __KERNEL__
24
25 #ifndef __ASSEMBLY__
26 #include <linux/bitops.h>
27 #endif
28 #include <asm/proc-armv/system.h>
29
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
38 extern void clear_bit(int nr, volatile void * addr);
39
40 extern void change_bit(int nr, volatile void * addr);
41
__change_bit(int nr,volatile void * addr)42 static inline void __change_bit(int nr, volatile void *addr)
43 {
44 unsigned long mask = BIT_MASK(nr);
45 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
46
47 *p ^= mask;
48 }
49
__test_and_set_bit(int nr,volatile void * addr)50 static inline int __test_and_set_bit(int nr, volatile void *addr)
51 {
52 unsigned long mask = BIT_MASK(nr);
53 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
54 unsigned long old = *p;
55
56 *p = old | mask;
57 return (old & mask) != 0;
58 }
59
test_and_set_bit(int nr,volatile void * addr)60 static inline int test_and_set_bit(int nr, volatile void * addr)
61 {
62 unsigned long flags = 0;
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 }
71
__test_and_clear_bit(int nr,volatile void * addr)72 static inline int __test_and_clear_bit(int nr, volatile void *addr)
73 {
74 unsigned long mask = BIT_MASK(nr);
75 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
76 unsigned long old = *p;
77
78 *p = old & ~mask;
79 return (old & mask) != 0;
80 }
81
test_and_clear_bit(int nr,volatile void * addr)82 static inline int test_and_clear_bit(int nr, volatile void * addr)
83 {
84 unsigned long flags = 0;
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
94 extern int test_and_change_bit(int nr, volatile void * addr);
95
__test_and_change_bit(int nr,volatile void * addr)96 static inline int __test_and_change_bit(int nr, volatile void *addr)
97 {
98 unsigned long mask = BIT_MASK(nr);
99 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
100 unsigned long old = *p;
101
102 *p = old ^ mask;
103 return (old & mask) != 0;
104 }
105
106 /*
107 * This routine doesn't need to be atomic.
108 */
test_bit(int nr,const void * addr)109 static inline int test_bit(int nr, const void * addr)
110 {
111 return ((unsigned char *) addr)[nr >> 3] & (1U << (nr & 7));
112 }
113
__ilog2(unsigned int x)114 static inline int __ilog2(unsigned int x)
115 {
116 return generic_fls(x) - 1;
117 }
118
119 #define ffz(x) __ffs(~(x))
120
find_next_zero_bit(void * addr,int size,int offset)121 static inline int find_next_zero_bit(void *addr, int size, int offset)
122 {
123 unsigned long *p = ((unsigned long *)addr) + (offset / BITS_PER_LONG);
124 unsigned long result = offset & ~(BITS_PER_LONG - 1);
125 unsigned long tmp;
126
127 if (offset >= size)
128 return size;
129 size -= result;
130 offset &= (BITS_PER_LONG - 1);
131 if (offset) {
132 tmp = *(p++);
133 tmp |= ~0UL >> (BITS_PER_LONG - offset);
134 if (size < BITS_PER_LONG)
135 goto found_first;
136 if (~tmp)
137 goto found_middle;
138 size -= BITS_PER_LONG;
139 result += BITS_PER_LONG;
140 }
141 while (size & ~(BITS_PER_LONG - 1)) {
142 tmp = *(p++);
143 if (~tmp)
144 goto found_middle;
145 result += BITS_PER_LONG;
146 size -= BITS_PER_LONG;
147 }
148 if (!size)
149 return result;
150 tmp = *p;
151
152 found_first:
153 tmp |= ~0UL << size;
154 found_middle:
155 return result + ffz(tmp);
156 }
157
158 /*
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
167 #define find_first_zero_bit(addr, size) \
168 find_next_zero_bit((addr), (size), 0)
169
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
185 #endif /* _ARM_BITOPS_H */
186