1 /* bitops.h: bit operations for the Fujitsu FR-V CPUs
3 * For an explanation of how atomic ops work in this arch, see:
4 * Documentation/frv/atomic-ops.txt
6 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
7 * Written by David Howells (dhowells@redhat.com)
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
17 #include <linux/compiler.h>
18 #include <asm/byteorder.h>
22 #ifndef _LINUX_BITOPS_H
23 #error only <linux/bitops.h> can be included directly
26 #include <asm-generic/bitops/ffz.h>
28 #ifndef CONFIG_FRV_OUTOFLINE_ATOMIC_OPS
30 unsigned long atomic_test_and_ANDNOT_mask(unsigned long mask
, volatile unsigned long *v
)
32 unsigned long old
, tmp
;
36 " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
38 " ld.p %M0,%1 \n" /* LD.P/ORCR are atomic */
39 " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
41 " cst.p %2,%M0 ,cc3,#1 \n" /* if store happens... */
42 " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* ... clear ICC3.Z */
44 : "+U"(*v
), "=&r"(old
), "=r"(tmp
)
46 : "memory", "cc7", "cc3", "icc3"
53 unsigned long atomic_test_and_OR_mask(unsigned long mask
, volatile unsigned long *v
)
55 unsigned long old
, tmp
;
59 " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
61 " ld.p %M0,%1 \n" /* LD.P/ORCR are atomic */
62 " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
64 " cst.p %2,%M0 ,cc3,#1 \n" /* if store happens... */
65 " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* ... clear ICC3.Z */
67 : "+U"(*v
), "=&r"(old
), "=r"(tmp
)
69 : "memory", "cc7", "cc3", "icc3"
76 unsigned long atomic_test_and_XOR_mask(unsigned long mask
, volatile unsigned long *v
)
78 unsigned long old
, tmp
;
82 " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
84 " ld.p %M0,%1 \n" /* LD.P/ORCR are atomic */
85 " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
87 " cst.p %2,%M0 ,cc3,#1 \n" /* if store happens... */
88 " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* ... clear ICC3.Z */
90 : "+U"(*v
), "=&r"(old
), "=r"(tmp
)
92 : "memory", "cc7", "cc3", "icc3"
100 extern unsigned long atomic_test_and_ANDNOT_mask(unsigned long mask
, volatile unsigned long *v
);
101 extern unsigned long atomic_test_and_OR_mask(unsigned long mask
, volatile unsigned long *v
);
102 extern unsigned long atomic_test_and_XOR_mask(unsigned long mask
, volatile unsigned long *v
);
106 #define atomic_clear_mask(mask, v) atomic_test_and_ANDNOT_mask((mask), (v))
107 #define atomic_set_mask(mask, v) atomic_test_and_OR_mask((mask), (v))
109 static inline int test_and_clear_bit(unsigned long nr
, volatile void *addr
)
111 volatile unsigned long *ptr
= addr
;
112 unsigned long mask
= 1UL << (nr
& 31);
114 return (atomic_test_and_ANDNOT_mask(mask
, ptr
) & mask
) != 0;
117 static inline int test_and_set_bit(unsigned long nr
, volatile void *addr
)
119 volatile unsigned long *ptr
= addr
;
120 unsigned long mask
= 1UL << (nr
& 31);
122 return (atomic_test_and_OR_mask(mask
, ptr
) & mask
) != 0;
125 static inline int test_and_change_bit(unsigned long nr
, volatile void *addr
)
127 volatile unsigned long *ptr
= addr
;
128 unsigned long mask
= 1UL << (nr
& 31);
130 return (atomic_test_and_XOR_mask(mask
, ptr
) & mask
) != 0;
133 static inline void clear_bit(unsigned long nr
, volatile void *addr
)
135 test_and_clear_bit(nr
, addr
);
138 static inline void set_bit(unsigned long nr
, volatile void *addr
)
140 test_and_set_bit(nr
, addr
);
143 static inline void change_bit(unsigned long nr
, volatile void *addr
)
145 test_and_change_bit(nr
, addr
);
148 static inline void __clear_bit(unsigned long nr
, volatile void *addr
)
150 volatile unsigned long *a
= addr
;
154 mask
= 1 << (nr
& 31);
158 static inline void __set_bit(unsigned long nr
, volatile void *addr
)
160 volatile unsigned long *a
= addr
;
164 mask
= 1 << (nr
& 31);
168 static inline void __change_bit(unsigned long nr
, volatile void *addr
)
170 volatile unsigned long *a
= addr
;
174 mask
= 1 << (nr
& 31);
178 static inline int __test_and_clear_bit(unsigned long nr
, volatile void *addr
)
180 volatile unsigned long *a
= addr
;
184 mask
= 1 << (nr
& 31);
185 retval
= (mask
& *a
) != 0;
190 static inline int __test_and_set_bit(unsigned long nr
, volatile void *addr
)
192 volatile unsigned long *a
= addr
;
196 mask
= 1 << (nr
& 31);
197 retval
= (mask
& *a
) != 0;
202 static inline int __test_and_change_bit(unsigned long nr
, volatile void *addr
)
204 volatile unsigned long *a
= addr
;
208 mask
= 1 << (nr
& 31);
209 retval
= (mask
& *a
) != 0;
215 * This routine doesn't need to be atomic.
218 __constant_test_bit(unsigned long nr
, const volatile void *addr
)
220 return ((1UL << (nr
& 31)) & (((const volatile unsigned int *) addr
)[nr
>> 5])) != 0;
223 static inline int __test_bit(unsigned long nr
, const volatile void *addr
)
225 int * a
= (int *) addr
;
229 mask
= 1 << (nr
& 0x1f);
230 return ((mask
& *a
) != 0);
233 #define test_bit(nr,addr) \
234 (__builtin_constant_p(nr) ? \
235 __constant_test_bit((nr),(addr)) : \
236 __test_bit((nr),(addr)))
238 #include <asm-generic/bitops/find.h>
241 * fls - find last bit set
242 * @x: the word to search
244 * This is defined the same way as ffs:
245 * - return 32..1 to indicate bit 31..0 most significant bit set
246 * - return 0 to indicate no bits set
252 asm(" subcc %1,gr0,gr0,icc0 \n" \
253 " ckne icc0,cc4 \n" \
254 " cscan.p %1,gr0,%0 ,cc4,#1 \n" \
255 " csub %0,%0,%0 ,cc4,#0 \n" \
256 " csub %2,%0,%0 ,cc4,#1 \n" \
266 * fls64 - find last bit set in a 64-bit value
267 * @n: the value to search
269 * This is defined the same way as ffs:
270 * - return 64..1 to indicate bit 63..0 most significant bit set
271 * - return 0 to indicate no bits set
273 static inline __attribute__((const))
278 struct { u32 h
, l
; };
284 asm(" subcc.p %3,gr0,gr0,icc0 \n"
285 " subcc %4,gr0,gr0,icc1 \n"
288 " norcr cc4,cc5,cc6 \n"
289 " csub.p %0,%0,%0 ,cc6,1 \n"
290 " orcr cc5,cc4,cc4 \n"
291 " andcr cc4,cc5,cc4 \n"
292 " cscan.p %3,gr0,%0 ,cc4,0 \n"
294 " cscan.p %4,gr0,%0 ,cc4,1 \n"
296 " csub.p %1,%0,%0 ,cc4,0 \n"
297 " csub %2,%0,%0 ,cc4,1 \n"
298 : "=&r"(bit
), "=r"(x
), "=r"(y
)
299 : "0r"(_
.h
), "r"(_
.l
)
300 : "icc0", "icc1", "cc4", "cc5", "cc6"
307 * ffs - find first bit set
308 * @x: the word to search
310 * - return 32..1 to indicate bit 31..0 most least significant bit set
311 * - return 0 to indicate no bits set
313 static inline __attribute__((const))
316 /* Note: (x & -x) gives us a mask that is the least significant
317 * (rightmost) 1-bit of the value in x.
323 * __ffs - find first bit set
324 * @x: the word to search
326 * - return 31..0 to indicate bit 31..0 most least significant bit set
327 * - if no bits are set in x, the result is undefined
329 static inline __attribute__((const))
330 int __ffs(unsigned long x
)
333 asm("scan %1,gr0,%0" : "=r"(bit
) : "r"(x
& -x
));
338 * __fls - find last (most-significant) set bit in a long word
339 * @word: the word to search
341 * Undefined if no set bit exists, so code should check against 0 first.
343 static inline unsigned long __fls(unsigned long word
)
346 asm("scan %1,gr0,%0" : "=r"(bit
) : "r"(word
));
351 * special slimline version of fls() for calculating ilog2_u32()
352 * - note: no protection against n == 0
354 #define ARCH_HAS_ILOG2_U32
355 static inline __attribute__((const))
356 int __ilog2_u32(u32 n
)
359 asm("scan %1,gr0,%0" : "=r"(bit
) : "r"(n
));
364 * special slimline version of fls64() for calculating ilog2_u64()
365 * - note: no protection against n == 0
367 #define ARCH_HAS_ILOG2_U64
368 static inline __attribute__((const))
369 int __ilog2_u64(u64 n
)
373 struct { u32 h
, l
; };
379 asm(" subcc %3,gr0,gr0,icc0 \n"
381 " cscan.p %3,gr0,%0 ,cc4,0 \n"
383 " cscan.p %4,gr0,%0 ,cc4,1 \n"
385 " csub.p %1,%0,%0 ,cc4,0 \n"
386 " csub %2,%0,%0 ,cc4,1 \n"
387 : "=&r"(bit
), "=r"(x
), "=r"(y
)
388 : "0r"(_
.h
), "r"(_
.l
)
394 #include <asm-generic/bitops/sched.h>
395 #include <asm-generic/bitops/hweight.h>
396 #include <asm-generic/bitops/lock.h>
398 #include <asm-generic/bitops/le.h>
400 #include <asm-generic/bitops/ext2-atomic-setbit.h>
402 #endif /* __KERNEL__ */
404 #endif /* _ASM_BITOPS_H */