18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * PowerPC atomic bit operations. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Merged version by David Gibson <david@gibson.dropbear.id.au>. 68c2ecf20Sopenharmony_ci * Based on ppc64 versions by: Dave Engebretsen, Todd Inglett, Don 78c2ecf20Sopenharmony_ci * Reed, Pat McCarthy, Peter Bergner, Anton Blanchard. They 88c2ecf20Sopenharmony_ci * originally took it from the ppc32 code. 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * Within a word, bits are numbered LSB first. Lot's of places make 118c2ecf20Sopenharmony_ci * this assumption by directly testing bits with (val & (1<<nr)). 128c2ecf20Sopenharmony_ci * This can cause confusion for large (> 1 word) bitmaps on a 138c2ecf20Sopenharmony_ci * big-endian system because, unlike little endian, the number of each 148c2ecf20Sopenharmony_ci * bit depends on the word size. 158c2ecf20Sopenharmony_ci * 168c2ecf20Sopenharmony_ci * The bitop functions are defined to work on unsigned longs, so for a 178c2ecf20Sopenharmony_ci * ppc64 system the bits end up numbered: 188c2ecf20Sopenharmony_ci * |63..............0|127............64|191...........128|255...........192| 198c2ecf20Sopenharmony_ci * and on ppc32: 208c2ecf20Sopenharmony_ci * |31.....0|63....32|95....64|127...96|159..128|191..160|223..192|255..224| 218c2ecf20Sopenharmony_ci * 228c2ecf20Sopenharmony_ci * There are a few little-endian macros used mostly for filesystem 238c2ecf20Sopenharmony_ci * bitmaps, these work on similar bit arrays layouts, but 248c2ecf20Sopenharmony_ci * byte-oriented: 258c2ecf20Sopenharmony_ci * |7...0|15...8|23...16|31...24|39...32|47...40|55...48|63...56| 268c2ecf20Sopenharmony_ci * 278c2ecf20Sopenharmony_ci * The main difference is that bit 3-5 (64b) or 3-4 (32b) in the bit 288c2ecf20Sopenharmony_ci * number field needs to be reversed compared to the big-endian bit 298c2ecf20Sopenharmony_ci * fields. This can be achieved by XOR with 0x38 (64b) or 0x18 (32b). 308c2ecf20Sopenharmony_ci */ 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#ifndef _ASM_POWERPC_BITOPS_H 338c2ecf20Sopenharmony_ci#define _ASM_POWERPC_BITOPS_H 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci#ifdef __KERNEL__ 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci#ifndef _LINUX_BITOPS_H 388c2ecf20Sopenharmony_ci#error only <linux/bitops.h> can be included directly 398c2ecf20Sopenharmony_ci#endif 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci#include <linux/compiler.h> 428c2ecf20Sopenharmony_ci#include <asm/asm-compat.h> 438c2ecf20Sopenharmony_ci#include <asm/synch.h> 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci/* PPC bit number conversion */ 468c2ecf20Sopenharmony_ci#define PPC_BITLSHIFT(be) (BITS_PER_LONG - 1 - (be)) 478c2ecf20Sopenharmony_ci#define PPC_BIT(bit) (1UL << PPC_BITLSHIFT(bit)) 488c2ecf20Sopenharmony_ci#define PPC_BITMASK(bs, be) ((PPC_BIT(bs) - PPC_BIT(be)) | PPC_BIT(bs)) 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci/* Put a PPC bit into a "normal" bit position */ 518c2ecf20Sopenharmony_ci#define PPC_BITEXTRACT(bits, ppc_bit, dst_bit) \ 528c2ecf20Sopenharmony_ci ((((bits) >> PPC_BITLSHIFT(ppc_bit)) & 1) << (dst_bit)) 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci#define PPC_BITLSHIFT32(be) (32 - 1 - (be)) 558c2ecf20Sopenharmony_ci#define PPC_BIT32(bit) (1UL << PPC_BITLSHIFT32(bit)) 568c2ecf20Sopenharmony_ci#define PPC_BITMASK32(bs, be) ((PPC_BIT32(bs) - PPC_BIT32(be))|PPC_BIT32(bs)) 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci#define PPC_BITLSHIFT8(be) (8 - 1 - (be)) 598c2ecf20Sopenharmony_ci#define PPC_BIT8(bit) (1UL << PPC_BITLSHIFT8(bit)) 608c2ecf20Sopenharmony_ci#define PPC_BITMASK8(bs, be) ((PPC_BIT8(bs) - PPC_BIT8(be))|PPC_BIT8(bs)) 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci#include <asm/barrier.h> 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci/* Macro for generating the ***_bits() functions */ 658c2ecf20Sopenharmony_ci#define DEFINE_BITOP(fn, op, prefix) \ 668c2ecf20Sopenharmony_cistatic inline void fn(unsigned long mask, \ 678c2ecf20Sopenharmony_ci volatile unsigned long *_p) \ 688c2ecf20Sopenharmony_ci{ \ 698c2ecf20Sopenharmony_ci unsigned long old; \ 708c2ecf20Sopenharmony_ci unsigned long *p = (unsigned long *)_p; \ 718c2ecf20Sopenharmony_ci __asm__ __volatile__ ( \ 728c2ecf20Sopenharmony_ci prefix \ 738c2ecf20Sopenharmony_ci"1:" PPC_LLARX(%0,0,%3,0) "\n" \ 748c2ecf20Sopenharmony_ci stringify_in_c(op) "%0,%0,%2\n" \ 758c2ecf20Sopenharmony_ci PPC_STLCX "%0,0,%3\n" \ 768c2ecf20Sopenharmony_ci "bne- 1b\n" \ 778c2ecf20Sopenharmony_ci : "=&r" (old), "+m" (*p) \ 788c2ecf20Sopenharmony_ci : "r" (mask), "r" (p) \ 798c2ecf20Sopenharmony_ci : "cc", "memory"); \ 808c2ecf20Sopenharmony_ci} 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ciDEFINE_BITOP(set_bits, or, "") 838c2ecf20Sopenharmony_ciDEFINE_BITOP(clear_bits, andc, "") 848c2ecf20Sopenharmony_ciDEFINE_BITOP(clear_bits_unlock, andc, PPC_RELEASE_BARRIER) 858c2ecf20Sopenharmony_ciDEFINE_BITOP(change_bits, xor, "") 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_cistatic inline void arch_set_bit(int nr, volatile unsigned long *addr) 888c2ecf20Sopenharmony_ci{ 898c2ecf20Sopenharmony_ci set_bits(BIT_MASK(nr), addr + BIT_WORD(nr)); 908c2ecf20Sopenharmony_ci} 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_cistatic inline void arch_clear_bit(int nr, volatile unsigned long *addr) 938c2ecf20Sopenharmony_ci{ 948c2ecf20Sopenharmony_ci clear_bits(BIT_MASK(nr), addr + BIT_WORD(nr)); 958c2ecf20Sopenharmony_ci} 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_cistatic inline void arch_clear_bit_unlock(int nr, volatile unsigned long *addr) 988c2ecf20Sopenharmony_ci{ 998c2ecf20Sopenharmony_ci clear_bits_unlock(BIT_MASK(nr), addr + BIT_WORD(nr)); 1008c2ecf20Sopenharmony_ci} 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_cistatic inline void arch_change_bit(int nr, volatile unsigned long *addr) 1038c2ecf20Sopenharmony_ci{ 1048c2ecf20Sopenharmony_ci change_bits(BIT_MASK(nr), addr + BIT_WORD(nr)); 1058c2ecf20Sopenharmony_ci} 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci/* Like DEFINE_BITOP(), with changes to the arguments to 'op' and the output 1088c2ecf20Sopenharmony_ci * operands. */ 1098c2ecf20Sopenharmony_ci#define DEFINE_TESTOP(fn, op, prefix, postfix, eh) \ 1108c2ecf20Sopenharmony_cistatic inline unsigned long fn( \ 1118c2ecf20Sopenharmony_ci unsigned long mask, \ 1128c2ecf20Sopenharmony_ci volatile unsigned long *_p) \ 1138c2ecf20Sopenharmony_ci{ \ 1148c2ecf20Sopenharmony_ci unsigned long old, t; \ 1158c2ecf20Sopenharmony_ci unsigned long *p = (unsigned long *)_p; \ 1168c2ecf20Sopenharmony_ci __asm__ __volatile__ ( \ 1178c2ecf20Sopenharmony_ci prefix \ 1188c2ecf20Sopenharmony_ci"1:" PPC_LLARX(%0,0,%3,eh) "\n" \ 1198c2ecf20Sopenharmony_ci stringify_in_c(op) "%1,%0,%2\n" \ 1208c2ecf20Sopenharmony_ci PPC_STLCX "%1,0,%3\n" \ 1218c2ecf20Sopenharmony_ci "bne- 1b\n" \ 1228c2ecf20Sopenharmony_ci postfix \ 1238c2ecf20Sopenharmony_ci : "=&r" (old), "=&r" (t) \ 1248c2ecf20Sopenharmony_ci : "r" (mask), "r" (p) \ 1258c2ecf20Sopenharmony_ci : "cc", "memory"); \ 1268c2ecf20Sopenharmony_ci return (old & mask); \ 1278c2ecf20Sopenharmony_ci} 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ciDEFINE_TESTOP(test_and_set_bits, or, PPC_ATOMIC_ENTRY_BARRIER, 1308c2ecf20Sopenharmony_ci PPC_ATOMIC_EXIT_BARRIER, 0) 1318c2ecf20Sopenharmony_ciDEFINE_TESTOP(test_and_set_bits_lock, or, "", 1328c2ecf20Sopenharmony_ci PPC_ACQUIRE_BARRIER, 1) 1338c2ecf20Sopenharmony_ciDEFINE_TESTOP(test_and_clear_bits, andc, PPC_ATOMIC_ENTRY_BARRIER, 1348c2ecf20Sopenharmony_ci PPC_ATOMIC_EXIT_BARRIER, 0) 1358c2ecf20Sopenharmony_ciDEFINE_TESTOP(test_and_change_bits, xor, PPC_ATOMIC_ENTRY_BARRIER, 1368c2ecf20Sopenharmony_ci PPC_ATOMIC_EXIT_BARRIER, 0) 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_cistatic inline int arch_test_and_set_bit(unsigned long nr, 1398c2ecf20Sopenharmony_ci volatile unsigned long *addr) 1408c2ecf20Sopenharmony_ci{ 1418c2ecf20Sopenharmony_ci return test_and_set_bits(BIT_MASK(nr), addr + BIT_WORD(nr)) != 0; 1428c2ecf20Sopenharmony_ci} 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_cistatic inline int arch_test_and_set_bit_lock(unsigned long nr, 1458c2ecf20Sopenharmony_ci volatile unsigned long *addr) 1468c2ecf20Sopenharmony_ci{ 1478c2ecf20Sopenharmony_ci return test_and_set_bits_lock(BIT_MASK(nr), 1488c2ecf20Sopenharmony_ci addr + BIT_WORD(nr)) != 0; 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_cistatic inline int arch_test_and_clear_bit(unsigned long nr, 1528c2ecf20Sopenharmony_ci volatile unsigned long *addr) 1538c2ecf20Sopenharmony_ci{ 1548c2ecf20Sopenharmony_ci return test_and_clear_bits(BIT_MASK(nr), addr + BIT_WORD(nr)) != 0; 1558c2ecf20Sopenharmony_ci} 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_cistatic inline int arch_test_and_change_bit(unsigned long nr, 1588c2ecf20Sopenharmony_ci volatile unsigned long *addr) 1598c2ecf20Sopenharmony_ci{ 1608c2ecf20Sopenharmony_ci return test_and_change_bits(BIT_MASK(nr), addr + BIT_WORD(nr)) != 0; 1618c2ecf20Sopenharmony_ci} 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC64 1648c2ecf20Sopenharmony_cistatic inline unsigned long 1658c2ecf20Sopenharmony_ciclear_bit_unlock_return_word(int nr, volatile unsigned long *addr) 1668c2ecf20Sopenharmony_ci{ 1678c2ecf20Sopenharmony_ci unsigned long old, t; 1688c2ecf20Sopenharmony_ci unsigned long *p = (unsigned long *)addr + BIT_WORD(nr); 1698c2ecf20Sopenharmony_ci unsigned long mask = BIT_MASK(nr); 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci __asm__ __volatile__ ( 1728c2ecf20Sopenharmony_ci PPC_RELEASE_BARRIER 1738c2ecf20Sopenharmony_ci"1:" PPC_LLARX(%0,0,%3,0) "\n" 1748c2ecf20Sopenharmony_ci "andc %1,%0,%2\n" 1758c2ecf20Sopenharmony_ci PPC_STLCX "%1,0,%3\n" 1768c2ecf20Sopenharmony_ci "bne- 1b\n" 1778c2ecf20Sopenharmony_ci : "=&r" (old), "=&r" (t) 1788c2ecf20Sopenharmony_ci : "r" (mask), "r" (p) 1798c2ecf20Sopenharmony_ci : "cc", "memory"); 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci return old; 1828c2ecf20Sopenharmony_ci} 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci/* 1858c2ecf20Sopenharmony_ci * This is a special function for mm/filemap.c 1868c2ecf20Sopenharmony_ci * Bit 7 corresponds to PG_waiters. 1878c2ecf20Sopenharmony_ci */ 1888c2ecf20Sopenharmony_ci#define arch_clear_bit_unlock_is_negative_byte(nr, addr) \ 1898c2ecf20Sopenharmony_ci (clear_bit_unlock_return_word(nr, addr) & BIT_MASK(7)) 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci#endif /* CONFIG_PPC64 */ 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci#include <asm-generic/bitops/non-atomic.h> 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_cistatic inline void arch___clear_bit_unlock(int nr, volatile unsigned long *addr) 1968c2ecf20Sopenharmony_ci{ 1978c2ecf20Sopenharmony_ci __asm__ __volatile__(PPC_RELEASE_BARRIER "" ::: "memory"); 1988c2ecf20Sopenharmony_ci __clear_bit(nr, addr); 1998c2ecf20Sopenharmony_ci} 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci/* 2028c2ecf20Sopenharmony_ci * Return the zero-based bit position (LE, not IBM bit numbering) of 2038c2ecf20Sopenharmony_ci * the most significant 1-bit in a double word. 2048c2ecf20Sopenharmony_ci */ 2058c2ecf20Sopenharmony_ci#define __ilog2(x) ilog2(x) 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci#include <asm-generic/bitops/ffz.h> 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci#include <asm-generic/bitops/builtin-__ffs.h> 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci#include <asm-generic/bitops/builtin-ffs.h> 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci/* 2148c2ecf20Sopenharmony_ci * fls: find last (most-significant) bit set. 2158c2ecf20Sopenharmony_ci * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. 2168c2ecf20Sopenharmony_ci */ 2178c2ecf20Sopenharmony_cistatic inline int fls(unsigned int x) 2188c2ecf20Sopenharmony_ci{ 2198c2ecf20Sopenharmony_ci int lz; 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci if (__builtin_constant_p(x)) 2228c2ecf20Sopenharmony_ci return x ? 32 - __builtin_clz(x) : 0; 2238c2ecf20Sopenharmony_ci asm("cntlzw %0,%1" : "=r" (lz) : "r" (x)); 2248c2ecf20Sopenharmony_ci return 32 - lz; 2258c2ecf20Sopenharmony_ci} 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci#include <asm-generic/bitops/builtin-__fls.h> 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci/* 2308c2ecf20Sopenharmony_ci * 64-bit can do this using one cntlzd (count leading zeroes doubleword) 2318c2ecf20Sopenharmony_ci * instruction; for 32-bit we use the generic version, which does two 2328c2ecf20Sopenharmony_ci * 32-bit fls calls. 2338c2ecf20Sopenharmony_ci */ 2348c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC64 2358c2ecf20Sopenharmony_cistatic inline int fls64(__u64 x) 2368c2ecf20Sopenharmony_ci{ 2378c2ecf20Sopenharmony_ci int lz; 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci if (__builtin_constant_p(x)) 2408c2ecf20Sopenharmony_ci return x ? 64 - __builtin_clzll(x) : 0; 2418c2ecf20Sopenharmony_ci asm("cntlzd %0,%1" : "=r" (lz) : "r" (x)); 2428c2ecf20Sopenharmony_ci return 64 - lz; 2438c2ecf20Sopenharmony_ci} 2448c2ecf20Sopenharmony_ci#else 2458c2ecf20Sopenharmony_ci#include <asm-generic/bitops/fls64.h> 2468c2ecf20Sopenharmony_ci#endif 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC64 2498c2ecf20Sopenharmony_ciunsigned int __arch_hweight8(unsigned int w); 2508c2ecf20Sopenharmony_ciunsigned int __arch_hweight16(unsigned int w); 2518c2ecf20Sopenharmony_ciunsigned int __arch_hweight32(unsigned int w); 2528c2ecf20Sopenharmony_ciunsigned long __arch_hweight64(__u64 w); 2538c2ecf20Sopenharmony_ci#include <asm-generic/bitops/const_hweight.h> 2548c2ecf20Sopenharmony_ci#else 2558c2ecf20Sopenharmony_ci#include <asm-generic/bitops/hweight.h> 2568c2ecf20Sopenharmony_ci#endif 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci#include <asm-generic/bitops/find.h> 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci/* wrappers that deal with KASAN instrumentation */ 2618c2ecf20Sopenharmony_ci#include <asm-generic/bitops/instrumented-atomic.h> 2628c2ecf20Sopenharmony_ci#include <asm-generic/bitops/instrumented-lock.h> 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci/* Little-endian versions */ 2658c2ecf20Sopenharmony_ci#include <asm-generic/bitops/le.h> 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci/* Bitmap functions for the ext2 filesystem */ 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci#include <asm-generic/bitops/ext2-atomic-setbit.h> 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_ci#include <asm-generic/bitops/sched.h> 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ci#endif /* __KERNEL__ */ 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci#endif /* _ASM_POWERPC_BITOPS_H */ 276