blob: b22eebaa0ae4d68a5ed98fa6c4bbcb5b89bfffc9 [file] [log] [blame]
wdenk2dab3012001-08-05 15:35:05 +00001/*
2 * bitops.h: Bit string operations on the ppc
3 */
4
5#ifndef _PPC_BITOPS_H
6#define _PPC_BITOPS_H
7
wdenk2dab3012001-08-05 15:35:05 +00008#include <asm/byteorder.h>
Fabio Estevam1a03a7e2015-11-05 12:43:40 -02009#include <asm-generic/bitops/__ffs.h>
wdenk2dab3012001-08-05 15:35:05 +000010
wdenk2dab3012001-08-05 15:35:05 +000011/*
12 * Arguably these bit operations don't imply any memory barrier or
13 * SMP ordering, but in fact a lot of drivers expect them to imply
14 * both, since they do on x86 cpus.
15 */
16#ifdef CONFIG_SMP
17#define SMP_WMB "eieio\n"
18#define SMP_MB "\nsync"
19#else
20#define SMP_WMB
21#define SMP_MB
22#endif /* CONFIG_SMP */
23
24#define __INLINE_BITOPS 1
25
26#if __INLINE_BITOPS
27/*
28 * These used to be if'd out here because using : "cc" as a constraint
29 * resulted in errors from egcs. Things may be OK with gcc-2.95.
30 */
Måns Rullgård4dc39702015-11-06 12:44:01 +000031static __inline__ void set_bit(int nr, volatile void * addr)
wdenk2dab3012001-08-05 15:35:05 +000032{
33 unsigned long old;
34 unsigned long mask = 1 << (nr & 0x1f);
35 unsigned long *p = ((unsigned long *)addr) + (nr >> 5);
36
37 __asm__ __volatile__(SMP_WMB "\
381: lwarx %0,0,%3\n\
39 or %0,%0,%2\n\
40 stwcx. %0,0,%3\n\
41 bne 1b"
42 SMP_MB
43 : "=&r" (old), "=m" (*p)
44 : "r" (mask), "r" (p), "m" (*p)
45 : "cc" );
46}
47
Måns Rullgård4dc39702015-11-06 12:44:01 +000048static __inline__ void clear_bit(int nr, volatile void *addr)
wdenk2dab3012001-08-05 15:35:05 +000049{
50 unsigned long old;
51 unsigned long mask = 1 << (nr & 0x1f);
52 unsigned long *p = ((unsigned long *)addr) + (nr >> 5);
53
54 __asm__ __volatile__(SMP_WMB "\
551: lwarx %0,0,%3\n\
56 andc %0,%0,%2\n\
57 stwcx. %0,0,%3\n\
58 bne 1b"
59 SMP_MB
60 : "=&r" (old), "=m" (*p)
61 : "r" (mask), "r" (p), "m" (*p)
62 : "cc");
63}
64
Måns Rullgård4dc39702015-11-06 12:44:01 +000065static __inline__ void change_bit(int nr, volatile void *addr)
wdenk2dab3012001-08-05 15:35:05 +000066{
67 unsigned long old;
68 unsigned long mask = 1 << (nr & 0x1f);
69 unsigned long *p = ((unsigned long *)addr) + (nr >> 5);
70
71 __asm__ __volatile__(SMP_WMB "\
721: lwarx %0,0,%3\n\
73 xor %0,%0,%2\n\
74 stwcx. %0,0,%3\n\
75 bne 1b"
76 SMP_MB
77 : "=&r" (old), "=m" (*p)
78 : "r" (mask), "r" (p), "m" (*p)
79 : "cc");
80}
81
Måns Rullgård4dc39702015-11-06 12:44:01 +000082static __inline__ int test_and_set_bit(int nr, volatile void *addr)
wdenk2dab3012001-08-05 15:35:05 +000083{
84 unsigned int old, t;
85 unsigned int mask = 1 << (nr & 0x1f);
86 volatile unsigned int *p = ((volatile unsigned int *)addr) + (nr >> 5);
87
88 __asm__ __volatile__(SMP_WMB "\
891: lwarx %0,0,%4\n\
90 or %1,%0,%3\n\
91 stwcx. %1,0,%4\n\
92 bne 1b"
93 SMP_MB
94 : "=&r" (old), "=&r" (t), "=m" (*p)
95 : "r" (mask), "r" (p), "m" (*p)
96 : "cc");
97
98 return (old & mask) != 0;
99}
100
Måns Rullgård4dc39702015-11-06 12:44:01 +0000101static __inline__ int test_and_clear_bit(int nr, volatile void *addr)
wdenk2dab3012001-08-05 15:35:05 +0000102{
103 unsigned int old, t;
104 unsigned int mask = 1 << (nr & 0x1f);
105 volatile unsigned int *p = ((volatile unsigned int *)addr) + (nr >> 5);
106
107 __asm__ __volatile__(SMP_WMB "\
1081: lwarx %0,0,%4\n\
109 andc %1,%0,%3\n\
110 stwcx. %1,0,%4\n\
111 bne 1b"
112 SMP_MB
113 : "=&r" (old), "=&r" (t), "=m" (*p)
114 : "r" (mask), "r" (p), "m" (*p)
115 : "cc");
116
117 return (old & mask) != 0;
118}
119
Måns Rullgård4dc39702015-11-06 12:44:01 +0000120static __inline__ int test_and_change_bit(int nr, volatile void *addr)
wdenk2dab3012001-08-05 15:35:05 +0000121{
122 unsigned int old, t;
123 unsigned int mask = 1 << (nr & 0x1f);
124 volatile unsigned int *p = ((volatile unsigned int *)addr) + (nr >> 5);
125
126 __asm__ __volatile__(SMP_WMB "\
1271: lwarx %0,0,%4\n\
128 xor %1,%0,%3\n\
129 stwcx. %1,0,%4\n\
130 bne 1b"
131 SMP_MB
132 : "=&r" (old), "=&r" (t), "=m" (*p)
133 : "r" (mask), "r" (p), "m" (*p)
134 : "cc");
135
136 return (old & mask) != 0;
137}
138#endif /* __INLINE_BITOPS */
139
Måns Rullgård4dc39702015-11-06 12:44:01 +0000140static __inline__ int test_bit(int nr, __const__ volatile void *addr)
wdenk2dab3012001-08-05 15:35:05 +0000141{
142 __const__ unsigned int *p = (__const__ unsigned int *) addr;
143
144 return ((p[nr >> 5] >> (nr & 0x1f)) & 1) != 0;
145}
146
147/* Return the bit position of the most significant 1 bit in a word */
Kumar Gala67bcc632008-06-11 10:14:06 -0500148/* - the result is undefined when x == 0 */
Måns Rullgård4dc39702015-11-06 12:44:01 +0000149static __inline__ int __ilog2(unsigned int x)
wdenk2dab3012001-08-05 15:35:05 +0000150{
151 int lz;
152
153 asm ("cntlzw %0,%1" : "=r" (lz) : "r" (x));
154 return 31 - lz;
155}
156
Måns Rullgård4dc39702015-11-06 12:44:01 +0000157static __inline__ int ffz(unsigned int x)
wdenk2dab3012001-08-05 15:35:05 +0000158{
159 if ((x = ~x) == 0)
160 return 32;
161 return __ilog2(x & -x);
162}
163
Kumar Gala67bcc632008-06-11 10:14:06 -0500164/*
165 * fls: find last (most-significant) bit set.
166 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
167 *
168 * On powerpc, __ilog2(0) returns -1, but this is not safe in general
169 */
170static __inline__ int fls(unsigned int x)
171{
172 return __ilog2(x) + 1;
173}
Simon Kagstrom95a3c732009-09-17 15:15:52 +0200174#define PLATFORM_FLS
Kumar Gala67bcc632008-06-11 10:14:06 -0500175
176/**
177 * fls64 - find last set bit in a 64-bit word
178 * @x: the word to search
179 *
180 * This is defined in a similar way as the libc and compiler builtin
181 * ffsll, but returns the position of the most significant set bit.
182 *
183 * fls64(value) returns 0 if value is 0 or the position of the last
184 * set bit if value is nonzero. The last (most significant) bit is
185 * at position 64.
186 */
187#if BITS_PER_LONG == 32
188static inline int fls64(__u64 x)
189{
190 __u32 h = x >> 32;
191 if (h)
192 return fls(h) + 32;
193 return fls(x);
194}
195#elif BITS_PER_LONG == 64
196static inline int fls64(__u64 x)
197{
198 if (x == 0)
199 return 0;
200 return __ilog2(x) + 1;
201}
202#else
203#error BITS_PER_LONG not 32 or 64
204#endif
205
wdenk2dab3012001-08-05 15:35:05 +0000206#ifdef __KERNEL__
207
208/*
209 * ffs: find first bit set. This is defined the same way as
210 * the libc and compiler builtin ffs routines, therefore
211 * differs in spirit from the above ffz (man ffs).
212 */
Måns Rullgård4dc39702015-11-06 12:44:01 +0000213static __inline__ int ffs(int x)
wdenk2dab3012001-08-05 15:35:05 +0000214{
215 return __ilog2(x & -x) + 1;
216}
Simon Kagstrom95a3c732009-09-17 15:15:52 +0200217#define PLATFORM_FFS
wdenk2dab3012001-08-05 15:35:05 +0000218
219/*
220 * hweightN: returns the hamming weight (i.e. the number
221 * of bits set) of a N-bit word
222 */
223
224#define hweight32(x) generic_hweight32(x)
225#define hweight16(x) generic_hweight16(x)
226#define hweight8(x) generic_hweight8(x)
227
228#endif /* __KERNEL__ */
229
230/*
231 * This implementation of find_{first,next}_zero_bit was stolen from
232 * Linus' asm-alpha/bitops.h.
233 */
234#define find_first_zero_bit(addr, size) \
235 find_next_zero_bit((addr), (size), 0)
236
Måns Rullgård4dc39702015-11-06 12:44:01 +0000237static __inline__ unsigned long find_next_zero_bit(void * addr,
wdenk2dab3012001-08-05 15:35:05 +0000238 unsigned long size, unsigned long offset)
239{
240 unsigned int * p = ((unsigned int *) addr) + (offset >> 5);
241 unsigned int result = offset & ~31UL;
242 unsigned int tmp;
243
244 if (offset >= size)
245 return size;
246 size -= result;
247 offset &= 31UL;
248 if (offset) {
249 tmp = *p++;
250 tmp |= ~0UL >> (32-offset);
251 if (size < 32)
252 goto found_first;
253 if (tmp != ~0U)
254 goto found_middle;
255 size -= 32;
256 result += 32;
257 }
258 while (size >= 32) {
259 if ((tmp = *p++) != ~0U)
260 goto found_middle;
261 result += 32;
262 size -= 32;
263 }
264 if (!size)
265 return result;
266 tmp = *p;
267found_first:
268 tmp |= ~0UL << size;
269found_middle:
270 return result + ffz(tmp);
271}
272
wdenk2dab3012001-08-05 15:35:05 +0000273#define _EXT2_HAVE_ASM_BITOPS_
274
275#ifdef __KERNEL__
276/*
277 * test_and_{set,clear}_bit guarantee atomicity without
278 * disabling interrupts.
279 */
280#define ext2_set_bit(nr, addr) test_and_set_bit((nr) ^ 0x18, addr)
281#define ext2_clear_bit(nr, addr) test_and_clear_bit((nr) ^ 0x18, addr)
282
283#else
Måns Rullgård4dc39702015-11-06 12:44:01 +0000284static __inline__ int ext2_set_bit(int nr, void * addr)
wdenk2dab3012001-08-05 15:35:05 +0000285{
286 int mask;
287 unsigned char *ADDR = (unsigned char *) addr;
288 int oldbit;
289
290 ADDR += nr >> 3;
291 mask = 1 << (nr & 0x07);
292 oldbit = (*ADDR & mask) ? 1 : 0;
293 *ADDR |= mask;
294 return oldbit;
295}
296
Måns Rullgård4dc39702015-11-06 12:44:01 +0000297static __inline__ int ext2_clear_bit(int nr, void * addr)
wdenk2dab3012001-08-05 15:35:05 +0000298{
299 int mask;
300 unsigned char *ADDR = (unsigned char *) addr;
301 int oldbit;
302
303 ADDR += nr >> 3;
304 mask = 1 << (nr & 0x07);
305 oldbit = (*ADDR & mask) ? 1 : 0;
306 *ADDR = *ADDR & ~mask;
307 return oldbit;
308}
309#endif /* __KERNEL__ */
310
Måns Rullgård4dc39702015-11-06 12:44:01 +0000311static __inline__ int ext2_test_bit(int nr, __const__ void * addr)
wdenk2dab3012001-08-05 15:35:05 +0000312{
313 __const__ unsigned char *ADDR = (__const__ unsigned char *) addr;
314
315 return (ADDR[nr >> 3] >> (nr & 7)) & 1;
316}
317
318/*
319 * This implementation of ext2_find_{first,next}_zero_bit was stolen from
320 * Linus' asm-alpha/bitops.h and modified for a big-endian machine.
321 */
322
323#define ext2_find_first_zero_bit(addr, size) \
wdenk57b2d802003-06-27 21:31:46 +0000324 ext2_find_next_zero_bit((addr), (size), 0)
wdenk2dab3012001-08-05 15:35:05 +0000325
Kumar Gala899032b2008-05-15 15:13:08 -0500326static __inline__ unsigned long ext2_find_next_zero_bit(void *addr,
wdenk2dab3012001-08-05 15:35:05 +0000327 unsigned long size, unsigned long offset)
328{
329 unsigned int *p = ((unsigned int *) addr) + (offset >> 5);
330 unsigned int result = offset & ~31UL;
331 unsigned int tmp;
332
333 if (offset >= size)
334 return size;
335 size -= result;
336 offset &= 31UL;
337 if (offset) {
338 tmp = cpu_to_le32p(p++);
339 tmp |= ~0UL >> (32-offset);
340 if (size < 32)
341 goto found_first;
342 if (tmp != ~0U)
343 goto found_middle;
344 size -= 32;
345 result += 32;
346 }
347 while (size >= 32) {
348 if ((tmp = cpu_to_le32p(p++)) != ~0U)
349 goto found_middle;
350 result += 32;
351 size -= 32;
352 }
353 if (!size)
354 return result;
355 tmp = cpu_to_le32p(p);
356found_first:
357 tmp |= ~0U << size;
358found_middle:
359 return result + ffz(tmp);
360}
361
362/* Bitmap functions for the minix filesystem. */
363#define minix_test_and_set_bit(nr,addr) ext2_set_bit(nr,addr)
364#define minix_set_bit(nr,addr) ((void)ext2_set_bit(nr,addr))
365#define minix_test_and_clear_bit(nr,addr) ext2_clear_bit(nr,addr)
366#define minix_test_bit(nr,addr) ext2_test_bit(nr,addr)
367#define minix_find_first_zero_bit(addr,size) ext2_find_first_zero_bit(addr,size)
368
369#endif /* _PPC_BITOPS_H */