tizen 2.4 release
[kernel/u-boot-tm1.git] / arch / arm / include / asm / bitops.h
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 #ifdef __KERNEL__
19
20 #include <asm/proc/system.h>
21
22 #define smp_mb__before_clear_bit()      do { } while (0)
23 #define smp_mb__after_clear_bit()       do { } while (0)
24
25 /*
26  * Function prototypes to keep gcc -Wall happy.
27  */
28 static inline void set_bit(int nr, volatile unsigned long * addr)
29 {
30         unsigned long mask = BIT_MASK(nr);
31         unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
32
33         *p  |= mask;
34 }
35
36 static inline void clear_bit(int nr, volatile unsigned long * addr)
37 {
38         unsigned long mask = BIT_MASK(nr);
39         unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
40
41         *p &= ~mask;
42 }
43
44 extern void change_bit(int nr, volatile void * addr);
45
46 static inline void __change_bit(int nr, volatile void *addr)
47 {
48         unsigned long mask = BIT_MASK(nr);
49         unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
50
51         *p ^= mask;
52 }
53
54 static inline int __test_and_set_bit(int nr, volatile void *addr)
55 {
56         unsigned long mask = BIT_MASK(nr);
57         unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
58         unsigned long old = *p;
59
60         *p = old | mask;
61         return (old & mask) != 0;
62 }
63
64 static inline int test_and_set_bit(int nr, volatile void * addr)
65 {
66         unsigned long flags;
67         int out;
68
69         local_irq_save(flags);
70         out = __test_and_set_bit(nr, addr);
71         local_irq_restore(flags);
72
73         return out;
74 }
75
76 static inline int __test_and_clear_bit(int nr, volatile void *addr)
77 {
78         unsigned long mask = BIT_MASK(nr);
79         unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
80         unsigned long old = *p;
81
82         *p = old & ~mask;
83         return (old & mask) != 0;
84 }
85
86 static inline int test_and_clear_bit(int nr, volatile void * addr)
87 {
88         unsigned long flags;
89         int out;
90
91         local_irq_save(flags);
92         out = __test_and_clear_bit(nr, addr);
93         local_irq_restore(flags);
94
95         return out;
96 }
97
98 extern int test_and_change_bit(int nr, volatile void * addr);
99
100 static inline int __test_and_change_bit(int nr, volatile void *addr)
101 {
102         unsigned long mask = BIT_MASK(nr);
103         unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
104         unsigned long old = *p;
105
106         *p = old ^ mask;
107         return (old & mask) != 0;
108 }
109
110 extern int find_first_zero_bit(void * addr, unsigned size);
111 extern int find_next_zero_bit(void * addr, int size, int offset);
112
113 /*
114  * This routine doesn't need to be atomic.
115  */
116 static inline int test_bit(int nr, const void * addr)
117 {
118     return ((unsigned char *) addr)[nr >> 3] & (1U << (nr & 7));
119 }
120
121 /*
122  * ffz = Find First Zero in word. Undefined if no zero exists,
123  * so code should check against ~0UL first..
124  */
125 static inline unsigned long ffz(unsigned long word)
126 {
127         int k;
128
129         word = ~word;
130         k = 31;
131         if (word & 0x0000ffff) { k -= 16; word <<= 16; }
132         if (word & 0x00ff0000) { k -= 8;  word <<= 8;  }
133         if (word & 0x0f000000) { k -= 4;  word <<= 4;  }
134         if (word & 0x30000000) { k -= 2;  word <<= 2;  }
135         if (word & 0x40000000) { k -= 1; }
136         return k;
137 }
138
139 /*
140  * hweightN: returns the hamming weight (i.e. the number
141  * of bits set) of a N-bit word
142  */
143
144 #define hweight32(x) generic_hweight32(x)
145 #define hweight16(x) generic_hweight16(x)
146 #define hweight8(x) generic_hweight8(x)
147
148 #define ext2_set_bit                    test_and_set_bit
149 #define ext2_clear_bit                  test_and_clear_bit
150 #define ext2_test_bit                   test_bit
151 #define ext2_find_first_zero_bit        find_first_zero_bit
152 #define ext2_find_next_zero_bit         find_next_zero_bit
153
154 /* Bitmap functions for the minix filesystem. */
155 #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
156 #define minix_set_bit(nr,addr)          set_bit(nr,addr)
157 #define minix_test_and_clear_bit(nr,addr)       test_and_clear_bit(nr,addr)
158 #define minix_test_bit(nr,addr)         test_bit(nr,addr)
159 #define minix_find_first_zero_bit(addr,size)    find_first_zero_bit(addr,size)
160
161 #endif /* __KERNEL__ */
162
163 #endif /* _ARM_BITOPS_H */