blob: 9ea217cd8544536432dbf06a38ed3f08bbac4218 [file] [log] [blame]
wdenk12490652004-04-18 21:13:41 +00001#ifndef _MICROBLAZE_BITOPS_H
2#define _MICROBLAZE_BITOPS_H
3
4/*
5 * Copyright 1992, Linus Torvalds.
6 */
7
wdenk12490652004-04-18 21:13:41 +00008#include <asm/byteorder.h> /* swab32 */
9#include <asm/system.h> /* save_flags */
Fabio Estevam6c36ca02015-11-05 12:43:29 -020010#include <asm-generic/bitops/fls.h>
11#include <asm-generic/bitops/__fls.h>
12#include <asm-generic/bitops/fls64.h>
13#include <asm-generic/bitops/__ffs.h>
wdenk12490652004-04-18 21:13:41 +000014
15#ifdef __KERNEL__
16/*
wdenk12490652004-04-18 21:13:41 +000017 * The __ functions are not atomic
18 */
19
wdenk12490652004-04-18 21:13:41 +000020/*
21 * ffz = Find First Zero in word. Undefined if no zero exists,
22 * so code should check against ~0UL first..
23 */
Nathan Rossi14c96dc2015-11-06 19:25:06 +100024static inline unsigned long ffz(unsigned long word)
wdenk12490652004-04-18 21:13:41 +000025{
26 unsigned long result = 0;
27
28 while(word & 1) {
29 result++;
30 word >>= 1;
31 }
32 return result;
33}
34
Nathan Rossi14c96dc2015-11-06 19:25:06 +100035static inline void set_bit(int nr, volatile void *addr)
wdenk12490652004-04-18 21:13:41 +000036{
37 int * a = (int *) addr;
38 int mask;
39 unsigned long flags;
40
41 a += nr >> 5;
42 mask = 1 << (nr & 0x1f);
43 save_flags_cli(flags);
44 *a |= mask;
45 restore_flags(flags);
46}
47
Nathan Rossi14c96dc2015-11-06 19:25:06 +100048static inline void __set_bit(int nr, volatile void *addr)
wdenk12490652004-04-18 21:13:41 +000049{
50 int * a = (int *) addr;
51 int mask;
52
53 a += nr >> 5;
54 mask = 1 << (nr & 0x1f);
55 *a |= mask;
56}
Simon Kagstrom95a3c732009-09-17 15:15:52 +020057#define PLATFORM__SET_BIT
wdenk12490652004-04-18 21:13:41 +000058
59/*
60 * clear_bit() doesn't provide any barrier for the compiler.
61 */
62#define smp_mb__before_clear_bit() barrier()
63#define smp_mb__after_clear_bit() barrier()
64
Nathan Rossi14c96dc2015-11-06 19:25:06 +100065static inline void clear_bit(int nr, volatile void *addr)
wdenk12490652004-04-18 21:13:41 +000066{
67 int * a = (int *) addr;
68 int mask;
69 unsigned long flags;
70
71 a += nr >> 5;
72 mask = 1 << (nr & 0x1f);
73 save_flags_cli(flags);
74 *a &= ~mask;
75 restore_flags(flags);
76}
77
Nathan Rossi14c96dc2015-11-06 19:25:06 +100078#define __clear_bit(nr, addr) clear_bit(nr, addr)
79#define PLATFORM__CLEAR_BIT
80
81static inline void change_bit(int nr, volatile void *addr)
wdenk12490652004-04-18 21:13:41 +000082{
83 int mask;
84 unsigned long flags;
85 unsigned long *ADDR = (unsigned long *) addr;
86
87 ADDR += nr >> 5;
88 mask = 1 << (nr & 31);
89 save_flags_cli(flags);
90 *ADDR ^= mask;
91 restore_flags(flags);
92}
93
Nathan Rossi14c96dc2015-11-06 19:25:06 +100094static inline void __change_bit(int nr, volatile void *addr)
wdenk12490652004-04-18 21:13:41 +000095{
96 int mask;
97 unsigned long *ADDR = (unsigned long *) addr;
98
99 ADDR += nr >> 5;
100 mask = 1 << (nr & 31);
101 *ADDR ^= mask;
102}
103
Nathan Rossi14c96dc2015-11-06 19:25:06 +1000104static inline int test_and_set_bit(int nr, volatile void *addr)
wdenk12490652004-04-18 21:13:41 +0000105{
106 int mask, retval;
107 volatile unsigned int *a = (volatile unsigned int *) addr;
108 unsigned long flags;
109
110 a += nr >> 5;
111 mask = 1 << (nr & 0x1f);
112 save_flags_cli(flags);
113 retval = (mask & *a) != 0;
114 *a |= mask;
115 restore_flags(flags);
116
117 return retval;
118}
119
Nathan Rossi14c96dc2015-11-06 19:25:06 +1000120static inline int __test_and_set_bit(int nr, volatile void *addr)
wdenk12490652004-04-18 21:13:41 +0000121{
122 int mask, retval;
123 volatile unsigned int *a = (volatile unsigned int *) addr;
124
125 a += nr >> 5;
126 mask = 1 << (nr & 0x1f);
127 retval = (mask & *a) != 0;
128 *a |= mask;
129 return retval;
130}
131
Nathan Rossi14c96dc2015-11-06 19:25:06 +1000132static inline int test_and_clear_bit(int nr, volatile void *addr)
wdenk12490652004-04-18 21:13:41 +0000133{
134 int mask, retval;
135 volatile unsigned int *a = (volatile unsigned int *) addr;
136 unsigned long flags;
137
138 a += nr >> 5;
139 mask = 1 << (nr & 0x1f);
140 save_flags_cli(flags);
141 retval = (mask & *a) != 0;
142 *a &= ~mask;
143 restore_flags(flags);
144
145 return retval;
146}
147
Nathan Rossi14c96dc2015-11-06 19:25:06 +1000148static inline int __test_and_clear_bit(int nr, volatile void *addr)
wdenk12490652004-04-18 21:13:41 +0000149{
150 int mask, retval;
151 volatile unsigned int *a = (volatile unsigned int *) addr;
152
153 a += nr >> 5;
154 mask = 1 << (nr & 0x1f);
155 retval = (mask & *a) != 0;
156 *a &= ~mask;
157 return retval;
158}
159
Nathan Rossi14c96dc2015-11-06 19:25:06 +1000160static inline int test_and_change_bit(int nr, volatile void *addr)
wdenk12490652004-04-18 21:13:41 +0000161{
162 int mask, retval;
163 volatile unsigned int *a = (volatile unsigned int *) addr;
164 unsigned long flags;
165
166 a += nr >> 5;
167 mask = 1 << (nr & 0x1f);
168 save_flags_cli(flags);
169 retval = (mask & *a) != 0;
170 *a ^= mask;
171 restore_flags(flags);
172
173 return retval;
174}
175
Nathan Rossi14c96dc2015-11-06 19:25:06 +1000176static inline int __test_and_change_bit(int nr, volatile void *addr)
wdenk12490652004-04-18 21:13:41 +0000177{
178 int mask, retval;
179 volatile unsigned int *a = (volatile unsigned int *) addr;
180
181 a += nr >> 5;
182 mask = 1 << (nr & 0x1f);
183 retval = (mask & *a) != 0;
184 *a ^= mask;
185 return retval;
186}
187
188/*
189 * This routine doesn't need to be atomic.
190 */
Nathan Rossi14c96dc2015-11-06 19:25:06 +1000191static inline int __constant_test_bit(int nr, const volatile void *addr)
wdenk12490652004-04-18 21:13:41 +0000192{
193 return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
194}
195
Nathan Rossi14c96dc2015-11-06 19:25:06 +1000196static inline int __test_bit(int nr, volatile void *addr)
wdenk12490652004-04-18 21:13:41 +0000197{
198 int * a = (int *) addr;
199 int mask;
200
201 a += nr >> 5;
202 mask = 1 << (nr & 0x1f);
203 return ((mask & *a) != 0);
204}
205
206#define test_bit(nr,addr) \
207(__builtin_constant_p(nr) ? \
208 __constant_test_bit((nr),(addr)) : \
209 __test_bit((nr),(addr)))
210
211#define find_first_zero_bit(addr, size) \
212 find_next_zero_bit((addr), (size), 0)
213
Nathan Rossi14c96dc2015-11-06 19:25:06 +1000214static inline int find_next_zero_bit(void *addr, int size, int offset)
wdenk12490652004-04-18 21:13:41 +0000215{
216 unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
217 unsigned long result = offset & ~31UL;
218 unsigned long tmp;
219
220 if (offset >= size)
221 return size;
222 size -= result;
223 offset &= 31UL;
224 if (offset) {
225 tmp = *(p++);
226 tmp |= ~0UL >> (32-offset);
227 if (size < 32)
228 goto found_first;
229 if (~tmp)
230 goto found_middle;
231 size -= 32;
232 result += 32;
233 }
234 while (size & ~31UL) {
235 if (~(tmp = *(p++)))
236 goto found_middle;
237 result += 32;
238 size -= 32;
239 }
240 if (!size)
241 return result;
242 tmp = *p;
243
244found_first:
245 tmp |= ~0UL >> size;
246found_middle:
247 return result + ffz(tmp);
248}
249
wdenk12490652004-04-18 21:13:41 +0000250/*
251 * hweightN: returns the hamming weight (i.e. the number
252 * of bits set) of a N-bit word
253 */
254
255#define hweight32(x) generic_hweight32(x)
256#define hweight16(x) generic_hweight16(x)
257#define hweight8(x) generic_hweight8(x)
258
Nathan Rossi14c96dc2015-11-06 19:25:06 +1000259static inline int ext2_set_bit(int nr, volatile void *addr)
wdenk12490652004-04-18 21:13:41 +0000260{
261 int mask, retval;
262 unsigned long flags;
263 volatile unsigned char *ADDR = (unsigned char *) addr;
264
265 ADDR += nr >> 3;
266 mask = 1 << (nr & 0x07);
267 save_flags_cli(flags);
268 retval = (mask & *ADDR) != 0;
269 *ADDR |= mask;
270 restore_flags(flags);
271 return retval;
272}
273
Nathan Rossi14c96dc2015-11-06 19:25:06 +1000274static inline int ext2_clear_bit(int nr, volatile void *addr)
wdenk12490652004-04-18 21:13:41 +0000275{
276 int mask, retval;
277 unsigned long flags;
278 volatile unsigned char *ADDR = (unsigned char *) addr;
279
280 ADDR += nr >> 3;
281 mask = 1 << (nr & 0x07);
282 save_flags_cli(flags);
283 retval = (mask & *ADDR) != 0;
284 *ADDR &= ~mask;
285 restore_flags(flags);
286 return retval;
287}
288
Nathan Rossi14c96dc2015-11-06 19:25:06 +1000289static inline int ext2_test_bit(int nr, const volatile void *addr)
wdenk12490652004-04-18 21:13:41 +0000290{
291 int mask;
292 const volatile unsigned char *ADDR = (const unsigned char *) addr;
293
294 ADDR += nr >> 3;
295 mask = 1 << (nr & 0x07);
296 return ((mask & *ADDR) != 0);
297}
298
299#define ext2_find_first_zero_bit(addr, size) \
300 ext2_find_next_zero_bit((addr), (size), 0)
301
Michal Simek59085d32012-09-26 14:42:38 +0200302static inline unsigned long ext2_find_next_zero_bit(void *addr,
303 unsigned long size, unsigned long offset)
wdenk12490652004-04-18 21:13:41 +0000304{
305 unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
306 unsigned long result = offset & ~31UL;
307 unsigned long tmp;
308
309 if (offset >= size)
310 return size;
311 size -= result;
312 offset &= 31UL;
313 if(offset) {
314 /* We hold the little endian value in tmp, but then the
315 * shift is illegal. So we could keep a big endian value
316 * in tmp, like this:
317 *
318 * tmp = __swab32(*(p++));
319 * tmp |= ~0UL >> (32-offset);
320 *
321 * but this would decrease preformance, so we change the
322 * shift:
323 */
324 tmp = *(p++);
325 tmp |= __swab32(~0UL >> (32-offset));
326 if(size < 32)
327 goto found_first;
328 if(~tmp)
329 goto found_middle;
330 size -= 32;
331 result += 32;
332 }
333 while(size & ~31UL) {
334 if(~(tmp = *(p++)))
335 goto found_middle;
336 result += 32;
337 size -= 32;
338 }
339 if(!size)
340 return result;
341 tmp = *p;
342
343found_first:
344 /* tmp is little endian, so we would have to swab the shift,
345 * see above. But then we have to swab tmp below for ffz, so
346 * we might as well do this here.
347 */
348 return result + ffz(__swab32(tmp) | (~0UL << size));
349found_middle:
350 return result + ffz(__swab32(tmp));
351}
352
353/* Bitmap functions for the minix filesystem. */
354#define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
355#define minix_set_bit(nr,addr) set_bit(nr,addr)
356#define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
357#define minix_test_bit(nr,addr) test_bit(nr,addr)
358#define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
359
360/**
361 * hweightN - returns the hamming weight of a N-bit word
362 * @x: the word to weigh
363 *
364 * The Hamming Weight of a number is the total number of bits set in it.
365 */
366
367#define hweight32(x) generic_hweight32(x)
368#define hweight16(x) generic_hweight16(x)
369#define hweight8(x) generic_hweight8(x)
370
371#endif /* __KERNEL__ */
372
373#endif /* _MICROBLAZE_BITOPS_H */