2 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
12 #ifndef _LINUX_BITOPS_H
13 #error only <linux/bitops.h> can be included directly
20 #include <linux/types.h>
21 #include <linux/compiler.h>
22 #include <asm/barrier.h>
25 * Hardware assisted read-modify-write using ARC700 LLOCK/SCOND insns.
26 * The Kconfig glue ensures that in SMP, this is only set if the container
27 * SoC/platform has cross-core coherent LLOCK/SCOND
29 #if defined(CONFIG_ARC_HAS_LLSC)
31 static inline void set_bit(unsigned long nr
, volatile unsigned long *m
)
37 if (__builtin_constant_p(nr
))
41 "1: llock %0, [%1] \n"
50 static inline void clear_bit(unsigned long nr
, volatile unsigned long *m
)
56 if (__builtin_constant_p(nr
))
60 "1: llock %0, [%1] \n"
69 static inline void change_bit(unsigned long nr
, volatile unsigned long *m
)
75 if (__builtin_constant_p(nr
))
79 "1: llock %0, [%1] \n"
92 * set it and return 0 (old value)
94 * return 1 (old value).
96 * Since ARC lacks a equivalent h/w primitive, the bit is set unconditionally
97 * and the old value of bit is returned
99 static inline int test_and_set_bit(unsigned long nr
, volatile unsigned long *m
)
101 unsigned long old
, temp
;
105 if (__builtin_constant_p(nr
))
108 __asm__
__volatile__(
109 "1: llock %0, [%2] \n"
110 " bset %1, %0, %3 \n"
113 : "=&r"(old
), "=&r"(temp
)
117 return (old
& (1 << nr
)) != 0;
121 test_and_clear_bit(unsigned long nr
, volatile unsigned long *m
)
123 unsigned int old
, temp
;
127 if (__builtin_constant_p(nr
))
130 __asm__
__volatile__(
131 "1: llock %0, [%2] \n"
132 " bclr %1, %0, %3 \n"
135 : "=&r"(old
), "=&r"(temp
)
139 return (old
& (1 << nr
)) != 0;
143 test_and_change_bit(unsigned long nr
, volatile unsigned long *m
)
145 unsigned int old
, temp
;
149 if (__builtin_constant_p(nr
))
152 __asm__
__volatile__(
153 "1: llock %0, [%2] \n"
154 " bxor %1, %0, %3 \n"
157 : "=&r"(old
), "=&r"(temp
)
161 return (old
& (1 << nr
)) != 0;
164 #else /* !CONFIG_ARC_HAS_LLSC */
169 * Non hardware assisted Atomic-R-M-W
170 * Locking would change to irq-disabling only (UP) and spinlocks (SMP)
172 * There's "significant" micro-optimization in writing our own variants of
173 * bitops (over generic variants)
175 * (1) The generic APIs have "signed" @nr while we have it "unsigned"
176 * This avoids extra code to be generated for pointer arithmatic, since
177 * is "not sure" that index is NOT -ve
178 * (2) Utilize the fact that ARCompact bit fidding insn (BSET/BCLR/ASL) etc
179 * only consider bottom 5 bits of @nr, so NO need to mask them off.
180 * (GCC Quirk: however for constant @nr we still need to do the masking
184 static inline void set_bit(unsigned long nr
, volatile unsigned long *m
)
186 unsigned long temp
, flags
;
189 if (__builtin_constant_p(nr
))
195 *m
= temp
| (1UL << nr
);
197 bitops_unlock(flags
);
200 static inline void clear_bit(unsigned long nr
, volatile unsigned long *m
)
202 unsigned long temp
, flags
;
205 if (__builtin_constant_p(nr
))
211 *m
= temp
& ~(1UL << nr
);
213 bitops_unlock(flags
);
216 static inline void change_bit(unsigned long nr
, volatile unsigned long *m
)
218 unsigned long temp
, flags
;
221 if (__builtin_constant_p(nr
))
227 *m
= temp
^ (1UL << nr
);
229 bitops_unlock(flags
);
232 static inline int test_and_set_bit(unsigned long nr
, volatile unsigned long *m
)
234 unsigned long old
, flags
;
237 if (__builtin_constant_p(nr
))
243 *m
= old
| (1 << nr
);
245 bitops_unlock(flags
);
247 return (old
& (1 << nr
)) != 0;
251 test_and_clear_bit(unsigned long nr
, volatile unsigned long *m
)
253 unsigned long old
, flags
;
256 if (__builtin_constant_p(nr
))
262 *m
= old
& ~(1 << nr
);
264 bitops_unlock(flags
);
266 return (old
& (1 << nr
)) != 0;
270 test_and_change_bit(unsigned long nr
, volatile unsigned long *m
)
272 unsigned long old
, flags
;
275 if (__builtin_constant_p(nr
))
281 *m
= old
^ (1 << nr
);
283 bitops_unlock(flags
);
285 return (old
& (1 << nr
)) != 0;
288 #endif /* CONFIG_ARC_HAS_LLSC */
290 /***************************************
291 * Non atomic variants
292 **************************************/
294 static inline void __set_bit(unsigned long nr
, volatile unsigned long *m
)
299 if (__builtin_constant_p(nr
))
303 *m
= temp
| (1UL << nr
);
306 static inline void __clear_bit(unsigned long nr
, volatile unsigned long *m
)
311 if (__builtin_constant_p(nr
))
315 *m
= temp
& ~(1UL << nr
);
318 static inline void __change_bit(unsigned long nr
, volatile unsigned long *m
)
323 if (__builtin_constant_p(nr
))
327 *m
= temp
^ (1UL << nr
);
331 __test_and_set_bit(unsigned long nr
, volatile unsigned long *m
)
336 if (__builtin_constant_p(nr
))
340 *m
= old
| (1 << nr
);
342 return (old
& (1 << nr
)) != 0;
346 __test_and_clear_bit(unsigned long nr
, volatile unsigned long *m
)
351 if (__builtin_constant_p(nr
))
355 *m
= old
& ~(1 << nr
);
357 return (old
& (1 << nr
)) != 0;
361 __test_and_change_bit(unsigned long nr
, volatile unsigned long *m
)
366 if (__builtin_constant_p(nr
))
370 *m
= old
^ (1 << nr
);
372 return (old
& (1 << nr
)) != 0;
376 * This routine doesn't need to be atomic.
379 __constant_test_bit(unsigned int nr
, const volatile unsigned long *addr
)
381 return ((1UL << (nr
& 31)) &
382 (((const volatile unsigned int *)addr
)[nr
>> 5])) != 0;
386 __test_bit(unsigned int nr
, const volatile unsigned long *addr
)
392 /* ARC700 only considers 5 bits in bit-fiddling insn */
395 return ((mask
& *addr
) != 0);
398 #define test_bit(nr, addr) (__builtin_constant_p(nr) ? \
399 __constant_test_bit((nr), (addr)) : \
400 __test_bit((nr), (addr)))
403 * Count the number of zeros, starting from MSB
404 * Helper for fls( ) friends
405 * This is a pure count, so (1-32) or (0-31) doesn't apply
406 * It could be 0 to 32, based on num of 0's in there
407 * clz(0x8000_0000) = 0, clz(0xFFFF_FFFF)=0, clz(0) = 32, clz(1) = 31
409 static inline __attribute__ ((const)) int clz(unsigned int x
)
413 __asm__
__volatile__(
416 " add.p %0, %0, 1 \n"
424 static inline int constant_fls(int x
)
430 if (!(x
& 0xffff0000u
)) {
434 if (!(x
& 0xff000000u
)) {
438 if (!(x
& 0xf0000000u
)) {
442 if (!(x
& 0xc0000000u
)) {
446 if (!(x
& 0x80000000u
)) {
454 * fls = Find Last Set in word
456 * fls(1) = 1, fls(0x80000000) = 32, fls(0) = 0
458 static inline __attribute__ ((const)) int fls(unsigned long x
)
460 if (__builtin_constant_p(x
))
461 return constant_fls(x
);
467 * __fls: Similar to fls, but zero based (0-31)
469 static inline __attribute__ ((const)) int __fls(unsigned long x
)
478 * ffs = Find First Set in word (LSB to MSB)
479 * @result: [1-32], 0 if all 0's
481 #define ffs(x) ({ unsigned long __t = (x); fls(__t & -__t); })
484 * __ffs: Similar to ffs, but zero based (0-31)
486 static inline __attribute__ ((const)) int __ffs(unsigned long word
)
491 return ffs(word
) - 1;
495 * ffz = Find First Zero in word.
496 * @return:[0-31], 32 if all 1's
498 #define ffz(x) __ffs(~(x))
500 #include <asm-generic/bitops/hweight.h>
501 #include <asm-generic/bitops/fls64.h>
502 #include <asm-generic/bitops/sched.h>
503 #include <asm-generic/bitops/lock.h>
505 #include <asm-generic/bitops/find.h>
506 #include <asm-generic/bitops/le.h>
507 #include <asm-generic/bitops/ext2-atomic-setbit.h>
509 #endif /* !__ASSEMBLY__ */
511 #endif /* __KERNEL__ */