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