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