1 /*
2  * Copyright 1995, Russell King.
3  * Various bits and pieces copyrights include:
4  * Linus Torvalds (test_bit).
5  *
6  * Copyright (C) 2017 Andes Technology Corporation
7  * Rick Chen, Andes Technology Corporation <rick@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_RISCV_BITOPS_H
19 #define __ASM_RISCV_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  */
__set_bit(int nr,void * addr)35 static inline void __set_bit(int nr, void *addr)
36 {
37 	int *a = (int *)addr;
38 	int mask;
39 
40 	a += nr >> 5;
41 	mask = 1 << (nr & 0x1f);
42 	*a |= mask;
43 }
44 
45 #define PLATFORM__SET_BIT
46 
__clear_bit(int nr,void * addr)47 static inline void __clear_bit(int nr, void *addr)
48 {
49 	int *a = (int *)addr;
50 	int mask;
51 
52 	a += nr >> 5;
53 	mask = 1 << (nr & 0x1f);
54 	*a &= ~mask;
55 }
56 
57 #define PLATFORM__CLEAR_BIT
58 
__change_bit(int nr,void * addr)59 static inline void __change_bit(int nr, void *addr)
60 {
61 	int mask;
62 	unsigned long *ADDR = (unsigned long *)addr;
63 
64 	ADDR += nr >> 5;
65 	mask = 1 << (nr & 31);
66 	*ADDR ^= mask;
67 }
68 
__test_and_set_bit(int nr,void * addr)69 static inline int __test_and_set_bit(int nr, void *addr)
70 {
71 	int mask, retval;
72 	unsigned int *a = (unsigned int *)addr;
73 
74 	a += nr >> 5;
75 	mask = 1 << (nr & 0x1f);
76 	retval = (mask & *a) != 0;
77 	*a |= mask;
78 	return retval;
79 }
80 
__test_and_clear_bit(int nr,void * addr)81 static inline int __test_and_clear_bit(int nr, void *addr)
82 {
83 	int mask, retval;
84 	unsigned int *a = (unsigned int *)addr;
85 
86 	a += nr >> 5;
87 	mask = 1 << (nr & 0x1f);
88 	retval = (mask & *a) != 0;
89 	*a &= ~mask;
90 	return retval;
91 }
92 
__test_and_change_bit(int nr,void * addr)93 static inline int __test_and_change_bit(int nr, void *addr)
94 {
95 	int mask, retval;
96 	unsigned int *a = (unsigned int *)addr;
97 
98 	a += nr >> 5;
99 	mask = 1 << (nr & 0x1f);
100 	retval = (mask & *a) != 0;
101 	*a ^= mask;
102 	return retval;
103 }
104 
105 /*
106  * This routine doesn't need to be atomic.
107  */
test_bit(int nr,const void * addr)108 static inline int test_bit(int nr, const void *addr)
109 {
110 	return ((unsigned char *)addr)[nr >> 3] & (1U << (nr & 7));
111 }
112 
113 /*
114  * ffz = Find First Zero in word. Undefined if no zero exists,
115  * so code should check against ~0UL first..
116  */
ffz(unsigned long word)117 static inline unsigned long ffz(unsigned long word)
118 {
119 	int k;
120 
121 	word = ~word;
122 	k = 31;
123 	if (word & 0x0000ffff) {
124 		k -= 16; word <<= 16;
125 	}
126 	if (word & 0x00ff0000) {
127 		k -= 8;  word <<= 8;
128 	}
129 	if (word & 0x0f000000) {
130 		k -= 4;  word <<= 4;
131 	}
132 	if (word & 0x30000000) {
133 		k -= 2;  word <<= 2;
134 	}
135 	if (word & 0x40000000)
136 		k -= 1;
137 
138 	return k;
139 }
140 
find_next_zero_bit(void * addr,int size,int offset)141 static inline int find_next_zero_bit(void *addr, int size, int offset)
142 {
143 	unsigned long *p = ((unsigned long *)addr) + (offset / BITS_PER_LONG);
144 	unsigned long result = offset & ~(BITS_PER_LONG - 1);
145 	unsigned long tmp;
146 
147 	if (offset >= size)
148 		return size;
149 	size -= result;
150 	offset &= (BITS_PER_LONG - 1);
151 	if (offset) {
152 		tmp = *(p++);
153 		tmp |= ~0UL >> (BITS_PER_LONG - offset);
154 		if (size < BITS_PER_LONG)
155 			goto found_first;
156 		if (~tmp)
157 			goto found_middle;
158 		size -= BITS_PER_LONG;
159 		result += BITS_PER_LONG;
160 	}
161 	while (size & ~(BITS_PER_LONG - 1)) {
162 		tmp = *(p++);
163 		if (~tmp)
164 			goto found_middle;
165 		result += BITS_PER_LONG;
166 		size -= BITS_PER_LONG;
167 	}
168 	if (!size)
169 		return result;
170 	tmp = *p;
171 
172 found_first:
173 	tmp |= ~0UL << size;
174 found_middle:
175 	return result + ffz(tmp);
176 }
177 
178 /*
179  * ffs: find first bit set. This is defined the same way as
180  * the libc and compiler builtin ffs routines, therefore
181  * differs in spirit from the above ffz (man ffs).
182  */
183 
184 /*
185  * redefined in include/linux/bitops.h
186  * #define ffs(x) generic_ffs(x)
187  */
188 
189 /*
190  * hweightN: returns the hamming weight (i.e. the number
191  * of bits set) of a N-bit word
192  */
193 
194 #define hweight32(x) generic_hweight32(x)
195 #define hweight16(x) generic_hweight16(x)
196 #define hweight8(x) generic_hweight8(x)
197 
198 #define find_first_zero_bit(addr, size) \
199 	find_next_zero_bit((addr), (size), 0)
200 
201 #define test_and_set_bit		__test_and_set_bit
202 #define test_and_clear_bit		__test_and_clear_bit
203 
204 #define ext2_set_bit			test_and_set_bit
205 #define ext2_clear_bit			test_and_clear_bit
206 #define ext2_test_bit			test_bit
207 #define ext2_find_first_zero_bit	find_first_zero_bit
208 #define ext2_find_next_zero_bit		find_next_zero_bit
209 
210 /* Bitmap functions for the minix filesystem. */
211 #define minix_test_and_set_bit(nr, addr)	test_and_set_bit(nr, addr)
212 #define minix_set_bit(nr, addr)			set_bit(nr, addr)
213 #define minix_test_and_clear_bit(nr, addr)	test_and_clear_bit(nr, addr)
214 #define minix_test_bit(nr, addr)		test_bit(nr, addr)
215 #define minix_find_first_zero_bit(addr, size)	find_first_zero_bit(addr, size)
216 
217 #endif /* __KERNEL__ */
218 
219 #endif /* __ASM_RISCV_BITOPS_H */
220