162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright 1995, Russell King. 462306a36Sopenharmony_ci * Various bits and pieces copyrights include: 562306a36Sopenharmony_ci * Linus Torvalds (test_bit). 662306a36Sopenharmony_ci * Big endian support: Copyright 2001, Nicolas Pitre 762306a36Sopenharmony_ci * reworked by rmk. 862306a36Sopenharmony_ci * 962306a36Sopenharmony_ci * bit 0 is the LSB of an "unsigned long" quantity. 1062306a36Sopenharmony_ci * 1162306a36Sopenharmony_ci * Please note that the code in this file should never be included 1262306a36Sopenharmony_ci * from user space. Many of these are not implemented in assembler 1362306a36Sopenharmony_ci * since they would be too costly. Also, they require privileged 1462306a36Sopenharmony_ci * instructions (which are not available from user mode) to ensure 1562306a36Sopenharmony_ci * that they are atomic. 1662306a36Sopenharmony_ci */ 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ci#ifndef __ASM_ARM_BITOPS_H 1962306a36Sopenharmony_ci#define __ASM_ARM_BITOPS_H 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ci#ifdef __KERNEL__ 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci#ifndef _LINUX_BITOPS_H 2462306a36Sopenharmony_ci#error only <linux/bitops.h> can be included directly 2562306a36Sopenharmony_ci#endif 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ci#include <linux/compiler.h> 2862306a36Sopenharmony_ci#include <linux/irqflags.h> 2962306a36Sopenharmony_ci#include <asm/barrier.h> 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci/* 3262306a36Sopenharmony_ci * These functions are the basis of our bit ops. 3362306a36Sopenharmony_ci * 3462306a36Sopenharmony_ci * First, the atomic bitops. These use native endian. 3562306a36Sopenharmony_ci */ 3662306a36Sopenharmony_cistatic inline void ____atomic_set_bit(unsigned int bit, volatile unsigned long *p) 3762306a36Sopenharmony_ci{ 3862306a36Sopenharmony_ci unsigned long flags; 3962306a36Sopenharmony_ci unsigned long mask = BIT_MASK(bit); 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci p += BIT_WORD(bit); 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ci raw_local_irq_save(flags); 4462306a36Sopenharmony_ci *p |= mask; 4562306a36Sopenharmony_ci raw_local_irq_restore(flags); 4662306a36Sopenharmony_ci} 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_cistatic inline void ____atomic_clear_bit(unsigned int bit, volatile unsigned long *p) 4962306a36Sopenharmony_ci{ 5062306a36Sopenharmony_ci unsigned long flags; 5162306a36Sopenharmony_ci unsigned long mask = BIT_MASK(bit); 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ci p += BIT_WORD(bit); 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci raw_local_irq_save(flags); 5662306a36Sopenharmony_ci *p &= ~mask; 5762306a36Sopenharmony_ci raw_local_irq_restore(flags); 5862306a36Sopenharmony_ci} 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_cistatic inline void ____atomic_change_bit(unsigned int bit, volatile unsigned long *p) 6162306a36Sopenharmony_ci{ 6262306a36Sopenharmony_ci unsigned long flags; 6362306a36Sopenharmony_ci unsigned long mask = BIT_MASK(bit); 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci p += BIT_WORD(bit); 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci raw_local_irq_save(flags); 6862306a36Sopenharmony_ci *p ^= mask; 6962306a36Sopenharmony_ci raw_local_irq_restore(flags); 7062306a36Sopenharmony_ci} 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_cistatic inline int 7362306a36Sopenharmony_ci____atomic_test_and_set_bit(unsigned int bit, volatile unsigned long *p) 7462306a36Sopenharmony_ci{ 7562306a36Sopenharmony_ci unsigned long flags; 7662306a36Sopenharmony_ci unsigned int res; 7762306a36Sopenharmony_ci unsigned long mask = BIT_MASK(bit); 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ci p += BIT_WORD(bit); 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci raw_local_irq_save(flags); 8262306a36Sopenharmony_ci res = *p; 8362306a36Sopenharmony_ci *p = res | mask; 8462306a36Sopenharmony_ci raw_local_irq_restore(flags); 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ci return (res & mask) != 0; 8762306a36Sopenharmony_ci} 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_cistatic inline int 9062306a36Sopenharmony_ci____atomic_test_and_clear_bit(unsigned int bit, volatile unsigned long *p) 9162306a36Sopenharmony_ci{ 9262306a36Sopenharmony_ci unsigned long flags; 9362306a36Sopenharmony_ci unsigned int res; 9462306a36Sopenharmony_ci unsigned long mask = BIT_MASK(bit); 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci p += BIT_WORD(bit); 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_ci raw_local_irq_save(flags); 9962306a36Sopenharmony_ci res = *p; 10062306a36Sopenharmony_ci *p = res & ~mask; 10162306a36Sopenharmony_ci raw_local_irq_restore(flags); 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci return (res & mask) != 0; 10462306a36Sopenharmony_ci} 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_cistatic inline int 10762306a36Sopenharmony_ci____atomic_test_and_change_bit(unsigned int bit, volatile unsigned long *p) 10862306a36Sopenharmony_ci{ 10962306a36Sopenharmony_ci unsigned long flags; 11062306a36Sopenharmony_ci unsigned int res; 11162306a36Sopenharmony_ci unsigned long mask = BIT_MASK(bit); 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci p += BIT_WORD(bit); 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci raw_local_irq_save(flags); 11662306a36Sopenharmony_ci res = *p; 11762306a36Sopenharmony_ci *p = res ^ mask; 11862306a36Sopenharmony_ci raw_local_irq_restore(flags); 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ci return (res & mask) != 0; 12162306a36Sopenharmony_ci} 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ci#include <asm-generic/bitops/non-atomic.h> 12462306a36Sopenharmony_ci 12562306a36Sopenharmony_ci/* 12662306a36Sopenharmony_ci * A note about Endian-ness. 12762306a36Sopenharmony_ci * ------------------------- 12862306a36Sopenharmony_ci * 12962306a36Sopenharmony_ci * When the ARM is put into big endian mode via CR15, the processor 13062306a36Sopenharmony_ci * merely swaps the order of bytes within words, thus: 13162306a36Sopenharmony_ci * 13262306a36Sopenharmony_ci * ------------ physical data bus bits ----------- 13362306a36Sopenharmony_ci * D31 ... D24 D23 ... D16 D15 ... D8 D7 ... D0 13462306a36Sopenharmony_ci * little byte 3 byte 2 byte 1 byte 0 13562306a36Sopenharmony_ci * big byte 0 byte 1 byte 2 byte 3 13662306a36Sopenharmony_ci * 13762306a36Sopenharmony_ci * This means that reading a 32-bit word at address 0 returns the same 13862306a36Sopenharmony_ci * value irrespective of the endian mode bit. 13962306a36Sopenharmony_ci * 14062306a36Sopenharmony_ci * Peripheral devices should be connected with the data bus reversed in 14162306a36Sopenharmony_ci * "Big Endian" mode. ARM Application Note 61 is applicable, and is 14262306a36Sopenharmony_ci * available from http://www.arm.com/. 14362306a36Sopenharmony_ci * 14462306a36Sopenharmony_ci * The following assumes that the data bus connectivity for big endian 14562306a36Sopenharmony_ci * mode has been followed. 14662306a36Sopenharmony_ci * 14762306a36Sopenharmony_ci * Note that bit 0 is defined to be 32-bit word bit 0, not byte 0 bit 0. 14862306a36Sopenharmony_ci */ 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ci/* 15162306a36Sopenharmony_ci * Native endian assembly bitops. nr = 0 -> word 0 bit 0. 15262306a36Sopenharmony_ci */ 15362306a36Sopenharmony_ciextern void _set_bit(int nr, volatile unsigned long * p); 15462306a36Sopenharmony_ciextern void _clear_bit(int nr, volatile unsigned long * p); 15562306a36Sopenharmony_ciextern void _change_bit(int nr, volatile unsigned long * p); 15662306a36Sopenharmony_ciextern int _test_and_set_bit(int nr, volatile unsigned long * p); 15762306a36Sopenharmony_ciextern int _test_and_clear_bit(int nr, volatile unsigned long * p); 15862306a36Sopenharmony_ciextern int _test_and_change_bit(int nr, volatile unsigned long * p); 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_ci/* 16162306a36Sopenharmony_ci * Little endian assembly bitops. nr = 0 -> byte 0 bit 0. 16262306a36Sopenharmony_ci */ 16362306a36Sopenharmony_ciunsigned long _find_first_zero_bit_le(const unsigned long *p, unsigned long size); 16462306a36Sopenharmony_ciunsigned long _find_next_zero_bit_le(const unsigned long *p, 16562306a36Sopenharmony_ci unsigned long size, unsigned long offset); 16662306a36Sopenharmony_ciunsigned long _find_first_bit_le(const unsigned long *p, unsigned long size); 16762306a36Sopenharmony_ciunsigned long _find_next_bit_le(const unsigned long *p, unsigned long size, unsigned long offset); 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_ci/* 17062306a36Sopenharmony_ci * Big endian assembly bitops. nr = 0 -> byte 3 bit 0. 17162306a36Sopenharmony_ci */ 17262306a36Sopenharmony_ciunsigned long _find_first_zero_bit_be(const unsigned long *p, unsigned long size); 17362306a36Sopenharmony_ciunsigned long _find_next_zero_bit_be(const unsigned long *p, 17462306a36Sopenharmony_ci unsigned long size, unsigned long offset); 17562306a36Sopenharmony_ciunsigned long _find_first_bit_be(const unsigned long *p, unsigned long size); 17662306a36Sopenharmony_ciunsigned long _find_next_bit_be(const unsigned long *p, unsigned long size, unsigned long offset); 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ci#ifndef CONFIG_SMP 17962306a36Sopenharmony_ci/* 18062306a36Sopenharmony_ci * The __* form of bitops are non-atomic and may be reordered. 18162306a36Sopenharmony_ci */ 18262306a36Sopenharmony_ci#define ATOMIC_BITOP(name,nr,p) \ 18362306a36Sopenharmony_ci (__builtin_constant_p(nr) ? ____atomic_##name(nr, p) : _##name(nr,p)) 18462306a36Sopenharmony_ci#else 18562306a36Sopenharmony_ci#define ATOMIC_BITOP(name,nr,p) _##name(nr,p) 18662306a36Sopenharmony_ci#endif 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_ci/* 18962306a36Sopenharmony_ci * Native endian atomic definitions. 19062306a36Sopenharmony_ci */ 19162306a36Sopenharmony_ci#define set_bit(nr,p) ATOMIC_BITOP(set_bit,nr,p) 19262306a36Sopenharmony_ci#define clear_bit(nr,p) ATOMIC_BITOP(clear_bit,nr,p) 19362306a36Sopenharmony_ci#define change_bit(nr,p) ATOMIC_BITOP(change_bit,nr,p) 19462306a36Sopenharmony_ci#define test_and_set_bit(nr,p) ATOMIC_BITOP(test_and_set_bit,nr,p) 19562306a36Sopenharmony_ci#define test_and_clear_bit(nr,p) ATOMIC_BITOP(test_and_clear_bit,nr,p) 19662306a36Sopenharmony_ci#define test_and_change_bit(nr,p) ATOMIC_BITOP(test_and_change_bit,nr,p) 19762306a36Sopenharmony_ci 19862306a36Sopenharmony_ci#ifndef __ARMEB__ 19962306a36Sopenharmony_ci/* 20062306a36Sopenharmony_ci * These are the little endian, atomic definitions. 20162306a36Sopenharmony_ci */ 20262306a36Sopenharmony_ci#define find_first_zero_bit(p,sz) _find_first_zero_bit_le(p,sz) 20362306a36Sopenharmony_ci#define find_next_zero_bit(p,sz,off) _find_next_zero_bit_le(p,sz,off) 20462306a36Sopenharmony_ci#define find_first_bit(p,sz) _find_first_bit_le(p,sz) 20562306a36Sopenharmony_ci#define find_next_bit(p,sz,off) _find_next_bit_le(p,sz,off) 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci#else 20862306a36Sopenharmony_ci/* 20962306a36Sopenharmony_ci * These are the big endian, atomic definitions. 21062306a36Sopenharmony_ci */ 21162306a36Sopenharmony_ci#define find_first_zero_bit(p,sz) _find_first_zero_bit_be(p,sz) 21262306a36Sopenharmony_ci#define find_next_zero_bit(p,sz,off) _find_next_zero_bit_be(p,sz,off) 21362306a36Sopenharmony_ci#define find_first_bit(p,sz) _find_first_bit_be(p,sz) 21462306a36Sopenharmony_ci#define find_next_bit(p,sz,off) _find_next_bit_be(p,sz,off) 21562306a36Sopenharmony_ci 21662306a36Sopenharmony_ci#endif 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ci#if __LINUX_ARM_ARCH__ < 5 21962306a36Sopenharmony_ci 22062306a36Sopenharmony_ci#include <asm-generic/bitops/__fls.h> 22162306a36Sopenharmony_ci#include <asm-generic/bitops/__ffs.h> 22262306a36Sopenharmony_ci#include <asm-generic/bitops/fls.h> 22362306a36Sopenharmony_ci#include <asm-generic/bitops/ffs.h> 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_ci#else 22662306a36Sopenharmony_ci 22762306a36Sopenharmony_ci/* 22862306a36Sopenharmony_ci * On ARMv5 and above, the gcc built-ins may rely on the clz instruction 22962306a36Sopenharmony_ci * and produce optimal inlined code in all cases. On ARMv7 it is even 23062306a36Sopenharmony_ci * better by also using the rbit instruction. 23162306a36Sopenharmony_ci */ 23262306a36Sopenharmony_ci#include <asm-generic/bitops/builtin-__fls.h> 23362306a36Sopenharmony_ci#include <asm-generic/bitops/builtin-__ffs.h> 23462306a36Sopenharmony_ci#include <asm-generic/bitops/builtin-fls.h> 23562306a36Sopenharmony_ci#include <asm-generic/bitops/builtin-ffs.h> 23662306a36Sopenharmony_ci 23762306a36Sopenharmony_ci#endif 23862306a36Sopenharmony_ci 23962306a36Sopenharmony_ci#include <asm-generic/bitops/ffz.h> 24062306a36Sopenharmony_ci 24162306a36Sopenharmony_ci#include <asm-generic/bitops/fls64.h> 24262306a36Sopenharmony_ci 24362306a36Sopenharmony_ci#include <asm-generic/bitops/sched.h> 24462306a36Sopenharmony_ci#include <asm-generic/bitops/hweight.h> 24562306a36Sopenharmony_ci#include <asm-generic/bitops/lock.h> 24662306a36Sopenharmony_ci 24762306a36Sopenharmony_ci#ifdef __ARMEB__ 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_cistatic inline int find_first_zero_bit_le(const void *p, unsigned size) 25062306a36Sopenharmony_ci{ 25162306a36Sopenharmony_ci return _find_first_zero_bit_le(p, size); 25262306a36Sopenharmony_ci} 25362306a36Sopenharmony_ci#define find_first_zero_bit_le find_first_zero_bit_le 25462306a36Sopenharmony_ci 25562306a36Sopenharmony_cistatic inline int find_next_zero_bit_le(const void *p, int size, int offset) 25662306a36Sopenharmony_ci{ 25762306a36Sopenharmony_ci return _find_next_zero_bit_le(p, size, offset); 25862306a36Sopenharmony_ci} 25962306a36Sopenharmony_ci#define find_next_zero_bit_le find_next_zero_bit_le 26062306a36Sopenharmony_ci 26162306a36Sopenharmony_cistatic inline int find_next_bit_le(const void *p, int size, int offset) 26262306a36Sopenharmony_ci{ 26362306a36Sopenharmony_ci return _find_next_bit_le(p, size, offset); 26462306a36Sopenharmony_ci} 26562306a36Sopenharmony_ci#define find_next_bit_le find_next_bit_le 26662306a36Sopenharmony_ci 26762306a36Sopenharmony_ci#endif 26862306a36Sopenharmony_ci 26962306a36Sopenharmony_ci#include <asm-generic/bitops/le.h> 27062306a36Sopenharmony_ci 27162306a36Sopenharmony_ci/* 27262306a36Sopenharmony_ci * Ext2 is defined to use little-endian byte ordering. 27362306a36Sopenharmony_ci */ 27462306a36Sopenharmony_ci#include <asm-generic/bitops/ext2-atomic-setbit.h> 27562306a36Sopenharmony_ci 27662306a36Sopenharmony_ci#endif /* __KERNEL__ */ 27762306a36Sopenharmony_ci 27862306a36Sopenharmony_ci#endif /* _ARM_BITOPS_H */ 279