#ifndef _ASM_X86_BITOPS_H1#define _ASM_X86_BITOPS_H23/*4* Copyright 1992, Linus Torvalds.5*6* Note: inlines with more than a single statement should be marked7* __always_inline to avoid problems with older gcc's inlining heuristics.8*/910#ifndef _LINUX_BITOPS_H11#error only <linux/bitops.h> can be included directly12#endif1314#include <linux/compiler.h>15#include <asm/alternative.h>1617/*18* These have to be done with inline assembly: that way the bit-setting19* is guaranteed to be atomic. All bit operations return 0 if the bit20* was cleared before the operation and != 0 if it was not.21*22* bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).23*/2425#if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1)26/* Technically wrong, but this avoids compilation errors on some gcc27versions. */28#define BITOP_ADDR(x) "=m" (*(volatile long *) (x))29#else30#define BITOP_ADDR(x) "+m" (*(volatile long *) (x))31#endif3233#define ADDR BITOP_ADDR(addr)3435/*36* We do the locked ops that don't return the old value as37* a mask operation on a byte.38*/39#define IS_IMMEDIATE(nr) (__builtin_constant_p(nr))40#define CONST_MASK_ADDR(nr, addr) BITOP_ADDR((void *)(addr) + ((nr)>>3))41#define CONST_MASK(nr) (1 << ((nr) & 7))4243/**44* set_bit - Atomically set a bit in memory45* @nr: the bit to set46* @addr: the address to start counting from47*48* This function is atomic and may not be reordered. See __set_bit()49* if you do not require the atomic guarantees.50*51* Note: there are no guarantees that this function will not be reordered52* on non x86 architectures, so if you are writing portable code,53* make sure not to rely on its reordering guarantees.54*55* Note that @nr may be almost arbitrarily large; this function is not56* restricted to acting on a single-word quantity.57*/58static __always_inline void59set_bit(unsigned int nr, volatile unsigned long *addr)60{61if (IS_IMMEDIATE(nr)) {62asm volatile(LOCK_PREFIX "orb %1,%0"63: CONST_MASK_ADDR(nr, addr)64: "iq" ((u8)CONST_MASK(nr))65: "memory");66} else {67asm volatile(LOCK_PREFIX "bts %1,%0"68: BITOP_ADDR(addr) : "Ir" (nr) : "memory");69}70}7172/**73* __set_bit - Set a bit in memory74* @nr: the bit to set75* @addr: the address to start counting from76*77* Unlike set_bit(), this function is non-atomic and may be reordered.78* If it's called on the same region of memory simultaneously, the effect79* may be that only one operation succeeds.80*/81static inline void __set_bit(int nr, volatile unsigned long *addr)82{83asm volatile("bts %1,%0" : ADDR : "Ir" (nr) : "memory");84}8586/**87* clear_bit - Clears a bit in memory88* @nr: Bit to clear89* @addr: Address to start counting from90*91* clear_bit() is atomic and may not be reordered. However, it does92* not contain a memory barrier, so if it is used for locking purposes,93* you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()94* in order to ensure changes are visible on other processors.95*/96static __always_inline void97clear_bit(int nr, volatile unsigned long *addr)98{99if (IS_IMMEDIATE(nr)) {100asm volatile(LOCK_PREFIX "andb %1,%0"101: CONST_MASK_ADDR(nr, addr)102: "iq" ((u8)~CONST_MASK(nr)));103} else {104asm volatile(LOCK_PREFIX "btr %1,%0"105: BITOP_ADDR(addr)106: "Ir" (nr));107}108}109110/*111* clear_bit_unlock - Clears a bit in memory112* @nr: Bit to clear113* @addr: Address to start counting from114*115* clear_bit() is atomic and implies release semantics before the memory116* operation. It can be used for an unlock.117*/118static inline void clear_bit_unlock(unsigned nr, volatile unsigned long *addr)119{120barrier();121clear_bit(nr, addr);122}123124static inline void __clear_bit(int nr, volatile unsigned long *addr)125{126asm volatile("btr %1,%0" : ADDR : "Ir" (nr));127}128129/*130* __clear_bit_unlock - Clears a bit in memory131* @nr: Bit to clear132* @addr: Address to start counting from133*134* __clear_bit() is non-atomic and implies release semantics before the memory135* operation. It can be used for an unlock if no other CPUs can concurrently136* modify other bits in the word.137*138* No memory barrier is required here, because x86 cannot reorder stores past139* older loads. Same principle as spin_unlock.140*/141static inline void __clear_bit_unlock(unsigned nr, volatile unsigned long *addr)142{143barrier();144__clear_bit(nr, addr);145}146147#define smp_mb__before_clear_bit() barrier()148#define smp_mb__after_clear_bit() barrier()149150/**151* __change_bit - Toggle a bit in memory152* @nr: the bit to change153* @addr: the address to start counting from154*155* Unlike change_bit(), this function is non-atomic and may be reordered.156* If it's called on the same region of memory simultaneously, the effect157* may be that only one operation succeeds.158*/159static inline void __change_bit(int nr, volatile unsigned long *addr)160{161asm volatile("btc %1,%0" : ADDR : "Ir" (nr));162}163164/**165* change_bit - Toggle a bit in memory166* @nr: Bit to change167* @addr: Address to start counting from168*169* change_bit() is atomic and may not be reordered.170* Note that @nr may be almost arbitrarily large; this function is not171* restricted to acting on a single-word quantity.172*/173static inline void change_bit(int nr, volatile unsigned long *addr)174{175if (IS_IMMEDIATE(nr)) {176asm volatile(LOCK_PREFIX "xorb %1,%0"177: CONST_MASK_ADDR(nr, addr)178: "iq" ((u8)CONST_MASK(nr)));179} else {180asm volatile(LOCK_PREFIX "btc %1,%0"181: BITOP_ADDR(addr)182: "Ir" (nr));183}184}185186/**187* test_and_set_bit - Set a bit and return its old value188* @nr: Bit to set189* @addr: Address to count from190*191* This operation is atomic and cannot be reordered.192* It also implies a memory barrier.193*/194static inline int test_and_set_bit(int nr, volatile unsigned long *addr)195{196int oldbit;197198asm volatile(LOCK_PREFIX "bts %2,%1\n\t"199"sbb %0,%0" : "=r" (oldbit), ADDR : "Ir" (nr) : "memory");200201return oldbit;202}203204/**205* test_and_set_bit_lock - Set a bit and return its old value for lock206* @nr: Bit to set207* @addr: Address to count from208*209* This is the same as test_and_set_bit on x86.210*/211static __always_inline int212test_and_set_bit_lock(int nr, volatile unsigned long *addr)213{214return test_and_set_bit(nr, addr);215}216217/**218* __test_and_set_bit - Set a bit and return its old value219* @nr: Bit to set220* @addr: Address to count from221*222* This operation is non-atomic and can be reordered.223* If two examples of this operation race, one can appear to succeed224* but actually fail. You must protect multiple accesses with a lock.225*/226static inline int __test_and_set_bit(int nr, volatile unsigned long *addr)227{228int oldbit;229230asm("bts %2,%1\n\t"231"sbb %0,%0"232: "=r" (oldbit), ADDR233: "Ir" (nr));234return oldbit;235}236237/**238* test_and_clear_bit - Clear a bit and return its old value239* @nr: Bit to clear240* @addr: Address to count from241*242* This operation is atomic and cannot be reordered.243* It also implies a memory barrier.244*/245static inline int test_and_clear_bit(int nr, volatile unsigned long *addr)246{247int oldbit;248249asm volatile(LOCK_PREFIX "btr %2,%1\n\t"250"sbb %0,%0"251: "=r" (oldbit), ADDR : "Ir" (nr) : "memory");252253return oldbit;254}255256/**257* __test_and_clear_bit - Clear a bit and return its old value258* @nr: Bit to clear259* @addr: Address to count from260*261* This operation is non-atomic and can be reordered.262* If two examples of this operation race, one can appear to succeed263* but actually fail. You must protect multiple accesses with a lock.264*/265static inline int __test_and_clear_bit(int nr, volatile unsigned long *addr)266{267int oldbit;268269asm volatile("btr %2,%1\n\t"270"sbb %0,%0"271: "=r" (oldbit), ADDR272: "Ir" (nr));273return oldbit;274}275276/* WARNING: non atomic and it can be reordered! */277static inline int __test_and_change_bit(int nr, volatile unsigned long *addr)278{279int oldbit;280281asm volatile("btc %2,%1\n\t"282"sbb %0,%0"283: "=r" (oldbit), ADDR284: "Ir" (nr) : "memory");285286return oldbit;287}288289/**290* test_and_change_bit - Change a bit and return its old value291* @nr: Bit to change292* @addr: Address to count from293*294* This operation is atomic and cannot be reordered.295* It also implies a memory barrier.296*/297static inline int test_and_change_bit(int nr, volatile unsigned long *addr)298{299int oldbit;300301asm volatile(LOCK_PREFIX "btc %2,%1\n\t"302"sbb %0,%0"303: "=r" (oldbit), ADDR : "Ir" (nr) : "memory");304305return oldbit;306}307308static __always_inline int constant_test_bit(unsigned int nr, const volatile unsigned long *addr)309{310return ((1UL << (nr % BITS_PER_LONG)) &311(addr[nr / BITS_PER_LONG])) != 0;312}313314static inline int variable_test_bit(int nr, volatile const unsigned long *addr)315{316int oldbit;317318asm volatile("bt %2,%1\n\t"319"sbb %0,%0"320: "=r" (oldbit)321: "m" (*(unsigned long *)addr), "Ir" (nr));322323return oldbit;324}325326#if 0 /* Fool kernel-doc since it doesn't do macros yet */327/**328* test_bit - Determine whether a bit is set329* @nr: bit number to test330* @addr: Address to start counting from331*/332static int test_bit(int nr, const volatile unsigned long *addr);333#endif334335#define test_bit(nr, addr) \336(__builtin_constant_p((nr)) \337? constant_test_bit((nr), (addr)) \338: variable_test_bit((nr), (addr)))339340/**341* __ffs - find first set bit in word342* @word: The word to search343*344* Undefined if no bit exists, so code should check against 0 first.345*/346static inline unsigned long __ffs(unsigned long word)347{348asm("bsf %1,%0"349: "=r" (word)350: "rm" (word));351return word;352}353354/**355* ffz - find first zero bit in word356* @word: The word to search357*358* Undefined if no zero exists, so code should check against ~0UL first.359*/360static inline unsigned long ffz(unsigned long word)361{362asm("bsf %1,%0"363: "=r" (word)364: "r" (~word));365return word;366}367368/*369* __fls: find last set bit in word370* @word: The word to search371*372* Undefined if no set bit exists, so code should check against 0 first.373*/374static inline unsigned long __fls(unsigned long word)375{376asm("bsr %1,%0"377: "=r" (word)378: "rm" (word));379return word;380}381382#ifdef __KERNEL__383/**384* ffs - find first set bit in word385* @x: the word to search386*387* This is defined the same way as the libc and compiler builtin ffs388* routines, therefore differs in spirit from the other bitops.389*390* ffs(value) returns 0 if value is 0 or the position of the first391* set bit if value is nonzero. The first (least significant) bit392* is at position 1.393*/394static inline int ffs(int x)395{396int r;397#ifdef CONFIG_X86_CMOV398asm("bsfl %1,%0\n\t"399"cmovzl %2,%0"400: "=r" (r) : "rm" (x), "r" (-1));401#else402asm("bsfl %1,%0\n\t"403"jnz 1f\n\t"404"movl $-1,%0\n"405"1:" : "=r" (r) : "rm" (x));406#endif407return r + 1;408}409410/**411* fls - find last set bit in word412* @x: the word to search413*414* This is defined in a similar way as the libc and compiler builtin415* ffs, but returns the position of the most significant set bit.416*417* fls(value) returns 0 if value is 0 or the position of the last418* set bit if value is nonzero. The last (most significant) bit is419* at position 32.420*/421static inline int fls(int x)422{423int r;424#ifdef CONFIG_X86_CMOV425asm("bsrl %1,%0\n\t"426"cmovzl %2,%0"427: "=&r" (r) : "rm" (x), "rm" (-1));428#else429asm("bsrl %1,%0\n\t"430"jnz 1f\n\t"431"movl $-1,%0\n"432"1:" : "=r" (r) : "rm" (x));433#endif434return r + 1;435}436#endif /* __KERNEL__ */437438#undef ADDR439440#ifdef __KERNEL__441442#include <asm-generic/bitops/find.h>443444#include <asm-generic/bitops/sched.h>445446#define ARCH_HAS_FAST_MULTIPLIER 1447448#include <asm/arch_hweight.h>449450#include <asm-generic/bitops/const_hweight.h>451452#endif /* __KERNEL__ */453454#include <asm-generic/bitops/fls64.h>455456#ifdef __KERNEL__457458#include <asm-generic/bitops/le.h>459460#define ext2_set_bit_atomic(lock, nr, addr) \461test_and_set_bit((nr), (unsigned long *)(addr))462#define ext2_clear_bit_atomic(lock, nr, addr) \463test_and_clear_bit((nr), (unsigned long *)(addr))464465#endif /* __KERNEL__ */466#endif /* _ASM_X86_BITOPS_H */467468469