blob: 04ea0207eea38cc809f6f53f84411faa484a8c5f [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
8#include <linux/config.h>
9#include <asm/byteorder.h> /* swab32 */
10#include <asm/system.h> /* save_flags */
11
12#ifdef __KERNEL__
13/*
14 * Function prototypes to keep gcc -Wall happy
15 */
16
17/*
18 * The __ functions are not atomic
19 */
20
21extern void set_bit(int nr, volatile void * addr);
22extern void __set_bit(int nr, volatile void * addr);
23
24extern void clear_bit(int nr, volatile void * addr);
25#define __clear_bit(nr, addr) clear_bit(nr, addr)
26
27extern void change_bit(int nr, volatile void * addr);
28extern void __change_bit(int nr, volatile void * addr);
29extern int test_and_set_bit(int nr, volatile void * addr);
30extern int __test_and_set_bit(int nr, volatile void * addr);
31extern int test_and_clear_bit(int nr, volatile void * addr);
32extern int __test_and_clear_bit(int nr, volatile void * addr);
33extern int test_and_change_bit(int nr, volatile void * addr);
34extern int __test_and_change_bit(int nr, volatile void * addr);
35extern int __constant_test_bit(int nr, const volatile void * addr);
36extern int __test_bit(int nr, volatile void * addr);
37extern int find_first_zero_bit(void * addr, unsigned size);
38extern int find_next_zero_bit (void * addr, int size, int offset);
39
40/*
41 * ffz = Find First Zero in word. Undefined if no zero exists,
42 * so code should check against ~0UL first..
43 */
44extern __inline__ unsigned long ffz(unsigned long word)
45{
46 unsigned long result = 0;
47
48 while(word & 1) {
49 result++;
50 word >>= 1;
51 }
52 return result;
53}
54
55
56extern __inline__ void set_bit(int nr, volatile void * addr)
57{
58 int * a = (int *) addr;
59 int mask;
60 unsigned long flags;
61
62 a += nr >> 5;
63 mask = 1 << (nr & 0x1f);
64 save_flags_cli(flags);
65 *a |= mask;
66 restore_flags(flags);
67}
68
69extern __inline__ void __set_bit(int nr, volatile void * addr)
70{
71 int * a = (int *) addr;
72 int mask;
73
74 a += nr >> 5;
75 mask = 1 << (nr & 0x1f);
76 *a |= mask;
77}
78
79/*
80 * clear_bit() doesn't provide any barrier for the compiler.
81 */
82#define smp_mb__before_clear_bit() barrier()
83#define smp_mb__after_clear_bit() barrier()
84
85extern __inline__ void clear_bit(int nr, volatile void * addr)
86{
87 int * a = (int *) addr;
88 int mask;
89 unsigned long flags;
90
91 a += nr >> 5;
92 mask = 1 << (nr & 0x1f);
93 save_flags_cli(flags);
94 *a &= ~mask;
95 restore_flags(flags);
96}
97
98extern __inline__ void change_bit(int nr, volatile void * addr)
99{
100 int mask;
101 unsigned long flags;
102 unsigned long *ADDR = (unsigned long *) addr;
103
104 ADDR += nr >> 5;
105 mask = 1 << (nr & 31);
106 save_flags_cli(flags);
107 *ADDR ^= mask;
108 restore_flags(flags);
109}
110
111extern __inline__ void __change_bit(int nr, volatile void * addr)
112{
113 int mask;
114 unsigned long *ADDR = (unsigned long *) addr;
115
116 ADDR += nr >> 5;
117 mask = 1 << (nr & 31);
118 *ADDR ^= mask;
119}
120
121extern __inline__ int test_and_set_bit(int nr, volatile void * addr)
122{
123 int mask, retval;
124 volatile unsigned int *a = (volatile unsigned int *) addr;
125 unsigned long flags;
126
127 a += nr >> 5;
128 mask = 1 << (nr & 0x1f);
129 save_flags_cli(flags);
130 retval = (mask & *a) != 0;
131 *a |= mask;
132 restore_flags(flags);
133
134 return retval;
135}
136
137extern __inline__ int __test_and_set_bit(int nr, volatile void * addr)
138{
139 int mask, retval;
140 volatile unsigned int *a = (volatile unsigned int *) addr;
141
142 a += nr >> 5;
143 mask = 1 << (nr & 0x1f);
144 retval = (mask & *a) != 0;
145 *a |= mask;
146 return retval;
147}
148
149extern __inline__ int test_and_clear_bit(int nr, volatile void * addr)
150{
151 int mask, retval;
152 volatile unsigned int *a = (volatile unsigned int *) addr;
153 unsigned long flags;
154
155 a += nr >> 5;
156 mask = 1 << (nr & 0x1f);
157 save_flags_cli(flags);
158 retval = (mask & *a) != 0;
159 *a &= ~mask;
160 restore_flags(flags);
161
162 return retval;
163}
164
165extern __inline__ int __test_and_clear_bit(int nr, volatile void * addr)
166{
167 int mask, retval;
168 volatile unsigned int *a = (volatile unsigned int *) addr;
169
170 a += nr >> 5;
171 mask = 1 << (nr & 0x1f);
172 retval = (mask & *a) != 0;
173 *a &= ~mask;
174 return retval;
175}
176
177extern __inline__ int test_and_change_bit(int nr, volatile void * addr)
178{
179 int mask, retval;
180 volatile unsigned int *a = (volatile unsigned int *) addr;
181 unsigned long flags;
182
183 a += nr >> 5;
184 mask = 1 << (nr & 0x1f);
185 save_flags_cli(flags);
186 retval = (mask & *a) != 0;
187 *a ^= mask;
188 restore_flags(flags);
189
190 return retval;
191}
192
193extern __inline__ int __test_and_change_bit(int nr, volatile void * addr)
194{
195 int mask, retval;
196 volatile unsigned int *a = (volatile unsigned int *) addr;
197
198 a += nr >> 5;
199 mask = 1 << (nr & 0x1f);
200 retval = (mask & *a) != 0;
201 *a ^= mask;
202 return retval;
203}
204
205/*
206 * This routine doesn't need to be atomic.
207 */
208extern __inline__ int __constant_test_bit(int nr, const volatile void * addr)
209{
210 return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
211}
212
213extern __inline__ int __test_bit(int nr, volatile void * addr)
214{
215 int * a = (int *) addr;
216 int mask;
217
218 a += nr >> 5;
219 mask = 1 << (nr & 0x1f);
220 return ((mask & *a) != 0);
221}
222
223#define test_bit(nr,addr) \
224(__builtin_constant_p(nr) ? \
225 __constant_test_bit((nr),(addr)) : \
226 __test_bit((nr),(addr)))
227
228#define find_first_zero_bit(addr, size) \
229 find_next_zero_bit((addr), (size), 0)
230
231extern __inline__ int find_next_zero_bit (void * addr, int size, int offset)
232{
233 unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
234 unsigned long result = offset & ~31UL;
235 unsigned long tmp;
236
237 if (offset >= size)
238 return size;
239 size -= result;
240 offset &= 31UL;
241 if (offset) {
242 tmp = *(p++);
243 tmp |= ~0UL >> (32-offset);
244 if (size < 32)
245 goto found_first;
246 if (~tmp)
247 goto found_middle;
248 size -= 32;
249 result += 32;
250 }
251 while (size & ~31UL) {
252 if (~(tmp = *(p++)))
253 goto found_middle;
254 result += 32;
255 size -= 32;
256 }
257 if (!size)
258 return result;
259 tmp = *p;
260
261found_first:
262 tmp |= ~0UL >> size;
263found_middle:
264 return result + ffz(tmp);
265}
266
267#define ffs(x) generic_ffs(x)
268
269/*
270 * hweightN: returns the hamming weight (i.e. the number
271 * of bits set) of a N-bit word
272 */
273
274#define hweight32(x) generic_hweight32(x)
275#define hweight16(x) generic_hweight16(x)
276#define hweight8(x) generic_hweight8(x)
277
278
279extern __inline__ int ext2_set_bit(int nr, volatile void * addr)
280{
281 int mask, retval;
282 unsigned long flags;
283 volatile unsigned char *ADDR = (unsigned char *) addr;
284
285 ADDR += nr >> 3;
286 mask = 1 << (nr & 0x07);
287 save_flags_cli(flags);
288 retval = (mask & *ADDR) != 0;
289 *ADDR |= mask;
290 restore_flags(flags);
291 return retval;
292}
293
294extern __inline__ int ext2_clear_bit(int nr, volatile void * addr)
295{
296 int mask, retval;
297 unsigned long flags;
298 volatile unsigned char *ADDR = (unsigned char *) addr;
299
300 ADDR += nr >> 3;
301 mask = 1 << (nr & 0x07);
302 save_flags_cli(flags);
303 retval = (mask & *ADDR) != 0;
304 *ADDR &= ~mask;
305 restore_flags(flags);
306 return retval;
307}
308
309extern __inline__ int ext2_test_bit(int nr, const volatile void * addr)
310{
311 int mask;
312 const volatile unsigned char *ADDR = (const unsigned char *) addr;
313
314 ADDR += nr >> 3;
315 mask = 1 << (nr & 0x07);
316 return ((mask & *ADDR) != 0);
317}
318
319#define ext2_find_first_zero_bit(addr, size) \
320 ext2_find_next_zero_bit((addr), (size), 0)
321
322extern __inline__ unsigned long ext2_find_next_zero_bit(void *addr, unsigned long size, unsigned long offset)
323{
324 unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
325 unsigned long result = offset & ~31UL;
326 unsigned long tmp;
327
328 if (offset >= size)
329 return size;
330 size -= result;
331 offset &= 31UL;
332 if(offset) {
333 /* We hold the little endian value in tmp, but then the
334 * shift is illegal. So we could keep a big endian value
335 * in tmp, like this:
336 *
337 * tmp = __swab32(*(p++));
338 * tmp |= ~0UL >> (32-offset);
339 *
340 * but this would decrease preformance, so we change the
341 * shift:
342 */
343 tmp = *(p++);
344 tmp |= __swab32(~0UL >> (32-offset));
345 if(size < 32)
346 goto found_first;
347 if(~tmp)
348 goto found_middle;
349 size -= 32;
350 result += 32;
351 }
352 while(size & ~31UL) {
353 if(~(tmp = *(p++)))
354 goto found_middle;
355 result += 32;
356 size -= 32;
357 }
358 if(!size)
359 return result;
360 tmp = *p;
361
362found_first:
363 /* tmp is little endian, so we would have to swab the shift,
364 * see above. But then we have to swab tmp below for ffz, so
365 * we might as well do this here.
366 */
367 return result + ffz(__swab32(tmp) | (~0UL << size));
368found_middle:
369 return result + ffz(__swab32(tmp));
370}
371
372/* Bitmap functions for the minix filesystem. */
373#define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
374#define minix_set_bit(nr,addr) set_bit(nr,addr)
375#define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
376#define minix_test_bit(nr,addr) test_bit(nr,addr)
377#define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
378
379/**
380 * hweightN - returns the hamming weight of a N-bit word
381 * @x: the word to weigh
382 *
383 * The Hamming Weight of a number is the total number of bits set in it.
384 */
385
386#define hweight32(x) generic_hweight32(x)
387#define hweight16(x) generic_hweight16(x)
388#define hweight8(x) generic_hweight8(x)
389
390#endif /* __KERNEL__ */
391
392#endif /* _MICROBLAZE_BITOPS_H */