2 * PowerPC atomic bit operations.
4 * Merged version by David Gibson <david@gibson.dropbear.id.au>.
5 * Based on ppc64 versions by: Dave Engebretsen, Todd Inglett, Don
6 * Reed, Pat McCarthy, Peter Bergner, Anton Blanchard. They
7 * originally took it from the ppc32 code.
9 * Within a word, bits are numbered LSB first. Lot's of places make
10 * this assumption by directly testing bits with (val & (1<<nr)).
11 * This can cause confusion for large (> 1 word) bitmaps on a
12 * big-endian system because, unlike little endian, the number of each
13 * bit depends on the word size.
15 * The bitop functions are defined to work on unsigned longs, so for a
16 * ppc64 system the bits end up numbered:
17 * |63..............0|127............64|191...........128|255...........196|
19 * |31.....0|63....31|95....64|127...96|159..128|191..160|223..192|255..224|
21 * There are a few little-endian macros used mostly for filesystem
22 * bitmaps, these work on similar bit arrays layouts, but
24 * |7...0|15...8|23...16|31...24|39...32|47...40|55...48|63...56|
26 * The main difference is that bit 3-5 (64b) or 3-4 (32b) in the bit
27 * number field needs to be reversed compared to the big-endian bit
28 * fields. This can be achieved by XOR with 0x38 (64b) or 0x18 (32b).
30 * This program is free software; you can redistribute it and/or
31 * modify it under the terms of the GNU General Public License
32 * as published by the Free Software Foundation; either version
33 * 2 of the License, or (at your option) any later version.
36 #ifndef _ASM_POWERPC_BITOPS_H
37 #define _ASM_POWERPC_BITOPS_H
41 #include <linux/compiler.h>
42 #include <asm/asm-compat.h>
43 #include <asm/synch.h>
46 * clear_bit doesn't imply a memory barrier
48 #define smp_mb__before_clear_bit() smp_mb()
49 #define smp_mb__after_clear_bit() smp_mb()
51 #define BITOP_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
52 #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
53 #define BITOP_LE_SWIZZLE ((BITS_PER_LONG-1) & ~0x7)
55 static __inline__
void set_bit(int nr
, volatile unsigned long *addr
)
58 unsigned long mask
= BITOP_MASK(nr
);
59 unsigned long *p
= ((unsigned long *)addr
) + BITOP_WORD(nr
);
62 "1:" PPC_LLARX
"%0,0,%3 # set_bit\n"
67 : "=&r" (old
), "+m" (*p
)
72 static __inline__
void clear_bit(int nr
, volatile unsigned long *addr
)
75 unsigned long mask
= BITOP_MASK(nr
);
76 unsigned long *p
= ((unsigned long *)addr
) + BITOP_WORD(nr
);
79 "1:" PPC_LLARX
"%0,0,%3 # clear_bit\n"
84 : "=&r" (old
), "+m" (*p
)
89 static __inline__
void clear_bit_unlock(int nr
, volatile unsigned long *addr
)
92 unsigned long mask
= BITOP_MASK(nr
);
93 unsigned long *p
= ((unsigned long *)addr
) + BITOP_WORD(nr
);
97 "1:" PPC_LLARX
"%0,0,%3 # clear_bit_unlock\n"
100 PPC_STLCX
"%0,0,%3\n"
102 : "=&r" (old
), "+m" (*p
)
103 : "r" (mask
), "r" (p
)
107 static __inline__
void change_bit(int nr
, volatile unsigned long *addr
)
110 unsigned long mask
= BITOP_MASK(nr
);
111 unsigned long *p
= ((unsigned long *)addr
) + BITOP_WORD(nr
);
113 __asm__
__volatile__(
114 "1:" PPC_LLARX
"%0,0,%3 # change_bit\n"
117 PPC_STLCX
"%0,0,%3\n"
119 : "=&r" (old
), "+m" (*p
)
120 : "r" (mask
), "r" (p
)
124 static __inline__
int test_and_set_bit(unsigned long nr
,
125 volatile unsigned long *addr
)
127 unsigned long old
, t
;
128 unsigned long mask
= BITOP_MASK(nr
);
129 unsigned long *p
= ((unsigned long *)addr
) + BITOP_WORD(nr
);
131 __asm__
__volatile__(
133 "1:" PPC_LLARX
"%0,0,%3 # test_and_set_bit\n"
136 PPC_STLCX
"%1,0,%3 \n"
139 : "=&r" (old
), "=&r" (t
)
140 : "r" (mask
), "r" (p
)
143 return (old
& mask
) != 0;
146 static __inline__
int test_and_set_bit_lock(unsigned long nr
,
147 volatile unsigned long *addr
)
149 unsigned long old
, t
;
150 unsigned long mask
= BITOP_MASK(nr
);
151 unsigned long *p
= ((unsigned long *)addr
) + BITOP_WORD(nr
);
153 __asm__
__volatile__(
154 "1:" PPC_LLARX
"%0,0,%3 # test_and_set_bit_lock\n"
157 PPC_STLCX
"%1,0,%3 \n"
160 : "=&r" (old
), "=&r" (t
)
161 : "r" (mask
), "r" (p
)
164 return (old
& mask
) != 0;
167 static __inline__
int test_and_clear_bit(unsigned long nr
,
168 volatile unsigned long *addr
)
170 unsigned long old
, t
;
171 unsigned long mask
= BITOP_MASK(nr
);
172 unsigned long *p
= ((unsigned long *)addr
) + BITOP_WORD(nr
);
174 __asm__
__volatile__(
176 "1:" PPC_LLARX
"%0,0,%3 # test_and_clear_bit\n"
179 PPC_STLCX
"%1,0,%3 \n"
182 : "=&r" (old
), "=&r" (t
)
183 : "r" (mask
), "r" (p
)
186 return (old
& mask
) != 0;
189 static __inline__
int test_and_change_bit(unsigned long nr
,
190 volatile unsigned long *addr
)
192 unsigned long old
, t
;
193 unsigned long mask
= BITOP_MASK(nr
);
194 unsigned long *p
= ((unsigned long *)addr
) + BITOP_WORD(nr
);
196 __asm__
__volatile__(
198 "1:" PPC_LLARX
"%0,0,%3 # test_and_change_bit\n"
201 PPC_STLCX
"%1,0,%3 \n"
204 : "=&r" (old
), "=&r" (t
)
205 : "r" (mask
), "r" (p
)
208 return (old
& mask
) != 0;
211 static __inline__
void set_bits(unsigned long mask
, unsigned long *addr
)
215 __asm__
__volatile__(
216 "1:" PPC_LLARX
"%0,0,%3 # set_bits\n"
218 PPC_STLCX
"%0,0,%3\n"
220 : "=&r" (old
), "+m" (*addr
)
221 : "r" (mask
), "r" (addr
)
225 #include <asm-generic/bitops/non-atomic.h>
227 static __inline__
void __clear_bit_unlock(int nr
, volatile unsigned long *addr
)
229 __asm__
__volatile__(LWSYNC_ON_SMP
"" ::: "memory");
230 __clear_bit(nr
, addr
);
234 * Return the zero-based bit position (LE, not IBM bit numbering) of
235 * the most significant 1-bit in a double word.
237 static __inline__
__attribute__((const))
238 int __ilog2(unsigned long x
)
242 asm (PPC_CNTLZL
"%0,%1" : "=r" (lz
) : "r" (x
));
243 return BITS_PER_LONG
- 1 - lz
;
246 static inline __attribute__((const))
247 int __ilog2_u32(u32 n
)
250 asm ("cntlzw %0,%1" : "=r" (bit
) : "r" (n
));
255 static inline __attribute__((const))
256 int __ilog2_u64(u64 n
)
259 asm ("cntlzd %0,%1" : "=r" (bit
) : "r" (n
));
265 * Determines the bit position of the least significant 0 bit in the
266 * specified double word. The returned bit position will be
267 * zero-based, starting from the right side (63/31 - 0).
269 static __inline__
unsigned long ffz(unsigned long x
)
271 /* no zero exists anywhere in the 8 byte area. */
273 return BITS_PER_LONG
;
276 * Calculate the bit position of the least signficant '1' bit in x
277 * (since x has been changed this will actually be the least signficant
278 * '0' bit in * the original x). Note: (x & -x) gives us a mask that
279 * is the least significant * (RIGHT-most) 1-bit of the value in x.
281 return __ilog2(x
& -x
);
284 static __inline__
int __ffs(unsigned long x
)
286 return __ilog2(x
& -x
);
290 * ffs: find first bit set. This is defined the same way as
291 * the libc and compiler builtin ffs routines, therefore
292 * differs in spirit from the above ffz (man ffs).
294 static __inline__
int ffs(int x
)
296 unsigned long i
= (unsigned long)x
;
297 return __ilog2(i
& -i
) + 1;
301 * fls: find last (most-significant) bit set.
302 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
304 static __inline__
int fls(unsigned int x
)
308 asm ("cntlzw %0,%1" : "=r" (lz
) : "r" (x
));
311 #include <asm-generic/bitops/fls64.h>
313 #include <asm-generic/bitops/hweight.h>
315 #define find_first_zero_bit(addr, size) find_next_zero_bit((addr), (size), 0)
316 unsigned long find_next_zero_bit(const unsigned long *addr
,
317 unsigned long size
, unsigned long offset
);
319 * find_first_bit - find the first set bit in a memory region
320 * @addr: The address to start the search at
321 * @size: The maximum size to search
323 * Returns the bit-number of the first set bit, not the number of the byte
326 #define find_first_bit(addr, size) find_next_bit((addr), (size), 0)
327 unsigned long find_next_bit(const unsigned long *addr
,
328 unsigned long size
, unsigned long offset
);
330 /* Little-endian versions */
332 static __inline__
int test_le_bit(unsigned long nr
,
333 __const__
unsigned long *addr
)
335 __const__
unsigned char *tmp
= (__const__
unsigned char *) addr
;
336 return (tmp
[nr
>> 3] >> (nr
& 7)) & 1;
339 #define __set_le_bit(nr, addr) \
340 __set_bit((nr) ^ BITOP_LE_SWIZZLE, (addr))
341 #define __clear_le_bit(nr, addr) \
342 __clear_bit((nr) ^ BITOP_LE_SWIZZLE, (addr))
344 #define test_and_set_le_bit(nr, addr) \
345 test_and_set_bit((nr) ^ BITOP_LE_SWIZZLE, (addr))
346 #define test_and_clear_le_bit(nr, addr) \
347 test_and_clear_bit((nr) ^ BITOP_LE_SWIZZLE, (addr))
349 #define __test_and_set_le_bit(nr, addr) \
350 __test_and_set_bit((nr) ^ BITOP_LE_SWIZZLE, (addr))
351 #define __test_and_clear_le_bit(nr, addr) \
352 __test_and_clear_bit((nr) ^ BITOP_LE_SWIZZLE, (addr))
354 #define find_first_zero_le_bit(addr, size) generic_find_next_zero_le_bit((addr), (size), 0)
355 unsigned long generic_find_next_zero_le_bit(const unsigned long *addr
,
356 unsigned long size
, unsigned long offset
);
358 /* Bitmap functions for the ext2 filesystem */
360 #define ext2_set_bit(nr,addr) \
361 __test_and_set_le_bit((nr), (unsigned long*)addr)
362 #define ext2_clear_bit(nr, addr) \
363 __test_and_clear_le_bit((nr), (unsigned long*)addr)
365 #define ext2_set_bit_atomic(lock, nr, addr) \
366 test_and_set_le_bit((nr), (unsigned long*)addr)
367 #define ext2_clear_bit_atomic(lock, nr, addr) \
368 test_and_clear_le_bit((nr), (unsigned long*)addr)
370 #define ext2_test_bit(nr, addr) test_le_bit((nr),(unsigned long*)addr)
372 #define ext2_find_first_zero_bit(addr, size) \
373 find_first_zero_le_bit((unsigned long*)addr, size)
374 #define ext2_find_next_zero_bit(addr, size, off) \
375 generic_find_next_zero_le_bit((unsigned long*)addr, size, off)
377 /* Bitmap functions for the minix filesystem. */
379 #define minix_test_and_set_bit(nr,addr) \
380 __test_and_set_le_bit(nr, (unsigned long *)addr)
381 #define minix_set_bit(nr,addr) \
382 __set_le_bit(nr, (unsigned long *)addr)
383 #define minix_test_and_clear_bit(nr,addr) \
384 __test_and_clear_le_bit(nr, (unsigned long *)addr)
385 #define minix_test_bit(nr,addr) \
386 test_le_bit(nr, (unsigned long *)addr)
388 #define minix_find_first_zero_bit(addr,size) \
389 find_first_zero_le_bit((unsigned long *)addr, size)
391 #include <asm-generic/bitops/sched.h>
393 #endif /* __KERNEL__ */
395 #endif /* _ASM_POWERPC_BITOPS_H */