1 /*
2 * Copyright 1995, Russell King.
3 * Various bits and pieces copyrights include:
4 * Linus Torvalds (test_bit).
5 *
6 * Copyright (C) 2011 Andes Technology Corporation
7 * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com>
8 *
9 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
10 *
11 * Please note that the code in this file should never be included
12 * from user space. Many of these are not implemented in assembler
13 * since they would be too costly. Also, they require priviledged
14 * instructions (which are not available from user mode) to ensure
15 * that they are atomic.
16 */
17
18 #ifndef __ASM_NDS_BITOPS_H
19 #define __ASM_NDS_BITOPS_H
20
21 #ifdef __KERNEL__
22
23 #include <asm/system.h>
24 #include <asm-generic/bitops/fls.h>
25 #include <asm-generic/bitops/__fls.h>
26 #include <asm-generic/bitops/fls64.h>
27 #include <asm-generic/bitops/__ffs.h>
28
29 #define smp_mb__before_clear_bit() do { } while (0)
30 #define smp_mb__after_clear_bit() do { } while (0)
31
32 /*
33 * Function prototypes to keep gcc -Wall happy.
34 */
35 extern void set_bit(int nr, void *addr);
36
__set_bit(int nr,void * addr)37 static inline void __set_bit(int nr, void *addr)
38 {
39 int *a = (int *)addr;
40 int mask;
41
42 a += nr >> 5;
43 mask = 1 << (nr & 0x1f);
44 *a |= mask;
45 }
46
47 #define PLATFORM__SET_BIT
48
49 extern void clear_bit(int nr, void *addr);
50
__clear_bit(int nr,void * addr)51 static inline void __clear_bit(int nr, void *addr)
52 {
53 int *a = (int *)addr;
54 int mask;
55 unsigned long flags;
56
57 a += nr >> 5;
58 mask = 1 << (nr & 0x1f);
59 local_irq_save(flags);
60 *a &= ~mask;
61 local_irq_restore(flags);
62 }
63
64 #define PLATFORM__CLEAR_BIT
65
66 extern void change_bit(int nr, void *addr);
67
__change_bit(int nr,void * addr)68 static inline void __change_bit(int nr, void *addr)
69 {
70 int mask;
71 unsigned long *ADDR = (unsigned long *)addr;
72
73 ADDR += nr >> 5;
74 mask = 1 << (nr & 31);
75 *ADDR ^= mask;
76 }
77
78 extern int test_and_set_bit(int nr, void *addr);
79
__test_and_set_bit(int nr,void * addr)80 static inline int __test_and_set_bit(int nr, void *addr)
81 {
82 int mask, retval;
83 unsigned int *a = (unsigned int *)addr;
84
85 a += nr >> 5;
86 mask = 1 << (nr & 0x1f);
87 retval = (mask & *a) != 0;
88 *a |= mask;
89 return retval;
90 }
91
92 extern int test_and_clear_bit(int nr, void *addr);
93
__test_and_clear_bit(int nr,void * addr)94 static inline int __test_and_clear_bit(int nr, void *addr)
95 {
96 int mask, retval;
97 unsigned int *a = (unsigned int *)addr;
98
99 a += nr >> 5;
100 mask = 1 << (nr & 0x1f);
101 retval = (mask & *a) != 0;
102 *a &= ~mask;
103 return retval;
104 }
105
106 extern int test_and_change_bit(int nr, void *addr);
107
__test_and_change_bit(int nr,void * addr)108 static inline int __test_and_change_bit(int nr, void *addr)
109 {
110 int mask, retval;
111 unsigned int *a = (unsigned int *)addr;
112
113 a += nr >> 5;
114 mask = 1 << (nr & 0x1f);
115 retval = (mask & *a) != 0;
116 *a ^= mask;
117 return retval;
118 }
119
120 extern int find_first_zero_bit(void *addr, unsigned size);
121 extern int find_next_zero_bit(void *addr, int size, int offset);
122
123 /*
124 * This routine doesn't need to be atomic.
125 */
test_bit(int nr,const void * addr)126 static inline int test_bit(int nr, const void *addr)
127 {
128 return ((unsigned char *) addr)[nr >> 3] & (1U << (nr & 7));
129 }
130
131 /*
132 * ffz = Find First Zero in word. Undefined if no zero exists,
133 * so code should check against ~0UL first..
134 */
ffz(unsigned long word)135 static inline unsigned long ffz(unsigned long word)
136 {
137 int k;
138
139 word = ~word;
140 k = 31;
141 if (word & 0x0000ffff) {
142 k -= 16; word <<= 16;
143 }
144 if (word & 0x00ff0000) {
145 k -= 8; word <<= 8;
146 }
147 if (word & 0x0f000000) {
148 k -= 4; word <<= 4;
149 }
150 if (word & 0x30000000) {
151 k -= 2; word <<= 2;
152 }
153 if (word & 0x40000000)
154 k -= 1;
155
156 return k;
157 }
158
159 /*
160 * ffs: find first bit set. This is defined the same way as
161 * the libc and compiler builtin ffs routines, therefore
162 * differs in spirit from the above ffz (man ffs).
163 */
164
165 /*
166 * redefined in include/linux/bitops.h
167 * #define ffs(x) generic_ffs(x)
168 */
169
170 /*
171 * hweightN: returns the hamming weight (i.e. the number
172 * of bits set) of a N-bit word
173 */
174
175 #define hweight32(x) generic_hweight32(x)
176 #define hweight16(x) generic_hweight16(x)
177 #define hweight8(x) generic_hweight8(x)
178
179 #define ext2_set_bit test_and_set_bit
180 #define ext2_clear_bit test_and_clear_bit
181 #define ext2_test_bit test_bit
182 #define ext2_find_first_zero_bit find_first_zero_bit
183 #define ext2_find_next_zero_bit find_next_zero_bit
184
185 /* Bitmap functions for the minix filesystem. */
186 #define minix_test_and_set_bit(nr, addr) test_and_set_bit(nr, addr)
187 #define minix_set_bit(nr, addr) set_bit(nr, addr)
188 #define minix_test_and_clear_bit(nr, addr) test_and_clear_bit(nr, addr)
189 #define minix_test_bit(nr, addr) test_bit(nr, addr)
190 #define minix_find_first_zero_bit(addr, size) find_first_zero_bit(addr, size)
191
192 #endif /* __KERNEL__ */
193
194 #endif /* __ASM_NDS_BITOPS_H */
195