1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
9 #ifndef _LINUX_BITOPS_H
10 #error only <linux/bitops.h> can be included directly
15 #include <linux/types.h>
16 #include <linux/compiler.h>
17 #include <asm/barrier.h>
18 #ifndef CONFIG_ARC_HAS_LLSC
22 #ifdef CONFIG_ARC_HAS_LLSC
25 * Hardware assisted Atomic-R-M-W
28 #define BIT_OP(op, c_op, asm_op) \
29 static inline void op##_bit(unsigned long nr, volatile unsigned long *m)\
37 __asm__ __volatile__( \
38 "1: llock %0, [%1] \n" \
39 " " #asm_op " %0, %0, %2 \n" \
40 " scond %0, [%1] \n" \
42 : "=&r"(temp) /* Early clobber, to prevent reg reuse */ \
43 : "r"(m), /* Not "m": llock only supports reg direct addr mode */ \
52 * set it and return 0 (old value)
54 * return 1 (old value).
56 * Since ARC lacks a equivalent h/w primitive, the bit is set unconditionally
57 * and the old value of bit is returned
59 #define TEST_N_BIT_OP(op, c_op, asm_op) \
60 static inline int test_and_##op##_bit(unsigned long nr, volatile unsigned long *m)\
62 unsigned long old, temp; \
69 * Explicit full memory barrier needed before/after as \
70 * LLOCK/SCOND themselves don't provide any such smenatic \
74 __asm__ __volatile__( \
75 "1: llock %0, [%2] \n" \
76 " " #asm_op " %1, %0, %3 \n" \
77 " scond %1, [%2] \n" \
79 : "=&r"(old), "=&r"(temp) \
85 return (old & (1 << nr)) != 0; \
88 #else /* !CONFIG_ARC_HAS_LLSC */
91 * Non hardware assisted Atomic-R-M-W
92 * Locking would change to irq-disabling only (UP) and spinlocks (SMP)
94 * There's "significant" micro-optimization in writing our own variants of
95 * bitops (over generic variants)
97 * (1) The generic APIs have "signed" @nr while we have it "unsigned"
98 * This avoids extra code to be generated for pointer arithmatic, since
99 * is "not sure" that index is NOT -ve
100 * (2) Utilize the fact that ARCompact bit fidding insn (BSET/BCLR/ASL) etc
101 * only consider bottom 5 bits of @nr, so NO need to mask them off.
102 * (GCC Quirk: however for constant @nr we still need to do the masking
106 #define BIT_OP(op, c_op, asm_op) \
107 static inline void op##_bit(unsigned long nr, volatile unsigned long *m)\
109 unsigned long temp, flags; \
113 * spin lock/unlock provide the needed smp_mb() before/after \
115 bitops_lock(flags); \
118 *m = temp c_op (1UL << (nr & 0x1f)); \
120 bitops_unlock(flags); \
123 #define TEST_N_BIT_OP(op, c_op, asm_op) \
124 static inline int test_and_##op##_bit(unsigned long nr, volatile unsigned long *m)\
126 unsigned long old, flags; \
129 bitops_lock(flags); \
132 *m = old c_op (1UL << (nr & 0x1f)); \
134 bitops_unlock(flags); \
136 return (old & (1UL << (nr & 0x1f))) != 0; \
141 /***************************************
142 * Non atomic variants
143 **************************************/
145 #define __BIT_OP(op, c_op, asm_op) \
146 static inline void __##op##_bit(unsigned long nr, volatile unsigned long *m) \
148 unsigned long temp; \
152 *m = temp c_op (1UL << (nr & 0x1f)); \
155 #define __TEST_N_BIT_OP(op, c_op, asm_op) \
156 static inline int __test_and_##op##_bit(unsigned long nr, volatile unsigned long *m)\
162 *m = old c_op (1UL << (nr & 0x1f)); \
164 return (old & (1UL << (nr & 0x1f))) != 0; \
167 #define BIT_OPS(op, c_op, asm_op) \
169 /* set_bit(), clear_bit(), change_bit() */ \
170 BIT_OP(op, c_op, asm_op) \
172 /* test_and_set_bit(), test_and_clear_bit(), test_and_change_bit() */\
173 TEST_N_BIT_OP(op, c_op, asm_op) \
175 /* __set_bit(), __clear_bit(), __change_bit() */ \
176 __BIT_OP(op, c_op, asm_op) \
178 /* __test_and_set_bit(), __test_and_clear_bit(), __test_and_change_bit() */\
179 __TEST_N_BIT_OP(op, c_op, asm_op)
181 BIT_OPS(set
, |, bset
)
182 BIT_OPS(clear
, & ~, bclr
)
183 BIT_OPS(change
, ^, bxor
)
186 * This routine doesn't need to be atomic.
189 test_bit(unsigned int nr
, const volatile unsigned long *addr
)
195 mask
= 1UL << (nr
& 0x1f);
197 return ((mask
& *addr
) != 0);
200 #ifdef CONFIG_ISA_ARCOMPACT
203 * Count the number of zeros, starting from MSB
204 * Helper for fls( ) friends
205 * This is a pure count, so (1-32) or (0-31) doesn't apply
206 * It could be 0 to 32, based on num of 0's in there
207 * clz(0x8000_0000) = 0, clz(0xFFFF_FFFF)=0, clz(0) = 32, clz(1) = 31
209 static inline __attribute__ ((const)) int clz(unsigned int x
)
213 __asm__
__volatile__(
216 " add.p %0, %0, 1 \n"
224 static inline int constant_fls(unsigned int x
)
230 if (!(x
& 0xffff0000u
)) {
234 if (!(x
& 0xff000000u
)) {
238 if (!(x
& 0xf0000000u
)) {
242 if (!(x
& 0xc0000000u
)) {
246 if (!(x
& 0x80000000u
))
252 * fls = Find Last Set in word
254 * fls(1) = 1, fls(0x80000000) = 32, fls(0) = 0
256 static inline __attribute__ ((const)) int fls(unsigned int x
)
258 if (__builtin_constant_p(x
))
259 return constant_fls(x
);
265 * __fls: Similar to fls, but zero based (0-31)
267 static inline __attribute__ ((const)) int __fls(unsigned long x
)
276 * ffs = Find First Set in word (LSB to MSB)
277 * @result: [1-32], 0 if all 0's
279 #define ffs(x) ({ unsigned long __t = (x); fls(__t & -__t); })
282 * __ffs: Similar to ffs, but zero based (0-31)
284 static inline __attribute__ ((const)) unsigned long __ffs(unsigned long word
)
289 return ffs(word
) - 1;
292 #else /* CONFIG_ISA_ARCV2 */
295 * fls = Find Last Set in word
297 * fls(1) = 1, fls(0x80000000) = 32, fls(0) = 0
299 static inline __attribute__ ((const)) int fls(unsigned long x
)
304 " fls.f %0, %1 \n" /* 0:31; 0(Z) if src 0 */
305 " add.nz %0, %0, 1 \n" /* 0:31 -> 1:32 */
306 : "=r"(n
) /* Early clobber not needed */
314 * __fls: Similar to fls, but zero based (0-31). Also 0 if no bit set
316 static inline __attribute__ ((const)) int __fls(unsigned long x
)
318 /* FLS insn has exactly same semantics as the API */
319 return __builtin_arc_fls(x
);
323 * ffs = Find First Set in word (LSB to MSB)
324 * @result: [1-32], 0 if all 0's
326 static inline __attribute__ ((const)) int ffs(unsigned long x
)
331 " ffs.f %0, %1 \n" /* 0:31; 31(Z) if src 0 */
332 " add.nz %0, %0, 1 \n" /* 0:31 -> 1:32 */
333 " mov.z %0, 0 \n" /* 31(Z)-> 0 */
334 : "=r"(n
) /* Early clobber not needed */
342 * __ffs: Similar to ffs, but zero based (0-31)
344 static inline __attribute__ ((const)) unsigned long __ffs(unsigned long x
)
349 " ffs.f %0, %1 \n" /* 0:31; 31(Z) if src 0 */
350 " mov.z %0, 0 \n" /* 31(Z)-> 0 */
359 #endif /* CONFIG_ISA_ARCOMPACT */
362 * ffz = Find First Zero in word.
363 * @return:[0-31], 32 if all 1's
365 #define ffz(x) __ffs(~(x))
367 #include <asm-generic/bitops/hweight.h>
368 #include <asm-generic/bitops/fls64.h>
369 #include <asm-generic/bitops/sched.h>
370 #include <asm-generic/bitops/lock.h>
372 #include <asm-generic/bitops/find.h>
373 #include <asm-generic/bitops/le.h>
374 #include <asm-generic/bitops/ext2-atomic-setbit.h>
376 #endif /* !__ASSEMBLY__ */