xref: /openbmc/u-boot/arch/sandbox/include/asm/bitops.h (revision ef64e782)
1 /*
2  * Copyright (c) 2011 The Chromium OS Authors.
3  *
4  * Modified from Linux arch/arm/include/asm/bitops.h
5  *
6  * Copyright 1995, Russell King.
7  * Various bits and pieces copyrights include:
8  *  Linus Torvalds (test_bit).
9  *
10  * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
11  *
12  * Please note that the code in this file should never be included
13  * from user space.  Many of these are not implemented in assembler
14  * since they would be too costly.  Also, they require priviledged
15  * instructions (which are not available from user mode) to ensure
16  * that they are atomic.
17  */
18 
19 #ifndef __ASM_SANDBOX_BITOPS_H
20 #define __ASM_SANDBOX_BITOPS_H
21 
22 #include <linux/compiler.h>
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 #ifdef __KERNEL__
30 
31 #define smp_mb__before_clear_bit()	do { } while (0)
32 #define smp_mb__after_clear_bit()	do { } while (0)
33 
34 /*
35  * Function prototypes to keep gcc -Wall happy.
36  */
37 extern void set_bit(int nr, void *addr);
38 
39 extern void clear_bit(int nr, void *addr);
40 
41 extern void change_bit(int nr, void *addr);
42 
43 static inline void __change_bit(int nr, void *addr)
44 {
45 	unsigned long mask = BIT_MASK(nr);
46 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
47 
48 	*p ^= mask;
49 }
50 
51 static inline int __test_and_set_bit(int nr, void *addr)
52 {
53 	unsigned long mask = BIT_MASK(nr);
54 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
55 	unsigned long old = *p;
56 
57 	*p = old | mask;
58 	return (old & mask) != 0;
59 }
60 
61 static inline int test_and_set_bit(int nr, void *addr)
62 {
63 	unsigned long __always_unused flags;
64 	int out;
65 
66 	local_irq_save(flags);
67 	out = __test_and_set_bit(nr, addr);
68 	local_irq_restore(flags);
69 
70 	return out;
71 }
72 
73 static inline int __test_and_clear_bit(int nr, void *addr)
74 {
75 	unsigned long mask = BIT_MASK(nr);
76 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
77 	unsigned long old = *p;
78 
79 	*p = old & ~mask;
80 	return (old & mask) != 0;
81 }
82 
83 static inline int test_and_clear_bit(int nr, void *addr)
84 {
85 	unsigned long __always_unused flags;
86 	int out;
87 
88 	local_irq_save(flags);
89 	out = __test_and_clear_bit(nr, addr);
90 	local_irq_restore(flags);
91 
92 	return out;
93 }
94 
95 extern int test_and_change_bit(int nr, void *addr);
96 
97 static inline int __test_and_change_bit(int nr, void *addr)
98 {
99 	unsigned long mask = BIT_MASK(nr);
100 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
101 	unsigned long old = *p;
102 
103 	*p = old ^ mask;
104 	return (old & mask) != 0;
105 }
106 
107 extern int find_first_zero_bit(void *addr, unsigned size);
108 extern int find_next_zero_bit(void *addr, int size, int offset);
109 
110 /*
111  * This routine doesn't need to be atomic.
112  */
113 static inline int test_bit(int nr, const void *addr)
114 {
115 	return ((unsigned char *) addr)[nr >> 3] & (1U << (nr & 7));
116 }
117 
118 /*
119  * ffz = Find First Zero in word. Undefined if no zero exists,
120  * so code should check against ~0UL first..
121  */
122 static inline unsigned long ffz(unsigned long word)
123 {
124 	int k;
125 
126 	word = ~word;
127 	k = 31;
128 	if (word & 0x0000ffff) {
129 		k -= 16; word <<= 16;
130 	}
131 	if (word & 0x00ff0000) {
132 		k -= 8;  word <<= 8;
133 	}
134 	if (word & 0x0f000000) {
135 		k -= 4;  word <<= 4;
136 	}
137 	if (word & 0x30000000) {
138 		k -= 2;  word <<= 2;
139 	}
140 	if (word & 0x40000000)
141 		k -= 1;
142 	return k;
143 }
144 
145 /*
146  * hweightN: returns the hamming weight (i.e. the number
147  * of bits set) of a N-bit word
148  */
149 
150 #define hweight32(x) generic_hweight32(x)
151 #define hweight16(x) generic_hweight16(x)
152 #define hweight8(x) generic_hweight8(x)
153 
154 #define ext2_set_bit			test_and_set_bit
155 #define ext2_clear_bit			test_and_clear_bit
156 #define ext2_test_bit			test_bit
157 #define ext2_find_first_zero_bit	find_first_zero_bit
158 #define ext2_find_next_zero_bit		find_next_zero_bit
159 
160 /* Bitmap functions for the minix filesystem. */
161 #define minix_test_and_set_bit(nr, addr)	test_and_set_bit(nr, addr)
162 #define minix_set_bit(nr, addr)			set_bit(nr, addr)
163 #define minix_test_and_clear_bit(nr, addr)	test_and_clear_bit(nr, addr)
164 #define minix_test_bit(nr, addr)		test_bit(nr, addr)
165 #define minix_find_first_zero_bit(addr, size)	find_first_zero_bit(addr, size)
166 
167 #endif /* __KERNEL__ */
168 
169 #endif /* _ARM_BITOPS_H */
170