Path: blob/master/arch/powerpc/include/asm/bitops.h
15117 views
/*1* PowerPC atomic bit operations.2*3* Merged version by David Gibson <[email protected]>.4* Based on ppc64 versions by: Dave Engebretsen, Todd Inglett, Don5* Reed, Pat McCarthy, Peter Bergner, Anton Blanchard. They6* originally took it from the ppc32 code.7*8* Within a word, bits are numbered LSB first. Lot's of places make9* this assumption by directly testing bits with (val & (1<<nr)).10* This can cause confusion for large (> 1 word) bitmaps on a11* big-endian system because, unlike little endian, the number of each12* bit depends on the word size.13*14* The bitop functions are defined to work on unsigned longs, so for a15* ppc64 system the bits end up numbered:16* |63..............0|127............64|191...........128|255...........196|17* and on ppc32:18* |31.....0|63....31|95....64|127...96|159..128|191..160|223..192|255..224|19*20* There are a few little-endian macros used mostly for filesystem21* bitmaps, these work on similar bit arrays layouts, but22* byte-oriented:23* |7...0|15...8|23...16|31...24|39...32|47...40|55...48|63...56|24*25* The main difference is that bit 3-5 (64b) or 3-4 (32b) in the bit26* number field needs to be reversed compared to the big-endian bit27* fields. This can be achieved by XOR with 0x38 (64b) or 0x18 (32b).28*29* This program is free software; you can redistribute it and/or30* modify it under the terms of the GNU General Public License31* as published by the Free Software Foundation; either version32* 2 of the License, or (at your option) any later version.33*/3435#ifndef _ASM_POWERPC_BITOPS_H36#define _ASM_POWERPC_BITOPS_H3738#ifdef __KERNEL__3940#ifndef _LINUX_BITOPS_H41#error only <linux/bitops.h> can be included directly42#endif4344#include <linux/compiler.h>45#include <asm/asm-compat.h>46#include <asm/synch.h>4748/*49* clear_bit doesn't imply a memory barrier50*/51#define smp_mb__before_clear_bit() smp_mb()52#define smp_mb__after_clear_bit() smp_mb()5354#define BITOP_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))55#define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)56#define BITOP_LE_SWIZZLE ((BITS_PER_LONG-1) & ~0x7)5758/* Macro for generating the ***_bits() functions */59#define DEFINE_BITOP(fn, op, prefix, postfix) \60static __inline__ void fn(unsigned long mask, \61volatile unsigned long *_p) \62{ \63unsigned long old; \64unsigned long *p = (unsigned long *)_p; \65__asm__ __volatile__ ( \66prefix \67"1:" PPC_LLARX(%0,0,%3,0) "\n" \68stringify_in_c(op) "%0,%0,%2\n" \69PPC405_ERR77(0,%3) \70PPC_STLCX "%0,0,%3\n" \71"bne- 1b\n" \72postfix \73: "=&r" (old), "+m" (*p) \74: "r" (mask), "r" (p) \75: "cc", "memory"); \76}7778DEFINE_BITOP(set_bits, or, "", "")79DEFINE_BITOP(clear_bits, andc, "", "")80DEFINE_BITOP(clear_bits_unlock, andc, PPC_RELEASE_BARRIER, "")81DEFINE_BITOP(change_bits, xor, "", "")8283static __inline__ void set_bit(int nr, volatile unsigned long *addr)84{85set_bits(BITOP_MASK(nr), addr + BITOP_WORD(nr));86}8788static __inline__ void clear_bit(int nr, volatile unsigned long *addr)89{90clear_bits(BITOP_MASK(nr), addr + BITOP_WORD(nr));91}9293static __inline__ void clear_bit_unlock(int nr, volatile unsigned long *addr)94{95clear_bits_unlock(BITOP_MASK(nr), addr + BITOP_WORD(nr));96}9798static __inline__ void change_bit(int nr, volatile unsigned long *addr)99{100change_bits(BITOP_MASK(nr), addr + BITOP_WORD(nr));101}102103/* Like DEFINE_BITOP(), with changes to the arguments to 'op' and the output104* operands. */105#define DEFINE_TESTOP(fn, op, prefix, postfix, eh) \106static __inline__ unsigned long fn( \107unsigned long mask, \108volatile unsigned long *_p) \109{ \110unsigned long old, t; \111unsigned long *p = (unsigned long *)_p; \112__asm__ __volatile__ ( \113prefix \114"1:" PPC_LLARX(%0,0,%3,eh) "\n" \115stringify_in_c(op) "%1,%0,%2\n" \116PPC405_ERR77(0,%3) \117PPC_STLCX "%1,0,%3\n" \118"bne- 1b\n" \119postfix \120: "=&r" (old), "=&r" (t) \121: "r" (mask), "r" (p) \122: "cc", "memory"); \123return (old & mask); \124}125126DEFINE_TESTOP(test_and_set_bits, or, PPC_RELEASE_BARRIER,127PPC_ACQUIRE_BARRIER, 0)128DEFINE_TESTOP(test_and_set_bits_lock, or, "",129PPC_ACQUIRE_BARRIER, 1)130DEFINE_TESTOP(test_and_clear_bits, andc, PPC_RELEASE_BARRIER,131PPC_ACQUIRE_BARRIER, 0)132DEFINE_TESTOP(test_and_change_bits, xor, PPC_RELEASE_BARRIER,133PPC_ACQUIRE_BARRIER, 0)134135static __inline__ int test_and_set_bit(unsigned long nr,136volatile unsigned long *addr)137{138return test_and_set_bits(BITOP_MASK(nr), addr + BITOP_WORD(nr)) != 0;139}140141static __inline__ int test_and_set_bit_lock(unsigned long nr,142volatile unsigned long *addr)143{144return test_and_set_bits_lock(BITOP_MASK(nr),145addr + BITOP_WORD(nr)) != 0;146}147148static __inline__ int test_and_clear_bit(unsigned long nr,149volatile unsigned long *addr)150{151return test_and_clear_bits(BITOP_MASK(nr), addr + BITOP_WORD(nr)) != 0;152}153154static __inline__ int test_and_change_bit(unsigned long nr,155volatile unsigned long *addr)156{157return test_and_change_bits(BITOP_MASK(nr), addr + BITOP_WORD(nr)) != 0;158}159160#include <asm-generic/bitops/non-atomic.h>161162static __inline__ void __clear_bit_unlock(int nr, volatile unsigned long *addr)163{164__asm__ __volatile__(PPC_RELEASE_BARRIER "" ::: "memory");165__clear_bit(nr, addr);166}167168/*169* Return the zero-based bit position (LE, not IBM bit numbering) of170* the most significant 1-bit in a double word.171*/172static __inline__ __attribute__((const))173int __ilog2(unsigned long x)174{175int lz;176177asm (PPC_CNTLZL "%0,%1" : "=r" (lz) : "r" (x));178return BITS_PER_LONG - 1 - lz;179}180181static inline __attribute__((const))182int __ilog2_u32(u32 n)183{184int bit;185asm ("cntlzw %0,%1" : "=r" (bit) : "r" (n));186return 31 - bit;187}188189#ifdef __powerpc64__190static inline __attribute__((const))191int __ilog2_u64(u64 n)192{193int bit;194asm ("cntlzd %0,%1" : "=r" (bit) : "r" (n));195return 63 - bit;196}197#endif198199/*200* Determines the bit position of the least significant 0 bit in the201* specified double word. The returned bit position will be202* zero-based, starting from the right side (63/31 - 0).203*/204static __inline__ unsigned long ffz(unsigned long x)205{206/* no zero exists anywhere in the 8 byte area. */207if ((x = ~x) == 0)208return BITS_PER_LONG;209210/*211* Calculate the bit position of the least significant '1' bit in x212* (since x has been changed this will actually be the least significant213* '0' bit in * the original x). Note: (x & -x) gives us a mask that214* is the least significant * (RIGHT-most) 1-bit of the value in x.215*/216return __ilog2(x & -x);217}218219static __inline__ int __ffs(unsigned long x)220{221return __ilog2(x & -x);222}223224/*225* ffs: find first bit set. This is defined the same way as226* the libc and compiler builtin ffs routines, therefore227* differs in spirit from the above ffz (man ffs).228*/229static __inline__ int ffs(int x)230{231unsigned long i = (unsigned long)x;232return __ilog2(i & -i) + 1;233}234235/*236* fls: find last (most-significant) bit set.237* Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.238*/239static __inline__ int fls(unsigned int x)240{241int lz;242243asm ("cntlzw %0,%1" : "=r" (lz) : "r" (x));244return 32 - lz;245}246247static __inline__ unsigned long __fls(unsigned long x)248{249return __ilog2(x);250}251252/*253* 64-bit can do this using one cntlzd (count leading zeroes doubleword)254* instruction; for 32-bit we use the generic version, which does two255* 32-bit fls calls.256*/257#ifdef __powerpc64__258static __inline__ int fls64(__u64 x)259{260int lz;261262asm ("cntlzd %0,%1" : "=r" (lz) : "r" (x));263return 64 - lz;264}265#else266#include <asm-generic/bitops/fls64.h>267#endif /* __powerpc64__ */268269#ifdef CONFIG_PPC64270unsigned int __arch_hweight8(unsigned int w);271unsigned int __arch_hweight16(unsigned int w);272unsigned int __arch_hweight32(unsigned int w);273unsigned long __arch_hweight64(__u64 w);274#include <asm-generic/bitops/const_hweight.h>275#else276#include <asm-generic/bitops/hweight.h>277#endif278279#include <asm-generic/bitops/find.h>280281/* Little-endian versions */282283static __inline__ int test_bit_le(unsigned long nr,284__const__ void *addr)285{286__const__ unsigned char *tmp = (__const__ unsigned char *) addr;287return (tmp[nr >> 3] >> (nr & 7)) & 1;288}289290static inline void __set_bit_le(int nr, void *addr)291{292__set_bit(nr ^ BITOP_LE_SWIZZLE, addr);293}294295static inline void __clear_bit_le(int nr, void *addr)296{297__clear_bit(nr ^ BITOP_LE_SWIZZLE, addr);298}299300static inline int test_and_set_bit_le(int nr, void *addr)301{302return test_and_set_bit(nr ^ BITOP_LE_SWIZZLE, addr);303}304305static inline int test_and_clear_bit_le(int nr, void *addr)306{307return test_and_clear_bit(nr ^ BITOP_LE_SWIZZLE, addr);308}309310static inline int __test_and_set_bit_le(int nr, void *addr)311{312return __test_and_set_bit(nr ^ BITOP_LE_SWIZZLE, addr);313}314315static inline int __test_and_clear_bit_le(int nr, void *addr)316{317return __test_and_clear_bit(nr ^ BITOP_LE_SWIZZLE, addr);318}319320#define find_first_zero_bit_le(addr, size) \321find_next_zero_bit_le((addr), (size), 0)322unsigned long find_next_zero_bit_le(const void *addr,323unsigned long size, unsigned long offset);324325unsigned long find_next_bit_le(const void *addr,326unsigned long size, unsigned long offset);327/* Bitmap functions for the ext2 filesystem */328329#define ext2_set_bit_atomic(lock, nr, addr) \330test_and_set_bit_le((nr), (unsigned long*)addr)331#define ext2_clear_bit_atomic(lock, nr, addr) \332test_and_clear_bit_le((nr), (unsigned long*)addr)333334#include <asm-generic/bitops/sched.h>335336#endif /* __KERNEL__ */337338#endif /* _ASM_POWERPC_BITOPS_H */339340341