ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-h8300 / bitops.h
1 #ifndef _H8300_BITOPS_H
2 #define _H8300_BITOPS_H
3
4 /*
5  * Copyright 1992, Linus Torvalds.
6  * Copyright 2002, Yoshinori Sato
7  */
8
9 #include <linux/kernel.h>
10 #include <linux/config.h>
11 #include <linux/compiler.h>
12 #include <asm/byteorder.h>      /* swab32 */
13 #include <asm/system.h>
14
15 #ifdef __KERNEL__
16 /*
17  * Function prototypes to keep gcc -Wall happy
18  */
19
20 /*
21  * ffz = Find First Zero in word. Undefined if no zero exists,
22  * so code should check against ~0UL first..
23  */
24 static __inline__ unsigned long ffz(unsigned long word)
25 {
26         unsigned long result;
27
28         result = -1;
29         __asm__("1:\n\t"
30                 "shlr.l %2\n\t"
31                 "adds #1,%0\n\t"
32                 "bcs 1b"
33                 : "=r" (result)
34                 : "0"  (result),"r" (word));
35         return result;
36 }
37
38 #define H8300_GEN_BITOP_CONST(OP,BIT)                       \
39         case BIT:                                           \
40         __asm__(OP " #" #BIT ",@%0"::"r"(b_addr):"memory"); \
41         break;
42
43 #define H8300_GEN_BITOP(FNAME,OP)                                     \
44 static __inline__ void FNAME(int nr, volatile unsigned long* addr)    \
45 {                                                                     \
46         volatile unsigned char *b_addr;                               \
47         b_addr = (volatile unsigned char *)addr + ((nr >> 3) ^ 3);    \
48         if (__builtin_constant_p(nr)) {                               \
49                 switch(nr & 7) {                                      \
50                         H8300_GEN_BITOP_CONST(OP,0)                   \
51                         H8300_GEN_BITOP_CONST(OP,1)                   \
52                         H8300_GEN_BITOP_CONST(OP,2)                   \
53                         H8300_GEN_BITOP_CONST(OP,3)                   \
54                         H8300_GEN_BITOP_CONST(OP,4)                   \
55                         H8300_GEN_BITOP_CONST(OP,5)                   \
56                         H8300_GEN_BITOP_CONST(OP,6)                   \
57                         H8300_GEN_BITOP_CONST(OP,7)                   \
58                 }                                                     \
59         } else {                                                      \
60                 __asm__(OP " %w0,@%1"::"r"(nr),"r"(b_addr):"memory"); \
61         }                                                             \
62 }
63
64 /*
65  * clear_bit() doesn't provide any barrier for the compiler.
66  */
67 #define smp_mb__before_clear_bit()      barrier()
68 #define smp_mb__after_clear_bit()       barrier()
69
70 H8300_GEN_BITOP(set_bit   ,"bset")
71 H8300_GEN_BITOP(clear_bit ,"bclr")
72 H8300_GEN_BITOP(change_bit,"bnot")
73 #define __set_bit(nr,addr)    set_bit((nr),(addr))
74 #define __clear_bit(nr,addr)  clear_bit((nr),(addr))
75 #define __change_bit(nr,addr) change_bit((nr),(addr))
76
77 #undef H8300_GEN_BITOP
78 #undef H8300_GEN_BITOP_CONST
79
80 static __inline__ int test_bit(int nr, const unsigned long* addr)
81 {
82         return (*((volatile unsigned char *)addr + 
83                ((nr >> 3) ^ 3)) & (1UL << (nr & 7))) != 0;
84 }
85
86 #define __test_bit(nr, addr) test_bit(nr, addr)
87
88 #define H8300_GEN_TEST_BITOP_CONST_INT(OP,BIT)                       \
89         case BIT:                                                    \
90         __asm__("stc ccr,%w1\n\t"                                    \
91                 "orc #0x80,ccr\n\t"                                  \
92                 "bld #" #BIT ",@%4\n\t"                              \
93                 OP " #" #BIT ",@%4\n\t"                              \
94                 "rotxl.l %0\n\t"                                     \
95                 "ldc %w1,ccr"                                        \
96                 : "=r"(retval),"=&r"(ccrsave),"=m"(*b_addr)          \
97                 : "0" (retval),"r" (b_addr)                          \
98                 : "memory");                                         \
99         break;
100
101 #define H8300_GEN_TEST_BITOP_CONST(OP,BIT)                           \
102         case BIT:                                                    \
103         __asm__("bld #" #BIT ",@%3\n\t"                              \
104                 OP " #" #BIT ",@%3\n\t"                              \
105                 "rotxl.l %0\n\t"                                     \
106                 : "=r"(retval),"=m"(*b_addr)                         \
107                 : "0" (retval),"r" (b_addr)                          \
108                 : "memory");                                         \
109         break;
110
111 #define H8300_GEN_TEST_BITOP(FNNAME,OP)                              \
112 static __inline__ int FNNAME(int nr, volatile void * addr)           \
113 {                                                                    \
114         int retval = 0;                                              \
115         char ccrsave;                                                \
116         volatile unsigned char *b_addr;                              \
117         b_addr = (volatile unsigned char *)addr + ((nr >> 3) ^ 3);   \
118         if (__builtin_constant_p(nr)) {                              \
119                 switch(nr & 7) {                                     \
120                         H8300_GEN_TEST_BITOP_CONST_INT(OP,0)         \
121                         H8300_GEN_TEST_BITOP_CONST_INT(OP,1)         \
122                         H8300_GEN_TEST_BITOP_CONST_INT(OP,2)         \
123                         H8300_GEN_TEST_BITOP_CONST_INT(OP,3)         \
124                         H8300_GEN_TEST_BITOP_CONST_INT(OP,4)         \
125                         H8300_GEN_TEST_BITOP_CONST_INT(OP,5)         \
126                         H8300_GEN_TEST_BITOP_CONST_INT(OP,6)         \
127                         H8300_GEN_TEST_BITOP_CONST_INT(OP,7)         \
128                 }                                                    \
129         } else {                                                     \
130                 __asm__("stc ccr,%w1\n\t"                            \
131                         "orc #0x80,ccr\n\t"                          \
132                         "btst %w5,@%4\n\t"                           \
133                         OP " %w5,@%4\n\t"                            \
134                         "beq 1f\n\t"                                 \
135                         "inc.l #1,%0\n"                              \
136                         "1:\n\t"                                     \
137                         "ldc %w1,ccr"                                \
138                         : "=r"(retval),"=&r"(ccrsave),"=m"(*b_addr)  \
139                         : "0" (retval),"r" (b_addr),"r"(nr)          \
140                         : "memory");                                 \
141         }                                                            \
142         return retval;                                               \
143 }                                                                    \
144                                                                      \
145 static __inline__ int __ ## FNNAME(int nr, volatile void * addr)     \
146 {                                                                    \
147         int retval = 0;                                              \
148         volatile unsigned char *b_addr;                              \
149         b_addr = (volatile unsigned char *)addr + ((nr >> 3) ^ 3);   \
150         if (__builtin_constant_p(nr)) {                              \
151                 switch(nr & 7) {                                     \
152                         H8300_GEN_TEST_BITOP_CONST(OP,0)             \
153                         H8300_GEN_TEST_BITOP_CONST(OP,1)             \
154                         H8300_GEN_TEST_BITOP_CONST(OP,2)             \
155                         H8300_GEN_TEST_BITOP_CONST(OP,3)             \
156                         H8300_GEN_TEST_BITOP_CONST(OP,4)             \
157                         H8300_GEN_TEST_BITOP_CONST(OP,5)             \
158                         H8300_GEN_TEST_BITOP_CONST(OP,6)             \
159                         H8300_GEN_TEST_BITOP_CONST(OP,7)             \
160                 }                                                    \
161         } else {                                                     \
162                 __asm__("btst %w4,@%3\n\t"                           \
163                         OP " %w4,@%3\n\t"                            \
164                         "beq 1f\n\t"                                 \
165                         "inc.l #1,%0\n"                              \
166                         "1:"                                         \
167                         : "=r"(retval),"=m"(*b_addr)                 \
168                         : "0" (retval),"r" (b_addr),"r"(nr)          \
169                         : "memory");                                 \
170         }                                                            \
171         return retval;                                               \
172 }
173
174 H8300_GEN_TEST_BITOP(test_and_set_bit,   "bset")
175 H8300_GEN_TEST_BITOP(test_and_clear_bit, "bclr")
176 H8300_GEN_TEST_BITOP(test_and_change_bit,"bnot")
177 #undef H8300_GEN_TEST_BITOP_CONST
178 #undef H8300_GEN_TEST_BITOP_CONST_INT
179 #undef H8300_GEN_TEST_BITOP
180
181 #define find_first_zero_bit(addr, size) \
182         find_next_zero_bit((addr), (size), 0)
183
184 static __inline__ int find_next_zero_bit (void * addr, int size, int offset)
185 {
186         unsigned long *p = (unsigned long *)(((unsigned long)addr + (offset >> 3)) & ~3);
187         unsigned long result = offset & ~31UL;
188         unsigned long tmp;
189
190         if (offset >= size)
191                 return size;
192         size -= result;
193         offset &= 31UL;
194         if (offset) {
195                 tmp = *(p++);
196                 tmp |= ~0UL >> (32-offset);
197                 if (size < 32)
198                         goto found_first;
199                 if (~tmp)
200                         goto found_middle;
201                 size -= 32;
202                 result += 32;
203         }
204         while (size & ~31UL) {
205                 if (~(tmp = *(p++)))
206                         goto found_middle;
207                 result += 32;
208                 size -= 32;
209         }
210         if (!size)
211                 return result;
212         tmp = *p;
213
214 found_first:
215         tmp |= ~0UL >> size;
216 found_middle:
217         return result + ffz(tmp);
218 }
219
220 static __inline__ unsigned long __ffs(unsigned long word)
221 {
222         unsigned long result;
223
224         result = -1;
225         __asm__("1:\n\t"
226                 "shlr.l %2\n\t"
227                 "adds #1,%0\n\t"
228                 "bcc 1b"
229                 : "=r" (result)
230                 : "0"(result),"r"(word));
231         return result;
232 }
233
234 #define ffs(x) generic_ffs(x)
235 #define fls(x) generic_fls(x)
236
237 /*
238  * Every architecture must define this function. It's the fastest
239  * way of searching a 140-bit bitmap where the first 100 bits are
240  * unlikely to be set. It's guaranteed that at least one of the 140
241  * bits is cleared.
242  */
243 static inline int sched_find_first_bit(unsigned long *b)
244 {
245         if (unlikely(b[0]))
246                 return __ffs(b[0]);
247         if (unlikely(b[1]))
248                 return __ffs(b[1]) + 32;
249         if (unlikely(b[2]))
250                 return __ffs(b[2]) + 64;
251         if (b[3])
252                 return __ffs(b[3]) + 96;
253         return __ffs(b[4]) + 128;
254 }
255
256 /*
257  * hweightN: returns the hamming weight (i.e. the number
258  * of bits set) of a N-bit word
259  */
260
261 #define hweight32(x) generic_hweight32(x)
262 #define hweight16(x) generic_hweight16(x)
263 #define hweight8(x) generic_hweight8(x)
264
265 static __inline__ int ext2_set_bit(int nr, volatile void * addr)
266 {
267         int             mask, retval;
268         unsigned long   flags;
269         volatile unsigned char  *ADDR = (unsigned char *) addr;
270
271         ADDR += nr >> 3;
272         mask = 1 << (nr & 0x07);
273         local_irq_save(flags);
274         retval = (mask & *ADDR) != 0;
275         *ADDR |= mask;
276         local_irq_restore(flags);
277         return retval;
278 }
279 #define ext2_set_bit_atomic(lock, nr, addr) ext2_set_bit(nr, addr)
280
281 static __inline__ int ext2_clear_bit(int nr, volatile void * addr)
282 {
283         int             mask, retval;
284         unsigned long   flags;
285         volatile unsigned char  *ADDR = (unsigned char *) addr;
286
287         ADDR += nr >> 3;
288         mask = 1 << (nr & 0x07);
289         local_irq_save(flags);
290         retval = (mask & *ADDR) != 0;
291         *ADDR &= ~mask;
292         local_irq_restore(flags);
293         return retval;
294 }
295 #define ext2_clear_bit_atomic(lock, nr, addr) ext2_set_bit(nr, addr)
296
297 static __inline__ int ext2_test_bit(int nr, const volatile void * addr)
298 {
299         int                     mask;
300         const volatile unsigned char    *ADDR = (const unsigned char *) addr;
301
302         ADDR += nr >> 3;
303         mask = 1 << (nr & 0x07);
304         return ((mask & *ADDR) != 0);
305 }
306
307 #define ext2_find_first_zero_bit(addr, size) \
308         ext2_find_next_zero_bit((addr), (size), 0)
309
310 static __inline__ unsigned long ext2_find_next_zero_bit(void *addr, unsigned long size, unsigned long offset)
311 {
312         unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
313         unsigned long result = offset & ~31UL;
314         unsigned long tmp;
315
316         if (offset >= size)
317                 return size;
318         size -= result;
319         offset &= 31UL;
320         if(offset) {
321                 /* We hold the little endian value in tmp, but then the
322                  * shift is illegal. So we could keep a big endian value
323                  * in tmp, like this:
324                  *
325                  * tmp = __swab32(*(p++));
326                  * tmp |= ~0UL >> (32-offset);
327                  *
328                  * but this would decrease performance, so we change the
329                  * shift:
330                  */
331                 tmp = *(p++);
332                 tmp |= __swab32(~0UL >> (32-offset));
333                 if(size < 32)
334                         goto found_first;
335                 if(~tmp)
336                         goto found_middle;
337                 size -= 32;
338                 result += 32;
339         }
340         while(size & ~31UL) {
341                 if(~(tmp = *(p++)))
342                         goto found_middle;
343                 result += 32;
344                 size -= 32;
345         }
346         if(!size)
347                 return result;
348         tmp = *p;
349
350 found_first:
351         /* tmp is little endian, so we would have to swab the shift,
352          * see above. But then we have to swab tmp below for ffz, so
353          * we might as well do this here.
354          */
355         return result + ffz(__swab32(tmp) | (~0UL << size));
356 found_middle:
357         return result + ffz(__swab32(tmp));
358 }
359
360 /* Bitmap functions for the minix filesystem.  */
361 #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
362 #define minix_set_bit(nr,addr) set_bit(nr,addr)
363 #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
364 #define minix_test_bit(nr,addr) test_bit(nr,addr)
365 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
366
367 #endif /* __KERNEL__ */
368
369 #endif /* _H8300_BITOPS_H */